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"
31 #include "ir_hierarchical_visitor.h"
38 * Base class of all IR instructions
40 class ir_instruction : public exec_node {
42 const struct glsl_type *type;
44 class ir_constant *constant_expression_value();
45 virtual void accept(ir_visitor *) = 0;
46 virtual ir_visitor_status accept(ir_hierarchical_visitor *) = 0;
49 * \name IR instruction downcast functions
51 * These functions either cast the object to a derived class or return
52 * \c NULL if the object's type does not match the specified derived class.
53 * Additional downcast functions will be added as needed.
56 virtual class ir_variable * as_variable() { return NULL; }
57 virtual class ir_function * as_function() { return NULL; }
58 virtual class ir_dereference * as_dereference() { return NULL; }
59 virtual class ir_rvalue * as_rvalue() { return NULL; }
60 virtual class ir_label * as_label() { return NULL; }
61 virtual class ir_loop * as_loop() { return NULL; }
62 virtual class ir_assignment * as_assignment() { return NULL; }
63 virtual class ir_call * as_call() { return NULL; }
64 virtual class ir_return * as_return() { return NULL; }
65 virtual class ir_if * as_if() { return NULL; }
66 virtual class ir_swizzle * as_swizzle() { return NULL; }
67 virtual class ir_constant * as_constant() { return NULL; }
78 class ir_rvalue : public ir_instruction {
80 virtual ir_rvalue * as_rvalue()
85 virtual bool is_lvalue()
91 * Get the variable that is ultimately referenced by an r-value
93 virtual ir_variable *variable_referenced()
100 * If an r-value is a reference to a whole variable, get that variable
103 * Pointer to a variable that is completely dereferenced by the r-value. If
104 * the r-value is not a dereference or the dereference does not access the
105 * entire variable (i.e., it's just one array element, struct field), \c NULL
108 virtual ir_variable *whole_variable_referenced()
121 enum ir_variable_mode {
129 enum ir_varaible_interpolation {
136 class ir_variable : public ir_instruction {
138 ir_variable(const struct glsl_type *, const char *);
140 virtual ir_variable *as_variable()
145 virtual void accept(ir_visitor *v)
150 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
153 * Duplicate an IR variable
156 * This will probably be made \c virtual and moved to the base class
159 ir_variable *clone() const
161 ir_variable *var = new ir_variable(type, name);
163 var->max_array_access = this->max_array_access;
164 var->read_only = this->read_only;
165 var->centroid = this->centroid;
166 var->invariant = this->invariant;
167 var->mode = this->mode;
168 var->interpolation = this->interpolation;
176 * Highest element accessed with a constant expression array index
178 * Not used for non-array variables.
180 unsigned max_array_access;
182 unsigned read_only:1;
184 unsigned invariant:1;
185 /** If the variable is initialized outside of the scope of the shader */
186 unsigned shader_in:1;
188 * If the variable value is later used outside of the scope of the shader.
190 unsigned shader_out:1;
193 unsigned interpolation:2;
196 * Flag that the whole array is assignable
198 * In GLSL 1.20 and later whole arrays are assignable (and comparable for
199 * equality). This flag enables this behavior.
201 unsigned array_lvalue:1;
204 * Emit a warning if this variable is accessed.
206 const char *warn_extension;
209 * Value assigned in the initializer of a variable declared "const"
211 ir_constant *constant_value;
217 * The representation of a function instance; may be the full definition or
218 * simply a prototype.
220 class ir_function_signature : public ir_instruction {
221 /* An ir_function_signature will be part of the list of signatures in
225 ir_function_signature(const glsl_type *return_type);
227 virtual void accept(ir_visitor *v)
232 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
235 * Get the name of the function for which this is a signature
237 const char *function_name() const;
240 * Check whether the qualifiers match between this signature's parameters
241 * and the supplied parameter list. If not, returns the name of the first
242 * parameter with mismatched qualifiers (for use in error messages).
244 const char *qualifiers_match(exec_list *params);
247 * Replace the current parameter list with the given one. This is useful
248 * if the current information came from a prototype, and either has invalid
249 * or missing parameter names.
251 void replace_parameters(exec_list *new_params);
254 * Function return type.
256 * \note This discards the optional precision qualifier.
258 const struct glsl_type *return_type;
261 * List of ir_variable of function parameters.
263 * This represents the storage. The paramaters passed in a particular
264 * call will be in ir_call::actual_paramaters.
266 struct exec_list parameters;
268 /** Whether or not this function has a body (which may be empty). */
269 unsigned is_defined:1;
271 /** Body of instructions in the function. */
272 struct exec_list body;
275 /** Function of which this signature is one overload. */
276 class ir_function *function;
278 friend class ir_function;
283 * Header for tracking multiple overloaded functions with the same name.
284 * Contains a list of ir_function_signatures representing each of the
287 class ir_function : public ir_instruction {
289 ir_function(const char *name);
291 virtual ir_function *as_function()
296 virtual void accept(ir_visitor *v)
301 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
303 void add_signature(ir_function_signature *sig)
305 sig->function = this;
306 signatures.push_tail(sig);
310 * Get an iterator for the set of function signatures
312 exec_list_iterator iterator()
314 return signatures.iterator();
318 * Find a signature that matches a set of actual parameters, taking implicit
319 * conversions into account.
321 const ir_function_signature *matching_signature(exec_list *actual_param);
324 * Find a signature that exactly matches a set of actual parameters without
325 * any implicit type conversions.
327 ir_function_signature *exact_matching_signature(exec_list *actual_ps);
330 * Name of the function.
336 * List of ir_function_signature for each overloaded function with this name.
338 struct exec_list signatures;
341 inline const char *ir_function_signature::function_name() const
343 return function->name;
349 * IR instruction representing high-level if-statements
351 class ir_if : public ir_instruction {
353 ir_if(ir_rvalue *condition)
354 : condition(condition)
359 virtual ir_if *as_if()
364 virtual void accept(ir_visitor *v)
369 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
371 ir_rvalue *condition;
372 /** List of ir_instruction for the body of the then branch */
373 exec_list then_instructions;
374 /** List of ir_instruction for the body of the else branch */
375 exec_list else_instructions;
380 * IR instruction representing a high-level loop structure.
382 class ir_loop : public ir_instruction {
384 ir_loop() : from(NULL), to(NULL), increment(NULL), counter(NULL)
389 virtual void accept(ir_visitor *v)
394 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
396 virtual ir_loop *as_loop()
402 * Get an iterator for the instructions of the loop body
404 exec_list_iterator iterator()
406 return body_instructions.iterator();
409 /** List of ir_instruction that make up the body of the loop. */
410 exec_list body_instructions;
413 * \name Loop counter and controls
418 ir_rvalue *increment;
419 ir_variable *counter;
424 class ir_assignment : public ir_rvalue {
426 ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition);
428 virtual void accept(ir_visitor *v)
433 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
435 virtual ir_assignment * as_assignment()
441 * Left-hand side of the assignment.
446 * Value being assigned
451 * Optional condition for the assignment.
453 ir_rvalue *condition;
456 /* Update ir_expression::num_operands() and operator_strs when
457 * updating this list.
459 enum ir_expression_operation {
472 ir_unop_f2i, /**< Float-to-integer conversion. */
473 ir_unop_i2f, /**< Integer-to-float conversion. */
474 ir_unop_f2b, /**< Float-to-boolean conversion */
475 ir_unop_b2f, /**< Boolean-to-float conversion */
476 ir_unop_i2b, /**< int-to-boolean conversion */
477 ir_unop_b2i, /**< Boolean-to-int conversion */
478 ir_unop_u2f, /**< Unsigned-to-float conversion. */
481 * \name Unary floating-point rounding operations.
490 * \name Trigonometric operations.
504 * \name Binary comparison operators
516 * \name Bit-wise binary operations.
537 class ir_expression : public ir_rvalue {
539 ir_expression(int op, const struct glsl_type *type,
540 ir_rvalue *, ir_rvalue *);
542 static unsigned int get_num_operands(ir_expression_operation);
543 unsigned int get_num_operands()
545 return get_num_operands(operation);
549 * Return a string representing this expression's operator.
551 const char *operator_string();
554 * Do a reverse-lookup to translate the given string into an operator.
556 static ir_expression_operation get_operator(const char *);
558 virtual void accept(ir_visitor *v)
563 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
565 ir_expression *clone();
567 ir_expression_operation operation;
568 ir_rvalue *operands[2];
573 * IR instruction representing a function call
575 class ir_call : public ir_rvalue {
577 ir_call(const ir_function_signature *callee, exec_list *actual_parameters)
580 assert(callee->return_type != NULL);
581 type = callee->return_type;
582 actual_parameters->move_nodes_to(& this->actual_parameters);
585 virtual ir_call *as_call()
590 virtual void accept(ir_visitor *v)
595 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
598 * Get a generic ir_call object when an error occurs
600 static ir_call *get_error_instruction();
603 * Get an iterator for the set of acutal parameters
605 exec_list_iterator iterator()
607 return actual_parameters.iterator();
611 * Get the name of the function being called.
613 const char *callee_name() const
615 return callee->function_name();
618 const ir_function_signature *get_callee()
624 * Generates an inline version of the function before @ir,
625 * returning the return value of the function.
627 ir_rvalue *generate_inline(ir_instruction *ir);
636 const ir_function_signature *callee;
638 /* List of ir_rvalue of paramaters passed in this call. */
639 exec_list actual_parameters;
644 * \name Jump-like IR instructions.
646 * These include \c break, \c continue, \c return, and \c discard.
649 class ir_jump : public ir_instruction {
657 class ir_return : public ir_jump {
665 ir_return(ir_rvalue *value)
671 virtual ir_return *as_return()
676 ir_rvalue *get_value() const
681 virtual void accept(ir_visitor *v)
686 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
694 * Jump instructions used inside loops
696 * These include \c break and \c continue. The \c break within a loop is
697 * different from the \c break within a switch-statement.
701 class ir_loop_jump : public ir_jump {
708 ir_loop_jump(ir_loop *loop, jump_mode mode)
709 : loop(loop), mode(mode)
714 virtual void accept(ir_visitor *v)
719 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
721 bool is_break() const
723 return mode == jump_break;
726 bool is_continue() const
728 return mode == jump_continue;
732 /** Loop containing this break instruction. */
735 /** Mode selector for the jump instruction. */
741 struct ir_swizzle_mask {
748 * Number of components in the swizzle.
750 unsigned num_components:3;
753 * Does the swizzle contain duplicate components?
755 * L-value swizzles cannot contain duplicate components.
757 unsigned has_duplicates:1;
761 class ir_swizzle : public ir_rvalue {
763 ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
765 ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask);
767 virtual ir_swizzle *as_swizzle()
774 return new ir_swizzle(this->val, this->mask);
778 * Construct an ir_swizzle from the textual representation. Can fail.
780 static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
782 virtual void accept(ir_visitor *v)
787 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
791 return val->is_lvalue() && !mask.has_duplicates;
795 * Get the variable that is ultimately referenced by an r-value
797 virtual ir_variable *variable_referenced();
800 ir_swizzle_mask mask;
804 class ir_dereference : public ir_rvalue {
806 virtual ir_dereference *as_dereference()
814 * Get the variable that is ultimately referenced by an r-value
816 virtual ir_variable *variable_referenced() = 0;
820 class ir_dereference_variable : public ir_dereference {
822 ir_dereference_variable(ir_variable *var);
825 * Get the variable that is ultimately referenced by an r-value
827 virtual ir_variable *variable_referenced()
832 virtual ir_variable *whole_variable_referenced()
834 /* ir_dereference_variable objects always dereference the entire
835 * variable. However, if this dereference is dereferenced by anything
836 * else, the complete deferefernce chain is not a whole-variable
837 * dereference. This method should only be called on the top most
838 * ir_rvalue in a dereference chain.
843 virtual void accept(ir_visitor *v)
848 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
851 * Object being dereferenced.
857 class ir_dereference_array : public ir_dereference {
859 ir_dereference_array(ir_rvalue *value, ir_rvalue *array_index);
861 ir_dereference_array(ir_variable *var, ir_rvalue *array_index);
864 * Get the variable that is ultimately referenced by an r-value
866 virtual ir_variable *variable_referenced()
868 return this->array->variable_referenced();
871 virtual void accept(ir_visitor *v)
876 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
879 ir_rvalue *array_index;
882 void set_array(ir_rvalue *value);
886 class ir_dereference_record : public ir_dereference {
888 ir_dereference_record(ir_rvalue *value, const char *field);
890 ir_dereference_record(ir_variable *var, const char *field);
893 * Get the variable that is ultimately referenced by an r-value
895 virtual ir_variable *variable_referenced()
897 return this->record->variable_referenced();
900 virtual void accept(ir_visitor *v)
905 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
912 class ir_constant : public ir_rvalue {
914 ir_constant(const struct glsl_type *type, const void *data);
916 ir_constant(unsigned int u);
918 ir_constant(float f);
920 virtual ir_constant *as_constant()
925 virtual void accept(ir_visitor *v)
930 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
934 return new ir_constant(this->type, &this->value);
938 * Value of the constant.
940 * The field used to back the values supplied by the constant is determined
941 * by the type associated with the \c ir_instruction. Constants may be
942 * scalars, vectors, or matrices.
953 visit_exec_list(exec_list *list, ir_visitor *visitor);
956 _mesa_glsl_initialize_variables(exec_list *instructions,
957 struct _mesa_glsl_parse_state *state);
960 _mesa_glsl_initialize_functions(exec_list *instructions,
961 struct _mesa_glsl_parse_state *state);