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