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
];
165 /* _slang_compile() */
168 parse_identifier_str(slang_parse_ctx
* C
, char **id
)
171 C
->I
+= _mesa_strlen(*id
) + 1;
175 parse_identifier(slang_parse_ctx
* C
)
179 id
= (const char *) C
->I
;
180 C
->I
+= _mesa_strlen(id
) + 1;
181 return slang_atom_pool_atom(C
->atoms
, id
);
185 parse_number(slang_parse_ctx
* C
, int *number
)
187 const int radix
= (int) (*C
->I
++);
189 while (*C
->I
!= '\0') {
191 if (*C
->I
>= '0' && *C
->I
<= '9')
192 digit
= (int) (*C
->I
- '0');
193 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
194 digit
= (int) (*C
->I
- 'A') + 10;
196 digit
= (int) (*C
->I
- 'a') + 10;
197 *number
= *number
* radix
+ digit
;
202 slang_info_log_warning(C
->L
, "%d: literal integer overflow.", *number
);
207 parse_float(slang_parse_ctx
* C
, float *number
)
209 char *integral
= NULL
;
210 char *fractional
= NULL
;
211 char *exponent
= NULL
;
214 parse_identifier_str(C
, &integral
);
215 parse_identifier_str(C
, &fractional
);
216 parse_identifier_str(C
, &exponent
);
218 whole
= (char *) _slang_alloc((_mesa_strlen(integral
) +
219 _mesa_strlen(fractional
) +
220 _mesa_strlen(exponent
) + 3) * sizeof(char));
222 slang_info_log_memory(C
->L
);
226 slang_string_copy(whole
, integral
);
227 slang_string_concat(whole
, ".");
228 slang_string_concat(whole
, fractional
);
229 slang_string_concat(whole
, "E");
230 slang_string_concat(whole
, exponent
);
232 *number
= (float) (_mesa_strtod(whole
, (char **) NULL
));
239 /* revision number - increment after each change affecting emitted output */
243 check_revision(slang_parse_ctx
* C
)
245 if (*C
->I
!= REVISION
) {
246 slang_info_log_error(C
->L
, "Internal compiler error.");
253 static int parse_statement(slang_parse_ctx
*, slang_output_ctx
*,
255 static int parse_expression(slang_parse_ctx
*, slang_output_ctx
*,
257 static int parse_type_specifier(slang_parse_ctx
*, slang_output_ctx
*,
258 slang_type_specifier
*);
261 parse_array_len(slang_parse_ctx
* C
, slang_output_ctx
* O
, GLuint
* len
)
263 slang_operation array_size
;
264 slang_name_space space
;
267 if (!slang_operation_construct(&array_size
))
269 if (!parse_expression(C
, O
, &array_size
)) {
270 slang_operation_destruct(&array_size
);
274 space
.funcs
= O
->funs
;
275 space
.structs
= O
->structs
;
276 space
.vars
= O
->vars
;
278 /* evaluate compile-time expression which is array size */
279 _slang_simplify(&array_size
, &space
, C
->atoms
);
281 if (array_size
.type
== SLANG_OPER_LITERAL_INT
) {
283 *len
= (GLint
) array_size
.literal
[0];
284 } else if (array_size
.type
== SLANG_OPER_IDENTIFIER
) {
285 slang_variable
*var
= _slang_locate_variable(array_size
.locals
, array_size
.a_id
, GL_TRUE
);
287 slang_info_log_error(C
->L
, "undefined variable '%s'",
288 (char *) array_size
.a_id
);
290 } else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&&
291 var
->type
.specifier
.type
== SLANG_SPEC_INT
) {
292 if (var
->initializer
&&
293 var
->initializer
->type
== SLANG_OPER_LITERAL_INT
) {
294 *len
= (GLint
) var
->initializer
->literal
[0];
297 slang_info_log_error(C
->L
, "unable to parse array size declaration");
301 slang_info_log_error(C
->L
, "unable to parse array size declaration");
308 slang_operation_destruct(&array_size
);
313 calculate_var_size(slang_parse_ctx
* C
, slang_output_ctx
* O
,
314 slang_variable
* var
)
316 slang_storage_aggregate agg
;
318 if (!slang_storage_aggregate_construct(&agg
))
320 if (!_slang_aggregate_variable(&agg
, &var
->type
.specifier
, var
->array_len
,
321 O
->funs
, O
->structs
, O
->vars
, C
->atoms
)) {
322 slang_storage_aggregate_destruct(&agg
);
325 var
->size
= _slang_sizeof_aggregate(&agg
);
326 slang_storage_aggregate_destruct(&agg
);
331 convert_to_array(slang_parse_ctx
* C
, slang_variable
* var
,
332 const slang_type_specifier
* sp
)
334 /* sized array - mark it as array, copy the specifier to the array element and
335 * parse the expression */
336 var
->type
.specifier
.type
= SLANG_SPEC_ARRAY
;
337 var
->type
.specifier
._array
= (slang_type_specifier
*)
338 _slang_alloc(sizeof(slang_type_specifier
));
339 if (var
->type
.specifier
._array
== NULL
) {
340 slang_info_log_memory(C
->L
);
343 slang_type_specifier_ctr(var
->type
.specifier
._array
);
344 return slang_type_specifier_copy(var
->type
.specifier
._array
, sp
);
347 /* structure field */
350 #define FIELD_ARRAY 2
353 parse_struct_field_var(slang_parse_ctx
* C
, slang_output_ctx
* O
,
354 slang_variable
* var
, slang_atom a_name
,
355 const slang_type_specifier
* sp
)
357 var
->a_name
= a_name
;
358 if (var
->a_name
== SLANG_ATOM_NULL
)
363 if (!slang_type_specifier_copy(&var
->type
.specifier
, sp
))
367 if (!convert_to_array(C
, var
, sp
))
369 if (!parse_array_len(C
, O
, &var
->array_len
))
376 return calculate_var_size(C
, O
, var
);
380 parse_struct_field(slang_parse_ctx
* C
, slang_output_ctx
* O
,
381 slang_struct
* st
, slang_type_specifier
* sp
)
383 slang_output_ctx o
= *O
;
385 o
.structs
= st
->structs
;
386 if (!parse_type_specifier(C
, &o
, sp
))
391 slang_variable
*var
= slang_variable_scope_grow(st
->fields
);
393 slang_info_log_memory(C
->L
);
396 a_name
= parse_identifier(C
);
397 if (_slang_locate_variable(st
->fields
, a_name
, GL_FALSE
)) {
398 slang_info_log_error(C
->L
, "duplicate field '%s'", (char *) a_name
);
402 if (!parse_struct_field_var(C
, &o
, var
, a_name
, sp
))
405 while (*C
->I
++ != FIELD_NONE
);
411 parse_struct(slang_parse_ctx
* C
, slang_output_ctx
* O
, slang_struct
** st
)
416 /* parse struct name (if any) and make sure it is unique in current scope */
417 a_name
= parse_identifier(C
);
418 if (a_name
== SLANG_ATOM_NULL
)
421 name
= slang_atom_pool_id(C
->atoms
, a_name
);
423 && slang_struct_scope_find(O
->structs
, a_name
, 0) != NULL
) {
424 slang_info_log_error(C
->L
, "%s: duplicate type name.", name
);
428 /* set-up a new struct */
429 *st
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
431 slang_info_log_memory(C
->L
);
434 if (!slang_struct_construct(*st
)) {
437 slang_info_log_memory(C
->L
);
440 (**st
).a_name
= a_name
;
441 (**st
).structs
->outer_scope
= O
->structs
;
443 /* parse individual struct fields */
445 slang_type_specifier sp
;
447 slang_type_specifier_ctr(&sp
);
448 if (!parse_struct_field(C
, O
, *st
, &sp
)) {
449 slang_type_specifier_dtr(&sp
);
452 slang_type_specifier_dtr(&sp
);
454 while (*C
->I
++ != FIELD_NONE
);
456 /* if named struct, copy it to current scope */
457 if (name
[0] != '\0') {
460 O
->structs
->structs
=
461 (slang_struct
*) _slang_realloc(O
->structs
->structs
,
462 O
->structs
->num_structs
463 * sizeof(slang_struct
),
464 (O
->structs
->num_structs
+ 1)
465 * sizeof(slang_struct
));
466 if (O
->structs
->structs
== NULL
) {
467 slang_info_log_memory(C
->L
);
470 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
471 if (!slang_struct_construct(s
))
473 O
->structs
->num_structs
++;
474 if (!slang_struct_copy(s
, *st
))
483 #define TYPE_QUALIFIER_NONE 0
484 #define TYPE_QUALIFIER_CONST 1
485 #define TYPE_QUALIFIER_ATTRIBUTE 2
486 #define TYPE_QUALIFIER_VARYING 3
487 #define TYPE_QUALIFIER_UNIFORM 4
488 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
489 #define TYPE_QUALIFIER_FIXEDINPUT 6
492 parse_type_qualifier(slang_parse_ctx
* C
, slang_type_qualifier
* qual
)
495 case TYPE_QUALIFIER_NONE
:
496 *qual
= SLANG_QUAL_NONE
;
498 case TYPE_QUALIFIER_CONST
:
499 *qual
= SLANG_QUAL_CONST
;
501 case TYPE_QUALIFIER_ATTRIBUTE
:
502 *qual
= SLANG_QUAL_ATTRIBUTE
;
504 case TYPE_QUALIFIER_VARYING
:
505 *qual
= SLANG_QUAL_VARYING
;
507 case TYPE_QUALIFIER_UNIFORM
:
508 *qual
= SLANG_QUAL_UNIFORM
;
510 case TYPE_QUALIFIER_FIXEDOUTPUT
:
511 *qual
= SLANG_QUAL_FIXEDOUTPUT
;
513 case TYPE_QUALIFIER_FIXEDINPUT
:
514 *qual
= SLANG_QUAL_FIXEDINPUT
;
523 #define TYPE_SPECIFIER_VOID 0
524 #define TYPE_SPECIFIER_BOOL 1
525 #define TYPE_SPECIFIER_BVEC2 2
526 #define TYPE_SPECIFIER_BVEC3 3
527 #define TYPE_SPECIFIER_BVEC4 4
528 #define TYPE_SPECIFIER_INT 5
529 #define TYPE_SPECIFIER_IVEC2 6
530 #define TYPE_SPECIFIER_IVEC3 7
531 #define TYPE_SPECIFIER_IVEC4 8
532 #define TYPE_SPECIFIER_FLOAT 9
533 #define TYPE_SPECIFIER_VEC2 10
534 #define TYPE_SPECIFIER_VEC3 11
535 #define TYPE_SPECIFIER_VEC4 12
536 #define TYPE_SPECIFIER_MAT2 13
537 #define TYPE_SPECIFIER_MAT3 14
538 #define TYPE_SPECIFIER_MAT4 15
539 #define TYPE_SPECIFIER_SAMPLER1D 16
540 #define TYPE_SPECIFIER_SAMPLER2D 17
541 #define TYPE_SPECIFIER_SAMPLER3D 18
542 #define TYPE_SPECIFIER_SAMPLERCUBE 19
543 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
544 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
545 #define TYPE_SPECIFIER_SAMPLER2DRECT 22
546 #define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
547 #define TYPE_SPECIFIER_STRUCT 24
548 #define TYPE_SPECIFIER_TYPENAME 25
549 #define TYPE_SPECIFIER_MAT23 26
550 #define TYPE_SPECIFIER_MAT32 27
551 #define TYPE_SPECIFIER_MAT24 28
552 #define TYPE_SPECIFIER_MAT42 29
553 #define TYPE_SPECIFIER_MAT34 30
554 #define TYPE_SPECIFIER_MAT43 31
555 #define TYPE_SPECIFIER_COUNT 32
558 parse_type_specifier(slang_parse_ctx
* C
, slang_output_ctx
* O
,
559 slang_type_specifier
* spec
)
562 case TYPE_SPECIFIER_VOID
:
563 spec
->type
= SLANG_SPEC_VOID
;
565 case TYPE_SPECIFIER_BOOL
:
566 spec
->type
= SLANG_SPEC_BOOL
;
568 case TYPE_SPECIFIER_BVEC2
:
569 spec
->type
= SLANG_SPEC_BVEC2
;
571 case TYPE_SPECIFIER_BVEC3
:
572 spec
->type
= SLANG_SPEC_BVEC3
;
574 case TYPE_SPECIFIER_BVEC4
:
575 spec
->type
= SLANG_SPEC_BVEC4
;
577 case TYPE_SPECIFIER_INT
:
578 spec
->type
= SLANG_SPEC_INT
;
580 case TYPE_SPECIFIER_IVEC2
:
581 spec
->type
= SLANG_SPEC_IVEC2
;
583 case TYPE_SPECIFIER_IVEC3
:
584 spec
->type
= SLANG_SPEC_IVEC3
;
586 case TYPE_SPECIFIER_IVEC4
:
587 spec
->type
= SLANG_SPEC_IVEC4
;
589 case TYPE_SPECIFIER_FLOAT
:
590 spec
->type
= SLANG_SPEC_FLOAT
;
592 case TYPE_SPECIFIER_VEC2
:
593 spec
->type
= SLANG_SPEC_VEC2
;
595 case TYPE_SPECIFIER_VEC3
:
596 spec
->type
= SLANG_SPEC_VEC3
;
598 case TYPE_SPECIFIER_VEC4
:
599 spec
->type
= SLANG_SPEC_VEC4
;
601 case TYPE_SPECIFIER_MAT2
:
602 spec
->type
= SLANG_SPEC_MAT2
;
604 case TYPE_SPECIFIER_MAT3
:
605 spec
->type
= SLANG_SPEC_MAT3
;
607 case TYPE_SPECIFIER_MAT4
:
608 spec
->type
= SLANG_SPEC_MAT4
;
610 case TYPE_SPECIFIER_MAT23
:
611 spec
->type
= SLANG_SPEC_MAT23
;
613 case TYPE_SPECIFIER_MAT32
:
614 spec
->type
= SLANG_SPEC_MAT32
;
616 case TYPE_SPECIFIER_MAT24
:
617 spec
->type
= SLANG_SPEC_MAT24
;
619 case TYPE_SPECIFIER_MAT42
:
620 spec
->type
= SLANG_SPEC_MAT42
;
622 case TYPE_SPECIFIER_MAT34
:
623 spec
->type
= SLANG_SPEC_MAT34
;
625 case TYPE_SPECIFIER_MAT43
:
626 spec
->type
= SLANG_SPEC_MAT43
;
628 case TYPE_SPECIFIER_SAMPLER1D
:
629 spec
->type
= SLANG_SPEC_SAMPLER1D
;
631 case TYPE_SPECIFIER_SAMPLER2D
:
632 spec
->type
= SLANG_SPEC_SAMPLER2D
;
634 case TYPE_SPECIFIER_SAMPLER3D
:
635 spec
->type
= SLANG_SPEC_SAMPLER3D
;
637 case TYPE_SPECIFIER_SAMPLERCUBE
:
638 spec
->type
= SLANG_SPEC_SAMPLERCUBE
;
640 case TYPE_SPECIFIER_SAMPLER2DRECT
:
641 spec
->type
= SLANG_SPEC_SAMPLER2DRECT
;
643 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
644 spec
->type
= SLANG_SPEC_SAMPLER1DSHADOW
;
646 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
647 spec
->type
= SLANG_SPEC_SAMPLER2DSHADOW
;
649 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
650 spec
->type
= SLANG_SPEC_SAMPLER2DRECTSHADOW
;
652 case TYPE_SPECIFIER_STRUCT
:
653 spec
->type
= SLANG_SPEC_STRUCT
;
654 if (!parse_struct(C
, O
, &spec
->_struct
))
657 case TYPE_SPECIFIER_TYPENAME
:
658 spec
->type
= SLANG_SPEC_STRUCT
;
663 a_name
= parse_identifier(C
);
667 stru
= slang_struct_scope_find(O
->structs
, a_name
, 1);
669 slang_info_log_error(C
->L
, "undeclared type name '%s'",
670 slang_atom_pool_id(C
->atoms
, a_name
));
674 spec
->_struct
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
675 if (spec
->_struct
== NULL
) {
676 slang_info_log_memory(C
->L
);
679 if (!slang_struct_construct(spec
->_struct
)) {
680 _slang_free(spec
->_struct
);
681 spec
->_struct
= NULL
;
684 if (!slang_struct_copy(spec
->_struct
, stru
))
695 parse_fully_specified_type(slang_parse_ctx
* C
, slang_output_ctx
* O
,
696 slang_fully_specified_type
* type
)
698 if (!parse_type_qualifier(C
, &type
->qualifier
))
700 if (!parse_type_specifier(C
, O
, &type
->specifier
))
707 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
708 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
712 #define OP_CONTINUE 6
715 #define OP_EXPRESSION 9
720 #define OP_PUSH_VOID 14
721 #define OP_PUSH_BOOL 15
722 #define OP_PUSH_INT 16
723 #define OP_PUSH_FLOAT 17
724 #define OP_PUSH_IDENTIFIER 18
725 #define OP_SEQUENCE 19
727 #define OP_ADDASSIGN 21
728 #define OP_SUBASSIGN 22
729 #define OP_MULASSIGN 23
730 #define OP_DIVASSIGN 24
731 /*#define OP_MODASSIGN 25*/
732 /*#define OP_LSHASSIGN 26*/
733 /*#define OP_RSHASSIGN 27*/
734 /*#define OP_ORASSIGN 28*/
735 /*#define OP_XORASSIGN 29*/
736 /*#define OP_ANDASSIGN 30*/
738 #define OP_LOGICALOR 32
739 #define OP_LOGICALXOR 33
740 #define OP_LOGICALAND 34
741 /*#define OP_BITOR 35*/
742 /*#define OP_BITXOR 36*/
743 /*#define OP_BITAND 37*/
745 #define OP_NOTEQUAL 39
747 #define OP_GREATER 41
748 #define OP_LESSEQUAL 42
749 #define OP_GREATEREQUAL 43
750 /*#define OP_LSHIFT 44*/
751 /*#define OP_RSHIFT 45*/
753 #define OP_SUBTRACT 47
754 #define OP_MULTIPLY 48
756 /*#define OP_MODULUS 50*/
757 #define OP_PREINCREMENT 51
758 #define OP_PREDECREMENT 52
761 /*#define OP_COMPLEMENT 55*/
763 #define OP_SUBSCRIPT 57
766 #define OP_POSTINCREMENT 60
767 #define OP_POSTDECREMENT 61
771 * When parsing a compound production, this function is used to parse the
773 * For example, a while-loop compound will have two children, the
774 * while condition expression and the loop body. So, this function will
775 * be called twice to parse those two sub-expressions.
776 * \param C the parsing context
777 * \param O the output context
778 * \param oper the operation we're parsing
779 * \param statement indicates whether parsing a statement, or expression
780 * \return 1 if success, 0 if error
783 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
784 slang_operation
* oper
, GLboolean statement
)
788 /* grow child array */
789 ch
= slang_operation_grow(&oper
->num_children
, &oper
->children
);
791 return parse_statement(C
, O
, ch
);
792 return parse_expression(C
, O
, ch
);
795 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
798 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
799 slang_operation
* oper
)
801 oper
->locals
->outer_scope
= O
->vars
;
803 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
804 /* parse child statements, do not create new variable scope */
805 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
806 while (*C
->I
!= OP_END
)
807 if (!parse_child_operation(C
, O
, oper
, 1))
811 case OP_BLOCK_BEGIN_NEW_SCOPE
:
812 /* parse child statements, create new variable scope */
814 slang_output_ctx o
= *O
;
816 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
817 o
.vars
= oper
->locals
;
818 while (*C
->I
!= OP_END
)
819 if (!parse_child_operation(C
, &o
, oper
, 1))
825 /* local variable declaration, individual declarators are stored as
826 * children identifiers
828 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
830 const unsigned int first_var
= O
->vars
->num_variables
;
832 /* parse the declaration, note that there can be zero or more
833 * than one declarators
835 if (!parse_declaration(C
, O
))
837 if (first_var
< O
->vars
->num_variables
) {
838 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
840 assert(oper
->num_children
== 0);
841 oper
->num_children
= num_vars
;
842 oper
->children
= slang_operation_new(num_vars
);
843 if (oper
->children
== NULL
) {
844 slang_info_log_memory(C
->L
);
847 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
848 slang_operation
*o
= &oper
->children
[i
- first_var
];
849 o
->type
= SLANG_OPER_VARIABLE_DECL
;
850 o
->locals
->outer_scope
= O
->vars
;
851 o
->a_id
= O
->vars
->variables
[i
]->a_name
;
853 if (!legal_identifier(o
->a_id
)) {
854 slang_info_log_error(C
->L
, "illegal variable name '%s'",
863 /* the __asm statement, parse the mnemonic and all its arguments
866 oper
->type
= SLANG_OPER_ASM
;
867 oper
->a_id
= parse_identifier(C
);
868 if (oper
->a_id
== SLANG_ATOM_NULL
)
870 while (*C
->I
!= OP_END
) {
871 if (!parse_child_operation(C
, O
, oper
, 0))
877 oper
->type
= SLANG_OPER_BREAK
;
880 oper
->type
= SLANG_OPER_CONTINUE
;
883 oper
->type
= SLANG_OPER_DISCARD
;
886 oper
->type
= SLANG_OPER_RETURN
;
887 if (!parse_child_operation(C
, O
, oper
, 0))
891 oper
->type
= SLANG_OPER_EXPRESSION
;
892 if (!parse_child_operation(C
, O
, oper
, 0))
896 oper
->type
= SLANG_OPER_IF
;
897 if (!parse_child_operation(C
, O
, oper
, 0))
899 if (!parse_child_operation(C
, O
, oper
, 1))
901 if (!parse_child_operation(C
, O
, oper
, 1))
906 slang_output_ctx o
= *O
;
908 oper
->type
= SLANG_OPER_WHILE
;
909 o
.vars
= oper
->locals
;
910 if (!parse_child_operation(C
, &o
, oper
, 1))
912 if (!parse_child_operation(C
, &o
, oper
, 1))
917 oper
->type
= SLANG_OPER_DO
;
918 if (!parse_child_operation(C
, O
, oper
, 1))
920 if (!parse_child_operation(C
, O
, oper
, 0))
925 slang_output_ctx o
= *O
;
927 oper
->type
= SLANG_OPER_FOR
;
928 o
.vars
= oper
->locals
;
929 if (!parse_child_operation(C
, &o
, oper
, 1))
931 if (!parse_child_operation(C
, &o
, oper
, 1))
933 if (!parse_child_operation(C
, &o
, oper
, 0))
935 if (!parse_child_operation(C
, &o
, oper
, 1))
946 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
947 slang_operation
** ops
, unsigned int *total_ops
,
952 op
->children
= slang_operation_new(n
);
953 if (op
->children
== NULL
) {
954 slang_info_log_memory(C
->L
);
957 op
->num_children
= n
;
959 for (i
= 0; i
< n
; i
++) {
960 slang_operation_destruct(&op
->children
[i
]);
961 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
964 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
967 *ops
= (slang_operation
*)
969 (*total_ops
+ n
) * sizeof(slang_operation
),
970 *total_ops
* sizeof(slang_operation
));
972 slang_info_log_memory(C
->L
);
979 is_constructor_name(const char *name
, slang_atom a_name
,
980 slang_struct_scope
* structs
)
982 if (slang_type_specifier_type_from_string(name
) != SLANG_SPEC_VOID
)
984 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
988 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
989 slang_operation
* oper
)
991 slang_operation
*ops
= NULL
;
992 unsigned int num_ops
= 0;
995 while (*C
->I
!= OP_END
) {
997 const unsigned int op_code
= *C
->I
++;
999 /* allocate default operation, becomes a no-op if not used */
1000 ops
= (slang_operation
*)
1002 num_ops
* sizeof(slang_operation
),
1003 (num_ops
+ 1) * sizeof(slang_operation
));
1005 slang_info_log_memory(C
->L
);
1009 if (!slang_operation_construct(op
)) {
1010 slang_info_log_memory(C
->L
);
1014 op
->locals
->outer_scope
= O
->vars
;
1018 op
->type
= SLANG_OPER_VOID
;
1021 op
->type
= SLANG_OPER_LITERAL_BOOL
;
1022 if (!parse_number(C
, &number
))
1027 op
->literal
[3] = (GLfloat
) number
;
1028 op
->literal_size
= 1;
1031 op
->type
= SLANG_OPER_LITERAL_INT
;
1032 if (!parse_number(C
, &number
))
1037 op
->literal
[3] = (GLfloat
) number
;
1038 op
->literal_size
= 1;
1041 op
->type
= SLANG_OPER_LITERAL_FLOAT
;
1042 if (!parse_float(C
, &op
->literal
[0]))
1046 op
->literal
[3] = op
->literal
[0];
1047 op
->literal_size
= 1;
1049 case OP_PUSH_IDENTIFIER
:
1050 op
->type
= SLANG_OPER_IDENTIFIER
;
1051 op
->a_id
= parse_identifier(C
);
1052 if (op
->a_id
== SLANG_ATOM_NULL
)
1056 op
->type
= SLANG_OPER_SEQUENCE
;
1057 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1061 op
->type
= SLANG_OPER_ASSIGN
;
1062 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1066 op
->type
= SLANG_OPER_ADDASSIGN
;
1067 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1071 op
->type
= SLANG_OPER_SUBASSIGN
;
1072 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1076 op
->type
= SLANG_OPER_MULASSIGN
;
1077 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1081 op
->type
= SLANG_OPER_DIVASSIGN
;
1082 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1085 /*case OP_MODASSIGN: */
1086 /*case OP_LSHASSIGN: */
1087 /*case OP_RSHASSIGN: */
1088 /*case OP_ORASSIGN: */
1089 /*case OP_XORASSIGN: */
1090 /*case OP_ANDASSIGN: */
1092 op
->type
= SLANG_OPER_SELECT
;
1093 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1097 op
->type
= SLANG_OPER_LOGICALOR
;
1098 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1102 op
->type
= SLANG_OPER_LOGICALXOR
;
1103 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1107 op
->type
= SLANG_OPER_LOGICALAND
;
1108 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1112 /*case OP_BITXOR: */
1113 /*case OP_BITAND: */
1115 op
->type
= SLANG_OPER_EQUAL
;
1116 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1120 op
->type
= SLANG_OPER_NOTEQUAL
;
1121 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1125 op
->type
= SLANG_OPER_LESS
;
1126 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1130 op
->type
= SLANG_OPER_GREATER
;
1131 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1135 op
->type
= SLANG_OPER_LESSEQUAL
;
1136 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1139 case OP_GREATEREQUAL
:
1140 op
->type
= SLANG_OPER_GREATEREQUAL
;
1141 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1144 /*case OP_LSHIFT: */
1145 /*case OP_RSHIFT: */
1147 op
->type
= SLANG_OPER_ADD
;
1148 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1152 op
->type
= SLANG_OPER_SUBTRACT
;
1153 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1157 op
->type
= SLANG_OPER_MULTIPLY
;
1158 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1162 op
->type
= SLANG_OPER_DIVIDE
;
1163 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1166 /*case OP_MODULUS: */
1167 case OP_PREINCREMENT
:
1168 op
->type
= SLANG_OPER_PREINCREMENT
;
1169 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1172 case OP_PREDECREMENT
:
1173 op
->type
= SLANG_OPER_PREDECREMENT
;
1174 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1178 op
->type
= SLANG_OPER_PLUS
;
1179 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1183 op
->type
= SLANG_OPER_MINUS
;
1184 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1188 op
->type
= SLANG_OPER_NOT
;
1189 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1192 /*case OP_COMPLEMENT: */
1194 op
->type
= SLANG_OPER_SUBSCRIPT
;
1195 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1199 op
->type
= SLANG_OPER_CALL
;
1200 op
->a_id
= parse_identifier(C
);
1201 if (op
->a_id
== SLANG_ATOM_NULL
)
1203 while (*C
->I
!= OP_END
)
1204 if (!parse_child_operation(C
, O
, op
, 0))
1208 if (!C
->parsing_builtin
1209 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1212 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1213 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1214 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1220 op
->type
= SLANG_OPER_FIELD
;
1221 op
->a_id
= parse_identifier(C
);
1222 if (op
->a_id
== SLANG_ATOM_NULL
)
1224 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1227 case OP_POSTINCREMENT
:
1228 op
->type
= SLANG_OPER_POSTINCREMENT
;
1229 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1232 case OP_POSTDECREMENT
:
1233 op
->type
= SLANG_OPER_POSTDECREMENT
;
1234 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1243 slang_operation_destruct(oper
);
1244 *oper
= *ops
; /* struct copy */
1250 /* parameter qualifier */
1251 #define PARAM_QUALIFIER_IN 0
1252 #define PARAM_QUALIFIER_OUT 1
1253 #define PARAM_QUALIFIER_INOUT 2
1255 /* function parameter array presence */
1256 #define PARAMETER_ARRAY_NOT_PRESENT 0
1257 #define PARAMETER_ARRAY_PRESENT 1
1260 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1261 slang_variable
* param
)
1263 /* parse and validate the parameter's type qualifiers (there can be
1264 * two at most) because not all combinations are valid
1266 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1269 case PARAM_QUALIFIER_IN
:
1270 if (param
->type
.qualifier
!= SLANG_QUAL_CONST
1271 && param
->type
.qualifier
!= SLANG_QUAL_NONE
) {
1272 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1276 case PARAM_QUALIFIER_OUT
:
1277 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1278 param
->type
.qualifier
= SLANG_QUAL_OUT
;
1280 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1284 case PARAM_QUALIFIER_INOUT
:
1285 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1286 param
->type
.qualifier
= SLANG_QUAL_INOUT
;
1288 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1296 /* parse parameter's type specifier and name */
1297 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1299 param
->a_name
= parse_identifier(C
);
1300 if (param
->a_name
== SLANG_ATOM_NULL
)
1303 /* if the parameter is an array, parse its size (the size must be
1304 * explicitly defined
1306 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1307 slang_type_specifier p
;
1309 slang_type_specifier_ctr(&p
);
1310 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1311 slang_type_specifier_dtr(&p
);
1314 if (!convert_to_array(C
, param
, &p
)) {
1315 slang_type_specifier_dtr(&p
);
1318 slang_type_specifier_dtr(&p
);
1319 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1323 /* calculate the parameter size */
1324 if (!calculate_var_size(C
, O
, param
))
1327 /* TODO: allocate the local address here? */
1332 #define FUNCTION_ORDINARY 0
1333 #define FUNCTION_CONSTRUCTOR 1
1334 #define FUNCTION_OPERATOR 2
1336 /* function parameter */
1337 #define PARAMETER_NONE 0
1338 #define PARAMETER_NEXT 1
1341 #define OPERATOR_ADDASSIGN 1
1342 #define OPERATOR_SUBASSIGN 2
1343 #define OPERATOR_MULASSIGN 3
1344 #define OPERATOR_DIVASSIGN 4
1345 /*#define OPERATOR_MODASSIGN 5*/
1346 /*#define OPERATOR_LSHASSIGN 6*/
1347 /*#define OPERATOR_RSHASSIGN 7*/
1348 /*#define OPERATOR_ANDASSIGN 8*/
1349 /*#define OPERATOR_XORASSIGN 9*/
1350 /*#define OPERATOR_ORASSIGN 10*/
1351 #define OPERATOR_LOGICALXOR 11
1352 /*#define OPERATOR_BITOR 12*/
1353 /*#define OPERATOR_BITXOR 13*/
1354 /*#define OPERATOR_BITAND 14*/
1355 #define OPERATOR_LESS 15
1356 #define OPERATOR_GREATER 16
1357 #define OPERATOR_LESSEQUAL 17
1358 #define OPERATOR_GREATEREQUAL 18
1359 /*#define OPERATOR_LSHIFT 19*/
1360 /*#define OPERATOR_RSHIFT 20*/
1361 #define OPERATOR_MULTIPLY 21
1362 #define OPERATOR_DIVIDE 22
1363 /*#define OPERATOR_MODULUS 23*/
1364 #define OPERATOR_INCREMENT 24
1365 #define OPERATOR_DECREMENT 25
1366 #define OPERATOR_PLUS 26
1367 #define OPERATOR_MINUS 27
1368 /*#define OPERATOR_COMPLEMENT 28*/
1369 #define OPERATOR_NOT 29
1373 unsigned int o_code
;
1375 } operator_names
[] = {
1376 {OPERATOR_INCREMENT
, "++"},
1377 {OPERATOR_ADDASSIGN
, "+="},
1378 {OPERATOR_PLUS
, "+"},
1379 {OPERATOR_DECREMENT
, "--"},
1380 {OPERATOR_SUBASSIGN
, "-="},
1381 {OPERATOR_MINUS
, "-"},
1382 {OPERATOR_NOT
, "!"},
1383 {OPERATOR_MULASSIGN
, "*="},
1384 {OPERATOR_MULTIPLY
, "*"},
1385 {OPERATOR_DIVASSIGN
, "/="},
1386 {OPERATOR_DIVIDE
, "/"},
1387 {OPERATOR_LESSEQUAL
, "<="},
1388 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1389 /*{ OPERATOR_LSHIFT, "<<" }, */
1390 {OPERATOR_LESS
, "<"},
1391 {OPERATOR_GREATEREQUAL
, ">="},
1392 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1393 /*{ OPERATOR_RSHIFT, ">>" }, */
1394 {OPERATOR_GREATER
, ">"},
1395 /*{ OPERATOR_MODASSIGN, "%=" }, */
1396 /*{ OPERATOR_MODULUS, "%" }, */
1397 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1398 /*{ OPERATOR_BITAND, "&" }, */
1399 /*{ OPERATOR_ORASSIGN, "|=" }, */
1400 /*{ OPERATOR_BITOR, "|" }, */
1401 /*{ OPERATOR_COMPLEMENT, "~" }, */
1402 /*{ OPERATOR_XORASSIGN, "^=" }, */
1403 {OPERATOR_LOGICALXOR
, "^^"},
1404 /*{ OPERATOR_BITXOR, "^" } */
1408 parse_operator_name(slang_parse_ctx
* C
)
1412 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1413 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1415 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1416 if (atom
== SLANG_ATOM_NULL
) {
1417 slang_info_log_memory(C
->L
);
1429 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1430 slang_function
* func
)
1432 /* parse function type and name */
1433 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1436 case FUNCTION_ORDINARY
:
1437 func
->kind
= SLANG_FUNC_ORDINARY
;
1438 func
->header
.a_name
= parse_identifier(C
);
1439 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1442 case FUNCTION_CONSTRUCTOR
:
1443 func
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1444 if (func
->header
.type
.specifier
.type
== SLANG_SPEC_STRUCT
)
1446 func
->header
.a_name
=
1447 slang_atom_pool_atom(C
->atoms
,
1448 slang_type_specifier_type_to_string
1449 (func
->header
.type
.specifier
.type
));
1450 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1451 slang_info_log_memory(C
->L
);
1455 case FUNCTION_OPERATOR
:
1456 func
->kind
= SLANG_FUNC_OPERATOR
;
1457 func
->header
.a_name
= parse_operator_name(C
);
1458 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1465 if (!legal_identifier(func
->header
.a_name
)) {
1466 slang_info_log_error(C
->L
, "illegal function name '%s'",
1467 (char *) func
->header
.a_name
);
1471 /* parse function parameters */
1472 while (*C
->I
++ == PARAMETER_NEXT
) {
1473 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1475 slang_info_log_memory(C
->L
);
1478 if (!parse_parameter_declaration(C
, O
, p
))
1482 /* if the function returns a value, append a hidden __retVal 'out'
1483 * parameter that corresponds to the return value.
1485 if (_slang_function_has_return_value(func
)) {
1486 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1487 slang_atom a_retVal
= slang_atom_pool_atom(C
->atoms
, "__retVal");
1489 p
->a_name
= a_retVal
;
1490 p
->type
= func
->header
.type
;
1491 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1494 /* function formal parameters and local variables share the same
1495 * scope, so save the information about param count in a seperate
1496 * place also link the scope to the global variable scope so when a
1497 * given identifier is not found here, the search process continues
1498 * in the global space
1500 func
->param_count
= func
->parameters
->num_variables
;
1501 func
->parameters
->outer_scope
= O
->vars
;
1507 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1508 slang_function
* func
)
1510 slang_output_ctx o
= *O
;
1512 if (!parse_function_prototype(C
, O
, func
))
1515 /* create function's body operation */
1516 func
->body
= (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1517 if (func
->body
== NULL
) {
1518 slang_info_log_memory(C
->L
);
1521 if (!slang_operation_construct(func
->body
)) {
1522 _slang_free(func
->body
);
1524 slang_info_log_memory(C
->L
);
1528 /* to parse the body the parse context is modified in order to
1529 * capture parsed variables into function's local variable scope
1531 C
->global_scope
= GL_FALSE
;
1532 o
.vars
= func
->parameters
;
1533 if (!parse_statement(C
, &o
, func
->body
))
1536 C
->global_scope
= GL_TRUE
;
1541 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1543 slang_operation op_id
, op_assign
;
1546 /* construct the left side of assignment */
1547 if (!slang_operation_construct(&op_id
))
1549 op_id
.type
= SLANG_OPER_IDENTIFIER
;
1550 op_id
.a_id
= var
->a_name
;
1552 /* put the variable into operation's scope */
1553 op_id
.locals
->variables
=
1554 (slang_variable
**) _slang_alloc(sizeof(slang_variable
*));
1555 if (op_id
.locals
->variables
== NULL
) {
1556 slang_operation_destruct(&op_id
);
1559 op_id
.locals
->num_variables
= 1;
1560 op_id
.locals
->variables
[0] = var
;
1562 /* construct the assignment expression */
1563 if (!slang_operation_construct(&op_assign
)) {
1564 op_id
.locals
->num_variables
= 0;
1565 slang_operation_destruct(&op_id
);
1568 op_assign
.type
= SLANG_OPER_ASSIGN
;
1569 op_assign
.children
=
1570 (slang_operation
*) _slang_alloc(2 * sizeof(slang_operation
));
1571 if (op_assign
.children
== NULL
) {
1572 slang_operation_destruct(&op_assign
);
1573 op_id
.locals
->num_variables
= 0;
1574 slang_operation_destruct(&op_id
);
1577 op_assign
.num_children
= 2;
1578 op_assign
.children
[0] = op_id
;
1579 op_assign
.children
[1] = *var
->initializer
;
1583 /* carefully destroy the operations */
1584 op_assign
.num_children
= 0;
1585 _slang_free(op_assign
.children
);
1586 op_assign
.children
= NULL
;
1587 slang_operation_destruct(&op_assign
);
1588 op_id
.locals
->num_variables
= 0;
1589 slang_operation_destruct(&op_id
);
1597 /* init declarator list */
1598 #define DECLARATOR_NONE 0
1599 #define DECLARATOR_NEXT 1
1601 /* variable declaration */
1602 #define VARIABLE_NONE 0
1603 #define VARIABLE_IDENTIFIER 1
1604 #define VARIABLE_INITIALIZER 2
1605 #define VARIABLE_ARRAY_EXPLICIT 3
1606 #define VARIABLE_ARRAY_UNKNOWN 4
1610 * Parse the initializer for a variable declaration.
1613 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1614 const slang_fully_specified_type
* type
)
1616 slang_variable
*var
;
1619 /* empty init declatator (without name, e.g. "float ;") */
1620 if (*C
->I
++ == VARIABLE_NONE
)
1623 a_name
= parse_identifier(C
);
1625 /* check if name is already in this scope */
1626 if (_slang_locate_variable(O
->vars
, a_name
, GL_FALSE
)) {
1627 slang_info_log_error(C
->L
,
1628 "declaration of '%s' conflicts with previous declaration",
1633 /* make room for the new variable and initialize it */
1634 var
= slang_variable_scope_grow(O
->vars
);
1636 slang_info_log_memory(C
->L
);
1640 /* copy the declarator qualifier type, parse the identifier */
1641 var
->type
.qualifier
= type
->qualifier
;
1642 var
->a_name
= a_name
;
1643 if (var
->a_name
== SLANG_ATOM_NULL
)
1648 /* simple variable declarator - just copy the specifier */
1649 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1652 case VARIABLE_INITIALIZER
:
1653 /* initialized variable - copy the specifier and parse the expression */
1654 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1657 (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1658 if (var
->initializer
== NULL
) {
1659 slang_info_log_memory(C
->L
);
1662 if (!slang_operation_construct(var
->initializer
)) {
1663 _slang_free(var
->initializer
);
1664 var
->initializer
= NULL
;
1665 slang_info_log_memory(C
->L
);
1668 if (!parse_expression(C
, O
, var
->initializer
))
1671 case VARIABLE_ARRAY_UNKNOWN
:
1672 /* unsized array - mark it as array and copy the specifier to
1675 if (!convert_to_array(C
, var
, &type
->specifier
))
1678 case VARIABLE_ARRAY_EXPLICIT
:
1679 if (!convert_to_array(C
, var
, &type
->specifier
))
1681 if (!parse_array_len(C
, O
, &var
->array_len
))
1688 /* allocate global address space for a variable with a known size */
1690 && !(var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
1691 && var
->array_len
== 0)) {
1692 if (!calculate_var_size(C
, O
, var
))
1694 var
->address
= slang_var_pool_alloc(O
->global_pool
, var
->size
);
1697 /* emit code for global var decl */
1698 if (C
->global_scope
) {
1699 slang_assemble_ctx A
;
1701 A
.space
.funcs
= O
->funs
;
1702 A
.space
.structs
= O
->structs
;
1703 A
.space
.vars
= O
->vars
;
1704 A
.program
= O
->program
;
1705 A
.vartable
= O
->vartable
;
1707 A
.curFuncEndLabel
= NULL
;
1708 if (!_slang_codegen_global_variable(&A
, var
, C
->type
))
1712 /* initialize global variable */
1713 if (C
->global_scope
) {
1714 if (var
->initializer
!= NULL
) {
1715 slang_assemble_ctx A
;
1718 A
.space
.funcs
= O
->funs
;
1719 A
.space
.structs
= O
->structs
;
1720 A
.space
.vars
= O
->vars
;
1721 if (!initialize_global(&A
, var
))
1729 * Parse a list of variable declarations. Each variable may have an
1733 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1735 slang_fully_specified_type type
;
1737 /* parse the fully specified type, common to all declarators */
1738 if (!slang_fully_specified_type_construct(&type
))
1740 if (!parse_fully_specified_type(C
, O
, &type
)) {
1741 slang_fully_specified_type_destruct(&type
);
1745 /* parse declarators, pass-in the parsed type */
1747 if (!parse_init_declarator(C
, O
, &type
)) {
1748 slang_fully_specified_type_destruct(&type
);
1752 while (*C
->I
++ == DECLARATOR_NEXT
);
1754 slang_fully_specified_type_destruct(&type
);
1760 * Parse a function definition or declaration.
1761 * \param C parsing context
1762 * \param O output context
1763 * \param definition if non-zero expect a definition, else a declaration
1764 * \param parsed_func_ret returns the parsed function
1765 * \return GL_TRUE if success, GL_FALSE if failure
1768 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
1769 slang_function
** parsed_func_ret
)
1771 slang_function parsed_func
, *found_func
;
1773 /* parse function definition/declaration */
1774 if (!slang_function_construct(&parsed_func
))
1777 if (!parse_function_definition(C
, O
, &parsed_func
)) {
1778 slang_function_destruct(&parsed_func
);
1783 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
1784 slang_function_destruct(&parsed_func
);
1789 /* find a function with a prototype matching the parsed one - only
1790 * the current scope is being searched to allow built-in function
1793 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
1794 if (found_func
== NULL
) {
1795 /* New function, add it to the function list */
1796 O
->funs
->functions
=
1797 (slang_function
*) _slang_realloc(O
->funs
->functions
,
1798 O
->funs
->num_functions
1799 * sizeof(slang_function
),
1800 (O
->funs
->num_functions
+ 1)
1801 * sizeof(slang_function
));
1802 if (O
->funs
->functions
== NULL
) {
1803 slang_info_log_memory(C
->L
);
1804 slang_function_destruct(&parsed_func
);
1807 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
1808 O
->funs
->num_functions
++;
1810 /* return the newly parsed function */
1811 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
1814 /* previously defined or declared */
1815 /* TODO: check function return type qualifiers and specifiers */
1817 if (found_func
->body
!= NULL
) {
1818 slang_info_log_error(C
->L
, "%s: function already has a body.",
1819 slang_atom_pool_id(C
->atoms
,
1822 slang_function_destruct(&parsed_func
);
1826 /* destroy the existing function declaration and replace it
1827 * with the new one, remember to save the fixup table
1829 parsed_func
.fixups
= found_func
->fixups
;
1830 slang_fixup_table_init(&found_func
->fixups
);
1831 slang_function_destruct(found_func
);
1832 *found_func
= parsed_func
;
1835 /* another declaration of the same function prototype - ignore it */
1836 slang_function_destruct(&parsed_func
);
1839 /* return the found function */
1840 *parsed_func_ret
= found_func
;
1847 #define DECLARATION_FUNCTION_PROTOTYPE 1
1848 #define DECLARATION_INIT_DECLARATOR_LIST 2
1851 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1854 case DECLARATION_INIT_DECLARATOR_LIST
:
1855 if (!parse_init_declarator_list(C
, O
))
1858 case DECLARATION_FUNCTION_PROTOTYPE
:
1860 slang_function
*dummy_func
;
1862 if (!parse_function(C
, O
, 0, &dummy_func
))
1873 #define PRECISION_LOW 0
1874 #define PRECISION_MEDIUM 1
1875 #define PRECISION_HIGH 2
1878 parse_default_precision(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1880 #if FEATURE_es2_glsl
1881 int precision
, type
;
1883 precision
= *C
->I
++;
1884 switch (precision
) {
1886 case PRECISION_MEDIUM
:
1887 case PRECISION_HIGH
:
1891 _mesa_problem(NULL
, "unexpected precision %d at %s:%d\n",
1892 precision
, __FILE__
, __LINE__
);
1898 case TYPE_SPECIFIER_FLOAT
:
1899 case TYPE_SPECIFIER_INT
:
1900 case TYPE_SPECIFIER_SAMPLER1D
:
1901 case TYPE_SPECIFIER_SAMPLER2D
:
1902 case TYPE_SPECIFIER_SAMPLER3D
:
1903 case TYPE_SPECIFIER_SAMPLERCUBE
:
1904 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
1905 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
1906 case TYPE_SPECIFIER_SAMPLER2DRECT
:
1907 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
1911 _mesa_problem(NULL
, "unexpected type %d at %s:%d\n",
1912 type
, __FILE__
, __LINE__
);
1916 assert(type
< TYPE_SPECIFIER_COUNT
);
1917 O
->default_precision
[type
] = precision
;
1921 slang_info_log_error(C
->L
, "syntax error at \"precision\"");
1928 * Initialize the default precision for all types.
1929 * XXX this info isn't used yet.
1932 init_default_precision(slang_output_ctx
*O
, slang_unit_type type
)
1935 for (i
= 0; i
< TYPE_SPECIFIER_COUNT
; i
++) {
1936 #if FEATURE_es2_glsl
1937 O
->default_precision
[i
] = PRECISION_LOW
;
1939 O
->default_precision
[i
] = PRECISION_HIGH
;
1942 #if FEATURE_es2_glsl
1943 if (type
== SLANG_UNIT_VERTEX_SHADER
) {
1944 O
->default_precision
[TYPE_SPECIFIER_FLOAT
] = PRECISION_HIGH
;
1945 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_HIGH
;
1948 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_MEDIUM
;
1955 parse_invariant(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1957 if (C
->version
>= 120 || FEATURE_es2_glsl
) {
1958 slang_atom
*a
= parse_identifier(C
);
1959 /* XXX not doing anything with this var yet */
1960 /*printf("ID: %s\n", (char*) a);*/
1964 slang_info_log_error(C
->L
, "syntax error at \"invariant\"");
1970 /* external declaration or default precision specifier */
1971 #define EXTERNAL_NULL 0
1972 #define EXTERNAL_FUNCTION_DEFINITION 1
1973 #define EXTERNAL_DECLARATION 2
1974 #define DEFAULT_PRECISION 3
1975 #define INVARIANT_STMT 4
1979 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
,
1980 struct gl_shader
*shader
)
1982 GET_CURRENT_CONTEXT(ctx
);
1986 slang_function
*mainFunc
= NULL
;
1988 if (unit
->type
== SLANG_UNIT_FRAGMENT_BUILTIN
||
1989 unit
->type
== SLANG_UNIT_FRAGMENT_SHADER
) {
1990 maxRegs
= ctx
->Const
.FragmentProgram
.MaxTemps
;
1993 assert(unit
->type
== SLANG_UNIT_VERTEX_BUILTIN
||
1994 unit
->type
== SLANG_UNIT_VERTEX_SHADER
);
1995 maxRegs
= ctx
->Const
.VertexProgram
.MaxTemps
;
1998 /* setup output context */
1999 init_default_precision(&o
, unit
->type
);
2000 o
.funs
= &unit
->funs
;
2001 o
.structs
= &unit
->structs
;
2002 o
.vars
= &unit
->vars
;
2003 o
.global_pool
= &unit
->object
->varpool
;
2004 o
.program
= shader
? shader
->Program
: NULL
;
2005 o
.vartable
= _slang_new_var_table(maxRegs
);
2006 _slang_push_var_table(o
.vartable
);
2008 /* parse individual functions and declarations */
2009 while (*C
->I
!= EXTERNAL_NULL
) {
2011 case EXTERNAL_FUNCTION_DEFINITION
:
2013 slang_function
*func
;
2014 success
= parse_function(C
, &o
, 1, &func
);
2016 _mesa_strcmp((char *) func
->header
.a_name
, "main") == 0) {
2022 case EXTERNAL_DECLARATION
:
2023 success
= parse_declaration(C
, &o
);
2025 case DEFAULT_PRECISION
:
2026 success
= parse_default_precision(C
, &o
);
2028 case INVARIANT_STMT
:
2029 success
= parse_invariant(C
, &o
);
2036 /* xxx free codegen */
2037 _slang_pop_var_table(o
.vartable
);
2044 /* assemble (generate code) for main() */
2045 slang_assemble_ctx A
;
2048 A
.space
.funcs
= o
.funs
;
2049 A
.space
.structs
= o
.structs
;
2050 A
.space
.vars
= o
.vars
;
2051 A
.program
= o
.program
;
2052 A
.vartable
= o
.vartable
;
2055 /* main() takes no parameters */
2056 if (mainFunc
->param_count
> 0) {
2057 slang_info_log_error(A
.log
, "main() takes no arguments");
2061 _slang_codegen_function(&A
, mainFunc
);
2063 shader
->Main
= GL_TRUE
; /* this shader defines main() */
2066 _slang_pop_var_table(o
.vartable
);
2067 _slang_delete_var_table(o
.vartable
);
2073 compile_binary(const byte
* prod
, slang_code_unit
* unit
,
2075 slang_unit_type type
, slang_info_log
* infolog
,
2076 slang_code_unit
* builtin
, slang_code_unit
* downlink
,
2077 struct gl_shader
*shader
)
2083 /* setup parse context */
2086 C
.parsing_builtin
= (builtin
== NULL
);
2087 C
.global_scope
= GL_TRUE
;
2088 C
.atoms
= &unit
->object
->atompool
;
2090 C
.version
= version
;
2092 if (!check_revision(&C
))
2095 if (downlink
!= NULL
) {
2096 unit
->vars
.outer_scope
= &downlink
->vars
;
2097 unit
->funs
.outer_scope
= &downlink
->funs
;
2098 unit
->structs
.outer_scope
= &downlink
->structs
;
2101 /* parse translation unit */
2102 return parse_code_unit(&C
, unit
, shader
);
2106 compile_with_grammar(grammar id
, const char *source
, slang_code_unit
* unit
,
2107 slang_unit_type type
, slang_info_log
* infolog
,
2108 slang_code_unit
* builtin
,
2109 struct gl_shader
*shader
)
2112 GLuint size
, start
, version
;
2113 slang_string preprocessed
;
2116 #if FEATURE_ARB_shading_language_120
2118 #elif FEATURE_es2_glsl
2124 /* First retrieve the version number. */
2125 if (!_slang_preprocess_version(source
, &version
, &start
, infolog
))
2128 if (version
> maxVersion
) {
2129 slang_info_log_error(infolog
,
2130 "language version %.2f is not supported.",
2135 /* Now preprocess the source string. */
2136 slang_string_init(&preprocessed
);
2137 if (!_slang_preprocess_directives(&preprocessed
, &source
[start
], infolog
)) {
2138 slang_string_free(&preprocessed
);
2139 slang_info_log_error(infolog
, "failed to preprocess the source.");
2143 /* Finally check the syntax and generate its binary representation. */
2144 if (!grammar_fast_check(id
,
2145 (const byte
*) (slang_string_cstr(&preprocessed
)),
2146 &prod
, &size
, 65536)) {
2150 slang_string_free(&preprocessed
);
2151 grammar_get_last_error((byte
*) (buf
), sizeof(buf
), &pos
);
2152 slang_info_log_error(infolog
, buf
);
2153 /* syntax error (possibly in library code) */
2158 s
= (char *) _mesa_find_line_column((const GLubyte
*) source
,
2159 (const GLubyte
*) source
+ pos
,
2161 printf("Error on line %d, col %d: %s\n", line
, col
, s
);
2166 slang_string_free(&preprocessed
);
2168 /* Syntax is okay - translate it to internal representation. */
2169 if (!compile_binary(prod
, unit
, version
, type
, infolog
, builtin
,
2170 &builtin
[SLANG_BUILTIN_TOTAL
- 1],
2172 grammar_alloc_free(prod
);
2175 grammar_alloc_free(prod
);
2179 LONGSTRING
static const char *slang_shader_syn
=
2180 #include "library/slang_shader_syn.h"
2183 static const byte slang_core_gc
[] = {
2184 #include "library/slang_core_gc.h"
2187 static const byte slang_120_core_gc
[] = {
2188 #include "library/slang_120_core_gc.h"
2191 static const byte slang_120_fragment_gc
[] = {
2192 #include "library/slang_builtin_120_fragment_gc.h"
2195 static const byte slang_common_builtin_gc
[] = {
2196 #include "library/slang_common_builtin_gc.h"
2199 static const byte slang_fragment_builtin_gc
[] = {
2200 #include "library/slang_fragment_builtin_gc.h"
2203 static const byte slang_vertex_builtin_gc
[] = {
2204 #include "library/slang_vertex_builtin_gc.h"
2208 compile_object(grammar
* id
, const char *source
, slang_code_object
* object
,
2209 slang_unit_type type
, slang_info_log
* infolog
,
2210 struct gl_shader
*shader
)
2212 slang_code_unit
*builtins
= NULL
;
2213 GLuint base_version
= 110;
2215 /* load GLSL grammar */
2216 *id
= grammar_load_from_text((const byte
*) (slang_shader_syn
));
2221 grammar_get_last_error(buf
, 1024, &pos
);
2222 slang_info_log_error(infolog
, (const char *) (buf
));
2226 /* set shader type - the syntax is slightly different for different shaders */
2227 if (type
== SLANG_UNIT_FRAGMENT_SHADER
2228 || type
== SLANG_UNIT_FRAGMENT_BUILTIN
)
2229 grammar_set_reg8(*id
, (const byte
*) "shader_type", 1);
2231 grammar_set_reg8(*id
, (const byte
*) "shader_type", 2);
2233 /* enable language extensions */
2234 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2236 /* if parsing user-specified shader, load built-in library */
2237 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_VERTEX_SHADER
) {
2238 /* compile core functionality first */
2239 if (!compile_binary(slang_core_gc
,
2240 &object
->builtin
[SLANG_BUILTIN_CORE
],
2242 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2246 #if FEATURE_ARB_shading_language_120
2247 if (!compile_binary(slang_120_core_gc
,
2248 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2250 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2251 NULL
, &object
->builtin
[SLANG_BUILTIN_CORE
], NULL
))
2255 /* compile common functions and variables, link to core */
2256 if (!compile_binary(slang_common_builtin_gc
,
2257 &object
->builtin
[SLANG_BUILTIN_COMMON
],
2258 #if FEATURE_ARB_shading_language_120
2263 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2264 #if FEATURE_ARB_shading_language_120
2265 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2267 &object
->builtin
[SLANG_BUILTIN_CORE
],
2272 /* compile target-specific functions and variables, link to common */
2273 if (type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2274 if (!compile_binary(slang_fragment_builtin_gc
,
2275 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2277 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2278 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2280 #if FEATURE_ARB_shading_language_120
2281 if (!compile_binary(slang_120_fragment_gc
,
2282 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2284 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2285 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2289 else if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2290 if (!compile_binary(slang_vertex_builtin_gc
,
2291 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2293 SLANG_UNIT_VERTEX_BUILTIN
, infolog
, NULL
,
2294 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2298 /* disable language extensions */
2299 #if NEW_SLANG /* allow-built-ins */
2300 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2302 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 0);
2304 builtins
= object
->builtin
;
2307 /* compile the actual shader - pass-in built-in library for external shader */
2308 return compile_with_grammar(*id
, source
, &object
->unit
, type
, infolog
,
2314 compile_shader(GLcontext
*ctx
, slang_code_object
* object
,
2315 slang_unit_type type
, slang_info_log
* infolog
,
2316 struct gl_shader
*shader
)
2321 #if 0 /* for debug */
2322 _mesa_printf("********* COMPILE SHADER ***********\n");
2323 _mesa_printf("%s\n", shader
->Source
);
2324 _mesa_printf("************************************\n");
2327 assert(shader
->Program
);
2329 _slang_code_object_dtr(object
);
2330 _slang_code_object_ctr(object
);
2332 success
= compile_object(&id
, shader
->Source
, object
, type
, infolog
, shader
);
2334 grammar_destroy(id
);
2344 _slang_compile(GLcontext
*ctx
, struct gl_shader
*shader
)
2347 slang_info_log info_log
;
2348 slang_code_object obj
;
2349 slang_unit_type type
;
2351 if (shader
->Type
== GL_VERTEX_SHADER
) {
2352 type
= SLANG_UNIT_VERTEX_SHADER
;
2355 assert(shader
->Type
== GL_FRAGMENT_SHADER
);
2356 type
= SLANG_UNIT_FRAGMENT_SHADER
;
2359 if (!shader
->Source
)
2362 ctx
->Shader
.MemPool
= _slang_new_mempool(1024*1024);
2364 shader
->Main
= GL_FALSE
;
2366 if (!shader
->Program
) {
2368 if (shader
->Type
== GL_VERTEX_SHADER
)
2369 progTarget
= GL_VERTEX_PROGRAM_ARB
;
2371 progTarget
= GL_FRAGMENT_PROGRAM_ARB
;
2372 shader
->Program
= ctx
->Driver
.NewProgram(ctx
, progTarget
, 1);
2373 shader
->Program
->Parameters
= _mesa_new_parameter_list();
2374 shader
->Program
->Varying
= _mesa_new_parameter_list();
2375 shader
->Program
->Attributes
= _mesa_new_parameter_list();
2378 slang_info_log_construct(&info_log
);
2379 _slang_code_object_ctr(&obj
);
2381 success
= compile_shader(ctx
, &obj
, type
, &info_log
, shader
);
2383 /* free shader's prev info log */
2384 if (shader
->InfoLog
) {
2385 _mesa_free(shader
->InfoLog
);
2386 shader
->InfoLog
= NULL
;
2389 if (info_log
.text
) {
2390 /* copy info-log string to shader object */
2391 shader
->InfoLog
= _mesa_strdup(info_log
.text
);
2394 if (info_log
.error_flag
) {
2398 slang_info_log_destruct(&info_log
);
2399 _slang_code_object_dtr(&obj
);
2401 _slang_delete_mempool((slang_mempool
*) ctx
->Shader
.MemPool
);
2402 ctx
->Shader
.MemPool
= NULL
;
2404 if (shader
->Type
== GL_VERTEX_SHADER
) {
2405 /* remove any reads of varying (output) registers */
2407 printf("Pre-remove output reads:\n");
2408 _mesa_print_program(shader
->Programs
);
2410 _mesa_remove_output_reads(shader
->Program
, PROGRAM_VARYING
);
2411 _mesa_remove_output_reads(shader
->Program
, PROGRAM_OUTPUT
);
2413 printf("Post-remove output reads:\n");
2414 _mesa_print_program(shader
->Programs
);