2 * Mesa 3-D graphics library
4 * Copyright (C) 2005-2006 Brian Paul All Rights Reserved.
5 * Copyright (C) 2008 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \file slang_compile.c
27 * slang front-end compiler
31 #include "main/imports.h"
32 #include "main/context.h"
33 #include "shader/program.h"
34 #include "shader/programopt.h"
35 #include "shader/prog_optimize.h"
36 #include "shader/prog_print.h"
37 #include "shader/prog_parameter.h"
38 #include "shader/grammar/grammar_mesa.h"
39 #include "slang_codegen.h"
40 #include "slang_compile.h"
41 #include "slang_storage.h"
42 #include "slang_emit.h"
43 #include "slang_log.h"
44 #include "slang_mem.h"
45 #include "slang_vartable.h"
46 #include "slang_simplify.h"
48 #include "slang_print.h"
51 * This is a straightforward implementation of the slang front-end
52 * compiler. Lots of error-checking functionality is missing but
53 * every well-formed shader source should compile successfully and
54 * execute as expected. However, some semantically ill-formed shaders
55 * may be accepted resulting in undefined behaviour.
59 /** re-defined below, should be the same though */
60 #define TYPE_SPECIFIER_COUNT 32
64 * Check if the given identifier is legal.
67 legal_identifier(slang_atom name
)
69 /* "gl_" is a reserved prefix */
70 if (_mesa_strncmp((char *) name
, "gl_", 3) == 0) {
82 _slang_code_unit_ctr(slang_code_unit
* self
,
83 struct slang_code_object_
* object
)
85 _slang_variable_scope_ctr(&self
->vars
);
86 _slang_function_scope_ctr(&self
->funs
);
87 _slang_struct_scope_ctr(&self
->structs
);
88 self
->object
= object
;
92 _slang_code_unit_dtr(slang_code_unit
* self
)
94 slang_variable_scope_destruct(&self
->vars
);
95 slang_function_scope_destruct(&self
->funs
);
96 slang_struct_scope_destruct(&self
->structs
);
104 _slang_code_object_ctr(slang_code_object
* self
)
108 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
109 _slang_code_unit_ctr(&self
->builtin
[i
], self
);
110 _slang_code_unit_ctr(&self
->unit
, self
);
111 slang_atom_pool_construct(&self
->atompool
);
115 _slang_code_object_dtr(slang_code_object
* self
)
119 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
120 _slang_code_unit_dtr(&self
->builtin
[i
]);
121 _slang_code_unit_dtr(&self
->unit
);
122 slang_atom_pool_destruct(&self
->atompool
);
126 /* slang_parse_ctx */
128 typedef struct slang_parse_ctx_
133 GLboolean global_scope
; /**< Is object being declared a global? */
134 slang_atom_pool
*atoms
;
135 slang_unit_type type
; /**< Vertex vs. Fragment */
136 GLuint version
; /**< user-specified (or default) #version */
139 /* slang_output_ctx */
141 typedef struct slang_output_ctx_
143 slang_variable_scope
*vars
;
144 slang_function_scope
*funs
;
145 slang_struct_scope
*structs
;
146 struct gl_program
*program
;
147 struct gl_sl_pragmas
*pragmas
;
148 slang_var_table
*vartable
;
149 GLuint default_precision
[TYPE_SPECIFIER_COUNT
];
150 GLboolean allow_precision
;
151 GLboolean allow_invariant
;
152 GLboolean allow_centroid
;
153 GLboolean allow_array_types
; /* float[] syntax */
156 /* _slang_compile() */
159 /* Debugging aid, print file/line where parsing error is detected */
163 printf("slang error at %s:%d\n", __FILE__, __LINE__); \
169 parse_identifier_str(slang_parse_ctx
* C
, char **id
)
172 C
->I
+= _mesa_strlen(*id
) + 1;
176 parse_identifier(slang_parse_ctx
* C
)
180 id
= (const char *) C
->I
;
181 C
->I
+= _mesa_strlen(id
) + 1;
182 return slang_atom_pool_atom(C
->atoms
, id
);
188 return (c
>= '0' && c
<= '9') || (c
>= 'a' && c
<= 'f') || (c
>= 'A' && c
<= 'F');
192 parse_general_number(slang_parse_ctx
*ctx
, float *number
)
196 if (*ctx
->I
== '0') {
200 if (ctx
->I
[1] == 'x' || ctx
->I
[1] == 'X') {
202 if (!is_hex_digit(*ctx
->I
)) {
208 if (*ctx
->I
>= '0' && *ctx
->I
<= '9') {
209 digit
= (int)(*ctx
->I
- '0');
210 } else if (*ctx
->I
>= 'a' && *ctx
->I
<= 'f') {
211 digit
= (int)(*ctx
->I
- 'a') + 10;
213 digit
= (int)(*ctx
->I
- 'A') + 10;
215 value
= value
* 0x10 + digit
;
217 } while (is_hex_digit(*ctx
->I
));
218 if (*ctx
->I
!= '\0') {
222 *number
= (float)value
;
228 while (*pi
>= '0' && *pi
<= '7') {
231 digit
= (int)(*pi
- '0');
232 value
= value
* 010 + digit
;
238 *number
= (float)value
;
243 parse_identifier_str(ctx
, &flt
);
248 if (flt
[strlen(flt
) - 1] == 'f' || flt
[strlen(flt
) - 1] == 'F') {
249 flt
[strlen(flt
) - 1] = '\0';
251 *number
= (float)_mesa_strtod(flt
, (char **)NULL
);
258 parse_number(slang_parse_ctx
* C
, int *number
)
260 const int radix
= (int) (*C
->I
++);
265 parse_general_number(C
, &f
);
269 while (*C
->I
!= '\0') {
271 if (*C
->I
>= '0' && *C
->I
<= '9')
272 digit
= (int) (*C
->I
- '0');
273 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
274 digit
= (int) (*C
->I
- 'A') + 10;
276 digit
= (int) (*C
->I
- 'a') + 10;
277 *number
= *number
* radix
+ digit
;
283 slang_info_log_warning(C
->L
, "%d: literal integer overflow.", *number
);
288 parse_float(slang_parse_ctx
* C
, float *number
)
292 parse_general_number(C
, number
);
294 char *integral
= NULL
;
295 char *fractional
= NULL
;
296 char *exponent
= NULL
;
299 parse_identifier_str(C
, &integral
);
300 parse_identifier_str(C
, &fractional
);
301 parse_identifier_str(C
, &exponent
);
303 whole
= (char *) _slang_alloc((_mesa_strlen(integral
) +
304 _mesa_strlen(fractional
) +
305 _mesa_strlen(exponent
) + 3) * sizeof(char));
307 slang_info_log_memory(C
->L
);
311 slang_string_copy(whole
, integral
);
312 slang_string_concat(whole
, ".");
313 slang_string_concat(whole
, fractional
);
314 slang_string_concat(whole
, "E");
315 slang_string_concat(whole
, exponent
);
317 *number
= (float) (_mesa_strtod(whole
, (char **) NULL
));
325 /* revision number - increment after each change affecting emitted output */
329 check_revision(slang_parse_ctx
* C
)
331 if (*C
->I
!= REVISION
) {
332 slang_info_log_error(C
->L
, "Internal compiler error.");
339 static int parse_statement(slang_parse_ctx
*, slang_output_ctx
*,
341 static int parse_expression(slang_parse_ctx
*, slang_output_ctx
*,
343 static int parse_type_specifier(slang_parse_ctx
*, slang_output_ctx
*,
344 slang_type_specifier
*);
346 parse_type_array_size(slang_parse_ctx
*C
,
351 parse_array_len(slang_parse_ctx
* C
, slang_output_ctx
* O
, GLuint
* len
)
353 slang_operation array_size
;
354 slang_name_space space
;
357 if (!slang_operation_construct(&array_size
))
359 if (!parse_expression(C
, O
, &array_size
)) {
360 slang_operation_destruct(&array_size
);
364 space
.funcs
= O
->funs
;
365 space
.structs
= O
->structs
;
366 space
.vars
= O
->vars
;
368 /* evaluate compile-time expression which is array size */
369 _slang_simplify(&array_size
, &space
, C
->atoms
);
371 if (array_size
.type
== SLANG_OPER_LITERAL_INT
) {
373 *len
= (GLint
) array_size
.literal
[0];
374 } else if (array_size
.type
== SLANG_OPER_IDENTIFIER
) {
375 slang_variable
*var
= _slang_variable_locate(array_size
.locals
, array_size
.a_id
, GL_TRUE
);
377 slang_info_log_error(C
->L
, "undefined variable '%s'",
378 (char *) array_size
.a_id
);
380 } else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&&
381 var
->type
.specifier
.type
== SLANG_SPEC_INT
) {
382 if (var
->initializer
&&
383 var
->initializer
->type
== SLANG_OPER_LITERAL_INT
) {
384 *len
= (GLint
) var
->initializer
->literal
[0];
387 slang_info_log_error(C
->L
, "unable to parse array size declaration");
391 slang_info_log_error(C
->L
, "unable to parse array size declaration");
398 slang_operation_destruct(&array_size
);
403 calculate_var_size(slang_parse_ctx
* C
, slang_output_ctx
* O
,
404 slang_variable
* var
)
406 slang_storage_aggregate agg
;
408 if (!slang_storage_aggregate_construct(&agg
))
410 if (!_slang_aggregate_variable(&agg
, &var
->type
.specifier
, var
->array_len
,
411 O
->funs
, O
->structs
, O
->vars
, C
->atoms
)) {
412 slang_storage_aggregate_destruct(&agg
);
415 var
->size
= _slang_sizeof_aggregate(&agg
);
416 slang_storage_aggregate_destruct(&agg
);
421 promote_type_to_array(slang_parse_ctx
*C
,
422 slang_fully_specified_type
*type
,
425 slang_type_specifier
*baseType
=
426 slang_type_specifier_new(type
->specifier
.type
, NULL
, NULL
);
428 type
->specifier
.type
= SLANG_SPEC_ARRAY
;
429 type
->specifier
._array
= baseType
;
430 type
->array_len
= array_len
;
435 convert_to_array(slang_parse_ctx
* C
, slang_variable
* var
,
436 const slang_type_specifier
* sp
)
438 /* sized array - mark it as array, copy the specifier to the array element
439 * and parse the expression */
440 var
->type
.specifier
.type
= SLANG_SPEC_ARRAY
;
441 var
->type
.specifier
._array
= (slang_type_specifier
*)
442 _slang_alloc(sizeof(slang_type_specifier
));
443 if (var
->type
.specifier
._array
== NULL
) {
444 slang_info_log_memory(C
->L
);
447 slang_type_specifier_ctr(var
->type
.specifier
._array
);
448 return slang_type_specifier_copy(var
->type
.specifier
._array
, sp
);
451 /* structure field */
454 #define FIELD_ARRAY 2
457 parse_struct_field_var(slang_parse_ctx
* C
, slang_output_ctx
* O
,
458 slang_variable
* var
, slang_atom a_name
,
459 const slang_type_specifier
* sp
,
462 var
->a_name
= a_name
;
463 if (var
->a_name
== SLANG_ATOM_NULL
)
468 if (array_len
!= -1) {
469 if (!convert_to_array(C
, var
, sp
))
471 var
->array_len
= array_len
;
474 if (!slang_type_specifier_copy(&var
->type
.specifier
, sp
))
481 if (!convert_to_array(C
, var
, sp
))
483 if (!parse_array_len(C
, O
, &var
->array_len
))
490 return calculate_var_size(C
, O
, var
);
494 parse_struct_field(slang_parse_ctx
* C
, slang_output_ctx
* O
,
495 slang_struct
* st
, slang_type_specifier
* sp
)
497 slang_output_ctx o
= *O
;
500 o
.structs
= st
->structs
;
501 if (!parse_type_specifier(C
, &o
, sp
))
503 if (!parse_type_array_size(C
, &o
, &array_len
))
508 slang_variable
*var
= slang_variable_scope_grow(st
->fields
);
510 slang_info_log_memory(C
->L
);
513 a_name
= parse_identifier(C
);
514 if (_slang_variable_locate(st
->fields
, a_name
, GL_FALSE
)) {
515 slang_info_log_error(C
->L
, "duplicate field '%s'", (char *) a_name
);
519 if (!parse_struct_field_var(C
, &o
, var
, a_name
, sp
, array_len
))
522 while (*C
->I
++ != FIELD_NONE
);
528 parse_struct(slang_parse_ctx
* C
, slang_output_ctx
* O
, slang_struct
** st
)
533 /* parse struct name (if any) and make sure it is unique in current scope */
534 a_name
= parse_identifier(C
);
535 if (a_name
== SLANG_ATOM_NULL
)
538 name
= slang_atom_pool_id(C
->atoms
, a_name
);
540 && slang_struct_scope_find(O
->structs
, a_name
, 0) != NULL
) {
541 slang_info_log_error(C
->L
, "%s: duplicate type name.", name
);
545 /* set-up a new struct */
546 *st
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
548 slang_info_log_memory(C
->L
);
551 if (!slang_struct_construct(*st
)) {
554 slang_info_log_memory(C
->L
);
557 (**st
).a_name
= a_name
;
558 (**st
).structs
->outer_scope
= O
->structs
;
560 /* parse individual struct fields */
562 slang_type_specifier sp
;
564 slang_type_specifier_ctr(&sp
);
565 if (!parse_struct_field(C
, O
, *st
, &sp
)) {
566 slang_type_specifier_dtr(&sp
);
569 slang_type_specifier_dtr(&sp
);
571 while (*C
->I
++ != FIELD_NONE
);
573 /* if named struct, copy it to current scope */
574 if (name
[0] != '\0') {
577 O
->structs
->structs
=
578 (slang_struct
*) _slang_realloc(O
->structs
->structs
,
579 O
->structs
->num_structs
580 * sizeof(slang_struct
),
581 (O
->structs
->num_structs
+ 1)
582 * sizeof(slang_struct
));
583 if (O
->structs
->structs
== NULL
) {
584 slang_info_log_memory(C
->L
);
587 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
588 if (!slang_struct_construct(s
))
590 O
->structs
->num_structs
++;
591 if (!slang_struct_copy(s
, *st
))
599 /* invariant qualifer */
600 #define TYPE_VARIANT 90
601 #define TYPE_INVARIANT 91
604 parse_type_variant(slang_parse_ctx
* C
, slang_type_variant
*variant
)
606 GLuint invariant
= *C
->I
++;
609 *variant
= SLANG_VARIANT
;
612 *variant
= SLANG_INVARIANT
;
620 /* centroid qualifer */
621 #define TYPE_CENTER 95
622 #define TYPE_CENTROID 96
625 parse_type_centroid(slang_parse_ctx
* C
, slang_type_centroid
*centroid
)
630 *centroid
= SLANG_CENTER
;
633 *centroid
= SLANG_CENTROID
;
642 #define TYPE_QUALIFIER_NONE 0
643 #define TYPE_QUALIFIER_CONST 1
644 #define TYPE_QUALIFIER_ATTRIBUTE 2
645 #define TYPE_QUALIFIER_VARYING 3
646 #define TYPE_QUALIFIER_UNIFORM 4
647 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
648 #define TYPE_QUALIFIER_FIXEDINPUT 6
651 parse_type_qualifier(slang_parse_ctx
* C
, slang_type_qualifier
* qual
)
653 GLuint qualifier
= *C
->I
++;
655 case TYPE_QUALIFIER_NONE
:
656 *qual
= SLANG_QUAL_NONE
;
658 case TYPE_QUALIFIER_CONST
:
659 *qual
= SLANG_QUAL_CONST
;
661 case TYPE_QUALIFIER_ATTRIBUTE
:
662 *qual
= SLANG_QUAL_ATTRIBUTE
;
664 case TYPE_QUALIFIER_VARYING
:
665 *qual
= SLANG_QUAL_VARYING
;
667 case TYPE_QUALIFIER_UNIFORM
:
668 *qual
= SLANG_QUAL_UNIFORM
;
670 case TYPE_QUALIFIER_FIXEDOUTPUT
:
671 *qual
= SLANG_QUAL_FIXEDOUTPUT
;
673 case TYPE_QUALIFIER_FIXEDINPUT
:
674 *qual
= SLANG_QUAL_FIXEDINPUT
;
683 #define TYPE_SPECIFIER_VOID 0
684 #define TYPE_SPECIFIER_BOOL 1
685 #define TYPE_SPECIFIER_BVEC2 2
686 #define TYPE_SPECIFIER_BVEC3 3
687 #define TYPE_SPECIFIER_BVEC4 4
688 #define TYPE_SPECIFIER_INT 5
689 #define TYPE_SPECIFIER_IVEC2 6
690 #define TYPE_SPECIFIER_IVEC3 7
691 #define TYPE_SPECIFIER_IVEC4 8
692 #define TYPE_SPECIFIER_FLOAT 9
693 #define TYPE_SPECIFIER_VEC2 10
694 #define TYPE_SPECIFIER_VEC3 11
695 #define TYPE_SPECIFIER_VEC4 12
696 #define TYPE_SPECIFIER_MAT2 13
697 #define TYPE_SPECIFIER_MAT3 14
698 #define TYPE_SPECIFIER_MAT4 15
699 #define TYPE_SPECIFIER_SAMPLER1D 16
700 #define TYPE_SPECIFIER_SAMPLER2D 17
701 #define TYPE_SPECIFIER_SAMPLER3D 18
702 #define TYPE_SPECIFIER_SAMPLERCUBE 19
703 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
704 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
705 #define TYPE_SPECIFIER_SAMPLER2DRECT 22
706 #define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
707 #define TYPE_SPECIFIER_STRUCT 24
708 #define TYPE_SPECIFIER_TYPENAME 25
709 #define TYPE_SPECIFIER_MAT23 26
710 #define TYPE_SPECIFIER_MAT32 27
711 #define TYPE_SPECIFIER_MAT24 28
712 #define TYPE_SPECIFIER_MAT42 29
713 #define TYPE_SPECIFIER_MAT34 30
714 #define TYPE_SPECIFIER_MAT43 31
715 #define TYPE_SPECIFIER_COUNT 32
718 parse_type_specifier(slang_parse_ctx
* C
, slang_output_ctx
* O
,
719 slang_type_specifier
* spec
)
722 case TYPE_SPECIFIER_VOID
:
723 spec
->type
= SLANG_SPEC_VOID
;
725 case TYPE_SPECIFIER_BOOL
:
726 spec
->type
= SLANG_SPEC_BOOL
;
728 case TYPE_SPECIFIER_BVEC2
:
729 spec
->type
= SLANG_SPEC_BVEC2
;
731 case TYPE_SPECIFIER_BVEC3
:
732 spec
->type
= SLANG_SPEC_BVEC3
;
734 case TYPE_SPECIFIER_BVEC4
:
735 spec
->type
= SLANG_SPEC_BVEC4
;
737 case TYPE_SPECIFIER_INT
:
738 spec
->type
= SLANG_SPEC_INT
;
740 case TYPE_SPECIFIER_IVEC2
:
741 spec
->type
= SLANG_SPEC_IVEC2
;
743 case TYPE_SPECIFIER_IVEC3
:
744 spec
->type
= SLANG_SPEC_IVEC3
;
746 case TYPE_SPECIFIER_IVEC4
:
747 spec
->type
= SLANG_SPEC_IVEC4
;
749 case TYPE_SPECIFIER_FLOAT
:
750 spec
->type
= SLANG_SPEC_FLOAT
;
752 case TYPE_SPECIFIER_VEC2
:
753 spec
->type
= SLANG_SPEC_VEC2
;
755 case TYPE_SPECIFIER_VEC3
:
756 spec
->type
= SLANG_SPEC_VEC3
;
758 case TYPE_SPECIFIER_VEC4
:
759 spec
->type
= SLANG_SPEC_VEC4
;
761 case TYPE_SPECIFIER_MAT2
:
762 spec
->type
= SLANG_SPEC_MAT2
;
764 case TYPE_SPECIFIER_MAT3
:
765 spec
->type
= SLANG_SPEC_MAT3
;
767 case TYPE_SPECIFIER_MAT4
:
768 spec
->type
= SLANG_SPEC_MAT4
;
770 case TYPE_SPECIFIER_MAT23
:
771 spec
->type
= SLANG_SPEC_MAT23
;
773 case TYPE_SPECIFIER_MAT32
:
774 spec
->type
= SLANG_SPEC_MAT32
;
776 case TYPE_SPECIFIER_MAT24
:
777 spec
->type
= SLANG_SPEC_MAT24
;
779 case TYPE_SPECIFIER_MAT42
:
780 spec
->type
= SLANG_SPEC_MAT42
;
782 case TYPE_SPECIFIER_MAT34
:
783 spec
->type
= SLANG_SPEC_MAT34
;
785 case TYPE_SPECIFIER_MAT43
:
786 spec
->type
= SLANG_SPEC_MAT43
;
788 case TYPE_SPECIFIER_SAMPLER1D
:
789 spec
->type
= SLANG_SPEC_SAMPLER1D
;
791 case TYPE_SPECIFIER_SAMPLER2D
:
792 spec
->type
= SLANG_SPEC_SAMPLER2D
;
794 case TYPE_SPECIFIER_SAMPLER3D
:
795 spec
->type
= SLANG_SPEC_SAMPLER3D
;
797 case TYPE_SPECIFIER_SAMPLERCUBE
:
798 spec
->type
= SLANG_SPEC_SAMPLERCUBE
;
800 case TYPE_SPECIFIER_SAMPLER2DRECT
:
801 spec
->type
= SLANG_SPEC_SAMPLER2DRECT
;
803 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
804 spec
->type
= SLANG_SPEC_SAMPLER1DSHADOW
;
806 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
807 spec
->type
= SLANG_SPEC_SAMPLER2DSHADOW
;
809 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
810 spec
->type
= SLANG_SPEC_SAMPLER2DRECTSHADOW
;
812 case TYPE_SPECIFIER_STRUCT
:
813 spec
->type
= SLANG_SPEC_STRUCT
;
814 if (!parse_struct(C
, O
, &spec
->_struct
))
817 case TYPE_SPECIFIER_TYPENAME
:
818 spec
->type
= SLANG_SPEC_STRUCT
;
823 a_name
= parse_identifier(C
);
827 stru
= slang_struct_scope_find(O
->structs
, a_name
, 1);
829 slang_info_log_error(C
->L
, "undeclared type name '%s'",
830 slang_atom_pool_id(C
->atoms
, a_name
));
834 spec
->_struct
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
835 if (spec
->_struct
== NULL
) {
836 slang_info_log_memory(C
->L
);
839 if (!slang_struct_construct(spec
->_struct
)) {
840 _slang_free(spec
->_struct
);
841 spec
->_struct
= NULL
;
844 if (!slang_struct_copy(spec
->_struct
, stru
))
854 #define TYPE_SPECIFIER_NONARRAY 0
855 #define TYPE_SPECIFIER_ARRAY 1
858 parse_type_array_size(slang_parse_ctx
*C
,
865 case TYPE_SPECIFIER_NONARRAY
:
866 *array_len
= -1; /* -1 = not an array */
868 case TYPE_SPECIFIER_ARRAY
:
869 if (!parse_array_len(C
, O
, &size
))
871 *array_len
= (GLint
) size
;
880 #define PRECISION_DEFAULT 0
881 #define PRECISION_LOW 1
882 #define PRECISION_MEDIUM 2
883 #define PRECISION_HIGH 3
886 parse_type_precision(slang_parse_ctx
*C
,
887 slang_type_precision
*precision
)
889 GLint prec
= *C
->I
++;
891 case PRECISION_DEFAULT
:
892 *precision
= SLANG_PREC_DEFAULT
;
895 *precision
= SLANG_PREC_LOW
;
897 case PRECISION_MEDIUM
:
898 *precision
= SLANG_PREC_MEDIUM
;
901 *precision
= SLANG_PREC_HIGH
;
909 parse_fully_specified_type(slang_parse_ctx
* C
, slang_output_ctx
* O
,
910 slang_fully_specified_type
* type
)
912 if (!parse_type_variant(C
, &type
->variant
))
915 if (!parse_type_centroid(C
, &type
->centroid
))
918 if (!parse_type_qualifier(C
, &type
->qualifier
))
921 if (!parse_type_precision(C
, &type
->precision
))
924 if (!parse_type_specifier(C
, O
, &type
->specifier
))
927 if (!parse_type_array_size(C
, O
, &type
->array_len
))
930 if (!O
->allow_invariant
&& type
->variant
== SLANG_INVARIANT
) {
931 slang_info_log_error(C
->L
,
932 "'invariant' keyword not allowed (perhaps set #version 120)");
936 if (!O
->allow_centroid
&& type
->centroid
== SLANG_CENTROID
) {
937 slang_info_log_error(C
->L
,
938 "'centroid' keyword not allowed (perhaps set #version 120)");
941 else if (type
->centroid
== SLANG_CENTROID
&&
942 type
->qualifier
!= SLANG_QUAL_VARYING
) {
943 slang_info_log_error(C
->L
,
944 "'centroid' keyword only allowed for varying vars");
950 if (type->qualifier != SLANG_QUAL_VARYING &&
951 type->variant == SLANG_INVARIANT) {
952 slang_info_log_error(C->L,
953 "invariant qualifer only allowed for varying vars");
958 if (O
->allow_precision
) {
959 if (type
->precision
== SLANG_PREC_DEFAULT
) {
960 assert(type
->specifier
.type
< TYPE_SPECIFIER_COUNT
);
961 /* use the default precision for this datatype */
962 type
->precision
= O
->default_precision
[type
->specifier
.type
];
966 /* only default is allowed */
967 if (type
->precision
!= SLANG_PREC_DEFAULT
) {
968 slang_info_log_error(C
->L
, "precision qualifiers not allowed");
973 if (!O
->allow_array_types
&& type
->array_len
>= 0) {
974 slang_info_log_error(C
->L
, "first-class array types not allowed");
978 if (type
->array_len
>= 0) {
979 /* convert type to array type (ex: convert "int" to "array of int" */
980 promote_type_to_array(C
, type
, type
->array_len
);
988 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
989 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
993 #define OP_CONTINUE 6
996 #define OP_EXPRESSION 9
1001 #define OP_PUSH_VOID 14
1002 #define OP_PUSH_BOOL 15
1003 #define OP_PUSH_INT 16
1004 #define OP_PUSH_FLOAT 17
1005 #define OP_PUSH_IDENTIFIER 18
1006 #define OP_SEQUENCE 19
1007 #define OP_ASSIGN 20
1008 #define OP_ADDASSIGN 21
1009 #define OP_SUBASSIGN 22
1010 #define OP_MULASSIGN 23
1011 #define OP_DIVASSIGN 24
1012 /*#define OP_MODASSIGN 25*/
1013 /*#define OP_LSHASSIGN 26*/
1014 /*#define OP_RSHASSIGN 27*/
1015 /*#define OP_ORASSIGN 28*/
1016 /*#define OP_XORASSIGN 29*/
1017 /*#define OP_ANDASSIGN 30*/
1018 #define OP_SELECT 31
1019 #define OP_LOGICALOR 32
1020 #define OP_LOGICALXOR 33
1021 #define OP_LOGICALAND 34
1022 /*#define OP_BITOR 35*/
1023 /*#define OP_BITXOR 36*/
1024 /*#define OP_BITAND 37*/
1026 #define OP_NOTEQUAL 39
1028 #define OP_GREATER 41
1029 #define OP_LESSEQUAL 42
1030 #define OP_GREATEREQUAL 43
1031 /*#define OP_LSHIFT 44*/
1032 /*#define OP_RSHIFT 45*/
1034 #define OP_SUBTRACT 47
1035 #define OP_MULTIPLY 48
1036 #define OP_DIVIDE 49
1037 /*#define OP_MODULUS 50*/
1038 #define OP_PREINCREMENT 51
1039 #define OP_PREDECREMENT 52
1042 /*#define OP_COMPLEMENT 55*/
1044 #define OP_SUBSCRIPT 57
1047 #define OP_POSTINCREMENT 60
1048 #define OP_POSTDECREMENT 61
1049 #define OP_PRECISION 62
1050 #define OP_METHOD 63
1054 * When parsing a compound production, this function is used to parse the
1056 * For example, a while-loop compound will have two children, the
1057 * while condition expression and the loop body. So, this function will
1058 * be called twice to parse those two sub-expressions.
1059 * \param C the parsing context
1060 * \param O the output context
1061 * \param oper the operation we're parsing
1062 * \param statement indicates whether parsing a statement, or expression
1063 * \return 1 if success, 0 if error
1066 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1067 slang_operation
* oper
, GLboolean statement
)
1069 slang_operation
*ch
;
1071 /* grow child array */
1072 ch
= slang_operation_grow(&oper
->num_children
, &oper
->children
);
1074 return parse_statement(C
, O
, ch
);
1075 return parse_expression(C
, O
, ch
);
1078 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
1081 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1082 slang_operation
* oper
)
1086 oper
->locals
->outer_scope
= O
->vars
;
1090 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
1091 /* parse child statements, do not create new variable scope */
1092 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1093 while (*C
->I
!= OP_END
)
1094 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1098 case OP_BLOCK_BEGIN_NEW_SCOPE
:
1099 /* parse child statements, create new variable scope */
1101 slang_output_ctx o
= *O
;
1103 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1104 o
.vars
= oper
->locals
;
1105 while (*C
->I
!= OP_END
)
1106 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1112 /* local variable declaration, individual declarators are stored as
1113 * children identifiers
1115 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1117 const unsigned int first_var
= O
->vars
->num_variables
;
1119 /* parse the declaration, note that there can be zero or more
1120 * than one declarators
1122 if (!parse_declaration(C
, O
))
1124 if (first_var
< O
->vars
->num_variables
) {
1125 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
1127 assert(oper
->num_children
== 0);
1128 oper
->num_children
= num_vars
;
1129 oper
->children
= slang_operation_new(num_vars
);
1130 if (oper
->children
== NULL
) {
1131 slang_info_log_memory(C
->L
);
1134 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
1135 slang_operation
*o
= &oper
->children
[i
- first_var
];
1136 slang_variable
*var
= O
->vars
->variables
[i
];
1137 o
->type
= SLANG_OPER_VARIABLE_DECL
;
1138 o
->locals
->outer_scope
= O
->vars
;
1139 o
->a_id
= var
->a_name
;
1142 calculate_var_size(C, O, var);
1145 if (!legal_identifier(o
->a_id
)) {
1146 slang_info_log_error(C
->L
, "illegal variable name '%s'",
1155 /* the __asm statement, parse the mnemonic and all its arguments
1158 oper
->type
= SLANG_OPER_ASM
;
1159 oper
->a_id
= parse_identifier(C
);
1160 if (oper
->a_id
== SLANG_ATOM_NULL
)
1162 while (*C
->I
!= OP_END
) {
1163 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1169 oper
->type
= SLANG_OPER_BREAK
;
1172 oper
->type
= SLANG_OPER_CONTINUE
;
1175 oper
->type
= SLANG_OPER_DISCARD
;
1178 oper
->type
= SLANG_OPER_RETURN
;
1179 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1183 oper
->type
= SLANG_OPER_EXPRESSION
;
1184 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1188 oper
->type
= SLANG_OPER_IF
;
1189 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1191 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1193 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1198 slang_output_ctx o
= *O
;
1200 oper
->type
= SLANG_OPER_WHILE
;
1201 o
.vars
= oper
->locals
;
1202 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1204 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1209 oper
->type
= SLANG_OPER_DO
;
1210 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1212 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1217 slang_output_ctx o
= *O
;
1219 oper
->type
= SLANG_OPER_FOR
;
1220 o
.vars
= oper
->locals
;
1221 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1223 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1225 if (!parse_child_operation(C
, &o
, oper
, GL_FALSE
))
1227 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1233 /* set default precision for a type in this scope */
1234 /* ignored at this time */
1235 int prec_qual
= *C
->I
++;
1236 int datatype
= *C
->I
++;
1242 /*printf("Unexpected operation %d\n", op);*/
1249 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
1250 slang_operation
** ops
, unsigned int *total_ops
,
1255 op
->children
= slang_operation_new(n
);
1256 if (op
->children
== NULL
) {
1257 slang_info_log_memory(C
->L
);
1260 op
->num_children
= n
;
1262 for (i
= 0; i
< n
; i
++) {
1263 slang_operation_destruct(&op
->children
[i
]);
1264 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
1267 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
1270 *ops
= (slang_operation
*)
1271 _slang_realloc(*ops
,
1272 (*total_ops
+ n
) * sizeof(slang_operation
),
1273 *total_ops
* sizeof(slang_operation
));
1275 slang_info_log_memory(C
->L
);
1282 is_constructor_name(const char *name
, slang_atom a_name
,
1283 slang_struct_scope
* structs
)
1285 if (slang_type_specifier_type_from_string(name
) != SLANG_SPEC_VOID
)
1287 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
1290 #define FUNCTION_CALL_NONARRAY 0
1291 #define FUNCTION_CALL_ARRAY 1
1294 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1295 slang_operation
* oper
)
1297 slang_operation
*ops
= NULL
;
1298 unsigned int num_ops
= 0;
1301 while (*C
->I
!= OP_END
) {
1302 slang_operation
*op
;
1303 const unsigned int op_code
= *C
->I
++;
1305 /* allocate default operation, becomes a no-op if not used */
1306 ops
= (slang_operation
*)
1308 num_ops
* sizeof(slang_operation
),
1309 (num_ops
+ 1) * sizeof(slang_operation
));
1311 slang_info_log_memory(C
->L
);
1315 if (!slang_operation_construct(op
)) {
1316 slang_info_log_memory(C
->L
);
1320 op
->locals
->outer_scope
= O
->vars
;
1324 op
->type
= SLANG_OPER_VOID
;
1327 op
->type
= SLANG_OPER_LITERAL_BOOL
;
1328 if (!parse_number(C
, &number
))
1333 op
->literal
[3] = (GLfloat
) number
;
1334 op
->literal_size
= 1;
1337 op
->type
= SLANG_OPER_LITERAL_INT
;
1338 if (!parse_number(C
, &number
))
1343 op
->literal
[3] = (GLfloat
) number
;
1344 op
->literal_size
= 1;
1347 op
->type
= SLANG_OPER_LITERAL_FLOAT
;
1348 if (!parse_float(C
, &op
->literal
[0]))
1352 op
->literal
[3] = op
->literal
[0];
1353 op
->literal_size
= 1;
1355 case OP_PUSH_IDENTIFIER
:
1356 op
->type
= SLANG_OPER_IDENTIFIER
;
1357 op
->a_id
= parse_identifier(C
);
1358 if (op
->a_id
== SLANG_ATOM_NULL
)
1362 op
->type
= SLANG_OPER_SEQUENCE
;
1363 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1367 op
->type
= SLANG_OPER_ASSIGN
;
1368 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1372 op
->type
= SLANG_OPER_ADDASSIGN
;
1373 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1377 op
->type
= SLANG_OPER_SUBASSIGN
;
1378 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1382 op
->type
= SLANG_OPER_MULASSIGN
;
1383 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1387 op
->type
= SLANG_OPER_DIVASSIGN
;
1388 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1391 /*case OP_MODASSIGN: */
1392 /*case OP_LSHASSIGN: */
1393 /*case OP_RSHASSIGN: */
1394 /*case OP_ORASSIGN: */
1395 /*case OP_XORASSIGN: */
1396 /*case OP_ANDASSIGN: */
1398 op
->type
= SLANG_OPER_SELECT
;
1399 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1403 op
->type
= SLANG_OPER_LOGICALOR
;
1404 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1408 op
->type
= SLANG_OPER_LOGICALXOR
;
1409 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1413 op
->type
= SLANG_OPER_LOGICALAND
;
1414 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1418 /*case OP_BITXOR: */
1419 /*case OP_BITAND: */
1421 op
->type
= SLANG_OPER_EQUAL
;
1422 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1426 op
->type
= SLANG_OPER_NOTEQUAL
;
1427 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1431 op
->type
= SLANG_OPER_LESS
;
1432 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1436 op
->type
= SLANG_OPER_GREATER
;
1437 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1441 op
->type
= SLANG_OPER_LESSEQUAL
;
1442 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1445 case OP_GREATEREQUAL
:
1446 op
->type
= SLANG_OPER_GREATEREQUAL
;
1447 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1450 /*case OP_LSHIFT: */
1451 /*case OP_RSHIFT: */
1453 op
->type
= SLANG_OPER_ADD
;
1454 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1458 op
->type
= SLANG_OPER_SUBTRACT
;
1459 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1463 op
->type
= SLANG_OPER_MULTIPLY
;
1464 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1468 op
->type
= SLANG_OPER_DIVIDE
;
1469 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1472 /*case OP_MODULUS: */
1473 case OP_PREINCREMENT
:
1474 op
->type
= SLANG_OPER_PREINCREMENT
;
1475 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1478 case OP_PREDECREMENT
:
1479 op
->type
= SLANG_OPER_PREDECREMENT
;
1480 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1484 op
->type
= SLANG_OPER_PLUS
;
1485 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1489 op
->type
= SLANG_OPER_MINUS
;
1490 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1494 op
->type
= SLANG_OPER_NOT
;
1495 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1498 /*case OP_COMPLEMENT: */
1500 op
->type
= SLANG_OPER_SUBSCRIPT
;
1501 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1505 op
->type
= SLANG_OPER_METHOD
;
1506 op
->a_obj
= parse_identifier(C
);
1507 if (op
->a_obj
== SLANG_ATOM_NULL
)
1510 op
->a_id
= parse_identifier(C
);
1511 if (op
->a_id
== SLANG_ATOM_NULL
)
1514 assert(*C
->I
== OP_END
);
1517 while (*C
->I
!= OP_END
)
1518 if (!parse_child_operation(C
, O
, op
, GL_FALSE
))
1522 /* don't lookup the method (not yet anyway) */
1523 if (!C
->parsing_builtin
1524 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1527 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1528 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1529 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1537 GLboolean array_constructor
= GL_FALSE
;
1538 GLint array_constructor_size
= 0;
1540 op
->type
= SLANG_OPER_CALL
;
1541 op
->a_id
= parse_identifier(C
);
1542 if (op
->a_id
== SLANG_ATOM_NULL
)
1545 case FUNCTION_CALL_NONARRAY
:
1546 /* Nothing to do. */
1548 case FUNCTION_CALL_ARRAY
:
1549 /* Calling an array constructor. For example:
1550 * float[3](1.1, 2.2, 3.3);
1552 if (!O
->allow_array_types
) {
1553 slang_info_log_error(C
->L
,
1554 "array constructors not allowed "
1555 "in this GLSL version");
1559 /* parse the array constructor size */
1560 slang_operation array_size
;
1561 array_constructor
= GL_TRUE
;
1562 slang_operation_construct(&array_size
);
1563 if (!parse_expression(C
, O
, &array_size
)) {
1564 slang_operation_destruct(&array_size
);
1567 if (array_size
.type
!= SLANG_OPER_LITERAL_INT
) {
1568 slang_info_log_error(C
->L
,
1569 "constructor array size is not an integer");
1570 slang_operation_destruct(&array_size
);
1573 array_constructor_size
= (int) array_size
.literal
[0];
1574 op
->array_constructor
= GL_TRUE
;
1575 slang_operation_destruct(&array_size
);
1582 while (*C
->I
!= OP_END
)
1583 if (!parse_child_operation(C
, O
, op
, GL_FALSE
))
1587 if (array_constructor
&&
1588 array_constructor_size
!= op
->num_children
) {
1589 slang_info_log_error(C
->L
, "number of parameters to array"
1590 " constructor does not match array size");
1594 if (!C
->parsing_builtin
1595 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1598 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1599 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1600 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1607 op
->type
= SLANG_OPER_FIELD
;
1608 op
->a_id
= parse_identifier(C
);
1609 if (op
->a_id
== SLANG_ATOM_NULL
)
1611 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1614 case OP_POSTINCREMENT
:
1615 op
->type
= SLANG_OPER_POSTINCREMENT
;
1616 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1619 case OP_POSTDECREMENT
:
1620 op
->type
= SLANG_OPER_POSTDECREMENT
;
1621 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1630 slang_operation_destruct(oper
);
1631 *oper
= *ops
; /* struct copy */
1637 /* parameter qualifier */
1638 #define PARAM_QUALIFIER_IN 0
1639 #define PARAM_QUALIFIER_OUT 1
1640 #define PARAM_QUALIFIER_INOUT 2
1642 /* function parameter array presence */
1643 #define PARAMETER_ARRAY_NOT_PRESENT 0
1644 #define PARAMETER_ARRAY_PRESENT 1
1647 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1648 slang_variable
* param
)
1650 int param_qual
, precision_qual
;
1652 /* parse and validate the parameter's type qualifiers (there can be
1653 * two at most) because not all combinations are valid
1655 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1658 param_qual
= *C
->I
++;
1659 switch (param_qual
) {
1660 case PARAM_QUALIFIER_IN
:
1661 if (param
->type
.qualifier
!= SLANG_QUAL_CONST
1662 && param
->type
.qualifier
!= SLANG_QUAL_NONE
) {
1663 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1667 case PARAM_QUALIFIER_OUT
:
1668 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1669 param
->type
.qualifier
= SLANG_QUAL_OUT
;
1671 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1675 case PARAM_QUALIFIER_INOUT
:
1676 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1677 param
->type
.qualifier
= SLANG_QUAL_INOUT
;
1679 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1687 /* parse precision qualifier (lowp, mediump, highp */
1688 precision_qual
= *C
->I
++;
1689 /* ignored at this time */
1690 (void) precision_qual
;
1692 /* parse parameter's type specifier and name */
1693 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1695 if (!parse_type_array_size(C
, O
, ¶m
->type
.array_len
))
1697 param
->a_name
= parse_identifier(C
);
1698 if (param
->a_name
== SLANG_ATOM_NULL
)
1701 /* first-class array
1703 if (param
->type
.array_len
>= 0) {
1704 slang_type_specifier p
;
1706 slang_type_specifier_ctr(&p
);
1707 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1708 slang_type_specifier_dtr(&p
);
1711 if (!convert_to_array(C
, param
, &p
)) {
1712 slang_type_specifier_dtr(&p
);
1715 slang_type_specifier_dtr(&p
);
1716 param
->array_len
= param
->type
.array_len
;
1719 /* if the parameter is an array, parse its size (the size must be
1720 * explicitly defined
1722 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1723 slang_type_specifier p
;
1725 if (param
->type
.array_len
>= 0) {
1726 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
1729 slang_type_specifier_ctr(&p
);
1730 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1731 slang_type_specifier_dtr(&p
);
1734 if (!convert_to_array(C
, param
, &p
)) {
1735 slang_type_specifier_dtr(&p
);
1738 slang_type_specifier_dtr(&p
);
1739 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1744 /* calculate the parameter size */
1745 if (!calculate_var_size(C
, O
, param
))
1748 /* TODO: allocate the local address here? */
1753 #define FUNCTION_ORDINARY 0
1754 #define FUNCTION_CONSTRUCTOR 1
1755 #define FUNCTION_OPERATOR 2
1757 /* function parameter */
1758 #define PARAMETER_NONE 0
1759 #define PARAMETER_NEXT 1
1762 #define OPERATOR_ADDASSIGN 1
1763 #define OPERATOR_SUBASSIGN 2
1764 #define OPERATOR_MULASSIGN 3
1765 #define OPERATOR_DIVASSIGN 4
1766 /*#define OPERATOR_MODASSIGN 5*/
1767 /*#define OPERATOR_LSHASSIGN 6*/
1768 /*#define OPERATOR_RSHASSIGN 7*/
1769 /*#define OPERATOR_ANDASSIGN 8*/
1770 /*#define OPERATOR_XORASSIGN 9*/
1771 /*#define OPERATOR_ORASSIGN 10*/
1772 #define OPERATOR_LOGICALXOR 11
1773 /*#define OPERATOR_BITOR 12*/
1774 /*#define OPERATOR_BITXOR 13*/
1775 /*#define OPERATOR_BITAND 14*/
1776 #define OPERATOR_LESS 15
1777 #define OPERATOR_GREATER 16
1778 #define OPERATOR_LESSEQUAL 17
1779 #define OPERATOR_GREATEREQUAL 18
1780 /*#define OPERATOR_LSHIFT 19*/
1781 /*#define OPERATOR_RSHIFT 20*/
1782 #define OPERATOR_MULTIPLY 21
1783 #define OPERATOR_DIVIDE 22
1784 /*#define OPERATOR_MODULUS 23*/
1785 #define OPERATOR_INCREMENT 24
1786 #define OPERATOR_DECREMENT 25
1787 #define OPERATOR_PLUS 26
1788 #define OPERATOR_MINUS 27
1789 /*#define OPERATOR_COMPLEMENT 28*/
1790 #define OPERATOR_NOT 29
1794 unsigned int o_code
;
1796 } operator_names
[] = {
1797 {OPERATOR_INCREMENT
, "++"},
1798 {OPERATOR_ADDASSIGN
, "+="},
1799 {OPERATOR_PLUS
, "+"},
1800 {OPERATOR_DECREMENT
, "--"},
1801 {OPERATOR_SUBASSIGN
, "-="},
1802 {OPERATOR_MINUS
, "-"},
1803 {OPERATOR_NOT
, "!"},
1804 {OPERATOR_MULASSIGN
, "*="},
1805 {OPERATOR_MULTIPLY
, "*"},
1806 {OPERATOR_DIVASSIGN
, "/="},
1807 {OPERATOR_DIVIDE
, "/"},
1808 {OPERATOR_LESSEQUAL
, "<="},
1809 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1810 /*{ OPERATOR_LSHIFT, "<<" }, */
1811 {OPERATOR_LESS
, "<"},
1812 {OPERATOR_GREATEREQUAL
, ">="},
1813 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1814 /*{ OPERATOR_RSHIFT, ">>" }, */
1815 {OPERATOR_GREATER
, ">"},
1816 /*{ OPERATOR_MODASSIGN, "%=" }, */
1817 /*{ OPERATOR_MODULUS, "%" }, */
1818 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1819 /*{ OPERATOR_BITAND, "&" }, */
1820 /*{ OPERATOR_ORASSIGN, "|=" }, */
1821 /*{ OPERATOR_BITOR, "|" }, */
1822 /*{ OPERATOR_COMPLEMENT, "~" }, */
1823 /*{ OPERATOR_XORASSIGN, "^=" }, */
1824 {OPERATOR_LOGICALXOR
, "^^"},
1825 /*{ OPERATOR_BITXOR, "^" } */
1829 parse_operator_name(slang_parse_ctx
* C
)
1833 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1834 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1836 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1837 if (atom
== SLANG_ATOM_NULL
) {
1838 slang_info_log_memory(C
->L
);
1850 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1851 slang_function
* func
)
1854 /* parse function type and name */
1855 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1860 case FUNCTION_ORDINARY
:
1861 func
->kind
= SLANG_FUNC_ORDINARY
;
1862 func
->header
.a_name
= parse_identifier(C
);
1863 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1866 case FUNCTION_CONSTRUCTOR
:
1867 func
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1868 if (func
->header
.type
.specifier
.type
== SLANG_SPEC_STRUCT
)
1870 func
->header
.a_name
=
1871 slang_atom_pool_atom(C
->atoms
,
1872 slang_type_specifier_type_to_string
1873 (func
->header
.type
.specifier
.type
));
1874 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1875 slang_info_log_memory(C
->L
);
1879 case FUNCTION_OPERATOR
:
1880 func
->kind
= SLANG_FUNC_OPERATOR
;
1881 func
->header
.a_name
= parse_operator_name(C
);
1882 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1889 if (!legal_identifier(func
->header
.a_name
)) {
1890 slang_info_log_error(C
->L
, "illegal function name '%s'",
1891 (char *) func
->header
.a_name
);
1895 /* parse function parameters */
1896 while (*C
->I
++ == PARAMETER_NEXT
) {
1897 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1899 slang_info_log_memory(C
->L
);
1902 if (!parse_parameter_declaration(C
, O
, p
))
1906 /* if the function returns a value, append a hidden __retVal 'out'
1907 * parameter that corresponds to the return value.
1909 if (_slang_function_has_return_value(func
)) {
1910 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1911 slang_atom a_retVal
= slang_atom_pool_atom(C
->atoms
, "__retVal");
1913 p
->a_name
= a_retVal
;
1914 p
->type
= func
->header
.type
;
1915 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1918 /* function formal parameters and local variables share the same
1919 * scope, so save the information about param count in a seperate
1920 * place also link the scope to the global variable scope so when a
1921 * given identifier is not found here, the search process continues
1922 * in the global space
1924 func
->param_count
= func
->parameters
->num_variables
;
1925 func
->parameters
->outer_scope
= O
->vars
;
1931 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1932 slang_function
* func
)
1934 slang_output_ctx o
= *O
;
1936 if (!parse_function_prototype(C
, O
, func
))
1939 /* create function's body operation */
1940 func
->body
= (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1941 if (func
->body
== NULL
) {
1942 slang_info_log_memory(C
->L
);
1945 if (!slang_operation_construct(func
->body
)) {
1946 _slang_free(func
->body
);
1948 slang_info_log_memory(C
->L
);
1952 /* to parse the body the parse context is modified in order to
1953 * capture parsed variables into function's local variable scope
1955 C
->global_scope
= GL_FALSE
;
1956 o
.vars
= func
->parameters
;
1957 if (!parse_statement(C
, &o
, func
->body
))
1960 C
->global_scope
= GL_TRUE
;
1965 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1967 slang_operation op_id
, op_assign
;
1970 /* construct the left side of assignment */
1971 if (!slang_operation_construct(&op_id
))
1973 op_id
.type
= SLANG_OPER_IDENTIFIER
;
1974 op_id
.a_id
= var
->a_name
;
1976 /* put the variable into operation's scope */
1977 op_id
.locals
->variables
=
1978 (slang_variable
**) _slang_alloc(sizeof(slang_variable
*));
1979 if (op_id
.locals
->variables
== NULL
) {
1980 slang_operation_destruct(&op_id
);
1983 op_id
.locals
->num_variables
= 1;
1984 op_id
.locals
->variables
[0] = var
;
1986 /* construct the assignment expression */
1987 if (!slang_operation_construct(&op_assign
)) {
1988 op_id
.locals
->num_variables
= 0;
1989 slang_operation_destruct(&op_id
);
1992 op_assign
.type
= SLANG_OPER_ASSIGN
;
1993 op_assign
.children
=
1994 (slang_operation
*) _slang_alloc(2 * sizeof(slang_operation
));
1995 if (op_assign
.children
== NULL
) {
1996 slang_operation_destruct(&op_assign
);
1997 op_id
.locals
->num_variables
= 0;
1998 slang_operation_destruct(&op_id
);
2001 op_assign
.num_children
= 2;
2002 op_assign
.children
[0] = op_id
;
2003 op_assign
.children
[1] = *var
->initializer
;
2007 /* carefully destroy the operations */
2008 op_assign
.num_children
= 0;
2009 _slang_free(op_assign
.children
);
2010 op_assign
.children
= NULL
;
2011 slang_operation_destruct(&op_assign
);
2012 op_id
.locals
->num_variables
= 0;
2013 slang_operation_destruct(&op_id
);
2021 /* init declarator list */
2022 #define DECLARATOR_NONE 0
2023 #define DECLARATOR_NEXT 1
2025 /* variable declaration */
2026 #define VARIABLE_NONE 0
2027 #define VARIABLE_IDENTIFIER 1
2028 #define VARIABLE_INITIALIZER 2
2029 #define VARIABLE_ARRAY_EXPLICIT 3
2030 #define VARIABLE_ARRAY_UNKNOWN 4
2034 * Parse the initializer for a variable declaration.
2037 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
2038 const slang_fully_specified_type
* type
)
2040 GET_CURRENT_CONTEXT(ctx
); /* a hack */
2041 slang_variable
*var
;
2044 /* empty init declatator (without name, e.g. "float ;") */
2045 if (*C
->I
++ == VARIABLE_NONE
)
2048 a_name
= parse_identifier(C
);
2050 /* check if name is already in this scope */
2051 if (_slang_variable_locate(O
->vars
, a_name
, GL_FALSE
)) {
2052 slang_info_log_error(C
->L
,
2053 "declaration of '%s' conflicts with previous declaration",
2058 /* make room for the new variable and initialize it */
2059 var
= slang_variable_scope_grow(O
->vars
);
2061 slang_info_log_memory(C
->L
);
2065 /* copy the declarator type qualifier/etc info, parse the identifier */
2066 var
->type
.qualifier
= type
->qualifier
;
2067 var
->type
.centroid
= type
->centroid
;
2068 var
->type
.precision
= type
->precision
;
2069 var
->type
.variant
= type
->variant
;
2070 var
->type
.array_len
= type
->array_len
;
2071 var
->a_name
= a_name
;
2072 if (var
->a_name
== SLANG_ATOM_NULL
)
2077 /* simple variable declarator - just copy the specifier */
2078 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
2081 case VARIABLE_INITIALIZER
:
2082 /* initialized variable - copy the specifier and parse the expression */
2083 if (0 && type
->array_len
>= 0) {
2084 /* The type was something like "float[4]" */
2085 convert_to_array(C
, var
, &type
->specifier
);
2086 var
->array_len
= type
->array_len
;
2089 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
2093 (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
2094 if (var
->initializer
== NULL
) {
2095 slang_info_log_memory(C
->L
);
2098 if (!slang_operation_construct(var
->initializer
)) {
2099 _slang_free(var
->initializer
);
2100 var
->initializer
= NULL
;
2101 slang_info_log_memory(C
->L
);
2104 if (!parse_expression(C
, O
, var
->initializer
))
2107 case VARIABLE_ARRAY_UNKNOWN
:
2108 /* unsized array - mark it as array and copy the specifier to
2111 if (type
->array_len
>= 0) {
2112 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
2115 if (!convert_to_array(C
, var
, &type
->specifier
))
2118 case VARIABLE_ARRAY_EXPLICIT
:
2119 if (type
->array_len
>= 0) {
2120 /* the user is trying to do something like: float[2] x[3]; */
2121 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
2124 if (!convert_to_array(C
, var
, &type
->specifier
))
2126 if (!parse_array_len(C
, O
, &var
->array_len
))
2133 /* allocate global address space for a variable with a known size */
2135 && !(var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
2136 && var
->array_len
== 0)) {
2137 if (!calculate_var_size(C
, O
, var
))
2141 /* emit code for global var decl */
2142 if (C
->global_scope
) {
2143 slang_assemble_ctx A
;
2144 memset(&A
, 0, sizeof(slang_assemble_ctx
));
2146 A
.space
.funcs
= O
->funs
;
2147 A
.space
.structs
= O
->structs
;
2148 A
.space
.vars
= O
->vars
;
2149 A
.program
= O
->program
;
2150 A
.pragmas
= O
->pragmas
;
2151 A
.vartable
= O
->vartable
;
2153 A
.curFuncEndLabel
= NULL
;
2154 A
.EmitContReturn
= ctx
->Shader
.EmitContReturn
;
2155 if (!_slang_codegen_global_variable(&A
, var
, C
->type
))
2159 /* initialize global variable */
2160 if (C
->global_scope
) {
2161 if (var
->initializer
!= NULL
) {
2162 slang_assemble_ctx A
;
2163 memset(&A
, 0, sizeof(slang_assemble_ctx
));
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
;
2522 /* main() takes no parameters */
2523 if (mainFunc
->param_count
> 0) {
2524 slang_info_log_error(A
.log
, "main() takes no arguments");
2528 _slang_codegen_function(&A
, mainFunc
);
2530 shader
->Main
= GL_TRUE
; /* this shader defines main() */
2532 shader
->UnresolvedRefs
= A
.UnresolvedRefs
;
2535 _slang_pop_var_table(o
.vartable
);
2536 _slang_delete_var_table(o
.vartable
);
2542 compile_binary(const byte
* prod
, slang_code_unit
* unit
,
2544 slang_unit_type type
, slang_info_log
* infolog
,
2545 slang_code_unit
* builtin
, slang_code_unit
* downlink
,
2546 struct gl_shader
*shader
)
2552 /* setup parse context */
2555 C
.parsing_builtin
= (builtin
== NULL
);
2556 C
.global_scope
= GL_TRUE
;
2557 C
.atoms
= &unit
->object
->atompool
;
2559 C
.version
= version
;
2561 if (!check_revision(&C
))
2564 if (downlink
!= NULL
) {
2565 unit
->vars
.outer_scope
= &downlink
->vars
;
2566 unit
->funs
.outer_scope
= &downlink
->funs
;
2567 unit
->structs
.outer_scope
= &downlink
->structs
;
2570 /* parse translation unit */
2571 return parse_code_unit(&C
, unit
, shader
);
2575 compile_with_grammar(grammar id
, const char *source
, slang_code_unit
* unit
,
2576 slang_unit_type type
, slang_info_log
* infolog
,
2577 slang_code_unit
* builtin
,
2578 struct gl_shader
*shader
,
2579 const struct gl_extensions
*extensions
,
2580 struct gl_sl_pragmas
*pragmas
)
2583 GLuint size
, version
;
2586 #if FEATURE_ARB_shading_language_120
2588 #elif FEATURE_es2_glsl
2594 /* First retrieve the version number. */
2597 if (version
> maxVersion
) {
2598 slang_info_log_error(infolog
,
2599 "language version %.2f is not supported.",
2604 /* Finally check the syntax and generate its binary representation. */
2605 if (!grammar_fast_check(id
,
2606 (const byte
*)source
,
2607 &prod
, &size
, 65536)) {
2611 grammar_get_last_error((byte
*) (buf
), sizeof(buf
), &pos
);
2612 slang_info_log_error(infolog
, buf
);
2613 /* syntax error (possibly in library code) */
2618 s
= (char *) _mesa_find_line_column((const GLubyte
*) source
,
2619 (const GLubyte
*) source
+ pos
,
2621 printf("Error on line %d, col %d: %s\n", line
, col
, s
);
2627 /* Syntax is okay - translate it to internal representation. */
2628 if (!compile_binary(prod
, unit
, version
, type
, infolog
, builtin
,
2629 &builtin
[SLANG_BUILTIN_TOTAL
- 1],
2631 grammar_alloc_free(prod
);
2634 grammar_alloc_free(prod
);
2638 LONGSTRING
static const char *slang_shader_syn
=
2639 #include "library/slang_shader_syn.h"
2642 static const byte slang_core_gc
[] = {
2643 #include "library/slang_core_gc.h"
2646 static const byte slang_120_core_gc
[] = {
2647 #include "library/slang_120_core_gc.h"
2650 static const byte slang_120_fragment_gc
[] = {
2651 #include "library/slang_builtin_120_fragment_gc.h"
2654 static const byte slang_common_builtin_gc
[] = {
2655 #include "library/slang_common_builtin_gc.h"
2658 static const byte slang_fragment_builtin_gc
[] = {
2659 #include "library/slang_fragment_builtin_gc.h"
2662 static const byte slang_vertex_builtin_gc
[] = {
2663 #include "library/slang_vertex_builtin_gc.h"
2667 compile_object(grammar
* id
, const char *source
, slang_code_object
* object
,
2668 slang_unit_type type
, slang_info_log
* infolog
,
2669 struct gl_shader
*shader
,
2670 const struct gl_extensions
*extensions
,
2671 struct gl_sl_pragmas
*pragmas
)
2673 slang_code_unit
*builtins
= NULL
;
2674 GLuint base_version
= 110;
2676 /* load GLSL grammar */
2677 *id
= grammar_load_from_text((const byte
*) (slang_shader_syn
));
2682 grammar_get_last_error(buf
, 1024, &pos
);
2683 slang_info_log_error(infolog
, (const char *) (buf
));
2687 /* set shader type - the syntax is slightly different for different shaders */
2688 if (type
== SLANG_UNIT_FRAGMENT_SHADER
2689 || type
== SLANG_UNIT_FRAGMENT_BUILTIN
)
2690 grammar_set_reg8(*id
, (const byte
*) "shader_type", 1);
2692 grammar_set_reg8(*id
, (const byte
*) "shader_type", 2);
2694 /* enable language extensions */
2695 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2697 /* if parsing user-specified shader, load built-in library */
2698 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_VERTEX_SHADER
) {
2699 /* compile core functionality first */
2700 if (!compile_binary(slang_core_gc
,
2701 &object
->builtin
[SLANG_BUILTIN_CORE
],
2703 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2707 #if FEATURE_ARB_shading_language_120
2708 if (!compile_binary(slang_120_core_gc
,
2709 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2711 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2712 NULL
, &object
->builtin
[SLANG_BUILTIN_CORE
], NULL
))
2716 /* compile common functions and variables, link to core */
2717 if (!compile_binary(slang_common_builtin_gc
,
2718 &object
->builtin
[SLANG_BUILTIN_COMMON
],
2719 #if FEATURE_ARB_shading_language_120
2724 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2725 #if FEATURE_ARB_shading_language_120
2726 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2728 &object
->builtin
[SLANG_BUILTIN_CORE
],
2733 /* compile target-specific functions and variables, link to common */
2734 if (type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2735 if (!compile_binary(slang_fragment_builtin_gc
,
2736 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2738 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2739 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2741 #if FEATURE_ARB_shading_language_120
2742 if (!compile_binary(slang_120_fragment_gc
,
2743 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2745 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2746 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2750 else if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2751 if (!compile_binary(slang_vertex_builtin_gc
,
2752 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2754 SLANG_UNIT_VERTEX_BUILTIN
, infolog
, NULL
,
2755 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2759 /* disable language extensions */
2760 #if NEW_SLANG /* allow-built-ins */
2761 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2763 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 0);
2765 builtins
= object
->builtin
;
2768 /* compile the actual shader - pass-in built-in library for external shader */
2769 return compile_with_grammar(*id
, source
, &object
->unit
, type
, infolog
,
2770 builtins
, shader
, extensions
, pragmas
);
2775 compile_shader(GLcontext
*ctx
, slang_code_object
* object
,
2776 slang_unit_type type
, slang_info_log
* infolog
,
2777 struct gl_shader
*shader
)
2782 #if 0 /* for debug */
2783 _mesa_printf("********* COMPILE SHADER ***********\n");
2784 _mesa_printf("%s\n", shader
->Source
);
2785 _mesa_printf("************************************\n");
2788 assert(shader
->Program
);
2790 _slang_code_object_dtr(object
);
2791 _slang_code_object_ctr(object
);
2793 success
= compile_object(&id
, shader
->Source
, object
, type
, infolog
, shader
,
2794 &ctx
->Extensions
, &shader
->Pragmas
);
2796 grammar_destroy(id
);
2806 _slang_compile(GLcontext
*ctx
, struct gl_shader
*shader
)
2809 slang_info_log info_log
;
2810 slang_code_object obj
;
2811 slang_unit_type type
;
2813 if (shader
->Type
== GL_VERTEX_SHADER
) {
2814 type
= SLANG_UNIT_VERTEX_SHADER
;
2817 assert(shader
->Type
== GL_FRAGMENT_SHADER
);
2818 type
= SLANG_UNIT_FRAGMENT_SHADER
;
2821 if (!shader
->Source
)
2824 ctx
->Shader
.MemPool
= _slang_new_mempool(1024*1024);
2826 shader
->Main
= GL_FALSE
;
2828 if (!shader
->Program
) {
2830 if (shader
->Type
== GL_VERTEX_SHADER
)
2831 progTarget
= GL_VERTEX_PROGRAM_ARB
;
2833 progTarget
= GL_FRAGMENT_PROGRAM_ARB
;
2834 shader
->Program
= ctx
->Driver
.NewProgram(ctx
, progTarget
, 1);
2835 shader
->Program
->Parameters
= _mesa_new_parameter_list();
2836 shader
->Program
->Varying
= _mesa_new_parameter_list();
2837 shader
->Program
->Attributes
= _mesa_new_parameter_list();
2840 slang_info_log_construct(&info_log
);
2841 _slang_code_object_ctr(&obj
);
2843 success
= compile_shader(ctx
, &obj
, type
, &info_log
, shader
);
2845 /* free shader's prev info log */
2846 if (shader
->InfoLog
) {
2847 _mesa_free(shader
->InfoLog
);
2848 shader
->InfoLog
= NULL
;
2851 if (info_log
.text
) {
2852 /* copy info-log string to shader object */
2853 shader
->InfoLog
= _mesa_strdup(info_log
.text
);
2856 if (info_log
.error_flag
) {
2860 slang_info_log_destruct(&info_log
);
2861 _slang_code_object_dtr(&obj
);
2863 _slang_delete_mempool((slang_mempool
*) ctx
->Shader
.MemPool
);
2864 ctx
->Shader
.MemPool
= NULL
;
2866 /* remove any reads of output registers */
2868 printf("Pre-remove output reads:\n");
2869 _mesa_print_program(shader
->Program
);
2871 _mesa_remove_output_reads(shader
->Program
, PROGRAM_OUTPUT
);
2872 if (shader
->Type
== GL_VERTEX_SHADER
) {
2873 /* and remove writes to varying vars in vertex programs */
2874 _mesa_remove_output_reads(shader
->Program
, PROGRAM_VARYING
);
2877 printf("Post-remove output reads:\n");
2878 _mesa_print_program(shader
->Program
);
2881 shader
->CompileStatus
= success
;
2884 if (shader
->Pragmas
.Optimize
&&
2885 (ctx
->Shader
.Flags
& GLSL_NO_OPT
) == 0) {
2886 _mesa_optimize_program(ctx
, shader
->Program
);
2890 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
2891 _mesa_write_shader_to_file(shader
);