2 * Mesa 3-D graphics library
5 * Copyright (C) 2005-2006 Brian Paul 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) {
78 * Allocate storage for a variable of 'size' bytes from given pool.
79 * Return the allocated address for the variable.
82 slang_var_pool_alloc(slang_var_pool
* pool
, unsigned int size
)
84 const GLuint addr
= pool
->next_addr
;
85 pool
->next_addr
+= size
;
94 _slang_code_unit_ctr(slang_code_unit
* self
,
95 struct slang_code_object_
* object
)
97 _slang_variable_scope_ctr(&self
->vars
);
98 _slang_function_scope_ctr(&self
->funs
);
99 _slang_struct_scope_ctr(&self
->structs
);
100 self
->object
= object
;
104 _slang_code_unit_dtr(slang_code_unit
* self
)
106 slang_variable_scope_destruct(&self
->vars
);
107 slang_function_scope_destruct(&self
->funs
);
108 slang_struct_scope_destruct(&self
->structs
);
116 _slang_code_object_ctr(slang_code_object
* self
)
120 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
121 _slang_code_unit_ctr(&self
->builtin
[i
], self
);
122 _slang_code_unit_ctr(&self
->unit
, self
);
123 self
->varpool
.next_addr
= 0;
124 slang_atom_pool_construct(&self
->atompool
);
128 _slang_code_object_dtr(slang_code_object
* self
)
132 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
133 _slang_code_unit_dtr(&self
->builtin
[i
]);
134 _slang_code_unit_dtr(&self
->unit
);
135 slang_atom_pool_destruct(&self
->atompool
);
139 /* slang_parse_ctx */
141 typedef struct slang_parse_ctx_
146 GLboolean global_scope
; /**< Is object being declared a global? */
147 slang_atom_pool
*atoms
;
148 slang_unit_type type
; /**< Vertex vs. Fragment */
149 GLuint version
; /**< user-specified (or default) #version */
152 /* slang_output_ctx */
154 typedef struct slang_output_ctx_
156 slang_variable_scope
*vars
;
157 slang_function_scope
*funs
;
158 slang_struct_scope
*structs
;
159 slang_var_pool
*global_pool
;
160 struct gl_program
*program
;
161 slang_var_table
*vartable
;
162 GLuint default_precision
[TYPE_SPECIFIER_COUNT
];
163 GLboolean allow_precision
;
164 GLboolean allow_invariant
;
165 GLboolean allow_centroid
;
166 GLboolean allow_array_types
; /* float[] syntax */
169 /* _slang_compile() */
172 /* Debugging aid, print file/line where parsing error is detected */
176 printf("slang error at %s:%d\n", __FILE__, __LINE__); \
182 parse_identifier_str(slang_parse_ctx
* C
, char **id
)
185 C
->I
+= _mesa_strlen(*id
) + 1;
189 parse_identifier(slang_parse_ctx
* C
)
193 id
= (const char *) C
->I
;
194 C
->I
+= _mesa_strlen(id
) + 1;
195 return slang_atom_pool_atom(C
->atoms
, id
);
199 parse_number(slang_parse_ctx
* C
, int *number
)
201 const int radix
= (int) (*C
->I
++);
203 while (*C
->I
!= '\0') {
205 if (*C
->I
>= '0' && *C
->I
<= '9')
206 digit
= (int) (*C
->I
- '0');
207 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
208 digit
= (int) (*C
->I
- 'A') + 10;
210 digit
= (int) (*C
->I
- 'a') + 10;
211 *number
= *number
* radix
+ digit
;
216 slang_info_log_warning(C
->L
, "%d: literal integer overflow.", *number
);
221 parse_float(slang_parse_ctx
* C
, float *number
)
223 char *integral
= NULL
;
224 char *fractional
= NULL
;
225 char *exponent
= NULL
;
228 parse_identifier_str(C
, &integral
);
229 parse_identifier_str(C
, &fractional
);
230 parse_identifier_str(C
, &exponent
);
232 whole
= (char *) _slang_alloc((_mesa_strlen(integral
) +
233 _mesa_strlen(fractional
) +
234 _mesa_strlen(exponent
) + 3) * sizeof(char));
236 slang_info_log_memory(C
->L
);
240 slang_string_copy(whole
, integral
);
241 slang_string_concat(whole
, ".");
242 slang_string_concat(whole
, fractional
);
243 slang_string_concat(whole
, "E");
244 slang_string_concat(whole
, exponent
);
246 *number
= (float) (_mesa_strtod(whole
, (char **) NULL
));
253 /* revision number - increment after each change affecting emitted output */
257 check_revision(slang_parse_ctx
* C
)
259 if (*C
->I
!= REVISION
) {
260 slang_info_log_error(C
->L
, "Internal compiler error.");
267 static int parse_statement(slang_parse_ctx
*, slang_output_ctx
*,
269 static int parse_expression(slang_parse_ctx
*, slang_output_ctx
*,
271 static int parse_type_specifier(slang_parse_ctx
*, slang_output_ctx
*,
272 slang_type_specifier
*);
275 parse_array_len(slang_parse_ctx
* C
, slang_output_ctx
* O
, GLuint
* len
)
277 slang_operation array_size
;
278 slang_name_space space
;
281 if (!slang_operation_construct(&array_size
))
283 if (!parse_expression(C
, O
, &array_size
)) {
284 slang_operation_destruct(&array_size
);
288 space
.funcs
= O
->funs
;
289 space
.structs
= O
->structs
;
290 space
.vars
= O
->vars
;
292 /* evaluate compile-time expression which is array size */
293 _slang_simplify(&array_size
, &space
, C
->atoms
);
295 if (array_size
.type
== SLANG_OPER_LITERAL_INT
) {
297 *len
= (GLint
) array_size
.literal
[0];
298 } else if (array_size
.type
== SLANG_OPER_IDENTIFIER
) {
299 slang_variable
*var
= _slang_locate_variable(array_size
.locals
, array_size
.a_id
, GL_TRUE
);
301 slang_info_log_error(C
->L
, "undefined variable '%s'",
302 (char *) array_size
.a_id
);
304 } else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&&
305 var
->type
.specifier
.type
== SLANG_SPEC_INT
) {
306 if (var
->initializer
&&
307 var
->initializer
->type
== SLANG_OPER_LITERAL_INT
) {
308 *len
= (GLint
) var
->initializer
->literal
[0];
311 slang_info_log_error(C
->L
, "unable to parse array size declaration");
315 slang_info_log_error(C
->L
, "unable to parse array size declaration");
322 slang_operation_destruct(&array_size
);
327 calculate_var_size(slang_parse_ctx
* C
, slang_output_ctx
* O
,
328 slang_variable
* var
)
330 slang_storage_aggregate agg
;
332 if (!slang_storage_aggregate_construct(&agg
))
334 if (!_slang_aggregate_variable(&agg
, &var
->type
.specifier
, var
->array_len
,
335 O
->funs
, O
->structs
, O
->vars
, C
->atoms
)) {
336 slang_storage_aggregate_destruct(&agg
);
339 var
->size
= _slang_sizeof_aggregate(&agg
);
340 slang_storage_aggregate_destruct(&agg
);
345 convert_to_array(slang_parse_ctx
* C
, slang_variable
* var
,
346 const slang_type_specifier
* sp
)
348 /* sized array - mark it as array, copy the specifier to the array element
349 * and parse the expression */
350 var
->type
.specifier
.type
= SLANG_SPEC_ARRAY
;
351 var
->type
.specifier
._array
= (slang_type_specifier
*)
352 _slang_alloc(sizeof(slang_type_specifier
));
353 if (var
->type
.specifier
._array
== NULL
) {
354 slang_info_log_memory(C
->L
);
357 slang_type_specifier_ctr(var
->type
.specifier
._array
);
358 return slang_type_specifier_copy(var
->type
.specifier
._array
, sp
);
361 /* structure field */
364 #define FIELD_ARRAY 2
367 parse_struct_field_var(slang_parse_ctx
* C
, slang_output_ctx
* O
,
368 slang_variable
* var
, slang_atom a_name
,
369 const slang_type_specifier
* sp
)
371 var
->a_name
= a_name
;
372 if (var
->a_name
== SLANG_ATOM_NULL
)
377 if (!slang_type_specifier_copy(&var
->type
.specifier
, sp
))
381 if (!convert_to_array(C
, var
, sp
))
383 if (!parse_array_len(C
, O
, &var
->array_len
))
390 return calculate_var_size(C
, O
, var
);
394 parse_struct_field(slang_parse_ctx
* C
, slang_output_ctx
* O
,
395 slang_struct
* st
, slang_type_specifier
* sp
)
397 slang_output_ctx o
= *O
;
399 o
.structs
= st
->structs
;
400 if (!parse_type_specifier(C
, &o
, sp
))
405 slang_variable
*var
= slang_variable_scope_grow(st
->fields
);
407 slang_info_log_memory(C
->L
);
410 a_name
= parse_identifier(C
);
411 if (_slang_locate_variable(st
->fields
, a_name
, GL_FALSE
)) {
412 slang_info_log_error(C
->L
, "duplicate field '%s'", (char *) a_name
);
416 if (!parse_struct_field_var(C
, &o
, var
, a_name
, sp
))
419 while (*C
->I
++ != FIELD_NONE
);
425 parse_struct(slang_parse_ctx
* C
, slang_output_ctx
* O
, slang_struct
** st
)
430 /* parse struct name (if any) and make sure it is unique in current scope */
431 a_name
= parse_identifier(C
);
432 if (a_name
== SLANG_ATOM_NULL
)
435 name
= slang_atom_pool_id(C
->atoms
, a_name
);
437 && slang_struct_scope_find(O
->structs
, a_name
, 0) != NULL
) {
438 slang_info_log_error(C
->L
, "%s: duplicate type name.", name
);
442 /* set-up a new struct */
443 *st
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
445 slang_info_log_memory(C
->L
);
448 if (!slang_struct_construct(*st
)) {
451 slang_info_log_memory(C
->L
);
454 (**st
).a_name
= a_name
;
455 (**st
).structs
->outer_scope
= O
->structs
;
457 /* parse individual struct fields */
459 slang_type_specifier sp
;
461 slang_type_specifier_ctr(&sp
);
462 if (!parse_struct_field(C
, O
, *st
, &sp
)) {
463 slang_type_specifier_dtr(&sp
);
466 slang_type_specifier_dtr(&sp
);
468 while (*C
->I
++ != FIELD_NONE
);
470 /* if named struct, copy it to current scope */
471 if (name
[0] != '\0') {
474 O
->structs
->structs
=
475 (slang_struct
*) _slang_realloc(O
->structs
->structs
,
476 O
->structs
->num_structs
477 * sizeof(slang_struct
),
478 (O
->structs
->num_structs
+ 1)
479 * sizeof(slang_struct
));
480 if (O
->structs
->structs
== NULL
) {
481 slang_info_log_memory(C
->L
);
484 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
485 if (!slang_struct_construct(s
))
487 O
->structs
->num_structs
++;
488 if (!slang_struct_copy(s
, *st
))
496 /* invariant qualifer */
497 #define TYPE_VARIANT 90
498 #define TYPE_INVARIANT 91
501 parse_type_variant(slang_parse_ctx
* C
, slang_type_variant
*variant
)
503 GLuint invariant
= *C
->I
++;
506 *variant
= SLANG_VARIANT
;
509 *variant
= SLANG_INVARIANT
;
517 /* centroid qualifer */
518 #define TYPE_CENTER 95
519 #define TYPE_CENTROID 96
522 parse_type_centroid(slang_parse_ctx
* C
, slang_type_centroid
*centroid
)
527 *centroid
= SLANG_CENTER
;
530 *centroid
= SLANG_CENTROID
;
539 #define TYPE_QUALIFIER_NONE 0
540 #define TYPE_QUALIFIER_CONST 1
541 #define TYPE_QUALIFIER_ATTRIBUTE 2
542 #define TYPE_QUALIFIER_VARYING 3
543 #define TYPE_QUALIFIER_UNIFORM 4
544 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
545 #define TYPE_QUALIFIER_FIXEDINPUT 6
548 parse_type_qualifier(slang_parse_ctx
* C
, slang_type_qualifier
* qual
)
550 GLuint qualifier
= *C
->I
++;
552 case TYPE_QUALIFIER_NONE
:
553 *qual
= SLANG_QUAL_NONE
;
555 case TYPE_QUALIFIER_CONST
:
556 *qual
= SLANG_QUAL_CONST
;
558 case TYPE_QUALIFIER_ATTRIBUTE
:
559 *qual
= SLANG_QUAL_ATTRIBUTE
;
561 case TYPE_QUALIFIER_VARYING
:
562 *qual
= SLANG_QUAL_VARYING
;
564 case TYPE_QUALIFIER_UNIFORM
:
565 *qual
= SLANG_QUAL_UNIFORM
;
567 case TYPE_QUALIFIER_FIXEDOUTPUT
:
568 *qual
= SLANG_QUAL_FIXEDOUTPUT
;
570 case TYPE_QUALIFIER_FIXEDINPUT
:
571 *qual
= SLANG_QUAL_FIXEDINPUT
;
580 #define TYPE_SPECIFIER_VOID 0
581 #define TYPE_SPECIFIER_BOOL 1
582 #define TYPE_SPECIFIER_BVEC2 2
583 #define TYPE_SPECIFIER_BVEC3 3
584 #define TYPE_SPECIFIER_BVEC4 4
585 #define TYPE_SPECIFIER_INT 5
586 #define TYPE_SPECIFIER_IVEC2 6
587 #define TYPE_SPECIFIER_IVEC3 7
588 #define TYPE_SPECIFIER_IVEC4 8
589 #define TYPE_SPECIFIER_FLOAT 9
590 #define TYPE_SPECIFIER_VEC2 10
591 #define TYPE_SPECIFIER_VEC3 11
592 #define TYPE_SPECIFIER_VEC4 12
593 #define TYPE_SPECIFIER_MAT2 13
594 #define TYPE_SPECIFIER_MAT3 14
595 #define TYPE_SPECIFIER_MAT4 15
596 #define TYPE_SPECIFIER_SAMPLER1D 16
597 #define TYPE_SPECIFIER_SAMPLER2D 17
598 #define TYPE_SPECIFIER_SAMPLER3D 18
599 #define TYPE_SPECIFIER_SAMPLERCUBE 19
600 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
601 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
602 #define TYPE_SPECIFIER_SAMPLER2DRECT 22
603 #define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
604 #define TYPE_SPECIFIER_STRUCT 24
605 #define TYPE_SPECIFIER_TYPENAME 25
606 #define TYPE_SPECIFIER_MAT23 26
607 #define TYPE_SPECIFIER_MAT32 27
608 #define TYPE_SPECIFIER_MAT24 28
609 #define TYPE_SPECIFIER_MAT42 29
610 #define TYPE_SPECIFIER_MAT34 30
611 #define TYPE_SPECIFIER_MAT43 31
612 #define TYPE_SPECIFIER_COUNT 32
615 parse_type_specifier(slang_parse_ctx
* C
, slang_output_ctx
* O
,
616 slang_type_specifier
* spec
)
619 case TYPE_SPECIFIER_VOID
:
620 spec
->type
= SLANG_SPEC_VOID
;
622 case TYPE_SPECIFIER_BOOL
:
623 spec
->type
= SLANG_SPEC_BOOL
;
625 case TYPE_SPECIFIER_BVEC2
:
626 spec
->type
= SLANG_SPEC_BVEC2
;
628 case TYPE_SPECIFIER_BVEC3
:
629 spec
->type
= SLANG_SPEC_BVEC3
;
631 case TYPE_SPECIFIER_BVEC4
:
632 spec
->type
= SLANG_SPEC_BVEC4
;
634 case TYPE_SPECIFIER_INT
:
635 spec
->type
= SLANG_SPEC_INT
;
637 case TYPE_SPECIFIER_IVEC2
:
638 spec
->type
= SLANG_SPEC_IVEC2
;
640 case TYPE_SPECIFIER_IVEC3
:
641 spec
->type
= SLANG_SPEC_IVEC3
;
643 case TYPE_SPECIFIER_IVEC4
:
644 spec
->type
= SLANG_SPEC_IVEC4
;
646 case TYPE_SPECIFIER_FLOAT
:
647 spec
->type
= SLANG_SPEC_FLOAT
;
649 case TYPE_SPECIFIER_VEC2
:
650 spec
->type
= SLANG_SPEC_VEC2
;
652 case TYPE_SPECIFIER_VEC3
:
653 spec
->type
= SLANG_SPEC_VEC3
;
655 case TYPE_SPECIFIER_VEC4
:
656 spec
->type
= SLANG_SPEC_VEC4
;
658 case TYPE_SPECIFIER_MAT2
:
659 spec
->type
= SLANG_SPEC_MAT2
;
661 case TYPE_SPECIFIER_MAT3
:
662 spec
->type
= SLANG_SPEC_MAT3
;
664 case TYPE_SPECIFIER_MAT4
:
665 spec
->type
= SLANG_SPEC_MAT4
;
667 case TYPE_SPECIFIER_MAT23
:
668 spec
->type
= SLANG_SPEC_MAT23
;
670 case TYPE_SPECIFIER_MAT32
:
671 spec
->type
= SLANG_SPEC_MAT32
;
673 case TYPE_SPECIFIER_MAT24
:
674 spec
->type
= SLANG_SPEC_MAT24
;
676 case TYPE_SPECIFIER_MAT42
:
677 spec
->type
= SLANG_SPEC_MAT42
;
679 case TYPE_SPECIFIER_MAT34
:
680 spec
->type
= SLANG_SPEC_MAT34
;
682 case TYPE_SPECIFIER_MAT43
:
683 spec
->type
= SLANG_SPEC_MAT43
;
685 case TYPE_SPECIFIER_SAMPLER1D
:
686 spec
->type
= SLANG_SPEC_SAMPLER1D
;
688 case TYPE_SPECIFIER_SAMPLER2D
:
689 spec
->type
= SLANG_SPEC_SAMPLER2D
;
691 case TYPE_SPECIFIER_SAMPLER3D
:
692 spec
->type
= SLANG_SPEC_SAMPLER3D
;
694 case TYPE_SPECIFIER_SAMPLERCUBE
:
695 spec
->type
= SLANG_SPEC_SAMPLERCUBE
;
697 case TYPE_SPECIFIER_SAMPLER2DRECT
:
698 spec
->type
= SLANG_SPEC_SAMPLER2DRECT
;
700 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
701 spec
->type
= SLANG_SPEC_SAMPLER1DSHADOW
;
703 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
704 spec
->type
= SLANG_SPEC_SAMPLER2DSHADOW
;
706 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
707 spec
->type
= SLANG_SPEC_SAMPLER2DRECTSHADOW
;
709 case TYPE_SPECIFIER_STRUCT
:
710 spec
->type
= SLANG_SPEC_STRUCT
;
711 if (!parse_struct(C
, O
, &spec
->_struct
))
714 case TYPE_SPECIFIER_TYPENAME
:
715 spec
->type
= SLANG_SPEC_STRUCT
;
720 a_name
= parse_identifier(C
);
724 stru
= slang_struct_scope_find(O
->structs
, a_name
, 1);
726 slang_info_log_error(C
->L
, "undeclared type name '%s'",
727 slang_atom_pool_id(C
->atoms
, a_name
));
731 spec
->_struct
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
732 if (spec
->_struct
== NULL
) {
733 slang_info_log_memory(C
->L
);
736 if (!slang_struct_construct(spec
->_struct
)) {
737 _slang_free(spec
->_struct
);
738 spec
->_struct
= NULL
;
741 if (!slang_struct_copy(spec
->_struct
, stru
))
752 #define PRECISION_DEFAULT 0
753 #define PRECISION_LOW 1
754 #define PRECISION_MEDIUM 2
755 #define PRECISION_HIGH 3
758 parse_type_precision(slang_parse_ctx
*C
,
759 slang_type_precision
*precision
)
761 GLint prec
= *C
->I
++;
763 case PRECISION_DEFAULT
:
764 *precision
= SLANG_PREC_DEFAULT
;
767 *precision
= SLANG_PREC_LOW
;
769 case PRECISION_MEDIUM
:
770 *precision
= SLANG_PREC_MEDIUM
;
773 *precision
= SLANG_PREC_HIGH
;
781 #define TYPE_ARRAY_SIZE 220
782 #define TYPE_NO_ARRAY_SIZE 221
786 * Parse array size (if present) in something like "uniform float [6] var;".
789 parse_type_array_size(slang_parse_ctx
*C
, slang_output_ctx
* O
,
796 case TYPE_ARRAY_SIZE
:
797 if (!parse_array_len(C
, O
, &sz
))
801 case TYPE_NO_ARRAY_SIZE
:
802 *size
= -1; /* -1 = not an array */
811 parse_fully_specified_type(slang_parse_ctx
* C
, slang_output_ctx
* O
,
812 slang_fully_specified_type
* type
)
814 if (!parse_type_variant(C
, &type
->variant
))
817 if (!parse_type_centroid(C
, &type
->centroid
))
820 if (!parse_type_qualifier(C
, &type
->qualifier
))
823 if (!parse_type_precision(C
, &type
->precision
))
826 if (!parse_type_specifier(C
, O
, &type
->specifier
))
829 if (!parse_type_array_size(C
, O
, &type
->array_len
))
832 if (!O
->allow_invariant
&& type
->variant
== SLANG_INVARIANT
) {
833 slang_info_log_error(C
->L
,
834 "'invariant' keyword not allowed (perhaps set #version 120)");
838 if (!O
->allow_centroid
&& type
->centroid
== SLANG_CENTROID
) {
839 slang_info_log_error(C
->L
,
840 "'centroid' keyword not allowed (perhaps set #version 120)");
843 else if (type
->centroid
== SLANG_CENTROID
&&
844 type
->qualifier
!= SLANG_QUAL_VARYING
) {
845 slang_info_log_error(C
->L
,
846 "'centroid' keyword only allowed for varying vars");
852 if (type->qualifier != SLANG_QUAL_VARYING &&
853 type->variant == SLANG_INVARIANT) {
854 slang_info_log_error(C->L,
855 "invariant qualifer only allowed for varying vars");
860 if (O
->allow_precision
) {
861 if (type
->precision
== SLANG_PREC_DEFAULT
) {
862 assert(type
->specifier
.type
< TYPE_SPECIFIER_COUNT
);
863 /* use the default precision for this datatype */
864 type
->precision
= O
->default_precision
[type
->specifier
.type
];
868 /* only default is allowed */
869 if (type
->precision
!= SLANG_PREC_DEFAULT
) {
870 slang_info_log_error(C
->L
, "precision qualifiers not allowed");
875 if (!O
->allow_array_types
&& type
->array_len
>= 0) {
876 slang_info_log_error(C
->L
, "first-class array types not allowed");
885 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
886 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
890 #define OP_CONTINUE 6
893 #define OP_EXPRESSION 9
898 #define OP_PUSH_VOID 14
899 #define OP_PUSH_BOOL 15
900 #define OP_PUSH_INT 16
901 #define OP_PUSH_FLOAT 17
902 #define OP_PUSH_IDENTIFIER 18
903 #define OP_SEQUENCE 19
905 #define OP_ADDASSIGN 21
906 #define OP_SUBASSIGN 22
907 #define OP_MULASSIGN 23
908 #define OP_DIVASSIGN 24
909 /*#define OP_MODASSIGN 25*/
910 /*#define OP_LSHASSIGN 26*/
911 /*#define OP_RSHASSIGN 27*/
912 /*#define OP_ORASSIGN 28*/
913 /*#define OP_XORASSIGN 29*/
914 /*#define OP_ANDASSIGN 30*/
916 #define OP_LOGICALOR 32
917 #define OP_LOGICALXOR 33
918 #define OP_LOGICALAND 34
919 /*#define OP_BITOR 35*/
920 /*#define OP_BITXOR 36*/
921 /*#define OP_BITAND 37*/
923 #define OP_NOTEQUAL 39
925 #define OP_GREATER 41
926 #define OP_LESSEQUAL 42
927 #define OP_GREATEREQUAL 43
928 /*#define OP_LSHIFT 44*/
929 /*#define OP_RSHIFT 45*/
931 #define OP_SUBTRACT 47
932 #define OP_MULTIPLY 48
934 /*#define OP_MODULUS 50*/
935 #define OP_PREINCREMENT 51
936 #define OP_PREDECREMENT 52
939 /*#define OP_COMPLEMENT 55*/
941 #define OP_SUBSCRIPT 57
944 #define OP_POSTINCREMENT 60
945 #define OP_POSTDECREMENT 61
946 #define OP_PRECISION 62
951 * When parsing a compound production, this function is used to parse the
953 * For example, a while-loop compound will have two children, the
954 * while condition expression and the loop body. So, this function will
955 * be called twice to parse those two sub-expressions.
956 * \param C the parsing context
957 * \param O the output context
958 * \param oper the operation we're parsing
959 * \param statement indicates whether parsing a statement, or expression
960 * \return 1 if success, 0 if error
963 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
964 slang_operation
* oper
, GLboolean statement
)
968 /* grow child array */
969 ch
= slang_operation_grow(&oper
->num_children
, &oper
->children
);
971 return parse_statement(C
, O
, ch
);
972 return parse_expression(C
, O
, ch
);
975 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
978 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
979 slang_operation
* oper
)
981 oper
->locals
->outer_scope
= O
->vars
;
983 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
984 /* parse child statements, do not create new variable scope */
985 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
986 while (*C
->I
!= OP_END
)
987 if (!parse_child_operation(C
, O
, oper
, 1))
991 case OP_BLOCK_BEGIN_NEW_SCOPE
:
992 /* parse child statements, create new variable scope */
994 slang_output_ctx o
= *O
;
996 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
997 o
.vars
= oper
->locals
;
998 while (*C
->I
!= OP_END
)
999 if (!parse_child_operation(C
, &o
, oper
, 1))
1005 /* local variable declaration, individual declarators are stored as
1006 * children identifiers
1008 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1010 const unsigned int first_var
= O
->vars
->num_variables
;
1012 /* parse the declaration, note that there can be zero or more
1013 * than one declarators
1015 if (!parse_declaration(C
, O
))
1017 if (first_var
< O
->vars
->num_variables
) {
1018 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
1020 assert(oper
->num_children
== 0);
1021 oper
->num_children
= num_vars
;
1022 oper
->children
= slang_operation_new(num_vars
);
1023 if (oper
->children
== NULL
) {
1024 slang_info_log_memory(C
->L
);
1027 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
1028 slang_operation
*o
= &oper
->children
[i
- first_var
];
1029 slang_variable
*var
= O
->vars
->variables
[i
];
1030 o
->type
= SLANG_OPER_VARIABLE_DECL
;
1031 o
->locals
->outer_scope
= O
->vars
;
1032 o
->a_id
= var
->a_name
;
1035 calculate_var_size(C, O, var);
1038 if (!legal_identifier(o
->a_id
)) {
1039 slang_info_log_error(C
->L
, "illegal variable name '%s'",
1048 /* the __asm statement, parse the mnemonic and all its arguments
1051 oper
->type
= SLANG_OPER_ASM
;
1052 oper
->a_id
= parse_identifier(C
);
1053 if (oper
->a_id
== SLANG_ATOM_NULL
)
1055 while (*C
->I
!= OP_END
) {
1056 if (!parse_child_operation(C
, O
, oper
, 0))
1062 oper
->type
= SLANG_OPER_BREAK
;
1065 oper
->type
= SLANG_OPER_CONTINUE
;
1068 oper
->type
= SLANG_OPER_DISCARD
;
1071 oper
->type
= SLANG_OPER_RETURN
;
1072 if (!parse_child_operation(C
, O
, oper
, 0))
1076 oper
->type
= SLANG_OPER_EXPRESSION
;
1077 if (!parse_child_operation(C
, O
, oper
, 0))
1081 oper
->type
= SLANG_OPER_IF
;
1082 if (!parse_child_operation(C
, O
, oper
, 0))
1084 if (!parse_child_operation(C
, O
, oper
, 1))
1086 if (!parse_child_operation(C
, O
, oper
, 1))
1091 slang_output_ctx o
= *O
;
1093 oper
->type
= SLANG_OPER_WHILE
;
1094 o
.vars
= oper
->locals
;
1095 if (!parse_child_operation(C
, &o
, oper
, 1))
1097 if (!parse_child_operation(C
, &o
, oper
, 1))
1102 oper
->type
= SLANG_OPER_DO
;
1103 if (!parse_child_operation(C
, O
, oper
, 1))
1105 if (!parse_child_operation(C
, O
, oper
, 0))
1110 slang_output_ctx o
= *O
;
1112 oper
->type
= SLANG_OPER_FOR
;
1113 o
.vars
= oper
->locals
;
1114 if (!parse_child_operation(C
, &o
, oper
, 1))
1116 if (!parse_child_operation(C
, &o
, oper
, 1))
1118 if (!parse_child_operation(C
, &o
, oper
, 0))
1120 if (!parse_child_operation(C
, &o
, oper
, 1))
1126 /* set default precision for a type in this scope */
1127 /* ignored at this time */
1128 int prec_qual
= *C
->I
++;
1129 int datatype
= *C
->I
++;
1141 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
1142 slang_operation
** ops
, unsigned int *total_ops
,
1147 op
->children
= slang_operation_new(n
);
1148 if (op
->children
== NULL
) {
1149 slang_info_log_memory(C
->L
);
1152 op
->num_children
= n
;
1154 for (i
= 0; i
< n
; i
++) {
1155 slang_operation_destruct(&op
->children
[i
]);
1156 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
1159 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
1162 *ops
= (slang_operation
*)
1163 _slang_realloc(*ops
,
1164 (*total_ops
+ n
) * sizeof(slang_operation
),
1165 *total_ops
* sizeof(slang_operation
));
1167 slang_info_log_memory(C
->L
);
1174 is_constructor_name(const char *name
, slang_atom a_name
,
1175 slang_struct_scope
* structs
)
1177 if (slang_type_specifier_type_from_string(name
) != SLANG_SPEC_VOID
)
1179 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
1183 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1184 slang_operation
* oper
)
1186 slang_operation
*ops
= NULL
;
1187 unsigned int num_ops
= 0;
1190 while (*C
->I
!= OP_END
) {
1191 slang_operation
*op
;
1192 const unsigned int op_code
= *C
->I
++;
1194 /* allocate default operation, becomes a no-op if not used */
1195 ops
= (slang_operation
*)
1197 num_ops
* sizeof(slang_operation
),
1198 (num_ops
+ 1) * sizeof(slang_operation
));
1200 slang_info_log_memory(C
->L
);
1204 if (!slang_operation_construct(op
)) {
1205 slang_info_log_memory(C
->L
);
1209 op
->locals
->outer_scope
= O
->vars
;
1213 op
->type
= SLANG_OPER_VOID
;
1216 op
->type
= SLANG_OPER_LITERAL_BOOL
;
1217 if (!parse_number(C
, &number
))
1222 op
->literal
[3] = (GLfloat
) number
;
1223 op
->literal_size
= 1;
1226 op
->type
= SLANG_OPER_LITERAL_INT
;
1227 if (!parse_number(C
, &number
))
1232 op
->literal
[3] = (GLfloat
) number
;
1233 op
->literal_size
= 1;
1236 op
->type
= SLANG_OPER_LITERAL_FLOAT
;
1237 if (!parse_float(C
, &op
->literal
[0]))
1241 op
->literal
[3] = op
->literal
[0];
1242 op
->literal_size
= 1;
1244 case OP_PUSH_IDENTIFIER
:
1245 op
->type
= SLANG_OPER_IDENTIFIER
;
1246 op
->a_id
= parse_identifier(C
);
1247 if (op
->a_id
== SLANG_ATOM_NULL
)
1251 op
->type
= SLANG_OPER_SEQUENCE
;
1252 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1256 op
->type
= SLANG_OPER_ASSIGN
;
1257 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1261 op
->type
= SLANG_OPER_ADDASSIGN
;
1262 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1266 op
->type
= SLANG_OPER_SUBASSIGN
;
1267 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1271 op
->type
= SLANG_OPER_MULASSIGN
;
1272 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1276 op
->type
= SLANG_OPER_DIVASSIGN
;
1277 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1280 /*case OP_MODASSIGN: */
1281 /*case OP_LSHASSIGN: */
1282 /*case OP_RSHASSIGN: */
1283 /*case OP_ORASSIGN: */
1284 /*case OP_XORASSIGN: */
1285 /*case OP_ANDASSIGN: */
1287 op
->type
= SLANG_OPER_SELECT
;
1288 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1292 op
->type
= SLANG_OPER_LOGICALOR
;
1293 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1297 op
->type
= SLANG_OPER_LOGICALXOR
;
1298 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1302 op
->type
= SLANG_OPER_LOGICALAND
;
1303 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1307 /*case OP_BITXOR: */
1308 /*case OP_BITAND: */
1310 op
->type
= SLANG_OPER_EQUAL
;
1311 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1315 op
->type
= SLANG_OPER_NOTEQUAL
;
1316 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1320 op
->type
= SLANG_OPER_LESS
;
1321 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1325 op
->type
= SLANG_OPER_GREATER
;
1326 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1330 op
->type
= SLANG_OPER_LESSEQUAL
;
1331 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1334 case OP_GREATEREQUAL
:
1335 op
->type
= SLANG_OPER_GREATEREQUAL
;
1336 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1339 /*case OP_LSHIFT: */
1340 /*case OP_RSHIFT: */
1342 op
->type
= SLANG_OPER_ADD
;
1343 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1347 op
->type
= SLANG_OPER_SUBTRACT
;
1348 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1352 op
->type
= SLANG_OPER_MULTIPLY
;
1353 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1357 op
->type
= SLANG_OPER_DIVIDE
;
1358 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1361 /*case OP_MODULUS: */
1362 case OP_PREINCREMENT
:
1363 op
->type
= SLANG_OPER_PREINCREMENT
;
1364 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1367 case OP_PREDECREMENT
:
1368 op
->type
= SLANG_OPER_PREDECREMENT
;
1369 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1373 op
->type
= SLANG_OPER_PLUS
;
1374 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1378 op
->type
= SLANG_OPER_MINUS
;
1379 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1383 op
->type
= SLANG_OPER_NOT
;
1384 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1387 /*case OP_COMPLEMENT: */
1389 op
->type
= SLANG_OPER_SUBSCRIPT
;
1390 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1394 op
->type
= SLANG_OPER_METHOD
;
1395 op
->a_obj
= parse_identifier(C
);
1396 if (op
->a_obj
== SLANG_ATOM_NULL
)
1399 op
->a_id
= parse_identifier(C
);
1400 if (op
->a_id
== SLANG_ATOM_NULL
)
1403 while (*C
->I
!= OP_END
)
1404 if (!parse_child_operation(C
, O
, op
, 0))
1408 /* don't lookup the method (not yet anyway) */
1409 if (!C
->parsing_builtin
1410 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1413 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1414 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1415 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1422 op
->type
= SLANG_OPER_CALL
;
1423 op
->a_id
= parse_identifier(C
);
1424 if (op
->a_id
== SLANG_ATOM_NULL
)
1426 while (*C
->I
!= OP_END
)
1427 if (!parse_child_operation(C
, O
, op
, 0))
1431 if (!C
->parsing_builtin
1432 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1435 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1436 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1437 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1443 op
->type
= SLANG_OPER_FIELD
;
1444 op
->a_id
= parse_identifier(C
);
1445 if (op
->a_id
== SLANG_ATOM_NULL
)
1447 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1450 case OP_POSTINCREMENT
:
1451 op
->type
= SLANG_OPER_POSTINCREMENT
;
1452 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1455 case OP_POSTDECREMENT
:
1456 op
->type
= SLANG_OPER_POSTDECREMENT
;
1457 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1466 slang_operation_destruct(oper
);
1467 *oper
= *ops
; /* struct copy */
1473 /* parameter qualifier */
1474 #define PARAM_QUALIFIER_IN 0
1475 #define PARAM_QUALIFIER_OUT 1
1476 #define PARAM_QUALIFIER_INOUT 2
1478 /* function parameter array presence */
1479 #define PARAMETER_ARRAY_NOT_PRESENT 0
1480 #define PARAMETER_ARRAY_PRESENT 1
1483 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1484 slang_variable
* param
)
1486 int param_qual
, precision_qual
;
1488 /* parse and validate the parameter's type qualifiers (there can be
1489 * two at most) because not all combinations are valid
1491 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1494 param_qual
= *C
->I
++;
1495 switch (param_qual
) {
1496 case PARAM_QUALIFIER_IN
:
1497 if (param
->type
.qualifier
!= SLANG_QUAL_CONST
1498 && param
->type
.qualifier
!= SLANG_QUAL_NONE
) {
1499 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1503 case PARAM_QUALIFIER_OUT
:
1504 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1505 param
->type
.qualifier
= SLANG_QUAL_OUT
;
1507 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1511 case PARAM_QUALIFIER_INOUT
:
1512 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1513 param
->type
.qualifier
= SLANG_QUAL_INOUT
;
1515 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1523 /* parse precision qualifier (lowp, mediump, highp */
1524 precision_qual
= *C
->I
++;
1525 /* ignored at this time */
1526 (void) precision_qual
;
1528 /* parse parameter's type specifier and name */
1529 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1531 param
->a_name
= parse_identifier(C
);
1532 if (param
->a_name
== SLANG_ATOM_NULL
)
1535 /* if the parameter is an array, parse its size (the size must be
1536 * explicitly defined
1538 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1539 slang_type_specifier p
;
1541 slang_type_specifier_ctr(&p
);
1542 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1543 slang_type_specifier_dtr(&p
);
1546 if (!convert_to_array(C
, param
, &p
)) {
1547 slang_type_specifier_dtr(&p
);
1550 slang_type_specifier_dtr(&p
);
1551 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1555 /* calculate the parameter size */
1556 if (!calculate_var_size(C
, O
, param
))
1559 /* TODO: allocate the local address here? */
1564 #define FUNCTION_ORDINARY 0
1565 #define FUNCTION_CONSTRUCTOR 1
1566 #define FUNCTION_OPERATOR 2
1568 /* function parameter */
1569 #define PARAMETER_NONE 0
1570 #define PARAMETER_NEXT 1
1573 #define OPERATOR_ADDASSIGN 1
1574 #define OPERATOR_SUBASSIGN 2
1575 #define OPERATOR_MULASSIGN 3
1576 #define OPERATOR_DIVASSIGN 4
1577 /*#define OPERATOR_MODASSIGN 5*/
1578 /*#define OPERATOR_LSHASSIGN 6*/
1579 /*#define OPERATOR_RSHASSIGN 7*/
1580 /*#define OPERATOR_ANDASSIGN 8*/
1581 /*#define OPERATOR_XORASSIGN 9*/
1582 /*#define OPERATOR_ORASSIGN 10*/
1583 #define OPERATOR_LOGICALXOR 11
1584 /*#define OPERATOR_BITOR 12*/
1585 /*#define OPERATOR_BITXOR 13*/
1586 /*#define OPERATOR_BITAND 14*/
1587 #define OPERATOR_LESS 15
1588 #define OPERATOR_GREATER 16
1589 #define OPERATOR_LESSEQUAL 17
1590 #define OPERATOR_GREATEREQUAL 18
1591 /*#define OPERATOR_LSHIFT 19*/
1592 /*#define OPERATOR_RSHIFT 20*/
1593 #define OPERATOR_MULTIPLY 21
1594 #define OPERATOR_DIVIDE 22
1595 /*#define OPERATOR_MODULUS 23*/
1596 #define OPERATOR_INCREMENT 24
1597 #define OPERATOR_DECREMENT 25
1598 #define OPERATOR_PLUS 26
1599 #define OPERATOR_MINUS 27
1600 /*#define OPERATOR_COMPLEMENT 28*/
1601 #define OPERATOR_NOT 29
1605 unsigned int o_code
;
1607 } operator_names
[] = {
1608 {OPERATOR_INCREMENT
, "++"},
1609 {OPERATOR_ADDASSIGN
, "+="},
1610 {OPERATOR_PLUS
, "+"},
1611 {OPERATOR_DECREMENT
, "--"},
1612 {OPERATOR_SUBASSIGN
, "-="},
1613 {OPERATOR_MINUS
, "-"},
1614 {OPERATOR_NOT
, "!"},
1615 {OPERATOR_MULASSIGN
, "*="},
1616 {OPERATOR_MULTIPLY
, "*"},
1617 {OPERATOR_DIVASSIGN
, "/="},
1618 {OPERATOR_DIVIDE
, "/"},
1619 {OPERATOR_LESSEQUAL
, "<="},
1620 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1621 /*{ OPERATOR_LSHIFT, "<<" }, */
1622 {OPERATOR_LESS
, "<"},
1623 {OPERATOR_GREATEREQUAL
, ">="},
1624 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1625 /*{ OPERATOR_RSHIFT, ">>" }, */
1626 {OPERATOR_GREATER
, ">"},
1627 /*{ OPERATOR_MODASSIGN, "%=" }, */
1628 /*{ OPERATOR_MODULUS, "%" }, */
1629 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1630 /*{ OPERATOR_BITAND, "&" }, */
1631 /*{ OPERATOR_ORASSIGN, "|=" }, */
1632 /*{ OPERATOR_BITOR, "|" }, */
1633 /*{ OPERATOR_COMPLEMENT, "~" }, */
1634 /*{ OPERATOR_XORASSIGN, "^=" }, */
1635 {OPERATOR_LOGICALXOR
, "^^"},
1636 /*{ OPERATOR_BITXOR, "^" } */
1640 parse_operator_name(slang_parse_ctx
* C
)
1644 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1645 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1647 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1648 if (atom
== SLANG_ATOM_NULL
) {
1649 slang_info_log_memory(C
->L
);
1661 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1662 slang_function
* func
)
1665 /* parse function type and name */
1666 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1671 case FUNCTION_ORDINARY
:
1672 func
->kind
= SLANG_FUNC_ORDINARY
;
1673 func
->header
.a_name
= parse_identifier(C
);
1674 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1677 case FUNCTION_CONSTRUCTOR
:
1678 func
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1679 if (func
->header
.type
.specifier
.type
== SLANG_SPEC_STRUCT
)
1681 func
->header
.a_name
=
1682 slang_atom_pool_atom(C
->atoms
,
1683 slang_type_specifier_type_to_string
1684 (func
->header
.type
.specifier
.type
));
1685 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1686 slang_info_log_memory(C
->L
);
1690 case FUNCTION_OPERATOR
:
1691 func
->kind
= SLANG_FUNC_OPERATOR
;
1692 func
->header
.a_name
= parse_operator_name(C
);
1693 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1700 if (!legal_identifier(func
->header
.a_name
)) {
1701 slang_info_log_error(C
->L
, "illegal function name '%s'",
1702 (char *) func
->header
.a_name
);
1706 /* parse function parameters */
1707 while (*C
->I
++ == PARAMETER_NEXT
) {
1708 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1710 slang_info_log_memory(C
->L
);
1713 if (!parse_parameter_declaration(C
, O
, p
))
1717 /* if the function returns a value, append a hidden __retVal 'out'
1718 * parameter that corresponds to the return value.
1720 if (_slang_function_has_return_value(func
)) {
1721 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1722 slang_atom a_retVal
= slang_atom_pool_atom(C
->atoms
, "__retVal");
1724 p
->a_name
= a_retVal
;
1725 p
->type
= func
->header
.type
;
1726 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1729 /* function formal parameters and local variables share the same
1730 * scope, so save the information about param count in a seperate
1731 * place also link the scope to the global variable scope so when a
1732 * given identifier is not found here, the search process continues
1733 * in the global space
1735 func
->param_count
= func
->parameters
->num_variables
;
1736 func
->parameters
->outer_scope
= O
->vars
;
1742 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1743 slang_function
* func
)
1745 slang_output_ctx o
= *O
;
1747 if (!parse_function_prototype(C
, O
, func
))
1750 /* create function's body operation */
1751 func
->body
= (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1752 if (func
->body
== NULL
) {
1753 slang_info_log_memory(C
->L
);
1756 if (!slang_operation_construct(func
->body
)) {
1757 _slang_free(func
->body
);
1759 slang_info_log_memory(C
->L
);
1763 /* to parse the body the parse context is modified in order to
1764 * capture parsed variables into function's local variable scope
1766 C
->global_scope
= GL_FALSE
;
1767 o
.vars
= func
->parameters
;
1768 if (!parse_statement(C
, &o
, func
->body
))
1771 C
->global_scope
= GL_TRUE
;
1776 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1778 slang_operation op_id
, op_assign
;
1781 /* construct the left side of assignment */
1782 if (!slang_operation_construct(&op_id
))
1784 op_id
.type
= SLANG_OPER_IDENTIFIER
;
1785 op_id
.a_id
= var
->a_name
;
1787 /* put the variable into operation's scope */
1788 op_id
.locals
->variables
=
1789 (slang_variable
**) _slang_alloc(sizeof(slang_variable
*));
1790 if (op_id
.locals
->variables
== NULL
) {
1791 slang_operation_destruct(&op_id
);
1794 op_id
.locals
->num_variables
= 1;
1795 op_id
.locals
->variables
[0] = var
;
1797 /* construct the assignment expression */
1798 if (!slang_operation_construct(&op_assign
)) {
1799 op_id
.locals
->num_variables
= 0;
1800 slang_operation_destruct(&op_id
);
1803 op_assign
.type
= SLANG_OPER_ASSIGN
;
1804 op_assign
.children
=
1805 (slang_operation
*) _slang_alloc(2 * sizeof(slang_operation
));
1806 if (op_assign
.children
== NULL
) {
1807 slang_operation_destruct(&op_assign
);
1808 op_id
.locals
->num_variables
= 0;
1809 slang_operation_destruct(&op_id
);
1812 op_assign
.num_children
= 2;
1813 op_assign
.children
[0] = op_id
;
1814 op_assign
.children
[1] = *var
->initializer
;
1818 /* carefully destroy the operations */
1819 op_assign
.num_children
= 0;
1820 _slang_free(op_assign
.children
);
1821 op_assign
.children
= NULL
;
1822 slang_operation_destruct(&op_assign
);
1823 op_id
.locals
->num_variables
= 0;
1824 slang_operation_destruct(&op_id
);
1832 /* init declarator list */
1833 #define DECLARATOR_NONE 0
1834 #define DECLARATOR_NEXT 1
1836 /* variable declaration */
1837 #define VARIABLE_NONE 0
1838 #define VARIABLE_IDENTIFIER 1
1839 #define VARIABLE_INITIALIZER 2
1840 #define VARIABLE_ARRAY_EXPLICIT 3
1841 #define VARIABLE_ARRAY_UNKNOWN 4
1845 * Parse the initializer for a variable declaration.
1848 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1849 const slang_fully_specified_type
* type
)
1851 slang_variable
*var
;
1854 /* empty init declatator (without name, e.g. "float ;") */
1855 if (*C
->I
++ == VARIABLE_NONE
)
1858 a_name
= parse_identifier(C
);
1860 /* check if name is already in this scope */
1861 if (_slang_locate_variable(O
->vars
, a_name
, GL_FALSE
)) {
1862 slang_info_log_error(C
->L
,
1863 "declaration of '%s' conflicts with previous declaration",
1868 /* make room for the new variable and initialize it */
1869 var
= slang_variable_scope_grow(O
->vars
);
1871 slang_info_log_memory(C
->L
);
1875 /* copy the declarator type qualifier/etc info, parse the identifier */
1876 var
->type
.qualifier
= type
->qualifier
;
1877 var
->type
.centroid
= type
->centroid
;
1878 var
->type
.precision
= type
->precision
;
1879 var
->type
.variant
= type
->variant
;
1880 var
->a_name
= a_name
;
1881 if (var
->a_name
== SLANG_ATOM_NULL
)
1886 /* simple variable declarator - just copy the specifier */
1887 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1890 case VARIABLE_INITIALIZER
:
1891 /* initialized variable - copy the specifier and parse the expression */
1892 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1895 (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1896 if (var
->initializer
== NULL
) {
1897 slang_info_log_memory(C
->L
);
1900 if (!slang_operation_construct(var
->initializer
)) {
1901 _slang_free(var
->initializer
);
1902 var
->initializer
= NULL
;
1903 slang_info_log_memory(C
->L
);
1906 if (!parse_expression(C
, O
, var
->initializer
))
1909 case VARIABLE_ARRAY_UNKNOWN
:
1910 /* unsized array - mark it as array and copy the specifier to
1913 if (!convert_to_array(C
, var
, &type
->specifier
))
1916 case VARIABLE_ARRAY_EXPLICIT
:
1917 if (!convert_to_array(C
, var
, &type
->specifier
))
1919 if (!parse_array_len(C
, O
, &var
->array_len
))
1926 if (type
->array_len
>= 0) {
1927 /* The type was something like "float[4]" */
1928 if (var
->array_len
!= 0) {
1929 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
1932 convert_to_array(C
, var
, &type
->specifier
);
1933 var
->array_len
= type
->array_len
;
1936 /* allocate global address space for a variable with a known size */
1938 && !(var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
1939 && var
->array_len
== 0)) {
1940 if (!calculate_var_size(C
, O
, var
))
1942 var
->address
= slang_var_pool_alloc(O
->global_pool
, var
->size
);
1945 /* emit code for global var decl */
1946 if (C
->global_scope
) {
1947 slang_assemble_ctx A
;
1949 A
.space
.funcs
= O
->funs
;
1950 A
.space
.structs
= O
->structs
;
1951 A
.space
.vars
= O
->vars
;
1952 A
.program
= O
->program
;
1953 A
.vartable
= O
->vartable
;
1955 A
.curFuncEndLabel
= NULL
;
1956 if (!_slang_codegen_global_variable(&A
, var
, C
->type
))
1960 /* initialize global variable */
1961 if (C
->global_scope
) {
1962 if (var
->initializer
!= NULL
) {
1963 slang_assemble_ctx A
;
1966 A
.space
.funcs
= O
->funs
;
1967 A
.space
.structs
= O
->structs
;
1968 A
.space
.vars
= O
->vars
;
1969 if (!initialize_global(&A
, var
))
1977 * Parse a list of variable declarations. Each variable may have an
1981 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1983 slang_fully_specified_type type
;
1985 /* parse the fully specified type, common to all declarators */
1986 if (!slang_fully_specified_type_construct(&type
))
1988 if (!parse_fully_specified_type(C
, O
, &type
)) {
1989 slang_fully_specified_type_destruct(&type
);
1993 /* parse declarators, pass-in the parsed type */
1995 if (!parse_init_declarator(C
, O
, &type
)) {
1996 slang_fully_specified_type_destruct(&type
);
2000 while (*C
->I
++ == DECLARATOR_NEXT
);
2002 slang_fully_specified_type_destruct(&type
);
2008 * Parse a function definition or declaration.
2009 * \param C parsing context
2010 * \param O output context
2011 * \param definition if non-zero expect a definition, else a declaration
2012 * \param parsed_func_ret returns the parsed function
2013 * \return GL_TRUE if success, GL_FALSE if failure
2016 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
2017 slang_function
** parsed_func_ret
)
2019 slang_function parsed_func
, *found_func
;
2021 /* parse function definition/declaration */
2022 if (!slang_function_construct(&parsed_func
))
2025 if (!parse_function_definition(C
, O
, &parsed_func
)) {
2026 slang_function_destruct(&parsed_func
);
2031 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
2032 slang_function_destruct(&parsed_func
);
2037 /* find a function with a prototype matching the parsed one - only
2038 * the current scope is being searched to allow built-in function
2041 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
2042 if (found_func
== NULL
) {
2043 /* New function, add it to the function list */
2044 O
->funs
->functions
=
2045 (slang_function
*) _slang_realloc(O
->funs
->functions
,
2046 O
->funs
->num_functions
2047 * sizeof(slang_function
),
2048 (O
->funs
->num_functions
+ 1)
2049 * sizeof(slang_function
));
2050 if (O
->funs
->functions
== NULL
) {
2051 slang_info_log_memory(C
->L
);
2052 slang_function_destruct(&parsed_func
);
2055 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
2056 O
->funs
->num_functions
++;
2058 /* return the newly parsed function */
2059 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
2062 /* previously defined or declared */
2063 /* TODO: check function return type qualifiers and specifiers */
2065 if (found_func
->body
!= NULL
) {
2066 slang_info_log_error(C
->L
, "%s: function already has a body.",
2067 slang_atom_pool_id(C
->atoms
,
2070 slang_function_destruct(&parsed_func
);
2074 /* destroy the existing function declaration and replace it
2075 * with the new one, remember to save the fixup table
2077 parsed_func
.fixups
= found_func
->fixups
;
2078 slang_fixup_table_init(&found_func
->fixups
);
2079 slang_function_destruct(found_func
);
2080 *found_func
= parsed_func
;
2083 /* another declaration of the same function prototype - ignore it */
2084 slang_function_destruct(&parsed_func
);
2087 /* return the found function */
2088 *parsed_func_ret
= found_func
;
2095 #define DECLARATION_FUNCTION_PROTOTYPE 1
2096 #define DECLARATION_INIT_DECLARATOR_LIST 2
2099 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2102 case DECLARATION_INIT_DECLARATOR_LIST
:
2103 if (!parse_init_declarator_list(C
, O
))
2106 case DECLARATION_FUNCTION_PROTOTYPE
:
2108 slang_function
*dummy_func
;
2110 if (!parse_function(C
, O
, 0, &dummy_func
))
2121 parse_default_precision(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2123 int precision
, type
;
2125 if (!O
->allow_precision
) {
2126 slang_info_log_error(C
->L
, "syntax error at \"precision\"");
2130 precision
= *C
->I
++;
2131 switch (precision
) {
2133 case PRECISION_MEDIUM
:
2134 case PRECISION_HIGH
:
2138 _mesa_problem(NULL
, "unexpected precision %d at %s:%d\n",
2139 precision
, __FILE__
, __LINE__
);
2145 case TYPE_SPECIFIER_FLOAT
:
2146 case TYPE_SPECIFIER_INT
:
2147 case TYPE_SPECIFIER_SAMPLER1D
:
2148 case TYPE_SPECIFIER_SAMPLER2D
:
2149 case TYPE_SPECIFIER_SAMPLER3D
:
2150 case TYPE_SPECIFIER_SAMPLERCUBE
:
2151 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
2152 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
2153 case TYPE_SPECIFIER_SAMPLER2DRECT
:
2154 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
2158 _mesa_problem(NULL
, "unexpected type %d at %s:%d\n",
2159 type
, __FILE__
, __LINE__
);
2163 assert(type
< TYPE_SPECIFIER_COUNT
);
2164 O
->default_precision
[type
] = precision
;
2171 * Initialize the default precision for all types.
2172 * XXX this info isn't used yet.
2175 init_default_precision(slang_output_ctx
*O
, slang_unit_type type
)
2178 for (i
= 0; i
< TYPE_SPECIFIER_COUNT
; i
++) {
2179 #if FEATURE_es2_glsl
2180 O
->default_precision
[i
] = PRECISION_LOW
;
2182 O
->default_precision
[i
] = PRECISION_HIGH
;
2186 if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2187 O
->default_precision
[TYPE_SPECIFIER_FLOAT
] = PRECISION_HIGH
;
2188 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_HIGH
;
2191 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_MEDIUM
;
2197 parse_invariant(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2199 if (O
->allow_invariant
) {
2200 slang_atom
*a
= parse_identifier(C
);
2201 /* XXX not doing anything with this var yet */
2202 /*printf("ID: %s\n", (char*) a);*/
2206 slang_info_log_error(C
->L
, "syntax error at \"invariant\"");
2212 /* external declaration or default precision specifier */
2213 #define EXTERNAL_NULL 0
2214 #define EXTERNAL_FUNCTION_DEFINITION 1
2215 #define EXTERNAL_DECLARATION 2
2216 #define DEFAULT_PRECISION 3
2217 #define INVARIANT_STMT 4
2221 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
,
2222 struct gl_shader
*shader
)
2224 GET_CURRENT_CONTEXT(ctx
);
2228 slang_function
*mainFunc
= NULL
;
2230 if (unit
->type
== SLANG_UNIT_FRAGMENT_BUILTIN
||
2231 unit
->type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2232 maxRegs
= ctx
->Const
.FragmentProgram
.MaxTemps
;
2235 assert(unit
->type
== SLANG_UNIT_VERTEX_BUILTIN
||
2236 unit
->type
== SLANG_UNIT_VERTEX_SHADER
);
2237 maxRegs
= ctx
->Const
.VertexProgram
.MaxTemps
;
2240 /* setup output context */
2241 o
.funs
= &unit
->funs
;
2242 o
.structs
= &unit
->structs
;
2243 o
.vars
= &unit
->vars
;
2244 o
.global_pool
= &unit
->object
->varpool
;
2245 o
.program
= shader
? shader
->Program
: NULL
;
2246 o
.vartable
= _slang_new_var_table(maxRegs
);
2247 _slang_push_var_table(o
.vartable
);
2249 /* allow 'invariant' keyword? */
2250 #if FEATURE_es2_glsl
2251 o
.allow_invariant
= GL_TRUE
;
2253 o
.allow_invariant
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2256 /* allow 'centroid' keyword? */
2257 o
.allow_centroid
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2259 /* allow 'lowp/mediump/highp' keywords? */
2260 #if FEATURE_es2_glsl
2261 o
.allow_precision
= GL_TRUE
;
2263 o
.allow_precision
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2265 init_default_precision(&o
, unit
->type
);
2267 /* allow 'float[]' keyword? */
2268 o
.allow_array_types
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2270 /* parse individual functions and declarations */
2271 while (*C
->I
!= EXTERNAL_NULL
) {
2273 case EXTERNAL_FUNCTION_DEFINITION
:
2275 slang_function
*func
;
2276 success
= parse_function(C
, &o
, 1, &func
);
2278 _mesa_strcmp((char *) func
->header
.a_name
, "main") == 0) {
2284 case EXTERNAL_DECLARATION
:
2285 success
= parse_declaration(C
, &o
);
2287 case DEFAULT_PRECISION
:
2288 success
= parse_default_precision(C
, &o
);
2290 case INVARIANT_STMT
:
2291 success
= parse_invariant(C
, &o
);
2298 /* xxx free codegen */
2299 _slang_pop_var_table(o
.vartable
);
2306 /* assemble (generate code) for main() */
2307 slang_assemble_ctx A
;
2310 A
.space
.funcs
= o
.funs
;
2311 A
.space
.structs
= o
.structs
;
2312 A
.space
.vars
= o
.vars
;
2313 A
.program
= o
.program
;
2314 A
.vartable
= o
.vartable
;
2317 /* main() takes no parameters */
2318 if (mainFunc
->param_count
> 0) {
2319 slang_info_log_error(A
.log
, "main() takes no arguments");
2323 _slang_codegen_function(&A
, mainFunc
);
2325 shader
->Main
= GL_TRUE
; /* this shader defines main() */
2328 _slang_pop_var_table(o
.vartable
);
2329 _slang_delete_var_table(o
.vartable
);
2335 compile_binary(const byte
* prod
, slang_code_unit
* unit
,
2337 slang_unit_type type
, slang_info_log
* infolog
,
2338 slang_code_unit
* builtin
, slang_code_unit
* downlink
,
2339 struct gl_shader
*shader
)
2345 /* setup parse context */
2348 C
.parsing_builtin
= (builtin
== NULL
);
2349 C
.global_scope
= GL_TRUE
;
2350 C
.atoms
= &unit
->object
->atompool
;
2352 C
.version
= version
;
2354 if (!check_revision(&C
))
2357 if (downlink
!= NULL
) {
2358 unit
->vars
.outer_scope
= &downlink
->vars
;
2359 unit
->funs
.outer_scope
= &downlink
->funs
;
2360 unit
->structs
.outer_scope
= &downlink
->structs
;
2363 /* parse translation unit */
2364 return parse_code_unit(&C
, unit
, shader
);
2368 compile_with_grammar(grammar id
, const char *source
, slang_code_unit
* unit
,
2369 slang_unit_type type
, slang_info_log
* infolog
,
2370 slang_code_unit
* builtin
,
2371 struct gl_shader
*shader
)
2374 GLuint size
, start
, version
;
2375 slang_string preprocessed
;
2378 #if FEATURE_ARB_shading_language_120
2380 #elif FEATURE_es2_glsl
2386 /* First retrieve the version number. */
2387 if (!_slang_preprocess_version(source
, &version
, &start
, infolog
))
2390 if (version
> maxVersion
) {
2391 slang_info_log_error(infolog
,
2392 "language version %.2f is not supported.",
2397 /* Now preprocess the source string. */
2398 slang_string_init(&preprocessed
);
2399 if (!_slang_preprocess_directives(&preprocessed
, &source
[start
], infolog
)) {
2400 slang_string_free(&preprocessed
);
2401 slang_info_log_error(infolog
, "failed to preprocess the source.");
2405 /* Finally check the syntax and generate its binary representation. */
2406 if (!grammar_fast_check(id
,
2407 (const byte
*) (slang_string_cstr(&preprocessed
)),
2408 &prod
, &size
, 65536)) {
2412 slang_string_free(&preprocessed
);
2413 grammar_get_last_error((byte
*) (buf
), sizeof(buf
), &pos
);
2414 slang_info_log_error(infolog
, buf
);
2415 /* syntax error (possibly in library code) */
2420 s
= (char *) _mesa_find_line_column((const GLubyte
*) source
,
2421 (const GLubyte
*) source
+ pos
,
2423 printf("Error on line %d, col %d: %s\n", line
, col
, s
);
2428 slang_string_free(&preprocessed
);
2430 /* Syntax is okay - translate it to internal representation. */
2431 if (!compile_binary(prod
, unit
, version
, type
, infolog
, builtin
,
2432 &builtin
[SLANG_BUILTIN_TOTAL
- 1],
2434 grammar_alloc_free(prod
);
2437 grammar_alloc_free(prod
);
2441 LONGSTRING
static const char *slang_shader_syn
=
2442 #include "library/slang_shader_syn.h"
2445 static const byte slang_core_gc
[] = {
2446 #include "library/slang_core_gc.h"
2449 static const byte slang_120_core_gc
[] = {
2450 #include "library/slang_120_core_gc.h"
2453 static const byte slang_120_fragment_gc
[] = {
2454 #include "library/slang_builtin_120_fragment_gc.h"
2457 static const byte slang_common_builtin_gc
[] = {
2458 #include "library/slang_common_builtin_gc.h"
2461 static const byte slang_fragment_builtin_gc
[] = {
2462 #include "library/slang_fragment_builtin_gc.h"
2465 static const byte slang_vertex_builtin_gc
[] = {
2466 #include "library/slang_vertex_builtin_gc.h"
2470 compile_object(grammar
* id
, const char *source
, slang_code_object
* object
,
2471 slang_unit_type type
, slang_info_log
* infolog
,
2472 struct gl_shader
*shader
)
2474 slang_code_unit
*builtins
= NULL
;
2475 GLuint base_version
= 110;
2477 /* load GLSL grammar */
2478 *id
= grammar_load_from_text((const byte
*) (slang_shader_syn
));
2483 grammar_get_last_error(buf
, 1024, &pos
);
2484 slang_info_log_error(infolog
, (const char *) (buf
));
2488 /* set shader type - the syntax is slightly different for different shaders */
2489 if (type
== SLANG_UNIT_FRAGMENT_SHADER
2490 || type
== SLANG_UNIT_FRAGMENT_BUILTIN
)
2491 grammar_set_reg8(*id
, (const byte
*) "shader_type", 1);
2493 grammar_set_reg8(*id
, (const byte
*) "shader_type", 2);
2495 /* enable language extensions */
2496 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2498 /* if parsing user-specified shader, load built-in library */
2499 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_VERTEX_SHADER
) {
2500 /* compile core functionality first */
2501 if (!compile_binary(slang_core_gc
,
2502 &object
->builtin
[SLANG_BUILTIN_CORE
],
2504 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2508 #if FEATURE_ARB_shading_language_120
2509 if (!compile_binary(slang_120_core_gc
,
2510 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2512 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2513 NULL
, &object
->builtin
[SLANG_BUILTIN_CORE
], NULL
))
2517 /* compile common functions and variables, link to core */
2518 if (!compile_binary(slang_common_builtin_gc
,
2519 &object
->builtin
[SLANG_BUILTIN_COMMON
],
2520 #if FEATURE_ARB_shading_language_120
2525 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2526 #if FEATURE_ARB_shading_language_120
2527 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2529 &object
->builtin
[SLANG_BUILTIN_CORE
],
2534 /* compile target-specific functions and variables, link to common */
2535 if (type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2536 if (!compile_binary(slang_fragment_builtin_gc
,
2537 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2539 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2540 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2542 #if FEATURE_ARB_shading_language_120
2543 if (!compile_binary(slang_120_fragment_gc
,
2544 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2546 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2547 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2551 else if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2552 if (!compile_binary(slang_vertex_builtin_gc
,
2553 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2555 SLANG_UNIT_VERTEX_BUILTIN
, infolog
, NULL
,
2556 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2560 /* disable language extensions */
2561 #if NEW_SLANG /* allow-built-ins */
2562 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2564 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 0);
2566 builtins
= object
->builtin
;
2569 /* compile the actual shader - pass-in built-in library for external shader */
2570 return compile_with_grammar(*id
, source
, &object
->unit
, type
, infolog
,
2576 compile_shader(GLcontext
*ctx
, slang_code_object
* object
,
2577 slang_unit_type type
, slang_info_log
* infolog
,
2578 struct gl_shader
*shader
)
2583 #if 0 /* for debug */
2584 _mesa_printf("********* COMPILE SHADER ***********\n");
2585 _mesa_printf("%s\n", shader
->Source
);
2586 _mesa_printf("************************************\n");
2589 assert(shader
->Program
);
2591 _slang_code_object_dtr(object
);
2592 _slang_code_object_ctr(object
);
2594 success
= compile_object(&id
, shader
->Source
, object
, type
, infolog
, shader
);
2596 grammar_destroy(id
);
2606 _slang_compile(GLcontext
*ctx
, struct gl_shader
*shader
)
2609 slang_info_log info_log
;
2610 slang_code_object obj
;
2611 slang_unit_type type
;
2613 if (shader
->Type
== GL_VERTEX_SHADER
) {
2614 type
= SLANG_UNIT_VERTEX_SHADER
;
2617 assert(shader
->Type
== GL_FRAGMENT_SHADER
);
2618 type
= SLANG_UNIT_FRAGMENT_SHADER
;
2621 if (!shader
->Source
)
2624 ctx
->Shader
.MemPool
= _slang_new_mempool(1024*1024);
2626 shader
->Main
= GL_FALSE
;
2628 if (!shader
->Program
) {
2630 if (shader
->Type
== GL_VERTEX_SHADER
)
2631 progTarget
= GL_VERTEX_PROGRAM_ARB
;
2633 progTarget
= GL_FRAGMENT_PROGRAM_ARB
;
2634 shader
->Program
= ctx
->Driver
.NewProgram(ctx
, progTarget
, 1);
2635 shader
->Program
->Parameters
= _mesa_new_parameter_list();
2636 shader
->Program
->Varying
= _mesa_new_parameter_list();
2637 shader
->Program
->Attributes
= _mesa_new_parameter_list();
2640 slang_info_log_construct(&info_log
);
2641 _slang_code_object_ctr(&obj
);
2643 success
= compile_shader(ctx
, &obj
, type
, &info_log
, shader
);
2645 /* free shader's prev info log */
2646 if (shader
->InfoLog
) {
2647 _mesa_free(shader
->InfoLog
);
2648 shader
->InfoLog
= NULL
;
2651 if (info_log
.text
) {
2652 /* copy info-log string to shader object */
2653 shader
->InfoLog
= _mesa_strdup(info_log
.text
);
2656 if (info_log
.error_flag
) {
2660 slang_info_log_destruct(&info_log
);
2661 _slang_code_object_dtr(&obj
);
2663 _slang_delete_mempool((slang_mempool
*) ctx
->Shader
.MemPool
);
2664 ctx
->Shader
.MemPool
= NULL
;
2666 /* remove any reads of output registers */
2668 printf("Pre-remove output reads:\n");
2669 _mesa_print_program(shader
->Program
);
2671 _mesa_remove_output_reads(shader
->Program
, PROGRAM_OUTPUT
);
2672 if (shader
->Type
== GL_VERTEX_SHADER
) {
2673 /* and remove writes to varying vars in vertex programs */
2674 _mesa_remove_output_reads(shader
->Program
, PROGRAM_VARYING
);
2677 printf("Post-remove output reads:\n");
2678 _mesa_print_program(shader
->Program
);