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.
30 #include "ir_visitor.h"
37 * Base class of all IR instructions
39 class ir_instruction
: public exec_node
{
41 const struct glsl_type
*type
;
43 class ir_constant
*constant_expression_value();
44 virtual void accept(ir_visitor
*) = 0;
47 * \name IR instruction downcast functions
49 * These functions either cast the object to a derived class or return
50 * \c NULL if the object's type does not match the specified derived class.
51 * Additional downcast functions will be added as needed.
54 virtual class ir_variable
* as_variable() { return NULL
; }
55 virtual class ir_function
* as_function() { return NULL
; }
56 virtual class ir_dereference
* as_dereference() { return NULL
; }
57 virtual class ir_rvalue
* as_rvalue() { return NULL
; }
58 virtual class ir_label
* as_label() { return NULL
; }
59 virtual class ir_loop
* as_loop() { return NULL
; }
60 virtual class ir_assignment
* as_assignment() { return NULL
; }
61 virtual class ir_call
* as_call() { return NULL
; }
62 virtual class ir_return
* as_return() { return NULL
; }
63 virtual class ir_if
* as_if() { return NULL
; }
64 virtual class ir_swizzle
* as_swizzle() { return NULL
; }
75 class ir_rvalue
: public ir_instruction
{
77 virtual ir_rvalue
* as_rvalue()
82 virtual bool is_lvalue()
95 enum ir_variable_mode
{
103 enum ir_varaible_interpolation
{
110 class ir_variable
: public ir_instruction
{
112 ir_variable(const struct glsl_type
*, const char *);
114 virtual ir_variable
*as_variable()
119 virtual void accept(ir_visitor
*v
)
125 * Duplicate an IR variable
128 * This will probably be made \c virtual and moved to the base class
131 ir_variable
*clone() const
133 ir_variable
*var
= new ir_variable(type
, name
);
135 var
->max_array_access
= this->max_array_access
;
136 var
->read_only
= this->read_only
;
137 var
->centroid
= this->centroid
;
138 var
->invariant
= this->invariant
;
139 var
->mode
= this->mode
;
140 var
->interpolation
= this->interpolation
;
148 * Highest element accessed with a constant expression array index
150 * Not used for non-array variables.
152 unsigned max_array_access
;
154 unsigned read_only
:1;
156 unsigned invariant
:1;
157 /** If the variable is initialized outside of the scope of the shader */
158 unsigned shader_in
:1;
160 * If the variable value is later used outside of the scope of the shader.
162 unsigned shader_out
:1;
165 unsigned interpolation
:2;
168 * Flag that the whole array is assignable
170 * In GLSL 1.20 and later whole arrays are assignable (and comparable for
171 * equality). This flag enables this behavior.
173 unsigned array_lvalue
:1;
176 * Emit a warning if this variable is accessed.
178 const char *warn_extension
;
181 * Value assigned in the initializer of a variable declared "const"
183 ir_constant
*constant_value
;
189 * The representation of a function instance; may be the full definition or
190 * simply a prototype.
192 class ir_function_signature
: public ir_instruction
{
193 /* An ir_function_signature will be part of the list of signatures in
197 ir_function_signature(const glsl_type
*return_type
);
199 virtual void accept(ir_visitor
*v
)
205 * Get the name of the function for which this is a signature
207 const char *function_name() const;
210 * Function return type.
212 * \note This discards the optional precision qualifier.
214 const struct glsl_type
*return_type
;
217 * List of ir_variable of function parameters.
219 * This represents the storage. The paramaters passed in a particular
220 * call will be in ir_call::actual_paramaters.
222 struct exec_list parameters
;
224 /** Whether or not this function has a body (which may be empty). */
225 unsigned is_defined
:1;
227 /** Body of instructions in the function. */
228 struct exec_list body
;
231 /** Function of which this signature is one overload. */
232 class ir_function
*function
;
234 friend class ir_function
;
239 * Header for tracking multiple overloaded functions with the same name.
240 * Contains a list of ir_function_signatures representing each of the
243 class ir_function
: public ir_instruction
{
245 ir_function(const char *name
);
247 virtual ir_function
*as_function()
252 virtual void accept(ir_visitor
*v
)
257 void add_signature(ir_function_signature
*sig
)
259 sig
->function
= this;
260 signatures
.push_tail(sig
);
264 * Get an iterator for the set of function signatures
266 exec_list_iterator
iterator()
268 return signatures
.iterator();
272 * Find a signature that matches a set of actual parameters.
274 const ir_function_signature
*matching_signature(exec_list
*actual_param
);
277 * Name of the function.
283 * List of ir_function_signature for each overloaded function with this name.
285 struct exec_list signatures
;
288 inline const char *ir_function_signature::function_name() const
290 return function
->name
;
296 * IR instruction representing high-level if-statements
298 class ir_if
: public ir_instruction
{
300 ir_if(ir_rvalue
*condition
)
301 : condition(condition
)
306 virtual ir_if
*as_if()
311 virtual void accept(ir_visitor
*v
)
316 ir_rvalue
*condition
;
317 /** List of ir_instruction for the body of the then branch */
318 exec_list then_instructions
;
319 /** List of ir_instruction for the body of the else branch */
320 exec_list else_instructions
;
325 * IR instruction representing a high-level loop structure.
327 class ir_loop
: public ir_instruction
{
329 ir_loop() : from(NULL
), to(NULL
), increment(NULL
), counter(NULL
)
334 virtual void accept(ir_visitor
*v
)
339 virtual ir_loop
*as_loop()
345 * Get an iterator for the instructions of the loop body
347 exec_list_iterator
iterator()
349 return body_instructions
.iterator();
352 /** List of ir_instruction that make up the body of the loop. */
353 exec_list body_instructions
;
356 * \name Loop counter and controls
361 ir_rvalue
*increment
;
362 ir_variable
*counter
;
367 class ir_assignment
: public ir_rvalue
{
369 ir_assignment(ir_rvalue
*lhs
, ir_rvalue
*rhs
, ir_rvalue
*condition
);
371 virtual void accept(ir_visitor
*v
)
376 virtual ir_assignment
* as_assignment()
382 * Left-hand side of the assignment.
387 * Value being assigned
392 * Optional condition for the assignment.
394 ir_rvalue
*condition
;
397 /* Update ir_expression::num_operands() and ir_print_visitor.cpp when
398 * updating this list.
400 enum ir_expression_operation
{
412 ir_unop_f2i
, /**< Float-to-integer conversion. */
413 ir_unop_i2f
, /**< Integer-to-float conversion. */
414 ir_unop_f2b
, /**< Float-to-boolean conversion */
415 ir_unop_b2f
, /**< Boolean-to-float conversion */
416 ir_unop_i2b
, /**< int-to-boolean conversion */
417 ir_unop_b2i
, /**< Boolean-to-int conversion */
418 ir_unop_u2f
, /**< Unsigned-to-float conversion. */
421 * \name Unary floating-point rounding operations.
436 * \name Binary comparison operators
448 * \name Bit-wise binary operations.
469 class ir_expression
: public ir_rvalue
{
471 ir_expression(int op
, const struct glsl_type
*type
,
472 ir_rvalue
*, ir_rvalue
*);
474 unsigned int get_num_operands(void);
476 virtual void accept(ir_visitor
*v
)
481 ir_expression
*clone();
483 ir_expression_operation operation
;
484 ir_rvalue
*operands
[2];
489 * IR instruction representing a function call
491 class ir_call
: public ir_rvalue
{
493 ir_call(const ir_function_signature
*callee
, exec_list
*actual_parameters
)
496 assert(callee
->return_type
!= NULL
);
497 type
= callee
->return_type
;
498 actual_parameters
->move_nodes_to(& this->actual_parameters
);
501 virtual ir_call
*as_call()
506 virtual void accept(ir_visitor
*v
)
512 * Get a generic ir_call object when an error occurs
514 static ir_call
*get_error_instruction();
517 * Get an iterator for the set of acutal parameters
519 exec_list_iterator
iterator()
521 return actual_parameters
.iterator();
525 * Get the name of the function being called.
527 const char *callee_name() const
529 return callee
->function_name();
532 const ir_function_signature
*get_callee()
538 * Generates an inline version of the function before @ir,
539 * returning the return value of the function.
541 ir_rvalue
*generate_inline(ir_instruction
*ir
);
550 const ir_function_signature
*callee
;
552 /* List of ir_rvalue of paramaters passed in this call. */
553 exec_list actual_parameters
;
558 * \name Jump-like IR instructions.
560 * These include \c break, \c continue, \c return, and \c discard.
563 class ir_jump
: public ir_instruction
{
571 class ir_return
: public ir_jump
{
579 ir_return(ir_rvalue
*value
)
585 virtual ir_return
*as_return()
590 ir_rvalue
*get_value() const
595 virtual void accept(ir_visitor
*v
)
606 * Jump instructions used inside loops
608 * These include \c break and \c continue. The \c break within a loop is
609 * different from the \c break within a switch-statement.
613 class ir_loop_jump
: public ir_jump
{
620 ir_loop_jump(ir_loop
*loop
, jump_mode mode
)
621 : loop(loop
), mode(mode
)
626 virtual void accept(ir_visitor
*v
)
631 bool is_break() const
633 return mode
== jump_break
;
636 bool is_continue() const
638 return mode
== jump_continue
;
642 /** Loop containing this break instruction. */
645 /** Mode selector for the jump instruction. */
651 struct ir_swizzle_mask
{
658 * Number of components in the swizzle.
660 unsigned num_components
:3;
663 * Does the swizzle contain duplicate components?
665 * L-value swizzles cannot contain duplicate components.
667 unsigned has_duplicates
:1;
671 class ir_swizzle
: public ir_rvalue
{
673 ir_swizzle(ir_rvalue
*, unsigned x
, unsigned y
, unsigned z
, unsigned w
,
675 ir_swizzle(ir_rvalue
*val
, ir_swizzle_mask mask
)
676 : val(val
), mask(mask
)
681 virtual ir_swizzle
*as_swizzle()
688 return new ir_swizzle(this->val
, this->mask
);
692 * Construct an ir_swizzle from the textual representation. Can fail.
694 static ir_swizzle
*create(ir_rvalue
*, const char *, unsigned vector_length
);
696 virtual void accept(ir_visitor
*v
)
703 return val
->is_lvalue() && !mask
.has_duplicates
;
707 ir_swizzle_mask mask
;
711 class ir_dereference
: public ir_rvalue
{
713 ir_dereference(struct ir_instruction
*);
715 ir_dereference(ir_instruction
*variable
, ir_rvalue
*array_index
);
717 virtual ir_dereference
*as_dereference()
722 virtual void accept(ir_visitor
*v
)
730 ir_reference_variable
,
736 * Object being dereferenced.
738 * Must be either an \c ir_variable or an \c ir_rvalue.
743 ir_rvalue
*array_index
;
749 class ir_constant
: public ir_rvalue
{
751 ir_constant(const struct glsl_type
*type
, const void *data
);
753 ir_constant(unsigned int u
);
755 ir_constant(float f
);
757 virtual void accept(ir_visitor
*v
)
764 return new ir_constant(this->type
, &this->value
);
768 * Value of the constant.
770 * The field used to back the values supplied by the constant is determined
771 * by the type associated with the \c ir_instruction. Constants may be
772 * scalars, vectors, or matrices.
783 visit_exec_list(exec_list
*list
, ir_visitor
*visitor
);
786 _mesa_glsl_initialize_variables(exec_list
*instructions
,
787 struct _mesa_glsl_parse_state
*state
);
790 _mesa_glsl_initialize_functions(exec_list
*instructions
,
791 struct _mesa_glsl_parse_state
*state
);