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 "../../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 32
62 * Check if the given identifier is legal.
65 legal_identifier(slang_atom name
)
67 /* "gl_" is a reserved prefix */
68 if (_mesa_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
+= _mesa_strlen(*id
) + 1;
174 parse_identifier(slang_parse_ctx
* C
)
178 id
= (const char *) C
->I
;
179 C
->I
+= _mesa_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
);
246 if (flt
[strlen(flt
) - 1] == 'f' || flt
[strlen(flt
) - 1] == 'F') {
247 flt
[strlen(flt
) - 1] = '\0';
249 *number
= (float)_mesa_strtod(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((_mesa_strlen(integral
) +
302 _mesa_strlen(fractional
) +
303 _mesa_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
= (float) (_mesa_strtod(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
;
640 #define TYPE_QUALIFIER_NONE 0
641 #define TYPE_QUALIFIER_CONST 1
642 #define TYPE_QUALIFIER_ATTRIBUTE 2
643 #define TYPE_QUALIFIER_VARYING 3
644 #define TYPE_QUALIFIER_UNIFORM 4
645 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
646 #define TYPE_QUALIFIER_FIXEDINPUT 6
649 parse_type_qualifier(slang_parse_ctx
* C
, slang_type_qualifier
* qual
)
651 GLuint qualifier
= *C
->I
++;
653 case TYPE_QUALIFIER_NONE
:
654 *qual
= SLANG_QUAL_NONE
;
656 case TYPE_QUALIFIER_CONST
:
657 *qual
= SLANG_QUAL_CONST
;
659 case TYPE_QUALIFIER_ATTRIBUTE
:
660 *qual
= SLANG_QUAL_ATTRIBUTE
;
662 case TYPE_QUALIFIER_VARYING
:
663 *qual
= SLANG_QUAL_VARYING
;
665 case TYPE_QUALIFIER_UNIFORM
:
666 *qual
= SLANG_QUAL_UNIFORM
;
668 case TYPE_QUALIFIER_FIXEDOUTPUT
:
669 *qual
= SLANG_QUAL_FIXEDOUTPUT
;
671 case TYPE_QUALIFIER_FIXEDINPUT
:
672 *qual
= SLANG_QUAL_FIXEDINPUT
;
681 #define TYPE_SPECIFIER_VOID 0
682 #define TYPE_SPECIFIER_BOOL 1
683 #define TYPE_SPECIFIER_BVEC2 2
684 #define TYPE_SPECIFIER_BVEC3 3
685 #define TYPE_SPECIFIER_BVEC4 4
686 #define TYPE_SPECIFIER_INT 5
687 #define TYPE_SPECIFIER_IVEC2 6
688 #define TYPE_SPECIFIER_IVEC3 7
689 #define TYPE_SPECIFIER_IVEC4 8
690 #define TYPE_SPECIFIER_FLOAT 9
691 #define TYPE_SPECIFIER_VEC2 10
692 #define TYPE_SPECIFIER_VEC3 11
693 #define TYPE_SPECIFIER_VEC4 12
694 #define TYPE_SPECIFIER_MAT2 13
695 #define TYPE_SPECIFIER_MAT3 14
696 #define TYPE_SPECIFIER_MAT4 15
697 #define TYPE_SPECIFIER_SAMPLER1D 16
698 #define TYPE_SPECIFIER_SAMPLER2D 17
699 #define TYPE_SPECIFIER_SAMPLER3D 18
700 #define TYPE_SPECIFIER_SAMPLERCUBE 19
701 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
702 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
703 #define TYPE_SPECIFIER_SAMPLER2DRECT 22
704 #define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
705 #define TYPE_SPECIFIER_STRUCT 24
706 #define TYPE_SPECIFIER_TYPENAME 25
707 #define TYPE_SPECIFIER_MAT23 26
708 #define TYPE_SPECIFIER_MAT32 27
709 #define TYPE_SPECIFIER_MAT24 28
710 #define TYPE_SPECIFIER_MAT42 29
711 #define TYPE_SPECIFIER_MAT34 30
712 #define TYPE_SPECIFIER_MAT43 31
713 #define TYPE_SPECIFIER_COUNT 32
716 parse_type_specifier(slang_parse_ctx
* C
, slang_output_ctx
* O
,
717 slang_type_specifier
* spec
)
720 case TYPE_SPECIFIER_VOID
:
721 spec
->type
= SLANG_SPEC_VOID
;
723 case TYPE_SPECIFIER_BOOL
:
724 spec
->type
= SLANG_SPEC_BOOL
;
726 case TYPE_SPECIFIER_BVEC2
:
727 spec
->type
= SLANG_SPEC_BVEC2
;
729 case TYPE_SPECIFIER_BVEC3
:
730 spec
->type
= SLANG_SPEC_BVEC3
;
732 case TYPE_SPECIFIER_BVEC4
:
733 spec
->type
= SLANG_SPEC_BVEC4
;
735 case TYPE_SPECIFIER_INT
:
736 spec
->type
= SLANG_SPEC_INT
;
738 case TYPE_SPECIFIER_IVEC2
:
739 spec
->type
= SLANG_SPEC_IVEC2
;
741 case TYPE_SPECIFIER_IVEC3
:
742 spec
->type
= SLANG_SPEC_IVEC3
;
744 case TYPE_SPECIFIER_IVEC4
:
745 spec
->type
= SLANG_SPEC_IVEC4
;
747 case TYPE_SPECIFIER_FLOAT
:
748 spec
->type
= SLANG_SPEC_FLOAT
;
750 case TYPE_SPECIFIER_VEC2
:
751 spec
->type
= SLANG_SPEC_VEC2
;
753 case TYPE_SPECIFIER_VEC3
:
754 spec
->type
= SLANG_SPEC_VEC3
;
756 case TYPE_SPECIFIER_VEC4
:
757 spec
->type
= SLANG_SPEC_VEC4
;
759 case TYPE_SPECIFIER_MAT2
:
760 spec
->type
= SLANG_SPEC_MAT2
;
762 case TYPE_SPECIFIER_MAT3
:
763 spec
->type
= SLANG_SPEC_MAT3
;
765 case TYPE_SPECIFIER_MAT4
:
766 spec
->type
= SLANG_SPEC_MAT4
;
768 case TYPE_SPECIFIER_MAT23
:
769 spec
->type
= SLANG_SPEC_MAT23
;
771 case TYPE_SPECIFIER_MAT32
:
772 spec
->type
= SLANG_SPEC_MAT32
;
774 case TYPE_SPECIFIER_MAT24
:
775 spec
->type
= SLANG_SPEC_MAT24
;
777 case TYPE_SPECIFIER_MAT42
:
778 spec
->type
= SLANG_SPEC_MAT42
;
780 case TYPE_SPECIFIER_MAT34
:
781 spec
->type
= SLANG_SPEC_MAT34
;
783 case TYPE_SPECIFIER_MAT43
:
784 spec
->type
= SLANG_SPEC_MAT43
;
786 case TYPE_SPECIFIER_SAMPLER1D
:
787 spec
->type
= SLANG_SPEC_SAMPLER1D
;
789 case TYPE_SPECIFIER_SAMPLER2D
:
790 spec
->type
= SLANG_SPEC_SAMPLER2D
;
792 case TYPE_SPECIFIER_SAMPLER3D
:
793 spec
->type
= SLANG_SPEC_SAMPLER3D
;
795 case TYPE_SPECIFIER_SAMPLERCUBE
:
796 spec
->type
= SLANG_SPEC_SAMPLERCUBE
;
798 case TYPE_SPECIFIER_SAMPLER2DRECT
:
799 spec
->type
= SLANG_SPEC_SAMPLER2DRECT
;
801 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
802 spec
->type
= SLANG_SPEC_SAMPLER1DSHADOW
;
804 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
805 spec
->type
= SLANG_SPEC_SAMPLER2DSHADOW
;
807 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
808 spec
->type
= SLANG_SPEC_SAMPLER2DRECTSHADOW
;
810 case TYPE_SPECIFIER_STRUCT
:
811 spec
->type
= SLANG_SPEC_STRUCT
;
812 if (!parse_struct(C
, O
, &spec
->_struct
))
815 case TYPE_SPECIFIER_TYPENAME
:
816 spec
->type
= SLANG_SPEC_STRUCT
;
821 a_name
= parse_identifier(C
);
825 stru
= slang_struct_scope_find(O
->structs
, a_name
, 1);
827 slang_info_log_error(C
->L
, "undeclared type name '%s'",
828 slang_atom_pool_id(C
->atoms
, a_name
));
832 spec
->_struct
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
833 if (spec
->_struct
== NULL
) {
834 slang_info_log_memory(C
->L
);
837 if (!slang_struct_construct(spec
->_struct
)) {
838 _slang_free(spec
->_struct
);
839 spec
->_struct
= NULL
;
842 if (!slang_struct_copy(spec
->_struct
, stru
))
852 #define TYPE_SPECIFIER_NONARRAY 0
853 #define TYPE_SPECIFIER_ARRAY 1
856 parse_type_array_size(slang_parse_ctx
*C
,
863 case TYPE_SPECIFIER_NONARRAY
:
864 *array_len
= -1; /* -1 = not an array */
866 case TYPE_SPECIFIER_ARRAY
:
867 if (!parse_array_len(C
, O
, &size
))
869 *array_len
= (GLint
) size
;
878 #define PRECISION_DEFAULT 0
879 #define PRECISION_LOW 1
880 #define PRECISION_MEDIUM 2
881 #define PRECISION_HIGH 3
884 parse_type_precision(slang_parse_ctx
*C
,
885 slang_type_precision
*precision
)
887 GLint prec
= *C
->I
++;
889 case PRECISION_DEFAULT
:
890 *precision
= SLANG_PREC_DEFAULT
;
893 *precision
= SLANG_PREC_LOW
;
895 case PRECISION_MEDIUM
:
896 *precision
= SLANG_PREC_MEDIUM
;
899 *precision
= SLANG_PREC_HIGH
;
907 parse_fully_specified_type(slang_parse_ctx
* C
, slang_output_ctx
* O
,
908 slang_fully_specified_type
* type
)
910 if (!parse_type_variant(C
, &type
->variant
))
913 if (!parse_type_centroid(C
, &type
->centroid
))
916 if (!parse_type_qualifier(C
, &type
->qualifier
))
919 if (!parse_type_precision(C
, &type
->precision
))
922 if (!parse_type_specifier(C
, O
, &type
->specifier
))
925 if (!parse_type_array_size(C
, O
, &type
->array_len
))
928 if (!O
->allow_invariant
&& type
->variant
== SLANG_INVARIANT
) {
929 slang_info_log_error(C
->L
,
930 "'invariant' keyword not allowed (perhaps set #version 120)");
934 if (!O
->allow_centroid
&& type
->centroid
== SLANG_CENTROID
) {
935 slang_info_log_error(C
->L
,
936 "'centroid' keyword not allowed (perhaps set #version 120)");
939 else if (type
->centroid
== SLANG_CENTROID
&&
940 type
->qualifier
!= SLANG_QUAL_VARYING
) {
941 slang_info_log_error(C
->L
,
942 "'centroid' keyword only allowed for varying vars");
948 if (type->qualifier != SLANG_QUAL_VARYING &&
949 type->variant == SLANG_INVARIANT) {
950 slang_info_log_error(C->L,
951 "invariant qualifer only allowed for varying vars");
956 if (O
->allow_precision
) {
957 if (type
->precision
== SLANG_PREC_DEFAULT
) {
958 assert(type
->specifier
.type
< TYPE_SPECIFIER_COUNT
);
959 /* use the default precision for this datatype */
960 type
->precision
= O
->default_precision
[type
->specifier
.type
];
964 /* only default is allowed */
965 if (type
->precision
!= SLANG_PREC_DEFAULT
) {
966 slang_info_log_error(C
->L
, "precision qualifiers not allowed");
971 if (!O
->allow_array_types
&& type
->array_len
>= 0) {
972 slang_info_log_error(C
->L
, "first-class array types not allowed");
976 if (type
->array_len
>= 0) {
977 /* convert type to array type (ex: convert "int" to "array of int" */
978 promote_type_to_array(C
, type
, type
->array_len
);
986 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
987 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
991 #define OP_CONTINUE 6
994 #define OP_EXPRESSION 9
999 #define OP_PUSH_VOID 14
1000 #define OP_PUSH_BOOL 15
1001 #define OP_PUSH_INT 16
1002 #define OP_PUSH_FLOAT 17
1003 #define OP_PUSH_IDENTIFIER 18
1004 #define OP_SEQUENCE 19
1005 #define OP_ASSIGN 20
1006 #define OP_ADDASSIGN 21
1007 #define OP_SUBASSIGN 22
1008 #define OP_MULASSIGN 23
1009 #define OP_DIVASSIGN 24
1010 /*#define OP_MODASSIGN 25*/
1011 /*#define OP_LSHASSIGN 26*/
1012 /*#define OP_RSHASSIGN 27*/
1013 /*#define OP_ORASSIGN 28*/
1014 /*#define OP_XORASSIGN 29*/
1015 /*#define OP_ANDASSIGN 30*/
1016 #define OP_SELECT 31
1017 #define OP_LOGICALOR 32
1018 #define OP_LOGICALXOR 33
1019 #define OP_LOGICALAND 34
1020 /*#define OP_BITOR 35*/
1021 /*#define OP_BITXOR 36*/
1022 /*#define OP_BITAND 37*/
1024 #define OP_NOTEQUAL 39
1026 #define OP_GREATER 41
1027 #define OP_LESSEQUAL 42
1028 #define OP_GREATEREQUAL 43
1029 /*#define OP_LSHIFT 44*/
1030 /*#define OP_RSHIFT 45*/
1032 #define OP_SUBTRACT 47
1033 #define OP_MULTIPLY 48
1034 #define OP_DIVIDE 49
1035 /*#define OP_MODULUS 50*/
1036 #define OP_PREINCREMENT 51
1037 #define OP_PREDECREMENT 52
1040 /*#define OP_COMPLEMENT 55*/
1042 #define OP_SUBSCRIPT 57
1045 #define OP_POSTINCREMENT 60
1046 #define OP_POSTDECREMENT 61
1047 #define OP_PRECISION 62
1048 #define OP_METHOD 63
1052 * When parsing a compound production, this function is used to parse the
1054 * For example, a while-loop compound will have two children, the
1055 * while condition expression and the loop body. So, this function will
1056 * be called twice to parse those two sub-expressions.
1057 * \param C the parsing context
1058 * \param O the output context
1059 * \param oper the operation we're parsing
1060 * \param statement indicates whether parsing a statement, or expression
1061 * \return 1 if success, 0 if error
1064 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1065 slang_operation
* oper
, GLboolean statement
)
1067 slang_operation
*ch
;
1069 /* grow child array */
1070 ch
= slang_operation_grow(&oper
->num_children
, &oper
->children
);
1072 return parse_statement(C
, O
, ch
);
1073 return parse_expression(C
, O
, ch
);
1076 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
1079 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1080 slang_operation
* oper
)
1084 oper
->locals
->outer_scope
= O
->vars
;
1088 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
1089 /* parse child statements, do not create new variable scope */
1090 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1091 while (*C
->I
!= OP_END
)
1092 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1096 case OP_BLOCK_BEGIN_NEW_SCOPE
:
1097 /* parse child statements, create new variable scope */
1099 slang_output_ctx o
= *O
;
1101 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1102 o
.vars
= oper
->locals
;
1103 while (*C
->I
!= OP_END
)
1104 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1110 /* local variable declaration, individual declarators are stored as
1111 * children identifiers
1113 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1115 const unsigned int first_var
= O
->vars
->num_variables
;
1117 /* parse the declaration, note that there can be zero or more
1118 * than one declarators
1120 if (!parse_declaration(C
, O
))
1122 if (first_var
< O
->vars
->num_variables
) {
1123 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
1125 assert(oper
->num_children
== 0);
1126 oper
->num_children
= num_vars
;
1127 oper
->children
= slang_operation_new(num_vars
);
1128 if (oper
->children
== NULL
) {
1129 slang_info_log_memory(C
->L
);
1132 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
1133 slang_operation
*o
= &oper
->children
[i
- first_var
];
1134 slang_variable
*var
= O
->vars
->variables
[i
];
1135 o
->type
= SLANG_OPER_VARIABLE_DECL
;
1136 o
->locals
->outer_scope
= O
->vars
;
1137 o
->a_id
= var
->a_name
;
1140 calculate_var_size(C, O, var);
1143 if (!legal_identifier(o
->a_id
)) {
1144 slang_info_log_error(C
->L
, "illegal variable name '%s'",
1153 /* the __asm statement, parse the mnemonic and all its arguments
1156 oper
->type
= SLANG_OPER_ASM
;
1157 oper
->a_id
= parse_identifier(C
);
1158 if (oper
->a_id
== SLANG_ATOM_NULL
)
1160 while (*C
->I
!= OP_END
) {
1161 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1167 oper
->type
= SLANG_OPER_BREAK
;
1170 oper
->type
= SLANG_OPER_CONTINUE
;
1173 oper
->type
= SLANG_OPER_DISCARD
;
1176 oper
->type
= SLANG_OPER_RETURN
;
1177 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1181 oper
->type
= SLANG_OPER_EXPRESSION
;
1182 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1186 oper
->type
= SLANG_OPER_IF
;
1187 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1189 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1191 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1196 slang_output_ctx o
= *O
;
1198 oper
->type
= SLANG_OPER_WHILE
;
1199 o
.vars
= oper
->locals
;
1200 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1202 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1207 oper
->type
= SLANG_OPER_DO
;
1208 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1210 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1215 slang_output_ctx o
= *O
;
1217 oper
->type
= SLANG_OPER_FOR
;
1218 o
.vars
= oper
->locals
;
1219 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1221 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1223 if (!parse_child_operation(C
, &o
, oper
, GL_FALSE
))
1225 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1231 /* set default precision for a type in this scope */
1232 /* ignored at this time */
1233 int prec_qual
= *C
->I
++;
1234 int datatype
= *C
->I
++;
1240 /*printf("Unexpected operation %d\n", op);*/
1247 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
1248 slang_operation
** ops
, unsigned int *total_ops
,
1253 op
->children
= slang_operation_new(n
);
1254 if (op
->children
== NULL
) {
1255 slang_info_log_memory(C
->L
);
1258 op
->num_children
= n
;
1260 for (i
= 0; i
< n
; i
++) {
1261 slang_operation_destruct(&op
->children
[i
]);
1262 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
1265 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
1268 *ops
= (slang_operation
*)
1269 _slang_realloc(*ops
,
1270 (*total_ops
+ n
) * sizeof(slang_operation
),
1271 *total_ops
* sizeof(slang_operation
));
1273 slang_info_log_memory(C
->L
);
1280 is_constructor_name(const char *name
, slang_atom a_name
,
1281 slang_struct_scope
* structs
)
1283 if (slang_type_specifier_type_from_string(name
) != SLANG_SPEC_VOID
)
1285 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
1288 #define FUNCTION_CALL_NONARRAY 0
1289 #define FUNCTION_CALL_ARRAY 1
1292 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1293 slang_operation
* oper
)
1295 slang_operation
*ops
= NULL
;
1296 unsigned int num_ops
= 0;
1299 while (*C
->I
!= OP_END
) {
1300 slang_operation
*op
;
1301 const unsigned int op_code
= *C
->I
++;
1303 /* allocate default operation, becomes a no-op if not used */
1304 ops
= (slang_operation
*)
1306 num_ops
* sizeof(slang_operation
),
1307 (num_ops
+ 1) * sizeof(slang_operation
));
1309 slang_info_log_memory(C
->L
);
1313 if (!slang_operation_construct(op
)) {
1314 slang_info_log_memory(C
->L
);
1318 op
->locals
->outer_scope
= O
->vars
;
1322 op
->type
= SLANG_OPER_VOID
;
1325 op
->type
= SLANG_OPER_LITERAL_BOOL
;
1326 if (!parse_number(C
, &number
))
1331 op
->literal
[3] = (GLfloat
) number
;
1332 op
->literal_size
= 1;
1335 op
->type
= SLANG_OPER_LITERAL_INT
;
1336 if (!parse_number(C
, &number
))
1341 op
->literal
[3] = (GLfloat
) number
;
1342 op
->literal_size
= 1;
1345 op
->type
= SLANG_OPER_LITERAL_FLOAT
;
1346 if (!parse_float(C
, &op
->literal
[0]))
1350 op
->literal
[3] = op
->literal
[0];
1351 op
->literal_size
= 1;
1353 case OP_PUSH_IDENTIFIER
:
1354 op
->type
= SLANG_OPER_IDENTIFIER
;
1355 op
->a_id
= parse_identifier(C
);
1356 if (op
->a_id
== SLANG_ATOM_NULL
)
1360 op
->type
= SLANG_OPER_SEQUENCE
;
1361 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1365 op
->type
= SLANG_OPER_ASSIGN
;
1366 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1370 op
->type
= SLANG_OPER_ADDASSIGN
;
1371 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1375 op
->type
= SLANG_OPER_SUBASSIGN
;
1376 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1380 op
->type
= SLANG_OPER_MULASSIGN
;
1381 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1385 op
->type
= SLANG_OPER_DIVASSIGN
;
1386 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1389 /*case OP_MODASSIGN: */
1390 /*case OP_LSHASSIGN: */
1391 /*case OP_RSHASSIGN: */
1392 /*case OP_ORASSIGN: */
1393 /*case OP_XORASSIGN: */
1394 /*case OP_ANDASSIGN: */
1396 op
->type
= SLANG_OPER_SELECT
;
1397 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1401 op
->type
= SLANG_OPER_LOGICALOR
;
1402 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1406 op
->type
= SLANG_OPER_LOGICALXOR
;
1407 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1411 op
->type
= SLANG_OPER_LOGICALAND
;
1412 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1416 /*case OP_BITXOR: */
1417 /*case OP_BITAND: */
1419 op
->type
= SLANG_OPER_EQUAL
;
1420 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1424 op
->type
= SLANG_OPER_NOTEQUAL
;
1425 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1429 op
->type
= SLANG_OPER_LESS
;
1430 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1434 op
->type
= SLANG_OPER_GREATER
;
1435 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1439 op
->type
= SLANG_OPER_LESSEQUAL
;
1440 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1443 case OP_GREATEREQUAL
:
1444 op
->type
= SLANG_OPER_GREATEREQUAL
;
1445 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1448 /*case OP_LSHIFT: */
1449 /*case OP_RSHIFT: */
1451 op
->type
= SLANG_OPER_ADD
;
1452 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1456 op
->type
= SLANG_OPER_SUBTRACT
;
1457 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1461 op
->type
= SLANG_OPER_MULTIPLY
;
1462 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1466 op
->type
= SLANG_OPER_DIVIDE
;
1467 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1470 /*case OP_MODULUS: */
1471 case OP_PREINCREMENT
:
1472 op
->type
= SLANG_OPER_PREINCREMENT
;
1473 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1476 case OP_PREDECREMENT
:
1477 op
->type
= SLANG_OPER_PREDECREMENT
;
1478 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1482 op
->type
= SLANG_OPER_PLUS
;
1483 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1487 op
->type
= SLANG_OPER_MINUS
;
1488 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1492 op
->type
= SLANG_OPER_NOT
;
1493 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1496 /*case OP_COMPLEMENT: */
1498 op
->type
= SLANG_OPER_SUBSCRIPT
;
1499 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1503 op
->type
= SLANG_OPER_METHOD
;
1504 op
->a_obj
= parse_identifier(C
);
1505 if (op
->a_obj
== SLANG_ATOM_NULL
)
1508 op
->a_id
= parse_identifier(C
);
1509 if (op
->a_id
== SLANG_ATOM_NULL
)
1512 assert(*C
->I
== OP_END
);
1515 while (*C
->I
!= OP_END
)
1516 if (!parse_child_operation(C
, O
, op
, GL_FALSE
))
1520 /* don't lookup the method (not yet anyway) */
1521 if (!C
->parsing_builtin
1522 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1525 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1526 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1527 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1535 GLboolean array_constructor
= GL_FALSE
;
1536 GLint array_constructor_size
= 0;
1538 op
->type
= SLANG_OPER_CALL
;
1539 op
->a_id
= parse_identifier(C
);
1540 if (op
->a_id
== SLANG_ATOM_NULL
)
1543 case FUNCTION_CALL_NONARRAY
:
1544 /* Nothing to do. */
1546 case FUNCTION_CALL_ARRAY
:
1547 /* Calling an array constructor. For example:
1548 * float[3](1.1, 2.2, 3.3);
1550 if (!O
->allow_array_types
) {
1551 slang_info_log_error(C
->L
,
1552 "array constructors not allowed "
1553 "in this GLSL version");
1557 /* parse the array constructor size */
1558 slang_operation array_size
;
1559 array_constructor
= GL_TRUE
;
1560 slang_operation_construct(&array_size
);
1561 if (!parse_expression(C
, O
, &array_size
)) {
1562 slang_operation_destruct(&array_size
);
1565 if (array_size
.type
!= SLANG_OPER_LITERAL_INT
) {
1566 slang_info_log_error(C
->L
,
1567 "constructor array size is not an integer");
1568 slang_operation_destruct(&array_size
);
1571 array_constructor_size
= (int) array_size
.literal
[0];
1572 op
->array_constructor
= GL_TRUE
;
1573 slang_operation_destruct(&array_size
);
1580 while (*C
->I
!= OP_END
)
1581 if (!parse_child_operation(C
, O
, op
, GL_FALSE
))
1585 if (array_constructor
&&
1586 array_constructor_size
!= op
->num_children
) {
1587 slang_info_log_error(C
->L
, "number of parameters to array"
1588 " constructor does not match array size");
1592 if (!C
->parsing_builtin
1593 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1596 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1597 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1598 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1605 op
->type
= SLANG_OPER_FIELD
;
1606 op
->a_id
= parse_identifier(C
);
1607 if (op
->a_id
== SLANG_ATOM_NULL
)
1609 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1612 case OP_POSTINCREMENT
:
1613 op
->type
= SLANG_OPER_POSTINCREMENT
;
1614 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1617 case OP_POSTDECREMENT
:
1618 op
->type
= SLANG_OPER_POSTDECREMENT
;
1619 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1628 slang_operation_destruct(oper
);
1629 *oper
= *ops
; /* struct copy */
1635 /* parameter qualifier */
1636 #define PARAM_QUALIFIER_IN 0
1637 #define PARAM_QUALIFIER_OUT 1
1638 #define PARAM_QUALIFIER_INOUT 2
1640 /* function parameter array presence */
1641 #define PARAMETER_ARRAY_NOT_PRESENT 0
1642 #define PARAMETER_ARRAY_PRESENT 1
1645 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1646 slang_variable
* param
)
1648 int param_qual
, precision_qual
;
1650 /* parse and validate the parameter's type qualifiers (there can be
1651 * two at most) because not all combinations are valid
1653 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1656 param_qual
= *C
->I
++;
1657 switch (param_qual
) {
1658 case PARAM_QUALIFIER_IN
:
1659 if (param
->type
.qualifier
!= SLANG_QUAL_CONST
1660 && param
->type
.qualifier
!= SLANG_QUAL_NONE
) {
1661 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1665 case PARAM_QUALIFIER_OUT
:
1666 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1667 param
->type
.qualifier
= SLANG_QUAL_OUT
;
1669 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1673 case PARAM_QUALIFIER_INOUT
:
1674 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1675 param
->type
.qualifier
= SLANG_QUAL_INOUT
;
1677 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1685 /* parse precision qualifier (lowp, mediump, highp */
1686 precision_qual
= *C
->I
++;
1687 /* ignored at this time */
1688 (void) precision_qual
;
1690 /* parse parameter's type specifier and name */
1691 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1693 if (!parse_type_array_size(C
, O
, ¶m
->type
.array_len
))
1695 param
->a_name
= parse_identifier(C
);
1696 if (param
->a_name
== SLANG_ATOM_NULL
)
1699 /* first-class array
1701 if (param
->type
.array_len
>= 0) {
1702 slang_type_specifier p
;
1704 slang_type_specifier_ctr(&p
);
1705 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1706 slang_type_specifier_dtr(&p
);
1709 if (!convert_to_array(C
, param
, &p
)) {
1710 slang_type_specifier_dtr(&p
);
1713 slang_type_specifier_dtr(&p
);
1714 param
->array_len
= param
->type
.array_len
;
1717 /* if the parameter is an array, parse its size (the size must be
1718 * explicitly defined
1720 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1721 slang_type_specifier p
;
1723 if (param
->type
.array_len
>= 0) {
1724 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
1727 slang_type_specifier_ctr(&p
);
1728 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1729 slang_type_specifier_dtr(&p
);
1732 if (!convert_to_array(C
, param
, &p
)) {
1733 slang_type_specifier_dtr(&p
);
1736 slang_type_specifier_dtr(&p
);
1737 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1742 /* calculate the parameter size */
1743 if (!calculate_var_size(C
, O
, param
))
1746 /* TODO: allocate the local address here? */
1751 #define FUNCTION_ORDINARY 0
1752 #define FUNCTION_CONSTRUCTOR 1
1753 #define FUNCTION_OPERATOR 2
1755 /* function parameter */
1756 #define PARAMETER_NONE 0
1757 #define PARAMETER_NEXT 1
1760 #define OPERATOR_ADDASSIGN 1
1761 #define OPERATOR_SUBASSIGN 2
1762 #define OPERATOR_MULASSIGN 3
1763 #define OPERATOR_DIVASSIGN 4
1764 /*#define OPERATOR_MODASSIGN 5*/
1765 /*#define OPERATOR_LSHASSIGN 6*/
1766 /*#define OPERATOR_RSHASSIGN 7*/
1767 /*#define OPERATOR_ANDASSIGN 8*/
1768 /*#define OPERATOR_XORASSIGN 9*/
1769 /*#define OPERATOR_ORASSIGN 10*/
1770 #define OPERATOR_LOGICALXOR 11
1771 /*#define OPERATOR_BITOR 12*/
1772 /*#define OPERATOR_BITXOR 13*/
1773 /*#define OPERATOR_BITAND 14*/
1774 #define OPERATOR_LESS 15
1775 #define OPERATOR_GREATER 16
1776 #define OPERATOR_LESSEQUAL 17
1777 #define OPERATOR_GREATEREQUAL 18
1778 /*#define OPERATOR_LSHIFT 19*/
1779 /*#define OPERATOR_RSHIFT 20*/
1780 #define OPERATOR_MULTIPLY 21
1781 #define OPERATOR_DIVIDE 22
1782 /*#define OPERATOR_MODULUS 23*/
1783 #define OPERATOR_INCREMENT 24
1784 #define OPERATOR_DECREMENT 25
1785 #define OPERATOR_PLUS 26
1786 #define OPERATOR_MINUS 27
1787 /*#define OPERATOR_COMPLEMENT 28*/
1788 #define OPERATOR_NOT 29
1792 unsigned int o_code
;
1794 } operator_names
[] = {
1795 {OPERATOR_INCREMENT
, "++"},
1796 {OPERATOR_ADDASSIGN
, "+="},
1797 {OPERATOR_PLUS
, "+"},
1798 {OPERATOR_DECREMENT
, "--"},
1799 {OPERATOR_SUBASSIGN
, "-="},
1800 {OPERATOR_MINUS
, "-"},
1801 {OPERATOR_NOT
, "!"},
1802 {OPERATOR_MULASSIGN
, "*="},
1803 {OPERATOR_MULTIPLY
, "*"},
1804 {OPERATOR_DIVASSIGN
, "/="},
1805 {OPERATOR_DIVIDE
, "/"},
1806 {OPERATOR_LESSEQUAL
, "<="},
1807 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1808 /*{ OPERATOR_LSHIFT, "<<" }, */
1809 {OPERATOR_LESS
, "<"},
1810 {OPERATOR_GREATEREQUAL
, ">="},
1811 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1812 /*{ OPERATOR_RSHIFT, ">>" }, */
1813 {OPERATOR_GREATER
, ">"},
1814 /*{ OPERATOR_MODASSIGN, "%=" }, */
1815 /*{ OPERATOR_MODULUS, "%" }, */
1816 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1817 /*{ OPERATOR_BITAND, "&" }, */
1818 /*{ OPERATOR_ORASSIGN, "|=" }, */
1819 /*{ OPERATOR_BITOR, "|" }, */
1820 /*{ OPERATOR_COMPLEMENT, "~" }, */
1821 /*{ OPERATOR_XORASSIGN, "^=" }, */
1822 {OPERATOR_LOGICALXOR
, "^^"},
1823 /*{ OPERATOR_BITXOR, "^" } */
1827 parse_operator_name(slang_parse_ctx
* C
)
1831 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1832 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1834 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1835 if (atom
== SLANG_ATOM_NULL
) {
1836 slang_info_log_memory(C
->L
);
1848 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1849 slang_function
* func
)
1852 /* parse function type and name */
1853 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1858 case FUNCTION_ORDINARY
:
1859 func
->kind
= SLANG_FUNC_ORDINARY
;
1860 func
->header
.a_name
= parse_identifier(C
);
1861 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1864 case FUNCTION_CONSTRUCTOR
:
1865 func
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1866 if (func
->header
.type
.specifier
.type
== SLANG_SPEC_STRUCT
)
1868 func
->header
.a_name
=
1869 slang_atom_pool_atom(C
->atoms
,
1870 slang_type_specifier_type_to_string
1871 (func
->header
.type
.specifier
.type
));
1872 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1873 slang_info_log_memory(C
->L
);
1877 case FUNCTION_OPERATOR
:
1878 func
->kind
= SLANG_FUNC_OPERATOR
;
1879 func
->header
.a_name
= parse_operator_name(C
);
1880 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1887 if (!legal_identifier(func
->header
.a_name
)) {
1888 slang_info_log_error(C
->L
, "illegal function name '%s'",
1889 (char *) func
->header
.a_name
);
1893 /* parse function parameters */
1894 while (*C
->I
++ == PARAMETER_NEXT
) {
1895 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1897 slang_info_log_memory(C
->L
);
1900 if (!parse_parameter_declaration(C
, O
, p
))
1904 /* if the function returns a value, append a hidden __retVal 'out'
1905 * parameter that corresponds to the return value.
1907 if (_slang_function_has_return_value(func
)) {
1908 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1909 slang_atom a_retVal
= slang_atom_pool_atom(C
->atoms
, "__retVal");
1911 p
->a_name
= a_retVal
;
1912 p
->type
= func
->header
.type
;
1913 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1916 /* function formal parameters and local variables share the same
1917 * scope, so save the information about param count in a seperate
1918 * place also link the scope to the global variable scope so when a
1919 * given identifier is not found here, the search process continues
1920 * in the global space
1922 func
->param_count
= func
->parameters
->num_variables
;
1923 func
->parameters
->outer_scope
= O
->vars
;
1929 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1930 slang_function
* func
)
1932 slang_output_ctx o
= *O
;
1934 if (!parse_function_prototype(C
, O
, func
))
1937 /* create function's body operation */
1938 func
->body
= (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1939 if (func
->body
== NULL
) {
1940 slang_info_log_memory(C
->L
);
1943 if (!slang_operation_construct(func
->body
)) {
1944 _slang_free(func
->body
);
1946 slang_info_log_memory(C
->L
);
1950 /* to parse the body the parse context is modified in order to
1951 * capture parsed variables into function's local variable scope
1953 C
->global_scope
= GL_FALSE
;
1954 o
.vars
= func
->parameters
;
1955 if (!parse_statement(C
, &o
, func
->body
))
1958 C
->global_scope
= GL_TRUE
;
1963 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1965 slang_operation op_id
, op_assign
;
1968 /* construct the left side of assignment */
1969 if (!slang_operation_construct(&op_id
))
1971 op_id
.type
= SLANG_OPER_IDENTIFIER
;
1972 op_id
.a_id
= var
->a_name
;
1974 /* put the variable into operation's scope */
1975 op_id
.locals
->variables
=
1976 (slang_variable
**) _slang_alloc(sizeof(slang_variable
*));
1977 if (op_id
.locals
->variables
== NULL
) {
1978 slang_operation_destruct(&op_id
);
1981 op_id
.locals
->num_variables
= 1;
1982 op_id
.locals
->variables
[0] = var
;
1984 /* construct the assignment expression */
1985 if (!slang_operation_construct(&op_assign
)) {
1986 op_id
.locals
->num_variables
= 0;
1987 slang_operation_destruct(&op_id
);
1990 op_assign
.type
= SLANG_OPER_ASSIGN
;
1991 op_assign
.children
=
1992 (slang_operation
*) _slang_alloc(2 * sizeof(slang_operation
));
1993 if (op_assign
.children
== NULL
) {
1994 slang_operation_destruct(&op_assign
);
1995 op_id
.locals
->num_variables
= 0;
1996 slang_operation_destruct(&op_id
);
1999 op_assign
.num_children
= 2;
2000 op_assign
.children
[0] = op_id
;
2001 op_assign
.children
[1] = *var
->initializer
;
2005 /* carefully destroy the operations */
2006 op_assign
.num_children
= 0;
2007 _slang_free(op_assign
.children
);
2008 op_assign
.children
= NULL
;
2009 slang_operation_destruct(&op_assign
);
2010 op_id
.locals
->num_variables
= 0;
2011 slang_operation_destruct(&op_id
);
2019 /* init declarator list */
2020 #define DECLARATOR_NONE 0
2021 #define DECLARATOR_NEXT 1
2023 /* variable declaration */
2024 #define VARIABLE_NONE 0
2025 #define VARIABLE_IDENTIFIER 1
2026 #define VARIABLE_INITIALIZER 2
2027 #define VARIABLE_ARRAY_EXPLICIT 3
2028 #define VARIABLE_ARRAY_UNKNOWN 4
2032 * Parse the initializer for a variable declaration.
2035 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
2036 const slang_fully_specified_type
* type
)
2038 GET_CURRENT_CONTEXT(ctx
); /* a hack */
2039 slang_variable
*var
;
2042 /* empty init declatator (without name, e.g. "float ;") */
2043 if (*C
->I
++ == VARIABLE_NONE
)
2046 a_name
= parse_identifier(C
);
2048 /* check if name is already in this scope */
2049 if (_slang_variable_locate(O
->vars
, a_name
, GL_FALSE
)) {
2050 slang_info_log_error(C
->L
,
2051 "declaration of '%s' conflicts with previous declaration",
2056 /* make room for the new variable and initialize it */
2057 var
= slang_variable_scope_grow(O
->vars
);
2059 slang_info_log_memory(C
->L
);
2063 /* copy the declarator type qualifier/etc info, parse the identifier */
2064 var
->type
.qualifier
= type
->qualifier
;
2065 var
->type
.centroid
= type
->centroid
;
2066 var
->type
.precision
= type
->precision
;
2067 var
->type
.variant
= type
->variant
;
2068 var
->type
.array_len
= type
->array_len
;
2069 var
->a_name
= a_name
;
2070 if (var
->a_name
== SLANG_ATOM_NULL
)
2075 /* simple variable declarator - just copy the specifier */
2076 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
2079 case VARIABLE_INITIALIZER
:
2080 /* initialized variable - copy the specifier and parse the expression */
2081 if (0 && type
->array_len
>= 0) {
2082 /* The type was something like "float[4]" */
2083 convert_to_array(C
, var
, &type
->specifier
);
2084 var
->array_len
= type
->array_len
;
2087 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
2091 (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
2092 if (var
->initializer
== NULL
) {
2093 slang_info_log_memory(C
->L
);
2096 if (!slang_operation_construct(var
->initializer
)) {
2097 _slang_free(var
->initializer
);
2098 var
->initializer
= NULL
;
2099 slang_info_log_memory(C
->L
);
2102 if (!parse_expression(C
, O
, var
->initializer
))
2105 case VARIABLE_ARRAY_UNKNOWN
:
2106 /* unsized array - mark it as array and copy the specifier to
2109 if (type
->array_len
>= 0) {
2110 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
2113 if (!convert_to_array(C
, var
, &type
->specifier
))
2116 case VARIABLE_ARRAY_EXPLICIT
:
2117 if (type
->array_len
>= 0) {
2118 /* the user is trying to do something like: float[2] x[3]; */
2119 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
2122 if (!convert_to_array(C
, var
, &type
->specifier
))
2124 if (!parse_array_len(C
, O
, &var
->array_len
))
2131 /* allocate global address space for a variable with a known size */
2133 && !(var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
2134 && var
->array_len
== 0)) {
2135 if (!calculate_var_size(C
, O
, var
))
2139 /* emit code for global var decl */
2140 if (C
->global_scope
) {
2141 slang_assemble_ctx A
;
2142 memset(&A
, 0, sizeof(slang_assemble_ctx
));
2143 A
.allow_uniform_initializers
= C
->version
> 110;
2145 A
.space
.funcs
= O
->funs
;
2146 A
.space
.structs
= O
->structs
;
2147 A
.space
.vars
= O
->vars
;
2148 A
.program
= O
->program
;
2149 A
.pragmas
= O
->pragmas
;
2150 A
.vartable
= O
->vartable
;
2152 A
.curFuncEndLabel
= NULL
;
2153 A
.EmitContReturn
= ctx
->Shader
.EmitContReturn
;
2154 if (!_slang_codegen_global_variable(&A
, var
, C
->type
))
2158 /* initialize global variable */
2159 if (C
->global_scope
) {
2160 if (var
->initializer
!= NULL
) {
2161 slang_assemble_ctx A
;
2162 memset(&A
, 0, sizeof(slang_assemble_ctx
));
2163 A
.allow_uniform_initializers
= C
->version
> 110;
2165 A
.space
.funcs
= O
->funs
;
2166 A
.space
.structs
= O
->structs
;
2167 A
.space
.vars
= O
->vars
;
2168 if (!initialize_global(&A
, var
))
2176 * Parse a list of variable declarations. Each variable may have an
2180 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2182 slang_fully_specified_type type
;
2184 /* parse the fully specified type, common to all declarators */
2185 if (!slang_fully_specified_type_construct(&type
))
2187 if (!parse_fully_specified_type(C
, O
, &type
)) {
2188 slang_fully_specified_type_destruct(&type
);
2192 /* parse declarators, pass-in the parsed type */
2194 if (!parse_init_declarator(C
, O
, &type
)) {
2195 slang_fully_specified_type_destruct(&type
);
2199 while (*C
->I
++ == DECLARATOR_NEXT
);
2201 slang_fully_specified_type_destruct(&type
);
2207 * Parse a function definition or declaration.
2208 * \param C parsing context
2209 * \param O output context
2210 * \param definition if non-zero expect a definition, else a declaration
2211 * \param parsed_func_ret returns the parsed function
2212 * \return GL_TRUE if success, GL_FALSE if failure
2215 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
2216 slang_function
** parsed_func_ret
)
2218 slang_function parsed_func
, *found_func
;
2220 /* parse function definition/declaration */
2221 if (!slang_function_construct(&parsed_func
))
2224 if (!parse_function_definition(C
, O
, &parsed_func
)) {
2225 slang_function_destruct(&parsed_func
);
2230 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
2231 slang_function_destruct(&parsed_func
);
2236 /* find a function with a prototype matching the parsed one - only
2237 * the current scope is being searched to allow built-in function
2240 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
2241 if (found_func
== NULL
) {
2242 /* New function, add it to the function list */
2243 O
->funs
->functions
=
2244 (slang_function
*) _slang_realloc(O
->funs
->functions
,
2245 O
->funs
->num_functions
2246 * sizeof(slang_function
),
2247 (O
->funs
->num_functions
+ 1)
2248 * sizeof(slang_function
));
2249 if (O
->funs
->functions
== NULL
) {
2250 /* Make sure that there are no functions marked, as the
2251 * allocation is currently NULL, in order to avoid
2252 * a potental segfault as we clean up later.
2254 O
->funs
->num_functions
= 0;
2256 slang_info_log_memory(C
->L
);
2257 slang_function_destruct(&parsed_func
);
2260 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
2261 O
->funs
->num_functions
++;
2263 /* return the newly parsed function */
2264 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
2267 /* previously defined or declared */
2268 /* TODO: check function return type qualifiers and specifiers */
2270 if (found_func
->body
!= NULL
) {
2271 slang_info_log_error(C
->L
, "%s: function already has a body.",
2272 slang_atom_pool_id(C
->atoms
,
2275 slang_function_destruct(&parsed_func
);
2279 /* destroy the existing function declaration and replace it
2282 slang_function_destruct(found_func
);
2283 *found_func
= parsed_func
;
2286 /* another declaration of the same function prototype - ignore it */
2287 slang_function_destruct(&parsed_func
);
2290 /* return the found function */
2291 *parsed_func_ret
= found_func
;
2298 #define DECLARATION_FUNCTION_PROTOTYPE 1
2299 #define DECLARATION_INIT_DECLARATOR_LIST 2
2302 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2305 case DECLARATION_INIT_DECLARATOR_LIST
:
2306 if (!parse_init_declarator_list(C
, O
))
2309 case DECLARATION_FUNCTION_PROTOTYPE
:
2311 slang_function
*dummy_func
;
2313 if (!parse_function(C
, O
, 0, &dummy_func
))
2324 parse_default_precision(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2326 int precision
, type
;
2328 if (!O
->allow_precision
) {
2329 slang_info_log_error(C
->L
, "syntax error at \"precision\"");
2333 precision
= *C
->I
++;
2334 switch (precision
) {
2336 case PRECISION_MEDIUM
:
2337 case PRECISION_HIGH
:
2341 _mesa_problem(NULL
, "unexpected precision %d at %s:%d\n",
2342 precision
, __FILE__
, __LINE__
);
2348 case TYPE_SPECIFIER_FLOAT
:
2349 case TYPE_SPECIFIER_INT
:
2350 case TYPE_SPECIFIER_SAMPLER1D
:
2351 case TYPE_SPECIFIER_SAMPLER2D
:
2352 case TYPE_SPECIFIER_SAMPLER3D
:
2353 case TYPE_SPECIFIER_SAMPLERCUBE
:
2354 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
2355 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
2356 case TYPE_SPECIFIER_SAMPLER2DRECT
:
2357 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
2361 _mesa_problem(NULL
, "unexpected type %d at %s:%d\n",
2362 type
, __FILE__
, __LINE__
);
2366 assert(type
< TYPE_SPECIFIER_COUNT
);
2367 O
->default_precision
[type
] = precision
;
2374 * Initialize the default precision for all types.
2375 * XXX this info isn't used yet.
2378 init_default_precision(slang_output_ctx
*O
, slang_unit_type type
)
2381 for (i
= 0; i
< TYPE_SPECIFIER_COUNT
; i
++) {
2382 #if FEATURE_es2_glsl
2383 O
->default_precision
[i
] = PRECISION_LOW
;
2385 O
->default_precision
[i
] = PRECISION_HIGH
;
2389 if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2390 O
->default_precision
[TYPE_SPECIFIER_FLOAT
] = PRECISION_HIGH
;
2391 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_HIGH
;
2394 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_MEDIUM
;
2400 parse_invariant(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2402 if (O
->allow_invariant
) {
2403 slang_atom
*a
= parse_identifier(C
);
2404 /* XXX not doing anything with this var yet */
2405 /*printf("ID: %s\n", (char*) a);*/
2409 slang_info_log_error(C
->L
, "syntax error at \"invariant\"");
2415 /* external declaration or default precision specifier */
2416 #define EXTERNAL_NULL 0
2417 #define EXTERNAL_FUNCTION_DEFINITION 1
2418 #define EXTERNAL_DECLARATION 2
2419 #define DEFAULT_PRECISION 3
2420 #define INVARIANT_STMT 4
2424 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
,
2425 struct gl_shader
*shader
)
2427 GET_CURRENT_CONTEXT(ctx
);
2431 slang_function
*mainFunc
= NULL
;
2433 if (unit
->type
== SLANG_UNIT_FRAGMENT_BUILTIN
||
2434 unit
->type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2435 maxRegs
= ctx
->Const
.FragmentProgram
.MaxTemps
;
2438 assert(unit
->type
== SLANG_UNIT_VERTEX_BUILTIN
||
2439 unit
->type
== SLANG_UNIT_VERTEX_SHADER
);
2440 maxRegs
= ctx
->Const
.VertexProgram
.MaxTemps
;
2443 /* setup output context */
2444 o
.funs
= &unit
->funs
;
2445 o
.structs
= &unit
->structs
;
2446 o
.vars
= &unit
->vars
;
2447 o
.program
= shader
? shader
->Program
: NULL
;
2448 o
.pragmas
= shader
? &shader
->Pragmas
: NULL
;
2449 o
.vartable
= _slang_new_var_table(maxRegs
);
2450 _slang_push_var_table(o
.vartable
);
2452 /* allow 'invariant' keyword? */
2453 #if FEATURE_es2_glsl
2454 o
.allow_invariant
= GL_TRUE
;
2456 o
.allow_invariant
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2459 /* allow 'centroid' keyword? */
2460 o
.allow_centroid
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2462 /* allow 'lowp/mediump/highp' keywords? */
2463 #if FEATURE_es2_glsl
2464 o
.allow_precision
= GL_TRUE
;
2466 o
.allow_precision
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2468 init_default_precision(&o
, unit
->type
);
2470 /* allow 'float[]' keyword? */
2471 o
.allow_array_types
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2473 /* parse individual functions and declarations */
2474 while (*C
->I
!= EXTERNAL_NULL
) {
2476 case EXTERNAL_FUNCTION_DEFINITION
:
2478 slang_function
*func
;
2479 success
= parse_function(C
, &o
, 1, &func
);
2481 _mesa_strcmp((char *) func
->header
.a_name
, "main") == 0) {
2487 case EXTERNAL_DECLARATION
:
2488 success
= parse_declaration(C
, &o
);
2490 case DEFAULT_PRECISION
:
2491 success
= parse_default_precision(C
, &o
);
2493 case INVARIANT_STMT
:
2494 success
= parse_invariant(C
, &o
);
2501 /* xxx free codegen */
2502 _slang_pop_var_table(o
.vartable
);
2509 /* assemble (generate code) for main() */
2510 slang_assemble_ctx A
;
2511 memset(&A
, 0, sizeof(slang_assemble_ctx
));
2513 A
.space
.funcs
= o
.funs
;
2514 A
.space
.structs
= o
.structs
;
2515 A
.space
.vars
= o
.vars
;
2516 A
.program
= o
.program
;
2517 A
.pragmas
= &shader
->Pragmas
;
2518 A
.vartable
= o
.vartable
;
2519 A
.EmitContReturn
= ctx
->Shader
.EmitContReturn
;
2521 A
.allow_uniform_initializers
= C
->version
> 110;
2523 /* main() takes no parameters */
2524 if (mainFunc
->param_count
> 0) {
2525 slang_info_log_error(A
.log
, "main() takes no arguments");
2529 _slang_codegen_function(&A
, mainFunc
);
2531 shader
->Main
= GL_TRUE
; /* this shader defines main() */
2533 shader
->UnresolvedRefs
= A
.UnresolvedRefs
;
2536 _slang_pop_var_table(o
.vartable
);
2537 _slang_delete_var_table(o
.vartable
);
2543 compile_binary(const unsigned char * prod
, slang_code_unit
* unit
,
2545 slang_unit_type type
, slang_info_log
* infolog
,
2546 slang_code_unit
* builtin
, slang_code_unit
* downlink
,
2547 struct gl_shader
*shader
)
2553 /* setup parse context */
2556 C
.parsing_builtin
= (builtin
== NULL
);
2557 C
.global_scope
= GL_TRUE
;
2558 C
.atoms
= &unit
->object
->atompool
;
2560 C
.version
= version
;
2562 if (!check_revision(&C
))
2565 if (downlink
!= NULL
) {
2566 unit
->vars
.outer_scope
= &downlink
->vars
;
2567 unit
->funs
.outer_scope
= &downlink
->funs
;
2568 unit
->structs
.outer_scope
= &downlink
->structs
;
2571 /* parse translation unit */
2572 return parse_code_unit(&C
, unit
, shader
);
2576 compile_with_grammar(const char *source
,
2577 slang_code_unit
*unit
,
2578 slang_unit_type type
,
2579 slang_info_log
*infolog
,
2580 slang_code_unit
*builtin
,
2581 struct gl_shader
*shader
,
2582 struct gl_sl_pragmas
*pragmas
,
2583 unsigned int shader_type
,
2584 unsigned int parsing_builtin
)
2586 struct sl_pp_purify_options options
;
2587 struct sl_pp_context
*context
;
2588 unsigned char *prod
;
2590 unsigned int version
;
2591 unsigned int maxVersion
;
2593 char errmsg
[200] = "";
2595 assert(shader_type
== 1 || shader_type
== 2);
2597 memset(&options
, 0, sizeof(options
));
2599 context
= sl_pp_context_create(source
, &options
);
2601 slang_info_log_error(infolog
, "out of memory");
2605 if (sl_pp_version(context
, &version
)) {
2606 slang_info_log_error(infolog
, "%s", sl_pp_context_error_message(context
));
2607 sl_pp_context_destroy(context
);
2611 if (sl_pp_context_add_extension(context
, "ARB_draw_buffers", "GL_ARB_draw_buffers") ||
2612 sl_pp_context_add_extension(context
, "ARB_texture_rectangle", "GL_ARB_texture_rectangle")) {
2613 slang_info_log_error(infolog
, "%s", sl_pp_context_error_message(context
));
2614 sl_pp_context_destroy(context
);
2618 #if FEATURE_es2_glsl
2619 if (sl_pp_context_add_predefined(context
, "GL_ES", "1") ||
2620 sl_pp_context_add_predefined(context
, "GL_FRAGMENT_PRECISION_HIGH", "1")) {
2621 slang_info_log_error(infolog
, "%s", sl_pp_context_error_message(context
));
2622 sl_pp_context_destroy(context
);
2627 #if FEATURE_ARB_shading_language_120
2629 #elif FEATURE_es2_glsl
2635 if (version
> maxVersion
||
2636 (version
!= 100 && version
!= 110 && version
!= 120)) {
2637 slang_info_log_error(infolog
,
2638 "language version %.2f is not supported.",
2640 sl_pp_context_destroy(context
);
2644 /* Finally check the syntax and generate its binary representation. */
2645 result
= sl_cl_compile(context
,
2653 sl_pp_context_destroy(context
);
2658 slang_info_log_error(infolog
, errmsg
);
2659 /* syntax error (possibly in library code) */
2664 s
= (char *) _mesa_find_line_column((const GLubyte
*) source
,
2665 (const GLubyte
*) source
+ pos
,
2667 printf("Error on line %d, col %d: %s\n", line
, col
, s
);
2673 /* Syntax is okay - translate it to internal representation. */
2674 if (!compile_binary(prod
, unit
, version
, type
, infolog
, builtin
,
2675 &builtin
[SLANG_BUILTIN_TOTAL
- 1],
2684 static const unsigned char slang_core_gc
[] = {
2685 #include "library/slang_core_gc.h"
2688 static const unsigned char slang_120_core_gc
[] = {
2689 #include "library/slang_120_core_gc.h"
2692 static const unsigned char slang_120_fragment_gc
[] = {
2693 #include "library/slang_builtin_120_fragment_gc.h"
2696 static const unsigned char slang_common_builtin_gc
[] = {
2697 #include "library/slang_common_builtin_gc.h"
2700 static const unsigned char slang_fragment_builtin_gc
[] = {
2701 #include "library/slang_fragment_builtin_gc.h"
2704 static const unsigned char slang_vertex_builtin_gc
[] = {
2705 #include "library/slang_vertex_builtin_gc.h"
2709 compile_object(const char *source
,
2710 slang_code_object
*object
,
2711 slang_unit_type type
,
2712 slang_info_log
*infolog
,
2713 struct gl_shader
*shader
,
2714 struct gl_sl_pragmas
*pragmas
)
2716 slang_code_unit
*builtins
= NULL
;
2717 GLuint base_version
= 110;
2718 unsigned int shader_type
;
2719 unsigned int parsing_builtin
;
2721 /* set shader type - the syntax is slightly different for different shaders */
2722 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
2728 /* enable language extensions */
2729 parsing_builtin
= 1;
2731 /* if parsing user-specified shader, load built-in library */
2732 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_VERTEX_SHADER
) {
2733 /* compile core functionality first */
2734 if (!compile_binary(slang_core_gc
,
2735 &object
->builtin
[SLANG_BUILTIN_CORE
],
2737 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2741 #if FEATURE_ARB_shading_language_120
2742 if (!compile_binary(slang_120_core_gc
,
2743 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2745 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2746 NULL
, &object
->builtin
[SLANG_BUILTIN_CORE
], NULL
))
2750 /* compile common functions and variables, link to core */
2751 if (!compile_binary(slang_common_builtin_gc
,
2752 &object
->builtin
[SLANG_BUILTIN_COMMON
],
2753 #if FEATURE_ARB_shading_language_120
2758 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2759 #if FEATURE_ARB_shading_language_120
2760 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2762 &object
->builtin
[SLANG_BUILTIN_CORE
],
2767 /* compile target-specific functions and variables, link to common */
2768 if (type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2769 if (!compile_binary(slang_fragment_builtin_gc
,
2770 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2772 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2773 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2775 #if FEATURE_ARB_shading_language_120
2776 if (!compile_binary(slang_120_fragment_gc
,
2777 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2779 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2780 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2784 else if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2785 if (!compile_binary(slang_vertex_builtin_gc
,
2786 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2788 SLANG_UNIT_VERTEX_BUILTIN
, infolog
, NULL
,
2789 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2793 /* disable language extensions */
2794 parsing_builtin
= 0;
2796 builtins
= object
->builtin
;
2799 /* compile the actual shader - pass-in built-in library for external shader */
2800 return compile_with_grammar(source
,
2813 _slang_compile(GLcontext
*ctx
, struct gl_shader
*shader
)
2816 slang_info_log info_log
;
2817 slang_code_object obj
;
2818 slang_unit_type type
;
2821 if (shader
->Type
== GL_VERTEX_SHADER
) {
2822 type
= SLANG_UNIT_VERTEX_SHADER
;
2825 assert(shader
->Type
== GL_FRAGMENT_SHADER
);
2826 type
= SLANG_UNIT_FRAGMENT_SHADER
;
2829 if (!shader
->Source
)
2832 ctx
->Shader
.MemPool
= _slang_new_mempool(1024*1024);
2834 shader
->Main
= GL_FALSE
;
2836 /* free the shader's old instructions, etc */
2837 _mesa_reference_program(ctx
, &shader
->Program
, NULL
);
2839 /* allocate new GPU program, parameter lists, etc. */
2840 if (shader
->Type
== GL_VERTEX_SHADER
)
2841 progTarget
= GL_VERTEX_PROGRAM_ARB
;
2843 progTarget
= GL_FRAGMENT_PROGRAM_ARB
;
2844 shader
->Program
= ctx
->Driver
.NewProgram(ctx
, progTarget
, 1);
2845 shader
->Program
->Parameters
= _mesa_new_parameter_list();
2846 shader
->Program
->Varying
= _mesa_new_parameter_list();
2847 shader
->Program
->Attributes
= _mesa_new_parameter_list();
2849 slang_info_log_construct(&info_log
);
2850 _slang_code_object_ctr(&obj
);
2852 success
= compile_object(shader
->Source
,
2859 /* free shader's prev info log */
2860 if (shader
->InfoLog
) {
2861 _mesa_free(shader
->InfoLog
);
2862 shader
->InfoLog
= NULL
;
2865 if (info_log
.text
) {
2866 /* copy info-log string to shader object */
2867 shader
->InfoLog
= _mesa_strdup(info_log
.text
);
2870 if (info_log
.error_flag
) {
2874 slang_info_log_destruct(&info_log
);
2875 _slang_code_object_dtr(&obj
);
2877 _slang_delete_mempool((slang_mempool
*) ctx
->Shader
.MemPool
);
2878 ctx
->Shader
.MemPool
= NULL
;
2880 /* remove any reads of output registers */
2882 printf("Pre-remove output reads:\n");
2883 _mesa_print_program(shader
->Program
);
2885 _mesa_remove_output_reads(shader
->Program
, PROGRAM_OUTPUT
);
2886 if (shader
->Type
== GL_VERTEX_SHADER
) {
2887 /* and remove writes to varying vars in vertex programs */
2888 _mesa_remove_output_reads(shader
->Program
, PROGRAM_VARYING
);
2891 printf("Post-remove output reads:\n");
2892 _mesa_print_program(shader
->Program
);
2895 shader
->CompileStatus
= success
;
2898 if (shader
->Pragmas
.Optimize
&&
2899 (ctx
->Shader
.Flags
& GLSL_NO_OPT
) == 0) {
2900 _mesa_optimize_program(ctx
, shader
->Program
);
2902 if ((ctx
->Shader
.Flags
& GLSL_NOP_VERT
) &&
2903 shader
->Program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
2904 _mesa_nop_vertex_program(ctx
,
2905 (struct gl_vertex_program
*) shader
->Program
);
2907 if ((ctx
->Shader
.Flags
& GLSL_NOP_FRAG
) &&
2908 shader
->Program
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
2909 _mesa_nop_fragment_program(ctx
,
2910 (struct gl_fragment_program
*) shader
->Program
);
2914 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
2915 _mesa_write_shader_to_file(shader
);