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