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