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