ast_to_hir.cpp revision 7e2aa91507a5883e33473e0a94215ee3985baad1
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/** 25 * \file ast_to_hir.c 26 * Convert abstract syntax to to high-level intermediate reprensentation (HIR). 27 * 28 * During the conversion to HIR, the majority of the symantic checking is 29 * preformed on the program. This includes: 30 * 31 * * Symbol table management 32 * * Type checking 33 * * Function binding 34 * 35 * The majority of this work could be done during parsing, and the parser could 36 * probably generate HIR directly. However, this results in frequent changes 37 * to the parser code. Since we do not assume that every system this complier 38 * is built on will have Flex and Bison installed, we have to store the code 39 * generated by these tools in our version control system. In other parts of 40 * the system we've seen problems where a parser was changed but the generated 41 * code was not committed, merge conflicts where created because two developers 42 * had slightly different versions of Bison installed, etc. 43 * 44 * I have also noticed that running Bison generated parsers in GDB is very 45 * irritating. When you get a segfault on '$$ = $1->foo', you can't very 46 * well 'print $1' in GDB. 47 * 48 * As a result, my preference is to put as little C code as possible in the 49 * parser (and lexer) sources. 50 */ 51 52#include "main/imports.h" 53#include "glsl_symbol_table.h" 54#include "glsl_parser_extras.h" 55#include "ast.h" 56#include "glsl_types.h" 57#include "ir.h" 58 59void 60_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state) 61{ 62 _mesa_glsl_initialize_variables(instructions, state); 63 _mesa_glsl_initialize_functions(instructions, state); 64 65 state->current_function = NULL; 66 67 foreach_list_typed (ast_node, ast, link, & state->translation_unit) 68 ast->hir(instructions, state); 69} 70 71 72/** 73 * If a conversion is available, convert one operand to a different type 74 * 75 * The \c from \c ir_rvalue is converted "in place". 76 * 77 * \param to Type that the operand it to be converted to 78 * \param from Operand that is being converted 79 * \param state GLSL compiler state 80 * 81 * \return 82 * If a conversion is possible (or unnecessary), \c true is returned. 83 * Otherwise \c false is returned. 84 */ 85static bool 86apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from, 87 struct _mesa_glsl_parse_state *state) 88{ 89 void *ctx = state; 90 if (to->base_type == from->type->base_type) 91 return true; 92 93 /* This conversion was added in GLSL 1.20. If the compilation mode is 94 * GLSL 1.10, the conversion is skipped. 95 */ 96 if (state->language_version < 120) 97 return false; 98 99 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec: 100 * 101 * "There are no implicit array or structure conversions. For 102 * example, an array of int cannot be implicitly converted to an 103 * array of float. There are no implicit conversions between 104 * signed and unsigned integers." 105 */ 106 /* FINISHME: The above comment is partially a lie. There is int/uint 107 * FINISHME: conversion for immediate constants. 108 */ 109 if (!to->is_float() || !from->type->is_numeric()) 110 return false; 111 112 /* Convert to a floating point type with the same number of components 113 * as the original type - i.e. int to float, not int to vec4. 114 */ 115 to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements, 116 from->type->matrix_columns); 117 118 switch (from->type->base_type) { 119 case GLSL_TYPE_INT: 120 from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL); 121 break; 122 case GLSL_TYPE_UINT: 123 from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL); 124 break; 125 case GLSL_TYPE_BOOL: 126 from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL); 127 break; 128 default: 129 assert(0); 130 } 131 132 return true; 133} 134 135 136static const struct glsl_type * 137arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, 138 bool multiply, 139 struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 140{ 141 const glsl_type *type_a = value_a->type; 142 const glsl_type *type_b = value_b->type; 143 144 /* From GLSL 1.50 spec, page 56: 145 * 146 * "The arithmetic binary operators add (+), subtract (-), 147 * multiply (*), and divide (/) operate on integer and 148 * floating-point scalars, vectors, and matrices." 149 */ 150 if (!type_a->is_numeric() || !type_b->is_numeric()) { 151 _mesa_glsl_error(loc, state, 152 "Operands to arithmetic operators must be numeric"); 153 return glsl_type::error_type; 154 } 155 156 157 /* "If one operand is floating-point based and the other is 158 * not, then the conversions from Section 4.1.10 "Implicit 159 * Conversions" are applied to the non-floating-point-based operand." 160 */ 161 if (!apply_implicit_conversion(type_a, value_b, state) 162 && !apply_implicit_conversion(type_b, value_a, state)) { 163 _mesa_glsl_error(loc, state, 164 "Could not implicitly convert operands to " 165 "arithmetic operator"); 166 return glsl_type::error_type; 167 } 168 type_a = value_a->type; 169 type_b = value_b->type; 170 171 /* "If the operands are integer types, they must both be signed or 172 * both be unsigned." 173 * 174 * From this rule and the preceeding conversion it can be inferred that 175 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT. 176 * The is_numeric check above already filtered out the case where either 177 * type is not one of these, so now the base types need only be tested for 178 * equality. 179 */ 180 if (type_a->base_type != type_b->base_type) { 181 _mesa_glsl_error(loc, state, 182 "base type mismatch for arithmetic operator"); 183 return glsl_type::error_type; 184 } 185 186 /* "All arithmetic binary operators result in the same fundamental type 187 * (signed integer, unsigned integer, or floating-point) as the 188 * operands they operate on, after operand type conversion. After 189 * conversion, the following cases are valid 190 * 191 * * The two operands are scalars. In this case the operation is 192 * applied, resulting in a scalar." 193 */ 194 if (type_a->is_scalar() && type_b->is_scalar()) 195 return type_a; 196 197 /* "* One operand is a scalar, and the other is a vector or matrix. 198 * In this case, the scalar operation is applied independently to each 199 * component of the vector or matrix, resulting in the same size 200 * vector or matrix." 201 */ 202 if (type_a->is_scalar()) { 203 if (!type_b->is_scalar()) 204 return type_b; 205 } else if (type_b->is_scalar()) { 206 return type_a; 207 } 208 209 /* All of the combinations of <scalar, scalar>, <vector, scalar>, 210 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been 211 * handled. 212 */ 213 assert(!type_a->is_scalar()); 214 assert(!type_b->is_scalar()); 215 216 /* "* The two operands are vectors of the same size. In this case, the 217 * operation is done component-wise resulting in the same size 218 * vector." 219 */ 220 if (type_a->is_vector() && type_b->is_vector()) { 221 if (type_a == type_b) { 222 return type_a; 223 } else { 224 _mesa_glsl_error(loc, state, 225 "vector size mismatch for arithmetic operator"); 226 return glsl_type::error_type; 227 } 228 } 229 230 /* All of the combinations of <scalar, scalar>, <vector, scalar>, 231 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and 232 * <vector, vector> have been handled. At least one of the operands must 233 * be matrix. Further, since there are no integer matrix types, the base 234 * type of both operands must be float. 235 */ 236 assert(type_a->is_matrix() || type_b->is_matrix()); 237 assert(type_a->base_type == GLSL_TYPE_FLOAT); 238 assert(type_b->base_type == GLSL_TYPE_FLOAT); 239 240 /* "* The operator is add (+), subtract (-), or divide (/), and the 241 * operands are matrices with the same number of rows and the same 242 * number of columns. In this case, the operation is done component- 243 * wise resulting in the same size matrix." 244 * * The operator is multiply (*), where both operands are matrices or 245 * one operand is a vector and the other a matrix. A right vector 246 * operand is treated as a column vector and a left vector operand as a 247 * row vector. In all these cases, it is required that the number of 248 * columns of the left operand is equal to the number of rows of the 249 * right operand. Then, the multiply (*) operation does a linear 250 * algebraic multiply, yielding an object that has the same number of 251 * rows as the left operand and the same number of columns as the right 252 * operand. Section 5.10 "Vector and Matrix Operations" explains in 253 * more detail how vectors and matrices are operated on." 254 */ 255 if (! multiply) { 256 if (type_a == type_b) 257 return type_a; 258 } else { 259 if (type_a->is_matrix() && type_b->is_matrix()) { 260 /* Matrix multiply. The columns of A must match the rows of B. Given 261 * the other previously tested constraints, this means the vector type 262 * of a row from A must be the same as the vector type of a column from 263 * B. 264 */ 265 if (type_a->row_type() == type_b->column_type()) { 266 /* The resulting matrix has the number of columns of matrix B and 267 * the number of rows of matrix A. We get the row count of A by 268 * looking at the size of a vector that makes up a column. The 269 * transpose (size of a row) is done for B. 270 */ 271 const glsl_type *const type = 272 glsl_type::get_instance(type_a->base_type, 273 type_a->column_type()->vector_elements, 274 type_b->row_type()->vector_elements); 275 assert(type != glsl_type::error_type); 276 277 return type; 278 } 279 } else if (type_a->is_matrix()) { 280 /* A is a matrix and B is a column vector. Columns of A must match 281 * rows of B. Given the other previously tested constraints, this 282 * means the vector type of a row from A must be the same as the 283 * vector the type of B. 284 */ 285 if (type_a->row_type() == type_b) 286 return type_b; 287 } else { 288 assert(type_b->is_matrix()); 289 290 /* A is a row vector and B is a matrix. Columns of A must match rows 291 * of B. Given the other previously tested constraints, this means 292 * the type of A must be the same as the vector type of a column from 293 * B. 294 */ 295 if (type_a == type_b->column_type()) 296 return type_a; 297 } 298 299 _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication"); 300 return glsl_type::error_type; 301 } 302 303 304 /* "All other cases are illegal." 305 */ 306 _mesa_glsl_error(loc, state, "type mismatch"); 307 return glsl_type::error_type; 308} 309 310 311static const struct glsl_type * 312unary_arithmetic_result_type(const struct glsl_type *type, 313 struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 314{ 315 /* From GLSL 1.50 spec, page 57: 316 * 317 * "The arithmetic unary operators negate (-), post- and pre-increment 318 * and decrement (-- and ++) operate on integer or floating-point 319 * values (including vectors and matrices). All unary operators work 320 * component-wise on their operands. These result with the same type 321 * they operated on." 322 */ 323 if (!type->is_numeric()) { 324 _mesa_glsl_error(loc, state, 325 "Operands to arithmetic operators must be numeric"); 326 return glsl_type::error_type; 327 } 328 329 return type; 330} 331 332 333static const struct glsl_type * 334modulus_result_type(const struct glsl_type *type_a, 335 const struct glsl_type *type_b, 336 struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 337{ 338 /* From GLSL 1.50 spec, page 56: 339 * "The operator modulus (%) operates on signed or unsigned integers or 340 * integer vectors. The operand types must both be signed or both be 341 * unsigned." 342 */ 343 if (!type_a->is_integer() || !type_b->is_integer() 344 || (type_a->base_type != type_b->base_type)) { 345 _mesa_glsl_error(loc, state, "type mismatch"); 346 return glsl_type::error_type; 347 } 348 349 /* "The operands cannot be vectors of differing size. If one operand is 350 * a scalar and the other vector, then the scalar is applied component- 351 * wise to the vector, resulting in the same type as the vector. If both 352 * are vectors of the same size, the result is computed component-wise." 353 */ 354 if (type_a->is_vector()) { 355 if (!type_b->is_vector() 356 || (type_a->vector_elements == type_b->vector_elements)) 357 return type_a; 358 } else 359 return type_b; 360 361 /* "The operator modulus (%) is not defined for any other data types 362 * (non-integer types)." 363 */ 364 _mesa_glsl_error(loc, state, "type mismatch"); 365 return glsl_type::error_type; 366} 367 368 369static const struct glsl_type * 370relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, 371 struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 372{ 373 const glsl_type *type_a = value_a->type; 374 const glsl_type *type_b = value_b->type; 375 376 /* From GLSL 1.50 spec, page 56: 377 * "The relational operators greater than (>), less than (<), greater 378 * than or equal (>=), and less than or equal (<=) operate only on 379 * scalar integer and scalar floating-point expressions." 380 */ 381 if (!type_a->is_numeric() 382 || !type_b->is_numeric() 383 || !type_a->is_scalar() 384 || !type_b->is_scalar()) { 385 _mesa_glsl_error(loc, state, 386 "Operands to relational operators must be scalar and " 387 "numeric"); 388 return glsl_type::error_type; 389 } 390 391 /* "Either the operands' types must match, or the conversions from 392 * Section 4.1.10 "Implicit Conversions" will be applied to the integer 393 * operand, after which the types must match." 394 */ 395 if (!apply_implicit_conversion(type_a, value_b, state) 396 && !apply_implicit_conversion(type_b, value_a, state)) { 397 _mesa_glsl_error(loc, state, 398 "Could not implicitly convert operands to " 399 "relational operator"); 400 return glsl_type::error_type; 401 } 402 type_a = value_a->type; 403 type_b = value_b->type; 404 405 if (type_a->base_type != type_b->base_type) { 406 _mesa_glsl_error(loc, state, "base type mismatch"); 407 return glsl_type::error_type; 408 } 409 410 /* "The result is scalar Boolean." 411 */ 412 return glsl_type::bool_type; 413} 414 415 416/** 417 * Validates that a value can be assigned to a location with a specified type 418 * 419 * Validates that \c rhs can be assigned to some location. If the types are 420 * not an exact match but an automatic conversion is possible, \c rhs will be 421 * converted. 422 * 423 * \return 424 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type. 425 * Otherwise the actual RHS to be assigned will be returned. This may be 426 * \c rhs, or it may be \c rhs after some type conversion. 427 * 428 * \note 429 * In addition to being used for assignments, this function is used to 430 * type-check return values. 431 */ 432ir_rvalue * 433validate_assignment(struct _mesa_glsl_parse_state *state, 434 const glsl_type *lhs_type, ir_rvalue *rhs) 435{ 436 const glsl_type *rhs_type = rhs->type; 437 438 /* If there is already some error in the RHS, just return it. Anything 439 * else will lead to an avalanche of error message back to the user. 440 */ 441 if (rhs_type->is_error()) 442 return rhs; 443 444 /* If the types are identical, the assignment can trivially proceed. 445 */ 446 if (rhs_type == lhs_type) 447 return rhs; 448 449 /* If the array element types are the same and the size of the LHS is zero, 450 * the assignment is okay. 451 * 452 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this 453 * is handled by ir_dereference::is_lvalue. 454 */ 455 if (lhs_type->is_array() && rhs->type->is_array() 456 && (lhs_type->element_type() == rhs->type->element_type()) 457 && (lhs_type->array_size() == 0)) { 458 return rhs; 459 } 460 461 /* Check for implicit conversion in GLSL 1.20 */ 462 if (apply_implicit_conversion(lhs_type, rhs, state)) { 463 rhs_type = rhs->type; 464 if (rhs_type == lhs_type) 465 return rhs; 466 } 467 468 return NULL; 469} 470 471ir_rvalue * 472do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state, 473 ir_rvalue *lhs, ir_rvalue *rhs, 474 YYLTYPE lhs_loc) 475{ 476 void *ctx = state; 477 bool error_emitted = (lhs->type->is_error() || rhs->type->is_error()); 478 479 if (!error_emitted) { 480 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */ 481 if (!lhs->is_lvalue()) { 482 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment"); 483 error_emitted = true; 484 } 485 } 486 487 ir_rvalue *new_rhs = validate_assignment(state, lhs->type, rhs); 488 if (new_rhs == NULL) { 489 _mesa_glsl_error(& lhs_loc, state, "type mismatch"); 490 } else { 491 rhs = new_rhs; 492 493 /* If the LHS array was not declared with a size, it takes it size from 494 * the RHS. If the LHS is an l-value and a whole array, it must be a 495 * dereference of a variable. Any other case would require that the LHS 496 * is either not an l-value or not a whole array. 497 */ 498 if (lhs->type->array_size() == 0) { 499 ir_dereference *const d = lhs->as_dereference(); 500 501 assert(d != NULL); 502 503 ir_variable *const var = d->variable_referenced(); 504 505 assert(var != NULL); 506 507 if (var->max_array_access >= unsigned(rhs->type->array_size())) { 508 /* FINISHME: This should actually log the location of the RHS. */ 509 _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to " 510 "previous access", 511 var->max_array_access); 512 } 513 514 var->type = glsl_type::get_array_instance(state, 515 lhs->type->element_type(), 516 rhs->type->array_size()); 517 } 518 } 519 520 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec, 521 * but not post_inc) need the converted assigned value as an rvalue 522 * to handle things like: 523 * 524 * i = j += 1; 525 * 526 * So we always just store the computed value being assigned to a 527 * temporary and return a deref of that temporary. If the rvalue 528 * ends up not being used, the temp will get copy-propagated out. 529 */ 530 ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp", 531 ir_var_temporary); 532 ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var); 533 instructions->push_tail(var); 534 instructions->push_tail(new(ctx) ir_assignment(deref_var, 535 rhs, 536 NULL)); 537 deref_var = new(ctx) ir_dereference_variable(var); 538 539 instructions->push_tail(new(ctx) ir_assignment(lhs, 540 deref_var, 541 NULL)); 542 543 return new(ctx) ir_dereference_variable(var); 544} 545 546static ir_rvalue * 547get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue) 548{ 549 void *ctx = talloc_parent(lvalue); 550 ir_variable *var; 551 552 /* FINISHME: Give unique names to the temporaries. */ 553 var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp", 554 ir_var_temporary); 555 instructions->push_tail(var); 556 var->mode = ir_var_auto; 557 558 instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var), 559 lvalue, NULL)); 560 561 /* Once we've created this temporary, mark it read only so it's no 562 * longer considered an lvalue. 563 */ 564 var->read_only = true; 565 566 return new(ctx) ir_dereference_variable(var); 567} 568 569 570ir_rvalue * 571ast_node::hir(exec_list *instructions, 572 struct _mesa_glsl_parse_state *state) 573{ 574 (void) instructions; 575 (void) state; 576 577 return NULL; 578} 579 580 581ir_rvalue * 582ast_expression::hir(exec_list *instructions, 583 struct _mesa_glsl_parse_state *state) 584{ 585 void *ctx = state; 586 static const int operations[AST_NUM_OPERATORS] = { 587 -1, /* ast_assign doesn't convert to ir_expression. */ 588 -1, /* ast_plus doesn't convert to ir_expression. */ 589 ir_unop_neg, 590 ir_binop_add, 591 ir_binop_sub, 592 ir_binop_mul, 593 ir_binop_div, 594 ir_binop_mod, 595 ir_binop_lshift, 596 ir_binop_rshift, 597 ir_binop_less, 598 ir_binop_greater, 599 ir_binop_lequal, 600 ir_binop_gequal, 601 ir_binop_equal, 602 ir_binop_nequal, 603 ir_binop_bit_and, 604 ir_binop_bit_xor, 605 ir_binop_bit_or, 606 ir_unop_bit_not, 607 ir_binop_logic_and, 608 ir_binop_logic_xor, 609 ir_binop_logic_or, 610 ir_unop_logic_not, 611 612 /* Note: The following block of expression types actually convert 613 * to multiple IR instructions. 614 */ 615 ir_binop_mul, /* ast_mul_assign */ 616 ir_binop_div, /* ast_div_assign */ 617 ir_binop_mod, /* ast_mod_assign */ 618 ir_binop_add, /* ast_add_assign */ 619 ir_binop_sub, /* ast_sub_assign */ 620 ir_binop_lshift, /* ast_ls_assign */ 621 ir_binop_rshift, /* ast_rs_assign */ 622 ir_binop_bit_and, /* ast_and_assign */ 623 ir_binop_bit_xor, /* ast_xor_assign */ 624 ir_binop_bit_or, /* ast_or_assign */ 625 626 -1, /* ast_conditional doesn't convert to ir_expression. */ 627 ir_binop_add, /* ast_pre_inc. */ 628 ir_binop_sub, /* ast_pre_dec. */ 629 ir_binop_add, /* ast_post_inc. */ 630 ir_binop_sub, /* ast_post_dec. */ 631 -1, /* ast_field_selection doesn't conv to ir_expression. */ 632 -1, /* ast_array_index doesn't convert to ir_expression. */ 633 -1, /* ast_function_call doesn't conv to ir_expression. */ 634 -1, /* ast_identifier doesn't convert to ir_expression. */ 635 -1, /* ast_int_constant doesn't convert to ir_expression. */ 636 -1, /* ast_uint_constant doesn't conv to ir_expression. */ 637 -1, /* ast_float_constant doesn't conv to ir_expression. */ 638 -1, /* ast_bool_constant doesn't conv to ir_expression. */ 639 -1, /* ast_sequence doesn't convert to ir_expression. */ 640 }; 641 ir_rvalue *result = NULL; 642 ir_rvalue *op[2]; 643 const struct glsl_type *type = glsl_type::error_type; 644 bool error_emitted = false; 645 YYLTYPE loc; 646 647 loc = this->get_location(); 648 649 switch (this->oper) { 650 case ast_assign: { 651 op[0] = this->subexpressions[0]->hir(instructions, state); 652 op[1] = this->subexpressions[1]->hir(instructions, state); 653 654 result = do_assignment(instructions, state, op[0], op[1], 655 this->subexpressions[0]->get_location()); 656 error_emitted = result->type->is_error(); 657 type = result->type; 658 break; 659 } 660 661 case ast_plus: 662 op[0] = this->subexpressions[0]->hir(instructions, state); 663 664 error_emitted = op[0]->type->is_error(); 665 if (type->is_error()) 666 op[0]->type = type; 667 668 result = op[0]; 669 break; 670 671 case ast_neg: 672 op[0] = this->subexpressions[0]->hir(instructions, state); 673 674 type = unary_arithmetic_result_type(op[0]->type, state, & loc); 675 676 error_emitted = type->is_error(); 677 678 result = new(ctx) ir_expression(operations[this->oper], type, 679 op[0], NULL); 680 break; 681 682 case ast_add: 683 case ast_sub: 684 case ast_mul: 685 case ast_div: 686 op[0] = this->subexpressions[0]->hir(instructions, state); 687 op[1] = this->subexpressions[1]->hir(instructions, state); 688 689 type = arithmetic_result_type(op[0], op[1], 690 (this->oper == ast_mul), 691 state, & loc); 692 error_emitted = type->is_error(); 693 694 result = new(ctx) ir_expression(operations[this->oper], type, 695 op[0], op[1]); 696 break; 697 698 case ast_mod: 699 op[0] = this->subexpressions[0]->hir(instructions, state); 700 op[1] = this->subexpressions[1]->hir(instructions, state); 701 702 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc); 703 704 assert(operations[this->oper] == ir_binop_mod); 705 706 result = new(ctx) ir_expression(operations[this->oper], type, 707 op[0], op[1]); 708 error_emitted = type->is_error(); 709 break; 710 711 case ast_lshift: 712 case ast_rshift: 713 _mesa_glsl_error(& loc, state, "FINISHME: implement bit-shift operators"); 714 error_emitted = true; 715 break; 716 717 case ast_less: 718 case ast_greater: 719 case ast_lequal: 720 case ast_gequal: 721 op[0] = this->subexpressions[0]->hir(instructions, state); 722 op[1] = this->subexpressions[1]->hir(instructions, state); 723 724 type = relational_result_type(op[0], op[1], state, & loc); 725 726 /* The relational operators must either generate an error or result 727 * in a scalar boolean. See page 57 of the GLSL 1.50 spec. 728 */ 729 assert(type->is_error() 730 || ((type->base_type == GLSL_TYPE_BOOL) 731 && type->is_scalar())); 732 733 result = new(ctx) ir_expression(operations[this->oper], type, 734 op[0], op[1]); 735 error_emitted = type->is_error(); 736 break; 737 738 case ast_nequal: 739 case ast_equal: 740 op[0] = this->subexpressions[0]->hir(instructions, state); 741 op[1] = this->subexpressions[1]->hir(instructions, state); 742 743 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec: 744 * 745 * "The equality operators equal (==), and not equal (!=) 746 * operate on all types. They result in a scalar Boolean. If 747 * the operand types do not match, then there must be a 748 * conversion from Section 4.1.10 "Implicit Conversions" 749 * applied to one operand that can make them match, in which 750 * case this conversion is done." 751 */ 752 if ((!apply_implicit_conversion(op[0]->type, op[1], state) 753 && !apply_implicit_conversion(op[1]->type, op[0], state)) 754 || (op[0]->type != op[1]->type)) { 755 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same " 756 "type", (this->oper == ast_equal) ? "==" : "!="); 757 error_emitted = true; 758 } else if ((state->language_version <= 110) 759 && (op[0]->type->is_array() || op[1]->type->is_array())) { 760 _mesa_glsl_error(& loc, state, "array comparisons forbidden in " 761 "GLSL 1.10"); 762 error_emitted = true; 763 } 764 765 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, 766 op[0], op[1]); 767 type = glsl_type::bool_type; 768 769 assert(result->type == glsl_type::bool_type); 770 break; 771 772 case ast_bit_and: 773 case ast_bit_xor: 774 case ast_bit_or: 775 case ast_bit_not: 776 _mesa_glsl_error(& loc, state, "FINISHME: implement bit-wise operators"); 777 error_emitted = true; 778 break; 779 780 case ast_logic_and: { 781 op[0] = this->subexpressions[0]->hir(instructions, state); 782 783 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) { 784 YYLTYPE loc = this->subexpressions[0]->get_location(); 785 786 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean", 787 operator_string(this->oper)); 788 error_emitted = true; 789 } 790 791 ir_constant *op0_const = op[0]->constant_expression_value(); 792 if (op0_const) { 793 if (op0_const->value.b[0]) { 794 op[1] = this->subexpressions[1]->hir(instructions, state); 795 796 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) { 797 YYLTYPE loc = this->subexpressions[1]->get_location(); 798 799 _mesa_glsl_error(& loc, state, 800 "RHS of `%s' must be scalar boolean", 801 operator_string(this->oper)); 802 error_emitted = true; 803 } 804 result = op[1]; 805 } else { 806 result = op0_const; 807 } 808 type = glsl_type::bool_type; 809 } else { 810 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type, 811 "and_tmp", 812 ir_var_temporary); 813 instructions->push_tail(tmp); 814 815 ir_if *const stmt = new(ctx) ir_if(op[0]); 816 instructions->push_tail(stmt); 817 818 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state); 819 820 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) { 821 YYLTYPE loc = this->subexpressions[1]->get_location(); 822 823 _mesa_glsl_error(& loc, state, 824 "RHS of `%s' must be scalar boolean", 825 operator_string(this->oper)); 826 error_emitted = true; 827 } 828 829 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp); 830 ir_assignment *const then_assign = 831 new(ctx) ir_assignment(then_deref, op[1], NULL); 832 stmt->then_instructions.push_tail(then_assign); 833 834 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp); 835 ir_assignment *const else_assign = 836 new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL); 837 stmt->else_instructions.push_tail(else_assign); 838 839 result = new(ctx) ir_dereference_variable(tmp); 840 type = tmp->type; 841 } 842 break; 843 } 844 845 case ast_logic_or: { 846 op[0] = this->subexpressions[0]->hir(instructions, state); 847 848 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) { 849 YYLTYPE loc = this->subexpressions[0]->get_location(); 850 851 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean", 852 operator_string(this->oper)); 853 error_emitted = true; 854 } 855 856 ir_constant *op0_const = op[0]->constant_expression_value(); 857 if (op0_const) { 858 if (op0_const->value.b[0]) { 859 result = op0_const; 860 } else { 861 op[1] = this->subexpressions[1]->hir(instructions, state); 862 863 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) { 864 YYLTYPE loc = this->subexpressions[1]->get_location(); 865 866 _mesa_glsl_error(& loc, state, 867 "RHS of `%s' must be scalar boolean", 868 operator_string(this->oper)); 869 error_emitted = true; 870 } 871 result = op[1]; 872 } 873 type = glsl_type::bool_type; 874 } else { 875 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type, 876 "or_tmp", 877 ir_var_temporary); 878 instructions->push_tail(tmp); 879 880 ir_if *const stmt = new(ctx) ir_if(op[0]); 881 instructions->push_tail(stmt); 882 883 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state); 884 885 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) { 886 YYLTYPE loc = this->subexpressions[1]->get_location(); 887 888 _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean", 889 operator_string(this->oper)); 890 error_emitted = true; 891 } 892 893 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp); 894 ir_assignment *const then_assign = 895 new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL); 896 stmt->then_instructions.push_tail(then_assign); 897 898 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp); 899 ir_assignment *const else_assign = 900 new(ctx) ir_assignment(else_deref, op[1], NULL); 901 stmt->else_instructions.push_tail(else_assign); 902 903 result = new(ctx) ir_dereference_variable(tmp); 904 type = tmp->type; 905 } 906 break; 907 } 908 909 case ast_logic_xor: 910 op[0] = this->subexpressions[0]->hir(instructions, state); 911 op[1] = this->subexpressions[1]->hir(instructions, state); 912 913 914 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, 915 op[0], op[1]); 916 type = glsl_type::bool_type; 917 break; 918 919 case ast_logic_not: 920 op[0] = this->subexpressions[0]->hir(instructions, state); 921 922 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) { 923 YYLTYPE loc = this->subexpressions[0]->get_location(); 924 925 _mesa_glsl_error(& loc, state, 926 "operand of `!' must be scalar boolean"); 927 error_emitted = true; 928 } 929 930 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, 931 op[0], NULL); 932 type = glsl_type::bool_type; 933 break; 934 935 case ast_mul_assign: 936 case ast_div_assign: 937 case ast_add_assign: 938 case ast_sub_assign: { 939 op[0] = this->subexpressions[0]->hir(instructions, state); 940 op[1] = this->subexpressions[1]->hir(instructions, state); 941 942 type = arithmetic_result_type(op[0], op[1], 943 (this->oper == ast_mul_assign), 944 state, & loc); 945 946 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type, 947 op[0], op[1]); 948 949 result = do_assignment(instructions, state, 950 op[0]->clone(NULL), temp_rhs, 951 this->subexpressions[0]->get_location()); 952 type = result->type; 953 error_emitted = (op[0]->type->is_error()); 954 955 /* GLSL 1.10 does not allow array assignment. However, we don't have to 956 * explicitly test for this because none of the binary expression 957 * operators allow array operands either. 958 */ 959 960 break; 961 } 962 963 case ast_mod_assign: { 964 op[0] = this->subexpressions[0]->hir(instructions, state); 965 op[1] = this->subexpressions[1]->hir(instructions, state); 966 967 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc); 968 969 assert(operations[this->oper] == ir_binop_mod); 970 971 struct ir_rvalue *temp_rhs; 972 temp_rhs = new(ctx) ir_expression(operations[this->oper], type, 973 op[0], op[1]); 974 975 result = do_assignment(instructions, state, 976 op[0]->clone(NULL), temp_rhs, 977 this->subexpressions[0]->get_location()); 978 type = result->type; 979 error_emitted = type->is_error(); 980 break; 981 } 982 983 case ast_ls_assign: 984 case ast_rs_assign: 985 _mesa_glsl_error(& loc, state, 986 "FINISHME: implement bit-shift assignment operators"); 987 error_emitted = true; 988 break; 989 990 case ast_and_assign: 991 case ast_xor_assign: 992 case ast_or_assign: 993 _mesa_glsl_error(& loc, state, 994 "FINISHME: implement logic assignment operators"); 995 error_emitted = true; 996 break; 997 998 case ast_conditional: { 999 op[0] = this->subexpressions[0]->hir(instructions, state); 1000 1001 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec: 1002 * 1003 * "The ternary selection operator (?:). It operates on three 1004 * expressions (exp1 ? exp2 : exp3). This operator evaluates the 1005 * first expression, which must result in a scalar Boolean." 1006 */ 1007 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) { 1008 YYLTYPE loc = this->subexpressions[0]->get_location(); 1009 1010 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean"); 1011 error_emitted = true; 1012 } 1013 1014 /* The :? operator is implemented by generating an anonymous temporary 1015 * followed by an if-statement. The last instruction in each branch of 1016 * the if-statement assigns a value to the anonymous temporary. This 1017 * temporary is the r-value of the expression. 1018 */ 1019 exec_list then_instructions; 1020 exec_list else_instructions; 1021 1022 op[1] = this->subexpressions[1]->hir(&then_instructions, state); 1023 op[2] = this->subexpressions[2]->hir(&else_instructions, state); 1024 1025 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec: 1026 * 1027 * "The second and third expressions can be any type, as 1028 * long their types match, or there is a conversion in 1029 * Section 4.1.10 "Implicit Conversions" that can be applied 1030 * to one of the expressions to make their types match. This 1031 * resulting matching type is the type of the entire 1032 * expression." 1033 */ 1034 if ((!apply_implicit_conversion(op[1]->type, op[2], state) 1035 && !apply_implicit_conversion(op[2]->type, op[1], state)) 1036 || (op[1]->type != op[2]->type)) { 1037 YYLTYPE loc = this->subexpressions[1]->get_location(); 1038 1039 _mesa_glsl_error(& loc, state, "Second and third operands of ?: " 1040 "operator must have matching types."); 1041 error_emitted = true; 1042 type = glsl_type::error_type; 1043 } else { 1044 type = op[1]->type; 1045 } 1046 1047 ir_constant *cond_val = op[0]->constant_expression_value(); 1048 ir_constant *then_val = op[1]->constant_expression_value(); 1049 ir_constant *else_val = op[2]->constant_expression_value(); 1050 1051 if (then_instructions.is_empty() 1052 && else_instructions.is_empty() 1053 && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) { 1054 result = (cond_val->value.b[0]) ? then_val : else_val; 1055 } else { 1056 ir_variable *const tmp = 1057 new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary); 1058 instructions->push_tail(tmp); 1059 1060 ir_if *const stmt = new(ctx) ir_if(op[0]); 1061 instructions->push_tail(stmt); 1062 1063 then_instructions.move_nodes_to(& stmt->then_instructions); 1064 ir_dereference *const then_deref = 1065 new(ctx) ir_dereference_variable(tmp); 1066 ir_assignment *const then_assign = 1067 new(ctx) ir_assignment(then_deref, op[1], NULL); 1068 stmt->then_instructions.push_tail(then_assign); 1069 1070 else_instructions.move_nodes_to(& stmt->else_instructions); 1071 ir_dereference *const else_deref = 1072 new(ctx) ir_dereference_variable(tmp); 1073 ir_assignment *const else_assign = 1074 new(ctx) ir_assignment(else_deref, op[2], NULL); 1075 stmt->else_instructions.push_tail(else_assign); 1076 1077 result = new(ctx) ir_dereference_variable(tmp); 1078 } 1079 break; 1080 } 1081 1082 case ast_pre_inc: 1083 case ast_pre_dec: { 1084 op[0] = this->subexpressions[0]->hir(instructions, state); 1085 if (op[0]->type->base_type == GLSL_TYPE_FLOAT) 1086 op[1] = new(ctx) ir_constant(1.0f); 1087 else 1088 op[1] = new(ctx) ir_constant(1); 1089 1090 type = arithmetic_result_type(op[0], op[1], false, state, & loc); 1091 1092 struct ir_rvalue *temp_rhs; 1093 temp_rhs = new(ctx) ir_expression(operations[this->oper], type, 1094 op[0], op[1]); 1095 1096 result = do_assignment(instructions, state, 1097 op[0]->clone(NULL), temp_rhs, 1098 this->subexpressions[0]->get_location()); 1099 type = result->type; 1100 error_emitted = op[0]->type->is_error(); 1101 break; 1102 } 1103 1104 case ast_post_inc: 1105 case ast_post_dec: { 1106 op[0] = this->subexpressions[0]->hir(instructions, state); 1107 if (op[0]->type->base_type == GLSL_TYPE_FLOAT) 1108 op[1] = new(ctx) ir_constant(1.0f); 1109 else 1110 op[1] = new(ctx) ir_constant(1); 1111 1112 error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); 1113 1114 type = arithmetic_result_type(op[0], op[1], false, state, & loc); 1115 1116 struct ir_rvalue *temp_rhs; 1117 temp_rhs = new(ctx) ir_expression(operations[this->oper], type, 1118 op[0], op[1]); 1119 1120 /* Get a temporary of a copy of the lvalue before it's modified. 1121 * This may get thrown away later. 1122 */ 1123 result = get_lvalue_copy(instructions, op[0]->clone(NULL)); 1124 1125 (void)do_assignment(instructions, state, 1126 op[0]->clone(NULL), temp_rhs, 1127 this->subexpressions[0]->get_location()); 1128 1129 type = result->type; 1130 error_emitted = op[0]->type->is_error(); 1131 break; 1132 } 1133 1134 case ast_field_selection: 1135 result = _mesa_ast_field_selection_to_hir(this, instructions, state); 1136 type = result->type; 1137 break; 1138 1139 case ast_array_index: { 1140 YYLTYPE index_loc = subexpressions[1]->get_location(); 1141 1142 op[0] = subexpressions[0]->hir(instructions, state); 1143 op[1] = subexpressions[1]->hir(instructions, state); 1144 1145 error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); 1146 1147 ir_rvalue *const array = op[0]; 1148 1149 result = new(ctx) ir_dereference_array(op[0], op[1]); 1150 1151 /* Do not use op[0] after this point. Use array. 1152 */ 1153 op[0] = NULL; 1154 1155 1156 if (error_emitted) 1157 break; 1158 1159 if (!array->type->is_array() 1160 && !array->type->is_matrix() 1161 && !array->type->is_vector()) { 1162 _mesa_glsl_error(& index_loc, state, 1163 "cannot dereference non-array / non-matrix / " 1164 "non-vector"); 1165 error_emitted = true; 1166 } 1167 1168 if (!op[1]->type->is_integer()) { 1169 _mesa_glsl_error(& index_loc, state, 1170 "array index must be integer type"); 1171 error_emitted = true; 1172 } else if (!op[1]->type->is_scalar()) { 1173 _mesa_glsl_error(& index_loc, state, 1174 "array index must be scalar"); 1175 error_emitted = true; 1176 } 1177 1178 /* If the array index is a constant expression and the array has a 1179 * declared size, ensure that the access is in-bounds. If the array 1180 * index is not a constant expression, ensure that the array has a 1181 * declared size. 1182 */ 1183 ir_constant *const const_index = op[1]->constant_expression_value(); 1184 if (const_index != NULL) { 1185 const int idx = const_index->value.i[0]; 1186 const char *type_name; 1187 unsigned bound = 0; 1188 1189 if (array->type->is_matrix()) { 1190 type_name = "matrix"; 1191 } else if (array->type->is_vector()) { 1192 type_name = "vector"; 1193 } else { 1194 type_name = "array"; 1195 } 1196 1197 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec: 1198 * 1199 * "It is illegal to declare an array with a size, and then 1200 * later (in the same shader) index the same array with an 1201 * integral constant expression greater than or equal to the 1202 * declared size. It is also illegal to index an array with a 1203 * negative constant expression." 1204 */ 1205 if (array->type->is_matrix()) { 1206 if (array->type->row_type()->vector_elements <= idx) { 1207 bound = array->type->row_type()->vector_elements; 1208 } 1209 } else if (array->type->is_vector()) { 1210 if (array->type->vector_elements <= idx) { 1211 bound = array->type->vector_elements; 1212 } 1213 } else { 1214 if ((array->type->array_size() > 0) 1215 && (array->type->array_size() <= idx)) { 1216 bound = array->type->array_size(); 1217 } 1218 } 1219 1220 if (bound > 0) { 1221 _mesa_glsl_error(& loc, state, "%s index must be < %u", 1222 type_name, bound); 1223 error_emitted = true; 1224 } else if (idx < 0) { 1225 _mesa_glsl_error(& loc, state, "%s index must be >= 0", 1226 type_name); 1227 error_emitted = true; 1228 } 1229 1230 if (array->type->is_array()) { 1231 /* If the array is a variable dereference, it dereferences the 1232 * whole array, by definition. Use this to get the variable. 1233 * 1234 * FINISHME: Should some methods for getting / setting / testing 1235 * FINISHME: array access limits be added to ir_dereference? 1236 */ 1237 ir_variable *const v = array->whole_variable_referenced(); 1238 if ((v != NULL) && (unsigned(idx) > v->max_array_access)) 1239 v->max_array_access = idx; 1240 } 1241 } else if (array->type->array_size() == 0) { 1242 _mesa_glsl_error(&loc, state, "unsized array index must be constant"); 1243 } 1244 1245 if (error_emitted) 1246 result->type = glsl_type::error_type; 1247 1248 type = result->type; 1249 break; 1250 } 1251 1252 case ast_function_call: 1253 /* Should *NEVER* get here. ast_function_call should always be handled 1254 * by ast_function_expression::hir. 1255 */ 1256 assert(0); 1257 break; 1258 1259 case ast_identifier: { 1260 /* ast_identifier can appear several places in a full abstract syntax 1261 * tree. This particular use must be at location specified in the grammar 1262 * as 'variable_identifier'. 1263 */ 1264 ir_variable *var = 1265 state->symbols->get_variable(this->primary_expression.identifier); 1266 1267 result = new(ctx) ir_dereference_variable(var); 1268 1269 if (var != NULL) { 1270 type = result->type; 1271 } else { 1272 _mesa_glsl_error(& loc, state, "`%s' undeclared", 1273 this->primary_expression.identifier); 1274 1275 error_emitted = true; 1276 } 1277 break; 1278 } 1279 1280 case ast_int_constant: 1281 type = glsl_type::int_type; 1282 result = new(ctx) ir_constant(this->primary_expression.int_constant); 1283 break; 1284 1285 case ast_uint_constant: 1286 type = glsl_type::uint_type; 1287 result = new(ctx) ir_constant(this->primary_expression.uint_constant); 1288 break; 1289 1290 case ast_float_constant: 1291 type = glsl_type::float_type; 1292 result = new(ctx) ir_constant(this->primary_expression.float_constant); 1293 break; 1294 1295 case ast_bool_constant: 1296 type = glsl_type::bool_type; 1297 result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant)); 1298 break; 1299 1300 case ast_sequence: { 1301 /* It should not be possible to generate a sequence in the AST without 1302 * any expressions in it. 1303 */ 1304 assert(!this->expressions.is_empty()); 1305 1306 /* The r-value of a sequence is the last expression in the sequence. If 1307 * the other expressions in the sequence do not have side-effects (and 1308 * therefore add instructions to the instruction list), they get dropped 1309 * on the floor. 1310 */ 1311 foreach_list_typed (ast_node, ast, link, &this->expressions) 1312 result = ast->hir(instructions, state); 1313 1314 type = result->type; 1315 1316 /* Any errors should have already been emitted in the loop above. 1317 */ 1318 error_emitted = true; 1319 break; 1320 } 1321 } 1322 1323 if (type->is_error() && !error_emitted) 1324 _mesa_glsl_error(& loc, state, "type mismatch"); 1325 1326 return result; 1327} 1328 1329 1330ir_rvalue * 1331ast_expression_statement::hir(exec_list *instructions, 1332 struct _mesa_glsl_parse_state *state) 1333{ 1334 /* It is possible to have expression statements that don't have an 1335 * expression. This is the solitary semicolon: 1336 * 1337 * for (i = 0; i < 5; i++) 1338 * ; 1339 * 1340 * In this case the expression will be NULL. Test for NULL and don't do 1341 * anything in that case. 1342 */ 1343 if (expression != NULL) 1344 expression->hir(instructions, state); 1345 1346 /* Statements do not have r-values. 1347 */ 1348 return NULL; 1349} 1350 1351 1352ir_rvalue * 1353ast_compound_statement::hir(exec_list *instructions, 1354 struct _mesa_glsl_parse_state *state) 1355{ 1356 if (new_scope) 1357 state->symbols->push_scope(); 1358 1359 foreach_list_typed (ast_node, ast, link, &this->statements) 1360 ast->hir(instructions, state); 1361 1362 if (new_scope) 1363 state->symbols->pop_scope(); 1364 1365 /* Compound statements do not have r-values. 1366 */ 1367 return NULL; 1368} 1369 1370 1371static const glsl_type * 1372process_array_type(const glsl_type *base, ast_node *array_size, 1373 struct _mesa_glsl_parse_state *state) 1374{ 1375 unsigned length = 0; 1376 1377 /* FINISHME: Reject delcarations of multidimensional arrays. */ 1378 1379 if (array_size != NULL) { 1380 exec_list dummy_instructions; 1381 ir_rvalue *const ir = array_size->hir(& dummy_instructions, state); 1382 YYLTYPE loc = array_size->get_location(); 1383 1384 /* FINISHME: Verify that the grammar forbids side-effects in array 1385 * FINISHME: sizes. i.e., 'vec4 [x = 12] data' 1386 */ 1387 assert(dummy_instructions.is_empty()); 1388 1389 if (ir != NULL) { 1390 if (!ir->type->is_integer()) { 1391 _mesa_glsl_error(& loc, state, "array size must be integer type"); 1392 } else if (!ir->type->is_scalar()) { 1393 _mesa_glsl_error(& loc, state, "array size must be scalar type"); 1394 } else { 1395 ir_constant *const size = ir->constant_expression_value(); 1396 1397 if (size == NULL) { 1398 _mesa_glsl_error(& loc, state, "array size must be a " 1399 "constant valued expression"); 1400 } else if (size->value.i[0] <= 0) { 1401 _mesa_glsl_error(& loc, state, "array size must be > 0"); 1402 } else { 1403 assert(size->type == ir->type); 1404 length = size->value.u[0]; 1405 } 1406 } 1407 } 1408 } 1409 1410 return glsl_type::get_array_instance(state, base, length); 1411} 1412 1413 1414const glsl_type * 1415ast_type_specifier::glsl_type(const char **name, 1416 struct _mesa_glsl_parse_state *state) const 1417{ 1418 const struct glsl_type *type; 1419 1420 if ((this->type_specifier == ast_struct) && (this->type_name == NULL)) { 1421 /* FINISHME: Handle annonymous structures. */ 1422 type = NULL; 1423 } else { 1424 type = state->symbols->get_type(this->type_name); 1425 *name = this->type_name; 1426 1427 if (this->is_array) { 1428 type = process_array_type(type, this->array_size, state); 1429 } 1430 } 1431 1432 return type; 1433} 1434 1435 1436static void 1437apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual, 1438 struct ir_variable *var, 1439 struct _mesa_glsl_parse_state *state, 1440 YYLTYPE *loc) 1441{ 1442 if (qual->invariant) 1443 var->invariant = 1; 1444 1445 /* FINISHME: Mark 'in' variables at global scope as read-only. */ 1446 if (qual->constant || qual->attribute || qual->uniform 1447 || (qual->varying && (state->target == fragment_shader))) 1448 var->read_only = 1; 1449 1450 if (qual->centroid) 1451 var->centroid = 1; 1452 1453 if (qual->attribute && state->target != vertex_shader) { 1454 var->type = glsl_type::error_type; 1455 _mesa_glsl_error(loc, state, 1456 "`attribute' variables may not be declared in the " 1457 "%s shader", 1458 _mesa_glsl_shader_target_name(state->target)); 1459 } 1460 1461 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec: 1462 * 1463 * "The varying qualifier can be used only with the data types 1464 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of 1465 * these." 1466 */ 1467 if (qual->varying) { 1468 const glsl_type *non_array_type; 1469 1470 if (var->type && var->type->is_array()) 1471 non_array_type = var->type->fields.array; 1472 else 1473 non_array_type = var->type; 1474 1475 if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) { 1476 var->type = glsl_type::error_type; 1477 _mesa_glsl_error(loc, state, 1478 "varying variables must be of base type float"); 1479 } 1480 } 1481 1482 /* If there is no qualifier that changes the mode of the variable, leave 1483 * the setting alone. 1484 */ 1485 if (qual->in && qual->out) 1486 var->mode = ir_var_inout; 1487 else if (qual->attribute || qual->in 1488 || (qual->varying && (state->target == fragment_shader))) 1489 var->mode = ir_var_in; 1490 else if (qual->out || (qual->varying && (state->target == vertex_shader))) 1491 var->mode = ir_var_out; 1492 else if (qual->uniform) 1493 var->mode = ir_var_uniform; 1494 1495 if (qual->uniform) 1496 var->shader_in = true; 1497 1498 /* Any 'in' or 'inout' variables at global scope must be marked as being 1499 * shader inputs. Likewise, any 'out' or 'inout' variables at global scope 1500 * must be marked as being shader outputs. 1501 */ 1502 if (state->current_function == NULL) { 1503 switch (var->mode) { 1504 case ir_var_in: 1505 case ir_var_uniform: 1506 var->shader_in = true; 1507 break; 1508 case ir_var_out: 1509 var->shader_out = true; 1510 break; 1511 case ir_var_inout: 1512 var->shader_in = true; 1513 var->shader_out = true; 1514 break; 1515 default: 1516 break; 1517 } 1518 } 1519 1520 if (qual->flat) 1521 var->interpolation = ir_var_flat; 1522 else if (qual->noperspective) 1523 var->interpolation = ir_var_noperspective; 1524 else 1525 var->interpolation = ir_var_smooth; 1526 1527 if (var->type->is_array() && (state->language_version >= 120)) { 1528 var->array_lvalue = true; 1529 } 1530} 1531 1532 1533ir_rvalue * 1534ast_declarator_list::hir(exec_list *instructions, 1535 struct _mesa_glsl_parse_state *state) 1536{ 1537 void *ctx = state; 1538 const struct glsl_type *decl_type; 1539 const char *type_name = NULL; 1540 ir_rvalue *result = NULL; 1541 YYLTYPE loc = this->get_location(); 1542 1543 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec: 1544 * 1545 * "To ensure that a particular output variable is invariant, it is 1546 * necessary to use the invariant qualifier. It can either be used to 1547 * qualify a previously declared variable as being invariant 1548 * 1549 * invariant gl_Position; // make existing gl_Position be invariant" 1550 * 1551 * In these cases the parser will set the 'invariant' flag in the declarator 1552 * list, and the type will be NULL. 1553 */ 1554 if (this->invariant) { 1555 assert(this->type == NULL); 1556 1557 if (state->current_function != NULL) { 1558 _mesa_glsl_error(& loc, state, 1559 "All uses of `invariant' keyword must be at global " 1560 "scope\n"); 1561 } 1562 1563 foreach_list_typed (ast_declaration, decl, link, &this->declarations) { 1564 assert(!decl->is_array); 1565 assert(decl->array_size == NULL); 1566 assert(decl->initializer == NULL); 1567 1568 ir_variable *const earlier = 1569 state->symbols->get_variable(decl->identifier); 1570 if (earlier == NULL) { 1571 _mesa_glsl_error(& loc, state, 1572 "Undeclared variable `%s' cannot be marked " 1573 "invariant\n", decl->identifier); 1574 } else if ((state->target == vertex_shader) 1575 && (earlier->mode != ir_var_out)) { 1576 _mesa_glsl_error(& loc, state, 1577 "`%s' cannot be marked invariant, vertex shader " 1578 "outputs only\n", decl->identifier); 1579 } else if ((state->target == fragment_shader) 1580 && (earlier->mode != ir_var_in)) { 1581 _mesa_glsl_error(& loc, state, 1582 "`%s' cannot be marked invariant, fragment shader " 1583 "inputs only\n", decl->identifier); 1584 } else { 1585 earlier->invariant = true; 1586 } 1587 } 1588 1589 /* Invariant redeclarations do not have r-values. 1590 */ 1591 return NULL; 1592 } 1593 1594 assert(this->type != NULL); 1595 assert(!this->invariant); 1596 1597 /* The type specifier may contain a structure definition. Process that 1598 * before any of the variable declarations. 1599 */ 1600 (void) this->type->specifier->hir(instructions, state); 1601 1602 decl_type = this->type->specifier->glsl_type(& type_name, state); 1603 if (this->declarations.is_empty()) { 1604 /* The only valid case where the declaration list can be empty is when 1605 * the declaration is setting the default precision of a built-in type 1606 * (e.g., 'precision highp vec4;'). 1607 */ 1608 1609 if (decl_type != NULL) { 1610 } else { 1611 _mesa_glsl_error(& loc, state, "incomplete declaration"); 1612 } 1613 } 1614 1615 foreach_list_typed (ast_declaration, decl, link, &this->declarations) { 1616 const struct glsl_type *var_type; 1617 struct ir_variable *var; 1618 1619 /* FINISHME: Emit a warning if a variable declaration shadows a 1620 * FINISHME: declaration at a higher scope. 1621 */ 1622 1623 if ((decl_type == NULL) || decl_type->is_void()) { 1624 if (type_name != NULL) { 1625 _mesa_glsl_error(& loc, state, 1626 "invalid type `%s' in declaration of `%s'", 1627 type_name, decl->identifier); 1628 } else { 1629 _mesa_glsl_error(& loc, state, 1630 "invalid type in declaration of `%s'", 1631 decl->identifier); 1632 } 1633 continue; 1634 } 1635 1636 if (decl->is_array) { 1637 var_type = process_array_type(decl_type, decl->array_size, state); 1638 } else { 1639 var_type = decl_type; 1640 } 1641 1642 var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto); 1643 1644 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification; 1645 * 1646 * "Global variables can only use the qualifiers const, 1647 * attribute, uni form, or varying. Only one may be 1648 * specified. 1649 * 1650 * Local variables can only use the qualifier const." 1651 * 1652 * This is relaxed in GLSL 1.30. 1653 */ 1654 if (state->language_version < 120) { 1655 if (this->type->qualifier.out) { 1656 _mesa_glsl_error(& loc, state, 1657 "`out' qualifier in declaration of `%s' " 1658 "only valid for function parameters in GLSL 1.10.", 1659 decl->identifier); 1660 } 1661 if (this->type->qualifier.in) { 1662 _mesa_glsl_error(& loc, state, 1663 "`in' qualifier in declaration of `%s' " 1664 "only valid for function parameters in GLSL 1.10.", 1665 decl->identifier); 1666 } 1667 /* FINISHME: Test for other invalid qualifiers. */ 1668 } 1669 1670 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, 1671 & loc); 1672 1673 if (this->type->qualifier.invariant) { 1674 if ((state->target == vertex_shader) && !var->shader_out) { 1675 _mesa_glsl_error(& loc, state, 1676 "`%s' cannot be marked invariant, vertex shader " 1677 "outputs only\n", var->name); 1678 } else if ((state->target == fragment_shader) && !var->shader_in) { 1679 _mesa_glsl_error(& loc, state, 1680 "`%s' cannot be marked invariant, fragment shader " 1681 "inputs only\n", var->name); 1682 } 1683 } 1684 1685 if (state->current_function != NULL) { 1686 const char *mode = NULL; 1687 const char *extra = ""; 1688 1689 /* There is no need to check for 'inout' here because the parser will 1690 * only allow that in function parameter lists. 1691 */ 1692 if (this->type->qualifier.attribute) { 1693 mode = "attribute"; 1694 } else if (this->type->qualifier.uniform) { 1695 mode = "uniform"; 1696 } else if (this->type->qualifier.varying) { 1697 mode = "varying"; 1698 } else if (this->type->qualifier.in) { 1699 mode = "in"; 1700 extra = " or in function parameter list"; 1701 } else if (this->type->qualifier.out) { 1702 mode = "out"; 1703 extra = " or in function parameter list"; 1704 } 1705 1706 if (mode) { 1707 _mesa_glsl_error(& loc, state, 1708 "%s variable `%s' must be declared at " 1709 "global scope%s", 1710 mode, var->name, extra); 1711 } 1712 } else if (var->mode == ir_var_in) { 1713 if (state->target == vertex_shader) { 1714 bool error_emitted = false; 1715 1716 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec: 1717 * 1718 * "Vertex shader inputs can only be float, floating-point 1719 * vectors, matrices, signed and unsigned integers and integer 1720 * vectors. Vertex shader inputs can also form arrays of these 1721 * types, but not structures." 1722 * 1723 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec: 1724 * 1725 * "Vertex shader inputs can only be float, floating-point 1726 * vectors, matrices, signed and unsigned integers and integer 1727 * vectors. They cannot be arrays or structures." 1728 * 1729 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec: 1730 * 1731 * "The attribute qualifier can be used only with float, 1732 * floating-point vectors, and matrices. Attribute variables 1733 * cannot be declared as arrays or structures." 1734 */ 1735 const glsl_type *check_type = var->type->is_array() 1736 ? var->type->fields.array : var->type; 1737 1738 switch (check_type->base_type) { 1739 case GLSL_TYPE_FLOAT: 1740 break; 1741 case GLSL_TYPE_UINT: 1742 case GLSL_TYPE_INT: 1743 if (state->language_version > 120) 1744 break; 1745 /* FALLTHROUGH */ 1746 default: 1747 _mesa_glsl_error(& loc, state, 1748 "vertex shader input / attribute cannot have " 1749 "type %s`%s'", 1750 var->type->is_array() ? "array of " : "", 1751 check_type->name); 1752 error_emitted = true; 1753 } 1754 1755 if (!error_emitted && (state->language_version <= 130) 1756 && var->type->is_array()) { 1757 _mesa_glsl_error(& loc, state, 1758 "vertex shader input / attribute cannot have " 1759 "array type"); 1760 error_emitted = true; 1761 } 1762 } 1763 } 1764 1765 /* Process the initializer and add its instructions to a temporary 1766 * list. This list will be added to the instruction stream (below) after 1767 * the declaration is added. This is done because in some cases (such as 1768 * redeclarations) the declaration may not actually be added to the 1769 * instruction stream. 1770 */ 1771 exec_list intializer_instructions; 1772 if (decl->initializer != NULL) { 1773 YYLTYPE initializer_loc = decl->initializer->get_location(); 1774 1775 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec: 1776 * 1777 * "All uniform variables are read-only and are initialized either 1778 * directly by an application via API commands, or indirectly by 1779 * OpenGL." 1780 */ 1781 if ((state->language_version <= 110) 1782 && (var->mode == ir_var_uniform)) { 1783 _mesa_glsl_error(& initializer_loc, state, 1784 "cannot initialize uniforms in GLSL 1.10"); 1785 } 1786 1787 if (var->type->is_sampler()) { 1788 _mesa_glsl_error(& initializer_loc, state, 1789 "cannot initialize samplers"); 1790 } 1791 1792 if ((var->mode == ir_var_in) && (state->current_function == NULL)) { 1793 _mesa_glsl_error(& initializer_loc, state, 1794 "cannot initialize %s shader input / %s", 1795 _mesa_glsl_shader_target_name(state->target), 1796 (state->target == vertex_shader) 1797 ? "attribute" : "varying"); 1798 } 1799 1800 ir_dereference *const lhs = new(ctx) ir_dereference_variable(var); 1801 ir_rvalue *rhs = decl->initializer->hir(&intializer_instructions, 1802 state); 1803 1804 /* Calculate the constant value if this is a const or uniform 1805 * declaration. 1806 */ 1807 if (this->type->qualifier.constant || this->type->qualifier.uniform) { 1808 ir_constant *constant_value = rhs->constant_expression_value(); 1809 if (!constant_value) { 1810 _mesa_glsl_error(& initializer_loc, state, 1811 "initializer of %s variable `%s' must be a " 1812 "constant expression", 1813 (this->type->qualifier.constant) 1814 ? "const" : "uniform", 1815 decl->identifier); 1816 } else { 1817 rhs = constant_value; 1818 var->constant_value = constant_value; 1819 } 1820 } 1821 1822 if (rhs && !rhs->type->is_error()) { 1823 bool temp = var->read_only; 1824 if (this->type->qualifier.constant) 1825 var->read_only = false; 1826 1827 /* Never emit code to initialize a uniform. 1828 */ 1829 if (!this->type->qualifier.uniform) 1830 result = do_assignment(&intializer_instructions, state, lhs, rhs, 1831 this->get_location()); 1832 var->read_only = temp; 1833 } 1834 } 1835 1836 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec: 1837 * 1838 * "It is an error to write to a const variable outside of 1839 * its declaration, so they must be initialized when 1840 * declared." 1841 */ 1842 if (this->type->qualifier.constant && decl->initializer == NULL) { 1843 _mesa_glsl_error(& loc, state, 1844 "const declaration of `%s' must be initialized"); 1845 } 1846 1847 /* Attempt to add the variable to the symbol table. If this fails, it 1848 * means the variable has already been declared at this scope. Arrays 1849 * fudge this rule a little bit. 1850 * 1851 * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec, 1852 * 1853 * "It is legal to declare an array without a size and then 1854 * later re-declare the same name as an array of the same 1855 * type and specify a size." 1856 */ 1857 if (state->symbols->name_declared_this_scope(decl->identifier)) { 1858 ir_variable *const earlier = 1859 state->symbols->get_variable(decl->identifier); 1860 1861 if ((earlier != NULL) 1862 && (earlier->type->array_size() == 0) 1863 && var->type->is_array() 1864 && (var->type->element_type() == earlier->type->element_type())) { 1865 /* FINISHME: This doesn't match the qualifiers on the two 1866 * FINISHME: declarations. It's not 100% clear whether this is 1867 * FINISHME: required or not. 1868 */ 1869 1870 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec: 1871 * 1872 * "The size [of gl_TexCoord] can be at most 1873 * gl_MaxTextureCoords." 1874 */ 1875 const unsigned size = unsigned(var->type->array_size()); 1876 if ((strcmp("gl_TexCoord", var->name) == 0) 1877 && (size > state->Const.MaxTextureCoords)) { 1878 YYLTYPE loc = this->get_location(); 1879 1880 _mesa_glsl_error(& loc, state, "`gl_TexCoord' array size cannot " 1881 "be larger than gl_MaxTextureCoords (%u)\n", 1882 state->Const.MaxTextureCoords); 1883 } else if ((size > 0) && (size <= earlier->max_array_access)) { 1884 YYLTYPE loc = this->get_location(); 1885 1886 _mesa_glsl_error(& loc, state, "array size must be > %u due to " 1887 "previous access", 1888 earlier->max_array_access); 1889 } 1890 1891 earlier->type = var->type; 1892 delete var; 1893 var = NULL; 1894 } else { 1895 YYLTYPE loc = this->get_location(); 1896 1897 _mesa_glsl_error(& loc, state, "`%s' redeclared", 1898 decl->identifier); 1899 } 1900 1901 continue; 1902 } 1903 1904 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec, 1905 * 1906 * "Identifiers starting with "gl_" are reserved for use by 1907 * OpenGL, and may not be declared in a shader as either a 1908 * variable or a function." 1909 */ 1910 if (strncmp(decl->identifier, "gl_", 3) == 0) { 1911 /* FINISHME: This should only trigger if we're not redefining 1912 * FINISHME: a builtin (to add a qualifier, for example). 1913 */ 1914 _mesa_glsl_error(& loc, state, 1915 "identifier `%s' uses reserved `gl_' prefix", 1916 decl->identifier); 1917 } 1918 1919 instructions->push_tail(var); 1920 instructions->append_list(&intializer_instructions); 1921 1922 /* Add the variable to the symbol table after processing the initializer. 1923 * This differs from most C-like languages, but it follows the GLSL 1924 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50 1925 * spec: 1926 * 1927 * "Within a declaration, the scope of a name starts immediately 1928 * after the initializer if present or immediately after the name 1929 * being declared if not." 1930 */ 1931 const bool added_variable = 1932 state->symbols->add_variable(var->name, var); 1933 assert(added_variable); 1934 } 1935 1936 1937 /* Generally, variable declarations do not have r-values. However, 1938 * one is used for the declaration in 1939 * 1940 * while (bool b = some_condition()) { 1941 * ... 1942 * } 1943 * 1944 * so we return the rvalue from the last seen declaration here. 1945 */ 1946 return result; 1947} 1948 1949 1950ir_rvalue * 1951ast_parameter_declarator::hir(exec_list *instructions, 1952 struct _mesa_glsl_parse_state *state) 1953{ 1954 void *ctx = state; 1955 const struct glsl_type *type; 1956 const char *name = NULL; 1957 YYLTYPE loc = this->get_location(); 1958 1959 type = this->type->specifier->glsl_type(& name, state); 1960 1961 if (type == NULL) { 1962 if (name != NULL) { 1963 _mesa_glsl_error(& loc, state, 1964 "invalid type `%s' in declaration of `%s'", 1965 name, this->identifier); 1966 } else { 1967 _mesa_glsl_error(& loc, state, 1968 "invalid type in declaration of `%s'", 1969 this->identifier); 1970 } 1971 1972 type = glsl_type::error_type; 1973 } 1974 1975 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec: 1976 * 1977 * "Functions that accept no input arguments need not use void in the 1978 * argument list because prototypes (or definitions) are required and 1979 * therefore there is no ambiguity when an empty argument list "( )" is 1980 * declared. The idiom "(void)" as a parameter list is provided for 1981 * convenience." 1982 * 1983 * Placing this check here prevents a void parameter being set up 1984 * for a function, which avoids tripping up checks for main taking 1985 * parameters and lookups of an unnamed symbol. 1986 */ 1987 if (type->is_void()) { 1988 if (this->identifier != NULL) 1989 _mesa_glsl_error(& loc, state, 1990 "named parameter cannot have type `void'"); 1991 1992 is_void = true; 1993 return NULL; 1994 } 1995 1996 if (formal_parameter && (this->identifier == NULL)) { 1997 _mesa_glsl_error(& loc, state, "formal parameter lacks a name"); 1998 return NULL; 1999 } 2000 2001 is_void = false; 2002 ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in); 2003 2004 /* FINISHME: Handle array declarations. Note that this requires 2005 * FINISHME: complete handling of constant expressions. 2006 */ 2007 2008 /* Apply any specified qualifiers to the parameter declaration. Note that 2009 * for function parameters the default mode is 'in'. 2010 */ 2011 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc); 2012 2013 instructions->push_tail(var); 2014 2015 /* Parameter declarations do not have r-values. 2016 */ 2017 return NULL; 2018} 2019 2020 2021void 2022ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters, 2023 bool formal, 2024 exec_list *ir_parameters, 2025 _mesa_glsl_parse_state *state) 2026{ 2027 ast_parameter_declarator *void_param = NULL; 2028 unsigned count = 0; 2029 2030 foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) { 2031 param->formal_parameter = formal; 2032 param->hir(ir_parameters, state); 2033 2034 if (param->is_void) 2035 void_param = param; 2036 2037 count++; 2038 } 2039 2040 if ((void_param != NULL) && (count > 1)) { 2041 YYLTYPE loc = void_param->get_location(); 2042 2043 _mesa_glsl_error(& loc, state, 2044 "`void' parameter must be only parameter"); 2045 } 2046} 2047 2048 2049ir_rvalue * 2050ast_function::hir(exec_list *instructions, 2051 struct _mesa_glsl_parse_state *state) 2052{ 2053 void *ctx = state; 2054 ir_function *f = NULL; 2055 ir_function_signature *sig = NULL; 2056 exec_list hir_parameters; 2057 2058 const char *const name = identifier; 2059 2060 /* Convert the list of function parameters to HIR now so that they can be 2061 * used below to compare this function's signature with previously seen 2062 * signatures for functions with the same name. 2063 */ 2064 ast_parameter_declarator::parameters_to_hir(& this->parameters, 2065 is_definition, 2066 & hir_parameters, state); 2067 2068 const char *return_type_name; 2069 const glsl_type *return_type = 2070 this->return_type->specifier->glsl_type(& return_type_name, state); 2071 2072 assert(return_type != NULL); 2073 2074 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec: 2075 * "No qualifier is allowed on the return type of a function." 2076 */ 2077 if (this->return_type->has_qualifiers()) { 2078 YYLTYPE loc = this->get_location(); 2079 _mesa_glsl_error(& loc, state, 2080 "function `%s' return type has qualifiers", name); 2081 } 2082 2083 /* Verify that this function's signature either doesn't match a previously 2084 * seen signature for a function with the same name, or, if a match is found, 2085 * that the previously seen signature does not have an associated definition. 2086 */ 2087 f = state->symbols->get_function(name); 2088 if (f != NULL) { 2089 ir_function_signature *sig = f->exact_matching_signature(&hir_parameters); 2090 if (sig != NULL) { 2091 const char *badvar = sig->qualifiers_match(&hir_parameters); 2092 if (badvar != NULL) { 2093 YYLTYPE loc = this->get_location(); 2094 2095 _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' " 2096 "qualifiers don't match prototype", name, badvar); 2097 } 2098 2099 if (sig->return_type != return_type) { 2100 YYLTYPE loc = this->get_location(); 2101 2102 _mesa_glsl_error(&loc, state, "function `%s' return type doesn't " 2103 "match prototype", name); 2104 } 2105 2106 if (is_definition && sig->is_defined) { 2107 YYLTYPE loc = this->get_location(); 2108 2109 _mesa_glsl_error(& loc, state, "function `%s' redefined", name); 2110 sig = NULL; 2111 } 2112 } 2113 } else if (state->symbols->name_declared_this_scope(name)) { 2114 /* This function name shadows a non-function use of the same name. 2115 */ 2116 YYLTYPE loc = this->get_location(); 2117 2118 _mesa_glsl_error(& loc, state, "function name `%s' conflicts with " 2119 "non-function", name); 2120 sig = NULL; 2121 } else { 2122 f = new(ctx) ir_function(name); 2123 state->symbols->add_function(f->name, f); 2124 2125 /* Emit the new function header */ 2126 instructions->push_tail(f); 2127 } 2128 2129 /* Verify the return type of main() */ 2130 if (strcmp(name, "main") == 0) { 2131 if (! return_type->is_void()) { 2132 YYLTYPE loc = this->get_location(); 2133 2134 _mesa_glsl_error(& loc, state, "main() must return void"); 2135 } 2136 2137 if (!hir_parameters.is_empty()) { 2138 YYLTYPE loc = this->get_location(); 2139 2140 _mesa_glsl_error(& loc, state, "main() must not take any parameters"); 2141 } 2142 } 2143 2144 /* Finish storing the information about this new function in its signature. 2145 */ 2146 if (sig == NULL) { 2147 sig = new(ctx) ir_function_signature(return_type); 2148 f->add_signature(sig); 2149 } 2150 2151 sig->replace_parameters(&hir_parameters); 2152 signature = sig; 2153 2154 /* Function declarations (prototypes) do not have r-values. 2155 */ 2156 return NULL; 2157} 2158 2159 2160ir_rvalue * 2161ast_function_definition::hir(exec_list *instructions, 2162 struct _mesa_glsl_parse_state *state) 2163{ 2164 prototype->is_definition = true; 2165 prototype->hir(instructions, state); 2166 2167 ir_function_signature *signature = prototype->signature; 2168 2169 assert(state->current_function == NULL); 2170 state->current_function = signature; 2171 state->found_return = false; 2172 2173 /* Duplicate parameters declared in the prototype as concrete variables. 2174 * Add these to the symbol table. 2175 */ 2176 state->symbols->push_scope(); 2177 foreach_iter(exec_list_iterator, iter, signature->parameters) { 2178 ir_variable *const var = ((ir_instruction *) iter.get())->as_variable(); 2179 2180 assert(var != NULL); 2181 2182 /* The only way a parameter would "exist" is if two parameters have 2183 * the same name. 2184 */ 2185 if (state->symbols->name_declared_this_scope(var->name)) { 2186 YYLTYPE loc = this->get_location(); 2187 2188 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name); 2189 } else { 2190 state->symbols->add_variable(var->name, var); 2191 } 2192 } 2193 2194 /* Convert the body of the function to HIR. */ 2195 this->body->hir(&signature->body, state); 2196 signature->is_defined = true; 2197 2198 state->symbols->pop_scope(); 2199 2200 assert(state->current_function == signature); 2201 state->current_function = NULL; 2202 2203 if (!signature->return_type->is_void() && !state->found_return) { 2204 YYLTYPE loc = this->get_location(); 2205 _mesa_glsl_error(& loc, state, "function `%s' has non-void return type " 2206 "%s, but no return statement", 2207 signature->function_name(), 2208 signature->return_type->name); 2209 } 2210 2211 /* Function definitions do not have r-values. 2212 */ 2213 return NULL; 2214} 2215 2216 2217ir_rvalue * 2218ast_jump_statement::hir(exec_list *instructions, 2219 struct _mesa_glsl_parse_state *state) 2220{ 2221 void *ctx = state; 2222 2223 switch (mode) { 2224 case ast_return: { 2225 ir_return *inst; 2226 assert(state->current_function); 2227 2228 if (opt_return_value) { 2229 if (state->current_function->return_type->base_type == 2230 GLSL_TYPE_VOID) { 2231 YYLTYPE loc = this->get_location(); 2232 2233 _mesa_glsl_error(& loc, state, 2234 "`return` with a value, in function `%s' " 2235 "returning void", 2236 state->current_function->function_name()); 2237 } 2238 2239 ir_expression *const ret = (ir_expression *) 2240 opt_return_value->hir(instructions, state); 2241 assert(ret != NULL); 2242 2243 /* Implicit conversions are not allowed for return values. */ 2244 if (state->current_function->return_type != ret->type) { 2245 YYLTYPE loc = this->get_location(); 2246 2247 _mesa_glsl_error(& loc, state, 2248 "`return' with wrong type %s, in function `%s' " 2249 "returning %s", 2250 ret->type->name, 2251 state->current_function->function_name(), 2252 state->current_function->return_type->name); 2253 } 2254 2255 inst = new(ctx) ir_return(ret); 2256 } else { 2257 if (state->current_function->return_type->base_type != 2258 GLSL_TYPE_VOID) { 2259 YYLTYPE loc = this->get_location(); 2260 2261 _mesa_glsl_error(& loc, state, 2262 "`return' with no value, in function %s returning " 2263 "non-void", 2264 state->current_function->function_name()); 2265 } 2266 inst = new(ctx) ir_return; 2267 } 2268 2269 state->found_return = true; 2270 instructions->push_tail(inst); 2271 break; 2272 } 2273 2274 case ast_discard: 2275 if (state->target != fragment_shader) { 2276 YYLTYPE loc = this->get_location(); 2277 2278 _mesa_glsl_error(& loc, state, 2279 "`discard' may only appear in a fragment shader"); 2280 } 2281 instructions->push_tail(new(ctx) ir_discard); 2282 break; 2283 2284 case ast_break: 2285 case ast_continue: 2286 /* FINISHME: Handle switch-statements. They cannot contain 'continue', 2287 * FINISHME: and they use a different IR instruction for 'break'. 2288 */ 2289 /* FINISHME: Correctly handle the nesting. If a switch-statement is 2290 * FINISHME: inside a loop, a 'continue' is valid and will bind to the 2291 * FINISHME: loop. 2292 */ 2293 if (state->loop_or_switch_nesting == NULL) { 2294 YYLTYPE loc = this->get_location(); 2295 2296 _mesa_glsl_error(& loc, state, 2297 "`%s' may only appear in a loop", 2298 (mode == ast_break) ? "break" : "continue"); 2299 } else { 2300 ir_loop *const loop = state->loop_or_switch_nesting->as_loop(); 2301 2302 if (loop != NULL) { 2303 ir_loop_jump *const jump = 2304 new(ctx) ir_loop_jump((mode == ast_break) 2305 ? ir_loop_jump::jump_break 2306 : ir_loop_jump::jump_continue); 2307 instructions->push_tail(jump); 2308 } 2309 } 2310 2311 break; 2312 } 2313 2314 /* Jump instructions do not have r-values. 2315 */ 2316 return NULL; 2317} 2318 2319 2320ir_rvalue * 2321ast_selection_statement::hir(exec_list *instructions, 2322 struct _mesa_glsl_parse_state *state) 2323{ 2324 void *ctx = state; 2325 2326 ir_rvalue *const condition = this->condition->hir(instructions, state); 2327 2328 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec: 2329 * 2330 * "Any expression whose type evaluates to a Boolean can be used as the 2331 * conditional expression bool-expression. Vector types are not accepted 2332 * as the expression to if." 2333 * 2334 * The checks are separated so that higher quality diagnostics can be 2335 * generated for cases where both rules are violated. 2336 */ 2337 if (!condition->type->is_boolean() || !condition->type->is_scalar()) { 2338 YYLTYPE loc = this->condition->get_location(); 2339 2340 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar " 2341 "boolean"); 2342 } 2343 2344 ir_if *const stmt = new(ctx) ir_if(condition); 2345 2346 if (then_statement != NULL) 2347 then_statement->hir(& stmt->then_instructions, state); 2348 2349 if (else_statement != NULL) 2350 else_statement->hir(& stmt->else_instructions, state); 2351 2352 instructions->push_tail(stmt); 2353 2354 /* if-statements do not have r-values. 2355 */ 2356 return NULL; 2357} 2358 2359 2360void 2361ast_iteration_statement::condition_to_hir(ir_loop *stmt, 2362 struct _mesa_glsl_parse_state *state) 2363{ 2364 void *ctx = state; 2365 2366 if (condition != NULL) { 2367 ir_rvalue *const cond = 2368 condition->hir(& stmt->body_instructions, state); 2369 2370 if ((cond == NULL) 2371 || !cond->type->is_boolean() || !cond->type->is_scalar()) { 2372 YYLTYPE loc = condition->get_location(); 2373 2374 _mesa_glsl_error(& loc, state, 2375 "loop condition must be scalar boolean"); 2376 } else { 2377 /* As the first code in the loop body, generate a block that looks 2378 * like 'if (!condition) break;' as the loop termination condition. 2379 */ 2380 ir_rvalue *const not_cond = 2381 new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond, 2382 NULL); 2383 2384 ir_if *const if_stmt = new(ctx) ir_if(not_cond); 2385 2386 ir_jump *const break_stmt = 2387 new(ctx) ir_loop_jump(ir_loop_jump::jump_break); 2388 2389 if_stmt->then_instructions.push_tail(break_stmt); 2390 stmt->body_instructions.push_tail(if_stmt); 2391 } 2392 } 2393} 2394 2395 2396ir_rvalue * 2397ast_iteration_statement::hir(exec_list *instructions, 2398 struct _mesa_glsl_parse_state *state) 2399{ 2400 void *ctx = state; 2401 2402 /* For-loops and while-loops start a new scope, but do-while loops do not. 2403 */ 2404 if (mode != ast_do_while) 2405 state->symbols->push_scope(); 2406 2407 if (init_statement != NULL) 2408 init_statement->hir(instructions, state); 2409 2410 ir_loop *const stmt = new(ctx) ir_loop(); 2411 instructions->push_tail(stmt); 2412 2413 /* Track the current loop and / or switch-statement nesting. 2414 */ 2415 ir_instruction *const nesting = state->loop_or_switch_nesting; 2416 state->loop_or_switch_nesting = stmt; 2417 2418 if (mode != ast_do_while) 2419 condition_to_hir(stmt, state); 2420 2421 if (body != NULL) 2422 body->hir(& stmt->body_instructions, state); 2423 2424 if (rest_expression != NULL) 2425 rest_expression->hir(& stmt->body_instructions, state); 2426 2427 if (mode == ast_do_while) 2428 condition_to_hir(stmt, state); 2429 2430 if (mode != ast_do_while) 2431 state->symbols->pop_scope(); 2432 2433 /* Restore previous nesting before returning. 2434 */ 2435 state->loop_or_switch_nesting = nesting; 2436 2437 /* Loops do not have r-values. 2438 */ 2439 return NULL; 2440} 2441 2442 2443ir_rvalue * 2444ast_type_specifier::hir(exec_list *instructions, 2445 struct _mesa_glsl_parse_state *state) 2446{ 2447 if (this->structure != NULL) 2448 return this->structure->hir(instructions, state); 2449 2450 return NULL; 2451} 2452 2453 2454ir_rvalue * 2455ast_struct_specifier::hir(exec_list *instructions, 2456 struct _mesa_glsl_parse_state *state) 2457{ 2458 unsigned decl_count = 0; 2459 2460 /* Make an initial pass over the list of structure fields to determine how 2461 * many there are. Each element in this list is an ast_declarator_list. 2462 * This means that we actually need to count the number of elements in the 2463 * 'declarations' list in each of the elements. 2464 */ 2465 foreach_list_typed (ast_declarator_list, decl_list, link, 2466 &this->declarations) { 2467 foreach_list_const (decl_ptr, & decl_list->declarations) { 2468 decl_count++; 2469 } 2470 } 2471 2472 2473 /* Allocate storage for the structure fields and process the field 2474 * declarations. As the declarations are processed, try to also convert 2475 * the types to HIR. This ensures that structure definitions embedded in 2476 * other structure definitions are processed. 2477 */ 2478 glsl_struct_field *const fields = talloc_array(state, glsl_struct_field, 2479 decl_count); 2480 2481 unsigned i = 0; 2482 foreach_list_typed (ast_declarator_list, decl_list, link, 2483 &this->declarations) { 2484 const char *type_name; 2485 2486 decl_list->type->specifier->hir(instructions, state); 2487 2488 const glsl_type *decl_type = 2489 decl_list->type->specifier->glsl_type(& type_name, state); 2490 2491 foreach_list_typed (ast_declaration, decl, link, 2492 &decl_list->declarations) { 2493 const struct glsl_type *const field_type = 2494 (decl->is_array) 2495 ? process_array_type(decl_type, decl->array_size, state) 2496 : decl_type; 2497 2498 fields[i].type = (field_type != NULL) 2499 ? field_type : glsl_type::error_type; 2500 fields[i].name = decl->identifier; 2501 i++; 2502 } 2503 } 2504 2505 assert(i == decl_count); 2506 2507 const char *name; 2508 if (this->name == NULL) { 2509 static unsigned anon_count = 1; 2510 char buf[32]; 2511 2512 snprintf(buf, sizeof(buf), "#anon_struct_%04x", anon_count); 2513 anon_count++; 2514 2515 name = strdup(buf); 2516 } else { 2517 name = this->name; 2518 } 2519 2520 const glsl_type *t = 2521 glsl_type::get_record_instance(fields, decl_count, name); 2522 2523 YYLTYPE loc = this->get_location(); 2524 if (!state->symbols->add_type(name, t)) { 2525 _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name); 2526 } else { 2527 /* This logic is a bit tricky. It is an error to declare a structure at 2528 * global scope if there is also a function with the same name. 2529 */ 2530 if ((state->current_function == NULL) 2531 && (state->symbols->get_function(name) != NULL)) { 2532 _mesa_glsl_error(& loc, state, "name `%s' previously defined", name); 2533 } else { 2534 t->generate_constructor(state->symbols); 2535 } 2536 2537 const glsl_type **s = (const glsl_type **) 2538 realloc(state->user_structures, 2539 sizeof(state->user_structures[0]) * 2540 (state->num_user_structures + 1)); 2541 if (s != NULL) { 2542 s[state->num_user_structures] = t; 2543 state->user_structures = s; 2544 state->num_user_structures++; 2545 } 2546 } 2547 2548 /* Structure type definitions do not have r-values. 2549 */ 2550 return NULL; 2551} 2552