blob: 866cbc4ecdf6a5a8b0f88183825bcba5ce2e1508 [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);
107 return ir_call::get_error_instruction();
108 }
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{
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700117 ir_function *f = state->symbols->get_function(name);
Ian Romanickf4749612010-03-15 13:26:02 -0700118
119 if (f == NULL) {
120 _mesa_glsl_error(loc, state, "function `%s' undeclared", name);
121 return ir_call::get_error_instruction();
122 }
123
Ian Romanickc0771312010-06-09 17:23:26 -0700124 /* Once we've determined that the function being called might exist, try
125 * to find an overload of the function that matches the parameters.
Ian Romanickf4749612010-03-15 13:26:02 -0700126 */
Ian Romanickc0771312010-06-09 17:23:26 -0700127 return process_call(instructions, f, loc, actual_parameters, state);
Ian Romanickf4749612010-03-15 13:26:02 -0700128}
129
130
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700131/**
132 * Perform automatic type conversion of constructor parameters
133 */
134static ir_rvalue *
135convert_component(ir_rvalue *src, const glsl_type *desired_type)
136{
Carl Worth1660a292010-06-23 18:11:51 -0700137 void *ctx = talloc_parent(src);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700138 const unsigned a = desired_type->base_type;
139 const unsigned b = src->type->base_type;
Ian Romanick00eb4662010-06-07 15:08:04 -0700140 ir_expression *result = NULL;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700141
142 if (src->type->is_error())
143 return src;
144
145 assert(a <= GLSL_TYPE_BOOL);
146 assert(b <= GLSL_TYPE_BOOL);
147
148 if ((a == b) || (src->type->is_integer() && desired_type->is_integer()))
149 return src;
150
151 switch (a) {
152 case GLSL_TYPE_UINT:
153 case GLSL_TYPE_INT:
154 if (b == GLSL_TYPE_FLOAT)
Carl Worth1660a292010-06-23 18:11:51 -0700155 result = new(ctx) ir_expression(ir_unop_f2i, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700156 else {
157 assert(b == GLSL_TYPE_BOOL);
Carl Worth1660a292010-06-23 18:11:51 -0700158 result = new(ctx) ir_expression(ir_unop_b2i, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700159 }
Ian Romanick565185c2010-06-11 13:49:00 -0700160 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700161 case GLSL_TYPE_FLOAT:
162 switch (b) {
163 case GLSL_TYPE_UINT:
Carl Worth1660a292010-06-23 18:11:51 -0700164 result = new(ctx) ir_expression(ir_unop_u2f, desired_type, src, NULL);
Ian Romanick00eb4662010-06-07 15:08:04 -0700165 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700166 case GLSL_TYPE_INT:
Carl Worth1660a292010-06-23 18:11:51 -0700167 result = new(ctx) ir_expression(ir_unop_i2f, desired_type, src, NULL);
Ian Romanick00eb4662010-06-07 15:08:04 -0700168 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700169 case GLSL_TYPE_BOOL:
Carl Worth1660a292010-06-23 18:11:51 -0700170 result = new(ctx) ir_expression(ir_unop_b2f, desired_type, src, NULL);
Ian Romanick00eb4662010-06-07 15:08:04 -0700171 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700172 }
173 break;
174 case GLSL_TYPE_BOOL: {
Ian Romanickb74b43e2010-06-11 16:52:09 -0700175 ir_constant *zero = NULL;
176
177 switch (b) {
Carl Worth1660a292010-06-23 18:11:51 -0700178 case GLSL_TYPE_UINT: zero = new(ctx) ir_constant(unsigned(0)); break;
179 case GLSL_TYPE_INT: zero = new(ctx) ir_constant(int(0)); break;
180 case GLSL_TYPE_FLOAT: zero = new(ctx) ir_constant(0.0f); break;
Ian Romanickb74b43e2010-06-11 16:52:09 -0700181 }
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700182
Carl Worth1660a292010-06-23 18:11:51 -0700183 result = new(ctx) ir_expression(ir_binop_nequal, desired_type, src, zero);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700184 }
185 }
186
Ian Romanick00eb4662010-06-07 15:08:04 -0700187 assert(result != NULL);
188
189 ir_constant *const constant = result->constant_expression_value();
190 return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700191}
192
193
194/**
195 * Dereference a specific component from a scalar, vector, or matrix
196 */
197static ir_rvalue *
198dereference_component(ir_rvalue *src, unsigned component)
199{
Carl Worth1660a292010-06-23 18:11:51 -0700200 void *ctx = talloc_parent(src);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700201 assert(component < src->type->components());
202
Ian Romanickc9cb1032010-06-04 16:20:35 -0700203 /* If the source is a constant, just create a new constant instead of a
204 * dereference of the existing constant.
205 */
206 ir_constant *constant = src->as_constant();
207 if (constant)
Carl Worth1660a292010-06-23 18:11:51 -0700208 return new(ctx) ir_constant(constant, component);
Ian Romanickc9cb1032010-06-04 16:20:35 -0700209
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700210 if (src->type->is_scalar()) {
211 return src;
212 } else if (src->type->is_vector()) {
Carl Worth1660a292010-06-23 18:11:51 -0700213 return new(ctx) ir_swizzle(src, component, 0, 0, 0, 1);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700214 } else {
215 assert(src->type->is_matrix());
216
217 /* Dereference a row of the matrix, then call this function again to get
218 * a specific element from that row.
219 */
220 const int c = component / src->type->column_type()->vector_elements;
221 const int r = component % src->type->column_type()->vector_elements;
Carl Worth1660a292010-06-23 18:11:51 -0700222 ir_constant *const col_index = new(ctx) ir_constant(c);
223 ir_dereference *const col = new(ctx) ir_dereference_array(src, col_index);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700224
225 col->type = src->type->column_type();
226
227 return dereference_component(col, r);
228 }
229
230 assert(!"Should not get here.");
231 return NULL;
232}
233
234
Ian Romanick00aa1732010-03-31 16:48:48 -0700235static ir_rvalue *
236process_array_constructor(exec_list *instructions,
237 const glsl_type *constructor_type,
Ian Romanick304ea902010-05-10 11:17:53 -0700238 YYLTYPE *loc, exec_list *parameters,
Ian Romanick00aa1732010-03-31 16:48:48 -0700239 struct _mesa_glsl_parse_state *state)
240{
241 /* Array constructors come in two forms: sized and unsized. Sized array
242 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
243 * variables. In this case the number of parameters must exactly match the
244 * specified size of the array.
245 *
246 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
247 * are vec4 variables. In this case the size of the array being constructed
248 * is determined by the number of parameters.
249 *
250 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
251 *
252 * "There must be exactly the same number of arguments as the size of
253 * the array being constructed. If no size is present in the
254 * constructor, then the array is explicitly sized to the number of
255 * arguments provided. The arguments are assigned in order, starting at
256 * element 0, to the elements of the constructed array. Each argument
257 * must be the same type as the element type of the array, or be a type
258 * that can be converted to the element type of the array according to
259 * Section 4.1.10 "Implicit Conversions.""
260 */
261 exec_list actual_parameters;
262 const unsigned parameter_count =
263 process_parameters(instructions, &actual_parameters, parameters, state);
264
265 if ((parameter_count == 0)
266 || ((constructor_type->length != 0)
267 && (constructor_type->length != parameter_count))) {
268 const unsigned min_param = (constructor_type->length == 0)
269 ? 1 : constructor_type->length;
270
271 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
272 "parameter%s",
273 (constructor_type->length != 0) ? "at least" : "exactly",
274 min_param, (min_param <= 1) ? "" : "s");
275 return ir_call::get_error_instruction();
276 }
277
278 if (constructor_type->length == 0) {
279 constructor_type =
Ian Romanickcb9cba22010-04-02 16:08:44 -0700280 glsl_type::get_array_instance(constructor_type->element_type(),
Ian Romanick00aa1732010-03-31 16:48:48 -0700281 parameter_count);
282 assert(constructor_type != NULL);
283 assert(constructor_type->length == parameter_count);
284 }
285
286 ir_function *f = state->symbols->get_function(constructor_type->name);
287
288 /* If the constructor for this type of array does not exist, generate the
Ian Romanick82baaf42010-04-23 13:21:22 -0700289 * prototype and add it to the symbol table.
Ian Romanick00aa1732010-03-31 16:48:48 -0700290 */
291 if (f == NULL) {
Ian Romanick82baaf42010-04-23 13:21:22 -0700292 f = constructor_type->generate_constructor(state->symbols);
Ian Romanick00aa1732010-03-31 16:48:48 -0700293 }
294
295 ir_rvalue *const r =
296 process_call(instructions, f, loc, &actual_parameters, state);
297
298 assert(r != NULL);
299 assert(r->type->is_error() || (r->type == constructor_type));
300
301 return r;
302}
303
304
Ian Romanickab92d0e2010-06-09 17:26:20 -0700305/**
306 * Try to convert a record constructor to a constant expression
307 */
308static ir_constant *
309constant_record_constructor(const glsl_type *constructor_type,
310 YYLTYPE *loc, exec_list *parameters,
311 struct _mesa_glsl_parse_state *state)
312{
Carl Worth1660a292010-06-23 18:11:51 -0700313 void *ctx = talloc_parent(state);
Ian Romanickab92d0e2010-06-09 17:26:20 -0700314 bool all_parameters_are_constant = true;
315
316 exec_node *node = parameters->head;
317 for (unsigned i = 0; i < constructor_type->length; i++) {
318 ir_instruction *ir = (ir_instruction *) node;
319
320 if (node->is_tail_sentinal()) {
321 _mesa_glsl_error(loc, state,
322 "insufficient parameters to constructor for `%s'",
323 constructor_type->name);
324 return NULL;
325 }
326
327 if (ir->type != constructor_type->fields.structure[i].type) {
328 _mesa_glsl_error(loc, state,
329 "parameter type mismatch in constructor for `%s' "
330 " (%s vs %s)",
331 constructor_type->name,
332 ir->type->name,
333 constructor_type->fields.structure[i].type->name);
334 return NULL;
335 }
336
337 if (ir->as_constant() == NULL)
338 all_parameters_are_constant = false;
339
340 node = node->next;
341 }
342
343 if (!all_parameters_are_constant)
344 return NULL;
345
Carl Worth1660a292010-06-23 18:11:51 -0700346 return new(ctx) ir_constant(constructor_type, parameters);
Ian Romanickab92d0e2010-06-09 17:26:20 -0700347}
348
349
Ian Romanickbe1d2bf2010-06-11 14:01:44 -0700350/**
351 * Generate data for a constant matrix constructor w/a single scalar parameter
352 *
353 * Matrix constructors in GLSL can be passed a single scalar of the
354 * approriate type. In these cases, the resulting matrix is the identity
355 * matrix multipled by the specified scalar. This function generates data for
356 * that matrix.
357 *
358 * \param type Type of the desired matrix.
359 * \param initializer Scalar value used to initialize the matrix diagonal.
360 * \param data Location to store the resulting matrix.
361 */
362void
363generate_constructor_matrix(const glsl_type *type, ir_constant *initializer,
364 ir_constant_data *data)
365{
366 switch (type->base_type) {
367 case GLSL_TYPE_UINT:
368 case GLSL_TYPE_INT:
369 for (unsigned i = 0; i < type->components(); i++)
370 data->u[i] = 0;
371
372 for (unsigned i = 0; i < type->matrix_columns; i++) {
373 /* The array offset of the ith row and column of the matrix.
374 */
375 const unsigned idx = (i * type->vector_elements) + i;
376
377 data->u[idx] = initializer->value.u[0];
378 }
379 break;
380
381 case GLSL_TYPE_FLOAT:
382 for (unsigned i = 0; i < type->components(); i++)
383 data->f[i] = 0;
384
385 for (unsigned i = 0; i < type->matrix_columns; i++) {
386 /* The array offset of the ith row and column of the matrix.
387 */
388 const unsigned idx = (i * type->vector_elements) + i;
389
390 data->f[idx] = initializer->value.f[0];
391 }
392
393 break;
394
395 default:
396 assert(!"Should not get here.");
397 break;
398 }
399}
400
401
402/**
403 * Generate data for a constant vector constructor w/a single scalar parameter
404 *
405 * Vector constructors in GLSL can be passed a single scalar of the
406 * approriate type. In these cases, the resulting vector contains the specified
407 * value in all components. This function generates data for that vector.
408 *
409 * \param type Type of the desired vector.
410 * \param initializer Scalar value used to initialize the vector.
411 * \param data Location to store the resulting vector data.
412 */
413void
414generate_constructor_vector(const glsl_type *type, ir_constant *initializer,
415 ir_constant_data *data)
416{
417 switch (type->base_type) {
418 case GLSL_TYPE_UINT:
419 case GLSL_TYPE_INT:
420 for (unsigned i = 0; i < type->components(); i++)
421 data->u[i] = initializer->value.u[0];
422
423 break;
424
425 case GLSL_TYPE_FLOAT:
426 for (unsigned i = 0; i < type->components(); i++)
427 data->f[i] = initializer->value.f[0];
428
429 break;
430
431 case GLSL_TYPE_BOOL:
432 for (unsigned i = 0; i < type->components(); i++)
433 data->b[i] = initializer->value.b[0];
434
435 break;
436
437 default:
438 assert(!"Should not get here.");
439 break;
440 }
441}
442
443
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700444ir_rvalue *
Ian Romanick548fa292010-03-15 13:04:13 -0700445ast_function_expression::hir(exec_list *instructions,
446 struct _mesa_glsl_parse_state *state)
447{
Carl Worth1660a292010-06-23 18:11:51 -0700448 void *ctx = talloc_parent(state);
Ian Romanick548fa292010-03-15 13:04:13 -0700449 /* There are three sorts of function calls.
450 *
451 * 1. contstructors - The first subexpression is an ast_type_specifier.
452 * 2. methods - Only the .length() method of array types.
453 * 3. functions - Calls to regular old functions.
454 *
Ian Romanick548fa292010-03-15 13:04:13 -0700455 * Method calls are actually detected when the ast_field_selection
456 * expression is handled.
457 */
458 if (is_constructor()) {
Ian Romanickabef9552010-03-23 15:08:30 -0700459 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
460 YYLTYPE loc = type->get_location();
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700461 const char *name;
Ian Romanickabef9552010-03-23 15:08:30 -0700462
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700463 const glsl_type *const constructor_type = type->glsl_type(& name, state);
Ian Romanickabef9552010-03-23 15:08:30 -0700464
465
466 /* Constructors for samplers are illegal.
467 */
468 if (constructor_type->is_sampler()) {
469 _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
470 constructor_type->name);
471 return ir_call::get_error_instruction();
472 }
473
Ian Romanickb6326ab2010-03-31 16:25:21 -0700474 if (constructor_type->is_array()) {
475 if (state->language_version <= 110) {
476 _mesa_glsl_error(& loc, state,
477 "array constructors forbidden in GLSL 1.10");
478 return ir_call::get_error_instruction();
479 }
480
Ian Romanick00aa1732010-03-31 16:48:48 -0700481 return process_array_constructor(instructions, constructor_type,
Ian Romanick3521f0b2010-05-10 10:47:14 -0700482 & loc, &this->expressions, state);
Ian Romanickb6326ab2010-03-31 16:25:21 -0700483 }
Ian Romanickabef9552010-03-23 15:08:30 -0700484
485 /* There are two kinds of constructor call. Constructors for built-in
486 * language types, such as mat4 and vec2, are free form. The only
487 * requirement is that the parameters must provide enough values of the
488 * correct scalar type. Constructors for arrays and structures must
489 * have the exact number of parameters with matching types in the
490 * correct order. These constructors follow essentially the same type
491 * matching rules as functions.
492 */
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700493 if (constructor_type->is_numeric() || constructor_type->is_boolean()) {
494 /* Constructing a numeric type has a couple steps. First all values
495 * passed to the constructor are broken into individual parameters
496 * and type converted to the base type of the thing being constructed.
497 *
498 * At that point we have some number of values that match the base
499 * type of the thing being constructed. Now the constructor can be
500 * treated like a function call. Each numeric type has a small set
501 * of constructor functions. The set of new parameters will either
502 * match one of those functions or the original constructor is
503 * invalid.
504 */
505 const glsl_type *const base_type = constructor_type->get_base_type();
506
507 /* Total number of components of the type being constructed.
508 */
509 const unsigned type_components = constructor_type->components();
510
511 /* Number of components from parameters that have actually been
512 * consumed. This is used to perform several kinds of error checking.
513 */
514 unsigned components_used = 0;
515
516 unsigned matrix_parameters = 0;
517 unsigned nonmatrix_parameters = 0;
518 exec_list actual_parameters;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700519
Ian Romanick9e08d012010-06-04 16:36:09 -0700520 bool all_parameters_are_constant = true;
521
Ian Romanickfa455fc2010-06-23 13:58:34 -0700522 /* This handles invalid constructor calls such as 'vec4 v = vec4();'
523 */
524 if (this->expressions.is_empty()) {
525 _mesa_glsl_error(& loc, state, "too few components to construct "
526 "`%s'",
527 constructor_type->name);
528 return ir_call::get_error_instruction();
529 }
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700530
Ian Romanick304ea902010-05-10 11:17:53 -0700531 foreach_list (n, &this->expressions) {
532 ast_node *ast = exec_node_data(ast_node, n, link);
Ian Romanick9e08d012010-06-04 16:36:09 -0700533 ir_rvalue *result =
Ian Romanick304ea902010-05-10 11:17:53 -0700534 ast->hir(instructions, state)->as_rvalue();
Eric Anholt53e48d32010-06-22 14:22:42 -0700535 ir_variable *result_var = NULL;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700536
Ian Romanick9e08d012010-06-04 16:36:09 -0700537 /* Attempt to convert the parameter to a constant valued expression.
538 * After doing so, track whether or not all the parameters to the
539 * constructor are trivially constant valued expressions.
540 */
541 ir_rvalue *const constant =
542 result->constant_expression_value();
543
544 if (constant != NULL)
545 result = constant;
546 else
547 all_parameters_are_constant = false;
548
Ian Romanick3521f0b2010-05-10 10:47:14 -0700549 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
550 *
551 * "It is an error to provide extra arguments beyond this
552 * last used argument."
553 */
554 if (components_used >= type_components) {
555 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
556 "constructor",
557 constructor_type->name);
558 return ir_call::get_error_instruction();
559 }
560
561 if (!result->type->is_numeric() && !result->type->is_boolean()) {
562 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
563 "non-numeric data type",
564 constructor_type->name);
565 return ir_call::get_error_instruction();
566 }
567
568 /* Count the number of matrix and nonmatrix parameters. This
569 * is used below to enforce some of the constructor rules.
570 */
571 if (result->type->is_matrix())
572 matrix_parameters++;
573 else
574 nonmatrix_parameters++;
575
Eric Anholt53e48d32010-06-22 14:22:42 -0700576 /* We can't use the same instruction node in the multiple
577 * swizzle dereferences that happen, so assign it to a
578 * variable and deref that. Plus it saves computation for
579 * complicated expressions and handles
580 * glsl-vs-constructor-call.shader_test.
581 */
582 if (result->type->components() >= 1 && !result->as_constant()) {
Carl Worth1660a292010-06-23 18:11:51 -0700583 result_var = new(ctx) ir_variable(result->type,
584 "constructor_tmp");
Eric Anholt53e48d32010-06-22 14:22:42 -0700585 ir_dereference_variable *lhs;
586
Carl Worth1660a292010-06-23 18:11:51 -0700587 lhs = new(ctx) ir_dereference_variable(result_var);
588 instructions->push_tail(new(ctx) ir_assignment(lhs,
589 result, NULL));
Eric Anholt53e48d32010-06-22 14:22:42 -0700590 }
Ian Romanick3521f0b2010-05-10 10:47:14 -0700591
592 /* Process each of the components of the parameter. Dereference
593 * each component individually, perform any type conversions, and
594 * add it to the parameter list for the constructor.
595 */
596 for (unsigned i = 0; i < result->type->components(); i++) {
597 if (components_used >= type_components)
598 break;
599
Eric Anholt53e48d32010-06-22 14:22:42 -0700600 ir_rvalue *component;
601
602 if (result_var) {
Carl Worth1660a292010-06-23 18:11:51 -0700603 ir_dereference *d = new(ctx) ir_dereference_variable(result_var);
Eric Anholt53e48d32010-06-22 14:22:42 -0700604 component = dereference_component(d, i);
605 } else {
606 component = dereference_component(result, i);
607 }
608 component = convert_component(component, base_type);
Ian Romanick3521f0b2010-05-10 10:47:14 -0700609
610 /* All cases that could result in component->type being the
611 * error type should have already been caught above.
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700612 */
Ian Romanick3521f0b2010-05-10 10:47:14 -0700613 assert(component->type == base_type);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700614
Ian Romanick9e08d012010-06-04 16:36:09 -0700615 if (component->as_constant() == NULL)
616 all_parameters_are_constant = false;
617
Ian Romanick3521f0b2010-05-10 10:47:14 -0700618 /* Don't actually generate constructor calls for scalars.
619 * Instead, do the usual component selection and conversion,
620 * and return the single component.
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700621 */
Ian Romanick3521f0b2010-05-10 10:47:14 -0700622 if (constructor_type->is_scalar())
623 return component;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700624
Ian Romanick3521f0b2010-05-10 10:47:14 -0700625 actual_parameters.push_tail(component);
626 components_used++;
627 }
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700628 }
629
630 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
631 *
632 * "It is an error to construct matrices from other matrices. This
633 * is reserved for future use."
634 */
635 if ((state->language_version <= 110) && (matrix_parameters > 0)
636 && constructor_type->is_matrix()) {
637 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
638 "matrix in GLSL 1.10",
639 constructor_type->name);
640 return ir_call::get_error_instruction();
641 }
642
643 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
644 *
645 * "If a matrix argument is given to a matrix constructor, it is
646 * an error to have any other arguments."
647 */
648 if ((matrix_parameters > 0)
649 && ((matrix_parameters + nonmatrix_parameters) > 1)
650 && constructor_type->is_matrix()) {
651 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
652 "matrix must be only parameter",
653 constructor_type->name);
654 return ir_call::get_error_instruction();
655 }
656
657 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
658 *
659 * "In these cases, there must be enough components provided in the
660 * arguments to provide an initializer for every component in the
661 * constructed value."
662 */
Ian Romanick8a24cd52010-03-29 15:36:02 -0700663 if ((components_used < type_components) && (components_used != 1)) {
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700664 _mesa_glsl_error(& loc, state, "too few components to construct "
665 "`%s'",
666 constructor_type->name);
667 return ir_call::get_error_instruction();
668 }
669
670 ir_function *f = state->symbols->get_function(constructor_type->name);
671 if (f == NULL) {
672 _mesa_glsl_error(& loc, state, "no constructor for type `%s'",
673 constructor_type->name);
674 return ir_call::get_error_instruction();
675 }
676
677 const ir_function_signature *sig =
678 f->matching_signature(& actual_parameters);
679 if (sig != NULL) {
Ian Romanick9e08d012010-06-04 16:36:09 -0700680 /* If all of the parameters are trivially constant, create a
681 * constant representing the complete collection of parameters.
682 */
Ian Romanickbe1d2bf2010-06-11 14:01:44 -0700683 if (all_parameters_are_constant) {
684 if (components_used >= type_components)
Carl Worth1660a292010-06-23 18:11:51 -0700685 return new(ctx) ir_constant(sig->return_type,
686 & actual_parameters);
Ian Romanickbe1d2bf2010-06-11 14:01:44 -0700687
688 assert(sig->return_type->is_vector()
689 || sig->return_type->is_matrix());
690
691 /* Constructors with exactly one component are special for
692 * vectors and matrices. For vectors it causes all elements of
693 * the vector to be filled with the value. For matrices it
694 * causes the matrix to be filled with 0 and the diagonal to be
695 * filled with the value.
696 */
697 ir_constant_data data;
698 ir_constant *const initializer =
699 (ir_constant *) actual_parameters.head;
700 if (sig->return_type->is_matrix())
701 generate_constructor_matrix(sig->return_type, initializer,
702 &data);
703 else
704 generate_constructor_vector(sig->return_type, initializer,
705 &data);
706
Carl Worth1660a292010-06-23 18:11:51 -0700707 return new(ctx) ir_constant(sig->return_type, &data);
Ian Romanickbe1d2bf2010-06-11 14:01:44 -0700708 } else
Carl Worth1660a292010-06-23 18:11:51 -0700709 return new(ctx) ir_call(sig, & actual_parameters);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700710 } else {
711 /* FINISHME: Log a better error message here. G++ will show the
712 * FINSIHME: types of the actual parameters and the set of
713 * FINSIHME: candidate functions. A different error should also be
714 * FINSIHME: logged when multiple functions match.
715 */
716 _mesa_glsl_error(& loc, state, "no matching constructor for `%s'",
717 constructor_type->name);
718 return ir_call::get_error_instruction();
719 }
720 }
Ian Romanickabef9552010-03-23 15:08:30 -0700721
Ian Romanick548fa292010-03-15 13:04:13 -0700722 return ir_call::get_error_instruction();
723 } else {
724 const ast_expression *id = subexpressions[0];
Ian Romanickf4749612010-03-15 13:26:02 -0700725 YYLTYPE loc = id->get_location();
Ian Romanickc0771312010-06-09 17:23:26 -0700726 exec_list actual_parameters;
727
728 process_parameters(instructions, &actual_parameters, &this->expressions,
729 state);
Ian Romanick548fa292010-03-15 13:04:13 -0700730
Ian Romanickab92d0e2010-06-09 17:26:20 -0700731 const glsl_type *const type =
732 state->symbols->get_type(id->primary_expression.identifier);
733
734 if ((type != NULL) && type->is_record()) {
735 ir_constant *constant =
736 constant_record_constructor(type, &loc, &actual_parameters, state);
737
738 if (constant != NULL)
739 return constant;
740 }
741
Ian Romanickf4749612010-03-15 13:26:02 -0700742 return match_function_by_name(instructions,
743 id->primary_expression.identifier, & loc,
Ian Romanickc0771312010-06-09 17:23:26 -0700744 &actual_parameters, state);
Ian Romanick548fa292010-03-15 13:04:13 -0700745 }
746
747 return ir_call::get_error_instruction();
748}