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 virtual void accept(ir_visitor
*) = 0;
46 * \name IR instruction downcast functions
48 * These functions either cast the object to a derived class or return
49 * \c NULL if the object's type does not match the specified derived class.
50 * Additional downcast functions will be added as needed.
53 virtual class ir_variable
* as_variable() { return NULL
; }
54 virtual class ir_dereference
* as_dereference() { return NULL
; }
55 virtual class ir_rvalue
* as_rvalue() { return NULL
; }
66 class ir_rvalue
: public ir_instruction
{
68 virtual ir_rvalue
* as_rvalue()
73 virtual bool is_lvalue()
79 ir_rvalue() : ir_instruction() { }
83 enum ir_variable_mode
{
91 enum ir_varaible_interpolation
{
98 class ir_variable
: public ir_instruction
{
100 ir_variable(const struct glsl_type
*, const char *);
102 virtual ir_variable
*as_variable()
107 virtual void accept(ir_visitor
*v
)
114 unsigned read_only
:1;
116 unsigned invariant
:1;
119 unsigned interpolation
:2;
123 class ir_label
: public ir_instruction
{
125 ir_label(const char *label
);
127 virtual void accept(ir_visitor
*v
)
137 class ir_function_signature
: public ir_instruction
{
139 ir_function_signature(const glsl_type
*return_type
);
141 virtual void accept(ir_visitor
*v
)
147 * Function return type.
149 * \note This discards the optional precision qualifier.
151 const struct glsl_type
*return_type
;
154 * List of function parameters stored as ir_variable objects.
156 struct exec_list parameters
;
159 * Pointer to the label that begins the function definition.
161 ir_label
*definition
;
166 * Header for tracking functions in the symbol table
168 class ir_function
: public ir_instruction
{
170 ir_function(const char *name
);
172 virtual void accept(ir_visitor
*v
)
178 * Find a signature that matches a set of actual parameters.
180 const ir_function_signature
*matching_signature(exec_list
*actual_param
);
183 * Name of the function.
188 * Set of overloaded functions with this name.
190 struct exec_list signatures
;
195 class ir_assignment
: public ir_rvalue
{
197 ir_assignment(ir_rvalue
*lhs
, ir_rvalue
*rhs
, ir_rvalue
*condition
);
199 virtual void accept(ir_visitor
*v
)
205 * Left-hand side of the assignment.
210 * Value being assigned
215 * Optional condition for the assignment.
217 ir_rvalue
*condition
;
220 /* Update ir_print_visitor.cpp when updating this list. */
221 enum ir_expression_operation
{
230 ir_unop_f2i
, /**< Float-to-integer conversion. */
231 ir_unop_i2f
, /**< Integer-to-float conversion. */
232 ir_unop_u2f
, /**< Unsigned-to-float conversion. */
235 * \name Unary floating-point rounding operations.
250 * \name Binary comparison operators
262 * \name Bit-wise binary operations.
284 class ir_expression
: public ir_rvalue
{
286 ir_expression(int op
, const struct glsl_type
*type
,
287 ir_rvalue
*, ir_rvalue
*);
289 virtual void accept(ir_visitor
*v
)
294 ir_expression_operation operation
;
295 ir_rvalue
*operands
[2];
300 * IR instruction representing a function call
302 class ir_call
: public ir_rvalue
{
304 ir_call(const ir_function_signature
*callee
, exec_list
*actual_parameters
)
305 : ir_rvalue(), callee(callee
)
307 assert(callee
->return_type
!= NULL
);
308 type
= callee
->return_type
;
309 actual_parameters
->move_nodes_to(& this->actual_parameters
);
312 virtual void accept(ir_visitor
*v
)
318 * Get a generic ir_call object when an error occurs
320 static ir_call
*get_error_instruction();
323 * Get an iterator for the set of acutal parameters
325 exec_list_iterator
iterator()
327 return actual_parameters
.iterator();
331 * Get the name of the function being called.
333 const char *callee_name() const
335 /* FINISHME: This only works for functions that have definitions. */
336 return callee
->definition
->label
;
341 : ir_rvalue(), callee(NULL
)
346 const ir_function_signature
*callee
;
347 exec_list actual_parameters
;
352 * \name Jump-like IR instructions.
354 * These include \c break, \c continue, \c return, and \c discard.
357 class ir_jump
: public ir_instruction
{
366 class ir_return
: public ir_jump
{
374 ir_return(ir_rvalue
*value
)
380 ir_rvalue
*get_value() const
385 virtual void accept(ir_visitor
*v
)
396 struct ir_swizzle_mask
{
403 * Number of components in the swizzle.
405 unsigned num_components
:3;
408 * Does the swizzle contain duplicate components?
410 * L-value swizzles cannot contain duplicate components.
412 unsigned has_duplicates
:1;
416 class ir_swizzle
: public ir_rvalue
{
418 ir_swizzle(ir_rvalue
*, unsigned x
, unsigned y
, unsigned z
, unsigned w
,
421 * Construct an ir_swizzle from the textual representation. Can fail.
423 static ir_swizzle
*create(ir_rvalue
*, const char *, unsigned vector_length
);
425 virtual void accept(ir_visitor
*v
)
432 return val
->is_lvalue();
436 ir_swizzle_mask mask
;
440 class ir_dereference
: public ir_rvalue
{
442 ir_dereference(struct ir_instruction
*);
444 ir_dereference(ir_instruction
*variable
, ir_rvalue
*array_index
);
446 virtual ir_dereference
*as_dereference()
451 virtual void accept(ir_visitor
*v
)
463 as_var
= var
->as_variable();
468 return !as_var
->read_only
;
472 ir_reference_variable
,
478 * Object being dereferenced.
480 * Must be either an \c ir_variable or an \c ir_rvalue.
485 ir_rvalue
*array_index
;
491 class ir_constant
: public ir_rvalue
{
493 ir_constant(const struct glsl_type
*type
, const void *data
);
495 ir_constant(unsigned int u
);
497 ir_constant(float f
);
499 virtual void accept(ir_visitor
*v
)
505 * Value of the constant.
507 * The field used to back the values supplied by the constant is determined
508 * by the type associated with the \c ir_instruction. Constants may be
509 * scalars, vectors, or matrices.
521 _mesa_glsl_initialize_variables(exec_list
*instructions
,
522 struct _mesa_glsl_parse_state
*state
);