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