blob: 340873ae4181984c83dc83cea25455dcffc82a13 [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);
133 assert(!"FINISHME: Convert bool to int / uint.");
134 }
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:
142 assert(!"FINISHME: Convert bool to float.");
143 }
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
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700191ir_rvalue *
Ian Romanick548fa292010-03-15 13:04:13 -0700192ast_function_expression::hir(exec_list *instructions,
193 struct _mesa_glsl_parse_state *state)
194{
195 /* There are three sorts of function calls.
196 *
197 * 1. contstructors - The first subexpression is an ast_type_specifier.
198 * 2. methods - Only the .length() method of array types.
199 * 3. functions - Calls to regular old functions.
200 *
Ian Romanick548fa292010-03-15 13:04:13 -0700201 * Method calls are actually detected when the ast_field_selection
202 * expression is handled.
203 */
204 if (is_constructor()) {
Ian Romanickabef9552010-03-23 15:08:30 -0700205 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
206 YYLTYPE loc = type->get_location();
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700207 const char *name;
Ian Romanickabef9552010-03-23 15:08:30 -0700208
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700209 const glsl_type *const constructor_type = type->glsl_type(& name, state);
Ian Romanickabef9552010-03-23 15:08:30 -0700210
211
212 /* Constructors for samplers are illegal.
213 */
214 if (constructor_type->is_sampler()) {
215 _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
216 constructor_type->name);
217 return ir_call::get_error_instruction();
218 }
219
Ian Romanickb6326ab2010-03-31 16:25:21 -0700220 if (constructor_type->is_array()) {
221 if (state->language_version <= 110) {
222 _mesa_glsl_error(& loc, state,
223 "array constructors forbidden in GLSL 1.10");
224 return ir_call::get_error_instruction();
225 }
226
227 return ir_call::get_error_instruction();
228 }
Ian Romanickabef9552010-03-23 15:08:30 -0700229
230 /* There are two kinds of constructor call. Constructors for built-in
231 * language types, such as mat4 and vec2, are free form. The only
232 * requirement is that the parameters must provide enough values of the
233 * correct scalar type. Constructors for arrays and structures must
234 * have the exact number of parameters with matching types in the
235 * correct order. These constructors follow essentially the same type
236 * matching rules as functions.
237 */
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700238 if (constructor_type->is_numeric() || constructor_type->is_boolean()) {
239 /* Constructing a numeric type has a couple steps. First all values
240 * passed to the constructor are broken into individual parameters
241 * and type converted to the base type of the thing being constructed.
242 *
243 * At that point we have some number of values that match the base
244 * type of the thing being constructed. Now the constructor can be
245 * treated like a function call. Each numeric type has a small set
246 * of constructor functions. The set of new parameters will either
247 * match one of those functions or the original constructor is
248 * invalid.
249 */
250 const glsl_type *const base_type = constructor_type->get_base_type();
251
252 /* Total number of components of the type being constructed.
253 */
254 const unsigned type_components = constructor_type->components();
255
256 /* Number of components from parameters that have actually been
257 * consumed. This is used to perform several kinds of error checking.
258 */
259 unsigned components_used = 0;
260
261 unsigned matrix_parameters = 0;
262 unsigned nonmatrix_parameters = 0;
263 exec_list actual_parameters;
264 simple_node *const first = subexpressions[1];
265
266 assert(first != NULL);
267
268 if (first != NULL) {
269 simple_node *ptr = first;
270 do {
271 ir_rvalue *const result =
272 ((ast_node *) ptr)->hir(instructions, state)->as_rvalue();
273 ptr = ptr->next;
274
275 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
276 *
277 * "It is an error to provide extra arguments beyond this
278 * last used argument."
279 */
280 if (components_used >= type_components) {
281 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
282 "constructor",
283 constructor_type->name);
284 return ir_call::get_error_instruction();
285 }
286
287 if (!result->type->is_numeric() && !result->type->is_boolean()) {
288 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
289 "non-numeric data type",
290 constructor_type->name);
291 return ir_call::get_error_instruction();
292 }
293
294 /* Count the number of matrix and nonmatrix parameters. This
295 * is used below to enforce some of the constructor rules.
296 */
297 if (result->type->is_matrix())
298 matrix_parameters++;
299 else
300 nonmatrix_parameters++;
301
302
303 /* Process each of the components of the parameter. Dereference
304 * each component individually, perform any type conversions, and
305 * add it to the parameter list for the constructor.
306 */
307 for (unsigned i = 0; i < result->type->components(); i++) {
308 if (components_used >= type_components)
309 break;
310
311 ir_rvalue *const component =
312 convert_component(dereference_component(result, i),
313 base_type);
314
315 /* All cases that could result in component->type being the
316 * error type should have already been caught above.
317 */
318 assert(component->type == base_type);
319
320 /* Don't actually generate constructor calls for scalars.
321 * Instead, do the usual component selection and conversion,
322 * and return the single component.
323 */
324 if (constructor_type->is_scalar())
325 return component;
326
327 actual_parameters.push_tail(component);
328 components_used++;
329 }
330 } while (ptr != first);
331 }
332
333 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
334 *
335 * "It is an error to construct matrices from other matrices. This
336 * is reserved for future use."
337 */
338 if ((state->language_version <= 110) && (matrix_parameters > 0)
339 && constructor_type->is_matrix()) {
340 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
341 "matrix in GLSL 1.10",
342 constructor_type->name);
343 return ir_call::get_error_instruction();
344 }
345
346 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
347 *
348 * "If a matrix argument is given to a matrix constructor, it is
349 * an error to have any other arguments."
350 */
351 if ((matrix_parameters > 0)
352 && ((matrix_parameters + nonmatrix_parameters) > 1)
353 && constructor_type->is_matrix()) {
354 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
355 "matrix must be only parameter",
356 constructor_type->name);
357 return ir_call::get_error_instruction();
358 }
359
360 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
361 *
362 * "In these cases, there must be enough components provided in the
363 * arguments to provide an initializer for every component in the
364 * constructed value."
365 */
Ian Romanick8a24cd52010-03-29 15:36:02 -0700366 if ((components_used < type_components) && (components_used != 1)) {
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700367 _mesa_glsl_error(& loc, state, "too few components to construct "
368 "`%s'",
369 constructor_type->name);
370 return ir_call::get_error_instruction();
371 }
372
373 ir_function *f = state->symbols->get_function(constructor_type->name);
374 if (f == NULL) {
375 _mesa_glsl_error(& loc, state, "no constructor for type `%s'",
376 constructor_type->name);
377 return ir_call::get_error_instruction();
378 }
379
380 const ir_function_signature *sig =
381 f->matching_signature(& actual_parameters);
382 if (sig != NULL) {
383 return new ir_call(sig, & actual_parameters);
384 } else {
385 /* FINISHME: Log a better error message here. G++ will show the
386 * FINSIHME: types of the actual parameters and the set of
387 * FINSIHME: candidate functions. A different error should also be
388 * FINSIHME: logged when multiple functions match.
389 */
390 _mesa_glsl_error(& loc, state, "no matching constructor for `%s'",
391 constructor_type->name);
392 return ir_call::get_error_instruction();
393 }
394 }
Ian Romanickabef9552010-03-23 15:08:30 -0700395
Ian Romanick548fa292010-03-15 13:04:13 -0700396 return ir_call::get_error_instruction();
397 } else {
398 const ast_expression *id = subexpressions[0];
Ian Romanickf4749612010-03-15 13:26:02 -0700399 YYLTYPE loc = id->get_location();
Ian Romanick548fa292010-03-15 13:04:13 -0700400
Ian Romanickf4749612010-03-15 13:26:02 -0700401 return match_function_by_name(instructions,
402 id->primary_expression.identifier, & loc,
403 subexpressions[1], state);
Ian Romanick548fa292010-03-15 13:04:13 -0700404 }
405
406 return ir_call::get_error_instruction();
407}