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