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
;
168 /* _slang_compile() */
171 parse_identifier_str(slang_parse_ctx
* C
, char **id
)
174 C
->I
+= _mesa_strlen(*id
) + 1;
178 parse_identifier(slang_parse_ctx
* C
)
182 id
= (const char *) C
->I
;
183 C
->I
+= _mesa_strlen(id
) + 1;
184 return slang_atom_pool_atom(C
->atoms
, id
);
188 parse_number(slang_parse_ctx
* C
, int *number
)
190 const int radix
= (int) (*C
->I
++);
192 while (*C
->I
!= '\0') {
194 if (*C
->I
>= '0' && *C
->I
<= '9')
195 digit
= (int) (*C
->I
- '0');
196 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
197 digit
= (int) (*C
->I
- 'A') + 10;
199 digit
= (int) (*C
->I
- 'a') + 10;
200 *number
= *number
* radix
+ digit
;
205 slang_info_log_warning(C
->L
, "%d: literal integer overflow.", *number
);
210 parse_float(slang_parse_ctx
* C
, float *number
)
212 char *integral
= NULL
;
213 char *fractional
= NULL
;
214 char *exponent
= NULL
;
217 parse_identifier_str(C
, &integral
);
218 parse_identifier_str(C
, &fractional
);
219 parse_identifier_str(C
, &exponent
);
221 whole
= (char *) _slang_alloc((_mesa_strlen(integral
) +
222 _mesa_strlen(fractional
) +
223 _mesa_strlen(exponent
) + 3) * sizeof(char));
225 slang_info_log_memory(C
->L
);
229 slang_string_copy(whole
, integral
);
230 slang_string_concat(whole
, ".");
231 slang_string_concat(whole
, fractional
);
232 slang_string_concat(whole
, "E");
233 slang_string_concat(whole
, exponent
);
235 *number
= (float) (_mesa_strtod(whole
, (char **) NULL
));
242 /* revision number - increment after each change affecting emitted output */
246 check_revision(slang_parse_ctx
* C
)
248 if (*C
->I
!= REVISION
) {
249 slang_info_log_error(C
->L
, "Internal compiler error.");
256 static int parse_statement(slang_parse_ctx
*, slang_output_ctx
*,
258 static int parse_expression(slang_parse_ctx
*, slang_output_ctx
*,
260 static int parse_type_specifier(slang_parse_ctx
*, slang_output_ctx
*,
261 slang_type_specifier
*);
264 parse_array_len(slang_parse_ctx
* C
, slang_output_ctx
* O
, GLuint
* len
)
266 slang_operation array_size
;
267 slang_name_space space
;
270 if (!slang_operation_construct(&array_size
))
272 if (!parse_expression(C
, O
, &array_size
)) {
273 slang_operation_destruct(&array_size
);
277 space
.funcs
= O
->funs
;
278 space
.structs
= O
->structs
;
279 space
.vars
= O
->vars
;
281 /* evaluate compile-time expression which is array size */
282 _slang_simplify(&array_size
, &space
, C
->atoms
);
284 if (array_size
.type
== SLANG_OPER_LITERAL_INT
) {
286 *len
= (GLint
) array_size
.literal
[0];
287 } else if (array_size
.type
== SLANG_OPER_IDENTIFIER
) {
288 slang_variable
*var
= _slang_locate_variable(array_size
.locals
, array_size
.a_id
, GL_TRUE
);
290 slang_info_log_error(C
->L
, "undefined variable '%s'",
291 (char *) array_size
.a_id
);
293 } else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&&
294 var
->type
.specifier
.type
== SLANG_SPEC_INT
) {
295 if (var
->initializer
&&
296 var
->initializer
->type
== SLANG_OPER_LITERAL_INT
) {
297 *len
= (GLint
) var
->initializer
->literal
[0];
300 slang_info_log_error(C
->L
, "unable to parse array size declaration");
304 slang_info_log_error(C
->L
, "unable to parse array size declaration");
311 slang_operation_destruct(&array_size
);
316 calculate_var_size(slang_parse_ctx
* C
, slang_output_ctx
* O
,
317 slang_variable
* var
)
319 slang_storage_aggregate agg
;
321 if (!slang_storage_aggregate_construct(&agg
))
323 if (!_slang_aggregate_variable(&agg
, &var
->type
.specifier
, var
->array_len
,
324 O
->funs
, O
->structs
, O
->vars
, C
->atoms
)) {
325 slang_storage_aggregate_destruct(&agg
);
328 var
->size
= _slang_sizeof_aggregate(&agg
);
329 slang_storage_aggregate_destruct(&agg
);
334 convert_to_array(slang_parse_ctx
* C
, slang_variable
* var
,
335 const slang_type_specifier
* sp
)
337 /* sized array - mark it as array, copy the specifier to the array element and
338 * parse the expression */
339 var
->type
.specifier
.type
= SLANG_SPEC_ARRAY
;
340 var
->type
.specifier
._array
= (slang_type_specifier
*)
341 _slang_alloc(sizeof(slang_type_specifier
));
342 if (var
->type
.specifier
._array
== NULL
) {
343 slang_info_log_memory(C
->L
);
346 slang_type_specifier_ctr(var
->type
.specifier
._array
);
347 return slang_type_specifier_copy(var
->type
.specifier
._array
, sp
);
350 /* structure field */
353 #define FIELD_ARRAY 2
356 parse_struct_field_var(slang_parse_ctx
* C
, slang_output_ctx
* O
,
357 slang_variable
* var
, slang_atom a_name
,
358 const slang_type_specifier
* sp
)
360 var
->a_name
= a_name
;
361 if (var
->a_name
== SLANG_ATOM_NULL
)
366 if (!slang_type_specifier_copy(&var
->type
.specifier
, sp
))
370 if (!convert_to_array(C
, var
, sp
))
372 if (!parse_array_len(C
, O
, &var
->array_len
))
379 return calculate_var_size(C
, O
, var
);
383 parse_struct_field(slang_parse_ctx
* C
, slang_output_ctx
* O
,
384 slang_struct
* st
, slang_type_specifier
* sp
)
386 slang_output_ctx o
= *O
;
388 o
.structs
= st
->structs
;
389 if (!parse_type_specifier(C
, &o
, sp
))
394 slang_variable
*var
= slang_variable_scope_grow(st
->fields
);
396 slang_info_log_memory(C
->L
);
399 a_name
= parse_identifier(C
);
400 if (_slang_locate_variable(st
->fields
, a_name
, GL_FALSE
)) {
401 slang_info_log_error(C
->L
, "duplicate field '%s'", (char *) a_name
);
405 if (!parse_struct_field_var(C
, &o
, var
, a_name
, sp
))
408 while (*C
->I
++ != FIELD_NONE
);
414 parse_struct(slang_parse_ctx
* C
, slang_output_ctx
* O
, slang_struct
** st
)
419 /* parse struct name (if any) and make sure it is unique in current scope */
420 a_name
= parse_identifier(C
);
421 if (a_name
== SLANG_ATOM_NULL
)
424 name
= slang_atom_pool_id(C
->atoms
, a_name
);
426 && slang_struct_scope_find(O
->structs
, a_name
, 0) != NULL
) {
427 slang_info_log_error(C
->L
, "%s: duplicate type name.", name
);
431 /* set-up a new struct */
432 *st
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
434 slang_info_log_memory(C
->L
);
437 if (!slang_struct_construct(*st
)) {
440 slang_info_log_memory(C
->L
);
443 (**st
).a_name
= a_name
;
444 (**st
).structs
->outer_scope
= O
->structs
;
446 /* parse individual struct fields */
448 slang_type_specifier sp
;
450 slang_type_specifier_ctr(&sp
);
451 if (!parse_struct_field(C
, O
, *st
, &sp
)) {
452 slang_type_specifier_dtr(&sp
);
455 slang_type_specifier_dtr(&sp
);
457 while (*C
->I
++ != FIELD_NONE
);
459 /* if named struct, copy it to current scope */
460 if (name
[0] != '\0') {
463 O
->structs
->structs
=
464 (slang_struct
*) _slang_realloc(O
->structs
->structs
,
465 O
->structs
->num_structs
466 * sizeof(slang_struct
),
467 (O
->structs
->num_structs
+ 1)
468 * sizeof(slang_struct
));
469 if (O
->structs
->structs
== NULL
) {
470 slang_info_log_memory(C
->L
);
473 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
474 if (!slang_struct_construct(s
))
476 O
->structs
->num_structs
++;
477 if (!slang_struct_copy(s
, *st
))
485 /* invariant qualifer */
486 #define TYPE_VARIANT 90
487 #define TYPE_INVARIANT 91
490 parse_type_variant(slang_parse_ctx
* C
, slang_type_variant
*variant
)
492 GLuint invariant
= *C
->I
++;
495 *variant
= SLANG_VARIANT
;
498 *variant
= SLANG_INVARIANT
;
506 /* centroid qualifer */
507 #define TYPE_CENTER 95
508 #define TYPE_CENTROID 96
511 parse_type_centroid(slang_parse_ctx
* C
, slang_type_centroid
*centroid
)
516 *centroid
= SLANG_CENTER
;
519 *centroid
= SLANG_CENTROID
;
528 #define TYPE_QUALIFIER_NONE 0
529 #define TYPE_QUALIFIER_CONST 1
530 #define TYPE_QUALIFIER_ATTRIBUTE 2
531 #define TYPE_QUALIFIER_VARYING 3
532 #define TYPE_QUALIFIER_UNIFORM 4
533 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
534 #define TYPE_QUALIFIER_FIXEDINPUT 6
537 parse_type_qualifier(slang_parse_ctx
* C
, slang_type_qualifier
* qual
)
539 GLuint qualifier
= *C
->I
++;
541 case TYPE_QUALIFIER_NONE
:
542 *qual
= SLANG_QUAL_NONE
;
544 case TYPE_QUALIFIER_CONST
:
545 *qual
= SLANG_QUAL_CONST
;
547 case TYPE_QUALIFIER_ATTRIBUTE
:
548 *qual
= SLANG_QUAL_ATTRIBUTE
;
550 case TYPE_QUALIFIER_VARYING
:
551 *qual
= SLANG_QUAL_VARYING
;
553 case TYPE_QUALIFIER_UNIFORM
:
554 *qual
= SLANG_QUAL_UNIFORM
;
556 case TYPE_QUALIFIER_FIXEDOUTPUT
:
557 *qual
= SLANG_QUAL_FIXEDOUTPUT
;
559 case TYPE_QUALIFIER_FIXEDINPUT
:
560 *qual
= SLANG_QUAL_FIXEDINPUT
;
569 #define TYPE_SPECIFIER_VOID 0
570 #define TYPE_SPECIFIER_BOOL 1
571 #define TYPE_SPECIFIER_BVEC2 2
572 #define TYPE_SPECIFIER_BVEC3 3
573 #define TYPE_SPECIFIER_BVEC4 4
574 #define TYPE_SPECIFIER_INT 5
575 #define TYPE_SPECIFIER_IVEC2 6
576 #define TYPE_SPECIFIER_IVEC3 7
577 #define TYPE_SPECIFIER_IVEC4 8
578 #define TYPE_SPECIFIER_FLOAT 9
579 #define TYPE_SPECIFIER_VEC2 10
580 #define TYPE_SPECIFIER_VEC3 11
581 #define TYPE_SPECIFIER_VEC4 12
582 #define TYPE_SPECIFIER_MAT2 13
583 #define TYPE_SPECIFIER_MAT3 14
584 #define TYPE_SPECIFIER_MAT4 15
585 #define TYPE_SPECIFIER_SAMPLER1D 16
586 #define TYPE_SPECIFIER_SAMPLER2D 17
587 #define TYPE_SPECIFIER_SAMPLER3D 18
588 #define TYPE_SPECIFIER_SAMPLERCUBE 19
589 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
590 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
591 #define TYPE_SPECIFIER_SAMPLER2DRECT 22
592 #define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
593 #define TYPE_SPECIFIER_STRUCT 24
594 #define TYPE_SPECIFIER_TYPENAME 25
595 #define TYPE_SPECIFIER_MAT23 26
596 #define TYPE_SPECIFIER_MAT32 27
597 #define TYPE_SPECIFIER_MAT24 28
598 #define TYPE_SPECIFIER_MAT42 29
599 #define TYPE_SPECIFIER_MAT34 30
600 #define TYPE_SPECIFIER_MAT43 31
601 #define TYPE_SPECIFIER_COUNT 32
604 parse_type_specifier(slang_parse_ctx
* C
, slang_output_ctx
* O
,
605 slang_type_specifier
* spec
)
608 case TYPE_SPECIFIER_VOID
:
609 spec
->type
= SLANG_SPEC_VOID
;
611 case TYPE_SPECIFIER_BOOL
:
612 spec
->type
= SLANG_SPEC_BOOL
;
614 case TYPE_SPECIFIER_BVEC2
:
615 spec
->type
= SLANG_SPEC_BVEC2
;
617 case TYPE_SPECIFIER_BVEC3
:
618 spec
->type
= SLANG_SPEC_BVEC3
;
620 case TYPE_SPECIFIER_BVEC4
:
621 spec
->type
= SLANG_SPEC_BVEC4
;
623 case TYPE_SPECIFIER_INT
:
624 spec
->type
= SLANG_SPEC_INT
;
626 case TYPE_SPECIFIER_IVEC2
:
627 spec
->type
= SLANG_SPEC_IVEC2
;
629 case TYPE_SPECIFIER_IVEC3
:
630 spec
->type
= SLANG_SPEC_IVEC3
;
632 case TYPE_SPECIFIER_IVEC4
:
633 spec
->type
= SLANG_SPEC_IVEC4
;
635 case TYPE_SPECIFIER_FLOAT
:
636 spec
->type
= SLANG_SPEC_FLOAT
;
638 case TYPE_SPECIFIER_VEC2
:
639 spec
->type
= SLANG_SPEC_VEC2
;
641 case TYPE_SPECIFIER_VEC3
:
642 spec
->type
= SLANG_SPEC_VEC3
;
644 case TYPE_SPECIFIER_VEC4
:
645 spec
->type
= SLANG_SPEC_VEC4
;
647 case TYPE_SPECIFIER_MAT2
:
648 spec
->type
= SLANG_SPEC_MAT2
;
650 case TYPE_SPECIFIER_MAT3
:
651 spec
->type
= SLANG_SPEC_MAT3
;
653 case TYPE_SPECIFIER_MAT4
:
654 spec
->type
= SLANG_SPEC_MAT4
;
656 case TYPE_SPECIFIER_MAT23
:
657 spec
->type
= SLANG_SPEC_MAT23
;
659 case TYPE_SPECIFIER_MAT32
:
660 spec
->type
= SLANG_SPEC_MAT32
;
662 case TYPE_SPECIFIER_MAT24
:
663 spec
->type
= SLANG_SPEC_MAT24
;
665 case TYPE_SPECIFIER_MAT42
:
666 spec
->type
= SLANG_SPEC_MAT42
;
668 case TYPE_SPECIFIER_MAT34
:
669 spec
->type
= SLANG_SPEC_MAT34
;
671 case TYPE_SPECIFIER_MAT43
:
672 spec
->type
= SLANG_SPEC_MAT43
;
674 case TYPE_SPECIFIER_SAMPLER1D
:
675 spec
->type
= SLANG_SPEC_SAMPLER1D
;
677 case TYPE_SPECIFIER_SAMPLER2D
:
678 spec
->type
= SLANG_SPEC_SAMPLER2D
;
680 case TYPE_SPECIFIER_SAMPLER3D
:
681 spec
->type
= SLANG_SPEC_SAMPLER3D
;
683 case TYPE_SPECIFIER_SAMPLERCUBE
:
684 spec
->type
= SLANG_SPEC_SAMPLERCUBE
;
686 case TYPE_SPECIFIER_SAMPLER2DRECT
:
687 spec
->type
= SLANG_SPEC_SAMPLER2DRECT
;
689 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
690 spec
->type
= SLANG_SPEC_SAMPLER1DSHADOW
;
692 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
693 spec
->type
= SLANG_SPEC_SAMPLER2DSHADOW
;
695 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
696 spec
->type
= SLANG_SPEC_SAMPLER2DRECTSHADOW
;
698 case TYPE_SPECIFIER_STRUCT
:
699 spec
->type
= SLANG_SPEC_STRUCT
;
700 if (!parse_struct(C
, O
, &spec
->_struct
))
703 case TYPE_SPECIFIER_TYPENAME
:
704 spec
->type
= SLANG_SPEC_STRUCT
;
709 a_name
= parse_identifier(C
);
713 stru
= slang_struct_scope_find(O
->structs
, a_name
, 1);
715 slang_info_log_error(C
->L
, "undeclared type name '%s'",
716 slang_atom_pool_id(C
->atoms
, a_name
));
720 spec
->_struct
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
721 if (spec
->_struct
== NULL
) {
722 slang_info_log_memory(C
->L
);
725 if (!slang_struct_construct(spec
->_struct
)) {
726 _slang_free(spec
->_struct
);
727 spec
->_struct
= NULL
;
730 if (!slang_struct_copy(spec
->_struct
, stru
))
741 #define PRECISION_DEFAULT 0
742 #define PRECISION_LOW 1
743 #define PRECISION_MEDIUM 2
744 #define PRECISION_HIGH 3
747 parse_type_precision(slang_parse_ctx
*C
,
748 slang_type_precision
*precision
)
750 GLint prec
= *C
->I
++;
752 case PRECISION_DEFAULT
:
753 *precision
= SLANG_PREC_DEFAULT
;
756 *precision
= SLANG_PREC_LOW
;
758 case PRECISION_MEDIUM
:
759 *precision
= SLANG_PREC_MEDIUM
;
762 *precision
= SLANG_PREC_HIGH
;
770 parse_fully_specified_type(slang_parse_ctx
* C
, slang_output_ctx
* O
,
771 slang_fully_specified_type
* type
)
773 if (!parse_type_variant(C
, &type
->variant
))
776 if (!parse_type_centroid(C
, &type
->centroid
))
779 if (!parse_type_qualifier(C
, &type
->qualifier
))
782 if (!parse_type_precision(C
, &type
->precision
))
785 if (!parse_type_specifier(C
, O
, &type
->specifier
))
788 if (!O
->allow_invariant
&& type
->variant
== SLANG_INVARIANT
) {
789 slang_info_log_error(C
->L
,
790 "'invariant' keyword not allowed (perhaps set #version 120)");
794 if (!O
->allow_centroid
&& type
->centroid
== SLANG_CENTROID
) {
795 slang_info_log_error(C
->L
,
796 "'centroid' keyword not allowed (perhaps set #version 120)");
799 else if (type
->centroid
== SLANG_CENTROID
&&
800 type
->qualifier
!= SLANG_QUAL_VARYING
) {
801 slang_info_log_error(C
->L
,
802 "'centroid' keyword only allowed for varying vars");
808 if (type->qualifier != SLANG_QUAL_VARYING &&
809 type->variant == SLANG_INVARIANT) {
810 slang_info_log_error(C->L,
811 "invariant qualifer only allowed for varying vars");
816 if (O
->allow_precision
) {
817 if (type
->precision
== SLANG_PREC_DEFAULT
) {
818 assert(type
->specifier
.type
< TYPE_SPECIFIER_COUNT
);
819 /* use the default precision for this datatype */
820 type
->precision
= O
->default_precision
[type
->specifier
.type
];
824 /* only default is allowed */
825 if (type
->precision
!= SLANG_PREC_DEFAULT
) {
826 slang_info_log_error(C
->L
, "precision qualifiers not allowed");
836 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
837 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
841 #define OP_CONTINUE 6
844 #define OP_EXPRESSION 9
849 #define OP_PUSH_VOID 14
850 #define OP_PUSH_BOOL 15
851 #define OP_PUSH_INT 16
852 #define OP_PUSH_FLOAT 17
853 #define OP_PUSH_IDENTIFIER 18
854 #define OP_SEQUENCE 19
856 #define OP_ADDASSIGN 21
857 #define OP_SUBASSIGN 22
858 #define OP_MULASSIGN 23
859 #define OP_DIVASSIGN 24
860 /*#define OP_MODASSIGN 25*/
861 /*#define OP_LSHASSIGN 26*/
862 /*#define OP_RSHASSIGN 27*/
863 /*#define OP_ORASSIGN 28*/
864 /*#define OP_XORASSIGN 29*/
865 /*#define OP_ANDASSIGN 30*/
867 #define OP_LOGICALOR 32
868 #define OP_LOGICALXOR 33
869 #define OP_LOGICALAND 34
870 /*#define OP_BITOR 35*/
871 /*#define OP_BITXOR 36*/
872 /*#define OP_BITAND 37*/
874 #define OP_NOTEQUAL 39
876 #define OP_GREATER 41
877 #define OP_LESSEQUAL 42
878 #define OP_GREATEREQUAL 43
879 /*#define OP_LSHIFT 44*/
880 /*#define OP_RSHIFT 45*/
882 #define OP_SUBTRACT 47
883 #define OP_MULTIPLY 48
885 /*#define OP_MODULUS 50*/
886 #define OP_PREINCREMENT 51
887 #define OP_PREDECREMENT 52
890 /*#define OP_COMPLEMENT 55*/
892 #define OP_SUBSCRIPT 57
895 #define OP_POSTINCREMENT 60
896 #define OP_POSTDECREMENT 61
897 #define OP_PRECISION 62
901 * When parsing a compound production, this function is used to parse the
903 * For example, a while-loop compound will have two children, the
904 * while condition expression and the loop body. So, this function will
905 * be called twice to parse those two sub-expressions.
906 * \param C the parsing context
907 * \param O the output context
908 * \param oper the operation we're parsing
909 * \param statement indicates whether parsing a statement, or expression
910 * \return 1 if success, 0 if error
913 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
914 slang_operation
* oper
, GLboolean statement
)
918 /* grow child array */
919 ch
= slang_operation_grow(&oper
->num_children
, &oper
->children
);
921 return parse_statement(C
, O
, ch
);
922 return parse_expression(C
, O
, ch
);
925 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
928 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
929 slang_operation
* oper
)
931 oper
->locals
->outer_scope
= O
->vars
;
933 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
934 /* parse child statements, do not create new variable scope */
935 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
936 while (*C
->I
!= OP_END
)
937 if (!parse_child_operation(C
, O
, oper
, 1))
941 case OP_BLOCK_BEGIN_NEW_SCOPE
:
942 /* parse child statements, create new variable scope */
944 slang_output_ctx o
= *O
;
946 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
947 o
.vars
= oper
->locals
;
948 while (*C
->I
!= OP_END
)
949 if (!parse_child_operation(C
, &o
, oper
, 1))
955 /* local variable declaration, individual declarators are stored as
956 * children identifiers
958 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
960 const unsigned int first_var
= O
->vars
->num_variables
;
962 /* parse the declaration, note that there can be zero or more
963 * than one declarators
965 if (!parse_declaration(C
, O
))
967 if (first_var
< O
->vars
->num_variables
) {
968 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
970 assert(oper
->num_children
== 0);
971 oper
->num_children
= num_vars
;
972 oper
->children
= slang_operation_new(num_vars
);
973 if (oper
->children
== NULL
) {
974 slang_info_log_memory(C
->L
);
977 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
978 slang_operation
*o
= &oper
->children
[i
- first_var
];
979 o
->type
= SLANG_OPER_VARIABLE_DECL
;
980 o
->locals
->outer_scope
= O
->vars
;
981 o
->a_id
= O
->vars
->variables
[i
]->a_name
;
983 if (!legal_identifier(o
->a_id
)) {
984 slang_info_log_error(C
->L
, "illegal variable name '%s'",
993 /* the __asm statement, parse the mnemonic and all its arguments
996 oper
->type
= SLANG_OPER_ASM
;
997 oper
->a_id
= parse_identifier(C
);
998 if (oper
->a_id
== SLANG_ATOM_NULL
)
1000 while (*C
->I
!= OP_END
) {
1001 if (!parse_child_operation(C
, O
, oper
, 0))
1007 oper
->type
= SLANG_OPER_BREAK
;
1010 oper
->type
= SLANG_OPER_CONTINUE
;
1013 oper
->type
= SLANG_OPER_DISCARD
;
1016 oper
->type
= SLANG_OPER_RETURN
;
1017 if (!parse_child_operation(C
, O
, oper
, 0))
1021 oper
->type
= SLANG_OPER_EXPRESSION
;
1022 if (!parse_child_operation(C
, O
, oper
, 0))
1026 oper
->type
= SLANG_OPER_IF
;
1027 if (!parse_child_operation(C
, O
, oper
, 0))
1029 if (!parse_child_operation(C
, O
, oper
, 1))
1031 if (!parse_child_operation(C
, O
, oper
, 1))
1036 slang_output_ctx o
= *O
;
1038 oper
->type
= SLANG_OPER_WHILE
;
1039 o
.vars
= oper
->locals
;
1040 if (!parse_child_operation(C
, &o
, oper
, 1))
1042 if (!parse_child_operation(C
, &o
, oper
, 1))
1047 oper
->type
= SLANG_OPER_DO
;
1048 if (!parse_child_operation(C
, O
, oper
, 1))
1050 if (!parse_child_operation(C
, O
, oper
, 0))
1055 slang_output_ctx o
= *O
;
1057 oper
->type
= SLANG_OPER_FOR
;
1058 o
.vars
= oper
->locals
;
1059 if (!parse_child_operation(C
, &o
, oper
, 1))
1061 if (!parse_child_operation(C
, &o
, oper
, 1))
1063 if (!parse_child_operation(C
, &o
, oper
, 0))
1065 if (!parse_child_operation(C
, &o
, oper
, 1))
1071 /* set default precision for a type in this scope */
1072 /* ignored at this time */
1073 int prec_qual
= *C
->I
++;
1074 int datatype
= *C
->I
++;
1086 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
1087 slang_operation
** ops
, unsigned int *total_ops
,
1092 op
->children
= slang_operation_new(n
);
1093 if (op
->children
== NULL
) {
1094 slang_info_log_memory(C
->L
);
1097 op
->num_children
= n
;
1099 for (i
= 0; i
< n
; i
++) {
1100 slang_operation_destruct(&op
->children
[i
]);
1101 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
1104 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
1107 *ops
= (slang_operation
*)
1108 _slang_realloc(*ops
,
1109 (*total_ops
+ n
) * sizeof(slang_operation
),
1110 *total_ops
* sizeof(slang_operation
));
1112 slang_info_log_memory(C
->L
);
1119 is_constructor_name(const char *name
, slang_atom a_name
,
1120 slang_struct_scope
* structs
)
1122 if (slang_type_specifier_type_from_string(name
) != SLANG_SPEC_VOID
)
1124 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
1128 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1129 slang_operation
* oper
)
1131 slang_operation
*ops
= NULL
;
1132 unsigned int num_ops
= 0;
1135 while (*C
->I
!= OP_END
) {
1136 slang_operation
*op
;
1137 const unsigned int op_code
= *C
->I
++;
1139 /* allocate default operation, becomes a no-op if not used */
1140 ops
= (slang_operation
*)
1142 num_ops
* sizeof(slang_operation
),
1143 (num_ops
+ 1) * sizeof(slang_operation
));
1145 slang_info_log_memory(C
->L
);
1149 if (!slang_operation_construct(op
)) {
1150 slang_info_log_memory(C
->L
);
1154 op
->locals
->outer_scope
= O
->vars
;
1158 op
->type
= SLANG_OPER_VOID
;
1161 op
->type
= SLANG_OPER_LITERAL_BOOL
;
1162 if (!parse_number(C
, &number
))
1167 op
->literal
[3] = (GLfloat
) number
;
1168 op
->literal_size
= 1;
1171 op
->type
= SLANG_OPER_LITERAL_INT
;
1172 if (!parse_number(C
, &number
))
1177 op
->literal
[3] = (GLfloat
) number
;
1178 op
->literal_size
= 1;
1181 op
->type
= SLANG_OPER_LITERAL_FLOAT
;
1182 if (!parse_float(C
, &op
->literal
[0]))
1186 op
->literal
[3] = op
->literal
[0];
1187 op
->literal_size
= 1;
1189 case OP_PUSH_IDENTIFIER
:
1190 op
->type
= SLANG_OPER_IDENTIFIER
;
1191 op
->a_id
= parse_identifier(C
);
1192 if (op
->a_id
== SLANG_ATOM_NULL
)
1196 op
->type
= SLANG_OPER_SEQUENCE
;
1197 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1201 op
->type
= SLANG_OPER_ASSIGN
;
1202 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1206 op
->type
= SLANG_OPER_ADDASSIGN
;
1207 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1211 op
->type
= SLANG_OPER_SUBASSIGN
;
1212 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1216 op
->type
= SLANG_OPER_MULASSIGN
;
1217 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1221 op
->type
= SLANG_OPER_DIVASSIGN
;
1222 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1225 /*case OP_MODASSIGN: */
1226 /*case OP_LSHASSIGN: */
1227 /*case OP_RSHASSIGN: */
1228 /*case OP_ORASSIGN: */
1229 /*case OP_XORASSIGN: */
1230 /*case OP_ANDASSIGN: */
1232 op
->type
= SLANG_OPER_SELECT
;
1233 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1237 op
->type
= SLANG_OPER_LOGICALOR
;
1238 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1242 op
->type
= SLANG_OPER_LOGICALXOR
;
1243 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1247 op
->type
= SLANG_OPER_LOGICALAND
;
1248 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1252 /*case OP_BITXOR: */
1253 /*case OP_BITAND: */
1255 op
->type
= SLANG_OPER_EQUAL
;
1256 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1260 op
->type
= SLANG_OPER_NOTEQUAL
;
1261 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1265 op
->type
= SLANG_OPER_LESS
;
1266 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1270 op
->type
= SLANG_OPER_GREATER
;
1271 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1275 op
->type
= SLANG_OPER_LESSEQUAL
;
1276 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1279 case OP_GREATEREQUAL
:
1280 op
->type
= SLANG_OPER_GREATEREQUAL
;
1281 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1284 /*case OP_LSHIFT: */
1285 /*case OP_RSHIFT: */
1287 op
->type
= SLANG_OPER_ADD
;
1288 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1292 op
->type
= SLANG_OPER_SUBTRACT
;
1293 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1297 op
->type
= SLANG_OPER_MULTIPLY
;
1298 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1302 op
->type
= SLANG_OPER_DIVIDE
;
1303 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1306 /*case OP_MODULUS: */
1307 case OP_PREINCREMENT
:
1308 op
->type
= SLANG_OPER_PREINCREMENT
;
1309 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1312 case OP_PREDECREMENT
:
1313 op
->type
= SLANG_OPER_PREDECREMENT
;
1314 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1318 op
->type
= SLANG_OPER_PLUS
;
1319 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1323 op
->type
= SLANG_OPER_MINUS
;
1324 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1328 op
->type
= SLANG_OPER_NOT
;
1329 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1332 /*case OP_COMPLEMENT: */
1334 op
->type
= SLANG_OPER_SUBSCRIPT
;
1335 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1339 op
->type
= SLANG_OPER_CALL
;
1340 op
->a_id
= parse_identifier(C
);
1341 if (op
->a_id
== SLANG_ATOM_NULL
)
1343 while (*C
->I
!= OP_END
)
1344 if (!parse_child_operation(C
, O
, op
, 0))
1348 if (!C
->parsing_builtin
1349 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1352 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1353 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1354 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1360 op
->type
= SLANG_OPER_FIELD
;
1361 op
->a_id
= parse_identifier(C
);
1362 if (op
->a_id
== SLANG_ATOM_NULL
)
1364 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1367 case OP_POSTINCREMENT
:
1368 op
->type
= SLANG_OPER_POSTINCREMENT
;
1369 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1372 case OP_POSTDECREMENT
:
1373 op
->type
= SLANG_OPER_POSTDECREMENT
;
1374 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1383 slang_operation_destruct(oper
);
1384 *oper
= *ops
; /* struct copy */
1390 /* parameter qualifier */
1391 #define PARAM_QUALIFIER_IN 0
1392 #define PARAM_QUALIFIER_OUT 1
1393 #define PARAM_QUALIFIER_INOUT 2
1395 /* function parameter array presence */
1396 #define PARAMETER_ARRAY_NOT_PRESENT 0
1397 #define PARAMETER_ARRAY_PRESENT 1
1400 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1401 slang_variable
* param
)
1403 int param_qual
, precision_qual
;
1405 /* parse and validate the parameter's type qualifiers (there can be
1406 * two at most) because not all combinations are valid
1408 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1411 param_qual
= *C
->I
++;
1412 switch (param_qual
) {
1413 case PARAM_QUALIFIER_IN
:
1414 if (param
->type
.qualifier
!= SLANG_QUAL_CONST
1415 && param
->type
.qualifier
!= SLANG_QUAL_NONE
) {
1416 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1420 case PARAM_QUALIFIER_OUT
:
1421 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1422 param
->type
.qualifier
= SLANG_QUAL_OUT
;
1424 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1428 case PARAM_QUALIFIER_INOUT
:
1429 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1430 param
->type
.qualifier
= SLANG_QUAL_INOUT
;
1432 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1440 /* parse precision qualifier (lowp, mediump, highp */
1441 precision_qual
= *C
->I
++;
1442 /* ignored at this time */
1443 (void) precision_qual
;
1445 /* parse parameter's type specifier and name */
1446 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1448 param
->a_name
= parse_identifier(C
);
1449 if (param
->a_name
== SLANG_ATOM_NULL
)
1452 /* if the parameter is an array, parse its size (the size must be
1453 * explicitly defined
1455 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1456 slang_type_specifier p
;
1458 slang_type_specifier_ctr(&p
);
1459 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1460 slang_type_specifier_dtr(&p
);
1463 if (!convert_to_array(C
, param
, &p
)) {
1464 slang_type_specifier_dtr(&p
);
1467 slang_type_specifier_dtr(&p
);
1468 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1472 /* calculate the parameter size */
1473 if (!calculate_var_size(C
, O
, param
))
1476 /* TODO: allocate the local address here? */
1481 #define FUNCTION_ORDINARY 0
1482 #define FUNCTION_CONSTRUCTOR 1
1483 #define FUNCTION_OPERATOR 2
1485 /* function parameter */
1486 #define PARAMETER_NONE 0
1487 #define PARAMETER_NEXT 1
1490 #define OPERATOR_ADDASSIGN 1
1491 #define OPERATOR_SUBASSIGN 2
1492 #define OPERATOR_MULASSIGN 3
1493 #define OPERATOR_DIVASSIGN 4
1494 /*#define OPERATOR_MODASSIGN 5*/
1495 /*#define OPERATOR_LSHASSIGN 6*/
1496 /*#define OPERATOR_RSHASSIGN 7*/
1497 /*#define OPERATOR_ANDASSIGN 8*/
1498 /*#define OPERATOR_XORASSIGN 9*/
1499 /*#define OPERATOR_ORASSIGN 10*/
1500 #define OPERATOR_LOGICALXOR 11
1501 /*#define OPERATOR_BITOR 12*/
1502 /*#define OPERATOR_BITXOR 13*/
1503 /*#define OPERATOR_BITAND 14*/
1504 #define OPERATOR_LESS 15
1505 #define OPERATOR_GREATER 16
1506 #define OPERATOR_LESSEQUAL 17
1507 #define OPERATOR_GREATEREQUAL 18
1508 /*#define OPERATOR_LSHIFT 19*/
1509 /*#define OPERATOR_RSHIFT 20*/
1510 #define OPERATOR_MULTIPLY 21
1511 #define OPERATOR_DIVIDE 22
1512 /*#define OPERATOR_MODULUS 23*/
1513 #define OPERATOR_INCREMENT 24
1514 #define OPERATOR_DECREMENT 25
1515 #define OPERATOR_PLUS 26
1516 #define OPERATOR_MINUS 27
1517 /*#define OPERATOR_COMPLEMENT 28*/
1518 #define OPERATOR_NOT 29
1522 unsigned int o_code
;
1524 } operator_names
[] = {
1525 {OPERATOR_INCREMENT
, "++"},
1526 {OPERATOR_ADDASSIGN
, "+="},
1527 {OPERATOR_PLUS
, "+"},
1528 {OPERATOR_DECREMENT
, "--"},
1529 {OPERATOR_SUBASSIGN
, "-="},
1530 {OPERATOR_MINUS
, "-"},
1531 {OPERATOR_NOT
, "!"},
1532 {OPERATOR_MULASSIGN
, "*="},
1533 {OPERATOR_MULTIPLY
, "*"},
1534 {OPERATOR_DIVASSIGN
, "/="},
1535 {OPERATOR_DIVIDE
, "/"},
1536 {OPERATOR_LESSEQUAL
, "<="},
1537 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1538 /*{ OPERATOR_LSHIFT, "<<" }, */
1539 {OPERATOR_LESS
, "<"},
1540 {OPERATOR_GREATEREQUAL
, ">="},
1541 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1542 /*{ OPERATOR_RSHIFT, ">>" }, */
1543 {OPERATOR_GREATER
, ">"},
1544 /*{ OPERATOR_MODASSIGN, "%=" }, */
1545 /*{ OPERATOR_MODULUS, "%" }, */
1546 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1547 /*{ OPERATOR_BITAND, "&" }, */
1548 /*{ OPERATOR_ORASSIGN, "|=" }, */
1549 /*{ OPERATOR_BITOR, "|" }, */
1550 /*{ OPERATOR_COMPLEMENT, "~" }, */
1551 /*{ OPERATOR_XORASSIGN, "^=" }, */
1552 {OPERATOR_LOGICALXOR
, "^^"},
1553 /*{ OPERATOR_BITXOR, "^" } */
1557 parse_operator_name(slang_parse_ctx
* C
)
1561 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1562 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1564 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1565 if (atom
== SLANG_ATOM_NULL
) {
1566 slang_info_log_memory(C
->L
);
1578 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1579 slang_function
* func
)
1582 /* parse function type and name */
1583 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1588 case FUNCTION_ORDINARY
:
1589 func
->kind
= SLANG_FUNC_ORDINARY
;
1590 func
->header
.a_name
= parse_identifier(C
);
1591 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1594 case FUNCTION_CONSTRUCTOR
:
1595 func
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1596 if (func
->header
.type
.specifier
.type
== SLANG_SPEC_STRUCT
)
1598 func
->header
.a_name
=
1599 slang_atom_pool_atom(C
->atoms
,
1600 slang_type_specifier_type_to_string
1601 (func
->header
.type
.specifier
.type
));
1602 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1603 slang_info_log_memory(C
->L
);
1607 case FUNCTION_OPERATOR
:
1608 func
->kind
= SLANG_FUNC_OPERATOR
;
1609 func
->header
.a_name
= parse_operator_name(C
);
1610 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1617 if (!legal_identifier(func
->header
.a_name
)) {
1618 slang_info_log_error(C
->L
, "illegal function name '%s'",
1619 (char *) func
->header
.a_name
);
1623 /* parse function parameters */
1624 while (*C
->I
++ == PARAMETER_NEXT
) {
1625 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1627 slang_info_log_memory(C
->L
);
1630 if (!parse_parameter_declaration(C
, O
, p
))
1634 /* if the function returns a value, append a hidden __retVal 'out'
1635 * parameter that corresponds to the return value.
1637 if (_slang_function_has_return_value(func
)) {
1638 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1639 slang_atom a_retVal
= slang_atom_pool_atom(C
->atoms
, "__retVal");
1641 p
->a_name
= a_retVal
;
1642 p
->type
= func
->header
.type
;
1643 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1646 /* function formal parameters and local variables share the same
1647 * scope, so save the information about param count in a seperate
1648 * place also link the scope to the global variable scope so when a
1649 * given identifier is not found here, the search process continues
1650 * in the global space
1652 func
->param_count
= func
->parameters
->num_variables
;
1653 func
->parameters
->outer_scope
= O
->vars
;
1659 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1660 slang_function
* func
)
1662 slang_output_ctx o
= *O
;
1664 if (!parse_function_prototype(C
, O
, func
))
1667 /* create function's body operation */
1668 func
->body
= (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1669 if (func
->body
== NULL
) {
1670 slang_info_log_memory(C
->L
);
1673 if (!slang_operation_construct(func
->body
)) {
1674 _slang_free(func
->body
);
1676 slang_info_log_memory(C
->L
);
1680 /* to parse the body the parse context is modified in order to
1681 * capture parsed variables into function's local variable scope
1683 C
->global_scope
= GL_FALSE
;
1684 o
.vars
= func
->parameters
;
1685 if (!parse_statement(C
, &o
, func
->body
))
1688 C
->global_scope
= GL_TRUE
;
1693 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1695 slang_operation op_id
, op_assign
;
1698 /* construct the left side of assignment */
1699 if (!slang_operation_construct(&op_id
))
1701 op_id
.type
= SLANG_OPER_IDENTIFIER
;
1702 op_id
.a_id
= var
->a_name
;
1704 /* put the variable into operation's scope */
1705 op_id
.locals
->variables
=
1706 (slang_variable
**) _slang_alloc(sizeof(slang_variable
*));
1707 if (op_id
.locals
->variables
== NULL
) {
1708 slang_operation_destruct(&op_id
);
1711 op_id
.locals
->num_variables
= 1;
1712 op_id
.locals
->variables
[0] = var
;
1714 /* construct the assignment expression */
1715 if (!slang_operation_construct(&op_assign
)) {
1716 op_id
.locals
->num_variables
= 0;
1717 slang_operation_destruct(&op_id
);
1720 op_assign
.type
= SLANG_OPER_ASSIGN
;
1721 op_assign
.children
=
1722 (slang_operation
*) _slang_alloc(2 * sizeof(slang_operation
));
1723 if (op_assign
.children
== NULL
) {
1724 slang_operation_destruct(&op_assign
);
1725 op_id
.locals
->num_variables
= 0;
1726 slang_operation_destruct(&op_id
);
1729 op_assign
.num_children
= 2;
1730 op_assign
.children
[0] = op_id
;
1731 op_assign
.children
[1] = *var
->initializer
;
1735 /* carefully destroy the operations */
1736 op_assign
.num_children
= 0;
1737 _slang_free(op_assign
.children
);
1738 op_assign
.children
= NULL
;
1739 slang_operation_destruct(&op_assign
);
1740 op_id
.locals
->num_variables
= 0;
1741 slang_operation_destruct(&op_id
);
1749 /* init declarator list */
1750 #define DECLARATOR_NONE 0
1751 #define DECLARATOR_NEXT 1
1753 /* variable declaration */
1754 #define VARIABLE_NONE 0
1755 #define VARIABLE_IDENTIFIER 1
1756 #define VARIABLE_INITIALIZER 2
1757 #define VARIABLE_ARRAY_EXPLICIT 3
1758 #define VARIABLE_ARRAY_UNKNOWN 4
1762 * Parse the initializer for a variable declaration.
1765 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1766 const slang_fully_specified_type
* type
)
1768 slang_variable
*var
;
1771 /* empty init declatator (without name, e.g. "float ;") */
1772 if (*C
->I
++ == VARIABLE_NONE
)
1775 a_name
= parse_identifier(C
);
1777 /* check if name is already in this scope */
1778 if (_slang_locate_variable(O
->vars
, a_name
, GL_FALSE
)) {
1779 slang_info_log_error(C
->L
,
1780 "declaration of '%s' conflicts with previous declaration",
1785 /* make room for the new variable and initialize it */
1786 var
= slang_variable_scope_grow(O
->vars
);
1788 slang_info_log_memory(C
->L
);
1792 /* copy the declarator qualifier type, parse the identifier */
1793 var
->type
.qualifier
= type
->qualifier
;
1794 var
->a_name
= a_name
;
1795 if (var
->a_name
== SLANG_ATOM_NULL
)
1800 /* simple variable declarator - just copy the specifier */
1801 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1804 case VARIABLE_INITIALIZER
:
1805 /* initialized variable - copy the specifier and parse the expression */
1806 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1809 (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1810 if (var
->initializer
== NULL
) {
1811 slang_info_log_memory(C
->L
);
1814 if (!slang_operation_construct(var
->initializer
)) {
1815 _slang_free(var
->initializer
);
1816 var
->initializer
= NULL
;
1817 slang_info_log_memory(C
->L
);
1820 if (!parse_expression(C
, O
, var
->initializer
))
1823 case VARIABLE_ARRAY_UNKNOWN
:
1824 /* unsized array - mark it as array and copy the specifier to
1827 if (!convert_to_array(C
, var
, &type
->specifier
))
1830 case VARIABLE_ARRAY_EXPLICIT
:
1831 if (!convert_to_array(C
, var
, &type
->specifier
))
1833 if (!parse_array_len(C
, O
, &var
->array_len
))
1840 /* allocate global address space for a variable with a known size */
1842 && !(var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
1843 && var
->array_len
== 0)) {
1844 if (!calculate_var_size(C
, O
, var
))
1846 var
->address
= slang_var_pool_alloc(O
->global_pool
, var
->size
);
1849 /* emit code for global var decl */
1850 if (C
->global_scope
) {
1851 slang_assemble_ctx A
;
1853 A
.space
.funcs
= O
->funs
;
1854 A
.space
.structs
= O
->structs
;
1855 A
.space
.vars
= O
->vars
;
1856 A
.program
= O
->program
;
1857 A
.vartable
= O
->vartable
;
1859 A
.curFuncEndLabel
= NULL
;
1860 if (!_slang_codegen_global_variable(&A
, var
, C
->type
))
1864 /* initialize global variable */
1865 if (C
->global_scope
) {
1866 if (var
->initializer
!= NULL
) {
1867 slang_assemble_ctx A
;
1870 A
.space
.funcs
= O
->funs
;
1871 A
.space
.structs
= O
->structs
;
1872 A
.space
.vars
= O
->vars
;
1873 if (!initialize_global(&A
, var
))
1881 * Parse a list of variable declarations. Each variable may have an
1885 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1887 slang_fully_specified_type type
;
1889 /* parse the fully specified type, common to all declarators */
1890 if (!slang_fully_specified_type_construct(&type
))
1892 if (!parse_fully_specified_type(C
, O
, &type
)) {
1893 slang_fully_specified_type_destruct(&type
);
1897 /* parse declarators, pass-in the parsed type */
1899 if (!parse_init_declarator(C
, O
, &type
)) {
1900 slang_fully_specified_type_destruct(&type
);
1904 while (*C
->I
++ == DECLARATOR_NEXT
);
1906 slang_fully_specified_type_destruct(&type
);
1912 * Parse a function definition or declaration.
1913 * \param C parsing context
1914 * \param O output context
1915 * \param definition if non-zero expect a definition, else a declaration
1916 * \param parsed_func_ret returns the parsed function
1917 * \return GL_TRUE if success, GL_FALSE if failure
1920 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
1921 slang_function
** parsed_func_ret
)
1923 slang_function parsed_func
, *found_func
;
1925 /* parse function definition/declaration */
1926 if (!slang_function_construct(&parsed_func
))
1929 if (!parse_function_definition(C
, O
, &parsed_func
)) {
1930 slang_function_destruct(&parsed_func
);
1935 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
1936 slang_function_destruct(&parsed_func
);
1941 /* find a function with a prototype matching the parsed one - only
1942 * the current scope is being searched to allow built-in function
1945 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
1946 if (found_func
== NULL
) {
1947 /* New function, add it to the function list */
1948 O
->funs
->functions
=
1949 (slang_function
*) _slang_realloc(O
->funs
->functions
,
1950 O
->funs
->num_functions
1951 * sizeof(slang_function
),
1952 (O
->funs
->num_functions
+ 1)
1953 * sizeof(slang_function
));
1954 if (O
->funs
->functions
== NULL
) {
1955 slang_info_log_memory(C
->L
);
1956 slang_function_destruct(&parsed_func
);
1959 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
1960 O
->funs
->num_functions
++;
1962 /* return the newly parsed function */
1963 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
1966 /* previously defined or declared */
1967 /* TODO: check function return type qualifiers and specifiers */
1969 if (found_func
->body
!= NULL
) {
1970 slang_info_log_error(C
->L
, "%s: function already has a body.",
1971 slang_atom_pool_id(C
->atoms
,
1974 slang_function_destruct(&parsed_func
);
1978 /* destroy the existing function declaration and replace it
1979 * with the new one, remember to save the fixup table
1981 parsed_func
.fixups
= found_func
->fixups
;
1982 slang_fixup_table_init(&found_func
->fixups
);
1983 slang_function_destruct(found_func
);
1984 *found_func
= parsed_func
;
1987 /* another declaration of the same function prototype - ignore it */
1988 slang_function_destruct(&parsed_func
);
1991 /* return the found function */
1992 *parsed_func_ret
= found_func
;
1999 #define DECLARATION_FUNCTION_PROTOTYPE 1
2000 #define DECLARATION_INIT_DECLARATOR_LIST 2
2003 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2006 case DECLARATION_INIT_DECLARATOR_LIST
:
2007 if (!parse_init_declarator_list(C
, O
))
2010 case DECLARATION_FUNCTION_PROTOTYPE
:
2012 slang_function
*dummy_func
;
2014 if (!parse_function(C
, O
, 0, &dummy_func
))
2025 parse_default_precision(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2027 int precision
, type
;
2029 if (!O
->allow_precision
) {
2030 slang_info_log_error(C
->L
, "syntax error at \"precision\"");
2034 precision
= *C
->I
++;
2035 switch (precision
) {
2037 case PRECISION_MEDIUM
:
2038 case PRECISION_HIGH
:
2042 _mesa_problem(NULL
, "unexpected precision %d at %s:%d\n",
2043 precision
, __FILE__
, __LINE__
);
2049 case TYPE_SPECIFIER_FLOAT
:
2050 case TYPE_SPECIFIER_INT
:
2051 case TYPE_SPECIFIER_SAMPLER1D
:
2052 case TYPE_SPECIFIER_SAMPLER2D
:
2053 case TYPE_SPECIFIER_SAMPLER3D
:
2054 case TYPE_SPECIFIER_SAMPLERCUBE
:
2055 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
2056 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
2057 case TYPE_SPECIFIER_SAMPLER2DRECT
:
2058 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
2062 _mesa_problem(NULL
, "unexpected type %d at %s:%d\n",
2063 type
, __FILE__
, __LINE__
);
2067 assert(type
< TYPE_SPECIFIER_COUNT
);
2068 O
->default_precision
[type
] = precision
;
2075 * Initialize the default precision for all types.
2076 * XXX this info isn't used yet.
2079 init_default_precision(slang_output_ctx
*O
, slang_unit_type type
)
2082 for (i
= 0; i
< TYPE_SPECIFIER_COUNT
; i
++) {
2083 #if FEATURE_es2_glsl
2084 O
->default_precision
[i
] = PRECISION_LOW
;
2086 O
->default_precision
[i
] = PRECISION_HIGH
;
2090 if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2091 O
->default_precision
[TYPE_SPECIFIER_FLOAT
] = PRECISION_HIGH
;
2092 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_HIGH
;
2095 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_MEDIUM
;
2101 parse_invariant(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2103 if (O
->allow_invariant
) {
2104 slang_atom
*a
= parse_identifier(C
);
2105 /* XXX not doing anything with this var yet */
2106 /*printf("ID: %s\n", (char*) a);*/
2110 slang_info_log_error(C
->L
, "syntax error at \"invariant\"");
2116 /* external declaration or default precision specifier */
2117 #define EXTERNAL_NULL 0
2118 #define EXTERNAL_FUNCTION_DEFINITION 1
2119 #define EXTERNAL_DECLARATION 2
2120 #define DEFAULT_PRECISION 3
2121 #define INVARIANT_STMT 4
2125 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
,
2126 struct gl_shader
*shader
)
2128 GET_CURRENT_CONTEXT(ctx
);
2132 slang_function
*mainFunc
= NULL
;
2134 if (unit
->type
== SLANG_UNIT_FRAGMENT_BUILTIN
||
2135 unit
->type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2136 maxRegs
= ctx
->Const
.FragmentProgram
.MaxTemps
;
2139 assert(unit
->type
== SLANG_UNIT_VERTEX_BUILTIN
||
2140 unit
->type
== SLANG_UNIT_VERTEX_SHADER
);
2141 maxRegs
= ctx
->Const
.VertexProgram
.MaxTemps
;
2144 /* setup output context */
2145 o
.funs
= &unit
->funs
;
2146 o
.structs
= &unit
->structs
;
2147 o
.vars
= &unit
->vars
;
2148 o
.global_pool
= &unit
->object
->varpool
;
2149 o
.program
= shader
? shader
->Program
: NULL
;
2150 o
.vartable
= _slang_new_var_table(maxRegs
);
2151 _slang_push_var_table(o
.vartable
);
2153 /* allow 'invariant' keyword? */
2154 #if FEATURE_es2_glsl
2155 o
.allow_invariant
= GL_TRUE
;
2157 o
.allow_invariant
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2160 /* allow 'centroid' keyword? */
2161 o
.allow_centroid
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2163 /* allow 'lowp/mediump/highp' keywords? */
2164 #if FEATURE_es2_glsl
2165 o
.allow_precision
= GL_TRUE
;
2167 o
.allow_precision
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2169 init_default_precision(&o
, unit
->type
);
2172 /* parse individual functions and declarations */
2173 while (*C
->I
!= EXTERNAL_NULL
) {
2175 case EXTERNAL_FUNCTION_DEFINITION
:
2177 slang_function
*func
;
2178 success
= parse_function(C
, &o
, 1, &func
);
2180 _mesa_strcmp((char *) func
->header
.a_name
, "main") == 0) {
2186 case EXTERNAL_DECLARATION
:
2187 success
= parse_declaration(C
, &o
);
2189 case DEFAULT_PRECISION
:
2190 success
= parse_default_precision(C
, &o
);
2192 case INVARIANT_STMT
:
2193 success
= parse_invariant(C
, &o
);
2200 /* xxx free codegen */
2201 _slang_pop_var_table(o
.vartable
);
2208 /* assemble (generate code) for main() */
2209 slang_assemble_ctx A
;
2212 A
.space
.funcs
= o
.funs
;
2213 A
.space
.structs
= o
.structs
;
2214 A
.space
.vars
= o
.vars
;
2215 A
.program
= o
.program
;
2216 A
.vartable
= o
.vartable
;
2219 /* main() takes no parameters */
2220 if (mainFunc
->param_count
> 0) {
2221 slang_info_log_error(A
.log
, "main() takes no arguments");
2225 _slang_codegen_function(&A
, mainFunc
);
2227 shader
->Main
= GL_TRUE
; /* this shader defines main() */
2230 _slang_pop_var_table(o
.vartable
);
2231 _slang_delete_var_table(o
.vartable
);
2237 compile_binary(const byte
* prod
, slang_code_unit
* unit
,
2239 slang_unit_type type
, slang_info_log
* infolog
,
2240 slang_code_unit
* builtin
, slang_code_unit
* downlink
,
2241 struct gl_shader
*shader
)
2247 /* setup parse context */
2250 C
.parsing_builtin
= (builtin
== NULL
);
2251 C
.global_scope
= GL_TRUE
;
2252 C
.atoms
= &unit
->object
->atompool
;
2254 C
.version
= version
;
2256 if (!check_revision(&C
))
2259 if (downlink
!= NULL
) {
2260 unit
->vars
.outer_scope
= &downlink
->vars
;
2261 unit
->funs
.outer_scope
= &downlink
->funs
;
2262 unit
->structs
.outer_scope
= &downlink
->structs
;
2265 /* parse translation unit */
2266 return parse_code_unit(&C
, unit
, shader
);
2270 compile_with_grammar(grammar id
, const char *source
, slang_code_unit
* unit
,
2271 slang_unit_type type
, slang_info_log
* infolog
,
2272 slang_code_unit
* builtin
,
2273 struct gl_shader
*shader
)
2276 GLuint size
, start
, version
;
2277 slang_string preprocessed
;
2280 #if FEATURE_ARB_shading_language_120
2282 #elif FEATURE_es2_glsl
2288 /* First retrieve the version number. */
2289 if (!_slang_preprocess_version(source
, &version
, &start
, infolog
))
2292 if (version
> maxVersion
) {
2293 slang_info_log_error(infolog
,
2294 "language version %.2f is not supported.",
2299 /* Now preprocess the source string. */
2300 slang_string_init(&preprocessed
);
2301 if (!_slang_preprocess_directives(&preprocessed
, &source
[start
], infolog
)) {
2302 slang_string_free(&preprocessed
);
2303 slang_info_log_error(infolog
, "failed to preprocess the source.");
2307 /* Finally check the syntax and generate its binary representation. */
2308 if (!grammar_fast_check(id
,
2309 (const byte
*) (slang_string_cstr(&preprocessed
)),
2310 &prod
, &size
, 65536)) {
2314 slang_string_free(&preprocessed
);
2315 grammar_get_last_error((byte
*) (buf
), sizeof(buf
), &pos
);
2316 slang_info_log_error(infolog
, buf
);
2317 /* syntax error (possibly in library code) */
2322 s
= (char *) _mesa_find_line_column((const GLubyte
*) source
,
2323 (const GLubyte
*) source
+ pos
,
2325 printf("Error on line %d, col %d: %s\n", line
, col
, s
);
2330 slang_string_free(&preprocessed
);
2332 /* Syntax is okay - translate it to internal representation. */
2333 if (!compile_binary(prod
, unit
, version
, type
, infolog
, builtin
,
2334 &builtin
[SLANG_BUILTIN_TOTAL
- 1],
2336 grammar_alloc_free(prod
);
2339 grammar_alloc_free(prod
);
2343 LONGSTRING
static const char *slang_shader_syn
=
2344 #include "library/slang_shader_syn.h"
2347 static const byte slang_core_gc
[] = {
2348 #include "library/slang_core_gc.h"
2351 static const byte slang_120_core_gc
[] = {
2352 #include "library/slang_120_core_gc.h"
2355 static const byte slang_120_fragment_gc
[] = {
2356 #include "library/slang_builtin_120_fragment_gc.h"
2359 static const byte slang_common_builtin_gc
[] = {
2360 #include "library/slang_common_builtin_gc.h"
2363 static const byte slang_fragment_builtin_gc
[] = {
2364 #include "library/slang_fragment_builtin_gc.h"
2367 static const byte slang_vertex_builtin_gc
[] = {
2368 #include "library/slang_vertex_builtin_gc.h"
2372 compile_object(grammar
* id
, const char *source
, slang_code_object
* object
,
2373 slang_unit_type type
, slang_info_log
* infolog
,
2374 struct gl_shader
*shader
)
2376 slang_code_unit
*builtins
= NULL
;
2377 GLuint base_version
= 110;
2379 /* load GLSL grammar */
2380 *id
= grammar_load_from_text((const byte
*) (slang_shader_syn
));
2385 grammar_get_last_error(buf
, 1024, &pos
);
2386 slang_info_log_error(infolog
, (const char *) (buf
));
2390 /* set shader type - the syntax is slightly different for different shaders */
2391 if (type
== SLANG_UNIT_FRAGMENT_SHADER
2392 || type
== SLANG_UNIT_FRAGMENT_BUILTIN
)
2393 grammar_set_reg8(*id
, (const byte
*) "shader_type", 1);
2395 grammar_set_reg8(*id
, (const byte
*) "shader_type", 2);
2397 /* enable language extensions */
2398 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2400 /* if parsing user-specified shader, load built-in library */
2401 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_VERTEX_SHADER
) {
2402 /* compile core functionality first */
2403 if (!compile_binary(slang_core_gc
,
2404 &object
->builtin
[SLANG_BUILTIN_CORE
],
2406 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2410 #if FEATURE_ARB_shading_language_120
2411 if (!compile_binary(slang_120_core_gc
,
2412 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2414 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2415 NULL
, &object
->builtin
[SLANG_BUILTIN_CORE
], NULL
))
2419 /* compile common functions and variables, link to core */
2420 if (!compile_binary(slang_common_builtin_gc
,
2421 &object
->builtin
[SLANG_BUILTIN_COMMON
],
2422 #if FEATURE_ARB_shading_language_120
2427 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2428 #if FEATURE_ARB_shading_language_120
2429 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2431 &object
->builtin
[SLANG_BUILTIN_CORE
],
2436 /* compile target-specific functions and variables, link to common */
2437 if (type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2438 if (!compile_binary(slang_fragment_builtin_gc
,
2439 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2441 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2442 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2444 #if FEATURE_ARB_shading_language_120
2445 if (!compile_binary(slang_120_fragment_gc
,
2446 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2448 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2449 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2453 else if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2454 if (!compile_binary(slang_vertex_builtin_gc
,
2455 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2457 SLANG_UNIT_VERTEX_BUILTIN
, infolog
, NULL
,
2458 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2462 /* disable language extensions */
2463 #if NEW_SLANG /* allow-built-ins */
2464 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2466 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 0);
2468 builtins
= object
->builtin
;
2471 /* compile the actual shader - pass-in built-in library for external shader */
2472 return compile_with_grammar(*id
, source
, &object
->unit
, type
, infolog
,
2478 compile_shader(GLcontext
*ctx
, slang_code_object
* object
,
2479 slang_unit_type type
, slang_info_log
* infolog
,
2480 struct gl_shader
*shader
)
2485 #if 0 /* for debug */
2486 _mesa_printf("********* COMPILE SHADER ***********\n");
2487 _mesa_printf("%s\n", shader
->Source
);
2488 _mesa_printf("************************************\n");
2491 assert(shader
->Program
);
2493 _slang_code_object_dtr(object
);
2494 _slang_code_object_ctr(object
);
2496 success
= compile_object(&id
, shader
->Source
, object
, type
, infolog
, shader
);
2498 grammar_destroy(id
);
2508 _slang_compile(GLcontext
*ctx
, struct gl_shader
*shader
)
2511 slang_info_log info_log
;
2512 slang_code_object obj
;
2513 slang_unit_type type
;
2515 if (shader
->Type
== GL_VERTEX_SHADER
) {
2516 type
= SLANG_UNIT_VERTEX_SHADER
;
2519 assert(shader
->Type
== GL_FRAGMENT_SHADER
);
2520 type
= SLANG_UNIT_FRAGMENT_SHADER
;
2523 if (!shader
->Source
)
2526 ctx
->Shader
.MemPool
= _slang_new_mempool(1024*1024);
2528 shader
->Main
= GL_FALSE
;
2530 if (!shader
->Program
) {
2532 if (shader
->Type
== GL_VERTEX_SHADER
)
2533 progTarget
= GL_VERTEX_PROGRAM_ARB
;
2535 progTarget
= GL_FRAGMENT_PROGRAM_ARB
;
2536 shader
->Program
= ctx
->Driver
.NewProgram(ctx
, progTarget
, 1);
2537 shader
->Program
->Parameters
= _mesa_new_parameter_list();
2538 shader
->Program
->Varying
= _mesa_new_parameter_list();
2539 shader
->Program
->Attributes
= _mesa_new_parameter_list();
2542 slang_info_log_construct(&info_log
);
2543 _slang_code_object_ctr(&obj
);
2545 success
= compile_shader(ctx
, &obj
, type
, &info_log
, shader
);
2547 /* free shader's prev info log */
2548 if (shader
->InfoLog
) {
2549 _mesa_free(shader
->InfoLog
);
2550 shader
->InfoLog
= NULL
;
2553 if (info_log
.text
) {
2554 /* copy info-log string to shader object */
2555 shader
->InfoLog
= _mesa_strdup(info_log
.text
);
2558 if (info_log
.error_flag
) {
2562 slang_info_log_destruct(&info_log
);
2563 _slang_code_object_dtr(&obj
);
2565 _slang_delete_mempool((slang_mempool
*) ctx
->Shader
.MemPool
);
2566 ctx
->Shader
.MemPool
= NULL
;
2568 /* remove any reads of output registers */
2570 printf("Pre-remove output reads:\n");
2571 _mesa_print_program(shader
->Program
);
2573 _mesa_remove_output_reads(shader
->Program
, PROGRAM_OUTPUT
);
2574 if (shader
->Type
== GL_VERTEX_SHADER
) {
2575 /* and remove writes to varying vars in vertex programs */
2576 _mesa_remove_output_reads(shader
->Program
, PROGRAM_VARYING
);
2579 printf("Post-remove output reads:\n");
2580 _mesa_print_program(shader
->Program
);