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