3d2c7ff5cf56be6ab0a52155ed840e65420c8551
[platform/upstream/mesa.git] / 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 <cstdio>
30 #include <cstdlib>
31
32 extern "C" {
33 #include <talloc.h>
34 }
35
36 #include "list.h"
37 #include "ir_visitor.h"
38 #include "ir_hierarchical_visitor.h"
39
40 #ifndef ARRAY_SIZE
41 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
42 #endif
43
44 struct ir_program {
45    void *bong_hits;
46 };
47
48 /**
49  * Base class of all IR instructions
50  */
51 class ir_instruction : public exec_node {
52 public:
53    const struct glsl_type *type;
54
55    class ir_constant *constant_expression_value();
56
57    /** ir_print_visitor helper for debugging. */
58    void print(void) const;
59
60    virtual void accept(ir_visitor *) = 0;
61    virtual ir_visitor_status accept(ir_hierarchical_visitor *) = 0;
62    virtual ir_instruction *clone(struct hash_table *ht) const = 0;
63
64    /**
65     * \name IR instruction downcast functions
66     *
67     * These functions either cast the object to a derived class or return
68     * \c NULL if the object's type does not match the specified derived class.
69     * Additional downcast functions will be added as needed.
70     */
71    /*@{*/
72    virtual class ir_variable *          as_variable()         { return NULL; }
73    virtual class ir_function *          as_function()         { return NULL; }
74    virtual class ir_dereference *       as_dereference()      { return NULL; }
75    virtual class ir_dereference_array * as_dereference_array() { return NULL; }
76    virtual class ir_rvalue *            as_rvalue()           { return NULL; }
77    virtual class ir_loop *              as_loop()             { return NULL; }
78    virtual class ir_assignment *        as_assignment()       { return NULL; }
79    virtual class ir_call *              as_call()             { return NULL; }
80    virtual class ir_return *            as_return()           { return NULL; }
81    virtual class ir_if *                as_if()               { return NULL; }
82    virtual class ir_swizzle *           as_swizzle()          { return NULL; }
83    virtual class ir_constant *          as_constant()         { return NULL; }
84    /*@}*/
85
86 protected:
87    ir_instruction()
88    {
89       /* empty */
90    }
91 };
92
93
94 class ir_rvalue : public ir_instruction {
95 public:
96    virtual ir_rvalue * as_rvalue()
97    {
98       return this;
99    }
100
101    virtual bool is_lvalue()
102    {
103       return false;
104    }
105
106    /**
107     * Get the variable that is ultimately referenced by an r-value
108     */
109    virtual ir_variable *variable_referenced()
110    {
111       return NULL;
112    }
113
114
115    /**
116     * If an r-value is a reference to a whole variable, get that variable
117     *
118     * \return
119     * Pointer to a variable that is completely dereferenced by the r-value.  If
120     * the r-value is not a dereference or the dereference does not access the
121     * entire variable (i.e., it's just one array element, struct field), \c NULL
122     * is returned.
123     */
124    virtual ir_variable *whole_variable_referenced()
125    {
126       return NULL;
127    }
128
129 protected:
130    ir_rvalue()
131    {
132       /* empty */
133    }
134 };
135
136
137 enum ir_variable_mode {
138    ir_var_auto = 0,
139    ir_var_uniform,
140    ir_var_in,
141    ir_var_out,
142    ir_var_inout
143 };
144
145 enum ir_variable_interpolation {
146    ir_var_smooth = 0,
147    ir_var_flat,
148    ir_var_noperspective
149 };
150
151
152 class ir_variable : public ir_instruction {
153 public:
154    ir_variable(const struct glsl_type *, const char *);
155
156    virtual ir_instruction *clone(struct hash_table *ht) const;
157
158    virtual ir_variable *as_variable()
159    {
160       return this;
161    }
162
163    virtual void accept(ir_visitor *v)
164    {
165       v->visit(this);
166    }
167
168    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
169
170
171    /**
172     * Get the string value for the interpolation qualifier
173     *
174     * \return
175     * If none of \c shader_in or \c shader_out is set, an empty string will
176     * be returned.  Otherwise the string that would be used in a shader to
177     * specify \c mode will be returned.
178     */
179    const char *interpolation_string() const;
180
181    /**
182     * Calculate the number of slots required to hold this variable
183     *
184     * This is used to determine how many uniform or varying locations a variable
185     * occupies.  The count is in units of floating point components.
186     */
187    unsigned component_slots() const;
188
189    const char *name;
190
191    /**
192     * Highest element accessed with a constant expression array index
193     *
194     * Not used for non-array variables.
195     */
196    unsigned max_array_access;
197
198    unsigned read_only:1;
199    unsigned centroid:1;
200    unsigned invariant:1;
201    /** If the variable is initialized outside of the scope of the shader */
202    unsigned shader_in:1;
203    /**
204     * If the variable value is later used outside of the scope of the shader.
205     */
206    unsigned shader_out:1;
207
208    unsigned mode:3;
209    unsigned interpolation:2;
210
211    /**
212     * Flag that the whole array is assignable
213     *
214     * In GLSL 1.20 and later whole arrays are assignable (and comparable for
215     * equality).  This flag enables this behavior.
216     */
217    unsigned array_lvalue:1;
218
219    /**
220     * Storage location of the base of this variable
221     *
222     * The precise meaning of this field depends on the nature of the variable.
223     *
224     *   - Vertex shader input: one of the values from \c gl_vert_attrib.
225     *   - Vertex shader output: one of the values from \c gl_vert_result.
226     *   - Fragment shader input: one of the values from \c gl_frag_attrib.
227     *   - Fragment shader output: one of the values from \c gl_frag_result.
228     *   - Uniforms: Per-stage uniform slot number.
229     *   - Other: This field is not currently used.
230     *
231     * If the variable is a uniform, shader input, or shader output, and the
232     * slot has not been assigned, the value will be -1.
233     */
234    int location;
235
236    /**
237     * Emit a warning if this variable is accessed.
238     */
239    const char *warn_extension;
240
241    /**
242     * Value assigned in the initializer of a variable declared "const"
243     */
244    ir_constant *constant_value;
245 };
246
247
248 /*@{*/
249 /**
250  * The representation of a function instance; may be the full definition or
251  * simply a prototype.
252  */
253 class ir_function_signature : public ir_instruction {
254    /* An ir_function_signature will be part of the list of signatures in
255     * an ir_function.
256     */
257 public:
258    ir_function_signature(const glsl_type *return_type);
259
260    virtual ir_instruction *clone(struct hash_table *ht) const;
261
262    virtual void accept(ir_visitor *v)
263    {
264       v->visit(this);
265    }
266
267    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
268
269    /**
270     * Get the name of the function for which this is a signature
271     */
272    const char *function_name() const;
273
274    /**
275     * Check whether the qualifiers match between this signature's parameters
276     * and the supplied parameter list.  If not, returns the name of the first
277     * parameter with mismatched qualifiers (for use in error messages).
278     */
279    const char *qualifiers_match(exec_list *params);
280
281    /**
282     * Replace the current parameter list with the given one.  This is useful
283     * if the current information came from a prototype, and either has invalid
284     * or missing parameter names.
285     */
286    void replace_parameters(exec_list *new_params);
287
288    /**
289     * Function return type.
290     *
291     * \note This discards the optional precision qualifier.
292     */
293    const struct glsl_type *return_type;
294
295    /**
296     * List of ir_variable of function parameters.
297     *
298     * This represents the storage.  The paramaters passed in a particular
299     * call will be in ir_call::actual_paramaters.
300     */
301    struct exec_list parameters;
302
303    /** Whether or not this function has a body (which may be empty). */
304    unsigned is_defined:1;
305
306    /** Body of instructions in the function. */
307    struct exec_list body;
308
309 private:
310    /** Function of which this signature is one overload. */
311    class ir_function *function;
312
313    friend class ir_function;
314 };
315
316
317 /**
318  * Header for tracking multiple overloaded functions with the same name.
319  * Contains a list of ir_function_signatures representing each of the
320  * actual functions.
321  */
322 class ir_function : public ir_instruction {
323 public:
324    ir_function(const char *name);
325
326    virtual ir_instruction *clone(struct hash_table *ht) const;
327
328    virtual ir_function *as_function()
329    {
330       return this;
331    }
332
333    virtual void accept(ir_visitor *v)
334    {
335       v->visit(this);
336    }
337
338    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
339
340    void add_signature(ir_function_signature *sig)
341    {
342       sig->function = this;
343       signatures.push_tail(sig);
344    }
345
346    /**
347     * Get an iterator for the set of function signatures
348     */
349    exec_list_iterator iterator()
350    {
351       return signatures.iterator();
352    }
353
354    /**
355     * Find a signature that matches a set of actual parameters, taking implicit
356     * conversions into account.
357     */
358    const ir_function_signature *matching_signature(exec_list *actual_param);
359
360    /**
361     * Find a signature that exactly matches a set of actual parameters without
362     * any implicit type conversions.
363     */
364    ir_function_signature *exact_matching_signature(exec_list *actual_ps);
365
366    /**
367     * Name of the function.
368     */
369    const char *name;
370
371 private:
372    /**
373     * List of ir_function_signature for each overloaded function with this name.
374     */
375    struct exec_list signatures;
376 };
377
378 inline const char *ir_function_signature::function_name() const
379 {
380    return function->name;
381 }
382 /*@}*/
383
384
385 /**
386  * IR instruction representing high-level if-statements
387  */
388 class ir_if : public ir_instruction {
389 public:
390    ir_if(ir_rvalue *condition)
391       : condition(condition)
392    {
393       /* empty */
394    }
395
396    virtual ir_instruction *clone(struct hash_table *ht) const;
397
398    virtual ir_if *as_if()
399    {
400       return this;
401    }
402
403    virtual void accept(ir_visitor *v)
404    {
405       v->visit(this);
406    }
407
408    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
409
410    ir_rvalue *condition;
411    /** List of ir_instruction for the body of the then branch */
412    exec_list  then_instructions;
413    /** List of ir_instruction for the body of the else branch */
414    exec_list  else_instructions;
415 };
416
417
418 /**
419  * IR instruction representing a high-level loop structure.
420  */
421 class ir_loop : public ir_instruction {
422 public:
423    ir_loop() : from(NULL), to(NULL), increment(NULL), counter(NULL)
424    {
425       /* empty */
426    }
427
428    virtual ir_instruction *clone(struct hash_table *ht) const;
429
430    virtual void accept(ir_visitor *v)
431    {
432       v->visit(this);
433    }
434
435    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
436
437    virtual ir_loop *as_loop()
438    {
439       return this;
440    }
441
442    /**
443     * Get an iterator for the instructions of the loop body
444     */
445    exec_list_iterator iterator()
446    {
447       return body_instructions.iterator();
448    }
449
450    /** List of ir_instruction that make up the body of the loop. */
451    exec_list body_instructions;
452
453    /**
454     * \name Loop counter and controls
455     */
456    /*@{*/
457    ir_rvalue *from;
458    ir_rvalue *to;
459    ir_rvalue *increment;
460    ir_variable *counter;
461    /*@}*/
462 };
463
464
465 class ir_assignment : public ir_rvalue {
466 public:
467    ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition);
468
469    virtual ir_instruction *clone(struct hash_table *ht) const;
470
471    virtual void accept(ir_visitor *v)
472    {
473       v->visit(this);
474    }
475
476    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
477
478    virtual ir_assignment * as_assignment()
479    {
480       return this;
481    }
482
483    /**
484     * Left-hand side of the assignment.
485     */
486    ir_rvalue *lhs;
487
488    /**
489     * Value being assigned
490     */
491    ir_rvalue *rhs;
492
493    /**
494     * Optional condition for the assignment.
495     */
496    ir_rvalue *condition;
497 };
498
499 /* Update ir_expression::num_operands() and operator_strs when
500  * updating this list.
501  */
502 enum ir_expression_operation {
503    ir_unop_bit_not,
504    ir_unop_logic_not,
505    ir_unop_neg,
506    ir_unop_abs,
507    ir_unop_sign,
508    ir_unop_rcp,
509    ir_unop_rsq,
510    ir_unop_sqrt,
511    ir_unop_exp,
512    ir_unop_log,
513    ir_unop_exp2,
514    ir_unop_log2,
515    ir_unop_f2i,      /**< Float-to-integer conversion. */
516    ir_unop_i2f,      /**< Integer-to-float conversion. */
517    ir_unop_f2b,      /**< Float-to-boolean conversion */
518    ir_unop_b2f,      /**< Boolean-to-float conversion */
519    ir_unop_i2b,      /**< int-to-boolean conversion */
520    ir_unop_b2i,      /**< Boolean-to-int conversion */
521    ir_unop_u2f,      /**< Unsigned-to-float conversion. */
522
523    /**
524     * \name Unary floating-point rounding operations.
525     */
526    /*@{*/
527    ir_unop_trunc,
528    ir_unop_ceil,
529    ir_unop_floor,
530    /*@}*/
531
532    /**
533     * \name Trigonometric operations.
534     */
535    /*@{*/
536    ir_unop_sin,
537    ir_unop_cos,
538    /*@}*/
539
540    /**
541     * \name Partial derivatives.
542     */
543    /*@{*/
544    ir_unop_dFdx,
545    ir_unop_dFdy,
546    /*@}*/
547
548    ir_binop_add,
549    ir_binop_sub,
550    ir_binop_mul,
551    ir_binop_div,
552    ir_binop_mod,
553
554    /**
555     * \name Binary comparison operators
556     */
557    /*@{*/
558    ir_binop_less,
559    ir_binop_greater,
560    ir_binop_lequal,
561    ir_binop_gequal,
562    ir_binop_equal,
563    ir_binop_nequal,
564    /*@}*/
565
566    /**
567     * \name Bit-wise binary operations.
568     */
569    /*@{*/
570    ir_binop_lshift,
571    ir_binop_rshift,
572    ir_binop_bit_and,
573    ir_binop_bit_xor,
574    ir_binop_bit_or,
575    /*@}*/
576
577    ir_binop_logic_and,
578    ir_binop_logic_xor,
579    ir_binop_logic_or,
580
581    ir_binop_dot,
582    ir_binop_min,
583    ir_binop_max,
584
585    ir_binop_pow
586 };
587
588 class ir_expression : public ir_rvalue {
589 public:
590    ir_expression(int op, const struct glsl_type *type,
591                  ir_rvalue *, ir_rvalue *);
592
593    virtual ir_instruction *clone(struct hash_table *ht) const;
594
595    static unsigned int get_num_operands(ir_expression_operation);
596    unsigned int get_num_operands() const
597    {
598       return get_num_operands(operation);
599    }
600
601    /**
602     * Return a string representing this expression's operator.
603     */
604    const char *operator_string();
605
606    /**
607     * Do a reverse-lookup to translate the given string into an operator.
608     */
609    static ir_expression_operation get_operator(const char *);
610
611    virtual void accept(ir_visitor *v)
612    {
613       v->visit(this);
614    }
615
616    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
617
618    ir_expression_operation operation;
619    ir_rvalue *operands[2];
620 };
621
622
623 /**
624  * IR instruction representing a function call
625  */
626 class ir_call : public ir_rvalue {
627 public:
628    ir_call(const ir_function_signature *callee, exec_list *actual_parameters)
629       : callee(callee)
630    {
631       assert(callee->return_type != NULL);
632       type = callee->return_type;
633       actual_parameters->move_nodes_to(& this->actual_parameters);
634    }
635
636    virtual ir_instruction *clone(struct hash_table *ht) const;
637
638    virtual ir_call *as_call()
639    {
640       return this;
641    }
642
643    virtual void accept(ir_visitor *v)
644    {
645       v->visit(this);
646    }
647
648    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
649
650    /**
651     * Get a generic ir_call object when an error occurs
652     *
653     * Any allocation will be performed with 'ctx' as talloc owner.
654     */
655    static ir_call *get_error_instruction(void *ctx);
656
657    /**
658     * Get an iterator for the set of acutal parameters
659     */
660    exec_list_iterator iterator()
661    {
662       return actual_parameters.iterator();
663    }
664
665    /**
666     * Get the name of the function being called.
667     */
668    const char *callee_name() const
669    {
670       return callee->function_name();
671    }
672
673    const ir_function_signature *get_callee()
674    {
675       return callee;
676    }
677
678    /**
679     * Generates an inline version of the function before @ir,
680     * returning the return value of the function.
681     */
682    ir_rvalue *generate_inline(ir_instruction *ir);
683
684 private:
685    ir_call()
686       : callee(NULL)
687    {
688       /* empty */
689    }
690
691    const ir_function_signature *callee;
692
693    /* List of ir_rvalue of paramaters passed in this call. */
694    exec_list actual_parameters;
695 };
696
697
698 /**
699  * \name Jump-like IR instructions.
700  *
701  * These include \c break, \c continue, \c return, and \c discard.
702  */
703 /*@{*/
704 class ir_jump : public ir_instruction {
705 protected:
706    ir_jump()
707    {
708       /* empty */
709    }
710 };
711
712 class ir_return : public ir_jump {
713 public:
714    ir_return()
715       : value(NULL)
716    {
717       /* empty */
718    }
719
720    ir_return(ir_rvalue *value)
721       : value(value)
722    {
723       /* empty */
724    }
725
726    virtual ir_instruction *clone(struct hash_table *) const;
727
728    virtual ir_return *as_return()
729    {
730       return this;
731    }
732
733    ir_rvalue *get_value() const
734    {
735       return value;
736    }
737
738    virtual void accept(ir_visitor *v)
739    {
740       v->visit(this);
741    }
742
743    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
744
745    ir_rvalue *value;
746 };
747
748
749 /**
750  * Jump instructions used inside loops
751  *
752  * These include \c break and \c continue.  The \c break within a loop is
753  * different from the \c break within a switch-statement.
754  *
755  * \sa ir_switch_jump
756  */
757 class ir_loop_jump : public ir_jump {
758 public:
759    enum jump_mode {
760       jump_break,
761       jump_continue
762    };
763
764    ir_loop_jump(jump_mode mode)
765    {
766       this->mode = mode;
767       this->loop = loop;
768    }
769
770    virtual ir_instruction *clone(struct hash_table *) const;
771
772    virtual void accept(ir_visitor *v)
773    {
774       v->visit(this);
775    }
776
777    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
778
779    bool is_break() const
780    {
781       return mode == jump_break;
782    }
783
784    bool is_continue() const
785    {
786       return mode == jump_continue;
787    }
788
789    /** Mode selector for the jump instruction. */
790    enum jump_mode mode;
791 private:
792    /** Loop containing this break instruction. */
793    ir_loop *loop;
794 };
795 /*@}*/
796
797
798 /**
799  * Texture sampling opcodes used in ir_texture
800  */
801 enum ir_texture_opcode {
802    ir_tex,              /* Regular texture look-up */
803    ir_txb,              /* Texture look-up with LOD bias */
804    ir_txl,              /* Texture look-up with explicit LOD */
805    ir_txd,              /* Texture look-up with partial derivatvies */
806    ir_txf               /* Texel fetch with explicit LOD */
807 };
808
809
810 /**
811  * IR instruction to sample a texture
812  *
813  * The specific form of the IR instruction depends on the \c mode value
814  * selected from \c ir_texture_opcodes.  In the printed IR, these will
815  * appear as:
816  *
817  *                              Texel offset
818  *                              |       Projection divisor
819  *                              |       |   Shadow comparitor
820  *                              |       |   |
821  *                              v       v   v
822  * (tex (sampler) (coordinate) (0 0 0) (1) ( ))
823  * (txb (sampler) (coordinate) (0 0 0) (1) ( ) (bias))
824  * (txl (sampler) (coordinate) (0 0 0) (1) ( ) (lod))
825  * (txd (sampler) (coordinate) (0 0 0) (1) ( ) (dPdx dPdy))
826  * (txf (sampler) (coordinate) (0 0 0)         (lod))
827  */
828 class ir_texture : public ir_rvalue {
829 public:
830    ir_texture(enum ir_texture_opcode op)
831       : op(op), projector(NULL), shadow_comparitor(NULL)
832    {
833       /* empty */
834    }
835
836    virtual ir_instruction *clone(struct hash_table *) const;
837
838    virtual void accept(ir_visitor *v)
839    {
840       v->visit(this);
841    }
842
843    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
844
845    /**
846     * Return a string representing the ir_texture_opcode.
847     */
848    const char *opcode_string();
849
850    /** Set the sampler and infer the type. */
851    void set_sampler(ir_dereference *sampler);
852
853    /**
854     * Do a reverse-lookup to translate a string into an ir_texture_opcode.
855     */
856    static ir_texture_opcode get_opcode(const char *);
857
858    enum ir_texture_opcode op;
859
860    /** Sampler to use for the texture access. */
861    ir_dereference *sampler;
862
863    /** Texture coordinate to sample */
864    ir_rvalue *coordinate;
865
866    /**
867     * Value used for projective divide.
868     *
869     * If there is no projective divide (the common case), this will be
870     * \c NULL.  Optimization passes should check for this to point to a constant
871     * of 1.0 and replace that with \c NULL.
872     */
873    ir_rvalue *projector;
874
875    /**
876     * Coordinate used for comparison on shadow look-ups.
877     *
878     * If there is no shadow comparison, this will be \c NULL.  For the
879     * \c ir_txf opcode, this *must* be \c NULL.
880     */
881    ir_rvalue *shadow_comparitor;
882
883    /** Explicit texel offsets. */
884    signed char offsets[3];
885
886    union {
887       ir_rvalue *lod;           /**< Floating point LOD */
888       ir_rvalue *bias;          /**< Floating point LOD bias */
889       struct {
890          ir_rvalue *dPdx;       /**< Partial derivative of coordinate wrt X */
891          ir_rvalue *dPdy;       /**< Partial derivative of coordinate wrt Y */
892       } grad;
893    } lod_info;
894 };
895
896
897 struct ir_swizzle_mask {
898    unsigned x:2;
899    unsigned y:2;
900    unsigned z:2;
901    unsigned w:2;
902
903    /**
904     * Number of components in the swizzle.
905     */
906    unsigned num_components:3;
907
908    /**
909     * Does the swizzle contain duplicate components?
910     *
911     * L-value swizzles cannot contain duplicate components.
912     */
913    unsigned has_duplicates:1;
914 };
915
916
917 class ir_swizzle : public ir_rvalue {
918 public:
919    ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
920               unsigned count);
921    ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask);
922
923    virtual ir_instruction *clone(struct hash_table *) const;
924
925    virtual ir_swizzle *as_swizzle()
926    {
927       return this;
928    }
929
930    /**
931     * Construct an ir_swizzle from the textual representation.  Can fail.
932     */
933    static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
934
935    virtual void accept(ir_visitor *v)
936    {
937       v->visit(this);
938    }
939
940    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
941
942    bool is_lvalue()
943    {
944       return val->is_lvalue() && !mask.has_duplicates;
945    }
946
947    /**
948     * Get the variable that is ultimately referenced by an r-value
949     */
950    virtual ir_variable *variable_referenced();
951
952    ir_rvalue *val;
953    ir_swizzle_mask mask;
954 };
955
956
957 class ir_dereference : public ir_rvalue {
958 public:
959    virtual ir_dereference *as_dereference()
960    {
961       return this;
962    }
963
964    bool is_lvalue();
965
966    /**
967     * Get the variable that is ultimately referenced by an r-value
968     */
969    virtual ir_variable *variable_referenced() = 0;
970 };
971
972
973 class ir_dereference_variable : public ir_dereference {
974 public:
975    ir_dereference_variable(ir_variable *var);
976
977    virtual ir_instruction *clone(struct hash_table *) const;
978
979    /**
980     * Get the variable that is ultimately referenced by an r-value
981     */
982    virtual ir_variable *variable_referenced()
983    {
984       return this->var;
985    }
986
987    virtual ir_variable *whole_variable_referenced()
988    {
989       /* ir_dereference_variable objects always dereference the entire
990        * variable.  However, if this dereference is dereferenced by anything
991        * else, the complete deferefernce chain is not a whole-variable
992        * dereference.  This method should only be called on the top most
993        * ir_rvalue in a dereference chain.
994        */
995       return this->var;
996    }
997
998    virtual void accept(ir_visitor *v)
999    {
1000       v->visit(this);
1001    }
1002
1003    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1004
1005    /**
1006     * Object being dereferenced.
1007     */
1008    ir_variable *var;
1009 };
1010
1011
1012 class ir_dereference_array : public ir_dereference {
1013 public:
1014    ir_dereference_array(ir_rvalue *value, ir_rvalue *array_index);
1015
1016    ir_dereference_array(ir_variable *var, ir_rvalue *array_index);
1017
1018    virtual ir_instruction *clone(struct hash_table *) const;
1019
1020    virtual ir_dereference_array *as_dereference_array()
1021    {
1022       return this;
1023    }
1024
1025    /**
1026     * Get the variable that is ultimately referenced by an r-value
1027     */
1028    virtual ir_variable *variable_referenced()
1029    {
1030       return this->array->variable_referenced();
1031    }
1032
1033    virtual void accept(ir_visitor *v)
1034    {
1035       v->visit(this);
1036    }
1037
1038    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1039
1040    ir_rvalue *array;
1041    ir_rvalue *array_index;
1042
1043 private:
1044    void set_array(ir_rvalue *value);
1045 };
1046
1047
1048 class ir_dereference_record : public ir_dereference {
1049 public:
1050    ir_dereference_record(ir_rvalue *value, const char *field);
1051
1052    ir_dereference_record(ir_variable *var, const char *field);
1053
1054    virtual ir_instruction *clone(struct hash_table *) const;
1055
1056    /**
1057     * Get the variable that is ultimately referenced by an r-value
1058     */
1059    virtual ir_variable *variable_referenced()
1060    {
1061       return this->record->variable_referenced();
1062    }
1063
1064    virtual void accept(ir_visitor *v)
1065    {
1066       v->visit(this);
1067    }
1068
1069    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1070
1071    ir_rvalue *record;
1072    const char *field;
1073 };
1074
1075
1076 /**
1077  * Data stored in an ir_constant
1078  */
1079 union ir_constant_data {
1080       unsigned u[16];
1081       int i[16];
1082       float f[16];
1083       bool b[16];
1084 };
1085
1086
1087 class ir_constant : public ir_rvalue {
1088 public:
1089    ir_constant(const struct glsl_type *type, const ir_constant_data *data);
1090    ir_constant(bool b);
1091    ir_constant(unsigned int u);
1092    ir_constant(int i);
1093    ir_constant(float f);
1094
1095    /**
1096     * Construct an ir_constant from a list of ir_constant values
1097     */
1098    ir_constant(const struct glsl_type *type, exec_list *values);
1099
1100    /**
1101     * Construct an ir_constant from a scalar component of another ir_constant
1102     *
1103     * The new \c ir_constant inherits the type of the component from the
1104     * source constant.
1105     *
1106     * \note
1107     * In the case of a matrix constant, the new constant is a scalar, \b not
1108     * a vector.
1109     */
1110    ir_constant(const ir_constant *c, unsigned i);
1111
1112    virtual ir_instruction *clone(struct hash_table *) const;
1113
1114    virtual ir_constant *as_constant()
1115    {
1116       return this;
1117    }
1118
1119    virtual void accept(ir_visitor *v)
1120    {
1121       v->visit(this);
1122    }
1123
1124    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1125
1126    /**
1127     * Get a particular component of a constant as a specific type
1128     *
1129     * This is useful, for example, to get a value from an integer constant
1130     * as a float or bool.  This appears frequently when constructors are
1131     * called with all constant parameters.
1132     */
1133    /*@{*/
1134    bool get_bool_component(unsigned i) const;
1135    float get_float_component(unsigned i) const;
1136    int get_int_component(unsigned i) const;
1137    unsigned get_uint_component(unsigned i) const;
1138    /*@}*/
1139
1140    ir_constant *get_record_field(const char *name);
1141
1142    /**
1143     * Determine whether a constant has the same value as another constant
1144     */
1145    bool has_value(const ir_constant *) const;
1146
1147    /**
1148     * Value of the constant.
1149     *
1150     * The field used to back the values supplied by the constant is determined
1151     * by the type associated with the \c ir_instruction.  Constants may be
1152     * scalars, vectors, or matrices.
1153     */
1154    union ir_constant_data value;
1155
1156    exec_list components;
1157
1158 private:
1159    /**
1160     * Parameterless constructor only used by the clone method
1161     */
1162    ir_constant(void);
1163 };
1164
1165 void
1166 visit_exec_list(exec_list *list, ir_visitor *visitor);
1167
1168 void validate_ir_tree(exec_list *instructions);
1169
1170 extern void
1171 _mesa_glsl_initialize_variables(exec_list *instructions,
1172                                 struct _mesa_glsl_parse_state *state);
1173
1174 extern void
1175 _mesa_glsl_initialize_functions(exec_list *instructions,
1176                                 struct _mesa_glsl_parse_state *state);
1177
1178 #endif /* IR_H */