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