Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / glsl / ir.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright © 2010 Intel Corporation
4  *
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:
11  *
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.
15  *
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.
23  */
24
25 #pragma once
26 #ifndef IR_H
27 #define IR_H
28
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include "ralloc.h"
33 #include "glsl_types.h"
34 #include "list.h"
35 #include "ir_visitor.h"
36 #include "ir_hierarchical_visitor.h"
37
38 /**
39  * \defgroup IR Intermediate representation nodes
40  *
41  * @{
42  */
43
44 /**
45  * Class tags
46  *
47  * Each concrete class derived from \c ir_instruction has a value in this
48  * enumerant.  The value for the type is stored in \c ir_instruction::ir_type
49  * by the constructor.  While using type tags is not very C++, it is extremely
50  * convenient.  For example, during debugging you can simply inspect
51  * \c ir_instruction::ir_type to find out the actual type of the object.
52  *
53  * In addition, it is possible to use a switch-statement based on \c
54  * \c ir_instruction::ir_type to select different behavior for different object
55  * types.  For functions that have only slight differences for several object
56  * types, this allows writing very straightforward, readable code.
57  */
58 enum ir_node_type {
59    /**
60     * Zero is unused so that the IR validator can detect cases where
61     * \c ir_instruction::ir_type has not been initialized.
62     */
63    ir_type_unset,
64    ir_type_variable,
65    ir_type_assignment,
66    ir_type_call,
67    ir_type_constant,
68    ir_type_dereference_array,
69    ir_type_dereference_record,
70    ir_type_dereference_variable,
71    ir_type_discard,
72    ir_type_expression,
73    ir_type_function,
74    ir_type_function_signature,
75    ir_type_if,
76    ir_type_loop,
77    ir_type_loop_jump,
78    ir_type_return,
79    ir_type_swizzle,
80    ir_type_texture,
81    ir_type_max /**< maximum ir_type enum number, for validation */
82 };
83
84 /**
85  * Base class of all IR instructions
86  */
87 class ir_instruction : public exec_node {
88 public:
89    enum ir_node_type ir_type;
90    const struct glsl_type *type;
91
92    /** ir_print_visitor helper for debugging. */
93    void print(void) const;
94
95    virtual void accept(ir_visitor *) = 0;
96    virtual ir_visitor_status accept(ir_hierarchical_visitor *) = 0;
97    virtual ir_instruction *clone(void *mem_ctx,
98                                  struct hash_table *ht) const = 0;
99
100    /**
101     * \name IR instruction downcast functions
102     *
103     * These functions either cast the object to a derived class or return
104     * \c NULL if the object's type does not match the specified derived class.
105     * Additional downcast functions will be added as needed.
106     */
107    /*@{*/
108    virtual class ir_variable *          as_variable()         { return NULL; }
109    virtual class ir_function *          as_function()         { return NULL; }
110    virtual class ir_dereference *       as_dereference()      { return NULL; }
111    virtual class ir_dereference_array * as_dereference_array() { return NULL; }
112    virtual class ir_dereference_variable *as_dereference_variable() { return NULL; }
113    virtual class ir_expression *        as_expression()       { return NULL; }
114    virtual class ir_rvalue *            as_rvalue()           { return NULL; }
115    virtual class ir_loop *              as_loop()             { return NULL; }
116    virtual class ir_assignment *        as_assignment()       { return NULL; }
117    virtual class ir_call *              as_call()             { return NULL; }
118    virtual class ir_return *            as_return()           { return NULL; }
119    virtual class ir_if *                as_if()               { return NULL; }
120    virtual class ir_swizzle *           as_swizzle()          { return NULL; }
121    virtual class ir_constant *          as_constant()         { return NULL; }
122    virtual class ir_discard *           as_discard()          { return NULL; }
123    /*@}*/
124
125 protected:
126    ir_instruction()
127    {
128       ir_type = ir_type_unset;
129       type = NULL;
130    }
131 };
132
133
134 class ir_rvalue : public ir_instruction {
135 public:
136    virtual ir_rvalue *clone(void *mem_ctx, struct hash_table *) const = 0;
137
138    virtual ir_constant *constant_expression_value() = 0;
139
140    virtual ir_rvalue * as_rvalue()
141    {
142       return this;
143    }
144
145    ir_rvalue *as_rvalue_to_saturate();
146
147    virtual bool is_lvalue()
148    {
149       return false;
150    }
151
152    /**
153     * Get the variable that is ultimately referenced by an r-value
154     */
155    virtual ir_variable *variable_referenced()
156    {
157       return NULL;
158    }
159
160
161    /**
162     * If an r-value is a reference to a whole variable, get that variable
163     *
164     * \return
165     * Pointer to a variable that is completely dereferenced by the r-value.  If
166     * the r-value is not a dereference or the dereference does not access the
167     * entire variable (i.e., it's just one array element, struct field), \c NULL
168     * is returned.
169     */
170    virtual ir_variable *whole_variable_referenced()
171    {
172       return NULL;
173    }
174
175    /**
176     * Determine if an r-value has the value zero
177     *
178     * The base implementation of this function always returns \c false.  The
179     * \c ir_constant class over-rides this function to return \c true \b only
180     * for vector and scalar types that have all elements set to the value
181     * zero (or \c false for booleans).
182     *
183     * \sa ir_constant::has_value, ir_rvalue::is_one, ir_rvalue::is_negative_one
184     */
185    virtual bool is_zero() const;
186
187    /**
188     * Determine if an r-value has the value one
189     *
190     * The base implementation of this function always returns \c false.  The
191     * \c ir_constant class over-rides this function to return \c true \b only
192     * for vector and scalar types that have all elements set to the value
193     * one (or \c true for booleans).
194     *
195     * \sa ir_constant::has_value, ir_rvalue::is_zero, ir_rvalue::is_negative_one
196     */
197    virtual bool is_one() const;
198
199    /**
200     * Determine if an r-value has the value negative one
201     *
202     * The base implementation of this function always returns \c false.  The
203     * \c ir_constant class over-rides this function to return \c true \b only
204     * for vector and scalar types that have all elements set to the value
205     * negative one.  For boolean times, the result is always \c false.
206     *
207     * \sa ir_constant::has_value, ir_rvalue::is_zero, ir_rvalue::is_one
208     */
209    virtual bool is_negative_one() const;
210
211 protected:
212    ir_rvalue();
213 };
214
215
216 /**
217  * Variable storage classes
218  */
219 enum ir_variable_mode {
220    ir_var_auto = 0,     /**< Function local variables and globals. */
221    ir_var_uniform,      /**< Variable declared as a uniform. */
222    ir_var_in,
223    ir_var_out,
224    ir_var_inout,
225    ir_var_const_in,     /**< "in" param that must be a constant expression */
226    ir_var_system_value, /**< Ex: front-face, instance-id, etc. */
227    ir_var_temporary     /**< Temporary variable generated during compilation. */
228 };
229
230 enum ir_variable_interpolation {
231    ir_var_smooth = 0,
232    ir_var_flat,
233    ir_var_noperspective
234 };
235
236 /**
237  * \brief Layout qualifiers for gl_FragDepth.
238  *
239  * The AMD_conservative_depth extension allows gl_FragDepth to be redeclared
240  * with a layout qualifier.
241  */
242 enum ir_depth_layout {
243     ir_depth_layout_none, /**< No depth layout is specified. */
244     ir_depth_layout_any,
245     ir_depth_layout_greater,
246     ir_depth_layout_less,
247     ir_depth_layout_unchanged
248 };
249
250 /**
251  * \brief Convert depth layout qualifier to string.
252  */
253 const char*
254 depth_layout_string(ir_depth_layout layout);
255
256 /**
257  * Description of built-in state associated with a uniform
258  *
259  * \sa ir_variable::state_slots
260  */
261 struct ir_state_slot {
262    int tokens[5];
263    int swizzle;
264 };
265
266 class ir_variable : public ir_instruction {
267 public:
268    ir_variable(const struct glsl_type *, const char *, ir_variable_mode);
269
270    virtual ir_variable *clone(void *mem_ctx, struct hash_table *ht) const;
271
272    virtual ir_variable *as_variable()
273    {
274       return this;
275    }
276
277    virtual void accept(ir_visitor *v)
278    {
279       v->visit(this);
280    }
281
282    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
283
284
285    /**
286     * Get the string value for the interpolation qualifier
287     *
288     * \return The string that would be used in a shader to specify \c
289     * mode will be returned.
290     *
291     * This function should only be used on a shader input or output variable.
292     */
293    const char *interpolation_string() const;
294
295    /**
296     * Calculate the number of slots required to hold this variable
297     *
298     * This is used to determine how many uniform or varying locations a variable
299     * occupies.  The count is in units of floating point components.
300     */
301    unsigned component_slots() const;
302
303    /**
304     * Delcared name of the variable
305     */
306    const char *name;
307
308    /**
309     * Highest element accessed with a constant expression array index
310     *
311     * Not used for non-array variables.
312     */
313    unsigned max_array_access;
314
315    /**
316     * Is the variable read-only?
317     *
318     * This is set for variables declared as \c const, shader inputs,
319     * and uniforms.
320     */
321    unsigned read_only:1;
322    unsigned centroid:1;
323    unsigned invariant:1;
324
325    /**
326     * Has this variable been used for reading or writing?
327     *
328     * Several GLSL semantic checks require knowledge of whether or not a
329     * variable has been used.  For example, it is an error to redeclare a
330     * variable as invariant after it has been used.
331     */
332    unsigned used:1;
333
334    /**
335     * Storage class of the variable.
336     *
337     * \sa ir_variable_mode
338     */
339    unsigned mode:3;
340
341    /**
342     * Interpolation mode for shader inputs / outputs
343     *
344     * \sa ir_variable_interpolation
345     */
346    unsigned interpolation:2;
347
348    /**
349     * \name ARB_fragment_coord_conventions
350     * @{
351     */
352    unsigned origin_upper_left:1;
353    unsigned pixel_center_integer:1;
354    /*@}*/
355
356    /**
357     * \brief Layout qualifier for gl_FragDepth.
358     *
359     * This is not equal to \c ir_depth_layout_none if and only if this
360     * variable is \c gl_FragDepth and a layout qualifier is specified.
361     */
362    ir_depth_layout depth_layout;
363
364    /**
365     * Was the location explicitly set in the shader?
366     *
367     * If the location is explicitly set in the shader, it \b cannot be changed
368     * by the linker or by the API (e.g., calls to \c glBindAttribLocation have
369     * no effect).
370     */
371    unsigned explicit_location:1;
372
373    /**
374     * Storage location of the base of this variable
375     *
376     * The precise meaning of this field depends on the nature of the variable.
377     *
378     *   - Vertex shader input: one of the values from \c gl_vert_attrib.
379     *   - Vertex shader output: one of the values from \c gl_vert_result.
380     *   - Fragment shader input: one of the values from \c gl_frag_attrib.
381     *   - Fragment shader output: one of the values from \c gl_frag_result.
382     *   - Uniforms: Per-stage uniform slot number.
383     *   - Other: This field is not currently used.
384     *
385     * If the variable is a uniform, shader input, or shader output, and the
386     * slot has not been assigned, the value will be -1.
387     */
388    int location;
389
390    /**
391     * Built-in state that backs this uniform
392     *
393     * Once set at variable creation, \c state_slots must remain invariant.
394     * This is because, ideally, this array would be shared by all clones of
395     * this variable in the IR tree.  In other words, we'd really like for it
396     * to be a fly-weight.
397     *
398     * If the variable is not a uniform, \c num_state_slots will be zero and
399     * \c state_slots will be \c NULL.
400     */
401    /*@{*/
402    unsigned num_state_slots;    /**< Number of state slots used */
403    ir_state_slot *state_slots;  /**< State descriptors. */
404    /*@}*/
405
406    /**
407     * Emit a warning if this variable is accessed.
408     */
409    const char *warn_extension;
410
411    /**
412     * Value assigned in the initializer of a variable declared "const"
413     */
414    ir_constant *constant_value;
415 };
416
417
418 /*@{*/
419 /**
420  * The representation of a function instance; may be the full definition or
421  * simply a prototype.
422  */
423 class ir_function_signature : public ir_instruction {
424    /* An ir_function_signature will be part of the list of signatures in
425     * an ir_function.
426     */
427 public:
428    ir_function_signature(const glsl_type *return_type);
429
430    virtual ir_function_signature *clone(void *mem_ctx,
431                                         struct hash_table *ht) const;
432    ir_function_signature *clone_prototype(void *mem_ctx,
433                                           struct hash_table *ht) const;
434
435    virtual void accept(ir_visitor *v)
436    {
437       v->visit(this);
438    }
439
440    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
441
442    /**
443     * Get the name of the function for which this is a signature
444     */
445    const char *function_name() const;
446
447    /**
448     * Get a handle to the function for which this is a signature
449     *
450     * There is no setter function, this function returns a \c const pointer,
451     * and \c ir_function_signature::_function is private for a reason.  The
452     * only way to make a connection between a function and function signature
453     * is via \c ir_function::add_signature.  This helps ensure that certain
454     * invariants (i.e., a function signature is in the list of signatures for
455     * its \c _function) are met.
456     *
457     * \sa ir_function::add_signature
458     */
459    inline const class ir_function *function() const
460    {
461       return this->_function;
462    }
463
464    /**
465     * Check whether the qualifiers match between this signature's parameters
466     * and the supplied parameter list.  If not, returns the name of the first
467     * parameter with mismatched qualifiers (for use in error messages).
468     */
469    const char *qualifiers_match(exec_list *params);
470
471    /**
472     * Replace the current parameter list with the given one.  This is useful
473     * if the current information came from a prototype, and either has invalid
474     * or missing parameter names.
475     */
476    void replace_parameters(exec_list *new_params);
477
478    /**
479     * Function return type.
480     *
481     * \note This discards the optional precision qualifier.
482     */
483    const struct glsl_type *return_type;
484
485    /**
486     * List of ir_variable of function parameters.
487     *
488     * This represents the storage.  The paramaters passed in a particular
489     * call will be in ir_call::actual_paramaters.
490     */
491    struct exec_list parameters;
492
493    /** Whether or not this function has a body (which may be empty). */
494    unsigned is_defined:1;
495
496    /** Whether or not this function signature is a built-in. */
497    unsigned is_builtin:1;
498
499    /** Body of instructions in the function. */
500    struct exec_list body;
501
502 private:
503    /** Function of which this signature is one overload. */
504    class ir_function *_function;
505
506    friend class ir_function;
507 };
508
509
510 /**
511  * Header for tracking multiple overloaded functions with the same name.
512  * Contains a list of ir_function_signatures representing each of the
513  * actual functions.
514  */
515 class ir_function : public ir_instruction {
516 public:
517    ir_function(const char *name);
518
519    virtual ir_function *clone(void *mem_ctx, struct hash_table *ht) const;
520
521    virtual ir_function *as_function()
522    {
523       return this;
524    }
525
526    virtual void accept(ir_visitor *v)
527    {
528       v->visit(this);
529    }
530
531    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
532
533    void add_signature(ir_function_signature *sig)
534    {
535       sig->_function = this;
536       this->signatures.push_tail(sig);
537    }
538
539    /**
540     * Get an iterator for the set of function signatures
541     */
542    exec_list_iterator iterator()
543    {
544       return signatures.iterator();
545    }
546
547    /**
548     * Find a signature that matches a set of actual parameters, taking implicit
549     * conversions into account.
550     */
551    ir_function_signature *matching_signature(const exec_list *actual_param);
552
553    /**
554     * Find a signature that exactly matches a set of actual parameters without
555     * any implicit type conversions.
556     */
557    ir_function_signature *exact_matching_signature(const exec_list *actual_ps);
558
559    /**
560     * Name of the function.
561     */
562    const char *name;
563
564    /** Whether or not this function has a signature that isn't a built-in. */
565    bool has_user_signature();
566
567    /**
568     * List of ir_function_signature for each overloaded function with this name.
569     */
570    struct exec_list signatures;
571 };
572
573 inline const char *ir_function_signature::function_name() const
574 {
575    return this->_function->name;
576 }
577 /*@}*/
578
579
580 /**
581  * IR instruction representing high-level if-statements
582  */
583 class ir_if : public ir_instruction {
584 public:
585    ir_if(ir_rvalue *condition)
586       : condition(condition)
587    {
588       ir_type = ir_type_if;
589    }
590
591    virtual ir_if *clone(void *mem_ctx, struct hash_table *ht) const;
592
593    virtual ir_if *as_if()
594    {
595       return this;
596    }
597
598    virtual void accept(ir_visitor *v)
599    {
600       v->visit(this);
601    }
602
603    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
604
605    ir_rvalue *condition;
606    /** List of ir_instruction for the body of the then branch */
607    exec_list  then_instructions;
608    /** List of ir_instruction for the body of the else branch */
609    exec_list  else_instructions;
610 };
611
612
613 /**
614  * IR instruction representing a high-level loop structure.
615  */
616 class ir_loop : public ir_instruction {
617 public:
618    ir_loop();
619
620    virtual ir_loop *clone(void *mem_ctx, struct hash_table *ht) const;
621
622    virtual void accept(ir_visitor *v)
623    {
624       v->visit(this);
625    }
626
627    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
628
629    virtual ir_loop *as_loop()
630    {
631       return this;
632    }
633
634    /**
635     * Get an iterator for the instructions of the loop body
636     */
637    exec_list_iterator iterator()
638    {
639       return body_instructions.iterator();
640    }
641
642    /** List of ir_instruction that make up the body of the loop. */
643    exec_list body_instructions;
644
645    /**
646     * \name Loop counter and controls
647     *
648     * Represents a loop like a FORTRAN \c do-loop.
649     *
650     * \note
651     * If \c from and \c to are the same value, the loop will execute once.
652     */
653    /*@{*/
654    ir_rvalue *from;             /** Value of the loop counter on the first
655                                  * iteration of the loop.
656                                  */
657    ir_rvalue *to;               /** Value of the loop counter on the last
658                                  * iteration of the loop.
659                                  */
660    ir_rvalue *increment;
661    ir_variable *counter;
662
663    /**
664     * Comparison operation in the loop terminator.
665     *
666     * If any of the loop control fields are non-\c NULL, this field must be
667     * one of \c ir_binop_less, \c ir_binop_greater, \c ir_binop_lequal,
668     * \c ir_binop_gequal, \c ir_binop_equal, or \c ir_binop_nequal.
669     */
670    int cmp;
671    /*@}*/
672 };
673
674
675 class ir_assignment : public ir_instruction {
676 public:
677    ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition = NULL);
678
679    /**
680     * Construct an assignment with an explicit write mask
681     *
682     * \note
683     * Since a write mask is supplied, the LHS must already be a bare
684     * \c ir_dereference.  The cannot be any swizzles in the LHS.
685     */
686    ir_assignment(ir_dereference *lhs, ir_rvalue *rhs, ir_rvalue *condition,
687                  unsigned write_mask);
688
689    virtual ir_assignment *clone(void *mem_ctx, struct hash_table *ht) const;
690
691    virtual ir_constant *constant_expression_value();
692
693    virtual void accept(ir_visitor *v)
694    {
695       v->visit(this);
696    }
697
698    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
699
700    virtual ir_assignment * as_assignment()
701    {
702       return this;
703    }
704
705    /**
706     * Get a whole variable written by an assignment
707     *
708     * If the LHS of the assignment writes a whole variable, the variable is
709     * returned.  Otherwise \c NULL is returned.  Examples of whole-variable
710     * assignment are:
711     *
712     *  - Assigning to a scalar
713     *  - Assigning to all components of a vector
714     *  - Whole array (or matrix) assignment
715     *  - Whole structure assignment
716     */
717    ir_variable *whole_variable_written();
718
719    /**
720     * Set the LHS of an assignment
721     */
722    void set_lhs(ir_rvalue *lhs);
723
724    /**
725     * Left-hand side of the assignment.
726     *
727     * This should be treated as read only.  If you need to set the LHS of an
728     * assignment, use \c ir_assignment::set_lhs.
729     */
730    ir_dereference *lhs;
731
732    /**
733     * Value being assigned
734     */
735    ir_rvalue *rhs;
736
737    /**
738     * Optional condition for the assignment.
739     */
740    ir_rvalue *condition;
741
742
743    /**
744     * Component mask written
745     *
746     * For non-vector types in the LHS, this field will be zero.  For vector
747     * types, a bit will be set for each component that is written.  Note that
748     * for \c vec2 and \c vec3 types only the lower bits will ever be set.
749     *
750     * A partially-set write mask means that each enabled channel gets
751     * the value from a consecutive channel of the rhs.  For example,
752     * to write just .xyw of gl_FrontColor with color:
753     *
754     * (assign (constant bool (1)) (xyw)
755     *     (var_ref gl_FragColor)
756     *     (swiz xyw (var_ref color)))
757     */
758    unsigned write_mask:4;
759 };
760
761 /* Update ir_expression::num_operands() and operator_strs when
762  * updating this list.
763  */
764 enum ir_expression_operation {
765    ir_unop_bit_not,
766    ir_unop_logic_not,
767    ir_unop_neg,
768    ir_unop_abs,
769    ir_unop_sign,
770    ir_unop_rcp,
771    ir_unop_rsq,
772    ir_unop_sqrt,
773    ir_unop_exp,      /**< Log base e on gentype */
774    ir_unop_log,      /**< Natural log on gentype */
775    ir_unop_exp2,
776    ir_unop_log2,
777    ir_unop_f2i,      /**< Float-to-integer conversion. */
778    ir_unop_i2f,      /**< Integer-to-float conversion. */
779    ir_unop_f2b,      /**< Float-to-boolean conversion */
780    ir_unop_b2f,      /**< Boolean-to-float conversion */
781    ir_unop_i2b,      /**< int-to-boolean conversion */
782    ir_unop_b2i,      /**< Boolean-to-int conversion */
783    ir_unop_u2f,      /**< Unsigned-to-float conversion. */
784    ir_unop_any,
785
786    /**
787     * \name Unary floating-point rounding operations.
788     */
789    /*@{*/
790    ir_unop_trunc,
791    ir_unop_ceil,
792    ir_unop_floor,
793    ir_unop_fract,
794    ir_unop_round_even,
795    /*@}*/
796
797    /**
798     * \name Trigonometric operations.
799     */
800    /*@{*/
801    ir_unop_sin,
802    ir_unop_cos,
803    ir_unop_sin_reduced,    /**< Reduced range sin. [-pi, pi] */
804    ir_unop_cos_reduced,    /**< Reduced range cos. [-pi, pi] */
805    /*@}*/
806
807    /**
808     * \name Partial derivatives.
809     */
810    /*@{*/
811    ir_unop_dFdx,
812    ir_unop_dFdy,
813    /*@}*/
814
815    ir_unop_noise,
816
817    /**
818     * A sentinel marking the last of the unary operations.
819     */
820    ir_last_unop = ir_unop_noise,
821
822    ir_binop_add,
823    ir_binop_sub,
824    ir_binop_mul,
825    ir_binop_div,
826
827    /**
828     * Takes one of two combinations of arguments:
829     *
830     * - mod(vecN, vecN)
831     * - mod(vecN, float)
832     *
833     * Does not take integer types.
834     */
835    ir_binop_mod,
836
837    /**
838     * \name Binary comparison operators which return a boolean vector.
839     * The type of both operands must be equal.
840     */
841    /*@{*/
842    ir_binop_less,
843    ir_binop_greater,
844    ir_binop_lequal,
845    ir_binop_gequal,
846    ir_binop_equal,
847    ir_binop_nequal,
848    /**
849     * Returns single boolean for whether all components of operands[0]
850     * equal the components of operands[1].
851     */
852    ir_binop_all_equal,
853    /**
854     * Returns single boolean for whether any component of operands[0]
855     * is not equal to the corresponding component of operands[1].
856     */
857    ir_binop_any_nequal,
858    /*@}*/
859
860    /**
861     * \name Bit-wise binary operations.
862     */
863    /*@{*/
864    ir_binop_lshift,
865    ir_binop_rshift,
866    ir_binop_bit_and,
867    ir_binop_bit_xor,
868    ir_binop_bit_or,
869    /*@}*/
870
871    ir_binop_logic_and,
872    ir_binop_logic_xor,
873    ir_binop_logic_or,
874
875    ir_binop_dot,
876    ir_binop_min,
877    ir_binop_max,
878
879    ir_binop_pow,
880
881    /**
882     * A sentinel marking the last of the binary operations.
883     */
884    ir_last_binop = ir_binop_pow,
885
886    ir_quadop_vector,
887
888    /**
889     * A sentinel marking the last of all operations.
890     */
891    ir_last_opcode = ir_last_binop
892 };
893
894 class ir_expression : public ir_rvalue {
895 public:
896    /**
897     * Constructor for unary operation expressions
898     */
899    ir_expression(int op, const struct glsl_type *type, ir_rvalue *);
900    ir_expression(int op, ir_rvalue *);
901
902    /**
903     * Constructor for binary operation expressions
904     */
905    ir_expression(int op, const struct glsl_type *type,
906                  ir_rvalue *, ir_rvalue *);
907    ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1);
908
909    /**
910     * Constructor for quad operator expressions
911     */
912    ir_expression(int op, const struct glsl_type *type,
913                  ir_rvalue *, ir_rvalue *, ir_rvalue *, ir_rvalue *);
914
915    virtual ir_expression *as_expression()
916    {
917       return this;
918    }
919
920    virtual ir_expression *clone(void *mem_ctx, struct hash_table *ht) const;
921
922    /**
923     * Attempt to constant-fold the expression
924     *
925     * If the expression cannot be constant folded, this method will return
926     * \c NULL.
927     */
928    virtual ir_constant *constant_expression_value();
929
930    /**
931     * Determine the number of operands used by an expression
932     */
933    static unsigned int get_num_operands(ir_expression_operation);
934
935    /**
936     * Determine the number of operands used by an expression
937     */
938    unsigned int get_num_operands() const
939    {
940       return (this->operation == ir_quadop_vector)
941          ? this->type->vector_elements : get_num_operands(operation);
942    }
943
944    /**
945     * Return a string representing this expression's operator.
946     */
947    const char *operator_string();
948
949    /**
950     * Return a string representing this expression's operator.
951     */
952    static const char *operator_string(ir_expression_operation);
953
954
955    /**
956     * Do a reverse-lookup to translate the given string into an operator.
957     */
958    static ir_expression_operation get_operator(const char *);
959
960    virtual void accept(ir_visitor *v)
961    {
962       v->visit(this);
963    }
964
965    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
966
967    ir_expression_operation operation;
968    ir_rvalue *operands[4];
969 };
970
971
972 /**
973  * IR instruction representing a function call
974  */
975 class ir_call : public ir_rvalue {
976 public:
977    ir_call(ir_function_signature *callee, exec_list *actual_parameters)
978       : callee(callee)
979    {
980       ir_type = ir_type_call;
981       assert(callee->return_type != NULL);
982       type = callee->return_type;
983       actual_parameters->move_nodes_to(& this->actual_parameters);
984       this->use_builtin = callee->is_builtin;
985    }
986
987    virtual ir_call *clone(void *mem_ctx, struct hash_table *ht) const;
988
989    virtual ir_constant *constant_expression_value();
990
991    virtual ir_call *as_call()
992    {
993       return this;
994    }
995
996    virtual void accept(ir_visitor *v)
997    {
998       v->visit(this);
999    }
1000
1001    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1002
1003    /**
1004     * Get a generic ir_call object when an error occurs
1005     *
1006     * Any allocation will be performed with 'ctx' as ralloc owner.
1007     */
1008    static ir_call *get_error_instruction(void *ctx);
1009
1010    /**
1011     * Get an iterator for the set of acutal parameters
1012     */
1013    exec_list_iterator iterator()
1014    {
1015       return actual_parameters.iterator();
1016    }
1017
1018    /**
1019     * Get the name of the function being called.
1020     */
1021    const char *callee_name() const
1022    {
1023       return callee->function_name();
1024    }
1025
1026    /**
1027     * Get the function signature bound to this function call
1028     */
1029    ir_function_signature *get_callee()
1030    {
1031       return callee;
1032    }
1033
1034    /**
1035     * Set the function call target
1036     */
1037    void set_callee(ir_function_signature *sig);
1038
1039    /**
1040     * Generates an inline version of the function before @ir,
1041     * returning the return value of the function.
1042     */
1043    ir_rvalue *generate_inline(ir_instruction *ir);
1044
1045    /* List of ir_rvalue of paramaters passed in this call. */
1046    exec_list actual_parameters;
1047
1048    /** Should this call only bind to a built-in function? */
1049    bool use_builtin;
1050
1051 private:
1052    ir_call()
1053       : callee(NULL)
1054    {
1055       this->ir_type = ir_type_call;
1056    }
1057
1058    ir_function_signature *callee;
1059 };
1060
1061
1062 /**
1063  * \name Jump-like IR instructions.
1064  *
1065  * These include \c break, \c continue, \c return, and \c discard.
1066  */
1067 /*@{*/
1068 class ir_jump : public ir_instruction {
1069 protected:
1070    ir_jump()
1071    {
1072       ir_type = ir_type_unset;
1073    }
1074 };
1075
1076 class ir_return : public ir_jump {
1077 public:
1078    ir_return()
1079       : value(NULL)
1080    {
1081       this->ir_type = ir_type_return;
1082    }
1083
1084    ir_return(ir_rvalue *value)
1085       : value(value)
1086    {
1087       this->ir_type = ir_type_return;
1088    }
1089
1090    virtual ir_return *clone(void *mem_ctx, struct hash_table *) const;
1091
1092    virtual ir_return *as_return()
1093    {
1094       return this;
1095    }
1096
1097    ir_rvalue *get_value() const
1098    {
1099       return value;
1100    }
1101
1102    virtual void accept(ir_visitor *v)
1103    {
1104       v->visit(this);
1105    }
1106
1107    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1108
1109    ir_rvalue *value;
1110 };
1111
1112
1113 /**
1114  * Jump instructions used inside loops
1115  *
1116  * These include \c break and \c continue.  The \c break within a loop is
1117  * different from the \c break within a switch-statement.
1118  *
1119  * \sa ir_switch_jump
1120  */
1121 class ir_loop_jump : public ir_jump {
1122 public:
1123    enum jump_mode {
1124       jump_break,
1125       jump_continue
1126    };
1127
1128    ir_loop_jump(jump_mode mode)
1129    {
1130       this->ir_type = ir_type_loop_jump;
1131       this->mode = mode;
1132       this->loop = loop;
1133    }
1134
1135    virtual ir_loop_jump *clone(void *mem_ctx, struct hash_table *) const;
1136
1137    virtual void accept(ir_visitor *v)
1138    {
1139       v->visit(this);
1140    }
1141
1142    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1143
1144    bool is_break() const
1145    {
1146       return mode == jump_break;
1147    }
1148
1149    bool is_continue() const
1150    {
1151       return mode == jump_continue;
1152    }
1153
1154    /** Mode selector for the jump instruction. */
1155    enum jump_mode mode;
1156 private:
1157    /** Loop containing this break instruction. */
1158    ir_loop *loop;
1159 };
1160
1161 /**
1162  * IR instruction representing discard statements.
1163  */
1164 class ir_discard : public ir_jump {
1165 public:
1166    ir_discard()
1167    {
1168       this->ir_type = ir_type_discard;
1169       this->condition = NULL;
1170    }
1171
1172    ir_discard(ir_rvalue *cond)
1173    {
1174       this->ir_type = ir_type_discard;
1175       this->condition = cond;
1176    }
1177
1178    virtual ir_discard *clone(void *mem_ctx, struct hash_table *ht) const;
1179
1180    virtual void accept(ir_visitor *v)
1181    {
1182       v->visit(this);
1183    }
1184
1185    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1186
1187    virtual ir_discard *as_discard()
1188    {
1189       return this;
1190    }
1191
1192    ir_rvalue *condition;
1193 };
1194 /*@}*/
1195
1196
1197 /**
1198  * Texture sampling opcodes used in ir_texture
1199  */
1200 enum ir_texture_opcode {
1201    ir_tex,              /**< Regular texture look-up */
1202    ir_txb,              /**< Texture look-up with LOD bias */
1203    ir_txl,              /**< Texture look-up with explicit LOD */
1204    ir_txd,              /**< Texture look-up with partial derivatvies */
1205    ir_txf               /**< Texel fetch with explicit LOD */
1206 };
1207
1208
1209 /**
1210  * IR instruction to sample a texture
1211  *
1212  * The specific form of the IR instruction depends on the \c mode value
1213  * selected from \c ir_texture_opcodes.  In the printed IR, these will
1214  * appear as:
1215  *
1216  *                                    Texel offset (0 or an expression)
1217  *                                    | Projection divisor
1218  *                                    | |  Shadow comparitor
1219  *                                    | |  |
1220  *                                    v v  v
1221  * (tex <type> <sampler> <coordinate> 0 1 ( ))
1222  * (txb <type> <sampler> <coordinate> 0 1 ( ) <bias>)
1223  * (txl <type> <sampler> <coordinate> 0 1 ( ) <lod>)
1224  * (txd <type> <sampler> <coordinate> 0 1 ( ) (dPdx dPdy))
1225  * (txf <type> <sampler> <coordinate> 0       <lod>)
1226  */
1227 class ir_texture : public ir_rvalue {
1228 public:
1229    ir_texture(enum ir_texture_opcode op)
1230       : op(op), projector(NULL), shadow_comparitor(NULL), offset(NULL)
1231    {
1232       this->ir_type = ir_type_texture;
1233    }
1234
1235    virtual ir_texture *clone(void *mem_ctx, struct hash_table *) const;
1236
1237    virtual ir_constant *constant_expression_value();
1238
1239    virtual void accept(ir_visitor *v)
1240    {
1241       v->visit(this);
1242    }
1243
1244    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1245
1246    /**
1247     * Return a string representing the ir_texture_opcode.
1248     */
1249    const char *opcode_string();
1250
1251    /** Set the sampler and type. */
1252    void set_sampler(ir_dereference *sampler, const glsl_type *type);
1253
1254    /**
1255     * Do a reverse-lookup to translate a string into an ir_texture_opcode.
1256     */
1257    static ir_texture_opcode get_opcode(const char *);
1258
1259    enum ir_texture_opcode op;
1260
1261    /** Sampler to use for the texture access. */
1262    ir_dereference *sampler;
1263
1264    /** Texture coordinate to sample */
1265    ir_rvalue *coordinate;
1266
1267    /**
1268     * Value used for projective divide.
1269     *
1270     * If there is no projective divide (the common case), this will be
1271     * \c NULL.  Optimization passes should check for this to point to a constant
1272     * of 1.0 and replace that with \c NULL.
1273     */
1274    ir_rvalue *projector;
1275
1276    /**
1277     * Coordinate used for comparison on shadow look-ups.
1278     *
1279     * If there is no shadow comparison, this will be \c NULL.  For the
1280     * \c ir_txf opcode, this *must* be \c NULL.
1281     */
1282    ir_rvalue *shadow_comparitor;
1283
1284    /** Texel offset. */
1285    ir_rvalue *offset;
1286
1287    union {
1288       ir_rvalue *lod;           /**< Floating point LOD */
1289       ir_rvalue *bias;          /**< Floating point LOD bias */
1290       struct {
1291          ir_rvalue *dPdx;       /**< Partial derivative of coordinate wrt X */
1292          ir_rvalue *dPdy;       /**< Partial derivative of coordinate wrt Y */
1293       } grad;
1294    } lod_info;
1295 };
1296
1297
1298 struct ir_swizzle_mask {
1299    unsigned x:2;
1300    unsigned y:2;
1301    unsigned z:2;
1302    unsigned w:2;
1303
1304    /**
1305     * Number of components in the swizzle.
1306     */
1307    unsigned num_components:3;
1308
1309    /**
1310     * Does the swizzle contain duplicate components?
1311     *
1312     * L-value swizzles cannot contain duplicate components.
1313     */
1314    unsigned has_duplicates:1;
1315 };
1316
1317
1318 class ir_swizzle : public ir_rvalue {
1319 public:
1320    ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
1321               unsigned count);
1322
1323    ir_swizzle(ir_rvalue *val, const unsigned *components, unsigned count);
1324
1325    ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask);
1326
1327    virtual ir_swizzle *clone(void *mem_ctx, struct hash_table *) const;
1328
1329    virtual ir_constant *constant_expression_value();
1330
1331    virtual ir_swizzle *as_swizzle()
1332    {
1333       return this;
1334    }
1335
1336    /**
1337     * Construct an ir_swizzle from the textual representation.  Can fail.
1338     */
1339    static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
1340
1341    virtual void accept(ir_visitor *v)
1342    {
1343       v->visit(this);
1344    }
1345
1346    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1347
1348    bool is_lvalue()
1349    {
1350       return val->is_lvalue() && !mask.has_duplicates;
1351    }
1352
1353    /**
1354     * Get the variable that is ultimately referenced by an r-value
1355     */
1356    virtual ir_variable *variable_referenced();
1357
1358    ir_rvalue *val;
1359    ir_swizzle_mask mask;
1360
1361 private:
1362    /**
1363     * Initialize the mask component of a swizzle
1364     *
1365     * This is used by the \c ir_swizzle constructors.
1366     */
1367    void init_mask(const unsigned *components, unsigned count);
1368 };
1369
1370
1371 class ir_dereference : public ir_rvalue {
1372 public:
1373    virtual ir_dereference *clone(void *mem_ctx, struct hash_table *) const = 0;
1374
1375    virtual ir_dereference *as_dereference()
1376    {
1377       return this;
1378    }
1379
1380    bool is_lvalue();
1381
1382    /**
1383     * Get the variable that is ultimately referenced by an r-value
1384     */
1385    virtual ir_variable *variable_referenced() = 0;
1386 };
1387
1388
1389 class ir_dereference_variable : public ir_dereference {
1390 public:
1391    ir_dereference_variable(ir_variable *var);
1392
1393    virtual ir_dereference_variable *clone(void *mem_ctx,
1394                                           struct hash_table *) const;
1395
1396    virtual ir_constant *constant_expression_value();
1397
1398    virtual ir_dereference_variable *as_dereference_variable()
1399    {
1400       return this;
1401    }
1402
1403    /**
1404     * Get the variable that is ultimately referenced by an r-value
1405     */
1406    virtual ir_variable *variable_referenced()
1407    {
1408       return this->var;
1409    }
1410
1411    virtual ir_variable *whole_variable_referenced()
1412    {
1413       /* ir_dereference_variable objects always dereference the entire
1414        * variable.  However, if this dereference is dereferenced by anything
1415        * else, the complete deferefernce chain is not a whole-variable
1416        * dereference.  This method should only be called on the top most
1417        * ir_rvalue in a dereference chain.
1418        */
1419       return this->var;
1420    }
1421
1422    virtual void accept(ir_visitor *v)
1423    {
1424       v->visit(this);
1425    }
1426
1427    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1428
1429    /**
1430     * Object being dereferenced.
1431     */
1432    ir_variable *var;
1433 };
1434
1435
1436 class ir_dereference_array : public ir_dereference {
1437 public:
1438    ir_dereference_array(ir_rvalue *value, ir_rvalue *array_index);
1439
1440    ir_dereference_array(ir_variable *var, ir_rvalue *array_index);
1441
1442    virtual ir_dereference_array *clone(void *mem_ctx,
1443                                        struct hash_table *) const;
1444
1445    virtual ir_constant *constant_expression_value();
1446
1447    virtual ir_dereference_array *as_dereference_array()
1448    {
1449       return this;
1450    }
1451
1452    /**
1453     * Get the variable that is ultimately referenced by an r-value
1454     */
1455    virtual ir_variable *variable_referenced()
1456    {
1457       return this->array->variable_referenced();
1458    }
1459
1460    virtual void accept(ir_visitor *v)
1461    {
1462       v->visit(this);
1463    }
1464
1465    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1466
1467    ir_rvalue *array;
1468    ir_rvalue *array_index;
1469
1470 private:
1471    void set_array(ir_rvalue *value);
1472 };
1473
1474
1475 class ir_dereference_record : public ir_dereference {
1476 public:
1477    ir_dereference_record(ir_rvalue *value, const char *field);
1478
1479    ir_dereference_record(ir_variable *var, const char *field);
1480
1481    virtual ir_dereference_record *clone(void *mem_ctx,
1482                                         struct hash_table *) const;
1483
1484    virtual ir_constant *constant_expression_value();
1485
1486    /**
1487     * Get the variable that is ultimately referenced by an r-value
1488     */
1489    virtual ir_variable *variable_referenced()
1490    {
1491       return this->record->variable_referenced();
1492    }
1493
1494    virtual void accept(ir_visitor *v)
1495    {
1496       v->visit(this);
1497    }
1498
1499    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1500
1501    ir_rvalue *record;
1502    const char *field;
1503 };
1504
1505
1506 /**
1507  * Data stored in an ir_constant
1508  */
1509 union ir_constant_data {
1510       unsigned u[16];
1511       int i[16];
1512       float f[16];
1513       bool b[16];
1514 };
1515
1516
1517 class ir_constant : public ir_rvalue {
1518 public:
1519    ir_constant(const struct glsl_type *type, const ir_constant_data *data);
1520    ir_constant(bool b);
1521    ir_constant(unsigned int u);
1522    ir_constant(int i);
1523    ir_constant(float f);
1524
1525    /**
1526     * Construct an ir_constant from a list of ir_constant values
1527     */
1528    ir_constant(const struct glsl_type *type, exec_list *values);
1529
1530    /**
1531     * Construct an ir_constant from a scalar component of another ir_constant
1532     *
1533     * The new \c ir_constant inherits the type of the component from the
1534     * source constant.
1535     *
1536     * \note
1537     * In the case of a matrix constant, the new constant is a scalar, \b not
1538     * a vector.
1539     */
1540    ir_constant(const ir_constant *c, unsigned i);
1541
1542    /**
1543     * Return a new ir_constant of the specified type containing all zeros.
1544     */
1545    static ir_constant *zero(void *mem_ctx, const glsl_type *type);
1546
1547    virtual ir_constant *clone(void *mem_ctx, struct hash_table *) const;
1548
1549    virtual ir_constant *constant_expression_value();
1550
1551    virtual ir_constant *as_constant()
1552    {
1553       return this;
1554    }
1555
1556    virtual void accept(ir_visitor *v)
1557    {
1558       v->visit(this);
1559    }
1560
1561    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1562
1563    /**
1564     * Get a particular component of a constant as a specific type
1565     *
1566     * This is useful, for example, to get a value from an integer constant
1567     * as a float or bool.  This appears frequently when constructors are
1568     * called with all constant parameters.
1569     */
1570    /*@{*/
1571    bool get_bool_component(unsigned i) const;
1572    float get_float_component(unsigned i) const;
1573    int get_int_component(unsigned i) const;
1574    unsigned get_uint_component(unsigned i) const;
1575    /*@}*/
1576
1577    ir_constant *get_array_element(unsigned i) const;
1578
1579    ir_constant *get_record_field(const char *name);
1580
1581    /**
1582     * Determine whether a constant has the same value as another constant
1583     *
1584     * \sa ir_constant::is_zero, ir_constant::is_one,
1585     * ir_constant::is_negative_one
1586     */
1587    bool has_value(const ir_constant *) const;
1588
1589    virtual bool is_zero() const;
1590    virtual bool is_one() const;
1591    virtual bool is_negative_one() const;
1592
1593    /**
1594     * Value of the constant.
1595     *
1596     * The field used to back the values supplied by the constant is determined
1597     * by the type associated with the \c ir_instruction.  Constants may be
1598     * scalars, vectors, or matrices.
1599     */
1600    union ir_constant_data value;
1601
1602    /* Array elements */
1603    ir_constant **array_elements;
1604
1605    /* Structure fields */
1606    exec_list components;
1607
1608 private:
1609    /**
1610     * Parameterless constructor only used by the clone method
1611     */
1612    ir_constant(void);
1613 };
1614
1615 /*@}*/
1616
1617 /**
1618  * Apply a visitor to each IR node in a list
1619  */
1620 void
1621 visit_exec_list(exec_list *list, ir_visitor *visitor);
1622
1623 /**
1624  * Validate invariants on each IR node in a list
1625  */
1626 void validate_ir_tree(exec_list *instructions);
1627
1628 struct _mesa_glsl_parse_state;
1629 struct gl_shader_program;
1630
1631 /**
1632  * Detect whether an unlinked shader contains static recursion
1633  *
1634  * If the list of instructions is determined to contain static recursion,
1635  * \c _mesa_glsl_error will be called to emit error messages for each function
1636  * that is in the recursion cycle.
1637  */
1638 void
1639 detect_recursion_unlinked(struct _mesa_glsl_parse_state *state,
1640                           exec_list *instructions);
1641
1642 /**
1643  * Detect whether a linked shader contains static recursion
1644  *
1645  * If the list of instructions is determined to contain static recursion,
1646  * \c link_error_printf will be called to emit error messages for each function
1647  * that is in the recursion cycle.  In addition,
1648  * \c gl_shader_program::LinkStatus will be set to false.
1649  */
1650 void
1651 detect_recursion_linked(struct gl_shader_program *prog,
1652                         exec_list *instructions);
1653
1654 /**
1655  * Make a clone of each IR instruction in a list
1656  *
1657  * \param in   List of IR instructions that are to be cloned
1658  * \param out  List to hold the cloned instructions
1659  */
1660 void
1661 clone_ir_list(void *mem_ctx, exec_list *out, const exec_list *in);
1662
1663 extern void
1664 _mesa_glsl_initialize_variables(exec_list *instructions,
1665                                 struct _mesa_glsl_parse_state *state);
1666
1667 extern void
1668 _mesa_glsl_initialize_functions(_mesa_glsl_parse_state *state);
1669
1670 extern void
1671 _mesa_glsl_release_functions(void);
1672
1673 extern void
1674 reparent_ir(exec_list *list, void *mem_ctx);
1675
1676 struct glsl_symbol_table;
1677
1678 extern void
1679 import_prototypes(const exec_list *source, exec_list *dest,
1680                   struct glsl_symbol_table *symbols, void *mem_ctx);
1681
1682 extern bool
1683 ir_has_call(ir_instruction *ir);
1684
1685 extern void
1686 do_set_program_inouts(exec_list *instructions, struct gl_program *prog);
1687
1688 extern char *
1689 prototype_string(const glsl_type *return_type, const char *name,
1690                  exec_list *parameters);
1691
1692 #endif /* IR_H */