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 /* Debugging aid, print file/line where parsing error is detected */
175 printf("slang error at %s:%d\n", __FILE__, __LINE__); \
181 parse_identifier_str(slang_parse_ctx
* C
, char **id
)
184 C
->I
+= _mesa_strlen(*id
) + 1;
188 parse_identifier(slang_parse_ctx
* C
)
192 id
= (const char *) C
->I
;
193 C
->I
+= _mesa_strlen(id
) + 1;
194 return slang_atom_pool_atom(C
->atoms
, id
);
198 parse_number(slang_parse_ctx
* C
, int *number
)
200 const int radix
= (int) (*C
->I
++);
202 while (*C
->I
!= '\0') {
204 if (*C
->I
>= '0' && *C
->I
<= '9')
205 digit
= (int) (*C
->I
- '0');
206 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
207 digit
= (int) (*C
->I
- 'A') + 10;
209 digit
= (int) (*C
->I
- 'a') + 10;
210 *number
= *number
* radix
+ digit
;
215 slang_info_log_warning(C
->L
, "%d: literal integer overflow.", *number
);
220 parse_float(slang_parse_ctx
* C
, float *number
)
222 char *integral
= NULL
;
223 char *fractional
= NULL
;
224 char *exponent
= NULL
;
227 parse_identifier_str(C
, &integral
);
228 parse_identifier_str(C
, &fractional
);
229 parse_identifier_str(C
, &exponent
);
231 whole
= (char *) _slang_alloc((_mesa_strlen(integral
) +
232 _mesa_strlen(fractional
) +
233 _mesa_strlen(exponent
) + 3) * sizeof(char));
235 slang_info_log_memory(C
->L
);
239 slang_string_copy(whole
, integral
);
240 slang_string_concat(whole
, ".");
241 slang_string_concat(whole
, fractional
);
242 slang_string_concat(whole
, "E");
243 slang_string_concat(whole
, exponent
);
245 *number
= (float) (_mesa_strtod(whole
, (char **) NULL
));
252 /* revision number - increment after each change affecting emitted output */
256 check_revision(slang_parse_ctx
* C
)
258 if (*C
->I
!= REVISION
) {
259 slang_info_log_error(C
->L
, "Internal compiler error.");
266 static int parse_statement(slang_parse_ctx
*, slang_output_ctx
*,
268 static int parse_expression(slang_parse_ctx
*, slang_output_ctx
*,
270 static int parse_type_specifier(slang_parse_ctx
*, slang_output_ctx
*,
271 slang_type_specifier
*);
274 parse_array_len(slang_parse_ctx
* C
, slang_output_ctx
* O
, GLuint
* len
)
276 slang_operation array_size
;
277 slang_name_space space
;
280 if (!slang_operation_construct(&array_size
))
282 if (!parse_expression(C
, O
, &array_size
)) {
283 slang_operation_destruct(&array_size
);
287 space
.funcs
= O
->funs
;
288 space
.structs
= O
->structs
;
289 space
.vars
= O
->vars
;
291 /* evaluate compile-time expression which is array size */
292 _slang_simplify(&array_size
, &space
, C
->atoms
);
294 if (array_size
.type
== SLANG_OPER_LITERAL_INT
) {
296 *len
= (GLint
) array_size
.literal
[0];
297 } else if (array_size
.type
== SLANG_OPER_IDENTIFIER
) {
298 slang_variable
*var
= _slang_locate_variable(array_size
.locals
, array_size
.a_id
, GL_TRUE
);
300 slang_info_log_error(C
->L
, "undefined variable '%s'",
301 (char *) array_size
.a_id
);
303 } else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&&
304 var
->type
.specifier
.type
== SLANG_SPEC_INT
) {
305 if (var
->initializer
&&
306 var
->initializer
->type
== SLANG_OPER_LITERAL_INT
) {
307 *len
= (GLint
) var
->initializer
->literal
[0];
310 slang_info_log_error(C
->L
, "unable to parse array size declaration");
314 slang_info_log_error(C
->L
, "unable to parse array size declaration");
321 slang_operation_destruct(&array_size
);
326 calculate_var_size(slang_parse_ctx
* C
, slang_output_ctx
* O
,
327 slang_variable
* var
)
329 slang_storage_aggregate agg
;
331 if (!slang_storage_aggregate_construct(&agg
))
333 if (!_slang_aggregate_variable(&agg
, &var
->type
.specifier
, var
->array_len
,
334 O
->funs
, O
->structs
, O
->vars
, C
->atoms
)) {
335 slang_storage_aggregate_destruct(&agg
);
338 var
->size
= _slang_sizeof_aggregate(&agg
);
339 slang_storage_aggregate_destruct(&agg
);
344 convert_to_array(slang_parse_ctx
* C
, slang_variable
* var
,
345 const slang_type_specifier
* sp
)
347 /* sized array - mark it as array, copy the specifier to the array element and
348 * parse the expression */
349 var
->type
.specifier
.type
= SLANG_SPEC_ARRAY
;
350 var
->type
.specifier
._array
= (slang_type_specifier
*)
351 _slang_alloc(sizeof(slang_type_specifier
));
352 if (var
->type
.specifier
._array
== NULL
) {
353 slang_info_log_memory(C
->L
);
356 slang_type_specifier_ctr(var
->type
.specifier
._array
);
357 return slang_type_specifier_copy(var
->type
.specifier
._array
, sp
);
360 /* structure field */
363 #define FIELD_ARRAY 2
366 parse_struct_field_var(slang_parse_ctx
* C
, slang_output_ctx
* O
,
367 slang_variable
* var
, slang_atom a_name
,
368 const slang_type_specifier
* sp
)
370 var
->a_name
= a_name
;
371 if (var
->a_name
== SLANG_ATOM_NULL
)
376 if (!slang_type_specifier_copy(&var
->type
.specifier
, sp
))
380 if (!convert_to_array(C
, var
, sp
))
382 if (!parse_array_len(C
, O
, &var
->array_len
))
389 return calculate_var_size(C
, O
, var
);
393 parse_struct_field(slang_parse_ctx
* C
, slang_output_ctx
* O
,
394 slang_struct
* st
, slang_type_specifier
* sp
)
396 slang_output_ctx o
= *O
;
398 o
.structs
= st
->structs
;
399 if (!parse_type_specifier(C
, &o
, sp
))
404 slang_variable
*var
= slang_variable_scope_grow(st
->fields
);
406 slang_info_log_memory(C
->L
);
409 a_name
= parse_identifier(C
);
410 if (_slang_locate_variable(st
->fields
, a_name
, GL_FALSE
)) {
411 slang_info_log_error(C
->L
, "duplicate field '%s'", (char *) a_name
);
415 if (!parse_struct_field_var(C
, &o
, var
, a_name
, sp
))
418 while (*C
->I
++ != FIELD_NONE
);
424 parse_struct(slang_parse_ctx
* C
, slang_output_ctx
* O
, slang_struct
** st
)
429 /* parse struct name (if any) and make sure it is unique in current scope */
430 a_name
= parse_identifier(C
);
431 if (a_name
== SLANG_ATOM_NULL
)
434 name
= slang_atom_pool_id(C
->atoms
, a_name
);
436 && slang_struct_scope_find(O
->structs
, a_name
, 0) != NULL
) {
437 slang_info_log_error(C
->L
, "%s: duplicate type name.", name
);
441 /* set-up a new struct */
442 *st
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
444 slang_info_log_memory(C
->L
);
447 if (!slang_struct_construct(*st
)) {
450 slang_info_log_memory(C
->L
);
453 (**st
).a_name
= a_name
;
454 (**st
).structs
->outer_scope
= O
->structs
;
456 /* parse individual struct fields */
458 slang_type_specifier sp
;
460 slang_type_specifier_ctr(&sp
);
461 if (!parse_struct_field(C
, O
, *st
, &sp
)) {
462 slang_type_specifier_dtr(&sp
);
465 slang_type_specifier_dtr(&sp
);
467 while (*C
->I
++ != FIELD_NONE
);
469 /* if named struct, copy it to current scope */
470 if (name
[0] != '\0') {
473 O
->structs
->structs
=
474 (slang_struct
*) _slang_realloc(O
->structs
->structs
,
475 O
->structs
->num_structs
476 * sizeof(slang_struct
),
477 (O
->structs
->num_structs
+ 1)
478 * sizeof(slang_struct
));
479 if (O
->structs
->structs
== NULL
) {
480 slang_info_log_memory(C
->L
);
483 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
484 if (!slang_struct_construct(s
))
486 O
->structs
->num_structs
++;
487 if (!slang_struct_copy(s
, *st
))
495 /* invariant qualifer */
496 #define TYPE_VARIANT 90
497 #define TYPE_INVARIANT 91
500 parse_type_variant(slang_parse_ctx
* C
, slang_type_variant
*variant
)
502 GLuint invariant
= *C
->I
++;
505 *variant
= SLANG_VARIANT
;
508 *variant
= SLANG_INVARIANT
;
516 /* centroid qualifer */
517 #define TYPE_CENTER 95
518 #define TYPE_CENTROID 96
521 parse_type_centroid(slang_parse_ctx
* C
, slang_type_centroid
*centroid
)
526 *centroid
= SLANG_CENTER
;
529 *centroid
= SLANG_CENTROID
;
538 #define TYPE_QUALIFIER_NONE 0
539 #define TYPE_QUALIFIER_CONST 1
540 #define TYPE_QUALIFIER_ATTRIBUTE 2
541 #define TYPE_QUALIFIER_VARYING 3
542 #define TYPE_QUALIFIER_UNIFORM 4
543 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
544 #define TYPE_QUALIFIER_FIXEDINPUT 6
547 parse_type_qualifier(slang_parse_ctx
* C
, slang_type_qualifier
* qual
)
549 GLuint qualifier
= *C
->I
++;
551 case TYPE_QUALIFIER_NONE
:
552 *qual
= SLANG_QUAL_NONE
;
554 case TYPE_QUALIFIER_CONST
:
555 *qual
= SLANG_QUAL_CONST
;
557 case TYPE_QUALIFIER_ATTRIBUTE
:
558 *qual
= SLANG_QUAL_ATTRIBUTE
;
560 case TYPE_QUALIFIER_VARYING
:
561 *qual
= SLANG_QUAL_VARYING
;
563 case TYPE_QUALIFIER_UNIFORM
:
564 *qual
= SLANG_QUAL_UNIFORM
;
566 case TYPE_QUALIFIER_FIXEDOUTPUT
:
567 *qual
= SLANG_QUAL_FIXEDOUTPUT
;
569 case TYPE_QUALIFIER_FIXEDINPUT
:
570 *qual
= SLANG_QUAL_FIXEDINPUT
;
579 #define TYPE_SPECIFIER_VOID 0
580 #define TYPE_SPECIFIER_BOOL 1
581 #define TYPE_SPECIFIER_BVEC2 2
582 #define TYPE_SPECIFIER_BVEC3 3
583 #define TYPE_SPECIFIER_BVEC4 4
584 #define TYPE_SPECIFIER_INT 5
585 #define TYPE_SPECIFIER_IVEC2 6
586 #define TYPE_SPECIFIER_IVEC3 7
587 #define TYPE_SPECIFIER_IVEC4 8
588 #define TYPE_SPECIFIER_FLOAT 9
589 #define TYPE_SPECIFIER_VEC2 10
590 #define TYPE_SPECIFIER_VEC3 11
591 #define TYPE_SPECIFIER_VEC4 12
592 #define TYPE_SPECIFIER_MAT2 13
593 #define TYPE_SPECIFIER_MAT3 14
594 #define TYPE_SPECIFIER_MAT4 15
595 #define TYPE_SPECIFIER_SAMPLER1D 16
596 #define TYPE_SPECIFIER_SAMPLER2D 17
597 #define TYPE_SPECIFIER_SAMPLER3D 18
598 #define TYPE_SPECIFIER_SAMPLERCUBE 19
599 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
600 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
601 #define TYPE_SPECIFIER_SAMPLER2DRECT 22
602 #define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
603 #define TYPE_SPECIFIER_STRUCT 24
604 #define TYPE_SPECIFIER_TYPENAME 25
605 #define TYPE_SPECIFIER_MAT23 26
606 #define TYPE_SPECIFIER_MAT32 27
607 #define TYPE_SPECIFIER_MAT24 28
608 #define TYPE_SPECIFIER_MAT42 29
609 #define TYPE_SPECIFIER_MAT34 30
610 #define TYPE_SPECIFIER_MAT43 31
611 #define TYPE_SPECIFIER_COUNT 32
614 parse_type_specifier(slang_parse_ctx
* C
, slang_output_ctx
* O
,
615 slang_type_specifier
* spec
)
618 case TYPE_SPECIFIER_VOID
:
619 spec
->type
= SLANG_SPEC_VOID
;
621 case TYPE_SPECIFIER_BOOL
:
622 spec
->type
= SLANG_SPEC_BOOL
;
624 case TYPE_SPECIFIER_BVEC2
:
625 spec
->type
= SLANG_SPEC_BVEC2
;
627 case TYPE_SPECIFIER_BVEC3
:
628 spec
->type
= SLANG_SPEC_BVEC3
;
630 case TYPE_SPECIFIER_BVEC4
:
631 spec
->type
= SLANG_SPEC_BVEC4
;
633 case TYPE_SPECIFIER_INT
:
634 spec
->type
= SLANG_SPEC_INT
;
636 case TYPE_SPECIFIER_IVEC2
:
637 spec
->type
= SLANG_SPEC_IVEC2
;
639 case TYPE_SPECIFIER_IVEC3
:
640 spec
->type
= SLANG_SPEC_IVEC3
;
642 case TYPE_SPECIFIER_IVEC4
:
643 spec
->type
= SLANG_SPEC_IVEC4
;
645 case TYPE_SPECIFIER_FLOAT
:
646 spec
->type
= SLANG_SPEC_FLOAT
;
648 case TYPE_SPECIFIER_VEC2
:
649 spec
->type
= SLANG_SPEC_VEC2
;
651 case TYPE_SPECIFIER_VEC3
:
652 spec
->type
= SLANG_SPEC_VEC3
;
654 case TYPE_SPECIFIER_VEC4
:
655 spec
->type
= SLANG_SPEC_VEC4
;
657 case TYPE_SPECIFIER_MAT2
:
658 spec
->type
= SLANG_SPEC_MAT2
;
660 case TYPE_SPECIFIER_MAT3
:
661 spec
->type
= SLANG_SPEC_MAT3
;
663 case TYPE_SPECIFIER_MAT4
:
664 spec
->type
= SLANG_SPEC_MAT4
;
666 case TYPE_SPECIFIER_MAT23
:
667 spec
->type
= SLANG_SPEC_MAT23
;
669 case TYPE_SPECIFIER_MAT32
:
670 spec
->type
= SLANG_SPEC_MAT32
;
672 case TYPE_SPECIFIER_MAT24
:
673 spec
->type
= SLANG_SPEC_MAT24
;
675 case TYPE_SPECIFIER_MAT42
:
676 spec
->type
= SLANG_SPEC_MAT42
;
678 case TYPE_SPECIFIER_MAT34
:
679 spec
->type
= SLANG_SPEC_MAT34
;
681 case TYPE_SPECIFIER_MAT43
:
682 spec
->type
= SLANG_SPEC_MAT43
;
684 case TYPE_SPECIFIER_SAMPLER1D
:
685 spec
->type
= SLANG_SPEC_SAMPLER1D
;
687 case TYPE_SPECIFIER_SAMPLER2D
:
688 spec
->type
= SLANG_SPEC_SAMPLER2D
;
690 case TYPE_SPECIFIER_SAMPLER3D
:
691 spec
->type
= SLANG_SPEC_SAMPLER3D
;
693 case TYPE_SPECIFIER_SAMPLERCUBE
:
694 spec
->type
= SLANG_SPEC_SAMPLERCUBE
;
696 case TYPE_SPECIFIER_SAMPLER2DRECT
:
697 spec
->type
= SLANG_SPEC_SAMPLER2DRECT
;
699 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
700 spec
->type
= SLANG_SPEC_SAMPLER1DSHADOW
;
702 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
703 spec
->type
= SLANG_SPEC_SAMPLER2DSHADOW
;
705 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
706 spec
->type
= SLANG_SPEC_SAMPLER2DRECTSHADOW
;
708 case TYPE_SPECIFIER_STRUCT
:
709 spec
->type
= SLANG_SPEC_STRUCT
;
710 if (!parse_struct(C
, O
, &spec
->_struct
))
713 case TYPE_SPECIFIER_TYPENAME
:
714 spec
->type
= SLANG_SPEC_STRUCT
;
719 a_name
= parse_identifier(C
);
723 stru
= slang_struct_scope_find(O
->structs
, a_name
, 1);
725 slang_info_log_error(C
->L
, "undeclared type name '%s'",
726 slang_atom_pool_id(C
->atoms
, a_name
));
730 spec
->_struct
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
731 if (spec
->_struct
== NULL
) {
732 slang_info_log_memory(C
->L
);
735 if (!slang_struct_construct(spec
->_struct
)) {
736 _slang_free(spec
->_struct
);
737 spec
->_struct
= NULL
;
740 if (!slang_struct_copy(spec
->_struct
, stru
))
751 #define PRECISION_DEFAULT 0
752 #define PRECISION_LOW 1
753 #define PRECISION_MEDIUM 2
754 #define PRECISION_HIGH 3
757 parse_type_precision(slang_parse_ctx
*C
,
758 slang_type_precision
*precision
)
760 GLint prec
= *C
->I
++;
762 case PRECISION_DEFAULT
:
763 *precision
= SLANG_PREC_DEFAULT
;
766 *precision
= SLANG_PREC_LOW
;
768 case PRECISION_MEDIUM
:
769 *precision
= SLANG_PREC_MEDIUM
;
772 *precision
= SLANG_PREC_HIGH
;
780 parse_fully_specified_type(slang_parse_ctx
* C
, slang_output_ctx
* O
,
781 slang_fully_specified_type
* type
)
783 if (!parse_type_variant(C
, &type
->variant
))
786 if (!parse_type_centroid(C
, &type
->centroid
))
789 if (!parse_type_qualifier(C
, &type
->qualifier
))
792 if (!parse_type_precision(C
, &type
->precision
))
795 if (!parse_type_specifier(C
, O
, &type
->specifier
))
798 if (!O
->allow_invariant
&& type
->variant
== SLANG_INVARIANT
) {
799 slang_info_log_error(C
->L
,
800 "'invariant' keyword not allowed (perhaps set #version 120)");
804 if (!O
->allow_centroid
&& type
->centroid
== SLANG_CENTROID
) {
805 slang_info_log_error(C
->L
,
806 "'centroid' keyword not allowed (perhaps set #version 120)");
809 else if (type
->centroid
== SLANG_CENTROID
&&
810 type
->qualifier
!= SLANG_QUAL_VARYING
) {
811 slang_info_log_error(C
->L
,
812 "'centroid' keyword only allowed for varying vars");
818 if (type->qualifier != SLANG_QUAL_VARYING &&
819 type->variant == SLANG_INVARIANT) {
820 slang_info_log_error(C->L,
821 "invariant qualifer only allowed for varying vars");
826 if (O
->allow_precision
) {
827 if (type
->precision
== SLANG_PREC_DEFAULT
) {
828 assert(type
->specifier
.type
< TYPE_SPECIFIER_COUNT
);
829 /* use the default precision for this datatype */
830 type
->precision
= O
->default_precision
[type
->specifier
.type
];
834 /* only default is allowed */
835 if (type
->precision
!= SLANG_PREC_DEFAULT
) {
836 slang_info_log_error(C
->L
, "precision qualifiers not allowed");
846 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
847 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
851 #define OP_CONTINUE 6
854 #define OP_EXPRESSION 9
859 #define OP_PUSH_VOID 14
860 #define OP_PUSH_BOOL 15
861 #define OP_PUSH_INT 16
862 #define OP_PUSH_FLOAT 17
863 #define OP_PUSH_IDENTIFIER 18
864 #define OP_SEQUENCE 19
866 #define OP_ADDASSIGN 21
867 #define OP_SUBASSIGN 22
868 #define OP_MULASSIGN 23
869 #define OP_DIVASSIGN 24
870 /*#define OP_MODASSIGN 25*/
871 /*#define OP_LSHASSIGN 26*/
872 /*#define OP_RSHASSIGN 27*/
873 /*#define OP_ORASSIGN 28*/
874 /*#define OP_XORASSIGN 29*/
875 /*#define OP_ANDASSIGN 30*/
877 #define OP_LOGICALOR 32
878 #define OP_LOGICALXOR 33
879 #define OP_LOGICALAND 34
880 /*#define OP_BITOR 35*/
881 /*#define OP_BITXOR 36*/
882 /*#define OP_BITAND 37*/
884 #define OP_NOTEQUAL 39
886 #define OP_GREATER 41
887 #define OP_LESSEQUAL 42
888 #define OP_GREATEREQUAL 43
889 /*#define OP_LSHIFT 44*/
890 /*#define OP_RSHIFT 45*/
892 #define OP_SUBTRACT 47
893 #define OP_MULTIPLY 48
895 /*#define OP_MODULUS 50*/
896 #define OP_PREINCREMENT 51
897 #define OP_PREDECREMENT 52
900 /*#define OP_COMPLEMENT 55*/
902 #define OP_SUBSCRIPT 57
905 #define OP_POSTINCREMENT 60
906 #define OP_POSTDECREMENT 61
907 #define OP_PRECISION 62
911 * When parsing a compound production, this function is used to parse the
913 * For example, a while-loop compound will have two children, the
914 * while condition expression and the loop body. So, this function will
915 * be called twice to parse those two sub-expressions.
916 * \param C the parsing context
917 * \param O the output context
918 * \param oper the operation we're parsing
919 * \param statement indicates whether parsing a statement, or expression
920 * \return 1 if success, 0 if error
923 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
924 slang_operation
* oper
, GLboolean statement
)
928 /* grow child array */
929 ch
= slang_operation_grow(&oper
->num_children
, &oper
->children
);
931 return parse_statement(C
, O
, ch
);
932 return parse_expression(C
, O
, ch
);
935 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
938 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
939 slang_operation
* oper
)
941 oper
->locals
->outer_scope
= O
->vars
;
943 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
944 /* parse child statements, do not create new variable scope */
945 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
946 while (*C
->I
!= OP_END
)
947 if (!parse_child_operation(C
, O
, oper
, 1))
951 case OP_BLOCK_BEGIN_NEW_SCOPE
:
952 /* parse child statements, create new variable scope */
954 slang_output_ctx o
= *O
;
956 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
957 o
.vars
= oper
->locals
;
958 while (*C
->I
!= OP_END
)
959 if (!parse_child_operation(C
, &o
, oper
, 1))
965 /* local variable declaration, individual declarators are stored as
966 * children identifiers
968 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
970 const unsigned int first_var
= O
->vars
->num_variables
;
972 /* parse the declaration, note that there can be zero or more
973 * than one declarators
975 if (!parse_declaration(C
, O
))
977 if (first_var
< O
->vars
->num_variables
) {
978 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
980 assert(oper
->num_children
== 0);
981 oper
->num_children
= num_vars
;
982 oper
->children
= slang_operation_new(num_vars
);
983 if (oper
->children
== NULL
) {
984 slang_info_log_memory(C
->L
);
987 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
988 slang_operation
*o
= &oper
->children
[i
- first_var
];
989 o
->type
= SLANG_OPER_VARIABLE_DECL
;
990 o
->locals
->outer_scope
= O
->vars
;
991 o
->a_id
= O
->vars
->variables
[i
]->a_name
;
993 if (!legal_identifier(o
->a_id
)) {
994 slang_info_log_error(C
->L
, "illegal variable name '%s'",
1003 /* the __asm statement, parse the mnemonic and all its arguments
1006 oper
->type
= SLANG_OPER_ASM
;
1007 oper
->a_id
= parse_identifier(C
);
1008 if (oper
->a_id
== SLANG_ATOM_NULL
)
1010 while (*C
->I
!= OP_END
) {
1011 if (!parse_child_operation(C
, O
, oper
, 0))
1017 oper
->type
= SLANG_OPER_BREAK
;
1020 oper
->type
= SLANG_OPER_CONTINUE
;
1023 oper
->type
= SLANG_OPER_DISCARD
;
1026 oper
->type
= SLANG_OPER_RETURN
;
1027 if (!parse_child_operation(C
, O
, oper
, 0))
1031 oper
->type
= SLANG_OPER_EXPRESSION
;
1032 if (!parse_child_operation(C
, O
, oper
, 0))
1036 oper
->type
= SLANG_OPER_IF
;
1037 if (!parse_child_operation(C
, O
, oper
, 0))
1039 if (!parse_child_operation(C
, O
, oper
, 1))
1041 if (!parse_child_operation(C
, O
, oper
, 1))
1046 slang_output_ctx o
= *O
;
1048 oper
->type
= SLANG_OPER_WHILE
;
1049 o
.vars
= oper
->locals
;
1050 if (!parse_child_operation(C
, &o
, oper
, 1))
1052 if (!parse_child_operation(C
, &o
, oper
, 1))
1057 oper
->type
= SLANG_OPER_DO
;
1058 if (!parse_child_operation(C
, O
, oper
, 1))
1060 if (!parse_child_operation(C
, O
, oper
, 0))
1065 slang_output_ctx o
= *O
;
1067 oper
->type
= SLANG_OPER_FOR
;
1068 o
.vars
= oper
->locals
;
1069 if (!parse_child_operation(C
, &o
, oper
, 1))
1071 if (!parse_child_operation(C
, &o
, oper
, 1))
1073 if (!parse_child_operation(C
, &o
, oper
, 0))
1075 if (!parse_child_operation(C
, &o
, oper
, 1))
1081 /* set default precision for a type in this scope */
1082 /* ignored at this time */
1083 int prec_qual
= *C
->I
++;
1084 int datatype
= *C
->I
++;
1096 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
1097 slang_operation
** ops
, unsigned int *total_ops
,
1102 op
->children
= slang_operation_new(n
);
1103 if (op
->children
== NULL
) {
1104 slang_info_log_memory(C
->L
);
1107 op
->num_children
= n
;
1109 for (i
= 0; i
< n
; i
++) {
1110 slang_operation_destruct(&op
->children
[i
]);
1111 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
1114 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
1117 *ops
= (slang_operation
*)
1118 _slang_realloc(*ops
,
1119 (*total_ops
+ n
) * sizeof(slang_operation
),
1120 *total_ops
* sizeof(slang_operation
));
1122 slang_info_log_memory(C
->L
);
1129 is_constructor_name(const char *name
, slang_atom a_name
,
1130 slang_struct_scope
* structs
)
1132 if (slang_type_specifier_type_from_string(name
) != SLANG_SPEC_VOID
)
1134 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
1138 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1139 slang_operation
* oper
)
1141 slang_operation
*ops
= NULL
;
1142 unsigned int num_ops
= 0;
1145 while (*C
->I
!= OP_END
) {
1146 slang_operation
*op
;
1147 const unsigned int op_code
= *C
->I
++;
1149 /* allocate default operation, becomes a no-op if not used */
1150 ops
= (slang_operation
*)
1152 num_ops
* sizeof(slang_operation
),
1153 (num_ops
+ 1) * sizeof(slang_operation
));
1155 slang_info_log_memory(C
->L
);
1159 if (!slang_operation_construct(op
)) {
1160 slang_info_log_memory(C
->L
);
1164 op
->locals
->outer_scope
= O
->vars
;
1168 op
->type
= SLANG_OPER_VOID
;
1171 op
->type
= SLANG_OPER_LITERAL_BOOL
;
1172 if (!parse_number(C
, &number
))
1177 op
->literal
[3] = (GLfloat
) number
;
1178 op
->literal_size
= 1;
1181 op
->type
= SLANG_OPER_LITERAL_INT
;
1182 if (!parse_number(C
, &number
))
1187 op
->literal
[3] = (GLfloat
) number
;
1188 op
->literal_size
= 1;
1191 op
->type
= SLANG_OPER_LITERAL_FLOAT
;
1192 if (!parse_float(C
, &op
->literal
[0]))
1196 op
->literal
[3] = op
->literal
[0];
1197 op
->literal_size
= 1;
1199 case OP_PUSH_IDENTIFIER
:
1200 op
->type
= SLANG_OPER_IDENTIFIER
;
1201 op
->a_id
= parse_identifier(C
);
1202 if (op
->a_id
== SLANG_ATOM_NULL
)
1206 op
->type
= SLANG_OPER_SEQUENCE
;
1207 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1211 op
->type
= SLANG_OPER_ASSIGN
;
1212 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1216 op
->type
= SLANG_OPER_ADDASSIGN
;
1217 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1221 op
->type
= SLANG_OPER_SUBASSIGN
;
1222 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1226 op
->type
= SLANG_OPER_MULASSIGN
;
1227 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1231 op
->type
= SLANG_OPER_DIVASSIGN
;
1232 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1235 /*case OP_MODASSIGN: */
1236 /*case OP_LSHASSIGN: */
1237 /*case OP_RSHASSIGN: */
1238 /*case OP_ORASSIGN: */
1239 /*case OP_XORASSIGN: */
1240 /*case OP_ANDASSIGN: */
1242 op
->type
= SLANG_OPER_SELECT
;
1243 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1247 op
->type
= SLANG_OPER_LOGICALOR
;
1248 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1252 op
->type
= SLANG_OPER_LOGICALXOR
;
1253 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1257 op
->type
= SLANG_OPER_LOGICALAND
;
1258 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1262 /*case OP_BITXOR: */
1263 /*case OP_BITAND: */
1265 op
->type
= SLANG_OPER_EQUAL
;
1266 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1270 op
->type
= SLANG_OPER_NOTEQUAL
;
1271 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1275 op
->type
= SLANG_OPER_LESS
;
1276 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1280 op
->type
= SLANG_OPER_GREATER
;
1281 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1285 op
->type
= SLANG_OPER_LESSEQUAL
;
1286 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1289 case OP_GREATEREQUAL
:
1290 op
->type
= SLANG_OPER_GREATEREQUAL
;
1291 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1294 /*case OP_LSHIFT: */
1295 /*case OP_RSHIFT: */
1297 op
->type
= SLANG_OPER_ADD
;
1298 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1302 op
->type
= SLANG_OPER_SUBTRACT
;
1303 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1307 op
->type
= SLANG_OPER_MULTIPLY
;
1308 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1312 op
->type
= SLANG_OPER_DIVIDE
;
1313 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1316 /*case OP_MODULUS: */
1317 case OP_PREINCREMENT
:
1318 op
->type
= SLANG_OPER_PREINCREMENT
;
1319 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1322 case OP_PREDECREMENT
:
1323 op
->type
= SLANG_OPER_PREDECREMENT
;
1324 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1328 op
->type
= SLANG_OPER_PLUS
;
1329 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1333 op
->type
= SLANG_OPER_MINUS
;
1334 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1338 op
->type
= SLANG_OPER_NOT
;
1339 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1342 /*case OP_COMPLEMENT: */
1344 op
->type
= SLANG_OPER_SUBSCRIPT
;
1345 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1349 op
->type
= SLANG_OPER_CALL
;
1350 op
->a_id
= parse_identifier(C
);
1351 if (op
->a_id
== SLANG_ATOM_NULL
)
1353 while (*C
->I
!= OP_END
)
1354 if (!parse_child_operation(C
, O
, op
, 0))
1358 if (!C
->parsing_builtin
1359 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1362 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1363 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1364 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1370 op
->type
= SLANG_OPER_FIELD
;
1371 op
->a_id
= parse_identifier(C
);
1372 if (op
->a_id
== SLANG_ATOM_NULL
)
1374 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1377 case OP_POSTINCREMENT
:
1378 op
->type
= SLANG_OPER_POSTINCREMENT
;
1379 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1382 case OP_POSTDECREMENT
:
1383 op
->type
= SLANG_OPER_POSTDECREMENT
;
1384 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1393 slang_operation_destruct(oper
);
1394 *oper
= *ops
; /* struct copy */
1400 /* parameter qualifier */
1401 #define PARAM_QUALIFIER_IN 0
1402 #define PARAM_QUALIFIER_OUT 1
1403 #define PARAM_QUALIFIER_INOUT 2
1405 /* function parameter array presence */
1406 #define PARAMETER_ARRAY_NOT_PRESENT 0
1407 #define PARAMETER_ARRAY_PRESENT 1
1410 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1411 slang_variable
* param
)
1413 int param_qual
, precision_qual
;
1415 /* parse and validate the parameter's type qualifiers (there can be
1416 * two at most) because not all combinations are valid
1418 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1421 param_qual
= *C
->I
++;
1422 switch (param_qual
) {
1423 case PARAM_QUALIFIER_IN
:
1424 if (param
->type
.qualifier
!= SLANG_QUAL_CONST
1425 && param
->type
.qualifier
!= SLANG_QUAL_NONE
) {
1426 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1430 case PARAM_QUALIFIER_OUT
:
1431 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1432 param
->type
.qualifier
= SLANG_QUAL_OUT
;
1434 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1438 case PARAM_QUALIFIER_INOUT
:
1439 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1440 param
->type
.qualifier
= SLANG_QUAL_INOUT
;
1442 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1450 /* parse precision qualifier (lowp, mediump, highp */
1451 precision_qual
= *C
->I
++;
1452 /* ignored at this time */
1453 (void) precision_qual
;
1455 /* parse parameter's type specifier and name */
1456 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1458 param
->a_name
= parse_identifier(C
);
1459 if (param
->a_name
== SLANG_ATOM_NULL
)
1462 /* if the parameter is an array, parse its size (the size must be
1463 * explicitly defined
1465 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1466 slang_type_specifier p
;
1468 slang_type_specifier_ctr(&p
);
1469 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1470 slang_type_specifier_dtr(&p
);
1473 if (!convert_to_array(C
, param
, &p
)) {
1474 slang_type_specifier_dtr(&p
);
1477 slang_type_specifier_dtr(&p
);
1478 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1482 /* calculate the parameter size */
1483 if (!calculate_var_size(C
, O
, param
))
1486 /* TODO: allocate the local address here? */
1491 #define FUNCTION_ORDINARY 0
1492 #define FUNCTION_CONSTRUCTOR 1
1493 #define FUNCTION_OPERATOR 2
1495 /* function parameter */
1496 #define PARAMETER_NONE 0
1497 #define PARAMETER_NEXT 1
1500 #define OPERATOR_ADDASSIGN 1
1501 #define OPERATOR_SUBASSIGN 2
1502 #define OPERATOR_MULASSIGN 3
1503 #define OPERATOR_DIVASSIGN 4
1504 /*#define OPERATOR_MODASSIGN 5*/
1505 /*#define OPERATOR_LSHASSIGN 6*/
1506 /*#define OPERATOR_RSHASSIGN 7*/
1507 /*#define OPERATOR_ANDASSIGN 8*/
1508 /*#define OPERATOR_XORASSIGN 9*/
1509 /*#define OPERATOR_ORASSIGN 10*/
1510 #define OPERATOR_LOGICALXOR 11
1511 /*#define OPERATOR_BITOR 12*/
1512 /*#define OPERATOR_BITXOR 13*/
1513 /*#define OPERATOR_BITAND 14*/
1514 #define OPERATOR_LESS 15
1515 #define OPERATOR_GREATER 16
1516 #define OPERATOR_LESSEQUAL 17
1517 #define OPERATOR_GREATEREQUAL 18
1518 /*#define OPERATOR_LSHIFT 19*/
1519 /*#define OPERATOR_RSHIFT 20*/
1520 #define OPERATOR_MULTIPLY 21
1521 #define OPERATOR_DIVIDE 22
1522 /*#define OPERATOR_MODULUS 23*/
1523 #define OPERATOR_INCREMENT 24
1524 #define OPERATOR_DECREMENT 25
1525 #define OPERATOR_PLUS 26
1526 #define OPERATOR_MINUS 27
1527 /*#define OPERATOR_COMPLEMENT 28*/
1528 #define OPERATOR_NOT 29
1532 unsigned int o_code
;
1534 } operator_names
[] = {
1535 {OPERATOR_INCREMENT
, "++"},
1536 {OPERATOR_ADDASSIGN
, "+="},
1537 {OPERATOR_PLUS
, "+"},
1538 {OPERATOR_DECREMENT
, "--"},
1539 {OPERATOR_SUBASSIGN
, "-="},
1540 {OPERATOR_MINUS
, "-"},
1541 {OPERATOR_NOT
, "!"},
1542 {OPERATOR_MULASSIGN
, "*="},
1543 {OPERATOR_MULTIPLY
, "*"},
1544 {OPERATOR_DIVASSIGN
, "/="},
1545 {OPERATOR_DIVIDE
, "/"},
1546 {OPERATOR_LESSEQUAL
, "<="},
1547 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1548 /*{ OPERATOR_LSHIFT, "<<" }, */
1549 {OPERATOR_LESS
, "<"},
1550 {OPERATOR_GREATEREQUAL
, ">="},
1551 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1552 /*{ OPERATOR_RSHIFT, ">>" }, */
1553 {OPERATOR_GREATER
, ">"},
1554 /*{ OPERATOR_MODASSIGN, "%=" }, */
1555 /*{ OPERATOR_MODULUS, "%" }, */
1556 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1557 /*{ OPERATOR_BITAND, "&" }, */
1558 /*{ OPERATOR_ORASSIGN, "|=" }, */
1559 /*{ OPERATOR_BITOR, "|" }, */
1560 /*{ OPERATOR_COMPLEMENT, "~" }, */
1561 /*{ OPERATOR_XORASSIGN, "^=" }, */
1562 {OPERATOR_LOGICALXOR
, "^^"},
1563 /*{ OPERATOR_BITXOR, "^" } */
1567 parse_operator_name(slang_parse_ctx
* C
)
1571 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1572 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1574 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1575 if (atom
== SLANG_ATOM_NULL
) {
1576 slang_info_log_memory(C
->L
);
1588 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1589 slang_function
* func
)
1592 /* parse function type and name */
1593 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1598 case FUNCTION_ORDINARY
:
1599 func
->kind
= SLANG_FUNC_ORDINARY
;
1600 func
->header
.a_name
= parse_identifier(C
);
1601 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1604 case FUNCTION_CONSTRUCTOR
:
1605 func
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1606 if (func
->header
.type
.specifier
.type
== SLANG_SPEC_STRUCT
)
1608 func
->header
.a_name
=
1609 slang_atom_pool_atom(C
->atoms
,
1610 slang_type_specifier_type_to_string
1611 (func
->header
.type
.specifier
.type
));
1612 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1613 slang_info_log_memory(C
->L
);
1617 case FUNCTION_OPERATOR
:
1618 func
->kind
= SLANG_FUNC_OPERATOR
;
1619 func
->header
.a_name
= parse_operator_name(C
);
1620 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1627 if (!legal_identifier(func
->header
.a_name
)) {
1628 slang_info_log_error(C
->L
, "illegal function name '%s'",
1629 (char *) func
->header
.a_name
);
1633 /* parse function parameters */
1634 while (*C
->I
++ == PARAMETER_NEXT
) {
1635 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1637 slang_info_log_memory(C
->L
);
1640 if (!parse_parameter_declaration(C
, O
, p
))
1644 /* if the function returns a value, append a hidden __retVal 'out'
1645 * parameter that corresponds to the return value.
1647 if (_slang_function_has_return_value(func
)) {
1648 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1649 slang_atom a_retVal
= slang_atom_pool_atom(C
->atoms
, "__retVal");
1651 p
->a_name
= a_retVal
;
1652 p
->type
= func
->header
.type
;
1653 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1656 /* function formal parameters and local variables share the same
1657 * scope, so save the information about param count in a seperate
1658 * place also link the scope to the global variable scope so when a
1659 * given identifier is not found here, the search process continues
1660 * in the global space
1662 func
->param_count
= func
->parameters
->num_variables
;
1663 func
->parameters
->outer_scope
= O
->vars
;
1669 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1670 slang_function
* func
)
1672 slang_output_ctx o
= *O
;
1674 if (!parse_function_prototype(C
, O
, func
))
1677 /* create function's body operation */
1678 func
->body
= (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1679 if (func
->body
== NULL
) {
1680 slang_info_log_memory(C
->L
);
1683 if (!slang_operation_construct(func
->body
)) {
1684 _slang_free(func
->body
);
1686 slang_info_log_memory(C
->L
);
1690 /* to parse the body the parse context is modified in order to
1691 * capture parsed variables into function's local variable scope
1693 C
->global_scope
= GL_FALSE
;
1694 o
.vars
= func
->parameters
;
1695 if (!parse_statement(C
, &o
, func
->body
))
1698 C
->global_scope
= GL_TRUE
;
1703 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1705 slang_operation op_id
, op_assign
;
1708 /* construct the left side of assignment */
1709 if (!slang_operation_construct(&op_id
))
1711 op_id
.type
= SLANG_OPER_IDENTIFIER
;
1712 op_id
.a_id
= var
->a_name
;
1714 /* put the variable into operation's scope */
1715 op_id
.locals
->variables
=
1716 (slang_variable
**) _slang_alloc(sizeof(slang_variable
*));
1717 if (op_id
.locals
->variables
== NULL
) {
1718 slang_operation_destruct(&op_id
);
1721 op_id
.locals
->num_variables
= 1;
1722 op_id
.locals
->variables
[0] = var
;
1724 /* construct the assignment expression */
1725 if (!slang_operation_construct(&op_assign
)) {
1726 op_id
.locals
->num_variables
= 0;
1727 slang_operation_destruct(&op_id
);
1730 op_assign
.type
= SLANG_OPER_ASSIGN
;
1731 op_assign
.children
=
1732 (slang_operation
*) _slang_alloc(2 * sizeof(slang_operation
));
1733 if (op_assign
.children
== NULL
) {
1734 slang_operation_destruct(&op_assign
);
1735 op_id
.locals
->num_variables
= 0;
1736 slang_operation_destruct(&op_id
);
1739 op_assign
.num_children
= 2;
1740 op_assign
.children
[0] = op_id
;
1741 op_assign
.children
[1] = *var
->initializer
;
1745 /* carefully destroy the operations */
1746 op_assign
.num_children
= 0;
1747 _slang_free(op_assign
.children
);
1748 op_assign
.children
= NULL
;
1749 slang_operation_destruct(&op_assign
);
1750 op_id
.locals
->num_variables
= 0;
1751 slang_operation_destruct(&op_id
);
1759 /* init declarator list */
1760 #define DECLARATOR_NONE 0
1761 #define DECLARATOR_NEXT 1
1763 /* variable declaration */
1764 #define VARIABLE_NONE 0
1765 #define VARIABLE_IDENTIFIER 1
1766 #define VARIABLE_INITIALIZER 2
1767 #define VARIABLE_ARRAY_EXPLICIT 3
1768 #define VARIABLE_ARRAY_UNKNOWN 4
1772 * Parse the initializer for a variable declaration.
1775 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1776 const slang_fully_specified_type
* type
)
1778 slang_variable
*var
;
1781 /* empty init declatator (without name, e.g. "float ;") */
1782 if (*C
->I
++ == VARIABLE_NONE
)
1785 a_name
= parse_identifier(C
);
1787 /* check if name is already in this scope */
1788 if (_slang_locate_variable(O
->vars
, a_name
, GL_FALSE
)) {
1789 slang_info_log_error(C
->L
,
1790 "declaration of '%s' conflicts with previous declaration",
1795 /* make room for the new variable and initialize it */
1796 var
= slang_variable_scope_grow(O
->vars
);
1798 slang_info_log_memory(C
->L
);
1802 /* copy the declarator type qualifier/etc info, parse the identifier */
1803 var
->type
.qualifier
= type
->qualifier
;
1804 var
->type
.centroid
= type
->centroid
;
1805 var
->type
.precision
= type
->precision
;
1806 var
->type
.variant
= type
->variant
;
1807 var
->a_name
= a_name
;
1808 if (var
->a_name
== SLANG_ATOM_NULL
)
1813 /* simple variable declarator - just copy the specifier */
1814 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1817 case VARIABLE_INITIALIZER
:
1818 /* initialized variable - copy the specifier and parse the expression */
1819 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1822 (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1823 if (var
->initializer
== NULL
) {
1824 slang_info_log_memory(C
->L
);
1827 if (!slang_operation_construct(var
->initializer
)) {
1828 _slang_free(var
->initializer
);
1829 var
->initializer
= NULL
;
1830 slang_info_log_memory(C
->L
);
1833 if (!parse_expression(C
, O
, var
->initializer
))
1836 case VARIABLE_ARRAY_UNKNOWN
:
1837 /* unsized array - mark it as array and copy the specifier to
1840 if (!convert_to_array(C
, var
, &type
->specifier
))
1843 case VARIABLE_ARRAY_EXPLICIT
:
1844 if (!convert_to_array(C
, var
, &type
->specifier
))
1846 if (!parse_array_len(C
, O
, &var
->array_len
))
1853 /* allocate global address space for a variable with a known size */
1855 && !(var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
1856 && var
->array_len
== 0)) {
1857 if (!calculate_var_size(C
, O
, var
))
1859 var
->address
= slang_var_pool_alloc(O
->global_pool
, var
->size
);
1862 /* emit code for global var decl */
1863 if (C
->global_scope
) {
1864 slang_assemble_ctx A
;
1866 A
.space
.funcs
= O
->funs
;
1867 A
.space
.structs
= O
->structs
;
1868 A
.space
.vars
= O
->vars
;
1869 A
.program
= O
->program
;
1870 A
.vartable
= O
->vartable
;
1872 A
.curFuncEndLabel
= NULL
;
1873 if (!_slang_codegen_global_variable(&A
, var
, C
->type
))
1877 /* initialize global variable */
1878 if (C
->global_scope
) {
1879 if (var
->initializer
!= NULL
) {
1880 slang_assemble_ctx A
;
1883 A
.space
.funcs
= O
->funs
;
1884 A
.space
.structs
= O
->structs
;
1885 A
.space
.vars
= O
->vars
;
1886 if (!initialize_global(&A
, var
))
1894 * Parse a list of variable declarations. Each variable may have an
1898 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1900 slang_fully_specified_type type
;
1902 /* parse the fully specified type, common to all declarators */
1903 if (!slang_fully_specified_type_construct(&type
))
1905 if (!parse_fully_specified_type(C
, O
, &type
)) {
1906 slang_fully_specified_type_destruct(&type
);
1910 /* parse declarators, pass-in the parsed type */
1912 if (!parse_init_declarator(C
, O
, &type
)) {
1913 slang_fully_specified_type_destruct(&type
);
1917 while (*C
->I
++ == DECLARATOR_NEXT
);
1919 slang_fully_specified_type_destruct(&type
);
1925 * Parse a function definition or declaration.
1926 * \param C parsing context
1927 * \param O output context
1928 * \param definition if non-zero expect a definition, else a declaration
1929 * \param parsed_func_ret returns the parsed function
1930 * \return GL_TRUE if success, GL_FALSE if failure
1933 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
1934 slang_function
** parsed_func_ret
)
1936 slang_function parsed_func
, *found_func
;
1938 /* parse function definition/declaration */
1939 if (!slang_function_construct(&parsed_func
))
1942 if (!parse_function_definition(C
, O
, &parsed_func
)) {
1943 slang_function_destruct(&parsed_func
);
1948 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
1949 slang_function_destruct(&parsed_func
);
1954 /* find a function with a prototype matching the parsed one - only
1955 * the current scope is being searched to allow built-in function
1958 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
1959 if (found_func
== NULL
) {
1960 /* New function, add it to the function list */
1961 O
->funs
->functions
=
1962 (slang_function
*) _slang_realloc(O
->funs
->functions
,
1963 O
->funs
->num_functions
1964 * sizeof(slang_function
),
1965 (O
->funs
->num_functions
+ 1)
1966 * sizeof(slang_function
));
1967 if (O
->funs
->functions
== NULL
) {
1968 slang_info_log_memory(C
->L
);
1969 slang_function_destruct(&parsed_func
);
1972 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
1973 O
->funs
->num_functions
++;
1975 /* return the newly parsed function */
1976 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
1979 /* previously defined or declared */
1980 /* TODO: check function return type qualifiers and specifiers */
1982 if (found_func
->body
!= NULL
) {
1983 slang_info_log_error(C
->L
, "%s: function already has a body.",
1984 slang_atom_pool_id(C
->atoms
,
1987 slang_function_destruct(&parsed_func
);
1991 /* destroy the existing function declaration and replace it
1992 * with the new one, remember to save the fixup table
1994 parsed_func
.fixups
= found_func
->fixups
;
1995 slang_fixup_table_init(&found_func
->fixups
);
1996 slang_function_destruct(found_func
);
1997 *found_func
= parsed_func
;
2000 /* another declaration of the same function prototype - ignore it */
2001 slang_function_destruct(&parsed_func
);
2004 /* return the found function */
2005 *parsed_func_ret
= found_func
;
2012 #define DECLARATION_FUNCTION_PROTOTYPE 1
2013 #define DECLARATION_INIT_DECLARATOR_LIST 2
2016 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2019 case DECLARATION_INIT_DECLARATOR_LIST
:
2020 if (!parse_init_declarator_list(C
, O
))
2023 case DECLARATION_FUNCTION_PROTOTYPE
:
2025 slang_function
*dummy_func
;
2027 if (!parse_function(C
, O
, 0, &dummy_func
))
2038 parse_default_precision(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2040 int precision
, type
;
2042 if (!O
->allow_precision
) {
2043 slang_info_log_error(C
->L
, "syntax error at \"precision\"");
2047 precision
= *C
->I
++;
2048 switch (precision
) {
2050 case PRECISION_MEDIUM
:
2051 case PRECISION_HIGH
:
2055 _mesa_problem(NULL
, "unexpected precision %d at %s:%d\n",
2056 precision
, __FILE__
, __LINE__
);
2062 case TYPE_SPECIFIER_FLOAT
:
2063 case TYPE_SPECIFIER_INT
:
2064 case TYPE_SPECIFIER_SAMPLER1D
:
2065 case TYPE_SPECIFIER_SAMPLER2D
:
2066 case TYPE_SPECIFIER_SAMPLER3D
:
2067 case TYPE_SPECIFIER_SAMPLERCUBE
:
2068 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
2069 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
2070 case TYPE_SPECIFIER_SAMPLER2DRECT
:
2071 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
2075 _mesa_problem(NULL
, "unexpected type %d at %s:%d\n",
2076 type
, __FILE__
, __LINE__
);
2080 assert(type
< TYPE_SPECIFIER_COUNT
);
2081 O
->default_precision
[type
] = precision
;
2088 * Initialize the default precision for all types.
2089 * XXX this info isn't used yet.
2092 init_default_precision(slang_output_ctx
*O
, slang_unit_type type
)
2095 for (i
= 0; i
< TYPE_SPECIFIER_COUNT
; i
++) {
2096 #if FEATURE_es2_glsl
2097 O
->default_precision
[i
] = PRECISION_LOW
;
2099 O
->default_precision
[i
] = PRECISION_HIGH
;
2103 if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2104 O
->default_precision
[TYPE_SPECIFIER_FLOAT
] = PRECISION_HIGH
;
2105 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_HIGH
;
2108 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_MEDIUM
;
2114 parse_invariant(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2116 if (O
->allow_invariant
) {
2117 slang_atom
*a
= parse_identifier(C
);
2118 /* XXX not doing anything with this var yet */
2119 /*printf("ID: %s\n", (char*) a);*/
2123 slang_info_log_error(C
->L
, "syntax error at \"invariant\"");
2129 /* external declaration or default precision specifier */
2130 #define EXTERNAL_NULL 0
2131 #define EXTERNAL_FUNCTION_DEFINITION 1
2132 #define EXTERNAL_DECLARATION 2
2133 #define DEFAULT_PRECISION 3
2134 #define INVARIANT_STMT 4
2138 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
,
2139 struct gl_shader
*shader
)
2141 GET_CURRENT_CONTEXT(ctx
);
2145 slang_function
*mainFunc
= NULL
;
2147 if (unit
->type
== SLANG_UNIT_FRAGMENT_BUILTIN
||
2148 unit
->type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2149 maxRegs
= ctx
->Const
.FragmentProgram
.MaxTemps
;
2152 assert(unit
->type
== SLANG_UNIT_VERTEX_BUILTIN
||
2153 unit
->type
== SLANG_UNIT_VERTEX_SHADER
);
2154 maxRegs
= ctx
->Const
.VertexProgram
.MaxTemps
;
2157 /* setup output context */
2158 o
.funs
= &unit
->funs
;
2159 o
.structs
= &unit
->structs
;
2160 o
.vars
= &unit
->vars
;
2161 o
.global_pool
= &unit
->object
->varpool
;
2162 o
.program
= shader
? shader
->Program
: NULL
;
2163 o
.vartable
= _slang_new_var_table(maxRegs
);
2164 _slang_push_var_table(o
.vartable
);
2166 /* allow 'invariant' keyword? */
2167 #if FEATURE_es2_glsl
2168 o
.allow_invariant
= GL_TRUE
;
2170 o
.allow_invariant
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2173 /* allow 'centroid' keyword? */
2174 o
.allow_centroid
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2176 /* allow 'lowp/mediump/highp' keywords? */
2177 #if FEATURE_es2_glsl
2178 o
.allow_precision
= GL_TRUE
;
2180 o
.allow_precision
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2182 init_default_precision(&o
, unit
->type
);
2185 /* parse individual functions and declarations */
2186 while (*C
->I
!= EXTERNAL_NULL
) {
2188 case EXTERNAL_FUNCTION_DEFINITION
:
2190 slang_function
*func
;
2191 success
= parse_function(C
, &o
, 1, &func
);
2193 _mesa_strcmp((char *) func
->header
.a_name
, "main") == 0) {
2199 case EXTERNAL_DECLARATION
:
2200 success
= parse_declaration(C
, &o
);
2202 case DEFAULT_PRECISION
:
2203 success
= parse_default_precision(C
, &o
);
2205 case INVARIANT_STMT
:
2206 success
= parse_invariant(C
, &o
);
2213 /* xxx free codegen */
2214 _slang_pop_var_table(o
.vartable
);
2221 /* assemble (generate code) for main() */
2222 slang_assemble_ctx A
;
2225 A
.space
.funcs
= o
.funs
;
2226 A
.space
.structs
= o
.structs
;
2227 A
.space
.vars
= o
.vars
;
2228 A
.program
= o
.program
;
2229 A
.vartable
= o
.vartable
;
2232 /* main() takes no parameters */
2233 if (mainFunc
->param_count
> 0) {
2234 slang_info_log_error(A
.log
, "main() takes no arguments");
2238 _slang_codegen_function(&A
, mainFunc
);
2240 shader
->Main
= GL_TRUE
; /* this shader defines main() */
2243 _slang_pop_var_table(o
.vartable
);
2244 _slang_delete_var_table(o
.vartable
);
2250 compile_binary(const byte
* prod
, slang_code_unit
* unit
,
2252 slang_unit_type type
, slang_info_log
* infolog
,
2253 slang_code_unit
* builtin
, slang_code_unit
* downlink
,
2254 struct gl_shader
*shader
)
2260 /* setup parse context */
2263 C
.parsing_builtin
= (builtin
== NULL
);
2264 C
.global_scope
= GL_TRUE
;
2265 C
.atoms
= &unit
->object
->atompool
;
2267 C
.version
= version
;
2269 if (!check_revision(&C
))
2272 if (downlink
!= NULL
) {
2273 unit
->vars
.outer_scope
= &downlink
->vars
;
2274 unit
->funs
.outer_scope
= &downlink
->funs
;
2275 unit
->structs
.outer_scope
= &downlink
->structs
;
2278 /* parse translation unit */
2279 return parse_code_unit(&C
, unit
, shader
);
2283 compile_with_grammar(grammar id
, const char *source
, slang_code_unit
* unit
,
2284 slang_unit_type type
, slang_info_log
* infolog
,
2285 slang_code_unit
* builtin
,
2286 struct gl_shader
*shader
)
2289 GLuint size
, start
, version
;
2290 slang_string preprocessed
;
2293 #if FEATURE_ARB_shading_language_120
2295 #elif FEATURE_es2_glsl
2301 /* First retrieve the version number. */
2302 if (!_slang_preprocess_version(source
, &version
, &start
, infolog
))
2305 if (version
> maxVersion
) {
2306 slang_info_log_error(infolog
,
2307 "language version %.2f is not supported.",
2312 /* Now preprocess the source string. */
2313 slang_string_init(&preprocessed
);
2314 if (!_slang_preprocess_directives(&preprocessed
, &source
[start
], infolog
)) {
2315 slang_string_free(&preprocessed
);
2316 slang_info_log_error(infolog
, "failed to preprocess the source.");
2320 /* Finally check the syntax and generate its binary representation. */
2321 if (!grammar_fast_check(id
,
2322 (const byte
*) (slang_string_cstr(&preprocessed
)),
2323 &prod
, &size
, 65536)) {
2327 slang_string_free(&preprocessed
);
2328 grammar_get_last_error((byte
*) (buf
), sizeof(buf
), &pos
);
2329 slang_info_log_error(infolog
, buf
);
2330 /* syntax error (possibly in library code) */
2335 s
= (char *) _mesa_find_line_column((const GLubyte
*) source
,
2336 (const GLubyte
*) source
+ pos
,
2338 printf("Error on line %d, col %d: %s\n", line
, col
, s
);
2343 slang_string_free(&preprocessed
);
2345 /* Syntax is okay - translate it to internal representation. */
2346 if (!compile_binary(prod
, unit
, version
, type
, infolog
, builtin
,
2347 &builtin
[SLANG_BUILTIN_TOTAL
- 1],
2349 grammar_alloc_free(prod
);
2352 grammar_alloc_free(prod
);
2356 LONGSTRING
static const char *slang_shader_syn
=
2357 #include "library/slang_shader_syn.h"
2360 static const byte slang_core_gc
[] = {
2361 #include "library/slang_core_gc.h"
2364 static const byte slang_120_core_gc
[] = {
2365 #include "library/slang_120_core_gc.h"
2368 static const byte slang_120_fragment_gc
[] = {
2369 #include "library/slang_builtin_120_fragment_gc.h"
2372 static const byte slang_common_builtin_gc
[] = {
2373 #include "library/slang_common_builtin_gc.h"
2376 static const byte slang_fragment_builtin_gc
[] = {
2377 #include "library/slang_fragment_builtin_gc.h"
2380 static const byte slang_vertex_builtin_gc
[] = {
2381 #include "library/slang_vertex_builtin_gc.h"
2385 compile_object(grammar
* id
, const char *source
, slang_code_object
* object
,
2386 slang_unit_type type
, slang_info_log
* infolog
,
2387 struct gl_shader
*shader
)
2389 slang_code_unit
*builtins
= NULL
;
2390 GLuint base_version
= 110;
2392 /* load GLSL grammar */
2393 *id
= grammar_load_from_text((const byte
*) (slang_shader_syn
));
2398 grammar_get_last_error(buf
, 1024, &pos
);
2399 slang_info_log_error(infolog
, (const char *) (buf
));
2403 /* set shader type - the syntax is slightly different for different shaders */
2404 if (type
== SLANG_UNIT_FRAGMENT_SHADER
2405 || type
== SLANG_UNIT_FRAGMENT_BUILTIN
)
2406 grammar_set_reg8(*id
, (const byte
*) "shader_type", 1);
2408 grammar_set_reg8(*id
, (const byte
*) "shader_type", 2);
2410 /* enable language extensions */
2411 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2413 /* if parsing user-specified shader, load built-in library */
2414 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_VERTEX_SHADER
) {
2415 /* compile core functionality first */
2416 if (!compile_binary(slang_core_gc
,
2417 &object
->builtin
[SLANG_BUILTIN_CORE
],
2419 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2423 #if FEATURE_ARB_shading_language_120
2424 if (!compile_binary(slang_120_core_gc
,
2425 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2427 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2428 NULL
, &object
->builtin
[SLANG_BUILTIN_CORE
], NULL
))
2432 /* compile common functions and variables, link to core */
2433 if (!compile_binary(slang_common_builtin_gc
,
2434 &object
->builtin
[SLANG_BUILTIN_COMMON
],
2435 #if FEATURE_ARB_shading_language_120
2440 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2441 #if FEATURE_ARB_shading_language_120
2442 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2444 &object
->builtin
[SLANG_BUILTIN_CORE
],
2449 /* compile target-specific functions and variables, link to common */
2450 if (type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2451 if (!compile_binary(slang_fragment_builtin_gc
,
2452 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2454 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2455 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2457 #if FEATURE_ARB_shading_language_120
2458 if (!compile_binary(slang_120_fragment_gc
,
2459 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2461 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2462 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2466 else if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2467 if (!compile_binary(slang_vertex_builtin_gc
,
2468 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2470 SLANG_UNIT_VERTEX_BUILTIN
, infolog
, NULL
,
2471 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2475 /* disable language extensions */
2476 #if NEW_SLANG /* allow-built-ins */
2477 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2479 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 0);
2481 builtins
= object
->builtin
;
2484 /* compile the actual shader - pass-in built-in library for external shader */
2485 return compile_with_grammar(*id
, source
, &object
->unit
, type
, infolog
,
2491 compile_shader(GLcontext
*ctx
, slang_code_object
* object
,
2492 slang_unit_type type
, slang_info_log
* infolog
,
2493 struct gl_shader
*shader
)
2498 #if 0 /* for debug */
2499 _mesa_printf("********* COMPILE SHADER ***********\n");
2500 _mesa_printf("%s\n", shader
->Source
);
2501 _mesa_printf("************************************\n");
2504 assert(shader
->Program
);
2506 _slang_code_object_dtr(object
);
2507 _slang_code_object_ctr(object
);
2509 success
= compile_object(&id
, shader
->Source
, object
, type
, infolog
, shader
);
2511 grammar_destroy(id
);
2521 _slang_compile(GLcontext
*ctx
, struct gl_shader
*shader
)
2524 slang_info_log info_log
;
2525 slang_code_object obj
;
2526 slang_unit_type type
;
2528 if (shader
->Type
== GL_VERTEX_SHADER
) {
2529 type
= SLANG_UNIT_VERTEX_SHADER
;
2532 assert(shader
->Type
== GL_FRAGMENT_SHADER
);
2533 type
= SLANG_UNIT_FRAGMENT_SHADER
;
2536 if (!shader
->Source
)
2539 ctx
->Shader
.MemPool
= _slang_new_mempool(1024*1024);
2541 shader
->Main
= GL_FALSE
;
2543 if (!shader
->Program
) {
2545 if (shader
->Type
== GL_VERTEX_SHADER
)
2546 progTarget
= GL_VERTEX_PROGRAM_ARB
;
2548 progTarget
= GL_FRAGMENT_PROGRAM_ARB
;
2549 shader
->Program
= ctx
->Driver
.NewProgram(ctx
, progTarget
, 1);
2550 shader
->Program
->Parameters
= _mesa_new_parameter_list();
2551 shader
->Program
->Varying
= _mesa_new_parameter_list();
2552 shader
->Program
->Attributes
= _mesa_new_parameter_list();
2555 slang_info_log_construct(&info_log
);
2556 _slang_code_object_ctr(&obj
);
2558 success
= compile_shader(ctx
, &obj
, type
, &info_log
, shader
);
2560 /* free shader's prev info log */
2561 if (shader
->InfoLog
) {
2562 _mesa_free(shader
->InfoLog
);
2563 shader
->InfoLog
= NULL
;
2566 if (info_log
.text
) {
2567 /* copy info-log string to shader object */
2568 shader
->InfoLog
= _mesa_strdup(info_log
.text
);
2571 if (info_log
.error_flag
) {
2575 slang_info_log_destruct(&info_log
);
2576 _slang_code_object_dtr(&obj
);
2578 _slang_delete_mempool((slang_mempool
*) ctx
->Shader
.MemPool
);
2579 ctx
->Shader
.MemPool
= NULL
;
2581 /* remove any reads of output registers */
2583 printf("Pre-remove output reads:\n");
2584 _mesa_print_program(shader
->Program
);
2586 _mesa_remove_output_reads(shader
->Program
, PROGRAM_OUTPUT
);
2587 if (shader
->Type
== GL_VERTEX_SHADER
) {
2588 /* and remove writes to varying vars in vertex programs */
2589 _mesa_remove_output_reads(shader
->Program
, PROGRAM_VARYING
);
2592 printf("Post-remove output reads:\n");
2593 _mesa_print_program(shader
->Program
);