Updates for OpenVMS : New makefiles
[mesa.git] / src / mesa / shader / slang / slang_compile.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.3
4 *
5 * Copyright (C) 2005 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25 /**
26 * \file slang_compile.c
27 * slang front-end compiler
28 * \author Michal Krol
29 */
30
31 #include "imports.h"
32 #include "slang_compile.h"
33 #include "grammar_mesa.h"
34 #include "slang_preprocess.h"
35
36 /*
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.
41 */
42
43 void slang_alloc_free (void *ptr)
44 {
45 _mesa_free (ptr);
46 }
47
48 void *slang_alloc_malloc (unsigned int size)
49 {
50 return _mesa_malloc (size);
51 }
52
53 void *slang_alloc_realloc (void *ptr, unsigned int old_size, unsigned int size)
54 {
55 return _mesa_realloc (ptr, old_size, size);
56 }
57
58 int slang_string_compare (const char *str1, const char *str2)
59 {
60 return _mesa_strcmp (str1, str2);
61 }
62
63 char *slang_string_copy (char *dst, const char *src)
64 {
65 return _mesa_strcpy (dst, src);
66 }
67
68 char *slang_string_concat (char *dst, const char *src)
69 {
70 return _mesa_strcpy (dst + _mesa_strlen (dst), src);
71 }
72
73 char *slang_string_duplicate (const char *src)
74 {
75 return _mesa_strdup (src);
76 }
77
78 unsigned int slang_string_length (const char *str)
79 {
80 return _mesa_strlen (str);
81 }
82
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 *);
90
91 /* slang_type_specifier */
92
93 static void slang_type_specifier_construct (slang_type_specifier *spec)
94 {
95 spec->type = slang_spec_void;
96 spec->_struct = NULL;
97 spec->_array = NULL;
98 }
99
100 static void slang_type_specifier_destruct (slang_type_specifier *spec)
101 {
102 if (spec->_struct != NULL)
103 {
104 slang_struct_destruct (spec->_struct);
105 slang_alloc_free (spec->_struct);
106 }
107 if (spec->_array != NULL)
108 {
109 slang_type_specifier_destruct (spec->_array);
110 slang_alloc_free (spec->_array);
111 }
112 }
113
114 static int slang_type_specifier_copy (slang_type_specifier *x, const slang_type_specifier *y)
115 {
116 slang_type_specifier_destruct (x);
117 slang_type_specifier_construct (x);
118 x->type = y->type;
119 if (x->type == slang_spec_struct)
120 {
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);
124 }
125 if (x->type == slang_spec_array)
126 {
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);
130 }
131 return 1;
132 }
133
134 static int slang_type_specifier_equal (const slang_type_specifier *x, const slang_type_specifier *y)
135 {
136 if (x->type != y->type)
137 return 0;
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);
142 return 1;
143 }
144
145 /* slang_fully_specified_type */
146
147 static void slang_fully_specified_type_construct (slang_fully_specified_type *type)
148 {
149 type->qualifier = slang_qual_none;
150 slang_type_specifier_construct (&type->specifier);
151 }
152
153 static void slang_fully_specified_type_destruct (slang_fully_specified_type *type)
154 {
155 slang_type_specifier_destruct (&type->specifier);
156 }
157
158 static int slang_fully_specified_type_copy (slang_fully_specified_type *x,
159 const slang_fully_specified_type *y)
160 {
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);
165 }
166
167 /* slang_variable_scope */
168
169 static void slang_variable_scope_construct (slang_variable_scope *scope)
170 {
171 scope->variables = NULL;
172 scope->num_variables = 0;
173 scope->outer_scope = NULL;
174 }
175
176 static void slang_variable_scope_destruct (slang_variable_scope *scope)
177 {
178 unsigned int i;
179 for (i = 0; i < scope->num_variables; i++)
180 slang_variable_destruct (scope->variables + i);
181 slang_alloc_free (scope->variables);
182 }
183
184 static int slang_variable_scope_copy (slang_variable_scope *x, const slang_variable_scope *y)
185 {
186 unsigned int i;
187 slang_variable_scope_destruct (x);
188 slang_variable_scope_construct (x);
189 x->variables = (slang_variable *) slang_alloc_malloc (y->num_variables * sizeof (
190 slang_variable));
191 if (x->variables == NULL)
192 return 0;
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))
198 return 0;
199 x->outer_scope = y->outer_scope;
200 return 1;
201 }
202
203 /* slang_operation */
204 /* XXX mem! */
205 static void slang_operation_construct (slang_operation *oper)
206 {
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);
214 }
215
216 static void slang_operation_destruct (slang_operation *oper)
217 {
218 unsigned int i;
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);
225 }
226
227 static int slang_operation_copy (slang_operation *x, const slang_operation *y)
228 {
229 unsigned int i;
230 slang_operation_destruct (x);
231 slang_operation_construct (x);
232 x->type = y->type;
233 x->children = (slang_operation *) slang_alloc_malloc (y->num_children * sizeof (
234 slang_operation));
235 if (x->children == NULL)
236 return 0;
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))
242 return 0;
243 x->literal = y->literal;
244 if (y->identifier != NULL)
245 {
246 x->identifier = slang_string_duplicate (y->identifier);
247 if (x->identifier == NULL)
248 return 0;
249 }
250 if (!slang_variable_scope_copy (x->locals, y->locals))
251 return 0;
252 return 1;
253 }
254
255 /* slang_variable */
256
257 static void slang_variable_construct (slang_variable *var)
258 {
259 slang_fully_specified_type_construct (&var->type);
260 var->name = NULL;
261 var->array_size = NULL;
262 var->initializer = NULL;
263 }
264
265 static void slang_variable_destruct (slang_variable *var)
266 {
267 slang_fully_specified_type_destruct (&var->type);
268 slang_alloc_free (var->name);
269 if (var->array_size != NULL)
270 {
271 slang_operation_destruct (var->array_size);
272 slang_alloc_free (var->array_size);
273 }
274 if (var->initializer != NULL)
275 {
276 slang_operation_destruct (var->initializer);
277 slang_alloc_free (var->initializer);
278 }
279 }
280
281 static int slang_variable_copy (slang_variable *x, const slang_variable *y)
282 {
283 slang_variable_destruct (x);
284 slang_variable_construct (x);
285 if (!slang_fully_specified_type_copy (&x->type, &y->type))
286 return 0;
287 if (y->name != NULL)
288 {
289 x->name = slang_string_duplicate (y->name);
290 if (x->name == NULL)
291 return 0;
292 }
293 if (y->array_size != NULL)
294 {
295 x->array_size = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation));
296 if (x->array_size == NULL)
297 return 0;
298 slang_operation_construct (x->array_size);
299 if (!slang_operation_copy (x->array_size, y->array_size))
300 return 0;
301 }
302 if (y->initializer != NULL)
303 {
304 x->initializer = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation));
305 if (x->initializer == NULL)
306 return 0;
307 slang_operation_construct (x->initializer);
308 if (!slang_operation_copy (x->initializer, y->initializer))
309 return 0;
310 }
311 return 1;
312 }
313
314 /* slang_struct_scope */
315
316 static void slang_struct_scope_construct (slang_struct_scope *scope)
317 {
318 scope->structs = NULL;
319 scope->num_structs = 0;
320 scope->outer_scope = NULL;
321 }
322
323 static void slang_struct_scope_destruct (slang_struct_scope *scope)
324 {
325 unsigned int i;
326 for (i = 0; i < scope->num_structs; i++)
327 slang_struct_destruct (scope->structs + i);
328 slang_alloc_free (scope->structs);
329 }
330
331 static int slang_struct_scope_copy (slang_struct_scope *x, const slang_struct_scope *y)
332 {
333 unsigned int i;
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)
338 return 0;
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))
344 return 0;
345 x->outer_scope = y->outer_scope;
346 return 1;
347 }
348
349 static slang_struct *slang_struct_scope_find (slang_struct_scope *stru, const char *name,
350 int all_scopes)
351 {
352 unsigned int i;
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);
358 return NULL;
359 }
360
361 /* slang_struct */
362 /* XXX mem! */
363 static void slang_struct_construct (slang_struct *stru)
364 {
365 stru->name = NULL;
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);
370 }
371
372 static void slang_struct_destruct (slang_struct *stru)
373 {
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);
379 }
380
381 static int slang_struct_copy (slang_struct *x, const slang_struct *y)
382 {
383 slang_struct_destruct (x);
384 slang_struct_construct (x);
385 if (y->name != NULL)
386 {
387 x->name = slang_string_duplicate (y->name);
388 if (x->name == NULL)
389 return 0;
390 }
391 if (!slang_variable_scope_copy (x->fields, y->fields))
392 return 0;
393 if (!slang_struct_scope_copy (x->structs, y->structs))
394 return 0;
395 return 1;
396 }
397
398 static int slang_struct_equal (const slang_struct *x, const slang_struct *y)
399 {
400 unsigned int i;
401 if (x->fields->num_variables != y->fields->num_variables)
402 return 0;
403 for (i = 0; i < x->fields->num_variables; i++)
404 {
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)
408 return 0;
409 if (!slang_type_specifier_equal (&varx->type.specifier, &vary->type.specifier))
410 return 0;
411 if (varx->type.specifier.type == slang_spec_array)
412 {
413 /* TODO compare array sizes */
414 }
415 }
416 return 1;
417 }
418
419 /* slang_function */
420 /* XXX mem! */
421 static void slang_function_construct (slang_function *func)
422 {
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);
427 func->body = NULL;
428 }
429
430 static void slang_function_destruct (slang_function *func)
431 {
432 slang_variable_destruct (&func->header);
433 slang_variable_scope_destruct (func->parameters);
434 slang_alloc_free (func->parameters);
435 if (func->body != NULL)
436 {
437 slang_operation_destruct (func->body);
438 slang_alloc_free (func->body);
439 }
440 }
441
442 /* slang_function_scope */
443
444 static void slang_function_scope_construct (slang_function_scope *scope)
445 {
446 scope->functions = NULL;
447 scope->num_functions = 0;
448 scope->outer_scope = NULL;
449 }
450
451 static void slang_function_scope_destruct (slang_function_scope *scope)
452 {
453 unsigned int i;
454 for (i = 0; i < scope->num_functions; i++)
455 slang_function_destruct (scope->functions + i);
456 slang_alloc_free (scope->functions);
457 }
458
459 static slang_function *slang_function_scope_find (slang_function_scope *funcs, slang_function *fun,
460 int all_scopes)
461 {
462 unsigned int i;
463 for (i = 0; i < funcs->num_functions; i++)
464 {
465 slang_function *f = funcs->functions + i;
466 unsigned int j;
467 if (slang_string_compare (fun->header.name, f->header.name) != 0)
468 continue;
469 if (fun->param_count != f->param_count)
470 continue;
471 for (j = 0; j < fun->param_count; j++)
472 {
473 if (!slang_type_specifier_equal (&fun->parameters->variables[j].type.specifier,
474 &f->parameters->variables[j].type.specifier))
475 {
476 break;
477 }
478 }
479 if (j == fun->param_count)
480 return f;
481 }
482 if (all_scopes && funcs->outer_scope != NULL)
483 return slang_function_scope_find (funcs->outer_scope, fun, 1);
484 return NULL;
485 }
486
487 /* slang_translation_unit */
488 /* XXX mem! */
489 static void slang_translation_unit_construct (slang_translation_unit *unit)
490 {
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);
496 }
497
498 static void slang_translation_unit_destruct (slang_translation_unit *unit)
499 {
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);
505 }
506
507 /* slang_info_log */
508
509 static char *out_of_memory = "error: out of memory\n";
510
511 void slang_info_log_construct (slang_info_log *log)
512 {
513 log->text = NULL;
514 log->dont_free_text = 0;
515 }
516
517 void slang_info_log_destruct (slang_info_log *log)
518 {
519 if (!log->dont_free_text)
520 slang_alloc_free (log->text);
521 }
522
523 static int slang_info_log_message (slang_info_log *log, const char *prefix, const char *msg)
524 {
525 unsigned int new_size;
526 if (log->dont_free_text)
527 return 0;
528 new_size = slang_string_length (prefix) + 3 + slang_string_length (msg);
529 if (log->text != NULL)
530 {
531 log->text = (char *) slang_alloc_realloc (log->text, slang_string_length (log->text) + 1,
532 new_size + slang_string_length (log->text) + 1);
533 }
534 else
535 {
536 log->text = (char *) slang_alloc_malloc (new_size + 1);
537 if (log->text != NULL)
538 *log->text = '\0';
539 }
540 if (log->text == NULL)
541 return 0;
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");
546 return 1;
547 }
548
549 int slang_info_log_error (slang_info_log *log, const char *msg, ...)
550 {
551 if (slang_info_log_message (log, "error", msg))
552 return 1;
553 slang_info_log_memory (log);
554 return 0;
555 }
556
557 int slang_info_log_warning (slang_info_log *log, const char *msg, ...)
558 {
559 if (slang_info_log_message (log, "warning", msg))
560 return 1;
561 slang_info_log_memory (log);
562 return 0;
563 }
564
565 void slang_info_log_memory (slang_info_log *log)
566 {
567 if (!slang_info_log_message (log, "error", "out of memory"))
568 {
569 log->dont_free_text = 1;
570 log->text = out_of_memory;
571 }
572 }
573
574 /* slang_parse_ctx */
575
576 typedef struct slang_parse_ctx_
577 {
578 const byte *I;
579 slang_info_log *L;
580 } slang_parse_ctx;
581
582 /* --- */
583
584 static int parse_identifier (slang_parse_ctx *C, char **id)
585 {
586 *id = slang_string_duplicate ((const char *) C->I);
587 if (*id == NULL)
588 {
589 slang_info_log_memory (C->L);
590 return 0;
591 }
592 C->I += strlen ((const char *) C->I) + 1;
593 return 1;
594 }
595
596 static int parse_number (slang_parse_ctx *C, int *number)
597 {
598 const int radix = (int) (*C->I++);
599 *number = 0;
600 while (*C->I != '\0')
601 {
602 int digit;
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;
607 else
608 digit = (int) (*C->I - 'a') + 10;
609 *number = *number * radix + digit;
610 C->I++;
611 }
612 C->I++;
613 if (*number > 65535)
614 slang_info_log_warning (C->L, "%d: literal integer overflow", *number);
615 return 1;
616 }
617
618 static int parse_float (slang_parse_ctx *C, float *number)
619 {
620 char *integral = NULL;
621 char *fractional = NULL;
622 char *exponent = NULL;
623 char *whole = NULL;
624
625 if (!parse_identifier (C, &integral))
626 return 0;
627
628 if (!parse_identifier (C, &fractional))
629 {
630 slang_alloc_free (integral);
631 return 0;
632 }
633
634 if (!parse_identifier (C, &exponent))
635 {
636 slang_alloc_free (fractional);
637 slang_alloc_free (integral);
638 return 0;
639 }
640
641 whole = (char *) (slang_alloc_malloc ((strlen (integral) + strlen (fractional) + strlen (
642 exponent) + 3) * sizeof (char)));
643 if (whole == NULL)
644 {
645 slang_alloc_free (exponent);
646 slang_alloc_free (fractional);
647 slang_alloc_free (integral);
648 slang_info_log_memory (C->L);
649 return 0;
650 }
651
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);
657
658 *number = (float) (atof (whole));
659
660 slang_alloc_free (whole);
661 slang_alloc_free (exponent);
662 slang_alloc_free (fractional);
663 slang_alloc_free (integral);
664 return 1;
665 }
666
667 /* revision number - increment after each change affecting emitted output */
668 #define REVISION 2
669
670 static int check_revision (slang_parse_ctx *C)
671 {
672 if (*C->I != REVISION)
673 {
674 slang_info_log_error (C->L, "internal compiler error");
675 return 0;
676 }
677 C->I++;
678 return 1;
679 }
680
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 *);
685
686 /* type qualifier */
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
694
695 static int parse_type_qualifier (slang_parse_ctx *C, slang_type_qualifier *qual)
696 {
697 switch (*C->I++)
698 {
699 case TYPE_QUALIFIER_NONE:
700 *qual = slang_qual_none;
701 break;
702 case TYPE_QUALIFIER_CONST:
703 *qual = slang_qual_const;
704 break;
705 case TYPE_QUALIFIER_ATTRIBUTE:
706 *qual = slang_qual_attribute;
707 break;
708 case TYPE_QUALIFIER_VARYING:
709 *qual = slang_qual_varying;
710 break;
711 case TYPE_QUALIFIER_UNIFORM:
712 *qual = slang_qual_uniform;
713 break;
714 case TYPE_QUALIFIER_FIXEDOUTPUT:
715 *qual = slang_qual_fixedoutput;
716 break;
717 case TYPE_QUALIFIER_FIXEDINPUT:
718 *qual = slang_qual_fixedinput;
719 break;
720 default:
721 return 0;
722 }
723 return 1;
724 }
725
726 /* type specifier */
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
751
752 /* structure field */
753 #define FIELD_NONE 0
754 #define FIELD_NEXT 1
755 #define FIELD_ARRAY 2
756
757 static int parse_type_specifier (slang_parse_ctx *C, slang_type_specifier *spec,
758 slang_struct_scope *structs, slang_variable_scope *scope)
759 {
760 switch (*C->I++)
761 {
762 case TYPE_SPECIFIER_VOID:
763 spec->type = slang_spec_void;
764 break;
765 case TYPE_SPECIFIER_BOOL:
766 spec->type = slang_spec_bool;
767 break;
768 case TYPE_SPECIFIER_BVEC2:
769 spec->type = slang_spec_bvec2;
770 break;
771 case TYPE_SPECIFIER_BVEC3:
772 spec->type = slang_spec_bvec3;
773 break;
774 case TYPE_SPECIFIER_BVEC4:
775 spec->type = slang_spec_bvec4;
776 break;
777 case TYPE_SPECIFIER_INT:
778 spec->type = slang_spec_int;
779 break;
780 case TYPE_SPECIFIER_IVEC2:
781 spec->type = slang_spec_ivec2;
782 break;
783 case TYPE_SPECIFIER_IVEC3:
784 spec->type = slang_spec_ivec3;
785 break;
786 case TYPE_SPECIFIER_IVEC4:
787 spec->type = slang_spec_ivec4;
788 break;
789 case TYPE_SPECIFIER_FLOAT:
790 spec->type = slang_spec_float;
791 break;
792 case TYPE_SPECIFIER_VEC2:
793 spec->type = slang_spec_vec2;
794 break;
795 case TYPE_SPECIFIER_VEC3:
796 spec->type = slang_spec_vec3;
797 break;
798 case TYPE_SPECIFIER_VEC4:
799 spec->type = slang_spec_vec4;
800 break;
801 case TYPE_SPECIFIER_MAT2:
802 spec->type = slang_spec_mat2;
803 break;
804 case TYPE_SPECIFIER_MAT3:
805 spec->type = slang_spec_mat3;
806 break;
807 case TYPE_SPECIFIER_MAT4:
808 spec->type = slang_spec_mat4;
809 break;
810 case TYPE_SPECIFIER_SAMPLER1D:
811 spec->type = slang_spec_sampler1D;
812 break;
813 case TYPE_SPECIFIER_SAMPLER2D:
814 spec->type = slang_spec_sampler2D;
815 break;
816 case TYPE_SPECIFIER_SAMPLER3D:
817 spec->type = slang_spec_sampler3D;
818 break;
819 case TYPE_SPECIFIER_SAMPLERCUBE:
820 spec->type = slang_spec_samplerCube;
821 break;
822 case TYPE_SPECIFIER_SAMPLER1DSHADOW:
823 spec->type = slang_spec_sampler1DShadow;
824 break;
825 case TYPE_SPECIFIER_SAMPLER2DSHADOW:
826 spec->type = slang_spec_sampler2DShadow;
827 break;
828 case TYPE_SPECIFIER_STRUCT:
829 spec->type = slang_spec_struct;
830 {
831 char *name;
832 if (!parse_identifier (C, &name))
833 return 0;
834 if (*name != '\0' && slang_struct_scope_find (structs, name, 0) != NULL)
835 {
836 slang_info_log_error (C->L, "%s: duplicate type name", name);
837 slang_alloc_free (name);
838 return 0;
839 }
840 spec->_struct = (slang_struct *) slang_alloc_malloc (sizeof (slang_struct));
841 if (spec->_struct == NULL)
842 {
843 slang_alloc_free (name);
844 slang_info_log_memory (C->L);
845 return 0;
846 }
847 slang_struct_construct (spec->_struct);
848 spec->_struct->name = name;
849 spec->_struct->structs->outer_scope = structs;
850 }
851 do
852 {
853 slang_type_specifier sp;
854 slang_type_specifier_construct (&sp);
855 if (!parse_type_specifier (C, &sp, spec->_struct->structs, scope))
856 {
857 slang_type_specifier_destruct (&sp);
858 return 0;
859 }
860 do
861 {
862 slang_variable *var;
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)
868 {
869 slang_type_specifier_destruct (&sp);
870 slang_info_log_memory (C->L);
871 return 0;
872 }
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))
877 {
878 slang_type_specifier_destruct (&sp);
879 return 0;
880 }
881 if (!parse_identifier (C, &var->name))
882 {
883 slang_type_specifier_destruct (&sp);
884 return 0;
885 }
886 switch (*C->I++)
887 {
888 case FIELD_NONE:
889 break;
890 case FIELD_ARRAY:
891 var->array_size = (slang_operation *) slang_alloc_malloc (sizeof (
892 slang_operation));
893 if (var->array_size == NULL)
894 {
895 slang_type_specifier_destruct (&sp);
896 slang_info_log_memory (C->L);
897 return 0;
898 }
899 slang_operation_construct (var->array_size);
900 if (!parse_expression (C, var->array_size, scope, structs))
901 {
902 slang_type_specifier_destruct (&sp);
903 return 0;
904 }
905 break;
906 default:
907 return 0;
908 }
909 }
910 while (*C->I++ != FIELD_NONE);
911 }
912 while (*C->I++ != FIELD_NONE);
913 if (*spec->_struct->name != '\0')
914 {
915 slang_struct *s;
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)
920 {
921 slang_info_log_memory (C->L);
922 return 0;
923 }
924 s = structs->structs + structs->num_structs;
925 structs->num_structs++;
926 slang_struct_construct (s);
927 if (!slang_struct_copy (s, spec->_struct))
928 return 0;
929 }
930 break;
931 case TYPE_SPECIFIER_TYPENAME:
932 spec->type = slang_spec_struct;
933 {
934 char *name;
935 slang_struct *stru;
936 if (!parse_identifier (C, &name))
937 return 0;
938 stru = slang_struct_scope_find (structs, name, 1);
939 if (stru == NULL)
940 {
941 slang_info_log_error (C->L, "%s: undeclared type name", name);
942 slang_alloc_free (name);
943 return 0;
944 }
945 slang_alloc_free (name);
946 spec->_struct = (slang_struct *) slang_alloc_malloc (sizeof (slang_struct));
947 if (spec->_struct == NULL)
948 {
949 slang_info_log_memory (C->L);
950 return 0;
951 }
952 slang_struct_construct (spec->_struct);
953 if (!slang_struct_copy (spec->_struct, stru))
954 return 0;
955 }
956 break;
957 default:
958 return 0;
959 }
960 return 1;
961 }
962
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)
965 {
966 if (!parse_type_qualifier (C, &type->qualifier))
967 return 0;
968 return parse_type_specifier (C, &type->specifier, structs, scope);
969 }
970
971 /* operation */
972 #define OP_END 0
973 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
974 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
975 #define OP_DECLARE 3
976 #define OP_ASM 4
977 #define OP_BREAK 5
978 #define OP_CONTINUE 6
979 #define OP_DISCARD 7
980 #define OP_RETURN 8
981 #define OP_EXPRESSION 9
982 #define OP_IF 10
983 #define OP_WHILE 11
984 #define OP_DO 12
985 #define OP_FOR 13
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
992 #define OP_ASSIGN 20
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*/
1010 #define OP_EQUAL 38
1011 #define OP_NOTEQUAL 39
1012 #define OP_LESS 40
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*/
1018 #define OP_ADD 46
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
1025 #define OP_PLUS 53
1026 #define OP_MINUS 54
1027 /*#define OP_COMPLEMENT 55*/
1028 #define OP_NOT 56
1029 #define OP_SUBSCRIPT 57
1030 #define OP_CALL 58
1031 #define OP_FIELD 59
1032 #define OP_POSTINCREMENT 60
1033 #define OP_POSTDECREMENT 61
1034
1035 static int parse_child_operation (slang_parse_ctx *C, slang_operation *oper, int statement,
1036 slang_variable_scope *scope, slang_struct_scope *structs)
1037 {
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)
1042 {
1043 slang_info_log_memory (C->L);
1044 return 0;
1045 }
1046 slang_operation_construct (oper->children + oper->num_children);
1047 oper->num_children++;
1048 if (statement)
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);
1051 }
1052
1053 static int parse_declaration (slang_parse_ctx *C, slang_variable_scope *, slang_struct_scope *,
1054 slang_function_scope *);
1055
1056 static int parse_statement (slang_parse_ctx *C, slang_operation *oper, slang_variable_scope *scope,
1057 slang_struct_scope *structs)
1058 {
1059 oper->locals->outer_scope = scope;
1060 switch (*C->I++)
1061 {
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))
1066 return 0;
1067 C->I++;
1068 break;
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))
1073 return 0;
1074 C->I++;
1075 break;
1076 case OP_DECLARE:
1077 oper->type = slang_oper_variable_decl;
1078 {
1079 const unsigned int first_var = scope->num_variables;
1080 if (!parse_declaration (C, scope, structs, NULL))
1081 return 0;
1082 if (first_var < scope->num_variables)
1083 {
1084 const unsigned int num_vars = scope->num_variables - first_var;
1085 unsigned int i;
1086 oper->children = (slang_operation *) slang_alloc_malloc (num_vars * sizeof (
1087 slang_operation));
1088 if (oper->children == NULL)
1089 {
1090 slang_info_log_memory (C->L);
1091 return 0;
1092 }
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++)
1097 {
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)
1103 {
1104 slang_info_log_memory (C->L);
1105 return 0;
1106 }
1107 }
1108 }
1109 }
1110 break;
1111 case OP_ASM:
1112 oper->type = slang_oper_asm;
1113 if (!parse_identifier (C, &oper->identifier))
1114 return 0;
1115 while (*C->I != OP_END)
1116 if (!parse_child_operation (C, oper, 0, scope, structs))
1117 return 0;
1118 C->I++;
1119 break;
1120 case OP_BREAK:
1121 oper->type = slang_oper_break;
1122 break;
1123 case OP_CONTINUE:
1124 oper->type = slang_oper_continue;
1125 break;
1126 case OP_DISCARD:
1127 oper->type = slang_oper_discard;
1128 break;
1129 case OP_RETURN:
1130 oper->type = slang_oper_return;
1131 if (!parse_child_operation (C, oper, 0, scope, structs))
1132 return 0;
1133 break;
1134 case OP_EXPRESSION:
1135 oper->type = slang_oper_expression;
1136 if (!parse_child_operation (C, oper, 0, scope, structs))
1137 return 0;
1138 break;
1139 case OP_IF:
1140 oper->type = slang_oper_if;
1141 if (!parse_child_operation (C, oper, 0, scope, structs))
1142 return 0;
1143 if (!parse_child_operation (C, oper, 1, scope, structs))
1144 return 0;
1145 if (!parse_child_operation (C, oper, 1, scope, structs))
1146 return 0;
1147 break;
1148 case OP_WHILE:
1149 oper->type = slang_oper_while;
1150 if (!parse_child_operation (C, oper, 1, oper->locals, structs))
1151 return 0;
1152 if (!parse_child_operation (C, oper, 1, oper->locals, structs))
1153 return 0;
1154 break;
1155 case OP_DO:
1156 oper->type = slang_oper_do;
1157 if (!parse_child_operation (C, oper, 1, scope, structs))
1158 return 0;
1159 if (!parse_child_operation (C, oper, 0, scope, structs))
1160 return 0;
1161 break;
1162 case OP_FOR:
1163 oper->type = slang_oper_for;
1164 if (!parse_child_operation (C, oper, 1, oper->locals, structs))
1165 return 0;
1166 if (!parse_child_operation (C, oper, 1, oper->locals, structs))
1167 return 0;
1168 if (!parse_child_operation (C, oper, 0, oper->locals, structs))
1169 return 0;
1170 if (!parse_child_operation (C, oper, 1, oper->locals, structs))
1171 return 0;
1172 break;
1173 default:
1174 return 0;
1175 }
1176 return 1;
1177 }
1178
1179 static int handle_trinary_expression (slang_parse_ctx *C, slang_operation *op,
1180 slang_operation **ops, unsigned int *num_ops)
1181 {
1182 op->num_children = 3;
1183 op->children = (slang_operation *) slang_alloc_malloc (3 * sizeof (slang_operation));
1184 if (op->children == NULL)
1185 {
1186 slang_info_log_memory (C->L);
1187 return 0;
1188 }
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];
1193 *num_ops -= 3;
1194 *ops = (slang_operation *) slang_alloc_realloc (*ops, (*num_ops + 3) * sizeof (slang_operation),
1195 *num_ops * sizeof (slang_operation));
1196 if (*ops == NULL)
1197 {
1198 slang_info_log_memory (C->L);
1199 return 0;
1200 }
1201 return 1;
1202 }
1203
1204 static int handle_binary_expression (slang_parse_ctx *C, slang_operation *op,
1205 slang_operation **ops, unsigned int *num_ops)
1206 {
1207 op->num_children = 2;
1208 op->children = (slang_operation *) slang_alloc_malloc (2 * sizeof (slang_operation));
1209 if (op->children == NULL)
1210 {
1211 slang_info_log_memory (C->L);
1212 return 0;
1213 }
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];
1217 *num_ops -= 2;
1218 *ops = (slang_operation *) slang_alloc_realloc (*ops, (*num_ops + 2) * sizeof (slang_operation),
1219 *num_ops * sizeof (slang_operation));
1220 if (*ops == NULL)
1221 {
1222 slang_info_log_memory (C->L);
1223 return 0;
1224 }
1225 return 1;
1226 }
1227
1228 static int handle_unary_expression (slang_parse_ctx *C, slang_operation *op,
1229 slang_operation **ops, unsigned int *num_ops)
1230 {
1231 op->num_children = 1;
1232 op->children = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation));
1233 if (op->children == NULL)
1234 {
1235 slang_info_log_memory (C->L);
1236 return 0;
1237 }
1238 op->children[0] = (*ops)[*num_ops - 2];
1239 (*ops)[*num_ops - 2] = (*ops)[*num_ops - 1];
1240 (*num_ops)--;
1241 *ops = (slang_operation *) slang_alloc_realloc (*ops, (*num_ops + 1) * sizeof (slang_operation),
1242 *num_ops * sizeof (slang_operation));
1243 if (*ops == NULL)
1244 {
1245 slang_info_log_memory (C->L);
1246 return 0;
1247 }
1248 return 1;
1249 }
1250
1251 static int parse_expression (slang_parse_ctx *C, slang_operation *oper, slang_variable_scope *scope,
1252 slang_struct_scope *structs)
1253 {
1254 slang_operation *ops = NULL;
1255 unsigned int num_ops = 0;
1256 int number;
1257
1258 while (*C->I != OP_END)
1259 {
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));
1264 if (ops == NULL)
1265 {
1266 slang_info_log_memory (C->L);
1267 return 0;
1268 }
1269 op = ops + num_ops;
1270 num_ops++;
1271 slang_operation_construct (op);
1272 op->locals->outer_scope = scope;
1273 switch (op_code)
1274 {
1275 case OP_PUSH_VOID:
1276 op->type = slang_oper_void;
1277 break;
1278 case OP_PUSH_BOOL:
1279 op->type = slang_oper_literal_bool;
1280 if (!parse_number (C, &number))
1281 return 0;
1282 op->literal = (float) number;
1283 break;
1284 case OP_PUSH_INT:
1285 op->type = slang_oper_literal_int;
1286 if (!parse_number (C, &number))
1287 return 0;
1288 op->literal = (float) number;
1289 break;
1290 case OP_PUSH_FLOAT:
1291 op->type = slang_oper_literal_float;
1292 if (!parse_float (C, &op->literal))
1293 return 0;
1294 break;
1295 case OP_PUSH_IDENTIFIER:
1296 op->type = slang_oper_identifier;
1297 if (!parse_identifier (C, &op->identifier))
1298 return 0;
1299 break;
1300 case OP_SEQUENCE:
1301 op->type = slang_oper_sequence;
1302 if (!handle_binary_expression (C, op, &ops, &num_ops))
1303 return 0;
1304 break;
1305 case OP_ASSIGN:
1306 op->type = slang_oper_assign;
1307 if (!handle_binary_expression (C, op, &ops, &num_ops))
1308 return 0;
1309 break;
1310 case OP_ADDASSIGN:
1311 op->type = slang_oper_addassign;
1312 if (!handle_binary_expression (C, op, &ops, &num_ops))
1313 return 0;
1314 break;
1315 case OP_SUBASSIGN:
1316 op->type = slang_oper_subassign;
1317 if (!handle_binary_expression (C, op, &ops, &num_ops))
1318 return 0;
1319 break;
1320 case OP_MULASSIGN:
1321 op->type = slang_oper_mulassign;
1322 if (!handle_binary_expression (C, op, &ops, &num_ops))
1323 return 0;
1324 break;
1325 case OP_DIVASSIGN:
1326 op->type = slang_oper_divassign;
1327 if (!handle_binary_expression (C, op, &ops, &num_ops))
1328 return 0;
1329 break;
1330 /*case OP_MODASSIGN:*/
1331 /*case OP_LSHASSIGN:*/
1332 /*case OP_RSHASSIGN:*/
1333 /*case OP_ORASSIGN:*/
1334 /*case OP_XORASSIGN:*/
1335 /*case OP_ANDASSIGN:*/
1336 case OP_SELECT:
1337 op->type = slang_oper_select;
1338 if (!handle_trinary_expression (C, op, &ops, &num_ops))
1339 return 0;
1340 break;
1341 case OP_LOGICALOR:
1342 op->type = slang_oper_logicalor;
1343 if (!handle_binary_expression (C, op, &ops, &num_ops))
1344 return 0;
1345 break;
1346 case OP_LOGICALXOR:
1347 op->type = slang_oper_logicalxor;
1348 if (!handle_binary_expression (C, op, &ops, &num_ops))
1349 return 0;
1350 break;
1351 case OP_LOGICALAND:
1352 op->type = slang_oper_logicaland;
1353 if (!handle_binary_expression (C, op, &ops, &num_ops))
1354 return 0;
1355 break;
1356 /*case OP_BITOR:*/
1357 /*case OP_BITXOR:*/
1358 /*case OP_BITAND:*/
1359 case OP_EQUAL:
1360 op->type = slang_oper_equal;
1361 if (!handle_binary_expression (C, op, &ops, &num_ops))
1362 return 0;
1363 break;
1364 case OP_NOTEQUAL:
1365 op->type = slang_oper_notequal;
1366 if (!handle_binary_expression (C, op, &ops, &num_ops))
1367 return 0;
1368 break;
1369 case OP_LESS:
1370 op->type = slang_oper_less;
1371 if (!handle_binary_expression (C, op, &ops, &num_ops))
1372 return 0;
1373 break;
1374 case OP_GREATER:
1375 op->type = slang_oper_greater;
1376 if (!handle_binary_expression (C, op, &ops, &num_ops))
1377 return 0;
1378 break;
1379 case OP_LESSEQUAL:
1380 op->type = slang_oper_lessequal;
1381 if (!handle_binary_expression (C, op, &ops, &num_ops))
1382 return 0;
1383 break;
1384 case OP_GREATEREQUAL:
1385 op->type = slang_oper_greaterequal;
1386 if (!handle_binary_expression (C, op, &ops, &num_ops))
1387 return 0;
1388 break;
1389 /*case OP_LSHIFT:*/
1390 /*case OP_RSHIFT:*/
1391 case OP_ADD:
1392 op->type = slang_oper_add;
1393 if (!handle_binary_expression (C, op, &ops, &num_ops))
1394 return 0;
1395 break;
1396 case OP_SUBTRACT:
1397 op->type = slang_oper_subtract;
1398 if (!handle_binary_expression (C, op, &ops, &num_ops))
1399 return 0;
1400 break;
1401 case OP_MULTIPLY:
1402 op->type = slang_oper_multiply;
1403 if (!handle_binary_expression (C, op, &ops, &num_ops))
1404 return 0;
1405 break;
1406 case OP_DIVIDE:
1407 op->type = slang_oper_divide;
1408 if (!handle_binary_expression (C, op, &ops, &num_ops))
1409 return 0;
1410 break;
1411 /*case OP_MODULUS:*/
1412 case OP_PREINCREMENT:
1413 op->type = slang_oper_preincrement;
1414 if (!handle_unary_expression (C, op, &ops, &num_ops))
1415 return 0;
1416 break;
1417 case OP_PREDECREMENT:
1418 op->type = slang_oper_predecrement;
1419 if (!handle_unary_expression (C, op, &ops, &num_ops))
1420 return 0;
1421 break;
1422 case OP_PLUS:
1423 op->type = slang_oper_plus;
1424 if (!handle_unary_expression (C, op, &ops, &num_ops))
1425 return 0;
1426 break;
1427 case OP_MINUS:
1428 op->type = slang_oper_minus;
1429 if (!handle_unary_expression (C, op, &ops, &num_ops))
1430 return 0;
1431 break;
1432 case OP_NOT:
1433 op->type = slang_oper_not;
1434 if (!handle_unary_expression (C, op, &ops, &num_ops))
1435 return 0;
1436 break;
1437 /*case OP_COMPLEMENT:*/
1438 case OP_SUBSCRIPT:
1439 op->type = slang_oper_subscript;
1440 if (!handle_binary_expression (C, op, &ops, &num_ops))
1441 return 0;
1442 break;
1443 case OP_CALL:
1444 op->type = slang_oper_call;
1445 if (!parse_identifier (C, &op->identifier))
1446 return 0;
1447 while (*C->I != OP_END)
1448 if (!parse_child_operation (C, op, 0, scope, structs))
1449 return 0;
1450 C->I++;
1451 break;
1452 case OP_FIELD:
1453 op->type = slang_oper_field;
1454 if (!parse_identifier (C, &op->identifier))
1455 return 0;
1456 if (!handle_unary_expression (C, op, &ops, &num_ops))
1457 return 0;
1458 break;
1459 case OP_POSTINCREMENT:
1460 op->type = slang_oper_postincrement;
1461 if (!handle_unary_expression (C, op, &ops, &num_ops))
1462 return 0;
1463 break;
1464 case OP_POSTDECREMENT:
1465 op->type = slang_oper_postdecrement;
1466 if (!handle_unary_expression (C, op, &ops, &num_ops))
1467 return 0;
1468 break;
1469 default:
1470 return 0;
1471 }
1472 }
1473 C->I++;
1474 *oper = *ops;
1475 slang_alloc_free (ops);
1476 return 1;
1477 }
1478
1479 /* parameter qualifier */
1480 #define PARAM_QUALIFIER_IN 0
1481 #define PARAM_QUALIFIER_OUT 1
1482 #define PARAM_QUALIFIER_INOUT 2
1483
1484 /* function parameter array presence */
1485 #define PARAMETER_ARRAY_NOT_PRESENT 0
1486 #define PARAMETER_ARRAY_PRESENT 1
1487
1488 static int parse_parameter_declaration (slang_parse_ctx *C, slang_variable *param,
1489 slang_struct_scope *structs, slang_variable_scope *scope)
1490 {
1491 if (!parse_type_qualifier (C, &param->type.qualifier))
1492 return 0;
1493 switch (*C->I++)
1494 {
1495 case PARAM_QUALIFIER_IN:
1496 if (param->type.qualifier != slang_qual_const && param->type.qualifier != slang_qual_none)
1497 {
1498 slang_info_log_error (C->L, "invalid type qualifier");
1499 return 0;
1500 }
1501 break;
1502 case PARAM_QUALIFIER_OUT:
1503 if (param->type.qualifier == slang_qual_none)
1504 param->type.qualifier = slang_qual_out;
1505 else
1506 {
1507 slang_info_log_error (C->L, "invalid type qualifier");
1508 return 0;
1509 }
1510 break;
1511 case PARAM_QUALIFIER_INOUT:
1512 if (param->type.qualifier == slang_qual_none)
1513 param->type.qualifier = slang_qual_inout;
1514 else
1515 {
1516 slang_info_log_error (C->L, "invalid type qualifier");
1517 return 0;
1518 }
1519 break;
1520 default:
1521 return 0;
1522 }
1523 if (!parse_type_specifier (C, &param->type.specifier, structs, scope))
1524 return 0;
1525 if (!parse_identifier (C, &param->name))
1526 return 0;
1527 if (*C->I++ == PARAMETER_ARRAY_PRESENT)
1528 {
1529 param->array_size = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation));
1530 if (param->array_size == NULL)
1531 {
1532 slang_info_log_memory (C->L);
1533 return 0;
1534 }
1535 slang_operation_construct (param->array_size);
1536 if (!parse_expression (C, param->array_size, scope, structs))
1537 return 0;
1538 }
1539 return 1;
1540 }
1541
1542 /* function type */
1543 #define FUNCTION_ORDINARY 0
1544 #define FUNCTION_CONSTRUCTOR 1
1545 #define FUNCTION_OPERATOR 2
1546
1547 /* function parameter */
1548 #define PARAMETER_NONE 0
1549 #define PARAMETER_NEXT 1
1550
1551 /* operator type */
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
1584
1585 /* these must match with slang_type_specifier_type enum */
1586 static const char *type_specifier_names[] = {
1587 "void",
1588 "bool",
1589 "bvec2",
1590 "bvec3",
1591 "bvec4",
1592 "int",
1593 "ivec2",
1594 "ivec3",
1595 "ivec4",
1596 "float",
1597 "vec2",
1598 "vec3",
1599 "vec4",
1600 "mat2",
1601 "mat3",
1602 "mat4",
1603 "sampler1D",
1604 "sampler2D",
1605 "sampler3D",
1606 "samplerCube",
1607 "sampler1DShadow",
1608 "sampler2DShadow"
1609 };
1610
1611 static const struct {
1612 unsigned int o_code;
1613 const char *o_name;
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, "^" }*/
1647 };
1648
1649 static int parse_operator_name (slang_parse_ctx *C, char **pname)
1650 {
1651 unsigned int i;
1652 for (i = 0; i < sizeof (operator_names) / sizeof (*operator_names); i++)
1653 if (operator_names[i].o_code == (unsigned int) (*C->I))
1654 {
1655 *pname = slang_string_duplicate (operator_names[i].o_name);
1656 if (*pname == NULL)
1657 {
1658 slang_info_log_memory (C->L);
1659 return 0;
1660 }
1661 C->I++;
1662 return 1;
1663 }
1664 return 0;
1665 }
1666
1667 static int parse_function_prototype (slang_parse_ctx *C, slang_function *func,
1668 slang_struct_scope *structs, slang_variable_scope *scope)
1669 {
1670 if (!parse_fully_specified_type (C, &func->header.type, structs, scope))
1671 return 0;
1672 switch (*C->I++)
1673 {
1674 case FUNCTION_ORDINARY:
1675 func->kind = slang_func_ordinary;
1676 if (!parse_identifier (C, &func->header.name))
1677 return 0;
1678 break;
1679 case FUNCTION_CONSTRUCTOR:
1680 func->kind = slang_func_constructor;
1681 if (func->header.type.specifier.type == slang_spec_struct)
1682 return 0;
1683 func->header.name = slang_string_duplicate (
1684 type_specifier_names[func->header.type.specifier.type]);
1685 if (func->header.name == NULL)
1686 {
1687 slang_info_log_memory (C->L);
1688 return 0;
1689 }
1690 break;
1691 case FUNCTION_OPERATOR:
1692 func->kind = slang_func_operator;
1693 if (!parse_operator_name (C, &func->header.name))
1694 return 0;
1695 break;
1696 default:
1697 return 0;
1698 }
1699 func->parameters->outer_scope = scope;
1700 while (*C->I++ == PARAMETER_NEXT)
1701 {
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)
1707 {
1708 slang_info_log_memory (C->L);
1709 return 0;
1710 }
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))
1715 return 0;
1716 }
1717 func->param_count = func->parameters->num_variables;
1718 return 1;
1719 }
1720
1721 static int parse_function_definition (slang_parse_ctx *C, slang_function *func,
1722 slang_struct_scope *structs, slang_variable_scope *scope)
1723 {
1724 if (!parse_function_prototype (C, func, structs, scope))
1725 return 0;
1726 func->body = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation));
1727 if (func->body == NULL)
1728 {
1729 slang_info_log_memory (C->L);
1730 return 0;
1731 }
1732 slang_operation_construct (func->body);
1733 if (!parse_statement (C, func->body, func->parameters, structs))
1734 return 0;
1735 return 1;
1736 }
1737
1738 /* init declarator list */
1739 #define DECLARATOR_NONE 0
1740 #define DECLARATOR_NEXT 1
1741
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
1748
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)
1751 {
1752 slang_variable *var;
1753
1754 if (*C->I++ == VARIABLE_NONE)
1755 return 1;
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)
1760 {
1761 slang_info_log_memory (C->L);
1762 return 0;
1763 }
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))
1769 return 0;
1770 switch (*C->I++)
1771 {
1772 case VARIABLE_NONE:
1773 if (!slang_type_specifier_copy (&var->type.specifier, &type->specifier))
1774 return 0;
1775 break;
1776 case VARIABLE_INITIALIZER:
1777 if (!slang_type_specifier_copy (&var->type.specifier, &type->specifier))
1778 return 0;
1779 var->initializer = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation));
1780 if (var->initializer == NULL)
1781 {
1782 slang_info_log_memory (C->L);
1783 return 0;
1784 }
1785 slang_operation_construct (var->initializer);
1786 if (!parse_expression (C, var->initializer, vars, structs))
1787 return 0;
1788 break;
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)
1794 {
1795 slang_info_log_memory (C->L);
1796 return 0;
1797 }
1798 slang_type_specifier_construct (var->type.specifier._array);
1799 if (!slang_type_specifier_copy (var->type.specifier._array, &type->specifier))
1800 return 0;
1801 break;
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)
1807 {
1808 slang_info_log_memory (C->L);
1809 return 0;
1810 }
1811 slang_type_specifier_construct (var->type.specifier._array);
1812 if (!slang_type_specifier_copy (var->type.specifier._array, &type->specifier))
1813 return 0;
1814 var->array_size = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation));
1815 if (var->array_size == NULL)
1816 {
1817 slang_info_log_memory (C->L);
1818 return 0;
1819 }
1820 slang_operation_construct (var->array_size);
1821 if (!parse_expression (C, var->array_size, vars, structs))
1822 return 0;
1823 break;
1824 default:
1825 return 0;
1826 }
1827 return 1;
1828 }
1829
1830 static int parse_init_declarator_list (slang_parse_ctx *C, slang_variable_scope *vars,
1831 slang_struct_scope *structs)
1832 {
1833 slang_fully_specified_type type;
1834
1835 slang_fully_specified_type_construct (&type);
1836 if (!parse_fully_specified_type (C, &type, structs, vars))
1837 {
1838 slang_fully_specified_type_destruct (&type);
1839 return 0;
1840 }
1841 do
1842 {
1843 if (!parse_init_declarator (C, &type, vars, structs))
1844 {
1845 slang_fully_specified_type_destruct (&type);
1846 return 0;
1847 }
1848 }
1849 while (*C->I++ == DECLARATOR_NEXT);
1850 slang_fully_specified_type_destruct (&type);
1851 return 1;
1852 }
1853
1854 static int parse_function (slang_parse_ctx *C, int definition, slang_struct_scope *structs,
1855 slang_function_scope *funcs, slang_variable_scope *scope)
1856 {
1857 slang_function parsed_func, *found_func;
1858
1859 slang_function_construct (&parsed_func);
1860 if (definition)
1861 {
1862 if (!parse_function_definition (C, &parsed_func, structs, scope))
1863 {
1864 slang_function_destruct (&parsed_func);
1865 return 0;
1866 }
1867 }
1868 else
1869 {
1870 if (!parse_function_prototype (C, &parsed_func, structs, scope))
1871 {
1872 slang_function_destruct (&parsed_func);
1873 return 0;
1874 }
1875 }
1876 found_func = slang_function_scope_find (funcs, &parsed_func, 0);
1877 if (found_func == NULL)
1878 {
1879 funcs->functions = (slang_function *) slang_alloc_realloc (funcs->functions,
1880 funcs->num_functions * sizeof (slang_function), (funcs->num_functions + 1) * sizeof (
1881 slang_function));
1882 if (funcs->functions == NULL)
1883 {
1884 slang_info_log_memory (C->L);
1885 slang_function_destruct (&parsed_func);
1886 return 0;
1887 }
1888 funcs->functions[funcs->num_functions] = parsed_func;
1889 funcs->num_functions++;
1890 }
1891 else
1892 {
1893 /* TODO check function return type qualifiers and specifiers */
1894 if (definition)
1895 {
1896 if (found_func->body != NULL)
1897 {
1898 slang_info_log_error (C->L, "%s: function already has a body",
1899 parsed_func.header.name);
1900 slang_function_destruct (&parsed_func);
1901 return 0;
1902 }
1903 slang_function_destruct (found_func);
1904 *found_func = parsed_func;
1905 }
1906 else
1907 {
1908 slang_function_destruct (&parsed_func);
1909 }
1910 }
1911 return 1;
1912 }
1913
1914 /* declaration */
1915 #define DECLARATION_FUNCTION_PROTOTYPE 1
1916 #define DECLARATION_INIT_DECLARATOR_LIST 2
1917
1918 static int parse_declaration (slang_parse_ctx *C, slang_variable_scope *scope,
1919 slang_struct_scope *structs, slang_function_scope *funcs)
1920 {
1921 switch (*C->I++)
1922 {
1923 case DECLARATION_INIT_DECLARATOR_LIST:
1924 if (!parse_init_declarator_list (C, scope, structs))
1925 return 0;
1926 break;
1927 case DECLARATION_FUNCTION_PROTOTYPE:
1928 if (!parse_function (C, 0, structs, funcs, scope))
1929 return 0;
1930 break;
1931 default:
1932 return 0;
1933 }
1934 return 1;
1935 }
1936
1937 /* external declaration */
1938 #define EXTERNAL_NULL 0
1939 #define EXTERNAL_FUNCTION_DEFINITION 1
1940 #define EXTERNAL_DECLARATION 2
1941
1942 static int parse_translation_unit (slang_parse_ctx *C, slang_translation_unit *unit)
1943 {
1944 while (*C->I != EXTERNAL_NULL)
1945 {
1946 switch (*C->I++)
1947 {
1948 case EXTERNAL_FUNCTION_DEFINITION:
1949 if (!parse_function (C, 1, unit->structs, &unit->functions, unit->globals))
1950 return 0;
1951 break;
1952 case EXTERNAL_DECLARATION:
1953 if (!parse_declaration (C, unit->globals, unit->structs, &unit->functions))
1954 return 0;
1955 break;
1956 default:
1957 return 0;
1958 }
1959 }
1960 C->I++;
1961 return 1;
1962 }
1963
1964 static int compile_with_grammar (grammar id, const char *source, slang_translation_unit *unit,
1965 slang_unit_type type, slang_info_log *log)
1966 {
1967 byte *prod;
1968 unsigned int size, start, version;
1969 slang_parse_ctx C;
1970
1971 /* retrieve version */
1972 if (!_slang_preprocess_version (source, &version, &start, log))
1973 return 0;
1974
1975 slang_translation_unit_construct (unit);
1976 unit->type = type;
1977 if (!grammar_fast_check (id, (const byte *) source + start, &prod, &size, 65536))
1978 {
1979 char buf[1024];
1980 unsigned int pos;
1981 grammar_get_last_error ( (unsigned char*) buf, 1024, (int*) &pos);
1982 slang_info_log_error (log, buf);
1983 return 0;
1984 }
1985 C.I = prod;
1986 C.L = log;
1987 if (!check_revision (&C))
1988 {
1989 grammar_alloc_free (prod);
1990 return 0;
1991 }
1992 if (!parse_translation_unit (&C, unit))
1993 {
1994 slang_translation_unit_destruct (unit);
1995 grammar_alloc_free (prod);
1996 return 0;
1997 }
1998 grammar_alloc_free (prod);
1999 return 1;
2000 }
2001
2002 static const char *slang_shader_syn =
2003 #include "library/slang_shader_syn.h"
2004 ;
2005
2006 int _slang_compile (const char *source, slang_translation_unit *unit, slang_unit_type type,
2007 slang_info_log *log)
2008 {
2009 grammar id;
2010
2011 id = grammar_load_from_text ((const byte *) slang_shader_syn);
2012 if (id == 0)
2013 {
2014 char buf[1024];
2015 unsigned int pos;
2016 grammar_get_last_error ( (unsigned char*) buf, 1024, (int*) &pos);
2017 slang_info_log_error (log, buf);
2018 return 0;
2019 }
2020
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);
2024 else
2025 grammar_set_reg8 (id, (const byte *) "shader_type", 2);
2026
2027 if (!compile_with_grammar (id, source, unit, type, log))
2028 {
2029 grammar_destroy (id);
2030 return 0;
2031 }
2032
2033 grammar_destroy (id);
2034 return 1;
2035 }
2036