Refactor IR function representation.
[mesa.git] / 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 "list.h"
30 #include "ir_visitor.h"
31
32 struct ir_program {
33 void *bong_hits;
34 };
35
36 /**
37 * Base class of all IR instructions
38 */
39 class ir_instruction : public exec_node {
40 public:
41 const struct glsl_type *type;
42
43 class ir_constant *constant_expression_value();
44 virtual void accept(ir_visitor *) = 0;
45
46 /**
47 * \name IR instruction downcast functions
48 *
49 * These functions either cast the object to a derived class or return
50 * \c NULL if the object's type does not match the specified derived class.
51 * Additional downcast functions will be added as needed.
52 */
53 /*@{*/
54 virtual class ir_variable * as_variable() { return NULL; }
55 virtual class ir_dereference * as_dereference() { return NULL; }
56 virtual class ir_rvalue * as_rvalue() { return NULL; }
57 virtual class ir_label * as_label() { return NULL; }
58 virtual class ir_loop * as_loop() { return NULL; }
59 virtual class ir_assignment * as_assignment() { return NULL; }
60 virtual class ir_call * as_call() { return NULL; }
61 virtual class ir_return * as_return() { return NULL; }
62 virtual class ir_if * as_if() { return NULL; }
63 virtual class ir_swizzle * as_swizzle() { return NULL; }
64 /*@}*/
65
66 protected:
67 ir_instruction()
68 {
69 /* empty */
70 }
71 };
72
73
74 class ir_rvalue : public ir_instruction {
75 public:
76 virtual ir_rvalue * as_rvalue()
77 {
78 return this;
79 }
80
81 virtual bool is_lvalue()
82 {
83 return false;
84 }
85
86 protected:
87 ir_rvalue()
88 {
89 /* empty */
90 }
91 };
92
93
94 enum ir_variable_mode {
95 ir_var_auto = 0,
96 ir_var_uniform,
97 ir_var_in,
98 ir_var_out,
99 ir_var_inout
100 };
101
102 enum ir_varaible_interpolation {
103 ir_var_smooth = 0,
104 ir_var_flat,
105 ir_var_noperspective
106 };
107
108
109 class ir_variable : public ir_instruction {
110 public:
111 ir_variable(const struct glsl_type *, const char *);
112
113 virtual ir_variable *as_variable()
114 {
115 return this;
116 }
117
118 virtual void accept(ir_visitor *v)
119 {
120 v->visit(this);
121 }
122
123 /**
124 * Duplicate an IR variable
125 *
126 * \note
127 * This will probably be made \c virtual and moved to the base class
128 * eventually.
129 */
130 ir_variable *clone() const
131 {
132 ir_variable *var = new ir_variable(type, name);
133
134 var->max_array_access = this->max_array_access;
135 var->read_only = this->read_only;
136 var->centroid = this->centroid;
137 var->invariant = this->invariant;
138 var->mode = this->mode;
139 var->interpolation = this->interpolation;
140
141 return var;
142 }
143
144 const char *name;
145
146 /**
147 * Highest element accessed with a constant expression array index
148 *
149 * Not used for non-array variables.
150 */
151 unsigned max_array_access;
152
153 unsigned read_only:1;
154 unsigned centroid:1;
155 unsigned invariant:1;
156 /** If the variable is initialized outside of the scope of the shader */
157 unsigned shader_in:1;
158 /**
159 * If the variable value is later used outside of the scope of the shader.
160 */
161 unsigned shader_out:1;
162
163 unsigned mode:3;
164 unsigned interpolation:2;
165
166 /**
167 * Flag that the whole array is assignable
168 *
169 * In GLSL 1.20 and later whole arrays are assignable (and comparable for
170 * equality). This flag enables this behavior.
171 */
172 unsigned array_lvalue:1;
173
174 /**
175 * Emit a warning if this variable is accessed.
176 */
177 const char *warn_extension;
178
179 /**
180 * Value assigned in the initializer of a variable declared "const"
181 */
182 ir_constant *constant_value;
183 };
184
185
186 class ir_label : public ir_instruction {
187 public:
188 ir_label(const char *label, ir_function_signature *signature);
189
190 virtual ir_label *as_label()
191 {
192 return this;
193 }
194
195 virtual void accept(ir_visitor *v)
196 {
197 v->visit(this);
198 }
199
200 const char *label;
201
202 ir_function_signature *signature;
203 };
204
205
206 /*@{*/
207 /**
208 * The representation of a function instance; may be the full definition or
209 * simply a prototype.
210 */
211 class ir_function_signature : public ir_instruction {
212 /* An ir_function_signature will be part of the list of signatures in
213 * an ir_function.
214 */
215 public:
216 ir_function_signature(const glsl_type *return_type);
217
218 virtual void accept(ir_visitor *v)
219 {
220 v->visit(this);
221 }
222
223 /**
224 * Get the name of the function for which this is a signature
225 */
226 const char *function_name() const;
227
228 /**
229 * Function return type.
230 *
231 * \note This discards the optional precision qualifier.
232 */
233 const struct glsl_type *return_type;
234
235 /**
236 * List of ir_variable of function parameters.
237 *
238 * This represents the storage. The paramaters passed in a particular
239 * call will be in ir_call::actual_paramaters.
240 */
241 struct exec_list parameters;
242
243 /** Whether or not this function has a body (which may be empty). */
244 unsigned is_defined:1;
245
246 /** Body of instructions in the function. */
247 struct exec_list body;
248
249 private:
250 /** Function of which this signature is one overload. */
251 class ir_function *function;
252
253 friend class ir_function;
254 };
255
256
257 /**
258 * Header for tracking multiple overloaded functions with the same name.
259 * Contains a list of ir_function_signatures representing each of the
260 * actual functions.
261 */
262 class ir_function : public ir_instruction {
263 public:
264 ir_function(const char *name);
265
266 virtual void accept(ir_visitor *v)
267 {
268 v->visit(this);
269 }
270
271 void add_signature(ir_function_signature *sig)
272 {
273 sig->function = this;
274 signatures.push_tail(sig);
275 }
276
277 /**
278 * Get an iterator for the set of function signatures
279 */
280 exec_list_iterator iterator()
281 {
282 return signatures.iterator();
283 }
284
285 /**
286 * Find a signature that matches a set of actual parameters.
287 */
288 const ir_function_signature *matching_signature(exec_list *actual_param);
289
290 /**
291 * Name of the function.
292 */
293 const char *name;
294
295 private:
296 /**
297 * List of ir_function_signature for each overloaded function with this name.
298 */
299 struct exec_list signatures;
300 };
301
302 inline const char *ir_function_signature::function_name() const
303 {
304 return function->name;
305 }
306 /*@}*/
307
308
309 /**
310 * IR instruction representing high-level if-statements
311 */
312 class ir_if : public ir_instruction {
313 public:
314 ir_if(ir_rvalue *condition)
315 : condition(condition)
316 {
317 /* empty */
318 }
319
320 virtual ir_if *as_if()
321 {
322 return this;
323 }
324
325 virtual void accept(ir_visitor *v)
326 {
327 v->visit(this);
328 }
329
330 ir_rvalue *condition;
331 /** List of ir_instruction for the body of the then branch */
332 exec_list then_instructions;
333 /** List of ir_instruction for the body of the else branch */
334 exec_list else_instructions;
335 };
336
337
338 /**
339 * IR instruction representing a high-level loop structure.
340 */
341 class ir_loop : public ir_instruction {
342 public:
343 ir_loop() : from(NULL), to(NULL), increment(NULL), counter(NULL)
344 {
345 /* empty */
346 }
347
348 virtual void accept(ir_visitor *v)
349 {
350 v->visit(this);
351 }
352
353 virtual ir_loop *as_loop()
354 {
355 return this;
356 }
357
358 /**
359 * Get an iterator for the instructions of the loop body
360 */
361 exec_list_iterator iterator()
362 {
363 return body_instructions.iterator();
364 }
365
366 /** List of ir_instruction that make up the body of the loop. */
367 exec_list body_instructions;
368
369 /**
370 * \name Loop counter and controls
371 */
372 /*@{*/
373 ir_rvalue *from;
374 ir_rvalue *to;
375 ir_rvalue *increment;
376 ir_variable *counter;
377 /*@}*/
378 };
379
380
381 class ir_assignment : public ir_rvalue {
382 public:
383 ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition);
384
385 virtual void accept(ir_visitor *v)
386 {
387 v->visit(this);
388 }
389
390 virtual ir_assignment * as_assignment()
391 {
392 return this;
393 }
394
395 /**
396 * Left-hand side of the assignment.
397 */
398 ir_rvalue *lhs;
399
400 /**
401 * Value being assigned
402 */
403 ir_rvalue *rhs;
404
405 /**
406 * Optional condition for the assignment.
407 */
408 ir_rvalue *condition;
409 };
410
411 /* Update ir_expression::num_operands() and ir_print_visitor.cpp when
412 * updating this list.
413 */
414 enum ir_expression_operation {
415 ir_unop_bit_not,
416 ir_unop_logic_not,
417 ir_unop_neg,
418 ir_unop_abs,
419 ir_unop_rcp,
420 ir_unop_rsq,
421 ir_unop_sqrt,
422 ir_unop_exp,
423 ir_unop_log,
424 ir_unop_exp2,
425 ir_unop_log2,
426 ir_unop_f2i, /**< Float-to-integer conversion. */
427 ir_unop_i2f, /**< Integer-to-float conversion. */
428 ir_unop_f2b, /**< Float-to-boolean conversion */
429 ir_unop_b2f, /**< Boolean-to-float conversion */
430 ir_unop_i2b, /**< int-to-boolean conversion */
431 ir_unop_b2i, /**< Boolean-to-int conversion */
432 ir_unop_u2f, /**< Unsigned-to-float conversion. */
433
434 /**
435 * \name Unary floating-point rounding operations.
436 */
437 /*@{*/
438 ir_unop_trunc,
439 ir_unop_ceil,
440 ir_unop_floor,
441 /*@}*/
442
443 ir_binop_add,
444 ir_binop_sub,
445 ir_binop_mul,
446 ir_binop_div,
447 ir_binop_mod,
448
449 /**
450 * \name Binary comparison operators
451 */
452 /*@{*/
453 ir_binop_less,
454 ir_binop_greater,
455 ir_binop_lequal,
456 ir_binop_gequal,
457 ir_binop_equal,
458 ir_binop_nequal,
459 /*@}*/
460
461 /**
462 * \name Bit-wise binary operations.
463 */
464 /*@{*/
465 ir_binop_lshift,
466 ir_binop_rshift,
467 ir_binop_bit_and,
468 ir_binop_bit_xor,
469 ir_binop_bit_or,
470 /*@}*/
471
472 ir_binop_logic_and,
473 ir_binop_logic_xor,
474 ir_binop_logic_or,
475
476 ir_binop_dot,
477 ir_binop_min,
478 ir_binop_max,
479
480 ir_binop_pow
481 };
482
483 class ir_expression : public ir_rvalue {
484 public:
485 ir_expression(int op, const struct glsl_type *type,
486 ir_rvalue *, ir_rvalue *);
487
488 unsigned int get_num_operands(void);
489
490 virtual void accept(ir_visitor *v)
491 {
492 v->visit(this);
493 }
494
495 ir_expression *clone();
496
497 ir_expression_operation operation;
498 ir_rvalue *operands[2];
499 };
500
501
502 /**
503 * IR instruction representing a function call
504 */
505 class ir_call : public ir_rvalue {
506 public:
507 ir_call(const ir_function_signature *callee, exec_list *actual_parameters)
508 : callee(callee)
509 {
510 assert(callee->return_type != NULL);
511 type = callee->return_type;
512 actual_parameters->move_nodes_to(& this->actual_parameters);
513 }
514
515 virtual ir_call *as_call()
516 {
517 return this;
518 }
519
520 virtual void accept(ir_visitor *v)
521 {
522 v->visit(this);
523 }
524
525 /**
526 * Get a generic ir_call object when an error occurs
527 */
528 static ir_call *get_error_instruction();
529
530 /**
531 * Get an iterator for the set of acutal parameters
532 */
533 exec_list_iterator iterator()
534 {
535 return actual_parameters.iterator();
536 }
537
538 /**
539 * Get the name of the function being called.
540 */
541 const char *callee_name() const
542 {
543 return callee->function_name();
544 }
545
546 const ir_function_signature *get_callee()
547 {
548 return callee;
549 }
550
551 /**
552 * Generates an inline version of the function before @ir,
553 * returning the return value of the function.
554 */
555 ir_rvalue *generate_inline(ir_instruction *ir);
556
557 private:
558 ir_call()
559 : callee(NULL)
560 {
561 /* empty */
562 }
563
564 const ir_function_signature *callee;
565
566 /* List of ir_rvalue of paramaters passed in this call. */
567 exec_list actual_parameters;
568 };
569
570
571 /**
572 * \name Jump-like IR instructions.
573 *
574 * These include \c break, \c continue, \c return, and \c discard.
575 */
576 /*@{*/
577 class ir_jump : public ir_instruction {
578 protected:
579 ir_jump()
580 {
581 /* empty */
582 }
583 };
584
585 class ir_return : public ir_jump {
586 public:
587 ir_return()
588 : value(NULL)
589 {
590 /* empty */
591 }
592
593 ir_return(ir_rvalue *value)
594 : value(value)
595 {
596 /* empty */
597 }
598
599 virtual ir_return *as_return()
600 {
601 return this;
602 }
603
604 ir_rvalue *get_value() const
605 {
606 return value;
607 }
608
609 virtual void accept(ir_visitor *v)
610 {
611 v->visit(this);
612 }
613
614 private:
615 ir_rvalue *value;
616 };
617
618
619 /**
620 * Jump instructions used inside loops
621 *
622 * These include \c break and \c continue. The \c break within a loop is
623 * different from the \c break within a switch-statement.
624 *
625 * \sa ir_switch_jump
626 */
627 class ir_loop_jump : public ir_jump {
628 public:
629 enum jump_mode {
630 jump_break,
631 jump_continue
632 };
633
634 ir_loop_jump(ir_loop *loop, jump_mode mode)
635 : loop(loop), mode(mode)
636 {
637 /* empty */
638 }
639
640 virtual void accept(ir_visitor *v)
641 {
642 v->visit(this);
643 }
644
645 bool is_break() const
646 {
647 return mode == jump_break;
648 }
649
650 bool is_continue() const
651 {
652 return mode == jump_continue;
653 }
654
655 private:
656 /** Loop containing this break instruction. */
657 ir_loop *loop;
658
659 /** Mode selector for the jump instruction. */
660 enum jump_mode mode;
661 };
662 /*@}*/
663
664
665 struct ir_swizzle_mask {
666 unsigned x:2;
667 unsigned y:2;
668 unsigned z:2;
669 unsigned w:2;
670
671 /**
672 * Number of components in the swizzle.
673 */
674 unsigned num_components:3;
675
676 /**
677 * Does the swizzle contain duplicate components?
678 *
679 * L-value swizzles cannot contain duplicate components.
680 */
681 unsigned has_duplicates:1;
682 };
683
684
685 class ir_swizzle : public ir_rvalue {
686 public:
687 ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
688 unsigned count);
689 ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask)
690 : val(val), mask(mask)
691 {
692 /* empty */
693 }
694
695 virtual ir_swizzle *as_swizzle()
696 {
697 return this;
698 }
699
700 ir_swizzle *clone()
701 {
702 return new ir_swizzle(this->val, this->mask);
703 }
704
705 /**
706 * Construct an ir_swizzle from the textual representation. Can fail.
707 */
708 static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
709
710 virtual void accept(ir_visitor *v)
711 {
712 v->visit(this);
713 }
714
715 bool is_lvalue()
716 {
717 return val->is_lvalue() && !mask.has_duplicates;
718 }
719
720 ir_rvalue *val;
721 ir_swizzle_mask mask;
722 };
723
724
725 class ir_dereference : public ir_rvalue {
726 public:
727 ir_dereference(struct ir_instruction *);
728
729 ir_dereference(ir_instruction *variable, ir_rvalue *array_index);
730
731 virtual ir_dereference *as_dereference()
732 {
733 return this;
734 }
735
736 virtual void accept(ir_visitor *v)
737 {
738 v->visit(this);
739 }
740
741 bool is_lvalue();
742
743 enum {
744 ir_reference_variable,
745 ir_reference_array,
746 ir_reference_record
747 } mode;
748
749 /**
750 * Object being dereferenced.
751 *
752 * Must be either an \c ir_variable or an \c ir_rvalue.
753 */
754 ir_instruction *var;
755
756 union {
757 ir_rvalue *array_index;
758 const char *field;
759 } selector;
760 };
761
762
763 class ir_constant : public ir_rvalue {
764 public:
765 ir_constant(const struct glsl_type *type, const void *data);
766 ir_constant(bool b);
767 ir_constant(unsigned int u);
768 ir_constant(int i);
769 ir_constant(float f);
770
771 virtual void accept(ir_visitor *v)
772 {
773 v->visit(this);
774 }
775
776 ir_constant *clone()
777 {
778 return new ir_constant(this->type, &this->value);
779 }
780
781 /**
782 * Value of the constant.
783 *
784 * The field used to back the values supplied by the constant is determined
785 * by the type associated with the \c ir_instruction. Constants may be
786 * scalars, vectors, or matrices.
787 */
788 union {
789 unsigned u[16];
790 int i[16];
791 float f[16];
792 bool b[16];
793 } value;
794 };
795
796 void
797 visit_exec_list(exec_list *list, ir_visitor *visitor);
798
799 extern void
800 _mesa_glsl_initialize_variables(exec_list *instructions,
801 struct _mesa_glsl_parse_state *state);
802
803 extern void
804 _mesa_glsl_initialize_functions(exec_list *instructions,
805 struct _mesa_glsl_parse_state *state);
806
807 #endif /* IR_H */