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