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