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_dereference_array * as_dereference_array() { return NULL; }
60 virtual class ir_rvalue * as_rvalue() { return NULL; }
61 virtual class ir_label * as_label() { return NULL; }
62 virtual class ir_loop * as_loop() { return NULL; }
63 virtual class ir_assignment * as_assignment() { return NULL; }
64 virtual class ir_call * as_call() { return NULL; }
65 virtual class ir_return * as_return() { return NULL; }
66 virtual class ir_if * as_if() { return NULL; }
67 virtual class ir_swizzle * as_swizzle() { return NULL; }
68 virtual class ir_constant * as_constant() { return NULL; }
79 class ir_rvalue : public ir_instruction {
81 virtual ir_rvalue * as_rvalue()
86 virtual bool is_lvalue()
92 * Get the variable that is ultimately referenced by an r-value
94 virtual ir_variable *variable_referenced()
101 * If an r-value is a reference to a whole variable, get that variable
104 * Pointer to a variable that is completely dereferenced by the r-value. If
105 * the r-value is not a dereference or the dereference does not access the
106 * entire variable (i.e., it's just one array element, struct field), \c NULL
109 virtual ir_variable *whole_variable_referenced()
122 enum ir_variable_mode {
130 enum ir_varaible_interpolation {
137 class ir_variable : public ir_instruction {
139 ir_variable(const struct glsl_type *, const char *);
141 virtual ir_variable *as_variable()
146 virtual void accept(ir_visitor *v)
151 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
154 * Duplicate an IR variable
157 * This will probably be made \c virtual and moved to the base class
160 ir_variable *clone() const
162 ir_variable *var = new ir_variable(type, name);
164 var->max_array_access = this->max_array_access;
165 var->read_only = this->read_only;
166 var->centroid = this->centroid;
167 var->invariant = this->invariant;
168 var->mode = this->mode;
169 var->interpolation = this->interpolation;
177 * Highest element accessed with a constant expression array index
179 * Not used for non-array variables.
181 unsigned max_array_access;
183 unsigned read_only:1;
185 unsigned invariant:1;
186 /** If the variable is initialized outside of the scope of the shader */
187 unsigned shader_in:1;
189 * If the variable value is later used outside of the scope of the shader.
191 unsigned shader_out:1;
194 unsigned interpolation:2;
197 * Flag that the whole array is assignable
199 * In GLSL 1.20 and later whole arrays are assignable (and comparable for
200 * equality). This flag enables this behavior.
202 unsigned array_lvalue:1;
205 * Emit a warning if this variable is accessed.
207 const char *warn_extension;
210 * Value assigned in the initializer of a variable declared "const"
212 ir_constant *constant_value;
218 * The representation of a function instance; may be the full definition or
219 * simply a prototype.
221 class ir_function_signature : public ir_instruction {
222 /* An ir_function_signature will be part of the list of signatures in
226 ir_function_signature(const glsl_type *return_type);
228 virtual void accept(ir_visitor *v)
233 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
236 * Get the name of the function for which this is a signature
238 const char *function_name() const;
241 * Check whether the qualifiers match between this signature's parameters
242 * and the supplied parameter list. If not, returns the name of the first
243 * parameter with mismatched qualifiers (for use in error messages).
245 const char *qualifiers_match(exec_list *params);
248 * Replace the current parameter list with the given one. This is useful
249 * if the current information came from a prototype, and either has invalid
250 * or missing parameter names.
252 void replace_parameters(exec_list *new_params);
255 * Function return type.
257 * \note This discards the optional precision qualifier.
259 const struct glsl_type *return_type;
262 * List of ir_variable of function parameters.
264 * This represents the storage. The paramaters passed in a particular
265 * call will be in ir_call::actual_paramaters.
267 struct exec_list parameters;
269 /** Whether or not this function has a body (which may be empty). */
270 unsigned is_defined:1;
272 /** Body of instructions in the function. */
273 struct exec_list body;
276 /** Function of which this signature is one overload. */
277 class ir_function *function;
279 friend class ir_function;
284 * Header for tracking multiple overloaded functions with the same name.
285 * Contains a list of ir_function_signatures representing each of the
288 class ir_function : public ir_instruction {
290 ir_function(const char *name);
292 virtual ir_function *as_function()
297 virtual void accept(ir_visitor *v)
302 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
304 void add_signature(ir_function_signature *sig)
306 sig->function = this;
307 signatures.push_tail(sig);
311 * Get an iterator for the set of function signatures
313 exec_list_iterator iterator()
315 return signatures.iterator();
319 * Find a signature that matches a set of actual parameters, taking implicit
320 * conversions into account.
322 const ir_function_signature *matching_signature(exec_list *actual_param);
325 * Find a signature that exactly matches a set of actual parameters without
326 * any implicit type conversions.
328 ir_function_signature *exact_matching_signature(exec_list *actual_ps);
331 * Name of the function.
337 * List of ir_function_signature for each overloaded function with this name.
339 struct exec_list signatures;
342 inline const char *ir_function_signature::function_name() const
344 return function->name;
350 * IR instruction representing high-level if-statements
352 class ir_if : public ir_instruction {
354 ir_if(ir_rvalue *condition)
355 : condition(condition)
360 virtual ir_if *as_if()
365 virtual void accept(ir_visitor *v)
370 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
372 ir_rvalue *condition;
373 /** List of ir_instruction for the body of the then branch */
374 exec_list then_instructions;
375 /** List of ir_instruction for the body of the else branch */
376 exec_list else_instructions;
381 * IR instruction representing a high-level loop structure.
383 class ir_loop : public ir_instruction {
385 ir_loop() : from(NULL), to(NULL), increment(NULL), counter(NULL)
390 virtual void accept(ir_visitor *v)
395 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
397 virtual ir_loop *as_loop()
403 * Get an iterator for the instructions of the loop body
405 exec_list_iterator iterator()
407 return body_instructions.iterator();
410 /** List of ir_instruction that make up the body of the loop. */
411 exec_list body_instructions;
414 * \name Loop counter and controls
419 ir_rvalue *increment;
420 ir_variable *counter;
425 class ir_assignment : public ir_rvalue {
427 ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition);
429 virtual void accept(ir_visitor *v)
434 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
436 virtual ir_assignment * as_assignment()
442 * Left-hand side of the assignment.
447 * Value being assigned
452 * Optional condition for the assignment.
454 ir_rvalue *condition;
457 /* Update ir_expression::num_operands() and operator_strs when
458 * updating this list.
460 enum ir_expression_operation {
473 ir_unop_f2i, /**< Float-to-integer conversion. */
474 ir_unop_i2f, /**< Integer-to-float conversion. */
475 ir_unop_f2b, /**< Float-to-boolean conversion */
476 ir_unop_b2f, /**< Boolean-to-float conversion */
477 ir_unop_i2b, /**< int-to-boolean conversion */
478 ir_unop_b2i, /**< Boolean-to-int conversion */
479 ir_unop_u2f, /**< Unsigned-to-float conversion. */
482 * \name Unary floating-point rounding operations.
491 * \name Trigonometric operations.
505 * \name Binary comparison operators
517 * \name Bit-wise binary operations.
538 class ir_expression : public ir_rvalue {
540 ir_expression(int op, const struct glsl_type *type,
541 ir_rvalue *, ir_rvalue *);
543 static unsigned int get_num_operands(ir_expression_operation);
544 unsigned int get_num_operands()
546 return get_num_operands(operation);
550 * Return a string representing this expression's operator.
552 const char *operator_string();
555 * Do a reverse-lookup to translate the given string into an operator.
557 static ir_expression_operation get_operator(const char *);
559 virtual void accept(ir_visitor *v)
564 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
566 ir_expression *clone();
568 ir_expression_operation operation;
569 ir_rvalue *operands[2];
574 * IR instruction representing a function call
576 class ir_call : public ir_rvalue {
578 ir_call(const ir_function_signature *callee, exec_list *actual_parameters)
581 assert(callee->return_type != NULL);
582 type = callee->return_type;
583 actual_parameters->move_nodes_to(& this->actual_parameters);
586 virtual ir_call *as_call()
591 virtual void accept(ir_visitor *v)
596 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
599 * Get a generic ir_call object when an error occurs
601 static ir_call *get_error_instruction();
604 * Get an iterator for the set of acutal parameters
606 exec_list_iterator iterator()
608 return actual_parameters.iterator();
612 * Get the name of the function being called.
614 const char *callee_name() const
616 return callee->function_name();
619 const ir_function_signature *get_callee()
625 * Generates an inline version of the function before @ir,
626 * returning the return value of the function.
628 ir_rvalue *generate_inline(ir_instruction *ir);
637 const ir_function_signature *callee;
639 /* List of ir_rvalue of paramaters passed in this call. */
640 exec_list actual_parameters;
645 * \name Jump-like IR instructions.
647 * These include \c break, \c continue, \c return, and \c discard.
650 class ir_jump : public ir_instruction {
658 class ir_return : public ir_jump {
666 ir_return(ir_rvalue *value)
672 virtual ir_return *as_return()
677 ir_rvalue *get_value() const
682 virtual void accept(ir_visitor *v)
687 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);
863 virtual ir_dereference_array *as_dereference_array()
869 * Get the variable that is ultimately referenced by an r-value
871 virtual ir_variable *variable_referenced()
873 return this->array->variable_referenced();
876 virtual void accept(ir_visitor *v)
881 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
884 ir_rvalue *array_index;
887 void set_array(ir_rvalue *value);
891 class ir_dereference_record : public ir_dereference {
893 ir_dereference_record(ir_rvalue *value, const char *field);
895 ir_dereference_record(ir_variable *var, const char *field);
898 * Get the variable that is ultimately referenced by an r-value
900 virtual ir_variable *variable_referenced()
902 return this->record->variable_referenced();
905 virtual void accept(ir_visitor *v)
910 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
917 class ir_constant : public ir_rvalue {
919 ir_constant(const struct glsl_type *type, const void *data);
921 ir_constant(unsigned int u);
923 ir_constant(float f);
925 virtual ir_constant *as_constant()
930 virtual void accept(ir_visitor *v)
935 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
939 return new ir_constant(this->type, &this->value);
943 * Value of the constant.
945 * The field used to back the values supplied by the constant is determined
946 * by the type associated with the \c ir_instruction. Constants may be
947 * scalars, vectors, or matrices.
958 visit_exec_list(exec_list *list, ir_visitor *visitor);
961 _mesa_glsl_initialize_variables(exec_list *instructions,
962 struct _mesa_glsl_parse_state *state);
965 _mesa_glsl_initialize_functions(exec_list *instructions,
966 struct _mesa_glsl_parse_state *state);