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