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
34 #include "prog_parameter.h"
35 #include "grammar_mesa.h"
36 #include "slang_codegen.h"
37 #include "slang_compile.h"
38 #include "slang_preprocess.h"
39 #include "slang_storage.h"
40 #include "slang_emit.h"
41 #include "slang_log.h"
42 #include "slang_vartable.h"
43 #include "slang_simplify.h"
45 #include "slang_print.h"
48 * This is a straightforward implementation of the slang front-end
49 * compiler. Lots of error-checking functionality is missing but
50 * every well-formed shader source should compile successfully and
51 * execute as expected. However, some semantically ill-formed shaders
52 * may be accepted resulting in undefined behaviour.
58 * Allocate storage for a variable of 'size' bytes from given pool.
59 * Return the allocated address for the variable.
62 slang_var_pool_alloc(slang_var_pool
* pool
, unsigned int size
)
64 const GLuint addr
= pool
->next_addr
;
65 pool
->next_addr
+= size
;
74 _slang_code_unit_ctr(slang_code_unit
* self
,
75 struct slang_code_object_
* object
)
77 _slang_variable_scope_ctr(&self
->vars
);
78 _slang_function_scope_ctr(&self
->funs
);
79 _slang_struct_scope_ctr(&self
->structs
);
80 self
->object
= object
;
84 _slang_code_unit_dtr(slang_code_unit
* self
)
86 slang_variable_scope_destruct(&self
->vars
);
87 slang_function_scope_destruct(&self
->funs
);
88 slang_struct_scope_destruct(&self
->structs
);
96 _slang_code_object_ctr(slang_code_object
* self
)
100 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
101 _slang_code_unit_ctr(&self
->builtin
[i
], self
);
102 _slang_code_unit_ctr(&self
->unit
, self
);
103 self
->varpool
.next_addr
= 0;
104 slang_atom_pool_construct(&self
->atompool
);
108 _slang_code_object_dtr(slang_code_object
* self
)
112 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
113 _slang_code_unit_dtr(&self
->builtin
[i
]);
114 _slang_code_unit_dtr(&self
->unit
);
115 slang_atom_pool_destruct(&self
->atompool
);
119 /* slang_parse_ctx */
121 typedef struct slang_parse_ctx_
126 GLboolean global_scope
; /**< Is object being declared a global? */
127 slang_atom_pool
*atoms
;
128 slang_unit_type type
; /**< Vertex vs. Fragment */
131 /* slang_output_ctx */
133 typedef struct slang_output_ctx_
135 slang_variable_scope
*vars
;
136 slang_function_scope
*funs
;
137 slang_struct_scope
*structs
;
138 slang_var_pool
*global_pool
;
139 struct gl_program
*program
;
140 slang_var_table
*vartable
;
143 /* _slang_compile() */
146 parse_identifier_str(slang_parse_ctx
* C
, char **id
)
149 C
->I
+= _mesa_strlen(*id
) + 1;
153 parse_identifier(slang_parse_ctx
* C
)
157 id
= (const char *) C
->I
;
158 C
->I
+= _mesa_strlen(id
) + 1;
159 return slang_atom_pool_atom(C
->atoms
, id
);
163 parse_number(slang_parse_ctx
* C
, int *number
)
165 const int radix
= (int) (*C
->I
++);
167 while (*C
->I
!= '\0') {
169 if (*C
->I
>= '0' && *C
->I
<= '9')
170 digit
= (int) (*C
->I
- '0');
171 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
172 digit
= (int) (*C
->I
- 'A') + 10;
174 digit
= (int) (*C
->I
- 'a') + 10;
175 *number
= *number
* radix
+ digit
;
180 slang_info_log_warning(C
->L
, "%d: literal integer overflow.", *number
);
185 parse_float(slang_parse_ctx
* C
, float *number
)
187 char *integral
= NULL
;
188 char *fractional
= NULL
;
189 char *exponent
= NULL
;
192 parse_identifier_str(C
, &integral
);
193 parse_identifier_str(C
, &fractional
);
194 parse_identifier_str(C
, &exponent
);
196 whole
= (char *) (slang_alloc_malloc((_mesa_strlen(integral
) +
197 _mesa_strlen(fractional
) +
198 _mesa_strlen(exponent
) + 3) * sizeof(char)));
200 slang_info_log_memory(C
->L
);
204 slang_string_copy(whole
, integral
);
205 slang_string_concat(whole
, ".");
206 slang_string_concat(whole
, fractional
);
207 slang_string_concat(whole
, "E");
208 slang_string_concat(whole
, exponent
);
210 *number
= (float) (_mesa_strtod(whole
, (char **) NULL
));
212 slang_alloc_free(whole
);
216 /* revision number - increment after each change affecting emitted output */
220 check_revision(slang_parse_ctx
* C
)
222 if (*C
->I
!= REVISION
) {
223 slang_info_log_error(C
->L
, "Internal compiler error.");
230 static int parse_statement(slang_parse_ctx
*, slang_output_ctx
*,
232 static int parse_expression(slang_parse_ctx
*, slang_output_ctx
*,
234 static int parse_type_specifier(slang_parse_ctx
*, slang_output_ctx
*,
235 slang_type_specifier
*);
238 parse_array_len(slang_parse_ctx
* C
, slang_output_ctx
* O
, GLuint
* len
)
240 slang_operation array_size
;
241 slang_name_space space
;
244 if (!slang_operation_construct(&array_size
))
246 if (!parse_expression(C
, O
, &array_size
)) {
247 slang_operation_destruct(&array_size
);
251 space
.funcs
= O
->funs
;
252 space
.structs
= O
->structs
;
253 space
.vars
= O
->vars
;
255 /* evaluate compile-time expression which is array size */
256 _slang_simplify(&array_size
, &space
, C
->atoms
);
257 result
= (array_size
.type
== SLANG_OPER_LITERAL_INT
);
259 *len
= (GLint
) array_size
.literal
[0];
261 slang_operation_destruct(&array_size
);
266 calculate_var_size(slang_parse_ctx
* C
, slang_output_ctx
* O
,
267 slang_variable
* var
)
269 slang_storage_aggregate agg
;
271 if (!slang_storage_aggregate_construct(&agg
))
273 if (!_slang_aggregate_variable(&agg
, &var
->type
.specifier
, var
->array_len
,
274 O
->funs
, O
->structs
, O
->vars
, C
->atoms
)) {
275 slang_storage_aggregate_destruct(&agg
);
278 var
->size
= _slang_sizeof_aggregate(&agg
);
279 slang_storage_aggregate_destruct(&agg
);
284 convert_to_array(slang_parse_ctx
* C
, slang_variable
* var
,
285 const slang_type_specifier
* sp
)
287 /* sized array - mark it as array, copy the specifier to the array element and
288 * parse the expression */
289 var
->type
.specifier
.type
= SLANG_SPEC_ARRAY
;
290 var
->type
.specifier
._array
= (slang_type_specifier
*)
291 slang_alloc_malloc(sizeof(slang_type_specifier
));
292 if (var
->type
.specifier
._array
== NULL
) {
293 slang_info_log_memory(C
->L
);
296 slang_type_specifier_ctr(var
->type
.specifier
._array
);
297 return slang_type_specifier_copy(var
->type
.specifier
._array
, sp
);
300 /* structure field */
303 #define FIELD_ARRAY 2
306 parse_struct_field_var(slang_parse_ctx
* C
, slang_output_ctx
* O
,
307 slang_variable
* var
, const slang_type_specifier
* sp
)
309 var
->a_name
= parse_identifier(C
);
310 if (var
->a_name
== SLANG_ATOM_NULL
)
315 if (!slang_type_specifier_copy(&var
->type
.specifier
, sp
))
319 if (!convert_to_array(C
, var
, sp
))
321 if (!parse_array_len(C
, O
, &var
->array_len
))
328 return calculate_var_size(C
, O
, var
);
332 parse_struct_field(slang_parse_ctx
* C
, slang_output_ctx
* O
,
333 slang_struct
* st
, slang_type_specifier
* sp
)
335 slang_output_ctx o
= *O
;
337 o
.structs
= st
->structs
;
338 if (!parse_type_specifier(C
, &o
, sp
))
342 slang_variable
*var
= slang_variable_scope_grow(st
->fields
);
344 slang_info_log_memory(C
->L
);
347 if (!parse_struct_field_var(C
, &o
, var
, sp
))
350 while (*C
->I
++ != FIELD_NONE
);
356 parse_struct(slang_parse_ctx
* C
, slang_output_ctx
* O
, slang_struct
** st
)
361 /* parse struct name (if any) and make sure it is unique in current scope */
362 a_name
= parse_identifier(C
);
363 if (a_name
== SLANG_ATOM_NULL
)
366 name
= slang_atom_pool_id(C
->atoms
, a_name
);
368 && slang_struct_scope_find(O
->structs
, a_name
, 0) != NULL
) {
369 slang_info_log_error(C
->L
, "%s: duplicate type name.", name
);
373 /* set-up a new struct */
374 *st
= (slang_struct
*) slang_alloc_malloc(sizeof(slang_struct
));
376 slang_info_log_memory(C
->L
);
379 if (!slang_struct_construct(*st
)) {
380 slang_alloc_free(*st
);
382 slang_info_log_memory(C
->L
);
385 (**st
).a_name
= a_name
;
386 (**st
).structs
->outer_scope
= O
->structs
;
388 /* parse individual struct fields */
390 slang_type_specifier sp
;
392 slang_type_specifier_ctr(&sp
);
393 if (!parse_struct_field(C
, O
, *st
, &sp
)) {
394 slang_type_specifier_dtr(&sp
);
397 slang_type_specifier_dtr(&sp
);
399 while (*C
->I
++ != FIELD_NONE
);
401 /* if named struct, copy it to current scope */
402 if (name
[0] != '\0') {
405 O
->structs
->structs
=
406 (slang_struct
*) slang_alloc_realloc(O
->structs
->structs
,
407 O
->structs
->num_structs
*
408 sizeof(slang_struct
),
409 (O
->structs
->num_structs
+
410 1) * sizeof(slang_struct
));
411 if (O
->structs
->structs
== NULL
) {
412 slang_info_log_memory(C
->L
);
415 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
416 if (!slang_struct_construct(s
))
418 O
->structs
->num_structs
++;
419 if (!slang_struct_copy(s
, *st
))
428 #define TYPE_QUALIFIER_NONE 0
429 #define TYPE_QUALIFIER_CONST 1
430 #define TYPE_QUALIFIER_ATTRIBUTE 2
431 #define TYPE_QUALIFIER_VARYING 3
432 #define TYPE_QUALIFIER_UNIFORM 4
433 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
434 #define TYPE_QUALIFIER_FIXEDINPUT 6
437 parse_type_qualifier(slang_parse_ctx
* C
, slang_type_qualifier
* qual
)
440 case TYPE_QUALIFIER_NONE
:
441 *qual
= SLANG_QUAL_NONE
;
443 case TYPE_QUALIFIER_CONST
:
444 *qual
= SLANG_QUAL_CONST
;
446 case TYPE_QUALIFIER_ATTRIBUTE
:
447 *qual
= SLANG_QUAL_ATTRIBUTE
;
449 case TYPE_QUALIFIER_VARYING
:
450 *qual
= SLANG_QUAL_VARYING
;
452 case TYPE_QUALIFIER_UNIFORM
:
453 *qual
= SLANG_QUAL_UNIFORM
;
455 case TYPE_QUALIFIER_FIXEDOUTPUT
:
456 *qual
= SLANG_QUAL_FIXEDOUTPUT
;
458 case TYPE_QUALIFIER_FIXEDINPUT
:
459 *qual
= SLANG_QUAL_FIXEDINPUT
;
468 #define TYPE_SPECIFIER_VOID 0
469 #define TYPE_SPECIFIER_BOOL 1
470 #define TYPE_SPECIFIER_BVEC2 2
471 #define TYPE_SPECIFIER_BVEC3 3
472 #define TYPE_SPECIFIER_BVEC4 4
473 #define TYPE_SPECIFIER_INT 5
474 #define TYPE_SPECIFIER_IVEC2 6
475 #define TYPE_SPECIFIER_IVEC3 7
476 #define TYPE_SPECIFIER_IVEC4 8
477 #define TYPE_SPECIFIER_FLOAT 9
478 #define TYPE_SPECIFIER_VEC2 10
479 #define TYPE_SPECIFIER_VEC3 11
480 #define TYPE_SPECIFIER_VEC4 12
481 #define TYPE_SPECIFIER_MAT2 13
482 #define TYPE_SPECIFIER_MAT3 14
483 #define TYPE_SPECIFIER_MAT4 15
484 #define TYPE_SPECIFIER_SAMPLER1D 16
485 #define TYPE_SPECIFIER_SAMPLER2D 17
486 #define TYPE_SPECIFIER_SAMPLER3D 18
487 #define TYPE_SPECIFIER_SAMPLERCUBE 19
488 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
489 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
490 #define TYPE_SPECIFIER_SAMPLER2DRECT 22
491 #define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
492 #define TYPE_SPECIFIER_STRUCT 24
493 #define TYPE_SPECIFIER_TYPENAME 25
496 parse_type_specifier(slang_parse_ctx
* C
, slang_output_ctx
* O
,
497 slang_type_specifier
* spec
)
500 case TYPE_SPECIFIER_VOID
:
501 spec
->type
= SLANG_SPEC_VOID
;
503 case TYPE_SPECIFIER_BOOL
:
504 spec
->type
= SLANG_SPEC_BOOL
;
506 case TYPE_SPECIFIER_BVEC2
:
507 spec
->type
= SLANG_SPEC_BVEC2
;
509 case TYPE_SPECIFIER_BVEC3
:
510 spec
->type
= SLANG_SPEC_BVEC3
;
512 case TYPE_SPECIFIER_BVEC4
:
513 spec
->type
= SLANG_SPEC_BVEC4
;
515 case TYPE_SPECIFIER_INT
:
516 spec
->type
= SLANG_SPEC_INT
;
518 case TYPE_SPECIFIER_IVEC2
:
519 spec
->type
= SLANG_SPEC_IVEC2
;
521 case TYPE_SPECIFIER_IVEC3
:
522 spec
->type
= SLANG_SPEC_IVEC3
;
524 case TYPE_SPECIFIER_IVEC4
:
525 spec
->type
= SLANG_SPEC_IVEC4
;
527 case TYPE_SPECIFIER_FLOAT
:
528 spec
->type
= SLANG_SPEC_FLOAT
;
530 case TYPE_SPECIFIER_VEC2
:
531 spec
->type
= SLANG_SPEC_VEC2
;
533 case TYPE_SPECIFIER_VEC3
:
534 spec
->type
= SLANG_SPEC_VEC3
;
536 case TYPE_SPECIFIER_VEC4
:
537 spec
->type
= SLANG_SPEC_VEC4
;
539 case TYPE_SPECIFIER_MAT2
:
540 spec
->type
= SLANG_SPEC_MAT2
;
542 case TYPE_SPECIFIER_MAT3
:
543 spec
->type
= SLANG_SPEC_MAT3
;
545 case TYPE_SPECIFIER_MAT4
:
546 spec
->type
= SLANG_SPEC_MAT4
;
548 case TYPE_SPECIFIER_SAMPLER1D
:
549 spec
->type
= SLANG_SPEC_SAMPLER1D
;
551 case TYPE_SPECIFIER_SAMPLER2D
:
552 spec
->type
= SLANG_SPEC_SAMPLER2D
;
554 case TYPE_SPECIFIER_SAMPLER3D
:
555 spec
->type
= SLANG_SPEC_SAMPLER3D
;
557 case TYPE_SPECIFIER_SAMPLERCUBE
:
558 spec
->type
= SLANG_SPEC_SAMPLERCUBE
;
560 case TYPE_SPECIFIER_SAMPLER2DRECT
:
561 spec
->type
= SLANG_SPEC_SAMPLER2DRECT
;
563 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
564 spec
->type
= SLANG_SPEC_SAMPLER1DSHADOW
;
566 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
567 spec
->type
= SLANG_SPEC_SAMPLER2DSHADOW
;
569 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
570 spec
->type
= SLANG_SPEC_SAMPLER2DRECTSHADOW
;
572 case TYPE_SPECIFIER_STRUCT
:
573 spec
->type
= SLANG_SPEC_STRUCT
;
574 if (!parse_struct(C
, O
, &spec
->_struct
))
577 case TYPE_SPECIFIER_TYPENAME
:
578 spec
->type
= SLANG_SPEC_STRUCT
;
583 a_name
= parse_identifier(C
);
587 stru
= slang_struct_scope_find(O
->structs
, a_name
, 1);
589 slang_info_log_error(C
->L
, "undeclared type name '%s'",
590 slang_atom_pool_id(C
->atoms
, a_name
));
595 (slang_struct
*) slang_alloc_malloc(sizeof(slang_struct
));
596 if (spec
->_struct
== NULL
) {
597 slang_info_log_memory(C
->L
);
600 if (!slang_struct_construct(spec
->_struct
)) {
601 slang_alloc_free(spec
->_struct
);
602 spec
->_struct
= NULL
;
605 if (!slang_struct_copy(spec
->_struct
, stru
))
616 parse_fully_specified_type(slang_parse_ctx
* C
, slang_output_ctx
* O
,
617 slang_fully_specified_type
* type
)
619 if (!parse_type_qualifier(C
, &type
->qualifier
))
621 if (!parse_type_specifier(C
, O
, &type
->specifier
))
628 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
629 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
633 #define OP_CONTINUE 6
636 #define OP_EXPRESSION 9
641 #define OP_PUSH_VOID 14
642 #define OP_PUSH_BOOL 15
643 #define OP_PUSH_INT 16
644 #define OP_PUSH_FLOAT 17
645 #define OP_PUSH_IDENTIFIER 18
646 #define OP_SEQUENCE 19
648 #define OP_ADDASSIGN 21
649 #define OP_SUBASSIGN 22
650 #define OP_MULASSIGN 23
651 #define OP_DIVASSIGN 24
652 /*#define OP_MODASSIGN 25*/
653 /*#define OP_LSHASSIGN 26*/
654 /*#define OP_RSHASSIGN 27*/
655 /*#define OP_ORASSIGN 28*/
656 /*#define OP_XORASSIGN 29*/
657 /*#define OP_ANDASSIGN 30*/
659 #define OP_LOGICALOR 32
660 #define OP_LOGICALXOR 33
661 #define OP_LOGICALAND 34
662 /*#define OP_BITOR 35*/
663 /*#define OP_BITXOR 36*/
664 /*#define OP_BITAND 37*/
666 #define OP_NOTEQUAL 39
668 #define OP_GREATER 41
669 #define OP_LESSEQUAL 42
670 #define OP_GREATEREQUAL 43
671 /*#define OP_LSHIFT 44*/
672 /*#define OP_RSHIFT 45*/
674 #define OP_SUBTRACT 47
675 #define OP_MULTIPLY 48
677 /*#define OP_MODULUS 50*/
678 #define OP_PREINCREMENT 51
679 #define OP_PREDECREMENT 52
682 /*#define OP_COMPLEMENT 55*/
684 #define OP_SUBSCRIPT 57
687 #define OP_POSTINCREMENT 60
688 #define OP_POSTDECREMENT 61
692 * When parsing a compound production, this function is used to parse the
694 * For example, a while-loop compound will have two children, the
695 * while condition expression and the loop body. So, this function will
696 * be called twice to parse those two sub-expressions.
697 * \param C the parsing context
698 * \param O the output context
699 * \param oper the operation we're parsing
700 * \param statement indicates whether parsing a statement, or expression
701 * \return 1 if success, 0 if error
704 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
705 slang_operation
* oper
, GLboolean statement
)
709 /* grow child array */
710 ch
= slang_operation_grow(&oper
->num_children
, &oper
->children
);
712 return parse_statement(C
, O
, ch
);
713 return parse_expression(C
, O
, ch
);
716 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
719 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
720 slang_operation
* oper
)
722 oper
->locals
->outer_scope
= O
->vars
;
724 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
725 /* parse child statements, do not create new variable scope */
726 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
727 while (*C
->I
!= OP_END
)
728 if (!parse_child_operation(C
, O
, oper
, 1))
732 case OP_BLOCK_BEGIN_NEW_SCOPE
:
733 /* parse child statements, create new variable scope */
735 slang_output_ctx o
= *O
;
737 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
738 o
.vars
= oper
->locals
;
739 while (*C
->I
!= OP_END
)
740 if (!parse_child_operation(C
, &o
, oper
, 1))
746 /* local variable declaration, individual declarators are stored as
747 * children identifiers
749 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
751 const unsigned int first_var
= O
->vars
->num_variables
;
753 /* parse the declaration, note that there can be zero or more
754 * than one declarators
756 if (!parse_declaration(C
, O
))
758 if (first_var
< O
->vars
->num_variables
) {
759 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
761 assert(oper
->num_children
== 0);
762 oper
->num_children
= num_vars
;
763 oper
->children
= slang_operation_new(num_vars
);
764 if (oper
->children
== NULL
) {
765 slang_info_log_memory(C
->L
);
768 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
769 slang_operation
*o
= &oper
->children
[i
- first_var
];
770 o
->type
= SLANG_OPER_VARIABLE_DECL
;
771 o
->locals
->outer_scope
= O
->vars
;
772 o
->a_id
= O
->vars
->variables
[i
]->a_name
;
778 /* the __asm statement, parse the mnemonic and all its arguments
781 oper
->type
= SLANG_OPER_ASM
;
782 oper
->a_id
= parse_identifier(C
);
783 if (oper
->a_id
== SLANG_ATOM_NULL
)
785 while (*C
->I
!= OP_END
) {
786 if (!parse_child_operation(C
, O
, oper
, 0))
792 oper
->type
= SLANG_OPER_BREAK
;
795 oper
->type
= SLANG_OPER_CONTINUE
;
798 oper
->type
= SLANG_OPER_DISCARD
;
801 oper
->type
= SLANG_OPER_RETURN
;
802 if (!parse_child_operation(C
, O
, oper
, 0))
806 oper
->type
= SLANG_OPER_EXPRESSION
;
807 if (!parse_child_operation(C
, O
, oper
, 0))
811 oper
->type
= SLANG_OPER_IF
;
812 if (!parse_child_operation(C
, O
, oper
, 0))
814 if (!parse_child_operation(C
, O
, oper
, 1))
816 if (!parse_child_operation(C
, O
, oper
, 1))
821 slang_output_ctx o
= *O
;
823 oper
->type
= SLANG_OPER_WHILE
;
824 o
.vars
= oper
->locals
;
825 if (!parse_child_operation(C
, &o
, oper
, 1))
827 if (!parse_child_operation(C
, &o
, oper
, 1))
832 oper
->type
= SLANG_OPER_DO
;
833 if (!parse_child_operation(C
, O
, oper
, 1))
835 if (!parse_child_operation(C
, O
, oper
, 0))
840 slang_output_ctx o
= *O
;
842 oper
->type
= SLANG_OPER_FOR
;
843 o
.vars
= oper
->locals
;
844 if (!parse_child_operation(C
, &o
, oper
, 1))
846 if (!parse_child_operation(C
, &o
, oper
, 1))
848 if (!parse_child_operation(C
, &o
, oper
, 0))
850 if (!parse_child_operation(C
, &o
, oper
, 1))
861 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
862 slang_operation
** ops
, unsigned int *total_ops
,
867 op
->children
= slang_operation_new(n
);
868 if (op
->children
== NULL
) {
869 slang_info_log_memory(C
->L
);
872 op
->num_children
= n
;
874 for (i
= 0; i
< n
; i
++) {
875 slang_operation_destruct(&op
->children
[i
]);
876 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
879 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
882 *ops
= (slang_operation
*)
883 slang_alloc_realloc(*ops
,
884 (*total_ops
+ n
) * sizeof(slang_operation
),
885 *total_ops
* sizeof(slang_operation
));
887 slang_info_log_memory(C
->L
);
894 is_constructor_name(const char *name
, slang_atom a_name
,
895 slang_struct_scope
* structs
)
897 if (slang_type_specifier_type_from_string(name
) != SLANG_SPEC_VOID
)
899 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
903 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
904 slang_operation
* oper
)
906 slang_operation
*ops
= NULL
;
907 unsigned int num_ops
= 0;
910 while (*C
->I
!= OP_END
) {
912 const unsigned int op_code
= *C
->I
++;
914 /* allocate default operation, becomes a no-op if not used */
915 ops
= (slang_operation
*)
916 slang_alloc_realloc(ops
,
917 num_ops
* sizeof(slang_operation
),
918 (num_ops
+ 1) * sizeof(slang_operation
));
920 slang_info_log_memory(C
->L
);
924 if (!slang_operation_construct(op
)) {
925 slang_info_log_memory(C
->L
);
929 op
->locals
->outer_scope
= O
->vars
;
933 op
->type
= SLANG_OPER_VOID
;
936 op
->type
= SLANG_OPER_LITERAL_BOOL
;
937 if (!parse_number(C
, &number
))
942 op
->literal
[3] = (GLfloat
) number
;
943 op
->literal_size
= 1;
946 op
->type
= SLANG_OPER_LITERAL_INT
;
947 if (!parse_number(C
, &number
))
952 op
->literal
[3] = (GLfloat
) number
;
953 op
->literal_size
= 1;
956 op
->type
= SLANG_OPER_LITERAL_FLOAT
;
957 if (!parse_float(C
, &op
->literal
[0]))
961 op
->literal
[3] = op
->literal
[0];
962 op
->literal_size
= 1;
964 case OP_PUSH_IDENTIFIER
:
965 op
->type
= SLANG_OPER_IDENTIFIER
;
966 op
->a_id
= parse_identifier(C
);
967 if (op
->a_id
== SLANG_ATOM_NULL
)
971 op
->type
= SLANG_OPER_SEQUENCE
;
972 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
976 op
->type
= SLANG_OPER_ASSIGN
;
977 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
981 op
->type
= SLANG_OPER_ADDASSIGN
;
982 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
986 op
->type
= SLANG_OPER_SUBASSIGN
;
987 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
991 op
->type
= SLANG_OPER_MULASSIGN
;
992 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
996 op
->type
= SLANG_OPER_DIVASSIGN
;
997 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1000 /*case OP_MODASSIGN: */
1001 /*case OP_LSHASSIGN: */
1002 /*case OP_RSHASSIGN: */
1003 /*case OP_ORASSIGN: */
1004 /*case OP_XORASSIGN: */
1005 /*case OP_ANDASSIGN: */
1007 op
->type
= SLANG_OPER_SELECT
;
1008 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1012 op
->type
= SLANG_OPER_LOGICALOR
;
1013 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1017 op
->type
= SLANG_OPER_LOGICALXOR
;
1018 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1022 op
->type
= SLANG_OPER_LOGICALAND
;
1023 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1027 /*case OP_BITXOR: */
1028 /*case OP_BITAND: */
1030 op
->type
= SLANG_OPER_EQUAL
;
1031 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1035 op
->type
= SLANG_OPER_NOTEQUAL
;
1036 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1040 op
->type
= SLANG_OPER_LESS
;
1041 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1045 op
->type
= SLANG_OPER_GREATER
;
1046 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1050 op
->type
= SLANG_OPER_LESSEQUAL
;
1051 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1054 case OP_GREATEREQUAL
:
1055 op
->type
= SLANG_OPER_GREATEREQUAL
;
1056 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1059 /*case OP_LSHIFT: */
1060 /*case OP_RSHIFT: */
1062 op
->type
= SLANG_OPER_ADD
;
1063 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1067 op
->type
= SLANG_OPER_SUBTRACT
;
1068 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1072 op
->type
= SLANG_OPER_MULTIPLY
;
1073 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1077 op
->type
= SLANG_OPER_DIVIDE
;
1078 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1081 /*case OP_MODULUS: */
1082 case OP_PREINCREMENT
:
1083 op
->type
= SLANG_OPER_PREINCREMENT
;
1084 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1087 case OP_PREDECREMENT
:
1088 op
->type
= SLANG_OPER_PREDECREMENT
;
1089 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1093 op
->type
= SLANG_OPER_PLUS
;
1094 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1098 op
->type
= SLANG_OPER_MINUS
;
1099 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1103 op
->type
= SLANG_OPER_NOT
;
1104 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1107 /*case OP_COMPLEMENT: */
1109 op
->type
= SLANG_OPER_SUBSCRIPT
;
1110 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1114 op
->type
= SLANG_OPER_CALL
;
1115 op
->a_id
= parse_identifier(C
);
1116 if (op
->a_id
== SLANG_ATOM_NULL
)
1118 while (*C
->I
!= OP_END
)
1119 if (!parse_child_operation(C
, O
, op
, 0))
1123 if (!C
->parsing_builtin
1124 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1127 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1128 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1129 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1135 op
->type
= SLANG_OPER_FIELD
;
1136 op
->a_id
= parse_identifier(C
);
1137 if (op
->a_id
== SLANG_ATOM_NULL
)
1139 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1142 case OP_POSTINCREMENT
:
1143 op
->type
= SLANG_OPER_POSTINCREMENT
;
1144 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1147 case OP_POSTDECREMENT
:
1148 op
->type
= SLANG_OPER_POSTDECREMENT
;
1149 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1158 slang_operation_destruct(oper
);
1159 *oper
= *ops
; /* struct copy */
1160 slang_alloc_free(ops
);
1165 /* parameter qualifier */
1166 #define PARAM_QUALIFIER_IN 0
1167 #define PARAM_QUALIFIER_OUT 1
1168 #define PARAM_QUALIFIER_INOUT 2
1170 /* function parameter array presence */
1171 #define PARAMETER_ARRAY_NOT_PRESENT 0
1172 #define PARAMETER_ARRAY_PRESENT 1
1175 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1176 slang_variable
* param
)
1178 /* parse and validate the parameter's type qualifiers (there can be
1179 * two at most) because not all combinations are valid
1181 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1184 case PARAM_QUALIFIER_IN
:
1185 if (param
->type
.qualifier
!= SLANG_QUAL_CONST
1186 && param
->type
.qualifier
!= SLANG_QUAL_NONE
) {
1187 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1191 case PARAM_QUALIFIER_OUT
:
1192 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1193 param
->type
.qualifier
= SLANG_QUAL_OUT
;
1195 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1199 case PARAM_QUALIFIER_INOUT
:
1200 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1201 param
->type
.qualifier
= SLANG_QUAL_INOUT
;
1203 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1211 /* parse parameter's type specifier and name */
1212 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1214 param
->a_name
= parse_identifier(C
);
1215 if (param
->a_name
== SLANG_ATOM_NULL
)
1218 /* if the parameter is an array, parse its size (the size must be
1219 * explicitly defined
1221 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1222 slang_type_specifier p
;
1224 slang_type_specifier_ctr(&p
);
1225 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1226 slang_type_specifier_dtr(&p
);
1229 if (!convert_to_array(C
, param
, &p
)) {
1230 slang_type_specifier_dtr(&p
);
1233 slang_type_specifier_dtr(&p
);
1234 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1238 /* calculate the parameter size */
1239 if (!calculate_var_size(C
, O
, param
))
1242 /* TODO: allocate the local address here? */
1247 #define FUNCTION_ORDINARY 0
1248 #define FUNCTION_CONSTRUCTOR 1
1249 #define FUNCTION_OPERATOR 2
1251 /* function parameter */
1252 #define PARAMETER_NONE 0
1253 #define PARAMETER_NEXT 1
1256 #define OPERATOR_ADDASSIGN 1
1257 #define OPERATOR_SUBASSIGN 2
1258 #define OPERATOR_MULASSIGN 3
1259 #define OPERATOR_DIVASSIGN 4
1260 /*#define OPERATOR_MODASSIGN 5*/
1261 /*#define OPERATOR_LSHASSIGN 6*/
1262 /*#define OPERATOR_RSHASSIGN 7*/
1263 /*#define OPERATOR_ANDASSIGN 8*/
1264 /*#define OPERATOR_XORASSIGN 9*/
1265 /*#define OPERATOR_ORASSIGN 10*/
1266 #define OPERATOR_LOGICALXOR 11
1267 /*#define OPERATOR_BITOR 12*/
1268 /*#define OPERATOR_BITXOR 13*/
1269 /*#define OPERATOR_BITAND 14*/
1270 #define OPERATOR_LESS 15
1271 #define OPERATOR_GREATER 16
1272 #define OPERATOR_LESSEQUAL 17
1273 #define OPERATOR_GREATEREQUAL 18
1274 /*#define OPERATOR_LSHIFT 19*/
1275 /*#define OPERATOR_RSHIFT 20*/
1276 #define OPERATOR_MULTIPLY 21
1277 #define OPERATOR_DIVIDE 22
1278 /*#define OPERATOR_MODULUS 23*/
1279 #define OPERATOR_INCREMENT 24
1280 #define OPERATOR_DECREMENT 25
1281 #define OPERATOR_PLUS 26
1282 #define OPERATOR_MINUS 27
1283 /*#define OPERATOR_COMPLEMENT 28*/
1284 #define OPERATOR_NOT 29
1288 unsigned int o_code
;
1290 } operator_names
[] = {
1291 {OPERATOR_INCREMENT
, "++"},
1292 {OPERATOR_ADDASSIGN
, "+="},
1293 {OPERATOR_PLUS
, "+"},
1294 {OPERATOR_DECREMENT
, "--"},
1295 {OPERATOR_SUBASSIGN
, "-="},
1296 {OPERATOR_MINUS
, "-"},
1297 {OPERATOR_NOT
, "!"},
1298 {OPERATOR_MULASSIGN
, "*="},
1299 {OPERATOR_MULTIPLY
, "*"},
1300 {OPERATOR_DIVASSIGN
, "/="},
1301 {OPERATOR_DIVIDE
, "/"},
1302 {OPERATOR_LESSEQUAL
, "<="},
1303 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1304 /*{ OPERATOR_LSHIFT, "<<" }, */
1305 {OPERATOR_LESS
, "<"},
1306 {OPERATOR_GREATEREQUAL
, ">="},
1307 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1308 /*{ OPERATOR_RSHIFT, ">>" }, */
1309 {OPERATOR_GREATER
, ">"},
1310 /*{ OPERATOR_MODASSIGN, "%=" }, */
1311 /*{ OPERATOR_MODULUS, "%" }, */
1312 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1313 /*{ OPERATOR_BITAND, "&" }, */
1314 /*{ OPERATOR_ORASSIGN, "|=" }, */
1315 /*{ OPERATOR_BITOR, "|" }, */
1316 /*{ OPERATOR_COMPLEMENT, "~" }, */
1317 /*{ OPERATOR_XORASSIGN, "^=" }, */
1318 {OPERATOR_LOGICALXOR
, "^^"},
1319 /*{ OPERATOR_BITXOR, "^" } */
1323 parse_operator_name(slang_parse_ctx
* C
)
1327 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1328 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1330 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1331 if (atom
== SLANG_ATOM_NULL
) {
1332 slang_info_log_memory(C
->L
);
1343 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1344 slang_function
* func
)
1346 /* parse function type and name */
1347 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1350 case FUNCTION_ORDINARY
:
1351 func
->kind
= SLANG_FUNC_ORDINARY
;
1352 func
->header
.a_name
= parse_identifier(C
);
1353 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1356 case FUNCTION_CONSTRUCTOR
:
1357 func
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1358 if (func
->header
.type
.specifier
.type
== SLANG_SPEC_STRUCT
)
1360 func
->header
.a_name
=
1361 slang_atom_pool_atom(C
->atoms
,
1362 slang_type_specifier_type_to_string
1363 (func
->header
.type
.specifier
.type
));
1364 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1365 slang_info_log_memory(C
->L
);
1369 case FUNCTION_OPERATOR
:
1370 func
->kind
= SLANG_FUNC_OPERATOR
;
1371 func
->header
.a_name
= parse_operator_name(C
);
1372 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1379 /* parse function parameters */
1380 while (*C
->I
++ == PARAMETER_NEXT
) {
1381 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1383 slang_info_log_memory(C
->L
);
1386 if (!parse_parameter_declaration(C
, O
, p
))
1390 /* if the function returns a value, append a hidden __retVal 'out'
1391 * parameter that corresponds to the return value.
1393 if (_slang_function_has_return_value(func
)) {
1394 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1395 slang_atom a_retVal
= slang_atom_pool_atom(C
->atoms
, "__retVal");
1397 p
->a_name
= a_retVal
;
1398 p
->type
= func
->header
.type
;
1399 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1402 /* function formal parameters and local variables share the same
1403 * scope, so save the information about param count in a seperate
1404 * place also link the scope to the global variable scope so when a
1405 * given identifier is not found here, the search process continues
1406 * in the global space
1408 func
->param_count
= func
->parameters
->num_variables
;
1409 func
->parameters
->outer_scope
= O
->vars
;
1415 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1416 slang_function
* func
)
1418 slang_output_ctx o
= *O
;
1420 if (!parse_function_prototype(C
, O
, func
))
1423 /* create function's body operation */
1425 (slang_operation
*) slang_alloc_malloc(sizeof(slang_operation
));
1426 if (func
->body
== NULL
) {
1427 slang_info_log_memory(C
->L
);
1430 if (!slang_operation_construct(func
->body
)) {
1431 slang_alloc_free(func
->body
);
1433 slang_info_log_memory(C
->L
);
1437 /* to parse the body the parse context is modified in order to
1438 * capture parsed variables into function's local variable scope
1440 C
->global_scope
= GL_FALSE
;
1441 o
.vars
= func
->parameters
;
1442 if (!parse_statement(C
, &o
, func
->body
))
1445 C
->global_scope
= GL_TRUE
;
1450 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1452 slang_operation op_id
, op_assign
;
1455 /* construct the left side of assignment */
1456 if (!slang_operation_construct(&op_id
))
1458 op_id
.type
= SLANG_OPER_IDENTIFIER
;
1459 op_id
.a_id
= var
->a_name
;
1461 /* put the variable into operation's scope */
1462 op_id
.locals
->variables
=
1463 (slang_variable
**) slang_alloc_malloc(sizeof(slang_variable
*));
1464 if (op_id
.locals
->variables
== NULL
) {
1465 slang_operation_destruct(&op_id
);
1468 op_id
.locals
->num_variables
= 1;
1469 op_id
.locals
->variables
[0] = var
;
1471 /* construct the assignment expression */
1472 if (!slang_operation_construct(&op_assign
)) {
1473 op_id
.locals
->num_variables
= 0;
1474 slang_operation_destruct(&op_id
);
1477 op_assign
.type
= SLANG_OPER_ASSIGN
;
1478 op_assign
.children
=
1479 (slang_operation
*) slang_alloc_malloc(2 * sizeof(slang_operation
));
1480 if (op_assign
.children
== NULL
) {
1481 slang_operation_destruct(&op_assign
);
1482 op_id
.locals
->num_variables
= 0;
1483 slang_operation_destruct(&op_id
);
1486 op_assign
.num_children
= 2;
1487 op_assign
.children
[0] = op_id
;
1488 op_assign
.children
[1] = *var
->initializer
;
1492 /* carefully destroy the operations */
1493 op_assign
.num_children
= 0;
1494 slang_alloc_free(op_assign
.children
);
1495 op_assign
.children
= NULL
;
1496 slang_operation_destruct(&op_assign
);
1497 op_id
.locals
->num_variables
= 0;
1498 slang_operation_destruct(&op_id
);
1506 /* init declarator list */
1507 #define DECLARATOR_NONE 0
1508 #define DECLARATOR_NEXT 1
1510 /* variable declaration */
1511 #define VARIABLE_NONE 0
1512 #define VARIABLE_IDENTIFIER 1
1513 #define VARIABLE_INITIALIZER 2
1514 #define VARIABLE_ARRAY_EXPLICIT 3
1515 #define VARIABLE_ARRAY_UNKNOWN 4
1519 * Parse the initializer for a variable declaration.
1522 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1523 const slang_fully_specified_type
* type
)
1525 slang_variable
*var
;
1527 /* empty init declatator (without name, e.g. "float ;") */
1528 if (*C
->I
++ == VARIABLE_NONE
)
1531 /* make room for the new variable and initialize it */
1532 var
= slang_variable_scope_grow(O
->vars
);
1534 slang_info_log_memory(C
->L
);
1538 /* copy the declarator qualifier type, parse the identifier */
1539 var
->type
.qualifier
= type
->qualifier
;
1540 var
->a_name
= parse_identifier(C
);
1541 if (var
->a_name
== SLANG_ATOM_NULL
)
1546 /* simple variable declarator - just copy the specifier */
1547 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1550 case VARIABLE_INITIALIZER
:
1551 /* initialized variable - copy the specifier and parse the expression */
1552 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1555 (slang_operation
*) slang_alloc_malloc(sizeof(slang_operation
));
1556 if (var
->initializer
== NULL
) {
1557 slang_info_log_memory(C
->L
);
1560 if (!slang_operation_construct(var
->initializer
)) {
1561 slang_alloc_free(var
->initializer
);
1562 var
->initializer
= NULL
;
1563 slang_info_log_memory(C
->L
);
1566 if (!parse_expression(C
, O
, var
->initializer
))
1569 case VARIABLE_ARRAY_UNKNOWN
:
1570 /* unsized array - mark it as array and copy the specifier to
1573 if (!convert_to_array(C
, var
, &type
->specifier
))
1576 case VARIABLE_ARRAY_EXPLICIT
:
1577 if (!convert_to_array(C
, var
, &type
->specifier
))
1579 if (!parse_array_len(C
, O
, &var
->array_len
))
1586 /* emit code for global var decl */
1587 if (C
->global_scope
) {
1588 slang_assemble_ctx A
;
1590 A
.space
.funcs
= O
->funs
;
1591 A
.space
.structs
= O
->structs
;
1592 A
.space
.vars
= O
->vars
;
1593 A
.program
= O
->program
;
1594 A
.vartable
= O
->vartable
;
1595 _slang_codegen_global_variable(&A
, var
, C
->type
);
1598 /* allocate global address space for a variable with a known size */
1600 && !(var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
1601 && var
->array_len
== 0)) {
1602 if (!calculate_var_size(C
, O
, var
))
1604 var
->address
= slang_var_pool_alloc(O
->global_pool
, var
->size
);
1607 /* initialize global variable */
1608 if (C
->global_scope
) {
1609 if (var
->initializer
!= NULL
) {
1610 slang_assemble_ctx A
;
1613 A
.space
.funcs
= O
->funs
;
1614 A
.space
.structs
= O
->structs
;
1615 A
.space
.vars
= O
->vars
;
1616 if (!initialize_global(&A
, var
))
1624 * Parse a list of variable declarations. Each variable may have an
1628 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1630 slang_fully_specified_type type
;
1632 /* parse the fully specified type, common to all declarators */
1633 if (!slang_fully_specified_type_construct(&type
))
1635 if (!parse_fully_specified_type(C
, O
, &type
)) {
1636 slang_fully_specified_type_destruct(&type
);
1640 /* parse declarators, pass-in the parsed type */
1642 if (!parse_init_declarator(C
, O
, &type
)) {
1643 slang_fully_specified_type_destruct(&type
);
1647 while (*C
->I
++ == DECLARATOR_NEXT
);
1649 slang_fully_specified_type_destruct(&type
);
1655 * Parse a function definition or declaration.
1656 * \param C parsing context
1657 * \param O output context
1658 * \param definition if non-zero expect a definition, else a declaration
1659 * \param parsed_func_ret returns the parsed function
1660 * \return GL_TRUE if success, GL_FALSE if failure
1663 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
1664 slang_function
** parsed_func_ret
)
1666 slang_function parsed_func
, *found_func
;
1668 /* parse function definition/declaration */
1669 if (!slang_function_construct(&parsed_func
))
1672 if (!parse_function_definition(C
, O
, &parsed_func
)) {
1673 slang_function_destruct(&parsed_func
);
1678 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
1679 slang_function_destruct(&parsed_func
);
1684 /* find a function with a prototype matching the parsed one - only
1685 * the current scope is being searched to allow built-in function
1688 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
1689 if (found_func
== NULL
) {
1690 /* New function, add it to the function list */
1691 O
->funs
->functions
=
1692 (slang_function
*) slang_alloc_realloc(O
->funs
->functions
,
1693 O
->funs
->num_functions
*
1694 sizeof(slang_function
),
1695 (O
->funs
->num_functions
+
1696 1) * sizeof(slang_function
));
1697 if (O
->funs
->functions
== NULL
) {
1698 slang_info_log_memory(C
->L
);
1699 slang_function_destruct(&parsed_func
);
1702 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
1703 O
->funs
->num_functions
++;
1705 /* return the newly parsed function */
1706 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
1709 /* previously defined or declared */
1710 /* TODO: check function return type qualifiers and specifiers */
1712 if (found_func
->body
!= NULL
) {
1713 slang_info_log_error(C
->L
, "%s: function already has a body.",
1714 slang_atom_pool_id(C
->atoms
,
1717 slang_function_destruct(&parsed_func
);
1721 /* destroy the existing function declaration and replace it
1722 * with the new one, remember to save the fixup table
1724 parsed_func
.fixups
= found_func
->fixups
;
1725 slang_fixup_table_init(&found_func
->fixups
);
1726 slang_function_destruct(found_func
);
1727 *found_func
= parsed_func
;
1730 /* another declaration of the same function prototype - ignore it */
1731 slang_function_destruct(&parsed_func
);
1734 /* return the found function */
1735 *parsed_func_ret
= found_func
;
1738 /* assemble the parsed function */
1740 slang_assemble_ctx A
;
1743 A
.space
.funcs
= O
->funs
;
1744 A
.space
.structs
= O
->structs
;
1745 A
.space
.vars
= O
->vars
;
1746 A
.program
= O
->program
;
1747 A
.vartable
= O
->vartable
;
1750 _slang_codegen_function(&A
, *parsed_func_ret
);
1756 #define DECLARATION_FUNCTION_PROTOTYPE 1
1757 #define DECLARATION_INIT_DECLARATOR_LIST 2
1760 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1763 case DECLARATION_INIT_DECLARATOR_LIST
:
1764 if (!parse_init_declarator_list(C
, O
))
1767 case DECLARATION_FUNCTION_PROTOTYPE
:
1769 slang_function
*dummy_func
;
1771 if (!parse_function(C
, O
, 0, &dummy_func
))
1781 /* external declaration */
1782 #define EXTERNAL_NULL 0
1783 #define EXTERNAL_FUNCTION_DEFINITION 1
1784 #define EXTERNAL_DECLARATION 2
1787 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
,
1788 struct gl_program
*program
)
1790 GET_CURRENT_CONTEXT(ctx
);
1795 if (unit
->type
== SLANG_UNIT_FRAGMENT_BUILTIN
||
1796 unit
->type
== SLANG_UNIT_FRAGMENT_SHADER
) {
1797 maxRegs
= ctx
->Const
.FragmentProgram
.MaxTemps
;
1800 assert(unit
->type
== SLANG_UNIT_VERTEX_BUILTIN
||
1801 unit
->type
== SLANG_UNIT_VERTEX_SHADER
);
1802 maxRegs
= ctx
->Const
.VertexProgram
.MaxTemps
;
1805 /* setup output context */
1806 o
.funs
= &unit
->funs
;
1807 o
.structs
= &unit
->structs
;
1808 o
.vars
= &unit
->vars
;
1809 o
.global_pool
= &unit
->object
->varpool
;
1810 o
.program
= program
;
1811 o
.vartable
= _slang_new_var_table(maxRegs
);
1812 _slang_push_var_table(o
.vartable
);
1814 /* parse individual functions and declarations */
1815 while (*C
->I
!= EXTERNAL_NULL
) {
1817 case EXTERNAL_FUNCTION_DEFINITION
:
1819 slang_function
*func
;
1820 success
= parse_function(C
, &o
, 1, &func
);
1823 case EXTERNAL_DECLARATION
:
1824 success
= parse_declaration(C
, &o
);
1831 /* xxx free codegen */
1832 _slang_pop_var_table(o
.vartable
);
1838 _slang_pop_var_table(o
.vartable
);
1843 compile_binary(const byte
* prod
, slang_code_unit
* unit
,
1844 slang_unit_type type
, slang_info_log
* infolog
,
1845 slang_code_unit
* builtin
, slang_code_unit
* downlink
,
1846 struct gl_program
*program
)
1852 /* setup parse context */
1855 C
.parsing_builtin
= (builtin
== NULL
);
1856 C
.global_scope
= GL_TRUE
;
1857 C
.atoms
= &unit
->object
->atompool
;
1860 if (!check_revision(&C
))
1863 if (downlink
!= NULL
) {
1864 unit
->vars
.outer_scope
= &downlink
->vars
;
1865 unit
->funs
.outer_scope
= &downlink
->funs
;
1866 unit
->structs
.outer_scope
= &downlink
->structs
;
1869 /* parse translation unit */
1870 return parse_code_unit(&C
, unit
, program
);
1874 compile_with_grammar(grammar id
, const char *source
, slang_code_unit
* unit
,
1875 slang_unit_type type
, slang_info_log
* infolog
,
1876 slang_code_unit
* builtin
,
1877 struct gl_program
*program
)
1880 GLuint size
, start
, version
;
1881 slang_string preprocessed
;
1883 /* First retrieve the version number. */
1884 if (!_slang_preprocess_version(source
, &version
, &start
, infolog
))
1887 if (version
> 110) {
1888 slang_info_log_error(infolog
,
1889 "language version specified is not supported.");
1893 /* Now preprocess the source string. */
1894 slang_string_init(&preprocessed
);
1895 if (!_slang_preprocess_directives(&preprocessed
, &source
[start
], infolog
)) {
1896 slang_string_free(&preprocessed
);
1897 slang_info_log_error(infolog
, "failed to preprocess the source.");
1901 /* Finally check the syntax and generate its binary representation. */
1902 if (!grammar_fast_check(id
,
1903 (const byte
*) (slang_string_cstr(&preprocessed
)),
1904 &prod
, &size
, 65536)) {
1908 slang_string_free(&preprocessed
);
1909 grammar_get_last_error((byte
*) (buf
), sizeof(buf
), &pos
);
1910 slang_info_log_error(infolog
, buf
);
1911 /* syntax error (possibly in library code) */
1916 s
= (char *) _mesa_find_line_column((const GLubyte
*) source
,
1917 (const GLubyte
*) source
+ pos
,
1919 printf("Error on line %d, col %d: %s\n", line
, col
, s
);
1924 slang_string_free(&preprocessed
);
1926 /* Syntax is okay - translate it to internal representation. */
1927 if (!compile_binary(prod
, unit
, type
, infolog
, builtin
,
1928 &builtin
[SLANG_BUILTIN_TOTAL
- 1],
1930 grammar_alloc_free(prod
);
1933 grammar_alloc_free(prod
);
1937 LONGSTRING
static const char *slang_shader_syn
=
1938 #include "library/slang_shader_syn.h"
1941 static const byte slang_core_gc
[] = {
1942 #include "library/slang_core_gc.h"
1945 static const byte slang_common_builtin_gc
[] = {
1946 #include "library/slang_common_builtin_gc.h"
1949 static const byte slang_fragment_builtin_gc
[] = {
1950 #include "library/slang_fragment_builtin_gc.h"
1953 static const byte slang_vertex_builtin_gc
[] = {
1954 #include "library/slang_vertex_builtin_gc.h"
1958 compile_object(grammar
* id
, const char *source
, slang_code_object
* object
,
1959 slang_unit_type type
, slang_info_log
* infolog
,
1960 struct gl_program
*program
)
1962 slang_code_unit
*builtins
= NULL
;
1964 /* load GLSL grammar */
1965 *id
= grammar_load_from_text((const byte
*) (slang_shader_syn
));
1970 grammar_get_last_error(buf
, 1024, &pos
);
1971 slang_info_log_error(infolog
, (const char *) (buf
));
1975 /* set shader type - the syntax is slightly different for different shaders */
1976 if (type
== SLANG_UNIT_FRAGMENT_SHADER
1977 || type
== SLANG_UNIT_FRAGMENT_BUILTIN
)
1978 grammar_set_reg8(*id
, (const byte
*) "shader_type", 1);
1980 grammar_set_reg8(*id
, (const byte
*) "shader_type", 2);
1982 /* enable language extensions */
1983 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
1985 /* if parsing user-specified shader, load built-in library */
1986 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_VERTEX_SHADER
) {
1987 /* compile core functionality first */
1988 if (!compile_binary(slang_core_gc
,
1989 &object
->builtin
[SLANG_BUILTIN_CORE
],
1990 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
1994 /* compile common functions and variables, link to core */
1995 if (!compile_binary(slang_common_builtin_gc
,
1996 &object
->builtin
[SLANG_BUILTIN_COMMON
],
1997 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
1998 &object
->builtin
[SLANG_BUILTIN_CORE
], NULL
))
2001 /* compile target-specific functions and variables, link to common */
2002 if (type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2003 if (!compile_binary(slang_fragment_builtin_gc
,
2004 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2005 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2006 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2009 else if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2010 if (!compile_binary(slang_vertex_builtin_gc
,
2011 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2012 SLANG_UNIT_VERTEX_BUILTIN
, infolog
, NULL
,
2013 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2017 /* disable language extensions */
2018 #if NEW_SLANG /* allow-built-ins */
2019 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2021 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 0);
2023 builtins
= object
->builtin
;
2026 /* compile the actual shader - pass-in built-in library for external shader */
2027 return compile_with_grammar(*id
, source
, &object
->unit
, type
, infolog
,
2033 compile_shader(GLcontext
*ctx
, slang_code_object
* object
,
2034 slang_unit_type type
, slang_info_log
* infolog
,
2035 struct gl_shader
*shader
)
2037 struct gl_program
*program
= shader
->Programs
[0];
2043 _slang_code_object_dtr(object
);
2044 _slang_code_object_ctr(object
);
2046 success
= compile_object(&id
, shader
->Source
, object
, type
, infolog
, program
);
2048 grammar_destroy(id
);
2058 _slang_compile(GLcontext
*ctx
, struct gl_shader
*shader
)
2061 slang_info_log info_log
;
2062 slang_code_object obj
;
2063 slang_unit_type type
;
2065 if (shader
->Type
== GL_VERTEX_SHADER
) {
2066 type
= SLANG_UNIT_VERTEX_SHADER
;
2069 assert(shader
->Type
== GL_FRAGMENT_SHADER
);
2070 type
= SLANG_UNIT_FRAGMENT_SHADER
;
2073 /* XXX temporary hack */
2074 if (!shader
->Programs
) {
2076 if (shader
->Type
== GL_VERTEX_SHADER
)
2077 progTarget
= GL_VERTEX_PROGRAM_ARB
;
2079 progTarget
= GL_FRAGMENT_PROGRAM_ARB
;
2081 = (struct gl_program
**) malloc(sizeof(struct gl_program
*));
2082 shader
->Programs
[0] = _mesa_new_program(ctx
, progTarget
, 1);
2083 shader
->NumPrograms
= 1;
2085 shader
->Programs
[0]->Parameters
= _mesa_new_parameter_list();
2086 shader
->Programs
[0]->Varying
= _mesa_new_parameter_list();
2087 shader
->Programs
[0]->Attributes
= _mesa_new_parameter_list();
2090 slang_info_log_construct(&info_log
);
2091 _slang_code_object_ctr(&obj
);
2093 success
= compile_shader(ctx
, &obj
, type
, &info_log
, shader
);
2095 /* free shader's prev info log */
2096 if (shader
->InfoLog
) {
2097 _mesa_free(shader
->InfoLog
);
2098 shader
->InfoLog
= NULL
;
2101 if (info_log
.text
) {
2102 /* copy info-log string to shader object */
2103 shader
->InfoLog
= _mesa_strdup(info_log
.text
);
2106 if (info_log
.error_flag
) {
2110 slang_info_log_destruct(&info_log
);
2111 _slang_code_object_dtr(&obj
);