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
;
344 static __inline
unsigned int
345 _emit(struct parse_context
*ctx
,
349 if (*out
== ctx
->out_cap
) {
350 ctx
->out_cap
+= 4096;
351 ctx
->out_buf
= (unsigned char *)realloc(ctx
->out_buf
, ctx
->out_cap
* sizeof(unsigned char));
353 ctx
->out_buf
[*out
] = b
;
359 _update(struct parse_context
*ctx
,
363 ctx
->out_buf
[out
] = b
;
368 _error(struct parse_context
*ctx
,
371 if (ctx
->error
[0] == '\0') {
372 strcpy(ctx
->error
, msg
);
378 _parse_token(struct parse_context
*ctx
,
379 enum sl_pp_token token
,
380 struct parse_state
*ps
)
382 if (ctx
->input
[ps
->in
].token
== token
) {
391 _parse_id(struct parse_context
*ctx
,
393 struct parse_state
*ps
)
395 if (ctx
->input
[ps
->in
].token
== SL_PP_IDENTIFIER
&&
396 ctx
->input
[ps
->in
].data
.identifier
== id
) {
405 _parse_identifier(struct parse_context
*ctx
,
406 struct parse_state
*ps
)
408 if (ctx
->input
[ps
->in
].token
== SL_PP_IDENTIFIER
) {
409 const char *cstr
= sl_pp_context_cstr(ctx
->context
, ctx
->input
[ps
->in
].data
.identifier
);
412 _emit(ctx
, &ps
->out
, *cstr
);
422 _parse_float(struct parse_context
*ctx
,
423 struct parse_state
*ps
)
425 if (ctx
->input
[ps
->in
].token
== SL_PP_FLOAT
) {
426 const char *cstr
= sl_pp_context_cstr(ctx
->context
, ctx
->input
[ps
->in
].data
._float
);
428 _emit(ctx
, &ps
->out
, 1);
430 _emit(ctx
, &ps
->out
, *cstr
);
440 _parse_uint(struct parse_context
*ctx
,
441 struct parse_state
*ps
)
443 if (ctx
->input
[ps
->in
].token
== SL_PP_UINT
) {
444 const char *cstr
= sl_pp_context_cstr(ctx
->context
, ctx
->input
[ps
->in
].data
._uint
);
446 _emit(ctx
, &ps
->out
, 1);
448 _emit(ctx
, &ps
->out
, *cstr
);
457 /**************************************/
461 _parse_unary_expression(struct parse_context
*ctx
,
462 struct parse_state
*ps
);
465 _parse_conditional_expression(struct parse_context
*ctx
,
466 struct parse_state
*ps
);
470 _parse_constant_expression(struct parse_context
*ctx
,
471 struct parse_state
*ps
);
475 _parse_primary_expression(struct parse_context
*ctx
,
476 struct parse_state
*ps
);
480 _parse_statement(struct parse_context
*ctx
,
481 struct parse_state
*ps
);
485 _parse_type_specifier(struct parse_context
*ctx
,
486 struct parse_state
*ps
);
490 _parse_declaration(struct parse_context
*ctx
,
491 struct parse_state
*ps
);
495 _parse_statement_list(struct parse_context
*ctx
,
496 struct parse_state
*ps
);
500 _parse_assignment_expression(struct parse_context
*ctx
,
501 struct parse_state
*ps
);
505 _parse_precision(struct parse_context
*ctx
,
506 struct parse_state
*ps
);
510 _parse_overriden_operator(struct parse_context
*ctx
,
511 struct parse_state
*ps
)
515 if (_parse_token(ctx
, SL_PP_INCREMENT
, ps
) == 0) {
516 op
= OPERATOR_INCREMENT
;
517 } else if (_parse_token(ctx
, SL_PP_ADDASSIGN
, ps
) == 0) {
518 op
= OPERATOR_ADDASSIGN
;
519 } else if (_parse_token(ctx
, SL_PP_PLUS
, ps
) == 0) {
521 } else if (_parse_token(ctx
, SL_PP_DECREMENT
, ps
) == 0) {
522 op
= OPERATOR_DECREMENT
;
523 } else if (_parse_token(ctx
, SL_PP_SUBASSIGN
, ps
) == 0) {
524 op
= OPERATOR_SUBASSIGN
;
525 } else if (_parse_token(ctx
, SL_PP_MINUS
, ps
) == 0) {
527 } else if (_parse_token(ctx
, SL_PP_NOT
, ps
) == 0) {
529 } else if (_parse_token(ctx
, SL_PP_MULASSIGN
, ps
) == 0) {
530 op
= OPERATOR_MULASSIGN
;
531 } else if (_parse_token(ctx
, SL_PP_STAR
, ps
) == 0) {
532 op
= OPERATOR_MULTIPLY
;
533 } else if (_parse_token(ctx
, SL_PP_DIVASSIGN
, ps
) == 0) {
534 op
= OPERATOR_DIVASSIGN
;
535 } else if (_parse_token(ctx
, SL_PP_SLASH
, ps
) == 0) {
536 op
= OPERATOR_DIVIDE
;
537 } else if (_parse_token(ctx
, SL_PP_LESSEQUAL
, ps
) == 0) {
538 op
= OPERATOR_LESSEQUAL
;
539 } else if (_parse_token(ctx
, SL_PP_LESS
, ps
) == 0) {
541 } else if (_parse_token(ctx
, SL_PP_GREATEREQUAL
, ps
) == 0) {
542 op
= OPERATOR_GREATEREQUAL
;
543 } else if (_parse_token(ctx
, SL_PP_GREATER
, ps
) == 0) {
544 op
= OPERATOR_GREATER
;
545 } else if (_parse_token(ctx
, SL_PP_XOR
, ps
) == 0) {
546 op
= OPERATOR_LOGICALXOR
;
551 _emit(ctx
, &ps
->out
, op
);
557 _parse_function_decl_identifier(struct parse_context
*ctx
,
558 struct parse_state
*ps
)
560 struct parse_state p
= *ps
;
561 unsigned int e
= _emit(ctx
, &p
.out
, 0);
563 if (ctx
->parsing_builtin
&& _parse_id(ctx
, ctx
->dict
.__constructor
, &p
) == 0) {
564 _update(ctx
, e
, FUNCTION_CONSTRUCTOR
);
569 if (ctx
->parsing_builtin
&& _parse_id(ctx
, ctx
->dict
.__operator
, &p
) == 0) {
570 _update(ctx
, e
, FUNCTION_OPERATOR
);
571 if (_parse_overriden_operator(ctx
, &p
) == 0) {
578 if (_parse_identifier(ctx
, &p
) == 0) {
579 _update(ctx
, e
, FUNCTION_ORDINARY
);
589 _parse_invariant_qualifier(struct parse_context
*ctx
,
590 struct parse_state
*ps
)
592 if (_parse_id(ctx
, ctx
->dict
.invariant
, ps
)) {
595 _emit(ctx
, &ps
->out
, TYPE_INVARIANT
);
601 _parse_centroid_qualifier(struct parse_context
*ctx
,
602 struct parse_state
*ps
)
604 if (_parse_id(ctx
, ctx
->dict
.centroid
, ps
)) {
607 _emit(ctx
, &ps
->out
, TYPE_CENTROID
);
613 _parse_type_qualifier(struct parse_context
*ctx
,
614 struct parse_state
*ps
)
616 struct parse_state p
= *ps
;
617 unsigned int e
= _emit(ctx
, &p
.out
, 0);
620 if (ctx
->input
[p
.in
].token
!= SL_PP_IDENTIFIER
) {
623 id
= ctx
->input
[p
.in
].data
.identifier
;
625 if (id
== ctx
->dict
._const
) {
626 _update(ctx
, e
, TYPE_QUALIFIER_CONST
);
627 } else if (ctx
->shader_type
== 2 && id
== ctx
->dict
.attribute
) {
628 _update(ctx
, e
, TYPE_QUALIFIER_ATTRIBUTE
);
629 } else if (id
== ctx
->dict
.varying
) {
630 _update(ctx
, e
, TYPE_QUALIFIER_VARYING
);
631 } else if (id
== ctx
->dict
.uniform
) {
632 _update(ctx
, e
, TYPE_QUALIFIER_UNIFORM
);
633 } else if (ctx
->parsing_builtin
&& id
== ctx
->dict
.__fixed_output
) {
634 _update(ctx
, e
, TYPE_QUALIFIER_FIXEDOUTPUT
);
635 } else if (ctx
->parsing_builtin
&& id
== ctx
->dict
.__fixed_input
) {
636 _update(ctx
, e
, TYPE_QUALIFIER_FIXEDINPUT
);
640 _parse_token(ctx
, SL_PP_IDENTIFIER
, &p
);
647 _parse_struct_declarator(struct parse_context
*ctx
,
648 struct parse_state
*ps
)
650 struct parse_state p
= *ps
;
653 if (_parse_identifier(ctx
, &p
)) {
656 e
= _emit(ctx
, &p
.out
, FIELD_NONE
);
659 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
662 if (_parse_constant_expression(ctx
, &p
)) {
663 _error(ctx
, "expected constant integral expression");
666 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
667 _error(ctx
, "expected `]'");
670 _update(ctx
, e
, FIELD_ARRAY
);
677 _parse_struct_declarator_list(struct parse_context
*ctx
,
678 struct parse_state
*ps
)
680 struct parse_state p
= *ps
;
682 if (_parse_struct_declarator(ctx
, &p
)) {
688 _emit(ctx
, &p
.out
, FIELD_NEXT
);
689 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
692 if (_parse_struct_declarator(ctx
, &p
)) {
700 _parse_struct_declaration(struct parse_context
*ctx
,
701 struct parse_state
*ps
)
703 struct parse_state p
= *ps
;
705 if (_parse_type_specifier(ctx
, &p
)) {
708 if (_parse_struct_declarator_list(ctx
, &p
)) {
711 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
714 _emit(ctx
, &p
.out
, FIELD_NONE
);
721 _parse_struct_declaration_list(struct parse_context
*ctx
,
722 struct parse_state
*ps
)
724 struct parse_state p
= *ps
;
726 if (_parse_struct_declaration(ctx
, &p
)) {
732 _emit(ctx
, &p
.out
, FIELD_NEXT
);
733 if (_parse_struct_declaration(ctx
, &p
)) {
741 _parse_struct_specifier(struct parse_context
*ctx
,
742 struct parse_state
*ps
)
744 struct parse_state p
= *ps
;
746 if (_parse_id(ctx
, ctx
->dict
._struct
, &p
)) {
749 if (_parse_identifier(ctx
, &p
)) {
750 _emit(ctx
, &p
.out
, '\0');
752 if (_parse_token(ctx
, SL_PP_LBRACE
, &p
)) {
753 _error(ctx
, "expected `{'");
756 if (_parse_struct_declaration_list(ctx
, &p
)) {
759 if (_parse_token(ctx
, SL_PP_RBRACE
, &p
)) {
762 _emit(ctx
, &p
.out
, FIELD_NONE
);
769 _parse_type_specifier_nonarray(struct parse_context
*ctx
,
770 struct parse_state
*ps
)
772 struct parse_state p
= *ps
;
773 unsigned int e
= _emit(ctx
, &p
.out
, 0);
776 if (_parse_struct_specifier(ctx
, &p
) == 0) {
777 _update(ctx
, e
, TYPE_SPECIFIER_STRUCT
);
782 if (ctx
->input
[p
.in
].token
!= SL_PP_IDENTIFIER
) {
785 id
= ctx
->input
[p
.in
].data
.identifier
;
787 if (id
== ctx
->dict
._void
) {
788 _update(ctx
, e
, TYPE_SPECIFIER_VOID
);
789 } else if (id
== ctx
->dict
._float
) {
790 _update(ctx
, e
, TYPE_SPECIFIER_FLOAT
);
791 } else if (id
== ctx
->dict
._int
) {
792 _update(ctx
, e
, TYPE_SPECIFIER_INT
);
793 } else if (id
== ctx
->dict
._bool
) {
794 _update(ctx
, e
, TYPE_SPECIFIER_BOOL
);
795 } else if (id
== ctx
->dict
.vec2
) {
796 _update(ctx
, e
, TYPE_SPECIFIER_VEC2
);
797 } else if (id
== ctx
->dict
.vec3
) {
798 _update(ctx
, e
, TYPE_SPECIFIER_VEC3
);
799 } else if (id
== ctx
->dict
.vec4
) {
800 _update(ctx
, e
, TYPE_SPECIFIER_VEC4
);
801 } else if (id
== ctx
->dict
.bvec2
) {
802 _update(ctx
, e
, TYPE_SPECIFIER_BVEC2
);
803 } else if (id
== ctx
->dict
.bvec3
) {
804 _update(ctx
, e
, TYPE_SPECIFIER_BVEC3
);
805 } else if (id
== ctx
->dict
.bvec4
) {
806 _update(ctx
, e
, TYPE_SPECIFIER_BVEC4
);
807 } else if (id
== ctx
->dict
.ivec2
) {
808 _update(ctx
, e
, TYPE_SPECIFIER_IVEC2
);
809 } else if (id
== ctx
->dict
.ivec3
) {
810 _update(ctx
, e
, TYPE_SPECIFIER_IVEC3
);
811 } else if (id
== ctx
->dict
.ivec4
) {
812 _update(ctx
, e
, TYPE_SPECIFIER_IVEC4
);
813 } else if (id
== ctx
->dict
.mat2
) {
814 _update(ctx
, e
, TYPE_SPECIFIER_MAT2
);
815 } else if (id
== ctx
->dict
.mat3
) {
816 _update(ctx
, e
, TYPE_SPECIFIER_MAT3
);
817 } else if (id
== ctx
->dict
.mat4
) {
818 _update(ctx
, e
, TYPE_SPECIFIER_MAT4
);
819 } else if (id
== ctx
->dict
.mat2x3
) {
820 _update(ctx
, e
, TYPE_SPECIFIER_MAT23
);
821 } else if (id
== ctx
->dict
.mat3x2
) {
822 _update(ctx
, e
, TYPE_SPECIFIER_MAT32
);
823 } else if (id
== ctx
->dict
.mat2x4
) {
824 _update(ctx
, e
, TYPE_SPECIFIER_MAT24
);
825 } else if (id
== ctx
->dict
.mat4x2
) {
826 _update(ctx
, e
, TYPE_SPECIFIER_MAT42
);
827 } else if (id
== ctx
->dict
.mat3x4
) {
828 _update(ctx
, e
, TYPE_SPECIFIER_MAT34
);
829 } else if (id
== ctx
->dict
.mat4x3
) {
830 _update(ctx
, e
, TYPE_SPECIFIER_MAT43
);
831 } else if (id
== ctx
->dict
.sampler1D
) {
832 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER1D
);
833 } else if (id
== ctx
->dict
.sampler2D
) {
834 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2D
);
835 } else if (id
== ctx
->dict
.sampler3D
) {
836 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER3D
);
837 } else if (id
== ctx
->dict
.samplerCube
) {
838 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLERCUBE
);
839 } else if (id
== ctx
->dict
.sampler1DShadow
) {
840 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER1DSHADOW
);
841 } else if (id
== ctx
->dict
.sampler2DShadow
) {
842 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2DSHADOW
);
843 } else if (id
== ctx
->dict
.sampler2DRect
) {
844 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2DRECT
);
845 } else if (id
== ctx
->dict
.sampler2DRectShadow
) {
846 _update(ctx
, e
, TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
);
847 } else if (_parse_identifier(ctx
, &p
) == 0) {
848 _update(ctx
, e
, TYPE_SPECIFIER_TYPENAME
);
855 _parse_token(ctx
, SL_PP_IDENTIFIER
, &p
);
862 _parse_type_specifier_array(struct parse_context
*ctx
,
863 struct parse_state
*ps
)
865 struct parse_state p
= *ps
;
867 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
870 if (_parse_constant_expression(ctx
, &p
)) {
871 _error(ctx
, "expected constant integral expression");
874 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
875 _error(ctx
, "expected `]'");
884 _parse_type_specifier(struct parse_context
*ctx
,
885 struct parse_state
*ps
)
887 struct parse_state p
= *ps
;
890 if (_parse_type_specifier_nonarray(ctx
, &p
)) {
894 e
= _emit(ctx
, &p
.out
, TYPE_SPECIFIER_ARRAY
);
895 if (_parse_type_specifier_array(ctx
, &p
)) {
896 _update(ctx
, e
, TYPE_SPECIFIER_NONARRAY
);
904 _parse_fully_specified_type(struct parse_context
*ctx
,
905 struct parse_state
*ps
)
907 struct parse_state p
= *ps
;
909 if (_parse_invariant_qualifier(ctx
, &p
)) {
910 _emit(ctx
, &p
.out
, TYPE_VARIANT
);
912 if (_parse_centroid_qualifier(ctx
, &p
)) {
913 _emit(ctx
, &p
.out
, TYPE_CENTER
);
915 if (_parse_type_qualifier(ctx
, &p
)) {
916 _emit(ctx
, &p
.out
, TYPE_QUALIFIER_NONE
);
918 if (_parse_precision(ctx
, &p
)) {
919 _emit(ctx
, &p
.out
, PRECISION_DEFAULT
);
921 if (_parse_type_specifier(ctx
, &p
)) {
930 _parse_function_header(struct parse_context
*ctx
,
931 struct parse_state
*ps
)
933 struct parse_state p
= *ps
;
935 if (_parse_fully_specified_type(ctx
, &p
)) {
938 if (_parse_function_decl_identifier(ctx
, &p
)) {
941 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
950 _parse_parameter_qualifier(struct parse_context
*ctx
,
951 struct parse_state
*ps
)
953 unsigned int e
= _emit(ctx
, &ps
->out
, PARAM_QUALIFIER_IN
);
955 if (_parse_id(ctx
, ctx
->dict
.out
, ps
) == 0) {
956 _update(ctx
, e
, PARAM_QUALIFIER_OUT
);
957 } else if (_parse_id(ctx
, ctx
->dict
.inout
, ps
) == 0) {
958 _update(ctx
, e
, PARAM_QUALIFIER_INOUT
);
960 _parse_id(ctx
, ctx
->dict
.in
, ps
);
967 _parse_function_identifier(struct parse_context
*ctx
,
968 struct parse_state
*ps
)
970 struct parse_state p
;
973 if (_parse_identifier(ctx
, ps
)) {
976 e
= _emit(ctx
, &ps
->out
, FUNCTION_CALL_NONARRAY
);
979 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
982 if (_parse_constant_expression(ctx
, &p
)) {
983 _error(ctx
, "expected constant integral expression");
986 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
987 _error(ctx
, "expected `]'");
990 _update(ctx
, e
, FUNCTION_CALL_ARRAY
);
997 _parse_function_call_header(struct parse_context
*ctx
,
998 struct parse_state
*ps
)
1000 struct parse_state p
= *ps
;
1002 if (_parse_function_identifier(ctx
, &p
)) {
1005 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
1014 _parse_assign_expression(struct parse_context
*ctx
,
1015 struct parse_state
*ps
)
1017 struct parse_state p
= *ps
;
1020 if (_parse_unary_expression(ctx
, &p
)) {
1024 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
) == 0) {
1026 } else if (_parse_token(ctx
, SL_PP_MULASSIGN
, &p
) == 0) {
1028 } else if (_parse_token(ctx
, SL_PP_DIVASSIGN
, &p
) == 0) {
1030 } else if (_parse_token(ctx
, SL_PP_ADDASSIGN
, &p
) == 0) {
1032 } else if (_parse_token(ctx
, SL_PP_SUBASSIGN
, &p
) == 0) {
1038 if (_parse_assignment_expression(ctx
, &p
)) {
1041 _emit(ctx
, &p
.out
, op
);
1049 _parse_assignment_expression(struct parse_context
*ctx
,
1050 struct parse_state
*ps
)
1052 if (_parse_assign_expression(ctx
, ps
) == 0) {
1056 if (_parse_conditional_expression(ctx
, ps
) == 0) {
1065 _parse_function_call_header_with_parameters(struct parse_context
*ctx
,
1066 struct parse_state
*ps
)
1068 struct parse_state p
= *ps
;
1070 if (_parse_function_call_header(ctx
, &p
)) {
1073 if (_parse_assignment_expression(ctx
, &p
)) {
1076 _emit(ctx
, &p
.out
, OP_END
);
1079 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1082 if (_parse_assignment_expression(ctx
, &p
)) {
1085 _emit(ctx
, &p
.out
, OP_END
);
1091 _parse_function_call_header_no_parameters(struct parse_context
*ctx
,
1092 struct parse_state
*ps
)
1094 if (_parse_function_call_header(ctx
, ps
)) {
1097 _parse_id(ctx
, ctx
->dict
._void
, ps
);
1103 _parse_function_call_generic(struct parse_context
*ctx
,
1104 struct parse_state
*ps
)
1106 struct parse_state p
= *ps
;
1108 if (_parse_function_call_header_with_parameters(ctx
, &p
) == 0) {
1109 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1113 _error(ctx
, "expected `)'");
1118 if (_parse_function_call_header_no_parameters(ctx
, &p
) == 0) {
1119 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1123 _error(ctx
, "expected `)'");
1132 _parse_method_call(struct parse_context
*ctx
,
1133 struct parse_state
*ps
)
1135 struct parse_state p
= *ps
;
1137 _emit(ctx
, &p
.out
, OP_METHOD
);
1138 if (_parse_identifier(ctx
, &p
)) {
1141 if (_parse_token(ctx
, SL_PP_DOT
, &p
)) {
1144 if (_parse_function_call_generic(ctx
, &p
)) {
1147 _emit(ctx
, &p
.out
, OP_END
);
1154 _parse_regular_function_call(struct parse_context
*ctx
,
1155 struct parse_state
*ps
)
1157 struct parse_state p
= *ps
;
1159 _emit(ctx
, &p
.out
, OP_CALL
);
1160 if (_parse_function_call_generic(ctx
, &p
)) {
1163 _emit(ctx
, &p
.out
, OP_END
);
1170 _parse_function_call(struct parse_context
*ctx
,
1171 struct parse_state
*ps
)
1173 if (_parse_regular_function_call(ctx
, ps
) == 0) {
1177 if (_parse_method_call(ctx
, ps
) == 0) {
1186 _parse_expression(struct parse_context
*ctx
,
1187 struct parse_state
*ps
)
1189 struct parse_state p
= *ps
;
1191 if (_parse_assignment_expression(ctx
, &p
)) {
1197 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1200 if (_parse_assignment_expression(ctx
, &p
)) {
1203 _emit(ctx
, &p
.out
, OP_SEQUENCE
);
1209 _parse_postfix_expression(struct parse_context
*ctx
,
1210 struct parse_state
*ps
)
1212 struct parse_state p
;
1214 if (_parse_function_call(ctx
, ps
)) {
1215 if (_parse_primary_expression(ctx
, ps
)) {
1222 if (_parse_token(ctx
, SL_PP_INCREMENT
, &p
) == 0) {
1223 _emit(ctx
, &p
.out
, OP_POSTINCREMENT
);
1224 } else if (_parse_token(ctx
, SL_PP_DECREMENT
, &p
) == 0) {
1225 _emit(ctx
, &p
.out
, OP_POSTDECREMENT
);
1226 } else if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
) == 0) {
1227 if (_parse_expression(ctx
, &p
)) {
1228 _error(ctx
, "expected an integral expression");
1231 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1232 _error(ctx
, "expected `]'");
1235 _emit(ctx
, &p
.out
, OP_SUBSCRIPT
);
1236 } else if (_parse_token(ctx
, SL_PP_DOT
, &p
) == 0) {
1237 _emit(ctx
, &p
.out
, OP_FIELD
);
1238 if (_parse_identifier(ctx
, &p
)) {
1249 _parse_unary_expression(struct parse_context
*ctx
,
1250 struct parse_state
*ps
)
1252 struct parse_state p
;
1255 if (_parse_postfix_expression(ctx
, ps
) == 0) {
1260 if (_parse_token(ctx
, SL_PP_INCREMENT
, &p
) == 0) {
1261 op
= OP_PREINCREMENT
;
1262 } else if (_parse_token(ctx
, SL_PP_DECREMENT
, &p
) == 0) {
1263 op
= OP_PREDECREMENT
;
1264 } else if (_parse_token(ctx
, SL_PP_PLUS
, &p
) == 0) {
1266 } else if (_parse_token(ctx
, SL_PP_MINUS
, &p
) == 0) {
1268 } else if (_parse_token(ctx
, SL_PP_NOT
, &p
) == 0) {
1274 if (_parse_unary_expression(ctx
, &p
)) {
1277 _emit(ctx
, &p
.out
, op
);
1284 _parse_multiplicative_expression(struct parse_context
*ctx
,
1285 struct parse_state
*ps
)
1287 struct parse_state p
= *ps
;
1289 if (_parse_unary_expression(ctx
, &p
)) {
1296 if (_parse_token(ctx
, SL_PP_STAR
, &p
) == 0) {
1298 } else if (_parse_token(ctx
, SL_PP_SLASH
, &p
) == 0) {
1303 if (_parse_unary_expression(ctx
, &p
)) {
1306 _emit(ctx
, &p
.out
, op
);
1312 _parse_additive_expression(struct parse_context
*ctx
,
1313 struct parse_state
*ps
)
1315 struct parse_state p
= *ps
;
1317 if (_parse_multiplicative_expression(ctx
, &p
)) {
1324 if (_parse_token(ctx
, SL_PP_PLUS
, &p
) == 0) {
1326 } else if (_parse_token(ctx
, SL_PP_MINUS
, &p
) == 0) {
1331 if (_parse_multiplicative_expression(ctx
, &p
)) {
1334 _emit(ctx
, &p
.out
, op
);
1340 _parse_relational_expression(struct parse_context
*ctx
,
1341 struct parse_state
*ps
)
1343 struct parse_state p
= *ps
;
1345 if (_parse_additive_expression(ctx
, &p
)) {
1352 if (_parse_token(ctx
, SL_PP_LESS
, &p
) == 0) {
1354 } else if (_parse_token(ctx
, SL_PP_GREATER
, &p
) == 0) {
1356 } else if (_parse_token(ctx
, SL_PP_LESSEQUAL
, &p
) == 0) {
1358 } else if (_parse_token(ctx
, SL_PP_GREATEREQUAL
, &p
) == 0) {
1359 op
= OP_GREATEREQUAL
;
1363 if (_parse_additive_expression(ctx
, &p
)) {
1366 _emit(ctx
, &p
.out
, op
);
1372 _parse_equality_expression(struct parse_context
*ctx
,
1373 struct parse_state
*ps
)
1375 struct parse_state p
= *ps
;
1377 if (_parse_relational_expression(ctx
, &p
)) {
1384 if (_parse_token(ctx
, SL_PP_EQUAL
, &p
) == 0) {
1386 } else if (_parse_token(ctx
, SL_PP_NOTEQUAL
, &p
) == 0) {
1391 if (_parse_relational_expression(ctx
, &p
)) {
1394 _emit(ctx
, &p
.out
, op
);
1400 _parse_logical_and_expression(struct parse_context
*ctx
,
1401 struct parse_state
*ps
)
1403 struct parse_state p
= *ps
;
1405 if (_parse_equality_expression(ctx
, &p
)) {
1410 if (_parse_token(ctx
, SL_PP_AND
, &p
)) {
1413 if (_parse_equality_expression(ctx
, &p
)) {
1416 _emit(ctx
, &p
.out
, OP_LOGICALAND
);
1422 _parse_logical_xor_expression(struct parse_context
*ctx
,
1423 struct parse_state
*ps
)
1425 struct parse_state p
= *ps
;
1427 if (_parse_logical_and_expression(ctx
, &p
)) {
1432 if (_parse_token(ctx
, SL_PP_XOR
, &p
)) {
1435 if (_parse_logical_and_expression(ctx
, &p
)) {
1438 _emit(ctx
, &p
.out
, OP_LOGICALXOR
);
1444 _parse_logical_or_expression(struct parse_context
*ctx
,
1445 struct parse_state
*ps
)
1447 struct parse_state p
= *ps
;
1449 if (_parse_logical_xor_expression(ctx
, &p
)) {
1454 if (_parse_token(ctx
, SL_PP_OR
, &p
)) {
1457 if (_parse_logical_xor_expression(ctx
, &p
)) {
1460 _emit(ctx
, &p
.out
, OP_LOGICALOR
);
1466 _parse_conditional_expression(struct parse_context
*ctx
,
1467 struct parse_state
*ps
)
1469 struct parse_state p
= *ps
;
1471 if (_parse_logical_or_expression(ctx
, &p
)) {
1476 if (_parse_token(ctx
, SL_PP_QUESTION
, &p
)) {
1479 if (_parse_expression(ctx
, &p
)) {
1482 if (_parse_token(ctx
, SL_PP_COLON
, &p
)) {
1485 if (_parse_conditional_expression(ctx
, &p
)) {
1488 _emit(ctx
, &p
.out
, OP_SELECT
);
1494 _parse_constant_expression(struct parse_context
*ctx
,
1495 struct parse_state
*ps
)
1497 if (_parse_conditional_expression(ctx
, ps
)) {
1500 _emit(ctx
, &ps
->out
, OP_END
);
1506 _parse_parameter_declarator_array(struct parse_context
*ctx
,
1507 struct parse_state
*ps
)
1509 struct parse_state p
= *ps
;
1511 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
1514 if (_parse_constant_expression(ctx
, &p
)) {
1515 _error(ctx
, "expected constant integral expression");
1518 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1519 _error(ctx
, "expected `]'");
1528 _parse_parameter_declarator(struct parse_context
*ctx
,
1529 struct parse_state
*ps
)
1531 struct parse_state p
= *ps
;
1534 if (_parse_type_specifier(ctx
, &p
)) {
1537 if (_parse_identifier(ctx
, &p
)) {
1540 e
= _emit(ctx
, &p
.out
, PARAMETER_ARRAY_PRESENT
);
1541 if (_parse_parameter_declarator_array(ctx
, &p
)) {
1542 _update(ctx
, e
, PARAMETER_ARRAY_NOT_PRESENT
);
1550 _parse_parameter_type_specifier_array(struct parse_context
*ctx
,
1551 struct parse_state
*ps
)
1553 struct parse_state p
= *ps
;
1555 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
)) {
1558 if (_parse_constant_expression(ctx
, &p
)) {
1559 _error(ctx
, "expected constant integral expression");
1562 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
)) {
1563 _error(ctx
, "expected `]'");
1572 _parse_parameter_type_specifier(struct parse_context
*ctx
,
1573 struct parse_state
*ps
)
1575 struct parse_state p
= *ps
;
1578 if (_parse_type_specifier(ctx
, &p
)) {
1581 _emit(ctx
, &p
.out
, '\0');
1583 e
= _emit(ctx
, &p
.out
, PARAMETER_ARRAY_PRESENT
);
1584 if (_parse_parameter_type_specifier_array(ctx
, &p
)) {
1585 _update(ctx
, e
, PARAMETER_ARRAY_NOT_PRESENT
);
1593 _parse_parameter_declaration(struct parse_context
*ctx
,
1594 struct parse_state
*ps
)
1596 struct parse_state p
= *ps
;
1597 unsigned int e
= _emit(ctx
, &p
.out
, PARAMETER_NEXT
);
1601 if (_parse_type_qualifier(ctx
, &p
)) {
1602 _emit(ctx
, &p
.out
, TYPE_QUALIFIER_NONE
);
1604 _parse_parameter_qualifier(ctx
, &p
);
1605 if (_parse_precision(ctx
, &p
)) {
1606 _emit(ctx
, &p
.out
, PRECISION_DEFAULT
);
1608 if (_parse_parameter_declarator(ctx
, &p
) == 0) {
1612 if (_parse_parameter_type_specifier(ctx
, &p
) == 0) {
1622 _parse_function_header_with_parameters(struct parse_context
*ctx
,
1623 struct parse_state
*ps
)
1625 struct parse_state p
= *ps
;
1627 if (_parse_function_header(ctx
, &p
)) {
1630 if (_parse_parameter_declaration(ctx
, &p
)) {
1636 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1639 if (_parse_parameter_declaration(ctx
, &p
)) {
1647 _parse_function_declarator(struct parse_context
*ctx
,
1648 struct parse_state
*ps
)
1650 if (_parse_function_header_with_parameters(ctx
, ps
) == 0) {
1654 if (_parse_function_header(ctx
, ps
) == 0) {
1663 _parse_function_prototype(struct parse_context
*ctx
,
1664 struct parse_state
*ps
)
1666 struct parse_state p
= *ps
;
1668 if (_parse_function_header(ctx
, &p
) == 0) {
1669 if (_parse_id(ctx
, ctx
->dict
._void
, &p
) == 0) {
1670 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1671 _emit(ctx
, &p
.out
, PARAMETER_NONE
);
1675 _error(ctx
, "expected `)'");
1681 if (_parse_function_declarator(ctx
, &p
) == 0) {
1682 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1683 _emit(ctx
, &p
.out
, PARAMETER_NONE
);
1687 _error(ctx
, "expected `)'");
1696 _parse_precision(struct parse_context
*ctx
,
1697 struct parse_state
*ps
)
1700 unsigned int precision
;
1702 if (ctx
->input
[ps
->in
].token
!= SL_PP_IDENTIFIER
) {
1705 id
= ctx
->input
[ps
->in
].data
.identifier
;
1707 if (id
== ctx
->dict
.lowp
) {
1708 precision
= PRECISION_LOW
;
1709 } else if (id
== ctx
->dict
.mediump
) {
1710 precision
= PRECISION_MEDIUM
;
1711 } else if (id
== ctx
->dict
.highp
) {
1712 precision
= PRECISION_HIGH
;
1717 _parse_token(ctx
, SL_PP_IDENTIFIER
, ps
);
1718 _emit(ctx
, &ps
->out
, precision
);
1724 _parse_prectype(struct parse_context
*ctx
,
1725 struct parse_state
*ps
)
1730 if (ctx
->input
[ps
->in
].token
!= SL_PP_IDENTIFIER
) {
1733 id
= ctx
->input
[ps
->in
].data
.identifier
;
1735 if (id
== ctx
->dict
._int
) {
1736 type
= TYPE_SPECIFIER_INT
;
1737 } else if (id
== ctx
->dict
._float
) {
1738 type
= TYPE_SPECIFIER_FLOAT
;
1739 } else if (id
== ctx
->dict
.sampler1D
) {
1740 type
= TYPE_SPECIFIER_SAMPLER1D
;
1741 } else if (id
== ctx
->dict
.sampler2D
) {
1742 type
= TYPE_SPECIFIER_SAMPLER2D
;
1743 } else if (id
== ctx
->dict
.sampler3D
) {
1744 type
= TYPE_SPECIFIER_SAMPLER3D
;
1745 } else if (id
== ctx
->dict
.samplerCube
) {
1746 type
= TYPE_SPECIFIER_SAMPLERCUBE
;
1747 } else if (id
== ctx
->dict
.sampler1DShadow
) {
1748 type
= TYPE_SPECIFIER_SAMPLER1DSHADOW
;
1749 } else if (id
== ctx
->dict
.sampler2DShadow
) {
1750 type
= TYPE_SPECIFIER_SAMPLER2DSHADOW
;
1751 } else if (id
== ctx
->dict
.sampler2DRect
) {
1752 type
= TYPE_SPECIFIER_SAMPLER2DRECT
;
1753 } else if (id
== ctx
->dict
.sampler2DRectShadow
) {
1754 type
= TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
;
1759 _parse_token(ctx
, SL_PP_IDENTIFIER
, ps
);
1760 _emit(ctx
, &ps
->out
, type
);
1766 _parse_precision_stmt(struct parse_context
*ctx
,
1767 struct parse_state
*ps
)
1769 struct parse_state p
= *ps
;
1771 if (_parse_id(ctx
, ctx
->dict
.precision
, &p
)) {
1774 if (_parse_precision(ctx
, &p
)) {
1777 if (_parse_prectype(ctx
, &p
)) {
1780 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
1789 _parse_floatconstant(struct parse_context
*ctx
,
1790 struct parse_state
*ps
)
1792 struct parse_state p
= *ps
;
1794 _emit(ctx
, &p
.out
, OP_PUSH_FLOAT
);
1795 if (_parse_float(ctx
, &p
)) {
1804 _parse_intconstant(struct parse_context
*ctx
,
1805 struct parse_state
*ps
)
1807 struct parse_state p
= *ps
;
1809 _emit(ctx
, &p
.out
, OP_PUSH_INT
);
1810 if (_parse_uint(ctx
, &p
)) {
1819 _parse_boolconstant(struct parse_context
*ctx
,
1820 struct parse_state
*ps
)
1822 if (_parse_id(ctx
, ctx
->dict
._false
, ps
) == 0) {
1823 _emit(ctx
, &ps
->out
, OP_PUSH_BOOL
);
1824 _emit(ctx
, &ps
->out
, '0');
1825 _emit(ctx
, &ps
->out
, '\0');
1829 if (_parse_id(ctx
, ctx
->dict
._true
, ps
) == 0) {
1830 _emit(ctx
, &ps
->out
, OP_PUSH_BOOL
);
1831 _emit(ctx
, &ps
->out
, '1');
1832 _emit(ctx
, &ps
->out
, '\0');
1841 _parse_variable_identifier(struct parse_context
*ctx
,
1842 struct parse_state
*ps
)
1844 struct parse_state p
= *ps
;
1846 _emit(ctx
, &p
.out
, OP_PUSH_IDENTIFIER
);
1847 if (_parse_identifier(ctx
, &p
)) {
1856 _parse_primary_expression(struct parse_context
*ctx
,
1857 struct parse_state
*ps
)
1859 struct parse_state p
;
1861 if (_parse_floatconstant(ctx
, ps
) == 0) {
1864 if (_parse_boolconstant(ctx
, ps
) == 0) {
1867 if (_parse_intconstant(ctx
, ps
) == 0) {
1870 if (_parse_variable_identifier(ctx
, ps
) == 0) {
1875 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
1878 if (_parse_expression(ctx
, &p
)) {
1881 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
1891 _parse_asm_argument(struct parse_context
*ctx
,
1892 struct parse_state
*ps
)
1894 if (_parse_variable_identifier(ctx
, ps
) == 0) {
1895 struct parse_state p
= *ps
;
1897 if (_parse_token(ctx
, SL_PP_DOT
, &p
)) {
1900 _emit(ctx
, &p
.out
, OP_FIELD
);
1901 if (_parse_identifier(ctx
, &p
)) {
1908 if (_parse_floatconstant(ctx
, ps
) == 0) {
1917 _parse_asm_arguments(struct parse_context
*ctx
,
1918 struct parse_state
*ps
)
1920 struct parse_state p
= *ps
;
1922 if (_parse_asm_argument(ctx
, &p
)) {
1925 _emit(ctx
, &p
.out
, OP_END
);
1929 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1932 if (_parse_asm_argument(ctx
, &p
)) {
1935 _emit(ctx
, &p
.out
, OP_END
);
1941 _parse_asm_statement(struct parse_context
*ctx
,
1942 struct parse_state
*ps
)
1944 struct parse_state p
= *ps
;
1946 if (_parse_id(ctx
, ctx
->dict
.___asm
, &p
)) {
1949 if (_parse_identifier(ctx
, &p
)) {
1952 if (_parse_asm_arguments(ctx
, &p
)) {
1955 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
1958 _emit(ctx
, &p
.out
, OP_END
);
1965 _parse_selection_statement(struct parse_context
*ctx
,
1966 struct parse_state
*ps
)
1968 struct parse_state p
= *ps
;
1970 _emit(ctx
, &p
.out
, OP_IF
);
1971 if (_parse_id(ctx
, ctx
->dict
._if
, &p
)) {
1974 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
1975 _error(ctx
, "expected `('");
1978 if (_parse_expression(ctx
, &p
)) {
1979 _error(ctx
, "expected an expression");
1982 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
1983 _error(ctx
, "expected `)'");
1986 _emit(ctx
, &p
.out
, OP_END
);
1987 if (_parse_statement(ctx
, &p
)) {
1992 if (_parse_id(ctx
, ctx
->dict
._else
, &p
) == 0) {
1993 if (_parse_statement(ctx
, &p
) == 0) {
1999 _emit(ctx
, &ps
->out
, OP_EXPRESSION
);
2000 _emit(ctx
, &ps
->out
, OP_PUSH_VOID
);
2001 _emit(ctx
, &ps
->out
, OP_END
);
2007 _parse_expression_statement(struct parse_context
*ctx
,
2008 struct parse_state
*ps
)
2010 struct parse_state p
= *ps
;
2012 if (_parse_expression(ctx
, &p
)) {
2013 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
2015 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2018 _emit(ctx
, &p
.out
, OP_END
);
2025 _parse_for_init_statement(struct parse_context
*ctx
,
2026 struct parse_state
*ps
)
2028 struct parse_state p
= *ps
;
2029 unsigned int e
= _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2031 if (_parse_expression_statement(ctx
, &p
) == 0) {
2036 if (_parse_declaration(ctx
, &p
) == 0) {
2037 _update(ctx
, e
, OP_DECLARE
);
2047 _parse_initializer(struct parse_context
*ctx
,
2048 struct parse_state
*ps
)
2050 if (_parse_assignment_expression(ctx
, ps
) == 0) {
2051 _emit(ctx
, &ps
->out
, OP_END
);
2059 _parse_condition_initializer(struct parse_context
*ctx
,
2060 struct parse_state
*ps
)
2062 struct parse_state p
= *ps
;
2064 _emit(ctx
, &p
.out
, OP_DECLARE
);
2065 _emit(ctx
, &p
.out
, DECLARATION_INIT_DECLARATOR_LIST
);
2066 if (_parse_fully_specified_type(ctx
, &p
)) {
2069 _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2070 if (_parse_identifier(ctx
, &p
)) {
2073 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
)) {
2074 _error(ctx
, "expected `='");
2077 _emit(ctx
, &p
.out
, VARIABLE_INITIALIZER
);
2078 if (_parse_initializer(ctx
, &p
)) {
2079 _error(ctx
, "expected an initialiser");
2082 _emit(ctx
, &p
.out
, DECLARATOR_NONE
);
2089 _parse_condition(struct parse_context
*ctx
,
2090 struct parse_state
*ps
)
2092 struct parse_state p
;
2094 if (_parse_condition_initializer(ctx
, ps
) == 0) {
2099 _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2100 if (_parse_expression(ctx
, &p
) == 0) {
2101 _emit(ctx
, &p
.out
, OP_END
);
2111 _parse_for_rest_statement(struct parse_context
*ctx
,
2112 struct parse_state
*ps
)
2114 struct parse_state p
= *ps
;
2116 if (_parse_condition(ctx
, &p
)) {
2117 _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2118 _emit(ctx
, &p
.out
, OP_PUSH_BOOL
);
2119 _emit(ctx
, &p
.out
, 2);
2120 _emit(ctx
, &p
.out
, '1');
2121 _emit(ctx
, &p
.out
, '\0');
2122 _emit(ctx
, &p
.out
, OP_END
);
2124 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2127 if (_parse_expression(ctx
, &p
)) {
2128 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
2130 _emit(ctx
, &p
.out
, OP_END
);
2137 _parse_iteration_statement(struct parse_context
*ctx
,
2138 struct parse_state
*ps
)
2140 struct parse_state p
= *ps
;
2142 if (_parse_id(ctx
, ctx
->dict
._while
, &p
) == 0) {
2143 _emit(ctx
, &p
.out
, OP_WHILE
);
2144 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2145 _error(ctx
, "expected `('");
2148 if (_parse_condition(ctx
, &p
)) {
2149 _error(ctx
, "expected an expression");
2152 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2153 _error(ctx
, "expected `)'");
2156 if (_parse_statement(ctx
, &p
)) {
2163 if (_parse_id(ctx
, ctx
->dict
._do
, &p
) == 0) {
2164 _emit(ctx
, &p
.out
, OP_DO
);
2165 if (_parse_statement(ctx
, &p
)) {
2168 if (_parse_id(ctx
, ctx
->dict
._while
, &p
)) {
2171 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2172 _error(ctx
, "expected `('");
2175 if (_parse_expression(ctx
, &p
)) {
2176 _error(ctx
, "expected an expression");
2179 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2180 _error(ctx
, "expected `)'");
2183 _emit(ctx
, &p
.out
, OP_END
);
2184 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2185 _error(ctx
, "expected `;'");
2192 if (_parse_id(ctx
, ctx
->dict
._for
, &p
) == 0) {
2193 _emit(ctx
, &p
.out
, OP_FOR
);
2194 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2195 _error(ctx
, "expected `('");
2198 if (_parse_for_init_statement(ctx
, &p
)) {
2201 if (_parse_for_rest_statement(ctx
, &p
)) {
2204 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2205 _error(ctx
, "expected `)'");
2208 if (_parse_statement(ctx
, &p
)) {
2220 _parse_jump_statement(struct parse_context
*ctx
,
2221 struct parse_state
*ps
)
2223 struct parse_state p
= *ps
;
2224 unsigned int e
= _emit(ctx
, &p
.out
, 0);
2226 if (_parse_id(ctx
, ctx
->dict
._continue
, &p
) == 0) {
2227 _update(ctx
, e
, OP_CONTINUE
);
2228 } else if (_parse_id(ctx
, ctx
->dict
._break
, &p
) == 0) {
2229 _update(ctx
, e
, OP_BREAK
);
2230 } else if (_parse_id(ctx
, ctx
->dict
._return
, &p
) == 0) {
2231 _update(ctx
, e
, OP_RETURN
);
2232 if (_parse_expression(ctx
, &p
)) {
2233 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
2235 _emit(ctx
, &p
.out
, OP_END
);
2236 } else if (ctx
->shader_type
== 1 && _parse_id(ctx
, ctx
->dict
.discard
, &p
) == 0) {
2237 _update(ctx
, e
, OP_DISCARD
);
2241 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2250 _parse_simple_statement(struct parse_context
*ctx
,
2251 struct parse_state
*ps
)
2253 struct parse_state p
;
2256 if (_parse_selection_statement(ctx
, ps
) == 0) {
2260 if (_parse_iteration_statement(ctx
, ps
) == 0) {
2264 if (_parse_jump_statement(ctx
, ps
) == 0) {
2269 e
= _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2270 if (_parse_expression_statement(ctx
, &p
) == 0) {
2275 if (_parse_precision_stmt(ctx
, &p
) == 0) {
2276 _update(ctx
, e
, OP_PRECISION
);
2281 if (ctx
->parsing_builtin
&& _parse_asm_statement(ctx
, &p
) == 0) {
2282 _update(ctx
, e
, OP_ASM
);
2287 if (_parse_declaration(ctx
, &p
) == 0) {
2288 _update(ctx
, e
, OP_DECLARE
);
2298 _parse_compound_statement(struct parse_context
*ctx
,
2299 struct parse_state
*ps
)
2301 struct parse_state p
= *ps
;
2303 if (_parse_token(ctx
, SL_PP_LBRACE
, &p
)) {
2306 _emit(ctx
, &p
.out
, OP_BLOCK_BEGIN_NEW_SCOPE
);
2307 _parse_statement_list(ctx
, &p
);
2308 if (_parse_token(ctx
, SL_PP_RBRACE
, &p
)) {
2311 _emit(ctx
, &p
.out
, OP_END
);
2318 _parse_statement(struct parse_context
*ctx
,
2319 struct parse_state
*ps
)
2321 if (_parse_compound_statement(ctx
, ps
) == 0) {
2325 if (_parse_simple_statement(ctx
, ps
) == 0) {
2334 _parse_statement_list(struct parse_context
*ctx
,
2335 struct parse_state
*ps
)
2337 struct parse_state p
= *ps
;
2339 if (_parse_statement(ctx
, &p
)) {
2345 if (_parse_statement(ctx
, &p
)) {
2353 _parse_compound_statement_no_new_scope(struct parse_context
*ctx
,
2354 struct parse_state
*ps
)
2356 struct parse_state p
= *ps
;
2358 if (_parse_token(ctx
, SL_PP_LBRACE
, &p
)) {
2361 _emit(ctx
, &p
.out
, OP_BLOCK_BEGIN_NO_NEW_SCOPE
);
2362 _parse_statement_list(ctx
, &p
);
2363 if (_parse_token(ctx
, SL_PP_RBRACE
, &p
)) {
2366 _emit(ctx
, &p
.out
, OP_END
);
2373 _parse_function_definition(struct parse_context
*ctx
,
2374 struct parse_state
*ps
)
2376 struct parse_state p
= *ps
;
2378 if (_parse_function_prototype(ctx
, &p
)) {
2381 if (_parse_compound_statement_no_new_scope(ctx
, &p
)) {
2390 _parse_invariant_stmt(struct parse_context
*ctx
,
2391 struct parse_state
*ps
)
2393 struct parse_state p
= *ps
;
2395 if (_parse_id(ctx
, ctx
->dict
.invariant
, &p
)) {
2398 if (_parse_identifier(ctx
, &p
)) {
2401 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2410 _parse_single_declaration(struct parse_context
*ctx
,
2411 struct parse_state
*ps
)
2413 struct parse_state p
= *ps
;
2416 if (_parse_fully_specified_type(ctx
, &p
)) {
2420 e
= _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2421 if (_parse_identifier(ctx
, &p
)) {
2422 _update(ctx
, e
, VARIABLE_NONE
);
2427 e
= _emit(ctx
, &p
.out
, VARIABLE_NONE
);
2430 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
) == 0) {
2431 _update(ctx
, e
, VARIABLE_INITIALIZER
);
2432 if (_parse_initializer(ctx
, &p
) == 0) {
2436 _error(ctx
, "expected an initialiser");
2441 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
) == 0) {
2442 if (_parse_constant_expression(ctx
, &p
)) {
2443 _update(ctx
, e
, VARIABLE_ARRAY_UNKNOWN
);
2445 _update(ctx
, e
, VARIABLE_ARRAY_EXPLICIT
);
2447 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
) == 0) {
2451 _error(ctx
, "expected `]'");
2459 _parse_init_declarator_list(struct parse_context
*ctx
,
2460 struct parse_state
*ps
)
2462 struct parse_state p
= *ps
;
2464 if (_parse_single_declaration(ctx
, &p
)) {
2472 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
2475 _emit(ctx
, &p
.out
, DECLARATOR_NEXT
);
2476 _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2477 if (_parse_identifier(ctx
, &p
)) {
2481 e
= _emit(ctx
, &p
.out
, VARIABLE_NONE
);
2484 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
) == 0) {
2485 if (_parse_initializer(ctx
, &p
) == 0) {
2486 _update(ctx
, e
, VARIABLE_INITIALIZER
);
2490 _error(ctx
, "expected an initialiser");
2495 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
) == 0) {
2498 if (_parse_constant_expression(ctx
, &p
)) {
2499 arr
= VARIABLE_ARRAY_UNKNOWN
;
2501 arr
= VARIABLE_ARRAY_EXPLICIT
;
2503 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
) == 0) {
2504 _update(ctx
, e
, arr
);
2508 _error(ctx
, "expected `]'");
2514 _emit(ctx
, &ps
->out
, DECLARATOR_NONE
);
2520 _parse_declaration(struct parse_context
*ctx
,
2521 struct parse_state
*ps
)
2523 struct parse_state p
= *ps
;
2524 unsigned int e
= _emit(ctx
, &p
.out
, DECLARATION_FUNCTION_PROTOTYPE
);
2526 if (_parse_function_prototype(ctx
, &p
)) {
2527 if (_parse_init_declarator_list(ctx
, &p
)) {
2530 _update(ctx
, e
, DECLARATION_INIT_DECLARATOR_LIST
);
2532 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2533 _error(ctx
, "expected `;'");
2542 _parse_external_declaration(struct parse_context
*ctx
,
2543 struct parse_state
*ps
)
2545 struct parse_state p
= *ps
;
2546 unsigned int e
= _emit(ctx
, &p
.out
, 0);
2548 if (_parse_precision_stmt(ctx
, &p
) == 0) {
2549 _update(ctx
, e
, DEFAULT_PRECISION
);
2554 if (_parse_function_definition(ctx
, &p
) == 0) {
2555 _update(ctx
, e
, EXTERNAL_FUNCTION_DEFINITION
);
2560 if (_parse_invariant_stmt(ctx
, &p
) == 0) {
2561 _update(ctx
, e
, INVARIANT_STMT
);
2566 if (_parse_declaration(ctx
, &p
) == 0) {
2567 _update(ctx
, e
, EXTERNAL_DECLARATION
);
2572 _error(ctx
, "expected an identifier");
2578 _parse_translation_unit(struct parse_context
*ctx
,
2579 struct parse_state
*ps
)
2581 _emit(ctx
, &ps
->out
, REVISION
);
2582 if (_parse_external_declaration(ctx
, ps
)) {
2585 while (_parse_external_declaration(ctx
, ps
) == 0) {
2587 _emit(ctx
, &ps
->out
, EXTERNAL_NULL
);
2588 if (_parse_token(ctx
, SL_PP_EOF
, ps
)) {
2595 #define ADD_NAME_STR(CTX, NAME, STR)\
2597 (CTX).dict.NAME = sl_pp_context_add_unique_str((CTX).context, (STR));\
2598 if ((CTX).dict.NAME == -1) {\
2603 #define ADD_NAME(CTX, NAME) ADD_NAME_STR(CTX, NAME, #NAME)
2607 sl_cl_compile(struct sl_pp_context
*context
,
2608 const struct sl_pp_token_info
*input
,
2609 unsigned int shader_type
,
2610 unsigned int parsing_builtin
,
2611 unsigned char **output
,
2612 unsigned int *cboutput
,
2614 unsigned int cberror
)
2616 struct parse_context ctx
;
2617 struct parse_state ps
;
2619 ctx
.context
= context
;
2622 ADD_NAME_STR(ctx
, _void
, "void");
2623 ADD_NAME_STR(ctx
, _float
, "float");
2624 ADD_NAME_STR(ctx
, _int
, "int");
2625 ADD_NAME_STR(ctx
, _bool
, "bool");
2626 ADD_NAME(ctx
, vec2
);
2627 ADD_NAME(ctx
, vec3
);
2628 ADD_NAME(ctx
, vec4
);
2629 ADD_NAME(ctx
, bvec2
);
2630 ADD_NAME(ctx
, bvec3
);
2631 ADD_NAME(ctx
, bvec4
);
2632 ADD_NAME(ctx
, ivec2
);
2633 ADD_NAME(ctx
, ivec3
);
2634 ADD_NAME(ctx
, ivec4
);
2635 ADD_NAME(ctx
, mat2
);
2636 ADD_NAME(ctx
, mat3
);
2637 ADD_NAME(ctx
, mat4
);
2638 ADD_NAME(ctx
, mat2x3
);
2639 ADD_NAME(ctx
, mat3x2
);
2640 ADD_NAME(ctx
, mat2x4
);
2641 ADD_NAME(ctx
, mat4x2
);
2642 ADD_NAME(ctx
, mat3x4
);
2643 ADD_NAME(ctx
, mat4x3
);
2644 ADD_NAME(ctx
, sampler1D
);
2645 ADD_NAME(ctx
, sampler2D
);
2646 ADD_NAME(ctx
, sampler3D
);
2647 ADD_NAME(ctx
, samplerCube
);
2648 ADD_NAME(ctx
, sampler1DShadow
);
2649 ADD_NAME(ctx
, sampler2DShadow
);
2650 ADD_NAME(ctx
, sampler2DRect
);
2651 ADD_NAME(ctx
, sampler2DRectShadow
);
2653 ADD_NAME(ctx
, invariant
);
2655 ADD_NAME(ctx
, centroid
);
2657 ADD_NAME(ctx
, precision
);
2658 ADD_NAME(ctx
, lowp
);
2659 ADD_NAME(ctx
, mediump
);
2660 ADD_NAME(ctx
, highp
);
2662 ADD_NAME_STR(ctx
, _const
, "const");
2663 ADD_NAME(ctx
, attribute
);
2664 ADD_NAME(ctx
, varying
);
2665 ADD_NAME(ctx
, uniform
);
2666 ADD_NAME(ctx
, __fixed_output
);
2667 ADD_NAME(ctx
, __fixed_input
);
2671 ADD_NAME(ctx
, inout
);
2673 ADD_NAME_STR(ctx
, _struct
, "struct");
2675 ADD_NAME(ctx
, __constructor
);
2676 ADD_NAME(ctx
, __operator
);
2677 ADD_NAME_STR(ctx
, ___asm
, "__asm");
2679 ADD_NAME_STR(ctx
, _if
, "if");
2680 ADD_NAME_STR(ctx
, _else
, "else");
2681 ADD_NAME_STR(ctx
, _for
, "for");
2682 ADD_NAME_STR(ctx
, _while
, "while");
2683 ADD_NAME_STR(ctx
, _do
, "do");
2685 ADD_NAME_STR(ctx
, _continue
, "continue");
2686 ADD_NAME_STR(ctx
, _break
, "break");
2687 ADD_NAME_STR(ctx
, _return
, "return");
2688 ADD_NAME(ctx
, discard
);
2690 ADD_NAME_STR(ctx
, _false
, "false");
2691 ADD_NAME_STR(ctx
, _true
, "true");
2696 ctx
.shader_type
= shader_type
;
2697 ctx
.parsing_builtin
= 1;
2699 ctx
.error
[0] = '\0';
2704 if (_parse_translation_unit(&ctx
, &ps
)) {
2705 strncpy(error
, ctx
.error
, cberror
);
2709 *output
= ctx
.out_buf
;