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
; }
67 class ir_rvalue
: public ir_instruction
{
69 virtual ir_rvalue
* as_rvalue()
74 virtual bool is_lvalue()
80 ir_rvalue() : ir_instruction() { }
84 enum ir_variable_mode
{
92 enum ir_varaible_interpolation
{
99 class ir_variable
: public ir_instruction
{
101 ir_variable(const struct glsl_type
*, const char *);
103 virtual ir_variable
*as_variable()
108 virtual void accept(ir_visitor
*v
)
114 * Duplicate an IR variable
117 * This will probably be made \c virtual and moved to the base class
120 ir_variable
*clone() const
122 ir_variable
*var
= new ir_variable(type
, name
);
124 var
->read_only
= this->read_only
;
125 var
->centroid
= this->centroid
;
126 var
->invariant
= this->invariant
;
127 var
->mode
= this->mode
;
128 var
->interpolation
= this->interpolation
;
135 unsigned read_only
:1;
137 unsigned invariant
:1;
140 unsigned interpolation
:2;
144 class ir_label
: public ir_instruction
{
146 ir_label(const char *label
);
148 virtual void accept(ir_visitor
*v
)
158 class ir_function_signature
: public ir_instruction
{
160 ir_function_signature(const glsl_type
*return_type
);
162 virtual void accept(ir_visitor
*v
)
168 * Get the name of the function for which this is a signature
170 const char *function_name() const;
173 * Function return type.
175 * \note This discards the optional precision qualifier.
177 const struct glsl_type
*return_type
;
180 * List of function parameters stored as ir_variable objects.
182 struct exec_list parameters
;
185 * Pointer to the label that begins the function definition.
187 ir_label
*definition
;
190 /** Function of which this signature is one overload. */
191 class ir_function
*function
;
193 friend class ir_function
;
198 * Header for tracking functions in the symbol table
200 class ir_function
: public ir_instruction
{
202 ir_function(const char *name
);
204 virtual void accept(ir_visitor
*v
)
209 void add_signature(ir_function_signature
*sig
)
211 sig
->function
= this;
212 signatures
.push_tail(sig
);
216 * Get an iterator for the set of function signatures
218 exec_list_iterator
iterator()
220 return signatures
.iterator();
224 * Find a signature that matches a set of actual parameters.
226 const ir_function_signature
*matching_signature(exec_list
*actual_param
);
229 * Name of the function.
235 * Set of overloaded functions with this name.
237 struct exec_list signatures
;
240 inline const char *ir_function_signature::function_name() const
242 return function
->name
;
248 * IR instruction representing high-level if-statements
250 class ir_if
: public ir_instruction
{
252 ir_if(ir_rvalue
*condition
)
253 : condition(condition
)
258 virtual void accept(ir_visitor
*v
)
263 ir_rvalue
*condition
;
264 exec_list then_instructions
;
265 exec_list else_instructions
;
269 class ir_assignment
: public ir_rvalue
{
271 ir_assignment(ir_rvalue
*lhs
, ir_rvalue
*rhs
, ir_rvalue
*condition
);
273 virtual void accept(ir_visitor
*v
)
279 * Left-hand side of the assignment.
284 * Value being assigned
289 * Optional condition for the assignment.
291 ir_rvalue
*condition
;
294 /* Update ir_print_visitor.cpp when updating this list. */
295 enum ir_expression_operation
{
307 ir_unop_f2i
, /**< Float-to-integer conversion. */
308 ir_unop_i2f
, /**< Integer-to-float conversion. */
309 ir_unop_u2f
, /**< Unsigned-to-float conversion. */
312 * \name Unary floating-point rounding operations.
327 * \name Binary comparison operators
339 * \name Bit-wise binary operations.
361 class ir_expression
: public ir_rvalue
{
363 ir_expression(int op
, const struct glsl_type
*type
,
364 ir_rvalue
*, ir_rvalue
*);
366 virtual void accept(ir_visitor
*v
)
371 ir_expression_operation operation
;
372 ir_rvalue
*operands
[2];
377 * IR instruction representing a function call
379 class ir_call
: public ir_rvalue
{
381 ir_call(const ir_function_signature
*callee
, exec_list
*actual_parameters
)
382 : ir_rvalue(), callee(callee
)
384 assert(callee
->return_type
!= NULL
);
385 type
= callee
->return_type
;
386 actual_parameters
->move_nodes_to(& this->actual_parameters
);
389 virtual void accept(ir_visitor
*v
)
395 * Get a generic ir_call object when an error occurs
397 static ir_call
*get_error_instruction();
400 * Get an iterator for the set of acutal parameters
402 exec_list_iterator
iterator()
404 return actual_parameters
.iterator();
408 * Get the name of the function being called.
410 const char *callee_name() const
412 return callee
->function_name();
417 : ir_rvalue(), callee(NULL
)
422 const ir_function_signature
*callee
;
423 exec_list actual_parameters
;
428 * \name Jump-like IR instructions.
430 * These include \c break, \c continue, \c return, and \c discard.
433 class ir_jump
: public ir_instruction
{
442 class ir_return
: public ir_jump
{
450 ir_return(ir_rvalue
*value
)
456 ir_rvalue
*get_value() const
461 virtual void accept(ir_visitor
*v
)
472 struct ir_swizzle_mask
{
479 * Number of components in the swizzle.
481 unsigned num_components
:3;
484 * Does the swizzle contain duplicate components?
486 * L-value swizzles cannot contain duplicate components.
488 unsigned has_duplicates
:1;
492 class ir_swizzle
: public ir_rvalue
{
494 ir_swizzle(ir_rvalue
*, unsigned x
, unsigned y
, unsigned z
, unsigned w
,
497 * Construct an ir_swizzle from the textual representation. Can fail.
499 static ir_swizzle
*create(ir_rvalue
*, const char *, unsigned vector_length
);
501 virtual void accept(ir_visitor
*v
)
508 return val
->is_lvalue() && !mask
.has_duplicates
;
512 ir_swizzle_mask mask
;
516 class ir_dereference
: public ir_rvalue
{
518 ir_dereference(struct ir_instruction
*);
520 ir_dereference(ir_instruction
*variable
, ir_rvalue
*array_index
);
522 virtual ir_dereference
*as_dereference()
527 virtual void accept(ir_visitor
*v
)
537 ir_variable
*const as_var
= var
->as_variable();
541 return !as_var
->read_only
;
545 ir_reference_variable
,
551 * Object being dereferenced.
553 * Must be either an \c ir_variable or an \c ir_rvalue.
558 ir_rvalue
*array_index
;
564 class ir_constant
: public ir_rvalue
{
566 ir_constant(const struct glsl_type
*type
, const void *data
);
568 ir_constant(unsigned int u
);
570 ir_constant(float f
);
572 virtual void accept(ir_visitor
*v
)
578 * Value of the constant.
580 * The field used to back the values supplied by the constant is determined
581 * by the type associated with the \c ir_instruction. Constants may be
582 * scalars, vectors, or matrices.
594 _mesa_glsl_initialize_variables(exec_list
*instructions
,
595 struct _mesa_glsl_parse_state
*state
);
598 _mesa_glsl_initialize_functions(exec_list
*instructions
,
599 struct _mesa_glsl_parse_state
*state
);