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