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
32 #include "grammar_mesa.h"
33 #include "slang_compile.h"
34 #include "slang_preprocess.h"
35 #include "slang_storage.h"
38 * This is a straightforward implementation of the slang front-end
39 * compiler. Lots of error-checking functionality is missing but
40 * every well-formed shader source should compile successfully and
41 * execute as expected. However, some semantically ill-formed shaders
42 * may be accepted resulting in undefined behaviour.
48 * Allocate storage for a variable of 'size' bytes from given pool.
49 * Return the allocated address for the variable.
52 slang_var_pool_alloc(slang_var_pool
* pool
, unsigned int size
)
54 const GLuint addr
= pool
->next_addr
;
55 pool
->next_addr
+= size
;
64 _slang_code_unit_ctr(slang_code_unit
* self
,
65 struct slang_code_object_
* object
)
67 _slang_variable_scope_ctr(&self
->vars
);
68 _slang_function_scope_ctr(&self
->funs
);
69 _slang_struct_scope_ctr(&self
->structs
);
70 self
->object
= object
;
74 _slang_code_unit_dtr(slang_code_unit
* self
)
76 slang_variable_scope_destruct(&self
->vars
);
77 slang_function_scope_destruct(&self
->funs
);
78 slang_struct_scope_destruct(&self
->structs
);
86 _slang_code_object_ctr(slang_code_object
* self
)
90 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
91 _slang_code_unit_ctr(&self
->builtin
[i
], self
);
92 _slang_code_unit_ctr(&self
->unit
, self
);
93 _slang_assembly_file_ctr(&self
->assembly
);
94 slang_machine_ctr(&self
->machine
);
95 self
->varpool
.next_addr
= 0;
96 slang_atom_pool_construct(&self
->atompool
);
97 slang_export_data_table_ctr(&self
->expdata
);
98 self
->expdata
.atoms
= &self
->atompool
;
99 slang_export_code_table_ctr(&self
->expcode
);
100 self
->expcode
.atoms
= &self
->atompool
;
104 _slang_code_object_dtr(slang_code_object
* self
)
108 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
109 _slang_code_unit_dtr(&self
->builtin
[i
]);
110 _slang_code_unit_dtr(&self
->unit
);
111 slang_assembly_file_destruct(&self
->assembly
);
112 slang_machine_dtr(&self
->machine
);
113 slang_atom_pool_destruct(&self
->atompool
);
114 slang_export_data_table_dtr(&self
->expdata
);
115 slang_export_code_table_ctr(&self
->expcode
);
120 static char *out_of_memory
= "Error: Out of memory.\n";
123 slang_info_log_construct(slang_info_log
* log
)
126 log
->dont_free_text
= 0;
130 slang_info_log_destruct(slang_info_log
* log
)
132 if (!log
->dont_free_text
)
133 slang_alloc_free(log
->text
);
137 slang_info_log_message(slang_info_log
* log
, const char *prefix
,
142 if (log
->dont_free_text
)
144 size
= slang_string_length(msg
) + 2;
146 size
+= slang_string_length(prefix
) + 2;
147 if (log
->text
!= NULL
) {
148 GLuint old_len
= slang_string_length(log
->text
);
150 slang_alloc_realloc(log
->text
, old_len
+ 1, old_len
+ size
);
153 log
->text
= (char *) (slang_alloc_malloc(size
));
154 if (log
->text
!= NULL
)
157 if (log
->text
== NULL
)
159 if (prefix
!= NULL
) {
160 slang_string_concat(log
->text
, prefix
);
161 slang_string_concat(log
->text
, ": ");
163 slang_string_concat(log
->text
, msg
);
164 slang_string_concat(log
->text
, "\n");
169 slang_info_log_print(slang_info_log
* log
, const char *msg
, ...)
175 _mesa_vsprintf(buf
, msg
, va
);
177 return slang_info_log_message(log
, NULL
, buf
);
181 slang_info_log_error(slang_info_log
* log
, const char *msg
, ...)
187 _mesa_vsprintf(buf
, msg
, va
);
189 if (slang_info_log_message(log
, "Error", buf
))
191 slang_info_log_memory(log
);
196 slang_info_log_warning(slang_info_log
* log
, const char *msg
, ...)
202 _mesa_vsprintf(buf
, msg
, va
);
204 if (slang_info_log_message(log
, "Warning", buf
))
206 slang_info_log_memory(log
);
211 slang_info_log_memory(slang_info_log
* log
)
213 if (!slang_info_log_message(log
, "Error", "Out of memory.")) {
214 log
->dont_free_text
= 1;
215 log
->text
= out_of_memory
;
219 /* slang_parse_ctx */
221 typedef struct slang_parse_ctx_
226 GLboolean global_scope
; /**< Is object being declared a global? */
227 slang_atom_pool
*atoms
;
230 /* slang_output_ctx */
232 typedef struct slang_output_ctx_
234 slang_variable_scope
*vars
;
235 slang_function_scope
*funs
;
236 slang_struct_scope
*structs
;
237 slang_assembly_file
*assembly
;
238 slang_var_pool
*global_pool
;
239 slang_machine
*machine
;
242 /* _slang_compile() */
245 parse_identifier_str(slang_parse_ctx
* C
, char **id
)
248 C
->I
+= _mesa_strlen(*id
) + 1;
252 parse_identifier(slang_parse_ctx
* C
)
256 id
= (const char *) C
->I
;
257 C
->I
+= _mesa_strlen(id
) + 1;
258 return slang_atom_pool_atom(C
->atoms
, id
);
262 parse_number(slang_parse_ctx
* C
, int *number
)
264 const int radix
= (int) (*C
->I
++);
266 while (*C
->I
!= '\0') {
268 if (*C
->I
>= '0' && *C
->I
<= '9')
269 digit
= (int) (*C
->I
- '0');
270 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
271 digit
= (int) (*C
->I
- 'A') + 10;
273 digit
= (int) (*C
->I
- 'a') + 10;
274 *number
= *number
* radix
+ digit
;
279 slang_info_log_warning(C
->L
, "%d: literal integer overflow.", *number
);
284 parse_float(slang_parse_ctx
* C
, float *number
)
286 char *integral
= NULL
;
287 char *fractional
= NULL
;
288 char *exponent
= NULL
;
291 parse_identifier_str(C
, &integral
);
292 parse_identifier_str(C
, &fractional
);
293 parse_identifier_str(C
, &exponent
);
295 whole
= (char *) (slang_alloc_malloc((_mesa_strlen(integral
) +
296 _mesa_strlen(fractional
) +
297 _mesa_strlen(exponent
) + 3) * sizeof(char)));
299 slang_info_log_memory(C
->L
);
303 slang_string_copy(whole
, integral
);
304 slang_string_concat(whole
, ".");
305 slang_string_concat(whole
, fractional
);
306 slang_string_concat(whole
, "E");
307 slang_string_concat(whole
, exponent
);
309 *number
= (float) (_mesa_strtod(whole
, (char **) NULL
));
311 slang_alloc_free(whole
);
315 /* revision number - increment after each change affecting emitted output */
319 check_revision(slang_parse_ctx
* C
)
321 if (*C
->I
!= REVISION
) {
322 slang_info_log_error(C
->L
, "Internal compiler error.");
329 static int parse_statement(slang_parse_ctx
*, slang_output_ctx
*,
331 static int parse_expression(slang_parse_ctx
*, slang_output_ctx
*,
333 static int parse_type_specifier(slang_parse_ctx
*, slang_output_ctx
*,
334 slang_type_specifier
*);
337 parse_array_len(slang_parse_ctx
* C
, slang_output_ctx
* O
, GLuint
* len
)
339 slang_operation array_size
;
340 slang_assembly_name_space space
;
343 if (!slang_operation_construct(&array_size
))
345 if (!parse_expression(C
, O
, &array_size
)) {
346 slang_operation_destruct(&array_size
);
350 space
.funcs
= O
->funs
;
351 space
.structs
= O
->structs
;
352 space
.vars
= O
->vars
;
353 result
= _slang_evaluate_int(O
->assembly
, O
->machine
, &space
,
354 &array_size
, len
, C
->atoms
);
355 slang_operation_destruct(&array_size
);
360 calculate_var_size(slang_parse_ctx
* C
, slang_output_ctx
* O
,
361 slang_variable
* var
)
363 slang_storage_aggregate agg
;
365 if (!slang_storage_aggregate_construct(&agg
))
367 if (!_slang_aggregate_variable(&agg
, &var
->type
.specifier
, var
->array_len
,
368 O
->funs
, O
->structs
, O
->vars
, O
->machine
,
369 O
->assembly
, C
->atoms
)) {
370 slang_storage_aggregate_destruct(&agg
);
373 var
->size
= _slang_sizeof_aggregate(&agg
);
374 slang_storage_aggregate_destruct(&agg
);
379 convert_to_array(slang_parse_ctx
* C
, slang_variable
* var
,
380 const slang_type_specifier
* sp
)
382 /* sized array - mark it as array, copy the specifier to the array element and
383 * parse the expression */
384 var
->type
.specifier
.type
= slang_spec_array
;
385 var
->type
.specifier
._array
= (slang_type_specifier
*)
386 slang_alloc_malloc(sizeof(slang_type_specifier
));
387 if (var
->type
.specifier
._array
== NULL
) {
388 slang_info_log_memory(C
->L
);
391 slang_type_specifier_ctr(var
->type
.specifier
._array
);
392 return slang_type_specifier_copy(var
->type
.specifier
._array
, sp
);
395 /* structure field */
398 #define FIELD_ARRAY 2
401 parse_struct_field_var(slang_parse_ctx
* C
, slang_output_ctx
* O
,
402 slang_variable
* var
, const slang_type_specifier
* sp
)
404 var
->a_name
= parse_identifier(C
);
405 if (var
->a_name
== SLANG_ATOM_NULL
)
410 if (!slang_type_specifier_copy(&var
->type
.specifier
, sp
))
414 if (!convert_to_array(C
, var
, sp
))
416 if (!parse_array_len(C
, O
, &var
->array_len
))
423 return calculate_var_size(C
, O
, var
);
427 parse_struct_field(slang_parse_ctx
* C
, slang_output_ctx
* O
,
428 slang_struct
* st
, slang_type_specifier
* sp
)
430 slang_output_ctx o
= *O
;
432 o
.structs
= st
->structs
;
433 if (!parse_type_specifier(C
, &o
, sp
))
437 slang_variable
*var
= slang_variable_scope_grow(st
->fields
);
439 slang_info_log_memory(C
->L
);
442 if (!parse_struct_field_var(C
, &o
, var
, sp
))
445 while (*C
->I
++ != FIELD_NONE
);
451 parse_struct(slang_parse_ctx
* C
, slang_output_ctx
* O
, slang_struct
** st
)
456 /* parse struct name (if any) and make sure it is unique in current scope */
457 a_name
= parse_identifier(C
);
458 if (a_name
== SLANG_ATOM_NULL
)
461 name
= slang_atom_pool_id(C
->atoms
, a_name
);
463 && slang_struct_scope_find(O
->structs
, a_name
, 0) != NULL
) {
464 slang_info_log_error(C
->L
, "%s: duplicate type name.", name
);
468 /* set-up a new struct */
469 *st
= (slang_struct
*) slang_alloc_malloc(sizeof(slang_struct
));
471 slang_info_log_memory(C
->L
);
474 if (!slang_struct_construct(*st
)) {
475 slang_alloc_free(*st
);
477 slang_info_log_memory(C
->L
);
480 (**st
).a_name
= a_name
;
481 (**st
).structs
->outer_scope
= O
->structs
;
483 /* parse individual struct fields */
485 slang_type_specifier sp
;
487 slang_type_specifier_ctr(&sp
);
488 if (!parse_struct_field(C
, O
, *st
, &sp
)) {
489 slang_type_specifier_dtr(&sp
);
492 slang_type_specifier_dtr(&sp
);
494 while (*C
->I
++ != FIELD_NONE
);
496 /* if named struct, copy it to current scope */
497 if (name
[0] != '\0') {
500 O
->structs
->structs
=
501 (slang_struct
*) slang_alloc_realloc(O
->structs
->structs
,
502 O
->structs
->num_structs
*
503 sizeof(slang_struct
),
504 (O
->structs
->num_structs
+
505 1) * sizeof(slang_struct
));
506 if (O
->structs
->structs
== NULL
) {
507 slang_info_log_memory(C
->L
);
510 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
511 if (!slang_struct_construct(s
))
513 O
->structs
->num_structs
++;
514 if (!slang_struct_copy(s
, *st
))
523 #define TYPE_QUALIFIER_NONE 0
524 #define TYPE_QUALIFIER_CONST 1
525 #define TYPE_QUALIFIER_ATTRIBUTE 2
526 #define TYPE_QUALIFIER_VARYING 3
527 #define TYPE_QUALIFIER_UNIFORM 4
528 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
529 #define TYPE_QUALIFIER_FIXEDINPUT 6
532 parse_type_qualifier(slang_parse_ctx
* C
, slang_type_qualifier
* qual
)
535 case TYPE_QUALIFIER_NONE
:
536 *qual
= slang_qual_none
;
538 case TYPE_QUALIFIER_CONST
:
539 *qual
= slang_qual_const
;
541 case TYPE_QUALIFIER_ATTRIBUTE
:
542 *qual
= slang_qual_attribute
;
544 case TYPE_QUALIFIER_VARYING
:
545 *qual
= slang_qual_varying
;
547 case TYPE_QUALIFIER_UNIFORM
:
548 *qual
= slang_qual_uniform
;
550 case TYPE_QUALIFIER_FIXEDOUTPUT
:
551 *qual
= slang_qual_fixedoutput
;
553 case TYPE_QUALIFIER_FIXEDINPUT
:
554 *qual
= slang_qual_fixedinput
;
563 #define TYPE_SPECIFIER_VOID 0
564 #define TYPE_SPECIFIER_BOOL 1
565 #define TYPE_SPECIFIER_BVEC2 2
566 #define TYPE_SPECIFIER_BVEC3 3
567 #define TYPE_SPECIFIER_BVEC4 4
568 #define TYPE_SPECIFIER_INT 5
569 #define TYPE_SPECIFIER_IVEC2 6
570 #define TYPE_SPECIFIER_IVEC3 7
571 #define TYPE_SPECIFIER_IVEC4 8
572 #define TYPE_SPECIFIER_FLOAT 9
573 #define TYPE_SPECIFIER_VEC2 10
574 #define TYPE_SPECIFIER_VEC3 11
575 #define TYPE_SPECIFIER_VEC4 12
576 #define TYPE_SPECIFIER_MAT2 13
577 #define TYPE_SPECIFIER_MAT3 14
578 #define TYPE_SPECIFIER_MAT4 15
579 #define TYPE_SPECIFIER_SAMPLER1D 16
580 #define TYPE_SPECIFIER_SAMPLER2D 17
581 #define TYPE_SPECIFIER_SAMPLER3D 18
582 #define TYPE_SPECIFIER_SAMPLERCUBE 19
583 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
584 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
585 #define TYPE_SPECIFIER_STRUCT 22
586 #define TYPE_SPECIFIER_TYPENAME 23
589 parse_type_specifier(slang_parse_ctx
* C
, slang_output_ctx
* O
,
590 slang_type_specifier
* spec
)
593 case TYPE_SPECIFIER_VOID
:
594 spec
->type
= slang_spec_void
;
596 case TYPE_SPECIFIER_BOOL
:
597 spec
->type
= slang_spec_bool
;
599 case TYPE_SPECIFIER_BVEC2
:
600 spec
->type
= slang_spec_bvec2
;
602 case TYPE_SPECIFIER_BVEC3
:
603 spec
->type
= slang_spec_bvec3
;
605 case TYPE_SPECIFIER_BVEC4
:
606 spec
->type
= slang_spec_bvec4
;
608 case TYPE_SPECIFIER_INT
:
609 spec
->type
= slang_spec_int
;
611 case TYPE_SPECIFIER_IVEC2
:
612 spec
->type
= slang_spec_ivec2
;
614 case TYPE_SPECIFIER_IVEC3
:
615 spec
->type
= slang_spec_ivec3
;
617 case TYPE_SPECIFIER_IVEC4
:
618 spec
->type
= slang_spec_ivec4
;
620 case TYPE_SPECIFIER_FLOAT
:
621 spec
->type
= slang_spec_float
;
623 case TYPE_SPECIFIER_VEC2
:
624 spec
->type
= slang_spec_vec2
;
626 case TYPE_SPECIFIER_VEC3
:
627 spec
->type
= slang_spec_vec3
;
629 case TYPE_SPECIFIER_VEC4
:
630 spec
->type
= slang_spec_vec4
;
632 case TYPE_SPECIFIER_MAT2
:
633 spec
->type
= slang_spec_mat2
;
635 case TYPE_SPECIFIER_MAT3
:
636 spec
->type
= slang_spec_mat3
;
638 case TYPE_SPECIFIER_MAT4
:
639 spec
->type
= slang_spec_mat4
;
641 case TYPE_SPECIFIER_SAMPLER1D
:
642 spec
->type
= slang_spec_sampler1D
;
644 case TYPE_SPECIFIER_SAMPLER2D
:
645 spec
->type
= slang_spec_sampler2D
;
647 case TYPE_SPECIFIER_SAMPLER3D
:
648 spec
->type
= slang_spec_sampler3D
;
650 case TYPE_SPECIFIER_SAMPLERCUBE
:
651 spec
->type
= slang_spec_samplerCube
;
653 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
654 spec
->type
= slang_spec_sampler1DShadow
;
656 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
657 spec
->type
= slang_spec_sampler2DShadow
;
659 case TYPE_SPECIFIER_STRUCT
:
660 spec
->type
= slang_spec_struct
;
661 if (!parse_struct(C
, O
, &spec
->_struct
))
664 case TYPE_SPECIFIER_TYPENAME
:
665 spec
->type
= slang_spec_struct
;
670 a_name
= parse_identifier(C
);
674 stru
= slang_struct_scope_find(O
->structs
, a_name
, 1);
676 slang_info_log_error(C
->L
, "%s: undeclared type name.",
677 slang_atom_pool_id(C
->atoms
, a_name
));
682 (slang_struct
*) slang_alloc_malloc(sizeof(slang_struct
));
683 if (spec
->_struct
== NULL
) {
684 slang_info_log_memory(C
->L
);
687 if (!slang_struct_construct(spec
->_struct
)) {
688 slang_alloc_free(spec
->_struct
);
689 spec
->_struct
= NULL
;
692 if (!slang_struct_copy(spec
->_struct
, stru
))
703 parse_fully_specified_type(slang_parse_ctx
* C
, slang_output_ctx
* O
,
704 slang_fully_specified_type
* type
)
706 if (!parse_type_qualifier(C
, &type
->qualifier
))
708 return parse_type_specifier(C
, O
, &type
->specifier
);
713 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
714 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
718 #define OP_CONTINUE 6
721 #define OP_EXPRESSION 9
726 #define OP_PUSH_VOID 14
727 #define OP_PUSH_BOOL 15
728 #define OP_PUSH_INT 16
729 #define OP_PUSH_FLOAT 17
730 #define OP_PUSH_IDENTIFIER 18
731 #define OP_SEQUENCE 19
733 #define OP_ADDASSIGN 21
734 #define OP_SUBASSIGN 22
735 #define OP_MULASSIGN 23
736 #define OP_DIVASSIGN 24
737 /*#define OP_MODASSIGN 25*/
738 /*#define OP_LSHASSIGN 26*/
739 /*#define OP_RSHASSIGN 27*/
740 /*#define OP_ORASSIGN 28*/
741 /*#define OP_XORASSIGN 29*/
742 /*#define OP_ANDASSIGN 30*/
744 #define OP_LOGICALOR 32
745 #define OP_LOGICALXOR 33
746 #define OP_LOGICALAND 34
747 /*#define OP_BITOR 35*/
748 /*#define OP_BITXOR 36*/
749 /*#define OP_BITAND 37*/
751 #define OP_NOTEQUAL 39
753 #define OP_GREATER 41
754 #define OP_LESSEQUAL 42
755 #define OP_GREATEREQUAL 43
756 /*#define OP_LSHIFT 44*/
757 /*#define OP_RSHIFT 45*/
759 #define OP_SUBTRACT 47
760 #define OP_MULTIPLY 48
762 /*#define OP_MODULUS 50*/
763 #define OP_PREINCREMENT 51
764 #define OP_PREDECREMENT 52
767 /*#define OP_COMPLEMENT 55*/
769 #define OP_SUBSCRIPT 57
772 #define OP_POSTINCREMENT 60
773 #define OP_POSTDECREMENT 61
777 * When parsing a compound production, this function is used to parse the
779 * For example, a a while-loop compound will have two children, the
780 * while condition expression and the loop body. So, this function will
781 * be called twice to parse those two sub-expressions.
782 * \param C the parsing context
783 * \param O the output context
784 * \param oper the operation we're parsing
785 * \param statment which child of the operation is being parsed
786 * \return 1 if success, 0 if error
789 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
790 slang_operation
* oper
, unsigned int statement
)
794 /* grow child array */
795 oper
->children
= (slang_operation
*)
796 slang_alloc_realloc(oper
->children
,
797 oper
->num_children
* sizeof(slang_operation
),
798 (oper
->num_children
+ 1) * sizeof(slang_operation
));
799 if (oper
->children
== NULL
) {
800 slang_info_log_memory(C
->L
);
804 ch
= &oper
->children
[oper
->num_children
];
805 if (!slang_operation_construct(ch
)) {
806 slang_info_log_memory(C
->L
);
809 oper
->num_children
++;
810 /* XXX I guess the 0th "statement" is not really a statement? */
812 return parse_statement(C
, O
, ch
);
813 return parse_expression(C
, O
, ch
);
816 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
819 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
820 slang_operation
* oper
)
822 oper
->locals
->outer_scope
= O
->vars
;
824 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
825 /* parse child statements, do not create new variable scope */
826 oper
->type
= slang_oper_block_no_new_scope
;
827 while (*C
->I
!= OP_END
)
828 if (!parse_child_operation(C
, O
, oper
, 1))
832 case OP_BLOCK_BEGIN_NEW_SCOPE
:
833 /* parse child statements, create new variable scope */
835 slang_output_ctx o
= *O
;
837 oper
->type
= slang_oper_block_new_scope
;
838 o
.vars
= oper
->locals
;
839 while (*C
->I
!= OP_END
)
840 if (!parse_child_operation(C
, &o
, oper
, 1))
846 /* local variable declaration, individual declarators are stored as
847 * children identifiers
849 oper
->type
= slang_oper_variable_decl
;
851 const unsigned int first_var
= O
->vars
->num_variables
;
853 /* parse the declaration, note that there can be zero or more
854 * than one declarators
856 if (!parse_declaration(C
, O
))
858 if (first_var
< O
->vars
->num_variables
) {
859 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
862 oper
->children
= (slang_operation
*)
863 slang_alloc_malloc(num_vars
* sizeof(slang_operation
));
864 if (oper
->children
== NULL
) {
865 slang_info_log_memory(C
->L
);
868 for (oper
->num_children
= 0; oper
->num_children
< num_vars
;
869 oper
->num_children
++) {
870 if (!slang_operation_construct
871 (&oper
->children
[oper
->num_children
])) {
872 slang_info_log_memory(C
->L
);
876 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
877 slang_operation
*o
= &oper
->children
[i
- first_var
];
878 o
->type
= slang_oper_identifier
;
879 o
->locals
->outer_scope
= O
->vars
;
880 o
->a_id
= O
->vars
->variables
[i
].a_name
;
886 /* the __asm statement, parse the mnemonic and all its arguments
889 oper
->type
= slang_oper_asm
;
890 oper
->a_id
= parse_identifier(C
);
891 if (oper
->a_id
== SLANG_ATOM_NULL
)
893 while (*C
->I
!= OP_END
) {
894 if (!parse_child_operation(C
, O
, oper
, 0))
900 oper
->type
= slang_oper_break
;
903 oper
->type
= slang_oper_continue
;
906 oper
->type
= slang_oper_discard
;
909 oper
->type
= slang_oper_return
;
910 if (!parse_child_operation(C
, O
, oper
, 0))
914 oper
->type
= slang_oper_expression
;
915 if (!parse_child_operation(C
, O
, oper
, 0))
919 oper
->type
= slang_oper_if
;
920 if (!parse_child_operation(C
, O
, oper
, 0))
922 if (!parse_child_operation(C
, O
, oper
, 1))
924 if (!parse_child_operation(C
, O
, oper
, 1))
929 slang_output_ctx o
= *O
;
931 oper
->type
= slang_oper_while
;
932 o
.vars
= oper
->locals
;
933 if (!parse_child_operation(C
, &o
, oper
, 1))
935 if (!parse_child_operation(C
, &o
, oper
, 1))
940 oper
->type
= slang_oper_do
;
941 if (!parse_child_operation(C
, O
, oper
, 1))
943 if (!parse_child_operation(C
, O
, oper
, 0))
948 slang_output_ctx o
= *O
;
950 oper
->type
= slang_oper_for
;
951 o
.vars
= oper
->locals
;
952 if (!parse_child_operation(C
, &o
, oper
, 1))
954 if (!parse_child_operation(C
, &o
, oper
, 1))
956 if (!parse_child_operation(C
, &o
, oper
, 0))
958 if (!parse_child_operation(C
, &o
, oper
, 1))
969 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
970 slang_operation
** ops
, unsigned int *total_ops
,
976 (slang_operation
*) slang_alloc_malloc(n
* sizeof(slang_operation
));
977 if (op
->children
== NULL
) {
978 slang_info_log_memory(C
->L
);
981 op
->num_children
= n
;
983 for (i
= 0; i
< n
; i
++)
984 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
985 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
988 *ops
= (slang_operation
*)
989 slang_alloc_realloc(*ops
,
990 (*total_ops
+ n
) * sizeof(slang_operation
),
991 *total_ops
* sizeof(slang_operation
));
993 slang_info_log_memory(C
->L
);
1000 is_constructor_name(const char *name
, slang_atom a_name
,
1001 slang_struct_scope
* structs
)
1003 if (slang_type_specifier_type_from_string(name
) != slang_spec_void
)
1005 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
1009 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1010 slang_operation
* oper
)
1012 slang_operation
*ops
= NULL
;
1013 unsigned int num_ops
= 0;
1016 while (*C
->I
!= OP_END
) {
1017 slang_operation
*op
;
1018 const unsigned int op_code
= *C
->I
++;
1020 /* allocate default operation, becomes a no-op if not used */
1021 ops
= (slang_operation
*)
1022 slang_alloc_realloc(ops
,
1023 num_ops
* sizeof(slang_operation
),
1024 (num_ops
+ 1) * sizeof(slang_operation
));
1026 slang_info_log_memory(C
->L
);
1030 if (!slang_operation_construct(op
)) {
1031 slang_info_log_memory(C
->L
);
1035 op
->locals
->outer_scope
= O
->vars
;
1039 op
->type
= slang_oper_void
;
1042 op
->type
= slang_oper_literal_bool
;
1043 if (!parse_number(C
, &number
))
1045 op
->literal
= (GLfloat
) number
;
1048 op
->type
= slang_oper_literal_int
;
1049 if (!parse_number(C
, &number
))
1051 op
->literal
= (GLfloat
) number
;
1054 op
->type
= slang_oper_literal_float
;
1055 if (!parse_float(C
, &op
->literal
))
1058 case OP_PUSH_IDENTIFIER
:
1059 op
->type
= slang_oper_identifier
;
1060 op
->a_id
= parse_identifier(C
);
1061 if (op
->a_id
== SLANG_ATOM_NULL
)
1065 op
->type
= slang_oper_sequence
;
1066 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1070 op
->type
= slang_oper_assign
;
1071 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1075 op
->type
= slang_oper_addassign
;
1076 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1080 op
->type
= slang_oper_subassign
;
1081 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1085 op
->type
= slang_oper_mulassign
;
1086 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1090 op
->type
= slang_oper_divassign
;
1091 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1094 /*case OP_MODASSIGN: */
1095 /*case OP_LSHASSIGN: */
1096 /*case OP_RSHASSIGN: */
1097 /*case OP_ORASSIGN: */
1098 /*case OP_XORASSIGN: */
1099 /*case OP_ANDASSIGN: */
1101 op
->type
= slang_oper_select
;
1102 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1106 op
->type
= slang_oper_logicalor
;
1107 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1111 op
->type
= slang_oper_logicalxor
;
1112 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1116 op
->type
= slang_oper_logicaland
;
1117 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1121 /*case OP_BITXOR: */
1122 /*case OP_BITAND: */
1124 op
->type
= slang_oper_equal
;
1125 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1129 op
->type
= slang_oper_notequal
;
1130 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1134 op
->type
= slang_oper_less
;
1135 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1139 op
->type
= slang_oper_greater
;
1140 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1144 op
->type
= slang_oper_lessequal
;
1145 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1148 case OP_GREATEREQUAL
:
1149 op
->type
= slang_oper_greaterequal
;
1150 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1153 /*case OP_LSHIFT: */
1154 /*case OP_RSHIFT: */
1156 op
->type
= slang_oper_add
;
1157 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1161 op
->type
= slang_oper_subtract
;
1162 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1166 op
->type
= slang_oper_multiply
;
1167 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1171 op
->type
= slang_oper_divide
;
1172 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1175 /*case OP_MODULUS: */
1176 case OP_PREINCREMENT
:
1177 op
->type
= slang_oper_preincrement
;
1178 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1181 case OP_PREDECREMENT
:
1182 op
->type
= slang_oper_predecrement
;
1183 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1187 op
->type
= slang_oper_plus
;
1188 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1192 op
->type
= slang_oper_minus
;
1193 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1197 op
->type
= slang_oper_not
;
1198 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1201 /*case OP_COMPLEMENT: */
1203 op
->type
= slang_oper_subscript
;
1204 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1208 op
->type
= slang_oper_call
;
1209 op
->a_id
= parse_identifier(C
);
1210 if (op
->a_id
== SLANG_ATOM_NULL
)
1212 while (*C
->I
!= OP_END
)
1213 if (!parse_child_operation(C
, O
, op
, 0))
1217 if (!C
->parsing_builtin
1218 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1221 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1222 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1223 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1229 op
->type
= slang_oper_field
;
1230 op
->a_id
= parse_identifier(C
);
1231 if (op
->a_id
== SLANG_ATOM_NULL
)
1233 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1236 case OP_POSTINCREMENT
:
1237 op
->type
= slang_oper_postincrement
;
1238 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1241 case OP_POSTDECREMENT
:
1242 op
->type
= slang_oper_postdecrement
;
1243 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1253 slang_alloc_free(ops
);
1258 /* parameter qualifier */
1259 #define PARAM_QUALIFIER_IN 0
1260 #define PARAM_QUALIFIER_OUT 1
1261 #define PARAM_QUALIFIER_INOUT 2
1263 /* function parameter array presence */
1264 #define PARAMETER_ARRAY_NOT_PRESENT 0
1265 #define PARAMETER_ARRAY_PRESENT 1
1268 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1269 slang_variable
* param
)
1271 /* parse and validate the parameter's type qualifiers (there can be
1272 * two at most) because not all combinations are valid
1274 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1277 case PARAM_QUALIFIER_IN
:
1278 if (param
->type
.qualifier
!= slang_qual_const
1279 && param
->type
.qualifier
!= slang_qual_none
) {
1280 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1284 case PARAM_QUALIFIER_OUT
:
1285 if (param
->type
.qualifier
== slang_qual_none
)
1286 param
->type
.qualifier
= slang_qual_out
;
1288 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1292 case PARAM_QUALIFIER_INOUT
:
1293 if (param
->type
.qualifier
== slang_qual_none
)
1294 param
->type
.qualifier
= slang_qual_inout
;
1296 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1304 /* parse parameter's type specifier and name */
1305 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1307 param
->a_name
= parse_identifier(C
);
1308 if (param
->a_name
== SLANG_ATOM_NULL
)
1311 /* if the parameter is an array, parse its size (the size must be
1312 * explicitly defined
1314 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1315 slang_type_specifier p
;
1317 slang_type_specifier_ctr(&p
);
1318 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1319 slang_type_specifier_dtr(&p
);
1322 if (!convert_to_array(C
, param
, &p
)) {
1323 slang_type_specifier_dtr(&p
);
1326 slang_type_specifier_dtr(&p
);
1327 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1331 /* calculate the parameter size */
1332 if (!calculate_var_size(C
, O
, param
))
1335 /* TODO: allocate the local address here? */
1340 #define FUNCTION_ORDINARY 0
1341 #define FUNCTION_CONSTRUCTOR 1
1342 #define FUNCTION_OPERATOR 2
1344 /* function parameter */
1345 #define PARAMETER_NONE 0
1346 #define PARAMETER_NEXT 1
1349 #define OPERATOR_ADDASSIGN 1
1350 #define OPERATOR_SUBASSIGN 2
1351 #define OPERATOR_MULASSIGN 3
1352 #define OPERATOR_DIVASSIGN 4
1353 /*#define OPERATOR_MODASSIGN 5*/
1354 /*#define OPERATOR_LSHASSIGN 6*/
1355 /*#define OPERATOR_RSHASSIGN 7*/
1356 /*#define OPERATOR_ANDASSIGN 8*/
1357 /*#define OPERATOR_XORASSIGN 9*/
1358 /*#define OPERATOR_ORASSIGN 10*/
1359 #define OPERATOR_LOGICALXOR 11
1360 /*#define OPERATOR_BITOR 12*/
1361 /*#define OPERATOR_BITXOR 13*/
1362 /*#define OPERATOR_BITAND 14*/
1363 #define OPERATOR_LESS 15
1364 #define OPERATOR_GREATER 16
1365 #define OPERATOR_LESSEQUAL 17
1366 #define OPERATOR_GREATEREQUAL 18
1367 /*#define OPERATOR_LSHIFT 19*/
1368 /*#define OPERATOR_RSHIFT 20*/
1369 #define OPERATOR_MULTIPLY 21
1370 #define OPERATOR_DIVIDE 22
1371 /*#define OPERATOR_MODULUS 23*/
1372 #define OPERATOR_INCREMENT 24
1373 #define OPERATOR_DECREMENT 25
1374 #define OPERATOR_PLUS 26
1375 #define OPERATOR_MINUS 27
1376 /*#define OPERATOR_COMPLEMENT 28*/
1377 #define OPERATOR_NOT 29
1381 unsigned int o_code
;
1383 } operator_names
[] = {
1384 {OPERATOR_INCREMENT
, "++"},
1385 {OPERATOR_ADDASSIGN
, "+="},
1386 {OPERATOR_PLUS
, "+"},
1387 {OPERATOR_DECREMENT
, "--"},
1388 {OPERATOR_SUBASSIGN
, "-="},
1389 {OPERATOR_MINUS
, "-"},
1390 {OPERATOR_NOT
, "!"},
1391 {OPERATOR_MULASSIGN
, "*="},
1392 {OPERATOR_MULTIPLY
, "*"},
1393 {OPERATOR_DIVASSIGN
, "/="},
1394 {OPERATOR_DIVIDE
, "/"},
1395 {OPERATOR_LESSEQUAL
, "<="},
1396 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1397 /*{ OPERATOR_LSHIFT, "<<" }, */
1398 {OPERATOR_LESS
, "<"},
1399 {OPERATOR_GREATEREQUAL
, ">="},
1400 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1401 /*{ OPERATOR_RSHIFT, ">>" }, */
1402 {OPERATOR_GREATER
, ">"},
1403 /*{ OPERATOR_MODASSIGN, "%=" }, */
1404 /*{ OPERATOR_MODULUS, "%" }, */
1405 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1406 /*{ OPERATOR_BITAND, "&" }, */
1407 /*{ OPERATOR_ORASSIGN, "|=" }, */
1408 /*{ OPERATOR_BITOR, "|" }, */
1409 /*{ OPERATOR_COMPLEMENT, "~" }, */
1410 /*{ OPERATOR_XORASSIGN, "^=" }, */
1411 {OPERATOR_LOGICALXOR
, "^^"},
1412 /*{ OPERATOR_BITXOR, "^" } */
1416 parse_operator_name(slang_parse_ctx
* C
)
1420 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1421 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1423 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1424 if (atom
== SLANG_ATOM_NULL
) {
1425 slang_info_log_memory(C
->L
);
1436 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1437 slang_function
* func
)
1439 /* parse function type and name */
1440 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1443 case FUNCTION_ORDINARY
:
1444 func
->kind
= slang_func_ordinary
;
1445 func
->header
.a_name
= parse_identifier(C
);
1446 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1449 case FUNCTION_CONSTRUCTOR
:
1450 func
->kind
= slang_func_constructor
;
1451 if (func
->header
.type
.specifier
.type
== slang_spec_struct
)
1453 func
->header
.a_name
=
1454 slang_atom_pool_atom(C
->atoms
,
1455 slang_type_specifier_type_to_string
1456 (func
->header
.type
.specifier
.type
));
1457 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1458 slang_info_log_memory(C
->L
);
1462 case FUNCTION_OPERATOR
:
1463 func
->kind
= slang_func_operator
;
1464 func
->header
.a_name
= parse_operator_name(C
);
1465 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1472 /* parse function parameters */
1473 while (*C
->I
++ == PARAMETER_NEXT
) {
1474 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1476 slang_info_log_memory(C
->L
);
1479 if (!parse_parameter_declaration(C
, O
, p
))
1483 /* function formal parameters and local variables share the same
1484 * scope, so save the information about param count in a seperate
1485 * place also link the scope to the global variable scope so when a
1486 * given identifier is not found here, the search process continues
1487 * in the global space
1489 func
->param_count
= func
->parameters
->num_variables
;
1490 func
->parameters
->outer_scope
= O
->vars
;
1495 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1496 slang_function
* func
)
1498 slang_output_ctx o
= *O
;
1500 if (!parse_function_prototype(C
, O
, func
))
1503 /* create function's body operation */
1505 (slang_operation
*) slang_alloc_malloc(sizeof(slang_operation
));
1506 if (func
->body
== NULL
) {
1507 slang_info_log_memory(C
->L
);
1510 if (!slang_operation_construct(func
->body
)) {
1511 slang_alloc_free(func
->body
);
1513 slang_info_log_memory(C
->L
);
1517 /* to parse the body the parse context is modified in order to
1518 * capture parsed variables into function's local variable scope
1520 C
->global_scope
= GL_FALSE
;
1521 o
.vars
= func
->parameters
;
1522 if (!parse_statement(C
, &o
, func
->body
))
1525 C
->global_scope
= GL_TRUE
;
1530 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1532 slang_assembly_file_restore_point point
;
1534 slang_assembly_local_info save_local
= A
->local
;
1535 slang_operation op_id
, op_assign
;
1538 /* save the current assembly */
1539 if (!slang_assembly_file_restore_point_save(A
->file
, &point
))
1542 /* setup the machine */
1544 mach
.ip
= A
->file
->count
;
1546 /* allocate local storage for expression */
1547 A
->local
.ret_size
= 0;
1548 A
->local
.addr_tmp
= 0;
1549 A
->local
.swizzle_tmp
= 4;
1550 if (!slang_assembly_file_push_label(A
->file
, slang_asm_local_alloc
, 20))
1552 if (!slang_assembly_file_push_label(A
->file
, slang_asm_enter
, 20))
1555 /* construct the left side of assignment */
1556 if (!slang_operation_construct(&op_id
))
1558 op_id
.type
= slang_oper_identifier
;
1559 op_id
.a_id
= var
->a_name
;
1561 /* put the variable into operation's scope */
1562 op_id
.locals
->variables
=
1563 (slang_variable
*) slang_alloc_malloc(sizeof(slang_variable
));
1564 if (op_id
.locals
->variables
== NULL
) {
1565 slang_operation_destruct(&op_id
);
1568 op_id
.locals
->num_variables
= 1;
1569 op_id
.locals
->variables
[0] = *var
;
1571 /* construct the assignment expression */
1572 if (!slang_operation_construct(&op_assign
)) {
1573 op_id
.locals
->num_variables
= 0;
1574 slang_operation_destruct(&op_id
);
1577 op_assign
.type
= slang_oper_assign
;
1578 op_assign
.children
=
1579 (slang_operation
*) slang_alloc_malloc(2 * sizeof(slang_operation
));
1580 if (op_assign
.children
== NULL
) {
1581 slang_operation_destruct(&op_assign
);
1582 op_id
.locals
->num_variables
= 0;
1583 slang_operation_destruct(&op_id
);
1586 op_assign
.num_children
= 2;
1587 op_assign
.children
[0] = op_id
;
1588 op_assign
.children
[1] = *var
->initializer
;
1590 /* insert the actual expression */
1591 result
= _slang_assemble_operation(A
, &op_assign
, slang_ref_forbid
);
1593 /* carefully destroy the operations */
1594 op_assign
.num_children
= 0;
1595 slang_alloc_free(op_assign
.children
);
1596 op_assign
.children
= NULL
;
1597 slang_operation_destruct(&op_assign
);
1598 op_id
.locals
->num_variables
= 0;
1599 slang_operation_destruct(&op_id
);
1603 if (!slang_assembly_file_push(A
->file
, slang_asm_exit
))
1606 /* execute the expression */
1607 if (!_slang_execute2(A
->file
, &mach
))
1610 /* restore the old assembly */
1611 if (!slang_assembly_file_restore_point_load(A
->file
, &point
))
1613 A
->local
= save_local
;
1615 /* now we copy the contents of the initialized variable back to the original machine */
1616 _mesa_memcpy((GLubyte
*) A
->mach
->mem
+ var
->address
,
1617 (GLubyte
*) mach
.mem
+ var
->address
, var
->size
);
1622 /* init declarator list */
1623 #define DECLARATOR_NONE 0
1624 #define DECLARATOR_NEXT 1
1626 /* variable declaration */
1627 #define VARIABLE_NONE 0
1628 #define VARIABLE_IDENTIFIER 1
1629 #define VARIABLE_INITIALIZER 2
1630 #define VARIABLE_ARRAY_EXPLICIT 3
1631 #define VARIABLE_ARRAY_UNKNOWN 4
1635 * Parse the initializer for a variable declaration.
1638 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1639 const slang_fully_specified_type
* type
)
1641 slang_variable
*var
;
1643 /* empty init declatator (without name, e.g. "float ;") */
1644 if (*C
->I
++ == VARIABLE_NONE
)
1647 /* make room for the new variable and initialize it */
1648 var
= slang_variable_scope_grow(O
->vars
);
1650 slang_info_log_memory(C
->L
);
1654 /* copy the declarator qualifier type, parse the identifier */
1655 var
->global
= C
->global_scope
;
1656 var
->type
.qualifier
= type
->qualifier
;
1657 var
->a_name
= parse_identifier(C
);
1658 if (var
->a_name
== SLANG_ATOM_NULL
)
1663 /* simple variable declarator - just copy the specifier */
1664 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1667 case VARIABLE_INITIALIZER
:
1668 /* initialized variable - copy the specifier and parse the expression */
1669 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1672 (slang_operation
*) slang_alloc_malloc(sizeof(slang_operation
));
1673 if (var
->initializer
== NULL
) {
1674 slang_info_log_memory(C
->L
);
1677 if (!slang_operation_construct(var
->initializer
)) {
1678 slang_alloc_free(var
->initializer
);
1679 var
->initializer
= NULL
;
1680 slang_info_log_memory(C
->L
);
1683 if (!parse_expression(C
, O
, var
->initializer
))
1687 case VARIABLE_ARRAY_UNKNOWN
:
1688 /* unsized array - mark it as array and copy the specifier to
1691 if (!convert_to_array(C
, var
, &type
->specifier
))
1695 case VARIABLE_ARRAY_EXPLICIT
:
1696 if (!convert_to_array(C
, var
, &type
->specifier
))
1698 if (!parse_array_len(C
, O
, &var
->array_len
))
1705 /* allocate global address space for a variable with a known size */
1707 && !(var
->type
.specifier
.type
== slang_spec_array
1708 && var
->array_len
== 0)) {
1709 if (!calculate_var_size(C
, O
, var
))
1711 var
->address
= slang_var_pool_alloc(O
->global_pool
, var
->size
);
1714 /* initialize global variable */
1715 if (C
->global_scope
) {
1716 if (var
->initializer
!= NULL
) {
1717 slang_assemble_ctx A
;
1719 A
.file
= O
->assembly
;
1720 A
.mach
= O
->machine
;
1722 A
.space
.funcs
= O
->funs
;
1723 A
.space
.structs
= O
->structs
;
1724 A
.space
.vars
= O
->vars
;
1725 if (!initialize_global(&A
, var
))
1729 _mesa_memset((GLubyte
*) (O
->machine
->mem
) + var
->address
, 0,
1737 * Parse a list of variable declarations. Each variable may have an
1741 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1743 slang_fully_specified_type type
;
1745 /* parse the fully specified type, common to all declarators */
1746 if (!slang_fully_specified_type_construct(&type
))
1748 if (!parse_fully_specified_type(C
, O
, &type
)) {
1749 slang_fully_specified_type_destruct(&type
);
1753 /* parse declarators, pass-in the parsed type */
1755 if (!parse_init_declarator(C
, O
, &type
)) {
1756 slang_fully_specified_type_destruct(&type
);
1760 while (*C
->I
++ == DECLARATOR_NEXT
);
1762 slang_fully_specified_type_destruct(&type
);
1768 * Parse a function definition or declaration.
1769 * \param C parsing context
1770 * \param O output context
1771 * \param definition if non-zero expect a definition, else a declaration
1772 * \param parsed_func_ret returns the parsed function
1773 * \return 1 if success, 0 if failure
1776 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
1777 slang_function
** parsed_func_ret
)
1779 slang_function parsed_func
, *found_func
;
1781 /* parse function definition/declaration */
1782 if (!slang_function_construct(&parsed_func
))
1785 if (!parse_function_definition(C
, O
, &parsed_func
)) {
1786 slang_function_destruct(&parsed_func
);
1791 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
1792 slang_function_destruct(&parsed_func
);
1797 /* find a function with a prototype matching the parsed one - only
1798 * the current scope is being searched to allow built-in function
1801 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
1802 if (found_func
== NULL
) {
1803 /* add the parsed function to the function list */
1804 O
->funs
->functions
=
1805 (slang_function
*) slang_alloc_realloc(O
->funs
->functions
,
1806 O
->funs
->num_functions
*
1807 sizeof(slang_function
),
1808 (O
->funs
->num_functions
+
1809 1) * sizeof(slang_function
));
1810 if (O
->funs
->functions
== NULL
) {
1811 slang_info_log_memory(C
->L
);
1812 slang_function_destruct(&parsed_func
);
1815 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
1816 O
->funs
->num_functions
++;
1818 /* return the newly parsed function */
1819 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
1822 /* TODO: check function return type qualifiers and specifiers */
1824 if (found_func
->body
!= NULL
) {
1825 slang_info_log_error(C
->L
, "%s: function already has a body.",
1826 slang_atom_pool_id(C
->atoms
,
1829 slang_function_destruct(&parsed_func
);
1833 /* destroy the existing function declaration and replace it
1834 * with the new one, remember to save the fixup table
1836 parsed_func
.fixups
= found_func
->fixups
;
1837 slang_fixup_table_init(&found_func
->fixups
);
1838 slang_function_destruct(found_func
);
1839 *found_func
= parsed_func
;
1842 /* another declaration of the same function prototype - ignore it */
1843 slang_function_destruct(&parsed_func
);
1846 /* return the found function */
1847 *parsed_func_ret
= found_func
;
1850 /* assemble the parsed function */
1852 slang_assemble_ctx A
;
1854 A
.file
= O
->assembly
;
1855 A
.mach
= O
->machine
;
1857 A
.space
.funcs
= O
->funs
;
1858 A
.space
.structs
= O
->structs
;
1859 A
.space
.vars
= O
->vars
;
1860 if (!_slang_assemble_function(&A
, *parsed_func_ret
))
1867 #define DECLARATION_FUNCTION_PROTOTYPE 1
1868 #define DECLARATION_INIT_DECLARATOR_LIST 2
1871 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1874 case DECLARATION_INIT_DECLARATOR_LIST
:
1875 if (!parse_init_declarator_list(C
, O
))
1878 case DECLARATION_FUNCTION_PROTOTYPE
:
1880 slang_function
*dummy_func
;
1882 if (!parse_function(C
, O
, 0, &dummy_func
))
1892 /* external declaration */
1893 #define EXTERNAL_NULL 0
1894 #define EXTERNAL_FUNCTION_DEFINITION 1
1895 #define EXTERNAL_DECLARATION 2
1898 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
)
1902 /* setup output context */
1903 o
.funs
= &unit
->funs
;
1904 o
.structs
= &unit
->structs
;
1905 o
.vars
= &unit
->vars
;
1906 o
.assembly
= &unit
->object
->assembly
;
1907 o
.global_pool
= &unit
->object
->varpool
;
1908 o
.machine
= &unit
->object
->machine
;
1910 /* parse individual functions and declarations */
1911 while (*C
->I
!= EXTERNAL_NULL
) {
1913 case EXTERNAL_FUNCTION_DEFINITION
:
1915 slang_function
*func
;
1917 if (!parse_function(C
, &o
, 1, &func
))
1921 case EXTERNAL_DECLARATION
:
1922 if (!parse_declaration(C
, &o
))
1934 compile_binary(const byte
* prod
, slang_code_unit
* unit
,
1935 slang_unit_type type
, slang_info_log
* infolog
,
1936 slang_code_unit
* builtin
, slang_code_unit
* downlink
)
1942 /* setup parse context */
1945 C
.parsing_builtin
= (builtin
== NULL
);
1946 C
.global_scope
= GL_TRUE
;
1947 C
.atoms
= &unit
->object
->atompool
;
1949 if (!check_revision(&C
))
1952 if (downlink
!= NULL
) {
1953 unit
->vars
.outer_scope
= &downlink
->vars
;
1954 unit
->funs
.outer_scope
= &downlink
->funs
;
1955 unit
->structs
.outer_scope
= &downlink
->structs
;
1958 /* parse translation unit */
1959 return parse_code_unit(&C
, unit
);
1963 compile_with_grammar(grammar id
, const char *source
, slang_code_unit
* unit
,
1964 slang_unit_type type
, slang_info_log
* infolog
,
1965 slang_code_unit
* builtin
)
1968 GLuint size
, start
, version
;
1969 slang_string preprocessed
;
1971 /* First retrieve the version number. */
1972 if (!_slang_preprocess_version(source
, &version
, &start
, infolog
))
1975 if (version
> 110) {
1976 slang_info_log_error(infolog
,
1977 "language version specified is not supported.");
1981 /* Now preprocess the source string. */
1982 slang_string_init(&preprocessed
);
1983 if (!_slang_preprocess_directives(&preprocessed
, &source
[start
], infolog
)) {
1984 slang_string_free(&preprocessed
);
1985 slang_info_log_error(infolog
, "failed to preprocess the source.");
1989 /* Finally check the syntax and generate its binary representation. */
1990 if (!grammar_fast_check
1991 (id
, (const byte
*) (slang_string_cstr(&preprocessed
)), &prod
, &size
,
1996 slang_string_free(&preprocessed
);
1997 grammar_get_last_error((byte
*) (buf
), sizeof(buf
), &pos
);
1998 slang_info_log_error(infolog
, buf
);
2001 slang_string_free(&preprocessed
);
2003 /* Syntax is okay - translate it to internal representation. */
2005 (prod
, unit
, type
, infolog
, builtin
,
2006 &builtin
[SLANG_BUILTIN_TOTAL
- 1])) {
2007 grammar_alloc_free(prod
);
2010 grammar_alloc_free(prod
);
2014 LONGSTRING
static const char *slang_shader_syn
=
2015 #include "library/slang_shader_syn.h"
2018 static const byte slang_core_gc
[] = {
2019 #include "library/slang_core_gc.h"
2022 static const byte slang_common_builtin_gc
[] = {
2023 #include "library/slang_common_builtin_gc.h"
2026 static const byte slang_fragment_builtin_gc
[] = {
2027 #include "library/slang_fragment_builtin_gc.h"
2030 static const byte slang_vertex_builtin_gc
[] = {
2031 #include "library/slang_vertex_builtin_gc.h"
2034 #if defined(USE_X86_ASM) || defined(SLANG_X86)
2035 static const byte slang_builtin_vec4_gc
[] = {
2036 #include "library/slang_builtin_vec4_gc.h"
2041 compile_object(grammar
* id
, const char *source
, slang_code_object
* object
,
2042 slang_unit_type type
, slang_info_log
* infolog
)
2044 slang_code_unit
*builtins
= NULL
;
2046 /* load GLSL grammar */
2047 *id
= grammar_load_from_text((const byte
*) (slang_shader_syn
));
2052 grammar_get_last_error(buf
, 1024, &pos
);
2053 slang_info_log_error(infolog
, (const char *) (buf
));
2057 /* set shader type - the syntax is slightly different for different shaders */
2058 if (type
== slang_unit_fragment_shader
2059 || type
== slang_unit_fragment_builtin
)
2060 grammar_set_reg8(*id
, (const byte
*) "shader_type", 1);
2062 grammar_set_reg8(*id
, (const byte
*) "shader_type", 2);
2064 /* enable language extensions */
2065 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2067 /* if parsing user-specified shader, load built-in library */
2068 if (type
== slang_unit_fragment_shader
|| type
== slang_unit_vertex_shader
) {
2069 /* compile core functionality first */
2070 if (!compile_binary(slang_core_gc
, &object
->builtin
[SLANG_BUILTIN_CORE
],
2071 slang_unit_fragment_builtin
, infolog
, NULL
, NULL
))
2074 /* compile common functions and variables, link to core */
2076 (slang_common_builtin_gc
, &object
->builtin
[SLANG_BUILTIN_COMMON
],
2077 slang_unit_fragment_builtin
, infolog
, NULL
,
2078 &object
->builtin
[SLANG_BUILTIN_CORE
]))
2081 /* compile target-specific functions and variables, link to common */
2082 if (type
== slang_unit_fragment_shader
) {
2084 (slang_fragment_builtin_gc
,
2085 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2086 slang_unit_fragment_builtin
, infolog
, NULL
,
2087 &object
->builtin
[SLANG_BUILTIN_COMMON
]))
2090 else if (type
== slang_unit_vertex_shader
) {
2092 (slang_vertex_builtin_gc
, &object
->builtin
[SLANG_BUILTIN_TARGET
],
2093 slang_unit_vertex_builtin
, infolog
, NULL
,
2094 &object
->builtin
[SLANG_BUILTIN_COMMON
]))
2098 #if defined(USE_X86_ASM) || defined(SLANG_X86)
2099 /* compile x86 4-component vector overrides, link to target */
2101 (slang_builtin_vec4_gc
, &object
->builtin
[SLANG_BUILTIN_VEC4
],
2102 slang_unit_fragment_builtin
, infolog
, NULL
,
2103 &object
->builtin
[SLANG_BUILTIN_TARGET
]))
2107 /* disable language extensions */
2108 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 0);
2109 builtins
= object
->builtin
;
2112 /* compile the actual shader - pass-in built-in library for external shader */
2113 return compile_with_grammar(*id
, source
, &object
->unit
, type
, infolog
,
2118 _slang_compile(const char *source
, slang_code_object
* object
,
2119 slang_unit_type type
, slang_info_log
* infolog
)
2124 _slang_code_object_dtr(object
);
2125 _slang_code_object_ctr(object
);
2127 success
= compile_object(&id
, source
, object
, type
, infolog
);
2129 grammar_destroy(id
);
2133 if (!_slang_build_export_data_table(&object
->expdata
, &object
->unit
.vars
))
2135 if (!_slang_build_export_code_table
2136 (&object
->expcode
, &object
->unit
.funs
, &object
->unit
))
2139 #if defined(USE_X86_ASM) || defined(SLANG_X86)
2140 /* XXX: lookup the @main label */
2141 if (!_slang_x86_codegen
2142 (&object
->machine
, &object
->assembly
,
2143 object
->expcode
.entries
[0].address
))