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
);
262 if (array_size
.type
== SLANG_OPER_LITERAL_INT
) {
264 *len
= (GLint
) array_size
.literal
[0];
265 } else if (array_size
.type
== SLANG_OPER_IDENTIFIER
) {
266 slang_variable
*var
= _slang_locate_variable(array_size
.locals
, array_size
.a_id
, GL_TRUE
);
268 slang_info_log_error(C
->L
, "undefined variable '%s'",
269 (char *) array_size
.a_id
);
271 } else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&&
272 var
->type
.specifier
.type
== SLANG_SPEC_INT
) {
273 if (var
->initializer
&&
274 var
->initializer
->type
== SLANG_OPER_LITERAL_INT
) {
275 *len
= (GLint
) var
->initializer
->literal
[0];
278 slang_info_log_error(C
->L
, "unable to parse array size declaration");
282 slang_info_log_error(C
->L
, "unable to parse array size declaration");
289 slang_operation_destruct(&array_size
);
294 calculate_var_size(slang_parse_ctx
* C
, slang_output_ctx
* O
,
295 slang_variable
* var
)
297 slang_storage_aggregate agg
;
299 if (!slang_storage_aggregate_construct(&agg
))
301 if (!_slang_aggregate_variable(&agg
, &var
->type
.specifier
, var
->array_len
,
302 O
->funs
, O
->structs
, O
->vars
, C
->atoms
)) {
303 slang_storage_aggregate_destruct(&agg
);
306 var
->size
= _slang_sizeof_aggregate(&agg
);
307 slang_storage_aggregate_destruct(&agg
);
312 convert_to_array(slang_parse_ctx
* C
, slang_variable
* var
,
313 const slang_type_specifier
* sp
)
315 /* sized array - mark it as array, copy the specifier to the array element and
316 * parse the expression */
317 var
->type
.specifier
.type
= SLANG_SPEC_ARRAY
;
318 var
->type
.specifier
._array
= (slang_type_specifier
*)
319 _slang_alloc(sizeof(slang_type_specifier
));
320 if (var
->type
.specifier
._array
== NULL
) {
321 slang_info_log_memory(C
->L
);
324 slang_type_specifier_ctr(var
->type
.specifier
._array
);
325 return slang_type_specifier_copy(var
->type
.specifier
._array
, sp
);
328 /* structure field */
331 #define FIELD_ARRAY 2
334 parse_struct_field_var(slang_parse_ctx
* C
, slang_output_ctx
* O
,
335 slang_variable
* var
, const slang_type_specifier
* sp
)
337 var
->a_name
= parse_identifier(C
);
338 if (var
->a_name
== SLANG_ATOM_NULL
)
343 if (!slang_type_specifier_copy(&var
->type
.specifier
, sp
))
347 if (!convert_to_array(C
, var
, sp
))
349 if (!parse_array_len(C
, O
, &var
->array_len
))
356 return calculate_var_size(C
, O
, var
);
360 parse_struct_field(slang_parse_ctx
* C
, slang_output_ctx
* O
,
361 slang_struct
* st
, slang_type_specifier
* sp
)
363 slang_output_ctx o
= *O
;
365 o
.structs
= st
->structs
;
366 if (!parse_type_specifier(C
, &o
, sp
))
370 slang_variable
*var
= slang_variable_scope_grow(st
->fields
);
372 slang_info_log_memory(C
->L
);
375 if (!parse_struct_field_var(C
, &o
, var
, sp
))
378 while (*C
->I
++ != FIELD_NONE
);
384 parse_struct(slang_parse_ctx
* C
, slang_output_ctx
* O
, slang_struct
** st
)
389 /* parse struct name (if any) and make sure it is unique in current scope */
390 a_name
= parse_identifier(C
);
391 if (a_name
== SLANG_ATOM_NULL
)
394 name
= slang_atom_pool_id(C
->atoms
, a_name
);
396 && slang_struct_scope_find(O
->structs
, a_name
, 0) != NULL
) {
397 slang_info_log_error(C
->L
, "%s: duplicate type name.", name
);
401 /* set-up a new struct */
402 *st
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
404 slang_info_log_memory(C
->L
);
407 if (!slang_struct_construct(*st
)) {
410 slang_info_log_memory(C
->L
);
413 (**st
).a_name
= a_name
;
414 (**st
).structs
->outer_scope
= O
->structs
;
416 /* parse individual struct fields */
418 slang_type_specifier sp
;
420 slang_type_specifier_ctr(&sp
);
421 if (!parse_struct_field(C
, O
, *st
, &sp
)) {
422 slang_type_specifier_dtr(&sp
);
425 slang_type_specifier_dtr(&sp
);
427 while (*C
->I
++ != FIELD_NONE
);
429 /* if named struct, copy it to current scope */
430 if (name
[0] != '\0') {
433 O
->structs
->structs
=
434 (slang_struct
*) _slang_realloc(O
->structs
->structs
,
435 O
->structs
->num_structs
436 * sizeof(slang_struct
),
437 (O
->structs
->num_structs
+ 1)
438 * sizeof(slang_struct
));
439 if (O
->structs
->structs
== NULL
) {
440 slang_info_log_memory(C
->L
);
443 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
444 if (!slang_struct_construct(s
))
446 O
->structs
->num_structs
++;
447 if (!slang_struct_copy(s
, *st
))
456 #define TYPE_QUALIFIER_NONE 0
457 #define TYPE_QUALIFIER_CONST 1
458 #define TYPE_QUALIFIER_ATTRIBUTE 2
459 #define TYPE_QUALIFIER_VARYING 3
460 #define TYPE_QUALIFIER_UNIFORM 4
461 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
462 #define TYPE_QUALIFIER_FIXEDINPUT 6
465 parse_type_qualifier(slang_parse_ctx
* C
, slang_type_qualifier
* qual
)
468 case TYPE_QUALIFIER_NONE
:
469 *qual
= SLANG_QUAL_NONE
;
471 case TYPE_QUALIFIER_CONST
:
472 *qual
= SLANG_QUAL_CONST
;
474 case TYPE_QUALIFIER_ATTRIBUTE
:
475 *qual
= SLANG_QUAL_ATTRIBUTE
;
477 case TYPE_QUALIFIER_VARYING
:
478 *qual
= SLANG_QUAL_VARYING
;
480 case TYPE_QUALIFIER_UNIFORM
:
481 *qual
= SLANG_QUAL_UNIFORM
;
483 case TYPE_QUALIFIER_FIXEDOUTPUT
:
484 *qual
= SLANG_QUAL_FIXEDOUTPUT
;
486 case TYPE_QUALIFIER_FIXEDINPUT
:
487 *qual
= SLANG_QUAL_FIXEDINPUT
;
496 #define TYPE_SPECIFIER_VOID 0
497 #define TYPE_SPECIFIER_BOOL 1
498 #define TYPE_SPECIFIER_BVEC2 2
499 #define TYPE_SPECIFIER_BVEC3 3
500 #define TYPE_SPECIFIER_BVEC4 4
501 #define TYPE_SPECIFIER_INT 5
502 #define TYPE_SPECIFIER_IVEC2 6
503 #define TYPE_SPECIFIER_IVEC3 7
504 #define TYPE_SPECIFIER_IVEC4 8
505 #define TYPE_SPECIFIER_FLOAT 9
506 #define TYPE_SPECIFIER_VEC2 10
507 #define TYPE_SPECIFIER_VEC3 11
508 #define TYPE_SPECIFIER_VEC4 12
509 #define TYPE_SPECIFIER_MAT2 13
510 #define TYPE_SPECIFIER_MAT3 14
511 #define TYPE_SPECIFIER_MAT4 15
512 #define TYPE_SPECIFIER_SAMPLER1D 16
513 #define TYPE_SPECIFIER_SAMPLER2D 17
514 #define TYPE_SPECIFIER_SAMPLER3D 18
515 #define TYPE_SPECIFIER_SAMPLERCUBE 19
516 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
517 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
518 #define TYPE_SPECIFIER_SAMPLER2DRECT 22
519 #define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
520 #define TYPE_SPECIFIER_STRUCT 24
521 #define TYPE_SPECIFIER_TYPENAME 25
522 #define TYPE_SPECIFIER_MAT23 26
523 #define TYPE_SPECIFIER_MAT32 27
524 #define TYPE_SPECIFIER_MAT24 28
525 #define TYPE_SPECIFIER_MAT42 29
526 #define TYPE_SPECIFIER_MAT34 30
527 #define TYPE_SPECIFIER_MAT43 31
531 parse_type_specifier(slang_parse_ctx
* C
, slang_output_ctx
* O
,
532 slang_type_specifier
* spec
)
535 case TYPE_SPECIFIER_VOID
:
536 spec
->type
= SLANG_SPEC_VOID
;
538 case TYPE_SPECIFIER_BOOL
:
539 spec
->type
= SLANG_SPEC_BOOL
;
541 case TYPE_SPECIFIER_BVEC2
:
542 spec
->type
= SLANG_SPEC_BVEC2
;
544 case TYPE_SPECIFIER_BVEC3
:
545 spec
->type
= SLANG_SPEC_BVEC3
;
547 case TYPE_SPECIFIER_BVEC4
:
548 spec
->type
= SLANG_SPEC_BVEC4
;
550 case TYPE_SPECIFIER_INT
:
551 spec
->type
= SLANG_SPEC_INT
;
553 case TYPE_SPECIFIER_IVEC2
:
554 spec
->type
= SLANG_SPEC_IVEC2
;
556 case TYPE_SPECIFIER_IVEC3
:
557 spec
->type
= SLANG_SPEC_IVEC3
;
559 case TYPE_SPECIFIER_IVEC4
:
560 spec
->type
= SLANG_SPEC_IVEC4
;
562 case TYPE_SPECIFIER_FLOAT
:
563 spec
->type
= SLANG_SPEC_FLOAT
;
565 case TYPE_SPECIFIER_VEC2
:
566 spec
->type
= SLANG_SPEC_VEC2
;
568 case TYPE_SPECIFIER_VEC3
:
569 spec
->type
= SLANG_SPEC_VEC3
;
571 case TYPE_SPECIFIER_VEC4
:
572 spec
->type
= SLANG_SPEC_VEC4
;
574 case TYPE_SPECIFIER_MAT2
:
575 spec
->type
= SLANG_SPEC_MAT2
;
577 case TYPE_SPECIFIER_MAT3
:
578 spec
->type
= SLANG_SPEC_MAT3
;
580 case TYPE_SPECIFIER_MAT4
:
581 spec
->type
= SLANG_SPEC_MAT4
;
583 case TYPE_SPECIFIER_MAT23
:
584 spec
->type
= SLANG_SPEC_MAT23
;
586 case TYPE_SPECIFIER_MAT32
:
587 spec
->type
= SLANG_SPEC_MAT32
;
589 case TYPE_SPECIFIER_MAT24
:
590 spec
->type
= SLANG_SPEC_MAT24
;
592 case TYPE_SPECIFIER_MAT42
:
593 spec
->type
= SLANG_SPEC_MAT42
;
595 case TYPE_SPECIFIER_MAT34
:
596 spec
->type
= SLANG_SPEC_MAT34
;
598 case TYPE_SPECIFIER_MAT43
:
599 spec
->type
= SLANG_SPEC_MAT43
;
601 case TYPE_SPECIFIER_SAMPLER1D
:
602 spec
->type
= SLANG_SPEC_SAMPLER1D
;
604 case TYPE_SPECIFIER_SAMPLER2D
:
605 spec
->type
= SLANG_SPEC_SAMPLER2D
;
607 case TYPE_SPECIFIER_SAMPLER3D
:
608 spec
->type
= SLANG_SPEC_SAMPLER3D
;
610 case TYPE_SPECIFIER_SAMPLERCUBE
:
611 spec
->type
= SLANG_SPEC_SAMPLERCUBE
;
613 case TYPE_SPECIFIER_SAMPLER2DRECT
:
614 spec
->type
= SLANG_SPEC_SAMPLER2DRECT
;
616 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
617 spec
->type
= SLANG_SPEC_SAMPLER1DSHADOW
;
619 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
620 spec
->type
= SLANG_SPEC_SAMPLER2DSHADOW
;
622 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
623 spec
->type
= SLANG_SPEC_SAMPLER2DRECTSHADOW
;
625 case TYPE_SPECIFIER_STRUCT
:
626 spec
->type
= SLANG_SPEC_STRUCT
;
627 if (!parse_struct(C
, O
, &spec
->_struct
))
630 case TYPE_SPECIFIER_TYPENAME
:
631 spec
->type
= SLANG_SPEC_STRUCT
;
636 a_name
= parse_identifier(C
);
640 stru
= slang_struct_scope_find(O
->structs
, a_name
, 1);
642 slang_info_log_error(C
->L
, "undeclared type name '%s'",
643 slang_atom_pool_id(C
->atoms
, a_name
));
647 spec
->_struct
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
648 if (spec
->_struct
== NULL
) {
649 slang_info_log_memory(C
->L
);
652 if (!slang_struct_construct(spec
->_struct
)) {
653 _slang_free(spec
->_struct
);
654 spec
->_struct
= NULL
;
657 if (!slang_struct_copy(spec
->_struct
, stru
))
668 parse_fully_specified_type(slang_parse_ctx
* C
, slang_output_ctx
* O
,
669 slang_fully_specified_type
* type
)
671 if (!parse_type_qualifier(C
, &type
->qualifier
))
673 if (!parse_type_specifier(C
, O
, &type
->specifier
))
680 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
681 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
685 #define OP_CONTINUE 6
688 #define OP_EXPRESSION 9
693 #define OP_PUSH_VOID 14
694 #define OP_PUSH_BOOL 15
695 #define OP_PUSH_INT 16
696 #define OP_PUSH_FLOAT 17
697 #define OP_PUSH_IDENTIFIER 18
698 #define OP_SEQUENCE 19
700 #define OP_ADDASSIGN 21
701 #define OP_SUBASSIGN 22
702 #define OP_MULASSIGN 23
703 #define OP_DIVASSIGN 24
704 /*#define OP_MODASSIGN 25*/
705 /*#define OP_LSHASSIGN 26*/
706 /*#define OP_RSHASSIGN 27*/
707 /*#define OP_ORASSIGN 28*/
708 /*#define OP_XORASSIGN 29*/
709 /*#define OP_ANDASSIGN 30*/
711 #define OP_LOGICALOR 32
712 #define OP_LOGICALXOR 33
713 #define OP_LOGICALAND 34
714 /*#define OP_BITOR 35*/
715 /*#define OP_BITXOR 36*/
716 /*#define OP_BITAND 37*/
718 #define OP_NOTEQUAL 39
720 #define OP_GREATER 41
721 #define OP_LESSEQUAL 42
722 #define OP_GREATEREQUAL 43
723 /*#define OP_LSHIFT 44*/
724 /*#define OP_RSHIFT 45*/
726 #define OP_SUBTRACT 47
727 #define OP_MULTIPLY 48
729 /*#define OP_MODULUS 50*/
730 #define OP_PREINCREMENT 51
731 #define OP_PREDECREMENT 52
734 /*#define OP_COMPLEMENT 55*/
736 #define OP_SUBSCRIPT 57
739 #define OP_POSTINCREMENT 60
740 #define OP_POSTDECREMENT 61
744 * When parsing a compound production, this function is used to parse the
746 * For example, a while-loop compound will have two children, the
747 * while condition expression and the loop body. So, this function will
748 * be called twice to parse those two sub-expressions.
749 * \param C the parsing context
750 * \param O the output context
751 * \param oper the operation we're parsing
752 * \param statement indicates whether parsing a statement, or expression
753 * \return 1 if success, 0 if error
756 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
757 slang_operation
* oper
, GLboolean statement
)
761 /* grow child array */
762 ch
= slang_operation_grow(&oper
->num_children
, &oper
->children
);
764 return parse_statement(C
, O
, ch
);
765 return parse_expression(C
, O
, ch
);
768 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
771 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
772 slang_operation
* oper
)
774 oper
->locals
->outer_scope
= O
->vars
;
776 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
777 /* parse child statements, do not create new variable scope */
778 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
779 while (*C
->I
!= OP_END
)
780 if (!parse_child_operation(C
, O
, oper
, 1))
784 case OP_BLOCK_BEGIN_NEW_SCOPE
:
785 /* parse child statements, create new variable scope */
787 slang_output_ctx o
= *O
;
789 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
790 o
.vars
= oper
->locals
;
791 while (*C
->I
!= OP_END
)
792 if (!parse_child_operation(C
, &o
, oper
, 1))
798 /* local variable declaration, individual declarators are stored as
799 * children identifiers
801 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
803 const unsigned int first_var
= O
->vars
->num_variables
;
805 /* parse the declaration, note that there can be zero or more
806 * than one declarators
808 if (!parse_declaration(C
, O
))
810 if (first_var
< O
->vars
->num_variables
) {
811 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
813 assert(oper
->num_children
== 0);
814 oper
->num_children
= num_vars
;
815 oper
->children
= slang_operation_new(num_vars
);
816 if (oper
->children
== NULL
) {
817 slang_info_log_memory(C
->L
);
820 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
821 slang_operation
*o
= &oper
->children
[i
- first_var
];
822 o
->type
= SLANG_OPER_VARIABLE_DECL
;
823 o
->locals
->outer_scope
= O
->vars
;
824 o
->a_id
= O
->vars
->variables
[i
]->a_name
;
830 /* the __asm statement, parse the mnemonic and all its arguments
833 oper
->type
= SLANG_OPER_ASM
;
834 oper
->a_id
= parse_identifier(C
);
835 if (oper
->a_id
== SLANG_ATOM_NULL
)
837 while (*C
->I
!= OP_END
) {
838 if (!parse_child_operation(C
, O
, oper
, 0))
844 oper
->type
= SLANG_OPER_BREAK
;
847 oper
->type
= SLANG_OPER_CONTINUE
;
850 oper
->type
= SLANG_OPER_DISCARD
;
853 oper
->type
= SLANG_OPER_RETURN
;
854 if (!parse_child_operation(C
, O
, oper
, 0))
858 oper
->type
= SLANG_OPER_EXPRESSION
;
859 if (!parse_child_operation(C
, O
, oper
, 0))
863 oper
->type
= SLANG_OPER_IF
;
864 if (!parse_child_operation(C
, O
, oper
, 0))
866 if (!parse_child_operation(C
, O
, oper
, 1))
868 if (!parse_child_operation(C
, O
, oper
, 1))
873 slang_output_ctx o
= *O
;
875 oper
->type
= SLANG_OPER_WHILE
;
876 o
.vars
= oper
->locals
;
877 if (!parse_child_operation(C
, &o
, oper
, 1))
879 if (!parse_child_operation(C
, &o
, oper
, 1))
884 oper
->type
= SLANG_OPER_DO
;
885 if (!parse_child_operation(C
, O
, oper
, 1))
887 if (!parse_child_operation(C
, O
, oper
, 0))
892 slang_output_ctx o
= *O
;
894 oper
->type
= SLANG_OPER_FOR
;
895 o
.vars
= oper
->locals
;
896 if (!parse_child_operation(C
, &o
, oper
, 1))
898 if (!parse_child_operation(C
, &o
, oper
, 1))
900 if (!parse_child_operation(C
, &o
, oper
, 0))
902 if (!parse_child_operation(C
, &o
, oper
, 1))
913 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
914 slang_operation
** ops
, unsigned int *total_ops
,
919 op
->children
= slang_operation_new(n
);
920 if (op
->children
== NULL
) {
921 slang_info_log_memory(C
->L
);
924 op
->num_children
= n
;
926 for (i
= 0; i
< n
; i
++) {
927 slang_operation_destruct(&op
->children
[i
]);
928 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
931 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
934 *ops
= (slang_operation
*)
936 (*total_ops
+ n
) * sizeof(slang_operation
),
937 *total_ops
* sizeof(slang_operation
));
939 slang_info_log_memory(C
->L
);
946 is_constructor_name(const char *name
, slang_atom a_name
,
947 slang_struct_scope
* structs
)
949 if (slang_type_specifier_type_from_string(name
) != SLANG_SPEC_VOID
)
951 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
955 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
956 slang_operation
* oper
)
958 slang_operation
*ops
= NULL
;
959 unsigned int num_ops
= 0;
962 while (*C
->I
!= OP_END
) {
964 const unsigned int op_code
= *C
->I
++;
966 /* allocate default operation, becomes a no-op if not used */
967 ops
= (slang_operation
*)
969 num_ops
* sizeof(slang_operation
),
970 (num_ops
+ 1) * sizeof(slang_operation
));
972 slang_info_log_memory(C
->L
);
976 if (!slang_operation_construct(op
)) {
977 slang_info_log_memory(C
->L
);
981 op
->locals
->outer_scope
= O
->vars
;
985 op
->type
= SLANG_OPER_VOID
;
988 op
->type
= SLANG_OPER_LITERAL_BOOL
;
989 if (!parse_number(C
, &number
))
994 op
->literal
[3] = (GLfloat
) number
;
995 op
->literal_size
= 1;
998 op
->type
= SLANG_OPER_LITERAL_INT
;
999 if (!parse_number(C
, &number
))
1004 op
->literal
[3] = (GLfloat
) number
;
1005 op
->literal_size
= 1;
1008 op
->type
= SLANG_OPER_LITERAL_FLOAT
;
1009 if (!parse_float(C
, &op
->literal
[0]))
1013 op
->literal
[3] = op
->literal
[0];
1014 op
->literal_size
= 1;
1016 case OP_PUSH_IDENTIFIER
:
1017 op
->type
= SLANG_OPER_IDENTIFIER
;
1018 op
->a_id
= parse_identifier(C
);
1019 if (op
->a_id
== SLANG_ATOM_NULL
)
1023 op
->type
= SLANG_OPER_SEQUENCE
;
1024 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1028 op
->type
= SLANG_OPER_ASSIGN
;
1029 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1033 op
->type
= SLANG_OPER_ADDASSIGN
;
1034 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1038 op
->type
= SLANG_OPER_SUBASSIGN
;
1039 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1043 op
->type
= SLANG_OPER_MULASSIGN
;
1044 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1048 op
->type
= SLANG_OPER_DIVASSIGN
;
1049 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1052 /*case OP_MODASSIGN: */
1053 /*case OP_LSHASSIGN: */
1054 /*case OP_RSHASSIGN: */
1055 /*case OP_ORASSIGN: */
1056 /*case OP_XORASSIGN: */
1057 /*case OP_ANDASSIGN: */
1059 op
->type
= SLANG_OPER_SELECT
;
1060 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1064 op
->type
= SLANG_OPER_LOGICALOR
;
1065 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1069 op
->type
= SLANG_OPER_LOGICALXOR
;
1070 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1074 op
->type
= SLANG_OPER_LOGICALAND
;
1075 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1079 /*case OP_BITXOR: */
1080 /*case OP_BITAND: */
1082 op
->type
= SLANG_OPER_EQUAL
;
1083 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1087 op
->type
= SLANG_OPER_NOTEQUAL
;
1088 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1092 op
->type
= SLANG_OPER_LESS
;
1093 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1097 op
->type
= SLANG_OPER_GREATER
;
1098 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1102 op
->type
= SLANG_OPER_LESSEQUAL
;
1103 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1106 case OP_GREATEREQUAL
:
1107 op
->type
= SLANG_OPER_GREATEREQUAL
;
1108 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1111 /*case OP_LSHIFT: */
1112 /*case OP_RSHIFT: */
1114 op
->type
= SLANG_OPER_ADD
;
1115 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1119 op
->type
= SLANG_OPER_SUBTRACT
;
1120 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1124 op
->type
= SLANG_OPER_MULTIPLY
;
1125 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1129 op
->type
= SLANG_OPER_DIVIDE
;
1130 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1133 /*case OP_MODULUS: */
1134 case OP_PREINCREMENT
:
1135 op
->type
= SLANG_OPER_PREINCREMENT
;
1136 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1139 case OP_PREDECREMENT
:
1140 op
->type
= SLANG_OPER_PREDECREMENT
;
1141 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1145 op
->type
= SLANG_OPER_PLUS
;
1146 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1150 op
->type
= SLANG_OPER_MINUS
;
1151 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1155 op
->type
= SLANG_OPER_NOT
;
1156 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1159 /*case OP_COMPLEMENT: */
1161 op
->type
= SLANG_OPER_SUBSCRIPT
;
1162 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1166 op
->type
= SLANG_OPER_CALL
;
1167 op
->a_id
= parse_identifier(C
);
1168 if (op
->a_id
== SLANG_ATOM_NULL
)
1170 while (*C
->I
!= OP_END
)
1171 if (!parse_child_operation(C
, O
, op
, 0))
1175 if (!C
->parsing_builtin
1176 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1179 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1180 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1181 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1187 op
->type
= SLANG_OPER_FIELD
;
1188 op
->a_id
= parse_identifier(C
);
1189 if (op
->a_id
== SLANG_ATOM_NULL
)
1191 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1194 case OP_POSTINCREMENT
:
1195 op
->type
= SLANG_OPER_POSTINCREMENT
;
1196 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1199 case OP_POSTDECREMENT
:
1200 op
->type
= SLANG_OPER_POSTDECREMENT
;
1201 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1210 slang_operation_destruct(oper
);
1211 *oper
= *ops
; /* struct copy */
1217 /* parameter qualifier */
1218 #define PARAM_QUALIFIER_IN 0
1219 #define PARAM_QUALIFIER_OUT 1
1220 #define PARAM_QUALIFIER_INOUT 2
1222 /* function parameter array presence */
1223 #define PARAMETER_ARRAY_NOT_PRESENT 0
1224 #define PARAMETER_ARRAY_PRESENT 1
1227 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1228 slang_variable
* param
)
1230 /* parse and validate the parameter's type qualifiers (there can be
1231 * two at most) because not all combinations are valid
1233 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1236 case PARAM_QUALIFIER_IN
:
1237 if (param
->type
.qualifier
!= SLANG_QUAL_CONST
1238 && param
->type
.qualifier
!= SLANG_QUAL_NONE
) {
1239 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1243 case PARAM_QUALIFIER_OUT
:
1244 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1245 param
->type
.qualifier
= SLANG_QUAL_OUT
;
1247 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1251 case PARAM_QUALIFIER_INOUT
:
1252 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1253 param
->type
.qualifier
= SLANG_QUAL_INOUT
;
1255 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1263 /* parse parameter's type specifier and name */
1264 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1266 param
->a_name
= parse_identifier(C
);
1267 if (param
->a_name
== SLANG_ATOM_NULL
)
1270 /* if the parameter is an array, parse its size (the size must be
1271 * explicitly defined
1273 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1274 slang_type_specifier p
;
1276 slang_type_specifier_ctr(&p
);
1277 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1278 slang_type_specifier_dtr(&p
);
1281 if (!convert_to_array(C
, param
, &p
)) {
1282 slang_type_specifier_dtr(&p
);
1285 slang_type_specifier_dtr(&p
);
1286 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1290 /* calculate the parameter size */
1291 if (!calculate_var_size(C
, O
, param
))
1294 /* TODO: allocate the local address here? */
1299 #define FUNCTION_ORDINARY 0
1300 #define FUNCTION_CONSTRUCTOR 1
1301 #define FUNCTION_OPERATOR 2
1303 /* function parameter */
1304 #define PARAMETER_NONE 0
1305 #define PARAMETER_NEXT 1
1308 #define OPERATOR_ADDASSIGN 1
1309 #define OPERATOR_SUBASSIGN 2
1310 #define OPERATOR_MULASSIGN 3
1311 #define OPERATOR_DIVASSIGN 4
1312 /*#define OPERATOR_MODASSIGN 5*/
1313 /*#define OPERATOR_LSHASSIGN 6*/
1314 /*#define OPERATOR_RSHASSIGN 7*/
1315 /*#define OPERATOR_ANDASSIGN 8*/
1316 /*#define OPERATOR_XORASSIGN 9*/
1317 /*#define OPERATOR_ORASSIGN 10*/
1318 #define OPERATOR_LOGICALXOR 11
1319 /*#define OPERATOR_BITOR 12*/
1320 /*#define OPERATOR_BITXOR 13*/
1321 /*#define OPERATOR_BITAND 14*/
1322 #define OPERATOR_LESS 15
1323 #define OPERATOR_GREATER 16
1324 #define OPERATOR_LESSEQUAL 17
1325 #define OPERATOR_GREATEREQUAL 18
1326 /*#define OPERATOR_LSHIFT 19*/
1327 /*#define OPERATOR_RSHIFT 20*/
1328 #define OPERATOR_MULTIPLY 21
1329 #define OPERATOR_DIVIDE 22
1330 /*#define OPERATOR_MODULUS 23*/
1331 #define OPERATOR_INCREMENT 24
1332 #define OPERATOR_DECREMENT 25
1333 #define OPERATOR_PLUS 26
1334 #define OPERATOR_MINUS 27
1335 /*#define OPERATOR_COMPLEMENT 28*/
1336 #define OPERATOR_NOT 29
1340 unsigned int o_code
;
1342 } operator_names
[] = {
1343 {OPERATOR_INCREMENT
, "++"},
1344 {OPERATOR_ADDASSIGN
, "+="},
1345 {OPERATOR_PLUS
, "+"},
1346 {OPERATOR_DECREMENT
, "--"},
1347 {OPERATOR_SUBASSIGN
, "-="},
1348 {OPERATOR_MINUS
, "-"},
1349 {OPERATOR_NOT
, "!"},
1350 {OPERATOR_MULASSIGN
, "*="},
1351 {OPERATOR_MULTIPLY
, "*"},
1352 {OPERATOR_DIVASSIGN
, "/="},
1353 {OPERATOR_DIVIDE
, "/"},
1354 {OPERATOR_LESSEQUAL
, "<="},
1355 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1356 /*{ OPERATOR_LSHIFT, "<<" }, */
1357 {OPERATOR_LESS
, "<"},
1358 {OPERATOR_GREATEREQUAL
, ">="},
1359 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1360 /*{ OPERATOR_RSHIFT, ">>" }, */
1361 {OPERATOR_GREATER
, ">"},
1362 /*{ OPERATOR_MODASSIGN, "%=" }, */
1363 /*{ OPERATOR_MODULUS, "%" }, */
1364 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1365 /*{ OPERATOR_BITAND, "&" }, */
1366 /*{ OPERATOR_ORASSIGN, "|=" }, */
1367 /*{ OPERATOR_BITOR, "|" }, */
1368 /*{ OPERATOR_COMPLEMENT, "~" }, */
1369 /*{ OPERATOR_XORASSIGN, "^=" }, */
1370 {OPERATOR_LOGICALXOR
, "^^"},
1371 /*{ OPERATOR_BITXOR, "^" } */
1375 parse_operator_name(slang_parse_ctx
* C
)
1379 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1380 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1382 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1383 if (atom
== SLANG_ATOM_NULL
) {
1384 slang_info_log_memory(C
->L
);
1395 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1396 slang_function
* func
)
1398 /* parse function type and name */
1399 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1402 case FUNCTION_ORDINARY
:
1403 func
->kind
= SLANG_FUNC_ORDINARY
;
1404 func
->header
.a_name
= parse_identifier(C
);
1405 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1408 case FUNCTION_CONSTRUCTOR
:
1409 func
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1410 if (func
->header
.type
.specifier
.type
== SLANG_SPEC_STRUCT
)
1412 func
->header
.a_name
=
1413 slang_atom_pool_atom(C
->atoms
,
1414 slang_type_specifier_type_to_string
1415 (func
->header
.type
.specifier
.type
));
1416 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1417 slang_info_log_memory(C
->L
);
1421 case FUNCTION_OPERATOR
:
1422 func
->kind
= SLANG_FUNC_OPERATOR
;
1423 func
->header
.a_name
= parse_operator_name(C
);
1424 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1431 /* parse function parameters */
1432 while (*C
->I
++ == PARAMETER_NEXT
) {
1433 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1435 slang_info_log_memory(C
->L
);
1438 if (!parse_parameter_declaration(C
, O
, p
))
1442 /* if the function returns a value, append a hidden __retVal 'out'
1443 * parameter that corresponds to the return value.
1445 if (_slang_function_has_return_value(func
)) {
1446 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1447 slang_atom a_retVal
= slang_atom_pool_atom(C
->atoms
, "__retVal");
1449 p
->a_name
= a_retVal
;
1450 p
->type
= func
->header
.type
;
1451 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1454 /* function formal parameters and local variables share the same
1455 * scope, so save the information about param count in a seperate
1456 * place also link the scope to the global variable scope so when a
1457 * given identifier is not found here, the search process continues
1458 * in the global space
1460 func
->param_count
= func
->parameters
->num_variables
;
1461 func
->parameters
->outer_scope
= O
->vars
;
1467 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1468 slang_function
* func
)
1470 slang_output_ctx o
= *O
;
1472 if (!parse_function_prototype(C
, O
, func
))
1475 /* create function's body operation */
1476 func
->body
= (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1477 if (func
->body
== NULL
) {
1478 slang_info_log_memory(C
->L
);
1481 if (!slang_operation_construct(func
->body
)) {
1482 _slang_free(func
->body
);
1484 slang_info_log_memory(C
->L
);
1488 /* to parse the body the parse context is modified in order to
1489 * capture parsed variables into function's local variable scope
1491 C
->global_scope
= GL_FALSE
;
1492 o
.vars
= func
->parameters
;
1493 if (!parse_statement(C
, &o
, func
->body
))
1496 C
->global_scope
= GL_TRUE
;
1501 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1503 slang_operation op_id
, op_assign
;
1506 /* construct the left side of assignment */
1507 if (!slang_operation_construct(&op_id
))
1509 op_id
.type
= SLANG_OPER_IDENTIFIER
;
1510 op_id
.a_id
= var
->a_name
;
1512 /* put the variable into operation's scope */
1513 op_id
.locals
->variables
=
1514 (slang_variable
**) _slang_alloc(sizeof(slang_variable
*));
1515 if (op_id
.locals
->variables
== NULL
) {
1516 slang_operation_destruct(&op_id
);
1519 op_id
.locals
->num_variables
= 1;
1520 op_id
.locals
->variables
[0] = var
;
1522 /* construct the assignment expression */
1523 if (!slang_operation_construct(&op_assign
)) {
1524 op_id
.locals
->num_variables
= 0;
1525 slang_operation_destruct(&op_id
);
1528 op_assign
.type
= SLANG_OPER_ASSIGN
;
1529 op_assign
.children
=
1530 (slang_operation
*) _slang_alloc(2 * sizeof(slang_operation
));
1531 if (op_assign
.children
== NULL
) {
1532 slang_operation_destruct(&op_assign
);
1533 op_id
.locals
->num_variables
= 0;
1534 slang_operation_destruct(&op_id
);
1537 op_assign
.num_children
= 2;
1538 op_assign
.children
[0] = op_id
;
1539 op_assign
.children
[1] = *var
->initializer
;
1543 /* carefully destroy the operations */
1544 op_assign
.num_children
= 0;
1545 _slang_free(op_assign
.children
);
1546 op_assign
.children
= NULL
;
1547 slang_operation_destruct(&op_assign
);
1548 op_id
.locals
->num_variables
= 0;
1549 slang_operation_destruct(&op_id
);
1557 /* init declarator list */
1558 #define DECLARATOR_NONE 0
1559 #define DECLARATOR_NEXT 1
1561 /* variable declaration */
1562 #define VARIABLE_NONE 0
1563 #define VARIABLE_IDENTIFIER 1
1564 #define VARIABLE_INITIALIZER 2
1565 #define VARIABLE_ARRAY_EXPLICIT 3
1566 #define VARIABLE_ARRAY_UNKNOWN 4
1570 * Parse the initializer for a variable declaration.
1573 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1574 const slang_fully_specified_type
* type
)
1576 slang_variable
*var
;
1578 /* empty init declatator (without name, e.g. "float ;") */
1579 if (*C
->I
++ == VARIABLE_NONE
)
1582 /* make room for the new variable and initialize it */
1583 var
= slang_variable_scope_grow(O
->vars
);
1585 slang_info_log_memory(C
->L
);
1589 /* copy the declarator qualifier type, parse the identifier */
1590 var
->type
.qualifier
= type
->qualifier
;
1591 var
->a_name
= parse_identifier(C
);
1592 if (var
->a_name
== SLANG_ATOM_NULL
)
1597 /* simple variable declarator - just copy the specifier */
1598 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1601 case VARIABLE_INITIALIZER
:
1602 /* initialized variable - copy the specifier and parse the expression */
1603 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1606 (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1607 if (var
->initializer
== NULL
) {
1608 slang_info_log_memory(C
->L
);
1611 if (!slang_operation_construct(var
->initializer
)) {
1612 _slang_free(var
->initializer
);
1613 var
->initializer
= NULL
;
1614 slang_info_log_memory(C
->L
);
1617 if (!parse_expression(C
, O
, var
->initializer
))
1620 case VARIABLE_ARRAY_UNKNOWN
:
1621 /* unsized array - mark it as array and copy the specifier to
1624 if (!convert_to_array(C
, var
, &type
->specifier
))
1627 case VARIABLE_ARRAY_EXPLICIT
:
1628 if (!convert_to_array(C
, var
, &type
->specifier
))
1630 if (!parse_array_len(C
, O
, &var
->array_len
))
1637 /* emit code for global var decl */
1638 if (C
->global_scope
) {
1639 slang_assemble_ctx A
;
1641 A
.space
.funcs
= O
->funs
;
1642 A
.space
.structs
= O
->structs
;
1643 A
.space
.vars
= O
->vars
;
1644 A
.program
= O
->program
;
1645 A
.vartable
= O
->vartable
;
1646 A
.curFuncEndLabel
= NULL
;
1647 if (!_slang_codegen_global_variable(&A
, var
, C
->type
))
1651 /* allocate global address space for a variable with a known size */
1653 && !(var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
1654 && var
->array_len
== 0)) {
1655 if (!calculate_var_size(C
, O
, var
))
1657 var
->address
= slang_var_pool_alloc(O
->global_pool
, var
->size
);
1660 /* initialize global variable */
1661 if (C
->global_scope
) {
1662 if (var
->initializer
!= NULL
) {
1663 slang_assemble_ctx A
;
1666 A
.space
.funcs
= O
->funs
;
1667 A
.space
.structs
= O
->structs
;
1668 A
.space
.vars
= O
->vars
;
1669 if (!initialize_global(&A
, var
))
1677 * Parse a list of variable declarations. Each variable may have an
1681 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1683 slang_fully_specified_type type
;
1685 /* parse the fully specified type, common to all declarators */
1686 if (!slang_fully_specified_type_construct(&type
))
1688 if (!parse_fully_specified_type(C
, O
, &type
)) {
1689 slang_fully_specified_type_destruct(&type
);
1693 /* parse declarators, pass-in the parsed type */
1695 if (!parse_init_declarator(C
, O
, &type
)) {
1696 slang_fully_specified_type_destruct(&type
);
1700 while (*C
->I
++ == DECLARATOR_NEXT
);
1702 slang_fully_specified_type_destruct(&type
);
1708 * Parse a function definition or declaration.
1709 * \param C parsing context
1710 * \param O output context
1711 * \param definition if non-zero expect a definition, else a declaration
1712 * \param parsed_func_ret returns the parsed function
1713 * \return GL_TRUE if success, GL_FALSE if failure
1716 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
1717 slang_function
** parsed_func_ret
)
1719 slang_function parsed_func
, *found_func
;
1721 /* parse function definition/declaration */
1722 if (!slang_function_construct(&parsed_func
))
1725 if (!parse_function_definition(C
, O
, &parsed_func
)) {
1726 slang_function_destruct(&parsed_func
);
1731 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
1732 slang_function_destruct(&parsed_func
);
1737 /* find a function with a prototype matching the parsed one - only
1738 * the current scope is being searched to allow built-in function
1741 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
1742 if (found_func
== NULL
) {
1743 /* New function, add it to the function list */
1744 O
->funs
->functions
=
1745 (slang_function
*) _slang_realloc(O
->funs
->functions
,
1746 O
->funs
->num_functions
1747 * sizeof(slang_function
),
1748 (O
->funs
->num_functions
+ 1)
1749 * sizeof(slang_function
));
1750 if (O
->funs
->functions
== NULL
) {
1751 slang_info_log_memory(C
->L
);
1752 slang_function_destruct(&parsed_func
);
1755 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
1756 O
->funs
->num_functions
++;
1758 /* return the newly parsed function */
1759 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
1762 /* previously defined or declared */
1763 /* TODO: check function return type qualifiers and specifiers */
1765 if (found_func
->body
!= NULL
) {
1766 slang_info_log_error(C
->L
, "%s: function already has a body.",
1767 slang_atom_pool_id(C
->atoms
,
1770 slang_function_destruct(&parsed_func
);
1774 /* destroy the existing function declaration and replace it
1775 * with the new one, remember to save the fixup table
1777 parsed_func
.fixups
= found_func
->fixups
;
1778 slang_fixup_table_init(&found_func
->fixups
);
1779 slang_function_destruct(found_func
);
1780 *found_func
= parsed_func
;
1783 /* another declaration of the same function prototype - ignore it */
1784 slang_function_destruct(&parsed_func
);
1787 /* return the found function */
1788 *parsed_func_ret
= found_func
;
1791 /* assemble the parsed function */
1793 slang_assemble_ctx A
;
1796 A
.space
.funcs
= O
->funs
;
1797 A
.space
.structs
= O
->structs
;
1798 A
.space
.vars
= O
->vars
;
1799 A
.program
= O
->program
;
1800 A
.vartable
= O
->vartable
;
1803 _slang_codegen_function(&A
, *parsed_func_ret
);
1809 #define DECLARATION_FUNCTION_PROTOTYPE 1
1810 #define DECLARATION_INIT_DECLARATOR_LIST 2
1813 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1816 case DECLARATION_INIT_DECLARATOR_LIST
:
1817 if (!parse_init_declarator_list(C
, O
))
1820 case DECLARATION_FUNCTION_PROTOTYPE
:
1822 slang_function
*dummy_func
;
1824 if (!parse_function(C
, O
, 0, &dummy_func
))
1834 /* external declaration */
1835 #define EXTERNAL_NULL 0
1836 #define EXTERNAL_FUNCTION_DEFINITION 1
1837 #define EXTERNAL_DECLARATION 2
1840 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
,
1841 struct gl_program
*program
)
1843 GET_CURRENT_CONTEXT(ctx
);
1848 if (unit
->type
== SLANG_UNIT_FRAGMENT_BUILTIN
||
1849 unit
->type
== SLANG_UNIT_FRAGMENT_SHADER
) {
1850 maxRegs
= ctx
->Const
.FragmentProgram
.MaxTemps
;
1853 assert(unit
->type
== SLANG_UNIT_VERTEX_BUILTIN
||
1854 unit
->type
== SLANG_UNIT_VERTEX_SHADER
);
1855 maxRegs
= ctx
->Const
.VertexProgram
.MaxTemps
;
1858 /* setup output context */
1859 o
.funs
= &unit
->funs
;
1860 o
.structs
= &unit
->structs
;
1861 o
.vars
= &unit
->vars
;
1862 o
.global_pool
= &unit
->object
->varpool
;
1863 o
.program
= program
;
1864 o
.vartable
= _slang_new_var_table(maxRegs
);
1865 _slang_push_var_table(o
.vartable
);
1867 /* parse individual functions and declarations */
1868 while (*C
->I
!= EXTERNAL_NULL
) {
1870 case EXTERNAL_FUNCTION_DEFINITION
:
1872 slang_function
*func
;
1873 success
= parse_function(C
, &o
, 1, &func
);
1876 case EXTERNAL_DECLARATION
:
1877 success
= parse_declaration(C
, &o
);
1884 /* xxx free codegen */
1885 _slang_pop_var_table(o
.vartable
);
1891 _slang_pop_var_table(o
.vartable
);
1892 _slang_delete_var_table(o
.vartable
);
1898 compile_binary(const byte
* prod
, slang_code_unit
* unit
,
1899 slang_unit_type type
, slang_info_log
* infolog
,
1900 slang_code_unit
* builtin
, slang_code_unit
* downlink
,
1901 struct gl_program
*program
)
1907 /* setup parse context */
1910 C
.parsing_builtin
= (builtin
== NULL
);
1911 C
.global_scope
= GL_TRUE
;
1912 C
.atoms
= &unit
->object
->atompool
;
1915 if (!check_revision(&C
))
1918 if (downlink
!= NULL
) {
1919 unit
->vars
.outer_scope
= &downlink
->vars
;
1920 unit
->funs
.outer_scope
= &downlink
->funs
;
1921 unit
->structs
.outer_scope
= &downlink
->structs
;
1924 /* parse translation unit */
1925 return parse_code_unit(&C
, unit
, program
);
1929 compile_with_grammar(grammar id
, const char *source
, slang_code_unit
* unit
,
1930 slang_unit_type type
, slang_info_log
* infolog
,
1931 slang_code_unit
* builtin
,
1932 struct gl_program
*program
)
1935 GLuint size
, start
, version
;
1936 slang_string preprocessed
;
1939 #if FEATURE_ARB_shading_language_120
1945 /* First retrieve the version number. */
1946 if (!_slang_preprocess_version(source
, &version
, &start
, infolog
))
1949 if (version
> maxVersion
) {
1950 slang_info_log_error(infolog
,
1951 "language version %.2f is not supported.",
1956 /* Now preprocess the source string. */
1957 slang_string_init(&preprocessed
);
1958 if (!_slang_preprocess_directives(&preprocessed
, &source
[start
], infolog
)) {
1959 slang_string_free(&preprocessed
);
1960 slang_info_log_error(infolog
, "failed to preprocess the source.");
1964 /* Finally check the syntax and generate its binary representation. */
1965 if (!grammar_fast_check(id
,
1966 (const byte
*) (slang_string_cstr(&preprocessed
)),
1967 &prod
, &size
, 65536)) {
1971 slang_string_free(&preprocessed
);
1972 grammar_get_last_error((byte
*) (buf
), sizeof(buf
), &pos
);
1973 slang_info_log_error(infolog
, buf
);
1974 /* syntax error (possibly in library code) */
1979 s
= (char *) _mesa_find_line_column((const GLubyte
*) source
,
1980 (const GLubyte
*) source
+ pos
,
1982 printf("Error on line %d, col %d: %s\n", line
, col
, s
);
1987 slang_string_free(&preprocessed
);
1989 /* Syntax is okay - translate it to internal representation. */
1990 if (!compile_binary(prod
, unit
, type
, infolog
, builtin
,
1991 &builtin
[SLANG_BUILTIN_TOTAL
- 1],
1993 grammar_alloc_free(prod
);
1996 grammar_alloc_free(prod
);
2000 LONGSTRING
static const char *slang_shader_syn
=
2001 #include "library/slang_shader_syn.h"
2004 static const byte slang_core_gc
[] = {
2005 #include "library/slang_core_gc.h"
2008 static const byte slang_120_core_gc
[] = {
2009 #include "library/slang_120_core_gc.h"
2012 static const byte slang_120_fragment_gc
[] = {
2013 #include "library/slang_builtin_120_fragment_gc.h"
2016 static const byte slang_common_builtin_gc
[] = {
2017 #include "library/slang_common_builtin_gc.h"
2020 static const byte slang_fragment_builtin_gc
[] = {
2021 #include "library/slang_fragment_builtin_gc.h"
2024 static const byte slang_vertex_builtin_gc
[] = {
2025 #include "library/slang_vertex_builtin_gc.h"
2029 compile_object(grammar
* id
, const char *source
, slang_code_object
* object
,
2030 slang_unit_type type
, slang_info_log
* infolog
,
2031 struct gl_program
*program
)
2033 slang_code_unit
*builtins
= NULL
;
2035 /* load GLSL grammar */
2036 *id
= grammar_load_from_text((const byte
*) (slang_shader_syn
));
2041 grammar_get_last_error(buf
, 1024, &pos
);
2042 slang_info_log_error(infolog
, (const char *) (buf
));
2046 /* set shader type - the syntax is slightly different for different shaders */
2047 if (type
== SLANG_UNIT_FRAGMENT_SHADER
2048 || type
== SLANG_UNIT_FRAGMENT_BUILTIN
)
2049 grammar_set_reg8(*id
, (const byte
*) "shader_type", 1);
2051 grammar_set_reg8(*id
, (const byte
*) "shader_type", 2);
2053 /* enable language extensions */
2054 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2056 /* if parsing user-specified shader, load built-in library */
2057 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_VERTEX_SHADER
) {
2058 /* compile core functionality first */
2059 if (!compile_binary(slang_core_gc
,
2060 &object
->builtin
[SLANG_BUILTIN_CORE
],
2061 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2065 #if FEATURE_ARB_shading_language_120
2066 if (!compile_binary(slang_120_core_gc
,
2067 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2068 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2069 NULL
, &object
->builtin
[SLANG_BUILTIN_CORE
], NULL
))
2073 /* compile common functions and variables, link to core */
2074 if (!compile_binary(slang_common_builtin_gc
,
2075 &object
->builtin
[SLANG_BUILTIN_COMMON
],
2076 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2077 #if FEATURE_ARB_shading_language_120
2078 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2080 &object
->builtin
[SLANG_BUILTIN_CORE
],
2085 /* compile target-specific functions and variables, link to common */
2086 if (type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2087 if (!compile_binary(slang_fragment_builtin_gc
,
2088 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2089 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2090 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2092 #if FEATURE_ARB_shading_language_120
2093 if (!compile_binary(slang_120_fragment_gc
,
2094 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2095 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2096 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2100 else if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2101 if (!compile_binary(slang_vertex_builtin_gc
,
2102 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2103 SLANG_UNIT_VERTEX_BUILTIN
, infolog
, NULL
,
2104 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2108 /* disable language extensions */
2109 #if NEW_SLANG /* allow-built-ins */
2110 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2112 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 0);
2114 builtins
= object
->builtin
;
2117 /* compile the actual shader - pass-in built-in library for external shader */
2118 return compile_with_grammar(*id
, source
, &object
->unit
, type
, infolog
,
2124 compile_shader(GLcontext
*ctx
, slang_code_object
* object
,
2125 slang_unit_type type
, slang_info_log
* infolog
,
2126 struct gl_shader
*shader
)
2128 struct gl_program
*program
= shader
->Programs
[0];
2134 _slang_code_object_dtr(object
);
2135 _slang_code_object_ctr(object
);
2137 success
= compile_object(&id
, shader
->Source
, object
, type
, infolog
, program
);
2139 grammar_destroy(id
);
2149 _slang_compile(GLcontext
*ctx
, struct gl_shader
*shader
)
2152 slang_info_log info_log
;
2153 slang_code_object obj
;
2154 slang_unit_type type
;
2156 if (shader
->Type
== GL_VERTEX_SHADER
) {
2157 type
= SLANG_UNIT_VERTEX_SHADER
;
2160 assert(shader
->Type
== GL_FRAGMENT_SHADER
);
2161 type
= SLANG_UNIT_FRAGMENT_SHADER
;
2164 ctx
->Shader
.MemPool
= _slang_new_mempool(1024*1024);
2166 /* XXX temporary hack */
2167 if (!shader
->Programs
) {
2169 if (shader
->Type
== GL_VERTEX_SHADER
)
2170 progTarget
= GL_VERTEX_PROGRAM_ARB
;
2172 progTarget
= GL_FRAGMENT_PROGRAM_ARB
;
2174 = (struct gl_program
**) malloc(sizeof(struct gl_program
*));
2175 shader
->Programs
[0] = ctx
->Driver
.NewProgram(ctx
, progTarget
, 1);
2176 shader
->NumPrograms
= 1;
2178 shader
->Programs
[0]->Parameters
= _mesa_new_parameter_list();
2179 shader
->Programs
[0]->Varying
= _mesa_new_parameter_list();
2180 shader
->Programs
[0]->Attributes
= _mesa_new_parameter_list();
2183 slang_info_log_construct(&info_log
);
2184 _slang_code_object_ctr(&obj
);
2186 success
= compile_shader(ctx
, &obj
, type
, &info_log
, shader
);
2188 /* free shader's prev info log */
2189 if (shader
->InfoLog
) {
2190 _mesa_free(shader
->InfoLog
);
2191 shader
->InfoLog
= NULL
;
2194 if (info_log
.text
) {
2195 /* copy info-log string to shader object */
2196 shader
->InfoLog
= _mesa_strdup(info_log
.text
);
2199 if (info_log
.error_flag
) {
2203 slang_info_log_destruct(&info_log
);
2204 _slang_code_object_dtr(&obj
);
2206 _slang_delete_mempool((slang_mempool
*) ctx
->Shader
.MemPool
);
2207 ctx
->Shader
.MemPool
= NULL
;
2209 if (shader
->Type
== GL_VERTEX_SHADER
) {
2210 /* remove any reads of varying (output) registers */
2212 printf("Pre-remove output reads:\n");
2213 _mesa_print_program(shader
->Programs
[0]);
2215 _mesa_remove_output_reads(shader
->Programs
[0], PROGRAM_VARYING
);
2216 _mesa_remove_output_reads(shader
->Programs
[0], PROGRAM_OUTPUT
);
2218 printf("Post-remove output reads:\n");
2219 _mesa_print_program(shader
->Programs
[0]);