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.
61 * Allocate storage for a variable of 'size' bytes from given pool.
62 * Return the allocated address for the variable.
65 slang_var_pool_alloc(slang_var_pool
* pool
, unsigned int size
)
67 const GLuint addr
= pool
->next_addr
;
68 pool
->next_addr
+= size
;
77 _slang_code_unit_ctr(slang_code_unit
* self
,
78 struct slang_code_object_
* object
)
80 _slang_variable_scope_ctr(&self
->vars
);
81 _slang_function_scope_ctr(&self
->funs
);
82 _slang_struct_scope_ctr(&self
->structs
);
83 self
->object
= object
;
87 _slang_code_unit_dtr(slang_code_unit
* self
)
89 slang_variable_scope_destruct(&self
->vars
);
90 slang_function_scope_destruct(&self
->funs
);
91 slang_struct_scope_destruct(&self
->structs
);
99 _slang_code_object_ctr(slang_code_object
* self
)
103 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
104 _slang_code_unit_ctr(&self
->builtin
[i
], self
);
105 _slang_code_unit_ctr(&self
->unit
, self
);
106 self
->varpool
.next_addr
= 0;
107 slang_atom_pool_construct(&self
->atompool
);
111 _slang_code_object_dtr(slang_code_object
* self
)
115 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
116 _slang_code_unit_dtr(&self
->builtin
[i
]);
117 _slang_code_unit_dtr(&self
->unit
);
118 slang_atom_pool_destruct(&self
->atompool
);
122 /* slang_parse_ctx */
124 typedef struct slang_parse_ctx_
129 GLboolean global_scope
; /**< Is object being declared a global? */
130 slang_atom_pool
*atoms
;
131 slang_unit_type type
; /**< Vertex vs. Fragment */
134 /* slang_output_ctx */
136 typedef struct slang_output_ctx_
138 slang_variable_scope
*vars
;
139 slang_function_scope
*funs
;
140 slang_struct_scope
*structs
;
141 slang_var_pool
*global_pool
;
142 struct gl_program
*program
;
143 slang_var_table
*vartable
;
146 /* _slang_compile() */
149 parse_identifier_str(slang_parse_ctx
* C
, char **id
)
152 C
->I
+= _mesa_strlen(*id
) + 1;
156 parse_identifier(slang_parse_ctx
* C
)
160 id
= (const char *) C
->I
;
161 C
->I
+= _mesa_strlen(id
) + 1;
162 return slang_atom_pool_atom(C
->atoms
, id
);
166 parse_number(slang_parse_ctx
* C
, int *number
)
168 const int radix
= (int) (*C
->I
++);
170 while (*C
->I
!= '\0') {
172 if (*C
->I
>= '0' && *C
->I
<= '9')
173 digit
= (int) (*C
->I
- '0');
174 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
175 digit
= (int) (*C
->I
- 'A') + 10;
177 digit
= (int) (*C
->I
- 'a') + 10;
178 *number
= *number
* radix
+ digit
;
183 slang_info_log_warning(C
->L
, "%d: literal integer overflow.", *number
);
188 parse_float(slang_parse_ctx
* C
, float *number
)
190 char *integral
= NULL
;
191 char *fractional
= NULL
;
192 char *exponent
= NULL
;
195 parse_identifier_str(C
, &integral
);
196 parse_identifier_str(C
, &fractional
);
197 parse_identifier_str(C
, &exponent
);
199 whole
= (char *) _slang_alloc((_mesa_strlen(integral
) +
200 _mesa_strlen(fractional
) +
201 _mesa_strlen(exponent
) + 3) * sizeof(char));
203 slang_info_log_memory(C
->L
);
207 slang_string_copy(whole
, integral
);
208 slang_string_concat(whole
, ".");
209 slang_string_concat(whole
, fractional
);
210 slang_string_concat(whole
, "E");
211 slang_string_concat(whole
, exponent
);
213 *number
= (float) (_mesa_strtod(whole
, (char **) NULL
));
220 /* revision number - increment after each change affecting emitted output */
224 check_revision(slang_parse_ctx
* C
)
226 if (*C
->I
!= REVISION
) {
227 slang_info_log_error(C
->L
, "Internal compiler error.");
234 static int parse_statement(slang_parse_ctx
*, slang_output_ctx
*,
236 static int parse_expression(slang_parse_ctx
*, slang_output_ctx
*,
238 static int parse_type_specifier(slang_parse_ctx
*, slang_output_ctx
*,
239 slang_type_specifier
*);
242 parse_array_len(slang_parse_ctx
* C
, slang_output_ctx
* O
, GLuint
* len
)
244 slang_operation array_size
;
245 slang_name_space space
;
248 if (!slang_operation_construct(&array_size
))
250 if (!parse_expression(C
, O
, &array_size
)) {
251 slang_operation_destruct(&array_size
);
255 space
.funcs
= O
->funs
;
256 space
.structs
= O
->structs
;
257 space
.vars
= O
->vars
;
259 /* evaluate compile-time expression which is array size */
260 _slang_simplify(&array_size
, &space
, C
->atoms
);
261 result
= (array_size
.type
== SLANG_OPER_LITERAL_INT
);
263 *len
= (GLint
) array_size
.literal
[0];
265 slang_operation_destruct(&array_size
);
270 calculate_var_size(slang_parse_ctx
* C
, slang_output_ctx
* O
,
271 slang_variable
* var
)
273 slang_storage_aggregate agg
;
275 if (!slang_storage_aggregate_construct(&agg
))
277 if (!_slang_aggregate_variable(&agg
, &var
->type
.specifier
, var
->array_len
,
278 O
->funs
, O
->structs
, O
->vars
, C
->atoms
)) {
279 slang_storage_aggregate_destruct(&agg
);
282 var
->size
= _slang_sizeof_aggregate(&agg
);
283 slang_storage_aggregate_destruct(&agg
);
288 convert_to_array(slang_parse_ctx
* C
, slang_variable
* var
,
289 const slang_type_specifier
* sp
)
291 /* sized array - mark it as array, copy the specifier to the array element and
292 * parse the expression */
293 var
->type
.specifier
.type
= SLANG_SPEC_ARRAY
;
294 var
->type
.specifier
._array
= (slang_type_specifier
*)
295 _slang_alloc(sizeof(slang_type_specifier
));
296 if (var
->type
.specifier
._array
== NULL
) {
297 slang_info_log_memory(C
->L
);
300 slang_type_specifier_ctr(var
->type
.specifier
._array
);
301 return slang_type_specifier_copy(var
->type
.specifier
._array
, sp
);
304 /* structure field */
307 #define FIELD_ARRAY 2
310 parse_struct_field_var(slang_parse_ctx
* C
, slang_output_ctx
* O
,
311 slang_variable
* var
, const slang_type_specifier
* sp
)
313 var
->a_name
= parse_identifier(C
);
314 if (var
->a_name
== SLANG_ATOM_NULL
)
319 if (!slang_type_specifier_copy(&var
->type
.specifier
, sp
))
323 if (!convert_to_array(C
, var
, sp
))
325 if (!parse_array_len(C
, O
, &var
->array_len
))
332 return calculate_var_size(C
, O
, var
);
336 parse_struct_field(slang_parse_ctx
* C
, slang_output_ctx
* O
,
337 slang_struct
* st
, slang_type_specifier
* sp
)
339 slang_output_ctx o
= *O
;
341 o
.structs
= st
->structs
;
342 if (!parse_type_specifier(C
, &o
, sp
))
346 slang_variable
*var
= slang_variable_scope_grow(st
->fields
);
348 slang_info_log_memory(C
->L
);
351 if (!parse_struct_field_var(C
, &o
, var
, sp
))
354 while (*C
->I
++ != FIELD_NONE
);
360 parse_struct(slang_parse_ctx
* C
, slang_output_ctx
* O
, slang_struct
** st
)
365 /* parse struct name (if any) and make sure it is unique in current scope */
366 a_name
= parse_identifier(C
);
367 if (a_name
== SLANG_ATOM_NULL
)
370 name
= slang_atom_pool_id(C
->atoms
, a_name
);
372 && slang_struct_scope_find(O
->structs
, a_name
, 0) != NULL
) {
373 slang_info_log_error(C
->L
, "%s: duplicate type name.", name
);
377 /* set-up a new struct */
378 *st
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
380 slang_info_log_memory(C
->L
);
383 if (!slang_struct_construct(*st
)) {
386 slang_info_log_memory(C
->L
);
389 (**st
).a_name
= a_name
;
390 (**st
).structs
->outer_scope
= O
->structs
;
392 /* parse individual struct fields */
394 slang_type_specifier sp
;
396 slang_type_specifier_ctr(&sp
);
397 if (!parse_struct_field(C
, O
, *st
, &sp
)) {
398 slang_type_specifier_dtr(&sp
);
401 slang_type_specifier_dtr(&sp
);
403 while (*C
->I
++ != FIELD_NONE
);
405 /* if named struct, copy it to current scope */
406 if (name
[0] != '\0') {
409 O
->structs
->structs
=
410 (slang_struct
*) _slang_realloc(O
->structs
->structs
,
411 O
->structs
->num_structs
412 * sizeof(slang_struct
),
413 (O
->structs
->num_structs
+ 1)
414 * sizeof(slang_struct
));
415 if (O
->structs
->structs
== NULL
) {
416 slang_info_log_memory(C
->L
);
419 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
420 if (!slang_struct_construct(s
))
422 O
->structs
->num_structs
++;
423 if (!slang_struct_copy(s
, *st
))
432 #define TYPE_QUALIFIER_NONE 0
433 #define TYPE_QUALIFIER_CONST 1
434 #define TYPE_QUALIFIER_ATTRIBUTE 2
435 #define TYPE_QUALIFIER_VARYING 3
436 #define TYPE_QUALIFIER_UNIFORM 4
437 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
438 #define TYPE_QUALIFIER_FIXEDINPUT 6
441 parse_type_qualifier(slang_parse_ctx
* C
, slang_type_qualifier
* qual
)
444 case TYPE_QUALIFIER_NONE
:
445 *qual
= SLANG_QUAL_NONE
;
447 case TYPE_QUALIFIER_CONST
:
448 *qual
= SLANG_QUAL_CONST
;
450 case TYPE_QUALIFIER_ATTRIBUTE
:
451 *qual
= SLANG_QUAL_ATTRIBUTE
;
453 case TYPE_QUALIFIER_VARYING
:
454 *qual
= SLANG_QUAL_VARYING
;
456 case TYPE_QUALIFIER_UNIFORM
:
457 *qual
= SLANG_QUAL_UNIFORM
;
459 case TYPE_QUALIFIER_FIXEDOUTPUT
:
460 *qual
= SLANG_QUAL_FIXEDOUTPUT
;
462 case TYPE_QUALIFIER_FIXEDINPUT
:
463 *qual
= SLANG_QUAL_FIXEDINPUT
;
472 #define TYPE_SPECIFIER_VOID 0
473 #define TYPE_SPECIFIER_BOOL 1
474 #define TYPE_SPECIFIER_BVEC2 2
475 #define TYPE_SPECIFIER_BVEC3 3
476 #define TYPE_SPECIFIER_BVEC4 4
477 #define TYPE_SPECIFIER_INT 5
478 #define TYPE_SPECIFIER_IVEC2 6
479 #define TYPE_SPECIFIER_IVEC3 7
480 #define TYPE_SPECIFIER_IVEC4 8
481 #define TYPE_SPECIFIER_FLOAT 9
482 #define TYPE_SPECIFIER_VEC2 10
483 #define TYPE_SPECIFIER_VEC3 11
484 #define TYPE_SPECIFIER_VEC4 12
485 #define TYPE_SPECIFIER_MAT2 13
486 #define TYPE_SPECIFIER_MAT3 14
487 #define TYPE_SPECIFIER_MAT4 15
488 #define TYPE_SPECIFIER_SAMPLER1D 16
489 #define TYPE_SPECIFIER_SAMPLER2D 17
490 #define TYPE_SPECIFIER_SAMPLER3D 18
491 #define TYPE_SPECIFIER_SAMPLERCUBE 19
492 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
493 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
494 #define TYPE_SPECIFIER_SAMPLER2DRECT 22
495 #define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
496 #define TYPE_SPECIFIER_STRUCT 24
497 #define TYPE_SPECIFIER_TYPENAME 25
498 #define TYPE_SPECIFIER_MAT23 26
499 #define TYPE_SPECIFIER_MAT32 27
500 #define TYPE_SPECIFIER_MAT24 28
501 #define TYPE_SPECIFIER_MAT42 29
502 #define TYPE_SPECIFIER_MAT34 30
503 #define TYPE_SPECIFIER_MAT43 31
507 parse_type_specifier(slang_parse_ctx
* C
, slang_output_ctx
* O
,
508 slang_type_specifier
* spec
)
511 case TYPE_SPECIFIER_VOID
:
512 spec
->type
= SLANG_SPEC_VOID
;
514 case TYPE_SPECIFIER_BOOL
:
515 spec
->type
= SLANG_SPEC_BOOL
;
517 case TYPE_SPECIFIER_BVEC2
:
518 spec
->type
= SLANG_SPEC_BVEC2
;
520 case TYPE_SPECIFIER_BVEC3
:
521 spec
->type
= SLANG_SPEC_BVEC3
;
523 case TYPE_SPECIFIER_BVEC4
:
524 spec
->type
= SLANG_SPEC_BVEC4
;
526 case TYPE_SPECIFIER_INT
:
527 spec
->type
= SLANG_SPEC_INT
;
529 case TYPE_SPECIFIER_IVEC2
:
530 spec
->type
= SLANG_SPEC_IVEC2
;
532 case TYPE_SPECIFIER_IVEC3
:
533 spec
->type
= SLANG_SPEC_IVEC3
;
535 case TYPE_SPECIFIER_IVEC4
:
536 spec
->type
= SLANG_SPEC_IVEC4
;
538 case TYPE_SPECIFIER_FLOAT
:
539 spec
->type
= SLANG_SPEC_FLOAT
;
541 case TYPE_SPECIFIER_VEC2
:
542 spec
->type
= SLANG_SPEC_VEC2
;
544 case TYPE_SPECIFIER_VEC3
:
545 spec
->type
= SLANG_SPEC_VEC3
;
547 case TYPE_SPECIFIER_VEC4
:
548 spec
->type
= SLANG_SPEC_VEC4
;
550 case TYPE_SPECIFIER_MAT2
:
551 spec
->type
= SLANG_SPEC_MAT2
;
553 case TYPE_SPECIFIER_MAT3
:
554 spec
->type
= SLANG_SPEC_MAT3
;
556 case TYPE_SPECIFIER_MAT4
:
557 spec
->type
= SLANG_SPEC_MAT4
;
559 case TYPE_SPECIFIER_MAT23
:
560 spec
->type
= SLANG_SPEC_MAT23
;
562 case TYPE_SPECIFIER_MAT32
:
563 spec
->type
= SLANG_SPEC_MAT32
;
565 case TYPE_SPECIFIER_MAT24
:
566 spec
->type
= SLANG_SPEC_MAT24
;
568 case TYPE_SPECIFIER_MAT42
:
569 spec
->type
= SLANG_SPEC_MAT42
;
571 case TYPE_SPECIFIER_MAT34
:
572 spec
->type
= SLANG_SPEC_MAT34
;
574 case TYPE_SPECIFIER_MAT43
:
575 spec
->type
= SLANG_SPEC_MAT43
;
577 case TYPE_SPECIFIER_SAMPLER1D
:
578 spec
->type
= SLANG_SPEC_SAMPLER1D
;
580 case TYPE_SPECIFIER_SAMPLER2D
:
581 spec
->type
= SLANG_SPEC_SAMPLER2D
;
583 case TYPE_SPECIFIER_SAMPLER3D
:
584 spec
->type
= SLANG_SPEC_SAMPLER3D
;
586 case TYPE_SPECIFIER_SAMPLERCUBE
:
587 spec
->type
= SLANG_SPEC_SAMPLERCUBE
;
589 case TYPE_SPECIFIER_SAMPLER2DRECT
:
590 spec
->type
= SLANG_SPEC_SAMPLER2DRECT
;
592 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
593 spec
->type
= SLANG_SPEC_SAMPLER1DSHADOW
;
595 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
596 spec
->type
= SLANG_SPEC_SAMPLER2DSHADOW
;
598 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
599 spec
->type
= SLANG_SPEC_SAMPLER2DRECTSHADOW
;
601 case TYPE_SPECIFIER_STRUCT
:
602 spec
->type
= SLANG_SPEC_STRUCT
;
603 if (!parse_struct(C
, O
, &spec
->_struct
))
606 case TYPE_SPECIFIER_TYPENAME
:
607 spec
->type
= SLANG_SPEC_STRUCT
;
612 a_name
= parse_identifier(C
);
616 stru
= slang_struct_scope_find(O
->structs
, a_name
, 1);
618 slang_info_log_error(C
->L
, "undeclared type name '%s'",
619 slang_atom_pool_id(C
->atoms
, a_name
));
623 spec
->_struct
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
624 if (spec
->_struct
== NULL
) {
625 slang_info_log_memory(C
->L
);
628 if (!slang_struct_construct(spec
->_struct
)) {
629 _slang_free(spec
->_struct
);
630 spec
->_struct
= NULL
;
633 if (!slang_struct_copy(spec
->_struct
, stru
))
644 parse_fully_specified_type(slang_parse_ctx
* C
, slang_output_ctx
* O
,
645 slang_fully_specified_type
* type
)
647 if (!parse_type_qualifier(C
, &type
->qualifier
))
649 if (!parse_type_specifier(C
, O
, &type
->specifier
))
656 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
657 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
661 #define OP_CONTINUE 6
664 #define OP_EXPRESSION 9
669 #define OP_PUSH_VOID 14
670 #define OP_PUSH_BOOL 15
671 #define OP_PUSH_INT 16
672 #define OP_PUSH_FLOAT 17
673 #define OP_PUSH_IDENTIFIER 18
674 #define OP_SEQUENCE 19
676 #define OP_ADDASSIGN 21
677 #define OP_SUBASSIGN 22
678 #define OP_MULASSIGN 23
679 #define OP_DIVASSIGN 24
680 /*#define OP_MODASSIGN 25*/
681 /*#define OP_LSHASSIGN 26*/
682 /*#define OP_RSHASSIGN 27*/
683 /*#define OP_ORASSIGN 28*/
684 /*#define OP_XORASSIGN 29*/
685 /*#define OP_ANDASSIGN 30*/
687 #define OP_LOGICALOR 32
688 #define OP_LOGICALXOR 33
689 #define OP_LOGICALAND 34
690 /*#define OP_BITOR 35*/
691 /*#define OP_BITXOR 36*/
692 /*#define OP_BITAND 37*/
694 #define OP_NOTEQUAL 39
696 #define OP_GREATER 41
697 #define OP_LESSEQUAL 42
698 #define OP_GREATEREQUAL 43
699 /*#define OP_LSHIFT 44*/
700 /*#define OP_RSHIFT 45*/
702 #define OP_SUBTRACT 47
703 #define OP_MULTIPLY 48
705 /*#define OP_MODULUS 50*/
706 #define OP_PREINCREMENT 51
707 #define OP_PREDECREMENT 52
710 /*#define OP_COMPLEMENT 55*/
712 #define OP_SUBSCRIPT 57
715 #define OP_POSTINCREMENT 60
716 #define OP_POSTDECREMENT 61
720 * When parsing a compound production, this function is used to parse the
722 * For example, a while-loop compound will have two children, the
723 * while condition expression and the loop body. So, this function will
724 * be called twice to parse those two sub-expressions.
725 * \param C the parsing context
726 * \param O the output context
727 * \param oper the operation we're parsing
728 * \param statement indicates whether parsing a statement, or expression
729 * \return 1 if success, 0 if error
732 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
733 slang_operation
* oper
, GLboolean statement
)
737 /* grow child array */
738 ch
= slang_operation_grow(&oper
->num_children
, &oper
->children
);
740 return parse_statement(C
, O
, ch
);
741 return parse_expression(C
, O
, ch
);
744 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
747 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
748 slang_operation
* oper
)
750 oper
->locals
->outer_scope
= O
->vars
;
752 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
753 /* parse child statements, do not create new variable scope */
754 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
755 while (*C
->I
!= OP_END
)
756 if (!parse_child_operation(C
, O
, oper
, 1))
760 case OP_BLOCK_BEGIN_NEW_SCOPE
:
761 /* parse child statements, create new variable scope */
763 slang_output_ctx o
= *O
;
765 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
766 o
.vars
= oper
->locals
;
767 while (*C
->I
!= OP_END
)
768 if (!parse_child_operation(C
, &o
, oper
, 1))
774 /* local variable declaration, individual declarators are stored as
775 * children identifiers
777 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
779 const unsigned int first_var
= O
->vars
->num_variables
;
781 /* parse the declaration, note that there can be zero or more
782 * than one declarators
784 if (!parse_declaration(C
, O
))
786 if (first_var
< O
->vars
->num_variables
) {
787 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
789 assert(oper
->num_children
== 0);
790 oper
->num_children
= num_vars
;
791 oper
->children
= slang_operation_new(num_vars
);
792 if (oper
->children
== NULL
) {
793 slang_info_log_memory(C
->L
);
796 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
797 slang_operation
*o
= &oper
->children
[i
- first_var
];
798 o
->type
= SLANG_OPER_VARIABLE_DECL
;
799 o
->locals
->outer_scope
= O
->vars
;
800 o
->a_id
= O
->vars
->variables
[i
]->a_name
;
806 /* the __asm statement, parse the mnemonic and all its arguments
809 oper
->type
= SLANG_OPER_ASM
;
810 oper
->a_id
= parse_identifier(C
);
811 if (oper
->a_id
== SLANG_ATOM_NULL
)
813 while (*C
->I
!= OP_END
) {
814 if (!parse_child_operation(C
, O
, oper
, 0))
820 oper
->type
= SLANG_OPER_BREAK
;
823 oper
->type
= SLANG_OPER_CONTINUE
;
826 oper
->type
= SLANG_OPER_DISCARD
;
829 oper
->type
= SLANG_OPER_RETURN
;
830 if (!parse_child_operation(C
, O
, oper
, 0))
834 oper
->type
= SLANG_OPER_EXPRESSION
;
835 if (!parse_child_operation(C
, O
, oper
, 0))
839 oper
->type
= SLANG_OPER_IF
;
840 if (!parse_child_operation(C
, O
, oper
, 0))
842 if (!parse_child_operation(C
, O
, oper
, 1))
844 if (!parse_child_operation(C
, O
, oper
, 1))
849 slang_output_ctx o
= *O
;
851 oper
->type
= SLANG_OPER_WHILE
;
852 o
.vars
= oper
->locals
;
853 if (!parse_child_operation(C
, &o
, oper
, 1))
855 if (!parse_child_operation(C
, &o
, oper
, 1))
860 oper
->type
= SLANG_OPER_DO
;
861 if (!parse_child_operation(C
, O
, oper
, 1))
863 if (!parse_child_operation(C
, O
, oper
, 0))
868 slang_output_ctx o
= *O
;
870 oper
->type
= SLANG_OPER_FOR
;
871 o
.vars
= oper
->locals
;
872 if (!parse_child_operation(C
, &o
, oper
, 1))
874 if (!parse_child_operation(C
, &o
, oper
, 1))
876 if (!parse_child_operation(C
, &o
, oper
, 0))
878 if (!parse_child_operation(C
, &o
, oper
, 1))
889 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
890 slang_operation
** ops
, unsigned int *total_ops
,
895 op
->children
= slang_operation_new(n
);
896 if (op
->children
== NULL
) {
897 slang_info_log_memory(C
->L
);
900 op
->num_children
= n
;
902 for (i
= 0; i
< n
; i
++) {
903 slang_operation_destruct(&op
->children
[i
]);
904 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
907 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
910 *ops
= (slang_operation
*)
912 (*total_ops
+ n
) * sizeof(slang_operation
),
913 *total_ops
* sizeof(slang_operation
));
915 slang_info_log_memory(C
->L
);
922 is_constructor_name(const char *name
, slang_atom a_name
,
923 slang_struct_scope
* structs
)
925 if (slang_type_specifier_type_from_string(name
) != SLANG_SPEC_VOID
)
927 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
931 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
932 slang_operation
* oper
)
934 slang_operation
*ops
= NULL
;
935 unsigned int num_ops
= 0;
938 while (*C
->I
!= OP_END
) {
940 const unsigned int op_code
= *C
->I
++;
942 /* allocate default operation, becomes a no-op if not used */
943 ops
= (slang_operation
*)
945 num_ops
* sizeof(slang_operation
),
946 (num_ops
+ 1) * sizeof(slang_operation
));
948 slang_info_log_memory(C
->L
);
952 if (!slang_operation_construct(op
)) {
953 slang_info_log_memory(C
->L
);
957 op
->locals
->outer_scope
= O
->vars
;
961 op
->type
= SLANG_OPER_VOID
;
964 op
->type
= SLANG_OPER_LITERAL_BOOL
;
965 if (!parse_number(C
, &number
))
970 op
->literal
[3] = (GLfloat
) number
;
971 op
->literal_size
= 1;
974 op
->type
= SLANG_OPER_LITERAL_INT
;
975 if (!parse_number(C
, &number
))
980 op
->literal
[3] = (GLfloat
) number
;
981 op
->literal_size
= 1;
984 op
->type
= SLANG_OPER_LITERAL_FLOAT
;
985 if (!parse_float(C
, &op
->literal
[0]))
989 op
->literal
[3] = op
->literal
[0];
990 op
->literal_size
= 1;
992 case OP_PUSH_IDENTIFIER
:
993 op
->type
= SLANG_OPER_IDENTIFIER
;
994 op
->a_id
= parse_identifier(C
);
995 if (op
->a_id
== SLANG_ATOM_NULL
)
999 op
->type
= SLANG_OPER_SEQUENCE
;
1000 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1004 op
->type
= SLANG_OPER_ASSIGN
;
1005 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1009 op
->type
= SLANG_OPER_ADDASSIGN
;
1010 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1014 op
->type
= SLANG_OPER_SUBASSIGN
;
1015 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1019 op
->type
= SLANG_OPER_MULASSIGN
;
1020 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1024 op
->type
= SLANG_OPER_DIVASSIGN
;
1025 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1028 /*case OP_MODASSIGN: */
1029 /*case OP_LSHASSIGN: */
1030 /*case OP_RSHASSIGN: */
1031 /*case OP_ORASSIGN: */
1032 /*case OP_XORASSIGN: */
1033 /*case OP_ANDASSIGN: */
1035 op
->type
= SLANG_OPER_SELECT
;
1036 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1040 op
->type
= SLANG_OPER_LOGICALOR
;
1041 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1045 op
->type
= SLANG_OPER_LOGICALXOR
;
1046 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1050 op
->type
= SLANG_OPER_LOGICALAND
;
1051 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1055 /*case OP_BITXOR: */
1056 /*case OP_BITAND: */
1058 op
->type
= SLANG_OPER_EQUAL
;
1059 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1063 op
->type
= SLANG_OPER_NOTEQUAL
;
1064 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1068 op
->type
= SLANG_OPER_LESS
;
1069 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1073 op
->type
= SLANG_OPER_GREATER
;
1074 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1078 op
->type
= SLANG_OPER_LESSEQUAL
;
1079 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1082 case OP_GREATEREQUAL
:
1083 op
->type
= SLANG_OPER_GREATEREQUAL
;
1084 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1087 /*case OP_LSHIFT: */
1088 /*case OP_RSHIFT: */
1090 op
->type
= SLANG_OPER_ADD
;
1091 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1095 op
->type
= SLANG_OPER_SUBTRACT
;
1096 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1100 op
->type
= SLANG_OPER_MULTIPLY
;
1101 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1105 op
->type
= SLANG_OPER_DIVIDE
;
1106 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1109 /*case OP_MODULUS: */
1110 case OP_PREINCREMENT
:
1111 op
->type
= SLANG_OPER_PREINCREMENT
;
1112 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1115 case OP_PREDECREMENT
:
1116 op
->type
= SLANG_OPER_PREDECREMENT
;
1117 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1121 op
->type
= SLANG_OPER_PLUS
;
1122 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1126 op
->type
= SLANG_OPER_MINUS
;
1127 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1131 op
->type
= SLANG_OPER_NOT
;
1132 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1135 /*case OP_COMPLEMENT: */
1137 op
->type
= SLANG_OPER_SUBSCRIPT
;
1138 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1142 op
->type
= SLANG_OPER_CALL
;
1143 op
->a_id
= parse_identifier(C
);
1144 if (op
->a_id
== SLANG_ATOM_NULL
)
1146 while (*C
->I
!= OP_END
)
1147 if (!parse_child_operation(C
, O
, op
, 0))
1151 if (!C
->parsing_builtin
1152 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1155 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1156 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1157 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1163 op
->type
= SLANG_OPER_FIELD
;
1164 op
->a_id
= parse_identifier(C
);
1165 if (op
->a_id
== SLANG_ATOM_NULL
)
1167 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1170 case OP_POSTINCREMENT
:
1171 op
->type
= SLANG_OPER_POSTINCREMENT
;
1172 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1175 case OP_POSTDECREMENT
:
1176 op
->type
= SLANG_OPER_POSTDECREMENT
;
1177 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1186 slang_operation_destruct(oper
);
1187 *oper
= *ops
; /* struct copy */
1193 /* parameter qualifier */
1194 #define PARAM_QUALIFIER_IN 0
1195 #define PARAM_QUALIFIER_OUT 1
1196 #define PARAM_QUALIFIER_INOUT 2
1198 /* function parameter array presence */
1199 #define PARAMETER_ARRAY_NOT_PRESENT 0
1200 #define PARAMETER_ARRAY_PRESENT 1
1203 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1204 slang_variable
* param
)
1206 /* parse and validate the parameter's type qualifiers (there can be
1207 * two at most) because not all combinations are valid
1209 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1212 case PARAM_QUALIFIER_IN
:
1213 if (param
->type
.qualifier
!= SLANG_QUAL_CONST
1214 && param
->type
.qualifier
!= SLANG_QUAL_NONE
) {
1215 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1219 case PARAM_QUALIFIER_OUT
:
1220 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1221 param
->type
.qualifier
= SLANG_QUAL_OUT
;
1223 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1227 case PARAM_QUALIFIER_INOUT
:
1228 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1229 param
->type
.qualifier
= SLANG_QUAL_INOUT
;
1231 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1239 /* parse parameter's type specifier and name */
1240 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1242 param
->a_name
= parse_identifier(C
);
1243 if (param
->a_name
== SLANG_ATOM_NULL
)
1246 /* if the parameter is an array, parse its size (the size must be
1247 * explicitly defined
1249 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1250 slang_type_specifier p
;
1252 slang_type_specifier_ctr(&p
);
1253 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1254 slang_type_specifier_dtr(&p
);
1257 if (!convert_to_array(C
, param
, &p
)) {
1258 slang_type_specifier_dtr(&p
);
1261 slang_type_specifier_dtr(&p
);
1262 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1266 /* calculate the parameter size */
1267 if (!calculate_var_size(C
, O
, param
))
1270 /* TODO: allocate the local address here? */
1275 #define FUNCTION_ORDINARY 0
1276 #define FUNCTION_CONSTRUCTOR 1
1277 #define FUNCTION_OPERATOR 2
1279 /* function parameter */
1280 #define PARAMETER_NONE 0
1281 #define PARAMETER_NEXT 1
1284 #define OPERATOR_ADDASSIGN 1
1285 #define OPERATOR_SUBASSIGN 2
1286 #define OPERATOR_MULASSIGN 3
1287 #define OPERATOR_DIVASSIGN 4
1288 /*#define OPERATOR_MODASSIGN 5*/
1289 /*#define OPERATOR_LSHASSIGN 6*/
1290 /*#define OPERATOR_RSHASSIGN 7*/
1291 /*#define OPERATOR_ANDASSIGN 8*/
1292 /*#define OPERATOR_XORASSIGN 9*/
1293 /*#define OPERATOR_ORASSIGN 10*/
1294 #define OPERATOR_LOGICALXOR 11
1295 /*#define OPERATOR_BITOR 12*/
1296 /*#define OPERATOR_BITXOR 13*/
1297 /*#define OPERATOR_BITAND 14*/
1298 #define OPERATOR_LESS 15
1299 #define OPERATOR_GREATER 16
1300 #define OPERATOR_LESSEQUAL 17
1301 #define OPERATOR_GREATEREQUAL 18
1302 /*#define OPERATOR_LSHIFT 19*/
1303 /*#define OPERATOR_RSHIFT 20*/
1304 #define OPERATOR_MULTIPLY 21
1305 #define OPERATOR_DIVIDE 22
1306 /*#define OPERATOR_MODULUS 23*/
1307 #define OPERATOR_INCREMENT 24
1308 #define OPERATOR_DECREMENT 25
1309 #define OPERATOR_PLUS 26
1310 #define OPERATOR_MINUS 27
1311 /*#define OPERATOR_COMPLEMENT 28*/
1312 #define OPERATOR_NOT 29
1316 unsigned int o_code
;
1318 } operator_names
[] = {
1319 {OPERATOR_INCREMENT
, "++"},
1320 {OPERATOR_ADDASSIGN
, "+="},
1321 {OPERATOR_PLUS
, "+"},
1322 {OPERATOR_DECREMENT
, "--"},
1323 {OPERATOR_SUBASSIGN
, "-="},
1324 {OPERATOR_MINUS
, "-"},
1325 {OPERATOR_NOT
, "!"},
1326 {OPERATOR_MULASSIGN
, "*="},
1327 {OPERATOR_MULTIPLY
, "*"},
1328 {OPERATOR_DIVASSIGN
, "/="},
1329 {OPERATOR_DIVIDE
, "/"},
1330 {OPERATOR_LESSEQUAL
, "<="},
1331 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1332 /*{ OPERATOR_LSHIFT, "<<" }, */
1333 {OPERATOR_LESS
, "<"},
1334 {OPERATOR_GREATEREQUAL
, ">="},
1335 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1336 /*{ OPERATOR_RSHIFT, ">>" }, */
1337 {OPERATOR_GREATER
, ">"},
1338 /*{ OPERATOR_MODASSIGN, "%=" }, */
1339 /*{ OPERATOR_MODULUS, "%" }, */
1340 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1341 /*{ OPERATOR_BITAND, "&" }, */
1342 /*{ OPERATOR_ORASSIGN, "|=" }, */
1343 /*{ OPERATOR_BITOR, "|" }, */
1344 /*{ OPERATOR_COMPLEMENT, "~" }, */
1345 /*{ OPERATOR_XORASSIGN, "^=" }, */
1346 {OPERATOR_LOGICALXOR
, "^^"},
1347 /*{ OPERATOR_BITXOR, "^" } */
1351 parse_operator_name(slang_parse_ctx
* C
)
1355 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1356 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1358 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1359 if (atom
== SLANG_ATOM_NULL
) {
1360 slang_info_log_memory(C
->L
);
1371 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1372 slang_function
* func
)
1374 /* parse function type and name */
1375 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1378 case FUNCTION_ORDINARY
:
1379 func
->kind
= SLANG_FUNC_ORDINARY
;
1380 func
->header
.a_name
= parse_identifier(C
);
1381 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1384 case FUNCTION_CONSTRUCTOR
:
1385 func
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1386 if (func
->header
.type
.specifier
.type
== SLANG_SPEC_STRUCT
)
1388 func
->header
.a_name
=
1389 slang_atom_pool_atom(C
->atoms
,
1390 slang_type_specifier_type_to_string
1391 (func
->header
.type
.specifier
.type
));
1392 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1393 slang_info_log_memory(C
->L
);
1397 case FUNCTION_OPERATOR
:
1398 func
->kind
= SLANG_FUNC_OPERATOR
;
1399 func
->header
.a_name
= parse_operator_name(C
);
1400 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1407 /* parse function parameters */
1408 while (*C
->I
++ == PARAMETER_NEXT
) {
1409 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1411 slang_info_log_memory(C
->L
);
1414 if (!parse_parameter_declaration(C
, O
, p
))
1418 /* if the function returns a value, append a hidden __retVal 'out'
1419 * parameter that corresponds to the return value.
1421 if (_slang_function_has_return_value(func
)) {
1422 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1423 slang_atom a_retVal
= slang_atom_pool_atom(C
->atoms
, "__retVal");
1425 p
->a_name
= a_retVal
;
1426 p
->type
= func
->header
.type
;
1427 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1430 /* function formal parameters and local variables share the same
1431 * scope, so save the information about param count in a seperate
1432 * place also link the scope to the global variable scope so when a
1433 * given identifier is not found here, the search process continues
1434 * in the global space
1436 func
->param_count
= func
->parameters
->num_variables
;
1437 func
->parameters
->outer_scope
= O
->vars
;
1443 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1444 slang_function
* func
)
1446 slang_output_ctx o
= *O
;
1448 if (!parse_function_prototype(C
, O
, func
))
1451 /* create function's body operation */
1452 func
->body
= (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1453 if (func
->body
== NULL
) {
1454 slang_info_log_memory(C
->L
);
1457 if (!slang_operation_construct(func
->body
)) {
1458 _slang_free(func
->body
);
1460 slang_info_log_memory(C
->L
);
1464 /* to parse the body the parse context is modified in order to
1465 * capture parsed variables into function's local variable scope
1467 C
->global_scope
= GL_FALSE
;
1468 o
.vars
= func
->parameters
;
1469 if (!parse_statement(C
, &o
, func
->body
))
1472 C
->global_scope
= GL_TRUE
;
1477 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1479 slang_operation op_id
, op_assign
;
1482 /* construct the left side of assignment */
1483 if (!slang_operation_construct(&op_id
))
1485 op_id
.type
= SLANG_OPER_IDENTIFIER
;
1486 op_id
.a_id
= var
->a_name
;
1488 /* put the variable into operation's scope */
1489 op_id
.locals
->variables
=
1490 (slang_variable
**) _slang_alloc(sizeof(slang_variable
*));
1491 if (op_id
.locals
->variables
== NULL
) {
1492 slang_operation_destruct(&op_id
);
1495 op_id
.locals
->num_variables
= 1;
1496 op_id
.locals
->variables
[0] = var
;
1498 /* construct the assignment expression */
1499 if (!slang_operation_construct(&op_assign
)) {
1500 op_id
.locals
->num_variables
= 0;
1501 slang_operation_destruct(&op_id
);
1504 op_assign
.type
= SLANG_OPER_ASSIGN
;
1505 op_assign
.children
=
1506 (slang_operation
*) _slang_alloc(2 * sizeof(slang_operation
));
1507 if (op_assign
.children
== NULL
) {
1508 slang_operation_destruct(&op_assign
);
1509 op_id
.locals
->num_variables
= 0;
1510 slang_operation_destruct(&op_id
);
1513 op_assign
.num_children
= 2;
1514 op_assign
.children
[0] = op_id
;
1515 op_assign
.children
[1] = *var
->initializer
;
1519 /* carefully destroy the operations */
1520 op_assign
.num_children
= 0;
1521 _slang_free(op_assign
.children
);
1522 op_assign
.children
= NULL
;
1523 slang_operation_destruct(&op_assign
);
1524 op_id
.locals
->num_variables
= 0;
1525 slang_operation_destruct(&op_id
);
1533 /* init declarator list */
1534 #define DECLARATOR_NONE 0
1535 #define DECLARATOR_NEXT 1
1537 /* variable declaration */
1538 #define VARIABLE_NONE 0
1539 #define VARIABLE_IDENTIFIER 1
1540 #define VARIABLE_INITIALIZER 2
1541 #define VARIABLE_ARRAY_EXPLICIT 3
1542 #define VARIABLE_ARRAY_UNKNOWN 4
1546 * Parse the initializer for a variable declaration.
1549 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1550 const slang_fully_specified_type
* type
)
1552 slang_variable
*var
;
1554 /* empty init declatator (without name, e.g. "float ;") */
1555 if (*C
->I
++ == VARIABLE_NONE
)
1558 /* make room for the new variable and initialize it */
1559 var
= slang_variable_scope_grow(O
->vars
);
1561 slang_info_log_memory(C
->L
);
1565 /* copy the declarator qualifier type, parse the identifier */
1566 var
->type
.qualifier
= type
->qualifier
;
1567 var
->a_name
= parse_identifier(C
);
1568 if (var
->a_name
== SLANG_ATOM_NULL
)
1573 /* simple variable declarator - just copy the specifier */
1574 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1577 case VARIABLE_INITIALIZER
:
1578 /* initialized variable - copy the specifier and parse the expression */
1579 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1582 (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1583 if (var
->initializer
== NULL
) {
1584 slang_info_log_memory(C
->L
);
1587 if (!slang_operation_construct(var
->initializer
)) {
1588 _slang_free(var
->initializer
);
1589 var
->initializer
= NULL
;
1590 slang_info_log_memory(C
->L
);
1593 if (!parse_expression(C
, O
, var
->initializer
))
1596 case VARIABLE_ARRAY_UNKNOWN
:
1597 /* unsized array - mark it as array and copy the specifier to
1600 if (!convert_to_array(C
, var
, &type
->specifier
))
1603 case VARIABLE_ARRAY_EXPLICIT
:
1604 if (!convert_to_array(C
, var
, &type
->specifier
))
1606 if (!parse_array_len(C
, O
, &var
->array_len
))
1613 /* emit code for global var decl */
1614 if (C
->global_scope
) {
1615 slang_assemble_ctx A
;
1617 A
.space
.funcs
= O
->funs
;
1618 A
.space
.structs
= O
->structs
;
1619 A
.space
.vars
= O
->vars
;
1620 A
.program
= O
->program
;
1621 A
.vartable
= O
->vartable
;
1622 A
.curFuncEndLabel
= NULL
;
1623 if (!_slang_codegen_global_variable(&A
, var
, C
->type
))
1627 /* allocate global address space for a variable with a known size */
1629 && !(var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
1630 && var
->array_len
== 0)) {
1631 if (!calculate_var_size(C
, O
, var
))
1633 var
->address
= slang_var_pool_alloc(O
->global_pool
, var
->size
);
1636 /* initialize global variable */
1637 if (C
->global_scope
) {
1638 if (var
->initializer
!= NULL
) {
1639 slang_assemble_ctx A
;
1642 A
.space
.funcs
= O
->funs
;
1643 A
.space
.structs
= O
->structs
;
1644 A
.space
.vars
= O
->vars
;
1645 if (!initialize_global(&A
, var
))
1653 * Parse a list of variable declarations. Each variable may have an
1657 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1659 slang_fully_specified_type type
;
1661 /* parse the fully specified type, common to all declarators */
1662 if (!slang_fully_specified_type_construct(&type
))
1664 if (!parse_fully_specified_type(C
, O
, &type
)) {
1665 slang_fully_specified_type_destruct(&type
);
1669 /* parse declarators, pass-in the parsed type */
1671 if (!parse_init_declarator(C
, O
, &type
)) {
1672 slang_fully_specified_type_destruct(&type
);
1676 while (*C
->I
++ == DECLARATOR_NEXT
);
1678 slang_fully_specified_type_destruct(&type
);
1684 * Parse a function definition or declaration.
1685 * \param C parsing context
1686 * \param O output context
1687 * \param definition if non-zero expect a definition, else a declaration
1688 * \param parsed_func_ret returns the parsed function
1689 * \return GL_TRUE if success, GL_FALSE if failure
1692 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
1693 slang_function
** parsed_func_ret
)
1695 slang_function parsed_func
, *found_func
;
1697 /* parse function definition/declaration */
1698 if (!slang_function_construct(&parsed_func
))
1701 if (!parse_function_definition(C
, O
, &parsed_func
)) {
1702 slang_function_destruct(&parsed_func
);
1707 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
1708 slang_function_destruct(&parsed_func
);
1713 /* find a function with a prototype matching the parsed one - only
1714 * the current scope is being searched to allow built-in function
1717 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
1718 if (found_func
== NULL
) {
1719 /* New function, add it to the function list */
1720 O
->funs
->functions
=
1721 (slang_function
*) _slang_realloc(O
->funs
->functions
,
1722 O
->funs
->num_functions
1723 * sizeof(slang_function
),
1724 (O
->funs
->num_functions
+ 1)
1725 * sizeof(slang_function
));
1726 if (O
->funs
->functions
== NULL
) {
1727 slang_info_log_memory(C
->L
);
1728 slang_function_destruct(&parsed_func
);
1731 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
1732 O
->funs
->num_functions
++;
1734 /* return the newly parsed function */
1735 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
1738 /* previously defined or declared */
1739 /* TODO: check function return type qualifiers and specifiers */
1741 if (found_func
->body
!= NULL
) {
1742 slang_info_log_error(C
->L
, "%s: function already has a body.",
1743 slang_atom_pool_id(C
->atoms
,
1746 slang_function_destruct(&parsed_func
);
1750 /* destroy the existing function declaration and replace it
1751 * with the new one, remember to save the fixup table
1753 parsed_func
.fixups
= found_func
->fixups
;
1754 slang_fixup_table_init(&found_func
->fixups
);
1755 slang_function_destruct(found_func
);
1756 *found_func
= parsed_func
;
1759 /* another declaration of the same function prototype - ignore it */
1760 slang_function_destruct(&parsed_func
);
1763 /* return the found function */
1764 *parsed_func_ret
= found_func
;
1767 /* assemble the parsed function */
1769 slang_assemble_ctx A
;
1772 A
.space
.funcs
= O
->funs
;
1773 A
.space
.structs
= O
->structs
;
1774 A
.space
.vars
= O
->vars
;
1775 A
.program
= O
->program
;
1776 A
.vartable
= O
->vartable
;
1779 _slang_codegen_function(&A
, *parsed_func_ret
);
1785 #define DECLARATION_FUNCTION_PROTOTYPE 1
1786 #define DECLARATION_INIT_DECLARATOR_LIST 2
1789 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1792 case DECLARATION_INIT_DECLARATOR_LIST
:
1793 if (!parse_init_declarator_list(C
, O
))
1796 case DECLARATION_FUNCTION_PROTOTYPE
:
1798 slang_function
*dummy_func
;
1800 if (!parse_function(C
, O
, 0, &dummy_func
))
1810 /* external declaration */
1811 #define EXTERNAL_NULL 0
1812 #define EXTERNAL_FUNCTION_DEFINITION 1
1813 #define EXTERNAL_DECLARATION 2
1816 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
,
1817 struct gl_program
*program
)
1819 GET_CURRENT_CONTEXT(ctx
);
1824 if (unit
->type
== SLANG_UNIT_FRAGMENT_BUILTIN
||
1825 unit
->type
== SLANG_UNIT_FRAGMENT_SHADER
) {
1826 maxRegs
= ctx
->Const
.FragmentProgram
.MaxTemps
;
1829 assert(unit
->type
== SLANG_UNIT_VERTEX_BUILTIN
||
1830 unit
->type
== SLANG_UNIT_VERTEX_SHADER
);
1831 maxRegs
= ctx
->Const
.VertexProgram
.MaxTemps
;
1834 /* setup output context */
1835 o
.funs
= &unit
->funs
;
1836 o
.structs
= &unit
->structs
;
1837 o
.vars
= &unit
->vars
;
1838 o
.global_pool
= &unit
->object
->varpool
;
1839 o
.program
= program
;
1840 o
.vartable
= _slang_new_var_table(maxRegs
);
1841 _slang_push_var_table(o
.vartable
);
1843 /* parse individual functions and declarations */
1844 while (*C
->I
!= EXTERNAL_NULL
) {
1846 case EXTERNAL_FUNCTION_DEFINITION
:
1848 slang_function
*func
;
1849 success
= parse_function(C
, &o
, 1, &func
);
1852 case EXTERNAL_DECLARATION
:
1853 success
= parse_declaration(C
, &o
);
1860 /* xxx free codegen */
1861 _slang_pop_var_table(o
.vartable
);
1867 _slang_pop_var_table(o
.vartable
);
1868 _slang_delete_var_table(o
.vartable
);
1874 compile_binary(const byte
* prod
, slang_code_unit
* unit
,
1875 slang_unit_type type
, slang_info_log
* infolog
,
1876 slang_code_unit
* builtin
, slang_code_unit
* downlink
,
1877 struct gl_program
*program
)
1883 /* setup parse context */
1886 C
.parsing_builtin
= (builtin
== NULL
);
1887 C
.global_scope
= GL_TRUE
;
1888 C
.atoms
= &unit
->object
->atompool
;
1891 if (!check_revision(&C
))
1894 if (downlink
!= NULL
) {
1895 unit
->vars
.outer_scope
= &downlink
->vars
;
1896 unit
->funs
.outer_scope
= &downlink
->funs
;
1897 unit
->structs
.outer_scope
= &downlink
->structs
;
1900 /* parse translation unit */
1901 return parse_code_unit(&C
, unit
, program
);
1905 compile_with_grammar(grammar id
, const char *source
, slang_code_unit
* unit
,
1906 slang_unit_type type
, slang_info_log
* infolog
,
1907 slang_code_unit
* builtin
,
1908 struct gl_program
*program
)
1911 GLuint size
, start
, version
;
1912 slang_string preprocessed
;
1915 #if FEATURE_ARB_shading_language_120
1921 /* First retrieve the version number. */
1922 if (!_slang_preprocess_version(source
, &version
, &start
, infolog
))
1925 if (version
> maxVersion
) {
1926 slang_info_log_error(infolog
,
1927 "language version %.2f is not supported.",
1932 /* Now preprocess the source string. */
1933 slang_string_init(&preprocessed
);
1934 if (!_slang_preprocess_directives(&preprocessed
, &source
[start
], infolog
)) {
1935 slang_string_free(&preprocessed
);
1936 slang_info_log_error(infolog
, "failed to preprocess the source.");
1940 /* Finally check the syntax and generate its binary representation. */
1941 if (!grammar_fast_check(id
,
1942 (const byte
*) (slang_string_cstr(&preprocessed
)),
1943 &prod
, &size
, 65536)) {
1947 slang_string_free(&preprocessed
);
1948 grammar_get_last_error((byte
*) (buf
), sizeof(buf
), &pos
);
1949 slang_info_log_error(infolog
, buf
);
1950 /* syntax error (possibly in library code) */
1955 s
= (char *) _mesa_find_line_column((const GLubyte
*) source
,
1956 (const GLubyte
*) source
+ pos
,
1958 printf("Error on line %d, col %d: %s\n", line
, col
, s
);
1963 slang_string_free(&preprocessed
);
1965 /* Syntax is okay - translate it to internal representation. */
1966 if (!compile_binary(prod
, unit
, type
, infolog
, builtin
,
1967 &builtin
[SLANG_BUILTIN_TOTAL
- 1],
1969 grammar_alloc_free(prod
);
1972 grammar_alloc_free(prod
);
1976 LONGSTRING
static const char *slang_shader_syn
=
1977 #include "library/slang_shader_syn.h"
1980 static const byte slang_core_gc
[] = {
1981 #include "library/slang_core_gc.h"
1984 static const byte slang_120_core_gc
[] = {
1985 #include "library/slang_120_core_gc.h"
1988 static const byte slang_120_fragment_gc
[] = {
1989 #include "library/slang_builtin_120_fragment_gc.h"
1992 static const byte slang_common_builtin_gc
[] = {
1993 #include "library/slang_common_builtin_gc.h"
1996 static const byte slang_fragment_builtin_gc
[] = {
1997 #include "library/slang_fragment_builtin_gc.h"
2000 static const byte slang_vertex_builtin_gc
[] = {
2001 #include "library/slang_vertex_builtin_gc.h"
2005 compile_object(grammar
* id
, const char *source
, slang_code_object
* object
,
2006 slang_unit_type type
, slang_info_log
* infolog
,
2007 struct gl_program
*program
)
2009 slang_code_unit
*builtins
= NULL
;
2011 /* load GLSL grammar */
2012 *id
= grammar_load_from_text((const byte
*) (slang_shader_syn
));
2017 grammar_get_last_error(buf
, 1024, &pos
);
2018 slang_info_log_error(infolog
, (const char *) (buf
));
2022 /* set shader type - the syntax is slightly different for different shaders */
2023 if (type
== SLANG_UNIT_FRAGMENT_SHADER
2024 || type
== SLANG_UNIT_FRAGMENT_BUILTIN
)
2025 grammar_set_reg8(*id
, (const byte
*) "shader_type", 1);
2027 grammar_set_reg8(*id
, (const byte
*) "shader_type", 2);
2029 /* enable language extensions */
2030 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2032 /* if parsing user-specified shader, load built-in library */
2033 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_VERTEX_SHADER
) {
2034 /* compile core functionality first */
2035 if (!compile_binary(slang_core_gc
,
2036 &object
->builtin
[SLANG_BUILTIN_CORE
],
2037 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2041 #if FEATURE_ARB_shading_language_120
2042 if (!compile_binary(slang_120_core_gc
,
2043 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2044 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2045 NULL
, &object
->builtin
[SLANG_BUILTIN_CORE
], NULL
))
2049 /* compile common functions and variables, link to core */
2050 if (!compile_binary(slang_common_builtin_gc
,
2051 &object
->builtin
[SLANG_BUILTIN_COMMON
],
2052 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2053 #if FEATURE_ARB_shading_language_120
2054 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2056 &object
->builtin
[SLANG_BUILTIN_CORE
],
2061 /* compile target-specific functions and variables, link to common */
2062 if (type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2063 if (!compile_binary(slang_fragment_builtin_gc
,
2064 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2065 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2066 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2068 #if FEATURE_ARB_shading_language_120
2069 if (!compile_binary(slang_120_fragment_gc
,
2070 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2071 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2072 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2076 else if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2077 if (!compile_binary(slang_vertex_builtin_gc
,
2078 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2079 SLANG_UNIT_VERTEX_BUILTIN
, infolog
, NULL
,
2080 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2084 /* disable language extensions */
2085 #if NEW_SLANG /* allow-built-ins */
2086 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2088 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 0);
2090 builtins
= object
->builtin
;
2093 /* compile the actual shader - pass-in built-in library for external shader */
2094 return compile_with_grammar(*id
, source
, &object
->unit
, type
, infolog
,
2100 compile_shader(GLcontext
*ctx
, slang_code_object
* object
,
2101 slang_unit_type type
, slang_info_log
* infolog
,
2102 struct gl_shader
*shader
)
2104 struct gl_program
*program
= shader
->Programs
[0];
2110 _slang_code_object_dtr(object
);
2111 _slang_code_object_ctr(object
);
2113 success
= compile_object(&id
, shader
->Source
, object
, type
, infolog
, program
);
2115 grammar_destroy(id
);
2125 _slang_compile(GLcontext
*ctx
, struct gl_shader
*shader
)
2128 slang_info_log info_log
;
2129 slang_code_object obj
;
2130 slang_unit_type type
;
2132 if (shader
->Type
== GL_VERTEX_SHADER
) {
2133 type
= SLANG_UNIT_VERTEX_SHADER
;
2136 assert(shader
->Type
== GL_FRAGMENT_SHADER
);
2137 type
= SLANG_UNIT_FRAGMENT_SHADER
;
2140 ctx
->Shader
.MemPool
= _slang_new_mempool(1024*1024);
2142 /* XXX temporary hack */
2143 if (!shader
->Programs
) {
2145 if (shader
->Type
== GL_VERTEX_SHADER
)
2146 progTarget
= GL_VERTEX_PROGRAM_ARB
;
2148 progTarget
= GL_FRAGMENT_PROGRAM_ARB
;
2150 = (struct gl_program
**) malloc(sizeof(struct gl_program
*));
2151 shader
->Programs
[0] = ctx
->Driver
.NewProgram(ctx
, progTarget
, 1);
2152 shader
->NumPrograms
= 1;
2154 shader
->Programs
[0]->Parameters
= _mesa_new_parameter_list();
2155 shader
->Programs
[0]->Varying
= _mesa_new_parameter_list();
2156 shader
->Programs
[0]->Attributes
= _mesa_new_parameter_list();
2159 slang_info_log_construct(&info_log
);
2160 _slang_code_object_ctr(&obj
);
2162 success
= compile_shader(ctx
, &obj
, type
, &info_log
, shader
);
2164 /* free shader's prev info log */
2165 if (shader
->InfoLog
) {
2166 _mesa_free(shader
->InfoLog
);
2167 shader
->InfoLog
= NULL
;
2170 if (info_log
.text
) {
2171 /* copy info-log string to shader object */
2172 shader
->InfoLog
= _mesa_strdup(info_log
.text
);
2175 if (info_log
.error_flag
) {
2179 slang_info_log_destruct(&info_log
);
2180 _slang_code_object_dtr(&obj
);
2182 _slang_delete_mempool((slang_mempool
*) ctx
->Shader
.MemPool
);
2183 ctx
->Shader
.MemPool
= NULL
;
2185 if (shader
->Type
== GL_VERTEX_SHADER
) {
2186 /* remove any reads of varying (output) registers */
2188 printf("Pre-remove output reads:\n");
2189 _mesa_print_program(shader
->Programs
[0]);
2191 _mesa_remove_varying_reads(shader
->Programs
[0]);
2193 printf("Post-remove output reads:\n");
2194 _mesa_print_program(shader
->Programs
[0]);