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();
48 virtual void accept(ir_visitor
*) = 0;
49 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*) = 0;
52 * \name IR instruction downcast functions
54 * These functions either cast the object to a derived class or return
55 * \c NULL if the object's type does not match the specified derived class.
56 * Additional downcast functions will be added as needed.
59 virtual class ir_variable
* as_variable() { return NULL
; }
60 virtual class ir_function
* as_function() { return NULL
; }
61 virtual class ir_dereference
* as_dereference() { return NULL
; }
62 virtual class ir_dereference_array
* as_dereference_array() { return NULL
; }
63 virtual class ir_rvalue
* as_rvalue() { return NULL
; }
64 virtual class ir_loop
* as_loop() { return NULL
; }
65 virtual class ir_assignment
* as_assignment() { return NULL
; }
66 virtual class ir_call
* as_call() { return NULL
; }
67 virtual class ir_return
* as_return() { return NULL
; }
68 virtual class ir_if
* as_if() { return NULL
; }
69 virtual class ir_swizzle
* as_swizzle() { return NULL
; }
70 virtual class ir_constant
* as_constant() { return NULL
; }
81 class ir_rvalue
: public ir_instruction
{
83 virtual ir_rvalue
* as_rvalue()
88 virtual bool is_lvalue()
94 * Get the variable that is ultimately referenced by an r-value
96 virtual ir_variable
*variable_referenced()
103 * If an r-value is a reference to a whole variable, get that variable
106 * Pointer to a variable that is completely dereferenced by the r-value. If
107 * the r-value is not a dereference or the dereference does not access the
108 * entire variable (i.e., it's just one array element, struct field), \c NULL
111 virtual ir_variable
*whole_variable_referenced()
124 enum ir_variable_mode
{
132 enum ir_varaible_interpolation
{
139 class ir_variable
: public ir_instruction
{
141 ir_variable(const struct glsl_type
*, const char *);
143 virtual ir_variable
*as_variable()
148 virtual void accept(ir_visitor
*v
)
153 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
156 * Duplicate an IR variable
159 * This will probably be made \c virtual and moved to the base class
162 ir_variable
*clone() const
164 ir_variable
*var
= new ir_variable(type
, name
);
166 var
->max_array_access
= this->max_array_access
;
167 var
->read_only
= this->read_only
;
168 var
->centroid
= this->centroid
;
169 var
->invariant
= this->invariant
;
170 var
->mode
= this->mode
;
171 var
->interpolation
= this->interpolation
;
179 * Highest element accessed with a constant expression array index
181 * Not used for non-array variables.
183 unsigned max_array_access
;
185 unsigned read_only
:1;
187 unsigned invariant
:1;
188 /** If the variable is initialized outside of the scope of the shader */
189 unsigned shader_in
:1;
191 * If the variable value is later used outside of the scope of the shader.
193 unsigned shader_out
:1;
196 unsigned interpolation
:2;
199 * Flag that the whole array is assignable
201 * In GLSL 1.20 and later whole arrays are assignable (and comparable for
202 * equality). This flag enables this behavior.
204 unsigned array_lvalue
:1;
207 * Emit a warning if this variable is accessed.
209 const char *warn_extension
;
212 * Value assigned in the initializer of a variable declared "const"
214 ir_constant
*constant_value
;
220 * The representation of a function instance; may be the full definition or
221 * simply a prototype.
223 class ir_function_signature
: public ir_instruction
{
224 /* An ir_function_signature will be part of the list of signatures in
228 ir_function_signature(const glsl_type
*return_type
);
230 virtual void accept(ir_visitor
*v
)
235 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
238 * Get the name of the function for which this is a signature
240 const char *function_name() const;
243 * Check whether the qualifiers match between this signature's parameters
244 * and the supplied parameter list. If not, returns the name of the first
245 * parameter with mismatched qualifiers (for use in error messages).
247 const char *qualifiers_match(exec_list
*params
);
250 * Replace the current parameter list with the given one. This is useful
251 * if the current information came from a prototype, and either has invalid
252 * or missing parameter names.
254 void replace_parameters(exec_list
*new_params
);
257 * Function return type.
259 * \note This discards the optional precision qualifier.
261 const struct glsl_type
*return_type
;
264 * List of ir_variable of function parameters.
266 * This represents the storage. The paramaters passed in a particular
267 * call will be in ir_call::actual_paramaters.
269 struct exec_list parameters
;
271 /** Whether or not this function has a body (which may be empty). */
272 unsigned is_defined
:1;
274 /** Body of instructions in the function. */
275 struct exec_list body
;
278 /** Function of which this signature is one overload. */
279 class ir_function
*function
;
281 friend class ir_function
;
286 * Header for tracking multiple overloaded functions with the same name.
287 * Contains a list of ir_function_signatures representing each of the
290 class ir_function
: public ir_instruction
{
292 ir_function(const char *name
);
294 virtual ir_function
*as_function()
299 virtual void accept(ir_visitor
*v
)
304 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
306 void add_signature(ir_function_signature
*sig
)
308 sig
->function
= this;
309 signatures
.push_tail(sig
);
313 * Get an iterator for the set of function signatures
315 exec_list_iterator
iterator()
317 return signatures
.iterator();
321 * Find a signature that matches a set of actual parameters, taking implicit
322 * conversions into account.
324 const ir_function_signature
*matching_signature(exec_list
*actual_param
);
327 * Find a signature that exactly matches a set of actual parameters without
328 * any implicit type conversions.
330 ir_function_signature
*exact_matching_signature(exec_list
*actual_ps
);
333 * Name of the function.
339 * List of ir_function_signature for each overloaded function with this name.
341 struct exec_list signatures
;
344 inline const char *ir_function_signature::function_name() const
346 return function
->name
;
352 * IR instruction representing high-level if-statements
354 class ir_if
: public ir_instruction
{
356 ir_if(ir_rvalue
*condition
)
357 : condition(condition
)
362 virtual ir_if
*as_if()
367 virtual void accept(ir_visitor
*v
)
372 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
374 ir_rvalue
*condition
;
375 /** List of ir_instruction for the body of the then branch */
376 exec_list then_instructions
;
377 /** List of ir_instruction for the body of the else branch */
378 exec_list else_instructions
;
383 * IR instruction representing a high-level loop structure.
385 class ir_loop
: public ir_instruction
{
387 ir_loop() : from(NULL
), to(NULL
), increment(NULL
), counter(NULL
)
392 virtual void accept(ir_visitor
*v
)
397 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
399 virtual ir_loop
*as_loop()
405 * Get an iterator for the instructions of the loop body
407 exec_list_iterator
iterator()
409 return body_instructions
.iterator();
412 /** List of ir_instruction that make up the body of the loop. */
413 exec_list body_instructions
;
416 * \name Loop counter and controls
421 ir_rvalue
*increment
;
422 ir_variable
*counter
;
427 class ir_assignment
: public ir_rvalue
{
429 ir_assignment(ir_rvalue
*lhs
, ir_rvalue
*rhs
, ir_rvalue
*condition
);
431 virtual void accept(ir_visitor
*v
)
436 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
438 virtual ir_assignment
* as_assignment()
444 * Left-hand side of the assignment.
449 * Value being assigned
454 * Optional condition for the assignment.
456 ir_rvalue
*condition
;
459 /* Update ir_expression::num_operands() and operator_strs when
460 * updating this list.
462 enum ir_expression_operation
{
475 ir_unop_f2i
, /**< Float-to-integer conversion. */
476 ir_unop_i2f
, /**< Integer-to-float conversion. */
477 ir_unop_f2b
, /**< Float-to-boolean conversion */
478 ir_unop_b2f
, /**< Boolean-to-float conversion */
479 ir_unop_i2b
, /**< int-to-boolean conversion */
480 ir_unop_b2i
, /**< Boolean-to-int conversion */
481 ir_unop_u2f
, /**< Unsigned-to-float conversion. */
484 * \name Unary floating-point rounding operations.
493 * \name Trigonometric operations.
501 * \name Partial derivatives.
515 * \name Binary comparison operators
527 * \name Bit-wise binary operations.
548 class ir_expression
: public ir_rvalue
{
550 ir_expression(int op
, const struct glsl_type
*type
,
551 ir_rvalue
*, ir_rvalue
*);
553 static unsigned int get_num_operands(ir_expression_operation
);
554 unsigned int get_num_operands()
556 return get_num_operands(operation
);
560 * Return a string representing this expression's operator.
562 const char *operator_string();
565 * Do a reverse-lookup to translate the given string into an operator.
567 static ir_expression_operation
get_operator(const char *);
569 virtual void accept(ir_visitor
*v
)
574 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
576 ir_expression
*clone();
578 ir_expression_operation operation
;
579 ir_rvalue
*operands
[2];
584 * IR instruction representing a function call
586 class ir_call
: public ir_rvalue
{
588 ir_call(const ir_function_signature
*callee
, exec_list
*actual_parameters
)
591 assert(callee
->return_type
!= NULL
);
592 type
= callee
->return_type
;
593 actual_parameters
->move_nodes_to(& this->actual_parameters
);
596 virtual ir_call
*as_call()
601 virtual void accept(ir_visitor
*v
)
606 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
609 * Get a generic ir_call object when an error occurs
611 static ir_call
*get_error_instruction();
614 * Get an iterator for the set of acutal parameters
616 exec_list_iterator
iterator()
618 return actual_parameters
.iterator();
622 * Get the name of the function being called.
624 const char *callee_name() const
626 return callee
->function_name();
629 const ir_function_signature
*get_callee()
635 * Generates an inline version of the function before @ir,
636 * returning the return value of the function.
638 ir_rvalue
*generate_inline(ir_instruction
*ir
);
647 const ir_function_signature
*callee
;
649 /* List of ir_rvalue of paramaters passed in this call. */
650 exec_list actual_parameters
;
655 * \name Jump-like IR instructions.
657 * These include \c break, \c continue, \c return, and \c discard.
660 class ir_jump
: public ir_instruction
{
668 class ir_return
: public ir_jump
{
676 ir_return(ir_rvalue
*value
)
682 virtual ir_return
*as_return()
687 ir_rvalue
*get_value() const
692 virtual void accept(ir_visitor
*v
)
697 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
704 * Jump instructions used inside loops
706 * These include \c break and \c continue. The \c break within a loop is
707 * different from the \c break within a switch-statement.
711 class ir_loop_jump
: public ir_jump
{
718 ir_loop_jump(ir_loop
*loop
, jump_mode mode
)
719 : loop(loop
), mode(mode
)
724 virtual void accept(ir_visitor
*v
)
729 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
731 bool is_break() const
733 return mode
== jump_break
;
736 bool is_continue() const
738 return mode
== jump_continue
;
742 /** Loop containing this break instruction. */
745 /** Mode selector for the jump instruction. */
752 * Texture sampling opcodes used in ir_texture
754 enum ir_texture_opcode
{
755 ir_tex
, /* Regular texture look-up */
756 ir_txb
, /* Texture look-up with LOD bias */
757 ir_txl
, /* Texture look-up with explicit LOD */
758 ir_txd
, /* Texture look-up with partial derivatvies */
759 ir_txf
/* Texel fetch with explicit LOD */
764 * IR instruction to sample a texture
766 * The specific form of the IR instruction depends on the \c mode value
767 * selected from \c ir_texture_opcodes. In the printed IR, these will
771 * | Projection divisor
772 * | | Shadow comparitor
775 * (tex (sampler) (coordinate) (0 0 0) (1) ( ))
776 * (txb (sampler) (coordinate) (0 0 0) (1) ( ) (bias))
777 * (txl (sampler) (coordinate) (0 0 0) (1) ( ) (lod))
778 * (txd (sampler) (coordinate) (0 0 0) (1) ( ) (dPdx dPdy))
779 * (txf (sampler) (coordinate) (0 0 0) (lod))
781 class ir_texture
: public ir_rvalue
{
783 ir_texture(enum ir_texture_opcode op
)
784 : op(op
), projector(NULL
), shadow_comparitor(NULL
)
789 virtual void accept(ir_visitor
*v
)
794 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
797 * Return a string representing the ir_texture_opcode.
799 const char *opcode_string();
801 /** Set the sampler and infer the type. */
802 void set_sampler(ir_dereference
*sampler
);
805 * Do a reverse-lookup to translate a string into an ir_texture_opcode.
807 static ir_texture_opcode
get_opcode(const char *);
809 enum ir_texture_opcode op
;
811 /** Sampler to use for the texture access. */
812 ir_dereference
*sampler
;
814 /** Texture coordinate to sample */
815 ir_rvalue
*coordinate
;
818 * Value used for projective divide.
820 * If there is no projective divide (the common case), this will be
821 * \c NULL. Optimization passes should check for this to point to a constant
822 * of 1.0 and replace that with \c NULL.
824 ir_rvalue
*projector
;
827 * Coordinate used for comparison on shadow look-ups.
829 * If there is no shadow comparison, this will be \c NULL. For the
830 * \c ir_txf opcode, this *must* be \c NULL.
832 ir_rvalue
*shadow_comparitor
;
834 /** Explicit texel offsets. */
835 signed char offsets
[3];
838 ir_rvalue
*lod
; /**< Floating point LOD */
839 ir_rvalue
*bias
; /**< Floating point LOD bias */
841 ir_rvalue
*dPdx
; /**< Partial derivative of coordinate wrt X */
842 ir_rvalue
*dPdy
; /**< Partial derivative of coordinate wrt Y */
848 struct ir_swizzle_mask
{
855 * Number of components in the swizzle.
857 unsigned num_components
:3;
860 * Does the swizzle contain duplicate components?
862 * L-value swizzles cannot contain duplicate components.
864 unsigned has_duplicates
:1;
868 class ir_swizzle
: public ir_rvalue
{
870 ir_swizzle(ir_rvalue
*, unsigned x
, unsigned y
, unsigned z
, unsigned w
,
872 ir_swizzle(ir_rvalue
*val
, ir_swizzle_mask mask
);
874 virtual ir_swizzle
*as_swizzle()
881 return new ir_swizzle(this->val
, this->mask
);
885 * Construct an ir_swizzle from the textual representation. Can fail.
887 static ir_swizzle
*create(ir_rvalue
*, const char *, unsigned vector_length
);
889 virtual void accept(ir_visitor
*v
)
894 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
898 return val
->is_lvalue() && !mask
.has_duplicates
;
902 * Get the variable that is ultimately referenced by an r-value
904 virtual ir_variable
*variable_referenced();
907 ir_swizzle_mask mask
;
911 class ir_dereference
: public ir_rvalue
{
913 virtual ir_dereference
*as_dereference()
921 * Get the variable that is ultimately referenced by an r-value
923 virtual ir_variable
*variable_referenced() = 0;
927 class ir_dereference_variable
: public ir_dereference
{
929 ir_dereference_variable(ir_variable
*var
);
932 * Get the variable that is ultimately referenced by an r-value
934 virtual ir_variable
*variable_referenced()
939 virtual ir_variable
*whole_variable_referenced()
941 /* ir_dereference_variable objects always dereference the entire
942 * variable. However, if this dereference is dereferenced by anything
943 * else, the complete deferefernce chain is not a whole-variable
944 * dereference. This method should only be called on the top most
945 * ir_rvalue in a dereference chain.
950 virtual void accept(ir_visitor
*v
)
955 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
958 * Object being dereferenced.
964 class ir_dereference_array
: public ir_dereference
{
966 ir_dereference_array(ir_rvalue
*value
, ir_rvalue
*array_index
);
968 ir_dereference_array(ir_variable
*var
, ir_rvalue
*array_index
);
970 virtual ir_dereference_array
*as_dereference_array()
976 * Get the variable that is ultimately referenced by an r-value
978 virtual ir_variable
*variable_referenced()
980 return this->array
->variable_referenced();
983 virtual void accept(ir_visitor
*v
)
988 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
991 ir_rvalue
*array_index
;
994 void set_array(ir_rvalue
*value
);
998 class ir_dereference_record
: public ir_dereference
{
1000 ir_dereference_record(ir_rvalue
*value
, const char *field
);
1002 ir_dereference_record(ir_variable
*var
, const char *field
);
1005 * Get the variable that is ultimately referenced by an r-value
1007 virtual ir_variable
*variable_referenced()
1009 return this->record
->variable_referenced();
1012 virtual void accept(ir_visitor
*v
)
1017 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1025 * Data stored in an ir_constant
1027 union ir_constant_data
{
1035 class ir_constant
: public ir_rvalue
{
1037 ir_constant(const struct glsl_type
*type
, const ir_constant_data
*data
);
1038 ir_constant(bool b
);
1039 ir_constant(unsigned int u
);
1041 ir_constant(float f
);
1044 * Construct an ir_constant from a list of ir_constant values
1046 ir_constant(const struct glsl_type
*type
, exec_list
*values
);
1049 * Construct an ir_constant from a scalar component of another ir_constant
1051 * The new \c ir_constant inherits the type of the component from the
1055 * In the case of a matrix constant, the new constant is a scalar, \b not
1058 ir_constant(const ir_constant
*c
, unsigned i
);
1060 virtual ir_constant
*as_constant()
1065 virtual void accept(ir_visitor
*v
)
1070 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1072 ir_constant
*clone();
1075 * Get a particular component of a constant as a specific type
1077 * This is useful, for example, to get a value from an integer constant
1078 * as a float or bool. This appears frequently when constructors are
1079 * called with all constant parameters.
1082 bool get_bool_component(unsigned i
) const;
1083 float get_float_component(unsigned i
) const;
1084 int get_int_component(unsigned i
) const;
1085 unsigned get_uint_component(unsigned i
) const;
1088 ir_constant
*get_record_field(const char *name
);
1091 * Value of the constant.
1093 * The field used to back the values supplied by the constant is determined
1094 * by the type associated with the \c ir_instruction. Constants may be
1095 * scalars, vectors, or matrices.
1097 union ir_constant_data value
;
1099 exec_list components
;
1103 * Parameterless constructor only used by the clone method
1109 visit_exec_list(exec_list
*list
, ir_visitor
*visitor
);
1112 _mesa_glsl_initialize_variables(exec_list
*instructions
,
1113 struct _mesa_glsl_parse_state
*state
);
1116 _mesa_glsl_initialize_functions(exec_list
*instructions
,
1117 struct _mesa_glsl_parse_state
*state
);