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