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 slang_var_pool_alloc(slang_var_pool
* pool
, unsigned int size
)
52 addr
= pool
->next_addr
;
53 pool
->next_addr
+= size
;
62 _slang_code_unit_ctr(slang_code_unit
* self
,
63 struct slang_code_object_
* object
)
65 _slang_variable_scope_ctr(&self
->vars
);
66 _slang_function_scope_ctr(&self
->funs
);
67 _slang_struct_scope_ctr(&self
->structs
);
68 self
->object
= object
;
72 _slang_code_unit_dtr(slang_code_unit
* self
)
74 slang_variable_scope_destruct(&self
->vars
);
75 slang_function_scope_destruct(&self
->funs
);
76 slang_struct_scope_destruct(&self
->structs
);
84 _slang_code_object_ctr(slang_code_object
* self
)
88 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
89 _slang_code_unit_ctr(&self
->builtin
[i
], self
);
90 _slang_code_unit_ctr(&self
->unit
, self
);
91 _slang_assembly_file_ctr(&self
->assembly
);
92 slang_machine_ctr(&self
->machine
);
93 self
->varpool
.next_addr
= 0;
94 slang_atom_pool_construct(&self
->atompool
);
95 slang_export_data_table_ctr(&self
->expdata
);
96 self
->expdata
.atoms
= &self
->atompool
;
97 slang_export_code_table_ctr(&self
->expcode
);
98 self
->expcode
.atoms
= &self
->atompool
;
102 _slang_code_object_dtr(slang_code_object
* self
)
106 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
107 _slang_code_unit_dtr(&self
->builtin
[i
]);
108 _slang_code_unit_dtr(&self
->unit
);
109 slang_assembly_file_destruct(&self
->assembly
);
110 slang_machine_dtr(&self
->machine
);
111 slang_atom_pool_destruct(&self
->atompool
);
112 slang_export_data_table_dtr(&self
->expdata
);
113 slang_export_code_table_ctr(&self
->expcode
);
118 static char *out_of_memory
= "Error: Out of memory.\n";
121 slang_info_log_construct(slang_info_log
* log
)
124 log
->dont_free_text
= 0;
128 slang_info_log_destruct(slang_info_log
* log
)
130 if (!log
->dont_free_text
)
131 slang_alloc_free(log
->text
);
135 slang_info_log_message(slang_info_log
* log
, const char *prefix
,
140 if (log
->dont_free_text
)
142 size
= slang_string_length(msg
) + 2;
144 size
+= slang_string_length(prefix
) + 2;
145 if (log
->text
!= NULL
) {
146 GLuint old_len
= slang_string_length(log
->text
);
148 slang_alloc_realloc(log
->text
, old_len
+ 1, old_len
+ size
);
151 log
->text
= (char *) (slang_alloc_malloc(size
));
152 if (log
->text
!= NULL
)
155 if (log
->text
== NULL
)
157 if (prefix
!= NULL
) {
158 slang_string_concat(log
->text
, prefix
);
159 slang_string_concat(log
->text
, ": ");
161 slang_string_concat(log
->text
, msg
);
162 slang_string_concat(log
->text
, "\n");
167 slang_info_log_print(slang_info_log
* log
, const char *msg
, ...)
173 _mesa_vsprintf(buf
, msg
, va
);
175 return slang_info_log_message(log
, NULL
, buf
);
179 slang_info_log_error(slang_info_log
* log
, const char *msg
, ...)
185 _mesa_vsprintf(buf
, msg
, va
);
187 if (slang_info_log_message(log
, "Error", buf
))
189 slang_info_log_memory(log
);
194 slang_info_log_warning(slang_info_log
* log
, const char *msg
, ...)
200 _mesa_vsprintf(buf
, msg
, va
);
202 if (slang_info_log_message(log
, "Warning", buf
))
204 slang_info_log_memory(log
);
209 slang_info_log_memory(slang_info_log
* log
)
211 if (!slang_info_log_message(log
, "Error", "Out of memory.")) {
212 log
->dont_free_text
= 1;
213 log
->text
= out_of_memory
;
217 /* slang_parse_ctx */
219 typedef struct slang_parse_ctx_
224 GLboolean global_scope
; /**< Is object being declared a global? */
225 slang_atom_pool
*atoms
;
228 /* slang_output_ctx */
230 typedef struct slang_output_ctx_
232 slang_variable_scope
*vars
;
233 slang_function_scope
*funs
;
234 slang_struct_scope
*structs
;
235 slang_assembly_file
*assembly
;
236 slang_var_pool
*global_pool
;
237 slang_machine
*machine
;
240 /* _slang_compile() */
243 parse_identifier_str(slang_parse_ctx
* C
, char **id
)
246 C
->I
+= _mesa_strlen(*id
) + 1;
250 parse_identifier(slang_parse_ctx
* C
)
254 id
= (const char *) C
->I
;
255 C
->I
+= _mesa_strlen(id
) + 1;
256 return slang_atom_pool_atom(C
->atoms
, id
);
260 parse_number(slang_parse_ctx
* C
, int *number
)
262 const int radix
= (int) (*C
->I
++);
264 while (*C
->I
!= '\0') {
266 if (*C
->I
>= '0' && *C
->I
<= '9')
267 digit
= (int) (*C
->I
- '0');
268 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
269 digit
= (int) (*C
->I
- 'A') + 10;
271 digit
= (int) (*C
->I
- 'a') + 10;
272 *number
= *number
* radix
+ digit
;
277 slang_info_log_warning(C
->L
, "%d: literal integer overflow.", *number
);
282 parse_float(slang_parse_ctx
* C
, float *number
)
284 char *integral
= NULL
;
285 char *fractional
= NULL
;
286 char *exponent
= NULL
;
289 parse_identifier_str(C
, &integral
);
290 parse_identifier_str(C
, &fractional
);
291 parse_identifier_str(C
, &exponent
);
293 whole
= (char *) (slang_alloc_malloc((_mesa_strlen(integral
) +
294 _mesa_strlen(fractional
) +
295 _mesa_strlen(exponent
) + 3) * sizeof(char)));
297 slang_info_log_memory(C
->L
);
301 slang_string_copy(whole
, integral
);
302 slang_string_concat(whole
, ".");
303 slang_string_concat(whole
, fractional
);
304 slang_string_concat(whole
, "E");
305 slang_string_concat(whole
, exponent
);
307 *number
= (float) (_mesa_strtod(whole
, (char **) NULL
));
309 slang_alloc_free(whole
);
313 /* revision number - increment after each change affecting emitted output */
317 check_revision(slang_parse_ctx
* C
)
319 if (*C
->I
!= REVISION
) {
320 slang_info_log_error(C
->L
, "Internal compiler error.");
327 static int parse_statement(slang_parse_ctx
*, slang_output_ctx
*,
329 static int parse_expression(slang_parse_ctx
*, slang_output_ctx
*,
331 static int parse_type_specifier(slang_parse_ctx
*, slang_output_ctx
*,
332 slang_type_specifier
*);
335 parse_array_len(slang_parse_ctx
* C
, slang_output_ctx
* O
, GLuint
* len
)
337 slang_operation array_size
;
338 slang_assembly_name_space space
;
341 if (!slang_operation_construct(&array_size
))
343 if (!parse_expression(C
, O
, &array_size
)) {
344 slang_operation_destruct(&array_size
);
348 space
.funcs
= O
->funs
;
349 space
.structs
= O
->structs
;
350 space
.vars
= O
->vars
;
351 result
= _slang_evaluate_int(O
->assembly
, O
->machine
, &space
,
352 &array_size
, len
, C
->atoms
);
353 slang_operation_destruct(&array_size
);
358 calculate_var_size(slang_parse_ctx
* C
, slang_output_ctx
* O
,
359 slang_variable
* var
)
361 slang_storage_aggregate agg
;
363 if (!slang_storage_aggregate_construct(&agg
))
365 if (!_slang_aggregate_variable(&agg
, &var
->type
.specifier
, var
->array_len
,
366 O
->funs
, O
->structs
, O
->vars
, O
->machine
,
367 O
->assembly
, C
->atoms
)) {
368 slang_storage_aggregate_destruct(&agg
);
371 var
->size
= _slang_sizeof_aggregate(&agg
);
372 slang_storage_aggregate_destruct(&agg
);
377 convert_to_array(slang_parse_ctx
* C
, slang_variable
* var
,
378 const slang_type_specifier
* sp
)
380 /* sized array - mark it as array, copy the specifier to the array element and
381 * parse the expression */
382 var
->type
.specifier
.type
= slang_spec_array
;
383 var
->type
.specifier
._array
= (slang_type_specifier
*)
384 slang_alloc_malloc(sizeof(slang_type_specifier
));
385 if (var
->type
.specifier
._array
== NULL
) {
386 slang_info_log_memory(C
->L
);
389 slang_type_specifier_ctr(var
->type
.specifier
._array
);
390 return slang_type_specifier_copy(var
->type
.specifier
._array
, sp
);
393 /* structure field */
396 #define FIELD_ARRAY 2
399 parse_struct_field_var(slang_parse_ctx
* C
, slang_output_ctx
* O
,
400 slang_variable
* var
, const slang_type_specifier
* sp
)
402 var
->a_name
= parse_identifier(C
);
403 if (var
->a_name
== SLANG_ATOM_NULL
)
408 if (!slang_type_specifier_copy(&var
->type
.specifier
, sp
))
412 if (!convert_to_array(C
, var
, sp
))
414 if (!parse_array_len(C
, O
, &var
->array_len
))
421 return calculate_var_size(C
, O
, var
);
425 parse_struct_field(slang_parse_ctx
* C
, slang_output_ctx
* O
,
426 slang_struct
* st
, slang_type_specifier
* sp
)
428 slang_output_ctx o
= *O
;
430 o
.structs
= st
->structs
;
431 if (!parse_type_specifier(C
, &o
, sp
))
437 st
->fields
->variables
=
438 (slang_variable
*) slang_alloc_realloc(st
->fields
->variables
,
439 st
->fields
->num_variables
*
440 sizeof(slang_variable
),
441 (st
->fields
->num_variables
+
442 1) * sizeof(slang_variable
));
443 if (st
->fields
->variables
== NULL
) {
444 slang_info_log_memory(C
->L
);
447 var
= &st
->fields
->variables
[st
->fields
->num_variables
];
448 if (!slang_variable_construct(var
))
450 st
->fields
->num_variables
++;
451 if (!parse_struct_field_var(C
, &o
, var
, sp
))
454 while (*C
->I
++ != FIELD_NONE
);
460 parse_struct(slang_parse_ctx
* C
, slang_output_ctx
* O
, slang_struct
** st
)
465 /* parse struct name (if any) and make sure it is unique in current scope */
466 a_name
= parse_identifier(C
);
467 if (a_name
== SLANG_ATOM_NULL
)
470 name
= slang_atom_pool_id(C
->atoms
, a_name
);
472 && slang_struct_scope_find(O
->structs
, a_name
, 0) != NULL
) {
473 slang_info_log_error(C
->L
, "%s: duplicate type name.", name
);
477 /* set-up a new struct */
478 *st
= (slang_struct
*) slang_alloc_malloc(sizeof(slang_struct
));
480 slang_info_log_memory(C
->L
);
483 if (!slang_struct_construct(*st
)) {
484 slang_alloc_free(*st
);
486 slang_info_log_memory(C
->L
);
489 (**st
).a_name
= a_name
;
490 (**st
).structs
->outer_scope
= O
->structs
;
492 /* parse individual struct fields */
494 slang_type_specifier sp
;
496 slang_type_specifier_ctr(&sp
);
497 if (!parse_struct_field(C
, O
, *st
, &sp
)) {
498 slang_type_specifier_dtr(&sp
);
501 slang_type_specifier_dtr(&sp
);
503 while (*C
->I
++ != FIELD_NONE
);
505 /* if named struct, copy it to current scope */
506 if (name
[0] != '\0') {
509 O
->structs
->structs
=
510 (slang_struct
*) slang_alloc_realloc(O
->structs
->structs
,
511 O
->structs
->num_structs
*
512 sizeof(slang_struct
),
513 (O
->structs
->num_structs
+
514 1) * sizeof(slang_struct
));
515 if (O
->structs
->structs
== NULL
) {
516 slang_info_log_memory(C
->L
);
519 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
520 if (!slang_struct_construct(s
))
522 O
->structs
->num_structs
++;
523 if (!slang_struct_copy(s
, *st
))
532 #define TYPE_QUALIFIER_NONE 0
533 #define TYPE_QUALIFIER_CONST 1
534 #define TYPE_QUALIFIER_ATTRIBUTE 2
535 #define TYPE_QUALIFIER_VARYING 3
536 #define TYPE_QUALIFIER_UNIFORM 4
537 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
538 #define TYPE_QUALIFIER_FIXEDINPUT 6
541 parse_type_qualifier(slang_parse_ctx
* C
, slang_type_qualifier
* qual
)
544 case TYPE_QUALIFIER_NONE
:
545 *qual
= slang_qual_none
;
547 case TYPE_QUALIFIER_CONST
:
548 *qual
= slang_qual_const
;
550 case TYPE_QUALIFIER_ATTRIBUTE
:
551 *qual
= slang_qual_attribute
;
553 case TYPE_QUALIFIER_VARYING
:
554 *qual
= slang_qual_varying
;
556 case TYPE_QUALIFIER_UNIFORM
:
557 *qual
= slang_qual_uniform
;
559 case TYPE_QUALIFIER_FIXEDOUTPUT
:
560 *qual
= slang_qual_fixedoutput
;
562 case TYPE_QUALIFIER_FIXEDINPUT
:
563 *qual
= slang_qual_fixedinput
;
572 #define TYPE_SPECIFIER_VOID 0
573 #define TYPE_SPECIFIER_BOOL 1
574 #define TYPE_SPECIFIER_BVEC2 2
575 #define TYPE_SPECIFIER_BVEC3 3
576 #define TYPE_SPECIFIER_BVEC4 4
577 #define TYPE_SPECIFIER_INT 5
578 #define TYPE_SPECIFIER_IVEC2 6
579 #define TYPE_SPECIFIER_IVEC3 7
580 #define TYPE_SPECIFIER_IVEC4 8
581 #define TYPE_SPECIFIER_FLOAT 9
582 #define TYPE_SPECIFIER_VEC2 10
583 #define TYPE_SPECIFIER_VEC3 11
584 #define TYPE_SPECIFIER_VEC4 12
585 #define TYPE_SPECIFIER_MAT2 13
586 #define TYPE_SPECIFIER_MAT3 14
587 #define TYPE_SPECIFIER_MAT4 15
588 #define TYPE_SPECIFIER_SAMPLER1D 16
589 #define TYPE_SPECIFIER_SAMPLER2D 17
590 #define TYPE_SPECIFIER_SAMPLER3D 18
591 #define TYPE_SPECIFIER_SAMPLERCUBE 19
592 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
593 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
594 #define TYPE_SPECIFIER_STRUCT 22
595 #define TYPE_SPECIFIER_TYPENAME 23
598 parse_type_specifier(slang_parse_ctx
* C
, slang_output_ctx
* O
,
599 slang_type_specifier
* spec
)
602 case TYPE_SPECIFIER_VOID
:
603 spec
->type
= slang_spec_void
;
605 case TYPE_SPECIFIER_BOOL
:
606 spec
->type
= slang_spec_bool
;
608 case TYPE_SPECIFIER_BVEC2
:
609 spec
->type
= slang_spec_bvec2
;
611 case TYPE_SPECIFIER_BVEC3
:
612 spec
->type
= slang_spec_bvec3
;
614 case TYPE_SPECIFIER_BVEC4
:
615 spec
->type
= slang_spec_bvec4
;
617 case TYPE_SPECIFIER_INT
:
618 spec
->type
= slang_spec_int
;
620 case TYPE_SPECIFIER_IVEC2
:
621 spec
->type
= slang_spec_ivec2
;
623 case TYPE_SPECIFIER_IVEC3
:
624 spec
->type
= slang_spec_ivec3
;
626 case TYPE_SPECIFIER_IVEC4
:
627 spec
->type
= slang_spec_ivec4
;
629 case TYPE_SPECIFIER_FLOAT
:
630 spec
->type
= slang_spec_float
;
632 case TYPE_SPECIFIER_VEC2
:
633 spec
->type
= slang_spec_vec2
;
635 case TYPE_SPECIFIER_VEC3
:
636 spec
->type
= slang_spec_vec3
;
638 case TYPE_SPECIFIER_VEC4
:
639 spec
->type
= slang_spec_vec4
;
641 case TYPE_SPECIFIER_MAT2
:
642 spec
->type
= slang_spec_mat2
;
644 case TYPE_SPECIFIER_MAT3
:
645 spec
->type
= slang_spec_mat3
;
647 case TYPE_SPECIFIER_MAT4
:
648 spec
->type
= slang_spec_mat4
;
650 case TYPE_SPECIFIER_SAMPLER1D
:
651 spec
->type
= slang_spec_sampler1D
;
653 case TYPE_SPECIFIER_SAMPLER2D
:
654 spec
->type
= slang_spec_sampler2D
;
656 case TYPE_SPECIFIER_SAMPLER3D
:
657 spec
->type
= slang_spec_sampler3D
;
659 case TYPE_SPECIFIER_SAMPLERCUBE
:
660 spec
->type
= slang_spec_samplerCube
;
662 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
663 spec
->type
= slang_spec_sampler1DShadow
;
665 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
666 spec
->type
= slang_spec_sampler2DShadow
;
668 case TYPE_SPECIFIER_STRUCT
:
669 spec
->type
= slang_spec_struct
;
670 if (!parse_struct(C
, O
, &spec
->_struct
))
673 case TYPE_SPECIFIER_TYPENAME
:
674 spec
->type
= slang_spec_struct
;
679 a_name
= parse_identifier(C
);
683 stru
= slang_struct_scope_find(O
->structs
, a_name
, 1);
685 slang_info_log_error(C
->L
, "%s: undeclared type name.",
686 slang_atom_pool_id(C
->atoms
, a_name
));
691 (slang_struct
*) slang_alloc_malloc(sizeof(slang_struct
));
692 if (spec
->_struct
== NULL
) {
693 slang_info_log_memory(C
->L
);
696 if (!slang_struct_construct(spec
->_struct
)) {
697 slang_alloc_free(spec
->_struct
);
698 spec
->_struct
= NULL
;
701 if (!slang_struct_copy(spec
->_struct
, stru
))
712 parse_fully_specified_type(slang_parse_ctx
* C
, slang_output_ctx
* O
,
713 slang_fully_specified_type
* type
)
715 if (!parse_type_qualifier(C
, &type
->qualifier
))
717 return parse_type_specifier(C
, O
, &type
->specifier
);
722 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
723 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
727 #define OP_CONTINUE 6
730 #define OP_EXPRESSION 9
735 #define OP_PUSH_VOID 14
736 #define OP_PUSH_BOOL 15
737 #define OP_PUSH_INT 16
738 #define OP_PUSH_FLOAT 17
739 #define OP_PUSH_IDENTIFIER 18
740 #define OP_SEQUENCE 19
742 #define OP_ADDASSIGN 21
743 #define OP_SUBASSIGN 22
744 #define OP_MULASSIGN 23
745 #define OP_DIVASSIGN 24
746 /*#define OP_MODASSIGN 25*/
747 /*#define OP_LSHASSIGN 26*/
748 /*#define OP_RSHASSIGN 27*/
749 /*#define OP_ORASSIGN 28*/
750 /*#define OP_XORASSIGN 29*/
751 /*#define OP_ANDASSIGN 30*/
753 #define OP_LOGICALOR 32
754 #define OP_LOGICALXOR 33
755 #define OP_LOGICALAND 34
756 /*#define OP_BITOR 35*/
757 /*#define OP_BITXOR 36*/
758 /*#define OP_BITAND 37*/
760 #define OP_NOTEQUAL 39
762 #define OP_GREATER 41
763 #define OP_LESSEQUAL 42
764 #define OP_GREATEREQUAL 43
765 /*#define OP_LSHIFT 44*/
766 /*#define OP_RSHIFT 45*/
768 #define OP_SUBTRACT 47
769 #define OP_MULTIPLY 48
771 /*#define OP_MODULUS 50*/
772 #define OP_PREINCREMENT 51
773 #define OP_PREDECREMENT 52
776 /*#define OP_COMPLEMENT 55*/
778 #define OP_SUBSCRIPT 57
781 #define OP_POSTINCREMENT 60
782 #define OP_POSTDECREMENT 61
786 * When parsing a compound production, this function is used to parse the
788 * For example, a a while-loop compound will have two children, the
789 * while condition expression and the loop body. So, this function will
790 * be called twice to parse those two sub-expressions.
791 * \param C the parsing context
792 * \param O the output context
793 * \param oper the operation we're parsing
794 * \param statment which child of the operation is being parsed
795 * \return 1 if success, 0 if error
798 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
799 slang_operation
* oper
, unsigned int statement
)
803 /* grow child array */
804 oper
->children
= (slang_operation
*)
805 slang_alloc_realloc(oper
->children
,
806 oper
->num_children
* sizeof(slang_operation
),
807 (oper
->num_children
+ 1) * sizeof(slang_operation
));
808 if (oper
->children
== NULL
) {
809 slang_info_log_memory(C
->L
);
813 ch
= &oper
->children
[oper
->num_children
];
814 if (!slang_operation_construct(ch
)) {
815 slang_info_log_memory(C
->L
);
818 oper
->num_children
++;
819 /* XXX I guess the 0th "statement" is not really a statement? */
821 return parse_statement(C
, O
, ch
);
822 return parse_expression(C
, O
, ch
);
825 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
828 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
829 slang_operation
* oper
)
831 oper
->locals
->outer_scope
= O
->vars
;
833 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
834 /* parse child statements, do not create new variable scope */
835 oper
->type
= slang_oper_block_no_new_scope
;
836 while (*C
->I
!= OP_END
)
837 if (!parse_child_operation(C
, O
, oper
, 1))
841 case OP_BLOCK_BEGIN_NEW_SCOPE
:
842 /* parse child statements, create new variable scope */
844 slang_output_ctx o
= *O
;
846 oper
->type
= slang_oper_block_new_scope
;
847 o
.vars
= oper
->locals
;
848 while (*C
->I
!= OP_END
)
849 if (!parse_child_operation(C
, &o
, oper
, 1))
855 /* local variable declaration, individual declarators are stored as
856 * children identifiers
858 oper
->type
= slang_oper_variable_decl
;
860 const unsigned int first_var
= O
->vars
->num_variables
;
862 /* parse the declaration, note that there can be zero or more
863 * than one declarators
865 if (!parse_declaration(C
, O
))
867 if (first_var
< O
->vars
->num_variables
) {
868 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
871 oper
->children
= (slang_operation
*)
872 slang_alloc_malloc(num_vars
* sizeof(slang_operation
));
873 if (oper
->children
== NULL
) {
874 slang_info_log_memory(C
->L
);
877 for (oper
->num_children
= 0; oper
->num_children
< num_vars
;
878 oper
->num_children
++) {
879 if (!slang_operation_construct
880 (&oper
->children
[oper
->num_children
])) {
881 slang_info_log_memory(C
->L
);
885 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
886 slang_operation
*o
= &oper
->children
[i
- first_var
];
887 o
->type
= slang_oper_identifier
;
888 o
->locals
->outer_scope
= O
->vars
;
889 o
->a_id
= O
->vars
->variables
[i
].a_name
;
895 /* the __asm statement, parse the mnemonic and all its arguments
898 oper
->type
= slang_oper_asm
;
899 oper
->a_id
= parse_identifier(C
);
900 if (oper
->a_id
== SLANG_ATOM_NULL
)
902 while (*C
->I
!= OP_END
) {
903 if (!parse_child_operation(C
, O
, oper
, 0))
909 oper
->type
= slang_oper_break
;
912 oper
->type
= slang_oper_continue
;
915 oper
->type
= slang_oper_discard
;
918 oper
->type
= slang_oper_return
;
919 if (!parse_child_operation(C
, O
, oper
, 0))
923 oper
->type
= slang_oper_expression
;
924 if (!parse_child_operation(C
, O
, oper
, 0))
928 oper
->type
= slang_oper_if
;
929 if (!parse_child_operation(C
, O
, oper
, 0))
931 if (!parse_child_operation(C
, O
, oper
, 1))
933 if (!parse_child_operation(C
, O
, oper
, 1))
938 slang_output_ctx o
= *O
;
940 oper
->type
= slang_oper_while
;
941 o
.vars
= oper
->locals
;
942 if (!parse_child_operation(C
, &o
, oper
, 1))
944 if (!parse_child_operation(C
, &o
, oper
, 1))
949 oper
->type
= slang_oper_do
;
950 if (!parse_child_operation(C
, O
, oper
, 1))
952 if (!parse_child_operation(C
, O
, oper
, 0))
957 slang_output_ctx o
= *O
;
959 oper
->type
= slang_oper_for
;
960 o
.vars
= oper
->locals
;
961 if (!parse_child_operation(C
, &o
, oper
, 1))
963 if (!parse_child_operation(C
, &o
, oper
, 1))
965 if (!parse_child_operation(C
, &o
, oper
, 0))
967 if (!parse_child_operation(C
, &o
, oper
, 1))
978 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
979 slang_operation
** ops
, unsigned int *total_ops
,
985 (slang_operation
*) slang_alloc_malloc(n
* sizeof(slang_operation
));
986 if (op
->children
== NULL
) {
987 slang_info_log_memory(C
->L
);
990 op
->num_children
= n
;
992 for (i
= 0; i
< n
; i
++)
993 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
994 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
997 *ops
= (slang_operation
*)
998 slang_alloc_realloc(*ops
,
999 (*total_ops
+ n
) * sizeof(slang_operation
),
1000 *total_ops
* sizeof(slang_operation
));
1002 slang_info_log_memory(C
->L
);
1009 is_constructor_name(const char *name
, slang_atom a_name
,
1010 slang_struct_scope
* structs
)
1012 if (slang_type_specifier_type_from_string(name
) != slang_spec_void
)
1014 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
1018 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1019 slang_operation
* oper
)
1021 slang_operation
*ops
= NULL
;
1022 unsigned int num_ops
= 0;
1025 while (*C
->I
!= OP_END
) {
1026 slang_operation
*op
;
1027 const unsigned int op_code
= *C
->I
++;
1029 /* allocate default operation, becomes a no-op if not used */
1030 ops
= (slang_operation
*)
1031 slang_alloc_realloc(ops
,
1032 num_ops
* sizeof(slang_operation
),
1033 (num_ops
+ 1) * sizeof(slang_operation
));
1035 slang_info_log_memory(C
->L
);
1039 if (!slang_operation_construct(op
)) {
1040 slang_info_log_memory(C
->L
);
1044 op
->locals
->outer_scope
= O
->vars
;
1048 op
->type
= slang_oper_void
;
1051 op
->type
= slang_oper_literal_bool
;
1052 if (!parse_number(C
, &number
))
1054 op
->literal
= (GLfloat
) number
;
1057 op
->type
= slang_oper_literal_int
;
1058 if (!parse_number(C
, &number
))
1060 op
->literal
= (GLfloat
) number
;
1063 op
->type
= slang_oper_literal_float
;
1064 if (!parse_float(C
, &op
->literal
))
1067 case OP_PUSH_IDENTIFIER
:
1068 op
->type
= slang_oper_identifier
;
1069 op
->a_id
= parse_identifier(C
);
1070 if (op
->a_id
== SLANG_ATOM_NULL
)
1074 op
->type
= slang_oper_sequence
;
1075 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1079 op
->type
= slang_oper_assign
;
1080 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1084 op
->type
= slang_oper_addassign
;
1085 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1089 op
->type
= slang_oper_subassign
;
1090 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1094 op
->type
= slang_oper_mulassign
;
1095 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1099 op
->type
= slang_oper_divassign
;
1100 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1103 /*case OP_MODASSIGN: */
1104 /*case OP_LSHASSIGN: */
1105 /*case OP_RSHASSIGN: */
1106 /*case OP_ORASSIGN: */
1107 /*case OP_XORASSIGN: */
1108 /*case OP_ANDASSIGN: */
1110 op
->type
= slang_oper_select
;
1111 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1115 op
->type
= slang_oper_logicalor
;
1116 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1120 op
->type
= slang_oper_logicalxor
;
1121 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1125 op
->type
= slang_oper_logicaland
;
1126 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1130 /*case OP_BITXOR: */
1131 /*case OP_BITAND: */
1133 op
->type
= slang_oper_equal
;
1134 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1138 op
->type
= slang_oper_notequal
;
1139 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1143 op
->type
= slang_oper_less
;
1144 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1148 op
->type
= slang_oper_greater
;
1149 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1153 op
->type
= slang_oper_lessequal
;
1154 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1157 case OP_GREATEREQUAL
:
1158 op
->type
= slang_oper_greaterequal
;
1159 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1162 /*case OP_LSHIFT: */
1163 /*case OP_RSHIFT: */
1165 op
->type
= slang_oper_add
;
1166 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1170 op
->type
= slang_oper_subtract
;
1171 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1175 op
->type
= slang_oper_multiply
;
1176 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1180 op
->type
= slang_oper_divide
;
1181 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1184 /*case OP_MODULUS: */
1185 case OP_PREINCREMENT
:
1186 op
->type
= slang_oper_preincrement
;
1187 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1190 case OP_PREDECREMENT
:
1191 op
->type
= slang_oper_predecrement
;
1192 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1196 op
->type
= slang_oper_plus
;
1197 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1201 op
->type
= slang_oper_minus
;
1202 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1206 op
->type
= slang_oper_not
;
1207 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1210 /*case OP_COMPLEMENT: */
1212 op
->type
= slang_oper_subscript
;
1213 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1217 op
->type
= slang_oper_call
;
1218 op
->a_id
= parse_identifier(C
);
1219 if (op
->a_id
== SLANG_ATOM_NULL
)
1221 while (*C
->I
!= OP_END
)
1222 if (!parse_child_operation(C
, O
, op
, 0))
1225 if (!C
->parsing_builtin
1226 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1229 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1230 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1231 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1237 op
->type
= slang_oper_field
;
1238 op
->a_id
= parse_identifier(C
);
1239 if (op
->a_id
== SLANG_ATOM_NULL
)
1241 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1244 case OP_POSTINCREMENT
:
1245 op
->type
= slang_oper_postincrement
;
1246 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1249 case OP_POSTDECREMENT
:
1250 op
->type
= slang_oper_postdecrement
;
1251 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1261 slang_alloc_free(ops
);
1266 /* parameter qualifier */
1267 #define PARAM_QUALIFIER_IN 0
1268 #define PARAM_QUALIFIER_OUT 1
1269 #define PARAM_QUALIFIER_INOUT 2
1271 /* function parameter array presence */
1272 #define PARAMETER_ARRAY_NOT_PRESENT 0
1273 #define PARAMETER_ARRAY_PRESENT 1
1276 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1277 slang_variable
* param
)
1279 /* parse and validate the parameter's type qualifiers (there can be
1280 * two at most) because not all combinations are valid
1282 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1285 case PARAM_QUALIFIER_IN
:
1286 if (param
->type
.qualifier
!= slang_qual_const
1287 && param
->type
.qualifier
!= slang_qual_none
) {
1288 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1292 case PARAM_QUALIFIER_OUT
:
1293 if (param
->type
.qualifier
== slang_qual_none
)
1294 param
->type
.qualifier
= slang_qual_out
;
1296 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1300 case PARAM_QUALIFIER_INOUT
:
1301 if (param
->type
.qualifier
== slang_qual_none
)
1302 param
->type
.qualifier
= slang_qual_inout
;
1304 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1312 /* parse parameter's type specifier and name */
1313 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1315 param
->a_name
= parse_identifier(C
);
1316 if (param
->a_name
== SLANG_ATOM_NULL
)
1319 /* if the parameter is an array, parse its size (the size must be
1320 * explicitly defined
1322 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1323 slang_type_specifier p
;
1325 slang_type_specifier_ctr(&p
);
1326 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1327 slang_type_specifier_dtr(&p
);
1330 if (!convert_to_array(C
, param
, &p
)) {
1331 slang_type_specifier_dtr(&p
);
1334 slang_type_specifier_dtr(&p
);
1335 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1339 /* calculate the parameter size */
1340 if (!calculate_var_size(C
, O
, param
))
1343 /* TODO: allocate the local address here? */
1348 #define FUNCTION_ORDINARY 0
1349 #define FUNCTION_CONSTRUCTOR 1
1350 #define FUNCTION_OPERATOR 2
1352 /* function parameter */
1353 #define PARAMETER_NONE 0
1354 #define PARAMETER_NEXT 1
1357 #define OPERATOR_ADDASSIGN 1
1358 #define OPERATOR_SUBASSIGN 2
1359 #define OPERATOR_MULASSIGN 3
1360 #define OPERATOR_DIVASSIGN 4
1361 /*#define OPERATOR_MODASSIGN 5*/
1362 /*#define OPERATOR_LSHASSIGN 6*/
1363 /*#define OPERATOR_RSHASSIGN 7*/
1364 /*#define OPERATOR_ANDASSIGN 8*/
1365 /*#define OPERATOR_XORASSIGN 9*/
1366 /*#define OPERATOR_ORASSIGN 10*/
1367 #define OPERATOR_LOGICALXOR 11
1368 /*#define OPERATOR_BITOR 12*/
1369 /*#define OPERATOR_BITXOR 13*/
1370 /*#define OPERATOR_BITAND 14*/
1371 #define OPERATOR_LESS 15
1372 #define OPERATOR_GREATER 16
1373 #define OPERATOR_LESSEQUAL 17
1374 #define OPERATOR_GREATEREQUAL 18
1375 /*#define OPERATOR_LSHIFT 19*/
1376 /*#define OPERATOR_RSHIFT 20*/
1377 #define OPERATOR_MULTIPLY 21
1378 #define OPERATOR_DIVIDE 22
1379 /*#define OPERATOR_MODULUS 23*/
1380 #define OPERATOR_INCREMENT 24
1381 #define OPERATOR_DECREMENT 25
1382 #define OPERATOR_PLUS 26
1383 #define OPERATOR_MINUS 27
1384 /*#define OPERATOR_COMPLEMENT 28*/
1385 #define OPERATOR_NOT 29
1389 unsigned int o_code
;
1391 } operator_names
[] = {
1392 {OPERATOR_INCREMENT
, "++"},
1393 {OPERATOR_ADDASSIGN
, "+="},
1394 {OPERATOR_PLUS
, "+"},
1395 {OPERATOR_DECREMENT
, "--"},
1396 {OPERATOR_SUBASSIGN
, "-="},
1397 {OPERATOR_MINUS
, "-"},
1398 {OPERATOR_NOT
, "!"},
1399 {OPERATOR_MULASSIGN
, "*="},
1400 {OPERATOR_MULTIPLY
, "*"},
1401 {OPERATOR_DIVASSIGN
, "/="},
1402 {OPERATOR_DIVIDE
, "/"},
1403 {OPERATOR_LESSEQUAL
, "<="},
1404 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1405 /*{ OPERATOR_LSHIFT, "<<" }, */
1406 {OPERATOR_LESS
, "<"},
1407 {OPERATOR_GREATEREQUAL
, ">="},
1408 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1409 /*{ OPERATOR_RSHIFT, ">>" }, */
1410 {OPERATOR_GREATER
, ">"},
1411 /*{ OPERATOR_MODASSIGN, "%=" }, */
1412 /*{ OPERATOR_MODULUS, "%" }, */
1413 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1414 /*{ OPERATOR_BITAND, "&" }, */
1415 /*{ OPERATOR_ORASSIGN, "|=" }, */
1416 /*{ OPERATOR_BITOR, "|" }, */
1417 /*{ OPERATOR_COMPLEMENT, "~" }, */
1418 /*{ OPERATOR_XORASSIGN, "^=" }, */
1419 {OPERATOR_LOGICALXOR
, "^^"},
1420 /*{ OPERATOR_BITXOR, "^" } */
1424 parse_operator_name(slang_parse_ctx
* C
)
1428 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1429 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1431 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1432 if (atom
== SLANG_ATOM_NULL
) {
1433 slang_info_log_memory(C
->L
);
1444 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1445 slang_function
* func
)
1447 /* parse function type and name */
1448 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1451 case FUNCTION_ORDINARY
:
1452 func
->kind
= slang_func_ordinary
;
1453 func
->header
.a_name
= parse_identifier(C
);
1454 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1457 case FUNCTION_CONSTRUCTOR
:
1458 func
->kind
= slang_func_constructor
;
1459 if (func
->header
.type
.specifier
.type
== slang_spec_struct
)
1461 func
->header
.a_name
=
1462 slang_atom_pool_atom(C
->atoms
,
1463 slang_type_specifier_type_to_string
1464 (func
->header
.type
.specifier
.type
));
1465 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1466 slang_info_log_memory(C
->L
);
1470 case FUNCTION_OPERATOR
:
1471 func
->kind
= slang_func_operator
;
1472 func
->header
.a_name
= parse_operator_name(C
);
1473 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1480 /* parse function parameters */
1481 while (*C
->I
++ == PARAMETER_NEXT
) {
1484 func
->parameters
->variables
= (slang_variable
*)
1485 slang_alloc_realloc(func
->parameters
->variables
,
1486 func
->parameters
->num_variables
* sizeof(slang_variable
),
1487 (func
->parameters
->num_variables
+ 1) * sizeof(slang_variable
));
1488 if (func
->parameters
->variables
== NULL
) {
1489 slang_info_log_memory(C
->L
);
1492 p
= &func
->parameters
->variables
[func
->parameters
->num_variables
];
1493 if (!slang_variable_construct(p
))
1495 func
->parameters
->num_variables
++;
1496 if (!parse_parameter_declaration(C
, O
, p
))
1500 /* function formal parameters and local variables share the same
1501 * scope, so save the information about param count in a seperate
1502 * place also link the scope to the global variable scope so when a
1503 * given identifier is not found here, the search process continues
1504 * in the global space
1506 func
->param_count
= func
->parameters
->num_variables
;
1507 func
->parameters
->outer_scope
= O
->vars
;
1512 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1513 slang_function
* func
)
1515 slang_output_ctx o
= *O
;
1517 if (!parse_function_prototype(C
, O
, func
))
1520 /* create function's body operation */
1522 (slang_operation
*) slang_alloc_malloc(sizeof(slang_operation
));
1523 if (func
->body
== NULL
) {
1524 slang_info_log_memory(C
->L
);
1527 if (!slang_operation_construct(func
->body
)) {
1528 slang_alloc_free(func
->body
);
1530 slang_info_log_memory(C
->L
);
1534 /* to parse the body the parse context is modified in order to
1535 * capture parsed variables into function's local variable scope
1537 C
->global_scope
= GL_FALSE
;
1538 o
.vars
= func
->parameters
;
1539 if (!parse_statement(C
, &o
, func
->body
))
1542 C
->global_scope
= GL_TRUE
;
1547 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1549 slang_assembly_file_restore_point point
;
1551 slang_assembly_local_info save_local
= A
->local
;
1552 slang_operation op_id
, op_assign
;
1555 /* save the current assembly */
1556 if (!slang_assembly_file_restore_point_save(A
->file
, &point
))
1559 /* setup the machine */
1561 mach
.ip
= A
->file
->count
;
1563 /* allocate local storage for expression */
1564 A
->local
.ret_size
= 0;
1565 A
->local
.addr_tmp
= 0;
1566 A
->local
.swizzle_tmp
= 4;
1567 if (!slang_assembly_file_push_label(A
->file
, slang_asm_local_alloc
, 20))
1569 if (!slang_assembly_file_push_label(A
->file
, slang_asm_enter
, 20))
1572 /* construct the left side of assignment */
1573 if (!slang_operation_construct(&op_id
))
1575 op_id
.type
= slang_oper_identifier
;
1576 op_id
.a_id
= var
->a_name
;
1578 /* put the variable into operation's scope */
1579 op_id
.locals
->variables
=
1580 (slang_variable
*) slang_alloc_malloc(sizeof(slang_variable
));
1581 if (op_id
.locals
->variables
== NULL
) {
1582 slang_operation_destruct(&op_id
);
1585 op_id
.locals
->num_variables
= 1;
1586 op_id
.locals
->variables
[0] = *var
;
1588 /* construct the assignment expression */
1589 if (!slang_operation_construct(&op_assign
)) {
1590 op_id
.locals
->num_variables
= 0;
1591 slang_operation_destruct(&op_id
);
1594 op_assign
.type
= slang_oper_assign
;
1595 op_assign
.children
=
1596 (slang_operation
*) slang_alloc_malloc(2 * sizeof(slang_operation
));
1597 if (op_assign
.children
== NULL
) {
1598 slang_operation_destruct(&op_assign
);
1599 op_id
.locals
->num_variables
= 0;
1600 slang_operation_destruct(&op_id
);
1603 op_assign
.num_children
= 2;
1604 op_assign
.children
[0] = op_id
;
1605 op_assign
.children
[1] = *var
->initializer
;
1607 /* insert the actual expression */
1608 result
= _slang_assemble_operation(A
, &op_assign
, slang_ref_forbid
);
1610 /* carefully destroy the operations */
1611 op_assign
.num_children
= 0;
1612 slang_alloc_free(op_assign
.children
);
1613 op_assign
.children
= NULL
;
1614 slang_operation_destruct(&op_assign
);
1615 op_id
.locals
->num_variables
= 0;
1616 slang_operation_destruct(&op_id
);
1620 if (!slang_assembly_file_push(A
->file
, slang_asm_exit
))
1623 /* execute the expression */
1624 if (!_slang_execute2(A
->file
, &mach
))
1627 /* restore the old assembly */
1628 if (!slang_assembly_file_restore_point_load(A
->file
, &point
))
1630 A
->local
= save_local
;
1632 /* now we copy the contents of the initialized variable back to the original machine */
1633 _mesa_memcpy((GLubyte
*) A
->mach
->mem
+ var
->address
,
1634 (GLubyte
*) mach
.mem
+ var
->address
, var
->size
);
1639 /* init declarator list */
1640 #define DECLARATOR_NONE 0
1641 #define DECLARATOR_NEXT 1
1643 /* variable declaration */
1644 #define VARIABLE_NONE 0
1645 #define VARIABLE_IDENTIFIER 1
1646 #define VARIABLE_INITIALIZER 2
1647 #define VARIABLE_ARRAY_EXPLICIT 3
1648 #define VARIABLE_ARRAY_UNKNOWN 4
1652 * Parse the initializer for a variable declaration.
1655 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1656 const slang_fully_specified_type
* type
)
1658 slang_variable
*var
;
1660 /* empty init declatator (without name, e.g. "float ;") */
1661 if (*C
->I
++ == VARIABLE_NONE
)
1664 /* make room for the new variable and initialize it */
1665 O
->vars
->variables
= (slang_variable
*)
1666 slang_alloc_realloc(O
->vars
->variables
,
1667 O
->vars
->num_variables
* sizeof(slang_variable
),
1668 (O
->vars
->num_variables
+ 1) * sizeof(slang_variable
));
1669 if (O
->vars
->variables
== NULL
) {
1670 slang_info_log_memory(C
->L
);
1673 var
= &O
->vars
->variables
[O
->vars
->num_variables
];
1674 if (!slang_variable_construct(var
))
1676 O
->vars
->num_variables
++;
1678 /* copy the declarator qualifier type, parse the identifier */
1679 var
->global
= C
->global_scope
;
1680 var
->type
.qualifier
= type
->qualifier
;
1681 var
->a_name
= parse_identifier(C
);
1682 if (var
->a_name
== SLANG_ATOM_NULL
)
1687 /* simple variable declarator - just copy the specifier */
1688 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1691 case VARIABLE_INITIALIZER
:
1692 /* initialized variable - copy the specifier and parse the expression */
1693 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1696 (slang_operation
*) slang_alloc_malloc(sizeof(slang_operation
));
1697 if (var
->initializer
== NULL
) {
1698 slang_info_log_memory(C
->L
);
1701 if (!slang_operation_construct(var
->initializer
)) {
1702 slang_alloc_free(var
->initializer
);
1703 var
->initializer
= NULL
;
1704 slang_info_log_memory(C
->L
);
1707 if (!parse_expression(C
, O
, var
->initializer
))
1711 case VARIABLE_ARRAY_UNKNOWN
:
1712 /* unsized array - mark it as array and copy the specifier to
1715 if (!convert_to_array(C
, var
, &type
->specifier
))
1719 case VARIABLE_ARRAY_EXPLICIT
:
1720 if (!convert_to_array(C
, var
, &type
->specifier
))
1722 if (!parse_array_len(C
, O
, &var
->array_len
))
1729 /* allocate global address space for a variable with a known size */
1731 && !(var
->type
.specifier
.type
== slang_spec_array
1732 && var
->array_len
== 0)) {
1733 if (!calculate_var_size(C
, O
, var
))
1735 var
->address
= slang_var_pool_alloc(O
->global_pool
, var
->size
);
1738 /* initialize global variable */
1739 if (C
->global_scope
) {
1740 if (var
->initializer
!= NULL
) {
1741 slang_assemble_ctx A
;
1743 A
.file
= O
->assembly
;
1744 A
.mach
= O
->machine
;
1746 A
.space
.funcs
= O
->funs
;
1747 A
.space
.structs
= O
->structs
;
1748 A
.space
.vars
= O
->vars
;
1749 if (!initialize_global(&A
, var
))
1753 _mesa_memset((GLubyte
*) (O
->machine
->mem
) + var
->address
, 0,
1761 * Parse a list of variable declarations. Each variable may have an
1765 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1767 slang_fully_specified_type type
;
1769 /* parse the fully specified type, common to all declarators */
1770 if (!slang_fully_specified_type_construct(&type
))
1772 if (!parse_fully_specified_type(C
, O
, &type
)) {
1773 slang_fully_specified_type_destruct(&type
);
1777 /* parse declarators, pass-in the parsed type */
1779 if (!parse_init_declarator(C
, O
, &type
)) {
1780 slang_fully_specified_type_destruct(&type
);
1784 while (*C
->I
++ == DECLARATOR_NEXT
);
1786 slang_fully_specified_type_destruct(&type
);
1792 * Parse a function definition or declaration.
1793 * \param C parsing context
1794 * \param O output context
1795 * \param definition if non-zero expect a definition, else a declaration
1796 * \param parsed_func_ret returns the parsed function
1797 * \return 1 if success, 0 if failure
1800 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
1801 slang_function
** parsed_func_ret
)
1803 slang_function parsed_func
, *found_func
;
1805 /* parse function definition/declaration */
1806 if (!slang_function_construct(&parsed_func
))
1809 if (!parse_function_definition(C
, O
, &parsed_func
)) {
1810 slang_function_destruct(&parsed_func
);
1815 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
1816 slang_function_destruct(&parsed_func
);
1821 /* find a function with a prototype matching the parsed one - only
1822 * the current scope is being searched to allow built-in function
1825 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
1826 if (found_func
== NULL
) {
1827 /* add the parsed function to the function list */
1828 O
->funs
->functions
=
1829 (slang_function
*) slang_alloc_realloc(O
->funs
->functions
,
1830 O
->funs
->num_functions
*
1831 sizeof(slang_function
),
1832 (O
->funs
->num_functions
+
1833 1) * sizeof(slang_function
));
1834 if (O
->funs
->functions
== NULL
) {
1835 slang_info_log_memory(C
->L
);
1836 slang_function_destruct(&parsed_func
);
1839 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
1840 O
->funs
->num_functions
++;
1842 /* return the newly parsed function */
1843 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
1846 /* TODO: check function return type qualifiers and specifiers */
1848 if (found_func
->body
!= NULL
) {
1849 slang_info_log_error(C
->L
, "%s: function already has a body.",
1850 slang_atom_pool_id(C
->atoms
,
1853 slang_function_destruct(&parsed_func
);
1857 /* destroy the existing function declaration and replace it
1858 * with the new one, remember to save the fixup table
1860 parsed_func
.fixups
= found_func
->fixups
;
1861 slang_fixup_table_init(&found_func
->fixups
);
1862 slang_function_destruct(found_func
);
1863 *found_func
= parsed_func
;
1866 /* another declaration of the same function prototype - ignore it */
1867 slang_function_destruct(&parsed_func
);
1870 /* return the found function */
1871 *parsed_func_ret
= found_func
;
1874 /* assemble the parsed function */
1876 slang_assemble_ctx A
;
1878 A
.file
= O
->assembly
;
1879 A
.mach
= O
->machine
;
1881 A
.space
.funcs
= O
->funs
;
1882 A
.space
.structs
= O
->structs
;
1883 A
.space
.vars
= O
->vars
;
1884 if (!_slang_assemble_function(&A
, *parsed_func_ret
))
1891 #define DECLARATION_FUNCTION_PROTOTYPE 1
1892 #define DECLARATION_INIT_DECLARATOR_LIST 2
1895 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1898 case DECLARATION_INIT_DECLARATOR_LIST
:
1899 if (!parse_init_declarator_list(C
, O
))
1902 case DECLARATION_FUNCTION_PROTOTYPE
:
1904 slang_function
*dummy_func
;
1906 if (!parse_function(C
, O
, 0, &dummy_func
))
1916 /* external declaration */
1917 #define EXTERNAL_NULL 0
1918 #define EXTERNAL_FUNCTION_DEFINITION 1
1919 #define EXTERNAL_DECLARATION 2
1922 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
)
1926 /* setup output context */
1927 o
.funs
= &unit
->funs
;
1928 o
.structs
= &unit
->structs
;
1929 o
.vars
= &unit
->vars
;
1930 o
.assembly
= &unit
->object
->assembly
;
1931 o
.global_pool
= &unit
->object
->varpool
;
1932 o
.machine
= &unit
->object
->machine
;
1934 /* parse individual functions and declarations */
1935 while (*C
->I
!= EXTERNAL_NULL
) {
1937 case EXTERNAL_FUNCTION_DEFINITION
:
1939 slang_function
*func
;
1941 if (!parse_function(C
, &o
, 1, &func
))
1945 case EXTERNAL_DECLARATION
:
1946 if (!parse_declaration(C
, &o
))
1958 compile_binary(const byte
* prod
, slang_code_unit
* unit
,
1959 slang_unit_type type
, slang_info_log
* infolog
,
1960 slang_code_unit
* builtin
, slang_code_unit
* downlink
)
1966 /* setup parse context */
1969 C
.parsing_builtin
= (builtin
== NULL
);
1970 C
.global_scope
= GL_TRUE
;
1971 C
.atoms
= &unit
->object
->atompool
;
1973 if (!check_revision(&C
))
1976 if (downlink
!= NULL
) {
1977 unit
->vars
.outer_scope
= &downlink
->vars
;
1978 unit
->funs
.outer_scope
= &downlink
->funs
;
1979 unit
->structs
.outer_scope
= &downlink
->structs
;
1982 /* parse translation unit */
1983 return parse_code_unit(&C
, unit
);
1987 compile_with_grammar(grammar id
, const char *source
, slang_code_unit
* unit
,
1988 slang_unit_type type
, slang_info_log
* infolog
,
1989 slang_code_unit
* builtin
)
1992 GLuint size
, start
, version
;
1993 slang_string preprocessed
;
1995 /* First retrieve the version number. */
1996 if (!_slang_preprocess_version(source
, &version
, &start
, infolog
))
1999 if (version
> 110) {
2000 slang_info_log_error(infolog
,
2001 "language version specified is not supported.");
2005 /* Now preprocess the source string. */
2006 slang_string_init(&preprocessed
);
2007 if (!_slang_preprocess_directives(&preprocessed
, &source
[start
], infolog
)) {
2008 slang_string_free(&preprocessed
);
2009 slang_info_log_error(infolog
, "failed to preprocess the source.");
2013 /* Finally check the syntax and generate its binary representation. */
2014 if (!grammar_fast_check
2015 (id
, (const byte
*) (slang_string_cstr(&preprocessed
)), &prod
, &size
,
2020 slang_string_free(&preprocessed
);
2021 grammar_get_last_error((byte
*) (buf
), sizeof(buf
), &pos
);
2022 slang_info_log_error(infolog
, buf
);
2025 slang_string_free(&preprocessed
);
2027 /* Syntax is okay - translate it to internal representation. */
2029 (prod
, unit
, type
, infolog
, builtin
,
2030 &builtin
[SLANG_BUILTIN_TOTAL
- 1])) {
2031 grammar_alloc_free(prod
);
2034 grammar_alloc_free(prod
);
2038 LONGSTRING
static const char *slang_shader_syn
=
2039 #include "library/slang_shader_syn.h"
2042 static const byte slang_core_gc
[] = {
2043 #include "library/slang_core_gc.h"
2046 static const byte slang_common_builtin_gc
[] = {
2047 #include "library/slang_common_builtin_gc.h"
2050 static const byte slang_fragment_builtin_gc
[] = {
2051 #include "library/slang_fragment_builtin_gc.h"
2054 static const byte slang_vertex_builtin_gc
[] = {
2055 #include "library/slang_vertex_builtin_gc.h"
2058 #if defined(USE_X86_ASM) || defined(SLANG_X86)
2059 static const byte slang_builtin_vec4_gc
[] = {
2060 #include "library/slang_builtin_vec4_gc.h"
2065 compile_object(grammar
* id
, const char *source
, slang_code_object
* object
,
2066 slang_unit_type type
, slang_info_log
* infolog
)
2068 slang_code_unit
*builtins
= NULL
;
2070 /* load GLSL grammar */
2071 *id
= grammar_load_from_text((const byte
*) (slang_shader_syn
));
2076 grammar_get_last_error(buf
, 1024, &pos
);
2077 slang_info_log_error(infolog
, (const char *) (buf
));
2081 /* set shader type - the syntax is slightly different for different shaders */
2082 if (type
== slang_unit_fragment_shader
2083 || type
== slang_unit_fragment_builtin
)
2084 grammar_set_reg8(*id
, (const byte
*) "shader_type", 1);
2086 grammar_set_reg8(*id
, (const byte
*) "shader_type", 2);
2088 /* enable language extensions */
2089 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2091 /* if parsing user-specified shader, load built-in library */
2092 if (type
== slang_unit_fragment_shader
|| type
== slang_unit_vertex_shader
) {
2093 /* compile core functionality first */
2094 if (!compile_binary(slang_core_gc
, &object
->builtin
[SLANG_BUILTIN_CORE
],
2095 slang_unit_fragment_builtin
, infolog
, NULL
, NULL
))
2098 /* compile common functions and variables, link to core */
2100 (slang_common_builtin_gc
, &object
->builtin
[SLANG_BUILTIN_COMMON
],
2101 slang_unit_fragment_builtin
, infolog
, NULL
,
2102 &object
->builtin
[SLANG_BUILTIN_CORE
]))
2105 /* compile target-specific functions and variables, link to common */
2106 if (type
== slang_unit_fragment_shader
) {
2108 (slang_fragment_builtin_gc
,
2109 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2110 slang_unit_fragment_builtin
, infolog
, NULL
,
2111 &object
->builtin
[SLANG_BUILTIN_COMMON
]))
2114 else if (type
== slang_unit_vertex_shader
) {
2116 (slang_vertex_builtin_gc
, &object
->builtin
[SLANG_BUILTIN_TARGET
],
2117 slang_unit_vertex_builtin
, infolog
, NULL
,
2118 &object
->builtin
[SLANG_BUILTIN_COMMON
]))
2122 #if defined(USE_X86_ASM) || defined(SLANG_X86)
2123 /* compile x86 4-component vector overrides, link to target */
2125 (slang_builtin_vec4_gc
, &object
->builtin
[SLANG_BUILTIN_VEC4
],
2126 slang_unit_fragment_builtin
, infolog
, NULL
,
2127 &object
->builtin
[SLANG_BUILTIN_TARGET
]))
2131 /* disable language extensions */
2132 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 0);
2133 builtins
= object
->builtin
;
2136 /* compile the actual shader - pass-in built-in library for external shader */
2137 return compile_with_grammar(*id
, source
, &object
->unit
, type
, infolog
,
2142 _slang_compile(const char *source
, slang_code_object
* object
,
2143 slang_unit_type type
, slang_info_log
* infolog
)
2148 _slang_code_object_dtr(object
);
2149 _slang_code_object_ctr(object
);
2151 success
= compile_object(&id
, source
, object
, type
, infolog
);
2153 grammar_destroy(id
);
2157 if (!_slang_build_export_data_table(&object
->expdata
, &object
->unit
.vars
))
2159 if (!_slang_build_export_code_table
2160 (&object
->expcode
, &object
->unit
.funs
, &object
->unit
))
2163 #if defined(USE_X86_ASM) || defined(SLANG_X86)
2164 /* XXX: lookup the @main label */
2165 if (!_slang_x86_codegen
2166 (&object
->machine
, &object
->assembly
,
2167 object
->expcode
.entries
[0].address
))