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