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 compiler.
39 * Lots of error-checking functionality is missing but every well-formed shader source should
40 * compile successfully and execute as expected. However, some semantically ill-formed shaders
41 * may be accepted resulting in undefined behaviour.
46 static GLuint
slang_var_pool_alloc (slang_var_pool
*pool
, unsigned int size
)
50 addr
= pool
->next_addr
;
51 pool
->next_addr
+= size
;
55 /* slang_translation_unit */
57 int slang_translation_unit_construct (slang_translation_unit
*unit
)
59 unit
->assembly
= (slang_assembly_file
*) slang_alloc_malloc (sizeof (slang_assembly_file
));
60 if (unit
->assembly
== NULL
)
62 if (!slang_assembly_file_construct (unit
->assembly
))
64 slang_alloc_free (unit
->assembly
);
67 unit
->global_pool
= (slang_var_pool
*) slang_alloc_malloc (sizeof (slang_var_pool
));
68 if (unit
->global_pool
== NULL
)
70 slang_assembly_file_destruct (unit
->assembly
);
71 slang_alloc_free (unit
->assembly
);
74 unit
->global_pool
->next_addr
= 0;
75 unit
->machine
= (slang_machine
*) slang_alloc_malloc (sizeof (slang_machine
));
76 if (unit
->machine
== NULL
)
78 slang_alloc_free (unit
->global_pool
);
79 slang_assembly_file_destruct (unit
->assembly
);
80 slang_alloc_free (unit
->assembly
);
83 slang_machine_ctr (unit
->machine
);
84 unit
->atom_pool
= (slang_atom_pool
*) slang_alloc_malloc (sizeof (slang_atom_pool
));
85 if (unit
->atom_pool
== NULL
)
87 slang_alloc_free (unit
->machine
);
88 slang_alloc_free (unit
->global_pool
);
89 slang_assembly_file_destruct (unit
->assembly
);
90 slang_alloc_free (unit
->assembly
);
93 slang_atom_pool_construct (unit
->atom_pool
);
94 if (!slang_translation_unit_construct2 (unit
, unit
->assembly
, unit
->global_pool
, unit
->machine
,
97 slang_alloc_free (unit
->atom_pool
);
98 slang_alloc_free (unit
->machine
);
99 slang_alloc_free (unit
->global_pool
);
100 slang_assembly_file_destruct (unit
->assembly
);
101 slang_alloc_free (unit
->assembly
);
104 unit
->free_assembly
= 1;
105 unit
->free_global_pool
= 1;
106 unit
->free_machine
= 1;
107 unit
->free_atom_pool
= 1;
111 int slang_translation_unit_construct2 (slang_translation_unit
*unit
, slang_assembly_file
*file
,
112 slang_var_pool
*pool
, struct slang_machine_
*mach
, slang_atom_pool
*atoms
)
114 if (!slang_variable_scope_construct (&unit
->globals
))
116 if (!slang_function_scope_construct (&unit
->functions
))
118 slang_variable_scope_destruct (&unit
->globals
);
121 if (!slang_struct_scope_construct (&unit
->structs
))
123 slang_variable_scope_destruct (&unit
->globals
);
124 slang_function_scope_destruct (&unit
->functions
);
127 unit
->assembly
= file
;
128 unit
->free_assembly
= 0;
129 unit
->global_pool
= pool
;
130 unit
->free_global_pool
= 0;
131 unit
->machine
= mach
;
132 unit
->free_machine
= 0;
133 unit
->atom_pool
= atoms
;
134 unit
->free_atom_pool
= 0;
135 slang_export_data_table_ctr (&unit
->exp_data
);
136 slang_export_code_table_ctr (&unit
->exp_code
);
140 void slang_translation_unit_destruct (slang_translation_unit
*unit
)
142 slang_variable_scope_destruct (&unit
->globals
);
143 slang_function_scope_destruct (&unit
->functions
);
144 slang_struct_scope_destruct (&unit
->structs
);
145 if (unit
->free_assembly
)
147 slang_assembly_file_destruct (unit
->assembly
);
148 slang_alloc_free (unit
->assembly
);
150 if (unit
->free_global_pool
)
151 slang_alloc_free (unit
->global_pool
);
152 if (unit
->free_machine
)
154 slang_machine_dtr (unit
->machine
);
155 slang_alloc_free (unit
->machine
);
157 if (unit
->free_atom_pool
)
159 slang_atom_pool_destruct (unit
->atom_pool
);
160 slang_alloc_free (unit
->atom_pool
);
162 slang_export_data_table_dtr (&unit
->exp_data
);
163 slang_export_code_table_ctr (&unit
->exp_code
);
168 static char *out_of_memory
= "error: out of memory\n";
170 void slang_info_log_construct (slang_info_log
*log
)
173 log
->dont_free_text
= 0;
176 void slang_info_log_destruct (slang_info_log
*log
)
178 if (!log
->dont_free_text
)
179 slang_alloc_free (log
->text
);
182 static int slang_info_log_message (slang_info_log
*log
, const char *prefix
, const char *msg
)
184 unsigned int new_size
;
186 if (log
->dont_free_text
)
188 new_size
= slang_string_length (prefix
) + 3 + slang_string_length (msg
);
189 if (log
->text
!= NULL
)
191 unsigned int text_len
= slang_string_length (log
->text
);
193 log
->text
= (char *) slang_alloc_realloc (log
->text
, text_len
+ 1, new_size
+ text_len
+ 1);
197 log
->text
= (char *) slang_alloc_malloc (new_size
+ 1);
198 if (log
->text
!= NULL
)
201 if (log
->text
== NULL
)
203 slang_string_concat (log
->text
, prefix
);
204 slang_string_concat (log
->text
, ": ");
205 slang_string_concat (log
->text
, msg
);
206 slang_string_concat (log
->text
, "\n");
210 int slang_info_log_error (slang_info_log
*log
, const char *msg
, ...)
216 _mesa_vsprintf (buf
, msg
, va
);
217 if (slang_info_log_message (log
, "error", buf
))
219 slang_info_log_memory (log
);
224 int slang_info_log_warning (slang_info_log
*log
, const char *msg
, ...)
230 _mesa_vsprintf (buf
, msg
, va
);
231 if (slang_info_log_message (log
, "warning", buf
))
233 slang_info_log_memory (log
);
238 void slang_info_log_memory (slang_info_log
*log
)
240 if (!slang_info_log_message (log
, "error", "out of memory"))
242 log
->dont_free_text
= 1;
243 log
->text
= out_of_memory
;
247 /* slang_parse_ctx */
249 typedef struct slang_parse_ctx_
255 slang_atom_pool
*atoms
;
258 /* slang_output_ctx */
260 typedef struct slang_output_ctx_
262 slang_variable_scope
*vars
;
263 slang_function_scope
*funs
;
264 slang_struct_scope
*structs
;
265 slang_assembly_file
*assembly
;
266 slang_var_pool
*global_pool
;
267 slang_machine
*machine
;
270 /* _slang_compile() */
272 static void parse_identifier_str (slang_parse_ctx
*C
, char **id
)
275 C
->I
+= _mesa_strlen (*id
) + 1;
278 static slang_atom
parse_identifier (slang_parse_ctx
*C
)
282 id
= (const char *) C
->I
;
283 C
->I
+= _mesa_strlen (id
) + 1;
284 return slang_atom_pool_atom (C
->atoms
, id
);
287 static int parse_number (slang_parse_ctx
*C
, int *number
)
289 const int radix
= (int) (*C
->I
++);
291 while (*C
->I
!= '\0')
294 if (*C
->I
>= '0' && *C
->I
<= '9')
295 digit
= (int) (*C
->I
- '0');
296 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
297 digit
= (int) (*C
->I
- 'A') + 10;
299 digit
= (int) (*C
->I
- 'a') + 10;
300 *number
= *number
* radix
+ digit
;
305 slang_info_log_warning (C
->L
, "%d: literal integer overflow", *number
);
309 static int parse_float (slang_parse_ctx
*C
, float *number
)
311 char *integral
= NULL
;
312 char *fractional
= NULL
;
313 char *exponent
= NULL
;
316 parse_identifier_str (C
, &integral
);
317 parse_identifier_str (C
, &fractional
);
318 parse_identifier_str (C
, &exponent
);
320 whole
= (char *) (slang_alloc_malloc ((_mesa_strlen (integral
) + _mesa_strlen (fractional
) +
321 _mesa_strlen (exponent
) + 3) * sizeof (char)));
324 slang_info_log_memory (C
->L
);
328 slang_string_copy (whole
, integral
);
329 slang_string_concat (whole
, ".");
330 slang_string_concat (whole
, fractional
);
331 slang_string_concat (whole
, "E");
332 slang_string_concat (whole
, exponent
);
334 *number
= (float) (_mesa_strtod(whole
, (char **)NULL
));
336 slang_alloc_free (whole
);
340 /* revision number - increment after each change affecting emitted output */
343 static int check_revision (slang_parse_ctx
*C
)
345 if (*C
->I
!= REVISION
)
347 slang_info_log_error (C
->L
, "internal compiler error");
354 static int parse_statement (slang_parse_ctx
*, slang_output_ctx
*, slang_operation
*);
355 static int parse_expression (slang_parse_ctx
*, slang_output_ctx
*, slang_operation
*);
356 static int parse_type_specifier (slang_parse_ctx
*, slang_output_ctx
*, slang_type_specifier
*);
358 static GLboolean
parse_array_len (slang_parse_ctx
*C
, slang_output_ctx
*O
, GLuint
*len
)
360 slang_operation array_size
;
361 slang_assembly_name_space space
;
364 if (!slang_operation_construct (&array_size
))
366 if (!parse_expression (C
, O
, &array_size
))
368 slang_operation_destruct (&array_size
);
372 space
.funcs
= O
->funs
;
373 space
.structs
= O
->structs
;
374 space
.vars
= O
->vars
;
375 result
= _slang_evaluate_int (O
->assembly
, O
->machine
, &space
, &array_size
, len
, C
->atoms
);
376 slang_operation_destruct (&array_size
);
380 static GLboolean
calculate_var_size (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_variable
*var
)
382 slang_storage_aggregate agg
;
384 if (!slang_storage_aggregate_construct (&agg
))
386 if (!_slang_aggregate_variable (&agg
, &var
->type
.specifier
, var
->array_len
, O
->funs
, O
->structs
,
387 O
->vars
, O
->machine
, O
->assembly
, C
->atoms
))
389 slang_storage_aggregate_destruct (&agg
);
392 var
->size
= _slang_sizeof_aggregate (&agg
);
393 slang_storage_aggregate_destruct (&agg
);
397 static GLboolean
convert_to_array (slang_parse_ctx
*C
, slang_variable
*var
,
398 const slang_type_specifier
*sp
)
400 /* sized array - mark it as array, copy the specifier to the array element and
401 * parse the expression */
402 var
->type
.specifier
.type
= slang_spec_array
;
403 var
->type
.specifier
._array
= (slang_type_specifier
*) slang_alloc_malloc (sizeof (
404 slang_type_specifier
));
405 if (var
->type
.specifier
._array
== NULL
)
407 slang_info_log_memory (C
->L
);
410 slang_type_specifier_ctr (var
->type
.specifier
._array
);
411 return slang_type_specifier_copy (var
->type
.specifier
._array
, sp
);
414 /* structure field */
417 #define FIELD_ARRAY 2
419 static GLboolean
parse_struct_field_var (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_variable
*var
,
420 const slang_type_specifier
*sp
)
422 var
->a_name
= parse_identifier (C
);
423 if (var
->a_name
== SLANG_ATOM_NULL
)
429 if (!slang_type_specifier_copy (&var
->type
.specifier
, sp
))
433 if (!convert_to_array (C
, var
, sp
))
435 if (!parse_array_len (C
, O
, &var
->array_len
))
442 return calculate_var_size (C
, O
, var
);
445 static int parse_struct_field (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_struct
*st
,
446 slang_type_specifier
*sp
)
448 slang_output_ctx o
= *O
;
450 o
.structs
= st
->structs
;
451 if (!parse_type_specifier (C
, &o
, sp
))
457 st
->fields
->variables
= (slang_variable
*) slang_alloc_realloc (st
->fields
->variables
,
458 st
->fields
->num_variables
* sizeof (slang_variable
),
459 (st
->fields
->num_variables
+ 1) * sizeof (slang_variable
));
460 if (st
->fields
->variables
== NULL
)
462 slang_info_log_memory (C
->L
);
465 var
= &st
->fields
->variables
[st
->fields
->num_variables
];
466 if (!slang_variable_construct (var
))
468 st
->fields
->num_variables
++;
469 if (!parse_struct_field_var (C
, &o
, var
, sp
))
472 while (*C
->I
++ != FIELD_NONE
);
477 static int parse_struct (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_struct
**st
)
482 /* parse struct name (if any) and make sure it is unique in current scope */
483 a_name
= parse_identifier (C
);
484 if (a_name
== SLANG_ATOM_NULL
)
486 name
= slang_atom_pool_id (C
->atoms
, a_name
);
487 if (name
[0] != '\0' && slang_struct_scope_find (O
->structs
, a_name
, 0) != NULL
)
489 slang_info_log_error (C
->L
, "%s: duplicate type name", name
);
493 /* set-up a new struct */
494 *st
= (slang_struct
*) slang_alloc_malloc (sizeof (slang_struct
));
497 slang_info_log_memory (C
->L
);
500 if (!slang_struct_construct (*st
))
502 slang_alloc_free (*st
);
504 slang_info_log_memory (C
->L
);
507 (**st
).a_name
= a_name
;
508 (**st
).structs
->outer_scope
= O
->structs
;
510 /* parse individual struct fields */
513 slang_type_specifier sp
;
515 slang_type_specifier_ctr (&sp
);
516 if (!parse_struct_field (C
, O
, *st
, &sp
))
518 slang_type_specifier_dtr (&sp
);
521 slang_type_specifier_dtr (&sp
);
523 while (*C
->I
++ != FIELD_NONE
);
525 /* if named struct, copy it to current scope */
530 O
->structs
->structs
= (slang_struct
*) slang_alloc_realloc (O
->structs
->structs
,
531 O
->structs
->num_structs
* sizeof (slang_struct
),
532 (O
->structs
->num_structs
+ 1) * sizeof (slang_struct
));
533 if (O
->structs
->structs
== NULL
)
535 slang_info_log_memory (C
->L
);
538 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
539 if (!slang_struct_construct (s
))
541 O
->structs
->num_structs
++;
542 if (!slang_struct_copy (s
, *st
))
550 #define TYPE_QUALIFIER_NONE 0
551 #define TYPE_QUALIFIER_CONST 1
552 #define TYPE_QUALIFIER_ATTRIBUTE 2
553 #define TYPE_QUALIFIER_VARYING 3
554 #define TYPE_QUALIFIER_UNIFORM 4
555 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
556 #define TYPE_QUALIFIER_FIXEDINPUT 6
558 static int parse_type_qualifier (slang_parse_ctx
*C
, slang_type_qualifier
*qual
)
562 case TYPE_QUALIFIER_NONE
:
563 *qual
= slang_qual_none
;
565 case TYPE_QUALIFIER_CONST
:
566 *qual
= slang_qual_const
;
568 case TYPE_QUALIFIER_ATTRIBUTE
:
569 *qual
= slang_qual_attribute
;
571 case TYPE_QUALIFIER_VARYING
:
572 *qual
= slang_qual_varying
;
574 case TYPE_QUALIFIER_UNIFORM
:
575 *qual
= slang_qual_uniform
;
577 case TYPE_QUALIFIER_FIXEDOUTPUT
:
578 *qual
= slang_qual_fixedoutput
;
580 case TYPE_QUALIFIER_FIXEDINPUT
:
581 *qual
= slang_qual_fixedinput
;
590 #define TYPE_SPECIFIER_VOID 0
591 #define TYPE_SPECIFIER_BOOL 1
592 #define TYPE_SPECIFIER_BVEC2 2
593 #define TYPE_SPECIFIER_BVEC3 3
594 #define TYPE_SPECIFIER_BVEC4 4
595 #define TYPE_SPECIFIER_INT 5
596 #define TYPE_SPECIFIER_IVEC2 6
597 #define TYPE_SPECIFIER_IVEC3 7
598 #define TYPE_SPECIFIER_IVEC4 8
599 #define TYPE_SPECIFIER_FLOAT 9
600 #define TYPE_SPECIFIER_VEC2 10
601 #define TYPE_SPECIFIER_VEC3 11
602 #define TYPE_SPECIFIER_VEC4 12
603 #define TYPE_SPECIFIER_MAT2 13
604 #define TYPE_SPECIFIER_MAT3 14
605 #define TYPE_SPECIFIER_MAT4 15
606 #define TYPE_SPECIFIER_SAMPLER1D 16
607 #define TYPE_SPECIFIER_SAMPLER2D 17
608 #define TYPE_SPECIFIER_SAMPLER3D 18
609 #define TYPE_SPECIFIER_SAMPLERCUBE 19
610 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
611 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
612 #define TYPE_SPECIFIER_STRUCT 22
613 #define TYPE_SPECIFIER_TYPENAME 23
615 static int parse_type_specifier (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_type_specifier
*spec
)
619 case TYPE_SPECIFIER_VOID
:
620 spec
->type
= slang_spec_void
;
622 case TYPE_SPECIFIER_BOOL
:
623 spec
->type
= slang_spec_bool
;
625 case TYPE_SPECIFIER_BVEC2
:
626 spec
->type
= slang_spec_bvec2
;
628 case TYPE_SPECIFIER_BVEC3
:
629 spec
->type
= slang_spec_bvec3
;
631 case TYPE_SPECIFIER_BVEC4
:
632 spec
->type
= slang_spec_bvec4
;
634 case TYPE_SPECIFIER_INT
:
635 spec
->type
= slang_spec_int
;
637 case TYPE_SPECIFIER_IVEC2
:
638 spec
->type
= slang_spec_ivec2
;
640 case TYPE_SPECIFIER_IVEC3
:
641 spec
->type
= slang_spec_ivec3
;
643 case TYPE_SPECIFIER_IVEC4
:
644 spec
->type
= slang_spec_ivec4
;
646 case TYPE_SPECIFIER_FLOAT
:
647 spec
->type
= slang_spec_float
;
649 case TYPE_SPECIFIER_VEC2
:
650 spec
->type
= slang_spec_vec2
;
652 case TYPE_SPECIFIER_VEC3
:
653 spec
->type
= slang_spec_vec3
;
655 case TYPE_SPECIFIER_VEC4
:
656 spec
->type
= slang_spec_vec4
;
658 case TYPE_SPECIFIER_MAT2
:
659 spec
->type
= slang_spec_mat2
;
661 case TYPE_SPECIFIER_MAT3
:
662 spec
->type
= slang_spec_mat3
;
664 case TYPE_SPECIFIER_MAT4
:
665 spec
->type
= slang_spec_mat4
;
667 case TYPE_SPECIFIER_SAMPLER1D
:
668 spec
->type
= slang_spec_sampler1D
;
670 case TYPE_SPECIFIER_SAMPLER2D
:
671 spec
->type
= slang_spec_sampler2D
;
673 case TYPE_SPECIFIER_SAMPLER3D
:
674 spec
->type
= slang_spec_sampler3D
;
676 case TYPE_SPECIFIER_SAMPLERCUBE
:
677 spec
->type
= slang_spec_samplerCube
;
679 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
680 spec
->type
= slang_spec_sampler1DShadow
;
682 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
683 spec
->type
= slang_spec_sampler2DShadow
;
685 case TYPE_SPECIFIER_STRUCT
:
686 spec
->type
= slang_spec_struct
;
687 if (!parse_struct (C
, O
, &spec
->_struct
))
690 case TYPE_SPECIFIER_TYPENAME
:
691 spec
->type
= slang_spec_struct
;
696 a_name
= parse_identifier (C
);
700 stru
= slang_struct_scope_find (O
->structs
, a_name
, 1);
703 slang_info_log_error (C
->L
, "%s: undeclared type name",
704 slang_atom_pool_id (C
->atoms
, a_name
));
708 spec
->_struct
= (slang_struct
*) slang_alloc_malloc (sizeof (slang_struct
));
709 if (spec
->_struct
== NULL
)
711 slang_info_log_memory (C
->L
);
714 if (!slang_struct_construct (spec
->_struct
))
716 slang_alloc_free (spec
->_struct
);
717 spec
->_struct
= NULL
;
720 if (!slang_struct_copy (spec
->_struct
, stru
))
730 static int parse_fully_specified_type (slang_parse_ctx
*C
, slang_output_ctx
*O
,
731 slang_fully_specified_type
*type
)
733 if (!parse_type_qualifier (C
, &type
->qualifier
))
735 return parse_type_specifier (C
, O
, &type
->specifier
);
740 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
741 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
745 #define OP_CONTINUE 6
748 #define OP_EXPRESSION 9
753 #define OP_PUSH_VOID 14
754 #define OP_PUSH_BOOL 15
755 #define OP_PUSH_INT 16
756 #define OP_PUSH_FLOAT 17
757 #define OP_PUSH_IDENTIFIER 18
758 #define OP_SEQUENCE 19
760 #define OP_ADDASSIGN 21
761 #define OP_SUBASSIGN 22
762 #define OP_MULASSIGN 23
763 #define OP_DIVASSIGN 24
764 /*#define OP_MODASSIGN 25*/
765 /*#define OP_LSHASSIGN 26*/
766 /*#define OP_RSHASSIGN 27*/
767 /*#define OP_ORASSIGN 28*/
768 /*#define OP_XORASSIGN 29*/
769 /*#define OP_ANDASSIGN 30*/
771 #define OP_LOGICALOR 32
772 #define OP_LOGICALXOR 33
773 #define OP_LOGICALAND 34
774 /*#define OP_BITOR 35*/
775 /*#define OP_BITXOR 36*/
776 /*#define OP_BITAND 37*/
778 #define OP_NOTEQUAL 39
780 #define OP_GREATER 41
781 #define OP_LESSEQUAL 42
782 #define OP_GREATEREQUAL 43
783 /*#define OP_LSHIFT 44*/
784 /*#define OP_RSHIFT 45*/
786 #define OP_SUBTRACT 47
787 #define OP_MULTIPLY 48
789 /*#define OP_MODULUS 50*/
790 #define OP_PREINCREMENT 51
791 #define OP_PREDECREMENT 52
794 /*#define OP_COMPLEMENT 55*/
796 #define OP_SUBSCRIPT 57
799 #define OP_POSTINCREMENT 60
800 #define OP_POSTDECREMENT 61
802 static int parse_child_operation (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_operation
*oper
,
807 oper
->children
= (slang_operation
*) slang_alloc_realloc (oper
->children
,
808 oper
->num_children
* sizeof (slang_operation
),
809 (oper
->num_children
+ 1) * sizeof (slang_operation
));
810 if (oper
->children
== NULL
)
812 slang_info_log_memory (C
->L
);
815 ch
= &oper
->children
[oper
->num_children
];
816 if (!slang_operation_construct (ch
))
818 slang_info_log_memory (C
->L
);
821 oper
->num_children
++;
823 return parse_statement (C
, O
, ch
);
824 return parse_expression (C
, O
, ch
);
827 static int parse_declaration (slang_parse_ctx
*C
, slang_output_ctx
*O
);
829 static int parse_statement (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_operation
*oper
)
831 oper
->locals
->outer_scope
= O
->vars
;
834 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
835 /* parse child statements, do not create new variable scope */
836 oper
->type
= slang_oper_block_no_new_scope
;
837 while (*C
->I
!= OP_END
)
838 if (!parse_child_operation (C
, O
, oper
, 1))
842 case OP_BLOCK_BEGIN_NEW_SCOPE
:
843 /* parse child statements, create new variable scope */
845 slang_output_ctx o
= *O
;
847 oper
->type
= slang_oper_block_new_scope
;
848 o
.vars
= oper
->locals
;
849 while (*C
->I
!= OP_END
)
850 if (!parse_child_operation (C
, &o
, oper
, 1))
856 /* local variable declaration, individual declarators are stored as children identifiers */
857 oper
->type
= slang_oper_variable_decl
;
859 const unsigned int first_var
= O
->vars
->num_variables
;
861 /* parse the declaration, note that there can be zero or more than one declarators */
862 if (!parse_declaration (C
, O
))
864 if (first_var
< O
->vars
->num_variables
)
866 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
869 oper
->children
= (slang_operation
*) slang_alloc_malloc (num_vars
* sizeof (
871 if (oper
->children
== NULL
)
873 slang_info_log_memory (C
->L
);
876 for (oper
->num_children
= 0; oper
->num_children
< num_vars
; oper
->num_children
++)
877 if (!slang_operation_construct (&oper
->children
[oper
->num_children
]))
879 slang_info_log_memory (C
->L
);
882 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++)
884 slang_operation
*o
= &oper
->children
[i
- first_var
];
886 o
->type
= slang_oper_identifier
;
887 o
->locals
->outer_scope
= O
->vars
;
888 o
->a_id
= O
->vars
->variables
[i
].a_name
;
894 /* the __asm statement, parse the mnemonic and all its arguments as expressions */
895 oper
->type
= slang_oper_asm
;
896 oper
->a_id
= parse_identifier (C
);
897 if (oper
->a_id
== SLANG_ATOM_NULL
)
899 while (*C
->I
!= OP_END
)
900 if (!parse_child_operation (C
, O
, oper
, 0))
905 oper
->type
= slang_oper_break
;
908 oper
->type
= slang_oper_continue
;
911 oper
->type
= slang_oper_discard
;
914 oper
->type
= slang_oper_return
;
915 if (!parse_child_operation (C
, O
, oper
, 0))
919 oper
->type
= slang_oper_expression
;
920 if (!parse_child_operation (C
, O
, oper
, 0))
924 oper
->type
= slang_oper_if
;
925 if (!parse_child_operation (C
, O
, oper
, 0))
927 if (!parse_child_operation (C
, O
, oper
, 1))
929 if (!parse_child_operation (C
, O
, oper
, 1))
934 slang_output_ctx o
= *O
;
936 oper
->type
= slang_oper_while
;
937 o
.vars
= oper
->locals
;
938 if (!parse_child_operation (C
, &o
, oper
, 1))
940 if (!parse_child_operation (C
, &o
, oper
, 1))
945 oper
->type
= slang_oper_do
;
946 if (!parse_child_operation (C
, O
, oper
, 1))
948 if (!parse_child_operation (C
, O
, oper
, 0))
953 slang_output_ctx o
= *O
;
955 oper
->type
= slang_oper_for
;
956 o
.vars
= oper
->locals
;
957 if (!parse_child_operation (C
, &o
, oper
, 1))
959 if (!parse_child_operation (C
, &o
, oper
, 1))
961 if (!parse_child_operation (C
, &o
, oper
, 0))
963 if (!parse_child_operation (C
, &o
, oper
, 1))
973 static int handle_nary_expression (slang_parse_ctx
*C
, slang_operation
*op
, slang_operation
**ops
,
974 unsigned int *total_ops
, unsigned int n
)
978 op
->children
= (slang_operation
*) slang_alloc_malloc (n
* sizeof (slang_operation
));
979 if (op
->children
== NULL
)
981 slang_info_log_memory (C
->L
);
984 op
->num_children
= n
;
986 for (i
= 0; i
< n
; i
++)
987 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
988 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
991 *ops
= (slang_operation
*) slang_alloc_realloc (*ops
, (*total_ops
+ n
) * sizeof (slang_operation
),
992 *total_ops
* sizeof (slang_operation
));
995 slang_info_log_memory (C
->L
);
1001 static int is_constructor_name (const char *name
, slang_atom a_name
, 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
;
1008 static int parse_expression (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_operation
*oper
)
1010 slang_operation
*ops
= NULL
;
1011 unsigned int num_ops
= 0;
1014 while (*C
->I
!= OP_END
)
1016 slang_operation
*op
;
1017 const unsigned int op_code
= *C
->I
++;
1019 /* allocate default operation, becomes a no-op if not used */
1020 ops
= (slang_operation
*) slang_alloc_realloc (ops
,
1021 num_ops
* sizeof (slang_operation
), (num_ops
+ 1) * sizeof (slang_operation
));
1024 slang_info_log_memory (C
->L
);
1028 if (!slang_operation_construct (op
))
1030 slang_info_log_memory (C
->L
);
1034 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))
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))
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))
1216 if (!C
->parsing_builtin
&& !slang_function_scope_find_by_name (O
->funs
, op
->a_id
, 1))
1220 id
= slang_atom_pool_id (C
->atoms
, op
->a_id
);
1221 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
);
1257 /* parameter qualifier */
1258 #define PARAM_QUALIFIER_IN 0
1259 #define PARAM_QUALIFIER_OUT 1
1260 #define PARAM_QUALIFIER_INOUT 2
1262 /* function parameter array presence */
1263 #define PARAMETER_ARRAY_NOT_PRESENT 0
1264 #define PARAMETER_ARRAY_PRESENT 1
1266 static int parse_parameter_declaration (slang_parse_ctx
*C
, slang_output_ctx
*O
,
1267 slang_variable
*param
)
1269 /* parse and validate the parameter's type qualifiers (there can be two at most) because
1270 * not all combinations are valid */
1271 if (!parse_type_qualifier (C
, ¶m
->type
.qualifier
))
1275 case PARAM_QUALIFIER_IN
:
1276 if (param
->type
.qualifier
!= slang_qual_const
&& param
->type
.qualifier
!= slang_qual_none
)
1278 slang_info_log_error (C
->L
, "invalid type qualifier");
1282 case PARAM_QUALIFIER_OUT
:
1283 if (param
->type
.qualifier
== slang_qual_none
)
1284 param
->type
.qualifier
= slang_qual_out
;
1287 slang_info_log_error (C
->L
, "invalid type qualifier");
1291 case PARAM_QUALIFIER_INOUT
:
1292 if (param
->type
.qualifier
== slang_qual_none
)
1293 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 explicitly defined */
1312 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
)
1314 slang_type_specifier p
;
1316 slang_type_specifier_ctr (&p
);
1317 if (!slang_type_specifier_copy (&p
, ¶m
->type
.specifier
))
1319 slang_type_specifier_dtr (&p
);
1322 if (!convert_to_array (C
, param
, &p
))
1324 slang_type_specifier_dtr (&p
);
1327 slang_type_specifier_dtr (&p
);
1328 if (!parse_array_len (C
, O
, ¶m
->array_len
))
1332 /* calculate the parameter size */
1333 if (!calculate_var_size (C
, O
, param
))
1336 /* TODO: allocate the local address here? */
1341 #define FUNCTION_ORDINARY 0
1342 #define FUNCTION_CONSTRUCTOR 1
1343 #define FUNCTION_OPERATOR 2
1345 /* function parameter */
1346 #define PARAMETER_NONE 0
1347 #define PARAMETER_NEXT 1
1350 #define OPERATOR_ADDASSIGN 1
1351 #define OPERATOR_SUBASSIGN 2
1352 #define OPERATOR_MULASSIGN 3
1353 #define OPERATOR_DIVASSIGN 4
1354 /*#define OPERATOR_MODASSIGN 5*/
1355 /*#define OPERATOR_LSHASSIGN 6*/
1356 /*#define OPERATOR_RSHASSIGN 7*/
1357 /*#define OPERATOR_ANDASSIGN 8*/
1358 /*#define OPERATOR_XORASSIGN 9*/
1359 /*#define OPERATOR_ORASSIGN 10*/
1360 #define OPERATOR_LOGICALXOR 11
1361 /*#define OPERATOR_BITOR 12*/
1362 /*#define OPERATOR_BITXOR 13*/
1363 /*#define OPERATOR_BITAND 14*/
1364 #define OPERATOR_LESS 15
1365 #define OPERATOR_GREATER 16
1366 #define OPERATOR_LESSEQUAL 17
1367 #define OPERATOR_GREATEREQUAL 18
1368 /*#define OPERATOR_LSHIFT 19*/
1369 /*#define OPERATOR_RSHIFT 20*/
1370 #define OPERATOR_MULTIPLY 21
1371 #define OPERATOR_DIVIDE 22
1372 /*#define OPERATOR_MODULUS 23*/
1373 #define OPERATOR_INCREMENT 24
1374 #define OPERATOR_DECREMENT 25
1375 #define OPERATOR_PLUS 26
1376 #define OPERATOR_MINUS 27
1377 /*#define OPERATOR_COMPLEMENT 28*/
1378 #define OPERATOR_NOT 29
1380 static const struct {
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, "^" }*/
1415 static slang_atom
parse_operator_name (slang_parse_ctx
*C
)
1419 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 atom
= slang_atom_pool_atom (C
->atoms
, operator_names
[i
].o_name
);
1424 if (atom
== SLANG_ATOM_NULL
)
1426 slang_info_log_memory (C
->L
);
1436 static int parse_function_prototype (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_function
*func
)
1438 /* parse function type and name */
1439 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
= slang_atom_pool_atom (C
->atoms
,
1454 slang_type_specifier_type_to_string (func
->header
.type
.specifier
.type
));
1455 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1457 slang_info_log_memory (C
->L
);
1461 case FUNCTION_OPERATOR
:
1462 func
->kind
= slang_func_operator
;
1463 func
->header
.a_name
= parse_operator_name (C
);
1464 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1471 /* parse function parameters */
1472 while (*C
->I
++ == PARAMETER_NEXT
)
1476 func
->parameters
->variables
= (slang_variable
*) slang_alloc_realloc (
1477 func
->parameters
->variables
,
1478 func
->parameters
->num_variables
* sizeof (slang_variable
),
1479 (func
->parameters
->num_variables
+ 1) * sizeof (slang_variable
));
1480 if (func
->parameters
->variables
== NULL
)
1482 slang_info_log_memory (C
->L
);
1485 p
= &func
->parameters
->variables
[func
->parameters
->num_variables
];
1486 if (!slang_variable_construct (p
))
1488 func
->parameters
->num_variables
++;
1489 if (!parse_parameter_declaration (C
, O
, p
))
1493 /* function formal parameters and local variables share the same scope, so save
1494 * the information about param count in a seperate place
1495 * also link the scope to the global variable scope so when a given identifier is not
1496 * found here, the search process continues in the global space */
1497 func
->param_count
= func
->parameters
->num_variables
;
1498 func
->parameters
->outer_scope
= O
->vars
;
1502 static int parse_function_definition (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_function
*func
)
1504 slang_output_ctx o
= *O
;
1506 if (!parse_function_prototype (C
, O
, func
))
1509 /* create function's body operation */
1510 func
->body
= (slang_operation
*) slang_alloc_malloc (sizeof (slang_operation
));
1511 if (func
->body
== NULL
)
1513 slang_info_log_memory (C
->L
);
1516 if (!slang_operation_construct (func
->body
))
1518 slang_alloc_free (func
->body
);
1520 slang_info_log_memory (C
->L
);
1524 /* to parse the body the parse context is modified in order to capture parsed variables
1525 * into function's local variable scope */
1526 C
->global_scope
= 0;
1527 o
.vars
= func
->parameters
;
1528 if (!parse_statement (C
, &o
, func
->body
))
1530 C
->global_scope
= 1;
1534 static GLboolean
initialize_global (slang_assemble_ctx
*A
, slang_variable
*var
)
1536 slang_assembly_file_restore_point point
;
1538 slang_assembly_local_info save_local
= A
->local
;
1539 slang_operation op_id
, op_assign
;
1542 /* save the current assembly */
1543 if (!slang_assembly_file_restore_point_save (A
->file
, &point
))
1546 /* setup the machine */
1548 mach
.ip
= A
->file
->count
;
1550 /* allocate local storage for expression */
1551 A
->local
.ret_size
= 0;
1552 A
->local
.addr_tmp
= 0;
1553 A
->local
.swizzle_tmp
= 4;
1554 if (!slang_assembly_file_push_label (A
->file
, slang_asm_local_alloc
, 20))
1556 if (!slang_assembly_file_push_label (A
->file
, slang_asm_enter
, 20))
1559 /* construct the left side of assignment */
1560 if (!slang_operation_construct (&op_id
))
1562 op_id
.type
= slang_oper_identifier
;
1563 op_id
.a_id
= var
->a_name
;
1565 /* put the variable into operation's scope */
1566 op_id
.locals
->variables
= (slang_variable
*) slang_alloc_malloc (sizeof (slang_variable
));
1567 if (op_id
.locals
->variables
== NULL
)
1569 slang_operation_destruct (&op_id
);
1572 op_id
.locals
->num_variables
= 1;
1573 op_id
.locals
->variables
[0] = *var
;
1575 /* construct the assignment expression */
1576 if (!slang_operation_construct (&op_assign
))
1578 op_id
.locals
->num_variables
= 0;
1579 slang_operation_destruct (&op_id
);
1582 op_assign
.type
= slang_oper_assign
;
1583 op_assign
.children
= (slang_operation
*) slang_alloc_malloc (2 * sizeof (slang_operation
));
1584 if (op_assign
.children
== NULL
)
1586 slang_operation_destruct (&op_assign
);
1587 op_id
.locals
->num_variables
= 0;
1588 slang_operation_destruct (&op_id
);
1591 op_assign
.num_children
= 2;
1592 op_assign
.children
[0] = op_id
;
1593 op_assign
.children
[1] = *var
->initializer
;
1595 /* insert the actual expression */
1596 result
= _slang_assemble_operation (A
, &op_assign
, slang_ref_forbid
);
1598 /* carefully destroy the operations */
1599 op_assign
.num_children
= 0;
1600 slang_alloc_free (op_assign
.children
);
1601 op_assign
.children
= NULL
;
1602 slang_operation_destruct (&op_assign
);
1603 op_id
.locals
->num_variables
= 0;
1604 slang_operation_destruct (&op_id
);
1608 if (!slang_assembly_file_push (A
->file
, slang_asm_exit
))
1611 /* execute the expression */
1612 if (!_slang_execute2 (A
->file
, &mach
))
1615 /* restore the old assembly */
1616 if (!slang_assembly_file_restore_point_load (A
->file
, &point
))
1618 A
->local
= save_local
;
1620 /* now we copy the contents of the initialized variable back to the original machine */
1621 _mesa_memcpy ((GLubyte
*) A
->mach
->mem
+ var
->address
, (GLubyte
*) mach
.mem
+ var
->address
,
1627 /* init declarator list */
1628 #define DECLARATOR_NONE 0
1629 #define DECLARATOR_NEXT 1
1631 /* variable declaration */
1632 #define VARIABLE_NONE 0
1633 #define VARIABLE_IDENTIFIER 1
1634 #define VARIABLE_INITIALIZER 2
1635 #define VARIABLE_ARRAY_EXPLICIT 3
1636 #define VARIABLE_ARRAY_UNKNOWN 4
1638 static int 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 O
->vars
->variables
= (slang_variable
*) slang_alloc_realloc (O
->vars
->variables
,
1649 O
->vars
->num_variables
* sizeof (slang_variable
),
1650 (O
->vars
->num_variables
+ 1) * sizeof (slang_variable
));
1651 if (O
->vars
->variables
== NULL
)
1653 slang_info_log_memory (C
->L
);
1656 var
= &O
->vars
->variables
[O
->vars
->num_variables
];
1657 if (!slang_variable_construct (var
))
1659 O
->vars
->num_variables
++;
1661 /* copy the declarator qualifier type, parse the identifier */
1662 var
->global
= C
->global_scope
;
1663 var
->type
.qualifier
= type
->qualifier
;
1664 var
->a_name
= parse_identifier (C
);
1665 if (var
->a_name
== SLANG_ATOM_NULL
)
1671 /* simple variable declarator - just copy the specifier */
1672 if (!slang_type_specifier_copy (&var
->type
.specifier
, &type
->specifier
))
1675 case VARIABLE_INITIALIZER
:
1676 /* initialized variable - copy the specifier and parse the expression */
1677 if (!slang_type_specifier_copy (&var
->type
.specifier
, &type
->specifier
))
1679 var
->initializer
= (slang_operation
*) slang_alloc_malloc (sizeof (slang_operation
));
1680 if (var
->initializer
== NULL
)
1682 slang_info_log_memory (C
->L
);
1685 if (!slang_operation_construct (var
->initializer
))
1687 slang_alloc_free (var
->initializer
);
1688 var
->initializer
= NULL
;
1689 slang_info_log_memory (C
->L
);
1692 if (!parse_expression (C
, O
, var
->initializer
))
1696 case VARIABLE_ARRAY_UNKNOWN
:
1697 /* unsized array - mark it as array and copy the specifier to the array element */
1698 if (!convert_to_array (C
, var
, &type
->specifier
))
1702 case VARIABLE_ARRAY_EXPLICIT
:
1703 if (!convert_to_array (C
, var
, &type
->specifier
))
1705 if (!parse_array_len (C
, O
, &var
->array_len
))
1712 /* allocate global address space for a variable with a known size */
1713 if (C
->global_scope
&& !(var
->type
.specifier
.type
== slang_spec_array
&& var
->array_len
== 0))
1715 if (!calculate_var_size (C
, O
, var
))
1717 var
->address
= slang_var_pool_alloc (O
->global_pool
, var
->size
);
1720 /* initialize global variable */
1721 if (C
->global_scope
&& var
->initializer
!= NULL
)
1723 slang_assemble_ctx A
;
1725 A
.file
= O
->assembly
;
1726 A
.mach
= O
->machine
;
1728 A
.space
.funcs
= O
->funs
;
1729 A
.space
.structs
= O
->structs
;
1730 A
.space
.vars
= O
->vars
;
1731 if (!initialize_global (&A
, var
))
1737 static int parse_init_declarator_list (slang_parse_ctx
*C
, slang_output_ctx
*O
)
1739 slang_fully_specified_type type
;
1741 /* parse the fully specified type, common to all declarators */
1742 if (!slang_fully_specified_type_construct (&type
))
1744 if (!parse_fully_specified_type (C
, O
, &type
))
1746 slang_fully_specified_type_destruct (&type
);
1750 /* parse declarators, pass-in the parsed type */
1753 if (!parse_init_declarator (C
, O
, &type
))
1755 slang_fully_specified_type_destruct (&type
);
1759 while (*C
->I
++ == DECLARATOR_NEXT
);
1761 slang_fully_specified_type_destruct (&type
);
1765 static int parse_function (slang_parse_ctx
*C
, slang_output_ctx
*O
, int definition
,
1766 slang_function
**parsed_func_ret
)
1768 slang_function parsed_func
, *found_func
;
1770 /* parse function definition/declaration */
1771 if (!slang_function_construct (&parsed_func
))
1775 if (!parse_function_definition (C
, O
, &parsed_func
))
1777 slang_function_destruct (&parsed_func
);
1783 if (!parse_function_prototype (C
, O
, &parsed_func
))
1785 slang_function_destruct (&parsed_func
);
1790 /* find a function with a prototype matching the parsed one - only the current scope
1791 * is being searched to allow built-in function overriding */
1792 found_func
= slang_function_scope_find (O
->funs
, &parsed_func
, 0);
1793 if (found_func
== NULL
)
1795 /* add the parsed function to the function list */
1796 O
->funs
->functions
= (slang_function
*) slang_alloc_realloc (O
->funs
->functions
,
1797 O
->funs
->num_functions
* sizeof (slang_function
),
1798 (O
->funs
->num_functions
+ 1) * sizeof (slang_function
));
1799 if (O
->funs
->functions
== NULL
)
1801 slang_info_log_memory (C
->L
);
1802 slang_function_destruct (&parsed_func
);
1805 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
1806 O
->funs
->num_functions
++;
1808 /* return the newly parsed function */
1809 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
1813 /* TODO: check function return type qualifiers and specifiers */
1816 if (found_func
->body
!= NULL
)
1818 slang_info_log_error (C
->L
, "%s: function already has a body",
1819 slang_atom_pool_id (C
->atoms
, parsed_func
.header
.a_name
));
1820 slang_function_destruct (&parsed_func
);
1824 /* destroy the existing function declaration and replace it with the new one,
1825 * remember to save the fixup table */
1826 parsed_func
.fixups
= found_func
->fixups
;
1827 slang_fixup_table_init (&found_func
->fixups
);
1828 slang_function_destruct (found_func
);
1829 *found_func
= parsed_func
;
1833 /* another declaration of the same function prototype - ignore it */
1834 slang_function_destruct (&parsed_func
);
1837 /* return the found function */
1838 *parsed_func_ret
= found_func
;
1841 /* assemble the parsed function */
1843 slang_assemble_ctx A
;
1845 A
.file
= O
->assembly
;
1846 A
.mach
= O
->machine
;
1848 A
.space
.funcs
= O
->funs
;
1849 A
.space
.structs
= O
->structs
;
1850 A
.space
.vars
= O
->vars
;
1851 if (!_slang_assemble_function (&A
, *parsed_func_ret
))
1858 #define DECLARATION_FUNCTION_PROTOTYPE 1
1859 #define DECLARATION_INIT_DECLARATOR_LIST 2
1861 static int parse_declaration (slang_parse_ctx
*C
, slang_output_ctx
*O
)
1865 case DECLARATION_INIT_DECLARATOR_LIST
:
1866 if (!parse_init_declarator_list (C
, O
))
1869 case DECLARATION_FUNCTION_PROTOTYPE
:
1871 slang_function
*dummy_func
;
1873 if (!parse_function (C
, O
, 0, &dummy_func
))
1883 /* external declaration */
1884 #define EXTERNAL_NULL 0
1885 #define EXTERNAL_FUNCTION_DEFINITION 1
1886 #define EXTERNAL_DECLARATION 2
1888 static int parse_translation_unit (slang_parse_ctx
*C
, slang_translation_unit
*unit
)
1892 /* setup output context */
1893 o
.funs
= &unit
->functions
;
1894 o
.structs
= &unit
->structs
;
1895 o
.vars
= &unit
->globals
;
1896 o
.assembly
= unit
->assembly
;
1897 o
.global_pool
= unit
->global_pool
;
1898 o
.machine
= unit
->machine
;
1900 /* parse individual functions and declarations */
1901 while (*C
->I
!= EXTERNAL_NULL
)
1905 case EXTERNAL_FUNCTION_DEFINITION
:
1907 slang_function
*func
;
1909 if (!parse_function (C
, &o
, 1, &func
))
1913 case EXTERNAL_DECLARATION
:
1914 if (!parse_declaration (C
, &o
))
1925 #define BUILTIN_CORE 0
1926 #define BUILTIN_COMMON 1
1927 #define BUILTIN_TARGET 2
1928 #define BUILTIN_TOTAL 3
1930 static int compile_binary (const byte
*prod
, slang_translation_unit
*unit
, slang_unit_type type
,
1931 slang_info_log
*log
, slang_translation_unit
*builtins
, slang_assembly_file
*file
,
1932 slang_var_pool
*pool
, slang_machine
*mach
, slang_translation_unit
*downlink
,
1933 slang_atom_pool
*atoms
)
1937 /* create translation unit object */
1940 if (!slang_translation_unit_construct2 (unit
, file
, pool
, mach
, atoms
))
1945 /* set-up parse context */
1948 C
.parsing_builtin
= builtins
== NULL
;
1950 C
.atoms
= unit
->atom_pool
;
1952 if (!check_revision (&C
))
1954 slang_translation_unit_destruct (unit
);
1958 if (downlink
!= NULL
)
1960 unit
->functions
.outer_scope
= &downlink
->functions
;
1961 unit
->globals
.outer_scope
= &downlink
->globals
;
1962 unit
->structs
.outer_scope
= &downlink
->structs
;
1965 /* parse translation unit */
1966 if (!parse_translation_unit (&C
, unit
))
1968 slang_translation_unit_destruct (unit
);
1975 static int compile_with_grammar (grammar id
, const char *source
, slang_translation_unit
*unit
,
1976 slang_unit_type type
, slang_info_log
*log
, slang_translation_unit
*builtins
)
1979 unsigned int size
, start
, version
;
1981 /* retrieve version */
1982 if (!_slang_preprocess_version (source
, &version
, &start
, log
))
1985 /* check the syntax and generate its binary representation */
1986 if (!grammar_fast_check (id
, (const byte
*) source
+ start
, &prod
, &size
, 65536))
1990 grammar_get_last_error ( (unsigned char*) buf
, 1024, (int*) &pos
);
1991 slang_info_log_error (log
, buf
);
1995 /* syntax is okay - translate it to internal representation */
1996 if (!compile_binary (prod
, unit
, type
, log
, builtins
, NULL
, NULL
, NULL
,
1997 &builtins
[BUILTIN_TARGET
], NULL
))
1999 grammar_alloc_free (prod
);
2003 grammar_alloc_free (prod
);
2007 static const char *slang_shader_syn
=
2008 #include "library/slang_shader_syn.h"
2011 static const byte slang_core_gc
[] = {
2012 #include "library/slang_core_gc.h"
2015 static const byte slang_common_builtin_gc
[] = {
2016 #include "library/slang_common_builtin_gc.h"
2019 static const byte slang_fragment_builtin_gc
[] = {
2020 #include "library/slang_fragment_builtin_gc.h"
2023 static const byte slang_vertex_builtin_gc
[] = {
2024 #include "library/slang_vertex_builtin_gc.h"
2027 static int compile (grammar
*id
, slang_translation_unit
*builtin_units
, int *compiled
,
2028 const char *source
, slang_translation_unit
*unit
, slang_unit_type type
, slang_info_log
*log
)
2030 slang_translation_unit
*builtins
= NULL
;
2032 /* load slang grammar */
2033 *id
= grammar_load_from_text ((const byte
*) (slang_shader_syn
));
2039 grammar_get_last_error (buf
, 1024, &pos
);
2040 slang_info_log_error (log
, (const char *) (buf
));
2044 /* set shader type - the syntax is slightly different for different shaders */
2045 if (type
== slang_unit_fragment_shader
|| type
== slang_unit_fragment_builtin
)
2046 grammar_set_reg8 (*id
, (const byte
*) "shader_type", 1);
2048 grammar_set_reg8 (*id
, (const byte
*) "shader_type", 2);
2050 /* enable language extensions */
2051 grammar_set_reg8 (*id
, (const byte
*) "parsing_builtin", 1);
2053 /* if parsing user-specified shader, load built-in library */
2054 if (type
== slang_unit_fragment_shader
|| type
== slang_unit_vertex_shader
)
2056 /* compile core functionality first */
2057 if (!compile_binary (slang_core_gc
, &builtin_units
[BUILTIN_CORE
],
2058 slang_unit_fragment_builtin
, log
, NULL
, unit
->assembly
, unit
->global_pool
,
2059 unit
->machine
, NULL
, unit
->atom_pool
))
2061 compiled
[BUILTIN_CORE
] = 1;
2063 /* compile common functions and variables, link to core */
2064 if (!compile_binary (slang_common_builtin_gc
, &builtin_units
[BUILTIN_COMMON
],
2065 slang_unit_fragment_builtin
, log
, NULL
, unit
->assembly
, unit
->global_pool
,
2066 unit
->machine
, &builtin_units
[BUILTIN_CORE
], unit
->atom_pool
))
2068 compiled
[BUILTIN_COMMON
] = 1;
2070 /* compile target-specific functions and variables, link to common */
2071 if (type
== slang_unit_fragment_shader
)
2073 if (!compile_binary (slang_fragment_builtin_gc
, &builtin_units
[BUILTIN_TARGET
],
2074 slang_unit_fragment_builtin
, log
, NULL
, unit
->assembly
, unit
->global_pool
,
2075 unit
->machine
, &builtin_units
[BUILTIN_COMMON
], unit
->atom_pool
))
2078 else if (type
== slang_unit_vertex_shader
)
2080 if (!compile_binary (slang_vertex_builtin_gc
, &builtin_units
[BUILTIN_TARGET
],
2081 slang_unit_vertex_builtin
, log
, NULL
, unit
->assembly
, unit
->global_pool
,
2082 unit
->machine
, &builtin_units
[BUILTIN_COMMON
], unit
->atom_pool
))
2085 compiled
[BUILTIN_TARGET
] = 1;
2087 /* disable language extensions */
2088 grammar_set_reg8 (*id
, (const byte
*) "parsing_builtin", 0);
2089 builtins
= builtin_units
;
2092 /* compile the actual shader - pass-in built-in library for external shader */
2093 if (!compile_with_grammar (*id
, source
, unit
, type
, log
, builtins
))
2099 #if defined(USE_X86_ASM) || defined(SLANG_X86)
2101 GLboolean
_slang_x86_codegen (slang_machine
*, slang_assembly_file
*, GLuint
);
2104 int _slang_compile (const char *source
, slang_translation_unit
*unit
, slang_unit_type type
,
2105 slang_info_log
*log
)
2109 /* slang_translation_unit builtin_units[BUILTIN_TOTAL];*/
2110 slang_translation_unit
*builtin_units
;
2111 int compiled
[BUILTIN_TOTAL
] = { 0 };
2113 /* create the main unit first */
2114 if (!slang_translation_unit_construct (unit
))
2118 builtin_units
= (slang_translation_unit
*) slang_alloc_malloc (BUILTIN_TOTAL
* sizeof (slang_translation_unit
));
2119 success
= compile (&id
, builtin_units
, compiled
, source
, unit
, type
, log
);
2121 /* destroy built-in library */
2122 /* XXX: free with the unit */
2123 /*if (type == slang_unit_fragment_shader || type == slang_unit_vertex_shader)
2127 for (i = 0; i < BUILTIN_TOTAL; i++)
2128 if (compiled[i] != 0)
2129 slang_translation_unit_destruct (&builtin_units[i]);
2132 grammar_destroy (id
);
2137 unit
->exp_data
.atoms
= unit
->atom_pool
;
2138 if (!_slang_build_export_data_table (&unit
->exp_data
, &unit
->globals
))
2141 unit
->exp_code
.atoms
= unit
->atom_pool
;
2142 if (!_slang_build_export_code_table (&unit
->exp_code
, &unit
->functions
, unit
))
2145 #if defined(USE_X86_ASM) || defined(SLANG_X86)
2146 /* XXX: lookup the @main label */
2147 if (!_slang_x86_codegen (unit
->machine
, unit
->assembly
, unit
->exp_code
.entries
[0].address
))