1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
31 #include "../pp/sl_pp_public.h"
32 #include "sl_cl_parse.h"
35 /* revision number - increment after each change affecting emitted output */
38 /* external declaration (or precision or invariant stmt) */
39 #define EXTERNAL_NULL 0
40 #define EXTERNAL_FUNCTION_DEFINITION 1
41 #define EXTERNAL_DECLARATION 2
42 #define DEFAULT_PRECISION 3
43 #define INVARIANT_STMT 4
46 #define PRECISION_DEFAULT 0
47 #define PRECISION_LOW 1
48 #define PRECISION_MEDIUM 2
49 #define PRECISION_HIGH 3
52 #define DECLARATION_FUNCTION_PROTOTYPE 1
53 #define DECLARATION_INIT_DECLARATOR_LIST 2
56 #define FUNCTION_ORDINARY 0
57 #define FUNCTION_CONSTRUCTOR 1
58 #define FUNCTION_OPERATOR 2
60 /* function call type */
61 #define FUNCTION_CALL_NONARRAY 0
62 #define FUNCTION_CALL_ARRAY 1
65 #define OPERATOR_ADDASSIGN 1
66 #define OPERATOR_SUBASSIGN 2
67 #define OPERATOR_MULASSIGN 3
68 #define OPERATOR_DIVASSIGN 4
69 /*#define OPERATOR_MODASSIGN 5*/
70 /*#define OPERATOR_LSHASSIGN 6*/
71 /*#define OPERATOR_RSHASSIGN 7*/
72 /*#define OPERATOR_ORASSIGN 8*/
73 /*#define OPERATOR_XORASSIGN 9*/
74 /*#define OPERATOR_ANDASSIGN 10*/
75 #define OPERATOR_LOGICALXOR 11
76 /*#define OPERATOR_BITOR 12*/
77 /*#define OPERATOR_BITXOR 13*/
78 /*#define OPERATOR_BITAND 14*/
79 #define OPERATOR_LESS 15
80 #define OPERATOR_GREATER 16
81 #define OPERATOR_LESSEQUAL 17
82 #define OPERATOR_GREATEREQUAL 18
83 /*#define OPERATOR_LSHIFT 19*/
84 /*#define OPERATOR_RSHIFT 20*/
85 #define OPERATOR_MULTIPLY 21
86 #define OPERATOR_DIVIDE 22
87 /*#define OPERATOR_MODULUS 23*/
88 #define OPERATOR_INCREMENT 24
89 #define OPERATOR_DECREMENT 25
90 #define OPERATOR_PLUS 26
91 #define OPERATOR_MINUS 27
92 /*#define OPERATOR_COMPLEMENT 28*/
93 #define OPERATOR_NOT 29
95 /* init declarator list */
96 #define DECLARATOR_NONE 0
97 #define DECLARATOR_NEXT 1
99 /* variable declaration */
100 #define VARIABLE_NONE 0
101 #define VARIABLE_IDENTIFIER 1
102 #define VARIABLE_INITIALIZER 2
103 #define VARIABLE_ARRAY_EXPLICIT 3
104 #define VARIABLE_ARRAY_UNKNOWN 4
107 #define TYPE_QUALIFIER_NONE 0
108 #define TYPE_QUALIFIER_CONST 1
109 #define TYPE_QUALIFIER_ATTRIBUTE 2
110 #define TYPE_QUALIFIER_VARYING 3
111 #define TYPE_QUALIFIER_UNIFORM 4
112 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
113 #define TYPE_QUALIFIER_FIXEDINPUT 6
115 /* invariant qualifier */
116 #define TYPE_VARIANT 90
117 #define TYPE_INVARIANT 91
119 /* centroid qualifier */
120 #define TYPE_CENTER 95
121 #define TYPE_CENTROID 96
124 #define TYPE_SPECIFIER_VOID 0
125 #define TYPE_SPECIFIER_BOOL 1
126 #define TYPE_SPECIFIER_BVEC2 2
127 #define TYPE_SPECIFIER_BVEC3 3
128 #define TYPE_SPECIFIER_BVEC4 4
129 #define TYPE_SPECIFIER_INT 5
130 #define TYPE_SPECIFIER_IVEC2 6
131 #define TYPE_SPECIFIER_IVEC3 7
132 #define TYPE_SPECIFIER_IVEC4 8
133 #define TYPE_SPECIFIER_FLOAT 9
134 #define TYPE_SPECIFIER_VEC2 10
135 #define TYPE_SPECIFIER_VEC3 11
136 #define TYPE_SPECIFIER_VEC4 12
137 #define TYPE_SPECIFIER_MAT2 13
138 #define TYPE_SPECIFIER_MAT3 14
139 #define TYPE_SPECIFIER_MAT4 15
140 #define TYPE_SPECIFIER_SAMPLER1D 16
141 #define TYPE_SPECIFIER_SAMPLER2D 17
142 #define TYPE_SPECIFIER_SAMPLER3D 18
143 #define TYPE_SPECIFIER_SAMPLERCUBE 19
144 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
145 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
146 #define TYPE_SPECIFIER_SAMPLER2DRECT 22
147 #define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
148 #define TYPE_SPECIFIER_STRUCT 24
149 #define TYPE_SPECIFIER_TYPENAME 25
152 #define TYPE_SPECIFIER_MAT23 26
153 #define TYPE_SPECIFIER_MAT32 27
154 #define TYPE_SPECIFIER_MAT24 28
155 #define TYPE_SPECIFIER_MAT42 29
156 #define TYPE_SPECIFIER_MAT34 30
157 #define TYPE_SPECIFIER_MAT43 31
159 /* type specifier array */
160 #define TYPE_SPECIFIER_NONARRAY 0
161 #define TYPE_SPECIFIER_ARRAY 1
163 /* structure field */
166 #define FIELD_ARRAY 2
170 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
171 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
175 #define OP_CONTINUE 6
178 #define OP_EXPRESSION 9
183 #define OP_PUSH_VOID 14
184 #define OP_PUSH_BOOL 15
185 #define OP_PUSH_INT 16
186 #define OP_PUSH_FLOAT 17
187 #define OP_PUSH_IDENTIFIER 18
188 #define OP_SEQUENCE 19
190 #define OP_ADDASSIGN 21
191 #define OP_SUBASSIGN 22
192 #define OP_MULASSIGN 23
193 #define OP_DIVASSIGN 24
194 /*#define OP_MODASSIGN 25*/
195 /*#define OP_LSHASSIGN 26*/
196 /*#define OP_RSHASSIGN 27*/
197 /*#define OP_ORASSIGN 28*/
198 /*#define OP_XORASSIGN 29*/
199 /*#define OP_ANDASSIGN 30*/
201 #define OP_LOGICALOR 32
202 #define OP_LOGICALXOR 33
203 #define OP_LOGICALAND 34
204 /*#define OP_BITOR 35*/
205 /*#define OP_BITXOR 36*/
206 /*#define OP_BITAND 37*/
208 #define OP_NOTEQUAL 39
210 #define OP_GREATER 41
211 #define OP_LESSEQUAL 42
212 #define OP_GREATEREQUAL 43
213 /*#define OP_LSHIFT 44*/
214 /*#define OP_RSHIFT 45*/
216 #define OP_SUBTRACT 47
217 #define OP_MULTIPLY 48
219 /*#define OP_MODULUS 50*/
220 #define OP_PREINCREMENT 51
221 #define OP_PREDECREMENT 52
224 /*#define OP_COMPLEMENT 55*/
226 #define OP_SUBSCRIPT 57
229 #define OP_POSTINCREMENT 60
230 #define OP_POSTDECREMENT 61
231 #define OP_PRECISION 62
234 /* parameter qualifier */
235 #define PARAM_QUALIFIER_IN 0
236 #define PARAM_QUALIFIER_OUT 1
237 #define PARAM_QUALIFIER_INOUT 2
239 /* function parameter */
240 #define PARAMETER_NONE 0
241 #define PARAMETER_NEXT 1
243 /* function parameter array presence */
244 #define PARAMETER_ARRAY_NOT_PRESENT 0
245 #define PARAMETER_ARRAY_PRESENT 1
278 int sampler2DRectShadow
;
322 struct parse_context
{
323 struct sl_pp_context
*context
;
324 const struct sl_pp_token_info
*input
;
326 struct parse_dict dict
;
328 unsigned char *out_buf
;
329 unsigned int out_cap
;
331 unsigned int shader_type
;
332 unsigned int parsing_builtin
;
342 static __inline
unsigned int
343 _emit(struct parse_context
*ctx
,
347 if (*out
== ctx
->out_cap
) {
348 ctx
->out_cap
+= 4096;
349 ctx
->out_buf
= (unsigned char *)realloc(ctx
->out_buf
, ctx
->out_cap
* sizeof(unsigned char));
351 ctx
->out_buf
[*out
] = b
;
357 _update(struct parse_context
*ctx
,
361 ctx
->out_buf
[out
] = b
;
366 _parse_token(struct parse_context
*ctx
,
367 enum sl_pp_token token
,
368 struct parse_state
*ps
)
370 if (ctx
->input
[ps
->in
].token
== token
) {
379 _parse_id(struct parse_context
*ctx
,
381 struct parse_state
*ps
)
383 if (ctx
->input
[ps
->in
].token
== SL_PP_IDENTIFIER
&&
384 ctx
->input
[ps
->in
].data
.identifier
== id
) {
393 _parse_identifier(struct parse_context
*ctx
,
394 struct parse_state
*ps
)
396 if (ctx
->input
[ps
->in
].token
== SL_PP_IDENTIFIER
) {
397 const char *cstr
= sl_pp_context_cstr(ctx
->context
, ctx
->input
[ps
->in
].data
.identifier
);
400 _emit(ctx
, &ps
->out
, *cstr
);
410 _parse_float(struct parse_context
*ctx
,
411 struct parse_state
*ps
)
413 if (ctx
->input
[ps
->in
].token
== SL_PP_FLOAT
) {
414 const char *cstr
= sl_pp_context_cstr(ctx
->context
, ctx
->input
[ps
->in
].data
._float
);
416 _emit(ctx
, &ps
->out
, 1);
418 _emit(ctx
, &ps
->out
, *cstr
);
428 _parse_uint(struct parse_context
*ctx
,
429 struct parse_state
*ps
)
431 if (ctx
->input
[ps
->in
].token
== SL_PP_UINT
) {
432 const char *cstr
= sl_pp_context_cstr(ctx
->context
, ctx
->input
[ps
->in
].data
._uint
);
434 _emit(ctx
, &ps
->out
, 1);
436 _emit(ctx
, &ps
->out
, *cstr
);
445 /**************************************/
449 _parse_unary_expression(struct parse_context
*ctx
,
450 struct parse_state
*ps
);
453 _parse_conditional_expression(struct parse_context
*ctx
,
454 struct parse_state
*ps
);
458 _parse_constant_expression(struct parse_context
*ctx
,
459 struct parse_state
*ps
);
463 _parse_primary_expression(struct parse_context
*ctx
,
464 struct parse_state
*ps
);
468 _parse_statement(struct parse_context
*ctx
,
469 struct parse_state
*ps
);
473 _parse_type_specifier(struct parse_context
*ctx
,
474 struct parse_state
*ps
);
478 _parse_declaration(struct parse_context
*ctx
,
479 struct parse_state
*ps
);
483 _parse_statement_list(struct parse_context
*ctx
,
484 struct parse_state
*ps
);
488 _parse_assignment_expression(struct parse_context
*ctx
,
489 struct parse_state
*ps
);
493 _parse_precision(struct parse_context
*ctx
,
494 struct parse_state
*ps
);
498 _parse_overriden_operator(struct parse_context
*ctx
,
499 struct parse_state
*ps
)
503 if (_parse_token(ctx
, SL_PP_INCREMENT
, ps
) == 0) {
504 op
= OPERATOR_INCREMENT
;
505 } else if (_parse_token(ctx
, SL_PP_ADDASSIGN
, ps
) == 0) {
506 op
= OPERATOR_ADDASSIGN
;
507 } else if (_parse_token(ctx
, SL_PP_PLUS
, ps
) == 0) {
509 } else if (_parse_token(ctx
, SL_PP_DECREMENT
, ps
) == 0) {
510 op
= OPERATOR_DECREMENT
;
511 } else if (_parse_token(ctx
, SL_PP_SUBASSIGN
, ps
) == 0) {
512 op
= OPERATOR_SUBASSIGN
;
513 } else if (_parse_token(ctx
, SL_PP_MINUS
, ps
) == 0) {
515 } else if (_parse_token(ctx
, SL_PP_NOT
, ps
) == 0) {
517 } else if (_parse_token(ctx
, SL_PP_MULASSIGN
, ps
) == 0) {
518 op
= OPERATOR_MULASSIGN
;
519 } else if (_parse_token(ctx
, SL_PP_STAR
, ps
) == 0) {
520 op
= OPERATOR_MULTIPLY
;
521 } else if (_parse_token(ctx
, SL_PP_DIVASSIGN
, ps
) == 0) {
522 op
= OPERATOR_DIVASSIGN
;
523 } else if (_parse_token(ctx
, SL_PP_SLASH
, ps
) == 0) {
524 op
= OPERATOR_DIVIDE
;
525 } else if (_parse_token(ctx
, SL_PP_LESSEQUAL
, ps
) == 0) {
526 op
= OPERATOR_LESSEQUAL
;
527 } else if (_parse_token(ctx
, SL_PP_LESS
, ps
) == 0) {
529 } else if (_parse_token(ctx
, SL_PP_GREATEREQUAL
, ps
) == 0) {
530 op
= OPERATOR_GREATEREQUAL
;
531 } else if (_parse_token(ctx
, SL_PP_GREATER
, ps
) == 0) {
532 op
= OPERATOR_GREATER
;
533 } else if (_parse_token(ctx
, SL_PP_XOR
, ps
) == 0) {
534 op
= OPERATOR_LOGICALXOR
;
539 _emit(ctx
, &ps
->out
, op
);
545 _parse_function_decl_identifier(struct parse_context
*ctx
,
546 struct parse_state
*ps
)
548 struct parse_state p
= *ps
;
549 unsigned int e
= _emit(ctx
, &p
.out
, 0);
551 if (ctx
->parsing_builtin
&& _parse_id(ctx
, ctx
->dict
.__constructor
, &p
) == 0) {
552 _update(ctx
, e
, FUNCTION_CONSTRUCTOR
);
557 if (ctx
->parsing_builtin
&& _parse_id(ctx
, ctx
->dict
.__operator
, &p
) == 0) {
558 _update(ctx
, e
, FUNCTION_OPERATOR
);
559 if (_parse_overriden_operator(ctx
, &p
) == 0) {
566 if (_parse_identifier(ctx
, &p
) == 0) {
567 _update(ctx
, e
, FUNCTION_ORDINARY
);
577 _parse_invariant_qualifier(struct parse_context
*ctx
,
578 struct parse_state
*ps
)
580 if (_parse_id(ctx
, ctx
->dict
.invariant
, ps
)) {
583 _emit(ctx
, &ps
->out
, TYPE_INVARIANT
);
589 _parse_centroid_qualifier(struct parse_context
*ctx
,
590 struct parse_state
*ps
)
592 if (_parse_id(ctx
, ctx
->dict
.centroid
, ps
)) {
595 _emit(ctx
, &ps
->out
, TYPE_CENTROID
);
601 _parse_type_qualifier(struct parse_context
*ctx
,
602 struct parse_state
*ps
)
604 struct parse_state p
= *ps
;
605 unsigned int e
= _emit(ctx
, &p
.out
, 0);
608 if (ctx
->input
[p
.in
].token
!= SL_PP_IDENTIFIER
) {
611 id
= ctx
->input
[p
.in
].data
.identifier
;
613 if (id
== ctx
->dict
._const
) {
614 _update(ctx
, e
, TYPE_QUALIFIER_CONST
);
615 } else if (ctx
->shader_type
== 2 && id
== ctx
->dict
.attribute
) {
616 _update(ctx
, e
, TYPE_QUALIFIER_ATTRIBUTE
);
617 } else if (id
== ctx
->dict
.varying
) {
618 _update(ctx
, e
, TYPE_QUALIFIER_VARYING
);
619 } else if (id
== ctx
->dict
.uniform
) {
620 _update(ctx
, e
, TYPE_QUALIFIER_UNIFORM
);
621 } else if (ctx
->parsing_builtin
&& id
== ctx
->dict
.__fixed_output
) {
622 _update(ctx
, e
, TYPE_QUALIFIER_FIXEDOUTPUT
);
623 } else if (ctx
->parsing_builtin
&& id
== ctx
->dict
.__fixed_input
) {
624 _update(ctx
, e
, TYPE_QUALIFIER_FIXEDINPUT
);
628 _parse_token(ctx
, SL_PP_IDENTIFIER
, &p
);
635 _parse_struct_declarator(struct parse_context
*ctx
,
636 struct parse_state
*ps
)
638 struct parse_state p
= *ps
;
641 if (_parse_identifier(ctx
, &p
)) {
644 e
= _emit(ctx
, &p
.out
, FIELD_NONE
);
647 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
650 if (_parse_constant_expression(ctx
, &p
)) {
653 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
656 _update(ctx
, e
, FIELD_ARRAY
);
663 _parse_struct_declarator_list(struct parse_context
*ctx
,
664 struct parse_state
*ps
)
666 struct parse_state p
= *ps
;
668 if (_parse_struct_declarator(ctx
, &p
)) {
674 _emit(ctx
, &p
.out
, FIELD_NEXT
);
675 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
678 if (_parse_struct_declarator(ctx
, &p
)) {
686 _parse_struct_declaration(struct parse_context
*ctx
,
687 struct parse_state
*ps
)
689 struct parse_state p
= *ps
;
691 if (_parse_type_specifier(ctx
, &p
)) {
694 if (_parse_struct_declarator_list(ctx
, &p
)) {
697 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
700 _emit(ctx
, &p
.out
, FIELD_NONE
);
707 _parse_struct_declaration_list(struct parse_context
*ctx
,
708 struct parse_state
*ps
)
710 struct parse_state p
= *ps
;
712 if (_parse_struct_declaration(ctx
, &p
)) {
718 _emit(ctx
, &p
.out
, FIELD_NEXT
);
719 if (_parse_struct_declaration(ctx
, &p
)) {
727 _parse_struct_specifier(struct parse_context
*ctx
,
728 struct parse_state
*ps
)
730 struct parse_state p
= *ps
;
732 if (_parse_id(ctx
, ctx
->dict
._struct
, &p
)) {
735 if (_parse_identifier(ctx
, &p
)) {
736 _emit(ctx
, &p
.out
, '\0');
738 if (_parse_token(ctx
, SL_PP_LBRACE
, &p
)) {
741 if (_parse_struct_declaration_list(ctx
, &p
)) {
744 if (_parse_token(ctx
, SL_PP_RBRACE
, &p
)) {
747 _emit(ctx
, &p
.out
, FIELD_NONE
);
754 _parse_type_specifier_nonarray(struct parse_context
*ctx
,
755 struct parse_state
*ps
)
757 struct parse_state p
= *ps
;
758 unsigned int e
= _emit(ctx
, &p
.out
, 0);
761 if (_parse_struct_specifier(ctx
, &p
) == 0) {
762 _update(ctx
, e
, TYPE_SPECIFIER_STRUCT
);
767 if (ctx
->input
[p
.in
].token
!= SL_PP_IDENTIFIER
) {
770 id
= ctx
->input
[p
.in
].data
.identifier
;
772 if (id
== ctx
->dict
._void
) {
773 _update(ctx
, e
, TYPE_SPECIFIER_VOID
);
774 } else if (id
== ctx
->dict
._float
) {
775 _update(ctx
, e
, TYPE_SPECIFIER_FLOAT
);
776 } else if (id
== ctx
->dict
._int
) {
777 _update(ctx
, e
, TYPE_SPECIFIER_INT
);
778 } else if (id
== ctx
->dict
._bool
) {
779 _update(ctx
, e
, TYPE_SPECIFIER_BOOL
);
780 } else if (id
== ctx
->dict
.vec2
) {
781 _update(ctx
, e
, TYPE_SPECIFIER_VEC2
);
782 } else if (id
== ctx
->dict
.vec3
) {
783 _update(ctx
, e
, TYPE_SPECIFIER_VEC3
);
784 } else if (id
== ctx
->dict
.vec4
) {
785 _update(ctx
, e
, TYPE_SPECIFIER_VEC4
);
786 } else if (id
== ctx
->dict
.bvec2
) {
787 _update(ctx
, e
, TYPE_SPECIFIER_BVEC2
);
788 } else if (id
== ctx
->dict
.bvec3
) {
789 _update(ctx
, e
, TYPE_SPECIFIER_BVEC3
);
790 } else if (id
== ctx
->dict
.bvec4
) {
791 _update(ctx
, e
, TYPE_SPECIFIER_BVEC4
);
792 } else if (id
== ctx
->dict
.ivec2
) {
793 _update(ctx
, e
, TYPE_SPECIFIER_IVEC2
);
794 } else if (id
== ctx
->dict
.ivec3
) {
795 _update(ctx
, e
, TYPE_SPECIFIER_IVEC3
);
796 } else if (id
== ctx
->dict
.ivec4
) {
797 _update(ctx
, e
, TYPE_SPECIFIER_IVEC4
);
798 } else if (id
== ctx
->dict
.mat2
) {
799 _update(ctx
, e
, TYPE_SPECIFIER_MAT2
);
800 } else if (id
== ctx
->dict
.mat3
) {
801 _update(ctx
, e
, TYPE_SPECIFIER_MAT3
);
802 } else if (id
== ctx
->dict
.mat4
) {
803 _update(ctx
, e
, TYPE_SPECIFIER_MAT4
);
804 } else if (id
== ctx
->dict
.mat2x3
) {
805 _update(ctx
, e
, TYPE_SPECIFIER_MAT23
);
806 } else if (id
== ctx
->dict
.mat3x2
) {
807 _update(ctx
, e
, TYPE_SPECIFIER_MAT32
);
808 } else if (id
== ctx
->dict
.mat2x4
) {
809 _update(ctx
, e
, TYPE_SPECIFIER_MAT24
);
810 } else if (id
== ctx
->dict
.mat4x2
) {
811 _update(ctx
, e
, TYPE_SPECIFIER_MAT42
);
812 } else if (id
== ctx
->dict
.mat3x4
) {
813 _update(ctx
, e
, TYPE_SPECIFIER_MAT34
);
814 } else if (id
== ctx
->dict
.mat4x3
) {
815 _update(ctx
, e
, TYPE_SPECIFIER_MAT43
);
816 } else if (id
== ctx
->dict
.sampler1D
) {
817 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER1D
);
818 } else if (id
== ctx
->dict
.sampler2D
) {
819 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2D
);
820 } else if (id
== ctx
->dict
.sampler3D
) {
821 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER3D
);
822 } else if (id
== ctx
->dict
.samplerCube
) {
823 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLERCUBE
);
824 } else if (id
== ctx
->dict
.sampler1DShadow
) {
825 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER1DSHADOW
);
826 } else if (id
== ctx
->dict
.sampler2DShadow
) {
827 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2DSHADOW
);
828 } else if (id
== ctx
->dict
.sampler2DRect
) {
829 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2DRECT
);
830 } else if (id
== ctx
->dict
.sampler2DRectShadow
) {
831 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
);
832 } else if (_parse_identifier(ctx
, &p
) == 0) {
833 _update(ctx
, e
, TYPE_SPECIFIER_TYPENAME
);
840 _parse_token(ctx
, SL_PP_IDENTIFIER
, &p
);
847 _parse_type_specifier_array(struct parse_context
*ctx
,
848 struct parse_state
*ps
)
850 struct parse_state p
= *ps
;
852 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
855 if (_parse_constant_expression(ctx
, &p
)) {
858 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
867 _parse_type_specifier(struct parse_context
*ctx
,
868 struct parse_state
*ps
)
870 struct parse_state p
= *ps
;
873 if (_parse_type_specifier_nonarray(ctx
, &p
)) {
877 e
= _emit(ctx
, &p
.out
, TYPE_SPECIFIER_ARRAY
);
878 if (_parse_type_specifier_array(ctx
, &p
)) {
879 _update(ctx
, e
, TYPE_SPECIFIER_NONARRAY
);
887 _parse_fully_specified_type(struct parse_context
*ctx
,
888 struct parse_state
*ps
)
890 struct parse_state p
= *ps
;
892 if (_parse_invariant_qualifier(ctx
, &p
)) {
893 _emit(ctx
, &p
.out
, TYPE_VARIANT
);
895 if (_parse_centroid_qualifier(ctx
, &p
)) {
896 _emit(ctx
, &p
.out
, TYPE_CENTER
);
898 if (_parse_type_qualifier(ctx
, &p
)) {
899 _emit(ctx
, &p
.out
, TYPE_QUALIFIER_NONE
);
901 if (_parse_precision(ctx
, &p
)) {
902 _emit(ctx
, &p
.out
, PRECISION_DEFAULT
);
904 if (_parse_type_specifier(ctx
, &p
)) {
913 _parse_function_header(struct parse_context
*ctx
,
914 struct parse_state
*ps
)
916 struct parse_state p
= *ps
;
918 if (_parse_fully_specified_type(ctx
, &p
)) {
921 if (_parse_function_decl_identifier(ctx
, &p
)) {
924 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
933 _parse_parameter_qualifier(struct parse_context
*ctx
,
934 struct parse_state
*ps
)
936 unsigned int e
= _emit(ctx
, &ps
->out
, PARAM_QUALIFIER_IN
);
938 if (_parse_id(ctx
, ctx
->dict
.out
, ps
) == 0) {
939 _update(ctx
, e
, PARAM_QUALIFIER_OUT
);
940 } else if (_parse_id(ctx
, ctx
->dict
.inout
, ps
) == 0) {
941 _update(ctx
, e
, PARAM_QUALIFIER_INOUT
);
943 _parse_id(ctx
, ctx
->dict
.in
, ps
);
950 _parse_function_identifier(struct parse_context
*ctx
,
951 struct parse_state
*ps
)
953 struct parse_state p
;
956 if (_parse_identifier(ctx
, ps
)) {
959 e
= _emit(ctx
, &ps
->out
, FUNCTION_CALL_NONARRAY
);
962 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
965 if (_parse_constant_expression(ctx
, &p
)) {
968 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
971 _update(ctx
, e
, FUNCTION_CALL_ARRAY
);
978 _parse_function_call_header(struct parse_context
*ctx
,
979 struct parse_state
*ps
)
981 struct parse_state p
= *ps
;
983 if (_parse_function_identifier(ctx
, &p
)) {
986 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
995 _parse_assign_expression(struct parse_context
*ctx
,
996 struct parse_state
*ps
)
998 struct parse_state p
= *ps
;
1001 if (_parse_unary_expression(ctx
, &p
)) {
1005 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
) == 0) {
1007 } else if (_parse_token(ctx
, SL_PP_MULASSIGN
, &p
) == 0) {
1009 } else if (_parse_token(ctx
, SL_PP_DIVASSIGN
, &p
) == 0) {
1011 } else if (_parse_token(ctx
, SL_PP_ADDASSIGN
, &p
) == 0) {
1013 } else if (_parse_token(ctx
, SL_PP_SUBASSIGN
, &p
) == 0) {
1019 if (_parse_assignment_expression(ctx
, &p
)) {
1022 _emit(ctx
, &p
.out
, op
);
1030 _parse_assignment_expression(struct parse_context
*ctx
,
1031 struct parse_state
*ps
)
1033 if (_parse_assign_expression(ctx
, ps
) == 0) {
1037 if (_parse_conditional_expression(ctx
, ps
) == 0) {
1046 _parse_function_call_header_with_parameters(struct parse_context
*ctx
,
1047 struct parse_state
*ps
)
1049 struct parse_state p
= *ps
;
1051 if (_parse_function_call_header(ctx
, &p
)) {
1054 if (_parse_assignment_expression(ctx
, &p
)) {
1057 _emit(ctx
, &p
.out
, OP_END
);
1060 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1063 if (_parse_assignment_expression(ctx
, &p
)) {
1066 _emit(ctx
, &p
.out
, OP_END
);
1072 _parse_function_call_header_no_parameters(struct parse_context
*ctx
,
1073 struct parse_state
*ps
)
1075 if (_parse_function_call_header(ctx
, ps
)) {
1078 _parse_id(ctx
, ctx
->dict
._void
, ps
);
1084 _parse_function_call_generic(struct parse_context
*ctx
,
1085 struct parse_state
*ps
)
1087 struct parse_state p
= *ps
;
1089 if (_parse_function_call_header_with_parameters(ctx
, &p
) == 0) {
1090 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1097 if (_parse_function_call_header_no_parameters(ctx
, &p
) == 0) {
1098 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1109 _parse_method_call(struct parse_context
*ctx
,
1110 struct parse_state
*ps
)
1112 struct parse_state p
= *ps
;
1114 _emit(ctx
, &p
.out
, OP_METHOD
);
1115 if (_parse_identifier(ctx
, &p
)) {
1118 if (_parse_token(ctx
, SL_PP_DOT
, &p
)) {
1121 if (_parse_function_call_generic(ctx
, &p
)) {
1124 _emit(ctx
, &p
.out
, OP_END
);
1131 _parse_regular_function_call(struct parse_context
*ctx
,
1132 struct parse_state
*ps
)
1134 struct parse_state p
= *ps
;
1136 _emit(ctx
, &p
.out
, OP_CALL
);
1137 if (_parse_function_call_generic(ctx
, &p
)) {
1140 _emit(ctx
, &p
.out
, OP_END
);
1147 _parse_function_call(struct parse_context
*ctx
,
1148 struct parse_state
*ps
)
1150 if (_parse_regular_function_call(ctx
, ps
) == 0) {
1154 if (_parse_method_call(ctx
, ps
) == 0) {
1163 _parse_expression(struct parse_context
*ctx
,
1164 struct parse_state
*ps
)
1166 struct parse_state p
= *ps
;
1168 if (_parse_assignment_expression(ctx
, &p
)) {
1174 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1177 if (_parse_assignment_expression(ctx
, &p
)) {
1180 _emit(ctx
, &p
.out
, OP_SEQUENCE
);
1186 _parse_postfix_expression(struct parse_context
*ctx
,
1187 struct parse_state
*ps
)
1189 struct parse_state p
;
1191 if (_parse_function_call(ctx
, ps
)) {
1192 if (_parse_primary_expression(ctx
, ps
)) {
1199 if (_parse_token(ctx
, SL_PP_INCREMENT
, &p
) == 0) {
1200 _emit(ctx
, &p
.out
, OP_POSTINCREMENT
);
1201 } else if (_parse_token(ctx
, SL_PP_DECREMENT
, &p
) == 0) {
1202 _emit(ctx
, &p
.out
, OP_POSTDECREMENT
);
1203 } else if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
) == 0) {
1204 if (_parse_expression(ctx
, &p
)) {
1207 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1210 _emit(ctx
, &p
.out
, OP_SUBSCRIPT
);
1211 } else if (_parse_token(ctx
, SL_PP_DOT
, &p
) == 0) {
1212 _emit(ctx
, &p
.out
, OP_FIELD
);
1213 if (_parse_identifier(ctx
, &p
)) {
1224 _parse_unary_expression(struct parse_context
*ctx
,
1225 struct parse_state
*ps
)
1227 struct parse_state p
;
1230 if (_parse_postfix_expression(ctx
, ps
) == 0) {
1235 if (_parse_token(ctx
, SL_PP_INCREMENT
, &p
) == 0) {
1236 op
= OP_PREINCREMENT
;
1237 } else if (_parse_token(ctx
, SL_PP_DECREMENT
, &p
) == 0) {
1238 op
= OP_PREDECREMENT
;
1239 } else if (_parse_token(ctx
, SL_PP_PLUS
, &p
) == 0) {
1241 } else if (_parse_token(ctx
, SL_PP_MINUS
, &p
) == 0) {
1243 } else if (_parse_token(ctx
, SL_PP_NOT
, &p
) == 0) {
1249 if (_parse_unary_expression(ctx
, &p
)) {
1252 _emit(ctx
, &p
.out
, op
);
1259 _parse_multiplicative_expression(struct parse_context
*ctx
,
1260 struct parse_state
*ps
)
1262 struct parse_state p
= *ps
;
1264 if (_parse_unary_expression(ctx
, &p
)) {
1271 if (_parse_token(ctx
, SL_PP_STAR
, &p
) == 0) {
1273 } else if (_parse_token(ctx
, SL_PP_SLASH
, &p
) == 0) {
1278 if (_parse_unary_expression(ctx
, &p
)) {
1281 _emit(ctx
, &p
.out
, op
);
1287 _parse_additive_expression(struct parse_context
*ctx
,
1288 struct parse_state
*ps
)
1290 struct parse_state p
= *ps
;
1292 if (_parse_multiplicative_expression(ctx
, &p
)) {
1299 if (_parse_token(ctx
, SL_PP_PLUS
, &p
) == 0) {
1301 } else if (_parse_token(ctx
, SL_PP_MINUS
, &p
) == 0) {
1306 if (_parse_multiplicative_expression(ctx
, &p
)) {
1309 _emit(ctx
, &p
.out
, op
);
1315 _parse_relational_expression(struct parse_context
*ctx
,
1316 struct parse_state
*ps
)
1318 struct parse_state p
= *ps
;
1320 if (_parse_additive_expression(ctx
, &p
)) {
1327 if (_parse_token(ctx
, SL_PP_LESS
, &p
) == 0) {
1329 } else if (_parse_token(ctx
, SL_PP_GREATER
, &p
) == 0) {
1331 } else if (_parse_token(ctx
, SL_PP_LESSEQUAL
, &p
) == 0) {
1333 } else if (_parse_token(ctx
, SL_PP_GREATEREQUAL
, &p
) == 0) {
1334 op
= OP_GREATEREQUAL
;
1338 if (_parse_additive_expression(ctx
, &p
)) {
1341 _emit(ctx
, &p
.out
, op
);
1347 _parse_equality_expression(struct parse_context
*ctx
,
1348 struct parse_state
*ps
)
1350 struct parse_state p
= *ps
;
1352 if (_parse_relational_expression(ctx
, &p
)) {
1359 if (_parse_token(ctx
, SL_PP_EQUAL
, &p
) == 0) {
1361 } else if (_parse_token(ctx
, SL_PP_NOTEQUAL
, &p
) == 0) {
1366 if (_parse_relational_expression(ctx
, &p
)) {
1369 _emit(ctx
, &p
.out
, op
);
1375 _parse_logical_and_expression(struct parse_context
*ctx
,
1376 struct parse_state
*ps
)
1378 struct parse_state p
= *ps
;
1380 if (_parse_equality_expression(ctx
, &p
)) {
1385 if (_parse_token(ctx
, SL_PP_AND
, &p
)) {
1388 if (_parse_equality_expression(ctx
, &p
)) {
1391 _emit(ctx
, &p
.out
, OP_LOGICALAND
);
1397 _parse_logical_xor_expression(struct parse_context
*ctx
,
1398 struct parse_state
*ps
)
1400 struct parse_state p
= *ps
;
1402 if (_parse_logical_and_expression(ctx
, &p
)) {
1407 if (_parse_token(ctx
, SL_PP_XOR
, &p
)) {
1410 if (_parse_logical_and_expression(ctx
, &p
)) {
1413 _emit(ctx
, &p
.out
, OP_LOGICALXOR
);
1419 _parse_logical_or_expression(struct parse_context
*ctx
,
1420 struct parse_state
*ps
)
1422 struct parse_state p
= *ps
;
1424 if (_parse_logical_xor_expression(ctx
, &p
)) {
1429 if (_parse_token(ctx
, SL_PP_OR
, &p
)) {
1432 if (_parse_logical_xor_expression(ctx
, &p
)) {
1435 _emit(ctx
, &p
.out
, OP_LOGICALOR
);
1441 _parse_conditional_expression(struct parse_context
*ctx
,
1442 struct parse_state
*ps
)
1444 struct parse_state p
= *ps
;
1446 if (_parse_logical_or_expression(ctx
, &p
)) {
1451 if (_parse_token(ctx
, SL_PP_QUESTION
, &p
)) {
1454 if (_parse_expression(ctx
, &p
)) {
1457 if (_parse_token(ctx
, SL_PP_COLON
, &p
)) {
1460 if (_parse_conditional_expression(ctx
, &p
)) {
1463 _emit(ctx
, &p
.out
, OP_SELECT
);
1469 _parse_constant_expression(struct parse_context
*ctx
,
1470 struct parse_state
*ps
)
1472 if (_parse_conditional_expression(ctx
, ps
)) {
1475 _emit(ctx
, &ps
->out
, OP_END
);
1481 _parse_parameter_declarator_array(struct parse_context
*ctx
,
1482 struct parse_state
*ps
)
1484 struct parse_state p
= *ps
;
1486 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
1489 if (_parse_constant_expression(ctx
, &p
)) {
1492 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1501 _parse_parameter_declarator(struct parse_context
*ctx
,
1502 struct parse_state
*ps
)
1504 struct parse_state p
= *ps
;
1507 if (_parse_type_specifier(ctx
, &p
)) {
1510 if (_parse_identifier(ctx
, &p
)) {
1513 e
= _emit(ctx
, &p
.out
, PARAMETER_ARRAY_PRESENT
);
1514 if (_parse_parameter_declarator_array(ctx
, &p
)) {
1515 _update(ctx
, e
, PARAMETER_ARRAY_NOT_PRESENT
);
1523 _parse_parameter_type_specifier_array(struct parse_context
*ctx
,
1524 struct parse_state
*ps
)
1526 struct parse_state p
= *ps
;
1528 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
1531 if (_parse_constant_expression(ctx
, &p
)) {
1534 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1543 _parse_parameter_type_specifier(struct parse_context
*ctx
,
1544 struct parse_state
*ps
)
1546 struct parse_state p
= *ps
;
1549 if (_parse_type_specifier(ctx
, &p
)) {
1552 _emit(ctx
, &p
.out
, '\0');
1554 e
= _emit(ctx
, &p
.out
, PARAMETER_ARRAY_PRESENT
);
1555 if (_parse_parameter_type_specifier_array(ctx
, &p
)) {
1556 _update(ctx
, e
, PARAMETER_ARRAY_NOT_PRESENT
);
1564 _parse_parameter_declaration(struct parse_context
*ctx
,
1565 struct parse_state
*ps
)
1567 struct parse_state p
= *ps
;
1568 unsigned int e
= _emit(ctx
, &p
.out
, PARAMETER_NEXT
);
1570 if (_parse_type_qualifier(ctx
, &p
)) {
1571 _emit(ctx
, &p
.out
, TYPE_QUALIFIER_NONE
);
1573 _parse_parameter_qualifier(ctx
, &p
);
1574 if (_parse_precision(ctx
, &p
)) {
1575 _emit(ctx
, &p
.out
, PRECISION_DEFAULT
);
1577 if (_parse_parameter_declarator(ctx
, &p
) == 0) {
1581 if (_parse_parameter_type_specifier(ctx
, &p
) == 0) {
1591 _parse_function_header_with_parameters(struct parse_context
*ctx
,
1592 struct parse_state
*ps
)
1594 struct parse_state p
= *ps
;
1596 if (_parse_function_header(ctx
, &p
)) {
1599 if (_parse_parameter_declaration(ctx
, &p
)) {
1605 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1608 if (_parse_parameter_declaration(ctx
, &p
)) {
1616 _parse_function_declarator(struct parse_context
*ctx
,
1617 struct parse_state
*ps
)
1619 if (_parse_function_header_with_parameters(ctx
, ps
) == 0) {
1623 if (_parse_function_header(ctx
, ps
) == 0) {
1632 _parse_function_prototype(struct parse_context
*ctx
,
1633 struct parse_state
*ps
)
1635 struct parse_state p
= *ps
;
1637 if (_parse_function_header(ctx
, &p
) == 0) {
1638 if (_parse_id(ctx
, ctx
->dict
._void
, &p
) == 0) {
1639 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1640 _emit(ctx
, &p
.out
, PARAMETER_NONE
);
1648 if (_parse_function_declarator(ctx
, &p
) == 0) {
1649 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1650 _emit(ctx
, &p
.out
, PARAMETER_NONE
);
1661 _parse_precision(struct parse_context
*ctx
,
1662 struct parse_state
*ps
)
1665 unsigned int precision
;
1667 if (ctx
->input
[ps
->in
].token
!= SL_PP_IDENTIFIER
) {
1670 id
= ctx
->input
[ps
->in
].data
.identifier
;
1672 if (id
== ctx
->dict
.lowp
) {
1673 precision
= PRECISION_LOW
;
1674 } else if (id
== ctx
->dict
.mediump
) {
1675 precision
= PRECISION_MEDIUM
;
1676 } else if (id
== ctx
->dict
.highp
) {
1677 precision
= PRECISION_HIGH
;
1682 _parse_token(ctx
, SL_PP_IDENTIFIER
, ps
);
1683 _emit(ctx
, &ps
->out
, precision
);
1689 _parse_prectype(struct parse_context
*ctx
,
1690 struct parse_state
*ps
)
1695 if (ctx
->input
[ps
->in
].token
!= SL_PP_IDENTIFIER
) {
1698 id
= ctx
->input
[ps
->in
].data
.identifier
;
1700 if (id
== ctx
->dict
._int
) {
1701 type
= TYPE_SPECIFIER_INT
;
1702 } else if (id
== ctx
->dict
._float
) {
1703 type
= TYPE_SPECIFIER_FLOAT
;
1704 } else if (id
== ctx
->dict
.sampler1D
) {
1705 type
= TYPE_SPECIFIER_SAMPLER1D
;
1706 } else if (id
== ctx
->dict
.sampler2D
) {
1707 type
= TYPE_SPECIFIER_SAMPLER2D
;
1708 } else if (id
== ctx
->dict
.sampler3D
) {
1709 type
= TYPE_SPECIFIER_SAMPLER3D
;
1710 } else if (id
== ctx
->dict
.samplerCube
) {
1711 type
= TYPE_SPECIFIER_SAMPLERCUBE
;
1712 } else if (id
== ctx
->dict
.sampler1DShadow
) {
1713 type
= TYPE_SPECIFIER_SAMPLER1DSHADOW
;
1714 } else if (id
== ctx
->dict
.sampler2DShadow
) {
1715 type
= TYPE_SPECIFIER_SAMPLER2DSHADOW
;
1716 } else if (id
== ctx
->dict
.sampler2DRect
) {
1717 type
= TYPE_SPECIFIER_SAMPLER2DRECT
;
1718 } else if (id
== ctx
->dict
.sampler2DRectShadow
) {
1719 type
= TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
;
1724 _parse_token(ctx
, SL_PP_IDENTIFIER
, ps
);
1725 _emit(ctx
, &ps
->out
, type
);
1731 _parse_precision_stmt(struct parse_context
*ctx
,
1732 struct parse_state
*ps
)
1734 struct parse_state p
= *ps
;
1736 if (_parse_id(ctx
, ctx
->dict
.precision
, &p
)) {
1739 if (_parse_precision(ctx
, &p
)) {
1742 if (_parse_prectype(ctx
, &p
)) {
1745 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
1754 _parse_floatconstant(struct parse_context
*ctx
,
1755 struct parse_state
*ps
)
1757 struct parse_state p
= *ps
;
1759 _emit(ctx
, &p
.out
, OP_PUSH_FLOAT
);
1760 if (_parse_float(ctx
, &p
)) {
1769 _parse_intconstant(struct parse_context
*ctx
,
1770 struct parse_state
*ps
)
1772 struct parse_state p
= *ps
;
1774 _emit(ctx
, &p
.out
, OP_PUSH_INT
);
1775 if (_parse_uint(ctx
, &p
)) {
1784 _parse_boolconstant(struct parse_context
*ctx
,
1785 struct parse_state
*ps
)
1787 if (_parse_id(ctx
, ctx
->dict
._false
, ps
) == 0) {
1788 _emit(ctx
, &ps
->out
, OP_PUSH_BOOL
);
1789 _emit(ctx
, &ps
->out
, '0');
1790 _emit(ctx
, &ps
->out
, '\0');
1794 if (_parse_id(ctx
, ctx
->dict
._true
, ps
) == 0) {
1795 _emit(ctx
, &ps
->out
, OP_PUSH_BOOL
);
1796 _emit(ctx
, &ps
->out
, '1');
1797 _emit(ctx
, &ps
->out
, '\0');
1806 _parse_variable_identifier(struct parse_context
*ctx
,
1807 struct parse_state
*ps
)
1809 struct parse_state p
= *ps
;
1811 _emit(ctx
, &p
.out
, OP_PUSH_IDENTIFIER
);
1812 if (_parse_identifier(ctx
, &p
)) {
1821 _parse_primary_expression(struct parse_context
*ctx
,
1822 struct parse_state
*ps
)
1824 struct parse_state p
;
1826 if (_parse_floatconstant(ctx
, ps
) == 0) {
1829 if (_parse_boolconstant(ctx
, ps
) == 0) {
1832 if (_parse_intconstant(ctx
, ps
) == 0) {
1835 if (_parse_variable_identifier(ctx
, ps
) == 0) {
1840 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
1843 if (_parse_expression(ctx
, &p
)) {
1846 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
1856 _parse_asm_argument(struct parse_context
*ctx
,
1857 struct parse_state
*ps
)
1859 if (_parse_variable_identifier(ctx
, ps
) == 0) {
1860 struct parse_state p
= *ps
;
1862 if (_parse_token(ctx
, SL_PP_DOT
, &p
)) {
1865 _emit(ctx
, &p
.out
, OP_FIELD
);
1866 if (_parse_identifier(ctx
, &p
)) {
1873 if (_parse_floatconstant(ctx
, ps
) == 0) {
1882 _parse_asm_arguments(struct parse_context
*ctx
,
1883 struct parse_state
*ps
)
1885 struct parse_state p
= *ps
;
1887 if (_parse_asm_argument(ctx
, &p
)) {
1890 _emit(ctx
, &p
.out
, OP_END
);
1894 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1897 if (_parse_asm_argument(ctx
, &p
)) {
1900 _emit(ctx
, &p
.out
, OP_END
);
1906 _parse_asm_statement(struct parse_context
*ctx
,
1907 struct parse_state
*ps
)
1909 struct parse_state p
= *ps
;
1911 if (_parse_id(ctx
, ctx
->dict
.___asm
, &p
)) {
1914 if (_parse_identifier(ctx
, &p
)) {
1917 if (_parse_asm_arguments(ctx
, &p
)) {
1920 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
1923 _emit(ctx
, &p
.out
, OP_END
);
1930 _parse_selection_statement(struct parse_context
*ctx
,
1931 struct parse_state
*ps
)
1933 struct parse_state p
= *ps
;
1935 _emit(ctx
, &p
.out
, OP_IF
);
1936 if (_parse_id(ctx
, ctx
->dict
._if
, &p
)) {
1939 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
1942 if (_parse_expression(ctx
, &p
)) {
1945 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
1948 _emit(ctx
, &p
.out
, OP_END
);
1949 if (_parse_statement(ctx
, &p
)) {
1954 if (_parse_id(ctx
, ctx
->dict
._else
, &p
) == 0) {
1955 if (_parse_statement(ctx
, &p
) == 0) {
1961 _emit(ctx
, &ps
->out
, OP_EXPRESSION
);
1962 _emit(ctx
, &ps
->out
, OP_PUSH_VOID
);
1963 _emit(ctx
, &ps
->out
, OP_END
);
1969 _parse_expression_statement(struct parse_context
*ctx
,
1970 struct parse_state
*ps
)
1972 struct parse_state p
= *ps
;
1974 if (_parse_expression(ctx
, &p
)) {
1975 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
1977 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
1980 _emit(ctx
, &p
.out
, OP_END
);
1987 _parse_for_init_statement(struct parse_context
*ctx
,
1988 struct parse_state
*ps
)
1990 struct parse_state p
= *ps
;
1991 unsigned int e
= _emit(ctx
, &p
.out
, OP_EXPRESSION
);
1993 if (_parse_expression_statement(ctx
, &p
) == 0) {
1998 if (_parse_declaration(ctx
, &p
) == 0) {
1999 _update(ctx
, e
, OP_DECLARE
);
2009 _parse_initializer(struct parse_context
*ctx
,
2010 struct parse_state
*ps
)
2012 if (_parse_assignment_expression(ctx
, ps
) == 0) {
2013 _emit(ctx
, &ps
->out
, OP_END
);
2021 _parse_condition_initializer(struct parse_context
*ctx
,
2022 struct parse_state
*ps
)
2024 struct parse_state p
= *ps
;
2026 _emit(ctx
, &p
.out
, OP_DECLARE
);
2027 _emit(ctx
, &p
.out
, DECLARATION_INIT_DECLARATOR_LIST
);
2028 if (_parse_fully_specified_type(ctx
, &p
)) {
2031 _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2032 if (_parse_identifier(ctx
, &p
)) {
2035 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
)) {
2038 _emit(ctx
, &p
.out
, VARIABLE_INITIALIZER
);
2039 if (_parse_initializer(ctx
, &p
)) {
2042 _emit(ctx
, &p
.out
, DECLARATOR_NONE
);
2049 _parse_condition(struct parse_context
*ctx
,
2050 struct parse_state
*ps
)
2052 struct parse_state p
;
2054 if (_parse_condition_initializer(ctx
, ps
) == 0) {
2059 _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2060 if (_parse_expression(ctx
, &p
) == 0) {
2061 _emit(ctx
, &p
.out
, OP_END
);
2071 _parse_for_rest_statement(struct parse_context
*ctx
,
2072 struct parse_state
*ps
)
2074 struct parse_state p
= *ps
;
2076 if (_parse_condition(ctx
, &p
)) {
2077 _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2078 _emit(ctx
, &p
.out
, OP_PUSH_BOOL
);
2079 _emit(ctx
, &p
.out
, 2);
2080 _emit(ctx
, &p
.out
, '1');
2081 _emit(ctx
, &p
.out
, '\0');
2082 _emit(ctx
, &p
.out
, OP_END
);
2084 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2087 if (_parse_expression(ctx
, &p
)) {
2088 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
2090 _emit(ctx
, &p
.out
, OP_END
);
2097 _parse_iteration_statement(struct parse_context
*ctx
,
2098 struct parse_state
*ps
)
2100 struct parse_state p
= *ps
;
2102 if (_parse_id(ctx
, ctx
->dict
._while
, &p
) == 0) {
2103 _emit(ctx
, &p
.out
, OP_WHILE
);
2104 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2107 if (_parse_condition(ctx
, &p
)) {
2110 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2113 if (_parse_statement(ctx
, &p
)) {
2120 if (_parse_id(ctx
, ctx
->dict
._do
, &p
) == 0) {
2121 _emit(ctx
, &p
.out
, OP_DO
);
2122 if (_parse_statement(ctx
, &p
)) {
2125 if (_parse_id(ctx
, ctx
->dict
._while
, &p
)) {
2128 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2131 if (_parse_expression(ctx
, &p
)) {
2134 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2137 _emit(ctx
, &p
.out
, OP_END
);
2138 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2145 if (_parse_id(ctx
, ctx
->dict
._for
, &p
) == 0) {
2146 _emit(ctx
, &p
.out
, OP_FOR
);
2147 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2150 if (_parse_for_init_statement(ctx
, &p
)) {
2153 if (_parse_for_rest_statement(ctx
, &p
)) {
2156 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2159 if (_parse_statement(ctx
, &p
)) {
2171 _parse_jump_statement(struct parse_context
*ctx
,
2172 struct parse_state
*ps
)
2174 struct parse_state p
= *ps
;
2175 unsigned int e
= _emit(ctx
, &p
.out
, 0);
2177 if (_parse_id(ctx
, ctx
->dict
._continue
, &p
) == 0) {
2178 _update(ctx
, e
, OP_CONTINUE
);
2179 } else if (_parse_id(ctx
, ctx
->dict
._break
, &p
) == 0) {
2180 _update(ctx
, e
, OP_BREAK
);
2181 } else if (_parse_id(ctx
, ctx
->dict
._return
, &p
) == 0) {
2182 _update(ctx
, e
, OP_RETURN
);
2183 if (_parse_expression(ctx
, &p
)) {
2184 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
2186 _emit(ctx
, &p
.out
, OP_END
);
2187 } else if (ctx
->shader_type
== 1 && _parse_id(ctx
, ctx
->dict
.discard
, &p
) == 0) {
2188 _update(ctx
, e
, OP_DISCARD
);
2192 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2201 _parse_simple_statement(struct parse_context
*ctx
,
2202 struct parse_state
*ps
)
2204 struct parse_state p
;
2207 if (_parse_selection_statement(ctx
, ps
) == 0) {
2211 if (_parse_iteration_statement(ctx
, ps
) == 0) {
2215 if (_parse_jump_statement(ctx
, ps
) == 0) {
2220 e
= _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2221 if (_parse_expression_statement(ctx
, &p
) == 0) {
2226 if (_parse_precision_stmt(ctx
, &p
) == 0) {
2227 _update(ctx
, e
, OP_PRECISION
);
2232 if (ctx
->parsing_builtin
&& _parse_asm_statement(ctx
, &p
) == 0) {
2233 _update(ctx
, e
, OP_ASM
);
2238 if (_parse_declaration(ctx
, &p
) == 0) {
2239 _update(ctx
, e
, OP_DECLARE
);
2249 _parse_compound_statement(struct parse_context
*ctx
,
2250 struct parse_state
*ps
)
2252 struct parse_state p
= *ps
;
2254 if (_parse_token(ctx
, SL_PP_LBRACE
, &p
)) {
2257 _emit(ctx
, &p
.out
, OP_BLOCK_BEGIN_NEW_SCOPE
);
2258 _parse_statement_list(ctx
, &p
);
2259 if (_parse_token(ctx
, SL_PP_RBRACE
, &p
)) {
2262 _emit(ctx
, &p
.out
, OP_END
);
2269 _parse_statement(struct parse_context
*ctx
,
2270 struct parse_state
*ps
)
2272 if (_parse_compound_statement(ctx
, ps
) == 0) {
2276 if (_parse_simple_statement(ctx
, ps
) == 0) {
2285 _parse_statement_list(struct parse_context
*ctx
,
2286 struct parse_state
*ps
)
2288 struct parse_state p
= *ps
;
2290 if (_parse_statement(ctx
, &p
)) {
2296 if (_parse_statement(ctx
, &p
)) {
2304 _parse_compound_statement_no_new_scope(struct parse_context
*ctx
,
2305 struct parse_state
*ps
)
2307 struct parse_state p
= *ps
;
2309 if (_parse_token(ctx
, SL_PP_LBRACE
, &p
)) {
2312 _emit(ctx
, &p
.out
, OP_BLOCK_BEGIN_NO_NEW_SCOPE
);
2313 _parse_statement_list(ctx
, &p
);
2314 if (_parse_token(ctx
, SL_PP_RBRACE
, &p
)) {
2317 _emit(ctx
, &p
.out
, OP_END
);
2324 _parse_function_definition(struct parse_context
*ctx
,
2325 struct parse_state
*ps
)
2327 struct parse_state p
= *ps
;
2329 if (_parse_function_prototype(ctx
, &p
)) {
2332 if (_parse_compound_statement_no_new_scope(ctx
, &p
)) {
2341 _parse_invariant_stmt(struct parse_context
*ctx
,
2342 struct parse_state
*ps
)
2344 struct parse_state p
= *ps
;
2346 if (_parse_id(ctx
, ctx
->dict
.invariant
, &p
)) {
2349 if (_parse_identifier(ctx
, &p
)) {
2352 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2361 _parse_single_declaration(struct parse_context
*ctx
,
2362 struct parse_state
*ps
)
2364 struct parse_state p
= *ps
;
2367 if (_parse_fully_specified_type(ctx
, &p
)) {
2371 e
= _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2372 if (_parse_identifier(ctx
, &p
)) {
2373 _update(ctx
, e
, VARIABLE_NONE
);
2378 e
= _emit(ctx
, &p
.out
, VARIABLE_NONE
);
2381 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
) == 0) {
2382 _update(ctx
, e
, VARIABLE_INITIALIZER
);
2383 if (_parse_initializer(ctx
, &p
) == 0) {
2390 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
) == 0) {
2391 if (_parse_constant_expression(ctx
, &p
)) {
2392 _update(ctx
, e
, VARIABLE_ARRAY_UNKNOWN
);
2394 _update(ctx
, e
, VARIABLE_ARRAY_EXPLICIT
);
2396 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
) == 0) {
2406 _parse_init_declarator_list(struct parse_context
*ctx
,
2407 struct parse_state
*ps
)
2409 struct parse_state p
= *ps
;
2411 if (_parse_single_declaration(ctx
, &p
)) {
2419 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
2422 _emit(ctx
, &p
.out
, DECLARATOR_NEXT
);
2423 _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2424 if (_parse_identifier(ctx
, &p
)) {
2428 e
= _emit(ctx
, &p
.out
, VARIABLE_NONE
);
2431 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
) == 0) {
2432 if (_parse_initializer(ctx
, &p
) == 0) {
2433 _update(ctx
, e
, VARIABLE_INITIALIZER
);
2440 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
) == 0) {
2443 if (_parse_constant_expression(ctx
, &p
)) {
2444 arr
= VARIABLE_ARRAY_UNKNOWN
;
2446 arr
= VARIABLE_ARRAY_EXPLICIT
;
2448 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
) == 0) {
2449 _update(ctx
, e
, arr
);
2457 _emit(ctx
, &ps
->out
, DECLARATOR_NONE
);
2463 _parse_declaration(struct parse_context
*ctx
,
2464 struct parse_state
*ps
)
2466 struct parse_state p
= *ps
;
2467 unsigned int e
= _emit(ctx
, &p
.out
, DECLARATION_FUNCTION_PROTOTYPE
);
2469 if (_parse_function_prototype(ctx
, &p
)) {
2470 if (_parse_init_declarator_list(ctx
, &p
)) {
2473 _update(ctx
, e
, DECLARATION_INIT_DECLARATOR_LIST
);
2475 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2484 _parse_external_declaration(struct parse_context
*ctx
,
2485 struct parse_state
*ps
)
2487 struct parse_state p
= *ps
;
2488 unsigned int e
= _emit(ctx
, &p
.out
, 0);
2490 if (_parse_precision_stmt(ctx
, &p
) == 0) {
2491 _update(ctx
, e
, DEFAULT_PRECISION
);
2496 if (_parse_function_definition(ctx
, &p
) == 0) {
2497 _update(ctx
, e
, EXTERNAL_FUNCTION_DEFINITION
);
2502 if (_parse_invariant_stmt(ctx
, &p
) == 0) {
2503 _update(ctx
, e
, INVARIANT_STMT
);
2508 if (_parse_declaration(ctx
, &p
) == 0) {
2509 _update(ctx
, e
, EXTERNAL_DECLARATION
);
2519 _parse_translation_unit(struct parse_context
*ctx
,
2520 struct parse_state
*ps
)
2522 _emit(ctx
, &ps
->out
, REVISION
);
2523 if (_parse_external_declaration(ctx
, ps
)) {
2526 while (_parse_external_declaration(ctx
, ps
) == 0) {
2528 _emit(ctx
, &ps
->out
, EXTERNAL_NULL
);
2529 if (_parse_token(ctx
, SL_PP_EOF
, ps
)) {
2536 #define ADD_NAME_STR(CTX, NAME, STR)\
2538 (CTX).dict.NAME = sl_pp_context_add_unique_str((CTX).context, (STR));\
2539 if ((CTX).dict.NAME == -1) {\
2544 #define ADD_NAME(CTX, NAME) ADD_NAME_STR(CTX, NAME, #NAME)
2548 sl_cl_compile(struct sl_pp_context
*context
,
2549 const struct sl_pp_token_info
*input
,
2550 unsigned int shader_type
,
2551 unsigned char **output
,
2552 unsigned int *cboutput
)
2554 struct parse_context ctx
;
2555 struct parse_state ps
;
2557 ctx
.context
= context
;
2560 ADD_NAME_STR(ctx
, _void
, "void");
2561 ADD_NAME_STR(ctx
, _float
, "float");
2562 ADD_NAME_STR(ctx
, _int
, "int");
2563 ADD_NAME_STR(ctx
, _bool
, "bool");
2564 ADD_NAME(ctx
, vec2
);
2565 ADD_NAME(ctx
, vec3
);
2566 ADD_NAME(ctx
, vec4
);
2567 ADD_NAME(ctx
, bvec2
);
2568 ADD_NAME(ctx
, bvec3
);
2569 ADD_NAME(ctx
, bvec4
);
2570 ADD_NAME(ctx
, ivec2
);
2571 ADD_NAME(ctx
, ivec3
);
2572 ADD_NAME(ctx
, ivec4
);
2573 ADD_NAME(ctx
, mat2
);
2574 ADD_NAME(ctx
, mat3
);
2575 ADD_NAME(ctx
, mat4
);
2576 ADD_NAME(ctx
, mat2x3
);
2577 ADD_NAME(ctx
, mat3x2
);
2578 ADD_NAME(ctx
, mat2x4
);
2579 ADD_NAME(ctx
, mat4x2
);
2580 ADD_NAME(ctx
, mat3x4
);
2581 ADD_NAME(ctx
, mat4x3
);
2582 ADD_NAME(ctx
, sampler1D
);
2583 ADD_NAME(ctx
, sampler2D
);
2584 ADD_NAME(ctx
, sampler3D
);
2585 ADD_NAME(ctx
, samplerCube
);
2586 ADD_NAME(ctx
, sampler1DShadow
);
2587 ADD_NAME(ctx
, sampler2DShadow
);
2588 ADD_NAME(ctx
, sampler2DRect
);
2589 ADD_NAME(ctx
, sampler2DRectShadow
);
2591 ADD_NAME(ctx
, invariant
);
2593 ADD_NAME(ctx
, centroid
);
2595 ADD_NAME(ctx
, precision
);
2596 ADD_NAME(ctx
, lowp
);
2597 ADD_NAME(ctx
, mediump
);
2598 ADD_NAME(ctx
, highp
);
2600 ADD_NAME_STR(ctx
, _const
, "const");
2601 ADD_NAME(ctx
, attribute
);
2602 ADD_NAME(ctx
, varying
);
2603 ADD_NAME(ctx
, uniform
);
2604 ADD_NAME(ctx
, __fixed_output
);
2605 ADD_NAME(ctx
, __fixed_input
);
2609 ADD_NAME(ctx
, inout
);
2611 ADD_NAME_STR(ctx
, _struct
, "struct");
2613 ADD_NAME(ctx
, __constructor
);
2614 ADD_NAME(ctx
, __operator
);
2615 ADD_NAME_STR(ctx
, ___asm
, "__asm");
2617 ADD_NAME_STR(ctx
, _if
, "if");
2618 ADD_NAME_STR(ctx
, _else
, "else");
2619 ADD_NAME_STR(ctx
, _for
, "for");
2620 ADD_NAME_STR(ctx
, _while
, "while");
2621 ADD_NAME_STR(ctx
, _do
, "do");
2623 ADD_NAME_STR(ctx
, _continue
, "continue");
2624 ADD_NAME_STR(ctx
, _break
, "break");
2625 ADD_NAME_STR(ctx
, _return
, "return");
2626 ADD_NAME(ctx
, discard
);
2628 ADD_NAME_STR(ctx
, _false
, "false");
2629 ADD_NAME_STR(ctx
, _true
, "true");
2634 ctx
.shader_type
= shader_type
;
2635 ctx
.parsing_builtin
= 1;
2640 if (_parse_translation_unit(&ctx
, &ps
)) {
2644 *output
= ctx
.out_buf
;