1 /* -*- c++ -*- */ 2 /* 3 * Copyright © 2009 Intel Corporation 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice (including the next 13 * paragraph) shall be included in all copies or substantial portions of the 14 * Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 * DEALINGS IN THE SOFTWARE. 23 */ 24 25 #pragma once 26 #ifndef AST_H 27 #define AST_H 28 29 #include "list.h" 30 #include "glsl_parser_extras.h" 31 32 struct _mesa_glsl_parse_state; 33 34 struct YYLTYPE; 35 36 /** 37 * \defgroup AST Abstract syntax tree node definitions 38 * 39 * An abstract syntax tree is generated by the parser. This is a fairly 40 * direct representation of the gramma derivation for the source program. 41 * No symantic checking is done during the generation of the AST. Only 42 * syntactic checking is done. Symantic checking is performed by a later 43 * stage that converts the AST to a more generic intermediate representation. 44 * 45 *@{ 46 */ 47 /** 48 * Base class of all abstract syntax tree nodes 49 */ 50 class ast_node { 51 public: 52 /* Callers of this hieralloc-based new need not call delete. It's 53 * easier to just hieralloc_free 'ctx' (or any of its ancestors). */ new(size_t size,void * ctx)54 static void* operator new(size_t size, void *ctx) 55 { 56 void *node; 57 58 node = hieralloc_zero_size(ctx, size); 59 assert(node != NULL); 60 61 return node; 62 } 63 64 /* If the user *does* call delete, that's OK, we will just 65 * hieralloc_free in that case. */ delete(void * table)66 static void operator delete(void *table) 67 { 68 hieralloc_free(table); 69 } 70 71 /** 72 * Print an AST node in something approximating the original GLSL code 73 */ 74 virtual void print(void) const; 75 76 /** 77 * Convert the AST node to the high-level intermediate representation 78 */ 79 virtual ir_rvalue *hir(exec_list *instructions, 80 struct _mesa_glsl_parse_state *state); 81 82 /** 83 * Retrieve the source location of an AST node 84 * 85 * This function is primarily used to get the source position of an AST node 86 * into a form that can be passed to \c _mesa_glsl_error. 87 * 88 * \sa _mesa_glsl_error, ast_node::set_location 89 */ get_location(void)90 struct YYLTYPE get_location(void) const 91 { 92 struct YYLTYPE locp; 93 94 locp.source = this->location.source; 95 locp.first_line = this->location.line; 96 locp.first_column = this->location.column; 97 locp.last_line = locp.first_line; 98 locp.last_column = locp.first_column; 99 100 return locp; 101 } 102 103 /** 104 * Set the source location of an AST node from a parser location 105 * 106 * \sa ast_node::get_location 107 */ set_location(const struct YYLTYPE & locp)108 void set_location(const struct YYLTYPE &locp) 109 { 110 this->location.source = locp.source; 111 this->location.line = locp.first_line; 112 this->location.column = locp.first_column; 113 } 114 115 /** 116 * Source location of the AST node. 117 */ 118 struct { 119 unsigned source; /**< GLSL source number. */ 120 unsigned line; /**< Line number within the source string. */ 121 unsigned column; /**< Column in the line. */ 122 } location; 123 124 exec_node link; ~ast_node()125 virtual ~ast_node(){}; 126 protected: 127 /** 128 * The only constructor is protected so that only derived class objects can 129 * be created. 130 */ 131 ast_node(void); 132 }; 133 134 135 /** 136 * Operators for AST expression nodes. 137 */ 138 enum ast_operators { 139 ast_assign, 140 ast_plus, /**< Unary + operator. */ 141 ast_neg, 142 ast_add, 143 ast_sub, 144 ast_mul, 145 ast_div, 146 ast_mod, 147 ast_lshift, 148 ast_rshift, 149 ast_less, 150 ast_greater, 151 ast_lequal, 152 ast_gequal, 153 ast_equal, 154 ast_nequal, 155 ast_bit_and, 156 ast_bit_xor, 157 ast_bit_or, 158 ast_bit_not, 159 ast_logic_and, 160 ast_logic_xor, 161 ast_logic_or, 162 ast_logic_not, 163 164 ast_mul_assign, 165 ast_div_assign, 166 ast_mod_assign, 167 ast_add_assign, 168 ast_sub_assign, 169 ast_ls_assign, 170 ast_rs_assign, 171 ast_and_assign, 172 ast_xor_assign, 173 ast_or_assign, 174 175 ast_conditional, 176 177 ast_pre_inc, 178 ast_pre_dec, 179 ast_post_inc, 180 ast_post_dec, 181 ast_field_selection, 182 ast_array_index, 183 184 ast_function_call, 185 186 ast_identifier, 187 ast_int_constant, 188 ast_uint_constant, 189 ast_float_constant, 190 ast_bool_constant, 191 192 ast_sequence 193 }; 194 195 /** 196 * Representation of any sort of expression. 197 */ 198 class ast_expression : public ast_node { 199 public: 200 ast_expression(int oper, ast_expression *, 201 ast_expression *, ast_expression *); 202 ast_expression(const char * identifier)203 ast_expression(const char *identifier) : 204 oper(ast_identifier) 205 { 206 subexpressions[0] = NULL; 207 subexpressions[1] = NULL; 208 subexpressions[2] = NULL; 209 primary_expression.identifier = (char *) identifier; 210 } 211 212 static const char *operator_string(enum ast_operators op); 213 214 virtual ir_rvalue *hir(exec_list *instructions, 215 struct _mesa_glsl_parse_state *state); 216 217 virtual void print(void) const; 218 219 enum ast_operators oper; 220 221 ast_expression *subexpressions[3]; 222 223 union { 224 char *identifier; 225 int int_constant; 226 float float_constant; 227 unsigned uint_constant; 228 int bool_constant; 229 } primary_expression; 230 231 232 /** 233 * List of expressions for an \c ast_sequence or parameters for an 234 * \c ast_function_call 235 */ 236 exec_list expressions; 237 }; 238 239 class ast_expression_bin : public ast_expression { 240 public: 241 ast_expression_bin(int oper, ast_expression *, ast_expression *); 242 243 virtual void print(void) const; 244 }; 245 246 /** 247 * Subclass of expressions for function calls 248 */ 249 class ast_function_expression : public ast_expression { 250 public: ast_function_expression(ast_expression * callee)251 ast_function_expression(ast_expression *callee) 252 : ast_expression(ast_function_call, callee, 253 NULL, NULL), 254 cons(false) 255 { 256 /* empty */ 257 } 258 ast_function_expression(class ast_type_specifier * type)259 ast_function_expression(class ast_type_specifier *type) 260 : ast_expression(ast_function_call, (ast_expression *) type, 261 NULL, NULL), 262 cons(true) 263 { 264 /* empty */ 265 } 266 is_constructor()267 bool is_constructor() const 268 { 269 return cons; 270 } 271 272 virtual ir_rvalue *hir(exec_list *instructions, 273 struct _mesa_glsl_parse_state *state); 274 275 private: 276 /** 277 * Is this function call actually a constructor? 278 */ 279 bool cons; 280 }; 281 282 283 /** 284 * Number of possible operators for an ast_expression 285 * 286 * This is done as a define instead of as an additional value in the enum so 287 * that the compiler won't generate spurious messages like "warning: 288 * enumeration value ‘ast_num_operators’ not handled in switch" 289 */ 290 #define AST_NUM_OPERATORS (ast_sequence + 1) 291 292 293 class ast_compound_statement : public ast_node { 294 public: 295 ast_compound_statement(int new_scope, ast_node *statements); 296 virtual void print(void) const; 297 298 virtual ir_rvalue *hir(exec_list *instructions, 299 struct _mesa_glsl_parse_state *state); 300 301 int new_scope; 302 exec_list statements; 303 }; 304 305 class ast_declaration : public ast_node { 306 public: 307 ast_declaration(char *identifier, int is_array, ast_expression *array_size, 308 ast_expression *initializer); 309 virtual void print(void) const; 310 311 char *identifier; 312 313 int is_array; 314 ast_expression *array_size; 315 316 ast_expression *initializer; 317 }; 318 319 320 enum { 321 ast_precision_high = 0, /**< Default precision. */ 322 ast_precision_medium, 323 ast_precision_low 324 }; 325 326 struct ast_type_qualifier { 327 union { 328 struct { 329 unsigned invariant:1; 330 unsigned constant:1; 331 unsigned attribute:1; 332 unsigned varying:1; 333 unsigned in:1; 334 unsigned out:1; 335 unsigned centroid:1; 336 unsigned uniform:1; 337 unsigned smooth:1; 338 unsigned flat:1; 339 unsigned noperspective:1; 340 341 /** \name Layout qualifiers for GL_ARB_fragment_coord_conventions */ 342 /*@{*/ 343 unsigned origin_upper_left:1; 344 unsigned pixel_center_integer:1; 345 /*@}*/ 346 347 /** 348 * Flag set if GL_ARB_explicit_attrib_location "location" layout 349 * qualifier is used. 350 */ 351 unsigned explicit_location:1; 352 } q; 353 unsigned i; 354 } flags; 355 356 /** 357 * Location specified via GL_ARB_explicit_attrib_location layout 358 * 359 * \note 360 * This field is only valid if \c explicit_location is set. 361 */ 362 unsigned location; 363 }; 364 365 class ast_struct_specifier : public ast_node { 366 public: 367 ast_struct_specifier(char *identifier, ast_node *declarator_list); 368 virtual void print(void) const; 369 370 virtual ir_rvalue *hir(exec_list *instructions, 371 struct _mesa_glsl_parse_state *state); 372 373 char *name; 374 exec_list declarations; 375 }; 376 377 378 enum ast_types { 379 ast_void, 380 ast_float, 381 ast_int, 382 ast_uint, 383 ast_bool, 384 ast_vec2, 385 ast_vec3, 386 ast_vec4, 387 ast_bvec2, 388 ast_bvec3, 389 ast_bvec4, 390 ast_ivec2, 391 ast_ivec3, 392 ast_ivec4, 393 ast_uvec2, 394 ast_uvec3, 395 ast_uvec4, 396 ast_mat2, 397 ast_mat2x3, 398 ast_mat2x4, 399 ast_mat3x2, 400 ast_mat3, 401 ast_mat3x4, 402 ast_mat4x2, 403 ast_mat4x3, 404 ast_mat4, 405 ast_sampler1d, 406 ast_sampler2d, 407 ast_sampler2drect, 408 ast_sampler3d, 409 ast_samplercube, 410 ast_sampler1dshadow, 411 ast_sampler2dshadow, 412 ast_sampler2drectshadow, 413 ast_samplercubeshadow, 414 ast_sampler1darray, 415 ast_sampler2darray, 416 ast_sampler1darrayshadow, 417 ast_sampler2darrayshadow, 418 ast_isampler1d, 419 ast_isampler2d, 420 ast_isampler3d, 421 ast_isamplercube, 422 ast_isampler1darray, 423 ast_isampler2darray, 424 ast_usampler1d, 425 ast_usampler2d, 426 ast_usampler3d, 427 ast_usamplercube, 428 ast_usampler1darray, 429 ast_usampler2darray, 430 431 ast_struct, 432 ast_type_name 433 }; 434 435 436 class ast_type_specifier : public ast_node { 437 public: 438 ast_type_specifier(int specifier); 439 440 /** Construct a type specifier from a type name */ ast_type_specifier(const char * name)441 ast_type_specifier(const char *name) 442 : type_specifier(ast_type_name), type_name(name), structure(NULL), 443 is_array(false), array_size(NULL), precision(ast_precision_high) 444 { 445 /* empty */ 446 } 447 448 /** Construct a type specifier from a structure definition */ ast_type_specifier(ast_struct_specifier * s)449 ast_type_specifier(ast_struct_specifier *s) 450 : type_specifier(ast_struct), type_name(s->name), structure(s), 451 is_array(false), array_size(NULL), precision(ast_precision_high) 452 { 453 /* empty */ 454 } 455 456 const struct glsl_type *glsl_type(const char **name, 457 struct _mesa_glsl_parse_state *state) 458 const; 459 460 virtual void print(void) const; 461 462 ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *); 463 464 enum ast_types type_specifier; 465 466 const char *type_name; 467 ast_struct_specifier *structure; 468 469 int is_array; 470 ast_expression *array_size; 471 472 unsigned precision:2; 473 }; 474 475 476 class ast_fully_specified_type : public ast_node { 477 public: 478 virtual void print(void) const; 479 bool has_qualifiers() const; 480 481 ast_type_qualifier qualifier; 482 ast_type_specifier *specifier; 483 }; 484 485 486 class ast_declarator_list : public ast_node { 487 public: 488 ast_declarator_list(ast_fully_specified_type *); 489 virtual void print(void) const; 490 491 virtual ir_rvalue *hir(exec_list *instructions, 492 struct _mesa_glsl_parse_state *state); 493 494 ast_fully_specified_type *type; 495 exec_list declarations; 496 497 /** 498 * Special flag for vertex shader "invariant" declarations. 499 * 500 * Vertex shaders can contain "invariant" variable redeclarations that do 501 * not include a type. For example, "invariant gl_Position;". This flag 502 * is used to note these cases when no type is specified. 503 */ 504 int invariant; 505 }; 506 507 508 class ast_parameter_declarator : public ast_node { 509 public: ast_parameter_declarator()510 ast_parameter_declarator() 511 { 512 this->identifier = NULL; 513 this->is_array = false; 514 this->array_size = 0; 515 } 516 517 virtual void print(void) const; 518 519 virtual ir_rvalue *hir(exec_list *instructions, 520 struct _mesa_glsl_parse_state *state); 521 522 ast_fully_specified_type *type; 523 char *identifier; 524 int is_array; 525 ast_expression *array_size; 526 527 static void parameters_to_hir(exec_list *ast_parameters, 528 bool formal, exec_list *ir_parameters, 529 struct _mesa_glsl_parse_state *state); 530 531 private: 532 /** Is this parameter declaration part of a formal parameter list? */ 533 bool formal_parameter; 534 535 /** 536 * Is this parameter 'void' type? 537 * 538 * This field is set by \c ::hir. 539 */ 540 bool is_void; 541 }; 542 543 544 class ast_function : public ast_node { 545 public: 546 ast_function(void); 547 548 virtual void print(void) const; 549 550 virtual ir_rvalue *hir(exec_list *instructions, 551 struct _mesa_glsl_parse_state *state); 552 553 ast_fully_specified_type *return_type; 554 char *identifier; 555 556 exec_list parameters; 557 558 private: 559 /** 560 * Is this prototype part of the function definition? 561 * 562 * Used by ast_function_definition::hir to process the parameters, etc. 563 * of the function. 564 * 565 * \sa ::hir 566 */ 567 bool is_definition; 568 569 /** 570 * Function signature corresponding to this function prototype instance 571 * 572 * Used by ast_function_definition::hir to process the parameters, etc. 573 * of the function. 574 * 575 * \sa ::hir 576 */ 577 class ir_function_signature *signature; 578 579 friend class ast_function_definition; 580 }; 581 582 583 class ast_declaration_statement : public ast_node { 584 public: 585 ast_declaration_statement(void); 586 587 enum { 588 ast_function, 589 ast_declaration, 590 ast_precision 591 } mode; 592 593 union { 594 class ast_function *function; 595 ast_declarator_list *declarator; 596 ast_type_specifier *type; 597 ast_node *node; 598 } declaration; 599 }; 600 601 602 class ast_expression_statement : public ast_node { 603 public: 604 ast_expression_statement(ast_expression *); 605 virtual void print(void) const; 606 607 virtual ir_rvalue *hir(exec_list *instructions, 608 struct _mesa_glsl_parse_state *state); 609 610 ast_expression *expression; 611 }; 612 613 614 class ast_case_label : public ast_node { 615 public: 616 617 /** 618 * An expression of NULL means 'default'. 619 */ 620 ast_expression *expression; 621 }; 622 623 class ast_selection_statement : public ast_node { 624 public: 625 ast_selection_statement(ast_expression *condition, 626 ast_node *then_statement, 627 ast_node *else_statement); 628 virtual void print(void) const; 629 630 virtual ir_rvalue *hir(exec_list *instructions, 631 struct _mesa_glsl_parse_state *state); 632 633 ast_expression *condition; 634 ast_node *then_statement; 635 ast_node *else_statement; 636 }; 637 638 639 class ast_switch_statement : public ast_node { 640 public: 641 ast_expression *expression; 642 exec_list statements; 643 }; 644 645 class ast_iteration_statement : public ast_node { 646 public: 647 ast_iteration_statement(int mode, ast_node *init, ast_node *condition, 648 ast_expression *rest_expression, ast_node *body); 649 650 virtual void print(void) const; 651 652 virtual ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *); 653 654 enum ast_iteration_modes { 655 ast_for, 656 ast_while, 657 ast_do_while 658 } mode; 659 660 661 ast_node *init_statement; 662 ast_node *condition; 663 ast_expression *rest_expression; 664 665 ast_node *body; 666 667 private: 668 /** 669 * Generate IR from the condition of a loop 670 * 671 * This is factored out of ::hir because some loops have the condition 672 * test at the top (for and while), and others have it at the end (do-while). 673 */ 674 void condition_to_hir(class ir_loop *, struct _mesa_glsl_parse_state *); 675 }; 676 677 678 class ast_jump_statement : public ast_node { 679 public: 680 ast_jump_statement(int mode, ast_expression *return_value); 681 virtual void print(void) const; 682 683 virtual ir_rvalue *hir(exec_list *instructions, 684 struct _mesa_glsl_parse_state *state); 685 686 enum ast_jump_modes { 687 ast_continue, 688 ast_break, 689 ast_return, 690 ast_discard 691 } mode; 692 693 ast_expression *opt_return_value; 694 }; 695 696 697 class ast_function_definition : public ast_node { 698 public: 699 virtual void print(void) const; 700 701 virtual ir_rvalue *hir(exec_list *instructions, 702 struct _mesa_glsl_parse_state *state); 703 704 ast_function *prototype; 705 ast_compound_statement *body; 706 }; 707 /*@}*/ 708 709 extern void 710 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state); 711 712 extern ir_rvalue * 713 _mesa_ast_field_selection_to_hir(const ast_expression *expr, 714 exec_list *instructions, 715 struct _mesa_glsl_parse_state *state); 716 717 void 718 emit_function(_mesa_glsl_parse_state *state, exec_list *instructions, 719 ir_function *f); 720 721 #endif /* AST_H */ 722