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
);
1599 if (_parse_type_qualifier(ctx
, &p
)) {
1600 _emit(ctx
, &p
.out
, TYPE_QUALIFIER_NONE
);
1602 _parse_parameter_qualifier(ctx
, &p
);
1603 if (_parse_precision(ctx
, &p
)) {
1604 _emit(ctx
, &p
.out
, PRECISION_DEFAULT
);
1606 if (_parse_parameter_declarator(ctx
, &p
) == 0) {
1610 if (_parse_parameter_type_specifier(ctx
, &p
) == 0) {
1620 _parse_function_header_with_parameters(struct parse_context
*ctx
,
1621 struct parse_state
*ps
)
1623 struct parse_state p
= *ps
;
1625 if (_parse_function_header(ctx
, &p
)) {
1628 if (_parse_parameter_declaration(ctx
, &p
)) {
1634 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1637 if (_parse_parameter_declaration(ctx
, &p
)) {
1645 _parse_function_declarator(struct parse_context
*ctx
,
1646 struct parse_state
*ps
)
1648 if (_parse_function_header_with_parameters(ctx
, ps
) == 0) {
1652 if (_parse_function_header(ctx
, ps
) == 0) {
1661 _parse_function_prototype(struct parse_context
*ctx
,
1662 struct parse_state
*ps
)
1664 struct parse_state p
= *ps
;
1666 if (_parse_function_header(ctx
, &p
) == 0) {
1667 if (_parse_id(ctx
, ctx
->dict
._void
, &p
) == 0) {
1668 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1669 _emit(ctx
, &p
.out
, PARAMETER_NONE
);
1673 _error(ctx
, "expected `)'");
1679 if (_parse_function_declarator(ctx
, &p
) == 0) {
1680 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
) == 0) {
1681 _emit(ctx
, &p
.out
, PARAMETER_NONE
);
1685 _error(ctx
, "expected `)'");
1694 _parse_precision(struct parse_context
*ctx
,
1695 struct parse_state
*ps
)
1698 unsigned int precision
;
1700 if (ctx
->input
[ps
->in
].token
!= SL_PP_IDENTIFIER
) {
1703 id
= ctx
->input
[ps
->in
].data
.identifier
;
1705 if (id
== ctx
->dict
.lowp
) {
1706 precision
= PRECISION_LOW
;
1707 } else if (id
== ctx
->dict
.mediump
) {
1708 precision
= PRECISION_MEDIUM
;
1709 } else if (id
== ctx
->dict
.highp
) {
1710 precision
= PRECISION_HIGH
;
1715 _parse_token(ctx
, SL_PP_IDENTIFIER
, ps
);
1716 _emit(ctx
, &ps
->out
, precision
);
1722 _parse_prectype(struct parse_context
*ctx
,
1723 struct parse_state
*ps
)
1728 if (ctx
->input
[ps
->in
].token
!= SL_PP_IDENTIFIER
) {
1731 id
= ctx
->input
[ps
->in
].data
.identifier
;
1733 if (id
== ctx
->dict
._int
) {
1734 type
= TYPE_SPECIFIER_INT
;
1735 } else if (id
== ctx
->dict
._float
) {
1736 type
= TYPE_SPECIFIER_FLOAT
;
1737 } else if (id
== ctx
->dict
.sampler1D
) {
1738 type
= TYPE_SPECIFIER_SAMPLER1D
;
1739 } else if (id
== ctx
->dict
.sampler2D
) {
1740 type
= TYPE_SPECIFIER_SAMPLER2D
;
1741 } else if (id
== ctx
->dict
.sampler3D
) {
1742 type
= TYPE_SPECIFIER_SAMPLER3D
;
1743 } else if (id
== ctx
->dict
.samplerCube
) {
1744 type
= TYPE_SPECIFIER_SAMPLERCUBE
;
1745 } else if (id
== ctx
->dict
.sampler1DShadow
) {
1746 type
= TYPE_SPECIFIER_SAMPLER1DSHADOW
;
1747 } else if (id
== ctx
->dict
.sampler2DShadow
) {
1748 type
= TYPE_SPECIFIER_SAMPLER2DSHADOW
;
1749 } else if (id
== ctx
->dict
.sampler2DRect
) {
1750 type
= TYPE_SPECIFIER_SAMPLER2DRECT
;
1751 } else if (id
== ctx
->dict
.sampler2DRectShadow
) {
1752 type
= TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
;
1757 _parse_token(ctx
, SL_PP_IDENTIFIER
, ps
);
1758 _emit(ctx
, &ps
->out
, type
);
1764 _parse_precision_stmt(struct parse_context
*ctx
,
1765 struct parse_state
*ps
)
1767 struct parse_state p
= *ps
;
1769 if (_parse_id(ctx
, ctx
->dict
.precision
, &p
)) {
1772 if (_parse_precision(ctx
, &p
)) {
1775 if (_parse_prectype(ctx
, &p
)) {
1778 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
1787 _parse_floatconstant(struct parse_context
*ctx
,
1788 struct parse_state
*ps
)
1790 struct parse_state p
= *ps
;
1792 _emit(ctx
, &p
.out
, OP_PUSH_FLOAT
);
1793 if (_parse_float(ctx
, &p
)) {
1802 _parse_intconstant(struct parse_context
*ctx
,
1803 struct parse_state
*ps
)
1805 struct parse_state p
= *ps
;
1807 _emit(ctx
, &p
.out
, OP_PUSH_INT
);
1808 if (_parse_uint(ctx
, &p
)) {
1817 _parse_boolconstant(struct parse_context
*ctx
,
1818 struct parse_state
*ps
)
1820 if (_parse_id(ctx
, ctx
->dict
._false
, ps
) == 0) {
1821 _emit(ctx
, &ps
->out
, OP_PUSH_BOOL
);
1822 _emit(ctx
, &ps
->out
, '0');
1823 _emit(ctx
, &ps
->out
, '\0');
1827 if (_parse_id(ctx
, ctx
->dict
._true
, ps
) == 0) {
1828 _emit(ctx
, &ps
->out
, OP_PUSH_BOOL
);
1829 _emit(ctx
, &ps
->out
, '1');
1830 _emit(ctx
, &ps
->out
, '\0');
1839 _parse_variable_identifier(struct parse_context
*ctx
,
1840 struct parse_state
*ps
)
1842 struct parse_state p
= *ps
;
1844 _emit(ctx
, &p
.out
, OP_PUSH_IDENTIFIER
);
1845 if (_parse_identifier(ctx
, &p
)) {
1854 _parse_primary_expression(struct parse_context
*ctx
,
1855 struct parse_state
*ps
)
1857 struct parse_state p
;
1859 if (_parse_floatconstant(ctx
, ps
) == 0) {
1862 if (_parse_boolconstant(ctx
, ps
) == 0) {
1865 if (_parse_intconstant(ctx
, ps
) == 0) {
1868 if (_parse_variable_identifier(ctx
, ps
) == 0) {
1873 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
1876 if (_parse_expression(ctx
, &p
)) {
1879 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
1889 _parse_asm_argument(struct parse_context
*ctx
,
1890 struct parse_state
*ps
)
1892 if (_parse_variable_identifier(ctx
, ps
) == 0) {
1893 struct parse_state p
= *ps
;
1895 if (_parse_token(ctx
, SL_PP_DOT
, &p
)) {
1898 _emit(ctx
, &p
.out
, OP_FIELD
);
1899 if (_parse_identifier(ctx
, &p
)) {
1906 if (_parse_floatconstant(ctx
, ps
) == 0) {
1915 _parse_asm_arguments(struct parse_context
*ctx
,
1916 struct parse_state
*ps
)
1918 struct parse_state p
= *ps
;
1920 if (_parse_asm_argument(ctx
, &p
)) {
1923 _emit(ctx
, &p
.out
, OP_END
);
1927 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
1930 if (_parse_asm_argument(ctx
, &p
)) {
1933 _emit(ctx
, &p
.out
, OP_END
);
1939 _parse_asm_statement(struct parse_context
*ctx
,
1940 struct parse_state
*ps
)
1942 struct parse_state p
= *ps
;
1944 if (_parse_id(ctx
, ctx
->dict
.___asm
, &p
)) {
1947 if (_parse_identifier(ctx
, &p
)) {
1950 if (_parse_asm_arguments(ctx
, &p
)) {
1953 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
1956 _emit(ctx
, &p
.out
, OP_END
);
1963 _parse_selection_statement(struct parse_context
*ctx
,
1964 struct parse_state
*ps
)
1966 struct parse_state p
= *ps
;
1968 _emit(ctx
, &p
.out
, OP_IF
);
1969 if (_parse_id(ctx
, ctx
->dict
._if
, &p
)) {
1972 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
1973 _error(ctx
, "expected `('");
1976 if (_parse_expression(ctx
, &p
)) {
1977 _error(ctx
, "expected an expression");
1980 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
1981 _error(ctx
, "expected `)'");
1984 _emit(ctx
, &p
.out
, OP_END
);
1985 if (_parse_statement(ctx
, &p
)) {
1990 if (_parse_id(ctx
, ctx
->dict
._else
, &p
) == 0) {
1991 if (_parse_statement(ctx
, &p
) == 0) {
1997 _emit(ctx
, &ps
->out
, OP_EXPRESSION
);
1998 _emit(ctx
, &ps
->out
, OP_PUSH_VOID
);
1999 _emit(ctx
, &ps
->out
, OP_END
);
2005 _parse_expression_statement(struct parse_context
*ctx
,
2006 struct parse_state
*ps
)
2008 struct parse_state p
= *ps
;
2010 if (_parse_expression(ctx
, &p
)) {
2011 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
2013 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2016 _emit(ctx
, &p
.out
, OP_END
);
2023 _parse_for_init_statement(struct parse_context
*ctx
,
2024 struct parse_state
*ps
)
2026 struct parse_state p
= *ps
;
2027 unsigned int e
= _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2029 if (_parse_expression_statement(ctx
, &p
) == 0) {
2034 if (_parse_declaration(ctx
, &p
) == 0) {
2035 _update(ctx
, e
, OP_DECLARE
);
2045 _parse_initializer(struct parse_context
*ctx
,
2046 struct parse_state
*ps
)
2048 if (_parse_assignment_expression(ctx
, ps
) == 0) {
2049 _emit(ctx
, &ps
->out
, OP_END
);
2057 _parse_condition_initializer(struct parse_context
*ctx
,
2058 struct parse_state
*ps
)
2060 struct parse_state p
= *ps
;
2062 _emit(ctx
, &p
.out
, OP_DECLARE
);
2063 _emit(ctx
, &p
.out
, DECLARATION_INIT_DECLARATOR_LIST
);
2064 if (_parse_fully_specified_type(ctx
, &p
)) {
2067 _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2068 if (_parse_identifier(ctx
, &p
)) {
2071 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
)) {
2072 _error(ctx
, "expected `='");
2075 _emit(ctx
, &p
.out
, VARIABLE_INITIALIZER
);
2076 if (_parse_initializer(ctx
, &p
)) {
2077 _error(ctx
, "expected an initialiser");
2080 _emit(ctx
, &p
.out
, DECLARATOR_NONE
);
2087 _parse_condition(struct parse_context
*ctx
,
2088 struct parse_state
*ps
)
2090 struct parse_state p
;
2092 if (_parse_condition_initializer(ctx
, ps
) == 0) {
2097 _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2098 if (_parse_expression(ctx
, &p
) == 0) {
2099 _emit(ctx
, &p
.out
, OP_END
);
2109 _parse_for_rest_statement(struct parse_context
*ctx
,
2110 struct parse_state
*ps
)
2112 struct parse_state p
= *ps
;
2114 if (_parse_condition(ctx
, &p
)) {
2115 _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2116 _emit(ctx
, &p
.out
, OP_PUSH_BOOL
);
2117 _emit(ctx
, &p
.out
, 2);
2118 _emit(ctx
, &p
.out
, '1');
2119 _emit(ctx
, &p
.out
, '\0');
2120 _emit(ctx
, &p
.out
, OP_END
);
2122 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2125 if (_parse_expression(ctx
, &p
)) {
2126 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
2128 _emit(ctx
, &p
.out
, OP_END
);
2135 _parse_iteration_statement(struct parse_context
*ctx
,
2136 struct parse_state
*ps
)
2138 struct parse_state p
= *ps
;
2140 if (_parse_id(ctx
, ctx
->dict
._while
, &p
) == 0) {
2141 _emit(ctx
, &p
.out
, OP_WHILE
);
2142 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2143 _error(ctx
, "expected `('");
2146 if (_parse_condition(ctx
, &p
)) {
2147 _error(ctx
, "expected an expression");
2150 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2151 _error(ctx
, "expected `)'");
2154 if (_parse_statement(ctx
, &p
)) {
2161 if (_parse_id(ctx
, ctx
->dict
._do
, &p
) == 0) {
2162 _emit(ctx
, &p
.out
, OP_DO
);
2163 if (_parse_statement(ctx
, &p
)) {
2166 if (_parse_id(ctx
, ctx
->dict
._while
, &p
)) {
2169 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2170 _error(ctx
, "expected `('");
2173 if (_parse_expression(ctx
, &p
)) {
2174 _error(ctx
, "expected an expression");
2177 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2178 _error(ctx
, "expected `)'");
2181 _emit(ctx
, &p
.out
, OP_END
);
2182 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2183 _error(ctx
, "expected `;'");
2190 if (_parse_id(ctx
, ctx
->dict
._for
, &p
) == 0) {
2191 _emit(ctx
, &p
.out
, OP_FOR
);
2192 if (_parse_token(ctx
, SL_PP_LPAREN
, &p
)) {
2193 _error(ctx
, "expected `('");
2196 if (_parse_for_init_statement(ctx
, &p
)) {
2199 if (_parse_for_rest_statement(ctx
, &p
)) {
2202 if (_parse_token(ctx
, SL_PP_RPAREN
, &p
)) {
2203 _error(ctx
, "expected `)'");
2206 if (_parse_statement(ctx
, &p
)) {
2218 _parse_jump_statement(struct parse_context
*ctx
,
2219 struct parse_state
*ps
)
2221 struct parse_state p
= *ps
;
2222 unsigned int e
= _emit(ctx
, &p
.out
, 0);
2224 if (_parse_id(ctx
, ctx
->dict
._continue
, &p
) == 0) {
2225 _update(ctx
, e
, OP_CONTINUE
);
2226 } else if (_parse_id(ctx
, ctx
->dict
._break
, &p
) == 0) {
2227 _update(ctx
, e
, OP_BREAK
);
2228 } else if (_parse_id(ctx
, ctx
->dict
._return
, &p
) == 0) {
2229 _update(ctx
, e
, OP_RETURN
);
2230 if (_parse_expression(ctx
, &p
)) {
2231 _emit(ctx
, &p
.out
, OP_PUSH_VOID
);
2233 _emit(ctx
, &p
.out
, OP_END
);
2234 } else if (ctx
->shader_type
== 1 && _parse_id(ctx
, ctx
->dict
.discard
, &p
) == 0) {
2235 _update(ctx
, e
, OP_DISCARD
);
2239 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2248 _parse_simple_statement(struct parse_context
*ctx
,
2249 struct parse_state
*ps
)
2251 struct parse_state p
;
2254 if (_parse_selection_statement(ctx
, ps
) == 0) {
2258 if (_parse_iteration_statement(ctx
, ps
) == 0) {
2262 if (_parse_jump_statement(ctx
, ps
) == 0) {
2267 e
= _emit(ctx
, &p
.out
, OP_EXPRESSION
);
2268 if (_parse_expression_statement(ctx
, &p
) == 0) {
2273 if (_parse_precision_stmt(ctx
, &p
) == 0) {
2274 _update(ctx
, e
, OP_PRECISION
);
2279 if (ctx
->parsing_builtin
&& _parse_asm_statement(ctx
, &p
) == 0) {
2280 _update(ctx
, e
, OP_ASM
);
2285 if (_parse_declaration(ctx
, &p
) == 0) {
2286 _update(ctx
, e
, OP_DECLARE
);
2296 _parse_compound_statement(struct parse_context
*ctx
,
2297 struct parse_state
*ps
)
2299 struct parse_state p
= *ps
;
2301 if (_parse_token(ctx
, SL_PP_LBRACE
, &p
)) {
2304 _emit(ctx
, &p
.out
, OP_BLOCK_BEGIN_NEW_SCOPE
);
2305 _parse_statement_list(ctx
, &p
);
2306 if (_parse_token(ctx
, SL_PP_RBRACE
, &p
)) {
2309 _emit(ctx
, &p
.out
, OP_END
);
2316 _parse_statement(struct parse_context
*ctx
,
2317 struct parse_state
*ps
)
2319 if (_parse_compound_statement(ctx
, ps
) == 0) {
2323 if (_parse_simple_statement(ctx
, ps
) == 0) {
2332 _parse_statement_list(struct parse_context
*ctx
,
2333 struct parse_state
*ps
)
2335 struct parse_state p
= *ps
;
2337 if (_parse_statement(ctx
, &p
)) {
2343 if (_parse_statement(ctx
, &p
)) {
2351 _parse_compound_statement_no_new_scope(struct parse_context
*ctx
,
2352 struct parse_state
*ps
)
2354 struct parse_state p
= *ps
;
2356 if (_parse_token(ctx
, SL_PP_LBRACE
, &p
)) {
2359 _emit(ctx
, &p
.out
, OP_BLOCK_BEGIN_NO_NEW_SCOPE
);
2360 _parse_statement_list(ctx
, &p
);
2361 if (_parse_token(ctx
, SL_PP_RBRACE
, &p
)) {
2364 _emit(ctx
, &p
.out
, OP_END
);
2371 _parse_function_definition(struct parse_context
*ctx
,
2372 struct parse_state
*ps
)
2374 struct parse_state p
= *ps
;
2376 if (_parse_function_prototype(ctx
, &p
)) {
2379 if (_parse_compound_statement_no_new_scope(ctx
, &p
)) {
2388 _parse_invariant_stmt(struct parse_context
*ctx
,
2389 struct parse_state
*ps
)
2391 struct parse_state p
= *ps
;
2393 if (_parse_id(ctx
, ctx
->dict
.invariant
, &p
)) {
2396 if (_parse_identifier(ctx
, &p
)) {
2399 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2408 _parse_single_declaration(struct parse_context
*ctx
,
2409 struct parse_state
*ps
)
2411 struct parse_state p
= *ps
;
2414 if (_parse_fully_specified_type(ctx
, &p
)) {
2418 e
= _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2419 if (_parse_identifier(ctx
, &p
)) {
2420 _update(ctx
, e
, VARIABLE_NONE
);
2425 e
= _emit(ctx
, &p
.out
, VARIABLE_NONE
);
2428 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
) == 0) {
2429 _update(ctx
, e
, VARIABLE_INITIALIZER
);
2430 if (_parse_initializer(ctx
, &p
) == 0) {
2434 _error(ctx
, "expected an initialiser");
2439 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
) == 0) {
2440 if (_parse_constant_expression(ctx
, &p
)) {
2441 _update(ctx
, e
, VARIABLE_ARRAY_UNKNOWN
);
2443 _update(ctx
, e
, VARIABLE_ARRAY_EXPLICIT
);
2445 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
) == 0) {
2449 _error(ctx
, "expected `]'");
2457 _parse_init_declarator_list(struct parse_context
*ctx
,
2458 struct parse_state
*ps
)
2460 struct parse_state p
= *ps
;
2462 if (_parse_single_declaration(ctx
, &p
)) {
2470 if (_parse_token(ctx
, SL_PP_COMMA
, &p
)) {
2473 _emit(ctx
, &p
.out
, DECLARATOR_NEXT
);
2474 _emit(ctx
, &p
.out
, VARIABLE_IDENTIFIER
);
2475 if (_parse_identifier(ctx
, &p
)) {
2479 e
= _emit(ctx
, &p
.out
, VARIABLE_NONE
);
2482 if (_parse_token(ctx
, SL_PP_ASSIGN
, &p
) == 0) {
2483 if (_parse_initializer(ctx
, &p
) == 0) {
2484 _update(ctx
, e
, VARIABLE_INITIALIZER
);
2488 _error(ctx
, "expected an initialiser");
2493 if (_parse_token(ctx
, SL_PP_LBRACKET
, &p
) == 0) {
2496 if (_parse_constant_expression(ctx
, &p
)) {
2497 arr
= VARIABLE_ARRAY_UNKNOWN
;
2499 arr
= VARIABLE_ARRAY_EXPLICIT
;
2501 if (_parse_token(ctx
, SL_PP_RBRACKET
, &p
) == 0) {
2502 _update(ctx
, e
, arr
);
2506 _error(ctx
, "expected `]'");
2512 _emit(ctx
, &ps
->out
, DECLARATOR_NONE
);
2518 _parse_declaration(struct parse_context
*ctx
,
2519 struct parse_state
*ps
)
2521 struct parse_state p
= *ps
;
2522 unsigned int e
= _emit(ctx
, &p
.out
, DECLARATION_FUNCTION_PROTOTYPE
);
2524 if (_parse_function_prototype(ctx
, &p
)) {
2525 if (_parse_init_declarator_list(ctx
, &p
)) {
2528 _update(ctx
, e
, DECLARATION_INIT_DECLARATOR_LIST
);
2530 if (_parse_token(ctx
, SL_PP_SEMICOLON
, &p
)) {
2531 _error(ctx
, "expected `;'");
2540 _parse_external_declaration(struct parse_context
*ctx
,
2541 struct parse_state
*ps
)
2543 struct parse_state p
= *ps
;
2544 unsigned int e
= _emit(ctx
, &p
.out
, 0);
2546 if (_parse_precision_stmt(ctx
, &p
) == 0) {
2547 _update(ctx
, e
, DEFAULT_PRECISION
);
2552 if (_parse_function_definition(ctx
, &p
) == 0) {
2553 _update(ctx
, e
, EXTERNAL_FUNCTION_DEFINITION
);
2558 if (_parse_invariant_stmt(ctx
, &p
) == 0) {
2559 _update(ctx
, e
, INVARIANT_STMT
);
2564 if (_parse_declaration(ctx
, &p
) == 0) {
2565 _update(ctx
, e
, EXTERNAL_DECLARATION
);
2570 _error(ctx
, "expected an identifier");
2576 _parse_translation_unit(struct parse_context
*ctx
,
2577 struct parse_state
*ps
)
2579 _emit(ctx
, &ps
->out
, REVISION
);
2580 if (_parse_external_declaration(ctx
, ps
)) {
2583 while (_parse_external_declaration(ctx
, ps
) == 0) {
2585 _emit(ctx
, &ps
->out
, EXTERNAL_NULL
);
2586 if (_parse_token(ctx
, SL_PP_EOF
, ps
)) {
2593 #define ADD_NAME_STR(CTX, NAME, STR)\
2595 (CTX).dict.NAME = sl_pp_context_add_unique_str((CTX).context, (STR));\
2596 if ((CTX).dict.NAME == -1) {\
2601 #define ADD_NAME(CTX, NAME) ADD_NAME_STR(CTX, NAME, #NAME)
2605 sl_cl_compile(struct sl_pp_context
*context
,
2606 const struct sl_pp_token_info
*input
,
2607 unsigned int shader_type
,
2608 unsigned int parsing_builtin
,
2609 unsigned char **output
,
2610 unsigned int *cboutput
,
2612 unsigned int cberror
)
2614 struct parse_context ctx
;
2615 struct parse_state ps
;
2617 ctx
.context
= context
;
2620 ADD_NAME_STR(ctx
, _void
, "void");
2621 ADD_NAME_STR(ctx
, _float
, "float");
2622 ADD_NAME_STR(ctx
, _int
, "int");
2623 ADD_NAME_STR(ctx
, _bool
, "bool");
2624 ADD_NAME(ctx
, vec2
);
2625 ADD_NAME(ctx
, vec3
);
2626 ADD_NAME(ctx
, vec4
);
2627 ADD_NAME(ctx
, bvec2
);
2628 ADD_NAME(ctx
, bvec3
);
2629 ADD_NAME(ctx
, bvec4
);
2630 ADD_NAME(ctx
, ivec2
);
2631 ADD_NAME(ctx
, ivec3
);
2632 ADD_NAME(ctx
, ivec4
);
2633 ADD_NAME(ctx
, mat2
);
2634 ADD_NAME(ctx
, mat3
);
2635 ADD_NAME(ctx
, mat4
);
2636 ADD_NAME(ctx
, mat2x3
);
2637 ADD_NAME(ctx
, mat3x2
);
2638 ADD_NAME(ctx
, mat2x4
);
2639 ADD_NAME(ctx
, mat4x2
);
2640 ADD_NAME(ctx
, mat3x4
);
2641 ADD_NAME(ctx
, mat4x3
);
2642 ADD_NAME(ctx
, sampler1D
);
2643 ADD_NAME(ctx
, sampler2D
);
2644 ADD_NAME(ctx
, sampler3D
);
2645 ADD_NAME(ctx
, samplerCube
);
2646 ADD_NAME(ctx
, sampler1DShadow
);
2647 ADD_NAME(ctx
, sampler2DShadow
);
2648 ADD_NAME(ctx
, sampler2DRect
);
2649 ADD_NAME(ctx
, sampler2DRectShadow
);
2651 ADD_NAME(ctx
, invariant
);
2653 ADD_NAME(ctx
, centroid
);
2655 ADD_NAME(ctx
, precision
);
2656 ADD_NAME(ctx
, lowp
);
2657 ADD_NAME(ctx
, mediump
);
2658 ADD_NAME(ctx
, highp
);
2660 ADD_NAME_STR(ctx
, _const
, "const");
2661 ADD_NAME(ctx
, attribute
);
2662 ADD_NAME(ctx
, varying
);
2663 ADD_NAME(ctx
, uniform
);
2664 ADD_NAME(ctx
, __fixed_output
);
2665 ADD_NAME(ctx
, __fixed_input
);
2669 ADD_NAME(ctx
, inout
);
2671 ADD_NAME_STR(ctx
, _struct
, "struct");
2673 ADD_NAME(ctx
, __constructor
);
2674 ADD_NAME(ctx
, __operator
);
2675 ADD_NAME_STR(ctx
, ___asm
, "__asm");
2677 ADD_NAME_STR(ctx
, _if
, "if");
2678 ADD_NAME_STR(ctx
, _else
, "else");
2679 ADD_NAME_STR(ctx
, _for
, "for");
2680 ADD_NAME_STR(ctx
, _while
, "while");
2681 ADD_NAME_STR(ctx
, _do
, "do");
2683 ADD_NAME_STR(ctx
, _continue
, "continue");
2684 ADD_NAME_STR(ctx
, _break
, "break");
2685 ADD_NAME_STR(ctx
, _return
, "return");
2686 ADD_NAME(ctx
, discard
);
2688 ADD_NAME_STR(ctx
, _false
, "false");
2689 ADD_NAME_STR(ctx
, _true
, "true");
2694 ctx
.shader_type
= shader_type
;
2695 ctx
.parsing_builtin
= 1;
2697 ctx
.error
[0] = '\0';
2702 if (_parse_translation_unit(&ctx
, &ps
)) {
2703 strncpy(error
, ctx
.error
, cberror
);
2707 *output
= ctx
.out_buf
;