a1cae5d338d8621b4b00f5b865d148a62bf9cc8c
[mesa.git] / src / mesa / shader / slang / slang_compile.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
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 "context.h"
33 #include "program.h"
34 #include "prog_parameter.h"
35 #include "grammar_mesa.h"
36 #include "slang_codegen.h"
37 #include "slang_compile.h"
38 #include "slang_preprocess.h"
39 #include "slang_storage.h"
40 #include "slang_error.h"
41 #include "slang_emit.h"
42 #include "slang_vartable.h"
43 #include "slang_simplify.h"
44
45 #include "slang_print.h"
46
47 /*
48 * This is a straightforward implementation of the slang front-end
49 * compiler. Lots of error-checking functionality is missing but
50 * every well-formed shader source should compile successfully and
51 * execute as expected. However, some semantically ill-formed shaders
52 * may be accepted resulting in undefined behaviour.
53 */
54
55
56
57 /**
58 * Allocate storage for a variable of 'size' bytes from given pool.
59 * Return the allocated address for the variable.
60 */
61 static GLuint
62 slang_var_pool_alloc(slang_var_pool * pool, unsigned int size)
63 {
64 const GLuint addr = pool->next_addr;
65 pool->next_addr += size;
66 return addr;
67 }
68
69 /*
70 * slang_code_unit
71 */
72
73 GLvoid
74 _slang_code_unit_ctr(slang_code_unit * self,
75 struct slang_code_object_ * object)
76 {
77 _slang_variable_scope_ctr(&self->vars);
78 _slang_function_scope_ctr(&self->funs);
79 _slang_struct_scope_ctr(&self->structs);
80 self->object = object;
81 }
82
83 GLvoid
84 _slang_code_unit_dtr(slang_code_unit * self)
85 {
86 slang_variable_scope_destruct(&self->vars);
87 slang_function_scope_destruct(&self->funs);
88 slang_struct_scope_destruct(&self->structs);
89 }
90
91 /*
92 * slang_code_object
93 */
94
95 GLvoid
96 _slang_code_object_ctr(slang_code_object * self)
97 {
98 GLuint i;
99
100 for (i = 0; i < SLANG_BUILTIN_TOTAL; i++)
101 _slang_code_unit_ctr(&self->builtin[i], self);
102 _slang_code_unit_ctr(&self->unit, self);
103 self->varpool.next_addr = 0;
104 slang_atom_pool_construct(&self->atompool);
105 }
106
107 GLvoid
108 _slang_code_object_dtr(slang_code_object * self)
109 {
110 GLuint i;
111
112 for (i = 0; i < SLANG_BUILTIN_TOTAL; i++)
113 _slang_code_unit_dtr(&self->builtin[i]);
114 _slang_code_unit_dtr(&self->unit);
115 slang_atom_pool_destruct(&self->atompool);
116 }
117
118 /* slang_info_log */
119
120 static char *out_of_memory = "Error: Out of memory.\n";
121
122 void
123 slang_info_log_construct(slang_info_log * log)
124 {
125 log->text = NULL;
126 log->dont_free_text = 0;
127 }
128
129 void
130 slang_info_log_destruct(slang_info_log * log)
131 {
132 if (!log->dont_free_text)
133 slang_alloc_free(log->text);
134 }
135
136 static int
137 slang_info_log_message(slang_info_log * log, const char *prefix,
138 const char *msg)
139 {
140 GLuint size;
141
142 if (log->dont_free_text)
143 return 0;
144 size = slang_string_length(msg) + 2;
145 if (prefix != NULL)
146 size += slang_string_length(prefix) + 2;
147 if (log->text != NULL) {
148 GLuint old_len = slang_string_length(log->text);
149 log->text = (char *)
150 slang_alloc_realloc(log->text, old_len + 1, old_len + size);
151 }
152 else {
153 log->text = (char *) (slang_alloc_malloc(size));
154 if (log->text != NULL)
155 log->text[0] = '\0';
156 }
157 if (log->text == NULL)
158 return 0;
159 if (prefix != NULL) {
160 slang_string_concat(log->text, prefix);
161 slang_string_concat(log->text, ": ");
162 }
163 slang_string_concat(log->text, msg);
164 slang_string_concat(log->text, "\n");
165 #if 1
166 abort(); /* XXX temporary */
167 #endif
168 return 1;
169 }
170
171 int
172 slang_info_log_print(slang_info_log * log, const char *msg, ...)
173 {
174 va_list va;
175 char buf[1024];
176
177 va_start(va, msg);
178 _mesa_vsprintf(buf, msg, va);
179 va_end(va);
180 return slang_info_log_message(log, NULL, buf);
181 }
182
183 int
184 slang_info_log_error(slang_info_log * log, const char *msg, ...)
185 {
186 va_list va;
187 char buf[1024];
188
189 va_start(va, msg);
190 _mesa_vsprintf(buf, msg, va);
191 va_end(va);
192 if (slang_info_log_message(log, "Error", buf))
193 return 1;
194 slang_info_log_memory(log);
195 return 0;
196 }
197
198 int
199 slang_info_log_warning(slang_info_log * log, const char *msg, ...)
200 {
201 va_list va;
202 char buf[1024];
203
204 va_start(va, msg);
205 _mesa_vsprintf(buf, msg, va);
206 va_end(va);
207 if (slang_info_log_message(log, "Warning", buf))
208 return 1;
209 slang_info_log_memory(log);
210 return 0;
211 }
212
213 void
214 slang_info_log_memory(slang_info_log * log)
215 {
216 if (!slang_info_log_message(log, "Error", "Out of memory.")) {
217 log->dont_free_text = 1;
218 log->text = out_of_memory;
219 }
220 abort(); /* XXX temporary */
221 }
222
223 /* slang_parse_ctx */
224
225 typedef struct slang_parse_ctx_
226 {
227 const byte *I;
228 slang_info_log *L;
229 int parsing_builtin;
230 GLboolean global_scope; /**< Is object being declared a global? */
231 slang_atom_pool *atoms;
232 slang_unit_type type; /**< Vertex vs. Fragment */
233 } slang_parse_ctx;
234
235 /* slang_output_ctx */
236
237 typedef struct slang_output_ctx_
238 {
239 slang_variable_scope *vars;
240 slang_function_scope *funs;
241 slang_struct_scope *structs;
242 slang_var_pool *global_pool;
243 struct gl_program *program;
244 slang_var_table *vartable;
245 } slang_output_ctx;
246
247 /* _slang_compile() */
248
249 static void
250 parse_identifier_str(slang_parse_ctx * C, char **id)
251 {
252 *id = (char *) C->I;
253 C->I += _mesa_strlen(*id) + 1;
254 }
255
256 static slang_atom
257 parse_identifier(slang_parse_ctx * C)
258 {
259 const char *id;
260
261 id = (const char *) C->I;
262 C->I += _mesa_strlen(id) + 1;
263 return slang_atom_pool_atom(C->atoms, id);
264 }
265
266 static int
267 parse_number(slang_parse_ctx * C, int *number)
268 {
269 const int radix = (int) (*C->I++);
270 *number = 0;
271 while (*C->I != '\0') {
272 int digit;
273 if (*C->I >= '0' && *C->I <= '9')
274 digit = (int) (*C->I - '0');
275 else if (*C->I >= 'A' && *C->I <= 'Z')
276 digit = (int) (*C->I - 'A') + 10;
277 else
278 digit = (int) (*C->I - 'a') + 10;
279 *number = *number * radix + digit;
280 C->I++;
281 }
282 C->I++;
283 if (*number > 65535)
284 slang_info_log_warning(C->L, "%d: literal integer overflow.", *number);
285 return 1;
286 }
287
288 static int
289 parse_float(slang_parse_ctx * C, float *number)
290 {
291 char *integral = NULL;
292 char *fractional = NULL;
293 char *exponent = NULL;
294 char *whole = NULL;
295
296 parse_identifier_str(C, &integral);
297 parse_identifier_str(C, &fractional);
298 parse_identifier_str(C, &exponent);
299
300 whole = (char *) (slang_alloc_malloc((_mesa_strlen(integral) +
301 _mesa_strlen(fractional) +
302 _mesa_strlen(exponent) + 3) * sizeof(char)));
303 if (whole == NULL) {
304 slang_info_log_memory(C->L);
305 return 0;
306 }
307
308 slang_string_copy(whole, integral);
309 slang_string_concat(whole, ".");
310 slang_string_concat(whole, fractional);
311 slang_string_concat(whole, "E");
312 slang_string_concat(whole, exponent);
313
314 *number = (float) (_mesa_strtod(whole, (char **) NULL));
315
316 slang_alloc_free(whole);
317 return 1;
318 }
319
320 /* revision number - increment after each change affecting emitted output */
321 #define REVISION 3
322
323 static int
324 check_revision(slang_parse_ctx * C)
325 {
326 if (*C->I != REVISION) {
327 slang_info_log_error(C->L, "Internal compiler error.");
328 return 0;
329 }
330 C->I++;
331 return 1;
332 }
333
334 static int parse_statement(slang_parse_ctx *, slang_output_ctx *,
335 slang_operation *);
336 static int parse_expression(slang_parse_ctx *, slang_output_ctx *,
337 slang_operation *);
338 static int parse_type_specifier(slang_parse_ctx *, slang_output_ctx *,
339 slang_type_specifier *);
340
341 static GLboolean
342 parse_array_len(slang_parse_ctx * C, slang_output_ctx * O, GLuint * len)
343 {
344 slang_operation array_size;
345 slang_name_space space;
346 GLboolean result;
347
348 if (!slang_operation_construct(&array_size))
349 return GL_FALSE;
350 if (!parse_expression(C, O, &array_size)) {
351 slang_operation_destruct(&array_size);
352 return GL_FALSE;
353 }
354
355 space.funcs = O->funs;
356 space.structs = O->structs;
357 space.vars = O->vars;
358
359 /* evaluate compile-time expression which is array size */
360 _slang_simplify(&array_size, &space, C->atoms);
361 result = (array_size.type == SLANG_OPER_LITERAL_INT);
362
363 *len = (GLint) array_size.literal[0];
364
365 slang_operation_destruct(&array_size);
366 return result;
367 }
368
369 static GLboolean
370 calculate_var_size(slang_parse_ctx * C, slang_output_ctx * O,
371 slang_variable * var)
372 {
373 slang_storage_aggregate agg;
374
375 if (!slang_storage_aggregate_construct(&agg))
376 return GL_FALSE;
377 if (!_slang_aggregate_variable(&agg, &var->type.specifier, var->array_len,
378 O->funs, O->structs, O->vars, C->atoms)) {
379 slang_storage_aggregate_destruct(&agg);
380 return GL_FALSE;
381 }
382 var->size = _slang_sizeof_aggregate(&agg);
383 slang_storage_aggregate_destruct(&agg);
384 return GL_TRUE;
385 }
386
387 static GLboolean
388 convert_to_array(slang_parse_ctx * C, slang_variable * var,
389 const slang_type_specifier * sp)
390 {
391 /* sized array - mark it as array, copy the specifier to the array element and
392 * parse the expression */
393 var->type.specifier.type = SLANG_SPEC_ARRAY;
394 var->type.specifier._array = (slang_type_specifier *)
395 slang_alloc_malloc(sizeof(slang_type_specifier));
396 if (var->type.specifier._array == NULL) {
397 slang_info_log_memory(C->L);
398 return GL_FALSE;
399 }
400 slang_type_specifier_ctr(var->type.specifier._array);
401 return slang_type_specifier_copy(var->type.specifier._array, sp);
402 }
403
404 /* structure field */
405 #define FIELD_NONE 0
406 #define FIELD_NEXT 1
407 #define FIELD_ARRAY 2
408
409 static GLboolean
410 parse_struct_field_var(slang_parse_ctx * C, slang_output_ctx * O,
411 slang_variable * var, const slang_type_specifier * sp)
412 {
413 var->a_name = parse_identifier(C);
414 if (var->a_name == SLANG_ATOM_NULL)
415 return GL_FALSE;
416
417 switch (*C->I++) {
418 case FIELD_NONE:
419 if (!slang_type_specifier_copy(&var->type.specifier, sp))
420 return GL_FALSE;
421 break;
422 case FIELD_ARRAY:
423 if (!convert_to_array(C, var, sp))
424 return GL_FALSE;
425 if (!parse_array_len(C, O, &var->array_len))
426 return GL_FALSE;
427 break;
428 default:
429 return GL_FALSE;
430 }
431
432 return calculate_var_size(C, O, var);
433 }
434
435 static int
436 parse_struct_field(slang_parse_ctx * C, slang_output_ctx * O,
437 slang_struct * st, slang_type_specifier * sp)
438 {
439 slang_output_ctx o = *O;
440
441 o.structs = st->structs;
442 if (!parse_type_specifier(C, &o, sp))
443 return 0;
444
445 do {
446 slang_variable *var = slang_variable_scope_grow(st->fields);
447 if (!var) {
448 slang_info_log_memory(C->L);
449 return 0;
450 }
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, "undeclared type name '%s'",
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 if (!parse_type_specifier(C, O, &type->specifier))
718 return 0;
719 return 1;
720 }
721
722 /* operation */
723 #define OP_END 0
724 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
725 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
726 #define OP_DECLARE 3
727 #define OP_ASM 4
728 #define OP_BREAK 5
729 #define OP_CONTINUE 6
730 #define OP_DISCARD 7
731 #define OP_RETURN 8
732 #define OP_EXPRESSION 9
733 #define OP_IF 10
734 #define OP_WHILE 11
735 #define OP_DO 12
736 #define OP_FOR 13
737 #define OP_PUSH_VOID 14
738 #define OP_PUSH_BOOL 15
739 #define OP_PUSH_INT 16
740 #define OP_PUSH_FLOAT 17
741 #define OP_PUSH_IDENTIFIER 18
742 #define OP_SEQUENCE 19
743 #define OP_ASSIGN 20
744 #define OP_ADDASSIGN 21
745 #define OP_SUBASSIGN 22
746 #define OP_MULASSIGN 23
747 #define OP_DIVASSIGN 24
748 /*#define OP_MODASSIGN 25*/
749 /*#define OP_LSHASSIGN 26*/
750 /*#define OP_RSHASSIGN 27*/
751 /*#define OP_ORASSIGN 28*/
752 /*#define OP_XORASSIGN 29*/
753 /*#define OP_ANDASSIGN 30*/
754 #define OP_SELECT 31
755 #define OP_LOGICALOR 32
756 #define OP_LOGICALXOR 33
757 #define OP_LOGICALAND 34
758 /*#define OP_BITOR 35*/
759 /*#define OP_BITXOR 36*/
760 /*#define OP_BITAND 37*/
761 #define OP_EQUAL 38
762 #define OP_NOTEQUAL 39
763 #define OP_LESS 40
764 #define OP_GREATER 41
765 #define OP_LESSEQUAL 42
766 #define OP_GREATEREQUAL 43
767 /*#define OP_LSHIFT 44*/
768 /*#define OP_RSHIFT 45*/
769 #define OP_ADD 46
770 #define OP_SUBTRACT 47
771 #define OP_MULTIPLY 48
772 #define OP_DIVIDE 49
773 /*#define OP_MODULUS 50*/
774 #define OP_PREINCREMENT 51
775 #define OP_PREDECREMENT 52
776 #define OP_PLUS 53
777 #define OP_MINUS 54
778 /*#define OP_COMPLEMENT 55*/
779 #define OP_NOT 56
780 #define OP_SUBSCRIPT 57
781 #define OP_CALL 58
782 #define OP_FIELD 59
783 #define OP_POSTINCREMENT 60
784 #define OP_POSTDECREMENT 61
785
786
787 /**
788 * When parsing a compound production, this function is used to parse the
789 * children.
790 * For example, a a while-loop compound will have two children, the
791 * while condition expression and the loop body. So, this function will
792 * be called twice to parse those two sub-expressions.
793 * \param C the parsing context
794 * \param O the output context
795 * \param oper the operation we're parsing
796 * \param statement indicates whether parsing a statement, or expression
797 * \return 1 if success, 0 if error
798 */
799 static int
800 parse_child_operation(slang_parse_ctx * C, slang_output_ctx * O,
801 slang_operation * oper, GLboolean statement)
802 {
803 slang_operation *ch;
804
805 /* grow child array */
806 ch = slang_operation_grow(&oper->num_children, &oper->children);
807 if (statement)
808 return parse_statement(C, O, ch);
809 return parse_expression(C, O, ch);
810 }
811
812 static int parse_declaration(slang_parse_ctx * C, slang_output_ctx * O);
813
814 static int
815 parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
816 slang_operation * oper)
817 {
818 oper->locals->outer_scope = O->vars;
819 switch (*C->I++) {
820 case OP_BLOCK_BEGIN_NO_NEW_SCOPE:
821 /* parse child statements, do not create new variable scope */
822 oper->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE;
823 while (*C->I != OP_END)
824 if (!parse_child_operation(C, O, oper, 1))
825 return 0;
826 C->I++;
827 break;
828 case OP_BLOCK_BEGIN_NEW_SCOPE:
829 /* parse child statements, create new variable scope */
830 {
831 slang_output_ctx o = *O;
832
833 oper->type = SLANG_OPER_BLOCK_NEW_SCOPE;
834 o.vars = oper->locals;
835 while (*C->I != OP_END)
836 if (!parse_child_operation(C, &o, oper, 1))
837 return 0;
838 C->I++;
839 }
840 break;
841 case OP_DECLARE:
842 /* local variable declaration, individual declarators are stored as
843 * children identifiers
844 */
845 oper->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE;
846 {
847 const unsigned int first_var = O->vars->num_variables;
848
849 /* parse the declaration, note that there can be zero or more
850 * than one declarators
851 */
852 if (!parse_declaration(C, O))
853 return 0;
854 if (first_var < O->vars->num_variables) {
855 const unsigned int num_vars = O->vars->num_variables - first_var;
856 unsigned int i;
857
858 oper->num_children = num_vars;
859 oper->children = slang_operation_new(num_vars);
860 if (oper->children == NULL) {
861 slang_info_log_memory(C->L);
862 return 0;
863 }
864 for (i = first_var; i < O->vars->num_variables; i++) {
865 slang_operation *o = &oper->children[i - first_var];
866 o->type = SLANG_OPER_VARIABLE_DECL;
867 o->locals->outer_scope = O->vars;
868 o->a_id = O->vars->variables[i]->a_name;
869 }
870 }
871 }
872 break;
873 case OP_ASM:
874 /* the __asm statement, parse the mnemonic and all its arguments
875 * as expressions
876 */
877 oper->type = SLANG_OPER_ASM;
878 oper->a_id = parse_identifier(C);
879 if (oper->a_id == SLANG_ATOM_NULL)
880 return 0;
881 while (*C->I != OP_END) {
882 if (!parse_child_operation(C, O, oper, 0))
883 return 0;
884 }
885 C->I++;
886 break;
887 case OP_BREAK:
888 oper->type = SLANG_OPER_BREAK;
889 break;
890 case OP_CONTINUE:
891 oper->type = SLANG_OPER_CONTINUE;
892 break;
893 case OP_DISCARD:
894 oper->type = SLANG_OPER_DISCARD;
895 break;
896 case OP_RETURN:
897 oper->type = SLANG_OPER_RETURN;
898 if (!parse_child_operation(C, O, oper, 0))
899 return 0;
900 break;
901 case OP_EXPRESSION:
902 oper->type = SLANG_OPER_EXPRESSION;
903 if (!parse_child_operation(C, O, oper, 0))
904 return 0;
905 break;
906 case OP_IF:
907 oper->type = SLANG_OPER_IF;
908 if (!parse_child_operation(C, O, oper, 0))
909 return 0;
910 if (!parse_child_operation(C, O, oper, 1))
911 return 0;
912 if (!parse_child_operation(C, O, oper, 1))
913 return 0;
914 break;
915 case OP_WHILE:
916 {
917 slang_output_ctx o = *O;
918
919 oper->type = SLANG_OPER_WHILE;
920 o.vars = oper->locals;
921 if (!parse_child_operation(C, &o, oper, 1))
922 return 0;
923 if (!parse_child_operation(C, &o, oper, 1))
924 return 0;
925 }
926 break;
927 case OP_DO:
928 oper->type = SLANG_OPER_DO;
929 if (!parse_child_operation(C, O, oper, 1))
930 return 0;
931 if (!parse_child_operation(C, O, oper, 0))
932 return 0;
933 break;
934 case OP_FOR:
935 {
936 slang_output_ctx o = *O;
937
938 oper->type = SLANG_OPER_FOR;
939 o.vars = oper->locals;
940 if (!parse_child_operation(C, &o, oper, 1))
941 return 0;
942 if (!parse_child_operation(C, &o, oper, 1))
943 return 0;
944 if (!parse_child_operation(C, &o, oper, 0))
945 return 0;
946 if (!parse_child_operation(C, &o, oper, 1))
947 return 0;
948 }
949 break;
950 default:
951 return 0;
952 }
953 return 1;
954 }
955
956 static int
957 handle_nary_expression(slang_parse_ctx * C, slang_operation * op,
958 slang_operation ** ops, unsigned int *total_ops,
959 unsigned int n)
960 {
961 unsigned int i;
962
963 op->children =
964 (slang_operation *) slang_alloc_malloc(n * sizeof(slang_operation));
965 if (op->children == NULL) {
966 slang_info_log_memory(C->L);
967 return 0;
968 }
969 op->num_children = n;
970
971 for (i = 0; i < n; i++)
972 op->children[i] = (*ops)[*total_ops - (n + 1 - i)];
973 (*ops)[*total_ops - (n + 1)] = (*ops)[*total_ops - 1];
974 *total_ops -= n;
975
976 *ops = (slang_operation *)
977 slang_alloc_realloc(*ops,
978 (*total_ops + n) * sizeof(slang_operation),
979 *total_ops * sizeof(slang_operation));
980 if (*ops == NULL) {
981 slang_info_log_memory(C->L);
982 return 0;
983 }
984 return 1;
985 }
986
987 static int
988 is_constructor_name(const char *name, slang_atom a_name,
989 slang_struct_scope * structs)
990 {
991 if (slang_type_specifier_type_from_string(name) != SLANG_SPEC_VOID)
992 return 1;
993 return slang_struct_scope_find(structs, a_name, 1) != NULL;
994 }
995
996 static int
997 parse_expression(slang_parse_ctx * C, slang_output_ctx * O,
998 slang_operation * oper)
999 {
1000 slang_operation *ops = NULL;
1001 unsigned int num_ops = 0;
1002 int number;
1003
1004 while (*C->I != OP_END) {
1005 slang_operation *op;
1006 const unsigned int op_code = *C->I++;
1007
1008 /* allocate default operation, becomes a no-op if not used */
1009 ops = (slang_operation *)
1010 slang_alloc_realloc(ops,
1011 num_ops * sizeof(slang_operation),
1012 (num_ops + 1) * sizeof(slang_operation));
1013 if (ops == NULL) {
1014 slang_info_log_memory(C->L);
1015 return 0;
1016 }
1017 op = &ops[num_ops];
1018 if (!slang_operation_construct(op)) {
1019 slang_info_log_memory(C->L);
1020 return 0;
1021 }
1022 num_ops++;
1023 op->locals->outer_scope = O->vars;
1024
1025 switch (op_code) {
1026 case OP_PUSH_VOID:
1027 op->type = SLANG_OPER_VOID;
1028 break;
1029 case OP_PUSH_BOOL:
1030 op->type = SLANG_OPER_LITERAL_BOOL;
1031 if (!parse_number(C, &number))
1032 return 0;
1033 op->literal[0] =
1034 op->literal[1] =
1035 op->literal[2] =
1036 op->literal[3] = (GLfloat) number;
1037 op->literal_size = 1;
1038 break;
1039 case OP_PUSH_INT:
1040 op->type = SLANG_OPER_LITERAL_INT;
1041 if (!parse_number(C, &number))
1042 return 0;
1043 op->literal[0] =
1044 op->literal[1] =
1045 op->literal[2] =
1046 op->literal[3] = (GLfloat) number;
1047 op->literal_size = 1;
1048 break;
1049 case OP_PUSH_FLOAT:
1050 op->type = SLANG_OPER_LITERAL_FLOAT;
1051 if (!parse_float(C, &op->literal[0]))
1052 return 0;
1053 op->literal[1] =
1054 op->literal[2] =
1055 op->literal[3] = op->literal[0];
1056 op->literal_size = 1;
1057 break;
1058 case OP_PUSH_IDENTIFIER:
1059 op->type = SLANG_OPER_IDENTIFIER;
1060 op->a_id = parse_identifier(C);
1061 if (op->a_id == SLANG_ATOM_NULL)
1062 return 0;
1063 break;
1064 case OP_SEQUENCE:
1065 op->type = SLANG_OPER_SEQUENCE;
1066 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1067 return 0;
1068 break;
1069 case OP_ASSIGN:
1070 op->type = SLANG_OPER_ASSIGN;
1071 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1072 return 0;
1073 break;
1074 case OP_ADDASSIGN:
1075 op->type = SLANG_OPER_ADDASSIGN;
1076 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1077 return 0;
1078 break;
1079 case OP_SUBASSIGN:
1080 op->type = SLANG_OPER_SUBASSIGN;
1081 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1082 return 0;
1083 break;
1084 case OP_MULASSIGN:
1085 op->type = SLANG_OPER_MULASSIGN;
1086 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1087 return 0;
1088 break;
1089 case OP_DIVASSIGN:
1090 op->type = SLANG_OPER_DIVASSIGN;
1091 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1092 return 0;
1093 break;
1094 /*case OP_MODASSIGN: */
1095 /*case OP_LSHASSIGN: */
1096 /*case OP_RSHASSIGN: */
1097 /*case OP_ORASSIGN: */
1098 /*case OP_XORASSIGN: */
1099 /*case OP_ANDASSIGN: */
1100 case OP_SELECT:
1101 op->type = SLANG_OPER_SELECT;
1102 if (!handle_nary_expression(C, op, &ops, &num_ops, 3))
1103 return 0;
1104 break;
1105 case OP_LOGICALOR:
1106 op->type = SLANG_OPER_LOGICALOR;
1107 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1108 return 0;
1109 break;
1110 case OP_LOGICALXOR:
1111 op->type = SLANG_OPER_LOGICALXOR;
1112 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1113 return 0;
1114 break;
1115 case OP_LOGICALAND:
1116 op->type = SLANG_OPER_LOGICALAND;
1117 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1118 return 0;
1119 break;
1120 /*case OP_BITOR: */
1121 /*case OP_BITXOR: */
1122 /*case OP_BITAND: */
1123 case OP_EQUAL:
1124 op->type = SLANG_OPER_EQUAL;
1125 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1126 return 0;
1127 break;
1128 case OP_NOTEQUAL:
1129 op->type = SLANG_OPER_NOTEQUAL;
1130 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1131 return 0;
1132 break;
1133 case OP_LESS:
1134 op->type = SLANG_OPER_LESS;
1135 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1136 return 0;
1137 break;
1138 case OP_GREATER:
1139 op->type = SLANG_OPER_GREATER;
1140 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1141 return 0;
1142 break;
1143 case OP_LESSEQUAL:
1144 op->type = SLANG_OPER_LESSequal;
1145 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1146 return 0;
1147 break;
1148 case OP_GREATEREQUAL:
1149 op->type = SLANG_OPER_GREATERequal;
1150 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1151 return 0;
1152 break;
1153 /*case OP_LSHIFT: */
1154 /*case OP_RSHIFT: */
1155 case OP_ADD:
1156 op->type = SLANG_OPER_ADD;
1157 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1158 return 0;
1159 break;
1160 case OP_SUBTRACT:
1161 op->type = SLANG_OPER_SUBTRACT;
1162 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1163 return 0;
1164 break;
1165 case OP_MULTIPLY:
1166 op->type = SLANG_OPER_MULTIPLY;
1167 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1168 return 0;
1169 break;
1170 case OP_DIVIDE:
1171 op->type = SLANG_OPER_DIVIDE;
1172 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1173 return 0;
1174 break;
1175 /*case OP_MODULUS: */
1176 case OP_PREINCREMENT:
1177 op->type = SLANG_OPER_PREINCREMENT;
1178 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1179 return 0;
1180 break;
1181 case OP_PREDECREMENT:
1182 op->type = SLANG_OPER_PREDECREMENT;
1183 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1184 return 0;
1185 break;
1186 case OP_PLUS:
1187 op->type = SLANG_OPER_PLUS;
1188 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1189 return 0;
1190 break;
1191 case OP_MINUS:
1192 op->type = SLANG_OPER_MINUS;
1193 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1194 return 0;
1195 break;
1196 case OP_NOT:
1197 op->type = SLANG_OPER_NOT;
1198 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1199 return 0;
1200 break;
1201 /*case OP_COMPLEMENT: */
1202 case OP_SUBSCRIPT:
1203 op->type = SLANG_OPER_SUBSCRIPT;
1204 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1205 return 0;
1206 break;
1207 case OP_CALL:
1208 op->type = SLANG_OPER_CALL;
1209 op->a_id = parse_identifier(C);
1210 if (op->a_id == SLANG_ATOM_NULL)
1211 return 0;
1212 while (*C->I != OP_END)
1213 if (!parse_child_operation(C, O, op, 0))
1214 return 0;
1215 C->I++;
1216
1217 if (!C->parsing_builtin
1218 && !slang_function_scope_find_by_name(O->funs, op->a_id, 1)) {
1219 const char *id;
1220
1221 id = slang_atom_pool_id(C->atoms, op->a_id);
1222 if (!is_constructor_name(id, op->a_id, O->structs)) {
1223 slang_info_log_error(C->L, "%s: undeclared function name.", id);
1224 return 0;
1225 }
1226 }
1227 break;
1228 case OP_FIELD:
1229 op->type = SLANG_OPER_FIELD;
1230 op->a_id = parse_identifier(C);
1231 if (op->a_id == SLANG_ATOM_NULL)
1232 return 0;
1233 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1234 return 0;
1235 break;
1236 case OP_POSTINCREMENT:
1237 op->type = SLANG_OPER_POSTINCREMENT;
1238 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1239 return 0;
1240 break;
1241 case OP_POSTDECREMENT:
1242 op->type = SLANG_OPER_POSTDECREMENT;
1243 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1244 return 0;
1245 break;
1246 default:
1247 return 0;
1248 }
1249 }
1250 C->I++;
1251
1252 *oper = *ops;
1253 slang_alloc_free(ops);
1254
1255 return 1;
1256 }
1257
1258 /* parameter qualifier */
1259 #define PARAM_QUALIFIER_IN 0
1260 #define PARAM_QUALIFIER_OUT 1
1261 #define PARAM_QUALIFIER_INOUT 2
1262
1263 /* function parameter array presence */
1264 #define PARAMETER_ARRAY_NOT_PRESENT 0
1265 #define PARAMETER_ARRAY_PRESENT 1
1266
1267 static int
1268 parse_parameter_declaration(slang_parse_ctx * C, slang_output_ctx * O,
1269 slang_variable * param)
1270 {
1271 /* parse and validate the parameter's type qualifiers (there can be
1272 * two at most) because not all combinations are valid
1273 */
1274 if (!parse_type_qualifier(C, &param->type.qualifier))
1275 return 0;
1276 switch (*C->I++) {
1277 case PARAM_QUALIFIER_IN:
1278 if (param->type.qualifier != SLANG_QUAL_CONST
1279 && param->type.qualifier != SLANG_QUAL_NONE) {
1280 slang_info_log_error(C->L, "Invalid type qualifier.");
1281 return 0;
1282 }
1283 break;
1284 case PARAM_QUALIFIER_OUT:
1285 if (param->type.qualifier == SLANG_QUAL_NONE)
1286 param->type.qualifier = SLANG_QUAL_OUT;
1287 else {
1288 slang_info_log_error(C->L, "Invalid type qualifier.");
1289 return 0;
1290 }
1291 break;
1292 case PARAM_QUALIFIER_INOUT:
1293 if (param->type.qualifier == SLANG_QUAL_NONE)
1294 param->type.qualifier = SLANG_QUAL_INOUT;
1295 else {
1296 slang_info_log_error(C->L, "Invalid type qualifier.");
1297 return 0;
1298 }
1299 break;
1300 default:
1301 return 0;
1302 }
1303
1304 /* parse parameter's type specifier and name */
1305 if (!parse_type_specifier(C, O, &param->type.specifier))
1306 return 0;
1307 param->a_name = parse_identifier(C);
1308 if (param->a_name == SLANG_ATOM_NULL)
1309 return 0;
1310
1311 /* if the parameter is an array, parse its size (the size must be
1312 * explicitly defined
1313 */
1314 if (*C->I++ == PARAMETER_ARRAY_PRESENT) {
1315 slang_type_specifier p;
1316
1317 slang_type_specifier_ctr(&p);
1318 if (!slang_type_specifier_copy(&p, &param->type.specifier)) {
1319 slang_type_specifier_dtr(&p);
1320 return GL_FALSE;
1321 }
1322 if (!convert_to_array(C, param, &p)) {
1323 slang_type_specifier_dtr(&p);
1324 return GL_FALSE;
1325 }
1326 slang_type_specifier_dtr(&p);
1327 if (!parse_array_len(C, O, &param->array_len))
1328 return GL_FALSE;
1329 }
1330
1331 /* calculate the parameter size */
1332 if (!calculate_var_size(C, O, param))
1333 return GL_FALSE;
1334
1335 /* TODO: allocate the local address here? */
1336 return 1;
1337 }
1338
1339 /* function type */
1340 #define FUNCTION_ORDINARY 0
1341 #define FUNCTION_CONSTRUCTOR 1
1342 #define FUNCTION_OPERATOR 2
1343
1344 /* function parameter */
1345 #define PARAMETER_NONE 0
1346 #define PARAMETER_NEXT 1
1347
1348 /* operator type */
1349 #define OPERATOR_ADDASSIGN 1
1350 #define OPERATOR_SUBASSIGN 2
1351 #define OPERATOR_MULASSIGN 3
1352 #define OPERATOR_DIVASSIGN 4
1353 /*#define OPERATOR_MODASSIGN 5*/
1354 /*#define OPERATOR_LSHASSIGN 6*/
1355 /*#define OPERATOR_RSHASSIGN 7*/
1356 /*#define OPERATOR_ANDASSIGN 8*/
1357 /*#define OPERATOR_XORASSIGN 9*/
1358 /*#define OPERATOR_ORASSIGN 10*/
1359 #define OPERATOR_LOGICALXOR 11
1360 /*#define OPERATOR_BITOR 12*/
1361 /*#define OPERATOR_BITXOR 13*/
1362 /*#define OPERATOR_BITAND 14*/
1363 #define OPERATOR_LESS 15
1364 #define OPERATOR_GREATER 16
1365 #define OPERATOR_LESSEQUAL 17
1366 #define OPERATOR_GREATEREQUAL 18
1367 /*#define OPERATOR_LSHIFT 19*/
1368 /*#define OPERATOR_RSHIFT 20*/
1369 #define OPERATOR_MULTIPLY 21
1370 #define OPERATOR_DIVIDE 22
1371 /*#define OPERATOR_MODULUS 23*/
1372 #define OPERATOR_INCREMENT 24
1373 #define OPERATOR_DECREMENT 25
1374 #define OPERATOR_PLUS 26
1375 #define OPERATOR_MINUS 27
1376 /*#define OPERATOR_COMPLEMENT 28*/
1377 #define OPERATOR_NOT 29
1378
1379 static const struct
1380 {
1381 unsigned int o_code;
1382 const char *o_name;
1383 } operator_names[] = {
1384 {OPERATOR_INCREMENT, "++"},
1385 {OPERATOR_ADDASSIGN, "+="},
1386 {OPERATOR_PLUS, "+"},
1387 {OPERATOR_DECREMENT, "--"},
1388 {OPERATOR_SUBASSIGN, "-="},
1389 {OPERATOR_MINUS, "-"},
1390 {OPERATOR_NOT, "!"},
1391 {OPERATOR_MULASSIGN, "*="},
1392 {OPERATOR_MULTIPLY, "*"},
1393 {OPERATOR_DIVASSIGN, "/="},
1394 {OPERATOR_DIVIDE, "/"},
1395 {OPERATOR_LESSEQUAL, "<="},
1396 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1397 /*{ OPERATOR_LSHIFT, "<<" }, */
1398 {OPERATOR_LESS, "<"},
1399 {OPERATOR_GREATEREQUAL, ">="},
1400 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1401 /*{ OPERATOR_RSHIFT, ">>" }, */
1402 {OPERATOR_GREATER, ">"},
1403 /*{ OPERATOR_MODASSIGN, "%=" }, */
1404 /*{ OPERATOR_MODULUS, "%" }, */
1405 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1406 /*{ OPERATOR_BITAND, "&" }, */
1407 /*{ OPERATOR_ORASSIGN, "|=" }, */
1408 /*{ OPERATOR_BITOR, "|" }, */
1409 /*{ OPERATOR_COMPLEMENT, "~" }, */
1410 /*{ OPERATOR_XORASSIGN, "^=" }, */
1411 {OPERATOR_LOGICALXOR, "^^"},
1412 /*{ OPERATOR_BITXOR, "^" } */
1413 };
1414
1415 static slang_atom
1416 parse_operator_name(slang_parse_ctx * C)
1417 {
1418 unsigned int i;
1419
1420 for (i = 0; i < sizeof(operator_names) / sizeof(*operator_names); i++) {
1421 if (operator_names[i].o_code == (unsigned int) (*C->I)) {
1422 slang_atom atom =
1423 slang_atom_pool_atom(C->atoms, operator_names[i].o_name);
1424 if (atom == SLANG_ATOM_NULL) {
1425 slang_info_log_memory(C->L);
1426 return 0;
1427 }
1428 C->I++;
1429 return atom;
1430 }
1431 }
1432 return 0;
1433 }
1434
1435 static int
1436 parse_function_prototype(slang_parse_ctx * C, slang_output_ctx * O,
1437 slang_function * func)
1438 {
1439 /* parse function type and name */
1440 if (!parse_fully_specified_type(C, O, &func->header.type))
1441 return 0;
1442 switch (*C->I++) {
1443 case FUNCTION_ORDINARY:
1444 func->kind = SLANG_FUNC_ORDINARY;
1445 func->header.a_name = parse_identifier(C);
1446 if (func->header.a_name == SLANG_ATOM_NULL)
1447 return 0;
1448 break;
1449 case FUNCTION_CONSTRUCTOR:
1450 func->kind = SLANG_FUNC_CONSTRUCTOR;
1451 if (func->header.type.specifier.type == SLANG_SPEC_STRUCT)
1452 return 0;
1453 func->header.a_name =
1454 slang_atom_pool_atom(C->atoms,
1455 slang_type_specifier_type_to_string
1456 (func->header.type.specifier.type));
1457 if (func->header.a_name == SLANG_ATOM_NULL) {
1458 slang_info_log_memory(C->L);
1459 return 0;
1460 }
1461 break;
1462 case FUNCTION_OPERATOR:
1463 func->kind = SLANG_FUNC_OPERATOR;
1464 func->header.a_name = parse_operator_name(C);
1465 if (func->header.a_name == SLANG_ATOM_NULL)
1466 return 0;
1467 break;
1468 default:
1469 return 0;
1470 }
1471
1472 /* parse function parameters */
1473 while (*C->I++ == PARAMETER_NEXT) {
1474 slang_variable *p = slang_variable_scope_grow(func->parameters);
1475 if (!p) {
1476 slang_info_log_memory(C->L);
1477 return 0;
1478 }
1479 if (!parse_parameter_declaration(C, O, p))
1480 return 0;
1481 }
1482
1483 /* if the function returns a value, append a hidden __retVal 'out'
1484 * parameter that corresponds to the return value.
1485 */
1486 if (_slang_function_has_return_value(func)) {
1487 slang_variable *p = slang_variable_scope_grow(func->parameters);
1488 slang_atom a_retVal = slang_atom_pool_atom(C->atoms, "__retVal");
1489 assert(a_retVal);
1490 p->a_name = a_retVal;
1491 p->type = func->header.type;
1492 p->type.qualifier = SLANG_QUAL_OUT;
1493 }
1494
1495 /* function formal parameters and local variables share the same
1496 * scope, so save the information about param count in a seperate
1497 * place also link the scope to the global variable scope so when a
1498 * given identifier is not found here, the search process continues
1499 * in the global space
1500 */
1501 func->param_count = func->parameters->num_variables;
1502 func->parameters->outer_scope = O->vars;
1503
1504 return 1;
1505 }
1506
1507 static int
1508 parse_function_definition(slang_parse_ctx * C, slang_output_ctx * O,
1509 slang_function * func)
1510 {
1511 slang_output_ctx o = *O;
1512
1513 if (!parse_function_prototype(C, O, func))
1514 return 0;
1515
1516 /* create function's body operation */
1517 func->body =
1518 (slang_operation *) slang_alloc_malloc(sizeof(slang_operation));
1519 if (func->body == NULL) {
1520 slang_info_log_memory(C->L);
1521 return 0;
1522 }
1523 if (!slang_operation_construct(func->body)) {
1524 slang_alloc_free(func->body);
1525 func->body = NULL;
1526 slang_info_log_memory(C->L);
1527 return 0;
1528 }
1529
1530 /* to parse the body the parse context is modified in order to
1531 * capture parsed variables into function's local variable scope
1532 */
1533 C->global_scope = GL_FALSE;
1534 o.vars = func->parameters;
1535 if (!parse_statement(C, &o, func->body))
1536 return 0;
1537
1538 C->global_scope = GL_TRUE;
1539 return 1;
1540 }
1541
1542 static GLboolean
1543 initialize_global(slang_assemble_ctx * A, slang_variable * var)
1544 {
1545 slang_operation op_id, op_assign;
1546 GLboolean result;
1547
1548 /* construct the left side of assignment */
1549 if (!slang_operation_construct(&op_id))
1550 return GL_FALSE;
1551 op_id.type = SLANG_OPER_IDENTIFIER;
1552 op_id.a_id = var->a_name;
1553
1554 /* put the variable into operation's scope */
1555 op_id.locals->variables =
1556 (slang_variable **) slang_alloc_malloc(sizeof(slang_variable *));
1557 if (op_id.locals->variables == NULL) {
1558 slang_operation_destruct(&op_id);
1559 return GL_FALSE;
1560 }
1561 op_id.locals->num_variables = 1;
1562 op_id.locals->variables[0] = var;
1563
1564 /* construct the assignment expression */
1565 if (!slang_operation_construct(&op_assign)) {
1566 op_id.locals->num_variables = 0;
1567 slang_operation_destruct(&op_id);
1568 return GL_FALSE;
1569 }
1570 op_assign.type = SLANG_OPER_ASSIGN;
1571 op_assign.children =
1572 (slang_operation *) slang_alloc_malloc(2 * sizeof(slang_operation));
1573 if (op_assign.children == NULL) {
1574 slang_operation_destruct(&op_assign);
1575 op_id.locals->num_variables = 0;
1576 slang_operation_destruct(&op_id);
1577 return GL_FALSE;
1578 }
1579 op_assign.num_children = 2;
1580 op_assign.children[0] = op_id;
1581 op_assign.children[1] = *var->initializer;
1582
1583 result = 1;
1584
1585 /* carefully destroy the operations */
1586 op_assign.num_children = 0;
1587 slang_alloc_free(op_assign.children);
1588 op_assign.children = NULL;
1589 slang_operation_destruct(&op_assign);
1590 op_id.locals->num_variables = 0;
1591 slang_operation_destruct(&op_id);
1592
1593 if (!result)
1594 return GL_FALSE;
1595
1596 return GL_TRUE;
1597 }
1598
1599 /* init declarator list */
1600 #define DECLARATOR_NONE 0
1601 #define DECLARATOR_NEXT 1
1602
1603 /* variable declaration */
1604 #define VARIABLE_NONE 0
1605 #define VARIABLE_IDENTIFIER 1
1606 #define VARIABLE_INITIALIZER 2
1607 #define VARIABLE_ARRAY_EXPLICIT 3
1608 #define VARIABLE_ARRAY_UNKNOWN 4
1609
1610
1611 /**
1612 * Parse the initializer for a variable declaration.
1613 */
1614 static int
1615 parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
1616 const slang_fully_specified_type * type)
1617 {
1618 slang_variable *var;
1619
1620 /* empty init declatator (without name, e.g. "float ;") */
1621 if (*C->I++ == VARIABLE_NONE)
1622 return 1;
1623
1624 /* make room for the new variable and initialize it */
1625 var = slang_variable_scope_grow(O->vars);
1626 if (!var) {
1627 slang_info_log_memory(C->L);
1628 return 0;
1629 }
1630
1631 /* copy the declarator qualifier type, parse the identifier */
1632 var->type.qualifier = type->qualifier;
1633 var->a_name = parse_identifier(C);
1634 if (var->a_name == SLANG_ATOM_NULL)
1635 return 0;
1636
1637 switch (*C->I++) {
1638 case VARIABLE_NONE:
1639 /* simple variable declarator - just copy the specifier */
1640 if (!slang_type_specifier_copy(&var->type.specifier, &type->specifier))
1641 return 0;
1642 break;
1643 case VARIABLE_INITIALIZER:
1644 /* initialized variable - copy the specifier and parse the expression */
1645 if (!slang_type_specifier_copy(&var->type.specifier, &type->specifier))
1646 return 0;
1647 var->initializer =
1648 (slang_operation *) slang_alloc_malloc(sizeof(slang_operation));
1649 if (var->initializer == NULL) {
1650 slang_info_log_memory(C->L);
1651 return 0;
1652 }
1653 if (!slang_operation_construct(var->initializer)) {
1654 slang_alloc_free(var->initializer);
1655 var->initializer = NULL;
1656 slang_info_log_memory(C->L);
1657 return 0;
1658 }
1659 if (!parse_expression(C, O, var->initializer))
1660 return 0;
1661 break;
1662 case VARIABLE_ARRAY_UNKNOWN:
1663 /* unsized array - mark it as array and copy the specifier to
1664 the array element
1665 */
1666 if (!convert_to_array(C, var, &type->specifier))
1667 return GL_FALSE;
1668 break;
1669 case VARIABLE_ARRAY_EXPLICIT:
1670 if (!convert_to_array(C, var, &type->specifier))
1671 return GL_FALSE;
1672 if (!parse_array_len(C, O, &var->array_len))
1673 return GL_FALSE;
1674 break;
1675 default:
1676 return 0;
1677 }
1678
1679 /* emit code for global var decl */
1680 if (C->global_scope) {
1681 slang_assemble_ctx A;
1682 A.atoms = C->atoms;
1683 A.space.funcs = O->funs;
1684 A.space.structs = O->structs;
1685 A.space.vars = O->vars;
1686 A.program = O->program;
1687 A.vartable = O->vartable;
1688 _slang_codegen_global_variable(&A, var, C->type);
1689 }
1690
1691 /* allocate global address space for a variable with a known size */
1692 if (C->global_scope
1693 && !(var->type.specifier.type == SLANG_SPEC_ARRAY
1694 && var->array_len == 0)) {
1695 if (!calculate_var_size(C, O, var))
1696 return GL_FALSE;
1697 var->address = slang_var_pool_alloc(O->global_pool, var->size);
1698 }
1699
1700 /* initialize global variable */
1701 if (C->global_scope) {
1702 if (var->initializer != NULL) {
1703 slang_assemble_ctx A;
1704
1705 A.atoms = C->atoms;
1706 A.space.funcs = O->funs;
1707 A.space.structs = O->structs;
1708 A.space.vars = O->vars;
1709 if (!initialize_global(&A, var))
1710 return 0;
1711 }
1712 }
1713 return 1;
1714 }
1715
1716 /**
1717 * Parse a list of variable declarations. Each variable may have an
1718 * initializer.
1719 */
1720 static int
1721 parse_init_declarator_list(slang_parse_ctx * C, slang_output_ctx * O)
1722 {
1723 slang_fully_specified_type type;
1724
1725 /* parse the fully specified type, common to all declarators */
1726 if (!slang_fully_specified_type_construct(&type))
1727 return 0;
1728 if (!parse_fully_specified_type(C, O, &type)) {
1729 slang_fully_specified_type_destruct(&type);
1730 return 0;
1731 }
1732
1733 /* parse declarators, pass-in the parsed type */
1734 do {
1735 if (!parse_init_declarator(C, O, &type)) {
1736 slang_fully_specified_type_destruct(&type);
1737 return 0;
1738 }
1739 }
1740 while (*C->I++ == DECLARATOR_NEXT);
1741
1742 slang_fully_specified_type_destruct(&type);
1743 return 1;
1744 }
1745
1746
1747 /**
1748 * Parse a function definition or declaration.
1749 * \param C parsing context
1750 * \param O output context
1751 * \param definition if non-zero expect a definition, else a declaration
1752 * \param parsed_func_ret returns the parsed function
1753 * \return GL_TRUE if success, GL_FALSE if failure
1754 */
1755 static GLboolean
1756 parse_function(slang_parse_ctx * C, slang_output_ctx * O, int definition,
1757 slang_function ** parsed_func_ret)
1758 {
1759 slang_function parsed_func, *found_func;
1760
1761 /* parse function definition/declaration */
1762 if (!slang_function_construct(&parsed_func))
1763 return GL_FALSE;
1764 if (definition) {
1765 if (!parse_function_definition(C, O, &parsed_func)) {
1766 slang_function_destruct(&parsed_func);
1767 return GL_FALSE;
1768 }
1769 }
1770 else {
1771 if (!parse_function_prototype(C, O, &parsed_func)) {
1772 slang_function_destruct(&parsed_func);
1773 return GL_FALSE;
1774 }
1775 }
1776
1777 /* find a function with a prototype matching the parsed one - only
1778 * the current scope is being searched to allow built-in function
1779 * overriding
1780 */
1781 found_func = slang_function_scope_find(O->funs, &parsed_func, 0);
1782 if (found_func == NULL) {
1783 /* New function, add it to the function list */
1784 O->funs->functions =
1785 (slang_function *) slang_alloc_realloc(O->funs->functions,
1786 O->funs->num_functions *
1787 sizeof(slang_function),
1788 (O->funs->num_functions +
1789 1) * sizeof(slang_function));
1790 if (O->funs->functions == NULL) {
1791 slang_info_log_memory(C->L);
1792 slang_function_destruct(&parsed_func);
1793 return GL_FALSE;
1794 }
1795 O->funs->functions[O->funs->num_functions] = parsed_func;
1796 O->funs->num_functions++;
1797
1798 /* return the newly parsed function */
1799 *parsed_func_ret = &O->funs->functions[O->funs->num_functions - 1];
1800 }
1801 else {
1802 /* previously defined or declared */
1803 /* TODO: check function return type qualifiers and specifiers */
1804 if (definition) {
1805 if (found_func->body != NULL) {
1806 slang_info_log_error(C->L, "%s: function already has a body.",
1807 slang_atom_pool_id(C->atoms,
1808 parsed_func.header.
1809 a_name));
1810 slang_function_destruct(&parsed_func);
1811 return GL_FALSE;
1812 }
1813
1814 /* destroy the existing function declaration and replace it
1815 * with the new one, remember to save the fixup table
1816 */
1817 parsed_func.fixups = found_func->fixups;
1818 slang_fixup_table_init(&found_func->fixups);
1819 slang_function_destruct(found_func);
1820 *found_func = parsed_func;
1821 }
1822 else {
1823 /* another declaration of the same function prototype - ignore it */
1824 slang_function_destruct(&parsed_func);
1825 }
1826
1827 /* return the found function */
1828 *parsed_func_ret = found_func;
1829 }
1830
1831 /* assemble the parsed function */
1832 {
1833 slang_assemble_ctx A;
1834
1835 A.atoms = C->atoms;
1836 A.space.funcs = O->funs;
1837 A.space.structs = O->structs;
1838 A.space.vars = O->vars;
1839 A.program = O->program;
1840 A.vartable = O->vartable;
1841
1842 _slang_reset_error();
1843
1844 _slang_codegen_function(&A, *parsed_func_ret);
1845 }
1846 return GL_TRUE;
1847 }
1848
1849 /* declaration */
1850 #define DECLARATION_FUNCTION_PROTOTYPE 1
1851 #define DECLARATION_INIT_DECLARATOR_LIST 2
1852
1853 static int
1854 parse_declaration(slang_parse_ctx * C, slang_output_ctx * O)
1855 {
1856 switch (*C->I++) {
1857 case DECLARATION_INIT_DECLARATOR_LIST:
1858 if (!parse_init_declarator_list(C, O))
1859 return 0;
1860 break;
1861 case DECLARATION_FUNCTION_PROTOTYPE:
1862 {
1863 slang_function *dummy_func;
1864
1865 if (!parse_function(C, O, 0, &dummy_func))
1866 return 0;
1867 }
1868 break;
1869 default:
1870 return 0;
1871 }
1872 return 1;
1873 }
1874
1875 /* external declaration */
1876 #define EXTERNAL_NULL 0
1877 #define EXTERNAL_FUNCTION_DEFINITION 1
1878 #define EXTERNAL_DECLARATION 2
1879
1880 static GLboolean
1881 parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
1882 struct gl_program *program)
1883 {
1884 GET_CURRENT_CONTEXT(ctx);
1885 slang_output_ctx o;
1886 GLboolean success;
1887 GLuint maxRegs;
1888
1889 if (unit->type == SLANG_UNIT_FRAGMENT_BUILTIN ||
1890 unit->type == SLANG_UNIT_FRAGMENT_SHADER) {
1891 maxRegs = ctx->Const.FragmentProgram.MaxTemps;
1892 }
1893 else {
1894 assert(unit->type == SLANG_UNIT_VERTEX_BUILTIN ||
1895 unit->type == SLANG_UNIT_VERTEX_SHADER);
1896 maxRegs = ctx->Const.VertexProgram.MaxTemps;
1897 }
1898
1899 /* setup output context */
1900 o.funs = &unit->funs;
1901 o.structs = &unit->structs;
1902 o.vars = &unit->vars;
1903 o.global_pool = &unit->object->varpool;
1904 o.program = program;
1905 o.vartable = _slang_new_var_table(maxRegs);
1906 _slang_push_var_table(o.vartable);
1907
1908 /* parse individual functions and declarations */
1909 while (*C->I != EXTERNAL_NULL) {
1910 switch (*C->I++) {
1911 case EXTERNAL_FUNCTION_DEFINITION:
1912 {
1913 slang_function *func;
1914 success = parse_function(C, &o, 1, &func);
1915 }
1916 break;
1917 case EXTERNAL_DECLARATION:
1918 success = parse_declaration(C, &o);
1919 break;
1920 default:
1921 success = GL_FALSE;
1922 }
1923
1924 if (!success) {
1925 /* xxx free codegen */
1926 _slang_pop_var_table(o.vartable);
1927 return GL_FALSE;
1928 }
1929 }
1930 C->I++;
1931
1932 _slang_pop_var_table(o.vartable);
1933 return GL_TRUE;
1934 }
1935
1936 static GLboolean
1937 compile_binary(const byte * prod, slang_code_unit * unit,
1938 slang_unit_type type, slang_info_log * infolog,
1939 slang_code_unit * builtin, slang_code_unit * downlink,
1940 struct gl_program *program)
1941 {
1942 slang_parse_ctx C;
1943
1944 unit->type = type;
1945
1946 /* setup parse context */
1947 C.I = prod;
1948 C.L = infolog;
1949 C.parsing_builtin = (builtin == NULL);
1950 C.global_scope = GL_TRUE;
1951 C.atoms = &unit->object->atompool;
1952 C.type = type;
1953
1954 if (!check_revision(&C))
1955 return GL_FALSE;
1956
1957 if (downlink != NULL) {
1958 unit->vars.outer_scope = &downlink->vars;
1959 unit->funs.outer_scope = &downlink->funs;
1960 unit->structs.outer_scope = &downlink->structs;
1961 }
1962
1963 /* parse translation unit */
1964 return parse_code_unit(&C, unit, program);
1965 }
1966
1967 static GLboolean
1968 compile_with_grammar(grammar id, const char *source, slang_code_unit * unit,
1969 slang_unit_type type, slang_info_log * infolog,
1970 slang_code_unit * builtin,
1971 struct gl_program *program)
1972 {
1973 byte *prod;
1974 GLuint size, start, version;
1975 slang_string preprocessed;
1976
1977 /* First retrieve the version number. */
1978 if (!_slang_preprocess_version(source, &version, &start, infolog))
1979 return GL_FALSE;
1980
1981 if (version > 110) {
1982 slang_info_log_error(infolog,
1983 "language version specified is not supported.");
1984 return GL_FALSE;
1985 }
1986
1987 /* Now preprocess the source string. */
1988 slang_string_init(&preprocessed);
1989 if (!_slang_preprocess_directives(&preprocessed, &source[start], infolog)) {
1990 slang_string_free(&preprocessed);
1991 slang_info_log_error(infolog, "failed to preprocess the source.");
1992 return GL_FALSE;
1993 }
1994
1995 /* Finally check the syntax and generate its binary representation. */
1996 if (!grammar_fast_check(id,
1997 (const byte *) (slang_string_cstr(&preprocessed)),
1998 &prod, &size, 65536)) {
1999 char buf[1024];
2000 GLint pos;
2001
2002 slang_string_free(&preprocessed);
2003 grammar_get_last_error((byte *) (buf), sizeof(buf), &pos);
2004 slang_info_log_error(infolog, buf);
2005 RETURN_ERROR("syntax error (possibly in library code)", 0);
2006 }
2007 slang_string_free(&preprocessed);
2008
2009 /* Syntax is okay - translate it to internal representation. */
2010 if (!compile_binary(prod, unit, type, infolog, builtin,
2011 &builtin[SLANG_BUILTIN_TOTAL - 1],
2012 program)) {
2013 grammar_alloc_free(prod);
2014 return GL_FALSE;
2015 }
2016 grammar_alloc_free(prod);
2017 return GL_TRUE;
2018 }
2019
2020 LONGSTRING static const char *slang_shader_syn =
2021 #include "library/slang_shader_syn.h"
2022 ;
2023
2024 static const byte slang_core_gc[] = {
2025 #include "library/slang_core_gc.h"
2026 };
2027
2028 static const byte slang_common_builtin_gc[] = {
2029 #include "library/slang_common_builtin_gc.h"
2030 };
2031
2032 static const byte slang_fragment_builtin_gc[] = {
2033 #include "library/slang_fragment_builtin_gc.h"
2034 };
2035
2036 static const byte slang_vertex_builtin_gc[] = {
2037 #include "library/slang_vertex_builtin_gc.h"
2038 };
2039
2040 static GLboolean
2041 compile_object(grammar * id, const char *source, slang_code_object * object,
2042 slang_unit_type type, slang_info_log * infolog,
2043 struct gl_program *program)
2044 {
2045 slang_code_unit *builtins = NULL;
2046
2047 /* load GLSL grammar */
2048 *id = grammar_load_from_text((const byte *) (slang_shader_syn));
2049 if (*id == 0) {
2050 byte buf[1024];
2051 int pos;
2052
2053 grammar_get_last_error(buf, 1024, &pos);
2054 slang_info_log_error(infolog, (const char *) (buf));
2055 return GL_FALSE;
2056 }
2057
2058 /* set shader type - the syntax is slightly different for different shaders */
2059 if (type == SLANG_UNIT_FRAGMENT_SHADER
2060 || type == SLANG_UNIT_FRAGMENT_BUILTIN)
2061 grammar_set_reg8(*id, (const byte *) "shader_type", 1);
2062 else
2063 grammar_set_reg8(*id, (const byte *) "shader_type", 2);
2064
2065 /* enable language extensions */
2066 grammar_set_reg8(*id, (const byte *) "parsing_builtin", 1);
2067
2068 /* if parsing user-specified shader, load built-in library */
2069 if (type == SLANG_UNIT_FRAGMENT_SHADER || type == SLANG_UNIT_VERTEX_SHADER) {
2070 /* compile core functionality first */
2071 if (!compile_binary(slang_core_gc,
2072 &object->builtin[SLANG_BUILTIN_CORE],
2073 SLANG_UNIT_FRAGMENT_BUILTIN, infolog,
2074 NULL, NULL, NULL))
2075 return GL_FALSE;
2076
2077 /* compile common functions and variables, link to core */
2078 if (!compile_binary(slang_common_builtin_gc,
2079 &object->builtin[SLANG_BUILTIN_COMMON],
2080 SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
2081 &object->builtin[SLANG_BUILTIN_CORE], NULL))
2082 return GL_FALSE;
2083
2084 /* compile target-specific functions and variables, link to common */
2085 if (type == SLANG_UNIT_FRAGMENT_SHADER) {
2086 if (!compile_binary(slang_fragment_builtin_gc,
2087 &object->builtin[SLANG_BUILTIN_TARGET],
2088 SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
2089 &object->builtin[SLANG_BUILTIN_COMMON], NULL))
2090 return GL_FALSE;
2091 }
2092 else if (type == SLANG_UNIT_VERTEX_SHADER) {
2093 if (!compile_binary(slang_vertex_builtin_gc,
2094 &object->builtin[SLANG_BUILTIN_TARGET],
2095 SLANG_UNIT_VERTEX_BUILTIN, infolog, NULL,
2096 &object->builtin[SLANG_BUILTIN_COMMON], NULL))
2097 return GL_FALSE;
2098 }
2099
2100 /* disable language extensions */
2101 #if NEW_SLANG /* allow-built-ins */
2102 grammar_set_reg8(*id, (const byte *) "parsing_builtin", 1);
2103 #else
2104 grammar_set_reg8(*id, (const byte *) "parsing_builtin", 0);
2105 #endif
2106 builtins = object->builtin;
2107 }
2108
2109 /* compile the actual shader - pass-in built-in library for external shader */
2110 return compile_with_grammar(*id, source, &object->unit, type, infolog,
2111 builtins, program);
2112 }
2113
2114
2115 static GLboolean
2116 compile_shader(GLcontext *ctx, slang_code_object * object,
2117 slang_unit_type type, slang_info_log * infolog,
2118 struct gl_shader *shader)
2119 {
2120 struct gl_program *program = shader->Programs[0];
2121 GLboolean success;
2122 grammar id = 0;
2123
2124 assert(program);
2125
2126 _slang_code_object_dtr(object);
2127 _slang_code_object_ctr(object);
2128
2129 success = compile_object(&id, shader->Source, object, type, infolog, program);
2130 if (id != 0)
2131 grammar_destroy(id);
2132 if (!success)
2133 return GL_FALSE;
2134
2135 return GL_TRUE;
2136 }
2137
2138
2139
2140 GLboolean
2141 _slang_compile(GLcontext *ctx, struct gl_shader *shader)
2142 {
2143 GLboolean success;
2144 slang_info_log info_log;
2145 slang_code_object obj;
2146 slang_unit_type type;
2147
2148 if (shader->Type == GL_VERTEX_SHADER) {
2149 type = SLANG_UNIT_VERTEX_SHADER;
2150 }
2151 else {
2152 assert(shader->Type == GL_FRAGMENT_SHADER);
2153 type = SLANG_UNIT_FRAGMENT_SHADER;
2154 }
2155
2156 /* XXX temporary hack */
2157 if (!shader->Programs) {
2158 GLenum progTarget;
2159 if (shader->Type == GL_VERTEX_SHADER)
2160 progTarget = GL_VERTEX_PROGRAM_ARB;
2161 else
2162 progTarget = GL_FRAGMENT_PROGRAM_ARB;
2163 shader->Programs
2164 = (struct gl_program **) malloc(sizeof(struct gl_program*));
2165 shader->Programs[0] = _mesa_new_program(ctx, progTarget, 1);
2166 shader->NumPrograms = 1;
2167
2168 shader->Programs[0]->Parameters = _mesa_new_parameter_list();
2169 shader->Programs[0]->Varying = _mesa_new_parameter_list();
2170 shader->Programs[0]->Attributes = _mesa_new_parameter_list();
2171 }
2172
2173 slang_info_log_construct(&info_log);
2174 _slang_code_object_ctr(&obj);
2175
2176 success = compile_shader(ctx, &obj, type, &info_log, shader);
2177
2178 if (success) {
2179 #if 0
2180 slang_create_uniforms(&object->expdata, shader);
2181 _mesa_print_program(program);
2182 _mesa_print_program_parameters(ctx, program);
2183 #endif
2184 }
2185 else {
2186 /* XXX more work on info log needed here */
2187 if (info_log.text) {
2188 if (shader->InfoLog) {
2189 free(shader->InfoLog);
2190 shader->InfoLog = NULL;
2191 }
2192 shader->InfoLog = strdup(info_log.text);
2193 }
2194 }
2195
2196 slang_info_log_destruct(&info_log);
2197 _slang_code_object_dtr(&obj);
2198
2199 return success;
2200 }
2201