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