ir.h revision 81f03393982c29f8f4165b5629c8e8fb708b97a3
1/* -*- c++ -*- */ 2/* 3 * Copyright © 2010 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 IR_H 27#define IR_H 28 29#include <cstdio> 30#include <cstdlib> 31 32extern "C" { 33#include <talloc.h> 34} 35 36#include "list.h" 37#include "ir_visitor.h" 38#include "ir_hierarchical_visitor.h" 39 40#ifndef ARRAY_SIZE 41#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) 42#endif 43 44enum ir_node_type { 45 ir_type_unset, 46 ir_type_variable, 47 ir_type_assignment, 48 ir_type_call, 49 ir_type_constant, 50 ir_type_dereference_array, 51 ir_type_dereference_record, 52 ir_type_dereference_variable, 53 ir_type_discard, 54 ir_type_expression, 55 ir_type_function, 56 ir_type_function_signature, 57 ir_type_if, 58 ir_type_loop, 59 ir_type_loop_jump, 60 ir_type_return, 61 ir_type_swizzle, 62 ir_type_texture, 63 ir_type_max /**< maximum ir_type enum number, for validation */ 64}; 65 66/** 67 * Base class of all IR instructions 68 */ 69class ir_instruction : public exec_node { 70public: 71 enum ir_node_type ir_type; 72 const struct glsl_type *type; 73 74 /** ir_print_visitor helper for debugging. */ 75 void print(void) const; 76 77 virtual void accept(ir_visitor *) = 0; 78 virtual ir_visitor_status accept(ir_hierarchical_visitor *) = 0; 79 virtual ir_instruction *clone(void *mem_ctx, 80 struct hash_table *ht) const = 0; 81 82 /** 83 * \name IR instruction downcast functions 84 * 85 * These functions either cast the object to a derived class or return 86 * \c NULL if the object's type does not match the specified derived class. 87 * Additional downcast functions will be added as needed. 88 */ 89 /*@{*/ 90 virtual class ir_variable * as_variable() { return NULL; } 91 virtual class ir_function * as_function() { return NULL; } 92 virtual class ir_dereference * as_dereference() { return NULL; } 93 virtual class ir_dereference_array * as_dereference_array() { return NULL; } 94 virtual class ir_dereference_variable *as_dereference_variable() { return NULL; } 95 virtual class ir_expression * as_expression() { return NULL; } 96 virtual class ir_rvalue * as_rvalue() { return NULL; } 97 virtual class ir_loop * as_loop() { return NULL; } 98 virtual class ir_assignment * as_assignment() { return NULL; } 99 virtual class ir_call * as_call() { return NULL; } 100 virtual class ir_return * as_return() { return NULL; } 101 virtual class ir_if * as_if() { return NULL; } 102 virtual class ir_swizzle * as_swizzle() { return NULL; } 103 virtual class ir_constant * as_constant() { return NULL; } 104 /*@}*/ 105 106protected: 107 ir_instruction() 108 { 109 ir_type = ir_type_unset; 110 type = NULL; 111 } 112}; 113 114 115class ir_rvalue : public ir_instruction { 116public: 117 virtual ir_rvalue *clone(void *mem_ctx, struct hash_table *) const = 0; 118 119 virtual ir_constant *constant_expression_value() = 0; 120 121 virtual ir_rvalue * as_rvalue() 122 { 123 return this; 124 } 125 126 virtual bool is_lvalue() 127 { 128 return false; 129 } 130 131 /** 132 * Get the variable that is ultimately referenced by an r-value 133 */ 134 virtual ir_variable *variable_referenced() 135 { 136 return NULL; 137 } 138 139 140 /** 141 * If an r-value is a reference to a whole variable, get that variable 142 * 143 * \return 144 * Pointer to a variable that is completely dereferenced by the r-value. If 145 * the r-value is not a dereference or the dereference does not access the 146 * entire variable (i.e., it's just one array element, struct field), \c NULL 147 * is returned. 148 */ 149 virtual ir_variable *whole_variable_referenced() 150 { 151 return NULL; 152 } 153 154protected: 155 ir_rvalue(); 156}; 157 158 159enum ir_variable_mode { 160 ir_var_auto = 0, 161 ir_var_uniform, 162 ir_var_in, 163 ir_var_out, 164 ir_var_inout, 165 ir_var_temporary /**< Temporary variable generated during compilation. */ 166}; 167 168enum ir_variable_interpolation { 169 ir_var_smooth = 0, 170 ir_var_flat, 171 ir_var_noperspective 172}; 173 174 175class ir_variable : public ir_instruction { 176public: 177 ir_variable(const struct glsl_type *, const char *, ir_variable_mode); 178 179 virtual ir_variable *clone(void *mem_ctx, struct hash_table *ht) const; 180 181 virtual ir_variable *as_variable() 182 { 183 return this; 184 } 185 186 virtual void accept(ir_visitor *v) 187 { 188 v->visit(this); 189 } 190 191 virtual ir_visitor_status accept(ir_hierarchical_visitor *); 192 193 194 /** 195 * Get the string value for the interpolation qualifier 196 * 197 * \return The string that would be used in a shader to specify \c 198 * mode will be returned. 199 * 200 * This function should only be used on a shader input or output variable. 201 */ 202 const char *interpolation_string() const; 203 204 /** 205 * Calculate the number of slots required to hold this variable 206 * 207 * This is used to determine how many uniform or varying locations a variable 208 * occupies. The count is in units of floating point components. 209 */ 210 unsigned component_slots() const; 211 212 const char *name; 213 214 /** 215 * Highest element accessed with a constant expression array index 216 * 217 * Not used for non-array variables. 218 */ 219 unsigned max_array_access; 220 221 unsigned read_only:1; 222 unsigned centroid:1; 223 unsigned invariant:1; 224 225 unsigned mode:3; 226 unsigned interpolation:2; 227 228 /** 229 * Flag that the whole array is assignable 230 * 231 * In GLSL 1.20 and later whole arrays are assignable (and comparable for 232 * equality). This flag enables this behavior. 233 */ 234 unsigned array_lvalue:1; 235 236 /* ARB_fragment_coord_conventions */ 237 unsigned origin_upper_left:1; 238 unsigned pixel_center_integer:1; 239 240 /** 241 * Storage location of the base of this variable 242 * 243 * The precise meaning of this field depends on the nature of the variable. 244 * 245 * - Vertex shader input: one of the values from \c gl_vert_attrib. 246 * - Vertex shader output: one of the values from \c gl_vert_result. 247 * - Fragment shader input: one of the values from \c gl_frag_attrib. 248 * - Fragment shader output: one of the values from \c gl_frag_result. 249 * - Uniforms: Per-stage uniform slot number. 250 * - Other: This field is not currently used. 251 * 252 * If the variable is a uniform, shader input, or shader output, and the 253 * slot has not been assigned, the value will be -1. 254 */ 255 int location; 256 257 /** 258 * Emit a warning if this variable is accessed. 259 */ 260 const char *warn_extension; 261 262 /** 263 * Value assigned in the initializer of a variable declared "const" 264 */ 265 ir_constant *constant_value; 266}; 267 268 269/*@{*/ 270/** 271 * The representation of a function instance; may be the full definition or 272 * simply a prototype. 273 */ 274class ir_function_signature : public ir_instruction { 275 /* An ir_function_signature will be part of the list of signatures in 276 * an ir_function. 277 */ 278public: 279 ir_function_signature(const glsl_type *return_type); 280 281 virtual ir_function_signature *clone(void *mem_ctx, 282 struct hash_table *ht) const; 283 284 virtual void accept(ir_visitor *v) 285 { 286 v->visit(this); 287 } 288 289 virtual ir_visitor_status accept(ir_hierarchical_visitor *); 290 291 /** 292 * Get the name of the function for which this is a signature 293 */ 294 const char *function_name() const; 295 296 /** 297 * Get a handle to the function for which this is a signature 298 * 299 * There is no setter function, this function returns a \c const pointer, 300 * and \c ir_function_signature::_function is private for a reason. The 301 * only way to make a connection between a function and function signature 302 * is via \c ir_function::add_signature. This helps ensure that certain 303 * invariants (i.e., a function signature is in the list of signatures for 304 * its \c _function) are met. 305 * 306 * \sa ir_function::add_signature 307 */ 308 inline const class ir_function *function() const 309 { 310 return this->_function; 311 } 312 313 /** 314 * Check whether the qualifiers match between this signature's parameters 315 * and the supplied parameter list. If not, returns the name of the first 316 * parameter with mismatched qualifiers (for use in error messages). 317 */ 318 const char *qualifiers_match(exec_list *params); 319 320 /** 321 * Replace the current parameter list with the given one. This is useful 322 * if the current information came from a prototype, and either has invalid 323 * or missing parameter names. 324 */ 325 void replace_parameters(exec_list *new_params); 326 327 /** 328 * Function return type. 329 * 330 * \note This discards the optional precision qualifier. 331 */ 332 const struct glsl_type *return_type; 333 334 /** 335 * List of ir_variable of function parameters. 336 * 337 * This represents the storage. The paramaters passed in a particular 338 * call will be in ir_call::actual_paramaters. 339 */ 340 struct exec_list parameters; 341 342 /** Whether or not this function has a body (which may be empty). */ 343 unsigned is_defined:1; 344 345 /** Whether or not this function signature is a built-in. */ 346 unsigned is_builtin:1; 347 348 /** Body of instructions in the function. */ 349 struct exec_list body; 350 351private: 352 /** Function of which this signature is one overload. */ 353 class ir_function *_function; 354 355 friend class ir_function; 356}; 357 358 359/** 360 * Header for tracking multiple overloaded functions with the same name. 361 * Contains a list of ir_function_signatures representing each of the 362 * actual functions. 363 */ 364class ir_function : public ir_instruction { 365public: 366 ir_function(const char *name); 367 368 virtual ir_function *clone(void *mem_ctx, struct hash_table *ht) const; 369 370 virtual ir_function *as_function() 371 { 372 return this; 373 } 374 375 virtual void accept(ir_visitor *v) 376 { 377 v->visit(this); 378 } 379 380 virtual ir_visitor_status accept(ir_hierarchical_visitor *); 381 382 void add_signature(ir_function_signature *sig) 383 { 384 sig->_function = this; 385 this->signatures.push_tail(sig); 386 } 387 388 /** 389 * Get an iterator for the set of function signatures 390 */ 391 exec_list_iterator iterator() 392 { 393 return signatures.iterator(); 394 } 395 396 /** 397 * Find a signature that matches a set of actual parameters, taking implicit 398 * conversions into account. 399 */ 400 ir_function_signature *matching_signature(const exec_list *actual_param); 401 402 /** 403 * Find a signature that exactly matches a set of actual parameters without 404 * any implicit type conversions. 405 */ 406 ir_function_signature *exact_matching_signature(const exec_list *actual_ps); 407 408 /** 409 * Name of the function. 410 */ 411 const char *name; 412 413 /** Whether or not this function has a signature that isn't a built-in. */ 414 bool has_user_signature(); 415 416 /** 417 * List of ir_function_signature for each overloaded function with this name. 418 */ 419 struct exec_list signatures; 420}; 421 422inline const char *ir_function_signature::function_name() const 423{ 424 return this->_function->name; 425} 426/*@}*/ 427 428 429/** 430 * IR instruction representing high-level if-statements 431 */ 432class ir_if : public ir_instruction { 433public: 434 ir_if(ir_rvalue *condition) 435 : condition(condition) 436 { 437 ir_type = ir_type_if; 438 } 439 440 virtual ir_if *clone(void *mem_ctx, struct hash_table *ht) const; 441 442 virtual ir_if *as_if() 443 { 444 return this; 445 } 446 447 virtual void accept(ir_visitor *v) 448 { 449 v->visit(this); 450 } 451 452 virtual ir_visitor_status accept(ir_hierarchical_visitor *); 453 454 ir_rvalue *condition; 455 /** List of ir_instruction for the body of the then branch */ 456 exec_list then_instructions; 457 /** List of ir_instruction for the body of the else branch */ 458 exec_list else_instructions; 459}; 460 461 462/** 463 * IR instruction representing a high-level loop structure. 464 */ 465class ir_loop : public ir_instruction { 466public: 467 ir_loop(); 468 469 virtual ir_loop *clone(void *mem_ctx, struct hash_table *ht) const; 470 471 virtual void accept(ir_visitor *v) 472 { 473 v->visit(this); 474 } 475 476 virtual ir_visitor_status accept(ir_hierarchical_visitor *); 477 478 virtual ir_loop *as_loop() 479 { 480 return this; 481 } 482 483 /** 484 * Get an iterator for the instructions of the loop body 485 */ 486 exec_list_iterator iterator() 487 { 488 return body_instructions.iterator(); 489 } 490 491 /** List of ir_instruction that make up the body of the loop. */ 492 exec_list body_instructions; 493 494 /** 495 * \name Loop counter and controls 496 * 497 * Represents a loop like a FORTRAN \c do-loop. 498 * 499 * \note 500 * If \c from and \c to are the same value, the loop will execute once. 501 */ 502 /*@{*/ 503 ir_rvalue *from; /** Value of the loop counter on the first 504 * iteration of the loop. 505 */ 506 ir_rvalue *to; /** Value of the loop counter on the last 507 * iteration of the loop. 508 */ 509 ir_rvalue *increment; 510 ir_variable *counter; 511 512 /** 513 * Comparison operation in the loop terminator. 514 * 515 * If any of the loop control fields are non-\c NULL, this field must be 516 * one of \c ir_binop_less, \c ir_binop_greater, \c ir_binop_lequal, 517 * \c ir_binop_gequal, \c ir_binop_equal, or \c ir_binop_nequal. 518 */ 519 int cmp; 520 /*@}*/ 521}; 522 523 524class ir_assignment : public ir_instruction { 525public: 526 ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition); 527 528 /** 529 * Construct an assignment with an explicit write mask 530 * 531 * \note 532 * Since a write mask is supplied, the LHS must already be a bare 533 * \c ir_dereference. The cannot be any swizzles in the LHS. 534 */ 535 ir_assignment(ir_dereference *lhs, ir_rvalue *rhs, ir_rvalue *condition, 536 unsigned write_mask); 537 538 virtual ir_assignment *clone(void *mem_ctx, struct hash_table *ht) const; 539 540 virtual ir_constant *constant_expression_value(); 541 542 virtual void accept(ir_visitor *v) 543 { 544 v->visit(this); 545 } 546 547 virtual ir_visitor_status accept(ir_hierarchical_visitor *); 548 549 virtual ir_assignment * as_assignment() 550 { 551 return this; 552 } 553 554 /** 555 * Get a whole variable written by an assignment 556 * 557 * If the LHS of the assignment writes a whole variable, the variable is 558 * returned. Otherwise \c NULL is returned. Examples of whole-variable 559 * assignment are: 560 * 561 * - Assigning to a scalar 562 * - Assigning to all components of a vector 563 * - Whole array (or matrix) assignment 564 * - Whole structure assignment 565 */ 566 ir_variable *whole_variable_written(); 567 568 /** 569 * Set the LHS of an assignment 570 */ 571 void set_lhs(ir_rvalue *lhs); 572 573 /** 574 * Left-hand side of the assignment. 575 * 576 * This should be treated as read only. If you need to set the LHS of an 577 * assignment, use \c ir_assignment::set_lhs. 578 */ 579 ir_dereference *lhs; 580 581 /** 582 * Value being assigned 583 */ 584 ir_rvalue *rhs; 585 586 /** 587 * Optional condition for the assignment. 588 */ 589 ir_rvalue *condition; 590 591 592 /** 593 * Component mask written 594 * 595 * For non-vector types in the LHS, this field will be zero. For vector 596 * types, a bit will be set for each component that is written. Note that 597 * for \c vec2 and \c vec3 types only the lower bits will ever be set. 598 */ 599 unsigned write_mask:4; 600}; 601 602/* Update ir_expression::num_operands() and operator_strs when 603 * updating this list. 604 */ 605enum ir_expression_operation { 606 ir_unop_bit_not, 607 ir_unop_logic_not, 608 ir_unop_neg, 609 ir_unop_abs, 610 ir_unop_sign, 611 ir_unop_rcp, 612 ir_unop_rsq, 613 ir_unop_sqrt, 614 ir_unop_exp, /**< Log base e on gentype */ 615 ir_unop_log, /**< Natural log on gentype */ 616 ir_unop_exp2, 617 ir_unop_log2, 618 ir_unop_f2i, /**< Float-to-integer conversion. */ 619 ir_unop_i2f, /**< Integer-to-float conversion. */ 620 ir_unop_f2b, /**< Float-to-boolean conversion */ 621 ir_unop_b2f, /**< Boolean-to-float conversion */ 622 ir_unop_i2b, /**< int-to-boolean conversion */ 623 ir_unop_b2i, /**< Boolean-to-int conversion */ 624 ir_unop_u2f, /**< Unsigned-to-float conversion. */ 625 ir_unop_any, 626 627 /** 628 * \name Unary floating-point rounding operations. 629 */ 630 /*@{*/ 631 ir_unop_trunc, 632 ir_unop_ceil, 633 ir_unop_floor, 634 ir_unop_fract, 635 /*@}*/ 636 637 /** 638 * \name Trigonometric operations. 639 */ 640 /*@{*/ 641 ir_unop_sin, 642 ir_unop_cos, 643 /*@}*/ 644 645 /** 646 * \name Partial derivatives. 647 */ 648 /*@{*/ 649 ir_unop_dFdx, 650 ir_unop_dFdy, 651 /*@}*/ 652 653 ir_unop_noise, 654 655 ir_binop_add, 656 ir_binop_sub, 657 ir_binop_mul, 658 ir_binop_div, 659 660 /** 661 * Takes one of two combinations of arguments: 662 * 663 * - mod(vecN, vecN) 664 * - mod(vecN, float) 665 * 666 * Does not take integer types. 667 */ 668 ir_binop_mod, 669 670 /** 671 * \name Binary comparison operators 672 */ 673 /*@{*/ 674 ir_binop_less, 675 ir_binop_greater, 676 ir_binop_lequal, 677 ir_binop_gequal, 678 ir_binop_equal, 679 ir_binop_nequal, 680 /** 681 * Returns single boolean for whether all components of operands[0] 682 * equal the components of operands[1]. 683 */ 684 ir_binop_all_equal, 685 /** 686 * Returns single boolean for whether any component of operands[0] 687 * is not equal to the corresponding component of operands[1]. 688 */ 689 ir_binop_any_nequal, 690 /*@}*/ 691 692 /** 693 * \name Bit-wise binary operations. 694 */ 695 /*@{*/ 696 ir_binop_lshift, 697 ir_binop_rshift, 698 ir_binop_bit_and, 699 ir_binop_bit_xor, 700 ir_binop_bit_or, 701 /*@}*/ 702 703 ir_binop_logic_and, 704 ir_binop_logic_xor, 705 ir_binop_logic_or, 706 707 ir_binop_dot, 708 ir_binop_cross, 709 ir_binop_min, 710 ir_binop_max, 711 712 ir_binop_pow 713}; 714 715class ir_expression : public ir_rvalue { 716public: 717 ir_expression(int op, const struct glsl_type *type, 718 ir_rvalue *, ir_rvalue *); 719 720 virtual ir_expression *as_expression() 721 { 722 return this; 723 } 724 725 virtual ir_expression *clone(void *mem_ctx, struct hash_table *ht) const; 726 727 virtual ir_constant *constant_expression_value(); 728 729 static unsigned int get_num_operands(ir_expression_operation); 730 unsigned int get_num_operands() const 731 { 732 return get_num_operands(operation); 733 } 734 735 /** 736 * Return a string representing this expression's operator. 737 */ 738 const char *operator_string(); 739 740 /** 741 * Return a string representing this expression's operator. 742 */ 743 static const char *operator_string(ir_expression_operation); 744 745 746 /** 747 * Do a reverse-lookup to translate the given string into an operator. 748 */ 749 static ir_expression_operation get_operator(const char *); 750 751 virtual void accept(ir_visitor *v) 752 { 753 v->visit(this); 754 } 755 756 virtual ir_visitor_status accept(ir_hierarchical_visitor *); 757 758 ir_expression_operation operation; 759 ir_rvalue *operands[2]; 760}; 761 762 763/** 764 * IR instruction representing a function call 765 */ 766class ir_call : public ir_rvalue { 767public: 768 ir_call(ir_function_signature *callee, exec_list *actual_parameters) 769 : callee(callee) 770 { 771 ir_type = ir_type_call; 772 assert(callee->return_type != NULL); 773 type = callee->return_type; 774 actual_parameters->move_nodes_to(& this->actual_parameters); 775 } 776 777 virtual ir_call *clone(void *mem_ctx, struct hash_table *ht) const; 778 779 virtual ir_constant *constant_expression_value(); 780 781 virtual ir_call *as_call() 782 { 783 return this; 784 } 785 786 virtual void accept(ir_visitor *v) 787 { 788 v->visit(this); 789 } 790 791 virtual ir_visitor_status accept(ir_hierarchical_visitor *); 792 793 /** 794 * Get a generic ir_call object when an error occurs 795 * 796 * Any allocation will be performed with 'ctx' as talloc owner. 797 */ 798 static ir_call *get_error_instruction(void *ctx); 799 800 /** 801 * Get an iterator for the set of acutal parameters 802 */ 803 exec_list_iterator iterator() 804 { 805 return actual_parameters.iterator(); 806 } 807 808 /** 809 * Get the name of the function being called. 810 */ 811 const char *callee_name() const 812 { 813 return callee->function_name(); 814 } 815 816 ir_function_signature *get_callee() 817 { 818 return callee; 819 } 820 821 /** 822 * Set the function call target 823 */ 824 void set_callee(ir_function_signature *sig); 825 826 /** 827 * Generates an inline version of the function before @ir, 828 * returning the return value of the function. 829 */ 830 ir_rvalue *generate_inline(ir_instruction *ir); 831 832 /* List of ir_rvalue of paramaters passed in this call. */ 833 exec_list actual_parameters; 834 835private: 836 ir_call() 837 : callee(NULL) 838 { 839 this->ir_type = ir_type_call; 840 } 841 842 ir_function_signature *callee; 843}; 844 845 846/** 847 * \name Jump-like IR instructions. 848 * 849 * These include \c break, \c continue, \c return, and \c discard. 850 */ 851/*@{*/ 852class ir_jump : public ir_instruction { 853protected: 854 ir_jump() 855 { 856 ir_type = ir_type_unset; 857 } 858}; 859 860class ir_return : public ir_jump { 861public: 862 ir_return() 863 : value(NULL) 864 { 865 this->ir_type = ir_type_return; 866 } 867 868 ir_return(ir_rvalue *value) 869 : value(value) 870 { 871 this->ir_type = ir_type_return; 872 } 873 874 virtual ir_return *clone(void *mem_ctx, struct hash_table *) const; 875 876 virtual ir_return *as_return() 877 { 878 return this; 879 } 880 881 ir_rvalue *get_value() const 882 { 883 return value; 884 } 885 886 virtual void accept(ir_visitor *v) 887 { 888 v->visit(this); 889 } 890 891 virtual ir_visitor_status accept(ir_hierarchical_visitor *); 892 893 ir_rvalue *value; 894}; 895 896 897/** 898 * Jump instructions used inside loops 899 * 900 * These include \c break and \c continue. The \c break within a loop is 901 * different from the \c break within a switch-statement. 902 * 903 * \sa ir_switch_jump 904 */ 905class ir_loop_jump : public ir_jump { 906public: 907 enum jump_mode { 908 jump_break, 909 jump_continue 910 }; 911 912 ir_loop_jump(jump_mode mode) 913 { 914 this->ir_type = ir_type_loop_jump; 915 this->mode = mode; 916 this->loop = loop; 917 } 918 919 virtual ir_loop_jump *clone(void *mem_ctx, struct hash_table *) const; 920 921 virtual void accept(ir_visitor *v) 922 { 923 v->visit(this); 924 } 925 926 virtual ir_visitor_status accept(ir_hierarchical_visitor *); 927 928 bool is_break() const 929 { 930 return mode == jump_break; 931 } 932 933 bool is_continue() const 934 { 935 return mode == jump_continue; 936 } 937 938 /** Mode selector for the jump instruction. */ 939 enum jump_mode mode; 940private: 941 /** Loop containing this break instruction. */ 942 ir_loop *loop; 943}; 944 945/** 946 * IR instruction representing discard statements. 947 */ 948class ir_discard : public ir_jump { 949public: 950 ir_discard() 951 { 952 this->ir_type = ir_type_discard; 953 this->condition = NULL; 954 } 955 956 ir_discard(ir_rvalue *cond) 957 { 958 this->ir_type = ir_type_discard; 959 this->condition = cond; 960 } 961 962 virtual ir_discard *clone(void *mem_ctx, struct hash_table *ht) const; 963 964 virtual void accept(ir_visitor *v) 965 { 966 v->visit(this); 967 } 968 969 virtual ir_visitor_status accept(ir_hierarchical_visitor *); 970 971 ir_rvalue *condition; 972}; 973/*@}*/ 974 975 976/** 977 * Texture sampling opcodes used in ir_texture 978 */ 979enum ir_texture_opcode { 980 ir_tex, /* Regular texture look-up */ 981 ir_txb, /* Texture look-up with LOD bias */ 982 ir_txl, /* Texture look-up with explicit LOD */ 983 ir_txd, /* Texture look-up with partial derivatvies */ 984 ir_txf /* Texel fetch with explicit LOD */ 985}; 986 987 988/** 989 * IR instruction to sample a texture 990 * 991 * The specific form of the IR instruction depends on the \c mode value 992 * selected from \c ir_texture_opcodes. In the printed IR, these will 993 * appear as: 994 * 995 * Texel offset 996 * | Projection divisor 997 * | | Shadow comparitor 998 * | | | 999 * v v v 1000 * (tex (sampler) (coordinate) (0 0 0) (1) ( )) 1001 * (txb (sampler) (coordinate) (0 0 0) (1) ( ) (bias)) 1002 * (txl (sampler) (coordinate) (0 0 0) (1) ( ) (lod)) 1003 * (txd (sampler) (coordinate) (0 0 0) (1) ( ) (dPdx dPdy)) 1004 * (txf (sampler) (coordinate) (0 0 0) (lod)) 1005 */ 1006class ir_texture : public ir_rvalue { 1007public: 1008 ir_texture(enum ir_texture_opcode op) 1009 : op(op), projector(NULL), shadow_comparitor(NULL) 1010 { 1011 this->ir_type = ir_type_texture; 1012 } 1013 1014 virtual ir_texture *clone(void *mem_ctx, struct hash_table *) const; 1015 1016 virtual ir_constant *constant_expression_value(); 1017 1018 virtual void accept(ir_visitor *v) 1019 { 1020 v->visit(this); 1021 } 1022 1023 virtual ir_visitor_status accept(ir_hierarchical_visitor *); 1024 1025 /** 1026 * Return a string representing the ir_texture_opcode. 1027 */ 1028 const char *opcode_string(); 1029 1030 /** Set the sampler and infer the type. */ 1031 void set_sampler(ir_dereference *sampler); 1032 1033 /** 1034 * Do a reverse-lookup to translate a string into an ir_texture_opcode. 1035 */ 1036 static ir_texture_opcode get_opcode(const char *); 1037 1038 enum ir_texture_opcode op; 1039 1040 /** Sampler to use for the texture access. */ 1041 ir_dereference *sampler; 1042 1043 /** Texture coordinate to sample */ 1044 ir_rvalue *coordinate; 1045 1046 /** 1047 * Value used for projective divide. 1048 * 1049 * If there is no projective divide (the common case), this will be 1050 * \c NULL. Optimization passes should check for this to point to a constant 1051 * of 1.0 and replace that with \c NULL. 1052 */ 1053 ir_rvalue *projector; 1054 1055 /** 1056 * Coordinate used for comparison on shadow look-ups. 1057 * 1058 * If there is no shadow comparison, this will be \c NULL. For the 1059 * \c ir_txf opcode, this *must* be \c NULL. 1060 */ 1061 ir_rvalue *shadow_comparitor; 1062 1063 /** Explicit texel offsets. */ 1064 signed char offsets[3]; 1065 1066 union { 1067 ir_rvalue *lod; /**< Floating point LOD */ 1068 ir_rvalue *bias; /**< Floating point LOD bias */ 1069 struct { 1070 ir_rvalue *dPdx; /**< Partial derivative of coordinate wrt X */ 1071 ir_rvalue *dPdy; /**< Partial derivative of coordinate wrt Y */ 1072 } grad; 1073 } lod_info; 1074}; 1075 1076 1077struct ir_swizzle_mask { 1078 unsigned x:2; 1079 unsigned y:2; 1080 unsigned z:2; 1081 unsigned w:2; 1082 1083 /** 1084 * Number of components in the swizzle. 1085 */ 1086 unsigned num_components:3; 1087 1088 /** 1089 * Does the swizzle contain duplicate components? 1090 * 1091 * L-value swizzles cannot contain duplicate components. 1092 */ 1093 unsigned has_duplicates:1; 1094}; 1095 1096 1097class ir_swizzle : public ir_rvalue { 1098public: 1099 ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w, 1100 unsigned count); 1101 1102 ir_swizzle(ir_rvalue *val, const unsigned *components, unsigned count); 1103 1104 ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask); 1105 1106 virtual ir_swizzle *clone(void *mem_ctx, struct hash_table *) const; 1107 1108 virtual ir_constant *constant_expression_value(); 1109 1110 virtual ir_swizzle *as_swizzle() 1111 { 1112 return this; 1113 } 1114 1115 /** 1116 * Construct an ir_swizzle from the textual representation. Can fail. 1117 */ 1118 static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length); 1119 1120 virtual void accept(ir_visitor *v) 1121 { 1122 v->visit(this); 1123 } 1124 1125 virtual ir_visitor_status accept(ir_hierarchical_visitor *); 1126 1127 bool is_lvalue() 1128 { 1129 return val->is_lvalue() && !mask.has_duplicates; 1130 } 1131 1132 /** 1133 * Get the variable that is ultimately referenced by an r-value 1134 */ 1135 virtual ir_variable *variable_referenced(); 1136 1137 ir_rvalue *val; 1138 ir_swizzle_mask mask; 1139 1140private: 1141 /** 1142 * Initialize the mask component of a swizzle 1143 * 1144 * This is used by the \c ir_swizzle constructors. 1145 */ 1146 void init_mask(const unsigned *components, unsigned count); 1147}; 1148 1149 1150class ir_dereference : public ir_rvalue { 1151public: 1152 virtual ir_dereference *clone(void *mem_ctx, struct hash_table *) const = 0; 1153 1154 virtual ir_dereference *as_dereference() 1155 { 1156 return this; 1157 } 1158 1159 bool is_lvalue(); 1160 1161 /** 1162 * Get the variable that is ultimately referenced by an r-value 1163 */ 1164 virtual ir_variable *variable_referenced() = 0; 1165}; 1166 1167 1168class ir_dereference_variable : public ir_dereference { 1169public: 1170 ir_dereference_variable(ir_variable *var); 1171 1172 virtual ir_dereference_variable *clone(void *mem_ctx, 1173 struct hash_table *) const; 1174 1175 virtual ir_constant *constant_expression_value(); 1176 1177 virtual ir_dereference_variable *as_dereference_variable() 1178 { 1179 return this; 1180 } 1181 1182 /** 1183 * Get the variable that is ultimately referenced by an r-value 1184 */ 1185 virtual ir_variable *variable_referenced() 1186 { 1187 return this->var; 1188 } 1189 1190 virtual ir_variable *whole_variable_referenced() 1191 { 1192 /* ir_dereference_variable objects always dereference the entire 1193 * variable. However, if this dereference is dereferenced by anything 1194 * else, the complete deferefernce chain is not a whole-variable 1195 * dereference. This method should only be called on the top most 1196 * ir_rvalue in a dereference chain. 1197 */ 1198 return this->var; 1199 } 1200 1201 virtual void accept(ir_visitor *v) 1202 { 1203 v->visit(this); 1204 } 1205 1206 virtual ir_visitor_status accept(ir_hierarchical_visitor *); 1207 1208 /** 1209 * Object being dereferenced. 1210 */ 1211 ir_variable *var; 1212}; 1213 1214 1215class ir_dereference_array : public ir_dereference { 1216public: 1217 ir_dereference_array(ir_rvalue *value, ir_rvalue *array_index); 1218 1219 ir_dereference_array(ir_variable *var, ir_rvalue *array_index); 1220 1221 virtual ir_dereference_array *clone(void *mem_ctx, 1222 struct hash_table *) const; 1223 1224 virtual ir_constant *constant_expression_value(); 1225 1226 virtual ir_dereference_array *as_dereference_array() 1227 { 1228 return this; 1229 } 1230 1231 /** 1232 * Get the variable that is ultimately referenced by an r-value 1233 */ 1234 virtual ir_variable *variable_referenced() 1235 { 1236 return this->array->variable_referenced(); 1237 } 1238 1239 virtual void accept(ir_visitor *v) 1240 { 1241 v->visit(this); 1242 } 1243 1244 virtual ir_visitor_status accept(ir_hierarchical_visitor *); 1245 1246 ir_rvalue *array; 1247 ir_rvalue *array_index; 1248 1249private: 1250 void set_array(ir_rvalue *value); 1251}; 1252 1253 1254class ir_dereference_record : public ir_dereference { 1255public: 1256 ir_dereference_record(ir_rvalue *value, const char *field); 1257 1258 ir_dereference_record(ir_variable *var, const char *field); 1259 1260 virtual ir_dereference_record *clone(void *mem_ctx, 1261 struct hash_table *) const; 1262 1263 virtual ir_constant *constant_expression_value(); 1264 1265 /** 1266 * Get the variable that is ultimately referenced by an r-value 1267 */ 1268 virtual ir_variable *variable_referenced() 1269 { 1270 return this->record->variable_referenced(); 1271 } 1272 1273 virtual void accept(ir_visitor *v) 1274 { 1275 v->visit(this); 1276 } 1277 1278 virtual ir_visitor_status accept(ir_hierarchical_visitor *); 1279 1280 ir_rvalue *record; 1281 const char *field; 1282}; 1283 1284 1285/** 1286 * Data stored in an ir_constant 1287 */ 1288union ir_constant_data { 1289 unsigned u[16]; 1290 int i[16]; 1291 float f[16]; 1292 bool b[16]; 1293}; 1294 1295 1296class ir_constant : public ir_rvalue { 1297public: 1298 ir_constant(const struct glsl_type *type, const ir_constant_data *data); 1299 ir_constant(bool b); 1300 ir_constant(unsigned int u); 1301 ir_constant(int i); 1302 ir_constant(float f); 1303 1304 /** 1305 * Construct an ir_constant from a list of ir_constant values 1306 */ 1307 ir_constant(const struct glsl_type *type, exec_list *values); 1308 1309 /** 1310 * Construct an ir_constant from a scalar component of another ir_constant 1311 * 1312 * The new \c ir_constant inherits the type of the component from the 1313 * source constant. 1314 * 1315 * \note 1316 * In the case of a matrix constant, the new constant is a scalar, \b not 1317 * a vector. 1318 */ 1319 ir_constant(const ir_constant *c, unsigned i); 1320 1321 /** 1322 * Return a new ir_constant of the specified type containing all zeros. 1323 */ 1324 static ir_constant *zero(void *mem_ctx, const glsl_type *type); 1325 1326 virtual ir_constant *clone(void *mem_ctx, struct hash_table *) const; 1327 1328 virtual ir_constant *constant_expression_value(); 1329 1330 virtual ir_constant *as_constant() 1331 { 1332 return this; 1333 } 1334 1335 virtual void accept(ir_visitor *v) 1336 { 1337 v->visit(this); 1338 } 1339 1340 virtual ir_visitor_status accept(ir_hierarchical_visitor *); 1341 1342 /** 1343 * Get a particular component of a constant as a specific type 1344 * 1345 * This is useful, for example, to get a value from an integer constant 1346 * as a float or bool. This appears frequently when constructors are 1347 * called with all constant parameters. 1348 */ 1349 /*@{*/ 1350 bool get_bool_component(unsigned i) const; 1351 float get_float_component(unsigned i) const; 1352 int get_int_component(unsigned i) const; 1353 unsigned get_uint_component(unsigned i) const; 1354 /*@}*/ 1355 1356 ir_constant *get_array_element(unsigned i) const; 1357 1358 ir_constant *get_record_field(const char *name); 1359 1360 /** 1361 * Determine whether a constant has the same value as another constant 1362 */ 1363 bool has_value(const ir_constant *) const; 1364 1365 /** 1366 * Value of the constant. 1367 * 1368 * The field used to back the values supplied by the constant is determined 1369 * by the type associated with the \c ir_instruction. Constants may be 1370 * scalars, vectors, or matrices. 1371 */ 1372 union ir_constant_data value; 1373 1374 /* Array elements */ 1375 ir_constant **array_elements; 1376 1377 /* Structure fields */ 1378 exec_list components; 1379 1380private: 1381 /** 1382 * Parameterless constructor only used by the clone method 1383 */ 1384 ir_constant(void); 1385}; 1386 1387void 1388visit_exec_list(exec_list *list, ir_visitor *visitor); 1389 1390void validate_ir_tree(exec_list *instructions); 1391 1392/** 1393 * Make a clone of each IR instruction in a list 1394 * 1395 * \param in List of IR instructions that are to be cloned 1396 * \param out List to hold the cloned instructions 1397 */ 1398void 1399clone_ir_list(void *mem_ctx, exec_list *out, const exec_list *in); 1400 1401extern void 1402_mesa_glsl_initialize_variables(exec_list *instructions, 1403 struct _mesa_glsl_parse_state *state); 1404 1405extern void 1406_mesa_glsl_initialize_functions(exec_list *instructions, 1407 struct _mesa_glsl_parse_state *state); 1408 1409extern void 1410_mesa_glsl_release_functions(void); 1411 1412extern void 1413reparent_ir(exec_list *list, void *mem_ctx); 1414 1415struct glsl_symbol_table; 1416 1417extern void 1418import_prototypes(const exec_list *source, exec_list *dest, 1419 struct glsl_symbol_table *symbols, void *mem_ctx); 1420 1421extern bool 1422ir_has_call(ir_instruction *ir); 1423 1424extern void 1425do_set_program_inouts(exec_list *instructions, struct gl_program *prog); 1426 1427#endif /* IR_H */ 1428