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