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