ir.h revision 7d19bf2ec34dceb621ec82861be3d12ba73e67d3
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
32extern "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/**
45 * Base class of all IR instructions
46 */
47class ir_instruction : public exec_node {
48public:
49   const struct glsl_type *type;
50
51   class ir_constant *constant_expression_value();
52
53   /** ir_print_visitor helper for debugging. */
54   void print(void) const;
55
56   virtual void accept(ir_visitor *) = 0;
57   virtual ir_visitor_status accept(ir_hierarchical_visitor *) = 0;
58   virtual ir_instruction *clone(struct hash_table *ht) const = 0;
59
60   /**
61    * \name IR instruction downcast functions
62    *
63    * These functions either cast the object to a derived class or return
64    * \c NULL if the object's type does not match the specified derived class.
65    * Additional downcast functions will be added as needed.
66    */
67   /*@{*/
68   virtual class ir_variable *          as_variable()         { return NULL; }
69   virtual class ir_function *          as_function()         { return NULL; }
70   virtual class ir_dereference *       as_dereference()      { return NULL; }
71   virtual class ir_dereference_array *	as_dereference_array() { return NULL; }
72   virtual class ir_dereference_variable *as_dereference_variable() { return NULL; }
73   virtual class ir_expression *        as_expression()       { return NULL; }
74   virtual class ir_rvalue *            as_rvalue()           { return NULL; }
75   virtual class ir_loop *              as_loop()             { return NULL; }
76   virtual class ir_assignment *        as_assignment()       { return NULL; }
77   virtual class ir_call *              as_call()             { return NULL; }
78   virtual class ir_return *            as_return()           { return NULL; }
79   virtual class ir_if *                as_if()               { return NULL; }
80   virtual class ir_swizzle *           as_swizzle()          { return NULL; }
81   virtual class ir_constant *          as_constant()         { return NULL; }
82   /*@}*/
83
84protected:
85   ir_instruction()
86   {
87      /* empty */
88   }
89};
90
91
92class ir_rvalue : public ir_instruction {
93public:
94   virtual ir_rvalue *clone(struct hash_table *) const = 0;
95
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
129protected:
130   ir_rvalue()
131   {
132      /* empty */
133   }
134};
135
136
137enum 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
145enum ir_variable_interpolation {
146   ir_var_smooth = 0,
147   ir_var_flat,
148   ir_var_noperspective
149};
150
151
152class ir_variable : public ir_instruction {
153public:
154   ir_variable(const struct glsl_type *, const char *);
155
156   virtual ir_variable *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 */
253class 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    */
257public:
258   ir_function_signature(const glsl_type *return_type);
259
260   virtual ir_function_signature *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    * Get a handle to the function for which this is a signature
276    *
277    * There is no setter function, this function returns a \c const pointer,
278    * and \c ir_function_signature::_function is private for a reason.  The
279    * only way to make a connection between a function and function signature
280    * is via \c ir_function::add_signature.  This helps ensure that certain
281    * invariants (i.e., a function signature is in the list of signatures for
282    * its \c _function) are met.
283    *
284    * \sa ir_function::add_signature
285    */
286   inline const class ir_function *function() const
287   {
288      return this->_function;
289   }
290
291   /**
292    * Check whether the qualifiers match between this signature's parameters
293    * and the supplied parameter list.  If not, returns the name of the first
294    * parameter with mismatched qualifiers (for use in error messages).
295    */
296   const char *qualifiers_match(exec_list *params);
297
298   /**
299    * Replace the current parameter list with the given one.  This is useful
300    * if the current information came from a prototype, and either has invalid
301    * or missing parameter names.
302    */
303   void replace_parameters(exec_list *new_params);
304
305   /**
306    * Function return type.
307    *
308    * \note This discards the optional precision qualifier.
309    */
310   const struct glsl_type *return_type;
311
312   /**
313    * List of ir_variable of function parameters.
314    *
315    * This represents the storage.  The paramaters passed in a particular
316    * call will be in ir_call::actual_paramaters.
317    */
318   struct exec_list parameters;
319
320   /** Whether or not this function has a body (which may be empty). */
321   unsigned is_defined:1;
322
323   /** Whether or not this function signature is a built-in. */
324   unsigned is_built_in:1;
325
326   /** Body of instructions in the function. */
327   struct exec_list body;
328
329private:
330   /** Function of which this signature is one overload. */
331   class ir_function *_function;
332
333   friend class ir_function;
334};
335
336
337/**
338 * Header for tracking multiple overloaded functions with the same name.
339 * Contains a list of ir_function_signatures representing each of the
340 * actual functions.
341 */
342class ir_function : public ir_instruction {
343public:
344   ir_function(const char *name);
345
346   virtual ir_function *clone(struct hash_table *ht) const;
347
348   virtual ir_function *as_function()
349   {
350      return this;
351   }
352
353   virtual void accept(ir_visitor *v)
354   {
355      v->visit(this);
356   }
357
358   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
359
360   void add_signature(ir_function_signature *sig)
361   {
362      sig->_function = this;
363      this->signatures.push_tail(sig);
364   }
365
366   /**
367    * Get an iterator for the set of function signatures
368    */
369   exec_list_iterator iterator()
370   {
371      return signatures.iterator();
372   }
373
374   /**
375    * Find a signature that matches a set of actual parameters, taking implicit
376    * conversions into account.
377    */
378   ir_function_signature *matching_signature(exec_list *actual_param);
379
380   /**
381    * Find a signature that exactly matches a set of actual parameters without
382    * any implicit type conversions.
383    */
384   ir_function_signature *exact_matching_signature(exec_list *actual_ps);
385
386   /**
387    * Name of the function.
388    */
389   const char *name;
390
391private:
392   /**
393    * List of ir_function_signature for each overloaded function with this name.
394    */
395   struct exec_list signatures;
396};
397
398inline const char *ir_function_signature::function_name() const
399{
400   return this->_function->name;
401}
402/*@}*/
403
404
405/**
406 * IR instruction representing high-level if-statements
407 */
408class ir_if : public ir_instruction {
409public:
410   ir_if(ir_rvalue *condition)
411      : condition(condition)
412   {
413      /* empty */
414   }
415
416   virtual ir_if *clone(struct hash_table *ht) const;
417
418   virtual ir_if *as_if()
419   {
420      return this;
421   }
422
423   virtual void accept(ir_visitor *v)
424   {
425      v->visit(this);
426   }
427
428   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
429
430   ir_rvalue *condition;
431   /** List of ir_instruction for the body of the then branch */
432   exec_list  then_instructions;
433   /** List of ir_instruction for the body of the else branch */
434   exec_list  else_instructions;
435};
436
437
438/**
439 * IR instruction representing a high-level loop structure.
440 */
441class ir_loop : public ir_instruction {
442public:
443   ir_loop() : from(NULL), to(NULL), increment(NULL), counter(NULL)
444   {
445      /* empty */
446   }
447
448   virtual ir_loop *clone(struct hash_table *ht) const;
449
450   virtual void accept(ir_visitor *v)
451   {
452      v->visit(this);
453   }
454
455   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
456
457   virtual ir_loop *as_loop()
458   {
459      return this;
460   }
461
462   /**
463    * Get an iterator for the instructions of the loop body
464    */
465   exec_list_iterator iterator()
466   {
467      return body_instructions.iterator();
468   }
469
470   /** List of ir_instruction that make up the body of the loop. */
471   exec_list body_instructions;
472
473   /**
474    * \name Loop counter and controls
475    */
476   /*@{*/
477   ir_rvalue *from;
478   ir_rvalue *to;
479   ir_rvalue *increment;
480   ir_variable *counter;
481   /*@}*/
482};
483
484
485class ir_assignment : public ir_rvalue {
486public:
487   ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition);
488
489   virtual ir_assignment *clone(struct hash_table *ht) const;
490
491   virtual void accept(ir_visitor *v)
492   {
493      v->visit(this);
494   }
495
496   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
497
498   virtual ir_assignment * as_assignment()
499   {
500      return this;
501   }
502
503   /**
504    * Left-hand side of the assignment.
505    */
506   ir_rvalue *lhs;
507
508   /**
509    * Value being assigned
510    */
511   ir_rvalue *rhs;
512
513   /**
514    * Optional condition for the assignment.
515    */
516   ir_rvalue *condition;
517};
518
519/* Update ir_expression::num_operands() and operator_strs when
520 * updating this list.
521 */
522enum ir_expression_operation {
523   ir_unop_bit_not,
524   ir_unop_logic_not,
525   ir_unop_neg,
526   ir_unop_abs,
527   ir_unop_sign,
528   ir_unop_rcp,
529   ir_unop_rsq,
530   ir_unop_sqrt,
531   ir_unop_exp,
532   ir_unop_log,
533   ir_unop_exp2,
534   ir_unop_log2,
535   ir_unop_f2i,      /**< Float-to-integer conversion. */
536   ir_unop_i2f,      /**< Integer-to-float conversion. */
537   ir_unop_f2b,      /**< Float-to-boolean conversion */
538   ir_unop_b2f,      /**< Boolean-to-float conversion */
539   ir_unop_i2b,      /**< int-to-boolean conversion */
540   ir_unop_b2i,      /**< Boolean-to-int conversion */
541   ir_unop_u2f,      /**< Unsigned-to-float conversion. */
542
543   /**
544    * \name Unary floating-point rounding operations.
545    */
546   /*@{*/
547   ir_unop_trunc,
548   ir_unop_ceil,
549   ir_unop_floor,
550   ir_unop_fract,
551   /*@}*/
552
553   /**
554    * \name Trigonometric operations.
555    */
556   /*@{*/
557   ir_unop_sin,
558   ir_unop_cos,
559   /*@}*/
560
561   /**
562    * \name Partial derivatives.
563    */
564   /*@{*/
565   ir_unop_dFdx,
566   ir_unop_dFdy,
567   /*@}*/
568
569   ir_binop_add,
570   ir_binop_sub,
571   ir_binop_mul,
572   ir_binop_div,
573
574   /**
575    * Takes one of two combinations of arguments:
576    *
577    * - mod(vecN, vecN)
578    * - mod(vecN, float)
579    *
580    * Does not take integer types.
581    */
582   ir_binop_mod,
583
584   /**
585    * \name Binary comparison operators
586    */
587   /*@{*/
588   ir_binop_less,
589   ir_binop_greater,
590   ir_binop_lequal,
591   ir_binop_gequal,
592   ir_binop_equal,
593   ir_binop_nequal,
594   /*@}*/
595
596   /**
597    * \name Bit-wise binary operations.
598    */
599   /*@{*/
600   ir_binop_lshift,
601   ir_binop_rshift,
602   ir_binop_bit_and,
603   ir_binop_bit_xor,
604   ir_binop_bit_or,
605   /*@}*/
606
607   ir_binop_logic_and,
608   ir_binop_logic_xor,
609   ir_binop_logic_or,
610
611   ir_binop_dot,
612   ir_binop_min,
613   ir_binop_max,
614
615   ir_binop_pow
616};
617
618class ir_expression : public ir_rvalue {
619public:
620   ir_expression(int op, const struct glsl_type *type,
621		 ir_rvalue *, ir_rvalue *);
622
623   virtual ir_expression *as_expression()
624   {
625      return this;
626   }
627
628   virtual ir_expression *clone(struct hash_table *ht) const;
629
630   static unsigned int get_num_operands(ir_expression_operation);
631   unsigned int get_num_operands() const
632   {
633      return get_num_operands(operation);
634   }
635
636   /**
637    * Return a string representing this expression's operator.
638    */
639   const char *operator_string();
640
641   /**
642    * Do a reverse-lookup to translate the given string into an operator.
643    */
644   static ir_expression_operation get_operator(const char *);
645
646   virtual void accept(ir_visitor *v)
647   {
648      v->visit(this);
649   }
650
651   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
652
653   ir_expression_operation operation;
654   ir_rvalue *operands[2];
655};
656
657
658/**
659 * IR instruction representing a function call
660 */
661class ir_call : public ir_rvalue {
662public:
663   ir_call(const ir_function_signature *callee, exec_list *actual_parameters)
664      : callee(callee)
665   {
666      assert(callee->return_type != NULL);
667      type = callee->return_type;
668      actual_parameters->move_nodes_to(& this->actual_parameters);
669   }
670
671   virtual ir_call *clone(struct hash_table *ht) const;
672
673   virtual ir_call *as_call()
674   {
675      return this;
676   }
677
678   virtual void accept(ir_visitor *v)
679   {
680      v->visit(this);
681   }
682
683   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
684
685   /**
686    * Get a generic ir_call object when an error occurs
687    *
688    * Any allocation will be performed with 'ctx' as talloc owner.
689    */
690   static ir_call *get_error_instruction(void *ctx);
691
692   /**
693    * Get an iterator for the set of acutal parameters
694    */
695   exec_list_iterator iterator()
696   {
697      return actual_parameters.iterator();
698   }
699
700   /**
701    * Get the name of the function being called.
702    */
703   const char *callee_name() const
704   {
705      return callee->function_name();
706   }
707
708   const ir_function_signature *get_callee()
709   {
710      return callee;
711   }
712
713   /**
714    * Set the function call target
715    */
716   void set_callee(const ir_function_signature *sig);
717
718   /**
719    * Generates an inline version of the function before @ir,
720    * returning the return value of the function.
721    */
722   ir_rvalue *generate_inline(ir_instruction *ir);
723
724private:
725   ir_call()
726      : callee(NULL)
727   {
728      /* empty */
729   }
730
731   const ir_function_signature *callee;
732
733   /* List of ir_rvalue of paramaters passed in this call. */
734   exec_list actual_parameters;
735};
736
737
738/**
739 * \name Jump-like IR instructions.
740 *
741 * These include \c break, \c continue, \c return, and \c discard.
742 */
743/*@{*/
744class ir_jump : public ir_instruction {
745protected:
746   ir_jump()
747   {
748      /* empty */
749   }
750};
751
752class ir_return : public ir_jump {
753public:
754   ir_return()
755      : value(NULL)
756   {
757      /* empty */
758   }
759
760   ir_return(ir_rvalue *value)
761      : value(value)
762   {
763      /* empty */
764   }
765
766   virtual ir_return *clone(struct hash_table *) const;
767
768   virtual ir_return *as_return()
769   {
770      return this;
771   }
772
773   ir_rvalue *get_value() const
774   {
775      return value;
776   }
777
778   virtual void accept(ir_visitor *v)
779   {
780      v->visit(this);
781   }
782
783   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
784
785   ir_rvalue *value;
786};
787
788
789/**
790 * Jump instructions used inside loops
791 *
792 * These include \c break and \c continue.  The \c break within a loop is
793 * different from the \c break within a switch-statement.
794 *
795 * \sa ir_switch_jump
796 */
797class ir_loop_jump : public ir_jump {
798public:
799   enum jump_mode {
800      jump_break,
801      jump_continue
802   };
803
804   ir_loop_jump(jump_mode mode)
805   {
806      this->mode = mode;
807      this->loop = loop;
808   }
809
810   virtual ir_loop_jump *clone(struct hash_table *) const;
811
812   virtual void accept(ir_visitor *v)
813   {
814      v->visit(this);
815   }
816
817   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
818
819   bool is_break() const
820   {
821      return mode == jump_break;
822   }
823
824   bool is_continue() const
825   {
826      return mode == jump_continue;
827   }
828
829   /** Mode selector for the jump instruction. */
830   enum jump_mode mode;
831private:
832   /** Loop containing this break instruction. */
833   ir_loop *loop;
834};
835
836/**
837 * IR instruction representing discard statements.
838 */
839class ir_discard : public ir_jump {
840public:
841   ir_discard()
842   {
843      this->condition = NULL;
844   }
845
846   ir_discard(ir_rvalue *cond)
847   {
848      this->condition = cond;
849   }
850
851   virtual ir_discard *clone(struct hash_table *ht) const;
852
853   virtual void accept(ir_visitor *v)
854   {
855      v->visit(this);
856   }
857
858   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
859
860   ir_rvalue *condition;
861};
862/*@}*/
863
864
865/**
866 * Texture sampling opcodes used in ir_texture
867 */
868enum ir_texture_opcode {
869   ir_tex,		/* Regular texture look-up */
870   ir_txb,		/* Texture look-up with LOD bias */
871   ir_txl,		/* Texture look-up with explicit LOD */
872   ir_txd,		/* Texture look-up with partial derivatvies */
873   ir_txf		/* Texel fetch with explicit LOD */
874};
875
876
877/**
878 * IR instruction to sample a texture
879 *
880 * The specific form of the IR instruction depends on the \c mode value
881 * selected from \c ir_texture_opcodes.  In the printed IR, these will
882 * appear as:
883 *
884 *                              Texel offset
885 *                              |       Projection divisor
886 *                              |       |   Shadow comparitor
887 *                              |       |   |
888 *                              v       v   v
889 * (tex (sampler) (coordinate) (0 0 0) (1) ( ))
890 * (txb (sampler) (coordinate) (0 0 0) (1) ( ) (bias))
891 * (txl (sampler) (coordinate) (0 0 0) (1) ( ) (lod))
892 * (txd (sampler) (coordinate) (0 0 0) (1) ( ) (dPdx dPdy))
893 * (txf (sampler) (coordinate) (0 0 0)         (lod))
894 */
895class ir_texture : public ir_rvalue {
896public:
897   ir_texture(enum ir_texture_opcode op)
898      : op(op), projector(NULL), shadow_comparitor(NULL)
899   {
900      /* empty */
901   }
902
903   virtual ir_texture *clone(struct hash_table *) const;
904
905   virtual void accept(ir_visitor *v)
906   {
907      v->visit(this);
908   }
909
910   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
911
912   /**
913    * Return a string representing the ir_texture_opcode.
914    */
915   const char *opcode_string();
916
917   /** Set the sampler and infer the type. */
918   void set_sampler(ir_dereference *sampler);
919
920   /**
921    * Do a reverse-lookup to translate a string into an ir_texture_opcode.
922    */
923   static ir_texture_opcode get_opcode(const char *);
924
925   enum ir_texture_opcode op;
926
927   /** Sampler to use for the texture access. */
928   ir_dereference *sampler;
929
930   /** Texture coordinate to sample */
931   ir_rvalue *coordinate;
932
933   /**
934    * Value used for projective divide.
935    *
936    * If there is no projective divide (the common case), this will be
937    * \c NULL.  Optimization passes should check for this to point to a constant
938    * of 1.0 and replace that with \c NULL.
939    */
940   ir_rvalue *projector;
941
942   /**
943    * Coordinate used for comparison on shadow look-ups.
944    *
945    * If there is no shadow comparison, this will be \c NULL.  For the
946    * \c ir_txf opcode, this *must* be \c NULL.
947    */
948   ir_rvalue *shadow_comparitor;
949
950   /** Explicit texel offsets. */
951   signed char offsets[3];
952
953   union {
954      ir_rvalue *lod;		/**< Floating point LOD */
955      ir_rvalue *bias;		/**< Floating point LOD bias */
956      struct {
957	 ir_rvalue *dPdx;	/**< Partial derivative of coordinate wrt X */
958	 ir_rvalue *dPdy;	/**< Partial derivative of coordinate wrt Y */
959      } grad;
960   } lod_info;
961};
962
963
964struct ir_swizzle_mask {
965   unsigned x:2;
966   unsigned y:2;
967   unsigned z:2;
968   unsigned w:2;
969
970   /**
971    * Number of components in the swizzle.
972    */
973   unsigned num_components:3;
974
975   /**
976    * Does the swizzle contain duplicate components?
977    *
978    * L-value swizzles cannot contain duplicate components.
979    */
980   unsigned has_duplicates:1;
981};
982
983
984class ir_swizzle : public ir_rvalue {
985public:
986   ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
987              unsigned count);
988
989   ir_swizzle(ir_rvalue *val, const unsigned *components, unsigned count);
990
991   ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask);
992
993   virtual ir_swizzle *clone(struct hash_table *) const;
994
995   virtual ir_swizzle *as_swizzle()
996   {
997      return this;
998   }
999
1000   /**
1001    * Construct an ir_swizzle from the textual representation.  Can fail.
1002    */
1003   static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
1004
1005   virtual void accept(ir_visitor *v)
1006   {
1007      v->visit(this);
1008   }
1009
1010   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1011
1012   bool is_lvalue()
1013   {
1014      return val->is_lvalue() && !mask.has_duplicates;
1015   }
1016
1017   /**
1018    * Get the variable that is ultimately referenced by an r-value
1019    */
1020   virtual ir_variable *variable_referenced();
1021
1022   ir_rvalue *val;
1023   ir_swizzle_mask mask;
1024
1025private:
1026   /**
1027    * Initialize the mask component of a swizzle
1028    *
1029    * This is used by the \c ir_swizzle constructors.
1030    */
1031   void init_mask(const unsigned *components, unsigned count);
1032};
1033
1034
1035class ir_dereference : public ir_rvalue {
1036public:
1037   virtual ir_dereference *clone(struct hash_table *) const = 0;
1038
1039   virtual ir_dereference *as_dereference()
1040   {
1041      return this;
1042   }
1043
1044   bool is_lvalue();
1045
1046   /**
1047    * Get the variable that is ultimately referenced by an r-value
1048    */
1049   virtual ir_variable *variable_referenced() = 0;
1050};
1051
1052
1053class ir_dereference_variable : public ir_dereference {
1054public:
1055   ir_dereference_variable(ir_variable *var);
1056
1057   virtual ir_dereference_variable *clone(struct hash_table *) const;
1058
1059   virtual ir_dereference_variable *as_dereference_variable()
1060   {
1061      return this;
1062   }
1063
1064   /**
1065    * Get the variable that is ultimately referenced by an r-value
1066    */
1067   virtual ir_variable *variable_referenced()
1068   {
1069      return this->var;
1070   }
1071
1072   virtual ir_variable *whole_variable_referenced()
1073   {
1074      /* ir_dereference_variable objects always dereference the entire
1075       * variable.  However, if this dereference is dereferenced by anything
1076       * else, the complete deferefernce chain is not a whole-variable
1077       * dereference.  This method should only be called on the top most
1078       * ir_rvalue in a dereference chain.
1079       */
1080      return this->var;
1081   }
1082
1083   virtual void accept(ir_visitor *v)
1084   {
1085      v->visit(this);
1086   }
1087
1088   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1089
1090   /**
1091    * Object being dereferenced.
1092    */
1093   ir_variable *var;
1094};
1095
1096
1097class ir_dereference_array : public ir_dereference {
1098public:
1099   ir_dereference_array(ir_rvalue *value, ir_rvalue *array_index);
1100
1101   ir_dereference_array(ir_variable *var, ir_rvalue *array_index);
1102
1103   virtual ir_dereference_array *clone(struct hash_table *) const;
1104
1105   virtual ir_dereference_array *as_dereference_array()
1106   {
1107      return this;
1108   }
1109
1110   /**
1111    * Get the variable that is ultimately referenced by an r-value
1112    */
1113   virtual ir_variable *variable_referenced()
1114   {
1115      return this->array->variable_referenced();
1116   }
1117
1118   virtual void accept(ir_visitor *v)
1119   {
1120      v->visit(this);
1121   }
1122
1123   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1124
1125   ir_rvalue *array;
1126   ir_rvalue *array_index;
1127
1128private:
1129   void set_array(ir_rvalue *value);
1130};
1131
1132
1133class ir_dereference_record : public ir_dereference {
1134public:
1135   ir_dereference_record(ir_rvalue *value, const char *field);
1136
1137   ir_dereference_record(ir_variable *var, const char *field);
1138
1139   virtual ir_dereference_record *clone(struct hash_table *) const;
1140
1141   /**
1142    * Get the variable that is ultimately referenced by an r-value
1143    */
1144   virtual ir_variable *variable_referenced()
1145   {
1146      return this->record->variable_referenced();
1147   }
1148
1149   virtual void accept(ir_visitor *v)
1150   {
1151      v->visit(this);
1152   }
1153
1154   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1155
1156   ir_rvalue *record;
1157   const char *field;
1158};
1159
1160
1161/**
1162 * Data stored in an ir_constant
1163 */
1164union ir_constant_data {
1165      unsigned u[16];
1166      int i[16];
1167      float f[16];
1168      bool b[16];
1169};
1170
1171
1172class ir_constant : public ir_rvalue {
1173public:
1174   ir_constant(const struct glsl_type *type, const ir_constant_data *data);
1175   ir_constant(bool b);
1176   ir_constant(unsigned int u);
1177   ir_constant(int i);
1178   ir_constant(float f);
1179
1180   /**
1181    * Construct an ir_constant from a list of ir_constant values
1182    */
1183   ir_constant(const struct glsl_type *type, exec_list *values);
1184
1185   /**
1186    * Construct an ir_constant from a scalar component of another ir_constant
1187    *
1188    * The new \c ir_constant inherits the type of the component from the
1189    * source constant.
1190    *
1191    * \note
1192    * In the case of a matrix constant, the new constant is a scalar, \b not
1193    * a vector.
1194    */
1195   ir_constant(const ir_constant *c, unsigned i);
1196
1197   virtual ir_constant *clone(struct hash_table *) const;
1198
1199   virtual ir_constant *as_constant()
1200   {
1201      return this;
1202   }
1203
1204   virtual void accept(ir_visitor *v)
1205   {
1206      v->visit(this);
1207   }
1208
1209   virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1210
1211   /**
1212    * Get a particular component of a constant as a specific type
1213    *
1214    * This is useful, for example, to get a value from an integer constant
1215    * as a float or bool.  This appears frequently when constructors are
1216    * called with all constant parameters.
1217    */
1218   /*@{*/
1219   bool get_bool_component(unsigned i) const;
1220   float get_float_component(unsigned i) const;
1221   int get_int_component(unsigned i) const;
1222   unsigned get_uint_component(unsigned i) const;
1223   /*@}*/
1224
1225   ir_constant *get_record_field(const char *name);
1226
1227   /**
1228    * Determine whether a constant has the same value as another constant
1229    */
1230   bool has_value(const ir_constant *) const;
1231
1232   /**
1233    * Value of the constant.
1234    *
1235    * The field used to back the values supplied by the constant is determined
1236    * by the type associated with the \c ir_instruction.  Constants may be
1237    * scalars, vectors, or matrices.
1238    */
1239   union ir_constant_data value;
1240
1241   exec_list components;
1242
1243private:
1244   /**
1245    * Parameterless constructor only used by the clone method
1246    */
1247   ir_constant(void);
1248};
1249
1250void
1251visit_exec_list(exec_list *list, ir_visitor *visitor);
1252
1253void validate_ir_tree(exec_list *instructions);
1254
1255/**
1256 * Make a clone of each IR instruction in a list
1257 *
1258 * \param in   List of IR instructions that are to be cloned
1259 * \param out  List to hold the cloned instructions
1260 */
1261void
1262clone_ir_list(exec_list *out, const exec_list *in);
1263
1264extern void
1265_mesa_glsl_initialize_variables(exec_list *instructions,
1266				struct _mesa_glsl_parse_state *state);
1267
1268extern void
1269_mesa_glsl_initialize_functions(exec_list *instructions,
1270				struct _mesa_glsl_parse_state *state);
1271
1272#endif /* IR_H */
1273