2 * Mesa 3-D graphics library
5 * Copyright (C) 2005 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 "slang_compile.h"
33 #include "grammar_mesa.h"
34 #include "slang_preprocess.h"
37 This is a straightforward implementation of the slang front-end compiler.
38 Lots of error-checking functionality is missing but every well-formed shader source should
39 compile successfully and execute as expected. However, some semantically ill-formed shaders
40 may be accepted resulting in undefined behaviour.
43 void slang_alloc_free (void *ptr
)
48 void *slang_alloc_malloc (unsigned int size
)
50 return _mesa_malloc (size
);
53 void *slang_alloc_realloc (void *ptr
, unsigned int old_size
, unsigned int size
)
55 return _mesa_realloc (ptr
, old_size
, size
);
58 int slang_string_compare (const char *str1
, const char *str2
)
60 return _mesa_strcmp (str1
, str2
);
63 char *slang_string_copy (char *dst
, const char *src
)
65 return _mesa_strcpy (dst
, src
);
68 char *slang_string_concat (char *dst
, const char *src
)
70 return _mesa_strcpy (dst
+ _mesa_strlen (dst
), src
);
73 char *slang_string_duplicate (const char *src
)
75 return _mesa_strdup (src
);
78 unsigned int slang_string_length (const char *str
)
80 return _mesa_strlen (str
);
83 static void slang_variable_construct (slang_variable
*);
84 static int slang_variable_copy (slang_variable
*, const slang_variable
*);
85 static void slang_struct_construct (slang_struct
*);
86 static void slang_struct_destruct (slang_struct
*);
87 static int slang_struct_copy (slang_struct
*, const slang_struct
*);
88 static int slang_struct_equal (const slang_struct
*, const slang_struct
*);
89 static void slang_variable_destruct (slang_variable
*);
91 /* slang_type_specifier */
93 static void slang_type_specifier_construct (slang_type_specifier
*spec
)
95 spec
->type
= slang_spec_void
;
100 static void slang_type_specifier_destruct (slang_type_specifier
*spec
)
102 if (spec
->_struct
!= NULL
)
104 slang_struct_destruct (spec
->_struct
);
105 slang_alloc_free (spec
->_struct
);
107 if (spec
->_array
!= NULL
)
109 slang_type_specifier_destruct (spec
->_array
);
110 slang_alloc_free (spec
->_array
);
114 static int slang_type_specifier_copy (slang_type_specifier
*x
, const slang_type_specifier
*y
)
116 slang_type_specifier_destruct (x
);
117 slang_type_specifier_construct (x
);
119 if (x
->type
== slang_spec_struct
)
121 x
->_struct
= (slang_struct
*) slang_alloc_malloc (sizeof (slang_struct
));
122 slang_struct_construct (x
->_struct
);
123 return slang_struct_copy (x
->_struct
, y
->_struct
);
125 if (x
->type
== slang_spec_array
)
127 x
->_array
= (slang_type_specifier
*) slang_alloc_malloc (sizeof (slang_type_specifier
));
128 slang_type_specifier_construct (x
->_array
);
129 return slang_type_specifier_copy (x
->_array
, y
->_array
);
134 static int slang_type_specifier_equal (const slang_type_specifier
*x
, const slang_type_specifier
*y
)
136 if (x
->type
!= y
->type
)
138 if (x
->type
== slang_spec_struct
)
139 return slang_struct_equal (x
->_struct
, y
->_struct
);
140 if (x
->type
== slang_spec_array
)
141 return slang_type_specifier_equal (x
->_array
, y
->_array
);
145 /* slang_fully_specified_type */
147 static void slang_fully_specified_type_construct (slang_fully_specified_type
*type
)
149 type
->qualifier
= slang_qual_none
;
150 slang_type_specifier_construct (&type
->specifier
);
153 static void slang_fully_specified_type_destruct (slang_fully_specified_type
*type
)
155 slang_type_specifier_destruct (&type
->specifier
);
158 static int slang_fully_specified_type_copy (slang_fully_specified_type
*x
,
159 const slang_fully_specified_type
*y
)
161 slang_fully_specified_type_construct (x
);
162 slang_fully_specified_type_destruct (x
);
163 x
->qualifier
= y
->qualifier
;
164 return slang_type_specifier_copy (&x
->specifier
, &y
->specifier
);
167 /* slang_variable_scope */
169 static void slang_variable_scope_construct (slang_variable_scope
*scope
)
171 scope
->variables
= NULL
;
172 scope
->num_variables
= 0;
173 scope
->outer_scope
= NULL
;
176 static void slang_variable_scope_destruct (slang_variable_scope
*scope
)
179 for (i
= 0; i
< scope
->num_variables
; i
++)
180 slang_variable_destruct (scope
->variables
+ i
);
181 slang_alloc_free (scope
->variables
);
184 static int slang_variable_scope_copy (slang_variable_scope
*x
, const slang_variable_scope
*y
)
187 slang_variable_scope_destruct (x
);
188 slang_variable_scope_construct (x
);
189 x
->variables
= (slang_variable
*) slang_alloc_malloc (y
->num_variables
* sizeof (
191 if (x
->variables
== NULL
)
193 x
->num_variables
= y
->num_variables
;
194 for (i
= 0; i
< x
->num_variables
; i
++)
195 slang_variable_construct (x
->variables
+ i
);
196 for (i
= 0; i
< x
->num_variables
; i
++)
197 if (!slang_variable_copy (x
->variables
+ i
, y
->variables
+ i
))
199 x
->outer_scope
= y
->outer_scope
;
203 /* slang_operation */
205 static void slang_operation_construct (slang_operation
*oper
)
207 oper
->type
= slang_oper_none
;
208 oper
->children
= NULL
;
209 oper
->num_children
= 0;
210 oper
->literal
= (float) 0;
211 oper
->identifier
= NULL
;
212 oper
->locals
= (slang_variable_scope
*) slang_alloc_malloc (sizeof (slang_variable_scope
));
213 slang_variable_scope_construct (oper
->locals
);
216 static void slang_operation_destruct (slang_operation
*oper
)
219 for (i
= 0; i
< oper
->num_children
; i
++)
220 slang_operation_destruct (oper
->children
+ i
);
221 slang_alloc_free (oper
->children
);
222 slang_alloc_free (oper
->identifier
);
223 slang_variable_scope_destruct (oper
->locals
);
224 slang_alloc_free (oper
->locals
);
227 static int slang_operation_copy (slang_operation
*x
, const slang_operation
*y
)
230 slang_operation_destruct (x
);
231 slang_operation_construct (x
);
233 x
->children
= (slang_operation
*) slang_alloc_malloc (y
->num_children
* sizeof (
235 if (x
->children
== NULL
)
237 x
->num_children
= y
->num_children
;
238 for (i
= 0; i
< x
->num_children
; i
++)
239 slang_operation_construct (x
->children
+ i
);
240 for (i
= 0; i
< x
->num_children
; i
++)
241 if (!slang_operation_copy (x
->children
+ i
, y
->children
+ i
))
243 x
->literal
= y
->literal
;
244 if (y
->identifier
!= NULL
)
246 x
->identifier
= slang_string_duplicate (y
->identifier
);
247 if (x
->identifier
== NULL
)
250 if (!slang_variable_scope_copy (x
->locals
, y
->locals
))
257 static void slang_variable_construct (slang_variable
*var
)
259 slang_fully_specified_type_construct (&var
->type
);
261 var
->array_size
= NULL
;
262 var
->initializer
= NULL
;
265 static void slang_variable_destruct (slang_variable
*var
)
267 slang_fully_specified_type_destruct (&var
->type
);
268 slang_alloc_free (var
->name
);
269 if (var
->array_size
!= NULL
)
271 slang_operation_destruct (var
->array_size
);
272 slang_alloc_free (var
->array_size
);
274 if (var
->initializer
!= NULL
)
276 slang_operation_destruct (var
->initializer
);
277 slang_alloc_free (var
->initializer
);
281 static int slang_variable_copy (slang_variable
*x
, const slang_variable
*y
)
283 slang_variable_destruct (x
);
284 slang_variable_construct (x
);
285 if (!slang_fully_specified_type_copy (&x
->type
, &y
->type
))
289 x
->name
= slang_string_duplicate (y
->name
);
293 if (y
->array_size
!= NULL
)
295 x
->array_size
= (slang_operation
*) slang_alloc_malloc (sizeof (slang_operation
));
296 if (x
->array_size
== NULL
)
298 slang_operation_construct (x
->array_size
);
299 if (!slang_operation_copy (x
->array_size
, y
->array_size
))
302 if (y
->initializer
!= NULL
)
304 x
->initializer
= (slang_operation
*) slang_alloc_malloc (sizeof (slang_operation
));
305 if (x
->initializer
== NULL
)
307 slang_operation_construct (x
->initializer
);
308 if (!slang_operation_copy (x
->initializer
, y
->initializer
))
314 /* slang_struct_scope */
316 static void slang_struct_scope_construct (slang_struct_scope
*scope
)
318 scope
->structs
= NULL
;
319 scope
->num_structs
= 0;
320 scope
->outer_scope
= NULL
;
323 static void slang_struct_scope_destruct (slang_struct_scope
*scope
)
326 for (i
= 0; i
< scope
->num_structs
; i
++)
327 slang_struct_destruct (scope
->structs
+ i
);
328 slang_alloc_free (scope
->structs
);
331 static int slang_struct_scope_copy (slang_struct_scope
*x
, const slang_struct_scope
*y
)
334 slang_struct_scope_destruct (x
);
335 slang_struct_scope_construct (x
);
336 x
->structs
= (slang_struct
*) slang_alloc_malloc (y
->num_structs
* sizeof (slang_struct
));
337 if (x
->structs
== NULL
)
339 x
->num_structs
= y
->num_structs
;
340 for (i
= 0; i
< x
->num_structs
; i
++)
341 slang_struct_construct (x
->structs
+ i
);
342 for (i
= 0; i
< x
->num_structs
; i
++)
343 if (!slang_struct_copy (x
->structs
+ i
, y
->structs
+ i
))
345 x
->outer_scope
= y
->outer_scope
;
349 static slang_struct
*slang_struct_scope_find (slang_struct_scope
*stru
, const char *name
,
353 for (i
= 0; i
< stru
->num_structs
; i
++)
354 if (slang_string_compare (name
, stru
->structs
[i
].name
) == 0)
355 return stru
->structs
+ i
;
356 if (all_scopes
&& stru
->outer_scope
!= NULL
)
357 return slang_struct_scope_find (stru
->outer_scope
, name
, 1);
363 static void slang_struct_construct (slang_struct
*stru
)
366 stru
->fields
= (slang_variable_scope
*) slang_alloc_malloc (sizeof (slang_variable_scope
));
367 slang_variable_scope_construct (stru
->fields
);
368 stru
->structs
= (slang_struct_scope
*) slang_alloc_malloc (sizeof (slang_struct_scope
));
369 slang_struct_scope_construct (stru
->structs
);
372 static void slang_struct_destruct (slang_struct
*stru
)
374 slang_alloc_free (stru
->name
);
375 slang_variable_scope_destruct (stru
->fields
);
376 slang_alloc_free (stru
->fields
);
377 slang_struct_scope_destruct (stru
->structs
);
378 slang_alloc_free (stru
->structs
);
381 static int slang_struct_copy (slang_struct
*x
, const slang_struct
*y
)
383 slang_struct_destruct (x
);
384 slang_struct_construct (x
);
387 x
->name
= slang_string_duplicate (y
->name
);
391 if (!slang_variable_scope_copy (x
->fields
, y
->fields
))
393 if (!slang_struct_scope_copy (x
->structs
, y
->structs
))
398 static int slang_struct_equal (const slang_struct
*x
, const slang_struct
*y
)
401 if (x
->fields
->num_variables
!= y
->fields
->num_variables
)
403 for (i
= 0; i
< x
->fields
->num_variables
; i
++)
405 slang_variable
*varx
= x
->fields
->variables
+ i
;
406 slang_variable
*vary
= y
->fields
->variables
+ i
;
407 if (slang_string_compare (varx
->name
, vary
->name
) != 0)
409 if (!slang_type_specifier_equal (&varx
->type
.specifier
, &vary
->type
.specifier
))
411 if (varx
->type
.specifier
.type
== slang_spec_array
)
413 /* TODO compare array sizes */
421 static void slang_function_construct (slang_function
*func
)
423 func
->kind
= slang_func_ordinary
;
424 slang_variable_construct (&func
->header
);
425 func
->parameters
= (slang_variable_scope
*) slang_alloc_malloc (sizeof (slang_variable_scope
));
426 slang_variable_scope_construct (func
->parameters
);
430 static void slang_function_destruct (slang_function
*func
)
432 slang_variable_destruct (&func
->header
);
433 slang_variable_scope_destruct (func
->parameters
);
434 slang_alloc_free (func
->parameters
);
435 if (func
->body
!= NULL
)
437 slang_operation_destruct (func
->body
);
438 slang_alloc_free (func
->body
);
442 /* slang_function_scope */
444 static void slang_function_scope_construct (slang_function_scope
*scope
)
446 scope
->functions
= NULL
;
447 scope
->num_functions
= 0;
448 scope
->outer_scope
= NULL
;
451 static void slang_function_scope_destruct (slang_function_scope
*scope
)
454 for (i
= 0; i
< scope
->num_functions
; i
++)
455 slang_function_destruct (scope
->functions
+ i
);
456 slang_alloc_free (scope
->functions
);
459 static slang_function
*slang_function_scope_find (slang_function_scope
*funcs
, slang_function
*fun
,
463 for (i
= 0; i
< funcs
->num_functions
; i
++)
465 slang_function
*f
= funcs
->functions
+ i
;
467 if (slang_string_compare (fun
->header
.name
, f
->header
.name
) != 0)
469 if (fun
->param_count
!= f
->param_count
)
471 for (j
= 0; j
< fun
->param_count
; j
++)
473 if (!slang_type_specifier_equal (&fun
->parameters
->variables
[j
].type
.specifier
,
474 &f
->parameters
->variables
[j
].type
.specifier
))
479 if (j
== fun
->param_count
)
482 if (all_scopes
&& funcs
->outer_scope
!= NULL
)
483 return slang_function_scope_find (funcs
->outer_scope
, fun
, 1);
487 /* slang_translation_unit */
489 static void slang_translation_unit_construct (slang_translation_unit
*unit
)
491 unit
->globals
= (slang_variable_scope
*) slang_alloc_malloc (sizeof (slang_variable_scope
));
492 slang_variable_scope_construct (unit
->globals
);
493 slang_function_scope_construct (&unit
->functions
);
494 unit
->structs
= (slang_struct_scope
*) slang_alloc_malloc (sizeof (slang_struct_scope
));
495 slang_struct_scope_construct (unit
->structs
);
498 static void slang_translation_unit_destruct (slang_translation_unit
*unit
)
500 slang_variable_scope_destruct (unit
->globals
);
501 slang_alloc_free (unit
->globals
);
502 slang_function_scope_destruct (&unit
->functions
);
503 slang_struct_scope_destruct (unit
->structs
);
504 slang_alloc_free (unit
->structs
);
509 static char *out_of_memory
= "error: out of memory\n";
511 void slang_info_log_construct (slang_info_log
*log
)
514 log
->dont_free_text
= 0;
517 void slang_info_log_destruct (slang_info_log
*log
)
519 if (!log
->dont_free_text
)
520 slang_alloc_free (log
->text
);
523 static int slang_info_log_message (slang_info_log
*log
, const char *prefix
, const char *msg
)
525 unsigned int new_size
;
526 if (log
->dont_free_text
)
528 new_size
= slang_string_length (prefix
) + 3 + slang_string_length (msg
);
529 if (log
->text
!= NULL
)
531 log
->text
= (char *) slang_alloc_realloc (log
->text
, slang_string_length (log
->text
) + 1,
532 new_size
+ slang_string_length (log
->text
) + 1);
536 log
->text
= (char *) slang_alloc_malloc (new_size
+ 1);
537 if (log
->text
!= NULL
)
540 if (log
->text
== NULL
)
542 slang_string_concat (log
->text
, prefix
);
543 slang_string_concat (log
->text
, ": ");
544 slang_string_concat (log
->text
, msg
);
545 slang_string_concat (log
->text
, "\n");
549 int slang_info_log_error (slang_info_log
*log
, const char *msg
, ...)
551 if (slang_info_log_message (log
, "error", msg
))
553 slang_info_log_memory (log
);
557 int slang_info_log_warning (slang_info_log
*log
, const char *msg
, ...)
559 if (slang_info_log_message (log
, "warning", msg
))
561 slang_info_log_memory (log
);
565 void slang_info_log_memory (slang_info_log
*log
)
567 if (!slang_info_log_message (log
, "error", "out of memory"))
569 log
->dont_free_text
= 1;
570 log
->text
= out_of_memory
;
574 /* slang_parse_ctx */
576 typedef struct slang_parse_ctx_
584 static int parse_identifier (slang_parse_ctx
*C
, char **id
)
586 *id
= slang_string_duplicate ((const char *) C
->I
);
589 slang_info_log_memory (C
->L
);
592 C
->I
+= strlen ((const char *) C
->I
) + 1;
596 static int parse_number (slang_parse_ctx
*C
, int *number
)
598 const int radix
= (int) (*C
->I
++);
600 while (*C
->I
!= '\0')
603 if (*C
->I
>= '0' && *C
->I
<= '9')
604 digit
= (int) (*C
->I
- '0');
605 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
606 digit
= (int) (*C
->I
- 'A') + 10;
608 digit
= (int) (*C
->I
- 'a') + 10;
609 *number
= *number
* radix
+ digit
;
614 slang_info_log_warning (C
->L
, "%d: literal integer overflow", *number
);
618 static int parse_float (slang_parse_ctx
*C
, float *number
)
620 char *integral
= NULL
;
621 char *fractional
= NULL
;
622 char *exponent
= NULL
;
625 if (!parse_identifier (C
, &integral
))
628 if (!parse_identifier (C
, &fractional
))
630 slang_alloc_free (integral
);
634 if (!parse_identifier (C
, &exponent
))
636 slang_alloc_free (fractional
);
637 slang_alloc_free (integral
);
641 whole
= (char *) (slang_alloc_malloc ((strlen (integral
) + strlen (fractional
) + strlen (
642 exponent
) + 3) * sizeof (char)));
645 slang_alloc_free (exponent
);
646 slang_alloc_free (fractional
);
647 slang_alloc_free (integral
);
648 slang_info_log_memory (C
->L
);
652 slang_string_copy (whole
, integral
);
653 slang_string_concat (whole
, ".");
654 slang_string_concat (whole
, fractional
);
655 slang_string_concat (whole
, "E");
656 slang_string_concat (whole
, exponent
);
658 *number
= (float) (atof (whole
));
660 slang_alloc_free (whole
);
661 slang_alloc_free (exponent
);
662 slang_alloc_free (fractional
);
663 slang_alloc_free (integral
);
667 /* revision number - increment after each change affecting emitted output */
670 static int check_revision (slang_parse_ctx
*C
)
672 if (*C
->I
!= REVISION
)
674 slang_info_log_error (C
->L
, "internal compiler error");
681 static int parse_statement (slang_parse_ctx
*, slang_operation
*, slang_variable_scope
*,
682 slang_struct_scope
*);
683 static int parse_expression (slang_parse_ctx
*, slang_operation
*, slang_variable_scope
*,
684 slang_struct_scope
*);
687 #define TYPE_QUALIFIER_NONE 0
688 #define TYPE_QUALIFIER_CONST 1
689 #define TYPE_QUALIFIER_ATTRIBUTE 2
690 #define TYPE_QUALIFIER_VARYING 3
691 #define TYPE_QUALIFIER_UNIFORM 4
692 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
693 #define TYPE_QUALIFIER_FIXEDINPUT 6
695 static int parse_type_qualifier (slang_parse_ctx
*C
, slang_type_qualifier
*qual
)
699 case TYPE_QUALIFIER_NONE
:
700 *qual
= slang_qual_none
;
702 case TYPE_QUALIFIER_CONST
:
703 *qual
= slang_qual_const
;
705 case TYPE_QUALIFIER_ATTRIBUTE
:
706 *qual
= slang_qual_attribute
;
708 case TYPE_QUALIFIER_VARYING
:
709 *qual
= slang_qual_varying
;
711 case TYPE_QUALIFIER_UNIFORM
:
712 *qual
= slang_qual_uniform
;
714 case TYPE_QUALIFIER_FIXEDOUTPUT
:
715 *qual
= slang_qual_fixedoutput
;
717 case TYPE_QUALIFIER_FIXEDINPUT
:
718 *qual
= slang_qual_fixedinput
;
727 #define TYPE_SPECIFIER_VOID 0
728 #define TYPE_SPECIFIER_BOOL 1
729 #define TYPE_SPECIFIER_BVEC2 2
730 #define TYPE_SPECIFIER_BVEC3 3
731 #define TYPE_SPECIFIER_BVEC4 4
732 #define TYPE_SPECIFIER_INT 5
733 #define TYPE_SPECIFIER_IVEC2 6
734 #define TYPE_SPECIFIER_IVEC3 7
735 #define TYPE_SPECIFIER_IVEC4 8
736 #define TYPE_SPECIFIER_FLOAT 9
737 #define TYPE_SPECIFIER_VEC2 10
738 #define TYPE_SPECIFIER_VEC3 11
739 #define TYPE_SPECIFIER_VEC4 12
740 #define TYPE_SPECIFIER_MAT2 13
741 #define TYPE_SPECIFIER_MAT3 14
742 #define TYPE_SPECIFIER_MAT4 15
743 #define TYPE_SPECIFIER_SAMPLER1D 16
744 #define TYPE_SPECIFIER_SAMPLER2D 17
745 #define TYPE_SPECIFIER_SAMPLER3D 18
746 #define TYPE_SPECIFIER_SAMPLERCUBE 19
747 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
748 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
749 #define TYPE_SPECIFIER_STRUCT 22
750 #define TYPE_SPECIFIER_TYPENAME 23
752 /* structure field */
755 #define FIELD_ARRAY 2
757 static int parse_type_specifier (slang_parse_ctx
*C
, slang_type_specifier
*spec
,
758 slang_struct_scope
*structs
, slang_variable_scope
*scope
)
762 case TYPE_SPECIFIER_VOID
:
763 spec
->type
= slang_spec_void
;
765 case TYPE_SPECIFIER_BOOL
:
766 spec
->type
= slang_spec_bool
;
768 case TYPE_SPECIFIER_BVEC2
:
769 spec
->type
= slang_spec_bvec2
;
771 case TYPE_SPECIFIER_BVEC3
:
772 spec
->type
= slang_spec_bvec3
;
774 case TYPE_SPECIFIER_BVEC4
:
775 spec
->type
= slang_spec_bvec4
;
777 case TYPE_SPECIFIER_INT
:
778 spec
->type
= slang_spec_int
;
780 case TYPE_SPECIFIER_IVEC2
:
781 spec
->type
= slang_spec_ivec2
;
783 case TYPE_SPECIFIER_IVEC3
:
784 spec
->type
= slang_spec_ivec3
;
786 case TYPE_SPECIFIER_IVEC4
:
787 spec
->type
= slang_spec_ivec4
;
789 case TYPE_SPECIFIER_FLOAT
:
790 spec
->type
= slang_spec_float
;
792 case TYPE_SPECIFIER_VEC2
:
793 spec
->type
= slang_spec_vec2
;
795 case TYPE_SPECIFIER_VEC3
:
796 spec
->type
= slang_spec_vec3
;
798 case TYPE_SPECIFIER_VEC4
:
799 spec
->type
= slang_spec_vec4
;
801 case TYPE_SPECIFIER_MAT2
:
802 spec
->type
= slang_spec_mat2
;
804 case TYPE_SPECIFIER_MAT3
:
805 spec
->type
= slang_spec_mat3
;
807 case TYPE_SPECIFIER_MAT4
:
808 spec
->type
= slang_spec_mat4
;
810 case TYPE_SPECIFIER_SAMPLER1D
:
811 spec
->type
= slang_spec_sampler1D
;
813 case TYPE_SPECIFIER_SAMPLER2D
:
814 spec
->type
= slang_spec_sampler2D
;
816 case TYPE_SPECIFIER_SAMPLER3D
:
817 spec
->type
= slang_spec_sampler3D
;
819 case TYPE_SPECIFIER_SAMPLERCUBE
:
820 spec
->type
= slang_spec_samplerCube
;
822 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
823 spec
->type
= slang_spec_sampler1DShadow
;
825 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
826 spec
->type
= slang_spec_sampler2DShadow
;
828 case TYPE_SPECIFIER_STRUCT
:
829 spec
->type
= slang_spec_struct
;
832 if (!parse_identifier (C
, &name
))
834 if (*name
!= '\0' && slang_struct_scope_find (structs
, name
, 0) != NULL
)
836 slang_info_log_error (C
->L
, "%s: duplicate type name", name
);
837 slang_alloc_free (name
);
840 spec
->_struct
= (slang_struct
*) slang_alloc_malloc (sizeof (slang_struct
));
841 if (spec
->_struct
== NULL
)
843 slang_alloc_free (name
);
844 slang_info_log_memory (C
->L
);
847 slang_struct_construct (spec
->_struct
);
848 spec
->_struct
->name
= name
;
849 spec
->_struct
->structs
->outer_scope
= structs
;
853 slang_type_specifier sp
;
854 slang_type_specifier_construct (&sp
);
855 if (!parse_type_specifier (C
, &sp
, spec
->_struct
->structs
, scope
))
857 slang_type_specifier_destruct (&sp
);
863 spec
->_struct
->fields
->variables
= (slang_variable
*) slang_alloc_realloc (
864 spec
->_struct
->fields
->variables
,
865 spec
->_struct
->fields
->num_variables
* sizeof (slang_variable
),
866 (spec
->_struct
->fields
->num_variables
+ 1) * sizeof (slang_variable
));
867 if (spec
->_struct
->fields
->variables
== NULL
)
869 slang_type_specifier_destruct (&sp
);
870 slang_info_log_memory (C
->L
);
873 var
= spec
->_struct
->fields
->variables
+ spec
->_struct
->fields
->num_variables
;
874 spec
->_struct
->fields
->num_variables
++;
875 slang_variable_construct (var
);
876 if (!slang_type_specifier_copy (&var
->type
.specifier
, &sp
))
878 slang_type_specifier_destruct (&sp
);
881 if (!parse_identifier (C
, &var
->name
))
883 slang_type_specifier_destruct (&sp
);
891 var
->array_size
= (slang_operation
*) slang_alloc_malloc (sizeof (
893 if (var
->array_size
== NULL
)
895 slang_type_specifier_destruct (&sp
);
896 slang_info_log_memory (C
->L
);
899 slang_operation_construct (var
->array_size
);
900 if (!parse_expression (C
, var
->array_size
, scope
, structs
))
902 slang_type_specifier_destruct (&sp
);
910 while (*C
->I
++ != FIELD_NONE
);
912 while (*C
->I
++ != FIELD_NONE
);
913 if (*spec
->_struct
->name
!= '\0')
916 structs
->structs
= (slang_struct
*) slang_alloc_realloc (structs
->structs
,
917 structs
->num_structs
* sizeof (slang_struct
),
918 (structs
->num_structs
+ 1) * sizeof (slang_struct
));
919 if (structs
->structs
== NULL
)
921 slang_info_log_memory (C
->L
);
924 s
= structs
->structs
+ structs
->num_structs
;
925 structs
->num_structs
++;
926 slang_struct_construct (s
);
927 if (!slang_struct_copy (s
, spec
->_struct
))
931 case TYPE_SPECIFIER_TYPENAME
:
932 spec
->type
= slang_spec_struct
;
936 if (!parse_identifier (C
, &name
))
938 stru
= slang_struct_scope_find (structs
, name
, 1);
941 slang_info_log_error (C
->L
, "%s: undeclared type name", name
);
942 slang_alloc_free (name
);
945 slang_alloc_free (name
);
946 spec
->_struct
= (slang_struct
*) slang_alloc_malloc (sizeof (slang_struct
));
947 if (spec
->_struct
== NULL
)
949 slang_info_log_memory (C
->L
);
952 slang_struct_construct (spec
->_struct
);
953 if (!slang_struct_copy (spec
->_struct
, stru
))
963 static int parse_fully_specified_type (slang_parse_ctx
*C
, slang_fully_specified_type
*type
,
964 slang_struct_scope
*structs
, slang_variable_scope
*scope
)
966 if (!parse_type_qualifier (C
, &type
->qualifier
))
968 return parse_type_specifier (C
, &type
->specifier
, structs
, scope
);
973 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
974 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
978 #define OP_CONTINUE 6
981 #define OP_EXPRESSION 9
986 #define OP_PUSH_VOID 14
987 #define OP_PUSH_BOOL 15
988 #define OP_PUSH_INT 16
989 #define OP_PUSH_FLOAT 17
990 #define OP_PUSH_IDENTIFIER 18
991 #define OP_SEQUENCE 19
993 #define OP_ADDASSIGN 21
994 #define OP_SUBASSIGN 22
995 #define OP_MULASSIGN 23
996 #define OP_DIVASSIGN 24
997 /*#define OP_MODASSIGN 25*/
998 /*#define OP_LSHASSIGN 26*/
999 /*#define OP_RSHASSIGN 27*/
1000 /*#define OP_ORASSIGN 28*/
1001 /*#define OP_XORASSIGN 29*/
1002 /*#define OP_ANDASSIGN 30*/
1003 #define OP_SELECT 31
1004 #define OP_LOGICALOR 32
1005 #define OP_LOGICALXOR 33
1006 #define OP_LOGICALAND 34
1007 /*#define OP_BITOR 35*/
1008 /*#define OP_BITXOR 36*/
1009 /*#define OP_BITAND 37*/
1011 #define OP_NOTEQUAL 39
1013 #define OP_GREATER 41
1014 #define OP_LESSEQUAL 42
1015 #define OP_GREATEREQUAL 43
1016 /*#define OP_LSHIFT 44*/
1017 /*#define OP_RSHIFT 45*/
1019 #define OP_SUBTRACT 47
1020 #define OP_MULTIPLY 48
1021 #define OP_DIVIDE 49
1022 /*#define OP_MODULUS 50*/
1023 #define OP_PREINCREMENT 51
1024 #define OP_PREDECREMENT 52
1027 /*#define OP_COMPLEMENT 55*/
1029 #define OP_SUBSCRIPT 57
1032 #define OP_POSTINCREMENT 60
1033 #define OP_POSTDECREMENT 61
1035 static int parse_child_operation (slang_parse_ctx
*C
, slang_operation
*oper
, int statement
,
1036 slang_variable_scope
*scope
, slang_struct_scope
*structs
)
1038 oper
->children
= (slang_operation
*) slang_alloc_realloc (oper
->children
,
1039 oper
->num_children
* sizeof (slang_operation
),
1040 (oper
->num_children
+ 1) * sizeof (slang_operation
));
1041 if (oper
->children
== NULL
)
1043 slang_info_log_memory (C
->L
);
1046 slang_operation_construct (oper
->children
+ oper
->num_children
);
1047 oper
->num_children
++;
1049 return parse_statement (C
, oper
->children
+ oper
->num_children
- 1, scope
, structs
);
1050 return parse_expression (C
, oper
->children
+ oper
->num_children
- 1, scope
, structs
);
1053 static int parse_declaration (slang_parse_ctx
*C
, slang_variable_scope
*, slang_struct_scope
*,
1054 slang_function_scope
*);
1056 static int parse_statement (slang_parse_ctx
*C
, slang_operation
*oper
, slang_variable_scope
*scope
,
1057 slang_struct_scope
*structs
)
1059 oper
->locals
->outer_scope
= scope
;
1062 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
1063 oper
->type
= slang_oper_block_no_new_scope
;
1064 while (*C
->I
!= OP_END
)
1065 if (!parse_child_operation (C
, oper
, 1, scope
, structs
))
1069 case OP_BLOCK_BEGIN_NEW_SCOPE
:
1070 oper
->type
= slang_oper_block_new_scope
;
1071 while (*C
->I
!= OP_END
)
1072 if (!parse_child_operation (C
, oper
, 1, oper
->locals
, structs
))
1077 oper
->type
= slang_oper_variable_decl
;
1079 const unsigned int first_var
= scope
->num_variables
;
1080 if (!parse_declaration (C
, scope
, structs
, NULL
))
1082 if (first_var
< scope
->num_variables
)
1084 const unsigned int num_vars
= scope
->num_variables
- first_var
;
1086 oper
->children
= (slang_operation
*) slang_alloc_malloc (num_vars
* sizeof (
1088 if (oper
->children
== NULL
)
1090 slang_info_log_memory (C
->L
);
1093 for (i
= 0; i
< num_vars
; i
++)
1094 slang_operation_construct (oper
->children
+ i
);
1095 oper
->num_children
= num_vars
;
1096 for (i
= first_var
; i
< scope
->num_variables
; i
++)
1098 slang_operation
*o
= oper
->children
+ i
- first_var
;
1099 o
->type
= slang_oper_identifier
;
1100 o
->locals
->outer_scope
= scope
;
1101 o
->identifier
= slang_string_duplicate (scope
->variables
[i
].name
);
1102 if (o
->identifier
== NULL
)
1104 slang_info_log_memory (C
->L
);
1112 oper
->type
= slang_oper_asm
;
1113 if (!parse_identifier (C
, &oper
->identifier
))
1115 while (*C
->I
!= OP_END
)
1116 if (!parse_child_operation (C
, oper
, 0, scope
, structs
))
1121 oper
->type
= slang_oper_break
;
1124 oper
->type
= slang_oper_continue
;
1127 oper
->type
= slang_oper_discard
;
1130 oper
->type
= slang_oper_return
;
1131 if (!parse_child_operation (C
, oper
, 0, scope
, structs
))
1135 oper
->type
= slang_oper_expression
;
1136 if (!parse_child_operation (C
, oper
, 0, scope
, structs
))
1140 oper
->type
= slang_oper_if
;
1141 if (!parse_child_operation (C
, oper
, 0, scope
, structs
))
1143 if (!parse_child_operation (C
, oper
, 1, scope
, structs
))
1145 if (!parse_child_operation (C
, oper
, 1, scope
, structs
))
1149 oper
->type
= slang_oper_while
;
1150 if (!parse_child_operation (C
, oper
, 1, oper
->locals
, structs
))
1152 if (!parse_child_operation (C
, oper
, 1, oper
->locals
, structs
))
1156 oper
->type
= slang_oper_do
;
1157 if (!parse_child_operation (C
, oper
, 1, scope
, structs
))
1159 if (!parse_child_operation (C
, oper
, 0, scope
, structs
))
1163 oper
->type
= slang_oper_for
;
1164 if (!parse_child_operation (C
, oper
, 1, oper
->locals
, structs
))
1166 if (!parse_child_operation (C
, oper
, 1, oper
->locals
, structs
))
1168 if (!parse_child_operation (C
, oper
, 0, oper
->locals
, structs
))
1170 if (!parse_child_operation (C
, oper
, 1, oper
->locals
, structs
))
1179 static int handle_trinary_expression (slang_parse_ctx
*C
, slang_operation
*op
,
1180 slang_operation
**ops
, unsigned int *num_ops
)
1182 op
->num_children
= 3;
1183 op
->children
= (slang_operation
*) slang_alloc_malloc (3 * sizeof (slang_operation
));
1184 if (op
->children
== NULL
)
1186 slang_info_log_memory (C
->L
);
1189 op
->children
[0] = (*ops
)[*num_ops
- 4];
1190 op
->children
[1] = (*ops
)[*num_ops
- 3];
1191 op
->children
[2] = (*ops
)[*num_ops
- 2];
1192 (*ops
)[*num_ops
- 4] = (*ops
)[*num_ops
- 1];
1194 *ops
= (slang_operation
*) slang_alloc_realloc (*ops
, (*num_ops
+ 3) * sizeof (slang_operation
),
1195 *num_ops
* sizeof (slang_operation
));
1198 slang_info_log_memory (C
->L
);
1204 static int handle_binary_expression (slang_parse_ctx
*C
, slang_operation
*op
,
1205 slang_operation
**ops
, unsigned int *num_ops
)
1207 op
->num_children
= 2;
1208 op
->children
= (slang_operation
*) slang_alloc_malloc (2 * sizeof (slang_operation
));
1209 if (op
->children
== NULL
)
1211 slang_info_log_memory (C
->L
);
1214 op
->children
[0] = (*ops
)[*num_ops
- 3];
1215 op
->children
[1] = (*ops
)[*num_ops
- 2];
1216 (*ops
)[*num_ops
- 3] = (*ops
)[*num_ops
- 1];
1218 *ops
= (slang_operation
*) slang_alloc_realloc (*ops
, (*num_ops
+ 2) * sizeof (slang_operation
),
1219 *num_ops
* sizeof (slang_operation
));
1222 slang_info_log_memory (C
->L
);
1228 static int handle_unary_expression (slang_parse_ctx
*C
, slang_operation
*op
,
1229 slang_operation
**ops
, unsigned int *num_ops
)
1231 op
->num_children
= 1;
1232 op
->children
= (slang_operation
*) slang_alloc_malloc (sizeof (slang_operation
));
1233 if (op
->children
== NULL
)
1235 slang_info_log_memory (C
->L
);
1238 op
->children
[0] = (*ops
)[*num_ops
- 2];
1239 (*ops
)[*num_ops
- 2] = (*ops
)[*num_ops
- 1];
1241 *ops
= (slang_operation
*) slang_alloc_realloc (*ops
, (*num_ops
+ 1) * sizeof (slang_operation
),
1242 *num_ops
* sizeof (slang_operation
));
1245 slang_info_log_memory (C
->L
);
1251 static int parse_expression (slang_parse_ctx
*C
, slang_operation
*oper
, slang_variable_scope
*scope
,
1252 slang_struct_scope
*structs
)
1254 slang_operation
*ops
= NULL
;
1255 unsigned int num_ops
= 0;
1258 while (*C
->I
!= OP_END
)
1260 slang_operation
*op
;
1261 const unsigned int op_code
= *C
->I
++;
1262 ops
= (slang_operation
*) slang_alloc_realloc (ops
,
1263 num_ops
* sizeof (slang_operation
), (num_ops
+ 1) * sizeof (slang_operation
));
1266 slang_info_log_memory (C
->L
);
1271 slang_operation_construct (op
);
1272 op
->locals
->outer_scope
= scope
;
1276 op
->type
= slang_oper_void
;
1279 op
->type
= slang_oper_literal_bool
;
1280 if (!parse_number (C
, &number
))
1282 op
->literal
= (float) number
;
1285 op
->type
= slang_oper_literal_int
;
1286 if (!parse_number (C
, &number
))
1288 op
->literal
= (float) number
;
1291 op
->type
= slang_oper_literal_float
;
1292 if (!parse_float (C
, &op
->literal
))
1295 case OP_PUSH_IDENTIFIER
:
1296 op
->type
= slang_oper_identifier
;
1297 if (!parse_identifier (C
, &op
->identifier
))
1301 op
->type
= slang_oper_sequence
;
1302 if (!handle_binary_expression (C
, op
, &ops
, &num_ops
))
1306 op
->type
= slang_oper_assign
;
1307 if (!handle_binary_expression (C
, op
, &ops
, &num_ops
))
1311 op
->type
= slang_oper_addassign
;
1312 if (!handle_binary_expression (C
, op
, &ops
, &num_ops
))
1316 op
->type
= slang_oper_subassign
;
1317 if (!handle_binary_expression (C
, op
, &ops
, &num_ops
))
1321 op
->type
= slang_oper_mulassign
;
1322 if (!handle_binary_expression (C
, op
, &ops
, &num_ops
))
1326 op
->type
= slang_oper_divassign
;
1327 if (!handle_binary_expression (C
, op
, &ops
, &num_ops
))
1330 /*case OP_MODASSIGN:*/
1331 /*case OP_LSHASSIGN:*/
1332 /*case OP_RSHASSIGN:*/
1333 /*case OP_ORASSIGN:*/
1334 /*case OP_XORASSIGN:*/
1335 /*case OP_ANDASSIGN:*/
1337 op
->type
= slang_oper_select
;
1338 if (!handle_trinary_expression (C
, op
, &ops
, &num_ops
))
1342 op
->type
= slang_oper_logicalor
;
1343 if (!handle_binary_expression (C
, op
, &ops
, &num_ops
))
1347 op
->type
= slang_oper_logicalxor
;
1348 if (!handle_binary_expression (C
, op
, &ops
, &num_ops
))
1352 op
->type
= slang_oper_logicaland
;
1353 if (!handle_binary_expression (C
, op
, &ops
, &num_ops
))
1360 op
->type
= slang_oper_equal
;
1361 if (!handle_binary_expression (C
, op
, &ops
, &num_ops
))
1365 op
->type
= slang_oper_notequal
;
1366 if (!handle_binary_expression (C
, op
, &ops
, &num_ops
))
1370 op
->type
= slang_oper_less
;
1371 if (!handle_binary_expression (C
, op
, &ops
, &num_ops
))
1375 op
->type
= slang_oper_greater
;
1376 if (!handle_binary_expression (C
, op
, &ops
, &num_ops
))
1380 op
->type
= slang_oper_lessequal
;
1381 if (!handle_binary_expression (C
, op
, &ops
, &num_ops
))
1384 case OP_GREATEREQUAL
:
1385 op
->type
= slang_oper_greaterequal
;
1386 if (!handle_binary_expression (C
, op
, &ops
, &num_ops
))
1392 op
->type
= slang_oper_add
;
1393 if (!handle_binary_expression (C
, op
, &ops
, &num_ops
))
1397 op
->type
= slang_oper_subtract
;
1398 if (!handle_binary_expression (C
, op
, &ops
, &num_ops
))
1402 op
->type
= slang_oper_multiply
;
1403 if (!handle_binary_expression (C
, op
, &ops
, &num_ops
))
1407 op
->type
= slang_oper_divide
;
1408 if (!handle_binary_expression (C
, op
, &ops
, &num_ops
))
1411 /*case OP_MODULUS:*/
1412 case OP_PREINCREMENT
:
1413 op
->type
= slang_oper_preincrement
;
1414 if (!handle_unary_expression (C
, op
, &ops
, &num_ops
))
1417 case OP_PREDECREMENT
:
1418 op
->type
= slang_oper_predecrement
;
1419 if (!handle_unary_expression (C
, op
, &ops
, &num_ops
))
1423 op
->type
= slang_oper_plus
;
1424 if (!handle_unary_expression (C
, op
, &ops
, &num_ops
))
1428 op
->type
= slang_oper_minus
;
1429 if (!handle_unary_expression (C
, op
, &ops
, &num_ops
))
1433 op
->type
= slang_oper_not
;
1434 if (!handle_unary_expression (C
, op
, &ops
, &num_ops
))
1437 /*case OP_COMPLEMENT:*/
1439 op
->type
= slang_oper_subscript
;
1440 if (!handle_binary_expression (C
, op
, &ops
, &num_ops
))
1444 op
->type
= slang_oper_call
;
1445 if (!parse_identifier (C
, &op
->identifier
))
1447 while (*C
->I
!= OP_END
)
1448 if (!parse_child_operation (C
, op
, 0, scope
, structs
))
1453 op
->type
= slang_oper_field
;
1454 if (!parse_identifier (C
, &op
->identifier
))
1456 if (!handle_unary_expression (C
, op
, &ops
, &num_ops
))
1459 case OP_POSTINCREMENT
:
1460 op
->type
= slang_oper_postincrement
;
1461 if (!handle_unary_expression (C
, op
, &ops
, &num_ops
))
1464 case OP_POSTDECREMENT
:
1465 op
->type
= slang_oper_postdecrement
;
1466 if (!handle_unary_expression (C
, op
, &ops
, &num_ops
))
1475 slang_alloc_free (ops
);
1479 /* parameter qualifier */
1480 #define PARAM_QUALIFIER_IN 0
1481 #define PARAM_QUALIFIER_OUT 1
1482 #define PARAM_QUALIFIER_INOUT 2
1484 /* function parameter array presence */
1485 #define PARAMETER_ARRAY_NOT_PRESENT 0
1486 #define PARAMETER_ARRAY_PRESENT 1
1488 static int parse_parameter_declaration (slang_parse_ctx
*C
, slang_variable
*param
,
1489 slang_struct_scope
*structs
, slang_variable_scope
*scope
)
1491 if (!parse_type_qualifier (C
, ¶m
->type
.qualifier
))
1495 case PARAM_QUALIFIER_IN
:
1496 if (param
->type
.qualifier
!= slang_qual_const
&& param
->type
.qualifier
!= slang_qual_none
)
1498 slang_info_log_error (C
->L
, "invalid type qualifier");
1502 case PARAM_QUALIFIER_OUT
:
1503 if (param
->type
.qualifier
== slang_qual_none
)
1504 param
->type
.qualifier
= slang_qual_out
;
1507 slang_info_log_error (C
->L
, "invalid type qualifier");
1511 case PARAM_QUALIFIER_INOUT
:
1512 if (param
->type
.qualifier
== slang_qual_none
)
1513 param
->type
.qualifier
= slang_qual_inout
;
1516 slang_info_log_error (C
->L
, "invalid type qualifier");
1523 if (!parse_type_specifier (C
, ¶m
->type
.specifier
, structs
, scope
))
1525 if (!parse_identifier (C
, ¶m
->name
))
1527 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
)
1529 param
->array_size
= (slang_operation
*) slang_alloc_malloc (sizeof (slang_operation
));
1530 if (param
->array_size
== NULL
)
1532 slang_info_log_memory (C
->L
);
1535 slang_operation_construct (param
->array_size
);
1536 if (!parse_expression (C
, param
->array_size
, scope
, structs
))
1543 #define FUNCTION_ORDINARY 0
1544 #define FUNCTION_CONSTRUCTOR 1
1545 #define FUNCTION_OPERATOR 2
1547 /* function parameter */
1548 #define PARAMETER_NONE 0
1549 #define PARAMETER_NEXT 1
1552 #define OPERATOR_ASSIGN 1
1553 #define OPERATOR_ADDASSIGN 2
1554 #define OPERATOR_SUBASSIGN 3
1555 #define OPERATOR_MULASSIGN 4
1556 #define OPERATOR_DIVASSIGN 5
1557 /*#define OPERATOR_MODASSIGN 6*/
1558 /*#define OPERATOR_LSHASSIGN 7*/
1559 /*#define OPERATOR_RSHASSIGN 8*/
1560 /*#define OPERATOR_ANDASSIGN 9*/
1561 /*#define OPERATOR_XORASSIGN 10*/
1562 /*#define OPERATOR_ORASSIGN 11*/
1563 #define OPERATOR_LOGICALXOR 12
1564 /*#define OPERATOR_BITOR 13*/
1565 /*#define OPERATOR_BITXOR 14*/
1566 /*#define OPERATOR_BITAND 15*/
1567 #define OPERATOR_EQUAL 16
1568 #define OPERATOR_NOTEQUAL 17
1569 #define OPERATOR_LESS 18
1570 #define OPERATOR_GREATER 19
1571 #define OPERATOR_LESSEQUAL 20
1572 #define OPERATOR_GREATEREQUAL 21
1573 /*#define OPERATOR_LSHIFT 22*/
1574 /*#define OPERATOR_RSHIFT 23*/
1575 #define OPERATOR_MULTIPLY 24
1576 #define OPERATOR_DIVIDE 25
1577 /*#define OPERATOR_MODULUS 26*/
1578 #define OPERATOR_INCREMENT 27
1579 #define OPERATOR_DECREMENT 28
1580 #define OPERATOR_PLUS 29
1581 #define OPERATOR_MINUS 30
1582 /*#define OPERATOR_COMPLEMENT 31*/
1583 #define OPERATOR_NOT 32
1585 /* these must match with slang_type_specifier_type enum */
1586 static const char *type_specifier_names
[] = {
1611 static const struct {
1612 unsigned int o_code
;
1614 } operator_names
[] = {
1615 { OPERATOR_INCREMENT
, "++" },
1616 { OPERATOR_ADDASSIGN
, "+=" },
1617 { OPERATOR_PLUS
, "+" },
1618 { OPERATOR_DECREMENT
, "--" },
1619 { OPERATOR_SUBASSIGN
, "-=" },
1620 { OPERATOR_MINUS
, "-" },
1621 { OPERATOR_NOTEQUAL
, "!=" },
1622 { OPERATOR_NOT
, "!" },
1623 { OPERATOR_MULASSIGN
, "*=" },
1624 { OPERATOR_MULTIPLY
, "*" },
1625 { OPERATOR_DIVASSIGN
, "/=" },
1626 { OPERATOR_DIVIDE
, "/" },
1627 { OPERATOR_LESSEQUAL
, "<=" },
1628 /*{ OPERATOR_LSHASSIGN, "<<=" },*/
1629 /*{ OPERATOR_LSHIFT, "<<" },*/
1630 { OPERATOR_LESS
, "<" },
1631 { OPERATOR_GREATEREQUAL
, ">=" },
1632 /*{ OPERATOR_RSHASSIGN, ">>=" },*/
1633 /*{ OPERATOR_RSHIFT, ">>" },*/
1634 { OPERATOR_GREATER
, ">" },
1635 { OPERATOR_EQUAL
, "==" },
1636 { OPERATOR_ASSIGN
, "=" },
1637 /*{ OPERATOR_MODASSIGN, "%=" },*/
1638 /*{ OPERATOR_MODULUS, "%" },*/
1639 /*{ OPERATOR_ANDASSIGN, "&=" },*/
1640 /*{ OPERATOR_BITAND, "&" },*/
1641 /*{ OPERATOR_ORASSIGN, "|=" },*/
1642 /*{ OPERATOR_BITOR, "|" },*/
1643 /*{ OPERATOR_COMPLEMENT, "~" },*/
1644 /*{ OPERATOR_XORASSIGN, "^=" },*/
1645 { OPERATOR_LOGICALXOR
, "^^" }/*,*/
1646 /*{ OPERATOR_BITXOR, "^" }*/
1649 static int parse_operator_name (slang_parse_ctx
*C
, char **pname
)
1652 for (i
= 0; i
< sizeof (operator_names
) / sizeof (*operator_names
); i
++)
1653 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
))
1655 *pname
= slang_string_duplicate (operator_names
[i
].o_name
);
1658 slang_info_log_memory (C
->L
);
1667 static int parse_function_prototype (slang_parse_ctx
*C
, slang_function
*func
,
1668 slang_struct_scope
*structs
, slang_variable_scope
*scope
)
1670 if (!parse_fully_specified_type (C
, &func
->header
.type
, structs
, scope
))
1674 case FUNCTION_ORDINARY
:
1675 func
->kind
= slang_func_ordinary
;
1676 if (!parse_identifier (C
, &func
->header
.name
))
1679 case FUNCTION_CONSTRUCTOR
:
1680 func
->kind
= slang_func_constructor
;
1681 if (func
->header
.type
.specifier
.type
== slang_spec_struct
)
1683 func
->header
.name
= slang_string_duplicate (
1684 type_specifier_names
[func
->header
.type
.specifier
.type
]);
1685 if (func
->header
.name
== NULL
)
1687 slang_info_log_memory (C
->L
);
1691 case FUNCTION_OPERATOR
:
1692 func
->kind
= slang_func_operator
;
1693 if (!parse_operator_name (C
, &func
->header
.name
))
1699 func
->parameters
->outer_scope
= scope
;
1700 while (*C
->I
++ == PARAMETER_NEXT
)
1702 func
->parameters
->variables
= (slang_variable
*) slang_alloc_realloc (
1703 func
->parameters
->variables
,
1704 func
->parameters
->num_variables
* sizeof (slang_variable
),
1705 (func
->parameters
->num_variables
+ 1) * sizeof (slang_variable
));
1706 if (func
->parameters
->variables
== NULL
)
1708 slang_info_log_memory (C
->L
);
1711 slang_variable_construct (func
->parameters
->variables
+ func
->parameters
->num_variables
);
1712 func
->parameters
->num_variables
++;
1713 if (!parse_parameter_declaration (C
, func
->parameters
->variables
+
1714 func
->parameters
->num_variables
- 1, structs
, scope
))
1717 func
->param_count
= func
->parameters
->num_variables
;
1721 static int parse_function_definition (slang_parse_ctx
*C
, slang_function
*func
,
1722 slang_struct_scope
*structs
, slang_variable_scope
*scope
)
1724 if (!parse_function_prototype (C
, func
, structs
, scope
))
1726 func
->body
= (slang_operation
*) slang_alloc_malloc (sizeof (slang_operation
));
1727 if (func
->body
== NULL
)
1729 slang_info_log_memory (C
->L
);
1732 slang_operation_construct (func
->body
);
1733 if (!parse_statement (C
, func
->body
, func
->parameters
, structs
))
1738 /* init declarator list */
1739 #define DECLARATOR_NONE 0
1740 #define DECLARATOR_NEXT 1
1742 /* variable declaration */
1743 #define VARIABLE_NONE 0
1744 #define VARIABLE_IDENTIFIER 1
1745 #define VARIABLE_INITIALIZER 2
1746 #define VARIABLE_ARRAY_EXPLICIT 3
1747 #define VARIABLE_ARRAY_UNKNOWN 4
1749 static int parse_init_declarator (slang_parse_ctx
*C
, const slang_fully_specified_type
*type
,
1750 slang_variable_scope
*vars
, slang_struct_scope
*structs
)
1752 slang_variable
*var
;
1754 if (*C
->I
++ == VARIABLE_NONE
)
1756 vars
->variables
= (slang_variable
*) slang_alloc_realloc (vars
->variables
,
1757 vars
->num_variables
* sizeof (slang_variable
),
1758 (vars
->num_variables
+ 1) * sizeof (slang_variable
));
1759 if (vars
->variables
== NULL
)
1761 slang_info_log_memory (C
->L
);
1764 var
= vars
->variables
+ vars
->num_variables
;
1765 vars
->num_variables
++;
1766 slang_variable_construct (var
);
1767 var
->type
.qualifier
= type
->qualifier
;
1768 if (!parse_identifier (C
, &var
->name
))
1773 if (!slang_type_specifier_copy (&var
->type
.specifier
, &type
->specifier
))
1776 case VARIABLE_INITIALIZER
:
1777 if (!slang_type_specifier_copy (&var
->type
.specifier
, &type
->specifier
))
1779 var
->initializer
= (slang_operation
*) slang_alloc_malloc (sizeof (slang_operation
));
1780 if (var
->initializer
== NULL
)
1782 slang_info_log_memory (C
->L
);
1785 slang_operation_construct (var
->initializer
);
1786 if (!parse_expression (C
, var
->initializer
, vars
, structs
))
1789 case VARIABLE_ARRAY_UNKNOWN
:
1790 var
->type
.specifier
.type
= slang_spec_array
;
1791 var
->type
.specifier
._array
= (slang_type_specifier
*) slang_alloc_malloc (sizeof (
1792 slang_type_specifier
));
1793 if (var
->type
.specifier
._array
== NULL
)
1795 slang_info_log_memory (C
->L
);
1798 slang_type_specifier_construct (var
->type
.specifier
._array
);
1799 if (!slang_type_specifier_copy (var
->type
.specifier
._array
, &type
->specifier
))
1802 case VARIABLE_ARRAY_EXPLICIT
:
1803 var
->type
.specifier
.type
= slang_spec_array
;
1804 var
->type
.specifier
._array
= (slang_type_specifier
*) slang_alloc_malloc (sizeof (
1805 slang_type_specifier
));
1806 if (var
->type
.specifier
._array
== NULL
)
1808 slang_info_log_memory (C
->L
);
1811 slang_type_specifier_construct (var
->type
.specifier
._array
);
1812 if (!slang_type_specifier_copy (var
->type
.specifier
._array
, &type
->specifier
))
1814 var
->array_size
= (slang_operation
*) slang_alloc_malloc (sizeof (slang_operation
));
1815 if (var
->array_size
== NULL
)
1817 slang_info_log_memory (C
->L
);
1820 slang_operation_construct (var
->array_size
);
1821 if (!parse_expression (C
, var
->array_size
, vars
, structs
))
1830 static int parse_init_declarator_list (slang_parse_ctx
*C
, slang_variable_scope
*vars
,
1831 slang_struct_scope
*structs
)
1833 slang_fully_specified_type type
;
1835 slang_fully_specified_type_construct (&type
);
1836 if (!parse_fully_specified_type (C
, &type
, structs
, vars
))
1838 slang_fully_specified_type_destruct (&type
);
1843 if (!parse_init_declarator (C
, &type
, vars
, structs
))
1845 slang_fully_specified_type_destruct (&type
);
1849 while (*C
->I
++ == DECLARATOR_NEXT
);
1850 slang_fully_specified_type_destruct (&type
);
1854 static int parse_function (slang_parse_ctx
*C
, int definition
, slang_struct_scope
*structs
,
1855 slang_function_scope
*funcs
, slang_variable_scope
*scope
)
1857 slang_function parsed_func
, *found_func
;
1859 slang_function_construct (&parsed_func
);
1862 if (!parse_function_definition (C
, &parsed_func
, structs
, scope
))
1864 slang_function_destruct (&parsed_func
);
1870 if (!parse_function_prototype (C
, &parsed_func
, structs
, scope
))
1872 slang_function_destruct (&parsed_func
);
1876 found_func
= slang_function_scope_find (funcs
, &parsed_func
, 0);
1877 if (found_func
== NULL
)
1879 funcs
->functions
= (slang_function
*) slang_alloc_realloc (funcs
->functions
,
1880 funcs
->num_functions
* sizeof (slang_function
), (funcs
->num_functions
+ 1) * sizeof (
1882 if (funcs
->functions
== NULL
)
1884 slang_info_log_memory (C
->L
);
1885 slang_function_destruct (&parsed_func
);
1888 funcs
->functions
[funcs
->num_functions
] = parsed_func
;
1889 funcs
->num_functions
++;
1893 /* TODO check function return type qualifiers and specifiers */
1896 if (found_func
->body
!= NULL
)
1898 slang_info_log_error (C
->L
, "%s: function already has a body",
1899 parsed_func
.header
.name
);
1900 slang_function_destruct (&parsed_func
);
1903 slang_function_destruct (found_func
);
1904 *found_func
= parsed_func
;
1908 slang_function_destruct (&parsed_func
);
1915 #define DECLARATION_FUNCTION_PROTOTYPE 1
1916 #define DECLARATION_INIT_DECLARATOR_LIST 2
1918 static int parse_declaration (slang_parse_ctx
*C
, slang_variable_scope
*scope
,
1919 slang_struct_scope
*structs
, slang_function_scope
*funcs
)
1923 case DECLARATION_INIT_DECLARATOR_LIST
:
1924 if (!parse_init_declarator_list (C
, scope
, structs
))
1927 case DECLARATION_FUNCTION_PROTOTYPE
:
1928 if (!parse_function (C
, 0, structs
, funcs
, scope
))
1937 /* external declaration */
1938 #define EXTERNAL_NULL 0
1939 #define EXTERNAL_FUNCTION_DEFINITION 1
1940 #define EXTERNAL_DECLARATION 2
1942 static int parse_translation_unit (slang_parse_ctx
*C
, slang_translation_unit
*unit
)
1944 while (*C
->I
!= EXTERNAL_NULL
)
1948 case EXTERNAL_FUNCTION_DEFINITION
:
1949 if (!parse_function (C
, 1, unit
->structs
, &unit
->functions
, unit
->globals
))
1952 case EXTERNAL_DECLARATION
:
1953 if (!parse_declaration (C
, unit
->globals
, unit
->structs
, &unit
->functions
))
1964 static int compile_with_grammar (grammar id
, const char *source
, slang_translation_unit
*unit
,
1965 slang_unit_type type
, slang_info_log
*log
)
1968 unsigned int size
, start
, version
;
1971 /* retrieve version */
1972 if (!_slang_preprocess_version (source
, &version
, &start
, log
))
1975 slang_translation_unit_construct (unit
);
1977 if (!grammar_fast_check (id
, (const byte
*) source
+ start
, &prod
, &size
, 65536))
1981 grammar_get_last_error ( (unsigned char*) buf
, 1024, (int*) &pos
);
1982 slang_info_log_error (log
, buf
);
1987 if (!check_revision (&C
))
1989 grammar_alloc_free (prod
);
1992 if (!parse_translation_unit (&C
, unit
))
1994 slang_translation_unit_destruct (unit
);
1995 grammar_alloc_free (prod
);
1998 grammar_alloc_free (prod
);
2002 static const char *slang_shader_syn
=
2003 #include "library/slang_shader_syn.h"
2006 int _slang_compile (const char *source
, slang_translation_unit
*unit
, slang_unit_type type
,
2007 slang_info_log
*log
)
2011 id
= grammar_load_from_text ((const byte
*) slang_shader_syn
);
2016 grammar_get_last_error ( (unsigned char*) buf
, 1024, (int*) &pos
);
2017 slang_info_log_error (log
, buf
);
2021 grammar_set_reg8 (id
, (const byte
*) "parsing_builtin", 1);
2022 if (type
== slang_unit_fragment_shader
)
2023 grammar_set_reg8 (id
, (const byte
*) "shader_type", 1);
2025 grammar_set_reg8 (id
, (const byte
*) "shader_type", 2);
2027 if (!compile_with_grammar (id
, source
, unit
, type
, log
))
2029 grammar_destroy (id
);
2033 grammar_destroy (id
);