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