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