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