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
;
60 _slang_code_unit_ctr (slang_code_unit
*self
, struct slang_code_object_
*object
)
62 _slang_variable_scope_ctr (&self
->vars
);
63 _slang_function_scope_ctr (&self
->funs
);
64 _slang_struct_scope_ctr (&self
->structs
);
65 self
->object
= object
;
69 _slang_code_unit_dtr (slang_code_unit
*self
)
71 slang_variable_scope_destruct (&self
->vars
);
72 slang_function_scope_destruct (&self
->funs
);
73 slang_struct_scope_destruct (&self
->structs
);
81 _slang_code_object_ctr (slang_code_object
*self
)
83 _slang_code_unit_ctr (&self
->builtin
[0], self
);
84 _slang_code_unit_ctr (&self
->builtin
[1], self
);
85 _slang_code_unit_ctr (&self
->builtin
[2], self
);
86 _slang_code_unit_ctr (&self
->unit
, self
);
87 _slang_assembly_file_ctr (&self
->assembly
);
88 slang_machine_ctr (&self
->machine
);
89 self
->varpool
.next_addr
= 0;
90 slang_atom_pool_construct (&self
->atompool
);
91 slang_export_data_table_ctr (&self
->expdata
);
92 self
->expdata
.atoms
= &self
->atompool
;
93 slang_export_code_table_ctr (&self
->expcode
);
94 self
->expcode
.atoms
= &self
->atompool
;
98 _slang_code_object_dtr (slang_code_object
*self
)
100 _slang_code_unit_dtr (&self
->builtin
[0]);
101 _slang_code_unit_dtr (&self
->builtin
[1]);
102 _slang_code_unit_dtr (&self
->builtin
[2]);
103 _slang_code_unit_dtr (&self
->unit
);
104 slang_assembly_file_destruct (&self
->assembly
);
105 slang_machine_dtr (&self
->machine
);
106 slang_atom_pool_destruct (&self
->atompool
);
107 slang_export_data_table_dtr (&self
->expdata
);
108 slang_export_code_table_ctr (&self
->expcode
);
113 static char *out_of_memory
= "Error: Out of memory.\n";
115 void slang_info_log_construct (slang_info_log
*log
)
118 log
->dont_free_text
= 0;
121 void slang_info_log_destruct (slang_info_log
*log
)
123 if (!log
->dont_free_text
)
124 slang_alloc_free (log
->text
);
127 static int slang_info_log_message (slang_info_log
*log
, const char *prefix
, const char *msg
)
131 if (log
->dont_free_text
)
133 size
= slang_string_length (msg
) + 2;
135 size
+= slang_string_length (prefix
) + 2;
136 if (log
->text
!= NULL
) {
137 GLuint old_len
= slang_string_length (log
->text
);
138 log
->text
= (char *) (slang_alloc_realloc (log
->text
, old_len
+ 1, old_len
+ size
));
141 log
->text
= (char *) (slang_alloc_malloc (size
));
142 if (log
->text
!= NULL
)
145 if (log
->text
== NULL
)
147 if (prefix
!= NULL
) {
148 slang_string_concat (log
->text
, prefix
);
149 slang_string_concat (log
->text
, ": ");
151 slang_string_concat (log
->text
, msg
);
152 slang_string_concat (log
->text
, "\n");
156 int slang_info_log_print (slang_info_log
*log
, const char *msg
, ...)
162 _mesa_vsprintf (buf
, msg
, va
);
164 return slang_info_log_message (log
, NULL
, buf
);
167 int slang_info_log_error (slang_info_log
*log
, const char *msg
, ...)
173 _mesa_vsprintf (buf
, msg
, va
);
175 if (slang_info_log_message (log
, "Error", buf
))
177 slang_info_log_memory (log
);
181 int slang_info_log_warning (slang_info_log
*log
, const char *msg
, ...)
187 _mesa_vsprintf (buf
, msg
, va
);
189 if (slang_info_log_message (log
, "Warning", buf
))
191 slang_info_log_memory (log
);
195 void slang_info_log_memory (slang_info_log
*log
)
197 if (!slang_info_log_message (log
, "Error", "Out of memory."))
199 log
->dont_free_text
= 1;
200 log
->text
= out_of_memory
;
204 /* slang_parse_ctx */
206 typedef struct slang_parse_ctx_
212 slang_atom_pool
*atoms
;
215 /* slang_output_ctx */
217 typedef struct slang_output_ctx_
219 slang_variable_scope
*vars
;
220 slang_function_scope
*funs
;
221 slang_struct_scope
*structs
;
222 slang_assembly_file
*assembly
;
223 slang_var_pool
*global_pool
;
224 slang_machine
*machine
;
227 /* _slang_compile() */
229 static void parse_identifier_str (slang_parse_ctx
*C
, char **id
)
232 C
->I
+= _mesa_strlen (*id
) + 1;
235 static slang_atom
parse_identifier (slang_parse_ctx
*C
)
239 id
= (const char *) C
->I
;
240 C
->I
+= _mesa_strlen (id
) + 1;
241 return slang_atom_pool_atom (C
->atoms
, id
);
244 static int parse_number (slang_parse_ctx
*C
, int *number
)
246 const int radix
= (int) (*C
->I
++);
248 while (*C
->I
!= '\0')
251 if (*C
->I
>= '0' && *C
->I
<= '9')
252 digit
= (int) (*C
->I
- '0');
253 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
254 digit
= (int) (*C
->I
- 'A') + 10;
256 digit
= (int) (*C
->I
- 'a') + 10;
257 *number
= *number
* radix
+ digit
;
262 slang_info_log_warning (C
->L
, "%d: literal integer overflow.", *number
);
266 static int parse_float (slang_parse_ctx
*C
, float *number
)
268 char *integral
= NULL
;
269 char *fractional
= NULL
;
270 char *exponent
= NULL
;
273 parse_identifier_str (C
, &integral
);
274 parse_identifier_str (C
, &fractional
);
275 parse_identifier_str (C
, &exponent
);
277 whole
= (char *) (slang_alloc_malloc ((_mesa_strlen (integral
) + _mesa_strlen (fractional
) +
278 _mesa_strlen (exponent
) + 3) * sizeof (char)));
281 slang_info_log_memory (C
->L
);
285 slang_string_copy (whole
, integral
);
286 slang_string_concat (whole
, ".");
287 slang_string_concat (whole
, fractional
);
288 slang_string_concat (whole
, "E");
289 slang_string_concat (whole
, exponent
);
291 *number
= (float) (_mesa_strtod(whole
, (char **)NULL
));
293 slang_alloc_free (whole
);
297 /* revision number - increment after each change affecting emitted output */
300 static int check_revision (slang_parse_ctx
*C
)
302 if (*C
->I
!= REVISION
)
304 slang_info_log_error (C
->L
, "Internal compiler error.");
311 static int parse_statement (slang_parse_ctx
*, slang_output_ctx
*, slang_operation
*);
312 static int parse_expression (slang_parse_ctx
*, slang_output_ctx
*, slang_operation
*);
313 static int parse_type_specifier (slang_parse_ctx
*, slang_output_ctx
*, slang_type_specifier
*);
315 static GLboolean
parse_array_len (slang_parse_ctx
*C
, slang_output_ctx
*O
, GLuint
*len
)
317 slang_operation array_size
;
318 slang_assembly_name_space space
;
321 if (!slang_operation_construct (&array_size
))
323 if (!parse_expression (C
, O
, &array_size
))
325 slang_operation_destruct (&array_size
);
329 space
.funcs
= O
->funs
;
330 space
.structs
= O
->structs
;
331 space
.vars
= O
->vars
;
332 result
= _slang_evaluate_int (O
->assembly
, O
->machine
, &space
, &array_size
, len
, C
->atoms
);
333 slang_operation_destruct (&array_size
);
337 static GLboolean
calculate_var_size (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_variable
*var
)
339 slang_storage_aggregate agg
;
341 if (!slang_storage_aggregate_construct (&agg
))
343 if (!_slang_aggregate_variable (&agg
, &var
->type
.specifier
, var
->array_len
, O
->funs
, O
->structs
,
344 O
->vars
, O
->machine
, O
->assembly
, C
->atoms
))
346 slang_storage_aggregate_destruct (&agg
);
349 var
->size
= _slang_sizeof_aggregate (&agg
);
350 slang_storage_aggregate_destruct (&agg
);
354 static GLboolean
convert_to_array (slang_parse_ctx
*C
, slang_variable
*var
,
355 const slang_type_specifier
*sp
)
357 /* sized array - mark it as array, copy the specifier to the array element and
358 * parse the expression */
359 var
->type
.specifier
.type
= slang_spec_array
;
360 var
->type
.specifier
._array
= (slang_type_specifier
*) slang_alloc_malloc (sizeof (
361 slang_type_specifier
));
362 if (var
->type
.specifier
._array
== NULL
)
364 slang_info_log_memory (C
->L
);
367 slang_type_specifier_ctr (var
->type
.specifier
._array
);
368 return slang_type_specifier_copy (var
->type
.specifier
._array
, sp
);
371 /* structure field */
374 #define FIELD_ARRAY 2
376 static GLboolean
parse_struct_field_var (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_variable
*var
,
377 const slang_type_specifier
*sp
)
379 var
->a_name
= parse_identifier (C
);
380 if (var
->a_name
== SLANG_ATOM_NULL
)
386 if (!slang_type_specifier_copy (&var
->type
.specifier
, sp
))
390 if (!convert_to_array (C
, var
, sp
))
392 if (!parse_array_len (C
, O
, &var
->array_len
))
399 return calculate_var_size (C
, O
, var
);
402 static int parse_struct_field (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_struct
*st
,
403 slang_type_specifier
*sp
)
405 slang_output_ctx o
= *O
;
407 o
.structs
= st
->structs
;
408 if (!parse_type_specifier (C
, &o
, sp
))
414 st
->fields
->variables
= (slang_variable
*) slang_alloc_realloc (st
->fields
->variables
,
415 st
->fields
->num_variables
* sizeof (slang_variable
),
416 (st
->fields
->num_variables
+ 1) * sizeof (slang_variable
));
417 if (st
->fields
->variables
== NULL
)
419 slang_info_log_memory (C
->L
);
422 var
= &st
->fields
->variables
[st
->fields
->num_variables
];
423 if (!slang_variable_construct (var
))
425 st
->fields
->num_variables
++;
426 if (!parse_struct_field_var (C
, &o
, var
, sp
))
429 while (*C
->I
++ != FIELD_NONE
);
434 static int parse_struct (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_struct
**st
)
439 /* parse struct name (if any) and make sure it is unique in current scope */
440 a_name
= parse_identifier (C
);
441 if (a_name
== SLANG_ATOM_NULL
)
443 name
= slang_atom_pool_id (C
->atoms
, a_name
);
444 if (name
[0] != '\0' && slang_struct_scope_find (O
->structs
, a_name
, 0) != NULL
)
446 slang_info_log_error (C
->L
, "%s: duplicate type name.", name
);
450 /* set-up a new struct */
451 *st
= (slang_struct
*) slang_alloc_malloc (sizeof (slang_struct
));
454 slang_info_log_memory (C
->L
);
457 if (!slang_struct_construct (*st
))
459 slang_alloc_free (*st
);
461 slang_info_log_memory (C
->L
);
464 (**st
).a_name
= a_name
;
465 (**st
).structs
->outer_scope
= O
->structs
;
467 /* parse individual struct fields */
470 slang_type_specifier sp
;
472 slang_type_specifier_ctr (&sp
);
473 if (!parse_struct_field (C
, O
, *st
, &sp
))
475 slang_type_specifier_dtr (&sp
);
478 slang_type_specifier_dtr (&sp
);
480 while (*C
->I
++ != FIELD_NONE
);
482 /* if named struct, copy it to current scope */
487 O
->structs
->structs
= (slang_struct
*) slang_alloc_realloc (O
->structs
->structs
,
488 O
->structs
->num_structs
* sizeof (slang_struct
),
489 (O
->structs
->num_structs
+ 1) * sizeof (slang_struct
));
490 if (O
->structs
->structs
== NULL
)
492 slang_info_log_memory (C
->L
);
495 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
496 if (!slang_struct_construct (s
))
498 O
->structs
->num_structs
++;
499 if (!slang_struct_copy (s
, *st
))
507 #define TYPE_QUALIFIER_NONE 0
508 #define TYPE_QUALIFIER_CONST 1
509 #define TYPE_QUALIFIER_ATTRIBUTE 2
510 #define TYPE_QUALIFIER_VARYING 3
511 #define TYPE_QUALIFIER_UNIFORM 4
512 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
513 #define TYPE_QUALIFIER_FIXEDINPUT 6
515 static int parse_type_qualifier (slang_parse_ctx
*C
, slang_type_qualifier
*qual
)
519 case TYPE_QUALIFIER_NONE
:
520 *qual
= slang_qual_none
;
522 case TYPE_QUALIFIER_CONST
:
523 *qual
= slang_qual_const
;
525 case TYPE_QUALIFIER_ATTRIBUTE
:
526 *qual
= slang_qual_attribute
;
528 case TYPE_QUALIFIER_VARYING
:
529 *qual
= slang_qual_varying
;
531 case TYPE_QUALIFIER_UNIFORM
:
532 *qual
= slang_qual_uniform
;
534 case TYPE_QUALIFIER_FIXEDOUTPUT
:
535 *qual
= slang_qual_fixedoutput
;
537 case TYPE_QUALIFIER_FIXEDINPUT
:
538 *qual
= slang_qual_fixedinput
;
547 #define TYPE_SPECIFIER_VOID 0
548 #define TYPE_SPECIFIER_BOOL 1
549 #define TYPE_SPECIFIER_BVEC2 2
550 #define TYPE_SPECIFIER_BVEC3 3
551 #define TYPE_SPECIFIER_BVEC4 4
552 #define TYPE_SPECIFIER_INT 5
553 #define TYPE_SPECIFIER_IVEC2 6
554 #define TYPE_SPECIFIER_IVEC3 7
555 #define TYPE_SPECIFIER_IVEC4 8
556 #define TYPE_SPECIFIER_FLOAT 9
557 #define TYPE_SPECIFIER_VEC2 10
558 #define TYPE_SPECIFIER_VEC3 11
559 #define TYPE_SPECIFIER_VEC4 12
560 #define TYPE_SPECIFIER_MAT2 13
561 #define TYPE_SPECIFIER_MAT3 14
562 #define TYPE_SPECIFIER_MAT4 15
563 #define TYPE_SPECIFIER_SAMPLER1D 16
564 #define TYPE_SPECIFIER_SAMPLER2D 17
565 #define TYPE_SPECIFIER_SAMPLER3D 18
566 #define TYPE_SPECIFIER_SAMPLERCUBE 19
567 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
568 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
569 #define TYPE_SPECIFIER_STRUCT 22
570 #define TYPE_SPECIFIER_TYPENAME 23
572 static int parse_type_specifier (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_type_specifier
*spec
)
576 case TYPE_SPECIFIER_VOID
:
577 spec
->type
= slang_spec_void
;
579 case TYPE_SPECIFIER_BOOL
:
580 spec
->type
= slang_spec_bool
;
582 case TYPE_SPECIFIER_BVEC2
:
583 spec
->type
= slang_spec_bvec2
;
585 case TYPE_SPECIFIER_BVEC3
:
586 spec
->type
= slang_spec_bvec3
;
588 case TYPE_SPECIFIER_BVEC4
:
589 spec
->type
= slang_spec_bvec4
;
591 case TYPE_SPECIFIER_INT
:
592 spec
->type
= slang_spec_int
;
594 case TYPE_SPECIFIER_IVEC2
:
595 spec
->type
= slang_spec_ivec2
;
597 case TYPE_SPECIFIER_IVEC3
:
598 spec
->type
= slang_spec_ivec3
;
600 case TYPE_SPECIFIER_IVEC4
:
601 spec
->type
= slang_spec_ivec4
;
603 case TYPE_SPECIFIER_FLOAT
:
604 spec
->type
= slang_spec_float
;
606 case TYPE_SPECIFIER_VEC2
:
607 spec
->type
= slang_spec_vec2
;
609 case TYPE_SPECIFIER_VEC3
:
610 spec
->type
= slang_spec_vec3
;
612 case TYPE_SPECIFIER_VEC4
:
613 spec
->type
= slang_spec_vec4
;
615 case TYPE_SPECIFIER_MAT2
:
616 spec
->type
= slang_spec_mat2
;
618 case TYPE_SPECIFIER_MAT3
:
619 spec
->type
= slang_spec_mat3
;
621 case TYPE_SPECIFIER_MAT4
:
622 spec
->type
= slang_spec_mat4
;
624 case TYPE_SPECIFIER_SAMPLER1D
:
625 spec
->type
= slang_spec_sampler1D
;
627 case TYPE_SPECIFIER_SAMPLER2D
:
628 spec
->type
= slang_spec_sampler2D
;
630 case TYPE_SPECIFIER_SAMPLER3D
:
631 spec
->type
= slang_spec_sampler3D
;
633 case TYPE_SPECIFIER_SAMPLERCUBE
:
634 spec
->type
= slang_spec_samplerCube
;
636 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
637 spec
->type
= slang_spec_sampler1DShadow
;
639 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
640 spec
->type
= slang_spec_sampler2DShadow
;
642 case TYPE_SPECIFIER_STRUCT
:
643 spec
->type
= slang_spec_struct
;
644 if (!parse_struct (C
, O
, &spec
->_struct
))
647 case TYPE_SPECIFIER_TYPENAME
:
648 spec
->type
= slang_spec_struct
;
653 a_name
= parse_identifier (C
);
657 stru
= slang_struct_scope_find (O
->structs
, a_name
, 1);
660 slang_info_log_error (C
->L
, "%s: undeclared type name.",
661 slang_atom_pool_id (C
->atoms
, a_name
));
665 spec
->_struct
= (slang_struct
*) slang_alloc_malloc (sizeof (slang_struct
));
666 if (spec
->_struct
== NULL
)
668 slang_info_log_memory (C
->L
);
671 if (!slang_struct_construct (spec
->_struct
))
673 slang_alloc_free (spec
->_struct
);
674 spec
->_struct
= NULL
;
677 if (!slang_struct_copy (spec
->_struct
, stru
))
687 static int parse_fully_specified_type (slang_parse_ctx
*C
, slang_output_ctx
*O
,
688 slang_fully_specified_type
*type
)
690 if (!parse_type_qualifier (C
, &type
->qualifier
))
692 return parse_type_specifier (C
, O
, &type
->specifier
);
697 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
698 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
702 #define OP_CONTINUE 6
705 #define OP_EXPRESSION 9
710 #define OP_PUSH_VOID 14
711 #define OP_PUSH_BOOL 15
712 #define OP_PUSH_INT 16
713 #define OP_PUSH_FLOAT 17
714 #define OP_PUSH_IDENTIFIER 18
715 #define OP_SEQUENCE 19
717 #define OP_ADDASSIGN 21
718 #define OP_SUBASSIGN 22
719 #define OP_MULASSIGN 23
720 #define OP_DIVASSIGN 24
721 /*#define OP_MODASSIGN 25*/
722 /*#define OP_LSHASSIGN 26*/
723 /*#define OP_RSHASSIGN 27*/
724 /*#define OP_ORASSIGN 28*/
725 /*#define OP_XORASSIGN 29*/
726 /*#define OP_ANDASSIGN 30*/
728 #define OP_LOGICALOR 32
729 #define OP_LOGICALXOR 33
730 #define OP_LOGICALAND 34
731 /*#define OP_BITOR 35*/
732 /*#define OP_BITXOR 36*/
733 /*#define OP_BITAND 37*/
735 #define OP_NOTEQUAL 39
737 #define OP_GREATER 41
738 #define OP_LESSEQUAL 42
739 #define OP_GREATEREQUAL 43
740 /*#define OP_LSHIFT 44*/
741 /*#define OP_RSHIFT 45*/
743 #define OP_SUBTRACT 47
744 #define OP_MULTIPLY 48
746 /*#define OP_MODULUS 50*/
747 #define OP_PREINCREMENT 51
748 #define OP_PREDECREMENT 52
751 /*#define OP_COMPLEMENT 55*/
753 #define OP_SUBSCRIPT 57
756 #define OP_POSTINCREMENT 60
757 #define OP_POSTDECREMENT 61
759 static int parse_child_operation (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_operation
*oper
,
764 oper
->children
= (slang_operation
*) slang_alloc_realloc (oper
->children
,
765 oper
->num_children
* sizeof (slang_operation
),
766 (oper
->num_children
+ 1) * sizeof (slang_operation
));
767 if (oper
->children
== NULL
)
769 slang_info_log_memory (C
->L
);
772 ch
= &oper
->children
[oper
->num_children
];
773 if (!slang_operation_construct (ch
))
775 slang_info_log_memory (C
->L
);
778 oper
->num_children
++;
780 return parse_statement (C
, O
, ch
);
781 return parse_expression (C
, O
, ch
);
784 static int parse_declaration (slang_parse_ctx
*C
, slang_output_ctx
*O
);
786 static int parse_statement (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_operation
*oper
)
788 oper
->locals
->outer_scope
= O
->vars
;
791 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
792 /* parse child statements, do not create new variable scope */
793 oper
->type
= slang_oper_block_no_new_scope
;
794 while (*C
->I
!= OP_END
)
795 if (!parse_child_operation (C
, O
, oper
, 1))
799 case OP_BLOCK_BEGIN_NEW_SCOPE
:
800 /* parse child statements, create new variable scope */
802 slang_output_ctx o
= *O
;
804 oper
->type
= slang_oper_block_new_scope
;
805 o
.vars
= oper
->locals
;
806 while (*C
->I
!= OP_END
)
807 if (!parse_child_operation (C
, &o
, oper
, 1))
813 /* local variable declaration, individual declarators are stored as children identifiers */
814 oper
->type
= slang_oper_variable_decl
;
816 const unsigned int first_var
= O
->vars
->num_variables
;
818 /* parse the declaration, note that there can be zero or more than one declarators */
819 if (!parse_declaration (C
, O
))
821 if (first_var
< O
->vars
->num_variables
)
823 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
826 oper
->children
= (slang_operation
*) slang_alloc_malloc (num_vars
* sizeof (
828 if (oper
->children
== NULL
)
830 slang_info_log_memory (C
->L
);
833 for (oper
->num_children
= 0; oper
->num_children
< num_vars
; oper
->num_children
++)
834 if (!slang_operation_construct (&oper
->children
[oper
->num_children
]))
836 slang_info_log_memory (C
->L
);
839 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++)
841 slang_operation
*o
= &oper
->children
[i
- first_var
];
843 o
->type
= slang_oper_identifier
;
844 o
->locals
->outer_scope
= O
->vars
;
845 o
->a_id
= O
->vars
->variables
[i
].a_name
;
851 /* the __asm statement, parse the mnemonic and all its arguments as expressions */
852 oper
->type
= slang_oper_asm
;
853 oper
->a_id
= parse_identifier (C
);
854 if (oper
->a_id
== SLANG_ATOM_NULL
)
856 while (*C
->I
!= OP_END
)
857 if (!parse_child_operation (C
, O
, oper
, 0))
862 oper
->type
= slang_oper_break
;
865 oper
->type
= slang_oper_continue
;
868 oper
->type
= slang_oper_discard
;
871 oper
->type
= slang_oper_return
;
872 if (!parse_child_operation (C
, O
, oper
, 0))
876 oper
->type
= slang_oper_expression
;
877 if (!parse_child_operation (C
, O
, oper
, 0))
881 oper
->type
= slang_oper_if
;
882 if (!parse_child_operation (C
, O
, oper
, 0))
884 if (!parse_child_operation (C
, O
, oper
, 1))
886 if (!parse_child_operation (C
, O
, oper
, 1))
891 slang_output_ctx o
= *O
;
893 oper
->type
= slang_oper_while
;
894 o
.vars
= oper
->locals
;
895 if (!parse_child_operation (C
, &o
, oper
, 1))
897 if (!parse_child_operation (C
, &o
, oper
, 1))
902 oper
->type
= slang_oper_do
;
903 if (!parse_child_operation (C
, O
, oper
, 1))
905 if (!parse_child_operation (C
, O
, oper
, 0))
910 slang_output_ctx o
= *O
;
912 oper
->type
= slang_oper_for
;
913 o
.vars
= oper
->locals
;
914 if (!parse_child_operation (C
, &o
, oper
, 1))
916 if (!parse_child_operation (C
, &o
, oper
, 1))
918 if (!parse_child_operation (C
, &o
, oper
, 0))
920 if (!parse_child_operation (C
, &o
, oper
, 1))
930 static int handle_nary_expression (slang_parse_ctx
*C
, slang_operation
*op
, slang_operation
**ops
,
931 unsigned int *total_ops
, unsigned int n
)
935 op
->children
= (slang_operation
*) slang_alloc_malloc (n
* sizeof (slang_operation
));
936 if (op
->children
== NULL
)
938 slang_info_log_memory (C
->L
);
941 op
->num_children
= n
;
943 for (i
= 0; i
< n
; i
++)
944 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
945 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
948 *ops
= (slang_operation
*) slang_alloc_realloc (*ops
, (*total_ops
+ n
) * sizeof (slang_operation
),
949 *total_ops
* sizeof (slang_operation
));
952 slang_info_log_memory (C
->L
);
958 static int is_constructor_name (const char *name
, slang_atom a_name
, slang_struct_scope
*structs
)
960 if (slang_type_specifier_type_from_string (name
) != slang_spec_void
)
962 return slang_struct_scope_find (structs
, a_name
, 1) != NULL
;
965 static int parse_expression (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_operation
*oper
)
967 slang_operation
*ops
= NULL
;
968 unsigned int num_ops
= 0;
971 while (*C
->I
!= OP_END
)
974 const unsigned int op_code
= *C
->I
++;
976 /* allocate default operation, becomes a no-op if not used */
977 ops
= (slang_operation
*) slang_alloc_realloc (ops
,
978 num_ops
* sizeof (slang_operation
), (num_ops
+ 1) * sizeof (slang_operation
));
981 slang_info_log_memory (C
->L
);
985 if (!slang_operation_construct (op
))
987 slang_info_log_memory (C
->L
);
991 op
->locals
->outer_scope
= O
->vars
;
996 op
->type
= slang_oper_void
;
999 op
->type
= slang_oper_literal_bool
;
1000 if (!parse_number (C
, &number
))
1002 op
->literal
= (GLfloat
) number
;
1005 op
->type
= slang_oper_literal_int
;
1006 if (!parse_number (C
, &number
))
1008 op
->literal
= (GLfloat
) number
;
1011 op
->type
= slang_oper_literal_float
;
1012 if (!parse_float (C
, &op
->literal
))
1015 case OP_PUSH_IDENTIFIER
:
1016 op
->type
= slang_oper_identifier
;
1017 op
->a_id
= parse_identifier (C
);
1018 if (op
->a_id
== SLANG_ATOM_NULL
)
1022 op
->type
= slang_oper_sequence
;
1023 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1027 op
->type
= slang_oper_assign
;
1028 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1032 op
->type
= slang_oper_addassign
;
1033 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1037 op
->type
= slang_oper_subassign
;
1038 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1042 op
->type
= slang_oper_mulassign
;
1043 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1047 op
->type
= slang_oper_divassign
;
1048 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1051 /*case OP_MODASSIGN:*/
1052 /*case OP_LSHASSIGN:*/
1053 /*case OP_RSHASSIGN:*/
1054 /*case OP_ORASSIGN:*/
1055 /*case OP_XORASSIGN:*/
1056 /*case OP_ANDASSIGN:*/
1058 op
->type
= slang_oper_select
;
1059 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 3))
1063 op
->type
= slang_oper_logicalor
;
1064 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1068 op
->type
= slang_oper_logicalxor
;
1069 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1073 op
->type
= slang_oper_logicaland
;
1074 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1081 op
->type
= slang_oper_equal
;
1082 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1086 op
->type
= slang_oper_notequal
;
1087 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1091 op
->type
= slang_oper_less
;
1092 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1096 op
->type
= slang_oper_greater
;
1097 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1101 op
->type
= slang_oper_lessequal
;
1102 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1105 case OP_GREATEREQUAL
:
1106 op
->type
= slang_oper_greaterequal
;
1107 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1113 op
->type
= slang_oper_add
;
1114 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1118 op
->type
= slang_oper_subtract
;
1119 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1123 op
->type
= slang_oper_multiply
;
1124 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1128 op
->type
= slang_oper_divide
;
1129 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1132 /*case OP_MODULUS:*/
1133 case OP_PREINCREMENT
:
1134 op
->type
= slang_oper_preincrement
;
1135 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 1))
1138 case OP_PREDECREMENT
:
1139 op
->type
= slang_oper_predecrement
;
1140 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 1))
1144 op
->type
= slang_oper_plus
;
1145 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 1))
1149 op
->type
= slang_oper_minus
;
1150 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 1))
1154 op
->type
= slang_oper_not
;
1155 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 1))
1158 /*case OP_COMPLEMENT:*/
1160 op
->type
= slang_oper_subscript
;
1161 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1165 op
->type
= slang_oper_call
;
1166 op
->a_id
= parse_identifier (C
);
1167 if (op
->a_id
== SLANG_ATOM_NULL
)
1169 while (*C
->I
!= OP_END
)
1170 if (!parse_child_operation (C
, O
, op
, 0))
1173 if (!C
->parsing_builtin
&& !slang_function_scope_find_by_name (O
->funs
, op
->a_id
, 1))
1177 id
= slang_atom_pool_id (C
->atoms
, op
->a_id
);
1178 if (!is_constructor_name (id
, op
->a_id
, O
->structs
))
1180 slang_info_log_error (C
->L
, "%s: undeclared function name.", id
);
1186 op
->type
= slang_oper_field
;
1187 op
->a_id
= parse_identifier (C
);
1188 if (op
->a_id
== SLANG_ATOM_NULL
)
1190 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 1))
1193 case OP_POSTINCREMENT
:
1194 op
->type
= slang_oper_postincrement
;
1195 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 1))
1198 case OP_POSTDECREMENT
:
1199 op
->type
= slang_oper_postdecrement
;
1200 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 1))
1210 slang_alloc_free (ops
);
1214 /* parameter qualifier */
1215 #define PARAM_QUALIFIER_IN 0
1216 #define PARAM_QUALIFIER_OUT 1
1217 #define PARAM_QUALIFIER_INOUT 2
1219 /* function parameter array presence */
1220 #define PARAMETER_ARRAY_NOT_PRESENT 0
1221 #define PARAMETER_ARRAY_PRESENT 1
1223 static int parse_parameter_declaration (slang_parse_ctx
*C
, slang_output_ctx
*O
,
1224 slang_variable
*param
)
1226 /* parse and validate the parameter's type qualifiers (there can be two at most) because
1227 * not all combinations are valid */
1228 if (!parse_type_qualifier (C
, ¶m
->type
.qualifier
))
1232 case PARAM_QUALIFIER_IN
:
1233 if (param
->type
.qualifier
!= slang_qual_const
&& param
->type
.qualifier
!= slang_qual_none
)
1235 slang_info_log_error (C
->L
, "Invalid type qualifier.");
1239 case PARAM_QUALIFIER_OUT
:
1240 if (param
->type
.qualifier
== slang_qual_none
)
1241 param
->type
.qualifier
= slang_qual_out
;
1244 slang_info_log_error (C
->L
, "Invalid type qualifier.");
1248 case PARAM_QUALIFIER_INOUT
:
1249 if (param
->type
.qualifier
== slang_qual_none
)
1250 param
->type
.qualifier
= slang_qual_inout
;
1253 slang_info_log_error (C
->L
, "Invalid type qualifier.");
1261 /* parse parameter's type specifier and name */
1262 if (!parse_type_specifier (C
, O
, ¶m
->type
.specifier
))
1264 param
->a_name
= parse_identifier (C
);
1265 if (param
->a_name
== SLANG_ATOM_NULL
)
1268 /* if the parameter is an array, parse its size (the size must be explicitly defined */
1269 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
)
1271 slang_type_specifier p
;
1273 slang_type_specifier_ctr (&p
);
1274 if (!slang_type_specifier_copy (&p
, ¶m
->type
.specifier
))
1276 slang_type_specifier_dtr (&p
);
1279 if (!convert_to_array (C
, param
, &p
))
1281 slang_type_specifier_dtr (&p
);
1284 slang_type_specifier_dtr (&p
);
1285 if (!parse_array_len (C
, O
, ¶m
->array_len
))
1289 /* calculate the parameter size */
1290 if (!calculate_var_size (C
, O
, param
))
1293 /* TODO: allocate the local address here? */
1298 #define FUNCTION_ORDINARY 0
1299 #define FUNCTION_CONSTRUCTOR 1
1300 #define FUNCTION_OPERATOR 2
1302 /* function parameter */
1303 #define PARAMETER_NONE 0
1304 #define PARAMETER_NEXT 1
1307 #define OPERATOR_ADDASSIGN 1
1308 #define OPERATOR_SUBASSIGN 2
1309 #define OPERATOR_MULASSIGN 3
1310 #define OPERATOR_DIVASSIGN 4
1311 /*#define OPERATOR_MODASSIGN 5*/
1312 /*#define OPERATOR_LSHASSIGN 6*/
1313 /*#define OPERATOR_RSHASSIGN 7*/
1314 /*#define OPERATOR_ANDASSIGN 8*/
1315 /*#define OPERATOR_XORASSIGN 9*/
1316 /*#define OPERATOR_ORASSIGN 10*/
1317 #define OPERATOR_LOGICALXOR 11
1318 /*#define OPERATOR_BITOR 12*/
1319 /*#define OPERATOR_BITXOR 13*/
1320 /*#define OPERATOR_BITAND 14*/
1321 #define OPERATOR_LESS 15
1322 #define OPERATOR_GREATER 16
1323 #define OPERATOR_LESSEQUAL 17
1324 #define OPERATOR_GREATEREQUAL 18
1325 /*#define OPERATOR_LSHIFT 19*/
1326 /*#define OPERATOR_RSHIFT 20*/
1327 #define OPERATOR_MULTIPLY 21
1328 #define OPERATOR_DIVIDE 22
1329 /*#define OPERATOR_MODULUS 23*/
1330 #define OPERATOR_INCREMENT 24
1331 #define OPERATOR_DECREMENT 25
1332 #define OPERATOR_PLUS 26
1333 #define OPERATOR_MINUS 27
1334 /*#define OPERATOR_COMPLEMENT 28*/
1335 #define OPERATOR_NOT 29
1337 static const struct {
1338 unsigned int o_code
;
1340 } operator_names
[] = {
1341 { OPERATOR_INCREMENT
, "++" },
1342 { OPERATOR_ADDASSIGN
, "+=" },
1343 { OPERATOR_PLUS
, "+" },
1344 { OPERATOR_DECREMENT
, "--" },
1345 { OPERATOR_SUBASSIGN
, "-=" },
1346 { OPERATOR_MINUS
, "-" },
1347 { OPERATOR_NOT
, "!" },
1348 { OPERATOR_MULASSIGN
, "*=" },
1349 { OPERATOR_MULTIPLY
, "*" },
1350 { OPERATOR_DIVASSIGN
, "/=" },
1351 { OPERATOR_DIVIDE
, "/" },
1352 { OPERATOR_LESSEQUAL
, "<=" },
1353 /*{ OPERATOR_LSHASSIGN, "<<=" },*/
1354 /*{ OPERATOR_LSHIFT, "<<" },*/
1355 { OPERATOR_LESS
, "<" },
1356 { OPERATOR_GREATEREQUAL
, ">=" },
1357 /*{ OPERATOR_RSHASSIGN, ">>=" },*/
1358 /*{ OPERATOR_RSHIFT, ">>" },*/
1359 { OPERATOR_GREATER
, ">" },
1360 /*{ OPERATOR_MODASSIGN, "%=" },*/
1361 /*{ OPERATOR_MODULUS, "%" },*/
1362 /*{ OPERATOR_ANDASSIGN, "&=" },*/
1363 /*{ OPERATOR_BITAND, "&" },*/
1364 /*{ OPERATOR_ORASSIGN, "|=" },*/
1365 /*{ OPERATOR_BITOR, "|" },*/
1366 /*{ OPERATOR_COMPLEMENT, "~" },*/
1367 /*{ OPERATOR_XORASSIGN, "^=" },*/
1368 { OPERATOR_LOGICALXOR
, "^^" },
1369 /*{ OPERATOR_BITXOR, "^" }*/
1372 static slang_atom
parse_operator_name (slang_parse_ctx
*C
)
1376 for (i
= 0; i
< sizeof (operator_names
) / sizeof (*operator_names
); i
++)
1378 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
))
1380 slang_atom atom
= slang_atom_pool_atom (C
->atoms
, operator_names
[i
].o_name
);
1381 if (atom
== SLANG_ATOM_NULL
)
1383 slang_info_log_memory (C
->L
);
1393 static int parse_function_prototype (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_function
*func
)
1395 /* parse function type and name */
1396 if (!parse_fully_specified_type (C
, O
, &func
->header
.type
))
1400 case FUNCTION_ORDINARY
:
1401 func
->kind
= slang_func_ordinary
;
1402 func
->header
.a_name
= parse_identifier (C
);
1403 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1406 case FUNCTION_CONSTRUCTOR
:
1407 func
->kind
= slang_func_constructor
;
1408 if (func
->header
.type
.specifier
.type
== slang_spec_struct
)
1410 func
->header
.a_name
= slang_atom_pool_atom (C
->atoms
,
1411 slang_type_specifier_type_to_string (func
->header
.type
.specifier
.type
));
1412 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1414 slang_info_log_memory (C
->L
);
1418 case FUNCTION_OPERATOR
:
1419 func
->kind
= slang_func_operator
;
1420 func
->header
.a_name
= parse_operator_name (C
);
1421 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1428 /* parse function parameters */
1429 while (*C
->I
++ == PARAMETER_NEXT
)
1433 func
->parameters
->variables
= (slang_variable
*) slang_alloc_realloc (
1434 func
->parameters
->variables
,
1435 func
->parameters
->num_variables
* sizeof (slang_variable
),
1436 (func
->parameters
->num_variables
+ 1) * sizeof (slang_variable
));
1437 if (func
->parameters
->variables
== NULL
)
1439 slang_info_log_memory (C
->L
);
1442 p
= &func
->parameters
->variables
[func
->parameters
->num_variables
];
1443 if (!slang_variable_construct (p
))
1445 func
->parameters
->num_variables
++;
1446 if (!parse_parameter_declaration (C
, O
, p
))
1450 /* function formal parameters and local variables share the same scope, so save
1451 * the information about param count in a seperate place
1452 * also link the scope to the global variable scope so when a given identifier is not
1453 * found here, the search process continues in the global space */
1454 func
->param_count
= func
->parameters
->num_variables
;
1455 func
->parameters
->outer_scope
= O
->vars
;
1459 static int parse_function_definition (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_function
*func
)
1461 slang_output_ctx o
= *O
;
1463 if (!parse_function_prototype (C
, O
, func
))
1466 /* create function's body operation */
1467 func
->body
= (slang_operation
*) slang_alloc_malloc (sizeof (slang_operation
));
1468 if (func
->body
== NULL
)
1470 slang_info_log_memory (C
->L
);
1473 if (!slang_operation_construct (func
->body
))
1475 slang_alloc_free (func
->body
);
1477 slang_info_log_memory (C
->L
);
1481 /* to parse the body the parse context is modified in order to capture parsed variables
1482 * into function's local variable scope */
1483 C
->global_scope
= 0;
1484 o
.vars
= func
->parameters
;
1485 if (!parse_statement (C
, &o
, func
->body
))
1487 C
->global_scope
= 1;
1491 static GLboolean
initialize_global (slang_assemble_ctx
*A
, slang_variable
*var
)
1493 slang_assembly_file_restore_point point
;
1495 slang_assembly_local_info save_local
= A
->local
;
1496 slang_operation op_id
, op_assign
;
1499 /* save the current assembly */
1500 if (!slang_assembly_file_restore_point_save (A
->file
, &point
))
1503 /* setup the machine */
1505 mach
.ip
= A
->file
->count
;
1507 /* allocate local storage for expression */
1508 A
->local
.ret_size
= 0;
1509 A
->local
.addr_tmp
= 0;
1510 A
->local
.swizzle_tmp
= 4;
1511 if (!slang_assembly_file_push_label (A
->file
, slang_asm_local_alloc
, 20))
1513 if (!slang_assembly_file_push_label (A
->file
, slang_asm_enter
, 20))
1516 /* construct the left side of assignment */
1517 if (!slang_operation_construct (&op_id
))
1519 op_id
.type
= slang_oper_identifier
;
1520 op_id
.a_id
= var
->a_name
;
1522 /* put the variable into operation's scope */
1523 op_id
.locals
->variables
= (slang_variable
*) slang_alloc_malloc (sizeof (slang_variable
));
1524 if (op_id
.locals
->variables
== NULL
)
1526 slang_operation_destruct (&op_id
);
1529 op_id
.locals
->num_variables
= 1;
1530 op_id
.locals
->variables
[0] = *var
;
1532 /* construct the assignment expression */
1533 if (!slang_operation_construct (&op_assign
))
1535 op_id
.locals
->num_variables
= 0;
1536 slang_operation_destruct (&op_id
);
1539 op_assign
.type
= slang_oper_assign
;
1540 op_assign
.children
= (slang_operation
*) slang_alloc_malloc (2 * sizeof (slang_operation
));
1541 if (op_assign
.children
== NULL
)
1543 slang_operation_destruct (&op_assign
);
1544 op_id
.locals
->num_variables
= 0;
1545 slang_operation_destruct (&op_id
);
1548 op_assign
.num_children
= 2;
1549 op_assign
.children
[0] = op_id
;
1550 op_assign
.children
[1] = *var
->initializer
;
1552 /* insert the actual expression */
1553 result
= _slang_assemble_operation (A
, &op_assign
, slang_ref_forbid
);
1555 /* carefully destroy the operations */
1556 op_assign
.num_children
= 0;
1557 slang_alloc_free (op_assign
.children
);
1558 op_assign
.children
= NULL
;
1559 slang_operation_destruct (&op_assign
);
1560 op_id
.locals
->num_variables
= 0;
1561 slang_operation_destruct (&op_id
);
1565 if (!slang_assembly_file_push (A
->file
, slang_asm_exit
))
1568 /* execute the expression */
1569 if (!_slang_execute2 (A
->file
, &mach
))
1572 /* restore the old assembly */
1573 if (!slang_assembly_file_restore_point_load (A
->file
, &point
))
1575 A
->local
= save_local
;
1577 /* now we copy the contents of the initialized variable back to the original machine */
1578 _mesa_memcpy ((GLubyte
*) A
->mach
->mem
+ var
->address
, (GLubyte
*) mach
.mem
+ var
->address
,
1584 /* init declarator list */
1585 #define DECLARATOR_NONE 0
1586 #define DECLARATOR_NEXT 1
1588 /* variable declaration */
1589 #define VARIABLE_NONE 0
1590 #define VARIABLE_IDENTIFIER 1
1591 #define VARIABLE_INITIALIZER 2
1592 #define VARIABLE_ARRAY_EXPLICIT 3
1593 #define VARIABLE_ARRAY_UNKNOWN 4
1595 static int parse_init_declarator (slang_parse_ctx
*C
, slang_output_ctx
*O
,
1596 const slang_fully_specified_type
*type
)
1598 slang_variable
*var
;
1600 /* empty init declatator (without name, e.g. "float ;") */
1601 if (*C
->I
++ == VARIABLE_NONE
)
1604 /* make room for the new variable and initialize it */
1605 O
->vars
->variables
= (slang_variable
*) slang_alloc_realloc (O
->vars
->variables
,
1606 O
->vars
->num_variables
* sizeof (slang_variable
),
1607 (O
->vars
->num_variables
+ 1) * sizeof (slang_variable
));
1608 if (O
->vars
->variables
== NULL
)
1610 slang_info_log_memory (C
->L
);
1613 var
= &O
->vars
->variables
[O
->vars
->num_variables
];
1614 if (!slang_variable_construct (var
))
1616 O
->vars
->num_variables
++;
1618 /* copy the declarator qualifier type, parse the identifier */
1619 var
->global
= C
->global_scope
;
1620 var
->type
.qualifier
= type
->qualifier
;
1621 var
->a_name
= parse_identifier (C
);
1622 if (var
->a_name
== SLANG_ATOM_NULL
)
1628 /* simple variable declarator - just copy the specifier */
1629 if (!slang_type_specifier_copy (&var
->type
.specifier
, &type
->specifier
))
1632 case VARIABLE_INITIALIZER
:
1633 /* initialized variable - copy the specifier and parse the expression */
1634 if (!slang_type_specifier_copy (&var
->type
.specifier
, &type
->specifier
))
1636 var
->initializer
= (slang_operation
*) slang_alloc_malloc (sizeof (slang_operation
));
1637 if (var
->initializer
== NULL
)
1639 slang_info_log_memory (C
->L
);
1642 if (!slang_operation_construct (var
->initializer
))
1644 slang_alloc_free (var
->initializer
);
1645 var
->initializer
= NULL
;
1646 slang_info_log_memory (C
->L
);
1649 if (!parse_expression (C
, O
, var
->initializer
))
1653 case VARIABLE_ARRAY_UNKNOWN
:
1654 /* unsized array - mark it as array and copy the specifier to the array element */
1655 if (!convert_to_array (C
, var
, &type
->specifier
))
1659 case VARIABLE_ARRAY_EXPLICIT
:
1660 if (!convert_to_array (C
, var
, &type
->specifier
))
1662 if (!parse_array_len (C
, O
, &var
->array_len
))
1669 /* allocate global address space for a variable with a known size */
1670 if (C
->global_scope
&& !(var
->type
.specifier
.type
== slang_spec_array
&& var
->array_len
== 0))
1672 if (!calculate_var_size (C
, O
, var
))
1674 var
->address
= slang_var_pool_alloc (O
->global_pool
, var
->size
);
1677 /* initialize global variable */
1678 if (C
->global_scope
&& var
->initializer
!= NULL
)
1680 slang_assemble_ctx A
;
1682 A
.file
= O
->assembly
;
1683 A
.mach
= O
->machine
;
1685 A
.space
.funcs
= O
->funs
;
1686 A
.space
.structs
= O
->structs
;
1687 A
.space
.vars
= O
->vars
;
1688 if (!initialize_global (&A
, var
))
1694 static int parse_init_declarator_list (slang_parse_ctx
*C
, slang_output_ctx
*O
)
1696 slang_fully_specified_type type
;
1698 /* parse the fully specified type, common to all declarators */
1699 if (!slang_fully_specified_type_construct (&type
))
1701 if (!parse_fully_specified_type (C
, O
, &type
))
1703 slang_fully_specified_type_destruct (&type
);
1707 /* parse declarators, pass-in the parsed type */
1710 if (!parse_init_declarator (C
, O
, &type
))
1712 slang_fully_specified_type_destruct (&type
);
1716 while (*C
->I
++ == DECLARATOR_NEXT
);
1718 slang_fully_specified_type_destruct (&type
);
1722 static int parse_function (slang_parse_ctx
*C
, slang_output_ctx
*O
, int definition
,
1723 slang_function
**parsed_func_ret
)
1725 slang_function parsed_func
, *found_func
;
1727 /* parse function definition/declaration */
1728 if (!slang_function_construct (&parsed_func
))
1732 if (!parse_function_definition (C
, O
, &parsed_func
))
1734 slang_function_destruct (&parsed_func
);
1740 if (!parse_function_prototype (C
, O
, &parsed_func
))
1742 slang_function_destruct (&parsed_func
);
1747 /* find a function with a prototype matching the parsed one - only the current scope
1748 * is being searched to allow built-in function overriding */
1749 found_func
= slang_function_scope_find (O
->funs
, &parsed_func
, 0);
1750 if (found_func
== NULL
)
1752 /* add the parsed function to the function list */
1753 O
->funs
->functions
= (slang_function
*) slang_alloc_realloc (O
->funs
->functions
,
1754 O
->funs
->num_functions
* sizeof (slang_function
),
1755 (O
->funs
->num_functions
+ 1) * sizeof (slang_function
));
1756 if (O
->funs
->functions
== NULL
)
1758 slang_info_log_memory (C
->L
);
1759 slang_function_destruct (&parsed_func
);
1762 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
1763 O
->funs
->num_functions
++;
1765 /* return the newly parsed function */
1766 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
1770 /* TODO: check function return type qualifiers and specifiers */
1773 if (found_func
->body
!= NULL
)
1775 slang_info_log_error (C
->L
, "%s: function already has a body.",
1776 slang_atom_pool_id (C
->atoms
, parsed_func
.header
.a_name
));
1777 slang_function_destruct (&parsed_func
);
1781 /* destroy the existing function declaration and replace it with the new one,
1782 * remember to save the fixup table */
1783 parsed_func
.fixups
= found_func
->fixups
;
1784 slang_fixup_table_init (&found_func
->fixups
);
1785 slang_function_destruct (found_func
);
1786 *found_func
= parsed_func
;
1790 /* another declaration of the same function prototype - ignore it */
1791 slang_function_destruct (&parsed_func
);
1794 /* return the found function */
1795 *parsed_func_ret
= found_func
;
1798 /* assemble the parsed function */
1800 slang_assemble_ctx A
;
1802 A
.file
= O
->assembly
;
1803 A
.mach
= O
->machine
;
1805 A
.space
.funcs
= O
->funs
;
1806 A
.space
.structs
= O
->structs
;
1807 A
.space
.vars
= O
->vars
;
1808 if (!_slang_assemble_function (&A
, *parsed_func_ret
))
1815 #define DECLARATION_FUNCTION_PROTOTYPE 1
1816 #define DECLARATION_INIT_DECLARATOR_LIST 2
1818 static int parse_declaration (slang_parse_ctx
*C
, slang_output_ctx
*O
)
1822 case DECLARATION_INIT_DECLARATOR_LIST
:
1823 if (!parse_init_declarator_list (C
, O
))
1826 case DECLARATION_FUNCTION_PROTOTYPE
:
1828 slang_function
*dummy_func
;
1830 if (!parse_function (C
, O
, 0, &dummy_func
))
1840 /* external declaration */
1841 #define EXTERNAL_NULL 0
1842 #define EXTERNAL_FUNCTION_DEFINITION 1
1843 #define EXTERNAL_DECLARATION 2
1846 parse_code_unit (slang_parse_ctx
*C
, slang_code_unit
*unit
)
1850 /* setup output context */
1851 o
.funs
= &unit
->funs
;
1852 o
.structs
= &unit
->structs
;
1853 o
.vars
= &unit
->vars
;
1854 o
.assembly
= &unit
->object
->assembly
;
1855 o
.global_pool
= &unit
->object
->varpool
;
1856 o
.machine
= &unit
->object
->machine
;
1858 /* parse individual functions and declarations */
1859 while (*C
->I
!= EXTERNAL_NULL
)
1863 case EXTERNAL_FUNCTION_DEFINITION
:
1865 slang_function
*func
;
1867 if (!parse_function (C
, &o
, 1, &func
))
1871 case EXTERNAL_DECLARATION
:
1872 if (!parse_declaration (C
, &o
))
1883 #define BUILTIN_CORE 0
1884 #define BUILTIN_COMMON 1
1885 #define BUILTIN_TARGET 2
1886 #define BUILTIN_TOTAL 3
1889 compile_binary (const byte
*prod
, slang_code_unit
*unit
, slang_unit_type type
,
1890 slang_info_log
*infolog
, slang_code_unit
*builtin
, slang_code_unit
*downlink
)
1896 /* setup parse context */
1899 C
.parsing_builtin
= (builtin
== NULL
);
1900 C
.global_scope
= GL_TRUE
;
1901 C
.atoms
= &unit
->object
->atompool
;
1903 if (!check_revision (&C
))
1906 if (downlink
!= NULL
) {
1907 unit
->vars
.outer_scope
= &downlink
->vars
;
1908 unit
->funs
.outer_scope
= &downlink
->funs
;
1909 unit
->structs
.outer_scope
= &downlink
->structs
;
1912 /* parse translation unit */
1913 return parse_code_unit (&C
, unit
);
1917 compile_with_grammar (grammar id
, const char *source
, slang_code_unit
*unit
, slang_unit_type type
,
1918 slang_info_log
*infolog
, slang_code_unit
*builtin
)
1921 GLuint size
, start
, version
;
1923 /* retrieve version */
1924 if (!_slang_preprocess_version (source
, &version
, &start
, infolog
))
1927 /* check the syntax and generate its binary representation */
1928 if (!grammar_fast_check (id
, (const byte
*) source
+ start
, &prod
, &size
, 65536))
1932 grammar_get_last_error ( (unsigned char*) buf
, 1024, (int*) &pos
);
1933 slang_info_log_error (infolog
, buf
);
1937 /* syntax is okay - translate it to internal representation */
1938 if (!compile_binary (prod
, unit
, type
, infolog
, builtin
, &builtin
[BUILTIN_TARGET
])) {
1939 grammar_alloc_free (prod
);
1943 grammar_alloc_free (prod
);
1947 static const char *slang_shader_syn
=
1948 #include "library/slang_shader_syn.h"
1951 static const byte slang_core_gc
[] = {
1952 #include "library/slang_core_gc.h"
1955 static const byte slang_common_builtin_gc
[] = {
1956 #include "library/slang_common_builtin_gc.h"
1959 static const byte slang_fragment_builtin_gc
[] = {
1960 #include "library/slang_fragment_builtin_gc.h"
1963 static const byte slang_vertex_builtin_gc
[] = {
1964 #include "library/slang_vertex_builtin_gc.h"
1968 compile_object (grammar
*id
, const char *source
, slang_code_object
*object
, slang_unit_type type
,
1969 slang_info_log
*infolog
)
1971 slang_code_unit
*builtins
= NULL
;
1973 /* load GLSL grammar */
1974 *id
= grammar_load_from_text ((const byte
*) (slang_shader_syn
));
1980 grammar_get_last_error (buf
, 1024, &pos
);
1981 slang_info_log_error (infolog
, (const char *) (buf
));
1985 /* set shader type - the syntax is slightly different for different shaders */
1986 if (type
== slang_unit_fragment_shader
|| type
== slang_unit_fragment_builtin
)
1987 grammar_set_reg8 (*id
, (const byte
*) "shader_type", 1);
1989 grammar_set_reg8 (*id
, (const byte
*) "shader_type", 2);
1991 /* enable language extensions */
1992 grammar_set_reg8 (*id
, (const byte
*) "parsing_builtin", 1);
1994 /* if parsing user-specified shader, load built-in library */
1995 if (type
== slang_unit_fragment_shader
|| type
== slang_unit_vertex_shader
)
1997 /* compile core functionality first */
1998 if (!compile_binary (slang_core_gc
, &object
->builtin
[BUILTIN_CORE
],
1999 slang_unit_fragment_builtin
, infolog
, NULL
, NULL
))
2002 /* compile common functions and variables, link to core */
2003 if (!compile_binary (slang_common_builtin_gc
, &object
->builtin
[BUILTIN_COMMON
],
2004 slang_unit_fragment_builtin
, infolog
, NULL
,
2005 &object
->builtin
[BUILTIN_CORE
]))
2008 /* compile target-specific functions and variables, link to common */
2009 if (type
== slang_unit_fragment_shader
)
2011 if (!compile_binary (slang_fragment_builtin_gc
, &object
->builtin
[BUILTIN_TARGET
],
2012 slang_unit_fragment_builtin
, infolog
, NULL
,
2013 &object
->builtin
[BUILTIN_COMMON
]))
2016 else if (type
== slang_unit_vertex_shader
)
2018 if (!compile_binary (slang_vertex_builtin_gc
, &object
->builtin
[BUILTIN_TARGET
],
2019 slang_unit_vertex_builtin
, infolog
, NULL
,
2020 &object
->builtin
[BUILTIN_COMMON
]))
2024 /* disable language extensions */
2025 grammar_set_reg8 (*id
, (const byte
*) "parsing_builtin", 0);
2026 builtins
= object
->builtin
;
2029 /* compile the actual shader - pass-in built-in library for external shader */
2030 return compile_with_grammar (*id
, source
, &object
->unit
, type
, infolog
, builtins
);
2034 _slang_compile (const char *source
, slang_code_object
*object
, slang_unit_type type
,
2035 slang_info_log
*infolog
)
2040 _slang_code_object_dtr (object
);
2041 _slang_code_object_ctr (object
);
2043 success
= compile_object (&id
, source
, object
, type
, infolog
);
2045 grammar_destroy (id
);
2049 if (!_slang_build_export_data_table (&object
->expdata
, &object
->unit
.vars
))
2051 if (!_slang_build_export_code_table (&object
->expcode
, &object
->unit
.funs
, &object
->unit
))
2054 #if defined(USE_X86_ASM) || defined(SLANG_X86)
2055 /* XXX: lookup the @main label */
2056 if (!_slang_x86_codegen (&object
->machine
, &object
->assembly
, object
->expcode
.entries
[0].address
))