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
950 * When parsing a compound production, this function is used to parse the
952 * For example, a while-loop compound will have two children, the
953 * while condition expression and the loop body. So, this function will
954 * be called twice to parse those two sub-expressions.
955 * \param C the parsing context
956 * \param O the output context
957 * \param oper the operation we're parsing
958 * \param statement indicates whether parsing a statement, or expression
959 * \return 1 if success, 0 if error
962 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
963 slang_operation
* oper
, GLboolean statement
)
967 /* grow child array */
968 ch
= slang_operation_grow(&oper
->num_children
, &oper
->children
);
970 return parse_statement(C
, O
, ch
);
971 return parse_expression(C
, O
, ch
);
974 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
977 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
978 slang_operation
* oper
)
980 oper
->locals
->outer_scope
= O
->vars
;
982 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
983 /* parse child statements, do not create new variable scope */
984 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
985 while (*C
->I
!= OP_END
)
986 if (!parse_child_operation(C
, O
, oper
, 1))
990 case OP_BLOCK_BEGIN_NEW_SCOPE
:
991 /* parse child statements, create new variable scope */
993 slang_output_ctx o
= *O
;
995 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
996 o
.vars
= oper
->locals
;
997 while (*C
->I
!= OP_END
)
998 if (!parse_child_operation(C
, &o
, oper
, 1))
1004 /* local variable declaration, individual declarators are stored as
1005 * children identifiers
1007 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1009 const unsigned int first_var
= O
->vars
->num_variables
;
1011 /* parse the declaration, note that there can be zero or more
1012 * than one declarators
1014 if (!parse_declaration(C
, O
))
1016 if (first_var
< O
->vars
->num_variables
) {
1017 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
1019 assert(oper
->num_children
== 0);
1020 oper
->num_children
= num_vars
;
1021 oper
->children
= slang_operation_new(num_vars
);
1022 if (oper
->children
== NULL
) {
1023 slang_info_log_memory(C
->L
);
1026 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
1027 slang_operation
*o
= &oper
->children
[i
- first_var
];
1028 slang_variable
*var
= O
->vars
->variables
[i
];
1029 o
->type
= SLANG_OPER_VARIABLE_DECL
;
1030 o
->locals
->outer_scope
= O
->vars
;
1031 o
->a_id
= var
->a_name
;
1034 calculate_var_size(C, O, var);
1037 if (!legal_identifier(o
->a_id
)) {
1038 slang_info_log_error(C
->L
, "illegal variable name '%s'",
1047 /* the __asm statement, parse the mnemonic and all its arguments
1050 oper
->type
= SLANG_OPER_ASM
;
1051 oper
->a_id
= parse_identifier(C
);
1052 if (oper
->a_id
== SLANG_ATOM_NULL
)
1054 while (*C
->I
!= OP_END
) {
1055 if (!parse_child_operation(C
, O
, oper
, 0))
1061 oper
->type
= SLANG_OPER_BREAK
;
1064 oper
->type
= SLANG_OPER_CONTINUE
;
1067 oper
->type
= SLANG_OPER_DISCARD
;
1070 oper
->type
= SLANG_OPER_RETURN
;
1071 if (!parse_child_operation(C
, O
, oper
, 0))
1075 oper
->type
= SLANG_OPER_EXPRESSION
;
1076 if (!parse_child_operation(C
, O
, oper
, 0))
1080 oper
->type
= SLANG_OPER_IF
;
1081 if (!parse_child_operation(C
, O
, oper
, 0))
1083 if (!parse_child_operation(C
, O
, oper
, 1))
1085 if (!parse_child_operation(C
, O
, oper
, 1))
1090 slang_output_ctx o
= *O
;
1092 oper
->type
= SLANG_OPER_WHILE
;
1093 o
.vars
= oper
->locals
;
1094 if (!parse_child_operation(C
, &o
, oper
, 1))
1096 if (!parse_child_operation(C
, &o
, oper
, 1))
1101 oper
->type
= SLANG_OPER_DO
;
1102 if (!parse_child_operation(C
, O
, oper
, 1))
1104 if (!parse_child_operation(C
, O
, oper
, 0))
1109 slang_output_ctx o
= *O
;
1111 oper
->type
= SLANG_OPER_FOR
;
1112 o
.vars
= oper
->locals
;
1113 if (!parse_child_operation(C
, &o
, oper
, 1))
1115 if (!parse_child_operation(C
, &o
, oper
, 1))
1117 if (!parse_child_operation(C
, &o
, oper
, 0))
1119 if (!parse_child_operation(C
, &o
, oper
, 1))
1125 /* set default precision for a type in this scope */
1126 /* ignored at this time */
1127 int prec_qual
= *C
->I
++;
1128 int datatype
= *C
->I
++;
1140 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
1141 slang_operation
** ops
, unsigned int *total_ops
,
1146 op
->children
= slang_operation_new(n
);
1147 if (op
->children
== NULL
) {
1148 slang_info_log_memory(C
->L
);
1151 op
->num_children
= n
;
1153 for (i
= 0; i
< n
; i
++) {
1154 slang_operation_destruct(&op
->children
[i
]);
1155 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
1158 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
1161 *ops
= (slang_operation
*)
1162 _slang_realloc(*ops
,
1163 (*total_ops
+ n
) * sizeof(slang_operation
),
1164 *total_ops
* sizeof(slang_operation
));
1166 slang_info_log_memory(C
->L
);
1173 is_constructor_name(const char *name
, slang_atom a_name
,
1174 slang_struct_scope
* structs
)
1176 if (slang_type_specifier_type_from_string(name
) != SLANG_SPEC_VOID
)
1178 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
1182 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1183 slang_operation
* oper
)
1185 slang_operation
*ops
= NULL
;
1186 unsigned int num_ops
= 0;
1189 while (*C
->I
!= OP_END
) {
1190 slang_operation
*op
;
1191 const unsigned int op_code
= *C
->I
++;
1193 /* allocate default operation, becomes a no-op if not used */
1194 ops
= (slang_operation
*)
1196 num_ops
* sizeof(slang_operation
),
1197 (num_ops
+ 1) * sizeof(slang_operation
));
1199 slang_info_log_memory(C
->L
);
1203 if (!slang_operation_construct(op
)) {
1204 slang_info_log_memory(C
->L
);
1208 op
->locals
->outer_scope
= O
->vars
;
1212 op
->type
= SLANG_OPER_VOID
;
1215 op
->type
= SLANG_OPER_LITERAL_BOOL
;
1216 if (!parse_number(C
, &number
))
1221 op
->literal
[3] = (GLfloat
) number
;
1222 op
->literal_size
= 1;
1225 op
->type
= SLANG_OPER_LITERAL_INT
;
1226 if (!parse_number(C
, &number
))
1231 op
->literal
[3] = (GLfloat
) number
;
1232 op
->literal_size
= 1;
1235 op
->type
= SLANG_OPER_LITERAL_FLOAT
;
1236 if (!parse_float(C
, &op
->literal
[0]))
1240 op
->literal
[3] = op
->literal
[0];
1241 op
->literal_size
= 1;
1243 case OP_PUSH_IDENTIFIER
:
1244 op
->type
= SLANG_OPER_IDENTIFIER
;
1245 op
->a_id
= parse_identifier(C
);
1246 if (op
->a_id
== SLANG_ATOM_NULL
)
1250 op
->type
= SLANG_OPER_SEQUENCE
;
1251 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1255 op
->type
= SLANG_OPER_ASSIGN
;
1256 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1260 op
->type
= SLANG_OPER_ADDASSIGN
;
1261 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1265 op
->type
= SLANG_OPER_SUBASSIGN
;
1266 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1270 op
->type
= SLANG_OPER_MULASSIGN
;
1271 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1275 op
->type
= SLANG_OPER_DIVASSIGN
;
1276 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1279 /*case OP_MODASSIGN: */
1280 /*case OP_LSHASSIGN: */
1281 /*case OP_RSHASSIGN: */
1282 /*case OP_ORASSIGN: */
1283 /*case OP_XORASSIGN: */
1284 /*case OP_ANDASSIGN: */
1286 op
->type
= SLANG_OPER_SELECT
;
1287 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1291 op
->type
= SLANG_OPER_LOGICALOR
;
1292 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1296 op
->type
= SLANG_OPER_LOGICALXOR
;
1297 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1301 op
->type
= SLANG_OPER_LOGICALAND
;
1302 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1306 /*case OP_BITXOR: */
1307 /*case OP_BITAND: */
1309 op
->type
= SLANG_OPER_EQUAL
;
1310 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1314 op
->type
= SLANG_OPER_NOTEQUAL
;
1315 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1319 op
->type
= SLANG_OPER_LESS
;
1320 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1324 op
->type
= SLANG_OPER_GREATER
;
1325 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1329 op
->type
= SLANG_OPER_LESSEQUAL
;
1330 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1333 case OP_GREATEREQUAL
:
1334 op
->type
= SLANG_OPER_GREATEREQUAL
;
1335 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1338 /*case OP_LSHIFT: */
1339 /*case OP_RSHIFT: */
1341 op
->type
= SLANG_OPER_ADD
;
1342 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1346 op
->type
= SLANG_OPER_SUBTRACT
;
1347 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1351 op
->type
= SLANG_OPER_MULTIPLY
;
1352 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1356 op
->type
= SLANG_OPER_DIVIDE
;
1357 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1360 /*case OP_MODULUS: */
1361 case OP_PREINCREMENT
:
1362 op
->type
= SLANG_OPER_PREINCREMENT
;
1363 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1366 case OP_PREDECREMENT
:
1367 op
->type
= SLANG_OPER_PREDECREMENT
;
1368 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1372 op
->type
= SLANG_OPER_PLUS
;
1373 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1377 op
->type
= SLANG_OPER_MINUS
;
1378 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1382 op
->type
= SLANG_OPER_NOT
;
1383 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1386 /*case OP_COMPLEMENT: */
1388 op
->type
= SLANG_OPER_SUBSCRIPT
;
1389 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1393 op
->type
= SLANG_OPER_CALL
;
1394 op
->a_id
= parse_identifier(C
);
1395 if (op
->a_id
== SLANG_ATOM_NULL
)
1397 while (*C
->I
!= OP_END
)
1398 if (!parse_child_operation(C
, O
, op
, 0))
1402 if (!C
->parsing_builtin
1403 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1406 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1407 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1408 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1414 op
->type
= SLANG_OPER_FIELD
;
1415 op
->a_id
= parse_identifier(C
);
1416 if (op
->a_id
== SLANG_ATOM_NULL
)
1418 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1421 case OP_POSTINCREMENT
:
1422 op
->type
= SLANG_OPER_POSTINCREMENT
;
1423 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1426 case OP_POSTDECREMENT
:
1427 op
->type
= SLANG_OPER_POSTDECREMENT
;
1428 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1437 slang_operation_destruct(oper
);
1438 *oper
= *ops
; /* struct copy */
1444 /* parameter qualifier */
1445 #define PARAM_QUALIFIER_IN 0
1446 #define PARAM_QUALIFIER_OUT 1
1447 #define PARAM_QUALIFIER_INOUT 2
1449 /* function parameter array presence */
1450 #define PARAMETER_ARRAY_NOT_PRESENT 0
1451 #define PARAMETER_ARRAY_PRESENT 1
1454 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1455 slang_variable
* param
)
1457 int param_qual
, precision_qual
;
1459 /* parse and validate the parameter's type qualifiers (there can be
1460 * two at most) because not all combinations are valid
1462 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1465 param_qual
= *C
->I
++;
1466 switch (param_qual
) {
1467 case PARAM_QUALIFIER_IN
:
1468 if (param
->type
.qualifier
!= SLANG_QUAL_CONST
1469 && param
->type
.qualifier
!= SLANG_QUAL_NONE
) {
1470 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1474 case PARAM_QUALIFIER_OUT
:
1475 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1476 param
->type
.qualifier
= SLANG_QUAL_OUT
;
1478 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1482 case PARAM_QUALIFIER_INOUT
:
1483 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1484 param
->type
.qualifier
= SLANG_QUAL_INOUT
;
1486 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1494 /* parse precision qualifier (lowp, mediump, highp */
1495 precision_qual
= *C
->I
++;
1496 /* ignored at this time */
1497 (void) precision_qual
;
1499 /* parse parameter's type specifier and name */
1500 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1502 param
->a_name
= parse_identifier(C
);
1503 if (param
->a_name
== SLANG_ATOM_NULL
)
1506 /* if the parameter is an array, parse its size (the size must be
1507 * explicitly defined
1509 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1510 slang_type_specifier p
;
1512 slang_type_specifier_ctr(&p
);
1513 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1514 slang_type_specifier_dtr(&p
);
1517 if (!convert_to_array(C
, param
, &p
)) {
1518 slang_type_specifier_dtr(&p
);
1521 slang_type_specifier_dtr(&p
);
1522 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1526 /* calculate the parameter size */
1527 if (!calculate_var_size(C
, O
, param
))
1530 /* TODO: allocate the local address here? */
1535 #define FUNCTION_ORDINARY 0
1536 #define FUNCTION_CONSTRUCTOR 1
1537 #define FUNCTION_OPERATOR 2
1539 /* function parameter */
1540 #define PARAMETER_NONE 0
1541 #define PARAMETER_NEXT 1
1544 #define OPERATOR_ADDASSIGN 1
1545 #define OPERATOR_SUBASSIGN 2
1546 #define OPERATOR_MULASSIGN 3
1547 #define OPERATOR_DIVASSIGN 4
1548 /*#define OPERATOR_MODASSIGN 5*/
1549 /*#define OPERATOR_LSHASSIGN 6*/
1550 /*#define OPERATOR_RSHASSIGN 7*/
1551 /*#define OPERATOR_ANDASSIGN 8*/
1552 /*#define OPERATOR_XORASSIGN 9*/
1553 /*#define OPERATOR_ORASSIGN 10*/
1554 #define OPERATOR_LOGICALXOR 11
1555 /*#define OPERATOR_BITOR 12*/
1556 /*#define OPERATOR_BITXOR 13*/
1557 /*#define OPERATOR_BITAND 14*/
1558 #define OPERATOR_LESS 15
1559 #define OPERATOR_GREATER 16
1560 #define OPERATOR_LESSEQUAL 17
1561 #define OPERATOR_GREATEREQUAL 18
1562 /*#define OPERATOR_LSHIFT 19*/
1563 /*#define OPERATOR_RSHIFT 20*/
1564 #define OPERATOR_MULTIPLY 21
1565 #define OPERATOR_DIVIDE 22
1566 /*#define OPERATOR_MODULUS 23*/
1567 #define OPERATOR_INCREMENT 24
1568 #define OPERATOR_DECREMENT 25
1569 #define OPERATOR_PLUS 26
1570 #define OPERATOR_MINUS 27
1571 /*#define OPERATOR_COMPLEMENT 28*/
1572 #define OPERATOR_NOT 29
1576 unsigned int o_code
;
1578 } operator_names
[] = {
1579 {OPERATOR_INCREMENT
, "++"},
1580 {OPERATOR_ADDASSIGN
, "+="},
1581 {OPERATOR_PLUS
, "+"},
1582 {OPERATOR_DECREMENT
, "--"},
1583 {OPERATOR_SUBASSIGN
, "-="},
1584 {OPERATOR_MINUS
, "-"},
1585 {OPERATOR_NOT
, "!"},
1586 {OPERATOR_MULASSIGN
, "*="},
1587 {OPERATOR_MULTIPLY
, "*"},
1588 {OPERATOR_DIVASSIGN
, "/="},
1589 {OPERATOR_DIVIDE
, "/"},
1590 {OPERATOR_LESSEQUAL
, "<="},
1591 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1592 /*{ OPERATOR_LSHIFT, "<<" }, */
1593 {OPERATOR_LESS
, "<"},
1594 {OPERATOR_GREATEREQUAL
, ">="},
1595 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1596 /*{ OPERATOR_RSHIFT, ">>" }, */
1597 {OPERATOR_GREATER
, ">"},
1598 /*{ OPERATOR_MODASSIGN, "%=" }, */
1599 /*{ OPERATOR_MODULUS, "%" }, */
1600 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1601 /*{ OPERATOR_BITAND, "&" }, */
1602 /*{ OPERATOR_ORASSIGN, "|=" }, */
1603 /*{ OPERATOR_BITOR, "|" }, */
1604 /*{ OPERATOR_COMPLEMENT, "~" }, */
1605 /*{ OPERATOR_XORASSIGN, "^=" }, */
1606 {OPERATOR_LOGICALXOR
, "^^"},
1607 /*{ OPERATOR_BITXOR, "^" } */
1611 parse_operator_name(slang_parse_ctx
* C
)
1615 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1616 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1618 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1619 if (atom
== SLANG_ATOM_NULL
) {
1620 slang_info_log_memory(C
->L
);
1632 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1633 slang_function
* func
)
1636 /* parse function type and name */
1637 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1642 case FUNCTION_ORDINARY
:
1643 func
->kind
= SLANG_FUNC_ORDINARY
;
1644 func
->header
.a_name
= parse_identifier(C
);
1645 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1648 case FUNCTION_CONSTRUCTOR
:
1649 func
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1650 if (func
->header
.type
.specifier
.type
== SLANG_SPEC_STRUCT
)
1652 func
->header
.a_name
=
1653 slang_atom_pool_atom(C
->atoms
,
1654 slang_type_specifier_type_to_string
1655 (func
->header
.type
.specifier
.type
));
1656 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1657 slang_info_log_memory(C
->L
);
1661 case FUNCTION_OPERATOR
:
1662 func
->kind
= SLANG_FUNC_OPERATOR
;
1663 func
->header
.a_name
= parse_operator_name(C
);
1664 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1671 if (!legal_identifier(func
->header
.a_name
)) {
1672 slang_info_log_error(C
->L
, "illegal function name '%s'",
1673 (char *) func
->header
.a_name
);
1677 /* parse function parameters */
1678 while (*C
->I
++ == PARAMETER_NEXT
) {
1679 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1681 slang_info_log_memory(C
->L
);
1684 if (!parse_parameter_declaration(C
, O
, p
))
1688 /* if the function returns a value, append a hidden __retVal 'out'
1689 * parameter that corresponds to the return value.
1691 if (_slang_function_has_return_value(func
)) {
1692 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1693 slang_atom a_retVal
= slang_atom_pool_atom(C
->atoms
, "__retVal");
1695 p
->a_name
= a_retVal
;
1696 p
->type
= func
->header
.type
;
1697 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1700 /* function formal parameters and local variables share the same
1701 * scope, so save the information about param count in a seperate
1702 * place also link the scope to the global variable scope so when a
1703 * given identifier is not found here, the search process continues
1704 * in the global space
1706 func
->param_count
= func
->parameters
->num_variables
;
1707 func
->parameters
->outer_scope
= O
->vars
;
1713 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1714 slang_function
* func
)
1716 slang_output_ctx o
= *O
;
1718 if (!parse_function_prototype(C
, O
, func
))
1721 /* create function's body operation */
1722 func
->body
= (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1723 if (func
->body
== NULL
) {
1724 slang_info_log_memory(C
->L
);
1727 if (!slang_operation_construct(func
->body
)) {
1728 _slang_free(func
->body
);
1730 slang_info_log_memory(C
->L
);
1734 /* to parse the body the parse context is modified in order to
1735 * capture parsed variables into function's local variable scope
1737 C
->global_scope
= GL_FALSE
;
1738 o
.vars
= func
->parameters
;
1739 if (!parse_statement(C
, &o
, func
->body
))
1742 C
->global_scope
= GL_TRUE
;
1747 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1749 slang_operation op_id
, op_assign
;
1752 /* construct the left side of assignment */
1753 if (!slang_operation_construct(&op_id
))
1755 op_id
.type
= SLANG_OPER_IDENTIFIER
;
1756 op_id
.a_id
= var
->a_name
;
1758 /* put the variable into operation's scope */
1759 op_id
.locals
->variables
=
1760 (slang_variable
**) _slang_alloc(sizeof(slang_variable
*));
1761 if (op_id
.locals
->variables
== NULL
) {
1762 slang_operation_destruct(&op_id
);
1765 op_id
.locals
->num_variables
= 1;
1766 op_id
.locals
->variables
[0] = var
;
1768 /* construct the assignment expression */
1769 if (!slang_operation_construct(&op_assign
)) {
1770 op_id
.locals
->num_variables
= 0;
1771 slang_operation_destruct(&op_id
);
1774 op_assign
.type
= SLANG_OPER_ASSIGN
;
1775 op_assign
.children
=
1776 (slang_operation
*) _slang_alloc(2 * sizeof(slang_operation
));
1777 if (op_assign
.children
== NULL
) {
1778 slang_operation_destruct(&op_assign
);
1779 op_id
.locals
->num_variables
= 0;
1780 slang_operation_destruct(&op_id
);
1783 op_assign
.num_children
= 2;
1784 op_assign
.children
[0] = op_id
;
1785 op_assign
.children
[1] = *var
->initializer
;
1789 /* carefully destroy the operations */
1790 op_assign
.num_children
= 0;
1791 _slang_free(op_assign
.children
);
1792 op_assign
.children
= NULL
;
1793 slang_operation_destruct(&op_assign
);
1794 op_id
.locals
->num_variables
= 0;
1795 slang_operation_destruct(&op_id
);
1803 /* init declarator list */
1804 #define DECLARATOR_NONE 0
1805 #define DECLARATOR_NEXT 1
1807 /* variable declaration */
1808 #define VARIABLE_NONE 0
1809 #define VARIABLE_IDENTIFIER 1
1810 #define VARIABLE_INITIALIZER 2
1811 #define VARIABLE_ARRAY_EXPLICIT 3
1812 #define VARIABLE_ARRAY_UNKNOWN 4
1816 * Parse the initializer for a variable declaration.
1819 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1820 const slang_fully_specified_type
* type
)
1822 slang_variable
*var
;
1825 /* empty init declatator (without name, e.g. "float ;") */
1826 if (*C
->I
++ == VARIABLE_NONE
)
1829 a_name
= parse_identifier(C
);
1831 /* check if name is already in this scope */
1832 if (_slang_locate_variable(O
->vars
, a_name
, GL_FALSE
)) {
1833 slang_info_log_error(C
->L
,
1834 "declaration of '%s' conflicts with previous declaration",
1839 /* make room for the new variable and initialize it */
1840 var
= slang_variable_scope_grow(O
->vars
);
1842 slang_info_log_memory(C
->L
);
1846 /* copy the declarator type qualifier/etc info, parse the identifier */
1847 var
->type
.qualifier
= type
->qualifier
;
1848 var
->type
.centroid
= type
->centroid
;
1849 var
->type
.precision
= type
->precision
;
1850 var
->type
.variant
= type
->variant
;
1851 var
->a_name
= a_name
;
1852 if (var
->a_name
== SLANG_ATOM_NULL
)
1857 /* simple variable declarator - just copy the specifier */
1858 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1861 case VARIABLE_INITIALIZER
:
1862 /* initialized variable - copy the specifier and parse the expression */
1863 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1866 (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1867 if (var
->initializer
== NULL
) {
1868 slang_info_log_memory(C
->L
);
1871 if (!slang_operation_construct(var
->initializer
)) {
1872 _slang_free(var
->initializer
);
1873 var
->initializer
= NULL
;
1874 slang_info_log_memory(C
->L
);
1877 if (!parse_expression(C
, O
, var
->initializer
))
1880 case VARIABLE_ARRAY_UNKNOWN
:
1881 /* unsized array - mark it as array and copy the specifier to
1884 if (!convert_to_array(C
, var
, &type
->specifier
))
1887 case VARIABLE_ARRAY_EXPLICIT
:
1888 if (!convert_to_array(C
, var
, &type
->specifier
))
1890 if (!parse_array_len(C
, O
, &var
->array_len
))
1897 if (type
->array_len
>= 0) {
1898 /* The type was something like "float[4]" */
1899 if (var
->array_len
!= 0) {
1900 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
1903 convert_to_array(C
, var
, &type
->specifier
);
1904 var
->array_len
= type
->array_len
;
1907 /* allocate global address space for a variable with a known size */
1909 && !(var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
1910 && var
->array_len
== 0)) {
1911 if (!calculate_var_size(C
, O
, var
))
1913 var
->address
= slang_var_pool_alloc(O
->global_pool
, var
->size
);
1916 /* emit code for global var decl */
1917 if (C
->global_scope
) {
1918 slang_assemble_ctx A
;
1920 A
.space
.funcs
= O
->funs
;
1921 A
.space
.structs
= O
->structs
;
1922 A
.space
.vars
= O
->vars
;
1923 A
.program
= O
->program
;
1924 A
.vartable
= O
->vartable
;
1926 A
.curFuncEndLabel
= NULL
;
1927 if (!_slang_codegen_global_variable(&A
, var
, C
->type
))
1931 /* initialize global variable */
1932 if (C
->global_scope
) {
1933 if (var
->initializer
!= NULL
) {
1934 slang_assemble_ctx A
;
1937 A
.space
.funcs
= O
->funs
;
1938 A
.space
.structs
= O
->structs
;
1939 A
.space
.vars
= O
->vars
;
1940 if (!initialize_global(&A
, var
))
1948 * Parse a list of variable declarations. Each variable may have an
1952 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1954 slang_fully_specified_type type
;
1956 /* parse the fully specified type, common to all declarators */
1957 if (!slang_fully_specified_type_construct(&type
))
1959 if (!parse_fully_specified_type(C
, O
, &type
)) {
1960 slang_fully_specified_type_destruct(&type
);
1964 /* parse declarators, pass-in the parsed type */
1966 if (!parse_init_declarator(C
, O
, &type
)) {
1967 slang_fully_specified_type_destruct(&type
);
1971 while (*C
->I
++ == DECLARATOR_NEXT
);
1973 slang_fully_specified_type_destruct(&type
);
1979 * Parse a function definition or declaration.
1980 * \param C parsing context
1981 * \param O output context
1982 * \param definition if non-zero expect a definition, else a declaration
1983 * \param parsed_func_ret returns the parsed function
1984 * \return GL_TRUE if success, GL_FALSE if failure
1987 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
1988 slang_function
** parsed_func_ret
)
1990 slang_function parsed_func
, *found_func
;
1992 /* parse function definition/declaration */
1993 if (!slang_function_construct(&parsed_func
))
1996 if (!parse_function_definition(C
, O
, &parsed_func
)) {
1997 slang_function_destruct(&parsed_func
);
2002 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
2003 slang_function_destruct(&parsed_func
);
2008 /* find a function with a prototype matching the parsed one - only
2009 * the current scope is being searched to allow built-in function
2012 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
2013 if (found_func
== NULL
) {
2014 /* New function, add it to the function list */
2015 O
->funs
->functions
=
2016 (slang_function
*) _slang_realloc(O
->funs
->functions
,
2017 O
->funs
->num_functions
2018 * sizeof(slang_function
),
2019 (O
->funs
->num_functions
+ 1)
2020 * sizeof(slang_function
));
2021 if (O
->funs
->functions
== NULL
) {
2022 slang_info_log_memory(C
->L
);
2023 slang_function_destruct(&parsed_func
);
2026 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
2027 O
->funs
->num_functions
++;
2029 /* return the newly parsed function */
2030 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
2033 /* previously defined or declared */
2034 /* TODO: check function return type qualifiers and specifiers */
2036 if (found_func
->body
!= NULL
) {
2037 slang_info_log_error(C
->L
, "%s: function already has a body.",
2038 slang_atom_pool_id(C
->atoms
,
2041 slang_function_destruct(&parsed_func
);
2045 /* destroy the existing function declaration and replace it
2046 * with the new one, remember to save the fixup table
2048 parsed_func
.fixups
= found_func
->fixups
;
2049 slang_fixup_table_init(&found_func
->fixups
);
2050 slang_function_destruct(found_func
);
2051 *found_func
= parsed_func
;
2054 /* another declaration of the same function prototype - ignore it */
2055 slang_function_destruct(&parsed_func
);
2058 /* return the found function */
2059 *parsed_func_ret
= found_func
;
2066 #define DECLARATION_FUNCTION_PROTOTYPE 1
2067 #define DECLARATION_INIT_DECLARATOR_LIST 2
2070 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2073 case DECLARATION_INIT_DECLARATOR_LIST
:
2074 if (!parse_init_declarator_list(C
, O
))
2077 case DECLARATION_FUNCTION_PROTOTYPE
:
2079 slang_function
*dummy_func
;
2081 if (!parse_function(C
, O
, 0, &dummy_func
))
2092 parse_default_precision(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2094 int precision
, type
;
2096 if (!O
->allow_precision
) {
2097 slang_info_log_error(C
->L
, "syntax error at \"precision\"");
2101 precision
= *C
->I
++;
2102 switch (precision
) {
2104 case PRECISION_MEDIUM
:
2105 case PRECISION_HIGH
:
2109 _mesa_problem(NULL
, "unexpected precision %d at %s:%d\n",
2110 precision
, __FILE__
, __LINE__
);
2116 case TYPE_SPECIFIER_FLOAT
:
2117 case TYPE_SPECIFIER_INT
:
2118 case TYPE_SPECIFIER_SAMPLER1D
:
2119 case TYPE_SPECIFIER_SAMPLER2D
:
2120 case TYPE_SPECIFIER_SAMPLER3D
:
2121 case TYPE_SPECIFIER_SAMPLERCUBE
:
2122 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
2123 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
2124 case TYPE_SPECIFIER_SAMPLER2DRECT
:
2125 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
2129 _mesa_problem(NULL
, "unexpected type %d at %s:%d\n",
2130 type
, __FILE__
, __LINE__
);
2134 assert(type
< TYPE_SPECIFIER_COUNT
);
2135 O
->default_precision
[type
] = precision
;
2142 * Initialize the default precision for all types.
2143 * XXX this info isn't used yet.
2146 init_default_precision(slang_output_ctx
*O
, slang_unit_type type
)
2149 for (i
= 0; i
< TYPE_SPECIFIER_COUNT
; i
++) {
2150 #if FEATURE_es2_glsl
2151 O
->default_precision
[i
] = PRECISION_LOW
;
2153 O
->default_precision
[i
] = PRECISION_HIGH
;
2157 if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2158 O
->default_precision
[TYPE_SPECIFIER_FLOAT
] = PRECISION_HIGH
;
2159 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_HIGH
;
2162 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_MEDIUM
;
2168 parse_invariant(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2170 if (O
->allow_invariant
) {
2171 slang_atom
*a
= parse_identifier(C
);
2172 /* XXX not doing anything with this var yet */
2173 /*printf("ID: %s\n", (char*) a);*/
2177 slang_info_log_error(C
->L
, "syntax error at \"invariant\"");
2183 /* external declaration or default precision specifier */
2184 #define EXTERNAL_NULL 0
2185 #define EXTERNAL_FUNCTION_DEFINITION 1
2186 #define EXTERNAL_DECLARATION 2
2187 #define DEFAULT_PRECISION 3
2188 #define INVARIANT_STMT 4
2192 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
,
2193 struct gl_shader
*shader
)
2195 GET_CURRENT_CONTEXT(ctx
);
2199 slang_function
*mainFunc
= NULL
;
2201 if (unit
->type
== SLANG_UNIT_FRAGMENT_BUILTIN
||
2202 unit
->type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2203 maxRegs
= ctx
->Const
.FragmentProgram
.MaxTemps
;
2206 assert(unit
->type
== SLANG_UNIT_VERTEX_BUILTIN
||
2207 unit
->type
== SLANG_UNIT_VERTEX_SHADER
);
2208 maxRegs
= ctx
->Const
.VertexProgram
.MaxTemps
;
2211 /* setup output context */
2212 o
.funs
= &unit
->funs
;
2213 o
.structs
= &unit
->structs
;
2214 o
.vars
= &unit
->vars
;
2215 o
.global_pool
= &unit
->object
->varpool
;
2216 o
.program
= shader
? shader
->Program
: NULL
;
2217 o
.vartable
= _slang_new_var_table(maxRegs
);
2218 _slang_push_var_table(o
.vartable
);
2220 /* allow 'invariant' keyword? */
2221 #if FEATURE_es2_glsl
2222 o
.allow_invariant
= GL_TRUE
;
2224 o
.allow_invariant
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2227 /* allow 'centroid' keyword? */
2228 o
.allow_centroid
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2230 /* allow 'lowp/mediump/highp' keywords? */
2231 #if FEATURE_es2_glsl
2232 o
.allow_precision
= GL_TRUE
;
2234 o
.allow_precision
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2236 init_default_precision(&o
, unit
->type
);
2238 /* allow 'float[]' keyword? */
2239 o
.allow_array_types
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2241 /* parse individual functions and declarations */
2242 while (*C
->I
!= EXTERNAL_NULL
) {
2244 case EXTERNAL_FUNCTION_DEFINITION
:
2246 slang_function
*func
;
2247 success
= parse_function(C
, &o
, 1, &func
);
2249 _mesa_strcmp((char *) func
->header
.a_name
, "main") == 0) {
2255 case EXTERNAL_DECLARATION
:
2256 success
= parse_declaration(C
, &o
);
2258 case DEFAULT_PRECISION
:
2259 success
= parse_default_precision(C
, &o
);
2261 case INVARIANT_STMT
:
2262 success
= parse_invariant(C
, &o
);
2269 /* xxx free codegen */
2270 _slang_pop_var_table(o
.vartable
);
2277 /* assemble (generate code) for main() */
2278 slang_assemble_ctx A
;
2281 A
.space
.funcs
= o
.funs
;
2282 A
.space
.structs
= o
.structs
;
2283 A
.space
.vars
= o
.vars
;
2284 A
.program
= o
.program
;
2285 A
.vartable
= o
.vartable
;
2288 /* main() takes no parameters */
2289 if (mainFunc
->param_count
> 0) {
2290 slang_info_log_error(A
.log
, "main() takes no arguments");
2294 _slang_codegen_function(&A
, mainFunc
);
2296 shader
->Main
= GL_TRUE
; /* this shader defines main() */
2299 _slang_pop_var_table(o
.vartable
);
2300 _slang_delete_var_table(o
.vartable
);
2306 compile_binary(const byte
* prod
, slang_code_unit
* unit
,
2308 slang_unit_type type
, slang_info_log
* infolog
,
2309 slang_code_unit
* builtin
, slang_code_unit
* downlink
,
2310 struct gl_shader
*shader
)
2316 /* setup parse context */
2319 C
.parsing_builtin
= (builtin
== NULL
);
2320 C
.global_scope
= GL_TRUE
;
2321 C
.atoms
= &unit
->object
->atompool
;
2323 C
.version
= version
;
2325 if (!check_revision(&C
))
2328 if (downlink
!= NULL
) {
2329 unit
->vars
.outer_scope
= &downlink
->vars
;
2330 unit
->funs
.outer_scope
= &downlink
->funs
;
2331 unit
->structs
.outer_scope
= &downlink
->structs
;
2334 /* parse translation unit */
2335 return parse_code_unit(&C
, unit
, shader
);
2339 compile_with_grammar(grammar id
, const char *source
, slang_code_unit
* unit
,
2340 slang_unit_type type
, slang_info_log
* infolog
,
2341 slang_code_unit
* builtin
,
2342 struct gl_shader
*shader
)
2345 GLuint size
, start
, version
;
2346 slang_string preprocessed
;
2349 #if FEATURE_ARB_shading_language_120
2351 #elif FEATURE_es2_glsl
2357 /* First retrieve the version number. */
2358 if (!_slang_preprocess_version(source
, &version
, &start
, infolog
))
2361 if (version
> maxVersion
) {
2362 slang_info_log_error(infolog
,
2363 "language version %.2f is not supported.",
2368 /* Now preprocess the source string. */
2369 slang_string_init(&preprocessed
);
2370 if (!_slang_preprocess_directives(&preprocessed
, &source
[start
], infolog
)) {
2371 slang_string_free(&preprocessed
);
2372 slang_info_log_error(infolog
, "failed to preprocess the source.");
2376 /* Finally check the syntax and generate its binary representation. */
2377 if (!grammar_fast_check(id
,
2378 (const byte
*) (slang_string_cstr(&preprocessed
)),
2379 &prod
, &size
, 65536)) {
2383 slang_string_free(&preprocessed
);
2384 grammar_get_last_error((byte
*) (buf
), sizeof(buf
), &pos
);
2385 slang_info_log_error(infolog
, buf
);
2386 /* syntax error (possibly in library code) */
2391 s
= (char *) _mesa_find_line_column((const GLubyte
*) source
,
2392 (const GLubyte
*) source
+ pos
,
2394 printf("Error on line %d, col %d: %s\n", line
, col
, s
);
2399 slang_string_free(&preprocessed
);
2401 /* Syntax is okay - translate it to internal representation. */
2402 if (!compile_binary(prod
, unit
, version
, type
, infolog
, builtin
,
2403 &builtin
[SLANG_BUILTIN_TOTAL
- 1],
2405 grammar_alloc_free(prod
);
2408 grammar_alloc_free(prod
);
2412 LONGSTRING
static const char *slang_shader_syn
=
2413 #include "library/slang_shader_syn.h"
2416 static const byte slang_core_gc
[] = {
2417 #include "library/slang_core_gc.h"
2420 static const byte slang_120_core_gc
[] = {
2421 #include "library/slang_120_core_gc.h"
2424 static const byte slang_120_fragment_gc
[] = {
2425 #include "library/slang_builtin_120_fragment_gc.h"
2428 static const byte slang_common_builtin_gc
[] = {
2429 #include "library/slang_common_builtin_gc.h"
2432 static const byte slang_fragment_builtin_gc
[] = {
2433 #include "library/slang_fragment_builtin_gc.h"
2436 static const byte slang_vertex_builtin_gc
[] = {
2437 #include "library/slang_vertex_builtin_gc.h"
2441 compile_object(grammar
* id
, const char *source
, slang_code_object
* object
,
2442 slang_unit_type type
, slang_info_log
* infolog
,
2443 struct gl_shader
*shader
)
2445 slang_code_unit
*builtins
= NULL
;
2446 GLuint base_version
= 110;
2448 /* load GLSL grammar */
2449 *id
= grammar_load_from_text((const byte
*) (slang_shader_syn
));
2454 grammar_get_last_error(buf
, 1024, &pos
);
2455 slang_info_log_error(infolog
, (const char *) (buf
));
2459 /* set shader type - the syntax is slightly different for different shaders */
2460 if (type
== SLANG_UNIT_FRAGMENT_SHADER
2461 || type
== SLANG_UNIT_FRAGMENT_BUILTIN
)
2462 grammar_set_reg8(*id
, (const byte
*) "shader_type", 1);
2464 grammar_set_reg8(*id
, (const byte
*) "shader_type", 2);
2466 /* enable language extensions */
2467 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2469 /* if parsing user-specified shader, load built-in library */
2470 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_VERTEX_SHADER
) {
2471 /* compile core functionality first */
2472 if (!compile_binary(slang_core_gc
,
2473 &object
->builtin
[SLANG_BUILTIN_CORE
],
2475 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2479 #if FEATURE_ARB_shading_language_120
2480 if (!compile_binary(slang_120_core_gc
,
2481 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2483 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2484 NULL
, &object
->builtin
[SLANG_BUILTIN_CORE
], NULL
))
2488 /* compile common functions and variables, link to core */
2489 if (!compile_binary(slang_common_builtin_gc
,
2490 &object
->builtin
[SLANG_BUILTIN_COMMON
],
2491 #if FEATURE_ARB_shading_language_120
2496 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2497 #if FEATURE_ARB_shading_language_120
2498 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2500 &object
->builtin
[SLANG_BUILTIN_CORE
],
2505 /* compile target-specific functions and variables, link to common */
2506 if (type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2507 if (!compile_binary(slang_fragment_builtin_gc
,
2508 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2510 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2511 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2513 #if FEATURE_ARB_shading_language_120
2514 if (!compile_binary(slang_120_fragment_gc
,
2515 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2517 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2518 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2522 else if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2523 if (!compile_binary(slang_vertex_builtin_gc
,
2524 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2526 SLANG_UNIT_VERTEX_BUILTIN
, infolog
, NULL
,
2527 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2531 /* disable language extensions */
2532 #if NEW_SLANG /* allow-built-ins */
2533 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2535 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 0);
2537 builtins
= object
->builtin
;
2540 /* compile the actual shader - pass-in built-in library for external shader */
2541 return compile_with_grammar(*id
, source
, &object
->unit
, type
, infolog
,
2547 compile_shader(GLcontext
*ctx
, slang_code_object
* object
,
2548 slang_unit_type type
, slang_info_log
* infolog
,
2549 struct gl_shader
*shader
)
2554 #if 0 /* for debug */
2555 _mesa_printf("********* COMPILE SHADER ***********\n");
2556 _mesa_printf("%s\n", shader
->Source
);
2557 _mesa_printf("************************************\n");
2560 assert(shader
->Program
);
2562 _slang_code_object_dtr(object
);
2563 _slang_code_object_ctr(object
);
2565 success
= compile_object(&id
, shader
->Source
, object
, type
, infolog
, shader
);
2567 grammar_destroy(id
);
2577 _slang_compile(GLcontext
*ctx
, struct gl_shader
*shader
)
2580 slang_info_log info_log
;
2581 slang_code_object obj
;
2582 slang_unit_type type
;
2584 if (shader
->Type
== GL_VERTEX_SHADER
) {
2585 type
= SLANG_UNIT_VERTEX_SHADER
;
2588 assert(shader
->Type
== GL_FRAGMENT_SHADER
);
2589 type
= SLANG_UNIT_FRAGMENT_SHADER
;
2592 if (!shader
->Source
)
2595 ctx
->Shader
.MemPool
= _slang_new_mempool(1024*1024);
2597 shader
->Main
= GL_FALSE
;
2599 if (!shader
->Program
) {
2601 if (shader
->Type
== GL_VERTEX_SHADER
)
2602 progTarget
= GL_VERTEX_PROGRAM_ARB
;
2604 progTarget
= GL_FRAGMENT_PROGRAM_ARB
;
2605 shader
->Program
= ctx
->Driver
.NewProgram(ctx
, progTarget
, 1);
2606 shader
->Program
->Parameters
= _mesa_new_parameter_list();
2607 shader
->Program
->Varying
= _mesa_new_parameter_list();
2608 shader
->Program
->Attributes
= _mesa_new_parameter_list();
2611 slang_info_log_construct(&info_log
);
2612 _slang_code_object_ctr(&obj
);
2614 success
= compile_shader(ctx
, &obj
, type
, &info_log
, shader
);
2616 /* free shader's prev info log */
2617 if (shader
->InfoLog
) {
2618 _mesa_free(shader
->InfoLog
);
2619 shader
->InfoLog
= NULL
;
2622 if (info_log
.text
) {
2623 /* copy info-log string to shader object */
2624 shader
->InfoLog
= _mesa_strdup(info_log
.text
);
2627 if (info_log
.error_flag
) {
2631 slang_info_log_destruct(&info_log
);
2632 _slang_code_object_dtr(&obj
);
2634 _slang_delete_mempool((slang_mempool
*) ctx
->Shader
.MemPool
);
2635 ctx
->Shader
.MemPool
= NULL
;
2637 /* remove any reads of output registers */
2639 printf("Pre-remove output reads:\n");
2640 _mesa_print_program(shader
->Program
);
2642 _mesa_remove_output_reads(shader
->Program
, PROGRAM_OUTPUT
);
2643 if (shader
->Type
== GL_VERTEX_SHADER
) {
2644 /* and remove writes to varying vars in vertex programs */
2645 _mesa_remove_output_reads(shader
->Program
, PROGRAM_VARYING
);
2648 printf("Post-remove output reads:\n");
2649 _mesa_print_program(shader
->Program
);