blob: cd57c32040bf1f96a9fb071f17d4454e32ef36db [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{
35 simple_node *const first = parameters;
36 unsigned count = 0;
37
38 if (first != NULL) {
39 simple_node *ptr = first;
40 do {
41 ir_instruction *const result =
42 ((ast_node *) ptr)->hir(instructions, state);
43 ptr = ptr->next;
44
45 actual_parameters->push_tail(result);
46 count++;
47 } while (ptr != first);
48 }
49
50 return count;
51}
52
53
54static ir_rvalue *
55process_call(exec_list *instructions, ir_function *f,
56 YYLTYPE *loc, exec_list *actual_parameters,
57 struct _mesa_glsl_parse_state *state)
58{
59 const ir_function_signature *sig =
60 f->matching_signature(actual_parameters);
61
62 /* The instructions param will be used when the FINISHMEs below are done */
63 (void) instructions;
64
65 if (sig != NULL) {
Ian Romanickc35bb002010-04-02 15:51:02 -070066 /* Verify that 'out' and 'inout' actual parameters are lvalues. This
67 * isn't done in ir_function::matching_signature because that function
68 * cannot generate the necessary diagnostics.
69 */
70 exec_list_iterator actual_iter = actual_parameters->iterator();
71 exec_list_iterator formal_iter = sig->parameters.iterator();
72
73 while (actual_iter.has_next()) {
74 ir_rvalue *actual =
75 ((ir_instruction *) actual_iter.get())->as_rvalue();
76 ir_variable *formal =
77 ((ir_instruction *) formal_iter.get())->as_variable();
78
79 assert(actual != NULL);
80 assert(formal != NULL);
81
82 if ((formal->mode == ir_var_out)
83 || (formal->mode == ir_var_inout)) {
84 if (! actual->is_lvalue()) {
85 /* FINISHME: Log a better diagnostic here. There is no way
86 * FINISHME: to tell the user which parameter is invalid.
87 */
88 _mesa_glsl_error(loc, state, "`%s' parameter is not lvalue",
89 (formal->mode == ir_var_out) ? "out" : "inout");
90 }
91 }
92
93 actual_iter.next();
94 formal_iter.next();
95 }
96
Ian Romanick68515ee2010-03-31 16:28:51 -070097 /* FINISHME: The list of actual parameters needs to be modified to
98 * FINISHME: include any necessary conversions.
99 */
100 return new ir_call(sig, actual_parameters);
101 } else {
102 /* FINISHME: Log a better error message here. G++ will show the types
103 * FINISHME: of the actual parameters and the set of candidate
104 * FINISHME: functions. A different error should also be logged when
105 * FINISHME: multiple functions match.
106 */
107 _mesa_glsl_error(loc, state, "no matching function for call to `%s'",
108 f->name);
109 return ir_call::get_error_instruction();
110 }
111}
112
113
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700114static ir_rvalue *
Ian Romanickf4749612010-03-15 13:26:02 -0700115match_function_by_name(exec_list *instructions, const char *name,
116 YYLTYPE *loc, simple_node *parameters,
117 struct _mesa_glsl_parse_state *state)
118{
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700119 ir_function *f = state->symbols->get_function(name);
Ian Romanickf4749612010-03-15 13:26:02 -0700120
121 if (f == NULL) {
122 _mesa_glsl_error(loc, state, "function `%s' undeclared", name);
123 return ir_call::get_error_instruction();
124 }
125
126 /* Once we've determined that the function being called might exist,
127 * process the parameters.
128 */
129 exec_list actual_parameters;
Ian Romanick68515ee2010-03-31 16:28:51 -0700130 process_parameters(instructions, &actual_parameters, parameters, state);
Ian Romanickf4749612010-03-15 13:26:02 -0700131
132 /* After processing the function's actual parameters, try to find an
133 * overload of the function that matches.
134 */
Ian Romanick68515ee2010-03-31 16:28:51 -0700135 return process_call(instructions, f, loc, &actual_parameters, state);
Ian Romanickf4749612010-03-15 13:26:02 -0700136}
137
138
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700139/**
140 * Perform automatic type conversion of constructor parameters
141 */
142static ir_rvalue *
143convert_component(ir_rvalue *src, const glsl_type *desired_type)
144{
145 const unsigned a = desired_type->base_type;
146 const unsigned b = src->type->base_type;
147
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)
161 return new ir_expression(ir_unop_f2i, desired_type, src, NULL);
162 else {
163 assert(b == GLSL_TYPE_BOOL);
Eric Anholtc2cb84e2010-04-02 02:17:08 -1000164 return new ir_expression(ir_unop_f2b, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700165 }
166 case GLSL_TYPE_FLOAT:
167 switch (b) {
168 case GLSL_TYPE_UINT:
169 return new ir_expression(ir_unop_u2f, desired_type, src, NULL);
170 case GLSL_TYPE_INT:
171 return new ir_expression(ir_unop_i2f, desired_type, src, NULL);
172 case GLSL_TYPE_BOOL:
Eric Anholtdc58b3f2010-04-02 02:13:43 -1000173 return new ir_expression(ir_unop_b2f, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700174 }
175 break;
176 case GLSL_TYPE_BOOL: {
177 int z = 0;
178 ir_constant *const zero = new ir_constant(src->type, &z);
179
180 return new ir_expression(ir_binop_nequal, desired_type, src, zero);
181 }
182 }
183
184 assert(!"Should not get here.");
185 return NULL;
186}
187
188
189/**
190 * Dereference a specific component from a scalar, vector, or matrix
191 */
192static ir_rvalue *
193dereference_component(ir_rvalue *src, unsigned component)
194{
195 assert(component < src->type->components());
196
197 if (src->type->is_scalar()) {
198 return src;
199 } else if (src->type->is_vector()) {
200 return new ir_swizzle(src, component, 0, 0, 0, 1);
201 } else {
202 assert(src->type->is_matrix());
203
204 /* Dereference a row of the matrix, then call this function again to get
205 * a specific element from that row.
206 */
207 const int c = component / src->type->column_type()->vector_elements;
208 const int r = component % src->type->column_type()->vector_elements;
209 ir_constant *const col_index = new ir_constant(glsl_type::int_type, &c);
210 ir_dereference *const col = new ir_dereference(src, col_index);
211
212 col->type = src->type->column_type();
213
214 return dereference_component(col, r);
215 }
216
217 assert(!"Should not get here.");
218 return NULL;
219}
220
221
Ian Romanick00aa1732010-03-31 16:48:48 -0700222static ir_rvalue *
223process_array_constructor(exec_list *instructions,
224 const glsl_type *constructor_type,
225 YYLTYPE *loc, simple_node *parameters,
226 struct _mesa_glsl_parse_state *state)
227{
228 /* Array constructors come in two forms: sized and unsized. Sized array
229 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
230 * variables. In this case the number of parameters must exactly match the
231 * specified size of the array.
232 *
233 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
234 * are vec4 variables. In this case the size of the array being constructed
235 * is determined by the number of parameters.
236 *
237 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
238 *
239 * "There must be exactly the same number of arguments as the size of
240 * the array being constructed. If no size is present in the
241 * constructor, then the array is explicitly sized to the number of
242 * arguments provided. The arguments are assigned in order, starting at
243 * element 0, to the elements of the constructed array. Each argument
244 * must be the same type as the element type of the array, or be a type
245 * that can be converted to the element type of the array according to
246 * Section 4.1.10 "Implicit Conversions.""
247 */
248 exec_list actual_parameters;
249 const unsigned parameter_count =
250 process_parameters(instructions, &actual_parameters, parameters, state);
251
252 if ((parameter_count == 0)
253 || ((constructor_type->length != 0)
254 && (constructor_type->length != parameter_count))) {
255 const unsigned min_param = (constructor_type->length == 0)
256 ? 1 : constructor_type->length;
257
258 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
259 "parameter%s",
260 (constructor_type->length != 0) ? "at least" : "exactly",
261 min_param, (min_param <= 1) ? "" : "s");
262 return ir_call::get_error_instruction();
263 }
264
265 if (constructor_type->length == 0) {
266 constructor_type =
Ian Romanickcb9cba22010-04-02 16:08:44 -0700267 glsl_type::get_array_instance(constructor_type->element_type(),
Ian Romanick00aa1732010-03-31 16:48:48 -0700268 parameter_count);
269 assert(constructor_type != NULL);
270 assert(constructor_type->length == parameter_count);
271 }
272
273 ir_function *f = state->symbols->get_function(constructor_type->name);
274
275 /* If the constructor for this type of array does not exist, generate the
276 * prototype and add it to the symbol table. The code will be generated
277 * later.
278 */
279 if (f == NULL) {
280 f = constructor_type->generate_constructor_prototype(state->symbols);
281 }
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,
330 & loc, subexpressions[1], 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;
367 simple_node *const first = subexpressions[1];
368
369 assert(first != NULL);
370
371 if (first != NULL) {
372 simple_node *ptr = first;
373 do {
374 ir_rvalue *const result =
375 ((ast_node *) ptr)->hir(instructions, state)->as_rvalue();
376 ptr = ptr->next;
377
378 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
379 *
380 * "It is an error to provide extra arguments beyond this
381 * last used argument."
382 */
383 if (components_used >= type_components) {
384 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
385 "constructor",
386 constructor_type->name);
387 return ir_call::get_error_instruction();
388 }
389
390 if (!result->type->is_numeric() && !result->type->is_boolean()) {
391 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
392 "non-numeric data type",
393 constructor_type->name);
394 return ir_call::get_error_instruction();
395 }
396
397 /* Count the number of matrix and nonmatrix parameters. This
398 * is used below to enforce some of the constructor rules.
399 */
400 if (result->type->is_matrix())
401 matrix_parameters++;
402 else
403 nonmatrix_parameters++;
404
405
406 /* Process each of the components of the parameter. Dereference
407 * each component individually, perform any type conversions, and
408 * add it to the parameter list for the constructor.
409 */
410 for (unsigned i = 0; i < result->type->components(); i++) {
411 if (components_used >= type_components)
412 break;
413
414 ir_rvalue *const component =
415 convert_component(dereference_component(result, i),
416 base_type);
417
418 /* All cases that could result in component->type being the
419 * error type should have already been caught above.
420 */
421 assert(component->type == base_type);
422
423 /* Don't actually generate constructor calls for scalars.
424 * Instead, do the usual component selection and conversion,
425 * and return the single component.
426 */
427 if (constructor_type->is_scalar())
428 return component;
429
430 actual_parameters.push_tail(component);
431 components_used++;
432 }
433 } while (ptr != first);
434 }
435
436 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
437 *
438 * "It is an error to construct matrices from other matrices. This
439 * is reserved for future use."
440 */
441 if ((state->language_version <= 110) && (matrix_parameters > 0)
442 && constructor_type->is_matrix()) {
443 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
444 "matrix in GLSL 1.10",
445 constructor_type->name);
446 return ir_call::get_error_instruction();
447 }
448
449 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
450 *
451 * "If a matrix argument is given to a matrix constructor, it is
452 * an error to have any other arguments."
453 */
454 if ((matrix_parameters > 0)
455 && ((matrix_parameters + nonmatrix_parameters) > 1)
456 && constructor_type->is_matrix()) {
457 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
458 "matrix must be only parameter",
459 constructor_type->name);
460 return ir_call::get_error_instruction();
461 }
462
463 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
464 *
465 * "In these cases, there must be enough components provided in the
466 * arguments to provide an initializer for every component in the
467 * constructed value."
468 */
Ian Romanick8a24cd52010-03-29 15:36:02 -0700469 if ((components_used < type_components) && (components_used != 1)) {
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700470 _mesa_glsl_error(& loc, state, "too few components to construct "
471 "`%s'",
472 constructor_type->name);
473 return ir_call::get_error_instruction();
474 }
475
476 ir_function *f = state->symbols->get_function(constructor_type->name);
477 if (f == NULL) {
478 _mesa_glsl_error(& loc, state, "no constructor for type `%s'",
479 constructor_type->name);
480 return ir_call::get_error_instruction();
481 }
482
483 const ir_function_signature *sig =
484 f->matching_signature(& actual_parameters);
485 if (sig != NULL) {
486 return new ir_call(sig, & actual_parameters);
487 } else {
488 /* FINISHME: Log a better error message here. G++ will show the
489 * FINSIHME: types of the actual parameters and the set of
490 * FINSIHME: candidate functions. A different error should also be
491 * FINSIHME: logged when multiple functions match.
492 */
493 _mesa_glsl_error(& loc, state, "no matching constructor for `%s'",
494 constructor_type->name);
495 return ir_call::get_error_instruction();
496 }
497 }
Ian Romanickabef9552010-03-23 15:08:30 -0700498
Ian Romanick548fa292010-03-15 13:04:13 -0700499 return ir_call::get_error_instruction();
500 } else {
501 const ast_expression *id = subexpressions[0];
Ian Romanickf4749612010-03-15 13:26:02 -0700502 YYLTYPE loc = id->get_location();
Ian Romanick548fa292010-03-15 13:04:13 -0700503
Ian Romanickf4749612010-03-15 13:26:02 -0700504 return match_function_by_name(instructions,
505 id->primary_expression.identifier, & loc,
506 subexpressions[1], state);
Ian Romanick548fa292010-03-15 13:04:13 -0700507 }
508
509 return ir_call::get_error_instruction();
510}