ast_function.cpp revision ee88c4664016b11359c391cc62296bcbfcc5decd
1/*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#include "glsl_symbol_table.h"
25#include "ast.h"
26#include "glsl_types.h"
27#include "ir.h"
28#include "main/core.h" /* for MIN2 */
29
30static ir_rvalue *
31convert_component(ir_rvalue *src, const glsl_type *desired_type);
32
33static unsigned
34process_parameters(exec_list *instructions, exec_list *actual_parameters,
35		   exec_list *parameters,
36		   struct _mesa_glsl_parse_state *state)
37{
38   unsigned count = 0;
39
40   foreach_list (n, parameters) {
41      ast_node *const ast = exec_node_data(ast_node, n, link);
42      ir_rvalue *result = ast->hir(instructions, state);
43
44      ir_constant *const constant = result->constant_expression_value();
45      if (constant != NULL)
46	 result = constant;
47
48      actual_parameters->push_tail(result);
49      count++;
50   }
51
52   return count;
53}
54
55
56/**
57 * Generate a source prototype for a function signature
58 *
59 * \param return_type Return type of the function.  May be \c NULL.
60 * \param name        Name of the function.
61 * \param parameters  Parameter list for the function.  This may be either a
62 *                    formal or actual parameter list.  Only the type is used.
63 *
64 * \return
65 * A talloced string representing the prototype of the function.
66 */
67char *
68prototype_string(const glsl_type *return_type, const char *name,
69		 exec_list *parameters)
70{
71   char *str = NULL;
72
73   if (return_type != NULL)
74      str = talloc_asprintf(str, "%s ", return_type->name);
75
76   str = talloc_asprintf_append(str, "%s(", name);
77
78   const char *comma = "";
79   foreach_list(node, parameters) {
80      const ir_instruction *const param = (ir_instruction *) node;
81
82      str = talloc_asprintf_append(str, "%s%s", comma, param->type->name);
83      comma = ", ";
84   }
85
86   str = talloc_strdup_append(str, ")");
87   return str;
88}
89
90
91static ir_rvalue *
92process_call(exec_list *instructions, ir_function *f,
93	     YYLTYPE *loc, exec_list *actual_parameters,
94	     struct _mesa_glsl_parse_state *state)
95{
96   void *ctx = state;
97
98   ir_function_signature *sig = f->matching_signature(actual_parameters);
99
100   /* The instructions param will be used when the FINISHMEs below are done */
101   (void) instructions;
102
103   if (sig != NULL) {
104      /* Verify that 'out' and 'inout' actual parameters are lvalues.  This
105       * isn't done in ir_function::matching_signature because that function
106       * cannot generate the necessary diagnostics.
107       */
108      exec_list_iterator actual_iter = actual_parameters->iterator();
109      exec_list_iterator formal_iter = sig->parameters.iterator();
110
111      while (actual_iter.has_next()) {
112	 ir_rvalue *actual = (ir_rvalue *) actual_iter.get();
113	 ir_variable *formal = (ir_variable *) formal_iter.get();
114
115	 assert(actual != NULL);
116	 assert(formal != NULL);
117
118	 if ((formal->mode == ir_var_out)
119	     || (formal->mode == ir_var_inout)) {
120	    if (! actual->is_lvalue()) {
121	       /* FINISHME: Log a better diagnostic here.  There is no way
122		* FINISHME: to tell the user which parameter is invalid.
123		*/
124	       _mesa_glsl_error(loc, state, "`%s' parameter is not lvalue",
125				(formal->mode == ir_var_out) ? "out" : "inout");
126	    }
127	 }
128
129	 if (formal->type->is_numeric() || formal->type->is_boolean()) {
130	    ir_rvalue *converted = convert_component(actual, formal->type);
131	    actual->replace_with(converted);
132	 }
133
134	 actual_iter.next();
135	 formal_iter.next();
136      }
137
138      /* Always insert the call in the instruction stream, and return a deref
139       * of its return val if it returns a value, since we don't know if
140       * the rvalue is going to be assigned to anything or not.
141       */
142      ir_call *call = new(ctx) ir_call(sig, actual_parameters);
143      if (!sig->return_type->is_void()) {
144	 ir_variable *var;
145	 ir_dereference_variable *deref;
146
147	 var = new(ctx) ir_variable(sig->return_type,
148				    talloc_asprintf(ctx, "%s_retval",
149						    sig->function_name()),
150				    ir_var_temporary);
151	 instructions->push_tail(var);
152
153	 deref = new(ctx) ir_dereference_variable(var);
154	 ir_assignment *assign = new(ctx) ir_assignment(deref, call, NULL);
155	 instructions->push_tail(assign);
156	 if (state->language_version >= 120)
157	    var->constant_value = call->constant_expression_value();
158
159	 deref = new(ctx) ir_dereference_variable(var);
160	 return deref;
161      } else {
162	 instructions->push_tail(call);
163	 return NULL;
164      }
165   } else {
166      char *str = prototype_string(NULL, f->name, actual_parameters);
167
168      _mesa_glsl_error(loc, state, "no matching function for call to `%s'",
169		       str);
170      talloc_free(str);
171
172      const char *prefix = "candidates are: ";
173      foreach_list (node, &f->signatures) {
174	 ir_function_signature *sig = (ir_function_signature *) node;
175
176	 str = prototype_string(sig->return_type, f->name, &sig->parameters);
177	 _mesa_glsl_error(loc, state, "%s%s\n", prefix, str);
178	 talloc_free(str);
179
180	 prefix = "                ";
181      }
182
183      return ir_call::get_error_instruction(ctx);
184   }
185}
186
187
188static ir_rvalue *
189match_function_by_name(exec_list *instructions, const char *name,
190		       YYLTYPE *loc, exec_list *actual_parameters,
191		       struct _mesa_glsl_parse_state *state)
192{
193   void *ctx = state;
194   ir_function *f = state->symbols->get_function(name);
195
196   if (f == NULL) {
197      _mesa_glsl_error(loc, state, "function `%s' undeclared", name);
198      return ir_call::get_error_instruction(ctx);
199   }
200
201   /* Once we've determined that the function being called might exist, try
202    * to find an overload of the function that matches the parameters.
203    */
204   return process_call(instructions, f, loc, actual_parameters, state);
205}
206
207
208/**
209 * Perform automatic type conversion of constructor parameters
210 *
211 * This implements the rules in the "Conversion and Scalar Constructors"
212 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
213 */
214static ir_rvalue *
215convert_component(ir_rvalue *src, const glsl_type *desired_type)
216{
217   void *ctx = talloc_parent(src);
218   const unsigned a = desired_type->base_type;
219   const unsigned b = src->type->base_type;
220   ir_expression *result = NULL;
221
222   if (src->type->is_error())
223      return src;
224
225   assert(a <= GLSL_TYPE_BOOL);
226   assert(b <= GLSL_TYPE_BOOL);
227
228   if ((a == b) || (src->type->is_integer() && desired_type->is_integer()))
229      return src;
230
231   switch (a) {
232   case GLSL_TYPE_UINT:
233   case GLSL_TYPE_INT:
234      if (b == GLSL_TYPE_FLOAT)
235	 result = new(ctx) ir_expression(ir_unop_f2i, desired_type, src, NULL);
236      else {
237	 assert(b == GLSL_TYPE_BOOL);
238	 result = new(ctx) ir_expression(ir_unop_b2i, desired_type, src, NULL);
239      }
240      break;
241   case GLSL_TYPE_FLOAT:
242      switch (b) {
243      case GLSL_TYPE_UINT:
244	 result = new(ctx) ir_expression(ir_unop_u2f, desired_type, src, NULL);
245	 break;
246      case GLSL_TYPE_INT:
247	 result = new(ctx) ir_expression(ir_unop_i2f, desired_type, src, NULL);
248	 break;
249      case GLSL_TYPE_BOOL:
250	 result = new(ctx) ir_expression(ir_unop_b2f, desired_type, src, NULL);
251	 break;
252      }
253      break;
254   case GLSL_TYPE_BOOL:
255      switch (b) {
256      case GLSL_TYPE_UINT:
257      case GLSL_TYPE_INT:
258	 result = new(ctx) ir_expression(ir_unop_i2b, desired_type, src, NULL);
259	 break;
260      case GLSL_TYPE_FLOAT:
261	 result = new(ctx) ir_expression(ir_unop_f2b, desired_type, src, NULL);
262	 break;
263      }
264      break;
265   }
266
267   assert(result != NULL);
268
269   /* Try constant folding; it may fold in the conversion we just added. */
270   ir_constant *const constant = result->constant_expression_value();
271   return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result;
272}
273
274/**
275 * Dereference a specific component from a scalar, vector, or matrix
276 */
277static ir_rvalue *
278dereference_component(ir_rvalue *src, unsigned component)
279{
280   void *ctx = talloc_parent(src);
281   assert(component < src->type->components());
282
283   /* If the source is a constant, just create a new constant instead of a
284    * dereference of the existing constant.
285    */
286   ir_constant *constant = src->as_constant();
287   if (constant)
288      return new(ctx) ir_constant(constant, component);
289
290   if (src->type->is_scalar()) {
291      return src;
292   } else if (src->type->is_vector()) {
293      return new(ctx) ir_swizzle(src, component, 0, 0, 0, 1);
294   } else {
295      assert(src->type->is_matrix());
296
297      /* Dereference a row of the matrix, then call this function again to get
298       * a specific element from that row.
299       */
300      const int c = component / src->type->column_type()->vector_elements;
301      const int r = component % src->type->column_type()->vector_elements;
302      ir_constant *const col_index = new(ctx) ir_constant(c);
303      ir_dereference *const col = new(ctx) ir_dereference_array(src, col_index);
304
305      col->type = src->type->column_type();
306
307      return dereference_component(col, r);
308   }
309
310   assert(!"Should not get here.");
311   return NULL;
312}
313
314
315static ir_rvalue *
316process_array_constructor(exec_list *instructions,
317			  const glsl_type *constructor_type,
318			  YYLTYPE *loc, exec_list *parameters,
319			  struct _mesa_glsl_parse_state *state)
320{
321   void *ctx = state;
322   /* Array constructors come in two forms: sized and unsized.  Sized array
323    * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
324    * variables.  In this case the number of parameters must exactly match the
325    * specified size of the array.
326    *
327    * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
328    * are vec4 variables.  In this case the size of the array being constructed
329    * is determined by the number of parameters.
330    *
331    * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
332    *
333    *    "There must be exactly the same number of arguments as the size of
334    *    the array being constructed. If no size is present in the
335    *    constructor, then the array is explicitly sized to the number of
336    *    arguments provided. The arguments are assigned in order, starting at
337    *    element 0, to the elements of the constructed array. Each argument
338    *    must be the same type as the element type of the array, or be a type
339    *    that can be converted to the element type of the array according to
340    *    Section 4.1.10 "Implicit Conversions.""
341    */
342   exec_list actual_parameters;
343   const unsigned parameter_count =
344      process_parameters(instructions, &actual_parameters, parameters, state);
345
346   if ((parameter_count == 0)
347       || ((constructor_type->length != 0)
348	   && (constructor_type->length != parameter_count))) {
349      const unsigned min_param = (constructor_type->length == 0)
350	 ? 1 : constructor_type->length;
351
352      _mesa_glsl_error(loc, state, "array constructor must have %s %u "
353		       "parameter%s",
354		       (constructor_type->length != 0) ? "at least" : "exactly",
355		       min_param, (min_param <= 1) ? "" : "s");
356      return ir_call::get_error_instruction(ctx);
357   }
358
359   if (constructor_type->length == 0) {
360      constructor_type =
361	 glsl_type::get_array_instance(constructor_type->element_type(),
362				       parameter_count);
363      assert(constructor_type != NULL);
364      assert(constructor_type->length == parameter_count);
365   }
366
367   bool all_parameters_are_constant = true;
368
369   /* Type cast each parameter and, if possible, fold constants. */
370   foreach_list_safe(n, &actual_parameters) {
371      ir_rvalue *ir = (ir_rvalue *) n;
372      ir_rvalue *result = ir;
373
374      /* Apply implicit conversions (not the scalar constructor rules!) */
375      if (constructor_type->element_type()->is_float()) {
376	 const glsl_type *desired_type =
377	    glsl_type::get_instance(GLSL_TYPE_FLOAT,
378				    ir->type->vector_elements,
379				    ir->type->matrix_columns);
380	 result = convert_component(ir, desired_type);
381      }
382
383      if (result->type != constructor_type->element_type()) {
384	 _mesa_glsl_error(loc, state, "type error in array constructor: "
385			  "expected: %s, found %s",
386			  constructor_type->element_type()->name,
387			  result->type->name);
388      }
389
390      /* Attempt to convert the parameter to a constant valued expression.
391       * After doing so, track whether or not all the parameters to the
392       * constructor are trivially constant valued expressions.
393       */
394      ir_rvalue *const constant = result->constant_expression_value();
395
396      if (constant != NULL)
397         result = constant;
398      else
399         all_parameters_are_constant = false;
400
401      ir->replace_with(result);
402   }
403
404   if (all_parameters_are_constant)
405      return new(ctx) ir_constant(constructor_type, &actual_parameters);
406
407   ir_variable *var = new(ctx) ir_variable(constructor_type, "array_ctor",
408					   ir_var_temporary);
409   instructions->push_tail(var);
410
411   int i = 0;
412   foreach_list(node, &actual_parameters) {
413      ir_rvalue *rhs = (ir_rvalue *) node;
414      ir_rvalue *lhs = new(ctx) ir_dereference_array(var,
415						     new(ctx) ir_constant(i));
416
417      ir_instruction *assignment = new(ctx) ir_assignment(lhs, rhs, NULL);
418      instructions->push_tail(assignment);
419
420      i++;
421   }
422
423   return new(ctx) ir_dereference_variable(var);
424}
425
426
427/**
428 * Try to convert a record constructor to a constant expression
429 */
430static ir_constant *
431constant_record_constructor(const glsl_type *constructor_type,
432			    YYLTYPE *loc, exec_list *parameters,
433			    struct _mesa_glsl_parse_state *state)
434{
435   void *ctx = state;
436   bool all_parameters_are_constant = true;
437
438   exec_node *node = parameters->head;
439   for (unsigned i = 0; i < constructor_type->length; i++) {
440      ir_instruction *ir = (ir_instruction *) node;
441
442      if (node->is_tail_sentinel()) {
443	 _mesa_glsl_error(loc, state,
444			  "insufficient parameters to constructor for `%s'",
445			  constructor_type->name);
446	 return NULL;
447      }
448
449      if (ir->type != constructor_type->fields.structure[i].type) {
450	 _mesa_glsl_error(loc, state,
451			  "parameter type mismatch in constructor for `%s' "
452			  " (%s vs %s)",
453			  constructor_type->name,
454			  ir->type->name,
455			  constructor_type->fields.structure[i].type->name);
456	 return NULL;
457      }
458
459      if (ir->as_constant() == NULL)
460	 all_parameters_are_constant = false;
461
462      node = node->next;
463   }
464
465   if (!all_parameters_are_constant)
466      return NULL;
467
468   return new(ctx) ir_constant(constructor_type, parameters);
469}
470
471
472/**
473 * Generate data for a constant matrix constructor w/a single scalar parameter
474 *
475 * Matrix constructors in GLSL can be passed a single scalar of the
476 * approriate type.  In these cases, the resulting matrix is the identity
477 * matrix multipled by the specified scalar.  This function generates data for
478 * that matrix.
479 *
480 * \param type         Type of the desired matrix.
481 * \param initializer  Scalar value used to initialize the matrix diagonal.
482 * \param data         Location to store the resulting matrix.
483 */
484void
485generate_constructor_matrix(const glsl_type *type, ir_constant *initializer,
486			    ir_constant_data *data)
487{
488   assert(type->base_type == GLSL_TYPE_FLOAT);
489   assert(initializer->type->is_scalar());
490
491   for (unsigned i = 0; i < type->components(); i++)
492      data->f[i] = 0;
493
494   for (unsigned i = 0; i < type->matrix_columns; i++) {
495      /* The array offset of the ith row and column of the matrix. */
496      const unsigned idx = (i * type->vector_elements) + i;
497
498      data->f[idx] = initializer->value.f[0];
499   }
500}
501
502
503/**
504 * Generate data for a constant vector constructor w/a single scalar parameter
505 *
506 * Vector constructors in GLSL can be passed a single scalar of the
507 * approriate type.  In these cases, the resulting vector contains the specified
508 * value in all components.  This function generates data for that vector.
509 *
510 * \param type         Type of the desired vector.
511 * \param initializer  Scalar value used to initialize the vector.
512 * \param data         Location to store the resulting vector data.
513 */
514void
515generate_constructor_vector(const glsl_type *type, ir_constant *initializer,
516			    ir_constant_data *data)
517{
518   switch (type->base_type) {
519   case GLSL_TYPE_UINT:
520   case GLSL_TYPE_INT:
521      for (unsigned i = 0; i < type->components(); i++)
522	 data->u[i] = initializer->value.u[0];
523
524      break;
525
526   case GLSL_TYPE_FLOAT:
527      for (unsigned i = 0; i < type->components(); i++)
528	 data->f[i] = initializer->value.f[0];
529
530      break;
531
532   case GLSL_TYPE_BOOL:
533      for (unsigned i = 0; i < type->components(); i++)
534	 data->b[i] = initializer->value.b[0];
535
536      break;
537
538   default:
539      assert(!"Should not get here.");
540      break;
541   }
542}
543
544
545/**
546 * Determine if a list consists of a single scalar r-value
547 */
548bool
549single_scalar_parameter(exec_list *parameters)
550{
551   const ir_rvalue *const p = (ir_rvalue *) parameters->head;
552   assert(((ir_rvalue *)p)->as_rvalue() != NULL);
553
554   return (p->type->is_scalar() && p->next->is_tail_sentinel());
555}
556
557
558/**
559 * Generate inline code for a vector constructor
560 *
561 * The generated constructor code will consist of a temporary variable
562 * declaration of the same type as the constructor.  A sequence of assignments
563 * from constructor parameters to the temporary will follow.
564 *
565 * \return
566 * An \c ir_dereference_variable of the temprorary generated in the constructor
567 * body.
568 */
569ir_rvalue *
570emit_inline_vector_constructor(const glsl_type *type,
571			       exec_list *instructions,
572			       exec_list *parameters,
573			       void *ctx)
574{
575   assert(!parameters->is_empty());
576
577   ir_variable *var = new(ctx) ir_variable(type, "vec_ctor", ir_var_temporary);
578   instructions->push_tail(var);
579
580   /* There are two kinds of vector constructors.
581    *
582    *  - Construct a vector from a single scalar by replicating that scalar to
583    *    all components of the vector.
584    *
585    *  - Construct a vector from an arbirary combination of vectors and
586    *    scalars.  The components of the constructor parameters are assigned
587    *    to the vector in order until the vector is full.
588    */
589   const unsigned lhs_components = type->components();
590   if (single_scalar_parameter(parameters)) {
591      ir_rvalue *first_param = (ir_rvalue *)parameters->head;
592      ir_rvalue *rhs = new(ctx) ir_swizzle(first_param, 0, 0, 0, 0,
593					   lhs_components);
594      ir_dereference_variable *lhs = new(ctx) ir_dereference_variable(var);
595      const unsigned mask = (1U << lhs_components) - 1;
596
597      assert(rhs->type == lhs->type);
598
599      ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL, mask);
600      instructions->push_tail(inst);
601   } else {
602      unsigned base_component = 0;
603      ir_constant_data data;
604      unsigned constant_mask = 0;
605
606      memset(&data, 0, sizeof(data));
607
608      foreach_list(node, parameters) {
609	 ir_rvalue *param = (ir_rvalue *) node;
610	 unsigned rhs_components = param->type->components();
611
612	 /* Do not try to assign more components to the vector than it has!
613	  */
614	 if ((rhs_components + base_component) > lhs_components) {
615	    rhs_components = lhs_components - base_component;
616	 }
617
618	 const ir_constant *const c = param->as_constant();
619	 if (c != NULL) {
620	    for (unsigned i = 0; i < rhs_components; i++) {
621	       switch (c->type->base_type) {
622	       case GLSL_TYPE_UINT:
623		  data.u[i + base_component] = c->get_uint_component(i);
624		  break;
625	       case GLSL_TYPE_INT:
626		  data.i[i + base_component] = c->get_int_component(i);
627		  break;
628	       case GLSL_TYPE_FLOAT:
629		  data.f[i + base_component] = c->get_float_component(i);
630		  break;
631	       case GLSL_TYPE_BOOL:
632		  data.b[i + base_component] = c->get_bool_component(i);
633		  break;
634	       default:
635		  assert(!"Should not get here.");
636		  break;
637	       }
638	    }
639
640	    /* Mask of fields to be written in the assignment.
641	     */
642	    constant_mask |= ((1U << rhs_components) - 1) << base_component;
643	 }
644
645	 /* Advance the component index by the number of components that were
646	  * just assigned.
647	  */
648	 base_component += rhs_components;
649      }
650
651      if (constant_mask != 0) {
652	 ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
653	 ir_rvalue *rhs = new(ctx) ir_constant(var->type, &data);
654
655	 ir_instruction *inst =
656	    new(ctx) ir_assignment(lhs, rhs, NULL, constant_mask);
657	 instructions->push_tail(inst);
658      }
659
660      base_component = 0;
661      foreach_list(node, parameters) {
662	 ir_rvalue *param = (ir_rvalue *) node;
663	 unsigned rhs_components = param->type->components();
664
665	 /* Do not try to assign more components to the vector than it has!
666	  */
667	 if ((rhs_components + base_component) > lhs_components) {
668	    rhs_components = lhs_components - base_component;
669	 }
670
671	 const ir_constant *const c = param->as_constant();
672	 if (c == NULL) {
673	    /* Generate a swizzle that puts the first element of the source at
674	     * the location of the first element of the destination.
675	     */
676	    unsigned swiz[4] = { 0, 0, 0, 0 };
677	    for (unsigned i = 0; i < rhs_components; i++)
678	       swiz[i + base_component] = i;
679
680	    /* Mask of fields to be written in the assignment.
681	     */
682	    const unsigned write_mask = ((1U << rhs_components) - 1)
683	       << base_component;
684
685	    ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
686	    ir_rvalue *rhs = new(ctx) ir_swizzle(param, swiz, lhs_components);
687
688	    ir_instruction *inst =
689	       new(ctx) ir_assignment(lhs, rhs, NULL, write_mask);
690	    instructions->push_tail(inst);
691	 }
692
693	 /* Advance the component index by the number of components that were
694	  * just assigned.
695	  */
696	 base_component += rhs_components;
697      }
698   }
699   return new(ctx) ir_dereference_variable(var);
700}
701
702
703/**
704 * Generate assignment of a portion of a vector to a portion of a matrix column
705 *
706 * \param src_base  First component of the source to be used in assignment
707 * \param column    Column of destination to be assiged
708 * \param row_base  First component of the destination column to be assigned
709 * \param count     Number of components to be assigned
710 *
711 * \note
712 * \c src_base + \c count must be less than or equal to the number of components
713 * in the source vector.
714 */
715ir_instruction *
716assign_to_matrix_column(ir_variable *var, unsigned column, unsigned row_base,
717			ir_rvalue *src, unsigned src_base, unsigned count,
718			void *mem_ctx)
719{
720   ir_constant *col_idx = new(mem_ctx) ir_constant(column);
721   ir_dereference *column_ref = new(mem_ctx) ir_dereference_array(var, col_idx);
722
723   assert(column_ref->type->components() >= (row_base + count));
724   assert(src->type->components() >= (src_base + count));
725
726   /* Generate a swizzle that puts the first element of the source at the
727    * location of the first element of the destination.
728    */
729   unsigned swiz[4] = { src_base, src_base, src_base, src_base };
730   for (unsigned i = 0; i < count; i++)
731      swiz[i + row_base] = src_base + i;
732
733   ir_rvalue *const rhs =
734      new(mem_ctx) ir_swizzle(src, swiz, column_ref->type->components());
735
736   /* Mask of fields to be written in the assignment.
737    */
738   const unsigned write_mask = ((1U << count) - 1) << row_base;
739
740   return new(mem_ctx) ir_assignment(column_ref, rhs, NULL, write_mask);
741}
742
743
744/**
745 * Generate inline code for a matrix constructor
746 *
747 * The generated constructor code will consist of a temporary variable
748 * declaration of the same type as the constructor.  A sequence of assignments
749 * from constructor parameters to the temporary will follow.
750 *
751 * \return
752 * An \c ir_dereference_variable of the temprorary generated in the constructor
753 * body.
754 */
755ir_rvalue *
756emit_inline_matrix_constructor(const glsl_type *type,
757			       exec_list *instructions,
758			       exec_list *parameters,
759			       void *ctx)
760{
761   assert(!parameters->is_empty());
762
763   ir_variable *var = new(ctx) ir_variable(type, "mat_ctor", ir_var_temporary);
764   instructions->push_tail(var);
765
766   /* There are three kinds of matrix constructors.
767    *
768    *  - Construct a matrix from a single scalar by replicating that scalar to
769    *    along the diagonal of the matrix and setting all other components to
770    *    zero.
771    *
772    *  - Construct a matrix from an arbirary combination of vectors and
773    *    scalars.  The components of the constructor parameters are assigned
774    *    to the matrix in colum-major order until the matrix is full.
775    *
776    *  - Construct a matrix from a single matrix.  The source matrix is copied
777    *    to the upper left portion of the constructed matrix, and the remaining
778    *    elements take values from the identity matrix.
779    */
780   ir_rvalue *const first_param = (ir_rvalue *) parameters->head;
781   if (single_scalar_parameter(parameters)) {
782      /* Assign the scalar to the X component of a vec4, and fill the remaining
783       * components with zero.
784       */
785      ir_variable *rhs_var =
786	 new(ctx) ir_variable(glsl_type::vec4_type, "mat_ctor_vec",
787			      ir_var_temporary);
788      instructions->push_tail(rhs_var);
789
790      ir_constant_data zero;
791      zero.f[0] = 0.0;
792      zero.f[1] = 0.0;
793      zero.f[2] = 0.0;
794      zero.f[3] = 0.0;
795
796      ir_instruction *inst =
797	 new(ctx) ir_assignment(new(ctx) ir_dereference_variable(rhs_var),
798				new(ctx) ir_constant(rhs_var->type, &zero),
799				NULL);
800      instructions->push_tail(inst);
801
802      ir_dereference *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
803
804      inst = new(ctx) ir_assignment(rhs_ref, first_param, NULL, 0x01);
805      instructions->push_tail(inst);
806
807      /* Assign the temporary vector to each column of the destination matrix
808       * with a swizzle that puts the X component on the diagonal of the
809       * matrix.  In some cases this may mean that the X component does not
810       * get assigned into the column at all (i.e., when the matrix has more
811       * columns than rows).
812       */
813      static const unsigned rhs_swiz[4][4] = {
814	 { 0, 1, 1, 1 },
815	 { 1, 0, 1, 1 },
816	 { 1, 1, 0, 1 },
817	 { 1, 1, 1, 0 }
818      };
819
820      const unsigned cols_to_init = MIN2(type->matrix_columns,
821					 type->vector_elements);
822      for (unsigned i = 0; i < cols_to_init; i++) {
823	 ir_constant *const col_idx = new(ctx) ir_constant(i);
824	 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
825
826	 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
827	 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, rhs_swiz[i],
828						    type->vector_elements);
829
830	 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
831	 instructions->push_tail(inst);
832      }
833
834      for (unsigned i = cols_to_init; i < type->matrix_columns; i++) {
835	 ir_constant *const col_idx = new(ctx) ir_constant(i);
836	 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
837
838	 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
839	 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, 1, 1, 1, 1,
840						    type->vector_elements);
841
842	 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
843	 instructions->push_tail(inst);
844      }
845   } else if (first_param->type->is_matrix()) {
846      /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
847       *
848       *     "If a matrix is constructed from a matrix, then each component
849       *     (column i, row j) in the result that has a corresponding
850       *     component (column i, row j) in the argument will be initialized
851       *     from there. All other components will be initialized to the
852       *     identity matrix. If a matrix argument is given to a matrix
853       *     constructor, it is an error to have any other arguments."
854       */
855      assert(first_param->next->is_tail_sentinel());
856      ir_rvalue *const src_matrix = first_param;
857
858      /* If the source matrix is smaller, pre-initialize the relavent parts of
859       * the destination matrix to the identity matrix.
860       */
861      if ((src_matrix->type->matrix_columns < var->type->matrix_columns)
862	  || (src_matrix->type->vector_elements < var->type->vector_elements)) {
863
864	 /* If the source matrix has fewer rows, every column of the destination
865	  * must be initialized.  Otherwise only the columns in the destination
866	  * that do not exist in the source must be initialized.
867	  */
868	 unsigned col =
869	    (src_matrix->type->vector_elements < var->type->vector_elements)
870	    ? 0 : src_matrix->type->matrix_columns;
871
872	 const glsl_type *const col_type = var->type->column_type();
873	 for (/* empty */; col < var->type->matrix_columns; col++) {
874	    ir_constant_data ident;
875
876	    ident.f[0] = 0.0;
877	    ident.f[1] = 0.0;
878	    ident.f[2] = 0.0;
879	    ident.f[3] = 0.0;
880
881	    ident.f[col] = 1.0;
882
883	    ir_rvalue *const rhs = new(ctx) ir_constant(col_type, &ident);
884
885	    ir_rvalue *const lhs =
886	       new(ctx) ir_dereference_array(var, new(ctx) ir_constant(col));
887
888	    ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
889	    instructions->push_tail(inst);
890	 }
891      }
892
893      /* Assign columns from the source matrix to the destination matrix.
894       *
895       * Since the parameter will be used in the RHS of multiple assignments,
896       * generate a temporary and copy the paramter there.
897       */
898      ir_variable *const rhs_var =
899	 new(ctx) ir_variable(first_param->type, "mat_ctor_mat",
900			      ir_var_temporary);
901      instructions->push_tail(rhs_var);
902
903      ir_dereference *const rhs_var_ref =
904	 new(ctx) ir_dereference_variable(rhs_var);
905      ir_instruction *const inst =
906	 new(ctx) ir_assignment(rhs_var_ref, first_param, NULL);
907      instructions->push_tail(inst);
908
909
910      unsigned swiz[4] = { 0, 0, 0, 0 };
911      for (unsigned i = 1; i < src_matrix->type->vector_elements; i++)
912	 swiz[i] = i;
913
914      const unsigned last_col = MIN2(src_matrix->type->matrix_columns,
915				     var->type->matrix_columns);
916      const unsigned write_mask = (1U << var->type->vector_elements) - 1;
917
918      for (unsigned i = 0; i < last_col; i++) {
919	 ir_dereference *const lhs =
920	    new(ctx) ir_dereference_array(var, new(ctx) ir_constant(i));
921	 ir_rvalue *const rhs_col =
922	    new(ctx) ir_dereference_array(rhs_var, new(ctx) ir_constant(i));
923
924	 /* If one matrix has columns that are smaller than the columns of the
925	  * other matrix, wrap the column access of the larger with a swizzle
926	  * so that the LHS and RHS of the assignment have the same size (and
927	  * therefore have the same type).
928	  *
929	  * It would be perfectly valid to unconditionally generate the
930	  * swizzles, this this will typically result in a more compact IR tree.
931	  */
932	 ir_rvalue *rhs;
933	 if (lhs->type->vector_elements != rhs_col->type->vector_elements) {
934	    rhs = new(ctx) ir_swizzle(rhs_col, swiz,
935				      lhs->type->vector_elements);
936	 } else {
937	    rhs = rhs_col;
938	 }
939
940	 assert(lhs->type == rhs->type);
941
942	 ir_instruction *inst =
943	    new(ctx) ir_assignment(lhs, rhs, NULL, write_mask);
944	 instructions->push_tail(inst);
945      }
946   } else {
947      const unsigned cols = type->matrix_columns;
948      const unsigned rows = type->vector_elements;
949      unsigned col_idx = 0;
950      unsigned row_idx = 0;
951
952      foreach_list (node, parameters) {
953	 ir_rvalue *const rhs = (ir_rvalue *) node;
954	 const unsigned components_remaining_this_column = rows - row_idx;
955	 unsigned rhs_components = rhs->type->components();
956	 unsigned rhs_base = 0;
957
958	 /* Since the parameter might be used in the RHS of two assignments,
959	  * generate a temporary and copy the paramter there.
960	  */
961	 ir_variable *rhs_var =
962	    new(ctx) ir_variable(rhs->type, "mat_ctor_vec", ir_var_temporary);
963	 instructions->push_tail(rhs_var);
964
965	 ir_dereference *rhs_var_ref =
966	    new(ctx) ir_dereference_variable(rhs_var);
967	 ir_instruction *inst = new(ctx) ir_assignment(rhs_var_ref, rhs, NULL);
968	 instructions->push_tail(inst);
969
970	 /* Assign the current parameter to as many components of the matrix
971	  * as it will fill.
972	  *
973	  * NOTE: A single vector parameter can span two matrix columns.  A
974	  * single vec4, for example, can completely fill a mat2.
975	  */
976	 if (rhs_components >= components_remaining_this_column) {
977	    const unsigned count = MIN2(rhs_components,
978					components_remaining_this_column);
979
980	    rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
981
982	    ir_instruction *inst = assign_to_matrix_column(var, col_idx,
983							   row_idx,
984							   rhs_var_ref, 0,
985							   count, ctx);
986	    instructions->push_tail(inst);
987
988	    rhs_base = count;
989
990	    col_idx++;
991	    row_idx = 0;
992	 }
993
994	 /* If there is data left in the parameter and components left to be
995	  * set in the destination, emit another assignment.  It is possible
996	  * that the assignment could be of a vec4 to the last element of the
997	  * matrix.  In this case col_idx==cols, but there is still data
998	  * left in the source parameter.  Obviously, don't emit an assignment
999	  * to data outside the destination matrix.
1000	  */
1001	 if ((col_idx < cols) && (rhs_base < rhs_components)) {
1002	    const unsigned count = rhs_components - rhs_base;
1003
1004	    rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
1005
1006	    ir_instruction *inst = assign_to_matrix_column(var, col_idx,
1007							   row_idx,
1008							   rhs_var_ref,
1009							   rhs_base,
1010							   count, ctx);
1011	    instructions->push_tail(inst);
1012
1013	    row_idx += count;
1014	 }
1015      }
1016   }
1017
1018   return new(ctx) ir_dereference_variable(var);
1019}
1020
1021
1022ir_rvalue *
1023ast_function_expression::hir(exec_list *instructions,
1024			     struct _mesa_glsl_parse_state *state)
1025{
1026   void *ctx = state;
1027   /* There are three sorts of function calls.
1028    *
1029    * 1. constructors - The first subexpression is an ast_type_specifier.
1030    * 2. methods - Only the .length() method of array types.
1031    * 3. functions - Calls to regular old functions.
1032    *
1033    * Method calls are actually detected when the ast_field_selection
1034    * expression is handled.
1035    */
1036   if (is_constructor()) {
1037      const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
1038      YYLTYPE loc = type->get_location();
1039      const char *name;
1040
1041      const glsl_type *const constructor_type = type->glsl_type(& name, state);
1042
1043
1044      /* Constructors for samplers are illegal.
1045       */
1046      if (constructor_type->is_sampler()) {
1047	 _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
1048			  constructor_type->name);
1049	 return ir_call::get_error_instruction(ctx);
1050      }
1051
1052      if (constructor_type->is_array()) {
1053	 if (state->language_version <= 110) {
1054	    _mesa_glsl_error(& loc, state,
1055			     "array constructors forbidden in GLSL 1.10");
1056	    return ir_call::get_error_instruction(ctx);
1057	 }
1058
1059	 return process_array_constructor(instructions, constructor_type,
1060					  & loc, &this->expressions, state);
1061      }
1062
1063      /* There are two kinds of constructor call.  Constructors for built-in
1064       * language types, such as mat4 and vec2, are free form.  The only
1065       * requirement is that the parameters must provide enough values of the
1066       * correct scalar type.  Constructors for arrays and structures must
1067       * have the exact number of parameters with matching types in the
1068       * correct order.  These constructors follow essentially the same type
1069       * matching rules as functions.
1070       */
1071      if (!constructor_type->is_numeric() && !constructor_type->is_boolean())
1072	 return ir_call::get_error_instruction(ctx);
1073
1074      /* Total number of components of the type being constructed. */
1075      const unsigned type_components = constructor_type->components();
1076
1077      /* Number of components from parameters that have actually been
1078       * consumed.  This is used to perform several kinds of error checking.
1079       */
1080      unsigned components_used = 0;
1081
1082      unsigned matrix_parameters = 0;
1083      unsigned nonmatrix_parameters = 0;
1084      exec_list actual_parameters;
1085
1086      foreach_list (n, &this->expressions) {
1087	 ast_node *ast = exec_node_data(ast_node, n, link);
1088	 ir_rvalue *result = ast->hir(instructions, state)->as_rvalue();
1089
1090	 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1091	  *
1092	  *    "It is an error to provide extra arguments beyond this
1093	  *    last used argument."
1094	  */
1095	 if (components_used >= type_components) {
1096	    _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
1097			     "constructor",
1098			     constructor_type->name);
1099	    return ir_call::get_error_instruction(ctx);
1100	 }
1101
1102	 if (!result->type->is_numeric() && !result->type->is_boolean()) {
1103	    _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
1104			     "non-numeric data type",
1105			     constructor_type->name);
1106	    return ir_call::get_error_instruction(ctx);
1107	 }
1108
1109	 /* Count the number of matrix and nonmatrix parameters.  This
1110	  * is used below to enforce some of the constructor rules.
1111	  */
1112	 if (result->type->is_matrix())
1113	    matrix_parameters++;
1114	 else
1115	    nonmatrix_parameters++;
1116
1117	 actual_parameters.push_tail(result);
1118	 components_used += result->type->components();
1119      }
1120
1121      /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1122       *
1123       *    "It is an error to construct matrices from other matrices. This
1124       *    is reserved for future use."
1125       */
1126      if ((state->language_version <= 110) && (matrix_parameters > 0)
1127	  && constructor_type->is_matrix()) {
1128	 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
1129			  "matrix in GLSL 1.10",
1130			  constructor_type->name);
1131	 return ir_call::get_error_instruction(ctx);
1132      }
1133
1134      /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1135       *
1136       *    "If a matrix argument is given to a matrix constructor, it is
1137       *    an error to have any other arguments."
1138       */
1139      if ((matrix_parameters > 0)
1140	  && ((matrix_parameters + nonmatrix_parameters) > 1)
1141	  && constructor_type->is_matrix()) {
1142	 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
1143			  "matrix must be only parameter",
1144			  constructor_type->name);
1145	 return ir_call::get_error_instruction(ctx);
1146      }
1147
1148      /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1149       *
1150       *    "In these cases, there must be enough components provided in the
1151       *    arguments to provide an initializer for every component in the
1152       *    constructed value."
1153       */
1154      if ((components_used < type_components) && (components_used != 1)) {
1155	 _mesa_glsl_error(& loc, state, "too few components to construct "
1156			  "`%s'",
1157			  constructor_type->name);
1158	 return ir_call::get_error_instruction(ctx);
1159      }
1160
1161      /* Later, we cast each parameter to the same base type as the
1162       * constructor.  Since there are no non-floating point matrices, we
1163       * need to break them up into a series of column vectors.
1164       */
1165      if (constructor_type->base_type != GLSL_TYPE_FLOAT) {
1166	 foreach_list_safe(n, &actual_parameters) {
1167	    ir_rvalue *matrix = (ir_rvalue *) n;
1168
1169	    if (!matrix->type->is_matrix())
1170	       continue;
1171
1172	    /* Create a temporary containing the matrix. */
1173	    ir_variable *var = new(ctx) ir_variable(matrix->type, "matrix_tmp",
1174						    ir_var_temporary);
1175	    instructions->push_tail(var);
1176	    instructions->push_tail(new(ctx) ir_assignment(new(ctx)
1177	       ir_dereference_variable(var), matrix, NULL));
1178	    var->constant_value = matrix->constant_expression_value();
1179
1180	    /* Replace the matrix with dereferences of its columns. */
1181	    for (int i = 0; i < matrix->type->matrix_columns; i++) {
1182	       matrix->insert_before(new (ctx) ir_dereference_array(var,
1183		  new(ctx) ir_constant(i)));
1184	    }
1185	    matrix->remove();
1186	 }
1187      }
1188
1189      bool all_parameters_are_constant = true;
1190
1191      /* Type cast each parameter and, if possible, fold constants.*/
1192      foreach_list_safe(n, &actual_parameters) {
1193	 ir_rvalue *ir = (ir_rvalue *) n;
1194
1195	 const glsl_type *desired_type =
1196	    glsl_type::get_instance(constructor_type->base_type,
1197				    ir->type->vector_elements,
1198				    ir->type->matrix_columns);
1199	 ir_rvalue *result = convert_component(ir, desired_type);
1200
1201	 /* Attempt to convert the parameter to a constant valued expression.
1202	  * After doing so, track whether or not all the parameters to the
1203	  * constructor are trivially constant valued expressions.
1204	  */
1205	 ir_rvalue *const constant = result->constant_expression_value();
1206
1207	 if (constant != NULL)
1208	    result = constant;
1209	 else
1210	    all_parameters_are_constant = false;
1211
1212	 if (result != ir) {
1213	    ir->replace_with(result);
1214	 }
1215      }
1216
1217      /* If all of the parameters are trivially constant, create a
1218       * constant representing the complete collection of parameters.
1219       */
1220      if (all_parameters_are_constant) {
1221	 if (components_used >= type_components)
1222	    return new(ctx) ir_constant(constructor_type,
1223					& actual_parameters);
1224
1225	 /* The above case must handle all scalar constructors.
1226	  */
1227	 assert(constructor_type->is_vector()
1228		|| constructor_type->is_matrix());
1229
1230	 /* Constructors with exactly one component are special for
1231	  * vectors and matrices.  For vectors it causes all elements of
1232	  * the vector to be filled with the value.  For matrices it
1233	  * causes the matrix to be filled with 0 and the diagonal to be
1234	  * filled with the value.
1235	  */
1236	 ir_constant_data data = { { 0 } };
1237	 ir_constant *const initializer =
1238	    (ir_constant *) actual_parameters.head;
1239	 if (constructor_type->is_matrix())
1240	    generate_constructor_matrix(constructor_type, initializer,
1241					&data);
1242	 else
1243	    generate_constructor_vector(constructor_type, initializer,
1244					&data);
1245
1246	 return new(ctx) ir_constant(constructor_type, &data);
1247      } else if (constructor_type->is_scalar()) {
1248	 return dereference_component((ir_rvalue *) actual_parameters.head,
1249				      0);
1250      } else if (constructor_type->is_vector()) {
1251	 return emit_inline_vector_constructor(constructor_type,
1252					       instructions,
1253					       &actual_parameters,
1254					       ctx);
1255      } else {
1256	 assert(constructor_type->is_matrix());
1257	 return emit_inline_matrix_constructor(constructor_type,
1258					       instructions,
1259					       &actual_parameters,
1260					       ctx);
1261      }
1262   } else {
1263      const ast_expression *id = subexpressions[0];
1264      YYLTYPE loc = id->get_location();
1265      exec_list actual_parameters;
1266
1267      process_parameters(instructions, &actual_parameters, &this->expressions,
1268			 state);
1269
1270      const glsl_type *const type =
1271	 state->symbols->get_type(id->primary_expression.identifier);
1272
1273      if ((type != NULL) && type->is_record()) {
1274	 ir_constant *constant =
1275	    constant_record_constructor(type, &loc, &actual_parameters, state);
1276
1277	 if (constant != NULL)
1278	    return constant;
1279      }
1280
1281      return match_function_by_name(instructions,
1282				    id->primary_expression.identifier, & loc,
1283				    &actual_parameters, state);
1284   }
1285
1286   return ir_call::get_error_instruction(ctx);
1287}
1288