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 "program/program.h"
34 #include "program/programopt.h"
35 #include "program/prog_optimize.h"
36 #include "program/prog_print.h"
37 #include "program/prog_parameter.h"
38 #include "../../glsl/pp/sl_pp_public.h"
39 #include "../../glsl/cl/sl_cl_parse.h"
40 #include "slang_codegen.h"
41 #include "slang_compile.h"
42 #include "slang_storage.h"
43 #include "slang_log.h"
44 #include "slang_mem.h"
45 #include "slang_vartable.h"
46 #include "slang_simplify.h"
49 * This is a straightforward implementation of the slang front-end
50 * compiler. Lots of error-checking functionality is missing but
51 * every well-formed shader source should compile successfully and
52 * execute as expected. However, some semantically ill-formed shaders
53 * may be accepted resulting in undefined behaviour.
57 /** re-defined below, should be the same though */
58 #define TYPE_SPECIFIER_COUNT 36
62 * Check if the given identifier is legal.
65 legal_identifier(slang_atom name
)
67 /* "gl_" is a reserved prefix */
68 if (strncmp((char *) name
, "gl_", 3) == 0) {
80 _slang_code_unit_ctr(slang_code_unit
* self
,
81 struct slang_code_object_
* object
)
83 _slang_variable_scope_ctr(&self
->vars
);
84 _slang_function_scope_ctr(&self
->funs
);
85 _slang_struct_scope_ctr(&self
->structs
);
86 self
->object
= object
;
90 _slang_code_unit_dtr(slang_code_unit
* self
)
92 slang_variable_scope_destruct(&self
->vars
);
93 slang_function_scope_destruct(&self
->funs
);
94 slang_struct_scope_destruct(&self
->structs
);
102 _slang_code_object_ctr(slang_code_object
* self
)
106 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
107 _slang_code_unit_ctr(&self
->builtin
[i
], self
);
108 _slang_code_unit_ctr(&self
->unit
, self
);
109 slang_atom_pool_construct(&self
->atompool
);
113 _slang_code_object_dtr(slang_code_object
* self
)
117 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
118 _slang_code_unit_dtr(&self
->builtin
[i
]);
119 _slang_code_unit_dtr(&self
->unit
);
120 slang_atom_pool_destruct(&self
->atompool
);
124 /* slang_parse_ctx */
126 typedef struct slang_parse_ctx_
128 const unsigned char *I
;
131 GLboolean global_scope
; /**< Is object being declared a global? */
132 slang_atom_pool
*atoms
;
133 slang_unit_type type
; /**< Vertex vs. Fragment */
134 GLuint version
; /**< user-specified (or default) #version */
137 /* slang_output_ctx */
139 typedef struct slang_output_ctx_
141 slang_variable_scope
*vars
;
142 slang_function_scope
*funs
;
143 slang_struct_scope
*structs
;
144 struct gl_program
*program
;
145 struct gl_sl_pragmas
*pragmas
;
146 slang_var_table
*vartable
;
147 GLuint default_precision
[TYPE_SPECIFIER_COUNT
];
148 GLboolean allow_precision
;
149 GLboolean allow_invariant
;
150 GLboolean allow_centroid
;
151 GLboolean allow_array_types
; /* float[] syntax */
154 /* _slang_compile() */
157 /* Debugging aid, print file/line where parsing error is detected */
161 printf("slang error at %s:%d\n", __FILE__, __LINE__); \
167 parse_identifier_str(slang_parse_ctx
* C
, char **id
)
170 C
->I
+= strlen(*id
) + 1;
174 parse_identifier(slang_parse_ctx
* C
)
178 id
= (const char *) C
->I
;
179 C
->I
+= strlen(id
) + 1;
180 return slang_atom_pool_atom(C
->atoms
, id
);
186 return (c
>= '0' && c
<= '9') || (c
>= 'a' && c
<= 'f') || (c
>= 'A' && c
<= 'F');
190 parse_general_number(slang_parse_ctx
*ctx
, float *number
)
194 if (*ctx
->I
== '0') {
196 const unsigned char *pi
;
198 if (ctx
->I
[1] == 'x' || ctx
->I
[1] == 'X') {
200 if (!is_hex_digit(*ctx
->I
)) {
206 if (*ctx
->I
>= '0' && *ctx
->I
<= '9') {
207 digit
= (int)(*ctx
->I
- '0');
208 } else if (*ctx
->I
>= 'a' && *ctx
->I
<= 'f') {
209 digit
= (int)(*ctx
->I
- 'a') + 10;
211 digit
= (int)(*ctx
->I
- 'A') + 10;
213 value
= value
* 0x10 + digit
;
215 } while (is_hex_digit(*ctx
->I
));
216 if (*ctx
->I
!= '\0') {
220 *number
= (float)value
;
226 while (*pi
>= '0' && *pi
<= '7') {
229 digit
= (int)(*pi
- '0');
230 value
= value
* 010 + digit
;
236 *number
= (float)value
;
241 parse_identifier_str(ctx
, &flt
);
242 flt
= _mesa_strdup(flt
);
246 if (flt
[strlen(flt
) - 1] == 'f' || flt
[strlen(flt
) - 1] == 'F') {
247 flt
[strlen(flt
) - 1] = '\0';
249 *number
= _mesa_strtof(flt
, (char **)NULL
);
256 parse_number(slang_parse_ctx
* C
, int *number
)
258 const int radix
= (int) (*C
->I
++);
263 parse_general_number(C
, &f
);
267 while (*C
->I
!= '\0') {
269 if (*C
->I
>= '0' && *C
->I
<= '9')
270 digit
= (int) (*C
->I
- '0');
271 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
272 digit
= (int) (*C
->I
- 'A') + 10;
274 digit
= (int) (*C
->I
- 'a') + 10;
275 *number
= *number
* radix
+ digit
;
281 slang_info_log_warning(C
->L
, "%d: literal integer overflow.", *number
);
286 parse_float(slang_parse_ctx
* C
, float *number
)
290 parse_general_number(C
, number
);
292 char *integral
= NULL
;
293 char *fractional
= NULL
;
294 char *exponent
= NULL
;
297 parse_identifier_str(C
, &integral
);
298 parse_identifier_str(C
, &fractional
);
299 parse_identifier_str(C
, &exponent
);
301 whole
= (char *) _slang_alloc((strlen(integral
) +
303 strlen(exponent
) + 3) * sizeof(char));
305 slang_info_log_memory(C
->L
);
309 slang_string_copy(whole
, integral
);
310 slang_string_concat(whole
, ".");
311 slang_string_concat(whole
, fractional
);
312 slang_string_concat(whole
, "E");
313 slang_string_concat(whole
, exponent
);
315 *number
= _mesa_strtof(whole
, (char **) NULL
);
323 /* revision number - increment after each change affecting emitted output */
327 check_revision(slang_parse_ctx
* C
)
329 if (*C
->I
!= REVISION
) {
330 slang_info_log_error(C
->L
, "Internal compiler error.");
337 static int parse_statement(slang_parse_ctx
*, slang_output_ctx
*,
339 static int parse_expression(slang_parse_ctx
*, slang_output_ctx
*,
341 static int parse_type_specifier(slang_parse_ctx
*, slang_output_ctx
*,
342 slang_type_specifier
*);
344 parse_type_array_size(slang_parse_ctx
*C
,
349 parse_array_len(slang_parse_ctx
* C
, slang_output_ctx
* O
, GLuint
* len
)
351 slang_operation array_size
;
352 slang_name_space space
;
355 if (!slang_operation_construct(&array_size
))
357 if (!parse_expression(C
, O
, &array_size
)) {
358 slang_operation_destruct(&array_size
);
362 space
.funcs
= O
->funs
;
363 space
.structs
= O
->structs
;
364 space
.vars
= O
->vars
;
366 /* evaluate compile-time expression which is array size */
367 _slang_simplify(&array_size
, &space
, C
->atoms
);
369 if (array_size
.type
== SLANG_OPER_LITERAL_INT
) {
371 *len
= (GLint
) array_size
.literal
[0];
372 } else if (array_size
.type
== SLANG_OPER_IDENTIFIER
) {
373 slang_variable
*var
= _slang_variable_locate(array_size
.locals
, array_size
.a_id
, GL_TRUE
);
375 slang_info_log_error(C
->L
, "undefined variable '%s'",
376 (char *) array_size
.a_id
);
378 } else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&&
379 var
->type
.specifier
.type
== SLANG_SPEC_INT
) {
380 if (var
->initializer
&&
381 var
->initializer
->type
== SLANG_OPER_LITERAL_INT
) {
382 *len
= (GLint
) var
->initializer
->literal
[0];
385 slang_info_log_error(C
->L
, "unable to parse array size declaration");
389 slang_info_log_error(C
->L
, "unable to parse array size declaration");
396 slang_operation_destruct(&array_size
);
401 calculate_var_size(slang_parse_ctx
* C
, slang_output_ctx
* O
,
402 slang_variable
* var
)
404 slang_storage_aggregate agg
;
406 if (!slang_storage_aggregate_construct(&agg
))
408 if (!_slang_aggregate_variable(&agg
, &var
->type
.specifier
, var
->array_len
,
409 O
->funs
, O
->structs
, O
->vars
, C
->atoms
)) {
410 slang_storage_aggregate_destruct(&agg
);
413 var
->size
= _slang_sizeof_aggregate(&agg
);
414 slang_storage_aggregate_destruct(&agg
);
419 promote_type_to_array(slang_parse_ctx
*C
,
420 slang_fully_specified_type
*type
,
423 slang_type_specifier
*baseType
=
424 slang_type_specifier_new(type
->specifier
.type
, NULL
, NULL
);
426 type
->specifier
.type
= SLANG_SPEC_ARRAY
;
427 type
->specifier
._array
= baseType
;
428 type
->array_len
= array_len
;
433 convert_to_array(slang_parse_ctx
* C
, slang_variable
* var
,
434 const slang_type_specifier
* sp
)
436 /* sized array - mark it as array, copy the specifier to the array element
437 * and parse the expression */
438 var
->type
.specifier
.type
= SLANG_SPEC_ARRAY
;
439 var
->type
.specifier
._array
= (slang_type_specifier
*)
440 _slang_alloc(sizeof(slang_type_specifier
));
441 if (var
->type
.specifier
._array
== NULL
) {
442 slang_info_log_memory(C
->L
);
445 slang_type_specifier_ctr(var
->type
.specifier
._array
);
446 return slang_type_specifier_copy(var
->type
.specifier
._array
, sp
);
449 /* structure field */
452 #define FIELD_ARRAY 2
455 parse_struct_field_var(slang_parse_ctx
* C
, slang_output_ctx
* O
,
456 slang_variable
* var
, slang_atom a_name
,
457 const slang_type_specifier
* sp
,
460 var
->a_name
= a_name
;
461 if (var
->a_name
== SLANG_ATOM_NULL
)
466 if (array_len
!= -1) {
467 if (!convert_to_array(C
, var
, sp
))
469 var
->array_len
= array_len
;
472 if (!slang_type_specifier_copy(&var
->type
.specifier
, sp
))
479 if (!convert_to_array(C
, var
, sp
))
481 if (!parse_array_len(C
, O
, &var
->array_len
))
488 return calculate_var_size(C
, O
, var
);
492 parse_struct_field(slang_parse_ctx
* C
, slang_output_ctx
* O
,
493 slang_struct
* st
, slang_type_specifier
* sp
)
495 slang_output_ctx o
= *O
;
498 o
.structs
= st
->structs
;
499 if (!parse_type_specifier(C
, &o
, sp
))
501 if (!parse_type_array_size(C
, &o
, &array_len
))
506 slang_variable
*var
= slang_variable_scope_grow(st
->fields
);
508 slang_info_log_memory(C
->L
);
511 a_name
= parse_identifier(C
);
512 if (_slang_variable_locate(st
->fields
, a_name
, GL_FALSE
)) {
513 slang_info_log_error(C
->L
, "duplicate field '%s'", (char *) a_name
);
517 if (!parse_struct_field_var(C
, &o
, var
, a_name
, sp
, array_len
))
520 while (*C
->I
++ != FIELD_NONE
);
526 parse_struct(slang_parse_ctx
* C
, slang_output_ctx
* O
, slang_struct
** st
)
531 /* parse struct name (if any) and make sure it is unique in current scope */
532 a_name
= parse_identifier(C
);
533 if (a_name
== SLANG_ATOM_NULL
)
536 name
= slang_atom_pool_id(C
->atoms
, a_name
);
538 && slang_struct_scope_find(O
->structs
, a_name
, 0) != NULL
) {
539 slang_info_log_error(C
->L
, "%s: duplicate type name.", name
);
543 /* set-up a new struct */
544 *st
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
546 slang_info_log_memory(C
->L
);
549 if (!slang_struct_construct(*st
)) {
552 slang_info_log_memory(C
->L
);
555 (**st
).a_name
= a_name
;
556 (**st
).structs
->outer_scope
= O
->structs
;
558 /* parse individual struct fields */
560 slang_type_specifier sp
;
562 slang_type_specifier_ctr(&sp
);
563 if (!parse_struct_field(C
, O
, *st
, &sp
)) {
564 slang_type_specifier_dtr(&sp
);
567 slang_type_specifier_dtr(&sp
);
569 while (*C
->I
++ != FIELD_NONE
);
571 /* if named struct, copy it to current scope */
572 if (name
[0] != '\0') {
575 O
->structs
->structs
=
576 (slang_struct
*) _slang_realloc(O
->structs
->structs
,
577 O
->structs
->num_structs
578 * sizeof(slang_struct
),
579 (O
->structs
->num_structs
+ 1)
580 * sizeof(slang_struct
));
581 if (O
->structs
->structs
== NULL
) {
582 slang_info_log_memory(C
->L
);
585 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
586 if (!slang_struct_construct(s
))
588 O
->structs
->num_structs
++;
589 if (!slang_struct_copy(s
, *st
))
597 /* invariant qualifer */
598 #define TYPE_VARIANT 90
599 #define TYPE_INVARIANT 91
602 parse_type_variant(slang_parse_ctx
* C
, slang_type_variant
*variant
)
604 GLuint invariant
= *C
->I
++;
607 *variant
= SLANG_VARIANT
;
610 *variant
= SLANG_INVARIANT
;
618 /* centroid qualifer */
619 #define TYPE_CENTER 95
620 #define TYPE_CENTROID 96
623 parse_type_centroid(slang_parse_ctx
* C
, slang_type_centroid
*centroid
)
628 *centroid
= SLANG_CENTER
;
631 *centroid
= SLANG_CENTROID
;
639 /* Layout qualifiers */
640 #define LAYOUT_QUALIFIER_NONE 0
641 #define LAYOUT_QUALIFIER_UPPER_LEFT 1
642 #define LAYOUT_QUALIFIER_PIXEL_CENTER_INTEGER 2
645 parse_layout_qualifiers(slang_parse_ctx
* C
, slang_layout_qualifier
*layout
)
649 /* the layout qualifiers come as a list of LAYOUT_QUALIFER_x tokens,
650 * terminated by LAYOUT_QUALIFIER_NONE.
655 case LAYOUT_QUALIFIER_NONE
:
656 /* end of list of qualifiers */
658 case LAYOUT_QUALIFIER_UPPER_LEFT
:
659 *layout
|= SLANG_LAYOUT_UPPER_LEFT_BIT
;
661 case LAYOUT_QUALIFIER_PIXEL_CENTER_INTEGER
:
662 *layout
|= SLANG_LAYOUT_PIXEL_CENTER_INTEGER_BIT
;
665 assert(0 && "Bad layout qualifier");
672 #define TYPE_QUALIFIER_NONE 0
673 #define TYPE_QUALIFIER_CONST 1
674 #define TYPE_QUALIFIER_ATTRIBUTE 2
675 #define TYPE_QUALIFIER_VARYING 3
676 #define TYPE_QUALIFIER_UNIFORM 4
677 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
678 #define TYPE_QUALIFIER_FIXEDINPUT 6
681 parse_type_qualifier(slang_parse_ctx
* C
, slang_type_qualifier
* qual
)
683 GLuint qualifier
= *C
->I
++;
685 case TYPE_QUALIFIER_NONE
:
686 *qual
= SLANG_QUAL_NONE
;
688 case TYPE_QUALIFIER_CONST
:
689 *qual
= SLANG_QUAL_CONST
;
691 case TYPE_QUALIFIER_ATTRIBUTE
:
692 *qual
= SLANG_QUAL_ATTRIBUTE
;
694 case TYPE_QUALIFIER_VARYING
:
695 *qual
= SLANG_QUAL_VARYING
;
697 case TYPE_QUALIFIER_UNIFORM
:
698 *qual
= SLANG_QUAL_UNIFORM
;
700 case TYPE_QUALIFIER_FIXEDOUTPUT
:
701 *qual
= SLANG_QUAL_FIXEDOUTPUT
;
703 case TYPE_QUALIFIER_FIXEDINPUT
:
704 *qual
= SLANG_QUAL_FIXEDINPUT
;
713 #define TYPE_SPECIFIER_VOID 0
714 #define TYPE_SPECIFIER_BOOL 1
715 #define TYPE_SPECIFIER_BVEC2 2
716 #define TYPE_SPECIFIER_BVEC3 3
717 #define TYPE_SPECIFIER_BVEC4 4
718 #define TYPE_SPECIFIER_INT 5
719 #define TYPE_SPECIFIER_IVEC2 6
720 #define TYPE_SPECIFIER_IVEC3 7
721 #define TYPE_SPECIFIER_IVEC4 8
722 #define TYPE_SPECIFIER_FLOAT 9
723 #define TYPE_SPECIFIER_VEC2 10
724 #define TYPE_SPECIFIER_VEC3 11
725 #define TYPE_SPECIFIER_VEC4 12
726 #define TYPE_SPECIFIER_MAT2 13
727 #define TYPE_SPECIFIER_MAT3 14
728 #define TYPE_SPECIFIER_MAT4 15
729 #define TYPE_SPECIFIER_SAMPLER1D 16
730 #define TYPE_SPECIFIER_SAMPLER2D 17
731 #define TYPE_SPECIFIER_SAMPLER3D 18
732 #define TYPE_SPECIFIER_SAMPLERCUBE 19
733 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
734 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
735 #define TYPE_SPECIFIER_SAMPLER2DRECT 22
736 #define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
737 #define TYPE_SPECIFIER_STRUCT 24
738 #define TYPE_SPECIFIER_TYPENAME 25
739 #define TYPE_SPECIFIER_MAT23 26
740 #define TYPE_SPECIFIER_MAT32 27
741 #define TYPE_SPECIFIER_MAT24 28
742 #define TYPE_SPECIFIER_MAT42 29
743 #define TYPE_SPECIFIER_MAT34 30
744 #define TYPE_SPECIFIER_MAT43 31
745 #define TYPE_SPECIFIER_SAMPLER_1D_ARRAY 32
746 #define TYPE_SPECIFIER_SAMPLER_2D_ARRAY 33
747 #define TYPE_SPECIFIER_SAMPLER_1D_ARRAY_SHADOW 34
748 #define TYPE_SPECIFIER_SAMPLER_2D_ARRAY_SHADOW 35
749 #define TYPE_SPECIFIER_COUNT 36
752 parse_type_specifier(slang_parse_ctx
* C
, slang_output_ctx
* O
,
753 slang_type_specifier
* spec
)
757 case TYPE_SPECIFIER_VOID
:
758 spec
->type
= SLANG_SPEC_VOID
;
760 case TYPE_SPECIFIER_BOOL
:
761 spec
->type
= SLANG_SPEC_BOOL
;
763 case TYPE_SPECIFIER_BVEC2
:
764 spec
->type
= SLANG_SPEC_BVEC2
;
766 case TYPE_SPECIFIER_BVEC3
:
767 spec
->type
= SLANG_SPEC_BVEC3
;
769 case TYPE_SPECIFIER_BVEC4
:
770 spec
->type
= SLANG_SPEC_BVEC4
;
772 case TYPE_SPECIFIER_INT
:
773 spec
->type
= SLANG_SPEC_INT
;
775 case TYPE_SPECIFIER_IVEC2
:
776 spec
->type
= SLANG_SPEC_IVEC2
;
778 case TYPE_SPECIFIER_IVEC3
:
779 spec
->type
= SLANG_SPEC_IVEC3
;
781 case TYPE_SPECIFIER_IVEC4
:
782 spec
->type
= SLANG_SPEC_IVEC4
;
784 case TYPE_SPECIFIER_FLOAT
:
785 spec
->type
= SLANG_SPEC_FLOAT
;
787 case TYPE_SPECIFIER_VEC2
:
788 spec
->type
= SLANG_SPEC_VEC2
;
790 case TYPE_SPECIFIER_VEC3
:
791 spec
->type
= SLANG_SPEC_VEC3
;
793 case TYPE_SPECIFIER_VEC4
:
794 spec
->type
= SLANG_SPEC_VEC4
;
796 case TYPE_SPECIFIER_MAT2
:
797 spec
->type
= SLANG_SPEC_MAT2
;
799 case TYPE_SPECIFIER_MAT3
:
800 spec
->type
= SLANG_SPEC_MAT3
;
802 case TYPE_SPECIFIER_MAT4
:
803 spec
->type
= SLANG_SPEC_MAT4
;
805 case TYPE_SPECIFIER_MAT23
:
806 spec
->type
= SLANG_SPEC_MAT23
;
808 case TYPE_SPECIFIER_MAT32
:
809 spec
->type
= SLANG_SPEC_MAT32
;
811 case TYPE_SPECIFIER_MAT24
:
812 spec
->type
= SLANG_SPEC_MAT24
;
814 case TYPE_SPECIFIER_MAT42
:
815 spec
->type
= SLANG_SPEC_MAT42
;
817 case TYPE_SPECIFIER_MAT34
:
818 spec
->type
= SLANG_SPEC_MAT34
;
820 case TYPE_SPECIFIER_MAT43
:
821 spec
->type
= SLANG_SPEC_MAT43
;
823 case TYPE_SPECIFIER_SAMPLER1D
:
824 spec
->type
= SLANG_SPEC_SAMPLER_1D
;
826 case TYPE_SPECIFIER_SAMPLER2D
:
827 spec
->type
= SLANG_SPEC_SAMPLER_2D
;
829 case TYPE_SPECIFIER_SAMPLER3D
:
830 spec
->type
= SLANG_SPEC_SAMPLER_3D
;
832 case TYPE_SPECIFIER_SAMPLERCUBE
:
833 spec
->type
= SLANG_SPEC_SAMPLER_CUBE
;
835 case TYPE_SPECIFIER_SAMPLER2DRECT
:
836 spec
->type
= SLANG_SPEC_SAMPLER_RECT
;
838 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
839 spec
->type
= SLANG_SPEC_SAMPLER_1D_SHADOW
;
841 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
842 spec
->type
= SLANG_SPEC_SAMPLER_2D_SHADOW
;
844 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
845 spec
->type
= SLANG_SPEC_SAMPLER_RECT_SHADOW
;
847 case TYPE_SPECIFIER_SAMPLER_1D_ARRAY
:
848 spec
->type
= SLANG_SPEC_SAMPLER_1D_ARRAY
;
850 case TYPE_SPECIFIER_SAMPLER_2D_ARRAY
:
851 spec
->type
= SLANG_SPEC_SAMPLER_2D_ARRAY
;
853 case TYPE_SPECIFIER_SAMPLER_1D_ARRAY_SHADOW
:
854 spec
->type
= SLANG_SPEC_SAMPLER_1D_ARRAY_SHADOW
;
856 case TYPE_SPECIFIER_SAMPLER_2D_ARRAY_SHADOW
:
857 spec
->type
= SLANG_SPEC_SAMPLER_2D_ARRAY_SHADOW
;
859 case TYPE_SPECIFIER_STRUCT
:
860 spec
->type
= SLANG_SPEC_STRUCT
;
861 if (!parse_struct(C
, O
, &spec
->_struct
))
864 case TYPE_SPECIFIER_TYPENAME
:
865 spec
->type
= SLANG_SPEC_STRUCT
;
870 a_name
= parse_identifier(C
);
874 stru
= slang_struct_scope_find(O
->structs
, a_name
, 1);
876 slang_info_log_error(C
->L
, "undeclared type name '%s'",
877 slang_atom_pool_id(C
->atoms
, a_name
));
881 spec
->_struct
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
882 if (spec
->_struct
== NULL
) {
883 slang_info_log_memory(C
->L
);
886 if (!slang_struct_construct(spec
->_struct
)) {
887 _slang_free(spec
->_struct
);
888 spec
->_struct
= NULL
;
891 if (!slang_struct_copy(spec
->_struct
, stru
))
901 #define TYPE_SPECIFIER_NONARRAY 0
902 #define TYPE_SPECIFIER_ARRAY 1
905 parse_type_array_size(slang_parse_ctx
*C
,
912 case TYPE_SPECIFIER_NONARRAY
:
913 *array_len
= -1; /* -1 = not an array */
915 case TYPE_SPECIFIER_ARRAY
:
916 if (!parse_array_len(C
, O
, &size
))
918 *array_len
= (GLint
) size
;
927 #define PRECISION_DEFAULT 0
928 #define PRECISION_LOW 1
929 #define PRECISION_MEDIUM 2
930 #define PRECISION_HIGH 3
933 parse_type_precision(slang_parse_ctx
*C
,
934 slang_type_precision
*precision
)
936 GLint prec
= *C
->I
++;
938 case PRECISION_DEFAULT
:
939 *precision
= SLANG_PREC_DEFAULT
;
942 *precision
= SLANG_PREC_LOW
;
944 case PRECISION_MEDIUM
:
945 *precision
= SLANG_PREC_MEDIUM
;
948 *precision
= SLANG_PREC_HIGH
;
956 /* parameter qualifier */
957 #define PARAM_QUALIFIER_IN 0
958 #define PARAM_QUALIFIER_OUT 1
959 #define PARAM_QUALIFIER_INOUT 2
960 #define PARAM_QUALIFIER_NONE 3
962 parse_varying_qualifier(slang_parse_ctx
* C
, slang_fully_specified_type
*type
)
964 int param_qual
= *C
->I
++;
966 if (type
->qualifier
!= SLANG_QUAL_VARYING
&&
967 param_qual
!= PARAM_QUALIFIER_NONE
) {
968 slang_info_log_error(C
->L
, "Invalid type qualifier.");
971 switch (param_qual
) {
972 case PARAM_QUALIFIER_IN
:
973 case PARAM_QUALIFIER_NONE
:
974 type
->varying_kind
= SLANG_VARYING_IN
;
976 case PARAM_QUALIFIER_OUT
:
977 type
->varying_kind
= SLANG_VARYING_OUT
;
979 case PARAM_QUALIFIER_INOUT
:
980 slang_info_log_error(C
->L
, "Invalid type qualifier.");
990 parse_fully_specified_type(slang_parse_ctx
* C
, slang_output_ctx
* O
,
991 slang_fully_specified_type
* type
)
993 if (!parse_layout_qualifiers(C
, &type
->layout
))
996 if (!parse_type_variant(C
, &type
->variant
))
999 if (!parse_type_centroid(C
, &type
->centroid
))
1002 if (!parse_type_qualifier(C
, &type
->qualifier
))
1005 if (!parse_varying_qualifier(C
, type
))
1008 if (!parse_type_precision(C
, &type
->precision
))
1011 if (!parse_type_specifier(C
, O
, &type
->specifier
))
1014 if (!parse_type_array_size(C
, O
, &type
->array_len
))
1017 if (!O
->allow_invariant
&& type
->variant
== SLANG_INVARIANT
) {
1018 slang_info_log_error(C
->L
,
1019 "'invariant' keyword not allowed (perhaps set #version 120)");
1023 if (!O
->allow_centroid
&& type
->centroid
== SLANG_CENTROID
) {
1024 slang_info_log_error(C
->L
,
1025 "'centroid' keyword not allowed (perhaps set #version 120)");
1028 else if (type
->centroid
== SLANG_CENTROID
&&
1029 type
->qualifier
!= SLANG_QUAL_VARYING
) {
1030 slang_info_log_error(C
->L
,
1031 "'centroid' keyword only allowed for varying vars");
1037 if (type->qualifier != SLANG_QUAL_VARYING &&
1038 type->variant == SLANG_INVARIANT) {
1039 slang_info_log_error(C->L,
1040 "invariant qualifer only allowed for varying vars");
1045 if (O
->allow_precision
) {
1046 if (type
->precision
== SLANG_PREC_DEFAULT
) {
1047 assert(type
->specifier
.type
< TYPE_SPECIFIER_COUNT
);
1048 /* use the default precision for this datatype */
1049 type
->precision
= O
->default_precision
[type
->specifier
.type
];
1053 /* only default is allowed */
1054 if (type
->precision
!= SLANG_PREC_DEFAULT
) {
1055 slang_info_log_error(C
->L
, "precision qualifiers not allowed");
1060 if (!O
->allow_array_types
&& type
->array_len
>= 0) {
1061 slang_info_log_error(C
->L
, "first-class array types not allowed");
1065 if (type
->array_len
>= 0) {
1066 /* convert type to array type (ex: convert "int" to "array of int" */
1067 promote_type_to_array(C
, type
, type
->array_len
);
1075 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
1076 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
1077 #define OP_DECLARE 3
1080 #define OP_CONTINUE 6
1081 #define OP_DISCARD 7
1083 #define OP_EXPRESSION 9
1088 #define OP_PUSH_VOID 14
1089 #define OP_PUSH_BOOL 15
1090 #define OP_PUSH_INT 16
1091 #define OP_PUSH_FLOAT 17
1092 #define OP_PUSH_IDENTIFIER 18
1093 #define OP_SEQUENCE 19
1094 #define OP_ASSIGN 20
1095 #define OP_ADDASSIGN 21
1096 #define OP_SUBASSIGN 22
1097 #define OP_MULASSIGN 23
1098 #define OP_DIVASSIGN 24
1099 /*#define OP_MODASSIGN 25*/
1100 /*#define OP_LSHASSIGN 26*/
1101 /*#define OP_RSHASSIGN 27*/
1102 /*#define OP_ORASSIGN 28*/
1103 /*#define OP_XORASSIGN 29*/
1104 /*#define OP_ANDASSIGN 30*/
1105 #define OP_SELECT 31
1106 #define OP_LOGICALOR 32
1107 #define OP_LOGICALXOR 33
1108 #define OP_LOGICALAND 34
1109 /*#define OP_BITOR 35*/
1110 /*#define OP_BITXOR 36*/
1111 /*#define OP_BITAND 37*/
1113 #define OP_NOTEQUAL 39
1115 #define OP_GREATER 41
1116 #define OP_LESSEQUAL 42
1117 #define OP_GREATEREQUAL 43
1118 /*#define OP_LSHIFT 44*/
1119 /*#define OP_RSHIFT 45*/
1121 #define OP_SUBTRACT 47
1122 #define OP_MULTIPLY 48
1123 #define OP_DIVIDE 49
1124 /*#define OP_MODULUS 50*/
1125 #define OP_PREINCREMENT 51
1126 #define OP_PREDECREMENT 52
1129 /*#define OP_COMPLEMENT 55*/
1131 #define OP_SUBSCRIPT 57
1134 #define OP_POSTINCREMENT 60
1135 #define OP_POSTDECREMENT 61
1136 #define OP_PRECISION 62
1137 #define OP_METHOD 63
1141 * When parsing a compound production, this function is used to parse the
1143 * For example, a while-loop compound will have two children, the
1144 * while condition expression and the loop body. So, this function will
1145 * be called twice to parse those two sub-expressions.
1146 * \param C the parsing context
1147 * \param O the output context
1148 * \param oper the operation we're parsing
1149 * \param statement indicates whether parsing a statement, or expression
1150 * \return 1 if success, 0 if error
1153 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1154 slang_operation
* oper
, GLboolean statement
)
1156 slang_operation
*ch
;
1158 /* grow child array */
1159 ch
= slang_operation_grow(&oper
->num_children
, &oper
->children
);
1161 return parse_statement(C
, O
, ch
);
1162 return parse_expression(C
, O
, ch
);
1165 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
1168 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1169 slang_operation
* oper
)
1173 oper
->locals
->outer_scope
= O
->vars
;
1177 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
1178 /* parse child statements, do not create new variable scope */
1179 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1180 while (*C
->I
!= OP_END
)
1181 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1185 case OP_BLOCK_BEGIN_NEW_SCOPE
:
1186 /* parse child statements, create new variable scope */
1188 slang_output_ctx o
= *O
;
1190 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1191 o
.vars
= oper
->locals
;
1192 while (*C
->I
!= OP_END
)
1193 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1199 /* local variable declaration, individual declarators are stored as
1200 * children identifiers
1202 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1204 const unsigned int first_var
= O
->vars
->num_variables
;
1206 /* parse the declaration, note that there can be zero or more
1207 * than one declarators
1209 if (!parse_declaration(C
, O
))
1211 if (first_var
< O
->vars
->num_variables
) {
1212 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
1214 assert(oper
->num_children
== 0);
1215 oper
->num_children
= num_vars
;
1216 oper
->children
= slang_operation_new(num_vars
);
1217 if (oper
->children
== NULL
) {
1218 slang_info_log_memory(C
->L
);
1221 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
1222 slang_operation
*o
= &oper
->children
[i
- first_var
];
1223 slang_variable
*var
= O
->vars
->variables
[i
];
1224 o
->type
= SLANG_OPER_VARIABLE_DECL
;
1225 o
->locals
->outer_scope
= O
->vars
;
1226 o
->a_id
= var
->a_name
;
1229 calculate_var_size(C, O, var);
1232 if (!legal_identifier(o
->a_id
)) {
1233 slang_info_log_error(C
->L
, "illegal variable name '%s'",
1242 /* the __asm statement, parse the mnemonic and all its arguments
1245 oper
->type
= SLANG_OPER_ASM
;
1246 oper
->a_id
= parse_identifier(C
);
1247 if (oper
->a_id
== SLANG_ATOM_NULL
)
1249 while (*C
->I
!= OP_END
) {
1250 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1256 oper
->type
= SLANG_OPER_BREAK
;
1259 oper
->type
= SLANG_OPER_CONTINUE
;
1262 oper
->type
= SLANG_OPER_DISCARD
;
1265 oper
->type
= SLANG_OPER_RETURN
;
1266 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1270 oper
->type
= SLANG_OPER_EXPRESSION
;
1271 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1275 oper
->type
= SLANG_OPER_IF
;
1276 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1278 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1280 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1285 slang_output_ctx o
= *O
;
1287 oper
->type
= SLANG_OPER_WHILE
;
1288 o
.vars
= oper
->locals
;
1289 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1291 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1296 oper
->type
= SLANG_OPER_DO
;
1297 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1299 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1304 slang_output_ctx o
= *O
;
1306 oper
->type
= SLANG_OPER_FOR
;
1307 o
.vars
= oper
->locals
;
1308 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1310 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1312 if (!parse_child_operation(C
, &o
, oper
, GL_FALSE
))
1314 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1320 /* set default precision for a type in this scope */
1321 /* ignored at this time */
1322 int prec_qual
= *C
->I
++;
1323 int datatype
= *C
->I
++;
1329 /*printf("Unexpected operation %d\n", op);*/
1336 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
1337 slang_operation
** ops
, unsigned int *total_ops
,
1342 op
->children
= slang_operation_new(n
);
1343 if (op
->children
== NULL
) {
1344 slang_info_log_memory(C
->L
);
1347 op
->num_children
= n
;
1349 for (i
= 0; i
< n
; i
++) {
1350 slang_operation_destruct(&op
->children
[i
]);
1351 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
1354 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
1357 *ops
= (slang_operation
*)
1358 _slang_realloc(*ops
,
1359 (*total_ops
+ n
) * sizeof(slang_operation
),
1360 *total_ops
* sizeof(slang_operation
));
1362 slang_info_log_memory(C
->L
);
1369 is_constructor_name(const char *name
, slang_atom a_name
,
1370 slang_struct_scope
* structs
)
1372 if (slang_type_specifier_type_from_string(name
) != SLANG_SPEC_VOID
)
1374 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
1377 #define FUNCTION_CALL_NONARRAY 0
1378 #define FUNCTION_CALL_ARRAY 1
1381 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1382 slang_operation
* oper
)
1384 slang_operation
*ops
= NULL
;
1385 unsigned int num_ops
= 0;
1388 while (*C
->I
!= OP_END
) {
1389 slang_operation
*op
;
1390 const unsigned int op_code
= *C
->I
++;
1392 /* allocate default operation, becomes a no-op if not used */
1393 ops
= (slang_operation
*)
1395 num_ops
* sizeof(slang_operation
),
1396 (num_ops
+ 1) * sizeof(slang_operation
));
1398 slang_info_log_memory(C
->L
);
1402 if (!slang_operation_construct(op
)) {
1403 slang_info_log_memory(C
->L
);
1407 op
->locals
->outer_scope
= O
->vars
;
1411 op
->type
= SLANG_OPER_VOID
;
1414 op
->type
= SLANG_OPER_LITERAL_BOOL
;
1415 if (!parse_number(C
, &number
))
1420 op
->literal
[3] = (GLfloat
) number
;
1421 op
->literal_size
= 1;
1424 op
->type
= SLANG_OPER_LITERAL_INT
;
1425 if (!parse_number(C
, &number
))
1430 op
->literal
[3] = (GLfloat
) number
;
1431 op
->literal_size
= 1;
1434 op
->type
= SLANG_OPER_LITERAL_FLOAT
;
1435 if (!parse_float(C
, &op
->literal
[0]))
1439 op
->literal
[3] = op
->literal
[0];
1440 op
->literal_size
= 1;
1442 case OP_PUSH_IDENTIFIER
:
1443 op
->type
= SLANG_OPER_IDENTIFIER
;
1444 op
->a_id
= parse_identifier(C
);
1445 if (op
->a_id
== SLANG_ATOM_NULL
)
1449 op
->type
= SLANG_OPER_SEQUENCE
;
1450 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1454 op
->type
= SLANG_OPER_ASSIGN
;
1455 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1459 op
->type
= SLANG_OPER_ADDASSIGN
;
1460 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1464 op
->type
= SLANG_OPER_SUBASSIGN
;
1465 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1469 op
->type
= SLANG_OPER_MULASSIGN
;
1470 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1474 op
->type
= SLANG_OPER_DIVASSIGN
;
1475 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1478 /*case OP_MODASSIGN: */
1479 /*case OP_LSHASSIGN: */
1480 /*case OP_RSHASSIGN: */
1481 /*case OP_ORASSIGN: */
1482 /*case OP_XORASSIGN: */
1483 /*case OP_ANDASSIGN: */
1485 op
->type
= SLANG_OPER_SELECT
;
1486 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1490 op
->type
= SLANG_OPER_LOGICALOR
;
1491 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1495 op
->type
= SLANG_OPER_LOGICALXOR
;
1496 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1500 op
->type
= SLANG_OPER_LOGICALAND
;
1501 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1505 /*case OP_BITXOR: */
1506 /*case OP_BITAND: */
1508 op
->type
= SLANG_OPER_EQUAL
;
1509 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1513 op
->type
= SLANG_OPER_NOTEQUAL
;
1514 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1518 op
->type
= SLANG_OPER_LESS
;
1519 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1523 op
->type
= SLANG_OPER_GREATER
;
1524 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1528 op
->type
= SLANG_OPER_LESSEQUAL
;
1529 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1532 case OP_GREATEREQUAL
:
1533 op
->type
= SLANG_OPER_GREATEREQUAL
;
1534 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1537 /*case OP_LSHIFT: */
1538 /*case OP_RSHIFT: */
1540 op
->type
= SLANG_OPER_ADD
;
1541 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1545 op
->type
= SLANG_OPER_SUBTRACT
;
1546 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1550 op
->type
= SLANG_OPER_MULTIPLY
;
1551 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1555 op
->type
= SLANG_OPER_DIVIDE
;
1556 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1559 /*case OP_MODULUS: */
1560 case OP_PREINCREMENT
:
1561 op
->type
= SLANG_OPER_PREINCREMENT
;
1562 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1565 case OP_PREDECREMENT
:
1566 op
->type
= SLANG_OPER_PREDECREMENT
;
1567 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1571 op
->type
= SLANG_OPER_PLUS
;
1572 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1576 op
->type
= SLANG_OPER_MINUS
;
1577 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1581 op
->type
= SLANG_OPER_NOT
;
1582 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1585 /*case OP_COMPLEMENT: */
1587 op
->type
= SLANG_OPER_SUBSCRIPT
;
1588 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1592 op
->type
= SLANG_OPER_METHOD
;
1593 op
->a_obj
= parse_identifier(C
);
1594 if (op
->a_obj
== SLANG_ATOM_NULL
)
1597 op
->a_id
= parse_identifier(C
);
1598 if (op
->a_id
== SLANG_ATOM_NULL
)
1601 assert(*C
->I
== OP_END
);
1604 while (*C
->I
!= OP_END
)
1605 if (!parse_child_operation(C
, O
, op
, GL_FALSE
))
1609 /* don't lookup the method (not yet anyway) */
1610 if (!C
->parsing_builtin
1611 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1614 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1615 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1616 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1624 GLboolean array_constructor
= GL_FALSE
;
1625 GLint array_constructor_size
= 0;
1627 op
->type
= SLANG_OPER_CALL
;
1628 op
->a_id
= parse_identifier(C
);
1629 if (op
->a_id
== SLANG_ATOM_NULL
)
1632 case FUNCTION_CALL_NONARRAY
:
1633 /* Nothing to do. */
1635 case FUNCTION_CALL_ARRAY
:
1636 /* Calling an array constructor. For example:
1637 * float[3](1.1, 2.2, 3.3);
1639 if (!O
->allow_array_types
) {
1640 slang_info_log_error(C
->L
,
1641 "array constructors not allowed "
1642 "in this GLSL version");
1646 /* parse the array constructor size */
1647 slang_operation array_size
;
1648 array_constructor
= GL_TRUE
;
1649 slang_operation_construct(&array_size
);
1650 if (!parse_expression(C
, O
, &array_size
)) {
1651 slang_operation_destruct(&array_size
);
1654 if (array_size
.type
!= SLANG_OPER_LITERAL_INT
) {
1655 slang_info_log_error(C
->L
,
1656 "constructor array size is not an integer");
1657 slang_operation_destruct(&array_size
);
1660 array_constructor_size
= (int) array_size
.literal
[0];
1661 op
->array_constructor
= GL_TRUE
;
1662 slang_operation_destruct(&array_size
);
1669 while (*C
->I
!= OP_END
)
1670 if (!parse_child_operation(C
, O
, op
, GL_FALSE
))
1674 if (array_constructor
&&
1675 array_constructor_size
!= op
->num_children
) {
1676 slang_info_log_error(C
->L
, "number of parameters to array"
1677 " constructor does not match array size");
1681 if (!C
->parsing_builtin
1682 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1685 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1686 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1687 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1694 op
->type
= SLANG_OPER_FIELD
;
1695 op
->a_id
= parse_identifier(C
);
1696 if (op
->a_id
== SLANG_ATOM_NULL
)
1698 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1701 case OP_POSTINCREMENT
:
1702 op
->type
= SLANG_OPER_POSTINCREMENT
;
1703 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1706 case OP_POSTDECREMENT
:
1707 op
->type
= SLANG_OPER_POSTDECREMENT
;
1708 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1717 slang_operation_destruct(oper
);
1718 *oper
= *ops
; /* struct copy */
1724 /* function parameter array presence */
1725 #define PARAMETER_ARRAY_NOT_PRESENT 0
1726 #define PARAMETER_ARRAY_PRESENT 1
1729 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1730 slang_variable
* param
)
1732 int param_qual
, precision_qual
;
1734 /* parse and validate the parameter's type qualifiers (there can be
1735 * two at most) because not all combinations are valid
1737 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1740 param_qual
= *C
->I
++;
1741 switch (param_qual
) {
1742 case PARAM_QUALIFIER_IN
:
1743 if (param
->type
.qualifier
!= SLANG_QUAL_CONST
1744 && param
->type
.qualifier
!= SLANG_QUAL_NONE
) {
1745 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1749 case PARAM_QUALIFIER_OUT
:
1750 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1751 param
->type
.qualifier
= SLANG_QUAL_OUT
;
1753 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1757 case PARAM_QUALIFIER_INOUT
:
1758 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1759 param
->type
.qualifier
= SLANG_QUAL_INOUT
;
1761 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1765 case PARAM_QUALIFIER_NONE
:
1766 /* like IN but doesn't throw error */
1772 /* parse precision qualifier (lowp, mediump, highp */
1773 precision_qual
= *C
->I
++;
1774 /* ignored at this time */
1775 (void) precision_qual
;
1777 /* parse parameter's type specifier and name */
1778 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1780 if (!parse_type_array_size(C
, O
, ¶m
->type
.array_len
))
1782 param
->a_name
= parse_identifier(C
);
1783 if (param
->a_name
== SLANG_ATOM_NULL
)
1786 /* first-class array
1788 if (param
->type
.array_len
>= 0) {
1789 slang_type_specifier p
;
1791 slang_type_specifier_ctr(&p
);
1792 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1793 slang_type_specifier_dtr(&p
);
1796 if (!convert_to_array(C
, param
, &p
)) {
1797 slang_type_specifier_dtr(&p
);
1800 slang_type_specifier_dtr(&p
);
1801 param
->array_len
= param
->type
.array_len
;
1804 /* if the parameter is an array, parse its size (the size must be
1805 * explicitly defined
1807 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1808 slang_type_specifier p
;
1810 if (param
->type
.array_len
>= 0) {
1811 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
1814 slang_type_specifier_ctr(&p
);
1815 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1816 slang_type_specifier_dtr(&p
);
1819 if (!convert_to_array(C
, param
, &p
)) {
1820 slang_type_specifier_dtr(&p
);
1823 slang_type_specifier_dtr(&p
);
1824 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1829 /* calculate the parameter size */
1830 if (!calculate_var_size(C
, O
, param
))
1833 /* TODO: allocate the local address here? */
1838 #define FUNCTION_ORDINARY 0
1839 #define FUNCTION_CONSTRUCTOR 1
1840 #define FUNCTION_OPERATOR 2
1842 /* function parameter */
1843 #define PARAMETER_NONE 0
1844 #define PARAMETER_NEXT 1
1847 #define OPERATOR_ADDASSIGN 1
1848 #define OPERATOR_SUBASSIGN 2
1849 #define OPERATOR_MULASSIGN 3
1850 #define OPERATOR_DIVASSIGN 4
1851 /*#define OPERATOR_MODASSIGN 5*/
1852 /*#define OPERATOR_LSHASSIGN 6*/
1853 /*#define OPERATOR_RSHASSIGN 7*/
1854 /*#define OPERATOR_ANDASSIGN 8*/
1855 /*#define OPERATOR_XORASSIGN 9*/
1856 /*#define OPERATOR_ORASSIGN 10*/
1857 #define OPERATOR_LOGICALXOR 11
1858 /*#define OPERATOR_BITOR 12*/
1859 /*#define OPERATOR_BITXOR 13*/
1860 /*#define OPERATOR_BITAND 14*/
1861 #define OPERATOR_LESS 15
1862 #define OPERATOR_GREATER 16
1863 #define OPERATOR_LESSEQUAL 17
1864 #define OPERATOR_GREATEREQUAL 18
1865 /*#define OPERATOR_LSHIFT 19*/
1866 /*#define OPERATOR_RSHIFT 20*/
1867 #define OPERATOR_MULTIPLY 21
1868 #define OPERATOR_DIVIDE 22
1869 /*#define OPERATOR_MODULUS 23*/
1870 #define OPERATOR_INCREMENT 24
1871 #define OPERATOR_DECREMENT 25
1872 #define OPERATOR_PLUS 26
1873 #define OPERATOR_MINUS 27
1874 /*#define OPERATOR_COMPLEMENT 28*/
1875 #define OPERATOR_NOT 29
1879 unsigned int o_code
;
1881 } operator_names
[] = {
1882 {OPERATOR_INCREMENT
, "++"},
1883 {OPERATOR_ADDASSIGN
, "+="},
1884 {OPERATOR_PLUS
, "+"},
1885 {OPERATOR_DECREMENT
, "--"},
1886 {OPERATOR_SUBASSIGN
, "-="},
1887 {OPERATOR_MINUS
, "-"},
1888 {OPERATOR_NOT
, "!"},
1889 {OPERATOR_MULASSIGN
, "*="},
1890 {OPERATOR_MULTIPLY
, "*"},
1891 {OPERATOR_DIVASSIGN
, "/="},
1892 {OPERATOR_DIVIDE
, "/"},
1893 {OPERATOR_LESSEQUAL
, "<="},
1894 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1895 /*{ OPERATOR_LSHIFT, "<<" }, */
1896 {OPERATOR_LESS
, "<"},
1897 {OPERATOR_GREATEREQUAL
, ">="},
1898 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1899 /*{ OPERATOR_RSHIFT, ">>" }, */
1900 {OPERATOR_GREATER
, ">"},
1901 /*{ OPERATOR_MODASSIGN, "%=" }, */
1902 /*{ OPERATOR_MODULUS, "%" }, */
1903 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1904 /*{ OPERATOR_BITAND, "&" }, */
1905 /*{ OPERATOR_ORASSIGN, "|=" }, */
1906 /*{ OPERATOR_BITOR, "|" }, */
1907 /*{ OPERATOR_COMPLEMENT, "~" }, */
1908 /*{ OPERATOR_XORASSIGN, "^=" }, */
1909 {OPERATOR_LOGICALXOR
, "^^"},
1910 /*{ OPERATOR_BITXOR, "^" } */
1914 parse_operator_name(slang_parse_ctx
* C
)
1918 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1919 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1921 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1922 if (atom
== SLANG_ATOM_NULL
) {
1923 slang_info_log_memory(C
->L
);
1935 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1936 slang_function
* func
)
1939 /* parse function type and name */
1940 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1945 case FUNCTION_ORDINARY
:
1946 func
->kind
= SLANG_FUNC_ORDINARY
;
1947 func
->header
.a_name
= parse_identifier(C
);
1948 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1951 case FUNCTION_CONSTRUCTOR
:
1952 func
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1953 if (func
->header
.type
.specifier
.type
== SLANG_SPEC_STRUCT
)
1955 func
->header
.a_name
=
1956 slang_atom_pool_atom(C
->atoms
,
1957 slang_type_specifier_type_to_string
1958 (func
->header
.type
.specifier
.type
));
1959 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1960 slang_info_log_memory(C
->L
);
1964 case FUNCTION_OPERATOR
:
1965 func
->kind
= SLANG_FUNC_OPERATOR
;
1966 func
->header
.a_name
= parse_operator_name(C
);
1967 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1974 if (!legal_identifier(func
->header
.a_name
)) {
1975 slang_info_log_error(C
->L
, "illegal function name '%s'",
1976 (char *) func
->header
.a_name
);
1980 /* parse function parameters */
1981 while (*C
->I
++ == PARAMETER_NEXT
) {
1982 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1984 slang_info_log_memory(C
->L
);
1987 if (!parse_parameter_declaration(C
, O
, p
))
1991 /* if the function returns a value, append a hidden __retVal 'out'
1992 * parameter that corresponds to the return value.
1994 if (_slang_function_has_return_value(func
)) {
1995 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1996 slang_atom a_retVal
= slang_atom_pool_atom(C
->atoms
, "__retVal");
1998 p
->a_name
= a_retVal
;
1999 p
->type
= func
->header
.type
;
2000 p
->type
.qualifier
= SLANG_QUAL_OUT
;
2003 /* function formal parameters and local variables share the same
2004 * scope, so save the information about param count in a seperate
2005 * place also link the scope to the global variable scope so when a
2006 * given identifier is not found here, the search process continues
2007 * in the global space
2009 func
->param_count
= func
->parameters
->num_variables
;
2010 func
->parameters
->outer_scope
= O
->vars
;
2016 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
2017 slang_function
* func
)
2019 slang_output_ctx o
= *O
;
2021 if (!parse_function_prototype(C
, O
, func
))
2024 /* create function's body operation */
2025 func
->body
= (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
2026 if (func
->body
== NULL
) {
2027 slang_info_log_memory(C
->L
);
2030 if (!slang_operation_construct(func
->body
)) {
2031 _slang_free(func
->body
);
2033 slang_info_log_memory(C
->L
);
2037 /* to parse the body the parse context is modified in order to
2038 * capture parsed variables into function's local variable scope
2040 C
->global_scope
= GL_FALSE
;
2041 o
.vars
= func
->parameters
;
2042 if (!parse_statement(C
, &o
, func
->body
))
2045 C
->global_scope
= GL_TRUE
;
2050 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
2052 slang_operation op_id
, op_assign
;
2055 /* construct the left side of assignment */
2056 if (!slang_operation_construct(&op_id
))
2058 op_id
.type
= SLANG_OPER_IDENTIFIER
;
2059 op_id
.a_id
= var
->a_name
;
2061 /* put the variable into operation's scope */
2062 op_id
.locals
->variables
=
2063 (slang_variable
**) _slang_alloc(sizeof(slang_variable
*));
2064 if (op_id
.locals
->variables
== NULL
) {
2065 slang_operation_destruct(&op_id
);
2068 op_id
.locals
->num_variables
= 1;
2069 op_id
.locals
->variables
[0] = var
;
2071 /* construct the assignment expression */
2072 if (!slang_operation_construct(&op_assign
)) {
2073 op_id
.locals
->num_variables
= 0;
2074 slang_operation_destruct(&op_id
);
2077 op_assign
.type
= SLANG_OPER_ASSIGN
;
2078 op_assign
.children
=
2079 (slang_operation
*) _slang_alloc(2 * sizeof(slang_operation
));
2080 if (op_assign
.children
== NULL
) {
2081 slang_operation_destruct(&op_assign
);
2082 op_id
.locals
->num_variables
= 0;
2083 slang_operation_destruct(&op_id
);
2086 op_assign
.num_children
= 2;
2087 op_assign
.children
[0] = op_id
;
2088 op_assign
.children
[1] = *var
->initializer
;
2092 /* carefully destroy the operations */
2093 op_assign
.num_children
= 0;
2094 _slang_free(op_assign
.children
);
2095 op_assign
.children
= NULL
;
2096 slang_operation_destruct(&op_assign
);
2097 op_id
.locals
->num_variables
= 0;
2098 slang_operation_destruct(&op_id
);
2106 /* init declarator list */
2107 #define DECLARATOR_NONE 0
2108 #define DECLARATOR_NEXT 1
2110 /* variable declaration */
2111 #define VARIABLE_NONE 0
2112 #define VARIABLE_IDENTIFIER 1
2113 #define VARIABLE_INITIALIZER 2
2114 #define VARIABLE_ARRAY_EXPLICIT 3
2115 #define VARIABLE_ARRAY_UNKNOWN 4
2119 * Check if it's OK to re-declare a variable with the given new type.
2120 * This happens when applying layout qualifiers to gl_FragCoord or
2121 * (re)setting an array size.
2122 * If redeclaration is OK, return a pointer to the incoming variable
2123 * updated with new type info. Else return NULL;
2125 static slang_variable
*
2126 redeclare_variable(slang_variable
*var
,
2127 const slang_fully_specified_type
*type
)
2129 if (slang_fully_specified_types_compatible(&var
->type
, type
)) {
2130 /* replace orig var layout with new layout */
2131 var
->type
.layout
= type
->layout
;
2133 /* XXX there may be other type updates in the future here */
2143 * Parse the initializer for a variable declaration.
2146 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
2147 const slang_fully_specified_type
* type
)
2149 GET_CURRENT_CONTEXT(ctx
); /* a hack */
2150 slang_variable
*var
= NULL
, *prevDecl
;
2153 /* empty init declatator (without name, e.g. "float ;") */
2154 if (*C
->I
++ == VARIABLE_NONE
)
2157 a_name
= parse_identifier(C
);
2159 /* check if name is already in this scope */
2160 prevDecl
= _slang_variable_locate(O
->vars
, a_name
, C
->global_scope
);
2162 /* A var with this name has already been declared.
2163 * Check if redeclaring the var with a different type/layout is legal.
2165 if (C
->global_scope
) {
2166 var
= redeclare_variable(prevDecl
, type
);
2169 slang_info_log_error(C
->L
,
2170 "declaration of '%s' conflicts with previous declaration",
2177 /* make room for a new variable and initialize it */
2178 var
= slang_variable_scope_grow(O
->vars
);
2180 slang_info_log_memory(C
->L
);
2184 /* copy the declarator type qualifier/etc info, parse the identifier */
2185 var
->type
.qualifier
= type
->qualifier
;
2186 var
->type
.centroid
= type
->centroid
;
2187 var
->type
.precision
= type
->precision
;
2188 var
->type
.specifier
= type
->specifier
;/*new*/
2189 var
->type
.variant
= type
->variant
;
2190 var
->type
.layout
= type
->layout
;
2191 var
->type
.array_len
= type
->array_len
;
2192 var
->type
.varying_kind
= type
->varying_kind
;
2193 var
->a_name
= a_name
;
2194 if (var
->a_name
== SLANG_ATOM_NULL
)
2200 /* simple variable declarator - just copy the specifier */
2201 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
2204 case VARIABLE_INITIALIZER
:
2205 /* initialized variable - copy the specifier and parse the expression */
2206 if (0 && type
->array_len
>= 0) {
2207 /* The type was something like "float[4]" */
2208 convert_to_array(C
, var
, &type
->specifier
);
2209 var
->array_len
= type
->array_len
;
2212 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
2216 (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
2217 if (var
->initializer
== NULL
) {
2218 slang_info_log_memory(C
->L
);
2221 if (!slang_operation_construct(var
->initializer
)) {
2222 _slang_free(var
->initializer
);
2223 var
->initializer
= NULL
;
2224 slang_info_log_memory(C
->L
);
2227 if (!parse_expression(C
, O
, var
->initializer
))
2230 case VARIABLE_ARRAY_UNKNOWN
:
2231 /* unsized array - mark it as array and copy the specifier to
2234 if (type
->array_len
>= 0) {
2235 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
2238 if (!convert_to_array(C
, var
, &type
->specifier
))
2241 case VARIABLE_ARRAY_EXPLICIT
:
2242 if (type
->array_len
>= 0) {
2243 /* the user is trying to do something like: float[2] x[3]; */
2244 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
2247 if (!convert_to_array(C
, var
, &type
->specifier
))
2249 if (!parse_array_len(C
, O
, &var
->array_len
))
2256 /* allocate global address space for a variable with a known size */
2258 && !(var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
2259 && var
->array_len
== 0)) {
2260 if (!calculate_var_size(C
, O
, var
))
2264 /* emit code for global var decl */
2265 if (C
->global_scope
) {
2266 slang_assemble_ctx A
;
2267 memset(&A
, 0, sizeof(slang_assemble_ctx
));
2268 A
.allow_uniform_initializers
= C
->version
> 110;
2270 A
.space
.funcs
= O
->funs
;
2271 A
.space
.structs
= O
->structs
;
2272 A
.space
.vars
= O
->vars
;
2273 A
.program
= O
->program
;
2274 A
.pragmas
= O
->pragmas
;
2275 A
.vartable
= O
->vartable
;
2277 A
.curFuncEndLabel
= NULL
;
2278 A
.EmitContReturn
= ctx
->Shader
.EmitContReturn
;
2279 if (!_slang_codegen_global_variable(&A
, var
, C
->type
))
2283 /* initialize global variable */
2284 if (C
->global_scope
) {
2285 if (var
->initializer
!= NULL
) {
2286 slang_assemble_ctx A
;
2287 memset(&A
, 0, sizeof(slang_assemble_ctx
));
2288 A
.allow_uniform_initializers
= C
->version
> 110;
2290 A
.space
.funcs
= O
->funs
;
2291 A
.space
.structs
= O
->structs
;
2292 A
.space
.vars
= O
->vars
;
2293 if (!initialize_global(&A
, var
))
2298 if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
&&
2299 var
->a_name
== slang_atom_pool_atom(C
->atoms
, "gl_FragCoord")) {
2300 /* set the program's PixelCenterInteger, OriginUpperLeft fields */
2301 struct gl_fragment_program
*fragProg
=
2302 (struct gl_fragment_program
*) O
->program
;
2304 if (var
->type
.layout
& SLANG_LAYOUT_UPPER_LEFT_BIT
) {
2305 fragProg
->OriginUpperLeft
= GL_TRUE
;
2307 if (var
->type
.layout
& SLANG_LAYOUT_PIXEL_CENTER_INTEGER_BIT
) {
2308 fragProg
->PixelCenterInteger
= GL_TRUE
;
2316 * Parse a list of variable declarations. Each variable may have an
2320 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2322 slang_fully_specified_type type
;
2324 /* parse the fully specified type, common to all declarators */
2325 if (!slang_fully_specified_type_construct(&type
))
2327 if (!parse_fully_specified_type(C
, O
, &type
)) {
2328 slang_fully_specified_type_destruct(&type
);
2332 /* parse declarators, pass-in the parsed type */
2334 if (!parse_init_declarator(C
, O
, &type
)) {
2335 slang_fully_specified_type_destruct(&type
);
2339 while (*C
->I
++ == DECLARATOR_NEXT
);
2341 slang_fully_specified_type_destruct(&type
);
2347 * Parse a function definition or declaration.
2348 * \param C parsing context
2349 * \param O output context
2350 * \param definition if non-zero expect a definition, else a declaration
2351 * \param parsed_func_ret returns the parsed function
2352 * \return GL_TRUE if success, GL_FALSE if failure
2355 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
2356 slang_function
** parsed_func_ret
)
2358 slang_function parsed_func
, *found_func
;
2360 /* parse function definition/declaration */
2361 if (!slang_function_construct(&parsed_func
))
2364 if (!parse_function_definition(C
, O
, &parsed_func
)) {
2365 slang_function_destruct(&parsed_func
);
2370 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
2371 slang_function_destruct(&parsed_func
);
2376 /* find a function with a prototype matching the parsed one - only
2377 * the current scope is being searched to allow built-in function
2380 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
2381 if (found_func
== NULL
) {
2382 /* New function, add it to the function list */
2383 O
->funs
->functions
=
2384 (slang_function
*) _slang_realloc(O
->funs
->functions
,
2385 O
->funs
->num_functions
2386 * sizeof(slang_function
),
2387 (O
->funs
->num_functions
+ 1)
2388 * sizeof(slang_function
));
2389 if (O
->funs
->functions
== NULL
) {
2390 /* Make sure that there are no functions marked, as the
2391 * allocation is currently NULL, in order to avoid
2392 * a potental segfault as we clean up later.
2394 O
->funs
->num_functions
= 0;
2396 slang_info_log_memory(C
->L
);
2397 slang_function_destruct(&parsed_func
);
2400 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
2401 O
->funs
->num_functions
++;
2403 /* return the newly parsed function */
2404 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
2407 /* previously defined or declared */
2408 /* TODO: check function return type qualifiers and specifiers */
2410 if (found_func
->body
!= NULL
) {
2411 slang_info_log_error(C
->L
, "%s: function already has a body.",
2412 slang_atom_pool_id(C
->atoms
,
2415 slang_function_destruct(&parsed_func
);
2419 /* destroy the existing function declaration and replace it
2422 slang_function_destruct(found_func
);
2423 *found_func
= parsed_func
;
2426 /* another declaration of the same function prototype - ignore it */
2427 slang_function_destruct(&parsed_func
);
2430 /* return the found function */
2431 *parsed_func_ret
= found_func
;
2438 #define DECLARATION_FUNCTION_PROTOTYPE 1
2439 #define DECLARATION_INIT_DECLARATOR_LIST 2
2442 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2445 case DECLARATION_INIT_DECLARATOR_LIST
:
2446 if (!parse_init_declarator_list(C
, O
))
2449 case DECLARATION_FUNCTION_PROTOTYPE
:
2451 slang_function
*dummy_func
;
2453 if (!parse_function(C
, O
, 0, &dummy_func
))
2464 parse_default_precision(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2466 int precision
, type
;
2468 if (!O
->allow_precision
) {
2469 slang_info_log_error(C
->L
, "syntax error at \"precision\"");
2473 precision
= *C
->I
++;
2474 switch (precision
) {
2476 case PRECISION_MEDIUM
:
2477 case PRECISION_HIGH
:
2481 _mesa_problem(NULL
, "unexpected precision %d at %s:%d\n",
2482 precision
, __FILE__
, __LINE__
);
2488 case TYPE_SPECIFIER_FLOAT
:
2489 case TYPE_SPECIFIER_INT
:
2490 case TYPE_SPECIFIER_SAMPLER1D
:
2491 case TYPE_SPECIFIER_SAMPLER2D
:
2492 case TYPE_SPECIFIER_SAMPLER3D
:
2493 case TYPE_SPECIFIER_SAMPLERCUBE
:
2494 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
2495 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
2496 case TYPE_SPECIFIER_SAMPLER2DRECT
:
2497 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
2498 case TYPE_SPECIFIER_SAMPLER_1D_ARRAY
:
2499 case TYPE_SPECIFIER_SAMPLER_2D_ARRAY
:
2500 case TYPE_SPECIFIER_SAMPLER_1D_ARRAY_SHADOW
:
2501 case TYPE_SPECIFIER_SAMPLER_2D_ARRAY_SHADOW
:
2505 _mesa_problem(NULL
, "unexpected type %d at %s:%d\n",
2506 type
, __FILE__
, __LINE__
);
2510 assert(type
< TYPE_SPECIFIER_COUNT
);
2511 O
->default_precision
[type
] = precision
;
2518 * Initialize the default precision for all types.
2519 * XXX this info isn't used yet.
2522 init_default_precision(slang_output_ctx
*O
, slang_unit_type type
)
2524 GET_CURRENT_CONTEXT(ctx
);
2526 for (i
= 0; i
< TYPE_SPECIFIER_COUNT
; i
++) {
2527 #if FEATURE_es2_glsl
2528 if (ctx
->API
== API_OPENGLES2
)
2529 O
->default_precision
[i
] = PRECISION_LOW
;
2531 O
->default_precision
[i
] = PRECISION_HIGH
;
2534 O
->default_precision
[i
] = PRECISION_HIGH
;
2538 if (type
== SLANG_UNIT_VERTEX_SHADER
|| type
== SLANG_UNIT_GEOMETRY_SHADER
) {
2539 O
->default_precision
[TYPE_SPECIFIER_FLOAT
] = PRECISION_HIGH
;
2540 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_HIGH
;
2543 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_MEDIUM
;
2549 parse_invariant(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2551 if (O
->allow_invariant
) {
2552 slang_atom
*a
= parse_identifier(C
);
2553 /* XXX not doing anything with this var yet */
2554 /*printf("ID: %s\n", (char*) a);*/
2558 slang_info_log_error(C
->L
, "syntax error at \"invariant\"");
2564 /* external declaration or default precision specifier */
2565 #define EXTERNAL_NULL 0
2566 #define EXTERNAL_FUNCTION_DEFINITION 1
2567 #define EXTERNAL_DECLARATION 2
2568 #define DEFAULT_PRECISION 3
2569 #define INVARIANT_STMT 4
2573 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
,
2574 struct gl_shader
*shader
)
2576 GET_CURRENT_CONTEXT(ctx
);
2580 slang_function
*mainFunc
= NULL
;
2582 if (unit
->type
== SLANG_UNIT_FRAGMENT_BUILTIN
||
2583 unit
->type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2584 maxRegs
= ctx
->Const
.FragmentProgram
.MaxTemps
;
2586 else if (unit
->type
== SLANG_UNIT_VERTEX_BUILTIN
||
2587 unit
->type
== SLANG_UNIT_VERTEX_SHADER
) {
2588 maxRegs
= ctx
->Const
.VertexProgram
.MaxTemps
;
2590 assert(unit
->type
== SLANG_UNIT_GEOMETRY_BUILTIN
||
2591 unit
->type
== SLANG_UNIT_GEOMETRY_SHADER
);
2592 maxRegs
= ctx
->Const
.GeometryProgram
.MaxTemps
;
2595 /* setup output context */
2596 o
.funs
= &unit
->funs
;
2597 o
.structs
= &unit
->structs
;
2598 o
.vars
= &unit
->vars
;
2599 o
.program
= shader
? shader
->Program
: NULL
;
2600 o
.pragmas
= shader
? &shader
->Pragmas
: NULL
;
2601 o
.vartable
= _slang_new_var_table(maxRegs
);
2602 _slang_push_var_table(o
.vartable
);
2604 /* allow 'invariant' keyword? */
2605 #if FEATURE_es2_glsl
2607 (ctx
->API
== API_OPENGLES2
|| C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2609 o
.allow_invariant
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2612 /* allow 'centroid' keyword? */
2613 o
.allow_centroid
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2615 /* allow 'lowp/mediump/highp' keywords? */
2616 #if FEATURE_es2_glsl
2618 (ctx
->API
== API_OPENGLES2
|| C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2620 o
.allow_precision
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2622 init_default_precision(&o
, unit
->type
);
2624 /* allow 'float[]' keyword? */
2625 o
.allow_array_types
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2627 /* parse individual functions and declarations */
2628 while (*C
->I
!= EXTERNAL_NULL
) {
2630 case EXTERNAL_FUNCTION_DEFINITION
:
2632 slang_function
*func
;
2633 success
= parse_function(C
, &o
, 1, &func
);
2634 if (success
&& strcmp((char *) func
->header
.a_name
, "main") == 0) {
2640 case EXTERNAL_DECLARATION
:
2641 success
= parse_declaration(C
, &o
);
2643 case DEFAULT_PRECISION
:
2644 success
= parse_default_precision(C
, &o
);
2646 case INVARIANT_STMT
:
2647 success
= parse_invariant(C
, &o
);
2654 /* xxx free codegen */
2655 _slang_pop_var_table(o
.vartable
);
2662 /* assemble (generate code) for main() */
2663 slang_assemble_ctx A
;
2664 memset(&A
, 0, sizeof(slang_assemble_ctx
));
2666 A
.space
.funcs
= o
.funs
;
2667 A
.space
.structs
= o
.structs
;
2668 A
.space
.vars
= o
.vars
;
2669 A
.program
= o
.program
;
2670 A
.pragmas
= &shader
->Pragmas
;
2671 A
.vartable
= o
.vartable
;
2672 A
.EmitContReturn
= ctx
->Shader
.EmitContReturn
;
2674 A
.allow_uniform_initializers
= C
->version
> 110;
2676 /* main() takes no parameters */
2677 if (mainFunc
->param_count
> 0) {
2678 slang_info_log_error(A
.log
, "main() takes no arguments");
2682 _slang_codegen_function(&A
, mainFunc
);
2684 shader
->Main
= GL_TRUE
; /* this shader defines main() */
2686 shader
->UnresolvedRefs
= A
.UnresolvedRefs
;
2689 _slang_pop_var_table(o
.vartable
);
2690 _slang_delete_var_table(o
.vartable
);
2696 compile_binary(const unsigned char * prod
, slang_code_unit
* unit
,
2698 slang_unit_type type
, slang_info_log
* infolog
,
2699 slang_code_unit
* builtin
, slang_code_unit
* downlink
,
2700 struct gl_shader
*shader
)
2706 /* setup parse context */
2709 C
.parsing_builtin
= (builtin
== NULL
);
2710 C
.global_scope
= GL_TRUE
;
2711 C
.atoms
= &unit
->object
->atompool
;
2713 C
.version
= version
;
2715 if (!check_revision(&C
))
2718 if (downlink
!= NULL
) {
2719 unit
->vars
.outer_scope
= &downlink
->vars
;
2720 unit
->funs
.outer_scope
= &downlink
->funs
;
2721 unit
->structs
.outer_scope
= &downlink
->structs
;
2724 /* parse translation unit */
2725 return parse_code_unit(&C
, unit
, shader
);
2729 compile_with_grammar(const char *source
,
2730 slang_code_unit
*unit
,
2731 slang_unit_type type
,
2732 slang_info_log
*infolog
,
2733 slang_code_unit
*builtin
,
2734 struct gl_shader
*shader
,
2735 struct gl_sl_pragmas
*pragmas
,
2736 unsigned int shader_type
,
2737 unsigned int parsing_builtin
)
2739 GET_CURRENT_CONTEXT(ctx
);
2740 struct sl_pp_purify_options options
;
2741 struct sl_pp_context
*context
;
2742 unsigned char *prod
;
2744 unsigned int version
;
2745 unsigned int maxVersion
;
2747 char errmsg
[200] = "";
2749 assert(shader_type
== 1 || shader_type
== 2);
2751 memset(&options
, 0, sizeof(options
));
2753 context
= sl_pp_context_create(source
, &options
);
2755 slang_info_log_error(infolog
, "out of memory");
2759 if (sl_pp_version(context
, &version
)) {
2760 slang_info_log_error(infolog
, "%s", sl_pp_context_error_message(context
));
2761 sl_pp_context_destroy(context
);
2765 if (sl_pp_context_add_extension(context
, "GL_ARB_draw_buffers") ||
2766 sl_pp_context_add_extension(context
, "GL_ARB_texture_rectangle")) {
2767 slang_info_log_error(infolog
, "%s", sl_pp_context_error_message(context
));
2768 sl_pp_context_destroy(context
);
2772 if (type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2773 sl_pp_context_add_extension(context
, "GL_ARB_fragment_coord_conventions");
2777 #if FEATURE_es2_glsl
2778 if (ctx
->API
== API_OPENGLES2
) {
2779 if (sl_pp_context_add_predefined(context
, "GL_ES", "1") ||
2780 sl_pp_context_add_predefined(context
, "GL_FRAGMENT_PRECISION_HIGH", "1")) {
2781 slang_info_log_error(infolog
, "%s", sl_pp_context_error_message(context
));
2782 sl_pp_context_destroy(context
);
2790 #if FEATURE_ARB_shading_language_120
2792 #elif FEATURE_es2_glsl
2798 if (version
> maxVersion
||
2799 (version
!= 100 && version
!= 110 && version
!= 120)) {
2800 slang_info_log_error(infolog
,
2801 "language version %.2f is not supported.",
2803 sl_pp_context_destroy(context
);
2807 /* Finally check the syntax and generate its binary representation. */
2808 result
= sl_cl_compile(context
,
2816 sl_pp_context_destroy(context
);
2821 slang_info_log_error(infolog
, errmsg
);
2822 /* syntax error (possibly in library code) */
2827 s
= (char *) _mesa_find_line_column((const GLubyte
*) source
,
2828 (const GLubyte
*) source
+ pos
,
2830 printf("Error on line %d, col %d: %s\n", line
, col
, s
);
2836 /* Syntax is okay - translate it to internal representation. */
2837 if (!compile_binary(prod
, unit
, version
, type
, infolog
, builtin
,
2838 &builtin
[SLANG_BUILTIN_TOTAL
- 1],
2847 static const unsigned char slang_core_gc
[] = {
2848 #include "library/slang_core_gc.h"
2851 static const unsigned char slang_120_core_gc
[] = {
2852 #include "library/slang_120_core_gc.h"
2855 static const unsigned char slang_120_fragment_gc
[] = {
2856 #include "library/slang_builtin_120_fragment_gc.h"
2859 static const unsigned char slang_common_builtin_gc
[] = {
2860 #include "library/slang_common_builtin_gc.h"
2863 static const unsigned char slang_fragment_builtin_gc
[] = {
2864 #include "library/slang_fragment_builtin_gc.h"
2867 static const unsigned char slang_vertex_builtin_gc
[] = {
2868 #include "library/slang_vertex_builtin_gc.h"
2871 static const unsigned char slang_geometry_builtin_gc
[] = {
2872 #include "library/slang_geometry_builtin_gc.h"
2876 compile_object(const char *source
,
2877 slang_code_object
*object
,
2878 slang_unit_type type
,
2879 slang_info_log
*infolog
,
2880 struct gl_shader
*shader
,
2881 struct gl_sl_pragmas
*pragmas
)
2883 slang_code_unit
*builtins
= NULL
;
2884 GLuint base_version
= 110;
2885 unsigned int shader_type
;
2886 unsigned int parsing_builtin
;
2888 /* set shader type - the syntax is slightly different for different shaders */
2889 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
2895 /* enable language extensions */
2896 parsing_builtin
= 1;
2898 /* if parsing user-specified shader, load built-in library */
2899 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_VERTEX_SHADER
||
2900 type
== SLANG_UNIT_GEOMETRY_SHADER
) {
2901 /* compile core functionality first */
2902 if (!compile_binary(slang_core_gc
,
2903 &object
->builtin
[SLANG_BUILTIN_CORE
],
2905 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2909 #if FEATURE_ARB_shading_language_120
2910 if (!compile_binary(slang_120_core_gc
,
2911 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2913 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2914 NULL
, &object
->builtin
[SLANG_BUILTIN_CORE
], NULL
))
2918 /* compile common functions and variables, link to core */
2919 if (!compile_binary(slang_common_builtin_gc
,
2920 &object
->builtin
[SLANG_BUILTIN_COMMON
],
2921 #if FEATURE_ARB_shading_language_120
2926 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2927 #if FEATURE_ARB_shading_language_120
2928 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2930 &object
->builtin
[SLANG_BUILTIN_CORE
],
2935 /* compile target-specific functions and variables, link to common */
2936 if (type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2937 if (!compile_binary(slang_fragment_builtin_gc
,
2938 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2940 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2941 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2943 #if FEATURE_ARB_shading_language_120
2944 if (!compile_binary(slang_120_fragment_gc
,
2945 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2947 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2948 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2952 else if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2953 if (!compile_binary(slang_vertex_builtin_gc
,
2954 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2956 SLANG_UNIT_VERTEX_BUILTIN
, infolog
, NULL
,
2957 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2960 #if FEATURE_ARB_geometry_shader4
2961 else if (type
== SLANG_UNIT_GEOMETRY_SHADER
) {
2962 if (!compile_binary(slang_geometry_builtin_gc
,
2963 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2965 SLANG_UNIT_GEOMETRY_BUILTIN
, infolog
, NULL
,
2966 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2971 /* disable language extensions */
2972 parsing_builtin
= 0;
2974 builtins
= object
->builtin
;
2977 /* compile the actual shader - pass-in built-in library for external shader */
2978 return compile_with_grammar(source
,
2991 _slang_compile(GLcontext
*ctx
, struct gl_shader
*shader
)
2994 slang_info_log info_log
;
2995 slang_code_object obj
;
2996 slang_unit_type type
;
2999 if (shader
->Type
== GL_VERTEX_SHADER
) {
3000 type
= SLANG_UNIT_VERTEX_SHADER
;
3002 else if (shader
->Type
== GL_FRAGMENT_SHADER
) {
3003 type
= SLANG_UNIT_FRAGMENT_SHADER
;
3005 assert(shader
->Type
== GL_GEOMETRY_SHADER_ARB
);
3006 type
= SLANG_UNIT_GEOMETRY_SHADER
;
3009 if (!shader
->Source
)
3012 ctx
->Shader
.MemPool
= _slang_new_mempool(1024*1024);
3014 shader
->Main
= GL_FALSE
;
3016 /* free the shader's old instructions, etc */
3017 _mesa_reference_program(ctx
, &shader
->Program
, NULL
);
3019 /* allocate new GPU program, parameter lists, etc. */
3020 if (shader
->Type
== GL_VERTEX_SHADER
)
3021 progTarget
= GL_VERTEX_PROGRAM_ARB
;
3022 else if (shader
->Type
== GL_FRAGMENT_SHADER
)
3023 progTarget
= GL_FRAGMENT_PROGRAM_ARB
;
3025 progTarget
= MESA_GEOMETRY_PROGRAM
;
3026 shader
->Program
= ctx
->Driver
.NewProgram(ctx
, progTarget
, 1);
3027 shader
->Program
->Parameters
= _mesa_new_parameter_list();
3028 shader
->Program
->Varying
= _mesa_new_parameter_list();
3029 shader
->Program
->Attributes
= _mesa_new_parameter_list();
3031 slang_info_log_construct(&info_log
);
3032 _slang_code_object_ctr(&obj
);
3034 success
= compile_object(shader
->Source
,
3041 /* free shader's prev info log */
3042 if (shader
->InfoLog
) {
3043 free(shader
->InfoLog
);
3044 shader
->InfoLog
= NULL
;
3047 if (info_log
.text
) {
3048 /* copy info-log string to shader object */
3049 shader
->InfoLog
= _mesa_strdup(info_log
.text
);
3052 if (info_log
.error_flag
) {
3056 slang_info_log_destruct(&info_log
);
3057 _slang_code_object_dtr(&obj
);
3059 _slang_delete_mempool((slang_mempool
*) ctx
->Shader
.MemPool
);
3060 ctx
->Shader
.MemPool
= NULL
;
3062 /* remove any reads of output registers */
3064 printf("Pre-remove output reads:\n");
3065 _mesa_print_program(shader
->Program
);
3067 _mesa_remove_output_reads(shader
->Program
, PROGRAM_OUTPUT
);
3068 if (shader
->Type
== GL_VERTEX_SHADER
) {
3069 /* and remove writes to varying vars in vertex programs */
3070 _mesa_remove_output_reads(shader
->Program
, PROGRAM_VARYING
);
3073 printf("Post-remove output reads:\n");
3074 _mesa_print_program(shader
->Program
);
3077 shader
->CompileStatus
= success
;
3080 if (shader
->Pragmas
.Optimize
&&
3081 (ctx
->Shader
.Flags
& GLSL_NO_OPT
) == 0) {
3082 _mesa_optimize_program(ctx
, shader
->Program
);
3084 if ((ctx
->Shader
.Flags
& GLSL_NOP_VERT
) &&
3085 shader
->Program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
3086 _mesa_nop_vertex_program(ctx
,
3087 (struct gl_vertex_program
*) shader
->Program
);
3089 if ((ctx
->Shader
.Flags
& GLSL_NOP_FRAG
) &&
3090 shader
->Program
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3091 _mesa_nop_fragment_program(ctx
,
3092 (struct gl_fragment_program
*) shader
->Program
);
3096 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
3097 _mesa_write_shader_to_file(shader
);