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