2 * Mesa 3-D graphics library
5 * Copyright (C) 2005-2006 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 * \file slang_compile.c
27 * slang front-end compiler
34 #include "prog_parameter.h"
35 #include "grammar_mesa.h"
36 #include "slang_codegen.h"
37 #include "slang_compile.h"
38 #include "slang_preprocess.h"
39 #include "slang_storage.h"
40 #include "slang_error.h"
41 #include "slang_emit.h"
42 #include "slang_vartable.h"
44 #include "slang_print.h"
47 * This is a straightforward implementation of the slang front-end
48 * compiler. Lots of error-checking functionality is missing but
49 * every well-formed shader source should compile successfully and
50 * execute as expected. However, some semantically ill-formed shaders
51 * may be accepted resulting in undefined behaviour.
57 * Allocate storage for a variable of 'size' bytes from given pool.
58 * Return the allocated address for the variable.
61 slang_var_pool_alloc(slang_var_pool
* pool
, unsigned int size
)
63 const GLuint addr
= pool
->next_addr
;
64 pool
->next_addr
+= size
;
73 _slang_code_unit_ctr(slang_code_unit
* self
,
74 struct slang_code_object_
* object
)
76 _slang_variable_scope_ctr(&self
->vars
);
77 _slang_function_scope_ctr(&self
->funs
);
78 _slang_struct_scope_ctr(&self
->structs
);
79 self
->object
= object
;
83 _slang_code_unit_dtr(slang_code_unit
* self
)
85 slang_variable_scope_destruct(&self
->vars
);
86 slang_function_scope_destruct(&self
->funs
);
87 slang_struct_scope_destruct(&self
->structs
);
95 _slang_code_object_ctr(slang_code_object
* self
)
99 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
100 _slang_code_unit_ctr(&self
->builtin
[i
], self
);
101 _slang_code_unit_ctr(&self
->unit
, self
);
103 _slang_assembly_file_ctr(&self
->assembly
);
105 slang_machine_ctr(&self
->machine
);
106 self
->varpool
.next_addr
= 0;
107 slang_atom_pool_construct(&self
->atompool
);
108 slang_export_data_table_ctr(&self
->expdata
);
109 self
->expdata
.atoms
= &self
->atompool
;
110 slang_export_code_table_ctr(&self
->expcode
);
111 self
->expcode
.atoms
= &self
->atompool
;
115 _slang_code_object_dtr(slang_code_object
* self
)
119 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
120 _slang_code_unit_dtr(&self
->builtin
[i
]);
121 _slang_code_unit_dtr(&self
->unit
);
123 slang_assembly_file_destruct(&self
->assembly
);
125 slang_machine_dtr(&self
->machine
);
126 slang_atom_pool_destruct(&self
->atompool
);
127 slang_export_data_table_dtr(&self
->expdata
);
128 slang_export_code_table_ctr(&self
->expcode
);
133 static char *out_of_memory
= "Error: Out of memory.\n";
136 slang_info_log_construct(slang_info_log
* log
)
139 log
->dont_free_text
= 0;
143 slang_info_log_destruct(slang_info_log
* log
)
145 if (!log
->dont_free_text
)
146 slang_alloc_free(log
->text
);
150 slang_info_log_message(slang_info_log
* log
, const char *prefix
,
155 if (log
->dont_free_text
)
157 size
= slang_string_length(msg
) + 2;
159 size
+= slang_string_length(prefix
) + 2;
160 if (log
->text
!= NULL
) {
161 GLuint old_len
= slang_string_length(log
->text
);
163 slang_alloc_realloc(log
->text
, old_len
+ 1, old_len
+ size
);
166 log
->text
= (char *) (slang_alloc_malloc(size
));
167 if (log
->text
!= NULL
)
170 if (log
->text
== NULL
)
172 if (prefix
!= NULL
) {
173 slang_string_concat(log
->text
, prefix
);
174 slang_string_concat(log
->text
, ": ");
176 slang_string_concat(log
->text
, msg
);
177 slang_string_concat(log
->text
, "\n");
182 slang_info_log_print(slang_info_log
* log
, const char *msg
, ...)
188 _mesa_vsprintf(buf
, msg
, va
);
190 return slang_info_log_message(log
, NULL
, buf
);
194 slang_info_log_error(slang_info_log
* log
, const char *msg
, ...)
200 _mesa_vsprintf(buf
, msg
, va
);
202 if (slang_info_log_message(log
, "Error", buf
))
204 slang_info_log_memory(log
);
209 slang_info_log_warning(slang_info_log
* log
, const char *msg
, ...)
215 _mesa_vsprintf(buf
, msg
, va
);
217 if (slang_info_log_message(log
, "Warning", buf
))
219 slang_info_log_memory(log
);
224 slang_info_log_memory(slang_info_log
* log
)
226 if (!slang_info_log_message(log
, "Error", "Out of memory.")) {
227 log
->dont_free_text
= 1;
228 log
->text
= out_of_memory
;
233 /* slang_parse_ctx */
235 typedef struct slang_parse_ctx_
240 GLboolean global_scope
; /**< Is object being declared a global? */
241 slang_atom_pool
*atoms
;
242 slang_unit_type type
; /**< Vertex vs. Fragment */
245 /* slang_output_ctx */
247 typedef struct slang_output_ctx_
249 slang_variable_scope
*vars
;
250 slang_function_scope
*funs
;
251 slang_struct_scope
*structs
;
252 slang_assembly_file
*assembly
;
253 slang_var_pool
*global_pool
;
254 slang_machine
*machine
;
255 struct gl_program
*program
;
256 slang_var_table
*vartable
;
259 /* _slang_compile() */
262 parse_identifier_str(slang_parse_ctx
* C
, char **id
)
265 C
->I
+= _mesa_strlen(*id
) + 1;
269 parse_identifier(slang_parse_ctx
* C
)
273 id
= (const char *) C
->I
;
274 C
->I
+= _mesa_strlen(id
) + 1;
275 return slang_atom_pool_atom(C
->atoms
, id
);
279 parse_number(slang_parse_ctx
* C
, int *number
)
281 const int radix
= (int) (*C
->I
++);
283 while (*C
->I
!= '\0') {
285 if (*C
->I
>= '0' && *C
->I
<= '9')
286 digit
= (int) (*C
->I
- '0');
287 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
288 digit
= (int) (*C
->I
- 'A') + 10;
290 digit
= (int) (*C
->I
- 'a') + 10;
291 *number
= *number
* radix
+ digit
;
296 slang_info_log_warning(C
->L
, "%d: literal integer overflow.", *number
);
301 parse_float(slang_parse_ctx
* C
, float *number
)
303 char *integral
= NULL
;
304 char *fractional
= NULL
;
305 char *exponent
= NULL
;
308 parse_identifier_str(C
, &integral
);
309 parse_identifier_str(C
, &fractional
);
310 parse_identifier_str(C
, &exponent
);
312 whole
= (char *) (slang_alloc_malloc((_mesa_strlen(integral
) +
313 _mesa_strlen(fractional
) +
314 _mesa_strlen(exponent
) + 3) * sizeof(char)));
316 slang_info_log_memory(C
->L
);
320 slang_string_copy(whole
, integral
);
321 slang_string_concat(whole
, ".");
322 slang_string_concat(whole
, fractional
);
323 slang_string_concat(whole
, "E");
324 slang_string_concat(whole
, exponent
);
326 *number
= (float) (_mesa_strtod(whole
, (char **) NULL
));
328 slang_alloc_free(whole
);
332 /* revision number - increment after each change affecting emitted output */
336 check_revision(slang_parse_ctx
* C
)
338 if (*C
->I
!= REVISION
) {
339 slang_info_log_error(C
->L
, "Internal compiler error.");
346 static int parse_statement(slang_parse_ctx
*, slang_output_ctx
*,
348 static int parse_expression(slang_parse_ctx
*, slang_output_ctx
*,
350 static int parse_type_specifier(slang_parse_ctx
*, slang_output_ctx
*,
351 slang_type_specifier
*);
354 parse_array_len(slang_parse_ctx
* C
, slang_output_ctx
* O
, GLuint
* len
)
356 slang_operation array_size
;
357 slang_assembly_name_space space
;
360 if (!slang_operation_construct(&array_size
))
362 if (!parse_expression(C
, O
, &array_size
)) {
363 slang_operation_destruct(&array_size
);
367 space
.funcs
= O
->funs
;
368 space
.structs
= O
->structs
;
369 space
.vars
= O
->vars
;
370 result
= _slang_evaluate_int(O
->assembly
, O
->machine
, &space
,
371 &array_size
, len
, C
->atoms
);
372 slang_operation_destruct(&array_size
);
377 calculate_var_size(slang_parse_ctx
* C
, slang_output_ctx
* O
,
378 slang_variable
* var
)
380 slang_storage_aggregate agg
;
382 if (!slang_storage_aggregate_construct(&agg
))
384 if (!_slang_aggregate_variable(&agg
, &var
->type
.specifier
, var
->array_len
,
385 O
->funs
, O
->structs
, O
->vars
, O
->machine
,
386 O
->assembly
, C
->atoms
)) {
387 slang_storage_aggregate_destruct(&agg
);
390 var
->size
= _slang_sizeof_aggregate(&agg
);
391 slang_storage_aggregate_destruct(&agg
);
396 convert_to_array(slang_parse_ctx
* C
, slang_variable
* var
,
397 const slang_type_specifier
* sp
)
399 /* sized array - mark it as array, copy the specifier to the array element and
400 * parse the expression */
401 var
->type
.specifier
.type
= slang_spec_array
;
402 var
->type
.specifier
._array
= (slang_type_specifier
*)
403 slang_alloc_malloc(sizeof(slang_type_specifier
));
404 if (var
->type
.specifier
._array
== NULL
) {
405 slang_info_log_memory(C
->L
);
408 slang_type_specifier_ctr(var
->type
.specifier
._array
);
409 return slang_type_specifier_copy(var
->type
.specifier
._array
, sp
);
412 /* structure field */
415 #define FIELD_ARRAY 2
418 parse_struct_field_var(slang_parse_ctx
* C
, slang_output_ctx
* O
,
419 slang_variable
* var
, const slang_type_specifier
* sp
)
421 var
->a_name
= parse_identifier(C
);
422 if (var
->a_name
== SLANG_ATOM_NULL
)
427 if (!slang_type_specifier_copy(&var
->type
.specifier
, sp
))
431 if (!convert_to_array(C
, var
, sp
))
433 if (!parse_array_len(C
, O
, &var
->array_len
))
440 return calculate_var_size(C
, O
, var
);
444 parse_struct_field(slang_parse_ctx
* C
, slang_output_ctx
* O
,
445 slang_struct
* st
, slang_type_specifier
* sp
)
447 slang_output_ctx o
= *O
;
449 o
.structs
= st
->structs
;
450 if (!parse_type_specifier(C
, &o
, sp
))
454 slang_variable
*var
= slang_variable_scope_grow(st
->fields
);
456 slang_info_log_memory(C
->L
);
459 if (!parse_struct_field_var(C
, &o
, var
, sp
))
462 while (*C
->I
++ != FIELD_NONE
);
468 parse_struct(slang_parse_ctx
* C
, slang_output_ctx
* O
, slang_struct
** st
)
473 /* parse struct name (if any) and make sure it is unique in current scope */
474 a_name
= parse_identifier(C
);
475 if (a_name
== SLANG_ATOM_NULL
)
478 name
= slang_atom_pool_id(C
->atoms
, a_name
);
480 && slang_struct_scope_find(O
->structs
, a_name
, 0) != NULL
) {
481 slang_info_log_error(C
->L
, "%s: duplicate type name.", name
);
485 /* set-up a new struct */
486 *st
= (slang_struct
*) slang_alloc_malloc(sizeof(slang_struct
));
488 slang_info_log_memory(C
->L
);
491 if (!slang_struct_construct(*st
)) {
492 slang_alloc_free(*st
);
494 slang_info_log_memory(C
->L
);
497 (**st
).a_name
= a_name
;
498 (**st
).structs
->outer_scope
= O
->structs
;
500 /* parse individual struct fields */
502 slang_type_specifier sp
;
504 slang_type_specifier_ctr(&sp
);
505 if (!parse_struct_field(C
, O
, *st
, &sp
)) {
506 slang_type_specifier_dtr(&sp
);
509 slang_type_specifier_dtr(&sp
);
511 while (*C
->I
++ != FIELD_NONE
);
513 /* if named struct, copy it to current scope */
514 if (name
[0] != '\0') {
517 O
->structs
->structs
=
518 (slang_struct
*) slang_alloc_realloc(O
->structs
->structs
,
519 O
->structs
->num_structs
*
520 sizeof(slang_struct
),
521 (O
->structs
->num_structs
+
522 1) * sizeof(slang_struct
));
523 if (O
->structs
->structs
== NULL
) {
524 slang_info_log_memory(C
->L
);
527 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
528 if (!slang_struct_construct(s
))
530 O
->structs
->num_structs
++;
531 if (!slang_struct_copy(s
, *st
))
540 #define TYPE_QUALIFIER_NONE 0
541 #define TYPE_QUALIFIER_CONST 1
542 #define TYPE_QUALIFIER_ATTRIBUTE 2
543 #define TYPE_QUALIFIER_VARYING 3
544 #define TYPE_QUALIFIER_UNIFORM 4
545 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
546 #define TYPE_QUALIFIER_FIXEDINPUT 6
549 parse_type_qualifier(slang_parse_ctx
* C
, slang_type_qualifier
* qual
)
552 case TYPE_QUALIFIER_NONE
:
553 *qual
= slang_qual_none
;
555 case TYPE_QUALIFIER_CONST
:
556 *qual
= slang_qual_const
;
558 case TYPE_QUALIFIER_ATTRIBUTE
:
559 *qual
= slang_qual_attribute
;
561 case TYPE_QUALIFIER_VARYING
:
562 *qual
= slang_qual_varying
;
564 case TYPE_QUALIFIER_UNIFORM
:
565 *qual
= slang_qual_uniform
;
567 case TYPE_QUALIFIER_FIXEDOUTPUT
:
568 *qual
= slang_qual_fixedoutput
;
570 case TYPE_QUALIFIER_FIXEDINPUT
:
571 *qual
= slang_qual_fixedinput
;
580 #define TYPE_SPECIFIER_VOID 0
581 #define TYPE_SPECIFIER_BOOL 1
582 #define TYPE_SPECIFIER_BVEC2 2
583 #define TYPE_SPECIFIER_BVEC3 3
584 #define TYPE_SPECIFIER_BVEC4 4
585 #define TYPE_SPECIFIER_INT 5
586 #define TYPE_SPECIFIER_IVEC2 6
587 #define TYPE_SPECIFIER_IVEC3 7
588 #define TYPE_SPECIFIER_IVEC4 8
589 #define TYPE_SPECIFIER_FLOAT 9
590 #define TYPE_SPECIFIER_VEC2 10
591 #define TYPE_SPECIFIER_VEC3 11
592 #define TYPE_SPECIFIER_VEC4 12
593 #define TYPE_SPECIFIER_MAT2 13
594 #define TYPE_SPECIFIER_MAT3 14
595 #define TYPE_SPECIFIER_MAT4 15
596 #define TYPE_SPECIFIER_SAMPLER1D 16
597 #define TYPE_SPECIFIER_SAMPLER2D 17
598 #define TYPE_SPECIFIER_SAMPLER3D 18
599 #define TYPE_SPECIFIER_SAMPLERCUBE 19
600 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
601 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
602 #define TYPE_SPECIFIER_STRUCT 22
603 #define TYPE_SPECIFIER_TYPENAME 23
606 parse_type_specifier(slang_parse_ctx
* C
, slang_output_ctx
* O
,
607 slang_type_specifier
* spec
)
610 case TYPE_SPECIFIER_VOID
:
611 spec
->type
= slang_spec_void
;
613 case TYPE_SPECIFIER_BOOL
:
614 spec
->type
= slang_spec_bool
;
616 case TYPE_SPECIFIER_BVEC2
:
617 spec
->type
= slang_spec_bvec2
;
619 case TYPE_SPECIFIER_BVEC3
:
620 spec
->type
= slang_spec_bvec3
;
622 case TYPE_SPECIFIER_BVEC4
:
623 spec
->type
= slang_spec_bvec4
;
625 case TYPE_SPECIFIER_INT
:
626 spec
->type
= slang_spec_int
;
628 case TYPE_SPECIFIER_IVEC2
:
629 spec
->type
= slang_spec_ivec2
;
631 case TYPE_SPECIFIER_IVEC3
:
632 spec
->type
= slang_spec_ivec3
;
634 case TYPE_SPECIFIER_IVEC4
:
635 spec
->type
= slang_spec_ivec4
;
637 case TYPE_SPECIFIER_FLOAT
:
638 spec
->type
= slang_spec_float
;
640 case TYPE_SPECIFIER_VEC2
:
641 spec
->type
= slang_spec_vec2
;
643 case TYPE_SPECIFIER_VEC3
:
644 spec
->type
= slang_spec_vec3
;
646 case TYPE_SPECIFIER_VEC4
:
647 spec
->type
= slang_spec_vec4
;
649 case TYPE_SPECIFIER_MAT2
:
650 spec
->type
= slang_spec_mat2
;
652 case TYPE_SPECIFIER_MAT3
:
653 spec
->type
= slang_spec_mat3
;
655 case TYPE_SPECIFIER_MAT4
:
656 spec
->type
= slang_spec_mat4
;
658 case TYPE_SPECIFIER_SAMPLER1D
:
659 spec
->type
= slang_spec_sampler1D
;
661 case TYPE_SPECIFIER_SAMPLER2D
:
662 spec
->type
= slang_spec_sampler2D
;
664 case TYPE_SPECIFIER_SAMPLER3D
:
665 spec
->type
= slang_spec_sampler3D
;
667 case TYPE_SPECIFIER_SAMPLERCUBE
:
668 spec
->type
= slang_spec_samplerCube
;
670 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
671 spec
->type
= slang_spec_sampler1DShadow
;
673 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
674 spec
->type
= slang_spec_sampler2DShadow
;
676 case TYPE_SPECIFIER_STRUCT
:
677 spec
->type
= slang_spec_struct
;
678 if (!parse_struct(C
, O
, &spec
->_struct
))
681 case TYPE_SPECIFIER_TYPENAME
:
682 spec
->type
= slang_spec_struct
;
687 a_name
= parse_identifier(C
);
691 stru
= slang_struct_scope_find(O
->structs
, a_name
, 1);
693 slang_info_log_error(C
->L
, "undeclared type name '%s'",
694 slang_atom_pool_id(C
->atoms
, a_name
));
699 (slang_struct
*) slang_alloc_malloc(sizeof(slang_struct
));
700 if (spec
->_struct
== NULL
) {
701 slang_info_log_memory(C
->L
);
704 if (!slang_struct_construct(spec
->_struct
)) {
705 slang_alloc_free(spec
->_struct
);
706 spec
->_struct
= NULL
;
709 if (!slang_struct_copy(spec
->_struct
, stru
))
720 parse_fully_specified_type(slang_parse_ctx
* C
, slang_output_ctx
* O
,
721 slang_fully_specified_type
* type
)
723 if (!parse_type_qualifier(C
, &type
->qualifier
))
725 if (!parse_type_specifier(C
, O
, &type
->specifier
))
732 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
733 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
737 #define OP_CONTINUE 6
740 #define OP_EXPRESSION 9
745 #define OP_PUSH_VOID 14
746 #define OP_PUSH_BOOL 15
747 #define OP_PUSH_INT 16
748 #define OP_PUSH_FLOAT 17
749 #define OP_PUSH_IDENTIFIER 18
750 #define OP_SEQUENCE 19
752 #define OP_ADDASSIGN 21
753 #define OP_SUBASSIGN 22
754 #define OP_MULASSIGN 23
755 #define OP_DIVASSIGN 24
756 /*#define OP_MODASSIGN 25*/
757 /*#define OP_LSHASSIGN 26*/
758 /*#define OP_RSHASSIGN 27*/
759 /*#define OP_ORASSIGN 28*/
760 /*#define OP_XORASSIGN 29*/
761 /*#define OP_ANDASSIGN 30*/
763 #define OP_LOGICALOR 32
764 #define OP_LOGICALXOR 33
765 #define OP_LOGICALAND 34
766 /*#define OP_BITOR 35*/
767 /*#define OP_BITXOR 36*/
768 /*#define OP_BITAND 37*/
770 #define OP_NOTEQUAL 39
772 #define OP_GREATER 41
773 #define OP_LESSEQUAL 42
774 #define OP_GREATEREQUAL 43
775 /*#define OP_LSHIFT 44*/
776 /*#define OP_RSHIFT 45*/
778 #define OP_SUBTRACT 47
779 #define OP_MULTIPLY 48
781 /*#define OP_MODULUS 50*/
782 #define OP_PREINCREMENT 51
783 #define OP_PREDECREMENT 52
786 /*#define OP_COMPLEMENT 55*/
788 #define OP_SUBSCRIPT 57
791 #define OP_POSTINCREMENT 60
792 #define OP_POSTDECREMENT 61
796 * When parsing a compound production, this function is used to parse the
798 * For example, a a while-loop compound will have two children, the
799 * while condition expression and the loop body. So, this function will
800 * be called twice to parse those two sub-expressions.
801 * \param C the parsing context
802 * \param O the output context
803 * \param oper the operation we're parsing
804 * \param statement indicates whether parsing a statement, or expression
805 * \return 1 if success, 0 if error
808 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
809 slang_operation
* oper
, GLboolean statement
)
813 /* grow child array */
814 ch
= slang_operation_grow(&oper
->num_children
, &oper
->children
);
816 return parse_statement(C
, O
, ch
);
817 return parse_expression(C
, O
, ch
);
820 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
823 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
824 slang_operation
* oper
)
826 oper
->locals
->outer_scope
= O
->vars
;
828 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
829 /* parse child statements, do not create new variable scope */
830 oper
->type
= slang_oper_block_no_new_scope
;
831 while (*C
->I
!= OP_END
)
832 if (!parse_child_operation(C
, O
, oper
, 1))
836 case OP_BLOCK_BEGIN_NEW_SCOPE
:
837 /* parse child statements, create new variable scope */
839 slang_output_ctx o
= *O
;
841 oper
->type
= slang_oper_block_new_scope
;
842 o
.vars
= oper
->locals
;
843 while (*C
->I
!= OP_END
)
844 if (!parse_child_operation(C
, &o
, oper
, 1))
850 /* local variable declaration, individual declarators are stored as
851 * children identifiers
853 oper
->type
= slang_oper_block_no_new_scope
;
855 const unsigned int first_var
= O
->vars
->num_variables
;
857 /* parse the declaration, note that there can be zero or more
858 * than one declarators
860 if (!parse_declaration(C
, O
))
862 if (first_var
< O
->vars
->num_variables
) {
863 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
866 oper
->num_children
= num_vars
;
867 oper
->children
= slang_operation_new(num_vars
);
868 if (oper
->children
== NULL
) {
869 slang_info_log_memory(C
->L
);
872 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
873 slang_operation
*o
= &oper
->children
[i
- first_var
];
874 o
->type
= slang_oper_variable_decl
;
875 o
->locals
->outer_scope
= O
->vars
;
876 o
->a_id
= O
->vars
->variables
[i
]->a_name
;
882 /* the __asm statement, parse the mnemonic and all its arguments
885 oper
->type
= slang_oper_asm
;
886 oper
->a_id
= parse_identifier(C
);
887 if (oper
->a_id
== SLANG_ATOM_NULL
)
889 while (*C
->I
!= OP_END
) {
890 if (!parse_child_operation(C
, O
, oper
, 0))
896 oper
->type
= slang_oper_break
;
899 oper
->type
= slang_oper_continue
;
902 oper
->type
= slang_oper_discard
;
905 oper
->type
= slang_oper_return
;
906 if (!parse_child_operation(C
, O
, oper
, 0))
910 oper
->type
= slang_oper_expression
;
911 if (!parse_child_operation(C
, O
, oper
, 0))
915 oper
->type
= slang_oper_if
;
916 if (!parse_child_operation(C
, O
, oper
, 0))
918 if (!parse_child_operation(C
, O
, oper
, 1))
920 if (!parse_child_operation(C
, O
, oper
, 1))
925 slang_output_ctx o
= *O
;
927 oper
->type
= slang_oper_while
;
928 o
.vars
= oper
->locals
;
929 if (!parse_child_operation(C
, &o
, oper
, 1))
931 if (!parse_child_operation(C
, &o
, oper
, 1))
936 oper
->type
= slang_oper_do
;
937 if (!parse_child_operation(C
, O
, oper
, 1))
939 if (!parse_child_operation(C
, O
, oper
, 0))
944 slang_output_ctx o
= *O
;
946 oper
->type
= slang_oper_for
;
947 o
.vars
= oper
->locals
;
948 if (!parse_child_operation(C
, &o
, oper
, 1))
950 if (!parse_child_operation(C
, &o
, oper
, 1))
952 if (!parse_child_operation(C
, &o
, oper
, 0))
954 if (!parse_child_operation(C
, &o
, oper
, 1))
965 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
966 slang_operation
** ops
, unsigned int *total_ops
,
972 (slang_operation
*) slang_alloc_malloc(n
* sizeof(slang_operation
));
973 if (op
->children
== NULL
) {
974 slang_info_log_memory(C
->L
);
977 op
->num_children
= n
;
979 for (i
= 0; i
< n
; i
++)
980 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
981 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
984 *ops
= (slang_operation
*)
985 slang_alloc_realloc(*ops
,
986 (*total_ops
+ n
) * sizeof(slang_operation
),
987 *total_ops
* sizeof(slang_operation
));
989 slang_info_log_memory(C
->L
);
996 is_constructor_name(const char *name
, slang_atom a_name
,
997 slang_struct_scope
* structs
)
999 if (slang_type_specifier_type_from_string(name
) != slang_spec_void
)
1001 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
1005 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1006 slang_operation
* oper
)
1008 slang_operation
*ops
= NULL
;
1009 unsigned int num_ops
= 0;
1012 while (*C
->I
!= OP_END
) {
1013 slang_operation
*op
;
1014 const unsigned int op_code
= *C
->I
++;
1016 /* allocate default operation, becomes a no-op if not used */
1017 ops
= (slang_operation
*)
1018 slang_alloc_realloc(ops
,
1019 num_ops
* sizeof(slang_operation
),
1020 (num_ops
+ 1) * sizeof(slang_operation
));
1022 slang_info_log_memory(C
->L
);
1026 if (!slang_operation_construct(op
)) {
1027 slang_info_log_memory(C
->L
);
1031 op
->locals
->outer_scope
= O
->vars
;
1035 op
->type
= slang_oper_void
;
1038 op
->type
= slang_oper_literal_bool
;
1039 if (!parse_number(C
, &number
))
1044 op
->literal
[3] = (GLfloat
) number
;
1047 op
->type
= slang_oper_literal_int
;
1048 if (!parse_number(C
, &number
))
1053 op
->literal
[3] = (GLfloat
) number
;
1056 op
->type
= slang_oper_literal_float
;
1057 if (!parse_float(C
, &op
->literal
[0]))
1061 op
->literal
[3] = op
->literal
[0];
1063 case OP_PUSH_IDENTIFIER
:
1064 op
->type
= slang_oper_identifier
;
1065 op
->a_id
= parse_identifier(C
);
1066 if (op
->a_id
== SLANG_ATOM_NULL
)
1070 op
->type
= slang_oper_sequence
;
1071 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1075 op
->type
= slang_oper_assign
;
1076 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1080 op
->type
= slang_oper_addassign
;
1081 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1085 op
->type
= slang_oper_subassign
;
1086 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1090 op
->type
= slang_oper_mulassign
;
1091 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1095 op
->type
= slang_oper_divassign
;
1096 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1099 /*case OP_MODASSIGN: */
1100 /*case OP_LSHASSIGN: */
1101 /*case OP_RSHASSIGN: */
1102 /*case OP_ORASSIGN: */
1103 /*case OP_XORASSIGN: */
1104 /*case OP_ANDASSIGN: */
1106 op
->type
= slang_oper_select
;
1107 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1111 op
->type
= slang_oper_logicalor
;
1112 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1116 op
->type
= slang_oper_logicalxor
;
1117 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1121 op
->type
= slang_oper_logicaland
;
1122 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1126 /*case OP_BITXOR: */
1127 /*case OP_BITAND: */
1129 op
->type
= slang_oper_equal
;
1130 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1134 op
->type
= slang_oper_notequal
;
1135 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1139 op
->type
= slang_oper_less
;
1140 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1144 op
->type
= slang_oper_greater
;
1145 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1149 op
->type
= slang_oper_lessequal
;
1150 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1153 case OP_GREATEREQUAL
:
1154 op
->type
= slang_oper_greaterequal
;
1155 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1158 /*case OP_LSHIFT: */
1159 /*case OP_RSHIFT: */
1161 op
->type
= slang_oper_add
;
1162 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1166 op
->type
= slang_oper_subtract
;
1167 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1171 op
->type
= slang_oper_multiply
;
1172 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1176 op
->type
= slang_oper_divide
;
1177 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1180 /*case OP_MODULUS: */
1181 case OP_PREINCREMENT
:
1182 op
->type
= slang_oper_preincrement
;
1183 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1186 case OP_PREDECREMENT
:
1187 op
->type
= slang_oper_predecrement
;
1188 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1192 op
->type
= slang_oper_plus
;
1193 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1197 op
->type
= slang_oper_minus
;
1198 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1202 op
->type
= slang_oper_not
;
1203 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1206 /*case OP_COMPLEMENT: */
1208 op
->type
= slang_oper_subscript
;
1209 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1213 op
->type
= slang_oper_call
;
1214 op
->a_id
= parse_identifier(C
);
1215 if (op
->a_id
== SLANG_ATOM_NULL
)
1217 while (*C
->I
!= OP_END
)
1218 if (!parse_child_operation(C
, O
, op
, 0))
1222 if (!C
->parsing_builtin
1223 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1226 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1227 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1228 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1234 op
->type
= slang_oper_field
;
1235 op
->a_id
= parse_identifier(C
);
1236 if (op
->a_id
== SLANG_ATOM_NULL
)
1238 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1241 case OP_POSTINCREMENT
:
1242 op
->type
= slang_oper_postincrement
;
1243 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1246 case OP_POSTDECREMENT
:
1247 op
->type
= slang_oper_postdecrement
;
1248 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1258 slang_alloc_free(ops
);
1263 /* parameter qualifier */
1264 #define PARAM_QUALIFIER_IN 0
1265 #define PARAM_QUALIFIER_OUT 1
1266 #define PARAM_QUALIFIER_INOUT 2
1268 /* function parameter array presence */
1269 #define PARAMETER_ARRAY_NOT_PRESENT 0
1270 #define PARAMETER_ARRAY_PRESENT 1
1273 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1274 slang_variable
* param
)
1276 /* parse and validate the parameter's type qualifiers (there can be
1277 * two at most) because not all combinations are valid
1279 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1282 case PARAM_QUALIFIER_IN
:
1283 if (param
->type
.qualifier
!= slang_qual_const
1284 && param
->type
.qualifier
!= slang_qual_none
) {
1285 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1289 case PARAM_QUALIFIER_OUT
:
1290 if (param
->type
.qualifier
== slang_qual_none
)
1291 param
->type
.qualifier
= slang_qual_out
;
1293 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1297 case PARAM_QUALIFIER_INOUT
:
1298 if (param
->type
.qualifier
== slang_qual_none
)
1299 param
->type
.qualifier
= slang_qual_inout
;
1301 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1309 /* parse parameter's type specifier and name */
1310 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1312 param
->a_name
= parse_identifier(C
);
1313 if (param
->a_name
== SLANG_ATOM_NULL
)
1316 /* if the parameter is an array, parse its size (the size must be
1317 * explicitly defined
1319 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1320 slang_type_specifier p
;
1322 slang_type_specifier_ctr(&p
);
1323 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1324 slang_type_specifier_dtr(&p
);
1327 if (!convert_to_array(C
, param
, &p
)) {
1328 slang_type_specifier_dtr(&p
);
1331 slang_type_specifier_dtr(&p
);
1332 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1336 /* calculate the parameter size */
1337 if (!calculate_var_size(C
, O
, param
))
1340 /* TODO: allocate the local address here? */
1345 #define FUNCTION_ORDINARY 0
1346 #define FUNCTION_CONSTRUCTOR 1
1347 #define FUNCTION_OPERATOR 2
1349 /* function parameter */
1350 #define PARAMETER_NONE 0
1351 #define PARAMETER_NEXT 1
1354 #define OPERATOR_ADDASSIGN 1
1355 #define OPERATOR_SUBASSIGN 2
1356 #define OPERATOR_MULASSIGN 3
1357 #define OPERATOR_DIVASSIGN 4
1358 /*#define OPERATOR_MODASSIGN 5*/
1359 /*#define OPERATOR_LSHASSIGN 6*/
1360 /*#define OPERATOR_RSHASSIGN 7*/
1361 /*#define OPERATOR_ANDASSIGN 8*/
1362 /*#define OPERATOR_XORASSIGN 9*/
1363 /*#define OPERATOR_ORASSIGN 10*/
1364 #define OPERATOR_LOGICALXOR 11
1365 /*#define OPERATOR_BITOR 12*/
1366 /*#define OPERATOR_BITXOR 13*/
1367 /*#define OPERATOR_BITAND 14*/
1368 #define OPERATOR_LESS 15
1369 #define OPERATOR_GREATER 16
1370 #define OPERATOR_LESSEQUAL 17
1371 #define OPERATOR_GREATEREQUAL 18
1372 /*#define OPERATOR_LSHIFT 19*/
1373 /*#define OPERATOR_RSHIFT 20*/
1374 #define OPERATOR_MULTIPLY 21
1375 #define OPERATOR_DIVIDE 22
1376 /*#define OPERATOR_MODULUS 23*/
1377 #define OPERATOR_INCREMENT 24
1378 #define OPERATOR_DECREMENT 25
1379 #define OPERATOR_PLUS 26
1380 #define OPERATOR_MINUS 27
1381 /*#define OPERATOR_COMPLEMENT 28*/
1382 #define OPERATOR_NOT 29
1386 unsigned int o_code
;
1388 } operator_names
[] = {
1389 {OPERATOR_INCREMENT
, "++"},
1390 {OPERATOR_ADDASSIGN
, "+="},
1391 {OPERATOR_PLUS
, "+"},
1392 {OPERATOR_DECREMENT
, "--"},
1393 {OPERATOR_SUBASSIGN
, "-="},
1394 {OPERATOR_MINUS
, "-"},
1395 {OPERATOR_NOT
, "!"},
1396 {OPERATOR_MULASSIGN
, "*="},
1397 {OPERATOR_MULTIPLY
, "*"},
1398 {OPERATOR_DIVASSIGN
, "/="},
1399 {OPERATOR_DIVIDE
, "/"},
1400 {OPERATOR_LESSEQUAL
, "<="},
1401 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1402 /*{ OPERATOR_LSHIFT, "<<" }, */
1403 {OPERATOR_LESS
, "<"},
1404 {OPERATOR_GREATEREQUAL
, ">="},
1405 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1406 /*{ OPERATOR_RSHIFT, ">>" }, */
1407 {OPERATOR_GREATER
, ">"},
1408 /*{ OPERATOR_MODASSIGN, "%=" }, */
1409 /*{ OPERATOR_MODULUS, "%" }, */
1410 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1411 /*{ OPERATOR_BITAND, "&" }, */
1412 /*{ OPERATOR_ORASSIGN, "|=" }, */
1413 /*{ OPERATOR_BITOR, "|" }, */
1414 /*{ OPERATOR_COMPLEMENT, "~" }, */
1415 /*{ OPERATOR_XORASSIGN, "^=" }, */
1416 {OPERATOR_LOGICALXOR
, "^^"},
1417 /*{ OPERATOR_BITXOR, "^" } */
1421 parse_operator_name(slang_parse_ctx
* C
)
1425 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1426 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1428 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1429 if (atom
== SLANG_ATOM_NULL
) {
1430 slang_info_log_memory(C
->L
);
1441 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1442 slang_function
* func
)
1444 /* parse function type and name */
1445 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1448 case FUNCTION_ORDINARY
:
1449 func
->kind
= slang_func_ordinary
;
1450 func
->header
.a_name
= parse_identifier(C
);
1451 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1454 case FUNCTION_CONSTRUCTOR
:
1455 func
->kind
= slang_func_constructor
;
1456 if (func
->header
.type
.specifier
.type
== slang_spec_struct
)
1458 func
->header
.a_name
=
1459 slang_atom_pool_atom(C
->atoms
,
1460 slang_type_specifier_type_to_string
1461 (func
->header
.type
.specifier
.type
));
1462 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1463 slang_info_log_memory(C
->L
);
1467 case FUNCTION_OPERATOR
:
1468 func
->kind
= slang_func_operator
;
1469 func
->header
.a_name
= parse_operator_name(C
);
1470 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1477 /* parse function parameters */
1478 while (*C
->I
++ == PARAMETER_NEXT
) {
1479 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1481 slang_info_log_memory(C
->L
);
1484 if (!parse_parameter_declaration(C
, O
, p
))
1489 /* if the function returns a value, append a hidden __retVal 'out'
1490 * parameter that corresponds to the return value.
1492 if (_slang_function_has_return_value(func
)) {
1493 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1494 slang_atom a_retVal
= slang_atom_pool_atom(C
->atoms
, "__retVal");
1496 p
->a_name
= a_retVal
;
1497 p
->type
= func
->header
.type
;
1498 p
->type
.qualifier
= slang_qual_out
;
1502 /* function formal parameters and local variables share the same
1503 * scope, so save the information about param count in a seperate
1504 * place also link the scope to the global variable scope so when a
1505 * given identifier is not found here, the search process continues
1506 * in the global space
1508 func
->param_count
= func
->parameters
->num_variables
;
1509 func
->parameters
->outer_scope
= O
->vars
;
1515 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1516 slang_function
* func
)
1518 slang_output_ctx o
= *O
;
1520 if (!parse_function_prototype(C
, O
, func
))
1523 /* create function's body operation */
1525 (slang_operation
*) slang_alloc_malloc(sizeof(slang_operation
));
1526 if (func
->body
== NULL
) {
1527 slang_info_log_memory(C
->L
);
1530 if (!slang_operation_construct(func
->body
)) {
1531 slang_alloc_free(func
->body
);
1533 slang_info_log_memory(C
->L
);
1537 /* to parse the body the parse context is modified in order to
1538 * capture parsed variables into function's local variable scope
1540 C
->global_scope
= GL_FALSE
;
1541 o
.vars
= func
->parameters
;
1542 if (!parse_statement(C
, &o
, func
->body
))
1545 C
->global_scope
= GL_TRUE
;
1550 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1553 slang_assembly_file_restore_point point
;
1556 slang_assembly_local_info save_local
= A
->local
;
1557 slang_operation op_id
, op_assign
;
1561 /* save the current assembly */
1562 if (!slang_assembly_file_restore_point_save(A
->file
, &point
))
1566 /* setup the machine */
1568 mach
.ip
= A
->file
->count
;
1570 /* allocate local storage for expression */
1571 A
->local
.ret_size
= 0;
1572 A
->local
.addr_tmp
= 0;
1573 A
->local
.swizzle_tmp
= 4;
1574 if (!slang_assembly_file_push_label(A
->file
, slang_asm_local_alloc
, 20))
1576 if (!slang_assembly_file_push_label(A
->file
, slang_asm_enter
, 20))
1579 /* construct the left side of assignment */
1580 if (!slang_operation_construct(&op_id
))
1582 op_id
.type
= slang_oper_identifier
;
1583 op_id
.a_id
= var
->a_name
;
1585 /* put the variable into operation's scope */
1586 op_id
.locals
->variables
=
1587 (slang_variable
**) slang_alloc_malloc(sizeof(slang_variable
*));
1588 if (op_id
.locals
->variables
== NULL
) {
1589 slang_operation_destruct(&op_id
);
1592 op_id
.locals
->num_variables
= 1;
1593 op_id
.locals
->variables
[0] = var
;
1595 /* construct the assignment expression */
1596 if (!slang_operation_construct(&op_assign
)) {
1597 op_id
.locals
->num_variables
= 0;
1598 slang_operation_destruct(&op_id
);
1601 op_assign
.type
= slang_oper_assign
;
1602 op_assign
.children
=
1603 (slang_operation
*) slang_alloc_malloc(2 * sizeof(slang_operation
));
1604 if (op_assign
.children
== NULL
) {
1605 slang_operation_destruct(&op_assign
);
1606 op_id
.locals
->num_variables
= 0;
1607 slang_operation_destruct(&op_id
);
1610 op_assign
.num_children
= 2;
1611 op_assign
.children
[0] = op_id
;
1612 op_assign
.children
[1] = *var
->initializer
;
1614 #if 0 /* this should go away */
1615 /* insert the actual expression */
1616 result
= _slang_assemble_operation(A
, &op_assign
, slang_ref_forbid
);
1621 /* carefully destroy the operations */
1622 op_assign
.num_children
= 0;
1623 slang_alloc_free(op_assign
.children
);
1624 op_assign
.children
= NULL
;
1625 slang_operation_destruct(&op_assign
);
1626 op_id
.locals
->num_variables
= 0;
1627 slang_operation_destruct(&op_id
);
1631 if (!slang_assembly_file_push(A
->file
, slang_asm_exit
))
1634 /* execute the expression */
1636 if (!_slang_execute2(A
->file
, &mach
))
1641 /* restore the old assembly */
1642 if (!slang_assembly_file_restore_point_load(A
->file
, &point
))
1645 A
->local
= save_local
;
1647 /* now we copy the contents of the initialized variable back to the original machine */
1648 _mesa_memcpy((GLubyte
*) A
->mach
->mem
+ var
->address
,
1649 (GLubyte
*) mach
.mem
+ var
->address
, var
->size
);
1654 /* init declarator list */
1655 #define DECLARATOR_NONE 0
1656 #define DECLARATOR_NEXT 1
1658 /* variable declaration */
1659 #define VARIABLE_NONE 0
1660 #define VARIABLE_IDENTIFIER 1
1661 #define VARIABLE_INITIALIZER 2
1662 #define VARIABLE_ARRAY_EXPLICIT 3
1663 #define VARIABLE_ARRAY_UNKNOWN 4
1667 * Parse the initializer for a variable declaration.
1670 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1671 const slang_fully_specified_type
* type
)
1673 slang_variable
*var
;
1675 /* empty init declatator (without name, e.g. "float ;") */
1676 if (*C
->I
++ == VARIABLE_NONE
)
1679 /* make room for the new variable and initialize it */
1680 var
= slang_variable_scope_grow(O
->vars
);
1682 slang_info_log_memory(C
->L
);
1686 /* copy the declarator qualifier type, parse the identifier */
1687 var
->global
= C
->global_scope
;
1688 var
->type
.qualifier
= type
->qualifier
;
1689 var
->a_name
= parse_identifier(C
);
1690 if (var
->a_name
== SLANG_ATOM_NULL
)
1695 /* simple variable declarator - just copy the specifier */
1696 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1699 case VARIABLE_INITIALIZER
:
1700 /* initialized variable - copy the specifier and parse the expression */
1701 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1704 (slang_operation
*) slang_alloc_malloc(sizeof(slang_operation
));
1705 if (var
->initializer
== NULL
) {
1706 slang_info_log_memory(C
->L
);
1709 if (!slang_operation_construct(var
->initializer
)) {
1710 slang_alloc_free(var
->initializer
);
1711 var
->initializer
= NULL
;
1712 slang_info_log_memory(C
->L
);
1715 if (!parse_expression(C
, O
, var
->initializer
))
1719 case VARIABLE_ARRAY_UNKNOWN
:
1720 /* unsized array - mark it as array and copy the specifier to
1723 if (!convert_to_array(C
, var
, &type
->specifier
))
1727 case VARIABLE_ARRAY_EXPLICIT
:
1728 if (!convert_to_array(C
, var
, &type
->specifier
))
1730 if (!parse_array_len(C
, O
, &var
->array_len
))
1738 if (C
->global_scope
) {
1739 slang_assemble_ctx A
;
1741 A
.file
= O
->assembly
;
1742 A
.mach
= O
->machine
;
1744 A
.space
.funcs
= O
->funs
;
1745 A
.space
.structs
= O
->structs
;
1746 A
.space
.vars
= O
->vars
;
1747 A
.program
= O
->program
;
1749 A
.codegen
= O
->codegen
;
1751 A
.vartable
= O
->vartable
;
1753 _slang_codegen_global_variable(&A
, var
, C
->type
);
1757 /* allocate global address space for a variable with a known size */
1759 && !(var
->type
.specifier
.type
== slang_spec_array
1760 && var
->array_len
== 0)) {
1761 if (!calculate_var_size(C
, O
, var
))
1763 var
->address
= slang_var_pool_alloc(O
->global_pool
, var
->size
);
1766 /* initialize global variable */
1767 if (C
->global_scope
) {
1768 if (var
->initializer
!= NULL
) {
1769 slang_assemble_ctx A
;
1771 A
.file
= O
->assembly
;
1772 A
.mach
= O
->machine
;
1774 A
.space
.funcs
= O
->funs
;
1775 A
.space
.structs
= O
->structs
;
1776 A
.space
.vars
= O
->vars
;
1777 if (!initialize_global(&A
, var
))
1781 _mesa_memset((GLubyte
*) (O
->machine
->mem
) + var
->address
, 0,
1789 * Parse a list of variable declarations. Each variable may have an
1793 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1795 slang_fully_specified_type type
;
1797 /* parse the fully specified type, common to all declarators */
1798 if (!slang_fully_specified_type_construct(&type
))
1800 if (!parse_fully_specified_type(C
, O
, &type
)) {
1801 slang_fully_specified_type_destruct(&type
);
1805 /* parse declarators, pass-in the parsed type */
1807 if (!parse_init_declarator(C
, O
, &type
)) {
1808 slang_fully_specified_type_destruct(&type
);
1812 while (*C
->I
++ == DECLARATOR_NEXT
);
1814 slang_fully_specified_type_destruct(&type
);
1820 * Parse a function definition or declaration.
1821 * \param C parsing context
1822 * \param O output context
1823 * \param definition if non-zero expect a definition, else a declaration
1824 * \param parsed_func_ret returns the parsed function
1825 * \return GL_TRUE if success, GL_FALSE if failure
1828 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
1829 slang_function
** parsed_func_ret
)
1831 slang_function parsed_func
, *found_func
;
1833 /* parse function definition/declaration */
1834 if (!slang_function_construct(&parsed_func
))
1837 if (!parse_function_definition(C
, O
, &parsed_func
)) {
1838 slang_function_destruct(&parsed_func
);
1843 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
1844 slang_function_destruct(&parsed_func
);
1849 /* find a function with a prototype matching the parsed one - only
1850 * the current scope is being searched to allow built-in function
1853 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
1854 if (found_func
== NULL
) {
1855 /* New function, add it to the function list */
1856 O
->funs
->functions
=
1857 (slang_function
*) slang_alloc_realloc(O
->funs
->functions
,
1858 O
->funs
->num_functions
*
1859 sizeof(slang_function
),
1860 (O
->funs
->num_functions
+
1861 1) * sizeof(slang_function
));
1862 if (O
->funs
->functions
== NULL
) {
1863 slang_info_log_memory(C
->L
);
1864 slang_function_destruct(&parsed_func
);
1867 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
1868 O
->funs
->num_functions
++;
1870 /* return the newly parsed function */
1871 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
1874 /* previously defined or declared */
1875 /* TODO: check function return type qualifiers and specifiers */
1877 if (found_func
->body
!= NULL
) {
1878 slang_info_log_error(C
->L
, "%s: function already has a body.",
1879 slang_atom_pool_id(C
->atoms
,
1882 slang_function_destruct(&parsed_func
);
1886 /* destroy the existing function declaration and replace it
1887 * with the new one, remember to save the fixup table
1889 parsed_func
.fixups
= found_func
->fixups
;
1890 slang_fixup_table_init(&found_func
->fixups
);
1891 slang_function_destruct(found_func
);
1892 *found_func
= parsed_func
;
1895 /* another declaration of the same function prototype - ignore it */
1896 slang_function_destruct(&parsed_func
);
1899 /* return the found function */
1900 *parsed_func_ret
= found_func
;
1903 /* assemble the parsed function */
1905 slang_assemble_ctx A
;
1907 A
.file
= O
->assembly
;
1908 A
.mach
= O
->machine
;
1910 A
.space
.funcs
= O
->funs
;
1911 A
.space
.structs
= O
->structs
;
1912 A
.space
.vars
= O
->vars
;
1913 A
.program
= O
->program
;
1915 A
.codegen
= O
->codegen
;
1917 A
.vartable
= O
->vartable
;
1919 _slang_reset_error();
1922 printf("*************** Assemble function %s ****\n", (char *) (*parsed_func_ret
)->header
.a_name
);
1923 slang_print_var_scope((*parsed_func_ret
)->parameters
,
1924 (*parsed_func_ret
)->param_count
);
1928 if (!_slang_assemble_function(&A
, *parsed_func_ret
)) {
1929 /* propogate the error message back through the info log */
1930 C
->L
->text
= _mesa_strdup(_slang_error_text());
1931 C
->L
->dont_free_text
= GL_FALSE
;
1937 printf("**************************************\n");
1940 _slang_codegen_function(&A
, *parsed_func_ret
);
1947 #define DECLARATION_FUNCTION_PROTOTYPE 1
1948 #define DECLARATION_INIT_DECLARATOR_LIST 2
1951 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
1954 case DECLARATION_INIT_DECLARATOR_LIST
:
1955 if (!parse_init_declarator_list(C
, O
))
1958 case DECLARATION_FUNCTION_PROTOTYPE
:
1960 slang_function
*dummy_func
;
1962 if (!parse_function(C
, O
, 0, &dummy_func
))
1972 /* external declaration */
1973 #define EXTERNAL_NULL 0
1974 #define EXTERNAL_FUNCTION_DEFINITION 1
1975 #define EXTERNAL_DECLARATION 2
1978 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
,
1979 struct gl_program
*program
)
1981 GET_CURRENT_CONTEXT(ctx
);
1986 if (unit
->type
== slang_unit_fragment_builtin
||
1987 unit
->type
== slang_unit_fragment_shader
) {
1988 maxRegs
= ctx
->Const
.FragmentProgram
.MaxTemps
;
1991 assert(unit
->type
== slang_unit_vertex_builtin
||
1992 unit
->type
== slang_unit_vertex_shader
);
1993 maxRegs
= ctx
->Const
.VertexProgram
.MaxTemps
;
1996 /* setup output context */
1997 o
.funs
= &unit
->funs
;
1998 o
.structs
= &unit
->structs
;
1999 o
.vars
= &unit
->vars
;
2000 o
.assembly
= &unit
->object
->assembly
;
2001 o
.global_pool
= &unit
->object
->varpool
;
2002 o
.machine
= &unit
->object
->machine
;
2003 o
.program
= program
;
2004 o
.vartable
= _slang_new_var_table(maxRegs
);
2005 _slang_push_var_table(o
.vartable
);
2007 /* parse individual functions and declarations */
2008 while (*C
->I
!= EXTERNAL_NULL
) {
2010 case EXTERNAL_FUNCTION_DEFINITION
:
2012 slang_function
*func
;
2013 success
= parse_function(C
, &o
, 1, &func
);
2016 case EXTERNAL_DECLARATION
:
2017 success
= parse_declaration(C
, &o
);
2024 /* xxx free codegen */
2025 _slang_pop_var_table(o
.vartable
);
2031 _slang_pop_var_table(o
.vartable
);
2036 compile_binary(const byte
* prod
, slang_code_unit
* unit
,
2037 slang_unit_type type
, slang_info_log
* infolog
,
2038 slang_code_unit
* builtin
, slang_code_unit
* downlink
,
2039 struct gl_program
*program
)
2045 /* setup parse context */
2048 C
.parsing_builtin
= (builtin
== NULL
);
2049 C
.global_scope
= GL_TRUE
;
2050 C
.atoms
= &unit
->object
->atompool
;
2053 if (!check_revision(&C
))
2056 if (downlink
!= NULL
) {
2057 unit
->vars
.outer_scope
= &downlink
->vars
;
2058 unit
->funs
.outer_scope
= &downlink
->funs
;
2059 unit
->structs
.outer_scope
= &downlink
->structs
;
2062 /* parse translation unit */
2063 return parse_code_unit(&C
, unit
, program
);
2067 compile_with_grammar(grammar id
, const char *source
, slang_code_unit
* unit
,
2068 slang_unit_type type
, slang_info_log
* infolog
,
2069 slang_code_unit
* builtin
,
2070 struct gl_program
*program
)
2073 GLuint size
, start
, version
;
2074 slang_string preprocessed
;
2076 /* First retrieve the version number. */
2077 if (!_slang_preprocess_version(source
, &version
, &start
, infolog
))
2080 if (version
> 110) {
2081 slang_info_log_error(infolog
,
2082 "language version specified is not supported.");
2086 /* Now preprocess the source string. */
2087 slang_string_init(&preprocessed
);
2088 if (!_slang_preprocess_directives(&preprocessed
, &source
[start
], infolog
)) {
2089 slang_string_free(&preprocessed
);
2090 slang_info_log_error(infolog
, "failed to preprocess the source.");
2094 /* Finally check the syntax and generate its binary representation. */
2095 if (!grammar_fast_check(id
,
2096 (const byte
*) (slang_string_cstr(&preprocessed
)),
2102 slang_string_free(&preprocessed
);
2103 grammar_get_last_error((byte
*) (buf
), sizeof(buf
), &pos
);
2104 slang_info_log_error(infolog
, buf
);
2105 RETURN_ERROR("syntax error (possibly in library code)", 0);
2107 slang_string_free(&preprocessed
);
2109 /* Syntax is okay - translate it to internal representation. */
2110 if (!compile_binary(prod
, unit
, type
, infolog
, builtin
,
2111 &builtin
[SLANG_BUILTIN_TOTAL
- 1],
2113 grammar_alloc_free(prod
);
2116 grammar_alloc_free(prod
);
2120 LONGSTRING
static const char *slang_shader_syn
=
2121 #include "library/slang_shader_syn.h"
2124 static const byte slang_core_gc
[] = {
2125 #include "library/slang_core_gc.h"
2128 static const byte slang_common_builtin_gc
[] = {
2129 #include "library/slang_common_builtin_gc.h"
2132 static const byte slang_fragment_builtin_gc
[] = {
2133 #include "library/slang_fragment_builtin_gc.h"
2136 static const byte slang_vertex_builtin_gc
[] = {
2137 #include "library/slang_vertex_builtin_gc.h"
2140 #if 0 /*defined(USE_X86_ASM) || defined(SLANG_X86)*/
2141 static const byte slang_builtin_vec4_gc
[] = {
2142 #include "library/slang_builtin_vec4_gc.h"
2147 compile_object(grammar
* id
, const char *source
, slang_code_object
* object
,
2148 slang_unit_type type
, slang_info_log
* infolog
,
2149 struct gl_program
*program
)
2151 slang_code_unit
*builtins
= NULL
;
2153 /* load GLSL grammar */
2154 *id
= grammar_load_from_text((const byte
*) (slang_shader_syn
));
2159 grammar_get_last_error(buf
, 1024, &pos
);
2160 slang_info_log_error(infolog
, (const char *) (buf
));
2164 /* set shader type - the syntax is slightly different for different shaders */
2165 if (type
== slang_unit_fragment_shader
2166 || type
== slang_unit_fragment_builtin
)
2167 grammar_set_reg8(*id
, (const byte
*) "shader_type", 1);
2169 grammar_set_reg8(*id
, (const byte
*) "shader_type", 2);
2171 /* enable language extensions */
2172 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2174 /* if parsing user-specified shader, load built-in library */
2175 if (type
== slang_unit_fragment_shader
|| type
== slang_unit_vertex_shader
) {
2176 /* compile core functionality first */
2177 if (!compile_binary(slang_core_gc
,
2178 &object
->builtin
[SLANG_BUILTIN_CORE
],
2179 slang_unit_fragment_builtin
, infolog
,
2183 /* compile common functions and variables, link to core */
2184 if (!compile_binary(slang_common_builtin_gc
,
2185 &object
->builtin
[SLANG_BUILTIN_COMMON
],
2186 slang_unit_fragment_builtin
, infolog
, NULL
,
2187 &object
->builtin
[SLANG_BUILTIN_CORE
], NULL
))
2190 /* compile target-specific functions and variables, link to common */
2191 if (type
== slang_unit_fragment_shader
) {
2192 if (!compile_binary(slang_fragment_builtin_gc
,
2193 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2194 slang_unit_fragment_builtin
, infolog
, NULL
,
2195 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2198 else if (type
== slang_unit_vertex_shader
) {
2199 if (!compile_binary(slang_vertex_builtin_gc
,
2200 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2201 slang_unit_vertex_builtin
, infolog
, NULL
,
2202 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2206 #if 0/*defined(USE_X86_ASM) || defined(SLANG_X86)*/
2207 /* compile x86 4-component vector overrides, link to target */
2208 if (!compile_binary(slang_builtin_vec4_gc
,
2209 &object
->builtin
[SLANG_BUILTIN_VEC4
],
2210 slang_unit_fragment_builtin
, infolog
, NULL
,
2211 &object
->builtin
[SLANG_BUILTIN_TARGET
]))
2215 /* disable language extensions */
2216 #if NEW_SLANG /* allow-built-ins */
2217 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2219 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 0);
2221 builtins
= object
->builtin
;
2224 /* compile the actual shader - pass-in built-in library for external shader */
2225 return compile_with_grammar(*id
, source
, &object
->unit
, type
, infolog
,
2232 slang_create_uniforms(const slang_export_data_table
*exports
,
2233 struct gl_program
*program
)
2235 /* XXX only add uniforms that are actually going to get used */
2237 for (i
= 0; i
< exports
->count
; i
++) {
2238 if (exports
->entries
[i
].access
== slang_exp_uniform
) {
2239 const char *name
= (char *) exports
->entries
[i
].quant
.name
;
2240 GLint j
= _mesa_add_uniform(program
->Parameters
, name
, 4);
2249 compile_shader(GLcontext
*ctx
, slang_code_object
* object
,
2250 slang_unit_type type
, slang_info_log
* infolog
,
2251 struct gl_shader
*shader
)
2253 struct gl_program
*program
= shader
->Programs
[0];
2259 _slang_code_object_dtr(object
);
2260 _slang_code_object_ctr(object
);
2262 success
= compile_object(&id
, shader
->Source
, object
, type
, infolog
, program
);
2264 grammar_destroy(id
);
2268 if (!_slang_build_export_data_table(&object
->expdata
, &object
->unit
.vars
))
2270 if (!_slang_build_export_code_table(&object
->expcode
, &object
->unit
.funs
,
2276 slang_create_uniforms(&object
->expdata
, shader
);
2277 _mesa_print_program(program
);
2278 _mesa_print_program_parameters(ctx
, program
);
2288 _slang_compile(GLcontext
*ctx
, struct gl_shader
*shader
)
2291 slang_info_log info_log
;
2292 slang_code_object obj
;
2293 slang_unit_type type
;
2295 if (shader
->Type
== GL_VERTEX_SHADER
) {
2296 type
= slang_unit_vertex_shader
;
2299 assert(shader
->Type
== GL_FRAGMENT_SHADER
);
2300 type
= slang_unit_fragment_shader
;
2303 /* XXX temporary hack */
2304 if (!shader
->Programs
) {
2306 if (shader
->Type
== GL_VERTEX_SHADER
)
2307 progTarget
= GL_VERTEX_PROGRAM_ARB
;
2309 progTarget
= GL_FRAGMENT_PROGRAM_ARB
;
2311 = (struct gl_program
**) malloc(sizeof(struct gl_program
*));
2312 shader
->Programs
[0] = _mesa_new_program(ctx
, progTarget
, 1);
2313 shader
->NumPrograms
= 1;
2315 shader
->Programs
[0]->Parameters
= _mesa_new_parameter_list();
2316 shader
->Programs
[0]->Varying
= _mesa_new_parameter_list();
2317 shader
->Programs
[0]->Attributes
= _mesa_new_parameter_list();
2320 slang_info_log_construct(&info_log
);
2321 _slang_code_object_ctr(&obj
);
2323 success
= compile_shader(ctx
, &obj
, type
, &info_log
, shader
);
2327 slang_create_uniforms(&object
->expdata
, shader
);
2328 _mesa_print_program(program
);
2329 _mesa_print_program_parameters(ctx
, program
);
2333 /* XXX more work on info log needed here */
2334 if (info_log
.text
) {
2335 if (shader
->InfoLog
) {
2336 free(shader
->InfoLog
);
2337 shader
->InfoLog
= NULL
;
2339 shader
->InfoLog
= strdup(info_log
.text
);
2343 slang_info_log_destruct(&info_log
);
2344 _slang_code_object_dtr(&obj
);