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