1a2c39104b82650e1c3a023e2b2800a7aa40e2bc
[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 += 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 += 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 = _mesa_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((strlen(integral) +
302 strlen(fractional) +
303 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 /* Layout qualifiers */
640 #define LAYOUT_QUALIFIER_NONE 0
641 #define LAYOUT_QUALIFIER_UPPER_LEFT 1
642 #define LAYOUT_QUALIFIER_PIXEL_CENTER_INTEGER 2
643
644 static int
645 parse_layout_qualifiers(slang_parse_ctx * C, slang_layout_qualifier *layout)
646 {
647 *layout = 0x0;
648
649 /* the layout qualifiers come as a list of LAYOUT_QUALIFER_x tokens,
650 * terminated by LAYOUT_QUALIFIER_NONE.
651 */
652 while (1) {
653 GLuint c = *C->I++;
654 switch (c) {
655 case LAYOUT_QUALIFIER_NONE:
656 /* end of list of qualifiers */
657 return 1;
658 case LAYOUT_QUALIFIER_UPPER_LEFT:
659 *layout |= SLANG_LAYOUT_UPPER_LEFT_BIT;
660 break;
661 case LAYOUT_QUALIFIER_PIXEL_CENTER_INTEGER:
662 *layout |= SLANG_LAYOUT_PIXEL_CENTER_INTEGER_BIT;
663 break;
664 default:
665 assert(0 && "Bad layout qualifier");
666 }
667 }
668 }
669
670
671 /* type qualifier */
672 #define TYPE_QUALIFIER_NONE 0
673 #define TYPE_QUALIFIER_CONST 1
674 #define TYPE_QUALIFIER_ATTRIBUTE 2
675 #define TYPE_QUALIFIER_VARYING 3
676 #define TYPE_QUALIFIER_UNIFORM 4
677 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
678 #define TYPE_QUALIFIER_FIXEDINPUT 6
679
680 static int
681 parse_type_qualifier(slang_parse_ctx * C, slang_type_qualifier * qual)
682 {
683 GLuint qualifier = *C->I++;
684 switch (qualifier) {
685 case TYPE_QUALIFIER_NONE:
686 *qual = SLANG_QUAL_NONE;
687 break;
688 case TYPE_QUALIFIER_CONST:
689 *qual = SLANG_QUAL_CONST;
690 break;
691 case TYPE_QUALIFIER_ATTRIBUTE:
692 *qual = SLANG_QUAL_ATTRIBUTE;
693 break;
694 case TYPE_QUALIFIER_VARYING:
695 *qual = SLANG_QUAL_VARYING;
696 break;
697 case TYPE_QUALIFIER_UNIFORM:
698 *qual = SLANG_QUAL_UNIFORM;
699 break;
700 case TYPE_QUALIFIER_FIXEDOUTPUT:
701 *qual = SLANG_QUAL_FIXEDOUTPUT;
702 break;
703 case TYPE_QUALIFIER_FIXEDINPUT:
704 *qual = SLANG_QUAL_FIXEDINPUT;
705 break;
706 default:
707 RETURN0;
708 }
709 return 1;
710 }
711
712 /* type specifier */
713 #define TYPE_SPECIFIER_VOID 0
714 #define TYPE_SPECIFIER_BOOL 1
715 #define TYPE_SPECIFIER_BVEC2 2
716 #define TYPE_SPECIFIER_BVEC3 3
717 #define TYPE_SPECIFIER_BVEC4 4
718 #define TYPE_SPECIFIER_INT 5
719 #define TYPE_SPECIFIER_IVEC2 6
720 #define TYPE_SPECIFIER_IVEC3 7
721 #define TYPE_SPECIFIER_IVEC4 8
722 #define TYPE_SPECIFIER_FLOAT 9
723 #define TYPE_SPECIFIER_VEC2 10
724 #define TYPE_SPECIFIER_VEC3 11
725 #define TYPE_SPECIFIER_VEC4 12
726 #define TYPE_SPECIFIER_MAT2 13
727 #define TYPE_SPECIFIER_MAT3 14
728 #define TYPE_SPECIFIER_MAT4 15
729 #define TYPE_SPECIFIER_SAMPLER1D 16
730 #define TYPE_SPECIFIER_SAMPLER2D 17
731 #define TYPE_SPECIFIER_SAMPLER3D 18
732 #define TYPE_SPECIFIER_SAMPLERCUBE 19
733 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
734 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
735 #define TYPE_SPECIFIER_SAMPLER2DRECT 22
736 #define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
737 #define TYPE_SPECIFIER_STRUCT 24
738 #define TYPE_SPECIFIER_TYPENAME 25
739 #define TYPE_SPECIFIER_MAT23 26
740 #define TYPE_SPECIFIER_MAT32 27
741 #define TYPE_SPECIFIER_MAT24 28
742 #define TYPE_SPECIFIER_MAT42 29
743 #define TYPE_SPECIFIER_MAT34 30
744 #define TYPE_SPECIFIER_MAT43 31
745 #define TYPE_SPECIFIER_COUNT 32
746
747 static int
748 parse_type_specifier(slang_parse_ctx * C, slang_output_ctx * O,
749 slang_type_specifier * spec)
750 {
751 switch (*C->I++) {
752 case TYPE_SPECIFIER_VOID:
753 spec->type = SLANG_SPEC_VOID;
754 break;
755 case TYPE_SPECIFIER_BOOL:
756 spec->type = SLANG_SPEC_BOOL;
757 break;
758 case TYPE_SPECIFIER_BVEC2:
759 spec->type = SLANG_SPEC_BVEC2;
760 break;
761 case TYPE_SPECIFIER_BVEC3:
762 spec->type = SLANG_SPEC_BVEC3;
763 break;
764 case TYPE_SPECIFIER_BVEC4:
765 spec->type = SLANG_SPEC_BVEC4;
766 break;
767 case TYPE_SPECIFIER_INT:
768 spec->type = SLANG_SPEC_INT;
769 break;
770 case TYPE_SPECIFIER_IVEC2:
771 spec->type = SLANG_SPEC_IVEC2;
772 break;
773 case TYPE_SPECIFIER_IVEC3:
774 spec->type = SLANG_SPEC_IVEC3;
775 break;
776 case TYPE_SPECIFIER_IVEC4:
777 spec->type = SLANG_SPEC_IVEC4;
778 break;
779 case TYPE_SPECIFIER_FLOAT:
780 spec->type = SLANG_SPEC_FLOAT;
781 break;
782 case TYPE_SPECIFIER_VEC2:
783 spec->type = SLANG_SPEC_VEC2;
784 break;
785 case TYPE_SPECIFIER_VEC3:
786 spec->type = SLANG_SPEC_VEC3;
787 break;
788 case TYPE_SPECIFIER_VEC4:
789 spec->type = SLANG_SPEC_VEC4;
790 break;
791 case TYPE_SPECIFIER_MAT2:
792 spec->type = SLANG_SPEC_MAT2;
793 break;
794 case TYPE_SPECIFIER_MAT3:
795 spec->type = SLANG_SPEC_MAT3;
796 break;
797 case TYPE_SPECIFIER_MAT4:
798 spec->type = SLANG_SPEC_MAT4;
799 break;
800 case TYPE_SPECIFIER_MAT23:
801 spec->type = SLANG_SPEC_MAT23;
802 break;
803 case TYPE_SPECIFIER_MAT32:
804 spec->type = SLANG_SPEC_MAT32;
805 break;
806 case TYPE_SPECIFIER_MAT24:
807 spec->type = SLANG_SPEC_MAT24;
808 break;
809 case TYPE_SPECIFIER_MAT42:
810 spec->type = SLANG_SPEC_MAT42;
811 break;
812 case TYPE_SPECIFIER_MAT34:
813 spec->type = SLANG_SPEC_MAT34;
814 break;
815 case TYPE_SPECIFIER_MAT43:
816 spec->type = SLANG_SPEC_MAT43;
817 break;
818 case TYPE_SPECIFIER_SAMPLER1D:
819 spec->type = SLANG_SPEC_SAMPLER1D;
820 break;
821 case TYPE_SPECIFIER_SAMPLER2D:
822 spec->type = SLANG_SPEC_SAMPLER2D;
823 break;
824 case TYPE_SPECIFIER_SAMPLER3D:
825 spec->type = SLANG_SPEC_SAMPLER3D;
826 break;
827 case TYPE_SPECIFIER_SAMPLERCUBE:
828 spec->type = SLANG_SPEC_SAMPLERCUBE;
829 break;
830 case TYPE_SPECIFIER_SAMPLER2DRECT:
831 spec->type = SLANG_SPEC_SAMPLER2DRECT;
832 break;
833 case TYPE_SPECIFIER_SAMPLER1DSHADOW:
834 spec->type = SLANG_SPEC_SAMPLER1DSHADOW;
835 break;
836 case TYPE_SPECIFIER_SAMPLER2DSHADOW:
837 spec->type = SLANG_SPEC_SAMPLER2DSHADOW;
838 break;
839 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW:
840 spec->type = SLANG_SPEC_SAMPLER2DRECTSHADOW;
841 break;
842 case TYPE_SPECIFIER_STRUCT:
843 spec->type = SLANG_SPEC_STRUCT;
844 if (!parse_struct(C, O, &spec->_struct))
845 RETURN0;
846 break;
847 case TYPE_SPECIFIER_TYPENAME:
848 spec->type = SLANG_SPEC_STRUCT;
849 {
850 slang_atom a_name;
851 slang_struct *stru;
852
853 a_name = parse_identifier(C);
854 if (a_name == NULL)
855 RETURN0;
856
857 stru = slang_struct_scope_find(O->structs, a_name, 1);
858 if (stru == NULL) {
859 slang_info_log_error(C->L, "undeclared type name '%s'",
860 slang_atom_pool_id(C->atoms, a_name));
861 RETURN0;
862 }
863
864 spec->_struct = (slang_struct *) _slang_alloc(sizeof(slang_struct));
865 if (spec->_struct == NULL) {
866 slang_info_log_memory(C->L);
867 RETURN0;
868 }
869 if (!slang_struct_construct(spec->_struct)) {
870 _slang_free(spec->_struct);
871 spec->_struct = NULL;
872 RETURN0;
873 }
874 if (!slang_struct_copy(spec->_struct, stru))
875 RETURN0;
876 }
877 break;
878 default:
879 RETURN0;
880 }
881 return 1;
882 }
883
884 #define TYPE_SPECIFIER_NONARRAY 0
885 #define TYPE_SPECIFIER_ARRAY 1
886
887 static int
888 parse_type_array_size(slang_parse_ctx *C,
889 slang_output_ctx *O,
890 GLint *array_len)
891 {
892 GLuint size;
893
894 switch (*C->I++) {
895 case TYPE_SPECIFIER_NONARRAY:
896 *array_len = -1; /* -1 = not an array */
897 break;
898 case TYPE_SPECIFIER_ARRAY:
899 if (!parse_array_len(C, O, &size))
900 RETURN0;
901 *array_len = (GLint) size;
902 break;
903 default:
904 assert(0);
905 RETURN0;
906 }
907 return 1;
908 }
909
910 #define PRECISION_DEFAULT 0
911 #define PRECISION_LOW 1
912 #define PRECISION_MEDIUM 2
913 #define PRECISION_HIGH 3
914
915 static int
916 parse_type_precision(slang_parse_ctx *C,
917 slang_type_precision *precision)
918 {
919 GLint prec = *C->I++;
920 switch (prec) {
921 case PRECISION_DEFAULT:
922 *precision = SLANG_PREC_DEFAULT;
923 return 1;
924 case PRECISION_LOW:
925 *precision = SLANG_PREC_LOW;
926 return 1;
927 case PRECISION_MEDIUM:
928 *precision = SLANG_PREC_MEDIUM;
929 return 1;
930 case PRECISION_HIGH:
931 *precision = SLANG_PREC_HIGH;
932 return 1;
933 default:
934 RETURN0;
935 }
936 }
937
938 static int
939 parse_fully_specified_type(slang_parse_ctx * C, slang_output_ctx * O,
940 slang_fully_specified_type * type)
941 {
942 if (!parse_layout_qualifiers(C, &type->layout))
943 RETURN0;
944
945 if (!parse_type_variant(C, &type->variant))
946 RETURN0;
947
948 if (!parse_type_centroid(C, &type->centroid))
949 RETURN0;
950
951 if (!parse_type_qualifier(C, &type->qualifier))
952 RETURN0;
953
954 if (!parse_type_precision(C, &type->precision))
955 RETURN0;
956
957 if (!parse_type_specifier(C, O, &type->specifier))
958 RETURN0;
959
960 if (!parse_type_array_size(C, O, &type->array_len))
961 RETURN0;
962
963 if (!O->allow_invariant && type->variant == SLANG_INVARIANT) {
964 slang_info_log_error(C->L,
965 "'invariant' keyword not allowed (perhaps set #version 120)");
966 RETURN0;
967 }
968
969 if (!O->allow_centroid && type->centroid == SLANG_CENTROID) {
970 slang_info_log_error(C->L,
971 "'centroid' keyword not allowed (perhaps set #version 120)");
972 RETURN0;
973 }
974 else if (type->centroid == SLANG_CENTROID &&
975 type->qualifier != SLANG_QUAL_VARYING) {
976 slang_info_log_error(C->L,
977 "'centroid' keyword only allowed for varying vars");
978 RETURN0;
979 }
980
981
982 /* need this?
983 if (type->qualifier != SLANG_QUAL_VARYING &&
984 type->variant == SLANG_INVARIANT) {
985 slang_info_log_error(C->L,
986 "invariant qualifer only allowed for varying vars");
987 RETURN0;
988 }
989 */
990
991 if (O->allow_precision) {
992 if (type->precision == SLANG_PREC_DEFAULT) {
993 assert(type->specifier.type < TYPE_SPECIFIER_COUNT);
994 /* use the default precision for this datatype */
995 type->precision = O->default_precision[type->specifier.type];
996 }
997 }
998 else {
999 /* only default is allowed */
1000 if (type->precision != SLANG_PREC_DEFAULT) {
1001 slang_info_log_error(C->L, "precision qualifiers not allowed");
1002 RETURN0;
1003 }
1004 }
1005
1006 if (!O->allow_array_types && type->array_len >= 0) {
1007 slang_info_log_error(C->L, "first-class array types not allowed");
1008 RETURN0;
1009 }
1010
1011 if (type->array_len >= 0) {
1012 /* convert type to array type (ex: convert "int" to "array of int" */
1013 promote_type_to_array(C, type, type->array_len);
1014 }
1015
1016 return 1;
1017 }
1018
1019 /* operation */
1020 #define OP_END 0
1021 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
1022 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
1023 #define OP_DECLARE 3
1024 #define OP_ASM 4
1025 #define OP_BREAK 5
1026 #define OP_CONTINUE 6
1027 #define OP_DISCARD 7
1028 #define OP_RETURN 8
1029 #define OP_EXPRESSION 9
1030 #define OP_IF 10
1031 #define OP_WHILE 11
1032 #define OP_DO 12
1033 #define OP_FOR 13
1034 #define OP_PUSH_VOID 14
1035 #define OP_PUSH_BOOL 15
1036 #define OP_PUSH_INT 16
1037 #define OP_PUSH_FLOAT 17
1038 #define OP_PUSH_IDENTIFIER 18
1039 #define OP_SEQUENCE 19
1040 #define OP_ASSIGN 20
1041 #define OP_ADDASSIGN 21
1042 #define OP_SUBASSIGN 22
1043 #define OP_MULASSIGN 23
1044 #define OP_DIVASSIGN 24
1045 /*#define OP_MODASSIGN 25*/
1046 /*#define OP_LSHASSIGN 26*/
1047 /*#define OP_RSHASSIGN 27*/
1048 /*#define OP_ORASSIGN 28*/
1049 /*#define OP_XORASSIGN 29*/
1050 /*#define OP_ANDASSIGN 30*/
1051 #define OP_SELECT 31
1052 #define OP_LOGICALOR 32
1053 #define OP_LOGICALXOR 33
1054 #define OP_LOGICALAND 34
1055 /*#define OP_BITOR 35*/
1056 /*#define OP_BITXOR 36*/
1057 /*#define OP_BITAND 37*/
1058 #define OP_EQUAL 38
1059 #define OP_NOTEQUAL 39
1060 #define OP_LESS 40
1061 #define OP_GREATER 41
1062 #define OP_LESSEQUAL 42
1063 #define OP_GREATEREQUAL 43
1064 /*#define OP_LSHIFT 44*/
1065 /*#define OP_RSHIFT 45*/
1066 #define OP_ADD 46
1067 #define OP_SUBTRACT 47
1068 #define OP_MULTIPLY 48
1069 #define OP_DIVIDE 49
1070 /*#define OP_MODULUS 50*/
1071 #define OP_PREINCREMENT 51
1072 #define OP_PREDECREMENT 52
1073 #define OP_PLUS 53
1074 #define OP_MINUS 54
1075 /*#define OP_COMPLEMENT 55*/
1076 #define OP_NOT 56
1077 #define OP_SUBSCRIPT 57
1078 #define OP_CALL 58
1079 #define OP_FIELD 59
1080 #define OP_POSTINCREMENT 60
1081 #define OP_POSTDECREMENT 61
1082 #define OP_PRECISION 62
1083 #define OP_METHOD 63
1084
1085
1086 /**
1087 * When parsing a compound production, this function is used to parse the
1088 * children.
1089 * For example, a while-loop compound will have two children, the
1090 * while condition expression and the loop body. So, this function will
1091 * be called twice to parse those two sub-expressions.
1092 * \param C the parsing context
1093 * \param O the output context
1094 * \param oper the operation we're parsing
1095 * \param statement indicates whether parsing a statement, or expression
1096 * \return 1 if success, 0 if error
1097 */
1098 static int
1099 parse_child_operation(slang_parse_ctx * C, slang_output_ctx * O,
1100 slang_operation * oper, GLboolean statement)
1101 {
1102 slang_operation *ch;
1103
1104 /* grow child array */
1105 ch = slang_operation_grow(&oper->num_children, &oper->children);
1106 if (statement)
1107 return parse_statement(C, O, ch);
1108 return parse_expression(C, O, ch);
1109 }
1110
1111 static int parse_declaration(slang_parse_ctx * C, slang_output_ctx * O);
1112
1113 static int
1114 parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
1115 slang_operation * oper)
1116 {
1117 int op;
1118
1119 oper->locals->outer_scope = O->vars;
1120
1121 op = *C->I++;
1122 switch (op) {
1123 case OP_BLOCK_BEGIN_NO_NEW_SCOPE:
1124 /* parse child statements, do not create new variable scope */
1125 oper->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE;
1126 while (*C->I != OP_END)
1127 if (!parse_child_operation(C, O, oper, GL_TRUE))
1128 RETURN0;
1129 C->I++;
1130 break;
1131 case OP_BLOCK_BEGIN_NEW_SCOPE:
1132 /* parse child statements, create new variable scope */
1133 {
1134 slang_output_ctx o = *O;
1135
1136 oper->type = SLANG_OPER_BLOCK_NEW_SCOPE;
1137 o.vars = oper->locals;
1138 while (*C->I != OP_END)
1139 if (!parse_child_operation(C, &o, oper, GL_TRUE))
1140 RETURN0;
1141 C->I++;
1142 }
1143 break;
1144 case OP_DECLARE:
1145 /* local variable declaration, individual declarators are stored as
1146 * children identifiers
1147 */
1148 oper->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE;
1149 {
1150 const unsigned int first_var = O->vars->num_variables;
1151
1152 /* parse the declaration, note that there can be zero or more
1153 * than one declarators
1154 */
1155 if (!parse_declaration(C, O))
1156 RETURN0;
1157 if (first_var < O->vars->num_variables) {
1158 const unsigned int num_vars = O->vars->num_variables - first_var;
1159 unsigned int i;
1160 assert(oper->num_children == 0);
1161 oper->num_children = num_vars;
1162 oper->children = slang_operation_new(num_vars);
1163 if (oper->children == NULL) {
1164 slang_info_log_memory(C->L);
1165 RETURN0;
1166 }
1167 for (i = first_var; i < O->vars->num_variables; i++) {
1168 slang_operation *o = &oper->children[i - first_var];
1169 slang_variable *var = O->vars->variables[i];
1170 o->type = SLANG_OPER_VARIABLE_DECL;
1171 o->locals->outer_scope = O->vars;
1172 o->a_id = var->a_name;
1173
1174 /* new/someday...
1175 calculate_var_size(C, O, var);
1176 */
1177
1178 if (!legal_identifier(o->a_id)) {
1179 slang_info_log_error(C->L, "illegal variable name '%s'",
1180 (char *) o->a_id);
1181 RETURN0;
1182 }
1183 }
1184 }
1185 }
1186 break;
1187 case OP_ASM:
1188 /* the __asm statement, parse the mnemonic and all its arguments
1189 * as expressions
1190 */
1191 oper->type = SLANG_OPER_ASM;
1192 oper->a_id = parse_identifier(C);
1193 if (oper->a_id == SLANG_ATOM_NULL)
1194 RETURN0;
1195 while (*C->I != OP_END) {
1196 if (!parse_child_operation(C, O, oper, GL_FALSE))
1197 RETURN0;
1198 }
1199 C->I++;
1200 break;
1201 case OP_BREAK:
1202 oper->type = SLANG_OPER_BREAK;
1203 break;
1204 case OP_CONTINUE:
1205 oper->type = SLANG_OPER_CONTINUE;
1206 break;
1207 case OP_DISCARD:
1208 oper->type = SLANG_OPER_DISCARD;
1209 break;
1210 case OP_RETURN:
1211 oper->type = SLANG_OPER_RETURN;
1212 if (!parse_child_operation(C, O, oper, GL_FALSE))
1213 RETURN0;
1214 break;
1215 case OP_EXPRESSION:
1216 oper->type = SLANG_OPER_EXPRESSION;
1217 if (!parse_child_operation(C, O, oper, GL_FALSE))
1218 RETURN0;
1219 break;
1220 case OP_IF:
1221 oper->type = SLANG_OPER_IF;
1222 if (!parse_child_operation(C, O, oper, GL_FALSE))
1223 RETURN0;
1224 if (!parse_child_operation(C, O, oper, GL_TRUE))
1225 RETURN0;
1226 if (!parse_child_operation(C, O, oper, GL_TRUE))
1227 RETURN0;
1228 break;
1229 case OP_WHILE:
1230 {
1231 slang_output_ctx o = *O;
1232
1233 oper->type = SLANG_OPER_WHILE;
1234 o.vars = oper->locals;
1235 if (!parse_child_operation(C, &o, oper, GL_TRUE))
1236 RETURN0;
1237 if (!parse_child_operation(C, &o, oper, GL_TRUE))
1238 RETURN0;
1239 }
1240 break;
1241 case OP_DO:
1242 oper->type = SLANG_OPER_DO;
1243 if (!parse_child_operation(C, O, oper, GL_TRUE))
1244 RETURN0;
1245 if (!parse_child_operation(C, O, oper, GL_FALSE))
1246 RETURN0;
1247 break;
1248 case OP_FOR:
1249 {
1250 slang_output_ctx o = *O;
1251
1252 oper->type = SLANG_OPER_FOR;
1253 o.vars = oper->locals;
1254 if (!parse_child_operation(C, &o, oper, GL_TRUE))
1255 RETURN0;
1256 if (!parse_child_operation(C, &o, oper, GL_TRUE))
1257 RETURN0;
1258 if (!parse_child_operation(C, &o, oper, GL_FALSE))
1259 RETURN0;
1260 if (!parse_child_operation(C, &o, oper, GL_TRUE))
1261 RETURN0;
1262 }
1263 break;
1264 case OP_PRECISION:
1265 {
1266 /* set default precision for a type in this scope */
1267 /* ignored at this time */
1268 int prec_qual = *C->I++;
1269 int datatype = *C->I++;
1270 (void) prec_qual;
1271 (void) datatype;
1272 }
1273 break;
1274 default:
1275 /*printf("Unexpected operation %d\n", op);*/
1276 RETURN0;
1277 }
1278 return 1;
1279 }
1280
1281 static int
1282 handle_nary_expression(slang_parse_ctx * C, slang_operation * op,
1283 slang_operation ** ops, unsigned int *total_ops,
1284 unsigned int n)
1285 {
1286 unsigned int i;
1287
1288 op->children = slang_operation_new(n);
1289 if (op->children == NULL) {
1290 slang_info_log_memory(C->L);
1291 RETURN0;
1292 }
1293 op->num_children = n;
1294
1295 for (i = 0; i < n; i++) {
1296 slang_operation_destruct(&op->children[i]);
1297 op->children[i] = (*ops)[*total_ops - (n + 1 - i)];
1298 }
1299
1300 (*ops)[*total_ops - (n + 1)] = (*ops)[*total_ops - 1];
1301 *total_ops -= n;
1302
1303 *ops = (slang_operation *)
1304 _slang_realloc(*ops,
1305 (*total_ops + n) * sizeof(slang_operation),
1306 *total_ops * sizeof(slang_operation));
1307 if (*ops == NULL) {
1308 slang_info_log_memory(C->L);
1309 RETURN0;
1310 }
1311 return 1;
1312 }
1313
1314 static int
1315 is_constructor_name(const char *name, slang_atom a_name,
1316 slang_struct_scope * structs)
1317 {
1318 if (slang_type_specifier_type_from_string(name) != SLANG_SPEC_VOID)
1319 return 1;
1320 return slang_struct_scope_find(structs, a_name, 1) != NULL;
1321 }
1322
1323 #define FUNCTION_CALL_NONARRAY 0
1324 #define FUNCTION_CALL_ARRAY 1
1325
1326 static int
1327 parse_expression(slang_parse_ctx * C, slang_output_ctx * O,
1328 slang_operation * oper)
1329 {
1330 slang_operation *ops = NULL;
1331 unsigned int num_ops = 0;
1332 int number;
1333
1334 while (*C->I != OP_END) {
1335 slang_operation *op;
1336 const unsigned int op_code = *C->I++;
1337
1338 /* allocate default operation, becomes a no-op if not used */
1339 ops = (slang_operation *)
1340 _slang_realloc(ops,
1341 num_ops * sizeof(slang_operation),
1342 (num_ops + 1) * sizeof(slang_operation));
1343 if (ops == NULL) {
1344 slang_info_log_memory(C->L);
1345 RETURN0;
1346 }
1347 op = &ops[num_ops];
1348 if (!slang_operation_construct(op)) {
1349 slang_info_log_memory(C->L);
1350 RETURN0;
1351 }
1352 num_ops++;
1353 op->locals->outer_scope = O->vars;
1354
1355 switch (op_code) {
1356 case OP_PUSH_VOID:
1357 op->type = SLANG_OPER_VOID;
1358 break;
1359 case OP_PUSH_BOOL:
1360 op->type = SLANG_OPER_LITERAL_BOOL;
1361 if (!parse_number(C, &number))
1362 RETURN0;
1363 op->literal[0] =
1364 op->literal[1] =
1365 op->literal[2] =
1366 op->literal[3] = (GLfloat) number;
1367 op->literal_size = 1;
1368 break;
1369 case OP_PUSH_INT:
1370 op->type = SLANG_OPER_LITERAL_INT;
1371 if (!parse_number(C, &number))
1372 RETURN0;
1373 op->literal[0] =
1374 op->literal[1] =
1375 op->literal[2] =
1376 op->literal[3] = (GLfloat) number;
1377 op->literal_size = 1;
1378 break;
1379 case OP_PUSH_FLOAT:
1380 op->type = SLANG_OPER_LITERAL_FLOAT;
1381 if (!parse_float(C, &op->literal[0]))
1382 RETURN0;
1383 op->literal[1] =
1384 op->literal[2] =
1385 op->literal[3] = op->literal[0];
1386 op->literal_size = 1;
1387 break;
1388 case OP_PUSH_IDENTIFIER:
1389 op->type = SLANG_OPER_IDENTIFIER;
1390 op->a_id = parse_identifier(C);
1391 if (op->a_id == SLANG_ATOM_NULL)
1392 RETURN0;
1393 break;
1394 case OP_SEQUENCE:
1395 op->type = SLANG_OPER_SEQUENCE;
1396 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1397 RETURN0;
1398 break;
1399 case OP_ASSIGN:
1400 op->type = SLANG_OPER_ASSIGN;
1401 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1402 RETURN0;
1403 break;
1404 case OP_ADDASSIGN:
1405 op->type = SLANG_OPER_ADDASSIGN;
1406 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1407 RETURN0;
1408 break;
1409 case OP_SUBASSIGN:
1410 op->type = SLANG_OPER_SUBASSIGN;
1411 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1412 RETURN0;
1413 break;
1414 case OP_MULASSIGN:
1415 op->type = SLANG_OPER_MULASSIGN;
1416 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1417 RETURN0;
1418 break;
1419 case OP_DIVASSIGN:
1420 op->type = SLANG_OPER_DIVASSIGN;
1421 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1422 RETURN0;
1423 break;
1424 /*case OP_MODASSIGN: */
1425 /*case OP_LSHASSIGN: */
1426 /*case OP_RSHASSIGN: */
1427 /*case OP_ORASSIGN: */
1428 /*case OP_XORASSIGN: */
1429 /*case OP_ANDASSIGN: */
1430 case OP_SELECT:
1431 op->type = SLANG_OPER_SELECT;
1432 if (!handle_nary_expression(C, op, &ops, &num_ops, 3))
1433 RETURN0;
1434 break;
1435 case OP_LOGICALOR:
1436 op->type = SLANG_OPER_LOGICALOR;
1437 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1438 RETURN0;
1439 break;
1440 case OP_LOGICALXOR:
1441 op->type = SLANG_OPER_LOGICALXOR;
1442 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1443 RETURN0;
1444 break;
1445 case OP_LOGICALAND:
1446 op->type = SLANG_OPER_LOGICALAND;
1447 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1448 RETURN0;
1449 break;
1450 /*case OP_BITOR: */
1451 /*case OP_BITXOR: */
1452 /*case OP_BITAND: */
1453 case OP_EQUAL:
1454 op->type = SLANG_OPER_EQUAL;
1455 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1456 RETURN0;
1457 break;
1458 case OP_NOTEQUAL:
1459 op->type = SLANG_OPER_NOTEQUAL;
1460 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1461 RETURN0;
1462 break;
1463 case OP_LESS:
1464 op->type = SLANG_OPER_LESS;
1465 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1466 RETURN0;
1467 break;
1468 case OP_GREATER:
1469 op->type = SLANG_OPER_GREATER;
1470 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1471 RETURN0;
1472 break;
1473 case OP_LESSEQUAL:
1474 op->type = SLANG_OPER_LESSEQUAL;
1475 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1476 RETURN0;
1477 break;
1478 case OP_GREATEREQUAL:
1479 op->type = SLANG_OPER_GREATEREQUAL;
1480 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1481 RETURN0;
1482 break;
1483 /*case OP_LSHIFT: */
1484 /*case OP_RSHIFT: */
1485 case OP_ADD:
1486 op->type = SLANG_OPER_ADD;
1487 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1488 RETURN0;
1489 break;
1490 case OP_SUBTRACT:
1491 op->type = SLANG_OPER_SUBTRACT;
1492 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1493 RETURN0;
1494 break;
1495 case OP_MULTIPLY:
1496 op->type = SLANG_OPER_MULTIPLY;
1497 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1498 RETURN0;
1499 break;
1500 case OP_DIVIDE:
1501 op->type = SLANG_OPER_DIVIDE;
1502 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1503 RETURN0;
1504 break;
1505 /*case OP_MODULUS: */
1506 case OP_PREINCREMENT:
1507 op->type = SLANG_OPER_PREINCREMENT;
1508 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1509 RETURN0;
1510 break;
1511 case OP_PREDECREMENT:
1512 op->type = SLANG_OPER_PREDECREMENT;
1513 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1514 RETURN0;
1515 break;
1516 case OP_PLUS:
1517 op->type = SLANG_OPER_PLUS;
1518 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1519 RETURN0;
1520 break;
1521 case OP_MINUS:
1522 op->type = SLANG_OPER_MINUS;
1523 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1524 RETURN0;
1525 break;
1526 case OP_NOT:
1527 op->type = SLANG_OPER_NOT;
1528 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1529 RETURN0;
1530 break;
1531 /*case OP_COMPLEMENT: */
1532 case OP_SUBSCRIPT:
1533 op->type = SLANG_OPER_SUBSCRIPT;
1534 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1535 RETURN0;
1536 break;
1537 case OP_METHOD:
1538 op->type = SLANG_OPER_METHOD;
1539 op->a_obj = parse_identifier(C);
1540 if (op->a_obj == SLANG_ATOM_NULL)
1541 RETURN0;
1542
1543 op->a_id = parse_identifier(C);
1544 if (op->a_id == SLANG_ATOM_NULL)
1545 RETURN0;
1546
1547 assert(*C->I == OP_END);
1548 C->I++;
1549
1550 while (*C->I != OP_END)
1551 if (!parse_child_operation(C, O, op, GL_FALSE))
1552 RETURN0;
1553 C->I++;
1554 #if 0
1555 /* don't lookup the method (not yet anyway) */
1556 if (!C->parsing_builtin
1557 && !slang_function_scope_find_by_name(O->funs, op->a_id, 1)) {
1558 const char *id;
1559
1560 id = slang_atom_pool_id(C->atoms, op->a_id);
1561 if (!is_constructor_name(id, op->a_id, O->structs)) {
1562 slang_info_log_error(C->L, "%s: undeclared function name.", id);
1563 RETURN0;
1564 }
1565 }
1566 #endif
1567 break;
1568 case OP_CALL:
1569 {
1570 GLboolean array_constructor = GL_FALSE;
1571 GLint array_constructor_size = 0;
1572
1573 op->type = SLANG_OPER_CALL;
1574 op->a_id = parse_identifier(C);
1575 if (op->a_id == SLANG_ATOM_NULL)
1576 RETURN0;
1577 switch (*C->I++) {
1578 case FUNCTION_CALL_NONARRAY:
1579 /* Nothing to do. */
1580 break;
1581 case FUNCTION_CALL_ARRAY:
1582 /* Calling an array constructor. For example:
1583 * float[3](1.1, 2.2, 3.3);
1584 */
1585 if (!O->allow_array_types) {
1586 slang_info_log_error(C->L,
1587 "array constructors not allowed "
1588 "in this GLSL version");
1589 RETURN0;
1590 }
1591 else {
1592 /* parse the array constructor size */
1593 slang_operation array_size;
1594 array_constructor = GL_TRUE;
1595 slang_operation_construct(&array_size);
1596 if (!parse_expression(C, O, &array_size)) {
1597 slang_operation_destruct(&array_size);
1598 return GL_FALSE;
1599 }
1600 if (array_size.type != SLANG_OPER_LITERAL_INT) {
1601 slang_info_log_error(C->L,
1602 "constructor array size is not an integer");
1603 slang_operation_destruct(&array_size);
1604 RETURN0;
1605 }
1606 array_constructor_size = (int) array_size.literal[0];
1607 op->array_constructor = GL_TRUE;
1608 slang_operation_destruct(&array_size);
1609 }
1610 break;
1611 default:
1612 assert(0);
1613 RETURN0;
1614 }
1615 while (*C->I != OP_END)
1616 if (!parse_child_operation(C, O, op, GL_FALSE))
1617 RETURN0;
1618 C->I++;
1619
1620 if (array_constructor &&
1621 array_constructor_size != op->num_children) {
1622 slang_info_log_error(C->L, "number of parameters to array"
1623 " constructor does not match array size");
1624 RETURN0;
1625 }
1626
1627 if (!C->parsing_builtin
1628 && !slang_function_scope_find_by_name(O->funs, op->a_id, 1)) {
1629 const char *id;
1630
1631 id = slang_atom_pool_id(C->atoms, op->a_id);
1632 if (!is_constructor_name(id, op->a_id, O->structs)) {
1633 slang_info_log_error(C->L, "%s: undeclared function name.", id);
1634 RETURN0;
1635 }
1636 }
1637 }
1638 break;
1639 case OP_FIELD:
1640 op->type = SLANG_OPER_FIELD;
1641 op->a_id = parse_identifier(C);
1642 if (op->a_id == SLANG_ATOM_NULL)
1643 RETURN0;
1644 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1645 RETURN0;
1646 break;
1647 case OP_POSTINCREMENT:
1648 op->type = SLANG_OPER_POSTINCREMENT;
1649 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1650 RETURN0;
1651 break;
1652 case OP_POSTDECREMENT:
1653 op->type = SLANG_OPER_POSTDECREMENT;
1654 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1655 RETURN0;
1656 break;
1657 default:
1658 RETURN0;
1659 }
1660 }
1661 C->I++;
1662
1663 slang_operation_destruct(oper);
1664 *oper = *ops; /* struct copy */
1665 _slang_free(ops);
1666
1667 return 1;
1668 }
1669
1670 /* parameter qualifier */
1671 #define PARAM_QUALIFIER_IN 0
1672 #define PARAM_QUALIFIER_OUT 1
1673 #define PARAM_QUALIFIER_INOUT 2
1674
1675 /* function parameter array presence */
1676 #define PARAMETER_ARRAY_NOT_PRESENT 0
1677 #define PARAMETER_ARRAY_PRESENT 1
1678
1679 static int
1680 parse_parameter_declaration(slang_parse_ctx * C, slang_output_ctx * O,
1681 slang_variable * param)
1682 {
1683 int param_qual, precision_qual;
1684
1685 /* parse and validate the parameter's type qualifiers (there can be
1686 * two at most) because not all combinations are valid
1687 */
1688 if (!parse_type_qualifier(C, &param->type.qualifier))
1689 RETURN0;
1690
1691 param_qual = *C->I++;
1692 switch (param_qual) {
1693 case PARAM_QUALIFIER_IN:
1694 if (param->type.qualifier != SLANG_QUAL_CONST
1695 && param->type.qualifier != SLANG_QUAL_NONE) {
1696 slang_info_log_error(C->L, "Invalid type qualifier.");
1697 RETURN0;
1698 }
1699 break;
1700 case PARAM_QUALIFIER_OUT:
1701 if (param->type.qualifier == SLANG_QUAL_NONE)
1702 param->type.qualifier = SLANG_QUAL_OUT;
1703 else {
1704 slang_info_log_error(C->L, "Invalid type qualifier.");
1705 RETURN0;
1706 }
1707 break;
1708 case PARAM_QUALIFIER_INOUT:
1709 if (param->type.qualifier == SLANG_QUAL_NONE)
1710 param->type.qualifier = SLANG_QUAL_INOUT;
1711 else {
1712 slang_info_log_error(C->L, "Invalid type qualifier.");
1713 RETURN0;
1714 }
1715 break;
1716 default:
1717 RETURN0;
1718 }
1719
1720 /* parse precision qualifier (lowp, mediump, highp */
1721 precision_qual = *C->I++;
1722 /* ignored at this time */
1723 (void) precision_qual;
1724
1725 /* parse parameter's type specifier and name */
1726 if (!parse_type_specifier(C, O, &param->type.specifier))
1727 RETURN0;
1728 if (!parse_type_array_size(C, O, &param->type.array_len))
1729 RETURN0;
1730 param->a_name = parse_identifier(C);
1731 if (param->a_name == SLANG_ATOM_NULL)
1732 RETURN0;
1733
1734 /* first-class array
1735 */
1736 if (param->type.array_len >= 0) {
1737 slang_type_specifier p;
1738
1739 slang_type_specifier_ctr(&p);
1740 if (!slang_type_specifier_copy(&p, &param->type.specifier)) {
1741 slang_type_specifier_dtr(&p);
1742 RETURN0;
1743 }
1744 if (!convert_to_array(C, param, &p)) {
1745 slang_type_specifier_dtr(&p);
1746 RETURN0;
1747 }
1748 slang_type_specifier_dtr(&p);
1749 param->array_len = param->type.array_len;
1750 }
1751
1752 /* if the parameter is an array, parse its size (the size must be
1753 * explicitly defined
1754 */
1755 if (*C->I++ == PARAMETER_ARRAY_PRESENT) {
1756 slang_type_specifier p;
1757
1758 if (param->type.array_len >= 0) {
1759 slang_info_log_error(C->L, "multi-dimensional arrays not allowed");
1760 RETURN0;
1761 }
1762 slang_type_specifier_ctr(&p);
1763 if (!slang_type_specifier_copy(&p, &param->type.specifier)) {
1764 slang_type_specifier_dtr(&p);
1765 RETURN0;
1766 }
1767 if (!convert_to_array(C, param, &p)) {
1768 slang_type_specifier_dtr(&p);
1769 RETURN0;
1770 }
1771 slang_type_specifier_dtr(&p);
1772 if (!parse_array_len(C, O, &param->array_len))
1773 RETURN0;
1774 }
1775
1776 #if 0
1777 /* calculate the parameter size */
1778 if (!calculate_var_size(C, O, param))
1779 RETURN0;
1780 #endif
1781 /* TODO: allocate the local address here? */
1782 return 1;
1783 }
1784
1785 /* function type */
1786 #define FUNCTION_ORDINARY 0
1787 #define FUNCTION_CONSTRUCTOR 1
1788 #define FUNCTION_OPERATOR 2
1789
1790 /* function parameter */
1791 #define PARAMETER_NONE 0
1792 #define PARAMETER_NEXT 1
1793
1794 /* operator type */
1795 #define OPERATOR_ADDASSIGN 1
1796 #define OPERATOR_SUBASSIGN 2
1797 #define OPERATOR_MULASSIGN 3
1798 #define OPERATOR_DIVASSIGN 4
1799 /*#define OPERATOR_MODASSIGN 5*/
1800 /*#define OPERATOR_LSHASSIGN 6*/
1801 /*#define OPERATOR_RSHASSIGN 7*/
1802 /*#define OPERATOR_ANDASSIGN 8*/
1803 /*#define OPERATOR_XORASSIGN 9*/
1804 /*#define OPERATOR_ORASSIGN 10*/
1805 #define OPERATOR_LOGICALXOR 11
1806 /*#define OPERATOR_BITOR 12*/
1807 /*#define OPERATOR_BITXOR 13*/
1808 /*#define OPERATOR_BITAND 14*/
1809 #define OPERATOR_LESS 15
1810 #define OPERATOR_GREATER 16
1811 #define OPERATOR_LESSEQUAL 17
1812 #define OPERATOR_GREATEREQUAL 18
1813 /*#define OPERATOR_LSHIFT 19*/
1814 /*#define OPERATOR_RSHIFT 20*/
1815 #define OPERATOR_MULTIPLY 21
1816 #define OPERATOR_DIVIDE 22
1817 /*#define OPERATOR_MODULUS 23*/
1818 #define OPERATOR_INCREMENT 24
1819 #define OPERATOR_DECREMENT 25
1820 #define OPERATOR_PLUS 26
1821 #define OPERATOR_MINUS 27
1822 /*#define OPERATOR_COMPLEMENT 28*/
1823 #define OPERATOR_NOT 29
1824
1825 static const struct
1826 {
1827 unsigned int o_code;
1828 const char *o_name;
1829 } operator_names[] = {
1830 {OPERATOR_INCREMENT, "++"},
1831 {OPERATOR_ADDASSIGN, "+="},
1832 {OPERATOR_PLUS, "+"},
1833 {OPERATOR_DECREMENT, "--"},
1834 {OPERATOR_SUBASSIGN, "-="},
1835 {OPERATOR_MINUS, "-"},
1836 {OPERATOR_NOT, "!"},
1837 {OPERATOR_MULASSIGN, "*="},
1838 {OPERATOR_MULTIPLY, "*"},
1839 {OPERATOR_DIVASSIGN, "/="},
1840 {OPERATOR_DIVIDE, "/"},
1841 {OPERATOR_LESSEQUAL, "<="},
1842 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1843 /*{ OPERATOR_LSHIFT, "<<" }, */
1844 {OPERATOR_LESS, "<"},
1845 {OPERATOR_GREATEREQUAL, ">="},
1846 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1847 /*{ OPERATOR_RSHIFT, ">>" }, */
1848 {OPERATOR_GREATER, ">"},
1849 /*{ OPERATOR_MODASSIGN, "%=" }, */
1850 /*{ OPERATOR_MODULUS, "%" }, */
1851 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1852 /*{ OPERATOR_BITAND, "&" }, */
1853 /*{ OPERATOR_ORASSIGN, "|=" }, */
1854 /*{ OPERATOR_BITOR, "|" }, */
1855 /*{ OPERATOR_COMPLEMENT, "~" }, */
1856 /*{ OPERATOR_XORASSIGN, "^=" }, */
1857 {OPERATOR_LOGICALXOR, "^^"},
1858 /*{ OPERATOR_BITXOR, "^" } */
1859 };
1860
1861 static slang_atom
1862 parse_operator_name(slang_parse_ctx * C)
1863 {
1864 unsigned int i;
1865
1866 for (i = 0; i < sizeof(operator_names) / sizeof(*operator_names); i++) {
1867 if (operator_names[i].o_code == (unsigned int) (*C->I)) {
1868 slang_atom atom =
1869 slang_atom_pool_atom(C->atoms, operator_names[i].o_name);
1870 if (atom == SLANG_ATOM_NULL) {
1871 slang_info_log_memory(C->L);
1872 RETURN0;
1873 }
1874 C->I++;
1875 return atom;
1876 }
1877 }
1878 RETURN0;
1879 }
1880
1881
1882 static int
1883 parse_function_prototype(slang_parse_ctx * C, slang_output_ctx * O,
1884 slang_function * func)
1885 {
1886 GLuint functype;
1887 /* parse function type and name */
1888 if (!parse_fully_specified_type(C, O, &func->header.type))
1889 RETURN0;
1890
1891 functype = *C->I++;
1892 switch (functype) {
1893 case FUNCTION_ORDINARY:
1894 func->kind = SLANG_FUNC_ORDINARY;
1895 func->header.a_name = parse_identifier(C);
1896 if (func->header.a_name == SLANG_ATOM_NULL)
1897 RETURN0;
1898 break;
1899 case FUNCTION_CONSTRUCTOR:
1900 func->kind = SLANG_FUNC_CONSTRUCTOR;
1901 if (func->header.type.specifier.type == SLANG_SPEC_STRUCT)
1902 RETURN0;
1903 func->header.a_name =
1904 slang_atom_pool_atom(C->atoms,
1905 slang_type_specifier_type_to_string
1906 (func->header.type.specifier.type));
1907 if (func->header.a_name == SLANG_ATOM_NULL) {
1908 slang_info_log_memory(C->L);
1909 RETURN0;
1910 }
1911 break;
1912 case FUNCTION_OPERATOR:
1913 func->kind = SLANG_FUNC_OPERATOR;
1914 func->header.a_name = parse_operator_name(C);
1915 if (func->header.a_name == SLANG_ATOM_NULL)
1916 RETURN0;
1917 break;
1918 default:
1919 RETURN0;
1920 }
1921
1922 if (!legal_identifier(func->header.a_name)) {
1923 slang_info_log_error(C->L, "illegal function name '%s'",
1924 (char *) func->header.a_name);
1925 RETURN0;
1926 }
1927
1928 /* parse function parameters */
1929 while (*C->I++ == PARAMETER_NEXT) {
1930 slang_variable *p = slang_variable_scope_grow(func->parameters);
1931 if (!p) {
1932 slang_info_log_memory(C->L);
1933 RETURN0;
1934 }
1935 if (!parse_parameter_declaration(C, O, p))
1936 RETURN0;
1937 }
1938
1939 /* if the function returns a value, append a hidden __retVal 'out'
1940 * parameter that corresponds to the return value.
1941 */
1942 if (_slang_function_has_return_value(func)) {
1943 slang_variable *p = slang_variable_scope_grow(func->parameters);
1944 slang_atom a_retVal = slang_atom_pool_atom(C->atoms, "__retVal");
1945 assert(a_retVal);
1946 p->a_name = a_retVal;
1947 p->type = func->header.type;
1948 p->type.qualifier = SLANG_QUAL_OUT;
1949 }
1950
1951 /* function formal parameters and local variables share the same
1952 * scope, so save the information about param count in a seperate
1953 * place also link the scope to the global variable scope so when a
1954 * given identifier is not found here, the search process continues
1955 * in the global space
1956 */
1957 func->param_count = func->parameters->num_variables;
1958 func->parameters->outer_scope = O->vars;
1959
1960 return 1;
1961 }
1962
1963 static int
1964 parse_function_definition(slang_parse_ctx * C, slang_output_ctx * O,
1965 slang_function * func)
1966 {
1967 slang_output_ctx o = *O;
1968
1969 if (!parse_function_prototype(C, O, func))
1970 RETURN0;
1971
1972 /* create function's body operation */
1973 func->body = (slang_operation *) _slang_alloc(sizeof(slang_operation));
1974 if (func->body == NULL) {
1975 slang_info_log_memory(C->L);
1976 RETURN0;
1977 }
1978 if (!slang_operation_construct(func->body)) {
1979 _slang_free(func->body);
1980 func->body = NULL;
1981 slang_info_log_memory(C->L);
1982 RETURN0;
1983 }
1984
1985 /* to parse the body the parse context is modified in order to
1986 * capture parsed variables into function's local variable scope
1987 */
1988 C->global_scope = GL_FALSE;
1989 o.vars = func->parameters;
1990 if (!parse_statement(C, &o, func->body))
1991 RETURN0;
1992
1993 C->global_scope = GL_TRUE;
1994 return 1;
1995 }
1996
1997 static GLboolean
1998 initialize_global(slang_assemble_ctx * A, slang_variable * var)
1999 {
2000 slang_operation op_id, op_assign;
2001 GLboolean result;
2002
2003 /* construct the left side of assignment */
2004 if (!slang_operation_construct(&op_id))
2005 return GL_FALSE;
2006 op_id.type = SLANG_OPER_IDENTIFIER;
2007 op_id.a_id = var->a_name;
2008
2009 /* put the variable into operation's scope */
2010 op_id.locals->variables =
2011 (slang_variable **) _slang_alloc(sizeof(slang_variable *));
2012 if (op_id.locals->variables == NULL) {
2013 slang_operation_destruct(&op_id);
2014 return GL_FALSE;
2015 }
2016 op_id.locals->num_variables = 1;
2017 op_id.locals->variables[0] = var;
2018
2019 /* construct the assignment expression */
2020 if (!slang_operation_construct(&op_assign)) {
2021 op_id.locals->num_variables = 0;
2022 slang_operation_destruct(&op_id);
2023 return GL_FALSE;
2024 }
2025 op_assign.type = SLANG_OPER_ASSIGN;
2026 op_assign.children =
2027 (slang_operation *) _slang_alloc(2 * sizeof(slang_operation));
2028 if (op_assign.children == NULL) {
2029 slang_operation_destruct(&op_assign);
2030 op_id.locals->num_variables = 0;
2031 slang_operation_destruct(&op_id);
2032 return GL_FALSE;
2033 }
2034 op_assign.num_children = 2;
2035 op_assign.children[0] = op_id;
2036 op_assign.children[1] = *var->initializer;
2037
2038 result = 1;
2039
2040 /* carefully destroy the operations */
2041 op_assign.num_children = 0;
2042 _slang_free(op_assign.children);
2043 op_assign.children = NULL;
2044 slang_operation_destruct(&op_assign);
2045 op_id.locals->num_variables = 0;
2046 slang_operation_destruct(&op_id);
2047
2048 if (!result)
2049 return GL_FALSE;
2050
2051 return GL_TRUE;
2052 }
2053
2054 /* init declarator list */
2055 #define DECLARATOR_NONE 0
2056 #define DECLARATOR_NEXT 1
2057
2058 /* variable declaration */
2059 #define VARIABLE_NONE 0
2060 #define VARIABLE_IDENTIFIER 1
2061 #define VARIABLE_INITIALIZER 2
2062 #define VARIABLE_ARRAY_EXPLICIT 3
2063 #define VARIABLE_ARRAY_UNKNOWN 4
2064
2065
2066 /**
2067 * Check if it's OK to re-declare a variable with the given new type.
2068 * This happens when applying layout qualifiers to gl_FragCoord or
2069 * (re)setting an array size.
2070 * If redeclaration is OK, return a pointer to the incoming variable
2071 * updated with new type info. Else return NULL;
2072 */
2073 static slang_variable *
2074 redeclare_variable(slang_variable *var,
2075 const slang_fully_specified_type *type)
2076 {
2077 if (slang_fully_specified_types_compatible(&var->type, type)) {
2078 /* replace orig var layout with new layout */
2079 var->type.layout = type->layout;
2080
2081 /* XXX there may be other type updates in the future here */
2082
2083 return var;
2084 }
2085 else
2086 return NULL;
2087 }
2088
2089
2090 /**
2091 * Parse the initializer for a variable declaration.
2092 */
2093 static int
2094 parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
2095 const slang_fully_specified_type * type)
2096 {
2097 GET_CURRENT_CONTEXT(ctx); /* a hack */
2098 slang_variable *var = NULL, *prevDecl;
2099 slang_atom a_name;
2100
2101 /* empty init declatator (without name, e.g. "float ;") */
2102 if (*C->I++ == VARIABLE_NONE)
2103 return 1;
2104
2105 a_name = parse_identifier(C);
2106
2107 /* check if name is already in this scope */
2108 prevDecl = _slang_variable_locate(O->vars, a_name, C->global_scope);
2109 if (prevDecl) {
2110 /* A var with this name has already been declared.
2111 * Check if redeclaring the var with a different type/layout is legal.
2112 */
2113 if (C->global_scope) {
2114 var = redeclare_variable(prevDecl, type);
2115 }
2116 if (!var) {
2117 slang_info_log_error(C->L,
2118 "declaration of '%s' conflicts with previous declaration",
2119 (char *) a_name);
2120 RETURN0;
2121 }
2122 }
2123
2124 if (!var) {
2125 /* make room for a new variable and initialize it */
2126 var = slang_variable_scope_grow(O->vars);
2127 if (!var) {
2128 slang_info_log_memory(C->L);
2129 RETURN0;
2130 }
2131
2132 /* copy the declarator type qualifier/etc info, parse the identifier */
2133 var->type.qualifier = type->qualifier;
2134 var->type.centroid = type->centroid;
2135 var->type.precision = type->precision;
2136 var->type.variant = type->variant;
2137 var->type.layout = type->layout;
2138 var->type.array_len = type->array_len;
2139 var->a_name = a_name;
2140 if (var->a_name == SLANG_ATOM_NULL)
2141 RETURN0;
2142 }
2143
2144 switch (*C->I++) {
2145 case VARIABLE_NONE:
2146 /* simple variable declarator - just copy the specifier */
2147 if (!slang_type_specifier_copy(&var->type.specifier, &type->specifier))
2148 RETURN0;
2149 break;
2150 case VARIABLE_INITIALIZER:
2151 /* initialized variable - copy the specifier and parse the expression */
2152 if (0 && type->array_len >= 0) {
2153 /* The type was something like "float[4]" */
2154 convert_to_array(C, var, &type->specifier);
2155 var->array_len = type->array_len;
2156 }
2157 else {
2158 if (!slang_type_specifier_copy(&var->type.specifier, &type->specifier))
2159 RETURN0;
2160 }
2161 var->initializer =
2162 (slang_operation *) _slang_alloc(sizeof(slang_operation));
2163 if (var->initializer == NULL) {
2164 slang_info_log_memory(C->L);
2165 RETURN0;
2166 }
2167 if (!slang_operation_construct(var->initializer)) {
2168 _slang_free(var->initializer);
2169 var->initializer = NULL;
2170 slang_info_log_memory(C->L);
2171 RETURN0;
2172 }
2173 if (!parse_expression(C, O, var->initializer))
2174 RETURN0;
2175 break;
2176 case VARIABLE_ARRAY_UNKNOWN:
2177 /* unsized array - mark it as array and copy the specifier to
2178 * the array element
2179 */
2180 if (type->array_len >= 0) {
2181 slang_info_log_error(C->L, "multi-dimensional arrays not allowed");
2182 RETURN0;
2183 }
2184 if (!convert_to_array(C, var, &type->specifier))
2185 return GL_FALSE;
2186 break;
2187 case VARIABLE_ARRAY_EXPLICIT:
2188 if (type->array_len >= 0) {
2189 /* the user is trying to do something like: float[2] x[3]; */
2190 slang_info_log_error(C->L, "multi-dimensional arrays not allowed");
2191 RETURN0;
2192 }
2193 if (!convert_to_array(C, var, &type->specifier))
2194 return GL_FALSE;
2195 if (!parse_array_len(C, O, &var->array_len))
2196 return GL_FALSE;
2197 break;
2198 default:
2199 RETURN0;
2200 }
2201
2202 /* allocate global address space for a variable with a known size */
2203 if (C->global_scope
2204 && !(var->type.specifier.type == SLANG_SPEC_ARRAY
2205 && var->array_len == 0)) {
2206 if (!calculate_var_size(C, O, var))
2207 return GL_FALSE;
2208 }
2209
2210 /* emit code for global var decl */
2211 if (C->global_scope) {
2212 slang_assemble_ctx A;
2213 memset(&A, 0, sizeof(slang_assemble_ctx));
2214 A.allow_uniform_initializers = C->version > 110;
2215 A.atoms = C->atoms;
2216 A.space.funcs = O->funs;
2217 A.space.structs = O->structs;
2218 A.space.vars = O->vars;
2219 A.program = O->program;
2220 A.pragmas = O->pragmas;
2221 A.vartable = O->vartable;
2222 A.log = C->L;
2223 A.curFuncEndLabel = NULL;
2224 A.EmitContReturn = ctx->Shader.EmitContReturn;
2225 if (!_slang_codegen_global_variable(&A, var, C->type))
2226 RETURN0;
2227 }
2228
2229 /* initialize global variable */
2230 if (C->global_scope) {
2231 if (var->initializer != NULL) {
2232 slang_assemble_ctx A;
2233 memset(&A, 0, sizeof(slang_assemble_ctx));
2234 A.allow_uniform_initializers = C->version > 110;
2235 A.atoms = C->atoms;
2236 A.space.funcs = O->funs;
2237 A.space.structs = O->structs;
2238 A.space.vars = O->vars;
2239 if (!initialize_global(&A, var))
2240 RETURN0;
2241 }
2242 }
2243
2244 if (var->type.qualifier == SLANG_QUAL_FIXEDINPUT &&
2245 var->a_name == slang_atom_pool_atom(C->atoms, "gl_FragCoord")) {
2246 /* set the program's PixelCenterInteger, OriginUpperLeft fields */
2247 struct gl_fragment_program *fragProg =
2248 (struct gl_fragment_program *) O->program;
2249
2250 if (var->type.layout & SLANG_LAYOUT_UPPER_LEFT_BIT) {
2251 fragProg->OriginUpperLeft = GL_TRUE;
2252 }
2253 if (var->type.layout & SLANG_LAYOUT_PIXEL_CENTER_INTEGER_BIT) {
2254 fragProg->PixelCenterInteger = GL_TRUE;
2255 }
2256 }
2257
2258 return 1;
2259 }
2260
2261 /**
2262 * Parse a list of variable declarations. Each variable may have an
2263 * initializer.
2264 */
2265 static int
2266 parse_init_declarator_list(slang_parse_ctx * C, slang_output_ctx * O)
2267 {
2268 slang_fully_specified_type type;
2269
2270 /* parse the fully specified type, common to all declarators */
2271 if (!slang_fully_specified_type_construct(&type))
2272 RETURN0;
2273 if (!parse_fully_specified_type(C, O, &type)) {
2274 slang_fully_specified_type_destruct(&type);
2275 RETURN0;
2276 }
2277
2278 /* parse declarators, pass-in the parsed type */
2279 do {
2280 if (!parse_init_declarator(C, O, &type)) {
2281 slang_fully_specified_type_destruct(&type);
2282 RETURN0;
2283 }
2284 }
2285 while (*C->I++ == DECLARATOR_NEXT);
2286
2287 slang_fully_specified_type_destruct(&type);
2288 return 1;
2289 }
2290
2291
2292 /**
2293 * Parse a function definition or declaration.
2294 * \param C parsing context
2295 * \param O output context
2296 * \param definition if non-zero expect a definition, else a declaration
2297 * \param parsed_func_ret returns the parsed function
2298 * \return GL_TRUE if success, GL_FALSE if failure
2299 */
2300 static GLboolean
2301 parse_function(slang_parse_ctx * C, slang_output_ctx * O, int definition,
2302 slang_function ** parsed_func_ret)
2303 {
2304 slang_function parsed_func, *found_func;
2305
2306 /* parse function definition/declaration */
2307 if (!slang_function_construct(&parsed_func))
2308 return GL_FALSE;
2309 if (definition) {
2310 if (!parse_function_definition(C, O, &parsed_func)) {
2311 slang_function_destruct(&parsed_func);
2312 return GL_FALSE;
2313 }
2314 }
2315 else {
2316 if (!parse_function_prototype(C, O, &parsed_func)) {
2317 slang_function_destruct(&parsed_func);
2318 return GL_FALSE;
2319 }
2320 }
2321
2322 /* find a function with a prototype matching the parsed one - only
2323 * the current scope is being searched to allow built-in function
2324 * overriding
2325 */
2326 found_func = slang_function_scope_find(O->funs, &parsed_func, 0);
2327 if (found_func == NULL) {
2328 /* New function, add it to the function list */
2329 O->funs->functions =
2330 (slang_function *) _slang_realloc(O->funs->functions,
2331 O->funs->num_functions
2332 * sizeof(slang_function),
2333 (O->funs->num_functions + 1)
2334 * sizeof(slang_function));
2335 if (O->funs->functions == NULL) {
2336 /* Make sure that there are no functions marked, as the
2337 * allocation is currently NULL, in order to avoid
2338 * a potental segfault as we clean up later.
2339 */
2340 O->funs->num_functions = 0;
2341
2342 slang_info_log_memory(C->L);
2343 slang_function_destruct(&parsed_func);
2344 return GL_FALSE;
2345 }
2346 O->funs->functions[O->funs->num_functions] = parsed_func;
2347 O->funs->num_functions++;
2348
2349 /* return the newly parsed function */
2350 *parsed_func_ret = &O->funs->functions[O->funs->num_functions - 1];
2351 }
2352 else {
2353 /* previously defined or declared */
2354 /* TODO: check function return type qualifiers and specifiers */
2355 if (definition) {
2356 if (found_func->body != NULL) {
2357 slang_info_log_error(C->L, "%s: function already has a body.",
2358 slang_atom_pool_id(C->atoms,
2359 parsed_func.header.
2360 a_name));
2361 slang_function_destruct(&parsed_func);
2362 return GL_FALSE;
2363 }
2364
2365 /* destroy the existing function declaration and replace it
2366 * with the new one
2367 */
2368 slang_function_destruct(found_func);
2369 *found_func = parsed_func;
2370 }
2371 else {
2372 /* another declaration of the same function prototype - ignore it */
2373 slang_function_destruct(&parsed_func);
2374 }
2375
2376 /* return the found function */
2377 *parsed_func_ret = found_func;
2378 }
2379
2380 return GL_TRUE;
2381 }
2382
2383 /* declaration */
2384 #define DECLARATION_FUNCTION_PROTOTYPE 1
2385 #define DECLARATION_INIT_DECLARATOR_LIST 2
2386
2387 static int
2388 parse_declaration(slang_parse_ctx * C, slang_output_ctx * O)
2389 {
2390 switch (*C->I++) {
2391 case DECLARATION_INIT_DECLARATOR_LIST:
2392 if (!parse_init_declarator_list(C, O))
2393 RETURN0;
2394 break;
2395 case DECLARATION_FUNCTION_PROTOTYPE:
2396 {
2397 slang_function *dummy_func;
2398
2399 if (!parse_function(C, O, 0, &dummy_func))
2400 RETURN0;
2401 }
2402 break;
2403 default:
2404 RETURN0;
2405 }
2406 return 1;
2407 }
2408
2409 static int
2410 parse_default_precision(slang_parse_ctx * C, slang_output_ctx * O)
2411 {
2412 int precision, type;
2413
2414 if (!O->allow_precision) {
2415 slang_info_log_error(C->L, "syntax error at \"precision\"");
2416 RETURN0;
2417 }
2418
2419 precision = *C->I++;
2420 switch (precision) {
2421 case PRECISION_LOW:
2422 case PRECISION_MEDIUM:
2423 case PRECISION_HIGH:
2424 /* OK */
2425 break;
2426 default:
2427 _mesa_problem(NULL, "unexpected precision %d at %s:%d\n",
2428 precision, __FILE__, __LINE__);
2429 RETURN0;
2430 }
2431
2432 type = *C->I++;
2433 switch (type) {
2434 case TYPE_SPECIFIER_FLOAT:
2435 case TYPE_SPECIFIER_INT:
2436 case TYPE_SPECIFIER_SAMPLER1D:
2437 case TYPE_SPECIFIER_SAMPLER2D:
2438 case TYPE_SPECIFIER_SAMPLER3D:
2439 case TYPE_SPECIFIER_SAMPLERCUBE:
2440 case TYPE_SPECIFIER_SAMPLER1DSHADOW:
2441 case TYPE_SPECIFIER_SAMPLER2DSHADOW:
2442 case TYPE_SPECIFIER_SAMPLER2DRECT:
2443 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW:
2444 /* OK */
2445 break;
2446 default:
2447 _mesa_problem(NULL, "unexpected type %d at %s:%d\n",
2448 type, __FILE__, __LINE__);
2449 RETURN0;
2450 }
2451
2452 assert(type < TYPE_SPECIFIER_COUNT);
2453 O->default_precision[type] = precision;
2454
2455 return 1;
2456 }
2457
2458
2459 /**
2460 * Initialize the default precision for all types.
2461 * XXX this info isn't used yet.
2462 */
2463 static void
2464 init_default_precision(slang_output_ctx *O, slang_unit_type type)
2465 {
2466 GLuint i;
2467 for (i = 0; i < TYPE_SPECIFIER_COUNT; i++) {
2468 #if FEATURE_es2_glsl
2469 O->default_precision[i] = PRECISION_LOW;
2470 #else
2471 O->default_precision[i] = PRECISION_HIGH;
2472 #endif
2473 }
2474
2475 if (type == SLANG_UNIT_VERTEX_SHADER) {
2476 O->default_precision[TYPE_SPECIFIER_FLOAT] = PRECISION_HIGH;
2477 O->default_precision[TYPE_SPECIFIER_INT] = PRECISION_HIGH;
2478 }
2479 else {
2480 O->default_precision[TYPE_SPECIFIER_INT] = PRECISION_MEDIUM;
2481 }
2482 }
2483
2484
2485 static int
2486 parse_invariant(slang_parse_ctx * C, slang_output_ctx * O)
2487 {
2488 if (O->allow_invariant) {
2489 slang_atom *a = parse_identifier(C);
2490 /* XXX not doing anything with this var yet */
2491 /*printf("ID: %s\n", (char*) a);*/
2492 return a ? 1 : 0;
2493 }
2494 else {
2495 slang_info_log_error(C->L, "syntax error at \"invariant\"");
2496 RETURN0;
2497 }
2498 }
2499
2500
2501 /* external declaration or default precision specifier */
2502 #define EXTERNAL_NULL 0
2503 #define EXTERNAL_FUNCTION_DEFINITION 1
2504 #define EXTERNAL_DECLARATION 2
2505 #define DEFAULT_PRECISION 3
2506 #define INVARIANT_STMT 4
2507
2508
2509 static GLboolean
2510 parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
2511 struct gl_shader *shader)
2512 {
2513 GET_CURRENT_CONTEXT(ctx);
2514 slang_output_ctx o;
2515 GLboolean success;
2516 GLuint maxRegs;
2517 slang_function *mainFunc = NULL;
2518
2519 if (unit->type == SLANG_UNIT_FRAGMENT_BUILTIN ||
2520 unit->type == SLANG_UNIT_FRAGMENT_SHADER) {
2521 maxRegs = ctx->Const.FragmentProgram.MaxTemps;
2522 }
2523 else {
2524 assert(unit->type == SLANG_UNIT_VERTEX_BUILTIN ||
2525 unit->type == SLANG_UNIT_VERTEX_SHADER);
2526 maxRegs = ctx->Const.VertexProgram.MaxTemps;
2527 }
2528
2529 /* setup output context */
2530 o.funs = &unit->funs;
2531 o.structs = &unit->structs;
2532 o.vars = &unit->vars;
2533 o.program = shader ? shader->Program : NULL;
2534 o.pragmas = shader ? &shader->Pragmas : NULL;
2535 o.vartable = _slang_new_var_table(maxRegs);
2536 _slang_push_var_table(o.vartable);
2537
2538 /* allow 'invariant' keyword? */
2539 #if FEATURE_es2_glsl
2540 o.allow_invariant = GL_TRUE;
2541 #else
2542 o.allow_invariant = (C->version >= 120) ? GL_TRUE : GL_FALSE;
2543 #endif
2544
2545 /* allow 'centroid' keyword? */
2546 o.allow_centroid = (C->version >= 120) ? GL_TRUE : GL_FALSE;
2547
2548 /* allow 'lowp/mediump/highp' keywords? */
2549 #if FEATURE_es2_glsl
2550 o.allow_precision = GL_TRUE;
2551 #else
2552 o.allow_precision = (C->version >= 120) ? GL_TRUE : GL_FALSE;
2553 #endif
2554 init_default_precision(&o, unit->type);
2555
2556 /* allow 'float[]' keyword? */
2557 o.allow_array_types = (C->version >= 120) ? GL_TRUE : GL_FALSE;
2558
2559 /* parse individual functions and declarations */
2560 while (*C->I != EXTERNAL_NULL) {
2561 switch (*C->I++) {
2562 case EXTERNAL_FUNCTION_DEFINITION:
2563 {
2564 slang_function *func;
2565 success = parse_function(C, &o, 1, &func);
2566 if (success && strcmp((char *) func->header.a_name, "main") == 0) {
2567 /* found main() */
2568 mainFunc = func;
2569 }
2570 }
2571 break;
2572 case EXTERNAL_DECLARATION:
2573 success = parse_declaration(C, &o);
2574 break;
2575 case DEFAULT_PRECISION:
2576 success = parse_default_precision(C, &o);
2577 break;
2578 case INVARIANT_STMT:
2579 success = parse_invariant(C, &o);
2580 break;
2581 default:
2582 success = GL_FALSE;
2583 }
2584
2585 if (!success) {
2586 /* xxx free codegen */
2587 _slang_pop_var_table(o.vartable);
2588 return GL_FALSE;
2589 }
2590 }
2591 C->I++;
2592
2593 if (mainFunc) {
2594 /* assemble (generate code) for main() */
2595 slang_assemble_ctx A;
2596 memset(&A, 0, sizeof(slang_assemble_ctx));
2597 A.atoms = C->atoms;
2598 A.space.funcs = o.funs;
2599 A.space.structs = o.structs;
2600 A.space.vars = o.vars;
2601 A.program = o.program;
2602 A.pragmas = &shader->Pragmas;
2603 A.vartable = o.vartable;
2604 A.EmitContReturn = ctx->Shader.EmitContReturn;
2605 A.log = C->L;
2606 A.allow_uniform_initializers = C->version > 110;
2607
2608 /* main() takes no parameters */
2609 if (mainFunc->param_count > 0) {
2610 slang_info_log_error(A.log, "main() takes no arguments");
2611 return GL_FALSE;
2612 }
2613
2614 _slang_codegen_function(&A, mainFunc);
2615
2616 shader->Main = GL_TRUE; /* this shader defines main() */
2617
2618 shader->UnresolvedRefs = A.UnresolvedRefs;
2619 }
2620
2621 _slang_pop_var_table(o.vartable);
2622 _slang_delete_var_table(o.vartable);
2623
2624 return GL_TRUE;
2625 }
2626
2627 static GLboolean
2628 compile_binary(const unsigned char * prod, slang_code_unit * unit,
2629 GLuint version,
2630 slang_unit_type type, slang_info_log * infolog,
2631 slang_code_unit * builtin, slang_code_unit * downlink,
2632 struct gl_shader *shader)
2633 {
2634 slang_parse_ctx C;
2635
2636 unit->type = type;
2637
2638 /* setup parse context */
2639 C.I = prod;
2640 C.L = infolog;
2641 C.parsing_builtin = (builtin == NULL);
2642 C.global_scope = GL_TRUE;
2643 C.atoms = &unit->object->atompool;
2644 C.type = type;
2645 C.version = version;
2646
2647 if (!check_revision(&C))
2648 return GL_FALSE;
2649
2650 if (downlink != NULL) {
2651 unit->vars.outer_scope = &downlink->vars;
2652 unit->funs.outer_scope = &downlink->funs;
2653 unit->structs.outer_scope = &downlink->structs;
2654 }
2655
2656 /* parse translation unit */
2657 return parse_code_unit(&C, unit, shader);
2658 }
2659
2660 static GLboolean
2661 compile_with_grammar(const char *source,
2662 slang_code_unit *unit,
2663 slang_unit_type type,
2664 slang_info_log *infolog,
2665 slang_code_unit *builtin,
2666 struct gl_shader *shader,
2667 struct gl_sl_pragmas *pragmas,
2668 unsigned int shader_type,
2669 unsigned int parsing_builtin)
2670 {
2671 struct sl_pp_purify_options options;
2672 struct sl_pp_context *context;
2673 unsigned char *prod;
2674 GLuint size;
2675 unsigned int version;
2676 unsigned int maxVersion;
2677 int result;
2678 char errmsg[200] = "";
2679
2680 assert(shader_type == 1 || shader_type == 2);
2681
2682 memset(&options, 0, sizeof(options));
2683
2684 context = sl_pp_context_create(source, &options);
2685 if (!context) {
2686 slang_info_log_error(infolog, "out of memory");
2687 return GL_FALSE;
2688 }
2689
2690 if (sl_pp_version(context, &version)) {
2691 slang_info_log_error(infolog, "%s", sl_pp_context_error_message(context));
2692 sl_pp_context_destroy(context);
2693 return GL_FALSE;
2694 }
2695
2696 if (sl_pp_context_add_extension(context, "GL_ARB_draw_buffers") ||
2697 sl_pp_context_add_extension(context, "GL_ARB_texture_rectangle")) {
2698 slang_info_log_error(infolog, "%s", sl_pp_context_error_message(context));
2699 sl_pp_context_destroy(context);
2700 return GL_FALSE;
2701 }
2702
2703 if (type == SLANG_UNIT_FRAGMENT_SHADER) {
2704 sl_pp_context_add_extension(context, "GL_ARB_fragment_coord_conventions");
2705 }
2706
2707
2708 #if FEATURE_es2_glsl
2709 if (sl_pp_context_add_predefined(context, "GL_ES", "1") ||
2710 sl_pp_context_add_predefined(context, "GL_FRAGMENT_PRECISION_HIGH", "1")) {
2711 slang_info_log_error(infolog, "%s", sl_pp_context_error_message(context));
2712 sl_pp_context_destroy(context);
2713 return GL_FALSE;
2714 }
2715 #endif
2716
2717 #if FEATURE_ARB_shading_language_120
2718 maxVersion = 120;
2719 #elif FEATURE_es2_glsl
2720 maxVersion = 100;
2721 #else
2722 maxVersion = 110;
2723 #endif
2724
2725 if (version > maxVersion ||
2726 (version != 100 && version != 110 && version != 120)) {
2727 slang_info_log_error(infolog,
2728 "language version %.2f is not supported.",
2729 version * 0.01);
2730 sl_pp_context_destroy(context);
2731 return GL_FALSE;
2732 }
2733
2734 /* Finally check the syntax and generate its binary representation. */
2735 result = sl_cl_compile(context,
2736 shader_type,
2737 parsing_builtin,
2738 &prod,
2739 &size,
2740 errmsg,
2741 sizeof(errmsg));
2742
2743 sl_pp_context_destroy(context);
2744
2745 if (result) {
2746 /*GLint pos;*/
2747
2748 slang_info_log_error(infolog, errmsg);
2749 /* syntax error (possibly in library code) */
2750 #if 0
2751 {
2752 int line, col;
2753 char *s;
2754 s = (char *) _mesa_find_line_column((const GLubyte *) source,
2755 (const GLubyte *) source + pos,
2756 &line, &col);
2757 printf("Error on line %d, col %d: %s\n", line, col, s);
2758 }
2759 #endif
2760 return GL_FALSE;
2761 }
2762
2763 /* Syntax is okay - translate it to internal representation. */
2764 if (!compile_binary(prod, unit, version, type, infolog, builtin,
2765 &builtin[SLANG_BUILTIN_TOTAL - 1],
2766 shader)) {
2767 free(prod);
2768 return GL_FALSE;
2769 }
2770 free(prod);
2771 return GL_TRUE;
2772 }
2773
2774 static const unsigned char slang_core_gc[] = {
2775 #include "library/slang_core_gc.h"
2776 };
2777
2778 static const unsigned char slang_120_core_gc[] = {
2779 #include "library/slang_120_core_gc.h"
2780 };
2781
2782 static const unsigned char slang_120_fragment_gc[] = {
2783 #include "library/slang_builtin_120_fragment_gc.h"
2784 };
2785
2786 static const unsigned char slang_common_builtin_gc[] = {
2787 #include "library/slang_common_builtin_gc.h"
2788 };
2789
2790 static const unsigned char slang_fragment_builtin_gc[] = {
2791 #include "library/slang_fragment_builtin_gc.h"
2792 };
2793
2794 static const unsigned char slang_vertex_builtin_gc[] = {
2795 #include "library/slang_vertex_builtin_gc.h"
2796 };
2797
2798 static GLboolean
2799 compile_object(const char *source,
2800 slang_code_object *object,
2801 slang_unit_type type,
2802 slang_info_log *infolog,
2803 struct gl_shader *shader,
2804 struct gl_sl_pragmas *pragmas)
2805 {
2806 slang_code_unit *builtins = NULL;
2807 GLuint base_version = 110;
2808 unsigned int shader_type;
2809 unsigned int parsing_builtin;
2810
2811 /* set shader type - the syntax is slightly different for different shaders */
2812 if (type == SLANG_UNIT_FRAGMENT_SHADER || type == SLANG_UNIT_FRAGMENT_BUILTIN) {
2813 shader_type = 1;
2814 } else {
2815 shader_type = 2;
2816 }
2817
2818 /* enable language extensions */
2819 parsing_builtin = 1;
2820
2821 /* if parsing user-specified shader, load built-in library */
2822 if (type == SLANG_UNIT_FRAGMENT_SHADER || type == SLANG_UNIT_VERTEX_SHADER) {
2823 /* compile core functionality first */
2824 if (!compile_binary(slang_core_gc,
2825 &object->builtin[SLANG_BUILTIN_CORE],
2826 base_version,
2827 SLANG_UNIT_FRAGMENT_BUILTIN, infolog,
2828 NULL, NULL, NULL))
2829 return GL_FALSE;
2830
2831 #if FEATURE_ARB_shading_language_120
2832 if (!compile_binary(slang_120_core_gc,
2833 &object->builtin[SLANG_BUILTIN_120_CORE],
2834 120,
2835 SLANG_UNIT_FRAGMENT_BUILTIN, infolog,
2836 NULL, &object->builtin[SLANG_BUILTIN_CORE], NULL))
2837 return GL_FALSE;
2838 #endif
2839
2840 /* compile common functions and variables, link to core */
2841 if (!compile_binary(slang_common_builtin_gc,
2842 &object->builtin[SLANG_BUILTIN_COMMON],
2843 #if FEATURE_ARB_shading_language_120
2844 120,
2845 #else
2846 base_version,
2847 #endif
2848 SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
2849 #if FEATURE_ARB_shading_language_120
2850 &object->builtin[SLANG_BUILTIN_120_CORE],
2851 #else
2852 &object->builtin[SLANG_BUILTIN_CORE],
2853 #endif
2854 NULL))
2855 return GL_FALSE;
2856
2857 /* compile target-specific functions and variables, link to common */
2858 if (type == SLANG_UNIT_FRAGMENT_SHADER) {
2859 if (!compile_binary(slang_fragment_builtin_gc,
2860 &object->builtin[SLANG_BUILTIN_TARGET],
2861 base_version,
2862 SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
2863 &object->builtin[SLANG_BUILTIN_COMMON], NULL))
2864 return GL_FALSE;
2865 #if FEATURE_ARB_shading_language_120
2866 if (!compile_binary(slang_120_fragment_gc,
2867 &object->builtin[SLANG_BUILTIN_TARGET],
2868 120,
2869 SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
2870 &object->builtin[SLANG_BUILTIN_COMMON], NULL))
2871 return GL_FALSE;
2872 #endif
2873 }
2874 else if (type == SLANG_UNIT_VERTEX_SHADER) {
2875 if (!compile_binary(slang_vertex_builtin_gc,
2876 &object->builtin[SLANG_BUILTIN_TARGET],
2877 base_version,
2878 SLANG_UNIT_VERTEX_BUILTIN, infolog, NULL,
2879 &object->builtin[SLANG_BUILTIN_COMMON], NULL))
2880 return GL_FALSE;
2881 }
2882
2883 /* disable language extensions */
2884 parsing_builtin = 0;
2885
2886 builtins = object->builtin;
2887 }
2888
2889 /* compile the actual shader - pass-in built-in library for external shader */
2890 return compile_with_grammar(source,
2891 &object->unit,
2892 type,
2893 infolog,
2894 builtins,
2895 shader,
2896 pragmas,
2897 shader_type,
2898 parsing_builtin);
2899 }
2900
2901
2902 GLboolean
2903 _slang_compile(GLcontext *ctx, struct gl_shader *shader)
2904 {
2905 GLboolean success;
2906 slang_info_log info_log;
2907 slang_code_object obj;
2908 slang_unit_type type;
2909 GLenum progTarget;
2910
2911 if (shader->Type == GL_VERTEX_SHADER) {
2912 type = SLANG_UNIT_VERTEX_SHADER;
2913 }
2914 else {
2915 assert(shader->Type == GL_FRAGMENT_SHADER);
2916 type = SLANG_UNIT_FRAGMENT_SHADER;
2917 }
2918
2919 if (!shader->Source)
2920 return GL_FALSE;
2921
2922 ctx->Shader.MemPool = _slang_new_mempool(1024*1024);
2923
2924 shader->Main = GL_FALSE;
2925
2926 /* free the shader's old instructions, etc */
2927 _mesa_reference_program(ctx, &shader->Program, NULL);
2928
2929 /* allocate new GPU program, parameter lists, etc. */
2930 if (shader->Type == GL_VERTEX_SHADER)
2931 progTarget = GL_VERTEX_PROGRAM_ARB;
2932 else
2933 progTarget = GL_FRAGMENT_PROGRAM_ARB;
2934 shader->Program = ctx->Driver.NewProgram(ctx, progTarget, 1);
2935 shader->Program->Parameters = _mesa_new_parameter_list();
2936 shader->Program->Varying = _mesa_new_parameter_list();
2937 shader->Program->Attributes = _mesa_new_parameter_list();
2938
2939 slang_info_log_construct(&info_log);
2940 _slang_code_object_ctr(&obj);
2941
2942 success = compile_object(shader->Source,
2943 &obj,
2944 type,
2945 &info_log,
2946 shader,
2947 &shader->Pragmas);
2948
2949 /* free shader's prev info log */
2950 if (shader->InfoLog) {
2951 _mesa_free(shader->InfoLog);
2952 shader->InfoLog = NULL;
2953 }
2954
2955 if (info_log.text) {
2956 /* copy info-log string to shader object */
2957 shader->InfoLog = _mesa_strdup(info_log.text);
2958 }
2959
2960 if (info_log.error_flag) {
2961 success = GL_FALSE;
2962 }
2963
2964 slang_info_log_destruct(&info_log);
2965 _slang_code_object_dtr(&obj);
2966
2967 _slang_delete_mempool((slang_mempool *) ctx->Shader.MemPool);
2968 ctx->Shader.MemPool = NULL;
2969
2970 /* remove any reads of output registers */
2971 #if 0
2972 printf("Pre-remove output reads:\n");
2973 _mesa_print_program(shader->Program);
2974 #endif
2975 _mesa_remove_output_reads(shader->Program, PROGRAM_OUTPUT);
2976 if (shader->Type == GL_VERTEX_SHADER) {
2977 /* and remove writes to varying vars in vertex programs */
2978 _mesa_remove_output_reads(shader->Program, PROGRAM_VARYING);
2979 }
2980 #if 0
2981 printf("Post-remove output reads:\n");
2982 _mesa_print_program(shader->Program);
2983 #endif
2984
2985 shader->CompileStatus = success;
2986
2987 if (success) {
2988 if (shader->Pragmas.Optimize &&
2989 (ctx->Shader.Flags & GLSL_NO_OPT) == 0) {
2990 _mesa_optimize_program(ctx, shader->Program);
2991 }
2992 if ((ctx->Shader.Flags & GLSL_NOP_VERT) &&
2993 shader->Program->Target == GL_VERTEX_PROGRAM_ARB) {
2994 _mesa_nop_vertex_program(ctx,
2995 (struct gl_vertex_program *) shader->Program);
2996 }
2997 if ((ctx->Shader.Flags & GLSL_NOP_FRAG) &&
2998 shader->Program->Target == GL_FRAGMENT_PROGRAM_ARB) {
2999 _mesa_nop_fragment_program(ctx,
3000 (struct gl_fragment_program *) shader->Program);
3001 }
3002 }
3003
3004 if (ctx->Shader.Flags & GLSL_LOG) {
3005 _mesa_write_shader_to_file(shader);
3006 }
3007
3008 return success;
3009 }
3010