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