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