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