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