additional wrapper updates, bug 4468
[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 "grammar_mesa.h"
33 #include "slang_utility.h"
34 #include "slang_compile.h"
35 #include "slang_preprocess.h"
36 #include "slang_storage.h"
37 #include "slang_assemble.h"
38 #include "slang_execute.h"
39
40 /*
41 This is a straightforward implementation of the slang front-end compiler.
42 Lots of error-checking functionality is missing but every well-formed shader source should
43 compile successfully and execute as expected. However, some semantically ill-formed shaders
44 may be accepted resulting in undefined behaviour.
45 */
46
47 static void slang_variable_construct (slang_variable *);
48 static int slang_variable_copy (slang_variable *, const slang_variable *);
49 static void slang_struct_destruct (slang_struct *);
50 static int slang_struct_equal (const slang_struct *, const slang_struct *);
51 static void slang_variable_destruct (slang_variable *);
52
53 /* slang_type_specifier_type */
54
55 /* these must match with slang_type_specifier_type enum */
56 static const char *type_specifier_type_names[] = {
57 "void",
58 "bool",
59 "bvec2",
60 "bvec3",
61 "bvec4",
62 "int",
63 "ivec2",
64 "ivec3",
65 "ivec4",
66 "float",
67 "vec2",
68 "vec3",
69 "vec4",
70 "mat2",
71 "mat3",
72 "mat4",
73 "sampler1D",
74 "sampler2D",
75 "sampler3D",
76 "samplerCube",
77 "sampler1DShadow",
78 "sampler2DShadow",
79 NULL
80 };
81
82 slang_type_specifier_type slang_type_specifier_type_from_string (const char *name)
83 {
84 const char **p = type_specifier_type_names;
85 while (*p != NULL)
86 {
87 if (slang_string_compare (*p, name) == 0)
88 return (slang_type_specifier_type) (p - type_specifier_type_names);
89 p++;
90 }
91 return slang_spec_void;
92 }
93
94 /* slang_type_specifier */
95
96 void slang_type_specifier_construct (slang_type_specifier *spec)
97 {
98 spec->type = slang_spec_void;
99 spec->_struct = NULL;
100 spec->_array = NULL;
101 }
102
103 void slang_type_specifier_destruct (slang_type_specifier *spec)
104 {
105 if (spec->_struct != NULL)
106 {
107 slang_struct_destruct (spec->_struct);
108 slang_alloc_free (spec->_struct);
109 }
110 if (spec->_array != NULL)
111 {
112 slang_type_specifier_destruct (spec->_array);
113 slang_alloc_free (spec->_array);
114 }
115 }
116
117 int slang_type_specifier_copy (slang_type_specifier *x, const slang_type_specifier *y)
118 {
119 slang_type_specifier_destruct (x);
120 slang_type_specifier_construct (x);
121 x->type = y->type;
122 if (x->type == slang_spec_struct)
123 {
124 x->_struct = (slang_struct *) slang_alloc_malloc (sizeof (slang_struct));
125 if (x->_struct == NULL)
126 return 0;
127 if (!slang_struct_construct_a (x->_struct))
128 {
129 slang_alloc_free (x->_struct);
130 x->_struct = NULL;
131 return 0;
132 }
133 return slang_struct_copy (x->_struct, y->_struct);
134 }
135 if (x->type == slang_spec_array)
136 {
137 x->_array = (slang_type_specifier *) slang_alloc_malloc (sizeof (slang_type_specifier));
138 if (x->_array == NULL)
139 return 0;
140 slang_type_specifier_construct (x->_array);
141 return slang_type_specifier_copy (x->_array, y->_array);
142 }
143 return 1;
144 }
145
146 int slang_type_specifier_equal (const slang_type_specifier *x, const slang_type_specifier *y)
147 {
148 if (x->type != y->type)
149 return 0;
150 if (x->type == slang_spec_struct)
151 return slang_struct_equal (x->_struct, y->_struct);
152 if (x->type == slang_spec_array)
153 return slang_type_specifier_equal (x->_array, y->_array);
154 return 1;
155 }
156
157 /* slang_fully_specified_type */
158
159 static void slang_fully_specified_type_construct (slang_fully_specified_type *type)
160 {
161 type->qualifier = slang_qual_none;
162 slang_type_specifier_construct (&type->specifier);
163 }
164
165 static void slang_fully_specified_type_destruct (slang_fully_specified_type *type)
166 {
167 slang_type_specifier_destruct (&type->specifier);
168 }
169
170 static int slang_fully_specified_type_copy (slang_fully_specified_type *x,
171 const slang_fully_specified_type *y)
172 {
173 slang_fully_specified_type_construct (x);
174 slang_fully_specified_type_destruct (x);
175 x->qualifier = y->qualifier;
176 return slang_type_specifier_copy (&x->specifier, &y->specifier);
177 }
178
179 /* slang_variable_scope */
180
181 static void slang_variable_scope_construct (slang_variable_scope *scope)
182 {
183 scope->variables = NULL;
184 scope->num_variables = 0;
185 scope->outer_scope = NULL;
186 }
187
188 static void slang_variable_scope_destruct (slang_variable_scope *scope)
189 {
190 unsigned int i;
191 for (i = 0; i < scope->num_variables; i++)
192 slang_variable_destruct (scope->variables + i);
193 slang_alloc_free (scope->variables);
194 }
195
196 static int slang_variable_scope_copy (slang_variable_scope *x, const slang_variable_scope *y)
197 {
198 unsigned int i;
199 slang_variable_scope_destruct (x);
200 slang_variable_scope_construct (x);
201 x->variables = (slang_variable *) slang_alloc_malloc (y->num_variables * sizeof (
202 slang_variable));
203 if (x->variables == NULL)
204 return 0;
205 x->num_variables = y->num_variables;
206 for (i = 0; i < x->num_variables; i++)
207 slang_variable_construct (x->variables + i);
208 for (i = 0; i < x->num_variables; i++)
209 if (!slang_variable_copy (x->variables + i, y->variables + i))
210 return 0;
211 x->outer_scope = y->outer_scope;
212 return 1;
213 }
214
215 /* slang_operation */
216
217 int slang_operation_construct_a (slang_operation *oper)
218 {
219 oper->type = slang_oper_none;
220 oper->children = NULL;
221 oper->num_children = 0;
222 oper->literal = (float) 0;
223 oper->identifier = NULL;
224 oper->locals = (slang_variable_scope *) slang_alloc_malloc (sizeof (slang_variable_scope));
225 if (oper->locals == NULL)
226 return 0;
227 slang_variable_scope_construct (oper->locals);
228 return 1;
229 }
230
231 void slang_operation_destruct (slang_operation *oper)
232 {
233 unsigned int i;
234 for (i = 0; i < oper->num_children; i++)
235 slang_operation_destruct (oper->children + i);
236 slang_alloc_free (oper->children);
237 slang_alloc_free (oper->identifier);
238 slang_variable_scope_destruct (oper->locals);
239 slang_alloc_free (oper->locals);
240 }
241
242 static int slang_operation_copy (slang_operation *x, const slang_operation *y)
243 {
244 unsigned int i;
245 for (i = 0; i < x->num_children; i++)
246 slang_operation_destruct (x->children + i);
247 slang_alloc_free (x->children);
248 x->num_children = 0;
249 slang_alloc_free (x->identifier);
250 x->identifier = NULL;
251 slang_variable_scope_destruct (x->locals);
252 slang_variable_scope_construct (x->locals);
253
254 x->type = y->type;
255 x->children = (slang_operation *) slang_alloc_malloc (y->num_children * sizeof (
256 slang_operation));
257 if (x->children == NULL)
258 return 0;
259 for (i = 0; i < y->num_children; i++)
260 if (!slang_operation_construct_a (x->children + i))
261 {
262 unsigned int j;
263 for (j = 0; j < i; j++)
264 slang_operation_destruct (x->children + j);
265 slang_alloc_free (x->children);
266 x->children = NULL;
267 return 0;
268 }
269 x->num_children = y->num_children;
270 for (i = 0; i < x->num_children; i++)
271 if (!slang_operation_copy (x->children + i, y->children + i))
272 return 0;
273 x->literal = y->literal;
274 if (y->identifier != NULL)
275 {
276 x->identifier = slang_string_duplicate (y->identifier);
277 if (x->identifier == NULL)
278 return 0;
279 }
280 if (!slang_variable_scope_copy (x->locals, y->locals))
281 return 0;
282 return 1;
283 }
284
285 /* slang_variable */
286
287 static void slang_variable_construct (slang_variable *var)
288 {
289 slang_fully_specified_type_construct (&var->type);
290 var->name = NULL;
291 var->array_size = NULL;
292 var->initializer = NULL;
293 var->address = ~0;
294 }
295
296 static void slang_variable_destruct (slang_variable *var)
297 {
298 slang_fully_specified_type_destruct (&var->type);
299 slang_alloc_free (var->name);
300 if (var->array_size != NULL)
301 {
302 slang_operation_destruct (var->array_size);
303 slang_alloc_free (var->array_size);
304 }
305 if (var->initializer != NULL)
306 {
307 slang_operation_destruct (var->initializer);
308 slang_alloc_free (var->initializer);
309 }
310 }
311
312 static int slang_variable_copy (slang_variable *x, const slang_variable *y)
313 {
314 slang_variable_destruct (x);
315 slang_variable_construct (x);
316 if (!slang_fully_specified_type_copy (&x->type, &y->type))
317 return 0;
318 if (y->name != NULL)
319 {
320 x->name = slang_string_duplicate (y->name);
321 if (x->name == NULL)
322 return 0;
323 }
324 if (y->array_size != NULL)
325 {
326 x->array_size = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation));
327 if (x->array_size == NULL)
328 return 0;
329 if (!slang_operation_construct_a (x->array_size))
330 {
331 slang_alloc_free (x->array_size);
332 x->array_size = NULL;
333 return 0;
334 }
335 if (!slang_operation_copy (x->array_size, y->array_size))
336 return 0;
337 }
338 if (y->initializer != NULL)
339 {
340 x->initializer = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation));
341 if (x->initializer == NULL)
342 return 0;
343 if (!slang_operation_construct_a (x->initializer))
344 {
345 slang_alloc_free (x->initializer);
346 x->initializer = NULL;
347 return 0;
348 }
349 if (!slang_operation_copy (x->initializer, y->initializer))
350 return 0;
351 }
352 return 1;
353 }
354
355 slang_variable *_slang_locate_variable (slang_variable_scope *scope, const char *name, int all)
356 {
357 unsigned int i;
358 for (i = 0; i < scope->num_variables; i++)
359 if (slang_string_compare (name, scope->variables[i].name) == 0)
360 return scope->variables + i;
361 if (all && scope->outer_scope != NULL)
362 return _slang_locate_variable (scope->outer_scope, name, 1);
363 return NULL;
364 }
365
366 /* slang_struct_scope */
367
368 static void slang_struct_scope_construct (slang_struct_scope *scope)
369 {
370 scope->structs = NULL;
371 scope->num_structs = 0;
372 scope->outer_scope = NULL;
373 }
374
375 static void slang_struct_scope_destruct (slang_struct_scope *scope)
376 {
377 unsigned int i;
378 for (i = 0; i < scope->num_structs; i++)
379 slang_struct_destruct (scope->structs + i);
380 slang_alloc_free (scope->structs);
381 }
382
383 static int slang_struct_scope_copy (slang_struct_scope *x, const slang_struct_scope *y)
384 {
385 unsigned int i;
386 slang_struct_scope_destruct (x);
387 slang_struct_scope_construct (x);
388 x->structs = (slang_struct *) slang_alloc_malloc (y->num_structs * sizeof (slang_struct));
389 if (x->structs == NULL)
390 return 0;
391 x->num_structs = y->num_structs;
392 for (i = 0; i < x->num_structs; i++)
393 {
394 unsigned int j;
395 if (!slang_struct_construct_a (x->structs + i))
396 {
397 for (j = 0; j < i; j++)
398 slang_struct_destruct (x->structs + j);
399 slang_alloc_free (x->structs);
400 x->structs = NULL;
401 return 0;
402 }
403 }
404 for (i = 0; i < x->num_structs; i++)
405 if (!slang_struct_copy (x->structs + i, y->structs + i))
406 return 0;
407 x->outer_scope = y->outer_scope;
408 return 1;
409 }
410
411 slang_struct *slang_struct_scope_find (slang_struct_scope *stru, const char *name, int all_scopes)
412 {
413 unsigned int i;
414 for (i = 0; i < stru->num_structs; i++)
415 if (slang_string_compare (name, stru->structs[i].name) == 0)
416 return stru->structs + i;
417 if (all_scopes && stru->outer_scope != NULL)
418 return slang_struct_scope_find (stru->outer_scope, name, 1);
419 return NULL;
420 }
421
422 /* slang_struct */
423
424 int slang_struct_construct_a (slang_struct *stru)
425 {
426 stru->name = NULL;
427 stru->fields = (slang_variable_scope *) slang_alloc_malloc (sizeof (slang_variable_scope));
428 if (stru->fields == NULL)
429 return 0;
430 slang_variable_scope_construct (stru->fields);
431 stru->structs = (slang_struct_scope *) slang_alloc_malloc (sizeof (slang_struct_scope));
432 if (stru->structs == NULL)
433 {
434 slang_variable_scope_destruct (stru->fields);
435 slang_alloc_free (stru->fields);
436 return 0;
437 }
438 slang_struct_scope_construct (stru->structs);
439 return 1;
440 }
441
442 static void slang_struct_destruct (slang_struct *stru)
443 {
444 slang_alloc_free (stru->name);
445 slang_variable_scope_destruct (stru->fields);
446 slang_alloc_free (stru->fields);
447 slang_struct_scope_destruct (stru->structs);
448 slang_alloc_free (stru->structs);
449 }
450
451 int slang_struct_copy (slang_struct *x, const slang_struct *y)
452 {
453 slang_alloc_free (x->name);
454 x->name = NULL;
455 slang_variable_scope_destruct (x->fields);
456 slang_variable_scope_construct (x->fields);
457 slang_struct_scope_destruct (x->structs);
458 slang_struct_scope_construct (x->structs);
459 if (y->name != NULL)
460 {
461 x->name = slang_string_duplicate (y->name);
462 if (x->name == NULL)
463 return 0;
464 }
465 if (!slang_variable_scope_copy (x->fields, y->fields))
466 return 0;
467 if (!slang_struct_scope_copy (x->structs, y->structs))
468 return 0;
469 return 1;
470 }
471
472 static int slang_struct_equal (const slang_struct *x, const slang_struct *y)
473 {
474 unsigned int i;
475 if (x->fields->num_variables != y->fields->num_variables)
476 return 0;
477 for (i = 0; i < x->fields->num_variables; i++)
478 {
479 slang_variable *varx = x->fields->variables + i;
480 slang_variable *vary = y->fields->variables + i;
481 if (slang_string_compare (varx->name, vary->name) != 0)
482 return 0;
483 if (!slang_type_specifier_equal (&varx->type.specifier, &vary->type.specifier))
484 return 0;
485 if (varx->type.specifier.type == slang_spec_array)
486 {
487 /* TODO compare array sizes */
488 }
489 }
490 return 1;
491 }
492
493 /* slang_function */
494 /* XXX mem! */
495 static void slang_function_construct (slang_function *func)
496 {
497 func->kind = slang_func_ordinary;
498 slang_variable_construct (&func->header);
499 func->parameters = (slang_variable_scope *) slang_alloc_malloc (sizeof (slang_variable_scope));
500 slang_variable_scope_construct (func->parameters);
501 func->body = NULL;
502 func->address = ~0;
503 }
504
505 static void slang_function_destruct (slang_function *func)
506 {
507 slang_variable_destruct (&func->header);
508 slang_variable_scope_destruct (func->parameters);
509 slang_alloc_free (func->parameters);
510 if (func->body != NULL)
511 {
512 slang_operation_destruct (func->body);
513 slang_alloc_free (func->body);
514 }
515 }
516
517 /* slang_function_scope */
518
519 static void slang_function_scope_construct (slang_function_scope *scope)
520 {
521 scope->functions = NULL;
522 scope->num_functions = 0;
523 scope->outer_scope = NULL;
524 }
525
526 static void slang_function_scope_destruct (slang_function_scope *scope)
527 {
528 unsigned int i;
529 for (i = 0; i < scope->num_functions; i++)
530 slang_function_destruct (scope->functions + i);
531 slang_alloc_free (scope->functions);
532 }
533
534 static int slang_function_scope_find_by_name (slang_function_scope *funcs, const char *name,
535 int all_scopes)
536 {
537 unsigned int i;
538 for (i = 0; i < funcs->num_functions; i++)
539 if (slang_string_compare (name, funcs->functions[i].header.name) == 0)
540 return 1;
541 if (all_scopes && funcs->outer_scope != NULL)
542 return slang_function_scope_find_by_name (funcs->outer_scope, name, 1);
543 return 0;
544 }
545
546 static slang_function *slang_function_scope_find (slang_function_scope *funcs, slang_function *fun,
547 int all_scopes)
548 {
549 unsigned int i;
550 for (i = 0; i < funcs->num_functions; i++)
551 {
552 slang_function *f = funcs->functions + i;
553 unsigned int j;
554 if (slang_string_compare (fun->header.name, f->header.name) != 0)
555 continue;
556 if (fun->param_count != f->param_count)
557 continue;
558 for (j = 0; j < fun->param_count; j++)
559 {
560 if (!slang_type_specifier_equal (&fun->parameters->variables[j].type.specifier,
561 &f->parameters->variables[j].type.specifier))
562 {
563 break;
564 }
565 }
566 if (j == fun->param_count)
567 return f;
568 }
569 if (all_scopes && funcs->outer_scope != NULL)
570 return slang_function_scope_find (funcs->outer_scope, fun, 1);
571 return NULL;
572 }
573
574 /* slang_translation_unit */
575
576 void slang_translation_unit_construct (slang_translation_unit *unit)
577 {
578 slang_variable_scope_construct (&unit->globals);
579 slang_function_scope_construct (&unit->functions);
580 slang_struct_scope_construct (&unit->structs);
581 }
582
583 void slang_translation_unit_destruct (slang_translation_unit *unit)
584 {
585 slang_variable_scope_destruct (&unit->globals);
586 slang_function_scope_destruct (&unit->functions);
587 slang_struct_scope_destruct (&unit->structs);
588 }
589
590 /* slang_info_log */
591
592 static char *out_of_memory = "error: out of memory\n";
593
594 void slang_info_log_construct (slang_info_log *log)
595 {
596 log->text = NULL;
597 log->dont_free_text = 0;
598 }
599
600 void slang_info_log_destruct (slang_info_log *log)
601 {
602 if (!log->dont_free_text)
603 slang_alloc_free (log->text);
604 }
605
606 static int slang_info_log_message (slang_info_log *log, const char *prefix, const char *msg)
607 {
608 unsigned int new_size;
609 if (log->dont_free_text)
610 return 0;
611 new_size = slang_string_length (prefix) + 3 + slang_string_length (msg);
612 if (log->text != NULL)
613 {
614 log->text = (char *) slang_alloc_realloc (log->text, slang_string_length (log->text) + 1,
615 new_size + slang_string_length (log->text) + 1);
616 }
617 else
618 {
619 log->text = (char *) slang_alloc_malloc (new_size + 1);
620 if (log->text != NULL)
621 *log->text = '\0';
622 }
623 if (log->text == NULL)
624 return 0;
625 slang_string_concat (log->text, prefix);
626 slang_string_concat (log->text, ": ");
627 slang_string_concat (log->text, msg);
628 slang_string_concat (log->text, "\n");
629 return 1;
630 }
631
632 int slang_info_log_error (slang_info_log *log, const char *msg, ...)
633 {
634 va_list va;
635 char buf[1024];
636
637 va_start (va, msg);
638 _mesa_vsprintf (buf, msg, va);
639 if (slang_info_log_message (log, "error", buf))
640 return 1;
641 slang_info_log_memory (log);
642 va_end (va);
643 return 0;
644 }
645
646 int slang_info_log_warning (slang_info_log *log, const char *msg, ...)
647 {
648 va_list va;
649 char buf[1024];
650
651 va_start (va, msg);
652 _mesa_vsprintf (buf, msg, va);
653 if (slang_info_log_message (log, "warning", buf))
654 return 1;
655 slang_info_log_memory (log);
656 va_end (va);
657 return 0;
658 }
659
660 void slang_info_log_memory (slang_info_log *log)
661 {
662 if (!slang_info_log_message (log, "error", "out of memory"))
663 {
664 log->dont_free_text = 1;
665 log->text = out_of_memory;
666 }
667 }
668
669 /* slang_parse_ctx */
670
671 typedef struct slang_parse_ctx_
672 {
673 const byte *I;
674 slang_info_log *L;
675 int parsing_builtin;
676 } slang_parse_ctx;
677
678 /* _slang_compile() */
679
680 static int parse_identifier (slang_parse_ctx *C, char **id)
681 {
682 *id = slang_string_duplicate ((const char *) C->I);
683 if (*id == NULL)
684 {
685 slang_info_log_memory (C->L);
686 return 0;
687 }
688 C->I += _mesa_strlen ((const char *) C->I) + 1;
689 return 1;
690 }
691
692 static int parse_number (slang_parse_ctx *C, int *number)
693 {
694 const int radix = (int) (*C->I++);
695 *number = 0;
696 while (*C->I != '\0')
697 {
698 int digit;
699 if (*C->I >= '0' && *C->I <= '9')
700 digit = (int) (*C->I - '0');
701 else if (*C->I >= 'A' && *C->I <= 'Z')
702 digit = (int) (*C->I - 'A') + 10;
703 else
704 digit = (int) (*C->I - 'a') + 10;
705 *number = *number * radix + digit;
706 C->I++;
707 }
708 C->I++;
709 if (*number > 65535)
710 slang_info_log_warning (C->L, "%d: literal integer overflow", *number);
711 return 1;
712 }
713
714 static int parse_float (slang_parse_ctx *C, float *number)
715 {
716 char *integral = NULL;
717 char *fractional = NULL;
718 char *exponent = NULL;
719 char *whole = NULL;
720
721 if (!parse_identifier (C, &integral))
722 return 0;
723
724 if (!parse_identifier (C, &fractional))
725 {
726 slang_alloc_free (integral);
727 return 0;
728 }
729
730 if (!parse_identifier (C, &exponent))
731 {
732 slang_alloc_free (fractional);
733 slang_alloc_free (integral);
734 return 0;
735 }
736
737 whole = (char *) (slang_alloc_malloc ((_mesa_strlen (integral) +
738 _mesa_strlen (fractional) + _mesa_strlen (exponent) + 3) *
739 sizeof (char)));
740 if (whole == NULL)
741 {
742 slang_alloc_free (exponent);
743 slang_alloc_free (fractional);
744 slang_alloc_free (integral);
745 slang_info_log_memory (C->L);
746 return 0;
747 }
748
749 slang_string_copy (whole, integral);
750 slang_string_concat (whole, ".");
751 slang_string_concat (whole, fractional);
752 slang_string_concat (whole, "E");
753 slang_string_concat (whole, exponent);
754
755 *number = (float) (_mesa_strtod(whole, (char **)NULL));
756
757 slang_alloc_free (whole);
758 slang_alloc_free (exponent);
759 slang_alloc_free (fractional);
760 slang_alloc_free (integral);
761 return 1;
762 }
763
764 /* revision number - increment after each change affecting emitted output */
765 #define REVISION 2
766
767 static int check_revision (slang_parse_ctx *C)
768 {
769 if (*C->I != REVISION)
770 {
771 slang_info_log_error (C->L, "internal compiler error");
772 return 0;
773 }
774 C->I++;
775 return 1;
776 }
777
778 static int parse_statement (slang_parse_ctx *, slang_operation *, slang_variable_scope *,
779 slang_struct_scope *, slang_function_scope *);
780 static int parse_expression (slang_parse_ctx *, slang_operation *, slang_variable_scope *,
781 slang_struct_scope *, slang_function_scope *);
782
783 /* type qualifier */
784 #define TYPE_QUALIFIER_NONE 0
785 #define TYPE_QUALIFIER_CONST 1
786 #define TYPE_QUALIFIER_ATTRIBUTE 2
787 #define TYPE_QUALIFIER_VARYING 3
788 #define TYPE_QUALIFIER_UNIFORM 4
789 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
790 #define TYPE_QUALIFIER_FIXEDINPUT 6
791
792 static int parse_type_qualifier (slang_parse_ctx *C, slang_type_qualifier *qual)
793 {
794 switch (*C->I++)
795 {
796 case TYPE_QUALIFIER_NONE:
797 *qual = slang_qual_none;
798 break;
799 case TYPE_QUALIFIER_CONST:
800 *qual = slang_qual_const;
801 break;
802 case TYPE_QUALIFIER_ATTRIBUTE:
803 *qual = slang_qual_attribute;
804 break;
805 case TYPE_QUALIFIER_VARYING:
806 *qual = slang_qual_varying;
807 break;
808 case TYPE_QUALIFIER_UNIFORM:
809 *qual = slang_qual_uniform;
810 break;
811 case TYPE_QUALIFIER_FIXEDOUTPUT:
812 *qual = slang_qual_fixedoutput;
813 break;
814 case TYPE_QUALIFIER_FIXEDINPUT:
815 *qual = slang_qual_fixedinput;
816 break;
817 default:
818 return 0;
819 }
820 return 1;
821 }
822
823 /* type specifier */
824 #define TYPE_SPECIFIER_VOID 0
825 #define TYPE_SPECIFIER_BOOL 1
826 #define TYPE_SPECIFIER_BVEC2 2
827 #define TYPE_SPECIFIER_BVEC3 3
828 #define TYPE_SPECIFIER_BVEC4 4
829 #define TYPE_SPECIFIER_INT 5
830 #define TYPE_SPECIFIER_IVEC2 6
831 #define TYPE_SPECIFIER_IVEC3 7
832 #define TYPE_SPECIFIER_IVEC4 8
833 #define TYPE_SPECIFIER_FLOAT 9
834 #define TYPE_SPECIFIER_VEC2 10
835 #define TYPE_SPECIFIER_VEC3 11
836 #define TYPE_SPECIFIER_VEC4 12
837 #define TYPE_SPECIFIER_MAT2 13
838 #define TYPE_SPECIFIER_MAT3 14
839 #define TYPE_SPECIFIER_MAT4 15
840 #define TYPE_SPECIFIER_SAMPLER1D 16
841 #define TYPE_SPECIFIER_SAMPLER2D 17
842 #define TYPE_SPECIFIER_SAMPLER3D 18
843 #define TYPE_SPECIFIER_SAMPLERCUBE 19
844 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
845 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
846 #define TYPE_SPECIFIER_STRUCT 22
847 #define TYPE_SPECIFIER_TYPENAME 23
848
849 /* structure field */
850 #define FIELD_NONE 0
851 #define FIELD_NEXT 1
852 #define FIELD_ARRAY 2
853
854 static int parse_type_specifier (slang_parse_ctx *C, slang_type_specifier *spec,
855 slang_struct_scope *structs, slang_variable_scope *scope, slang_function_scope *funcs)
856 {
857 switch (*C->I++)
858 {
859 case TYPE_SPECIFIER_VOID:
860 spec->type = slang_spec_void;
861 break;
862 case TYPE_SPECIFIER_BOOL:
863 spec->type = slang_spec_bool;
864 break;
865 case TYPE_SPECIFIER_BVEC2:
866 spec->type = slang_spec_bvec2;
867 break;
868 case TYPE_SPECIFIER_BVEC3:
869 spec->type = slang_spec_bvec3;
870 break;
871 case TYPE_SPECIFIER_BVEC4:
872 spec->type = slang_spec_bvec4;
873 break;
874 case TYPE_SPECIFIER_INT:
875 spec->type = slang_spec_int;
876 break;
877 case TYPE_SPECIFIER_IVEC2:
878 spec->type = slang_spec_ivec2;
879 break;
880 case TYPE_SPECIFIER_IVEC3:
881 spec->type = slang_spec_ivec3;
882 break;
883 case TYPE_SPECIFIER_IVEC4:
884 spec->type = slang_spec_ivec4;
885 break;
886 case TYPE_SPECIFIER_FLOAT:
887 spec->type = slang_spec_float;
888 break;
889 case TYPE_SPECIFIER_VEC2:
890 spec->type = slang_spec_vec2;
891 break;
892 case TYPE_SPECIFIER_VEC3:
893 spec->type = slang_spec_vec3;
894 break;
895 case TYPE_SPECIFIER_VEC4:
896 spec->type = slang_spec_vec4;
897 break;
898 case TYPE_SPECIFIER_MAT2:
899 spec->type = slang_spec_mat2;
900 break;
901 case TYPE_SPECIFIER_MAT3:
902 spec->type = slang_spec_mat3;
903 break;
904 case TYPE_SPECIFIER_MAT4:
905 spec->type = slang_spec_mat4;
906 break;
907 case TYPE_SPECIFIER_SAMPLER1D:
908 spec->type = slang_spec_sampler1D;
909 break;
910 case TYPE_SPECIFIER_SAMPLER2D:
911 spec->type = slang_spec_sampler2D;
912 break;
913 case TYPE_SPECIFIER_SAMPLER3D:
914 spec->type = slang_spec_sampler3D;
915 break;
916 case TYPE_SPECIFIER_SAMPLERCUBE:
917 spec->type = slang_spec_samplerCube;
918 break;
919 case TYPE_SPECIFIER_SAMPLER1DSHADOW:
920 spec->type = slang_spec_sampler1DShadow;
921 break;
922 case TYPE_SPECIFIER_SAMPLER2DSHADOW:
923 spec->type = slang_spec_sampler2DShadow;
924 break;
925 case TYPE_SPECIFIER_STRUCT:
926 spec->type = slang_spec_struct;
927 {
928 char *name;
929 if (!parse_identifier (C, &name))
930 return 0;
931 if (*name != '\0' && slang_struct_scope_find (structs, name, 0) != NULL)
932 {
933 slang_info_log_error (C->L, "%s: duplicate type name", name);
934 slang_alloc_free (name);
935 return 0;
936 }
937 spec->_struct = (slang_struct *) slang_alloc_malloc (sizeof (slang_struct));
938 if (spec->_struct == NULL)
939 {
940 slang_alloc_free (name);
941 slang_info_log_memory (C->L);
942 return 0;
943 }
944 if (!slang_struct_construct_a (spec->_struct))
945 {
946 slang_alloc_free (spec->_struct);
947 spec->_struct = NULL;
948 slang_alloc_free (name);
949 slang_info_log_memory (C->L);
950 return 0;
951 }
952 spec->_struct->name = name;
953 spec->_struct->structs->outer_scope = structs;
954 }
955 do
956 {
957 slang_type_specifier sp;
958 slang_type_specifier_construct (&sp);
959 if (!parse_type_specifier (C, &sp, spec->_struct->structs, scope, funcs))
960 {
961 slang_type_specifier_destruct (&sp);
962 return 0;
963 }
964 do
965 {
966 slang_variable *var;
967 spec->_struct->fields->variables = (slang_variable *) slang_alloc_realloc (
968 spec->_struct->fields->variables,
969 spec->_struct->fields->num_variables * sizeof (slang_variable),
970 (spec->_struct->fields->num_variables + 1) * sizeof (slang_variable));
971 if (spec->_struct->fields->variables == NULL)
972 {
973 slang_type_specifier_destruct (&sp);
974 slang_info_log_memory (C->L);
975 return 0;
976 }
977 var = spec->_struct->fields->variables + spec->_struct->fields->num_variables;
978 spec->_struct->fields->num_variables++;
979 slang_variable_construct (var);
980 if (!slang_type_specifier_copy (&var->type.specifier, &sp))
981 {
982 slang_type_specifier_destruct (&sp);
983 return 0;
984 }
985 if (!parse_identifier (C, &var->name))
986 {
987 slang_type_specifier_destruct (&sp);
988 return 0;
989 }
990 switch (*C->I++)
991 {
992 case FIELD_NONE:
993 break;
994 case FIELD_ARRAY:
995 var->array_size = (slang_operation *) slang_alloc_malloc (sizeof (
996 slang_operation));
997 if (var->array_size == NULL)
998 {
999 slang_type_specifier_destruct (&sp);
1000 slang_info_log_memory (C->L);
1001 return 0;
1002 }
1003 if (!slang_operation_construct_a (var->array_size))
1004 {
1005 slang_alloc_free (var->array_size);
1006 var->array_size = NULL;
1007 slang_type_specifier_destruct (&sp);
1008 slang_info_log_memory (C->L);
1009 return 0;
1010 }
1011 if (!parse_expression (C, var->array_size, scope, structs, funcs))
1012 {
1013 slang_type_specifier_destruct (&sp);
1014 return 0;
1015 }
1016 break;
1017 default:
1018 return 0;
1019 }
1020 }
1021 while (*C->I++ != FIELD_NONE);
1022 }
1023 while (*C->I++ != FIELD_NONE);
1024 if (*spec->_struct->name != '\0')
1025 {
1026 slang_struct *s;
1027 structs->structs = (slang_struct *) slang_alloc_realloc (structs->structs,
1028 structs->num_structs * sizeof (slang_struct),
1029 (structs->num_structs + 1) * sizeof (slang_struct));
1030 if (structs->structs == NULL)
1031 {
1032 slang_info_log_memory (C->L);
1033 return 0;
1034 }
1035 s = structs->structs + structs->num_structs;
1036 if (!slang_struct_construct_a (s))
1037 return 0;
1038 structs->num_structs++;
1039 if (!slang_struct_copy (s, spec->_struct))
1040 return 0;
1041 }
1042 break;
1043 case TYPE_SPECIFIER_TYPENAME:
1044 spec->type = slang_spec_struct;
1045 {
1046 char *name;
1047 slang_struct *stru;
1048 if (!parse_identifier (C, &name))
1049 return 0;
1050 stru = slang_struct_scope_find (structs, name, 1);
1051 if (stru == NULL)
1052 {
1053 slang_info_log_error (C->L, "%s: undeclared type name", name);
1054 slang_alloc_free (name);
1055 return 0;
1056 }
1057 slang_alloc_free (name);
1058 spec->_struct = (slang_struct *) slang_alloc_malloc (sizeof (slang_struct));
1059 if (spec->_struct == NULL)
1060 {
1061 slang_info_log_memory (C->L);
1062 return 0;
1063 }
1064 if (!slang_struct_construct_a (spec->_struct))
1065 {
1066 slang_alloc_free (spec->_struct);
1067 spec->_struct = NULL;
1068 return 0;
1069 }
1070 if (!slang_struct_copy (spec->_struct, stru))
1071 return 0;
1072 }
1073 break;
1074 default:
1075 return 0;
1076 }
1077 return 1;
1078 }
1079
1080 static int parse_fully_specified_type (slang_parse_ctx *C, slang_fully_specified_type *type,
1081 slang_struct_scope *structs, slang_variable_scope *scope, slang_function_scope *funcs)
1082 {
1083 if (!parse_type_qualifier (C, &type->qualifier))
1084 return 0;
1085 return parse_type_specifier (C, &type->specifier, structs, scope, funcs);
1086 }
1087
1088 /* operation */
1089 #define OP_END 0
1090 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
1091 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
1092 #define OP_DECLARE 3
1093 #define OP_ASM 4
1094 #define OP_BREAK 5
1095 #define OP_CONTINUE 6
1096 #define OP_DISCARD 7
1097 #define OP_RETURN 8
1098 #define OP_EXPRESSION 9
1099 #define OP_IF 10
1100 #define OP_WHILE 11
1101 #define OP_DO 12
1102 #define OP_FOR 13
1103 #define OP_PUSH_VOID 14
1104 #define OP_PUSH_BOOL 15
1105 #define OP_PUSH_INT 16
1106 #define OP_PUSH_FLOAT 17
1107 #define OP_PUSH_IDENTIFIER 18
1108 #define OP_SEQUENCE 19
1109 #define OP_ASSIGN 20
1110 #define OP_ADDASSIGN 21
1111 #define OP_SUBASSIGN 22
1112 #define OP_MULASSIGN 23
1113 #define OP_DIVASSIGN 24
1114 /*#define OP_MODASSIGN 25*/
1115 /*#define OP_LSHASSIGN 26*/
1116 /*#define OP_RSHASSIGN 27*/
1117 /*#define OP_ORASSIGN 28*/
1118 /*#define OP_XORASSIGN 29*/
1119 /*#define OP_ANDASSIGN 30*/
1120 #define OP_SELECT 31
1121 #define OP_LOGICALOR 32
1122 #define OP_LOGICALXOR 33
1123 #define OP_LOGICALAND 34
1124 /*#define OP_BITOR 35*/
1125 /*#define OP_BITXOR 36*/
1126 /*#define OP_BITAND 37*/
1127 #define OP_EQUAL 38
1128 #define OP_NOTEQUAL 39
1129 #define OP_LESS 40
1130 #define OP_GREATER 41
1131 #define OP_LESSEQUAL 42
1132 #define OP_GREATEREQUAL 43
1133 /*#define OP_LSHIFT 44*/
1134 /*#define OP_RSHIFT 45*/
1135 #define OP_ADD 46
1136 #define OP_SUBTRACT 47
1137 #define OP_MULTIPLY 48
1138 #define OP_DIVIDE 49
1139 /*#define OP_MODULUS 50*/
1140 #define OP_PREINCREMENT 51
1141 #define OP_PREDECREMENT 52
1142 #define OP_PLUS 53
1143 #define OP_MINUS 54
1144 /*#define OP_COMPLEMENT 55*/
1145 #define OP_NOT 56
1146 #define OP_SUBSCRIPT 57
1147 #define OP_CALL 58
1148 #define OP_FIELD 59
1149 #define OP_POSTINCREMENT 60
1150 #define OP_POSTDECREMENT 61
1151
1152 static int parse_child_operation (slang_parse_ctx *C, slang_operation *oper, int statement,
1153 slang_variable_scope *scope, slang_struct_scope *structs, slang_function_scope *funcs)
1154 {
1155 oper->children = (slang_operation *) slang_alloc_realloc (oper->children,
1156 oper->num_children * sizeof (slang_operation),
1157 (oper->num_children + 1) * sizeof (slang_operation));
1158 if (oper->children == NULL)
1159 {
1160 slang_info_log_memory (C->L);
1161 return 0;
1162 }
1163 if (!slang_operation_construct_a (oper->children + oper->num_children))
1164 {
1165 slang_info_log_memory (C->L);
1166 return 0;
1167 }
1168 oper->num_children++;
1169 if (statement)
1170 return parse_statement (C, oper->children + oper->num_children - 1, scope, structs, funcs);
1171 return parse_expression (C, oper->children + oper->num_children - 1, scope, structs, funcs);
1172 }
1173
1174 static int parse_declaration (slang_parse_ctx *C, slang_variable_scope *, slang_struct_scope *,
1175 slang_function_scope *);
1176
1177 static int parse_statement (slang_parse_ctx *C, slang_operation *oper, slang_variable_scope *scope,
1178 slang_struct_scope *structs, slang_function_scope *funcs)
1179 {
1180 oper->locals->outer_scope = scope;
1181 switch (*C->I++)
1182 {
1183 case OP_BLOCK_BEGIN_NO_NEW_SCOPE:
1184 oper->type = slang_oper_block_no_new_scope;
1185 while (*C->I != OP_END)
1186 if (!parse_child_operation (C, oper, 1, scope, structs, funcs))
1187 return 0;
1188 C->I++;
1189 break;
1190 case OP_BLOCK_BEGIN_NEW_SCOPE:
1191 oper->type = slang_oper_block_new_scope;
1192 while (*C->I != OP_END)
1193 if (!parse_child_operation (C, oper, 1, oper->locals, structs, funcs))
1194 return 0;
1195 C->I++;
1196 break;
1197 case OP_DECLARE:
1198 oper->type = slang_oper_variable_decl;
1199 {
1200 const unsigned int first_var = scope->num_variables;
1201 if (!parse_declaration (C, scope, structs, funcs))
1202 return 0;
1203 if (first_var < scope->num_variables)
1204 {
1205 const unsigned int num_vars = scope->num_variables - first_var;
1206 unsigned int i;
1207 oper->children = (slang_operation *) slang_alloc_malloc (num_vars * sizeof (
1208 slang_operation));
1209 if (oper->children == NULL)
1210 {
1211 slang_info_log_memory (C->L);
1212 return 0;
1213 }
1214 for (i = 0; i < num_vars; i++)
1215 if (!slang_operation_construct_a (oper->children + i))
1216 {
1217 unsigned int j;
1218 for (j = 0; j < i; j++)
1219 slang_operation_destruct (oper->children + j);
1220 slang_alloc_free (oper->children);
1221 oper->children = NULL;
1222 slang_info_log_memory (C->L);
1223 return 0;
1224 }
1225 oper->num_children = num_vars;
1226 for (i = first_var; i < scope->num_variables; i++)
1227 {
1228 slang_operation *o = oper->children + i - first_var;
1229 o->type = slang_oper_identifier;
1230 o->locals->outer_scope = scope;
1231 o->identifier = slang_string_duplicate (scope->variables[i].name);
1232 if (o->identifier == NULL)
1233 {
1234 slang_info_log_memory (C->L);
1235 return 0;
1236 }
1237 }
1238 }
1239 }
1240 break;
1241 case OP_ASM:
1242 oper->type = slang_oper_asm;
1243 if (!parse_identifier (C, &oper->identifier))
1244 return 0;
1245 while (*C->I != OP_END)
1246 if (!parse_child_operation (C, oper, 0, scope, structs, funcs))
1247 return 0;
1248 C->I++;
1249 break;
1250 case OP_BREAK:
1251 oper->type = slang_oper_break;
1252 break;
1253 case OP_CONTINUE:
1254 oper->type = slang_oper_continue;
1255 break;
1256 case OP_DISCARD:
1257 oper->type = slang_oper_discard;
1258 break;
1259 case OP_RETURN:
1260 oper->type = slang_oper_return;
1261 if (!parse_child_operation (C, oper, 0, scope, structs, funcs))
1262 return 0;
1263 break;
1264 case OP_EXPRESSION:
1265 oper->type = slang_oper_expression;
1266 if (!parse_child_operation (C, oper, 0, scope, structs, funcs))
1267 return 0;
1268 break;
1269 case OP_IF:
1270 oper->type = slang_oper_if;
1271 if (!parse_child_operation (C, oper, 0, scope, structs, funcs))
1272 return 0;
1273 if (!parse_child_operation (C, oper, 1, scope, structs, funcs))
1274 return 0;
1275 if (!parse_child_operation (C, oper, 1, scope, structs, funcs))
1276 return 0;
1277 break;
1278 case OP_WHILE:
1279 oper->type = slang_oper_while;
1280 if (!parse_child_operation (C, oper, 1, oper->locals, structs, funcs))
1281 return 0;
1282 if (!parse_child_operation (C, oper, 1, oper->locals, structs, funcs))
1283 return 0;
1284 break;
1285 case OP_DO:
1286 oper->type = slang_oper_do;
1287 if (!parse_child_operation (C, oper, 1, scope, structs, funcs))
1288 return 0;
1289 if (!parse_child_operation (C, oper, 0, scope, structs, funcs))
1290 return 0;
1291 break;
1292 case OP_FOR:
1293 oper->type = slang_oper_for;
1294 if (!parse_child_operation (C, oper, 1, oper->locals, structs, funcs))
1295 return 0;
1296 if (!parse_child_operation (C, oper, 1, oper->locals, structs, funcs))
1297 return 0;
1298 if (!parse_child_operation (C, oper, 0, oper->locals, structs, funcs))
1299 return 0;
1300 if (!parse_child_operation (C, oper, 1, oper->locals, structs, funcs))
1301 return 0;
1302 break;
1303 default:
1304 return 0;
1305 }
1306 return 1;
1307 }
1308
1309 static int handle_trinary_expression (slang_parse_ctx *C, slang_operation *op,
1310 slang_operation **ops, unsigned int *num_ops)
1311 {
1312 op->num_children = 3;
1313 op->children = (slang_operation *) slang_alloc_malloc (3 * sizeof (slang_operation));
1314 if (op->children == NULL)
1315 {
1316 slang_info_log_memory (C->L);
1317 return 0;
1318 }
1319 op->children[0] = (*ops)[*num_ops - 4];
1320 op->children[1] = (*ops)[*num_ops - 3];
1321 op->children[2] = (*ops)[*num_ops - 2];
1322 (*ops)[*num_ops - 4] = (*ops)[*num_ops - 1];
1323 *num_ops -= 3;
1324 *ops = (slang_operation *) slang_alloc_realloc (*ops, (*num_ops + 3) * sizeof (slang_operation),
1325 *num_ops * sizeof (slang_operation));
1326 if (*ops == NULL)
1327 {
1328 slang_info_log_memory (C->L);
1329 return 0;
1330 }
1331 return 1;
1332 }
1333
1334 static int handle_binary_expression (slang_parse_ctx *C, slang_operation *op,
1335 slang_operation **ops, unsigned int *num_ops)
1336 {
1337 op->num_children = 2;
1338 op->children = (slang_operation *) slang_alloc_malloc (2 * sizeof (slang_operation));
1339 if (op->children == NULL)
1340 {
1341 slang_info_log_memory (C->L);
1342 return 0;
1343 }
1344 op->children[0] = (*ops)[*num_ops - 3];
1345 op->children[1] = (*ops)[*num_ops - 2];
1346 (*ops)[*num_ops - 3] = (*ops)[*num_ops - 1];
1347 *num_ops -= 2;
1348 *ops = (slang_operation *) slang_alloc_realloc (*ops, (*num_ops + 2) * sizeof (slang_operation),
1349 *num_ops * sizeof (slang_operation));
1350 if (*ops == NULL)
1351 {
1352 slang_info_log_memory (C->L);
1353 return 0;
1354 }
1355 return 1;
1356 }
1357
1358 static int handle_unary_expression (slang_parse_ctx *C, slang_operation *op,
1359 slang_operation **ops, unsigned int *num_ops)
1360 {
1361 op->num_children = 1;
1362 op->children = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation));
1363 if (op->children == NULL)
1364 {
1365 slang_info_log_memory (C->L);
1366 return 0;
1367 }
1368 op->children[0] = (*ops)[*num_ops - 2];
1369 (*ops)[*num_ops - 2] = (*ops)[*num_ops - 1];
1370 (*num_ops)--;
1371 *ops = (slang_operation *) slang_alloc_realloc (*ops, (*num_ops + 1) * sizeof (slang_operation),
1372 *num_ops * sizeof (slang_operation));
1373 if (*ops == NULL)
1374 {
1375 slang_info_log_memory (C->L);
1376 return 0;
1377 }
1378 return 1;
1379 }
1380
1381 static int is_constructor_name (const char *name, slang_struct_scope *structs)
1382 {
1383 if (slang_type_specifier_type_from_string (name) != slang_spec_void)
1384 return 1;
1385 return slang_struct_scope_find (structs, name, 1) != NULL;
1386 }
1387
1388 static int parse_expression (slang_parse_ctx *C, slang_operation *oper, slang_variable_scope *scope,
1389 slang_struct_scope *structs, slang_function_scope *funcs)
1390 {
1391 slang_operation *ops = NULL;
1392 unsigned int num_ops = 0;
1393 int number;
1394
1395 while (*C->I != OP_END)
1396 {
1397 slang_operation *op;
1398 const unsigned int op_code = *C->I++;
1399 ops = (slang_operation *) slang_alloc_realloc (ops,
1400 num_ops * sizeof (slang_operation), (num_ops + 1) * sizeof (slang_operation));
1401 if (ops == NULL)
1402 {
1403 slang_info_log_memory (C->L);
1404 return 0;
1405 }
1406 op = ops + num_ops;
1407 if (!slang_operation_construct_a (op))
1408 {
1409 slang_info_log_memory (C->L);
1410 return 0;
1411 }
1412 num_ops++;
1413 op->locals->outer_scope = scope;
1414 switch (op_code)
1415 {
1416 case OP_PUSH_VOID:
1417 op->type = slang_oper_void;
1418 break;
1419 case OP_PUSH_BOOL:
1420 op->type = slang_oper_literal_bool;
1421 if (!parse_number (C, &number))
1422 return 0;
1423 op->literal = (float) number;
1424 break;
1425 case OP_PUSH_INT:
1426 op->type = slang_oper_literal_int;
1427 if (!parse_number (C, &number))
1428 return 0;
1429 op->literal = (float) number;
1430 break;
1431 case OP_PUSH_FLOAT:
1432 op->type = slang_oper_literal_float;
1433 if (!parse_float (C, &op->literal))
1434 return 0;
1435 break;
1436 case OP_PUSH_IDENTIFIER:
1437 op->type = slang_oper_identifier;
1438 if (!parse_identifier (C, &op->identifier))
1439 return 0;
1440 break;
1441 case OP_SEQUENCE:
1442 op->type = slang_oper_sequence;
1443 if (!handle_binary_expression (C, op, &ops, &num_ops))
1444 return 0;
1445 break;
1446 case OP_ASSIGN:
1447 op->type = slang_oper_assign;
1448 if (!handle_binary_expression (C, op, &ops, &num_ops))
1449 return 0;
1450 break;
1451 case OP_ADDASSIGN:
1452 op->type = slang_oper_addassign;
1453 if (!handle_binary_expression (C, op, &ops, &num_ops))
1454 return 0;
1455 break;
1456 case OP_SUBASSIGN:
1457 op->type = slang_oper_subassign;
1458 if (!handle_binary_expression (C, op, &ops, &num_ops))
1459 return 0;
1460 break;
1461 case OP_MULASSIGN:
1462 op->type = slang_oper_mulassign;
1463 if (!handle_binary_expression (C, op, &ops, &num_ops))
1464 return 0;
1465 break;
1466 case OP_DIVASSIGN:
1467 op->type = slang_oper_divassign;
1468 if (!handle_binary_expression (C, op, &ops, &num_ops))
1469 return 0;
1470 break;
1471 /*case OP_MODASSIGN:*/
1472 /*case OP_LSHASSIGN:*/
1473 /*case OP_RSHASSIGN:*/
1474 /*case OP_ORASSIGN:*/
1475 /*case OP_XORASSIGN:*/
1476 /*case OP_ANDASSIGN:*/
1477 case OP_SELECT:
1478 op->type = slang_oper_select;
1479 if (!handle_trinary_expression (C, op, &ops, &num_ops))
1480 return 0;
1481 break;
1482 case OP_LOGICALOR:
1483 op->type = slang_oper_logicalor;
1484 if (!handle_binary_expression (C, op, &ops, &num_ops))
1485 return 0;
1486 break;
1487 case OP_LOGICALXOR:
1488 op->type = slang_oper_logicalxor;
1489 if (!handle_binary_expression (C, op, &ops, &num_ops))
1490 return 0;
1491 break;
1492 case OP_LOGICALAND:
1493 op->type = slang_oper_logicaland;
1494 if (!handle_binary_expression (C, op, &ops, &num_ops))
1495 return 0;
1496 break;
1497 /*case OP_BITOR:*/
1498 /*case OP_BITXOR:*/
1499 /*case OP_BITAND:*/
1500 case OP_EQUAL:
1501 op->type = slang_oper_equal;
1502 if (!handle_binary_expression (C, op, &ops, &num_ops))
1503 return 0;
1504 break;
1505 case OP_NOTEQUAL:
1506 op->type = slang_oper_notequal;
1507 if (!handle_binary_expression (C, op, &ops, &num_ops))
1508 return 0;
1509 break;
1510 case OP_LESS:
1511 op->type = slang_oper_less;
1512 if (!handle_binary_expression (C, op, &ops, &num_ops))
1513 return 0;
1514 break;
1515 case OP_GREATER:
1516 op->type = slang_oper_greater;
1517 if (!handle_binary_expression (C, op, &ops, &num_ops))
1518 return 0;
1519 break;
1520 case OP_LESSEQUAL:
1521 op->type = slang_oper_lessequal;
1522 if (!handle_binary_expression (C, op, &ops, &num_ops))
1523 return 0;
1524 break;
1525 case OP_GREATEREQUAL:
1526 op->type = slang_oper_greaterequal;
1527 if (!handle_binary_expression (C, op, &ops, &num_ops))
1528 return 0;
1529 break;
1530 /*case OP_LSHIFT:*/
1531 /*case OP_RSHIFT:*/
1532 case OP_ADD:
1533 op->type = slang_oper_add;
1534 if (!handle_binary_expression (C, op, &ops, &num_ops))
1535 return 0;
1536 break;
1537 case OP_SUBTRACT:
1538 op->type = slang_oper_subtract;
1539 if (!handle_binary_expression (C, op, &ops, &num_ops))
1540 return 0;
1541 break;
1542 case OP_MULTIPLY:
1543 op->type = slang_oper_multiply;
1544 if (!handle_binary_expression (C, op, &ops, &num_ops))
1545 return 0;
1546 break;
1547 case OP_DIVIDE:
1548 op->type = slang_oper_divide;
1549 if (!handle_binary_expression (C, op, &ops, &num_ops))
1550 return 0;
1551 break;
1552 /*case OP_MODULUS:*/
1553 case OP_PREINCREMENT:
1554 op->type = slang_oper_preincrement;
1555 if (!handle_unary_expression (C, op, &ops, &num_ops))
1556 return 0;
1557 break;
1558 case OP_PREDECREMENT:
1559 op->type = slang_oper_predecrement;
1560 if (!handle_unary_expression (C, op, &ops, &num_ops))
1561 return 0;
1562 break;
1563 case OP_PLUS:
1564 op->type = slang_oper_plus;
1565 if (!handle_unary_expression (C, op, &ops, &num_ops))
1566 return 0;
1567 break;
1568 case OP_MINUS:
1569 op->type = slang_oper_minus;
1570 if (!handle_unary_expression (C, op, &ops, &num_ops))
1571 return 0;
1572 break;
1573 case OP_NOT:
1574 op->type = slang_oper_not;
1575 if (!handle_unary_expression (C, op, &ops, &num_ops))
1576 return 0;
1577 break;
1578 /*case OP_COMPLEMENT:*/
1579 case OP_SUBSCRIPT:
1580 op->type = slang_oper_subscript;
1581 if (!handle_binary_expression (C, op, &ops, &num_ops))
1582 return 0;
1583 break;
1584 case OP_CALL:
1585 op->type = slang_oper_call;
1586 if (!parse_identifier (C, &op->identifier))
1587 return 0;
1588 while (*C->I != OP_END)
1589 if (!parse_child_operation (C, op, 0, scope, structs, funcs))
1590 return 0;
1591 C->I++;
1592 if (!C->parsing_builtin &&
1593 !slang_function_scope_find_by_name (funcs, op->identifier, 1) &&
1594 !is_constructor_name (op->identifier, structs))
1595 {
1596 slang_info_log_error (C->L, "%s: undeclared function name", op->identifier);
1597 return 0;
1598 }
1599 break;
1600 case OP_FIELD:
1601 op->type = slang_oper_field;
1602 if (!parse_identifier (C, &op->identifier))
1603 return 0;
1604 if (!handle_unary_expression (C, op, &ops, &num_ops))
1605 return 0;
1606 break;
1607 case OP_POSTINCREMENT:
1608 op->type = slang_oper_postincrement;
1609 if (!handle_unary_expression (C, op, &ops, &num_ops))
1610 return 0;
1611 break;
1612 case OP_POSTDECREMENT:
1613 op->type = slang_oper_postdecrement;
1614 if (!handle_unary_expression (C, op, &ops, &num_ops))
1615 return 0;
1616 break;
1617 default:
1618 return 0;
1619 }
1620 }
1621 C->I++;
1622 *oper = *ops;
1623 slang_alloc_free (ops);
1624 return 1;
1625 }
1626
1627 /* parameter qualifier */
1628 #define PARAM_QUALIFIER_IN 0
1629 #define PARAM_QUALIFIER_OUT 1
1630 #define PARAM_QUALIFIER_INOUT 2
1631
1632 /* function parameter array presence */
1633 #define PARAMETER_ARRAY_NOT_PRESENT 0
1634 #define PARAMETER_ARRAY_PRESENT 1
1635
1636 static int parse_parameter_declaration (slang_parse_ctx *C, slang_variable *param,
1637 slang_struct_scope *structs, slang_variable_scope *scope, slang_function_scope *funcs)
1638 {
1639 slang_storage_aggregate agg;
1640 if (!parse_type_qualifier (C, &param->type.qualifier))
1641 return 0;
1642 switch (*C->I++)
1643 {
1644 case PARAM_QUALIFIER_IN:
1645 if (param->type.qualifier != slang_qual_const && param->type.qualifier != slang_qual_none)
1646 {
1647 slang_info_log_error (C->L, "invalid type qualifier");
1648 return 0;
1649 }
1650 break;
1651 case PARAM_QUALIFIER_OUT:
1652 if (param->type.qualifier == slang_qual_none)
1653 param->type.qualifier = slang_qual_out;
1654 else
1655 {
1656 slang_info_log_error (C->L, "invalid type qualifier");
1657 return 0;
1658 }
1659 break;
1660 case PARAM_QUALIFIER_INOUT:
1661 if (param->type.qualifier == slang_qual_none)
1662 param->type.qualifier = slang_qual_inout;
1663 else
1664 {
1665 slang_info_log_error (C->L, "invalid type qualifier");
1666 return 0;
1667 }
1668 break;
1669 default:
1670 return 0;
1671 }
1672 if (!parse_type_specifier (C, &param->type.specifier, structs, scope, funcs))
1673 return 0;
1674 if (!parse_identifier (C, &param->name))
1675 return 0;
1676 if (*C->I++ == PARAMETER_ARRAY_PRESENT)
1677 {
1678 param->array_size = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation));
1679 if (param->array_size == NULL)
1680 {
1681 slang_info_log_memory (C->L);
1682 return 0;
1683 }
1684 if (!slang_operation_construct_a (param->array_size))
1685 {
1686 slang_alloc_free (param->array_size);
1687 param->array_size = NULL;
1688 slang_info_log_memory (C->L);
1689 return 0;
1690 }
1691 if (!parse_expression (C, param->array_size, scope, structs, funcs))
1692 return 0;
1693 }
1694 slang_storage_aggregate_construct (&agg);
1695 if (!_slang_aggregate_variable (&agg, &param->type.specifier, param->array_size, funcs,
1696 structs))
1697 {
1698 slang_storage_aggregate_destruct (&agg);
1699 return 0;
1700 }
1701 slang_storage_aggregate_destruct (&agg);
1702 return 1;
1703 }
1704
1705 /* function type */
1706 #define FUNCTION_ORDINARY 0
1707 #define FUNCTION_CONSTRUCTOR 1
1708 #define FUNCTION_OPERATOR 2
1709
1710 /* function parameter */
1711 #define PARAMETER_NONE 0
1712 #define PARAMETER_NEXT 1
1713
1714 /* operator type */
1715 #define OPERATOR_ASSIGN 1
1716 #define OPERATOR_ADDASSIGN 2
1717 #define OPERATOR_SUBASSIGN 3
1718 #define OPERATOR_MULASSIGN 4
1719 #define OPERATOR_DIVASSIGN 5
1720 /*#define OPERATOR_MODASSIGN 6*/
1721 /*#define OPERATOR_LSHASSIGN 7*/
1722 /*#define OPERATOR_RSHASSIGN 8*/
1723 /*#define OPERATOR_ANDASSIGN 9*/
1724 /*#define OPERATOR_XORASSIGN 10*/
1725 /*#define OPERATOR_ORASSIGN 11*/
1726 #define OPERATOR_LOGICALXOR 12
1727 /*#define OPERATOR_BITOR 13*/
1728 /*#define OPERATOR_BITXOR 14*/
1729 /*#define OPERATOR_BITAND 15*/
1730 #define OPERATOR_EQUAL 16
1731 #define OPERATOR_NOTEQUAL 17
1732 #define OPERATOR_LESS 18
1733 #define OPERATOR_GREATER 19
1734 #define OPERATOR_LESSEQUAL 20
1735 #define OPERATOR_GREATEREQUAL 21
1736 /*#define OPERATOR_LSHIFT 22*/
1737 /*#define OPERATOR_RSHIFT 23*/
1738 #define OPERATOR_MULTIPLY 24
1739 #define OPERATOR_DIVIDE 25
1740 /*#define OPERATOR_MODULUS 26*/
1741 #define OPERATOR_INCREMENT 27
1742 #define OPERATOR_DECREMENT 28
1743 #define OPERATOR_PLUS 29
1744 #define OPERATOR_MINUS 30
1745 /*#define OPERATOR_COMPLEMENT 31*/
1746 #define OPERATOR_NOT 32
1747
1748 static const struct {
1749 unsigned int o_code;
1750 const char *o_name;
1751 } operator_names[] = {
1752 { OPERATOR_INCREMENT, "++" },
1753 { OPERATOR_ADDASSIGN, "+=" },
1754 { OPERATOR_PLUS, "+" },
1755 { OPERATOR_DECREMENT, "--" },
1756 { OPERATOR_SUBASSIGN, "-=" },
1757 { OPERATOR_MINUS, "-" },
1758 { OPERATOR_NOTEQUAL, "!=" },
1759 { OPERATOR_NOT, "!" },
1760 { OPERATOR_MULASSIGN, "*=" },
1761 { OPERATOR_MULTIPLY, "*" },
1762 { OPERATOR_DIVASSIGN, "/=" },
1763 { OPERATOR_DIVIDE, "/" },
1764 { OPERATOR_LESSEQUAL, "<=" },
1765 /*{ OPERATOR_LSHASSIGN, "<<=" },*/
1766 /*{ OPERATOR_LSHIFT, "<<" },*/
1767 { OPERATOR_LESS, "<" },
1768 { OPERATOR_GREATEREQUAL, ">=" },
1769 /*{ OPERATOR_RSHASSIGN, ">>=" },*/
1770 /*{ OPERATOR_RSHIFT, ">>" },*/
1771 { OPERATOR_GREATER, ">" },
1772 { OPERATOR_EQUAL, "==" },
1773 { OPERATOR_ASSIGN, "=" },
1774 /*{ OPERATOR_MODASSIGN, "%=" },*/
1775 /*{ OPERATOR_MODULUS, "%" },*/
1776 /*{ OPERATOR_ANDASSIGN, "&=" },*/
1777 /*{ OPERATOR_BITAND, "&" },*/
1778 /*{ OPERATOR_ORASSIGN, "|=" },*/
1779 /*{ OPERATOR_BITOR, "|" },*/
1780 /*{ OPERATOR_COMPLEMENT, "~" },*/
1781 /*{ OPERATOR_XORASSIGN, "^=" },*/
1782 { OPERATOR_LOGICALXOR, "^^" }/*,*/
1783 /*{ OPERATOR_BITXOR, "^" }*/
1784 };
1785
1786 static int parse_operator_name (slang_parse_ctx *C, char **pname)
1787 {
1788 unsigned int i;
1789 for (i = 0; i < sizeof (operator_names) / sizeof (*operator_names); i++)
1790 if (operator_names[i].o_code == (unsigned int) (*C->I))
1791 {
1792 *pname = slang_string_duplicate (operator_names[i].o_name);
1793 if (*pname == NULL)
1794 {
1795 slang_info_log_memory (C->L);
1796 return 0;
1797 }
1798 C->I++;
1799 return 1;
1800 }
1801 return 0;
1802 }
1803
1804 static int parse_function_prototype (slang_parse_ctx *C, slang_function *func,
1805 slang_struct_scope *structs, slang_variable_scope *scope, slang_function_scope *funcs)
1806 {
1807 if (!parse_fully_specified_type (C, &func->header.type, structs, scope, funcs))
1808 return 0;
1809 switch (*C->I++)
1810 {
1811 case FUNCTION_ORDINARY:
1812 func->kind = slang_func_ordinary;
1813 if (!parse_identifier (C, &func->header.name))
1814 return 0;
1815 break;
1816 case FUNCTION_CONSTRUCTOR:
1817 func->kind = slang_func_constructor;
1818 if (func->header.type.specifier.type == slang_spec_struct)
1819 return 0;
1820 func->header.name = slang_string_duplicate (
1821 type_specifier_type_names[func->header.type.specifier.type]);
1822 if (func->header.name == NULL)
1823 {
1824 slang_info_log_memory (C->L);
1825 return 0;
1826 }
1827 break;
1828 case FUNCTION_OPERATOR:
1829 func->kind = slang_func_operator;
1830 if (!parse_operator_name (C, &func->header.name))
1831 return 0;
1832 break;
1833 default:
1834 return 0;
1835 }
1836 func->parameters->outer_scope = scope;
1837 while (*C->I++ == PARAMETER_NEXT)
1838 {
1839 func->parameters->variables = (slang_variable *) slang_alloc_realloc (
1840 func->parameters->variables,
1841 func->parameters->num_variables * sizeof (slang_variable),
1842 (func->parameters->num_variables + 1) * sizeof (slang_variable));
1843 if (func->parameters->variables == NULL)
1844 {
1845 slang_info_log_memory (C->L);
1846 return 0;
1847 }
1848 slang_variable_construct (func->parameters->variables + func->parameters->num_variables);
1849 func->parameters->num_variables++;
1850 if (!parse_parameter_declaration (C, func->parameters->variables +
1851 func->parameters->num_variables - 1, structs, scope, funcs))
1852 return 0;
1853 }
1854 func->param_count = func->parameters->num_variables;
1855 return 1;
1856 }
1857
1858 static int parse_function_definition (slang_parse_ctx *C, slang_function *func,
1859 slang_struct_scope *structs, slang_variable_scope *scope, slang_function_scope *funcs)
1860 {
1861 if (!parse_function_prototype (C, func, structs, scope, funcs))
1862 return 0;
1863 func->body = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation));
1864 if (func->body == NULL)
1865 {
1866 slang_info_log_memory (C->L);
1867 return 0;
1868 }
1869 if (!slang_operation_construct_a (func->body))
1870 {
1871 slang_alloc_free (func->body);
1872 func->body = NULL;
1873 slang_info_log_memory (C->L);
1874 return 0;
1875 }
1876 if (!parse_statement (C, func->body, func->parameters, structs, funcs))
1877 return 0;
1878 return 1;
1879 }
1880
1881 /* init declarator list */
1882 #define DECLARATOR_NONE 0
1883 #define DECLARATOR_NEXT 1
1884
1885 /* variable declaration */
1886 #define VARIABLE_NONE 0
1887 #define VARIABLE_IDENTIFIER 1
1888 #define VARIABLE_INITIALIZER 2
1889 #define VARIABLE_ARRAY_EXPLICIT 3
1890 #define VARIABLE_ARRAY_UNKNOWN 4
1891
1892 static int parse_init_declarator (slang_parse_ctx *C, const slang_fully_specified_type *type,
1893 slang_variable_scope *vars, slang_struct_scope *structs, slang_function_scope *funcs)
1894 {
1895 slang_variable *var;
1896
1897 if (*C->I++ == VARIABLE_NONE)
1898 return 1;
1899 vars->variables = (slang_variable *) slang_alloc_realloc (vars->variables,
1900 vars->num_variables * sizeof (slang_variable),
1901 (vars->num_variables + 1) * sizeof (slang_variable));
1902 if (vars->variables == NULL)
1903 {
1904 slang_info_log_memory (C->L);
1905 return 0;
1906 }
1907 var = vars->variables + vars->num_variables;
1908 vars->num_variables++;
1909 slang_variable_construct (var);
1910 var->type.qualifier = type->qualifier;
1911 if (!parse_identifier (C, &var->name))
1912 return 0;
1913 switch (*C->I++)
1914 {
1915 case VARIABLE_NONE:
1916 if (!slang_type_specifier_copy (&var->type.specifier, &type->specifier))
1917 return 0;
1918 break;
1919 case VARIABLE_INITIALIZER:
1920 if (!slang_type_specifier_copy (&var->type.specifier, &type->specifier))
1921 return 0;
1922 var->initializer = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation));
1923 if (var->initializer == NULL)
1924 {
1925 slang_info_log_memory (C->L);
1926 return 0;
1927 }
1928 if (!slang_operation_construct_a (var->initializer))
1929 {
1930 slang_alloc_free (var->initializer);
1931 var->initializer = NULL;
1932 slang_info_log_memory (C->L);
1933 return 0;
1934 }
1935 if (!parse_expression (C, var->initializer, vars, structs, funcs))
1936 return 0;
1937 break;
1938 case VARIABLE_ARRAY_UNKNOWN:
1939 var->type.specifier.type = slang_spec_array;
1940 var->type.specifier._array = (slang_type_specifier *) slang_alloc_malloc (sizeof (
1941 slang_type_specifier));
1942 if (var->type.specifier._array == NULL)
1943 {
1944 slang_info_log_memory (C->L);
1945 return 0;
1946 }
1947 slang_type_specifier_construct (var->type.specifier._array);
1948 if (!slang_type_specifier_copy (var->type.specifier._array, &type->specifier))
1949 return 0;
1950 break;
1951 case VARIABLE_ARRAY_EXPLICIT:
1952 var->type.specifier.type = slang_spec_array;
1953 var->type.specifier._array = (slang_type_specifier *) slang_alloc_malloc (sizeof (
1954 slang_type_specifier));
1955 if (var->type.specifier._array == NULL)
1956 {
1957 slang_info_log_memory (C->L);
1958 return 0;
1959 }
1960 slang_type_specifier_construct (var->type.specifier._array);
1961 if (!slang_type_specifier_copy (var->type.specifier._array, &type->specifier))
1962 return 0;
1963 var->array_size = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation));
1964 if (var->array_size == NULL)
1965 {
1966 slang_info_log_memory (C->L);
1967 return 0;
1968 }
1969 if (!slang_operation_construct_a (var->array_size))
1970 {
1971 slang_alloc_free (var->array_size);
1972 var->array_size = NULL;
1973 slang_info_log_memory (C->L);
1974 return 0;
1975 }
1976 if (!parse_expression (C, var->array_size, vars, structs, funcs))
1977 return 0;
1978 break;
1979 default:
1980 return 0;
1981 }
1982 if (!(var->type.specifier.type == slang_spec_array && var->array_size == NULL))
1983 {
1984 slang_storage_aggregate agg;
1985
1986 slang_storage_aggregate_construct (&agg);
1987 if (!_slang_aggregate_variable (&agg, &var->type.specifier, var->array_size, funcs,
1988 structs))
1989 {
1990 slang_storage_aggregate_destruct (&agg);
1991 return 0;
1992 }
1993 slang_storage_aggregate_destruct (&agg);
1994 }
1995 return 1;
1996 }
1997
1998 static int parse_init_declarator_list (slang_parse_ctx *C, slang_variable_scope *vars,
1999 slang_struct_scope *structs, slang_function_scope *funcs)
2000 {
2001 slang_fully_specified_type type;
2002
2003 slang_fully_specified_type_construct (&type);
2004 if (!parse_fully_specified_type (C, &type, structs, vars, funcs))
2005 {
2006 slang_fully_specified_type_destruct (&type);
2007 return 0;
2008 }
2009 do
2010 {
2011 if (!parse_init_declarator (C, &type, vars, structs, funcs))
2012 {
2013 slang_fully_specified_type_destruct (&type);
2014 return 0;
2015 }
2016 }
2017 while (*C->I++ == DECLARATOR_NEXT);
2018 slang_fully_specified_type_destruct (&type);
2019 return 1;
2020 }
2021
2022 static int parse_function (slang_parse_ctx *C, int definition, slang_struct_scope *structs,
2023 slang_function_scope *funcs, slang_variable_scope *scope, slang_function **parsed_func_ret)
2024 {
2025 slang_function parsed_func, *found_func;
2026
2027 /* parse function definition/declaration */
2028 slang_function_construct (&parsed_func);
2029 if (definition)
2030 {
2031 if (!parse_function_definition (C, &parsed_func, structs, scope, funcs))
2032 {
2033 slang_function_destruct (&parsed_func);
2034 return 0;
2035 }
2036 }
2037 else
2038 {
2039 if (!parse_function_prototype (C, &parsed_func, structs, scope, funcs))
2040 {
2041 slang_function_destruct (&parsed_func);
2042 return 0;
2043 }
2044 }
2045
2046 /* find a function with a prototype matching the parsed one - only the current scope
2047 is being searched to allow built-in function overriding */
2048 found_func = slang_function_scope_find (funcs, &parsed_func, 0);
2049 if (found_func == NULL)
2050 {
2051 /* add the parsed function to the function list */
2052 funcs->functions = (slang_function *) slang_alloc_realloc (funcs->functions,
2053 funcs->num_functions * sizeof (slang_function), (funcs->num_functions + 1) * sizeof (
2054 slang_function));
2055 if (funcs->functions == NULL)
2056 {
2057 slang_info_log_memory (C->L);
2058 slang_function_destruct (&parsed_func);
2059 return 0;
2060 }
2061 funcs->functions[funcs->num_functions] = parsed_func;
2062 funcs->num_functions++;
2063
2064 /* return the newly parsed function */
2065 *parsed_func_ret = funcs->functions + funcs->num_functions - 1;
2066 }
2067 else
2068 {
2069 /* TODO: check function return type qualifiers and specifiers */
2070 if (definition)
2071 {
2072 /* destroy the existing function declaration and replace it with the new one */
2073 if (found_func->body != NULL)
2074 {
2075 slang_info_log_error (C->L, "%s: function already has a body",
2076 parsed_func.header.name);
2077 slang_function_destruct (&parsed_func);
2078 return 0;
2079 }
2080 slang_function_destruct (found_func);
2081 *found_func = parsed_func;
2082 }
2083 else
2084 {
2085 /* another declaration of the same function prototype - ignore it */
2086 slang_function_destruct (&parsed_func);
2087 }
2088
2089 /* return the found function */
2090 *parsed_func_ret = found_func;
2091 }
2092
2093 /* assemble the parsed function */
2094 if (definition)
2095 {
2096 static int x = 0;
2097 static
2098 slang_assembly_file file;
2099 slang_assembly_name_space space;
2100 x++;
2101 if (x == 1)
2102 slang_assembly_file_construct (&file);
2103 space.funcs = funcs;
2104 space.structs = structs;
2105 space.vars = scope;
2106 if (x == 1)
2107 xxx_first (&file);
2108 (**parsed_func_ret).address = file.count;
2109 if (!_slang_assemble_function (&file, *parsed_func_ret, &space))
2110 {
2111 slang_assembly_file_destruct (&file);
2112 return 0;
2113 }
2114 if (slang_string_compare ("main", (**parsed_func_ret).header.name) == 0)
2115 {
2116 xxx_prolog (&file, (**parsed_func_ret).address);
2117 _slang_execute (&file);
2118 slang_assembly_file_destruct (&file);
2119 _mesa_exit (0);
2120 }
2121 }
2122 return 1;
2123 }
2124
2125 /* declaration */
2126 #define DECLARATION_FUNCTION_PROTOTYPE 1
2127 #define DECLARATION_INIT_DECLARATOR_LIST 2
2128
2129 static int parse_declaration (slang_parse_ctx *C, slang_variable_scope *scope,
2130 slang_struct_scope *structs, slang_function_scope *funcs)
2131 {
2132 slang_function *dummy_func;
2133
2134 switch (*C->I++)
2135 {
2136 case DECLARATION_INIT_DECLARATOR_LIST:
2137 if (!parse_init_declarator_list (C, scope, structs, funcs))
2138 return 0;
2139 break;
2140 case DECLARATION_FUNCTION_PROTOTYPE:
2141 if (!parse_function (C, 0, structs, funcs, scope, &dummy_func))
2142 return 0;
2143 break;
2144 default:
2145 return 0;
2146 }
2147 return 1;
2148 }
2149
2150 /* external declaration */
2151 #define EXTERNAL_NULL 0
2152 #define EXTERNAL_FUNCTION_DEFINITION 1
2153 #define EXTERNAL_DECLARATION 2
2154
2155 static int parse_translation_unit (slang_parse_ctx *C, slang_translation_unit *unit)
2156 {
2157 while (*C->I != EXTERNAL_NULL)
2158 {
2159 slang_function *func;
2160
2161 switch (*C->I++)
2162 {
2163 case EXTERNAL_FUNCTION_DEFINITION:
2164 if (!parse_function (C, 1, &unit->structs, &unit->functions, &unit->globals, &func))
2165 return 0;
2166 break;
2167 case EXTERNAL_DECLARATION:
2168 if (!parse_declaration (C, &unit->globals, &unit->structs, &unit->functions))
2169 return 0;
2170 break;
2171 default:
2172 return 0;
2173 }
2174 }
2175 C->I++;
2176 return 1;
2177 }
2178
2179 static int compile_binary (const byte *prod, slang_translation_unit *unit, slang_unit_type type,
2180 slang_info_log *log, slang_translation_unit *builtins)
2181 {
2182 slang_parse_ctx C;
2183
2184 /* set-up parse context */
2185 C.I = prod;
2186 C.L = log;
2187 C.parsing_builtin = builtins == NULL;
2188
2189 if (!check_revision (&C))
2190 return 0;
2191
2192 /* create translation unit object */
2193 slang_translation_unit_construct (unit);
2194 unit->type = type;
2195
2196 if (builtins != NULL)
2197 {
2198 /* link to built-in functions */
2199 builtins[1].functions.outer_scope = &builtins[0].functions;
2200 builtins[2].functions.outer_scope = &builtins[1].functions;
2201 unit->functions.outer_scope = &builtins[2].functions;
2202
2203 /* link to built-in variables - core unit does not define any */
2204 builtins[2].globals.outer_scope = &builtins[1].globals;
2205 unit->globals.outer_scope = &builtins[2].globals;
2206
2207 /* link to built-in structure typedefs - only in common unit */
2208 unit->structs.outer_scope = &builtins[1].structs;
2209 }
2210
2211 /* parse translation unit */
2212 if (!parse_translation_unit (&C, unit))
2213 {
2214 slang_translation_unit_destruct (unit);
2215 return 0;
2216 }
2217
2218 return 1;
2219 }
2220
2221 static int compile_with_grammar (grammar id, const char *source, slang_translation_unit *unit,
2222 slang_unit_type type, slang_info_log *log, slang_translation_unit *builtins)
2223 {
2224 byte *prod;
2225 unsigned int size, start, version;
2226
2227 /* retrieve version */
2228 if (!_slang_preprocess_version (source, &version, &start, log))
2229 return 0;
2230
2231 /* check the syntax */
2232 if (!grammar_fast_check (id, (const byte *) source + start, &prod, &size, 65536))
2233 {
2234 char buf[1024];
2235 unsigned int pos;
2236 grammar_get_last_error ( (unsigned char*) buf, 1024, (int*) &pos);
2237 slang_info_log_error (log, buf);
2238 return 0;
2239 }
2240
2241 if (!compile_binary (prod, unit, type, log, builtins))
2242 {
2243 grammar_alloc_free (prod);
2244 return 0;
2245 }
2246
2247 grammar_alloc_free (prod);
2248 return 1;
2249 }
2250
2251 static const char *slang_shader_syn =
2252 #include "library/slang_shader_syn.h"
2253 ;
2254 /*
2255 static const byte slang_core_gc_bin[] = {
2256 #include "library/slang_core_gc_bin.h"
2257 };*/
2258 static const byte slang_core_gc[] = {
2259 #include "library/slang_core_gc.h"
2260 };
2261
2262 static const byte slang_common_builtin_gc_bin[] = {
2263 #include "library/slang_common_builtin_gc_bin.h"
2264 };
2265
2266 static const byte slang_fragment_builtin_gc_bin[] = {
2267 #include "library/slang_fragment_builtin_gc_bin.h"
2268 };
2269
2270 static const byte slang_vertex_builtin_gc_bin[] = {
2271 #include "library/slang_vertex_builtin_gc_bin.h"
2272 };
2273
2274 int _slang_compile (const char *source, slang_translation_unit *unit, slang_unit_type type,
2275 slang_info_log *log)
2276 {
2277 grammar id;
2278 slang_translation_unit builtin_units[3];
2279 slang_translation_unit *builtins = NULL;
2280
2281 /* load slang grammar */
2282 id = grammar_load_from_text ((const byte *) slang_shader_syn);
2283 if (id == 0)
2284 {
2285 char buf[1024];
2286 unsigned int pos;
2287 grammar_get_last_error ( (unsigned char*) buf, 1024, (int*) &pos);
2288 slang_info_log_error (log, buf);
2289 return 0;
2290 }
2291
2292 /* set shader type - the syntax is slightly different for different shaders */
2293 if (type == slang_unit_fragment_shader || type == slang_unit_fragment_builtin)
2294 grammar_set_reg8 (id, (const byte *) "shader_type", 1);
2295 else
2296 grammar_set_reg8 (id, (const byte *) "shader_type", 2);
2297
2298 /* enable language extensions */
2299 grammar_set_reg8 (id, (const byte *) "parsing_builtin", 1);
2300
2301 /* if parsing user-specified shader, load built-in library */
2302 if (type == slang_unit_fragment_shader || type == slang_unit_vertex_shader)
2303 {
2304 /*if (!compile_binary (slang_core_gc_bin, builtin_units,
2305 slang_unit_fragment_builtin, log, NULL))*/
2306 if (!compile_with_grammar (id, (const char*) slang_core_gc, builtin_units, slang_unit_fragment_builtin,
2307 log, NULL))
2308 {
2309 grammar_destroy (id);
2310 return 0;
2311 }
2312 if (!compile_binary (slang_common_builtin_gc_bin, builtin_units + 1,
2313 slang_unit_fragment_builtin, log, NULL))
2314 {
2315 slang_translation_unit_destruct (builtin_units);
2316 grammar_destroy (id);
2317 return 0;
2318 }
2319 if (type == slang_unit_fragment_shader)
2320 {
2321 if (!compile_binary (slang_fragment_builtin_gc_bin, builtin_units + 2,
2322 slang_unit_fragment_builtin, log, NULL))
2323 {
2324 slang_translation_unit_destruct (builtin_units);
2325 slang_translation_unit_destruct (builtin_units + 1);
2326 grammar_destroy (id);
2327 return 0;
2328 }
2329 }
2330 else if (type == slang_unit_vertex_shader)
2331 {
2332 if (!compile_binary (slang_vertex_builtin_gc_bin, builtin_units + 2,
2333 slang_unit_vertex_builtin, log, NULL))
2334 {
2335 slang_translation_unit_destruct (builtin_units);
2336 slang_translation_unit_destruct (builtin_units + 1);
2337 grammar_destroy (id);
2338 return 0;
2339 }
2340 }
2341
2342 /* disable language extensions */
2343 grammar_set_reg8 (id, (const byte *) "parsing_builtin", 0);
2344 builtins = builtin_units;
2345 }
2346
2347 /* compile the actual shader - pass-in built-in library for external shader */
2348 if (!compile_with_grammar (id, source, unit, type, log, builtins))
2349 {
2350 if (type == slang_unit_fragment_shader || type == slang_unit_vertex_shader)
2351 {
2352 slang_translation_unit_destruct (builtin_units);
2353 slang_translation_unit_destruct (builtin_units + 1);
2354 slang_translation_unit_destruct (builtin_units + 2);
2355 }
2356 grammar_destroy (id);
2357 return 0;
2358 }
2359
2360 /* destroy built-in library */
2361 if (type == slang_unit_fragment_shader || type == slang_unit_vertex_shader)
2362 {
2363 slang_translation_unit_destruct (builtin_units);
2364 slang_translation_unit_destruct (builtin_units + 1);
2365 slang_translation_unit_destruct (builtin_units + 2);
2366 }
2367 grammar_destroy (id);
2368 return 1;
2369 }
2370