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