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