OSDN Git Service

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