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
->max_array_access
= this->max_array_access
;
125 var
->read_only
= this->read_only
;
126 var
->centroid
= this->centroid
;
127 var
->invariant
= this->invariant
;
128 var
->mode
= this->mode
;
129 var
->interpolation
= this->interpolation
;
137 * Highest element accessed with a constant expression array index
139 * Not used for non-array variables.
141 unsigned max_array_access
;
143 unsigned read_only
:1;
145 unsigned invariant
:1;
148 unsigned interpolation
:2;
151 * Flag that the whole array is assignable
153 * In GLSL 1.20 and later whole arrays are assignable (and comparable for
154 * equality). This flag enables this behavior.
156 unsigned array_lvalue
:1;
159 * Value assigned in the initializer of a variable declared "const"
161 ir_constant
*constant_value
;
165 class ir_label
: public ir_instruction
{
167 ir_label(const char *label
);
169 virtual void accept(ir_visitor
*v
)
179 class ir_function_signature
: public ir_instruction
{
181 ir_function_signature(const glsl_type
*return_type
);
183 virtual void accept(ir_visitor
*v
)
189 * Get the name of the function for which this is a signature
191 const char *function_name() const;
194 * Function return type.
196 * \note This discards the optional precision qualifier.
198 const struct glsl_type
*return_type
;
201 * List of function parameters stored as ir_variable objects.
203 struct exec_list parameters
;
206 * Pointer to the label that begins the function definition.
208 ir_label
*definition
;
211 /** Function of which this signature is one overload. */
212 class ir_function
*function
;
214 friend class ir_function
;
219 * Header for tracking functions in the symbol table
221 class ir_function
: public ir_instruction
{
223 ir_function(const char *name
);
225 virtual void accept(ir_visitor
*v
)
230 void add_signature(ir_function_signature
*sig
)
232 sig
->function
= this;
233 signatures
.push_tail(sig
);
237 * Get an iterator for the set of function signatures
239 exec_list_iterator
iterator()
241 return signatures
.iterator();
245 * Find a signature that matches a set of actual parameters.
247 const ir_function_signature
*matching_signature(exec_list
*actual_param
);
250 * Name of the function.
256 * Set of overloaded functions with this name.
258 struct exec_list signatures
;
261 inline const char *ir_function_signature::function_name() const
263 return function
->name
;
269 * IR instruction representing high-level if-statements
271 class ir_if
: public ir_instruction
{
273 ir_if(ir_rvalue
*condition
)
274 : condition(condition
)
279 virtual void accept(ir_visitor
*v
)
284 ir_rvalue
*condition
;
285 exec_list then_instructions
;
286 exec_list else_instructions
;
290 class ir_assignment
: public ir_rvalue
{
292 ir_assignment(ir_rvalue
*lhs
, ir_rvalue
*rhs
, ir_rvalue
*condition
);
294 virtual void accept(ir_visitor
*v
)
300 * Left-hand side of the assignment.
305 * Value being assigned
310 * Optional condition for the assignment.
312 ir_rvalue
*condition
;
315 /* Update ir_expression::num_operands() and ir_print_visitor.cpp when
316 * updating this list.
318 enum ir_expression_operation
{
330 ir_unop_f2i
, /**< Float-to-integer conversion. */
331 ir_unop_i2f
, /**< Integer-to-float conversion. */
332 ir_unop_f2b
, /**< Float-to-boolean conversion */
333 ir_unop_b2f
, /**< Boolean-to-float conversion */
334 ir_unop_i2b
, /**< int-to-boolean conversion */
335 ir_unop_b2i
, /**< Boolean-to-int conversion */
336 ir_unop_u2f
, /**< Unsigned-to-float conversion. */
339 * \name Unary floating-point rounding operations.
354 * \name Binary comparison operators
366 * \name Bit-wise binary operations.
387 class ir_expression
: public ir_rvalue
{
389 ir_expression(int op
, const struct glsl_type
*type
,
390 ir_rvalue
*, ir_rvalue
*);
392 unsigned int get_num_operands(void);
394 virtual void accept(ir_visitor
*v
)
399 ir_expression_operation operation
;
400 ir_rvalue
*operands
[2];
405 * IR instruction representing a function call
407 class ir_call
: public ir_rvalue
{
409 ir_call(const ir_function_signature
*callee
, exec_list
*actual_parameters
)
410 : ir_rvalue(), callee(callee
)
412 assert(callee
->return_type
!= NULL
);
413 type
= callee
->return_type
;
414 actual_parameters
->move_nodes_to(& this->actual_parameters
);
417 virtual void accept(ir_visitor
*v
)
423 * Get a generic ir_call object when an error occurs
425 static ir_call
*get_error_instruction();
428 * Get an iterator for the set of acutal parameters
430 exec_list_iterator
iterator()
432 return actual_parameters
.iterator();
436 * Get the name of the function being called.
438 const char *callee_name() const
440 return callee
->function_name();
445 : ir_rvalue(), callee(NULL
)
450 const ir_function_signature
*callee
;
451 exec_list actual_parameters
;
456 * \name Jump-like IR instructions.
458 * These include \c break, \c continue, \c return, and \c discard.
461 class ir_jump
: public ir_instruction
{
470 class ir_return
: public ir_jump
{
478 ir_return(ir_rvalue
*value
)
484 ir_rvalue
*get_value() const
489 virtual void accept(ir_visitor
*v
)
500 struct ir_swizzle_mask
{
507 * Number of components in the swizzle.
509 unsigned num_components
:3;
512 * Does the swizzle contain duplicate components?
514 * L-value swizzles cannot contain duplicate components.
516 unsigned has_duplicates
:1;
520 class ir_swizzle
: public ir_rvalue
{
522 ir_swizzle(ir_rvalue
*, unsigned x
, unsigned y
, unsigned z
, unsigned w
,
525 * Construct an ir_swizzle from the textual representation. Can fail.
527 static ir_swizzle
*create(ir_rvalue
*, const char *, unsigned vector_length
);
529 virtual void accept(ir_visitor
*v
)
536 return val
->is_lvalue() && !mask
.has_duplicates
;
540 ir_swizzle_mask mask
;
544 class ir_dereference
: public ir_rvalue
{
546 ir_dereference(struct ir_instruction
*);
548 ir_dereference(ir_instruction
*variable
, ir_rvalue
*array_index
);
550 virtual ir_dereference
*as_dereference()
555 virtual void accept(ir_visitor
*v
)
563 ir_reference_variable
,
569 * Object being dereferenced.
571 * Must be either an \c ir_variable or an \c ir_rvalue.
576 ir_rvalue
*array_index
;
582 class ir_constant
: public ir_rvalue
{
584 ir_constant(const struct glsl_type
*type
, const void *data
);
586 ir_constant(unsigned int u
);
588 ir_constant(float f
);
590 virtual void accept(ir_visitor
*v
)
596 * Value of the constant.
598 * The field used to back the values supplied by the constant is determined
599 * by the type associated with the \c ir_instruction. Constants may be
600 * scalars, vectors, or matrices.
612 _mesa_glsl_initialize_variables(exec_list
*instructions
,
613 struct _mesa_glsl_parse_state
*state
);
616 _mesa_glsl_initialize_functions(exec_list
*instructions
,
617 struct _mesa_glsl_parse_state
*state
);