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