2 * Mesa 3-D graphics library
4 * Copyright (C) 2005-2006 Brian Paul All Rights Reserved.
5 * Copyright (C) 2008 VMware, Inc. 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
31 #include "main/imports.h"
32 #include "main/context.h"
33 #include "shader/program.h"
34 #include "shader/programopt.h"
35 #include "shader/prog_optimize.h"
36 #include "shader/prog_print.h"
37 #include "shader/prog_parameter.h"
38 #include "shader/grammar/grammar_mesa.h"
39 #include "slang_codegen.h"
40 #include "slang_compile.h"
41 #include "slang_preprocess.h"
42 #include "slang_storage.h"
43 #include "slang_log.h"
44 #include "slang_mem.h"
45 #include "slang_vartable.h"
46 #include "slang_simplify.h"
49 * This is a straightforward implementation of the slang front-end
50 * compiler. Lots of error-checking functionality is missing but
51 * every well-formed shader source should compile successfully and
52 * execute as expected. However, some semantically ill-formed shaders
53 * may be accepted resulting in undefined behaviour.
57 /** re-defined below, should be the same though */
58 #define TYPE_SPECIFIER_COUNT 32
62 * Check if the given identifier is legal.
65 legal_identifier(slang_atom name
)
67 /* "gl_" is a reserved prefix */
68 if (_mesa_strncmp((char *) name
, "gl_", 3) == 0) {
80 _slang_code_unit_ctr(slang_code_unit
* self
,
81 struct slang_code_object_
* object
)
83 _slang_variable_scope_ctr(&self
->vars
);
84 _slang_function_scope_ctr(&self
->funs
);
85 _slang_struct_scope_ctr(&self
->structs
);
86 self
->object
= object
;
90 _slang_code_unit_dtr(slang_code_unit
* self
)
92 slang_variable_scope_destruct(&self
->vars
);
93 slang_function_scope_destruct(&self
->funs
);
94 slang_struct_scope_destruct(&self
->structs
);
102 _slang_code_object_ctr(slang_code_object
* self
)
106 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
107 _slang_code_unit_ctr(&self
->builtin
[i
], self
);
108 _slang_code_unit_ctr(&self
->unit
, self
);
109 slang_atom_pool_construct(&self
->atompool
);
113 _slang_code_object_dtr(slang_code_object
* self
)
117 for (i
= 0; i
< SLANG_BUILTIN_TOTAL
; i
++)
118 _slang_code_unit_dtr(&self
->builtin
[i
]);
119 _slang_code_unit_dtr(&self
->unit
);
120 slang_atom_pool_destruct(&self
->atompool
);
124 /* slang_parse_ctx */
126 typedef struct slang_parse_ctx_
131 GLboolean global_scope
; /**< Is object being declared a global? */
132 slang_atom_pool
*atoms
;
133 slang_unit_type type
; /**< Vertex vs. Fragment */
134 GLuint version
; /**< user-specified (or default) #version */
137 /* slang_output_ctx */
139 typedef struct slang_output_ctx_
141 slang_variable_scope
*vars
;
142 slang_function_scope
*funs
;
143 slang_struct_scope
*structs
;
144 struct gl_program
*program
;
145 struct gl_sl_pragmas
*pragmas
;
146 slang_var_table
*vartable
;
147 GLuint default_precision
[TYPE_SPECIFIER_COUNT
];
148 GLboolean allow_precision
;
149 GLboolean allow_invariant
;
150 GLboolean allow_centroid
;
151 GLboolean allow_array_types
; /* float[] syntax */
154 /* _slang_compile() */
157 /* Debugging aid, print file/line where parsing error is detected */
161 printf("slang error at %s:%d\n", __FILE__, __LINE__); \
167 parse_identifier_str(slang_parse_ctx
* C
, char **id
)
170 C
->I
+= _mesa_strlen(*id
) + 1;
174 parse_identifier(slang_parse_ctx
* C
)
178 id
= (const char *) C
->I
;
179 C
->I
+= _mesa_strlen(id
) + 1;
180 return slang_atom_pool_atom(C
->atoms
, id
);
184 parse_number(slang_parse_ctx
* C
, int *number
)
186 const int radix
= (int) (*C
->I
++);
188 while (*C
->I
!= '\0') {
190 if (*C
->I
>= '0' && *C
->I
<= '9')
191 digit
= (int) (*C
->I
- '0');
192 else if (*C
->I
>= 'A' && *C
->I
<= 'Z')
193 digit
= (int) (*C
->I
- 'A') + 10;
195 digit
= (int) (*C
->I
- 'a') + 10;
196 *number
= *number
* radix
+ digit
;
201 slang_info_log_warning(C
->L
, "%d: literal integer overflow.", *number
);
206 parse_float(slang_parse_ctx
* C
, float *number
)
208 char *integral
= NULL
;
209 char *fractional
= NULL
;
210 char *exponent
= NULL
;
213 parse_identifier_str(C
, &integral
);
214 parse_identifier_str(C
, &fractional
);
215 parse_identifier_str(C
, &exponent
);
217 whole
= (char *) _slang_alloc((_mesa_strlen(integral
) +
218 _mesa_strlen(fractional
) +
219 _mesa_strlen(exponent
) + 3) * sizeof(char));
221 slang_info_log_memory(C
->L
);
225 slang_string_copy(whole
, integral
);
226 slang_string_concat(whole
, ".");
227 slang_string_concat(whole
, fractional
);
228 slang_string_concat(whole
, "E");
229 slang_string_concat(whole
, exponent
);
231 *number
= (float) (_mesa_strtod(whole
, (char **) NULL
));
238 /* revision number - increment after each change affecting emitted output */
242 check_revision(slang_parse_ctx
* C
)
244 if (*C
->I
!= REVISION
) {
245 slang_info_log_error(C
->L
, "Internal compiler error.");
252 static int parse_statement(slang_parse_ctx
*, slang_output_ctx
*,
254 static int parse_expression(slang_parse_ctx
*, slang_output_ctx
*,
256 static int parse_type_specifier(slang_parse_ctx
*, slang_output_ctx
*,
257 slang_type_specifier
*);
259 parse_type_array_size(slang_parse_ctx
*C
,
264 parse_array_len(slang_parse_ctx
* C
, slang_output_ctx
* O
, GLuint
* len
)
266 slang_operation array_size
;
267 slang_name_space space
;
270 if (!slang_operation_construct(&array_size
))
272 if (!parse_expression(C
, O
, &array_size
)) {
273 slang_operation_destruct(&array_size
);
277 space
.funcs
= O
->funs
;
278 space
.structs
= O
->structs
;
279 space
.vars
= O
->vars
;
281 /* evaluate compile-time expression which is array size */
282 _slang_simplify(&array_size
, &space
, C
->atoms
);
284 if (array_size
.type
== SLANG_OPER_LITERAL_INT
) {
286 *len
= (GLint
) array_size
.literal
[0];
287 } else if (array_size
.type
== SLANG_OPER_IDENTIFIER
) {
288 slang_variable
*var
= _slang_variable_locate(array_size
.locals
, array_size
.a_id
, GL_TRUE
);
290 slang_info_log_error(C
->L
, "undefined variable '%s'",
291 (char *) array_size
.a_id
);
293 } else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&&
294 var
->type
.specifier
.type
== SLANG_SPEC_INT
) {
295 if (var
->initializer
&&
296 var
->initializer
->type
== SLANG_OPER_LITERAL_INT
) {
297 *len
= (GLint
) var
->initializer
->literal
[0];
300 slang_info_log_error(C
->L
, "unable to parse array size declaration");
304 slang_info_log_error(C
->L
, "unable to parse array size declaration");
311 slang_operation_destruct(&array_size
);
316 calculate_var_size(slang_parse_ctx
* C
, slang_output_ctx
* O
,
317 slang_variable
* var
)
319 slang_storage_aggregate agg
;
321 if (!slang_storage_aggregate_construct(&agg
))
323 if (!_slang_aggregate_variable(&agg
, &var
->type
.specifier
, var
->array_len
,
324 O
->funs
, O
->structs
, O
->vars
, C
->atoms
)) {
325 slang_storage_aggregate_destruct(&agg
);
328 var
->size
= _slang_sizeof_aggregate(&agg
);
329 slang_storage_aggregate_destruct(&agg
);
334 promote_type_to_array(slang_parse_ctx
*C
,
335 slang_fully_specified_type
*type
,
338 slang_type_specifier
*baseType
=
339 slang_type_specifier_new(type
->specifier
.type
, NULL
, NULL
);
341 type
->specifier
.type
= SLANG_SPEC_ARRAY
;
342 type
->specifier
._array
= baseType
;
343 type
->array_len
= array_len
;
348 convert_to_array(slang_parse_ctx
* C
, slang_variable
* var
,
349 const slang_type_specifier
* sp
)
351 /* sized array - mark it as array, copy the specifier to the array element
352 * and parse the expression */
353 var
->type
.specifier
.type
= SLANG_SPEC_ARRAY
;
354 var
->type
.specifier
._array
= (slang_type_specifier
*)
355 _slang_alloc(sizeof(slang_type_specifier
));
356 if (var
->type
.specifier
._array
== NULL
) {
357 slang_info_log_memory(C
->L
);
360 slang_type_specifier_ctr(var
->type
.specifier
._array
);
361 return slang_type_specifier_copy(var
->type
.specifier
._array
, sp
);
364 /* structure field */
367 #define FIELD_ARRAY 2
370 parse_struct_field_var(slang_parse_ctx
* C
, slang_output_ctx
* O
,
371 slang_variable
* var
, slang_atom a_name
,
372 const slang_type_specifier
* sp
,
375 var
->a_name
= a_name
;
376 if (var
->a_name
== SLANG_ATOM_NULL
)
381 if (array_len
!= -1) {
382 if (!convert_to_array(C
, var
, sp
))
384 var
->array_len
= array_len
;
387 if (!slang_type_specifier_copy(&var
->type
.specifier
, sp
))
394 if (!convert_to_array(C
, var
, sp
))
396 if (!parse_array_len(C
, O
, &var
->array_len
))
403 return calculate_var_size(C
, O
, var
);
407 parse_struct_field(slang_parse_ctx
* C
, slang_output_ctx
* O
,
408 slang_struct
* st
, slang_type_specifier
* sp
)
410 slang_output_ctx o
= *O
;
413 o
.structs
= st
->structs
;
414 if (!parse_type_specifier(C
, &o
, sp
))
416 if (!parse_type_array_size(C
, &o
, &array_len
))
421 slang_variable
*var
= slang_variable_scope_grow(st
->fields
);
423 slang_info_log_memory(C
->L
);
426 a_name
= parse_identifier(C
);
427 if (_slang_variable_locate(st
->fields
, a_name
, GL_FALSE
)) {
428 slang_info_log_error(C
->L
, "duplicate field '%s'", (char *) a_name
);
432 if (!parse_struct_field_var(C
, &o
, var
, a_name
, sp
, array_len
))
435 while (*C
->I
++ != FIELD_NONE
);
441 parse_struct(slang_parse_ctx
* C
, slang_output_ctx
* O
, slang_struct
** st
)
446 /* parse struct name (if any) and make sure it is unique in current scope */
447 a_name
= parse_identifier(C
);
448 if (a_name
== SLANG_ATOM_NULL
)
451 name
= slang_atom_pool_id(C
->atoms
, a_name
);
453 && slang_struct_scope_find(O
->structs
, a_name
, 0) != NULL
) {
454 slang_info_log_error(C
->L
, "%s: duplicate type name.", name
);
458 /* set-up a new struct */
459 *st
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
461 slang_info_log_memory(C
->L
);
464 if (!slang_struct_construct(*st
)) {
467 slang_info_log_memory(C
->L
);
470 (**st
).a_name
= a_name
;
471 (**st
).structs
->outer_scope
= O
->structs
;
473 /* parse individual struct fields */
475 slang_type_specifier sp
;
477 slang_type_specifier_ctr(&sp
);
478 if (!parse_struct_field(C
, O
, *st
, &sp
)) {
479 slang_type_specifier_dtr(&sp
);
482 slang_type_specifier_dtr(&sp
);
484 while (*C
->I
++ != FIELD_NONE
);
486 /* if named struct, copy it to current scope */
487 if (name
[0] != '\0') {
490 O
->structs
->structs
=
491 (slang_struct
*) _slang_realloc(O
->structs
->structs
,
492 O
->structs
->num_structs
493 * sizeof(slang_struct
),
494 (O
->structs
->num_structs
+ 1)
495 * sizeof(slang_struct
));
496 if (O
->structs
->structs
== NULL
) {
497 slang_info_log_memory(C
->L
);
500 s
= &O
->structs
->structs
[O
->structs
->num_structs
];
501 if (!slang_struct_construct(s
))
503 O
->structs
->num_structs
++;
504 if (!slang_struct_copy(s
, *st
))
512 /* invariant qualifer */
513 #define TYPE_VARIANT 90
514 #define TYPE_INVARIANT 91
517 parse_type_variant(slang_parse_ctx
* C
, slang_type_variant
*variant
)
519 GLuint invariant
= *C
->I
++;
522 *variant
= SLANG_VARIANT
;
525 *variant
= SLANG_INVARIANT
;
533 /* centroid qualifer */
534 #define TYPE_CENTER 95
535 #define TYPE_CENTROID 96
538 parse_type_centroid(slang_parse_ctx
* C
, slang_type_centroid
*centroid
)
543 *centroid
= SLANG_CENTER
;
546 *centroid
= SLANG_CENTROID
;
555 #define TYPE_QUALIFIER_NONE 0
556 #define TYPE_QUALIFIER_CONST 1
557 #define TYPE_QUALIFIER_ATTRIBUTE 2
558 #define TYPE_QUALIFIER_VARYING 3
559 #define TYPE_QUALIFIER_UNIFORM 4
560 #define TYPE_QUALIFIER_FIXEDOUTPUT 5
561 #define TYPE_QUALIFIER_FIXEDINPUT 6
564 parse_type_qualifier(slang_parse_ctx
* C
, slang_type_qualifier
* qual
)
566 GLuint qualifier
= *C
->I
++;
568 case TYPE_QUALIFIER_NONE
:
569 *qual
= SLANG_QUAL_NONE
;
571 case TYPE_QUALIFIER_CONST
:
572 *qual
= SLANG_QUAL_CONST
;
574 case TYPE_QUALIFIER_ATTRIBUTE
:
575 *qual
= SLANG_QUAL_ATTRIBUTE
;
577 case TYPE_QUALIFIER_VARYING
:
578 *qual
= SLANG_QUAL_VARYING
;
580 case TYPE_QUALIFIER_UNIFORM
:
581 *qual
= SLANG_QUAL_UNIFORM
;
583 case TYPE_QUALIFIER_FIXEDOUTPUT
:
584 *qual
= SLANG_QUAL_FIXEDOUTPUT
;
586 case TYPE_QUALIFIER_FIXEDINPUT
:
587 *qual
= SLANG_QUAL_FIXEDINPUT
;
596 #define TYPE_SPECIFIER_VOID 0
597 #define TYPE_SPECIFIER_BOOL 1
598 #define TYPE_SPECIFIER_BVEC2 2
599 #define TYPE_SPECIFIER_BVEC3 3
600 #define TYPE_SPECIFIER_BVEC4 4
601 #define TYPE_SPECIFIER_INT 5
602 #define TYPE_SPECIFIER_IVEC2 6
603 #define TYPE_SPECIFIER_IVEC3 7
604 #define TYPE_SPECIFIER_IVEC4 8
605 #define TYPE_SPECIFIER_FLOAT 9
606 #define TYPE_SPECIFIER_VEC2 10
607 #define TYPE_SPECIFIER_VEC3 11
608 #define TYPE_SPECIFIER_VEC4 12
609 #define TYPE_SPECIFIER_MAT2 13
610 #define TYPE_SPECIFIER_MAT3 14
611 #define TYPE_SPECIFIER_MAT4 15
612 #define TYPE_SPECIFIER_SAMPLER1D 16
613 #define TYPE_SPECIFIER_SAMPLER2D 17
614 #define TYPE_SPECIFIER_SAMPLER3D 18
615 #define TYPE_SPECIFIER_SAMPLERCUBE 19
616 #define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
617 #define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
618 #define TYPE_SPECIFIER_SAMPLER2DRECT 22
619 #define TYPE_SPECIFIER_SAMPLER2DRECTSHADOW 23
620 #define TYPE_SPECIFIER_STRUCT 24
621 #define TYPE_SPECIFIER_TYPENAME 25
622 #define TYPE_SPECIFIER_MAT23 26
623 #define TYPE_SPECIFIER_MAT32 27
624 #define TYPE_SPECIFIER_MAT24 28
625 #define TYPE_SPECIFIER_MAT42 29
626 #define TYPE_SPECIFIER_MAT34 30
627 #define TYPE_SPECIFIER_MAT43 31
628 #define TYPE_SPECIFIER_COUNT 32
631 parse_type_specifier(slang_parse_ctx
* C
, slang_output_ctx
* O
,
632 slang_type_specifier
* spec
)
635 case TYPE_SPECIFIER_VOID
:
636 spec
->type
= SLANG_SPEC_VOID
;
638 case TYPE_SPECIFIER_BOOL
:
639 spec
->type
= SLANG_SPEC_BOOL
;
641 case TYPE_SPECIFIER_BVEC2
:
642 spec
->type
= SLANG_SPEC_BVEC2
;
644 case TYPE_SPECIFIER_BVEC3
:
645 spec
->type
= SLANG_SPEC_BVEC3
;
647 case TYPE_SPECIFIER_BVEC4
:
648 spec
->type
= SLANG_SPEC_BVEC4
;
650 case TYPE_SPECIFIER_INT
:
651 spec
->type
= SLANG_SPEC_INT
;
653 case TYPE_SPECIFIER_IVEC2
:
654 spec
->type
= SLANG_SPEC_IVEC2
;
656 case TYPE_SPECIFIER_IVEC3
:
657 spec
->type
= SLANG_SPEC_IVEC3
;
659 case TYPE_SPECIFIER_IVEC4
:
660 spec
->type
= SLANG_SPEC_IVEC4
;
662 case TYPE_SPECIFIER_FLOAT
:
663 spec
->type
= SLANG_SPEC_FLOAT
;
665 case TYPE_SPECIFIER_VEC2
:
666 spec
->type
= SLANG_SPEC_VEC2
;
668 case TYPE_SPECIFIER_VEC3
:
669 spec
->type
= SLANG_SPEC_VEC3
;
671 case TYPE_SPECIFIER_VEC4
:
672 spec
->type
= SLANG_SPEC_VEC4
;
674 case TYPE_SPECIFIER_MAT2
:
675 spec
->type
= SLANG_SPEC_MAT2
;
677 case TYPE_SPECIFIER_MAT3
:
678 spec
->type
= SLANG_SPEC_MAT3
;
680 case TYPE_SPECIFIER_MAT4
:
681 spec
->type
= SLANG_SPEC_MAT4
;
683 case TYPE_SPECIFIER_MAT23
:
684 spec
->type
= SLANG_SPEC_MAT23
;
686 case TYPE_SPECIFIER_MAT32
:
687 spec
->type
= SLANG_SPEC_MAT32
;
689 case TYPE_SPECIFIER_MAT24
:
690 spec
->type
= SLANG_SPEC_MAT24
;
692 case TYPE_SPECIFIER_MAT42
:
693 spec
->type
= SLANG_SPEC_MAT42
;
695 case TYPE_SPECIFIER_MAT34
:
696 spec
->type
= SLANG_SPEC_MAT34
;
698 case TYPE_SPECIFIER_MAT43
:
699 spec
->type
= SLANG_SPEC_MAT43
;
701 case TYPE_SPECIFIER_SAMPLER1D
:
702 spec
->type
= SLANG_SPEC_SAMPLER1D
;
704 case TYPE_SPECIFIER_SAMPLER2D
:
705 spec
->type
= SLANG_SPEC_SAMPLER2D
;
707 case TYPE_SPECIFIER_SAMPLER3D
:
708 spec
->type
= SLANG_SPEC_SAMPLER3D
;
710 case TYPE_SPECIFIER_SAMPLERCUBE
:
711 spec
->type
= SLANG_SPEC_SAMPLERCUBE
;
713 case TYPE_SPECIFIER_SAMPLER2DRECT
:
714 spec
->type
= SLANG_SPEC_SAMPLER2DRECT
;
716 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
717 spec
->type
= SLANG_SPEC_SAMPLER1DSHADOW
;
719 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
720 spec
->type
= SLANG_SPEC_SAMPLER2DSHADOW
;
722 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
723 spec
->type
= SLANG_SPEC_SAMPLER2DRECTSHADOW
;
725 case TYPE_SPECIFIER_STRUCT
:
726 spec
->type
= SLANG_SPEC_STRUCT
;
727 if (!parse_struct(C
, O
, &spec
->_struct
))
730 case TYPE_SPECIFIER_TYPENAME
:
731 spec
->type
= SLANG_SPEC_STRUCT
;
736 a_name
= parse_identifier(C
);
740 stru
= slang_struct_scope_find(O
->structs
, a_name
, 1);
742 slang_info_log_error(C
->L
, "undeclared type name '%s'",
743 slang_atom_pool_id(C
->atoms
, a_name
));
747 spec
->_struct
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
748 if (spec
->_struct
== NULL
) {
749 slang_info_log_memory(C
->L
);
752 if (!slang_struct_construct(spec
->_struct
)) {
753 _slang_free(spec
->_struct
);
754 spec
->_struct
= NULL
;
757 if (!slang_struct_copy(spec
->_struct
, stru
))
767 #define TYPE_SPECIFIER_NONARRAY 0
768 #define TYPE_SPECIFIER_ARRAY 1
771 parse_type_array_size(slang_parse_ctx
*C
,
778 case TYPE_SPECIFIER_NONARRAY
:
779 *array_len
= -1; /* -1 = not an array */
781 case TYPE_SPECIFIER_ARRAY
:
782 if (!parse_array_len(C
, O
, &size
))
784 *array_len
= (GLint
) size
;
793 #define PRECISION_DEFAULT 0
794 #define PRECISION_LOW 1
795 #define PRECISION_MEDIUM 2
796 #define PRECISION_HIGH 3
799 parse_type_precision(slang_parse_ctx
*C
,
800 slang_type_precision
*precision
)
802 GLint prec
= *C
->I
++;
804 case PRECISION_DEFAULT
:
805 *precision
= SLANG_PREC_DEFAULT
;
808 *precision
= SLANG_PREC_LOW
;
810 case PRECISION_MEDIUM
:
811 *precision
= SLANG_PREC_MEDIUM
;
814 *precision
= SLANG_PREC_HIGH
;
822 parse_fully_specified_type(slang_parse_ctx
* C
, slang_output_ctx
* O
,
823 slang_fully_specified_type
* type
)
825 if (!parse_type_variant(C
, &type
->variant
))
828 if (!parse_type_centroid(C
, &type
->centroid
))
831 if (!parse_type_qualifier(C
, &type
->qualifier
))
834 if (!parse_type_precision(C
, &type
->precision
))
837 if (!parse_type_specifier(C
, O
, &type
->specifier
))
840 if (!parse_type_array_size(C
, O
, &type
->array_len
))
843 if (!O
->allow_invariant
&& type
->variant
== SLANG_INVARIANT
) {
844 slang_info_log_error(C
->L
,
845 "'invariant' keyword not allowed (perhaps set #version 120)");
849 if (!O
->allow_centroid
&& type
->centroid
== SLANG_CENTROID
) {
850 slang_info_log_error(C
->L
,
851 "'centroid' keyword not allowed (perhaps set #version 120)");
854 else if (type
->centroid
== SLANG_CENTROID
&&
855 type
->qualifier
!= SLANG_QUAL_VARYING
) {
856 slang_info_log_error(C
->L
,
857 "'centroid' keyword only allowed for varying vars");
863 if (type->qualifier != SLANG_QUAL_VARYING &&
864 type->variant == SLANG_INVARIANT) {
865 slang_info_log_error(C->L,
866 "invariant qualifer only allowed for varying vars");
871 if (O
->allow_precision
) {
872 if (type
->precision
== SLANG_PREC_DEFAULT
) {
873 assert(type
->specifier
.type
< TYPE_SPECIFIER_COUNT
);
874 /* use the default precision for this datatype */
875 type
->precision
= O
->default_precision
[type
->specifier
.type
];
879 /* only default is allowed */
880 if (type
->precision
!= SLANG_PREC_DEFAULT
) {
881 slang_info_log_error(C
->L
, "precision qualifiers not allowed");
886 if (!O
->allow_array_types
&& type
->array_len
>= 0) {
887 slang_info_log_error(C
->L
, "first-class array types not allowed");
891 if (type
->array_len
>= 0) {
892 /* convert type to array type (ex: convert "int" to "array of int" */
893 promote_type_to_array(C
, type
, type
->array_len
);
901 #define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
902 #define OP_BLOCK_BEGIN_NEW_SCOPE 2
906 #define OP_CONTINUE 6
909 #define OP_EXPRESSION 9
914 #define OP_PUSH_VOID 14
915 #define OP_PUSH_BOOL 15
916 #define OP_PUSH_INT 16
917 #define OP_PUSH_FLOAT 17
918 #define OP_PUSH_IDENTIFIER 18
919 #define OP_SEQUENCE 19
921 #define OP_ADDASSIGN 21
922 #define OP_SUBASSIGN 22
923 #define OP_MULASSIGN 23
924 #define OP_DIVASSIGN 24
925 /*#define OP_MODASSIGN 25*/
926 /*#define OP_LSHASSIGN 26*/
927 /*#define OP_RSHASSIGN 27*/
928 /*#define OP_ORASSIGN 28*/
929 /*#define OP_XORASSIGN 29*/
930 /*#define OP_ANDASSIGN 30*/
932 #define OP_LOGICALOR 32
933 #define OP_LOGICALXOR 33
934 #define OP_LOGICALAND 34
935 /*#define OP_BITOR 35*/
936 /*#define OP_BITXOR 36*/
937 /*#define OP_BITAND 37*/
939 #define OP_NOTEQUAL 39
941 #define OP_GREATER 41
942 #define OP_LESSEQUAL 42
943 #define OP_GREATEREQUAL 43
944 /*#define OP_LSHIFT 44*/
945 /*#define OP_RSHIFT 45*/
947 #define OP_SUBTRACT 47
948 #define OP_MULTIPLY 48
950 /*#define OP_MODULUS 50*/
951 #define OP_PREINCREMENT 51
952 #define OP_PREDECREMENT 52
955 /*#define OP_COMPLEMENT 55*/
957 #define OP_SUBSCRIPT 57
960 #define OP_POSTINCREMENT 60
961 #define OP_POSTDECREMENT 61
962 #define OP_PRECISION 62
967 * When parsing a compound production, this function is used to parse the
969 * For example, a while-loop compound will have two children, the
970 * while condition expression and the loop body. So, this function will
971 * be called twice to parse those two sub-expressions.
972 * \param C the parsing context
973 * \param O the output context
974 * \param oper the operation we're parsing
975 * \param statement indicates whether parsing a statement, or expression
976 * \return 1 if success, 0 if error
979 parse_child_operation(slang_parse_ctx
* C
, slang_output_ctx
* O
,
980 slang_operation
* oper
, GLboolean statement
)
984 /* grow child array */
985 ch
= slang_operation_grow(&oper
->num_children
, &oper
->children
);
987 return parse_statement(C
, O
, ch
);
988 return parse_expression(C
, O
, ch
);
991 static int parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
);
994 parse_statement(slang_parse_ctx
* C
, slang_output_ctx
* O
,
995 slang_operation
* oper
)
999 oper
->locals
->outer_scope
= O
->vars
;
1003 case OP_BLOCK_BEGIN_NO_NEW_SCOPE
:
1004 /* parse child statements, do not create new variable scope */
1005 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1006 while (*C
->I
!= OP_END
)
1007 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1011 case OP_BLOCK_BEGIN_NEW_SCOPE
:
1012 /* parse child statements, create new variable scope */
1014 slang_output_ctx o
= *O
;
1016 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1017 o
.vars
= oper
->locals
;
1018 while (*C
->I
!= OP_END
)
1019 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1025 /* local variable declaration, individual declarators are stored as
1026 * children identifiers
1028 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1030 const unsigned int first_var
= O
->vars
->num_variables
;
1032 /* parse the declaration, note that there can be zero or more
1033 * than one declarators
1035 if (!parse_declaration(C
, O
))
1037 if (first_var
< O
->vars
->num_variables
) {
1038 const unsigned int num_vars
= O
->vars
->num_variables
- first_var
;
1040 assert(oper
->num_children
== 0);
1041 oper
->num_children
= num_vars
;
1042 oper
->children
= slang_operation_new(num_vars
);
1043 if (oper
->children
== NULL
) {
1044 slang_info_log_memory(C
->L
);
1047 for (i
= first_var
; i
< O
->vars
->num_variables
; i
++) {
1048 slang_operation
*o
= &oper
->children
[i
- first_var
];
1049 slang_variable
*var
= O
->vars
->variables
[i
];
1050 o
->type
= SLANG_OPER_VARIABLE_DECL
;
1051 o
->locals
->outer_scope
= O
->vars
;
1052 o
->a_id
= var
->a_name
;
1055 calculate_var_size(C, O, var);
1058 if (!legal_identifier(o
->a_id
)) {
1059 slang_info_log_error(C
->L
, "illegal variable name '%s'",
1068 /* the __asm statement, parse the mnemonic and all its arguments
1071 oper
->type
= SLANG_OPER_ASM
;
1072 oper
->a_id
= parse_identifier(C
);
1073 if (oper
->a_id
== SLANG_ATOM_NULL
)
1075 while (*C
->I
!= OP_END
) {
1076 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1082 oper
->type
= SLANG_OPER_BREAK
;
1085 oper
->type
= SLANG_OPER_CONTINUE
;
1088 oper
->type
= SLANG_OPER_DISCARD
;
1091 oper
->type
= SLANG_OPER_RETURN
;
1092 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1096 oper
->type
= SLANG_OPER_EXPRESSION
;
1097 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1101 oper
->type
= SLANG_OPER_IF
;
1102 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1104 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1106 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1111 slang_output_ctx o
= *O
;
1113 oper
->type
= SLANG_OPER_WHILE
;
1114 o
.vars
= oper
->locals
;
1115 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1117 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1122 oper
->type
= SLANG_OPER_DO
;
1123 if (!parse_child_operation(C
, O
, oper
, GL_TRUE
))
1125 if (!parse_child_operation(C
, O
, oper
, GL_FALSE
))
1130 slang_output_ctx o
= *O
;
1132 oper
->type
= SLANG_OPER_FOR
;
1133 o
.vars
= oper
->locals
;
1134 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1136 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1138 if (!parse_child_operation(C
, &o
, oper
, GL_FALSE
))
1140 if (!parse_child_operation(C
, &o
, oper
, GL_TRUE
))
1146 /* set default precision for a type in this scope */
1147 /* ignored at this time */
1148 int prec_qual
= *C
->I
++;
1149 int datatype
= *C
->I
++;
1155 /*printf("Unexpected operation %d\n", op);*/
1162 handle_nary_expression(slang_parse_ctx
* C
, slang_operation
* op
,
1163 slang_operation
** ops
, unsigned int *total_ops
,
1168 op
->children
= slang_operation_new(n
);
1169 if (op
->children
== NULL
) {
1170 slang_info_log_memory(C
->L
);
1173 op
->num_children
= n
;
1175 for (i
= 0; i
< n
; i
++) {
1176 slang_operation_destruct(&op
->children
[i
]);
1177 op
->children
[i
] = (*ops
)[*total_ops
- (n
+ 1 - i
)];
1180 (*ops
)[*total_ops
- (n
+ 1)] = (*ops
)[*total_ops
- 1];
1183 *ops
= (slang_operation
*)
1184 _slang_realloc(*ops
,
1185 (*total_ops
+ n
) * sizeof(slang_operation
),
1186 *total_ops
* sizeof(slang_operation
));
1188 slang_info_log_memory(C
->L
);
1195 is_constructor_name(const char *name
, slang_atom a_name
,
1196 slang_struct_scope
* structs
)
1198 if (slang_type_specifier_type_from_string(name
) != SLANG_SPEC_VOID
)
1200 return slang_struct_scope_find(structs
, a_name
, 1) != NULL
;
1203 #define FUNCTION_CALL_NONARRAY 0
1204 #define FUNCTION_CALL_ARRAY 1
1207 parse_expression(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1208 slang_operation
* oper
)
1210 slang_operation
*ops
= NULL
;
1211 unsigned int num_ops
= 0;
1214 while (*C
->I
!= OP_END
) {
1215 slang_operation
*op
;
1216 const unsigned int op_code
= *C
->I
++;
1218 /* allocate default operation, becomes a no-op if not used */
1219 ops
= (slang_operation
*)
1221 num_ops
* sizeof(slang_operation
),
1222 (num_ops
+ 1) * sizeof(slang_operation
));
1224 slang_info_log_memory(C
->L
);
1228 if (!slang_operation_construct(op
)) {
1229 slang_info_log_memory(C
->L
);
1233 op
->locals
->outer_scope
= O
->vars
;
1237 op
->type
= SLANG_OPER_VOID
;
1240 op
->type
= SLANG_OPER_LITERAL_BOOL
;
1241 if (!parse_number(C
, &number
))
1246 op
->literal
[3] = (GLfloat
) number
;
1247 op
->literal_size
= 1;
1250 op
->type
= SLANG_OPER_LITERAL_INT
;
1251 if (!parse_number(C
, &number
))
1256 op
->literal
[3] = (GLfloat
) number
;
1257 op
->literal_size
= 1;
1260 op
->type
= SLANG_OPER_LITERAL_FLOAT
;
1261 if (!parse_float(C
, &op
->literal
[0]))
1265 op
->literal
[3] = op
->literal
[0];
1266 op
->literal_size
= 1;
1268 case OP_PUSH_IDENTIFIER
:
1269 op
->type
= SLANG_OPER_IDENTIFIER
;
1270 op
->a_id
= parse_identifier(C
);
1271 if (op
->a_id
== SLANG_ATOM_NULL
)
1275 op
->type
= SLANG_OPER_SEQUENCE
;
1276 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1280 op
->type
= SLANG_OPER_ASSIGN
;
1281 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1285 op
->type
= SLANG_OPER_ADDASSIGN
;
1286 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1290 op
->type
= SLANG_OPER_SUBASSIGN
;
1291 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1295 op
->type
= SLANG_OPER_MULASSIGN
;
1296 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1300 op
->type
= SLANG_OPER_DIVASSIGN
;
1301 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1304 /*case OP_MODASSIGN: */
1305 /*case OP_LSHASSIGN: */
1306 /*case OP_RSHASSIGN: */
1307 /*case OP_ORASSIGN: */
1308 /*case OP_XORASSIGN: */
1309 /*case OP_ANDASSIGN: */
1311 op
->type
= SLANG_OPER_SELECT
;
1312 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 3))
1316 op
->type
= SLANG_OPER_LOGICALOR
;
1317 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1321 op
->type
= SLANG_OPER_LOGICALXOR
;
1322 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1326 op
->type
= SLANG_OPER_LOGICALAND
;
1327 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1331 /*case OP_BITXOR: */
1332 /*case OP_BITAND: */
1334 op
->type
= SLANG_OPER_EQUAL
;
1335 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1339 op
->type
= SLANG_OPER_NOTEQUAL
;
1340 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1344 op
->type
= SLANG_OPER_LESS
;
1345 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1349 op
->type
= SLANG_OPER_GREATER
;
1350 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1354 op
->type
= SLANG_OPER_LESSEQUAL
;
1355 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1358 case OP_GREATEREQUAL
:
1359 op
->type
= SLANG_OPER_GREATEREQUAL
;
1360 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1363 /*case OP_LSHIFT: */
1364 /*case OP_RSHIFT: */
1366 op
->type
= SLANG_OPER_ADD
;
1367 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1371 op
->type
= SLANG_OPER_SUBTRACT
;
1372 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1376 op
->type
= SLANG_OPER_MULTIPLY
;
1377 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1381 op
->type
= SLANG_OPER_DIVIDE
;
1382 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1385 /*case OP_MODULUS: */
1386 case OP_PREINCREMENT
:
1387 op
->type
= SLANG_OPER_PREINCREMENT
;
1388 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1391 case OP_PREDECREMENT
:
1392 op
->type
= SLANG_OPER_PREDECREMENT
;
1393 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1397 op
->type
= SLANG_OPER_PLUS
;
1398 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1402 op
->type
= SLANG_OPER_MINUS
;
1403 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1407 op
->type
= SLANG_OPER_NOT
;
1408 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1411 /*case OP_COMPLEMENT: */
1413 op
->type
= SLANG_OPER_SUBSCRIPT
;
1414 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 2))
1418 op
->type
= SLANG_OPER_METHOD
;
1419 op
->a_obj
= parse_identifier(C
);
1420 if (op
->a_obj
== SLANG_ATOM_NULL
)
1423 op
->a_id
= parse_identifier(C
);
1424 if (op
->a_id
== SLANG_ATOM_NULL
)
1427 assert(*C
->I
== OP_END
);
1430 while (*C
->I
!= OP_END
)
1431 if (!parse_child_operation(C
, O
, op
, GL_FALSE
))
1435 /* don't lookup the method (not yet anyway) */
1436 if (!C
->parsing_builtin
1437 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1440 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1441 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1442 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1450 GLboolean array_constructor
= GL_FALSE
;
1451 GLint array_constructor_size
= 0;
1453 op
->type
= SLANG_OPER_CALL
;
1454 op
->a_id
= parse_identifier(C
);
1455 if (op
->a_id
== SLANG_ATOM_NULL
)
1458 case FUNCTION_CALL_NONARRAY
:
1459 /* Nothing to do. */
1461 case FUNCTION_CALL_ARRAY
:
1462 /* Calling an array constructor. For example:
1463 * float[3](1.1, 2.2, 3.3);
1465 if (!O
->allow_array_types
) {
1466 slang_info_log_error(C
->L
,
1467 "array constructors not allowed "
1468 "in this GLSL version");
1472 /* parse the array constructor size */
1473 slang_operation array_size
;
1474 array_constructor
= GL_TRUE
;
1475 slang_operation_construct(&array_size
);
1476 if (!parse_expression(C
, O
, &array_size
)) {
1477 slang_operation_destruct(&array_size
);
1480 if (array_size
.type
!= SLANG_OPER_LITERAL_INT
) {
1481 slang_info_log_error(C
->L
,
1482 "constructor array size is not an integer");
1483 slang_operation_destruct(&array_size
);
1486 array_constructor_size
= (int) array_size
.literal
[0];
1487 op
->array_constructor
= GL_TRUE
;
1488 slang_operation_destruct(&array_size
);
1495 while (*C
->I
!= OP_END
)
1496 if (!parse_child_operation(C
, O
, op
, GL_FALSE
))
1500 if (array_constructor
&&
1501 array_constructor_size
!= op
->num_children
) {
1502 slang_info_log_error(C
->L
, "number of parameters to array"
1503 " constructor does not match array size");
1507 if (!C
->parsing_builtin
1508 && !slang_function_scope_find_by_name(O
->funs
, op
->a_id
, 1)) {
1511 id
= slang_atom_pool_id(C
->atoms
, op
->a_id
);
1512 if (!is_constructor_name(id
, op
->a_id
, O
->structs
)) {
1513 slang_info_log_error(C
->L
, "%s: undeclared function name.", id
);
1520 op
->type
= SLANG_OPER_FIELD
;
1521 op
->a_id
= parse_identifier(C
);
1522 if (op
->a_id
== SLANG_ATOM_NULL
)
1524 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1527 case OP_POSTINCREMENT
:
1528 op
->type
= SLANG_OPER_POSTINCREMENT
;
1529 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1532 case OP_POSTDECREMENT
:
1533 op
->type
= SLANG_OPER_POSTDECREMENT
;
1534 if (!handle_nary_expression(C
, op
, &ops
, &num_ops
, 1))
1543 slang_operation_destruct(oper
);
1544 *oper
= *ops
; /* struct copy */
1550 /* parameter qualifier */
1551 #define PARAM_QUALIFIER_IN 0
1552 #define PARAM_QUALIFIER_OUT 1
1553 #define PARAM_QUALIFIER_INOUT 2
1555 /* function parameter array presence */
1556 #define PARAMETER_ARRAY_NOT_PRESENT 0
1557 #define PARAMETER_ARRAY_PRESENT 1
1560 parse_parameter_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1561 slang_variable
* param
)
1563 int param_qual
, precision_qual
;
1565 /* parse and validate the parameter's type qualifiers (there can be
1566 * two at most) because not all combinations are valid
1568 if (!parse_type_qualifier(C
, ¶m
->type
.qualifier
))
1571 param_qual
= *C
->I
++;
1572 switch (param_qual
) {
1573 case PARAM_QUALIFIER_IN
:
1574 if (param
->type
.qualifier
!= SLANG_QUAL_CONST
1575 && param
->type
.qualifier
!= SLANG_QUAL_NONE
) {
1576 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1580 case PARAM_QUALIFIER_OUT
:
1581 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1582 param
->type
.qualifier
= SLANG_QUAL_OUT
;
1584 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1588 case PARAM_QUALIFIER_INOUT
:
1589 if (param
->type
.qualifier
== SLANG_QUAL_NONE
)
1590 param
->type
.qualifier
= SLANG_QUAL_INOUT
;
1592 slang_info_log_error(C
->L
, "Invalid type qualifier.");
1600 /* parse precision qualifier (lowp, mediump, highp */
1601 precision_qual
= *C
->I
++;
1602 /* ignored at this time */
1603 (void) precision_qual
;
1605 /* parse parameter's type specifier and name */
1606 if (!parse_type_specifier(C
, O
, ¶m
->type
.specifier
))
1608 if (!parse_type_array_size(C
, O
, ¶m
->type
.array_len
))
1610 param
->a_name
= parse_identifier(C
);
1611 if (param
->a_name
== SLANG_ATOM_NULL
)
1614 /* first-class array
1616 if (param
->type
.array_len
>= 0) {
1617 slang_type_specifier p
;
1619 slang_type_specifier_ctr(&p
);
1620 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1621 slang_type_specifier_dtr(&p
);
1624 if (!convert_to_array(C
, param
, &p
)) {
1625 slang_type_specifier_dtr(&p
);
1628 slang_type_specifier_dtr(&p
);
1629 param
->array_len
= param
->type
.array_len
;
1632 /* if the parameter is an array, parse its size (the size must be
1633 * explicitly defined
1635 if (*C
->I
++ == PARAMETER_ARRAY_PRESENT
) {
1636 slang_type_specifier p
;
1638 if (param
->type
.array_len
>= 0) {
1639 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
1642 slang_type_specifier_ctr(&p
);
1643 if (!slang_type_specifier_copy(&p
, ¶m
->type
.specifier
)) {
1644 slang_type_specifier_dtr(&p
);
1647 if (!convert_to_array(C
, param
, &p
)) {
1648 slang_type_specifier_dtr(&p
);
1651 slang_type_specifier_dtr(&p
);
1652 if (!parse_array_len(C
, O
, ¶m
->array_len
))
1657 /* calculate the parameter size */
1658 if (!calculate_var_size(C
, O
, param
))
1661 /* TODO: allocate the local address here? */
1666 #define FUNCTION_ORDINARY 0
1667 #define FUNCTION_CONSTRUCTOR 1
1668 #define FUNCTION_OPERATOR 2
1670 /* function parameter */
1671 #define PARAMETER_NONE 0
1672 #define PARAMETER_NEXT 1
1675 #define OPERATOR_ADDASSIGN 1
1676 #define OPERATOR_SUBASSIGN 2
1677 #define OPERATOR_MULASSIGN 3
1678 #define OPERATOR_DIVASSIGN 4
1679 /*#define OPERATOR_MODASSIGN 5*/
1680 /*#define OPERATOR_LSHASSIGN 6*/
1681 /*#define OPERATOR_RSHASSIGN 7*/
1682 /*#define OPERATOR_ANDASSIGN 8*/
1683 /*#define OPERATOR_XORASSIGN 9*/
1684 /*#define OPERATOR_ORASSIGN 10*/
1685 #define OPERATOR_LOGICALXOR 11
1686 /*#define OPERATOR_BITOR 12*/
1687 /*#define OPERATOR_BITXOR 13*/
1688 /*#define OPERATOR_BITAND 14*/
1689 #define OPERATOR_LESS 15
1690 #define OPERATOR_GREATER 16
1691 #define OPERATOR_LESSEQUAL 17
1692 #define OPERATOR_GREATEREQUAL 18
1693 /*#define OPERATOR_LSHIFT 19*/
1694 /*#define OPERATOR_RSHIFT 20*/
1695 #define OPERATOR_MULTIPLY 21
1696 #define OPERATOR_DIVIDE 22
1697 /*#define OPERATOR_MODULUS 23*/
1698 #define OPERATOR_INCREMENT 24
1699 #define OPERATOR_DECREMENT 25
1700 #define OPERATOR_PLUS 26
1701 #define OPERATOR_MINUS 27
1702 /*#define OPERATOR_COMPLEMENT 28*/
1703 #define OPERATOR_NOT 29
1707 unsigned int o_code
;
1709 } operator_names
[] = {
1710 {OPERATOR_INCREMENT
, "++"},
1711 {OPERATOR_ADDASSIGN
, "+="},
1712 {OPERATOR_PLUS
, "+"},
1713 {OPERATOR_DECREMENT
, "--"},
1714 {OPERATOR_SUBASSIGN
, "-="},
1715 {OPERATOR_MINUS
, "-"},
1716 {OPERATOR_NOT
, "!"},
1717 {OPERATOR_MULASSIGN
, "*="},
1718 {OPERATOR_MULTIPLY
, "*"},
1719 {OPERATOR_DIVASSIGN
, "/="},
1720 {OPERATOR_DIVIDE
, "/"},
1721 {OPERATOR_LESSEQUAL
, "<="},
1722 /*{ OPERATOR_LSHASSIGN, "<<=" }, */
1723 /*{ OPERATOR_LSHIFT, "<<" }, */
1724 {OPERATOR_LESS
, "<"},
1725 {OPERATOR_GREATEREQUAL
, ">="},
1726 /*{ OPERATOR_RSHASSIGN, ">>=" }, */
1727 /*{ OPERATOR_RSHIFT, ">>" }, */
1728 {OPERATOR_GREATER
, ">"},
1729 /*{ OPERATOR_MODASSIGN, "%=" }, */
1730 /*{ OPERATOR_MODULUS, "%" }, */
1731 /*{ OPERATOR_ANDASSIGN, "&=" }, */
1732 /*{ OPERATOR_BITAND, "&" }, */
1733 /*{ OPERATOR_ORASSIGN, "|=" }, */
1734 /*{ OPERATOR_BITOR, "|" }, */
1735 /*{ OPERATOR_COMPLEMENT, "~" }, */
1736 /*{ OPERATOR_XORASSIGN, "^=" }, */
1737 {OPERATOR_LOGICALXOR
, "^^"},
1738 /*{ OPERATOR_BITXOR, "^" } */
1742 parse_operator_name(slang_parse_ctx
* C
)
1746 for (i
= 0; i
< sizeof(operator_names
) / sizeof(*operator_names
); i
++) {
1747 if (operator_names
[i
].o_code
== (unsigned int) (*C
->I
)) {
1749 slang_atom_pool_atom(C
->atoms
, operator_names
[i
].o_name
);
1750 if (atom
== SLANG_ATOM_NULL
) {
1751 slang_info_log_memory(C
->L
);
1763 parse_function_prototype(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1764 slang_function
* func
)
1767 /* parse function type and name */
1768 if (!parse_fully_specified_type(C
, O
, &func
->header
.type
))
1773 case FUNCTION_ORDINARY
:
1774 func
->kind
= SLANG_FUNC_ORDINARY
;
1775 func
->header
.a_name
= parse_identifier(C
);
1776 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1779 case FUNCTION_CONSTRUCTOR
:
1780 func
->kind
= SLANG_FUNC_CONSTRUCTOR
;
1781 if (func
->header
.type
.specifier
.type
== SLANG_SPEC_STRUCT
)
1783 func
->header
.a_name
=
1784 slang_atom_pool_atom(C
->atoms
,
1785 slang_type_specifier_type_to_string
1786 (func
->header
.type
.specifier
.type
));
1787 if (func
->header
.a_name
== SLANG_ATOM_NULL
) {
1788 slang_info_log_memory(C
->L
);
1792 case FUNCTION_OPERATOR
:
1793 func
->kind
= SLANG_FUNC_OPERATOR
;
1794 func
->header
.a_name
= parse_operator_name(C
);
1795 if (func
->header
.a_name
== SLANG_ATOM_NULL
)
1802 if (!legal_identifier(func
->header
.a_name
)) {
1803 slang_info_log_error(C
->L
, "illegal function name '%s'",
1804 (char *) func
->header
.a_name
);
1808 /* parse function parameters */
1809 while (*C
->I
++ == PARAMETER_NEXT
) {
1810 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1812 slang_info_log_memory(C
->L
);
1815 if (!parse_parameter_declaration(C
, O
, p
))
1819 /* if the function returns a value, append a hidden __retVal 'out'
1820 * parameter that corresponds to the return value.
1822 if (_slang_function_has_return_value(func
)) {
1823 slang_variable
*p
= slang_variable_scope_grow(func
->parameters
);
1824 slang_atom a_retVal
= slang_atom_pool_atom(C
->atoms
, "__retVal");
1826 p
->a_name
= a_retVal
;
1827 p
->type
= func
->header
.type
;
1828 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1831 /* function formal parameters and local variables share the same
1832 * scope, so save the information about param count in a seperate
1833 * place also link the scope to the global variable scope so when a
1834 * given identifier is not found here, the search process continues
1835 * in the global space
1837 func
->param_count
= func
->parameters
->num_variables
;
1838 func
->parameters
->outer_scope
= O
->vars
;
1844 parse_function_definition(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1845 slang_function
* func
)
1847 slang_output_ctx o
= *O
;
1849 if (!parse_function_prototype(C
, O
, func
))
1852 /* create function's body operation */
1853 func
->body
= (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
1854 if (func
->body
== NULL
) {
1855 slang_info_log_memory(C
->L
);
1858 if (!slang_operation_construct(func
->body
)) {
1859 _slang_free(func
->body
);
1861 slang_info_log_memory(C
->L
);
1865 /* to parse the body the parse context is modified in order to
1866 * capture parsed variables into function's local variable scope
1868 C
->global_scope
= GL_FALSE
;
1869 o
.vars
= func
->parameters
;
1870 if (!parse_statement(C
, &o
, func
->body
))
1873 C
->global_scope
= GL_TRUE
;
1878 initialize_global(slang_assemble_ctx
* A
, slang_variable
* var
)
1880 slang_operation op_id
, op_assign
;
1883 /* construct the left side of assignment */
1884 if (!slang_operation_construct(&op_id
))
1886 op_id
.type
= SLANG_OPER_IDENTIFIER
;
1887 op_id
.a_id
= var
->a_name
;
1889 /* put the variable into operation's scope */
1890 op_id
.locals
->variables
=
1891 (slang_variable
**) _slang_alloc(sizeof(slang_variable
*));
1892 if (op_id
.locals
->variables
== NULL
) {
1893 slang_operation_destruct(&op_id
);
1896 op_id
.locals
->num_variables
= 1;
1897 op_id
.locals
->variables
[0] = var
;
1899 /* construct the assignment expression */
1900 if (!slang_operation_construct(&op_assign
)) {
1901 op_id
.locals
->num_variables
= 0;
1902 slang_operation_destruct(&op_id
);
1905 op_assign
.type
= SLANG_OPER_ASSIGN
;
1906 op_assign
.children
=
1907 (slang_operation
*) _slang_alloc(2 * sizeof(slang_operation
));
1908 if (op_assign
.children
== NULL
) {
1909 slang_operation_destruct(&op_assign
);
1910 op_id
.locals
->num_variables
= 0;
1911 slang_operation_destruct(&op_id
);
1914 op_assign
.num_children
= 2;
1915 op_assign
.children
[0] = op_id
;
1916 op_assign
.children
[1] = *var
->initializer
;
1920 /* carefully destroy the operations */
1921 op_assign
.num_children
= 0;
1922 _slang_free(op_assign
.children
);
1923 op_assign
.children
= NULL
;
1924 slang_operation_destruct(&op_assign
);
1925 op_id
.locals
->num_variables
= 0;
1926 slang_operation_destruct(&op_id
);
1934 /* init declarator list */
1935 #define DECLARATOR_NONE 0
1936 #define DECLARATOR_NEXT 1
1938 /* variable declaration */
1939 #define VARIABLE_NONE 0
1940 #define VARIABLE_IDENTIFIER 1
1941 #define VARIABLE_INITIALIZER 2
1942 #define VARIABLE_ARRAY_EXPLICIT 3
1943 #define VARIABLE_ARRAY_UNKNOWN 4
1947 * Parse the initializer for a variable declaration.
1950 parse_init_declarator(slang_parse_ctx
* C
, slang_output_ctx
* O
,
1951 const slang_fully_specified_type
* type
)
1953 GET_CURRENT_CONTEXT(ctx
); /* a hack */
1954 slang_variable
*var
;
1957 /* empty init declatator (without name, e.g. "float ;") */
1958 if (*C
->I
++ == VARIABLE_NONE
)
1961 a_name
= parse_identifier(C
);
1963 /* check if name is already in this scope */
1964 if (_slang_variable_locate(O
->vars
, a_name
, GL_FALSE
)) {
1965 slang_info_log_error(C
->L
,
1966 "declaration of '%s' conflicts with previous declaration",
1971 /* make room for the new variable and initialize it */
1972 var
= slang_variable_scope_grow(O
->vars
);
1974 slang_info_log_memory(C
->L
);
1978 /* copy the declarator type qualifier/etc info, parse the identifier */
1979 var
->type
.qualifier
= type
->qualifier
;
1980 var
->type
.centroid
= type
->centroid
;
1981 var
->type
.precision
= type
->precision
;
1982 var
->type
.variant
= type
->variant
;
1983 var
->type
.array_len
= type
->array_len
;
1984 var
->a_name
= a_name
;
1985 if (var
->a_name
== SLANG_ATOM_NULL
)
1990 /* simple variable declarator - just copy the specifier */
1991 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
1994 case VARIABLE_INITIALIZER
:
1995 /* initialized variable - copy the specifier and parse the expression */
1996 if (0 && type
->array_len
>= 0) {
1997 /* The type was something like "float[4]" */
1998 convert_to_array(C
, var
, &type
->specifier
);
1999 var
->array_len
= type
->array_len
;
2002 if (!slang_type_specifier_copy(&var
->type
.specifier
, &type
->specifier
))
2006 (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
2007 if (var
->initializer
== NULL
) {
2008 slang_info_log_memory(C
->L
);
2011 if (!slang_operation_construct(var
->initializer
)) {
2012 _slang_free(var
->initializer
);
2013 var
->initializer
= NULL
;
2014 slang_info_log_memory(C
->L
);
2017 if (!parse_expression(C
, O
, var
->initializer
))
2020 case VARIABLE_ARRAY_UNKNOWN
:
2021 /* unsized array - mark it as array and copy the specifier to
2024 if (type
->array_len
>= 0) {
2025 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
2028 if (!convert_to_array(C
, var
, &type
->specifier
))
2031 case VARIABLE_ARRAY_EXPLICIT
:
2032 if (type
->array_len
>= 0) {
2033 /* the user is trying to do something like: float[2] x[3]; */
2034 slang_info_log_error(C
->L
, "multi-dimensional arrays not allowed");
2037 if (!convert_to_array(C
, var
, &type
->specifier
))
2039 if (!parse_array_len(C
, O
, &var
->array_len
))
2046 /* allocate global address space for a variable with a known size */
2048 && !(var
->type
.specifier
.type
== SLANG_SPEC_ARRAY
2049 && var
->array_len
== 0)) {
2050 if (!calculate_var_size(C
, O
, var
))
2054 /* emit code for global var decl */
2055 if (C
->global_scope
) {
2056 slang_assemble_ctx A
;
2057 memset(&A
, 0, sizeof(slang_assemble_ctx
));
2058 A
.allow_uniform_initializers
= C
->version
> 110;
2060 A
.space
.funcs
= O
->funs
;
2061 A
.space
.structs
= O
->structs
;
2062 A
.space
.vars
= O
->vars
;
2063 A
.program
= O
->program
;
2064 A
.pragmas
= O
->pragmas
;
2065 A
.vartable
= O
->vartable
;
2067 A
.curFuncEndLabel
= NULL
;
2068 A
.EmitContReturn
= ctx
->Shader
.EmitContReturn
;
2069 if (!_slang_codegen_global_variable(&A
, var
, C
->type
))
2073 /* initialize global variable */
2074 if (C
->global_scope
) {
2075 if (var
->initializer
!= NULL
) {
2076 slang_assemble_ctx A
;
2077 memset(&A
, 0, sizeof(slang_assemble_ctx
));
2078 A
.allow_uniform_initializers
= C
->version
> 110;
2080 A
.space
.funcs
= O
->funs
;
2081 A
.space
.structs
= O
->structs
;
2082 A
.space
.vars
= O
->vars
;
2083 if (!initialize_global(&A
, var
))
2091 * Parse a list of variable declarations. Each variable may have an
2095 parse_init_declarator_list(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2097 slang_fully_specified_type type
;
2099 /* parse the fully specified type, common to all declarators */
2100 if (!slang_fully_specified_type_construct(&type
))
2102 if (!parse_fully_specified_type(C
, O
, &type
)) {
2103 slang_fully_specified_type_destruct(&type
);
2107 /* parse declarators, pass-in the parsed type */
2109 if (!parse_init_declarator(C
, O
, &type
)) {
2110 slang_fully_specified_type_destruct(&type
);
2114 while (*C
->I
++ == DECLARATOR_NEXT
);
2116 slang_fully_specified_type_destruct(&type
);
2122 * Parse a function definition or declaration.
2123 * \param C parsing context
2124 * \param O output context
2125 * \param definition if non-zero expect a definition, else a declaration
2126 * \param parsed_func_ret returns the parsed function
2127 * \return GL_TRUE if success, GL_FALSE if failure
2130 parse_function(slang_parse_ctx
* C
, slang_output_ctx
* O
, int definition
,
2131 slang_function
** parsed_func_ret
)
2133 slang_function parsed_func
, *found_func
;
2135 /* parse function definition/declaration */
2136 if (!slang_function_construct(&parsed_func
))
2139 if (!parse_function_definition(C
, O
, &parsed_func
)) {
2140 slang_function_destruct(&parsed_func
);
2145 if (!parse_function_prototype(C
, O
, &parsed_func
)) {
2146 slang_function_destruct(&parsed_func
);
2151 /* find a function with a prototype matching the parsed one - only
2152 * the current scope is being searched to allow built-in function
2155 found_func
= slang_function_scope_find(O
->funs
, &parsed_func
, 0);
2156 if (found_func
== NULL
) {
2157 /* New function, add it to the function list */
2158 O
->funs
->functions
=
2159 (slang_function
*) _slang_realloc(O
->funs
->functions
,
2160 O
->funs
->num_functions
2161 * sizeof(slang_function
),
2162 (O
->funs
->num_functions
+ 1)
2163 * sizeof(slang_function
));
2164 if (O
->funs
->functions
== NULL
) {
2165 /* Make sure that there are no functions marked, as the
2166 * allocation is currently NULL, in order to avoid
2167 * a potental segfault as we clean up later.
2169 O
->funs
->num_functions
= 0;
2171 slang_info_log_memory(C
->L
);
2172 slang_function_destruct(&parsed_func
);
2175 O
->funs
->functions
[O
->funs
->num_functions
] = parsed_func
;
2176 O
->funs
->num_functions
++;
2178 /* return the newly parsed function */
2179 *parsed_func_ret
= &O
->funs
->functions
[O
->funs
->num_functions
- 1];
2182 /* previously defined or declared */
2183 /* TODO: check function return type qualifiers and specifiers */
2185 if (found_func
->body
!= NULL
) {
2186 slang_info_log_error(C
->L
, "%s: function already has a body.",
2187 slang_atom_pool_id(C
->atoms
,
2190 slang_function_destruct(&parsed_func
);
2194 /* destroy the existing function declaration and replace it
2197 slang_function_destruct(found_func
);
2198 *found_func
= parsed_func
;
2201 /* another declaration of the same function prototype - ignore it */
2202 slang_function_destruct(&parsed_func
);
2205 /* return the found function */
2206 *parsed_func_ret
= found_func
;
2213 #define DECLARATION_FUNCTION_PROTOTYPE 1
2214 #define DECLARATION_INIT_DECLARATOR_LIST 2
2217 parse_declaration(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2220 case DECLARATION_INIT_DECLARATOR_LIST
:
2221 if (!parse_init_declarator_list(C
, O
))
2224 case DECLARATION_FUNCTION_PROTOTYPE
:
2226 slang_function
*dummy_func
;
2228 if (!parse_function(C
, O
, 0, &dummy_func
))
2239 parse_default_precision(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2241 int precision
, type
;
2243 if (!O
->allow_precision
) {
2244 slang_info_log_error(C
->L
, "syntax error at \"precision\"");
2248 precision
= *C
->I
++;
2249 switch (precision
) {
2251 case PRECISION_MEDIUM
:
2252 case PRECISION_HIGH
:
2256 _mesa_problem(NULL
, "unexpected precision %d at %s:%d\n",
2257 precision
, __FILE__
, __LINE__
);
2263 case TYPE_SPECIFIER_FLOAT
:
2264 case TYPE_SPECIFIER_INT
:
2265 case TYPE_SPECIFIER_SAMPLER1D
:
2266 case TYPE_SPECIFIER_SAMPLER2D
:
2267 case TYPE_SPECIFIER_SAMPLER3D
:
2268 case TYPE_SPECIFIER_SAMPLERCUBE
:
2269 case TYPE_SPECIFIER_SAMPLER1DSHADOW
:
2270 case TYPE_SPECIFIER_SAMPLER2DSHADOW
:
2271 case TYPE_SPECIFIER_SAMPLER2DRECT
:
2272 case TYPE_SPECIFIER_SAMPLER2DRECTSHADOW
:
2276 _mesa_problem(NULL
, "unexpected type %d at %s:%d\n",
2277 type
, __FILE__
, __LINE__
);
2281 assert(type
< TYPE_SPECIFIER_COUNT
);
2282 O
->default_precision
[type
] = precision
;
2289 * Initialize the default precision for all types.
2290 * XXX this info isn't used yet.
2293 init_default_precision(slang_output_ctx
*O
, slang_unit_type type
)
2296 for (i
= 0; i
< TYPE_SPECIFIER_COUNT
; i
++) {
2297 #if FEATURE_es2_glsl
2298 O
->default_precision
[i
] = PRECISION_LOW
;
2300 O
->default_precision
[i
] = PRECISION_HIGH
;
2304 if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2305 O
->default_precision
[TYPE_SPECIFIER_FLOAT
] = PRECISION_HIGH
;
2306 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_HIGH
;
2309 O
->default_precision
[TYPE_SPECIFIER_INT
] = PRECISION_MEDIUM
;
2315 parse_invariant(slang_parse_ctx
* C
, slang_output_ctx
* O
)
2317 if (O
->allow_invariant
) {
2318 slang_atom
*a
= parse_identifier(C
);
2319 /* XXX not doing anything with this var yet */
2320 /*printf("ID: %s\n", (char*) a);*/
2324 slang_info_log_error(C
->L
, "syntax error at \"invariant\"");
2330 /* external declaration or default precision specifier */
2331 #define EXTERNAL_NULL 0
2332 #define EXTERNAL_FUNCTION_DEFINITION 1
2333 #define EXTERNAL_DECLARATION 2
2334 #define DEFAULT_PRECISION 3
2335 #define INVARIANT_STMT 4
2339 parse_code_unit(slang_parse_ctx
* C
, slang_code_unit
* unit
,
2340 struct gl_shader
*shader
)
2342 GET_CURRENT_CONTEXT(ctx
);
2346 slang_function
*mainFunc
= NULL
;
2348 if (unit
->type
== SLANG_UNIT_FRAGMENT_BUILTIN
||
2349 unit
->type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2350 maxRegs
= ctx
->Const
.FragmentProgram
.MaxTemps
;
2353 assert(unit
->type
== SLANG_UNIT_VERTEX_BUILTIN
||
2354 unit
->type
== SLANG_UNIT_VERTEX_SHADER
);
2355 maxRegs
= ctx
->Const
.VertexProgram
.MaxTemps
;
2358 /* setup output context */
2359 o
.funs
= &unit
->funs
;
2360 o
.structs
= &unit
->structs
;
2361 o
.vars
= &unit
->vars
;
2362 o
.program
= shader
? shader
->Program
: NULL
;
2363 o
.pragmas
= shader
? &shader
->Pragmas
: NULL
;
2364 o
.vartable
= _slang_new_var_table(maxRegs
);
2365 _slang_push_var_table(o
.vartable
);
2367 /* allow 'invariant' keyword? */
2368 #if FEATURE_es2_glsl
2369 o
.allow_invariant
= GL_TRUE
;
2371 o
.allow_invariant
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2374 /* allow 'centroid' keyword? */
2375 o
.allow_centroid
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2377 /* allow 'lowp/mediump/highp' keywords? */
2378 #if FEATURE_es2_glsl
2379 o
.allow_precision
= GL_TRUE
;
2381 o
.allow_precision
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2383 init_default_precision(&o
, unit
->type
);
2385 /* allow 'float[]' keyword? */
2386 o
.allow_array_types
= (C
->version
>= 120) ? GL_TRUE
: GL_FALSE
;
2388 /* parse individual functions and declarations */
2389 while (*C
->I
!= EXTERNAL_NULL
) {
2391 case EXTERNAL_FUNCTION_DEFINITION
:
2393 slang_function
*func
;
2394 success
= parse_function(C
, &o
, 1, &func
);
2396 _mesa_strcmp((char *) func
->header
.a_name
, "main") == 0) {
2402 case EXTERNAL_DECLARATION
:
2403 success
= parse_declaration(C
, &o
);
2405 case DEFAULT_PRECISION
:
2406 success
= parse_default_precision(C
, &o
);
2408 case INVARIANT_STMT
:
2409 success
= parse_invariant(C
, &o
);
2416 /* xxx free codegen */
2417 _slang_pop_var_table(o
.vartable
);
2424 /* assemble (generate code) for main() */
2425 slang_assemble_ctx A
;
2426 memset(&A
, 0, sizeof(slang_assemble_ctx
));
2428 A
.space
.funcs
= o
.funs
;
2429 A
.space
.structs
= o
.structs
;
2430 A
.space
.vars
= o
.vars
;
2431 A
.program
= o
.program
;
2432 A
.pragmas
= &shader
->Pragmas
;
2433 A
.vartable
= o
.vartable
;
2434 A
.EmitContReturn
= ctx
->Shader
.EmitContReturn
;
2436 A
.allow_uniform_initializers
= C
->version
> 110;
2438 /* main() takes no parameters */
2439 if (mainFunc
->param_count
> 0) {
2440 slang_info_log_error(A
.log
, "main() takes no arguments");
2444 _slang_codegen_function(&A
, mainFunc
);
2446 shader
->Main
= GL_TRUE
; /* this shader defines main() */
2448 shader
->UnresolvedRefs
= A
.UnresolvedRefs
;
2451 _slang_pop_var_table(o
.vartable
);
2452 _slang_delete_var_table(o
.vartable
);
2458 compile_binary(const byte
* prod
, slang_code_unit
* unit
,
2460 slang_unit_type type
, slang_info_log
* infolog
,
2461 slang_code_unit
* builtin
, slang_code_unit
* downlink
,
2462 struct gl_shader
*shader
)
2468 /* setup parse context */
2471 C
.parsing_builtin
= (builtin
== NULL
);
2472 C
.global_scope
= GL_TRUE
;
2473 C
.atoms
= &unit
->object
->atompool
;
2475 C
.version
= version
;
2477 if (!check_revision(&C
))
2480 if (downlink
!= NULL
) {
2481 unit
->vars
.outer_scope
= &downlink
->vars
;
2482 unit
->funs
.outer_scope
= &downlink
->funs
;
2483 unit
->structs
.outer_scope
= &downlink
->structs
;
2486 /* parse translation unit */
2487 return parse_code_unit(&C
, unit
, shader
);
2491 compile_with_grammar(grammar id
, const char *source
, slang_code_unit
* unit
,
2492 slang_unit_type type
, slang_info_log
* infolog
,
2493 slang_code_unit
* builtin
,
2494 struct gl_shader
*shader
,
2495 const struct gl_extensions
*extensions
,
2496 struct gl_sl_pragmas
*pragmas
)
2499 GLuint size
, start
, version
;
2500 slang_string preprocessed
;
2503 #if FEATURE_ARB_shading_language_120
2505 #elif FEATURE_es2_glsl
2511 /* First retrieve the version number. */
2512 if (!_slang_preprocess_version(source
, &version
, &start
, infolog
))
2515 if (version
> maxVersion
) {
2516 slang_info_log_error(infolog
,
2517 "language version %.2f is not supported.",
2522 /* Now preprocess the source string. */
2523 slang_string_init(&preprocessed
);
2524 if (!_slang_preprocess_directives(&preprocessed
, &source
[start
],
2525 infolog
, extensions
, pragmas
)) {
2526 slang_string_free(&preprocessed
);
2527 slang_info_log_error(infolog
, "failed to preprocess the source.");
2531 /* Finally check the syntax and generate its binary representation. */
2532 if (!grammar_fast_check(id
,
2533 (const byte
*) (slang_string_cstr(&preprocessed
)),
2534 &prod
, &size
, 65536)) {
2538 slang_string_free(&preprocessed
);
2539 grammar_get_last_error((byte
*) (buf
), sizeof(buf
), &pos
);
2540 slang_info_log_error(infolog
, buf
);
2541 /* syntax error (possibly in library code) */
2546 s
= (char *) _mesa_find_line_column((const GLubyte
*) source
,
2547 (const GLubyte
*) source
+ pos
,
2549 printf("Error on line %d, col %d: %s\n", line
, col
, s
);
2554 slang_string_free(&preprocessed
);
2556 /* Syntax is okay - translate it to internal representation. */
2557 if (!compile_binary(prod
, unit
, version
, type
, infolog
, builtin
,
2558 &builtin
[SLANG_BUILTIN_TOTAL
- 1],
2560 grammar_alloc_free(prod
);
2563 grammar_alloc_free(prod
);
2567 LONGSTRING
static const char *slang_shader_syn
=
2568 #include "library/slang_shader_syn.h"
2571 static const byte slang_core_gc
[] = {
2572 #include "library/slang_core_gc.h"
2575 static const byte slang_120_core_gc
[] = {
2576 #include "library/slang_120_core_gc.h"
2579 static const byte slang_120_fragment_gc
[] = {
2580 #include "library/slang_builtin_120_fragment_gc.h"
2583 static const byte slang_common_builtin_gc
[] = {
2584 #include "library/slang_common_builtin_gc.h"
2587 static const byte slang_fragment_builtin_gc
[] = {
2588 #include "library/slang_fragment_builtin_gc.h"
2591 static const byte slang_vertex_builtin_gc
[] = {
2592 #include "library/slang_vertex_builtin_gc.h"
2596 compile_object(grammar
* id
, const char *source
, slang_code_object
* object
,
2597 slang_unit_type type
, slang_info_log
* infolog
,
2598 struct gl_shader
*shader
,
2599 const struct gl_extensions
*extensions
,
2600 struct gl_sl_pragmas
*pragmas
)
2602 slang_code_unit
*builtins
= NULL
;
2603 GLuint base_version
= 110;
2605 /* load GLSL grammar */
2606 *id
= grammar_load_from_text((const byte
*) (slang_shader_syn
));
2611 grammar_get_last_error(buf
, 1024, &pos
);
2612 slang_info_log_error(infolog
, (const char *) (buf
));
2616 /* set shader type - the syntax is slightly different for different shaders */
2617 if (type
== SLANG_UNIT_FRAGMENT_SHADER
2618 || type
== SLANG_UNIT_FRAGMENT_BUILTIN
)
2619 grammar_set_reg8(*id
, (const byte
*) "shader_type", 1);
2621 grammar_set_reg8(*id
, (const byte
*) "shader_type", 2);
2623 /* enable language extensions */
2624 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2626 /* if parsing user-specified shader, load built-in library */
2627 if (type
== SLANG_UNIT_FRAGMENT_SHADER
|| type
== SLANG_UNIT_VERTEX_SHADER
) {
2628 /* compile core functionality first */
2629 if (!compile_binary(slang_core_gc
,
2630 &object
->builtin
[SLANG_BUILTIN_CORE
],
2632 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2636 #if FEATURE_ARB_shading_language_120
2637 if (!compile_binary(slang_120_core_gc
,
2638 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2640 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
,
2641 NULL
, &object
->builtin
[SLANG_BUILTIN_CORE
], NULL
))
2645 /* compile common functions and variables, link to core */
2646 if (!compile_binary(slang_common_builtin_gc
,
2647 &object
->builtin
[SLANG_BUILTIN_COMMON
],
2648 #if FEATURE_ARB_shading_language_120
2653 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2654 #if FEATURE_ARB_shading_language_120
2655 &object
->builtin
[SLANG_BUILTIN_120_CORE
],
2657 &object
->builtin
[SLANG_BUILTIN_CORE
],
2662 /* compile target-specific functions and variables, link to common */
2663 if (type
== SLANG_UNIT_FRAGMENT_SHADER
) {
2664 if (!compile_binary(slang_fragment_builtin_gc
,
2665 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2667 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2668 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2670 #if FEATURE_ARB_shading_language_120
2671 if (!compile_binary(slang_120_fragment_gc
,
2672 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2674 SLANG_UNIT_FRAGMENT_BUILTIN
, infolog
, NULL
,
2675 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2679 else if (type
== SLANG_UNIT_VERTEX_SHADER
) {
2680 if (!compile_binary(slang_vertex_builtin_gc
,
2681 &object
->builtin
[SLANG_BUILTIN_TARGET
],
2683 SLANG_UNIT_VERTEX_BUILTIN
, infolog
, NULL
,
2684 &object
->builtin
[SLANG_BUILTIN_COMMON
], NULL
))
2688 /* disable language extensions */
2689 #if NEW_SLANG /* allow-built-ins */
2690 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 1);
2692 grammar_set_reg8(*id
, (const byte
*) "parsing_builtin", 0);
2694 builtins
= object
->builtin
;
2697 /* compile the actual shader - pass-in built-in library for external shader */
2698 return compile_with_grammar(*id
, source
, &object
->unit
, type
, infolog
,
2699 builtins
, shader
, extensions
, pragmas
);
2704 compile_shader(GLcontext
*ctx
, slang_code_object
* object
,
2705 slang_unit_type type
, slang_info_log
* infolog
,
2706 struct gl_shader
*shader
)
2711 #if 0 /* for debug */
2712 _mesa_printf("********* COMPILE SHADER ***********\n");
2713 _mesa_printf("%s\n", shader
->Source
);
2714 _mesa_printf("************************************\n");
2717 assert(shader
->Program
);
2719 _slang_code_object_dtr(object
);
2720 _slang_code_object_ctr(object
);
2722 success
= compile_object(&id
, shader
->Source
, object
, type
, infolog
, shader
,
2723 &ctx
->Extensions
, &shader
->Pragmas
);
2725 grammar_destroy(id
);
2735 _slang_compile(GLcontext
*ctx
, struct gl_shader
*shader
)
2738 slang_info_log info_log
;
2739 slang_code_object obj
;
2740 slang_unit_type type
;
2743 if (shader
->Type
== GL_VERTEX_SHADER
) {
2744 type
= SLANG_UNIT_VERTEX_SHADER
;
2747 assert(shader
->Type
== GL_FRAGMENT_SHADER
);
2748 type
= SLANG_UNIT_FRAGMENT_SHADER
;
2751 if (!shader
->Source
)
2754 ctx
->Shader
.MemPool
= _slang_new_mempool(1024*1024);
2756 shader
->Main
= GL_FALSE
;
2758 /* free the shader's old instructions, etc */
2759 _mesa_reference_program(ctx
, &shader
->Program
, NULL
);
2761 /* allocate new GPU program, parameter lists, etc. */
2762 if (shader
->Type
== GL_VERTEX_SHADER
)
2763 progTarget
= GL_VERTEX_PROGRAM_ARB
;
2765 progTarget
= GL_FRAGMENT_PROGRAM_ARB
;
2766 shader
->Program
= ctx
->Driver
.NewProgram(ctx
, progTarget
, 1);
2767 shader
->Program
->Parameters
= _mesa_new_parameter_list();
2768 shader
->Program
->Varying
= _mesa_new_parameter_list();
2769 shader
->Program
->Attributes
= _mesa_new_parameter_list();
2771 slang_info_log_construct(&info_log
);
2772 _slang_code_object_ctr(&obj
);
2774 success
= compile_shader(ctx
, &obj
, type
, &info_log
, shader
);
2776 /* free shader's prev info log */
2777 if (shader
->InfoLog
) {
2778 _mesa_free(shader
->InfoLog
);
2779 shader
->InfoLog
= NULL
;
2782 if (info_log
.text
) {
2783 /* copy info-log string to shader object */
2784 shader
->InfoLog
= _mesa_strdup(info_log
.text
);
2787 if (info_log
.error_flag
) {
2791 slang_info_log_destruct(&info_log
);
2792 _slang_code_object_dtr(&obj
);
2794 _slang_delete_mempool((slang_mempool
*) ctx
->Shader
.MemPool
);
2795 ctx
->Shader
.MemPool
= NULL
;
2797 /* remove any reads of output registers */
2799 printf("Pre-remove output reads:\n");
2800 _mesa_print_program(shader
->Program
);
2802 _mesa_remove_output_reads(shader
->Program
, PROGRAM_OUTPUT
);
2803 if (shader
->Type
== GL_VERTEX_SHADER
) {
2804 /* and remove writes to varying vars in vertex programs */
2805 _mesa_remove_output_reads(shader
->Program
, PROGRAM_VARYING
);
2808 printf("Post-remove output reads:\n");
2809 _mesa_print_program(shader
->Program
);
2812 shader
->CompileStatus
= success
;
2815 if (shader
->Pragmas
.Optimize
&&
2816 (ctx
->Shader
.Flags
& GLSL_NO_OPT
) == 0) {
2817 _mesa_optimize_program(ctx
, shader
->Program
);
2819 if ((ctx
->Shader
.Flags
& GLSL_NOP_VERT
) &&
2820 shader
->Program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
2821 _mesa_nop_vertex_program(ctx
,
2822 (struct gl_vertex_program
*) shader
->Program
);
2824 if ((ctx
->Shader
.Flags
& GLSL_NOP_FRAG
) &&
2825 shader
->Program
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
2826 _mesa_nop_fragment_program(ctx
,
2827 (struct gl_fragment_program
*) shader
->Program
);
2831 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
2832 _mesa_write_shader_to_file(shader
);