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 slang_var_table
*vartable
;
148 GLuint default_precision
[TYPE_SPECIFIER_COUNT
];
149 GLboolean allow_precision
;
150 GLboolean allow_invariant
;
151 GLboolean allow_centroid
;
152 GLboolean allow_array_types
; /* float[] syntax */
155 /* _slang_compile() */
158 /* Debugging aid, print file/line where parsing error is detected */
162 printf("slang error at %s:%d\n", __FILE__, __LINE__); \
168 parse_identifier_str(slang_parse_ctx
* C
, char **id
)
171 C
->I
+= _mesa_strlen(*id
) + 1;
175 parse_identifier(slang_parse_ctx
* C
)
179 id
= (const char *) C
->I
;
180 C
->I
+= _mesa_strlen(id
) + 1;
181 return slang_atom_pool_atom(C
->atoms
, id
);
185 parse_number(slang_parse_ctx
* C
, int *number
)
187 const int radix
= (int) (*C
->I
++);
189 while (*C
->I
!= '\0') {
191 if (*C
->I
>= '0' && *C
->I
<= '9')
192 digit
= (int) (*C
->I
- '0');
193 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
194 digit
= (int) (*C
->I
- 'A') + 10;
196 digit
= (int) (*C
->I
- 'a') + 10;
197 *number
= *number
* radix
+ digit
;
202 slang_info_log_warning(C
->L
, "%d: literal integer overflow.", *number
);
207 parse_float(slang_parse_ctx
* C
, float *number
)
209 char *integral
= NULL
;
210 char *fractional
= NULL
;
211 char *exponent
= NULL
;
214 parse_identifier_str(C
, &integral
);
215 parse_identifier_str(C
, &fractional
);
216 parse_identifier_str(C
, &exponent
);
218 whole
= (char *) _slang_alloc((_mesa_strlen(integral
) +
219 _mesa_strlen(fractional
) +
220 _mesa_strlen(exponent
) + 3) * sizeof(char));
222 slang_info_log_memory(C
->L
);
226 slang_string_copy(whole
, integral
);
227 slang_string_concat(whole
, ".");
228 slang_string_concat(whole
, fractional
);
229 slang_string_concat(whole
, "E");
230 slang_string_concat(whole
, exponent
);
232 *number
= (float) (_mesa_strtod(whole
, (char **) NULL
));
239 /* revision number - increment after each change affecting emitted output */
243 check_revision(slang_parse_ctx
* C
)
245 if (*C
->I
!= REVISION
) {
246 slang_info_log_error(C
->L
, "Internal compiler error.");
253 static int parse_statement(slang_parse_ctx
*, slang_output_ctx
*,
255 static int parse_expression(slang_parse_ctx
*, slang_output_ctx
*,
257 static int parse_type_specifier(slang_parse_ctx
*, slang_output_ctx
*,
258 slang_type_specifier
*);
260 parse_type_array_size(slang_parse_ctx
*C
,
265 parse_array_len(slang_parse_ctx
* C
, slang_output_ctx
* O
, GLuint
* len
)
267 slang_operation array_size
;
268 slang_name_space space
;
271 if (!slang_operation_construct(&array_size
))
273 if (!parse_expression(C
, O
, &array_size
)) {
274 slang_operation_destruct(&array_size
);
278 space
.funcs
= O
->funs
;
279 space
.structs
= O
->structs
;
280 space
.vars
= O
->vars
;
282 /* evaluate compile-time expression which is array size */
283 _slang_simplify(&array_size
, &space
, C
->atoms
);
285 if (array_size
.type
== SLANG_OPER_LITERAL_INT
) {
287 *len
= (GLint
) array_size
.literal
[0];
288 } else if (array_size
.type
== SLANG_OPER_IDENTIFIER
) {
289 slang_variable
*var
= _slang_variable_locate(array_size
.locals
, array_size
.a_id
, GL_TRUE
);
291 slang_info_log_error(C
->L
, "undefined variable '%s'",
292 (char *) array_size
.a_id
);
294 } else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&&
295 var
->type
.specifier
.type
== SLANG_SPEC_INT
) {
296 if (var
->initializer
&&
297 var
->initializer
->type
== SLANG_OPER_LITERAL_INT
) {
298 *len
= (GLint
) var
->initializer
->literal
[0];
301 slang_info_log_error(C
->L
, "unable to parse array size declaration");
305 slang_info_log_error(C
->L
, "unable to parse array size declaration");
312 slang_operation_destruct(&array_size
);
317 calculate_var_size(slang_parse_ctx
* C
, slang_output_ctx
* O
,
318 slang_variable
* var
)
320 slang_storage_aggregate agg
;
322 if (!slang_storage_aggregate_construct(&agg
))
324 if (!_slang_aggregate_variable(&agg
, &var
->type
.specifier
, var
->array_len
,
325 O
->funs
, O
->structs
, O
->vars
, C
->atoms
)) {
326 slang_storage_aggregate_destruct(&agg
);
329 var
->size
= _slang_sizeof_aggregate(&agg
);
330 slang_storage_aggregate_destruct(&agg
);
335 promote_type_to_array(slang_parse_ctx
*C
,
336 slang_fully_specified_type
*type
,
339 slang_type_specifier
*baseType
=
340 slang_type_specifier_new(type
->specifier
.type
, NULL
, NULL
);
342 type
->specifier
.type
= SLANG_SPEC_ARRAY
;
343 type
->specifier
._array
= baseType
;
344 type
->array_len
= array_len
;
349 convert_to_array(slang_parse_ctx
* C
, slang_variable
* var
,
350 const slang_type_specifier
* sp
)
352 /* sized array - mark it as array, copy the specifier to the array element
353 * and parse the expression */
354 var
->type
.specifier
.type
= SLANG_SPEC_ARRAY
;
355 var
->type
.specifier
._array
= (slang_type_specifier
*)
356 _slang_alloc(sizeof(slang_type_specifier
));
357 if (var
->type
.specifier
._array
== NULL
) {
358 slang_info_log_memory(C
->L
);
361 slang_type_specifier_ctr(var
->type
.specifier
._array
);
362 return slang_type_specifier_copy(var
->type
.specifier
._array
, sp
);
365 /* structure field */
368 #define FIELD_ARRAY 2
371 parse_struct_field_var(slang_parse_ctx
* C
, slang_output_ctx
* O
,
372 slang_variable
* var
, slang_atom a_name
,
373 const slang_type_specifier
* sp
,
376 var
->a_name
= a_name
;
377 if (var
->a_name
== SLANG_ATOM_NULL
)
382 if (array_len
!= -1) {
383 if (!convert_to_array(C
, var
, sp
))
385 var
->array_len
= array_len
;
388 if (!slang_type_specifier_copy(&var
->type
.specifier
, sp
))
395 if (!convert_to_array(C
, var
, sp
))
397 if (!parse_array_len(C
, O
, &var
->array_len
))
404 return calculate_var_size(C
, O
, var
);
408 parse_struct_field(slang_parse_ctx
* C
, slang_output_ctx
* O
,
409 slang_struct
* st
, slang_type_specifier
* sp
)
411 slang_output_ctx o
= *O
;
414 o
.structs
= st
->structs
;
415 if (!parse_type_specifier(C
, &o
, sp
))
417 if (!parse_type_array_size(C
, &o
, &array_len
))
422 slang_variable
*var
= slang_variable_scope_grow(st
->fields
);
424 slang_info_log_memory(C
->L
);
427 a_name
= parse_identifier(C
);
428 if (_slang_variable_locate(st
->fields
, a_name
, GL_FALSE
)) {
429 slang_info_log_error(C
->L
, "duplicate field '%s'", (char *) a_name
);
433 if (!parse_struct_field_var(C
, &o
, var
, a_name
, sp
, array_len
))
436 while (*C
->I
++ != FIELD_NONE
);
442 parse_struct(slang_parse_ctx
* C
, slang_output_ctx
* O
, slang_struct
** st
)
447 /* parse struct name (if any) and make sure it is unique in current scope */
448 a_name
= parse_identifier(C
);
449 if (a_name
== SLANG_ATOM_NULL
)
452 name
= slang_atom_pool_id(C
->atoms
, a_name
);
454 && slang_struct_scope_find(O
->structs
, a_name
, 0) != NULL
) {
455 slang_info_log_error(C
->L
, "%s: duplicate type name.", name
);
459 /* set-up a new struct */
460 *st
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
462 slang_info_log_memory(C
->L
);
465 if (!slang_struct_construct(*st
)) {
468 slang_info_log_memory(C
->L
);
471 (**st
).a_name
= a_name
;
472 (**st
).structs
->outer_scope
= O
->structs
;
474 /* parse individual struct fields */
476 slang_type_specifier sp
;
478 slang_type_specifier_ctr(&sp
);
479 if (!parse_struct_field(C
, O
, *st
, &sp
)) {
480 slang_type_specifier_dtr(&sp
);
483 slang_type_specifier_dtr(&sp
);
485 while (*C
->I
++ != FIELD_NONE
);
487 /* if named struct, copy it to current scope */
488 if (name
[0] != '\0') {
491 O
->structs
->structs
=
492 (slang_struct
*) _slang_realloc(O
->structs
->structs
,
493 O
->structs
->num_structs
494 * sizeof(slang_struct
),
495 (O
->structs
->num_structs
+ 1)
496 * sizeof(slang_struct
));
497 if (O
->structs
->structs
== NULL
) {
498 slang_info_log_memory(C
->L
);
501 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
502 if (!slang_struct_construct(s
))
504 O
->structs
->num_structs
++;
505 if (!slang_struct_copy(s
, *st
))
513 /* invariant qualifer */
514 #define TYPE_VARIANT 90
515 #define TYPE_INVARIANT 91
518 parse_type_variant(slang_parse_ctx
* C
, slang_type_variant
*variant
)
520 GLuint invariant
= *C
->I
++;
523 *variant
= SLANG_VARIANT
;
526 *variant
= SLANG_INVARIANT
;
534 /* centroid qualifer */
535 #define TYPE_CENTER 95
536 #define TYPE_CENTROID 96
539 parse_type_centroid(slang_parse_ctx
* C
, slang_type_centroid
*centroid
)
544 *centroid
= SLANG_CENTER
;
547 *centroid
= SLANG_CENTROID
;
556 #define TYPE_QUALIFIER_NONE 0
557 #define TYPE_QUALIFIER_CONST 1
558 #define TYPE_QUALIFIER_ATTRIBUTE 2
559 #define TYPE_QUALIFIER_VARYING 3
560 #define TYPE_QUALIFIER_UNIFORM 4
561 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
562 #define TYPE_QUALIFIER_FIXEDINPUT 6
565 parse_type_qualifier(slang_parse_ctx
* C
, slang_type_qualifier
* qual
)
567 GLuint qualifier
= *C
->I
++;
569 case TYPE_QUALIFIER_NONE
:
570 *qual
= SLANG_QUAL_NONE
;
572 case TYPE_QUALIFIER_CONST
:
573 *qual
= SLANG_QUAL_CONST
;
575 case TYPE_QUALIFIER_ATTRIBUTE
:
576 *qual
= SLANG_QUAL_ATTRIBUTE
;
578 case TYPE_QUALIFIER_VARYING
:
579 *qual
= SLANG_QUAL_VARYING
;
581 case TYPE_QUALIFIER_UNIFORM
:
582 *qual
= SLANG_QUAL_UNIFORM
;
584 case TYPE_QUALIFIER_FIXEDOUTPUT
:
585 *qual
= SLANG_QUAL_FIXEDOUTPUT
;
587 case TYPE_QUALIFIER_FIXEDINPUT
:
588 *qual
= SLANG_QUAL_FIXEDINPUT
;
597 #define TYPE_SPECIFIER_VOID 0
598 #define TYPE_SPECIFIER_BOOL 1
599 #define TYPE_SPECIFIER_BVEC2 2
600 #define TYPE_SPECIFIER_BVEC3 3
601 #define TYPE_SPECIFIER_BVEC4 4
602 #define TYPE_SPECIFIER_INT 5
603 #define TYPE_SPECIFIER_IVEC2 6
604 #define TYPE_SPECIFIER_IVEC3 7
605 #define TYPE_SPECIFIER_IVEC4 8
606 #define TYPE_SPECIFIER_FLOAT 9
607 #define TYPE_SPECIFIER_VEC2 10
608 #define TYPE_SPECIFIER_VEC3 11
609 #define TYPE_SPECIFIER_VEC4 12
610 #define TYPE_SPECIFIER_MAT2 13
611 #define TYPE_SPECIFIER_MAT3 14
612 #define TYPE_SPECIFIER_MAT4 15
613 #define TYPE_SPECIFIER_SAMPLER1D 16
614 #define TYPE_SPECIFIER_SAMPLER2D 17
615 #define TYPE_SPECIFIER_SAMPLER3D 18
616 #define TYPE_SPECIFIER_SAMPLERCUBE 19
617 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
618 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
619 #define TYPE_SPECIFIER_SAMPLER2DRECT 22
620 #define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
621 #define TYPE_SPECIFIER_STRUCT 24
622 #define TYPE_SPECIFIER_TYPENAME 25
623 #define TYPE_SPECIFIER_MAT23 26
624 #define TYPE_SPECIFIER_MAT32 27
625 #define TYPE_SPECIFIER_MAT24 28
626 #define TYPE_SPECIFIER_MAT42 29
627 #define TYPE_SPECIFIER_MAT34 30
628 #define TYPE_SPECIFIER_MAT43 31
629 #define TYPE_SPECIFIER_COUNT 32
632 parse_type_specifier(slang_parse_ctx
* C
, slang_output_ctx
* O
,
633 slang_type_specifier
* spec
)
636 case TYPE_SPECIFIER_VOID
:
637 spec
->type
= SLANG_SPEC_VOID
;
639 case TYPE_SPECIFIER_BOOL
:
640 spec
->type
= SLANG_SPEC_BOOL
;
642 case TYPE_SPECIFIER_BVEC2
:
643 spec
->type
= SLANG_SPEC_BVEC2
;
645 case TYPE_SPECIFIER_BVEC3
:
646 spec
->type
= SLANG_SPEC_BVEC3
;
648 case TYPE_SPECIFIER_BVEC4
:
649 spec
->type
= SLANG_SPEC_BVEC4
;
651 case TYPE_SPECIFIER_INT
:
652 spec
->type
= SLANG_SPEC_INT
;
654 case TYPE_SPECIFIER_IVEC2
:
655 spec
->type
= SLANG_SPEC_IVEC2
;
657 case TYPE_SPECIFIER_IVEC3
:
658 spec
->type
= SLANG_SPEC_IVEC3
;
660 case TYPE_SPECIFIER_IVEC4
:
661 spec
->type
= SLANG_SPEC_IVEC4
;
663 case TYPE_SPECIFIER_FLOAT
:
664 spec
->type
= SLANG_SPEC_FLOAT
;
666 case TYPE_SPECIFIER_VEC2
:
667 spec
->type
= SLANG_SPEC_VEC2
;
669 case TYPE_SPECIFIER_VEC3
:
670 spec
->type
= SLANG_SPEC_VEC3
;
672 case TYPE_SPECIFIER_VEC4
:
673 spec
->type
= SLANG_SPEC_VEC4
;
675 case TYPE_SPECIFIER_MAT2
:
676 spec
->type
= SLANG_SPEC_MAT2
;
678 case TYPE_SPECIFIER_MAT3
:
679 spec
->type
= SLANG_SPEC_MAT3
;
681 case TYPE_SPECIFIER_MAT4
:
682 spec
->type
= SLANG_SPEC_MAT4
;
684 case TYPE_SPECIFIER_MAT23
:
685 spec
->type
= SLANG_SPEC_MAT23
;
687 case TYPE_SPECIFIER_MAT32
:
688 spec
->type
= SLANG_SPEC_MAT32
;
690 case TYPE_SPECIFIER_MAT24
:
691 spec
->type
= SLANG_SPEC_MAT24
;
693 case TYPE_SPECIFIER_MAT42
:
694 spec
->type
= SLANG_SPEC_MAT42
;
696 case TYPE_SPECIFIER_MAT34
:
697 spec
->type
= SLANG_SPEC_MAT34
;
699 case TYPE_SPECIFIER_MAT43
:
700 spec
->type
= SLANG_SPEC_MAT43
;
702 case TYPE_SPECIFIER_SAMPLER1D
:
703 spec
->type
= SLANG_SPEC_SAMPLER1D
;
705 case TYPE_SPECIFIER_SAMPLER2D
:
706 spec
->type
= SLANG_SPEC_SAMPLER2D
;
708 case TYPE_SPECIFIER_SAMPLER3D
:
709 spec
->type
= SLANG_SPEC_SAMPLER3D
;
711 case TYPE_SPECIFIER_SAMPLERCUBE
:
712 spec
->type
= SLANG_SPEC_SAMPLERCUBE
;
714 case TYPE_SPECIFIER_SAMPLER2DRECT
:
715 spec
->type
= SLANG_SPEC_SAMPLER2DRECT
;
717 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
718 spec
->type
= SLANG_SPEC_SAMPLER1DSHADOW
;
720 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
721 spec
->type
= SLANG_SPEC_SAMPLER2DSHADOW
;
723 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
724 spec
->type
= SLANG_SPEC_SAMPLER2DRECTSHADOW
;
726 case TYPE_SPECIFIER_STRUCT
:
727 spec
->type
= SLANG_SPEC_STRUCT
;
728 if (!parse_struct(C
, O
, &spec
->_struct
))
731 case TYPE_SPECIFIER_TYPENAME
:
732 spec
->type
= SLANG_SPEC_STRUCT
;
737 a_name
= parse_identifier(C
);
741 stru
= slang_struct_scope_find(O
->structs
, a_name
, 1);
743 slang_info_log_error(C
->L
, "undeclared type name '%s'",
744 slang_atom_pool_id(C
->atoms
, a_name
));
748 spec
->_struct
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
749 if (spec
->_struct
== NULL
) {
750 slang_info_log_memory(C
->L
);
753 if (!slang_struct_construct(spec
->_struct
)) {
754 _slang_free(spec
->_struct
);
755 spec
->_struct
= NULL
;
758 if (!slang_struct_copy(spec
->_struct
, stru
))
768 #define TYPE_SPECIFIER_NONARRAY 0
769 #define TYPE_SPECIFIER_ARRAY 1
772 parse_type_array_size(slang_parse_ctx
*C
,
779 case TYPE_SPECIFIER_NONARRAY
:
780 *array_len
= -1; /* -1 = not an array */
782 case TYPE_SPECIFIER_ARRAY
:
783 if (!parse_array_len(C
, O
, &size
))
785 *array_len
= (GLint
) size
;
794 #define PRECISION_DEFAULT 0
795 #define PRECISION_LOW 1
796 #define PRECISION_MEDIUM 2
797 #define PRECISION_HIGH 3
800 parse_type_precision(slang_parse_ctx
*C
,
801 slang_type_precision
*precision
)
803 GLint prec
= *C
->I
++;
805 case PRECISION_DEFAULT
:
806 *precision
= SLANG_PREC_DEFAULT
;
809 *precision
= SLANG_PREC_LOW
;
811 case PRECISION_MEDIUM
:
812 *precision
= SLANG_PREC_MEDIUM
;
815 *precision
= SLANG_PREC_HIGH
;
823 parse_fully_specified_type(slang_parse_ctx
* C
, slang_output_ctx
* O
,
824 slang_fully_specified_type
* type
)
826 if (!parse_type_variant(C
, &type
->variant
))
829 if (!parse_type_centroid(C
, &type
->centroid
))
832 if (!parse_type_qualifier(C
, &type
->qualifier
))
835 if (!parse_type_precision(C
, &type
->precision
))
838 if (!parse_type_specifier(C
, O
, &type
->specifier
))
841 if (!parse_type_array_size(C
, O
, &type
->array_len
))
844 if (!O
->allow_invariant
&& type
->variant
== SLANG_INVARIANT
) {
845 slang_info_log_error(C
->L
,
846 "'invariant' keyword not allowed (perhaps set #version 120)");
850 if (!O
->allow_centroid
&& type
->centroid
== SLANG_CENTROID
) {
851 slang_info_log_error(C
->L
,
852 "'centroid' keyword not allowed (perhaps set #version 120)");
855 else if (type
->centroid
== SLANG_CENTROID
&&
856 type
->qualifier
!= SLANG_QUAL_VARYING
) {
857 slang_info_log_error(C
->L
,
858 "'centroid' keyword only allowed for varying vars");
864 if (type->qualifier != SLANG_QUAL_VARYING &&
865 type->variant == SLANG_INVARIANT) {
866 slang_info_log_error(C->L,
867 "invariant qualifer only allowed for varying vars");
872 if (O
->allow_precision
) {
873 if (type
->precision
== SLANG_PREC_DEFAULT
) {
874 assert(type
->specifier
.type
< TYPE_SPECIFIER_COUNT
);
875 /* use the default precision for this datatype */
876 type
->precision
= O
->default_precision
[type
->specifier
.type
];
880 /* only default is allowed */
881 if (type
->precision
!= SLANG_PREC_DEFAULT
) {
882 slang_info_log_error(C
->L
, "precision qualifiers not allowed");
887 if (!O
->allow_array_types
&& type
->array_len
>= 0) {
888 slang_info_log_error(C
->L
, "first-class array types not allowed");
892 if (type
->array_len
>= 0) {
893 /* convert type to array type (ex: convert "int" to "array of int" */
894 promote_type_to_array(C
, type
, type
->array_len
);
902 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
903 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
907 #define OP_CONTINUE 6
910 #define OP_EXPRESSION 9
915 #define OP_PUSH_VOID 14
916 #define OP_PUSH_BOOL 15
917 #define OP_PUSH_INT 16
918 #define OP_PUSH_FLOAT 17
919 #define OP_PUSH_IDENTIFIER 18
920 #define OP_SEQUENCE 19
922 #define OP_ADDASSIGN 21
923 #define OP_SUBASSIGN 22
924 #define OP_MULASSIGN 23
925 #define OP_DIVASSIGN 24
926 /*#define OP_MODASSIGN 25*/
927 /*#define OP_LSHASSIGN 26*/
928 /*#define OP_RSHASSIGN 27*/
929 /*#define OP_ORASSIGN 28*/
930 /*#define OP_XORASSIGN 29*/
931 /*#define OP_ANDASSIGN 30*/
933 #define OP_LOGICALOR 32
934 #define OP_LOGICALXOR 33
935 #define OP_LOGICALAND 34
936 /*#define OP_BITOR 35*/
937 /*#define OP_BITXOR 36*/
938 /*#define OP_BITAND 37*/
940 #define OP_NOTEQUAL 39
942 #define OP_GREATER 41
943 #define OP_LESSEQUAL 42
944 #define OP_GREATEREQUAL 43
945 /*#define OP_LSHIFT 44*/
946 /*#define OP_RSHIFT 45*/
948 #define OP_SUBTRACT 47
949 #define OP_MULTIPLY 48
951 /*#define OP_MODULUS 50*/
952 #define OP_PREINCREMENT 51
953 #define OP_PREDECREMENT 52
956 /*#define OP_COMPLEMENT 55*/
958 #define OP_SUBSCRIPT 57
961 #define OP_POSTINCREMENT 60
962 #define OP_POSTDECREMENT 61
963 #define OP_PRECISION 62
968 * When parsing a compound production, this function is used to parse the
970 * For example, a while-loop compound will have two children, the
971 * while condition expression and the loop body. So, this function will
972 * be called twice to parse those two sub-expressions.
973 * \param C the parsing context
974 * \param O the output context
975 * \param oper the operation we're parsing
976 * \param statement indicates whether parsing a statement, or expression
977 * \return 1 if success, 0 if error
980 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
981 slang_operation
* oper
, GLboolean statement
)
985 /* grow child array */
986 ch
= slang_operation_grow(&oper
->num_children
, &oper
->children
);
988 return parse_statement(C
, O
, ch
);
989 return parse_expression(C
, O
, ch
);
992 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
995 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
996 slang_operation
* oper
)
1000 oper
->locals
->outer_scope
= O
->vars
;
1004 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
1005 /* parse child statements, do not create new variable scope */
1006 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1007 while (*C
->I
!= OP_END
)
1008 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1012 case OP_BLOCK_BEGIN_NEW_SCOPE
:
1013 /* parse child statements, create new variable scope */
1015 slang_output_ctx o
= *O
;
1017 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1018 o
.vars
= oper
->locals
;
1019 while (*C
->I
!= OP_END
)
1020 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1026 /* local variable declaration, individual declarators are stored as
1027 * children identifiers
1029 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1031 const unsigned int first_var
= O
->vars
->num_variables
;
1033 /* parse the declaration, note that there can be zero or more
1034 * than one declarators
1036 if (!parse_declaration(C
, O
))
1038 if (first_var
< O
->vars
->num_variables
) {
1039 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
1041 assert(oper
->num_children
== 0);
1042 oper
->num_children
= num_vars
;
1043 oper
->children
= slang_operation_new(num_vars
);
1044 if (oper
->children
== NULL
) {
1045 slang_info_log_memory(C
->L
);
1048 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
1049 slang_operation
*o
= &oper
->children
[i
- first_var
];
1050 slang_variable
*var
= O
->vars
->variables
[i
];
1051 o
->type
= SLANG_OPER_VARIABLE_DECL
;
1052 o
->locals
->outer_scope
= O
->vars
;
1053 o
->a_id
= var
->a_name
;
1056 calculate_var_size(C, O, var);
1059 if (!legal_identifier(o
->a_id
)) {
1060 slang_info_log_error(C
->L
, "illegal variable name '%s'",
1069 /* the __asm statement, parse the mnemonic and all its arguments
1072 oper
->type
= SLANG_OPER_ASM
;
1073 oper
->a_id
= parse_identifier(C
);
1074 if (oper
->a_id
== SLANG_ATOM_NULL
)
1076 while (*C
->I
!= OP_END
) {
1077 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1083 oper
->type
= SLANG_OPER_BREAK
;
1086 oper
->type
= SLANG_OPER_CONTINUE
;
1089 oper
->type
= SLANG_OPER_DISCARD
;
1092 oper
->type
= SLANG_OPER_RETURN
;
1093 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1097 oper
->type
= SLANG_OPER_EXPRESSION
;
1098 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1102 oper
->type
= SLANG_OPER_IF
;
1103 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1105 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1107 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1112 slang_output_ctx o
= *O
;
1114 oper
->type
= SLANG_OPER_WHILE
;
1115 o
.vars
= oper
->locals
;
1116 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1118 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1123 oper
->type
= SLANG_OPER_DO
;
1124 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1126 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1131 slang_output_ctx o
= *O
;
1133 oper
->type
= SLANG_OPER_FOR
;
1134 o
.vars
= oper
->locals
;
1135 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1137 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1139 if (!parse_child_operation(C
, &o
, oper
, GL_FALSE
))
1142 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1145 /* force creation of new scope for loop body */
1147 slang_operation
*ch
;
1148 slang_output_ctx oo
= o
;
1150 /* grow child array */
1151 ch
= slang_operation_grow(&oper
->num_children
, &oper
->children
);
1152 ch
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1154 ch
->locals
->outer_scope
= o
.vars
;
1155 oo
.vars
= ch
->locals
;
1157 if (!parse_child_operation(C
, &oo
, ch
, GL_TRUE
))
1165 /* set default precision for a type in this scope */
1166 /* ignored at this time */
1167 int prec_qual
= *C
->I
++;
1168 int datatype
= *C
->I
++;
1174 /*printf("Unexpected operation %d\n", op);*/
1181 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
1182 slang_operation
** ops
, unsigned int *total_ops
,
1187 op
->children
= slang_operation_new(n
);
1188 if (op
->children
== NULL
) {
1189 slang_info_log_memory(C
->L
);
1192 op
->num_children
= n
;
1194 for (i
= 0; i
< n
; i
++) {
1195 slang_operation_destruct(&op
->children
[i
]);
1196 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
1199 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
1202 *ops
= (slang_operation
*)
1203 _slang_realloc(*ops
,
1204 (*total_ops
+ n
) * sizeof(slang_operation
),
1205 *total_ops
* sizeof(slang_operation
));
1207 slang_info_log_memory(C
->L
);
1214 is_constructor_name(const char *name
, slang_atom a_name
,
1215 slang_struct_scope
* structs
)
1217 if (slang_type_specifier_type_from_string(name
) != SLANG_SPEC_VOID
)
1219 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
1222 #define FUNCTION_CALL_NONARRAY 0
1223 #define FUNCTION_CALL_ARRAY 1
1226 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1227 slang_operation
* oper
)
1229 slang_operation
*ops
= NULL
;
1230 unsigned int num_ops
= 0;
1233 while (*C
->I
!= OP_END
) {
1234 slang_operation
*op
;
1235 const unsigned int op_code
= *C
->I
++;
1237 /* allocate default operation, becomes a no-op if not used */
1238 ops
= (slang_operation
*)
1240 num_ops
* sizeof(slang_operation
),
1241 (num_ops
+ 1) * sizeof(slang_operation
));
1243 slang_info_log_memory(C
->L
);
1247 if (!slang_operation_construct(op
)) {
1248 slang_info_log_memory(C
->L
);
1252 op
->locals
->outer_scope
= O
->vars
;
1256 op
->type
= SLANG_OPER_VOID
;
1259 op
->type
= SLANG_OPER_LITERAL_BOOL
;
1260 if (!parse_number(C
, &number
))
1265 op
->literal
[3] = (GLfloat
) number
;
1266 op
->literal_size
= 1;
1269 op
->type
= SLANG_OPER_LITERAL_INT
;
1270 if (!parse_number(C
, &number
))
1275 op
->literal
[3] = (GLfloat
) number
;
1276 op
->literal_size
= 1;
1279 op
->type
= SLANG_OPER_LITERAL_FLOAT
;
1280 if (!parse_float(C
, &op
->literal
[0]))
1284 op
->literal
[3] = op
->literal
[0];
1285 op
->literal_size
= 1;
1287 case OP_PUSH_IDENTIFIER
:
1288 op
->type
= SLANG_OPER_IDENTIFIER
;
1289 op
->a_id
= parse_identifier(C
);
1290 if (op
->a_id
== SLANG_ATOM_NULL
)
1294 op
->type
= SLANG_OPER_SEQUENCE
;
1295 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1299 op
->type
= SLANG_OPER_ASSIGN
;
1300 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1304 op
->type
= SLANG_OPER_ADDASSIGN
;
1305 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1309 op
->type
= SLANG_OPER_SUBASSIGN
;
1310 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1314 op
->type
= SLANG_OPER_MULASSIGN
;
1315 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1319 op
->type
= SLANG_OPER_DIVASSIGN
;
1320 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1323 /*case OP_MODASSIGN: */
1324 /*case OP_LSHASSIGN: */
1325 /*case OP_RSHASSIGN: */
1326 /*case OP_ORASSIGN: */
1327 /*case OP_XORASSIGN: */
1328 /*case OP_ANDASSIGN: */
1330 op
->type
= SLANG_OPER_SELECT
;
1331 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1335 op
->type
= SLANG_OPER_LOGICALOR
;
1336 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1340 op
->type
= SLANG_OPER_LOGICALXOR
;
1341 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1345 op
->type
= SLANG_OPER_LOGICALAND
;
1346 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1350 /*case OP_BITXOR: */
1351 /*case OP_BITAND: */
1353 op
->type
= SLANG_OPER_EQUAL
;
1354 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1358 op
->type
= SLANG_OPER_NOTEQUAL
;
1359 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1363 op
->type
= SLANG_OPER_LESS
;
1364 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1368 op
->type
= SLANG_OPER_GREATER
;
1369 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1373 op
->type
= SLANG_OPER_LESSEQUAL
;
1374 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1377 case OP_GREATEREQUAL
:
1378 op
->type
= SLANG_OPER_GREATEREQUAL
;
1379 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1382 /*case OP_LSHIFT: */
1383 /*case OP_RSHIFT: */
1385 op
->type
= SLANG_OPER_ADD
;
1386 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1390 op
->type
= SLANG_OPER_SUBTRACT
;
1391 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1395 op
->type
= SLANG_OPER_MULTIPLY
;
1396 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1400 op
->type
= SLANG_OPER_DIVIDE
;
1401 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1404 /*case OP_MODULUS: */
1405 case OP_PREINCREMENT
:
1406 op
->type
= SLANG_OPER_PREINCREMENT
;
1407 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1410 case OP_PREDECREMENT
:
1411 op
->type
= SLANG_OPER_PREDECREMENT
;
1412 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1416 op
->type
= SLANG_OPER_PLUS
;
1417 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1421 op
->type
= SLANG_OPER_MINUS
;
1422 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1426 op
->type
= SLANG_OPER_NOT
;
1427 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1430 /*case OP_COMPLEMENT: */
1432 op
->type
= SLANG_OPER_SUBSCRIPT
;
1433 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1437 op
->type
= SLANG_OPER_METHOD
;
1438 op
->a_obj
= parse_identifier(C
);
1439 if (op
->a_obj
== SLANG_ATOM_NULL
)
1442 op
->a_id
= parse_identifier(C
);
1443 if (op
->a_id
== SLANG_ATOM_NULL
)
1446 assert(*C
->I
== OP_END
);
1449 while (*C
->I
!= OP_END
)
1450 if (!parse_child_operation(C
, O
, op
, GL_FALSE
))
1454 /* don't lookup the method (not yet anyway) */
1455 if (!C
->parsing_builtin
1456 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1459 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1460 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1461 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1469 GLboolean array_constructor
= GL_FALSE
;
1470 GLint array_constructor_size
;
1472 op
->type
= SLANG_OPER_CALL
;
1473 op
->a_id
= parse_identifier(C
);
1474 if (op
->a_id
== SLANG_ATOM_NULL
)
1477 case FUNCTION_CALL_NONARRAY
:
1478 /* Nothing to do. */
1480 case FUNCTION_CALL_ARRAY
:
1481 /* Calling an array constructor. For example:
1482 * float[3](1.1, 2.2, 3.3);
1484 if (!O
->allow_array_types
) {
1485 slang_info_log_error(C
->L
,
1486 "array constructors not allowed "
1487 "in this GLSL version");
1491 array_constructor
= GL_TRUE
;
1492 /* parse the array constructor size */
1493 slang_operation array_size
;
1494 slang_operation_construct(&array_size
);
1495 if (!parse_expression(C
, O
, &array_size
)) {
1496 slang_operation_destruct(&array_size
);
1499 if (array_size
.type
!= SLANG_OPER_LITERAL_INT
) {
1500 slang_info_log_error(C
->L
,
1501 "constructor array size is not an integer");
1502 slang_operation_destruct(&array_size
);
1505 array_constructor_size
= (int) array_size
.literal
[0];
1506 op
->array_constructor
= GL_TRUE
;
1507 slang_operation_destruct(&array_size
);
1514 while (*C
->I
!= OP_END
)
1515 if (!parse_child_operation(C
, O
, op
, GL_FALSE
))
1519 if (array_constructor
&&
1520 array_constructor_size
!= op
->num_children
) {
1521 slang_info_log_error(C
->L
, "number of parameters to array"
1522 " constructor does not match array size");
1526 if (!C
->parsing_builtin
1527 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1530 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1531 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1532 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1539 op
->type
= SLANG_OPER_FIELD
;
1540 op
->a_id
= parse_identifier(C
);
1541 if (op
->a_id
== SLANG_ATOM_NULL
)
1543 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1546 case OP_POSTINCREMENT
:
1547 op
->type
= SLANG_OPER_POSTINCREMENT
;
1548 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1551 case OP_POSTDECREMENT
:
1552 op
->type
= SLANG_OPER_POSTDECREMENT
;
1553 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1562 slang_operation_destruct(oper
);
1563 *oper
= *ops
; /* struct copy */
1569 /* parameter qualifier */
1570 #define PARAM_QUALIFIER_IN 0
1571 #define PARAM_QUALIFIER_OUT 1
1572 #define PARAM_QUALIFIER_INOUT 2
1574 /* function parameter array presence */
1575 #define PARAMETER_ARRAY_NOT_PRESENT 0
1576 #define PARAMETER_ARRAY_PRESENT 1
1579 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1580 slang_variable
* param
)
1582 int param_qual
, precision_qual
;
1584 /* parse and validate the parameter's type qualifiers (there can be
1585 * two at most) because not all combinations are valid
1587 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1590 param_qual
= *C
->I
++;
1591 switch (param_qual
) {
1592 case PARAM_QUALIFIER_IN
:
1593 if (param
->type
.qualifier
!= SLANG_QUAL_CONST
1594 && param
->type
.qualifier
!= SLANG_QUAL_NONE
) {
1595 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1599 case PARAM_QUALIFIER_OUT
:
1600 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1601 param
->type
.qualifier
= SLANG_QUAL_OUT
;
1603 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1607 case PARAM_QUALIFIER_INOUT
:
1608 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1609 param
->type
.qualifier
= SLANG_QUAL_INOUT
;
1611 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1619 /* parse precision qualifier (lowp, mediump, highp */
1620 precision_qual
= *C
->I
++;
1621 /* ignored at this time */
1622 (void) precision_qual
;
1624 /* parse parameter's type specifier and name */
1625 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1627 if (!parse_type_array_size(C
, O
, ¶m
->type
.array_len
))
1629 param
->a_name
= parse_identifier(C
);
1630 if (param
->a_name
== SLANG_ATOM_NULL
)
1633 /* first-class array
1635 if (param
->type
.array_len
>= 0) {
1636 slang_type_specifier p
;
1638 slang_type_specifier_ctr(&p
);
1639 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1640 slang_type_specifier_dtr(&p
);
1643 if (!convert_to_array(C
, param
, &p
)) {
1644 slang_type_specifier_dtr(&p
);
1647 slang_type_specifier_dtr(&p
);
1648 param
->array_len
= param
->type
.array_len
;
1651 /* if the parameter is an array, parse its size (the size must be
1652 * explicitly defined
1654 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1655 slang_type_specifier p
;
1657 if (param
->type
.array_len
>= 0) {
1658 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
1661 slang_type_specifier_ctr(&p
);
1662 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1663 slang_type_specifier_dtr(&p
);
1666 if (!convert_to_array(C
, param
, &p
)) {
1667 slang_type_specifier_dtr(&p
);
1670 slang_type_specifier_dtr(&p
);
1671 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1676 /* calculate the parameter size */
1677 if (!calculate_var_size(C
, O
, param
))
1680 /* TODO: allocate the local address here? */
1685 #define FUNCTION_ORDINARY 0
1686 #define FUNCTION_CONSTRUCTOR 1
1687 #define FUNCTION_OPERATOR 2
1689 /* function parameter */
1690 #define PARAMETER_NONE 0
1691 #define PARAMETER_NEXT 1
1694 #define OPERATOR_ADDASSIGN 1
1695 #define OPERATOR_SUBASSIGN 2
1696 #define OPERATOR_MULASSIGN 3
1697 #define OPERATOR_DIVASSIGN 4
1698 /*#define OPERATOR_MODASSIGN 5*/
1699 /*#define OPERATOR_LSHASSIGN 6*/
1700 /*#define OPERATOR_RSHASSIGN 7*/
1701 /*#define OPERATOR_ANDASSIGN 8*/
1702 /*#define OPERATOR_XORASSIGN 9*/
1703 /*#define OPERATOR_ORASSIGN 10*/
1704 #define OPERATOR_LOGICALXOR 11
1705 /*#define OPERATOR_BITOR 12*/
1706 /*#define OPERATOR_BITXOR 13*/
1707 /*#define OPERATOR_BITAND 14*/
1708 #define OPERATOR_LESS 15
1709 #define OPERATOR_GREATER 16
1710 #define OPERATOR_LESSEQUAL 17
1711 #define OPERATOR_GREATEREQUAL 18
1712 /*#define OPERATOR_LSHIFT 19*/
1713 /*#define OPERATOR_RSHIFT 20*/
1714 #define OPERATOR_MULTIPLY 21
1715 #define OPERATOR_DIVIDE 22
1716 /*#define OPERATOR_MODULUS 23*/
1717 #define OPERATOR_INCREMENT 24
1718 #define OPERATOR_DECREMENT 25
1719 #define OPERATOR_PLUS 26
1720 #define OPERATOR_MINUS 27
1721 /*#define OPERATOR_COMPLEMENT 28*/
1722 #define OPERATOR_NOT 29
1726 unsigned int o_code
;
1728 } operator_names
[] = {
1729 {OPERATOR_INCREMENT
, "++"},
1730 {OPERATOR_ADDASSIGN
, "+="},
1731 {OPERATOR_PLUS
, "+"},
1732 {OPERATOR_DECREMENT
, "--"},
1733 {OPERATOR_SUBASSIGN
, "-="},
1734 {OPERATOR_MINUS
, "-"},
1735 {OPERATOR_NOT
, "!"},
1736 {OPERATOR_MULASSIGN
, "*="},
1737 {OPERATOR_MULTIPLY
, "*"},
1738 {OPERATOR_DIVASSIGN
, "/="},
1739 {OPERATOR_DIVIDE
, "/"},
1740 {OPERATOR_LESSEQUAL
, "<="},
1741 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1742 /*{ OPERATOR_LSHIFT, "<<" }, */
1743 {OPERATOR_LESS
, "<"},
1744 {OPERATOR_GREATEREQUAL
, ">="},
1745 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1746 /*{ OPERATOR_RSHIFT, ">>" }, */
1747 {OPERATOR_GREATER
, ">"},
1748 /*{ OPERATOR_MODASSIGN, "%=" }, */
1749 /*{ OPERATOR_MODULUS, "%" }, */
1750 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1751 /*{ OPERATOR_BITAND, "&" }, */
1752 /*{ OPERATOR_ORASSIGN, "|=" }, */
1753 /*{ OPERATOR_BITOR, "|" }, */
1754 /*{ OPERATOR_COMPLEMENT, "~" }, */
1755 /*{ OPERATOR_XORASSIGN, "^=" }, */
1756 {OPERATOR_LOGICALXOR
, "^^"},
1757 /*{ OPERATOR_BITXOR, "^" } */
1761 parse_operator_name(slang_parse_ctx
* C
)
1765 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1766 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1768 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1769 if (atom
== SLANG_ATOM_NULL
) {
1770 slang_info_log_memory(C
->L
);
1782 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1783 slang_function
* func
)
1786 /* parse function type and name */
1787 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1792 case FUNCTION_ORDINARY
:
1793 func
->kind
= SLANG_FUNC_ORDINARY
;
1794 func
->header
.a_name
= parse_identifier(C
);
1795 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1798 case FUNCTION_CONSTRUCTOR
:
1799 func
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1800 if (func
->header
.type
.specifier
.type
== SLANG_SPEC_STRUCT
)
1802 func
->header
.a_name
=
1803 slang_atom_pool_atom(C
->atoms
,
1804 slang_type_specifier_type_to_string
1805 (func
->header
.type
.specifier
.type
));
1806 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1807 slang_info_log_memory(C
->L
);
1811 case FUNCTION_OPERATOR
:
1812 func
->kind
= SLANG_FUNC_OPERATOR
;
1813 func
->header
.a_name
= parse_operator_name(C
);
1814 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1821 if (!legal_identifier(func
->header
.a_name
)) {
1822 slang_info_log_error(C
->L
, "illegal function name '%s'",
1823 (char *) func
->header
.a_name
);
1827 /* parse function parameters */
1828 while (*C
->I
++ == PARAMETER_NEXT
) {
1829 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1831 slang_info_log_memory(C
->L
);
1834 if (!parse_parameter_declaration(C
, O
, p
))
1838 /* if the function returns a value, append a hidden __retVal 'out'
1839 * parameter that corresponds to the return value.
1841 if (_slang_function_has_return_value(func
)) {
1842 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1843 slang_atom a_retVal
= slang_atom_pool_atom(C
->atoms
, "__retVal");
1845 p
->a_name
= a_retVal
;
1846 p
->type
= func
->header
.type
;
1847 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1850 /* function formal parameters and local variables share the same
1851 * scope, so save the information about param count in a seperate
1852 * place also link the scope to the global variable scope so when a
1853 * given identifier is not found here, the search process continues
1854 * in the global space
1856 func
->param_count
= func
->parameters
->num_variables
;
1857 func
->parameters
->outer_scope
= O
->vars
;
1863 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1864 slang_function
* func
)
1866 slang_output_ctx o
= *O
;
1868 if (!parse_function_prototype(C
, O
, func
))
1871 /* create function's body operation */
1872 func
->body
= (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1873 if (func
->body
== NULL
) {
1874 slang_info_log_memory(C
->L
);
1877 if (!slang_operation_construct(func
->body
)) {
1878 _slang_free(func
->body
);
1880 slang_info_log_memory(C
->L
);
1884 /* to parse the body the parse context is modified in order to
1885 * capture parsed variables into function's local variable scope
1887 C
->global_scope
= GL_FALSE
;
1888 o
.vars
= func
->parameters
;
1889 if (!parse_statement(C
, &o
, func
->body
))
1892 C
->global_scope
= GL_TRUE
;
1897 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1899 slang_operation op_id
, op_assign
;
1902 /* construct the left side of assignment */
1903 if (!slang_operation_construct(&op_id
))
1905 op_id
.type
= SLANG_OPER_IDENTIFIER
;
1906 op_id
.a_id
= var
->a_name
;
1908 /* put the variable into operation's scope */
1909 op_id
.locals
->variables
=
1910 (slang_variable
**) _slang_alloc(sizeof(slang_variable
*));
1911 if (op_id
.locals
->variables
== NULL
) {
1912 slang_operation_destruct(&op_id
);
1915 op_id
.locals
->num_variables
= 1;
1916 op_id
.locals
->variables
[0] = var
;
1918 /* construct the assignment expression */
1919 if (!slang_operation_construct(&op_assign
)) {
1920 op_id
.locals
->num_variables
= 0;
1921 slang_operation_destruct(&op_id
);
1924 op_assign
.type
= SLANG_OPER_ASSIGN
;
1925 op_assign
.children
=
1926 (slang_operation
*) _slang_alloc(2 * sizeof(slang_operation
));
1927 if (op_assign
.children
== NULL
) {
1928 slang_operation_destruct(&op_assign
);
1929 op_id
.locals
->num_variables
= 0;
1930 slang_operation_destruct(&op_id
);
1933 op_assign
.num_children
= 2;
1934 op_assign
.children
[0] = op_id
;
1935 op_assign
.children
[1] = *var
->initializer
;
1939 /* carefully destroy the operations */
1940 op_assign
.num_children
= 0;
1941 _slang_free(op_assign
.children
);
1942 op_assign
.children
= NULL
;
1943 slang_operation_destruct(&op_assign
);
1944 op_id
.locals
->num_variables
= 0;
1945 slang_operation_destruct(&op_id
);
1953 /* init declarator list */
1954 #define DECLARATOR_NONE 0
1955 #define DECLARATOR_NEXT 1
1957 /* variable declaration */
1958 #define VARIABLE_NONE 0
1959 #define VARIABLE_IDENTIFIER 1
1960 #define VARIABLE_INITIALIZER 2
1961 #define VARIABLE_ARRAY_EXPLICIT 3
1962 #define VARIABLE_ARRAY_UNKNOWN 4
1966 * Parse the initializer for a variable declaration.
1969 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1970 const slang_fully_specified_type
* type
)
1972 slang_variable
*var
;
1975 /* empty init declatator (without name, e.g. "float ;") */
1976 if (*C
->I
++ == VARIABLE_NONE
)
1979 a_name
= parse_identifier(C
);
1981 /* check if name is already in this scope */
1982 if (_slang_variable_locate(O
->vars
, a_name
, GL_FALSE
)) {
1983 slang_info_log_error(C
->L
,
1984 "declaration of '%s' conflicts with previous declaration",
1989 /* make room for the new variable and initialize it */
1990 var
= slang_variable_scope_grow(O
->vars
);
1992 slang_info_log_memory(C
->L
);
1996 /* copy the declarator type qualifier/etc info, parse the identifier */
1997 var
->type
.qualifier
= type
->qualifier
;
1998 var
->type
.centroid
= type
->centroid
;
1999 var
->type
.precision
= type
->precision
;
2000 var
->type
.variant
= type
->variant
;
2001 var
->type
.array_len
= type
->array_len
;
2002 var
->a_name
= a_name
;
2003 if (var
->a_name
== SLANG_ATOM_NULL
)
2008 /* simple variable declarator - just copy the specifier */
2009 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
2012 case VARIABLE_INITIALIZER
:
2013 /* initialized variable - copy the specifier and parse the expression */
2014 if (0 && type
->array_len
>= 0) {
2015 /* The type was something like "float[4]" */
2016 convert_to_array(C
, var
, &type
->specifier
);
2017 var
->array_len
= type
->array_len
;
2020 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
2024 (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
2025 if (var
->initializer
== NULL
) {
2026 slang_info_log_memory(C
->L
);
2029 if (!slang_operation_construct(var
->initializer
)) {
2030 _slang_free(var
->initializer
);
2031 var
->initializer
= NULL
;
2032 slang_info_log_memory(C
->L
);
2035 if (!parse_expression(C
, O
, var
->initializer
))
2038 case VARIABLE_ARRAY_UNKNOWN
:
2039 /* unsized array - mark it as array and copy the specifier to
2042 if (type
->array_len
>= 0) {
2043 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
2046 if (!convert_to_array(C
, var
, &type
->specifier
))
2049 case VARIABLE_ARRAY_EXPLICIT
:
2050 if (type
->array_len
>= 0) {
2051 /* the user is trying to do something like: float[2] x[3]; */
2052 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
2055 if (!convert_to_array(C
, var
, &type
->specifier
))
2057 if (!parse_array_len(C
, O
, &var
->array_len
))
2064 /* allocate global address space for a variable with a known size */
2066 && !(var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
2067 && var
->array_len
== 0)) {
2068 if (!calculate_var_size(C
, O
, var
))
2072 /* emit code for global var decl */
2073 if (C
->global_scope
) {
2074 slang_assemble_ctx A
;
2076 A
.space
.funcs
= O
->funs
;
2077 A
.space
.structs
= O
->structs
;
2078 A
.space
.vars
= O
->vars
;
2079 A
.program
= O
->program
;
2080 A
.vartable
= O
->vartable
;
2082 A
.curFuncEndLabel
= NULL
;
2083 if (!_slang_codegen_global_variable(&A
, var
, C
->type
))
2087 /* initialize global variable */
2088 if (C
->global_scope
) {
2089 if (var
->initializer
!= NULL
) {
2090 slang_assemble_ctx A
;
2093 A
.space
.funcs
= O
->funs
;
2094 A
.space
.structs
= O
->structs
;
2095 A
.space
.vars
= O
->vars
;
2096 if (!initialize_global(&A
, var
))
2104 * Parse a list of variable declarations. Each variable may have an
2108 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2110 slang_fully_specified_type type
;
2112 /* parse the fully specified type, common to all declarators */
2113 if (!slang_fully_specified_type_construct(&type
))
2115 if (!parse_fully_specified_type(C
, O
, &type
)) {
2116 slang_fully_specified_type_destruct(&type
);
2120 /* parse declarators, pass-in the parsed type */
2122 if (!parse_init_declarator(C
, O
, &type
)) {
2123 slang_fully_specified_type_destruct(&type
);
2127 while (*C
->I
++ == DECLARATOR_NEXT
);
2129 slang_fully_specified_type_destruct(&type
);
2135 * Parse a function definition or declaration.
2136 * \param C parsing context
2137 * \param O output context
2138 * \param definition if non-zero expect a definition, else a declaration
2139 * \param parsed_func_ret returns the parsed function
2140 * \return GL_TRUE if success, GL_FALSE if failure
2143 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
2144 slang_function
** parsed_func_ret
)
2146 slang_function parsed_func
, *found_func
;
2148 /* parse function definition/declaration */
2149 if (!slang_function_construct(&parsed_func
))
2152 if (!parse_function_definition(C
, O
, &parsed_func
)) {
2153 slang_function_destruct(&parsed_func
);
2158 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
2159 slang_function_destruct(&parsed_func
);
2164 /* find a function with a prototype matching the parsed one - only
2165 * the current scope is being searched to allow built-in function
2168 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
2169 if (found_func
== NULL
) {
2170 /* New function, add it to the function list */
2171 O
->funs
->functions
=
2172 (slang_function
*) _slang_realloc(O
->funs
->functions
,
2173 O
->funs
->num_functions
2174 * sizeof(slang_function
),
2175 (O
->funs
->num_functions
+ 1)
2176 * sizeof(slang_function
));
2177 if (O
->funs
->functions
== NULL
) {
2178 slang_info_log_memory(C
->L
);
2179 slang_function_destruct(&parsed_func
);
2182 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
2183 O
->funs
->num_functions
++;
2185 /* return the newly parsed function */
2186 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
2189 /* previously defined or declared */
2190 /* TODO: check function return type qualifiers and specifiers */
2192 if (found_func
->body
!= NULL
) {
2193 slang_info_log_error(C
->L
, "%s: function already has a body.",
2194 slang_atom_pool_id(C
->atoms
,
2197 slang_function_destruct(&parsed_func
);
2201 /* destroy the existing function declaration and replace it
2204 slang_function_destruct(found_func
);
2205 *found_func
= parsed_func
;
2208 /* another declaration of the same function prototype - ignore it */
2209 slang_function_destruct(&parsed_func
);
2212 /* return the found function */
2213 *parsed_func_ret
= found_func
;
2220 #define DECLARATION_FUNCTION_PROTOTYPE 1
2221 #define DECLARATION_INIT_DECLARATOR_LIST 2
2224 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2227 case DECLARATION_INIT_DECLARATOR_LIST
:
2228 if (!parse_init_declarator_list(C
, O
))
2231 case DECLARATION_FUNCTION_PROTOTYPE
:
2233 slang_function
*dummy_func
;
2235 if (!parse_function(C
, O
, 0, &dummy_func
))
2246 parse_default_precision(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2248 int precision
, type
;
2250 if (!O
->allow_precision
) {
2251 slang_info_log_error(C
->L
, "syntax error at \"precision\"");
2255 precision
= *C
->I
++;
2256 switch (precision
) {
2258 case PRECISION_MEDIUM
:
2259 case PRECISION_HIGH
:
2263 _mesa_problem(NULL
, "unexpected precision %d at %s:%d\n",
2264 precision
, __FILE__
, __LINE__
);
2270 case TYPE_SPECIFIER_FLOAT
:
2271 case TYPE_SPECIFIER_INT
:
2272 case TYPE_SPECIFIER_SAMPLER1D
:
2273 case TYPE_SPECIFIER_SAMPLER2D
:
2274 case TYPE_SPECIFIER_SAMPLER3D
:
2275 case TYPE_SPECIFIER_SAMPLERCUBE
:
2276 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
2277 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
2278 case TYPE_SPECIFIER_SAMPLER2DRECT
:
2279 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
2283 _mesa_problem(NULL
, "unexpected type %d at %s:%d\n",
2284 type
, __FILE__
, __LINE__
);
2288 assert(type
< TYPE_SPECIFIER_COUNT
);
2289 O
->default_precision
[type
] = precision
;
2296 * Initialize the default precision for all types.
2297 * XXX this info isn't used yet.
2300 init_default_precision(slang_output_ctx
*O
, slang_unit_type type
)
2303 for (i
= 0; i
< TYPE_SPECIFIER_COUNT
; i
++) {
2304 #if FEATURE_es2_glsl
2305 O
->default_precision
[i
] = PRECISION_LOW
;
2307 O
->default_precision
[i
] = PRECISION_HIGH
;
2311 if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2312 O
->default_precision
[TYPE_SPECIFIER_FLOAT
] = PRECISION_HIGH
;
2313 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_HIGH
;
2316 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_MEDIUM
;
2322 parse_invariant(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2324 if (O
->allow_invariant
) {
2325 slang_atom
*a
= parse_identifier(C
);
2326 /* XXX not doing anything with this var yet */
2327 /*printf("ID: %s\n", (char*) a);*/
2331 slang_info_log_error(C
->L
, "syntax error at \"invariant\"");
2337 /* external declaration or default precision specifier */
2338 #define EXTERNAL_NULL 0
2339 #define EXTERNAL_FUNCTION_DEFINITION 1
2340 #define EXTERNAL_DECLARATION 2
2341 #define DEFAULT_PRECISION 3
2342 #define INVARIANT_STMT 4
2346 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
,
2347 struct gl_shader
*shader
)
2349 GET_CURRENT_CONTEXT(ctx
);
2353 slang_function
*mainFunc
= NULL
;
2355 if (unit
->type
== SLANG_UNIT_FRAGMENT_BUILTIN
||
2356 unit
->type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2357 maxRegs
= ctx
->Const
.FragmentProgram
.MaxTemps
;
2360 assert(unit
->type
== SLANG_UNIT_VERTEX_BUILTIN
||
2361 unit
->type
== SLANG_UNIT_VERTEX_SHADER
);
2362 maxRegs
= ctx
->Const
.VertexProgram
.MaxTemps
;
2365 /* setup output context */
2366 o
.funs
= &unit
->funs
;
2367 o
.structs
= &unit
->structs
;
2368 o
.vars
= &unit
->vars
;
2369 o
.program
= shader
? shader
->Program
: NULL
;
2370 o
.vartable
= _slang_new_var_table(maxRegs
);
2371 _slang_push_var_table(o
.vartable
);
2373 /* allow 'invariant' keyword? */
2374 #if FEATURE_es2_glsl
2375 o
.allow_invariant
= GL_TRUE
;
2377 o
.allow_invariant
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2380 /* allow 'centroid' keyword? */
2381 o
.allow_centroid
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2383 /* allow 'lowp/mediump/highp' keywords? */
2384 #if FEATURE_es2_glsl
2385 o
.allow_precision
= GL_TRUE
;
2387 o
.allow_precision
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2389 init_default_precision(&o
, unit
->type
);
2391 /* allow 'float[]' keyword? */
2392 o
.allow_array_types
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2394 /* parse individual functions and declarations */
2395 while (*C
->I
!= EXTERNAL_NULL
) {
2397 case EXTERNAL_FUNCTION_DEFINITION
:
2399 slang_function
*func
;
2400 success
= parse_function(C
, &o
, 1, &func
);
2402 _mesa_strcmp((char *) func
->header
.a_name
, "main") == 0) {
2408 case EXTERNAL_DECLARATION
:
2409 success
= parse_declaration(C
, &o
);
2411 case DEFAULT_PRECISION
:
2412 success
= parse_default_precision(C
, &o
);
2414 case INVARIANT_STMT
:
2415 success
= parse_invariant(C
, &o
);
2422 /* xxx free codegen */
2423 _slang_pop_var_table(o
.vartable
);
2430 /* assemble (generate code) for main() */
2431 slang_assemble_ctx A
;
2434 A
.space
.funcs
= o
.funs
;
2435 A
.space
.structs
= o
.structs
;
2436 A
.space
.vars
= o
.vars
;
2437 A
.program
= o
.program
;
2438 A
.vartable
= o
.vartable
;
2441 /* main() takes no parameters */
2442 if (mainFunc
->param_count
> 0) {
2443 slang_info_log_error(A
.log
, "main() takes no arguments");
2447 _slang_codegen_function(&A
, mainFunc
);
2449 shader
->Main
= GL_TRUE
; /* this shader defines main() */
2452 _slang_pop_var_table(o
.vartable
);
2453 _slang_delete_var_table(o
.vartable
);
2459 compile_binary(const byte
* prod
, slang_code_unit
* unit
,
2461 slang_unit_type type
, slang_info_log
* infolog
,
2462 slang_code_unit
* builtin
, slang_code_unit
* downlink
,
2463 struct gl_shader
*shader
)
2469 /* setup parse context */
2472 C
.parsing_builtin
= (builtin
== NULL
);
2473 C
.global_scope
= GL_TRUE
;
2474 C
.atoms
= &unit
->object
->atompool
;
2476 C
.version
= version
;
2478 if (!check_revision(&C
))
2481 if (downlink
!= NULL
) {
2482 unit
->vars
.outer_scope
= &downlink
->vars
;
2483 unit
->funs
.outer_scope
= &downlink
->funs
;
2484 unit
->structs
.outer_scope
= &downlink
->structs
;
2487 /* parse translation unit */
2488 return parse_code_unit(&C
, unit
, shader
);
2492 compile_with_grammar(grammar id
, const char *source
, slang_code_unit
* unit
,
2493 slang_unit_type type
, slang_info_log
* infolog
,
2494 slang_code_unit
* builtin
,
2495 struct gl_shader
*shader
,
2496 const struct gl_extensions
*extensions
)
2499 GLuint size
, start
, version
;
2500 slang_string preprocessed
;
2503 #if FEATURE_ARB_shading_language_120
2505 #elif FEATURE_es2_glsl
2511 /* First retrieve the version number. */
2512 if (!_slang_preprocess_version(source
, &version
, &start
, infolog
))
2515 if (version
> maxVersion
) {
2516 slang_info_log_error(infolog
,
2517 "language version %.2f is not supported.",
2522 /* Now preprocess the source string. */
2523 slang_string_init(&preprocessed
);
2524 if (!_slang_preprocess_directives(&preprocessed
, &source
[start
],
2525 infolog
, extensions
)) {
2526 slang_string_free(&preprocessed
);
2527 slang_info_log_error(infolog
, "failed to preprocess the source.");
2531 /* Finally check the syntax and generate its binary representation. */
2532 if (!grammar_fast_check(id
,
2533 (const byte
*) (slang_string_cstr(&preprocessed
)),
2534 &prod
, &size
, 65536)) {
2538 slang_string_free(&preprocessed
);
2539 grammar_get_last_error((byte
*) (buf
), sizeof(buf
), &pos
);
2540 slang_info_log_error(infolog
, buf
);
2541 /* syntax error (possibly in library code) */
2546 s
= (char *) _mesa_find_line_column((const GLubyte
*) source
,
2547 (const GLubyte
*) source
+ pos
,
2549 printf("Error on line %d, col %d: %s\n", line
, col
, s
);
2554 slang_string_free(&preprocessed
);
2556 /* Syntax is okay - translate it to internal representation. */
2557 if (!compile_binary(prod
, unit
, version
, type
, infolog
, builtin
,
2558 &builtin
[SLANG_BUILTIN_TOTAL
- 1],
2560 grammar_alloc_free(prod
);
2563 grammar_alloc_free(prod
);
2567 LONGSTRING
static const char *slang_shader_syn
=
2568 #include "library/slang_shader_syn.h"
2571 static const byte slang_core_gc
[] = {
2572 #include "library/slang_core_gc.h"
2575 static const byte slang_120_core_gc
[] = {
2576 #include "library/slang_120_core_gc.h"
2579 static const byte slang_120_fragment_gc
[] = {
2580 #include "library/slang_builtin_120_fragment_gc.h"
2583 static const byte slang_common_builtin_gc
[] = {
2584 #include "library/slang_common_builtin_gc.h"
2587 static const byte slang_fragment_builtin_gc
[] = {
2588 #include "library/slang_fragment_builtin_gc.h"
2591 static const byte slang_vertex_builtin_gc
[] = {
2592 #include "library/slang_vertex_builtin_gc.h"
2596 compile_object(grammar
* id
, const char *source
, slang_code_object
* object
,
2597 slang_unit_type type
, slang_info_log
* infolog
,
2598 struct gl_shader
*shader
,
2599 const struct gl_extensions
*extensions
)
2601 slang_code_unit
*builtins
= NULL
;
2602 GLuint base_version
= 110;
2604 /* load GLSL grammar */
2605 *id
= grammar_load_from_text((const byte
*) (slang_shader_syn
));
2610 grammar_get_last_error(buf
, 1024, &pos
);
2611 slang_info_log_error(infolog
, (const char *) (buf
));
2615 /* set shader type - the syntax is slightly different for different shaders */
2616 if (type
== SLANG_UNIT_FRAGMENT_SHADER
2617 || type
== SLANG_UNIT_FRAGMENT_BUILTIN
)
2618 grammar_set_reg8(*id
, (const byte
*) "shader_type", 1);
2620 grammar_set_reg8(*id
, (const byte
*) "shader_type", 2);
2622 /* enable language extensions */
2623 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2625 /* if parsing user-specified shader, load built-in library */
2626 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_VERTEX_SHADER
) {
2627 /* compile core functionality first */
2628 if (!compile_binary(slang_core_gc
,
2629 &object
->builtin
[SLANG_BUILTIN_CORE
],
2631 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2635 #if FEATURE_ARB_shading_language_120
2636 if (!compile_binary(slang_120_core_gc
,
2637 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2639 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2640 NULL
, &object
->builtin
[SLANG_BUILTIN_CORE
], NULL
))
2644 /* compile common functions and variables, link to core */
2645 if (!compile_binary(slang_common_builtin_gc
,
2646 &object
->builtin
[SLANG_BUILTIN_COMMON
],
2647 #if FEATURE_ARB_shading_language_120
2652 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2653 #if FEATURE_ARB_shading_language_120
2654 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2656 &object
->builtin
[SLANG_BUILTIN_CORE
],
2661 /* compile target-specific functions and variables, link to common */
2662 if (type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2663 if (!compile_binary(slang_fragment_builtin_gc
,
2664 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2666 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2667 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2669 #if FEATURE_ARB_shading_language_120
2670 if (!compile_binary(slang_120_fragment_gc
,
2671 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2673 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2674 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2678 else if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2679 if (!compile_binary(slang_vertex_builtin_gc
,
2680 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2682 SLANG_UNIT_VERTEX_BUILTIN
, infolog
, NULL
,
2683 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2687 /* disable language extensions */
2688 #if NEW_SLANG /* allow-built-ins */
2689 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2691 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 0);
2693 builtins
= object
->builtin
;
2696 /* compile the actual shader - pass-in built-in library for external shader */
2697 return compile_with_grammar(*id
, source
, &object
->unit
, type
, infolog
,
2698 builtins
, shader
, extensions
);
2703 compile_shader(GLcontext
*ctx
, slang_code_object
* object
,
2704 slang_unit_type type
, slang_info_log
* infolog
,
2705 struct gl_shader
*shader
)
2710 #if 0 /* for debug */
2711 _mesa_printf("********* COMPILE SHADER ***********\n");
2712 _mesa_printf("%s\n", shader
->Source
);
2713 _mesa_printf("************************************\n");
2716 assert(shader
->Program
);
2718 _slang_code_object_dtr(object
);
2719 _slang_code_object_ctr(object
);
2721 success
= compile_object(&id
, shader
->Source
, object
, type
, infolog
, shader
,
2724 grammar_destroy(id
);
2734 _slang_compile(GLcontext
*ctx
, struct gl_shader
*shader
)
2737 slang_info_log info_log
;
2738 slang_code_object obj
;
2739 slang_unit_type type
;
2741 if (shader
->Type
== GL_VERTEX_SHADER
) {
2742 type
= SLANG_UNIT_VERTEX_SHADER
;
2745 assert(shader
->Type
== GL_FRAGMENT_SHADER
);
2746 type
= SLANG_UNIT_FRAGMENT_SHADER
;
2749 if (!shader
->Source
)
2752 ctx
->Shader
.MemPool
= _slang_new_mempool(1024*1024);
2754 shader
->Main
= GL_FALSE
;
2756 if (!shader
->Program
) {
2758 if (shader
->Type
== GL_VERTEX_SHADER
)
2759 progTarget
= GL_VERTEX_PROGRAM_ARB
;
2761 progTarget
= GL_FRAGMENT_PROGRAM_ARB
;
2762 shader
->Program
= ctx
->Driver
.NewProgram(ctx
, progTarget
, 1);
2763 shader
->Program
->Parameters
= _mesa_new_parameter_list();
2764 shader
->Program
->Varying
= _mesa_new_parameter_list();
2765 shader
->Program
->Attributes
= _mesa_new_parameter_list();
2768 slang_info_log_construct(&info_log
);
2769 _slang_code_object_ctr(&obj
);
2771 success
= compile_shader(ctx
, &obj
, type
, &info_log
, shader
);
2773 /* free shader's prev info log */
2774 if (shader
->InfoLog
) {
2775 _mesa_free(shader
->InfoLog
);
2776 shader
->InfoLog
= NULL
;
2779 if (info_log
.text
) {
2780 /* copy info-log string to shader object */
2781 shader
->InfoLog
= _mesa_strdup(info_log
.text
);
2784 if (info_log
.error_flag
) {
2788 slang_info_log_destruct(&info_log
);
2789 _slang_code_object_dtr(&obj
);
2791 _slang_delete_mempool((slang_mempool
*) ctx
->Shader
.MemPool
);
2792 ctx
->Shader
.MemPool
= NULL
;
2794 /* remove any reads of output registers */
2796 printf("Pre-remove output reads:\n");
2797 _mesa_print_program(shader
->Program
);
2799 _mesa_remove_output_reads(shader
->Program
, PROGRAM_OUTPUT
);
2800 if (shader
->Type
== GL_VERTEX_SHADER
) {
2801 /* and remove writes to varying vars in vertex programs */
2802 _mesa_remove_output_reads(shader
->Program
, PROGRAM_VARYING
);
2805 printf("Post-remove output reads:\n");
2806 _mesa_print_program(shader
->Program
);