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