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
))
694 #define PRECISION_DEFAULT 0
695 #define PRECISION_LOW 1
696 #define PRECISION_MEDIUM 2
697 #define PRECISION_HIGH 3
700 parse_fully_specified_type(slang_parse_ctx
* C
, slang_output_ctx
* O
,
701 slang_fully_specified_type
* type
)
705 if (!parse_type_qualifier(C
, &type
->qualifier
))
708 if (!parse_type_specifier(C
, O
, &type
->specifier
))
712 case PRECISION_DEFAULT
:
713 assert(type
->specifier
.type
< TYPE_SPECIFIER_COUNT
);
714 if (type
->specifier
.type
< TYPE_SPECIFIER_COUNT
)
715 type
->precision
= O
->default_precision
[type
->specifier
.type
];
718 type
->precision
= SLANG_PREC_LOW
;
720 case PRECISION_MEDIUM
:
721 type
->precision
= SLANG_PREC_MEDIUM
;
724 type
->precision
= SLANG_PREC_HIGH
;
730 #if !FEATURE_es2_glsl
731 if (precision
!= PRECISION_DEFAULT
) {
732 slang_info_log_error(C
->L
, "precision qualifiers not allowed");
742 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
743 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
747 #define OP_CONTINUE 6
750 #define OP_EXPRESSION 9
755 #define OP_PUSH_VOID 14
756 #define OP_PUSH_BOOL 15
757 #define OP_PUSH_INT 16
758 #define OP_PUSH_FLOAT 17
759 #define OP_PUSH_IDENTIFIER 18
760 #define OP_SEQUENCE 19
762 #define OP_ADDASSIGN 21
763 #define OP_SUBASSIGN 22
764 #define OP_MULASSIGN 23
765 #define OP_DIVASSIGN 24
766 /*#define OP_MODASSIGN 25*/
767 /*#define OP_LSHASSIGN 26*/
768 /*#define OP_RSHASSIGN 27*/
769 /*#define OP_ORASSIGN 28*/
770 /*#define OP_XORASSIGN 29*/
771 /*#define OP_ANDASSIGN 30*/
773 #define OP_LOGICALOR 32
774 #define OP_LOGICALXOR 33
775 #define OP_LOGICALAND 34
776 /*#define OP_BITOR 35*/
777 /*#define OP_BITXOR 36*/
778 /*#define OP_BITAND 37*/
780 #define OP_NOTEQUAL 39
782 #define OP_GREATER 41
783 #define OP_LESSEQUAL 42
784 #define OP_GREATEREQUAL 43
785 /*#define OP_LSHIFT 44*/
786 /*#define OP_RSHIFT 45*/
788 #define OP_SUBTRACT 47
789 #define OP_MULTIPLY 48
791 /*#define OP_MODULUS 50*/
792 #define OP_PREINCREMENT 51
793 #define OP_PREDECREMENT 52
796 /*#define OP_COMPLEMENT 55*/
798 #define OP_SUBSCRIPT 57
801 #define OP_POSTINCREMENT 60
802 #define OP_POSTDECREMENT 61
803 #define OP_PRECISION 62
807 * When parsing a compound production, this function is used to parse the
809 * For example, a while-loop compound will have two children, the
810 * while condition expression and the loop body. So, this function will
811 * be called twice to parse those two sub-expressions.
812 * \param C the parsing context
813 * \param O the output context
814 * \param oper the operation we're parsing
815 * \param statement indicates whether parsing a statement, or expression
816 * \return 1 if success, 0 if error
819 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
820 slang_operation
* oper
, GLboolean statement
)
824 /* grow child array */
825 ch
= slang_operation_grow(&oper
->num_children
, &oper
->children
);
827 return parse_statement(C
, O
, ch
);
828 return parse_expression(C
, O
, ch
);
831 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
834 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
835 slang_operation
* oper
)
837 oper
->locals
->outer_scope
= O
->vars
;
839 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
840 /* parse child statements, do not create new variable scope */
841 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
842 while (*C
->I
!= OP_END
)
843 if (!parse_child_operation(C
, O
, oper
, 1))
847 case OP_BLOCK_BEGIN_NEW_SCOPE
:
848 /* parse child statements, create new variable scope */
850 slang_output_ctx o
= *O
;
852 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
853 o
.vars
= oper
->locals
;
854 while (*C
->I
!= OP_END
)
855 if (!parse_child_operation(C
, &o
, oper
, 1))
861 /* local variable declaration, individual declarators are stored as
862 * children identifiers
864 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
866 const unsigned int first_var
= O
->vars
->num_variables
;
868 /* parse the declaration, note that there can be zero or more
869 * than one declarators
871 if (!parse_declaration(C
, O
))
873 if (first_var
< O
->vars
->num_variables
) {
874 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
876 assert(oper
->num_children
== 0);
877 oper
->num_children
= num_vars
;
878 oper
->children
= slang_operation_new(num_vars
);
879 if (oper
->children
== NULL
) {
880 slang_info_log_memory(C
->L
);
883 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
884 slang_operation
*o
= &oper
->children
[i
- first_var
];
885 o
->type
= SLANG_OPER_VARIABLE_DECL
;
886 o
->locals
->outer_scope
= O
->vars
;
887 o
->a_id
= O
->vars
->variables
[i
]->a_name
;
889 if (!legal_identifier(o
->a_id
)) {
890 slang_info_log_error(C
->L
, "illegal variable name '%s'",
899 /* the __asm statement, parse the mnemonic and all its arguments
902 oper
->type
= SLANG_OPER_ASM
;
903 oper
->a_id
= parse_identifier(C
);
904 if (oper
->a_id
== SLANG_ATOM_NULL
)
906 while (*C
->I
!= OP_END
) {
907 if (!parse_child_operation(C
, O
, oper
, 0))
913 oper
->type
= SLANG_OPER_BREAK
;
916 oper
->type
= SLANG_OPER_CONTINUE
;
919 oper
->type
= SLANG_OPER_DISCARD
;
922 oper
->type
= SLANG_OPER_RETURN
;
923 if (!parse_child_operation(C
, O
, oper
, 0))
927 oper
->type
= SLANG_OPER_EXPRESSION
;
928 if (!parse_child_operation(C
, O
, oper
, 0))
932 oper
->type
= SLANG_OPER_IF
;
933 if (!parse_child_operation(C
, O
, oper
, 0))
935 if (!parse_child_operation(C
, O
, oper
, 1))
937 if (!parse_child_operation(C
, O
, oper
, 1))
942 slang_output_ctx o
= *O
;
944 oper
->type
= SLANG_OPER_WHILE
;
945 o
.vars
= oper
->locals
;
946 if (!parse_child_operation(C
, &o
, oper
, 1))
948 if (!parse_child_operation(C
, &o
, oper
, 1))
953 oper
->type
= SLANG_OPER_DO
;
954 if (!parse_child_operation(C
, O
, oper
, 1))
956 if (!parse_child_operation(C
, O
, oper
, 0))
961 slang_output_ctx o
= *O
;
963 oper
->type
= SLANG_OPER_FOR
;
964 o
.vars
= oper
->locals
;
965 if (!parse_child_operation(C
, &o
, oper
, 1))
967 if (!parse_child_operation(C
, &o
, oper
, 1))
969 if (!parse_child_operation(C
, &o
, oper
, 0))
971 if (!parse_child_operation(C
, &o
, oper
, 1))
977 /* set default precision for a type in this scope */
978 /* ignored at this time */
979 int prec_qual
= *C
->I
++;
980 int datatype
= *C
->I
++;
992 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
993 slang_operation
** ops
, unsigned int *total_ops
,
998 op
->children
= slang_operation_new(n
);
999 if (op
->children
== NULL
) {
1000 slang_info_log_memory(C
->L
);
1003 op
->num_children
= n
;
1005 for (i
= 0; i
< n
; i
++) {
1006 slang_operation_destruct(&op
->children
[i
]);
1007 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
1010 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
1013 *ops
= (slang_operation
*)
1014 _slang_realloc(*ops
,
1015 (*total_ops
+ n
) * sizeof(slang_operation
),
1016 *total_ops
* sizeof(slang_operation
));
1018 slang_info_log_memory(C
->L
);
1025 is_constructor_name(const char *name
, slang_atom a_name
,
1026 slang_struct_scope
* structs
)
1028 if (slang_type_specifier_type_from_string(name
) != SLANG_SPEC_VOID
)
1030 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
1034 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1035 slang_operation
* oper
)
1037 slang_operation
*ops
= NULL
;
1038 unsigned int num_ops
= 0;
1041 while (*C
->I
!= OP_END
) {
1042 slang_operation
*op
;
1043 const unsigned int op_code
= *C
->I
++;
1045 /* allocate default operation, becomes a no-op if not used */
1046 ops
= (slang_operation
*)
1048 num_ops
* sizeof(slang_operation
),
1049 (num_ops
+ 1) * sizeof(slang_operation
));
1051 slang_info_log_memory(C
->L
);
1055 if (!slang_operation_construct(op
)) {
1056 slang_info_log_memory(C
->L
);
1060 op
->locals
->outer_scope
= O
->vars
;
1064 op
->type
= SLANG_OPER_VOID
;
1067 op
->type
= SLANG_OPER_LITERAL_BOOL
;
1068 if (!parse_number(C
, &number
))
1073 op
->literal
[3] = (GLfloat
) number
;
1074 op
->literal_size
= 1;
1077 op
->type
= SLANG_OPER_LITERAL_INT
;
1078 if (!parse_number(C
, &number
))
1083 op
->literal
[3] = (GLfloat
) number
;
1084 op
->literal_size
= 1;
1087 op
->type
= SLANG_OPER_LITERAL_FLOAT
;
1088 if (!parse_float(C
, &op
->literal
[0]))
1092 op
->literal
[3] = op
->literal
[0];
1093 op
->literal_size
= 1;
1095 case OP_PUSH_IDENTIFIER
:
1096 op
->type
= SLANG_OPER_IDENTIFIER
;
1097 op
->a_id
= parse_identifier(C
);
1098 if (op
->a_id
== SLANG_ATOM_NULL
)
1102 op
->type
= SLANG_OPER_SEQUENCE
;
1103 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1107 op
->type
= SLANG_OPER_ASSIGN
;
1108 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1112 op
->type
= SLANG_OPER_ADDASSIGN
;
1113 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1117 op
->type
= SLANG_OPER_SUBASSIGN
;
1118 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1122 op
->type
= SLANG_OPER_MULASSIGN
;
1123 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1127 op
->type
= SLANG_OPER_DIVASSIGN
;
1128 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1131 /*case OP_MODASSIGN: */
1132 /*case OP_LSHASSIGN: */
1133 /*case OP_RSHASSIGN: */
1134 /*case OP_ORASSIGN: */
1135 /*case OP_XORASSIGN: */
1136 /*case OP_ANDASSIGN: */
1138 op
->type
= SLANG_OPER_SELECT
;
1139 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1143 op
->type
= SLANG_OPER_LOGICALOR
;
1144 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1148 op
->type
= SLANG_OPER_LOGICALXOR
;
1149 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1153 op
->type
= SLANG_OPER_LOGICALAND
;
1154 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1158 /*case OP_BITXOR: */
1159 /*case OP_BITAND: */
1161 op
->type
= SLANG_OPER_EQUAL
;
1162 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1166 op
->type
= SLANG_OPER_NOTEQUAL
;
1167 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1171 op
->type
= SLANG_OPER_LESS
;
1172 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1176 op
->type
= SLANG_OPER_GREATER
;
1177 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1181 op
->type
= SLANG_OPER_LESSEQUAL
;
1182 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1185 case OP_GREATEREQUAL
:
1186 op
->type
= SLANG_OPER_GREATEREQUAL
;
1187 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1190 /*case OP_LSHIFT: */
1191 /*case OP_RSHIFT: */
1193 op
->type
= SLANG_OPER_ADD
;
1194 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1198 op
->type
= SLANG_OPER_SUBTRACT
;
1199 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1203 op
->type
= SLANG_OPER_MULTIPLY
;
1204 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1208 op
->type
= SLANG_OPER_DIVIDE
;
1209 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1212 /*case OP_MODULUS: */
1213 case OP_PREINCREMENT
:
1214 op
->type
= SLANG_OPER_PREINCREMENT
;
1215 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1218 case OP_PREDECREMENT
:
1219 op
->type
= SLANG_OPER_PREDECREMENT
;
1220 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1224 op
->type
= SLANG_OPER_PLUS
;
1225 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1229 op
->type
= SLANG_OPER_MINUS
;
1230 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1234 op
->type
= SLANG_OPER_NOT
;
1235 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1238 /*case OP_COMPLEMENT: */
1240 op
->type
= SLANG_OPER_SUBSCRIPT
;
1241 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1245 op
->type
= SLANG_OPER_CALL
;
1246 op
->a_id
= parse_identifier(C
);
1247 if (op
->a_id
== SLANG_ATOM_NULL
)
1249 while (*C
->I
!= OP_END
)
1250 if (!parse_child_operation(C
, O
, op
, 0))
1254 if (!C
->parsing_builtin
1255 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1258 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1259 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1260 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1266 op
->type
= SLANG_OPER_FIELD
;
1267 op
->a_id
= parse_identifier(C
);
1268 if (op
->a_id
== SLANG_ATOM_NULL
)
1270 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1273 case OP_POSTINCREMENT
:
1274 op
->type
= SLANG_OPER_POSTINCREMENT
;
1275 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1278 case OP_POSTDECREMENT
:
1279 op
->type
= SLANG_OPER_POSTDECREMENT
;
1280 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1289 slang_operation_destruct(oper
);
1290 *oper
= *ops
; /* struct copy */
1296 /* parameter qualifier */
1297 #define PARAM_QUALIFIER_IN 0
1298 #define PARAM_QUALIFIER_OUT 1
1299 #define PARAM_QUALIFIER_INOUT 2
1301 /* function parameter array presence */
1302 #define PARAMETER_ARRAY_NOT_PRESENT 0
1303 #define PARAMETER_ARRAY_PRESENT 1
1306 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1307 slang_variable
* param
)
1309 int param_qual
, precision_qual
;
1311 /* parse and validate the parameter's type qualifiers (there can be
1312 * two at most) because not all combinations are valid
1314 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1317 param_qual
= *C
->I
++;
1318 switch (param_qual
) {
1319 case PARAM_QUALIFIER_IN
:
1320 if (param
->type
.qualifier
!= SLANG_QUAL_CONST
1321 && param
->type
.qualifier
!= SLANG_QUAL_NONE
) {
1322 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1326 case PARAM_QUALIFIER_OUT
:
1327 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1328 param
->type
.qualifier
= SLANG_QUAL_OUT
;
1330 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1334 case PARAM_QUALIFIER_INOUT
:
1335 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1336 param
->type
.qualifier
= SLANG_QUAL_INOUT
;
1338 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1346 /* parse precision qualifier (lowp, mediump, highp */
1347 precision_qual
= *C
->I
++;
1348 /* ignored at this time */
1349 (void) precision_qual
;
1351 /* parse parameter's type specifier and name */
1352 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1354 param
->a_name
= parse_identifier(C
);
1355 if (param
->a_name
== SLANG_ATOM_NULL
)
1358 /* if the parameter is an array, parse its size (the size must be
1359 * explicitly defined
1361 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1362 slang_type_specifier p
;
1364 slang_type_specifier_ctr(&p
);
1365 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1366 slang_type_specifier_dtr(&p
);
1369 if (!convert_to_array(C
, param
, &p
)) {
1370 slang_type_specifier_dtr(&p
);
1373 slang_type_specifier_dtr(&p
);
1374 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1378 /* calculate the parameter size */
1379 if (!calculate_var_size(C
, O
, param
))
1382 /* TODO: allocate the local address here? */
1387 #define FUNCTION_ORDINARY 0
1388 #define FUNCTION_CONSTRUCTOR 1
1389 #define FUNCTION_OPERATOR 2
1391 /* function parameter */
1392 #define PARAMETER_NONE 0
1393 #define PARAMETER_NEXT 1
1396 #define OPERATOR_ADDASSIGN 1
1397 #define OPERATOR_SUBASSIGN 2
1398 #define OPERATOR_MULASSIGN 3
1399 #define OPERATOR_DIVASSIGN 4
1400 /*#define OPERATOR_MODASSIGN 5*/
1401 /*#define OPERATOR_LSHASSIGN 6*/
1402 /*#define OPERATOR_RSHASSIGN 7*/
1403 /*#define OPERATOR_ANDASSIGN 8*/
1404 /*#define OPERATOR_XORASSIGN 9*/
1405 /*#define OPERATOR_ORASSIGN 10*/
1406 #define OPERATOR_LOGICALXOR 11
1407 /*#define OPERATOR_BITOR 12*/
1408 /*#define OPERATOR_BITXOR 13*/
1409 /*#define OPERATOR_BITAND 14*/
1410 #define OPERATOR_LESS 15
1411 #define OPERATOR_GREATER 16
1412 #define OPERATOR_LESSEQUAL 17
1413 #define OPERATOR_GREATEREQUAL 18
1414 /*#define OPERATOR_LSHIFT 19*/
1415 /*#define OPERATOR_RSHIFT 20*/
1416 #define OPERATOR_MULTIPLY 21
1417 #define OPERATOR_DIVIDE 22
1418 /*#define OPERATOR_MODULUS 23*/
1419 #define OPERATOR_INCREMENT 24
1420 #define OPERATOR_DECREMENT 25
1421 #define OPERATOR_PLUS 26
1422 #define OPERATOR_MINUS 27
1423 /*#define OPERATOR_COMPLEMENT 28*/
1424 #define OPERATOR_NOT 29
1428 unsigned int o_code
;
1430 } operator_names
[] = {
1431 {OPERATOR_INCREMENT
, "++"},
1432 {OPERATOR_ADDASSIGN
, "+="},
1433 {OPERATOR_PLUS
, "+"},
1434 {OPERATOR_DECREMENT
, "--"},
1435 {OPERATOR_SUBASSIGN
, "-="},
1436 {OPERATOR_MINUS
, "-"},
1437 {OPERATOR_NOT
, "!"},
1438 {OPERATOR_MULASSIGN
, "*="},
1439 {OPERATOR_MULTIPLY
, "*"},
1440 {OPERATOR_DIVASSIGN
, "/="},
1441 {OPERATOR_DIVIDE
, "/"},
1442 {OPERATOR_LESSEQUAL
, "<="},
1443 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1444 /*{ OPERATOR_LSHIFT, "<<" }, */
1445 {OPERATOR_LESS
, "<"},
1446 {OPERATOR_GREATEREQUAL
, ">="},
1447 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1448 /*{ OPERATOR_RSHIFT, ">>" }, */
1449 {OPERATOR_GREATER
, ">"},
1450 /*{ OPERATOR_MODASSIGN, "%=" }, */
1451 /*{ OPERATOR_MODULUS, "%" }, */
1452 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1453 /*{ OPERATOR_BITAND, "&" }, */
1454 /*{ OPERATOR_ORASSIGN, "|=" }, */
1455 /*{ OPERATOR_BITOR, "|" }, */
1456 /*{ OPERATOR_COMPLEMENT, "~" }, */
1457 /*{ OPERATOR_XORASSIGN, "^=" }, */
1458 {OPERATOR_LOGICALXOR
, "^^"},
1459 /*{ OPERATOR_BITXOR, "^" } */
1463 parse_operator_name(slang_parse_ctx
* C
)
1467 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1468 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1470 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1471 if (atom
== SLANG_ATOM_NULL
) {
1472 slang_info_log_memory(C
->L
);
1484 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1485 slang_function
* func
)
1487 /* parse function type and name */
1488 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1491 case FUNCTION_ORDINARY
:
1492 func
->kind
= SLANG_FUNC_ORDINARY
;
1493 func
->header
.a_name
= parse_identifier(C
);
1494 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1497 case FUNCTION_CONSTRUCTOR
:
1498 func
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1499 if (func
->header
.type
.specifier
.type
== SLANG_SPEC_STRUCT
)
1501 func
->header
.a_name
=
1502 slang_atom_pool_atom(C
->atoms
,
1503 slang_type_specifier_type_to_string
1504 (func
->header
.type
.specifier
.type
));
1505 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1506 slang_info_log_memory(C
->L
);
1510 case FUNCTION_OPERATOR
:
1511 func
->kind
= SLANG_FUNC_OPERATOR
;
1512 func
->header
.a_name
= parse_operator_name(C
);
1513 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1520 if (!legal_identifier(func
->header
.a_name
)) {
1521 slang_info_log_error(C
->L
, "illegal function name '%s'",
1522 (char *) func
->header
.a_name
);
1526 /* parse function parameters */
1527 while (*C
->I
++ == PARAMETER_NEXT
) {
1528 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1530 slang_info_log_memory(C
->L
);
1533 if (!parse_parameter_declaration(C
, O
, p
))
1537 /* if the function returns a value, append a hidden __retVal 'out'
1538 * parameter that corresponds to the return value.
1540 if (_slang_function_has_return_value(func
)) {
1541 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1542 slang_atom a_retVal
= slang_atom_pool_atom(C
->atoms
, "__retVal");
1544 p
->a_name
= a_retVal
;
1545 p
->type
= func
->header
.type
;
1546 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1549 /* function formal parameters and local variables share the same
1550 * scope, so save the information about param count in a seperate
1551 * place also link the scope to the global variable scope so when a
1552 * given identifier is not found here, the search process continues
1553 * in the global space
1555 func
->param_count
= func
->parameters
->num_variables
;
1556 func
->parameters
->outer_scope
= O
->vars
;
1562 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1563 slang_function
* func
)
1565 slang_output_ctx o
= *O
;
1567 if (!parse_function_prototype(C
, O
, func
))
1570 /* create function's body operation */
1571 func
->body
= (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1572 if (func
->body
== NULL
) {
1573 slang_info_log_memory(C
->L
);
1576 if (!slang_operation_construct(func
->body
)) {
1577 _slang_free(func
->body
);
1579 slang_info_log_memory(C
->L
);
1583 /* to parse the body the parse context is modified in order to
1584 * capture parsed variables into function's local variable scope
1586 C
->global_scope
= GL_FALSE
;
1587 o
.vars
= func
->parameters
;
1588 if (!parse_statement(C
, &o
, func
->body
))
1591 C
->global_scope
= GL_TRUE
;
1596 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1598 slang_operation op_id
, op_assign
;
1601 /* construct the left side of assignment */
1602 if (!slang_operation_construct(&op_id
))
1604 op_id
.type
= SLANG_OPER_IDENTIFIER
;
1605 op_id
.a_id
= var
->a_name
;
1607 /* put the variable into operation's scope */
1608 op_id
.locals
->variables
=
1609 (slang_variable
**) _slang_alloc(sizeof(slang_variable
*));
1610 if (op_id
.locals
->variables
== NULL
) {
1611 slang_operation_destruct(&op_id
);
1614 op_id
.locals
->num_variables
= 1;
1615 op_id
.locals
->variables
[0] = var
;
1617 /* construct the assignment expression */
1618 if (!slang_operation_construct(&op_assign
)) {
1619 op_id
.locals
->num_variables
= 0;
1620 slang_operation_destruct(&op_id
);
1623 op_assign
.type
= SLANG_OPER_ASSIGN
;
1624 op_assign
.children
=
1625 (slang_operation
*) _slang_alloc(2 * sizeof(slang_operation
));
1626 if (op_assign
.children
== NULL
) {
1627 slang_operation_destruct(&op_assign
);
1628 op_id
.locals
->num_variables
= 0;
1629 slang_operation_destruct(&op_id
);
1632 op_assign
.num_children
= 2;
1633 op_assign
.children
[0] = op_id
;
1634 op_assign
.children
[1] = *var
->initializer
;
1638 /* carefully destroy the operations */
1639 op_assign
.num_children
= 0;
1640 _slang_free(op_assign
.children
);
1641 op_assign
.children
= NULL
;
1642 slang_operation_destruct(&op_assign
);
1643 op_id
.locals
->num_variables
= 0;
1644 slang_operation_destruct(&op_id
);
1652 /* init declarator list */
1653 #define DECLARATOR_NONE 0
1654 #define DECLARATOR_NEXT 1
1656 /* variable declaration */
1657 #define VARIABLE_NONE 0
1658 #define VARIABLE_IDENTIFIER 1
1659 #define VARIABLE_INITIALIZER 2
1660 #define VARIABLE_ARRAY_EXPLICIT 3
1661 #define VARIABLE_ARRAY_UNKNOWN 4
1665 * Parse the initializer for a variable declaration.
1668 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1669 const slang_fully_specified_type
* type
)
1671 slang_variable
*var
;
1674 /* empty init declatator (without name, e.g. "float ;") */
1675 if (*C
->I
++ == VARIABLE_NONE
)
1678 a_name
= parse_identifier(C
);
1680 /* check if name is already in this scope */
1681 if (_slang_locate_variable(O
->vars
, a_name
, GL_FALSE
)) {
1682 slang_info_log_error(C
->L
,
1683 "declaration of '%s' conflicts with previous declaration",
1688 /* make room for the new variable and initialize it */
1689 var
= slang_variable_scope_grow(O
->vars
);
1691 slang_info_log_memory(C
->L
);
1695 /* copy the declarator qualifier type, parse the identifier */
1696 var
->type
.qualifier
= type
->qualifier
;
1697 var
->a_name
= a_name
;
1698 if (var
->a_name
== SLANG_ATOM_NULL
)
1703 /* simple variable declarator - just copy the specifier */
1704 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1707 case VARIABLE_INITIALIZER
:
1708 /* initialized variable - copy the specifier and parse the expression */
1709 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1712 (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1713 if (var
->initializer
== NULL
) {
1714 slang_info_log_memory(C
->L
);
1717 if (!slang_operation_construct(var
->initializer
)) {
1718 _slang_free(var
->initializer
);
1719 var
->initializer
= NULL
;
1720 slang_info_log_memory(C
->L
);
1723 if (!parse_expression(C
, O
, var
->initializer
))
1726 case VARIABLE_ARRAY_UNKNOWN
:
1727 /* unsized array - mark it as array and copy the specifier to
1730 if (!convert_to_array(C
, var
, &type
->specifier
))
1733 case VARIABLE_ARRAY_EXPLICIT
:
1734 if (!convert_to_array(C
, var
, &type
->specifier
))
1736 if (!parse_array_len(C
, O
, &var
->array_len
))
1743 /* allocate global address space for a variable with a known size */
1745 && !(var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
1746 && var
->array_len
== 0)) {
1747 if (!calculate_var_size(C
, O
, var
))
1749 var
->address
= slang_var_pool_alloc(O
->global_pool
, var
->size
);
1752 /* emit code for global var decl */
1753 if (C
->global_scope
) {
1754 slang_assemble_ctx A
;
1756 A
.space
.funcs
= O
->funs
;
1757 A
.space
.structs
= O
->structs
;
1758 A
.space
.vars
= O
->vars
;
1759 A
.program
= O
->program
;
1760 A
.vartable
= O
->vartable
;
1762 A
.curFuncEndLabel
= NULL
;
1763 if (!_slang_codegen_global_variable(&A
, var
, C
->type
))
1767 /* initialize global variable */
1768 if (C
->global_scope
) {
1769 if (var
->initializer
!= NULL
) {
1770 slang_assemble_ctx A
;
1773 A
.space
.funcs
= O
->funs
;
1774 A
.space
.structs
= O
->structs
;
1775 A
.space
.vars
= O
->vars
;
1776 if (!initialize_global(&A
, var
))
1784 * Parse a list of variable declarations. Each variable may have an
1788 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1790 slang_fully_specified_type type
;
1792 /* parse the fully specified type, common to all declarators */
1793 if (!slang_fully_specified_type_construct(&type
))
1795 if (!parse_fully_specified_type(C
, O
, &type
)) {
1796 slang_fully_specified_type_destruct(&type
);
1800 /* parse declarators, pass-in the parsed type */
1802 if (!parse_init_declarator(C
, O
, &type
)) {
1803 slang_fully_specified_type_destruct(&type
);
1807 while (*C
->I
++ == DECLARATOR_NEXT
);
1809 slang_fully_specified_type_destruct(&type
);
1815 * Parse a function definition or declaration.
1816 * \param C parsing context
1817 * \param O output context
1818 * \param definition if non-zero expect a definition, else a declaration
1819 * \param parsed_func_ret returns the parsed function
1820 * \return GL_TRUE if success, GL_FALSE if failure
1823 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
1824 slang_function
** parsed_func_ret
)
1826 slang_function parsed_func
, *found_func
;
1828 /* parse function definition/declaration */
1829 if (!slang_function_construct(&parsed_func
))
1832 if (!parse_function_definition(C
, O
, &parsed_func
)) {
1833 slang_function_destruct(&parsed_func
);
1838 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
1839 slang_function_destruct(&parsed_func
);
1844 /* find a function with a prototype matching the parsed one - only
1845 * the current scope is being searched to allow built-in function
1848 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
1849 if (found_func
== NULL
) {
1850 /* New function, add it to the function list */
1851 O
->funs
->functions
=
1852 (slang_function
*) _slang_realloc(O
->funs
->functions
,
1853 O
->funs
->num_functions
1854 * sizeof(slang_function
),
1855 (O
->funs
->num_functions
+ 1)
1856 * sizeof(slang_function
));
1857 if (O
->funs
->functions
== NULL
) {
1858 slang_info_log_memory(C
->L
);
1859 slang_function_destruct(&parsed_func
);
1862 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
1863 O
->funs
->num_functions
++;
1865 /* return the newly parsed function */
1866 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
1869 /* previously defined or declared */
1870 /* TODO: check function return type qualifiers and specifiers */
1872 if (found_func
->body
!= NULL
) {
1873 slang_info_log_error(C
->L
, "%s: function already has a body.",
1874 slang_atom_pool_id(C
->atoms
,
1877 slang_function_destruct(&parsed_func
);
1881 /* destroy the existing function declaration and replace it
1882 * with the new one, remember to save the fixup table
1884 parsed_func
.fixups
= found_func
->fixups
;
1885 slang_fixup_table_init(&found_func
->fixups
);
1886 slang_function_destruct(found_func
);
1887 *found_func
= parsed_func
;
1890 /* another declaration of the same function prototype - ignore it */
1891 slang_function_destruct(&parsed_func
);
1894 /* return the found function */
1895 *parsed_func_ret
= found_func
;
1902 #define DECLARATION_FUNCTION_PROTOTYPE 1
1903 #define DECLARATION_INIT_DECLARATOR_LIST 2
1906 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1909 case DECLARATION_INIT_DECLARATOR_LIST
:
1910 if (!parse_init_declarator_list(C
, O
))
1913 case DECLARATION_FUNCTION_PROTOTYPE
:
1915 slang_function
*dummy_func
;
1917 if (!parse_function(C
, O
, 0, &dummy_func
))
1928 parse_default_precision(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1930 #if FEATURE_es2_glsl
1931 int precision
, type
;
1933 precision
= *C
->I
++;
1934 switch (precision
) {
1936 case PRECISION_MEDIUM
:
1937 case PRECISION_HIGH
:
1941 _mesa_problem(NULL
, "unexpected precision %d at %s:%d\n",
1942 precision
, __FILE__
, __LINE__
);
1948 case TYPE_SPECIFIER_FLOAT
:
1949 case TYPE_SPECIFIER_INT
:
1950 case TYPE_SPECIFIER_SAMPLER1D
:
1951 case TYPE_SPECIFIER_SAMPLER2D
:
1952 case TYPE_SPECIFIER_SAMPLER3D
:
1953 case TYPE_SPECIFIER_SAMPLERCUBE
:
1954 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
1955 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
1956 case TYPE_SPECIFIER_SAMPLER2DRECT
:
1957 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
1961 _mesa_problem(NULL
, "unexpected type %d at %s:%d\n",
1962 type
, __FILE__
, __LINE__
);
1966 assert(type
< TYPE_SPECIFIER_COUNT
);
1967 O
->default_precision
[type
] = precision
;
1971 slang_info_log_error(C
->L
, "syntax error at \"precision\"");
1978 * Initialize the default precision for all types.
1979 * XXX this info isn't used yet.
1982 init_default_precision(slang_output_ctx
*O
, slang_unit_type type
)
1985 for (i
= 0; i
< TYPE_SPECIFIER_COUNT
; i
++) {
1986 #if FEATURE_es2_glsl
1987 O
->default_precision
[i
] = PRECISION_LOW
;
1989 O
->default_precision
[i
] = PRECISION_HIGH
;
1992 #if FEATURE_es2_glsl
1993 if (type
== SLANG_UNIT_VERTEX_SHADER
) {
1994 O
->default_precision
[TYPE_SPECIFIER_FLOAT
] = PRECISION_HIGH
;
1995 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_HIGH
;
1998 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_MEDIUM
;
2005 parse_invariant(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2007 if (C
->version
>= 120 || FEATURE_es2_glsl
) {
2008 slang_atom
*a
= parse_identifier(C
);
2009 /* XXX not doing anything with this var yet */
2010 /*printf("ID: %s\n", (char*) a);*/
2014 slang_info_log_error(C
->L
, "syntax error at \"invariant\"");
2020 /* external declaration or default precision specifier */
2021 #define EXTERNAL_NULL 0
2022 #define EXTERNAL_FUNCTION_DEFINITION 1
2023 #define EXTERNAL_DECLARATION 2
2024 #define DEFAULT_PRECISION 3
2025 #define INVARIANT_STMT 4
2029 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
,
2030 struct gl_shader
*shader
)
2032 GET_CURRENT_CONTEXT(ctx
);
2036 slang_function
*mainFunc
= NULL
;
2038 if (unit
->type
== SLANG_UNIT_FRAGMENT_BUILTIN
||
2039 unit
->type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2040 maxRegs
= ctx
->Const
.FragmentProgram
.MaxTemps
;
2043 assert(unit
->type
== SLANG_UNIT_VERTEX_BUILTIN
||
2044 unit
->type
== SLANG_UNIT_VERTEX_SHADER
);
2045 maxRegs
= ctx
->Const
.VertexProgram
.MaxTemps
;
2048 /* setup output context */
2049 init_default_precision(&o
, unit
->type
);
2050 o
.funs
= &unit
->funs
;
2051 o
.structs
= &unit
->structs
;
2052 o
.vars
= &unit
->vars
;
2053 o
.global_pool
= &unit
->object
->varpool
;
2054 o
.program
= shader
? shader
->Program
: NULL
;
2055 o
.vartable
= _slang_new_var_table(maxRegs
);
2056 _slang_push_var_table(o
.vartable
);
2058 /* parse individual functions and declarations */
2059 while (*C
->I
!= EXTERNAL_NULL
) {
2061 case EXTERNAL_FUNCTION_DEFINITION
:
2063 slang_function
*func
;
2064 success
= parse_function(C
, &o
, 1, &func
);
2066 _mesa_strcmp((char *) func
->header
.a_name
, "main") == 0) {
2072 case EXTERNAL_DECLARATION
:
2073 success
= parse_declaration(C
, &o
);
2075 case DEFAULT_PRECISION
:
2076 success
= parse_default_precision(C
, &o
);
2078 case INVARIANT_STMT
:
2079 success
= parse_invariant(C
, &o
);
2086 /* xxx free codegen */
2087 _slang_pop_var_table(o
.vartable
);
2094 /* assemble (generate code) for main() */
2095 slang_assemble_ctx A
;
2098 A
.space
.funcs
= o
.funs
;
2099 A
.space
.structs
= o
.structs
;
2100 A
.space
.vars
= o
.vars
;
2101 A
.program
= o
.program
;
2102 A
.vartable
= o
.vartable
;
2105 /* main() takes no parameters */
2106 if (mainFunc
->param_count
> 0) {
2107 slang_info_log_error(A
.log
, "main() takes no arguments");
2111 _slang_codegen_function(&A
, mainFunc
);
2113 shader
->Main
= GL_TRUE
; /* this shader defines main() */
2116 _slang_pop_var_table(o
.vartable
);
2117 _slang_delete_var_table(o
.vartable
);
2123 compile_binary(const byte
* prod
, slang_code_unit
* unit
,
2125 slang_unit_type type
, slang_info_log
* infolog
,
2126 slang_code_unit
* builtin
, slang_code_unit
* downlink
,
2127 struct gl_shader
*shader
)
2133 /* setup parse context */
2136 C
.parsing_builtin
= (builtin
== NULL
);
2137 C
.global_scope
= GL_TRUE
;
2138 C
.atoms
= &unit
->object
->atompool
;
2140 C
.version
= version
;
2142 if (!check_revision(&C
))
2145 if (downlink
!= NULL
) {
2146 unit
->vars
.outer_scope
= &downlink
->vars
;
2147 unit
->funs
.outer_scope
= &downlink
->funs
;
2148 unit
->structs
.outer_scope
= &downlink
->structs
;
2151 /* parse translation unit */
2152 return parse_code_unit(&C
, unit
, shader
);
2156 compile_with_grammar(grammar id
, const char *source
, slang_code_unit
* unit
,
2157 slang_unit_type type
, slang_info_log
* infolog
,
2158 slang_code_unit
* builtin
,
2159 struct gl_shader
*shader
)
2162 GLuint size
, start
, version
;
2163 slang_string preprocessed
;
2166 #if FEATURE_ARB_shading_language_120
2168 #elif FEATURE_es2_glsl
2174 /* First retrieve the version number. */
2175 if (!_slang_preprocess_version(source
, &version
, &start
, infolog
))
2178 if (version
> maxVersion
) {
2179 slang_info_log_error(infolog
,
2180 "language version %.2f is not supported.",
2185 /* Now preprocess the source string. */
2186 slang_string_init(&preprocessed
);
2187 if (!_slang_preprocess_directives(&preprocessed
, &source
[start
], infolog
)) {
2188 slang_string_free(&preprocessed
);
2189 slang_info_log_error(infolog
, "failed to preprocess the source.");
2193 /* Finally check the syntax and generate its binary representation. */
2194 if (!grammar_fast_check(id
,
2195 (const byte
*) (slang_string_cstr(&preprocessed
)),
2196 &prod
, &size
, 65536)) {
2200 slang_string_free(&preprocessed
);
2201 grammar_get_last_error((byte
*) (buf
), sizeof(buf
), &pos
);
2202 slang_info_log_error(infolog
, buf
);
2203 /* syntax error (possibly in library code) */
2208 s
= (char *) _mesa_find_line_column((const GLubyte
*) source
,
2209 (const GLubyte
*) source
+ pos
,
2211 printf("Error on line %d, col %d: %s\n", line
, col
, s
);
2216 slang_string_free(&preprocessed
);
2218 /* Syntax is okay - translate it to internal representation. */
2219 if (!compile_binary(prod
, unit
, version
, type
, infolog
, builtin
,
2220 &builtin
[SLANG_BUILTIN_TOTAL
- 1],
2222 grammar_alloc_free(prod
);
2225 grammar_alloc_free(prod
);
2229 LONGSTRING
static const char *slang_shader_syn
=
2230 #include "library/slang_shader_syn.h"
2233 static const byte slang_core_gc
[] = {
2234 #include "library/slang_core_gc.h"
2237 static const byte slang_120_core_gc
[] = {
2238 #include "library/slang_120_core_gc.h"
2241 static const byte slang_120_fragment_gc
[] = {
2242 #include "library/slang_builtin_120_fragment_gc.h"
2245 static const byte slang_common_builtin_gc
[] = {
2246 #include "library/slang_common_builtin_gc.h"
2249 static const byte slang_fragment_builtin_gc
[] = {
2250 #include "library/slang_fragment_builtin_gc.h"
2253 static const byte slang_vertex_builtin_gc
[] = {
2254 #include "library/slang_vertex_builtin_gc.h"
2258 compile_object(grammar
* id
, const char *source
, slang_code_object
* object
,
2259 slang_unit_type type
, slang_info_log
* infolog
,
2260 struct gl_shader
*shader
)
2262 slang_code_unit
*builtins
= NULL
;
2263 GLuint base_version
= 110;
2265 /* load GLSL grammar */
2266 *id
= grammar_load_from_text((const byte
*) (slang_shader_syn
));
2271 grammar_get_last_error(buf
, 1024, &pos
);
2272 slang_info_log_error(infolog
, (const char *) (buf
));
2276 /* set shader type - the syntax is slightly different for different shaders */
2277 if (type
== SLANG_UNIT_FRAGMENT_SHADER
2278 || type
== SLANG_UNIT_FRAGMENT_BUILTIN
)
2279 grammar_set_reg8(*id
, (const byte
*) "shader_type", 1);
2281 grammar_set_reg8(*id
, (const byte
*) "shader_type", 2);
2283 /* enable language extensions */
2284 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2286 /* if parsing user-specified shader, load built-in library */
2287 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_VERTEX_SHADER
) {
2288 /* compile core functionality first */
2289 if (!compile_binary(slang_core_gc
,
2290 &object
->builtin
[SLANG_BUILTIN_CORE
],
2292 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2296 #if FEATURE_ARB_shading_language_120
2297 if (!compile_binary(slang_120_core_gc
,
2298 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2300 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2301 NULL
, &object
->builtin
[SLANG_BUILTIN_CORE
], NULL
))
2305 /* compile common functions and variables, link to core */
2306 if (!compile_binary(slang_common_builtin_gc
,
2307 &object
->builtin
[SLANG_BUILTIN_COMMON
],
2308 #if FEATURE_ARB_shading_language_120
2313 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2314 #if FEATURE_ARB_shading_language_120
2315 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2317 &object
->builtin
[SLANG_BUILTIN_CORE
],
2322 /* compile target-specific functions and variables, link to common */
2323 if (type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2324 if (!compile_binary(slang_fragment_builtin_gc
,
2325 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2327 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2328 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2330 #if FEATURE_ARB_shading_language_120
2331 if (!compile_binary(slang_120_fragment_gc
,
2332 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2334 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2335 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2339 else if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2340 if (!compile_binary(slang_vertex_builtin_gc
,
2341 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2343 SLANG_UNIT_VERTEX_BUILTIN
, infolog
, NULL
,
2344 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2348 /* disable language extensions */
2349 #if NEW_SLANG /* allow-built-ins */
2350 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2352 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 0);
2354 builtins
= object
->builtin
;
2357 /* compile the actual shader - pass-in built-in library for external shader */
2358 return compile_with_grammar(*id
, source
, &object
->unit
, type
, infolog
,
2364 compile_shader(GLcontext
*ctx
, slang_code_object
* object
,
2365 slang_unit_type type
, slang_info_log
* infolog
,
2366 struct gl_shader
*shader
)
2371 #if 0 /* for debug */
2372 _mesa_printf("********* COMPILE SHADER ***********\n");
2373 _mesa_printf("%s\n", shader
->Source
);
2374 _mesa_printf("************************************\n");
2377 assert(shader
->Program
);
2379 _slang_code_object_dtr(object
);
2380 _slang_code_object_ctr(object
);
2382 success
= compile_object(&id
, shader
->Source
, object
, type
, infolog
, shader
);
2384 grammar_destroy(id
);
2394 _slang_compile(GLcontext
*ctx
, struct gl_shader
*shader
)
2397 slang_info_log info_log
;
2398 slang_code_object obj
;
2399 slang_unit_type type
;
2401 if (shader
->Type
== GL_VERTEX_SHADER
) {
2402 type
= SLANG_UNIT_VERTEX_SHADER
;
2405 assert(shader
->Type
== GL_FRAGMENT_SHADER
);
2406 type
= SLANG_UNIT_FRAGMENT_SHADER
;
2409 if (!shader
->Source
)
2412 ctx
->Shader
.MemPool
= _slang_new_mempool(1024*1024);
2414 shader
->Main
= GL_FALSE
;
2416 if (!shader
->Program
) {
2418 if (shader
->Type
== GL_VERTEX_SHADER
)
2419 progTarget
= GL_VERTEX_PROGRAM_ARB
;
2421 progTarget
= GL_FRAGMENT_PROGRAM_ARB
;
2422 shader
->Program
= ctx
->Driver
.NewProgram(ctx
, progTarget
, 1);
2423 shader
->Program
->Parameters
= _mesa_new_parameter_list();
2424 shader
->Program
->Varying
= _mesa_new_parameter_list();
2425 shader
->Program
->Attributes
= _mesa_new_parameter_list();
2428 slang_info_log_construct(&info_log
);
2429 _slang_code_object_ctr(&obj
);
2431 success
= compile_shader(ctx
, &obj
, type
, &info_log
, shader
);
2433 /* free shader's prev info log */
2434 if (shader
->InfoLog
) {
2435 _mesa_free(shader
->InfoLog
);
2436 shader
->InfoLog
= NULL
;
2439 if (info_log
.text
) {
2440 /* copy info-log string to shader object */
2441 shader
->InfoLog
= _mesa_strdup(info_log
.text
);
2444 if (info_log
.error_flag
) {
2448 slang_info_log_destruct(&info_log
);
2449 _slang_code_object_dtr(&obj
);
2451 _slang_delete_mempool((slang_mempool
*) ctx
->Shader
.MemPool
);
2452 ctx
->Shader
.MemPool
= NULL
;
2454 /* remove any reads of output registers */
2456 printf("Pre-remove output reads:\n");
2457 _mesa_print_program(shader
->Program
);
2459 _mesa_remove_output_reads(shader
->Program
, PROGRAM_OUTPUT
);
2460 if (shader
->Type
== GL_VERTEX_SHADER
) {
2461 /* and remove writes to varying vars in vertex programs */
2462 _mesa_remove_output_reads(shader
->Program
, PROGRAM_VARYING
);
2465 printf("Post-remove output reads:\n");
2466 _mesa_print_program(shader
->Program
);