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