use log2 from stdc
[AROS.git] / workbench / libs / mesa / src / glsl / ir.h
blob89aa13839207ca5dbfef92993fe4c2de99e88ea3
1 /* -*- c++ -*- */
2 /*
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
14 * Software.
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.
25 #pragma once
26 #ifndef IR_H
27 #define IR_H
29 #include <stdio.h>
30 #include <stdlib.h>
32 #include "ralloc.h"
33 #include "glsl_types.h"
34 #include "list.h"
35 #include "ir_visitor.h"
36 #include "ir_hierarchical_visitor.h"
38 /**
39 * \defgroup IR Intermediate representation nodes
41 * @{
44 #if defined(__AROS__)
45 #include <aros/debug.h>
46 #undef VOLATILE
47 #undef STATIC
48 #define printf(fmt, ...) bug(fmt, ##__VA_ARGS__)
49 #endif
51 /**
52 * Class tags
54 * Each concrete class derived from \c ir_instruction has a value in this
55 * enumerant. The value for the type is stored in \c ir_instruction::ir_type
56 * by the constructor. While using type tags is not very C++, it is extremely
57 * convenient. For example, during debugging you can simply inspect
58 * \c ir_instruction::ir_type to find out the actual type of the object.
60 * In addition, it is possible to use a switch-statement based on \c
61 * \c ir_instruction::ir_type to select different behavior for different object
62 * types. For functions that have only slight differences for several object
63 * types, this allows writing very straightforward, readable code.
65 enum ir_node_type {
66 /**
67 * Zero is unused so that the IR validator can detect cases where
68 * \c ir_instruction::ir_type has not been initialized.
70 ir_type_unset,
71 ir_type_variable,
72 ir_type_assignment,
73 ir_type_call,
74 ir_type_constant,
75 ir_type_dereference_array,
76 ir_type_dereference_record,
77 ir_type_dereference_variable,
78 ir_type_discard,
79 ir_type_expression,
80 ir_type_function,
81 ir_type_function_signature,
82 ir_type_if,
83 ir_type_loop,
84 ir_type_loop_jump,
85 ir_type_return,
86 ir_type_swizzle,
87 ir_type_texture,
88 ir_type_max /**< maximum ir_type enum number, for validation */
91 /**
92 * Base class of all IR instructions
94 class ir_instruction : public exec_node {
95 public:
96 enum ir_node_type ir_type;
97 const struct glsl_type *type;
99 /** ir_print_visitor helper for debugging. */
100 void print(void) const;
102 virtual void accept(ir_visitor *) = 0;
103 virtual ir_visitor_status accept(ir_hierarchical_visitor *) = 0;
104 virtual ir_instruction *clone(void *mem_ctx,
105 struct hash_table *ht) const = 0;
108 * \name IR instruction downcast functions
110 * These functions either cast the object to a derived class or return
111 * \c NULL if the object's type does not match the specified derived class.
112 * Additional downcast functions will be added as needed.
114 /*@{*/
115 virtual class ir_variable * as_variable() { return NULL; }
116 virtual class ir_function * as_function() { return NULL; }
117 virtual class ir_dereference * as_dereference() { return NULL; }
118 virtual class ir_dereference_array * as_dereference_array() { return NULL; }
119 virtual class ir_dereference_variable *as_dereference_variable() { return NULL; }
120 virtual class ir_expression * as_expression() { return NULL; }
121 virtual class ir_rvalue * as_rvalue() { return NULL; }
122 virtual class ir_loop * as_loop() { return NULL; }
123 virtual class ir_assignment * as_assignment() { return NULL; }
124 virtual class ir_call * as_call() { return NULL; }
125 virtual class ir_return * as_return() { return NULL; }
126 virtual class ir_if * as_if() { return NULL; }
127 virtual class ir_swizzle * as_swizzle() { return NULL; }
128 virtual class ir_constant * as_constant() { return NULL; }
129 virtual class ir_discard * as_discard() { return NULL; }
130 /*@}*/
132 protected:
133 ir_instruction()
135 ir_type = ir_type_unset;
136 type = NULL;
138 virtual ~ir_instruction() { }
142 class ir_rvalue : public ir_instruction {
143 public:
144 virtual ir_rvalue *clone(void *mem_ctx, struct hash_table *) const = 0;
146 virtual ir_constant *constant_expression_value() = 0;
148 virtual ir_rvalue * as_rvalue()
150 return this;
153 ir_rvalue *as_rvalue_to_saturate();
155 virtual bool is_lvalue()
157 return false;
161 * Get the variable that is ultimately referenced by an r-value
163 virtual ir_variable *variable_referenced()
165 return NULL;
170 * If an r-value is a reference to a whole variable, get that variable
172 * \return
173 * Pointer to a variable that is completely dereferenced by the r-value. If
174 * the r-value is not a dereference or the dereference does not access the
175 * entire variable (i.e., it's just one array element, struct field), \c NULL
176 * is returned.
178 virtual ir_variable *whole_variable_referenced()
180 return NULL;
184 * Determine if an r-value has the value zero
186 * The base implementation of this function always returns \c false. The
187 * \c ir_constant class over-rides this function to return \c true \b only
188 * for vector and scalar types that have all elements set to the value
189 * zero (or \c false for booleans).
191 * \sa ir_constant::has_value, ir_rvalue::is_one, ir_rvalue::is_negative_one
193 virtual bool is_zero() const;
196 * Determine if an r-value has the value one
198 * The base implementation of this function always returns \c false. The
199 * \c ir_constant class over-rides this function to return \c true \b only
200 * for vector and scalar types that have all elements set to the value
201 * one (or \c true for booleans).
203 * \sa ir_constant::has_value, ir_rvalue::is_zero, ir_rvalue::is_negative_one
205 virtual bool is_one() const;
208 * Determine if an r-value has the value negative one
210 * The base implementation of this function always returns \c false. The
211 * \c ir_constant class over-rides this function to return \c true \b only
212 * for vector and scalar types that have all elements set to the value
213 * negative one. For boolean times, the result is always \c false.
215 * \sa ir_constant::has_value, ir_rvalue::is_zero, ir_rvalue::is_one
217 virtual bool is_negative_one() const;
219 protected:
220 ir_rvalue();
225 * Variable storage classes
227 enum ir_variable_mode {
228 ir_var_auto = 0, /**< Function local variables and globals. */
229 ir_var_uniform, /**< Variable declared as a uniform. */
230 ir_var_in,
231 ir_var_out,
232 ir_var_inout,
233 ir_var_const_in, /**< "in" param that must be a constant expression */
234 ir_var_system_value, /**< Ex: front-face, instance-id, etc. */
235 ir_var_temporary /**< Temporary variable generated during compilation. */
238 enum ir_variable_interpolation {
239 ir_var_smooth = 0,
240 ir_var_flat,
241 ir_var_noperspective
245 * \brief Layout qualifiers for gl_FragDepth.
247 * The AMD_conservative_depth extension allows gl_FragDepth to be redeclared
248 * with a layout qualifier.
250 enum ir_depth_layout {
251 ir_depth_layout_none, /**< No depth layout is specified. */
252 ir_depth_layout_any,
253 ir_depth_layout_greater,
254 ir_depth_layout_less,
255 ir_depth_layout_unchanged
259 * \brief Convert depth layout qualifier to string.
261 const char*
262 depth_layout_string(ir_depth_layout layout);
265 * Description of built-in state associated with a uniform
267 * \sa ir_variable::state_slots
269 struct ir_state_slot {
270 int tokens[5];
271 int swizzle;
274 class ir_variable : public ir_instruction {
275 public:
276 ir_variable(const struct glsl_type *, const char *, ir_variable_mode);
278 virtual ir_variable *clone(void *mem_ctx, struct hash_table *ht) const;
280 virtual ir_variable *as_variable()
282 return this;
285 virtual void accept(ir_visitor *v)
287 v->visit(this);
290 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
294 * Get the string value for the interpolation qualifier
296 * \return The string that would be used in a shader to specify \c
297 * mode will be returned.
299 * This function should only be used on a shader input or output variable.
301 const char *interpolation_string() const;
304 * Calculate the number of slots required to hold this variable
306 * This is used to determine how many uniform or varying locations a variable
307 * occupies. The count is in units of floating point components.
309 unsigned component_slots() const;
312 * Delcared name of the variable
314 const char *name;
317 * Highest element accessed with a constant expression array index
319 * Not used for non-array variables.
321 unsigned max_array_access;
324 * Is the variable read-only?
326 * This is set for variables declared as \c const, shader inputs,
327 * and uniforms.
329 unsigned read_only:1;
330 unsigned centroid:1;
331 unsigned invariant:1;
334 * Has this variable been used for reading or writing?
336 * Several GLSL semantic checks require knowledge of whether or not a
337 * variable has been used. For example, it is an error to redeclare a
338 * variable as invariant after it has been used.
340 unsigned used:1;
343 * Storage class of the variable.
345 * \sa ir_variable_mode
347 unsigned mode:3;
350 * Interpolation mode for shader inputs / outputs
352 * \sa ir_variable_interpolation
354 unsigned interpolation:2;
357 * Flag that the whole array is assignable
359 * In GLSL 1.20 and later whole arrays are assignable (and comparable for
360 * equality). This flag enables this behavior.
362 unsigned array_lvalue:1;
365 * \name ARB_fragment_coord_conventions
366 * @{
368 unsigned origin_upper_left:1;
369 unsigned pixel_center_integer:1;
370 /*@}*/
373 * \brief Layout qualifier for gl_FragDepth.
375 * This is not equal to \c ir_depth_layout_none if and only if this
376 * variable is \c gl_FragDepth and a layout qualifier is specified.
378 ir_depth_layout depth_layout;
381 * Was the location explicitly set in the shader?
383 * If the location is explicitly set in the shader, it \b cannot be changed
384 * by the linker or by the API (e.g., calls to \c glBindAttribLocation have
385 * no effect).
387 unsigned explicit_location:1;
390 * Storage location of the base of this variable
392 * The precise meaning of this field depends on the nature of the variable.
394 * - Vertex shader input: one of the values from \c gl_vert_attrib.
395 * - Vertex shader output: one of the values from \c gl_vert_result.
396 * - Fragment shader input: one of the values from \c gl_frag_attrib.
397 * - Fragment shader output: one of the values from \c gl_frag_result.
398 * - Uniforms: Per-stage uniform slot number.
399 * - Other: This field is not currently used.
401 * If the variable is a uniform, shader input, or shader output, and the
402 * slot has not been assigned, the value will be -1.
404 int location;
407 * Built-in state that backs this uniform
409 * Once set at variable creation, \c state_slots must remain invariant.
410 * This is because, ideally, this array would be shared by all clones of
411 * this variable in the IR tree. In other words, we'd really like for it
412 * to be a fly-weight.
414 * If the variable is not a uniform, \c num_state_slots will be zero and
415 * \c state_slots will be \c NULL.
417 /*@{*/
418 unsigned num_state_slots; /**< Number of state slots used */
419 ir_state_slot *state_slots; /**< State descriptors. */
420 /*@}*/
423 * Emit a warning if this variable is accessed.
425 const char *warn_extension;
428 * Value assigned in the initializer of a variable declared "const"
430 ir_constant *constant_value;
434 /*@{*/
436 * The representation of a function instance; may be the full definition or
437 * simply a prototype.
439 class ir_function_signature : public ir_instruction {
440 /* An ir_function_signature will be part of the list of signatures in
441 * an ir_function.
443 public:
444 ir_function_signature(const glsl_type *return_type);
446 virtual ir_function_signature *clone(void *mem_ctx,
447 struct hash_table *ht) const;
448 ir_function_signature *clone_prototype(void *mem_ctx,
449 struct hash_table *ht) const;
451 virtual void accept(ir_visitor *v)
453 v->visit(this);
456 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
459 * Get the name of the function for which this is a signature
461 const char *function_name() const;
464 * Get a handle to the function for which this is a signature
466 * There is no setter function, this function returns a \c const pointer,
467 * and \c ir_function_signature::_function is private for a reason. The
468 * only way to make a connection between a function and function signature
469 * is via \c ir_function::add_signature. This helps ensure that certain
470 * invariants (i.e., a function signature is in the list of signatures for
471 * its \c _function) are met.
473 * \sa ir_function::add_signature
475 inline const class ir_function *function() const
477 return this->_function;
481 * Check whether the qualifiers match between this signature's parameters
482 * and the supplied parameter list. If not, returns the name of the first
483 * parameter with mismatched qualifiers (for use in error messages).
485 const char *qualifiers_match(exec_list *params);
488 * Replace the current parameter list with the given one. This is useful
489 * if the current information came from a prototype, and either has invalid
490 * or missing parameter names.
492 void replace_parameters(exec_list *new_params);
495 * Function return type.
497 * \note This discards the optional precision qualifier.
499 const struct glsl_type *return_type;
502 * List of ir_variable of function parameters.
504 * This represents the storage. The paramaters passed in a particular
505 * call will be in ir_call::actual_paramaters.
507 struct exec_list parameters;
509 /** Whether or not this function has a body (which may be empty). */
510 unsigned is_defined:1;
512 /** Whether or not this function signature is a built-in. */
513 unsigned is_builtin:1;
515 /** Body of instructions in the function. */
516 struct exec_list body;
518 private:
519 /** Function of which this signature is one overload. */
520 class ir_function *_function;
522 friend class ir_function;
527 * Header for tracking multiple overloaded functions with the same name.
528 * Contains a list of ir_function_signatures representing each of the
529 * actual functions.
531 class ir_function : public ir_instruction {
532 public:
533 ir_function(const char *name);
535 virtual ir_function *clone(void *mem_ctx, struct hash_table *ht) const;
537 virtual ir_function *as_function()
539 return this;
542 virtual void accept(ir_visitor *v)
544 v->visit(this);
547 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
549 void add_signature(ir_function_signature *sig)
551 sig->_function = this;
552 this->signatures.push_tail(sig);
556 * Get an iterator for the set of function signatures
558 exec_list_iterator iterator()
560 return signatures.iterator();
564 * Find a signature that matches a set of actual parameters, taking implicit
565 * conversions into account.
567 ir_function_signature *matching_signature(const exec_list *actual_param);
570 * Find a signature that exactly matches a set of actual parameters without
571 * any implicit type conversions.
573 ir_function_signature *exact_matching_signature(const exec_list *actual_ps);
576 * Name of the function.
578 const char *name;
580 /** Whether or not this function has a signature that isn't a built-in. */
581 bool has_user_signature();
584 * List of ir_function_signature for each overloaded function with this name.
586 struct exec_list signatures;
589 inline const char *ir_function_signature::function_name() const
591 return this->_function->name;
593 /*@}*/
597 * IR instruction representing high-level if-statements
599 class ir_if : public ir_instruction {
600 public:
601 ir_if(ir_rvalue *condition)
602 : condition(condition)
604 ir_type = ir_type_if;
607 virtual ir_if *clone(void *mem_ctx, struct hash_table *ht) const;
609 virtual ir_if *as_if()
611 return this;
614 virtual void accept(ir_visitor *v)
616 v->visit(this);
619 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
621 ir_rvalue *condition;
622 /** List of ir_instruction for the body of the then branch */
623 exec_list then_instructions;
624 /** List of ir_instruction for the body of the else branch */
625 exec_list else_instructions;
630 * IR instruction representing a high-level loop structure.
632 class ir_loop : public ir_instruction {
633 public:
634 ir_loop();
636 virtual ir_loop *clone(void *mem_ctx, struct hash_table *ht) const;
638 virtual void accept(ir_visitor *v)
640 v->visit(this);
643 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
645 virtual ir_loop *as_loop()
647 return this;
651 * Get an iterator for the instructions of the loop body
653 exec_list_iterator iterator()
655 return body_instructions.iterator();
658 /** List of ir_instruction that make up the body of the loop. */
659 exec_list body_instructions;
662 * \name Loop counter and controls
664 * Represents a loop like a FORTRAN \c do-loop.
666 * \note
667 * If \c from and \c to are the same value, the loop will execute once.
669 /*@{*/
670 ir_rvalue *from; /** Value of the loop counter on the first
671 * iteration of the loop.
673 ir_rvalue *to; /** Value of the loop counter on the last
674 * iteration of the loop.
676 ir_rvalue *increment;
677 ir_variable *counter;
680 * Comparison operation in the loop terminator.
682 * If any of the loop control fields are non-\c NULL, this field must be
683 * one of \c ir_binop_less, \c ir_binop_greater, \c ir_binop_lequal,
684 * \c ir_binop_gequal, \c ir_binop_equal, or \c ir_binop_nequal.
686 int cmp;
687 /*@}*/
691 class ir_assignment : public ir_instruction {
692 public:
693 ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition);
696 * Construct an assignment with an explicit write mask
698 * \note
699 * Since a write mask is supplied, the LHS must already be a bare
700 * \c ir_dereference. The cannot be any swizzles in the LHS.
702 ir_assignment(ir_dereference *lhs, ir_rvalue *rhs, ir_rvalue *condition,
703 unsigned write_mask);
705 virtual ir_assignment *clone(void *mem_ctx, struct hash_table *ht) const;
707 virtual ir_constant *constant_expression_value();
709 virtual void accept(ir_visitor *v)
711 v->visit(this);
714 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
716 virtual ir_assignment * as_assignment()
718 return this;
722 * Get a whole variable written by an assignment
724 * If the LHS of the assignment writes a whole variable, the variable is
725 * returned. Otherwise \c NULL is returned. Examples of whole-variable
726 * assignment are:
728 * - Assigning to a scalar
729 * - Assigning to all components of a vector
730 * - Whole array (or matrix) assignment
731 * - Whole structure assignment
733 ir_variable *whole_variable_written();
736 * Set the LHS of an assignment
738 void set_lhs(ir_rvalue *lhs);
741 * Left-hand side of the assignment.
743 * This should be treated as read only. If you need to set the LHS of an
744 * assignment, use \c ir_assignment::set_lhs.
746 ir_dereference *lhs;
749 * Value being assigned
751 ir_rvalue *rhs;
754 * Optional condition for the assignment.
756 ir_rvalue *condition;
760 * Component mask written
762 * For non-vector types in the LHS, this field will be zero. For vector
763 * types, a bit will be set for each component that is written. Note that
764 * for \c vec2 and \c vec3 types only the lower bits will ever be set.
766 * A partially-set write mask means that each enabled channel gets
767 * the value from a consecutive channel of the rhs. For example,
768 * to write just .xyw of gl_FrontColor with color:
770 * (assign (constant bool (1)) (xyw)
771 * (var_ref gl_FragColor)
772 * (swiz xyw (var_ref color)))
774 unsigned write_mask:4;
777 /* Update ir_expression::num_operands() and operator_strs when
778 * updating this list.
780 enum ir_expression_operation {
781 ir_unop_bit_not,
782 ir_unop_logic_not,
783 ir_unop_neg,
784 ir_unop_abs,
785 ir_unop_sign,
786 ir_unop_rcp,
787 ir_unop_rsq,
788 ir_unop_sqrt,
789 ir_unop_exp, /**< Log base e on gentype */
790 ir_unop_log, /**< Natural log on gentype */
791 ir_unop_exp2,
792 ir_unop_log2,
793 ir_unop_f2i, /**< Float-to-integer conversion. */
794 ir_unop_i2f, /**< Integer-to-float conversion. */
795 ir_unop_f2b, /**< Float-to-boolean conversion */
796 ir_unop_b2f, /**< Boolean-to-float conversion */
797 ir_unop_i2b, /**< int-to-boolean conversion */
798 ir_unop_b2i, /**< Boolean-to-int conversion */
799 ir_unop_u2f, /**< Unsigned-to-float conversion. */
800 ir_unop_any,
803 * \name Unary floating-point rounding operations.
805 /*@{*/
806 ir_unop_trunc,
807 ir_unop_ceil,
808 ir_unop_floor,
809 ir_unop_fract,
810 ir_unop_round_even,
811 /*@}*/
814 * \name Trigonometric operations.
816 /*@{*/
817 ir_unop_sin,
818 ir_unop_cos,
819 ir_unop_sin_reduced, /**< Reduced range sin. [-pi, pi] */
820 ir_unop_cos_reduced, /**< Reduced range cos. [-pi, pi] */
821 /*@}*/
824 * \name Partial derivatives.
826 /*@{*/
827 ir_unop_dFdx,
828 ir_unop_dFdy,
829 /*@}*/
831 ir_unop_noise,
834 * A sentinel marking the last of the unary operations.
836 ir_last_unop = ir_unop_noise,
838 ir_binop_add,
839 ir_binop_sub,
840 ir_binop_mul,
841 ir_binop_div,
844 * Takes one of two combinations of arguments:
846 * - mod(vecN, vecN)
847 * - mod(vecN, float)
849 * Does not take integer types.
851 ir_binop_mod,
854 * \name Binary comparison operators which return a boolean vector.
855 * The type of both operands must be equal.
857 /*@{*/
858 ir_binop_less,
859 ir_binop_greater,
860 ir_binop_lequal,
861 ir_binop_gequal,
862 ir_binop_equal,
863 ir_binop_nequal,
865 * Returns single boolean for whether all components of operands[0]
866 * equal the components of operands[1].
868 ir_binop_all_equal,
870 * Returns single boolean for whether any component of operands[0]
871 * is not equal to the corresponding component of operands[1].
873 ir_binop_any_nequal,
874 /*@}*/
877 * \name Bit-wise binary operations.
879 /*@{*/
880 ir_binop_lshift,
881 ir_binop_rshift,
882 ir_binop_bit_and,
883 ir_binop_bit_xor,
884 ir_binop_bit_or,
885 /*@}*/
887 ir_binop_logic_and,
888 ir_binop_logic_xor,
889 ir_binop_logic_or,
891 ir_binop_dot,
892 ir_binop_min,
893 ir_binop_max,
895 ir_binop_pow,
898 * A sentinel marking the last of the binary operations.
900 ir_last_binop = ir_binop_pow,
902 ir_quadop_vector,
905 * A sentinel marking the last of all operations.
907 ir_last_opcode = ir_last_binop
910 class ir_expression : public ir_rvalue {
911 public:
913 * Constructor for unary operation expressions
915 ir_expression(int op, const struct glsl_type *type, ir_rvalue *);
916 ir_expression(int op, ir_rvalue *);
919 * Constructor for binary operation expressions
921 ir_expression(int op, const struct glsl_type *type,
922 ir_rvalue *, ir_rvalue *);
923 ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1);
926 * Constructor for quad operator expressions
928 ir_expression(int op, const struct glsl_type *type,
929 ir_rvalue *, ir_rvalue *, ir_rvalue *, ir_rvalue *);
931 virtual ir_expression *as_expression()
933 return this;
936 virtual ir_expression *clone(void *mem_ctx, struct hash_table *ht) const;
939 * Attempt to constant-fold the expression
941 * If the expression cannot be constant folded, this method will return
942 * \c NULL.
944 virtual ir_constant *constant_expression_value();
947 * Determine the number of operands used by an expression
949 static unsigned int get_num_operands(ir_expression_operation);
952 * Determine the number of operands used by an expression
954 unsigned int get_num_operands() const
956 return (this->operation == ir_quadop_vector)
957 ? this->type->vector_elements : get_num_operands(operation);
961 * Return a string representing this expression's operator.
963 const char *operator_string();
966 * Return a string representing this expression's operator.
968 static const char *operator_string(ir_expression_operation);
972 * Do a reverse-lookup to translate the given string into an operator.
974 static ir_expression_operation get_operator(const char *);
976 virtual void accept(ir_visitor *v)
978 v->visit(this);
981 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
983 ir_expression_operation operation;
984 ir_rvalue *operands[4];
989 * IR instruction representing a function call
991 class ir_call : public ir_rvalue {
992 public:
993 ir_call(ir_function_signature *callee, exec_list *actual_parameters)
994 : callee(callee)
996 ir_type = ir_type_call;
997 assert(callee->return_type != NULL);
998 type = callee->return_type;
999 actual_parameters->move_nodes_to(& this->actual_parameters);
1000 this->use_builtin = callee->is_builtin;
1003 virtual ir_call *clone(void *mem_ctx, struct hash_table *ht) const;
1005 virtual ir_constant *constant_expression_value();
1007 virtual ir_call *as_call()
1009 return this;
1012 virtual void accept(ir_visitor *v)
1014 v->visit(this);
1017 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1020 * Get a generic ir_call object when an error occurs
1022 * Any allocation will be performed with 'ctx' as ralloc owner.
1024 static ir_call *get_error_instruction(void *ctx);
1027 * Get an iterator for the set of acutal parameters
1029 exec_list_iterator iterator()
1031 return actual_parameters.iterator();
1035 * Get the name of the function being called.
1037 const char *callee_name() const
1039 return callee->function_name();
1043 * Get the function signature bound to this function call
1045 ir_function_signature *get_callee()
1047 return callee;
1051 * Set the function call target
1053 void set_callee(ir_function_signature *sig);
1056 * Generates an inline version of the function before @ir,
1057 * returning the return value of the function.
1059 ir_rvalue *generate_inline(ir_instruction *ir);
1061 /* List of ir_rvalue of paramaters passed in this call. */
1062 exec_list actual_parameters;
1064 /** Should this call only bind to a built-in function? */
1065 bool use_builtin;
1067 private:
1068 ir_call()
1069 : callee(NULL)
1071 this->ir_type = ir_type_call;
1074 ir_function_signature *callee;
1079 * \name Jump-like IR instructions.
1081 * These include \c break, \c continue, \c return, and \c discard.
1083 /*@{*/
1084 class ir_jump : public ir_instruction {
1085 protected:
1086 ir_jump()
1088 ir_type = ir_type_unset;
1092 class ir_return : public ir_jump {
1093 public:
1094 ir_return()
1095 : value(NULL)
1097 this->ir_type = ir_type_return;
1100 ir_return(ir_rvalue *value)
1101 : value(value)
1103 this->ir_type = ir_type_return;
1106 virtual ir_return *clone(void *mem_ctx, struct hash_table *) const;
1108 virtual ir_return *as_return()
1110 return this;
1113 ir_rvalue *get_value() const
1115 return value;
1118 virtual void accept(ir_visitor *v)
1120 v->visit(this);
1123 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1125 ir_rvalue *value;
1130 * Jump instructions used inside loops
1132 * These include \c break and \c continue. The \c break within a loop is
1133 * different from the \c break within a switch-statement.
1135 * \sa ir_switch_jump
1137 class ir_loop_jump : public ir_jump {
1138 public:
1139 enum jump_mode {
1140 jump_break,
1141 jump_continue
1144 ir_loop_jump(jump_mode mode)
1146 this->ir_type = ir_type_loop_jump;
1147 this->mode = mode;
1148 this->loop = loop;
1151 virtual ir_loop_jump *clone(void *mem_ctx, struct hash_table *) const;
1153 virtual void accept(ir_visitor *v)
1155 v->visit(this);
1158 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1160 bool is_break() const
1162 return mode == jump_break;
1165 bool is_continue() const
1167 return mode == jump_continue;
1170 /** Mode selector for the jump instruction. */
1171 enum jump_mode mode;
1172 private:
1173 /** Loop containing this break instruction. */
1174 ir_loop *loop;
1178 * IR instruction representing discard statements.
1180 class ir_discard : public ir_jump {
1181 public:
1182 ir_discard()
1184 this->ir_type = ir_type_discard;
1185 this->condition = NULL;
1188 ir_discard(ir_rvalue *cond)
1190 this->ir_type = ir_type_discard;
1191 this->condition = cond;
1194 virtual ir_discard *clone(void *mem_ctx, struct hash_table *ht) const;
1196 virtual void accept(ir_visitor *v)
1198 v->visit(this);
1201 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1203 virtual ir_discard *as_discard()
1205 return this;
1208 ir_rvalue *condition;
1210 /*@}*/
1214 * Texture sampling opcodes used in ir_texture
1216 enum ir_texture_opcode {
1217 ir_tex, /**< Regular texture look-up */
1218 ir_txb, /**< Texture look-up with LOD bias */
1219 ir_txl, /**< Texture look-up with explicit LOD */
1220 ir_txd, /**< Texture look-up with partial derivatvies */
1221 ir_txf /**< Texel fetch with explicit LOD */
1226 * IR instruction to sample a texture
1228 * The specific form of the IR instruction depends on the \c mode value
1229 * selected from \c ir_texture_opcodes. In the printed IR, these will
1230 * appear as:
1232 * Texel offset (0 or an expression)
1233 * | Projection divisor
1234 * | | Shadow comparitor
1235 * | | |
1236 * v v v
1237 * (tex <type> <sampler> <coordinate> 0 1 ( ))
1238 * (txb <type> <sampler> <coordinate> 0 1 ( ) <bias>)
1239 * (txl <type> <sampler> <coordinate> 0 1 ( ) <lod>)
1240 * (txd <type> <sampler> <coordinate> 0 1 ( ) (dPdx dPdy))
1241 * (txf <type> <sampler> <coordinate> 0 <lod>)
1243 class ir_texture : public ir_rvalue {
1244 public:
1245 ir_texture(enum ir_texture_opcode op)
1246 : op(op), projector(NULL), shadow_comparitor(NULL), offset(NULL)
1248 this->ir_type = ir_type_texture;
1251 virtual ir_texture *clone(void *mem_ctx, struct hash_table *) const;
1253 virtual ir_constant *constant_expression_value();
1255 virtual void accept(ir_visitor *v)
1257 v->visit(this);
1260 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1263 * Return a string representing the ir_texture_opcode.
1265 const char *opcode_string();
1267 /** Set the sampler and type. */
1268 void set_sampler(ir_dereference *sampler, const glsl_type *type);
1271 * Do a reverse-lookup to translate a string into an ir_texture_opcode.
1273 static ir_texture_opcode get_opcode(const char *);
1275 enum ir_texture_opcode op;
1277 /** Sampler to use for the texture access. */
1278 ir_dereference *sampler;
1280 /** Texture coordinate to sample */
1281 ir_rvalue *coordinate;
1284 * Value used for projective divide.
1286 * If there is no projective divide (the common case), this will be
1287 * \c NULL. Optimization passes should check for this to point to a constant
1288 * of 1.0 and replace that with \c NULL.
1290 ir_rvalue *projector;
1293 * Coordinate used for comparison on shadow look-ups.
1295 * If there is no shadow comparison, this will be \c NULL. For the
1296 * \c ir_txf opcode, this *must* be \c NULL.
1298 ir_rvalue *shadow_comparitor;
1300 /** Texel offset. */
1301 ir_rvalue *offset;
1303 union {
1304 ir_rvalue *lod; /**< Floating point LOD */
1305 ir_rvalue *bias; /**< Floating point LOD bias */
1306 struct {
1307 ir_rvalue *dPdx; /**< Partial derivative of coordinate wrt X */
1308 ir_rvalue *dPdy; /**< Partial derivative of coordinate wrt Y */
1309 } grad;
1310 } lod_info;
1314 struct ir_swizzle_mask {
1315 unsigned x:2;
1316 unsigned y:2;
1317 unsigned z:2;
1318 unsigned w:2;
1321 * Number of components in the swizzle.
1323 unsigned num_components:3;
1326 * Does the swizzle contain duplicate components?
1328 * L-value swizzles cannot contain duplicate components.
1330 unsigned has_duplicates:1;
1334 class ir_swizzle : public ir_rvalue {
1335 public:
1336 ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
1337 unsigned count);
1339 ir_swizzle(ir_rvalue *val, const unsigned *components, unsigned count);
1341 ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask);
1343 virtual ir_swizzle *clone(void *mem_ctx, struct hash_table *) const;
1345 virtual ir_constant *constant_expression_value();
1347 virtual ir_swizzle *as_swizzle()
1349 return this;
1353 * Construct an ir_swizzle from the textual representation. Can fail.
1355 static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
1357 virtual void accept(ir_visitor *v)
1359 v->visit(this);
1362 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1364 bool is_lvalue()
1366 return val->is_lvalue() && !mask.has_duplicates;
1370 * Get the variable that is ultimately referenced by an r-value
1372 virtual ir_variable *variable_referenced();
1374 ir_rvalue *val;
1375 ir_swizzle_mask mask;
1377 private:
1379 * Initialize the mask component of a swizzle
1381 * This is used by the \c ir_swizzle constructors.
1383 void init_mask(const unsigned *components, unsigned count);
1387 class ir_dereference : public ir_rvalue {
1388 public:
1389 virtual ir_dereference *clone(void *mem_ctx, struct hash_table *) const = 0;
1391 virtual ir_dereference *as_dereference()
1393 return this;
1396 bool is_lvalue();
1399 * Get the variable that is ultimately referenced by an r-value
1401 virtual ir_variable *variable_referenced() = 0;
1405 class ir_dereference_variable : public ir_dereference {
1406 public:
1407 ir_dereference_variable(ir_variable *var);
1409 virtual ir_dereference_variable *clone(void *mem_ctx,
1410 struct hash_table *) const;
1412 virtual ir_constant *constant_expression_value();
1414 virtual ir_dereference_variable *as_dereference_variable()
1416 return this;
1420 * Get the variable that is ultimately referenced by an r-value
1422 virtual ir_variable *variable_referenced()
1424 return this->var;
1427 virtual ir_variable *whole_variable_referenced()
1429 /* ir_dereference_variable objects always dereference the entire
1430 * variable. However, if this dereference is dereferenced by anything
1431 * else, the complete deferefernce chain is not a whole-variable
1432 * dereference. This method should only be called on the top most
1433 * ir_rvalue in a dereference chain.
1435 return this->var;
1438 virtual void accept(ir_visitor *v)
1440 v->visit(this);
1443 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1446 * Object being dereferenced.
1448 ir_variable *var;
1452 class ir_dereference_array : public ir_dereference {
1453 public:
1454 ir_dereference_array(ir_rvalue *value, ir_rvalue *array_index);
1456 ir_dereference_array(ir_variable *var, ir_rvalue *array_index);
1458 virtual ir_dereference_array *clone(void *mem_ctx,
1459 struct hash_table *) const;
1461 virtual ir_constant *constant_expression_value();
1463 virtual ir_dereference_array *as_dereference_array()
1465 return this;
1469 * Get the variable that is ultimately referenced by an r-value
1471 virtual ir_variable *variable_referenced()
1473 return this->array->variable_referenced();
1476 virtual void accept(ir_visitor *v)
1478 v->visit(this);
1481 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1483 ir_rvalue *array;
1484 ir_rvalue *array_index;
1486 private:
1487 void set_array(ir_rvalue *value);
1491 class ir_dereference_record : public ir_dereference {
1492 public:
1493 ir_dereference_record(ir_rvalue *value, const char *field);
1495 ir_dereference_record(ir_variable *var, const char *field);
1497 virtual ir_dereference_record *clone(void *mem_ctx,
1498 struct hash_table *) const;
1500 virtual ir_constant *constant_expression_value();
1503 * Get the variable that is ultimately referenced by an r-value
1505 virtual ir_variable *variable_referenced()
1507 return this->record->variable_referenced();
1510 virtual void accept(ir_visitor *v)
1512 v->visit(this);
1515 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1517 ir_rvalue *record;
1518 const char *field;
1523 * Data stored in an ir_constant
1525 union ir_constant_data {
1526 unsigned u[16];
1527 int i[16];
1528 float f[16];
1529 bool b[16];
1533 class ir_constant : public ir_rvalue {
1534 public:
1535 ir_constant(const struct glsl_type *type, const ir_constant_data *data);
1536 ir_constant(bool b);
1537 ir_constant(unsigned int u);
1538 ir_constant(int i);
1539 ir_constant(float f);
1542 * Construct an ir_constant from a list of ir_constant values
1544 ir_constant(const struct glsl_type *type, exec_list *values);
1547 * Construct an ir_constant from a scalar component of another ir_constant
1549 * The new \c ir_constant inherits the type of the component from the
1550 * source constant.
1552 * \note
1553 * In the case of a matrix constant, the new constant is a scalar, \b not
1554 * a vector.
1556 ir_constant(const ir_constant *c, unsigned i);
1559 * Return a new ir_constant of the specified type containing all zeros.
1561 static ir_constant *zero(void *mem_ctx, const glsl_type *type);
1563 virtual ir_constant *clone(void *mem_ctx, struct hash_table *) const;
1565 virtual ir_constant *constant_expression_value();
1567 virtual ir_constant *as_constant()
1569 return this;
1572 virtual void accept(ir_visitor *v)
1574 v->visit(this);
1577 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1580 * Get a particular component of a constant as a specific type
1582 * This is useful, for example, to get a value from an integer constant
1583 * as a float or bool. This appears frequently when constructors are
1584 * called with all constant parameters.
1586 /*@{*/
1587 bool get_bool_component(unsigned i) const;
1588 float get_float_component(unsigned i) const;
1589 int get_int_component(unsigned i) const;
1590 unsigned get_uint_component(unsigned i) const;
1591 /*@}*/
1593 ir_constant *get_array_element(unsigned i) const;
1595 ir_constant *get_record_field(const char *name);
1598 * Determine whether a constant has the same value as another constant
1600 * \sa ir_constant::is_zero, ir_constant::is_one,
1601 * ir_constant::is_negative_one
1603 bool has_value(const ir_constant *) const;
1605 virtual bool is_zero() const;
1606 virtual bool is_one() const;
1607 virtual bool is_negative_one() const;
1610 * Value of the constant.
1612 * The field used to back the values supplied by the constant is determined
1613 * by the type associated with the \c ir_instruction. Constants may be
1614 * scalars, vectors, or matrices.
1616 union ir_constant_data value;
1618 /* Array elements */
1619 ir_constant **array_elements;
1621 /* Structure fields */
1622 exec_list components;
1624 private:
1626 * Parameterless constructor only used by the clone method
1628 ir_constant(void);
1631 /*@}*/
1634 * Apply a visitor to each IR node in a list
1636 void
1637 visit_exec_list(exec_list *list, ir_visitor *visitor);
1640 * Validate invariants on each IR node in a list
1642 void validate_ir_tree(exec_list *instructions);
1644 struct _mesa_glsl_parse_state;
1645 struct gl_shader_program;
1648 * Detect whether an unlinked shader contains static recursion
1650 * If the list of instructions is determined to contain static recursion,
1651 * \c _mesa_glsl_error will be called to emit error messages for each function
1652 * that is in the recursion cycle.
1654 void
1655 detect_recursion_unlinked(struct _mesa_glsl_parse_state *state,
1656 exec_list *instructions);
1659 * Detect whether a linked shader contains static recursion
1661 * If the list of instructions is determined to contain static recursion,
1662 * \c link_error_printf will be called to emit error messages for each function
1663 * that is in the recursion cycle. In addition,
1664 * \c gl_shader_program::LinkStatus will be set to false.
1666 void
1667 detect_recursion_linked(struct gl_shader_program *prog,
1668 exec_list *instructions);
1671 * Make a clone of each IR instruction in a list
1673 * \param in List of IR instructions that are to be cloned
1674 * \param out List to hold the cloned instructions
1676 void
1677 clone_ir_list(void *mem_ctx, exec_list *out, const exec_list *in);
1679 extern void
1680 _mesa_glsl_initialize_variables(exec_list *instructions,
1681 struct _mesa_glsl_parse_state *state);
1683 extern void
1684 _mesa_glsl_initialize_functions(_mesa_glsl_parse_state *state);
1686 extern void
1687 _mesa_glsl_release_functions(void);
1689 extern void
1690 reparent_ir(exec_list *list, void *mem_ctx);
1692 struct glsl_symbol_table;
1694 extern void
1695 import_prototypes(const exec_list *source, exec_list *dest,
1696 struct glsl_symbol_table *symbols, void *mem_ctx);
1698 extern bool
1699 ir_has_call(ir_instruction *ir);
1701 extern void
1702 do_set_program_inouts(exec_list *instructions, struct gl_program *prog);
1704 extern char *
1705 prototype_string(const glsl_type *return_type, const char *name,
1706 exec_list *parameters);
1708 #endif /* IR_H */