blob: f1ab6f0c5a1c3d8597baddc8d9c4b4622c311121 [file] [log] [blame]
Ian Romanick548fa292010-03-15 13:04:13 -07001/*
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
Ian Romanick8bde4ce2010-03-19 11:57:24 -070024#include "glsl_symbol_table.h"
Ian Romanick548fa292010-03-15 13:04:13 -070025#include "ast.h"
26#include "glsl_types.h"
27#include "ir.h"
28
Ian Romanick68515ee2010-03-31 16:28:51 -070029static unsigned
30process_parameters(exec_list *instructions, exec_list *actual_parameters,
Ian Romanick304ea902010-05-10 11:17:53 -070031 exec_list *parameters,
Ian Romanick68515ee2010-03-31 16:28:51 -070032 struct _mesa_glsl_parse_state *state)
33{
Ian Romanick68515ee2010-03-31 16:28:51 -070034 unsigned count = 0;
35
Ian Romanick304ea902010-05-10 11:17:53 -070036 foreach_list (n, parameters) {
37 ast_node *const ast = exec_node_data(ast_node, n, link);
Ian Romanick1a872b12010-06-09 17:31:02 -070038 ir_rvalue *result = ast->hir(instructions, state);
39
40 ir_constant *const constant = result->constant_expression_value();
41 if (constant != NULL)
42 result = constant;
Ian Romanick68515ee2010-03-31 16:28:51 -070043
Ian Romanick3521f0b2010-05-10 10:47:14 -070044 actual_parameters->push_tail(result);
45 count++;
Ian Romanick68515ee2010-03-31 16:28:51 -070046 }
47
48 return count;
49}
50
51
52static ir_rvalue *
53process_call(exec_list *instructions, ir_function *f,
54 YYLTYPE *loc, exec_list *actual_parameters,
55 struct _mesa_glsl_parse_state *state)
56{
Carl Worth1660a292010-06-23 18:11:51 -070057 void *ctx = talloc_parent(state);
58
Ian Romanick68515ee2010-03-31 16:28:51 -070059 const ir_function_signature *sig =
60 f->matching_signature(actual_parameters);
61
62 /* The instructions param will be used when the FINISHMEs below are done */
63 (void) instructions;
64
65 if (sig != NULL) {
Ian Romanickc35bb002010-04-02 15:51:02 -070066 /* Verify that 'out' and 'inout' actual parameters are lvalues. This
67 * isn't done in ir_function::matching_signature because that function
68 * cannot generate the necessary diagnostics.
69 */
70 exec_list_iterator actual_iter = actual_parameters->iterator();
71 exec_list_iterator formal_iter = sig->parameters.iterator();
72
73 while (actual_iter.has_next()) {
Eric Anholtf1ddca92010-04-07 12:35:34 -070074 ir_rvalue *actual = (ir_rvalue *) actual_iter.get();
75 ir_variable *formal = (ir_variable *) formal_iter.get();
Ian Romanickc35bb002010-04-02 15:51:02 -070076
77 assert(actual != NULL);
78 assert(formal != NULL);
79
80 if ((formal->mode == ir_var_out)
81 || (formal->mode == ir_var_inout)) {
82 if (! actual->is_lvalue()) {
83 /* FINISHME: Log a better diagnostic here. There is no way
84 * FINISHME: to tell the user which parameter is invalid.
85 */
86 _mesa_glsl_error(loc, state, "`%s' parameter is not lvalue",
87 (formal->mode == ir_var_out) ? "out" : "inout");
88 }
89 }
90
91 actual_iter.next();
92 formal_iter.next();
93 }
94
Ian Romanick68515ee2010-03-31 16:28:51 -070095 /* FINISHME: The list of actual parameters needs to be modified to
96 * FINISHME: include any necessary conversions.
97 */
Carl Worth1660a292010-06-23 18:11:51 -070098 return new(ctx) ir_call(sig, actual_parameters);
Ian Romanick68515ee2010-03-31 16:28:51 -070099 } else {
100 /* FINISHME: Log a better error message here. G++ will show the types
101 * FINISHME: of the actual parameters and the set of candidate
102 * FINISHME: functions. A different error should also be logged when
103 * FINISHME: multiple functions match.
104 */
105 _mesa_glsl_error(loc, state, "no matching function for call to `%s'",
106 f->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700107 return ir_call::get_error_instruction(ctx);
Ian Romanick68515ee2010-03-31 16:28:51 -0700108 }
109}
110
111
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700112static ir_rvalue *
Ian Romanickf4749612010-03-15 13:26:02 -0700113match_function_by_name(exec_list *instructions, const char *name,
Ian Romanickc0771312010-06-09 17:23:26 -0700114 YYLTYPE *loc, exec_list *actual_parameters,
Ian Romanickf4749612010-03-15 13:26:02 -0700115 struct _mesa_glsl_parse_state *state)
116{
Carl Worthe01193a2010-06-23 18:25:04 -0700117 void *ctx = talloc_parent(state);
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700118 ir_function *f = state->symbols->get_function(name);
Ian Romanickf4749612010-03-15 13:26:02 -0700119
120 if (f == NULL) {
121 _mesa_glsl_error(loc, state, "function `%s' undeclared", name);
Carl Worthe01193a2010-06-23 18:25:04 -0700122 return ir_call::get_error_instruction(ctx);
Ian Romanickf4749612010-03-15 13:26:02 -0700123 }
124
Ian Romanickc0771312010-06-09 17:23:26 -0700125 /* Once we've determined that the function being called might exist, try
126 * to find an overload of the function that matches the parameters.
Ian Romanickf4749612010-03-15 13:26:02 -0700127 */
Ian Romanickc0771312010-06-09 17:23:26 -0700128 return process_call(instructions, f, loc, actual_parameters, state);
Ian Romanickf4749612010-03-15 13:26:02 -0700129}
130
131
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700132/**
133 * Perform automatic type conversion of constructor parameters
134 */
135static ir_rvalue *
136convert_component(ir_rvalue *src, const glsl_type *desired_type)
137{
Carl Worth1660a292010-06-23 18:11:51 -0700138 void *ctx = talloc_parent(src);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700139 const unsigned a = desired_type->base_type;
140 const unsigned b = src->type->base_type;
Ian Romanick00eb4662010-06-07 15:08:04 -0700141 ir_expression *result = NULL;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700142
143 if (src->type->is_error())
144 return src;
145
146 assert(a <= GLSL_TYPE_BOOL);
147 assert(b <= GLSL_TYPE_BOOL);
148
149 if ((a == b) || (src->type->is_integer() && desired_type->is_integer()))
150 return src;
151
152 switch (a) {
153 case GLSL_TYPE_UINT:
154 case GLSL_TYPE_INT:
155 if (b == GLSL_TYPE_FLOAT)
Carl Worth1660a292010-06-23 18:11:51 -0700156 result = new(ctx) ir_expression(ir_unop_f2i, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700157 else {
158 assert(b == GLSL_TYPE_BOOL);
Carl Worth1660a292010-06-23 18:11:51 -0700159 result = new(ctx) ir_expression(ir_unop_b2i, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700160 }
Ian Romanick565185c2010-06-11 13:49:00 -0700161 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700162 case GLSL_TYPE_FLOAT:
163 switch (b) {
164 case GLSL_TYPE_UINT:
Carl Worth1660a292010-06-23 18:11:51 -0700165 result = new(ctx) ir_expression(ir_unop_u2f, desired_type, src, NULL);
Ian Romanick00eb4662010-06-07 15:08:04 -0700166 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700167 case GLSL_TYPE_INT:
Carl Worth1660a292010-06-23 18:11:51 -0700168 result = new(ctx) ir_expression(ir_unop_i2f, desired_type, src, NULL);
Ian Romanick00eb4662010-06-07 15:08:04 -0700169 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700170 case GLSL_TYPE_BOOL:
Carl Worth1660a292010-06-23 18:11:51 -0700171 result = new(ctx) ir_expression(ir_unop_b2f, desired_type, src, NULL);
Ian Romanick00eb4662010-06-07 15:08:04 -0700172 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700173 }
174 break;
Ian Romanick26b5d332010-06-25 16:19:45 -0700175 case GLSL_TYPE_BOOL:
Ian Romanickb74b43e2010-06-11 16:52:09 -0700176 switch (b) {
Ian Romanick26b5d332010-06-25 16:19:45 -0700177 case GLSL_TYPE_UINT:
178 case GLSL_TYPE_INT:
179 result = new(ctx) ir_expression(ir_unop_i2b, desired_type, src, NULL);
180 break;
181 case GLSL_TYPE_FLOAT:
182 result = new(ctx) ir_expression(ir_unop_f2b, desired_type, src, NULL);
183 break;
Ian Romanickb74b43e2010-06-11 16:52:09 -0700184 }
Ian Romanick26b5d332010-06-25 16:19:45 -0700185 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700186 }
187
Ian Romanick00eb4662010-06-07 15:08:04 -0700188 assert(result != NULL);
189
190 ir_constant *const constant = result->constant_expression_value();
191 return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700192}
193
194
195/**
196 * Dereference a specific component from a scalar, vector, or matrix
197 */
198static ir_rvalue *
199dereference_component(ir_rvalue *src, unsigned component)
200{
Carl Worth1660a292010-06-23 18:11:51 -0700201 void *ctx = talloc_parent(src);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700202 assert(component < src->type->components());
203
Ian Romanickc9cb1032010-06-04 16:20:35 -0700204 /* If the source is a constant, just create a new constant instead of a
205 * dereference of the existing constant.
206 */
207 ir_constant *constant = src->as_constant();
208 if (constant)
Carl Worth1660a292010-06-23 18:11:51 -0700209 return new(ctx) ir_constant(constant, component);
Ian Romanickc9cb1032010-06-04 16:20:35 -0700210
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700211 if (src->type->is_scalar()) {
212 return src;
213 } else if (src->type->is_vector()) {
Carl Worth1660a292010-06-23 18:11:51 -0700214 return new(ctx) ir_swizzle(src, component, 0, 0, 0, 1);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700215 } else {
216 assert(src->type->is_matrix());
217
218 /* Dereference a row of the matrix, then call this function again to get
219 * a specific element from that row.
220 */
221 const int c = component / src->type->column_type()->vector_elements;
222 const int r = component % src->type->column_type()->vector_elements;
Carl Worth1660a292010-06-23 18:11:51 -0700223 ir_constant *const col_index = new(ctx) ir_constant(c);
224 ir_dereference *const col = new(ctx) ir_dereference_array(src, col_index);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700225
226 col->type = src->type->column_type();
227
228 return dereference_component(col, r);
229 }
230
231 assert(!"Should not get here.");
232 return NULL;
233}
234
235
Ian Romanick00aa1732010-03-31 16:48:48 -0700236static ir_rvalue *
237process_array_constructor(exec_list *instructions,
238 const glsl_type *constructor_type,
Ian Romanick304ea902010-05-10 11:17:53 -0700239 YYLTYPE *loc, exec_list *parameters,
Ian Romanick00aa1732010-03-31 16:48:48 -0700240 struct _mesa_glsl_parse_state *state)
241{
Carl Worthe01193a2010-06-23 18:25:04 -0700242 void *ctx = talloc_parent(state);
Ian Romanick00aa1732010-03-31 16:48:48 -0700243 /* Array constructors come in two forms: sized and unsized. Sized array
244 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
245 * variables. In this case the number of parameters must exactly match the
246 * specified size of the array.
247 *
248 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
249 * are vec4 variables. In this case the size of the array being constructed
250 * is determined by the number of parameters.
251 *
252 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
253 *
254 * "There must be exactly the same number of arguments as the size of
255 * the array being constructed. If no size is present in the
256 * constructor, then the array is explicitly sized to the number of
257 * arguments provided. The arguments are assigned in order, starting at
258 * element 0, to the elements of the constructed array. Each argument
259 * must be the same type as the element type of the array, or be a type
260 * that can be converted to the element type of the array according to
261 * Section 4.1.10 "Implicit Conversions.""
262 */
263 exec_list actual_parameters;
264 const unsigned parameter_count =
265 process_parameters(instructions, &actual_parameters, parameters, state);
266
267 if ((parameter_count == 0)
268 || ((constructor_type->length != 0)
269 && (constructor_type->length != parameter_count))) {
270 const unsigned min_param = (constructor_type->length == 0)
271 ? 1 : constructor_type->length;
272
273 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
274 "parameter%s",
275 (constructor_type->length != 0) ? "at least" : "exactly",
276 min_param, (min_param <= 1) ? "" : "s");
Carl Worthe01193a2010-06-23 18:25:04 -0700277 return ir_call::get_error_instruction(ctx);
Ian Romanick00aa1732010-03-31 16:48:48 -0700278 }
279
280 if (constructor_type->length == 0) {
281 constructor_type =
Carl Worth12c41152010-06-18 17:52:59 -0700282 glsl_type::get_array_instance(state,
283 constructor_type->element_type(),
Ian Romanick00aa1732010-03-31 16:48:48 -0700284 parameter_count);
285 assert(constructor_type != NULL);
286 assert(constructor_type->length == parameter_count);
287 }
288
289 ir_function *f = state->symbols->get_function(constructor_type->name);
290
291 /* If the constructor for this type of array does not exist, generate the
Ian Romanick82baaf42010-04-23 13:21:22 -0700292 * prototype and add it to the symbol table.
Ian Romanick00aa1732010-03-31 16:48:48 -0700293 */
294 if (f == NULL) {
Ian Romanick82baaf42010-04-23 13:21:22 -0700295 f = constructor_type->generate_constructor(state->symbols);
Ian Romanick00aa1732010-03-31 16:48:48 -0700296 }
297
298 ir_rvalue *const r =
299 process_call(instructions, f, loc, &actual_parameters, state);
300
301 assert(r != NULL);
302 assert(r->type->is_error() || (r->type == constructor_type));
303
304 return r;
305}
306
307
Ian Romanickab92d0e2010-06-09 17:26:20 -0700308/**
309 * Try to convert a record constructor to a constant expression
310 */
311static ir_constant *
312constant_record_constructor(const glsl_type *constructor_type,
313 YYLTYPE *loc, exec_list *parameters,
314 struct _mesa_glsl_parse_state *state)
315{
Carl Worth1660a292010-06-23 18:11:51 -0700316 void *ctx = talloc_parent(state);
Ian Romanickab92d0e2010-06-09 17:26:20 -0700317 bool all_parameters_are_constant = true;
318
319 exec_node *node = parameters->head;
320 for (unsigned i = 0; i < constructor_type->length; i++) {
321 ir_instruction *ir = (ir_instruction *) node;
322
323 if (node->is_tail_sentinal()) {
324 _mesa_glsl_error(loc, state,
325 "insufficient parameters to constructor for `%s'",
326 constructor_type->name);
327 return NULL;
328 }
329
330 if (ir->type != constructor_type->fields.structure[i].type) {
331 _mesa_glsl_error(loc, state,
332 "parameter type mismatch in constructor for `%s' "
333 " (%s vs %s)",
334 constructor_type->name,
335 ir->type->name,
336 constructor_type->fields.structure[i].type->name);
337 return NULL;
338 }
339
340 if (ir->as_constant() == NULL)
341 all_parameters_are_constant = false;
342
343 node = node->next;
344 }
345
346 if (!all_parameters_are_constant)
347 return NULL;
348
Carl Worth1660a292010-06-23 18:11:51 -0700349 return new(ctx) ir_constant(constructor_type, parameters);
Ian Romanickab92d0e2010-06-09 17:26:20 -0700350}
351
352
Ian Romanickbe1d2bf2010-06-11 14:01:44 -0700353/**
354 * Generate data for a constant matrix constructor w/a single scalar parameter
355 *
356 * Matrix constructors in GLSL can be passed a single scalar of the
357 * approriate type. In these cases, the resulting matrix is the identity
358 * matrix multipled by the specified scalar. This function generates data for
359 * that matrix.
360 *
361 * \param type Type of the desired matrix.
362 * \param initializer Scalar value used to initialize the matrix diagonal.
363 * \param data Location to store the resulting matrix.
364 */
365void
366generate_constructor_matrix(const glsl_type *type, ir_constant *initializer,
367 ir_constant_data *data)
368{
369 switch (type->base_type) {
370 case GLSL_TYPE_UINT:
371 case GLSL_TYPE_INT:
372 for (unsigned i = 0; i < type->components(); i++)
373 data->u[i] = 0;
374
375 for (unsigned i = 0; i < type->matrix_columns; i++) {
376 /* The array offset of the ith row and column of the matrix.
377 */
378 const unsigned idx = (i * type->vector_elements) + i;
379
380 data->u[idx] = initializer->value.u[0];
381 }
382 break;
383
384 case GLSL_TYPE_FLOAT:
385 for (unsigned i = 0; i < type->components(); i++)
386 data->f[i] = 0;
387
388 for (unsigned i = 0; i < type->matrix_columns; i++) {
389 /* The array offset of the ith row and column of the matrix.
390 */
391 const unsigned idx = (i * type->vector_elements) + i;
392
393 data->f[idx] = initializer->value.f[0];
394 }
395
396 break;
397
398 default:
399 assert(!"Should not get here.");
400 break;
401 }
402}
403
404
405/**
406 * Generate data for a constant vector constructor w/a single scalar parameter
407 *
408 * Vector constructors in GLSL can be passed a single scalar of the
409 * approriate type. In these cases, the resulting vector contains the specified
410 * value in all components. This function generates data for that vector.
411 *
412 * \param type Type of the desired vector.
413 * \param initializer Scalar value used to initialize the vector.
414 * \param data Location to store the resulting vector data.
415 */
416void
417generate_constructor_vector(const glsl_type *type, ir_constant *initializer,
418 ir_constant_data *data)
419{
420 switch (type->base_type) {
421 case GLSL_TYPE_UINT:
422 case GLSL_TYPE_INT:
423 for (unsigned i = 0; i < type->components(); i++)
424 data->u[i] = initializer->value.u[0];
425
426 break;
427
428 case GLSL_TYPE_FLOAT:
429 for (unsigned i = 0; i < type->components(); i++)
430 data->f[i] = initializer->value.f[0];
431
432 break;
433
434 case GLSL_TYPE_BOOL:
435 for (unsigned i = 0; i < type->components(); i++)
436 data->b[i] = initializer->value.b[0];
437
438 break;
439
440 default:
441 assert(!"Should not get here.");
442 break;
443 }
444}
445
446
Ian Romanickc31dcdf2010-06-23 15:19:40 -0700447/**
448 * Determine if a list consists of a single scalar r-value
449 */
450bool
451single_scalar_parameter(exec_list *parameters)
452{
453 const ir_rvalue *const p = (ir_rvalue *) parameters->head;
454 assert(((ir_rvalue *)p)->as_rvalue() != NULL);
455
456 return (p->type->is_scalar() && p->next->is_tail_sentinal());
457}
458
459
460/**
461 * Generate inline code for a vector constructor
462 *
463 * The generated constructor code will consist of a temporary variable
464 * declaration of the same type as the constructor. A sequence of assignments
465 * from constructor parameters to the temporary will follow.
466 *
467 * \return
468 * An \c ir_dereference_variable of the temprorary generated in the constructor
469 * body.
470 */
471ir_rvalue *
472emit_inline_vector_constructor(const glsl_type *type,
473 exec_list *instructions,
474 exec_list *parameters,
475 void *ctx)
476{
477 assert(!parameters->is_empty());
478
479 ir_variable *var = new(ctx) ir_variable(type, strdup("vec_ctor"));
480 instructions->push_tail(var);
481
482 /* There are two kinds of vector constructors.
483 *
484 * - Construct a vector from a single scalar by replicating that scalar to
485 * all components of the vector.
486 *
487 * - Construct a vector from an arbirary combination of vectors and
488 * scalars. The components of the constructor parameters are assigned
489 * to the vector in order until the vector is full.
490 */
491 const unsigned lhs_components = type->components();
492 if (single_scalar_parameter(parameters)) {
493 ir_rvalue *first_param = (ir_rvalue *)parameters->head;
494 ir_rvalue *rhs = new(ctx) ir_swizzle(first_param, 0, 0, 0, 0,
495 lhs_components);
496 ir_dereference_variable *lhs = new(ctx) ir_dereference_variable(var);
497
498 assert(rhs->type == lhs->type);
499
500 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
501 instructions->push_tail(inst);
502 } else {
503 unsigned base_component = 0;
504 foreach_list(node, parameters) {
505 ir_rvalue *rhs = (ir_rvalue *) node;
506 unsigned rhs_components = rhs->type->components();
507
508 /* Do not try to assign more components to the vector than it has!
509 */
510 if ((rhs_components + base_component) > lhs_components) {
511 rhs_components = lhs_components - base_component;
512 }
513
514 /* Emit an assignment of the constructor parameter to the next set of
515 * components in the temporary variable.
516 */
517 unsigned mask[4] = { 0, 0, 0, 0 };
518 for (unsigned i = 0; i < rhs_components; i++) {
519 mask[i] = i + base_component;
520 }
521
522
523 ir_rvalue *lhs_ref = new(ctx) ir_dereference_variable(var);
524 ir_swizzle *lhs = new(ctx) ir_swizzle(lhs_ref, mask, rhs_components);
525
526 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
527 instructions->push_tail(inst);
528
529 /* Advance the component index by the number of components that were
530 * just assigned.
531 */
532 base_component += rhs_components;
533 }
534 }
535 return new(ctx) ir_dereference_variable(var);
536}
537
538
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700539ir_rvalue *
Ian Romanick548fa292010-03-15 13:04:13 -0700540ast_function_expression::hir(exec_list *instructions,
541 struct _mesa_glsl_parse_state *state)
542{
Carl Worth1660a292010-06-23 18:11:51 -0700543 void *ctx = talloc_parent(state);
Ian Romanick548fa292010-03-15 13:04:13 -0700544 /* There are three sorts of function calls.
545 *
546 * 1. contstructors - The first subexpression is an ast_type_specifier.
547 * 2. methods - Only the .length() method of array types.
548 * 3. functions - Calls to regular old functions.
549 *
Ian Romanick548fa292010-03-15 13:04:13 -0700550 * Method calls are actually detected when the ast_field_selection
551 * expression is handled.
552 */
553 if (is_constructor()) {
Ian Romanickabef9552010-03-23 15:08:30 -0700554 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
555 YYLTYPE loc = type->get_location();
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700556 const char *name;
Ian Romanickabef9552010-03-23 15:08:30 -0700557
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700558 const glsl_type *const constructor_type = type->glsl_type(& name, state);
Ian Romanickabef9552010-03-23 15:08:30 -0700559
560
561 /* Constructors for samplers are illegal.
562 */
563 if (constructor_type->is_sampler()) {
564 _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
565 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700566 return ir_call::get_error_instruction(ctx);
Ian Romanickabef9552010-03-23 15:08:30 -0700567 }
568
Ian Romanickb6326ab2010-03-31 16:25:21 -0700569 if (constructor_type->is_array()) {
570 if (state->language_version <= 110) {
571 _mesa_glsl_error(& loc, state,
572 "array constructors forbidden in GLSL 1.10");
Carl Worthe01193a2010-06-23 18:25:04 -0700573 return ir_call::get_error_instruction(ctx);
Ian Romanickb6326ab2010-03-31 16:25:21 -0700574 }
575
Ian Romanick00aa1732010-03-31 16:48:48 -0700576 return process_array_constructor(instructions, constructor_type,
Ian Romanick3521f0b2010-05-10 10:47:14 -0700577 & loc, &this->expressions, state);
Ian Romanickb6326ab2010-03-31 16:25:21 -0700578 }
Ian Romanickabef9552010-03-23 15:08:30 -0700579
580 /* There are two kinds of constructor call. Constructors for built-in
581 * language types, such as mat4 and vec2, are free form. The only
582 * requirement is that the parameters must provide enough values of the
583 * correct scalar type. Constructors for arrays and structures must
584 * have the exact number of parameters with matching types in the
585 * correct order. These constructors follow essentially the same type
586 * matching rules as functions.
587 */
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700588 if (constructor_type->is_numeric() || constructor_type->is_boolean()) {
589 /* Constructing a numeric type has a couple steps. First all values
590 * passed to the constructor are broken into individual parameters
591 * and type converted to the base type of the thing being constructed.
592 *
593 * At that point we have some number of values that match the base
594 * type of the thing being constructed. Now the constructor can be
595 * treated like a function call. Each numeric type has a small set
596 * of constructor functions. The set of new parameters will either
597 * match one of those functions or the original constructor is
598 * invalid.
599 */
600 const glsl_type *const base_type = constructor_type->get_base_type();
601
602 /* Total number of components of the type being constructed.
603 */
604 const unsigned type_components = constructor_type->components();
605
606 /* Number of components from parameters that have actually been
607 * consumed. This is used to perform several kinds of error checking.
608 */
609 unsigned components_used = 0;
610
611 unsigned matrix_parameters = 0;
612 unsigned nonmatrix_parameters = 0;
613 exec_list actual_parameters;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700614
Ian Romanick9e08d012010-06-04 16:36:09 -0700615 bool all_parameters_are_constant = true;
616
Ian Romanickfa455fc2010-06-23 13:58:34 -0700617 /* This handles invalid constructor calls such as 'vec4 v = vec4();'
618 */
619 if (this->expressions.is_empty()) {
620 _mesa_glsl_error(& loc, state, "too few components to construct "
621 "`%s'",
622 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700623 return ir_call::get_error_instruction(ctx);
Ian Romanickfa455fc2010-06-23 13:58:34 -0700624 }
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700625
Ian Romanick304ea902010-05-10 11:17:53 -0700626 foreach_list (n, &this->expressions) {
627 ast_node *ast = exec_node_data(ast_node, n, link);
Ian Romanick9e08d012010-06-04 16:36:09 -0700628 ir_rvalue *result =
Ian Romanick304ea902010-05-10 11:17:53 -0700629 ast->hir(instructions, state)->as_rvalue();
Eric Anholt53e48d32010-06-22 14:22:42 -0700630 ir_variable *result_var = NULL;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700631
Ian Romanick9e08d012010-06-04 16:36:09 -0700632 /* Attempt to convert the parameter to a constant valued expression.
633 * After doing so, track whether or not all the parameters to the
634 * constructor are trivially constant valued expressions.
635 */
636 ir_rvalue *const constant =
637 result->constant_expression_value();
638
639 if (constant != NULL)
640 result = constant;
641 else
642 all_parameters_are_constant = false;
643
Ian Romanick3521f0b2010-05-10 10:47:14 -0700644 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
645 *
646 * "It is an error to provide extra arguments beyond this
647 * last used argument."
648 */
649 if (components_used >= type_components) {
650 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
651 "constructor",
652 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700653 return ir_call::get_error_instruction(ctx);
Ian Romanick3521f0b2010-05-10 10:47:14 -0700654 }
655
656 if (!result->type->is_numeric() && !result->type->is_boolean()) {
657 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
658 "non-numeric data type",
659 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700660 return ir_call::get_error_instruction(ctx);
Ian Romanick3521f0b2010-05-10 10:47:14 -0700661 }
662
663 /* Count the number of matrix and nonmatrix parameters. This
664 * is used below to enforce some of the constructor rules.
665 */
666 if (result->type->is_matrix())
667 matrix_parameters++;
668 else
669 nonmatrix_parameters++;
670
Eric Anholt53e48d32010-06-22 14:22:42 -0700671 /* We can't use the same instruction node in the multiple
672 * swizzle dereferences that happen, so assign it to a
673 * variable and deref that. Plus it saves computation for
674 * complicated expressions and handles
675 * glsl-vs-constructor-call.shader_test.
676 */
677 if (result->type->components() >= 1 && !result->as_constant()) {
Carl Worth1660a292010-06-23 18:11:51 -0700678 result_var = new(ctx) ir_variable(result->type,
679 "constructor_tmp");
Eric Anholt53e48d32010-06-22 14:22:42 -0700680 ir_dereference_variable *lhs;
681
Carl Worth1660a292010-06-23 18:11:51 -0700682 lhs = new(ctx) ir_dereference_variable(result_var);
683 instructions->push_tail(new(ctx) ir_assignment(lhs,
684 result, NULL));
Eric Anholt53e48d32010-06-22 14:22:42 -0700685 }
Ian Romanick3521f0b2010-05-10 10:47:14 -0700686
687 /* Process each of the components of the parameter. Dereference
688 * each component individually, perform any type conversions, and
689 * add it to the parameter list for the constructor.
690 */
691 for (unsigned i = 0; i < result->type->components(); i++) {
692 if (components_used >= type_components)
693 break;
694
Eric Anholt53e48d32010-06-22 14:22:42 -0700695 ir_rvalue *component;
696
697 if (result_var) {
Carl Worth1660a292010-06-23 18:11:51 -0700698 ir_dereference *d = new(ctx) ir_dereference_variable(result_var);
Eric Anholt53e48d32010-06-22 14:22:42 -0700699 component = dereference_component(d, i);
700 } else {
701 component = dereference_component(result, i);
702 }
703 component = convert_component(component, base_type);
Ian Romanick3521f0b2010-05-10 10:47:14 -0700704
705 /* All cases that could result in component->type being the
706 * error type should have already been caught above.
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700707 */
Ian Romanick3521f0b2010-05-10 10:47:14 -0700708 assert(component->type == base_type);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700709
Ian Romanick9e08d012010-06-04 16:36:09 -0700710 if (component->as_constant() == NULL)
711 all_parameters_are_constant = false;
712
Ian Romanick3521f0b2010-05-10 10:47:14 -0700713 /* Don't actually generate constructor calls for scalars.
714 * Instead, do the usual component selection and conversion,
715 * and return the single component.
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700716 */
Ian Romanick3521f0b2010-05-10 10:47:14 -0700717 if (constructor_type->is_scalar())
718 return component;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700719
Ian Romanick3521f0b2010-05-10 10:47:14 -0700720 actual_parameters.push_tail(component);
721 components_used++;
722 }
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700723 }
724
725 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
726 *
727 * "It is an error to construct matrices from other matrices. This
728 * is reserved for future use."
729 */
730 if ((state->language_version <= 110) && (matrix_parameters > 0)
731 && constructor_type->is_matrix()) {
732 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
733 "matrix in GLSL 1.10",
734 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700735 return ir_call::get_error_instruction(ctx);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700736 }
737
738 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
739 *
740 * "If a matrix argument is given to a matrix constructor, it is
741 * an error to have any other arguments."
742 */
743 if ((matrix_parameters > 0)
744 && ((matrix_parameters + nonmatrix_parameters) > 1)
745 && constructor_type->is_matrix()) {
746 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
747 "matrix must be only parameter",
748 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700749 return ir_call::get_error_instruction(ctx);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700750 }
751
752 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
753 *
754 * "In these cases, there must be enough components provided in the
755 * arguments to provide an initializer for every component in the
756 * constructed value."
757 */
Ian Romanick8a24cd52010-03-29 15:36:02 -0700758 if ((components_used < type_components) && (components_used != 1)) {
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700759 _mesa_glsl_error(& loc, state, "too few components to construct "
760 "`%s'",
761 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700762 return ir_call::get_error_instruction(ctx);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700763 }
764
765 ir_function *f = state->symbols->get_function(constructor_type->name);
766 if (f == NULL) {
767 _mesa_glsl_error(& loc, state, "no constructor for type `%s'",
768 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700769 return ir_call::get_error_instruction(ctx);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700770 }
771
772 const ir_function_signature *sig =
773 f->matching_signature(& actual_parameters);
774 if (sig != NULL) {
Ian Romanick9e08d012010-06-04 16:36:09 -0700775 /* If all of the parameters are trivially constant, create a
776 * constant representing the complete collection of parameters.
777 */
Ian Romanickbe1d2bf2010-06-11 14:01:44 -0700778 if (all_parameters_are_constant) {
779 if (components_used >= type_components)
Carl Worth1660a292010-06-23 18:11:51 -0700780 return new(ctx) ir_constant(sig->return_type,
781 & actual_parameters);
Ian Romanickbe1d2bf2010-06-11 14:01:44 -0700782
783 assert(sig->return_type->is_vector()
784 || sig->return_type->is_matrix());
785
786 /* Constructors with exactly one component are special for
787 * vectors and matrices. For vectors it causes all elements of
788 * the vector to be filled with the value. For matrices it
789 * causes the matrix to be filled with 0 and the diagonal to be
790 * filled with the value.
791 */
792 ir_constant_data data;
793 ir_constant *const initializer =
794 (ir_constant *) actual_parameters.head;
795 if (sig->return_type->is_matrix())
796 generate_constructor_matrix(sig->return_type, initializer,
797 &data);
798 else
799 generate_constructor_vector(sig->return_type, initializer,
800 &data);
801
Carl Worth1660a292010-06-23 18:11:51 -0700802 return new(ctx) ir_constant(sig->return_type, &data);
Ian Romanickc31dcdf2010-06-23 15:19:40 -0700803 } else if (constructor_type->is_vector()) {
804 return emit_inline_vector_constructor(constructor_type,
805 instructions,
806 &actual_parameters,
807 ctx);
808 } else {
809 assert(constructor_type->is_matrix());
Carl Worth1660a292010-06-23 18:11:51 -0700810 return new(ctx) ir_call(sig, & actual_parameters);
Ian Romanickc31dcdf2010-06-23 15:19:40 -0700811 }
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700812 } else {
813 /* FINISHME: Log a better error message here. G++ will show the
814 * FINSIHME: types of the actual parameters and the set of
815 * FINSIHME: candidate functions. A different error should also be
816 * FINSIHME: logged when multiple functions match.
817 */
818 _mesa_glsl_error(& loc, state, "no matching constructor for `%s'",
819 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700820 return ir_call::get_error_instruction(ctx);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700821 }
822 }
Ian Romanickabef9552010-03-23 15:08:30 -0700823
Carl Worthe01193a2010-06-23 18:25:04 -0700824 return ir_call::get_error_instruction(ctx);
Ian Romanick548fa292010-03-15 13:04:13 -0700825 } else {
826 const ast_expression *id = subexpressions[0];
Ian Romanickf4749612010-03-15 13:26:02 -0700827 YYLTYPE loc = id->get_location();
Ian Romanickc0771312010-06-09 17:23:26 -0700828 exec_list actual_parameters;
829
830 process_parameters(instructions, &actual_parameters, &this->expressions,
831 state);
Ian Romanick548fa292010-03-15 13:04:13 -0700832
Ian Romanickab92d0e2010-06-09 17:26:20 -0700833 const glsl_type *const type =
834 state->symbols->get_type(id->primary_expression.identifier);
835
836 if ((type != NULL) && type->is_record()) {
837 ir_constant *constant =
838 constant_record_constructor(type, &loc, &actual_parameters, state);
839
840 if (constant != NULL)
841 return constant;
842 }
843
Ian Romanickf4749612010-03-15 13:26:02 -0700844 return match_function_by_name(instructions,
845 id->primary_expression.identifier, & loc,
Ian Romanickc0771312010-06-09 17:23:26 -0700846 &actual_parameters, state);
Ian Romanick548fa292010-03-15 13:04:13 -0700847 }
848
Carl Worthe01193a2010-06-23 18:25:04 -0700849 return ir_call::get_error_instruction(ctx);
Ian Romanick548fa292010-03-15 13:04:13 -0700850}