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