mesa: added support for GLSL 1.20 array.length() method
[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 printf("******* begin OP_METHOD\n");
1395 op->type = SLANG_OPER_METHOD;
1396 op->a_obj = parse_identifier(C);
1397 if (op->a_obj == SLANG_ATOM_NULL)
1398 RETURN0;
1399
1400 op->a_id = parse_identifier(C);
1401 if (op->a_id == SLANG_ATOM_NULL)
1402 RETURN0;
1403
1404 while (*C->I != OP_END)
1405 if (!parse_child_operation(C, O, op, 0))
1406 RETURN0;
1407 C->I++;
1408 #if 0
1409 /* don't lookup the method (not yet anyway) */
1410 if (!C->parsing_builtin
1411 && !slang_function_scope_find_by_name(O->funs, op->a_id, 1)) {
1412 const char *id;
1413
1414 id = slang_atom_pool_id(C->atoms, op->a_id);
1415 if (!is_constructor_name(id, op->a_id, O->structs)) {
1416 slang_info_log_error(C->L, "%s: undeclared function name.", id);
1417 RETURN0;
1418 }
1419 }
1420 #endif
1421 break;
1422 case OP_CALL:
1423 op->type = SLANG_OPER_CALL;
1424 op->a_id = parse_identifier(C);
1425 if (op->a_id == SLANG_ATOM_NULL)
1426 RETURN0;
1427 while (*C->I != OP_END)
1428 if (!parse_child_operation(C, O, op, 0))
1429 RETURN0;
1430 C->I++;
1431
1432 if (!C->parsing_builtin
1433 && !slang_function_scope_find_by_name(O->funs, op->a_id, 1)) {
1434 const char *id;
1435
1436 id = slang_atom_pool_id(C->atoms, op->a_id);
1437 if (!is_constructor_name(id, op->a_id, O->structs)) {
1438 slang_info_log_error(C->L, "%s: undeclared function name.", id);
1439 RETURN0;
1440 }
1441 }
1442 break;
1443 case OP_FIELD:
1444 op->type = SLANG_OPER_FIELD;
1445 op->a_id = parse_identifier(C);
1446 if (op->a_id == SLANG_ATOM_NULL)
1447 RETURN0;
1448 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1449 RETURN0;
1450 break;
1451 case OP_POSTINCREMENT:
1452 op->type = SLANG_OPER_POSTINCREMENT;
1453 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1454 RETURN0;
1455 break;
1456 case OP_POSTDECREMENT:
1457 op->type = SLANG_OPER_POSTDECREMENT;
1458 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1459 RETURN0;
1460 break;
1461 default:
1462 RETURN0;
1463 }
1464 }
1465 C->I++;
1466
1467 slang_operation_destruct(oper);
1468 *oper = *ops; /* struct copy */
1469 _slang_free(ops);
1470
1471 return 1;
1472 }
1473
1474 /* parameter qualifier */
1475 #define PARAM_QUALIFIER_IN 0
1476 #define PARAM_QUALIFIER_OUT 1
1477 #define PARAM_QUALIFIER_INOUT 2
1478
1479 /* function parameter array presence */
1480 #define PARAMETER_ARRAY_NOT_PRESENT 0
1481 #define PARAMETER_ARRAY_PRESENT 1
1482
1483 static int
1484 parse_parameter_declaration(slang_parse_ctx * C, slang_output_ctx * O,
1485 slang_variable * param)
1486 {
1487 int param_qual, precision_qual;
1488
1489 /* parse and validate the parameter's type qualifiers (there can be
1490 * two at most) because not all combinations are valid
1491 */
1492 if (!parse_type_qualifier(C, &param->type.qualifier))
1493 RETURN0;
1494
1495 param_qual = *C->I++;
1496 switch (param_qual) {
1497 case PARAM_QUALIFIER_IN:
1498 if (param->type.qualifier != SLANG_QUAL_CONST
1499 && param->type.qualifier != SLANG_QUAL_NONE) {
1500 slang_info_log_error(C->L, "Invalid type qualifier.");
1501 RETURN0;
1502 }
1503 break;
1504 case PARAM_QUALIFIER_OUT:
1505 if (param->type.qualifier == SLANG_QUAL_NONE)
1506 param->type.qualifier = SLANG_QUAL_OUT;
1507 else {
1508 slang_info_log_error(C->L, "Invalid type qualifier.");
1509 RETURN0;
1510 }
1511 break;
1512 case PARAM_QUALIFIER_INOUT:
1513 if (param->type.qualifier == SLANG_QUAL_NONE)
1514 param->type.qualifier = SLANG_QUAL_INOUT;
1515 else {
1516 slang_info_log_error(C->L, "Invalid type qualifier.");
1517 RETURN0;
1518 }
1519 break;
1520 default:
1521 RETURN0;
1522 }
1523
1524 /* parse precision qualifier (lowp, mediump, highp */
1525 precision_qual = *C->I++;
1526 /* ignored at this time */
1527 (void) precision_qual;
1528
1529 /* parse parameter's type specifier and name */
1530 if (!parse_type_specifier(C, O, &param->type.specifier))
1531 RETURN0;
1532 param->a_name = parse_identifier(C);
1533 if (param->a_name == SLANG_ATOM_NULL)
1534 RETURN0;
1535
1536 /* if the parameter is an array, parse its size (the size must be
1537 * explicitly defined
1538 */
1539 if (*C->I++ == PARAMETER_ARRAY_PRESENT) {
1540 slang_type_specifier p;
1541
1542 slang_type_specifier_ctr(&p);
1543 if (!slang_type_specifier_copy(&p, &param->type.specifier)) {
1544 slang_type_specifier_dtr(&p);
1545 return GL_FALSE;
1546 }
1547 if (!convert_to_array(C, param, &p)) {
1548 slang_type_specifier_dtr(&p);
1549 return GL_FALSE;
1550 }
1551 slang_type_specifier_dtr(&p);
1552 if (!parse_array_len(C, O, &param->array_len))
1553 return GL_FALSE;
1554 }
1555
1556 /* calculate the parameter size */
1557 if (!calculate_var_size(C, O, param))
1558 return GL_FALSE;
1559
1560 /* TODO: allocate the local address here? */
1561 return 1;
1562 }
1563
1564 /* function type */
1565 #define FUNCTION_ORDINARY 0
1566 #define FUNCTION_CONSTRUCTOR 1
1567 #define FUNCTION_OPERATOR 2
1568
1569 /* function parameter */
1570 #define PARAMETER_NONE 0
1571 #define PARAMETER_NEXT 1
1572
1573 /* operator type */
1574 #define OPERATOR_ADDASSIGN 1
1575 #define OPERATOR_SUBASSIGN 2
1576 #define OPERATOR_MULASSIGN 3
1577 #define OPERATOR_DIVASSIGN 4
1578 /*#define OPERATOR_MODASSIGN 5*/
1579 /*#define OPERATOR_LSHASSIGN 6*/
1580 /*#define OPERATOR_RSHASSIGN 7*/
1581 /*#define OPERATOR_ANDASSIGN 8*/
1582 /*#define OPERATOR_XORASSIGN 9*/
1583 /*#define OPERATOR_ORASSIGN 10*/
1584 #define OPERATOR_LOGICALXOR 11
1585 /*#define OPERATOR_BITOR 12*/
1586 /*#define OPERATOR_BITXOR 13*/
1587 /*#define OPERATOR_BITAND 14*/
1588 #define OPERATOR_LESS 15
1589 #define OPERATOR_GREATER 16
1590 #define OPERATOR_LESSEQUAL 17
1591 #define OPERATOR_GREATEREQUAL 18
1592 /*#define OPERATOR_LSHIFT 19*/
1593 /*#define OPERATOR_RSHIFT 20*/
1594 #define OPERATOR_MULTIPLY 21
1595 #define OPERATOR_DIVIDE 22
1596 /*#define OPERATOR_MODULUS 23*/
1597 #define OPERATOR_INCREMENT 24
1598 #define OPERATOR_DECREMENT 25
1599 #define OPERATOR_PLUS 26
1600 #define OPERATOR_MINUS 27
1601 /*#define OPERATOR_COMPLEMENT 28*/
1602 #define OPERATOR_NOT 29
1603
1604 static const struct
1605 {
1606 unsigned int o_code;
1607 const char *o_name;
1608 } operator_names[] = {
1609 {OPERATOR_INCREMENT, "++"},
1610 {OPERATOR_ADDASSIGN, "+="},
1611 {OPERATOR_PLUS, "+"},
1612 {OPERATOR_DECREMENT, "--"},
1613 {OPERATOR_SUBASSIGN, "-="},
1614 {OPERATOR_MINUS, "-"},
1615 {OPERATOR_NOT, "!"},
1616 {OPERATOR_MULASSIGN, "*="},
1617 {OPERATOR_MULTIPLY, "*"},
1618 {OPERATOR_DIVASSIGN, "/="},
1619 {OPERATOR_DIVIDE, "/"},
1620 {OPERATOR_LESSEQUAL, "<="},
1621 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1622 /*{ OPERATOR_LSHIFT, "<<" }, */
1623 {OPERATOR_LESS, "<"},
1624 {OPERATOR_GREATEREQUAL, ">="},
1625 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1626 /*{ OPERATOR_RSHIFT, ">>" }, */
1627 {OPERATOR_GREATER, ">"},
1628 /*{ OPERATOR_MODASSIGN, "%=" }, */
1629 /*{ OPERATOR_MODULUS, "%" }, */
1630 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1631 /*{ OPERATOR_BITAND, "&" }, */
1632 /*{ OPERATOR_ORASSIGN, "|=" }, */
1633 /*{ OPERATOR_BITOR, "|" }, */
1634 /*{ OPERATOR_COMPLEMENT, "~" }, */
1635 /*{ OPERATOR_XORASSIGN, "^=" }, */
1636 {OPERATOR_LOGICALXOR, "^^"},
1637 /*{ OPERATOR_BITXOR, "^" } */
1638 };
1639
1640 static slang_atom
1641 parse_operator_name(slang_parse_ctx * C)
1642 {
1643 unsigned int i;
1644
1645 for (i = 0; i < sizeof(operator_names) / sizeof(*operator_names); i++) {
1646 if (operator_names[i].o_code == (unsigned int) (*C->I)) {
1647 slang_atom atom =
1648 slang_atom_pool_atom(C->atoms, operator_names[i].o_name);
1649 if (atom == SLANG_ATOM_NULL) {
1650 slang_info_log_memory(C->L);
1651 RETURN0;
1652 }
1653 C->I++;
1654 return atom;
1655 }
1656 }
1657 RETURN0;
1658 }
1659
1660
1661 static int
1662 parse_function_prototype(slang_parse_ctx * C, slang_output_ctx * O,
1663 slang_function * func)
1664 {
1665 GLuint functype;
1666 /* parse function type and name */
1667 if (!parse_fully_specified_type(C, O, &func->header.type))
1668 RETURN0;
1669
1670 functype = *C->I++;
1671 switch (functype) {
1672 case FUNCTION_ORDINARY:
1673 func->kind = SLANG_FUNC_ORDINARY;
1674 func->header.a_name = parse_identifier(C);
1675 if (func->header.a_name == SLANG_ATOM_NULL)
1676 RETURN0;
1677 break;
1678 case FUNCTION_CONSTRUCTOR:
1679 func->kind = SLANG_FUNC_CONSTRUCTOR;
1680 if (func->header.type.specifier.type == SLANG_SPEC_STRUCT)
1681 RETURN0;
1682 func->header.a_name =
1683 slang_atom_pool_atom(C->atoms,
1684 slang_type_specifier_type_to_string
1685 (func->header.type.specifier.type));
1686 if (func->header.a_name == SLANG_ATOM_NULL) {
1687 slang_info_log_memory(C->L);
1688 RETURN0;
1689 }
1690 break;
1691 case FUNCTION_OPERATOR:
1692 func->kind = SLANG_FUNC_OPERATOR;
1693 func->header.a_name = parse_operator_name(C);
1694 if (func->header.a_name == SLANG_ATOM_NULL)
1695 RETURN0;
1696 break;
1697 default:
1698 RETURN0;
1699 }
1700
1701 if (!legal_identifier(func->header.a_name)) {
1702 slang_info_log_error(C->L, "illegal function name '%s'",
1703 (char *) func->header.a_name);
1704 RETURN0;
1705 }
1706
1707 /* parse function parameters */
1708 while (*C->I++ == PARAMETER_NEXT) {
1709 slang_variable *p = slang_variable_scope_grow(func->parameters);
1710 if (!p) {
1711 slang_info_log_memory(C->L);
1712 RETURN0;
1713 }
1714 if (!parse_parameter_declaration(C, O, p))
1715 RETURN0;
1716 }
1717
1718 /* if the function returns a value, append a hidden __retVal 'out'
1719 * parameter that corresponds to the return value.
1720 */
1721 if (_slang_function_has_return_value(func)) {
1722 slang_variable *p = slang_variable_scope_grow(func->parameters);
1723 slang_atom a_retVal = slang_atom_pool_atom(C->atoms, "__retVal");
1724 assert(a_retVal);
1725 p->a_name = a_retVal;
1726 p->type = func->header.type;
1727 p->type.qualifier = SLANG_QUAL_OUT;
1728 }
1729
1730 /* function formal parameters and local variables share the same
1731 * scope, so save the information about param count in a seperate
1732 * place also link the scope to the global variable scope so when a
1733 * given identifier is not found here, the search process continues
1734 * in the global space
1735 */
1736 func->param_count = func->parameters->num_variables;
1737 func->parameters->outer_scope = O->vars;
1738
1739 return 1;
1740 }
1741
1742 static int
1743 parse_function_definition(slang_parse_ctx * C, slang_output_ctx * O,
1744 slang_function * func)
1745 {
1746 slang_output_ctx o = *O;
1747
1748 if (!parse_function_prototype(C, O, func))
1749 RETURN0;
1750
1751 /* create function's body operation */
1752 func->body = (slang_operation *) _slang_alloc(sizeof(slang_operation));
1753 if (func->body == NULL) {
1754 slang_info_log_memory(C->L);
1755 RETURN0;
1756 }
1757 if (!slang_operation_construct(func->body)) {
1758 _slang_free(func->body);
1759 func->body = NULL;
1760 slang_info_log_memory(C->L);
1761 RETURN0;
1762 }
1763
1764 /* to parse the body the parse context is modified in order to
1765 * capture parsed variables into function's local variable scope
1766 */
1767 C->global_scope = GL_FALSE;
1768 o.vars = func->parameters;
1769 if (!parse_statement(C, &o, func->body))
1770 RETURN0;
1771
1772 C->global_scope = GL_TRUE;
1773 return 1;
1774 }
1775
1776 static GLboolean
1777 initialize_global(slang_assemble_ctx * A, slang_variable * var)
1778 {
1779 slang_operation op_id, op_assign;
1780 GLboolean result;
1781
1782 /* construct the left side of assignment */
1783 if (!slang_operation_construct(&op_id))
1784 return GL_FALSE;
1785 op_id.type = SLANG_OPER_IDENTIFIER;
1786 op_id.a_id = var->a_name;
1787
1788 /* put the variable into operation's scope */
1789 op_id.locals->variables =
1790 (slang_variable **) _slang_alloc(sizeof(slang_variable *));
1791 if (op_id.locals->variables == NULL) {
1792 slang_operation_destruct(&op_id);
1793 return GL_FALSE;
1794 }
1795 op_id.locals->num_variables = 1;
1796 op_id.locals->variables[0] = var;
1797
1798 /* construct the assignment expression */
1799 if (!slang_operation_construct(&op_assign)) {
1800 op_id.locals->num_variables = 0;
1801 slang_operation_destruct(&op_id);
1802 return GL_FALSE;
1803 }
1804 op_assign.type = SLANG_OPER_ASSIGN;
1805 op_assign.children =
1806 (slang_operation *) _slang_alloc(2 * sizeof(slang_operation));
1807 if (op_assign.children == NULL) {
1808 slang_operation_destruct(&op_assign);
1809 op_id.locals->num_variables = 0;
1810 slang_operation_destruct(&op_id);
1811 return GL_FALSE;
1812 }
1813 op_assign.num_children = 2;
1814 op_assign.children[0] = op_id;
1815 op_assign.children[1] = *var->initializer;
1816
1817 result = 1;
1818
1819 /* carefully destroy the operations */
1820 op_assign.num_children = 0;
1821 _slang_free(op_assign.children);
1822 op_assign.children = NULL;
1823 slang_operation_destruct(&op_assign);
1824 op_id.locals->num_variables = 0;
1825 slang_operation_destruct(&op_id);
1826
1827 if (!result)
1828 return GL_FALSE;
1829
1830 return GL_TRUE;
1831 }
1832
1833 /* init declarator list */
1834 #define DECLARATOR_NONE 0
1835 #define DECLARATOR_NEXT 1
1836
1837 /* variable declaration */
1838 #define VARIABLE_NONE 0
1839 #define VARIABLE_IDENTIFIER 1
1840 #define VARIABLE_INITIALIZER 2
1841 #define VARIABLE_ARRAY_EXPLICIT 3
1842 #define VARIABLE_ARRAY_UNKNOWN 4
1843
1844
1845 /**
1846 * Parse the initializer for a variable declaration.
1847 */
1848 static int
1849 parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
1850 const slang_fully_specified_type * type)
1851 {
1852 slang_variable *var;
1853 slang_atom a_name;
1854
1855 /* empty init declatator (without name, e.g. "float ;") */
1856 if (*C->I++ == VARIABLE_NONE)
1857 return 1;
1858
1859 a_name = parse_identifier(C);
1860
1861 /* check if name is already in this scope */
1862 if (_slang_locate_variable(O->vars, a_name, GL_FALSE)) {
1863 slang_info_log_error(C->L,
1864 "declaration of '%s' conflicts with previous declaration",
1865 (char *) a_name);
1866 RETURN0;
1867 }
1868
1869 /* make room for the new variable and initialize it */
1870 var = slang_variable_scope_grow(O->vars);
1871 if (!var) {
1872 slang_info_log_memory(C->L);
1873 RETURN0;
1874 }
1875
1876 /* copy the declarator type qualifier/etc info, parse the identifier */
1877 var->type.qualifier = type->qualifier;
1878 var->type.centroid = type->centroid;
1879 var->type.precision = type->precision;
1880 var->type.variant = type->variant;
1881 var->a_name = a_name;
1882 if (var->a_name == SLANG_ATOM_NULL)
1883 RETURN0;
1884
1885 switch (*C->I++) {
1886 case VARIABLE_NONE:
1887 /* simple variable declarator - just copy the specifier */
1888 if (!slang_type_specifier_copy(&var->type.specifier, &type->specifier))
1889 RETURN0;
1890 break;
1891 case VARIABLE_INITIALIZER:
1892 /* initialized variable - copy the specifier and parse the expression */
1893 if (!slang_type_specifier_copy(&var->type.specifier, &type->specifier))
1894 RETURN0;
1895 var->initializer =
1896 (slang_operation *) _slang_alloc(sizeof(slang_operation));
1897 if (var->initializer == NULL) {
1898 slang_info_log_memory(C->L);
1899 RETURN0;
1900 }
1901 if (!slang_operation_construct(var->initializer)) {
1902 _slang_free(var->initializer);
1903 var->initializer = NULL;
1904 slang_info_log_memory(C->L);
1905 RETURN0;
1906 }
1907 if (!parse_expression(C, O, var->initializer))
1908 RETURN0;
1909 break;
1910 case VARIABLE_ARRAY_UNKNOWN:
1911 /* unsized array - mark it as array and copy the specifier to
1912 the array element
1913 */
1914 if (!convert_to_array(C, var, &type->specifier))
1915 return GL_FALSE;
1916 break;
1917 case VARIABLE_ARRAY_EXPLICIT:
1918 if (!convert_to_array(C, var, &type->specifier))
1919 return GL_FALSE;
1920 if (!parse_array_len(C, O, &var->array_len))
1921 return GL_FALSE;
1922 break;
1923 default:
1924 RETURN0;
1925 }
1926
1927 if (type->array_len >= 0) {
1928 /* The type was something like "float[4]" */
1929 if (var->array_len != 0) {
1930 slang_info_log_error(C->L, "multi-dimensional arrays not allowed");
1931 RETURN0;
1932 }
1933 convert_to_array(C, var, &type->specifier);
1934 var->array_len = type->array_len;
1935 }
1936
1937 /* allocate global address space for a variable with a known size */
1938 if (C->global_scope
1939 && !(var->type.specifier.type == SLANG_SPEC_ARRAY
1940 && var->array_len == 0)) {
1941 if (!calculate_var_size(C, O, var))
1942 return GL_FALSE;
1943 var->address = slang_var_pool_alloc(O->global_pool, var->size);
1944 }
1945
1946 /* emit code for global var decl */
1947 if (C->global_scope) {
1948 slang_assemble_ctx A;
1949 A.atoms = C->atoms;
1950 A.space.funcs = O->funs;
1951 A.space.structs = O->structs;
1952 A.space.vars = O->vars;
1953 A.program = O->program;
1954 A.vartable = O->vartable;
1955 A.log = C->L;
1956 A.curFuncEndLabel = NULL;
1957 if (!_slang_codegen_global_variable(&A, var, C->type))
1958 RETURN0;
1959 }
1960
1961 /* initialize global variable */
1962 if (C->global_scope) {
1963 if (var->initializer != NULL) {
1964 slang_assemble_ctx A;
1965
1966 A.atoms = C->atoms;
1967 A.space.funcs = O->funs;
1968 A.space.structs = O->structs;
1969 A.space.vars = O->vars;
1970 if (!initialize_global(&A, var))
1971 RETURN0;
1972 }
1973 }
1974 return 1;
1975 }
1976
1977 /**
1978 * Parse a list of variable declarations. Each variable may have an
1979 * initializer.
1980 */
1981 static int
1982 parse_init_declarator_list(slang_parse_ctx * C, slang_output_ctx * O)
1983 {
1984 slang_fully_specified_type type;
1985
1986 /* parse the fully specified type, common to all declarators */
1987 if (!slang_fully_specified_type_construct(&type))
1988 RETURN0;
1989 if (!parse_fully_specified_type(C, O, &type)) {
1990 slang_fully_specified_type_destruct(&type);
1991 RETURN0;
1992 }
1993
1994 /* parse declarators, pass-in the parsed type */
1995 do {
1996 if (!parse_init_declarator(C, O, &type)) {
1997 slang_fully_specified_type_destruct(&type);
1998 RETURN0;
1999 }
2000 }
2001 while (*C->I++ == DECLARATOR_NEXT);
2002
2003 slang_fully_specified_type_destruct(&type);
2004 return 1;
2005 }
2006
2007
2008 /**
2009 * Parse a function definition or declaration.
2010 * \param C parsing context
2011 * \param O output context
2012 * \param definition if non-zero expect a definition, else a declaration
2013 * \param parsed_func_ret returns the parsed function
2014 * \return GL_TRUE if success, GL_FALSE if failure
2015 */
2016 static GLboolean
2017 parse_function(slang_parse_ctx * C, slang_output_ctx * O, int definition,
2018 slang_function ** parsed_func_ret)
2019 {
2020 slang_function parsed_func, *found_func;
2021
2022 /* parse function definition/declaration */
2023 if (!slang_function_construct(&parsed_func))
2024 return GL_FALSE;
2025 if (definition) {
2026 if (!parse_function_definition(C, O, &parsed_func)) {
2027 slang_function_destruct(&parsed_func);
2028 return GL_FALSE;
2029 }
2030 }
2031 else {
2032 if (!parse_function_prototype(C, O, &parsed_func)) {
2033 slang_function_destruct(&parsed_func);
2034 return GL_FALSE;
2035 }
2036 }
2037
2038 /* find a function with a prototype matching the parsed one - only
2039 * the current scope is being searched to allow built-in function
2040 * overriding
2041 */
2042 found_func = slang_function_scope_find(O->funs, &parsed_func, 0);
2043 if (found_func == NULL) {
2044 /* New function, add it to the function list */
2045 O->funs->functions =
2046 (slang_function *) _slang_realloc(O->funs->functions,
2047 O->funs->num_functions
2048 * sizeof(slang_function),
2049 (O->funs->num_functions + 1)
2050 * sizeof(slang_function));
2051 if (O->funs->functions == NULL) {
2052 slang_info_log_memory(C->L);
2053 slang_function_destruct(&parsed_func);
2054 return GL_FALSE;
2055 }
2056 O->funs->functions[O->funs->num_functions] = parsed_func;
2057 O->funs->num_functions++;
2058
2059 /* return the newly parsed function */
2060 *parsed_func_ret = &O->funs->functions[O->funs->num_functions - 1];
2061 }
2062 else {
2063 /* previously defined or declared */
2064 /* TODO: check function return type qualifiers and specifiers */
2065 if (definition) {
2066 if (found_func->body != NULL) {
2067 slang_info_log_error(C->L, "%s: function already has a body.",
2068 slang_atom_pool_id(C->atoms,
2069 parsed_func.header.
2070 a_name));
2071 slang_function_destruct(&parsed_func);
2072 return GL_FALSE;
2073 }
2074
2075 /* destroy the existing function declaration and replace it
2076 * with the new one, remember to save the fixup table
2077 */
2078 parsed_func.fixups = found_func->fixups;
2079 slang_fixup_table_init(&found_func->fixups);
2080 slang_function_destruct(found_func);
2081 *found_func = parsed_func;
2082 }
2083 else {
2084 /* another declaration of the same function prototype - ignore it */
2085 slang_function_destruct(&parsed_func);
2086 }
2087
2088 /* return the found function */
2089 *parsed_func_ret = found_func;
2090 }
2091
2092 return GL_TRUE;
2093 }
2094
2095 /* declaration */
2096 #define DECLARATION_FUNCTION_PROTOTYPE 1
2097 #define DECLARATION_INIT_DECLARATOR_LIST 2
2098
2099 static int
2100 parse_declaration(slang_parse_ctx * C, slang_output_ctx * O)
2101 {
2102 switch (*C->I++) {
2103 case DECLARATION_INIT_DECLARATOR_LIST:
2104 if (!parse_init_declarator_list(C, O))
2105 RETURN0;
2106 break;
2107 case DECLARATION_FUNCTION_PROTOTYPE:
2108 {
2109 slang_function *dummy_func;
2110
2111 if (!parse_function(C, O, 0, &dummy_func))
2112 RETURN0;
2113 }
2114 break;
2115 default:
2116 RETURN0;
2117 }
2118 return 1;
2119 }
2120
2121 static int
2122 parse_default_precision(slang_parse_ctx * C, slang_output_ctx * O)
2123 {
2124 int precision, type;
2125
2126 if (!O->allow_precision) {
2127 slang_info_log_error(C->L, "syntax error at \"precision\"");
2128 RETURN0;
2129 }
2130
2131 precision = *C->I++;
2132 switch (precision) {
2133 case PRECISION_LOW:
2134 case PRECISION_MEDIUM:
2135 case PRECISION_HIGH:
2136 /* OK */
2137 break;
2138 default:
2139 _mesa_problem(NULL, "unexpected precision %d at %s:%d\n",
2140 precision, __FILE__, __LINE__);
2141 RETURN0;
2142 }
2143
2144 type = *C->I++;
2145 switch (type) {
2146 case TYPE_SPECIFIER_FLOAT:
2147 case TYPE_SPECIFIER_INT:
2148 case TYPE_SPECIFIER_SAMPLER1D:
2149 case TYPE_SPECIFIER_SAMPLER2D:
2150 case TYPE_SPECIFIER_SAMPLER3D:
2151 case TYPE_SPECIFIER_SAMPLERCUBE:
2152 case TYPE_SPECIFIER_SAMPLER1DSHADOW:
2153 case TYPE_SPECIFIER_SAMPLER2DSHADOW:
2154 case TYPE_SPECIFIER_SAMPLER2DRECT:
2155 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW:
2156 /* OK */
2157 break;
2158 default:
2159 _mesa_problem(NULL, "unexpected type %d at %s:%d\n",
2160 type, __FILE__, __LINE__);
2161 RETURN0;
2162 }
2163
2164 assert(type < TYPE_SPECIFIER_COUNT);
2165 O->default_precision[type] = precision;
2166
2167 return 1;
2168 }
2169
2170
2171 /**
2172 * Initialize the default precision for all types.
2173 * XXX this info isn't used yet.
2174 */
2175 static void
2176 init_default_precision(slang_output_ctx *O, slang_unit_type type)
2177 {
2178 GLuint i;
2179 for (i = 0; i < TYPE_SPECIFIER_COUNT; i++) {
2180 #if FEATURE_es2_glsl
2181 O->default_precision[i] = PRECISION_LOW;
2182 #else
2183 O->default_precision[i] = PRECISION_HIGH;
2184 #endif
2185 }
2186
2187 if (type == SLANG_UNIT_VERTEX_SHADER) {
2188 O->default_precision[TYPE_SPECIFIER_FLOAT] = PRECISION_HIGH;
2189 O->default_precision[TYPE_SPECIFIER_INT] = PRECISION_HIGH;
2190 }
2191 else {
2192 O->default_precision[TYPE_SPECIFIER_INT] = PRECISION_MEDIUM;
2193 }
2194 }
2195
2196
2197 static int
2198 parse_invariant(slang_parse_ctx * C, slang_output_ctx * O)
2199 {
2200 if (O->allow_invariant) {
2201 slang_atom *a = parse_identifier(C);
2202 /* XXX not doing anything with this var yet */
2203 /*printf("ID: %s\n", (char*) a);*/
2204 return a ? 1 : 0;
2205 }
2206 else {
2207 slang_info_log_error(C->L, "syntax error at \"invariant\"");
2208 RETURN0;
2209 }
2210 }
2211
2212
2213 /* external declaration or default precision specifier */
2214 #define EXTERNAL_NULL 0
2215 #define EXTERNAL_FUNCTION_DEFINITION 1
2216 #define EXTERNAL_DECLARATION 2
2217 #define DEFAULT_PRECISION 3
2218 #define INVARIANT_STMT 4
2219
2220
2221 static GLboolean
2222 parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
2223 struct gl_shader *shader)
2224 {
2225 GET_CURRENT_CONTEXT(ctx);
2226 slang_output_ctx o;
2227 GLboolean success;
2228 GLuint maxRegs;
2229 slang_function *mainFunc = NULL;
2230
2231 if (unit->type == SLANG_UNIT_FRAGMENT_BUILTIN ||
2232 unit->type == SLANG_UNIT_FRAGMENT_SHADER) {
2233 maxRegs = ctx->Const.FragmentProgram.MaxTemps;
2234 }
2235 else {
2236 assert(unit->type == SLANG_UNIT_VERTEX_BUILTIN ||
2237 unit->type == SLANG_UNIT_VERTEX_SHADER);
2238 maxRegs = ctx->Const.VertexProgram.MaxTemps;
2239 }
2240
2241 /* setup output context */
2242 o.funs = &unit->funs;
2243 o.structs = &unit->structs;
2244 o.vars = &unit->vars;
2245 o.global_pool = &unit->object->varpool;
2246 o.program = shader ? shader->Program : NULL;
2247 o.vartable = _slang_new_var_table(maxRegs);
2248 _slang_push_var_table(o.vartable);
2249
2250 /* allow 'invariant' keyword? */
2251 #if FEATURE_es2_glsl
2252 o.allow_invariant = GL_TRUE;
2253 #else
2254 o.allow_invariant = (C->version >= 120) ? GL_TRUE : GL_FALSE;
2255 #endif
2256
2257 /* allow 'centroid' keyword? */
2258 o.allow_centroid = (C->version >= 120) ? GL_TRUE : GL_FALSE;
2259
2260 /* allow 'lowp/mediump/highp' keywords? */
2261 #if FEATURE_es2_glsl
2262 o.allow_precision = GL_TRUE;
2263 #else
2264 o.allow_precision = (C->version >= 120) ? GL_TRUE : GL_FALSE;
2265 #endif
2266 init_default_precision(&o, unit->type);
2267
2268 /* allow 'float[]' keyword? */
2269 o.allow_array_types = (C->version >= 120) ? GL_TRUE : GL_FALSE;
2270
2271 /* parse individual functions and declarations */
2272 while (*C->I != EXTERNAL_NULL) {
2273 switch (*C->I++) {
2274 case EXTERNAL_FUNCTION_DEFINITION:
2275 {
2276 slang_function *func;
2277 success = parse_function(C, &o, 1, &func);
2278 if (success &&
2279 _mesa_strcmp((char *) func->header.a_name, "main") == 0) {
2280 /* found main() */
2281 mainFunc = func;
2282 }
2283 }
2284 break;
2285 case EXTERNAL_DECLARATION:
2286 success = parse_declaration(C, &o);
2287 break;
2288 case DEFAULT_PRECISION:
2289 success = parse_default_precision(C, &o);
2290 break;
2291 case INVARIANT_STMT:
2292 success = parse_invariant(C, &o);
2293 break;
2294 default:
2295 success = GL_FALSE;
2296 }
2297
2298 if (!success) {
2299 /* xxx free codegen */
2300 _slang_pop_var_table(o.vartable);
2301 return GL_FALSE;
2302 }
2303 }
2304 C->I++;
2305
2306 if (mainFunc) {
2307 /* assemble (generate code) for main() */
2308 slang_assemble_ctx A;
2309
2310 A.atoms = C->atoms;
2311 A.space.funcs = o.funs;
2312 A.space.structs = o.structs;
2313 A.space.vars = o.vars;
2314 A.program = o.program;
2315 A.vartable = o.vartable;
2316 A.log = C->L;
2317
2318 /* main() takes no parameters */
2319 if (mainFunc->param_count > 0) {
2320 slang_info_log_error(A.log, "main() takes no arguments");
2321 return GL_FALSE;
2322 }
2323
2324 _slang_codegen_function(&A, mainFunc);
2325
2326 shader->Main = GL_TRUE; /* this shader defines main() */
2327 }
2328
2329 _slang_pop_var_table(o.vartable);
2330 _slang_delete_var_table(o.vartable);
2331
2332 return GL_TRUE;
2333 }
2334
2335 static GLboolean
2336 compile_binary(const byte * prod, slang_code_unit * unit,
2337 GLuint version,
2338 slang_unit_type type, slang_info_log * infolog,
2339 slang_code_unit * builtin, slang_code_unit * downlink,
2340 struct gl_shader *shader)
2341 {
2342 slang_parse_ctx C;
2343
2344 unit->type = type;
2345
2346 /* setup parse context */
2347 C.I = prod;
2348 C.L = infolog;
2349 C.parsing_builtin = (builtin == NULL);
2350 C.global_scope = GL_TRUE;
2351 C.atoms = &unit->object->atompool;
2352 C.type = type;
2353 C.version = version;
2354
2355 if (!check_revision(&C))
2356 return GL_FALSE;
2357
2358 if (downlink != NULL) {
2359 unit->vars.outer_scope = &downlink->vars;
2360 unit->funs.outer_scope = &downlink->funs;
2361 unit->structs.outer_scope = &downlink->structs;
2362 }
2363
2364 /* parse translation unit */
2365 return parse_code_unit(&C, unit, shader);
2366 }
2367
2368 static GLboolean
2369 compile_with_grammar(grammar id, const char *source, slang_code_unit * unit,
2370 slang_unit_type type, slang_info_log * infolog,
2371 slang_code_unit * builtin,
2372 struct gl_shader *shader)
2373 {
2374 byte *prod;
2375 GLuint size, start, version;
2376 slang_string preprocessed;
2377 GLuint maxVersion;
2378
2379 #if FEATURE_ARB_shading_language_120
2380 maxVersion = 120;
2381 #elif FEATURE_es2_glsl
2382 maxVersion = 100;
2383 #else
2384 maxVersion = 110;
2385 #endif
2386
2387 /* First retrieve the version number. */
2388 if (!_slang_preprocess_version(source, &version, &start, infolog))
2389 return GL_FALSE;
2390
2391 if (version > maxVersion) {
2392 slang_info_log_error(infolog,
2393 "language version %.2f is not supported.",
2394 version * 0.01);
2395 return GL_FALSE;
2396 }
2397
2398 /* Now preprocess the source string. */
2399 slang_string_init(&preprocessed);
2400 if (!_slang_preprocess_directives(&preprocessed, &source[start], infolog)) {
2401 slang_string_free(&preprocessed);
2402 slang_info_log_error(infolog, "failed to preprocess the source.");
2403 return GL_FALSE;
2404 }
2405
2406 /* Finally check the syntax and generate its binary representation. */
2407 if (!grammar_fast_check(id,
2408 (const byte *) (slang_string_cstr(&preprocessed)),
2409 &prod, &size, 65536)) {
2410 char buf[1024];
2411 GLint pos;
2412
2413 slang_string_free(&preprocessed);
2414 grammar_get_last_error((byte *) (buf), sizeof(buf), &pos);
2415 slang_info_log_error(infolog, buf);
2416 /* syntax error (possibly in library code) */
2417 #if 0
2418 {
2419 int line, col;
2420 char *s;
2421 s = (char *) _mesa_find_line_column((const GLubyte *) source,
2422 (const GLubyte *) source + pos,
2423 &line, &col);
2424 printf("Error on line %d, col %d: %s\n", line, col, s);
2425 }
2426 #endif
2427 return GL_FALSE;
2428 }
2429 slang_string_free(&preprocessed);
2430
2431 /* Syntax is okay - translate it to internal representation. */
2432 if (!compile_binary(prod, unit, version, type, infolog, builtin,
2433 &builtin[SLANG_BUILTIN_TOTAL - 1],
2434 shader)) {
2435 grammar_alloc_free(prod);
2436 return GL_FALSE;
2437 }
2438 grammar_alloc_free(prod);
2439 return GL_TRUE;
2440 }
2441
2442 LONGSTRING static const char *slang_shader_syn =
2443 #include "library/slang_shader_syn.h"
2444 ;
2445
2446 static const byte slang_core_gc[] = {
2447 #include "library/slang_core_gc.h"
2448 };
2449
2450 static const byte slang_120_core_gc[] = {
2451 #include "library/slang_120_core_gc.h"
2452 };
2453
2454 static const byte slang_120_fragment_gc[] = {
2455 #include "library/slang_builtin_120_fragment_gc.h"
2456 };
2457
2458 static const byte slang_common_builtin_gc[] = {
2459 #include "library/slang_common_builtin_gc.h"
2460 };
2461
2462 static const byte slang_fragment_builtin_gc[] = {
2463 #include "library/slang_fragment_builtin_gc.h"
2464 };
2465
2466 static const byte slang_vertex_builtin_gc[] = {
2467 #include "library/slang_vertex_builtin_gc.h"
2468 };
2469
2470 static GLboolean
2471 compile_object(grammar * id, const char *source, slang_code_object * object,
2472 slang_unit_type type, slang_info_log * infolog,
2473 struct gl_shader *shader)
2474 {
2475 slang_code_unit *builtins = NULL;
2476 GLuint base_version = 110;
2477
2478 /* load GLSL grammar */
2479 *id = grammar_load_from_text((const byte *) (slang_shader_syn));
2480 if (*id == 0) {
2481 byte buf[1024];
2482 int pos;
2483
2484 grammar_get_last_error(buf, 1024, &pos);
2485 slang_info_log_error(infolog, (const char *) (buf));
2486 return GL_FALSE;
2487 }
2488
2489 /* set shader type - the syntax is slightly different for different shaders */
2490 if (type == SLANG_UNIT_FRAGMENT_SHADER
2491 || type == SLANG_UNIT_FRAGMENT_BUILTIN)
2492 grammar_set_reg8(*id, (const byte *) "shader_type", 1);
2493 else
2494 grammar_set_reg8(*id, (const byte *) "shader_type", 2);
2495
2496 /* enable language extensions */
2497 grammar_set_reg8(*id, (const byte *) "parsing_builtin", 1);
2498
2499 /* if parsing user-specified shader, load built-in library */
2500 if (type == SLANG_UNIT_FRAGMENT_SHADER || type == SLANG_UNIT_VERTEX_SHADER) {
2501 /* compile core functionality first */
2502 if (!compile_binary(slang_core_gc,
2503 &object->builtin[SLANG_BUILTIN_CORE],
2504 base_version,
2505 SLANG_UNIT_FRAGMENT_BUILTIN, infolog,
2506 NULL, NULL, NULL))
2507 return GL_FALSE;
2508
2509 #if FEATURE_ARB_shading_language_120
2510 if (!compile_binary(slang_120_core_gc,
2511 &object->builtin[SLANG_BUILTIN_120_CORE],
2512 120,
2513 SLANG_UNIT_FRAGMENT_BUILTIN, infolog,
2514 NULL, &object->builtin[SLANG_BUILTIN_CORE], NULL))
2515 return GL_FALSE;
2516 #endif
2517
2518 /* compile common functions and variables, link to core */
2519 if (!compile_binary(slang_common_builtin_gc,
2520 &object->builtin[SLANG_BUILTIN_COMMON],
2521 #if FEATURE_ARB_shading_language_120
2522 120,
2523 #else
2524 base_version,
2525 #endif
2526 SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
2527 #if FEATURE_ARB_shading_language_120
2528 &object->builtin[SLANG_BUILTIN_120_CORE],
2529 #else
2530 &object->builtin[SLANG_BUILTIN_CORE],
2531 #endif
2532 NULL))
2533 return GL_FALSE;
2534
2535 /* compile target-specific functions and variables, link to common */
2536 if (type == SLANG_UNIT_FRAGMENT_SHADER) {
2537 if (!compile_binary(slang_fragment_builtin_gc,
2538 &object->builtin[SLANG_BUILTIN_TARGET],
2539 base_version,
2540 SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
2541 &object->builtin[SLANG_BUILTIN_COMMON], NULL))
2542 return GL_FALSE;
2543 #if FEATURE_ARB_shading_language_120
2544 if (!compile_binary(slang_120_fragment_gc,
2545 &object->builtin[SLANG_BUILTIN_TARGET],
2546 120,
2547 SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
2548 &object->builtin[SLANG_BUILTIN_COMMON], NULL))
2549 return GL_FALSE;
2550 #endif
2551 }
2552 else if (type == SLANG_UNIT_VERTEX_SHADER) {
2553 if (!compile_binary(slang_vertex_builtin_gc,
2554 &object->builtin[SLANG_BUILTIN_TARGET],
2555 base_version,
2556 SLANG_UNIT_VERTEX_BUILTIN, infolog, NULL,
2557 &object->builtin[SLANG_BUILTIN_COMMON], NULL))
2558 return GL_FALSE;
2559 }
2560
2561 /* disable language extensions */
2562 #if NEW_SLANG /* allow-built-ins */
2563 grammar_set_reg8(*id, (const byte *) "parsing_builtin", 1);
2564 #else
2565 grammar_set_reg8(*id, (const byte *) "parsing_builtin", 0);
2566 #endif
2567 builtins = object->builtin;
2568 }
2569
2570 /* compile the actual shader - pass-in built-in library for external shader */
2571 return compile_with_grammar(*id, source, &object->unit, type, infolog,
2572 builtins, shader);
2573 }
2574
2575
2576 static GLboolean
2577 compile_shader(GLcontext *ctx, slang_code_object * object,
2578 slang_unit_type type, slang_info_log * infolog,
2579 struct gl_shader *shader)
2580 {
2581 GLboolean success;
2582 grammar id = 0;
2583
2584 #if 0 /* for debug */
2585 _mesa_printf("********* COMPILE SHADER ***********\n");
2586 _mesa_printf("%s\n", shader->Source);
2587 _mesa_printf("************************************\n");
2588 #endif
2589
2590 assert(shader->Program);
2591
2592 _slang_code_object_dtr(object);
2593 _slang_code_object_ctr(object);
2594
2595 success = compile_object(&id, shader->Source, object, type, infolog, shader);
2596 if (id != 0)
2597 grammar_destroy(id);
2598 if (!success)
2599 return GL_FALSE;
2600
2601 return GL_TRUE;
2602 }
2603
2604
2605
2606 GLboolean
2607 _slang_compile(GLcontext *ctx, struct gl_shader *shader)
2608 {
2609 GLboolean success;
2610 slang_info_log info_log;
2611 slang_code_object obj;
2612 slang_unit_type type;
2613
2614 if (shader->Type == GL_VERTEX_SHADER) {
2615 type = SLANG_UNIT_VERTEX_SHADER;
2616 }
2617 else {
2618 assert(shader->Type == GL_FRAGMENT_SHADER);
2619 type = SLANG_UNIT_FRAGMENT_SHADER;
2620 }
2621
2622 if (!shader->Source)
2623 return GL_FALSE;
2624
2625 ctx->Shader.MemPool = _slang_new_mempool(1024*1024);
2626
2627 shader->Main = GL_FALSE;
2628
2629 if (!shader->Program) {
2630 GLenum progTarget;
2631 if (shader->Type == GL_VERTEX_SHADER)
2632 progTarget = GL_VERTEX_PROGRAM_ARB;
2633 else
2634 progTarget = GL_FRAGMENT_PROGRAM_ARB;
2635 shader->Program = ctx->Driver.NewProgram(ctx, progTarget, 1);
2636 shader->Program->Parameters = _mesa_new_parameter_list();
2637 shader->Program->Varying = _mesa_new_parameter_list();
2638 shader->Program->Attributes = _mesa_new_parameter_list();
2639 }
2640
2641 slang_info_log_construct(&info_log);
2642 _slang_code_object_ctr(&obj);
2643
2644 success = compile_shader(ctx, &obj, type, &info_log, shader);
2645
2646 /* free shader's prev info log */
2647 if (shader->InfoLog) {
2648 _mesa_free(shader->InfoLog);
2649 shader->InfoLog = NULL;
2650 }
2651
2652 if (info_log.text) {
2653 /* copy info-log string to shader object */
2654 shader->InfoLog = _mesa_strdup(info_log.text);
2655 }
2656
2657 if (info_log.error_flag) {
2658 success = GL_FALSE;
2659 }
2660
2661 slang_info_log_destruct(&info_log);
2662 _slang_code_object_dtr(&obj);
2663
2664 _slang_delete_mempool((slang_mempool *) ctx->Shader.MemPool);
2665 ctx->Shader.MemPool = NULL;
2666
2667 /* remove any reads of output registers */
2668 #if 0
2669 printf("Pre-remove output reads:\n");
2670 _mesa_print_program(shader->Program);
2671 #endif
2672 _mesa_remove_output_reads(shader->Program, PROGRAM_OUTPUT);
2673 if (shader->Type == GL_VERTEX_SHADER) {
2674 /* and remove writes to varying vars in vertex programs */
2675 _mesa_remove_output_reads(shader->Program, PROGRAM_VARYING);
2676 }
2677 #if 0
2678 printf("Post-remove output reads:\n");
2679 _mesa_print_program(shader->Program);
2680 #endif
2681
2682 return success;
2683 }
2684