blob: 9315a92ecbc90272d653d5d9293a73c0113588ca [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 Romanick81c7e942010-06-25 16:10:43 -070029inline unsigned min(unsigned a, unsigned b)
30{
31 return (a < b) ? a : b;
32}
33
Ian Romanick68515ee2010-03-31 16:28:51 -070034static unsigned
35process_parameters(exec_list *instructions, exec_list *actual_parameters,
Ian Romanick304ea902010-05-10 11:17:53 -070036 exec_list *parameters,
Ian Romanick68515ee2010-03-31 16:28:51 -070037 struct _mesa_glsl_parse_state *state)
38{
Ian Romanick68515ee2010-03-31 16:28:51 -070039 unsigned count = 0;
40
Ian Romanick304ea902010-05-10 11:17:53 -070041 foreach_list (n, parameters) {
42 ast_node *const ast = exec_node_data(ast_node, n, link);
Ian Romanick1a872b12010-06-09 17:31:02 -070043 ir_rvalue *result = ast->hir(instructions, state);
44
45 ir_constant *const constant = result->constant_expression_value();
46 if (constant != NULL)
47 result = constant;
Ian Romanick68515ee2010-03-31 16:28:51 -070048
Ian Romanick3521f0b2010-05-10 10:47:14 -070049 actual_parameters->push_tail(result);
50 count++;
Ian Romanick68515ee2010-03-31 16:28:51 -070051 }
52
53 return count;
54}
55
56
57static ir_rvalue *
58process_call(exec_list *instructions, ir_function *f,
59 YYLTYPE *loc, exec_list *actual_parameters,
60 struct _mesa_glsl_parse_state *state)
61{
Kenneth Graunke953ff122010-06-25 13:14:37 -070062 void *ctx = state;
Carl Worth1660a292010-06-23 18:11:51 -070063
Ian Romanick68515ee2010-03-31 16:28:51 -070064 const ir_function_signature *sig =
65 f->matching_signature(actual_parameters);
66
67 /* The instructions param will be used when the FINISHMEs below are done */
68 (void) instructions;
69
70 if (sig != NULL) {
Ian Romanickc35bb002010-04-02 15:51:02 -070071 /* Verify that 'out' and 'inout' actual parameters are lvalues. This
72 * isn't done in ir_function::matching_signature because that function
73 * cannot generate the necessary diagnostics.
74 */
75 exec_list_iterator actual_iter = actual_parameters->iterator();
76 exec_list_iterator formal_iter = sig->parameters.iterator();
77
78 while (actual_iter.has_next()) {
Eric Anholtf1ddca92010-04-07 12:35:34 -070079 ir_rvalue *actual = (ir_rvalue *) actual_iter.get();
80 ir_variable *formal = (ir_variable *) formal_iter.get();
Ian Romanickc35bb002010-04-02 15:51:02 -070081
82 assert(actual != NULL);
83 assert(formal != NULL);
84
85 if ((formal->mode == ir_var_out)
86 || (formal->mode == ir_var_inout)) {
87 if (! actual->is_lvalue()) {
88 /* FINISHME: Log a better diagnostic here. There is no way
89 * FINISHME: to tell the user which parameter is invalid.
90 */
91 _mesa_glsl_error(loc, state, "`%s' parameter is not lvalue",
92 (formal->mode == ir_var_out) ? "out" : "inout");
93 }
94 }
95
96 actual_iter.next();
97 formal_iter.next();
98 }
99
Ian Romanick68515ee2010-03-31 16:28:51 -0700100 /* FINISHME: The list of actual parameters needs to be modified to
101 * FINISHME: include any necessary conversions.
102 */
Carl Worth1660a292010-06-23 18:11:51 -0700103 return new(ctx) ir_call(sig, actual_parameters);
Ian Romanick68515ee2010-03-31 16:28:51 -0700104 } else {
105 /* FINISHME: Log a better error message here. G++ will show the types
106 * FINISHME: of the actual parameters and the set of candidate
107 * FINISHME: functions. A different error should also be logged when
108 * FINISHME: multiple functions match.
109 */
110 _mesa_glsl_error(loc, state, "no matching function for call to `%s'",
111 f->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700112 return ir_call::get_error_instruction(ctx);
Ian Romanick68515ee2010-03-31 16:28:51 -0700113 }
114}
115
116
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700117static ir_rvalue *
Ian Romanickf4749612010-03-15 13:26:02 -0700118match_function_by_name(exec_list *instructions, const char *name,
Ian Romanickc0771312010-06-09 17:23:26 -0700119 YYLTYPE *loc, exec_list *actual_parameters,
Ian Romanickf4749612010-03-15 13:26:02 -0700120 struct _mesa_glsl_parse_state *state)
121{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700122 void *ctx = state;
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700123 ir_function *f = state->symbols->get_function(name);
Ian Romanickf4749612010-03-15 13:26:02 -0700124
125 if (f == NULL) {
126 _mesa_glsl_error(loc, state, "function `%s' undeclared", name);
Carl Worthe01193a2010-06-23 18:25:04 -0700127 return ir_call::get_error_instruction(ctx);
Ian Romanickf4749612010-03-15 13:26:02 -0700128 }
129
Ian Romanickc0771312010-06-09 17:23:26 -0700130 /* Once we've determined that the function being called might exist, try
131 * to find an overload of the function that matches the parameters.
Ian Romanickf4749612010-03-15 13:26:02 -0700132 */
Ian Romanickc0771312010-06-09 17:23:26 -0700133 return process_call(instructions, f, loc, actual_parameters, state);
Ian Romanickf4749612010-03-15 13:26:02 -0700134}
135
136
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700137/**
138 * Perform automatic type conversion of constructor parameters
139 */
140static ir_rvalue *
141convert_component(ir_rvalue *src, const glsl_type *desired_type)
142{
Carl Worth1660a292010-06-23 18:11:51 -0700143 void *ctx = talloc_parent(src);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700144 const unsigned a = desired_type->base_type;
145 const unsigned b = src->type->base_type;
Ian Romanick00eb4662010-06-07 15:08:04 -0700146 ir_expression *result = NULL;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700147
148 if (src->type->is_error())
149 return src;
150
151 assert(a <= GLSL_TYPE_BOOL);
152 assert(b <= GLSL_TYPE_BOOL);
153
154 if ((a == b) || (src->type->is_integer() && desired_type->is_integer()))
155 return src;
156
157 switch (a) {
158 case GLSL_TYPE_UINT:
159 case GLSL_TYPE_INT:
160 if (b == GLSL_TYPE_FLOAT)
Carl Worth1660a292010-06-23 18:11:51 -0700161 result = new(ctx) ir_expression(ir_unop_f2i, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700162 else {
163 assert(b == GLSL_TYPE_BOOL);
Carl Worth1660a292010-06-23 18:11:51 -0700164 result = new(ctx) ir_expression(ir_unop_b2i, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700165 }
Ian Romanick565185c2010-06-11 13:49:00 -0700166 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700167 case GLSL_TYPE_FLOAT:
168 switch (b) {
169 case GLSL_TYPE_UINT:
Carl Worth1660a292010-06-23 18:11:51 -0700170 result = new(ctx) ir_expression(ir_unop_u2f, desired_type, src, NULL);
Ian Romanick00eb4662010-06-07 15:08:04 -0700171 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700172 case GLSL_TYPE_INT:
Carl Worth1660a292010-06-23 18:11:51 -0700173 result = new(ctx) ir_expression(ir_unop_i2f, desired_type, src, NULL);
Ian Romanick00eb4662010-06-07 15:08:04 -0700174 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700175 case GLSL_TYPE_BOOL:
Carl Worth1660a292010-06-23 18:11:51 -0700176 result = new(ctx) ir_expression(ir_unop_b2f, desired_type, src, NULL);
Ian Romanick00eb4662010-06-07 15:08:04 -0700177 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700178 }
179 break;
Ian Romanick26b5d332010-06-25 16:19:45 -0700180 case GLSL_TYPE_BOOL:
Ian Romanickb74b43e2010-06-11 16:52:09 -0700181 switch (b) {
Ian Romanick26b5d332010-06-25 16:19:45 -0700182 case GLSL_TYPE_UINT:
183 case GLSL_TYPE_INT:
184 result = new(ctx) ir_expression(ir_unop_i2b, desired_type, src, NULL);
185 break;
186 case GLSL_TYPE_FLOAT:
187 result = new(ctx) ir_expression(ir_unop_f2b, desired_type, src, NULL);
188 break;
Ian Romanickb74b43e2010-06-11 16:52:09 -0700189 }
Ian Romanick26b5d332010-06-25 16:19:45 -0700190 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700191 }
192
Ian Romanick00eb4662010-06-07 15:08:04 -0700193 assert(result != NULL);
194
195 ir_constant *const constant = result->constant_expression_value();
196 return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700197}
198
199
200/**
201 * Dereference a specific component from a scalar, vector, or matrix
202 */
203static ir_rvalue *
204dereference_component(ir_rvalue *src, unsigned component)
205{
Carl Worth1660a292010-06-23 18:11:51 -0700206 void *ctx = talloc_parent(src);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700207 assert(component < src->type->components());
208
Ian Romanickc9cb1032010-06-04 16:20:35 -0700209 /* If the source is a constant, just create a new constant instead of a
210 * dereference of the existing constant.
211 */
212 ir_constant *constant = src->as_constant();
213 if (constant)
Carl Worth1660a292010-06-23 18:11:51 -0700214 return new(ctx) ir_constant(constant, component);
Ian Romanickc9cb1032010-06-04 16:20:35 -0700215
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700216 if (src->type->is_scalar()) {
217 return src;
218 } else if (src->type->is_vector()) {
Carl Worth1660a292010-06-23 18:11:51 -0700219 return new(ctx) ir_swizzle(src, component, 0, 0, 0, 1);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700220 } else {
221 assert(src->type->is_matrix());
222
223 /* Dereference a row of the matrix, then call this function again to get
224 * a specific element from that row.
225 */
226 const int c = component / src->type->column_type()->vector_elements;
227 const int r = component % src->type->column_type()->vector_elements;
Carl Worth1660a292010-06-23 18:11:51 -0700228 ir_constant *const col_index = new(ctx) ir_constant(c);
229 ir_dereference *const col = new(ctx) ir_dereference_array(src, col_index);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700230
231 col->type = src->type->column_type();
232
233 return dereference_component(col, r);
234 }
235
236 assert(!"Should not get here.");
237 return NULL;
238}
239
240
Ian Romanick00aa1732010-03-31 16:48:48 -0700241static ir_rvalue *
242process_array_constructor(exec_list *instructions,
243 const glsl_type *constructor_type,
Ian Romanick304ea902010-05-10 11:17:53 -0700244 YYLTYPE *loc, exec_list *parameters,
Ian Romanick00aa1732010-03-31 16:48:48 -0700245 struct _mesa_glsl_parse_state *state)
246{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700247 void *ctx = state;
Ian Romanick00aa1732010-03-31 16:48:48 -0700248 /* Array constructors come in two forms: sized and unsized. Sized array
249 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
250 * variables. In this case the number of parameters must exactly match the
251 * specified size of the array.
252 *
253 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
254 * are vec4 variables. In this case the size of the array being constructed
255 * is determined by the number of parameters.
256 *
257 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
258 *
259 * "There must be exactly the same number of arguments as the size of
260 * the array being constructed. If no size is present in the
261 * constructor, then the array is explicitly sized to the number of
262 * arguments provided. The arguments are assigned in order, starting at
263 * element 0, to the elements of the constructed array. Each argument
264 * must be the same type as the element type of the array, or be a type
265 * that can be converted to the element type of the array according to
266 * Section 4.1.10 "Implicit Conversions.""
267 */
268 exec_list actual_parameters;
269 const unsigned parameter_count =
270 process_parameters(instructions, &actual_parameters, parameters, state);
271
272 if ((parameter_count == 0)
273 || ((constructor_type->length != 0)
274 && (constructor_type->length != parameter_count))) {
275 const unsigned min_param = (constructor_type->length == 0)
276 ? 1 : constructor_type->length;
277
278 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
279 "parameter%s",
280 (constructor_type->length != 0) ? "at least" : "exactly",
281 min_param, (min_param <= 1) ? "" : "s");
Carl Worthe01193a2010-06-23 18:25:04 -0700282 return ir_call::get_error_instruction(ctx);
Ian Romanick00aa1732010-03-31 16:48:48 -0700283 }
284
285 if (constructor_type->length == 0) {
286 constructor_type =
Carl Worth12c41152010-06-18 17:52:59 -0700287 glsl_type::get_array_instance(state,
288 constructor_type->element_type(),
Ian Romanick00aa1732010-03-31 16:48:48 -0700289 parameter_count);
290 assert(constructor_type != NULL);
291 assert(constructor_type->length == parameter_count);
292 }
293
294 ir_function *f = state->symbols->get_function(constructor_type->name);
295
296 /* If the constructor for this type of array does not exist, generate the
Ian Romanick82baaf42010-04-23 13:21:22 -0700297 * prototype and add it to the symbol table.
Ian Romanick00aa1732010-03-31 16:48:48 -0700298 */
299 if (f == NULL) {
Ian Romanick82baaf42010-04-23 13:21:22 -0700300 f = constructor_type->generate_constructor(state->symbols);
Ian Romanick00aa1732010-03-31 16:48:48 -0700301 }
302
303 ir_rvalue *const r =
304 process_call(instructions, f, loc, &actual_parameters, state);
305
306 assert(r != NULL);
307 assert(r->type->is_error() || (r->type == constructor_type));
308
309 return r;
310}
311
312
Ian Romanickab92d0e2010-06-09 17:26:20 -0700313/**
314 * Try to convert a record constructor to a constant expression
315 */
316static ir_constant *
317constant_record_constructor(const glsl_type *constructor_type,
318 YYLTYPE *loc, exec_list *parameters,
319 struct _mesa_glsl_parse_state *state)
320{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700321 void *ctx = state;
Ian Romanickab92d0e2010-06-09 17:26:20 -0700322 bool all_parameters_are_constant = true;
323
324 exec_node *node = parameters->head;
325 for (unsigned i = 0; i < constructor_type->length; i++) {
326 ir_instruction *ir = (ir_instruction *) node;
327
328 if (node->is_tail_sentinal()) {
329 _mesa_glsl_error(loc, state,
330 "insufficient parameters to constructor for `%s'",
331 constructor_type->name);
332 return NULL;
333 }
334
335 if (ir->type != constructor_type->fields.structure[i].type) {
336 _mesa_glsl_error(loc, state,
337 "parameter type mismatch in constructor for `%s' "
338 " (%s vs %s)",
339 constructor_type->name,
340 ir->type->name,
341 constructor_type->fields.structure[i].type->name);
342 return NULL;
343 }
344
345 if (ir->as_constant() == NULL)
346 all_parameters_are_constant = false;
347
348 node = node->next;
349 }
350
351 if (!all_parameters_are_constant)
352 return NULL;
353
Carl Worth1660a292010-06-23 18:11:51 -0700354 return new(ctx) ir_constant(constructor_type, parameters);
Ian Romanickab92d0e2010-06-09 17:26:20 -0700355}
356
357
Ian Romanickbe1d2bf2010-06-11 14:01:44 -0700358/**
359 * Generate data for a constant matrix constructor w/a single scalar parameter
360 *
361 * Matrix constructors in GLSL can be passed a single scalar of the
362 * approriate type. In these cases, the resulting matrix is the identity
363 * matrix multipled by the specified scalar. This function generates data for
364 * that matrix.
365 *
366 * \param type Type of the desired matrix.
367 * \param initializer Scalar value used to initialize the matrix diagonal.
368 * \param data Location to store the resulting matrix.
369 */
370void
371generate_constructor_matrix(const glsl_type *type, ir_constant *initializer,
372 ir_constant_data *data)
373{
374 switch (type->base_type) {
375 case GLSL_TYPE_UINT:
376 case GLSL_TYPE_INT:
377 for (unsigned i = 0; i < type->components(); i++)
378 data->u[i] = 0;
379
380 for (unsigned i = 0; i < type->matrix_columns; i++) {
381 /* The array offset of the ith row and column of the matrix.
382 */
383 const unsigned idx = (i * type->vector_elements) + i;
384
385 data->u[idx] = initializer->value.u[0];
386 }
387 break;
388
389 case GLSL_TYPE_FLOAT:
390 for (unsigned i = 0; i < type->components(); i++)
391 data->f[i] = 0;
392
393 for (unsigned i = 0; i < type->matrix_columns; i++) {
394 /* The array offset of the ith row and column of the matrix.
395 */
396 const unsigned idx = (i * type->vector_elements) + i;
397
398 data->f[idx] = initializer->value.f[0];
399 }
400
401 break;
402
403 default:
404 assert(!"Should not get here.");
405 break;
406 }
407}
408
409
410/**
411 * Generate data for a constant vector constructor w/a single scalar parameter
412 *
413 * Vector constructors in GLSL can be passed a single scalar of the
414 * approriate type. In these cases, the resulting vector contains the specified
415 * value in all components. This function generates data for that vector.
416 *
417 * \param type Type of the desired vector.
418 * \param initializer Scalar value used to initialize the vector.
419 * \param data Location to store the resulting vector data.
420 */
421void
422generate_constructor_vector(const glsl_type *type, ir_constant *initializer,
423 ir_constant_data *data)
424{
425 switch (type->base_type) {
426 case GLSL_TYPE_UINT:
427 case GLSL_TYPE_INT:
428 for (unsigned i = 0; i < type->components(); i++)
429 data->u[i] = initializer->value.u[0];
430
431 break;
432
433 case GLSL_TYPE_FLOAT:
434 for (unsigned i = 0; i < type->components(); i++)
435 data->f[i] = initializer->value.f[0];
436
437 break;
438
439 case GLSL_TYPE_BOOL:
440 for (unsigned i = 0; i < type->components(); i++)
441 data->b[i] = initializer->value.b[0];
442
443 break;
444
445 default:
446 assert(!"Should not get here.");
447 break;
448 }
449}
450
451
Ian Romanickc31dcdf2010-06-23 15:19:40 -0700452/**
453 * Determine if a list consists of a single scalar r-value
454 */
455bool
456single_scalar_parameter(exec_list *parameters)
457{
458 const ir_rvalue *const p = (ir_rvalue *) parameters->head;
459 assert(((ir_rvalue *)p)->as_rvalue() != NULL);
460
461 return (p->type->is_scalar() && p->next->is_tail_sentinal());
462}
463
464
465/**
466 * Generate inline code for a vector constructor
467 *
468 * The generated constructor code will consist of a temporary variable
469 * declaration of the same type as the constructor. A sequence of assignments
470 * from constructor parameters to the temporary will follow.
471 *
472 * \return
473 * An \c ir_dereference_variable of the temprorary generated in the constructor
474 * body.
475 */
476ir_rvalue *
477emit_inline_vector_constructor(const glsl_type *type,
478 exec_list *instructions,
479 exec_list *parameters,
480 void *ctx)
481{
482 assert(!parameters->is_empty());
483
Ian Romanick4b6feb02010-06-28 13:22:55 -0700484 ir_variable *var = new(ctx) ir_variable(type,
485 talloc_strdup(ctx, "vec_ctor"));
Ian Romanickc31dcdf2010-06-23 15:19:40 -0700486 instructions->push_tail(var);
487
488 /* There are two kinds of vector constructors.
489 *
490 * - Construct a vector from a single scalar by replicating that scalar to
491 * all components of the vector.
492 *
493 * - Construct a vector from an arbirary combination of vectors and
494 * scalars. The components of the constructor parameters are assigned
495 * to the vector in order until the vector is full.
496 */
497 const unsigned lhs_components = type->components();
498 if (single_scalar_parameter(parameters)) {
499 ir_rvalue *first_param = (ir_rvalue *)parameters->head;
500 ir_rvalue *rhs = new(ctx) ir_swizzle(first_param, 0, 0, 0, 0,
501 lhs_components);
502 ir_dereference_variable *lhs = new(ctx) ir_dereference_variable(var);
503
504 assert(rhs->type == lhs->type);
505
506 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
507 instructions->push_tail(inst);
508 } else {
509 unsigned base_component = 0;
510 foreach_list(node, parameters) {
511 ir_rvalue *rhs = (ir_rvalue *) node;
512 unsigned rhs_components = rhs->type->components();
513
514 /* Do not try to assign more components to the vector than it has!
515 */
516 if ((rhs_components + base_component) > lhs_components) {
517 rhs_components = lhs_components - base_component;
518 }
519
520 /* Emit an assignment of the constructor parameter to the next set of
521 * components in the temporary variable.
522 */
523 unsigned mask[4] = { 0, 0, 0, 0 };
524 for (unsigned i = 0; i < rhs_components; i++) {
525 mask[i] = i + base_component;
526 }
527
528
529 ir_rvalue *lhs_ref = new(ctx) ir_dereference_variable(var);
530 ir_swizzle *lhs = new(ctx) ir_swizzle(lhs_ref, mask, rhs_components);
531
532 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
533 instructions->push_tail(inst);
534
535 /* Advance the component index by the number of components that were
536 * just assigned.
537 */
538 base_component += rhs_components;
539 }
540 }
541 return new(ctx) ir_dereference_variable(var);
542}
543
544
Ian Romanick81c7e942010-06-25 16:10:43 -0700545/**
546 * Generate assignment of a portion of a vector to a portion of a matrix column
547 *
548 * \param src_base First component of the source to be used in assignment
549 * \param column Column of destination to be assiged
550 * \param row_base First component of the destination column to be assigned
551 * \param count Number of components to be assigned
552 *
553 * \note
554 * \c src_base + \c count must be less than or equal to the number of components
555 * in the source vector.
556 */
557ir_instruction *
558assign_to_matrix_column(ir_variable *var, unsigned column, unsigned row_base,
559 ir_rvalue *src, unsigned src_base, unsigned count,
560 TALLOC_CTX *ctx)
561{
562 const unsigned mask[8] = { 0, 1, 2, 3, 0, 0, 0, 0 };
563
564 ir_constant *col_idx = new(ctx) ir_constant(column);
565 ir_rvalue *column_ref = new(ctx) ir_dereference_array(var, col_idx);
566
567 assert(column_ref->type->components() >= (row_base + count));
568 ir_rvalue *lhs = new(ctx) ir_swizzle(column_ref, &mask[row_base], count);
569
570 assert(src->type->components() >= (src_base + count));
571 ir_rvalue *rhs = new(ctx) ir_swizzle(src, &mask[src_base], count);
572
573 return new(ctx) ir_assignment(lhs, rhs, NULL);
574}
575
576
577/**
578 * Generate inline code for a matrix constructor
579 *
580 * The generated constructor code will consist of a temporary variable
581 * declaration of the same type as the constructor. A sequence of assignments
582 * from constructor parameters to the temporary will follow.
583 *
584 * \return
585 * An \c ir_dereference_variable of the temprorary generated in the constructor
586 * body.
587 */
588ir_rvalue *
589emit_inline_matrix_constructor(const glsl_type *type,
590 exec_list *instructions,
591 exec_list *parameters,
592 void *ctx)
593{
594 assert(!parameters->is_empty());
595
Ian Romanick4b6feb02010-06-28 13:22:55 -0700596 ir_variable *var = new(ctx) ir_variable(type,
597 talloc_strdup(ctx, "mat_ctor"));
Ian Romanick81c7e942010-06-25 16:10:43 -0700598 instructions->push_tail(var);
599
600 /* There are three kinds of matrix constructors.
601 *
602 * - Construct a matrix from a single scalar by replicating that scalar to
603 * along the diagonal of the matrix and setting all other components to
604 * zero.
605 *
606 * - Construct a matrix from an arbirary combination of vectors and
607 * scalars. The components of the constructor parameters are assigned
608 * to the matrix in colum-major order until the matrix is full.
609 *
610 * - Construct a matrix from a single matrix. The source matrix is copied
611 * to the upper left portion of the constructed matrix, and the remaining
612 * elements take values from the identity matrix.
613 */
614 ir_rvalue *const first_param = (ir_rvalue *) parameters->head;
615 if (single_scalar_parameter(parameters)) {
616 /* Assign the scalar to the X component of a vec4, and fill the remaining
617 * components with zero.
618 */
Ian Romanick4b6feb02010-06-28 13:22:55 -0700619 ir_variable *rhs_var =
620 new(ctx) ir_variable(glsl_type::vec4_type,
621 talloc_strdup(ctx, "mat_ctor_vec"));
Ian Romanick81c7e942010-06-25 16:10:43 -0700622 instructions->push_tail(rhs_var);
623
624 ir_constant_data zero;
625 zero.f[0] = 0.0;
626 zero.f[1] = 0.0;
627 zero.f[2] = 0.0;
628 zero.f[3] = 0.0;
629
630 ir_instruction *inst =
631 new(ctx) ir_assignment(new(ctx) ir_dereference_variable(rhs_var),
632 new(ctx) ir_constant(rhs_var->type, &zero),
633 NULL);
634 instructions->push_tail(inst);
635
636 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
637 ir_rvalue *const x_of_rhs = new(ctx) ir_swizzle(rhs_ref, 0, 0, 0, 0, 1);
638
639 inst = new(ctx) ir_assignment(x_of_rhs, first_param, NULL);
640 instructions->push_tail(inst);
641
642 /* Assign the temporary vector to each column of the destination matrix
643 * with a swizzle that puts the X component on the diagonal of the
644 * matrix. In some cases this may mean that the X component does not
645 * get assigned into the column at all (i.e., when the matrix has more
646 * columns than rows).
647 */
648 static const unsigned rhs_swiz[4][4] = {
649 { 0, 1, 1, 1 },
650 { 1, 0, 1, 1 },
651 { 1, 1, 0, 1 },
652 { 1, 1, 1, 0 }
653 };
654
655 const unsigned cols_to_init = min(type->matrix_columns,
656 type->vector_elements);
657 for (unsigned i = 0; i < cols_to_init; i++) {
658 ir_constant *const col_idx = new(ctx) ir_constant(i);
659 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
660
661 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
662 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, rhs_swiz[i],
663 type->vector_elements);
664
665 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
666 instructions->push_tail(inst);
667 }
668
669 for (unsigned i = cols_to_init; i < type->matrix_columns; i++) {
670 ir_constant *const col_idx = new(ctx) ir_constant(i);
671 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
672
673 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
674 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, 1, 1, 1, 1,
675 type->vector_elements);
676
677 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
678 instructions->push_tail(inst);
679 }
680 } else if (first_param->type->is_matrix()) {
681 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
682 *
683 * "If a matrix is constructed from a matrix, then each component
684 * (column i, row j) in the result that has a corresponding
685 * component (column i, row j) in the argument will be initialized
686 * from there. All other components will be initialized to the
687 * identity matrix. If a matrix argument is given to a matrix
688 * constructor, it is an error to have any other arguments."
689 */
690 assert(first_param->next->is_tail_sentinal());
691 ir_rvalue *const src_matrix = first_param;
692
693 /* If the source matrix is smaller, pre-initialize the relavent parts of
694 * the destination matrix to the identity matrix.
695 */
696 if ((src_matrix->type->matrix_columns < var->type->matrix_columns)
697 || (src_matrix->type->vector_elements < var->type->vector_elements)) {
698
699 /* If the source matrix has fewer rows, every column of the destination
700 * must be initialized. Otherwise only the columns in the destination
701 * that do not exist in the source must be initialized.
702 */
703 unsigned col =
704 (src_matrix->type->vector_elements < var->type->vector_elements)
705 ? 0 : src_matrix->type->matrix_columns;
706
707 const glsl_type *const col_type = var->type->column_type();
708 for (/* empty */; col < var->type->matrix_columns; col++) {
709 ir_constant_data ident;
710
711 ident.f[0] = 0.0;
712 ident.f[1] = 0.0;
713 ident.f[2] = 0.0;
714 ident.f[3] = 0.0;
715
716 ident.f[col] = 1.0;
717
718 ir_rvalue *const rhs = new(ctx) ir_constant(col_type, &ident);
719
720 ir_rvalue *const lhs =
721 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(col));
722
723 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
724 instructions->push_tail(inst);
725 }
726 }
727
728 /* Assign columns from the source matrix to the destination matrix.
729 *
730 * Since the parameter will be used in the RHS of multiple assignments,
731 * generate a temporary and copy the paramter there.
732 */
Ian Romanick4b6feb02010-06-28 13:22:55 -0700733 ir_variable *const rhs_var =
734 new(ctx) ir_variable(first_param->type,
735 talloc_strdup(ctx, "mat_ctor_mat"));
Ian Romanick81c7e942010-06-25 16:10:43 -0700736 instructions->push_tail(rhs_var);
737
738 ir_dereference *const rhs_var_ref =
739 new(ctx) ir_dereference_variable(rhs_var);
740 ir_instruction *const inst =
741 new(ctx) ir_assignment(rhs_var_ref, first_param, NULL);
742 instructions->push_tail(inst);
743
744
745 const unsigned swiz[4] = { 0, 1, 2, 3 };
746 const unsigned last_col = min(src_matrix->type->matrix_columns,
747 var->type->matrix_columns);
748 for (unsigned i = 0; i < last_col; i++) {
749 ir_rvalue *const lhs_col =
750 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(i));
751 ir_rvalue *const rhs_col =
752 new(ctx) ir_dereference_array(rhs_var, new(ctx) ir_constant(i));
753
754 /* If one matrix has columns that are smaller than the columns of the
755 * other matrix, wrap the column access of the larger with a swizzle
756 * so that the LHS and RHS of the assignment have the same size (and
757 * therefore have the same type).
758 *
759 * It would be perfectly valid to unconditionally generate the
760 * swizzles, this this will typically result in a more compact IR tree.
761 */
762 ir_rvalue *lhs;
763 ir_rvalue *rhs;
764 if (lhs_col->type->vector_elements < rhs_col->type->vector_elements) {
765 lhs = lhs_col;
766
767 rhs = new(ctx) ir_swizzle(rhs_col, swiz,
768 lhs_col->type->vector_elements);
769 } else if (lhs_col->type->vector_elements
770 > rhs_col->type->vector_elements) {
771 lhs = new(ctx) ir_swizzle(lhs_col, swiz,
772 rhs_col->type->vector_elements);
773 rhs = rhs_col;
774 } else {
775 lhs = lhs_col;
776 rhs = rhs_col;
777 }
778
779 assert(lhs->type == rhs->type);
780
781 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
782 instructions->push_tail(inst);
783 }
784 } else {
785 const unsigned rows = type->matrix_columns;
786 const unsigned cols = type->vector_elements;
787 unsigned col_idx = 0;
788 unsigned row_idx = 0;
789
790 foreach_list (node, parameters) {
791 ir_rvalue *const rhs = (ir_rvalue *) node;
792 const unsigned components_remaining_this_column = rows - row_idx;
793 unsigned rhs_components = rhs->type->components();
794 unsigned rhs_base = 0;
795
796 /* Since the parameter might be used in the RHS of two assignments,
797 * generate a temporary and copy the paramter there.
798 */
Ian Romanick4b6feb02010-06-28 13:22:55 -0700799 ir_variable *rhs_var =
800 new(ctx) ir_variable(rhs->type,
801 talloc_strdup(ctx, "mat_ctor_vec"));
Ian Romanick81c7e942010-06-25 16:10:43 -0700802 instructions->push_tail(rhs_var);
803
804 ir_dereference *rhs_var_ref =
805 new(ctx) ir_dereference_variable(rhs_var);
806 ir_instruction *inst = new(ctx) ir_assignment(rhs_var_ref, rhs, NULL);
807 instructions->push_tail(inst);
808
809 /* Assign the current parameter to as many components of the matrix
810 * as it will fill.
811 *
812 * NOTE: A single vector parameter can span two matrix columns. A
813 * single vec4, for example, can completely fill a mat2.
814 */
815 if (rhs_components >= components_remaining_this_column) {
816 const unsigned count = min(rhs_components,
817 components_remaining_this_column);
818
819 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
820
821 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
822 row_idx,
823 rhs_var_ref, 0,
824 count, ctx);
825 instructions->push_tail(inst);
826
827 rhs_base = count;
828
829 col_idx++;
830 row_idx = 0;
831 }
832
833 /* If there is data left in the parameter and components left to be
834 * set in the destination, emit another assignment. It is possible
835 * that the assignment could be of a vec4 to the last element of the
836 * matrix. In this case col_idx==cols, but there is still data
837 * left in the source parameter. Obviously, don't emit an assignment
838 * to data outside the destination matrix.
839 */
840 if ((col_idx < cols) && (rhs_base < rhs_components)) {
841 const unsigned count = rhs_components - rhs_base;
842
843 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
844
845 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
846 row_idx,
847 rhs_var_ref,
848 rhs_base,
849 count, ctx);
850 instructions->push_tail(inst);
851
852 row_idx += count;
853 }
854 }
855 }
856
857 return new(ctx) ir_dereference_variable(var);
858}
859
860
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700861ir_rvalue *
Ian Romanick548fa292010-03-15 13:04:13 -0700862ast_function_expression::hir(exec_list *instructions,
863 struct _mesa_glsl_parse_state *state)
864{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700865 void *ctx = state;
Ian Romanick548fa292010-03-15 13:04:13 -0700866 /* There are three sorts of function calls.
867 *
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700868 * 1. constructors - The first subexpression is an ast_type_specifier.
Ian Romanick548fa292010-03-15 13:04:13 -0700869 * 2. methods - Only the .length() method of array types.
870 * 3. functions - Calls to regular old functions.
871 *
Ian Romanick548fa292010-03-15 13:04:13 -0700872 * Method calls are actually detected when the ast_field_selection
873 * expression is handled.
874 */
875 if (is_constructor()) {
Ian Romanickabef9552010-03-23 15:08:30 -0700876 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
877 YYLTYPE loc = type->get_location();
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700878 const char *name;
Ian Romanickabef9552010-03-23 15:08:30 -0700879
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700880 const glsl_type *const constructor_type = type->glsl_type(& name, state);
Ian Romanickabef9552010-03-23 15:08:30 -0700881
882
883 /* Constructors for samplers are illegal.
884 */
885 if (constructor_type->is_sampler()) {
886 _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
887 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700888 return ir_call::get_error_instruction(ctx);
Ian Romanickabef9552010-03-23 15:08:30 -0700889 }
890
Ian Romanickb6326ab2010-03-31 16:25:21 -0700891 if (constructor_type->is_array()) {
892 if (state->language_version <= 110) {
893 _mesa_glsl_error(& loc, state,
894 "array constructors forbidden in GLSL 1.10");
Carl Worthe01193a2010-06-23 18:25:04 -0700895 return ir_call::get_error_instruction(ctx);
Ian Romanickb6326ab2010-03-31 16:25:21 -0700896 }
897
Ian Romanick00aa1732010-03-31 16:48:48 -0700898 return process_array_constructor(instructions, constructor_type,
Ian Romanick3521f0b2010-05-10 10:47:14 -0700899 & loc, &this->expressions, state);
Ian Romanickb6326ab2010-03-31 16:25:21 -0700900 }
Ian Romanickabef9552010-03-23 15:08:30 -0700901
902 /* There are two kinds of constructor call. Constructors for built-in
903 * language types, such as mat4 and vec2, are free form. The only
904 * requirement is that the parameters must provide enough values of the
905 * correct scalar type. Constructors for arrays and structures must
906 * have the exact number of parameters with matching types in the
907 * correct order. These constructors follow essentially the same type
908 * matching rules as functions.
909 */
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700910 if (!constructor_type->is_numeric() && !constructor_type->is_boolean())
911 return ir_call::get_error_instruction(ctx);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700912
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700913 /* Total number of components of the type being constructed. */
914 const unsigned type_components = constructor_type->components();
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700915
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700916 /* Number of components from parameters that have actually been
917 * consumed. This is used to perform several kinds of error checking.
918 */
919 unsigned components_used = 0;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700920
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700921 unsigned matrix_parameters = 0;
922 unsigned nonmatrix_parameters = 0;
923 exec_list actual_parameters;
Ian Romanick9e08d012010-06-04 16:36:09 -0700924
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700925 foreach_list (n, &this->expressions) {
926 ast_node *ast = exec_node_data(ast_node, n, link);
927 ir_rvalue *result = ast->hir(instructions, state)->as_rvalue();
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700928
929 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
930 *
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700931 * "It is an error to provide extra arguments beyond this
932 * last used argument."
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700933 */
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700934 if (components_used >= type_components) {
935 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
936 "constructor",
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700937 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700938 return ir_call::get_error_instruction(ctx);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700939 }
940
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700941 if (!result->type->is_numeric() && !result->type->is_boolean()) {
942 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
943 "non-numeric data type",
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700944 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700945 return ir_call::get_error_instruction(ctx);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700946 }
947
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700948 /* Count the number of matrix and nonmatrix parameters. This
949 * is used below to enforce some of the constructor rules.
Ian Romanick699b2472010-06-25 17:36:17 -0700950 */
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700951 if (result->type->is_matrix())
952 matrix_parameters++;
953 else
954 nonmatrix_parameters++;
Ian Romanick699b2472010-06-25 17:36:17 -0700955
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700956 actual_parameters.push_tail(result);
957 components_used += result->type->components();
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700958 }
Ian Romanickabef9552010-03-23 15:08:30 -0700959
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700960 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
961 *
962 * "It is an error to construct matrices from other matrices. This
963 * is reserved for future use."
964 */
965 if ((state->language_version <= 110) && (matrix_parameters > 0)
966 && constructor_type->is_matrix()) {
967 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
968 "matrix in GLSL 1.10",
969 constructor_type->name);
970 return ir_call::get_error_instruction(ctx);
971 }
972
973 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
974 *
975 * "If a matrix argument is given to a matrix constructor, it is
976 * an error to have any other arguments."
977 */
978 if ((matrix_parameters > 0)
979 && ((matrix_parameters + nonmatrix_parameters) > 1)
980 && constructor_type->is_matrix()) {
981 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
982 "matrix must be only parameter",
983 constructor_type->name);
984 return ir_call::get_error_instruction(ctx);
985 }
986
987 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
988 *
989 * "In these cases, there must be enough components provided in the
990 * arguments to provide an initializer for every component in the
991 * constructed value."
992 */
993 if ((components_used < type_components) && (components_used != 1)) {
994 _mesa_glsl_error(& loc, state, "too few components to construct "
995 "`%s'",
996 constructor_type->name);
997 return ir_call::get_error_instruction(ctx);
998 }
999
Kenneth Graunke284d8212010-07-08 18:15:32 -07001000 /* Later, we cast each parameter to the same base type as the
1001 * constructor. Since there are no non-floating point matrices, we
1002 * need to break them up into a series of column vectors.
1003 */
1004 if (constructor_type->base_type != GLSL_TYPE_FLOAT) {
1005 foreach_list_safe(n, &actual_parameters) {
1006 ir_rvalue *matrix = (ir_rvalue *) n;
1007
1008 if (!matrix->type->is_matrix())
1009 continue;
1010
1011 /* Create a temporary containing the matrix. */
1012 ir_variable *var = new(ctx) ir_variable(matrix->type, "matrix_tmp");
1013 instructions->push_tail(var);
1014 instructions->push_tail(new(ctx) ir_assignment(new(ctx)
1015 ir_dereference_variable(var), matrix, NULL));
1016 var->constant_value = matrix->constant_expression_value();
1017
1018 /* Replace the matrix with dereferences of its columns. */
1019 for (int i = 0; i < matrix->type->matrix_columns; i++) {
1020 matrix->insert_before(new (ctx) ir_dereference_array(var,
1021 new(ctx) ir_constant(i)));
1022 }
1023 matrix->remove();
1024 }
1025 }
1026
1027 bool all_parameters_are_constant = true;
1028
1029 /* Type cast each parameter and, if possible, fold constants.*/
1030 foreach_list_safe(n, &actual_parameters) {
1031 ir_rvalue *ir = (ir_rvalue *) n;
1032
1033 const glsl_type *desired_type =
1034 glsl_type::get_instance(constructor_type->base_type,
1035 ir->type->vector_elements,
1036 ir->type->matrix_columns);
1037 ir_rvalue *result = convert_component(ir, desired_type);
1038
1039 /* Attempt to convert the parameter to a constant valued expression.
1040 * After doing so, track whether or not all the parameters to the
1041 * constructor are trivially constant valued expressions.
1042 */
1043 ir_rvalue *const constant = result->constant_expression_value();
1044
1045 if (constant != NULL)
1046 result = constant;
1047 else
1048 all_parameters_are_constant = false;
1049
1050 if (result != ir) {
1051 ir->insert_before(result);
1052 ir->remove();
1053 }
1054 }
Kenneth Graunkef58bbd12010-07-08 18:03:28 -07001055
1056 /* If all of the parameters are trivially constant, create a
1057 * constant representing the complete collection of parameters.
1058 */
1059 if (all_parameters_are_constant) {
1060 if (components_used >= type_components)
1061 return new(ctx) ir_constant(constructor_type,
1062 & actual_parameters);
1063
1064 /* The above case must handle all scalar constructors.
1065 */
1066 assert(constructor_type->is_vector()
1067 || constructor_type->is_matrix());
1068
1069 /* Constructors with exactly one component are special for
1070 * vectors and matrices. For vectors it causes all elements of
1071 * the vector to be filled with the value. For matrices it
1072 * causes the matrix to be filled with 0 and the diagonal to be
1073 * filled with the value.
1074 */
1075 ir_constant_data data;
1076 ir_constant *const initializer =
1077 (ir_constant *) actual_parameters.head;
1078 if (constructor_type->is_matrix())
1079 generate_constructor_matrix(constructor_type, initializer,
1080 &data);
1081 else
1082 generate_constructor_vector(constructor_type, initializer,
1083 &data);
1084
1085 return new(ctx) ir_constant(constructor_type, &data);
1086 } else if (constructor_type->is_scalar()) {
1087 return dereference_component((ir_rvalue *) actual_parameters.head,
1088 0);
1089 } else if (constructor_type->is_vector()) {
1090 return emit_inline_vector_constructor(constructor_type,
1091 instructions,
1092 &actual_parameters,
1093 ctx);
1094 } else {
1095 assert(constructor_type->is_matrix());
1096 return emit_inline_matrix_constructor(constructor_type,
1097 instructions,
1098 &actual_parameters,
1099 ctx);
1100 }
Ian Romanick548fa292010-03-15 13:04:13 -07001101 } else {
1102 const ast_expression *id = subexpressions[0];
Ian Romanickf4749612010-03-15 13:26:02 -07001103 YYLTYPE loc = id->get_location();
Ian Romanickc0771312010-06-09 17:23:26 -07001104 exec_list actual_parameters;
1105
1106 process_parameters(instructions, &actual_parameters, &this->expressions,
1107 state);
Ian Romanick548fa292010-03-15 13:04:13 -07001108
Ian Romanickab92d0e2010-06-09 17:26:20 -07001109 const glsl_type *const type =
1110 state->symbols->get_type(id->primary_expression.identifier);
1111
1112 if ((type != NULL) && type->is_record()) {
1113 ir_constant *constant =
1114 constant_record_constructor(type, &loc, &actual_parameters, state);
1115
1116 if (constant != NULL)
1117 return constant;
1118 }
1119
Ian Romanickf4749612010-03-15 13:26:02 -07001120 return match_function_by_name(instructions,
1121 id->primary_expression.identifier, & loc,
Ian Romanickc0771312010-06-09 17:23:26 -07001122 &actual_parameters, state);
Ian Romanick548fa292010-03-15 13:04:13 -07001123 }
1124
Carl Worthe01193a2010-06-23 18:25:04 -07001125 return ir_call::get_error_instruction(ctx);
Ian Romanick548fa292010-03-15 13:04:13 -07001126}