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_emit.h"
44 #include "slang_log.h"
45 #include "slang_mem.h"
46 #include "slang_vartable.h"
47 #include "slang_simplify.h"
49 #include "slang_print.h"
52 * This is a straightforward implementation of the slang front-end
53 * compiler. Lots of error-checking functionality is missing but
54 * every well-formed shader source should compile successfully and
55 * execute as expected. However, some semantically ill-formed shaders
56 * may be accepted resulting in undefined behaviour.
60 /** re-defined below, should be the same though */
61 #define TYPE_SPECIFIER_COUNT 32
65 * Check if the given identifier is legal.
68 legal_identifier(slang_atom name
)
70 /* "gl_" is a reserved prefix */
71 if (_mesa_strncmp((char *) name
, "gl_", 3) == 0) {
83 _slang_code_unit_ctr(slang_code_unit
* self
,
84 struct slang_code_object_
* object
)
86 _slang_variable_scope_ctr(&self
->vars
);
87 _slang_function_scope_ctr(&self
->funs
);
88 _slang_struct_scope_ctr(&self
->structs
);
89 self
->object
= object
;
93 _slang_code_unit_dtr(slang_code_unit
* self
)
95 slang_variable_scope_destruct(&self
->vars
);
96 slang_function_scope_destruct(&self
->funs
);
97 slang_struct_scope_destruct(&self
->structs
);
105 _slang_code_object_ctr(slang_code_object
* self
)
109 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
110 _slang_code_unit_ctr(&self
->builtin
[i
], self
);
111 _slang_code_unit_ctr(&self
->unit
, self
);
112 slang_atom_pool_construct(&self
->atompool
);
116 _slang_code_object_dtr(slang_code_object
* self
)
120 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
121 _slang_code_unit_dtr(&self
->builtin
[i
]);
122 _slang_code_unit_dtr(&self
->unit
);
123 slang_atom_pool_destruct(&self
->atompool
);
127 /* slang_parse_ctx */
129 typedef struct slang_parse_ctx_
131 const unsigned char *I
;
134 GLboolean global_scope
; /**< Is object being declared a global? */
135 slang_atom_pool
*atoms
;
136 slang_unit_type type
; /**< Vertex vs. Fragment */
137 GLuint version
; /**< user-specified (or default) #version */
140 /* slang_output_ctx */
142 typedef struct slang_output_ctx_
144 slang_variable_scope
*vars
;
145 slang_function_scope
*funs
;
146 slang_struct_scope
*structs
;
147 struct gl_program
*program
;
148 struct gl_sl_pragmas
*pragmas
;
149 slang_var_table
*vartable
;
150 GLuint default_precision
[TYPE_SPECIFIER_COUNT
];
151 GLboolean allow_precision
;
152 GLboolean allow_invariant
;
153 GLboolean allow_centroid
;
154 GLboolean allow_array_types
; /* float[] syntax */
157 /* _slang_compile() */
160 /* Debugging aid, print file/line where parsing error is detected */
164 printf("slang error at %s:%d\n", __FILE__, __LINE__); \
170 parse_identifier_str(slang_parse_ctx
* C
, char **id
)
173 C
->I
+= _mesa_strlen(*id
) + 1;
177 parse_identifier(slang_parse_ctx
* C
)
181 id
= (const char *) C
->I
;
182 C
->I
+= _mesa_strlen(id
) + 1;
183 return slang_atom_pool_atom(C
->atoms
, id
);
189 return (c
>= '0' && c
<= '9') || (c
>= 'a' && c
<= 'f') || (c
>= 'A' && c
<= 'F');
193 parse_general_number(slang_parse_ctx
*ctx
, float *number
)
197 if (*ctx
->I
== '0') {
199 const unsigned char *pi
;
201 if (ctx
->I
[1] == 'x' || ctx
->I
[1] == 'X') {
203 if (!is_hex_digit(*ctx
->I
)) {
209 if (*ctx
->I
>= '0' && *ctx
->I
<= '9') {
210 digit
= (int)(*ctx
->I
- '0');
211 } else if (*ctx
->I
>= 'a' && *ctx
->I
<= 'f') {
212 digit
= (int)(*ctx
->I
- 'a') + 10;
214 digit
= (int)(*ctx
->I
- 'A') + 10;
216 value
= value
* 0x10 + digit
;
218 } while (is_hex_digit(*ctx
->I
));
219 if (*ctx
->I
!= '\0') {
223 *number
= (float)value
;
229 while (*pi
>= '0' && *pi
<= '7') {
232 digit
= (int)(*pi
- '0');
233 value
= value
* 010 + digit
;
239 *number
= (float)value
;
244 parse_identifier_str(ctx
, &flt
);
249 if (flt
[strlen(flt
) - 1] == 'f' || flt
[strlen(flt
) - 1] == 'F') {
250 flt
[strlen(flt
) - 1] = '\0';
252 *number
= (float)_mesa_strtod(flt
, (char **)NULL
);
259 parse_number(slang_parse_ctx
* C
, int *number
)
261 const int radix
= (int) (*C
->I
++);
266 parse_general_number(C
, &f
);
270 while (*C
->I
!= '\0') {
272 if (*C
->I
>= '0' && *C
->I
<= '9')
273 digit
= (int) (*C
->I
- '0');
274 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
275 digit
= (int) (*C
->I
- 'A') + 10;
277 digit
= (int) (*C
->I
- 'a') + 10;
278 *number
= *number
* radix
+ digit
;
284 slang_info_log_warning(C
->L
, "%d: literal integer overflow.", *number
);
289 parse_float(slang_parse_ctx
* C
, float *number
)
293 parse_general_number(C
, number
);
295 char *integral
= NULL
;
296 char *fractional
= NULL
;
297 char *exponent
= NULL
;
300 parse_identifier_str(C
, &integral
);
301 parse_identifier_str(C
, &fractional
);
302 parse_identifier_str(C
, &exponent
);
304 whole
= (char *) _slang_alloc((_mesa_strlen(integral
) +
305 _mesa_strlen(fractional
) +
306 _mesa_strlen(exponent
) + 3) * sizeof(char));
308 slang_info_log_memory(C
->L
);
312 slang_string_copy(whole
, integral
);
313 slang_string_concat(whole
, ".");
314 slang_string_concat(whole
, fractional
);
315 slang_string_concat(whole
, "E");
316 slang_string_concat(whole
, exponent
);
318 *number
= (float) (_mesa_strtod(whole
, (char **) NULL
));
326 /* revision number - increment after each change affecting emitted output */
330 check_revision(slang_parse_ctx
* C
)
332 if (*C
->I
!= REVISION
) {
333 slang_info_log_error(C
->L
, "Internal compiler error.");
340 static int parse_statement(slang_parse_ctx
*, slang_output_ctx
*,
342 static int parse_expression(slang_parse_ctx
*, slang_output_ctx
*,
344 static int parse_type_specifier(slang_parse_ctx
*, slang_output_ctx
*,
345 slang_type_specifier
*);
347 parse_type_array_size(slang_parse_ctx
*C
,
352 parse_array_len(slang_parse_ctx
* C
, slang_output_ctx
* O
, GLuint
* len
)
354 slang_operation array_size
;
355 slang_name_space space
;
358 if (!slang_operation_construct(&array_size
))
360 if (!parse_expression(C
, O
, &array_size
)) {
361 slang_operation_destruct(&array_size
);
365 space
.funcs
= O
->funs
;
366 space
.structs
= O
->structs
;
367 space
.vars
= O
->vars
;
369 /* evaluate compile-time expression which is array size */
370 _slang_simplify(&array_size
, &space
, C
->atoms
);
372 if (array_size
.type
== SLANG_OPER_LITERAL_INT
) {
374 *len
= (GLint
) array_size
.literal
[0];
375 } else if (array_size
.type
== SLANG_OPER_IDENTIFIER
) {
376 slang_variable
*var
= _slang_variable_locate(array_size
.locals
, array_size
.a_id
, GL_TRUE
);
378 slang_info_log_error(C
->L
, "undefined variable '%s'",
379 (char *) array_size
.a_id
);
381 } else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&&
382 var
->type
.specifier
.type
== SLANG_SPEC_INT
) {
383 if (var
->initializer
&&
384 var
->initializer
->type
== SLANG_OPER_LITERAL_INT
) {
385 *len
= (GLint
) var
->initializer
->literal
[0];
388 slang_info_log_error(C
->L
, "unable to parse array size declaration");
392 slang_info_log_error(C
->L
, "unable to parse array size declaration");
399 slang_operation_destruct(&array_size
);
404 calculate_var_size(slang_parse_ctx
* C
, slang_output_ctx
* O
,
405 slang_variable
* var
)
407 slang_storage_aggregate agg
;
409 if (!slang_storage_aggregate_construct(&agg
))
411 if (!_slang_aggregate_variable(&agg
, &var
->type
.specifier
, var
->array_len
,
412 O
->funs
, O
->structs
, O
->vars
, C
->atoms
)) {
413 slang_storage_aggregate_destruct(&agg
);
416 var
->size
= _slang_sizeof_aggregate(&agg
);
417 slang_storage_aggregate_destruct(&agg
);
422 promote_type_to_array(slang_parse_ctx
*C
,
423 slang_fully_specified_type
*type
,
426 slang_type_specifier
*baseType
=
427 slang_type_specifier_new(type
->specifier
.type
, NULL
, NULL
);
429 type
->specifier
.type
= SLANG_SPEC_ARRAY
;
430 type
->specifier
._array
= baseType
;
431 type
->array_len
= array_len
;
436 convert_to_array(slang_parse_ctx
* C
, slang_variable
* var
,
437 const slang_type_specifier
* sp
)
439 /* sized array - mark it as array, copy the specifier to the array element
440 * and parse the expression */
441 var
->type
.specifier
.type
= SLANG_SPEC_ARRAY
;
442 var
->type
.specifier
._array
= (slang_type_specifier
*)
443 _slang_alloc(sizeof(slang_type_specifier
));
444 if (var
->type
.specifier
._array
== NULL
) {
445 slang_info_log_memory(C
->L
);
448 slang_type_specifier_ctr(var
->type
.specifier
._array
);
449 return slang_type_specifier_copy(var
->type
.specifier
._array
, sp
);
452 /* structure field */
455 #define FIELD_ARRAY 2
458 parse_struct_field_var(slang_parse_ctx
* C
, slang_output_ctx
* O
,
459 slang_variable
* var
, slang_atom a_name
,
460 const slang_type_specifier
* sp
,
463 var
->a_name
= a_name
;
464 if (var
->a_name
== SLANG_ATOM_NULL
)
469 if (array_len
!= -1) {
470 if (!convert_to_array(C
, var
, sp
))
472 var
->array_len
= array_len
;
475 if (!slang_type_specifier_copy(&var
->type
.specifier
, sp
))
482 if (!convert_to_array(C
, var
, sp
))
484 if (!parse_array_len(C
, O
, &var
->array_len
))
491 return calculate_var_size(C
, O
, var
);
495 parse_struct_field(slang_parse_ctx
* C
, slang_output_ctx
* O
,
496 slang_struct
* st
, slang_type_specifier
* sp
)
498 slang_output_ctx o
= *O
;
501 o
.structs
= st
->structs
;
502 if (!parse_type_specifier(C
, &o
, sp
))
504 if (!parse_type_array_size(C
, &o
, &array_len
))
509 slang_variable
*var
= slang_variable_scope_grow(st
->fields
);
511 slang_info_log_memory(C
->L
);
514 a_name
= parse_identifier(C
);
515 if (_slang_variable_locate(st
->fields
, a_name
, GL_FALSE
)) {
516 slang_info_log_error(C
->L
, "duplicate field '%s'", (char *) a_name
);
520 if (!parse_struct_field_var(C
, &o
, var
, a_name
, sp
, array_len
))
523 while (*C
->I
++ != FIELD_NONE
);
529 parse_struct(slang_parse_ctx
* C
, slang_output_ctx
* O
, slang_struct
** st
)
534 /* parse struct name (if any) and make sure it is unique in current scope */
535 a_name
= parse_identifier(C
);
536 if (a_name
== SLANG_ATOM_NULL
)
539 name
= slang_atom_pool_id(C
->atoms
, a_name
);
541 && slang_struct_scope_find(O
->structs
, a_name
, 0) != NULL
) {
542 slang_info_log_error(C
->L
, "%s: duplicate type name.", name
);
546 /* set-up a new struct */
547 *st
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
549 slang_info_log_memory(C
->L
);
552 if (!slang_struct_construct(*st
)) {
555 slang_info_log_memory(C
->L
);
558 (**st
).a_name
= a_name
;
559 (**st
).structs
->outer_scope
= O
->structs
;
561 /* parse individual struct fields */
563 slang_type_specifier sp
;
565 slang_type_specifier_ctr(&sp
);
566 if (!parse_struct_field(C
, O
, *st
, &sp
)) {
567 slang_type_specifier_dtr(&sp
);
570 slang_type_specifier_dtr(&sp
);
572 while (*C
->I
++ != FIELD_NONE
);
574 /* if named struct, copy it to current scope */
575 if (name
[0] != '\0') {
578 O
->structs
->structs
=
579 (slang_struct
*) _slang_realloc(O
->structs
->structs
,
580 O
->structs
->num_structs
581 * sizeof(slang_struct
),
582 (O
->structs
->num_structs
+ 1)
583 * sizeof(slang_struct
));
584 if (O
->structs
->structs
== NULL
) {
585 slang_info_log_memory(C
->L
);
588 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
589 if (!slang_struct_construct(s
))
591 O
->structs
->num_structs
++;
592 if (!slang_struct_copy(s
, *st
))
600 /* invariant qualifer */
601 #define TYPE_VARIANT 90
602 #define TYPE_INVARIANT 91
605 parse_type_variant(slang_parse_ctx
* C
, slang_type_variant
*variant
)
607 GLuint invariant
= *C
->I
++;
610 *variant
= SLANG_VARIANT
;
613 *variant
= SLANG_INVARIANT
;
621 /* centroid qualifer */
622 #define TYPE_CENTER 95
623 #define TYPE_CENTROID 96
626 parse_type_centroid(slang_parse_ctx
* C
, slang_type_centroid
*centroid
)
631 *centroid
= SLANG_CENTER
;
634 *centroid
= SLANG_CENTROID
;
643 #define TYPE_QUALIFIER_NONE 0
644 #define TYPE_QUALIFIER_CONST 1
645 #define TYPE_QUALIFIER_ATTRIBUTE 2
646 #define TYPE_QUALIFIER_VARYING 3
647 #define TYPE_QUALIFIER_UNIFORM 4
648 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
649 #define TYPE_QUALIFIER_FIXEDINPUT 6
652 parse_type_qualifier(slang_parse_ctx
* C
, slang_type_qualifier
* qual
)
654 GLuint qualifier
= *C
->I
++;
656 case TYPE_QUALIFIER_NONE
:
657 *qual
= SLANG_QUAL_NONE
;
659 case TYPE_QUALIFIER_CONST
:
660 *qual
= SLANG_QUAL_CONST
;
662 case TYPE_QUALIFIER_ATTRIBUTE
:
663 *qual
= SLANG_QUAL_ATTRIBUTE
;
665 case TYPE_QUALIFIER_VARYING
:
666 *qual
= SLANG_QUAL_VARYING
;
668 case TYPE_QUALIFIER_UNIFORM
:
669 *qual
= SLANG_QUAL_UNIFORM
;
671 case TYPE_QUALIFIER_FIXEDOUTPUT
:
672 *qual
= SLANG_QUAL_FIXEDOUTPUT
;
674 case TYPE_QUALIFIER_FIXEDINPUT
:
675 *qual
= SLANG_QUAL_FIXEDINPUT
;
684 #define TYPE_SPECIFIER_VOID 0
685 #define TYPE_SPECIFIER_BOOL 1
686 #define TYPE_SPECIFIER_BVEC2 2
687 #define TYPE_SPECIFIER_BVEC3 3
688 #define TYPE_SPECIFIER_BVEC4 4
689 #define TYPE_SPECIFIER_INT 5
690 #define TYPE_SPECIFIER_IVEC2 6
691 #define TYPE_SPECIFIER_IVEC3 7
692 #define TYPE_SPECIFIER_IVEC4 8
693 #define TYPE_SPECIFIER_FLOAT 9
694 #define TYPE_SPECIFIER_VEC2 10
695 #define TYPE_SPECIFIER_VEC3 11
696 #define TYPE_SPECIFIER_VEC4 12
697 #define TYPE_SPECIFIER_MAT2 13
698 #define TYPE_SPECIFIER_MAT3 14
699 #define TYPE_SPECIFIER_MAT4 15
700 #define TYPE_SPECIFIER_SAMPLER1D 16
701 #define TYPE_SPECIFIER_SAMPLER2D 17
702 #define TYPE_SPECIFIER_SAMPLER3D 18
703 #define TYPE_SPECIFIER_SAMPLERCUBE 19
704 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
705 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
706 #define TYPE_SPECIFIER_SAMPLER2DRECT 22
707 #define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
708 #define TYPE_SPECIFIER_STRUCT 24
709 #define TYPE_SPECIFIER_TYPENAME 25
710 #define TYPE_SPECIFIER_MAT23 26
711 #define TYPE_SPECIFIER_MAT32 27
712 #define TYPE_SPECIFIER_MAT24 28
713 #define TYPE_SPECIFIER_MAT42 29
714 #define TYPE_SPECIFIER_MAT34 30
715 #define TYPE_SPECIFIER_MAT43 31
716 #define TYPE_SPECIFIER_COUNT 32
719 parse_type_specifier(slang_parse_ctx
* C
, slang_output_ctx
* O
,
720 slang_type_specifier
* spec
)
723 case TYPE_SPECIFIER_VOID
:
724 spec
->type
= SLANG_SPEC_VOID
;
726 case TYPE_SPECIFIER_BOOL
:
727 spec
->type
= SLANG_SPEC_BOOL
;
729 case TYPE_SPECIFIER_BVEC2
:
730 spec
->type
= SLANG_SPEC_BVEC2
;
732 case TYPE_SPECIFIER_BVEC3
:
733 spec
->type
= SLANG_SPEC_BVEC3
;
735 case TYPE_SPECIFIER_BVEC4
:
736 spec
->type
= SLANG_SPEC_BVEC4
;
738 case TYPE_SPECIFIER_INT
:
739 spec
->type
= SLANG_SPEC_INT
;
741 case TYPE_SPECIFIER_IVEC2
:
742 spec
->type
= SLANG_SPEC_IVEC2
;
744 case TYPE_SPECIFIER_IVEC3
:
745 spec
->type
= SLANG_SPEC_IVEC3
;
747 case TYPE_SPECIFIER_IVEC4
:
748 spec
->type
= SLANG_SPEC_IVEC4
;
750 case TYPE_SPECIFIER_FLOAT
:
751 spec
->type
= SLANG_SPEC_FLOAT
;
753 case TYPE_SPECIFIER_VEC2
:
754 spec
->type
= SLANG_SPEC_VEC2
;
756 case TYPE_SPECIFIER_VEC3
:
757 spec
->type
= SLANG_SPEC_VEC3
;
759 case TYPE_SPECIFIER_VEC4
:
760 spec
->type
= SLANG_SPEC_VEC4
;
762 case TYPE_SPECIFIER_MAT2
:
763 spec
->type
= SLANG_SPEC_MAT2
;
765 case TYPE_SPECIFIER_MAT3
:
766 spec
->type
= SLANG_SPEC_MAT3
;
768 case TYPE_SPECIFIER_MAT4
:
769 spec
->type
= SLANG_SPEC_MAT4
;
771 case TYPE_SPECIFIER_MAT23
:
772 spec
->type
= SLANG_SPEC_MAT23
;
774 case TYPE_SPECIFIER_MAT32
:
775 spec
->type
= SLANG_SPEC_MAT32
;
777 case TYPE_SPECIFIER_MAT24
:
778 spec
->type
= SLANG_SPEC_MAT24
;
780 case TYPE_SPECIFIER_MAT42
:
781 spec
->type
= SLANG_SPEC_MAT42
;
783 case TYPE_SPECIFIER_MAT34
:
784 spec
->type
= SLANG_SPEC_MAT34
;
786 case TYPE_SPECIFIER_MAT43
:
787 spec
->type
= SLANG_SPEC_MAT43
;
789 case TYPE_SPECIFIER_SAMPLER1D
:
790 spec
->type
= SLANG_SPEC_SAMPLER1D
;
792 case TYPE_SPECIFIER_SAMPLER2D
:
793 spec
->type
= SLANG_SPEC_SAMPLER2D
;
795 case TYPE_SPECIFIER_SAMPLER3D
:
796 spec
->type
= SLANG_SPEC_SAMPLER3D
;
798 case TYPE_SPECIFIER_SAMPLERCUBE
:
799 spec
->type
= SLANG_SPEC_SAMPLERCUBE
;
801 case TYPE_SPECIFIER_SAMPLER2DRECT
:
802 spec
->type
= SLANG_SPEC_SAMPLER2DRECT
;
804 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
805 spec
->type
= SLANG_SPEC_SAMPLER1DSHADOW
;
807 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
808 spec
->type
= SLANG_SPEC_SAMPLER2DSHADOW
;
810 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
811 spec
->type
= SLANG_SPEC_SAMPLER2DRECTSHADOW
;
813 case TYPE_SPECIFIER_STRUCT
:
814 spec
->type
= SLANG_SPEC_STRUCT
;
815 if (!parse_struct(C
, O
, &spec
->_struct
))
818 case TYPE_SPECIFIER_TYPENAME
:
819 spec
->type
= SLANG_SPEC_STRUCT
;
824 a_name
= parse_identifier(C
);
828 stru
= slang_struct_scope_find(O
->structs
, a_name
, 1);
830 slang_info_log_error(C
->L
, "undeclared type name '%s'",
831 slang_atom_pool_id(C
->atoms
, a_name
));
835 spec
->_struct
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
836 if (spec
->_struct
== NULL
) {
837 slang_info_log_memory(C
->L
);
840 if (!slang_struct_construct(spec
->_struct
)) {
841 _slang_free(spec
->_struct
);
842 spec
->_struct
= NULL
;
845 if (!slang_struct_copy(spec
->_struct
, stru
))
855 #define TYPE_SPECIFIER_NONARRAY 0
856 #define TYPE_SPECIFIER_ARRAY 1
859 parse_type_array_size(slang_parse_ctx
*C
,
866 case TYPE_SPECIFIER_NONARRAY
:
867 *array_len
= -1; /* -1 = not an array */
869 case TYPE_SPECIFIER_ARRAY
:
870 if (!parse_array_len(C
, O
, &size
))
872 *array_len
= (GLint
) size
;
881 #define PRECISION_DEFAULT 0
882 #define PRECISION_LOW 1
883 #define PRECISION_MEDIUM 2
884 #define PRECISION_HIGH 3
887 parse_type_precision(slang_parse_ctx
*C
,
888 slang_type_precision
*precision
)
890 GLint prec
= *C
->I
++;
892 case PRECISION_DEFAULT
:
893 *precision
= SLANG_PREC_DEFAULT
;
896 *precision
= SLANG_PREC_LOW
;
898 case PRECISION_MEDIUM
:
899 *precision
= SLANG_PREC_MEDIUM
;
902 *precision
= SLANG_PREC_HIGH
;
910 parse_fully_specified_type(slang_parse_ctx
* C
, slang_output_ctx
* O
,
911 slang_fully_specified_type
* type
)
913 if (!parse_type_variant(C
, &type
->variant
))
916 if (!parse_type_centroid(C
, &type
->centroid
))
919 if (!parse_type_qualifier(C
, &type
->qualifier
))
922 if (!parse_type_precision(C
, &type
->precision
))
925 if (!parse_type_specifier(C
, O
, &type
->specifier
))
928 if (!parse_type_array_size(C
, O
, &type
->array_len
))
931 if (!O
->allow_invariant
&& type
->variant
== SLANG_INVARIANT
) {
932 slang_info_log_error(C
->L
,
933 "'invariant' keyword not allowed (perhaps set #version 120)");
937 if (!O
->allow_centroid
&& type
->centroid
== SLANG_CENTROID
) {
938 slang_info_log_error(C
->L
,
939 "'centroid' keyword not allowed (perhaps set #version 120)");
942 else if (type
->centroid
== SLANG_CENTROID
&&
943 type
->qualifier
!= SLANG_QUAL_VARYING
) {
944 slang_info_log_error(C
->L
,
945 "'centroid' keyword only allowed for varying vars");
951 if (type->qualifier != SLANG_QUAL_VARYING &&
952 type->variant == SLANG_INVARIANT) {
953 slang_info_log_error(C->L,
954 "invariant qualifer only allowed for varying vars");
959 if (O
->allow_precision
) {
960 if (type
->precision
== SLANG_PREC_DEFAULT
) {
961 assert(type
->specifier
.type
< TYPE_SPECIFIER_COUNT
);
962 /* use the default precision for this datatype */
963 type
->precision
= O
->default_precision
[type
->specifier
.type
];
967 /* only default is allowed */
968 if (type
->precision
!= SLANG_PREC_DEFAULT
) {
969 slang_info_log_error(C
->L
, "precision qualifiers not allowed");
974 if (!O
->allow_array_types
&& type
->array_len
>= 0) {
975 slang_info_log_error(C
->L
, "first-class array types not allowed");
979 if (type
->array_len
>= 0) {
980 /* convert type to array type (ex: convert "int" to "array of int" */
981 promote_type_to_array(C
, type
, type
->array_len
);
989 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
990 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
994 #define OP_CONTINUE 6
997 #define OP_EXPRESSION 9
1002 #define OP_PUSH_VOID 14
1003 #define OP_PUSH_BOOL 15
1004 #define OP_PUSH_INT 16
1005 #define OP_PUSH_FLOAT 17
1006 #define OP_PUSH_IDENTIFIER 18
1007 #define OP_SEQUENCE 19
1008 #define OP_ASSIGN 20
1009 #define OP_ADDASSIGN 21
1010 #define OP_SUBASSIGN 22
1011 #define OP_MULASSIGN 23
1012 #define OP_DIVASSIGN 24
1013 /*#define OP_MODASSIGN 25*/
1014 /*#define OP_LSHASSIGN 26*/
1015 /*#define OP_RSHASSIGN 27*/
1016 /*#define OP_ORASSIGN 28*/
1017 /*#define OP_XORASSIGN 29*/
1018 /*#define OP_ANDASSIGN 30*/
1019 #define OP_SELECT 31
1020 #define OP_LOGICALOR 32
1021 #define OP_LOGICALXOR 33
1022 #define OP_LOGICALAND 34
1023 /*#define OP_BITOR 35*/
1024 /*#define OP_BITXOR 36*/
1025 /*#define OP_BITAND 37*/
1027 #define OP_NOTEQUAL 39
1029 #define OP_GREATER 41
1030 #define OP_LESSEQUAL 42
1031 #define OP_GREATEREQUAL 43
1032 /*#define OP_LSHIFT 44*/
1033 /*#define OP_RSHIFT 45*/
1035 #define OP_SUBTRACT 47
1036 #define OP_MULTIPLY 48
1037 #define OP_DIVIDE 49
1038 /*#define OP_MODULUS 50*/
1039 #define OP_PREINCREMENT 51
1040 #define OP_PREDECREMENT 52
1043 /*#define OP_COMPLEMENT 55*/
1045 #define OP_SUBSCRIPT 57
1048 #define OP_POSTINCREMENT 60
1049 #define OP_POSTDECREMENT 61
1050 #define OP_PRECISION 62
1051 #define OP_METHOD 63
1055 * When parsing a compound production, this function is used to parse the
1057 * For example, a while-loop compound will have two children, the
1058 * while condition expression and the loop body. So, this function will
1059 * be called twice to parse those two sub-expressions.
1060 * \param C the parsing context
1061 * \param O the output context
1062 * \param oper the operation we're parsing
1063 * \param statement indicates whether parsing a statement, or expression
1064 * \return 1 if success, 0 if error
1067 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1068 slang_operation
* oper
, GLboolean statement
)
1070 slang_operation
*ch
;
1072 /* grow child array */
1073 ch
= slang_operation_grow(&oper
->num_children
, &oper
->children
);
1075 return parse_statement(C
, O
, ch
);
1076 return parse_expression(C
, O
, ch
);
1079 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
1082 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1083 slang_operation
* oper
)
1087 oper
->locals
->outer_scope
= O
->vars
;
1091 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
1092 /* parse child statements, do not create new variable scope */
1093 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1094 while (*C
->I
!= OP_END
)
1095 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1099 case OP_BLOCK_BEGIN_NEW_SCOPE
:
1100 /* parse child statements, create new variable scope */
1102 slang_output_ctx o
= *O
;
1104 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1105 o
.vars
= oper
->locals
;
1106 while (*C
->I
!= OP_END
)
1107 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1113 /* local variable declaration, individual declarators are stored as
1114 * children identifiers
1116 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1118 const unsigned int first_var
= O
->vars
->num_variables
;
1120 /* parse the declaration, note that there can be zero or more
1121 * than one declarators
1123 if (!parse_declaration(C
, O
))
1125 if (first_var
< O
->vars
->num_variables
) {
1126 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
1128 assert(oper
->num_children
== 0);
1129 oper
->num_children
= num_vars
;
1130 oper
->children
= slang_operation_new(num_vars
);
1131 if (oper
->children
== NULL
) {
1132 slang_info_log_memory(C
->L
);
1135 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
1136 slang_operation
*o
= &oper
->children
[i
- first_var
];
1137 slang_variable
*var
= O
->vars
->variables
[i
];
1138 o
->type
= SLANG_OPER_VARIABLE_DECL
;
1139 o
->locals
->outer_scope
= O
->vars
;
1140 o
->a_id
= var
->a_name
;
1143 calculate_var_size(C, O, var);
1146 if (!legal_identifier(o
->a_id
)) {
1147 slang_info_log_error(C
->L
, "illegal variable name '%s'",
1156 /* the __asm statement, parse the mnemonic and all its arguments
1159 oper
->type
= SLANG_OPER_ASM
;
1160 oper
->a_id
= parse_identifier(C
);
1161 if (oper
->a_id
== SLANG_ATOM_NULL
)
1163 while (*C
->I
!= OP_END
) {
1164 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1170 oper
->type
= SLANG_OPER_BREAK
;
1173 oper
->type
= SLANG_OPER_CONTINUE
;
1176 oper
->type
= SLANG_OPER_DISCARD
;
1179 oper
->type
= SLANG_OPER_RETURN
;
1180 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1184 oper
->type
= SLANG_OPER_EXPRESSION
;
1185 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1189 oper
->type
= SLANG_OPER_IF
;
1190 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1192 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1194 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1199 slang_output_ctx o
= *O
;
1201 oper
->type
= SLANG_OPER_WHILE
;
1202 o
.vars
= oper
->locals
;
1203 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1205 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1210 oper
->type
= SLANG_OPER_DO
;
1211 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1213 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1218 slang_output_ctx o
= *O
;
1220 oper
->type
= SLANG_OPER_FOR
;
1221 o
.vars
= oper
->locals
;
1222 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1224 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1226 if (!parse_child_operation(C
, &o
, oper
, GL_FALSE
))
1228 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1234 /* set default precision for a type in this scope */
1235 /* ignored at this time */
1236 int prec_qual
= *C
->I
++;
1237 int datatype
= *C
->I
++;
1243 /*printf("Unexpected operation %d\n", op);*/
1250 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
1251 slang_operation
** ops
, unsigned int *total_ops
,
1256 op
->children
= slang_operation_new(n
);
1257 if (op
->children
== NULL
) {
1258 slang_info_log_memory(C
->L
);
1261 op
->num_children
= n
;
1263 for (i
= 0; i
< n
; i
++) {
1264 slang_operation_destruct(&op
->children
[i
]);
1265 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
1268 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
1271 *ops
= (slang_operation
*)
1272 _slang_realloc(*ops
,
1273 (*total_ops
+ n
) * sizeof(slang_operation
),
1274 *total_ops
* sizeof(slang_operation
));
1276 slang_info_log_memory(C
->L
);
1283 is_constructor_name(const char *name
, slang_atom a_name
,
1284 slang_struct_scope
* structs
)
1286 if (slang_type_specifier_type_from_string(name
) != SLANG_SPEC_VOID
)
1288 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
1291 #define FUNCTION_CALL_NONARRAY 0
1292 #define FUNCTION_CALL_ARRAY 1
1295 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1296 slang_operation
* oper
)
1298 slang_operation
*ops
= NULL
;
1299 unsigned int num_ops
= 0;
1302 while (*C
->I
!= OP_END
) {
1303 slang_operation
*op
;
1304 const unsigned int op_code
= *C
->I
++;
1306 /* allocate default operation, becomes a no-op if not used */
1307 ops
= (slang_operation
*)
1309 num_ops
* sizeof(slang_operation
),
1310 (num_ops
+ 1) * sizeof(slang_operation
));
1312 slang_info_log_memory(C
->L
);
1316 if (!slang_operation_construct(op
)) {
1317 slang_info_log_memory(C
->L
);
1321 op
->locals
->outer_scope
= O
->vars
;
1325 op
->type
= SLANG_OPER_VOID
;
1328 op
->type
= SLANG_OPER_LITERAL_BOOL
;
1329 if (!parse_number(C
, &number
))
1334 op
->literal
[3] = (GLfloat
) number
;
1335 op
->literal_size
= 1;
1338 op
->type
= SLANG_OPER_LITERAL_INT
;
1339 if (!parse_number(C
, &number
))
1344 op
->literal
[3] = (GLfloat
) number
;
1345 op
->literal_size
= 1;
1348 op
->type
= SLANG_OPER_LITERAL_FLOAT
;
1349 if (!parse_float(C
, &op
->literal
[0]))
1353 op
->literal
[3] = op
->literal
[0];
1354 op
->literal_size
= 1;
1356 case OP_PUSH_IDENTIFIER
:
1357 op
->type
= SLANG_OPER_IDENTIFIER
;
1358 op
->a_id
= parse_identifier(C
);
1359 if (op
->a_id
== SLANG_ATOM_NULL
)
1363 op
->type
= SLANG_OPER_SEQUENCE
;
1364 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1368 op
->type
= SLANG_OPER_ASSIGN
;
1369 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1373 op
->type
= SLANG_OPER_ADDASSIGN
;
1374 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1378 op
->type
= SLANG_OPER_SUBASSIGN
;
1379 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1383 op
->type
= SLANG_OPER_MULASSIGN
;
1384 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1388 op
->type
= SLANG_OPER_DIVASSIGN
;
1389 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1392 /*case OP_MODASSIGN: */
1393 /*case OP_LSHASSIGN: */
1394 /*case OP_RSHASSIGN: */
1395 /*case OP_ORASSIGN: */
1396 /*case OP_XORASSIGN: */
1397 /*case OP_ANDASSIGN: */
1399 op
->type
= SLANG_OPER_SELECT
;
1400 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1404 op
->type
= SLANG_OPER_LOGICALOR
;
1405 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1409 op
->type
= SLANG_OPER_LOGICALXOR
;
1410 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1414 op
->type
= SLANG_OPER_LOGICALAND
;
1415 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1419 /*case OP_BITXOR: */
1420 /*case OP_BITAND: */
1422 op
->type
= SLANG_OPER_EQUAL
;
1423 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1427 op
->type
= SLANG_OPER_NOTEQUAL
;
1428 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1432 op
->type
= SLANG_OPER_LESS
;
1433 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1437 op
->type
= SLANG_OPER_GREATER
;
1438 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1442 op
->type
= SLANG_OPER_LESSEQUAL
;
1443 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1446 case OP_GREATEREQUAL
:
1447 op
->type
= SLANG_OPER_GREATEREQUAL
;
1448 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1451 /*case OP_LSHIFT: */
1452 /*case OP_RSHIFT: */
1454 op
->type
= SLANG_OPER_ADD
;
1455 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1459 op
->type
= SLANG_OPER_SUBTRACT
;
1460 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1464 op
->type
= SLANG_OPER_MULTIPLY
;
1465 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1469 op
->type
= SLANG_OPER_DIVIDE
;
1470 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1473 /*case OP_MODULUS: */
1474 case OP_PREINCREMENT
:
1475 op
->type
= SLANG_OPER_PREINCREMENT
;
1476 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1479 case OP_PREDECREMENT
:
1480 op
->type
= SLANG_OPER_PREDECREMENT
;
1481 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1485 op
->type
= SLANG_OPER_PLUS
;
1486 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1490 op
->type
= SLANG_OPER_MINUS
;
1491 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1495 op
->type
= SLANG_OPER_NOT
;
1496 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1499 /*case OP_COMPLEMENT: */
1501 op
->type
= SLANG_OPER_SUBSCRIPT
;
1502 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1506 op
->type
= SLANG_OPER_METHOD
;
1507 op
->a_obj
= parse_identifier(C
);
1508 if (op
->a_obj
== SLANG_ATOM_NULL
)
1511 op
->a_id
= parse_identifier(C
);
1512 if (op
->a_id
== SLANG_ATOM_NULL
)
1515 assert(*C
->I
== OP_END
);
1518 while (*C
->I
!= OP_END
)
1519 if (!parse_child_operation(C
, O
, op
, GL_FALSE
))
1523 /* don't lookup the method (not yet anyway) */
1524 if (!C
->parsing_builtin
1525 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1528 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1529 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1530 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1538 GLboolean array_constructor
= GL_FALSE
;
1539 GLint array_constructor_size
= 0;
1541 op
->type
= SLANG_OPER_CALL
;
1542 op
->a_id
= parse_identifier(C
);
1543 if (op
->a_id
== SLANG_ATOM_NULL
)
1546 case FUNCTION_CALL_NONARRAY
:
1547 /* Nothing to do. */
1549 case FUNCTION_CALL_ARRAY
:
1550 /* Calling an array constructor. For example:
1551 * float[3](1.1, 2.2, 3.3);
1553 if (!O
->allow_array_types
) {
1554 slang_info_log_error(C
->L
,
1555 "array constructors not allowed "
1556 "in this GLSL version");
1560 /* parse the array constructor size */
1561 slang_operation array_size
;
1562 array_constructor
= GL_TRUE
;
1563 slang_operation_construct(&array_size
);
1564 if (!parse_expression(C
, O
, &array_size
)) {
1565 slang_operation_destruct(&array_size
);
1568 if (array_size
.type
!= SLANG_OPER_LITERAL_INT
) {
1569 slang_info_log_error(C
->L
,
1570 "constructor array size is not an integer");
1571 slang_operation_destruct(&array_size
);
1574 array_constructor_size
= (int) array_size
.literal
[0];
1575 op
->array_constructor
= GL_TRUE
;
1576 slang_operation_destruct(&array_size
);
1583 while (*C
->I
!= OP_END
)
1584 if (!parse_child_operation(C
, O
, op
, GL_FALSE
))
1588 if (array_constructor
&&
1589 array_constructor_size
!= op
->num_children
) {
1590 slang_info_log_error(C
->L
, "number of parameters to array"
1591 " constructor does not match array size");
1595 if (!C
->parsing_builtin
1596 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1599 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1600 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1601 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1608 op
->type
= SLANG_OPER_FIELD
;
1609 op
->a_id
= parse_identifier(C
);
1610 if (op
->a_id
== SLANG_ATOM_NULL
)
1612 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1615 case OP_POSTINCREMENT
:
1616 op
->type
= SLANG_OPER_POSTINCREMENT
;
1617 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1620 case OP_POSTDECREMENT
:
1621 op
->type
= SLANG_OPER_POSTDECREMENT
;
1622 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1631 slang_operation_destruct(oper
);
1632 *oper
= *ops
; /* struct copy */
1638 /* parameter qualifier */
1639 #define PARAM_QUALIFIER_IN 0
1640 #define PARAM_QUALIFIER_OUT 1
1641 #define PARAM_QUALIFIER_INOUT 2
1643 /* function parameter array presence */
1644 #define PARAMETER_ARRAY_NOT_PRESENT 0
1645 #define PARAMETER_ARRAY_PRESENT 1
1648 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1649 slang_variable
* param
)
1651 int param_qual
, precision_qual
;
1653 /* parse and validate the parameter's type qualifiers (there can be
1654 * two at most) because not all combinations are valid
1656 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1659 param_qual
= *C
->I
++;
1660 switch (param_qual
) {
1661 case PARAM_QUALIFIER_IN
:
1662 if (param
->type
.qualifier
!= SLANG_QUAL_CONST
1663 && param
->type
.qualifier
!= SLANG_QUAL_NONE
) {
1664 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1668 case PARAM_QUALIFIER_OUT
:
1669 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1670 param
->type
.qualifier
= SLANG_QUAL_OUT
;
1672 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1676 case PARAM_QUALIFIER_INOUT
:
1677 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1678 param
->type
.qualifier
= SLANG_QUAL_INOUT
;
1680 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1688 /* parse precision qualifier (lowp, mediump, highp */
1689 precision_qual
= *C
->I
++;
1690 /* ignored at this time */
1691 (void) precision_qual
;
1693 /* parse parameter's type specifier and name */
1694 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1696 if (!parse_type_array_size(C
, O
, ¶m
->type
.array_len
))
1698 param
->a_name
= parse_identifier(C
);
1699 if (param
->a_name
== SLANG_ATOM_NULL
)
1702 /* first-class array
1704 if (param
->type
.array_len
>= 0) {
1705 slang_type_specifier p
;
1707 slang_type_specifier_ctr(&p
);
1708 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1709 slang_type_specifier_dtr(&p
);
1712 if (!convert_to_array(C
, param
, &p
)) {
1713 slang_type_specifier_dtr(&p
);
1716 slang_type_specifier_dtr(&p
);
1717 param
->array_len
= param
->type
.array_len
;
1720 /* if the parameter is an array, parse its size (the size must be
1721 * explicitly defined
1723 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1724 slang_type_specifier p
;
1726 if (param
->type
.array_len
>= 0) {
1727 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
1730 slang_type_specifier_ctr(&p
);
1731 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1732 slang_type_specifier_dtr(&p
);
1735 if (!convert_to_array(C
, param
, &p
)) {
1736 slang_type_specifier_dtr(&p
);
1739 slang_type_specifier_dtr(&p
);
1740 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1745 /* calculate the parameter size */
1746 if (!calculate_var_size(C
, O
, param
))
1749 /* TODO: allocate the local address here? */
1754 #define FUNCTION_ORDINARY 0
1755 #define FUNCTION_CONSTRUCTOR 1
1756 #define FUNCTION_OPERATOR 2
1758 /* function parameter */
1759 #define PARAMETER_NONE 0
1760 #define PARAMETER_NEXT 1
1763 #define OPERATOR_ADDASSIGN 1
1764 #define OPERATOR_SUBASSIGN 2
1765 #define OPERATOR_MULASSIGN 3
1766 #define OPERATOR_DIVASSIGN 4
1767 /*#define OPERATOR_MODASSIGN 5*/
1768 /*#define OPERATOR_LSHASSIGN 6*/
1769 /*#define OPERATOR_RSHASSIGN 7*/
1770 /*#define OPERATOR_ANDASSIGN 8*/
1771 /*#define OPERATOR_XORASSIGN 9*/
1772 /*#define OPERATOR_ORASSIGN 10*/
1773 #define OPERATOR_LOGICALXOR 11
1774 /*#define OPERATOR_BITOR 12*/
1775 /*#define OPERATOR_BITXOR 13*/
1776 /*#define OPERATOR_BITAND 14*/
1777 #define OPERATOR_LESS 15
1778 #define OPERATOR_GREATER 16
1779 #define OPERATOR_LESSEQUAL 17
1780 #define OPERATOR_GREATEREQUAL 18
1781 /*#define OPERATOR_LSHIFT 19*/
1782 /*#define OPERATOR_RSHIFT 20*/
1783 #define OPERATOR_MULTIPLY 21
1784 #define OPERATOR_DIVIDE 22
1785 /*#define OPERATOR_MODULUS 23*/
1786 #define OPERATOR_INCREMENT 24
1787 #define OPERATOR_DECREMENT 25
1788 #define OPERATOR_PLUS 26
1789 #define OPERATOR_MINUS 27
1790 /*#define OPERATOR_COMPLEMENT 28*/
1791 #define OPERATOR_NOT 29
1795 unsigned int o_code
;
1797 } operator_names
[] = {
1798 {OPERATOR_INCREMENT
, "++"},
1799 {OPERATOR_ADDASSIGN
, "+="},
1800 {OPERATOR_PLUS
, "+"},
1801 {OPERATOR_DECREMENT
, "--"},
1802 {OPERATOR_SUBASSIGN
, "-="},
1803 {OPERATOR_MINUS
, "-"},
1804 {OPERATOR_NOT
, "!"},
1805 {OPERATOR_MULASSIGN
, "*="},
1806 {OPERATOR_MULTIPLY
, "*"},
1807 {OPERATOR_DIVASSIGN
, "/="},
1808 {OPERATOR_DIVIDE
, "/"},
1809 {OPERATOR_LESSEQUAL
, "<="},
1810 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1811 /*{ OPERATOR_LSHIFT, "<<" }, */
1812 {OPERATOR_LESS
, "<"},
1813 {OPERATOR_GREATEREQUAL
, ">="},
1814 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1815 /*{ OPERATOR_RSHIFT, ">>" }, */
1816 {OPERATOR_GREATER
, ">"},
1817 /*{ OPERATOR_MODASSIGN, "%=" }, */
1818 /*{ OPERATOR_MODULUS, "%" }, */
1819 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1820 /*{ OPERATOR_BITAND, "&" }, */
1821 /*{ OPERATOR_ORASSIGN, "|=" }, */
1822 /*{ OPERATOR_BITOR, "|" }, */
1823 /*{ OPERATOR_COMPLEMENT, "~" }, */
1824 /*{ OPERATOR_XORASSIGN, "^=" }, */
1825 {OPERATOR_LOGICALXOR
, "^^"},
1826 /*{ OPERATOR_BITXOR, "^" } */
1830 parse_operator_name(slang_parse_ctx
* C
)
1834 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1835 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1837 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1838 if (atom
== SLANG_ATOM_NULL
) {
1839 slang_info_log_memory(C
->L
);
1851 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1852 slang_function
* func
)
1855 /* parse function type and name */
1856 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1861 case FUNCTION_ORDINARY
:
1862 func
->kind
= SLANG_FUNC_ORDINARY
;
1863 func
->header
.a_name
= parse_identifier(C
);
1864 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1867 case FUNCTION_CONSTRUCTOR
:
1868 func
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1869 if (func
->header
.type
.specifier
.type
== SLANG_SPEC_STRUCT
)
1871 func
->header
.a_name
=
1872 slang_atom_pool_atom(C
->atoms
,
1873 slang_type_specifier_type_to_string
1874 (func
->header
.type
.specifier
.type
));
1875 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1876 slang_info_log_memory(C
->L
);
1880 case FUNCTION_OPERATOR
:
1881 func
->kind
= SLANG_FUNC_OPERATOR
;
1882 func
->header
.a_name
= parse_operator_name(C
);
1883 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1890 if (!legal_identifier(func
->header
.a_name
)) {
1891 slang_info_log_error(C
->L
, "illegal function name '%s'",
1892 (char *) func
->header
.a_name
);
1896 /* parse function parameters */
1897 while (*C
->I
++ == PARAMETER_NEXT
) {
1898 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1900 slang_info_log_memory(C
->L
);
1903 if (!parse_parameter_declaration(C
, O
, p
))
1907 /* if the function returns a value, append a hidden __retVal 'out'
1908 * parameter that corresponds to the return value.
1910 if (_slang_function_has_return_value(func
)) {
1911 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1912 slang_atom a_retVal
= slang_atom_pool_atom(C
->atoms
, "__retVal");
1914 p
->a_name
= a_retVal
;
1915 p
->type
= func
->header
.type
;
1916 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1919 /* function formal parameters and local variables share the same
1920 * scope, so save the information about param count in a seperate
1921 * place also link the scope to the global variable scope so when a
1922 * given identifier is not found here, the search process continues
1923 * in the global space
1925 func
->param_count
= func
->parameters
->num_variables
;
1926 func
->parameters
->outer_scope
= O
->vars
;
1932 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1933 slang_function
* func
)
1935 slang_output_ctx o
= *O
;
1937 if (!parse_function_prototype(C
, O
, func
))
1940 /* create function's body operation */
1941 func
->body
= (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1942 if (func
->body
== NULL
) {
1943 slang_info_log_memory(C
->L
);
1946 if (!slang_operation_construct(func
->body
)) {
1947 _slang_free(func
->body
);
1949 slang_info_log_memory(C
->L
);
1953 /* to parse the body the parse context is modified in order to
1954 * capture parsed variables into function's local variable scope
1956 C
->global_scope
= GL_FALSE
;
1957 o
.vars
= func
->parameters
;
1958 if (!parse_statement(C
, &o
, func
->body
))
1961 C
->global_scope
= GL_TRUE
;
1966 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1968 slang_operation op_id
, op_assign
;
1971 /* construct the left side of assignment */
1972 if (!slang_operation_construct(&op_id
))
1974 op_id
.type
= SLANG_OPER_IDENTIFIER
;
1975 op_id
.a_id
= var
->a_name
;
1977 /* put the variable into operation's scope */
1978 op_id
.locals
->variables
=
1979 (slang_variable
**) _slang_alloc(sizeof(slang_variable
*));
1980 if (op_id
.locals
->variables
== NULL
) {
1981 slang_operation_destruct(&op_id
);
1984 op_id
.locals
->num_variables
= 1;
1985 op_id
.locals
->variables
[0] = var
;
1987 /* construct the assignment expression */
1988 if (!slang_operation_construct(&op_assign
)) {
1989 op_id
.locals
->num_variables
= 0;
1990 slang_operation_destruct(&op_id
);
1993 op_assign
.type
= SLANG_OPER_ASSIGN
;
1994 op_assign
.children
=
1995 (slang_operation
*) _slang_alloc(2 * sizeof(slang_operation
));
1996 if (op_assign
.children
== NULL
) {
1997 slang_operation_destruct(&op_assign
);
1998 op_id
.locals
->num_variables
= 0;
1999 slang_operation_destruct(&op_id
);
2002 op_assign
.num_children
= 2;
2003 op_assign
.children
[0] = op_id
;
2004 op_assign
.children
[1] = *var
->initializer
;
2008 /* carefully destroy the operations */
2009 op_assign
.num_children
= 0;
2010 _slang_free(op_assign
.children
);
2011 op_assign
.children
= NULL
;
2012 slang_operation_destruct(&op_assign
);
2013 op_id
.locals
->num_variables
= 0;
2014 slang_operation_destruct(&op_id
);
2022 /* init declarator list */
2023 #define DECLARATOR_NONE 0
2024 #define DECLARATOR_NEXT 1
2026 /* variable declaration */
2027 #define VARIABLE_NONE 0
2028 #define VARIABLE_IDENTIFIER 1
2029 #define VARIABLE_INITIALIZER 2
2030 #define VARIABLE_ARRAY_EXPLICIT 3
2031 #define VARIABLE_ARRAY_UNKNOWN 4
2035 * Parse the initializer for a variable declaration.
2038 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
2039 const slang_fully_specified_type
* type
)
2041 GET_CURRENT_CONTEXT(ctx
); /* a hack */
2042 slang_variable
*var
;
2045 /* empty init declatator (without name, e.g. "float ;") */
2046 if (*C
->I
++ == VARIABLE_NONE
)
2049 a_name
= parse_identifier(C
);
2051 /* check if name is already in this scope */
2052 if (_slang_variable_locate(O
->vars
, a_name
, GL_FALSE
)) {
2053 slang_info_log_error(C
->L
,
2054 "declaration of '%s' conflicts with previous declaration",
2059 /* make room for the new variable and initialize it */
2060 var
= slang_variable_scope_grow(O
->vars
);
2062 slang_info_log_memory(C
->L
);
2066 /* copy the declarator type qualifier/etc info, parse the identifier */
2067 var
->type
.qualifier
= type
->qualifier
;
2068 var
->type
.centroid
= type
->centroid
;
2069 var
->type
.precision
= type
->precision
;
2070 var
->type
.variant
= type
->variant
;
2071 var
->type
.array_len
= type
->array_len
;
2072 var
->a_name
= a_name
;
2073 if (var
->a_name
== SLANG_ATOM_NULL
)
2078 /* simple variable declarator - just copy the specifier */
2079 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
2082 case VARIABLE_INITIALIZER
:
2083 /* initialized variable - copy the specifier and parse the expression */
2084 if (0 && type
->array_len
>= 0) {
2085 /* The type was something like "float[4]" */
2086 convert_to_array(C
, var
, &type
->specifier
);
2087 var
->array_len
= type
->array_len
;
2090 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
2094 (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
2095 if (var
->initializer
== NULL
) {
2096 slang_info_log_memory(C
->L
);
2099 if (!slang_operation_construct(var
->initializer
)) {
2100 _slang_free(var
->initializer
);
2101 var
->initializer
= NULL
;
2102 slang_info_log_memory(C
->L
);
2105 if (!parse_expression(C
, O
, var
->initializer
))
2108 case VARIABLE_ARRAY_UNKNOWN
:
2109 /* unsized array - mark it as array and copy the specifier to
2112 if (type
->array_len
>= 0) {
2113 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
2116 if (!convert_to_array(C
, var
, &type
->specifier
))
2119 case VARIABLE_ARRAY_EXPLICIT
:
2120 if (type
->array_len
>= 0) {
2121 /* the user is trying to do something like: float[2] x[3]; */
2122 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
2125 if (!convert_to_array(C
, var
, &type
->specifier
))
2127 if (!parse_array_len(C
, O
, &var
->array_len
))
2134 /* allocate global address space for a variable with a known size */
2136 && !(var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
2137 && var
->array_len
== 0)) {
2138 if (!calculate_var_size(C
, O
, var
))
2142 /* emit code for global var decl */
2143 if (C
->global_scope
) {
2144 slang_assemble_ctx A
;
2145 memset(&A
, 0, sizeof(slang_assemble_ctx
));
2147 A
.space
.funcs
= O
->funs
;
2148 A
.space
.structs
= O
->structs
;
2149 A
.space
.vars
= O
->vars
;
2150 A
.program
= O
->program
;
2151 A
.pragmas
= O
->pragmas
;
2152 A
.vartable
= O
->vartable
;
2154 A
.curFuncEndLabel
= NULL
;
2155 A
.EmitContReturn
= ctx
->Shader
.EmitContReturn
;
2156 if (!_slang_codegen_global_variable(&A
, var
, C
->type
))
2160 /* initialize global variable */
2161 if (C
->global_scope
) {
2162 if (var
->initializer
!= NULL
) {
2163 slang_assemble_ctx A
;
2164 memset(&A
, 0, sizeof(slang_assemble_ctx
));
2166 A
.space
.funcs
= O
->funs
;
2167 A
.space
.structs
= O
->structs
;
2168 A
.space
.vars
= O
->vars
;
2169 if (!initialize_global(&A
, var
))
2177 * Parse a list of variable declarations. Each variable may have an
2181 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2183 slang_fully_specified_type type
;
2185 /* parse the fully specified type, common to all declarators */
2186 if (!slang_fully_specified_type_construct(&type
))
2188 if (!parse_fully_specified_type(C
, O
, &type
)) {
2189 slang_fully_specified_type_destruct(&type
);
2193 /* parse declarators, pass-in the parsed type */
2195 if (!parse_init_declarator(C
, O
, &type
)) {
2196 slang_fully_specified_type_destruct(&type
);
2200 while (*C
->I
++ == DECLARATOR_NEXT
);
2202 slang_fully_specified_type_destruct(&type
);
2208 * Parse a function definition or declaration.
2209 * \param C parsing context
2210 * \param O output context
2211 * \param definition if non-zero expect a definition, else a declaration
2212 * \param parsed_func_ret returns the parsed function
2213 * \return GL_TRUE if success, GL_FALSE if failure
2216 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
2217 slang_function
** parsed_func_ret
)
2219 slang_function parsed_func
, *found_func
;
2221 /* parse function definition/declaration */
2222 if (!slang_function_construct(&parsed_func
))
2225 if (!parse_function_definition(C
, O
, &parsed_func
)) {
2226 slang_function_destruct(&parsed_func
);
2231 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
2232 slang_function_destruct(&parsed_func
);
2237 /* find a function with a prototype matching the parsed one - only
2238 * the current scope is being searched to allow built-in function
2241 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
2242 if (found_func
== NULL
) {
2243 /* New function, add it to the function list */
2244 O
->funs
->functions
=
2245 (slang_function
*) _slang_realloc(O
->funs
->functions
,
2246 O
->funs
->num_functions
2247 * sizeof(slang_function
),
2248 (O
->funs
->num_functions
+ 1)
2249 * sizeof(slang_function
));
2250 if (O
->funs
->functions
== NULL
) {
2251 /* Make sure that there are no functions marked, as the
2252 * allocation is currently NULL, in order to avoid
2253 * a potental segfault as we clean up later.
2255 O
->funs
->num_functions
= 0;
2257 slang_info_log_memory(C
->L
);
2258 slang_function_destruct(&parsed_func
);
2261 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
2262 O
->funs
->num_functions
++;
2264 /* return the newly parsed function */
2265 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
2268 /* previously defined or declared */
2269 /* TODO: check function return type qualifiers and specifiers */
2271 if (found_func
->body
!= NULL
) {
2272 slang_info_log_error(C
->L
, "%s: function already has a body.",
2273 slang_atom_pool_id(C
->atoms
,
2276 slang_function_destruct(&parsed_func
);
2280 /* destroy the existing function declaration and replace it
2283 slang_function_destruct(found_func
);
2284 *found_func
= parsed_func
;
2287 /* another declaration of the same function prototype - ignore it */
2288 slang_function_destruct(&parsed_func
);
2291 /* return the found function */
2292 *parsed_func_ret
= found_func
;
2299 #define DECLARATION_FUNCTION_PROTOTYPE 1
2300 #define DECLARATION_INIT_DECLARATOR_LIST 2
2303 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2306 case DECLARATION_INIT_DECLARATOR_LIST
:
2307 if (!parse_init_declarator_list(C
, O
))
2310 case DECLARATION_FUNCTION_PROTOTYPE
:
2312 slang_function
*dummy_func
;
2314 if (!parse_function(C
, O
, 0, &dummy_func
))
2325 parse_default_precision(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2327 int precision
, type
;
2329 if (!O
->allow_precision
) {
2330 slang_info_log_error(C
->L
, "syntax error at \"precision\"");
2334 precision
= *C
->I
++;
2335 switch (precision
) {
2337 case PRECISION_MEDIUM
:
2338 case PRECISION_HIGH
:
2342 _mesa_problem(NULL
, "unexpected precision %d at %s:%d\n",
2343 precision
, __FILE__
, __LINE__
);
2349 case TYPE_SPECIFIER_FLOAT
:
2350 case TYPE_SPECIFIER_INT
:
2351 case TYPE_SPECIFIER_SAMPLER1D
:
2352 case TYPE_SPECIFIER_SAMPLER2D
:
2353 case TYPE_SPECIFIER_SAMPLER3D
:
2354 case TYPE_SPECIFIER_SAMPLERCUBE
:
2355 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
2356 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
2357 case TYPE_SPECIFIER_SAMPLER2DRECT
:
2358 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
2362 _mesa_problem(NULL
, "unexpected type %d at %s:%d\n",
2363 type
, __FILE__
, __LINE__
);
2367 assert(type
< TYPE_SPECIFIER_COUNT
);
2368 O
->default_precision
[type
] = precision
;
2375 * Initialize the default precision for all types.
2376 * XXX this info isn't used yet.
2379 init_default_precision(slang_output_ctx
*O
, slang_unit_type type
)
2382 for (i
= 0; i
< TYPE_SPECIFIER_COUNT
; i
++) {
2383 #if FEATURE_es2_glsl
2384 O
->default_precision
[i
] = PRECISION_LOW
;
2386 O
->default_precision
[i
] = PRECISION_HIGH
;
2390 if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2391 O
->default_precision
[TYPE_SPECIFIER_FLOAT
] = PRECISION_HIGH
;
2392 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_HIGH
;
2395 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_MEDIUM
;
2401 parse_invariant(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2403 if (O
->allow_invariant
) {
2404 slang_atom
*a
= parse_identifier(C
);
2405 /* XXX not doing anything with this var yet */
2406 /*printf("ID: %s\n", (char*) a);*/
2410 slang_info_log_error(C
->L
, "syntax error at \"invariant\"");
2416 /* external declaration or default precision specifier */
2417 #define EXTERNAL_NULL 0
2418 #define EXTERNAL_FUNCTION_DEFINITION 1
2419 #define EXTERNAL_DECLARATION 2
2420 #define DEFAULT_PRECISION 3
2421 #define INVARIANT_STMT 4
2425 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
,
2426 struct gl_shader
*shader
)
2428 GET_CURRENT_CONTEXT(ctx
);
2432 slang_function
*mainFunc
= NULL
;
2434 if (unit
->type
== SLANG_UNIT_FRAGMENT_BUILTIN
||
2435 unit
->type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2436 maxRegs
= ctx
->Const
.FragmentProgram
.MaxTemps
;
2439 assert(unit
->type
== SLANG_UNIT_VERTEX_BUILTIN
||
2440 unit
->type
== SLANG_UNIT_VERTEX_SHADER
);
2441 maxRegs
= ctx
->Const
.VertexProgram
.MaxTemps
;
2444 /* setup output context */
2445 o
.funs
= &unit
->funs
;
2446 o
.structs
= &unit
->structs
;
2447 o
.vars
= &unit
->vars
;
2448 o
.program
= shader
? shader
->Program
: NULL
;
2449 o
.pragmas
= shader
? &shader
->Pragmas
: NULL
;
2450 o
.vartable
= _slang_new_var_table(maxRegs
);
2451 _slang_push_var_table(o
.vartable
);
2453 /* allow 'invariant' keyword? */
2454 #if FEATURE_es2_glsl
2455 o
.allow_invariant
= GL_TRUE
;
2457 o
.allow_invariant
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2460 /* allow 'centroid' keyword? */
2461 o
.allow_centroid
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2463 /* allow 'lowp/mediump/highp' keywords? */
2464 #if FEATURE_es2_glsl
2465 o
.allow_precision
= GL_TRUE
;
2467 o
.allow_precision
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2469 init_default_precision(&o
, unit
->type
);
2471 /* allow 'float[]' keyword? */
2472 o
.allow_array_types
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2474 /* parse individual functions and declarations */
2475 while (*C
->I
!= EXTERNAL_NULL
) {
2477 case EXTERNAL_FUNCTION_DEFINITION
:
2479 slang_function
*func
;
2480 success
= parse_function(C
, &o
, 1, &func
);
2482 _mesa_strcmp((char *) func
->header
.a_name
, "main") == 0) {
2488 case EXTERNAL_DECLARATION
:
2489 success
= parse_declaration(C
, &o
);
2491 case DEFAULT_PRECISION
:
2492 success
= parse_default_precision(C
, &o
);
2494 case INVARIANT_STMT
:
2495 success
= parse_invariant(C
, &o
);
2502 /* xxx free codegen */
2503 _slang_pop_var_table(o
.vartable
);
2510 /* assemble (generate code) for main() */
2511 slang_assemble_ctx A
;
2512 memset(&A
, 0, sizeof(slang_assemble_ctx
));
2514 A
.space
.funcs
= o
.funs
;
2515 A
.space
.structs
= o
.structs
;
2516 A
.space
.vars
= o
.vars
;
2517 A
.program
= o
.program
;
2518 A
.pragmas
= &shader
->Pragmas
;
2519 A
.vartable
= o
.vartable
;
2520 A
.EmitContReturn
= ctx
->Shader
.EmitContReturn
;
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 const struct gl_extensions
*extensions
,
2583 struct gl_sl_pragmas
*pragmas
,
2584 unsigned int shader_type
,
2585 unsigned int parsing_builtin
)
2587 struct sl_pp_purify_options options
;
2588 struct sl_pp_context
*context
;
2589 unsigned char *prod
;
2591 unsigned int version
;
2592 unsigned int maxVersion
;
2594 char errmsg
[200] = "";
2595 unsigned int errline
= 0;
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 const struct gl_extensions
*extensions
,
2715 struct gl_sl_pragmas
*pragmas
)
2717 slang_code_unit
*builtins
= NULL
;
2718 GLuint base_version
= 110;
2719 unsigned int shader_type
;
2720 unsigned int parsing_builtin
;
2722 /* set shader type - the syntax is slightly different for different shaders */
2723 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
2729 /* enable language extensions */
2730 parsing_builtin
= 1;
2732 /* if parsing user-specified shader, load built-in library */
2733 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_VERTEX_SHADER
) {
2734 /* compile core functionality first */
2735 if (!compile_binary(slang_core_gc
,
2736 &object
->builtin
[SLANG_BUILTIN_CORE
],
2738 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2742 #if FEATURE_ARB_shading_language_120
2743 if (!compile_binary(slang_120_core_gc
,
2744 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2746 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2747 NULL
, &object
->builtin
[SLANG_BUILTIN_CORE
], NULL
))
2751 /* compile common functions and variables, link to core */
2752 if (!compile_binary(slang_common_builtin_gc
,
2753 &object
->builtin
[SLANG_BUILTIN_COMMON
],
2754 #if FEATURE_ARB_shading_language_120
2759 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2760 #if FEATURE_ARB_shading_language_120
2761 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2763 &object
->builtin
[SLANG_BUILTIN_CORE
],
2768 /* compile target-specific functions and variables, link to common */
2769 if (type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2770 if (!compile_binary(slang_fragment_builtin_gc
,
2771 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2773 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2774 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2776 #if FEATURE_ARB_shading_language_120
2777 if (!compile_binary(slang_120_fragment_gc
,
2778 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2780 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2781 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2785 else if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2786 if (!compile_binary(slang_vertex_builtin_gc
,
2787 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2789 SLANG_UNIT_VERTEX_BUILTIN
, infolog
, NULL
,
2790 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2794 /* disable language extensions */
2795 #if NEW_SLANG /* allow-built-ins */
2796 parsing_builtin
= 1;
2798 parsing_builtin
= 0;
2800 builtins
= object
->builtin
;
2803 /* compile the actual shader - pass-in built-in library for external shader */
2804 return compile_with_grammar(source
,
2818 compile_shader(GLcontext
*ctx
, slang_code_object
* object
,
2819 slang_unit_type type
, slang_info_log
* infolog
,
2820 struct gl_shader
*shader
)
2822 #if 0 /* for debug */
2823 _mesa_printf("********* COMPILE SHADER ***********\n");
2824 _mesa_printf("%s\n", shader
->Source
);
2825 _mesa_printf("************************************\n");
2828 assert(shader
->Program
);
2830 _slang_code_object_dtr(object
);
2831 _slang_code_object_ctr(object
);
2833 return compile_object(shader
->Source
,
2845 _slang_compile(GLcontext
*ctx
, struct gl_shader
*shader
)
2848 slang_info_log info_log
;
2849 slang_code_object obj
;
2850 slang_unit_type type
;
2853 if (shader
->Type
== GL_VERTEX_SHADER
) {
2854 type
= SLANG_UNIT_VERTEX_SHADER
;
2857 assert(shader
->Type
== GL_FRAGMENT_SHADER
);
2858 type
= SLANG_UNIT_FRAGMENT_SHADER
;
2861 if (!shader
->Source
)
2864 ctx
->Shader
.MemPool
= _slang_new_mempool(1024*1024);
2866 shader
->Main
= GL_FALSE
;
2868 /* free the shader's old instructions, etc */
2869 _mesa_reference_program(ctx
, &shader
->Program
, NULL
);
2871 /* allocate new GPU program, parameter lists, etc. */
2872 if (shader
->Type
== GL_VERTEX_SHADER
)
2873 progTarget
= GL_VERTEX_PROGRAM_ARB
;
2875 progTarget
= GL_FRAGMENT_PROGRAM_ARB
;
2876 shader
->Program
= ctx
->Driver
.NewProgram(ctx
, progTarget
, 1);
2877 shader
->Program
->Parameters
= _mesa_new_parameter_list();
2878 shader
->Program
->Varying
= _mesa_new_parameter_list();
2879 shader
->Program
->Attributes
= _mesa_new_parameter_list();
2881 slang_info_log_construct(&info_log
);
2882 _slang_code_object_ctr(&obj
);
2884 success
= compile_shader(ctx
, &obj
, type
, &info_log
, shader
);
2886 /* free shader's prev info log */
2887 if (shader
->InfoLog
) {
2888 _mesa_free(shader
->InfoLog
);
2889 shader
->InfoLog
= NULL
;
2892 if (info_log
.text
) {
2893 /* copy info-log string to shader object */
2894 shader
->InfoLog
= _mesa_strdup(info_log
.text
);
2897 if (info_log
.error_flag
) {
2901 slang_info_log_destruct(&info_log
);
2902 _slang_code_object_dtr(&obj
);
2904 _slang_delete_mempool((slang_mempool
*) ctx
->Shader
.MemPool
);
2905 ctx
->Shader
.MemPool
= NULL
;
2907 /* remove any reads of output registers */
2909 printf("Pre-remove output reads:\n");
2910 _mesa_print_program(shader
->Program
);
2912 _mesa_remove_output_reads(shader
->Program
, PROGRAM_OUTPUT
);
2913 if (shader
->Type
== GL_VERTEX_SHADER
) {
2914 /* and remove writes to varying vars in vertex programs */
2915 _mesa_remove_output_reads(shader
->Program
, PROGRAM_VARYING
);
2918 printf("Post-remove output reads:\n");
2919 _mesa_print_program(shader
->Program
);
2922 shader
->CompileStatus
= success
;
2925 if (shader
->Pragmas
.Optimize
&&
2926 (ctx
->Shader
.Flags
& GLSL_NO_OPT
) == 0) {
2927 _mesa_optimize_program(ctx
, shader
->Program
);
2929 if ((ctx
->Shader
.Flags
& GLSL_NOP_VERT
) &&
2930 shader
->Program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
2931 _mesa_nop_vertex_program(ctx
,
2932 (struct gl_vertex_program
*) shader
->Program
);
2934 if ((ctx
->Shader
.Flags
& GLSL_NOP_FRAG
) &&
2935 shader
->Program
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
2936 _mesa_nop_fragment_program(ctx
,
2937 (struct gl_fragment_program
*) shader
->Program
);
2941 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
2942 _mesa_write_shader_to_file(shader
);