1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
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 VMWARE 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_strings.h"
39 #include "tgsi_util.h"
40 #include "tgsi_dump.h"
42 static boolean
is_alpha_underscore( const char *cur
)
45 (*cur
>= 'a' && *cur
<= 'z') ||
46 (*cur
>= 'A' && *cur
<= 'Z') ||
50 static boolean
is_digit( const char *cur
)
52 return *cur
>= '0' && *cur
<= '9';
55 static boolean
is_digit_alpha_underscore( const char *cur
)
57 return is_digit( cur
) || is_alpha_underscore( cur
);
60 static char uprcase( char c
)
62 if (c
>= 'a' && c
<= 'z')
68 * Ignore case of str1 and assume str1 is already uppercase.
69 * Return TRUE iff str1 and str2 are equal.
72 streq_nocase_uprcase(const char *str1
,
75 while (*str1
&& *str2
) {
76 if (*str1
!= uprcase(*str2
))
81 return *str1
== 0 && *str2
== 0;
84 /* Return TRUE if both strings match.
85 * The second string is terminated by zero.
86 * The pointer to the first string is moved at end of the read word
89 static boolean
str_match_no_case( const char **pcur
, const char *str
)
91 const char *cur
= *pcur
;
93 while (*str
!= '\0' && *str
== uprcase( *cur
)) {
104 /* Return TRUE if both strings match.
105 * The first string is be terminated by a non-digit non-letter non-underscore
106 * character, the second string is terminated by zero.
107 * The pointer to the first string is moved at end of the read word
110 static boolean
str_match_nocase_whole( const char **pcur
, const char *str
)
112 const char *cur
= *pcur
;
114 if (str_match_no_case(&cur
, str
) &&
115 !is_digit_alpha_underscore(cur
)) {
122 /* Eat zero or more whitespaces.
124 static void eat_opt_white( const char **pcur
)
126 while (**pcur
== ' ' || **pcur
== '\t' || **pcur
== '\n')
130 /* Eat one or more whitespaces.
131 * Return TRUE if at least one whitespace eaten.
133 static boolean
eat_white( const char **pcur
)
135 const char *cur
= *pcur
;
137 eat_opt_white( pcur
);
141 /* Parse unsigned integer.
142 * No checks for overflow.
144 static boolean
parse_uint( const char **pcur
, uint
*val
)
146 const char *cur
= *pcur
;
148 if (is_digit( cur
)) {
150 while (is_digit( cur
))
151 *val
= *val
* 10 + *cur
++ - '0';
158 static boolean
parse_int( const char **pcur
, int *val
)
160 const char *cur
= *pcur
;
161 int sign
= (*cur
== '-' ? -1 : 1);
163 if (*cur
== '+' || *cur
== '-')
166 if (parse_uint(&cur
, (uint
*)val
)) {
175 static boolean
parse_identifier( const char **pcur
, char *ret
)
177 const char *cur
= *pcur
;
179 if (is_alpha_underscore( cur
)) {
181 while (is_alpha_underscore( cur
) || is_digit( cur
))
190 /* Parse floating point.
192 static boolean
parse_float( const char **pcur
, float *val
)
194 const char *cur
= *pcur
;
195 boolean integral_part
= FALSE
;
196 boolean fractional_part
= FALSE
;
198 *val
= (float) atof( cur
);
200 if (*cur
== '-' || *cur
== '+')
202 if (is_digit( cur
)) {
204 integral_part
= TRUE
;
205 while (is_digit( cur
))
210 if (is_digit( cur
)) {
212 fractional_part
= TRUE
;
213 while (is_digit( cur
))
217 if (!integral_part
&& !fractional_part
)
219 if (uprcase( *cur
) == 'E') {
221 if (*cur
== '-' || *cur
== '+')
223 if (is_digit( cur
)) {
225 while (is_digit( cur
))
235 static boolean
parse_double( const char **pcur
, uint32_t *val0
, uint32_t *val1
)
237 const char *cur
= *pcur
;
243 v
.dval
= strtod(cur
, pcur
);
257 struct tgsi_token
*tokens
;
258 struct tgsi_token
*tokens_cur
;
259 struct tgsi_token
*tokens_end
;
260 struct tgsi_header
*header
;
261 unsigned processor
: 4;
262 int implied_array_size
: 5;
263 unsigned num_immediates
;
266 static void report_error( struct translate_ctx
*ctx
, const char *msg
)
270 const char *itr
= ctx
->text
;
272 while (itr
!= ctx
->cur
) {
281 debug_printf( "\nTGSI asm error: %s [%d : %d] \n", msg
, line
, column
);
284 /* Parse shader header.
285 * Return TRUE for one of the following headers.
290 static boolean
parse_header( struct translate_ctx
*ctx
)
294 if (str_match_nocase_whole( &ctx
->cur
, "FRAG" ))
295 processor
= TGSI_PROCESSOR_FRAGMENT
;
296 else if (str_match_nocase_whole( &ctx
->cur
, "VERT" ))
297 processor
= TGSI_PROCESSOR_VERTEX
;
298 else if (str_match_nocase_whole( &ctx
->cur
, "GEOM" ))
299 processor
= TGSI_PROCESSOR_GEOMETRY
;
300 else if (str_match_nocase_whole( &ctx
->cur
, "COMP" ))
301 processor
= TGSI_PROCESSOR_COMPUTE
;
303 report_error( ctx
, "Unknown header" );
307 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
309 ctx
->header
= (struct tgsi_header
*) ctx
->tokens_cur
++;
310 *ctx
->header
= tgsi_build_header();
312 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
314 *(struct tgsi_processor
*) ctx
->tokens_cur
++ = tgsi_build_processor( processor
, ctx
->header
);
315 ctx
->processor
= processor
;
320 static boolean
parse_label( struct translate_ctx
*ctx
, uint
*val
)
322 const char *cur
= ctx
->cur
;
324 if (parse_uint( &cur
, val
)) {
325 eat_opt_white( &cur
);
336 parse_file( const char **pcur
, uint
*file
)
340 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++) {
341 const char *cur
= *pcur
;
343 if (str_match_nocase_whole( &cur
, tgsi_file_name(i
) )) {
354 struct translate_ctx
*ctx
,
360 eat_opt_white( &cur
);
363 *writemask
= TGSI_WRITEMASK_NONE
;
364 eat_opt_white( &cur
);
365 if (uprcase( *cur
) == 'X') {
367 *writemask
|= TGSI_WRITEMASK_X
;
369 if (uprcase( *cur
) == 'Y') {
371 *writemask
|= TGSI_WRITEMASK_Y
;
373 if (uprcase( *cur
) == 'Z') {
375 *writemask
|= TGSI_WRITEMASK_Z
;
377 if (uprcase( *cur
) == 'W') {
379 *writemask
|= TGSI_WRITEMASK_W
;
382 if (*writemask
== TGSI_WRITEMASK_NONE
) {
383 report_error( ctx
, "Writemask expected" );
390 *writemask
= TGSI_WRITEMASK_XYZW
;
396 /* <register_file_bracket> ::= <file> `['
399 parse_register_file_bracket(
400 struct translate_ctx
*ctx
,
403 if (!parse_file( &ctx
->cur
, file
)) {
404 report_error( ctx
, "Unknown register file" );
407 eat_opt_white( &ctx
->cur
);
408 if (*ctx
->cur
!= '[') {
409 report_error( ctx
, "Expected `['" );
416 /* <register_file_bracket_index> ::= <register_file_bracket> <uint>
419 parse_register_file_bracket_index(
420 struct translate_ctx
*ctx
,
426 if (!parse_register_file_bracket( ctx
, file
))
428 eat_opt_white( &ctx
->cur
);
429 if (!parse_uint( &ctx
->cur
, &uindex
)) {
430 report_error( ctx
, "Expected literal unsigned integer" );
433 *index
= (int) uindex
;
437 /* Parse simple 1d register operand.
438 * <register_dst> ::= <register_file_bracket_index> `]'
441 parse_register_1d(struct translate_ctx
*ctx
,
445 if (!parse_register_file_bracket_index( ctx
, file
, index
))
447 eat_opt_white( &ctx
->cur
);
448 if (*ctx
->cur
!= ']') {
449 report_error( ctx
, "Expected `]'" );
456 struct parsed_bracket
{
467 parse_register_bracket(
468 struct translate_ctx
*ctx
,
469 struct parsed_bracket
*brackets
)
474 memset(brackets
, 0, sizeof(struct parsed_bracket
));
476 eat_opt_white( &ctx
->cur
);
479 if (parse_file( &cur
, &brackets
->ind_file
)) {
480 if (!parse_register_1d( ctx
, &brackets
->ind_file
,
481 &brackets
->ind_index
))
483 eat_opt_white( &ctx
->cur
);
485 if (*ctx
->cur
== '.') {
487 eat_opt_white(&ctx
->cur
);
489 switch (uprcase(*ctx
->cur
)) {
491 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
494 brackets
->ind_comp
= TGSI_SWIZZLE_Y
;
497 brackets
->ind_comp
= TGSI_SWIZZLE_Z
;
500 brackets
->ind_comp
= TGSI_SWIZZLE_W
;
503 report_error(ctx
, "Expected indirect register swizzle component `x', `y', `z' or `w'");
507 eat_opt_white(&ctx
->cur
);
510 if (*ctx
->cur
== '+' || *ctx
->cur
== '-')
511 parse_int( &ctx
->cur
, &brackets
->index
);
516 if (!parse_uint( &ctx
->cur
, &uindex
)) {
517 report_error( ctx
, "Expected literal unsigned integer" );
520 brackets
->index
= (int) uindex
;
521 brackets
->ind_file
= TGSI_FILE_NULL
;
522 brackets
->ind_index
= 0;
524 eat_opt_white( &ctx
->cur
);
525 if (*ctx
->cur
!= ']') {
526 report_error( ctx
, "Expected `]'" );
530 if (*ctx
->cur
== '(') {
532 eat_opt_white( &ctx
->cur
);
533 if (!parse_uint( &ctx
->cur
, &brackets
->ind_array
)) {
534 report_error( ctx
, "Expected literal unsigned integer" );
537 eat_opt_white( &ctx
->cur
);
538 if (*ctx
->cur
!= ')') {
539 report_error( ctx
, "Expected `)'" );
548 parse_opt_register_src_bracket(
549 struct translate_ctx
*ctx
,
550 struct parsed_bracket
*brackets
,
551 int *parsed_brackets
)
553 const char *cur
= ctx
->cur
;
555 *parsed_brackets
= 0;
557 eat_opt_white( &cur
);
562 if (!parse_register_bracket(ctx
, brackets
))
565 *parsed_brackets
= 1;
572 /* Parse source register operand.
573 * <register_src> ::= <register_file_bracket_index> `]' |
574 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `]' |
575 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `+' <uint> `]' |
576 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `-' <uint> `]'
580 struct translate_ctx
*ctx
,
582 struct parsed_bracket
*brackets
)
584 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
585 if (!parse_register_file_bracket( ctx
, file
))
587 if (!parse_register_bracket( ctx
, brackets
))
593 struct parsed_dcl_bracket
{
599 parse_register_dcl_bracket(
600 struct translate_ctx
*ctx
,
601 struct parsed_dcl_bracket
*bracket
)
604 memset(bracket
, 0, sizeof(struct parsed_dcl_bracket
));
606 eat_opt_white( &ctx
->cur
);
608 if (!parse_uint( &ctx
->cur
, &uindex
)) {
609 /* it can be an empty bracket [] which means its range
610 * is from 0 to some implied size */
611 if (ctx
->cur
[0] == ']' && ctx
->implied_array_size
!= 0) {
613 bracket
->last
= ctx
->implied_array_size
- 1;
616 report_error( ctx
, "Expected literal unsigned integer" );
619 bracket
->first
= uindex
;
621 eat_opt_white( &ctx
->cur
);
623 if (ctx
->cur
[0] == '.' && ctx
->cur
[1] == '.') {
627 eat_opt_white( &ctx
->cur
);
628 if (!parse_uint( &ctx
->cur
, &uindex
)) {
629 report_error( ctx
, "Expected literal integer" );
632 bracket
->last
= (int) uindex
;
633 eat_opt_white( &ctx
->cur
);
636 bracket
->last
= bracket
->first
;
640 if (*ctx
->cur
!= ']') {
641 report_error( ctx
, "Expected `]' or `..'" );
648 /* Parse register declaration.
649 * <register_dcl> ::= <register_file_bracket_index> `]' |
650 * <register_file_bracket_index> `..' <index> `]'
654 struct translate_ctx
*ctx
,
656 struct parsed_dcl_bracket
*brackets
,
663 if (!parse_register_file_bracket( ctx
, file
))
665 if (!parse_register_dcl_bracket( ctx
, &brackets
[0] ))
671 eat_opt_white( &cur
);
676 if (!parse_register_dcl_bracket( ctx
, &brackets
[1] ))
678 /* for geometry shader we don't really care about
679 * the first brackets it's always the size of the
680 * input primitive. so we want to declare just
681 * the index relevant to the semantics which is in
682 * the second bracket */
683 if (ctx
->processor
== TGSI_PROCESSOR_GEOMETRY
&& *file
== TGSI_FILE_INPUT
) {
684 brackets
[0] = brackets
[1];
695 /* Parse destination register operand.*/
698 struct translate_ctx
*ctx
,
700 struct parsed_bracket
*brackets
)
702 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
703 if (!parse_register_file_bracket( ctx
, file
))
705 if (!parse_register_bracket( ctx
, brackets
))
713 struct translate_ctx
*ctx
,
714 struct tgsi_full_dst_register
*dst
)
719 struct parsed_bracket bracket
[2];
720 int parsed_opt_brackets
;
722 if (!parse_register_dst( ctx
, &file
, &bracket
[0] ))
724 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
728 eat_opt_white( &cur
);
730 if (!parse_opt_writemask( ctx
, &writemask
))
733 dst
->Register
.File
= file
;
734 if (parsed_opt_brackets
) {
735 dst
->Register
.Dimension
= 1;
736 dst
->Dimension
.Indirect
= 0;
737 dst
->Dimension
.Dimension
= 0;
738 dst
->Dimension
.Index
= bracket
[0].index
;
739 bracket
[0] = bracket
[1];
741 dst
->Register
.Index
= bracket
[0].index
;
742 dst
->Register
.WriteMask
= writemask
;
743 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
744 dst
->Register
.Indirect
= 1;
745 dst
->Indirect
.File
= bracket
[0].ind_file
;
746 dst
->Indirect
.Index
= bracket
[0].ind_index
;
747 dst
->Indirect
.Swizzle
= bracket
[0].ind_comp
;
748 dst
->Indirect
.ArrayID
= bracket
[0].ind_array
;
754 parse_optional_swizzle(
755 struct translate_ctx
*ctx
,
757 boolean
*parsed_swizzle
,
760 const char *cur
= ctx
->cur
;
762 *parsed_swizzle
= FALSE
;
764 eat_opt_white( &cur
);
769 eat_opt_white( &cur
);
770 for (i
= 0; i
< components
; i
++) {
771 if (uprcase( *cur
) == 'X')
772 swizzle
[i
] = TGSI_SWIZZLE_X
;
773 else if (uprcase( *cur
) == 'Y')
774 swizzle
[i
] = TGSI_SWIZZLE_Y
;
775 else if (uprcase( *cur
) == 'Z')
776 swizzle
[i
] = TGSI_SWIZZLE_Z
;
777 else if (uprcase( *cur
) == 'W')
778 swizzle
[i
] = TGSI_SWIZZLE_W
;
780 report_error( ctx
, "Expected register swizzle component `x', `y', `z' or `w'" );
785 *parsed_swizzle
= TRUE
;
793 struct translate_ctx
*ctx
,
794 struct tgsi_full_src_register
*src
)
798 boolean parsed_swizzle
;
799 struct parsed_bracket bracket
[2];
800 int parsed_opt_brackets
;
802 if (*ctx
->cur
== '-') {
804 eat_opt_white( &ctx
->cur
);
805 src
->Register
.Negate
= 1;
808 if (*ctx
->cur
== '|') {
810 eat_opt_white( &ctx
->cur
);
811 src
->Register
.Absolute
= 1;
814 if (!parse_register_src(ctx
, &file
, &bracket
[0]))
816 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
819 src
->Register
.File
= file
;
820 if (parsed_opt_brackets
) {
821 src
->Register
.Dimension
= 1;
822 src
->Dimension
.Indirect
= 0;
823 src
->Dimension
.Dimension
= 0;
824 src
->Dimension
.Index
= bracket
[0].index
;
825 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
826 src
->Dimension
.Indirect
= 1;
827 src
->DimIndirect
.File
= bracket
[0].ind_file
;
828 src
->DimIndirect
.Index
= bracket
[0].ind_index
;
829 src
->DimIndirect
.Swizzle
= bracket
[0].ind_comp
;
830 src
->DimIndirect
.ArrayID
= bracket
[0].ind_array
;
832 bracket
[0] = bracket
[1];
834 src
->Register
.Index
= bracket
[0].index
;
835 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
836 src
->Register
.Indirect
= 1;
837 src
->Indirect
.File
= bracket
[0].ind_file
;
838 src
->Indirect
.Index
= bracket
[0].ind_index
;
839 src
->Indirect
.Swizzle
= bracket
[0].ind_comp
;
840 src
->Indirect
.ArrayID
= bracket
[0].ind_array
;
843 /* Parse optional swizzle.
845 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
, 4 )) {
846 if (parsed_swizzle
) {
847 src
->Register
.SwizzleX
= swizzle
[0];
848 src
->Register
.SwizzleY
= swizzle
[1];
849 src
->Register
.SwizzleZ
= swizzle
[2];
850 src
->Register
.SwizzleW
= swizzle
[3];
854 if (src
->Register
.Absolute
) {
855 eat_opt_white( &ctx
->cur
);
856 if (*ctx
->cur
!= '|') {
857 report_error( ctx
, "Expected `|'" );
868 parse_texoffset_operand(
869 struct translate_ctx
*ctx
,
870 struct tgsi_texture_offset
*src
)
874 boolean parsed_swizzle
;
875 struct parsed_bracket bracket
;
877 if (!parse_register_src(ctx
, &file
, &bracket
))
881 src
->Index
= bracket
.index
;
883 /* Parse optional swizzle.
885 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
, 3 )) {
886 if (parsed_swizzle
) {
887 src
->SwizzleX
= swizzle
[0];
888 src
->SwizzleY
= swizzle
[1];
889 src
->SwizzleZ
= swizzle
[2];
897 match_inst(const char **pcur
,
899 const struct tgsi_opcode_info
*info
)
901 const char *cur
= *pcur
;
903 /* simple case: the whole string matches the instruction name */
904 if (str_match_nocase_whole(&cur
, info
->mnemonic
)) {
906 *saturate
= TGSI_SAT_NONE
;
910 if (str_match_no_case(&cur
, info
->mnemonic
)) {
911 /* the instruction has a suffix, figure it out */
912 if (str_match_nocase_whole(&cur
, "_SAT")) {
914 *saturate
= TGSI_SAT_ZERO_ONE
;
918 if (str_match_nocase_whole(&cur
, "_SATNV")) {
920 *saturate
= TGSI_SAT_MINUS_PLUS_ONE
;
930 struct translate_ctx
*ctx
,
934 uint saturate
= TGSI_SAT_NONE
;
935 const struct tgsi_opcode_info
*info
;
936 struct tgsi_full_instruction inst
;
940 inst
= tgsi_default_full_instruction();
944 eat_opt_white( &ctx
->cur
);
945 if (*ctx
->cur
== '(') {
949 boolean parsed_swizzle
;
951 inst
.Instruction
.Predicate
= 1;
954 if (*ctx
->cur
== '!') {
956 inst
.Predicate
.Negate
= 1;
959 if (!parse_register_1d( ctx
, &file
, &index
))
962 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
, 4 )) {
963 if (parsed_swizzle
) {
964 inst
.Predicate
.SwizzleX
= swizzle
[0];
965 inst
.Predicate
.SwizzleY
= swizzle
[1];
966 inst
.Predicate
.SwizzleZ
= swizzle
[2];
967 inst
.Predicate
.SwizzleW
= swizzle
[3];
971 if (*ctx
->cur
!= ')') {
972 report_error( ctx
, "Expected `)'" );
979 /* Parse instruction name.
981 eat_opt_white( &ctx
->cur
);
982 for (i
= 0; i
< TGSI_OPCODE_LAST
; i
++) {
985 info
= tgsi_get_opcode_info( i
);
986 if (match_inst(&cur
, &saturate
, info
)) {
987 if (info
->num_dst
+ info
->num_src
+ info
->is_tex
== 0) {
991 else if (*cur
== '\0' || eat_white( &cur
)) {
997 if (i
== TGSI_OPCODE_LAST
) {
999 report_error( ctx
, "Unknown opcode" );
1001 report_error( ctx
, "Expected `DCL', `IMM' or a label" );
1005 inst
.Instruction
.Opcode
= i
;
1006 inst
.Instruction
.Saturate
= saturate
;
1007 inst
.Instruction
.NumDstRegs
= info
->num_dst
;
1008 inst
.Instruction
.NumSrcRegs
= info
->num_src
;
1010 if (i
>= TGSI_OPCODE_SAMPLE
&& i
<= TGSI_OPCODE_GATHER4
) {
1012 * These are not considered tex opcodes here (no additional
1013 * target argument) however we're required to set the Texture
1014 * bit so we can set the number of tex offsets.
1016 inst
.Instruction
.Texture
= 1;
1017 inst
.Texture
.Texture
= TGSI_TEXTURE_UNKNOWN
;
1020 /* Parse instruction operands.
1022 for (i
= 0; i
< info
->num_dst
+ info
->num_src
+ info
->is_tex
; i
++) {
1024 eat_opt_white( &ctx
->cur
);
1025 if (*ctx
->cur
!= ',') {
1026 report_error( ctx
, "Expected `,'" );
1030 eat_opt_white( &ctx
->cur
);
1033 if (i
< info
->num_dst
) {
1034 if (!parse_dst_operand( ctx
, &inst
.Dst
[i
] ))
1037 else if (i
< info
->num_dst
+ info
->num_src
) {
1038 if (!parse_src_operand( ctx
, &inst
.Src
[i
- info
->num_dst
] ))
1044 for (j
= 0; j
< TGSI_TEXTURE_COUNT
; j
++) {
1045 if (str_match_nocase_whole( &ctx
->cur
, tgsi_texture_names
[j
] )) {
1046 inst
.Instruction
.Texture
= 1;
1047 inst
.Texture
.Texture
= j
;
1051 if (j
== TGSI_TEXTURE_COUNT
) {
1052 report_error( ctx
, "Expected texture target" );
1059 eat_opt_white( &cur
);
1060 for (i
= 0; inst
.Instruction
.Texture
&& *cur
== ','; i
++) {
1062 eat_opt_white( &cur
);
1064 if (!parse_texoffset_operand( ctx
, &inst
.TexOffsets
[i
] ))
1067 eat_opt_white( &cur
);
1069 inst
.Texture
.NumOffsets
= i
;
1072 eat_opt_white( &cur
);
1073 if (info
->is_branch
&& *cur
== ':') {
1077 eat_opt_white( &cur
);
1078 if (!parse_uint( &cur
, &target
)) {
1079 report_error( ctx
, "Expected a label" );
1082 inst
.Instruction
.Label
= 1;
1083 inst
.Label
.Label
= target
;
1087 advance
= tgsi_build_full_instruction(
1091 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1094 ctx
->tokens_cur
+= advance
;
1099 /* parses a 4-touple of the form {x, y, z, w}
1100 * where x, y, z, w are numbers */
1101 static boolean
parse_immediate_data(struct translate_ctx
*ctx
, unsigned type
,
1102 union tgsi_immediate_data
*values
)
1107 eat_opt_white( &ctx
->cur
);
1108 if (*ctx
->cur
!= '{') {
1109 report_error( ctx
, "Expected `{'" );
1113 for (i
= 0; i
< 4; i
++) {
1114 eat_opt_white( &ctx
->cur
);
1116 if (*ctx
->cur
!= ',') {
1117 report_error( ctx
, "Expected `,'" );
1121 eat_opt_white( &ctx
->cur
);
1125 case TGSI_IMM_FLOAT64
:
1126 ret
= parse_double(&ctx
->cur
, &values
[i
].Uint
, &values
[i
+1].Uint
);
1129 case TGSI_IMM_FLOAT32
:
1130 ret
= parse_float(&ctx
->cur
, &values
[i
].Float
);
1132 case TGSI_IMM_UINT32
:
1133 ret
= parse_uint(&ctx
->cur
, &values
[i
].Uint
);
1135 case TGSI_IMM_INT32
:
1136 ret
= parse_int(&ctx
->cur
, &values
[i
].Int
);
1145 report_error( ctx
, "Expected immediate constant" );
1149 eat_opt_white( &ctx
->cur
);
1150 if (*ctx
->cur
!= '}') {
1151 report_error( ctx
, "Expected `}'" );
1159 static boolean
parse_declaration( struct translate_ctx
*ctx
)
1161 struct tgsi_full_declaration decl
;
1163 struct parsed_dcl_bracket brackets
[2];
1166 const char *cur
, *cur2
;
1168 boolean is_vs_input
;
1170 if (!eat_white( &ctx
->cur
)) {
1171 report_error( ctx
, "Syntax error" );
1174 if (!parse_register_dcl( ctx
, &file
, brackets
, &num_brackets
))
1176 if (!parse_opt_writemask( ctx
, &writemask
))
1179 decl
= tgsi_default_full_declaration();
1180 decl
.Declaration
.File
= file
;
1181 decl
.Declaration
.UsageMask
= writemask
;
1183 if (num_brackets
== 1) {
1184 decl
.Range
.First
= brackets
[0].first
;
1185 decl
.Range
.Last
= brackets
[0].last
;
1187 decl
.Range
.First
= brackets
[1].first
;
1188 decl
.Range
.Last
= brackets
[1].last
;
1190 decl
.Declaration
.Dimension
= 1;
1191 decl
.Dim
.Index2D
= brackets
[0].first
;
1194 is_vs_input
= (file
== TGSI_FILE_INPUT
&&
1195 ctx
->processor
== TGSI_PROCESSOR_VERTEX
);
1198 eat_opt_white( &cur
);
1202 eat_opt_white( &cur2
);
1203 if (str_match_nocase_whole( &cur2
, "ARRAY" )) {
1206 report_error( ctx
, "Expected `('" );
1210 eat_opt_white( &cur2
);
1211 if (!parse_int( &cur2
, &arrayid
)) {
1212 report_error( ctx
, "Expected `,'" );
1215 eat_opt_white( &cur2
);
1217 report_error( ctx
, "Expected `)'" );
1221 decl
.Declaration
.Array
= 1;
1222 decl
.Array
.ArrayID
= arrayid
;
1223 ctx
->cur
= cur
= cur2
;
1227 if (*cur
== ',' && !is_vs_input
) {
1231 eat_opt_white( &cur
);
1232 if (file
== TGSI_FILE_RESOURCE
) {
1233 for (i
= 0; i
< TGSI_TEXTURE_COUNT
; i
++) {
1234 if (str_match_nocase_whole(&cur
, tgsi_texture_names
[i
])) {
1235 decl
.Resource
.Resource
= i
;
1239 if (i
== TGSI_TEXTURE_COUNT
) {
1240 report_error(ctx
, "Expected texture target");
1245 eat_opt_white(&cur2
);
1246 while (*cur2
== ',') {
1248 eat_opt_white(&cur2
);
1249 if (str_match_nocase_whole(&cur2
, "RAW")) {
1250 decl
.Resource
.Raw
= 1;
1252 } else if (str_match_nocase_whole(&cur2
, "WR")) {
1253 decl
.Resource
.Writable
= 1;
1259 eat_opt_white(&cur2
);
1264 } else if (file
== TGSI_FILE_SAMPLER_VIEW
) {
1265 for (i
= 0; i
< TGSI_TEXTURE_COUNT
; i
++) {
1266 if (str_match_nocase_whole(&cur
, tgsi_texture_names
[i
])) {
1267 decl
.SamplerView
.Resource
= i
;
1271 if (i
== TGSI_TEXTURE_COUNT
) {
1272 report_error(ctx
, "Expected texture target");
1275 eat_opt_white( &cur
);
1277 report_error( ctx
, "Expected `,'" );
1281 eat_opt_white( &cur
);
1282 for (j
= 0; j
< 4; ++j
) {
1283 for (i
= 0; i
< TGSI_RETURN_TYPE_COUNT
; ++i
) {
1284 if (str_match_nocase_whole(&cur
, tgsi_return_type_names
[i
])) {
1287 decl
.SamplerView
.ReturnTypeX
= i
;
1290 decl
.SamplerView
.ReturnTypeY
= i
;
1293 decl
.SamplerView
.ReturnTypeZ
= i
;
1296 decl
.SamplerView
.ReturnTypeW
= i
;
1304 if (i
== TGSI_RETURN_TYPE_COUNT
) {
1305 if (j
== 0 || j
> 2) {
1306 report_error(ctx
, "Expected type name");
1312 eat_opt_white( &cur2
);
1315 eat_opt_white( &cur2
);
1323 decl
.SamplerView
.ReturnTypeY
=
1324 decl
.SamplerView
.ReturnTypeZ
=
1325 decl
.SamplerView
.ReturnTypeW
=
1326 decl
.SamplerView
.ReturnTypeX
;
1330 if (str_match_nocase_whole(&cur
, "LOCAL")) {
1331 decl
.Declaration
.Local
= 1;
1336 eat_opt_white( &cur
);
1339 eat_opt_white( &cur
);
1341 for (i
= 0; i
< TGSI_SEMANTIC_COUNT
; i
++) {
1342 if (str_match_nocase_whole(&cur
, tgsi_semantic_names
[i
])) {
1346 eat_opt_white( &cur2
);
1349 eat_opt_white( &cur2
);
1350 if (!parse_uint( &cur2
, &index
)) {
1351 report_error( ctx
, "Expected literal integer" );
1354 eat_opt_white( &cur2
);
1356 report_error( ctx
, "Expected `]'" );
1361 decl
.Semantic
.Index
= index
;
1366 decl
.Declaration
.Semantic
= 1;
1367 decl
.Semantic
.Name
= i
;
1378 eat_opt_white( &cur
);
1379 if (*cur
== ',' && !is_vs_input
) {
1383 eat_opt_white( &cur
);
1384 for (i
= 0; i
< TGSI_INTERPOLATE_COUNT
; i
++) {
1385 if (str_match_nocase_whole( &cur
, tgsi_interpolate_names
[i
] )) {
1386 decl
.Declaration
.Interpolate
= 1;
1387 decl
.Interp
.Interpolate
= i
;
1393 if (i
== TGSI_INTERPOLATE_COUNT
) {
1394 report_error( ctx
, "Expected semantic or interpolate attribute" );
1400 eat_opt_white( &cur
);
1401 if (*cur
== ',' && !is_vs_input
) {
1405 eat_opt_white( &cur
);
1406 for (i
= 0; i
< TGSI_INTERPOLATE_LOC_COUNT
; i
++) {
1407 if (str_match_nocase_whole( &cur
, tgsi_interpolate_locations
[i
] )) {
1408 decl
.Interp
.Location
= i
;
1416 advance
= tgsi_build_full_declaration(
1420 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1424 ctx
->tokens_cur
+= advance
;
1429 static boolean
parse_immediate( struct translate_ctx
*ctx
)
1431 struct tgsi_full_immediate imm
;
1435 if (*ctx
->cur
== '[') {
1440 eat_opt_white( &ctx
->cur
);
1441 if (!parse_uint( &ctx
->cur
, &uindex
)) {
1442 report_error( ctx
, "Expected literal unsigned integer" );
1446 if (uindex
!= ctx
->num_immediates
) {
1447 report_error( ctx
, "Immediates must be sorted" );
1451 eat_opt_white( &ctx
->cur
);
1452 if (*ctx
->cur
!= ']') {
1453 report_error( ctx
, "Expected `]'" );
1460 if (!eat_white( &ctx
->cur
)) {
1461 report_error( ctx
, "Syntax error" );
1464 for (type
= 0; type
< Elements(tgsi_immediate_type_names
); ++type
) {
1465 if (str_match_nocase_whole(&ctx
->cur
, tgsi_immediate_type_names
[type
]))
1468 if (type
== Elements(tgsi_immediate_type_names
)) {
1469 report_error( ctx
, "Expected immediate type" );
1473 imm
= tgsi_default_full_immediate();
1474 imm
.Immediate
.NrTokens
+= 4;
1475 imm
.Immediate
.DataType
= type
;
1476 parse_immediate_data(ctx
, type
, imm
.u
);
1478 advance
= tgsi_build_full_immediate(
1482 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1485 ctx
->tokens_cur
+= advance
;
1487 ctx
->num_immediates
++;
1493 parse_primitive( const char **pcur
, uint
*primitive
)
1497 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++) {
1498 const char *cur
= *pcur
;
1500 if (str_match_nocase_whole( &cur
, tgsi_primitive_names
[i
])) {
1510 parse_fs_coord_origin( const char **pcur
, uint
*fs_coord_origin
)
1514 for (i
= 0; i
< Elements(tgsi_fs_coord_origin_names
); i
++) {
1515 const char *cur
= *pcur
;
1517 if (str_match_nocase_whole( &cur
, tgsi_fs_coord_origin_names
[i
])) {
1518 *fs_coord_origin
= i
;
1527 parse_fs_coord_pixel_center( const char **pcur
, uint
*fs_coord_pixel_center
)
1531 for (i
= 0; i
< Elements(tgsi_fs_coord_pixel_center_names
); i
++) {
1532 const char *cur
= *pcur
;
1534 if (str_match_nocase_whole( &cur
, tgsi_fs_coord_pixel_center_names
[i
])) {
1535 *fs_coord_pixel_center
= i
;
1544 static boolean
parse_property( struct translate_ctx
*ctx
)
1546 struct tgsi_full_property prop
;
1552 if (!eat_white( &ctx
->cur
)) {
1553 report_error( ctx
, "Syntax error" );
1556 if (!parse_identifier( &ctx
->cur
, id
)) {
1557 report_error( ctx
, "Syntax error" );
1560 for (property_name
= 0; property_name
< TGSI_PROPERTY_COUNT
;
1562 if (streq_nocase_uprcase(tgsi_property_names
[property_name
], id
)) {
1566 if (property_name
>= TGSI_PROPERTY_COUNT
) {
1567 debug_printf( "\nError: Unknown property : '%s'", id
);
1571 eat_opt_white( &ctx
->cur
);
1572 switch(property_name
) {
1573 case TGSI_PROPERTY_GS_INPUT_PRIM
:
1574 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
1575 if (!parse_primitive(&ctx
->cur
, &values
[0] )) {
1576 report_error( ctx
, "Unknown primitive name as property!" );
1579 if (property_name
== TGSI_PROPERTY_GS_INPUT_PRIM
&&
1580 ctx
->processor
== TGSI_PROCESSOR_GEOMETRY
) {
1581 ctx
->implied_array_size
= u_vertices_per_prim(values
[0]);
1584 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
1585 if (!parse_fs_coord_origin(&ctx
->cur
, &values
[0] )) {
1586 report_error( ctx
, "Unknown coord origin as property: must be UPPER_LEFT or LOWER_LEFT!" );
1590 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
1591 if (!parse_fs_coord_pixel_center(&ctx
->cur
, &values
[0] )) {
1592 report_error( ctx
, "Unknown coord pixel center as property: must be HALF_INTEGER or INTEGER!" );
1596 case TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
:
1598 if (!parse_uint(&ctx
->cur
, &values
[0] )) {
1599 report_error( ctx
, "Expected unsigned integer as property!" );
1604 prop
= tgsi_default_full_property();
1605 prop
.Property
.PropertyName
= property_name
;
1606 prop
.Property
.NrTokens
+= 1;
1607 prop
.u
[0].Data
= values
[0];
1609 advance
= tgsi_build_full_property(
1613 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1616 ctx
->tokens_cur
+= advance
;
1622 static boolean
translate( struct translate_ctx
*ctx
)
1624 eat_opt_white( &ctx
->cur
);
1625 if (!parse_header( ctx
))
1628 while (*ctx
->cur
!= '\0') {
1630 if (!eat_white( &ctx
->cur
)) {
1631 report_error( ctx
, "Syntax error" );
1635 if (*ctx
->cur
== '\0')
1637 if (parse_label( ctx
, &label_val
)) {
1638 if (!parse_instruction( ctx
, TRUE
))
1641 else if (str_match_nocase_whole( &ctx
->cur
, "DCL" )) {
1642 if (!parse_declaration( ctx
))
1645 else if (str_match_nocase_whole( &ctx
->cur
, "IMM" )) {
1646 if (!parse_immediate( ctx
))
1649 else if (str_match_nocase_whole( &ctx
->cur
, "PROPERTY" )) {
1650 if (!parse_property( ctx
))
1653 else if (!parse_instruction( ctx
, FALSE
)) {
1662 tgsi_text_translate(
1664 struct tgsi_token
*tokens
,
1667 struct translate_ctx ctx
= {0};
1671 ctx
.tokens
= tokens
;
1672 ctx
.tokens_cur
= tokens
;
1673 ctx
.tokens_end
= tokens
+ num_tokens
;
1675 if (!translate( &ctx
))
1678 return tgsi_sanity_check( tokens
);