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