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 /* Return the array index that matches starting at *pcur, where the string at
123 * *pcur is terminated by a non-digit non-letter non-underscore.
124 * Returns -1 if no match is found.
126 * On success, the pointer to the first string is moved to the end of the read
129 static int str_match_name_from_array(const char **pcur
,
130 const char * const *array
,
133 for (unsigned j
= 0; j
< array_size
; ++j
) {
134 if (str_match_nocase_whole(pcur
, array
[j
]))
140 /* Return the format corresponding to the name at *pcur.
141 * Returns -1 if there is no format name.
143 * On success, the pointer to the string is moved to the end of the read format
146 static int str_match_format(const char **pcur
)
148 for (unsigned i
= 0; i
< PIPE_FORMAT_COUNT
; i
++) {
149 const struct util_format_description
*desc
=
150 util_format_description(i
);
151 if (desc
&& str_match_nocase_whole(pcur
, desc
->name
)) {
158 /* Eat zero or more whitespaces.
160 static void eat_opt_white( const char **pcur
)
162 while (**pcur
== ' ' || **pcur
== '\t' || **pcur
== '\n')
166 /* Eat one or more whitespaces.
167 * Return TRUE if at least one whitespace eaten.
169 static boolean
eat_white( const char **pcur
)
171 const char *cur
= *pcur
;
173 eat_opt_white( pcur
);
177 /* Parse unsigned integer.
178 * No checks for overflow.
180 static boolean
parse_uint( const char **pcur
, uint
*val
)
182 const char *cur
= *pcur
;
184 if (is_digit( cur
)) {
186 while (is_digit( cur
))
187 *val
= *val
* 10 + *cur
++ - '0';
194 static boolean
parse_int( const char **pcur
, int *val
)
196 const char *cur
= *pcur
;
197 int sign
= (*cur
== '-' ? -1 : 1);
199 if (*cur
== '+' || *cur
== '-')
202 if (parse_uint(&cur
, (uint
*)val
)) {
211 static boolean
parse_identifier( const char **pcur
, char *ret
)
213 const char *cur
= *pcur
;
215 if (is_alpha_underscore( cur
)) {
217 while (is_alpha_underscore( cur
) || is_digit( cur
))
226 /* Parse floating point.
228 static boolean
parse_float( const char **pcur
, float *val
)
230 const char *cur
= *pcur
;
231 boolean integral_part
= FALSE
;
232 boolean fractional_part
= FALSE
;
234 if (*cur
== '0' && *(cur
+ 1) == 'x') {
236 fi
.ui
= strtoul(cur
, NULL
, 16);
242 *val
= (float) atof( cur
);
243 if (*cur
== '-' || *cur
== '+')
245 if (is_digit( cur
)) {
247 integral_part
= TRUE
;
248 while (is_digit( cur
))
253 if (is_digit( cur
)) {
255 fractional_part
= TRUE
;
256 while (is_digit( cur
))
260 if (!integral_part
&& !fractional_part
)
262 if (uprcase( *cur
) == 'E') {
264 if (*cur
== '-' || *cur
== '+')
266 if (is_digit( cur
)) {
268 while (is_digit( cur
))
280 static boolean
parse_double( const char **pcur
, uint32_t *val0
, uint32_t *val1
)
282 const char *cur
= *pcur
;
288 v
.dval
= strtod(cur
, (char**)pcur
);
298 static boolean
parse_int64( const char **pcur
, uint32_t *val0
, uint32_t *val1
)
300 const char *cur
= *pcur
;
306 v
.i64val
= strtoll(cur
, (char**)pcur
, 0);
316 static boolean
parse_uint64( const char **pcur
, uint32_t *val0
, uint32_t *val1
)
318 const char *cur
= *pcur
;
324 v
.u64val
= strtoull(cur
, (char**)pcur
, 0);
338 struct tgsi_token
*tokens
;
339 struct tgsi_token
*tokens_cur
;
340 struct tgsi_token
*tokens_end
;
341 struct tgsi_header
*header
;
342 unsigned processor
: 4;
343 unsigned implied_array_size
: 6;
344 unsigned num_immediates
;
347 static void report_error( struct translate_ctx
*ctx
, const char *msg
)
351 const char *itr
= ctx
->text
;
353 while (itr
!= ctx
->cur
) {
362 debug_printf( "\nTGSI asm error: %s [%d : %d] \n", msg
, line
, column
);
365 /* Parse shader header.
366 * Return TRUE for one of the following headers.
371 static boolean
parse_header( struct translate_ctx
*ctx
)
375 if (str_match_nocase_whole( &ctx
->cur
, "FRAG" ))
376 processor
= PIPE_SHADER_FRAGMENT
;
377 else if (str_match_nocase_whole( &ctx
->cur
, "VERT" ))
378 processor
= PIPE_SHADER_VERTEX
;
379 else if (str_match_nocase_whole( &ctx
->cur
, "GEOM" ))
380 processor
= PIPE_SHADER_GEOMETRY
;
381 else if (str_match_nocase_whole( &ctx
->cur
, "TESS_CTRL" ))
382 processor
= PIPE_SHADER_TESS_CTRL
;
383 else if (str_match_nocase_whole( &ctx
->cur
, "TESS_EVAL" ))
384 processor
= PIPE_SHADER_TESS_EVAL
;
385 else if (str_match_nocase_whole( &ctx
->cur
, "COMP" ))
386 processor
= PIPE_SHADER_COMPUTE
;
388 report_error( ctx
, "Unknown header" );
392 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
394 ctx
->header
= (struct tgsi_header
*) ctx
->tokens_cur
++;
395 *ctx
->header
= tgsi_build_header();
397 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
399 *(struct tgsi_processor
*) ctx
->tokens_cur
++ = tgsi_build_processor( processor
, ctx
->header
);
400 ctx
->processor
= processor
;
405 static boolean
parse_label( struct translate_ctx
*ctx
, uint
*val
)
407 const char *cur
= ctx
->cur
;
409 if (parse_uint( &cur
, val
)) {
410 eat_opt_white( &cur
);
421 parse_file( const char **pcur
, uint
*file
)
425 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++) {
426 const char *cur
= *pcur
;
428 if (str_match_nocase_whole( &cur
, tgsi_file_name(i
) )) {
439 struct translate_ctx
*ctx
,
445 eat_opt_white( &cur
);
448 *writemask
= TGSI_WRITEMASK_NONE
;
449 eat_opt_white( &cur
);
450 if (uprcase( *cur
) == 'X') {
452 *writemask
|= TGSI_WRITEMASK_X
;
454 if (uprcase( *cur
) == 'Y') {
456 *writemask
|= TGSI_WRITEMASK_Y
;
458 if (uprcase( *cur
) == 'Z') {
460 *writemask
|= TGSI_WRITEMASK_Z
;
462 if (uprcase( *cur
) == 'W') {
464 *writemask
|= TGSI_WRITEMASK_W
;
467 if (*writemask
== TGSI_WRITEMASK_NONE
) {
468 report_error( ctx
, "Writemask expected" );
475 *writemask
= TGSI_WRITEMASK_XYZW
;
481 /* <register_file_bracket> ::= <file> `['
484 parse_register_file_bracket(
485 struct translate_ctx
*ctx
,
488 if (!parse_file( &ctx
->cur
, file
)) {
489 report_error( ctx
, "Unknown register file" );
492 eat_opt_white( &ctx
->cur
);
493 if (*ctx
->cur
!= '[') {
494 report_error( ctx
, "Expected `['" );
501 /* <register_file_bracket_index> ::= <register_file_bracket> <uint>
504 parse_register_file_bracket_index(
505 struct translate_ctx
*ctx
,
511 if (!parse_register_file_bracket( ctx
, file
))
513 eat_opt_white( &ctx
->cur
);
514 if (!parse_uint( &ctx
->cur
, &uindex
)) {
515 report_error( ctx
, "Expected literal unsigned integer" );
518 *index
= (int) uindex
;
522 /* Parse simple 1d register operand.
523 * <register_dst> ::= <register_file_bracket_index> `]'
526 parse_register_1d(struct translate_ctx
*ctx
,
530 if (!parse_register_file_bracket_index( ctx
, file
, index
))
532 eat_opt_white( &ctx
->cur
);
533 if (*ctx
->cur
!= ']') {
534 report_error( ctx
, "Expected `]'" );
541 struct parsed_bracket
{
552 parse_register_bracket(
553 struct translate_ctx
*ctx
,
554 struct parsed_bracket
*brackets
)
559 memset(brackets
, 0, sizeof(struct parsed_bracket
));
561 eat_opt_white( &ctx
->cur
);
564 if (parse_file( &cur
, &brackets
->ind_file
)) {
565 if (!parse_register_1d( ctx
, &brackets
->ind_file
,
566 &brackets
->ind_index
))
568 eat_opt_white( &ctx
->cur
);
570 if (*ctx
->cur
== '.') {
572 eat_opt_white(&ctx
->cur
);
574 switch (uprcase(*ctx
->cur
)) {
576 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
579 brackets
->ind_comp
= TGSI_SWIZZLE_Y
;
582 brackets
->ind_comp
= TGSI_SWIZZLE_Z
;
585 brackets
->ind_comp
= TGSI_SWIZZLE_W
;
588 report_error(ctx
, "Expected indirect register swizzle component `x', `y', `z' or `w'");
592 eat_opt_white(&ctx
->cur
);
595 if (*ctx
->cur
== '+' || *ctx
->cur
== '-')
596 parse_int( &ctx
->cur
, &brackets
->index
);
601 if (!parse_uint( &ctx
->cur
, &uindex
)) {
602 report_error( ctx
, "Expected literal unsigned integer" );
605 brackets
->index
= (int) uindex
;
606 brackets
->ind_file
= TGSI_FILE_NULL
;
607 brackets
->ind_index
= 0;
609 eat_opt_white( &ctx
->cur
);
610 if (*ctx
->cur
!= ']') {
611 report_error( ctx
, "Expected `]'" );
615 if (*ctx
->cur
== '(') {
617 eat_opt_white( &ctx
->cur
);
618 if (!parse_uint( &ctx
->cur
, &brackets
->ind_array
)) {
619 report_error( ctx
, "Expected literal unsigned integer" );
622 eat_opt_white( &ctx
->cur
);
623 if (*ctx
->cur
!= ')') {
624 report_error( ctx
, "Expected `)'" );
633 parse_opt_register_src_bracket(
634 struct translate_ctx
*ctx
,
635 struct parsed_bracket
*brackets
,
636 int *parsed_brackets
)
638 const char *cur
= ctx
->cur
;
640 *parsed_brackets
= 0;
642 eat_opt_white( &cur
);
647 if (!parse_register_bracket(ctx
, brackets
))
650 *parsed_brackets
= 1;
657 /* Parse source register operand.
658 * <register_src> ::= <register_file_bracket_index> `]' |
659 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `]' |
660 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `+' <uint> `]' |
661 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `-' <uint> `]'
665 struct translate_ctx
*ctx
,
667 struct parsed_bracket
*brackets
)
669 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
670 if (!parse_register_file_bracket( ctx
, file
))
672 if (!parse_register_bracket( ctx
, brackets
))
678 struct parsed_dcl_bracket
{
684 parse_register_dcl_bracket(
685 struct translate_ctx
*ctx
,
686 struct parsed_dcl_bracket
*bracket
)
689 memset(bracket
, 0, sizeof(struct parsed_dcl_bracket
));
691 eat_opt_white( &ctx
->cur
);
693 if (!parse_uint( &ctx
->cur
, &uindex
)) {
694 /* it can be an empty bracket [] which means its range
695 * is from 0 to some implied size */
696 if (ctx
->cur
[0] == ']' && ctx
->implied_array_size
!= 0) {
698 bracket
->last
= ctx
->implied_array_size
- 1;
701 report_error( ctx
, "Expected literal unsigned integer" );
704 bracket
->first
= uindex
;
706 eat_opt_white( &ctx
->cur
);
708 if (ctx
->cur
[0] == '.' && ctx
->cur
[1] == '.') {
712 eat_opt_white( &ctx
->cur
);
713 if (!parse_uint( &ctx
->cur
, &uindex
)) {
714 report_error( ctx
, "Expected literal integer" );
717 bracket
->last
= (int) uindex
;
718 eat_opt_white( &ctx
->cur
);
721 bracket
->last
= bracket
->first
;
725 if (*ctx
->cur
!= ']') {
726 report_error( ctx
, "Expected `]' or `..'" );
733 /* Parse register declaration.
734 * <register_dcl> ::= <register_file_bracket_index> `]' |
735 * <register_file_bracket_index> `..' <index> `]'
739 struct translate_ctx
*ctx
,
741 struct parsed_dcl_bracket
*brackets
,
748 if (!parse_register_file_bracket( ctx
, file
))
750 if (!parse_register_dcl_bracket( ctx
, &brackets
[0] ))
756 eat_opt_white( &cur
);
759 bool is_in
= *file
== TGSI_FILE_INPUT
;
760 bool is_out
= *file
== TGSI_FILE_OUTPUT
;
764 if (!parse_register_dcl_bracket( ctx
, &brackets
[1] ))
766 /* for geometry shader we don't really care about
767 * the first brackets it's always the size of the
768 * input primitive. so we want to declare just
769 * the index relevant to the semantics which is in
770 * the second bracket */
772 /* tessellation has similar constraints to geometry shader */
773 if ((ctx
->processor
== PIPE_SHADER_GEOMETRY
&& is_in
) ||
774 (ctx
->processor
== PIPE_SHADER_TESS_EVAL
&& is_in
) ||
775 (ctx
->processor
== PIPE_SHADER_TESS_CTRL
&& (is_in
|| is_out
))) {
776 brackets
[0] = brackets
[1];
787 /* Parse destination register operand.*/
790 struct translate_ctx
*ctx
,
792 struct parsed_bracket
*brackets
)
794 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
795 if (!parse_register_file_bracket( ctx
, file
))
797 if (!parse_register_bracket( ctx
, brackets
))
805 struct translate_ctx
*ctx
,
806 struct tgsi_full_dst_register
*dst
)
811 struct parsed_bracket bracket
[2];
812 int parsed_opt_brackets
;
814 if (!parse_register_dst( ctx
, &file
, &bracket
[0] ))
816 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
820 eat_opt_white( &cur
);
822 if (!parse_opt_writemask( ctx
, &writemask
))
825 dst
->Register
.File
= file
;
826 if (parsed_opt_brackets
) {
827 dst
->Register
.Dimension
= 1;
828 dst
->Dimension
.Indirect
= 0;
829 dst
->Dimension
.Dimension
= 0;
830 dst
->Dimension
.Index
= bracket
[0].index
;
832 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
833 dst
->Dimension
.Indirect
= 1;
834 dst
->DimIndirect
.File
= bracket
[0].ind_file
;
835 dst
->DimIndirect
.Index
= bracket
[0].ind_index
;
836 dst
->DimIndirect
.Swizzle
= bracket
[0].ind_comp
;
837 dst
->DimIndirect
.ArrayID
= bracket
[0].ind_array
;
839 bracket
[0] = bracket
[1];
841 dst
->Register
.Index
= bracket
[0].index
;
842 dst
->Register
.WriteMask
= writemask
;
843 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
844 dst
->Register
.Indirect
= 1;
845 dst
->Indirect
.File
= bracket
[0].ind_file
;
846 dst
->Indirect
.Index
= bracket
[0].ind_index
;
847 dst
->Indirect
.Swizzle
= bracket
[0].ind_comp
;
848 dst
->Indirect
.ArrayID
= bracket
[0].ind_array
;
854 parse_optional_swizzle(
855 struct translate_ctx
*ctx
,
857 boolean
*parsed_swizzle
,
860 const char *cur
= ctx
->cur
;
862 *parsed_swizzle
= FALSE
;
864 eat_opt_white( &cur
);
869 eat_opt_white( &cur
);
870 for (i
= 0; i
< components
; i
++) {
871 if (uprcase( *cur
) == 'X')
872 swizzle
[i
] = TGSI_SWIZZLE_X
;
873 else if (uprcase( *cur
) == 'Y')
874 swizzle
[i
] = TGSI_SWIZZLE_Y
;
875 else if (uprcase( *cur
) == 'Z')
876 swizzle
[i
] = TGSI_SWIZZLE_Z
;
877 else if (uprcase( *cur
) == 'W')
878 swizzle
[i
] = TGSI_SWIZZLE_W
;
880 report_error( ctx
, "Expected register swizzle component `x', `y', `z' or `w'" );
885 *parsed_swizzle
= TRUE
;
893 struct translate_ctx
*ctx
,
894 struct tgsi_full_src_register
*src
)
898 boolean parsed_swizzle
;
899 struct parsed_bracket bracket
[2];
900 int parsed_opt_brackets
;
902 if (*ctx
->cur
== '-') {
904 eat_opt_white( &ctx
->cur
);
905 src
->Register
.Negate
= 1;
908 if (*ctx
->cur
== '|') {
910 eat_opt_white( &ctx
->cur
);
911 src
->Register
.Absolute
= 1;
914 if (!parse_register_src(ctx
, &file
, &bracket
[0]))
916 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
919 src
->Register
.File
= file
;
920 if (parsed_opt_brackets
) {
921 src
->Register
.Dimension
= 1;
922 src
->Dimension
.Indirect
= 0;
923 src
->Dimension
.Dimension
= 0;
924 src
->Dimension
.Index
= bracket
[0].index
;
925 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
926 src
->Dimension
.Indirect
= 1;
927 src
->DimIndirect
.File
= bracket
[0].ind_file
;
928 src
->DimIndirect
.Index
= bracket
[0].ind_index
;
929 src
->DimIndirect
.Swizzle
= bracket
[0].ind_comp
;
930 src
->DimIndirect
.ArrayID
= bracket
[0].ind_array
;
932 bracket
[0] = bracket
[1];
934 src
->Register
.Index
= bracket
[0].index
;
935 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
936 src
->Register
.Indirect
= 1;
937 src
->Indirect
.File
= bracket
[0].ind_file
;
938 src
->Indirect
.Index
= bracket
[0].ind_index
;
939 src
->Indirect
.Swizzle
= bracket
[0].ind_comp
;
940 src
->Indirect
.ArrayID
= bracket
[0].ind_array
;
943 /* Parse optional swizzle.
945 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
, 4 )) {
946 if (parsed_swizzle
) {
947 src
->Register
.SwizzleX
= swizzle
[0];
948 src
->Register
.SwizzleY
= swizzle
[1];
949 src
->Register
.SwizzleZ
= swizzle
[2];
950 src
->Register
.SwizzleW
= swizzle
[3];
954 if (src
->Register
.Absolute
) {
955 eat_opt_white( &ctx
->cur
);
956 if (*ctx
->cur
!= '|') {
957 report_error( ctx
, "Expected `|'" );
968 parse_texoffset_operand(
969 struct translate_ctx
*ctx
,
970 struct tgsi_texture_offset
*src
)
974 boolean parsed_swizzle
;
975 struct parsed_bracket bracket
;
977 if (!parse_register_src(ctx
, &file
, &bracket
))
981 src
->Index
= bracket
.index
;
983 /* Parse optional swizzle.
985 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
, 3 )) {
986 if (parsed_swizzle
) {
987 src
->SwizzleX
= swizzle
[0];
988 src
->SwizzleY
= swizzle
[1];
989 src
->SwizzleZ
= swizzle
[2];
997 match_inst(const char **pcur
,
999 const struct tgsi_opcode_info
*info
)
1001 const char *cur
= *pcur
;
1003 /* simple case: the whole string matches the instruction name */
1004 if (str_match_nocase_whole(&cur
, info
->mnemonic
)) {
1010 if (str_match_no_case(&cur
, info
->mnemonic
)) {
1011 /* the instruction has a suffix, figure it out */
1012 if (str_match_nocase_whole(&cur
, "_SAT")) {
1024 struct translate_ctx
*ctx
,
1029 const struct tgsi_opcode_info
*info
;
1030 struct tgsi_full_instruction inst
;
1034 inst
= tgsi_default_full_instruction();
1038 eat_opt_white( &ctx
->cur
);
1039 if (*ctx
->cur
== '(') {
1043 boolean parsed_swizzle
;
1045 inst
.Instruction
.Predicate
= 1;
1048 if (*ctx
->cur
== '!') {
1050 inst
.Predicate
.Negate
= 1;
1053 if (!parse_register_1d( ctx
, &file
, &index
))
1056 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
, 4 )) {
1057 if (parsed_swizzle
) {
1058 inst
.Predicate
.SwizzleX
= swizzle
[0];
1059 inst
.Predicate
.SwizzleY
= swizzle
[1];
1060 inst
.Predicate
.SwizzleZ
= swizzle
[2];
1061 inst
.Predicate
.SwizzleW
= swizzle
[3];
1065 if (*ctx
->cur
!= ')') {
1066 report_error( ctx
, "Expected `)'" );
1073 /* Parse instruction name.
1075 eat_opt_white( &ctx
->cur
);
1076 for (i
= 0; i
< TGSI_OPCODE_LAST
; i
++) {
1079 info
= tgsi_get_opcode_info( i
);
1080 if (match_inst(&cur
, &saturate
, info
)) {
1081 if (info
->num_dst
+ info
->num_src
+ info
->is_tex
== 0) {
1085 else if (*cur
== '\0' || eat_white( &cur
)) {
1091 if (i
== TGSI_OPCODE_LAST
) {
1093 report_error( ctx
, "Unknown opcode" );
1095 report_error( ctx
, "Expected `DCL', `IMM' or a label" );
1099 inst
.Instruction
.Opcode
= i
;
1100 inst
.Instruction
.Saturate
= saturate
;
1101 inst
.Instruction
.NumDstRegs
= info
->num_dst
;
1102 inst
.Instruction
.NumSrcRegs
= info
->num_src
;
1104 if (i
>= TGSI_OPCODE_SAMPLE
&& i
<= TGSI_OPCODE_GATHER4
) {
1106 * These are not considered tex opcodes here (no additional
1107 * target argument) however we're required to set the Texture
1108 * bit so we can set the number of tex offsets.
1110 inst
.Instruction
.Texture
= 1;
1111 inst
.Texture
.Texture
= TGSI_TEXTURE_UNKNOWN
;
1114 if ((i
>= TGSI_OPCODE_LOAD
&& i
<= TGSI_OPCODE_ATOMIMAX
) ||
1115 i
== TGSI_OPCODE_RESQ
) {
1116 inst
.Instruction
.Memory
= 1;
1117 inst
.Memory
.Qualifier
= 0;
1120 assume(info
->num_dst
<= TGSI_FULL_MAX_DST_REGISTERS
);
1121 assume(info
->num_src
<= TGSI_FULL_MAX_SRC_REGISTERS
);
1123 /* Parse instruction operands.
1125 for (i
= 0; i
< info
->num_dst
+ info
->num_src
+ info
->is_tex
; i
++) {
1127 eat_opt_white( &ctx
->cur
);
1128 if (*ctx
->cur
!= ',') {
1129 report_error( ctx
, "Expected `,'" );
1133 eat_opt_white( &ctx
->cur
);
1136 if (i
< info
->num_dst
) {
1137 if (!parse_dst_operand( ctx
, &inst
.Dst
[i
] ))
1140 else if (i
< info
->num_dst
+ info
->num_src
) {
1141 if (!parse_src_operand( ctx
, &inst
.Src
[i
- info
->num_dst
] ))
1147 for (j
= 0; j
< TGSI_TEXTURE_COUNT
; j
++) {
1148 if (str_match_nocase_whole( &ctx
->cur
, tgsi_texture_names
[j
] )) {
1149 inst
.Instruction
.Texture
= 1;
1150 inst
.Texture
.Texture
= j
;
1154 if (j
== TGSI_TEXTURE_COUNT
) {
1155 report_error( ctx
, "Expected texture target" );
1162 eat_opt_white( &cur
);
1163 for (i
= 0; inst
.Instruction
.Texture
&& *cur
== ','; i
++) {
1165 eat_opt_white( &cur
);
1167 if (!parse_texoffset_operand( ctx
, &inst
.TexOffsets
[i
] ))
1170 eat_opt_white( &cur
);
1172 inst
.Texture
.NumOffsets
= i
;
1175 eat_opt_white(&cur
);
1177 for (; inst
.Instruction
.Memory
&& *cur
== ',';
1178 ctx
->cur
= cur
, eat_opt_white(&cur
)) {
1182 eat_opt_white(&cur
);
1184 j
= str_match_name_from_array(&cur
, tgsi_memory_names
,
1185 ARRAY_SIZE(tgsi_memory_names
));
1187 inst
.Memory
.Qualifier
|= 1U << j
;
1191 j
= str_match_name_from_array(&cur
, tgsi_texture_names
,
1192 ARRAY_SIZE(tgsi_texture_names
));
1194 inst
.Memory
.Texture
= j
;
1198 j
= str_match_format(&cur
);
1200 inst
.Memory
.Format
= j
;
1205 report_error(ctx
, "Expected memory qualifier, texture target, or format\n");
1210 eat_opt_white( &cur
);
1211 if (info
->is_branch
&& *cur
== ':') {
1215 eat_opt_white( &cur
);
1216 if (!parse_uint( &cur
, &target
)) {
1217 report_error( ctx
, "Expected a label" );
1220 inst
.Instruction
.Label
= 1;
1221 inst
.Label
.Label
= target
;
1225 advance
= tgsi_build_full_instruction(
1229 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1232 ctx
->tokens_cur
+= advance
;
1237 /* parses a 4-touple of the form {x, y, z, w}
1238 * where x, y, z, w are numbers */
1239 static boolean
parse_immediate_data(struct translate_ctx
*ctx
, unsigned type
,
1240 union tgsi_immediate_data
*values
)
1245 eat_opt_white( &ctx
->cur
);
1246 if (*ctx
->cur
!= '{') {
1247 report_error( ctx
, "Expected `{'" );
1251 for (i
= 0; i
< 4; i
++) {
1252 eat_opt_white( &ctx
->cur
);
1254 if (*ctx
->cur
!= ',') {
1255 report_error( ctx
, "Expected `,'" );
1259 eat_opt_white( &ctx
->cur
);
1263 case TGSI_IMM_FLOAT64
:
1264 ret
= parse_double(&ctx
->cur
, &values
[i
].Uint
, &values
[i
+1].Uint
);
1267 case TGSI_IMM_INT64
:
1268 ret
= parse_int64(&ctx
->cur
, &values
[i
].Uint
, &values
[i
+1].Uint
);
1271 case TGSI_IMM_UINT64
:
1272 ret
= parse_uint64(&ctx
->cur
, &values
[i
].Uint
, &values
[i
+1].Uint
);
1275 case TGSI_IMM_FLOAT32
:
1276 ret
= parse_float(&ctx
->cur
, &values
[i
].Float
);
1278 case TGSI_IMM_UINT32
:
1279 ret
= parse_uint(&ctx
->cur
, &values
[i
].Uint
);
1281 case TGSI_IMM_INT32
:
1282 ret
= parse_int(&ctx
->cur
, &values
[i
].Int
);
1291 report_error( ctx
, "Expected immediate constant" );
1295 eat_opt_white( &ctx
->cur
);
1296 if (*ctx
->cur
!= '}') {
1297 report_error( ctx
, "Expected `}'" );
1305 static boolean
parse_declaration( struct translate_ctx
*ctx
)
1307 struct tgsi_full_declaration decl
;
1309 struct parsed_dcl_bracket brackets
[2];
1312 const char *cur
, *cur2
;
1314 boolean is_vs_input
;
1316 if (!eat_white( &ctx
->cur
)) {
1317 report_error( ctx
, "Syntax error" );
1320 if (!parse_register_dcl( ctx
, &file
, brackets
, &num_brackets
))
1322 if (!parse_opt_writemask( ctx
, &writemask
))
1325 decl
= tgsi_default_full_declaration();
1326 decl
.Declaration
.File
= file
;
1327 decl
.Declaration
.UsageMask
= writemask
;
1329 if (num_brackets
== 1) {
1330 decl
.Range
.First
= brackets
[0].first
;
1331 decl
.Range
.Last
= brackets
[0].last
;
1333 decl
.Range
.First
= brackets
[1].first
;
1334 decl
.Range
.Last
= brackets
[1].last
;
1336 decl
.Declaration
.Dimension
= 1;
1337 decl
.Dim
.Index2D
= brackets
[0].first
;
1340 is_vs_input
= (file
== TGSI_FILE_INPUT
&&
1341 ctx
->processor
== PIPE_SHADER_VERTEX
);
1344 eat_opt_white( &cur
);
1348 eat_opt_white( &cur2
);
1349 if (str_match_nocase_whole( &cur2
, "ARRAY" )) {
1352 report_error( ctx
, "Expected `('" );
1356 eat_opt_white( &cur2
);
1357 if (!parse_int( &cur2
, &arrayid
)) {
1358 report_error( ctx
, "Expected `,'" );
1361 eat_opt_white( &cur2
);
1363 report_error( ctx
, "Expected `)'" );
1367 decl
.Declaration
.Array
= 1;
1368 decl
.Array
.ArrayID
= arrayid
;
1369 ctx
->cur
= cur
= cur2
;
1373 if (*cur
== ',' && !is_vs_input
) {
1377 eat_opt_white( &cur
);
1378 if (file
== TGSI_FILE_IMAGE
) {
1379 for (i
= 0; i
< TGSI_TEXTURE_COUNT
; i
++) {
1380 if (str_match_nocase_whole(&cur
, tgsi_texture_names
[i
])) {
1381 decl
.Image
.Resource
= i
;
1385 if (i
== TGSI_TEXTURE_COUNT
) {
1386 report_error(ctx
, "Expected texture target");
1391 eat_opt_white(&cur2
);
1392 while (*cur2
== ',') {
1394 eat_opt_white(&cur2
);
1395 if (str_match_nocase_whole(&cur2
, "RAW")) {
1398 } else if (str_match_nocase_whole(&cur2
, "WR")) {
1399 decl
.Image
.Writable
= 1;
1402 int format
= str_match_format(&cur2
);
1406 decl
.Image
.Format
= format
;
1409 eat_opt_white(&cur2
);
1414 } else if (file
== TGSI_FILE_SAMPLER_VIEW
) {
1415 for (i
= 0; i
< TGSI_TEXTURE_COUNT
; i
++) {
1416 if (str_match_nocase_whole(&cur
, tgsi_texture_names
[i
])) {
1417 decl
.SamplerView
.Resource
= i
;
1421 if (i
== TGSI_TEXTURE_COUNT
) {
1422 report_error(ctx
, "Expected texture target");
1425 eat_opt_white( &cur
);
1427 report_error( ctx
, "Expected `,'" );
1431 eat_opt_white( &cur
);
1432 for (j
= 0; j
< 4; ++j
) {
1433 for (i
= 0; i
< TGSI_RETURN_TYPE_COUNT
; ++i
) {
1434 if (str_match_nocase_whole(&cur
, tgsi_return_type_names
[i
])) {
1437 decl
.SamplerView
.ReturnTypeX
= i
;
1440 decl
.SamplerView
.ReturnTypeY
= i
;
1443 decl
.SamplerView
.ReturnTypeZ
= i
;
1446 decl
.SamplerView
.ReturnTypeW
= i
;
1454 if (i
== TGSI_RETURN_TYPE_COUNT
) {
1455 if (j
== 0 || j
> 2) {
1456 report_error(ctx
, "Expected type name");
1462 eat_opt_white( &cur2
);
1465 eat_opt_white( &cur2
);
1473 decl
.SamplerView
.ReturnTypeY
=
1474 decl
.SamplerView
.ReturnTypeZ
=
1475 decl
.SamplerView
.ReturnTypeW
=
1476 decl
.SamplerView
.ReturnTypeX
;
1479 } else if (file
== TGSI_FILE_BUFFER
) {
1480 if (str_match_nocase_whole(&cur
, "ATOMIC")) {
1481 decl
.Declaration
.Atomic
= 1;
1484 } else if (file
== TGSI_FILE_MEMORY
) {
1485 if (str_match_nocase_whole(&cur
, "GLOBAL")) {
1486 /* Note this is a no-op global is the default */
1487 decl
.Declaration
.MemType
= TGSI_MEMORY_TYPE_GLOBAL
;
1489 } else if (str_match_nocase_whole(&cur
, "SHARED")) {
1490 decl
.Declaration
.MemType
= TGSI_MEMORY_TYPE_SHARED
;
1492 } else if (str_match_nocase_whole(&cur
, "PRIVATE")) {
1493 decl
.Declaration
.MemType
= TGSI_MEMORY_TYPE_PRIVATE
;
1495 } else if (str_match_nocase_whole(&cur
, "INPUT")) {
1496 decl
.Declaration
.MemType
= TGSI_MEMORY_TYPE_INPUT
;
1500 if (str_match_nocase_whole(&cur
, "LOCAL")) {
1501 decl
.Declaration
.Local
= 1;
1506 eat_opt_white( &cur
);
1509 eat_opt_white( &cur
);
1511 for (i
= 0; i
< TGSI_SEMANTIC_COUNT
; i
++) {
1512 if (str_match_nocase_whole(&cur
, tgsi_semantic_names
[i
])) {
1516 eat_opt_white( &cur2
);
1519 eat_opt_white( &cur2
);
1520 if (!parse_uint( &cur2
, &index
)) {
1521 report_error( ctx
, "Expected literal integer" );
1524 eat_opt_white( &cur2
);
1526 report_error( ctx
, "Expected `]'" );
1531 decl
.Semantic
.Index
= index
;
1536 decl
.Declaration
.Semantic
= 1;
1537 decl
.Semantic
.Name
= i
;
1548 eat_opt_white( &cur
);
1549 if (*cur
== ',' && !is_vs_input
) {
1553 eat_opt_white( &cur
);
1554 for (i
= 0; i
< TGSI_INTERPOLATE_COUNT
; i
++) {
1555 if (str_match_nocase_whole( &cur
, tgsi_interpolate_names
[i
] )) {
1556 decl
.Declaration
.Interpolate
= 1;
1557 decl
.Interp
.Interpolate
= i
;
1563 if (i
== TGSI_INTERPOLATE_COUNT
) {
1564 report_error( ctx
, "Expected semantic or interpolate attribute" );
1570 eat_opt_white( &cur
);
1571 if (*cur
== ',' && !is_vs_input
) {
1575 eat_opt_white( &cur
);
1576 for (i
= 0; i
< TGSI_INTERPOLATE_LOC_COUNT
; i
++) {
1577 if (str_match_nocase_whole( &cur
, tgsi_interpolate_locations
[i
] )) {
1578 decl
.Interp
.Location
= i
;
1586 advance
= tgsi_build_full_declaration(
1590 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1594 ctx
->tokens_cur
+= advance
;
1599 static boolean
parse_immediate( struct translate_ctx
*ctx
)
1601 struct tgsi_full_immediate imm
;
1605 if (*ctx
->cur
== '[') {
1610 eat_opt_white( &ctx
->cur
);
1611 if (!parse_uint( &ctx
->cur
, &uindex
)) {
1612 report_error( ctx
, "Expected literal unsigned integer" );
1616 if (uindex
!= ctx
->num_immediates
) {
1617 report_error( ctx
, "Immediates must be sorted" );
1621 eat_opt_white( &ctx
->cur
);
1622 if (*ctx
->cur
!= ']') {
1623 report_error( ctx
, "Expected `]'" );
1630 if (!eat_white( &ctx
->cur
)) {
1631 report_error( ctx
, "Syntax error" );
1634 for (type
= 0; type
< ARRAY_SIZE(tgsi_immediate_type_names
); ++type
) {
1635 if (str_match_nocase_whole(&ctx
->cur
, tgsi_immediate_type_names
[type
]))
1638 if (type
== ARRAY_SIZE(tgsi_immediate_type_names
)) {
1639 report_error( ctx
, "Expected immediate type" );
1643 imm
= tgsi_default_full_immediate();
1644 imm
.Immediate
.NrTokens
+= 4;
1645 imm
.Immediate
.DataType
= type
;
1646 parse_immediate_data(ctx
, type
, imm
.u
);
1648 advance
= tgsi_build_full_immediate(
1652 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1655 ctx
->tokens_cur
+= advance
;
1657 ctx
->num_immediates
++;
1663 parse_primitive( const char **pcur
, uint
*primitive
)
1667 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++) {
1668 const char *cur
= *pcur
;
1670 if (str_match_nocase_whole( &cur
, tgsi_primitive_names
[i
])) {
1680 parse_fs_coord_origin( const char **pcur
, uint
*fs_coord_origin
)
1684 for (i
= 0; i
< ARRAY_SIZE(tgsi_fs_coord_origin_names
); i
++) {
1685 const char *cur
= *pcur
;
1687 if (str_match_nocase_whole( &cur
, tgsi_fs_coord_origin_names
[i
])) {
1688 *fs_coord_origin
= i
;
1697 parse_fs_coord_pixel_center( const char **pcur
, uint
*fs_coord_pixel_center
)
1701 for (i
= 0; i
< ARRAY_SIZE(tgsi_fs_coord_pixel_center_names
); i
++) {
1702 const char *cur
= *pcur
;
1704 if (str_match_nocase_whole( &cur
, tgsi_fs_coord_pixel_center_names
[i
])) {
1705 *fs_coord_pixel_center
= i
;
1714 parse_property_next_shader( const char **pcur
, uint
*next_shader
)
1718 for (i
= 0; i
< ARRAY_SIZE(tgsi_processor_type_names
); i
++) {
1719 const char *cur
= *pcur
;
1721 if (str_match_nocase_whole( &cur
, tgsi_processor_type_names
[i
])) {
1730 static boolean
parse_property( struct translate_ctx
*ctx
)
1732 struct tgsi_full_property prop
;
1738 if (!eat_white( &ctx
->cur
)) {
1739 report_error( ctx
, "Syntax error" );
1742 if (!parse_identifier( &ctx
->cur
, id
)) {
1743 report_error( ctx
, "Syntax error" );
1746 for (property_name
= 0; property_name
< TGSI_PROPERTY_COUNT
;
1748 if (streq_nocase_uprcase(tgsi_property_names
[property_name
], id
)) {
1752 if (property_name
>= TGSI_PROPERTY_COUNT
) {
1753 debug_printf( "\nError: Unknown property : '%s'", id
);
1757 eat_opt_white( &ctx
->cur
);
1758 switch(property_name
) {
1759 case TGSI_PROPERTY_GS_INPUT_PRIM
:
1760 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
1761 if (!parse_primitive(&ctx
->cur
, &values
[0] )) {
1762 report_error( ctx
, "Unknown primitive name as property!" );
1765 if (property_name
== TGSI_PROPERTY_GS_INPUT_PRIM
&&
1766 ctx
->processor
== PIPE_SHADER_GEOMETRY
) {
1767 ctx
->implied_array_size
= u_vertices_per_prim(values
[0]);
1770 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
1771 if (!parse_fs_coord_origin(&ctx
->cur
, &values
[0] )) {
1772 report_error( ctx
, "Unknown coord origin as property: must be UPPER_LEFT or LOWER_LEFT!" );
1776 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
1777 if (!parse_fs_coord_pixel_center(&ctx
->cur
, &values
[0] )) {
1778 report_error( ctx
, "Unknown coord pixel center as property: must be HALF_INTEGER or INTEGER!" );
1782 case TGSI_PROPERTY_NEXT_SHADER
:
1783 if (!parse_property_next_shader(&ctx
->cur
, &values
[0] )) {
1784 report_error( ctx
, "Unknown next shader property value." );
1788 case TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
:
1790 if (!parse_uint(&ctx
->cur
, &values
[0] )) {
1791 report_error( ctx
, "Expected unsigned integer as property!" );
1796 prop
= tgsi_default_full_property();
1797 prop
.Property
.PropertyName
= property_name
;
1798 prop
.Property
.NrTokens
+= 1;
1799 prop
.u
[0].Data
= values
[0];
1801 advance
= tgsi_build_full_property(
1805 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1808 ctx
->tokens_cur
+= advance
;
1814 static boolean
translate( struct translate_ctx
*ctx
)
1816 eat_opt_white( &ctx
->cur
);
1817 if (!parse_header( ctx
))
1820 if (ctx
->processor
== PIPE_SHADER_TESS_CTRL
||
1821 ctx
->processor
== PIPE_SHADER_TESS_EVAL
)
1822 ctx
->implied_array_size
= 32;
1824 while (*ctx
->cur
!= '\0') {
1826 if (!eat_white( &ctx
->cur
)) {
1827 report_error( ctx
, "Syntax error" );
1831 if (*ctx
->cur
== '\0')
1833 if (parse_label( ctx
, &label_val
)) {
1834 if (!parse_instruction( ctx
, TRUE
))
1837 else if (str_match_nocase_whole( &ctx
->cur
, "DCL" )) {
1838 if (!parse_declaration( ctx
))
1841 else if (str_match_nocase_whole( &ctx
->cur
, "IMM" )) {
1842 if (!parse_immediate( ctx
))
1845 else if (str_match_nocase_whole( &ctx
->cur
, "PROPERTY" )) {
1846 if (!parse_property( ctx
))
1849 else if (!parse_instruction( ctx
, FALSE
)) {
1858 tgsi_text_translate(
1860 struct tgsi_token
*tokens
,
1863 struct translate_ctx ctx
= {0};
1867 ctx
.tokens
= tokens
;
1868 ctx
.tokens_cur
= tokens
;
1869 ctx
.tokens_end
= tokens
+ num_tokens
;
1871 if (!translate( &ctx
))
1874 return tgsi_sanity_check( tokens
);