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
, 1))
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
, 1))
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
, 0))
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
, 0))
1097 oper
->type
= SLANG_OPER_EXPRESSION
;
1098 if (!parse_child_operation(C
, O
, oper
, 0))
1102 oper
->type
= SLANG_OPER_IF
;
1103 if (!parse_child_operation(C
, O
, oper
, 0))
1105 if (!parse_child_operation(C
, O
, oper
, 1))
1107 if (!parse_child_operation(C
, O
, oper
, 1))
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
, 1))
1118 if (!parse_child_operation(C
, &o
, oper
, 1))
1123 oper
->type
= SLANG_OPER_DO
;
1124 if (!parse_child_operation(C
, O
, oper
, 1))
1126 if (!parse_child_operation(C
, O
, oper
, 0))
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
, 1))
1137 if (!parse_child_operation(C
, &o
, oper
, 1))
1139 if (!parse_child_operation(C
, &o
, oper
, 0))
1141 if (!parse_child_operation(C
, &o
, oper
, 1))
1147 /* set default precision for a type in this scope */
1148 /* ignored at this time */
1149 int prec_qual
= *C
->I
++;
1150 int datatype
= *C
->I
++;
1156 /*printf("Unexpected operation %d\n", op);*/
1163 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
1164 slang_operation
** ops
, unsigned int *total_ops
,
1169 op
->children
= slang_operation_new(n
);
1170 if (op
->children
== NULL
) {
1171 slang_info_log_memory(C
->L
);
1174 op
->num_children
= n
;
1176 for (i
= 0; i
< n
; i
++) {
1177 slang_operation_destruct(&op
->children
[i
]);
1178 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
1181 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
1184 *ops
= (slang_operation
*)
1185 _slang_realloc(*ops
,
1186 (*total_ops
+ n
) * sizeof(slang_operation
),
1187 *total_ops
* sizeof(slang_operation
));
1189 slang_info_log_memory(C
->L
);
1196 is_constructor_name(const char *name
, slang_atom a_name
,
1197 slang_struct_scope
* structs
)
1199 if (slang_type_specifier_type_from_string(name
) != SLANG_SPEC_VOID
)
1201 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
1204 #define FUNCTION_CALL_NONARRAY 0
1205 #define FUNCTION_CALL_ARRAY 1
1208 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1209 slang_operation
* oper
)
1211 slang_operation
*ops
= NULL
;
1212 unsigned int num_ops
= 0;
1215 while (*C
->I
!= OP_END
) {
1216 slang_operation
*op
;
1217 const unsigned int op_code
= *C
->I
++;
1219 /* allocate default operation, becomes a no-op if not used */
1220 ops
= (slang_operation
*)
1222 num_ops
* sizeof(slang_operation
),
1223 (num_ops
+ 1) * sizeof(slang_operation
));
1225 slang_info_log_memory(C
->L
);
1229 if (!slang_operation_construct(op
)) {
1230 slang_info_log_memory(C
->L
);
1234 op
->locals
->outer_scope
= O
->vars
;
1238 op
->type
= SLANG_OPER_VOID
;
1241 op
->type
= SLANG_OPER_LITERAL_BOOL
;
1242 if (!parse_number(C
, &number
))
1247 op
->literal
[3] = (GLfloat
) number
;
1248 op
->literal_size
= 1;
1251 op
->type
= SLANG_OPER_LITERAL_INT
;
1252 if (!parse_number(C
, &number
))
1257 op
->literal
[3] = (GLfloat
) number
;
1258 op
->literal_size
= 1;
1261 op
->type
= SLANG_OPER_LITERAL_FLOAT
;
1262 if (!parse_float(C
, &op
->literal
[0]))
1266 op
->literal
[3] = op
->literal
[0];
1267 op
->literal_size
= 1;
1269 case OP_PUSH_IDENTIFIER
:
1270 op
->type
= SLANG_OPER_IDENTIFIER
;
1271 op
->a_id
= parse_identifier(C
);
1272 if (op
->a_id
== SLANG_ATOM_NULL
)
1276 op
->type
= SLANG_OPER_SEQUENCE
;
1277 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1281 op
->type
= SLANG_OPER_ASSIGN
;
1282 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1286 op
->type
= SLANG_OPER_ADDASSIGN
;
1287 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1291 op
->type
= SLANG_OPER_SUBASSIGN
;
1292 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1296 op
->type
= SLANG_OPER_MULASSIGN
;
1297 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1301 op
->type
= SLANG_OPER_DIVASSIGN
;
1302 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1305 /*case OP_MODASSIGN: */
1306 /*case OP_LSHASSIGN: */
1307 /*case OP_RSHASSIGN: */
1308 /*case OP_ORASSIGN: */
1309 /*case OP_XORASSIGN: */
1310 /*case OP_ANDASSIGN: */
1312 op
->type
= SLANG_OPER_SELECT
;
1313 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1317 op
->type
= SLANG_OPER_LOGICALOR
;
1318 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1322 op
->type
= SLANG_OPER_LOGICALXOR
;
1323 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1327 op
->type
= SLANG_OPER_LOGICALAND
;
1328 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1332 /*case OP_BITXOR: */
1333 /*case OP_BITAND: */
1335 op
->type
= SLANG_OPER_EQUAL
;
1336 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1340 op
->type
= SLANG_OPER_NOTEQUAL
;
1341 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1345 op
->type
= SLANG_OPER_LESS
;
1346 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1350 op
->type
= SLANG_OPER_GREATER
;
1351 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1355 op
->type
= SLANG_OPER_LESSEQUAL
;
1356 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1359 case OP_GREATEREQUAL
:
1360 op
->type
= SLANG_OPER_GREATEREQUAL
;
1361 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1364 /*case OP_LSHIFT: */
1365 /*case OP_RSHIFT: */
1367 op
->type
= SLANG_OPER_ADD
;
1368 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1372 op
->type
= SLANG_OPER_SUBTRACT
;
1373 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1377 op
->type
= SLANG_OPER_MULTIPLY
;
1378 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1382 op
->type
= SLANG_OPER_DIVIDE
;
1383 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1386 /*case OP_MODULUS: */
1387 case OP_PREINCREMENT
:
1388 op
->type
= SLANG_OPER_PREINCREMENT
;
1389 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1392 case OP_PREDECREMENT
:
1393 op
->type
= SLANG_OPER_PREDECREMENT
;
1394 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1398 op
->type
= SLANG_OPER_PLUS
;
1399 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1403 op
->type
= SLANG_OPER_MINUS
;
1404 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1408 op
->type
= SLANG_OPER_NOT
;
1409 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1412 /*case OP_COMPLEMENT: */
1414 op
->type
= SLANG_OPER_SUBSCRIPT
;
1415 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1419 op
->type
= SLANG_OPER_METHOD
;
1420 op
->a_obj
= parse_identifier(C
);
1421 if (op
->a_obj
== SLANG_ATOM_NULL
)
1424 op
->a_id
= parse_identifier(C
);
1425 if (op
->a_id
== SLANG_ATOM_NULL
)
1428 assert(*C
->I
== OP_END
);
1431 while (*C
->I
!= OP_END
)
1432 if (!parse_child_operation(C
, O
, op
, 0))
1436 /* don't lookup the method (not yet anyway) */
1437 if (!C
->parsing_builtin
1438 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1441 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1442 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1443 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1451 GLboolean array_constructor
= GL_FALSE
;
1452 GLint array_constructor_size
;
1454 op
->type
= SLANG_OPER_CALL
;
1455 op
->a_id
= parse_identifier(C
);
1456 if (op
->a_id
== SLANG_ATOM_NULL
)
1459 case FUNCTION_CALL_NONARRAY
:
1460 /* Nothing to do. */
1462 case FUNCTION_CALL_ARRAY
:
1463 /* Calling an array constructor. For example:
1464 * float[3](1.1, 2.2, 3.3);
1466 if (!O
->allow_array_types
) {
1467 slang_info_log_error(C
->L
,
1468 "array constructors not allowed "
1469 "in this GLSL version");
1473 slang_operation array_size
;
1474 array_constructor
= GL_TRUE
;
1475 /* parse the array constructor size */
1476 slang_operation_construct(&array_size
);
1477 if (!parse_expression(C
, O
, &array_size
)) {
1478 slang_operation_destruct(&array_size
);
1481 if (array_size
.type
!= SLANG_OPER_LITERAL_INT
) {
1482 slang_info_log_error(C
->L
,
1483 "constructor array size is not an integer");
1484 slang_operation_destruct(&array_size
);
1487 array_constructor_size
= (int) array_size
.literal
[0];
1488 op
->array_constructor
= GL_TRUE
;
1489 slang_operation_destruct(&array_size
);
1496 while (*C
->I
!= OP_END
)
1497 if (!parse_child_operation(C
, O
, op
, 0))
1501 if (array_constructor
&&
1502 array_constructor_size
!= op
->num_children
) {
1503 slang_info_log_error(C
->L
, "number of parameters to array"
1504 " constructor does not match array size");
1508 if (!C
->parsing_builtin
1509 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1512 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1513 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1514 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1521 op
->type
= SLANG_OPER_FIELD
;
1522 op
->a_id
= parse_identifier(C
);
1523 if (op
->a_id
== SLANG_ATOM_NULL
)
1525 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1528 case OP_POSTINCREMENT
:
1529 op
->type
= SLANG_OPER_POSTINCREMENT
;
1530 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1533 case OP_POSTDECREMENT
:
1534 op
->type
= SLANG_OPER_POSTDECREMENT
;
1535 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1544 slang_operation_destruct(oper
);
1545 *oper
= *ops
; /* struct copy */
1551 /* parameter qualifier */
1552 #define PARAM_QUALIFIER_IN 0
1553 #define PARAM_QUALIFIER_OUT 1
1554 #define PARAM_QUALIFIER_INOUT 2
1556 /* function parameter array presence */
1557 #define PARAMETER_ARRAY_NOT_PRESENT 0
1558 #define PARAMETER_ARRAY_PRESENT 1
1561 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1562 slang_variable
* param
)
1564 int param_qual
, precision_qual
;
1566 /* parse and validate the parameter's type qualifiers (there can be
1567 * two at most) because not all combinations are valid
1569 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1572 param_qual
= *C
->I
++;
1573 switch (param_qual
) {
1574 case PARAM_QUALIFIER_IN
:
1575 if (param
->type
.qualifier
!= SLANG_QUAL_CONST
1576 && param
->type
.qualifier
!= SLANG_QUAL_NONE
) {
1577 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1581 case PARAM_QUALIFIER_OUT
:
1582 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1583 param
->type
.qualifier
= SLANG_QUAL_OUT
;
1585 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1589 case PARAM_QUALIFIER_INOUT
:
1590 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1591 param
->type
.qualifier
= SLANG_QUAL_INOUT
;
1593 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1601 /* parse precision qualifier (lowp, mediump, highp */
1602 precision_qual
= *C
->I
++;
1603 /* ignored at this time */
1604 (void) precision_qual
;
1606 /* parse parameter's type specifier and name */
1607 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1609 if (!parse_type_array_size(C
, O
, ¶m
->type
.array_len
))
1611 param
->a_name
= parse_identifier(C
);
1612 if (param
->a_name
== SLANG_ATOM_NULL
)
1615 /* first-class array
1617 if (param
->type
.array_len
>= 0) {
1618 slang_type_specifier p
;
1620 slang_type_specifier_ctr(&p
);
1621 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1622 slang_type_specifier_dtr(&p
);
1625 if (!convert_to_array(C
, param
, &p
)) {
1626 slang_type_specifier_dtr(&p
);
1629 slang_type_specifier_dtr(&p
);
1630 param
->array_len
= param
->type
.array_len
;
1633 /* if the parameter is an array, parse its size (the size must be
1634 * explicitly defined
1636 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1637 slang_type_specifier p
;
1639 if (param
->type
.array_len
>= 0) {
1640 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
1643 slang_type_specifier_ctr(&p
);
1644 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1645 slang_type_specifier_dtr(&p
);
1648 if (!convert_to_array(C
, param
, &p
)) {
1649 slang_type_specifier_dtr(&p
);
1652 slang_type_specifier_dtr(&p
);
1653 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1658 /* calculate the parameter size */
1659 if (!calculate_var_size(C
, O
, param
))
1662 /* TODO: allocate the local address here? */
1667 #define FUNCTION_ORDINARY 0
1668 #define FUNCTION_CONSTRUCTOR 1
1669 #define FUNCTION_OPERATOR 2
1671 /* function parameter */
1672 #define PARAMETER_NONE 0
1673 #define PARAMETER_NEXT 1
1676 #define OPERATOR_ADDASSIGN 1
1677 #define OPERATOR_SUBASSIGN 2
1678 #define OPERATOR_MULASSIGN 3
1679 #define OPERATOR_DIVASSIGN 4
1680 /*#define OPERATOR_MODASSIGN 5*/
1681 /*#define OPERATOR_LSHASSIGN 6*/
1682 /*#define OPERATOR_RSHASSIGN 7*/
1683 /*#define OPERATOR_ANDASSIGN 8*/
1684 /*#define OPERATOR_XORASSIGN 9*/
1685 /*#define OPERATOR_ORASSIGN 10*/
1686 #define OPERATOR_LOGICALXOR 11
1687 /*#define OPERATOR_BITOR 12*/
1688 /*#define OPERATOR_BITXOR 13*/
1689 /*#define OPERATOR_BITAND 14*/
1690 #define OPERATOR_LESS 15
1691 #define OPERATOR_GREATER 16
1692 #define OPERATOR_LESSEQUAL 17
1693 #define OPERATOR_GREATEREQUAL 18
1694 /*#define OPERATOR_LSHIFT 19*/
1695 /*#define OPERATOR_RSHIFT 20*/
1696 #define OPERATOR_MULTIPLY 21
1697 #define OPERATOR_DIVIDE 22
1698 /*#define OPERATOR_MODULUS 23*/
1699 #define OPERATOR_INCREMENT 24
1700 #define OPERATOR_DECREMENT 25
1701 #define OPERATOR_PLUS 26
1702 #define OPERATOR_MINUS 27
1703 /*#define OPERATOR_COMPLEMENT 28*/
1704 #define OPERATOR_NOT 29
1708 unsigned int o_code
;
1710 } operator_names
[] = {
1711 {OPERATOR_INCREMENT
, "++"},
1712 {OPERATOR_ADDASSIGN
, "+="},
1713 {OPERATOR_PLUS
, "+"},
1714 {OPERATOR_DECREMENT
, "--"},
1715 {OPERATOR_SUBASSIGN
, "-="},
1716 {OPERATOR_MINUS
, "-"},
1717 {OPERATOR_NOT
, "!"},
1718 {OPERATOR_MULASSIGN
, "*="},
1719 {OPERATOR_MULTIPLY
, "*"},
1720 {OPERATOR_DIVASSIGN
, "/="},
1721 {OPERATOR_DIVIDE
, "/"},
1722 {OPERATOR_LESSEQUAL
, "<="},
1723 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1724 /*{ OPERATOR_LSHIFT, "<<" }, */
1725 {OPERATOR_LESS
, "<"},
1726 {OPERATOR_GREATEREQUAL
, ">="},
1727 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1728 /*{ OPERATOR_RSHIFT, ">>" }, */
1729 {OPERATOR_GREATER
, ">"},
1730 /*{ OPERATOR_MODASSIGN, "%=" }, */
1731 /*{ OPERATOR_MODULUS, "%" }, */
1732 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1733 /*{ OPERATOR_BITAND, "&" }, */
1734 /*{ OPERATOR_ORASSIGN, "|=" }, */
1735 /*{ OPERATOR_BITOR, "|" }, */
1736 /*{ OPERATOR_COMPLEMENT, "~" }, */
1737 /*{ OPERATOR_XORASSIGN, "^=" }, */
1738 {OPERATOR_LOGICALXOR
, "^^"},
1739 /*{ OPERATOR_BITXOR, "^" } */
1743 parse_operator_name(slang_parse_ctx
* C
)
1747 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1748 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1750 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1751 if (atom
== SLANG_ATOM_NULL
) {
1752 slang_info_log_memory(C
->L
);
1764 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1765 slang_function
* func
)
1768 /* parse function type and name */
1769 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1774 case FUNCTION_ORDINARY
:
1775 func
->kind
= SLANG_FUNC_ORDINARY
;
1776 func
->header
.a_name
= parse_identifier(C
);
1777 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1780 case FUNCTION_CONSTRUCTOR
:
1781 func
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1782 if (func
->header
.type
.specifier
.type
== SLANG_SPEC_STRUCT
)
1784 func
->header
.a_name
=
1785 slang_atom_pool_atom(C
->atoms
,
1786 slang_type_specifier_type_to_string
1787 (func
->header
.type
.specifier
.type
));
1788 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1789 slang_info_log_memory(C
->L
);
1793 case FUNCTION_OPERATOR
:
1794 func
->kind
= SLANG_FUNC_OPERATOR
;
1795 func
->header
.a_name
= parse_operator_name(C
);
1796 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1803 if (!legal_identifier(func
->header
.a_name
)) {
1804 slang_info_log_error(C
->L
, "illegal function name '%s'",
1805 (char *) func
->header
.a_name
);
1809 /* parse function parameters */
1810 while (*C
->I
++ == PARAMETER_NEXT
) {
1811 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1813 slang_info_log_memory(C
->L
);
1816 if (!parse_parameter_declaration(C
, O
, p
))
1820 /* if the function returns a value, append a hidden __retVal 'out'
1821 * parameter that corresponds to the return value.
1823 if (_slang_function_has_return_value(func
)) {
1824 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1825 slang_atom a_retVal
= slang_atom_pool_atom(C
->atoms
, "__retVal");
1827 p
->a_name
= a_retVal
;
1828 p
->type
= func
->header
.type
;
1829 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1832 /* function formal parameters and local variables share the same
1833 * scope, so save the information about param count in a seperate
1834 * place also link the scope to the global variable scope so when a
1835 * given identifier is not found here, the search process continues
1836 * in the global space
1838 func
->param_count
= func
->parameters
->num_variables
;
1839 func
->parameters
->outer_scope
= O
->vars
;
1845 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1846 slang_function
* func
)
1848 slang_output_ctx o
= *O
;
1850 if (!parse_function_prototype(C
, O
, func
))
1853 /* create function's body operation */
1854 func
->body
= (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1855 if (func
->body
== NULL
) {
1856 slang_info_log_memory(C
->L
);
1859 if (!slang_operation_construct(func
->body
)) {
1860 _slang_free(func
->body
);
1862 slang_info_log_memory(C
->L
);
1866 /* to parse the body the parse context is modified in order to
1867 * capture parsed variables into function's local variable scope
1869 C
->global_scope
= GL_FALSE
;
1870 o
.vars
= func
->parameters
;
1871 if (!parse_statement(C
, &o
, func
->body
))
1874 C
->global_scope
= GL_TRUE
;
1879 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1881 slang_operation op_id
, op_assign
;
1884 /* construct the left side of assignment */
1885 if (!slang_operation_construct(&op_id
))
1887 op_id
.type
= SLANG_OPER_IDENTIFIER
;
1888 op_id
.a_id
= var
->a_name
;
1890 /* put the variable into operation's scope */
1891 op_id
.locals
->variables
=
1892 (slang_variable
**) _slang_alloc(sizeof(slang_variable
*));
1893 if (op_id
.locals
->variables
== NULL
) {
1894 slang_operation_destruct(&op_id
);
1897 op_id
.locals
->num_variables
= 1;
1898 op_id
.locals
->variables
[0] = var
;
1900 /* construct the assignment expression */
1901 if (!slang_operation_construct(&op_assign
)) {
1902 op_id
.locals
->num_variables
= 0;
1903 slang_operation_destruct(&op_id
);
1906 op_assign
.type
= SLANG_OPER_ASSIGN
;
1907 op_assign
.children
=
1908 (slang_operation
*) _slang_alloc(2 * sizeof(slang_operation
));
1909 if (op_assign
.children
== NULL
) {
1910 slang_operation_destruct(&op_assign
);
1911 op_id
.locals
->num_variables
= 0;
1912 slang_operation_destruct(&op_id
);
1915 op_assign
.num_children
= 2;
1916 op_assign
.children
[0] = op_id
;
1917 op_assign
.children
[1] = *var
->initializer
;
1921 /* carefully destroy the operations */
1922 op_assign
.num_children
= 0;
1923 _slang_free(op_assign
.children
);
1924 op_assign
.children
= NULL
;
1925 slang_operation_destruct(&op_assign
);
1926 op_id
.locals
->num_variables
= 0;
1927 slang_operation_destruct(&op_id
);
1935 /* init declarator list */
1936 #define DECLARATOR_NONE 0
1937 #define DECLARATOR_NEXT 1
1939 /* variable declaration */
1940 #define VARIABLE_NONE 0
1941 #define VARIABLE_IDENTIFIER 1
1942 #define VARIABLE_INITIALIZER 2
1943 #define VARIABLE_ARRAY_EXPLICIT 3
1944 #define VARIABLE_ARRAY_UNKNOWN 4
1948 * Parse the initializer for a variable declaration.
1951 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1952 const slang_fully_specified_type
* type
)
1954 slang_variable
*var
;
1957 /* empty init declatator (without name, e.g. "float ;") */
1958 if (*C
->I
++ == VARIABLE_NONE
)
1961 a_name
= parse_identifier(C
);
1963 /* check if name is already in this scope */
1964 if (_slang_variable_locate(O
->vars
, a_name
, GL_FALSE
)) {
1965 slang_info_log_error(C
->L
,
1966 "declaration of '%s' conflicts with previous declaration",
1971 /* make room for the new variable and initialize it */
1972 var
= slang_variable_scope_grow(O
->vars
);
1974 slang_info_log_memory(C
->L
);
1978 /* copy the declarator type qualifier/etc info, parse the identifier */
1979 var
->type
.qualifier
= type
->qualifier
;
1980 var
->type
.centroid
= type
->centroid
;
1981 var
->type
.precision
= type
->precision
;
1982 var
->type
.variant
= type
->variant
;
1983 var
->type
.array_len
= type
->array_len
;
1984 var
->a_name
= a_name
;
1985 if (var
->a_name
== SLANG_ATOM_NULL
)
1990 /* simple variable declarator - just copy the specifier */
1991 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1994 case VARIABLE_INITIALIZER
:
1995 /* initialized variable - copy the specifier and parse the expression */
1996 if (0 && type
->array_len
>= 0) {
1997 /* The type was something like "float[4]" */
1998 convert_to_array(C
, var
, &type
->specifier
);
1999 var
->array_len
= type
->array_len
;
2002 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
2006 (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
2007 if (var
->initializer
== NULL
) {
2008 slang_info_log_memory(C
->L
);
2011 if (!slang_operation_construct(var
->initializer
)) {
2012 _slang_free(var
->initializer
);
2013 var
->initializer
= NULL
;
2014 slang_info_log_memory(C
->L
);
2017 if (!parse_expression(C
, O
, var
->initializer
))
2020 case VARIABLE_ARRAY_UNKNOWN
:
2021 /* unsized array - mark it as array and copy the specifier to
2024 if (type
->array_len
>= 0) {
2025 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
2028 if (!convert_to_array(C
, var
, &type
->specifier
))
2031 case VARIABLE_ARRAY_EXPLICIT
:
2032 if (type
->array_len
>= 0) {
2033 /* the user is trying to do something like: float[2] x[3]; */
2034 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
2037 if (!convert_to_array(C
, var
, &type
->specifier
))
2039 if (!parse_array_len(C
, O
, &var
->array_len
))
2046 /* allocate global address space for a variable with a known size */
2048 && !(var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
2049 && var
->array_len
== 0)) {
2050 if (!calculate_var_size(C
, O
, var
))
2054 /* emit code for global var decl */
2055 if (C
->global_scope
) {
2056 slang_assemble_ctx A
;
2058 A
.space
.funcs
= O
->funs
;
2059 A
.space
.structs
= O
->structs
;
2060 A
.space
.vars
= O
->vars
;
2061 A
.program
= O
->program
;
2062 A
.vartable
= O
->vartable
;
2064 A
.curFuncEndLabel
= NULL
;
2065 if (!_slang_codegen_global_variable(&A
, var
, C
->type
))
2069 /* initialize global variable */
2070 if (C
->global_scope
) {
2071 if (var
->initializer
!= NULL
) {
2072 slang_assemble_ctx A
;
2075 A
.space
.funcs
= O
->funs
;
2076 A
.space
.structs
= O
->structs
;
2077 A
.space
.vars
= O
->vars
;
2078 if (!initialize_global(&A
, var
))
2086 * Parse a list of variable declarations. Each variable may have an
2090 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2092 slang_fully_specified_type type
;
2094 /* parse the fully specified type, common to all declarators */
2095 if (!slang_fully_specified_type_construct(&type
))
2097 if (!parse_fully_specified_type(C
, O
, &type
)) {
2098 slang_fully_specified_type_destruct(&type
);
2102 /* parse declarators, pass-in the parsed type */
2104 if (!parse_init_declarator(C
, O
, &type
)) {
2105 slang_fully_specified_type_destruct(&type
);
2109 while (*C
->I
++ == DECLARATOR_NEXT
);
2111 slang_fully_specified_type_destruct(&type
);
2117 * Parse a function definition or declaration.
2118 * \param C parsing context
2119 * \param O output context
2120 * \param definition if non-zero expect a definition, else a declaration
2121 * \param parsed_func_ret returns the parsed function
2122 * \return GL_TRUE if success, GL_FALSE if failure
2125 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
2126 slang_function
** parsed_func_ret
)
2128 slang_function parsed_func
, *found_func
;
2130 /* parse function definition/declaration */
2131 if (!slang_function_construct(&parsed_func
))
2134 if (!parse_function_definition(C
, O
, &parsed_func
)) {
2135 slang_function_destruct(&parsed_func
);
2140 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
2141 slang_function_destruct(&parsed_func
);
2146 /* find a function with a prototype matching the parsed one - only
2147 * the current scope is being searched to allow built-in function
2150 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
2151 if (found_func
== NULL
) {
2152 /* New function, add it to the function list */
2153 O
->funs
->functions
=
2154 (slang_function
*) _slang_realloc(O
->funs
->functions
,
2155 O
->funs
->num_functions
2156 * sizeof(slang_function
),
2157 (O
->funs
->num_functions
+ 1)
2158 * sizeof(slang_function
));
2159 if (O
->funs
->functions
== NULL
) {
2160 slang_info_log_memory(C
->L
);
2161 slang_function_destruct(&parsed_func
);
2164 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
2165 O
->funs
->num_functions
++;
2167 /* return the newly parsed function */
2168 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
2171 /* previously defined or declared */
2172 /* TODO: check function return type qualifiers and specifiers */
2174 if (found_func
->body
!= NULL
) {
2175 slang_info_log_error(C
->L
, "%s: function already has a body.",
2176 slang_atom_pool_id(C
->atoms
,
2179 slang_function_destruct(&parsed_func
);
2183 /* destroy the existing function declaration and replace it
2186 slang_function_destruct(found_func
);
2187 *found_func
= parsed_func
;
2190 /* another declaration of the same function prototype - ignore it */
2191 slang_function_destruct(&parsed_func
);
2194 /* return the found function */
2195 *parsed_func_ret
= found_func
;
2202 #define DECLARATION_FUNCTION_PROTOTYPE 1
2203 #define DECLARATION_INIT_DECLARATOR_LIST 2
2206 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2209 case DECLARATION_INIT_DECLARATOR_LIST
:
2210 if (!parse_init_declarator_list(C
, O
))
2213 case DECLARATION_FUNCTION_PROTOTYPE
:
2215 slang_function
*dummy_func
;
2217 if (!parse_function(C
, O
, 0, &dummy_func
))
2228 parse_default_precision(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2230 int precision
, type
;
2232 if (!O
->allow_precision
) {
2233 slang_info_log_error(C
->L
, "syntax error at \"precision\"");
2237 precision
= *C
->I
++;
2238 switch (precision
) {
2240 case PRECISION_MEDIUM
:
2241 case PRECISION_HIGH
:
2245 _mesa_problem(NULL
, "unexpected precision %d at %s:%d\n",
2246 precision
, __FILE__
, __LINE__
);
2252 case TYPE_SPECIFIER_FLOAT
:
2253 case TYPE_SPECIFIER_INT
:
2254 case TYPE_SPECIFIER_SAMPLER1D
:
2255 case TYPE_SPECIFIER_SAMPLER2D
:
2256 case TYPE_SPECIFIER_SAMPLER3D
:
2257 case TYPE_SPECIFIER_SAMPLERCUBE
:
2258 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
2259 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
2260 case TYPE_SPECIFIER_SAMPLER2DRECT
:
2261 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
2265 _mesa_problem(NULL
, "unexpected type %d at %s:%d\n",
2266 type
, __FILE__
, __LINE__
);
2270 assert(type
< TYPE_SPECIFIER_COUNT
);
2271 O
->default_precision
[type
] = precision
;
2278 * Initialize the default precision for all types.
2279 * XXX this info isn't used yet.
2282 init_default_precision(slang_output_ctx
*O
, slang_unit_type type
)
2285 for (i
= 0; i
< TYPE_SPECIFIER_COUNT
; i
++) {
2286 #if FEATURE_es2_glsl
2287 O
->default_precision
[i
] = PRECISION_LOW
;
2289 O
->default_precision
[i
] = PRECISION_HIGH
;
2293 if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2294 O
->default_precision
[TYPE_SPECIFIER_FLOAT
] = PRECISION_HIGH
;
2295 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_HIGH
;
2298 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_MEDIUM
;
2304 parse_invariant(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2306 if (O
->allow_invariant
) {
2307 slang_atom
*a
= parse_identifier(C
);
2308 /* XXX not doing anything with this var yet */
2309 /*printf("ID: %s\n", (char*) a);*/
2313 slang_info_log_error(C
->L
, "syntax error at \"invariant\"");
2319 /* external declaration or default precision specifier */
2320 #define EXTERNAL_NULL 0
2321 #define EXTERNAL_FUNCTION_DEFINITION 1
2322 #define EXTERNAL_DECLARATION 2
2323 #define DEFAULT_PRECISION 3
2324 #define INVARIANT_STMT 4
2328 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
,
2329 struct gl_shader
*shader
)
2331 GET_CURRENT_CONTEXT(ctx
);
2335 slang_function
*mainFunc
= NULL
;
2337 if (unit
->type
== SLANG_UNIT_FRAGMENT_BUILTIN
||
2338 unit
->type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2339 maxRegs
= ctx
->Const
.FragmentProgram
.MaxTemps
;
2342 assert(unit
->type
== SLANG_UNIT_VERTEX_BUILTIN
||
2343 unit
->type
== SLANG_UNIT_VERTEX_SHADER
);
2344 maxRegs
= ctx
->Const
.VertexProgram
.MaxTemps
;
2347 /* setup output context */
2348 o
.funs
= &unit
->funs
;
2349 o
.structs
= &unit
->structs
;
2350 o
.vars
= &unit
->vars
;
2351 o
.program
= shader
? shader
->Program
: NULL
;
2352 o
.vartable
= _slang_new_var_table(maxRegs
);
2353 _slang_push_var_table(o
.vartable
);
2355 /* allow 'invariant' keyword? */
2356 #if FEATURE_es2_glsl
2357 o
.allow_invariant
= GL_TRUE
;
2359 o
.allow_invariant
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2362 /* allow 'centroid' keyword? */
2363 o
.allow_centroid
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2365 /* allow 'lowp/mediump/highp' keywords? */
2366 #if FEATURE_es2_glsl
2367 o
.allow_precision
= GL_TRUE
;
2369 o
.allow_precision
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2371 init_default_precision(&o
, unit
->type
);
2373 /* allow 'float[]' keyword? */
2374 o
.allow_array_types
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2376 /* parse individual functions and declarations */
2377 while (*C
->I
!= EXTERNAL_NULL
) {
2379 case EXTERNAL_FUNCTION_DEFINITION
:
2381 slang_function
*func
;
2382 success
= parse_function(C
, &o
, 1, &func
);
2384 _mesa_strcmp((char *) func
->header
.a_name
, "main") == 0) {
2390 case EXTERNAL_DECLARATION
:
2391 success
= parse_declaration(C
, &o
);
2393 case DEFAULT_PRECISION
:
2394 success
= parse_default_precision(C
, &o
);
2396 case INVARIANT_STMT
:
2397 success
= parse_invariant(C
, &o
);
2404 /* xxx free codegen */
2405 _slang_pop_var_table(o
.vartable
);
2412 /* assemble (generate code) for main() */
2413 slang_assemble_ctx A
;
2416 A
.space
.funcs
= o
.funs
;
2417 A
.space
.structs
= o
.structs
;
2418 A
.space
.vars
= o
.vars
;
2419 A
.program
= o
.program
;
2420 A
.vartable
= o
.vartable
;
2423 /* main() takes no parameters */
2424 if (mainFunc
->param_count
> 0) {
2425 slang_info_log_error(A
.log
, "main() takes no arguments");
2429 _slang_codegen_function(&A
, mainFunc
);
2431 shader
->Main
= GL_TRUE
; /* this shader defines main() */
2434 _slang_pop_var_table(o
.vartable
);
2435 _slang_delete_var_table(o
.vartable
);
2441 compile_binary(const byte
* prod
, slang_code_unit
* unit
,
2443 slang_unit_type type
, slang_info_log
* infolog
,
2444 slang_code_unit
* builtin
, slang_code_unit
* downlink
,
2445 struct gl_shader
*shader
)
2451 /* setup parse context */
2454 C
.parsing_builtin
= (builtin
== NULL
);
2455 C
.global_scope
= GL_TRUE
;
2456 C
.atoms
= &unit
->object
->atompool
;
2458 C
.version
= version
;
2460 if (!check_revision(&C
))
2463 if (downlink
!= NULL
) {
2464 unit
->vars
.outer_scope
= &downlink
->vars
;
2465 unit
->funs
.outer_scope
= &downlink
->funs
;
2466 unit
->structs
.outer_scope
= &downlink
->structs
;
2469 /* parse translation unit */
2470 return parse_code_unit(&C
, unit
, shader
);
2474 compile_with_grammar(grammar id
, const char *source
, slang_code_unit
* unit
,
2475 slang_unit_type type
, slang_info_log
* infolog
,
2476 slang_code_unit
* builtin
,
2477 struct gl_shader
*shader
)
2480 GLuint size
, start
, version
;
2481 slang_string preprocessed
;
2484 #if FEATURE_ARB_shading_language_120
2486 #elif FEATURE_es2_glsl
2492 /* First retrieve the version number. */
2493 if (!_slang_preprocess_version(source
, &version
, &start
, infolog
))
2496 if (version
> maxVersion
) {
2497 slang_info_log_error(infolog
,
2498 "language version %.2f is not supported.",
2503 /* Now preprocess the source string. */
2504 slang_string_init(&preprocessed
);
2505 if (!_slang_preprocess_directives(&preprocessed
, &source
[start
], infolog
)) {
2506 slang_string_free(&preprocessed
);
2507 slang_info_log_error(infolog
, "failed to preprocess the source.");
2511 /* Finally check the syntax and generate its binary representation. */
2512 if (!grammar_fast_check(id
,
2513 (const byte
*) (slang_string_cstr(&preprocessed
)),
2514 &prod
, &size
, 65536)) {
2518 slang_string_free(&preprocessed
);
2519 grammar_get_last_error((byte
*) (buf
), sizeof(buf
), &pos
);
2520 slang_info_log_error(infolog
, buf
);
2521 /* syntax error (possibly in library code) */
2526 s
= (char *) _mesa_find_line_column((const GLubyte
*) source
,
2527 (const GLubyte
*) source
+ pos
,
2529 printf("Error on line %d, col %d: %s\n", line
, col
, s
);
2534 slang_string_free(&preprocessed
);
2536 /* Syntax is okay - translate it to internal representation. */
2537 if (!compile_binary(prod
, unit
, version
, type
, infolog
, builtin
,
2538 &builtin
[SLANG_BUILTIN_TOTAL
- 1],
2540 grammar_alloc_free(prod
);
2543 grammar_alloc_free(prod
);
2547 LONGSTRING
static const char *slang_shader_syn
=
2548 #include "library/slang_shader_syn.h"
2551 static const byte slang_core_gc
[] = {
2552 #include "library/slang_core_gc.h"
2555 static const byte slang_120_core_gc
[] = {
2556 #include "library/slang_120_core_gc.h"
2559 static const byte slang_120_fragment_gc
[] = {
2560 #include "library/slang_builtin_120_fragment_gc.h"
2563 static const byte slang_common_builtin_gc
[] = {
2564 #include "library/slang_common_builtin_gc.h"
2567 static const byte slang_fragment_builtin_gc
[] = {
2568 #include "library/slang_fragment_builtin_gc.h"
2571 static const byte slang_vertex_builtin_gc
[] = {
2572 #include "library/slang_vertex_builtin_gc.h"
2576 compile_object(grammar
* id
, const char *source
, slang_code_object
* object
,
2577 slang_unit_type type
, slang_info_log
* infolog
,
2578 struct gl_shader
*shader
)
2580 slang_code_unit
*builtins
= NULL
;
2581 GLuint base_version
= 110;
2583 /* load GLSL grammar */
2584 *id
= grammar_load_from_text((const byte
*) (slang_shader_syn
));
2589 grammar_get_last_error(buf
, 1024, &pos
);
2590 slang_info_log_error(infolog
, (const char *) (buf
));
2594 /* set shader type - the syntax is slightly different for different shaders */
2595 if (type
== SLANG_UNIT_FRAGMENT_SHADER
2596 || type
== SLANG_UNIT_FRAGMENT_BUILTIN
)
2597 grammar_set_reg8(*id
, (const byte
*) "shader_type", 1);
2599 grammar_set_reg8(*id
, (const byte
*) "shader_type", 2);
2601 /* enable language extensions */
2602 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2604 /* if parsing user-specified shader, load built-in library */
2605 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_VERTEX_SHADER
) {
2606 /* compile core functionality first */
2607 if (!compile_binary(slang_core_gc
,
2608 &object
->builtin
[SLANG_BUILTIN_CORE
],
2610 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2614 #if FEATURE_ARB_shading_language_120
2615 if (!compile_binary(slang_120_core_gc
,
2616 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2618 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2619 NULL
, &object
->builtin
[SLANG_BUILTIN_CORE
], NULL
))
2623 /* compile common functions and variables, link to core */
2624 if (!compile_binary(slang_common_builtin_gc
,
2625 &object
->builtin
[SLANG_BUILTIN_COMMON
],
2626 #if FEATURE_ARB_shading_language_120
2631 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2632 #if FEATURE_ARB_shading_language_120
2633 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2635 &object
->builtin
[SLANG_BUILTIN_CORE
],
2640 /* compile target-specific functions and variables, link to common */
2641 if (type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2642 if (!compile_binary(slang_fragment_builtin_gc
,
2643 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2645 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2646 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2648 #if FEATURE_ARB_shading_language_120
2649 if (!compile_binary(slang_120_fragment_gc
,
2650 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2652 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2653 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2657 else if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2658 if (!compile_binary(slang_vertex_builtin_gc
,
2659 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2661 SLANG_UNIT_VERTEX_BUILTIN
, infolog
, NULL
,
2662 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2666 /* disable language extensions */
2667 #if NEW_SLANG /* allow-built-ins */
2668 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2670 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 0);
2672 builtins
= object
->builtin
;
2675 /* compile the actual shader - pass-in built-in library for external shader */
2676 return compile_with_grammar(*id
, source
, &object
->unit
, type
, infolog
,
2682 compile_shader(GLcontext
*ctx
, slang_code_object
* object
,
2683 slang_unit_type type
, slang_info_log
* infolog
,
2684 struct gl_shader
*shader
)
2689 #if 0 /* for debug */
2690 _mesa_printf("********* COMPILE SHADER ***********\n");
2691 _mesa_printf("%s\n", shader
->Source
);
2692 _mesa_printf("************************************\n");
2695 assert(shader
->Program
);
2697 _slang_code_object_dtr(object
);
2698 _slang_code_object_ctr(object
);
2700 success
= compile_object(&id
, shader
->Source
, object
, type
, infolog
, shader
);
2702 grammar_destroy(id
);
2712 _slang_compile(GLcontext
*ctx
, struct gl_shader
*shader
)
2715 slang_info_log info_log
;
2716 slang_code_object obj
;
2717 slang_unit_type type
;
2719 if (shader
->Type
== GL_VERTEX_SHADER
) {
2720 type
= SLANG_UNIT_VERTEX_SHADER
;
2723 assert(shader
->Type
== GL_FRAGMENT_SHADER
);
2724 type
= SLANG_UNIT_FRAGMENT_SHADER
;
2727 if (!shader
->Source
)
2730 ctx
->Shader
.MemPool
= _slang_new_mempool(1024*1024);
2732 shader
->Main
= GL_FALSE
;
2734 if (!shader
->Program
) {
2736 if (shader
->Type
== GL_VERTEX_SHADER
)
2737 progTarget
= GL_VERTEX_PROGRAM_ARB
;
2739 progTarget
= GL_FRAGMENT_PROGRAM_ARB
;
2740 shader
->Program
= ctx
->Driver
.NewProgram(ctx
, progTarget
, 1);
2741 shader
->Program
->Parameters
= _mesa_new_parameter_list();
2742 shader
->Program
->Varying
= _mesa_new_parameter_list();
2743 shader
->Program
->Attributes
= _mesa_new_parameter_list();
2746 slang_info_log_construct(&info_log
);
2747 _slang_code_object_ctr(&obj
);
2749 success
= compile_shader(ctx
, &obj
, type
, &info_log
, shader
);
2751 /* free shader's prev info log */
2752 if (shader
->InfoLog
) {
2753 _mesa_free(shader
->InfoLog
);
2754 shader
->InfoLog
= NULL
;
2757 if (info_log
.text
) {
2758 /* copy info-log string to shader object */
2759 shader
->InfoLog
= _mesa_strdup(info_log
.text
);
2762 if (info_log
.error_flag
) {
2766 slang_info_log_destruct(&info_log
);
2767 _slang_code_object_dtr(&obj
);
2769 _slang_delete_mempool((slang_mempool
*) ctx
->Shader
.MemPool
);
2770 ctx
->Shader
.MemPool
= NULL
;
2772 /* remove any reads of output registers */
2774 printf("Pre-remove output reads:\n");
2775 _mesa_print_program(shader
->Program
);
2777 _mesa_remove_output_reads(shader
->Program
, PROGRAM_OUTPUT
);
2778 if (shader
->Type
== GL_VERTEX_SHADER
) {
2779 /* and remove writes to varying vars in vertex programs */
2780 _mesa_remove_output_reads(shader
->Program
, PROGRAM_VARYING
);
2783 printf("Post-remove output reads:\n");
2784 _mesa_print_program(shader
->Program
);