blob: 35ffcdbb8bc4ef0fe72d96414e315cf4d2cef724 [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,
32 simple_node *parameters,
33 struct _mesa_glsl_parse_state *state)
34{
Ian Romanick3521f0b2010-05-10 10:47:14 -070035 simple_node *ptr;
Ian Romanick68515ee2010-03-31 16:28:51 -070036 unsigned count = 0;
37
Ian Romanick3521f0b2010-05-10 10:47:14 -070038 foreach (ptr, parameters) {
39 ir_rvalue *const result =
40 ((ast_node *) ptr)->hir(instructions, state);
Ian Romanick68515ee2010-03-31 16:28:51 -070041
Ian Romanick3521f0b2010-05-10 10:47:14 -070042 actual_parameters->push_tail(result);
43 count++;
Ian Romanick68515ee2010-03-31 16:28:51 -070044 }
45
46 return count;
47}
48
49
50static ir_rvalue *
51process_call(exec_list *instructions, ir_function *f,
52 YYLTYPE *loc, exec_list *actual_parameters,
53 struct _mesa_glsl_parse_state *state)
54{
55 const ir_function_signature *sig =
56 f->matching_signature(actual_parameters);
57
58 /* The instructions param will be used when the FINISHMEs below are done */
59 (void) instructions;
60
61 if (sig != NULL) {
Ian Romanickc35bb002010-04-02 15:51:02 -070062 /* Verify that 'out' and 'inout' actual parameters are lvalues. This
63 * isn't done in ir_function::matching_signature because that function
64 * cannot generate the necessary diagnostics.
65 */
66 exec_list_iterator actual_iter = actual_parameters->iterator();
67 exec_list_iterator formal_iter = sig->parameters.iterator();
68
69 while (actual_iter.has_next()) {
Eric Anholtf1ddca92010-04-07 12:35:34 -070070 ir_rvalue *actual = (ir_rvalue *) actual_iter.get();
71 ir_variable *formal = (ir_variable *) formal_iter.get();
Ian Romanickc35bb002010-04-02 15:51:02 -070072
73 assert(actual != NULL);
74 assert(formal != NULL);
75
76 if ((formal->mode == ir_var_out)
77 || (formal->mode == ir_var_inout)) {
78 if (! actual->is_lvalue()) {
79 /* FINISHME: Log a better diagnostic here. There is no way
80 * FINISHME: to tell the user which parameter is invalid.
81 */
82 _mesa_glsl_error(loc, state, "`%s' parameter is not lvalue",
83 (formal->mode == ir_var_out) ? "out" : "inout");
84 }
85 }
86
87 actual_iter.next();
88 formal_iter.next();
89 }
90
Ian Romanick68515ee2010-03-31 16:28:51 -070091 /* FINISHME: The list of actual parameters needs to be modified to
92 * FINISHME: include any necessary conversions.
93 */
94 return new ir_call(sig, actual_parameters);
95 } else {
96 /* FINISHME: Log a better error message here. G++ will show the types
97 * FINISHME: of the actual parameters and the set of candidate
98 * FINISHME: functions. A different error should also be logged when
99 * FINISHME: multiple functions match.
100 */
101 _mesa_glsl_error(loc, state, "no matching function for call to `%s'",
102 f->name);
103 return ir_call::get_error_instruction();
104 }
105}
106
107
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700108static ir_rvalue *
Ian Romanickf4749612010-03-15 13:26:02 -0700109match_function_by_name(exec_list *instructions, const char *name,
110 YYLTYPE *loc, simple_node *parameters,
111 struct _mesa_glsl_parse_state *state)
112{
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700113 ir_function *f = state->symbols->get_function(name);
Ian Romanickf4749612010-03-15 13:26:02 -0700114
115 if (f == NULL) {
116 _mesa_glsl_error(loc, state, "function `%s' undeclared", name);
117 return ir_call::get_error_instruction();
118 }
119
120 /* Once we've determined that the function being called might exist,
121 * process the parameters.
122 */
123 exec_list actual_parameters;
Ian Romanick68515ee2010-03-31 16:28:51 -0700124 process_parameters(instructions, &actual_parameters, parameters, state);
Ian Romanickf4749612010-03-15 13:26:02 -0700125
126 /* After processing the function's actual parameters, try to find an
127 * overload of the function that matches.
128 */
Ian Romanick68515ee2010-03-31 16:28:51 -0700129 return process_call(instructions, f, loc, &actual_parameters, state);
Ian Romanickf4749612010-03-15 13:26:02 -0700130}
131
132
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700133/**
134 * Perform automatic type conversion of constructor parameters
135 */
136static ir_rvalue *
137convert_component(ir_rvalue *src, const glsl_type *desired_type)
138{
139 const unsigned a = desired_type->base_type;
140 const unsigned b = src->type->base_type;
141
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)
155 return new ir_expression(ir_unop_f2i, desired_type, src, NULL);
156 else {
157 assert(b == GLSL_TYPE_BOOL);
Eric Anholtc2cb84e2010-04-02 02:17:08 -1000158 return new ir_expression(ir_unop_f2b, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700159 }
160 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
191 if (src->type->is_scalar()) {
192 return src;
193 } else if (src->type->is_vector()) {
194 return new ir_swizzle(src, component, 0, 0, 0, 1);
195 } else {
196 assert(src->type->is_matrix());
197
198 /* Dereference a row of the matrix, then call this function again to get
199 * a specific element from that row.
200 */
201 const int c = component / src->type->column_type()->vector_elements;
202 const int r = component % src->type->column_type()->vector_elements;
203 ir_constant *const col_index = new ir_constant(glsl_type::int_type, &c);
204 ir_dereference *const col = new ir_dereference(src, col_index);
205
206 col->type = src->type->column_type();
207
208 return dereference_component(col, r);
209 }
210
211 assert(!"Should not get here.");
212 return NULL;
213}
214
215
Ian Romanick00aa1732010-03-31 16:48:48 -0700216static ir_rvalue *
217process_array_constructor(exec_list *instructions,
218 const glsl_type *constructor_type,
219 YYLTYPE *loc, simple_node *parameters,
220 struct _mesa_glsl_parse_state *state)
221{
222 /* Array constructors come in two forms: sized and unsized. Sized array
223 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
224 * variables. In this case the number of parameters must exactly match the
225 * specified size of the array.
226 *
227 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
228 * are vec4 variables. In this case the size of the array being constructed
229 * is determined by the number of parameters.
230 *
231 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
232 *
233 * "There must be exactly the same number of arguments as the size of
234 * the array being constructed. If no size is present in the
235 * constructor, then the array is explicitly sized to the number of
236 * arguments provided. The arguments are assigned in order, starting at
237 * element 0, to the elements of the constructed array. Each argument
238 * must be the same type as the element type of the array, or be a type
239 * that can be converted to the element type of the array according to
240 * Section 4.1.10 "Implicit Conversions.""
241 */
242 exec_list actual_parameters;
243 const unsigned parameter_count =
244 process_parameters(instructions, &actual_parameters, parameters, state);
245
246 if ((parameter_count == 0)
247 || ((constructor_type->length != 0)
248 && (constructor_type->length != parameter_count))) {
249 const unsigned min_param = (constructor_type->length == 0)
250 ? 1 : constructor_type->length;
251
252 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
253 "parameter%s",
254 (constructor_type->length != 0) ? "at least" : "exactly",
255 min_param, (min_param <= 1) ? "" : "s");
256 return ir_call::get_error_instruction();
257 }
258
259 if (constructor_type->length == 0) {
260 constructor_type =
Ian Romanickcb9cba22010-04-02 16:08:44 -0700261 glsl_type::get_array_instance(constructor_type->element_type(),
Ian Romanick00aa1732010-03-31 16:48:48 -0700262 parameter_count);
263 assert(constructor_type != NULL);
264 assert(constructor_type->length == parameter_count);
265 }
266
267 ir_function *f = state->symbols->get_function(constructor_type->name);
268
269 /* If the constructor for this type of array does not exist, generate the
Ian Romanick82baaf42010-04-23 13:21:22 -0700270 * prototype and add it to the symbol table.
Ian Romanick00aa1732010-03-31 16:48:48 -0700271 */
272 if (f == NULL) {
Ian Romanick82baaf42010-04-23 13:21:22 -0700273 f = constructor_type->generate_constructor(state->symbols);
Ian Romanick00aa1732010-03-31 16:48:48 -0700274 }
275
276 ir_rvalue *const r =
277 process_call(instructions, f, loc, &actual_parameters, state);
278
279 assert(r != NULL);
280 assert(r->type->is_error() || (r->type == constructor_type));
281
282 return r;
283}
284
285
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700286ir_rvalue *
Ian Romanick548fa292010-03-15 13:04:13 -0700287ast_function_expression::hir(exec_list *instructions,
288 struct _mesa_glsl_parse_state *state)
289{
290 /* There are three sorts of function calls.
291 *
292 * 1. contstructors - The first subexpression is an ast_type_specifier.
293 * 2. methods - Only the .length() method of array types.
294 * 3. functions - Calls to regular old functions.
295 *
Ian Romanick548fa292010-03-15 13:04:13 -0700296 * Method calls are actually detected when the ast_field_selection
297 * expression is handled.
298 */
299 if (is_constructor()) {
Ian Romanickabef9552010-03-23 15:08:30 -0700300 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
301 YYLTYPE loc = type->get_location();
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700302 const char *name;
Ian Romanickabef9552010-03-23 15:08:30 -0700303
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700304 const glsl_type *const constructor_type = type->glsl_type(& name, state);
Ian Romanickabef9552010-03-23 15:08:30 -0700305
306
307 /* Constructors for samplers are illegal.
308 */
309 if (constructor_type->is_sampler()) {
310 _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
311 constructor_type->name);
312 return ir_call::get_error_instruction();
313 }
314
Ian Romanickb6326ab2010-03-31 16:25:21 -0700315 if (constructor_type->is_array()) {
316 if (state->language_version <= 110) {
317 _mesa_glsl_error(& loc, state,
318 "array constructors forbidden in GLSL 1.10");
319 return ir_call::get_error_instruction();
320 }
321
Ian Romanick00aa1732010-03-31 16:48:48 -0700322 return process_array_constructor(instructions, constructor_type,
Ian Romanick3521f0b2010-05-10 10:47:14 -0700323 & loc, &this->expressions, state);
Ian Romanickb6326ab2010-03-31 16:25:21 -0700324 }
Ian Romanickabef9552010-03-23 15:08:30 -0700325
326 /* There are two kinds of constructor call. Constructors for built-in
327 * language types, such as mat4 and vec2, are free form. The only
328 * requirement is that the parameters must provide enough values of the
329 * correct scalar type. Constructors for arrays and structures must
330 * have the exact number of parameters with matching types in the
331 * correct order. These constructors follow essentially the same type
332 * matching rules as functions.
333 */
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700334 if (constructor_type->is_numeric() || constructor_type->is_boolean()) {
335 /* Constructing a numeric type has a couple steps. First all values
336 * passed to the constructor are broken into individual parameters
337 * and type converted to the base type of the thing being constructed.
338 *
339 * At that point we have some number of values that match the base
340 * type of the thing being constructed. Now the constructor can be
341 * treated like a function call. Each numeric type has a small set
342 * of constructor functions. The set of new parameters will either
343 * match one of those functions or the original constructor is
344 * invalid.
345 */
346 const glsl_type *const base_type = constructor_type->get_base_type();
347
348 /* Total number of components of the type being constructed.
349 */
350 const unsigned type_components = constructor_type->components();
351
352 /* Number of components from parameters that have actually been
353 * consumed. This is used to perform several kinds of error checking.
354 */
355 unsigned components_used = 0;
356
357 unsigned matrix_parameters = 0;
358 unsigned nonmatrix_parameters = 0;
359 exec_list actual_parameters;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700360
Ian Romanick3521f0b2010-05-10 10:47:14 -0700361 assert(!is_empty_list(&this->expressions));
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700362
Ian Romanick3521f0b2010-05-10 10:47:14 -0700363 simple_node *ptr;
364 foreach (ptr, &this->expressions) {
365 ir_rvalue *const result =
366 ((ast_node *) ptr)->hir(instructions, state)->as_rvalue();
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700367
Ian Romanick3521f0b2010-05-10 10:47:14 -0700368 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
369 *
370 * "It is an error to provide extra arguments beyond this
371 * last used argument."
372 */
373 if (components_used >= type_components) {
374 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
375 "constructor",
376 constructor_type->name);
377 return ir_call::get_error_instruction();
378 }
379
380 if (!result->type->is_numeric() && !result->type->is_boolean()) {
381 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
382 "non-numeric data type",
383 constructor_type->name);
384 return ir_call::get_error_instruction();
385 }
386
387 /* Count the number of matrix and nonmatrix parameters. This
388 * is used below to enforce some of the constructor rules.
389 */
390 if (result->type->is_matrix())
391 matrix_parameters++;
392 else
393 nonmatrix_parameters++;
394
395
396 /* Process each of the components of the parameter. Dereference
397 * each component individually, perform any type conversions, and
398 * add it to the parameter list for the constructor.
399 */
400 for (unsigned i = 0; i < result->type->components(); i++) {
401 if (components_used >= type_components)
402 break;
403
404 ir_rvalue *const component =
405 convert_component(dereference_component(result, i),
406 base_type);
407
408 /* All cases that could result in component->type being the
409 * error type should have already been caught above.
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700410 */
Ian Romanick3521f0b2010-05-10 10:47:14 -0700411 assert(component->type == base_type);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700412
Ian Romanick3521f0b2010-05-10 10:47:14 -0700413 /* Don't actually generate constructor calls for scalars.
414 * Instead, do the usual component selection and conversion,
415 * and return the single component.
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700416 */
Ian Romanick3521f0b2010-05-10 10:47:14 -0700417 if (constructor_type->is_scalar())
418 return component;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700419
Ian Romanick3521f0b2010-05-10 10:47:14 -0700420 actual_parameters.push_tail(component);
421 components_used++;
422 }
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700423 }
424
425 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
426 *
427 * "It is an error to construct matrices from other matrices. This
428 * is reserved for future use."
429 */
430 if ((state->language_version <= 110) && (matrix_parameters > 0)
431 && constructor_type->is_matrix()) {
432 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
433 "matrix in GLSL 1.10",
434 constructor_type->name);
435 return ir_call::get_error_instruction();
436 }
437
438 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
439 *
440 * "If a matrix argument is given to a matrix constructor, it is
441 * an error to have any other arguments."
442 */
443 if ((matrix_parameters > 0)
444 && ((matrix_parameters + nonmatrix_parameters) > 1)
445 && constructor_type->is_matrix()) {
446 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
447 "matrix must be only parameter",
448 constructor_type->name);
449 return ir_call::get_error_instruction();
450 }
451
452 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
453 *
454 * "In these cases, there must be enough components provided in the
455 * arguments to provide an initializer for every component in the
456 * constructed value."
457 */
Ian Romanick8a24cd52010-03-29 15:36:02 -0700458 if ((components_used < type_components) && (components_used != 1)) {
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700459 _mesa_glsl_error(& loc, state, "too few components to construct "
460 "`%s'",
461 constructor_type->name);
462 return ir_call::get_error_instruction();
463 }
464
465 ir_function *f = state->symbols->get_function(constructor_type->name);
466 if (f == NULL) {
467 _mesa_glsl_error(& loc, state, "no constructor for type `%s'",
468 constructor_type->name);
469 return ir_call::get_error_instruction();
470 }
471
472 const ir_function_signature *sig =
473 f->matching_signature(& actual_parameters);
474 if (sig != NULL) {
475 return new ir_call(sig, & actual_parameters);
476 } else {
477 /* FINISHME: Log a better error message here. G++ will show the
478 * FINSIHME: types of the actual parameters and the set of
479 * FINSIHME: candidate functions. A different error should also be
480 * FINSIHME: logged when multiple functions match.
481 */
482 _mesa_glsl_error(& loc, state, "no matching constructor for `%s'",
483 constructor_type->name);
484 return ir_call::get_error_instruction();
485 }
486 }
Ian Romanickabef9552010-03-23 15:08:30 -0700487
Ian Romanick548fa292010-03-15 13:04:13 -0700488 return ir_call::get_error_instruction();
489 } else {
490 const ast_expression *id = subexpressions[0];
Ian Romanickf4749612010-03-15 13:26:02 -0700491 YYLTYPE loc = id->get_location();
Ian Romanick548fa292010-03-15 13:04:13 -0700492
Ian Romanickf4749612010-03-15 13:26:02 -0700493 return match_function_by_name(instructions,
494 id->primary_expression.identifier, & loc,
Ian Romanick3521f0b2010-05-10 10:47:14 -0700495 &this->expressions, state);
Ian Romanick548fa292010-03-15 13:04:13 -0700496 }
497
498 return ir_call::get_error_instruction();
499}