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