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