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