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