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