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_dereference
* as_dereference() { return NULL
; }
56 virtual class ir_rvalue
* as_rvalue() { return NULL
; }
57 virtual class ir_label
* as_label() { return NULL
; }
58 virtual class ir_loop
* as_loop() { return NULL
; }
59 virtual class ir_assignment
* as_assignment() { return NULL
; }
60 virtual class ir_call
* as_call() { return NULL
; }
61 virtual class ir_return
* as_return() { return NULL
; }
62 virtual class ir_if
* as_if() { return NULL
; }
63 virtual class ir_swizzle
* as_swizzle() { return NULL
; }
74 class ir_rvalue
: public ir_instruction
{
76 virtual ir_rvalue
* as_rvalue()
81 virtual bool is_lvalue()
94 enum ir_variable_mode
{
102 enum ir_varaible_interpolation
{
109 class ir_variable
: public ir_instruction
{
111 ir_variable(const struct glsl_type
*, const char *);
113 virtual ir_variable
*as_variable()
118 virtual void accept(ir_visitor
*v
)
124 * Duplicate an IR variable
127 * This will probably be made \c virtual and moved to the base class
130 ir_variable
*clone() const
132 ir_variable
*var
= new ir_variable(type
, name
);
134 var
->max_array_access
= this->max_array_access
;
135 var
->read_only
= this->read_only
;
136 var
->centroid
= this->centroid
;
137 var
->invariant
= this->invariant
;
138 var
->mode
= this->mode
;
139 var
->interpolation
= this->interpolation
;
147 * Highest element accessed with a constant expression array index
149 * Not used for non-array variables.
151 unsigned max_array_access
;
153 unsigned read_only
:1;
155 unsigned invariant
:1;
156 /** If the variable is initialized outside of the scope of the shader */
157 unsigned shader_in
:1;
159 * If the variable value is later used outside of the scope of the shader.
161 unsigned shader_out
:1;
164 unsigned interpolation
:2;
167 * Flag that the whole array is assignable
169 * In GLSL 1.20 and later whole arrays are assignable (and comparable for
170 * equality). This flag enables this behavior.
172 unsigned array_lvalue
:1;
175 * Emit a warning if this variable is accessed.
177 const char *warn_extension
;
180 * Value assigned in the initializer of a variable declared "const"
182 ir_constant
*constant_value
;
186 class ir_label
: public ir_instruction
{
188 ir_label(const char *label
, ir_function_signature
*signature
);
190 virtual ir_label
*as_label()
195 virtual void accept(ir_visitor
*v
)
202 ir_function_signature
*signature
;
208 * The representation of a function instance; may be the full definition or
209 * simply a prototype.
211 class ir_function_signature
: public ir_instruction
{
212 /* An ir_function_signature will be part of the list of signatures in
216 ir_function_signature(const glsl_type
*return_type
);
218 virtual void accept(ir_visitor
*v
)
224 * Get the name of the function for which this is a signature
226 const char *function_name() const;
229 * Function return type.
231 * \note This discards the optional precision qualifier.
233 const struct glsl_type
*return_type
;
236 * List of ir_variable of function parameters.
238 * This represents the storage. The paramaters passed in a particular
239 * call will be in ir_call::actual_paramaters.
241 struct exec_list parameters
;
243 /** Whether or not this function has a body (which may be empty). */
244 unsigned is_defined
:1;
246 /** Body of instructions in the function. */
247 struct exec_list body
;
250 /** Function of which this signature is one overload. */
251 class ir_function
*function
;
253 friend class ir_function
;
258 * Header for tracking multiple overloaded functions with the same name.
259 * Contains a list of ir_function_signatures representing each of the
262 class ir_function
: public ir_instruction
{
264 ir_function(const char *name
);
266 virtual void accept(ir_visitor
*v
)
271 void add_signature(ir_function_signature
*sig
)
273 sig
->function
= this;
274 signatures
.push_tail(sig
);
278 * Get an iterator for the set of function signatures
280 exec_list_iterator
iterator()
282 return signatures
.iterator();
286 * Find a signature that matches a set of actual parameters.
288 const ir_function_signature
*matching_signature(exec_list
*actual_param
);
291 * Name of the function.
297 * List of ir_function_signature for each overloaded function with this name.
299 struct exec_list signatures
;
302 inline const char *ir_function_signature::function_name() const
304 return function
->name
;
310 * IR instruction representing high-level if-statements
312 class ir_if
: public ir_instruction
{
314 ir_if(ir_rvalue
*condition
)
315 : condition(condition
)
320 virtual ir_if
*as_if()
325 virtual void accept(ir_visitor
*v
)
330 ir_rvalue
*condition
;
331 /** List of ir_instruction for the body of the then branch */
332 exec_list then_instructions
;
333 /** List of ir_instruction for the body of the else branch */
334 exec_list else_instructions
;
339 * IR instruction representing a high-level loop structure.
341 class ir_loop
: public ir_instruction
{
343 ir_loop() : from(NULL
), to(NULL
), increment(NULL
), counter(NULL
)
348 virtual void accept(ir_visitor
*v
)
353 virtual ir_loop
*as_loop()
359 * Get an iterator for the instructions of the loop body
361 exec_list_iterator
iterator()
363 return body_instructions
.iterator();
366 /** List of ir_instruction that make up the body of the loop. */
367 exec_list body_instructions
;
370 * \name Loop counter and controls
375 ir_rvalue
*increment
;
376 ir_variable
*counter
;
381 class ir_assignment
: public ir_rvalue
{
383 ir_assignment(ir_rvalue
*lhs
, ir_rvalue
*rhs
, ir_rvalue
*condition
);
385 virtual void accept(ir_visitor
*v
)
390 virtual ir_assignment
* as_assignment()
396 * Left-hand side of the assignment.
401 * Value being assigned
406 * Optional condition for the assignment.
408 ir_rvalue
*condition
;
411 /* Update ir_expression::num_operands() and ir_print_visitor.cpp when
412 * updating this list.
414 enum ir_expression_operation
{
426 ir_unop_f2i
, /**< Float-to-integer conversion. */
427 ir_unop_i2f
, /**< Integer-to-float conversion. */
428 ir_unop_f2b
, /**< Float-to-boolean conversion */
429 ir_unop_b2f
, /**< Boolean-to-float conversion */
430 ir_unop_i2b
, /**< int-to-boolean conversion */
431 ir_unop_b2i
, /**< Boolean-to-int conversion */
432 ir_unop_u2f
, /**< Unsigned-to-float conversion. */
435 * \name Unary floating-point rounding operations.
450 * \name Binary comparison operators
462 * \name Bit-wise binary operations.
483 class ir_expression
: public ir_rvalue
{
485 ir_expression(int op
, const struct glsl_type
*type
,
486 ir_rvalue
*, ir_rvalue
*);
488 unsigned int get_num_operands(void);
490 virtual void accept(ir_visitor
*v
)
495 ir_expression
*clone();
497 ir_expression_operation operation
;
498 ir_rvalue
*operands
[2];
503 * IR instruction representing a function call
505 class ir_call
: public ir_rvalue
{
507 ir_call(const ir_function_signature
*callee
, exec_list
*actual_parameters
)
510 assert(callee
->return_type
!= NULL
);
511 type
= callee
->return_type
;
512 actual_parameters
->move_nodes_to(& this->actual_parameters
);
515 virtual ir_call
*as_call()
520 virtual void accept(ir_visitor
*v
)
526 * Get a generic ir_call object when an error occurs
528 static ir_call
*get_error_instruction();
531 * Get an iterator for the set of acutal parameters
533 exec_list_iterator
iterator()
535 return actual_parameters
.iterator();
539 * Get the name of the function being called.
541 const char *callee_name() const
543 return callee
->function_name();
546 const ir_function_signature
*get_callee()
552 * Generates an inline version of the function before @ir,
553 * returning the return value of the function.
555 ir_rvalue
*generate_inline(ir_instruction
*ir
);
564 const ir_function_signature
*callee
;
566 /* List of ir_rvalue of paramaters passed in this call. */
567 exec_list actual_parameters
;
572 * \name Jump-like IR instructions.
574 * These include \c break, \c continue, \c return, and \c discard.
577 class ir_jump
: public ir_instruction
{
585 class ir_return
: public ir_jump
{
593 ir_return(ir_rvalue
*value
)
599 virtual ir_return
*as_return()
604 ir_rvalue
*get_value() const
609 virtual void accept(ir_visitor
*v
)
620 * Jump instructions used inside loops
622 * These include \c break and \c continue. The \c break within a loop is
623 * different from the \c break within a switch-statement.
627 class ir_loop_jump
: public ir_jump
{
634 ir_loop_jump(ir_loop
*loop
, jump_mode mode
)
635 : loop(loop
), mode(mode
)
640 virtual void accept(ir_visitor
*v
)
645 bool is_break() const
647 return mode
== jump_break
;
650 bool is_continue() const
652 return mode
== jump_continue
;
656 /** Loop containing this break instruction. */
659 /** Mode selector for the jump instruction. */
665 struct ir_swizzle_mask
{
672 * Number of components in the swizzle.
674 unsigned num_components
:3;
677 * Does the swizzle contain duplicate components?
679 * L-value swizzles cannot contain duplicate components.
681 unsigned has_duplicates
:1;
685 class ir_swizzle
: public ir_rvalue
{
687 ir_swizzle(ir_rvalue
*, unsigned x
, unsigned y
, unsigned z
, unsigned w
,
689 ir_swizzle(ir_rvalue
*val
, ir_swizzle_mask mask
)
690 : val(val
), mask(mask
)
695 virtual ir_swizzle
*as_swizzle()
702 return new ir_swizzle(this->val
, this->mask
);
706 * Construct an ir_swizzle from the textual representation. Can fail.
708 static ir_swizzle
*create(ir_rvalue
*, const char *, unsigned vector_length
);
710 virtual void accept(ir_visitor
*v
)
717 return val
->is_lvalue() && !mask
.has_duplicates
;
721 ir_swizzle_mask mask
;
725 class ir_dereference
: public ir_rvalue
{
727 ir_dereference(struct ir_instruction
*);
729 ir_dereference(ir_instruction
*variable
, ir_rvalue
*array_index
);
731 virtual ir_dereference
*as_dereference()
736 virtual void accept(ir_visitor
*v
)
744 ir_reference_variable
,
750 * Object being dereferenced.
752 * Must be either an \c ir_variable or an \c ir_rvalue.
757 ir_rvalue
*array_index
;
763 class ir_constant
: public ir_rvalue
{
765 ir_constant(const struct glsl_type
*type
, const void *data
);
767 ir_constant(unsigned int u
);
769 ir_constant(float f
);
771 virtual void accept(ir_visitor
*v
)
778 return new ir_constant(this->type
, &this->value
);
782 * Value of the constant.
784 * The field used to back the values supplied by the constant is determined
785 * by the type associated with the \c ir_instruction. Constants may be
786 * scalars, vectors, or matrices.
797 visit_exec_list(exec_list
*list
, ir_visitor
*visitor
);
800 _mesa_glsl_initialize_variables(exec_list
*instructions
,
801 struct _mesa_glsl_parse_state
*state
);
804 _mesa_glsl_initialize_functions(exec_list
*instructions
,
805 struct _mesa_glsl_parse_state
*state
);