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