blob: 7931633c5a1f3c0a3fb45e9b8161c62b46394ca9 [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);
Eric Anholtc2cb84e2010-04-02 02:17:08 -1000157 return new ir_expression(ir_unop_f2b, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700158 }
159 case GLSL_TYPE_FLOAT:
160 switch (b) {
161 case GLSL_TYPE_UINT:
162 return new ir_expression(ir_unop_u2f, desired_type, src, NULL);
163 case GLSL_TYPE_INT:
164 return new ir_expression(ir_unop_i2f, desired_type, src, NULL);
165 case GLSL_TYPE_BOOL:
Eric Anholtdc58b3f2010-04-02 02:13:43 -1000166 return new ir_expression(ir_unop_b2f, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700167 }
168 break;
169 case GLSL_TYPE_BOOL: {
170 int z = 0;
171 ir_constant *const zero = new ir_constant(src->type, &z);
172
173 return new ir_expression(ir_binop_nequal, desired_type, src, zero);
174 }
175 }
176
177 assert(!"Should not get here.");
178 return NULL;
179}
180
181
182/**
183 * Dereference a specific component from a scalar, vector, or matrix
184 */
185static ir_rvalue *
186dereference_component(ir_rvalue *src, unsigned component)
187{
188 assert(component < src->type->components());
189
190 if (src->type->is_scalar()) {
191 return src;
192 } else if (src->type->is_vector()) {
193 return new ir_swizzle(src, component, 0, 0, 0, 1);
194 } else {
195 assert(src->type->is_matrix());
196
197 /* Dereference a row of the matrix, then call this function again to get
198 * a specific element from that row.
199 */
200 const int c = component / src->type->column_type()->vector_elements;
201 const int r = component % src->type->column_type()->vector_elements;
202 ir_constant *const col_index = new ir_constant(glsl_type::int_type, &c);
203 ir_dereference *const col = new ir_dereference(src, col_index);
204
205 col->type = src->type->column_type();
206
207 return dereference_component(col, r);
208 }
209
210 assert(!"Should not get here.");
211 return NULL;
212}
213
214
Ian Romanick00aa1732010-03-31 16:48:48 -0700215static ir_rvalue *
216process_array_constructor(exec_list *instructions,
217 const glsl_type *constructor_type,
Ian Romanick304ea902010-05-10 11:17:53 -0700218 YYLTYPE *loc, exec_list *parameters,
Ian Romanick00aa1732010-03-31 16:48:48 -0700219 struct _mesa_glsl_parse_state *state)
220{
221 /* Array constructors come in two forms: sized and unsized. Sized array
222 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
223 * variables. In this case the number of parameters must exactly match the
224 * specified size of the array.
225 *
226 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
227 * are vec4 variables. In this case the size of the array being constructed
228 * is determined by the number of parameters.
229 *
230 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
231 *
232 * "There must be exactly the same number of arguments as the size of
233 * the array being constructed. If no size is present in the
234 * constructor, then the array is explicitly sized to the number of
235 * arguments provided. The arguments are assigned in order, starting at
236 * element 0, to the elements of the constructed array. Each argument
237 * must be the same type as the element type of the array, or be a type
238 * that can be converted to the element type of the array according to
239 * Section 4.1.10 "Implicit Conversions.""
240 */
241 exec_list actual_parameters;
242 const unsigned parameter_count =
243 process_parameters(instructions, &actual_parameters, parameters, state);
244
245 if ((parameter_count == 0)
246 || ((constructor_type->length != 0)
247 && (constructor_type->length != parameter_count))) {
248 const unsigned min_param = (constructor_type->length == 0)
249 ? 1 : constructor_type->length;
250
251 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
252 "parameter%s",
253 (constructor_type->length != 0) ? "at least" : "exactly",
254 min_param, (min_param <= 1) ? "" : "s");
255 return ir_call::get_error_instruction();
256 }
257
258 if (constructor_type->length == 0) {
259 constructor_type =
Ian Romanickcb9cba22010-04-02 16:08:44 -0700260 glsl_type::get_array_instance(constructor_type->element_type(),
Ian Romanick00aa1732010-03-31 16:48:48 -0700261 parameter_count);
262 assert(constructor_type != NULL);
263 assert(constructor_type->length == parameter_count);
264 }
265
266 ir_function *f = state->symbols->get_function(constructor_type->name);
267
268 /* If the constructor for this type of array does not exist, generate the
Ian Romanick82baaf42010-04-23 13:21:22 -0700269 * prototype and add it to the symbol table.
Ian Romanick00aa1732010-03-31 16:48:48 -0700270 */
271 if (f == NULL) {
Ian Romanick82baaf42010-04-23 13:21:22 -0700272 f = constructor_type->generate_constructor(state->symbols);
Ian Romanick00aa1732010-03-31 16:48:48 -0700273 }
274
275 ir_rvalue *const r =
276 process_call(instructions, f, loc, &actual_parameters, state);
277
278 assert(r != NULL);
279 assert(r->type->is_error() || (r->type == constructor_type));
280
281 return r;
282}
283
284
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700285ir_rvalue *
Ian Romanick548fa292010-03-15 13:04:13 -0700286ast_function_expression::hir(exec_list *instructions,
287 struct _mesa_glsl_parse_state *state)
288{
289 /* There are three sorts of function calls.
290 *
291 * 1. contstructors - The first subexpression is an ast_type_specifier.
292 * 2. methods - Only the .length() method of array types.
293 * 3. functions - Calls to regular old functions.
294 *
Ian Romanick548fa292010-03-15 13:04:13 -0700295 * Method calls are actually detected when the ast_field_selection
296 * expression is handled.
297 */
298 if (is_constructor()) {
Ian Romanickabef9552010-03-23 15:08:30 -0700299 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
300 YYLTYPE loc = type->get_location();
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700301 const char *name;
Ian Romanickabef9552010-03-23 15:08:30 -0700302
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700303 const glsl_type *const constructor_type = type->glsl_type(& name, state);
Ian Romanickabef9552010-03-23 15:08:30 -0700304
305
306 /* Constructors for samplers are illegal.
307 */
308 if (constructor_type->is_sampler()) {
309 _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
310 constructor_type->name);
311 return ir_call::get_error_instruction();
312 }
313
Ian Romanickb6326ab2010-03-31 16:25:21 -0700314 if (constructor_type->is_array()) {
315 if (state->language_version <= 110) {
316 _mesa_glsl_error(& loc, state,
317 "array constructors forbidden in GLSL 1.10");
318 return ir_call::get_error_instruction();
319 }
320
Ian Romanick00aa1732010-03-31 16:48:48 -0700321 return process_array_constructor(instructions, constructor_type,
Ian Romanick3521f0b2010-05-10 10:47:14 -0700322 & loc, &this->expressions, state);
Ian Romanickb6326ab2010-03-31 16:25:21 -0700323 }
Ian Romanickabef9552010-03-23 15:08:30 -0700324
325 /* There are two kinds of constructor call. Constructors for built-in
326 * language types, such as mat4 and vec2, are free form. The only
327 * requirement is that the parameters must provide enough values of the
328 * correct scalar type. Constructors for arrays and structures must
329 * have the exact number of parameters with matching types in the
330 * correct order. These constructors follow essentially the same type
331 * matching rules as functions.
332 */
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700333 if (constructor_type->is_numeric() || constructor_type->is_boolean()) {
334 /* Constructing a numeric type has a couple steps. First all values
335 * passed to the constructor are broken into individual parameters
336 * and type converted to the base type of the thing being constructed.
337 *
338 * At that point we have some number of values that match the base
339 * type of the thing being constructed. Now the constructor can be
340 * treated like a function call. Each numeric type has a small set
341 * of constructor functions. The set of new parameters will either
342 * match one of those functions or the original constructor is
343 * invalid.
344 */
345 const glsl_type *const base_type = constructor_type->get_base_type();
346
347 /* Total number of components of the type being constructed.
348 */
349 const unsigned type_components = constructor_type->components();
350
351 /* Number of components from parameters that have actually been
352 * consumed. This is used to perform several kinds of error checking.
353 */
354 unsigned components_used = 0;
355
356 unsigned matrix_parameters = 0;
357 unsigned nonmatrix_parameters = 0;
358 exec_list actual_parameters;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700359
Ian Romanick304ea902010-05-10 11:17:53 -0700360 assert(!this->expressions.is_empty());
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700361
Ian Romanick304ea902010-05-10 11:17:53 -0700362 foreach_list (n, &this->expressions) {
363 ast_node *ast = exec_node_data(ast_node, n, link);
Ian Romanick3521f0b2010-05-10 10:47:14 -0700364 ir_rvalue *const result =
Ian Romanick304ea902010-05-10 11:17:53 -0700365 ast->hir(instructions, state)->as_rvalue();
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700366
Ian Romanick3521f0b2010-05-10 10:47:14 -0700367 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
368 *
369 * "It is an error to provide extra arguments beyond this
370 * last used argument."
371 */
372 if (components_used >= type_components) {
373 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
374 "constructor",
375 constructor_type->name);
376 return ir_call::get_error_instruction();
377 }
378
379 if (!result->type->is_numeric() && !result->type->is_boolean()) {
380 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
381 "non-numeric data type",
382 constructor_type->name);
383 return ir_call::get_error_instruction();
384 }
385
386 /* Count the number of matrix and nonmatrix parameters. This
387 * is used below to enforce some of the constructor rules.
388 */
389 if (result->type->is_matrix())
390 matrix_parameters++;
391 else
392 nonmatrix_parameters++;
393
394
395 /* Process each of the components of the parameter. Dereference
396 * each component individually, perform any type conversions, and
397 * add it to the parameter list for the constructor.
398 */
399 for (unsigned i = 0; i < result->type->components(); i++) {
400 if (components_used >= type_components)
401 break;
402
403 ir_rvalue *const component =
404 convert_component(dereference_component(result, i),
405 base_type);
406
407 /* All cases that could result in component->type being the
408 * error type should have already been caught above.
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700409 */
Ian Romanick3521f0b2010-05-10 10:47:14 -0700410 assert(component->type == base_type);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700411
Ian Romanick3521f0b2010-05-10 10:47:14 -0700412 /* Don't actually generate constructor calls for scalars.
413 * Instead, do the usual component selection and conversion,
414 * and return the single component.
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700415 */
Ian Romanick3521f0b2010-05-10 10:47:14 -0700416 if (constructor_type->is_scalar())
417 return component;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700418
Ian Romanick3521f0b2010-05-10 10:47:14 -0700419 actual_parameters.push_tail(component);
420 components_used++;
421 }
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700422 }
423
424 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
425 *
426 * "It is an error to construct matrices from other matrices. This
427 * is reserved for future use."
428 */
429 if ((state->language_version <= 110) && (matrix_parameters > 0)
430 && constructor_type->is_matrix()) {
431 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
432 "matrix in GLSL 1.10",
433 constructor_type->name);
434 return ir_call::get_error_instruction();
435 }
436
437 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
438 *
439 * "If a matrix argument is given to a matrix constructor, it is
440 * an error to have any other arguments."
441 */
442 if ((matrix_parameters > 0)
443 && ((matrix_parameters + nonmatrix_parameters) > 1)
444 && constructor_type->is_matrix()) {
445 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
446 "matrix must be only parameter",
447 constructor_type->name);
448 return ir_call::get_error_instruction();
449 }
450
451 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
452 *
453 * "In these cases, there must be enough components provided in the
454 * arguments to provide an initializer for every component in the
455 * constructed value."
456 */
Ian Romanick8a24cd52010-03-29 15:36:02 -0700457 if ((components_used < type_components) && (components_used != 1)) {
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700458 _mesa_glsl_error(& loc, state, "too few components to construct "
459 "`%s'",
460 constructor_type->name);
461 return ir_call::get_error_instruction();
462 }
463
464 ir_function *f = state->symbols->get_function(constructor_type->name);
465 if (f == NULL) {
466 _mesa_glsl_error(& loc, state, "no constructor for type `%s'",
467 constructor_type->name);
468 return ir_call::get_error_instruction();
469 }
470
471 const ir_function_signature *sig =
472 f->matching_signature(& actual_parameters);
473 if (sig != NULL) {
474 return new ir_call(sig, & actual_parameters);
475 } else {
476 /* FINISHME: Log a better error message here. G++ will show the
477 * FINSIHME: types of the actual parameters and the set of
478 * FINSIHME: candidate functions. A different error should also be
479 * FINSIHME: logged when multiple functions match.
480 */
481 _mesa_glsl_error(& loc, state, "no matching constructor for `%s'",
482 constructor_type->name);
483 return ir_call::get_error_instruction();
484 }
485 }
Ian Romanickabef9552010-03-23 15:08:30 -0700486
Ian Romanick548fa292010-03-15 13:04:13 -0700487 return ir_call::get_error_instruction();
488 } else {
489 const ast_expression *id = subexpressions[0];
Ian Romanickf4749612010-03-15 13:26:02 -0700490 YYLTYPE loc = id->get_location();
Ian Romanick548fa292010-03-15 13:04:13 -0700491
Ian Romanickf4749612010-03-15 13:26:02 -0700492 return match_function_by_name(instructions,
493 id->primary_expression.identifier, & loc,
Ian Romanick3521f0b2010-05-10 10:47:14 -0700494 &this->expressions, state);
Ian Romanick548fa292010-03-15 13:04:13 -0700495 }
496
497 return ir_call::get_error_instruction();
498}