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