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