"Fossies" - the Fresh Open Source Software Archive

Member "mesa-20.1.8/src/compiler/glsl/ast_function.cpp" (16 Sep 2020, 97254 Bytes) of package /linux/misc/mesa-20.1.8.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ast_function.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 20.1.5_vs_20.2.0-rc1.

    1 /*
    2  * Copyright © 2010 Intel Corporation
    3  *
    4  * Permission is hereby granted, free of charge, to any person obtaining a
    5  * copy of this software and associated documentation files (the "Software"),
    6  * to deal in the Software without restriction, including without limitation
    7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
    8  * and/or sell copies of the Software, and to permit persons to whom the
    9  * Software is furnished to do so, subject to the following conditions:
   10  *
   11  * The above copyright notice and this permission notice (including the next
   12  * paragraph) shall be included in all copies or substantial portions of the
   13  * Software.
   14  *
   15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   21  * DEALINGS IN THE SOFTWARE.
   22  */
   23 
   24 #include "glsl_symbol_table.h"
   25 #include "ast.h"
   26 #include "compiler/glsl_types.h"
   27 #include "ir.h"
   28 #include "main/mtypes.h"
   29 #include "main/shaderobj.h"
   30 #include "builtin_functions.h"
   31 
   32 static ir_rvalue *
   33 convert_component(ir_rvalue *src, const glsl_type *desired_type);
   34 
   35 static unsigned
   36 process_parameters(exec_list *instructions, exec_list *actual_parameters,
   37                    exec_list *parameters,
   38                    struct _mesa_glsl_parse_state *state)
   39 {
   40    void *mem_ctx = state;
   41    unsigned count = 0;
   42 
   43    foreach_list_typed(ast_node, ast, link, parameters) {
   44       /* We need to process the parameters first in order to know if we can
   45        * raise or not a unitialized warning. Calling set_is_lhs silence the
   46        * warning for now. Raising the warning or not will be checked at
   47        * verify_parameter_modes.
   48        */
   49       ast->set_is_lhs(true);
   50       ir_rvalue *result = ast->hir(instructions, state);
   51 
   52       /* Error happened processing function parameter */
   53       if (!result) {
   54          actual_parameters->push_tail(ir_rvalue::error_value(mem_ctx));
   55          count++;
   56          continue;
   57       }
   58 
   59       ir_constant *const constant =
   60          result->constant_expression_value(mem_ctx);
   61 
   62       if (constant != NULL)
   63          result = constant;
   64 
   65       actual_parameters->push_tail(result);
   66       count++;
   67    }
   68 
   69    return count;
   70 }
   71 
   72 
   73 /**
   74  * Generate a source prototype for a function signature
   75  *
   76  * \param return_type Return type of the function.  May be \c NULL.
   77  * \param name        Name of the function.
   78  * \param parameters  List of \c ir_instruction nodes representing the
   79  *                    parameter list for the function.  This may be either a
   80  *                    formal (\c ir_variable) or actual (\c ir_rvalue)
   81  *                    parameter list.  Only the type is used.
   82  *
   83  * \return
   84  * A ralloced string representing the prototype of the function.
   85  */
   86 char *
   87 prototype_string(const glsl_type *return_type, const char *name,
   88                  exec_list *parameters)
   89 {
   90    char *str = NULL;
   91 
   92    if (return_type != NULL)
   93       str = ralloc_asprintf(NULL, "%s ", return_type->name);
   94 
   95    ralloc_asprintf_append(&str, "%s(", name);
   96 
   97    const char *comma = "";
   98    foreach_in_list(const ir_variable, param, parameters) {
   99       ralloc_asprintf_append(&str, "%s%s", comma, param->type->name);
  100       comma = ", ";
  101    }
  102 
  103    ralloc_strcat(&str, ")");
  104    return str;
  105 }
  106 
  107 static bool
  108 verify_image_parameter(YYLTYPE *loc, _mesa_glsl_parse_state *state,
  109                        const ir_variable *formal, const ir_variable *actual)
  110 {
  111    /**
  112     * From the ARB_shader_image_load_store specification:
  113     *
  114     * "The values of image variables qualified with coherent,
  115     *  volatile, restrict, readonly, or writeonly may not be passed
  116     *  to functions whose formal parameters lack such
  117     *  qualifiers. [...] It is legal to have additional qualifiers
  118     *  on a formal parameter, but not to have fewer."
  119     */
  120    if (actual->data.memory_coherent && !formal->data.memory_coherent) {
  121       _mesa_glsl_error(loc, state,
  122                        "function call parameter `%s' drops "
  123                        "`coherent' qualifier", formal->name);
  124       return false;
  125    }
  126 
  127    if (actual->data.memory_volatile && !formal->data.memory_volatile) {
  128       _mesa_glsl_error(loc, state,
  129                        "function call parameter `%s' drops "
  130                        "`volatile' qualifier", formal->name);
  131       return false;
  132    }
  133 
  134    if (actual->data.memory_restrict && !formal->data.memory_restrict) {
  135       _mesa_glsl_error(loc, state,
  136                        "function call parameter `%s' drops "
  137                        "`restrict' qualifier", formal->name);
  138       return false;
  139    }
  140 
  141    if (actual->data.memory_read_only && !formal->data.memory_read_only) {
  142       _mesa_glsl_error(loc, state,
  143                        "function call parameter `%s' drops "
  144                        "`readonly' qualifier", formal->name);
  145       return false;
  146    }
  147 
  148    if (actual->data.memory_write_only && !formal->data.memory_write_only) {
  149       _mesa_glsl_error(loc, state,
  150                        "function call parameter `%s' drops "
  151                        "`writeonly' qualifier", formal->name);
  152       return false;
  153    }
  154 
  155    return true;
  156 }
  157 
  158 static bool
  159 verify_first_atomic_parameter(YYLTYPE *loc, _mesa_glsl_parse_state *state,
  160                               ir_variable *var)
  161 {
  162    if (!var ||
  163        (!var->is_in_shader_storage_block() &&
  164         var->data.mode != ir_var_shader_shared)) {
  165       _mesa_glsl_error(loc, state, "First argument to atomic function "
  166                        "must be a buffer or shared variable");
  167       return false;
  168    }
  169    return true;
  170 }
  171 
  172 static bool
  173 is_atomic_function(const char *func_name)
  174 {
  175    return !strcmp(func_name, "atomicAdd") ||
  176           !strcmp(func_name, "atomicMin") ||
  177           !strcmp(func_name, "atomicMax") ||
  178           !strcmp(func_name, "atomicAnd") ||
  179           !strcmp(func_name, "atomicOr") ||
  180           !strcmp(func_name, "atomicXor") ||
  181           !strcmp(func_name, "atomicExchange") ||
  182           !strcmp(func_name, "atomicCompSwap");
  183 }
  184 
  185 static bool
  186 verify_atomic_image_parameter_qualifier(YYLTYPE *loc, _mesa_glsl_parse_state *state,
  187                                         ir_variable *var)
  188 {
  189    if (!var ||
  190        (var->data.image_format != PIPE_FORMAT_R32_UINT &&
  191         var->data.image_format != PIPE_FORMAT_R32_SINT &&
  192         var->data.image_format != PIPE_FORMAT_R32_FLOAT)) {
  193       _mesa_glsl_error(loc, state, "Image atomic functions should use r32i/r32ui "
  194                        "format qualifier");
  195       return false;
  196    }
  197    return true;
  198 }
  199 
  200 static bool
  201 is_atomic_image_function(const char *func_name)
  202 {
  203    return !strcmp(func_name, "imageAtomicAdd") ||
  204           !strcmp(func_name, "imageAtomicMin") ||
  205           !strcmp(func_name, "imageAtomicMax") ||
  206           !strcmp(func_name, "imageAtomicAnd") ||
  207           !strcmp(func_name, "imageAtomicOr") ||
  208           !strcmp(func_name, "imageAtomicXor") ||
  209           !strcmp(func_name, "imageAtomicExchange") ||
  210           !strcmp(func_name, "imageAtomicCompSwap") ||
  211           !strcmp(func_name, "imageAtomicIncWrap") ||
  212           !strcmp(func_name, "imageAtomicDecWrap");
  213 }
  214 
  215 
  216 /**
  217  * Verify that 'out' and 'inout' actual parameters are lvalues.  Also, verify
  218  * that 'const_in' formal parameters (an extension in our IR) correspond to
  219  * ir_constant actual parameters.
  220  */
  221 static bool
  222 verify_parameter_modes(_mesa_glsl_parse_state *state,
  223                        ir_function_signature *sig,
  224                        exec_list &actual_ir_parameters,
  225                        exec_list &actual_ast_parameters)
  226 {
  227    exec_node *actual_ir_node  = actual_ir_parameters.get_head_raw();
  228    exec_node *actual_ast_node = actual_ast_parameters.get_head_raw();
  229 
  230    foreach_in_list(const ir_variable, formal, &sig->parameters) {
  231       /* The lists must be the same length. */
  232       assert(!actual_ir_node->is_tail_sentinel());
  233       assert(!actual_ast_node->is_tail_sentinel());
  234 
  235       const ir_rvalue *const actual = (ir_rvalue *) actual_ir_node;
  236       const ast_expression *const actual_ast =
  237          exec_node_data(ast_expression, actual_ast_node, link);
  238 
  239       /* FIXME: 'loc' is incorrect (as of 2011-01-21). It is always
  240        * FIXME: 0:0(0).
  241        */
  242       YYLTYPE loc = actual_ast->get_location();
  243 
  244       /* Verify that 'const_in' parameters are ir_constants. */
  245       if (formal->data.mode == ir_var_const_in &&
  246           actual->ir_type != ir_type_constant) {
  247          _mesa_glsl_error(&loc, state,
  248                           "parameter `in %s' must be a constant expression",
  249                           formal->name);
  250          return false;
  251       }
  252 
  253       /* Verify that shader_in parameters are shader inputs */
  254       if (formal->data.must_be_shader_input) {
  255          const ir_rvalue *val = actual;
  256 
  257          /* GLSL 4.40 allows swizzles, while earlier GLSL versions do not. */
  258          if (val->ir_type == ir_type_swizzle) {
  259             if (!state->is_version(440, 0)) {
  260                _mesa_glsl_error(&loc, state,
  261                                 "parameter `%s` must not be swizzled",
  262                                 formal->name);
  263                return false;
  264             }
  265             val = ((ir_swizzle *)val)->val;
  266          }
  267 
  268          for (;;) {
  269             if (val->ir_type == ir_type_dereference_array) {
  270                val = ((ir_dereference_array *)val)->array;
  271             } else if (val->ir_type == ir_type_dereference_record &&
  272                        !state->es_shader) {
  273                val = ((ir_dereference_record *)val)->record;
  274             } else
  275                break;
  276          }
  277 
  278          ir_variable *var = NULL;
  279          if (const ir_dereference_variable *deref_var = val->as_dereference_variable())
  280             var = deref_var->variable_referenced();
  281 
  282          if (!var || var->data.mode != ir_var_shader_in) {
  283             _mesa_glsl_error(&loc, state,
  284                              "parameter `%s` must be a shader input",
  285                              formal->name);
  286             return false;
  287          }
  288 
  289          var->data.must_be_shader_input = 1;
  290       }
  291 
  292       /* Verify that 'out' and 'inout' actual parameters are lvalues. */
  293       if (formal->data.mode == ir_var_function_out
  294           || formal->data.mode == ir_var_function_inout) {
  295          const char *mode = NULL;
  296          switch (formal->data.mode) {
  297          case ir_var_function_out:   mode = "out";   break;
  298          case ir_var_function_inout: mode = "inout"; break;
  299          default:                    assert(false);  break;
  300          }
  301 
  302          /* This AST-based check catches errors like f(i++).  The IR-based
  303           * is_lvalue() is insufficient because the actual parameter at the
  304           * IR-level is just a temporary value, which is an l-value.
  305           */
  306          if (actual_ast->non_lvalue_description != NULL) {
  307             _mesa_glsl_error(&loc, state,
  308                              "function parameter '%s %s' references a %s",
  309                              mode, formal->name,
  310                              actual_ast->non_lvalue_description);
  311             return false;
  312          }
  313 
  314          ir_variable *var = actual->variable_referenced();
  315 
  316          if (var && formal->data.mode == ir_var_function_inout) {
  317             if ((var->data.mode == ir_var_auto ||
  318                  var->data.mode == ir_var_shader_out) &&
  319                 !var->data.assigned &&
  320                 !is_gl_identifier(var->name)) {
  321                _mesa_glsl_warning(&loc, state, "`%s' used uninitialized",
  322                                   var->name);
  323             }
  324          }
  325 
  326          if (var)
  327             var->data.assigned = true;
  328 
  329          if (var && var->data.read_only) {
  330             _mesa_glsl_error(&loc, state,
  331                              "function parameter '%s %s' references the "
  332                              "read-only variable '%s'",
  333                              mode, formal->name,
  334                              actual->variable_referenced()->name);
  335             return false;
  336          } else if (!actual->is_lvalue(state)) {
  337             _mesa_glsl_error(&loc, state,
  338                              "function parameter '%s %s' is not an lvalue",
  339                              mode, formal->name);
  340             return false;
  341          }
  342       } else {
  343          assert(formal->data.mode == ir_var_function_in ||
  344                 formal->data.mode == ir_var_const_in);
  345          ir_variable *var = actual->variable_referenced();
  346          if (var) {
  347             if ((var->data.mode == ir_var_auto ||
  348                  var->data.mode == ir_var_shader_out) &&
  349                 !var->data.assigned &&
  350                 !is_gl_identifier(var->name)) {
  351                _mesa_glsl_warning(&loc, state, "`%s' used uninitialized",
  352                                   var->name);
  353             }
  354          }
  355       }
  356 
  357       if (formal->type->is_image() &&
  358           actual->variable_referenced()) {
  359          if (!verify_image_parameter(&loc, state, formal,
  360                                      actual->variable_referenced()))
  361             return false;
  362       }
  363 
  364       actual_ir_node  = actual_ir_node->next;
  365       actual_ast_node = actual_ast_node->next;
  366    }
  367 
  368    /* The first parameter of atomic functions must be a buffer variable */
  369    const char *func_name = sig->function_name();
  370    bool is_atomic = is_atomic_function(func_name);
  371    if (is_atomic) {
  372       const ir_rvalue *const actual =
  373          (ir_rvalue *) actual_ir_parameters.get_head_raw();
  374 
  375       const ast_expression *const actual_ast =
  376          exec_node_data(ast_expression,
  377                         actual_ast_parameters.get_head_raw(), link);
  378       YYLTYPE loc = actual_ast->get_location();
  379 
  380       if (!verify_first_atomic_parameter(&loc, state,
  381                                          actual->variable_referenced())) {
  382          return false;
  383       }
  384    } else if (is_atomic_image_function(func_name)) {
  385       const ir_rvalue *const actual =
  386          (ir_rvalue *) actual_ir_parameters.get_head_raw();
  387 
  388       const ast_expression *const actual_ast =
  389          exec_node_data(ast_expression,
  390                         actual_ast_parameters.get_head_raw(), link);
  391       YYLTYPE loc = actual_ast->get_location();
  392 
  393       if (!verify_atomic_image_parameter_qualifier(&loc, state,
  394                                          actual->variable_referenced())) {
  395          return false;
  396       }
  397    }
  398 
  399    return true;
  400 }
  401 
  402 struct copy_index_deref_data {
  403    void *mem_ctx;
  404    exec_list *before_instructions;
  405 };
  406 
  407 static void
  408 copy_index_derefs_to_temps(ir_instruction *ir, void *data)
  409 {
  410    struct copy_index_deref_data *d = (struct copy_index_deref_data *)data;
  411 
  412    if (ir->ir_type == ir_type_dereference_array) {
  413       ir_dereference_array *a = (ir_dereference_array *) ir;
  414       ir = a->array->as_dereference();
  415 
  416       ir_rvalue *idx = a->array_index;
  417       ir_variable *var = idx->variable_referenced();
  418 
  419       /* If the index is read only it cannot change so there is no need
  420        * to copy it.
  421        */
  422       if (!var || var->data.read_only || var->data.memory_read_only)
  423          return;
  424 
  425       ir_variable *tmp = new(d->mem_ctx) ir_variable(idx->type, "idx_tmp",
  426                                                       ir_var_temporary);
  427       d->before_instructions->push_tail(tmp);
  428 
  429       ir_dereference_variable *const deref_tmp_1 =
  430          new(d->mem_ctx) ir_dereference_variable(tmp);
  431       ir_assignment *const assignment =
  432          new(d->mem_ctx) ir_assignment(deref_tmp_1,
  433                                        idx->clone(d->mem_ctx, NULL));
  434       d->before_instructions->push_tail(assignment);
  435 
  436       /* Replace the array index with a dereference of the new temporary */
  437       ir_dereference_variable *const deref_tmp_2 =
  438          new(d->mem_ctx) ir_dereference_variable(tmp);
  439       a->array_index = deref_tmp_2;
  440    }
  441 }
  442 
  443 static void
  444 fix_parameter(void *mem_ctx, ir_rvalue *actual, const glsl_type *formal_type,
  445               exec_list *before_instructions, exec_list *after_instructions,
  446               bool parameter_is_inout)
  447 {
  448    ir_expression *const expr = actual->as_expression();
  449 
  450    /* If the types match exactly and the parameter is not a vector-extract,
  451     * nothing needs to be done to fix the parameter.
  452     */
  453    if (formal_type == actual->type
  454        && (expr == NULL || expr->operation != ir_binop_vector_extract)
  455        && actual->as_dereference_variable())
  456       return;
  457 
  458    /* An array index could also be an out variable so we need to make a copy
  459     * of them before the function is called.
  460     */
  461    if (!actual->as_dereference_variable()) {
  462       struct copy_index_deref_data data;
  463       data.mem_ctx = mem_ctx;
  464       data.before_instructions = before_instructions;
  465 
  466       visit_tree(actual, copy_index_derefs_to_temps, &data);
  467    }
  468 
  469    /* To convert an out parameter, we need to create a temporary variable to
  470     * hold the value before conversion, and then perform the conversion after
  471     * the function call returns.
  472     *
  473     * This has the effect of transforming code like this:
  474     *
  475     *   void f(out int x);
  476     *   float value;
  477     *   f(value);
  478     *
  479     * Into IR that's equivalent to this:
  480     *
  481     *   void f(out int x);
  482     *   float value;
  483     *   int out_parameter_conversion;
  484     *   f(out_parameter_conversion);
  485     *   value = float(out_parameter_conversion);
  486     *
  487     * If the parameter is an ir_expression of ir_binop_vector_extract,
  488     * additional conversion is needed in the post-call re-write.
  489     */
  490    ir_variable *tmp =
  491       new(mem_ctx) ir_variable(formal_type, "inout_tmp", ir_var_temporary);
  492 
  493    before_instructions->push_tail(tmp);
  494 
  495    /* If the parameter is an inout parameter, copy the value of the actual
  496     * parameter to the new temporary.  Note that no type conversion is allowed
  497     * here because inout parameters must match types exactly.
  498     */
  499    if (parameter_is_inout) {
  500       /* Inout parameters should never require conversion, since that would
  501        * require an implicit conversion to exist both to and from the formal
  502        * parameter type, and there are no bidirectional implicit conversions.
  503        */
  504       assert (actual->type == formal_type);
  505 
  506       ir_dereference_variable *const deref_tmp_1 =
  507          new(mem_ctx) ir_dereference_variable(tmp);
  508       ir_assignment *const assignment =
  509          new(mem_ctx) ir_assignment(deref_tmp_1, actual->clone(mem_ctx, NULL));
  510       before_instructions->push_tail(assignment);
  511    }
  512 
  513    /* Replace the parameter in the call with a dereference of the new
  514     * temporary.
  515     */
  516    ir_dereference_variable *const deref_tmp_2 =
  517       new(mem_ctx) ir_dereference_variable(tmp);
  518    actual->replace_with(deref_tmp_2);
  519 
  520 
  521    /* Copy the temporary variable to the actual parameter with optional
  522     * type conversion applied.
  523     */
  524    ir_rvalue *rhs = new(mem_ctx) ir_dereference_variable(tmp);
  525    if (actual->type != formal_type)
  526       rhs = convert_component(rhs, actual->type);
  527 
  528    ir_rvalue *lhs = actual;
  529    if (expr != NULL && expr->operation == ir_binop_vector_extract) {
  530       lhs = new(mem_ctx) ir_dereference_array(expr->operands[0]->clone(mem_ctx,
  531                                                                        NULL),
  532                                               expr->operands[1]->clone(mem_ctx,
  533                                                                        NULL));
  534    }
  535 
  536    ir_assignment *const assignment_2 = new(mem_ctx) ir_assignment(lhs, rhs);
  537    after_instructions->push_tail(assignment_2);
  538 }
  539 
  540 /**
  541  * Generate a function call.
  542  *
  543  * For non-void functions, this returns a dereference of the temporary
  544  * variable which stores the return value for the call.  For void functions,
  545  * this returns NULL.
  546  */
  547 static ir_rvalue *
  548 generate_call(exec_list *instructions, ir_function_signature *sig,
  549               exec_list *actual_parameters,
  550               ir_variable *sub_var,
  551               ir_rvalue *array_idx,
  552               struct _mesa_glsl_parse_state *state)
  553 {
  554    void *ctx = state;
  555    exec_list post_call_conversions;
  556 
  557    /* Perform implicit conversion of arguments.  For out parameters, we need
  558     * to place them in a temporary variable and do the conversion after the
  559     * call takes place.  Since we haven't emitted the call yet, we'll place
  560     * the post-call conversions in a temporary exec_list, and emit them later.
  561     */
  562    foreach_two_lists(formal_node, &sig->parameters,
  563                      actual_node, actual_parameters) {
  564       ir_rvalue *actual = (ir_rvalue *) actual_node;
  565       ir_variable *formal = (ir_variable *) formal_node;
  566 
  567       if (formal->type->is_numeric() || formal->type->is_boolean()) {
  568          switch (formal->data.mode) {
  569          case ir_var_const_in:
  570          case ir_var_function_in: {
  571             ir_rvalue *converted
  572                = convert_component(actual, formal->type);
  573             actual->replace_with(converted);
  574             break;
  575          }
  576          case ir_var_function_out:
  577          case ir_var_function_inout:
  578             fix_parameter(ctx, actual, formal->type,
  579                           instructions, &post_call_conversions,
  580                           formal->data.mode == ir_var_function_inout);
  581             break;
  582          default:
  583             assert (!"Illegal formal parameter mode");
  584             break;
  585          }
  586       }
  587    }
  588 
  589    /* Section 4.3.2 (Const) of the GLSL 1.10.59 spec says:
  590     *
  591     *     "Initializers for const declarations must be formed from literal
  592     *     values, other const variables (not including function call
  593     *     paramaters), or expressions of these.
  594     *
  595     *     Constructors may be used in such expressions, but function calls may
  596     *     not."
  597     *
  598     * Section 4.3.3 (Constant Expressions) of the GLSL 1.20.8 spec says:
  599     *
  600     *     "A constant expression is one of
  601     *
  602     *         ...
  603     *
  604     *         - a built-in function call whose arguments are all constant
  605     *           expressions, with the exception of the texture lookup
  606     *           functions, the noise functions, and ftransform. The built-in
  607     *           functions dFdx, dFdy, and fwidth must return 0 when evaluated
  608     *           inside an initializer with an argument that is a constant
  609     *           expression."
  610     *
  611     * Section 5.10 (Constant Expressions) of the GLSL ES 1.00.17 spec says:
  612     *
  613     *     "A constant expression is one of
  614     *
  615     *         ...
  616     *
  617     *         - a built-in function call whose arguments are all constant
  618     *           expressions, with the exception of the texture lookup
  619     *           functions."
  620     *
  621     * Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec says:
  622     *
  623     *     "A constant expression is one of
  624     *
  625     *         ...
  626     *
  627     *         - a built-in function call whose arguments are all constant
  628     *           expressions, with the exception of the texture lookup
  629     *           functions.  The built-in functions dFdx, dFdy, and fwidth must
  630     *           return 0 when evaluated inside an initializer with an argument
  631     *           that is a constant expression."
  632     *
  633     * If the function call is a constant expression, don't generate any
  634     * instructions; just generate an ir_constant.
  635     */
  636    if (state->is_version(120, 100) ||
  637        state->ctx->Const.AllowGLSLBuiltinConstantExpression) {
  638       ir_constant *value = sig->constant_expression_value(ctx,
  639                                                           actual_parameters,
  640                                                           NULL);
  641       if (value != NULL) {
  642          return value;
  643       }
  644    }
  645 
  646    ir_dereference_variable *deref = NULL;
  647    if (!sig->return_type->is_void()) {
  648       /* Create a new temporary to hold the return value. */
  649       char *const name = ir_variable::temporaries_allocate_names
  650          ? ralloc_asprintf(ctx, "%s_retval", sig->function_name())
  651          : NULL;
  652 
  653       ir_variable *var;
  654 
  655       var = new(ctx) ir_variable(sig->return_type, name, ir_var_temporary);
  656       instructions->push_tail(var);
  657 
  658       ralloc_free(name);
  659 
  660       deref = new(ctx) ir_dereference_variable(var);
  661    }
  662 
  663    ir_call *call = new(ctx) ir_call(sig, deref,
  664                                     actual_parameters, sub_var, array_idx);
  665    instructions->push_tail(call);
  666 
  667    /* Also emit any necessary out-parameter conversions. */
  668    instructions->append_list(&post_call_conversions);
  669 
  670    return deref ? deref->clone(ctx, NULL) : NULL;
  671 }
  672 
  673 /**
  674  * Given a function name and parameter list, find the matching signature.
  675  */
  676 static ir_function_signature *
  677 match_function_by_name(const char *name,
  678                        exec_list *actual_parameters,
  679                        struct _mesa_glsl_parse_state *state)
  680 {
  681    ir_function *f = state->symbols->get_function(name);
  682    ir_function_signature *local_sig = NULL;
  683    ir_function_signature *sig = NULL;
  684 
  685    /* Is the function hidden by a record type constructor? */
  686    if (state->symbols->get_type(name))
  687       return sig; /* no match */
  688 
  689    /* Is the function hidden by a variable (impossible in 1.10)? */
  690    if (!state->symbols->separate_function_namespace
  691        && state->symbols->get_variable(name))
  692       return sig; /* no match */
  693 
  694    if (f != NULL) {
  695       /* In desktop GL, the presence of a user-defined signature hides any
  696        * built-in signatures, so we must ignore them.  In contrast, in ES2
  697        * user-defined signatures add new overloads, so we must consider them.
  698        */
  699       bool allow_builtins = state->es_shader || !f->has_user_signature();
  700 
  701       /* Look for a match in the local shader.  If exact, we're done. */
  702       bool is_exact = false;
  703       sig = local_sig = f->matching_signature(state, actual_parameters,
  704                                               allow_builtins, &is_exact);
  705       if (is_exact)
  706          return sig;
  707 
  708       if (!allow_builtins)
  709          return sig;
  710    }
  711 
  712    /* Local shader has no exact candidates; check the built-ins. */
  713    sig = _mesa_glsl_find_builtin_function(state, name, actual_parameters);
  714 
  715    /* if _mesa_glsl_find_builtin_function failed, fall back to the result
  716     * of choose_best_inexact_overload() instead. This should only affect
  717     * GLES.
  718     */
  719    return sig ? sig : local_sig;
  720 }
  721 
  722 static ir_function_signature *
  723 match_subroutine_by_name(const char *name,
  724                          exec_list *actual_parameters,
  725                          struct _mesa_glsl_parse_state *state,
  726                          ir_variable **var_r)
  727 {
  728    void *ctx = state;
  729    ir_function_signature *sig = NULL;
  730    ir_function *f, *found = NULL;
  731    const char *new_name;
  732    ir_variable *var;
  733    bool is_exact = false;
  734 
  735    new_name =
  736       ralloc_asprintf(ctx, "%s_%s",
  737                       _mesa_shader_stage_to_subroutine_prefix(state->stage),
  738                       name);
  739    var = state->symbols->get_variable(new_name);
  740    if (!var)
  741       return NULL;
  742 
  743    for (int i = 0; i < state->num_subroutine_types; i++) {
  744       f = state->subroutine_types[i];
  745       if (strcmp(f->name, var->type->without_array()->name))
  746          continue;
  747       found = f;
  748       break;
  749    }
  750 
  751    if (!found)
  752       return NULL;
  753    *var_r = var;
  754    sig = found->matching_signature(state, actual_parameters,
  755                                    false, &is_exact);
  756    return sig;
  757 }
  758 
  759 static ir_rvalue *
  760 generate_array_index(void *mem_ctx, exec_list *instructions,
  761                      struct _mesa_glsl_parse_state *state, YYLTYPE loc,
  762                      const ast_expression *array, ast_expression *idx,
  763                      const char **function_name, exec_list *actual_parameters)
  764 {
  765    if (array->oper == ast_array_index) {
  766       /* This handles arrays of arrays */
  767       ir_rvalue *outer_array = generate_array_index(mem_ctx, instructions,
  768                                                     state, loc,
  769                                                     array->subexpressions[0],
  770                                                     array->subexpressions[1],
  771                                                     function_name,
  772                                                     actual_parameters);
  773       ir_rvalue *outer_array_idx = idx->hir(instructions, state);
  774 
  775       YYLTYPE index_loc = idx->get_location();
  776       return _mesa_ast_array_index_to_hir(mem_ctx, state, outer_array,
  777                                           outer_array_idx, loc,
  778                                           index_loc);
  779    } else {
  780       ir_variable *sub_var = NULL;
  781       *function_name = array->primary_expression.identifier;
  782 
  783       if (!match_subroutine_by_name(*function_name, actual_parameters,
  784                                     state, &sub_var)) {
  785          _mesa_glsl_error(&loc, state, "Unknown subroutine `%s'",
  786                           *function_name);
  787          *function_name = NULL; /* indicate error condition to caller */
  788          return NULL;
  789       }
  790 
  791       ir_rvalue *outer_array_idx = idx->hir(instructions, state);
  792       return new(mem_ctx) ir_dereference_array(sub_var, outer_array_idx);
  793    }
  794 }
  795 
  796 static bool
  797 function_exists(_mesa_glsl_parse_state *state,
  798                 struct glsl_symbol_table *symbols, const char *name)
  799 {
  800    ir_function *f = symbols->get_function(name);
  801    if (f != NULL) {
  802       foreach_in_list(ir_function_signature, sig, &f->signatures) {
  803          if (sig->is_builtin() && !sig->is_builtin_available(state))
  804             continue;
  805          return true;
  806       }
  807    }
  808    return false;
  809 }
  810 
  811 static void
  812 print_function_prototypes(_mesa_glsl_parse_state *state, YYLTYPE *loc,
  813                           ir_function *f)
  814 {
  815    if (f == NULL)
  816       return;
  817 
  818    foreach_in_list(ir_function_signature, sig, &f->signatures) {
  819       if (sig->is_builtin() && !sig->is_builtin_available(state))
  820          continue;
  821 
  822       char *str = prototype_string(sig->return_type, f->name,
  823                                    &sig->parameters);
  824       _mesa_glsl_error(loc, state, "   %s", str);
  825       ralloc_free(str);
  826    }
  827 }
  828 
  829 /**
  830  * Raise a "no matching function" error, listing all possible overloads the
  831  * compiler considered so developers can figure out what went wrong.
  832  */
  833 static void
  834 no_matching_function_error(const char *name,
  835                            YYLTYPE *loc,
  836                            exec_list *actual_parameters,
  837                            _mesa_glsl_parse_state *state)
  838 {
  839    gl_shader *sh = _mesa_glsl_get_builtin_function_shader();
  840 
  841    if (!function_exists(state, state->symbols, name)
  842        && (!state->uses_builtin_functions
  843            || !function_exists(state, sh->symbols, name))) {
  844       _mesa_glsl_error(loc, state, "no function with name '%s'", name);
  845    } else {
  846       char *str = prototype_string(NULL, name, actual_parameters);
  847       _mesa_glsl_error(loc, state,
  848                        "no matching function for call to `%s';"
  849                        " candidates are:",
  850                        str);
  851       ralloc_free(str);
  852 
  853       print_function_prototypes(state, loc,
  854                                 state->symbols->get_function(name));
  855 
  856       if (state->uses_builtin_functions) {
  857          print_function_prototypes(state, loc,
  858                                    sh->symbols->get_function(name));
  859       }
  860    }
  861 }
  862 
  863 /**
  864  * Perform automatic type conversion of constructor parameters
  865  *
  866  * This implements the rules in the "Conversion and Scalar Constructors"
  867  * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
  868  */
  869 static ir_rvalue *
  870 convert_component(ir_rvalue *src, const glsl_type *desired_type)
  871 {
  872    void *ctx = ralloc_parent(src);
  873    const unsigned a = desired_type->base_type;
  874    const unsigned b = src->type->base_type;
  875    ir_expression *result = NULL;
  876 
  877    if (src->type->is_error())
  878       return src;
  879 
  880    assert(a <= GLSL_TYPE_IMAGE);
  881    assert(b <= GLSL_TYPE_IMAGE);
  882 
  883    if (a == b)
  884       return src;
  885 
  886    switch (a) {
  887    case GLSL_TYPE_UINT:
  888       switch (b) {
  889       case GLSL_TYPE_INT:
  890          result = new(ctx) ir_expression(ir_unop_i2u, src);
  891          break;
  892       case GLSL_TYPE_FLOAT:
  893          result = new(ctx) ir_expression(ir_unop_f2u, src);
  894          break;
  895       case GLSL_TYPE_BOOL:
  896          result = new(ctx) ir_expression(ir_unop_i2u,
  897                                          new(ctx) ir_expression(ir_unop_b2i,
  898                                                                 src));
  899          break;
  900       case GLSL_TYPE_DOUBLE:
  901          result = new(ctx) ir_expression(ir_unop_d2u, src);
  902          break;
  903       case GLSL_TYPE_UINT64:
  904          result = new(ctx) ir_expression(ir_unop_u642u, src);
  905          break;
  906       case GLSL_TYPE_INT64:
  907          result = new(ctx) ir_expression(ir_unop_i642u, src);
  908          break;
  909       case GLSL_TYPE_SAMPLER:
  910          result = new(ctx) ir_expression(ir_unop_unpack_sampler_2x32, src);
  911          break;
  912       case GLSL_TYPE_IMAGE:
  913          result = new(ctx) ir_expression(ir_unop_unpack_image_2x32, src);
  914          break;
  915       }
  916       break;
  917    case GLSL_TYPE_INT:
  918       switch (b) {
  919       case GLSL_TYPE_UINT:
  920          result = new(ctx) ir_expression(ir_unop_u2i, src);
  921          break;
  922       case GLSL_TYPE_FLOAT:
  923          result = new(ctx) ir_expression(ir_unop_f2i, src);
  924          break;
  925       case GLSL_TYPE_BOOL:
  926          result = new(ctx) ir_expression(ir_unop_b2i, src);
  927          break;
  928       case GLSL_TYPE_DOUBLE:
  929          result = new(ctx) ir_expression(ir_unop_d2i, src);
  930          break;
  931       case GLSL_TYPE_UINT64:
  932          result = new(ctx) ir_expression(ir_unop_u642i, src);
  933          break;
  934       case GLSL_TYPE_INT64:
  935          result = new(ctx) ir_expression(ir_unop_i642i, src);
  936          break;
  937       }
  938       break;
  939    case GLSL_TYPE_FLOAT:
  940       switch (b) {
  941       case GLSL_TYPE_UINT:
  942          result = new(ctx) ir_expression(ir_unop_u2f, desired_type, src, NULL);
  943          break;
  944       case GLSL_TYPE_INT:
  945          result = new(ctx) ir_expression(ir_unop_i2f, desired_type, src, NULL);
  946          break;
  947       case GLSL_TYPE_BOOL:
  948          result = new(ctx) ir_expression(ir_unop_b2f, desired_type, src, NULL);
  949          break;
  950       case GLSL_TYPE_DOUBLE:
  951          result = new(ctx) ir_expression(ir_unop_d2f, desired_type, src, NULL);
  952          break;
  953       case GLSL_TYPE_UINT64:
  954          result = new(ctx) ir_expression(ir_unop_u642f, desired_type, src, NULL);
  955          break;
  956       case GLSL_TYPE_INT64:
  957          result = new(ctx) ir_expression(ir_unop_i642f, desired_type, src, NULL);
  958          break;
  959       }
  960       break;
  961    case GLSL_TYPE_BOOL:
  962       switch (b) {
  963       case GLSL_TYPE_UINT:
  964          result = new(ctx) ir_expression(ir_unop_i2b,
  965                                          new(ctx) ir_expression(ir_unop_u2i,
  966                                                                 src));
  967          break;
  968       case GLSL_TYPE_INT:
  969          result = new(ctx) ir_expression(ir_unop_i2b, desired_type, src, NULL);
  970          break;
  971       case GLSL_TYPE_FLOAT:
  972          result = new(ctx) ir_expression(ir_unop_f2b, desired_type, src, NULL);
  973          break;
  974       case GLSL_TYPE_DOUBLE:
  975          result = new(ctx) ir_expression(ir_unop_d2b, desired_type, src, NULL);
  976          break;
  977       case GLSL_TYPE_UINT64:
  978          result = new(ctx) ir_expression(ir_unop_i642b,
  979                                          new(ctx) ir_expression(ir_unop_u642i64,
  980                                                                 src));
  981          break;
  982       case GLSL_TYPE_INT64:
  983          result = new(ctx) ir_expression(ir_unop_i642b, desired_type, src, NULL);
  984          break;
  985       }
  986       break;
  987    case GLSL_TYPE_DOUBLE:
  988       switch (b) {
  989       case GLSL_TYPE_INT:
  990          result = new(ctx) ir_expression(ir_unop_i2d, src);
  991          break;
  992       case GLSL_TYPE_UINT:
  993          result = new(ctx) ir_expression(ir_unop_u2d, src);
  994          break;
  995       case GLSL_TYPE_BOOL:
  996          result = new(ctx) ir_expression(ir_unop_f2d,
  997                                          new(ctx) ir_expression(ir_unop_b2f,
  998                                                                 src));
  999          break;
 1000       case GLSL_TYPE_FLOAT:
 1001          result = new(ctx) ir_expression(ir_unop_f2d, desired_type, src, NULL);
 1002          break;
 1003       case GLSL_TYPE_UINT64:
 1004          result = new(ctx) ir_expression(ir_unop_u642d, desired_type, src, NULL);
 1005          break;
 1006       case GLSL_TYPE_INT64:
 1007          result = new(ctx) ir_expression(ir_unop_i642d, desired_type, src, NULL);
 1008          break;
 1009       }
 1010       break;
 1011    case GLSL_TYPE_UINT64:
 1012       switch (b) {
 1013       case GLSL_TYPE_INT:
 1014          result = new(ctx) ir_expression(ir_unop_i2u64, src);
 1015          break;
 1016       case GLSL_TYPE_UINT:
 1017          result = new(ctx) ir_expression(ir_unop_u2u64, src);
 1018          break;
 1019       case GLSL_TYPE_BOOL:
 1020          result = new(ctx) ir_expression(ir_unop_i642u64,
 1021                                          new(ctx) ir_expression(ir_unop_b2i64,
 1022                                                                 src));
 1023          break;
 1024       case GLSL_TYPE_FLOAT:
 1025          result = new(ctx) ir_expression(ir_unop_f2u64, src);
 1026          break;
 1027       case GLSL_TYPE_DOUBLE:
 1028          result = new(ctx) ir_expression(ir_unop_d2u64, src);
 1029          break;
 1030       case GLSL_TYPE_INT64:
 1031          result = new(ctx) ir_expression(ir_unop_i642u64, src);
 1032          break;
 1033       }
 1034       break;
 1035    case GLSL_TYPE_INT64:
 1036       switch (b) {
 1037       case GLSL_TYPE_INT:
 1038          result = new(ctx) ir_expression(ir_unop_i2i64, src);
 1039          break;
 1040       case GLSL_TYPE_UINT:
 1041          result = new(ctx) ir_expression(ir_unop_u2i64, src);
 1042          break;
 1043       case GLSL_TYPE_BOOL:
 1044          result = new(ctx) ir_expression(ir_unop_b2i64, src);
 1045          break;
 1046       case GLSL_TYPE_FLOAT:
 1047          result = new(ctx) ir_expression(ir_unop_f2i64, src);
 1048          break;
 1049       case GLSL_TYPE_DOUBLE:
 1050          result = new(ctx) ir_expression(ir_unop_d2i64, src);
 1051          break;
 1052       case GLSL_TYPE_UINT64:
 1053          result = new(ctx) ir_expression(ir_unop_u642i64, src);
 1054          break;
 1055       }
 1056       break;
 1057    case GLSL_TYPE_SAMPLER:
 1058       switch (b) {
 1059       case GLSL_TYPE_UINT:
 1060          result = new(ctx)
 1061             ir_expression(ir_unop_pack_sampler_2x32, desired_type, src);
 1062          break;
 1063       }
 1064       break;
 1065    case GLSL_TYPE_IMAGE:
 1066       switch (b) {
 1067       case GLSL_TYPE_UINT:
 1068          result = new(ctx)
 1069             ir_expression(ir_unop_pack_image_2x32, desired_type, src);
 1070          break;
 1071       }
 1072       break;
 1073    }
 1074 
 1075    assert(result != NULL);
 1076    assert(result->type == desired_type);
 1077 
 1078    /* Try constant folding; it may fold in the conversion we just added. */
 1079    ir_constant *const constant = result->constant_expression_value(ctx);
 1080    return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result;
 1081 }
 1082 
 1083 
 1084 /**
 1085  * Perform automatic type and constant conversion of constructor parameters
 1086  *
 1087  * This implements the rules in the "Implicit Conversions" rules, not the
 1088  * "Conversion and Scalar Constructors".
 1089  *
 1090  * After attempting the implicit conversion, an attempt to convert into a
 1091  * constant valued expression is also done.
 1092  *
 1093  * The \c from \c ir_rvalue is converted "in place".
 1094  *
 1095  * \param from   Operand that is being converted
 1096  * \param to     Base type the operand will be converted to
 1097  * \param state  GLSL compiler state
 1098  *
 1099  * \return
 1100  * If the attempt to convert into a constant expression succeeds, \c true is
 1101  * returned. Otherwise \c false is returned.
 1102  */
 1103 static bool
 1104 implicitly_convert_component(ir_rvalue * &from, const glsl_base_type to,
 1105                              struct _mesa_glsl_parse_state *state)
 1106 {
 1107    void *mem_ctx = state;
 1108    ir_rvalue *result = from;
 1109 
 1110    if (to != from->type->base_type) {
 1111       const glsl_type *desired_type =
 1112          glsl_type::get_instance(to,
 1113                                  from->type->vector_elements,
 1114                                  from->type->matrix_columns);
 1115 
 1116       if (from->type->can_implicitly_convert_to(desired_type, state)) {
 1117          /* Even though convert_component() implements the constructor
 1118           * conversion rules (not the implicit conversion rules), its safe
 1119           * to use it here because we already checked that the implicit
 1120           * conversion is legal.
 1121           */
 1122          result = convert_component(from, desired_type);
 1123       }
 1124    }
 1125 
 1126    ir_rvalue *const constant = result->constant_expression_value(mem_ctx);
 1127 
 1128    if (constant != NULL)
 1129       result = constant;
 1130 
 1131    if (from != result) {
 1132       from->replace_with(result);
 1133       from = result;
 1134    }
 1135 
 1136    return constant != NULL;
 1137 }
 1138 
 1139 
 1140 /**
 1141  * Dereference a specific component from a scalar, vector, or matrix
 1142  */
 1143 static ir_rvalue *
 1144 dereference_component(ir_rvalue *src, unsigned component)
 1145 {
 1146    void *ctx = ralloc_parent(src);
 1147    assert(component < src->type->components());
 1148 
 1149    /* If the source is a constant, just create a new constant instead of a
 1150     * dereference of the existing constant.
 1151     */
 1152    ir_constant *constant = src->as_constant();
 1153    if (constant)
 1154       return new(ctx) ir_constant(constant, component);
 1155 
 1156    if (src->type->is_scalar()) {
 1157       return src;
 1158    } else if (src->type->is_vector()) {
 1159       return new(ctx) ir_swizzle(src, component, 0, 0, 0, 1);
 1160    } else {
 1161       assert(src->type->is_matrix());
 1162 
 1163       /* Dereference a row of the matrix, then call this function again to get
 1164        * a specific element from that row.
 1165        */
 1166       const int c = component / src->type->column_type()->vector_elements;
 1167       const int r = component % src->type->column_type()->vector_elements;
 1168       ir_constant *const col_index = new(ctx) ir_constant(c);
 1169       ir_dereference *const col = new(ctx) ir_dereference_array(src,
 1170                                                                 col_index);
 1171 
 1172       col->type = src->type->column_type();
 1173 
 1174       return dereference_component(col, r);
 1175    }
 1176 
 1177    assert(!"Should not get here.");
 1178    return NULL;
 1179 }
 1180 
 1181 
 1182 static ir_rvalue *
 1183 process_vec_mat_constructor(exec_list *instructions,
 1184                             const glsl_type *constructor_type,
 1185                             YYLTYPE *loc, exec_list *parameters,
 1186                             struct _mesa_glsl_parse_state *state)
 1187 {
 1188    void *ctx = state;
 1189 
 1190    /* The ARB_shading_language_420pack spec says:
 1191     *
 1192     * "If an initializer is a list of initializers enclosed in curly braces,
 1193     *  the variable being declared must be a vector, a matrix, an array, or a
 1194     *  structure.
 1195     *
 1196     *      int i = { 1 }; // illegal, i is not an aggregate"
 1197     */
 1198    if (constructor_type->vector_elements <= 1) {
 1199       _mesa_glsl_error(loc, state, "aggregates can only initialize vectors, "
 1200                        "matrices, arrays, and structs");
 1201       return ir_rvalue::error_value(ctx);
 1202    }
 1203 
 1204    exec_list actual_parameters;
 1205    const unsigned parameter_count =
 1206       process_parameters(instructions, &actual_parameters, parameters, state);
 1207 
 1208    if (parameter_count == 0
 1209        || (constructor_type->is_vector() &&
 1210            constructor_type->vector_elements != parameter_count)
 1211        || (constructor_type->is_matrix() &&
 1212            constructor_type->matrix_columns != parameter_count)) {
 1213       _mesa_glsl_error(loc, state, "%s constructor must have %u parameters",
 1214                        constructor_type->is_vector() ? "vector" : "matrix",
 1215                        constructor_type->vector_elements);
 1216       return ir_rvalue::error_value(ctx);
 1217    }
 1218 
 1219    bool all_parameters_are_constant = true;
 1220 
 1221    /* Type cast each parameter and, if possible, fold constants. */
 1222    foreach_in_list_safe(ir_rvalue, ir, &actual_parameters) {
 1223       /* Apply implicit conversions (not the scalar constructor rules, see the
 1224        * spec quote above!) and attempt to convert the parameter to a constant
 1225        * valued expression. After doing so, track whether or not all the
 1226        * parameters to the constructor are trivially constant valued
 1227        * expressions.
 1228        */
 1229       all_parameters_are_constant &=
 1230          implicitly_convert_component(ir, constructor_type->base_type, state);
 1231 
 1232       if (constructor_type->is_matrix()) {
 1233          if (ir->type != constructor_type->column_type()) {
 1234             _mesa_glsl_error(loc, state, "type error in matrix constructor: "
 1235                              "expected: %s, found %s",
 1236                              constructor_type->column_type()->name,
 1237                              ir->type->name);
 1238             return ir_rvalue::error_value(ctx);
 1239          }
 1240       } else if (ir->type != constructor_type->get_scalar_type()) {
 1241          _mesa_glsl_error(loc, state, "type error in vector constructor: "
 1242                           "expected: %s, found %s",
 1243                           constructor_type->get_scalar_type()->name,
 1244                           ir->type->name);
 1245          return ir_rvalue::error_value(ctx);
 1246       }
 1247    }
 1248 
 1249    if (all_parameters_are_constant)
 1250       return new(ctx) ir_constant(constructor_type, &actual_parameters);
 1251 
 1252    ir_variable *var = new(ctx) ir_variable(constructor_type, "vec_mat_ctor",
 1253                                            ir_var_temporary);
 1254    instructions->push_tail(var);
 1255 
 1256    int i = 0;
 1257 
 1258    foreach_in_list(ir_rvalue, rhs, &actual_parameters) {
 1259       ir_instruction *assignment = NULL;
 1260 
 1261       if (var->type->is_matrix()) {
 1262          ir_rvalue *lhs =
 1263             new(ctx) ir_dereference_array(var, new(ctx) ir_constant(i));
 1264          assignment = new(ctx) ir_assignment(lhs, rhs);
 1265       } else {
 1266          /* use writemask rather than index for vector */
 1267          assert(var->type->is_vector());
 1268          assert(i < 4);
 1269          ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
 1270          assignment = new(ctx) ir_assignment(lhs, rhs, NULL,
 1271                                              (unsigned)(1 << i));
 1272       }
 1273 
 1274       instructions->push_tail(assignment);
 1275 
 1276       i++;
 1277    }
 1278 
 1279    return new(ctx) ir_dereference_variable(var);
 1280 }
 1281 
 1282 
 1283 static ir_rvalue *
 1284 process_array_constructor(exec_list *instructions,
 1285                           const glsl_type *constructor_type,
 1286                           YYLTYPE *loc, exec_list *parameters,
 1287                           struct _mesa_glsl_parse_state *state)
 1288 {
 1289    void *ctx = state;
 1290    /* Array constructors come in two forms: sized and unsized.  Sized array
 1291     * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
 1292     * variables.  In this case the number of parameters must exactly match the
 1293     * specified size of the array.
 1294     *
 1295     * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
 1296     * are vec4 variables.  In this case the size of the array being constructed
 1297     * is determined by the number of parameters.
 1298     *
 1299     * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
 1300     *
 1301     *    "There must be exactly the same number of arguments as the size of
 1302     *    the array being constructed. If no size is present in the
 1303     *    constructor, then the array is explicitly sized to the number of
 1304     *    arguments provided. The arguments are assigned in order, starting at
 1305     *    element 0, to the elements of the constructed array. Each argument
 1306     *    must be the same type as the element type of the array, or be a type
 1307     *    that can be converted to the element type of the array according to
 1308     *    Section 4.1.10 "Implicit Conversions.""
 1309     */
 1310    exec_list actual_parameters;
 1311    const unsigned parameter_count =
 1312       process_parameters(instructions, &actual_parameters, parameters, state);
 1313    bool is_unsized_array = constructor_type->is_unsized_array();
 1314 
 1315    if ((parameter_count == 0) ||
 1316        (!is_unsized_array && (constructor_type->length != parameter_count))) {
 1317       const unsigned min_param = is_unsized_array
 1318          ? 1 : constructor_type->length;
 1319 
 1320       _mesa_glsl_error(loc, state, "array constructor must have %s %u "
 1321                        "parameter%s",
 1322                        is_unsized_array ? "at least" : "exactly",
 1323                        min_param, (min_param <= 1) ? "" : "s");
 1324       return ir_rvalue::error_value(ctx);
 1325    }
 1326 
 1327    if (is_unsized_array) {
 1328       constructor_type =
 1329          glsl_type::get_array_instance(constructor_type->fields.array,
 1330                                        parameter_count);
 1331       assert(constructor_type != NULL);
 1332       assert(constructor_type->length == parameter_count);
 1333    }
 1334 
 1335    bool all_parameters_are_constant = true;
 1336    const glsl_type *element_type = constructor_type->fields.array;
 1337 
 1338    /* Type cast each parameter and, if possible, fold constants. */
 1339    foreach_in_list_safe(ir_rvalue, ir, &actual_parameters) {
 1340       /* Apply implicit conversions (not the scalar constructor rules, see the
 1341        * spec quote above!) and attempt to convert the parameter to a constant
 1342        * valued expression. After doing so, track whether or not all the
 1343        * parameters to the constructor are trivially constant valued
 1344        * expressions.
 1345        */
 1346       all_parameters_are_constant &=
 1347          implicitly_convert_component(ir, element_type->base_type, state);
 1348 
 1349       if (constructor_type->fields.array->is_unsized_array()) {
 1350          /* As the inner parameters of the constructor are created without
 1351           * knowledge of each other we need to check to make sure unsized
 1352           * parameters of unsized constructors all end up with the same size.
 1353           *
 1354           * e.g we make sure to fail for a constructor like this:
 1355           * vec4[][] a = vec4[][](vec4[](vec4(0.0), vec4(1.0)),
 1356           *                       vec4[](vec4(0.0), vec4(1.0), vec4(1.0)),
 1357           *                       vec4[](vec4(0.0), vec4(1.0)));
 1358           */
 1359          if (element_type->is_unsized_array()) {
 1360             /* This is the first parameter so just get the type */
 1361             element_type = ir->type;
 1362          } else if (element_type != ir->type) {
 1363             _mesa_glsl_error(loc, state, "type error in array constructor: "
 1364                              "expected: %s, found %s",
 1365                              element_type->name,
 1366                              ir->type->name);
 1367             return ir_rvalue::error_value(ctx);
 1368          }
 1369       } else if (ir->type != constructor_type->fields.array) {
 1370          _mesa_glsl_error(loc, state, "type error in array constructor: "
 1371                           "expected: %s, found %s",
 1372                           constructor_type->fields.array->name,
 1373                           ir->type->name);
 1374          return ir_rvalue::error_value(ctx);
 1375       } else {
 1376          element_type = ir->type;
 1377       }
 1378    }
 1379 
 1380    if (constructor_type->fields.array->is_unsized_array()) {
 1381       constructor_type =
 1382          glsl_type::get_array_instance(element_type,
 1383                                        parameter_count);
 1384       assert(constructor_type != NULL);
 1385       assert(constructor_type->length == parameter_count);
 1386    }
 1387 
 1388    if (all_parameters_are_constant)
 1389       return new(ctx) ir_constant(constructor_type, &actual_parameters);
 1390 
 1391    ir_variable *var = new(ctx) ir_variable(constructor_type, "array_ctor",
 1392                                            ir_var_temporary);
 1393    instructions->push_tail(var);
 1394 
 1395    int i = 0;
 1396    foreach_in_list(ir_rvalue, rhs, &actual_parameters) {
 1397       ir_rvalue *lhs = new(ctx) ir_dereference_array(var,
 1398                                                      new(ctx) ir_constant(i));
 1399 
 1400       ir_instruction *assignment = new(ctx) ir_assignment(lhs, rhs);
 1401       instructions->push_tail(assignment);
 1402 
 1403       i++;
 1404    }
 1405 
 1406    return new(ctx) ir_dereference_variable(var);
 1407 }
 1408 
 1409 
 1410 /**
 1411  * Determine if a list consists of a single scalar r-value
 1412  */
 1413 static bool
 1414 single_scalar_parameter(exec_list *parameters)
 1415 {
 1416    const ir_rvalue *const p = (ir_rvalue *) parameters->get_head_raw();
 1417    assert(((ir_rvalue *)p)->as_rvalue() != NULL);
 1418 
 1419    return (p->type->is_scalar() && p->next->is_tail_sentinel());
 1420 }
 1421 
 1422 
 1423 /**
 1424  * Generate inline code for a vector constructor
 1425  *
 1426  * The generated constructor code will consist of a temporary variable
 1427  * declaration of the same type as the constructor.  A sequence of assignments
 1428  * from constructor parameters to the temporary will follow.
 1429  *
 1430  * \return
 1431  * An \c ir_dereference_variable of the temprorary generated in the constructor
 1432  * body.
 1433  */
 1434 static ir_rvalue *
 1435 emit_inline_vector_constructor(const glsl_type *type,
 1436                                exec_list *instructions,
 1437                                exec_list *parameters,
 1438                                void *ctx)
 1439 {
 1440    assert(!parameters->is_empty());
 1441 
 1442    ir_variable *var = new(ctx) ir_variable(type, "vec_ctor", ir_var_temporary);
 1443    instructions->push_tail(var);
 1444 
 1445    /* There are three kinds of vector constructors.
 1446     *
 1447     *  - Construct a vector from a single scalar by replicating that scalar to
 1448     *    all components of the vector.
 1449     *
 1450     *  - Construct a vector from at least a matrix. This case should already
 1451     *    have been taken care of in ast_function_expression::hir by breaking
 1452     *    down the matrix into a series of column vectors.
 1453     *
 1454     *  - Construct a vector from an arbirary combination of vectors and
 1455     *    scalars.  The components of the constructor parameters are assigned
 1456     *    to the vector in order until the vector is full.
 1457     */
 1458    const unsigned lhs_components = type->components();
 1459    if (single_scalar_parameter(parameters)) {
 1460       ir_rvalue *first_param = (ir_rvalue *)parameters->get_head_raw();
 1461       ir_rvalue *rhs = new(ctx) ir_swizzle(first_param, 0, 0, 0, 0,
 1462                                            lhs_components);
 1463       ir_dereference_variable *lhs = new(ctx) ir_dereference_variable(var);
 1464       const unsigned mask = (1U << lhs_components) - 1;
 1465 
 1466       assert(rhs->type == lhs->type);
 1467 
 1468       ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL, mask);
 1469       instructions->push_tail(inst);
 1470    } else {
 1471       unsigned base_component = 0;
 1472       unsigned base_lhs_component = 0;
 1473       ir_constant_data data;
 1474       unsigned constant_mask = 0, constant_components = 0;
 1475 
 1476       memset(&data, 0, sizeof(data));
 1477 
 1478       foreach_in_list(ir_rvalue, param, parameters) {
 1479          unsigned rhs_components = param->type->components();
 1480 
 1481          /* Do not try to assign more components to the vector than it has! */
 1482          if ((rhs_components + base_lhs_component) > lhs_components) {
 1483             rhs_components = lhs_components - base_lhs_component;
 1484          }
 1485 
 1486          const ir_constant *const c = param->as_constant();
 1487          if (c != NULL) {
 1488             for (unsigned i = 0; i < rhs_components; i++) {
 1489                switch (c->type->base_type) {
 1490                case GLSL_TYPE_UINT:
 1491                   data.u[i + base_component] = c->get_uint_component(i);
 1492                   break;
 1493                case GLSL_TYPE_INT:
 1494                   data.i[i + base_component] = c->get_int_component(i);
 1495                   break;
 1496                case GLSL_TYPE_FLOAT:
 1497                   data.f[i + base_component] = c->get_float_component(i);
 1498                   break;
 1499                case GLSL_TYPE_DOUBLE:
 1500                   data.d[i + base_component] = c->get_double_component(i);
 1501                   break;
 1502                case GLSL_TYPE_BOOL:
 1503                   data.b[i + base_component] = c->get_bool_component(i);
 1504                   break;
 1505                case GLSL_TYPE_UINT64:
 1506                   data.u64[i + base_component] = c->get_uint64_component(i);
 1507                   break;
 1508                case GLSL_TYPE_INT64:
 1509                   data.i64[i + base_component] = c->get_int64_component(i);
 1510                   break;
 1511                default:
 1512                   assert(!"Should not get here.");
 1513                   break;
 1514                }
 1515             }
 1516 
 1517             /* Mask of fields to be written in the assignment. */
 1518             constant_mask |= ((1U << rhs_components) - 1) << base_lhs_component;
 1519             constant_components += rhs_components;
 1520 
 1521             base_component += rhs_components;
 1522          }
 1523          /* Advance the component index by the number of components
 1524           * that were just assigned.
 1525           */
 1526          base_lhs_component += rhs_components;
 1527       }
 1528 
 1529       if (constant_mask != 0) {
 1530          ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
 1531          const glsl_type *rhs_type =
 1532             glsl_type::get_instance(var->type->base_type,
 1533                                     constant_components,
 1534                                     1);
 1535          ir_rvalue *rhs = new(ctx) ir_constant(rhs_type, &data);
 1536 
 1537          ir_instruction *inst =
 1538             new(ctx) ir_assignment(lhs, rhs, NULL, constant_mask);
 1539          instructions->push_tail(inst);
 1540       }
 1541 
 1542       base_component = 0;
 1543       foreach_in_list(ir_rvalue, param, parameters) {
 1544          unsigned rhs_components = param->type->components();
 1545 
 1546          /* Do not try to assign more components to the vector than it has! */
 1547          if ((rhs_components + base_component) > lhs_components) {
 1548             rhs_components = lhs_components - base_component;
 1549          }
 1550 
 1551          /* If we do not have any components left to copy, break out of the
 1552           * loop. This can happen when initializing a vec4 with a mat3 as the
 1553           * mat3 would have been broken into a series of column vectors.
 1554           */
 1555          if (rhs_components == 0) {
 1556             break;
 1557          }
 1558 
 1559          const ir_constant *const c = param->as_constant();
 1560          if (c == NULL) {
 1561             /* Mask of fields to be written in the assignment. */
 1562             const unsigned write_mask = ((1U << rhs_components) - 1)
 1563                << base_component;
 1564 
 1565             ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
 1566 
 1567             /* Generate a swizzle so that LHS and RHS sizes match. */
 1568             ir_rvalue *rhs =
 1569                new(ctx) ir_swizzle(param, 0, 1, 2, 3, rhs_components);
 1570 
 1571             ir_instruction *inst =
 1572                new(ctx) ir_assignment(lhs, rhs, NULL, write_mask);
 1573             instructions->push_tail(inst);
 1574          }
 1575 
 1576          /* Advance the component index by the number of components that were
 1577           * just assigned.
 1578           */
 1579          base_component += rhs_components;
 1580       }
 1581    }
 1582    return new(ctx) ir_dereference_variable(var);
 1583 }
 1584 
 1585 
 1586 /**
 1587  * Generate assignment of a portion of a vector to a portion of a matrix column
 1588  *
 1589  * \param src_base  First component of the source to be used in assignment
 1590  * \param column    Column of destination to be assiged
 1591  * \param row_base  First component of the destination column to be assigned
 1592  * \param count     Number of components to be assigned
 1593  *
 1594  * \note
 1595  * \c src_base + \c count must be less than or equal to the number of
 1596  * components in the source vector.
 1597  */
 1598 static ir_instruction *
 1599 assign_to_matrix_column(ir_variable *var, unsigned column, unsigned row_base,
 1600                         ir_rvalue *src, unsigned src_base, unsigned count,
 1601                         void *mem_ctx)
 1602 {
 1603    ir_constant *col_idx = new(mem_ctx) ir_constant(column);
 1604    ir_dereference *column_ref = new(mem_ctx) ir_dereference_array(var,
 1605                                                                   col_idx);
 1606 
 1607    assert(column_ref->type->components() >= (row_base + count));
 1608    assert(src->type->components() >= (src_base + count));
 1609 
 1610    /* Generate a swizzle that extracts the number of components from the source
 1611     * that are to be assigned to the column of the matrix.
 1612     */
 1613    if (count < src->type->vector_elements) {
 1614       src = new(mem_ctx) ir_swizzle(src,
 1615                                     src_base + 0, src_base + 1,
 1616                                     src_base + 2, src_base + 3,
 1617                                     count);
 1618    }
 1619 
 1620    /* Mask of fields to be written in the assignment. */
 1621    const unsigned write_mask = ((1U << count) - 1) << row_base;
 1622 
 1623    return new(mem_ctx) ir_assignment(column_ref, src, NULL, write_mask);
 1624 }
 1625 
 1626 
 1627 /**
 1628  * Generate inline code for a matrix constructor
 1629  *
 1630  * The generated constructor code will consist of a temporary variable
 1631  * declaration of the same type as the constructor.  A sequence of assignments
 1632  * from constructor parameters to the temporary will follow.
 1633  *
 1634  * \return
 1635  * An \c ir_dereference_variable of the temprorary generated in the constructor
 1636  * body.
 1637  */
 1638 static ir_rvalue *
 1639 emit_inline_matrix_constructor(const glsl_type *type,
 1640                                exec_list *instructions,
 1641                                exec_list *parameters,
 1642                                void *ctx)
 1643 {
 1644    assert(!parameters->is_empty());
 1645 
 1646    ir_variable *var = new(ctx) ir_variable(type, "mat_ctor", ir_var_temporary);
 1647    instructions->push_tail(var);
 1648 
 1649    /* There are three kinds of matrix constructors.
 1650     *
 1651     *  - Construct a matrix from a single scalar by replicating that scalar to
 1652     *    along the diagonal of the matrix and setting all other components to
 1653     *    zero.
 1654     *
 1655     *  - Construct a matrix from an arbirary combination of vectors and
 1656     *    scalars.  The components of the constructor parameters are assigned
 1657     *    to the matrix in column-major order until the matrix is full.
 1658     *
 1659     *  - Construct a matrix from a single matrix.  The source matrix is copied
 1660     *    to the upper left portion of the constructed matrix, and the remaining
 1661     *    elements take values from the identity matrix.
 1662     */
 1663    ir_rvalue *const first_param = (ir_rvalue *) parameters->get_head_raw();
 1664    if (single_scalar_parameter(parameters)) {
 1665       /* Assign the scalar to the X component of a vec4, and fill the remaining
 1666        * components with zero.
 1667        */
 1668       glsl_base_type param_base_type = first_param->type->base_type;
 1669       assert(first_param->type->is_float() || first_param->type->is_double());
 1670       ir_variable *rhs_var =
 1671          new(ctx) ir_variable(glsl_type::get_instance(param_base_type, 4, 1),
 1672                               "mat_ctor_vec",
 1673                               ir_var_temporary);
 1674       instructions->push_tail(rhs_var);
 1675 
 1676       ir_constant_data zero;
 1677       for (unsigned i = 0; i < 4; i++)
 1678          if (first_param->type->is_float())
 1679             zero.f[i] = 0.0;
 1680          else
 1681             zero.d[i] = 0.0;
 1682 
 1683       ir_instruction *inst =
 1684          new(ctx) ir_assignment(new(ctx) ir_dereference_variable(rhs_var),
 1685                                 new(ctx) ir_constant(rhs_var->type, &zero));
 1686       instructions->push_tail(inst);
 1687 
 1688       ir_dereference *const rhs_ref =
 1689          new(ctx) ir_dereference_variable(rhs_var);
 1690 
 1691       inst = new(ctx) ir_assignment(rhs_ref, first_param, NULL, 0x01);
 1692       instructions->push_tail(inst);
 1693 
 1694       /* Assign the temporary vector to each column of the destination matrix
 1695        * with a swizzle that puts the X component on the diagonal of the
 1696        * matrix.  In some cases this may mean that the X component does not
 1697        * get assigned into the column at all (i.e., when the matrix has more
 1698        * columns than rows).
 1699        */
 1700       static const unsigned rhs_swiz[4][4] = {
 1701          { 0, 1, 1, 1 },
 1702          { 1, 0, 1, 1 },
 1703          { 1, 1, 0, 1 },
 1704          { 1, 1, 1, 0 }
 1705       };
 1706 
 1707       const unsigned cols_to_init = MIN2(type->matrix_columns,
 1708                                          type->vector_elements);
 1709       for (unsigned i = 0; i < cols_to_init; i++) {
 1710          ir_constant *const col_idx = new(ctx) ir_constant(i);
 1711          ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var,
 1712                                                                   col_idx);
 1713 
 1714          ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
 1715          ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, rhs_swiz[i],
 1716                                                     type->vector_elements);
 1717 
 1718          inst = new(ctx) ir_assignment(col_ref, rhs);
 1719          instructions->push_tail(inst);
 1720       }
 1721 
 1722       for (unsigned i = cols_to_init; i < type->matrix_columns; i++) {
 1723          ir_constant *const col_idx = new(ctx) ir_constant(i);
 1724          ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var,
 1725                                                                   col_idx);
 1726 
 1727          ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
 1728          ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, 1, 1, 1, 1,
 1729                                                     type->vector_elements);
 1730 
 1731          inst = new(ctx) ir_assignment(col_ref, rhs);
 1732          instructions->push_tail(inst);
 1733       }
 1734    } else if (first_param->type->is_matrix()) {
 1735       /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
 1736        *
 1737        *     "If a matrix is constructed from a matrix, then each component
 1738        *     (column i, row j) in the result that has a corresponding
 1739        *     component (column i, row j) in the argument will be initialized
 1740        *     from there. All other components will be initialized to the
 1741        *     identity matrix. If a matrix argument is given to a matrix
 1742        *     constructor, it is an error to have any other arguments."
 1743        */
 1744       assert(first_param->next->is_tail_sentinel());
 1745       ir_rvalue *const src_matrix = first_param;
 1746 
 1747       /* If the source matrix is smaller, pre-initialize the relavent parts of
 1748        * the destination matrix to the identity matrix.
 1749        */
 1750       if ((src_matrix->type->matrix_columns < var->type->matrix_columns) ||
 1751           (src_matrix->type->vector_elements < var->type->vector_elements)) {
 1752 
 1753          /* If the source matrix has fewer rows, every column of the
 1754           * destination must be initialized.  Otherwise only the columns in
 1755           * the destination that do not exist in the source must be
 1756           * initialized.
 1757           */
 1758          unsigned col =
 1759             (src_matrix->type->vector_elements < var->type->vector_elements)
 1760             ? 0 : src_matrix->type->matrix_columns;
 1761 
 1762          const glsl_type *const col_type = var->type->column_type();
 1763          for (/* empty */; col < var->type->matrix_columns; col++) {
 1764             ir_constant_data ident;
 1765 
 1766             if (!col_type->is_double()) {
 1767                ident.f[0] = 0.0f;
 1768                ident.f[1] = 0.0f;
 1769                ident.f[2] = 0.0f;
 1770                ident.f[3] = 0.0f;
 1771                ident.f[col] = 1.0f;
 1772             } else {
 1773                ident.d[0] = 0.0;
 1774                ident.d[1] = 0.0;
 1775                ident.d[2] = 0.0;
 1776                ident.d[3] = 0.0;
 1777                ident.d[col] = 1.0;
 1778             }
 1779 
 1780             ir_rvalue *const rhs = new(ctx) ir_constant(col_type, &ident);
 1781 
 1782             ir_rvalue *const lhs =
 1783                new(ctx) ir_dereference_array(var, new(ctx) ir_constant(col));
 1784 
 1785             ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs);
 1786             instructions->push_tail(inst);
 1787          }
 1788       }
 1789 
 1790       /* Assign columns from the source matrix to the destination matrix.
 1791        *
 1792        * Since the parameter will be used in the RHS of multiple assignments,
 1793        * generate a temporary and copy the paramter there.
 1794        */
 1795       ir_variable *const rhs_var =
 1796          new(ctx) ir_variable(first_param->type, "mat_ctor_mat",
 1797                               ir_var_temporary);
 1798       instructions->push_tail(rhs_var);
 1799 
 1800       ir_dereference *const rhs_var_ref =
 1801          new(ctx) ir_dereference_variable(rhs_var);
 1802       ir_instruction *const inst =
 1803          new(ctx) ir_assignment(rhs_var_ref, first_param);
 1804       instructions->push_tail(inst);
 1805 
 1806       const unsigned last_row = MIN2(src_matrix->type->vector_elements,
 1807                                      var->type->vector_elements);
 1808       const unsigned last_col = MIN2(src_matrix->type->matrix_columns,
 1809                                      var->type->matrix_columns);
 1810 
 1811       unsigned swiz[4] = { 0, 0, 0, 0 };
 1812       for (unsigned i = 1; i < last_row; i++)
 1813          swiz[i] = i;
 1814 
 1815       const unsigned write_mask = (1U << last_row) - 1;
 1816 
 1817       for (unsigned i = 0; i < last_col; i++) {
 1818          ir_dereference *const lhs =
 1819             new(ctx) ir_dereference_array(var, new(ctx) ir_constant(i));
 1820          ir_rvalue *const rhs_col =
 1821             new(ctx) ir_dereference_array(rhs_var, new(ctx) ir_constant(i));
 1822 
 1823          /* If one matrix has columns that are smaller than the columns of the
 1824           * other matrix, wrap the column access of the larger with a swizzle
 1825           * so that the LHS and RHS of the assignment have the same size (and
 1826           * therefore have the same type).
 1827           *
 1828           * It would be perfectly valid to unconditionally generate the
 1829           * swizzles, this this will typically result in a more compact IR
 1830           * tree.
 1831           */
 1832          ir_rvalue *rhs;
 1833          if (lhs->type->vector_elements != rhs_col->type->vector_elements) {
 1834             rhs = new(ctx) ir_swizzle(rhs_col, swiz, last_row);
 1835          } else {
 1836             rhs = rhs_col;
 1837          }
 1838 
 1839          ir_instruction *inst =
 1840             new(ctx) ir_assignment(lhs, rhs, NULL, write_mask);
 1841          instructions->push_tail(inst);
 1842       }
 1843    } else {
 1844       const unsigned cols = type->matrix_columns;
 1845       const unsigned rows = type->vector_elements;
 1846       unsigned remaining_slots = rows * cols;
 1847       unsigned col_idx = 0;
 1848       unsigned row_idx = 0;
 1849 
 1850       foreach_in_list(ir_rvalue, rhs, parameters) {
 1851          unsigned rhs_components = rhs->type->components();
 1852          unsigned rhs_base = 0;
 1853 
 1854          if (remaining_slots == 0)
 1855             break;
 1856 
 1857          /* Since the parameter might be used in the RHS of two assignments,
 1858           * generate a temporary and copy the paramter there.
 1859           */
 1860          ir_variable *rhs_var =
 1861             new(ctx) ir_variable(rhs->type, "mat_ctor_vec", ir_var_temporary);
 1862          instructions->push_tail(rhs_var);
 1863 
 1864          ir_dereference *rhs_var_ref =
 1865             new(ctx) ir_dereference_variable(rhs_var);
 1866          ir_instruction *inst = new(ctx) ir_assignment(rhs_var_ref, rhs);
 1867          instructions->push_tail(inst);
 1868 
 1869          do {
 1870             /* Assign the current parameter to as many components of the matrix
 1871              * as it will fill.
 1872              *
 1873              * NOTE: A single vector parameter can span two matrix columns.  A
 1874              * single vec4, for example, can completely fill a mat2.
 1875              */
 1876             unsigned count = MIN2(rows - row_idx,
 1877                                   rhs_components - rhs_base);
 1878 
 1879             rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
 1880             ir_instruction *inst = assign_to_matrix_column(var, col_idx,
 1881                                                            row_idx,
 1882                                                            rhs_var_ref,
 1883                                                            rhs_base,
 1884                                                            count, ctx);
 1885             instructions->push_tail(inst);
 1886             rhs_base += count;
 1887             row_idx += count;
 1888             remaining_slots -= count;
 1889 
 1890             /* Sometimes, there is still data left in the parameters and
 1891              * components left to be set in the destination but in other
 1892              * column.
 1893              */
 1894             if (row_idx >= rows) {
 1895                row_idx = 0;
 1896                col_idx++;
 1897             }
 1898          } while(remaining_slots > 0 && rhs_base < rhs_components);
 1899       }
 1900    }
 1901 
 1902    return new(ctx) ir_dereference_variable(var);
 1903 }
 1904 
 1905 
 1906 static ir_rvalue *
 1907 emit_inline_record_constructor(const glsl_type *type,
 1908                                exec_list *instructions,
 1909                                exec_list *parameters,
 1910                                void *mem_ctx)
 1911 {
 1912    ir_variable *const var =
 1913       new(mem_ctx) ir_variable(type, "record_ctor", ir_var_temporary);
 1914    ir_dereference_variable *const d =
 1915       new(mem_ctx) ir_dereference_variable(var);
 1916 
 1917    instructions->push_tail(var);
 1918 
 1919    exec_node *node = parameters->get_head_raw();
 1920    for (unsigned i = 0; i < type->length; i++) {
 1921       assert(!node->is_tail_sentinel());
 1922 
 1923       ir_dereference *const lhs =
 1924          new(mem_ctx) ir_dereference_record(d->clone(mem_ctx, NULL),
 1925                                             type->fields.structure[i].name);
 1926 
 1927       ir_rvalue *const rhs = ((ir_instruction *) node)->as_rvalue();
 1928       assert(rhs != NULL);
 1929 
 1930       ir_instruction *const assign = new(mem_ctx) ir_assignment(lhs, rhs);
 1931 
 1932       instructions->push_tail(assign);
 1933       node = node->next;
 1934    }
 1935 
 1936    return d;
 1937 }
 1938 
 1939 
 1940 static ir_rvalue *
 1941 process_record_constructor(exec_list *instructions,
 1942                            const glsl_type *constructor_type,
 1943                            YYLTYPE *loc, exec_list *parameters,
 1944                            struct _mesa_glsl_parse_state *state)
 1945 {
 1946    void *ctx = state;
 1947    /* From page 32 (page 38 of the PDF) of the GLSL 1.20 spec:
 1948     *
 1949     *    "The arguments to the constructor will be used to set the structure's
 1950     *     fields, in order, using one argument per field. Each argument must
 1951     *     be the same type as the field it sets, or be a type that can be
 1952     *     converted to the field's type according to Section 4.1.10 “Implicit
 1953     *     Conversions.”"
 1954     *
 1955     * From page 35 (page 41 of the PDF) of the GLSL 4.20 spec:
 1956     *
 1957     *    "In all cases, the innermost initializer (i.e., not a list of
 1958     *     initializers enclosed in curly braces) applied to an object must
 1959     *     have the same type as the object being initialized or be a type that
 1960     *     can be converted to the object's type according to section 4.1.10
 1961     *     "Implicit Conversions". In the latter case, an implicit conversion
 1962     *     will be done on the initializer before the assignment is done."
 1963     */
 1964    exec_list actual_parameters;
 1965 
 1966    const unsigned parameter_count =
 1967          process_parameters(instructions, &actual_parameters, parameters,
 1968                             state);
 1969 
 1970    if (parameter_count != constructor_type->length) {
 1971       _mesa_glsl_error(loc, state,
 1972                        "%s parameters in constructor for `%s'",
 1973                        parameter_count > constructor_type->length
 1974                        ? "too many": "insufficient",
 1975                        constructor_type->name);
 1976       return ir_rvalue::error_value(ctx);
 1977    }
 1978 
 1979    bool all_parameters_are_constant = true;
 1980 
 1981    int i = 0;
 1982    /* Type cast each parameter and, if possible, fold constants. */
 1983    foreach_in_list_safe(ir_rvalue, ir, &actual_parameters) {
 1984 
 1985       const glsl_struct_field *struct_field =
 1986          &constructor_type->fields.structure[i];
 1987 
 1988       /* Apply implicit conversions (not the scalar constructor rules, see the
 1989        * spec quote above!) and attempt to convert the parameter to a constant
 1990        * valued expression. After doing so, track whether or not all the
 1991        * parameters to the constructor are trivially constant valued
 1992        * expressions.
 1993        */
 1994       all_parameters_are_constant &=
 1995          implicitly_convert_component(ir, struct_field->type->base_type,
 1996                                       state);
 1997 
 1998       if (ir->type != struct_field->type) {
 1999          _mesa_glsl_error(loc, state,
 2000                           "parameter type mismatch in constructor for `%s.%s' "
 2001                           "(%s vs %s)",
 2002                           constructor_type->name,
 2003                           struct_field->name,
 2004                           ir->type->name,
 2005                           struct_field->type->name);
 2006          return ir_rvalue::error_value(ctx);
 2007       }
 2008 
 2009       i++;
 2010    }
 2011 
 2012    if (all_parameters_are_constant) {
 2013       return new(ctx) ir_constant(constructor_type, &actual_parameters);
 2014    } else {
 2015       return emit_inline_record_constructor(constructor_type, instructions,
 2016                                             &actual_parameters, state);
 2017    }
 2018 }
 2019 
 2020 ir_rvalue *
 2021 ast_function_expression::handle_method(exec_list *instructions,
 2022                                        struct _mesa_glsl_parse_state *state)
 2023 {
 2024    const ast_expression *field = subexpressions[0];
 2025    ir_rvalue *op;
 2026    ir_rvalue *result;
 2027    void *ctx = state;
 2028    /* Handle "method calls" in GLSL 1.20 - namely, array.length() */
 2029    YYLTYPE loc = get_location();
 2030    state->check_version(120, 300, &loc, "methods not supported");
 2031 
 2032    const char *method;
 2033    method = field->primary_expression.identifier;
 2034 
 2035    /* This would prevent to raise "uninitialized variable" warnings when
 2036     * calling array.length.
 2037     */
 2038    field->subexpressions[0]->set_is_lhs(true);
 2039    op = field->subexpressions[0]->hir(instructions, state);
 2040    if (strcmp(method, "length") == 0) {
 2041       if (!this->expressions.is_empty()) {
 2042          _mesa_glsl_error(&loc, state, "length method takes no arguments");
 2043          goto fail;
 2044       }
 2045 
 2046       if (op->type->is_array()) {
 2047          if (op->type->is_unsized_array()) {
 2048             if (!state->has_shader_storage_buffer_objects()) {
 2049                _mesa_glsl_error(&loc, state,
 2050                                 "length called on unsized array"
 2051                                 " only available with"
 2052                                 " ARB_shader_storage_buffer_object");
 2053             }
 2054             /* Calculate length of an unsized array in run-time */
 2055             result = new(ctx) ir_expression(ir_unop_ssbo_unsized_array_length,
 2056                                             op);
 2057          } else {
 2058             result = new(ctx) ir_constant(op->type->array_size());
 2059          }
 2060       } else if (op->type->is_vector()) {
 2061          if (state->has_420pack()) {
 2062             /* .length() returns int. */
 2063             result = new(ctx) ir_constant((int) op->type->vector_elements);
 2064          } else {
 2065             _mesa_glsl_error(&loc, state, "length method on matrix only"
 2066                              " available with ARB_shading_language_420pack");
 2067             goto fail;
 2068          }
 2069       } else if (op->type->is_matrix()) {
 2070          if (state->has_420pack()) {
 2071             /* .length() returns int. */
 2072             result = new(ctx) ir_constant((int) op->type->matrix_columns);
 2073          } else {
 2074             _mesa_glsl_error(&loc, state, "length method on matrix only"
 2075                              " available with ARB_shading_language_420pack");
 2076             goto fail;
 2077          }
 2078       } else {
 2079          _mesa_glsl_error(&loc, state, "length called on scalar.");
 2080          goto fail;
 2081       }
 2082    } else {
 2083       _mesa_glsl_error(&loc, state, "unknown method: `%s'", method);
 2084       goto fail;
 2085    }
 2086    return result;
 2087  fail:
 2088    return ir_rvalue::error_value(ctx);
 2089 }
 2090 
 2091 static inline bool is_valid_constructor(const glsl_type *type,
 2092                                         struct _mesa_glsl_parse_state *state)
 2093 {
 2094    return type->is_numeric() || type->is_boolean() ||
 2095           (state->has_bindless() && (type->is_sampler() || type->is_image()));
 2096 }
 2097 
 2098 ir_rvalue *
 2099 ast_function_expression::hir(exec_list *instructions,
 2100                              struct _mesa_glsl_parse_state *state)
 2101 {
 2102    void *ctx = state;
 2103    /* There are three sorts of function calls.
 2104     *
 2105     * 1. constructors - The first subexpression is an ast_type_specifier.
 2106     * 2. methods - Only the .length() method of array types.
 2107     * 3. functions - Calls to regular old functions.
 2108     *
 2109     */
 2110    if (is_constructor()) {
 2111       const ast_type_specifier *type =
 2112          (ast_type_specifier *) subexpressions[0];
 2113       YYLTYPE loc = type->get_location();
 2114       const char *name;
 2115 
 2116       const glsl_type *const constructor_type = type->glsl_type(& name, state);
 2117 
 2118       /* constructor_type can be NULL if a variable with the same name as the
 2119        * structure has come into scope.
 2120        */
 2121       if (constructor_type == NULL) {
 2122          _mesa_glsl_error(& loc, state, "unknown type `%s' (structure name "
 2123                           "may be shadowed by a variable with the same name)",
 2124                           type->type_name);
 2125          return ir_rvalue::error_value(ctx);
 2126       }
 2127 
 2128 
 2129       /* Constructors for opaque types are illegal.
 2130        *
 2131        * From section 4.1.7 of the ARB_bindless_texture spec:
 2132        *
 2133        * "Samplers are represented using 64-bit integer handles, and may be "
 2134        *  converted to and from 64-bit integers using constructors."
 2135        *
 2136        * From section 4.1.X of the ARB_bindless_texture spec:
 2137        *
 2138        * "Images are represented using 64-bit integer handles, and may be
 2139        *  converted to and from 64-bit integers using constructors."
 2140        */
 2141       if (constructor_type->contains_atomic() ||
 2142           (!state->has_bindless() && constructor_type->contains_opaque())) {
 2143          _mesa_glsl_error(& loc, state, "cannot construct %s type `%s'",
 2144                           state->has_bindless() ? "atomic" : "opaque",
 2145                           constructor_type->name);
 2146          return ir_rvalue::error_value(ctx);
 2147       }
 2148 
 2149       if (constructor_type->is_subroutine()) {
 2150          _mesa_glsl_error(& loc, state,
 2151                           "subroutine name cannot be a constructor `%s'",
 2152                           constructor_type->name);
 2153          return ir_rvalue::error_value(ctx);
 2154       }
 2155 
 2156       if (constructor_type->is_array()) {
 2157          if (!state->check_version(120, 300, &loc,
 2158                                    "array constructors forbidden")) {
 2159             return ir_rvalue::error_value(ctx);
 2160          }
 2161 
 2162          return process_array_constructor(instructions, constructor_type,
 2163                                           & loc, &this->expressions, state);
 2164       }
 2165 
 2166 
 2167       /* There are two kinds of constructor calls.  Constructors for arrays and
 2168        * structures must have the exact number of arguments with matching types
 2169        * in the correct order.  These constructors follow essentially the same
 2170        * type matching rules as functions.
 2171        *
 2172        * Constructors for built-in language types, such as mat4 and vec2, are
 2173        * free form.  The only requirements are that the parameters must provide
 2174        * enough values of the correct scalar type and that no arguments are
 2175        * given past the last used argument.
 2176        *
 2177        * When using the C-style initializer syntax from GLSL 4.20, constructors
 2178        * must have the exact number of arguments with matching types in the
 2179        * correct order.
 2180        */
 2181       if (constructor_type->is_struct()) {
 2182          return process_record_constructor(instructions, constructor_type,
 2183                                            &loc, &this->expressions,
 2184                                            state);
 2185       }
 2186 
 2187       if (!is_valid_constructor(constructor_type, state))
 2188          return ir_rvalue::error_value(ctx);
 2189 
 2190       /* Total number of components of the type being constructed. */
 2191       const unsigned type_components = constructor_type->components();
 2192 
 2193       /* Number of components from parameters that have actually been
 2194        * consumed.  This is used to perform several kinds of error checking.
 2195        */
 2196       unsigned components_used = 0;
 2197 
 2198       unsigned matrix_parameters = 0;
 2199       unsigned nonmatrix_parameters = 0;
 2200       exec_list actual_parameters;
 2201 
 2202       foreach_list_typed(ast_node, ast, link, &this->expressions) {
 2203          ir_rvalue *result = ast->hir(instructions, state);
 2204 
 2205          /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
 2206           *
 2207           *    "It is an error to provide extra arguments beyond this
 2208           *    last used argument."
 2209           */
 2210          if (components_used >= type_components) {
 2211             _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
 2212                              "constructor",
 2213                              constructor_type->name);
 2214             return ir_rvalue::error_value(ctx);
 2215          }
 2216 
 2217          if (!is_valid_constructor(result->type, state)) {
 2218             _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
 2219                              "non-numeric data type",
 2220                              constructor_type->name);
 2221             return ir_rvalue::error_value(ctx);
 2222          }
 2223 
 2224          /* Count the number of matrix and nonmatrix parameters.  This
 2225           * is used below to enforce some of the constructor rules.
 2226           */
 2227          if (result->type->is_matrix())
 2228             matrix_parameters++;
 2229          else
 2230             nonmatrix_parameters++;
 2231 
 2232          actual_parameters.push_tail(result);
 2233          components_used += result->type->components();
 2234       }
 2235 
 2236       /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
 2237        *
 2238        *    "It is an error to construct matrices from other matrices. This
 2239        *    is reserved for future use."
 2240        */
 2241       if (matrix_parameters > 0
 2242           && constructor_type->is_matrix()
 2243           && !state->check_version(120, 100, &loc,
 2244                                    "cannot construct `%s' from a matrix",
 2245                                    constructor_type->name)) {
 2246          return ir_rvalue::error_value(ctx);
 2247       }
 2248 
 2249       /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
 2250        *
 2251        *    "If a matrix argument is given to a matrix constructor, it is
 2252        *    an error to have any other arguments."
 2253        */
 2254       if ((matrix_parameters > 0)
 2255           && ((matrix_parameters + nonmatrix_parameters) > 1)
 2256           && constructor_type->is_matrix()) {
 2257          _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
 2258                           "matrix must be only parameter",
 2259                           constructor_type->name);
 2260          return ir_rvalue::error_value(ctx);
 2261       }
 2262 
 2263       /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
 2264        *
 2265        *    "In these cases, there must be enough components provided in the
 2266        *    arguments to provide an initializer for every component in the
 2267        *    constructed value."
 2268        */
 2269       if (components_used < type_components && components_used != 1
 2270           && matrix_parameters == 0) {
 2271          _mesa_glsl_error(& loc, state, "too few components to construct "
 2272                           "`%s'",
 2273                           constructor_type->name);
 2274          return ir_rvalue::error_value(ctx);
 2275       }
 2276 
 2277       /* Matrices can never be consumed as is by any constructor but matrix
 2278        * constructors. If the constructor type is not matrix, always break the
 2279        * matrix up into a series of column vectors.
 2280        */
 2281       if (!constructor_type->is_matrix()) {
 2282          foreach_in_list_safe(ir_rvalue, matrix, &actual_parameters) {
 2283             if (!matrix->type->is_matrix())
 2284                continue;
 2285 
 2286             /* Create a temporary containing the matrix. */
 2287             ir_variable *var = new(ctx) ir_variable(matrix->type, "matrix_tmp",
 2288                                                     ir_var_temporary);
 2289             instructions->push_tail(var);
 2290             instructions->push_tail(
 2291                new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
 2292                                       matrix));
 2293             var->constant_value = matrix->constant_expression_value(ctx);
 2294 
 2295             /* Replace the matrix with dereferences of its columns. */
 2296             for (int i = 0; i < matrix->type->matrix_columns; i++) {
 2297                matrix->insert_before(
 2298                   new (ctx) ir_dereference_array(var,
 2299                                                  new(ctx) ir_constant(i)));
 2300             }
 2301             matrix->remove();
 2302          }
 2303       }
 2304 
 2305       bool all_parameters_are_constant = true;
 2306 
 2307       /* Type cast each parameter and, if possible, fold constants.*/
 2308       foreach_in_list_safe(ir_rvalue, ir, &actual_parameters) {
 2309          const glsl_type *desired_type;
 2310 
 2311          /* From section 5.4.1 of the ARB_bindless_texture spec:
 2312           *
 2313           * "In the following four constructors, the low 32 bits of the sampler
 2314           *  type correspond to the .x component of the uvec2 and the high 32
 2315           *  bits correspond to the .y component."
 2316           *
 2317           *  uvec2(any sampler type)     // Converts a sampler type to a
 2318           *                              //   pair of 32-bit unsigned integers
 2319           *  any sampler type(uvec2)     // Converts a pair of 32-bit unsigned integers to
 2320           *                              //   a sampler type
 2321           *  uvec2(any image type)       // Converts an image type to a
 2322           *                              //   pair of 32-bit unsigned integers
 2323           *  any image type(uvec2)       // Converts a pair of 32-bit unsigned integers to
 2324           *                              //   an image type
 2325           */
 2326          if (ir->type->is_sampler() || ir->type->is_image()) {
 2327             /* Convert a sampler/image type to a pair of 32-bit unsigned
 2328              * integers as defined by ARB_bindless_texture.
 2329              */
 2330             if (constructor_type != glsl_type::uvec2_type) {
 2331                _mesa_glsl_error(&loc, state, "sampler and image types can only "
 2332                                 "be converted to a pair of 32-bit unsigned "
 2333                                 "integers");
 2334             }
 2335             desired_type = glsl_type::uvec2_type;
 2336          } else if (constructor_type->is_sampler() ||
 2337                     constructor_type->is_image()) {
 2338             /* Convert a pair of 32-bit unsigned integers to a sampler or image
 2339              * type as defined by ARB_bindless_texture.
 2340              */
 2341             if (ir->type != glsl_type::uvec2_type) {
 2342                _mesa_glsl_error(&loc, state, "sampler and image types can only "
 2343                                 "be converted from a pair of 32-bit unsigned "
 2344                                 "integers");
 2345             }
 2346             desired_type = constructor_type;
 2347          } else {
 2348             desired_type =
 2349                glsl_type::get_instance(constructor_type->base_type,
 2350                                        ir->type->vector_elements,
 2351                                        ir->type->matrix_columns);
 2352          }
 2353 
 2354          ir_rvalue *result = convert_component(ir, desired_type);
 2355 
 2356          /* Attempt to convert the parameter to a constant valued expression.
 2357           * After doing so, track whether or not all the parameters to the
 2358           * constructor are trivially constant valued expressions.
 2359           */
 2360          ir_rvalue *const constant = result->constant_expression_value(ctx);
 2361 
 2362          if (constant != NULL)
 2363             result = constant;
 2364          else
 2365             all_parameters_are_constant = false;
 2366 
 2367          if (result != ir) {
 2368             ir->replace_with(result);
 2369          }
 2370       }
 2371 
 2372       /* If all of the parameters are trivially constant, create a
 2373        * constant representing the complete collection of parameters.
 2374        */
 2375       if (all_parameters_are_constant) {
 2376          return new(ctx) ir_constant(constructor_type, &actual_parameters);
 2377       } else if (constructor_type->is_scalar()) {
 2378          return dereference_component((ir_rvalue *)
 2379                                       actual_parameters.get_head_raw(),
 2380                                       0);
 2381       } else if (constructor_type->is_vector()) {
 2382          return emit_inline_vector_constructor(constructor_type,
 2383                                                instructions,
 2384                                                &actual_parameters,
 2385                                                ctx);
 2386       } else {
 2387          assert(constructor_type->is_matrix());
 2388          return emit_inline_matrix_constructor(constructor_type,
 2389                                                instructions,
 2390                                                &actual_parameters,
 2391                                                ctx);
 2392       }
 2393    } else if (subexpressions[0]->oper == ast_field_selection) {
 2394       return handle_method(instructions, state);
 2395    } else {
 2396       const ast_expression *id = subexpressions[0];
 2397       const char *func_name = NULL;
 2398       YYLTYPE loc = get_location();
 2399       exec_list actual_parameters;
 2400       ir_variable *sub_var = NULL;
 2401       ir_rvalue *array_idx = NULL;
 2402 
 2403       process_parameters(instructions, &actual_parameters, &this->expressions,
 2404                          state);
 2405 
 2406       if (id->oper == ast_array_index) {
 2407          array_idx = generate_array_index(ctx, instructions, state, loc,
 2408                                           id->subexpressions[0],
 2409                                           id->subexpressions[1], &func_name,
 2410                                           &actual_parameters);
 2411       } else if (id->oper == ast_identifier) {
 2412          func_name = id->primary_expression.identifier;
 2413       } else {
 2414          _mesa_glsl_error(&loc, state, "function name is not an identifier");
 2415       }
 2416 
 2417       /* an error was emitted earlier */
 2418       if (!func_name)
 2419          return ir_rvalue::error_value(ctx);
 2420 
 2421       ir_function_signature *sig =
 2422          match_function_by_name(func_name, &actual_parameters, state);
 2423 
 2424       ir_rvalue *value = NULL;
 2425       if (sig == NULL) {
 2426          sig = match_subroutine_by_name(func_name, &actual_parameters,
 2427                                         state, &sub_var);
 2428       }
 2429 
 2430       if (sig == NULL) {
 2431          no_matching_function_error(func_name, &loc,
 2432                                     &actual_parameters, state);
 2433          value = ir_rvalue::error_value(ctx);
 2434       } else if (!verify_parameter_modes(state, sig,
 2435                                          actual_parameters,
 2436                                          this->expressions)) {
 2437          /* an error has already been emitted */
 2438          value = ir_rvalue::error_value(ctx);
 2439       } else if (sig->is_builtin() && strcmp(func_name, "ftransform") == 0) {
 2440          /* ftransform refers to global variables, and we don't have any code
 2441           * for remapping the variable references in the built-in shader.
 2442           */
 2443          ir_variable *mvp =
 2444             state->symbols->get_variable("gl_ModelViewProjectionMatrix");
 2445          ir_variable *vtx = state->symbols->get_variable("gl_Vertex");
 2446          value = new(ctx) ir_expression(ir_binop_mul, glsl_type::vec4_type,
 2447                                         new(ctx) ir_dereference_variable(mvp),
 2448                                         new(ctx) ir_dereference_variable(vtx));
 2449       } else {
 2450          bool is_begin_interlock = false;
 2451          bool is_end_interlock = false;
 2452          if (sig->is_builtin() &&
 2453              state->stage == MESA_SHADER_FRAGMENT &&
 2454              state->ARB_fragment_shader_interlock_enable) {
 2455             is_begin_interlock = strcmp(func_name, "beginInvocationInterlockARB") == 0;
 2456             is_end_interlock = strcmp(func_name, "endInvocationInterlockARB") == 0;
 2457          }
 2458 
 2459          if (sig->is_builtin() &&
 2460              ((state->stage == MESA_SHADER_TESS_CTRL &&
 2461                strcmp(func_name, "barrier") == 0) ||
 2462               is_begin_interlock || is_end_interlock)) {
 2463             if (state->current_function == NULL ||
 2464                 strcmp(state->current_function->function_name(), "main") != 0) {
 2465                _mesa_glsl_error(&loc, state,
 2466                                 "%s() may only be used in main()", func_name);
 2467             }
 2468 
 2469             if (state->found_return) {
 2470                _mesa_glsl_error(&loc, state,
 2471                                 "%s() may not be used after return", func_name);
 2472             }
 2473 
 2474             if (instructions != &state->current_function->body) {
 2475                _mesa_glsl_error(&loc, state,
 2476                                 "%s() may not be used in control flow", func_name);
 2477             }
 2478          }
 2479 
 2480          /* There can be only one begin/end interlock pair in the function. */
 2481          if (is_begin_interlock) {
 2482             if (state->found_begin_interlock)
 2483                _mesa_glsl_error(&loc, state,
 2484                                 "beginInvocationInterlockARB may not be used twice");
 2485             state->found_begin_interlock = true;
 2486          } else if (is_end_interlock) {
 2487             if (!state->found_begin_interlock)
 2488                _mesa_glsl_error(&loc, state,
 2489                                 "endInvocationInterlockARB may not be used "
 2490                                 "before beginInvocationInterlockARB");
 2491             if (state->found_end_interlock)
 2492                _mesa_glsl_error(&loc, state,
 2493                                 "endInvocationInterlockARB may not be used twice");
 2494             state->found_end_interlock = true;
 2495          }
 2496 
 2497          value = generate_call(instructions, sig, &actual_parameters, sub_var,
 2498                                array_idx, state);
 2499          if (!value) {
 2500             ir_variable *const tmp = new(ctx) ir_variable(glsl_type::void_type,
 2501                                                           "void_var",
 2502                                                           ir_var_temporary);
 2503             instructions->push_tail(tmp);
 2504             value = new(ctx) ir_dereference_variable(tmp);
 2505          }
 2506       }
 2507 
 2508       return value;
 2509    }
 2510 
 2511    unreachable("not reached");
 2512 }
 2513 
 2514 bool
 2515 ast_function_expression::has_sequence_subexpression() const
 2516 {
 2517    foreach_list_typed(const ast_node, ast, link, &this->expressions) {
 2518       if (ast->has_sequence_subexpression())
 2519          return true;
 2520    }
 2521 
 2522    return false;
 2523 }
 2524 
 2525 ir_rvalue *
 2526 ast_aggregate_initializer::hir(exec_list *instructions,
 2527                                struct _mesa_glsl_parse_state *state)
 2528 {
 2529    void *ctx = state;
 2530    YYLTYPE loc = this->get_location();
 2531 
 2532    if (!this->constructor_type) {
 2533       _mesa_glsl_error(&loc, state, "type of C-style initializer unknown");
 2534       return ir_rvalue::error_value(ctx);
 2535    }
 2536    const glsl_type *const constructor_type = this->constructor_type;
 2537 
 2538    if (!state->has_420pack()) {
 2539       _mesa_glsl_error(&loc, state, "C-style initialization requires the "
 2540                        "GL_ARB_shading_language_420pack extension");
 2541       return ir_rvalue::error_value(ctx);
 2542    }
 2543 
 2544    if (constructor_type->is_array()) {
 2545       return process_array_constructor(instructions, constructor_type, &loc,
 2546                                        &this->expressions, state);
 2547    }
 2548 
 2549    if (constructor_type->is_struct()) {
 2550       return process_record_constructor(instructions, constructor_type, &loc,
 2551                                         &this->expressions, state);
 2552    }
 2553 
 2554    return process_vec_mat_constructor(instructions, constructor_type, &loc,
 2555                                       &this->expressions, state);
 2556 }