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
)
85 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
86 _slang_code_unit_ctr (&self
->builtin
[i
], self
);
87 _slang_code_unit_ctr (&self
->unit
, self
);
88 _slang_assembly_file_ctr (&self
->assembly
);
89 slang_machine_ctr (&self
->machine
);
90 self
->varpool
.next_addr
= 0;
91 slang_atom_pool_construct (&self
->atompool
);
92 slang_export_data_table_ctr (&self
->expdata
);
93 self
->expdata
.atoms
= &self
->atompool
;
94 slang_export_code_table_ctr (&self
->expcode
);
95 self
->expcode
.atoms
= &self
->atompool
;
99 _slang_code_object_dtr (slang_code_object
*self
)
103 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
104 _slang_code_unit_dtr (&self
->builtin
[i
]);
105 _slang_code_unit_dtr (&self
->unit
);
106 slang_assembly_file_destruct (&self
->assembly
);
107 slang_machine_dtr (&self
->machine
);
108 slang_atom_pool_destruct (&self
->atompool
);
109 slang_export_data_table_dtr (&self
->expdata
);
110 slang_export_code_table_ctr (&self
->expcode
);
115 static char *out_of_memory
= "Error: Out of memory.\n";
117 void slang_info_log_construct (slang_info_log
*log
)
120 log
->dont_free_text
= 0;
123 void slang_info_log_destruct (slang_info_log
*log
)
125 if (!log
->dont_free_text
)
126 slang_alloc_free (log
->text
);
129 static int slang_info_log_message (slang_info_log
*log
, const char *prefix
, const char *msg
)
133 if (log
->dont_free_text
)
135 size
= slang_string_length (msg
) + 2;
137 size
+= slang_string_length (prefix
) + 2;
138 if (log
->text
!= NULL
) {
139 GLuint old_len
= slang_string_length (log
->text
);
140 log
->text
= (char *) (slang_alloc_realloc (log
->text
, old_len
+ 1, old_len
+ size
));
143 log
->text
= (char *) (slang_alloc_malloc (size
));
144 if (log
->text
!= NULL
)
147 if (log
->text
== NULL
)
149 if (prefix
!= NULL
) {
150 slang_string_concat (log
->text
, prefix
);
151 slang_string_concat (log
->text
, ": ");
153 slang_string_concat (log
->text
, msg
);
154 slang_string_concat (log
->text
, "\n");
158 int slang_info_log_print (slang_info_log
*log
, const char *msg
, ...)
164 _mesa_vsprintf (buf
, msg
, va
);
166 return slang_info_log_message (log
, NULL
, buf
);
169 int slang_info_log_error (slang_info_log
*log
, const char *msg
, ...)
175 _mesa_vsprintf (buf
, msg
, va
);
177 if (slang_info_log_message (log
, "Error", buf
))
179 slang_info_log_memory (log
);
183 int slang_info_log_warning (slang_info_log
*log
, const char *msg
, ...)
189 _mesa_vsprintf (buf
, msg
, va
);
191 if (slang_info_log_message (log
, "Warning", buf
))
193 slang_info_log_memory (log
);
197 void slang_info_log_memory (slang_info_log
*log
)
199 if (!slang_info_log_message (log
, "Error", "Out of memory."))
201 log
->dont_free_text
= 1;
202 log
->text
= out_of_memory
;
206 /* slang_parse_ctx */
208 typedef struct slang_parse_ctx_
214 slang_atom_pool
*atoms
;
217 /* slang_output_ctx */
219 typedef struct slang_output_ctx_
221 slang_variable_scope
*vars
;
222 slang_function_scope
*funs
;
223 slang_struct_scope
*structs
;
224 slang_assembly_file
*assembly
;
225 slang_var_pool
*global_pool
;
226 slang_machine
*machine
;
229 /* _slang_compile() */
231 static void parse_identifier_str (slang_parse_ctx
*C
, char **id
)
234 C
->I
+= _mesa_strlen (*id
) + 1;
237 static slang_atom
parse_identifier (slang_parse_ctx
*C
)
241 id
= (const char *) C
->I
;
242 C
->I
+= _mesa_strlen (id
) + 1;
243 return slang_atom_pool_atom (C
->atoms
, id
);
246 static int parse_number (slang_parse_ctx
*C
, int *number
)
248 const int radix
= (int) (*C
->I
++);
250 while (*C
->I
!= '\0')
253 if (*C
->I
>= '0' && *C
->I
<= '9')
254 digit
= (int) (*C
->I
- '0');
255 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
256 digit
= (int) (*C
->I
- 'A') + 10;
258 digit
= (int) (*C
->I
- 'a') + 10;
259 *number
= *number
* radix
+ digit
;
264 slang_info_log_warning (C
->L
, "%d: literal integer overflow.", *number
);
268 static int parse_float (slang_parse_ctx
*C
, float *number
)
270 char *integral
= NULL
;
271 char *fractional
= NULL
;
272 char *exponent
= NULL
;
275 parse_identifier_str (C
, &integral
);
276 parse_identifier_str (C
, &fractional
);
277 parse_identifier_str (C
, &exponent
);
279 whole
= (char *) (slang_alloc_malloc ((_mesa_strlen (integral
) + _mesa_strlen (fractional
) +
280 _mesa_strlen (exponent
) + 3) * sizeof (char)));
283 slang_info_log_memory (C
->L
);
287 slang_string_copy (whole
, integral
);
288 slang_string_concat (whole
, ".");
289 slang_string_concat (whole
, fractional
);
290 slang_string_concat (whole
, "E");
291 slang_string_concat (whole
, exponent
);
293 *number
= (float) (_mesa_strtod(whole
, (char **)NULL
));
295 slang_alloc_free (whole
);
299 /* revision number - increment after each change affecting emitted output */
302 static int check_revision (slang_parse_ctx
*C
)
304 if (*C
->I
!= REVISION
)
306 slang_info_log_error (C
->L
, "Internal compiler error.");
313 static int parse_statement (slang_parse_ctx
*, slang_output_ctx
*, slang_operation
*);
314 static int parse_expression (slang_parse_ctx
*, slang_output_ctx
*, slang_operation
*);
315 static int parse_type_specifier (slang_parse_ctx
*, slang_output_ctx
*, slang_type_specifier
*);
317 static GLboolean
parse_array_len (slang_parse_ctx
*C
, slang_output_ctx
*O
, GLuint
*len
)
319 slang_operation array_size
;
320 slang_assembly_name_space space
;
323 if (!slang_operation_construct (&array_size
))
325 if (!parse_expression (C
, O
, &array_size
))
327 slang_operation_destruct (&array_size
);
331 space
.funcs
= O
->funs
;
332 space
.structs
= O
->structs
;
333 space
.vars
= O
->vars
;
334 result
= _slang_evaluate_int (O
->assembly
, O
->machine
, &space
, &array_size
, len
, C
->atoms
);
335 slang_operation_destruct (&array_size
);
339 static GLboolean
calculate_var_size (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_variable
*var
)
341 slang_storage_aggregate agg
;
343 if (!slang_storage_aggregate_construct (&agg
))
345 if (!_slang_aggregate_variable (&agg
, &var
->type
.specifier
, var
->array_len
, O
->funs
, O
->structs
,
346 O
->vars
, O
->machine
, O
->assembly
, C
->atoms
))
348 slang_storage_aggregate_destruct (&agg
);
351 var
->size
= _slang_sizeof_aggregate (&agg
);
352 slang_storage_aggregate_destruct (&agg
);
356 static GLboolean
convert_to_array (slang_parse_ctx
*C
, slang_variable
*var
,
357 const slang_type_specifier
*sp
)
359 /* sized array - mark it as array, copy the specifier to the array element and
360 * parse the expression */
361 var
->type
.specifier
.type
= slang_spec_array
;
362 var
->type
.specifier
._array
= (slang_type_specifier
*) slang_alloc_malloc (sizeof (
363 slang_type_specifier
));
364 if (var
->type
.specifier
._array
== NULL
)
366 slang_info_log_memory (C
->L
);
369 slang_type_specifier_ctr (var
->type
.specifier
._array
);
370 return slang_type_specifier_copy (var
->type
.specifier
._array
, sp
);
373 /* structure field */
376 #define FIELD_ARRAY 2
378 static GLboolean
parse_struct_field_var (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_variable
*var
,
379 const slang_type_specifier
*sp
)
381 var
->a_name
= parse_identifier (C
);
382 if (var
->a_name
== SLANG_ATOM_NULL
)
388 if (!slang_type_specifier_copy (&var
->type
.specifier
, sp
))
392 if (!convert_to_array (C
, var
, sp
))
394 if (!parse_array_len (C
, O
, &var
->array_len
))
401 return calculate_var_size (C
, O
, var
);
404 static int parse_struct_field (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_struct
*st
,
405 slang_type_specifier
*sp
)
407 slang_output_ctx o
= *O
;
409 o
.structs
= st
->structs
;
410 if (!parse_type_specifier (C
, &o
, sp
))
416 st
->fields
->variables
= (slang_variable
*) slang_alloc_realloc (st
->fields
->variables
,
417 st
->fields
->num_variables
* sizeof (slang_variable
),
418 (st
->fields
->num_variables
+ 1) * sizeof (slang_variable
));
419 if (st
->fields
->variables
== NULL
)
421 slang_info_log_memory (C
->L
);
424 var
= &st
->fields
->variables
[st
->fields
->num_variables
];
425 if (!slang_variable_construct (var
))
427 st
->fields
->num_variables
++;
428 if (!parse_struct_field_var (C
, &o
, var
, sp
))
431 while (*C
->I
++ != FIELD_NONE
);
436 static int parse_struct (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_struct
**st
)
441 /* parse struct name (if any) and make sure it is unique in current scope */
442 a_name
= parse_identifier (C
);
443 if (a_name
== SLANG_ATOM_NULL
)
445 name
= slang_atom_pool_id (C
->atoms
, a_name
);
446 if (name
[0] != '\0' && slang_struct_scope_find (O
->structs
, a_name
, 0) != NULL
)
448 slang_info_log_error (C
->L
, "%s: duplicate type name.", name
);
452 /* set-up a new struct */
453 *st
= (slang_struct
*) slang_alloc_malloc (sizeof (slang_struct
));
456 slang_info_log_memory (C
->L
);
459 if (!slang_struct_construct (*st
))
461 slang_alloc_free (*st
);
463 slang_info_log_memory (C
->L
);
466 (**st
).a_name
= a_name
;
467 (**st
).structs
->outer_scope
= O
->structs
;
469 /* parse individual struct fields */
472 slang_type_specifier sp
;
474 slang_type_specifier_ctr (&sp
);
475 if (!parse_struct_field (C
, O
, *st
, &sp
))
477 slang_type_specifier_dtr (&sp
);
480 slang_type_specifier_dtr (&sp
);
482 while (*C
->I
++ != FIELD_NONE
);
484 /* if named struct, copy it to current scope */
489 O
->structs
->structs
= (slang_struct
*) slang_alloc_realloc (O
->structs
->structs
,
490 O
->structs
->num_structs
* sizeof (slang_struct
),
491 (O
->structs
->num_structs
+ 1) * sizeof (slang_struct
));
492 if (O
->structs
->structs
== NULL
)
494 slang_info_log_memory (C
->L
);
497 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
498 if (!slang_struct_construct (s
))
500 O
->structs
->num_structs
++;
501 if (!slang_struct_copy (s
, *st
))
509 #define TYPE_QUALIFIER_NONE 0
510 #define TYPE_QUALIFIER_CONST 1
511 #define TYPE_QUALIFIER_ATTRIBUTE 2
512 #define TYPE_QUALIFIER_VARYING 3
513 #define TYPE_QUALIFIER_UNIFORM 4
514 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
515 #define TYPE_QUALIFIER_FIXEDINPUT 6
517 static int parse_type_qualifier (slang_parse_ctx
*C
, slang_type_qualifier
*qual
)
521 case TYPE_QUALIFIER_NONE
:
522 *qual
= slang_qual_none
;
524 case TYPE_QUALIFIER_CONST
:
525 *qual
= slang_qual_const
;
527 case TYPE_QUALIFIER_ATTRIBUTE
:
528 *qual
= slang_qual_attribute
;
530 case TYPE_QUALIFIER_VARYING
:
531 *qual
= slang_qual_varying
;
533 case TYPE_QUALIFIER_UNIFORM
:
534 *qual
= slang_qual_uniform
;
536 case TYPE_QUALIFIER_FIXEDOUTPUT
:
537 *qual
= slang_qual_fixedoutput
;
539 case TYPE_QUALIFIER_FIXEDINPUT
:
540 *qual
= slang_qual_fixedinput
;
549 #define TYPE_SPECIFIER_VOID 0
550 #define TYPE_SPECIFIER_BOOL 1
551 #define TYPE_SPECIFIER_BVEC2 2
552 #define TYPE_SPECIFIER_BVEC3 3
553 #define TYPE_SPECIFIER_BVEC4 4
554 #define TYPE_SPECIFIER_INT 5
555 #define TYPE_SPECIFIER_IVEC2 6
556 #define TYPE_SPECIFIER_IVEC3 7
557 #define TYPE_SPECIFIER_IVEC4 8
558 #define TYPE_SPECIFIER_FLOAT 9
559 #define TYPE_SPECIFIER_VEC2 10
560 #define TYPE_SPECIFIER_VEC3 11
561 #define TYPE_SPECIFIER_VEC4 12
562 #define TYPE_SPECIFIER_MAT2 13
563 #define TYPE_SPECIFIER_MAT3 14
564 #define TYPE_SPECIFIER_MAT4 15
565 #define TYPE_SPECIFIER_SAMPLER1D 16
566 #define TYPE_SPECIFIER_SAMPLER2D 17
567 #define TYPE_SPECIFIER_SAMPLER3D 18
568 #define TYPE_SPECIFIER_SAMPLERCUBE 19
569 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
570 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
571 #define TYPE_SPECIFIER_STRUCT 22
572 #define TYPE_SPECIFIER_TYPENAME 23
574 static int parse_type_specifier (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_type_specifier
*spec
)
578 case TYPE_SPECIFIER_VOID
:
579 spec
->type
= slang_spec_void
;
581 case TYPE_SPECIFIER_BOOL
:
582 spec
->type
= slang_spec_bool
;
584 case TYPE_SPECIFIER_BVEC2
:
585 spec
->type
= slang_spec_bvec2
;
587 case TYPE_SPECIFIER_BVEC3
:
588 spec
->type
= slang_spec_bvec3
;
590 case TYPE_SPECIFIER_BVEC4
:
591 spec
->type
= slang_spec_bvec4
;
593 case TYPE_SPECIFIER_INT
:
594 spec
->type
= slang_spec_int
;
596 case TYPE_SPECIFIER_IVEC2
:
597 spec
->type
= slang_spec_ivec2
;
599 case TYPE_SPECIFIER_IVEC3
:
600 spec
->type
= slang_spec_ivec3
;
602 case TYPE_SPECIFIER_IVEC4
:
603 spec
->type
= slang_spec_ivec4
;
605 case TYPE_SPECIFIER_FLOAT
:
606 spec
->type
= slang_spec_float
;
608 case TYPE_SPECIFIER_VEC2
:
609 spec
->type
= slang_spec_vec2
;
611 case TYPE_SPECIFIER_VEC3
:
612 spec
->type
= slang_spec_vec3
;
614 case TYPE_SPECIFIER_VEC4
:
615 spec
->type
= slang_spec_vec4
;
617 case TYPE_SPECIFIER_MAT2
:
618 spec
->type
= slang_spec_mat2
;
620 case TYPE_SPECIFIER_MAT3
:
621 spec
->type
= slang_spec_mat3
;
623 case TYPE_SPECIFIER_MAT4
:
624 spec
->type
= slang_spec_mat4
;
626 case TYPE_SPECIFIER_SAMPLER1D
:
627 spec
->type
= slang_spec_sampler1D
;
629 case TYPE_SPECIFIER_SAMPLER2D
:
630 spec
->type
= slang_spec_sampler2D
;
632 case TYPE_SPECIFIER_SAMPLER3D
:
633 spec
->type
= slang_spec_sampler3D
;
635 case TYPE_SPECIFIER_SAMPLERCUBE
:
636 spec
->type
= slang_spec_samplerCube
;
638 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
639 spec
->type
= slang_spec_sampler1DShadow
;
641 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
642 spec
->type
= slang_spec_sampler2DShadow
;
644 case TYPE_SPECIFIER_STRUCT
:
645 spec
->type
= slang_spec_struct
;
646 if (!parse_struct (C
, O
, &spec
->_struct
))
649 case TYPE_SPECIFIER_TYPENAME
:
650 spec
->type
= slang_spec_struct
;
655 a_name
= parse_identifier (C
);
659 stru
= slang_struct_scope_find (O
->structs
, a_name
, 1);
662 slang_info_log_error (C
->L
, "%s: undeclared type name.",
663 slang_atom_pool_id (C
->atoms
, a_name
));
667 spec
->_struct
= (slang_struct
*) slang_alloc_malloc (sizeof (slang_struct
));
668 if (spec
->_struct
== NULL
)
670 slang_info_log_memory (C
->L
);
673 if (!slang_struct_construct (spec
->_struct
))
675 slang_alloc_free (spec
->_struct
);
676 spec
->_struct
= NULL
;
679 if (!slang_struct_copy (spec
->_struct
, stru
))
689 static int parse_fully_specified_type (slang_parse_ctx
*C
, slang_output_ctx
*O
,
690 slang_fully_specified_type
*type
)
692 if (!parse_type_qualifier (C
, &type
->qualifier
))
694 return parse_type_specifier (C
, O
, &type
->specifier
);
699 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
700 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
704 #define OP_CONTINUE 6
707 #define OP_EXPRESSION 9
712 #define OP_PUSH_VOID 14
713 #define OP_PUSH_BOOL 15
714 #define OP_PUSH_INT 16
715 #define OP_PUSH_FLOAT 17
716 #define OP_PUSH_IDENTIFIER 18
717 #define OP_SEQUENCE 19
719 #define OP_ADDASSIGN 21
720 #define OP_SUBASSIGN 22
721 #define OP_MULASSIGN 23
722 #define OP_DIVASSIGN 24
723 /*#define OP_MODASSIGN 25*/
724 /*#define OP_LSHASSIGN 26*/
725 /*#define OP_RSHASSIGN 27*/
726 /*#define OP_ORASSIGN 28*/
727 /*#define OP_XORASSIGN 29*/
728 /*#define OP_ANDASSIGN 30*/
730 #define OP_LOGICALOR 32
731 #define OP_LOGICALXOR 33
732 #define OP_LOGICALAND 34
733 /*#define OP_BITOR 35*/
734 /*#define OP_BITXOR 36*/
735 /*#define OP_BITAND 37*/
737 #define OP_NOTEQUAL 39
739 #define OP_GREATER 41
740 #define OP_LESSEQUAL 42
741 #define OP_GREATEREQUAL 43
742 /*#define OP_LSHIFT 44*/
743 /*#define OP_RSHIFT 45*/
745 #define OP_SUBTRACT 47
746 #define OP_MULTIPLY 48
748 /*#define OP_MODULUS 50*/
749 #define OP_PREINCREMENT 51
750 #define OP_PREDECREMENT 52
753 /*#define OP_COMPLEMENT 55*/
755 #define OP_SUBSCRIPT 57
758 #define OP_POSTINCREMENT 60
759 #define OP_POSTDECREMENT 61
761 static int parse_child_operation (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_operation
*oper
,
766 oper
->children
= (slang_operation
*) slang_alloc_realloc (oper
->children
,
767 oper
->num_children
* sizeof (slang_operation
),
768 (oper
->num_children
+ 1) * sizeof (slang_operation
));
769 if (oper
->children
== NULL
)
771 slang_info_log_memory (C
->L
);
774 ch
= &oper
->children
[oper
->num_children
];
775 if (!slang_operation_construct (ch
))
777 slang_info_log_memory (C
->L
);
780 oper
->num_children
++;
782 return parse_statement (C
, O
, ch
);
783 return parse_expression (C
, O
, ch
);
786 static int parse_declaration (slang_parse_ctx
*C
, slang_output_ctx
*O
);
788 static int parse_statement (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_operation
*oper
)
790 oper
->locals
->outer_scope
= O
->vars
;
793 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
794 /* parse child statements, do not create new variable scope */
795 oper
->type
= slang_oper_block_no_new_scope
;
796 while (*C
->I
!= OP_END
)
797 if (!parse_child_operation (C
, O
, oper
, 1))
801 case OP_BLOCK_BEGIN_NEW_SCOPE
:
802 /* parse child statements, create new variable scope */
804 slang_output_ctx o
= *O
;
806 oper
->type
= slang_oper_block_new_scope
;
807 o
.vars
= oper
->locals
;
808 while (*C
->I
!= OP_END
)
809 if (!parse_child_operation (C
, &o
, oper
, 1))
815 /* local variable declaration, individual declarators are stored as children identifiers */
816 oper
->type
= slang_oper_variable_decl
;
818 const unsigned int first_var
= O
->vars
->num_variables
;
820 /* parse the declaration, note that there can be zero or more than one declarators */
821 if (!parse_declaration (C
, O
))
823 if (first_var
< O
->vars
->num_variables
)
825 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
828 oper
->children
= (slang_operation
*) slang_alloc_malloc (num_vars
* sizeof (
830 if (oper
->children
== NULL
)
832 slang_info_log_memory (C
->L
);
835 for (oper
->num_children
= 0; oper
->num_children
< num_vars
; oper
->num_children
++)
836 if (!slang_operation_construct (&oper
->children
[oper
->num_children
]))
838 slang_info_log_memory (C
->L
);
841 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++)
843 slang_operation
*o
= &oper
->children
[i
- first_var
];
845 o
->type
= slang_oper_identifier
;
846 o
->locals
->outer_scope
= O
->vars
;
847 o
->a_id
= O
->vars
->variables
[i
].a_name
;
853 /* the __asm statement, parse the mnemonic and all its arguments as expressions */
854 oper
->type
= slang_oper_asm
;
855 oper
->a_id
= parse_identifier (C
);
856 if (oper
->a_id
== SLANG_ATOM_NULL
)
858 while (*C
->I
!= OP_END
)
859 if (!parse_child_operation (C
, O
, oper
, 0))
864 oper
->type
= slang_oper_break
;
867 oper
->type
= slang_oper_continue
;
870 oper
->type
= slang_oper_discard
;
873 oper
->type
= slang_oper_return
;
874 if (!parse_child_operation (C
, O
, oper
, 0))
878 oper
->type
= slang_oper_expression
;
879 if (!parse_child_operation (C
, O
, oper
, 0))
883 oper
->type
= slang_oper_if
;
884 if (!parse_child_operation (C
, O
, oper
, 0))
886 if (!parse_child_operation (C
, O
, oper
, 1))
888 if (!parse_child_operation (C
, O
, oper
, 1))
893 slang_output_ctx o
= *O
;
895 oper
->type
= slang_oper_while
;
896 o
.vars
= oper
->locals
;
897 if (!parse_child_operation (C
, &o
, oper
, 1))
899 if (!parse_child_operation (C
, &o
, oper
, 1))
904 oper
->type
= slang_oper_do
;
905 if (!parse_child_operation (C
, O
, oper
, 1))
907 if (!parse_child_operation (C
, O
, oper
, 0))
912 slang_output_ctx o
= *O
;
914 oper
->type
= slang_oper_for
;
915 o
.vars
= oper
->locals
;
916 if (!parse_child_operation (C
, &o
, oper
, 1))
918 if (!parse_child_operation (C
, &o
, oper
, 1))
920 if (!parse_child_operation (C
, &o
, oper
, 0))
922 if (!parse_child_operation (C
, &o
, oper
, 1))
932 static int handle_nary_expression (slang_parse_ctx
*C
, slang_operation
*op
, slang_operation
**ops
,
933 unsigned int *total_ops
, unsigned int n
)
937 op
->children
= (slang_operation
*) slang_alloc_malloc (n
* sizeof (slang_operation
));
938 if (op
->children
== NULL
)
940 slang_info_log_memory (C
->L
);
943 op
->num_children
= n
;
945 for (i
= 0; i
< n
; i
++)
946 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
947 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
950 *ops
= (slang_operation
*) slang_alloc_realloc (*ops
, (*total_ops
+ n
) * sizeof (slang_operation
),
951 *total_ops
* sizeof (slang_operation
));
954 slang_info_log_memory (C
->L
);
960 static int is_constructor_name (const char *name
, slang_atom a_name
, slang_struct_scope
*structs
)
962 if (slang_type_specifier_type_from_string (name
) != slang_spec_void
)
964 return slang_struct_scope_find (structs
, a_name
, 1) != NULL
;
967 static int parse_expression (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_operation
*oper
)
969 slang_operation
*ops
= NULL
;
970 unsigned int num_ops
= 0;
973 while (*C
->I
!= OP_END
)
976 const unsigned int op_code
= *C
->I
++;
978 /* allocate default operation, becomes a no-op if not used */
979 ops
= (slang_operation
*) slang_alloc_realloc (ops
,
980 num_ops
* sizeof (slang_operation
), (num_ops
+ 1) * sizeof (slang_operation
));
983 slang_info_log_memory (C
->L
);
987 if (!slang_operation_construct (op
))
989 slang_info_log_memory (C
->L
);
993 op
->locals
->outer_scope
= O
->vars
;
998 op
->type
= slang_oper_void
;
1001 op
->type
= slang_oper_literal_bool
;
1002 if (!parse_number (C
, &number
))
1004 op
->literal
= (GLfloat
) number
;
1007 op
->type
= slang_oper_literal_int
;
1008 if (!parse_number (C
, &number
))
1010 op
->literal
= (GLfloat
) number
;
1013 op
->type
= slang_oper_literal_float
;
1014 if (!parse_float (C
, &op
->literal
))
1017 case OP_PUSH_IDENTIFIER
:
1018 op
->type
= slang_oper_identifier
;
1019 op
->a_id
= parse_identifier (C
);
1020 if (op
->a_id
== SLANG_ATOM_NULL
)
1024 op
->type
= slang_oper_sequence
;
1025 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1029 op
->type
= slang_oper_assign
;
1030 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1034 op
->type
= slang_oper_addassign
;
1035 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1039 op
->type
= slang_oper_subassign
;
1040 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1044 op
->type
= slang_oper_mulassign
;
1045 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1049 op
->type
= slang_oper_divassign
;
1050 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1053 /*case OP_MODASSIGN:*/
1054 /*case OP_LSHASSIGN:*/
1055 /*case OP_RSHASSIGN:*/
1056 /*case OP_ORASSIGN:*/
1057 /*case OP_XORASSIGN:*/
1058 /*case OP_ANDASSIGN:*/
1060 op
->type
= slang_oper_select
;
1061 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 3))
1065 op
->type
= slang_oper_logicalor
;
1066 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1070 op
->type
= slang_oper_logicalxor
;
1071 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1075 op
->type
= slang_oper_logicaland
;
1076 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1083 op
->type
= slang_oper_equal
;
1084 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1088 op
->type
= slang_oper_notequal
;
1089 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1093 op
->type
= slang_oper_less
;
1094 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1098 op
->type
= slang_oper_greater
;
1099 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1103 op
->type
= slang_oper_lessequal
;
1104 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1107 case OP_GREATEREQUAL
:
1108 op
->type
= slang_oper_greaterequal
;
1109 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1115 op
->type
= slang_oper_add
;
1116 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1120 op
->type
= slang_oper_subtract
;
1121 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1125 op
->type
= slang_oper_multiply
;
1126 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1130 op
->type
= slang_oper_divide
;
1131 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1134 /*case OP_MODULUS:*/
1135 case OP_PREINCREMENT
:
1136 op
->type
= slang_oper_preincrement
;
1137 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 1))
1140 case OP_PREDECREMENT
:
1141 op
->type
= slang_oper_predecrement
;
1142 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 1))
1146 op
->type
= slang_oper_plus
;
1147 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 1))
1151 op
->type
= slang_oper_minus
;
1152 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 1))
1156 op
->type
= slang_oper_not
;
1157 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 1))
1160 /*case OP_COMPLEMENT:*/
1162 op
->type
= slang_oper_subscript
;
1163 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 2))
1167 op
->type
= slang_oper_call
;
1168 op
->a_id
= parse_identifier (C
);
1169 if (op
->a_id
== SLANG_ATOM_NULL
)
1171 while (*C
->I
!= OP_END
)
1172 if (!parse_child_operation (C
, O
, op
, 0))
1175 if (!C
->parsing_builtin
&& !slang_function_scope_find_by_name (O
->funs
, op
->a_id
, 1))
1179 id
= slang_atom_pool_id (C
->atoms
, op
->a_id
);
1180 if (!is_constructor_name (id
, op
->a_id
, O
->structs
))
1182 slang_info_log_error (C
->L
, "%s: undeclared function name.", id
);
1188 op
->type
= slang_oper_field
;
1189 op
->a_id
= parse_identifier (C
);
1190 if (op
->a_id
== SLANG_ATOM_NULL
)
1192 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 1))
1195 case OP_POSTINCREMENT
:
1196 op
->type
= slang_oper_postincrement
;
1197 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 1))
1200 case OP_POSTDECREMENT
:
1201 op
->type
= slang_oper_postdecrement
;
1202 if (!handle_nary_expression (C
, op
, &ops
, &num_ops
, 1))
1212 slang_alloc_free (ops
);
1216 /* parameter qualifier */
1217 #define PARAM_QUALIFIER_IN 0
1218 #define PARAM_QUALIFIER_OUT 1
1219 #define PARAM_QUALIFIER_INOUT 2
1221 /* function parameter array presence */
1222 #define PARAMETER_ARRAY_NOT_PRESENT 0
1223 #define PARAMETER_ARRAY_PRESENT 1
1225 static int parse_parameter_declaration (slang_parse_ctx
*C
, slang_output_ctx
*O
,
1226 slang_variable
*param
)
1228 /* parse and validate the parameter's type qualifiers (there can be two at most) because
1229 * not all combinations are valid */
1230 if (!parse_type_qualifier (C
, ¶m
->type
.qualifier
))
1234 case PARAM_QUALIFIER_IN
:
1235 if (param
->type
.qualifier
!= slang_qual_const
&& param
->type
.qualifier
!= slang_qual_none
)
1237 slang_info_log_error (C
->L
, "Invalid type qualifier.");
1241 case PARAM_QUALIFIER_OUT
:
1242 if (param
->type
.qualifier
== slang_qual_none
)
1243 param
->type
.qualifier
= slang_qual_out
;
1246 slang_info_log_error (C
->L
, "Invalid type qualifier.");
1250 case PARAM_QUALIFIER_INOUT
:
1251 if (param
->type
.qualifier
== slang_qual_none
)
1252 param
->type
.qualifier
= slang_qual_inout
;
1255 slang_info_log_error (C
->L
, "Invalid type qualifier.");
1263 /* parse parameter's type specifier and name */
1264 if (!parse_type_specifier (C
, O
, ¶m
->type
.specifier
))
1266 param
->a_name
= parse_identifier (C
);
1267 if (param
->a_name
== SLANG_ATOM_NULL
)
1270 /* if the parameter is an array, parse its size (the size must be explicitly defined */
1271 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
)
1273 slang_type_specifier p
;
1275 slang_type_specifier_ctr (&p
);
1276 if (!slang_type_specifier_copy (&p
, ¶m
->type
.specifier
))
1278 slang_type_specifier_dtr (&p
);
1281 if (!convert_to_array (C
, param
, &p
))
1283 slang_type_specifier_dtr (&p
);
1286 slang_type_specifier_dtr (&p
);
1287 if (!parse_array_len (C
, O
, ¶m
->array_len
))
1291 /* calculate the parameter size */
1292 if (!calculate_var_size (C
, O
, param
))
1295 /* TODO: allocate the local address here? */
1300 #define FUNCTION_ORDINARY 0
1301 #define FUNCTION_CONSTRUCTOR 1
1302 #define FUNCTION_OPERATOR 2
1304 /* function parameter */
1305 #define PARAMETER_NONE 0
1306 #define PARAMETER_NEXT 1
1309 #define OPERATOR_ADDASSIGN 1
1310 #define OPERATOR_SUBASSIGN 2
1311 #define OPERATOR_MULASSIGN 3
1312 #define OPERATOR_DIVASSIGN 4
1313 /*#define OPERATOR_MODASSIGN 5*/
1314 /*#define OPERATOR_LSHASSIGN 6*/
1315 /*#define OPERATOR_RSHASSIGN 7*/
1316 /*#define OPERATOR_ANDASSIGN 8*/
1317 /*#define OPERATOR_XORASSIGN 9*/
1318 /*#define OPERATOR_ORASSIGN 10*/
1319 #define OPERATOR_LOGICALXOR 11
1320 /*#define OPERATOR_BITOR 12*/
1321 /*#define OPERATOR_BITXOR 13*/
1322 /*#define OPERATOR_BITAND 14*/
1323 #define OPERATOR_LESS 15
1324 #define OPERATOR_GREATER 16
1325 #define OPERATOR_LESSEQUAL 17
1326 #define OPERATOR_GREATEREQUAL 18
1327 /*#define OPERATOR_LSHIFT 19*/
1328 /*#define OPERATOR_RSHIFT 20*/
1329 #define OPERATOR_MULTIPLY 21
1330 #define OPERATOR_DIVIDE 22
1331 /*#define OPERATOR_MODULUS 23*/
1332 #define OPERATOR_INCREMENT 24
1333 #define OPERATOR_DECREMENT 25
1334 #define OPERATOR_PLUS 26
1335 #define OPERATOR_MINUS 27
1336 /*#define OPERATOR_COMPLEMENT 28*/
1337 #define OPERATOR_NOT 29
1339 static const struct {
1340 unsigned int o_code
;
1342 } operator_names
[] = {
1343 { OPERATOR_INCREMENT
, "++" },
1344 { OPERATOR_ADDASSIGN
, "+=" },
1345 { OPERATOR_PLUS
, "+" },
1346 { OPERATOR_DECREMENT
, "--" },
1347 { OPERATOR_SUBASSIGN
, "-=" },
1348 { OPERATOR_MINUS
, "-" },
1349 { OPERATOR_NOT
, "!" },
1350 { OPERATOR_MULASSIGN
, "*=" },
1351 { OPERATOR_MULTIPLY
, "*" },
1352 { OPERATOR_DIVASSIGN
, "/=" },
1353 { OPERATOR_DIVIDE
, "/" },
1354 { OPERATOR_LESSEQUAL
, "<=" },
1355 /*{ OPERATOR_LSHASSIGN, "<<=" },*/
1356 /*{ OPERATOR_LSHIFT, "<<" },*/
1357 { OPERATOR_LESS
, "<" },
1358 { OPERATOR_GREATEREQUAL
, ">=" },
1359 /*{ OPERATOR_RSHASSIGN, ">>=" },*/
1360 /*{ OPERATOR_RSHIFT, ">>" },*/
1361 { OPERATOR_GREATER
, ">" },
1362 /*{ OPERATOR_MODASSIGN, "%=" },*/
1363 /*{ OPERATOR_MODULUS, "%" },*/
1364 /*{ OPERATOR_ANDASSIGN, "&=" },*/
1365 /*{ OPERATOR_BITAND, "&" },*/
1366 /*{ OPERATOR_ORASSIGN, "|=" },*/
1367 /*{ OPERATOR_BITOR, "|" },*/
1368 /*{ OPERATOR_COMPLEMENT, "~" },*/
1369 /*{ OPERATOR_XORASSIGN, "^=" },*/
1370 { OPERATOR_LOGICALXOR
, "^^" },
1371 /*{ OPERATOR_BITXOR, "^" }*/
1374 static slang_atom
parse_operator_name (slang_parse_ctx
*C
)
1378 for (i
= 0; i
< sizeof (operator_names
) / sizeof (*operator_names
); i
++)
1380 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
))
1382 slang_atom atom
= slang_atom_pool_atom (C
->atoms
, operator_names
[i
].o_name
);
1383 if (atom
== SLANG_ATOM_NULL
)
1385 slang_info_log_memory (C
->L
);
1395 static int parse_function_prototype (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_function
*func
)
1397 /* parse function type and name */
1398 if (!parse_fully_specified_type (C
, O
, &func
->header
.type
))
1402 case FUNCTION_ORDINARY
:
1403 func
->kind
= slang_func_ordinary
;
1404 func
->header
.a_name
= parse_identifier (C
);
1405 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1408 case FUNCTION_CONSTRUCTOR
:
1409 func
->kind
= slang_func_constructor
;
1410 if (func
->header
.type
.specifier
.type
== slang_spec_struct
)
1412 func
->header
.a_name
= slang_atom_pool_atom (C
->atoms
,
1413 slang_type_specifier_type_to_string (func
->header
.type
.specifier
.type
));
1414 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1416 slang_info_log_memory (C
->L
);
1420 case FUNCTION_OPERATOR
:
1421 func
->kind
= slang_func_operator
;
1422 func
->header
.a_name
= parse_operator_name (C
);
1423 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1430 /* parse function parameters */
1431 while (*C
->I
++ == PARAMETER_NEXT
)
1435 func
->parameters
->variables
= (slang_variable
*) slang_alloc_realloc (
1436 func
->parameters
->variables
,
1437 func
->parameters
->num_variables
* sizeof (slang_variable
),
1438 (func
->parameters
->num_variables
+ 1) * sizeof (slang_variable
));
1439 if (func
->parameters
->variables
== NULL
)
1441 slang_info_log_memory (C
->L
);
1444 p
= &func
->parameters
->variables
[func
->parameters
->num_variables
];
1445 if (!slang_variable_construct (p
))
1447 func
->parameters
->num_variables
++;
1448 if (!parse_parameter_declaration (C
, O
, p
))
1452 /* function formal parameters and local variables share the same scope, so save
1453 * the information about param count in a seperate place
1454 * also link the scope to the global variable scope so when a given identifier is not
1455 * found here, the search process continues in the global space */
1456 func
->param_count
= func
->parameters
->num_variables
;
1457 func
->parameters
->outer_scope
= O
->vars
;
1461 static int parse_function_definition (slang_parse_ctx
*C
, slang_output_ctx
*O
, slang_function
*func
)
1463 slang_output_ctx o
= *O
;
1465 if (!parse_function_prototype (C
, O
, func
))
1468 /* create function's body operation */
1469 func
->body
= (slang_operation
*) slang_alloc_malloc (sizeof (slang_operation
));
1470 if (func
->body
== NULL
)
1472 slang_info_log_memory (C
->L
);
1475 if (!slang_operation_construct (func
->body
))
1477 slang_alloc_free (func
->body
);
1479 slang_info_log_memory (C
->L
);
1483 /* to parse the body the parse context is modified in order to capture parsed variables
1484 * into function's local variable scope */
1485 C
->global_scope
= 0;
1486 o
.vars
= func
->parameters
;
1487 if (!parse_statement (C
, &o
, func
->body
))
1489 C
->global_scope
= 1;
1493 static GLboolean
initialize_global (slang_assemble_ctx
*A
, slang_variable
*var
)
1495 slang_assembly_file_restore_point point
;
1497 slang_assembly_local_info save_local
= A
->local
;
1498 slang_operation op_id
, op_assign
;
1501 /* save the current assembly */
1502 if (!slang_assembly_file_restore_point_save (A
->file
, &point
))
1505 /* setup the machine */
1507 mach
.ip
= A
->file
->count
;
1509 /* allocate local storage for expression */
1510 A
->local
.ret_size
= 0;
1511 A
->local
.addr_tmp
= 0;
1512 A
->local
.swizzle_tmp
= 4;
1513 if (!slang_assembly_file_push_label (A
->file
, slang_asm_local_alloc
, 20))
1515 if (!slang_assembly_file_push_label (A
->file
, slang_asm_enter
, 20))
1518 /* construct the left side of assignment */
1519 if (!slang_operation_construct (&op_id
))
1521 op_id
.type
= slang_oper_identifier
;
1522 op_id
.a_id
= var
->a_name
;
1524 /* put the variable into operation's scope */
1525 op_id
.locals
->variables
= (slang_variable
*) slang_alloc_malloc (sizeof (slang_variable
));
1526 if (op_id
.locals
->variables
== NULL
)
1528 slang_operation_destruct (&op_id
);
1531 op_id
.locals
->num_variables
= 1;
1532 op_id
.locals
->variables
[0] = *var
;
1534 /* construct the assignment expression */
1535 if (!slang_operation_construct (&op_assign
))
1537 op_id
.locals
->num_variables
= 0;
1538 slang_operation_destruct (&op_id
);
1541 op_assign
.type
= slang_oper_assign
;
1542 op_assign
.children
= (slang_operation
*) slang_alloc_malloc (2 * sizeof (slang_operation
));
1543 if (op_assign
.children
== NULL
)
1545 slang_operation_destruct (&op_assign
);
1546 op_id
.locals
->num_variables
= 0;
1547 slang_operation_destruct (&op_id
);
1550 op_assign
.num_children
= 2;
1551 op_assign
.children
[0] = op_id
;
1552 op_assign
.children
[1] = *var
->initializer
;
1554 /* insert the actual expression */
1555 result
= _slang_assemble_operation (A
, &op_assign
, slang_ref_forbid
);
1557 /* carefully destroy the operations */
1558 op_assign
.num_children
= 0;
1559 slang_alloc_free (op_assign
.children
);
1560 op_assign
.children
= NULL
;
1561 slang_operation_destruct (&op_assign
);
1562 op_id
.locals
->num_variables
= 0;
1563 slang_operation_destruct (&op_id
);
1567 if (!slang_assembly_file_push (A
->file
, slang_asm_exit
))
1570 /* execute the expression */
1571 if (!_slang_execute2 (A
->file
, &mach
))
1574 /* restore the old assembly */
1575 if (!slang_assembly_file_restore_point_load (A
->file
, &point
))
1577 A
->local
= save_local
;
1579 /* now we copy the contents of the initialized variable back to the original machine */
1580 _mesa_memcpy ((GLubyte
*) A
->mach
->mem
+ var
->address
, (GLubyte
*) mach
.mem
+ var
->address
,
1586 /* init declarator list */
1587 #define DECLARATOR_NONE 0
1588 #define DECLARATOR_NEXT 1
1590 /* variable declaration */
1591 #define VARIABLE_NONE 0
1592 #define VARIABLE_IDENTIFIER 1
1593 #define VARIABLE_INITIALIZER 2
1594 #define VARIABLE_ARRAY_EXPLICIT 3
1595 #define VARIABLE_ARRAY_UNKNOWN 4
1597 static int parse_init_declarator (slang_parse_ctx
*C
, slang_output_ctx
*O
,
1598 const slang_fully_specified_type
*type
)
1600 slang_variable
*var
;
1602 /* empty init declatator (without name, e.g. "float ;") */
1603 if (*C
->I
++ == VARIABLE_NONE
)
1606 /* make room for the new variable and initialize it */
1607 O
->vars
->variables
= (slang_variable
*) slang_alloc_realloc (O
->vars
->variables
,
1608 O
->vars
->num_variables
* sizeof (slang_variable
),
1609 (O
->vars
->num_variables
+ 1) * sizeof (slang_variable
));
1610 if (O
->vars
->variables
== NULL
)
1612 slang_info_log_memory (C
->L
);
1615 var
= &O
->vars
->variables
[O
->vars
->num_variables
];
1616 if (!slang_variable_construct (var
))
1618 O
->vars
->num_variables
++;
1620 /* copy the declarator qualifier type, parse the identifier */
1621 var
->global
= C
->global_scope
;
1622 var
->type
.qualifier
= type
->qualifier
;
1623 var
->a_name
= parse_identifier (C
);
1624 if (var
->a_name
== SLANG_ATOM_NULL
)
1630 /* simple variable declarator - just copy the specifier */
1631 if (!slang_type_specifier_copy (&var
->type
.specifier
, &type
->specifier
))
1634 case VARIABLE_INITIALIZER
:
1635 /* initialized variable - copy the specifier and parse the expression */
1636 if (!slang_type_specifier_copy (&var
->type
.specifier
, &type
->specifier
))
1638 var
->initializer
= (slang_operation
*) slang_alloc_malloc (sizeof (slang_operation
));
1639 if (var
->initializer
== NULL
)
1641 slang_info_log_memory (C
->L
);
1644 if (!slang_operation_construct (var
->initializer
))
1646 slang_alloc_free (var
->initializer
);
1647 var
->initializer
= NULL
;
1648 slang_info_log_memory (C
->L
);
1651 if (!parse_expression (C
, O
, var
->initializer
))
1655 case VARIABLE_ARRAY_UNKNOWN
:
1656 /* unsized array - mark it as array and copy the specifier to the array element */
1657 if (!convert_to_array (C
, var
, &type
->specifier
))
1661 case VARIABLE_ARRAY_EXPLICIT
:
1662 if (!convert_to_array (C
, var
, &type
->specifier
))
1664 if (!parse_array_len (C
, O
, &var
->array_len
))
1671 /* allocate global address space for a variable with a known size */
1672 if (C
->global_scope
&& !(var
->type
.specifier
.type
== slang_spec_array
&& var
->array_len
== 0))
1674 if (!calculate_var_size (C
, O
, var
))
1676 var
->address
= slang_var_pool_alloc (O
->global_pool
, var
->size
);
1679 /* initialize global variable */
1680 if (C
->global_scope
) {
1681 if (var
->initializer
!= NULL
) {
1682 slang_assemble_ctx A
;
1684 A
.file
= O
->assembly
;
1685 A
.mach
= O
->machine
;
1687 A
.space
.funcs
= O
->funs
;
1688 A
.space
.structs
= O
->structs
;
1689 A
.space
.vars
= O
->vars
;
1690 if (!initialize_global (&A
, var
))
1694 _mesa_memset ((GLubyte
*) (O
->machine
->mem
) + var
->address
, 0, var
->size
);
1700 static int parse_init_declarator_list (slang_parse_ctx
*C
, slang_output_ctx
*O
)
1702 slang_fully_specified_type type
;
1704 /* parse the fully specified type, common to all declarators */
1705 if (!slang_fully_specified_type_construct (&type
))
1707 if (!parse_fully_specified_type (C
, O
, &type
))
1709 slang_fully_specified_type_destruct (&type
);
1713 /* parse declarators, pass-in the parsed type */
1716 if (!parse_init_declarator (C
, O
, &type
))
1718 slang_fully_specified_type_destruct (&type
);
1722 while (*C
->I
++ == DECLARATOR_NEXT
);
1724 slang_fully_specified_type_destruct (&type
);
1728 static int parse_function (slang_parse_ctx
*C
, slang_output_ctx
*O
, int definition
,
1729 slang_function
**parsed_func_ret
)
1731 slang_function parsed_func
, *found_func
;
1733 /* parse function definition/declaration */
1734 if (!slang_function_construct (&parsed_func
))
1738 if (!parse_function_definition (C
, O
, &parsed_func
))
1740 slang_function_destruct (&parsed_func
);
1746 if (!parse_function_prototype (C
, O
, &parsed_func
))
1748 slang_function_destruct (&parsed_func
);
1753 /* find a function with a prototype matching the parsed one - only the current scope
1754 * is being searched to allow built-in function overriding */
1755 found_func
= slang_function_scope_find (O
->funs
, &parsed_func
, 0);
1756 if (found_func
== NULL
)
1758 /* add the parsed function to the function list */
1759 O
->funs
->functions
= (slang_function
*) slang_alloc_realloc (O
->funs
->functions
,
1760 O
->funs
->num_functions
* sizeof (slang_function
),
1761 (O
->funs
->num_functions
+ 1) * sizeof (slang_function
));
1762 if (O
->funs
->functions
== NULL
)
1764 slang_info_log_memory (C
->L
);
1765 slang_function_destruct (&parsed_func
);
1768 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
1769 O
->funs
->num_functions
++;
1771 /* return the newly parsed function */
1772 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
1776 /* TODO: check function return type qualifiers and specifiers */
1779 if (found_func
->body
!= NULL
)
1781 slang_info_log_error (C
->L
, "%s: function already has a body.",
1782 slang_atom_pool_id (C
->atoms
, parsed_func
.header
.a_name
));
1783 slang_function_destruct (&parsed_func
);
1787 /* destroy the existing function declaration and replace it with the new one,
1788 * remember to save the fixup table */
1789 parsed_func
.fixups
= found_func
->fixups
;
1790 slang_fixup_table_init (&found_func
->fixups
);
1791 slang_function_destruct (found_func
);
1792 *found_func
= parsed_func
;
1796 /* another declaration of the same function prototype - ignore it */
1797 slang_function_destruct (&parsed_func
);
1800 /* return the found function */
1801 *parsed_func_ret
= found_func
;
1804 /* assemble the parsed function */
1806 slang_assemble_ctx A
;
1808 A
.file
= O
->assembly
;
1809 A
.mach
= O
->machine
;
1811 A
.space
.funcs
= O
->funs
;
1812 A
.space
.structs
= O
->structs
;
1813 A
.space
.vars
= O
->vars
;
1814 if (!_slang_assemble_function (&A
, *parsed_func_ret
))
1821 #define DECLARATION_FUNCTION_PROTOTYPE 1
1822 #define DECLARATION_INIT_DECLARATOR_LIST 2
1824 static int parse_declaration (slang_parse_ctx
*C
, slang_output_ctx
*O
)
1828 case DECLARATION_INIT_DECLARATOR_LIST
:
1829 if (!parse_init_declarator_list (C
, O
))
1832 case DECLARATION_FUNCTION_PROTOTYPE
:
1834 slang_function
*dummy_func
;
1836 if (!parse_function (C
, O
, 0, &dummy_func
))
1846 /* external declaration */
1847 #define EXTERNAL_NULL 0
1848 #define EXTERNAL_FUNCTION_DEFINITION 1
1849 #define EXTERNAL_DECLARATION 2
1852 parse_code_unit (slang_parse_ctx
*C
, slang_code_unit
*unit
)
1856 /* setup output context */
1857 o
.funs
= &unit
->funs
;
1858 o
.structs
= &unit
->structs
;
1859 o
.vars
= &unit
->vars
;
1860 o
.assembly
= &unit
->object
->assembly
;
1861 o
.global_pool
= &unit
->object
->varpool
;
1862 o
.machine
= &unit
->object
->machine
;
1864 /* parse individual functions and declarations */
1865 while (*C
->I
!= EXTERNAL_NULL
)
1869 case EXTERNAL_FUNCTION_DEFINITION
:
1871 slang_function
*func
;
1873 if (!parse_function (C
, &o
, 1, &func
))
1877 case EXTERNAL_DECLARATION
:
1878 if (!parse_declaration (C
, &o
))
1890 compile_binary (const byte
*prod
, slang_code_unit
*unit
, slang_unit_type type
,
1891 slang_info_log
*infolog
, slang_code_unit
*builtin
, slang_code_unit
*downlink
)
1897 /* setup parse context */
1900 C
.parsing_builtin
= (builtin
== NULL
);
1901 C
.global_scope
= GL_TRUE
;
1902 C
.atoms
= &unit
->object
->atompool
;
1904 if (!check_revision (&C
))
1907 if (downlink
!= NULL
) {
1908 unit
->vars
.outer_scope
= &downlink
->vars
;
1909 unit
->funs
.outer_scope
= &downlink
->funs
;
1910 unit
->structs
.outer_scope
= &downlink
->structs
;
1913 /* parse translation unit */
1914 return parse_code_unit (&C
, unit
);
1918 compile_with_grammar (grammar id
, const char *source
, slang_code_unit
*unit
, slang_unit_type type
,
1919 slang_info_log
*infolog
, slang_code_unit
*builtin
)
1922 GLuint size
, start
, version
;
1923 slang_string preprocessed
;
1925 /* First retrieve the version number. */
1926 if (!_slang_preprocess_version (source
, &version
, &start
, infolog
))
1929 if (version
> 110) {
1930 slang_info_log_error (infolog
, "language version specified is not supported.");
1934 /* Now preprocess the source string. */
1935 slang_string_init (&preprocessed
);
1936 if (!_slang_preprocess_directives (&preprocessed
, &source
[start
], infolog
)) {
1937 slang_string_free (&preprocessed
);
1938 slang_info_log_error (infolog
, "failed to preprocess the source.");
1942 /* Finally check the syntax and generate its binary representation. */
1943 if (!grammar_fast_check (id
, (const byte
*) (slang_string_cstr (&preprocessed
)), &prod
, &size
, 65536)) {
1947 slang_string_free (&preprocessed
);
1948 grammar_get_last_error ((byte
*) (buf
), sizeof (buf
), &pos
);
1949 slang_info_log_error (infolog
, buf
);
1952 slang_string_free (&preprocessed
);
1954 /* Syntax is okay - translate it to internal representation. */
1955 if (!compile_binary (prod
, unit
, type
, infolog
, builtin
, &builtin
[SLANG_BUILTIN_TOTAL
- 1])) {
1956 grammar_alloc_free (prod
);
1959 grammar_alloc_free (prod
);
1963 static const char *slang_shader_syn
=
1964 #include "library/slang_shader_syn.h"
1967 static const byte slang_core_gc
[] = {
1968 #include "library/slang_core_gc.h"
1971 static const byte slang_common_builtin_gc
[] = {
1972 #include "library/slang_common_builtin_gc.h"
1975 static const byte slang_fragment_builtin_gc
[] = {
1976 #include "library/slang_fragment_builtin_gc.h"
1979 static const byte slang_vertex_builtin_gc
[] = {
1980 #include "library/slang_vertex_builtin_gc.h"
1983 #if defined(USE_X86_ASM) || defined(SLANG_X86)
1984 static const byte slang_builtin_vec4_gc
[] = {
1985 #include "library/slang_builtin_vec4_gc.h"
1990 compile_object (grammar
*id
, const char *source
, slang_code_object
*object
, slang_unit_type type
,
1991 slang_info_log
*infolog
)
1993 slang_code_unit
*builtins
= NULL
;
1995 /* load GLSL grammar */
1996 *id
= grammar_load_from_text ((const byte
*) (slang_shader_syn
));
2002 grammar_get_last_error (buf
, 1024, &pos
);
2003 slang_info_log_error (infolog
, (const char *) (buf
));
2007 /* set shader type - the syntax is slightly different for different shaders */
2008 if (type
== slang_unit_fragment_shader
|| type
== slang_unit_fragment_builtin
)
2009 grammar_set_reg8 (*id
, (const byte
*) "shader_type", 1);
2011 grammar_set_reg8 (*id
, (const byte
*) "shader_type", 2);
2013 /* enable language extensions */
2014 grammar_set_reg8 (*id
, (const byte
*) "parsing_builtin", 1);
2016 /* if parsing user-specified shader, load built-in library */
2017 if (type
== slang_unit_fragment_shader
|| type
== slang_unit_vertex_shader
)
2019 /* compile core functionality first */
2020 if (!compile_binary (slang_core_gc
, &object
->builtin
[SLANG_BUILTIN_CORE
],
2021 slang_unit_fragment_builtin
, infolog
, NULL
, NULL
))
2024 /* compile common functions and variables, link to core */
2025 if (!compile_binary (slang_common_builtin_gc
, &object
->builtin
[SLANG_BUILTIN_COMMON
],
2026 slang_unit_fragment_builtin
, infolog
, NULL
,
2027 &object
->builtin
[SLANG_BUILTIN_CORE
]))
2030 /* compile target-specific functions and variables, link to common */
2031 if (type
== slang_unit_fragment_shader
)
2033 if (!compile_binary (slang_fragment_builtin_gc
, &object
->builtin
[SLANG_BUILTIN_TARGET
],
2034 slang_unit_fragment_builtin
, infolog
, NULL
,
2035 &object
->builtin
[SLANG_BUILTIN_COMMON
]))
2038 else if (type
== slang_unit_vertex_shader
)
2040 if (!compile_binary (slang_vertex_builtin_gc
, &object
->builtin
[SLANG_BUILTIN_TARGET
],
2041 slang_unit_vertex_builtin
, infolog
, NULL
,
2042 &object
->builtin
[SLANG_BUILTIN_COMMON
]))
2046 #if defined(USE_X86_ASM) || defined(SLANG_X86)
2047 /* compile x86 4-component vector overrides, link to target */
2048 if (!compile_binary (slang_builtin_vec4_gc
, &object
->builtin
[SLANG_BUILTIN_VEC4
],
2049 slang_unit_fragment_builtin
, infolog
, NULL
,
2050 &object
->builtin
[SLANG_BUILTIN_TARGET
]))
2054 /* disable language extensions */
2055 grammar_set_reg8 (*id
, (const byte
*) "parsing_builtin", 0);
2056 builtins
= object
->builtin
;
2059 /* compile the actual shader - pass-in built-in library for external shader */
2060 return compile_with_grammar (*id
, source
, &object
->unit
, type
, infolog
, builtins
);
2064 _slang_compile (const char *source
, slang_code_object
*object
, slang_unit_type type
,
2065 slang_info_log
*infolog
)
2070 _slang_code_object_dtr (object
);
2071 _slang_code_object_ctr (object
);
2073 success
= compile_object (&id
, source
, object
, type
, infolog
);
2075 grammar_destroy (id
);
2079 if (!_slang_build_export_data_table (&object
->expdata
, &object
->unit
.vars
))
2081 if (!_slang_build_export_code_table (&object
->expcode
, &object
->unit
.funs
, &object
->unit
))
2084 #if defined(USE_X86_ASM) || defined(SLANG_X86)
2085 /* XXX: lookup the @main label */
2086 if (!_slang_x86_codegen (&object
->machine
, &object
->assembly
, object
->expcode
.entries
[0].address
))