Merge commit 'origin/gallium-0.1' into gallium-0.2
[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 #define OP_METHOD 63
948
949
950 /**
951 * When parsing a compound production, this function is used to parse the
952 * children.
953 * For example, a while-loop compound will have two children, the
954 * while condition expression and the loop body. So, this function will
955 * be called twice to parse those two sub-expressions.
956 * \param C the parsing context
957 * \param O the output context
958 * \param oper the operation we're parsing
959 * \param statement indicates whether parsing a statement, or expression
960 * \return 1 if success, 0 if error
961 */
962 static int
963 parse_child_operation(slang_parse_ctx * C, slang_output_ctx * O,
964 slang_operation * oper, GLboolean statement)
965 {
966 slang_operation *ch;
967
968 /* grow child array */
969 ch = slang_operation_grow(&oper->num_children, &oper->children);
970 if (statement)
971 return parse_statement(C, O, ch);
972 return parse_expression(C, O, ch);
973 }
974
975 static int parse_declaration(slang_parse_ctx * C, slang_output_ctx * O);
976
977 static int
978 parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
979 slang_operation * oper)
980 {
981 oper->locals->outer_scope = O->vars;
982 switch (*C->I++) {
983 case OP_BLOCK_BEGIN_NO_NEW_SCOPE:
984 /* parse child statements, do not create new variable scope */
985 oper->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE;
986 while (*C->I != OP_END)
987 if (!parse_child_operation(C, O, oper, 1))
988 RETURN0;
989 C->I++;
990 break;
991 case OP_BLOCK_BEGIN_NEW_SCOPE:
992 /* parse child statements, create new variable scope */
993 {
994 slang_output_ctx o = *O;
995
996 oper->type = SLANG_OPER_BLOCK_NEW_SCOPE;
997 o.vars = oper->locals;
998 while (*C->I != OP_END)
999 if (!parse_child_operation(C, &o, oper, 1))
1000 RETURN0;
1001 C->I++;
1002 }
1003 break;
1004 case OP_DECLARE:
1005 /* local variable declaration, individual declarators are stored as
1006 * children identifiers
1007 */
1008 oper->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE;
1009 {
1010 const unsigned int first_var = O->vars->num_variables;
1011
1012 /* parse the declaration, note that there can be zero or more
1013 * than one declarators
1014 */
1015 if (!parse_declaration(C, O))
1016 RETURN0;
1017 if (first_var < O->vars->num_variables) {
1018 const unsigned int num_vars = O->vars->num_variables - first_var;
1019 unsigned int i;
1020 assert(oper->num_children == 0);
1021 oper->num_children = num_vars;
1022 oper->children = slang_operation_new(num_vars);
1023 if (oper->children == NULL) {
1024 slang_info_log_memory(C->L);
1025 RETURN0;
1026 }
1027 for (i = first_var; i < O->vars->num_variables; i++) {
1028 slang_operation *o = &oper->children[i - first_var];
1029 slang_variable *var = O->vars->variables[i];
1030 o->type = SLANG_OPER_VARIABLE_DECL;
1031 o->locals->outer_scope = O->vars;
1032 o->a_id = var->a_name;
1033
1034 /* new/someday...
1035 calculate_var_size(C, O, var);
1036 */
1037
1038 if (!legal_identifier(o->a_id)) {
1039 slang_info_log_error(C->L, "illegal variable name '%s'",
1040 (char *) o->a_id);
1041 RETURN0;
1042 }
1043 }
1044 }
1045 }
1046 break;
1047 case OP_ASM:
1048 /* the __asm statement, parse the mnemonic and all its arguments
1049 * as expressions
1050 */
1051 oper->type = SLANG_OPER_ASM;
1052 oper->a_id = parse_identifier(C);
1053 if (oper->a_id == SLANG_ATOM_NULL)
1054 RETURN0;
1055 while (*C->I != OP_END) {
1056 if (!parse_child_operation(C, O, oper, 0))
1057 RETURN0;
1058 }
1059 C->I++;
1060 break;
1061 case OP_BREAK:
1062 oper->type = SLANG_OPER_BREAK;
1063 break;
1064 case OP_CONTINUE:
1065 oper->type = SLANG_OPER_CONTINUE;
1066 break;
1067 case OP_DISCARD:
1068 oper->type = SLANG_OPER_DISCARD;
1069 break;
1070 case OP_RETURN:
1071 oper->type = SLANG_OPER_RETURN;
1072 if (!parse_child_operation(C, O, oper, 0))
1073 RETURN0;
1074 break;
1075 case OP_EXPRESSION:
1076 oper->type = SLANG_OPER_EXPRESSION;
1077 if (!parse_child_operation(C, O, oper, 0))
1078 RETURN0;
1079 break;
1080 case OP_IF:
1081 oper->type = SLANG_OPER_IF;
1082 if (!parse_child_operation(C, O, oper, 0))
1083 RETURN0;
1084 if (!parse_child_operation(C, O, oper, 1))
1085 RETURN0;
1086 if (!parse_child_operation(C, O, oper, 1))
1087 RETURN0;
1088 break;
1089 case OP_WHILE:
1090 {
1091 slang_output_ctx o = *O;
1092
1093 oper->type = SLANG_OPER_WHILE;
1094 o.vars = oper->locals;
1095 if (!parse_child_operation(C, &o, oper, 1))
1096 RETURN0;
1097 if (!parse_child_operation(C, &o, oper, 1))
1098 RETURN0;
1099 }
1100 break;
1101 case OP_DO:
1102 oper->type = SLANG_OPER_DO;
1103 if (!parse_child_operation(C, O, oper, 1))
1104 RETURN0;
1105 if (!parse_child_operation(C, O, oper, 0))
1106 RETURN0;
1107 break;
1108 case OP_FOR:
1109 {
1110 slang_output_ctx o = *O;
1111
1112 oper->type = SLANG_OPER_FOR;
1113 o.vars = oper->locals;
1114 if (!parse_child_operation(C, &o, oper, 1))
1115 RETURN0;
1116 if (!parse_child_operation(C, &o, oper, 1))
1117 RETURN0;
1118 if (!parse_child_operation(C, &o, oper, 0))
1119 RETURN0;
1120 if (!parse_child_operation(C, &o, oper, 1))
1121 RETURN0;
1122 }
1123 break;
1124 case OP_PRECISION:
1125 {
1126 /* set default precision for a type in this scope */
1127 /* ignored at this time */
1128 int prec_qual = *C->I++;
1129 int datatype = *C->I++;
1130 (void) prec_qual;
1131 (void) datatype;
1132 }
1133 break;
1134 default:
1135 RETURN0;
1136 }
1137 return 1;
1138 }
1139
1140 static int
1141 handle_nary_expression(slang_parse_ctx * C, slang_operation * op,
1142 slang_operation ** ops, unsigned int *total_ops,
1143 unsigned int n)
1144 {
1145 unsigned int i;
1146
1147 op->children = slang_operation_new(n);
1148 if (op->children == NULL) {
1149 slang_info_log_memory(C->L);
1150 RETURN0;
1151 }
1152 op->num_children = n;
1153
1154 for (i = 0; i < n; i++) {
1155 slang_operation_destruct(&op->children[i]);
1156 op->children[i] = (*ops)[*total_ops - (n + 1 - i)];
1157 }
1158
1159 (*ops)[*total_ops - (n + 1)] = (*ops)[*total_ops - 1];
1160 *total_ops -= n;
1161
1162 *ops = (slang_operation *)
1163 _slang_realloc(*ops,
1164 (*total_ops + n) * sizeof(slang_operation),
1165 *total_ops * sizeof(slang_operation));
1166 if (*ops == NULL) {
1167 slang_info_log_memory(C->L);
1168 RETURN0;
1169 }
1170 return 1;
1171 }
1172
1173 static int
1174 is_constructor_name(const char *name, slang_atom a_name,
1175 slang_struct_scope * structs)
1176 {
1177 if (slang_type_specifier_type_from_string(name) != SLANG_SPEC_VOID)
1178 return 1;
1179 return slang_struct_scope_find(structs, a_name, 1) != NULL;
1180 }
1181
1182 static int
1183 parse_expression(slang_parse_ctx * C, slang_output_ctx * O,
1184 slang_operation * oper)
1185 {
1186 slang_operation *ops = NULL;
1187 unsigned int num_ops = 0;
1188 int number;
1189
1190 while (*C->I != OP_END) {
1191 slang_operation *op;
1192 const unsigned int op_code = *C->I++;
1193
1194 /* allocate default operation, becomes a no-op if not used */
1195 ops = (slang_operation *)
1196 _slang_realloc(ops,
1197 num_ops * sizeof(slang_operation),
1198 (num_ops + 1) * sizeof(slang_operation));
1199 if (ops == NULL) {
1200 slang_info_log_memory(C->L);
1201 RETURN0;
1202 }
1203 op = &ops[num_ops];
1204 if (!slang_operation_construct(op)) {
1205 slang_info_log_memory(C->L);
1206 RETURN0;
1207 }
1208 num_ops++;
1209 op->locals->outer_scope = O->vars;
1210
1211 switch (op_code) {
1212 case OP_PUSH_VOID:
1213 op->type = SLANG_OPER_VOID;
1214 break;
1215 case OP_PUSH_BOOL:
1216 op->type = SLANG_OPER_LITERAL_BOOL;
1217 if (!parse_number(C, &number))
1218 RETURN0;
1219 op->literal[0] =
1220 op->literal[1] =
1221 op->literal[2] =
1222 op->literal[3] = (GLfloat) number;
1223 op->literal_size = 1;
1224 break;
1225 case OP_PUSH_INT:
1226 op->type = SLANG_OPER_LITERAL_INT;
1227 if (!parse_number(C, &number))
1228 RETURN0;
1229 op->literal[0] =
1230 op->literal[1] =
1231 op->literal[2] =
1232 op->literal[3] = (GLfloat) number;
1233 op->literal_size = 1;
1234 break;
1235 case OP_PUSH_FLOAT:
1236 op->type = SLANG_OPER_LITERAL_FLOAT;
1237 if (!parse_float(C, &op->literal[0]))
1238 RETURN0;
1239 op->literal[1] =
1240 op->literal[2] =
1241 op->literal[3] = op->literal[0];
1242 op->literal_size = 1;
1243 break;
1244 case OP_PUSH_IDENTIFIER:
1245 op->type = SLANG_OPER_IDENTIFIER;
1246 op->a_id = parse_identifier(C);
1247 if (op->a_id == SLANG_ATOM_NULL)
1248 RETURN0;
1249 break;
1250 case OP_SEQUENCE:
1251 op->type = SLANG_OPER_SEQUENCE;
1252 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1253 RETURN0;
1254 break;
1255 case OP_ASSIGN:
1256 op->type = SLANG_OPER_ASSIGN;
1257 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1258 RETURN0;
1259 break;
1260 case OP_ADDASSIGN:
1261 op->type = SLANG_OPER_ADDASSIGN;
1262 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1263 RETURN0;
1264 break;
1265 case OP_SUBASSIGN:
1266 op->type = SLANG_OPER_SUBASSIGN;
1267 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1268 RETURN0;
1269 break;
1270 case OP_MULASSIGN:
1271 op->type = SLANG_OPER_MULASSIGN;
1272 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1273 RETURN0;
1274 break;
1275 case OP_DIVASSIGN:
1276 op->type = SLANG_OPER_DIVASSIGN;
1277 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1278 RETURN0;
1279 break;
1280 /*case OP_MODASSIGN: */
1281 /*case OP_LSHASSIGN: */
1282 /*case OP_RSHASSIGN: */
1283 /*case OP_ORASSIGN: */
1284 /*case OP_XORASSIGN: */
1285 /*case OP_ANDASSIGN: */
1286 case OP_SELECT:
1287 op->type = SLANG_OPER_SELECT;
1288 if (!handle_nary_expression(C, op, &ops, &num_ops, 3))
1289 RETURN0;
1290 break;
1291 case OP_LOGICALOR:
1292 op->type = SLANG_OPER_LOGICALOR;
1293 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1294 RETURN0;
1295 break;
1296 case OP_LOGICALXOR:
1297 op->type = SLANG_OPER_LOGICALXOR;
1298 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1299 RETURN0;
1300 break;
1301 case OP_LOGICALAND:
1302 op->type = SLANG_OPER_LOGICALAND;
1303 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1304 RETURN0;
1305 break;
1306 /*case OP_BITOR: */
1307 /*case OP_BITXOR: */
1308 /*case OP_BITAND: */
1309 case OP_EQUAL:
1310 op->type = SLANG_OPER_EQUAL;
1311 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1312 RETURN0;
1313 break;
1314 case OP_NOTEQUAL:
1315 op->type = SLANG_OPER_NOTEQUAL;
1316 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1317 RETURN0;
1318 break;
1319 case OP_LESS:
1320 op->type = SLANG_OPER_LESS;
1321 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1322 RETURN0;
1323 break;
1324 case OP_GREATER:
1325 op->type = SLANG_OPER_GREATER;
1326 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1327 RETURN0;
1328 break;
1329 case OP_LESSEQUAL:
1330 op->type = SLANG_OPER_LESSEQUAL;
1331 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1332 RETURN0;
1333 break;
1334 case OP_GREATEREQUAL:
1335 op->type = SLANG_OPER_GREATEREQUAL;
1336 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1337 RETURN0;
1338 break;
1339 /*case OP_LSHIFT: */
1340 /*case OP_RSHIFT: */
1341 case OP_ADD:
1342 op->type = SLANG_OPER_ADD;
1343 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1344 RETURN0;
1345 break;
1346 case OP_SUBTRACT:
1347 op->type = SLANG_OPER_SUBTRACT;
1348 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1349 RETURN0;
1350 break;
1351 case OP_MULTIPLY:
1352 op->type = SLANG_OPER_MULTIPLY;
1353 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1354 RETURN0;
1355 break;
1356 case OP_DIVIDE:
1357 op->type = SLANG_OPER_DIVIDE;
1358 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1359 RETURN0;
1360 break;
1361 /*case OP_MODULUS: */
1362 case OP_PREINCREMENT:
1363 op->type = SLANG_OPER_PREINCREMENT;
1364 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1365 RETURN0;
1366 break;
1367 case OP_PREDECREMENT:
1368 op->type = SLANG_OPER_PREDECREMENT;
1369 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1370 RETURN0;
1371 break;
1372 case OP_PLUS:
1373 op->type = SLANG_OPER_PLUS;
1374 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1375 RETURN0;
1376 break;
1377 case OP_MINUS:
1378 op->type = SLANG_OPER_MINUS;
1379 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1380 RETURN0;
1381 break;
1382 case OP_NOT:
1383 op->type = SLANG_OPER_NOT;
1384 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1385 RETURN0;
1386 break;
1387 /*case OP_COMPLEMENT: */
1388 case OP_SUBSCRIPT:
1389 op->type = SLANG_OPER_SUBSCRIPT;
1390 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1391 RETURN0;
1392 break;
1393 case OP_METHOD:
1394 op->type = SLANG_OPER_METHOD;
1395 op->a_obj = parse_identifier(C);
1396 if (op->a_obj == SLANG_ATOM_NULL)
1397 RETURN0;
1398
1399 op->a_id = parse_identifier(C);
1400 if (op->a_id == SLANG_ATOM_NULL)
1401 RETURN0;
1402
1403 while (*C->I != OP_END)
1404 if (!parse_child_operation(C, O, op, 0))
1405 RETURN0;
1406 C->I++;
1407 #if 0
1408 /* don't lookup the method (not yet anyway) */
1409 if (!C->parsing_builtin
1410 && !slang_function_scope_find_by_name(O->funs, op->a_id, 1)) {
1411 const char *id;
1412
1413 id = slang_atom_pool_id(C->atoms, op->a_id);
1414 if (!is_constructor_name(id, op->a_id, O->structs)) {
1415 slang_info_log_error(C->L, "%s: undeclared function name.", id);
1416 RETURN0;
1417 }
1418 }
1419 #endif
1420 break;
1421 case OP_CALL:
1422 op->type = SLANG_OPER_CALL;
1423 op->a_id = parse_identifier(C);
1424 if (op->a_id == SLANG_ATOM_NULL)
1425 RETURN0;
1426 while (*C->I != OP_END)
1427 if (!parse_child_operation(C, O, op, 0))
1428 RETURN0;
1429 C->I++;
1430
1431 if (!C->parsing_builtin
1432 && !slang_function_scope_find_by_name(O->funs, op->a_id, 1)) {
1433 const char *id;
1434
1435 id = slang_atom_pool_id(C->atoms, op->a_id);
1436 if (!is_constructor_name(id, op->a_id, O->structs)) {
1437 slang_info_log_error(C->L, "%s: undeclared function name.", id);
1438 RETURN0;
1439 }
1440 }
1441 break;
1442 case OP_FIELD:
1443 op->type = SLANG_OPER_FIELD;
1444 op->a_id = parse_identifier(C);
1445 if (op->a_id == SLANG_ATOM_NULL)
1446 RETURN0;
1447 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1448 RETURN0;
1449 break;
1450 case OP_POSTINCREMENT:
1451 op->type = SLANG_OPER_POSTINCREMENT;
1452 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1453 RETURN0;
1454 break;
1455 case OP_POSTDECREMENT:
1456 op->type = SLANG_OPER_POSTDECREMENT;
1457 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1458 RETURN0;
1459 break;
1460 default:
1461 RETURN0;
1462 }
1463 }
1464 C->I++;
1465
1466 slang_operation_destruct(oper);
1467 *oper = *ops; /* struct copy */
1468 _slang_free(ops);
1469
1470 return 1;
1471 }
1472
1473 /* parameter qualifier */
1474 #define PARAM_QUALIFIER_IN 0
1475 #define PARAM_QUALIFIER_OUT 1
1476 #define PARAM_QUALIFIER_INOUT 2
1477
1478 /* function parameter array presence */
1479 #define PARAMETER_ARRAY_NOT_PRESENT 0
1480 #define PARAMETER_ARRAY_PRESENT 1
1481
1482 static int
1483 parse_parameter_declaration(slang_parse_ctx * C, slang_output_ctx * O,
1484 slang_variable * param)
1485 {
1486 int param_qual, precision_qual;
1487
1488 /* parse and validate the parameter's type qualifiers (there can be
1489 * two at most) because not all combinations are valid
1490 */
1491 if (!parse_type_qualifier(C, &param->type.qualifier))
1492 RETURN0;
1493
1494 param_qual = *C->I++;
1495 switch (param_qual) {
1496 case PARAM_QUALIFIER_IN:
1497 if (param->type.qualifier != SLANG_QUAL_CONST
1498 && param->type.qualifier != SLANG_QUAL_NONE) {
1499 slang_info_log_error(C->L, "Invalid type qualifier.");
1500 RETURN0;
1501 }
1502 break;
1503 case PARAM_QUALIFIER_OUT:
1504 if (param->type.qualifier == SLANG_QUAL_NONE)
1505 param->type.qualifier = SLANG_QUAL_OUT;
1506 else {
1507 slang_info_log_error(C->L, "Invalid type qualifier.");
1508 RETURN0;
1509 }
1510 break;
1511 case PARAM_QUALIFIER_INOUT:
1512 if (param->type.qualifier == SLANG_QUAL_NONE)
1513 param->type.qualifier = SLANG_QUAL_INOUT;
1514 else {
1515 slang_info_log_error(C->L, "Invalid type qualifier.");
1516 RETURN0;
1517 }
1518 break;
1519 default:
1520 RETURN0;
1521 }
1522
1523 /* parse precision qualifier (lowp, mediump, highp */
1524 precision_qual = *C->I++;
1525 /* ignored at this time */
1526 (void) precision_qual;
1527
1528 /* parse parameter's type specifier and name */
1529 if (!parse_type_specifier(C, O, &param->type.specifier))
1530 RETURN0;
1531 param->a_name = parse_identifier(C);
1532 if (param->a_name == SLANG_ATOM_NULL)
1533 RETURN0;
1534
1535 /* if the parameter is an array, parse its size (the size must be
1536 * explicitly defined
1537 */
1538 if (*C->I++ == PARAMETER_ARRAY_PRESENT) {
1539 slang_type_specifier p;
1540
1541 slang_type_specifier_ctr(&p);
1542 if (!slang_type_specifier_copy(&p, &param->type.specifier)) {
1543 slang_type_specifier_dtr(&p);
1544 return GL_FALSE;
1545 }
1546 if (!convert_to_array(C, param, &p)) {
1547 slang_type_specifier_dtr(&p);
1548 return GL_FALSE;
1549 }
1550 slang_type_specifier_dtr(&p);
1551 if (!parse_array_len(C, O, &param->array_len))
1552 return GL_FALSE;
1553 }
1554
1555 /* calculate the parameter size */
1556 if (!calculate_var_size(C, O, param))
1557 return GL_FALSE;
1558
1559 /* TODO: allocate the local address here? */
1560 return 1;
1561 }
1562
1563 /* function type */
1564 #define FUNCTION_ORDINARY 0
1565 #define FUNCTION_CONSTRUCTOR 1
1566 #define FUNCTION_OPERATOR 2
1567
1568 /* function parameter */
1569 #define PARAMETER_NONE 0
1570 #define PARAMETER_NEXT 1
1571
1572 /* operator type */
1573 #define OPERATOR_ADDASSIGN 1
1574 #define OPERATOR_SUBASSIGN 2
1575 #define OPERATOR_MULASSIGN 3
1576 #define OPERATOR_DIVASSIGN 4
1577 /*#define OPERATOR_MODASSIGN 5*/
1578 /*#define OPERATOR_LSHASSIGN 6*/
1579 /*#define OPERATOR_RSHASSIGN 7*/
1580 /*#define OPERATOR_ANDASSIGN 8*/
1581 /*#define OPERATOR_XORASSIGN 9*/
1582 /*#define OPERATOR_ORASSIGN 10*/
1583 #define OPERATOR_LOGICALXOR 11
1584 /*#define OPERATOR_BITOR 12*/
1585 /*#define OPERATOR_BITXOR 13*/
1586 /*#define OPERATOR_BITAND 14*/
1587 #define OPERATOR_LESS 15
1588 #define OPERATOR_GREATER 16
1589 #define OPERATOR_LESSEQUAL 17
1590 #define OPERATOR_GREATEREQUAL 18
1591 /*#define OPERATOR_LSHIFT 19*/
1592 /*#define OPERATOR_RSHIFT 20*/
1593 #define OPERATOR_MULTIPLY 21
1594 #define OPERATOR_DIVIDE 22
1595 /*#define OPERATOR_MODULUS 23*/
1596 #define OPERATOR_INCREMENT 24
1597 #define OPERATOR_DECREMENT 25
1598 #define OPERATOR_PLUS 26
1599 #define OPERATOR_MINUS 27
1600 /*#define OPERATOR_COMPLEMENT 28*/
1601 #define OPERATOR_NOT 29
1602
1603 static const struct
1604 {
1605 unsigned int o_code;
1606 const char *o_name;
1607 } operator_names[] = {
1608 {OPERATOR_INCREMENT, "++"},
1609 {OPERATOR_ADDASSIGN, "+="},
1610 {OPERATOR_PLUS, "+"},
1611 {OPERATOR_DECREMENT, "--"},
1612 {OPERATOR_SUBASSIGN, "-="},
1613 {OPERATOR_MINUS, "-"},
1614 {OPERATOR_NOT, "!"},
1615 {OPERATOR_MULASSIGN, "*="},
1616 {OPERATOR_MULTIPLY, "*"},
1617 {OPERATOR_DIVASSIGN, "/="},
1618 {OPERATOR_DIVIDE, "/"},
1619 {OPERATOR_LESSEQUAL, "<="},
1620 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1621 /*{ OPERATOR_LSHIFT, "<<" }, */
1622 {OPERATOR_LESS, "<"},
1623 {OPERATOR_GREATEREQUAL, ">="},
1624 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1625 /*{ OPERATOR_RSHIFT, ">>" }, */
1626 {OPERATOR_GREATER, ">"},
1627 /*{ OPERATOR_MODASSIGN, "%=" }, */
1628 /*{ OPERATOR_MODULUS, "%" }, */
1629 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1630 /*{ OPERATOR_BITAND, "&" }, */
1631 /*{ OPERATOR_ORASSIGN, "|=" }, */
1632 /*{ OPERATOR_BITOR, "|" }, */
1633 /*{ OPERATOR_COMPLEMENT, "~" }, */
1634 /*{ OPERATOR_XORASSIGN, "^=" }, */
1635 {OPERATOR_LOGICALXOR, "^^"},
1636 /*{ OPERATOR_BITXOR, "^" } */
1637 };
1638
1639 static slang_atom
1640 parse_operator_name(slang_parse_ctx * C)
1641 {
1642 unsigned int i;
1643
1644 for (i = 0; i < sizeof(operator_names) / sizeof(*operator_names); i++) {
1645 if (operator_names[i].o_code == (unsigned int) (*C->I)) {
1646 slang_atom atom =
1647 slang_atom_pool_atom(C->atoms, operator_names[i].o_name);
1648 if (atom == SLANG_ATOM_NULL) {
1649 slang_info_log_memory(C->L);
1650 RETURN0;
1651 }
1652 C->I++;
1653 return atom;
1654 }
1655 }
1656 RETURN0;
1657 }
1658
1659
1660 static int
1661 parse_function_prototype(slang_parse_ctx * C, slang_output_ctx * O,
1662 slang_function * func)
1663 {
1664 GLuint functype;
1665 /* parse function type and name */
1666 if (!parse_fully_specified_type(C, O, &func->header.type))
1667 RETURN0;
1668
1669 functype = *C->I++;
1670 switch (functype) {
1671 case FUNCTION_ORDINARY:
1672 func->kind = SLANG_FUNC_ORDINARY;
1673 func->header.a_name = parse_identifier(C);
1674 if (func->header.a_name == SLANG_ATOM_NULL)
1675 RETURN0;
1676 break;
1677 case FUNCTION_CONSTRUCTOR:
1678 func->kind = SLANG_FUNC_CONSTRUCTOR;
1679 if (func->header.type.specifier.type == SLANG_SPEC_STRUCT)
1680 RETURN0;
1681 func->header.a_name =
1682 slang_atom_pool_atom(C->atoms,
1683 slang_type_specifier_type_to_string
1684 (func->header.type.specifier.type));
1685 if (func->header.a_name == SLANG_ATOM_NULL) {
1686 slang_info_log_memory(C->L);
1687 RETURN0;
1688 }
1689 break;
1690 case FUNCTION_OPERATOR:
1691 func->kind = SLANG_FUNC_OPERATOR;
1692 func->header.a_name = parse_operator_name(C);
1693 if (func->header.a_name == SLANG_ATOM_NULL)
1694 RETURN0;
1695 break;
1696 default:
1697 RETURN0;
1698 }
1699
1700 if (!legal_identifier(func->header.a_name)) {
1701 slang_info_log_error(C->L, "illegal function name '%s'",
1702 (char *) func->header.a_name);
1703 RETURN0;
1704 }
1705
1706 /* parse function parameters */
1707 while (*C->I++ == PARAMETER_NEXT) {
1708 slang_variable *p = slang_variable_scope_grow(func->parameters);
1709 if (!p) {
1710 slang_info_log_memory(C->L);
1711 RETURN0;
1712 }
1713 if (!parse_parameter_declaration(C, O, p))
1714 RETURN0;
1715 }
1716
1717 /* if the function returns a value, append a hidden __retVal 'out'
1718 * parameter that corresponds to the return value.
1719 */
1720 if (_slang_function_has_return_value(func)) {
1721 slang_variable *p = slang_variable_scope_grow(func->parameters);
1722 slang_atom a_retVal = slang_atom_pool_atom(C->atoms, "__retVal");
1723 assert(a_retVal);
1724 p->a_name = a_retVal;
1725 p->type = func->header.type;
1726 p->type.qualifier = SLANG_QUAL_OUT;
1727 }
1728
1729 /* function formal parameters and local variables share the same
1730 * scope, so save the information about param count in a seperate
1731 * place also link the scope to the global variable scope so when a
1732 * given identifier is not found here, the search process continues
1733 * in the global space
1734 */
1735 func->param_count = func->parameters->num_variables;
1736 func->parameters->outer_scope = O->vars;
1737
1738 return 1;
1739 }
1740
1741 static int
1742 parse_function_definition(slang_parse_ctx * C, slang_output_ctx * O,
1743 slang_function * func)
1744 {
1745 slang_output_ctx o = *O;
1746
1747 if (!parse_function_prototype(C, O, func))
1748 RETURN0;
1749
1750 /* create function's body operation */
1751 func->body = (slang_operation *) _slang_alloc(sizeof(slang_operation));
1752 if (func->body == NULL) {
1753 slang_info_log_memory(C->L);
1754 RETURN0;
1755 }
1756 if (!slang_operation_construct(func->body)) {
1757 _slang_free(func->body);
1758 func->body = NULL;
1759 slang_info_log_memory(C->L);
1760 RETURN0;
1761 }
1762
1763 /* to parse the body the parse context is modified in order to
1764 * capture parsed variables into function's local variable scope
1765 */
1766 C->global_scope = GL_FALSE;
1767 o.vars = func->parameters;
1768 if (!parse_statement(C, &o, func->body))
1769 RETURN0;
1770
1771 C->global_scope = GL_TRUE;
1772 return 1;
1773 }
1774
1775 static GLboolean
1776 initialize_global(slang_assemble_ctx * A, slang_variable * var)
1777 {
1778 slang_operation op_id, op_assign;
1779 GLboolean result;
1780
1781 /* construct the left side of assignment */
1782 if (!slang_operation_construct(&op_id))
1783 return GL_FALSE;
1784 op_id.type = SLANG_OPER_IDENTIFIER;
1785 op_id.a_id = var->a_name;
1786
1787 /* put the variable into operation's scope */
1788 op_id.locals->variables =
1789 (slang_variable **) _slang_alloc(sizeof(slang_variable *));
1790 if (op_id.locals->variables == NULL) {
1791 slang_operation_destruct(&op_id);
1792 return GL_FALSE;
1793 }
1794 op_id.locals->num_variables = 1;
1795 op_id.locals->variables[0] = var;
1796
1797 /* construct the assignment expression */
1798 if (!slang_operation_construct(&op_assign)) {
1799 op_id.locals->num_variables = 0;
1800 slang_operation_destruct(&op_id);
1801 return GL_FALSE;
1802 }
1803 op_assign.type = SLANG_OPER_ASSIGN;
1804 op_assign.children =
1805 (slang_operation *) _slang_alloc(2 * sizeof(slang_operation));
1806 if (op_assign.children == NULL) {
1807 slang_operation_destruct(&op_assign);
1808 op_id.locals->num_variables = 0;
1809 slang_operation_destruct(&op_id);
1810 return GL_FALSE;
1811 }
1812 op_assign.num_children = 2;
1813 op_assign.children[0] = op_id;
1814 op_assign.children[1] = *var->initializer;
1815
1816 result = 1;
1817
1818 /* carefully destroy the operations */
1819 op_assign.num_children = 0;
1820 _slang_free(op_assign.children);
1821 op_assign.children = NULL;
1822 slang_operation_destruct(&op_assign);
1823 op_id.locals->num_variables = 0;
1824 slang_operation_destruct(&op_id);
1825
1826 if (!result)
1827 return GL_FALSE;
1828
1829 return GL_TRUE;
1830 }
1831
1832 /* init declarator list */
1833 #define DECLARATOR_NONE 0
1834 #define DECLARATOR_NEXT 1
1835
1836 /* variable declaration */
1837 #define VARIABLE_NONE 0
1838 #define VARIABLE_IDENTIFIER 1
1839 #define VARIABLE_INITIALIZER 2
1840 #define VARIABLE_ARRAY_EXPLICIT 3
1841 #define VARIABLE_ARRAY_UNKNOWN 4
1842
1843
1844 /**
1845 * Parse the initializer for a variable declaration.
1846 */
1847 static int
1848 parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
1849 const slang_fully_specified_type * type)
1850 {
1851 slang_variable *var;
1852 slang_atom a_name;
1853
1854 /* empty init declatator (without name, e.g. "float ;") */
1855 if (*C->I++ == VARIABLE_NONE)
1856 return 1;
1857
1858 a_name = parse_identifier(C);
1859
1860 /* check if name is already in this scope */
1861 if (_slang_locate_variable(O->vars, a_name, GL_FALSE)) {
1862 slang_info_log_error(C->L,
1863 "declaration of '%s' conflicts with previous declaration",
1864 (char *) a_name);
1865 RETURN0;
1866 }
1867
1868 /* make room for the new variable and initialize it */
1869 var = slang_variable_scope_grow(O->vars);
1870 if (!var) {
1871 slang_info_log_memory(C->L);
1872 RETURN0;
1873 }
1874
1875 /* copy the declarator type qualifier/etc info, parse the identifier */
1876 var->type.qualifier = type->qualifier;
1877 var->type.centroid = type->centroid;
1878 var->type.precision = type->precision;
1879 var->type.variant = type->variant;
1880 var->a_name = a_name;
1881 if (var->a_name == SLANG_ATOM_NULL)
1882 RETURN0;
1883
1884 switch (*C->I++) {
1885 case VARIABLE_NONE:
1886 /* simple variable declarator - just copy the specifier */
1887 if (!slang_type_specifier_copy(&var->type.specifier, &type->specifier))
1888 RETURN0;
1889 break;
1890 case VARIABLE_INITIALIZER:
1891 /* initialized variable - copy the specifier and parse the expression */
1892 if (!slang_type_specifier_copy(&var->type.specifier, &type->specifier))
1893 RETURN0;
1894 var->initializer =
1895 (slang_operation *) _slang_alloc(sizeof(slang_operation));
1896 if (var->initializer == NULL) {
1897 slang_info_log_memory(C->L);
1898 RETURN0;
1899 }
1900 if (!slang_operation_construct(var->initializer)) {
1901 _slang_free(var->initializer);
1902 var->initializer = NULL;
1903 slang_info_log_memory(C->L);
1904 RETURN0;
1905 }
1906 if (!parse_expression(C, O, var->initializer))
1907 RETURN0;
1908 break;
1909 case VARIABLE_ARRAY_UNKNOWN:
1910 /* unsized array - mark it as array and copy the specifier to
1911 the array element
1912 */
1913 if (!convert_to_array(C, var, &type->specifier))
1914 return GL_FALSE;
1915 break;
1916 case VARIABLE_ARRAY_EXPLICIT:
1917 if (!convert_to_array(C, var, &type->specifier))
1918 return GL_FALSE;
1919 if (!parse_array_len(C, O, &var->array_len))
1920 return GL_FALSE;
1921 break;
1922 default:
1923 RETURN0;
1924 }
1925
1926 if (type->array_len >= 0) {
1927 /* The type was something like "float[4]" */
1928 if (var->array_len != 0) {
1929 slang_info_log_error(C->L, "multi-dimensional arrays not allowed");
1930 RETURN0;
1931 }
1932 convert_to_array(C, var, &type->specifier);
1933 var->array_len = type->array_len;
1934 }
1935
1936 /* allocate global address space for a variable with a known size */
1937 if (C->global_scope
1938 && !(var->type.specifier.type == SLANG_SPEC_ARRAY
1939 && var->array_len == 0)) {
1940 if (!calculate_var_size(C, O, var))
1941 return GL_FALSE;
1942 var->address = slang_var_pool_alloc(O->global_pool, var->size);
1943 }
1944
1945 /* emit code for global var decl */
1946 if (C->global_scope) {
1947 slang_assemble_ctx A;
1948 A.atoms = C->atoms;
1949 A.space.funcs = O->funs;
1950 A.space.structs = O->structs;
1951 A.space.vars = O->vars;
1952 A.program = O->program;
1953 A.vartable = O->vartable;
1954 A.log = C->L;
1955 A.curFuncEndLabel = NULL;
1956 if (!_slang_codegen_global_variable(&A, var, C->type))
1957 RETURN0;
1958 }
1959
1960 /* initialize global variable */
1961 if (C->global_scope) {
1962 if (var->initializer != NULL) {
1963 slang_assemble_ctx A;
1964
1965 A.atoms = C->atoms;
1966 A.space.funcs = O->funs;
1967 A.space.structs = O->structs;
1968 A.space.vars = O->vars;
1969 if (!initialize_global(&A, var))
1970 RETURN0;
1971 }
1972 }
1973 return 1;
1974 }
1975
1976 /**
1977 * Parse a list of variable declarations. Each variable may have an
1978 * initializer.
1979 */
1980 static int
1981 parse_init_declarator_list(slang_parse_ctx * C, slang_output_ctx * O)
1982 {
1983 slang_fully_specified_type type;
1984
1985 /* parse the fully specified type, common to all declarators */
1986 if (!slang_fully_specified_type_construct(&type))
1987 RETURN0;
1988 if (!parse_fully_specified_type(C, O, &type)) {
1989 slang_fully_specified_type_destruct(&type);
1990 RETURN0;
1991 }
1992
1993 /* parse declarators, pass-in the parsed type */
1994 do {
1995 if (!parse_init_declarator(C, O, &type)) {
1996 slang_fully_specified_type_destruct(&type);
1997 RETURN0;
1998 }
1999 }
2000 while (*C->I++ == DECLARATOR_NEXT);
2001
2002 slang_fully_specified_type_destruct(&type);
2003 return 1;
2004 }
2005
2006
2007 /**
2008 * Parse a function definition or declaration.
2009 * \param C parsing context
2010 * \param O output context
2011 * \param definition if non-zero expect a definition, else a declaration
2012 * \param parsed_func_ret returns the parsed function
2013 * \return GL_TRUE if success, GL_FALSE if failure
2014 */
2015 static GLboolean
2016 parse_function(slang_parse_ctx * C, slang_output_ctx * O, int definition,
2017 slang_function ** parsed_func_ret)
2018 {
2019 slang_function parsed_func, *found_func;
2020
2021 /* parse function definition/declaration */
2022 if (!slang_function_construct(&parsed_func))
2023 return GL_FALSE;
2024 if (definition) {
2025 if (!parse_function_definition(C, O, &parsed_func)) {
2026 slang_function_destruct(&parsed_func);
2027 return GL_FALSE;
2028 }
2029 }
2030 else {
2031 if (!parse_function_prototype(C, O, &parsed_func)) {
2032 slang_function_destruct(&parsed_func);
2033 return GL_FALSE;
2034 }
2035 }
2036
2037 /* find a function with a prototype matching the parsed one - only
2038 * the current scope is being searched to allow built-in function
2039 * overriding
2040 */
2041 found_func = slang_function_scope_find(O->funs, &parsed_func, 0);
2042 if (found_func == NULL) {
2043 /* New function, add it to the function list */
2044 O->funs->functions =
2045 (slang_function *) _slang_realloc(O->funs->functions,
2046 O->funs->num_functions
2047 * sizeof(slang_function),
2048 (O->funs->num_functions + 1)
2049 * sizeof(slang_function));
2050 if (O->funs->functions == NULL) {
2051 slang_info_log_memory(C->L);
2052 slang_function_destruct(&parsed_func);
2053 return GL_FALSE;
2054 }
2055 O->funs->functions[O->funs->num_functions] = parsed_func;
2056 O->funs->num_functions++;
2057
2058 /* return the newly parsed function */
2059 *parsed_func_ret = &O->funs->functions[O->funs->num_functions - 1];
2060 }
2061 else {
2062 /* previously defined or declared */
2063 /* TODO: check function return type qualifiers and specifiers */
2064 if (definition) {
2065 if (found_func->body != NULL) {
2066 slang_info_log_error(C->L, "%s: function already has a body.",
2067 slang_atom_pool_id(C->atoms,
2068 parsed_func.header.
2069 a_name));
2070 slang_function_destruct(&parsed_func);
2071 return GL_FALSE;
2072 }
2073
2074 /* destroy the existing function declaration and replace it
2075 * with the new one, remember to save the fixup table
2076 */
2077 parsed_func.fixups = found_func->fixups;
2078 slang_fixup_table_init(&found_func->fixups);
2079 slang_function_destruct(found_func);
2080 *found_func = parsed_func;
2081 }
2082 else {
2083 /* another declaration of the same function prototype - ignore it */
2084 slang_function_destruct(&parsed_func);
2085 }
2086
2087 /* return the found function */
2088 *parsed_func_ret = found_func;
2089 }
2090
2091 return GL_TRUE;
2092 }
2093
2094 /* declaration */
2095 #define DECLARATION_FUNCTION_PROTOTYPE 1
2096 #define DECLARATION_INIT_DECLARATOR_LIST 2
2097
2098 static int
2099 parse_declaration(slang_parse_ctx * C, slang_output_ctx * O)
2100 {
2101 switch (*C->I++) {
2102 case DECLARATION_INIT_DECLARATOR_LIST:
2103 if (!parse_init_declarator_list(C, O))
2104 RETURN0;
2105 break;
2106 case DECLARATION_FUNCTION_PROTOTYPE:
2107 {
2108 slang_function *dummy_func;
2109
2110 if (!parse_function(C, O, 0, &dummy_func))
2111 RETURN0;
2112 }
2113 break;
2114 default:
2115 RETURN0;
2116 }
2117 return 1;
2118 }
2119
2120 static int
2121 parse_default_precision(slang_parse_ctx * C, slang_output_ctx * O)
2122 {
2123 int precision, type;
2124
2125 if (!O->allow_precision) {
2126 slang_info_log_error(C->L, "syntax error at \"precision\"");
2127 RETURN0;
2128 }
2129
2130 precision = *C->I++;
2131 switch (precision) {
2132 case PRECISION_LOW:
2133 case PRECISION_MEDIUM:
2134 case PRECISION_HIGH:
2135 /* OK */
2136 break;
2137 default:
2138 _mesa_problem(NULL, "unexpected precision %d at %s:%d\n",
2139 precision, __FILE__, __LINE__);
2140 RETURN0;
2141 }
2142
2143 type = *C->I++;
2144 switch (type) {
2145 case TYPE_SPECIFIER_FLOAT:
2146 case TYPE_SPECIFIER_INT:
2147 case TYPE_SPECIFIER_SAMPLER1D:
2148 case TYPE_SPECIFIER_SAMPLER2D:
2149 case TYPE_SPECIFIER_SAMPLER3D:
2150 case TYPE_SPECIFIER_SAMPLERCUBE:
2151 case TYPE_SPECIFIER_SAMPLER1DSHADOW:
2152 case TYPE_SPECIFIER_SAMPLER2DSHADOW:
2153 case TYPE_SPECIFIER_SAMPLER2DRECT:
2154 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW:
2155 /* OK */
2156 break;
2157 default:
2158 _mesa_problem(NULL, "unexpected type %d at %s:%d\n",
2159 type, __FILE__, __LINE__);
2160 RETURN0;
2161 }
2162
2163 assert(type < TYPE_SPECIFIER_COUNT);
2164 O->default_precision[type] = precision;
2165
2166 return 1;
2167 }
2168
2169
2170 /**
2171 * Initialize the default precision for all types.
2172 * XXX this info isn't used yet.
2173 */
2174 static void
2175 init_default_precision(slang_output_ctx *O, slang_unit_type type)
2176 {
2177 GLuint i;
2178 for (i = 0; i < TYPE_SPECIFIER_COUNT; i++) {
2179 #if FEATURE_es2_glsl
2180 O->default_precision[i] = PRECISION_LOW;
2181 #else
2182 O->default_precision[i] = PRECISION_HIGH;
2183 #endif
2184 }
2185
2186 if (type == SLANG_UNIT_VERTEX_SHADER) {
2187 O->default_precision[TYPE_SPECIFIER_FLOAT] = PRECISION_HIGH;
2188 O->default_precision[TYPE_SPECIFIER_INT] = PRECISION_HIGH;
2189 }
2190 else {
2191 O->default_precision[TYPE_SPECIFIER_INT] = PRECISION_MEDIUM;
2192 }
2193 }
2194
2195
2196 static int
2197 parse_invariant(slang_parse_ctx * C, slang_output_ctx * O)
2198 {
2199 if (O->allow_invariant) {
2200 slang_atom *a = parse_identifier(C);
2201 /* XXX not doing anything with this var yet */
2202 /*printf("ID: %s\n", (char*) a);*/
2203 return a ? 1 : 0;
2204 }
2205 else {
2206 slang_info_log_error(C->L, "syntax error at \"invariant\"");
2207 RETURN0;
2208 }
2209 }
2210
2211
2212 /* external declaration or default precision specifier */
2213 #define EXTERNAL_NULL 0
2214 #define EXTERNAL_FUNCTION_DEFINITION 1
2215 #define EXTERNAL_DECLARATION 2
2216 #define DEFAULT_PRECISION 3
2217 #define INVARIANT_STMT 4
2218
2219
2220 static GLboolean
2221 parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
2222 struct gl_shader *shader)
2223 {
2224 GET_CURRENT_CONTEXT(ctx);
2225 slang_output_ctx o;
2226 GLboolean success;
2227 GLuint maxRegs;
2228 slang_function *mainFunc = NULL;
2229
2230 if (unit->type == SLANG_UNIT_FRAGMENT_BUILTIN ||
2231 unit->type == SLANG_UNIT_FRAGMENT_SHADER) {
2232 maxRegs = ctx->Const.FragmentProgram.MaxTemps;
2233 }
2234 else {
2235 assert(unit->type == SLANG_UNIT_VERTEX_BUILTIN ||
2236 unit->type == SLANG_UNIT_VERTEX_SHADER);
2237 maxRegs = ctx->Const.VertexProgram.MaxTemps;
2238 }
2239
2240 /* setup output context */
2241 o.funs = &unit->funs;
2242 o.structs = &unit->structs;
2243 o.vars = &unit->vars;
2244 o.global_pool = &unit->object->varpool;
2245 o.program = shader ? shader->Program : NULL;
2246 o.vartable = _slang_new_var_table(maxRegs);
2247 _slang_push_var_table(o.vartable);
2248
2249 /* allow 'invariant' keyword? */
2250 #if FEATURE_es2_glsl
2251 o.allow_invariant = GL_TRUE;
2252 #else
2253 o.allow_invariant = (C->version >= 120) ? GL_TRUE : GL_FALSE;
2254 #endif
2255
2256 /* allow 'centroid' keyword? */
2257 o.allow_centroid = (C->version >= 120) ? GL_TRUE : GL_FALSE;
2258
2259 /* allow 'lowp/mediump/highp' keywords? */
2260 #if FEATURE_es2_glsl
2261 o.allow_precision = GL_TRUE;
2262 #else
2263 o.allow_precision = (C->version >= 120) ? GL_TRUE : GL_FALSE;
2264 #endif
2265 init_default_precision(&o, unit->type);
2266
2267 /* allow 'float[]' keyword? */
2268 o.allow_array_types = (C->version >= 120) ? GL_TRUE : GL_FALSE;
2269
2270 /* parse individual functions and declarations */
2271 while (*C->I != EXTERNAL_NULL) {
2272 switch (*C->I++) {
2273 case EXTERNAL_FUNCTION_DEFINITION:
2274 {
2275 slang_function *func;
2276 success = parse_function(C, &o, 1, &func);
2277 if (success &&
2278 _mesa_strcmp((char *) func->header.a_name, "main") == 0) {
2279 /* found main() */
2280 mainFunc = func;
2281 }
2282 }
2283 break;
2284 case EXTERNAL_DECLARATION:
2285 success = parse_declaration(C, &o);
2286 break;
2287 case DEFAULT_PRECISION:
2288 success = parse_default_precision(C, &o);
2289 break;
2290 case INVARIANT_STMT:
2291 success = parse_invariant(C, &o);
2292 break;
2293 default:
2294 success = GL_FALSE;
2295 }
2296
2297 if (!success) {
2298 /* xxx free codegen */
2299 _slang_pop_var_table(o.vartable);
2300 return GL_FALSE;
2301 }
2302 }
2303 C->I++;
2304
2305 if (mainFunc) {
2306 /* assemble (generate code) for main() */
2307 slang_assemble_ctx A;
2308
2309 A.atoms = C->atoms;
2310 A.space.funcs = o.funs;
2311 A.space.structs = o.structs;
2312 A.space.vars = o.vars;
2313 A.program = o.program;
2314 A.vartable = o.vartable;
2315 A.log = C->L;
2316
2317 /* main() takes no parameters */
2318 if (mainFunc->param_count > 0) {
2319 slang_info_log_error(A.log, "main() takes no arguments");
2320 return GL_FALSE;
2321 }
2322
2323 _slang_codegen_function(&A, mainFunc);
2324
2325 shader->Main = GL_TRUE; /* this shader defines main() */
2326 }
2327
2328 _slang_pop_var_table(o.vartable);
2329 _slang_delete_var_table(o.vartable);
2330
2331 return GL_TRUE;
2332 }
2333
2334 static GLboolean
2335 compile_binary(const byte * prod, slang_code_unit * unit,
2336 GLuint version,
2337 slang_unit_type type, slang_info_log * infolog,
2338 slang_code_unit * builtin, slang_code_unit * downlink,
2339 struct gl_shader *shader)
2340 {
2341 slang_parse_ctx C;
2342
2343 unit->type = type;
2344
2345 /* setup parse context */
2346 C.I = prod;
2347 C.L = infolog;
2348 C.parsing_builtin = (builtin == NULL);
2349 C.global_scope = GL_TRUE;
2350 C.atoms = &unit->object->atompool;
2351 C.type = type;
2352 C.version = version;
2353
2354 if (!check_revision(&C))
2355 return GL_FALSE;
2356
2357 if (downlink != NULL) {
2358 unit->vars.outer_scope = &downlink->vars;
2359 unit->funs.outer_scope = &downlink->funs;
2360 unit->structs.outer_scope = &downlink->structs;
2361 }
2362
2363 /* parse translation unit */
2364 return parse_code_unit(&C, unit, shader);
2365 }
2366
2367 static GLboolean
2368 compile_with_grammar(grammar id, const char *source, slang_code_unit * unit,
2369 slang_unit_type type, slang_info_log * infolog,
2370 slang_code_unit * builtin,
2371 struct gl_shader *shader)
2372 {
2373 byte *prod;
2374 GLuint size, start, version;
2375 slang_string preprocessed;
2376 GLuint maxVersion;
2377
2378 #if FEATURE_ARB_shading_language_120
2379 maxVersion = 120;
2380 #elif FEATURE_es2_glsl
2381 maxVersion = 100;
2382 #else
2383 maxVersion = 110;
2384 #endif
2385
2386 /* First retrieve the version number. */
2387 if (!_slang_preprocess_version(source, &version, &start, infolog))
2388 return GL_FALSE;
2389
2390 if (version > maxVersion) {
2391 slang_info_log_error(infolog,
2392 "language version %.2f is not supported.",
2393 version * 0.01);
2394 return GL_FALSE;
2395 }
2396
2397 /* Now preprocess the source string. */
2398 slang_string_init(&preprocessed);
2399 if (!_slang_preprocess_directives(&preprocessed, &source[start], infolog)) {
2400 slang_string_free(&preprocessed);
2401 slang_info_log_error(infolog, "failed to preprocess the source.");
2402 return GL_FALSE;
2403 }
2404
2405 /* Finally check the syntax and generate its binary representation. */
2406 if (!grammar_fast_check(id,
2407 (const byte *) (slang_string_cstr(&preprocessed)),
2408 &prod, &size, 65536)) {
2409 char buf[1024];
2410 GLint pos;
2411
2412 slang_string_free(&preprocessed);
2413 grammar_get_last_error((byte *) (buf), sizeof(buf), &pos);
2414 slang_info_log_error(infolog, buf);
2415 /* syntax error (possibly in library code) */
2416 #if 0
2417 {
2418 int line, col;
2419 char *s;
2420 s = (char *) _mesa_find_line_column((const GLubyte *) source,
2421 (const GLubyte *) source + pos,
2422 &line, &col);
2423 printf("Error on line %d, col %d: %s\n", line, col, s);
2424 }
2425 #endif
2426 return GL_FALSE;
2427 }
2428 slang_string_free(&preprocessed);
2429
2430 /* Syntax is okay - translate it to internal representation. */
2431 if (!compile_binary(prod, unit, version, type, infolog, builtin,
2432 &builtin[SLANG_BUILTIN_TOTAL - 1],
2433 shader)) {
2434 grammar_alloc_free(prod);
2435 return GL_FALSE;
2436 }
2437 grammar_alloc_free(prod);
2438 return GL_TRUE;
2439 }
2440
2441 LONGSTRING static const char *slang_shader_syn =
2442 #include "library/slang_shader_syn.h"
2443 ;
2444
2445 static const byte slang_core_gc[] = {
2446 #include "library/slang_core_gc.h"
2447 };
2448
2449 static const byte slang_120_core_gc[] = {
2450 #include "library/slang_120_core_gc.h"
2451 };
2452
2453 static const byte slang_120_fragment_gc[] = {
2454 #include "library/slang_builtin_120_fragment_gc.h"
2455 };
2456
2457 static const byte slang_common_builtin_gc[] = {
2458 #include "library/slang_common_builtin_gc.h"
2459 };
2460
2461 static const byte slang_fragment_builtin_gc[] = {
2462 #include "library/slang_fragment_builtin_gc.h"
2463 };
2464
2465 static const byte slang_vertex_builtin_gc[] = {
2466 #include "library/slang_vertex_builtin_gc.h"
2467 };
2468
2469 static GLboolean
2470 compile_object(grammar * id, const char *source, slang_code_object * object,
2471 slang_unit_type type, slang_info_log * infolog,
2472 struct gl_shader *shader)
2473 {
2474 slang_code_unit *builtins = NULL;
2475 GLuint base_version = 110;
2476
2477 /* load GLSL grammar */
2478 *id = grammar_load_from_text((const byte *) (slang_shader_syn));
2479 if (*id == 0) {
2480 byte buf[1024];
2481 int pos;
2482
2483 grammar_get_last_error(buf, 1024, &pos);
2484 slang_info_log_error(infolog, (const char *) (buf));
2485 return GL_FALSE;
2486 }
2487
2488 /* set shader type - the syntax is slightly different for different shaders */
2489 if (type == SLANG_UNIT_FRAGMENT_SHADER
2490 || type == SLANG_UNIT_FRAGMENT_BUILTIN)
2491 grammar_set_reg8(*id, (const byte *) "shader_type", 1);
2492 else
2493 grammar_set_reg8(*id, (const byte *) "shader_type", 2);
2494
2495 /* enable language extensions */
2496 grammar_set_reg8(*id, (const byte *) "parsing_builtin", 1);
2497
2498 /* if parsing user-specified shader, load built-in library */
2499 if (type == SLANG_UNIT_FRAGMENT_SHADER || type == SLANG_UNIT_VERTEX_SHADER) {
2500 /* compile core functionality first */
2501 if (!compile_binary(slang_core_gc,
2502 &object->builtin[SLANG_BUILTIN_CORE],
2503 base_version,
2504 SLANG_UNIT_FRAGMENT_BUILTIN, infolog,
2505 NULL, NULL, NULL))
2506 return GL_FALSE;
2507
2508 #if FEATURE_ARB_shading_language_120
2509 if (!compile_binary(slang_120_core_gc,
2510 &object->builtin[SLANG_BUILTIN_120_CORE],
2511 120,
2512 SLANG_UNIT_FRAGMENT_BUILTIN, infolog,
2513 NULL, &object->builtin[SLANG_BUILTIN_CORE], NULL))
2514 return GL_FALSE;
2515 #endif
2516
2517 /* compile common functions and variables, link to core */
2518 if (!compile_binary(slang_common_builtin_gc,
2519 &object->builtin[SLANG_BUILTIN_COMMON],
2520 #if FEATURE_ARB_shading_language_120
2521 120,
2522 #else
2523 base_version,
2524 #endif
2525 SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
2526 #if FEATURE_ARB_shading_language_120
2527 &object->builtin[SLANG_BUILTIN_120_CORE],
2528 #else
2529 &object->builtin[SLANG_BUILTIN_CORE],
2530 #endif
2531 NULL))
2532 return GL_FALSE;
2533
2534 /* compile target-specific functions and variables, link to common */
2535 if (type == SLANG_UNIT_FRAGMENT_SHADER) {
2536 if (!compile_binary(slang_fragment_builtin_gc,
2537 &object->builtin[SLANG_BUILTIN_TARGET],
2538 base_version,
2539 SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
2540 &object->builtin[SLANG_BUILTIN_COMMON], NULL))
2541 return GL_FALSE;
2542 #if FEATURE_ARB_shading_language_120
2543 if (!compile_binary(slang_120_fragment_gc,
2544 &object->builtin[SLANG_BUILTIN_TARGET],
2545 120,
2546 SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
2547 &object->builtin[SLANG_BUILTIN_COMMON], NULL))
2548 return GL_FALSE;
2549 #endif
2550 }
2551 else if (type == SLANG_UNIT_VERTEX_SHADER) {
2552 if (!compile_binary(slang_vertex_builtin_gc,
2553 &object->builtin[SLANG_BUILTIN_TARGET],
2554 base_version,
2555 SLANG_UNIT_VERTEX_BUILTIN, infolog, NULL,
2556 &object->builtin[SLANG_BUILTIN_COMMON], NULL))
2557 return GL_FALSE;
2558 }
2559
2560 /* disable language extensions */
2561 #if NEW_SLANG /* allow-built-ins */
2562 grammar_set_reg8(*id, (const byte *) "parsing_builtin", 1);
2563 #else
2564 grammar_set_reg8(*id, (const byte *) "parsing_builtin", 0);
2565 #endif
2566 builtins = object->builtin;
2567 }
2568
2569 /* compile the actual shader - pass-in built-in library for external shader */
2570 return compile_with_grammar(*id, source, &object->unit, type, infolog,
2571 builtins, shader);
2572 }
2573
2574
2575 static GLboolean
2576 compile_shader(GLcontext *ctx, slang_code_object * object,
2577 slang_unit_type type, slang_info_log * infolog,
2578 struct gl_shader *shader)
2579 {
2580 GLboolean success;
2581 grammar id = 0;
2582
2583 #if 0 /* for debug */
2584 _mesa_printf("********* COMPILE SHADER ***********\n");
2585 _mesa_printf("%s\n", shader->Source);
2586 _mesa_printf("************************************\n");
2587 #endif
2588
2589 assert(shader->Program);
2590
2591 _slang_code_object_dtr(object);
2592 _slang_code_object_ctr(object);
2593
2594 success = compile_object(&id, shader->Source, object, type, infolog, shader);
2595 if (id != 0)
2596 grammar_destroy(id);
2597 if (!success)
2598 return GL_FALSE;
2599
2600 return GL_TRUE;
2601 }
2602
2603
2604
2605 GLboolean
2606 _slang_compile(GLcontext *ctx, struct gl_shader *shader)
2607 {
2608 GLboolean success;
2609 slang_info_log info_log;
2610 slang_code_object obj;
2611 slang_unit_type type;
2612
2613 if (shader->Type == GL_VERTEX_SHADER) {
2614 type = SLANG_UNIT_VERTEX_SHADER;
2615 }
2616 else {
2617 assert(shader->Type == GL_FRAGMENT_SHADER);
2618 type = SLANG_UNIT_FRAGMENT_SHADER;
2619 }
2620
2621 if (!shader->Source)
2622 return GL_FALSE;
2623
2624 ctx->Shader.MemPool = _slang_new_mempool(1024*1024);
2625
2626 shader->Main = GL_FALSE;
2627
2628 if (!shader->Program) {
2629 GLenum progTarget;
2630 if (shader->Type == GL_VERTEX_SHADER)
2631 progTarget = GL_VERTEX_PROGRAM_ARB;
2632 else
2633 progTarget = GL_FRAGMENT_PROGRAM_ARB;
2634 shader->Program = ctx->Driver.NewProgram(ctx, progTarget, 1);
2635 shader->Program->Parameters = _mesa_new_parameter_list();
2636 shader->Program->Varying = _mesa_new_parameter_list();
2637 shader->Program->Attributes = _mesa_new_parameter_list();
2638 }
2639
2640 slang_info_log_construct(&info_log);
2641 _slang_code_object_ctr(&obj);
2642
2643 success = compile_shader(ctx, &obj, type, &info_log, shader);
2644
2645 /* free shader's prev info log */
2646 if (shader->InfoLog) {
2647 _mesa_free(shader->InfoLog);
2648 shader->InfoLog = NULL;
2649 }
2650
2651 if (info_log.text) {
2652 /* copy info-log string to shader object */
2653 shader->InfoLog = _mesa_strdup(info_log.text);
2654 }
2655
2656 if (info_log.error_flag) {
2657 success = GL_FALSE;
2658 }
2659
2660 slang_info_log_destruct(&info_log);
2661 _slang_code_object_dtr(&obj);
2662
2663 _slang_delete_mempool((slang_mempool *) ctx->Shader.MemPool);
2664 ctx->Shader.MemPool = NULL;
2665
2666 /* remove any reads of output registers */
2667 #if 0
2668 printf("Pre-remove output reads:\n");
2669 _mesa_print_program(shader->Program);
2670 #endif
2671 _mesa_remove_output_reads(shader->Program, PROGRAM_OUTPUT);
2672 if (shader->Type == GL_VERTEX_SHADER) {
2673 /* and remove writes to varying vars in vertex programs */
2674 _mesa_remove_output_reads(shader->Program, PROGRAM_VARYING);
2675 }
2676 #if 0
2677 printf("Post-remove output reads:\n");
2678 _mesa_print_program(shader->Program);
2679 #endif
2680
2681 return success;
2682 }
2683