blob: 09b7879185bfd6a461bd4cf82963dc79aff3bae4 [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) {
66 /* FINISHME: The list of actual parameters needs to be modified to
67 * FINISHME: include any necessary conversions.
68 */
69 return new ir_call(sig, actual_parameters);
70 } else {
71 /* FINISHME: Log a better error message here. G++ will show the types
72 * FINISHME: of the actual parameters and the set of candidate
73 * FINISHME: functions. A different error should also be logged when
74 * FINISHME: multiple functions match.
75 */
76 _mesa_glsl_error(loc, state, "no matching function for call to `%s'",
77 f->name);
78 return ir_call::get_error_instruction();
79 }
80}
81
82
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -070083static ir_rvalue *
Ian Romanickf4749612010-03-15 13:26:02 -070084match_function_by_name(exec_list *instructions, const char *name,
85 YYLTYPE *loc, simple_node *parameters,
86 struct _mesa_glsl_parse_state *state)
87{
Ian Romanick8bde4ce2010-03-19 11:57:24 -070088 ir_function *f = state->symbols->get_function(name);
Ian Romanickf4749612010-03-15 13:26:02 -070089
90 if (f == NULL) {
91 _mesa_glsl_error(loc, state, "function `%s' undeclared", name);
92 return ir_call::get_error_instruction();
93 }
94
95 /* Once we've determined that the function being called might exist,
96 * process the parameters.
97 */
98 exec_list actual_parameters;
Ian Romanick68515ee2010-03-31 16:28:51 -070099 process_parameters(instructions, &actual_parameters, parameters, state);
Ian Romanickf4749612010-03-15 13:26:02 -0700100
101 /* After processing the function's actual parameters, try to find an
102 * overload of the function that matches.
103 */
Ian Romanick68515ee2010-03-31 16:28:51 -0700104 return process_call(instructions, f, loc, &actual_parameters, state);
Ian Romanickf4749612010-03-15 13:26:02 -0700105}
106
107
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700108/**
109 * Perform automatic type conversion of constructor parameters
110 */
111static ir_rvalue *
112convert_component(ir_rvalue *src, const glsl_type *desired_type)
113{
114 const unsigned a = desired_type->base_type;
115 const unsigned b = src->type->base_type;
116
117 if (src->type->is_error())
118 return src;
119
120 assert(a <= GLSL_TYPE_BOOL);
121 assert(b <= GLSL_TYPE_BOOL);
122
123 if ((a == b) || (src->type->is_integer() && desired_type->is_integer()))
124 return src;
125
126 switch (a) {
127 case GLSL_TYPE_UINT:
128 case GLSL_TYPE_INT:
129 if (b == GLSL_TYPE_FLOAT)
130 return new ir_expression(ir_unop_f2i, desired_type, src, NULL);
131 else {
132 assert(b == GLSL_TYPE_BOOL);
Eric Anholtc2cb84e2010-04-02 02:17:08 -1000133 return new ir_expression(ir_unop_f2b, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700134 }
135 case GLSL_TYPE_FLOAT:
136 switch (b) {
137 case GLSL_TYPE_UINT:
138 return new ir_expression(ir_unop_u2f, desired_type, src, NULL);
139 case GLSL_TYPE_INT:
140 return new ir_expression(ir_unop_i2f, desired_type, src, NULL);
141 case GLSL_TYPE_BOOL:
Eric Anholtdc58b3f2010-04-02 02:13:43 -1000142 return new ir_expression(ir_unop_b2f, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700143 }
144 break;
145 case GLSL_TYPE_BOOL: {
146 int z = 0;
147 ir_constant *const zero = new ir_constant(src->type, &z);
148
149 return new ir_expression(ir_binop_nequal, desired_type, src, zero);
150 }
151 }
152
153 assert(!"Should not get here.");
154 return NULL;
155}
156
157
158/**
159 * Dereference a specific component from a scalar, vector, or matrix
160 */
161static ir_rvalue *
162dereference_component(ir_rvalue *src, unsigned component)
163{
164 assert(component < src->type->components());
165
166 if (src->type->is_scalar()) {
167 return src;
168 } else if (src->type->is_vector()) {
169 return new ir_swizzle(src, component, 0, 0, 0, 1);
170 } else {
171 assert(src->type->is_matrix());
172
173 /* Dereference a row of the matrix, then call this function again to get
174 * a specific element from that row.
175 */
176 const int c = component / src->type->column_type()->vector_elements;
177 const int r = component % src->type->column_type()->vector_elements;
178 ir_constant *const col_index = new ir_constant(glsl_type::int_type, &c);
179 ir_dereference *const col = new ir_dereference(src, col_index);
180
181 col->type = src->type->column_type();
182
183 return dereference_component(col, r);
184 }
185
186 assert(!"Should not get here.");
187 return NULL;
188}
189
190
Ian Romanick00aa1732010-03-31 16:48:48 -0700191static ir_rvalue *
192process_array_constructor(exec_list *instructions,
193 const glsl_type *constructor_type,
194 YYLTYPE *loc, simple_node *parameters,
195 struct _mesa_glsl_parse_state *state)
196{
197 /* Array constructors come in two forms: sized and unsized. Sized array
198 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
199 * variables. In this case the number of parameters must exactly match the
200 * specified size of the array.
201 *
202 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
203 * are vec4 variables. In this case the size of the array being constructed
204 * is determined by the number of parameters.
205 *
206 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
207 *
208 * "There must be exactly the same number of arguments as the size of
209 * the array being constructed. If no size is present in the
210 * constructor, then the array is explicitly sized to the number of
211 * arguments provided. The arguments are assigned in order, starting at
212 * element 0, to the elements of the constructed array. Each argument
213 * must be the same type as the element type of the array, or be a type
214 * that can be converted to the element type of the array according to
215 * Section 4.1.10 "Implicit Conversions.""
216 */
217 exec_list actual_parameters;
218 const unsigned parameter_count =
219 process_parameters(instructions, &actual_parameters, parameters, state);
220
221 if ((parameter_count == 0)
222 || ((constructor_type->length != 0)
223 && (constructor_type->length != parameter_count))) {
224 const unsigned min_param = (constructor_type->length == 0)
225 ? 1 : constructor_type->length;
226
227 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
228 "parameter%s",
229 (constructor_type->length != 0) ? "at least" : "exactly",
230 min_param, (min_param <= 1) ? "" : "s");
231 return ir_call::get_error_instruction();
232 }
233
234 if (constructor_type->length == 0) {
235 constructor_type =
236 glsl_type::get_array_instance(constructor_type->get_base_type(),
237 parameter_count);
238 assert(constructor_type != NULL);
239 assert(constructor_type->length == parameter_count);
240 }
241
242 ir_function *f = state->symbols->get_function(constructor_type->name);
243
244 /* If the constructor for this type of array does not exist, generate the
245 * prototype and add it to the symbol table. The code will be generated
246 * later.
247 */
248 if (f == NULL) {
249 f = constructor_type->generate_constructor_prototype(state->symbols);
250 }
251
252 ir_rvalue *const r =
253 process_call(instructions, f, loc, &actual_parameters, state);
254
255 assert(r != NULL);
256 assert(r->type->is_error() || (r->type == constructor_type));
257
258 return r;
259}
260
261
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700262ir_rvalue *
Ian Romanick548fa292010-03-15 13:04:13 -0700263ast_function_expression::hir(exec_list *instructions,
264 struct _mesa_glsl_parse_state *state)
265{
266 /* There are three sorts of function calls.
267 *
268 * 1. contstructors - The first subexpression is an ast_type_specifier.
269 * 2. methods - Only the .length() method of array types.
270 * 3. functions - Calls to regular old functions.
271 *
Ian Romanick548fa292010-03-15 13:04:13 -0700272 * Method calls are actually detected when the ast_field_selection
273 * expression is handled.
274 */
275 if (is_constructor()) {
Ian Romanickabef9552010-03-23 15:08:30 -0700276 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
277 YYLTYPE loc = type->get_location();
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700278 const char *name;
Ian Romanickabef9552010-03-23 15:08:30 -0700279
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700280 const glsl_type *const constructor_type = type->glsl_type(& name, state);
Ian Romanickabef9552010-03-23 15:08:30 -0700281
282
283 /* Constructors for samplers are illegal.
284 */
285 if (constructor_type->is_sampler()) {
286 _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
287 constructor_type->name);
288 return ir_call::get_error_instruction();
289 }
290
Ian Romanickb6326ab2010-03-31 16:25:21 -0700291 if (constructor_type->is_array()) {
292 if (state->language_version <= 110) {
293 _mesa_glsl_error(& loc, state,
294 "array constructors forbidden in GLSL 1.10");
295 return ir_call::get_error_instruction();
296 }
297
Ian Romanick00aa1732010-03-31 16:48:48 -0700298 return process_array_constructor(instructions, constructor_type,
299 & loc, subexpressions[1], state);
Ian Romanickb6326ab2010-03-31 16:25:21 -0700300 }
Ian Romanickabef9552010-03-23 15:08:30 -0700301
302 /* There are two kinds of constructor call. Constructors for built-in
303 * language types, such as mat4 and vec2, are free form. The only
304 * requirement is that the parameters must provide enough values of the
305 * correct scalar type. Constructors for arrays and structures must
306 * have the exact number of parameters with matching types in the
307 * correct order. These constructors follow essentially the same type
308 * matching rules as functions.
309 */
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700310 if (constructor_type->is_numeric() || constructor_type->is_boolean()) {
311 /* Constructing a numeric type has a couple steps. First all values
312 * passed to the constructor are broken into individual parameters
313 * and type converted to the base type of the thing being constructed.
314 *
315 * At that point we have some number of values that match the base
316 * type of the thing being constructed. Now the constructor can be
317 * treated like a function call. Each numeric type has a small set
318 * of constructor functions. The set of new parameters will either
319 * match one of those functions or the original constructor is
320 * invalid.
321 */
322 const glsl_type *const base_type = constructor_type->get_base_type();
323
324 /* Total number of components of the type being constructed.
325 */
326 const unsigned type_components = constructor_type->components();
327
328 /* Number of components from parameters that have actually been
329 * consumed. This is used to perform several kinds of error checking.
330 */
331 unsigned components_used = 0;
332
333 unsigned matrix_parameters = 0;
334 unsigned nonmatrix_parameters = 0;
335 exec_list actual_parameters;
336 simple_node *const first = subexpressions[1];
337
338 assert(first != NULL);
339
340 if (first != NULL) {
341 simple_node *ptr = first;
342 do {
343 ir_rvalue *const result =
344 ((ast_node *) ptr)->hir(instructions, state)->as_rvalue();
345 ptr = ptr->next;
346
347 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
348 *
349 * "It is an error to provide extra arguments beyond this
350 * last used argument."
351 */
352 if (components_used >= type_components) {
353 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
354 "constructor",
355 constructor_type->name);
356 return ir_call::get_error_instruction();
357 }
358
359 if (!result->type->is_numeric() && !result->type->is_boolean()) {
360 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
361 "non-numeric data type",
362 constructor_type->name);
363 return ir_call::get_error_instruction();
364 }
365
366 /* Count the number of matrix and nonmatrix parameters. This
367 * is used below to enforce some of the constructor rules.
368 */
369 if (result->type->is_matrix())
370 matrix_parameters++;
371 else
372 nonmatrix_parameters++;
373
374
375 /* Process each of the components of the parameter. Dereference
376 * each component individually, perform any type conversions, and
377 * add it to the parameter list for the constructor.
378 */
379 for (unsigned i = 0; i < result->type->components(); i++) {
380 if (components_used >= type_components)
381 break;
382
383 ir_rvalue *const component =
384 convert_component(dereference_component(result, i),
385 base_type);
386
387 /* All cases that could result in component->type being the
388 * error type should have already been caught above.
389 */
390 assert(component->type == base_type);
391
392 /* Don't actually generate constructor calls for scalars.
393 * Instead, do the usual component selection and conversion,
394 * and return the single component.
395 */
396 if (constructor_type->is_scalar())
397 return component;
398
399 actual_parameters.push_tail(component);
400 components_used++;
401 }
402 } while (ptr != first);
403 }
404
405 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
406 *
407 * "It is an error to construct matrices from other matrices. This
408 * is reserved for future use."
409 */
410 if ((state->language_version <= 110) && (matrix_parameters > 0)
411 && constructor_type->is_matrix()) {
412 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
413 "matrix in GLSL 1.10",
414 constructor_type->name);
415 return ir_call::get_error_instruction();
416 }
417
418 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
419 *
420 * "If a matrix argument is given to a matrix constructor, it is
421 * an error to have any other arguments."
422 */
423 if ((matrix_parameters > 0)
424 && ((matrix_parameters + nonmatrix_parameters) > 1)
425 && constructor_type->is_matrix()) {
426 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
427 "matrix must be only parameter",
428 constructor_type->name);
429 return ir_call::get_error_instruction();
430 }
431
432 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
433 *
434 * "In these cases, there must be enough components provided in the
435 * arguments to provide an initializer for every component in the
436 * constructed value."
437 */
Ian Romanick8a24cd52010-03-29 15:36:02 -0700438 if ((components_used < type_components) && (components_used != 1)) {
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700439 _mesa_glsl_error(& loc, state, "too few components to construct "
440 "`%s'",
441 constructor_type->name);
442 return ir_call::get_error_instruction();
443 }
444
445 ir_function *f = state->symbols->get_function(constructor_type->name);
446 if (f == NULL) {
447 _mesa_glsl_error(& loc, state, "no constructor for type `%s'",
448 constructor_type->name);
449 return ir_call::get_error_instruction();
450 }
451
452 const ir_function_signature *sig =
453 f->matching_signature(& actual_parameters);
454 if (sig != NULL) {
455 return new ir_call(sig, & actual_parameters);
456 } else {
457 /* FINISHME: Log a better error message here. G++ will show the
458 * FINSIHME: types of the actual parameters and the set of
459 * FINSIHME: candidate functions. A different error should also be
460 * FINSIHME: logged when multiple functions match.
461 */
462 _mesa_glsl_error(& loc, state, "no matching constructor for `%s'",
463 constructor_type->name);
464 return ir_call::get_error_instruction();
465 }
466 }
Ian Romanickabef9552010-03-23 15:08:30 -0700467
Ian Romanick548fa292010-03-15 13:04:13 -0700468 return ir_call::get_error_instruction();
469 } else {
470 const ast_expression *id = subexpressions[0];
Ian Romanickf4749612010-03-15 13:26:02 -0700471 YYLTYPE loc = id->get_location();
Ian Romanick548fa292010-03-15 13:04:13 -0700472
Ian Romanickf4749612010-03-15 13:26:02 -0700473 return match_function_by_name(instructions,
474 id->primary_expression.identifier, & loc,
475 subexpressions[1], state);
Ian Romanick548fa292010-03-15 13:04:13 -0700476 }
477
478 return ir_call::get_error_instruction();
479}