f47813786b508f913316e6b42bc7802693776c76
[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 ir_unop_fract,
532 /*@}*/
533
534 /**
535 * \name Trigonometric operations.
536 */
537 /*@{*/
538 ir_unop_sin,
539 ir_unop_cos,
540 /*@}*/
541
542 /**
543 * \name Partial derivatives.
544 */
545 /*@{*/
546 ir_unop_dFdx,
547 ir_unop_dFdy,
548 /*@}*/
549
550 ir_binop_add,
551 ir_binop_sub,
552 ir_binop_mul,
553 ir_binop_div,
554 ir_binop_mod,
555
556 /**
557 * \name Binary comparison operators
558 */
559 /*@{*/
560 ir_binop_less,
561 ir_binop_greater,
562 ir_binop_lequal,
563 ir_binop_gequal,
564 ir_binop_equal,
565 ir_binop_nequal,
566 /*@}*/
567
568 /**
569 * \name Bit-wise binary operations.
570 */
571 /*@{*/
572 ir_binop_lshift,
573 ir_binop_rshift,
574 ir_binop_bit_and,
575 ir_binop_bit_xor,
576 ir_binop_bit_or,
577 /*@}*/
578
579 ir_binop_logic_and,
580 ir_binop_logic_xor,
581 ir_binop_logic_or,
582
583 ir_binop_dot,
584 ir_binop_min,
585 ir_binop_max,
586
587 ir_binop_pow
588 };
589
590 class ir_expression : public ir_rvalue {
591 public:
592 ir_expression(int op, const struct glsl_type *type,
593 ir_rvalue *, ir_rvalue *);
594
595 virtual ir_instruction *clone(struct hash_table *ht) const;
596
597 static unsigned int get_num_operands(ir_expression_operation);
598 unsigned int get_num_operands() const
599 {
600 return get_num_operands(operation);
601 }
602
603 /**
604 * Return a string representing this expression's operator.
605 */
606 const char *operator_string();
607
608 /**
609 * Do a reverse-lookup to translate the given string into an operator.
610 */
611 static ir_expression_operation get_operator(const char *);
612
613 virtual void accept(ir_visitor *v)
614 {
615 v->visit(this);
616 }
617
618 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
619
620 ir_expression_operation operation;
621 ir_rvalue *operands[2];
622 };
623
624
625 /**
626 * IR instruction representing a function call
627 */
628 class ir_call : public ir_rvalue {
629 public:
630 ir_call(const ir_function_signature *callee, exec_list *actual_parameters)
631 : callee(callee)
632 {
633 assert(callee->return_type != NULL);
634 type = callee->return_type;
635 actual_parameters->move_nodes_to(& this->actual_parameters);
636 }
637
638 virtual ir_instruction *clone(struct hash_table *ht) const;
639
640 virtual ir_call *as_call()
641 {
642 return this;
643 }
644
645 virtual void accept(ir_visitor *v)
646 {
647 v->visit(this);
648 }
649
650 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
651
652 /**
653 * Get a generic ir_call object when an error occurs
654 *
655 * Any allocation will be performed with 'ctx' as talloc owner.
656 */
657 static ir_call *get_error_instruction(void *ctx);
658
659 /**
660 * Get an iterator for the set of acutal parameters
661 */
662 exec_list_iterator iterator()
663 {
664 return actual_parameters.iterator();
665 }
666
667 /**
668 * Get the name of the function being called.
669 */
670 const char *callee_name() const
671 {
672 return callee->function_name();
673 }
674
675 const ir_function_signature *get_callee()
676 {
677 return callee;
678 }
679
680 /**
681 * Generates an inline version of the function before @ir,
682 * returning the return value of the function.
683 */
684 ir_rvalue *generate_inline(ir_instruction *ir);
685
686 private:
687 ir_call()
688 : callee(NULL)
689 {
690 /* empty */
691 }
692
693 const ir_function_signature *callee;
694
695 /* List of ir_rvalue of paramaters passed in this call. */
696 exec_list actual_parameters;
697 };
698
699
700 /**
701 * \name Jump-like IR instructions.
702 *
703 * These include \c break, \c continue, \c return, and \c discard.
704 */
705 /*@{*/
706 class ir_jump : public ir_instruction {
707 protected:
708 ir_jump()
709 {
710 /* empty */
711 }
712 };
713
714 class ir_return : public ir_jump {
715 public:
716 ir_return()
717 : value(NULL)
718 {
719 /* empty */
720 }
721
722 ir_return(ir_rvalue *value)
723 : value(value)
724 {
725 /* empty */
726 }
727
728 virtual ir_instruction *clone(struct hash_table *) const;
729
730 virtual ir_return *as_return()
731 {
732 return this;
733 }
734
735 ir_rvalue *get_value() const
736 {
737 return value;
738 }
739
740 virtual void accept(ir_visitor *v)
741 {
742 v->visit(this);
743 }
744
745 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
746
747 ir_rvalue *value;
748 };
749
750
751 /**
752 * Jump instructions used inside loops
753 *
754 * These include \c break and \c continue. The \c break within a loop is
755 * different from the \c break within a switch-statement.
756 *
757 * \sa ir_switch_jump
758 */
759 class ir_loop_jump : public ir_jump {
760 public:
761 enum jump_mode {
762 jump_break,
763 jump_continue
764 };
765
766 ir_loop_jump(jump_mode mode)
767 {
768 this->mode = mode;
769 this->loop = loop;
770 }
771
772 virtual ir_instruction *clone(struct hash_table *) const;
773
774 virtual void accept(ir_visitor *v)
775 {
776 v->visit(this);
777 }
778
779 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
780
781 bool is_break() const
782 {
783 return mode == jump_break;
784 }
785
786 bool is_continue() const
787 {
788 return mode == jump_continue;
789 }
790
791 /** Mode selector for the jump instruction. */
792 enum jump_mode mode;
793 private:
794 /** Loop containing this break instruction. */
795 ir_loop *loop;
796 };
797
798 /**
799 * IR instruction representing discard statements.
800 */
801 class ir_discard : public ir_jump {
802 public:
803 ir_discard()
804 {
805 this->condition = NULL;
806 }
807
808 ir_discard(ir_rvalue *cond)
809 {
810 this->condition = cond;
811 }
812
813 virtual ir_instruction *clone(struct hash_table *ht) const;
814
815 virtual void accept(ir_visitor *v)
816 {
817 v->visit(this);
818 }
819
820 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
821
822 ir_rvalue *condition;
823 };
824 /*@}*/
825
826
827 /**
828 * Texture sampling opcodes used in ir_texture
829 */
830 enum ir_texture_opcode {
831 ir_tex, /* Regular texture look-up */
832 ir_txb, /* Texture look-up with LOD bias */
833 ir_txl, /* Texture look-up with explicit LOD */
834 ir_txd, /* Texture look-up with partial derivatvies */
835 ir_txf /* Texel fetch with explicit LOD */
836 };
837
838
839 /**
840 * IR instruction to sample a texture
841 *
842 * The specific form of the IR instruction depends on the \c mode value
843 * selected from \c ir_texture_opcodes. In the printed IR, these will
844 * appear as:
845 *
846 * Texel offset
847 * | Projection divisor
848 * | | Shadow comparitor
849 * | | |
850 * v v v
851 * (tex (sampler) (coordinate) (0 0 0) (1) ( ))
852 * (txb (sampler) (coordinate) (0 0 0) (1) ( ) (bias))
853 * (txl (sampler) (coordinate) (0 0 0) (1) ( ) (lod))
854 * (txd (sampler) (coordinate) (0 0 0) (1) ( ) (dPdx dPdy))
855 * (txf (sampler) (coordinate) (0 0 0) (lod))
856 */
857 class ir_texture : public ir_rvalue {
858 public:
859 ir_texture(enum ir_texture_opcode op)
860 : op(op), projector(NULL), shadow_comparitor(NULL)
861 {
862 /* empty */
863 }
864
865 virtual ir_instruction *clone(struct hash_table *) const;
866
867 virtual void accept(ir_visitor *v)
868 {
869 v->visit(this);
870 }
871
872 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
873
874 /**
875 * Return a string representing the ir_texture_opcode.
876 */
877 const char *opcode_string();
878
879 /** Set the sampler and infer the type. */
880 void set_sampler(ir_dereference *sampler);
881
882 /**
883 * Do a reverse-lookup to translate a string into an ir_texture_opcode.
884 */
885 static ir_texture_opcode get_opcode(const char *);
886
887 enum ir_texture_opcode op;
888
889 /** Sampler to use for the texture access. */
890 ir_dereference *sampler;
891
892 /** Texture coordinate to sample */
893 ir_rvalue *coordinate;
894
895 /**
896 * Value used for projective divide.
897 *
898 * If there is no projective divide (the common case), this will be
899 * \c NULL. Optimization passes should check for this to point to a constant
900 * of 1.0 and replace that with \c NULL.
901 */
902 ir_rvalue *projector;
903
904 /**
905 * Coordinate used for comparison on shadow look-ups.
906 *
907 * If there is no shadow comparison, this will be \c NULL. For the
908 * \c ir_txf opcode, this *must* be \c NULL.
909 */
910 ir_rvalue *shadow_comparitor;
911
912 /** Explicit texel offsets. */
913 signed char offsets[3];
914
915 union {
916 ir_rvalue *lod; /**< Floating point LOD */
917 ir_rvalue *bias; /**< Floating point LOD bias */
918 struct {
919 ir_rvalue *dPdx; /**< Partial derivative of coordinate wrt X */
920 ir_rvalue *dPdy; /**< Partial derivative of coordinate wrt Y */
921 } grad;
922 } lod_info;
923 };
924
925
926 struct ir_swizzle_mask {
927 unsigned x:2;
928 unsigned y:2;
929 unsigned z:2;
930 unsigned w:2;
931
932 /**
933 * Number of components in the swizzle.
934 */
935 unsigned num_components:3;
936
937 /**
938 * Does the swizzle contain duplicate components?
939 *
940 * L-value swizzles cannot contain duplicate components.
941 */
942 unsigned has_duplicates:1;
943 };
944
945
946 class ir_swizzle : public ir_rvalue {
947 public:
948 ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
949 unsigned count);
950
951 ir_swizzle(ir_rvalue *val, const unsigned *components, unsigned count);
952
953 ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask);
954
955 virtual ir_instruction *clone(struct hash_table *) const;
956
957 virtual ir_swizzle *as_swizzle()
958 {
959 return this;
960 }
961
962 /**
963 * Construct an ir_swizzle from the textual representation. Can fail.
964 */
965 static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
966
967 virtual void accept(ir_visitor *v)
968 {
969 v->visit(this);
970 }
971
972 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
973
974 bool is_lvalue()
975 {
976 return val->is_lvalue() && !mask.has_duplicates;
977 }
978
979 /**
980 * Get the variable that is ultimately referenced by an r-value
981 */
982 virtual ir_variable *variable_referenced();
983
984 ir_rvalue *val;
985 ir_swizzle_mask mask;
986
987 private:
988 /**
989 * Initialize the mask component of a swizzle
990 *
991 * This is used by the \c ir_swizzle constructors.
992 */
993 void init_mask(const unsigned *components, unsigned count);
994 };
995
996
997 class ir_dereference : public ir_rvalue {
998 public:
999 virtual ir_dereference *as_dereference()
1000 {
1001 return this;
1002 }
1003
1004 bool is_lvalue();
1005
1006 /**
1007 * Get the variable that is ultimately referenced by an r-value
1008 */
1009 virtual ir_variable *variable_referenced() = 0;
1010 };
1011
1012
1013 class ir_dereference_variable : public ir_dereference {
1014 public:
1015 ir_dereference_variable(ir_variable *var);
1016
1017 virtual ir_instruction *clone(struct hash_table *) const;
1018
1019 virtual ir_dereference_variable *as_dereference_variable()
1020 {
1021 return this;
1022 }
1023
1024 /**
1025 * Get the variable that is ultimately referenced by an r-value
1026 */
1027 virtual ir_variable *variable_referenced()
1028 {
1029 return this->var;
1030 }
1031
1032 virtual ir_variable *whole_variable_referenced()
1033 {
1034 /* ir_dereference_variable objects always dereference the entire
1035 * variable. However, if this dereference is dereferenced by anything
1036 * else, the complete deferefernce chain is not a whole-variable
1037 * dereference. This method should only be called on the top most
1038 * ir_rvalue in a dereference chain.
1039 */
1040 return this->var;
1041 }
1042
1043 virtual void accept(ir_visitor *v)
1044 {
1045 v->visit(this);
1046 }
1047
1048 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1049
1050 /**
1051 * Object being dereferenced.
1052 */
1053 ir_variable *var;
1054 };
1055
1056
1057 class ir_dereference_array : public ir_dereference {
1058 public:
1059 ir_dereference_array(ir_rvalue *value, ir_rvalue *array_index);
1060
1061 ir_dereference_array(ir_variable *var, ir_rvalue *array_index);
1062
1063 virtual ir_instruction *clone(struct hash_table *) const;
1064
1065 virtual ir_dereference_array *as_dereference_array()
1066 {
1067 return this;
1068 }
1069
1070 /**
1071 * Get the variable that is ultimately referenced by an r-value
1072 */
1073 virtual ir_variable *variable_referenced()
1074 {
1075 return this->array->variable_referenced();
1076 }
1077
1078 virtual void accept(ir_visitor *v)
1079 {
1080 v->visit(this);
1081 }
1082
1083 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1084
1085 ir_rvalue *array;
1086 ir_rvalue *array_index;
1087
1088 private:
1089 void set_array(ir_rvalue *value);
1090 };
1091
1092
1093 class ir_dereference_record : public ir_dereference {
1094 public:
1095 ir_dereference_record(ir_rvalue *value, const char *field);
1096
1097 ir_dereference_record(ir_variable *var, const char *field);
1098
1099 virtual ir_instruction *clone(struct hash_table *) const;
1100
1101 /**
1102 * Get the variable that is ultimately referenced by an r-value
1103 */
1104 virtual ir_variable *variable_referenced()
1105 {
1106 return this->record->variable_referenced();
1107 }
1108
1109 virtual void accept(ir_visitor *v)
1110 {
1111 v->visit(this);
1112 }
1113
1114 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1115
1116 ir_rvalue *record;
1117 const char *field;
1118 };
1119
1120
1121 /**
1122 * Data stored in an ir_constant
1123 */
1124 union ir_constant_data {
1125 unsigned u[16];
1126 int i[16];
1127 float f[16];
1128 bool b[16];
1129 };
1130
1131
1132 class ir_constant : public ir_rvalue {
1133 public:
1134 ir_constant(const struct glsl_type *type, const ir_constant_data *data);
1135 ir_constant(bool b);
1136 ir_constant(unsigned int u);
1137 ir_constant(int i);
1138 ir_constant(float f);
1139
1140 /**
1141 * Construct an ir_constant from a list of ir_constant values
1142 */
1143 ir_constant(const struct glsl_type *type, exec_list *values);
1144
1145 /**
1146 * Construct an ir_constant from a scalar component of another ir_constant
1147 *
1148 * The new \c ir_constant inherits the type of the component from the
1149 * source constant.
1150 *
1151 * \note
1152 * In the case of a matrix constant, the new constant is a scalar, \b not
1153 * a vector.
1154 */
1155 ir_constant(const ir_constant *c, unsigned i);
1156
1157 virtual ir_instruction *clone(struct hash_table *) const;
1158
1159 virtual ir_constant *as_constant()
1160 {
1161 return this;
1162 }
1163
1164 virtual void accept(ir_visitor *v)
1165 {
1166 v->visit(this);
1167 }
1168
1169 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1170
1171 /**
1172 * Get a particular component of a constant as a specific type
1173 *
1174 * This is useful, for example, to get a value from an integer constant
1175 * as a float or bool. This appears frequently when constructors are
1176 * called with all constant parameters.
1177 */
1178 /*@{*/
1179 bool get_bool_component(unsigned i) const;
1180 float get_float_component(unsigned i) const;
1181 int get_int_component(unsigned i) const;
1182 unsigned get_uint_component(unsigned i) const;
1183 /*@}*/
1184
1185 ir_constant *get_record_field(const char *name);
1186
1187 /**
1188 * Determine whether a constant has the same value as another constant
1189 */
1190 bool has_value(const ir_constant *) const;
1191
1192 /**
1193 * Value of the constant.
1194 *
1195 * The field used to back the values supplied by the constant is determined
1196 * by the type associated with the \c ir_instruction. Constants may be
1197 * scalars, vectors, or matrices.
1198 */
1199 union ir_constant_data value;
1200
1201 exec_list components;
1202
1203 private:
1204 /**
1205 * Parameterless constructor only used by the clone method
1206 */
1207 ir_constant(void);
1208 };
1209
1210 void
1211 visit_exec_list(exec_list *list, ir_visitor *visitor);
1212
1213 void validate_ir_tree(exec_list *instructions);
1214
1215 extern void
1216 _mesa_glsl_initialize_variables(exec_list *instructions,
1217 struct _mesa_glsl_parse_state *state);
1218
1219 extern void
1220 _mesa_glsl_initialize_functions(exec_list *instructions,
1221 struct _mesa_glsl_parse_state *state);
1222
1223 #endif /* IR_H */