OSDN Git Service

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