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