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 #ifndef AST_H 26 #define AST_H 27 28 #include "list.h" 29 #include "glsl_parser_extras.h" 30 #include "compiler/glsl_types.h" 31 #include "util/bitset.h" 32 33 struct _mesa_glsl_parse_state; 34 35 struct YYLTYPE; 36 37 /** 38 * \defgroup AST Abstract syntax tree node definitions 39 * 40 * An abstract syntax tree is generated by the parser. This is a fairly 41 * direct representation of the gramma derivation for the source program. 42 * No symantic checking is done during the generation of the AST. Only 43 * syntactic checking is done. Symantic checking is performed by a later 44 * stage that converts the AST to a more generic intermediate representation. 45 * 46 *@{ 47 */ 48 /** 49 * Base class of all abstract syntax tree nodes 50 */ 51 class ast_node { 52 public: 53 DECLARE_LINEAR_ZALLOC_CXX_OPERATORS(ast_node); 54 55 /** 56 * Print an AST node in something approximating the original GLSL code 57 */ 58 virtual void print(void) const; 59 60 /** 61 * Convert the AST node to the high-level intermediate representation 62 */ 63 virtual ir_rvalue *hir(exec_list *instructions, 64 struct _mesa_glsl_parse_state *state); 65 66 virtual bool has_sequence_subexpression() const; 67 68 /** 69 * Retrieve the source location of an AST node 70 * 71 * This function is primarily used to get the source position of an AST node 72 * into a form that can be passed to \c _mesa_glsl_error. 73 * 74 * \sa _mesa_glsl_error, ast_node::set_location 75 */ get_location(void)76 struct YYLTYPE get_location(void) const 77 { 78 struct YYLTYPE locp; 79 80 locp.path = this->location.path; 81 locp.source = this->location.source; 82 locp.first_line = this->location.first_line; 83 locp.first_column = this->location.first_column; 84 locp.last_line = this->location.last_line; 85 locp.last_column = this->location.last_column; 86 87 return locp; 88 } 89 90 /** 91 * Set the source location of an AST node from a parser location 92 * 93 * \sa ast_node::get_location 94 */ set_location(const struct YYLTYPE & locp)95 void set_location(const struct YYLTYPE &locp) 96 { 97 this->location.path = locp.path; 98 this->location.source = locp.source; 99 this->location.first_line = locp.first_line; 100 this->location.first_column = locp.first_column; 101 this->location.last_line = locp.last_line; 102 this->location.last_column = locp.last_column; 103 } 104 105 /** 106 * Set the source location range of an AST node using two location nodes 107 * 108 * \sa ast_node::set_location 109 */ set_location_range(const struct YYLTYPE & begin,const struct YYLTYPE & end)110 void set_location_range(const struct YYLTYPE &begin, const struct YYLTYPE &end) 111 { 112 this->location.path = begin.path; 113 this->location.source = begin.source; 114 this->location.first_line = begin.first_line; 115 this->location.last_line = end.last_line; 116 this->location.first_column = begin.first_column; 117 this->location.last_column = end.last_column; 118 } 119 120 /** 121 * Source location of the AST node. 122 */ 123 struct { 124 char *path; /**< GLSL shader include path. */ 125 unsigned source; /**< GLSL source number. */ 126 unsigned first_line; /**< First line number within the source string. */ 127 unsigned first_column; /**< First column in the first line. */ 128 unsigned last_line; /**< Last line number within the source string. */ 129 unsigned last_column; /**< Last column in the last line. */ 130 } location; 131 132 exec_node link; 133 134 virtual void set_is_lhs(bool); 135 136 protected: 137 /** 138 * The only constructor is protected so that only derived class objects can 139 * be created. 140 */ 141 ast_node(void); 142 }; 143 144 145 /** 146 * Operators for AST expression nodes. 147 */ 148 enum ast_operators { 149 ast_assign, 150 ast_plus, /**< Unary + operator. */ 151 ast_neg, 152 ast_add, 153 ast_sub, 154 ast_mul, 155 ast_div, 156 ast_mod, 157 ast_lshift, 158 ast_rshift, 159 ast_less, 160 ast_greater, 161 ast_lequal, 162 ast_gequal, 163 ast_equal, 164 ast_nequal, 165 ast_bit_and, 166 ast_bit_xor, 167 ast_bit_or, 168 ast_bit_not, 169 ast_logic_and, 170 ast_logic_xor, 171 ast_logic_or, 172 ast_logic_not, 173 174 ast_mul_assign, 175 ast_div_assign, 176 ast_mod_assign, 177 ast_add_assign, 178 ast_sub_assign, 179 ast_ls_assign, 180 ast_rs_assign, 181 ast_and_assign, 182 ast_xor_assign, 183 ast_or_assign, 184 185 ast_conditional, 186 187 ast_pre_inc, 188 ast_pre_dec, 189 ast_post_inc, 190 ast_post_dec, 191 ast_field_selection, 192 ast_array_index, 193 ast_unsized_array_dim, 194 195 ast_function_call, 196 197 ast_identifier, 198 ast_int_constant, 199 ast_uint_constant, 200 ast_float_constant, 201 ast_bool_constant, 202 ast_double_constant, 203 ast_int64_constant, 204 ast_uint64_constant, 205 206 ast_sequence, 207 ast_aggregate 208 209 /** 210 * Number of possible operators for an ast_expression 211 * 212 * This is done as a define instead of as an additional value in the enum so 213 * that the compiler won't generate spurious messages like "warning: 214 * enumeration value ‘ast_num_operators’ not handled in switch" 215 */ 216 #define AST_NUM_OPERATORS (ast_aggregate + 1) 217 }; 218 219 /** 220 * Representation of any sort of expression. 221 */ 222 class ast_expression : public ast_node { 223 public: 224 ast_expression(int oper, ast_expression *, 225 ast_expression *, ast_expression *); 226 ast_expression(const char * identifier)227 ast_expression(const char *identifier) : 228 oper(ast_identifier) 229 { 230 subexpressions[0] = NULL; 231 subexpressions[1] = NULL; 232 subexpressions[2] = NULL; 233 primary_expression.identifier = identifier; 234 this->non_lvalue_description = NULL; 235 this->is_lhs = false; 236 } 237 238 static const char *operator_string(enum ast_operators op); 239 240 virtual ir_rvalue *hir(exec_list *instructions, 241 struct _mesa_glsl_parse_state *state); 242 243 virtual void hir_no_rvalue(exec_list *instructions, 244 struct _mesa_glsl_parse_state *state); 245 246 virtual bool has_sequence_subexpression() const; 247 248 ir_rvalue *do_hir(exec_list *instructions, 249 struct _mesa_glsl_parse_state *state, 250 bool needs_rvalue); 251 252 virtual void print(void) const; 253 254 enum ast_operators oper; 255 256 ast_expression *subexpressions[3]; 257 258 union { 259 const char *identifier; 260 int int_constant; 261 float float_constant; 262 unsigned uint_constant; 263 int bool_constant; 264 double double_constant; 265 uint64_t uint64_constant; 266 int64_t int64_constant; 267 } primary_expression; 268 269 270 /** 271 * List of expressions for an \c ast_sequence or parameters for an 272 * \c ast_function_call 273 */ 274 exec_list expressions; 275 276 /** 277 * For things that can't be l-values, this describes what it is. 278 * 279 * This text is used by the code that generates IR for assignments to 280 * detect and emit useful messages for assignments to some things that 281 * can't be l-values. For example, pre- or post-incerement expressions. 282 * 283 * \note 284 * This pointer may be \c NULL. 285 */ 286 const char *non_lvalue_description; 287 288 void set_is_lhs(bool new_value); 289 290 private: 291 bool is_lhs; 292 }; 293 294 class ast_expression_bin : public ast_expression { 295 public: 296 ast_expression_bin(int oper, ast_expression *, ast_expression *); 297 298 virtual void print(void) const; 299 }; 300 301 /** 302 * Subclass of expressions for function calls 303 */ 304 class ast_function_expression : public ast_expression { 305 public: ast_function_expression(ast_expression * callee)306 ast_function_expression(ast_expression *callee) 307 : ast_expression(ast_function_call, callee, 308 NULL, NULL), 309 cons(false) 310 { 311 /* empty */ 312 } 313 ast_function_expression(class ast_type_specifier * type)314 ast_function_expression(class ast_type_specifier *type) 315 : ast_expression(ast_function_call, (ast_expression *) type, 316 NULL, NULL), 317 cons(true) 318 { 319 /* empty */ 320 } 321 is_constructor()322 bool is_constructor() const 323 { 324 return cons; 325 } 326 327 virtual ir_rvalue *hir(exec_list *instructions, 328 struct _mesa_glsl_parse_state *state); 329 330 virtual void hir_no_rvalue(exec_list *instructions, 331 struct _mesa_glsl_parse_state *state); 332 333 virtual bool has_sequence_subexpression() const; 334 335 private: 336 /** 337 * Is this function call actually a constructor? 338 */ 339 bool cons; 340 ir_rvalue * 341 handle_method(exec_list *instructions, 342 struct _mesa_glsl_parse_state *state); 343 }; 344 345 class ast_subroutine_list : public ast_node 346 { 347 public: 348 virtual void print(void) const; 349 exec_list declarations; 350 }; 351 352 class ast_array_specifier : public ast_node { 353 public: ast_array_specifier(const struct YYLTYPE & locp,ast_expression * dim)354 ast_array_specifier(const struct YYLTYPE &locp, ast_expression *dim) 355 { 356 set_location(locp); 357 array_dimensions.push_tail(&dim->link); 358 } 359 add_dimension(ast_expression * dim)360 void add_dimension(ast_expression *dim) 361 { 362 array_dimensions.push_tail(&dim->link); 363 } 364 is_single_dimension()365 bool is_single_dimension() const 366 { 367 return this->array_dimensions.get_tail_raw()->prev != NULL && 368 this->array_dimensions.get_tail_raw()->prev->is_head_sentinel(); 369 } 370 371 virtual void print(void) const; 372 373 /* This list contains objects of type ast_node containing the 374 * array dimensions in outermost-to-innermost order. 375 */ 376 exec_list array_dimensions; 377 }; 378 379 class ast_layout_expression : public ast_node { 380 public: ast_layout_expression(const struct YYLTYPE & locp,ast_expression * expr)381 ast_layout_expression(const struct YYLTYPE &locp, ast_expression *expr) 382 { 383 set_location(locp); 384 layout_const_expressions.push_tail(&expr->link); 385 } 386 387 bool process_qualifier_constant(struct _mesa_glsl_parse_state *state, 388 const char *qual_indentifier, 389 unsigned *value, bool can_be_zero); 390 merge_qualifier(ast_layout_expression * l_expr)391 void merge_qualifier(ast_layout_expression *l_expr) 392 { 393 layout_const_expressions.append_list(&l_expr->layout_const_expressions); 394 } 395 396 exec_list layout_const_expressions; 397 }; 398 399 /** 400 * C-style aggregate initialization class 401 * 402 * Represents C-style initializers of vectors, matrices, arrays, and 403 * structures. E.g., vec3 pos = {1.0, 0.0, -1.0} is equivalent to 404 * vec3 pos = vec3(1.0, 0.0, -1.0). 405 * 406 * Specified in GLSL 4.20 and GL_ARB_shading_language_420pack. 407 * 408 * \sa _mesa_ast_set_aggregate_type 409 */ 410 class ast_aggregate_initializer : public ast_expression { 411 public: ast_aggregate_initializer()412 ast_aggregate_initializer() 413 : ast_expression(ast_aggregate, NULL, NULL, NULL), 414 constructor_type(NULL) 415 { 416 /* empty */ 417 } 418 419 /** 420 * glsl_type of the aggregate, which is inferred from the LHS of whatever 421 * the aggregate is being used to initialize. This can't be inferred at 422 * parse time (since the parser deals with ast_type_specifiers, not 423 * glsl_types), so the parser leaves it NULL. However, the ast-to-hir 424 * conversion code makes sure to fill it in with the appropriate type 425 * before hir() is called. 426 */ 427 const glsl_type *constructor_type; 428 429 virtual ir_rvalue *hir(exec_list *instructions, 430 struct _mesa_glsl_parse_state *state); 431 432 virtual void hir_no_rvalue(exec_list *instructions, 433 struct _mesa_glsl_parse_state *state); 434 }; 435 436 437 class ast_compound_statement : public ast_node { 438 public: 439 ast_compound_statement(int new_scope, ast_node *statements); 440 virtual void print(void) const; 441 442 virtual ir_rvalue *hir(exec_list *instructions, 443 struct _mesa_glsl_parse_state *state); 444 445 int new_scope; 446 exec_list statements; 447 }; 448 449 class ast_declaration : public ast_node { 450 public: 451 ast_declaration(const char *identifier, 452 ast_array_specifier *array_specifier, 453 ast_expression *initializer); 454 virtual void print(void) const; 455 456 const char *identifier; 457 458 ast_array_specifier *array_specifier; 459 460 ast_expression *initializer; 461 }; 462 463 464 enum { 465 ast_precision_none = 0, /**< Absence of precision qualifier. */ 466 ast_precision_high, 467 ast_precision_medium, 468 ast_precision_low 469 }; 470 471 enum { 472 ast_depth_none = 0, /**< Absence of depth qualifier. */ 473 ast_depth_any, 474 ast_depth_greater, 475 ast_depth_less, 476 ast_depth_unchanged 477 }; 478 479 struct ast_type_qualifier { 480 DECLARE_RALLOC_CXX_OPERATORS(ast_type_qualifier); 481 /* Note: this bitset needs to have at least as many bits as the 'q' 482 * struct has flags, below. Previously, the size was 128 instead of 96. 483 * But an apparent bug in GCC 5.4.0 causes bad SSE code generation 484 * elsewhere, leading to a crash. 96 bits works around the issue. 485 * See https://bugs.freedesktop.org/show_bug.cgi?id=105497 486 */ 487 DECLARE_BITSET_T(bitset_t, 96); 488 489 union flags { 490 struct { 491 unsigned invariant:1; 492 unsigned precise:1; 493 unsigned constant:1; 494 unsigned attribute:1; 495 unsigned varying:1; 496 unsigned in:1; 497 unsigned out:1; 498 unsigned centroid:1; 499 unsigned sample:1; 500 unsigned patch:1; 501 unsigned uniform:1; 502 unsigned buffer:1; 503 unsigned shared_storage:1; 504 unsigned smooth:1; 505 unsigned flat:1; 506 unsigned noperspective:1; 507 508 /** \name Layout qualifiers for GL_ARB_fragment_coord_conventions */ 509 /*@{*/ 510 unsigned origin_upper_left:1; 511 unsigned pixel_center_integer:1; 512 /*@}*/ 513 514 /** 515 * Flag set if GL_ARB_enhanced_layouts "align" layout qualifier is 516 * used. 517 */ 518 unsigned explicit_align:1; 519 520 /** 521 * Flag set if GL_ARB_explicit_attrib_location "location" layout 522 * qualifier is used. 523 */ 524 unsigned explicit_location:1; 525 /** 526 * Flag set if GL_ARB_explicit_attrib_location "index" layout 527 * qualifier is used. 528 */ 529 unsigned explicit_index:1; 530 531 /** 532 * Flag set if GL_ARB_enhanced_layouts "component" layout 533 * qualifier is used. 534 */ 535 unsigned explicit_component:1; 536 537 /** 538 * Flag set if GL_ARB_shading_language_420pack "binding" layout 539 * qualifier is used. 540 */ 541 unsigned explicit_binding:1; 542 543 /** 544 * Flag set if GL_ARB_shader_atomic counter "offset" layout 545 * qualifier is used. 546 */ 547 unsigned explicit_offset:1; 548 549 /** \name Layout qualifiers for GL_AMD_conservative_depth */ 550 /** \{ */ 551 unsigned depth_type:1; 552 /** \} */ 553 554 /** \name Layout qualifiers for GL_ARB_uniform_buffer_object */ 555 /** \{ */ 556 unsigned std140:1; 557 unsigned std430:1; 558 unsigned shared:1; 559 unsigned packed:1; 560 unsigned column_major:1; 561 unsigned row_major:1; 562 /** \} */ 563 564 /** \name Layout qualifiers for GLSL 1.50 geometry shaders */ 565 /** \{ */ 566 unsigned prim_type:1; 567 unsigned max_vertices:1; 568 /** \} */ 569 570 /** 571 * local_size_{x,y,z} flags for compute shaders. Bit 0 represents 572 * local_size_x, and so on. 573 */ 574 unsigned local_size:3; 575 576 /** \name Layout qualifiers for ARB_compute_variable_group_size. */ 577 /** \{ */ 578 unsigned local_size_variable:1; 579 /** \} */ 580 581 /** \name Layout and memory qualifiers for ARB_shader_image_load_store. */ 582 /** \{ */ 583 unsigned early_fragment_tests:1; 584 unsigned explicit_image_format:1; 585 unsigned coherent:1; 586 unsigned _volatile:1; 587 unsigned restrict_flag:1; 588 unsigned read_only:1; /**< "readonly" qualifier. */ 589 unsigned write_only:1; /**< "writeonly" qualifier. */ 590 /** \} */ 591 592 /** \name Layout qualifiers for GL_ARB_gpu_shader5 */ 593 /** \{ */ 594 unsigned invocations:1; 595 unsigned stream:1; /**< Has stream value assigned */ 596 unsigned explicit_stream:1; /**< stream value assigned explicitly by shader code */ 597 /** \} */ 598 599 /** \name Layout qualifiers for GL_ARB_enhanced_layouts */ 600 /** \{ */ 601 unsigned explicit_xfb_offset:1; /**< xfb_offset value assigned explicitly by shader code */ 602 unsigned xfb_buffer:1; /**< Has xfb_buffer value assigned */ 603 unsigned explicit_xfb_buffer:1; /**< xfb_buffer value assigned explicitly by shader code */ 604 unsigned xfb_stride:1; /**< Is xfb_stride value yet to be merged with global values */ 605 unsigned explicit_xfb_stride:1; /**< xfb_stride value assigned explicitly by shader code */ 606 /** \} */ 607 608 /** \name Layout qualifiers for GL_ARB_tessellation_shader */ 609 /** \{ */ 610 /* tess eval input layout */ 611 /* gs prim_type reused for primitive mode */ 612 unsigned vertex_spacing:1; 613 unsigned ordering:1; 614 unsigned point_mode:1; 615 /* tess control output layout */ 616 unsigned vertices:1; 617 /** \} */ 618 619 /** \name Qualifiers for GL_ARB_shader_subroutine */ 620 /** \{ */ 621 unsigned subroutine:1; /**< Is this marked 'subroutine' */ 622 /** \} */ 623 624 /** \name Qualifiers for GL_KHR_blend_equation_advanced */ 625 /** \{ */ 626 unsigned blend_support:1; /**< Are there any blend_support_ qualifiers */ 627 /** \} */ 628 629 /** 630 * Flag set if GL_ARB_post_depth_coverage layout qualifier is used. 631 */ 632 unsigned post_depth_coverage:1; 633 634 /** 635 * Flags for the layout qualifers added by ARB_fragment_shader_interlock 636 */ 637 638 unsigned pixel_interlock_ordered:1; 639 unsigned pixel_interlock_unordered:1; 640 unsigned sample_interlock_ordered:1; 641 unsigned sample_interlock_unordered:1; 642 643 /** 644 * Flag set if GL_INTEL_conservartive_rasterization layout qualifier 645 * is used. 646 */ 647 unsigned inner_coverage:1; 648 649 /** \name Layout qualifiers for GL_ARB_bindless_texture */ 650 /** \{ */ 651 unsigned bindless_sampler:1; 652 unsigned bindless_image:1; 653 unsigned bound_sampler:1; 654 unsigned bound_image:1; 655 /** \} */ 656 657 /** \name Layout qualifiers for GL_EXT_shader_framebuffer_fetch_non_coherent */ 658 /** \{ */ 659 unsigned non_coherent:1; 660 /** \} */ 661 662 /** \name Layout qualifiers for NV_compute_shader_derivatives */ 663 /** \{ */ 664 unsigned derivative_group:1; 665 /** \} */ 666 667 /** 668 * Flag set if GL_NV_viewport_array2 viewport_relative layout 669 * qualifier is used. 670 */ 671 unsigned viewport_relative:1; 672 } 673 /** \brief Set of flags, accessed by name. */ 674 q; 675 676 /** \brief Set of flags, accessed as a bitmask. */ 677 bitset_t i; 678 } flags; 679 680 /** Precision of the type (highp/medium/lowp). */ 681 unsigned precision:2; 682 683 /** Type of layout qualifiers for GL_AMD_conservative_depth. */ 684 unsigned depth_type:3; 685 686 /** 687 * Alignment specified via GL_ARB_enhanced_layouts "align" layout qualifier 688 */ 689 ast_expression *align; 690 691 /** Geometry shader invocations for GL_ARB_gpu_shader5. */ 692 ast_layout_expression *invocations; 693 694 /** 695 * Location specified via GL_ARB_explicit_attrib_location layout 696 * 697 * \note 698 * This field is only valid if \c explicit_location is set. 699 */ 700 ast_expression *location; 701 /** 702 * Index specified via GL_ARB_explicit_attrib_location layout 703 * 704 * \note 705 * This field is only valid if \c explicit_index is set. 706 */ 707 ast_expression *index; 708 709 /** 710 * Component specified via GL_ARB_enhaced_layouts 711 * 712 * \note 713 * This field is only valid if \c explicit_component is set. 714 */ 715 ast_expression *component; 716 717 /** Maximum output vertices in GLSL 1.50 geometry shaders. */ 718 ast_layout_expression *max_vertices; 719 720 /** Stream in GLSL 1.50 geometry shaders. */ 721 ast_expression *stream; 722 723 /** xfb_buffer specified via the GL_ARB_enhanced_layouts keyword. */ 724 ast_expression *xfb_buffer; 725 726 /** xfb_stride specified via the GL_ARB_enhanced_layouts keyword. */ 727 ast_expression *xfb_stride; 728 729 /** global xfb_stride values for each buffer */ 730 ast_layout_expression *out_xfb_stride[MAX_FEEDBACK_BUFFERS]; 731 732 /** 733 * Input or output primitive type in GLSL 1.50 geometry shaders 734 * and tessellation shaders. 735 */ 736 GLenum prim_type; 737 738 /** 739 * Binding specified via GL_ARB_shading_language_420pack's "binding" keyword. 740 * 741 * \note 742 * This field is only valid if \c explicit_binding is set. 743 */ 744 ast_expression *binding; 745 746 /** 747 * Offset specified via GL_ARB_shader_atomic_counter's or 748 * GL_ARB_enhanced_layouts "offset" keyword, or by GL_ARB_enhanced_layouts 749 * "xfb_offset" keyword. 750 * 751 * \note 752 * This field is only valid if \c explicit_offset is set. 753 */ 754 ast_expression *offset; 755 756 /** 757 * Local size specified via GL_ARB_compute_shader's "local_size_{x,y,z}" 758 * layout qualifier. Element i of this array is only valid if 759 * flags.q.local_size & (1 << i) is set. 760 */ 761 ast_layout_expression *local_size[3]; 762 763 /** Tessellation evaluation shader: vertex spacing (equal, fractional even/odd) */ 764 enum gl_tess_spacing vertex_spacing; 765 766 /** Tessellation evaluation shader: vertex ordering (CW or CCW) */ 767 GLenum ordering; 768 769 /** Tessellation evaluation shader: point mode */ 770 bool point_mode; 771 772 /** Tessellation control shader: number of output vertices */ 773 ast_layout_expression *vertices; 774 775 /** 776 * Image format specified with an ARB_shader_image_load_store 777 * layout qualifier. 778 * 779 * \note 780 * This field is only valid if \c explicit_image_format is set. 781 */ 782 enum pipe_format image_format; 783 784 /** 785 * Arrangement of invocations used to calculate derivatives in a compute 786 * shader. From NV_compute_shader_derivatives. 787 */ 788 enum gl_derivative_group derivative_group; 789 790 /** 791 * Base type of the data read from or written to this image. Only 792 * the following enumerants are allowed: GLSL_TYPE_UINT, 793 * GLSL_TYPE_INT, GLSL_TYPE_FLOAT. 794 * 795 * \note 796 * This field is only valid if \c explicit_image_format is set. 797 */ 798 glsl_base_type image_base_type; 799 800 /** 801 * Return true if and only if an interpolation qualifier is present. 802 */ 803 bool has_interpolation() const; 804 805 /** 806 * Return whether a layout qualifier is present. 807 */ 808 bool has_layout() const; 809 810 /** 811 * Return whether a storage qualifier is present. 812 */ 813 bool has_storage() const; 814 815 /** 816 * Return whether an auxiliary storage qualifier is present. 817 */ 818 bool has_auxiliary_storage() const; 819 820 /** 821 * Return true if and only if a memory qualifier is present. 822 */ 823 bool has_memory() const; 824 825 /** 826 * Return true if the qualifier is a subroutine declaration. 827 */ 828 bool is_subroutine_decl() const; 829 830 bool merge_qualifier(YYLTYPE *loc, 831 _mesa_glsl_parse_state *state, 832 const ast_type_qualifier &q, 833 bool is_single_layout_merge, 834 bool is_multiple_layouts_merge = false); 835 836 /** 837 * Validate current qualifier against the global out one. 838 */ 839 bool validate_out_qualifier(YYLTYPE *loc, 840 _mesa_glsl_parse_state *state); 841 842 /** 843 * Merge current qualifier into the global out one. 844 */ 845 bool merge_into_out_qualifier(YYLTYPE *loc, 846 _mesa_glsl_parse_state *state, 847 ast_node* &node); 848 849 /** 850 * Validate current qualifier against the global in one. 851 */ 852 bool validate_in_qualifier(YYLTYPE *loc, 853 _mesa_glsl_parse_state *state); 854 855 /** 856 * Merge current qualifier into the global in one. 857 */ 858 bool merge_into_in_qualifier(YYLTYPE *loc, 859 _mesa_glsl_parse_state *state, 860 ast_node* &node); 861 862 /** 863 * Push pending layout qualifiers to the global values. 864 */ 865 bool push_to_global(YYLTYPE *loc, 866 _mesa_glsl_parse_state *state); 867 868 bool validate_flags(YYLTYPE *loc, 869 _mesa_glsl_parse_state *state, 870 const ast_type_qualifier &allowed_flags, 871 const char *message, const char *name); 872 873 ast_subroutine_list *subroutine_list; 874 }; 875 876 class ast_declarator_list; 877 878 class ast_struct_specifier : public ast_node { 879 public: 880 ast_struct_specifier(const char *identifier, 881 ast_declarator_list *declarator_list); 882 virtual void print(void) const; 883 884 virtual ir_rvalue *hir(exec_list *instructions, 885 struct _mesa_glsl_parse_state *state); 886 887 const char *name; 888 ast_type_qualifier *layout; 889 /* List of ast_declarator_list * */ 890 exec_list declarations; 891 bool is_declaration; 892 const glsl_type *type; 893 }; 894 895 896 897 class ast_type_specifier : public ast_node { 898 public: 899 /** Construct a type specifier from a type name */ ast_type_specifier(const char * name)900 ast_type_specifier(const char *name) 901 : type(NULL), type_name(name), structure(NULL), array_specifier(NULL), 902 default_precision(ast_precision_none) 903 { 904 /* empty */ 905 } 906 907 /** Construct a type specifier from a structure definition */ ast_type_specifier(ast_struct_specifier * s)908 ast_type_specifier(ast_struct_specifier *s) 909 : type(NULL), type_name(s->name), structure(s), array_specifier(NULL), 910 default_precision(ast_precision_none) 911 { 912 /* empty */ 913 } 914 ast_type_specifier(const glsl_type * t)915 ast_type_specifier(const glsl_type *t) 916 : type(t), type_name(t->name), structure(NULL), array_specifier(NULL), 917 default_precision(ast_precision_none) 918 { 919 /* empty */ 920 } 921 922 const struct glsl_type *glsl_type(const char **name, 923 struct _mesa_glsl_parse_state *state) 924 const; 925 926 virtual void print(void) const; 927 928 ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *); 929 930 const struct glsl_type *type; 931 const char *type_name; 932 ast_struct_specifier *structure; 933 934 ast_array_specifier *array_specifier; 935 936 /** For precision statements, this is the given precision; otherwise none. */ 937 unsigned default_precision:2; 938 }; 939 940 941 class ast_fully_specified_type : public ast_node { 942 public: 943 virtual void print(void) const; 944 bool has_qualifiers(_mesa_glsl_parse_state *state) const; 945 ast_fully_specified_type()946 ast_fully_specified_type() : qualifier(), specifier(NULL) 947 { 948 } 949 950 const struct glsl_type *glsl_type(const char **name, 951 struct _mesa_glsl_parse_state *state) 952 const; 953 954 ast_type_qualifier qualifier; 955 ast_type_specifier *specifier; 956 }; 957 958 959 class ast_declarator_list : public ast_node { 960 public: 961 ast_declarator_list(ast_fully_specified_type *); 962 virtual void print(void) const; 963 964 virtual ir_rvalue *hir(exec_list *instructions, 965 struct _mesa_glsl_parse_state *state); 966 967 ast_fully_specified_type *type; 968 /** List of 'ast_declaration *' */ 969 exec_list declarations; 970 971 /** 972 * Flags for redeclarations. In these cases, no type is specified, to 973 * `type` is allowed to be NULL. In all other cases, this would be an error. 974 */ 975 int invariant; /** < `invariant` redeclaration */ 976 int precise; /** < `precise` redeclaration */ 977 }; 978 979 980 class ast_parameter_declarator : public ast_node { 981 public: ast_parameter_declarator()982 ast_parameter_declarator() : 983 type(NULL), 984 identifier(NULL), 985 array_specifier(NULL), 986 formal_parameter(false), 987 is_void(false) 988 { 989 /* empty */ 990 } 991 992 virtual void print(void) const; 993 994 virtual ir_rvalue *hir(exec_list *instructions, 995 struct _mesa_glsl_parse_state *state); 996 997 ast_fully_specified_type *type; 998 const char *identifier; 999 ast_array_specifier *array_specifier; 1000 1001 static void parameters_to_hir(exec_list *ast_parameters, 1002 bool formal, exec_list *ir_parameters, 1003 struct _mesa_glsl_parse_state *state); 1004 1005 private: 1006 /** Is this parameter declaration part of a formal parameter list? */ 1007 bool formal_parameter; 1008 1009 /** 1010 * Is this parameter 'void' type? 1011 * 1012 * This field is set by \c ::hir. 1013 */ 1014 bool is_void; 1015 }; 1016 1017 1018 class ast_function : public ast_node { 1019 public: 1020 ast_function(void); 1021 1022 virtual void print(void) const; 1023 1024 virtual ir_rvalue *hir(exec_list *instructions, 1025 struct _mesa_glsl_parse_state *state); 1026 1027 ast_fully_specified_type *return_type; 1028 const char *identifier; 1029 1030 exec_list parameters; 1031 1032 private: 1033 /** 1034 * Is this prototype part of the function definition? 1035 * 1036 * Used by ast_function_definition::hir to process the parameters, etc. 1037 * of the function. 1038 * 1039 * \sa ::hir 1040 */ 1041 bool is_definition; 1042 1043 /** 1044 * Function signature corresponding to this function prototype instance 1045 * 1046 * Used by ast_function_definition::hir to process the parameters, etc. 1047 * of the function. 1048 * 1049 * \sa ::hir 1050 */ 1051 class ir_function_signature *signature; 1052 1053 friend class ast_function_definition; 1054 }; 1055 1056 1057 class ast_expression_statement : public ast_node { 1058 public: 1059 ast_expression_statement(ast_expression *); 1060 virtual void print(void) const; 1061 1062 virtual ir_rvalue *hir(exec_list *instructions, 1063 struct _mesa_glsl_parse_state *state); 1064 1065 ast_expression *expression; 1066 }; 1067 1068 1069 class ast_case_label : public ast_node { 1070 public: 1071 ast_case_label(ast_expression *test_value); 1072 virtual void print(void) const; 1073 1074 virtual ir_rvalue *hir(exec_list *instructions, 1075 struct _mesa_glsl_parse_state *state); 1076 1077 /** 1078 * An test value of NULL means 'default'. 1079 */ 1080 ast_expression *test_value; 1081 }; 1082 1083 1084 class ast_case_label_list : public ast_node { 1085 public: 1086 ast_case_label_list(void); 1087 virtual void print(void) const; 1088 1089 virtual ir_rvalue *hir(exec_list *instructions, 1090 struct _mesa_glsl_parse_state *state); 1091 1092 /** 1093 * A list of case labels. 1094 */ 1095 exec_list labels; 1096 }; 1097 1098 1099 class ast_case_statement : public ast_node { 1100 public: 1101 ast_case_statement(ast_case_label_list *labels); 1102 virtual void print(void) const; 1103 1104 virtual ir_rvalue *hir(exec_list *instructions, 1105 struct _mesa_glsl_parse_state *state); 1106 1107 ast_case_label_list *labels; 1108 1109 /** 1110 * A list of statements. 1111 */ 1112 exec_list stmts; 1113 }; 1114 1115 1116 class ast_case_statement_list : public ast_node { 1117 public: 1118 ast_case_statement_list(void); 1119 virtual void print(void) const; 1120 1121 virtual ir_rvalue *hir(exec_list *instructions, 1122 struct _mesa_glsl_parse_state *state); 1123 1124 /** 1125 * A list of cases. 1126 */ 1127 exec_list cases; 1128 }; 1129 1130 1131 class ast_switch_body : public ast_node { 1132 public: 1133 ast_switch_body(ast_case_statement_list *stmts); 1134 virtual void print(void) const; 1135 1136 virtual ir_rvalue *hir(exec_list *instructions, 1137 struct _mesa_glsl_parse_state *state); 1138 1139 ast_case_statement_list *stmts; 1140 }; 1141 1142 1143 class ast_selection_statement : public ast_node { 1144 public: 1145 ast_selection_statement(ast_expression *condition, 1146 ast_node *then_statement, 1147 ast_node *else_statement); 1148 virtual void print(void) const; 1149 1150 virtual ir_rvalue *hir(exec_list *instructions, 1151 struct _mesa_glsl_parse_state *state); 1152 1153 ast_expression *condition; 1154 ast_node *then_statement; 1155 ast_node *else_statement; 1156 }; 1157 1158 1159 class ast_switch_statement : public ast_node { 1160 public: 1161 ast_switch_statement(ast_expression *test_expression, 1162 ast_node *body); 1163 virtual void print(void) const; 1164 1165 virtual ir_rvalue *hir(exec_list *instructions, 1166 struct _mesa_glsl_parse_state *state); 1167 1168 ast_expression *test_expression; 1169 ast_node *body; 1170 1171 protected: 1172 void test_to_hir(exec_list *, struct _mesa_glsl_parse_state *); 1173 }; 1174 1175 class ast_iteration_statement : public ast_node { 1176 public: 1177 ast_iteration_statement(int mode, ast_node *init, ast_node *condition, 1178 ast_expression *rest_expression, ast_node *body); 1179 1180 virtual void print(void) const; 1181 1182 virtual ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *); 1183 1184 enum ast_iteration_modes { 1185 ast_for, 1186 ast_while, 1187 ast_do_while 1188 } mode; 1189 1190 1191 ast_node *init_statement; 1192 ast_node *condition; 1193 ast_expression *rest_expression; 1194 1195 ast_node *body; 1196 1197 /** 1198 * Generate IR from the condition of a loop 1199 * 1200 * This is factored out of ::hir because some loops have the condition 1201 * test at the top (for and while), and others have it at the end (do-while). 1202 */ 1203 void condition_to_hir(exec_list *, struct _mesa_glsl_parse_state *); 1204 }; 1205 1206 1207 class ast_jump_statement : public ast_node { 1208 public: 1209 ast_jump_statement(int mode, ast_expression *return_value); 1210 virtual void print(void) const; 1211 1212 virtual ir_rvalue *hir(exec_list *instructions, 1213 struct _mesa_glsl_parse_state *state); 1214 1215 enum ast_jump_modes { 1216 ast_continue, 1217 ast_break, 1218 ast_return, 1219 ast_discard 1220 } mode; 1221 1222 ast_expression *opt_return_value; 1223 }; 1224 1225 1226 class ast_demote_statement : public ast_node { 1227 public: ast_demote_statement(void)1228 ast_demote_statement(void) {} 1229 virtual void print(void) const; 1230 1231 virtual ir_rvalue *hir(exec_list *instructions, 1232 struct _mesa_glsl_parse_state *state); 1233 }; 1234 1235 1236 class ast_function_definition : public ast_node { 1237 public: ast_function_definition()1238 ast_function_definition() : prototype(NULL), body(NULL) 1239 { 1240 } 1241 1242 virtual void print(void) const; 1243 1244 virtual ir_rvalue *hir(exec_list *instructions, 1245 struct _mesa_glsl_parse_state *state); 1246 1247 ast_function *prototype; 1248 ast_compound_statement *body; 1249 }; 1250 1251 class ast_interface_block : public ast_node { 1252 public: ast_interface_block(const char * instance_name,ast_array_specifier * array_specifier)1253 ast_interface_block(const char *instance_name, 1254 ast_array_specifier *array_specifier) 1255 : block_name(NULL), instance_name(instance_name), 1256 array_specifier(array_specifier) 1257 { 1258 } 1259 1260 virtual ir_rvalue *hir(exec_list *instructions, 1261 struct _mesa_glsl_parse_state *state); 1262 1263 ast_type_qualifier default_layout; 1264 ast_type_qualifier layout; 1265 const char *block_name; 1266 1267 /** 1268 * Declared name of the block instance, if specified. 1269 * 1270 * If the block does not have an instance name, this field will be 1271 * \c NULL. 1272 */ 1273 const char *instance_name; 1274 1275 /** List of ast_declarator_list * */ 1276 exec_list declarations; 1277 1278 /** 1279 * Declared array size of the block instance 1280 * 1281 * If the block is not declared as an array or if the block instance array 1282 * is unsized, this field will be \c NULL. 1283 */ 1284 ast_array_specifier *array_specifier; 1285 }; 1286 1287 1288 /** 1289 * AST node representing a declaration of the output layout for tessellation 1290 * control shaders. 1291 */ 1292 class ast_tcs_output_layout : public ast_node 1293 { 1294 public: ast_tcs_output_layout(const struct YYLTYPE & locp)1295 ast_tcs_output_layout(const struct YYLTYPE &locp) 1296 { 1297 set_location(locp); 1298 } 1299 1300 virtual ir_rvalue *hir(exec_list *instructions, 1301 struct _mesa_glsl_parse_state *state); 1302 }; 1303 1304 1305 /** 1306 * AST node representing a declaration of the input layout for geometry 1307 * shaders. 1308 */ 1309 class ast_gs_input_layout : public ast_node 1310 { 1311 public: ast_gs_input_layout(const struct YYLTYPE & locp,GLenum prim_type)1312 ast_gs_input_layout(const struct YYLTYPE &locp, GLenum prim_type) 1313 : prim_type(prim_type) 1314 { 1315 set_location(locp); 1316 } 1317 1318 virtual ir_rvalue *hir(exec_list *instructions, 1319 struct _mesa_glsl_parse_state *state); 1320 1321 private: 1322 const GLenum prim_type; 1323 }; 1324 1325 1326 /** 1327 * AST node representing a decalaration of the input layout for compute 1328 * shaders. 1329 */ 1330 class ast_cs_input_layout : public ast_node 1331 { 1332 public: ast_cs_input_layout(const struct YYLTYPE & locp,ast_layout_expression * const * local_size)1333 ast_cs_input_layout(const struct YYLTYPE &locp, 1334 ast_layout_expression *const *local_size) 1335 { 1336 for (int i = 0; i < 3; i++) { 1337 this->local_size[i] = local_size[i]; 1338 } 1339 set_location(locp); 1340 } 1341 1342 virtual ir_rvalue *hir(exec_list *instructions, 1343 struct _mesa_glsl_parse_state *state); 1344 1345 private: 1346 ast_layout_expression *local_size[3]; 1347 }; 1348 1349 class ast_warnings_toggle : public ast_node { 1350 public: ast_warnings_toggle(bool _enable)1351 ast_warnings_toggle(bool _enable) 1352 : enable(_enable) 1353 { 1354 /* empty */ 1355 } 1356 1357 virtual ir_rvalue *hir(exec_list *instructions, 1358 struct _mesa_glsl_parse_state *state); 1359 1360 private: 1361 bool enable; 1362 }; 1363 /*@}*/ 1364 1365 extern void 1366 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state); 1367 1368 extern ir_rvalue * 1369 _mesa_ast_field_selection_to_hir(const ast_expression *expr, 1370 exec_list *instructions, 1371 struct _mesa_glsl_parse_state *state); 1372 1373 extern ir_rvalue * 1374 _mesa_ast_array_index_to_hir(void *mem_ctx, 1375 struct _mesa_glsl_parse_state *state, 1376 ir_rvalue *array, ir_rvalue *idx, 1377 YYLTYPE &loc, YYLTYPE &idx_loc); 1378 1379 extern void 1380 _mesa_ast_set_aggregate_type(const glsl_type *type, 1381 ast_expression *expr); 1382 1383 void 1384 emit_function(_mesa_glsl_parse_state *state, ir_function *f); 1385 1386 extern void 1387 check_builtin_array_max_size(const char *name, unsigned size, 1388 YYLTYPE loc, struct _mesa_glsl_parse_state *state); 1389 1390 extern void _mesa_ast_process_interface_block(YYLTYPE *locp, 1391 _mesa_glsl_parse_state *state, 1392 ast_interface_block *const block, 1393 const struct ast_type_qualifier &q); 1394 1395 extern bool 1396 process_qualifier_constant(struct _mesa_glsl_parse_state *state, 1397 YYLTYPE *loc, 1398 const char *qual_indentifier, 1399 ast_expression *const_expression, 1400 unsigned *value); 1401 #endif /* AST_H */ 1402