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_mem.h"
43 #include "slang_vartable.h"
44 #include "slang_simplify.h"
46 #include "slang_print.h"
49 * This is a straightforward implementation of the slang front-end
50 * compiler. Lots of error-checking functionality is missing but
51 * every well-formed shader source should compile successfully and
52 * execute as expected. However, some semantically ill-formed shaders
53 * may be accepted resulting in undefined behaviour.
59 * Allocate storage for a variable of 'size' bytes from given pool.
60 * Return the allocated address for the variable.
63 slang_var_pool_alloc(slang_var_pool
* pool
, unsigned int size
)
65 const GLuint addr
= pool
->next_addr
;
66 pool
->next_addr
+= size
;
75 _slang_code_unit_ctr(slang_code_unit
* self
,
76 struct slang_code_object_
* object
)
78 _slang_variable_scope_ctr(&self
->vars
);
79 _slang_function_scope_ctr(&self
->funs
);
80 _slang_struct_scope_ctr(&self
->structs
);
81 self
->object
= object
;
85 _slang_code_unit_dtr(slang_code_unit
* self
)
87 slang_variable_scope_destruct(&self
->vars
);
88 slang_function_scope_destruct(&self
->funs
);
89 slang_struct_scope_destruct(&self
->structs
);
97 _slang_code_object_ctr(slang_code_object
* self
)
101 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
102 _slang_code_unit_ctr(&self
->builtin
[i
], self
);
103 _slang_code_unit_ctr(&self
->unit
, self
);
104 self
->varpool
.next_addr
= 0;
105 slang_atom_pool_construct(&self
->atompool
);
109 _slang_code_object_dtr(slang_code_object
* self
)
113 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
114 _slang_code_unit_dtr(&self
->builtin
[i
]);
115 _slang_code_unit_dtr(&self
->unit
);
116 slang_atom_pool_destruct(&self
->atompool
);
120 /* slang_parse_ctx */
122 typedef struct slang_parse_ctx_
127 GLboolean global_scope
; /**< Is object being declared a global? */
128 slang_atom_pool
*atoms
;
129 slang_unit_type type
; /**< Vertex vs. Fragment */
132 /* slang_output_ctx */
134 typedef struct slang_output_ctx_
136 slang_variable_scope
*vars
;
137 slang_function_scope
*funs
;
138 slang_struct_scope
*structs
;
139 slang_var_pool
*global_pool
;
140 struct gl_program
*program
;
141 slang_var_table
*vartable
;
144 /* _slang_compile() */
147 parse_identifier_str(slang_parse_ctx
* C
, char **id
)
150 C
->I
+= _mesa_strlen(*id
) + 1;
154 parse_identifier(slang_parse_ctx
* C
)
158 id
= (const char *) C
->I
;
159 C
->I
+= _mesa_strlen(id
) + 1;
160 return slang_atom_pool_atom(C
->atoms
, id
);
164 parse_number(slang_parse_ctx
* C
, int *number
)
166 const int radix
= (int) (*C
->I
++);
168 while (*C
->I
!= '\0') {
170 if (*C
->I
>= '0' && *C
->I
<= '9')
171 digit
= (int) (*C
->I
- '0');
172 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
173 digit
= (int) (*C
->I
- 'A') + 10;
175 digit
= (int) (*C
->I
- 'a') + 10;
176 *number
= *number
* radix
+ digit
;
181 slang_info_log_warning(C
->L
, "%d: literal integer overflow.", *number
);
186 parse_float(slang_parse_ctx
* C
, float *number
)
188 char *integral
= NULL
;
189 char *fractional
= NULL
;
190 char *exponent
= NULL
;
193 parse_identifier_str(C
, &integral
);
194 parse_identifier_str(C
, &fractional
);
195 parse_identifier_str(C
, &exponent
);
197 whole
= (char *) _slang_alloc((_mesa_strlen(integral
) +
198 _mesa_strlen(fractional
) +
199 _mesa_strlen(exponent
) + 3) * sizeof(char));
201 slang_info_log_memory(C
->L
);
205 slang_string_copy(whole
, integral
);
206 slang_string_concat(whole
, ".");
207 slang_string_concat(whole
, fractional
);
208 slang_string_concat(whole
, "E");
209 slang_string_concat(whole
, exponent
);
211 *number
= (float) (_mesa_strtod(whole
, (char **) NULL
));
218 /* revision number - increment after each change affecting emitted output */
222 check_revision(slang_parse_ctx
* C
)
224 if (*C
->I
!= REVISION
) {
225 slang_info_log_error(C
->L
, "Internal compiler error.");
232 static int parse_statement(slang_parse_ctx
*, slang_output_ctx
*,
234 static int parse_expression(slang_parse_ctx
*, slang_output_ctx
*,
236 static int parse_type_specifier(slang_parse_ctx
*, slang_output_ctx
*,
237 slang_type_specifier
*);
240 parse_array_len(slang_parse_ctx
* C
, slang_output_ctx
* O
, GLuint
* len
)
242 slang_operation array_size
;
243 slang_name_space space
;
246 if (!slang_operation_construct(&array_size
))
248 if (!parse_expression(C
, O
, &array_size
)) {
249 slang_operation_destruct(&array_size
);
253 space
.funcs
= O
->funs
;
254 space
.structs
= O
->structs
;
255 space
.vars
= O
->vars
;
257 /* evaluate compile-time expression which is array size */
258 _slang_simplify(&array_size
, &space
, C
->atoms
);
259 result
= (array_size
.type
== SLANG_OPER_LITERAL_INT
);
261 *len
= (GLint
) array_size
.literal
[0];
263 slang_operation_destruct(&array_size
);
268 calculate_var_size(slang_parse_ctx
* C
, slang_output_ctx
* O
,
269 slang_variable
* var
)
271 slang_storage_aggregate agg
;
273 if (!slang_storage_aggregate_construct(&agg
))
275 if (!_slang_aggregate_variable(&agg
, &var
->type
.specifier
, var
->array_len
,
276 O
->funs
, O
->structs
, O
->vars
, C
->atoms
)) {
277 slang_storage_aggregate_destruct(&agg
);
280 var
->size
= _slang_sizeof_aggregate(&agg
);
281 slang_storage_aggregate_destruct(&agg
);
286 convert_to_array(slang_parse_ctx
* C
, slang_variable
* var
,
287 const slang_type_specifier
* sp
)
289 /* sized array - mark it as array, copy the specifier to the array element and
290 * parse the expression */
291 var
->type
.specifier
.type
= SLANG_SPEC_ARRAY
;
292 var
->type
.specifier
._array
= (slang_type_specifier
*)
293 _slang_alloc(sizeof(slang_type_specifier
));
294 if (var
->type
.specifier
._array
== NULL
) {
295 slang_info_log_memory(C
->L
);
298 slang_type_specifier_ctr(var
->type
.specifier
._array
);
299 return slang_type_specifier_copy(var
->type
.specifier
._array
, sp
);
302 /* structure field */
305 #define FIELD_ARRAY 2
308 parse_struct_field_var(slang_parse_ctx
* C
, slang_output_ctx
* O
,
309 slang_variable
* var
, const slang_type_specifier
* sp
)
311 var
->a_name
= parse_identifier(C
);
312 if (var
->a_name
== SLANG_ATOM_NULL
)
317 if (!slang_type_specifier_copy(&var
->type
.specifier
, sp
))
321 if (!convert_to_array(C
, var
, sp
))
323 if (!parse_array_len(C
, O
, &var
->array_len
))
330 return calculate_var_size(C
, O
, var
);
334 parse_struct_field(slang_parse_ctx
* C
, slang_output_ctx
* O
,
335 slang_struct
* st
, slang_type_specifier
* sp
)
337 slang_output_ctx o
= *O
;
339 o
.structs
= st
->structs
;
340 if (!parse_type_specifier(C
, &o
, sp
))
344 slang_variable
*var
= slang_variable_scope_grow(st
->fields
);
346 slang_info_log_memory(C
->L
);
349 if (!parse_struct_field_var(C
, &o
, var
, sp
))
352 while (*C
->I
++ != FIELD_NONE
);
358 parse_struct(slang_parse_ctx
* C
, slang_output_ctx
* O
, slang_struct
** st
)
363 /* parse struct name (if any) and make sure it is unique in current scope */
364 a_name
= parse_identifier(C
);
365 if (a_name
== SLANG_ATOM_NULL
)
368 name
= slang_atom_pool_id(C
->atoms
, a_name
);
370 && slang_struct_scope_find(O
->structs
, a_name
, 0) != NULL
) {
371 slang_info_log_error(C
->L
, "%s: duplicate type name.", name
);
375 /* set-up a new struct */
376 *st
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
378 slang_info_log_memory(C
->L
);
381 if (!slang_struct_construct(*st
)) {
384 slang_info_log_memory(C
->L
);
387 (**st
).a_name
= a_name
;
388 (**st
).structs
->outer_scope
= O
->structs
;
390 /* parse individual struct fields */
392 slang_type_specifier sp
;
394 slang_type_specifier_ctr(&sp
);
395 if (!parse_struct_field(C
, O
, *st
, &sp
)) {
396 slang_type_specifier_dtr(&sp
);
399 slang_type_specifier_dtr(&sp
);
401 while (*C
->I
++ != FIELD_NONE
);
403 /* if named struct, copy it to current scope */
404 if (name
[0] != '\0') {
407 O
->structs
->structs
=
408 (slang_struct
*) _slang_realloc(O
->structs
->structs
,
409 O
->structs
->num_structs
410 * sizeof(slang_struct
),
411 (O
->structs
->num_structs
+ 1)
412 * sizeof(slang_struct
));
413 if (O
->structs
->structs
== NULL
) {
414 slang_info_log_memory(C
->L
);
417 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
418 if (!slang_struct_construct(s
))
420 O
->structs
->num_structs
++;
421 if (!slang_struct_copy(s
, *st
))
430 #define TYPE_QUALIFIER_NONE 0
431 #define TYPE_QUALIFIER_CONST 1
432 #define TYPE_QUALIFIER_ATTRIBUTE 2
433 #define TYPE_QUALIFIER_VARYING 3
434 #define TYPE_QUALIFIER_UNIFORM 4
435 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
436 #define TYPE_QUALIFIER_FIXEDINPUT 6
439 parse_type_qualifier(slang_parse_ctx
* C
, slang_type_qualifier
* qual
)
442 case TYPE_QUALIFIER_NONE
:
443 *qual
= SLANG_QUAL_NONE
;
445 case TYPE_QUALIFIER_CONST
:
446 *qual
= SLANG_QUAL_CONST
;
448 case TYPE_QUALIFIER_ATTRIBUTE
:
449 *qual
= SLANG_QUAL_ATTRIBUTE
;
451 case TYPE_QUALIFIER_VARYING
:
452 *qual
= SLANG_QUAL_VARYING
;
454 case TYPE_QUALIFIER_UNIFORM
:
455 *qual
= SLANG_QUAL_UNIFORM
;
457 case TYPE_QUALIFIER_FIXEDOUTPUT
:
458 *qual
= SLANG_QUAL_FIXEDOUTPUT
;
460 case TYPE_QUALIFIER_FIXEDINPUT
:
461 *qual
= SLANG_QUAL_FIXEDINPUT
;
470 #define TYPE_SPECIFIER_VOID 0
471 #define TYPE_SPECIFIER_BOOL 1
472 #define TYPE_SPECIFIER_BVEC2 2
473 #define TYPE_SPECIFIER_BVEC3 3
474 #define TYPE_SPECIFIER_BVEC4 4
475 #define TYPE_SPECIFIER_INT 5
476 #define TYPE_SPECIFIER_IVEC2 6
477 #define TYPE_SPECIFIER_IVEC3 7
478 #define TYPE_SPECIFIER_IVEC4 8
479 #define TYPE_SPECIFIER_FLOAT 9
480 #define TYPE_SPECIFIER_VEC2 10
481 #define TYPE_SPECIFIER_VEC3 11
482 #define TYPE_SPECIFIER_VEC4 12
483 #define TYPE_SPECIFIER_MAT2 13
484 #define TYPE_SPECIFIER_MAT3 14
485 #define TYPE_SPECIFIER_MAT4 15
486 #define TYPE_SPECIFIER_SAMPLER1D 16
487 #define TYPE_SPECIFIER_SAMPLER2D 17
488 #define TYPE_SPECIFIER_SAMPLER3D 18
489 #define TYPE_SPECIFIER_SAMPLERCUBE 19
490 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
491 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
492 #define TYPE_SPECIFIER_SAMPLER2DRECT 22
493 #define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
494 #define TYPE_SPECIFIER_STRUCT 24
495 #define TYPE_SPECIFIER_TYPENAME 25
496 #define TYPE_SPECIFIER_MAT23 26
497 #define TYPE_SPECIFIER_MAT32 27
498 #define TYPE_SPECIFIER_MAT24 28
499 #define TYPE_SPECIFIER_MAT42 29
500 #define TYPE_SPECIFIER_MAT34 30
501 #define TYPE_SPECIFIER_MAT43 31
505 parse_type_specifier(slang_parse_ctx
* C
, slang_output_ctx
* O
,
506 slang_type_specifier
* spec
)
509 case TYPE_SPECIFIER_VOID
:
510 spec
->type
= SLANG_SPEC_VOID
;
512 case TYPE_SPECIFIER_BOOL
:
513 spec
->type
= SLANG_SPEC_BOOL
;
515 case TYPE_SPECIFIER_BVEC2
:
516 spec
->type
= SLANG_SPEC_BVEC2
;
518 case TYPE_SPECIFIER_BVEC3
:
519 spec
->type
= SLANG_SPEC_BVEC3
;
521 case TYPE_SPECIFIER_BVEC4
:
522 spec
->type
= SLANG_SPEC_BVEC4
;
524 case TYPE_SPECIFIER_INT
:
525 spec
->type
= SLANG_SPEC_INT
;
527 case TYPE_SPECIFIER_IVEC2
:
528 spec
->type
= SLANG_SPEC_IVEC2
;
530 case TYPE_SPECIFIER_IVEC3
:
531 spec
->type
= SLANG_SPEC_IVEC3
;
533 case TYPE_SPECIFIER_IVEC4
:
534 spec
->type
= SLANG_SPEC_IVEC4
;
536 case TYPE_SPECIFIER_FLOAT
:
537 spec
->type
= SLANG_SPEC_FLOAT
;
539 case TYPE_SPECIFIER_VEC2
:
540 spec
->type
= SLANG_SPEC_VEC2
;
542 case TYPE_SPECIFIER_VEC3
:
543 spec
->type
= SLANG_SPEC_VEC3
;
545 case TYPE_SPECIFIER_VEC4
:
546 spec
->type
= SLANG_SPEC_VEC4
;
548 case TYPE_SPECIFIER_MAT2
:
549 spec
->type
= SLANG_SPEC_MAT2
;
551 case TYPE_SPECIFIER_MAT3
:
552 spec
->type
= SLANG_SPEC_MAT3
;
554 case TYPE_SPECIFIER_MAT4
:
555 spec
->type
= SLANG_SPEC_MAT4
;
557 case TYPE_SPECIFIER_MAT23
:
558 spec
->type
= SLANG_SPEC_MAT23
;
560 case TYPE_SPECIFIER_MAT32
:
561 spec
->type
= SLANG_SPEC_MAT32
;
563 case TYPE_SPECIFIER_MAT24
:
564 spec
->type
= SLANG_SPEC_MAT24
;
566 case TYPE_SPECIFIER_MAT42
:
567 spec
->type
= SLANG_SPEC_MAT42
;
569 case TYPE_SPECIFIER_MAT34
:
570 spec
->type
= SLANG_SPEC_MAT34
;
572 case TYPE_SPECIFIER_MAT43
:
573 spec
->type
= SLANG_SPEC_MAT43
;
575 case TYPE_SPECIFIER_SAMPLER1D
:
576 spec
->type
= SLANG_SPEC_SAMPLER1D
;
578 case TYPE_SPECIFIER_SAMPLER2D
:
579 spec
->type
= SLANG_SPEC_SAMPLER2D
;
581 case TYPE_SPECIFIER_SAMPLER3D
:
582 spec
->type
= SLANG_SPEC_SAMPLER3D
;
584 case TYPE_SPECIFIER_SAMPLERCUBE
:
585 spec
->type
= SLANG_SPEC_SAMPLERCUBE
;
587 case TYPE_SPECIFIER_SAMPLER2DRECT
:
588 spec
->type
= SLANG_SPEC_SAMPLER2DRECT
;
590 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
591 spec
->type
= SLANG_SPEC_SAMPLER1DSHADOW
;
593 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
594 spec
->type
= SLANG_SPEC_SAMPLER2DSHADOW
;
596 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
597 spec
->type
= SLANG_SPEC_SAMPLER2DRECTSHADOW
;
599 case TYPE_SPECIFIER_STRUCT
:
600 spec
->type
= SLANG_SPEC_STRUCT
;
601 if (!parse_struct(C
, O
, &spec
->_struct
))
604 case TYPE_SPECIFIER_TYPENAME
:
605 spec
->type
= SLANG_SPEC_STRUCT
;
610 a_name
= parse_identifier(C
);
614 stru
= slang_struct_scope_find(O
->structs
, a_name
, 1);
616 slang_info_log_error(C
->L
, "undeclared type name '%s'",
617 slang_atom_pool_id(C
->atoms
, a_name
));
621 spec
->_struct
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
622 if (spec
->_struct
== NULL
) {
623 slang_info_log_memory(C
->L
);
626 if (!slang_struct_construct(spec
->_struct
)) {
627 _slang_free(spec
->_struct
);
628 spec
->_struct
= NULL
;
631 if (!slang_struct_copy(spec
->_struct
, stru
))
642 parse_fully_specified_type(slang_parse_ctx
* C
, slang_output_ctx
* O
,
643 slang_fully_specified_type
* type
)
645 if (!parse_type_qualifier(C
, &type
->qualifier
))
647 if (!parse_type_specifier(C
, O
, &type
->specifier
))
654 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
655 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
659 #define OP_CONTINUE 6
662 #define OP_EXPRESSION 9
667 #define OP_PUSH_VOID 14
668 #define OP_PUSH_BOOL 15
669 #define OP_PUSH_INT 16
670 #define OP_PUSH_FLOAT 17
671 #define OP_PUSH_IDENTIFIER 18
672 #define OP_SEQUENCE 19
674 #define OP_ADDASSIGN 21
675 #define OP_SUBASSIGN 22
676 #define OP_MULASSIGN 23
677 #define OP_DIVASSIGN 24
678 /*#define OP_MODASSIGN 25*/
679 /*#define OP_LSHASSIGN 26*/
680 /*#define OP_RSHASSIGN 27*/
681 /*#define OP_ORASSIGN 28*/
682 /*#define OP_XORASSIGN 29*/
683 /*#define OP_ANDASSIGN 30*/
685 #define OP_LOGICALOR 32
686 #define OP_LOGICALXOR 33
687 #define OP_LOGICALAND 34
688 /*#define OP_BITOR 35*/
689 /*#define OP_BITXOR 36*/
690 /*#define OP_BITAND 37*/
692 #define OP_NOTEQUAL 39
694 #define OP_GREATER 41
695 #define OP_LESSEQUAL 42
696 #define OP_GREATEREQUAL 43
697 /*#define OP_LSHIFT 44*/
698 /*#define OP_RSHIFT 45*/
700 #define OP_SUBTRACT 47
701 #define OP_MULTIPLY 48
703 /*#define OP_MODULUS 50*/
704 #define OP_PREINCREMENT 51
705 #define OP_PREDECREMENT 52
708 /*#define OP_COMPLEMENT 55*/
710 #define OP_SUBSCRIPT 57
713 #define OP_POSTINCREMENT 60
714 #define OP_POSTDECREMENT 61
718 * When parsing a compound production, this function is used to parse the
720 * For example, a while-loop compound will have two children, the
721 * while condition expression and the loop body. So, this function will
722 * be called twice to parse those two sub-expressions.
723 * \param C the parsing context
724 * \param O the output context
725 * \param oper the operation we're parsing
726 * \param statement indicates whether parsing a statement, or expression
727 * \return 1 if success, 0 if error
730 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
731 slang_operation
* oper
, GLboolean statement
)
735 /* grow child array */
736 ch
= slang_operation_grow(&oper
->num_children
, &oper
->children
);
738 return parse_statement(C
, O
, ch
);
739 return parse_expression(C
, O
, ch
);
742 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
745 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
746 slang_operation
* oper
)
748 oper
->locals
->outer_scope
= O
->vars
;
750 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
751 /* parse child statements, do not create new variable scope */
752 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
753 while (*C
->I
!= OP_END
)
754 if (!parse_child_operation(C
, O
, oper
, 1))
758 case OP_BLOCK_BEGIN_NEW_SCOPE
:
759 /* parse child statements, create new variable scope */
761 slang_output_ctx o
= *O
;
763 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
764 o
.vars
= oper
->locals
;
765 while (*C
->I
!= OP_END
)
766 if (!parse_child_operation(C
, &o
, oper
, 1))
772 /* local variable declaration, individual declarators are stored as
773 * children identifiers
775 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
777 const unsigned int first_var
= O
->vars
->num_variables
;
779 /* parse the declaration, note that there can be zero or more
780 * than one declarators
782 if (!parse_declaration(C
, O
))
784 if (first_var
< O
->vars
->num_variables
) {
785 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
787 assert(oper
->num_children
== 0);
788 oper
->num_children
= num_vars
;
789 oper
->children
= slang_operation_new(num_vars
);
790 if (oper
->children
== NULL
) {
791 slang_info_log_memory(C
->L
);
794 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
795 slang_operation
*o
= &oper
->children
[i
- first_var
];
796 o
->type
= SLANG_OPER_VARIABLE_DECL
;
797 o
->locals
->outer_scope
= O
->vars
;
798 o
->a_id
= O
->vars
->variables
[i
]->a_name
;
804 /* the __asm statement, parse the mnemonic and all its arguments
807 oper
->type
= SLANG_OPER_ASM
;
808 oper
->a_id
= parse_identifier(C
);
809 if (oper
->a_id
== SLANG_ATOM_NULL
)
811 while (*C
->I
!= OP_END
) {
812 if (!parse_child_operation(C
, O
, oper
, 0))
818 oper
->type
= SLANG_OPER_BREAK
;
821 oper
->type
= SLANG_OPER_CONTINUE
;
824 oper
->type
= SLANG_OPER_DISCARD
;
827 oper
->type
= SLANG_OPER_RETURN
;
828 if (!parse_child_operation(C
, O
, oper
, 0))
832 oper
->type
= SLANG_OPER_EXPRESSION
;
833 if (!parse_child_operation(C
, O
, oper
, 0))
837 oper
->type
= SLANG_OPER_IF
;
838 if (!parse_child_operation(C
, O
, oper
, 0))
840 if (!parse_child_operation(C
, O
, oper
, 1))
842 if (!parse_child_operation(C
, O
, oper
, 1))
847 slang_output_ctx o
= *O
;
849 oper
->type
= SLANG_OPER_WHILE
;
850 o
.vars
= oper
->locals
;
851 if (!parse_child_operation(C
, &o
, oper
, 1))
853 if (!parse_child_operation(C
, &o
, oper
, 1))
858 oper
->type
= SLANG_OPER_DO
;
859 if (!parse_child_operation(C
, O
, oper
, 1))
861 if (!parse_child_operation(C
, O
, oper
, 0))
866 slang_output_ctx o
= *O
;
868 oper
->type
= SLANG_OPER_FOR
;
869 o
.vars
= oper
->locals
;
870 if (!parse_child_operation(C
, &o
, oper
, 1))
872 if (!parse_child_operation(C
, &o
, oper
, 1))
874 if (!parse_child_operation(C
, &o
, oper
, 0))
876 if (!parse_child_operation(C
, &o
, oper
, 1))
887 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
888 slang_operation
** ops
, unsigned int *total_ops
,
893 op
->children
= slang_operation_new(n
);
894 if (op
->children
== NULL
) {
895 slang_info_log_memory(C
->L
);
898 op
->num_children
= n
;
900 for (i
= 0; i
< n
; i
++) {
901 slang_operation_destruct(&op
->children
[i
]);
902 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
905 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
908 *ops
= (slang_operation
*)
910 (*total_ops
+ n
) * sizeof(slang_operation
),
911 *total_ops
* sizeof(slang_operation
));
913 slang_info_log_memory(C
->L
);
920 is_constructor_name(const char *name
, slang_atom a_name
,
921 slang_struct_scope
* structs
)
923 if (slang_type_specifier_type_from_string(name
) != SLANG_SPEC_VOID
)
925 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
929 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
930 slang_operation
* oper
)
932 slang_operation
*ops
= NULL
;
933 unsigned int num_ops
= 0;
936 while (*C
->I
!= OP_END
) {
938 const unsigned int op_code
= *C
->I
++;
940 /* allocate default operation, becomes a no-op if not used */
941 ops
= (slang_operation
*)
943 num_ops
* sizeof(slang_operation
),
944 (num_ops
+ 1) * sizeof(slang_operation
));
946 slang_info_log_memory(C
->L
);
950 if (!slang_operation_construct(op
)) {
951 slang_info_log_memory(C
->L
);
955 op
->locals
->outer_scope
= O
->vars
;
959 op
->type
= SLANG_OPER_VOID
;
962 op
->type
= SLANG_OPER_LITERAL_BOOL
;
963 if (!parse_number(C
, &number
))
968 op
->literal
[3] = (GLfloat
) number
;
969 op
->literal_size
= 1;
972 op
->type
= SLANG_OPER_LITERAL_INT
;
973 if (!parse_number(C
, &number
))
978 op
->literal
[3] = (GLfloat
) number
;
979 op
->literal_size
= 1;
982 op
->type
= SLANG_OPER_LITERAL_FLOAT
;
983 if (!parse_float(C
, &op
->literal
[0]))
987 op
->literal
[3] = op
->literal
[0];
988 op
->literal_size
= 1;
990 case OP_PUSH_IDENTIFIER
:
991 op
->type
= SLANG_OPER_IDENTIFIER
;
992 op
->a_id
= parse_identifier(C
);
993 if (op
->a_id
== SLANG_ATOM_NULL
)
997 op
->type
= SLANG_OPER_SEQUENCE
;
998 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1002 op
->type
= SLANG_OPER_ASSIGN
;
1003 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1007 op
->type
= SLANG_OPER_ADDASSIGN
;
1008 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1012 op
->type
= SLANG_OPER_SUBASSIGN
;
1013 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1017 op
->type
= SLANG_OPER_MULASSIGN
;
1018 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1022 op
->type
= SLANG_OPER_DIVASSIGN
;
1023 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1026 /*case OP_MODASSIGN: */
1027 /*case OP_LSHASSIGN: */
1028 /*case OP_RSHASSIGN: */
1029 /*case OP_ORASSIGN: */
1030 /*case OP_XORASSIGN: */
1031 /*case OP_ANDASSIGN: */
1033 op
->type
= SLANG_OPER_SELECT
;
1034 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1038 op
->type
= SLANG_OPER_LOGICALOR
;
1039 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1043 op
->type
= SLANG_OPER_LOGICALXOR
;
1044 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1048 op
->type
= SLANG_OPER_LOGICALAND
;
1049 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1053 /*case OP_BITXOR: */
1054 /*case OP_BITAND: */
1056 op
->type
= SLANG_OPER_EQUAL
;
1057 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1061 op
->type
= SLANG_OPER_NOTEQUAL
;
1062 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1066 op
->type
= SLANG_OPER_LESS
;
1067 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1071 op
->type
= SLANG_OPER_GREATER
;
1072 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1076 op
->type
= SLANG_OPER_LESSEQUAL
;
1077 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1080 case OP_GREATEREQUAL
:
1081 op
->type
= SLANG_OPER_GREATEREQUAL
;
1082 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1085 /*case OP_LSHIFT: */
1086 /*case OP_RSHIFT: */
1088 op
->type
= SLANG_OPER_ADD
;
1089 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1093 op
->type
= SLANG_OPER_SUBTRACT
;
1094 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1098 op
->type
= SLANG_OPER_MULTIPLY
;
1099 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1103 op
->type
= SLANG_OPER_DIVIDE
;
1104 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1107 /*case OP_MODULUS: */
1108 case OP_PREINCREMENT
:
1109 op
->type
= SLANG_OPER_PREINCREMENT
;
1110 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1113 case OP_PREDECREMENT
:
1114 op
->type
= SLANG_OPER_PREDECREMENT
;
1115 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1119 op
->type
= SLANG_OPER_PLUS
;
1120 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1124 op
->type
= SLANG_OPER_MINUS
;
1125 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1129 op
->type
= SLANG_OPER_NOT
;
1130 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1133 /*case OP_COMPLEMENT: */
1135 op
->type
= SLANG_OPER_SUBSCRIPT
;
1136 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1140 op
->type
= SLANG_OPER_CALL
;
1141 op
->a_id
= parse_identifier(C
);
1142 if (op
->a_id
== SLANG_ATOM_NULL
)
1144 while (*C
->I
!= OP_END
)
1145 if (!parse_child_operation(C
, O
, op
, 0))
1149 if (!C
->parsing_builtin
1150 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1153 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1154 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1155 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1161 op
->type
= SLANG_OPER_FIELD
;
1162 op
->a_id
= parse_identifier(C
);
1163 if (op
->a_id
== SLANG_ATOM_NULL
)
1165 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1168 case OP_POSTINCREMENT
:
1169 op
->type
= SLANG_OPER_POSTINCREMENT
;
1170 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1173 case OP_POSTDECREMENT
:
1174 op
->type
= SLANG_OPER_POSTDECREMENT
;
1175 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1184 slang_operation_destruct(oper
);
1185 *oper
= *ops
; /* struct copy */
1191 /* parameter qualifier */
1192 #define PARAM_QUALIFIER_IN 0
1193 #define PARAM_QUALIFIER_OUT 1
1194 #define PARAM_QUALIFIER_INOUT 2
1196 /* function parameter array presence */
1197 #define PARAMETER_ARRAY_NOT_PRESENT 0
1198 #define PARAMETER_ARRAY_PRESENT 1
1201 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1202 slang_variable
* param
)
1204 /* parse and validate the parameter's type qualifiers (there can be
1205 * two at most) because not all combinations are valid
1207 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1210 case PARAM_QUALIFIER_IN
:
1211 if (param
->type
.qualifier
!= SLANG_QUAL_CONST
1212 && param
->type
.qualifier
!= SLANG_QUAL_NONE
) {
1213 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1217 case PARAM_QUALIFIER_OUT
:
1218 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1219 param
->type
.qualifier
= SLANG_QUAL_OUT
;
1221 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1225 case PARAM_QUALIFIER_INOUT
:
1226 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1227 param
->type
.qualifier
= SLANG_QUAL_INOUT
;
1229 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1237 /* parse parameter's type specifier and name */
1238 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1240 param
->a_name
= parse_identifier(C
);
1241 if (param
->a_name
== SLANG_ATOM_NULL
)
1244 /* if the parameter is an array, parse its size (the size must be
1245 * explicitly defined
1247 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1248 slang_type_specifier p
;
1250 slang_type_specifier_ctr(&p
);
1251 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1252 slang_type_specifier_dtr(&p
);
1255 if (!convert_to_array(C
, param
, &p
)) {
1256 slang_type_specifier_dtr(&p
);
1259 slang_type_specifier_dtr(&p
);
1260 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1264 /* calculate the parameter size */
1265 if (!calculate_var_size(C
, O
, param
))
1268 /* TODO: allocate the local address here? */
1273 #define FUNCTION_ORDINARY 0
1274 #define FUNCTION_CONSTRUCTOR 1
1275 #define FUNCTION_OPERATOR 2
1277 /* function parameter */
1278 #define PARAMETER_NONE 0
1279 #define PARAMETER_NEXT 1
1282 #define OPERATOR_ADDASSIGN 1
1283 #define OPERATOR_SUBASSIGN 2
1284 #define OPERATOR_MULASSIGN 3
1285 #define OPERATOR_DIVASSIGN 4
1286 /*#define OPERATOR_MODASSIGN 5*/
1287 /*#define OPERATOR_LSHASSIGN 6*/
1288 /*#define OPERATOR_RSHASSIGN 7*/
1289 /*#define OPERATOR_ANDASSIGN 8*/
1290 /*#define OPERATOR_XORASSIGN 9*/
1291 /*#define OPERATOR_ORASSIGN 10*/
1292 #define OPERATOR_LOGICALXOR 11
1293 /*#define OPERATOR_BITOR 12*/
1294 /*#define OPERATOR_BITXOR 13*/
1295 /*#define OPERATOR_BITAND 14*/
1296 #define OPERATOR_LESS 15
1297 #define OPERATOR_GREATER 16
1298 #define OPERATOR_LESSEQUAL 17
1299 #define OPERATOR_GREATEREQUAL 18
1300 /*#define OPERATOR_LSHIFT 19*/
1301 /*#define OPERATOR_RSHIFT 20*/
1302 #define OPERATOR_MULTIPLY 21
1303 #define OPERATOR_DIVIDE 22
1304 /*#define OPERATOR_MODULUS 23*/
1305 #define OPERATOR_INCREMENT 24
1306 #define OPERATOR_DECREMENT 25
1307 #define OPERATOR_PLUS 26
1308 #define OPERATOR_MINUS 27
1309 /*#define OPERATOR_COMPLEMENT 28*/
1310 #define OPERATOR_NOT 29
1314 unsigned int o_code
;
1316 } operator_names
[] = {
1317 {OPERATOR_INCREMENT
, "++"},
1318 {OPERATOR_ADDASSIGN
, "+="},
1319 {OPERATOR_PLUS
, "+"},
1320 {OPERATOR_DECREMENT
, "--"},
1321 {OPERATOR_SUBASSIGN
, "-="},
1322 {OPERATOR_MINUS
, "-"},
1323 {OPERATOR_NOT
, "!"},
1324 {OPERATOR_MULASSIGN
, "*="},
1325 {OPERATOR_MULTIPLY
, "*"},
1326 {OPERATOR_DIVASSIGN
, "/="},
1327 {OPERATOR_DIVIDE
, "/"},
1328 {OPERATOR_LESSEQUAL
, "<="},
1329 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1330 /*{ OPERATOR_LSHIFT, "<<" }, */
1331 {OPERATOR_LESS
, "<"},
1332 {OPERATOR_GREATEREQUAL
, ">="},
1333 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1334 /*{ OPERATOR_RSHIFT, ">>" }, */
1335 {OPERATOR_GREATER
, ">"},
1336 /*{ OPERATOR_MODASSIGN, "%=" }, */
1337 /*{ OPERATOR_MODULUS, "%" }, */
1338 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1339 /*{ OPERATOR_BITAND, "&" }, */
1340 /*{ OPERATOR_ORASSIGN, "|=" }, */
1341 /*{ OPERATOR_BITOR, "|" }, */
1342 /*{ OPERATOR_COMPLEMENT, "~" }, */
1343 /*{ OPERATOR_XORASSIGN, "^=" }, */
1344 {OPERATOR_LOGICALXOR
, "^^"},
1345 /*{ OPERATOR_BITXOR, "^" } */
1349 parse_operator_name(slang_parse_ctx
* C
)
1353 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1354 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1356 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1357 if (atom
== SLANG_ATOM_NULL
) {
1358 slang_info_log_memory(C
->L
);
1369 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1370 slang_function
* func
)
1372 /* parse function type and name */
1373 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1376 case FUNCTION_ORDINARY
:
1377 func
->kind
= SLANG_FUNC_ORDINARY
;
1378 func
->header
.a_name
= parse_identifier(C
);
1379 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1382 case FUNCTION_CONSTRUCTOR
:
1383 func
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1384 if (func
->header
.type
.specifier
.type
== SLANG_SPEC_STRUCT
)
1386 func
->header
.a_name
=
1387 slang_atom_pool_atom(C
->atoms
,
1388 slang_type_specifier_type_to_string
1389 (func
->header
.type
.specifier
.type
));
1390 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1391 slang_info_log_memory(C
->L
);
1395 case FUNCTION_OPERATOR
:
1396 func
->kind
= SLANG_FUNC_OPERATOR
;
1397 func
->header
.a_name
= parse_operator_name(C
);
1398 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1405 /* parse function parameters */
1406 while (*C
->I
++ == PARAMETER_NEXT
) {
1407 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1409 slang_info_log_memory(C
->L
);
1412 if (!parse_parameter_declaration(C
, O
, p
))
1416 /* if the function returns a value, append a hidden __retVal 'out'
1417 * parameter that corresponds to the return value.
1419 if (_slang_function_has_return_value(func
)) {
1420 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1421 slang_atom a_retVal
= slang_atom_pool_atom(C
->atoms
, "__retVal");
1423 p
->a_name
= a_retVal
;
1424 p
->type
= func
->header
.type
;
1425 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1428 /* function formal parameters and local variables share the same
1429 * scope, so save the information about param count in a seperate
1430 * place also link the scope to the global variable scope so when a
1431 * given identifier is not found here, the search process continues
1432 * in the global space
1434 func
->param_count
= func
->parameters
->num_variables
;
1435 func
->parameters
->outer_scope
= O
->vars
;
1441 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1442 slang_function
* func
)
1444 slang_output_ctx o
= *O
;
1446 if (!parse_function_prototype(C
, O
, func
))
1449 /* create function's body operation */
1450 func
->body
= (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1451 if (func
->body
== NULL
) {
1452 slang_info_log_memory(C
->L
);
1455 if (!slang_operation_construct(func
->body
)) {
1456 _slang_free(func
->body
);
1458 slang_info_log_memory(C
->L
);
1462 /* to parse the body the parse context is modified in order to
1463 * capture parsed variables into function's local variable scope
1465 C
->global_scope
= GL_FALSE
;
1466 o
.vars
= func
->parameters
;
1467 if (!parse_statement(C
, &o
, func
->body
))
1470 C
->global_scope
= GL_TRUE
;
1475 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1477 slang_operation op_id
, op_assign
;
1480 /* construct the left side of assignment */
1481 if (!slang_operation_construct(&op_id
))
1483 op_id
.type
= SLANG_OPER_IDENTIFIER
;
1484 op_id
.a_id
= var
->a_name
;
1486 /* put the variable into operation's scope */
1487 op_id
.locals
->variables
=
1488 (slang_variable
**) _slang_alloc(sizeof(slang_variable
*));
1489 if (op_id
.locals
->variables
== NULL
) {
1490 slang_operation_destruct(&op_id
);
1493 op_id
.locals
->num_variables
= 1;
1494 op_id
.locals
->variables
[0] = var
;
1496 /* construct the assignment expression */
1497 if (!slang_operation_construct(&op_assign
)) {
1498 op_id
.locals
->num_variables
= 0;
1499 slang_operation_destruct(&op_id
);
1502 op_assign
.type
= SLANG_OPER_ASSIGN
;
1503 op_assign
.children
=
1504 (slang_operation
*) _slang_alloc(2 * sizeof(slang_operation
));
1505 if (op_assign
.children
== NULL
) {
1506 slang_operation_destruct(&op_assign
);
1507 op_id
.locals
->num_variables
= 0;
1508 slang_operation_destruct(&op_id
);
1511 op_assign
.num_children
= 2;
1512 op_assign
.children
[0] = op_id
;
1513 op_assign
.children
[1] = *var
->initializer
;
1517 /* carefully destroy the operations */
1518 op_assign
.num_children
= 0;
1519 _slang_free(op_assign
.children
);
1520 op_assign
.children
= NULL
;
1521 slang_operation_destruct(&op_assign
);
1522 op_id
.locals
->num_variables
= 0;
1523 slang_operation_destruct(&op_id
);
1531 /* init declarator list */
1532 #define DECLARATOR_NONE 0
1533 #define DECLARATOR_NEXT 1
1535 /* variable declaration */
1536 #define VARIABLE_NONE 0
1537 #define VARIABLE_IDENTIFIER 1
1538 #define VARIABLE_INITIALIZER 2
1539 #define VARIABLE_ARRAY_EXPLICIT 3
1540 #define VARIABLE_ARRAY_UNKNOWN 4
1544 * Parse the initializer for a variable declaration.
1547 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1548 const slang_fully_specified_type
* type
)
1550 slang_variable
*var
;
1552 /* empty init declatator (without name, e.g. "float ;") */
1553 if (*C
->I
++ == VARIABLE_NONE
)
1556 /* make room for the new variable and initialize it */
1557 var
= slang_variable_scope_grow(O
->vars
);
1559 slang_info_log_memory(C
->L
);
1563 /* copy the declarator qualifier type, parse the identifier */
1564 var
->type
.qualifier
= type
->qualifier
;
1565 var
->a_name
= parse_identifier(C
);
1566 if (var
->a_name
== SLANG_ATOM_NULL
)
1571 /* simple variable declarator - just copy the specifier */
1572 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1575 case VARIABLE_INITIALIZER
:
1576 /* initialized variable - copy the specifier and parse the expression */
1577 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1580 (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1581 if (var
->initializer
== NULL
) {
1582 slang_info_log_memory(C
->L
);
1585 if (!slang_operation_construct(var
->initializer
)) {
1586 _slang_free(var
->initializer
);
1587 var
->initializer
= NULL
;
1588 slang_info_log_memory(C
->L
);
1591 if (!parse_expression(C
, O
, var
->initializer
))
1594 case VARIABLE_ARRAY_UNKNOWN
:
1595 /* unsized array - mark it as array and copy the specifier to
1598 if (!convert_to_array(C
, var
, &type
->specifier
))
1601 case VARIABLE_ARRAY_EXPLICIT
:
1602 if (!convert_to_array(C
, var
, &type
->specifier
))
1604 if (!parse_array_len(C
, O
, &var
->array_len
))
1611 /* emit code for global var decl */
1612 if (C
->global_scope
) {
1613 slang_assemble_ctx A
;
1615 A
.space
.funcs
= O
->funs
;
1616 A
.space
.structs
= O
->structs
;
1617 A
.space
.vars
= O
->vars
;
1618 A
.program
= O
->program
;
1619 A
.vartable
= O
->vartable
;
1620 A
.curFuncEndLabel
= NULL
;
1621 if (!_slang_codegen_global_variable(&A
, var
, C
->type
))
1625 /* allocate global address space for a variable with a known size */
1627 && !(var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
1628 && var
->array_len
== 0)) {
1629 if (!calculate_var_size(C
, O
, var
))
1631 var
->address
= slang_var_pool_alloc(O
->global_pool
, var
->size
);
1634 /* initialize global variable */
1635 if (C
->global_scope
) {
1636 if (var
->initializer
!= NULL
) {
1637 slang_assemble_ctx A
;
1640 A
.space
.funcs
= O
->funs
;
1641 A
.space
.structs
= O
->structs
;
1642 A
.space
.vars
= O
->vars
;
1643 if (!initialize_global(&A
, var
))
1651 * Parse a list of variable declarations. Each variable may have an
1655 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1657 slang_fully_specified_type type
;
1659 /* parse the fully specified type, common to all declarators */
1660 if (!slang_fully_specified_type_construct(&type
))
1662 if (!parse_fully_specified_type(C
, O
, &type
)) {
1663 slang_fully_specified_type_destruct(&type
);
1667 /* parse declarators, pass-in the parsed type */
1669 if (!parse_init_declarator(C
, O
, &type
)) {
1670 slang_fully_specified_type_destruct(&type
);
1674 while (*C
->I
++ == DECLARATOR_NEXT
);
1676 slang_fully_specified_type_destruct(&type
);
1682 * Parse a function definition or declaration.
1683 * \param C parsing context
1684 * \param O output context
1685 * \param definition if non-zero expect a definition, else a declaration
1686 * \param parsed_func_ret returns the parsed function
1687 * \return GL_TRUE if success, GL_FALSE if failure
1690 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
1691 slang_function
** parsed_func_ret
)
1693 slang_function parsed_func
, *found_func
;
1695 /* parse function definition/declaration */
1696 if (!slang_function_construct(&parsed_func
))
1699 if (!parse_function_definition(C
, O
, &parsed_func
)) {
1700 slang_function_destruct(&parsed_func
);
1705 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
1706 slang_function_destruct(&parsed_func
);
1711 /* find a function with a prototype matching the parsed one - only
1712 * the current scope is being searched to allow built-in function
1715 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
1716 if (found_func
== NULL
) {
1717 /* New function, add it to the function list */
1718 O
->funs
->functions
=
1719 (slang_function
*) _slang_realloc(O
->funs
->functions
,
1720 O
->funs
->num_functions
1721 * sizeof(slang_function
),
1722 (O
->funs
->num_functions
+ 1)
1723 * sizeof(slang_function
));
1724 if (O
->funs
->functions
== NULL
) {
1725 slang_info_log_memory(C
->L
);
1726 slang_function_destruct(&parsed_func
);
1729 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
1730 O
->funs
->num_functions
++;
1732 /* return the newly parsed function */
1733 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
1736 /* previously defined or declared */
1737 /* TODO: check function return type qualifiers and specifiers */
1739 if (found_func
->body
!= NULL
) {
1740 slang_info_log_error(C
->L
, "%s: function already has a body.",
1741 slang_atom_pool_id(C
->atoms
,
1744 slang_function_destruct(&parsed_func
);
1748 /* destroy the existing function declaration and replace it
1749 * with the new one, remember to save the fixup table
1751 parsed_func
.fixups
= found_func
->fixups
;
1752 slang_fixup_table_init(&found_func
->fixups
);
1753 slang_function_destruct(found_func
);
1754 *found_func
= parsed_func
;
1757 /* another declaration of the same function prototype - ignore it */
1758 slang_function_destruct(&parsed_func
);
1761 /* return the found function */
1762 *parsed_func_ret
= found_func
;
1765 /* assemble the parsed function */
1767 slang_assemble_ctx A
;
1770 A
.space
.funcs
= O
->funs
;
1771 A
.space
.structs
= O
->structs
;
1772 A
.space
.vars
= O
->vars
;
1773 A
.program
= O
->program
;
1774 A
.vartable
= O
->vartable
;
1777 _slang_codegen_function(&A
, *parsed_func_ret
);
1783 #define DECLARATION_FUNCTION_PROTOTYPE 1
1784 #define DECLARATION_INIT_DECLARATOR_LIST 2
1787 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1790 case DECLARATION_INIT_DECLARATOR_LIST
:
1791 if (!parse_init_declarator_list(C
, O
))
1794 case DECLARATION_FUNCTION_PROTOTYPE
:
1796 slang_function
*dummy_func
;
1798 if (!parse_function(C
, O
, 0, &dummy_func
))
1808 /* external declaration */
1809 #define EXTERNAL_NULL 0
1810 #define EXTERNAL_FUNCTION_DEFINITION 1
1811 #define EXTERNAL_DECLARATION 2
1814 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
,
1815 struct gl_program
*program
)
1817 GET_CURRENT_CONTEXT(ctx
);
1822 if (unit
->type
== SLANG_UNIT_FRAGMENT_BUILTIN
||
1823 unit
->type
== SLANG_UNIT_FRAGMENT_SHADER
) {
1824 maxRegs
= ctx
->Const
.FragmentProgram
.MaxTemps
;
1827 assert(unit
->type
== SLANG_UNIT_VERTEX_BUILTIN
||
1828 unit
->type
== SLANG_UNIT_VERTEX_SHADER
);
1829 maxRegs
= ctx
->Const
.VertexProgram
.MaxTemps
;
1832 /* setup output context */
1833 o
.funs
= &unit
->funs
;
1834 o
.structs
= &unit
->structs
;
1835 o
.vars
= &unit
->vars
;
1836 o
.global_pool
= &unit
->object
->varpool
;
1837 o
.program
= program
;
1838 o
.vartable
= _slang_new_var_table(maxRegs
);
1839 _slang_push_var_table(o
.vartable
);
1841 /* parse individual functions and declarations */
1842 while (*C
->I
!= EXTERNAL_NULL
) {
1844 case EXTERNAL_FUNCTION_DEFINITION
:
1846 slang_function
*func
;
1847 success
= parse_function(C
, &o
, 1, &func
);
1850 case EXTERNAL_DECLARATION
:
1851 success
= parse_declaration(C
, &o
);
1858 /* xxx free codegen */
1859 _slang_pop_var_table(o
.vartable
);
1865 _slang_pop_var_table(o
.vartable
);
1866 _slang_delete_var_table(o
.vartable
);
1872 compile_binary(const byte
* prod
, slang_code_unit
* unit
,
1873 slang_unit_type type
, slang_info_log
* infolog
,
1874 slang_code_unit
* builtin
, slang_code_unit
* downlink
,
1875 struct gl_program
*program
)
1881 /* setup parse context */
1884 C
.parsing_builtin
= (builtin
== NULL
);
1885 C
.global_scope
= GL_TRUE
;
1886 C
.atoms
= &unit
->object
->atompool
;
1889 if (!check_revision(&C
))
1892 if (downlink
!= NULL
) {
1893 unit
->vars
.outer_scope
= &downlink
->vars
;
1894 unit
->funs
.outer_scope
= &downlink
->funs
;
1895 unit
->structs
.outer_scope
= &downlink
->structs
;
1898 /* parse translation unit */
1899 return parse_code_unit(&C
, unit
, program
);
1903 compile_with_grammar(grammar id
, const char *source
, slang_code_unit
* unit
,
1904 slang_unit_type type
, slang_info_log
* infolog
,
1905 slang_code_unit
* builtin
,
1906 struct gl_program
*program
)
1909 GLuint size
, start
, version
;
1910 slang_string preprocessed
;
1913 #if FEATURE_ARB_shading_language_120
1919 /* First retrieve the version number. */
1920 if (!_slang_preprocess_version(source
, &version
, &start
, infolog
))
1923 if (version
> maxVersion
) {
1924 slang_info_log_error(infolog
,
1925 "language version %.2f is not supported.",
1930 /* Now preprocess the source string. */
1931 slang_string_init(&preprocessed
);
1932 if (!_slang_preprocess_directives(&preprocessed
, &source
[start
], infolog
)) {
1933 slang_string_free(&preprocessed
);
1934 slang_info_log_error(infolog
, "failed to preprocess the source.");
1938 /* Finally check the syntax and generate its binary representation. */
1939 if (!grammar_fast_check(id
,
1940 (const byte
*) (slang_string_cstr(&preprocessed
)),
1941 &prod
, &size
, 65536)) {
1945 slang_string_free(&preprocessed
);
1946 grammar_get_last_error((byte
*) (buf
), sizeof(buf
), &pos
);
1947 slang_info_log_error(infolog
, buf
);
1948 /* syntax error (possibly in library code) */
1953 s
= (char *) _mesa_find_line_column((const GLubyte
*) source
,
1954 (const GLubyte
*) source
+ pos
,
1956 printf("Error on line %d, col %d: %s\n", line
, col
, s
);
1961 slang_string_free(&preprocessed
);
1963 /* Syntax is okay - translate it to internal representation. */
1964 if (!compile_binary(prod
, unit
, type
, infolog
, builtin
,
1965 &builtin
[SLANG_BUILTIN_TOTAL
- 1],
1967 grammar_alloc_free(prod
);
1970 grammar_alloc_free(prod
);
1974 LONGSTRING
static const char *slang_shader_syn
=
1975 #include "library/slang_shader_syn.h"
1978 static const byte slang_core_gc
[] = {
1979 #include "library/slang_core_gc.h"
1982 static const byte slang_120_core_gc
[] = {
1983 #include "library/slang_120_core_gc.h"
1986 static const byte slang_common_builtin_gc
[] = {
1987 #include "library/slang_common_builtin_gc.h"
1990 static const byte slang_fragment_builtin_gc
[] = {
1991 #include "library/slang_fragment_builtin_gc.h"
1994 static const byte slang_vertex_builtin_gc
[] = {
1995 #include "library/slang_vertex_builtin_gc.h"
1999 compile_object(grammar
* id
, const char *source
, slang_code_object
* object
,
2000 slang_unit_type type
, slang_info_log
* infolog
,
2001 struct gl_program
*program
)
2003 slang_code_unit
*builtins
= NULL
;
2005 /* load GLSL grammar */
2006 *id
= grammar_load_from_text((const byte
*) (slang_shader_syn
));
2011 grammar_get_last_error(buf
, 1024, &pos
);
2012 slang_info_log_error(infolog
, (const char *) (buf
));
2016 /* set shader type - the syntax is slightly different for different shaders */
2017 if (type
== SLANG_UNIT_FRAGMENT_SHADER
2018 || type
== SLANG_UNIT_FRAGMENT_BUILTIN
)
2019 grammar_set_reg8(*id
, (const byte
*) "shader_type", 1);
2021 grammar_set_reg8(*id
, (const byte
*) "shader_type", 2);
2023 /* enable language extensions */
2024 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2026 /* if parsing user-specified shader, load built-in library */
2027 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_VERTEX_SHADER
) {
2028 /* compile core functionality first */
2029 if (!compile_binary(slang_core_gc
,
2030 &object
->builtin
[SLANG_BUILTIN_CORE
],
2031 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2035 #if FEATURE_ARB_shading_language_120
2036 if (!compile_binary(slang_120_core_gc
,
2037 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2038 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2039 NULL
, &object
->builtin
[SLANG_BUILTIN_CORE
], NULL
))
2043 /* compile common functions and variables, link to core */
2044 if (!compile_binary(slang_common_builtin_gc
,
2045 &object
->builtin
[SLANG_BUILTIN_COMMON
],
2046 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2047 #if FEATURE_ARB_shading_language_120
2048 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2050 &object
->builtin
[SLANG_BUILTIN_CORE
],
2055 /* compile target-specific functions and variables, link to common */
2056 if (type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2057 if (!compile_binary(slang_fragment_builtin_gc
,
2058 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2059 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2060 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2063 else if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2064 if (!compile_binary(slang_vertex_builtin_gc
,
2065 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2066 SLANG_UNIT_VERTEX_BUILTIN
, infolog
, NULL
,
2067 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2071 /* disable language extensions */
2072 #if NEW_SLANG /* allow-built-ins */
2073 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2075 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 0);
2077 builtins
= object
->builtin
;
2080 /* compile the actual shader - pass-in built-in library for external shader */
2081 return compile_with_grammar(*id
, source
, &object
->unit
, type
, infolog
,
2087 compile_shader(GLcontext
*ctx
, slang_code_object
* object
,
2088 slang_unit_type type
, slang_info_log
* infolog
,
2089 struct gl_shader
*shader
)
2091 struct gl_program
*program
= shader
->Programs
[0];
2097 _slang_code_object_dtr(object
);
2098 _slang_code_object_ctr(object
);
2100 success
= compile_object(&id
, shader
->Source
, object
, type
, infolog
, program
);
2102 grammar_destroy(id
);
2112 _slang_compile(GLcontext
*ctx
, struct gl_shader
*shader
)
2115 slang_info_log info_log
;
2116 slang_code_object obj
;
2117 slang_unit_type type
;
2119 if (shader
->Type
== GL_VERTEX_SHADER
) {
2120 type
= SLANG_UNIT_VERTEX_SHADER
;
2123 assert(shader
->Type
== GL_FRAGMENT_SHADER
);
2124 type
= SLANG_UNIT_FRAGMENT_SHADER
;
2127 ctx
->Shader
.MemPool
= _slang_new_mempool(1024*1024);
2129 /* XXX temporary hack */
2130 if (!shader
->Programs
) {
2132 if (shader
->Type
== GL_VERTEX_SHADER
)
2133 progTarget
= GL_VERTEX_PROGRAM_ARB
;
2135 progTarget
= GL_FRAGMENT_PROGRAM_ARB
;
2137 = (struct gl_program
**) malloc(sizeof(struct gl_program
*));
2138 shader
->Programs
[0] = _mesa_new_program(ctx
, progTarget
, 1);
2139 shader
->NumPrograms
= 1;
2141 shader
->Programs
[0]->Parameters
= _mesa_new_parameter_list();
2142 shader
->Programs
[0]->Varying
= _mesa_new_parameter_list();
2143 shader
->Programs
[0]->Attributes
= _mesa_new_parameter_list();
2146 slang_info_log_construct(&info_log
);
2147 _slang_code_object_ctr(&obj
);
2149 success
= compile_shader(ctx
, &obj
, type
, &info_log
, shader
);
2151 /* free shader's prev info log */
2152 if (shader
->InfoLog
) {
2153 _mesa_free(shader
->InfoLog
);
2154 shader
->InfoLog
= NULL
;
2157 if (info_log
.text
) {
2158 /* copy info-log string to shader object */
2159 shader
->InfoLog
= _mesa_strdup(info_log
.text
);
2162 if (info_log
.error_flag
) {
2166 slang_info_log_destruct(&info_log
);
2167 _slang_code_object_dtr(&obj
);
2169 _slang_delete_mempool((slang_mempool
*) ctx
->Shader
.MemPool
);
2170 ctx
->Shader
.MemPool
= NULL
;