da77de709c31abd4b76fa531be00ca2d1667499c
[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, "undeclared type name '%s'",
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 if (!parse_type_specifier(C, O, &type->specifier))
718 return 0;
719 return 1;
720 }
721
722 /* operation */
723 #define OP_END 0
724 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
725 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
726 #define OP_DECLARE 3
727 #define OP_ASM 4
728 #define OP_BREAK 5
729 #define OP_CONTINUE 6
730 #define OP_DISCARD 7
731 #define OP_RETURN 8
732 #define OP_EXPRESSION 9
733 #define OP_IF 10
734 #define OP_WHILE 11
735 #define OP_DO 12
736 #define OP_FOR 13
737 #define OP_PUSH_VOID 14
738 #define OP_PUSH_BOOL 15
739 #define OP_PUSH_INT 16
740 #define OP_PUSH_FLOAT 17
741 #define OP_PUSH_IDENTIFIER 18
742 #define OP_SEQUENCE 19
743 #define OP_ASSIGN 20
744 #define OP_ADDASSIGN 21
745 #define OP_SUBASSIGN 22
746 #define OP_MULASSIGN 23
747 #define OP_DIVASSIGN 24
748 /*#define OP_MODASSIGN 25*/
749 /*#define OP_LSHASSIGN 26*/
750 /*#define OP_RSHASSIGN 27*/
751 /*#define OP_ORASSIGN 28*/
752 /*#define OP_XORASSIGN 29*/
753 /*#define OP_ANDASSIGN 30*/
754 #define OP_SELECT 31
755 #define OP_LOGICALOR 32
756 #define OP_LOGICALXOR 33
757 #define OP_LOGICALAND 34
758 /*#define OP_BITOR 35*/
759 /*#define OP_BITXOR 36*/
760 /*#define OP_BITAND 37*/
761 #define OP_EQUAL 38
762 #define OP_NOTEQUAL 39
763 #define OP_LESS 40
764 #define OP_GREATER 41
765 #define OP_LESSEQUAL 42
766 #define OP_GREATEREQUAL 43
767 /*#define OP_LSHIFT 44*/
768 /*#define OP_RSHIFT 45*/
769 #define OP_ADD 46
770 #define OP_SUBTRACT 47
771 #define OP_MULTIPLY 48
772 #define OP_DIVIDE 49
773 /*#define OP_MODULUS 50*/
774 #define OP_PREINCREMENT 51
775 #define OP_PREDECREMENT 52
776 #define OP_PLUS 53
777 #define OP_MINUS 54
778 /*#define OP_COMPLEMENT 55*/
779 #define OP_NOT 56
780 #define OP_SUBSCRIPT 57
781 #define OP_CALL 58
782 #define OP_FIELD 59
783 #define OP_POSTINCREMENT 60
784 #define OP_POSTDECREMENT 61
785
786
787 /**
788 * When parsing a compound production, this function is used to parse the
789 * children.
790 * For example, a a while-loop compound will have two children, the
791 * while condition expression and the loop body. So, this function will
792 * be called twice to parse those two sub-expressions.
793 * \param C the parsing context
794 * \param O the output context
795 * \param oper the operation we're parsing
796 * \param statement indicates whether parsing a statement, or expression
797 * \return 1 if success, 0 if error
798 */
799 static int
800 parse_child_operation(slang_parse_ctx * C, slang_output_ctx * O,
801 slang_operation * oper, GLboolean statement)
802 {
803 slang_operation *ch;
804
805 /* grow child array */
806 #if 000
807 oper->children = (slang_operation *)
808 slang_alloc_realloc(oper->children,
809 oper->num_children * sizeof(slang_operation),
810 (oper->num_children + 1) * sizeof(slang_operation));
811 if (oper->children == NULL) {
812 slang_info_log_memory(C->L);
813 return 0;
814 }
815
816 ch = &oper->children[oper->num_children];
817 if (!slang_operation_construct(ch)) {
818 slang_info_log_memory(C->L);
819 return 0;
820 }
821 oper->num_children++;
822 #else
823 ch = slang_operation_grow(&oper->num_children, &oper->children);
824 #endif
825 if (statement)
826 return parse_statement(C, O, ch);
827 return parse_expression(C, O, ch);
828 }
829
830 static int parse_declaration(slang_parse_ctx * C, slang_output_ctx * O);
831
832 static int
833 parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
834 slang_operation * oper)
835 {
836 oper->locals->outer_scope = O->vars;
837 switch (*C->I++) {
838 case OP_BLOCK_BEGIN_NO_NEW_SCOPE:
839 /* parse child statements, do not create new variable scope */
840 oper->type = slang_oper_block_no_new_scope;
841 while (*C->I != OP_END)
842 if (!parse_child_operation(C, O, oper, 1))
843 return 0;
844 C->I++;
845 break;
846 case OP_BLOCK_BEGIN_NEW_SCOPE:
847 /* parse child statements, create new variable scope */
848 {
849 slang_output_ctx o = *O;
850
851 oper->type = slang_oper_block_new_scope;
852 o.vars = oper->locals;
853 while (*C->I != OP_END)
854 if (!parse_child_operation(C, &o, oper, 1))
855 return 0;
856 C->I++;
857 }
858 break;
859 case OP_DECLARE:
860 /* local variable declaration, individual declarators are stored as
861 * children identifiers
862 */
863 #if 000
864 oper->type = slang_oper_variable_decl;
865 {
866 const unsigned int first_var = O->vars->num_variables;
867
868 /* parse the declaration, note that there can be zero or more
869 * than one declarators
870 */
871 if (!parse_declaration(C, O))
872 return 0;
873 if (first_var < O->vars->num_variables) {
874 const unsigned int num_vars = O->vars->num_variables - first_var;
875 unsigned int i;
876
877 oper->children = (slang_operation *)
878 slang_alloc_malloc(num_vars * sizeof(slang_operation));
879 if (oper->children == NULL) {
880 slang_info_log_memory(C->L);
881 return 0;
882 }
883 for (oper->num_children = 0; oper->num_children < num_vars;
884 oper->num_children++) {
885 if (!slang_operation_construct
886 (&oper->children[oper->num_children])) {
887 slang_info_log_memory(C->L);
888 return 0;
889 }
890 }
891 for (i = first_var; i < O->vars->num_variables; i++) {
892 slang_operation *o = &oper->children[i - first_var];
893 o->type = slang_oper_identifier;
894 o->locals->outer_scope = O->vars;
895 o->a_id = O->vars->variables[i].a_name;
896 }
897 }
898 }
899 #else
900
901 oper->type = slang_oper_block_no_new_scope;
902 {
903 const unsigned int first_var = O->vars->num_variables;
904
905 /* parse the declaration, note that there can be zero or more
906 * than one declarators
907 */
908 if (!parse_declaration(C, O))
909 return 0;
910 if (first_var < O->vars->num_variables) {
911 const unsigned int num_vars = O->vars->num_variables - first_var;
912 unsigned int i;
913
914 oper->num_children = num_vars;
915 oper->children = slang_operation_new(num_vars);
916 if (oper->children == NULL) {
917 slang_info_log_memory(C->L);
918 return 0;
919 }
920 for (i = first_var; i < O->vars->num_variables; i++) {
921 slang_operation *o = &oper->children[i - first_var];
922 o->type = slang_oper_variable_decl;
923 o->locals->outer_scope = O->vars;
924 o->a_id = O->vars->variables[i].a_name;
925 }
926 }
927 }
928
929
930 #endif
931 break;
932 case OP_ASM:
933 /* the __asm statement, parse the mnemonic and all its arguments
934 * as expressions
935 */
936 oper->type = slang_oper_asm;
937 oper->a_id = parse_identifier(C);
938 if (strcmp((char*)oper->a_id, "dot") == 0) {
939 printf("Assemble dot! **************************\n");
940 }
941 if (oper->a_id == SLANG_ATOM_NULL)
942 return 0;
943 while (*C->I != OP_END) {
944 if (!parse_child_operation(C, O, oper, 0))
945 return 0;
946 }
947 C->I++;
948 break;
949 case OP_BREAK:
950 oper->type = slang_oper_break;
951 break;
952 case OP_CONTINUE:
953 oper->type = slang_oper_continue;
954 break;
955 case OP_DISCARD:
956 oper->type = slang_oper_discard;
957 break;
958 case OP_RETURN:
959 oper->type = slang_oper_return;
960 if (!parse_child_operation(C, O, oper, 0))
961 return 0;
962 break;
963 case OP_EXPRESSION:
964 oper->type = slang_oper_expression;
965 if (!parse_child_operation(C, O, oper, 0))
966 return 0;
967 break;
968 case OP_IF:
969 oper->type = slang_oper_if;
970 if (!parse_child_operation(C, O, oper, 0))
971 return 0;
972 if (!parse_child_operation(C, O, oper, 1))
973 return 0;
974 if (!parse_child_operation(C, O, oper, 1))
975 return 0;
976 break;
977 case OP_WHILE:
978 {
979 slang_output_ctx o = *O;
980
981 oper->type = slang_oper_while;
982 o.vars = oper->locals;
983 if (!parse_child_operation(C, &o, oper, 1))
984 return 0;
985 if (!parse_child_operation(C, &o, oper, 1))
986 return 0;
987 }
988 break;
989 case OP_DO:
990 oper->type = slang_oper_do;
991 if (!parse_child_operation(C, O, oper, 1))
992 return 0;
993 if (!parse_child_operation(C, O, oper, 0))
994 return 0;
995 break;
996 case OP_FOR:
997 {
998 slang_output_ctx o = *O;
999
1000 oper->type = slang_oper_for;
1001 o.vars = oper->locals;
1002 if (!parse_child_operation(C, &o, oper, 1))
1003 return 0;
1004 if (!parse_child_operation(C, &o, oper, 1))
1005 return 0;
1006 if (!parse_child_operation(C, &o, oper, 0))
1007 return 0;
1008 if (!parse_child_operation(C, &o, oper, 1))
1009 return 0;
1010 }
1011 break;
1012 default:
1013 return 0;
1014 }
1015 return 1;
1016 }
1017
1018 static int
1019 handle_nary_expression(slang_parse_ctx * C, slang_operation * op,
1020 slang_operation ** ops, unsigned int *total_ops,
1021 unsigned int n)
1022 {
1023 unsigned int i;
1024
1025 op->children =
1026 (slang_operation *) slang_alloc_malloc(n * sizeof(slang_operation));
1027 if (op->children == NULL) {
1028 slang_info_log_memory(C->L);
1029 return 0;
1030 }
1031 op->num_children = n;
1032
1033 for (i = 0; i < n; i++)
1034 op->children[i] = (*ops)[*total_ops - (n + 1 - i)];
1035 (*ops)[*total_ops - (n + 1)] = (*ops)[*total_ops - 1];
1036 *total_ops -= n;
1037
1038 *ops = (slang_operation *)
1039 slang_alloc_realloc(*ops,
1040 (*total_ops + n) * sizeof(slang_operation),
1041 *total_ops * sizeof(slang_operation));
1042 if (*ops == NULL) {
1043 slang_info_log_memory(C->L);
1044 return 0;
1045 }
1046 return 1;
1047 }
1048
1049 static int
1050 is_constructor_name(const char *name, slang_atom a_name,
1051 slang_struct_scope * structs)
1052 {
1053 if (slang_type_specifier_type_from_string(name) != slang_spec_void)
1054 return 1;
1055 return slang_struct_scope_find(structs, a_name, 1) != NULL;
1056 }
1057
1058 static int
1059 parse_expression(slang_parse_ctx * C, slang_output_ctx * O,
1060 slang_operation * oper)
1061 {
1062 slang_operation *ops = NULL;
1063 unsigned int num_ops = 0;
1064 int number;
1065
1066 while (*C->I != OP_END) {
1067 slang_operation *op;
1068 const unsigned int op_code = *C->I++;
1069
1070 /* allocate default operation, becomes a no-op if not used */
1071 ops = (slang_operation *)
1072 slang_alloc_realloc(ops,
1073 num_ops * sizeof(slang_operation),
1074 (num_ops + 1) * sizeof(slang_operation));
1075 if (ops == NULL) {
1076 slang_info_log_memory(C->L);
1077 return 0;
1078 }
1079 op = &ops[num_ops];
1080 if (!slang_operation_construct(op)) {
1081 slang_info_log_memory(C->L);
1082 return 0;
1083 }
1084 num_ops++;
1085 op->locals->outer_scope = O->vars;
1086
1087 switch (op_code) {
1088 case OP_PUSH_VOID:
1089 op->type = slang_oper_void;
1090 break;
1091 case OP_PUSH_BOOL:
1092 op->type = slang_oper_literal_bool;
1093 if (!parse_number(C, &number))
1094 return 0;
1095 op->literal[0] =
1096 op->literal[1] =
1097 op->literal[2] =
1098 op->literal[3] = (GLfloat) number;
1099 break;
1100 case OP_PUSH_INT:
1101 op->type = slang_oper_literal_int;
1102 if (!parse_number(C, &number))
1103 return 0;
1104 op->literal[0] =
1105 op->literal[1] =
1106 op->literal[2] =
1107 op->literal[3] = (GLfloat) number;
1108 break;
1109 case OP_PUSH_FLOAT:
1110 op->type = slang_oper_literal_float;
1111 if (!parse_float(C, &op->literal[0]))
1112 return 0;
1113 op->literal[1] =
1114 op->literal[2] =
1115 op->literal[3] = op->literal[0];
1116 break;
1117 case OP_PUSH_IDENTIFIER:
1118 op->type = slang_oper_identifier;
1119 op->a_id = parse_identifier(C);
1120 if (op->a_id == SLANG_ATOM_NULL)
1121 return 0;
1122 break;
1123 case OP_SEQUENCE:
1124 op->type = slang_oper_sequence;
1125 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1126 return 0;
1127 break;
1128 case OP_ASSIGN:
1129 op->type = slang_oper_assign;
1130 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1131 return 0;
1132 break;
1133 case OP_ADDASSIGN:
1134 op->type = slang_oper_addassign;
1135 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1136 return 0;
1137 break;
1138 case OP_SUBASSIGN:
1139 op->type = slang_oper_subassign;
1140 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1141 return 0;
1142 break;
1143 case OP_MULASSIGN:
1144 op->type = slang_oper_mulassign;
1145 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1146 return 0;
1147 break;
1148 case OP_DIVASSIGN:
1149 op->type = slang_oper_divassign;
1150 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1151 return 0;
1152 break;
1153 /*case OP_MODASSIGN: */
1154 /*case OP_LSHASSIGN: */
1155 /*case OP_RSHASSIGN: */
1156 /*case OP_ORASSIGN: */
1157 /*case OP_XORASSIGN: */
1158 /*case OP_ANDASSIGN: */
1159 case OP_SELECT:
1160 op->type = slang_oper_select;
1161 if (!handle_nary_expression(C, op, &ops, &num_ops, 3))
1162 return 0;
1163 break;
1164 case OP_LOGICALOR:
1165 op->type = slang_oper_logicalor;
1166 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1167 return 0;
1168 break;
1169 case OP_LOGICALXOR:
1170 op->type = slang_oper_logicalxor;
1171 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1172 return 0;
1173 break;
1174 case OP_LOGICALAND:
1175 op->type = slang_oper_logicaland;
1176 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1177 return 0;
1178 break;
1179 /*case OP_BITOR: */
1180 /*case OP_BITXOR: */
1181 /*case OP_BITAND: */
1182 case OP_EQUAL:
1183 op->type = slang_oper_equal;
1184 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1185 return 0;
1186 break;
1187 case OP_NOTEQUAL:
1188 op->type = slang_oper_notequal;
1189 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1190 return 0;
1191 break;
1192 case OP_LESS:
1193 op->type = slang_oper_less;
1194 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1195 return 0;
1196 break;
1197 case OP_GREATER:
1198 op->type = slang_oper_greater;
1199 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1200 return 0;
1201 break;
1202 case OP_LESSEQUAL:
1203 op->type = slang_oper_lessequal;
1204 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1205 return 0;
1206 break;
1207 case OP_GREATEREQUAL:
1208 op->type = slang_oper_greaterequal;
1209 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1210 return 0;
1211 break;
1212 /*case OP_LSHIFT: */
1213 /*case OP_RSHIFT: */
1214 case OP_ADD:
1215 op->type = slang_oper_add;
1216 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1217 return 0;
1218 break;
1219 case OP_SUBTRACT:
1220 op->type = slang_oper_subtract;
1221 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1222 return 0;
1223 break;
1224 case OP_MULTIPLY:
1225 op->type = slang_oper_multiply;
1226 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1227 return 0;
1228 break;
1229 case OP_DIVIDE:
1230 op->type = slang_oper_divide;
1231 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1232 return 0;
1233 break;
1234 /*case OP_MODULUS: */
1235 case OP_PREINCREMENT:
1236 op->type = slang_oper_preincrement;
1237 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1238 return 0;
1239 break;
1240 case OP_PREDECREMENT:
1241 op->type = slang_oper_predecrement;
1242 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1243 return 0;
1244 break;
1245 case OP_PLUS:
1246 op->type = slang_oper_plus;
1247 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1248 return 0;
1249 break;
1250 case OP_MINUS:
1251 op->type = slang_oper_minus;
1252 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1253 return 0;
1254 break;
1255 case OP_NOT:
1256 op->type = slang_oper_not;
1257 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1258 return 0;
1259 break;
1260 /*case OP_COMPLEMENT: */
1261 case OP_SUBSCRIPT:
1262 op->type = slang_oper_subscript;
1263 if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
1264 return 0;
1265 break;
1266 case OP_CALL:
1267 op->type = slang_oper_call;
1268 op->a_id = parse_identifier(C);
1269 if (op->a_id == SLANG_ATOM_NULL)
1270 return 0;
1271 while (*C->I != OP_END)
1272 if (!parse_child_operation(C, O, op, 0))
1273 return 0;
1274 C->I++;
1275
1276 if (!C->parsing_builtin
1277 && !slang_function_scope_find_by_name(O->funs, op->a_id, 1)) {
1278 const char *id;
1279
1280 id = slang_atom_pool_id(C->atoms, op->a_id);
1281 if (!is_constructor_name(id, op->a_id, O->structs)) {
1282 slang_info_log_error(C->L, "%s: undeclared function name.", id);
1283 return 0;
1284 }
1285 }
1286 break;
1287 case OP_FIELD:
1288 op->type = slang_oper_field;
1289 op->a_id = parse_identifier(C);
1290 if (op->a_id == SLANG_ATOM_NULL)
1291 return 0;
1292 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1293 return 0;
1294 break;
1295 case OP_POSTINCREMENT:
1296 op->type = slang_oper_postincrement;
1297 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1298 return 0;
1299 break;
1300 case OP_POSTDECREMENT:
1301 op->type = slang_oper_postdecrement;
1302 if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
1303 return 0;
1304 break;
1305 default:
1306 return 0;
1307 }
1308 }
1309 C->I++;
1310
1311 *oper = *ops;
1312 slang_alloc_free(ops);
1313
1314 return 1;
1315 }
1316
1317 /* parameter qualifier */
1318 #define PARAM_QUALIFIER_IN 0
1319 #define PARAM_QUALIFIER_OUT 1
1320 #define PARAM_QUALIFIER_INOUT 2
1321
1322 /* function parameter array presence */
1323 #define PARAMETER_ARRAY_NOT_PRESENT 0
1324 #define PARAMETER_ARRAY_PRESENT 1
1325
1326 static int
1327 parse_parameter_declaration(slang_parse_ctx * C, slang_output_ctx * O,
1328 slang_variable * param)
1329 {
1330 /* parse and validate the parameter's type qualifiers (there can be
1331 * two at most) because not all combinations are valid
1332 */
1333 if (!parse_type_qualifier(C, &param->type.qualifier))
1334 return 0;
1335 switch (*C->I++) {
1336 case PARAM_QUALIFIER_IN:
1337 if (param->type.qualifier != slang_qual_const
1338 && param->type.qualifier != slang_qual_none) {
1339 slang_info_log_error(C->L, "Invalid type qualifier.");
1340 return 0;
1341 }
1342 break;
1343 case PARAM_QUALIFIER_OUT:
1344 if (param->type.qualifier == slang_qual_none)
1345 param->type.qualifier = slang_qual_out;
1346 else {
1347 slang_info_log_error(C->L, "Invalid type qualifier.");
1348 return 0;
1349 }
1350 break;
1351 case PARAM_QUALIFIER_INOUT:
1352 if (param->type.qualifier == slang_qual_none)
1353 param->type.qualifier = slang_qual_inout;
1354 else {
1355 slang_info_log_error(C->L, "Invalid type qualifier.");
1356 return 0;
1357 }
1358 break;
1359 default:
1360 return 0;
1361 }
1362
1363 /* parse parameter's type specifier and name */
1364 if (!parse_type_specifier(C, O, &param->type.specifier))
1365 return 0;
1366 param->a_name = parse_identifier(C);
1367 if (param->a_name == SLANG_ATOM_NULL)
1368 return 0;
1369
1370 /* if the parameter is an array, parse its size (the size must be
1371 * explicitly defined
1372 */
1373 if (*C->I++ == PARAMETER_ARRAY_PRESENT) {
1374 slang_type_specifier p;
1375
1376 slang_type_specifier_ctr(&p);
1377 if (!slang_type_specifier_copy(&p, &param->type.specifier)) {
1378 slang_type_specifier_dtr(&p);
1379 return GL_FALSE;
1380 }
1381 if (!convert_to_array(C, param, &p)) {
1382 slang_type_specifier_dtr(&p);
1383 return GL_FALSE;
1384 }
1385 slang_type_specifier_dtr(&p);
1386 if (!parse_array_len(C, O, &param->array_len))
1387 return GL_FALSE;
1388 }
1389
1390 /* calculate the parameter size */
1391 if (!calculate_var_size(C, O, param))
1392 return GL_FALSE;
1393
1394 /* TODO: allocate the local address here? */
1395 return 1;
1396 }
1397
1398 /* function type */
1399 #define FUNCTION_ORDINARY 0
1400 #define FUNCTION_CONSTRUCTOR 1
1401 #define FUNCTION_OPERATOR 2
1402
1403 /* function parameter */
1404 #define PARAMETER_NONE 0
1405 #define PARAMETER_NEXT 1
1406
1407 /* operator type */
1408 #define OPERATOR_ADDASSIGN 1
1409 #define OPERATOR_SUBASSIGN 2
1410 #define OPERATOR_MULASSIGN 3
1411 #define OPERATOR_DIVASSIGN 4
1412 /*#define OPERATOR_MODASSIGN 5*/
1413 /*#define OPERATOR_LSHASSIGN 6*/
1414 /*#define OPERATOR_RSHASSIGN 7*/
1415 /*#define OPERATOR_ANDASSIGN 8*/
1416 /*#define OPERATOR_XORASSIGN 9*/
1417 /*#define OPERATOR_ORASSIGN 10*/
1418 #define OPERATOR_LOGICALXOR 11
1419 /*#define OPERATOR_BITOR 12*/
1420 /*#define OPERATOR_BITXOR 13*/
1421 /*#define OPERATOR_BITAND 14*/
1422 #define OPERATOR_LESS 15
1423 #define OPERATOR_GREATER 16
1424 #define OPERATOR_LESSEQUAL 17
1425 #define OPERATOR_GREATEREQUAL 18
1426 /*#define OPERATOR_LSHIFT 19*/
1427 /*#define OPERATOR_RSHIFT 20*/
1428 #define OPERATOR_MULTIPLY 21
1429 #define OPERATOR_DIVIDE 22
1430 /*#define OPERATOR_MODULUS 23*/
1431 #define OPERATOR_INCREMENT 24
1432 #define OPERATOR_DECREMENT 25
1433 #define OPERATOR_PLUS 26
1434 #define OPERATOR_MINUS 27
1435 /*#define OPERATOR_COMPLEMENT 28*/
1436 #define OPERATOR_NOT 29
1437
1438 static const struct
1439 {
1440 unsigned int o_code;
1441 const char *o_name;
1442 } operator_names[] = {
1443 {OPERATOR_INCREMENT, "++"},
1444 {OPERATOR_ADDASSIGN, "+="},
1445 {OPERATOR_PLUS, "+"},
1446 {OPERATOR_DECREMENT, "--"},
1447 {OPERATOR_SUBASSIGN, "-="},
1448 {OPERATOR_MINUS, "-"},
1449 {OPERATOR_NOT, "!"},
1450 {OPERATOR_MULASSIGN, "*="},
1451 {OPERATOR_MULTIPLY, "*"},
1452 {OPERATOR_DIVASSIGN, "/="},
1453 {OPERATOR_DIVIDE, "/"},
1454 {OPERATOR_LESSEQUAL, "<="},
1455 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1456 /*{ OPERATOR_LSHIFT, "<<" }, */
1457 {OPERATOR_LESS, "<"},
1458 {OPERATOR_GREATEREQUAL, ">="},
1459 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1460 /*{ OPERATOR_RSHIFT, ">>" }, */
1461 {OPERATOR_GREATER, ">"},
1462 /*{ OPERATOR_MODASSIGN, "%=" }, */
1463 /*{ OPERATOR_MODULUS, "%" }, */
1464 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1465 /*{ OPERATOR_BITAND, "&" }, */
1466 /*{ OPERATOR_ORASSIGN, "|=" }, */
1467 /*{ OPERATOR_BITOR, "|" }, */
1468 /*{ OPERATOR_COMPLEMENT, "~" }, */
1469 /*{ OPERATOR_XORASSIGN, "^=" }, */
1470 {OPERATOR_LOGICALXOR, "^^"},
1471 /*{ OPERATOR_BITXOR, "^" } */
1472 };
1473
1474 static slang_atom
1475 parse_operator_name(slang_parse_ctx * C)
1476 {
1477 unsigned int i;
1478
1479 for (i = 0; i < sizeof(operator_names) / sizeof(*operator_names); i++) {
1480 if (operator_names[i].o_code == (unsigned int) (*C->I)) {
1481 slang_atom atom =
1482 slang_atom_pool_atom(C->atoms, operator_names[i].o_name);
1483 if (atom == SLANG_ATOM_NULL) {
1484 slang_info_log_memory(C->L);
1485 return 0;
1486 }
1487 C->I++;
1488 return atom;
1489 }
1490 }
1491 return 0;
1492 }
1493
1494 static int
1495 parse_function_prototype(slang_parse_ctx * C, slang_output_ctx * O,
1496 slang_function * func)
1497 {
1498 /* parse function type and name */
1499 if (!parse_fully_specified_type(C, O, &func->header.type))
1500 return 0;
1501 switch (*C->I++) {
1502 case FUNCTION_ORDINARY:
1503 func->kind = slang_func_ordinary;
1504 func->header.a_name = parse_identifier(C);
1505 if (func->header.a_name == SLANG_ATOM_NULL)
1506 return 0;
1507 break;
1508 case FUNCTION_CONSTRUCTOR:
1509 func->kind = slang_func_constructor;
1510 if (func->header.type.specifier.type == slang_spec_struct)
1511 return 0;
1512 func->header.a_name =
1513 slang_atom_pool_atom(C->atoms,
1514 slang_type_specifier_type_to_string
1515 (func->header.type.specifier.type));
1516 if (func->header.a_name == SLANG_ATOM_NULL) {
1517 slang_info_log_memory(C->L);
1518 return 0;
1519 }
1520 break;
1521 case FUNCTION_OPERATOR:
1522 func->kind = slang_func_operator;
1523 func->header.a_name = parse_operator_name(C);
1524 if (func->header.a_name == SLANG_ATOM_NULL)
1525 return 0;
1526 break;
1527 default:
1528 return 0;
1529 }
1530
1531 /* parse function parameters */
1532 while (*C->I++ == PARAMETER_NEXT) {
1533 slang_variable *p = slang_variable_scope_grow(func->parameters);
1534 if (!p) {
1535 slang_info_log_memory(C->L);
1536 return 0;
1537 }
1538 if (!parse_parameter_declaration(C, O, p))
1539 return 0;
1540 }
1541
1542 #if 111
1543 /* if the function returns a value, append a hidden __retVal 'out'
1544 * parameter that corresponds to the return value.
1545 */
1546 if (_slang_function_has_return_value(func)) {
1547 slang_variable *p = slang_variable_scope_grow(func->parameters);
1548 slang_atom a_retVal = slang_atom_pool_atom(C->atoms, "__retVal");
1549 assert(a_retVal);
1550 p->a_name = a_retVal;
1551 p->type = func->header.type;
1552 p->type.qualifier = slang_qual_out;
1553 }
1554 #endif
1555
1556 /* function formal parameters and local variables share the same
1557 * scope, so save the information about param count in a seperate
1558 * place also link the scope to the global variable scope so when a
1559 * given identifier is not found here, the search process continues
1560 * in the global space
1561 */
1562 func->param_count = func->parameters->num_variables;
1563 func->parameters->outer_scope = O->vars;
1564
1565 return 1;
1566 }
1567
1568 static int
1569 parse_function_definition(slang_parse_ctx * C, slang_output_ctx * O,
1570 slang_function * func)
1571 {
1572 slang_output_ctx o = *O;
1573
1574 if (!parse_function_prototype(C, O, func))
1575 return 0;
1576
1577 /* create function's body operation */
1578 func->body =
1579 (slang_operation *) slang_alloc_malloc(sizeof(slang_operation));
1580 if (func->body == NULL) {
1581 slang_info_log_memory(C->L);
1582 return 0;
1583 }
1584 if (!slang_operation_construct(func->body)) {
1585 slang_alloc_free(func->body);
1586 func->body = NULL;
1587 slang_info_log_memory(C->L);
1588 return 0;
1589 }
1590
1591 /* to parse the body the parse context is modified in order to
1592 * capture parsed variables into function's local variable scope
1593 */
1594 C->global_scope = GL_FALSE;
1595 o.vars = func->parameters;
1596 if (!parse_statement(C, &o, func->body))
1597 return 0;
1598
1599 C->global_scope = GL_TRUE;
1600 return 1;
1601 }
1602
1603 static GLboolean
1604 initialize_global(slang_assemble_ctx * A, slang_variable * var)
1605 {
1606 slang_assembly_file_restore_point point;
1607 slang_machine mach;
1608 slang_assembly_local_info save_local = A->local;
1609 slang_operation op_id, op_assign;
1610 GLboolean result;
1611
1612 /* save the current assembly */
1613 if (!slang_assembly_file_restore_point_save(A->file, &point))
1614 return GL_FALSE;
1615
1616 /* setup the machine */
1617 mach = *A->mach;
1618 mach.ip = A->file->count;
1619
1620 /* allocate local storage for expression */
1621 A->local.ret_size = 0;
1622 A->local.addr_tmp = 0;
1623 A->local.swizzle_tmp = 4;
1624 if (!slang_assembly_file_push_label(A->file, slang_asm_local_alloc, 20))
1625 return GL_FALSE;
1626 if (!slang_assembly_file_push_label(A->file, slang_asm_enter, 20))
1627 return GL_FALSE;
1628
1629 /* construct the left side of assignment */
1630 if (!slang_operation_construct(&op_id))
1631 return GL_FALSE;
1632 op_id.type = slang_oper_identifier;
1633 op_id.a_id = var->a_name;
1634
1635 /* put the variable into operation's scope */
1636 op_id.locals->variables =
1637 (slang_variable *) slang_alloc_malloc(sizeof(slang_variable));
1638 if (op_id.locals->variables == NULL) {
1639 slang_operation_destruct(&op_id);
1640 return GL_FALSE;
1641 }
1642 op_id.locals->num_variables = 1;
1643 op_id.locals->variables[0] = *var;
1644
1645 /* construct the assignment expression */
1646 if (!slang_operation_construct(&op_assign)) {
1647 op_id.locals->num_variables = 0;
1648 slang_operation_destruct(&op_id);
1649 return GL_FALSE;
1650 }
1651 op_assign.type = slang_oper_assign;
1652 op_assign.children =
1653 (slang_operation *) slang_alloc_malloc(2 * sizeof(slang_operation));
1654 if (op_assign.children == NULL) {
1655 slang_operation_destruct(&op_assign);
1656 op_id.locals->num_variables = 0;
1657 slang_operation_destruct(&op_id);
1658 return GL_FALSE;
1659 }
1660 op_assign.num_children = 2;
1661 op_assign.children[0] = op_id;
1662 op_assign.children[1] = *var->initializer;
1663
1664 /* insert the actual expression */
1665 result = _slang_assemble_operation(A, &op_assign, slang_ref_forbid);
1666
1667 /* carefully destroy the operations */
1668 op_assign.num_children = 0;
1669 slang_alloc_free(op_assign.children);
1670 op_assign.children = NULL;
1671 slang_operation_destruct(&op_assign);
1672 op_id.locals->num_variables = 0;
1673 slang_operation_destruct(&op_id);
1674
1675 if (!result)
1676 return GL_FALSE;
1677 if (!slang_assembly_file_push(A->file, slang_asm_exit))
1678 return GL_FALSE;
1679
1680 /* execute the expression */
1681 if (!_slang_execute2(A->file, &mach))
1682 return GL_FALSE;
1683
1684 /* restore the old assembly */
1685 if (!slang_assembly_file_restore_point_load(A->file, &point))
1686 return GL_FALSE;
1687 A->local = save_local;
1688
1689 /* now we copy the contents of the initialized variable back to the original machine */
1690 _mesa_memcpy((GLubyte *) A->mach->mem + var->address,
1691 (GLubyte *) mach.mem + var->address, var->size);
1692
1693 return GL_TRUE;
1694 }
1695
1696 /* init declarator list */
1697 #define DECLARATOR_NONE 0
1698 #define DECLARATOR_NEXT 1
1699
1700 /* variable declaration */
1701 #define VARIABLE_NONE 0
1702 #define VARIABLE_IDENTIFIER 1
1703 #define VARIABLE_INITIALIZER 2
1704 #define VARIABLE_ARRAY_EXPLICIT 3
1705 #define VARIABLE_ARRAY_UNKNOWN 4
1706
1707
1708 /**
1709 * Parse the initializer for a variable declaration.
1710 */
1711 static int
1712 parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
1713 const slang_fully_specified_type * type)
1714 {
1715 slang_variable *var;
1716
1717 /* empty init declatator (without name, e.g. "float ;") */
1718 if (*C->I++ == VARIABLE_NONE)
1719 return 1;
1720
1721 /* make room for the new variable and initialize it */
1722 var = slang_variable_scope_grow(O->vars);
1723 if (!var) {
1724 slang_info_log_memory(C->L);
1725 return 0;
1726 }
1727
1728 /* copy the declarator qualifier type, parse the identifier */
1729 var->global = C->global_scope;
1730 var->type.qualifier = type->qualifier;
1731 var->a_name = parse_identifier(C);
1732 if (var->a_name == SLANG_ATOM_NULL)
1733 return 0;
1734
1735 switch (*C->I++) {
1736 case VARIABLE_NONE:
1737 /* simple variable declarator - just copy the specifier */
1738 if (!slang_type_specifier_copy(&var->type.specifier, &type->specifier))
1739 return 0;
1740 break;
1741 case VARIABLE_INITIALIZER:
1742 /* initialized variable - copy the specifier and parse the expression */
1743 if (!slang_type_specifier_copy(&var->type.specifier, &type->specifier))
1744 return 0;
1745 var->initializer =
1746 (slang_operation *) slang_alloc_malloc(sizeof(slang_operation));
1747 if (var->initializer == NULL) {
1748 slang_info_log_memory(C->L);
1749 return 0;
1750 }
1751 if (!slang_operation_construct(var->initializer)) {
1752 slang_alloc_free(var->initializer);
1753 var->initializer = NULL;
1754 slang_info_log_memory(C->L);
1755 return 0;
1756 }
1757 if (!parse_expression(C, O, var->initializer))
1758 return 0;
1759 break;
1760 #if 0
1761 case VARIABLE_ARRAY_UNKNOWN:
1762 /* unsized array - mark it as array and copy the specifier to
1763 the array element
1764 */
1765 if (!convert_to_array(C, var, &type->specifier))
1766 return GL_FALSE;
1767 break;
1768 #endif
1769 case VARIABLE_ARRAY_EXPLICIT:
1770 if (!convert_to_array(C, var, &type->specifier))
1771 return GL_FALSE;
1772 if (!parse_array_len(C, O, &var->array_len))
1773 return GL_FALSE;
1774 break;
1775 default:
1776 return 0;
1777 }
1778
1779 /* allocate global address space for a variable with a known size */
1780 if (C->global_scope
1781 && !(var->type.specifier.type == slang_spec_array
1782 && var->array_len == 0)) {
1783 if (!calculate_var_size(C, O, var))
1784 return GL_FALSE;
1785 var->address = slang_var_pool_alloc(O->global_pool, var->size);
1786 }
1787
1788 /* initialize global variable */
1789 if (C->global_scope) {
1790 if (var->initializer != NULL) {
1791 slang_assemble_ctx A;
1792
1793 A.file = O->assembly;
1794 A.mach = O->machine;
1795 A.atoms = C->atoms;
1796 A.space.funcs = O->funs;
1797 A.space.structs = O->structs;
1798 A.space.vars = O->vars;
1799 if (!initialize_global(&A, var))
1800 return 0;
1801 }
1802 else {
1803 _mesa_memset((GLubyte *) (O->machine->mem) + var->address, 0,
1804 var->size);
1805 }
1806 }
1807 return 1;
1808 }
1809
1810 /**
1811 * Parse a list of variable declarations. Each variable may have an
1812 * initializer.
1813 */
1814 static int
1815 parse_init_declarator_list(slang_parse_ctx * C, slang_output_ctx * O)
1816 {
1817 slang_fully_specified_type type;
1818
1819 /* parse the fully specified type, common to all declarators */
1820 if (!slang_fully_specified_type_construct(&type))
1821 return 0;
1822 if (!parse_fully_specified_type(C, O, &type)) {
1823 slang_fully_specified_type_destruct(&type);
1824 return 0;
1825 }
1826
1827 /* parse declarators, pass-in the parsed type */
1828 do {
1829 if (!parse_init_declarator(C, O, &type)) {
1830 slang_fully_specified_type_destruct(&type);
1831 return 0;
1832 }
1833 }
1834 while (*C->I++ == DECLARATOR_NEXT);
1835
1836 slang_fully_specified_type_destruct(&type);
1837 return 1;
1838 }
1839
1840
1841 /**
1842 * Parse a function definition or declaration.
1843 * \param C parsing context
1844 * \param O output context
1845 * \param definition if non-zero expect a definition, else a declaration
1846 * \param parsed_func_ret returns the parsed function
1847 * \return GL_TRUE if success, GL_FALSE if failure
1848 */
1849 static GLboolean
1850 parse_function(slang_parse_ctx * C, slang_output_ctx * O, int definition,
1851 slang_function ** parsed_func_ret)
1852 {
1853 slang_function parsed_func, *found_func;
1854
1855 /* parse function definition/declaration */
1856 if (!slang_function_construct(&parsed_func))
1857 return GL_FALSE;
1858 if (definition) {
1859 if (!parse_function_definition(C, O, &parsed_func)) {
1860 slang_function_destruct(&parsed_func);
1861 return GL_FALSE;
1862 }
1863 }
1864 else {
1865 if (!parse_function_prototype(C, O, &parsed_func)) {
1866 slang_function_destruct(&parsed_func);
1867 return GL_FALSE;
1868 }
1869 }
1870
1871 /* find a function with a prototype matching the parsed one - only
1872 * the current scope is being searched to allow built-in function
1873 * overriding
1874 */
1875 found_func = slang_function_scope_find(O->funs, &parsed_func, 0);
1876 if (found_func == NULL) {
1877 /* New function, add it to the function list */
1878 O->funs->functions =
1879 (slang_function *) slang_alloc_realloc(O->funs->functions,
1880 O->funs->num_functions *
1881 sizeof(slang_function),
1882 (O->funs->num_functions +
1883 1) * sizeof(slang_function));
1884 if (O->funs->functions == NULL) {
1885 slang_info_log_memory(C->L);
1886 slang_function_destruct(&parsed_func);
1887 return GL_FALSE;
1888 }
1889 O->funs->functions[O->funs->num_functions] = parsed_func;
1890 O->funs->num_functions++;
1891
1892 /* return the newly parsed function */
1893 *parsed_func_ret = &O->funs->functions[O->funs->num_functions - 1];
1894 }
1895 else {
1896 /* previously defined or declared */
1897 /* TODO: check function return type qualifiers and specifiers */
1898 if (definition) {
1899 if (found_func->body != NULL) {
1900 slang_info_log_error(C->L, "%s: function already has a body.",
1901 slang_atom_pool_id(C->atoms,
1902 parsed_func.header.
1903 a_name));
1904 slang_function_destruct(&parsed_func);
1905 return GL_FALSE;
1906 }
1907
1908 /* destroy the existing function declaration and replace it
1909 * with the new one, remember to save the fixup table
1910 */
1911 parsed_func.fixups = found_func->fixups;
1912 slang_fixup_table_init(&found_func->fixups);
1913 slang_function_destruct(found_func);
1914 *found_func = parsed_func;
1915 }
1916 else {
1917 /* another declaration of the same function prototype - ignore it */
1918 slang_function_destruct(&parsed_func);
1919 }
1920
1921 /* return the found function */
1922 *parsed_func_ret = found_func;
1923 }
1924
1925 /* assemble the parsed function */
1926 {
1927 slang_assemble_ctx A;
1928
1929 A.file = O->assembly;
1930 A.mach = O->machine;
1931 A.atoms = C->atoms;
1932 A.space.funcs = O->funs;
1933 A.space.structs = O->structs;
1934 A.space.vars = O->vars;
1935 A.program = O->program;
1936
1937 _slang_reset_error();
1938
1939 #if 0
1940 printf("*************** Assemble function %s ****\n", (char *) (*parsed_func_ret)->header.a_name);
1941 slang_print_var_scope((*parsed_func_ret)->parameters,
1942 (*parsed_func_ret)->param_count);
1943 #endif
1944
1945
1946 if (!_slang_assemble_function(&A, *parsed_func_ret)) {
1947 /* propogate the error message back through the info log */
1948 C->L->text = _mesa_strdup(_slang_error_text());
1949 C->L->dont_free_text = GL_FALSE;
1950 return GL_FALSE;
1951 }
1952
1953
1954 #if 0
1955 printf("**************************************\n");
1956 #endif
1957 #if 1
1958 _slang_codegen_function(&A, *parsed_func_ret);
1959 #endif
1960 }
1961 return GL_TRUE;
1962 }
1963
1964 /* declaration */
1965 #define DECLARATION_FUNCTION_PROTOTYPE 1
1966 #define DECLARATION_INIT_DECLARATOR_LIST 2
1967
1968 static int
1969 parse_declaration(slang_parse_ctx * C, slang_output_ctx * O)
1970 {
1971 switch (*C->I++) {
1972 case DECLARATION_INIT_DECLARATOR_LIST:
1973 if (!parse_init_declarator_list(C, O))
1974 return 0;
1975 break;
1976 case DECLARATION_FUNCTION_PROTOTYPE:
1977 {
1978 slang_function *dummy_func;
1979
1980 if (!parse_function(C, O, 0, &dummy_func))
1981 return 0;
1982 }
1983 break;
1984 default:
1985 return 0;
1986 }
1987 return 1;
1988 }
1989
1990 /* external declaration */
1991 #define EXTERNAL_NULL 0
1992 #define EXTERNAL_FUNCTION_DEFINITION 1
1993 #define EXTERNAL_DECLARATION 2
1994
1995 static GLboolean
1996 parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
1997 struct gl_program *program)
1998 {
1999 slang_output_ctx o;
2000
2001 /* setup output context */
2002 o.funs = &unit->funs;
2003 o.structs = &unit->structs;
2004 o.vars = &unit->vars;
2005 o.assembly = &unit->object->assembly;
2006 o.global_pool = &unit->object->varpool;
2007 o.machine = &unit->object->machine;
2008 o.program = program;
2009
2010 /* parse individual functions and declarations */
2011 while (*C->I != EXTERNAL_NULL) {
2012 switch (*C->I++) {
2013 case EXTERNAL_FUNCTION_DEFINITION:
2014 {
2015 slang_function *func;
2016
2017 if (!parse_function(C, &o, 1, &func))
2018 return GL_FALSE;
2019 }
2020 break;
2021 case EXTERNAL_DECLARATION:
2022 if (!parse_declaration(C, &o))
2023 return GL_FALSE;
2024 break;
2025 default:
2026 return GL_FALSE;
2027 }
2028 }
2029 C->I++;
2030 return GL_TRUE;
2031 }
2032
2033 static GLboolean
2034 compile_binary(const byte * prod, slang_code_unit * unit,
2035 slang_unit_type type, slang_info_log * infolog,
2036 slang_code_unit * builtin, slang_code_unit * downlink,
2037 struct gl_program *program)
2038 {
2039 slang_parse_ctx C;
2040
2041 unit->type = type;
2042
2043 /* setup parse context */
2044 C.I = prod;
2045 C.L = infolog;
2046 C.parsing_builtin = (builtin == NULL);
2047 C.global_scope = GL_TRUE;
2048 C.atoms = &unit->object->atompool;
2049
2050 if (!check_revision(&C))
2051 return GL_FALSE;
2052
2053 if (downlink != NULL) {
2054 unit->vars.outer_scope = &downlink->vars;
2055 unit->funs.outer_scope = &downlink->funs;
2056 unit->structs.outer_scope = &downlink->structs;
2057 }
2058
2059 /* parse translation unit */
2060 return parse_code_unit(&C, unit, program);
2061 }
2062
2063 static GLboolean
2064 compile_with_grammar(grammar id, const char *source, slang_code_unit * unit,
2065 slang_unit_type type, slang_info_log * infolog,
2066 slang_code_unit * builtin,
2067 struct gl_program *program)
2068 {
2069 byte *prod;
2070 GLuint size, start, version;
2071 slang_string preprocessed;
2072
2073 /* First retrieve the version number. */
2074 if (!_slang_preprocess_version(source, &version, &start, infolog))
2075 return GL_FALSE;
2076
2077 if (version > 110) {
2078 slang_info_log_error(infolog,
2079 "language version specified is not supported.");
2080 return GL_FALSE;
2081 }
2082
2083 /* Now preprocess the source string. */
2084 slang_string_init(&preprocessed);
2085 if (!_slang_preprocess_directives(&preprocessed, &source[start], infolog)) {
2086 slang_string_free(&preprocessed);
2087 slang_info_log_error(infolog, "failed to preprocess the source.");
2088 return GL_FALSE;
2089 }
2090
2091 /* Finally check the syntax and generate its binary representation. */
2092 if (!grammar_fast_check(id,
2093 (const byte *) (slang_string_cstr(&preprocessed)),
2094 &prod, &size,
2095 65536)) {
2096 char buf[1024];
2097 GLint pos;
2098
2099 slang_string_free(&preprocessed);
2100 grammar_get_last_error((byte *) (buf), sizeof(buf), &pos);
2101 slang_info_log_error(infolog, buf);
2102 RETURN_ERROR("syntax error (possibly in library code)", 0);
2103 }
2104 slang_string_free(&preprocessed);
2105
2106 /* Syntax is okay - translate it to internal representation. */
2107 if (!compile_binary(prod, unit, type, infolog, builtin,
2108 &builtin[SLANG_BUILTIN_TOTAL - 1],
2109 program)) {
2110 grammar_alloc_free(prod);
2111 return GL_FALSE;
2112 }
2113 grammar_alloc_free(prod);
2114 return GL_TRUE;
2115 }
2116
2117 LONGSTRING static const char *slang_shader_syn =
2118 #include "library/slang_shader_syn.h"
2119 ;
2120
2121 static const byte slang_core_gc[] = {
2122 #include "library/slang_core_gc.h"
2123 };
2124
2125 static const byte slang_common_builtin_gc[] = {
2126 #include "library/slang_common_builtin_gc.h"
2127 };
2128
2129 static const byte slang_fragment_builtin_gc[] = {
2130 #include "library/slang_fragment_builtin_gc.h"
2131 };
2132
2133 static const byte slang_vertex_builtin_gc[] = {
2134 #include "library/slang_vertex_builtin_gc.h"
2135 };
2136
2137 #if defined(USE_X86_ASM) || defined(SLANG_X86)
2138 foo
2139 static const byte slang_builtin_vec4_gc[] = {
2140 #include "library/slang_builtin_vec4_gc.h"
2141 };
2142 #endif
2143
2144 static GLboolean
2145 compile_object(grammar * id, const char *source, slang_code_object * object,
2146 slang_unit_type type, slang_info_log * infolog,
2147 struct gl_program *program)
2148 {
2149 slang_code_unit *builtins = NULL;
2150
2151 /* load GLSL grammar */
2152 *id = grammar_load_from_text((const byte *) (slang_shader_syn));
2153 if (*id == 0) {
2154 byte buf[1024];
2155 int pos;
2156
2157 grammar_get_last_error(buf, 1024, &pos);
2158 slang_info_log_error(infolog, (const char *) (buf));
2159 return GL_FALSE;
2160 }
2161
2162 /* set shader type - the syntax is slightly different for different shaders */
2163 if (type == slang_unit_fragment_shader
2164 || type == slang_unit_fragment_builtin)
2165 grammar_set_reg8(*id, (const byte *) "shader_type", 1);
2166 else
2167 grammar_set_reg8(*id, (const byte *) "shader_type", 2);
2168
2169 /* enable language extensions */
2170 grammar_set_reg8(*id, (const byte *) "parsing_builtin", 1);
2171
2172 /* if parsing user-specified shader, load built-in library */
2173 if (type == slang_unit_fragment_shader || type == slang_unit_vertex_shader) {
2174 /* compile core functionality first */
2175 if (!compile_binary(slang_core_gc,
2176 &object->builtin[SLANG_BUILTIN_CORE],
2177 slang_unit_fragment_builtin, infolog,
2178 NULL, NULL, NULL))
2179 return GL_FALSE;
2180
2181 /* compile common functions and variables, link to core */
2182 if (!compile_binary(slang_common_builtin_gc,
2183 &object->builtin[SLANG_BUILTIN_COMMON],
2184 slang_unit_fragment_builtin, infolog, NULL,
2185 &object->builtin[SLANG_BUILTIN_CORE], NULL))
2186 return GL_FALSE;
2187
2188 /* compile target-specific functions and variables, link to common */
2189 if (type == slang_unit_fragment_shader) {
2190 if (!compile_binary(slang_fragment_builtin_gc,
2191 &object->builtin[SLANG_BUILTIN_TARGET],
2192 slang_unit_fragment_builtin, infolog, NULL,
2193 &object->builtin[SLANG_BUILTIN_COMMON], NULL))
2194 return GL_FALSE;
2195 }
2196 else if (type == slang_unit_vertex_shader) {
2197 if (!compile_binary(slang_vertex_builtin_gc,
2198 &object->builtin[SLANG_BUILTIN_TARGET],
2199 slang_unit_vertex_builtin, infolog, NULL,
2200 &object->builtin[SLANG_BUILTIN_COMMON], NULL))
2201 return GL_FALSE;
2202 }
2203
2204 #if defined(USE_X86_ASM) || defined(SLANG_X86)
2205 /* compile x86 4-component vector overrides, link to target */
2206 if (!compile_binary(slang_builtin_vec4_gc,
2207 &object->builtin[SLANG_BUILTIN_VEC4],
2208 slang_unit_fragment_builtin, infolog, NULL,
2209 &object->builtin[SLANG_BUILTIN_TARGET]))
2210 return GL_FALSE;
2211 #endif
2212
2213 /* disable language extensions */
2214 #if NEW_SLANG /* allow-built-ins */
2215 grammar_set_reg8(*id, (const byte *) "parsing_builtin", 1);
2216 #else
2217 grammar_set_reg8(*id, (const byte *) "parsing_builtin", 0);
2218 #endif
2219 builtins = object->builtin;
2220 }
2221
2222 /* compile the actual shader - pass-in built-in library for external shader */
2223 return compile_with_grammar(*id, source, &object->unit, type, infolog,
2224 builtins, program);
2225 }
2226
2227
2228 #if 0
2229 static void
2230 slang_create_uniforms(const slang_export_data_table *exports,
2231 struct gl_program *program)
2232 {
2233 /* XXX only add uniforms that are actually going to get used */
2234 GLuint i;
2235 for (i = 0; i < exports->count; i++) {
2236 if (exports->entries[i].access == slang_exp_uniform) {
2237 const char *name = (char *) exports->entries[i].quant.name;
2238 GLint j = _mesa_add_uniform(program->Parameters, name, 4);
2239 assert(j >= 0);
2240 }
2241 }
2242 }
2243 #endif
2244
2245
2246 static GLboolean
2247 compile_shader(GLcontext *ctx, slang_code_object * object,
2248 slang_unit_type type, slang_info_log * infolog,
2249 struct gl_shader *shader)
2250 {
2251 struct gl_program *program = shader->Programs[0];
2252 GLboolean success;
2253 grammar id = 0;
2254
2255 assert(program);
2256
2257 _slang_code_object_dtr(object);
2258 _slang_code_object_ctr(object);
2259
2260 success = compile_object(&id, shader->Source, object, type, infolog, program);
2261 if (id != 0)
2262 grammar_destroy(id);
2263 if (!success)
2264 return GL_FALSE;
2265
2266 if (!_slang_build_export_data_table(&object->expdata, &object->unit.vars))
2267 return GL_FALSE;
2268 if (!_slang_build_export_code_table(&object->expcode, &object->unit.funs,
2269 &object->unit))
2270 return GL_FALSE;
2271
2272 #if NEW_SLANG
2273 {
2274 slang_create_uniforms(&object->expdata, shader);
2275 _mesa_print_program(program);
2276 _mesa_print_program_parameters(ctx, program);
2277 }
2278 #endif
2279
2280 return GL_TRUE;
2281 }
2282
2283
2284
2285 GLboolean
2286 _slang_compile(GLcontext *ctx, struct gl_shader *shader)
2287 {
2288 GLboolean success;
2289 slang_info_log info_log;
2290 slang_code_object obj;
2291 slang_unit_type type;
2292
2293 if (shader->Type == GL_VERTEX_SHADER) {
2294 type = slang_unit_vertex_shader;
2295 }
2296 else {
2297 assert(shader->Type == GL_FRAGMENT_SHADER);
2298 type = slang_unit_fragment_shader;
2299 }
2300
2301 /* XXX temporary hack */
2302 if (!shader->Programs) {
2303 GLenum progTarget;
2304 if (shader->Type == GL_VERTEX_SHADER)
2305 progTarget = GL_VERTEX_PROGRAM_ARB;
2306 else
2307 progTarget = GL_FRAGMENT_PROGRAM_ARB;
2308 shader->Programs
2309 = (struct gl_program **) malloc(sizeof(struct gl_program*));
2310 shader->Programs[0] = _mesa_new_program(ctx, progTarget, 1);
2311 shader->NumPrograms = 1;
2312 }
2313
2314 slang_info_log_construct(&info_log);
2315 _slang_code_object_ctr(&obj);
2316
2317 success = compile_shader(ctx, &obj, type, &info_log, shader);
2318
2319 if (success) {
2320 #if 0
2321 slang_create_uniforms(&object->expdata, shader);
2322 _mesa_print_program(program);
2323 _mesa_print_program_parameters(ctx, program);
2324 #endif
2325 }
2326 else {
2327 /* XXX more work on info log needed here */
2328 if (info_log.text) {
2329 if (shader->InfoLog) {
2330 free(shader->InfoLog);
2331 shader->InfoLog = NULL;
2332 }
2333 shader->InfoLog = strdup(info_log.text);
2334 }
2335 }
2336
2337 slang_info_log_destruct(&info_log);
2338 _slang_code_object_dtr(&obj);
2339
2340 return success;
2341 }
2342