blob: fc53d7ac1cbc0ce0899c751e703f5b980a2c452a [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
24#include <cstdio>
Ian Romanick8bde4ce2010-03-19 11:57:24 -070025#include "glsl_symbol_table.h"
Ian Romanick548fa292010-03-15 13:04:13 -070026#include "ast.h"
27#include "glsl_types.h"
28#include "ir.h"
29
Ian Romanick68515ee2010-03-31 16:28:51 -070030static unsigned
31process_parameters(exec_list *instructions, exec_list *actual_parameters,
Ian Romanick304ea902010-05-10 11:17:53 -070032 exec_list *parameters,
Ian Romanick68515ee2010-03-31 16:28:51 -070033 struct _mesa_glsl_parse_state *state)
34{
Ian Romanick68515ee2010-03-31 16:28:51 -070035 unsigned count = 0;
36
Ian Romanick304ea902010-05-10 11:17:53 -070037 foreach_list (n, parameters) {
38 ast_node *const ast = exec_node_data(ast_node, n, link);
39 ir_rvalue *const result = ast->hir(instructions, state);
Ian Romanick68515ee2010-03-31 16:28:51 -070040
Ian Romanick3521f0b2010-05-10 10:47:14 -070041 actual_parameters->push_tail(result);
42 count++;
Ian Romanick68515ee2010-03-31 16:28:51 -070043 }
44
45 return count;
46}
47
48
49static ir_rvalue *
50process_call(exec_list *instructions, ir_function *f,
51 YYLTYPE *loc, exec_list *actual_parameters,
52 struct _mesa_glsl_parse_state *state)
53{
54 const ir_function_signature *sig =
55 f->matching_signature(actual_parameters);
56
57 /* The instructions param will be used when the FINISHMEs below are done */
58 (void) instructions;
59
60 if (sig != NULL) {
Ian Romanickc35bb002010-04-02 15:51:02 -070061 /* Verify that 'out' and 'inout' actual parameters are lvalues. This
62 * isn't done in ir_function::matching_signature because that function
63 * cannot generate the necessary diagnostics.
64 */
65 exec_list_iterator actual_iter = actual_parameters->iterator();
66 exec_list_iterator formal_iter = sig->parameters.iterator();
67
68 while (actual_iter.has_next()) {
Eric Anholtf1ddca92010-04-07 12:35:34 -070069 ir_rvalue *actual = (ir_rvalue *) actual_iter.get();
70 ir_variable *formal = (ir_variable *) formal_iter.get();
Ian Romanickc35bb002010-04-02 15:51:02 -070071
72 assert(actual != NULL);
73 assert(formal != NULL);
74
75 if ((formal->mode == ir_var_out)
76 || (formal->mode == ir_var_inout)) {
77 if (! actual->is_lvalue()) {
78 /* FINISHME: Log a better diagnostic here. There is no way
79 * FINISHME: to tell the user which parameter is invalid.
80 */
81 _mesa_glsl_error(loc, state, "`%s' parameter is not lvalue",
82 (formal->mode == ir_var_out) ? "out" : "inout");
83 }
84 }
85
86 actual_iter.next();
87 formal_iter.next();
88 }
89
Ian Romanick68515ee2010-03-31 16:28:51 -070090 /* FINISHME: The list of actual parameters needs to be modified to
91 * FINISHME: include any necessary conversions.
92 */
93 return new ir_call(sig, actual_parameters);
94 } else {
95 /* FINISHME: Log a better error message here. G++ will show the types
96 * FINISHME: of the actual parameters and the set of candidate
97 * FINISHME: functions. A different error should also be logged when
98 * FINISHME: multiple functions match.
99 */
100 _mesa_glsl_error(loc, state, "no matching function for call to `%s'",
101 f->name);
102 return ir_call::get_error_instruction();
103 }
104}
105
106
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700107static ir_rvalue *
Ian Romanickf4749612010-03-15 13:26:02 -0700108match_function_by_name(exec_list *instructions, const char *name,
Ian Romanick304ea902010-05-10 11:17:53 -0700109 YYLTYPE *loc, exec_list *parameters,
Ian Romanickf4749612010-03-15 13:26:02 -0700110 struct _mesa_glsl_parse_state *state)
111{
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700112 ir_function *f = state->symbols->get_function(name);
Ian Romanickf4749612010-03-15 13:26:02 -0700113
114 if (f == NULL) {
115 _mesa_glsl_error(loc, state, "function `%s' undeclared", name);
116 return ir_call::get_error_instruction();
117 }
118
119 /* Once we've determined that the function being called might exist,
120 * process the parameters.
121 */
122 exec_list actual_parameters;
Ian Romanick68515ee2010-03-31 16:28:51 -0700123 process_parameters(instructions, &actual_parameters, parameters, state);
Ian Romanickf4749612010-03-15 13:26:02 -0700124
125 /* After processing the function's actual parameters, try to find an
126 * overload of the function that matches.
127 */
Ian Romanick68515ee2010-03-31 16:28:51 -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{
138 const unsigned a = desired_type->base_type;
139 const unsigned b = src->type->base_type;
140
141 if (src->type->is_error())
142 return src;
143
144 assert(a <= GLSL_TYPE_BOOL);
145 assert(b <= GLSL_TYPE_BOOL);
146
147 if ((a == b) || (src->type->is_integer() && desired_type->is_integer()))
148 return src;
149
150 switch (a) {
151 case GLSL_TYPE_UINT:
152 case GLSL_TYPE_INT:
153 if (b == GLSL_TYPE_FLOAT)
154 return new ir_expression(ir_unop_f2i, desired_type, src, NULL);
155 else {
156 assert(b == GLSL_TYPE_BOOL);
Ian Romanick565185c2010-06-11 13:49:00 -0700157 return new ir_expression(ir_unop_b2i, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700158 }
Ian Romanick565185c2010-06-11 13:49:00 -0700159 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700160 case GLSL_TYPE_FLOAT:
161 switch (b) {
162 case GLSL_TYPE_UINT:
163 return new ir_expression(ir_unop_u2f, desired_type, src, NULL);
164 case GLSL_TYPE_INT:
165 return new ir_expression(ir_unop_i2f, desired_type, src, NULL);
166 case GLSL_TYPE_BOOL:
Eric Anholtdc58b3f2010-04-02 02:13:43 -1000167 return new ir_expression(ir_unop_b2f, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700168 }
169 break;
170 case GLSL_TYPE_BOOL: {
171 int z = 0;
172 ir_constant *const zero = new ir_constant(src->type, &z);
173
174 return new ir_expression(ir_binop_nequal, desired_type, src, zero);
175 }
176 }
177
178 assert(!"Should not get here.");
179 return NULL;
180}
181
182
183/**
184 * Dereference a specific component from a scalar, vector, or matrix
185 */
186static ir_rvalue *
187dereference_component(ir_rvalue *src, unsigned component)
188{
189 assert(component < src->type->components());
190
Ian Romanickc9cb1032010-06-04 16:20:35 -0700191 /* If the source is a constant, just create a new constant instead of a
192 * dereference of the existing constant.
193 */
194 ir_constant *constant = src->as_constant();
195 if (constant)
196 return new ir_constant(constant, component);
197
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700198 if (src->type->is_scalar()) {
199 return src;
200 } else if (src->type->is_vector()) {
201 return new ir_swizzle(src, component, 0, 0, 0, 1);
202 } else {
203 assert(src->type->is_matrix());
204
205 /* Dereference a row of the matrix, then call this function again to get
206 * a specific element from that row.
207 */
208 const int c = component / src->type->column_type()->vector_elements;
209 const int r = component % src->type->column_type()->vector_elements;
210 ir_constant *const col_index = new ir_constant(glsl_type::int_type, &c);
Ian Romanick70fe8b62010-05-19 11:37:35 +0200211 ir_dereference *const col = new ir_dereference_array(src, col_index);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700212
213 col->type = src->type->column_type();
214
215 return dereference_component(col, r);
216 }
217
218 assert(!"Should not get here.");
219 return NULL;
220}
221
222
Ian Romanick00aa1732010-03-31 16:48:48 -0700223static ir_rvalue *
224process_array_constructor(exec_list *instructions,
225 const glsl_type *constructor_type,
Ian Romanick304ea902010-05-10 11:17:53 -0700226 YYLTYPE *loc, exec_list *parameters,
Ian Romanick00aa1732010-03-31 16:48:48 -0700227 struct _mesa_glsl_parse_state *state)
228{
229 /* Array constructors come in two forms: sized and unsized. Sized array
230 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
231 * variables. In this case the number of parameters must exactly match the
232 * specified size of the array.
233 *
234 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
235 * are vec4 variables. In this case the size of the array being constructed
236 * is determined by the number of parameters.
237 *
238 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
239 *
240 * "There must be exactly the same number of arguments as the size of
241 * the array being constructed. If no size is present in the
242 * constructor, then the array is explicitly sized to the number of
243 * arguments provided. The arguments are assigned in order, starting at
244 * element 0, to the elements of the constructed array. Each argument
245 * must be the same type as the element type of the array, or be a type
246 * that can be converted to the element type of the array according to
247 * Section 4.1.10 "Implicit Conversions.""
248 */
249 exec_list actual_parameters;
250 const unsigned parameter_count =
251 process_parameters(instructions, &actual_parameters, parameters, state);
252
253 if ((parameter_count == 0)
254 || ((constructor_type->length != 0)
255 && (constructor_type->length != parameter_count))) {
256 const unsigned min_param = (constructor_type->length == 0)
257 ? 1 : constructor_type->length;
258
259 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
260 "parameter%s",
261 (constructor_type->length != 0) ? "at least" : "exactly",
262 min_param, (min_param <= 1) ? "" : "s");
263 return ir_call::get_error_instruction();
264 }
265
266 if (constructor_type->length == 0) {
267 constructor_type =
Ian Romanickcb9cba22010-04-02 16:08:44 -0700268 glsl_type::get_array_instance(constructor_type->element_type(),
Ian Romanick00aa1732010-03-31 16:48:48 -0700269 parameter_count);
270 assert(constructor_type != NULL);
271 assert(constructor_type->length == parameter_count);
272 }
273
274 ir_function *f = state->symbols->get_function(constructor_type->name);
275
276 /* If the constructor for this type of array does not exist, generate the
Ian Romanick82baaf42010-04-23 13:21:22 -0700277 * prototype and add it to the symbol table.
Ian Romanick00aa1732010-03-31 16:48:48 -0700278 */
279 if (f == NULL) {
Ian Romanick82baaf42010-04-23 13:21:22 -0700280 f = constructor_type->generate_constructor(state->symbols);
Ian Romanick00aa1732010-03-31 16:48:48 -0700281 }
282
283 ir_rvalue *const r =
284 process_call(instructions, f, loc, &actual_parameters, state);
285
286 assert(r != NULL);
287 assert(r->type->is_error() || (r->type == constructor_type));
288
289 return r;
290}
291
292
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700293ir_rvalue *
Ian Romanick548fa292010-03-15 13:04:13 -0700294ast_function_expression::hir(exec_list *instructions,
295 struct _mesa_glsl_parse_state *state)
296{
297 /* There are three sorts of function calls.
298 *
299 * 1. contstructors - The first subexpression is an ast_type_specifier.
300 * 2. methods - Only the .length() method of array types.
301 * 3. functions - Calls to regular old functions.
302 *
Ian Romanick548fa292010-03-15 13:04:13 -0700303 * Method calls are actually detected when the ast_field_selection
304 * expression is handled.
305 */
306 if (is_constructor()) {
Ian Romanickabef9552010-03-23 15:08:30 -0700307 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
308 YYLTYPE loc = type->get_location();
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700309 const char *name;
Ian Romanickabef9552010-03-23 15:08:30 -0700310
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700311 const glsl_type *const constructor_type = type->glsl_type(& name, state);
Ian Romanickabef9552010-03-23 15:08:30 -0700312
313
314 /* Constructors for samplers are illegal.
315 */
316 if (constructor_type->is_sampler()) {
317 _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
318 constructor_type->name);
319 return ir_call::get_error_instruction();
320 }
321
Ian Romanickb6326ab2010-03-31 16:25:21 -0700322 if (constructor_type->is_array()) {
323 if (state->language_version <= 110) {
324 _mesa_glsl_error(& loc, state,
325 "array constructors forbidden in GLSL 1.10");
326 return ir_call::get_error_instruction();
327 }
328
Ian Romanick00aa1732010-03-31 16:48:48 -0700329 return process_array_constructor(instructions, constructor_type,
Ian Romanick3521f0b2010-05-10 10:47:14 -0700330 & loc, &this->expressions, state);
Ian Romanickb6326ab2010-03-31 16:25:21 -0700331 }
Ian Romanickabef9552010-03-23 15:08:30 -0700332
333 /* There are two kinds of constructor call. Constructors for built-in
334 * language types, such as mat4 and vec2, are free form. The only
335 * requirement is that the parameters must provide enough values of the
336 * correct scalar type. Constructors for arrays and structures must
337 * have the exact number of parameters with matching types in the
338 * correct order. These constructors follow essentially the same type
339 * matching rules as functions.
340 */
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700341 if (constructor_type->is_numeric() || constructor_type->is_boolean()) {
342 /* Constructing a numeric type has a couple steps. First all values
343 * passed to the constructor are broken into individual parameters
344 * and type converted to the base type of the thing being constructed.
345 *
346 * At that point we have some number of values that match the base
347 * type of the thing being constructed. Now the constructor can be
348 * treated like a function call. Each numeric type has a small set
349 * of constructor functions. The set of new parameters will either
350 * match one of those functions or the original constructor is
351 * invalid.
352 */
353 const glsl_type *const base_type = constructor_type->get_base_type();
354
355 /* Total number of components of the type being constructed.
356 */
357 const unsigned type_components = constructor_type->components();
358
359 /* Number of components from parameters that have actually been
360 * consumed. This is used to perform several kinds of error checking.
361 */
362 unsigned components_used = 0;
363
364 unsigned matrix_parameters = 0;
365 unsigned nonmatrix_parameters = 0;
366 exec_list actual_parameters;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700367
Ian Romanick304ea902010-05-10 11:17:53 -0700368 assert(!this->expressions.is_empty());
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700369
Ian Romanick304ea902010-05-10 11:17:53 -0700370 foreach_list (n, &this->expressions) {
371 ast_node *ast = exec_node_data(ast_node, n, link);
Ian Romanick3521f0b2010-05-10 10:47:14 -0700372 ir_rvalue *const result =
Ian Romanick304ea902010-05-10 11:17:53 -0700373 ast->hir(instructions, state)->as_rvalue();
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700374
Ian Romanick3521f0b2010-05-10 10:47:14 -0700375 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
376 *
377 * "It is an error to provide extra arguments beyond this
378 * last used argument."
379 */
380 if (components_used >= type_components) {
381 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
382 "constructor",
383 constructor_type->name);
384 return ir_call::get_error_instruction();
385 }
386
387 if (!result->type->is_numeric() && !result->type->is_boolean()) {
388 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
389 "non-numeric data type",
390 constructor_type->name);
391 return ir_call::get_error_instruction();
392 }
393
394 /* Count the number of matrix and nonmatrix parameters. This
395 * is used below to enforce some of the constructor rules.
396 */
397 if (result->type->is_matrix())
398 matrix_parameters++;
399 else
400 nonmatrix_parameters++;
401
402
403 /* Process each of the components of the parameter. Dereference
404 * each component individually, perform any type conversions, and
405 * add it to the parameter list for the constructor.
406 */
407 for (unsigned i = 0; i < result->type->components(); i++) {
408 if (components_used >= type_components)
409 break;
410
411 ir_rvalue *const component =
412 convert_component(dereference_component(result, i),
413 base_type);
414
415 /* All cases that could result in component->type being the
416 * error type should have already been caught above.
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700417 */
Ian Romanick3521f0b2010-05-10 10:47:14 -0700418 assert(component->type == base_type);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700419
Ian Romanick3521f0b2010-05-10 10:47:14 -0700420 /* Don't actually generate constructor calls for scalars.
421 * Instead, do the usual component selection and conversion,
422 * and return the single component.
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700423 */
Ian Romanick3521f0b2010-05-10 10:47:14 -0700424 if (constructor_type->is_scalar())
425 return component;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700426
Ian Romanick3521f0b2010-05-10 10:47:14 -0700427 actual_parameters.push_tail(component);
428 components_used++;
429 }
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700430 }
431
432 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
433 *
434 * "It is an error to construct matrices from other matrices. This
435 * is reserved for future use."
436 */
437 if ((state->language_version <= 110) && (matrix_parameters > 0)
438 && constructor_type->is_matrix()) {
439 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
440 "matrix in GLSL 1.10",
441 constructor_type->name);
442 return ir_call::get_error_instruction();
443 }
444
445 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
446 *
447 * "If a matrix argument is given to a matrix constructor, it is
448 * an error to have any other arguments."
449 */
450 if ((matrix_parameters > 0)
451 && ((matrix_parameters + nonmatrix_parameters) > 1)
452 && constructor_type->is_matrix()) {
453 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
454 "matrix must be only parameter",
455 constructor_type->name);
456 return ir_call::get_error_instruction();
457 }
458
459 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
460 *
461 * "In these cases, there must be enough components provided in the
462 * arguments to provide an initializer for every component in the
463 * constructed value."
464 */
Ian Romanick8a24cd52010-03-29 15:36:02 -0700465 if ((components_used < type_components) && (components_used != 1)) {
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700466 _mesa_glsl_error(& loc, state, "too few components to construct "
467 "`%s'",
468 constructor_type->name);
469 return ir_call::get_error_instruction();
470 }
471
472 ir_function *f = state->symbols->get_function(constructor_type->name);
473 if (f == NULL) {
474 _mesa_glsl_error(& loc, state, "no constructor for type `%s'",
475 constructor_type->name);
476 return ir_call::get_error_instruction();
477 }
478
479 const ir_function_signature *sig =
480 f->matching_signature(& actual_parameters);
481 if (sig != NULL) {
482 return new ir_call(sig, & actual_parameters);
483 } else {
484 /* FINISHME: Log a better error message here. G++ will show the
485 * FINSIHME: types of the actual parameters and the set of
486 * FINSIHME: candidate functions. A different error should also be
487 * FINSIHME: logged when multiple functions match.
488 */
489 _mesa_glsl_error(& loc, state, "no matching constructor for `%s'",
490 constructor_type->name);
491 return ir_call::get_error_instruction();
492 }
493 }
Ian Romanickabef9552010-03-23 15:08:30 -0700494
Ian Romanick548fa292010-03-15 13:04:13 -0700495 return ir_call::get_error_instruction();
496 } else {
497 const ast_expression *id = subexpressions[0];
Ian Romanickf4749612010-03-15 13:26:02 -0700498 YYLTYPE loc = id->get_location();
Ian Romanick548fa292010-03-15 13:04:13 -0700499
Ian Romanickf4749612010-03-15 13:26:02 -0700500 return match_function_by_name(instructions,
501 id->primary_expression.identifier, & loc,
Ian Romanick3521f0b2010-05-10 10:47:14 -0700502 &this->expressions, state);
Ian Romanick548fa292010-03-15 13:04:13 -0700503 }
504
505 return ir_call::get_error_instruction();
506}