aboutsummaryrefslogtreecommitdiff
path: root/mesalib/src/glsl
diff options
context:
space:
mode:
Diffstat (limited to 'mesalib/src/glsl')
-rw-r--r--mesalib/src/glsl/ir_optimization.h2
-rw-r--r--mesalib/src/glsl/ir_reader.cpp2012
-rw-r--r--mesalib/src/glsl/linker.cpp2
-rw-r--r--mesalib/src/glsl/lower_jumps.cpp1580
4 files changed, 2018 insertions, 1578 deletions
diff --git a/mesalib/src/glsl/ir_optimization.h b/mesalib/src/glsl/ir_optimization.h
index dd265673c..59a040751 100644
--- a/mesalib/src/glsl/ir_optimization.h
+++ b/mesalib/src/glsl/ir_optimization.h
@@ -56,10 +56,8 @@ bool do_if_simplification(exec_list *instructions);
bool do_discard_simplification(exec_list *instructions);
bool lower_if_to_cond_assign(exec_list *instructions, unsigned max_depth = 0);
bool do_mat_op_to_vec(exec_list *instructions);
-bool do_mod_to_fract(exec_list *instructions);
bool do_noop_swizzle(exec_list *instructions);
bool do_structure_splitting(exec_list *instructions);
-bool do_sub_to_add_neg(exec_list *instructions);
bool do_swizzle_swizzle(exec_list *instructions);
bool do_tree_grafting(exec_list *instructions);
bool do_vec_index_to_cond_assign(exec_list *instructions);
diff --git a/mesalib/src/glsl/ir_reader.cpp b/mesalib/src/glsl/ir_reader.cpp
index 201e436be..f3a621734 100644
--- a/mesalib/src/glsl/ir_reader.cpp
+++ b/mesalib/src/glsl/ir_reader.cpp
@@ -1,1005 +1,1007 @@
-/*
- * Copyright © 2010 Intel Corporation
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice (including the next
- * paragraph) shall be included in all copies or substantial portions of the
- * Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- */
-
-#include "ir_reader.h"
-#include "glsl_parser_extras.h"
-#include "glsl_types.h"
-#include "s_expression.h"
-
-const static bool debug = false;
-
-class ir_reader {
-public:
- ir_reader(_mesa_glsl_parse_state *);
-
- void read(exec_list *instructions, const char *src, bool scan_for_protos);
-
-private:
- void *mem_ctx;
- _mesa_glsl_parse_state *state;
-
- void ir_read_error(s_expression *, const char *fmt, ...);
-
- const glsl_type *read_type(s_expression *);
-
- void scan_for_prototypes(exec_list *, s_expression *);
- ir_function *read_function(s_expression *, bool skip_body);
- void read_function_sig(ir_function *, s_expression *, bool skip_body);
-
- void read_instructions(exec_list *, s_expression *, ir_loop *);
- ir_instruction *read_instruction(s_expression *, ir_loop *);
- ir_variable *read_declaration(s_expression *);
- ir_if *read_if(s_expression *, ir_loop *);
- ir_loop *read_loop(s_expression *);
- ir_return *read_return(s_expression *);
- ir_rvalue *read_rvalue(s_expression *);
- ir_assignment *read_assignment(s_expression *);
- ir_expression *read_expression(s_expression *);
- ir_call *read_call(s_expression *);
- ir_swizzle *read_swizzle(s_expression *);
- ir_constant *read_constant(s_expression *);
- ir_texture *read_texture(s_expression *);
-
- ir_dereference *read_dereference(s_expression *);
-};
-
-ir_reader::ir_reader(_mesa_glsl_parse_state *state) : state(state)
-{
- this->mem_ctx = state;
-}
-
-void
-_mesa_glsl_read_ir(_mesa_glsl_parse_state *state, exec_list *instructions,
- const char *src, bool scan_for_protos)
-{
- ir_reader r(state);
- r.read(instructions, src, scan_for_protos);
-}
-
-void
-ir_reader::read(exec_list *instructions, const char *src, bool scan_for_protos)
-{
- s_expression *expr = s_expression::read_expression(mem_ctx, src);
- if (expr == NULL) {
- ir_read_error(NULL, "couldn't parse S-Expression.");
- return;
- }
-
- if (scan_for_protos) {
- scan_for_prototypes(instructions, expr);
- if (state->error)
- return;
- }
-
- read_instructions(instructions, expr, NULL);
- ralloc_free(expr);
-
- if (debug)
- validate_ir_tree(instructions);
-}
-
-void
-ir_reader::ir_read_error(s_expression *expr, const char *fmt, ...)
-{
- va_list ap;
-
- state->error = true;
-
- if (state->current_function != NULL)
- ralloc_asprintf_append(&state->info_log, "In function %s:\n",
- state->current_function->function_name());
- ralloc_strcat(&state->info_log, "error: ");
-
- va_start(ap, fmt);
- ralloc_vasprintf_append(&state->info_log, fmt, ap);
- va_end(ap);
- ralloc_strcat(&state->info_log, "\n");
-
- if (expr != NULL) {
- ralloc_strcat(&state->info_log, "...in this context:\n ");
- expr->print();
- ralloc_strcat(&state->info_log, "\n\n");
- }
-}
-
-const glsl_type *
-ir_reader::read_type(s_expression *expr)
-{
- s_expression *s_base_type;
- s_int *s_size;
-
- s_pattern pat[] = { "array", s_base_type, s_size };
- if (MATCH(expr, pat)) {
- const glsl_type *base_type = read_type(s_base_type);
- if (base_type == NULL) {
- ir_read_error(NULL, "when reading base type of array type");
- return NULL;
- }
-
- return glsl_type::get_array_instance(base_type, s_size->value());
- }
-
- s_symbol *type_sym = SX_AS_SYMBOL(expr);
- if (type_sym == NULL) {
- ir_read_error(expr, "expected <type>");
- return NULL;
- }
-
- const glsl_type *type = state->symbols->get_type(type_sym->value());
- if (type == NULL)
- ir_read_error(expr, "invalid type: %s", type_sym->value());
-
- return type;
-}
-
-
-void
-ir_reader::scan_for_prototypes(exec_list *instructions, s_expression *expr)
-{
- s_list *list = SX_AS_LIST(expr);
- if (list == NULL) {
- ir_read_error(expr, "Expected (<instruction> ...); found an atom.");
- return;
- }
-
- foreach_iter(exec_list_iterator, it, list->subexpressions) {
- s_list *sub = SX_AS_LIST(it.get());
- if (sub == NULL)
- continue; // not a (function ...); ignore it.
-
- s_symbol *tag = SX_AS_SYMBOL(sub->subexpressions.get_head());
- if (tag == NULL || strcmp(tag->value(), "function") != 0)
- continue; // not a (function ...); ignore it.
-
- ir_function *f = read_function(sub, true);
- if (f == NULL)
- return;
- instructions->push_tail(f);
- }
-}
-
-ir_function *
-ir_reader::read_function(s_expression *expr, bool skip_body)
-{
- bool added = false;
- s_symbol *name;
-
- s_pattern pat[] = { "function", name };
- if (!PARTIAL_MATCH(expr, pat)) {
- ir_read_error(expr, "Expected (function <name> (signature ...) ...)");
- return NULL;
- }
-
- ir_function *f = state->symbols->get_function(name->value());
- if (f == NULL) {
- f = new(mem_ctx) ir_function(name->value());
- added = state->symbols->add_function(f);
- assert(added);
- }
-
- exec_list_iterator it = ((s_list *) expr)->subexpressions.iterator();
- it.next(); // skip "function" tag
- it.next(); // skip function name
- for (/* nothing */; it.has_next(); it.next()) {
- s_expression *s_sig = (s_expression *) it.get();
- read_function_sig(f, s_sig, skip_body);
- }
- return added ? f : NULL;
-}
-
-void
-ir_reader::read_function_sig(ir_function *f, s_expression *expr, bool skip_body)
-{
- s_expression *type_expr;
- s_list *paramlist;
- s_list *body_list;
-
- s_pattern pat[] = { "signature", type_expr, paramlist, body_list };
- if (!MATCH(expr, pat)) {
- ir_read_error(expr, "Expected (signature <type> (parameters ...) "
- "(<instruction> ...))");
- return;
- }
-
- const glsl_type *return_type = read_type(type_expr);
- if (return_type == NULL)
- return;
-
- s_symbol *paramtag = SX_AS_SYMBOL(paramlist->subexpressions.get_head());
- if (paramtag == NULL || strcmp(paramtag->value(), "parameters") != 0) {
- ir_read_error(paramlist, "Expected (parameters ...)");
- return;
- }
-
- // Read the parameters list into a temporary place.
- exec_list hir_parameters;
- state->symbols->push_scope();
-
- exec_list_iterator it = paramlist->subexpressions.iterator();
- for (it.next() /* skip "parameters" */; it.has_next(); it.next()) {
- ir_variable *var = read_declaration((s_expression *) it.get());
- if (var == NULL)
- return;
-
- hir_parameters.push_tail(var);
- }
-
- ir_function_signature *sig = f->exact_matching_signature(&hir_parameters);
- if (sig == NULL && skip_body) {
- /* If scanning for prototypes, generate a new signature. */
- sig = new(mem_ctx) ir_function_signature(return_type);
- sig->is_builtin = true;
- f->add_signature(sig);
- } else if (sig != NULL) {
- const char *badvar = sig->qualifiers_match(&hir_parameters);
- if (badvar != NULL) {
- ir_read_error(expr, "function `%s' parameter `%s' qualifiers "
- "don't match prototype", f->name, badvar);
- return;
- }
-
- if (sig->return_type != return_type) {
- ir_read_error(expr, "function `%s' return type doesn't "
- "match prototype", f->name);
- return;
- }
- } else {
- /* No prototype for this body exists - skip it. */
- state->symbols->pop_scope();
- return;
- }
- assert(sig != NULL);
-
- sig->replace_parameters(&hir_parameters);
-
- if (!skip_body && !body_list->subexpressions.is_empty()) {
- if (sig->is_defined) {
- ir_read_error(expr, "function %s redefined", f->name);
- return;
- }
- state->current_function = sig;
- read_instructions(&sig->body, body_list, NULL);
- state->current_function = NULL;
- sig->is_defined = true;
- }
-
- state->symbols->pop_scope();
-}
-
-void
-ir_reader::read_instructions(exec_list *instructions, s_expression *expr,
- ir_loop *loop_ctx)
-{
- // Read in a list of instructions
- s_list *list = SX_AS_LIST(expr);
- if (list == NULL) {
- ir_read_error(expr, "Expected (<instruction> ...); found an atom.");
- return;
- }
-
- foreach_iter(exec_list_iterator, it, list->subexpressions) {
- s_expression *sub = (s_expression*) it.get();
- ir_instruction *ir = read_instruction(sub, loop_ctx);
- if (ir != NULL) {
- /* Global variable declarations should be moved to the top, before
- * any functions that might use them. Functions are added to the
- * instruction stream when scanning for prototypes, so without this
- * hack, they always appear before variable declarations.
- */
- if (state->current_function == NULL && ir->as_variable() != NULL)
- instructions->push_head(ir);
- else
- instructions->push_tail(ir);
- }
- }
-}
-
-
-ir_instruction *
-ir_reader::read_instruction(s_expression *expr, ir_loop *loop_ctx)
-{
- s_symbol *symbol = SX_AS_SYMBOL(expr);
- if (symbol != NULL) {
- if (strcmp(symbol->value(), "break") == 0 && loop_ctx != NULL)
- return new(mem_ctx) ir_loop_jump(ir_loop_jump::jump_break);
- if (strcmp(symbol->value(), "continue") == 0 && loop_ctx != NULL)
- return new(mem_ctx) ir_loop_jump(ir_loop_jump::jump_continue);
- }
-
- s_list *list = SX_AS_LIST(expr);
- if (list == NULL || list->subexpressions.is_empty()) {
- ir_read_error(expr, "Invalid instruction.\n");
- return NULL;
- }
-
- s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
- if (tag == NULL) {
- ir_read_error(expr, "expected instruction tag");
- return NULL;
- }
-
- ir_instruction *inst = NULL;
- if (strcmp(tag->value(), "declare") == 0) {
- inst = read_declaration(list);
- } else if (strcmp(tag->value(), "assign") == 0) {
- inst = read_assignment(list);
- } else if (strcmp(tag->value(), "if") == 0) {
- inst = read_if(list, loop_ctx);
- } else if (strcmp(tag->value(), "loop") == 0) {
- inst = read_loop(list);
- } else if (strcmp(tag->value(), "return") == 0) {
- inst = read_return(list);
- } else if (strcmp(tag->value(), "function") == 0) {
- inst = read_function(list, false);
- } else {
- inst = read_rvalue(list);
- if (inst == NULL)
- ir_read_error(NULL, "when reading instruction");
- }
- return inst;
-}
-
-ir_variable *
-ir_reader::read_declaration(s_expression *expr)
-{
- s_list *s_quals;
- s_expression *s_type;
- s_symbol *s_name;
-
- s_pattern pat[] = { "declare", s_quals, s_type, s_name };
- if (!MATCH(expr, pat)) {
- ir_read_error(expr, "expected (declare (<qualifiers>) <type> <name>)");
- return NULL;
- }
-
- const glsl_type *type = read_type(s_type);
- if (type == NULL)
- return NULL;
-
- ir_variable *var = new(mem_ctx) ir_variable(type, s_name->value(),
- ir_var_auto);
-
- foreach_iter(exec_list_iterator, it, s_quals->subexpressions) {
- s_symbol *qualifier = SX_AS_SYMBOL(it.get());
- if (qualifier == NULL) {
- ir_read_error(expr, "qualifier list must contain only symbols");
- return NULL;
- }
-
- // FINISHME: Check for duplicate/conflicting qualifiers.
- if (strcmp(qualifier->value(), "centroid") == 0) {
- var->centroid = 1;
- } else if (strcmp(qualifier->value(), "invariant") == 0) {
- var->invariant = 1;
- } else if (strcmp(qualifier->value(), "uniform") == 0) {
- var->mode = ir_var_uniform;
- } else if (strcmp(qualifier->value(), "auto") == 0) {
- var->mode = ir_var_auto;
- } else if (strcmp(qualifier->value(), "in") == 0) {
- var->mode = ir_var_in;
- } else if (strcmp(qualifier->value(), "const_in") == 0) {
- var->mode = ir_var_const_in;
- } else if (strcmp(qualifier->value(), "out") == 0) {
- var->mode = ir_var_out;
- } else if (strcmp(qualifier->value(), "inout") == 0) {
- var->mode = ir_var_inout;
- } else if (strcmp(qualifier->value(), "smooth") == 0) {
- var->interpolation = ir_var_smooth;
- } else if (strcmp(qualifier->value(), "flat") == 0) {
- var->interpolation = ir_var_flat;
- } else if (strcmp(qualifier->value(), "noperspective") == 0) {
- var->interpolation = ir_var_noperspective;
- } else {
- ir_read_error(expr, "unknown qualifier: %s", qualifier->value());
- return NULL;
- }
- }
-
- // Add the variable to the symbol table
- state->symbols->add_variable(var);
-
- return var;
-}
-
-
-ir_if *
-ir_reader::read_if(s_expression *expr, ir_loop *loop_ctx)
-{
- s_expression *s_cond;
- s_expression *s_then;
- s_expression *s_else;
-
- s_pattern pat[] = { "if", s_cond, s_then, s_else };
- if (!MATCH(expr, pat)) {
- ir_read_error(expr, "expected (if <condition> (<then>...) (<else>...))");
- return NULL;
- }
-
- ir_rvalue *condition = read_rvalue(s_cond);
- if (condition == NULL) {
- ir_read_error(NULL, "when reading condition of (if ...)");
- return NULL;
- }
-
- ir_if *iff = new(mem_ctx) ir_if(condition);
-
- read_instructions(&iff->then_instructions, s_then, loop_ctx);
- read_instructions(&iff->else_instructions, s_else, loop_ctx);
- if (state->error) {
- delete iff;
- iff = NULL;
- }
- return iff;
-}
-
-
-ir_loop *
-ir_reader::read_loop(s_expression *expr)
-{
- s_expression *s_counter, *s_from, *s_to, *s_inc, *s_body;
-
- s_pattern pat[] = { "loop", s_counter, s_from, s_to, s_inc, s_body };
- if (!MATCH(expr, pat)) {
- ir_read_error(expr, "expected (loop <counter> <from> <to> "
- "<increment> <body>)");
- return NULL;
- }
-
- // FINISHME: actually read the count/from/to fields.
-
- ir_loop *loop = new(mem_ctx) ir_loop;
- read_instructions(&loop->body_instructions, s_body, loop);
- if (state->error) {
- delete loop;
- loop = NULL;
- }
- return loop;
-}
-
-
-ir_return *
-ir_reader::read_return(s_expression *expr)
-{
- s_expression *s_retval;
-
- s_pattern pat[] = { "return", s_retval};
- if (!MATCH(expr, pat)) {
- ir_read_error(expr, "expected (return <rvalue>)");
- return NULL;
- }
-
- ir_rvalue *retval = read_rvalue(s_retval);
- if (retval == NULL) {
- ir_read_error(NULL, "when reading return value");
- return NULL;
- }
-
- return new(mem_ctx) ir_return(retval);
-}
-
-
-ir_rvalue *
-ir_reader::read_rvalue(s_expression *expr)
-{
- s_list *list = SX_AS_LIST(expr);
- if (list == NULL || list->subexpressions.is_empty())
- return NULL;
-
- s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
- if (tag == NULL) {
- ir_read_error(expr, "expected rvalue tag");
- return NULL;
- }
-
- ir_rvalue *rvalue = read_dereference(list);
- if (rvalue != NULL || state->error)
- return rvalue;
- else if (strcmp(tag->value(), "swiz") == 0) {
- rvalue = read_swizzle(list);
- } else if (strcmp(tag->value(), "expression") == 0) {
- rvalue = read_expression(list);
- } else if (strcmp(tag->value(), "call") == 0) {
- rvalue = read_call(list);
- } else if (strcmp(tag->value(), "constant") == 0) {
- rvalue = read_constant(list);
- } else {
- rvalue = read_texture(list);
- if (rvalue == NULL && !state->error)
- ir_read_error(expr, "unrecognized rvalue tag: %s", tag->value());
- }
-
- return rvalue;
-}
-
-ir_assignment *
-ir_reader::read_assignment(s_expression *expr)
-{
- s_expression *cond_expr = NULL;
- s_expression *lhs_expr, *rhs_expr;
- s_list *mask_list;
-
- s_pattern pat4[] = { "assign", mask_list, lhs_expr, rhs_expr };
- s_pattern pat5[] = { "assign", cond_expr, mask_list, lhs_expr, rhs_expr };
- if (!MATCH(expr, pat4) && !MATCH(expr, pat5)) {
- ir_read_error(expr, "expected (assign [<condition>] (<write mask>) "
- "<lhs> <rhs>)");
- return NULL;
- }
-
- ir_rvalue *condition = NULL;
- if (cond_expr != NULL) {
- condition = read_rvalue(cond_expr);
- if (condition == NULL) {
- ir_read_error(NULL, "when reading condition of assignment");
- return NULL;
- }
- }
-
- unsigned mask = 0;
-
- s_symbol *mask_symbol;
- s_pattern mask_pat[] = { mask_symbol };
- if (MATCH(mask_list, mask_pat)) {
- const char *mask_str = mask_symbol->value();
- unsigned mask_length = strlen(mask_str);
- if (mask_length > 4) {
- ir_read_error(expr, "invalid write mask: %s", mask_str);
- return NULL;
- }
-
- const unsigned idx_map[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
-
- for (unsigned i = 0; i < mask_length; i++) {
- if (mask_str[i] < 'w' || mask_str[i] > 'z') {
- ir_read_error(expr, "write mask contains invalid character: %c",
- mask_str[i]);
- return NULL;
- }
- mask |= 1 << idx_map[mask_str[i] - 'w'];
- }
- } else if (!mask_list->subexpressions.is_empty()) {
- ir_read_error(mask_list, "expected () or (<write mask>)");
- return NULL;
- }
-
- ir_dereference *lhs = read_dereference(lhs_expr);
- if (lhs == NULL) {
- ir_read_error(NULL, "when reading left-hand side of assignment");
- return NULL;
- }
-
- ir_rvalue *rhs = read_rvalue(rhs_expr);
- if (rhs == NULL) {
- ir_read_error(NULL, "when reading right-hand side of assignment");
- return NULL;
- }
-
- if (mask == 0 && (lhs->type->is_vector() || lhs->type->is_scalar())) {
- ir_read_error(expr, "non-zero write mask required.");
- return NULL;
- }
-
- return new(mem_ctx) ir_assignment(lhs, rhs, condition, mask);
-}
-
-ir_call *
-ir_reader::read_call(s_expression *expr)
-{
- s_symbol *name;
- s_list *params;
-
- s_pattern pat[] = { "call", name, params };
- if (!MATCH(expr, pat)) {
- ir_read_error(expr, "expected (call <name> (<param> ...))");
- return NULL;
- }
-
- exec_list parameters;
-
- foreach_iter(exec_list_iterator, it, params->subexpressions) {
- s_expression *expr = (s_expression*) it.get();
- ir_rvalue *param = read_rvalue(expr);
- if (param == NULL) {
- ir_read_error(expr, "when reading parameter to function call");
- return NULL;
- }
- parameters.push_tail(param);
- }
-
- ir_function *f = state->symbols->get_function(name->value());
- if (f == NULL) {
- ir_read_error(expr, "found call to undefined function %s",
- name->value());
- return NULL;
- }
-
- ir_function_signature *callee = f->matching_signature(&parameters);
- if (callee == NULL) {
- ir_read_error(expr, "couldn't find matching signature for function "
- "%s", name->value());
- return NULL;
- }
-
- return new(mem_ctx) ir_call(callee, &parameters);
-}
-
-ir_expression *
-ir_reader::read_expression(s_expression *expr)
-{
- s_expression *s_type;
- s_symbol *s_op;
- s_expression *s_arg1;
-
- s_pattern pat[] = { "expression", s_type, s_op, s_arg1 };
- if (!PARTIAL_MATCH(expr, pat)) {
- ir_read_error(expr, "expected (expression <type> <operator> "
- "<operand> [<operand>])");
- return NULL;
- }
- s_expression *s_arg2 = (s_expression *) s_arg1->next; // may be tail sentinel
-
- const glsl_type *type = read_type(s_type);
- if (type == NULL)
- return NULL;
-
- /* Read the operator */
- ir_expression_operation op = ir_expression::get_operator(s_op->value());
- if (op == (ir_expression_operation) -1) {
- ir_read_error(expr, "invalid operator: %s", s_op->value());
- return NULL;
- }
-
- unsigned num_operands = ir_expression::get_num_operands(op);
- if (num_operands == 1 && !s_arg1->next->is_tail_sentinel()) {
- ir_read_error(expr, "expected (expression <type> %s <operand>)",
- s_op->value());
- return NULL;
- }
-
- ir_rvalue *arg1 = read_rvalue(s_arg1);
- ir_rvalue *arg2 = NULL;
- if (arg1 == NULL) {
- ir_read_error(NULL, "when reading first operand of %s", s_op->value());
- return NULL;
- }
-
- if (num_operands == 2) {
- if (s_arg2->is_tail_sentinel() || !s_arg2->next->is_tail_sentinel()) {
- ir_read_error(expr, "expected (expression <type> %s <operand> "
- "<operand>)", s_op->value());
- return NULL;
- }
- arg2 = read_rvalue(s_arg2);
- if (arg2 == NULL) {
- ir_read_error(NULL, "when reading second operand of %s",
- s_op->value());
- return NULL;
- }
- }
-
- return new(mem_ctx) ir_expression(op, type, arg1, arg2);
-}
-
-ir_swizzle *
-ir_reader::read_swizzle(s_expression *expr)
-{
- s_symbol *swiz;
- s_expression *sub;
-
- s_pattern pat[] = { "swiz", swiz, sub };
- if (!MATCH(expr, pat)) {
- ir_read_error(expr, "expected (swiz <swizzle> <rvalue>)");
- return NULL;
- }
-
- if (strlen(swiz->value()) > 4) {
- ir_read_error(expr, "expected a valid swizzle; found %s", swiz->value());
- return NULL;
- }
-
- ir_rvalue *rvalue = read_rvalue(sub);
- if (rvalue == NULL)
- return NULL;
-
- ir_swizzle *ir = ir_swizzle::create(rvalue, swiz->value(),
- rvalue->type->vector_elements);
- if (ir == NULL)
- ir_read_error(expr, "invalid swizzle");
-
- return ir;
-}
-
-ir_constant *
-ir_reader::read_constant(s_expression *expr)
-{
- s_expression *type_expr;
- s_list *values;
-
- s_pattern pat[] = { "constant", type_expr, values };
- if (!MATCH(expr, pat)) {
- ir_read_error(expr, "expected (constant <type> (...))");
- return NULL;
- }
-
- const glsl_type *type = read_type(type_expr);
- if (type == NULL)
- return NULL;
-
- if (values == NULL) {
- ir_read_error(expr, "expected (constant <type> (...))");
- return NULL;
- }
-
- if (type->is_array()) {
- unsigned elements_supplied = 0;
- exec_list elements;
- foreach_iter(exec_list_iterator, it, values->subexpressions) {
- s_expression *elt = (s_expression *) it.get();
- ir_constant *ir_elt = read_constant(elt);
- if (ir_elt == NULL)
- return NULL;
- elements.push_tail(ir_elt);
- elements_supplied++;
- }
-
- if (elements_supplied != type->length) {
- ir_read_error(values, "expected exactly %u array elements, "
- "given %u", type->length, elements_supplied);
- return NULL;
- }
- return new(mem_ctx) ir_constant(type, &elements);
- }
-
- const glsl_type *const base_type = type->get_base_type();
-
- ir_constant_data data = { { 0 } };
-
- // Read in list of values (at most 16).
- int k = 0;
- foreach_iter(exec_list_iterator, it, values->subexpressions) {
- if (k >= 16) {
- ir_read_error(values, "expected at most 16 numbers");
- return NULL;
- }
-
- s_expression *expr = (s_expression*) it.get();
-
- if (base_type->base_type == GLSL_TYPE_FLOAT) {
- s_number *value = SX_AS_NUMBER(expr);
- if (value == NULL) {
- ir_read_error(values, "expected numbers");
- return NULL;
- }
- data.f[k] = value->fvalue();
- } else {
- s_int *value = SX_AS_INT(expr);
- if (value == NULL) {
- ir_read_error(values, "expected integers");
- return NULL;
- }
-
- switch (base_type->base_type) {
- case GLSL_TYPE_UINT: {
- data.u[k] = value->value();
- break;
- }
- case GLSL_TYPE_INT: {
- data.i[k] = value->value();
- break;
- }
- case GLSL_TYPE_BOOL: {
- data.b[k] = value->value();
- break;
- }
- default:
- ir_read_error(values, "unsupported constant type");
- return NULL;
- }
- }
- ++k;
- }
-
- return new(mem_ctx) ir_constant(type, &data);
-}
-
-ir_dereference *
-ir_reader::read_dereference(s_expression *expr)
-{
- s_symbol *s_var;
- s_expression *s_subject;
- s_expression *s_index;
- s_symbol *s_field;
-
- s_pattern var_pat[] = { "var_ref", s_var };
- s_pattern array_pat[] = { "array_ref", s_subject, s_index };
- s_pattern record_pat[] = { "record_ref", s_subject, s_field };
-
- if (MATCH(expr, var_pat)) {
- ir_variable *var = state->symbols->get_variable(s_var->value());
- if (var == NULL) {
- ir_read_error(expr, "undeclared variable: %s", s_var->value());
- return NULL;
- }
- return new(mem_ctx) ir_dereference_variable(var);
- } else if (MATCH(expr, array_pat)) {
- ir_rvalue *subject = read_rvalue(s_subject);
- if (subject == NULL) {
- ir_read_error(NULL, "when reading the subject of an array_ref");
- return NULL;
- }
-
- ir_rvalue *idx = read_rvalue(s_index);
- if (subject == NULL) {
- ir_read_error(NULL, "when reading the index of an array_ref");
- return NULL;
- }
- return new(mem_ctx) ir_dereference_array(subject, idx);
- } else if (MATCH(expr, record_pat)) {
- ir_rvalue *subject = read_rvalue(s_subject);
- if (subject == NULL) {
- ir_read_error(NULL, "when reading the subject of a record_ref");
- return NULL;
- }
- return new(mem_ctx) ir_dereference_record(subject, s_field->value());
- }
- return NULL;
-}
-
-ir_texture *
-ir_reader::read_texture(s_expression *expr)
-{
- s_symbol *tag = NULL;
- s_expression *s_type = NULL;
- s_expression *s_sampler = NULL;
- s_expression *s_coord = NULL;
- s_expression *s_offset = NULL;
- s_expression *s_proj = NULL;
- s_list *s_shadow = NULL;
- s_expression *s_lod = NULL;
-
- ir_texture_opcode op = ir_tex; /* silence warning */
-
- s_pattern tex_pattern[] =
- { "tex", s_type, s_sampler, s_coord, s_offset, s_proj, s_shadow };
- s_pattern txf_pattern[] =
- { "txf", s_type, s_sampler, s_coord, s_offset, s_lod };
- s_pattern other_pattern[] =
- { tag, s_type, s_sampler, s_coord, s_offset, s_proj, s_shadow, s_lod };
-
- if (MATCH(expr, tex_pattern)) {
- op = ir_tex;
- } else if (MATCH(expr, txf_pattern)) {
- op = ir_txf;
- } else if (MATCH(expr, other_pattern)) {
- op = ir_texture::get_opcode(tag->value());
- if (op == -1)
- return NULL;
- } else {
- ir_read_error(NULL, "unexpected texture pattern");
- return NULL;
- }
-
- ir_texture *tex = new(mem_ctx) ir_texture(op);
-
- // Read return type
- const glsl_type *type = read_type(s_type);
- if (type == NULL) {
- ir_read_error(NULL, "when reading type in (%s ...)",
- tex->opcode_string());
- return NULL;
- }
-
- // Read sampler (must be a deref)
- ir_dereference *sampler = read_dereference(s_sampler);
- if (sampler == NULL) {
- ir_read_error(NULL, "when reading sampler in (%s ...)",
- tex->opcode_string());
- return NULL;
- }
- tex->set_sampler(sampler, type);
-
- // Read coordinate (any rvalue)
- tex->coordinate = read_rvalue(s_coord);
- if (tex->coordinate == NULL) {
- ir_read_error(NULL, "when reading coordinate in (%s ...)",
- tex->opcode_string());
- return NULL;
- }
-
- // Read texel offset - either 0 or an rvalue.
- s_int *si_offset = SX_AS_INT(s_offset);
- if (si_offset == NULL || si_offset->value() != 0) {
- tex->offset = read_rvalue(s_offset);
- if (tex->offset == NULL) {
- ir_read_error(s_offset, "expected 0 or an expression");
- return NULL;
- }
- }
-
- if (op != ir_txf) {
- s_int *proj_as_int = SX_AS_INT(s_proj);
- if (proj_as_int && proj_as_int->value() == 1) {
- tex->projector = NULL;
- } else {
- tex->projector = read_rvalue(s_proj);
- if (tex->projector == NULL) {
- ir_read_error(NULL, "when reading projective divide in (%s ..)",
- tex->opcode_string());
- return NULL;
- }
- }
-
- if (s_shadow->subexpressions.is_empty()) {
- tex->shadow_comparitor = NULL;
- } else {
- tex->shadow_comparitor = read_rvalue(s_shadow);
- if (tex->shadow_comparitor == NULL) {
- ir_read_error(NULL, "when reading shadow comparitor in (%s ..)",
- tex->opcode_string());
- return NULL;
- }
- }
- }
-
- switch (op) {
- case ir_txb:
- tex->lod_info.bias = read_rvalue(s_lod);
- if (tex->lod_info.bias == NULL) {
- ir_read_error(NULL, "when reading LOD bias in (txb ...)");
- return NULL;
- }
- break;
- case ir_txl:
- case ir_txf:
- tex->lod_info.lod = read_rvalue(s_lod);
- if (tex->lod_info.lod == NULL) {
- ir_read_error(NULL, "when reading LOD in (%s ...)",
- tex->opcode_string());
- return NULL;
- }
- break;
- case ir_txd: {
- s_expression *s_dx, *s_dy;
- s_pattern dxdy_pat[] = { s_dx, s_dy };
- if (!MATCH(s_lod, dxdy_pat)) {
- ir_read_error(s_lod, "expected (dPdx dPdy) in (txd ...)");
- return NULL;
- }
- tex->lod_info.grad.dPdx = read_rvalue(s_dx);
- if (tex->lod_info.grad.dPdx == NULL) {
- ir_read_error(NULL, "when reading dPdx in (txd ...)");
- return NULL;
- }
- tex->lod_info.grad.dPdy = read_rvalue(s_dy);
- if (tex->lod_info.grad.dPdy == NULL) {
- ir_read_error(NULL, "when reading dPdy in (txd ...)");
- return NULL;
- }
- break;
- }
- default:
- // tex doesn't have any extra parameters.
- break;
- };
- return tex;
-}
+/*
+ * Copyright © 2010 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "ir_reader.h"
+#include "glsl_parser_extras.h"
+#include "glsl_types.h"
+#include "s_expression.h"
+
+const static bool debug = false;
+
+class ir_reader {
+public:
+ ir_reader(_mesa_glsl_parse_state *);
+
+ void read(exec_list *instructions, const char *src, bool scan_for_protos);
+
+private:
+ void *mem_ctx;
+ _mesa_glsl_parse_state *state;
+
+ void ir_read_error(s_expression *, const char *fmt, ...);
+
+ const glsl_type *read_type(s_expression *);
+
+ void scan_for_prototypes(exec_list *, s_expression *);
+ ir_function *read_function(s_expression *, bool skip_body);
+ void read_function_sig(ir_function *, s_expression *, bool skip_body);
+
+ void read_instructions(exec_list *, s_expression *, ir_loop *);
+ ir_instruction *read_instruction(s_expression *, ir_loop *);
+ ir_variable *read_declaration(s_expression *);
+ ir_if *read_if(s_expression *, ir_loop *);
+ ir_loop *read_loop(s_expression *);
+ ir_return *read_return(s_expression *);
+ ir_rvalue *read_rvalue(s_expression *);
+ ir_assignment *read_assignment(s_expression *);
+ ir_expression *read_expression(s_expression *);
+ ir_call *read_call(s_expression *);
+ ir_swizzle *read_swizzle(s_expression *);
+ ir_constant *read_constant(s_expression *);
+ ir_texture *read_texture(s_expression *);
+
+ ir_dereference *read_dereference(s_expression *);
+};
+
+ir_reader::ir_reader(_mesa_glsl_parse_state *state) : state(state)
+{
+ this->mem_ctx = state;
+}
+
+void
+_mesa_glsl_read_ir(_mesa_glsl_parse_state *state, exec_list *instructions,
+ const char *src, bool scan_for_protos)
+{
+ ir_reader r(state);
+ r.read(instructions, src, scan_for_protos);
+}
+
+void
+ir_reader::read(exec_list *instructions, const char *src, bool scan_for_protos)
+{
+ s_expression *expr = s_expression::read_expression(mem_ctx, src);
+ if (expr == NULL) {
+ ir_read_error(NULL, "couldn't parse S-Expression.");
+ return;
+ }
+
+ if (scan_for_protos) {
+ scan_for_prototypes(instructions, expr);
+ if (state->error)
+ return;
+ }
+
+ read_instructions(instructions, expr, NULL);
+ ralloc_free(expr);
+
+ if (debug)
+ validate_ir_tree(instructions);
+}
+
+void
+ir_reader::ir_read_error(s_expression *expr, const char *fmt, ...)
+{
+ va_list ap;
+
+ state->error = true;
+
+ if (state->current_function != NULL)
+ ralloc_asprintf_append(&state->info_log, "In function %s:\n",
+ state->current_function->function_name());
+ ralloc_strcat(&state->info_log, "error: ");
+
+ va_start(ap, fmt);
+ ralloc_vasprintf_append(&state->info_log, fmt, ap);
+ va_end(ap);
+ ralloc_strcat(&state->info_log, "\n");
+
+ if (expr != NULL) {
+ ralloc_strcat(&state->info_log, "...in this context:\n ");
+ expr->print();
+ ralloc_strcat(&state->info_log, "\n\n");
+ }
+}
+
+const glsl_type *
+ir_reader::read_type(s_expression *expr)
+{
+ s_expression *s_base_type;
+ s_int *s_size;
+
+ s_pattern pat[] = { "array", s_base_type, s_size };
+ if (MATCH(expr, pat)) {
+ const glsl_type *base_type = read_type(s_base_type);
+ if (base_type == NULL) {
+ ir_read_error(NULL, "when reading base type of array type");
+ return NULL;
+ }
+
+ return glsl_type::get_array_instance(base_type, s_size->value());
+ }
+
+ s_symbol *type_sym = SX_AS_SYMBOL(expr);
+ if (type_sym == NULL) {
+ ir_read_error(expr, "expected <type>");
+ return NULL;
+ }
+
+ const glsl_type *type = state->symbols->get_type(type_sym->value());
+ if (type == NULL)
+ ir_read_error(expr, "invalid type: %s", type_sym->value());
+
+ return type;
+}
+
+
+void
+ir_reader::scan_for_prototypes(exec_list *instructions, s_expression *expr)
+{
+ s_list *list = SX_AS_LIST(expr);
+ if (list == NULL) {
+ ir_read_error(expr, "Expected (<instruction> ...); found an atom.");
+ return;
+ }
+
+ foreach_iter(exec_list_iterator, it, list->subexpressions) {
+ s_list *sub = SX_AS_LIST(it.get());
+ if (sub == NULL)
+ continue; // not a (function ...); ignore it.
+
+ s_symbol *tag = SX_AS_SYMBOL(sub->subexpressions.get_head());
+ if (tag == NULL || strcmp(tag->value(), "function") != 0)
+ continue; // not a (function ...); ignore it.
+
+ ir_function *f = read_function(sub, true);
+ if (f == NULL)
+ return;
+ instructions->push_tail(f);
+ }
+}
+
+ir_function *
+ir_reader::read_function(s_expression *expr, bool skip_body)
+{
+ bool added = false;
+ s_symbol *name;
+
+ s_pattern pat[] = { "function", name };
+ if (!PARTIAL_MATCH(expr, pat)) {
+ ir_read_error(expr, "Expected (function <name> (signature ...) ...)");
+ return NULL;
+ }
+
+ ir_function *f = state->symbols->get_function(name->value());
+ if (f == NULL) {
+ f = new(mem_ctx) ir_function(name->value());
+ added = state->symbols->add_function(f);
+ assert(added);
+ }
+
+ exec_list_iterator it = ((s_list *) expr)->subexpressions.iterator();
+ it.next(); // skip "function" tag
+ it.next(); // skip function name
+ for (/* nothing */; it.has_next(); it.next()) {
+ s_expression *s_sig = (s_expression *) it.get();
+ read_function_sig(f, s_sig, skip_body);
+ }
+ return added ? f : NULL;
+}
+
+void
+ir_reader::read_function_sig(ir_function *f, s_expression *expr, bool skip_body)
+{
+ s_expression *type_expr;
+ s_list *paramlist;
+ s_list *body_list;
+
+ s_pattern pat[] = { "signature", type_expr, paramlist, body_list };
+ if (!MATCH(expr, pat)) {
+ ir_read_error(expr, "Expected (signature <type> (parameters ...) "
+ "(<instruction> ...))");
+ return;
+ }
+
+ const glsl_type *return_type = read_type(type_expr);
+ if (return_type == NULL)
+ return;
+
+ s_symbol *paramtag = SX_AS_SYMBOL(paramlist->subexpressions.get_head());
+ if (paramtag == NULL || strcmp(paramtag->value(), "parameters") != 0) {
+ ir_read_error(paramlist, "Expected (parameters ...)");
+ return;
+ }
+
+ // Read the parameters list into a temporary place.
+ exec_list hir_parameters;
+ state->symbols->push_scope();
+
+ exec_list_iterator it = paramlist->subexpressions.iterator();
+ for (it.next() /* skip "parameters" */; it.has_next(); it.next()) {
+ ir_variable *var = read_declaration((s_expression *) it.get());
+ if (var == NULL)
+ return;
+
+ hir_parameters.push_tail(var);
+ }
+
+ ir_function_signature *sig = f->exact_matching_signature(&hir_parameters);
+ if (sig == NULL && skip_body) {
+ /* If scanning for prototypes, generate a new signature. */
+ sig = new(mem_ctx) ir_function_signature(return_type);
+ sig->is_builtin = true;
+ f->add_signature(sig);
+ } else if (sig != NULL) {
+ const char *badvar = sig->qualifiers_match(&hir_parameters);
+ if (badvar != NULL) {
+ ir_read_error(expr, "function `%s' parameter `%s' qualifiers "
+ "don't match prototype", f->name, badvar);
+ return;
+ }
+
+ if (sig->return_type != return_type) {
+ ir_read_error(expr, "function `%s' return type doesn't "
+ "match prototype", f->name);
+ return;
+ }
+ } else {
+ /* No prototype for this body exists - skip it. */
+ state->symbols->pop_scope();
+ return;
+ }
+ assert(sig != NULL);
+
+ sig->replace_parameters(&hir_parameters);
+
+ if (!skip_body && !body_list->subexpressions.is_empty()) {
+ if (sig->is_defined) {
+ ir_read_error(expr, "function %s redefined", f->name);
+ return;
+ }
+ state->current_function = sig;
+ read_instructions(&sig->body, body_list, NULL);
+ state->current_function = NULL;
+ sig->is_defined = true;
+ }
+
+ state->symbols->pop_scope();
+}
+
+void
+ir_reader::read_instructions(exec_list *instructions, s_expression *expr,
+ ir_loop *loop_ctx)
+{
+ // Read in a list of instructions
+ s_list *list = SX_AS_LIST(expr);
+ if (list == NULL) {
+ ir_read_error(expr, "Expected (<instruction> ...); found an atom.");
+ return;
+ }
+
+ foreach_iter(exec_list_iterator, it, list->subexpressions) {
+ s_expression *sub = (s_expression*) it.get();
+ ir_instruction *ir = read_instruction(sub, loop_ctx);
+ if (ir != NULL) {
+ /* Global variable declarations should be moved to the top, before
+ * any functions that might use them. Functions are added to the
+ * instruction stream when scanning for prototypes, so without this
+ * hack, they always appear before variable declarations.
+ */
+ if (state->current_function == NULL && ir->as_variable() != NULL)
+ instructions->push_head(ir);
+ else
+ instructions->push_tail(ir);
+ }
+ }
+}
+
+
+ir_instruction *
+ir_reader::read_instruction(s_expression *expr, ir_loop *loop_ctx)
+{
+ s_symbol *symbol = SX_AS_SYMBOL(expr);
+ if (symbol != NULL) {
+ if (strcmp(symbol->value(), "break") == 0 && loop_ctx != NULL)
+ return new(mem_ctx) ir_loop_jump(ir_loop_jump::jump_break);
+ if (strcmp(symbol->value(), "continue") == 0 && loop_ctx != NULL)
+ return new(mem_ctx) ir_loop_jump(ir_loop_jump::jump_continue);
+ }
+
+ s_list *list = SX_AS_LIST(expr);
+ if (list == NULL || list->subexpressions.is_empty()) {
+ ir_read_error(expr, "Invalid instruction.\n");
+ return NULL;
+ }
+
+ s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
+ if (tag == NULL) {
+ ir_read_error(expr, "expected instruction tag");
+ return NULL;
+ }
+
+ ir_instruction *inst = NULL;
+ if (strcmp(tag->value(), "declare") == 0) {
+ inst = read_declaration(list);
+ } else if (strcmp(tag->value(), "assign") == 0) {
+ inst = read_assignment(list);
+ } else if (strcmp(tag->value(), "if") == 0) {
+ inst = read_if(list, loop_ctx);
+ } else if (strcmp(tag->value(), "loop") == 0) {
+ inst = read_loop(list);
+ } else if (strcmp(tag->value(), "return") == 0) {
+ inst = read_return(list);
+ } else if (strcmp(tag->value(), "function") == 0) {
+ inst = read_function(list, false);
+ } else {
+ inst = read_rvalue(list);
+ if (inst == NULL)
+ ir_read_error(NULL, "when reading instruction");
+ }
+ return inst;
+}
+
+ir_variable *
+ir_reader::read_declaration(s_expression *expr)
+{
+ s_list *s_quals;
+ s_expression *s_type;
+ s_symbol *s_name;
+
+ s_pattern pat[] = { "declare", s_quals, s_type, s_name };
+ if (!MATCH(expr, pat)) {
+ ir_read_error(expr, "expected (declare (<qualifiers>) <type> <name>)");
+ return NULL;
+ }
+
+ const glsl_type *type = read_type(s_type);
+ if (type == NULL)
+ return NULL;
+
+ ir_variable *var = new(mem_ctx) ir_variable(type, s_name->value(),
+ ir_var_auto);
+
+ foreach_iter(exec_list_iterator, it, s_quals->subexpressions) {
+ s_symbol *qualifier = SX_AS_SYMBOL(it.get());
+ if (qualifier == NULL) {
+ ir_read_error(expr, "qualifier list must contain only symbols");
+ return NULL;
+ }
+
+ // FINISHME: Check for duplicate/conflicting qualifiers.
+ if (strcmp(qualifier->value(), "centroid") == 0) {
+ var->centroid = 1;
+ } else if (strcmp(qualifier->value(), "invariant") == 0) {
+ var->invariant = 1;
+ } else if (strcmp(qualifier->value(), "uniform") == 0) {
+ var->mode = ir_var_uniform;
+ } else if (strcmp(qualifier->value(), "auto") == 0) {
+ var->mode = ir_var_auto;
+ } else if (strcmp(qualifier->value(), "in") == 0) {
+ var->mode = ir_var_in;
+ } else if (strcmp(qualifier->value(), "const_in") == 0) {
+ var->mode = ir_var_const_in;
+ } else if (strcmp(qualifier->value(), "out") == 0) {
+ var->mode = ir_var_out;
+ } else if (strcmp(qualifier->value(), "inout") == 0) {
+ var->mode = ir_var_inout;
+ } else if (strcmp(qualifier->value(), "smooth") == 0) {
+ var->interpolation = ir_var_smooth;
+ } else if (strcmp(qualifier->value(), "flat") == 0) {
+ var->interpolation = ir_var_flat;
+ } else if (strcmp(qualifier->value(), "noperspective") == 0) {
+ var->interpolation = ir_var_noperspective;
+ } else {
+ ir_read_error(expr, "unknown qualifier: %s", qualifier->value());
+ return NULL;
+ }
+ }
+
+ // Add the variable to the symbol table
+ state->symbols->add_variable(var);
+
+ return var;
+}
+
+
+ir_if *
+ir_reader::read_if(s_expression *expr, ir_loop *loop_ctx)
+{
+ s_expression *s_cond;
+ s_expression *s_then;
+ s_expression *s_else;
+
+ s_pattern pat[] = { "if", s_cond, s_then, s_else };
+ if (!MATCH(expr, pat)) {
+ ir_read_error(expr, "expected (if <condition> (<then>...) (<else>...))");
+ return NULL;
+ }
+
+ ir_rvalue *condition = read_rvalue(s_cond);
+ if (condition == NULL) {
+ ir_read_error(NULL, "when reading condition of (if ...)");
+ return NULL;
+ }
+
+ ir_if *iff = new(mem_ctx) ir_if(condition);
+
+ read_instructions(&iff->then_instructions, s_then, loop_ctx);
+ read_instructions(&iff->else_instructions, s_else, loop_ctx);
+ if (state->error) {
+ delete iff;
+ iff = NULL;
+ }
+ return iff;
+}
+
+
+ir_loop *
+ir_reader::read_loop(s_expression *expr)
+{
+ s_expression *s_counter, *s_from, *s_to, *s_inc, *s_body;
+
+ s_pattern pat[] = { "loop", s_counter, s_from, s_to, s_inc, s_body };
+ if (!MATCH(expr, pat)) {
+ ir_read_error(expr, "expected (loop <counter> <from> <to> "
+ "<increment> <body>)");
+ return NULL;
+ }
+
+ // FINISHME: actually read the count/from/to fields.
+
+ ir_loop *loop = new(mem_ctx) ir_loop;
+ read_instructions(&loop->body_instructions, s_body, loop);
+ if (state->error) {
+ delete loop;
+ loop = NULL;
+ }
+ return loop;
+}
+
+
+ir_return *
+ir_reader::read_return(s_expression *expr)
+{
+ s_expression *s_retval;
+
+ s_pattern return_value_pat[] = { "return", s_retval};
+ s_pattern return_void_pat[] = { "return" };
+ if (MATCH(expr, return_value_pat)) {
+ ir_rvalue *retval = read_rvalue(s_retval);
+ if (retval == NULL) {
+ ir_read_error(NULL, "when reading return value");
+ return NULL;
+ }
+ return new(mem_ctx) ir_return(retval);
+ } else if (MATCH(expr, return_void_pat)) {
+ return new(mem_ctx) ir_return;
+ } else {
+ ir_read_error(expr, "expected (return <rvalue>) or (return)");
+ return NULL;
+ }
+}
+
+
+ir_rvalue *
+ir_reader::read_rvalue(s_expression *expr)
+{
+ s_list *list = SX_AS_LIST(expr);
+ if (list == NULL || list->subexpressions.is_empty())
+ return NULL;
+
+ s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
+ if (tag == NULL) {
+ ir_read_error(expr, "expected rvalue tag");
+ return NULL;
+ }
+
+ ir_rvalue *rvalue = read_dereference(list);
+ if (rvalue != NULL || state->error)
+ return rvalue;
+ else if (strcmp(tag->value(), "swiz") == 0) {
+ rvalue = read_swizzle(list);
+ } else if (strcmp(tag->value(), "expression") == 0) {
+ rvalue = read_expression(list);
+ } else if (strcmp(tag->value(), "call") == 0) {
+ rvalue = read_call(list);
+ } else if (strcmp(tag->value(), "constant") == 0) {
+ rvalue = read_constant(list);
+ } else {
+ rvalue = read_texture(list);
+ if (rvalue == NULL && !state->error)
+ ir_read_error(expr, "unrecognized rvalue tag: %s", tag->value());
+ }
+
+ return rvalue;
+}
+
+ir_assignment *
+ir_reader::read_assignment(s_expression *expr)
+{
+ s_expression *cond_expr = NULL;
+ s_expression *lhs_expr, *rhs_expr;
+ s_list *mask_list;
+
+ s_pattern pat4[] = { "assign", mask_list, lhs_expr, rhs_expr };
+ s_pattern pat5[] = { "assign", cond_expr, mask_list, lhs_expr, rhs_expr };
+ if (!MATCH(expr, pat4) && !MATCH(expr, pat5)) {
+ ir_read_error(expr, "expected (assign [<condition>] (<write mask>) "
+ "<lhs> <rhs>)");
+ return NULL;
+ }
+
+ ir_rvalue *condition = NULL;
+ if (cond_expr != NULL) {
+ condition = read_rvalue(cond_expr);
+ if (condition == NULL) {
+ ir_read_error(NULL, "when reading condition of assignment");
+ return NULL;
+ }
+ }
+
+ unsigned mask = 0;
+
+ s_symbol *mask_symbol;
+ s_pattern mask_pat[] = { mask_symbol };
+ if (MATCH(mask_list, mask_pat)) {
+ const char *mask_str = mask_symbol->value();
+ unsigned mask_length = strlen(mask_str);
+ if (mask_length > 4) {
+ ir_read_error(expr, "invalid write mask: %s", mask_str);
+ return NULL;
+ }
+
+ const unsigned idx_map[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
+
+ for (unsigned i = 0; i < mask_length; i++) {
+ if (mask_str[i] < 'w' || mask_str[i] > 'z') {
+ ir_read_error(expr, "write mask contains invalid character: %c",
+ mask_str[i]);
+ return NULL;
+ }
+ mask |= 1 << idx_map[mask_str[i] - 'w'];
+ }
+ } else if (!mask_list->subexpressions.is_empty()) {
+ ir_read_error(mask_list, "expected () or (<write mask>)");
+ return NULL;
+ }
+
+ ir_dereference *lhs = read_dereference(lhs_expr);
+ if (lhs == NULL) {
+ ir_read_error(NULL, "when reading left-hand side of assignment");
+ return NULL;
+ }
+
+ ir_rvalue *rhs = read_rvalue(rhs_expr);
+ if (rhs == NULL) {
+ ir_read_error(NULL, "when reading right-hand side of assignment");
+ return NULL;
+ }
+
+ if (mask == 0 && (lhs->type->is_vector() || lhs->type->is_scalar())) {
+ ir_read_error(expr, "non-zero write mask required.");
+ return NULL;
+ }
+
+ return new(mem_ctx) ir_assignment(lhs, rhs, condition, mask);
+}
+
+ir_call *
+ir_reader::read_call(s_expression *expr)
+{
+ s_symbol *name;
+ s_list *params;
+
+ s_pattern pat[] = { "call", name, params };
+ if (!MATCH(expr, pat)) {
+ ir_read_error(expr, "expected (call <name> (<param> ...))");
+ return NULL;
+ }
+
+ exec_list parameters;
+
+ foreach_iter(exec_list_iterator, it, params->subexpressions) {
+ s_expression *expr = (s_expression*) it.get();
+ ir_rvalue *param = read_rvalue(expr);
+ if (param == NULL) {
+ ir_read_error(expr, "when reading parameter to function call");
+ return NULL;
+ }
+ parameters.push_tail(param);
+ }
+
+ ir_function *f = state->symbols->get_function(name->value());
+ if (f == NULL) {
+ ir_read_error(expr, "found call to undefined function %s",
+ name->value());
+ return NULL;
+ }
+
+ ir_function_signature *callee = f->matching_signature(&parameters);
+ if (callee == NULL) {
+ ir_read_error(expr, "couldn't find matching signature for function "
+ "%s", name->value());
+ return NULL;
+ }
+
+ return new(mem_ctx) ir_call(callee, &parameters);
+}
+
+ir_expression *
+ir_reader::read_expression(s_expression *expr)
+{
+ s_expression *s_type;
+ s_symbol *s_op;
+ s_expression *s_arg1;
+
+ s_pattern pat[] = { "expression", s_type, s_op, s_arg1 };
+ if (!PARTIAL_MATCH(expr, pat)) {
+ ir_read_error(expr, "expected (expression <type> <operator> "
+ "<operand> [<operand>])");
+ return NULL;
+ }
+ s_expression *s_arg2 = (s_expression *) s_arg1->next; // may be tail sentinel
+
+ const glsl_type *type = read_type(s_type);
+ if (type == NULL)
+ return NULL;
+
+ /* Read the operator */
+ ir_expression_operation op = ir_expression::get_operator(s_op->value());
+ if (op == (ir_expression_operation) -1) {
+ ir_read_error(expr, "invalid operator: %s", s_op->value());
+ return NULL;
+ }
+
+ unsigned num_operands = ir_expression::get_num_operands(op);
+ if (num_operands == 1 && !s_arg1->next->is_tail_sentinel()) {
+ ir_read_error(expr, "expected (expression <type> %s <operand>)",
+ s_op->value());
+ return NULL;
+ }
+
+ ir_rvalue *arg1 = read_rvalue(s_arg1);
+ ir_rvalue *arg2 = NULL;
+ if (arg1 == NULL) {
+ ir_read_error(NULL, "when reading first operand of %s", s_op->value());
+ return NULL;
+ }
+
+ if (num_operands == 2) {
+ if (s_arg2->is_tail_sentinel() || !s_arg2->next->is_tail_sentinel()) {
+ ir_read_error(expr, "expected (expression <type> %s <operand> "
+ "<operand>)", s_op->value());
+ return NULL;
+ }
+ arg2 = read_rvalue(s_arg2);
+ if (arg2 == NULL) {
+ ir_read_error(NULL, "when reading second operand of %s",
+ s_op->value());
+ return NULL;
+ }
+ }
+
+ return new(mem_ctx) ir_expression(op, type, arg1, arg2);
+}
+
+ir_swizzle *
+ir_reader::read_swizzle(s_expression *expr)
+{
+ s_symbol *swiz;
+ s_expression *sub;
+
+ s_pattern pat[] = { "swiz", swiz, sub };
+ if (!MATCH(expr, pat)) {
+ ir_read_error(expr, "expected (swiz <swizzle> <rvalue>)");
+ return NULL;
+ }
+
+ if (strlen(swiz->value()) > 4) {
+ ir_read_error(expr, "expected a valid swizzle; found %s", swiz->value());
+ return NULL;
+ }
+
+ ir_rvalue *rvalue = read_rvalue(sub);
+ if (rvalue == NULL)
+ return NULL;
+
+ ir_swizzle *ir = ir_swizzle::create(rvalue, swiz->value(),
+ rvalue->type->vector_elements);
+ if (ir == NULL)
+ ir_read_error(expr, "invalid swizzle");
+
+ return ir;
+}
+
+ir_constant *
+ir_reader::read_constant(s_expression *expr)
+{
+ s_expression *type_expr;
+ s_list *values;
+
+ s_pattern pat[] = { "constant", type_expr, values };
+ if (!MATCH(expr, pat)) {
+ ir_read_error(expr, "expected (constant <type> (...))");
+ return NULL;
+ }
+
+ const glsl_type *type = read_type(type_expr);
+ if (type == NULL)
+ return NULL;
+
+ if (values == NULL) {
+ ir_read_error(expr, "expected (constant <type> (...))");
+ return NULL;
+ }
+
+ if (type->is_array()) {
+ unsigned elements_supplied = 0;
+ exec_list elements;
+ foreach_iter(exec_list_iterator, it, values->subexpressions) {
+ s_expression *elt = (s_expression *) it.get();
+ ir_constant *ir_elt = read_constant(elt);
+ if (ir_elt == NULL)
+ return NULL;
+ elements.push_tail(ir_elt);
+ elements_supplied++;
+ }
+
+ if (elements_supplied != type->length) {
+ ir_read_error(values, "expected exactly %u array elements, "
+ "given %u", type->length, elements_supplied);
+ return NULL;
+ }
+ return new(mem_ctx) ir_constant(type, &elements);
+ }
+
+ const glsl_type *const base_type = type->get_base_type();
+
+ ir_constant_data data = { { 0 } };
+
+ // Read in list of values (at most 16).
+ int k = 0;
+ foreach_iter(exec_list_iterator, it, values->subexpressions) {
+ if (k >= 16) {
+ ir_read_error(values, "expected at most 16 numbers");
+ return NULL;
+ }
+
+ s_expression *expr = (s_expression*) it.get();
+
+ if (base_type->base_type == GLSL_TYPE_FLOAT) {
+ s_number *value = SX_AS_NUMBER(expr);
+ if (value == NULL) {
+ ir_read_error(values, "expected numbers");
+ return NULL;
+ }
+ data.f[k] = value->fvalue();
+ } else {
+ s_int *value = SX_AS_INT(expr);
+ if (value == NULL) {
+ ir_read_error(values, "expected integers");
+ return NULL;
+ }
+
+ switch (base_type->base_type) {
+ case GLSL_TYPE_UINT: {
+ data.u[k] = value->value();
+ break;
+ }
+ case GLSL_TYPE_INT: {
+ data.i[k] = value->value();
+ break;
+ }
+ case GLSL_TYPE_BOOL: {
+ data.b[k] = value->value();
+ break;
+ }
+ default:
+ ir_read_error(values, "unsupported constant type");
+ return NULL;
+ }
+ }
+ ++k;
+ }
+
+ return new(mem_ctx) ir_constant(type, &data);
+}
+
+ir_dereference *
+ir_reader::read_dereference(s_expression *expr)
+{
+ s_symbol *s_var;
+ s_expression *s_subject;
+ s_expression *s_index;
+ s_symbol *s_field;
+
+ s_pattern var_pat[] = { "var_ref", s_var };
+ s_pattern array_pat[] = { "array_ref", s_subject, s_index };
+ s_pattern record_pat[] = { "record_ref", s_subject, s_field };
+
+ if (MATCH(expr, var_pat)) {
+ ir_variable *var = state->symbols->get_variable(s_var->value());
+ if (var == NULL) {
+ ir_read_error(expr, "undeclared variable: %s", s_var->value());
+ return NULL;
+ }
+ return new(mem_ctx) ir_dereference_variable(var);
+ } else if (MATCH(expr, array_pat)) {
+ ir_rvalue *subject = read_rvalue(s_subject);
+ if (subject == NULL) {
+ ir_read_error(NULL, "when reading the subject of an array_ref");
+ return NULL;
+ }
+
+ ir_rvalue *idx = read_rvalue(s_index);
+ if (subject == NULL) {
+ ir_read_error(NULL, "when reading the index of an array_ref");
+ return NULL;
+ }
+ return new(mem_ctx) ir_dereference_array(subject, idx);
+ } else if (MATCH(expr, record_pat)) {
+ ir_rvalue *subject = read_rvalue(s_subject);
+ if (subject == NULL) {
+ ir_read_error(NULL, "when reading the subject of a record_ref");
+ return NULL;
+ }
+ return new(mem_ctx) ir_dereference_record(subject, s_field->value());
+ }
+ return NULL;
+}
+
+ir_texture *
+ir_reader::read_texture(s_expression *expr)
+{
+ s_symbol *tag = NULL;
+ s_expression *s_type = NULL;
+ s_expression *s_sampler = NULL;
+ s_expression *s_coord = NULL;
+ s_expression *s_offset = NULL;
+ s_expression *s_proj = NULL;
+ s_list *s_shadow = NULL;
+ s_expression *s_lod = NULL;
+
+ ir_texture_opcode op = ir_tex; /* silence warning */
+
+ s_pattern tex_pattern[] =
+ { "tex", s_type, s_sampler, s_coord, s_offset, s_proj, s_shadow };
+ s_pattern txf_pattern[] =
+ { "txf", s_type, s_sampler, s_coord, s_offset, s_lod };
+ s_pattern other_pattern[] =
+ { tag, s_type, s_sampler, s_coord, s_offset, s_proj, s_shadow, s_lod };
+
+ if (MATCH(expr, tex_pattern)) {
+ op = ir_tex;
+ } else if (MATCH(expr, txf_pattern)) {
+ op = ir_txf;
+ } else if (MATCH(expr, other_pattern)) {
+ op = ir_texture::get_opcode(tag->value());
+ if (op == -1)
+ return NULL;
+ } else {
+ ir_read_error(NULL, "unexpected texture pattern");
+ return NULL;
+ }
+
+ ir_texture *tex = new(mem_ctx) ir_texture(op);
+
+ // Read return type
+ const glsl_type *type = read_type(s_type);
+ if (type == NULL) {
+ ir_read_error(NULL, "when reading type in (%s ...)",
+ tex->opcode_string());
+ return NULL;
+ }
+
+ // Read sampler (must be a deref)
+ ir_dereference *sampler = read_dereference(s_sampler);
+ if (sampler == NULL) {
+ ir_read_error(NULL, "when reading sampler in (%s ...)",
+ tex->opcode_string());
+ return NULL;
+ }
+ tex->set_sampler(sampler, type);
+
+ // Read coordinate (any rvalue)
+ tex->coordinate = read_rvalue(s_coord);
+ if (tex->coordinate == NULL) {
+ ir_read_error(NULL, "when reading coordinate in (%s ...)",
+ tex->opcode_string());
+ return NULL;
+ }
+
+ // Read texel offset - either 0 or an rvalue.
+ s_int *si_offset = SX_AS_INT(s_offset);
+ if (si_offset == NULL || si_offset->value() != 0) {
+ tex->offset = read_rvalue(s_offset);
+ if (tex->offset == NULL) {
+ ir_read_error(s_offset, "expected 0 or an expression");
+ return NULL;
+ }
+ }
+
+ if (op != ir_txf) {
+ s_int *proj_as_int = SX_AS_INT(s_proj);
+ if (proj_as_int && proj_as_int->value() == 1) {
+ tex->projector = NULL;
+ } else {
+ tex->projector = read_rvalue(s_proj);
+ if (tex->projector == NULL) {
+ ir_read_error(NULL, "when reading projective divide in (%s ..)",
+ tex->opcode_string());
+ return NULL;
+ }
+ }
+
+ if (s_shadow->subexpressions.is_empty()) {
+ tex->shadow_comparitor = NULL;
+ } else {
+ tex->shadow_comparitor = read_rvalue(s_shadow);
+ if (tex->shadow_comparitor == NULL) {
+ ir_read_error(NULL, "when reading shadow comparitor in (%s ..)",
+ tex->opcode_string());
+ return NULL;
+ }
+ }
+ }
+
+ switch (op) {
+ case ir_txb:
+ tex->lod_info.bias = read_rvalue(s_lod);
+ if (tex->lod_info.bias == NULL) {
+ ir_read_error(NULL, "when reading LOD bias in (txb ...)");
+ return NULL;
+ }
+ break;
+ case ir_txl:
+ case ir_txf:
+ tex->lod_info.lod = read_rvalue(s_lod);
+ if (tex->lod_info.lod == NULL) {
+ ir_read_error(NULL, "when reading LOD in (%s ...)",
+ tex->opcode_string());
+ return NULL;
+ }
+ break;
+ case ir_txd: {
+ s_expression *s_dx, *s_dy;
+ s_pattern dxdy_pat[] = { s_dx, s_dy };
+ if (!MATCH(s_lod, dxdy_pat)) {
+ ir_read_error(s_lod, "expected (dPdx dPdy) in (txd ...)");
+ return NULL;
+ }
+ tex->lod_info.grad.dPdx = read_rvalue(s_dx);
+ if (tex->lod_info.grad.dPdx == NULL) {
+ ir_read_error(NULL, "when reading dPdx in (txd ...)");
+ return NULL;
+ }
+ tex->lod_info.grad.dPdy = read_rvalue(s_dy);
+ if (tex->lod_info.grad.dPdy == NULL) {
+ ir_read_error(NULL, "when reading dPdy in (txd ...)");
+ return NULL;
+ }
+ break;
+ }
+ default:
+ // tex doesn't have any extra parameters.
+ break;
+ };
+ return tex;
+}
diff --git a/mesalib/src/glsl/linker.cpp b/mesalib/src/glsl/linker.cpp
index 1c0785ad7..e0d7efa23 100644
--- a/mesalib/src/glsl/linker.cpp
+++ b/mesalib/src/glsl/linker.cpp
@@ -1248,7 +1248,7 @@ assign_attribute_or_color_locations(gl_shader_program *prog,
*/
const int generic_base = (target_index == MESA_SHADER_VERTEX)
- ? VERT_ATTRIB_GENERIC0 : FRAG_RESULT_DATA0;
+ ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
const enum ir_variable_mode direction =
(target_index == MESA_SHADER_VERTEX) ? ir_var_in : ir_var_out;
diff --git a/mesalib/src/glsl/lower_jumps.cpp b/mesalib/src/glsl/lower_jumps.cpp
index 17be39f5b..61874990a 100644
--- a/mesalib/src/glsl/lower_jumps.cpp
+++ b/mesalib/src/glsl/lower_jumps.cpp
@@ -1,570 +1,1010 @@
-/*
- * Copyright © 2010 Luca Barbieri
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice (including the next
- * paragraph) shall be included in all copies or substantial portions of the
- * Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- */
-
-/**
- * \file lower_jumps.cpp
- *
- * This pass lowers jumps (break, continue, and return) to if/else structures.
- *
- * It can be asked to:
- * 1. Pull jumps out of ifs where possible
- * 2. Remove all "continue"s, replacing them with an "execute flag"
- * 3. Replace all "break" with a single conditional one at the end of the loop
- * 4. Replace all "return"s with a single return at the end of the function,
- * for the main function and/or other functions
- *
- * Applying this pass gives several benefits:
- * 1. All functions can be inlined.
- * 2. nv40 and other pre-DX10 chips without "continue" can be supported
- * 3. nv30 and other pre-DX10 chips with no control flow at all are better
- * supported
- *
- * Continues are lowered by adding a per-loop "execute flag", initialized to
- * true, that when cleared inhibits all execution until the end of the loop.
- *
- * Breaks are lowered to continues, plus setting a "break flag" that is checked
- * at the end of the loop, and trigger the unique "break".
- *
- * Returns are lowered to breaks/continues, plus adding a "return flag" that
- * causes loops to break again out of their enclosing loops until all the
- * loops are exited: then the "execute flag" logic will ignore everything
- * until the end of the function.
- *
- * Note that "continue" and "return" can also be implemented by adding
- * a dummy loop and using break.
- * However, this is bad for hardware with limited nesting depth, and
- * prevents further optimization, and thus is not currently performed.
- */
-
-#include "glsl_types.h"
-#include <string.h>
-#include "ir.h"
-
-enum jump_strength
-{
- strength_none,
- strength_always_clears_execute_flag,
- strength_continue,
- strength_break,
- strength_return
-};
-
-struct block_record
-{
- /* minimum jump strength (of lowered IR, not pre-lowering IR)
- *
- * If the block ends with a jump, must be the strength of the jump.
- * Otherwise, the jump would be dead and have been deleted before)
- *
- * If the block doesn't end with a jump, it can be different than strength_none if all paths before it lead to some jump
- * (e.g. an if with a return in one branch, and a break in the other, while not lowering them)
- * Note that identical jumps are usually unified though.
- */
- jump_strength min_strength;
-
- /* can anything clear the execute flag? */
- bool may_clear_execute_flag;
-
- block_record()
- {
- this->min_strength = strength_none;
- this->may_clear_execute_flag = false;
- }
-};
-
-struct loop_record
-{
- ir_function_signature* signature;
- ir_loop* loop;
-
- /* used to avoid lowering the break used to represent lowered breaks */
- unsigned nesting_depth;
- bool in_if_at_the_end_of_the_loop;
-
- bool may_set_return_flag;
-
- ir_variable* break_flag;
- ir_variable* execute_flag; /* cleared to emulate continue */
-
- loop_record(ir_function_signature* p_signature = 0, ir_loop* p_loop = 0)
- {
- this->signature = p_signature;
- this->loop = p_loop;
- this->nesting_depth = 0;
- this->in_if_at_the_end_of_the_loop = false;
- this->may_set_return_flag = false;
- this->break_flag = 0;
- this->execute_flag = 0;
- }
-
- ir_variable* get_execute_flag()
- {
- /* also supported for the "function loop" */
- if(!this->execute_flag) {
- exec_list& list = this->loop ? this->loop->body_instructions : signature->body;
- this->execute_flag = new(this->signature) ir_variable(glsl_type::bool_type, "execute_flag", ir_var_temporary);
- list.push_head(new(this->signature) ir_assignment(new(this->signature) ir_dereference_variable(execute_flag), new(this->signature) ir_constant(true), 0));
- list.push_head(this->execute_flag);
- }
- return this->execute_flag;
- }
-
- ir_variable* get_break_flag()
- {
- assert(this->loop);
- if(!this->break_flag) {
- this->break_flag = new(this->signature) ir_variable(glsl_type::bool_type, "break_flag", ir_var_temporary);
- this->loop->insert_before(this->break_flag);
- this->loop->insert_before(new(this->signature) ir_assignment(new(this->signature) ir_dereference_variable(break_flag), new(this->signature) ir_constant(false), 0));
- }
- return this->break_flag;
- }
-};
-
-struct function_record
-{
- ir_function_signature* signature;
- ir_variable* return_flag; /* used to break out of all loops and then jump to the return instruction */
- ir_variable* return_value;
- bool is_main;
- unsigned nesting_depth;
-
- function_record(ir_function_signature* p_signature = 0)
- {
- this->signature = p_signature;
- this->return_flag = 0;
- this->return_value = 0;
- this->nesting_depth = 0;
- this->is_main = this->signature && (strcmp(this->signature->function_name(), "main") == 0);
- }
-
- ir_variable* get_return_flag()
- {
- if(!this->return_flag) {
- this->return_flag = new(this->signature) ir_variable(glsl_type::bool_type, "return_flag", ir_var_temporary);
- this->signature->body.push_head(new(this->signature) ir_assignment(new(this->signature) ir_dereference_variable(return_flag), new(this->signature) ir_constant(false), 0));
- this->signature->body.push_head(this->return_flag);
- }
- return this->return_flag;
- }
-
- ir_variable* get_return_value()
- {
- if(!this->return_value) {
- assert(!this->signature->return_type->is_void());
- return_value = new(this->signature) ir_variable(this->signature->return_type, "return_value", ir_var_temporary);
- this->signature->body.push_head(this->return_value);
- }
- return this->return_value;
- }
-};
-
-struct ir_lower_jumps_visitor : public ir_control_flow_visitor {
- bool progress;
-
- struct function_record function;
- struct loop_record loop;
- struct block_record block;
-
- bool pull_out_jumps;
- bool lower_continue;
- bool lower_break;
- bool lower_sub_return;
- bool lower_main_return;
-
- ir_lower_jumps_visitor()
- {
- this->progress = false;
- }
-
- void truncate_after_instruction(exec_node *ir)
- {
- if (!ir)
- return;
-
- while (!ir->get_next()->is_tail_sentinel()) {
- ((ir_instruction *)ir->get_next())->remove();
- this->progress = true;
- }
- }
-
- void move_outer_block_inside(ir_instruction *ir, exec_list *inner_block)
- {
- while (!ir->get_next()->is_tail_sentinel()) {
- ir_instruction *move_ir = (ir_instruction *)ir->get_next();
-
- move_ir->remove();
- inner_block->push_tail(move_ir);
- }
- }
-
- virtual void visit(class ir_loop_jump * ir)
- {
- truncate_after_instruction(ir);
- this->block.min_strength = ir->is_break() ? strength_break : strength_continue;
- }
-
- virtual void visit(class ir_return * ir)
- {
- truncate_after_instruction(ir);
- this->block.min_strength = strength_return;
- }
-
- virtual void visit(class ir_discard * ir)
- {
- }
-
- enum jump_strength get_jump_strength(ir_instruction* ir)
- {
- if(!ir)
- return strength_none;
- else if(ir->ir_type == ir_type_loop_jump) {
- if(((ir_loop_jump*)ir)->is_break())
- return strength_break;
- else
- return strength_continue;
- } else if(ir->ir_type == ir_type_return)
- return strength_return;
- else
- return strength_none;
- }
-
- bool should_lower_jump(ir_jump* ir)
- {
- unsigned strength = get_jump_strength(ir);
- bool lower;
- switch(strength)
- {
- case strength_none:
- lower = false; /* don't change this, code relies on it */
- break;
- case strength_continue:
- lower = lower_continue;
- break;
- case strength_break:
- assert(this->loop.loop);
- /* never lower "canonical break" */
- if(ir->get_next()->is_tail_sentinel() && (this->loop.nesting_depth == 0
- || (this->loop.nesting_depth == 1 && this->loop.in_if_at_the_end_of_the_loop)))
- lower = false;
- else
- lower = lower_break;
- break;
- case strength_return:
- /* never lower return at the end of a this->function */
- if(this->function.nesting_depth == 0 && ir->get_next()->is_tail_sentinel())
- lower = false;
- else if (this->function.is_main)
- lower = lower_main_return;
- else
- lower = lower_sub_return;
- break;
- }
- return lower;
- }
-
- block_record visit_block(exec_list* list)
- {
- block_record saved_block = this->block;
- this->block = block_record();
- visit_exec_list(list, this);
- block_record ret = this->block;
- this->block = saved_block;
- return ret;
- }
-
- virtual void visit(ir_if *ir)
- {
- if(this->loop.nesting_depth == 0 && ir->get_next()->is_tail_sentinel())
- this->loop.in_if_at_the_end_of_the_loop = true;
-
- ++this->function.nesting_depth;
- ++this->loop.nesting_depth;
-
- block_record block_records[2];
- ir_jump* jumps[2];
-
- block_records[0] = visit_block(&ir->then_instructions);
- block_records[1] = visit_block(&ir->else_instructions);
-
-retry: /* we get here if we put code after the if inside a branch */
- for(unsigned i = 0; i < 2; ++i) {
- exec_list& list = i ? ir->else_instructions : ir->then_instructions;
- jumps[i] = 0;
- if(!list.is_empty() && get_jump_strength((ir_instruction*)list.get_tail()))
- jumps[i] = (ir_jump*)list.get_tail();
- }
-
- for(;;) {
- jump_strength jump_strengths[2];
-
- for(unsigned i = 0; i < 2; ++i) {
- if(jumps[i]) {
- jump_strengths[i] = block_records[i].min_strength;
- assert(jump_strengths[i] == get_jump_strength(jumps[i]));
- } else
- jump_strengths[i] = strength_none;
- }
-
- /* move both jumps out if possible */
- if(pull_out_jumps && jump_strengths[0] == jump_strengths[1]) {
- bool unify = true;
- if(jump_strengths[0] == strength_continue)
- ir->insert_after(new(ir) ir_loop_jump(ir_loop_jump::jump_continue));
- else if(jump_strengths[0] == strength_break)
- ir->insert_after(new(ir) ir_loop_jump(ir_loop_jump::jump_break));
- /* FINISHME: unify returns with identical expressions */
- else if(jump_strengths[0] == strength_return && this->function.signature->return_type->is_void())
- ir->insert_after(new(ir) ir_return(NULL));
- else
- unify = false;
-
- if(unify) {
- jumps[0]->remove();
- jumps[1]->remove();
- this->progress = true;
-
- jumps[0] = 0;
- jumps[1] = 0;
- block_records[0].min_strength = strength_none;
- block_records[1].min_strength = strength_none;
- break;
- }
- }
-
- /* lower a jump: if both need to lowered, start with the strongest one, so that
- * we might later unify the lowered version with the other one
- */
- bool should_lower[2];
- for(unsigned i = 0; i < 2; ++i)
- should_lower[i] = should_lower_jump(jumps[i]);
-
- int lower;
- if(should_lower[1] && should_lower[0])
- lower = jump_strengths[1] > jump_strengths[0];
- else if(should_lower[0])
- lower = 0;
- else if(should_lower[1])
- lower = 1;
- else
- break;
-
- if(jump_strengths[lower] == strength_return) {
- ir_variable* return_flag = this->function.get_return_flag();
- if(!this->function.signature->return_type->is_void()) {
- ir_variable* return_value = this->function.get_return_value();
- jumps[lower]->insert_before(new(ir) ir_assignment(new (ir) ir_dereference_variable(return_value), ((ir_return*)jumps[lower])->value, NULL));
- }
- jumps[lower]->insert_before(new(ir) ir_assignment(new (ir) ir_dereference_variable(return_flag), new (ir) ir_constant(true), NULL));
- this->loop.may_set_return_flag = true;
- if(this->loop.loop) {
- ir_loop_jump* lowered = 0;
- lowered = new(ir) ir_loop_jump(ir_loop_jump::jump_break);
- block_records[lower].min_strength = strength_break;
- jumps[lower]->replace_with(lowered);
- jumps[lower] = lowered;
- } else
- goto lower_continue;
- this->progress = true;
- } else if(jump_strengths[lower] == strength_break) {
- /* We can't lower to an actual continue because that would execute the increment.
- *
- * In the lowered code, we instead put the break check between the this->loop body and the increment,
- * which is impossible with a real continue as defined by the GLSL IR currently.
- *
- * Smarter options (such as undoing the increment) are possible but it's not worth implementing them,
- * because if break is lowered, continue is almost surely lowered too.
- */
- jumps[lower]->insert_before(new(ir) ir_assignment(new (ir) ir_dereference_variable(this->loop.get_break_flag()), new (ir) ir_constant(true), 0));
- goto lower_continue;
- } else if(jump_strengths[lower] == strength_continue) {
-lower_continue:
- ir_variable* execute_flag = this->loop.get_execute_flag();
- jumps[lower]->replace_with(new(ir) ir_assignment(new (ir) ir_dereference_variable(execute_flag), new (ir) ir_constant(false), 0));
- jumps[lower] = 0;
- block_records[lower].min_strength = strength_always_clears_execute_flag;
- block_records[lower].may_clear_execute_flag = true;
- this->progress = true;
- break;
- }
- }
-
- /* move out a jump out if possible */
- if(pull_out_jumps) {
- int move_out = -1;
- if(jumps[0] && block_records[1].min_strength >= strength_continue)
- move_out = 0;
- else if(jumps[1] && block_records[0].min_strength >= strength_continue)
- move_out = 1;
-
- if(move_out >= 0)
- {
- jumps[move_out]->remove();
- ir->insert_after(jumps[move_out]);
- jumps[move_out] = 0;
- block_records[move_out].min_strength = strength_none;
- this->progress = true;
- }
- }
-
- if(block_records[0].min_strength < block_records[1].min_strength)
- this->block.min_strength = block_records[0].min_strength;
- else
- this->block.min_strength = block_records[1].min_strength;
- this->block.may_clear_execute_flag = this->block.may_clear_execute_flag || block_records[0].may_clear_execute_flag || block_records[1].may_clear_execute_flag;
-
- if(this->block.min_strength)
- truncate_after_instruction(ir);
- else if(this->block.may_clear_execute_flag)
- {
- int move_into = -1;
- if(block_records[0].min_strength && !block_records[1].may_clear_execute_flag)
- move_into = 1;
- else if(block_records[1].min_strength && !block_records[0].may_clear_execute_flag)
- move_into = 0;
-
- if(move_into >= 0) {
- assert(!block_records[move_into].min_strength && !block_records[move_into].may_clear_execute_flag); /* otherwise, we just truncated */
-
- exec_list* list = move_into ? &ir->else_instructions : &ir->then_instructions;
- exec_node* next = ir->get_next();
- if(!next->is_tail_sentinel()) {
- move_outer_block_inside(ir, list);
-
- exec_list list;
- list.head = next;
- block_records[move_into] = visit_block(&list);
-
- this->progress = true;
- goto retry;
- }
- } else {
- ir_instruction* ir_after;
- for(ir_after = (ir_instruction*)ir->get_next(); !ir_after->is_tail_sentinel();)
- {
- ir_if* ir_if = ir_after->as_if();
- if(ir_if && ir_if->else_instructions.is_empty()) {
- ir_dereference_variable* ir_if_cond_deref = ir_if->condition->as_dereference_variable();
- if(ir_if_cond_deref && ir_if_cond_deref->var == this->loop.execute_flag) {
- ir_instruction* ir_next = (ir_instruction*)ir_after->get_next();
- ir_after->insert_before(&ir_if->then_instructions);
- ir_after->remove();
- ir_after = ir_next;
- continue;
- }
- }
- ir_after = (ir_instruction*)ir_after->get_next();
-
- /* only set this if we find any unprotected instruction */
- this->progress = true;
- }
-
- if(!ir->get_next()->is_tail_sentinel()) {
- assert(this->loop.execute_flag);
- ir_if* if_execute = new(ir) ir_if(new(ir) ir_dereference_variable(this->loop.execute_flag));
- move_outer_block_inside(ir, &if_execute->then_instructions);
- ir->insert_after(if_execute);
- }
- }
- }
- --this->loop.nesting_depth;
- --this->function.nesting_depth;
- }
-
- virtual void visit(ir_loop *ir)
- {
- ++this->function.nesting_depth;
- loop_record saved_loop = this->loop;
- this->loop = loop_record(this->function.signature, ir);
-
- block_record body = visit_block(&ir->body_instructions);
-
- if(body.min_strength >= strength_break) {
- /* FINISHME: turn the this->loop into an if, or replace it with its body */
- }
-
- if(this->loop.break_flag) {
- ir_if* break_if = new(ir) ir_if(new(ir) ir_dereference_variable(this->loop.break_flag));
- break_if->then_instructions.push_tail(new(ir) ir_loop_jump(ir_loop_jump::jump_break));
- ir->body_instructions.push_tail(break_if);
- }
-
- if(this->loop.may_set_return_flag) {
- assert(this->function.return_flag);
- ir_if* return_if = new(ir) ir_if(new(ir) ir_dereference_variable(this->function.return_flag));
- saved_loop.may_set_return_flag = true;
- if(saved_loop.loop)
- return_if->then_instructions.push_tail(new(ir) ir_loop_jump(ir_loop_jump::jump_break));
- else
- move_outer_block_inside(ir, &return_if->else_instructions);
- ir->insert_after(return_if);
- }
-
- this->loop = saved_loop;
- --this->function.nesting_depth;
- }
-
- virtual void visit(ir_function_signature *ir)
- {
- /* these are not strictly necessary */
- assert(!this->function.signature);
- assert(!this->loop.loop);
-
- function_record saved_function = this->function;
- loop_record saved_loop = this->loop;
- this->function = function_record(ir);
- this->loop = loop_record(ir);
-
- assert(!this->loop.loop);
- visit_block(&ir->body);
-
- if(this->function.return_value)
- ir->body.push_tail(new(ir) ir_return(new (ir) ir_dereference_variable(this->function.return_value)));
-
- this->loop = saved_loop;
- this->function = saved_function;
- }
-
- virtual void visit(class ir_function * ir)
- {
- visit_block(&ir->signatures);
- }
-};
-
-bool
-do_lower_jumps(exec_list *instructions, bool pull_out_jumps, bool lower_sub_return, bool lower_main_return, bool lower_continue, bool lower_break)
-{
- ir_lower_jumps_visitor v;
- v.pull_out_jumps = pull_out_jumps;
- v.lower_continue = lower_continue;
- v.lower_break = lower_break;
- v.lower_sub_return = lower_sub_return;
- v.lower_main_return = lower_main_return;
-
- do {
- v.progress = false;
- visit_exec_list(instructions, &v);
- } while (v.progress);
-
- return v.progress;
-}
+/*
+ * Copyright © 2010 Luca Barbieri
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+/**
+ * \file lower_jumps.cpp
+ *
+ * This pass lowers jumps (break, continue, and return) to if/else structures.
+ *
+ * It can be asked to:
+ * 1. Pull jumps out of ifs where possible
+ * 2. Remove all "continue"s, replacing them with an "execute flag"
+ * 3. Replace all "break" with a single conditional one at the end of the loop
+ * 4. Replace all "return"s with a single return at the end of the function,
+ * for the main function and/or other functions
+ *
+ * Applying this pass gives several benefits:
+ * 1. All functions can be inlined.
+ * 2. nv40 and other pre-DX10 chips without "continue" can be supported
+ * 3. nv30 and other pre-DX10 chips with no control flow at all are better
+ * supported
+ *
+ * Continues are lowered by adding a per-loop "execute flag", initialized to
+ * true, that when cleared inhibits all execution until the end of the loop.
+ *
+ * Breaks are lowered to continues, plus setting a "break flag" that is checked
+ * at the end of the loop, and trigger the unique "break".
+ *
+ * Returns are lowered to breaks/continues, plus adding a "return flag" that
+ * causes loops to break again out of their enclosing loops until all the
+ * loops are exited: then the "execute flag" logic will ignore everything
+ * until the end of the function.
+ *
+ * Note that "continue" and "return" can also be implemented by adding
+ * a dummy loop and using break.
+ * However, this is bad for hardware with limited nesting depth, and
+ * prevents further optimization, and thus is not currently performed.
+ */
+
+#include "glsl_types.h"
+#include <string.h>
+#include "ir.h"
+
+/**
+ * Enum recording the result of analyzing how control flow might exit
+ * an IR node.
+ *
+ * Each possible value of jump_strength indicates a strictly stronger
+ * guarantee on control flow than the previous value.
+ *
+ * The ordering of strengths roughly reflects the way jumps are
+ * lowered: jumps with higher strength tend to be lowered to jumps of
+ * lower strength. Accordingly, strength is used as a heuristic to
+ * determine which lowering to perform first.
+ *
+ * This enum is also used by get_jump_strength() to categorize
+ * instructions as either break, continue, return, or other. When
+ * used in this fashion, strength_always_clears_execute_flag is not
+ * used.
+ *
+ * The control flow analysis made by this optimization pass makes two
+ * simplifying assumptions:
+ *
+ * - It ignores discard instructions, since they are lowered by a
+ * separate pass (lower_discard.cpp).
+ *
+ * - It assumes it is always possible for control to flow from a loop
+ * to the instruction immediately following it. Technically, this
+ * is not true (since all execution paths through the loop might
+ * jump back to the top, or return from the function).
+ *
+ * Both of these simplifying assumtions are safe, since they can never
+ * cause reachable code to be incorrectly classified as unreachable;
+ * they can only do the opposite.
+ */
+enum jump_strength
+{
+ /**
+ * Analysis has produced no guarantee on how control flow might
+ * exit this IR node. It might fall out the bottom (with or
+ * without clearing the execute flag, if present), or it might
+ * continue to the top of the innermost enclosing loop, break out
+ * of it, or return from the function.
+ */
+ strength_none,
+
+ /**
+ * The only way control can fall out the bottom of this node is
+ * through a code path that clears the execute flag. It might also
+ * continue to the top of the innermost enclosing loop, break out
+ * of it, or return from the function.
+ */
+ strength_always_clears_execute_flag,
+
+ /**
+ * Control cannot fall out the bottom of this node. It might
+ * continue to the top of the innermost enclosing loop, break out
+ * of it, or return from the function.
+ */
+ strength_continue,
+
+ /**
+ * Control cannot fall out the bottom of this node, or continue the
+ * top of the innermost enclosing loop. It can only break out of
+ * it or return from the function.
+ */
+ strength_break,
+
+ /**
+ * Control cannot fall out the bottom of this node, continue to the
+ * top of the innermost enclosing loop, or break out of it. It can
+ * only return from the function.
+ */
+ strength_return
+};
+
+struct block_record
+{
+ /* minimum jump strength (of lowered IR, not pre-lowering IR)
+ *
+ * If the block ends with a jump, must be the strength of the jump.
+ * Otherwise, the jump would be dead and have been deleted before)
+ *
+ * If the block doesn't end with a jump, it can be different than strength_none if all paths before it lead to some jump
+ * (e.g. an if with a return in one branch, and a break in the other, while not lowering them)
+ * Note that identical jumps are usually unified though.
+ */
+ jump_strength min_strength;
+
+ /* can anything clear the execute flag? */
+ bool may_clear_execute_flag;
+
+ block_record()
+ {
+ this->min_strength = strength_none;
+ this->may_clear_execute_flag = false;
+ }
+};
+
+struct loop_record
+{
+ ir_function_signature* signature;
+ ir_loop* loop;
+
+ /* used to avoid lowering the break used to represent lowered breaks */
+ unsigned nesting_depth;
+ bool in_if_at_the_end_of_the_loop;
+
+ bool may_set_return_flag;
+
+ ir_variable* break_flag;
+ ir_variable* execute_flag; /* cleared to emulate continue */
+
+ loop_record(ir_function_signature* p_signature = 0, ir_loop* p_loop = 0)
+ {
+ this->signature = p_signature;
+ this->loop = p_loop;
+ this->nesting_depth = 0;
+ this->in_if_at_the_end_of_the_loop = false;
+ this->may_set_return_flag = false;
+ this->break_flag = 0;
+ this->execute_flag = 0;
+ }
+
+ ir_variable* get_execute_flag()
+ {
+ /* also supported for the "function loop" */
+ if(!this->execute_flag) {
+ exec_list& list = this->loop ? this->loop->body_instructions : signature->body;
+ this->execute_flag = new(this->signature) ir_variable(glsl_type::bool_type, "execute_flag", ir_var_temporary);
+ list.push_head(new(this->signature) ir_assignment(new(this->signature) ir_dereference_variable(execute_flag), new(this->signature) ir_constant(true), 0));
+ list.push_head(this->execute_flag);
+ }
+ return this->execute_flag;
+ }
+
+ ir_variable* get_break_flag()
+ {
+ assert(this->loop);
+ if(!this->break_flag) {
+ this->break_flag = new(this->signature) ir_variable(glsl_type::bool_type, "break_flag", ir_var_temporary);
+ this->loop->insert_before(this->break_flag);
+ this->loop->insert_before(new(this->signature) ir_assignment(new(this->signature) ir_dereference_variable(break_flag), new(this->signature) ir_constant(false), 0));
+ }
+ return this->break_flag;
+ }
+};
+
+struct function_record
+{
+ ir_function_signature* signature;
+ ir_variable* return_flag; /* used to break out of all loops and then jump to the return instruction */
+ ir_variable* return_value;
+ bool lower_return;
+ unsigned nesting_depth;
+
+ function_record(ir_function_signature* p_signature = 0,
+ bool lower_return = false)
+ {
+ this->signature = p_signature;
+ this->return_flag = 0;
+ this->return_value = 0;
+ this->nesting_depth = 0;
+ this->lower_return = lower_return;
+ }
+
+ ir_variable* get_return_flag()
+ {
+ if(!this->return_flag) {
+ this->return_flag = new(this->signature) ir_variable(glsl_type::bool_type, "return_flag", ir_var_temporary);
+ this->signature->body.push_head(new(this->signature) ir_assignment(new(this->signature) ir_dereference_variable(return_flag), new(this->signature) ir_constant(false), 0));
+ this->signature->body.push_head(this->return_flag);
+ }
+ return this->return_flag;
+ }
+
+ ir_variable* get_return_value()
+ {
+ if(!this->return_value) {
+ assert(!this->signature->return_type->is_void());
+ return_value = new(this->signature) ir_variable(this->signature->return_type, "return_value", ir_var_temporary);
+ this->signature->body.push_head(this->return_value);
+ }
+ return this->return_value;
+ }
+};
+
+struct ir_lower_jumps_visitor : public ir_control_flow_visitor {
+ /* Postconditions: on exit of any visit() function:
+ *
+ * ANALYSIS: this->block.min_strength,
+ * this->block.may_clear_execute_flag, and
+ * this->loop.may_set_return_flag are updated to reflect the
+ * characteristics of the visited statement.
+ *
+ * DEAD_CODE_ELIMINATION: If this->block.min_strength is not
+ * strength_none, the visited node is at the end of its exec_list.
+ * In other words, any unreachable statements that follow the
+ * visited statement in its exec_list have been removed.
+ *
+ * CONTAINED_JUMPS_LOWERED: If the visited statement contains other
+ * statements, then should_lower_jump() is false for all of the
+ * return, break, or continue statements it contains.
+ *
+ * Note that visiting a jump does not lower it. That is the
+ * responsibility of the statement (or function signature) that
+ * contains the jump.
+ */
+
+ bool progress;
+
+ struct function_record function;
+ struct loop_record loop;
+ struct block_record block;
+
+ bool pull_out_jumps;
+ bool lower_continue;
+ bool lower_break;
+ bool lower_sub_return;
+ bool lower_main_return;
+
+ ir_lower_jumps_visitor()
+ {
+ this->progress = false;
+ }
+
+ void truncate_after_instruction(exec_node *ir)
+ {
+ if (!ir)
+ return;
+
+ while (!ir->get_next()->is_tail_sentinel()) {
+ ((ir_instruction *)ir->get_next())->remove();
+ this->progress = true;
+ }
+ }
+
+ void move_outer_block_inside(ir_instruction *ir, exec_list *inner_block)
+ {
+ while (!ir->get_next()->is_tail_sentinel()) {
+ ir_instruction *move_ir = (ir_instruction *)ir->get_next();
+
+ move_ir->remove();
+ inner_block->push_tail(move_ir);
+ }
+ }
+
+ /**
+ * Insert the instructions necessary to lower a return statement,
+ * before the given return instruction.
+ */
+ void insert_lowered_return(ir_return *ir)
+ {
+ ir_variable* return_flag = this->function.get_return_flag();
+ if(!this->function.signature->return_type->is_void()) {
+ ir_variable* return_value = this->function.get_return_value();
+ ir->insert_before(
+ new(ir) ir_assignment(
+ new (ir) ir_dereference_variable(return_value),
+ ir->value));
+ }
+ ir->insert_before(
+ new(ir) ir_assignment(
+ new (ir) ir_dereference_variable(return_flag),
+ new (ir) ir_constant(true)));
+ this->loop.may_set_return_flag = true;
+ }
+
+ /**
+ * If the given instruction is a return, lower it to instructions
+ * that store the return value (if there is one), set the return
+ * flag, and then break.
+ *
+ * It is safe to pass NULL to this function.
+ */
+ void lower_return_unconditionally(ir_instruction *ir)
+ {
+ if (get_jump_strength(ir) != strength_return) {
+ return;
+ }
+ insert_lowered_return((ir_return*)ir);
+ ir->replace_with(new(ir) ir_loop_jump(ir_loop_jump::jump_break));
+ }
+
+ /**
+ * Create the necessary instruction to replace a break instruction.
+ */
+ ir_instruction *create_lowered_break()
+ {
+ void *ctx = this->function.signature;
+ return new(ctx) ir_assignment(
+ new(ctx) ir_dereference_variable(this->loop.get_break_flag()),
+ new(ctx) ir_constant(true),
+ 0);
+ }
+
+ /**
+ * If the given instruction is a break, lower it to an instruction
+ * that sets the break flag, without consulting
+ * should_lower_jump().
+ *
+ * It is safe to pass NULL to this function.
+ */
+ void lower_break_unconditionally(ir_instruction *ir)
+ {
+ if (get_jump_strength(ir) != strength_break) {
+ return;
+ }
+ ir->replace_with(create_lowered_break());
+ }
+
+ /**
+ * If the block ends in a conditional or unconditional break, lower
+ * it, even though should_lower_jump() says it needn't be lowered.
+ */
+ void lower_final_breaks(exec_list *block)
+ {
+ ir_instruction *ir = (ir_instruction *) block->get_tail();
+ lower_break_unconditionally(ir);
+ ir_if *ir_if = ir->as_if();
+ if (ir_if) {
+ lower_break_unconditionally(
+ (ir_instruction *) ir_if->then_instructions.get_tail());
+ lower_break_unconditionally(
+ (ir_instruction *) ir_if->else_instructions.get_tail());
+ }
+ }
+
+ virtual void visit(class ir_loop_jump * ir)
+ {
+ /* Eliminate all instructions after each one, since they are
+ * unreachable. This satisfies the DEAD_CODE_ELIMINATION
+ * postcondition.
+ */
+ truncate_after_instruction(ir);
+
+ /* Set this->block.min_strength based on this instruction. This
+ * satisfies the ANALYSIS postcondition. It is not necessary to
+ * update this->block.may_clear_execute_flag or
+ * this->loop.may_set_return_flag, because an unlowered jump
+ * instruction can't change any flags.
+ */
+ this->block.min_strength = ir->is_break() ? strength_break : strength_continue;
+
+ /* The CONTAINED_JUMPS_LOWERED postcondition is already
+ * satisfied, because jump statements can't contain other
+ * statements.
+ */
+ }
+
+ virtual void visit(class ir_return * ir)
+ {
+ /* Eliminate all instructions after each one, since they are
+ * unreachable. This satisfies the DEAD_CODE_ELIMINATION
+ * postcondition.
+ */
+ truncate_after_instruction(ir);
+
+ /* Set this->block.min_strength based on this instruction. This
+ * satisfies the ANALYSIS postcondition. It is not necessary to
+ * update this->block.may_clear_execute_flag or
+ * this->loop.may_set_return_flag, because an unlowered return
+ * instruction can't change any flags.
+ */
+ this->block.min_strength = strength_return;
+
+ /* The CONTAINED_JUMPS_LOWERED postcondition is already
+ * satisfied, because jump statements can't contain other
+ * statements.
+ */
+ }
+
+ virtual void visit(class ir_discard * ir)
+ {
+ /* Nothing needs to be done. The ANALYSIS and
+ * DEAD_CODE_ELIMINATION postconditions are already satisfied,
+ * because discard statements are ignored by this optimization
+ * pass. The CONTAINED_JUMPS_LOWERED postcondition is already
+ * satisfied, because discard statements can't contain other
+ * statements.
+ */
+ }
+
+ enum jump_strength get_jump_strength(ir_instruction* ir)
+ {
+ if(!ir)
+ return strength_none;
+ else if(ir->ir_type == ir_type_loop_jump) {
+ if(((ir_loop_jump*)ir)->is_break())
+ return strength_break;
+ else
+ return strength_continue;
+ } else if(ir->ir_type == ir_type_return)
+ return strength_return;
+ else
+ return strength_none;
+ }
+
+ bool should_lower_jump(ir_jump* ir)
+ {
+ unsigned strength = get_jump_strength(ir);
+ bool lower;
+ switch(strength)
+ {
+ case strength_none:
+ lower = false; /* don't change this, code relies on it */
+ break;
+ case strength_continue:
+ lower = lower_continue;
+ break;
+ case strength_break:
+ assert(this->loop.loop);
+ /* never lower "canonical break" */
+ if(ir->get_next()->is_tail_sentinel() && (this->loop.nesting_depth == 0
+ || (this->loop.nesting_depth == 1 && this->loop.in_if_at_the_end_of_the_loop)))
+ lower = false;
+ else
+ lower = lower_break;
+ break;
+ case strength_return:
+ /* never lower return at the end of a this->function */
+ if(this->function.nesting_depth == 0 && ir->get_next()->is_tail_sentinel())
+ lower = false;
+ else
+ lower = this->function.lower_return;
+ break;
+ }
+ return lower;
+ }
+
+ block_record visit_block(exec_list* list)
+ {
+ /* Note: since visiting a node may change that node's next
+ * pointer, we can't use visit_exec_list(), because
+ * visit_exec_list() caches the node's next pointer before
+ * visiting it. So we use foreach_list() instead.
+ *
+ * foreach_list() isn't safe if the node being visited gets
+ * removed, but fortunately this visitor doesn't do that.
+ */
+
+ block_record saved_block = this->block;
+ this->block = block_record();
+ foreach_list(node, list) {
+ ((ir_instruction *) node)->accept(this);
+ }
+ block_record ret = this->block;
+ this->block = saved_block;
+ return ret;
+ }
+
+ virtual void visit(ir_if *ir)
+ {
+ if(this->loop.nesting_depth == 0 && ir->get_next()->is_tail_sentinel())
+ this->loop.in_if_at_the_end_of_the_loop = true;
+
+ ++this->function.nesting_depth;
+ ++this->loop.nesting_depth;
+
+ block_record block_records[2];
+ ir_jump* jumps[2];
+
+ /* Recursively lower nested jumps. This satisfies the
+ * CONTAINED_JUMPS_LOWERED postcondition, except in the case of
+ * unconditional jumps at the end of ir->then_instructions and
+ * ir->else_instructions, which are handled below.
+ */
+ block_records[0] = visit_block(&ir->then_instructions);
+ block_records[1] = visit_block(&ir->else_instructions);
+
+retry: /* we get here if we put code after the if inside a branch */
+
+ /* Determine which of ir->then_instructions and
+ * ir->else_instructions end with an unconditional jump.
+ */
+ for(unsigned i = 0; i < 2; ++i) {
+ exec_list& list = i ? ir->else_instructions : ir->then_instructions;
+ jumps[i] = 0;
+ if(!list.is_empty() && get_jump_strength((ir_instruction*)list.get_tail()))
+ jumps[i] = (ir_jump*)list.get_tail();
+ }
+
+ /* Loop until we have satisfied the CONTAINED_JUMPS_LOWERED
+ * postcondition by lowering jumps in both then_instructions and
+ * else_instructions.
+ */
+ for(;;) {
+ /* Determine the types of the jumps that terminate
+ * ir->then_instructions and ir->else_instructions.
+ */
+ jump_strength jump_strengths[2];
+
+ for(unsigned i = 0; i < 2; ++i) {
+ if(jumps[i]) {
+ jump_strengths[i] = block_records[i].min_strength;
+ assert(jump_strengths[i] == get_jump_strength(jumps[i]));
+ } else
+ jump_strengths[i] = strength_none;
+ }
+
+ /* If both code paths end in a jump, and the jumps are the
+ * same, and we are pulling out jumps, replace them with a
+ * single jump that comes after the if instruction. The new
+ * jump will be visited next, and it will be lowered if
+ * necessary by the loop or conditional that encloses it.
+ */
+ if(pull_out_jumps && jump_strengths[0] == jump_strengths[1]) {
+ bool unify = true;
+ if(jump_strengths[0] == strength_continue)
+ ir->insert_after(new(ir) ir_loop_jump(ir_loop_jump::jump_continue));
+ else if(jump_strengths[0] == strength_break)
+ ir->insert_after(new(ir) ir_loop_jump(ir_loop_jump::jump_break));
+ /* FINISHME: unify returns with identical expressions */
+ else if(jump_strengths[0] == strength_return && this->function.signature->return_type->is_void())
+ ir->insert_after(new(ir) ir_return(NULL));
+ else
+ unify = false;
+
+ if(unify) {
+ jumps[0]->remove();
+ jumps[1]->remove();
+ this->progress = true;
+
+ /* Update jumps[] to reflect the fact that the jumps
+ * are gone, and update block_records[] to reflect the
+ * fact that control can now flow to the next
+ * instruction.
+ */
+ jumps[0] = 0;
+ jumps[1] = 0;
+ block_records[0].min_strength = strength_none;
+ block_records[1].min_strength = strength_none;
+
+ /* The CONTAINED_JUMPS_LOWERED postcondition is now
+ * satisfied, so we can break out of the loop.
+ */
+ break;
+ }
+ }
+
+ /* lower a jump: if both need to lowered, start with the strongest one, so that
+ * we might later unify the lowered version with the other one
+ */
+ bool should_lower[2];
+ for(unsigned i = 0; i < 2; ++i)
+ should_lower[i] = should_lower_jump(jumps[i]);
+
+ int lower;
+ if(should_lower[1] && should_lower[0])
+ lower = jump_strengths[1] > jump_strengths[0];
+ else if(should_lower[0])
+ lower = 0;
+ else if(should_lower[1])
+ lower = 1;
+ else
+ /* Neither code path ends in a jump that needs to be
+ * lowered, so the CONTAINED_JUMPS_LOWERED postcondition
+ * is satisfied and we can break out of the loop.
+ */
+ break;
+
+ if(jump_strengths[lower] == strength_return) {
+ /* To lower a return, we create a return flag (if the
+ * function doesn't have one already) and add instructions
+ * that: 1. store the return value (if this function has a
+ * non-void return) and 2. set the return flag
+ */
+ insert_lowered_return((ir_return*)jumps[lower]);
+ if(this->loop.loop) {
+ /* If we are in a loop, replace the return instruction
+ * with a break instruction, and then loop so that the
+ * break instruction can be lowered if necessary.
+ */
+ ir_loop_jump* lowered = 0;
+ lowered = new(ir) ir_loop_jump(ir_loop_jump::jump_break);
+ /* Note: we must update block_records and jumps to
+ * reflect the fact that the control path has been
+ * altered from a return to a break.
+ */
+ block_records[lower].min_strength = strength_break;
+ jumps[lower]->replace_with(lowered);
+ jumps[lower] = lowered;
+ } else {
+ /* If we are not in a loop, we then proceed as we would
+ * for a continue statement (set the execute flag to
+ * false to prevent the rest of the function from
+ * executing).
+ */
+ goto lower_continue;
+ }
+ this->progress = true;
+ } else if(jump_strengths[lower] == strength_break) {
+ /* To lower a break, we create a break flag (if the loop
+ * doesn't have one already) and add an instruction that
+ * sets it.
+ *
+ * Then we proceed as we would for a continue statement
+ * (set the execute flag to false to prevent the rest of
+ * the loop body from executing).
+ *
+ * The visit() function for the loop will ensure that the
+ * break flag is checked after executing the loop body.
+ */
+ jumps[lower]->insert_before(create_lowered_break());
+ goto lower_continue;
+ } else if(jump_strengths[lower] == strength_continue) {
+lower_continue:
+ /* To lower a continue, we create an execute flag (if the
+ * loop doesn't have one already) and replace the continue
+ * with an instruction that clears it.
+ *
+ * Note that this code path gets exercised when lowering
+ * return statements that are not inside a loop, so
+ * this->loop must be initialized even outside of loops.
+ */
+ ir_variable* execute_flag = this->loop.get_execute_flag();
+ jumps[lower]->replace_with(new(ir) ir_assignment(new (ir) ir_dereference_variable(execute_flag), new (ir) ir_constant(false), 0));
+ /* Note: we must update block_records and jumps to reflect
+ * the fact that the control path has been altered to an
+ * instruction that clears the execute flag.
+ */
+ jumps[lower] = 0;
+ block_records[lower].min_strength = strength_always_clears_execute_flag;
+ block_records[lower].may_clear_execute_flag = true;
+ this->progress = true;
+
+ /* Let the loop run again, in case the other branch of the
+ * if needs to be lowered too.
+ */
+ }
+ }
+
+ /* move out a jump out if possible */
+ if(pull_out_jumps) {
+ /* If one of the branches ends in a jump, and control cannot
+ * fall out the bottom of the other branch, then we can move
+ * the jump after the if.
+ *
+ * Set move_out to the branch we are moving a jump out of.
+ */
+ int move_out = -1;
+ if(jumps[0] && block_records[1].min_strength >= strength_continue)
+ move_out = 0;
+ else if(jumps[1] && block_records[0].min_strength >= strength_continue)
+ move_out = 1;
+
+ if(move_out >= 0)
+ {
+ jumps[move_out]->remove();
+ ir->insert_after(jumps[move_out]);
+ /* Note: we must update block_records and jumps to reflect
+ * the fact that the jump has been moved out of the if.
+ */
+ jumps[move_out] = 0;
+ block_records[move_out].min_strength = strength_none;
+ this->progress = true;
+ }
+ }
+
+ /* Now satisfy the ANALYSIS postcondition by setting
+ * this->block.min_strength and
+ * this->block.may_clear_execute_flag based on the
+ * characteristics of the two branches.
+ */
+ if(block_records[0].min_strength < block_records[1].min_strength)
+ this->block.min_strength = block_records[0].min_strength;
+ else
+ this->block.min_strength = block_records[1].min_strength;
+ this->block.may_clear_execute_flag = this->block.may_clear_execute_flag || block_records[0].may_clear_execute_flag || block_records[1].may_clear_execute_flag;
+
+ /* Now we need to clean up the instructions that follow the
+ * if.
+ *
+ * If those instructions are unreachable, then satisfy the
+ * DEAD_CODE_ELIMINATION postcondition by eliminating them.
+ * Otherwise that postcondition is already satisfied.
+ */
+ if(this->block.min_strength)
+ truncate_after_instruction(ir);
+ else if(this->block.may_clear_execute_flag)
+ {
+ /* If the "if" instruction might clear the execute flag, then
+ * we need to guard any instructions that follow so that they
+ * are only executed if the execute flag is set.
+ *
+ * If one of the branches of the "if" always clears the
+ * execute flag, and the other branch never clears it, then
+ * this is easy: just move all the instructions following the
+ * "if" into the branch that never clears it.
+ */
+ int move_into = -1;
+ if(block_records[0].min_strength && !block_records[1].may_clear_execute_flag)
+ move_into = 1;
+ else if(block_records[1].min_strength && !block_records[0].may_clear_execute_flag)
+ move_into = 0;
+
+ if(move_into >= 0) {
+ assert(!block_records[move_into].min_strength && !block_records[move_into].may_clear_execute_flag); /* otherwise, we just truncated */
+
+ exec_list* list = move_into ? &ir->else_instructions : &ir->then_instructions;
+ exec_node* next = ir->get_next();
+ if(!next->is_tail_sentinel()) {
+ move_outer_block_inside(ir, list);
+
+ /* If any instructions moved, then we need to visit
+ * them (since they are now inside the "if"). Since
+ * block_records[move_into] is in its default state
+ * (see assertion above), we can safely replace
+ * block_records[move_into] with the result of this
+ * analysis.
+ */
+ exec_list list;
+ list.head = next;
+ block_records[move_into] = visit_block(&list);
+
+ /*
+ * Then we need to re-start our jump lowering, since one
+ * of the instructions we moved might be a jump that
+ * needs to be lowered.
+ */
+ this->progress = true;
+ goto retry;
+ }
+ } else {
+ /* If we get here, then the simple case didn't apply; we
+ * need to actually guard the instructions that follow.
+ *
+ * To avoid creating unnecessarily-deep nesting, first
+ * look through the instructions that follow and unwrap
+ * any instructions that that are already wrapped in the
+ * appropriate guard.
+ */
+ ir_instruction* ir_after;
+ for(ir_after = (ir_instruction*)ir->get_next(); !ir_after->is_tail_sentinel();)
+ {
+ ir_if* ir_if = ir_after->as_if();
+ if(ir_if && ir_if->else_instructions.is_empty()) {
+ ir_dereference_variable* ir_if_cond_deref = ir_if->condition->as_dereference_variable();
+ if(ir_if_cond_deref && ir_if_cond_deref->var == this->loop.execute_flag) {
+ ir_instruction* ir_next = (ir_instruction*)ir_after->get_next();
+ ir_after->insert_before(&ir_if->then_instructions);
+ ir_after->remove();
+ ir_after = ir_next;
+ continue;
+ }
+ }
+ ir_after = (ir_instruction*)ir_after->get_next();
+
+ /* only set this if we find any unprotected instruction */
+ this->progress = true;
+ }
+
+ /* Then, wrap all the instructions that follow in a single
+ * guard.
+ */
+ if(!ir->get_next()->is_tail_sentinel()) {
+ assert(this->loop.execute_flag);
+ ir_if* if_execute = new(ir) ir_if(new(ir) ir_dereference_variable(this->loop.execute_flag));
+ move_outer_block_inside(ir, &if_execute->then_instructions);
+ ir->insert_after(if_execute);
+ }
+ }
+ }
+ --this->loop.nesting_depth;
+ --this->function.nesting_depth;
+ }
+
+ virtual void visit(ir_loop *ir)
+ {
+ /* Visit the body of the loop, with a fresh data structure in
+ * this->loop so that the analysis we do here won't bleed into
+ * enclosing loops.
+ *
+ * We assume that all code after a loop is reachable from the
+ * loop (see comments on enum jump_strength), so the
+ * DEAD_CODE_ELIMINATION postcondition is automatically
+ * satisfied, as is the block.min_strength portion of the
+ * ANALYSIS postcondition.
+ *
+ * The block.may_clear_execute_flag portion of the ANALYSIS
+ * postcondition is automatically satisfied because execute
+ * flags do not propagate outside of loops.
+ *
+ * The loop.may_set_return_flag portion of the ANALYSIS
+ * postcondition is handled below.
+ */
+ ++this->function.nesting_depth;
+ loop_record saved_loop = this->loop;
+ this->loop = loop_record(this->function.signature, ir);
+
+ /* Recursively lower nested jumps. This satisfies the
+ * CONTAINED_JUMPS_LOWERED postcondition, except in the case of
+ * an unconditional continue or return at the bottom of the
+ * loop, which are handled below.
+ */
+ block_record body = visit_block(&ir->body_instructions);
+
+ /* If the loop ends in an unconditional continue, eliminate it
+ * because it is redundant.
+ */
+ ir_instruction *ir_last
+ = (ir_instruction *) ir->body_instructions.get_tail();
+ if (get_jump_strength(ir_last) == strength_continue) {
+ ir_last->remove();
+ }
+
+ /* If the loop ends in an unconditional return, and we are
+ * lowering returns, lower it.
+ */
+ if (this->function.lower_return)
+ lower_return_unconditionally(ir_last);
+
+ if(body.min_strength >= strength_break) {
+ /* FINISHME: If the min_strength of the loop body is
+ * strength_break or strength_return, that means that it
+ * isn't a loop at all, since control flow always leaves the
+ * body of the loop via break or return. In principle the
+ * loop could be eliminated in this case. This optimization
+ * is not implemented yet.
+ */
+ }
+
+ if(this->loop.break_flag) {
+ /* We only get here if we are lowering breaks */
+ assert (lower_break);
+
+ /* If a break flag was generated while visiting the body of
+ * the loop, then at least one break was lowered, so we need
+ * to generate an if statement at the end of the loop that
+ * does a "break" if the break flag is set. The break we
+ * generate won't violate the CONTAINED_JUMPS_LOWERED
+ * postcondition, because should_lower_jump() always returns
+ * false for a break that happens at the end of a loop.
+ *
+ * However, if the loop already ends in a conditional or
+ * unconditional break, then we need to lower that break,
+ * because it won't be at the end of the loop anymore.
+ */
+ lower_final_breaks(&ir->body_instructions);
+
+ ir_if* break_if = new(ir) ir_if(new(ir) ir_dereference_variable(this->loop.break_flag));
+ break_if->then_instructions.push_tail(new(ir) ir_loop_jump(ir_loop_jump::jump_break));
+ ir->body_instructions.push_tail(break_if);
+ }
+
+ /* If the body of the loop may set the return flag, then at
+ * least one return was lowered to a break, so we need to ensure
+ * that the return flag is checked after the body of the loop is
+ * executed.
+ */
+ if(this->loop.may_set_return_flag) {
+ assert(this->function.return_flag);
+ /* Generate the if statement to check the return flag */
+ ir_if* return_if = new(ir) ir_if(new(ir) ir_dereference_variable(this->function.return_flag));
+ /* Note: we also need to propagate the knowledge that the
+ * return flag may get set to the outer context. This
+ * satisfies the loop.may_set_return_flag part of the
+ * ANALYSIS postcondition.
+ */
+ saved_loop.may_set_return_flag = true;
+ if(saved_loop.loop)
+ /* If this loop is nested inside another one, then the if
+ * statement that we generated should break out of that
+ * loop if the return flag is set. Caller will lower that
+ * break statement if necessary.
+ */
+ return_if->then_instructions.push_tail(new(ir) ir_loop_jump(ir_loop_jump::jump_break));
+ else
+ /* Otherwise, all we need to do is ensure that the
+ * instructions that follow are only executed if the
+ * return flag is clear. We can do that by moving those
+ * instructions into the else clause of the generated if
+ * statement.
+ */
+ move_outer_block_inside(ir, &return_if->else_instructions);
+ ir->insert_after(return_if);
+ }
+
+ this->loop = saved_loop;
+ --this->function.nesting_depth;
+ }
+
+ virtual void visit(ir_function_signature *ir)
+ {
+ /* these are not strictly necessary */
+ assert(!this->function.signature);
+ assert(!this->loop.loop);
+
+ bool lower_return;
+ if (strcmp(ir->function_name(), "main") == 0)
+ lower_return = lower_main_return;
+ else
+ lower_return = lower_sub_return;
+
+ function_record saved_function = this->function;
+ loop_record saved_loop = this->loop;
+ this->function = function_record(ir, lower_return);
+ this->loop = loop_record(ir);
+
+ assert(!this->loop.loop);
+
+ /* Visit the body of the function to lower any jumps that occur
+ * in it, except possibly an unconditional return statement at
+ * the end of it.
+ */
+ visit_block(&ir->body);
+
+ /* If the body ended in an unconditional return of non-void,
+ * then we don't need to lower it because it's the one canonical
+ * return.
+ *
+ * If the body ended in a return of void, eliminate it because
+ * it is redundant.
+ */
+ if (ir->return_type->is_void() &&
+ get_jump_strength((ir_instruction *) ir->body.get_tail())) {
+ ir_jump *jump = (ir_jump *) ir->body.get_tail();
+ assert (jump->ir_type == ir_type_return);
+ jump->remove();
+ }
+
+ if(this->function.return_value)
+ ir->body.push_tail(new(ir) ir_return(new (ir) ir_dereference_variable(this->function.return_value)));
+
+ this->loop = saved_loop;
+ this->function = saved_function;
+ }
+
+ virtual void visit(class ir_function * ir)
+ {
+ visit_block(&ir->signatures);
+ }
+};
+
+bool
+do_lower_jumps(exec_list *instructions, bool pull_out_jumps, bool lower_sub_return, bool lower_main_return, bool lower_continue, bool lower_break)
+{
+ ir_lower_jumps_visitor v;
+ v.pull_out_jumps = pull_out_jumps;
+ v.lower_continue = lower_continue;
+ v.lower_break = lower_break;
+ v.lower_sub_return = lower_sub_return;
+ v.lower_main_return = lower_main_return;
+
+ do {
+ v.progress = false;
+ visit_exec_list(instructions, &v);
+ } while (v.progress);
+
+ return v.progress;
+}