OSDN Git Service

builtins: Add the mix(gentype, gentype, float) variant.
[android-x86/external-mesa.git] / ast.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright © 2009 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 AST_H
27 #define AST_H
28
29 #include "list.h"
30 #include "glsl_parser_extras.h"
31
32 struct ir_instruction;
33 struct _mesa_glsl_parse_state;
34
35 struct YYLTYPE;
36
37 class ast_node {
38 public:
39    virtual ~ast_node();
40    virtual void print(void) const;
41    virtual ir_rvalue *hir(exec_list *instructions,
42                           struct _mesa_glsl_parse_state *state);
43
44    /**
45     * Retrieve the source location of an AST node
46     *
47     * This function is primarily used to get the source position of an AST node
48     * into a form that can be passed to \c _mesa_glsl_error.
49     *
50     * \sa _mesa_glsl_error, ast_node::set_location
51     */
52    struct YYLTYPE get_location(void) const
53    {
54       struct YYLTYPE locp;
55
56       locp.source = this->location.source;
57       locp.first_line = this->location.line;
58       locp.first_column = this->location.column;
59       locp.last_line = locp.first_line;
60       locp.last_column = locp.first_column;
61
62       return locp;
63    }
64
65    /**
66     * Set the source location of an AST node from a parser location
67     *
68     * \sa ast_node::get_location
69     */
70    void set_location(const struct YYLTYPE &locp)
71    {
72       this->location.source = locp.source;
73       this->location.line = locp.first_line;
74       this->location.column = locp.first_column;
75    }
76
77    struct {
78       unsigned source;
79       unsigned line;
80       unsigned column;
81    } location;
82
83    exec_node link;
84
85 protected:
86    ast_node(void);
87 };
88
89
90 enum ast_operators {
91    ast_assign,
92    ast_plus,        /**< Unary + operator. */
93    ast_neg,
94    ast_add,
95    ast_sub,
96    ast_mul,
97    ast_div,
98    ast_mod,
99    ast_lshift,
100    ast_rshift,
101    ast_less,
102    ast_greater,
103    ast_lequal,
104    ast_gequal,
105    ast_equal,
106    ast_nequal,
107    ast_bit_and,
108    ast_bit_xor,
109    ast_bit_or,
110    ast_bit_not,
111    ast_logic_and,
112    ast_logic_xor,
113    ast_logic_or,
114    ast_logic_not,
115
116    ast_mul_assign,
117    ast_div_assign,
118    ast_mod_assign,
119    ast_add_assign,
120    ast_sub_assign,
121    ast_ls_assign,
122    ast_rs_assign,
123    ast_and_assign,
124    ast_xor_assign,
125    ast_or_assign,
126
127    ast_conditional,
128
129    ast_pre_inc,
130    ast_pre_dec,
131    ast_post_inc,
132    ast_post_dec,
133    ast_field_selection,
134    ast_array_index,
135
136    ast_function_call,
137
138    ast_identifier,
139    ast_int_constant,
140    ast_uint_constant,
141    ast_float_constant,
142    ast_bool_constant,
143
144    ast_sequence
145 };
146
147 class ast_expression : public ast_node {
148 public:
149    ast_expression(int oper, ast_expression *,
150                   ast_expression *, ast_expression *);
151
152    ast_expression(const char *identifier) :
153       oper(ast_identifier)
154    {
155       subexpressions[0] = NULL;
156       subexpressions[1] = NULL;
157       subexpressions[2] = NULL;
158       primary_expression.identifier = (char *) identifier;
159    }
160
161    static const char *operator_string(enum ast_operators op);
162
163    virtual ir_rvalue *hir(exec_list *instructions,
164                           struct _mesa_glsl_parse_state *state);
165
166    virtual void print(void) const;
167
168    enum ast_operators oper;
169
170    ast_expression *subexpressions[3];
171
172    union {
173       char *identifier;
174       int int_constant;
175       float float_constant;
176       unsigned uint_constant;
177       int bool_constant;
178    } primary_expression;
179
180
181    /**
182     * List of expressions for an \c ast_sequence or parameters for an
183     * \c ast_function_call
184     */
185    exec_list expressions;
186 };
187
188 class ast_expression_bin : public ast_expression {
189 public:
190    ast_expression_bin(int oper, ast_expression *, ast_expression *);
191
192    virtual void print(void) const;
193 };
194
195 /**
196  * Subclass of expressions for function calls
197  */
198 class ast_function_expression : public ast_expression {
199 public:
200    ast_function_expression(ast_expression *callee)
201       : ast_expression(ast_function_call, callee,
202                        NULL, NULL),
203         cons(false)
204    {
205       /* empty */
206    }
207
208    ast_function_expression(class ast_type_specifier *type)
209       : ast_expression(ast_function_call, (ast_expression *) type,
210                        NULL, NULL),
211         cons(true)
212    {
213       /* empty */
214    }
215
216    bool is_constructor() const
217    {
218       return cons;
219    }
220
221    virtual ir_rvalue *hir(exec_list *instructions,
222                           struct _mesa_glsl_parse_state *state);
223
224 private:
225    /**
226     * Is this function call actually a constructor?
227     */
228    bool cons;
229 };
230
231
232 /**
233  * Number of possible operators for an ast_expression
234  *
235  * This is done as a define instead of as an additional value in the enum so
236  * that the compiler won't generate spurious messages like "warning:
237  * enumeration value ‘ast_num_operators’ not handled in switch"
238  */
239 #define AST_NUM_OPERATORS (ast_sequence + 1)
240
241
242 class ast_compound_statement : public ast_node {
243 public:
244    ast_compound_statement(int new_scope, ast_node *statements);
245    virtual void print(void) const;
246
247    virtual ir_rvalue *hir(exec_list *instructions,
248                           struct _mesa_glsl_parse_state *state);
249
250    int new_scope;
251    exec_list statements;
252 };
253
254 class ast_declaration : public ast_node {
255 public:
256    ast_declaration(char *identifier, int is_array, ast_expression *array_size,
257                    ast_expression *initializer);
258    virtual void print(void) const;
259
260    char *identifier;
261    
262    int is_array;
263    ast_expression *array_size;
264
265    ast_expression *initializer;
266 };
267
268
269 enum {
270    ast_precision_high = 0, /**< Default precision. */
271    ast_precision_medium,
272    ast_precision_low
273 };
274
275 struct ast_type_qualifier {
276    unsigned invariant:1;
277    unsigned constant:1;
278    unsigned attribute:1;
279    unsigned varying:1;
280    unsigned in:1;
281    unsigned out:1;
282    unsigned centroid:1;
283    unsigned uniform:1;
284    unsigned smooth:1;
285    unsigned flat:1;
286    unsigned noperspective:1;
287 };
288
289 class ast_struct_specifier : public ast_node {
290 public:
291    ast_struct_specifier(char *identifier, ast_node *declarator_list);
292    virtual void print(void) const;
293
294    virtual ir_rvalue *hir(exec_list *instructions,
295                           struct _mesa_glsl_parse_state *state);
296
297    char *name;
298    exec_list declarations;
299 };
300
301
302 enum ast_types {
303    ast_void,
304    ast_float,
305    ast_int,
306    ast_uint,
307    ast_bool,
308    ast_vec2,
309    ast_vec3,
310    ast_vec4,
311    ast_bvec2,
312    ast_bvec3,
313    ast_bvec4,
314    ast_ivec2,
315    ast_ivec3,
316    ast_ivec4,
317    ast_uvec2,
318    ast_uvec3,
319    ast_uvec4,
320    ast_mat2,
321    ast_mat2x3,
322    ast_mat2x4,
323    ast_mat3x2,
324    ast_mat3,
325    ast_mat3x4,
326    ast_mat4x2,
327    ast_mat4x3,
328    ast_mat4,
329    ast_sampler1d,
330    ast_sampler2d,
331    ast_sampler2drect,
332    ast_sampler3d,
333    ast_samplercube,
334    ast_sampler1dshadow,
335    ast_sampler2dshadow,
336    ast_sampler2drectshadow,
337    ast_samplercubeshadow,
338    ast_sampler1darray,
339    ast_sampler2darray,
340    ast_sampler1darrayshadow,
341    ast_sampler2darrayshadow,
342    ast_isampler1d,
343    ast_isampler2d,
344    ast_isampler3d,
345    ast_isamplercube,
346    ast_isampler1darray,
347    ast_isampler2darray,
348    ast_usampler1d,
349    ast_usampler2d,
350    ast_usampler3d,
351    ast_usamplercube,
352    ast_usampler1darray,
353    ast_usampler2darray,
354
355    ast_struct,
356    ast_type_name
357 };
358
359
360 class ast_type_specifier : public ast_node {
361 public:
362    ast_type_specifier(int specifier);
363
364    /** Construct a type specifier from a type name */
365    ast_type_specifier(const char *name) 
366       : type_specifier(ast_type_name), type_name(name), structure(NULL),
367         is_array(false), array_size(NULL), precision(ast_precision_high)
368    {
369       /* empty */
370    }
371
372    /** Construct a type specifier from a structure definition */
373    ast_type_specifier(ast_struct_specifier *s)
374       : type_specifier(ast_struct), type_name(s->name), structure(s),
375         is_array(false), array_size(NULL), precision(ast_precision_high)
376    {
377       /* empty */
378    }
379
380    const struct glsl_type *glsl_type(const char **name,
381                                      struct _mesa_glsl_parse_state *state)
382       const;
383
384    virtual void print(void) const;
385
386    ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
387
388    enum ast_types type_specifier;
389
390    const char *type_name;
391    ast_struct_specifier *structure;
392
393    int is_array;
394    ast_expression *array_size;
395
396    unsigned precision:2;
397 };
398
399
400 class ast_fully_specified_type : public ast_node {
401 public:
402    virtual void print(void) const;
403
404    ast_type_qualifier qualifier;
405    ast_type_specifier *specifier;
406 };
407
408
409 class ast_declarator_list : public ast_node {
410 public:
411    ast_declarator_list(ast_fully_specified_type *);
412    virtual void print(void) const;
413
414    virtual ir_rvalue *hir(exec_list *instructions,
415                           struct _mesa_glsl_parse_state *state);
416
417    ast_fully_specified_type *type;
418    exec_list declarations;
419
420    /**
421     * Special flag for vertex shader "invariant" declarations.
422     *
423     * Vertex shaders can contain "invariant" variable redeclarations that do
424     * not include a type.  For example, "invariant gl_Position;".  This flag
425     * is used to note these cases when no type is specified.
426     */
427    int invariant;
428 };
429
430
431 class ast_parameter_declarator : public ast_node {
432 public:
433    virtual void print(void) const;
434
435    virtual ir_rvalue *hir(exec_list *instructions,
436                           struct _mesa_glsl_parse_state *state);
437
438    ast_fully_specified_type *type;
439    char *identifier;
440    int is_array;
441    ast_expression *array_size;
442
443    static void parameters_to_hir(exec_list *ast_parameters,
444                                  bool formal, exec_list *ir_parameters,
445                                  struct _mesa_glsl_parse_state *state);
446
447 private:
448    /** Is this parameter declaration part of a formal parameter list? */
449    bool formal_parameter;
450
451    /**
452     * Is this parameter 'void' type?
453     *
454     * This field is set by \c ::hir.
455     */
456    bool is_void;
457 };
458
459
460 class ast_function : public ast_node {
461 public:
462    ast_function(void);
463
464    virtual void print(void) const;
465
466    virtual ir_rvalue *hir(exec_list *instructions,
467                           struct _mesa_glsl_parse_state *state);
468
469    ast_fully_specified_type *return_type;
470    char *identifier;
471
472    exec_list parameters;
473
474 private:
475    /**
476     * Is this prototype part of the function definition?
477     *
478     * Used by ast_function_definition::hir to process the parameters, etc.
479     * of the function.
480     *
481     * \sa ::hir
482     */
483    bool is_definition;
484
485    /**
486     * Function signature corresponding to this function prototype instance
487     *
488     * Used by ast_function_definition::hir to process the parameters, etc.
489     * of the function.
490     *
491     * \sa ::hir
492     */
493    class ir_function_signature *signature;
494
495    friend class ast_function_definition;
496 };
497
498
499 class ast_declaration_statement : public ast_node {
500 public:
501    ast_declaration_statement(void);
502
503    enum {
504       ast_function,
505       ast_declaration,
506       ast_precision
507    } mode;
508
509    union {
510       class ast_function *function;
511       ast_declarator_list *declarator;
512       ast_type_specifier *type;
513       ast_node *node;
514    } declaration;
515 };
516
517
518 class ast_expression_statement : public ast_node {
519 public:
520    ast_expression_statement(ast_expression *);
521    virtual void print(void) const;
522
523    virtual ir_rvalue *hir(exec_list *instructions,
524                           struct _mesa_glsl_parse_state *state);
525
526    ast_expression *expression;
527 };
528
529
530 class ast_case_label : public ast_node {
531 public:
532
533    /**
534     * An expression of NULL means 'default'.
535     */
536    ast_expression *expression;
537 };
538
539 class ast_selection_statement : public ast_node {
540 public:
541    ast_selection_statement(ast_expression *condition,
542                            ast_node *then_statement,
543                            ast_node *else_statement);
544    virtual void print(void) const;
545
546    virtual ir_rvalue *hir(exec_list *instructions,
547                           struct _mesa_glsl_parse_state *state);
548
549    ast_expression *condition;
550    ast_node *then_statement;
551    ast_node *else_statement;
552 };
553
554
555 class ast_switch_statement : public ast_node {
556 public:
557    ast_expression *expression;
558    exec_list statements;
559 };
560
561 class ast_iteration_statement : public ast_node {
562 public:
563    ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
564                            ast_expression *rest_expression, ast_node *body);
565
566    virtual void print(void) const;
567
568    virtual ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
569
570    enum ast_iteration_modes {
571       ast_for,
572       ast_while,
573       ast_do_while
574    } mode;
575    
576
577    ast_node *init_statement;
578    ast_node *condition;
579    ast_expression *rest_expression;
580
581    ast_node *body;
582
583 private:
584    /**
585     * Generate IR from the condition of a loop
586     *
587     * This is factored out of ::hir because some loops have the condition
588     * test at the top (for and while), and others have it at the end (do-while).
589     */
590    void condition_to_hir(class ir_loop *, struct _mesa_glsl_parse_state *);
591 };
592
593
594 class ast_jump_statement : public ast_node {
595 public:
596    ast_jump_statement(int mode, ast_expression *return_value);
597    virtual void print(void) const;
598
599    virtual ir_rvalue *hir(exec_list *instructions,
600                           struct _mesa_glsl_parse_state *state);
601
602    enum ast_jump_modes {
603       ast_continue,
604       ast_break,
605       ast_return,
606       ast_discard
607    } mode;
608
609    ast_expression *opt_return_value;
610 };
611
612
613 class ast_function_definition : public ast_node {
614 public:
615    virtual void print(void) const;
616
617    virtual ir_rvalue *hir(exec_list *instructions,
618                           struct _mesa_glsl_parse_state *state);
619
620    ast_function *prototype;
621    ast_compound_statement *body;
622 };
623
624
625 extern void
626 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state);
627
628 extern struct ir_rvalue *
629 _mesa_ast_field_selection_to_hir(const struct ast_expression *expr,
630                                  exec_list *instructions,
631                                  struct _mesa_glsl_parse_state *state);
632
633 #endif /* AST_H */