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