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