1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "util/u_debug.h"
29 #include "util/u_memory.h"
30 #include "util/u_prim.h"
31 #include "pipe/p_defines.h"
32 #include "util/u_inlines.h"
33 #include "tgsi_text.h"
34 #include "tgsi_build.h"
35 #include "tgsi_info.h"
36 #include "tgsi_parse.h"
37 #include "tgsi_sanity.h"
38 #include "tgsi_util.h"
39 #include "tgsi_dump.h"
41 static boolean
is_alpha_underscore( const char *cur
)
44 (*cur
>= 'a' && *cur
<= 'z') ||
45 (*cur
>= 'A' && *cur
<= 'Z') ||
49 static boolean
is_digit( const char *cur
)
51 return *cur
>= '0' && *cur
<= '9';
54 static boolean
is_digit_alpha_underscore( const char *cur
)
56 return is_digit( cur
) || is_alpha_underscore( cur
);
59 static char uprcase( char c
)
61 if (c
>= 'a' && c
<= 'z')
67 * Ignore case of str1 and assume str1 is already uppercase.
68 * Return TRUE iff str1 and str2 are equal.
71 streq_nocase_uprcase(const char *str1
,
74 while (*str1
&& *str2
) {
75 if (*str1
!= uprcase(*str2
))
80 return *str1
== 0 && *str2
== 0;
83 static boolean
str_match_no_case( const char **pcur
, const char *str
)
85 const char *cur
= *pcur
;
87 while (*str
!= '\0' && *str
== uprcase( *cur
)) {
98 /* Eat zero or more whitespaces.
100 static void eat_opt_white( const char **pcur
)
102 while (**pcur
== ' ' || **pcur
== '\t' || **pcur
== '\n')
106 /* Eat one or more whitespaces.
107 * Return TRUE if at least one whitespace eaten.
109 static boolean
eat_white( const char **pcur
)
111 const char *cur
= *pcur
;
113 eat_opt_white( pcur
);
117 /* Parse unsigned integer.
118 * No checks for overflow.
120 static boolean
parse_uint( const char **pcur
, uint
*val
)
122 const char *cur
= *pcur
;
124 if (is_digit( cur
)) {
126 while (is_digit( cur
))
127 *val
= *val
* 10 + *cur
++ - '0';
134 static boolean
parse_identifier( const char **pcur
, char *ret
)
136 const char *cur
= *pcur
;
138 if (is_alpha_underscore( cur
)) {
140 while (is_alpha_underscore( cur
))
149 /* Parse floating point.
151 static boolean
parse_float( const char **pcur
, float *val
)
153 const char *cur
= *pcur
;
154 boolean integral_part
= FALSE
;
155 boolean fractional_part
= FALSE
;
157 *val
= (float) atof( cur
);
159 if (*cur
== '-' || *cur
== '+')
161 if (is_digit( cur
)) {
163 integral_part
= TRUE
;
164 while (is_digit( cur
))
169 if (is_digit( cur
)) {
171 fractional_part
= TRUE
;
172 while (is_digit( cur
))
176 if (!integral_part
&& !fractional_part
)
178 if (uprcase( *cur
) == 'E') {
180 if (*cur
== '-' || *cur
== '+')
182 if (is_digit( cur
)) {
184 while (is_digit( cur
))
198 struct tgsi_token
*tokens
;
199 struct tgsi_token
*tokens_cur
;
200 struct tgsi_token
*tokens_end
;
201 struct tgsi_header
*header
;
202 unsigned processor
: 4;
203 int implied_array_size
: 5;
206 static void report_error( struct translate_ctx
*ctx
, const char *msg
)
210 const char *itr
= ctx
->text
;
212 while (itr
!= ctx
->cur
) {
221 debug_printf( "\nTGSI asm error: %s [%d : %d] \n", msg
, line
, column
);
224 /* Parse shader header.
225 * Return TRUE for one of the following headers.
230 static boolean
parse_header( struct translate_ctx
*ctx
)
234 if (str_match_no_case( &ctx
->cur
, "FRAG" ))
235 processor
= TGSI_PROCESSOR_FRAGMENT
;
236 else if (str_match_no_case( &ctx
->cur
, "VERT" ))
237 processor
= TGSI_PROCESSOR_VERTEX
;
238 else if (str_match_no_case( &ctx
->cur
, "GEOM" ))
239 processor
= TGSI_PROCESSOR_GEOMETRY
;
241 report_error( ctx
, "Unknown header" );
245 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
247 ctx
->header
= (struct tgsi_header
*) ctx
->tokens_cur
++;
248 *ctx
->header
= tgsi_build_header();
250 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
252 *(struct tgsi_processor
*) ctx
->tokens_cur
++ = tgsi_build_processor( processor
, ctx
->header
);
253 ctx
->processor
= processor
;
258 static boolean
parse_label( struct translate_ctx
*ctx
, uint
*val
)
260 const char *cur
= ctx
->cur
;
262 if (parse_uint( &cur
, val
)) {
263 eat_opt_white( &cur
);
273 static const char *file_names
[TGSI_FILE_COUNT
] =
290 parse_file( const char **pcur
, uint
*file
)
294 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++) {
295 const char *cur
= *pcur
;
297 if (str_match_no_case( &cur
, file_names
[i
] )) {
298 if (!is_digit_alpha_underscore( cur
)) {
310 struct translate_ctx
*ctx
,
316 eat_opt_white( &cur
);
319 *writemask
= TGSI_WRITEMASK_NONE
;
320 eat_opt_white( &cur
);
321 if (uprcase( *cur
) == 'X') {
323 *writemask
|= TGSI_WRITEMASK_X
;
325 if (uprcase( *cur
) == 'Y') {
327 *writemask
|= TGSI_WRITEMASK_Y
;
329 if (uprcase( *cur
) == 'Z') {
331 *writemask
|= TGSI_WRITEMASK_Z
;
333 if (uprcase( *cur
) == 'W') {
335 *writemask
|= TGSI_WRITEMASK_W
;
338 if (*writemask
== TGSI_WRITEMASK_NONE
) {
339 report_error( ctx
, "Writemask expected" );
346 *writemask
= TGSI_WRITEMASK_XYZW
;
352 /* <register_file_bracket> ::= <file> `['
355 parse_register_file_bracket(
356 struct translate_ctx
*ctx
,
359 if (!parse_file( &ctx
->cur
, file
)) {
360 report_error( ctx
, "Unknown register file" );
363 eat_opt_white( &ctx
->cur
);
364 if (*ctx
->cur
!= '[') {
365 report_error( ctx
, "Expected `['" );
372 /* <register_file_bracket_index> ::= <register_file_bracket> <uint>
375 parse_register_file_bracket_index(
376 struct translate_ctx
*ctx
,
382 if (!parse_register_file_bracket( ctx
, file
))
384 eat_opt_white( &ctx
->cur
);
385 if (!parse_uint( &ctx
->cur
, &uindex
)) {
386 report_error( ctx
, "Expected literal unsigned integer" );
389 *index
= (int) uindex
;
393 /* Parse simple 1d register operand.
394 * <register_dst> ::= <register_file_bracket_index> `]'
397 parse_register_1d(struct translate_ctx
*ctx
,
401 if (!parse_register_file_bracket_index( ctx
, file
, index
))
403 eat_opt_white( &ctx
->cur
);
404 if (*ctx
->cur
!= ']') {
405 report_error( ctx
, "Expected `]'" );
412 struct parsed_bracket
{
422 parse_register_bracket(
423 struct translate_ctx
*ctx
,
424 struct parsed_bracket
*brackets
)
429 memset(brackets
, 0, sizeof(struct parsed_bracket
));
431 eat_opt_white( &ctx
->cur
);
434 if (parse_file( &cur
, &brackets
->ind_file
)) {
435 if (!parse_register_1d( ctx
, &brackets
->ind_file
,
436 &brackets
->ind_index
))
438 eat_opt_white( &ctx
->cur
);
440 if (*ctx
->cur
== '.') {
442 eat_opt_white(&ctx
->cur
);
444 switch (uprcase(*ctx
->cur
)) {
446 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
449 brackets
->ind_comp
= TGSI_SWIZZLE_Y
;
452 brackets
->ind_comp
= TGSI_SWIZZLE_Z
;
455 brackets
->ind_comp
= TGSI_SWIZZLE_W
;
458 report_error(ctx
, "Expected indirect register swizzle component `x', `y', `z' or `w'");
462 eat_opt_white(&ctx
->cur
);
465 if (*ctx
->cur
== '+' || *ctx
->cur
== '-') {
468 negate
= *ctx
->cur
== '-';
470 eat_opt_white( &ctx
->cur
);
471 if (!parse_uint( &ctx
->cur
, &uindex
)) {
472 report_error( ctx
, "Expected literal unsigned integer" );
476 brackets
->index
= -(int) uindex
;
478 brackets
->index
= (int) uindex
;
485 if (!parse_uint( &ctx
->cur
, &uindex
)) {
486 report_error( ctx
, "Expected literal unsigned integer" );
489 brackets
->index
= (int) uindex
;
490 brackets
->ind_file
= TGSI_FILE_NULL
;
491 brackets
->ind_index
= 0;
493 eat_opt_white( &ctx
->cur
);
494 if (*ctx
->cur
!= ']') {
495 report_error( ctx
, "Expected `]'" );
503 parse_opt_register_src_bracket(
504 struct translate_ctx
*ctx
,
505 struct parsed_bracket
*brackets
,
506 int *parsed_brackets
)
508 const char *cur
= ctx
->cur
;
510 *parsed_brackets
= 0;
512 eat_opt_white( &cur
);
517 if (!parse_register_bracket(ctx
, brackets
))
520 *parsed_brackets
= 1;
527 /* Parse source register operand.
528 * <register_src> ::= <register_file_bracket_index> `]' |
529 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `]' |
530 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `+' <uint> `]' |
531 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `-' <uint> `]'
535 struct translate_ctx
*ctx
,
537 struct parsed_bracket
*brackets
)
539 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
540 if (!parse_register_file_bracket( ctx
, file
))
542 if (!parse_register_bracket( ctx
, brackets
))
548 struct parsed_dcl_bracket
{
554 parse_register_dcl_bracket(
555 struct translate_ctx
*ctx
,
556 struct parsed_dcl_bracket
*bracket
)
559 memset(bracket
, 0, sizeof(struct parsed_dcl_bracket
));
561 eat_opt_white( &ctx
->cur
);
563 if (!parse_uint( &ctx
->cur
, &uindex
)) {
564 /* it can be an empty bracket [] which means its range
565 * is from 0 to some implied size */
566 if (ctx
->cur
[0] == ']' && ctx
->implied_array_size
!= 0) {
568 bracket
->last
= ctx
->implied_array_size
- 1;
571 report_error( ctx
, "Expected literal unsigned integer" );
574 bracket
->first
= uindex
;
576 eat_opt_white( &ctx
->cur
);
578 if (ctx
->cur
[0] == '.' && ctx
->cur
[1] == '.') {
582 eat_opt_white( &ctx
->cur
);
583 if (!parse_uint( &ctx
->cur
, &uindex
)) {
584 report_error( ctx
, "Expected literal integer" );
587 bracket
->last
= (int) uindex
;
588 eat_opt_white( &ctx
->cur
);
591 bracket
->last
= bracket
->first
;
595 if (*ctx
->cur
!= ']') {
596 report_error( ctx
, "Expected `]' or `..'" );
603 /* Parse register declaration.
604 * <register_dcl> ::= <register_file_bracket_index> `]' |
605 * <register_file_bracket_index> `..' <index> `]'
609 struct translate_ctx
*ctx
,
611 struct parsed_dcl_bracket
*brackets
,
618 if (!parse_register_file_bracket( ctx
, file
))
620 if (!parse_register_dcl_bracket( ctx
, &brackets
[0] ))
626 eat_opt_white( &cur
);
631 if (!parse_register_dcl_bracket( ctx
, &brackets
[1] ))
633 /* for geometry shader we don't really care about
634 * the first brackets it's always the size of the
635 * input primitive. so we want to declare just
636 * the index relevant to the semantics which is in
637 * the second bracket */
638 if (ctx
->processor
== TGSI_PROCESSOR_GEOMETRY
&& *file
== TGSI_FILE_INPUT
) {
639 brackets
[0] = brackets
[1];
650 /* Parse destination register operand.*/
653 struct translate_ctx
*ctx
,
655 struct parsed_bracket
*brackets
)
657 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
658 if (!parse_register_file_bracket( ctx
, file
))
660 if (!parse_register_bracket( ctx
, brackets
))
668 struct translate_ctx
*ctx
,
669 struct tgsi_full_dst_register
*dst
)
674 struct parsed_bracket bracket
[2];
675 int parsed_opt_brackets
;
677 if (!parse_register_dst( ctx
, &file
, &bracket
[0] ))
679 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
683 eat_opt_white( &cur
);
685 if (!parse_opt_writemask( ctx
, &writemask
))
688 dst
->Register
.File
= file
;
689 if (parsed_opt_brackets
) {
690 dst
->Register
.Dimension
= 1;
691 dst
->Dimension
.Indirect
= 0;
692 dst
->Dimension
.Dimension
= 0;
693 dst
->Dimension
.Index
= bracket
[0].index
;
694 bracket
[0] = bracket
[1];
696 dst
->Register
.Index
= bracket
[0].index
;
697 dst
->Register
.WriteMask
= writemask
;
698 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
699 dst
->Register
.Indirect
= 1;
700 dst
->Indirect
.File
= bracket
[0].ind_file
;
701 dst
->Indirect
.Index
= bracket
[0].ind_index
;
702 dst
->Indirect
.SwizzleX
= bracket
[0].ind_comp
;
703 dst
->Indirect
.SwizzleY
= bracket
[0].ind_comp
;
704 dst
->Indirect
.SwizzleZ
= bracket
[0].ind_comp
;
705 dst
->Indirect
.SwizzleW
= bracket
[0].ind_comp
;
711 parse_optional_swizzle(
712 struct translate_ctx
*ctx
,
714 boolean
*parsed_swizzle
)
716 const char *cur
= ctx
->cur
;
718 *parsed_swizzle
= FALSE
;
720 eat_opt_white( &cur
);
725 eat_opt_white( &cur
);
726 for (i
= 0; i
< 4; i
++) {
727 if (uprcase( *cur
) == 'X')
728 swizzle
[i
] = TGSI_SWIZZLE_X
;
729 else if (uprcase( *cur
) == 'Y')
730 swizzle
[i
] = TGSI_SWIZZLE_Y
;
731 else if (uprcase( *cur
) == 'Z')
732 swizzle
[i
] = TGSI_SWIZZLE_Z
;
733 else if (uprcase( *cur
) == 'W')
734 swizzle
[i
] = TGSI_SWIZZLE_W
;
736 report_error( ctx
, "Expected register swizzle component `x', `y', `z' or `w'" );
741 *parsed_swizzle
= TRUE
;
749 struct translate_ctx
*ctx
,
750 struct tgsi_full_src_register
*src
)
754 boolean parsed_swizzle
;
755 struct parsed_bracket bracket
[2];
756 int parsed_opt_brackets
;
758 if (*ctx
->cur
== '-') {
760 eat_opt_white( &ctx
->cur
);
761 src
->Register
.Negate
= 1;
764 if (*ctx
->cur
== '|') {
766 eat_opt_white( &ctx
->cur
);
767 src
->Register
.Absolute
= 1;
770 if (!parse_register_src(ctx
, &file
, &bracket
[0]))
772 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
775 src
->Register
.File
= file
;
776 if (parsed_opt_brackets
) {
777 src
->Register
.Dimension
= 1;
778 src
->Dimension
.Indirect
= 0;
779 src
->Dimension
.Dimension
= 0;
780 src
->Dimension
.Index
= bracket
[0].index
;
781 bracket
[0] = bracket
[1];
783 src
->Register
.Index
= bracket
[0].index
;
784 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
785 src
->Register
.Indirect
= 1;
786 src
->Indirect
.File
= bracket
[0].ind_file
;
787 src
->Indirect
.Index
= bracket
[0].ind_index
;
788 src
->Indirect
.SwizzleX
= bracket
[0].ind_comp
;
789 src
->Indirect
.SwizzleY
= bracket
[0].ind_comp
;
790 src
->Indirect
.SwizzleZ
= bracket
[0].ind_comp
;
791 src
->Indirect
.SwizzleW
= bracket
[0].ind_comp
;
794 /* Parse optional swizzle.
796 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
)) {
797 if (parsed_swizzle
) {
798 src
->Register
.SwizzleX
= swizzle
[0];
799 src
->Register
.SwizzleY
= swizzle
[1];
800 src
->Register
.SwizzleZ
= swizzle
[2];
801 src
->Register
.SwizzleW
= swizzle
[3];
805 if (src
->Register
.Absolute
) {
806 eat_opt_white( &ctx
->cur
);
807 if (*ctx
->cur
!= '|') {
808 report_error( ctx
, "Expected `|'" );
818 static const char *texture_names
[TGSI_TEXTURE_COUNT
] =
832 match_inst_mnemonic(const char **pcur
,
833 const struct tgsi_opcode_info
*info
)
835 if (str_match_no_case(pcur
, info
->mnemonic
)) {
843 struct translate_ctx
*ctx
,
847 uint saturate
= TGSI_SAT_NONE
;
848 const struct tgsi_opcode_info
*info
;
849 struct tgsi_full_instruction inst
;
852 inst
= tgsi_default_full_instruction();
856 eat_opt_white( &ctx
->cur
);
857 if (*ctx
->cur
== '(') {
861 boolean parsed_swizzle
;
863 inst
.Instruction
.Predicate
= 1;
866 if (*ctx
->cur
== '!') {
868 inst
.Predicate
.Negate
= 1;
871 if (!parse_register_1d( ctx
, &file
, &index
))
874 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
)) {
875 if (parsed_swizzle
) {
876 inst
.Predicate
.SwizzleX
= swizzle
[0];
877 inst
.Predicate
.SwizzleY
= swizzle
[1];
878 inst
.Predicate
.SwizzleZ
= swizzle
[2];
879 inst
.Predicate
.SwizzleW
= swizzle
[3];
883 if (*ctx
->cur
!= ')') {
884 report_error( ctx
, "Expected `)'" );
891 /* Parse instruction name.
893 eat_opt_white( &ctx
->cur
);
894 for (i
= 0; i
< TGSI_OPCODE_LAST
; i
++) {
895 const char *cur
= ctx
->cur
;
897 info
= tgsi_get_opcode_info( i
);
898 if (match_inst_mnemonic(&cur
, info
)) {
899 if (str_match_no_case( &cur
, "_SATNV" ))
900 saturate
= TGSI_SAT_MINUS_PLUS_ONE
;
901 else if (str_match_no_case( &cur
, "_SAT" ))
902 saturate
= TGSI_SAT_ZERO_ONE
;
904 if (info
->num_dst
+ info
->num_src
+ info
->is_tex
== 0) {
905 if (!is_digit_alpha_underscore( cur
)) {
910 else if (*cur
== '\0' || eat_white( &cur
)) {
916 if (i
== TGSI_OPCODE_LAST
) {
918 report_error( ctx
, "Unknown opcode" );
920 report_error( ctx
, "Expected `DCL', `IMM' or a label" );
924 inst
.Instruction
.Opcode
= i
;
925 inst
.Instruction
.Saturate
= saturate
;
926 inst
.Instruction
.NumDstRegs
= info
->num_dst
;
927 inst
.Instruction
.NumSrcRegs
= info
->num_src
;
929 /* Parse instruction operands.
931 for (i
= 0; i
< info
->num_dst
+ info
->num_src
+ info
->is_tex
; i
++) {
933 eat_opt_white( &ctx
->cur
);
934 if (*ctx
->cur
!= ',') {
935 report_error( ctx
, "Expected `,'" );
939 eat_opt_white( &ctx
->cur
);
942 if (i
< info
->num_dst
) {
943 if (!parse_dst_operand( ctx
, &inst
.Dst
[i
] ))
946 else if (i
< info
->num_dst
+ info
->num_src
) {
947 if (!parse_src_operand( ctx
, &inst
.Src
[i
- info
->num_dst
] ))
953 for (j
= 0; j
< TGSI_TEXTURE_COUNT
; j
++) {
954 if (str_match_no_case( &ctx
->cur
, texture_names
[j
] )) {
955 if (!is_digit_alpha_underscore( ctx
->cur
)) {
956 inst
.Instruction
.Texture
= 1;
957 inst
.Texture
.Texture
= j
;
962 if (j
== TGSI_TEXTURE_COUNT
) {
963 report_error( ctx
, "Expected texture target" );
969 if (info
->is_branch
) {
972 eat_opt_white( &ctx
->cur
);
973 if (*ctx
->cur
!= ':') {
974 report_error( ctx
, "Expected `:'" );
978 eat_opt_white( &ctx
->cur
);
979 if (!parse_uint( &ctx
->cur
, &target
)) {
980 report_error( ctx
, "Expected a label" );
983 inst
.Instruction
.Label
= 1;
984 inst
.Label
.Label
= target
;
987 advance
= tgsi_build_full_instruction(
991 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
994 ctx
->tokens_cur
+= advance
;
999 static const char *semantic_names
[TGSI_SEMANTIC_COUNT
] =
1015 static const char *interpolate_names
[TGSI_INTERPOLATE_COUNT
] =
1023 /* parses a 4-touple of the form {x, y, z, w}
1024 * where x, y, z, w are numbers */
1025 static boolean
parse_immediate_data(struct translate_ctx
*ctx
,
1030 eat_opt_white( &ctx
->cur
);
1031 if (*ctx
->cur
!= '{') {
1032 report_error( ctx
, "Expected `{'" );
1036 for (i
= 0; i
< 4; i
++) {
1037 eat_opt_white( &ctx
->cur
);
1039 if (*ctx
->cur
!= ',') {
1040 report_error( ctx
, "Expected `,'" );
1044 eat_opt_white( &ctx
->cur
);
1046 if (!parse_float( &ctx
->cur
, &values
[i
] )) {
1047 report_error( ctx
, "Expected literal floating point" );
1051 eat_opt_white( &ctx
->cur
);
1052 if (*ctx
->cur
!= '}') {
1053 report_error( ctx
, "Expected `}'" );
1061 static boolean
parse_declaration( struct translate_ctx
*ctx
)
1063 struct tgsi_full_declaration decl
;
1065 struct parsed_dcl_bracket brackets
[2];
1070 boolean is_vs_input
;
1071 boolean is_imm_array
;
1073 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
1074 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
1076 if (!eat_white( &ctx
->cur
)) {
1077 report_error( ctx
, "Syntax error" );
1080 if (!parse_register_dcl( ctx
, &file
, brackets
, &num_brackets
))
1082 if (!parse_opt_writemask( ctx
, &writemask
))
1085 decl
= tgsi_default_full_declaration();
1086 decl
.Declaration
.File
= file
;
1087 decl
.Declaration
.UsageMask
= writemask
;
1089 if (num_brackets
== 1) {
1090 decl
.Range
.First
= brackets
[0].first
;
1091 decl
.Range
.Last
= brackets
[0].last
;
1093 decl
.Range
.First
= brackets
[1].first
;
1094 decl
.Range
.Last
= brackets
[1].last
;
1096 decl
.Declaration
.Dimension
= 1;
1097 decl
.Dim
.Index2D
= brackets
[0].first
;
1100 is_vs_input
= (file
== TGSI_FILE_INPUT
&&
1101 ctx
->processor
== TGSI_PROCESSOR_VERTEX
);
1102 is_imm_array
= (file
== TGSI_FILE_IMMEDIATE_ARRAY
);
1105 eat_opt_white( &cur
);
1106 if (*cur
== ',' && !is_vs_input
) {
1110 eat_opt_white( &cur
);
1111 for (i
= 0; i
< TGSI_SEMANTIC_COUNT
; i
++) {
1112 if (str_match_no_case( &cur
, semantic_names
[i
] )) {
1113 const char *cur2
= cur
;
1116 if (is_digit_alpha_underscore( cur
))
1118 eat_opt_white( &cur2
);
1121 eat_opt_white( &cur2
);
1122 if (!parse_uint( &cur2
, &index
)) {
1123 report_error( ctx
, "Expected literal integer" );
1126 eat_opt_white( &cur2
);
1128 report_error( ctx
, "Expected `]'" );
1133 decl
.Semantic
.Index
= index
;
1138 decl
.Declaration
.Semantic
= 1;
1139 decl
.Semantic
.Name
= i
;
1145 } else if (is_imm_array
) {
1148 /* we have our immediate data */
1150 report_error( ctx
, "Immediate array without data" );
1156 decl
.ImmediateData
.u
=
1157 MALLOC(sizeof(union tgsi_immediate_data
) * 4 *
1158 (decl
.Range
.Last
+ 1));
1159 vals_itr
= (float*)decl
.ImmediateData
.u
;
1160 for (i
= 0; i
<= decl
.Range
.Last
; ++i
) {
1161 if (!parse_immediate_data(ctx
, vals_itr
)) {
1162 FREE(decl
.ImmediateData
.u
);
1166 eat_opt_white( &ctx
->cur
);
1167 if (*ctx
->cur
!= ',') {
1168 if (i
!= decl
.Range
.Last
) {
1169 report_error( ctx
, "Not enough data in immediate array!" );
1170 FREE(decl
.ImmediateData
.u
);
1176 eat_opt_white( &ctx
->cur
);
1177 if (*ctx
->cur
!= '}') {
1178 FREE(decl
.ImmediateData
.u
);
1179 report_error( ctx
, "Immediate array data missing closing '}'" );
1186 eat_opt_white( &cur
);
1187 if (*cur
== ',' && !is_vs_input
) {
1191 eat_opt_white( &cur
);
1192 for (i
= 0; i
< TGSI_INTERPOLATE_COUNT
; i
++) {
1193 if (str_match_no_case( &cur
, interpolate_names
[i
] )) {
1194 if (is_digit_alpha_underscore( cur
))
1196 decl
.Declaration
.Interpolate
= i
;
1202 if (i
== TGSI_INTERPOLATE_COUNT
) {
1203 report_error( ctx
, "Expected semantic or interpolate attribute" );
1208 advance
= tgsi_build_full_declaration(
1212 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1215 FREE(decl
.ImmediateData
.u
);
1219 ctx
->tokens_cur
+= advance
;
1224 static boolean
parse_immediate( struct translate_ctx
*ctx
)
1226 struct tgsi_full_immediate imm
;
1230 if (!eat_white( &ctx
->cur
)) {
1231 report_error( ctx
, "Syntax error" );
1234 if (!str_match_no_case( &ctx
->cur
, "FLT32" ) ||
1235 is_digit_alpha_underscore( ctx
->cur
)) {
1236 report_error( ctx
, "Expected `FLT32'" );
1240 parse_immediate_data(ctx
, values
);
1242 imm
= tgsi_default_full_immediate();
1243 imm
.Immediate
.NrTokens
+= 4;
1244 imm
.Immediate
.DataType
= TGSI_IMM_FLOAT32
;
1245 imm
.u
[0].Float
= values
[0];
1246 imm
.u
[1].Float
= values
[1];
1247 imm
.u
[2].Float
= values
[2];
1248 imm
.u
[3].Float
= values
[3];
1250 advance
= tgsi_build_full_immediate(
1254 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1257 ctx
->tokens_cur
+= advance
;
1263 parse_primitive( const char **pcur
, uint
*primitive
)
1267 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++) {
1268 const char *cur
= *pcur
;
1270 if (str_match_no_case( &cur
, tgsi_primitive_names
[i
])) {
1280 parse_fs_coord_origin( const char **pcur
, uint
*fs_coord_origin
)
1284 for (i
= 0; i
< Elements(tgsi_fs_coord_origin_names
); i
++) {
1285 const char *cur
= *pcur
;
1287 if (str_match_no_case( &cur
, tgsi_fs_coord_origin_names
[i
])) {
1288 *fs_coord_origin
= i
;
1297 parse_fs_coord_pixel_center( const char **pcur
, uint
*fs_coord_pixel_center
)
1301 for (i
= 0; i
< Elements(tgsi_fs_coord_pixel_center_names
); i
++) {
1302 const char *cur
= *pcur
;
1304 if (str_match_no_case( &cur
, tgsi_fs_coord_pixel_center_names
[i
])) {
1305 *fs_coord_pixel_center
= i
;
1314 static boolean
parse_property( struct translate_ctx
*ctx
)
1316 struct tgsi_full_property prop
;
1322 if (!eat_white( &ctx
->cur
)) {
1323 report_error( ctx
, "Syntax error" );
1326 if (!parse_identifier( &ctx
->cur
, id
)) {
1327 report_error( ctx
, "Syntax error" );
1330 for (property_name
= 0; property_name
< TGSI_PROPERTY_COUNT
;
1332 if (streq_nocase_uprcase(tgsi_property_names
[property_name
], id
)) {
1336 if (property_name
>= TGSI_PROPERTY_COUNT
) {
1337 debug_printf( "\nError: Unknown property : '%s'", id
);
1341 eat_opt_white( &ctx
->cur
);
1342 switch(property_name
) {
1343 case TGSI_PROPERTY_GS_INPUT_PRIM
:
1344 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
1345 if (!parse_primitive(&ctx
->cur
, &values
[0] )) {
1346 report_error( ctx
, "Unknown primitive name as property!" );
1349 if (property_name
== TGSI_PROPERTY_GS_INPUT_PRIM
&&
1350 ctx
->processor
== TGSI_PROCESSOR_GEOMETRY
) {
1351 ctx
->implied_array_size
= u_vertices_per_prim(values
[0]);
1354 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
1355 if (!parse_fs_coord_origin(&ctx
->cur
, &values
[0] )) {
1356 report_error( ctx
, "Unknown coord origin as property: must be UPPER_LEFT or LOWER_LEFT!" );
1360 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
1361 if (!parse_fs_coord_pixel_center(&ctx
->cur
, &values
[0] )) {
1362 report_error( ctx
, "Unknown coord pixel center as property: must be HALF_INTEGER or INTEGER!" );
1366 case TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
:
1368 if (!parse_uint(&ctx
->cur
, &values
[0] )) {
1369 report_error( ctx
, "Expected unsigned integer as property!" );
1374 prop
= tgsi_default_full_property();
1375 prop
.Property
.PropertyName
= property_name
;
1376 prop
.Property
.NrTokens
+= 1;
1377 prop
.u
[0].Data
= values
[0];
1379 advance
= tgsi_build_full_property(
1383 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1386 ctx
->tokens_cur
+= advance
;
1392 static boolean
translate( struct translate_ctx
*ctx
)
1394 eat_opt_white( &ctx
->cur
);
1395 if (!parse_header( ctx
))
1398 while (*ctx
->cur
!= '\0') {
1400 if (!eat_white( &ctx
->cur
)) {
1401 report_error( ctx
, "Syntax error" );
1405 if (*ctx
->cur
== '\0')
1407 if (parse_label( ctx
, &label_val
)) {
1408 if (!parse_instruction( ctx
, TRUE
))
1411 else if (str_match_no_case( &ctx
->cur
, "DCL" )) {
1412 if (!parse_declaration( ctx
))
1415 else if (str_match_no_case( &ctx
->cur
, "IMM" )) {
1416 if (!parse_immediate( ctx
))
1419 else if (str_match_no_case( &ctx
->cur
, "PROPERTY" )) {
1420 if (!parse_property( ctx
))
1423 else if (!parse_instruction( ctx
, FALSE
)) {
1432 tgsi_text_translate(
1434 struct tgsi_token
*tokens
,
1437 struct translate_ctx ctx
;
1441 ctx
.tokens
= tokens
;
1442 ctx
.tokens_cur
= tokens
;
1443 ctx
.tokens_end
= tokens
+ num_tokens
;
1445 if (!translate( &ctx
))
1448 return tgsi_sanity_check( tokens
);