glsl: force creation of new scope for for-loop body
[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, GL_TRUE))
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, GL_TRUE))
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, GL_FALSE))
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, GL_FALSE))
1094 RETURN0;
1095 break;
1096 case OP_EXPRESSION:
1097 oper->type = SLANG_OPER_EXPRESSION;
1098 if (!parse_child_operation(C, O, oper, GL_FALSE))
1099 RETURN0;
1100 break;
1101 case OP_IF:
1102 oper->type = SLANG_OPER_IF;
1103 if (!parse_child_operation(C, O, oper, GL_FALSE))
1104 RETURN0;
1105 if (!parse_child_operation(C, O, oper, GL_TRUE))
1106 RETURN0;
1107 if (!parse_child_operation(C, O, oper, GL_TRUE))
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, GL_TRUE))
1117 RETURN0;
1118 if (!parse_child_operation(C, &o, oper, GL_TRUE))
1119 RETURN0;
1120 }
1121 break;
1122 case OP_DO:
1123 oper->type = SLANG_OPER_DO;
1124 if (!parse_child_operation(C, O, oper, GL_TRUE))
1125 RETURN0;
1126 if (!parse_child_operation(C, O, oper, GL_FALSE))
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, GL_TRUE))
1136 RETURN0;
1137 if (!parse_child_operation(C, &o, oper, GL_TRUE))
1138 RETURN0;
1139 if (!parse_child_operation(C, &o, oper, GL_FALSE))
1140 RETURN0;
1141 #if 0
1142 if (!parse_child_operation(C, &o, oper, GL_TRUE))
1143 RETURN0;
1144 #else
1145 /* force creation of new scope for loop body */
1146 {
1147 slang_operation *ch;
1148 slang_output_ctx oo = o;
1149
1150 /* grow child array */
1151 ch = slang_operation_grow(&oper->num_children, &oper->children);
1152 ch->type = SLANG_OPER_BLOCK_NEW_SCOPE;
1153
1154 ch->locals->outer_scope = o.vars;
1155 oo.vars = ch->locals;
1156
1157 if (!parse_child_operation(C, &oo, ch, GL_TRUE))
1158 RETURN0;
1159 }
1160 #endif
1161 }
1162 break;
1163 case OP_PRECISION:
1164 {
1165 /* set default precision for a type in this scope */
1166 /* ignored at this time */
1167 int prec_qual = *C->I++;
1168 int datatype = *C->I++;
1169 (void) prec_qual;
1170 (void) datatype;
1171 }
1172 break;
1173 default:
1174 /*printf("Unexpected operation %d\n", op);*/
1175 RETURN0;
1176 }
1177 return 1;
1178 }
1179
1180 static int
1181 handle_nary_expression(slang_parse_ctx * C, slang_operation * op,
1182 slang_operation ** ops, unsigned int *total_ops,
1183 unsigned int n)
1184 {
1185 unsigned int i;
1186
1187 op->children = slang_operation_new(n);
1188 if (op->children == NULL) {
1189 slang_info_log_memory(C->L);
1190 RETURN0;
1191 }
1192 op->num_children = n;
1193
1194 for (i = 0; i < n; i++) {
1195 slang_operation_destruct(&op->children[i]);
1196 op->children[i] = (*ops)[*total_ops - (n + 1 - i)];
1197 }
1198
1199 (*ops)[*total_ops - (n + 1)] = (*ops)[*total_ops - 1];
1200 *total_ops -= n;
1201
1202 *ops = (slang_operation *)
1203 _slang_realloc(*ops,
1204 (*total_ops + n) * sizeof(slang_operation),
1205 *total_ops * sizeof(slang_operation));
1206 if (*ops == NULL) {
1207 slang_info_log_memory(C->L);
1208 RETURN0;
1209 }
1210 return 1;
1211 }
1212
1213 static int
1214 is_constructor_name(const char *name, slang_atom a_name,
1215 slang_struct_scope * structs)
1216 {
1217 if (slang_type_specifier_type_from_string(name) != SLANG_SPEC_VOID)
1218 return 1;
1219 return slang_struct_scope_find(structs, a_name, 1) != NULL;
1220 }
1221
1222 #define FUNCTION_CALL_NONARRAY 0
1223 #define FUNCTION_CALL_ARRAY 1
1224
1225 static int
1226 parse_expression(slang_parse_ctx * C, slang_output_ctx * O,
1227 slang_operation * oper)
1228 {
1229 slang_operation *ops = NULL;
1230 unsigned int num_ops = 0;
1231 int number;
1232
1233 while (*C->I != OP_END) {
1234 slang_operation *op;
1235 const unsigned int op_code = *C->I++;
1236
1237 /* allocate default operation, becomes a no-op if not used */
1238 ops = (slang_operation *)
1239 _slang_realloc(ops,
1240 num_ops * sizeof(slang_operation),
1241 (num_ops + 1) * sizeof(slang_operation));
1242 if (ops == NULL) {
1243 slang_info_log_memory(C->L);
1244 RETURN0;
1245 }
1246 op = &ops[num_ops];
1247 if (!slang_operation_construct(op)) {
1248 slang_info_log_memory(C->L);
1249 RETURN0;
1250 }
1251 num_ops++;
1252 op->locals->outer_scope = O->vars;
1253
1254 switch (op_code) {
1255 case OP_PUSH_VOID:
1256 op->type = SLANG_OPER_VOID;
1257 break;
1258 case OP_PUSH_BOOL:
1259 op->type = SLANG_OPER_LITERAL_BOOL;
1260 if (!parse_number(C, &number))
1261 RETURN0;
1262 op->literal[0] =
1263 op->literal[1] =
1264 op->literal[2] =
1265 op->literal[3] = (GLfloat) number;
1266 op->literal_size = 1;
1267 break;
1268 case OP_PUSH_INT:
1269 op->type = SLANG_OPER_LITERAL_INT;
1270 if (!parse_number(C, &number))
1271 RETURN0;
1272 op->literal[0] =
1273 op->literal[1] =
1274 op->literal[2] =
1275 op->literal[3] = (GLfloat) number;
1276 op->literal_size = 1;
1277 break;
1278 case OP_PUSH_FLOAT:
1279 op->type = SLANG_OPER_LITERAL_FLOAT;
1280 if (!parse_float(C, &op->literal[0]))
1281 RETURN0;
1282 op->literal[1] =
1283 op->literal[2] =
1284 op->literal[3] = op->literal[0];
1285 op->literal_size = 1;
1286 break;
1287 case OP_PUSH_IDENTIFIER:
1288 op->type = SLANG_OPER_IDENTIFIER;
1289 op->a_id = parse_identifier(C);
1290 if (op->a_id == SLANG_ATOM_NULL)
1291 RETURN0;
1292 break;
1293 case OP_SEQUENCE:
1294 op->type = SLANG_OPER_SEQUENCE;
1295 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1296 RETURN0;
1297 break;
1298 case OP_ASSIGN:
1299 op->type = SLANG_OPER_ASSIGN;
1300 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1301 RETURN0;
1302 break;
1303 case OP_ADDASSIGN:
1304 op->type = SLANG_OPER_ADDASSIGN;
1305 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1306 RETURN0;
1307 break;
1308 case OP_SUBASSIGN:
1309 op->type = SLANG_OPER_SUBASSIGN;
1310 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1311 RETURN0;
1312 break;
1313 case OP_MULASSIGN:
1314 op->type = SLANG_OPER_MULASSIGN;
1315 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1316 RETURN0;
1317 break;
1318 case OP_DIVASSIGN:
1319 op->type = SLANG_OPER_DIVASSIGN;
1320 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1321 RETURN0;
1322 break;
1323 /*case OP_MODASSIGN: */
1324 /*case OP_LSHASSIGN: */
1325 /*case OP_RSHASSIGN: */
1326 /*case OP_ORASSIGN: */
1327 /*case OP_XORASSIGN: */
1328 /*case OP_ANDASSIGN: */
1329 case OP_SELECT:
1330 op->type = SLANG_OPER_SELECT;
1331 if (!handle_nary_expression(C, op, &ops, &num_ops, 3))
1332 RETURN0;
1333 break;
1334 case OP_LOGICALOR:
1335 op->type = SLANG_OPER_LOGICALOR;
1336 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1337 RETURN0;
1338 break;
1339 case OP_LOGICALXOR:
1340 op->type = SLANG_OPER_LOGICALXOR;
1341 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1342 RETURN0;
1343 break;
1344 case OP_LOGICALAND:
1345 op->type = SLANG_OPER_LOGICALAND;
1346 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1347 RETURN0;
1348 break;
1349 /*case OP_BITOR: */
1350 /*case OP_BITXOR: */
1351 /*case OP_BITAND: */
1352 case OP_EQUAL:
1353 op->type = SLANG_OPER_EQUAL;
1354 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1355 RETURN0;
1356 break;
1357 case OP_NOTEQUAL:
1358 op->type = SLANG_OPER_NOTEQUAL;
1359 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1360 RETURN0;
1361 break;
1362 case OP_LESS:
1363 op->type = SLANG_OPER_LESS;
1364 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1365 RETURN0;
1366 break;
1367 case OP_GREATER:
1368 op->type = SLANG_OPER_GREATER;
1369 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1370 RETURN0;
1371 break;
1372 case OP_LESSEQUAL:
1373 op->type = SLANG_OPER_LESSEQUAL;
1374 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1375 RETURN0;
1376 break;
1377 case OP_GREATEREQUAL:
1378 op->type = SLANG_OPER_GREATEREQUAL;
1379 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1380 RETURN0;
1381 break;
1382 /*case OP_LSHIFT: */
1383 /*case OP_RSHIFT: */
1384 case OP_ADD:
1385 op->type = SLANG_OPER_ADD;
1386 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1387 RETURN0;
1388 break;
1389 case OP_SUBTRACT:
1390 op->type = SLANG_OPER_SUBTRACT;
1391 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1392 RETURN0;
1393 break;
1394 case OP_MULTIPLY:
1395 op->type = SLANG_OPER_MULTIPLY;
1396 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1397 RETURN0;
1398 break;
1399 case OP_DIVIDE:
1400 op->type = SLANG_OPER_DIVIDE;
1401 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1402 RETURN0;
1403 break;
1404 /*case OP_MODULUS: */
1405 case OP_PREINCREMENT:
1406 op->type = SLANG_OPER_PREINCREMENT;
1407 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1408 RETURN0;
1409 break;
1410 case OP_PREDECREMENT:
1411 op->type = SLANG_OPER_PREDECREMENT;
1412 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1413 RETURN0;
1414 break;
1415 case OP_PLUS:
1416 op->type = SLANG_OPER_PLUS;
1417 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1418 RETURN0;
1419 break;
1420 case OP_MINUS:
1421 op->type = SLANG_OPER_MINUS;
1422 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1423 RETURN0;
1424 break;
1425 case OP_NOT:
1426 op->type = SLANG_OPER_NOT;
1427 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1428 RETURN0;
1429 break;
1430 /*case OP_COMPLEMENT: */
1431 case OP_SUBSCRIPT:
1432 op->type = SLANG_OPER_SUBSCRIPT;
1433 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1434 RETURN0;
1435 break;
1436 case OP_METHOD:
1437 op->type = SLANG_OPER_METHOD;
1438 op->a_obj = parse_identifier(C);
1439 if (op->a_obj == SLANG_ATOM_NULL)
1440 RETURN0;
1441
1442 op->a_id = parse_identifier(C);
1443 if (op->a_id == SLANG_ATOM_NULL)
1444 RETURN0;
1445
1446 assert(*C->I == OP_END);
1447 C->I++;
1448
1449 while (*C->I != OP_END)
1450 if (!parse_child_operation(C, O, op, GL_FALSE))
1451 RETURN0;
1452 C->I++;
1453 #if 0
1454 /* don't lookup the method (not yet anyway) */
1455 if (!C->parsing_builtin
1456 && !slang_function_scope_find_by_name(O->funs, op->a_id, 1)) {
1457 const char *id;
1458
1459 id = slang_atom_pool_id(C->atoms, op->a_id);
1460 if (!is_constructor_name(id, op->a_id, O->structs)) {
1461 slang_info_log_error(C->L, "%s: undeclared function name.", id);
1462 RETURN0;
1463 }
1464 }
1465 #endif
1466 break;
1467 case OP_CALL:
1468 {
1469 GLboolean array_constructor = GL_FALSE;
1470 GLint array_constructor_size;
1471
1472 op->type = SLANG_OPER_CALL;
1473 op->a_id = parse_identifier(C);
1474 if (op->a_id == SLANG_ATOM_NULL)
1475 RETURN0;
1476 switch (*C->I++) {
1477 case FUNCTION_CALL_NONARRAY:
1478 /* Nothing to do. */
1479 break;
1480 case FUNCTION_CALL_ARRAY:
1481 /* Calling an array constructor. For example:
1482 * float[3](1.1, 2.2, 3.3);
1483 */
1484 if (!O->allow_array_types) {
1485 slang_info_log_error(C->L,
1486 "array constructors not allowed "
1487 "in this GLSL version");
1488 RETURN0;
1489 }
1490 else {
1491 array_constructor = GL_TRUE;
1492 /* parse the array constructor size */
1493 slang_operation array_size;
1494 slang_operation_construct(&array_size);
1495 if (!parse_expression(C, O, &array_size)) {
1496 slang_operation_destruct(&array_size);
1497 return GL_FALSE;
1498 }
1499 if (array_size.type != SLANG_OPER_LITERAL_INT) {
1500 slang_info_log_error(C->L,
1501 "constructor array size is not an integer");
1502 slang_operation_destruct(&array_size);
1503 RETURN0;
1504 }
1505 array_constructor_size = (int) array_size.literal[0];
1506 op->array_constructor = GL_TRUE;
1507 slang_operation_destruct(&array_size);
1508 }
1509 break;
1510 default:
1511 assert(0);
1512 RETURN0;
1513 }
1514 while (*C->I != OP_END)
1515 if (!parse_child_operation(C, O, op, GL_FALSE))
1516 RETURN0;
1517 C->I++;
1518
1519 if (array_constructor &&
1520 array_constructor_size != op->num_children) {
1521 slang_info_log_error(C->L, "number of parameters to array"
1522 " constructor does not match array size");
1523 RETURN0;
1524 }
1525
1526 if (!C->parsing_builtin
1527 && !slang_function_scope_find_by_name(O->funs, op->a_id, 1)) {
1528 const char *id;
1529
1530 id = slang_atom_pool_id(C->atoms, op->a_id);
1531 if (!is_constructor_name(id, op->a_id, O->structs)) {
1532 slang_info_log_error(C->L, "%s: undeclared function name.", id);
1533 RETURN0;
1534 }
1535 }
1536 }
1537 break;
1538 case OP_FIELD:
1539 op->type = SLANG_OPER_FIELD;
1540 op->a_id = parse_identifier(C);
1541 if (op->a_id == SLANG_ATOM_NULL)
1542 RETURN0;
1543 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1544 RETURN0;
1545 break;
1546 case OP_POSTINCREMENT:
1547 op->type = SLANG_OPER_POSTINCREMENT;
1548 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1549 RETURN0;
1550 break;
1551 case OP_POSTDECREMENT:
1552 op->type = SLANG_OPER_POSTDECREMENT;
1553 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1554 RETURN0;
1555 break;
1556 default:
1557 RETURN0;
1558 }
1559 }
1560 C->I++;
1561
1562 slang_operation_destruct(oper);
1563 *oper = *ops; /* struct copy */
1564 _slang_free(ops);
1565
1566 return 1;
1567 }
1568
1569 /* parameter qualifier */
1570 #define PARAM_QUALIFIER_IN 0
1571 #define PARAM_QUALIFIER_OUT 1
1572 #define PARAM_QUALIFIER_INOUT 2
1573
1574 /* function parameter array presence */
1575 #define PARAMETER_ARRAY_NOT_PRESENT 0
1576 #define PARAMETER_ARRAY_PRESENT 1
1577
1578 static int
1579 parse_parameter_declaration(slang_parse_ctx * C, slang_output_ctx * O,
1580 slang_variable * param)
1581 {
1582 int param_qual, precision_qual;
1583
1584 /* parse and validate the parameter's type qualifiers (there can be
1585 * two at most) because not all combinations are valid
1586 */
1587 if (!parse_type_qualifier(C, &param->type.qualifier))
1588 RETURN0;
1589
1590 param_qual = *C->I++;
1591 switch (param_qual) {
1592 case PARAM_QUALIFIER_IN:
1593 if (param->type.qualifier != SLANG_QUAL_CONST
1594 && param->type.qualifier != SLANG_QUAL_NONE) {
1595 slang_info_log_error(C->L, "Invalid type qualifier.");
1596 RETURN0;
1597 }
1598 break;
1599 case PARAM_QUALIFIER_OUT:
1600 if (param->type.qualifier == SLANG_QUAL_NONE)
1601 param->type.qualifier = SLANG_QUAL_OUT;
1602 else {
1603 slang_info_log_error(C->L, "Invalid type qualifier.");
1604 RETURN0;
1605 }
1606 break;
1607 case PARAM_QUALIFIER_INOUT:
1608 if (param->type.qualifier == SLANG_QUAL_NONE)
1609 param->type.qualifier = SLANG_QUAL_INOUT;
1610 else {
1611 slang_info_log_error(C->L, "Invalid type qualifier.");
1612 RETURN0;
1613 }
1614 break;
1615 default:
1616 RETURN0;
1617 }
1618
1619 /* parse precision qualifier (lowp, mediump, highp */
1620 precision_qual = *C->I++;
1621 /* ignored at this time */
1622 (void) precision_qual;
1623
1624 /* parse parameter's type specifier and name */
1625 if (!parse_type_specifier(C, O, &param->type.specifier))
1626 RETURN0;
1627 if (!parse_type_array_size(C, O, &param->type.array_len))
1628 RETURN0;
1629 param->a_name = parse_identifier(C);
1630 if (param->a_name == SLANG_ATOM_NULL)
1631 RETURN0;
1632
1633 /* first-class array
1634 */
1635 if (param->type.array_len >= 0) {
1636 slang_type_specifier p;
1637
1638 slang_type_specifier_ctr(&p);
1639 if (!slang_type_specifier_copy(&p, &param->type.specifier)) {
1640 slang_type_specifier_dtr(&p);
1641 RETURN0;
1642 }
1643 if (!convert_to_array(C, param, &p)) {
1644 slang_type_specifier_dtr(&p);
1645 RETURN0;
1646 }
1647 slang_type_specifier_dtr(&p);
1648 param->array_len = param->type.array_len;
1649 }
1650
1651 /* if the parameter is an array, parse its size (the size must be
1652 * explicitly defined
1653 */
1654 if (*C->I++ == PARAMETER_ARRAY_PRESENT) {
1655 slang_type_specifier p;
1656
1657 if (param->type.array_len >= 0) {
1658 slang_info_log_error(C->L, "multi-dimensional arrays not allowed");
1659 RETURN0;
1660 }
1661 slang_type_specifier_ctr(&p);
1662 if (!slang_type_specifier_copy(&p, &param->type.specifier)) {
1663 slang_type_specifier_dtr(&p);
1664 RETURN0;
1665 }
1666 if (!convert_to_array(C, param, &p)) {
1667 slang_type_specifier_dtr(&p);
1668 RETURN0;
1669 }
1670 slang_type_specifier_dtr(&p);
1671 if (!parse_array_len(C, O, &param->array_len))
1672 RETURN0;
1673 }
1674
1675 #if 0
1676 /* calculate the parameter size */
1677 if (!calculate_var_size(C, O, param))
1678 RETURN0;
1679 #endif
1680 /* TODO: allocate the local address here? */
1681 return 1;
1682 }
1683
1684 /* function type */
1685 #define FUNCTION_ORDINARY 0
1686 #define FUNCTION_CONSTRUCTOR 1
1687 #define FUNCTION_OPERATOR 2
1688
1689 /* function parameter */
1690 #define PARAMETER_NONE 0
1691 #define PARAMETER_NEXT 1
1692
1693 /* operator type */
1694 #define OPERATOR_ADDASSIGN 1
1695 #define OPERATOR_SUBASSIGN 2
1696 #define OPERATOR_MULASSIGN 3
1697 #define OPERATOR_DIVASSIGN 4
1698 /*#define OPERATOR_MODASSIGN 5*/
1699 /*#define OPERATOR_LSHASSIGN 6*/
1700 /*#define OPERATOR_RSHASSIGN 7*/
1701 /*#define OPERATOR_ANDASSIGN 8*/
1702 /*#define OPERATOR_XORASSIGN 9*/
1703 /*#define OPERATOR_ORASSIGN 10*/
1704 #define OPERATOR_LOGICALXOR 11
1705 /*#define OPERATOR_BITOR 12*/
1706 /*#define OPERATOR_BITXOR 13*/
1707 /*#define OPERATOR_BITAND 14*/
1708 #define OPERATOR_LESS 15
1709 #define OPERATOR_GREATER 16
1710 #define OPERATOR_LESSEQUAL 17
1711 #define OPERATOR_GREATEREQUAL 18
1712 /*#define OPERATOR_LSHIFT 19*/
1713 /*#define OPERATOR_RSHIFT 20*/
1714 #define OPERATOR_MULTIPLY 21
1715 #define OPERATOR_DIVIDE 22
1716 /*#define OPERATOR_MODULUS 23*/
1717 #define OPERATOR_INCREMENT 24
1718 #define OPERATOR_DECREMENT 25
1719 #define OPERATOR_PLUS 26
1720 #define OPERATOR_MINUS 27
1721 /*#define OPERATOR_COMPLEMENT 28*/
1722 #define OPERATOR_NOT 29
1723
1724 static const struct
1725 {
1726 unsigned int o_code;
1727 const char *o_name;
1728 } operator_names[] = {
1729 {OPERATOR_INCREMENT, "++"},
1730 {OPERATOR_ADDASSIGN, "+="},
1731 {OPERATOR_PLUS, "+"},
1732 {OPERATOR_DECREMENT, "--"},
1733 {OPERATOR_SUBASSIGN, "-="},
1734 {OPERATOR_MINUS, "-"},
1735 {OPERATOR_NOT, "!"},
1736 {OPERATOR_MULASSIGN, "*="},
1737 {OPERATOR_MULTIPLY, "*"},
1738 {OPERATOR_DIVASSIGN, "/="},
1739 {OPERATOR_DIVIDE, "/"},
1740 {OPERATOR_LESSEQUAL, "<="},
1741 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1742 /*{ OPERATOR_LSHIFT, "<<" }, */
1743 {OPERATOR_LESS, "<"},
1744 {OPERATOR_GREATEREQUAL, ">="},
1745 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1746 /*{ OPERATOR_RSHIFT, ">>" }, */
1747 {OPERATOR_GREATER, ">"},
1748 /*{ OPERATOR_MODASSIGN, "%=" }, */
1749 /*{ OPERATOR_MODULUS, "%" }, */
1750 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1751 /*{ OPERATOR_BITAND, "&" }, */
1752 /*{ OPERATOR_ORASSIGN, "|=" }, */
1753 /*{ OPERATOR_BITOR, "|" }, */
1754 /*{ OPERATOR_COMPLEMENT, "~" }, */
1755 /*{ OPERATOR_XORASSIGN, "^=" }, */
1756 {OPERATOR_LOGICALXOR, "^^"},
1757 /*{ OPERATOR_BITXOR, "^" } */
1758 };
1759
1760 static slang_atom
1761 parse_operator_name(slang_parse_ctx * C)
1762 {
1763 unsigned int i;
1764
1765 for (i = 0; i < sizeof(operator_names) / sizeof(*operator_names); i++) {
1766 if (operator_names[i].o_code == (unsigned int) (*C->I)) {
1767 slang_atom atom =
1768 slang_atom_pool_atom(C->atoms, operator_names[i].o_name);
1769 if (atom == SLANG_ATOM_NULL) {
1770 slang_info_log_memory(C->L);
1771 RETURN0;
1772 }
1773 C->I++;
1774 return atom;
1775 }
1776 }
1777 RETURN0;
1778 }
1779
1780
1781 static int
1782 parse_function_prototype(slang_parse_ctx * C, slang_output_ctx * O,
1783 slang_function * func)
1784 {
1785 GLuint functype;
1786 /* parse function type and name */
1787 if (!parse_fully_specified_type(C, O, &func->header.type))
1788 RETURN0;
1789
1790 functype = *C->I++;
1791 switch (functype) {
1792 case FUNCTION_ORDINARY:
1793 func->kind = SLANG_FUNC_ORDINARY;
1794 func->header.a_name = parse_identifier(C);
1795 if (func->header.a_name == SLANG_ATOM_NULL)
1796 RETURN0;
1797 break;
1798 case FUNCTION_CONSTRUCTOR:
1799 func->kind = SLANG_FUNC_CONSTRUCTOR;
1800 if (func->header.type.specifier.type == SLANG_SPEC_STRUCT)
1801 RETURN0;
1802 func->header.a_name =
1803 slang_atom_pool_atom(C->atoms,
1804 slang_type_specifier_type_to_string
1805 (func->header.type.specifier.type));
1806 if (func->header.a_name == SLANG_ATOM_NULL) {
1807 slang_info_log_memory(C->L);
1808 RETURN0;
1809 }
1810 break;
1811 case FUNCTION_OPERATOR:
1812 func->kind = SLANG_FUNC_OPERATOR;
1813 func->header.a_name = parse_operator_name(C);
1814 if (func->header.a_name == SLANG_ATOM_NULL)
1815 RETURN0;
1816 break;
1817 default:
1818 RETURN0;
1819 }
1820
1821 if (!legal_identifier(func->header.a_name)) {
1822 slang_info_log_error(C->L, "illegal function name '%s'",
1823 (char *) func->header.a_name);
1824 RETURN0;
1825 }
1826
1827 /* parse function parameters */
1828 while (*C->I++ == PARAMETER_NEXT) {
1829 slang_variable *p = slang_variable_scope_grow(func->parameters);
1830 if (!p) {
1831 slang_info_log_memory(C->L);
1832 RETURN0;
1833 }
1834 if (!parse_parameter_declaration(C, O, p))
1835 RETURN0;
1836 }
1837
1838 /* if the function returns a value, append a hidden __retVal 'out'
1839 * parameter that corresponds to the return value.
1840 */
1841 if (_slang_function_has_return_value(func)) {
1842 slang_variable *p = slang_variable_scope_grow(func->parameters);
1843 slang_atom a_retVal = slang_atom_pool_atom(C->atoms, "__retVal");
1844 assert(a_retVal);
1845 p->a_name = a_retVal;
1846 p->type = func->header.type;
1847 p->type.qualifier = SLANG_QUAL_OUT;
1848 }
1849
1850 /* function formal parameters and local variables share the same
1851 * scope, so save the information about param count in a seperate
1852 * place also link the scope to the global variable scope so when a
1853 * given identifier is not found here, the search process continues
1854 * in the global space
1855 */
1856 func->param_count = func->parameters->num_variables;
1857 func->parameters->outer_scope = O->vars;
1858
1859 return 1;
1860 }
1861
1862 static int
1863 parse_function_definition(slang_parse_ctx * C, slang_output_ctx * O,
1864 slang_function * func)
1865 {
1866 slang_output_ctx o = *O;
1867
1868 if (!parse_function_prototype(C, O, func))
1869 RETURN0;
1870
1871 /* create function's body operation */
1872 func->body = (slang_operation *) _slang_alloc(sizeof(slang_operation));
1873 if (func->body == NULL) {
1874 slang_info_log_memory(C->L);
1875 RETURN0;
1876 }
1877 if (!slang_operation_construct(func->body)) {
1878 _slang_free(func->body);
1879 func->body = NULL;
1880 slang_info_log_memory(C->L);
1881 RETURN0;
1882 }
1883
1884 /* to parse the body the parse context is modified in order to
1885 * capture parsed variables into function's local variable scope
1886 */
1887 C->global_scope = GL_FALSE;
1888 o.vars = func->parameters;
1889 if (!parse_statement(C, &o, func->body))
1890 RETURN0;
1891
1892 C->global_scope = GL_TRUE;
1893 return 1;
1894 }
1895
1896 static GLboolean
1897 initialize_global(slang_assemble_ctx * A, slang_variable * var)
1898 {
1899 slang_operation op_id, op_assign;
1900 GLboolean result;
1901
1902 /* construct the left side of assignment */
1903 if (!slang_operation_construct(&op_id))
1904 return GL_FALSE;
1905 op_id.type = SLANG_OPER_IDENTIFIER;
1906 op_id.a_id = var->a_name;
1907
1908 /* put the variable into operation's scope */
1909 op_id.locals->variables =
1910 (slang_variable **) _slang_alloc(sizeof(slang_variable *));
1911 if (op_id.locals->variables == NULL) {
1912 slang_operation_destruct(&op_id);
1913 return GL_FALSE;
1914 }
1915 op_id.locals->num_variables = 1;
1916 op_id.locals->variables[0] = var;
1917
1918 /* construct the assignment expression */
1919 if (!slang_operation_construct(&op_assign)) {
1920 op_id.locals->num_variables = 0;
1921 slang_operation_destruct(&op_id);
1922 return GL_FALSE;
1923 }
1924 op_assign.type = SLANG_OPER_ASSIGN;
1925 op_assign.children =
1926 (slang_operation *) _slang_alloc(2 * sizeof(slang_operation));
1927 if (op_assign.children == NULL) {
1928 slang_operation_destruct(&op_assign);
1929 op_id.locals->num_variables = 0;
1930 slang_operation_destruct(&op_id);
1931 return GL_FALSE;
1932 }
1933 op_assign.num_children = 2;
1934 op_assign.children[0] = op_id;
1935 op_assign.children[1] = *var->initializer;
1936
1937 result = 1;
1938
1939 /* carefully destroy the operations */
1940 op_assign.num_children = 0;
1941 _slang_free(op_assign.children);
1942 op_assign.children = NULL;
1943 slang_operation_destruct(&op_assign);
1944 op_id.locals->num_variables = 0;
1945 slang_operation_destruct(&op_id);
1946
1947 if (!result)
1948 return GL_FALSE;
1949
1950 return GL_TRUE;
1951 }
1952
1953 /* init declarator list */
1954 #define DECLARATOR_NONE 0
1955 #define DECLARATOR_NEXT 1
1956
1957 /* variable declaration */
1958 #define VARIABLE_NONE 0
1959 #define VARIABLE_IDENTIFIER 1
1960 #define VARIABLE_INITIALIZER 2
1961 #define VARIABLE_ARRAY_EXPLICIT 3
1962 #define VARIABLE_ARRAY_UNKNOWN 4
1963
1964
1965 /**
1966 * Parse the initializer for a variable declaration.
1967 */
1968 static int
1969 parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
1970 const slang_fully_specified_type * type)
1971 {
1972 slang_variable *var;
1973 slang_atom a_name;
1974
1975 /* empty init declatator (without name, e.g. "float ;") */
1976 if (*C->I++ == VARIABLE_NONE)
1977 return 1;
1978
1979 a_name = parse_identifier(C);
1980
1981 /* check if name is already in this scope */
1982 if (_slang_variable_locate(O->vars, a_name, GL_FALSE)) {
1983 slang_info_log_error(C->L,
1984 "declaration of '%s' conflicts with previous declaration",
1985 (char *) a_name);
1986 RETURN0;
1987 }
1988
1989 /* make room for the new variable and initialize it */
1990 var = slang_variable_scope_grow(O->vars);
1991 if (!var) {
1992 slang_info_log_memory(C->L);
1993 RETURN0;
1994 }
1995
1996 /* copy the declarator type qualifier/etc info, parse the identifier */
1997 var->type.qualifier = type->qualifier;
1998 var->type.centroid = type->centroid;
1999 var->type.precision = type->precision;
2000 var->type.variant = type->variant;
2001 var->type.array_len = type->array_len;
2002 var->a_name = a_name;
2003 if (var->a_name == SLANG_ATOM_NULL)
2004 RETURN0;
2005
2006 switch (*C->I++) {
2007 case VARIABLE_NONE:
2008 /* simple variable declarator - just copy the specifier */
2009 if (!slang_type_specifier_copy(&var->type.specifier, &type->specifier))
2010 RETURN0;
2011 break;
2012 case VARIABLE_INITIALIZER:
2013 /* initialized variable - copy the specifier and parse the expression */
2014 if (0 && type->array_len >= 0) {
2015 /* The type was something like "float[4]" */
2016 convert_to_array(C, var, &type->specifier);
2017 var->array_len = type->array_len;
2018 }
2019 else {
2020 if (!slang_type_specifier_copy(&var->type.specifier, &type->specifier))
2021 RETURN0;
2022 }
2023 var->initializer =
2024 (slang_operation *) _slang_alloc(sizeof(slang_operation));
2025 if (var->initializer == NULL) {
2026 slang_info_log_memory(C->L);
2027 RETURN0;
2028 }
2029 if (!slang_operation_construct(var->initializer)) {
2030 _slang_free(var->initializer);
2031 var->initializer = NULL;
2032 slang_info_log_memory(C->L);
2033 RETURN0;
2034 }
2035 if (!parse_expression(C, O, var->initializer))
2036 RETURN0;
2037 break;
2038 case VARIABLE_ARRAY_UNKNOWN:
2039 /* unsized array - mark it as array and copy the specifier to
2040 * the array element
2041 */
2042 if (type->array_len >= 0) {
2043 slang_info_log_error(C->L, "multi-dimensional arrays not allowed");
2044 RETURN0;
2045 }
2046 if (!convert_to_array(C, var, &type->specifier))
2047 return GL_FALSE;
2048 break;
2049 case VARIABLE_ARRAY_EXPLICIT:
2050 if (type->array_len >= 0) {
2051 /* the user is trying to do something like: float[2] x[3]; */
2052 slang_info_log_error(C->L, "multi-dimensional arrays not allowed");
2053 RETURN0;
2054 }
2055 if (!convert_to_array(C, var, &type->specifier))
2056 return GL_FALSE;
2057 if (!parse_array_len(C, O, &var->array_len))
2058 return GL_FALSE;
2059 break;
2060 default:
2061 RETURN0;
2062 }
2063
2064 /* allocate global address space for a variable with a known size */
2065 if (C->global_scope
2066 && !(var->type.specifier.type == SLANG_SPEC_ARRAY
2067 && var->array_len == 0)) {
2068 if (!calculate_var_size(C, O, var))
2069 return GL_FALSE;
2070 }
2071
2072 /* emit code for global var decl */
2073 if (C->global_scope) {
2074 slang_assemble_ctx A;
2075 A.atoms = C->atoms;
2076 A.space.funcs = O->funs;
2077 A.space.structs = O->structs;
2078 A.space.vars = O->vars;
2079 A.program = O->program;
2080 A.vartable = O->vartable;
2081 A.log = C->L;
2082 A.curFuncEndLabel = NULL;
2083 if (!_slang_codegen_global_variable(&A, var, C->type))
2084 RETURN0;
2085 }
2086
2087 /* initialize global variable */
2088 if (C->global_scope) {
2089 if (var->initializer != NULL) {
2090 slang_assemble_ctx A;
2091
2092 A.atoms = C->atoms;
2093 A.space.funcs = O->funs;
2094 A.space.structs = O->structs;
2095 A.space.vars = O->vars;
2096 if (!initialize_global(&A, var))
2097 RETURN0;
2098 }
2099 }
2100 return 1;
2101 }
2102
2103 /**
2104 * Parse a list of variable declarations. Each variable may have an
2105 * initializer.
2106 */
2107 static int
2108 parse_init_declarator_list(slang_parse_ctx * C, slang_output_ctx * O)
2109 {
2110 slang_fully_specified_type type;
2111
2112 /* parse the fully specified type, common to all declarators */
2113 if (!slang_fully_specified_type_construct(&type))
2114 RETURN0;
2115 if (!parse_fully_specified_type(C, O, &type)) {
2116 slang_fully_specified_type_destruct(&type);
2117 RETURN0;
2118 }
2119
2120 /* parse declarators, pass-in the parsed type */
2121 do {
2122 if (!parse_init_declarator(C, O, &type)) {
2123 slang_fully_specified_type_destruct(&type);
2124 RETURN0;
2125 }
2126 }
2127 while (*C->I++ == DECLARATOR_NEXT);
2128
2129 slang_fully_specified_type_destruct(&type);
2130 return 1;
2131 }
2132
2133
2134 /**
2135 * Parse a function definition or declaration.
2136 * \param C parsing context
2137 * \param O output context
2138 * \param definition if non-zero expect a definition, else a declaration
2139 * \param parsed_func_ret returns the parsed function
2140 * \return GL_TRUE if success, GL_FALSE if failure
2141 */
2142 static GLboolean
2143 parse_function(slang_parse_ctx * C, slang_output_ctx * O, int definition,
2144 slang_function ** parsed_func_ret)
2145 {
2146 slang_function parsed_func, *found_func;
2147
2148 /* parse function definition/declaration */
2149 if (!slang_function_construct(&parsed_func))
2150 return GL_FALSE;
2151 if (definition) {
2152 if (!parse_function_definition(C, O, &parsed_func)) {
2153 slang_function_destruct(&parsed_func);
2154 return GL_FALSE;
2155 }
2156 }
2157 else {
2158 if (!parse_function_prototype(C, O, &parsed_func)) {
2159 slang_function_destruct(&parsed_func);
2160 return GL_FALSE;
2161 }
2162 }
2163
2164 /* find a function with a prototype matching the parsed one - only
2165 * the current scope is being searched to allow built-in function
2166 * overriding
2167 */
2168 found_func = slang_function_scope_find(O->funs, &parsed_func, 0);
2169 if (found_func == NULL) {
2170 /* New function, add it to the function list */
2171 O->funs->functions =
2172 (slang_function *) _slang_realloc(O->funs->functions,
2173 O->funs->num_functions
2174 * sizeof(slang_function),
2175 (O->funs->num_functions + 1)
2176 * sizeof(slang_function));
2177 if (O->funs->functions == NULL) {
2178 slang_info_log_memory(C->L);
2179 slang_function_destruct(&parsed_func);
2180 return GL_FALSE;
2181 }
2182 O->funs->functions[O->funs->num_functions] = parsed_func;
2183 O->funs->num_functions++;
2184
2185 /* return the newly parsed function */
2186 *parsed_func_ret = &O->funs->functions[O->funs->num_functions - 1];
2187 }
2188 else {
2189 /* previously defined or declared */
2190 /* TODO: check function return type qualifiers and specifiers */
2191 if (definition) {
2192 if (found_func->body != NULL) {
2193 slang_info_log_error(C->L, "%s: function already has a body.",
2194 slang_atom_pool_id(C->atoms,
2195 parsed_func.header.
2196 a_name));
2197 slang_function_destruct(&parsed_func);
2198 return GL_FALSE;
2199 }
2200
2201 /* destroy the existing function declaration and replace it
2202 * with the new one
2203 */
2204 slang_function_destruct(found_func);
2205 *found_func = parsed_func;
2206 }
2207 else {
2208 /* another declaration of the same function prototype - ignore it */
2209 slang_function_destruct(&parsed_func);
2210 }
2211
2212 /* return the found function */
2213 *parsed_func_ret = found_func;
2214 }
2215
2216 return GL_TRUE;
2217 }
2218
2219 /* declaration */
2220 #define DECLARATION_FUNCTION_PROTOTYPE 1
2221 #define DECLARATION_INIT_DECLARATOR_LIST 2
2222
2223 static int
2224 parse_declaration(slang_parse_ctx * C, slang_output_ctx * O)
2225 {
2226 switch (*C->I++) {
2227 case DECLARATION_INIT_DECLARATOR_LIST:
2228 if (!parse_init_declarator_list(C, O))
2229 RETURN0;
2230 break;
2231 case DECLARATION_FUNCTION_PROTOTYPE:
2232 {
2233 slang_function *dummy_func;
2234
2235 if (!parse_function(C, O, 0, &dummy_func))
2236 RETURN0;
2237 }
2238 break;
2239 default:
2240 RETURN0;
2241 }
2242 return 1;
2243 }
2244
2245 static int
2246 parse_default_precision(slang_parse_ctx * C, slang_output_ctx * O)
2247 {
2248 int precision, type;
2249
2250 if (!O->allow_precision) {
2251 slang_info_log_error(C->L, "syntax error at \"precision\"");
2252 RETURN0;
2253 }
2254
2255 precision = *C->I++;
2256 switch (precision) {
2257 case PRECISION_LOW:
2258 case PRECISION_MEDIUM:
2259 case PRECISION_HIGH:
2260 /* OK */
2261 break;
2262 default:
2263 _mesa_problem(NULL, "unexpected precision %d at %s:%d\n",
2264 precision, __FILE__, __LINE__);
2265 RETURN0;
2266 }
2267
2268 type = *C->I++;
2269 switch (type) {
2270 case TYPE_SPECIFIER_FLOAT:
2271 case TYPE_SPECIFIER_INT:
2272 case TYPE_SPECIFIER_SAMPLER1D:
2273 case TYPE_SPECIFIER_SAMPLER2D:
2274 case TYPE_SPECIFIER_SAMPLER3D:
2275 case TYPE_SPECIFIER_SAMPLERCUBE:
2276 case TYPE_SPECIFIER_SAMPLER1DSHADOW:
2277 case TYPE_SPECIFIER_SAMPLER2DSHADOW:
2278 case TYPE_SPECIFIER_SAMPLER2DRECT:
2279 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW:
2280 /* OK */
2281 break;
2282 default:
2283 _mesa_problem(NULL, "unexpected type %d at %s:%d\n",
2284 type, __FILE__, __LINE__);
2285 RETURN0;
2286 }
2287
2288 assert(type < TYPE_SPECIFIER_COUNT);
2289 O->default_precision[type] = precision;
2290
2291 return 1;
2292 }
2293
2294
2295 /**
2296 * Initialize the default precision for all types.
2297 * XXX this info isn't used yet.
2298 */
2299 static void
2300 init_default_precision(slang_output_ctx *O, slang_unit_type type)
2301 {
2302 GLuint i;
2303 for (i = 0; i < TYPE_SPECIFIER_COUNT; i++) {
2304 #if FEATURE_es2_glsl
2305 O->default_precision[i] = PRECISION_LOW;
2306 #else
2307 O->default_precision[i] = PRECISION_HIGH;
2308 #endif
2309 }
2310
2311 if (type == SLANG_UNIT_VERTEX_SHADER) {
2312 O->default_precision[TYPE_SPECIFIER_FLOAT] = PRECISION_HIGH;
2313 O->default_precision[TYPE_SPECIFIER_INT] = PRECISION_HIGH;
2314 }
2315 else {
2316 O->default_precision[TYPE_SPECIFIER_INT] = PRECISION_MEDIUM;
2317 }
2318 }
2319
2320
2321 static int
2322 parse_invariant(slang_parse_ctx * C, slang_output_ctx * O)
2323 {
2324 if (O->allow_invariant) {
2325 slang_atom *a = parse_identifier(C);
2326 /* XXX not doing anything with this var yet */
2327 /*printf("ID: %s\n", (char*) a);*/
2328 return a ? 1 : 0;
2329 }
2330 else {
2331 slang_info_log_error(C->L, "syntax error at \"invariant\"");
2332 RETURN0;
2333 }
2334 }
2335
2336
2337 /* external declaration or default precision specifier */
2338 #define EXTERNAL_NULL 0
2339 #define EXTERNAL_FUNCTION_DEFINITION 1
2340 #define EXTERNAL_DECLARATION 2
2341 #define DEFAULT_PRECISION 3
2342 #define INVARIANT_STMT 4
2343
2344
2345 static GLboolean
2346 parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
2347 struct gl_shader *shader)
2348 {
2349 GET_CURRENT_CONTEXT(ctx);
2350 slang_output_ctx o;
2351 GLboolean success;
2352 GLuint maxRegs;
2353 slang_function *mainFunc = NULL;
2354
2355 if (unit->type == SLANG_UNIT_FRAGMENT_BUILTIN ||
2356 unit->type == SLANG_UNIT_FRAGMENT_SHADER) {
2357 maxRegs = ctx->Const.FragmentProgram.MaxTemps;
2358 }
2359 else {
2360 assert(unit->type == SLANG_UNIT_VERTEX_BUILTIN ||
2361 unit->type == SLANG_UNIT_VERTEX_SHADER);
2362 maxRegs = ctx->Const.VertexProgram.MaxTemps;
2363 }
2364
2365 /* setup output context */
2366 o.funs = &unit->funs;
2367 o.structs = &unit->structs;
2368 o.vars = &unit->vars;
2369 o.program = shader ? shader->Program : NULL;
2370 o.vartable = _slang_new_var_table(maxRegs);
2371 _slang_push_var_table(o.vartable);
2372
2373 /* allow 'invariant' keyword? */
2374 #if FEATURE_es2_glsl
2375 o.allow_invariant = GL_TRUE;
2376 #else
2377 o.allow_invariant = (C->version >= 120) ? GL_TRUE : GL_FALSE;
2378 #endif
2379
2380 /* allow 'centroid' keyword? */
2381 o.allow_centroid = (C->version >= 120) ? GL_TRUE : GL_FALSE;
2382
2383 /* allow 'lowp/mediump/highp' keywords? */
2384 #if FEATURE_es2_glsl
2385 o.allow_precision = GL_TRUE;
2386 #else
2387 o.allow_precision = (C->version >= 120) ? GL_TRUE : GL_FALSE;
2388 #endif
2389 init_default_precision(&o, unit->type);
2390
2391 /* allow 'float[]' keyword? */
2392 o.allow_array_types = (C->version >= 120) ? GL_TRUE : GL_FALSE;
2393
2394 /* parse individual functions and declarations */
2395 while (*C->I != EXTERNAL_NULL) {
2396 switch (*C->I++) {
2397 case EXTERNAL_FUNCTION_DEFINITION:
2398 {
2399 slang_function *func;
2400 success = parse_function(C, &o, 1, &func);
2401 if (success &&
2402 _mesa_strcmp((char *) func->header.a_name, "main") == 0) {
2403 /* found main() */
2404 mainFunc = func;
2405 }
2406 }
2407 break;
2408 case EXTERNAL_DECLARATION:
2409 success = parse_declaration(C, &o);
2410 break;
2411 case DEFAULT_PRECISION:
2412 success = parse_default_precision(C, &o);
2413 break;
2414 case INVARIANT_STMT:
2415 success = parse_invariant(C, &o);
2416 break;
2417 default:
2418 success = GL_FALSE;
2419 }
2420
2421 if (!success) {
2422 /* xxx free codegen */
2423 _slang_pop_var_table(o.vartable);
2424 return GL_FALSE;
2425 }
2426 }
2427 C->I++;
2428
2429 if (mainFunc) {
2430 /* assemble (generate code) for main() */
2431 slang_assemble_ctx A;
2432
2433 A.atoms = C->atoms;
2434 A.space.funcs = o.funs;
2435 A.space.structs = o.structs;
2436 A.space.vars = o.vars;
2437 A.program = o.program;
2438 A.vartable = o.vartable;
2439 A.log = C->L;
2440
2441 /* main() takes no parameters */
2442 if (mainFunc->param_count > 0) {
2443 slang_info_log_error(A.log, "main() takes no arguments");
2444 return GL_FALSE;
2445 }
2446
2447 _slang_codegen_function(&A, mainFunc);
2448
2449 shader->Main = GL_TRUE; /* this shader defines main() */
2450 }
2451
2452 _slang_pop_var_table(o.vartable);
2453 _slang_delete_var_table(o.vartable);
2454
2455 return GL_TRUE;
2456 }
2457
2458 static GLboolean
2459 compile_binary(const byte * prod, slang_code_unit * unit,
2460 GLuint version,
2461 slang_unit_type type, slang_info_log * infolog,
2462 slang_code_unit * builtin, slang_code_unit * downlink,
2463 struct gl_shader *shader)
2464 {
2465 slang_parse_ctx C;
2466
2467 unit->type = type;
2468
2469 /* setup parse context */
2470 C.I = prod;
2471 C.L = infolog;
2472 C.parsing_builtin = (builtin == NULL);
2473 C.global_scope = GL_TRUE;
2474 C.atoms = &unit->object->atompool;
2475 C.type = type;
2476 C.version = version;
2477
2478 if (!check_revision(&C))
2479 return GL_FALSE;
2480
2481 if (downlink != NULL) {
2482 unit->vars.outer_scope = &downlink->vars;
2483 unit->funs.outer_scope = &downlink->funs;
2484 unit->structs.outer_scope = &downlink->structs;
2485 }
2486
2487 /* parse translation unit */
2488 return parse_code_unit(&C, unit, shader);
2489 }
2490
2491 static GLboolean
2492 compile_with_grammar(grammar id, const char *source, slang_code_unit * unit,
2493 slang_unit_type type, slang_info_log * infolog,
2494 slang_code_unit * builtin,
2495 struct gl_shader *shader,
2496 const struct gl_extensions *extensions)
2497 {
2498 byte *prod;
2499 GLuint size, start, version;
2500 slang_string preprocessed;
2501 GLuint maxVersion;
2502
2503 #if FEATURE_ARB_shading_language_120
2504 maxVersion = 120;
2505 #elif FEATURE_es2_glsl
2506 maxVersion = 100;
2507 #else
2508 maxVersion = 110;
2509 #endif
2510
2511 /* First retrieve the version number. */
2512 if (!_slang_preprocess_version(source, &version, &start, infolog))
2513 return GL_FALSE;
2514
2515 if (version > maxVersion) {
2516 slang_info_log_error(infolog,
2517 "language version %.2f is not supported.",
2518 version * 0.01);
2519 return GL_FALSE;
2520 }
2521
2522 /* Now preprocess the source string. */
2523 slang_string_init(&preprocessed);
2524 if (!_slang_preprocess_directives(&preprocessed, &source[start],
2525 infolog, extensions)) {
2526 slang_string_free(&preprocessed);
2527 slang_info_log_error(infolog, "failed to preprocess the source.");
2528 return GL_FALSE;
2529 }
2530
2531 /* Finally check the syntax and generate its binary representation. */
2532 if (!grammar_fast_check(id,
2533 (const byte *) (slang_string_cstr(&preprocessed)),
2534 &prod, &size, 65536)) {
2535 char buf[1024];
2536 GLint pos;
2537
2538 slang_string_free(&preprocessed);
2539 grammar_get_last_error((byte *) (buf), sizeof(buf), &pos);
2540 slang_info_log_error(infolog, buf);
2541 /* syntax error (possibly in library code) */
2542 #if 0
2543 {
2544 int line, col;
2545 char *s;
2546 s = (char *) _mesa_find_line_column((const GLubyte *) source,
2547 (const GLubyte *) source + pos,
2548 &line, &col);
2549 printf("Error on line %d, col %d: %s\n", line, col, s);
2550 }
2551 #endif
2552 return GL_FALSE;
2553 }
2554 slang_string_free(&preprocessed);
2555
2556 /* Syntax is okay - translate it to internal representation. */
2557 if (!compile_binary(prod, unit, version, type, infolog, builtin,
2558 &builtin[SLANG_BUILTIN_TOTAL - 1],
2559 shader)) {
2560 grammar_alloc_free(prod);
2561 return GL_FALSE;
2562 }
2563 grammar_alloc_free(prod);
2564 return GL_TRUE;
2565 }
2566
2567 LONGSTRING static const char *slang_shader_syn =
2568 #include "library/slang_shader_syn.h"
2569 ;
2570
2571 static const byte slang_core_gc[] = {
2572 #include "library/slang_core_gc.h"
2573 };
2574
2575 static const byte slang_120_core_gc[] = {
2576 #include "library/slang_120_core_gc.h"
2577 };
2578
2579 static const byte slang_120_fragment_gc[] = {
2580 #include "library/slang_builtin_120_fragment_gc.h"
2581 };
2582
2583 static const byte slang_common_builtin_gc[] = {
2584 #include "library/slang_common_builtin_gc.h"
2585 };
2586
2587 static const byte slang_fragment_builtin_gc[] = {
2588 #include "library/slang_fragment_builtin_gc.h"
2589 };
2590
2591 static const byte slang_vertex_builtin_gc[] = {
2592 #include "library/slang_vertex_builtin_gc.h"
2593 };
2594
2595 static GLboolean
2596 compile_object(grammar * id, const char *source, slang_code_object * object,
2597 slang_unit_type type, slang_info_log * infolog,
2598 struct gl_shader *shader,
2599 const struct gl_extensions *extensions)
2600 {
2601 slang_code_unit *builtins = NULL;
2602 GLuint base_version = 110;
2603
2604 /* load GLSL grammar */
2605 *id = grammar_load_from_text((const byte *) (slang_shader_syn));
2606 if (*id == 0) {
2607 byte buf[1024];
2608 int pos;
2609
2610 grammar_get_last_error(buf, 1024, &pos);
2611 slang_info_log_error(infolog, (const char *) (buf));
2612 return GL_FALSE;
2613 }
2614
2615 /* set shader type - the syntax is slightly different for different shaders */
2616 if (type == SLANG_UNIT_FRAGMENT_SHADER
2617 || type == SLANG_UNIT_FRAGMENT_BUILTIN)
2618 grammar_set_reg8(*id, (const byte *) "shader_type", 1);
2619 else
2620 grammar_set_reg8(*id, (const byte *) "shader_type", 2);
2621
2622 /* enable language extensions */
2623 grammar_set_reg8(*id, (const byte *) "parsing_builtin", 1);
2624
2625 /* if parsing user-specified shader, load built-in library */
2626 if (type == SLANG_UNIT_FRAGMENT_SHADER || type == SLANG_UNIT_VERTEX_SHADER) {
2627 /* compile core functionality first */
2628 if (!compile_binary(slang_core_gc,
2629 &object->builtin[SLANG_BUILTIN_CORE],
2630 base_version,
2631 SLANG_UNIT_FRAGMENT_BUILTIN, infolog,
2632 NULL, NULL, NULL))
2633 return GL_FALSE;
2634
2635 #if FEATURE_ARB_shading_language_120
2636 if (!compile_binary(slang_120_core_gc,
2637 &object->builtin[SLANG_BUILTIN_120_CORE],
2638 120,
2639 SLANG_UNIT_FRAGMENT_BUILTIN, infolog,
2640 NULL, &object->builtin[SLANG_BUILTIN_CORE], NULL))
2641 return GL_FALSE;
2642 #endif
2643
2644 /* compile common functions and variables, link to core */
2645 if (!compile_binary(slang_common_builtin_gc,
2646 &object->builtin[SLANG_BUILTIN_COMMON],
2647 #if FEATURE_ARB_shading_language_120
2648 120,
2649 #else
2650 base_version,
2651 #endif
2652 SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
2653 #if FEATURE_ARB_shading_language_120
2654 &object->builtin[SLANG_BUILTIN_120_CORE],
2655 #else
2656 &object->builtin[SLANG_BUILTIN_CORE],
2657 #endif
2658 NULL))
2659 return GL_FALSE;
2660
2661 /* compile target-specific functions and variables, link to common */
2662 if (type == SLANG_UNIT_FRAGMENT_SHADER) {
2663 if (!compile_binary(slang_fragment_builtin_gc,
2664 &object->builtin[SLANG_BUILTIN_TARGET],
2665 base_version,
2666 SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
2667 &object->builtin[SLANG_BUILTIN_COMMON], NULL))
2668 return GL_FALSE;
2669 #if FEATURE_ARB_shading_language_120
2670 if (!compile_binary(slang_120_fragment_gc,
2671 &object->builtin[SLANG_BUILTIN_TARGET],
2672 120,
2673 SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
2674 &object->builtin[SLANG_BUILTIN_COMMON], NULL))
2675 return GL_FALSE;
2676 #endif
2677 }
2678 else if (type == SLANG_UNIT_VERTEX_SHADER) {
2679 if (!compile_binary(slang_vertex_builtin_gc,
2680 &object->builtin[SLANG_BUILTIN_TARGET],
2681 base_version,
2682 SLANG_UNIT_VERTEX_BUILTIN, infolog, NULL,
2683 &object->builtin[SLANG_BUILTIN_COMMON], NULL))
2684 return GL_FALSE;
2685 }
2686
2687 /* disable language extensions */
2688 #if NEW_SLANG /* allow-built-ins */
2689 grammar_set_reg8(*id, (const byte *) "parsing_builtin", 1);
2690 #else
2691 grammar_set_reg8(*id, (const byte *) "parsing_builtin", 0);
2692 #endif
2693 builtins = object->builtin;
2694 }
2695
2696 /* compile the actual shader - pass-in built-in library for external shader */
2697 return compile_with_grammar(*id, source, &object->unit, type, infolog,
2698 builtins, shader, extensions);
2699 }
2700
2701
2702 static GLboolean
2703 compile_shader(GLcontext *ctx, slang_code_object * object,
2704 slang_unit_type type, slang_info_log * infolog,
2705 struct gl_shader *shader)
2706 {
2707 GLboolean success;
2708 grammar id = 0;
2709
2710 #if 0 /* for debug */
2711 _mesa_printf("********* COMPILE SHADER ***********\n");
2712 _mesa_printf("%s\n", shader->Source);
2713 _mesa_printf("************************************\n");
2714 #endif
2715
2716 assert(shader->Program);
2717
2718 _slang_code_object_dtr(object);
2719 _slang_code_object_ctr(object);
2720
2721 success = compile_object(&id, shader->Source, object, type, infolog, shader,
2722 &ctx->Extensions);
2723 if (id != 0)
2724 grammar_destroy(id);
2725 if (!success)
2726 return GL_FALSE;
2727
2728 return GL_TRUE;
2729 }
2730
2731
2732
2733 GLboolean
2734 _slang_compile(GLcontext *ctx, struct gl_shader *shader)
2735 {
2736 GLboolean success;
2737 slang_info_log info_log;
2738 slang_code_object obj;
2739 slang_unit_type type;
2740
2741 if (shader->Type == GL_VERTEX_SHADER) {
2742 type = SLANG_UNIT_VERTEX_SHADER;
2743 }
2744 else {
2745 assert(shader->Type == GL_FRAGMENT_SHADER);
2746 type = SLANG_UNIT_FRAGMENT_SHADER;
2747 }
2748
2749 if (!shader->Source)
2750 return GL_FALSE;
2751
2752 ctx->Shader.MemPool = _slang_new_mempool(1024*1024);
2753
2754 shader->Main = GL_FALSE;
2755
2756 if (!shader->Program) {
2757 GLenum progTarget;
2758 if (shader->Type == GL_VERTEX_SHADER)
2759 progTarget = GL_VERTEX_PROGRAM_ARB;
2760 else
2761 progTarget = GL_FRAGMENT_PROGRAM_ARB;
2762 shader->Program = ctx->Driver.NewProgram(ctx, progTarget, 1);
2763 shader->Program->Parameters = _mesa_new_parameter_list();
2764 shader->Program->Varying = _mesa_new_parameter_list();
2765 shader->Program->Attributes = _mesa_new_parameter_list();
2766 }
2767
2768 slang_info_log_construct(&info_log);
2769 _slang_code_object_ctr(&obj);
2770
2771 success = compile_shader(ctx, &obj, type, &info_log, shader);
2772
2773 /* free shader's prev info log */
2774 if (shader->InfoLog) {
2775 _mesa_free(shader->InfoLog);
2776 shader->InfoLog = NULL;
2777 }
2778
2779 if (info_log.text) {
2780 /* copy info-log string to shader object */
2781 shader->InfoLog = _mesa_strdup(info_log.text);
2782 }
2783
2784 if (info_log.error_flag) {
2785 success = GL_FALSE;
2786 }
2787
2788 slang_info_log_destruct(&info_log);
2789 _slang_code_object_dtr(&obj);
2790
2791 _slang_delete_mempool((slang_mempool *) ctx->Shader.MemPool);
2792 ctx->Shader.MemPool = NULL;
2793
2794 /* remove any reads of output registers */
2795 #if 0
2796 printf("Pre-remove output reads:\n");
2797 _mesa_print_program(shader->Program);
2798 #endif
2799 _mesa_remove_output_reads(shader->Program, PROGRAM_OUTPUT);
2800 if (shader->Type == GL_VERTEX_SHADER) {
2801 /* and remove writes to varying vars in vertex programs */
2802 _mesa_remove_output_reads(shader->Program, PROGRAM_VARYING);
2803 }
2804 #if 0
2805 printf("Post-remove output reads:\n");
2806 _mesa_print_program(shader->Program);
2807 #endif
2808
2809 return success;
2810 }
2811