619b0de0d0dd353226b3578a023328620e0994ef
[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
495 static int
496 parse_type_specifier(slang_parse_ctx * C, slang_output_ctx * O,
497 slang_type_specifier * spec)
498 {
499 switch (*C->I++) {
500 case TYPE_SPECIFIER_VOID:
501 spec->type = SLANG_SPEC_VOID;
502 break;
503 case TYPE_SPECIFIER_BOOL:
504 spec->type = SLANG_SPEC_BOOL;
505 break;
506 case TYPE_SPECIFIER_BVEC2:
507 spec->type = SLANG_SPEC_BVEC2;
508 break;
509 case TYPE_SPECIFIER_BVEC3:
510 spec->type = SLANG_SPEC_BVEC3;
511 break;
512 case TYPE_SPECIFIER_BVEC4:
513 spec->type = SLANG_SPEC_BVEC4;
514 break;
515 case TYPE_SPECIFIER_INT:
516 spec->type = SLANG_SPEC_INT;
517 break;
518 case TYPE_SPECIFIER_IVEC2:
519 spec->type = SLANG_SPEC_IVEC2;
520 break;
521 case TYPE_SPECIFIER_IVEC3:
522 spec->type = SLANG_SPEC_IVEC3;
523 break;
524 case TYPE_SPECIFIER_IVEC4:
525 spec->type = SLANG_SPEC_IVEC4;
526 break;
527 case TYPE_SPECIFIER_FLOAT:
528 spec->type = SLANG_SPEC_FLOAT;
529 break;
530 case TYPE_SPECIFIER_VEC2:
531 spec->type = SLANG_SPEC_VEC2;
532 break;
533 case TYPE_SPECIFIER_VEC3:
534 spec->type = SLANG_SPEC_VEC3;
535 break;
536 case TYPE_SPECIFIER_VEC4:
537 spec->type = SLANG_SPEC_VEC4;
538 break;
539 case TYPE_SPECIFIER_MAT2:
540 spec->type = SLANG_SPEC_MAT2;
541 break;
542 case TYPE_SPECIFIER_MAT3:
543 spec->type = SLANG_SPEC_MAT3;
544 break;
545 case TYPE_SPECIFIER_MAT4:
546 spec->type = SLANG_SPEC_MAT4;
547 break;
548 case TYPE_SPECIFIER_SAMPLER1D:
549 spec->type = SLANG_SPEC_SAMPLER1D;
550 break;
551 case TYPE_SPECIFIER_SAMPLER2D:
552 spec->type = SLANG_SPEC_SAMPLER2D;
553 break;
554 case TYPE_SPECIFIER_SAMPLER3D:
555 spec->type = SLANG_SPEC_SAMPLER3D;
556 break;
557 case TYPE_SPECIFIER_SAMPLERCUBE:
558 spec->type = SLANG_SPEC_SAMPLERCUBE;
559 break;
560 case TYPE_SPECIFIER_SAMPLER2DRECT:
561 spec->type = SLANG_SPEC_SAMPLER2DRECT;
562 break;
563 case TYPE_SPECIFIER_SAMPLER1DSHADOW:
564 spec->type = SLANG_SPEC_SAMPLER1DSHADOW;
565 break;
566 case TYPE_SPECIFIER_SAMPLER2DSHADOW:
567 spec->type = SLANG_SPEC_SAMPLER2DSHADOW;
568 break;
569 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW:
570 spec->type = SLANG_SPEC_SAMPLER2DRECTSHADOW;
571 break;
572 case TYPE_SPECIFIER_STRUCT:
573 spec->type = SLANG_SPEC_STRUCT;
574 if (!parse_struct(C, O, &spec->_struct))
575 return 0;
576 break;
577 case TYPE_SPECIFIER_TYPENAME:
578 spec->type = SLANG_SPEC_STRUCT;
579 {
580 slang_atom a_name;
581 slang_struct *stru;
582
583 a_name = parse_identifier(C);
584 if (a_name == NULL)
585 return 0;
586
587 stru = slang_struct_scope_find(O->structs, a_name, 1);
588 if (stru == NULL) {
589 slang_info_log_error(C->L, "undeclared type name '%s'",
590 slang_atom_pool_id(C->atoms, a_name));
591 return 0;
592 }
593
594 spec->_struct =
595 (slang_struct *) slang_alloc_malloc(sizeof(slang_struct));
596 if (spec->_struct == NULL) {
597 slang_info_log_memory(C->L);
598 return 0;
599 }
600 if (!slang_struct_construct(spec->_struct)) {
601 slang_alloc_free(spec->_struct);
602 spec->_struct = NULL;
603 return 0;
604 }
605 if (!slang_struct_copy(spec->_struct, stru))
606 return 0;
607 }
608 break;
609 default:
610 return 0;
611 }
612 return 1;
613 }
614
615 static int
616 parse_fully_specified_type(slang_parse_ctx * C, slang_output_ctx * O,
617 slang_fully_specified_type * type)
618 {
619 if (!parse_type_qualifier(C, &type->qualifier))
620 return 0;
621 if (!parse_type_specifier(C, O, &type->specifier))
622 return 0;
623 return 1;
624 }
625
626 /* operation */
627 #define OP_END 0
628 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
629 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
630 #define OP_DECLARE 3
631 #define OP_ASM 4
632 #define OP_BREAK 5
633 #define OP_CONTINUE 6
634 #define OP_DISCARD 7
635 #define OP_RETURN 8
636 #define OP_EXPRESSION 9
637 #define OP_IF 10
638 #define OP_WHILE 11
639 #define OP_DO 12
640 #define OP_FOR 13
641 #define OP_PUSH_VOID 14
642 #define OP_PUSH_BOOL 15
643 #define OP_PUSH_INT 16
644 #define OP_PUSH_FLOAT 17
645 #define OP_PUSH_IDENTIFIER 18
646 #define OP_SEQUENCE 19
647 #define OP_ASSIGN 20
648 #define OP_ADDASSIGN 21
649 #define OP_SUBASSIGN 22
650 #define OP_MULASSIGN 23
651 #define OP_DIVASSIGN 24
652 /*#define OP_MODASSIGN 25*/
653 /*#define OP_LSHASSIGN 26*/
654 /*#define OP_RSHASSIGN 27*/
655 /*#define OP_ORASSIGN 28*/
656 /*#define OP_XORASSIGN 29*/
657 /*#define OP_ANDASSIGN 30*/
658 #define OP_SELECT 31
659 #define OP_LOGICALOR 32
660 #define OP_LOGICALXOR 33
661 #define OP_LOGICALAND 34
662 /*#define OP_BITOR 35*/
663 /*#define OP_BITXOR 36*/
664 /*#define OP_BITAND 37*/
665 #define OP_EQUAL 38
666 #define OP_NOTEQUAL 39
667 #define OP_LESS 40
668 #define OP_GREATER 41
669 #define OP_LESSEQUAL 42
670 #define OP_GREATEREQUAL 43
671 /*#define OP_LSHIFT 44*/
672 /*#define OP_RSHIFT 45*/
673 #define OP_ADD 46
674 #define OP_SUBTRACT 47
675 #define OP_MULTIPLY 48
676 #define OP_DIVIDE 49
677 /*#define OP_MODULUS 50*/
678 #define OP_PREINCREMENT 51
679 #define OP_PREDECREMENT 52
680 #define OP_PLUS 53
681 #define OP_MINUS 54
682 /*#define OP_COMPLEMENT 55*/
683 #define OP_NOT 56
684 #define OP_SUBSCRIPT 57
685 #define OP_CALL 58
686 #define OP_FIELD 59
687 #define OP_POSTINCREMENT 60
688 #define OP_POSTDECREMENT 61
689
690
691 /**
692 * When parsing a compound production, this function is used to parse the
693 * children.
694 * For example, a a while-loop compound will have two children, the
695 * while condition expression and the loop body. So, this function will
696 * be called twice to parse those two sub-expressions.
697 * \param C the parsing context
698 * \param O the output context
699 * \param oper the operation we're parsing
700 * \param statement indicates whether parsing a statement, or expression
701 * \return 1 if success, 0 if error
702 */
703 static int
704 parse_child_operation(slang_parse_ctx * C, slang_output_ctx * O,
705 slang_operation * oper, GLboolean statement)
706 {
707 slang_operation *ch;
708
709 /* grow child array */
710 ch = slang_operation_grow(&oper->num_children, &oper->children);
711 if (statement)
712 return parse_statement(C, O, ch);
713 return parse_expression(C, O, ch);
714 }
715
716 static int parse_declaration(slang_parse_ctx * C, slang_output_ctx * O);
717
718 static int
719 parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
720 slang_operation * oper)
721 {
722 oper->locals->outer_scope = O->vars;
723 switch (*C->I++) {
724 case OP_BLOCK_BEGIN_NO_NEW_SCOPE:
725 /* parse child statements, do not create new variable scope */
726 oper->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE;
727 while (*C->I != OP_END)
728 if (!parse_child_operation(C, O, oper, 1))
729 return 0;
730 C->I++;
731 break;
732 case OP_BLOCK_BEGIN_NEW_SCOPE:
733 /* parse child statements, create new variable scope */
734 {
735 slang_output_ctx o = *O;
736
737 oper->type = SLANG_OPER_BLOCK_NEW_SCOPE;
738 o.vars = oper->locals;
739 while (*C->I != OP_END)
740 if (!parse_child_operation(C, &o, oper, 1))
741 return 0;
742 C->I++;
743 }
744 break;
745 case OP_DECLARE:
746 /* local variable declaration, individual declarators are stored as
747 * children identifiers
748 */
749 oper->type = SLANG_OPER_BLOCK_NO_NEW_SCOPE;
750 {
751 const unsigned int first_var = O->vars->num_variables;
752
753 /* parse the declaration, note that there can be zero or more
754 * than one declarators
755 */
756 if (!parse_declaration(C, O))
757 return 0;
758 if (first_var < O->vars->num_variables) {
759 const unsigned int num_vars = O->vars->num_variables - first_var;
760 unsigned int i;
761
762 oper->num_children = num_vars;
763 oper->children = slang_operation_new(num_vars);
764 if (oper->children == NULL) {
765 slang_info_log_memory(C->L);
766 return 0;
767 }
768 for (i = first_var; i < O->vars->num_variables; i++) {
769 slang_operation *o = &oper->children[i - first_var];
770 o->type = SLANG_OPER_VARIABLE_DECL;
771 o->locals->outer_scope = O->vars;
772 o->a_id = O->vars->variables[i]->a_name;
773 }
774 }
775 }
776 break;
777 case OP_ASM:
778 /* the __asm statement, parse the mnemonic and all its arguments
779 * as expressions
780 */
781 oper->type = SLANG_OPER_ASM;
782 oper->a_id = parse_identifier(C);
783 if (oper->a_id == SLANG_ATOM_NULL)
784 return 0;
785 while (*C->I != OP_END) {
786 if (!parse_child_operation(C, O, oper, 0))
787 return 0;
788 }
789 C->I++;
790 break;
791 case OP_BREAK:
792 oper->type = SLANG_OPER_BREAK;
793 break;
794 case OP_CONTINUE:
795 oper->type = SLANG_OPER_CONTINUE;
796 break;
797 case OP_DISCARD:
798 oper->type = SLANG_OPER_DISCARD;
799 break;
800 case OP_RETURN:
801 oper->type = SLANG_OPER_RETURN;
802 if (!parse_child_operation(C, O, oper, 0))
803 return 0;
804 break;
805 case OP_EXPRESSION:
806 oper->type = SLANG_OPER_EXPRESSION;
807 if (!parse_child_operation(C, O, oper, 0))
808 return 0;
809 break;
810 case OP_IF:
811 oper->type = SLANG_OPER_IF;
812 if (!parse_child_operation(C, O, oper, 0))
813 return 0;
814 if (!parse_child_operation(C, O, oper, 1))
815 return 0;
816 if (!parse_child_operation(C, O, oper, 1))
817 return 0;
818 break;
819 case OP_WHILE:
820 {
821 slang_output_ctx o = *O;
822
823 oper->type = SLANG_OPER_WHILE;
824 o.vars = oper->locals;
825 if (!parse_child_operation(C, &o, oper, 1))
826 return 0;
827 if (!parse_child_operation(C, &o, oper, 1))
828 return 0;
829 }
830 break;
831 case OP_DO:
832 oper->type = SLANG_OPER_DO;
833 if (!parse_child_operation(C, O, oper, 1))
834 return 0;
835 if (!parse_child_operation(C, O, oper, 0))
836 return 0;
837 break;
838 case OP_FOR:
839 {
840 slang_output_ctx o = *O;
841
842 oper->type = SLANG_OPER_FOR;
843 o.vars = oper->locals;
844 if (!parse_child_operation(C, &o, oper, 1))
845 return 0;
846 if (!parse_child_operation(C, &o, oper, 1))
847 return 0;
848 if (!parse_child_operation(C, &o, oper, 0))
849 return 0;
850 if (!parse_child_operation(C, &o, oper, 1))
851 return 0;
852 }
853 break;
854 default:
855 return 0;
856 }
857 return 1;
858 }
859
860 static int
861 handle_nary_expression(slang_parse_ctx * C, slang_operation * op,
862 slang_operation ** ops, unsigned int *total_ops,
863 unsigned int n)
864 {
865 unsigned int i;
866
867 op->children =
868 (slang_operation *) slang_alloc_malloc(n * sizeof(slang_operation));
869 if (op->children == NULL) {
870 slang_info_log_memory(C->L);
871 return 0;
872 }
873 op->num_children = n;
874
875 for (i = 0; i < n; i++)
876 op->children[i] = (*ops)[*total_ops - (n + 1 - i)];
877 (*ops)[*total_ops - (n + 1)] = (*ops)[*total_ops - 1];
878 *total_ops -= n;
879
880 *ops = (slang_operation *)
881 slang_alloc_realloc(*ops,
882 (*total_ops + n) * sizeof(slang_operation),
883 *total_ops * sizeof(slang_operation));
884 if (*ops == NULL) {
885 slang_info_log_memory(C->L);
886 return 0;
887 }
888 return 1;
889 }
890
891 static int
892 is_constructor_name(const char *name, slang_atom a_name,
893 slang_struct_scope * structs)
894 {
895 if (slang_type_specifier_type_from_string(name) != SLANG_SPEC_VOID)
896 return 1;
897 return slang_struct_scope_find(structs, a_name, 1) != NULL;
898 }
899
900 static int
901 parse_expression(slang_parse_ctx * C, slang_output_ctx * O,
902 slang_operation * oper)
903 {
904 slang_operation *ops = NULL;
905 unsigned int num_ops = 0;
906 int number;
907
908 while (*C->I != OP_END) {
909 slang_operation *op;
910 const unsigned int op_code = *C->I++;
911
912 /* allocate default operation, becomes a no-op if not used */
913 ops = (slang_operation *)
914 slang_alloc_realloc(ops,
915 num_ops * sizeof(slang_operation),
916 (num_ops + 1) * sizeof(slang_operation));
917 if (ops == NULL) {
918 slang_info_log_memory(C->L);
919 return 0;
920 }
921 op = &ops[num_ops];
922 if (!slang_operation_construct(op)) {
923 slang_info_log_memory(C->L);
924 return 0;
925 }
926 num_ops++;
927 op->locals->outer_scope = O->vars;
928
929 switch (op_code) {
930 case OP_PUSH_VOID:
931 op->type = SLANG_OPER_VOID;
932 break;
933 case OP_PUSH_BOOL:
934 op->type = SLANG_OPER_LITERAL_BOOL;
935 if (!parse_number(C, &number))
936 return 0;
937 op->literal[0] =
938 op->literal[1] =
939 op->literal[2] =
940 op->literal[3] = (GLfloat) number;
941 op->literal_size = 1;
942 break;
943 case OP_PUSH_INT:
944 op->type = SLANG_OPER_LITERAL_INT;
945 if (!parse_number(C, &number))
946 return 0;
947 op->literal[0] =
948 op->literal[1] =
949 op->literal[2] =
950 op->literal[3] = (GLfloat) number;
951 op->literal_size = 1;
952 break;
953 case OP_PUSH_FLOAT:
954 op->type = SLANG_OPER_LITERAL_FLOAT;
955 if (!parse_float(C, &op->literal[0]))
956 return 0;
957 op->literal[1] =
958 op->literal[2] =
959 op->literal[3] = op->literal[0];
960 op->literal_size = 1;
961 break;
962 case OP_PUSH_IDENTIFIER:
963 op->type = SLANG_OPER_IDENTIFIER;
964 op->a_id = parse_identifier(C);
965 if (op->a_id == SLANG_ATOM_NULL)
966 return 0;
967 break;
968 case OP_SEQUENCE:
969 op->type = SLANG_OPER_SEQUENCE;
970 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
971 return 0;
972 break;
973 case OP_ASSIGN:
974 op->type = SLANG_OPER_ASSIGN;
975 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
976 return 0;
977 break;
978 case OP_ADDASSIGN:
979 op->type = SLANG_OPER_ADDASSIGN;
980 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
981 return 0;
982 break;
983 case OP_SUBASSIGN:
984 op->type = SLANG_OPER_SUBASSIGN;
985 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
986 return 0;
987 break;
988 case OP_MULASSIGN:
989 op->type = SLANG_OPER_MULASSIGN;
990 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
991 return 0;
992 break;
993 case OP_DIVASSIGN:
994 op->type = SLANG_OPER_DIVASSIGN;
995 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
996 return 0;
997 break;
998 /*case OP_MODASSIGN: */
999 /*case OP_LSHASSIGN: */
1000 /*case OP_RSHASSIGN: */
1001 /*case OP_ORASSIGN: */
1002 /*case OP_XORASSIGN: */
1003 /*case OP_ANDASSIGN: */
1004 case OP_SELECT:
1005 op->type = SLANG_OPER_SELECT;
1006 if (!handle_nary_expression(C, op, &ops, &num_ops, 3))
1007 return 0;
1008 break;
1009 case OP_LOGICALOR:
1010 op->type = SLANG_OPER_LOGICALOR;
1011 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1012 return 0;
1013 break;
1014 case OP_LOGICALXOR:
1015 op->type = SLANG_OPER_LOGICALXOR;
1016 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1017 return 0;
1018 break;
1019 case OP_LOGICALAND:
1020 op->type = SLANG_OPER_LOGICALAND;
1021 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1022 return 0;
1023 break;
1024 /*case OP_BITOR: */
1025 /*case OP_BITXOR: */
1026 /*case OP_BITAND: */
1027 case OP_EQUAL:
1028 op->type = SLANG_OPER_EQUAL;
1029 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1030 return 0;
1031 break;
1032 case OP_NOTEQUAL:
1033 op->type = SLANG_OPER_NOTEQUAL;
1034 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1035 return 0;
1036 break;
1037 case OP_LESS:
1038 op->type = SLANG_OPER_LESS;
1039 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1040 return 0;
1041 break;
1042 case OP_GREATER:
1043 op->type = SLANG_OPER_GREATER;
1044 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1045 return 0;
1046 break;
1047 case OP_LESSEQUAL:
1048 op->type = SLANG_OPER_LESSEQUAL;
1049 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1050 return 0;
1051 break;
1052 case OP_GREATEREQUAL:
1053 op->type = SLANG_OPER_GREATEREQUAL;
1054 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1055 return 0;
1056 break;
1057 /*case OP_LSHIFT: */
1058 /*case OP_RSHIFT: */
1059 case OP_ADD:
1060 op->type = SLANG_OPER_ADD;
1061 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1062 return 0;
1063 break;
1064 case OP_SUBTRACT:
1065 op->type = SLANG_OPER_SUBTRACT;
1066 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1067 return 0;
1068 break;
1069 case OP_MULTIPLY:
1070 op->type = SLANG_OPER_MULTIPLY;
1071 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1072 return 0;
1073 break;
1074 case OP_DIVIDE:
1075 op->type = SLANG_OPER_DIVIDE;
1076 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1077 return 0;
1078 break;
1079 /*case OP_MODULUS: */
1080 case OP_PREINCREMENT:
1081 op->type = SLANG_OPER_PREINCREMENT;
1082 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1083 return 0;
1084 break;
1085 case OP_PREDECREMENT:
1086 op->type = SLANG_OPER_PREDECREMENT;
1087 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1088 return 0;
1089 break;
1090 case OP_PLUS:
1091 op->type = SLANG_OPER_PLUS;
1092 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1093 return 0;
1094 break;
1095 case OP_MINUS:
1096 op->type = SLANG_OPER_MINUS;
1097 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1098 return 0;
1099 break;
1100 case OP_NOT:
1101 op->type = SLANG_OPER_NOT;
1102 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1103 return 0;
1104 break;
1105 /*case OP_COMPLEMENT: */
1106 case OP_SUBSCRIPT:
1107 op->type = SLANG_OPER_SUBSCRIPT;
1108 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1109 return 0;
1110 break;
1111 case OP_CALL:
1112 op->type = SLANG_OPER_CALL;
1113 op->a_id = parse_identifier(C);
1114 if (op->a_id == SLANG_ATOM_NULL)
1115 return 0;
1116 while (*C->I != OP_END)
1117 if (!parse_child_operation(C, O, op, 0))
1118 return 0;
1119 C->I++;
1120
1121 if (!C->parsing_builtin
1122 && !slang_function_scope_find_by_name(O->funs, op->a_id, 1)) {
1123 const char *id;
1124
1125 id = slang_atom_pool_id(C->atoms, op->a_id);
1126 if (!is_constructor_name(id, op->a_id, O->structs)) {
1127 slang_info_log_error(C->L, "%s: undeclared function name.", id);
1128 return 0;
1129 }
1130 }
1131 break;
1132 case OP_FIELD:
1133 op->type = SLANG_OPER_FIELD;
1134 op->a_id = parse_identifier(C);
1135 if (op->a_id == SLANG_ATOM_NULL)
1136 return 0;
1137 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1138 return 0;
1139 break;
1140 case OP_POSTINCREMENT:
1141 op->type = SLANG_OPER_POSTINCREMENT;
1142 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1143 return 0;
1144 break;
1145 case OP_POSTDECREMENT:
1146 op->type = SLANG_OPER_POSTDECREMENT;
1147 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1148 return 0;
1149 break;
1150 default:
1151 return 0;
1152 }
1153 }
1154 C->I++;
1155
1156 *oper = *ops;
1157 slang_alloc_free(ops);
1158
1159 return 1;
1160 }
1161
1162 /* parameter qualifier */
1163 #define PARAM_QUALIFIER_IN 0
1164 #define PARAM_QUALIFIER_OUT 1
1165 #define PARAM_QUALIFIER_INOUT 2
1166
1167 /* function parameter array presence */
1168 #define PARAMETER_ARRAY_NOT_PRESENT 0
1169 #define PARAMETER_ARRAY_PRESENT 1
1170
1171 static int
1172 parse_parameter_declaration(slang_parse_ctx * C, slang_output_ctx * O,
1173 slang_variable * param)
1174 {
1175 /* parse and validate the parameter's type qualifiers (there can be
1176 * two at most) because not all combinations are valid
1177 */
1178 if (!parse_type_qualifier(C, &param->type.qualifier))
1179 return 0;
1180 switch (*C->I++) {
1181 case PARAM_QUALIFIER_IN:
1182 if (param->type.qualifier != SLANG_QUAL_CONST
1183 && param->type.qualifier != SLANG_QUAL_NONE) {
1184 slang_info_log_error(C->L, "Invalid type qualifier.");
1185 return 0;
1186 }
1187 break;
1188 case PARAM_QUALIFIER_OUT:
1189 if (param->type.qualifier == SLANG_QUAL_NONE)
1190 param->type.qualifier = SLANG_QUAL_OUT;
1191 else {
1192 slang_info_log_error(C->L, "Invalid type qualifier.");
1193 return 0;
1194 }
1195 break;
1196 case PARAM_QUALIFIER_INOUT:
1197 if (param->type.qualifier == SLANG_QUAL_NONE)
1198 param->type.qualifier = SLANG_QUAL_INOUT;
1199 else {
1200 slang_info_log_error(C->L, "Invalid type qualifier.");
1201 return 0;
1202 }
1203 break;
1204 default:
1205 return 0;
1206 }
1207
1208 /* parse parameter's type specifier and name */
1209 if (!parse_type_specifier(C, O, &param->type.specifier))
1210 return 0;
1211 param->a_name = parse_identifier(C);
1212 if (param->a_name == SLANG_ATOM_NULL)
1213 return 0;
1214
1215 /* if the parameter is an array, parse its size (the size must be
1216 * explicitly defined
1217 */
1218 if (*C->I++ == PARAMETER_ARRAY_PRESENT) {
1219 slang_type_specifier p;
1220
1221 slang_type_specifier_ctr(&p);
1222 if (!slang_type_specifier_copy(&p, &param->type.specifier)) {
1223 slang_type_specifier_dtr(&p);
1224 return GL_FALSE;
1225 }
1226 if (!convert_to_array(C, param, &p)) {
1227 slang_type_specifier_dtr(&p);
1228 return GL_FALSE;
1229 }
1230 slang_type_specifier_dtr(&p);
1231 if (!parse_array_len(C, O, &param->array_len))
1232 return GL_FALSE;
1233 }
1234
1235 /* calculate the parameter size */
1236 if (!calculate_var_size(C, O, param))
1237 return GL_FALSE;
1238
1239 /* TODO: allocate the local address here? */
1240 return 1;
1241 }
1242
1243 /* function type */
1244 #define FUNCTION_ORDINARY 0
1245 #define FUNCTION_CONSTRUCTOR 1
1246 #define FUNCTION_OPERATOR 2
1247
1248 /* function parameter */
1249 #define PARAMETER_NONE 0
1250 #define PARAMETER_NEXT 1
1251
1252 /* operator type */
1253 #define OPERATOR_ADDASSIGN 1
1254 #define OPERATOR_SUBASSIGN 2
1255 #define OPERATOR_MULASSIGN 3
1256 #define OPERATOR_DIVASSIGN 4
1257 /*#define OPERATOR_MODASSIGN 5*/
1258 /*#define OPERATOR_LSHASSIGN 6*/
1259 /*#define OPERATOR_RSHASSIGN 7*/
1260 /*#define OPERATOR_ANDASSIGN 8*/
1261 /*#define OPERATOR_XORASSIGN 9*/
1262 /*#define OPERATOR_ORASSIGN 10*/
1263 #define OPERATOR_LOGICALXOR 11
1264 /*#define OPERATOR_BITOR 12*/
1265 /*#define OPERATOR_BITXOR 13*/
1266 /*#define OPERATOR_BITAND 14*/
1267 #define OPERATOR_LESS 15
1268 #define OPERATOR_GREATER 16
1269 #define OPERATOR_LESSEQUAL 17
1270 #define OPERATOR_GREATEREQUAL 18
1271 /*#define OPERATOR_LSHIFT 19*/
1272 /*#define OPERATOR_RSHIFT 20*/
1273 #define OPERATOR_MULTIPLY 21
1274 #define OPERATOR_DIVIDE 22
1275 /*#define OPERATOR_MODULUS 23*/
1276 #define OPERATOR_INCREMENT 24
1277 #define OPERATOR_DECREMENT 25
1278 #define OPERATOR_PLUS 26
1279 #define OPERATOR_MINUS 27
1280 /*#define OPERATOR_COMPLEMENT 28*/
1281 #define OPERATOR_NOT 29
1282
1283 static const struct
1284 {
1285 unsigned int o_code;
1286 const char *o_name;
1287 } operator_names[] = {
1288 {OPERATOR_INCREMENT, "++"},
1289 {OPERATOR_ADDASSIGN, "+="},
1290 {OPERATOR_PLUS, "+"},
1291 {OPERATOR_DECREMENT, "--"},
1292 {OPERATOR_SUBASSIGN, "-="},
1293 {OPERATOR_MINUS, "-"},
1294 {OPERATOR_NOT, "!"},
1295 {OPERATOR_MULASSIGN, "*="},
1296 {OPERATOR_MULTIPLY, "*"},
1297 {OPERATOR_DIVASSIGN, "/="},
1298 {OPERATOR_DIVIDE, "/"},
1299 {OPERATOR_LESSEQUAL, "<="},
1300 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1301 /*{ OPERATOR_LSHIFT, "<<" }, */
1302 {OPERATOR_LESS, "<"},
1303 {OPERATOR_GREATEREQUAL, ">="},
1304 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1305 /*{ OPERATOR_RSHIFT, ">>" }, */
1306 {OPERATOR_GREATER, ">"},
1307 /*{ OPERATOR_MODASSIGN, "%=" }, */
1308 /*{ OPERATOR_MODULUS, "%" }, */
1309 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1310 /*{ OPERATOR_BITAND, "&" }, */
1311 /*{ OPERATOR_ORASSIGN, "|=" }, */
1312 /*{ OPERATOR_BITOR, "|" }, */
1313 /*{ OPERATOR_COMPLEMENT, "~" }, */
1314 /*{ OPERATOR_XORASSIGN, "^=" }, */
1315 {OPERATOR_LOGICALXOR, "^^"},
1316 /*{ OPERATOR_BITXOR, "^" } */
1317 };
1318
1319 static slang_atom
1320 parse_operator_name(slang_parse_ctx * C)
1321 {
1322 unsigned int i;
1323
1324 for (i = 0; i < sizeof(operator_names) / sizeof(*operator_names); i++) {
1325 if (operator_names[i].o_code == (unsigned int) (*C->I)) {
1326 slang_atom atom =
1327 slang_atom_pool_atom(C->atoms, operator_names[i].o_name);
1328 if (atom == SLANG_ATOM_NULL) {
1329 slang_info_log_memory(C->L);
1330 return 0;
1331 }
1332 C->I++;
1333 return atom;
1334 }
1335 }
1336 return 0;
1337 }
1338
1339 static int
1340 parse_function_prototype(slang_parse_ctx * C, slang_output_ctx * O,
1341 slang_function * func)
1342 {
1343 /* parse function type and name */
1344 if (!parse_fully_specified_type(C, O, &func->header.type))
1345 return 0;
1346 switch (*C->I++) {
1347 case FUNCTION_ORDINARY:
1348 func->kind = SLANG_FUNC_ORDINARY;
1349 func->header.a_name = parse_identifier(C);
1350 if (func->header.a_name == SLANG_ATOM_NULL)
1351 return 0;
1352 break;
1353 case FUNCTION_CONSTRUCTOR:
1354 func->kind = SLANG_FUNC_CONSTRUCTOR;
1355 if (func->header.type.specifier.type == SLANG_SPEC_STRUCT)
1356 return 0;
1357 func->header.a_name =
1358 slang_atom_pool_atom(C->atoms,
1359 slang_type_specifier_type_to_string
1360 (func->header.type.specifier.type));
1361 if (func->header.a_name == SLANG_ATOM_NULL) {
1362 slang_info_log_memory(C->L);
1363 return 0;
1364 }
1365 break;
1366 case FUNCTION_OPERATOR:
1367 func->kind = SLANG_FUNC_OPERATOR;
1368 func->header.a_name = parse_operator_name(C);
1369 if (func->header.a_name == SLANG_ATOM_NULL)
1370 return 0;
1371 break;
1372 default:
1373 return 0;
1374 }
1375
1376 /* parse function parameters */
1377 while (*C->I++ == PARAMETER_NEXT) {
1378 slang_variable *p = slang_variable_scope_grow(func->parameters);
1379 if (!p) {
1380 slang_info_log_memory(C->L);
1381 return 0;
1382 }
1383 if (!parse_parameter_declaration(C, O, p))
1384 return 0;
1385 }
1386
1387 /* if the function returns a value, append a hidden __retVal 'out'
1388 * parameter that corresponds to the return value.
1389 */
1390 if (_slang_function_has_return_value(func)) {
1391 slang_variable *p = slang_variable_scope_grow(func->parameters);
1392 slang_atom a_retVal = slang_atom_pool_atom(C->atoms, "__retVal");
1393 assert(a_retVal);
1394 p->a_name = a_retVal;
1395 p->type = func->header.type;
1396 p->type.qualifier = SLANG_QUAL_OUT;
1397 }
1398
1399 /* function formal parameters and local variables share the same
1400 * scope, so save the information about param count in a seperate
1401 * place also link the scope to the global variable scope so when a
1402 * given identifier is not found here, the search process continues
1403 * in the global space
1404 */
1405 func->param_count = func->parameters->num_variables;
1406 func->parameters->outer_scope = O->vars;
1407
1408 return 1;
1409 }
1410
1411 static int
1412 parse_function_definition(slang_parse_ctx * C, slang_output_ctx * O,
1413 slang_function * func)
1414 {
1415 slang_output_ctx o = *O;
1416
1417 if (!parse_function_prototype(C, O, func))
1418 return 0;
1419
1420 /* create function's body operation */
1421 func->body =
1422 (slang_operation *) slang_alloc_malloc(sizeof(slang_operation));
1423 if (func->body == NULL) {
1424 slang_info_log_memory(C->L);
1425 return 0;
1426 }
1427 if (!slang_operation_construct(func->body)) {
1428 slang_alloc_free(func->body);
1429 func->body = NULL;
1430 slang_info_log_memory(C->L);
1431 return 0;
1432 }
1433
1434 /* to parse the body the parse context is modified in order to
1435 * capture parsed variables into function's local variable scope
1436 */
1437 C->global_scope = GL_FALSE;
1438 o.vars = func->parameters;
1439 if (!parse_statement(C, &o, func->body))
1440 return 0;
1441
1442 C->global_scope = GL_TRUE;
1443 return 1;
1444 }
1445
1446 static GLboolean
1447 initialize_global(slang_assemble_ctx * A, slang_variable * var)
1448 {
1449 slang_operation op_id, op_assign;
1450 GLboolean result;
1451
1452 /* construct the left side of assignment */
1453 if (!slang_operation_construct(&op_id))
1454 return GL_FALSE;
1455 op_id.type = SLANG_OPER_IDENTIFIER;
1456 op_id.a_id = var->a_name;
1457
1458 /* put the variable into operation's scope */
1459 op_id.locals->variables =
1460 (slang_variable **) slang_alloc_malloc(sizeof(slang_variable *));
1461 if (op_id.locals->variables == NULL) {
1462 slang_operation_destruct(&op_id);
1463 return GL_FALSE;
1464 }
1465 op_id.locals->num_variables = 1;
1466 op_id.locals->variables[0] = var;
1467
1468 /* construct the assignment expression */
1469 if (!slang_operation_construct(&op_assign)) {
1470 op_id.locals->num_variables = 0;
1471 slang_operation_destruct(&op_id);
1472 return GL_FALSE;
1473 }
1474 op_assign.type = SLANG_OPER_ASSIGN;
1475 op_assign.children =
1476 (slang_operation *) slang_alloc_malloc(2 * sizeof(slang_operation));
1477 if (op_assign.children == NULL) {
1478 slang_operation_destruct(&op_assign);
1479 op_id.locals->num_variables = 0;
1480 slang_operation_destruct(&op_id);
1481 return GL_FALSE;
1482 }
1483 op_assign.num_children = 2;
1484 op_assign.children[0] = op_id;
1485 op_assign.children[1] = *var->initializer;
1486
1487 result = 1;
1488
1489 /* carefully destroy the operations */
1490 op_assign.num_children = 0;
1491 slang_alloc_free(op_assign.children);
1492 op_assign.children = NULL;
1493 slang_operation_destruct(&op_assign);
1494 op_id.locals->num_variables = 0;
1495 slang_operation_destruct(&op_id);
1496
1497 if (!result)
1498 return GL_FALSE;
1499
1500 return GL_TRUE;
1501 }
1502
1503 /* init declarator list */
1504 #define DECLARATOR_NONE 0
1505 #define DECLARATOR_NEXT 1
1506
1507 /* variable declaration */
1508 #define VARIABLE_NONE 0
1509 #define VARIABLE_IDENTIFIER 1
1510 #define VARIABLE_INITIALIZER 2
1511 #define VARIABLE_ARRAY_EXPLICIT 3
1512 #define VARIABLE_ARRAY_UNKNOWN 4
1513
1514
1515 /**
1516 * Parse the initializer for a variable declaration.
1517 */
1518 static int
1519 parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
1520 const slang_fully_specified_type * type)
1521 {
1522 slang_variable *var;
1523
1524 /* empty init declatator (without name, e.g. "float ;") */
1525 if (*C->I++ == VARIABLE_NONE)
1526 return 1;
1527
1528 /* make room for the new variable and initialize it */
1529 var = slang_variable_scope_grow(O->vars);
1530 if (!var) {
1531 slang_info_log_memory(C->L);
1532 return 0;
1533 }
1534
1535 /* copy the declarator qualifier type, parse the identifier */
1536 var->type.qualifier = type->qualifier;
1537 var->a_name = parse_identifier(C);
1538 if (var->a_name == SLANG_ATOM_NULL)
1539 return 0;
1540
1541 switch (*C->I++) {
1542 case VARIABLE_NONE:
1543 /* simple variable declarator - just copy the specifier */
1544 if (!slang_type_specifier_copy(&var->type.specifier, &type->specifier))
1545 return 0;
1546 break;
1547 case VARIABLE_INITIALIZER:
1548 /* initialized variable - copy the specifier and parse the expression */
1549 if (!slang_type_specifier_copy(&var->type.specifier, &type->specifier))
1550 return 0;
1551 var->initializer =
1552 (slang_operation *) slang_alloc_malloc(sizeof(slang_operation));
1553 if (var->initializer == NULL) {
1554 slang_info_log_memory(C->L);
1555 return 0;
1556 }
1557 if (!slang_operation_construct(var->initializer)) {
1558 slang_alloc_free(var->initializer);
1559 var->initializer = NULL;
1560 slang_info_log_memory(C->L);
1561 return 0;
1562 }
1563 if (!parse_expression(C, O, var->initializer))
1564 return 0;
1565 break;
1566 case VARIABLE_ARRAY_UNKNOWN:
1567 /* unsized array - mark it as array and copy the specifier to
1568 the array element
1569 */
1570 if (!convert_to_array(C, var, &type->specifier))
1571 return GL_FALSE;
1572 break;
1573 case VARIABLE_ARRAY_EXPLICIT:
1574 if (!convert_to_array(C, var, &type->specifier))
1575 return GL_FALSE;
1576 if (!parse_array_len(C, O, &var->array_len))
1577 return GL_FALSE;
1578 break;
1579 default:
1580 return 0;
1581 }
1582
1583 /* emit code for global var decl */
1584 if (C->global_scope) {
1585 slang_assemble_ctx A;
1586 A.atoms = C->atoms;
1587 A.space.funcs = O->funs;
1588 A.space.structs = O->structs;
1589 A.space.vars = O->vars;
1590 A.program = O->program;
1591 A.vartable = O->vartable;
1592 _slang_codegen_global_variable(&A, var, C->type);
1593 }
1594
1595 /* allocate global address space for a variable with a known size */
1596 if (C->global_scope
1597 && !(var->type.specifier.type == SLANG_SPEC_ARRAY
1598 && var->array_len == 0)) {
1599 if (!calculate_var_size(C, O, var))
1600 return GL_FALSE;
1601 var->address = slang_var_pool_alloc(O->global_pool, var->size);
1602 }
1603
1604 /* initialize global variable */
1605 if (C->global_scope) {
1606 if (var->initializer != NULL) {
1607 slang_assemble_ctx A;
1608
1609 A.atoms = C->atoms;
1610 A.space.funcs = O->funs;
1611 A.space.structs = O->structs;
1612 A.space.vars = O->vars;
1613 if (!initialize_global(&A, var))
1614 return 0;
1615 }
1616 }
1617 return 1;
1618 }
1619
1620 /**
1621 * Parse a list of variable declarations. Each variable may have an
1622 * initializer.
1623 */
1624 static int
1625 parse_init_declarator_list(slang_parse_ctx * C, slang_output_ctx * O)
1626 {
1627 slang_fully_specified_type type;
1628
1629 /* parse the fully specified type, common to all declarators */
1630 if (!slang_fully_specified_type_construct(&type))
1631 return 0;
1632 if (!parse_fully_specified_type(C, O, &type)) {
1633 slang_fully_specified_type_destruct(&type);
1634 return 0;
1635 }
1636
1637 /* parse declarators, pass-in the parsed type */
1638 do {
1639 if (!parse_init_declarator(C, O, &type)) {
1640 slang_fully_specified_type_destruct(&type);
1641 return 0;
1642 }
1643 }
1644 while (*C->I++ == DECLARATOR_NEXT);
1645
1646 slang_fully_specified_type_destruct(&type);
1647 return 1;
1648 }
1649
1650
1651 /**
1652 * Parse a function definition or declaration.
1653 * \param C parsing context
1654 * \param O output context
1655 * \param definition if non-zero expect a definition, else a declaration
1656 * \param parsed_func_ret returns the parsed function
1657 * \return GL_TRUE if success, GL_FALSE if failure
1658 */
1659 static GLboolean
1660 parse_function(slang_parse_ctx * C, slang_output_ctx * O, int definition,
1661 slang_function ** parsed_func_ret)
1662 {
1663 slang_function parsed_func, *found_func;
1664
1665 /* parse function definition/declaration */
1666 if (!slang_function_construct(&parsed_func))
1667 return GL_FALSE;
1668 if (definition) {
1669 if (!parse_function_definition(C, O, &parsed_func)) {
1670 slang_function_destruct(&parsed_func);
1671 return GL_FALSE;
1672 }
1673 }
1674 else {
1675 if (!parse_function_prototype(C, O, &parsed_func)) {
1676 slang_function_destruct(&parsed_func);
1677 return GL_FALSE;
1678 }
1679 }
1680
1681 /* find a function with a prototype matching the parsed one - only
1682 * the current scope is being searched to allow built-in function
1683 * overriding
1684 */
1685 found_func = slang_function_scope_find(O->funs, &parsed_func, 0);
1686 if (found_func == NULL) {
1687 /* New function, add it to the function list */
1688 O->funs->functions =
1689 (slang_function *) slang_alloc_realloc(O->funs->functions,
1690 O->funs->num_functions *
1691 sizeof(slang_function),
1692 (O->funs->num_functions +
1693 1) * sizeof(slang_function));
1694 if (O->funs->functions == NULL) {
1695 slang_info_log_memory(C->L);
1696 slang_function_destruct(&parsed_func);
1697 return GL_FALSE;
1698 }
1699 O->funs->functions[O->funs->num_functions] = parsed_func;
1700 O->funs->num_functions++;
1701
1702 /* return the newly parsed function */
1703 *parsed_func_ret = &O->funs->functions[O->funs->num_functions - 1];
1704 }
1705 else {
1706 /* previously defined or declared */
1707 /* TODO: check function return type qualifiers and specifiers */
1708 if (definition) {
1709 if (found_func->body != NULL) {
1710 slang_info_log_error(C->L, "%s: function already has a body.",
1711 slang_atom_pool_id(C->atoms,
1712 parsed_func.header.
1713 a_name));
1714 slang_function_destruct(&parsed_func);
1715 return GL_FALSE;
1716 }
1717
1718 /* destroy the existing function declaration and replace it
1719 * with the new one, remember to save the fixup table
1720 */
1721 parsed_func.fixups = found_func->fixups;
1722 slang_fixup_table_init(&found_func->fixups);
1723 slang_function_destruct(found_func);
1724 *found_func = parsed_func;
1725 }
1726 else {
1727 /* another declaration of the same function prototype - ignore it */
1728 slang_function_destruct(&parsed_func);
1729 }
1730
1731 /* return the found function */
1732 *parsed_func_ret = found_func;
1733 }
1734
1735 /* assemble the parsed function */
1736 {
1737 slang_assemble_ctx A;
1738
1739 A.atoms = C->atoms;
1740 A.space.funcs = O->funs;
1741 A.space.structs = O->structs;
1742 A.space.vars = O->vars;
1743 A.program = O->program;
1744 A.vartable = O->vartable;
1745 A.log = C->L;
1746
1747 _slang_codegen_function(&A, *parsed_func_ret);
1748 }
1749 return GL_TRUE;
1750 }
1751
1752 /* declaration */
1753 #define DECLARATION_FUNCTION_PROTOTYPE 1
1754 #define DECLARATION_INIT_DECLARATOR_LIST 2
1755
1756 static int
1757 parse_declaration(slang_parse_ctx * C, slang_output_ctx * O)
1758 {
1759 switch (*C->I++) {
1760 case DECLARATION_INIT_DECLARATOR_LIST:
1761 if (!parse_init_declarator_list(C, O))
1762 return 0;
1763 break;
1764 case DECLARATION_FUNCTION_PROTOTYPE:
1765 {
1766 slang_function *dummy_func;
1767
1768 if (!parse_function(C, O, 0, &dummy_func))
1769 return 0;
1770 }
1771 break;
1772 default:
1773 return 0;
1774 }
1775 return 1;
1776 }
1777
1778 /* external declaration */
1779 #define EXTERNAL_NULL 0
1780 #define EXTERNAL_FUNCTION_DEFINITION 1
1781 #define EXTERNAL_DECLARATION 2
1782
1783 static GLboolean
1784 parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
1785 struct gl_program *program)
1786 {
1787 GET_CURRENT_CONTEXT(ctx);
1788 slang_output_ctx o;
1789 GLboolean success;
1790 GLuint maxRegs;
1791
1792 if (unit->type == SLANG_UNIT_FRAGMENT_BUILTIN ||
1793 unit->type == SLANG_UNIT_FRAGMENT_SHADER) {
1794 maxRegs = ctx->Const.FragmentProgram.MaxTemps;
1795 }
1796 else {
1797 assert(unit->type == SLANG_UNIT_VERTEX_BUILTIN ||
1798 unit->type == SLANG_UNIT_VERTEX_SHADER);
1799 maxRegs = ctx->Const.VertexProgram.MaxTemps;
1800 }
1801
1802 /* setup output context */
1803 o.funs = &unit->funs;
1804 o.structs = &unit->structs;
1805 o.vars = &unit->vars;
1806 o.global_pool = &unit->object->varpool;
1807 o.program = program;
1808 o.vartable = _slang_new_var_table(maxRegs);
1809 _slang_push_var_table(o.vartable);
1810
1811 /* parse individual functions and declarations */
1812 while (*C->I != EXTERNAL_NULL) {
1813 switch (*C->I++) {
1814 case EXTERNAL_FUNCTION_DEFINITION:
1815 {
1816 slang_function *func;
1817 success = parse_function(C, &o, 1, &func);
1818 }
1819 break;
1820 case EXTERNAL_DECLARATION:
1821 success = parse_declaration(C, &o);
1822 break;
1823 default:
1824 success = GL_FALSE;
1825 }
1826
1827 if (!success) {
1828 /* xxx free codegen */
1829 _slang_pop_var_table(o.vartable);
1830 return GL_FALSE;
1831 }
1832 }
1833 C->I++;
1834
1835 _slang_pop_var_table(o.vartable);
1836 return GL_TRUE;
1837 }
1838
1839 static GLboolean
1840 compile_binary(const byte * prod, slang_code_unit * unit,
1841 slang_unit_type type, slang_info_log * infolog,
1842 slang_code_unit * builtin, slang_code_unit * downlink,
1843 struct gl_program *program)
1844 {
1845 slang_parse_ctx C;
1846
1847 unit->type = type;
1848
1849 /* setup parse context */
1850 C.I = prod;
1851 C.L = infolog;
1852 C.parsing_builtin = (builtin == NULL);
1853 C.global_scope = GL_TRUE;
1854 C.atoms = &unit->object->atompool;
1855 C.type = type;
1856
1857 if (!check_revision(&C))
1858 return GL_FALSE;
1859
1860 if (downlink != NULL) {
1861 unit->vars.outer_scope = &downlink->vars;
1862 unit->funs.outer_scope = &downlink->funs;
1863 unit->structs.outer_scope = &downlink->structs;
1864 }
1865
1866 /* parse translation unit */
1867 return parse_code_unit(&C, unit, program);
1868 }
1869
1870 static GLboolean
1871 compile_with_grammar(grammar id, const char *source, slang_code_unit * unit,
1872 slang_unit_type type, slang_info_log * infolog,
1873 slang_code_unit * builtin,
1874 struct gl_program *program)
1875 {
1876 byte *prod;
1877 GLuint size, start, version;
1878 slang_string preprocessed;
1879
1880 /* First retrieve the version number. */
1881 if (!_slang_preprocess_version(source, &version, &start, infolog))
1882 return GL_FALSE;
1883
1884 if (version > 110) {
1885 slang_info_log_error(infolog,
1886 "language version specified is not supported.");
1887 return GL_FALSE;
1888 }
1889
1890 /* Now preprocess the source string. */
1891 slang_string_init(&preprocessed);
1892 if (!_slang_preprocess_directives(&preprocessed, &source[start], infolog)) {
1893 slang_string_free(&preprocessed);
1894 slang_info_log_error(infolog, "failed to preprocess the source.");
1895 return GL_FALSE;
1896 }
1897
1898 /* Finally check the syntax and generate its binary representation. */
1899 if (!grammar_fast_check(id,
1900 (const byte *) (slang_string_cstr(&preprocessed)),
1901 &prod, &size, 65536)) {
1902 char buf[1024];
1903 GLint pos;
1904
1905 slang_string_free(&preprocessed);
1906 grammar_get_last_error((byte *) (buf), sizeof(buf), &pos);
1907 slang_info_log_error(infolog, buf);
1908 /* syntax error (possibly in library code) */
1909 #if 0
1910 {
1911 int line, col;
1912 char *s;
1913 s = (char *) _mesa_find_line_column((const GLubyte *) source,
1914 (const GLubyte *) source + pos,
1915 &line, &col);
1916 printf("Error on line %d, col %d: %s\n", line, col, s);
1917 }
1918 #endif
1919 return GL_FALSE;
1920 }
1921 slang_string_free(&preprocessed);
1922
1923 /* Syntax is okay - translate it to internal representation. */
1924 if (!compile_binary(prod, unit, type, infolog, builtin,
1925 &builtin[SLANG_BUILTIN_TOTAL - 1],
1926 program)) {
1927 grammar_alloc_free(prod);
1928 return GL_FALSE;
1929 }
1930 grammar_alloc_free(prod);
1931 return GL_TRUE;
1932 }
1933
1934 LONGSTRING static const char *slang_shader_syn =
1935 #include "library/slang_shader_syn.h"
1936 ;
1937
1938 static const byte slang_core_gc[] = {
1939 #include "library/slang_core_gc.h"
1940 };
1941
1942 static const byte slang_common_builtin_gc[] = {
1943 #include "library/slang_common_builtin_gc.h"
1944 };
1945
1946 static const byte slang_fragment_builtin_gc[] = {
1947 #include "library/slang_fragment_builtin_gc.h"
1948 };
1949
1950 static const byte slang_vertex_builtin_gc[] = {
1951 #include "library/slang_vertex_builtin_gc.h"
1952 };
1953
1954 static GLboolean
1955 compile_object(grammar * id, const char *source, slang_code_object * object,
1956 slang_unit_type type, slang_info_log * infolog,
1957 struct gl_program *program)
1958 {
1959 slang_code_unit *builtins = NULL;
1960
1961 /* load GLSL grammar */
1962 *id = grammar_load_from_text((const byte *) (slang_shader_syn));
1963 if (*id == 0) {
1964 byte buf[1024];
1965 int pos;
1966
1967 grammar_get_last_error(buf, 1024, &pos);
1968 slang_info_log_error(infolog, (const char *) (buf));
1969 return GL_FALSE;
1970 }
1971
1972 /* set shader type - the syntax is slightly different for different shaders */
1973 if (type == SLANG_UNIT_FRAGMENT_SHADER
1974 || type == SLANG_UNIT_FRAGMENT_BUILTIN)
1975 grammar_set_reg8(*id, (const byte *) "shader_type", 1);
1976 else
1977 grammar_set_reg8(*id, (const byte *) "shader_type", 2);
1978
1979 /* enable language extensions */
1980 grammar_set_reg8(*id, (const byte *) "parsing_builtin", 1);
1981
1982 /* if parsing user-specified shader, load built-in library */
1983 if (type == SLANG_UNIT_FRAGMENT_SHADER || type == SLANG_UNIT_VERTEX_SHADER) {
1984 /* compile core functionality first */
1985 if (!compile_binary(slang_core_gc,
1986 &object->builtin[SLANG_BUILTIN_CORE],
1987 SLANG_UNIT_FRAGMENT_BUILTIN, infolog,
1988 NULL, NULL, NULL))
1989 return GL_FALSE;
1990
1991 /* compile common functions and variables, link to core */
1992 if (!compile_binary(slang_common_builtin_gc,
1993 &object->builtin[SLANG_BUILTIN_COMMON],
1994 SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
1995 &object->builtin[SLANG_BUILTIN_CORE], NULL))
1996 return GL_FALSE;
1997
1998 /* compile target-specific functions and variables, link to common */
1999 if (type == SLANG_UNIT_FRAGMENT_SHADER) {
2000 if (!compile_binary(slang_fragment_builtin_gc,
2001 &object->builtin[SLANG_BUILTIN_TARGET],
2002 SLANG_UNIT_FRAGMENT_BUILTIN, infolog, NULL,
2003 &object->builtin[SLANG_BUILTIN_COMMON], NULL))
2004 return GL_FALSE;
2005 }
2006 else if (type == SLANG_UNIT_VERTEX_SHADER) {
2007 if (!compile_binary(slang_vertex_builtin_gc,
2008 &object->builtin[SLANG_BUILTIN_TARGET],
2009 SLANG_UNIT_VERTEX_BUILTIN, infolog, NULL,
2010 &object->builtin[SLANG_BUILTIN_COMMON], NULL))
2011 return GL_FALSE;
2012 }
2013
2014 /* disable language extensions */
2015 #if NEW_SLANG /* allow-built-ins */
2016 grammar_set_reg8(*id, (const byte *) "parsing_builtin", 1);
2017 #else
2018 grammar_set_reg8(*id, (const byte *) "parsing_builtin", 0);
2019 #endif
2020 builtins = object->builtin;
2021 }
2022
2023 /* compile the actual shader - pass-in built-in library for external shader */
2024 return compile_with_grammar(*id, source, &object->unit, type, infolog,
2025 builtins, program);
2026 }
2027
2028
2029 static GLboolean
2030 compile_shader(GLcontext *ctx, slang_code_object * object,
2031 slang_unit_type type, slang_info_log * infolog,
2032 struct gl_shader *shader)
2033 {
2034 struct gl_program *program = shader->Programs[0];
2035 GLboolean success;
2036 grammar id = 0;
2037
2038 assert(program);
2039
2040 _slang_code_object_dtr(object);
2041 _slang_code_object_ctr(object);
2042
2043 success = compile_object(&id, shader->Source, object, type, infolog, program);
2044 if (id != 0)
2045 grammar_destroy(id);
2046 if (!success)
2047 return GL_FALSE;
2048
2049 return GL_TRUE;
2050 }
2051
2052
2053
2054 GLboolean
2055 _slang_compile(GLcontext *ctx, struct gl_shader *shader)
2056 {
2057 GLboolean success;
2058 slang_info_log info_log;
2059 slang_code_object obj;
2060 slang_unit_type type;
2061
2062 if (shader->Type == GL_VERTEX_SHADER) {
2063 type = SLANG_UNIT_VERTEX_SHADER;
2064 }
2065 else {
2066 assert(shader->Type == GL_FRAGMENT_SHADER);
2067 type = SLANG_UNIT_FRAGMENT_SHADER;
2068 }
2069
2070 /* XXX temporary hack */
2071 if (!shader->Programs) {
2072 GLenum progTarget;
2073 if (shader->Type == GL_VERTEX_SHADER)
2074 progTarget = GL_VERTEX_PROGRAM_ARB;
2075 else
2076 progTarget = GL_FRAGMENT_PROGRAM_ARB;
2077 shader->Programs
2078 = (struct gl_program **) malloc(sizeof(struct gl_program*));
2079 shader->Programs[0] = _mesa_new_program(ctx, progTarget, 1);
2080 shader->NumPrograms = 1;
2081
2082 shader->Programs[0]->Parameters = _mesa_new_parameter_list();
2083 shader->Programs[0]->Varying = _mesa_new_parameter_list();
2084 shader->Programs[0]->Attributes = _mesa_new_parameter_list();
2085 }
2086
2087 slang_info_log_construct(&info_log);
2088 _slang_code_object_ctr(&obj);
2089
2090 success = compile_shader(ctx, &obj, type, &info_log, shader);
2091
2092 /* free shader's prev info log */
2093 if (shader->InfoLog) {
2094 _mesa_free(shader->InfoLog);
2095 shader->InfoLog = NULL;
2096 }
2097
2098 if (info_log.text) {
2099 /* copy info-log string to shader object */
2100 shader->InfoLog = _mesa_strdup(info_log.text);
2101 }
2102
2103 if (info_log.error_flag) {
2104 success = GL_FALSE;
2105 }
2106
2107 slang_info_log_destruct(&info_log);
2108 _slang_code_object_dtr(&obj);
2109
2110 return success;
2111 }
2112