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_print.h"
36 #include "shader/prog_parameter.h"
37 #include "shader/grammar/grammar_mesa.h"
38 #include "slang_codegen.h"
39 #include "slang_compile.h"
40 #include "slang_preprocess.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
);
186 parse_number(slang_parse_ctx
* C
, int *number
)
188 const int radix
= (int) (*C
->I
++);
190 while (*C
->I
!= '\0') {
192 if (*C
->I
>= '0' && *C
->I
<= '9')
193 digit
= (int) (*C
->I
- '0');
194 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
195 digit
= (int) (*C
->I
- 'A') + 10;
197 digit
= (int) (*C
->I
- 'a') + 10;
198 *number
= *number
* radix
+ digit
;
203 slang_info_log_warning(C
->L
, "%d: literal integer overflow.", *number
);
208 parse_float(slang_parse_ctx
* C
, float *number
)
210 char *integral
= NULL
;
211 char *fractional
= NULL
;
212 char *exponent
= NULL
;
215 parse_identifier_str(C
, &integral
);
216 parse_identifier_str(C
, &fractional
);
217 parse_identifier_str(C
, &exponent
);
219 whole
= (char *) _slang_alloc((_mesa_strlen(integral
) +
220 _mesa_strlen(fractional
) +
221 _mesa_strlen(exponent
) + 3) * sizeof(char));
223 slang_info_log_memory(C
->L
);
227 slang_string_copy(whole
, integral
);
228 slang_string_concat(whole
, ".");
229 slang_string_concat(whole
, fractional
);
230 slang_string_concat(whole
, "E");
231 slang_string_concat(whole
, exponent
);
233 *number
= (float) (_mesa_strtod(whole
, (char **) NULL
));
240 /* revision number - increment after each change affecting emitted output */
244 check_revision(slang_parse_ctx
* C
)
246 if (*C
->I
!= REVISION
) {
247 slang_info_log_error(C
->L
, "Internal compiler error.");
254 static int parse_statement(slang_parse_ctx
*, slang_output_ctx
*,
256 static int parse_expression(slang_parse_ctx
*, slang_output_ctx
*,
258 static int parse_type_specifier(slang_parse_ctx
*, slang_output_ctx
*,
259 slang_type_specifier
*);
261 parse_type_array_size(slang_parse_ctx
*C
,
266 parse_array_len(slang_parse_ctx
* C
, slang_output_ctx
* O
, GLuint
* len
)
268 slang_operation array_size
;
269 slang_name_space space
;
272 if (!slang_operation_construct(&array_size
))
274 if (!parse_expression(C
, O
, &array_size
)) {
275 slang_operation_destruct(&array_size
);
279 space
.funcs
= O
->funs
;
280 space
.structs
= O
->structs
;
281 space
.vars
= O
->vars
;
283 /* evaluate compile-time expression which is array size */
284 _slang_simplify(&array_size
, &space
, C
->atoms
);
286 if (array_size
.type
== SLANG_OPER_LITERAL_INT
) {
288 *len
= (GLint
) array_size
.literal
[0];
289 } else if (array_size
.type
== SLANG_OPER_IDENTIFIER
) {
290 slang_variable
*var
= _slang_variable_locate(array_size
.locals
, array_size
.a_id
, GL_TRUE
);
292 slang_info_log_error(C
->L
, "undefined variable '%s'",
293 (char *) array_size
.a_id
);
295 } else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&&
296 var
->type
.specifier
.type
== SLANG_SPEC_INT
) {
297 if (var
->initializer
&&
298 var
->initializer
->type
== SLANG_OPER_LITERAL_INT
) {
299 *len
= (GLint
) var
->initializer
->literal
[0];
302 slang_info_log_error(C
->L
, "unable to parse array size declaration");
306 slang_info_log_error(C
->L
, "unable to parse array size declaration");
313 slang_operation_destruct(&array_size
);
318 calculate_var_size(slang_parse_ctx
* C
, slang_output_ctx
* O
,
319 slang_variable
* var
)
321 slang_storage_aggregate agg
;
323 if (!slang_storage_aggregate_construct(&agg
))
325 if (!_slang_aggregate_variable(&agg
, &var
->type
.specifier
, var
->array_len
,
326 O
->funs
, O
->structs
, O
->vars
, C
->atoms
)) {
327 slang_storage_aggregate_destruct(&agg
);
330 var
->size
= _slang_sizeof_aggregate(&agg
);
331 slang_storage_aggregate_destruct(&agg
);
336 promote_type_to_array(slang_parse_ctx
*C
,
337 slang_fully_specified_type
*type
,
340 slang_type_specifier
*baseType
=
341 slang_type_specifier_new(type
->specifier
.type
, NULL
, NULL
);
343 type
->specifier
.type
= SLANG_SPEC_ARRAY
;
344 type
->specifier
._array
= baseType
;
345 type
->array_len
= array_len
;
350 convert_to_array(slang_parse_ctx
* C
, slang_variable
* var
,
351 const slang_type_specifier
* sp
)
353 /* sized array - mark it as array, copy the specifier to the array element
354 * and parse the expression */
355 var
->type
.specifier
.type
= SLANG_SPEC_ARRAY
;
356 var
->type
.specifier
._array
= (slang_type_specifier
*)
357 _slang_alloc(sizeof(slang_type_specifier
));
358 if (var
->type
.specifier
._array
== NULL
) {
359 slang_info_log_memory(C
->L
);
362 slang_type_specifier_ctr(var
->type
.specifier
._array
);
363 return slang_type_specifier_copy(var
->type
.specifier
._array
, sp
);
366 /* structure field */
369 #define FIELD_ARRAY 2
372 parse_struct_field_var(slang_parse_ctx
* C
, slang_output_ctx
* O
,
373 slang_variable
* var
, slang_atom a_name
,
374 const slang_type_specifier
* sp
,
377 var
->a_name
= a_name
;
378 if (var
->a_name
== SLANG_ATOM_NULL
)
383 if (array_len
!= -1) {
384 if (!convert_to_array(C
, var
, sp
))
386 var
->array_len
= array_len
;
389 if (!slang_type_specifier_copy(&var
->type
.specifier
, sp
))
396 if (!convert_to_array(C
, var
, sp
))
398 if (!parse_array_len(C
, O
, &var
->array_len
))
405 return calculate_var_size(C
, O
, var
);
409 parse_struct_field(slang_parse_ctx
* C
, slang_output_ctx
* O
,
410 slang_struct
* st
, slang_type_specifier
* sp
)
412 slang_output_ctx o
= *O
;
415 o
.structs
= st
->structs
;
416 if (!parse_type_specifier(C
, &o
, sp
))
418 if (!parse_type_array_size(C
, &o
, &array_len
))
423 slang_variable
*var
= slang_variable_scope_grow(st
->fields
);
425 slang_info_log_memory(C
->L
);
428 a_name
= parse_identifier(C
);
429 if (_slang_variable_locate(st
->fields
, a_name
, GL_FALSE
)) {
430 slang_info_log_error(C
->L
, "duplicate field '%s'", (char *) a_name
);
434 if (!parse_struct_field_var(C
, &o
, var
, a_name
, sp
, array_len
))
437 while (*C
->I
++ != FIELD_NONE
);
443 parse_struct(slang_parse_ctx
* C
, slang_output_ctx
* O
, slang_struct
** st
)
448 /* parse struct name (if any) and make sure it is unique in current scope */
449 a_name
= parse_identifier(C
);
450 if (a_name
== SLANG_ATOM_NULL
)
453 name
= slang_atom_pool_id(C
->atoms
, a_name
);
455 && slang_struct_scope_find(O
->structs
, a_name
, 0) != NULL
) {
456 slang_info_log_error(C
->L
, "%s: duplicate type name.", name
);
460 /* set-up a new struct */
461 *st
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
463 slang_info_log_memory(C
->L
);
466 if (!slang_struct_construct(*st
)) {
469 slang_info_log_memory(C
->L
);
472 (**st
).a_name
= a_name
;
473 (**st
).structs
->outer_scope
= O
->structs
;
475 /* parse individual struct fields */
477 slang_type_specifier sp
;
479 slang_type_specifier_ctr(&sp
);
480 if (!parse_struct_field(C
, O
, *st
, &sp
)) {
481 slang_type_specifier_dtr(&sp
);
484 slang_type_specifier_dtr(&sp
);
486 while (*C
->I
++ != FIELD_NONE
);
488 /* if named struct, copy it to current scope */
489 if (name
[0] != '\0') {
492 O
->structs
->structs
=
493 (slang_struct
*) _slang_realloc(O
->structs
->structs
,
494 O
->structs
->num_structs
495 * sizeof(slang_struct
),
496 (O
->structs
->num_structs
+ 1)
497 * sizeof(slang_struct
));
498 if (O
->structs
->structs
== NULL
) {
499 slang_info_log_memory(C
->L
);
502 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
503 if (!slang_struct_construct(s
))
505 O
->structs
->num_structs
++;
506 if (!slang_struct_copy(s
, *st
))
514 /* invariant qualifer */
515 #define TYPE_VARIANT 90
516 #define TYPE_INVARIANT 91
519 parse_type_variant(slang_parse_ctx
* C
, slang_type_variant
*variant
)
521 GLuint invariant
= *C
->I
++;
524 *variant
= SLANG_VARIANT
;
527 *variant
= SLANG_INVARIANT
;
535 /* centroid qualifer */
536 #define TYPE_CENTER 95
537 #define TYPE_CENTROID 96
540 parse_type_centroid(slang_parse_ctx
* C
, slang_type_centroid
*centroid
)
545 *centroid
= SLANG_CENTER
;
548 *centroid
= SLANG_CENTROID
;
557 #define TYPE_QUALIFIER_NONE 0
558 #define TYPE_QUALIFIER_CONST 1
559 #define TYPE_QUALIFIER_ATTRIBUTE 2
560 #define TYPE_QUALIFIER_VARYING 3
561 #define TYPE_QUALIFIER_UNIFORM 4
562 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
563 #define TYPE_QUALIFIER_FIXEDINPUT 6
566 parse_type_qualifier(slang_parse_ctx
* C
, slang_type_qualifier
* qual
)
568 GLuint qualifier
= *C
->I
++;
570 case TYPE_QUALIFIER_NONE
:
571 *qual
= SLANG_QUAL_NONE
;
573 case TYPE_QUALIFIER_CONST
:
574 *qual
= SLANG_QUAL_CONST
;
576 case TYPE_QUALIFIER_ATTRIBUTE
:
577 *qual
= SLANG_QUAL_ATTRIBUTE
;
579 case TYPE_QUALIFIER_VARYING
:
580 *qual
= SLANG_QUAL_VARYING
;
582 case TYPE_QUALIFIER_UNIFORM
:
583 *qual
= SLANG_QUAL_UNIFORM
;
585 case TYPE_QUALIFIER_FIXEDOUTPUT
:
586 *qual
= SLANG_QUAL_FIXEDOUTPUT
;
588 case TYPE_QUALIFIER_FIXEDINPUT
:
589 *qual
= SLANG_QUAL_FIXEDINPUT
;
598 #define TYPE_SPECIFIER_VOID 0
599 #define TYPE_SPECIFIER_BOOL 1
600 #define TYPE_SPECIFIER_BVEC2 2
601 #define TYPE_SPECIFIER_BVEC3 3
602 #define TYPE_SPECIFIER_BVEC4 4
603 #define TYPE_SPECIFIER_INT 5
604 #define TYPE_SPECIFIER_IVEC2 6
605 #define TYPE_SPECIFIER_IVEC3 7
606 #define TYPE_SPECIFIER_IVEC4 8
607 #define TYPE_SPECIFIER_FLOAT 9
608 #define TYPE_SPECIFIER_VEC2 10
609 #define TYPE_SPECIFIER_VEC3 11
610 #define TYPE_SPECIFIER_VEC4 12
611 #define TYPE_SPECIFIER_MAT2 13
612 #define TYPE_SPECIFIER_MAT3 14
613 #define TYPE_SPECIFIER_MAT4 15
614 #define TYPE_SPECIFIER_SAMPLER1D 16
615 #define TYPE_SPECIFIER_SAMPLER2D 17
616 #define TYPE_SPECIFIER_SAMPLER3D 18
617 #define TYPE_SPECIFIER_SAMPLERCUBE 19
618 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
619 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
620 #define TYPE_SPECIFIER_SAMPLER2DRECT 22
621 #define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
622 #define TYPE_SPECIFIER_STRUCT 24
623 #define TYPE_SPECIFIER_TYPENAME 25
624 #define TYPE_SPECIFIER_MAT23 26
625 #define TYPE_SPECIFIER_MAT32 27
626 #define TYPE_SPECIFIER_MAT24 28
627 #define TYPE_SPECIFIER_MAT42 29
628 #define TYPE_SPECIFIER_MAT34 30
629 #define TYPE_SPECIFIER_MAT43 31
630 #define TYPE_SPECIFIER_COUNT 32
633 parse_type_specifier(slang_parse_ctx
* C
, slang_output_ctx
* O
,
634 slang_type_specifier
* spec
)
637 case TYPE_SPECIFIER_VOID
:
638 spec
->type
= SLANG_SPEC_VOID
;
640 case TYPE_SPECIFIER_BOOL
:
641 spec
->type
= SLANG_SPEC_BOOL
;
643 case TYPE_SPECIFIER_BVEC2
:
644 spec
->type
= SLANG_SPEC_BVEC2
;
646 case TYPE_SPECIFIER_BVEC3
:
647 spec
->type
= SLANG_SPEC_BVEC3
;
649 case TYPE_SPECIFIER_BVEC4
:
650 spec
->type
= SLANG_SPEC_BVEC4
;
652 case TYPE_SPECIFIER_INT
:
653 spec
->type
= SLANG_SPEC_INT
;
655 case TYPE_SPECIFIER_IVEC2
:
656 spec
->type
= SLANG_SPEC_IVEC2
;
658 case TYPE_SPECIFIER_IVEC3
:
659 spec
->type
= SLANG_SPEC_IVEC3
;
661 case TYPE_SPECIFIER_IVEC4
:
662 spec
->type
= SLANG_SPEC_IVEC4
;
664 case TYPE_SPECIFIER_FLOAT
:
665 spec
->type
= SLANG_SPEC_FLOAT
;
667 case TYPE_SPECIFIER_VEC2
:
668 spec
->type
= SLANG_SPEC_VEC2
;
670 case TYPE_SPECIFIER_VEC3
:
671 spec
->type
= SLANG_SPEC_VEC3
;
673 case TYPE_SPECIFIER_VEC4
:
674 spec
->type
= SLANG_SPEC_VEC4
;
676 case TYPE_SPECIFIER_MAT2
:
677 spec
->type
= SLANG_SPEC_MAT2
;
679 case TYPE_SPECIFIER_MAT3
:
680 spec
->type
= SLANG_SPEC_MAT3
;
682 case TYPE_SPECIFIER_MAT4
:
683 spec
->type
= SLANG_SPEC_MAT4
;
685 case TYPE_SPECIFIER_MAT23
:
686 spec
->type
= SLANG_SPEC_MAT23
;
688 case TYPE_SPECIFIER_MAT32
:
689 spec
->type
= SLANG_SPEC_MAT32
;
691 case TYPE_SPECIFIER_MAT24
:
692 spec
->type
= SLANG_SPEC_MAT24
;
694 case TYPE_SPECIFIER_MAT42
:
695 spec
->type
= SLANG_SPEC_MAT42
;
697 case TYPE_SPECIFIER_MAT34
:
698 spec
->type
= SLANG_SPEC_MAT34
;
700 case TYPE_SPECIFIER_MAT43
:
701 spec
->type
= SLANG_SPEC_MAT43
;
703 case TYPE_SPECIFIER_SAMPLER1D
:
704 spec
->type
= SLANG_SPEC_SAMPLER1D
;
706 case TYPE_SPECIFIER_SAMPLER2D
:
707 spec
->type
= SLANG_SPEC_SAMPLER2D
;
709 case TYPE_SPECIFIER_SAMPLER3D
:
710 spec
->type
= SLANG_SPEC_SAMPLER3D
;
712 case TYPE_SPECIFIER_SAMPLERCUBE
:
713 spec
->type
= SLANG_SPEC_SAMPLERCUBE
;
715 case TYPE_SPECIFIER_SAMPLER2DRECT
:
716 spec
->type
= SLANG_SPEC_SAMPLER2DRECT
;
718 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
719 spec
->type
= SLANG_SPEC_SAMPLER1DSHADOW
;
721 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
722 spec
->type
= SLANG_SPEC_SAMPLER2DSHADOW
;
724 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
725 spec
->type
= SLANG_SPEC_SAMPLER2DRECTSHADOW
;
727 case TYPE_SPECIFIER_STRUCT
:
728 spec
->type
= SLANG_SPEC_STRUCT
;
729 if (!parse_struct(C
, O
, &spec
->_struct
))
732 case TYPE_SPECIFIER_TYPENAME
:
733 spec
->type
= SLANG_SPEC_STRUCT
;
738 a_name
= parse_identifier(C
);
742 stru
= slang_struct_scope_find(O
->structs
, a_name
, 1);
744 slang_info_log_error(C
->L
, "undeclared type name '%s'",
745 slang_atom_pool_id(C
->atoms
, a_name
));
749 spec
->_struct
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
750 if (spec
->_struct
== NULL
) {
751 slang_info_log_memory(C
->L
);
754 if (!slang_struct_construct(spec
->_struct
)) {
755 _slang_free(spec
->_struct
);
756 spec
->_struct
= NULL
;
759 if (!slang_struct_copy(spec
->_struct
, stru
))
769 #define TYPE_SPECIFIER_NONARRAY 0
770 #define TYPE_SPECIFIER_ARRAY 1
773 parse_type_array_size(slang_parse_ctx
*C
,
780 case TYPE_SPECIFIER_NONARRAY
:
781 *array_len
= -1; /* -1 = not an array */
783 case TYPE_SPECIFIER_ARRAY
:
784 if (!parse_array_len(C
, O
, &size
))
786 *array_len
= (GLint
) size
;
795 #define PRECISION_DEFAULT 0
796 #define PRECISION_LOW 1
797 #define PRECISION_MEDIUM 2
798 #define PRECISION_HIGH 3
801 parse_type_precision(slang_parse_ctx
*C
,
802 slang_type_precision
*precision
)
804 GLint prec
= *C
->I
++;
806 case PRECISION_DEFAULT
:
807 *precision
= SLANG_PREC_DEFAULT
;
810 *precision
= SLANG_PREC_LOW
;
812 case PRECISION_MEDIUM
:
813 *precision
= SLANG_PREC_MEDIUM
;
816 *precision
= SLANG_PREC_HIGH
;
824 parse_fully_specified_type(slang_parse_ctx
* C
, slang_output_ctx
* O
,
825 slang_fully_specified_type
* type
)
827 if (!parse_type_variant(C
, &type
->variant
))
830 if (!parse_type_centroid(C
, &type
->centroid
))
833 if (!parse_type_qualifier(C
, &type
->qualifier
))
836 if (!parse_type_precision(C
, &type
->precision
))
839 if (!parse_type_specifier(C
, O
, &type
->specifier
))
842 if (!parse_type_array_size(C
, O
, &type
->array_len
))
845 if (!O
->allow_invariant
&& type
->variant
== SLANG_INVARIANT
) {
846 slang_info_log_error(C
->L
,
847 "'invariant' keyword not allowed (perhaps set #version 120)");
851 if (!O
->allow_centroid
&& type
->centroid
== SLANG_CENTROID
) {
852 slang_info_log_error(C
->L
,
853 "'centroid' keyword not allowed (perhaps set #version 120)");
856 else if (type
->centroid
== SLANG_CENTROID
&&
857 type
->qualifier
!= SLANG_QUAL_VARYING
) {
858 slang_info_log_error(C
->L
,
859 "'centroid' keyword only allowed for varying vars");
865 if (type->qualifier != SLANG_QUAL_VARYING &&
866 type->variant == SLANG_INVARIANT) {
867 slang_info_log_error(C->L,
868 "invariant qualifer only allowed for varying vars");
873 if (O
->allow_precision
) {
874 if (type
->precision
== SLANG_PREC_DEFAULT
) {
875 assert(type
->specifier
.type
< TYPE_SPECIFIER_COUNT
);
876 /* use the default precision for this datatype */
877 type
->precision
= O
->default_precision
[type
->specifier
.type
];
881 /* only default is allowed */
882 if (type
->precision
!= SLANG_PREC_DEFAULT
) {
883 slang_info_log_error(C
->L
, "precision qualifiers not allowed");
888 if (!O
->allow_array_types
&& type
->array_len
>= 0) {
889 slang_info_log_error(C
->L
, "first-class array types not allowed");
893 if (type
->array_len
>= 0) {
894 /* convert type to array type (ex: convert "int" to "array of int" */
895 promote_type_to_array(C
, type
, type
->array_len
);
903 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
904 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
908 #define OP_CONTINUE 6
911 #define OP_EXPRESSION 9
916 #define OP_PUSH_VOID 14
917 #define OP_PUSH_BOOL 15
918 #define OP_PUSH_INT 16
919 #define OP_PUSH_FLOAT 17
920 #define OP_PUSH_IDENTIFIER 18
921 #define OP_SEQUENCE 19
923 #define OP_ADDASSIGN 21
924 #define OP_SUBASSIGN 22
925 #define OP_MULASSIGN 23
926 #define OP_DIVASSIGN 24
927 /*#define OP_MODASSIGN 25*/
928 /*#define OP_LSHASSIGN 26*/
929 /*#define OP_RSHASSIGN 27*/
930 /*#define OP_ORASSIGN 28*/
931 /*#define OP_XORASSIGN 29*/
932 /*#define OP_ANDASSIGN 30*/
934 #define OP_LOGICALOR 32
935 #define OP_LOGICALXOR 33
936 #define OP_LOGICALAND 34
937 /*#define OP_BITOR 35*/
938 /*#define OP_BITXOR 36*/
939 /*#define OP_BITAND 37*/
941 #define OP_NOTEQUAL 39
943 #define OP_GREATER 41
944 #define OP_LESSEQUAL 42
945 #define OP_GREATEREQUAL 43
946 /*#define OP_LSHIFT 44*/
947 /*#define OP_RSHIFT 45*/
949 #define OP_SUBTRACT 47
950 #define OP_MULTIPLY 48
952 /*#define OP_MODULUS 50*/
953 #define OP_PREINCREMENT 51
954 #define OP_PREDECREMENT 52
957 /*#define OP_COMPLEMENT 55*/
959 #define OP_SUBSCRIPT 57
962 #define OP_POSTINCREMENT 60
963 #define OP_POSTDECREMENT 61
964 #define OP_PRECISION 62
969 * When parsing a compound production, this function is used to parse the
971 * For example, a while-loop compound will have two children, the
972 * while condition expression and the loop body. So, this function will
973 * be called twice to parse those two sub-expressions.
974 * \param C the parsing context
975 * \param O the output context
976 * \param oper the operation we're parsing
977 * \param statement indicates whether parsing a statement, or expression
978 * \return 1 if success, 0 if error
981 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
982 slang_operation
* oper
, GLboolean statement
)
986 /* grow child array */
987 ch
= slang_operation_grow(&oper
->num_children
, &oper
->children
);
989 return parse_statement(C
, O
, ch
);
990 return parse_expression(C
, O
, ch
);
993 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
996 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
997 slang_operation
* oper
)
1001 oper
->locals
->outer_scope
= O
->vars
;
1005 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
1006 /* parse child statements, do not create new variable scope */
1007 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1008 while (*C
->I
!= OP_END
)
1009 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1013 case OP_BLOCK_BEGIN_NEW_SCOPE
:
1014 /* parse child statements, create new variable scope */
1016 slang_output_ctx o
= *O
;
1018 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1019 o
.vars
= oper
->locals
;
1020 while (*C
->I
!= OP_END
)
1021 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1027 /* local variable declaration, individual declarators are stored as
1028 * children identifiers
1030 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1032 const unsigned int first_var
= O
->vars
->num_variables
;
1034 /* parse the declaration, note that there can be zero or more
1035 * than one declarators
1037 if (!parse_declaration(C
, O
))
1039 if (first_var
< O
->vars
->num_variables
) {
1040 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
1042 assert(oper
->num_children
== 0);
1043 oper
->num_children
= num_vars
;
1044 oper
->children
= slang_operation_new(num_vars
);
1045 if (oper
->children
== NULL
) {
1046 slang_info_log_memory(C
->L
);
1049 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
1050 slang_operation
*o
= &oper
->children
[i
- first_var
];
1051 slang_variable
*var
= O
->vars
->variables
[i
];
1052 o
->type
= SLANG_OPER_VARIABLE_DECL
;
1053 o
->locals
->outer_scope
= O
->vars
;
1054 o
->a_id
= var
->a_name
;
1057 calculate_var_size(C, O, var);
1060 if (!legal_identifier(o
->a_id
)) {
1061 slang_info_log_error(C
->L
, "illegal variable name '%s'",
1070 /* the __asm statement, parse the mnemonic and all its arguments
1073 oper
->type
= SLANG_OPER_ASM
;
1074 oper
->a_id
= parse_identifier(C
);
1075 if (oper
->a_id
== SLANG_ATOM_NULL
)
1077 while (*C
->I
!= OP_END
) {
1078 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1084 oper
->type
= SLANG_OPER_BREAK
;
1087 oper
->type
= SLANG_OPER_CONTINUE
;
1090 oper
->type
= SLANG_OPER_DISCARD
;
1093 oper
->type
= SLANG_OPER_RETURN
;
1094 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1098 oper
->type
= SLANG_OPER_EXPRESSION
;
1099 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1103 oper
->type
= SLANG_OPER_IF
;
1104 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1106 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1108 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1113 slang_output_ctx o
= *O
;
1115 oper
->type
= SLANG_OPER_WHILE
;
1116 o
.vars
= oper
->locals
;
1117 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1119 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1124 oper
->type
= SLANG_OPER_DO
;
1125 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1127 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1132 slang_output_ctx o
= *O
;
1134 oper
->type
= SLANG_OPER_FOR
;
1135 o
.vars
= oper
->locals
;
1136 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1138 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1140 if (!parse_child_operation(C
, &o
, oper
, GL_FALSE
))
1142 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1148 /* set default precision for a type in this scope */
1149 /* ignored at this time */
1150 int prec_qual
= *C
->I
++;
1151 int datatype
= *C
->I
++;
1157 /*printf("Unexpected operation %d\n", op);*/
1164 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
1165 slang_operation
** ops
, unsigned int *total_ops
,
1170 op
->children
= slang_operation_new(n
);
1171 if (op
->children
== NULL
) {
1172 slang_info_log_memory(C
->L
);
1175 op
->num_children
= n
;
1177 for (i
= 0; i
< n
; i
++) {
1178 slang_operation_destruct(&op
->children
[i
]);
1179 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
1182 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
1185 *ops
= (slang_operation
*)
1186 _slang_realloc(*ops
,
1187 (*total_ops
+ n
) * sizeof(slang_operation
),
1188 *total_ops
* sizeof(slang_operation
));
1190 slang_info_log_memory(C
->L
);
1197 is_constructor_name(const char *name
, slang_atom a_name
,
1198 slang_struct_scope
* structs
)
1200 if (slang_type_specifier_type_from_string(name
) != SLANG_SPEC_VOID
)
1202 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
1205 #define FUNCTION_CALL_NONARRAY 0
1206 #define FUNCTION_CALL_ARRAY 1
1209 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1210 slang_operation
* oper
)
1212 slang_operation
*ops
= NULL
;
1213 unsigned int num_ops
= 0;
1216 while (*C
->I
!= OP_END
) {
1217 slang_operation
*op
;
1218 const unsigned int op_code
= *C
->I
++;
1220 /* allocate default operation, becomes a no-op if not used */
1221 ops
= (slang_operation
*)
1223 num_ops
* sizeof(slang_operation
),
1224 (num_ops
+ 1) * sizeof(slang_operation
));
1226 slang_info_log_memory(C
->L
);
1230 if (!slang_operation_construct(op
)) {
1231 slang_info_log_memory(C
->L
);
1235 op
->locals
->outer_scope
= O
->vars
;
1239 op
->type
= SLANG_OPER_VOID
;
1242 op
->type
= SLANG_OPER_LITERAL_BOOL
;
1243 if (!parse_number(C
, &number
))
1248 op
->literal
[3] = (GLfloat
) number
;
1249 op
->literal_size
= 1;
1252 op
->type
= SLANG_OPER_LITERAL_INT
;
1253 if (!parse_number(C
, &number
))
1258 op
->literal
[3] = (GLfloat
) number
;
1259 op
->literal_size
= 1;
1262 op
->type
= SLANG_OPER_LITERAL_FLOAT
;
1263 if (!parse_float(C
, &op
->literal
[0]))
1267 op
->literal
[3] = op
->literal
[0];
1268 op
->literal_size
= 1;
1270 case OP_PUSH_IDENTIFIER
:
1271 op
->type
= SLANG_OPER_IDENTIFIER
;
1272 op
->a_id
= parse_identifier(C
);
1273 if (op
->a_id
== SLANG_ATOM_NULL
)
1277 op
->type
= SLANG_OPER_SEQUENCE
;
1278 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1282 op
->type
= SLANG_OPER_ASSIGN
;
1283 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1287 op
->type
= SLANG_OPER_ADDASSIGN
;
1288 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1292 op
->type
= SLANG_OPER_SUBASSIGN
;
1293 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1297 op
->type
= SLANG_OPER_MULASSIGN
;
1298 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1302 op
->type
= SLANG_OPER_DIVASSIGN
;
1303 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1306 /*case OP_MODASSIGN: */
1307 /*case OP_LSHASSIGN: */
1308 /*case OP_RSHASSIGN: */
1309 /*case OP_ORASSIGN: */
1310 /*case OP_XORASSIGN: */
1311 /*case OP_ANDASSIGN: */
1313 op
->type
= SLANG_OPER_SELECT
;
1314 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1318 op
->type
= SLANG_OPER_LOGICALOR
;
1319 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1323 op
->type
= SLANG_OPER_LOGICALXOR
;
1324 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1328 op
->type
= SLANG_OPER_LOGICALAND
;
1329 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1333 /*case OP_BITXOR: */
1334 /*case OP_BITAND: */
1336 op
->type
= SLANG_OPER_EQUAL
;
1337 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1341 op
->type
= SLANG_OPER_NOTEQUAL
;
1342 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1346 op
->type
= SLANG_OPER_LESS
;
1347 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1351 op
->type
= SLANG_OPER_GREATER
;
1352 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1356 op
->type
= SLANG_OPER_LESSEQUAL
;
1357 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1360 case OP_GREATEREQUAL
:
1361 op
->type
= SLANG_OPER_GREATEREQUAL
;
1362 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1365 /*case OP_LSHIFT: */
1366 /*case OP_RSHIFT: */
1368 op
->type
= SLANG_OPER_ADD
;
1369 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1373 op
->type
= SLANG_OPER_SUBTRACT
;
1374 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1378 op
->type
= SLANG_OPER_MULTIPLY
;
1379 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1383 op
->type
= SLANG_OPER_DIVIDE
;
1384 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1387 /*case OP_MODULUS: */
1388 case OP_PREINCREMENT
:
1389 op
->type
= SLANG_OPER_PREINCREMENT
;
1390 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1393 case OP_PREDECREMENT
:
1394 op
->type
= SLANG_OPER_PREDECREMENT
;
1395 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1399 op
->type
= SLANG_OPER_PLUS
;
1400 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1404 op
->type
= SLANG_OPER_MINUS
;
1405 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1409 op
->type
= SLANG_OPER_NOT
;
1410 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1413 /*case OP_COMPLEMENT: */
1415 op
->type
= SLANG_OPER_SUBSCRIPT
;
1416 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1420 op
->type
= SLANG_OPER_METHOD
;
1421 op
->a_obj
= parse_identifier(C
);
1422 if (op
->a_obj
== SLANG_ATOM_NULL
)
1425 op
->a_id
= parse_identifier(C
);
1426 if (op
->a_id
== SLANG_ATOM_NULL
)
1429 assert(*C
->I
== OP_END
);
1432 while (*C
->I
!= OP_END
)
1433 if (!parse_child_operation(C
, O
, op
, GL_FALSE
))
1437 /* don't lookup the method (not yet anyway) */
1438 if (!C
->parsing_builtin
1439 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1442 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1443 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1444 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1452 GLboolean array_constructor
= GL_FALSE
;
1453 GLint array_constructor_size
;
1455 op
->type
= SLANG_OPER_CALL
;
1456 op
->a_id
= parse_identifier(C
);
1457 if (op
->a_id
== SLANG_ATOM_NULL
)
1460 case FUNCTION_CALL_NONARRAY
:
1461 /* Nothing to do. */
1463 case FUNCTION_CALL_ARRAY
:
1464 /* Calling an array constructor. For example:
1465 * float[3](1.1, 2.2, 3.3);
1467 if (!O
->allow_array_types
) {
1468 slang_info_log_error(C
->L
,
1469 "array constructors not allowed "
1470 "in this GLSL version");
1474 slang_operation array_size
;
1475 array_constructor
= GL_TRUE
;
1476 /* parse the array constructor size */
1477 slang_operation_construct(&array_size
);
1478 if (!parse_expression(C
, O
, &array_size
)) {
1479 slang_operation_destruct(&array_size
);
1482 if (array_size
.type
!= SLANG_OPER_LITERAL_INT
) {
1483 slang_info_log_error(C
->L
,
1484 "constructor array size is not an integer");
1485 slang_operation_destruct(&array_size
);
1488 array_constructor_size
= (int) array_size
.literal
[0];
1489 op
->array_constructor
= GL_TRUE
;
1490 slang_operation_destruct(&array_size
);
1497 while (*C
->I
!= OP_END
)
1498 if (!parse_child_operation(C
, O
, op
, GL_FALSE
))
1502 if (array_constructor
&&
1503 array_constructor_size
!= op
->num_children
) {
1504 slang_info_log_error(C
->L
, "number of parameters to array"
1505 " constructor does not match array size");
1509 if (!C
->parsing_builtin
1510 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1513 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1514 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1515 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1522 op
->type
= SLANG_OPER_FIELD
;
1523 op
->a_id
= parse_identifier(C
);
1524 if (op
->a_id
== SLANG_ATOM_NULL
)
1526 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1529 case OP_POSTINCREMENT
:
1530 op
->type
= SLANG_OPER_POSTINCREMENT
;
1531 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1534 case OP_POSTDECREMENT
:
1535 op
->type
= SLANG_OPER_POSTDECREMENT
;
1536 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1545 slang_operation_destruct(oper
);
1546 *oper
= *ops
; /* struct copy */
1552 /* parameter qualifier */
1553 #define PARAM_QUALIFIER_IN 0
1554 #define PARAM_QUALIFIER_OUT 1
1555 #define PARAM_QUALIFIER_INOUT 2
1557 /* function parameter array presence */
1558 #define PARAMETER_ARRAY_NOT_PRESENT 0
1559 #define PARAMETER_ARRAY_PRESENT 1
1562 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1563 slang_variable
* param
)
1565 int param_qual
, precision_qual
;
1567 /* parse and validate the parameter's type qualifiers (there can be
1568 * two at most) because not all combinations are valid
1570 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1573 param_qual
= *C
->I
++;
1574 switch (param_qual
) {
1575 case PARAM_QUALIFIER_IN
:
1576 if (param
->type
.qualifier
!= SLANG_QUAL_CONST
1577 && param
->type
.qualifier
!= SLANG_QUAL_NONE
) {
1578 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1582 case PARAM_QUALIFIER_OUT
:
1583 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1584 param
->type
.qualifier
= SLANG_QUAL_OUT
;
1586 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1590 case PARAM_QUALIFIER_INOUT
:
1591 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1592 param
->type
.qualifier
= SLANG_QUAL_INOUT
;
1594 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1602 /* parse precision qualifier (lowp, mediump, highp */
1603 precision_qual
= *C
->I
++;
1604 /* ignored at this time */
1605 (void) precision_qual
;
1607 /* parse parameter's type specifier and name */
1608 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1610 if (!parse_type_array_size(C
, O
, ¶m
->type
.array_len
))
1612 param
->a_name
= parse_identifier(C
);
1613 if (param
->a_name
== SLANG_ATOM_NULL
)
1616 /* first-class array
1618 if (param
->type
.array_len
>= 0) {
1619 slang_type_specifier p
;
1621 slang_type_specifier_ctr(&p
);
1622 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1623 slang_type_specifier_dtr(&p
);
1626 if (!convert_to_array(C
, param
, &p
)) {
1627 slang_type_specifier_dtr(&p
);
1630 slang_type_specifier_dtr(&p
);
1631 param
->array_len
= param
->type
.array_len
;
1634 /* if the parameter is an array, parse its size (the size must be
1635 * explicitly defined
1637 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1638 slang_type_specifier p
;
1640 if (param
->type
.array_len
>= 0) {
1641 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
1644 slang_type_specifier_ctr(&p
);
1645 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1646 slang_type_specifier_dtr(&p
);
1649 if (!convert_to_array(C
, param
, &p
)) {
1650 slang_type_specifier_dtr(&p
);
1653 slang_type_specifier_dtr(&p
);
1654 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1659 /* calculate the parameter size */
1660 if (!calculate_var_size(C
, O
, param
))
1663 /* TODO: allocate the local address here? */
1668 #define FUNCTION_ORDINARY 0
1669 #define FUNCTION_CONSTRUCTOR 1
1670 #define FUNCTION_OPERATOR 2
1672 /* function parameter */
1673 #define PARAMETER_NONE 0
1674 #define PARAMETER_NEXT 1
1677 #define OPERATOR_ADDASSIGN 1
1678 #define OPERATOR_SUBASSIGN 2
1679 #define OPERATOR_MULASSIGN 3
1680 #define OPERATOR_DIVASSIGN 4
1681 /*#define OPERATOR_MODASSIGN 5*/
1682 /*#define OPERATOR_LSHASSIGN 6*/
1683 /*#define OPERATOR_RSHASSIGN 7*/
1684 /*#define OPERATOR_ANDASSIGN 8*/
1685 /*#define OPERATOR_XORASSIGN 9*/
1686 /*#define OPERATOR_ORASSIGN 10*/
1687 #define OPERATOR_LOGICALXOR 11
1688 /*#define OPERATOR_BITOR 12*/
1689 /*#define OPERATOR_BITXOR 13*/
1690 /*#define OPERATOR_BITAND 14*/
1691 #define OPERATOR_LESS 15
1692 #define OPERATOR_GREATER 16
1693 #define OPERATOR_LESSEQUAL 17
1694 #define OPERATOR_GREATEREQUAL 18
1695 /*#define OPERATOR_LSHIFT 19*/
1696 /*#define OPERATOR_RSHIFT 20*/
1697 #define OPERATOR_MULTIPLY 21
1698 #define OPERATOR_DIVIDE 22
1699 /*#define OPERATOR_MODULUS 23*/
1700 #define OPERATOR_INCREMENT 24
1701 #define OPERATOR_DECREMENT 25
1702 #define OPERATOR_PLUS 26
1703 #define OPERATOR_MINUS 27
1704 /*#define OPERATOR_COMPLEMENT 28*/
1705 #define OPERATOR_NOT 29
1709 unsigned int o_code
;
1711 } operator_names
[] = {
1712 {OPERATOR_INCREMENT
, "++"},
1713 {OPERATOR_ADDASSIGN
, "+="},
1714 {OPERATOR_PLUS
, "+"},
1715 {OPERATOR_DECREMENT
, "--"},
1716 {OPERATOR_SUBASSIGN
, "-="},
1717 {OPERATOR_MINUS
, "-"},
1718 {OPERATOR_NOT
, "!"},
1719 {OPERATOR_MULASSIGN
, "*="},
1720 {OPERATOR_MULTIPLY
, "*"},
1721 {OPERATOR_DIVASSIGN
, "/="},
1722 {OPERATOR_DIVIDE
, "/"},
1723 {OPERATOR_LESSEQUAL
, "<="},
1724 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1725 /*{ OPERATOR_LSHIFT, "<<" }, */
1726 {OPERATOR_LESS
, "<"},
1727 {OPERATOR_GREATEREQUAL
, ">="},
1728 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1729 /*{ OPERATOR_RSHIFT, ">>" }, */
1730 {OPERATOR_GREATER
, ">"},
1731 /*{ OPERATOR_MODASSIGN, "%=" }, */
1732 /*{ OPERATOR_MODULUS, "%" }, */
1733 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1734 /*{ OPERATOR_BITAND, "&" }, */
1735 /*{ OPERATOR_ORASSIGN, "|=" }, */
1736 /*{ OPERATOR_BITOR, "|" }, */
1737 /*{ OPERATOR_COMPLEMENT, "~" }, */
1738 /*{ OPERATOR_XORASSIGN, "^=" }, */
1739 {OPERATOR_LOGICALXOR
, "^^"},
1740 /*{ OPERATOR_BITXOR, "^" } */
1744 parse_operator_name(slang_parse_ctx
* C
)
1748 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1749 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1751 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1752 if (atom
== SLANG_ATOM_NULL
) {
1753 slang_info_log_memory(C
->L
);
1765 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1766 slang_function
* func
)
1769 /* parse function type and name */
1770 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1775 case FUNCTION_ORDINARY
:
1776 func
->kind
= SLANG_FUNC_ORDINARY
;
1777 func
->header
.a_name
= parse_identifier(C
);
1778 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1781 case FUNCTION_CONSTRUCTOR
:
1782 func
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1783 if (func
->header
.type
.specifier
.type
== SLANG_SPEC_STRUCT
)
1785 func
->header
.a_name
=
1786 slang_atom_pool_atom(C
->atoms
,
1787 slang_type_specifier_type_to_string
1788 (func
->header
.type
.specifier
.type
));
1789 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1790 slang_info_log_memory(C
->L
);
1794 case FUNCTION_OPERATOR
:
1795 func
->kind
= SLANG_FUNC_OPERATOR
;
1796 func
->header
.a_name
= parse_operator_name(C
);
1797 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1804 if (!legal_identifier(func
->header
.a_name
)) {
1805 slang_info_log_error(C
->L
, "illegal function name '%s'",
1806 (char *) func
->header
.a_name
);
1810 /* parse function parameters */
1811 while (*C
->I
++ == PARAMETER_NEXT
) {
1812 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1814 slang_info_log_memory(C
->L
);
1817 if (!parse_parameter_declaration(C
, O
, p
))
1821 /* if the function returns a value, append a hidden __retVal 'out'
1822 * parameter that corresponds to the return value.
1824 if (_slang_function_has_return_value(func
)) {
1825 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1826 slang_atom a_retVal
= slang_atom_pool_atom(C
->atoms
, "__retVal");
1828 p
->a_name
= a_retVal
;
1829 p
->type
= func
->header
.type
;
1830 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1833 /* function formal parameters and local variables share the same
1834 * scope, so save the information about param count in a seperate
1835 * place also link the scope to the global variable scope so when a
1836 * given identifier is not found here, the search process continues
1837 * in the global space
1839 func
->param_count
= func
->parameters
->num_variables
;
1840 func
->parameters
->outer_scope
= O
->vars
;
1846 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1847 slang_function
* func
)
1849 slang_output_ctx o
= *O
;
1851 if (!parse_function_prototype(C
, O
, func
))
1854 /* create function's body operation */
1855 func
->body
= (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1856 if (func
->body
== NULL
) {
1857 slang_info_log_memory(C
->L
);
1860 if (!slang_operation_construct(func
->body
)) {
1861 _slang_free(func
->body
);
1863 slang_info_log_memory(C
->L
);
1867 /* to parse the body the parse context is modified in order to
1868 * capture parsed variables into function's local variable scope
1870 C
->global_scope
= GL_FALSE
;
1871 o
.vars
= func
->parameters
;
1872 if (!parse_statement(C
, &o
, func
->body
))
1875 C
->global_scope
= GL_TRUE
;
1880 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1882 slang_operation op_id
, op_assign
;
1885 /* construct the left side of assignment */
1886 if (!slang_operation_construct(&op_id
))
1888 op_id
.type
= SLANG_OPER_IDENTIFIER
;
1889 op_id
.a_id
= var
->a_name
;
1891 /* put the variable into operation's scope */
1892 op_id
.locals
->variables
=
1893 (slang_variable
**) _slang_alloc(sizeof(slang_variable
*));
1894 if (op_id
.locals
->variables
== NULL
) {
1895 slang_operation_destruct(&op_id
);
1898 op_id
.locals
->num_variables
= 1;
1899 op_id
.locals
->variables
[0] = var
;
1901 /* construct the assignment expression */
1902 if (!slang_operation_construct(&op_assign
)) {
1903 op_id
.locals
->num_variables
= 0;
1904 slang_operation_destruct(&op_id
);
1907 op_assign
.type
= SLANG_OPER_ASSIGN
;
1908 op_assign
.children
=
1909 (slang_operation
*) _slang_alloc(2 * sizeof(slang_operation
));
1910 if (op_assign
.children
== NULL
) {
1911 slang_operation_destruct(&op_assign
);
1912 op_id
.locals
->num_variables
= 0;
1913 slang_operation_destruct(&op_id
);
1916 op_assign
.num_children
= 2;
1917 op_assign
.children
[0] = op_id
;
1918 op_assign
.children
[1] = *var
->initializer
;
1922 /* carefully destroy the operations */
1923 op_assign
.num_children
= 0;
1924 _slang_free(op_assign
.children
);
1925 op_assign
.children
= NULL
;
1926 slang_operation_destruct(&op_assign
);
1927 op_id
.locals
->num_variables
= 0;
1928 slang_operation_destruct(&op_id
);
1936 /* init declarator list */
1937 #define DECLARATOR_NONE 0
1938 #define DECLARATOR_NEXT 1
1940 /* variable declaration */
1941 #define VARIABLE_NONE 0
1942 #define VARIABLE_IDENTIFIER 1
1943 #define VARIABLE_INITIALIZER 2
1944 #define VARIABLE_ARRAY_EXPLICIT 3
1945 #define VARIABLE_ARRAY_UNKNOWN 4
1949 * Parse the initializer for a variable declaration.
1952 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1953 const slang_fully_specified_type
* type
)
1955 slang_variable
*var
;
1958 /* empty init declatator (without name, e.g. "float ;") */
1959 if (*C
->I
++ == VARIABLE_NONE
)
1962 a_name
= parse_identifier(C
);
1964 /* check if name is already in this scope */
1965 if (_slang_variable_locate(O
->vars
, a_name
, GL_FALSE
)) {
1966 slang_info_log_error(C
->L
,
1967 "declaration of '%s' conflicts with previous declaration",
1972 /* make room for the new variable and initialize it */
1973 var
= slang_variable_scope_grow(O
->vars
);
1975 slang_info_log_memory(C
->L
);
1979 /* copy the declarator type qualifier/etc info, parse the identifier */
1980 var
->type
.qualifier
= type
->qualifier
;
1981 var
->type
.centroid
= type
->centroid
;
1982 var
->type
.precision
= type
->precision
;
1983 var
->type
.variant
= type
->variant
;
1984 var
->type
.array_len
= type
->array_len
;
1985 var
->a_name
= a_name
;
1986 if (var
->a_name
== SLANG_ATOM_NULL
)
1991 /* simple variable declarator - just copy the specifier */
1992 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1995 case VARIABLE_INITIALIZER
:
1996 /* initialized variable - copy the specifier and parse the expression */
1997 if (0 && type
->array_len
>= 0) {
1998 /* The type was something like "float[4]" */
1999 convert_to_array(C
, var
, &type
->specifier
);
2000 var
->array_len
= type
->array_len
;
2003 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
2007 (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
2008 if (var
->initializer
== NULL
) {
2009 slang_info_log_memory(C
->L
);
2012 if (!slang_operation_construct(var
->initializer
)) {
2013 _slang_free(var
->initializer
);
2014 var
->initializer
= NULL
;
2015 slang_info_log_memory(C
->L
);
2018 if (!parse_expression(C
, O
, var
->initializer
))
2021 case VARIABLE_ARRAY_UNKNOWN
:
2022 /* unsized array - mark it as array and copy the specifier to
2025 if (type
->array_len
>= 0) {
2026 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
2029 if (!convert_to_array(C
, var
, &type
->specifier
))
2032 case VARIABLE_ARRAY_EXPLICIT
:
2033 if (type
->array_len
>= 0) {
2034 /* the user is trying to do something like: float[2] x[3]; */
2035 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
2038 if (!convert_to_array(C
, var
, &type
->specifier
))
2040 if (!parse_array_len(C
, O
, &var
->array_len
))
2047 /* allocate global address space for a variable with a known size */
2049 && !(var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
2050 && var
->array_len
== 0)) {
2051 if (!calculate_var_size(C
, O
, var
))
2055 /* emit code for global var decl */
2056 if (C
->global_scope
) {
2057 slang_assemble_ctx A
;
2059 A
.space
.funcs
= O
->funs
;
2060 A
.space
.structs
= O
->structs
;
2061 A
.space
.vars
= O
->vars
;
2062 A
.program
= O
->program
;
2063 A
.pragmas
= O
->pragmas
;
2064 A
.vartable
= O
->vartable
;
2066 A
.curFuncEndLabel
= NULL
;
2067 if (!_slang_codegen_global_variable(&A
, var
, C
->type
))
2071 /* initialize global variable */
2072 if (C
->global_scope
) {
2073 if (var
->initializer
!= NULL
) {
2074 slang_assemble_ctx A
;
2077 A
.space
.funcs
= O
->funs
;
2078 A
.space
.structs
= O
->structs
;
2079 A
.space
.vars
= O
->vars
;
2080 if (!initialize_global(&A
, var
))
2088 * Parse a list of variable declarations. Each variable may have an
2092 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2094 slang_fully_specified_type type
;
2096 /* parse the fully specified type, common to all declarators */
2097 if (!slang_fully_specified_type_construct(&type
))
2099 if (!parse_fully_specified_type(C
, O
, &type
)) {
2100 slang_fully_specified_type_destruct(&type
);
2104 /* parse declarators, pass-in the parsed type */
2106 if (!parse_init_declarator(C
, O
, &type
)) {
2107 slang_fully_specified_type_destruct(&type
);
2111 while (*C
->I
++ == DECLARATOR_NEXT
);
2113 slang_fully_specified_type_destruct(&type
);
2119 * Parse a function definition or declaration.
2120 * \param C parsing context
2121 * \param O output context
2122 * \param definition if non-zero expect a definition, else a declaration
2123 * \param parsed_func_ret returns the parsed function
2124 * \return GL_TRUE if success, GL_FALSE if failure
2127 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
2128 slang_function
** parsed_func_ret
)
2130 slang_function parsed_func
, *found_func
;
2132 /* parse function definition/declaration */
2133 if (!slang_function_construct(&parsed_func
))
2136 if (!parse_function_definition(C
, O
, &parsed_func
)) {
2137 slang_function_destruct(&parsed_func
);
2142 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
2143 slang_function_destruct(&parsed_func
);
2148 /* find a function with a prototype matching the parsed one - only
2149 * the current scope is being searched to allow built-in function
2152 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
2153 if (found_func
== NULL
) {
2154 /* New function, add it to the function list */
2155 O
->funs
->functions
=
2156 (slang_function
*) _slang_realloc(O
->funs
->functions
,
2157 O
->funs
->num_functions
2158 * sizeof(slang_function
),
2159 (O
->funs
->num_functions
+ 1)
2160 * sizeof(slang_function
));
2161 if (O
->funs
->functions
== NULL
) {
2162 slang_info_log_memory(C
->L
);
2163 slang_function_destruct(&parsed_func
);
2166 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
2167 O
->funs
->num_functions
++;
2169 /* return the newly parsed function */
2170 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
2173 /* previously defined or declared */
2174 /* TODO: check function return type qualifiers and specifiers */
2176 if (found_func
->body
!= NULL
) {
2177 slang_info_log_error(C
->L
, "%s: function already has a body.",
2178 slang_atom_pool_id(C
->atoms
,
2181 slang_function_destruct(&parsed_func
);
2185 /* destroy the existing function declaration and replace it
2188 slang_function_destruct(found_func
);
2189 *found_func
= parsed_func
;
2192 /* another declaration of the same function prototype - ignore it */
2193 slang_function_destruct(&parsed_func
);
2196 /* return the found function */
2197 *parsed_func_ret
= found_func
;
2204 #define DECLARATION_FUNCTION_PROTOTYPE 1
2205 #define DECLARATION_INIT_DECLARATOR_LIST 2
2208 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2211 case DECLARATION_INIT_DECLARATOR_LIST
:
2212 if (!parse_init_declarator_list(C
, O
))
2215 case DECLARATION_FUNCTION_PROTOTYPE
:
2217 slang_function
*dummy_func
;
2219 if (!parse_function(C
, O
, 0, &dummy_func
))
2230 parse_default_precision(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2232 int precision
, type
;
2234 if (!O
->allow_precision
) {
2235 slang_info_log_error(C
->L
, "syntax error at \"precision\"");
2239 precision
= *C
->I
++;
2240 switch (precision
) {
2242 case PRECISION_MEDIUM
:
2243 case PRECISION_HIGH
:
2247 _mesa_problem(NULL
, "unexpected precision %d at %s:%d\n",
2248 precision
, __FILE__
, __LINE__
);
2254 case TYPE_SPECIFIER_FLOAT
:
2255 case TYPE_SPECIFIER_INT
:
2256 case TYPE_SPECIFIER_SAMPLER1D
:
2257 case TYPE_SPECIFIER_SAMPLER2D
:
2258 case TYPE_SPECIFIER_SAMPLER3D
:
2259 case TYPE_SPECIFIER_SAMPLERCUBE
:
2260 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
2261 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
2262 case TYPE_SPECIFIER_SAMPLER2DRECT
:
2263 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
2267 _mesa_problem(NULL
, "unexpected type %d at %s:%d\n",
2268 type
, __FILE__
, __LINE__
);
2272 assert(type
< TYPE_SPECIFIER_COUNT
);
2273 O
->default_precision
[type
] = precision
;
2280 * Initialize the default precision for all types.
2281 * XXX this info isn't used yet.
2284 init_default_precision(slang_output_ctx
*O
, slang_unit_type type
)
2287 for (i
= 0; i
< TYPE_SPECIFIER_COUNT
; i
++) {
2288 #if FEATURE_es2_glsl
2289 O
->default_precision
[i
] = PRECISION_LOW
;
2291 O
->default_precision
[i
] = PRECISION_HIGH
;
2295 if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2296 O
->default_precision
[TYPE_SPECIFIER_FLOAT
] = PRECISION_HIGH
;
2297 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_HIGH
;
2300 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_MEDIUM
;
2306 parse_invariant(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2308 if (O
->allow_invariant
) {
2309 slang_atom
*a
= parse_identifier(C
);
2310 /* XXX not doing anything with this var yet */
2311 /*printf("ID: %s\n", (char*) a);*/
2315 slang_info_log_error(C
->L
, "syntax error at \"invariant\"");
2321 /* external declaration or default precision specifier */
2322 #define EXTERNAL_NULL 0
2323 #define EXTERNAL_FUNCTION_DEFINITION 1
2324 #define EXTERNAL_DECLARATION 2
2325 #define DEFAULT_PRECISION 3
2326 #define INVARIANT_STMT 4
2330 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
,
2331 struct gl_shader
*shader
)
2333 GET_CURRENT_CONTEXT(ctx
);
2337 slang_function
*mainFunc
= NULL
;
2339 if (unit
->type
== SLANG_UNIT_FRAGMENT_BUILTIN
||
2340 unit
->type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2341 maxRegs
= ctx
->Const
.FragmentProgram
.MaxTemps
;
2344 assert(unit
->type
== SLANG_UNIT_VERTEX_BUILTIN
||
2345 unit
->type
== SLANG_UNIT_VERTEX_SHADER
);
2346 maxRegs
= ctx
->Const
.VertexProgram
.MaxTemps
;
2349 /* setup output context */
2350 o
.funs
= &unit
->funs
;
2351 o
.structs
= &unit
->structs
;
2352 o
.vars
= &unit
->vars
;
2353 o
.program
= shader
? shader
->Program
: NULL
;
2354 o
.pragmas
= shader
? &shader
->Pragmas
: NULL
;
2355 o
.vartable
= _slang_new_var_table(maxRegs
);
2356 _slang_push_var_table(o
.vartable
);
2358 /* allow 'invariant' keyword? */
2359 #if FEATURE_es2_glsl
2360 o
.allow_invariant
= GL_TRUE
;
2362 o
.allow_invariant
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2365 /* allow 'centroid' keyword? */
2366 o
.allow_centroid
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2368 /* allow 'lowp/mediump/highp' keywords? */
2369 #if FEATURE_es2_glsl
2370 o
.allow_precision
= GL_TRUE
;
2372 o
.allow_precision
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2374 init_default_precision(&o
, unit
->type
);
2376 /* allow 'float[]' keyword? */
2377 o
.allow_array_types
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2379 /* parse individual functions and declarations */
2380 while (*C
->I
!= EXTERNAL_NULL
) {
2382 case EXTERNAL_FUNCTION_DEFINITION
:
2384 slang_function
*func
;
2385 success
= parse_function(C
, &o
, 1, &func
);
2387 _mesa_strcmp((char *) func
->header
.a_name
, "main") == 0) {
2393 case EXTERNAL_DECLARATION
:
2394 success
= parse_declaration(C
, &o
);
2396 case DEFAULT_PRECISION
:
2397 success
= parse_default_precision(C
, &o
);
2399 case INVARIANT_STMT
:
2400 success
= parse_invariant(C
, &o
);
2407 /* xxx free codegen */
2408 _slang_pop_var_table(o
.vartable
);
2415 /* assemble (generate code) for main() */
2416 slang_assemble_ctx A
;
2419 A
.space
.funcs
= o
.funs
;
2420 A
.space
.structs
= o
.structs
;
2421 A
.space
.vars
= o
.vars
;
2422 A
.program
= o
.program
;
2423 A
.pragmas
= &shader
->Pragmas
;
2424 A
.vartable
= o
.vartable
;
2427 /* main() takes no parameters */
2428 if (mainFunc
->param_count
> 0) {
2429 slang_info_log_error(A
.log
, "main() takes no arguments");
2433 _slang_codegen_function(&A
, mainFunc
);
2435 shader
->Main
= GL_TRUE
; /* this shader defines main() */
2438 _slang_pop_var_table(o
.vartable
);
2439 _slang_delete_var_table(o
.vartable
);
2445 compile_binary(const byte
* prod
, slang_code_unit
* unit
,
2447 slang_unit_type type
, slang_info_log
* infolog
,
2448 slang_code_unit
* builtin
, slang_code_unit
* downlink
,
2449 struct gl_shader
*shader
)
2455 /* setup parse context */
2458 C
.parsing_builtin
= (builtin
== NULL
);
2459 C
.global_scope
= GL_TRUE
;
2460 C
.atoms
= &unit
->object
->atompool
;
2462 C
.version
= version
;
2464 if (!check_revision(&C
))
2467 if (downlink
!= NULL
) {
2468 unit
->vars
.outer_scope
= &downlink
->vars
;
2469 unit
->funs
.outer_scope
= &downlink
->funs
;
2470 unit
->structs
.outer_scope
= &downlink
->structs
;
2473 /* parse translation unit */
2474 return parse_code_unit(&C
, unit
, shader
);
2478 compile_with_grammar(grammar id
, const char *source
, slang_code_unit
* unit
,
2479 slang_unit_type type
, slang_info_log
* infolog
,
2480 slang_code_unit
* builtin
,
2481 struct gl_shader
*shader
,
2482 const struct gl_extensions
*extensions
,
2483 struct gl_sl_pragmas
*pragmas
)
2486 GLuint size
, start
, version
;
2487 slang_string preprocessed
;
2490 #if FEATURE_ARB_shading_language_120
2492 #elif FEATURE_es2_glsl
2498 /* First retrieve the version number. */
2499 if (!_slang_preprocess_version(source
, &version
, &start
, infolog
))
2502 if (version
> maxVersion
) {
2503 slang_info_log_error(infolog
,
2504 "language version %.2f is not supported.",
2509 /* Now preprocess the source string. */
2510 slang_string_init(&preprocessed
);
2511 if (!_slang_preprocess_directives(&preprocessed
, &source
[start
],
2512 infolog
, extensions
, pragmas
)) {
2513 slang_string_free(&preprocessed
);
2514 slang_info_log_error(infolog
, "failed to preprocess the source.");
2518 /* Finally check the syntax and generate its binary representation. */
2519 if (!grammar_fast_check(id
,
2520 (const byte
*) (slang_string_cstr(&preprocessed
)),
2521 &prod
, &size
, 65536)) {
2525 slang_string_free(&preprocessed
);
2526 grammar_get_last_error((byte
*) (buf
), sizeof(buf
), &pos
);
2527 slang_info_log_error(infolog
, buf
);
2528 /* syntax error (possibly in library code) */
2533 s
= (char *) _mesa_find_line_column((const GLubyte
*) source
,
2534 (const GLubyte
*) source
+ pos
,
2536 printf("Error on line %d, col %d: %s\n", line
, col
, s
);
2541 slang_string_free(&preprocessed
);
2543 /* Syntax is okay - translate it to internal representation. */
2544 if (!compile_binary(prod
, unit
, version
, type
, infolog
, builtin
,
2545 &builtin
[SLANG_BUILTIN_TOTAL
- 1],
2547 grammar_alloc_free(prod
);
2550 grammar_alloc_free(prod
);
2554 LONGSTRING
static const char *slang_shader_syn
=
2555 #include "library/slang_shader_syn.h"
2558 static const byte slang_core_gc
[] = {
2559 #include "library/slang_core_gc.h"
2562 static const byte slang_120_core_gc
[] = {
2563 #include "library/slang_120_core_gc.h"
2566 static const byte slang_120_fragment_gc
[] = {
2567 #include "library/slang_builtin_120_fragment_gc.h"
2570 static const byte slang_common_builtin_gc
[] = {
2571 #include "library/slang_common_builtin_gc.h"
2574 static const byte slang_fragment_builtin_gc
[] = {
2575 #include "library/slang_fragment_builtin_gc.h"
2578 static const byte slang_vertex_builtin_gc
[] = {
2579 #include "library/slang_vertex_builtin_gc.h"
2583 compile_object(grammar
* id
, const char *source
, slang_code_object
* object
,
2584 slang_unit_type type
, slang_info_log
* infolog
,
2585 struct gl_shader
*shader
,
2586 const struct gl_extensions
*extensions
,
2587 struct gl_sl_pragmas
*pragmas
)
2589 slang_code_unit
*builtins
= NULL
;
2590 GLuint base_version
= 110;
2592 /* load GLSL grammar */
2593 *id
= grammar_load_from_text((const byte
*) (slang_shader_syn
));
2598 grammar_get_last_error(buf
, 1024, &pos
);
2599 slang_info_log_error(infolog
, (const char *) (buf
));
2603 /* set shader type - the syntax is slightly different for different shaders */
2604 if (type
== SLANG_UNIT_FRAGMENT_SHADER
2605 || type
== SLANG_UNIT_FRAGMENT_BUILTIN
)
2606 grammar_set_reg8(*id
, (const byte
*) "shader_type", 1);
2608 grammar_set_reg8(*id
, (const byte
*) "shader_type", 2);
2610 /* enable language extensions */
2611 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2613 /* if parsing user-specified shader, load built-in library */
2614 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_VERTEX_SHADER
) {
2615 /* compile core functionality first */
2616 if (!compile_binary(slang_core_gc
,
2617 &object
->builtin
[SLANG_BUILTIN_CORE
],
2619 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2623 #if FEATURE_ARB_shading_language_120
2624 if (!compile_binary(slang_120_core_gc
,
2625 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2627 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2628 NULL
, &object
->builtin
[SLANG_BUILTIN_CORE
], NULL
))
2632 /* compile common functions and variables, link to core */
2633 if (!compile_binary(slang_common_builtin_gc
,
2634 &object
->builtin
[SLANG_BUILTIN_COMMON
],
2635 #if FEATURE_ARB_shading_language_120
2640 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2641 #if FEATURE_ARB_shading_language_120
2642 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2644 &object
->builtin
[SLANG_BUILTIN_CORE
],
2649 /* compile target-specific functions and variables, link to common */
2650 if (type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2651 if (!compile_binary(slang_fragment_builtin_gc
,
2652 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2654 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2655 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2657 #if FEATURE_ARB_shading_language_120
2658 if (!compile_binary(slang_120_fragment_gc
,
2659 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2661 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2662 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2666 else if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2667 if (!compile_binary(slang_vertex_builtin_gc
,
2668 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2670 SLANG_UNIT_VERTEX_BUILTIN
, infolog
, NULL
,
2671 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2675 /* disable language extensions */
2676 #if NEW_SLANG /* allow-built-ins */
2677 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2679 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 0);
2681 builtins
= object
->builtin
;
2684 /* compile the actual shader - pass-in built-in library for external shader */
2685 return compile_with_grammar(*id
, source
, &object
->unit
, type
, infolog
,
2686 builtins
, shader
, extensions
, pragmas
);
2691 compile_shader(GLcontext
*ctx
, slang_code_object
* object
,
2692 slang_unit_type type
, slang_info_log
* infolog
,
2693 struct gl_shader
*shader
)
2698 #if 0 /* for debug */
2699 _mesa_printf("********* COMPILE SHADER ***********\n");
2700 _mesa_printf("%s\n", shader
->Source
);
2701 _mesa_printf("************************************\n");
2704 assert(shader
->Program
);
2706 _slang_code_object_dtr(object
);
2707 _slang_code_object_ctr(object
);
2709 success
= compile_object(&id
, shader
->Source
, object
, type
, infolog
, shader
,
2710 &ctx
->Extensions
, &shader
->Pragmas
);
2712 grammar_destroy(id
);
2722 _slang_compile(GLcontext
*ctx
, struct gl_shader
*shader
)
2725 slang_info_log info_log
;
2726 slang_code_object obj
;
2727 slang_unit_type type
;
2729 if (shader
->Type
== GL_VERTEX_SHADER
) {
2730 type
= SLANG_UNIT_VERTEX_SHADER
;
2733 assert(shader
->Type
== GL_FRAGMENT_SHADER
);
2734 type
= SLANG_UNIT_FRAGMENT_SHADER
;
2737 if (!shader
->Source
)
2740 ctx
->Shader
.MemPool
= _slang_new_mempool(1024*1024);
2742 shader
->Main
= GL_FALSE
;
2744 if (!shader
->Program
) {
2746 if (shader
->Type
== GL_VERTEX_SHADER
)
2747 progTarget
= GL_VERTEX_PROGRAM_ARB
;
2749 progTarget
= GL_FRAGMENT_PROGRAM_ARB
;
2750 shader
->Program
= ctx
->Driver
.NewProgram(ctx
, progTarget
, 1);
2751 shader
->Program
->Parameters
= _mesa_new_parameter_list();
2752 shader
->Program
->Varying
= _mesa_new_parameter_list();
2753 shader
->Program
->Attributes
= _mesa_new_parameter_list();
2756 slang_info_log_construct(&info_log
);
2757 _slang_code_object_ctr(&obj
);
2759 success
= compile_shader(ctx
, &obj
, type
, &info_log
, shader
);
2761 /* free shader's prev info log */
2762 if (shader
->InfoLog
) {
2763 _mesa_free(shader
->InfoLog
);
2764 shader
->InfoLog
= NULL
;
2767 if (info_log
.text
) {
2768 /* copy info-log string to shader object */
2769 shader
->InfoLog
= _mesa_strdup(info_log
.text
);
2772 if (info_log
.error_flag
) {
2776 slang_info_log_destruct(&info_log
);
2777 _slang_code_object_dtr(&obj
);
2779 _slang_delete_mempool((slang_mempool
*) ctx
->Shader
.MemPool
);
2780 ctx
->Shader
.MemPool
= NULL
;
2782 /* remove any reads of output registers */
2784 printf("Pre-remove output reads:\n");
2785 _mesa_print_program(shader
->Program
);
2787 _mesa_remove_output_reads(shader
->Program
, PROGRAM_OUTPUT
);
2788 if (shader
->Type
== GL_VERTEX_SHADER
) {
2789 /* and remove writes to varying vars in vertex programs */
2790 _mesa_remove_output_reads(shader
->Program
, PROGRAM_VARYING
);
2793 printf("Post-remove output reads:\n");
2794 _mesa_print_program(shader
->Program
);