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