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