3 * Copyright © 2010 Intel Corporation
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:
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
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.
33 #include "ir_visitor.h"
34 #include "ir_hierarchical_visitor.h"
41 * Base class of all IR instructions
43 class ir_instruction
: public exec_node
{
45 const struct glsl_type
*type
;
47 class ir_constant
*constant_expression_value();
49 /** ir_print_visitor helper for debugging. */
52 virtual void accept(ir_visitor
*) = 0;
53 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*) = 0;
56 * \name IR instruction downcast functions
58 * These functions either cast the object to a derived class or return
59 * \c NULL if the object's type does not match the specified derived class.
60 * Additional downcast functions will be added as needed.
63 virtual class ir_variable
* as_variable() { return NULL
; }
64 virtual class ir_function
* as_function() { return NULL
; }
65 virtual class ir_dereference
* as_dereference() { return NULL
; }
66 virtual class ir_dereference_array
* as_dereference_array() { return NULL
; }
67 virtual class ir_rvalue
* as_rvalue() { return NULL
; }
68 virtual class ir_loop
* as_loop() { return NULL
; }
69 virtual class ir_assignment
* as_assignment() { return NULL
; }
70 virtual class ir_call
* as_call() { return NULL
; }
71 virtual class ir_return
* as_return() { return NULL
; }
72 virtual class ir_if
* as_if() { return NULL
; }
73 virtual class ir_swizzle
* as_swizzle() { return NULL
; }
74 virtual class ir_constant
* as_constant() { return NULL
; }
85 class ir_rvalue
: public ir_instruction
{
87 virtual ir_rvalue
* as_rvalue()
92 virtual bool is_lvalue()
98 * Get the variable that is ultimately referenced by an r-value
100 virtual ir_variable
*variable_referenced()
107 * If an r-value is a reference to a whole variable, get that variable
110 * Pointer to a variable that is completely dereferenced by the r-value. If
111 * the r-value is not a dereference or the dereference does not access the
112 * entire variable (i.e., it's just one array element, struct field), \c NULL
115 virtual ir_variable
*whole_variable_referenced()
128 enum ir_variable_mode
{
136 enum ir_varaible_interpolation
{
143 class ir_variable
: public ir_instruction
{
145 ir_variable(const struct glsl_type
*, const char *);
147 virtual ir_variable
*as_variable()
152 virtual void accept(ir_visitor
*v
)
157 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
160 * Duplicate an IR variable
163 * This will probably be made \c virtual and moved to the base class
166 ir_variable
*clone() const
168 ir_variable
*var
= new ir_variable(type
, name
);
170 var
->max_array_access
= this->max_array_access
;
171 var
->read_only
= this->read_only
;
172 var
->centroid
= this->centroid
;
173 var
->invariant
= this->invariant
;
174 var
->mode
= this->mode
;
175 var
->interpolation
= this->interpolation
;
183 * Highest element accessed with a constant expression array index
185 * Not used for non-array variables.
187 unsigned max_array_access
;
189 unsigned read_only
:1;
191 unsigned invariant
:1;
192 /** If the variable is initialized outside of the scope of the shader */
193 unsigned shader_in
:1;
195 * If the variable value is later used outside of the scope of the shader.
197 unsigned shader_out
:1;
200 unsigned interpolation
:2;
203 * Flag that the whole array is assignable
205 * In GLSL 1.20 and later whole arrays are assignable (and comparable for
206 * equality). This flag enables this behavior.
208 unsigned array_lvalue
:1;
211 * Emit a warning if this variable is accessed.
213 const char *warn_extension
;
216 * Value assigned in the initializer of a variable declared "const"
218 ir_constant
*constant_value
;
224 * The representation of a function instance; may be the full definition or
225 * simply a prototype.
227 class ir_function_signature
: public ir_instruction
{
228 /* An ir_function_signature will be part of the list of signatures in
232 ir_function_signature(const glsl_type
*return_type
);
234 virtual void accept(ir_visitor
*v
)
239 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
242 * Get the name of the function for which this is a signature
244 const char *function_name() const;
247 * Check whether the qualifiers match between this signature's parameters
248 * and the supplied parameter list. If not, returns the name of the first
249 * parameter with mismatched qualifiers (for use in error messages).
251 const char *qualifiers_match(exec_list
*params
);
254 * Replace the current parameter list with the given one. This is useful
255 * if the current information came from a prototype, and either has invalid
256 * or missing parameter names.
258 void replace_parameters(exec_list
*new_params
);
261 * Function return type.
263 * \note This discards the optional precision qualifier.
265 const struct glsl_type
*return_type
;
268 * List of ir_variable of function parameters.
270 * This represents the storage. The paramaters passed in a particular
271 * call will be in ir_call::actual_paramaters.
273 struct exec_list parameters
;
275 /** Whether or not this function has a body (which may be empty). */
276 unsigned is_defined
:1;
278 /** Body of instructions in the function. */
279 struct exec_list body
;
282 /** Function of which this signature is one overload. */
283 class ir_function
*function
;
285 friend class ir_function
;
290 * Header for tracking multiple overloaded functions with the same name.
291 * Contains a list of ir_function_signatures representing each of the
294 class ir_function
: public ir_instruction
{
296 ir_function(const char *name
);
298 virtual ir_function
*as_function()
303 virtual void accept(ir_visitor
*v
)
308 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
310 void add_signature(ir_function_signature
*sig
)
312 sig
->function
= this;
313 signatures
.push_tail(sig
);
317 * Get an iterator for the set of function signatures
319 exec_list_iterator
iterator()
321 return signatures
.iterator();
325 * Find a signature that matches a set of actual parameters, taking implicit
326 * conversions into account.
328 const ir_function_signature
*matching_signature(exec_list
*actual_param
);
331 * Find a signature that exactly matches a set of actual parameters without
332 * any implicit type conversions.
334 ir_function_signature
*exact_matching_signature(exec_list
*actual_ps
);
337 * Name of the function.
343 * List of ir_function_signature for each overloaded function with this name.
345 struct exec_list signatures
;
348 inline const char *ir_function_signature::function_name() const
350 return function
->name
;
356 * IR instruction representing high-level if-statements
358 class ir_if
: public ir_instruction
{
360 ir_if(ir_rvalue
*condition
)
361 : condition(condition
)
366 virtual ir_if
*as_if()
371 virtual void accept(ir_visitor
*v
)
376 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
378 ir_rvalue
*condition
;
379 /** List of ir_instruction for the body of the then branch */
380 exec_list then_instructions
;
381 /** List of ir_instruction for the body of the else branch */
382 exec_list else_instructions
;
387 * IR instruction representing a high-level loop structure.
389 class ir_loop
: public ir_instruction
{
391 ir_loop() : from(NULL
), to(NULL
), increment(NULL
), counter(NULL
)
396 virtual void accept(ir_visitor
*v
)
401 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
403 virtual ir_loop
*as_loop()
409 * Get an iterator for the instructions of the loop body
411 exec_list_iterator
iterator()
413 return body_instructions
.iterator();
416 /** List of ir_instruction that make up the body of the loop. */
417 exec_list body_instructions
;
420 * \name Loop counter and controls
425 ir_rvalue
*increment
;
426 ir_variable
*counter
;
431 class ir_assignment
: public ir_rvalue
{
433 ir_assignment(ir_rvalue
*lhs
, ir_rvalue
*rhs
, ir_rvalue
*condition
);
435 virtual void accept(ir_visitor
*v
)
440 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
442 virtual ir_assignment
* as_assignment()
448 * Left-hand side of the assignment.
453 * Value being assigned
458 * Optional condition for the assignment.
460 ir_rvalue
*condition
;
463 /* Update ir_expression::num_operands() and operator_strs when
464 * updating this list.
466 enum ir_expression_operation
{
479 ir_unop_f2i
, /**< Float-to-integer conversion. */
480 ir_unop_i2f
, /**< Integer-to-float conversion. */
481 ir_unop_f2b
, /**< Float-to-boolean conversion */
482 ir_unop_b2f
, /**< Boolean-to-float conversion */
483 ir_unop_i2b
, /**< int-to-boolean conversion */
484 ir_unop_b2i
, /**< Boolean-to-int conversion */
485 ir_unop_u2f
, /**< Unsigned-to-float conversion. */
488 * \name Unary floating-point rounding operations.
497 * \name Trigonometric operations.
505 * \name Partial derivatives.
519 * \name Binary comparison operators
531 * \name Bit-wise binary operations.
552 class ir_expression
: public ir_rvalue
{
554 ir_expression(int op
, const struct glsl_type
*type
,
555 ir_rvalue
*, ir_rvalue
*);
557 static unsigned int get_num_operands(ir_expression_operation
);
558 unsigned int get_num_operands()
560 return get_num_operands(operation
);
564 * Return a string representing this expression's operator.
566 const char *operator_string();
569 * Do a reverse-lookup to translate the given string into an operator.
571 static ir_expression_operation
get_operator(const char *);
573 virtual void accept(ir_visitor
*v
)
578 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
580 ir_expression
*clone();
582 ir_expression_operation operation
;
583 ir_rvalue
*operands
[2];
588 * IR instruction representing a function call
590 class ir_call
: public ir_rvalue
{
592 ir_call(const ir_function_signature
*callee
, exec_list
*actual_parameters
)
595 assert(callee
->return_type
!= NULL
);
596 type
= callee
->return_type
;
597 actual_parameters
->move_nodes_to(& this->actual_parameters
);
600 virtual ir_call
*as_call()
605 virtual void accept(ir_visitor
*v
)
610 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
613 * Get a generic ir_call object when an error occurs
615 static ir_call
*get_error_instruction();
618 * Get an iterator for the set of acutal parameters
620 exec_list_iterator
iterator()
622 return actual_parameters
.iterator();
626 * Get the name of the function being called.
628 const char *callee_name() const
630 return callee
->function_name();
633 const ir_function_signature
*get_callee()
639 * Generates an inline version of the function before @ir,
640 * returning the return value of the function.
642 ir_rvalue
*generate_inline(ir_instruction
*ir
);
651 const ir_function_signature
*callee
;
653 /* List of ir_rvalue of paramaters passed in this call. */
654 exec_list actual_parameters
;
659 * \name Jump-like IR instructions.
661 * These include \c break, \c continue, \c return, and \c discard.
664 class ir_jump
: public ir_instruction
{
672 class ir_return
: public ir_jump
{
680 ir_return(ir_rvalue
*value
)
686 virtual ir_return
*as_return()
691 ir_rvalue
*get_value() const
696 virtual void accept(ir_visitor
*v
)
701 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
708 * Jump instructions used inside loops
710 * These include \c break and \c continue. The \c break within a loop is
711 * different from the \c break within a switch-statement.
715 class ir_loop_jump
: public ir_jump
{
722 ir_loop_jump(ir_loop
*loop
, jump_mode mode
)
723 : loop(loop
), mode(mode
)
728 virtual void accept(ir_visitor
*v
)
733 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
735 bool is_break() const
737 return mode
== jump_break
;
740 bool is_continue() const
742 return mode
== jump_continue
;
746 /** Loop containing this break instruction. */
749 /** Mode selector for the jump instruction. */
756 * Texture sampling opcodes used in ir_texture
758 enum ir_texture_opcode
{
759 ir_tex
, /* Regular texture look-up */
760 ir_txb
, /* Texture look-up with LOD bias */
761 ir_txl
, /* Texture look-up with explicit LOD */
762 ir_txd
, /* Texture look-up with partial derivatvies */
763 ir_txf
/* Texel fetch with explicit LOD */
768 * IR instruction to sample a texture
770 * The specific form of the IR instruction depends on the \c mode value
771 * selected from \c ir_texture_opcodes. In the printed IR, these will
775 * | Projection divisor
776 * | | Shadow comparitor
779 * (tex (sampler) (coordinate) (0 0 0) (1) ( ))
780 * (txb (sampler) (coordinate) (0 0 0) (1) ( ) (bias))
781 * (txl (sampler) (coordinate) (0 0 0) (1) ( ) (lod))
782 * (txd (sampler) (coordinate) (0 0 0) (1) ( ) (dPdx dPdy))
783 * (txf (sampler) (coordinate) (0 0 0) (lod))
785 class ir_texture
: public ir_rvalue
{
787 ir_texture(enum ir_texture_opcode op
)
788 : op(op
), projector(NULL
), shadow_comparitor(NULL
)
793 virtual void accept(ir_visitor
*v
)
798 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
801 * Return a string representing the ir_texture_opcode.
803 const char *opcode_string();
805 /** Set the sampler and infer the type. */
806 void set_sampler(ir_dereference
*sampler
);
809 * Do a reverse-lookup to translate a string into an ir_texture_opcode.
811 static ir_texture_opcode
get_opcode(const char *);
813 enum ir_texture_opcode op
;
815 /** Sampler to use for the texture access. */
816 ir_dereference
*sampler
;
818 /** Texture coordinate to sample */
819 ir_rvalue
*coordinate
;
822 * Value used for projective divide.
824 * If there is no projective divide (the common case), this will be
825 * \c NULL. Optimization passes should check for this to point to a constant
826 * of 1.0 and replace that with \c NULL.
828 ir_rvalue
*projector
;
831 * Coordinate used for comparison on shadow look-ups.
833 * If there is no shadow comparison, this will be \c NULL. For the
834 * \c ir_txf opcode, this *must* be \c NULL.
836 ir_rvalue
*shadow_comparitor
;
838 /** Explicit texel offsets. */
839 signed char offsets
[3];
842 ir_rvalue
*lod
; /**< Floating point LOD */
843 ir_rvalue
*bias
; /**< Floating point LOD bias */
845 ir_rvalue
*dPdx
; /**< Partial derivative of coordinate wrt X */
846 ir_rvalue
*dPdy
; /**< Partial derivative of coordinate wrt Y */
852 struct ir_swizzle_mask
{
859 * Number of components in the swizzle.
861 unsigned num_components
:3;
864 * Does the swizzle contain duplicate components?
866 * L-value swizzles cannot contain duplicate components.
868 unsigned has_duplicates
:1;
872 class ir_swizzle
: public ir_rvalue
{
874 ir_swizzle(ir_rvalue
*, unsigned x
, unsigned y
, unsigned z
, unsigned w
,
876 ir_swizzle(ir_rvalue
*val
, ir_swizzle_mask mask
);
878 virtual ir_swizzle
*as_swizzle()
885 return new ir_swizzle(this->val
, this->mask
);
889 * Construct an ir_swizzle from the textual representation. Can fail.
891 static ir_swizzle
*create(ir_rvalue
*, const char *, unsigned vector_length
);
893 virtual void accept(ir_visitor
*v
)
898 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
902 return val
->is_lvalue() && !mask
.has_duplicates
;
906 * Get the variable that is ultimately referenced by an r-value
908 virtual ir_variable
*variable_referenced();
911 ir_swizzle_mask mask
;
915 class ir_dereference
: public ir_rvalue
{
917 virtual ir_dereference
*as_dereference()
925 * Get the variable that is ultimately referenced by an r-value
927 virtual ir_variable
*variable_referenced() = 0;
931 class ir_dereference_variable
: public ir_dereference
{
933 ir_dereference_variable(ir_variable
*var
);
936 * Get the variable that is ultimately referenced by an r-value
938 virtual ir_variable
*variable_referenced()
943 virtual ir_variable
*whole_variable_referenced()
945 /* ir_dereference_variable objects always dereference the entire
946 * variable. However, if this dereference is dereferenced by anything
947 * else, the complete deferefernce chain is not a whole-variable
948 * dereference. This method should only be called on the top most
949 * ir_rvalue in a dereference chain.
954 virtual void accept(ir_visitor
*v
)
959 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
962 * Object being dereferenced.
968 class ir_dereference_array
: public ir_dereference
{
970 ir_dereference_array(ir_rvalue
*value
, ir_rvalue
*array_index
);
972 ir_dereference_array(ir_variable
*var
, ir_rvalue
*array_index
);
974 virtual ir_dereference_array
*as_dereference_array()
980 * Get the variable that is ultimately referenced by an r-value
982 virtual ir_variable
*variable_referenced()
984 return this->array
->variable_referenced();
987 virtual void accept(ir_visitor
*v
)
992 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
995 ir_rvalue
*array_index
;
998 void set_array(ir_rvalue
*value
);
1002 class ir_dereference_record
: public ir_dereference
{
1004 ir_dereference_record(ir_rvalue
*value
, const char *field
);
1006 ir_dereference_record(ir_variable
*var
, const char *field
);
1009 * Get the variable that is ultimately referenced by an r-value
1011 virtual ir_variable
*variable_referenced()
1013 return this->record
->variable_referenced();
1016 virtual void accept(ir_visitor
*v
)
1021 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1029 * Data stored in an ir_constant
1031 union ir_constant_data
{
1039 class ir_constant
: public ir_rvalue
{
1041 ir_constant(const struct glsl_type
*type
, const ir_constant_data
*data
);
1042 ir_constant(bool b
);
1043 ir_constant(unsigned int u
);
1045 ir_constant(float f
);
1048 * Construct an ir_constant from a list of ir_constant values
1050 ir_constant(const struct glsl_type
*type
, exec_list
*values
);
1053 * Construct an ir_constant from a scalar component of another ir_constant
1055 * The new \c ir_constant inherits the type of the component from the
1059 * In the case of a matrix constant, the new constant is a scalar, \b not
1062 ir_constant(const ir_constant
*c
, unsigned i
);
1064 virtual ir_constant
*as_constant()
1069 virtual void accept(ir_visitor
*v
)
1074 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1076 ir_constant
*clone();
1079 * Get a particular component of a constant as a specific type
1081 * This is useful, for example, to get a value from an integer constant
1082 * as a float or bool. This appears frequently when constructors are
1083 * called with all constant parameters.
1086 bool get_bool_component(unsigned i
) const;
1087 float get_float_component(unsigned i
) const;
1088 int get_int_component(unsigned i
) const;
1089 unsigned get_uint_component(unsigned i
) const;
1092 ir_constant
*get_record_field(const char *name
);
1095 * Value of the constant.
1097 * The field used to back the values supplied by the constant is determined
1098 * by the type associated with the \c ir_instruction. Constants may be
1099 * scalars, vectors, or matrices.
1101 union ir_constant_data value
;
1103 exec_list components
;
1107 * Parameterless constructor only used by the clone method
1113 visit_exec_list(exec_list
*list
, ir_visitor
*visitor
);
1116 _mesa_glsl_initialize_variables(exec_list
*instructions
,
1117 struct _mesa_glsl_parse_state
*state
);
1120 _mesa_glsl_initialize_functions(exec_list
*instructions
,
1121 struct _mesa_glsl_parse_state
*state
);