blob: 74ae5d8962bf13db4c3d92bc89ab086e52ba28d0 [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
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -070030static ir_rvalue *
Ian Romanickf4749612010-03-15 13:26:02 -070031match_function_by_name(exec_list *instructions, const char *name,
32 YYLTYPE *loc, simple_node *parameters,
33 struct _mesa_glsl_parse_state *state)
34{
Ian Romanick8bde4ce2010-03-19 11:57:24 -070035 ir_function *f = state->symbols->get_function(name);
Ian Romanickf4749612010-03-15 13:26:02 -070036
37 if (f == NULL) {
38 _mesa_glsl_error(loc, state, "function `%s' undeclared", name);
39 return ir_call::get_error_instruction();
40 }
41
42 /* Once we've determined that the function being called might exist,
43 * process the parameters.
44 */
45 exec_list actual_parameters;
46 simple_node *const first = parameters;
47 if (first != NULL) {
48 simple_node *ptr = first;
49 do {
50 ir_instruction *const result =
51 ((ast_node *) ptr)->hir(instructions, state);
52 ptr = ptr->next;
53
54 actual_parameters.push_tail(result);
55 } while (ptr != first);
56 }
57
58 /* After processing the function's actual parameters, try to find an
59 * overload of the function that matches.
60 */
61 const ir_function_signature *sig =
62 f->matching_signature(& actual_parameters);
63 if (sig != NULL) {
64 /* FINISHME: The list of actual parameters needs to be modified to
65 * FINISHME: include any necessary conversions.
66 */
67 return new ir_call(sig, & actual_parameters);
68 } else {
69 /* FINISHME: Log a better error message here. G++ will show the types
70 * FINISHME: of the actual parameters and the set of candidate
71 * FINISHME: functions. A different error should also be logged when
72 * FINISHME: multiple functions match.
73 */
74 _mesa_glsl_error(loc, state, "no matching function for call to `%s'",
75 name);
76 return ir_call::get_error_instruction();
77 }
78}
79
80
Ian Romanick0b7dcc82010-03-26 17:38:58 -070081/**
82 * Perform automatic type conversion of constructor parameters
83 */
84static ir_rvalue *
85convert_component(ir_rvalue *src, const glsl_type *desired_type)
86{
87 const unsigned a = desired_type->base_type;
88 const unsigned b = src->type->base_type;
89
90 if (src->type->is_error())
91 return src;
92
93 assert(a <= GLSL_TYPE_BOOL);
94 assert(b <= GLSL_TYPE_BOOL);
95
96 if ((a == b) || (src->type->is_integer() && desired_type->is_integer()))
97 return src;
98
99 switch (a) {
100 case GLSL_TYPE_UINT:
101 case GLSL_TYPE_INT:
102 if (b == GLSL_TYPE_FLOAT)
103 return new ir_expression(ir_unop_f2i, desired_type, src, NULL);
104 else {
105 assert(b == GLSL_TYPE_BOOL);
106 assert(!"FINISHME: Convert bool to int / uint.");
107 }
108 case GLSL_TYPE_FLOAT:
109 switch (b) {
110 case GLSL_TYPE_UINT:
111 return new ir_expression(ir_unop_u2f, desired_type, src, NULL);
112 case GLSL_TYPE_INT:
113 return new ir_expression(ir_unop_i2f, desired_type, src, NULL);
114 case GLSL_TYPE_BOOL:
115 assert(!"FINISHME: Convert bool to float.");
116 }
117 break;
118 case GLSL_TYPE_BOOL: {
119 int z = 0;
120 ir_constant *const zero = new ir_constant(src->type, &z);
121
122 return new ir_expression(ir_binop_nequal, desired_type, src, zero);
123 }
124 }
125
126 assert(!"Should not get here.");
127 return NULL;
128}
129
130
131/**
132 * Dereference a specific component from a scalar, vector, or matrix
133 */
134static ir_rvalue *
135dereference_component(ir_rvalue *src, unsigned component)
136{
137 assert(component < src->type->components());
138
139 if (src->type->is_scalar()) {
140 return src;
141 } else if (src->type->is_vector()) {
142 return new ir_swizzle(src, component, 0, 0, 0, 1);
143 } else {
144 assert(src->type->is_matrix());
145
146 /* Dereference a row of the matrix, then call this function again to get
147 * a specific element from that row.
148 */
149 const int c = component / src->type->column_type()->vector_elements;
150 const int r = component % src->type->column_type()->vector_elements;
151 ir_constant *const col_index = new ir_constant(glsl_type::int_type, &c);
152 ir_dereference *const col = new ir_dereference(src, col_index);
153
154 col->type = src->type->column_type();
155
156 return dereference_component(col, r);
157 }
158
159 assert(!"Should not get here.");
160 return NULL;
161}
162
163
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700164ir_rvalue *
Ian Romanick548fa292010-03-15 13:04:13 -0700165ast_function_expression::hir(exec_list *instructions,
166 struct _mesa_glsl_parse_state *state)
167{
168 /* There are three sorts of function calls.
169 *
170 * 1. contstructors - The first subexpression is an ast_type_specifier.
171 * 2. methods - Only the .length() method of array types.
172 * 3. functions - Calls to regular old functions.
173 *
Ian Romanick548fa292010-03-15 13:04:13 -0700174 * Method calls are actually detected when the ast_field_selection
175 * expression is handled.
176 */
177 if (is_constructor()) {
Ian Romanickabef9552010-03-23 15:08:30 -0700178 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
179 YYLTYPE loc = type->get_location();
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700180 const char *name;
Ian Romanickabef9552010-03-23 15:08:30 -0700181
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700182 const glsl_type *const constructor_type = type->glsl_type(& name, state);
Ian Romanickabef9552010-03-23 15:08:30 -0700183
184
185 /* Constructors for samplers are illegal.
186 */
187 if (constructor_type->is_sampler()) {
188 _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
189 constructor_type->name);
190 return ir_call::get_error_instruction();
191 }
192
Ian Romanickb6326ab2010-03-31 16:25:21 -0700193 if (constructor_type->is_array()) {
194 if (state->language_version <= 110) {
195 _mesa_glsl_error(& loc, state,
196 "array constructors forbidden in GLSL 1.10");
197 return ir_call::get_error_instruction();
198 }
199
200 return ir_call::get_error_instruction();
201 }
Ian Romanickabef9552010-03-23 15:08:30 -0700202
203 /* There are two kinds of constructor call. Constructors for built-in
204 * language types, such as mat4 and vec2, are free form. The only
205 * requirement is that the parameters must provide enough values of the
206 * correct scalar type. Constructors for arrays and structures must
207 * have the exact number of parameters with matching types in the
208 * correct order. These constructors follow essentially the same type
209 * matching rules as functions.
210 */
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700211 if (constructor_type->is_numeric() || constructor_type->is_boolean()) {
212 /* Constructing a numeric type has a couple steps. First all values
213 * passed to the constructor are broken into individual parameters
214 * and type converted to the base type of the thing being constructed.
215 *
216 * At that point we have some number of values that match the base
217 * type of the thing being constructed. Now the constructor can be
218 * treated like a function call. Each numeric type has a small set
219 * of constructor functions. The set of new parameters will either
220 * match one of those functions or the original constructor is
221 * invalid.
222 */
223 const glsl_type *const base_type = constructor_type->get_base_type();
224
225 /* Total number of components of the type being constructed.
226 */
227 const unsigned type_components = constructor_type->components();
228
229 /* Number of components from parameters that have actually been
230 * consumed. This is used to perform several kinds of error checking.
231 */
232 unsigned components_used = 0;
233
234 unsigned matrix_parameters = 0;
235 unsigned nonmatrix_parameters = 0;
236 exec_list actual_parameters;
237 simple_node *const first = subexpressions[1];
238
239 assert(first != NULL);
240
241 if (first != NULL) {
242 simple_node *ptr = first;
243 do {
244 ir_rvalue *const result =
245 ((ast_node *) ptr)->hir(instructions, state)->as_rvalue();
246 ptr = ptr->next;
247
248 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
249 *
250 * "It is an error to provide extra arguments beyond this
251 * last used argument."
252 */
253 if (components_used >= type_components) {
254 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
255 "constructor",
256 constructor_type->name);
257 return ir_call::get_error_instruction();
258 }
259
260 if (!result->type->is_numeric() && !result->type->is_boolean()) {
261 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
262 "non-numeric data type",
263 constructor_type->name);
264 return ir_call::get_error_instruction();
265 }
266
267 /* Count the number of matrix and nonmatrix parameters. This
268 * is used below to enforce some of the constructor rules.
269 */
270 if (result->type->is_matrix())
271 matrix_parameters++;
272 else
273 nonmatrix_parameters++;
274
275
276 /* Process each of the components of the parameter. Dereference
277 * each component individually, perform any type conversions, and
278 * add it to the parameter list for the constructor.
279 */
280 for (unsigned i = 0; i < result->type->components(); i++) {
281 if (components_used >= type_components)
282 break;
283
284 ir_rvalue *const component =
285 convert_component(dereference_component(result, i),
286 base_type);
287
288 /* All cases that could result in component->type being the
289 * error type should have already been caught above.
290 */
291 assert(component->type == base_type);
292
293 /* Don't actually generate constructor calls for scalars.
294 * Instead, do the usual component selection and conversion,
295 * and return the single component.
296 */
297 if (constructor_type->is_scalar())
298 return component;
299
300 actual_parameters.push_tail(component);
301 components_used++;
302 }
303 } while (ptr != first);
304 }
305
306 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
307 *
308 * "It is an error to construct matrices from other matrices. This
309 * is reserved for future use."
310 */
311 if ((state->language_version <= 110) && (matrix_parameters > 0)
312 && constructor_type->is_matrix()) {
313 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
314 "matrix in GLSL 1.10",
315 constructor_type->name);
316 return ir_call::get_error_instruction();
317 }
318
319 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
320 *
321 * "If a matrix argument is given to a matrix constructor, it is
322 * an error to have any other arguments."
323 */
324 if ((matrix_parameters > 0)
325 && ((matrix_parameters + nonmatrix_parameters) > 1)
326 && constructor_type->is_matrix()) {
327 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
328 "matrix must be only parameter",
329 constructor_type->name);
330 return ir_call::get_error_instruction();
331 }
332
333 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
334 *
335 * "In these cases, there must be enough components provided in the
336 * arguments to provide an initializer for every component in the
337 * constructed value."
338 */
Ian Romanick8a24cd52010-03-29 15:36:02 -0700339 if ((components_used < type_components) && (components_used != 1)) {
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700340 _mesa_glsl_error(& loc, state, "too few components to construct "
341 "`%s'",
342 constructor_type->name);
343 return ir_call::get_error_instruction();
344 }
345
346 ir_function *f = state->symbols->get_function(constructor_type->name);
347 if (f == NULL) {
348 _mesa_glsl_error(& loc, state, "no constructor for type `%s'",
349 constructor_type->name);
350 return ir_call::get_error_instruction();
351 }
352
353 const ir_function_signature *sig =
354 f->matching_signature(& actual_parameters);
355 if (sig != NULL) {
356 return new ir_call(sig, & actual_parameters);
357 } else {
358 /* FINISHME: Log a better error message here. G++ will show the
359 * FINSIHME: types of the actual parameters and the set of
360 * FINSIHME: candidate functions. A different error should also be
361 * FINSIHME: logged when multiple functions match.
362 */
363 _mesa_glsl_error(& loc, state, "no matching constructor for `%s'",
364 constructor_type->name);
365 return ir_call::get_error_instruction();
366 }
367 }
Ian Romanickabef9552010-03-23 15:08:30 -0700368
Ian Romanick548fa292010-03-15 13:04:13 -0700369 return ir_call::get_error_instruction();
370 } else {
371 const ast_expression *id = subexpressions[0];
Ian Romanickf4749612010-03-15 13:26:02 -0700372 YYLTYPE loc = id->get_location();
Ian Romanick548fa292010-03-15 13:04:13 -0700373
Ian Romanickf4749612010-03-15 13:26:02 -0700374 return match_function_by_name(instructions,
375 id->primary_expression.identifier, & loc,
376 subexpressions[1], state);
Ian Romanick548fa292010-03-15 13:04:13 -0700377 }
378
379 return ir_call::get_error_instruction();
380}