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