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