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