2 * Mesa 3-D graphics library
4 * Copyright (C) 2005-2006 Brian Paul All Rights Reserved.
5 * Copyright (C) 2008 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \file slang_compile.c
27 * slang front-end compiler
31 #include "main/imports.h"
32 #include "main/context.h"
33 #include "shader/program.h"
34 #include "shader/programopt.h"
35 #include "shader/prog_optimize.h"
36 #include "shader/prog_print.h"
37 #include "shader/prog_parameter.h"
38 #include "shader/grammar/grammar_mesa.h"
39 #include "slang_codegen.h"
40 #include "slang_compile.h"
41 #include "slang_preprocess.h"
42 #include "slang_storage.h"
43 #include "slang_emit.h"
44 #include "slang_log.h"
45 #include "slang_mem.h"
46 #include "slang_vartable.h"
47 #include "slang_simplify.h"
49 #include "slang_print.h"
52 * This is a straightforward implementation of the slang front-end
53 * compiler. Lots of error-checking functionality is missing but
54 * every well-formed shader source should compile successfully and
55 * execute as expected. However, some semantically ill-formed shaders
56 * may be accepted resulting in undefined behaviour.
60 /** re-defined below, should be the same though */
61 #define TYPE_SPECIFIER_COUNT 32
65 * Check if the given identifier is legal.
68 legal_identifier(slang_atom name
)
70 /* "gl_" is a reserved prefix */
71 if (_mesa_strncmp((char *) name
, "gl_", 3) == 0) {
83 _slang_code_unit_ctr(slang_code_unit
* self
,
84 struct slang_code_object_
* object
)
86 _slang_variable_scope_ctr(&self
->vars
);
87 _slang_function_scope_ctr(&self
->funs
);
88 _slang_struct_scope_ctr(&self
->structs
);
89 self
->object
= object
;
93 _slang_code_unit_dtr(slang_code_unit
* self
)
95 slang_variable_scope_destruct(&self
->vars
);
96 slang_function_scope_destruct(&self
->funs
);
97 slang_struct_scope_destruct(&self
->structs
);
105 _slang_code_object_ctr(slang_code_object
* self
)
109 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
110 _slang_code_unit_ctr(&self
->builtin
[i
], self
);
111 _slang_code_unit_ctr(&self
->unit
, self
);
112 slang_atom_pool_construct(&self
->atompool
);
116 _slang_code_object_dtr(slang_code_object
* self
)
120 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
121 _slang_code_unit_dtr(&self
->builtin
[i
]);
122 _slang_code_unit_dtr(&self
->unit
);
123 slang_atom_pool_destruct(&self
->atompool
);
127 /* slang_parse_ctx */
129 typedef struct slang_parse_ctx_
134 GLboolean global_scope
; /**< Is object being declared a global? */
135 slang_atom_pool
*atoms
;
136 slang_unit_type type
; /**< Vertex vs. Fragment */
137 GLuint version
; /**< user-specified (or default) #version */
140 /* slang_output_ctx */
142 typedef struct slang_output_ctx_
144 slang_variable_scope
*vars
;
145 slang_function_scope
*funs
;
146 slang_struct_scope
*structs
;
147 struct gl_program
*program
;
148 struct gl_sl_pragmas
*pragmas
;
149 slang_var_table
*vartable
;
150 GLuint default_precision
[TYPE_SPECIFIER_COUNT
];
151 GLboolean allow_precision
;
152 GLboolean allow_invariant
;
153 GLboolean allow_centroid
;
154 GLboolean allow_array_types
; /* float[] syntax */
157 /* _slang_compile() */
160 /* Debugging aid, print file/line where parsing error is detected */
164 printf("slang error at %s:%d\n", __FILE__, __LINE__); \
170 parse_identifier_str(slang_parse_ctx
* C
, char **id
)
173 C
->I
+= _mesa_strlen(*id
) + 1;
177 parse_identifier(slang_parse_ctx
* C
)
181 id
= (const char *) C
->I
;
182 C
->I
+= _mesa_strlen(id
) + 1;
183 return slang_atom_pool_atom(C
->atoms
, id
);
187 parse_number(slang_parse_ctx
* C
, int *number
)
189 const int radix
= (int) (*C
->I
++);
191 while (*C
->I
!= '\0') {
193 if (*C
->I
>= '0' && *C
->I
<= '9')
194 digit
= (int) (*C
->I
- '0');
195 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
196 digit
= (int) (*C
->I
- 'A') + 10;
198 digit
= (int) (*C
->I
- 'a') + 10;
199 *number
= *number
* radix
+ digit
;
204 slang_info_log_warning(C
->L
, "%d: literal integer overflow.", *number
);
209 parse_float(slang_parse_ctx
* C
, float *number
)
211 char *integral
= NULL
;
212 char *fractional
= NULL
;
213 char *exponent
= NULL
;
216 parse_identifier_str(C
, &integral
);
217 parse_identifier_str(C
, &fractional
);
218 parse_identifier_str(C
, &exponent
);
220 whole
= (char *) _slang_alloc((_mesa_strlen(integral
) +
221 _mesa_strlen(fractional
) +
222 _mesa_strlen(exponent
) + 3) * sizeof(char));
224 slang_info_log_memory(C
->L
);
228 slang_string_copy(whole
, integral
);
229 slang_string_concat(whole
, ".");
230 slang_string_concat(whole
, fractional
);
231 slang_string_concat(whole
, "E");
232 slang_string_concat(whole
, exponent
);
234 *number
= (float) (_mesa_strtod(whole
, (char **) NULL
));
241 /* revision number - increment after each change affecting emitted output */
245 check_revision(slang_parse_ctx
* C
)
247 if (*C
->I
!= REVISION
) {
248 slang_info_log_error(C
->L
, "Internal compiler error.");
255 static int parse_statement(slang_parse_ctx
*, slang_output_ctx
*,
257 static int parse_expression(slang_parse_ctx
*, slang_output_ctx
*,
259 static int parse_type_specifier(slang_parse_ctx
*, slang_output_ctx
*,
260 slang_type_specifier
*);
262 parse_type_array_size(slang_parse_ctx
*C
,
267 parse_array_len(slang_parse_ctx
* C
, slang_output_ctx
* O
, GLuint
* len
)
269 slang_operation array_size
;
270 slang_name_space space
;
273 if (!slang_operation_construct(&array_size
))
275 if (!parse_expression(C
, O
, &array_size
)) {
276 slang_operation_destruct(&array_size
);
280 space
.funcs
= O
->funs
;
281 space
.structs
= O
->structs
;
282 space
.vars
= O
->vars
;
284 /* evaluate compile-time expression which is array size */
285 _slang_simplify(&array_size
, &space
, C
->atoms
);
287 if (array_size
.type
== SLANG_OPER_LITERAL_INT
) {
289 *len
= (GLint
) array_size
.literal
[0];
290 } else if (array_size
.type
== SLANG_OPER_IDENTIFIER
) {
291 slang_variable
*var
= _slang_variable_locate(array_size
.locals
, array_size
.a_id
, GL_TRUE
);
293 slang_info_log_error(C
->L
, "undefined variable '%s'",
294 (char *) array_size
.a_id
);
296 } else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&&
297 var
->type
.specifier
.type
== SLANG_SPEC_INT
) {
298 if (var
->initializer
&&
299 var
->initializer
->type
== SLANG_OPER_LITERAL_INT
) {
300 *len
= (GLint
) var
->initializer
->literal
[0];
303 slang_info_log_error(C
->L
, "unable to parse array size declaration");
307 slang_info_log_error(C
->L
, "unable to parse array size declaration");
314 slang_operation_destruct(&array_size
);
319 calculate_var_size(slang_parse_ctx
* C
, slang_output_ctx
* O
,
320 slang_variable
* var
)
322 slang_storage_aggregate agg
;
324 if (!slang_storage_aggregate_construct(&agg
))
326 if (!_slang_aggregate_variable(&agg
, &var
->type
.specifier
, var
->array_len
,
327 O
->funs
, O
->structs
, O
->vars
, C
->atoms
)) {
328 slang_storage_aggregate_destruct(&agg
);
331 var
->size
= _slang_sizeof_aggregate(&agg
);
332 slang_storage_aggregate_destruct(&agg
);
337 promote_type_to_array(slang_parse_ctx
*C
,
338 slang_fully_specified_type
*type
,
341 slang_type_specifier
*baseType
=
342 slang_type_specifier_new(type
->specifier
.type
, NULL
, NULL
);
344 type
->specifier
.type
= SLANG_SPEC_ARRAY
;
345 type
->specifier
._array
= baseType
;
346 type
->array_len
= array_len
;
351 convert_to_array(slang_parse_ctx
* C
, slang_variable
* var
,
352 const slang_type_specifier
* sp
)
354 /* sized array - mark it as array, copy the specifier to the array element
355 * and parse the expression */
356 var
->type
.specifier
.type
= SLANG_SPEC_ARRAY
;
357 var
->type
.specifier
._array
= (slang_type_specifier
*)
358 _slang_alloc(sizeof(slang_type_specifier
));
359 if (var
->type
.specifier
._array
== NULL
) {
360 slang_info_log_memory(C
->L
);
363 slang_type_specifier_ctr(var
->type
.specifier
._array
);
364 return slang_type_specifier_copy(var
->type
.specifier
._array
, sp
);
367 /* structure field */
370 #define FIELD_ARRAY 2
373 parse_struct_field_var(slang_parse_ctx
* C
, slang_output_ctx
* O
,
374 slang_variable
* var
, slang_atom a_name
,
375 const slang_type_specifier
* sp
,
378 var
->a_name
= a_name
;
379 if (var
->a_name
== SLANG_ATOM_NULL
)
384 if (array_len
!= -1) {
385 if (!convert_to_array(C
, var
, sp
))
387 var
->array_len
= array_len
;
390 if (!slang_type_specifier_copy(&var
->type
.specifier
, sp
))
397 if (!convert_to_array(C
, var
, sp
))
399 if (!parse_array_len(C
, O
, &var
->array_len
))
406 return calculate_var_size(C
, O
, var
);
410 parse_struct_field(slang_parse_ctx
* C
, slang_output_ctx
* O
,
411 slang_struct
* st
, slang_type_specifier
* sp
)
413 slang_output_ctx o
= *O
;
416 o
.structs
= st
->structs
;
417 if (!parse_type_specifier(C
, &o
, sp
))
419 if (!parse_type_array_size(C
, &o
, &array_len
))
424 slang_variable
*var
= slang_variable_scope_grow(st
->fields
);
426 slang_info_log_memory(C
->L
);
429 a_name
= parse_identifier(C
);
430 if (_slang_variable_locate(st
->fields
, a_name
, GL_FALSE
)) {
431 slang_info_log_error(C
->L
, "duplicate field '%s'", (char *) a_name
);
435 if (!parse_struct_field_var(C
, &o
, var
, a_name
, sp
, array_len
))
438 while (*C
->I
++ != FIELD_NONE
);
444 parse_struct(slang_parse_ctx
* C
, slang_output_ctx
* O
, slang_struct
** st
)
449 /* parse struct name (if any) and make sure it is unique in current scope */
450 a_name
= parse_identifier(C
);
451 if (a_name
== SLANG_ATOM_NULL
)
454 name
= slang_atom_pool_id(C
->atoms
, a_name
);
456 && slang_struct_scope_find(O
->structs
, a_name
, 0) != NULL
) {
457 slang_info_log_error(C
->L
, "%s: duplicate type name.", name
);
461 /* set-up a new struct */
462 *st
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
464 slang_info_log_memory(C
->L
);
467 if (!slang_struct_construct(*st
)) {
470 slang_info_log_memory(C
->L
);
473 (**st
).a_name
= a_name
;
474 (**st
).structs
->outer_scope
= O
->structs
;
476 /* parse individual struct fields */
478 slang_type_specifier sp
;
480 slang_type_specifier_ctr(&sp
);
481 if (!parse_struct_field(C
, O
, *st
, &sp
)) {
482 slang_type_specifier_dtr(&sp
);
485 slang_type_specifier_dtr(&sp
);
487 while (*C
->I
++ != FIELD_NONE
);
489 /* if named struct, copy it to current scope */
490 if (name
[0] != '\0') {
493 O
->structs
->structs
=
494 (slang_struct
*) _slang_realloc(O
->structs
->structs
,
495 O
->structs
->num_structs
496 * sizeof(slang_struct
),
497 (O
->structs
->num_structs
+ 1)
498 * sizeof(slang_struct
));
499 if (O
->structs
->structs
== NULL
) {
500 slang_info_log_memory(C
->L
);
503 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
504 if (!slang_struct_construct(s
))
506 O
->structs
->num_structs
++;
507 if (!slang_struct_copy(s
, *st
))
515 /* invariant qualifer */
516 #define TYPE_VARIANT 90
517 #define TYPE_INVARIANT 91
520 parse_type_variant(slang_parse_ctx
* C
, slang_type_variant
*variant
)
522 GLuint invariant
= *C
->I
++;
525 *variant
= SLANG_VARIANT
;
528 *variant
= SLANG_INVARIANT
;
536 /* centroid qualifer */
537 #define TYPE_CENTER 95
538 #define TYPE_CENTROID 96
541 parse_type_centroid(slang_parse_ctx
* C
, slang_type_centroid
*centroid
)
546 *centroid
= SLANG_CENTER
;
549 *centroid
= SLANG_CENTROID
;
558 #define TYPE_QUALIFIER_NONE 0
559 #define TYPE_QUALIFIER_CONST 1
560 #define TYPE_QUALIFIER_ATTRIBUTE 2
561 #define TYPE_QUALIFIER_VARYING 3
562 #define TYPE_QUALIFIER_UNIFORM 4
563 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
564 #define TYPE_QUALIFIER_FIXEDINPUT 6
567 parse_type_qualifier(slang_parse_ctx
* C
, slang_type_qualifier
* qual
)
569 GLuint qualifier
= *C
->I
++;
571 case TYPE_QUALIFIER_NONE
:
572 *qual
= SLANG_QUAL_NONE
;
574 case TYPE_QUALIFIER_CONST
:
575 *qual
= SLANG_QUAL_CONST
;
577 case TYPE_QUALIFIER_ATTRIBUTE
:
578 *qual
= SLANG_QUAL_ATTRIBUTE
;
580 case TYPE_QUALIFIER_VARYING
:
581 *qual
= SLANG_QUAL_VARYING
;
583 case TYPE_QUALIFIER_UNIFORM
:
584 *qual
= SLANG_QUAL_UNIFORM
;
586 case TYPE_QUALIFIER_FIXEDOUTPUT
:
587 *qual
= SLANG_QUAL_FIXEDOUTPUT
;
589 case TYPE_QUALIFIER_FIXEDINPUT
:
590 *qual
= SLANG_QUAL_FIXEDINPUT
;
599 #define TYPE_SPECIFIER_VOID 0
600 #define TYPE_SPECIFIER_BOOL 1
601 #define TYPE_SPECIFIER_BVEC2 2
602 #define TYPE_SPECIFIER_BVEC3 3
603 #define TYPE_SPECIFIER_BVEC4 4
604 #define TYPE_SPECIFIER_INT 5
605 #define TYPE_SPECIFIER_IVEC2 6
606 #define TYPE_SPECIFIER_IVEC3 7
607 #define TYPE_SPECIFIER_IVEC4 8
608 #define TYPE_SPECIFIER_FLOAT 9
609 #define TYPE_SPECIFIER_VEC2 10
610 #define TYPE_SPECIFIER_VEC3 11
611 #define TYPE_SPECIFIER_VEC4 12
612 #define TYPE_SPECIFIER_MAT2 13
613 #define TYPE_SPECIFIER_MAT3 14
614 #define TYPE_SPECIFIER_MAT4 15
615 #define TYPE_SPECIFIER_SAMPLER1D 16
616 #define TYPE_SPECIFIER_SAMPLER2D 17
617 #define TYPE_SPECIFIER_SAMPLER3D 18
618 #define TYPE_SPECIFIER_SAMPLERCUBE 19
619 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
620 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
621 #define TYPE_SPECIFIER_SAMPLER2DRECT 22
622 #define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
623 #define TYPE_SPECIFIER_STRUCT 24
624 #define TYPE_SPECIFIER_TYPENAME 25
625 #define TYPE_SPECIFIER_MAT23 26
626 #define TYPE_SPECIFIER_MAT32 27
627 #define TYPE_SPECIFIER_MAT24 28
628 #define TYPE_SPECIFIER_MAT42 29
629 #define TYPE_SPECIFIER_MAT34 30
630 #define TYPE_SPECIFIER_MAT43 31
631 #define TYPE_SPECIFIER_COUNT 32
634 parse_type_specifier(slang_parse_ctx
* C
, slang_output_ctx
* O
,
635 slang_type_specifier
* spec
)
638 case TYPE_SPECIFIER_VOID
:
639 spec
->type
= SLANG_SPEC_VOID
;
641 case TYPE_SPECIFIER_BOOL
:
642 spec
->type
= SLANG_SPEC_BOOL
;
644 case TYPE_SPECIFIER_BVEC2
:
645 spec
->type
= SLANG_SPEC_BVEC2
;
647 case TYPE_SPECIFIER_BVEC3
:
648 spec
->type
= SLANG_SPEC_BVEC3
;
650 case TYPE_SPECIFIER_BVEC4
:
651 spec
->type
= SLANG_SPEC_BVEC4
;
653 case TYPE_SPECIFIER_INT
:
654 spec
->type
= SLANG_SPEC_INT
;
656 case TYPE_SPECIFIER_IVEC2
:
657 spec
->type
= SLANG_SPEC_IVEC2
;
659 case TYPE_SPECIFIER_IVEC3
:
660 spec
->type
= SLANG_SPEC_IVEC3
;
662 case TYPE_SPECIFIER_IVEC4
:
663 spec
->type
= SLANG_SPEC_IVEC4
;
665 case TYPE_SPECIFIER_FLOAT
:
666 spec
->type
= SLANG_SPEC_FLOAT
;
668 case TYPE_SPECIFIER_VEC2
:
669 spec
->type
= SLANG_SPEC_VEC2
;
671 case TYPE_SPECIFIER_VEC3
:
672 spec
->type
= SLANG_SPEC_VEC3
;
674 case TYPE_SPECIFIER_VEC4
:
675 spec
->type
= SLANG_SPEC_VEC4
;
677 case TYPE_SPECIFIER_MAT2
:
678 spec
->type
= SLANG_SPEC_MAT2
;
680 case TYPE_SPECIFIER_MAT3
:
681 spec
->type
= SLANG_SPEC_MAT3
;
683 case TYPE_SPECIFIER_MAT4
:
684 spec
->type
= SLANG_SPEC_MAT4
;
686 case TYPE_SPECIFIER_MAT23
:
687 spec
->type
= SLANG_SPEC_MAT23
;
689 case TYPE_SPECIFIER_MAT32
:
690 spec
->type
= SLANG_SPEC_MAT32
;
692 case TYPE_SPECIFIER_MAT24
:
693 spec
->type
= SLANG_SPEC_MAT24
;
695 case TYPE_SPECIFIER_MAT42
:
696 spec
->type
= SLANG_SPEC_MAT42
;
698 case TYPE_SPECIFIER_MAT34
:
699 spec
->type
= SLANG_SPEC_MAT34
;
701 case TYPE_SPECIFIER_MAT43
:
702 spec
->type
= SLANG_SPEC_MAT43
;
704 case TYPE_SPECIFIER_SAMPLER1D
:
705 spec
->type
= SLANG_SPEC_SAMPLER1D
;
707 case TYPE_SPECIFIER_SAMPLER2D
:
708 spec
->type
= SLANG_SPEC_SAMPLER2D
;
710 case TYPE_SPECIFIER_SAMPLER3D
:
711 spec
->type
= SLANG_SPEC_SAMPLER3D
;
713 case TYPE_SPECIFIER_SAMPLERCUBE
:
714 spec
->type
= SLANG_SPEC_SAMPLERCUBE
;
716 case TYPE_SPECIFIER_SAMPLER2DRECT
:
717 spec
->type
= SLANG_SPEC_SAMPLER2DRECT
;
719 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
720 spec
->type
= SLANG_SPEC_SAMPLER1DSHADOW
;
722 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
723 spec
->type
= SLANG_SPEC_SAMPLER2DSHADOW
;
725 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
726 spec
->type
= SLANG_SPEC_SAMPLER2DRECTSHADOW
;
728 case TYPE_SPECIFIER_STRUCT
:
729 spec
->type
= SLANG_SPEC_STRUCT
;
730 if (!parse_struct(C
, O
, &spec
->_struct
))
733 case TYPE_SPECIFIER_TYPENAME
:
734 spec
->type
= SLANG_SPEC_STRUCT
;
739 a_name
= parse_identifier(C
);
743 stru
= slang_struct_scope_find(O
->structs
, a_name
, 1);
745 slang_info_log_error(C
->L
, "undeclared type name '%s'",
746 slang_atom_pool_id(C
->atoms
, a_name
));
750 spec
->_struct
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
751 if (spec
->_struct
== NULL
) {
752 slang_info_log_memory(C
->L
);
755 if (!slang_struct_construct(spec
->_struct
)) {
756 _slang_free(spec
->_struct
);
757 spec
->_struct
= NULL
;
760 if (!slang_struct_copy(spec
->_struct
, stru
))
770 #define TYPE_SPECIFIER_NONARRAY 0
771 #define TYPE_SPECIFIER_ARRAY 1
774 parse_type_array_size(slang_parse_ctx
*C
,
781 case TYPE_SPECIFIER_NONARRAY
:
782 *array_len
= -1; /* -1 = not an array */
784 case TYPE_SPECIFIER_ARRAY
:
785 if (!parse_array_len(C
, O
, &size
))
787 *array_len
= (GLint
) size
;
796 #define PRECISION_DEFAULT 0
797 #define PRECISION_LOW 1
798 #define PRECISION_MEDIUM 2
799 #define PRECISION_HIGH 3
802 parse_type_precision(slang_parse_ctx
*C
,
803 slang_type_precision
*precision
)
805 GLint prec
= *C
->I
++;
807 case PRECISION_DEFAULT
:
808 *precision
= SLANG_PREC_DEFAULT
;
811 *precision
= SLANG_PREC_LOW
;
813 case PRECISION_MEDIUM
:
814 *precision
= SLANG_PREC_MEDIUM
;
817 *precision
= SLANG_PREC_HIGH
;
825 parse_fully_specified_type(slang_parse_ctx
* C
, slang_output_ctx
* O
,
826 slang_fully_specified_type
* type
)
828 if (!parse_type_variant(C
, &type
->variant
))
831 if (!parse_type_centroid(C
, &type
->centroid
))
834 if (!parse_type_qualifier(C
, &type
->qualifier
))
837 if (!parse_type_precision(C
, &type
->precision
))
840 if (!parse_type_specifier(C
, O
, &type
->specifier
))
843 if (!parse_type_array_size(C
, O
, &type
->array_len
))
846 if (!O
->allow_invariant
&& type
->variant
== SLANG_INVARIANT
) {
847 slang_info_log_error(C
->L
,
848 "'invariant' keyword not allowed (perhaps set #version 120)");
852 if (!O
->allow_centroid
&& type
->centroid
== SLANG_CENTROID
) {
853 slang_info_log_error(C
->L
,
854 "'centroid' keyword not allowed (perhaps set #version 120)");
857 else if (type
->centroid
== SLANG_CENTROID
&&
858 type
->qualifier
!= SLANG_QUAL_VARYING
) {
859 slang_info_log_error(C
->L
,
860 "'centroid' keyword only allowed for varying vars");
866 if (type->qualifier != SLANG_QUAL_VARYING &&
867 type->variant == SLANG_INVARIANT) {
868 slang_info_log_error(C->L,
869 "invariant qualifer only allowed for varying vars");
874 if (O
->allow_precision
) {
875 if (type
->precision
== SLANG_PREC_DEFAULT
) {
876 assert(type
->specifier
.type
< TYPE_SPECIFIER_COUNT
);
877 /* use the default precision for this datatype */
878 type
->precision
= O
->default_precision
[type
->specifier
.type
];
882 /* only default is allowed */
883 if (type
->precision
!= SLANG_PREC_DEFAULT
) {
884 slang_info_log_error(C
->L
, "precision qualifiers not allowed");
889 if (!O
->allow_array_types
&& type
->array_len
>= 0) {
890 slang_info_log_error(C
->L
, "first-class array types not allowed");
894 if (type
->array_len
>= 0) {
895 /* convert type to array type (ex: convert "int" to "array of int" */
896 promote_type_to_array(C
, type
, type
->array_len
);
904 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
905 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
909 #define OP_CONTINUE 6
912 #define OP_EXPRESSION 9
917 #define OP_PUSH_VOID 14
918 #define OP_PUSH_BOOL 15
919 #define OP_PUSH_INT 16
920 #define OP_PUSH_FLOAT 17
921 #define OP_PUSH_IDENTIFIER 18
922 #define OP_SEQUENCE 19
924 #define OP_ADDASSIGN 21
925 #define OP_SUBASSIGN 22
926 #define OP_MULASSIGN 23
927 #define OP_DIVASSIGN 24
928 /*#define OP_MODASSIGN 25*/
929 /*#define OP_LSHASSIGN 26*/
930 /*#define OP_RSHASSIGN 27*/
931 /*#define OP_ORASSIGN 28*/
932 /*#define OP_XORASSIGN 29*/
933 /*#define OP_ANDASSIGN 30*/
935 #define OP_LOGICALOR 32
936 #define OP_LOGICALXOR 33
937 #define OP_LOGICALAND 34
938 /*#define OP_BITOR 35*/
939 /*#define OP_BITXOR 36*/
940 /*#define OP_BITAND 37*/
942 #define OP_NOTEQUAL 39
944 #define OP_GREATER 41
945 #define OP_LESSEQUAL 42
946 #define OP_GREATEREQUAL 43
947 /*#define OP_LSHIFT 44*/
948 /*#define OP_RSHIFT 45*/
950 #define OP_SUBTRACT 47
951 #define OP_MULTIPLY 48
953 /*#define OP_MODULUS 50*/
954 #define OP_PREINCREMENT 51
955 #define OP_PREDECREMENT 52
958 /*#define OP_COMPLEMENT 55*/
960 #define OP_SUBSCRIPT 57
963 #define OP_POSTINCREMENT 60
964 #define OP_POSTDECREMENT 61
965 #define OP_PRECISION 62
970 * When parsing a compound production, this function is used to parse the
972 * For example, a while-loop compound will have two children, the
973 * while condition expression and the loop body. So, this function will
974 * be called twice to parse those two sub-expressions.
975 * \param C the parsing context
976 * \param O the output context
977 * \param oper the operation we're parsing
978 * \param statement indicates whether parsing a statement, or expression
979 * \return 1 if success, 0 if error
982 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
983 slang_operation
* oper
, GLboolean statement
)
987 /* grow child array */
988 ch
= slang_operation_grow(&oper
->num_children
, &oper
->children
);
990 return parse_statement(C
, O
, ch
);
991 return parse_expression(C
, O
, ch
);
994 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
997 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
998 slang_operation
* oper
)
1002 oper
->locals
->outer_scope
= O
->vars
;
1006 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
1007 /* parse child statements, do not create new variable scope */
1008 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1009 while (*C
->I
!= OP_END
)
1010 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1014 case OP_BLOCK_BEGIN_NEW_SCOPE
:
1015 /* parse child statements, create new variable scope */
1017 slang_output_ctx o
= *O
;
1019 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1020 o
.vars
= oper
->locals
;
1021 while (*C
->I
!= OP_END
)
1022 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1028 /* local variable declaration, individual declarators are stored as
1029 * children identifiers
1031 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1033 const unsigned int first_var
= O
->vars
->num_variables
;
1035 /* parse the declaration, note that there can be zero or more
1036 * than one declarators
1038 if (!parse_declaration(C
, O
))
1040 if (first_var
< O
->vars
->num_variables
) {
1041 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
1043 assert(oper
->num_children
== 0);
1044 oper
->num_children
= num_vars
;
1045 oper
->children
= slang_operation_new(num_vars
);
1046 if (oper
->children
== NULL
) {
1047 slang_info_log_memory(C
->L
);
1050 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
1051 slang_operation
*o
= &oper
->children
[i
- first_var
];
1052 slang_variable
*var
= O
->vars
->variables
[i
];
1053 o
->type
= SLANG_OPER_VARIABLE_DECL
;
1054 o
->locals
->outer_scope
= O
->vars
;
1055 o
->a_id
= var
->a_name
;
1058 calculate_var_size(C, O, var);
1061 if (!legal_identifier(o
->a_id
)) {
1062 slang_info_log_error(C
->L
, "illegal variable name '%s'",
1071 /* the __asm statement, parse the mnemonic and all its arguments
1074 oper
->type
= SLANG_OPER_ASM
;
1075 oper
->a_id
= parse_identifier(C
);
1076 if (oper
->a_id
== SLANG_ATOM_NULL
)
1078 while (*C
->I
!= OP_END
) {
1079 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1085 oper
->type
= SLANG_OPER_BREAK
;
1088 oper
->type
= SLANG_OPER_CONTINUE
;
1091 oper
->type
= SLANG_OPER_DISCARD
;
1094 oper
->type
= SLANG_OPER_RETURN
;
1095 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1099 oper
->type
= SLANG_OPER_EXPRESSION
;
1100 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1104 oper
->type
= SLANG_OPER_IF
;
1105 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1107 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1109 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1114 slang_output_ctx o
= *O
;
1116 oper
->type
= SLANG_OPER_WHILE
;
1117 o
.vars
= oper
->locals
;
1118 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1120 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1125 oper
->type
= SLANG_OPER_DO
;
1126 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1128 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1133 slang_output_ctx o
= *O
;
1135 oper
->type
= SLANG_OPER_FOR
;
1136 o
.vars
= oper
->locals
;
1137 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1139 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1141 if (!parse_child_operation(C
, &o
, oper
, GL_FALSE
))
1143 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1149 /* set default precision for a type in this scope */
1150 /* ignored at this time */
1151 int prec_qual
= *C
->I
++;
1152 int datatype
= *C
->I
++;
1158 /*printf("Unexpected operation %d\n", op);*/
1165 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
1166 slang_operation
** ops
, unsigned int *total_ops
,
1171 op
->children
= slang_operation_new(n
);
1172 if (op
->children
== NULL
) {
1173 slang_info_log_memory(C
->L
);
1176 op
->num_children
= n
;
1178 for (i
= 0; i
< n
; i
++) {
1179 slang_operation_destruct(&op
->children
[i
]);
1180 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
1183 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
1186 *ops
= (slang_operation
*)
1187 _slang_realloc(*ops
,
1188 (*total_ops
+ n
) * sizeof(slang_operation
),
1189 *total_ops
* sizeof(slang_operation
));
1191 slang_info_log_memory(C
->L
);
1198 is_constructor_name(const char *name
, slang_atom a_name
,
1199 slang_struct_scope
* structs
)
1201 if (slang_type_specifier_type_from_string(name
) != SLANG_SPEC_VOID
)
1203 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
1206 #define FUNCTION_CALL_NONARRAY 0
1207 #define FUNCTION_CALL_ARRAY 1
1210 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1211 slang_operation
* oper
)
1213 slang_operation
*ops
= NULL
;
1214 unsigned int num_ops
= 0;
1217 while (*C
->I
!= OP_END
) {
1218 slang_operation
*op
;
1219 const unsigned int op_code
= *C
->I
++;
1221 /* allocate default operation, becomes a no-op if not used */
1222 ops
= (slang_operation
*)
1224 num_ops
* sizeof(slang_operation
),
1225 (num_ops
+ 1) * sizeof(slang_operation
));
1227 slang_info_log_memory(C
->L
);
1231 if (!slang_operation_construct(op
)) {
1232 slang_info_log_memory(C
->L
);
1236 op
->locals
->outer_scope
= O
->vars
;
1240 op
->type
= SLANG_OPER_VOID
;
1243 op
->type
= SLANG_OPER_LITERAL_BOOL
;
1244 if (!parse_number(C
, &number
))
1249 op
->literal
[3] = (GLfloat
) number
;
1250 op
->literal_size
= 1;
1253 op
->type
= SLANG_OPER_LITERAL_INT
;
1254 if (!parse_number(C
, &number
))
1259 op
->literal
[3] = (GLfloat
) number
;
1260 op
->literal_size
= 1;
1263 op
->type
= SLANG_OPER_LITERAL_FLOAT
;
1264 if (!parse_float(C
, &op
->literal
[0]))
1268 op
->literal
[3] = op
->literal
[0];
1269 op
->literal_size
= 1;
1271 case OP_PUSH_IDENTIFIER
:
1272 op
->type
= SLANG_OPER_IDENTIFIER
;
1273 op
->a_id
= parse_identifier(C
);
1274 if (op
->a_id
== SLANG_ATOM_NULL
)
1278 op
->type
= SLANG_OPER_SEQUENCE
;
1279 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1283 op
->type
= SLANG_OPER_ASSIGN
;
1284 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1288 op
->type
= SLANG_OPER_ADDASSIGN
;
1289 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1293 op
->type
= SLANG_OPER_SUBASSIGN
;
1294 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1298 op
->type
= SLANG_OPER_MULASSIGN
;
1299 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1303 op
->type
= SLANG_OPER_DIVASSIGN
;
1304 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1307 /*case OP_MODASSIGN: */
1308 /*case OP_LSHASSIGN: */
1309 /*case OP_RSHASSIGN: */
1310 /*case OP_ORASSIGN: */
1311 /*case OP_XORASSIGN: */
1312 /*case OP_ANDASSIGN: */
1314 op
->type
= SLANG_OPER_SELECT
;
1315 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1319 op
->type
= SLANG_OPER_LOGICALOR
;
1320 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1324 op
->type
= SLANG_OPER_LOGICALXOR
;
1325 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1329 op
->type
= SLANG_OPER_LOGICALAND
;
1330 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1334 /*case OP_BITXOR: */
1335 /*case OP_BITAND: */
1337 op
->type
= SLANG_OPER_EQUAL
;
1338 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1342 op
->type
= SLANG_OPER_NOTEQUAL
;
1343 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1347 op
->type
= SLANG_OPER_LESS
;
1348 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1352 op
->type
= SLANG_OPER_GREATER
;
1353 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1357 op
->type
= SLANG_OPER_LESSEQUAL
;
1358 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1361 case OP_GREATEREQUAL
:
1362 op
->type
= SLANG_OPER_GREATEREQUAL
;
1363 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1366 /*case OP_LSHIFT: */
1367 /*case OP_RSHIFT: */
1369 op
->type
= SLANG_OPER_ADD
;
1370 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1374 op
->type
= SLANG_OPER_SUBTRACT
;
1375 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1379 op
->type
= SLANG_OPER_MULTIPLY
;
1380 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1384 op
->type
= SLANG_OPER_DIVIDE
;
1385 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1388 /*case OP_MODULUS: */
1389 case OP_PREINCREMENT
:
1390 op
->type
= SLANG_OPER_PREINCREMENT
;
1391 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1394 case OP_PREDECREMENT
:
1395 op
->type
= SLANG_OPER_PREDECREMENT
;
1396 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1400 op
->type
= SLANG_OPER_PLUS
;
1401 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1405 op
->type
= SLANG_OPER_MINUS
;
1406 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1410 op
->type
= SLANG_OPER_NOT
;
1411 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1414 /*case OP_COMPLEMENT: */
1416 op
->type
= SLANG_OPER_SUBSCRIPT
;
1417 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1421 op
->type
= SLANG_OPER_METHOD
;
1422 op
->a_obj
= parse_identifier(C
);
1423 if (op
->a_obj
== SLANG_ATOM_NULL
)
1426 op
->a_id
= parse_identifier(C
);
1427 if (op
->a_id
== SLANG_ATOM_NULL
)
1430 assert(*C
->I
== OP_END
);
1433 while (*C
->I
!= OP_END
)
1434 if (!parse_child_operation(C
, O
, op
, GL_FALSE
))
1438 /* don't lookup the method (not yet anyway) */
1439 if (!C
->parsing_builtin
1440 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1443 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1444 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1445 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1453 GLboolean array_constructor
= GL_FALSE
;
1454 GLint array_constructor_size
= 0;
1456 op
->type
= SLANG_OPER_CALL
;
1457 op
->a_id
= parse_identifier(C
);
1458 if (op
->a_id
== SLANG_ATOM_NULL
)
1461 case FUNCTION_CALL_NONARRAY
:
1462 /* Nothing to do. */
1464 case FUNCTION_CALL_ARRAY
:
1465 /* Calling an array constructor. For example:
1466 * float[3](1.1, 2.2, 3.3);
1468 if (!O
->allow_array_types
) {
1469 slang_info_log_error(C
->L
,
1470 "array constructors not allowed "
1471 "in this GLSL version");
1475 /* parse the array constructor size */
1476 slang_operation array_size
;
1477 array_constructor
= GL_TRUE
;
1478 slang_operation_construct(&array_size
);
1479 if (!parse_expression(C
, O
, &array_size
)) {
1480 slang_operation_destruct(&array_size
);
1483 if (array_size
.type
!= SLANG_OPER_LITERAL_INT
) {
1484 slang_info_log_error(C
->L
,
1485 "constructor array size is not an integer");
1486 slang_operation_destruct(&array_size
);
1489 array_constructor_size
= (int) array_size
.literal
[0];
1490 op
->array_constructor
= GL_TRUE
;
1491 slang_operation_destruct(&array_size
);
1498 while (*C
->I
!= OP_END
)
1499 if (!parse_child_operation(C
, O
, op
, GL_FALSE
))
1503 if (array_constructor
&&
1504 array_constructor_size
!= op
->num_children
) {
1505 slang_info_log_error(C
->L
, "number of parameters to array"
1506 " constructor does not match array size");
1510 if (!C
->parsing_builtin
1511 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1514 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1515 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1516 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1523 op
->type
= SLANG_OPER_FIELD
;
1524 op
->a_id
= parse_identifier(C
);
1525 if (op
->a_id
== SLANG_ATOM_NULL
)
1527 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1530 case OP_POSTINCREMENT
:
1531 op
->type
= SLANG_OPER_POSTINCREMENT
;
1532 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1535 case OP_POSTDECREMENT
:
1536 op
->type
= SLANG_OPER_POSTDECREMENT
;
1537 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1546 slang_operation_destruct(oper
);
1547 *oper
= *ops
; /* struct copy */
1553 /* parameter qualifier */
1554 #define PARAM_QUALIFIER_IN 0
1555 #define PARAM_QUALIFIER_OUT 1
1556 #define PARAM_QUALIFIER_INOUT 2
1558 /* function parameter array presence */
1559 #define PARAMETER_ARRAY_NOT_PRESENT 0
1560 #define PARAMETER_ARRAY_PRESENT 1
1563 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1564 slang_variable
* param
)
1566 int param_qual
, precision_qual
;
1568 /* parse and validate the parameter's type qualifiers (there can be
1569 * two at most) because not all combinations are valid
1571 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1574 param_qual
= *C
->I
++;
1575 switch (param_qual
) {
1576 case PARAM_QUALIFIER_IN
:
1577 if (param
->type
.qualifier
!= SLANG_QUAL_CONST
1578 && param
->type
.qualifier
!= SLANG_QUAL_NONE
) {
1579 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1583 case PARAM_QUALIFIER_OUT
:
1584 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1585 param
->type
.qualifier
= SLANG_QUAL_OUT
;
1587 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1591 case PARAM_QUALIFIER_INOUT
:
1592 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1593 param
->type
.qualifier
= SLANG_QUAL_INOUT
;
1595 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1603 /* parse precision qualifier (lowp, mediump, highp */
1604 precision_qual
= *C
->I
++;
1605 /* ignored at this time */
1606 (void) precision_qual
;
1608 /* parse parameter's type specifier and name */
1609 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1611 if (!parse_type_array_size(C
, O
, ¶m
->type
.array_len
))
1613 param
->a_name
= parse_identifier(C
);
1614 if (param
->a_name
== SLANG_ATOM_NULL
)
1617 /* first-class array
1619 if (param
->type
.array_len
>= 0) {
1620 slang_type_specifier p
;
1622 slang_type_specifier_ctr(&p
);
1623 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1624 slang_type_specifier_dtr(&p
);
1627 if (!convert_to_array(C
, param
, &p
)) {
1628 slang_type_specifier_dtr(&p
);
1631 slang_type_specifier_dtr(&p
);
1632 param
->array_len
= param
->type
.array_len
;
1635 /* if the parameter is an array, parse its size (the size must be
1636 * explicitly defined
1638 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1639 slang_type_specifier p
;
1641 if (param
->type
.array_len
>= 0) {
1642 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
1645 slang_type_specifier_ctr(&p
);
1646 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1647 slang_type_specifier_dtr(&p
);
1650 if (!convert_to_array(C
, param
, &p
)) {
1651 slang_type_specifier_dtr(&p
);
1654 slang_type_specifier_dtr(&p
);
1655 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1660 /* calculate the parameter size */
1661 if (!calculate_var_size(C
, O
, param
))
1664 /* TODO: allocate the local address here? */
1669 #define FUNCTION_ORDINARY 0
1670 #define FUNCTION_CONSTRUCTOR 1
1671 #define FUNCTION_OPERATOR 2
1673 /* function parameter */
1674 #define PARAMETER_NONE 0
1675 #define PARAMETER_NEXT 1
1678 #define OPERATOR_ADDASSIGN 1
1679 #define OPERATOR_SUBASSIGN 2
1680 #define OPERATOR_MULASSIGN 3
1681 #define OPERATOR_DIVASSIGN 4
1682 /*#define OPERATOR_MODASSIGN 5*/
1683 /*#define OPERATOR_LSHASSIGN 6*/
1684 /*#define OPERATOR_RSHASSIGN 7*/
1685 /*#define OPERATOR_ANDASSIGN 8*/
1686 /*#define OPERATOR_XORASSIGN 9*/
1687 /*#define OPERATOR_ORASSIGN 10*/
1688 #define OPERATOR_LOGICALXOR 11
1689 /*#define OPERATOR_BITOR 12*/
1690 /*#define OPERATOR_BITXOR 13*/
1691 /*#define OPERATOR_BITAND 14*/
1692 #define OPERATOR_LESS 15
1693 #define OPERATOR_GREATER 16
1694 #define OPERATOR_LESSEQUAL 17
1695 #define OPERATOR_GREATEREQUAL 18
1696 /*#define OPERATOR_LSHIFT 19*/
1697 /*#define OPERATOR_RSHIFT 20*/
1698 #define OPERATOR_MULTIPLY 21
1699 #define OPERATOR_DIVIDE 22
1700 /*#define OPERATOR_MODULUS 23*/
1701 #define OPERATOR_INCREMENT 24
1702 #define OPERATOR_DECREMENT 25
1703 #define OPERATOR_PLUS 26
1704 #define OPERATOR_MINUS 27
1705 /*#define OPERATOR_COMPLEMENT 28*/
1706 #define OPERATOR_NOT 29
1710 unsigned int o_code
;
1712 } operator_names
[] = {
1713 {OPERATOR_INCREMENT
, "++"},
1714 {OPERATOR_ADDASSIGN
, "+="},
1715 {OPERATOR_PLUS
, "+"},
1716 {OPERATOR_DECREMENT
, "--"},
1717 {OPERATOR_SUBASSIGN
, "-="},
1718 {OPERATOR_MINUS
, "-"},
1719 {OPERATOR_NOT
, "!"},
1720 {OPERATOR_MULASSIGN
, "*="},
1721 {OPERATOR_MULTIPLY
, "*"},
1722 {OPERATOR_DIVASSIGN
, "/="},
1723 {OPERATOR_DIVIDE
, "/"},
1724 {OPERATOR_LESSEQUAL
, "<="},
1725 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1726 /*{ OPERATOR_LSHIFT, "<<" }, */
1727 {OPERATOR_LESS
, "<"},
1728 {OPERATOR_GREATEREQUAL
, ">="},
1729 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1730 /*{ OPERATOR_RSHIFT, ">>" }, */
1731 {OPERATOR_GREATER
, ">"},
1732 /*{ OPERATOR_MODASSIGN, "%=" }, */
1733 /*{ OPERATOR_MODULUS, "%" }, */
1734 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1735 /*{ OPERATOR_BITAND, "&" }, */
1736 /*{ OPERATOR_ORASSIGN, "|=" }, */
1737 /*{ OPERATOR_BITOR, "|" }, */
1738 /*{ OPERATOR_COMPLEMENT, "~" }, */
1739 /*{ OPERATOR_XORASSIGN, "^=" }, */
1740 {OPERATOR_LOGICALXOR
, "^^"},
1741 /*{ OPERATOR_BITXOR, "^" } */
1745 parse_operator_name(slang_parse_ctx
* C
)
1749 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1750 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1752 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1753 if (atom
== SLANG_ATOM_NULL
) {
1754 slang_info_log_memory(C
->L
);
1766 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1767 slang_function
* func
)
1770 /* parse function type and name */
1771 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1776 case FUNCTION_ORDINARY
:
1777 func
->kind
= SLANG_FUNC_ORDINARY
;
1778 func
->header
.a_name
= parse_identifier(C
);
1779 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1782 case FUNCTION_CONSTRUCTOR
:
1783 func
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1784 if (func
->header
.type
.specifier
.type
== SLANG_SPEC_STRUCT
)
1786 func
->header
.a_name
=
1787 slang_atom_pool_atom(C
->atoms
,
1788 slang_type_specifier_type_to_string
1789 (func
->header
.type
.specifier
.type
));
1790 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1791 slang_info_log_memory(C
->L
);
1795 case FUNCTION_OPERATOR
:
1796 func
->kind
= SLANG_FUNC_OPERATOR
;
1797 func
->header
.a_name
= parse_operator_name(C
);
1798 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1805 if (!legal_identifier(func
->header
.a_name
)) {
1806 slang_info_log_error(C
->L
, "illegal function name '%s'",
1807 (char *) func
->header
.a_name
);
1811 /* parse function parameters */
1812 while (*C
->I
++ == PARAMETER_NEXT
) {
1813 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1815 slang_info_log_memory(C
->L
);
1818 if (!parse_parameter_declaration(C
, O
, p
))
1822 /* if the function returns a value, append a hidden __retVal 'out'
1823 * parameter that corresponds to the return value.
1825 if (_slang_function_has_return_value(func
)) {
1826 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1827 slang_atom a_retVal
= slang_atom_pool_atom(C
->atoms
, "__retVal");
1829 p
->a_name
= a_retVal
;
1830 p
->type
= func
->header
.type
;
1831 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1834 /* function formal parameters and local variables share the same
1835 * scope, so save the information about param count in a seperate
1836 * place also link the scope to the global variable scope so when a
1837 * given identifier is not found here, the search process continues
1838 * in the global space
1840 func
->param_count
= func
->parameters
->num_variables
;
1841 func
->parameters
->outer_scope
= O
->vars
;
1847 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1848 slang_function
* func
)
1850 slang_output_ctx o
= *O
;
1852 if (!parse_function_prototype(C
, O
, func
))
1855 /* create function's body operation */
1856 func
->body
= (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1857 if (func
->body
== NULL
) {
1858 slang_info_log_memory(C
->L
);
1861 if (!slang_operation_construct(func
->body
)) {
1862 _slang_free(func
->body
);
1864 slang_info_log_memory(C
->L
);
1868 /* to parse the body the parse context is modified in order to
1869 * capture parsed variables into function's local variable scope
1871 C
->global_scope
= GL_FALSE
;
1872 o
.vars
= func
->parameters
;
1873 if (!parse_statement(C
, &o
, func
->body
))
1876 C
->global_scope
= GL_TRUE
;
1881 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1883 slang_operation op_id
, op_assign
;
1886 /* construct the left side of assignment */
1887 if (!slang_operation_construct(&op_id
))
1889 op_id
.type
= SLANG_OPER_IDENTIFIER
;
1890 op_id
.a_id
= var
->a_name
;
1892 /* put the variable into operation's scope */
1893 op_id
.locals
->variables
=
1894 (slang_variable
**) _slang_alloc(sizeof(slang_variable
*));
1895 if (op_id
.locals
->variables
== NULL
) {
1896 slang_operation_destruct(&op_id
);
1899 op_id
.locals
->num_variables
= 1;
1900 op_id
.locals
->variables
[0] = var
;
1902 /* construct the assignment expression */
1903 if (!slang_operation_construct(&op_assign
)) {
1904 op_id
.locals
->num_variables
= 0;
1905 slang_operation_destruct(&op_id
);
1908 op_assign
.type
= SLANG_OPER_ASSIGN
;
1909 op_assign
.children
=
1910 (slang_operation
*) _slang_alloc(2 * sizeof(slang_operation
));
1911 if (op_assign
.children
== NULL
) {
1912 slang_operation_destruct(&op_assign
);
1913 op_id
.locals
->num_variables
= 0;
1914 slang_operation_destruct(&op_id
);
1917 op_assign
.num_children
= 2;
1918 op_assign
.children
[0] = op_id
;
1919 op_assign
.children
[1] = *var
->initializer
;
1923 /* carefully destroy the operations */
1924 op_assign
.num_children
= 0;
1925 _slang_free(op_assign
.children
);
1926 op_assign
.children
= NULL
;
1927 slang_operation_destruct(&op_assign
);
1928 op_id
.locals
->num_variables
= 0;
1929 slang_operation_destruct(&op_id
);
1937 /* init declarator list */
1938 #define DECLARATOR_NONE 0
1939 #define DECLARATOR_NEXT 1
1941 /* variable declaration */
1942 #define VARIABLE_NONE 0
1943 #define VARIABLE_IDENTIFIER 1
1944 #define VARIABLE_INITIALIZER 2
1945 #define VARIABLE_ARRAY_EXPLICIT 3
1946 #define VARIABLE_ARRAY_UNKNOWN 4
1950 * Parse the initializer for a variable declaration.
1953 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1954 const slang_fully_specified_type
* type
)
1956 GET_CURRENT_CONTEXT(ctx
); /* a hack */
1957 slang_variable
*var
;
1960 /* empty init declatator (without name, e.g. "float ;") */
1961 if (*C
->I
++ == VARIABLE_NONE
)
1964 a_name
= parse_identifier(C
);
1966 /* check if name is already in this scope */
1967 if (_slang_variable_locate(O
->vars
, a_name
, GL_FALSE
)) {
1968 slang_info_log_error(C
->L
,
1969 "declaration of '%s' conflicts with previous declaration",
1974 /* make room for the new variable and initialize it */
1975 var
= slang_variable_scope_grow(O
->vars
);
1977 slang_info_log_memory(C
->L
);
1981 /* copy the declarator type qualifier/etc info, parse the identifier */
1982 var
->type
.qualifier
= type
->qualifier
;
1983 var
->type
.centroid
= type
->centroid
;
1984 var
->type
.precision
= type
->precision
;
1985 var
->type
.variant
= type
->variant
;
1986 var
->type
.array_len
= type
->array_len
;
1987 var
->a_name
= a_name
;
1988 if (var
->a_name
== SLANG_ATOM_NULL
)
1993 /* simple variable declarator - just copy the specifier */
1994 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1997 case VARIABLE_INITIALIZER
:
1998 /* initialized variable - copy the specifier and parse the expression */
1999 if (0 && type
->array_len
>= 0) {
2000 /* The type was something like "float[4]" */
2001 convert_to_array(C
, var
, &type
->specifier
);
2002 var
->array_len
= type
->array_len
;
2005 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
2009 (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
2010 if (var
->initializer
== NULL
) {
2011 slang_info_log_memory(C
->L
);
2014 if (!slang_operation_construct(var
->initializer
)) {
2015 _slang_free(var
->initializer
);
2016 var
->initializer
= NULL
;
2017 slang_info_log_memory(C
->L
);
2020 if (!parse_expression(C
, O
, var
->initializer
))
2023 case VARIABLE_ARRAY_UNKNOWN
:
2024 /* unsized array - mark it as array and copy the specifier to
2027 if (type
->array_len
>= 0) {
2028 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
2031 if (!convert_to_array(C
, var
, &type
->specifier
))
2034 case VARIABLE_ARRAY_EXPLICIT
:
2035 if (type
->array_len
>= 0) {
2036 /* the user is trying to do something like: float[2] x[3]; */
2037 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
2040 if (!convert_to_array(C
, var
, &type
->specifier
))
2042 if (!parse_array_len(C
, O
, &var
->array_len
))
2049 /* allocate global address space for a variable with a known size */
2051 && !(var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
2052 && var
->array_len
== 0)) {
2053 if (!calculate_var_size(C
, O
, var
))
2057 /* emit code for global var decl */
2058 if (C
->global_scope
) {
2059 slang_assemble_ctx A
;
2060 memset(&A
, 0, sizeof(slang_assemble_ctx
));
2062 A
.space
.funcs
= O
->funs
;
2063 A
.space
.structs
= O
->structs
;
2064 A
.space
.vars
= O
->vars
;
2065 A
.program
= O
->program
;
2066 A
.pragmas
= O
->pragmas
;
2067 A
.vartable
= O
->vartable
;
2069 A
.curFuncEndLabel
= NULL
;
2070 A
.EmitContReturn
= ctx
->Shader
.EmitContReturn
;
2071 if (!_slang_codegen_global_variable(&A
, var
, C
->type
))
2075 /* initialize global variable */
2076 if (C
->global_scope
) {
2077 if (var
->initializer
!= NULL
) {
2078 slang_assemble_ctx A
;
2079 memset(&A
, 0, sizeof(slang_assemble_ctx
));
2081 A
.space
.funcs
= O
->funs
;
2082 A
.space
.structs
= O
->structs
;
2083 A
.space
.vars
= O
->vars
;
2084 if (!initialize_global(&A
, var
))
2092 * Parse a list of variable declarations. Each variable may have an
2096 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2098 slang_fully_specified_type type
;
2100 /* parse the fully specified type, common to all declarators */
2101 if (!slang_fully_specified_type_construct(&type
))
2103 if (!parse_fully_specified_type(C
, O
, &type
)) {
2104 slang_fully_specified_type_destruct(&type
);
2108 /* parse declarators, pass-in the parsed type */
2110 if (!parse_init_declarator(C
, O
, &type
)) {
2111 slang_fully_specified_type_destruct(&type
);
2115 while (*C
->I
++ == DECLARATOR_NEXT
);
2117 slang_fully_specified_type_destruct(&type
);
2123 * Parse a function definition or declaration.
2124 * \param C parsing context
2125 * \param O output context
2126 * \param definition if non-zero expect a definition, else a declaration
2127 * \param parsed_func_ret returns the parsed function
2128 * \return GL_TRUE if success, GL_FALSE if failure
2131 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
2132 slang_function
** parsed_func_ret
)
2134 slang_function parsed_func
, *found_func
;
2136 /* parse function definition/declaration */
2137 if (!slang_function_construct(&parsed_func
))
2140 if (!parse_function_definition(C
, O
, &parsed_func
)) {
2141 slang_function_destruct(&parsed_func
);
2146 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
2147 slang_function_destruct(&parsed_func
);
2152 /* find a function with a prototype matching the parsed one - only
2153 * the current scope is being searched to allow built-in function
2156 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
2157 if (found_func
== NULL
) {
2158 /* New function, add it to the function list */
2159 O
->funs
->functions
=
2160 (slang_function
*) _slang_realloc(O
->funs
->functions
,
2161 O
->funs
->num_functions
2162 * sizeof(slang_function
),
2163 (O
->funs
->num_functions
+ 1)
2164 * sizeof(slang_function
));
2165 if (O
->funs
->functions
== NULL
) {
2166 /* Make sure that there are no functions marked, as the
2167 * allocation is currently NULL, in order to avoid
2168 * a potental segfault as we clean up later.
2170 O
->funs
->num_functions
= 0;
2172 slang_info_log_memory(C
->L
);
2173 slang_function_destruct(&parsed_func
);
2176 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
2177 O
->funs
->num_functions
++;
2179 /* return the newly parsed function */
2180 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
2183 /* previously defined or declared */
2184 /* TODO: check function return type qualifiers and specifiers */
2186 if (found_func
->body
!= NULL
) {
2187 slang_info_log_error(C
->L
, "%s: function already has a body.",
2188 slang_atom_pool_id(C
->atoms
,
2191 slang_function_destruct(&parsed_func
);
2195 /* destroy the existing function declaration and replace it
2198 slang_function_destruct(found_func
);
2199 *found_func
= parsed_func
;
2202 /* another declaration of the same function prototype - ignore it */
2203 slang_function_destruct(&parsed_func
);
2206 /* return the found function */
2207 *parsed_func_ret
= found_func
;
2214 #define DECLARATION_FUNCTION_PROTOTYPE 1
2215 #define DECLARATION_INIT_DECLARATOR_LIST 2
2218 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2221 case DECLARATION_INIT_DECLARATOR_LIST
:
2222 if (!parse_init_declarator_list(C
, O
))
2225 case DECLARATION_FUNCTION_PROTOTYPE
:
2227 slang_function
*dummy_func
;
2229 if (!parse_function(C
, O
, 0, &dummy_func
))
2240 parse_default_precision(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2242 int precision
, type
;
2244 if (!O
->allow_precision
) {
2245 slang_info_log_error(C
->L
, "syntax error at \"precision\"");
2249 precision
= *C
->I
++;
2250 switch (precision
) {
2252 case PRECISION_MEDIUM
:
2253 case PRECISION_HIGH
:
2257 _mesa_problem(NULL
, "unexpected precision %d at %s:%d\n",
2258 precision
, __FILE__
, __LINE__
);
2264 case TYPE_SPECIFIER_FLOAT
:
2265 case TYPE_SPECIFIER_INT
:
2266 case TYPE_SPECIFIER_SAMPLER1D
:
2267 case TYPE_SPECIFIER_SAMPLER2D
:
2268 case TYPE_SPECIFIER_SAMPLER3D
:
2269 case TYPE_SPECIFIER_SAMPLERCUBE
:
2270 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
2271 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
2272 case TYPE_SPECIFIER_SAMPLER2DRECT
:
2273 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
2277 _mesa_problem(NULL
, "unexpected type %d at %s:%d\n",
2278 type
, __FILE__
, __LINE__
);
2282 assert(type
< TYPE_SPECIFIER_COUNT
);
2283 O
->default_precision
[type
] = precision
;
2290 * Initialize the default precision for all types.
2291 * XXX this info isn't used yet.
2294 init_default_precision(slang_output_ctx
*O
, slang_unit_type type
)
2297 for (i
= 0; i
< TYPE_SPECIFIER_COUNT
; i
++) {
2298 #if FEATURE_es2_glsl
2299 O
->default_precision
[i
] = PRECISION_LOW
;
2301 O
->default_precision
[i
] = PRECISION_HIGH
;
2305 if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2306 O
->default_precision
[TYPE_SPECIFIER_FLOAT
] = PRECISION_HIGH
;
2307 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_HIGH
;
2310 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_MEDIUM
;
2316 parse_invariant(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2318 if (O
->allow_invariant
) {
2319 slang_atom
*a
= parse_identifier(C
);
2320 /* XXX not doing anything with this var yet */
2321 /*printf("ID: %s\n", (char*) a);*/
2325 slang_info_log_error(C
->L
, "syntax error at \"invariant\"");
2331 /* external declaration or default precision specifier */
2332 #define EXTERNAL_NULL 0
2333 #define EXTERNAL_FUNCTION_DEFINITION 1
2334 #define EXTERNAL_DECLARATION 2
2335 #define DEFAULT_PRECISION 3
2336 #define INVARIANT_STMT 4
2340 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
,
2341 struct gl_shader
*shader
)
2343 GET_CURRENT_CONTEXT(ctx
);
2347 slang_function
*mainFunc
= NULL
;
2349 if (unit
->type
== SLANG_UNIT_FRAGMENT_BUILTIN
||
2350 unit
->type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2351 maxRegs
= ctx
->Const
.FragmentProgram
.MaxTemps
;
2354 assert(unit
->type
== SLANG_UNIT_VERTEX_BUILTIN
||
2355 unit
->type
== SLANG_UNIT_VERTEX_SHADER
);
2356 maxRegs
= ctx
->Const
.VertexProgram
.MaxTemps
;
2359 /* setup output context */
2360 o
.funs
= &unit
->funs
;
2361 o
.structs
= &unit
->structs
;
2362 o
.vars
= &unit
->vars
;
2363 o
.program
= shader
? shader
->Program
: NULL
;
2364 o
.pragmas
= shader
? &shader
->Pragmas
: NULL
;
2365 o
.vartable
= _slang_new_var_table(maxRegs
);
2366 _slang_push_var_table(o
.vartable
);
2368 /* allow 'invariant' keyword? */
2369 #if FEATURE_es2_glsl
2370 o
.allow_invariant
= GL_TRUE
;
2372 o
.allow_invariant
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2375 /* allow 'centroid' keyword? */
2376 o
.allow_centroid
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2378 /* allow 'lowp/mediump/highp' keywords? */
2379 #if FEATURE_es2_glsl
2380 o
.allow_precision
= GL_TRUE
;
2382 o
.allow_precision
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2384 init_default_precision(&o
, unit
->type
);
2386 /* allow 'float[]' keyword? */
2387 o
.allow_array_types
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2389 /* parse individual functions and declarations */
2390 while (*C
->I
!= EXTERNAL_NULL
) {
2392 case EXTERNAL_FUNCTION_DEFINITION
:
2394 slang_function
*func
;
2395 success
= parse_function(C
, &o
, 1, &func
);
2397 _mesa_strcmp((char *) func
->header
.a_name
, "main") == 0) {
2403 case EXTERNAL_DECLARATION
:
2404 success
= parse_declaration(C
, &o
);
2406 case DEFAULT_PRECISION
:
2407 success
= parse_default_precision(C
, &o
);
2409 case INVARIANT_STMT
:
2410 success
= parse_invariant(C
, &o
);
2417 /* xxx free codegen */
2418 _slang_pop_var_table(o
.vartable
);
2425 /* assemble (generate code) for main() */
2426 slang_assemble_ctx A
;
2427 memset(&A
, 0, sizeof(slang_assemble_ctx
));
2429 A
.space
.funcs
= o
.funs
;
2430 A
.space
.structs
= o
.structs
;
2431 A
.space
.vars
= o
.vars
;
2432 A
.program
= o
.program
;
2433 A
.pragmas
= &shader
->Pragmas
;
2434 A
.vartable
= o
.vartable
;
2435 A
.EmitContReturn
= ctx
->Shader
.EmitContReturn
;
2438 /* main() takes no parameters */
2439 if (mainFunc
->param_count
> 0) {
2440 slang_info_log_error(A
.log
, "main() takes no arguments");
2444 _slang_codegen_function(&A
, mainFunc
);
2446 shader
->Main
= GL_TRUE
; /* this shader defines main() */
2448 shader
->UnresolvedRefs
= A
.UnresolvedRefs
;
2451 _slang_pop_var_table(o
.vartable
);
2452 _slang_delete_var_table(o
.vartable
);
2458 compile_binary(const byte
* prod
, slang_code_unit
* unit
,
2460 slang_unit_type type
, slang_info_log
* infolog
,
2461 slang_code_unit
* builtin
, slang_code_unit
* downlink
,
2462 struct gl_shader
*shader
)
2468 /* setup parse context */
2471 C
.parsing_builtin
= (builtin
== NULL
);
2472 C
.global_scope
= GL_TRUE
;
2473 C
.atoms
= &unit
->object
->atompool
;
2475 C
.version
= version
;
2477 if (!check_revision(&C
))
2480 if (downlink
!= NULL
) {
2481 unit
->vars
.outer_scope
= &downlink
->vars
;
2482 unit
->funs
.outer_scope
= &downlink
->funs
;
2483 unit
->structs
.outer_scope
= &downlink
->structs
;
2486 /* parse translation unit */
2487 return parse_code_unit(&C
, unit
, shader
);
2491 compile_with_grammar(grammar id
, const char *source
, slang_code_unit
* unit
,
2492 slang_unit_type type
, slang_info_log
* infolog
,
2493 slang_code_unit
* builtin
,
2494 struct gl_shader
*shader
,
2495 const struct gl_extensions
*extensions
,
2496 struct gl_sl_pragmas
*pragmas
)
2499 GLuint size
, start
, version
;
2500 slang_string preprocessed
;
2503 #if FEATURE_ARB_shading_language_120
2505 #elif FEATURE_es2_glsl
2511 /* First retrieve the version number. */
2512 if (!_slang_preprocess_version(source
, &version
, &start
, infolog
))
2515 if (version
> maxVersion
) {
2516 slang_info_log_error(infolog
,
2517 "language version %.2f is not supported.",
2522 /* Now preprocess the source string. */
2523 slang_string_init(&preprocessed
);
2524 if (!_slang_preprocess_directives(&preprocessed
, &source
[start
],
2525 infolog
, extensions
, pragmas
)) {
2526 slang_string_free(&preprocessed
);
2527 slang_info_log_error(infolog
, "failed to preprocess the source.");
2531 /* Finally check the syntax and generate its binary representation. */
2532 if (!grammar_fast_check(id
,
2533 (const byte
*) (slang_string_cstr(&preprocessed
)),
2534 &prod
, &size
, 65536)) {
2538 slang_string_free(&preprocessed
);
2539 grammar_get_last_error((byte
*) (buf
), sizeof(buf
), &pos
);
2540 slang_info_log_error(infolog
, buf
);
2541 /* syntax error (possibly in library code) */
2546 s
= (char *) _mesa_find_line_column((const GLubyte
*) source
,
2547 (const GLubyte
*) source
+ pos
,
2549 printf("Error on line %d, col %d: %s\n", line
, col
, s
);
2554 slang_string_free(&preprocessed
);
2556 /* Syntax is okay - translate it to internal representation. */
2557 if (!compile_binary(prod
, unit
, version
, type
, infolog
, builtin
,
2558 &builtin
[SLANG_BUILTIN_TOTAL
- 1],
2560 grammar_alloc_free(prod
);
2563 grammar_alloc_free(prod
);
2567 LONGSTRING
static const char *slang_shader_syn
=
2568 #include "library/slang_shader_syn.h"
2571 static const byte slang_core_gc
[] = {
2572 #include "library/slang_core_gc.h"
2575 static const byte slang_120_core_gc
[] = {
2576 #include "library/slang_120_core_gc.h"
2579 static const byte slang_120_fragment_gc
[] = {
2580 #include "library/slang_builtin_120_fragment_gc.h"
2583 static const byte slang_common_builtin_gc
[] = {
2584 #include "library/slang_common_builtin_gc.h"
2587 static const byte slang_fragment_builtin_gc
[] = {
2588 #include "library/slang_fragment_builtin_gc.h"
2591 static const byte slang_vertex_builtin_gc
[] = {
2592 #include "library/slang_vertex_builtin_gc.h"
2596 compile_object(grammar
* id
, const char *source
, slang_code_object
* object
,
2597 slang_unit_type type
, slang_info_log
* infolog
,
2598 struct gl_shader
*shader
,
2599 const struct gl_extensions
*extensions
,
2600 struct gl_sl_pragmas
*pragmas
)
2602 slang_code_unit
*builtins
= NULL
;
2603 GLuint base_version
= 110;
2605 /* load GLSL grammar */
2606 *id
= grammar_load_from_text((const byte
*) (slang_shader_syn
));
2611 grammar_get_last_error(buf
, 1024, &pos
);
2612 slang_info_log_error(infolog
, (const char *) (buf
));
2616 /* set shader type - the syntax is slightly different for different shaders */
2617 if (type
== SLANG_UNIT_FRAGMENT_SHADER
2618 || type
== SLANG_UNIT_FRAGMENT_BUILTIN
)
2619 grammar_set_reg8(*id
, (const byte
*) "shader_type", 1);
2621 grammar_set_reg8(*id
, (const byte
*) "shader_type", 2);
2623 /* enable language extensions */
2624 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2626 /* if parsing user-specified shader, load built-in library */
2627 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_VERTEX_SHADER
) {
2628 /* compile core functionality first */
2629 if (!compile_binary(slang_core_gc
,
2630 &object
->builtin
[SLANG_BUILTIN_CORE
],
2632 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2636 #if FEATURE_ARB_shading_language_120
2637 if (!compile_binary(slang_120_core_gc
,
2638 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2640 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2641 NULL
, &object
->builtin
[SLANG_BUILTIN_CORE
], NULL
))
2645 /* compile common functions and variables, link to core */
2646 if (!compile_binary(slang_common_builtin_gc
,
2647 &object
->builtin
[SLANG_BUILTIN_COMMON
],
2648 #if FEATURE_ARB_shading_language_120
2653 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2654 #if FEATURE_ARB_shading_language_120
2655 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2657 &object
->builtin
[SLANG_BUILTIN_CORE
],
2662 /* compile target-specific functions and variables, link to common */
2663 if (type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2664 if (!compile_binary(slang_fragment_builtin_gc
,
2665 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2667 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2668 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2670 #if FEATURE_ARB_shading_language_120
2671 if (!compile_binary(slang_120_fragment_gc
,
2672 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2674 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2675 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2679 else if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2680 if (!compile_binary(slang_vertex_builtin_gc
,
2681 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2683 SLANG_UNIT_VERTEX_BUILTIN
, infolog
, NULL
,
2684 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2688 /* disable language extensions */
2689 #if NEW_SLANG /* allow-built-ins */
2690 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2692 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 0);
2694 builtins
= object
->builtin
;
2697 /* compile the actual shader - pass-in built-in library for external shader */
2698 return compile_with_grammar(*id
, source
, &object
->unit
, type
, infolog
,
2699 builtins
, shader
, extensions
, pragmas
);
2704 compile_shader(GLcontext
*ctx
, slang_code_object
* object
,
2705 slang_unit_type type
, slang_info_log
* infolog
,
2706 struct gl_shader
*shader
)
2711 #if 0 /* for debug */
2712 _mesa_printf("********* COMPILE SHADER ***********\n");
2713 _mesa_printf("%s\n", shader
->Source
);
2714 _mesa_printf("************************************\n");
2717 assert(shader
->Program
);
2719 _slang_code_object_dtr(object
);
2720 _slang_code_object_ctr(object
);
2722 success
= compile_object(&id
, shader
->Source
, object
, type
, infolog
, shader
,
2723 &ctx
->Extensions
, &shader
->Pragmas
);
2725 grammar_destroy(id
);
2735 _slang_compile(GLcontext
*ctx
, struct gl_shader
*shader
)
2738 slang_info_log info_log
;
2739 slang_code_object obj
;
2740 slang_unit_type type
;
2742 if (shader
->Type
== GL_VERTEX_SHADER
) {
2743 type
= SLANG_UNIT_VERTEX_SHADER
;
2746 assert(shader
->Type
== GL_FRAGMENT_SHADER
);
2747 type
= SLANG_UNIT_FRAGMENT_SHADER
;
2750 if (!shader
->Source
)
2753 ctx
->Shader
.MemPool
= _slang_new_mempool(1024*1024);
2755 shader
->Main
= GL_FALSE
;
2757 if (!shader
->Program
) {
2759 if (shader
->Type
== GL_VERTEX_SHADER
)
2760 progTarget
= GL_VERTEX_PROGRAM_ARB
;
2762 progTarget
= GL_FRAGMENT_PROGRAM_ARB
;
2763 shader
->Program
= ctx
->Driver
.NewProgram(ctx
, progTarget
, 1);
2764 shader
->Program
->Parameters
= _mesa_new_parameter_list();
2765 shader
->Program
->Varying
= _mesa_new_parameter_list();
2766 shader
->Program
->Attributes
= _mesa_new_parameter_list();
2769 slang_info_log_construct(&info_log
);
2770 _slang_code_object_ctr(&obj
);
2772 success
= compile_shader(ctx
, &obj
, type
, &info_log
, shader
);
2774 /* free shader's prev info log */
2775 if (shader
->InfoLog
) {
2776 _mesa_free(shader
->InfoLog
);
2777 shader
->InfoLog
= NULL
;
2780 if (info_log
.text
) {
2781 /* copy info-log string to shader object */
2782 shader
->InfoLog
= _mesa_strdup(info_log
.text
);
2785 if (info_log
.error_flag
) {
2789 slang_info_log_destruct(&info_log
);
2790 _slang_code_object_dtr(&obj
);
2792 _slang_delete_mempool((slang_mempool
*) ctx
->Shader
.MemPool
);
2793 ctx
->Shader
.MemPool
= NULL
;
2795 /* remove any reads of output registers */
2797 printf("Pre-remove output reads:\n");
2798 _mesa_print_program(shader
->Program
);
2800 _mesa_remove_output_reads(shader
->Program
, PROGRAM_OUTPUT
);
2801 if (shader
->Type
== GL_VERTEX_SHADER
) {
2802 /* and remove writes to varying vars in vertex programs */
2803 _mesa_remove_output_reads(shader
->Program
, PROGRAM_VARYING
);
2806 printf("Post-remove output reads:\n");
2807 _mesa_print_program(shader
->Program
);
2810 shader
->CompileStatus
= success
;
2813 if (shader
->Pragmas
.Optimize
&&
2814 (ctx
->Shader
.Flags
& GLSL_NO_OPT
) == 0) {
2815 _mesa_optimize_program(ctx
, shader
->Program
);
2817 if ((ctx
->Shader
.Flags
& GLSL_NOP_VERT
) &&
2818 shader
->Program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
2819 _mesa_nop_vertex_program(ctx
,
2820 (struct gl_vertex_program
*) shader
->Program
);
2822 if ((ctx
->Shader
.Flags
& GLSL_NOP_FRAG
) &&
2823 shader
->Program
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
2824 _mesa_nop_fragment_program(ctx
,
2825 (struct gl_fragment_program
*) shader
->Program
);
2829 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
2830 _mesa_write_shader_to_file(shader
);