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