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
, (char**)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 unsigned implied_array_size
: 6;
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
, "TESS_CTRL" ))
301 processor
= TGSI_PROCESSOR_TESS_CTRL
;
302 else if (str_match_nocase_whole( &ctx
->cur
, "TESS_EVAL" ))
303 processor
= TGSI_PROCESSOR_TESS_EVAL
;
304 else if (str_match_nocase_whole( &ctx
->cur
, "COMP" ))
305 processor
= TGSI_PROCESSOR_COMPUTE
;
307 report_error( ctx
, "Unknown header" );
311 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
313 ctx
->header
= (struct tgsi_header
*) ctx
->tokens_cur
++;
314 *ctx
->header
= tgsi_build_header();
316 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
318 *(struct tgsi_processor
*) ctx
->tokens_cur
++ = tgsi_build_processor( processor
, ctx
->header
);
319 ctx
->processor
= processor
;
324 static boolean
parse_label( struct translate_ctx
*ctx
, uint
*val
)
326 const char *cur
= ctx
->cur
;
328 if (parse_uint( &cur
, val
)) {
329 eat_opt_white( &cur
);
340 parse_file( const char **pcur
, uint
*file
)
344 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++) {
345 const char *cur
= *pcur
;
347 if (str_match_nocase_whole( &cur
, tgsi_file_name(i
) )) {
358 struct translate_ctx
*ctx
,
364 eat_opt_white( &cur
);
367 *writemask
= TGSI_WRITEMASK_NONE
;
368 eat_opt_white( &cur
);
369 if (uprcase( *cur
) == 'X') {
371 *writemask
|= TGSI_WRITEMASK_X
;
373 if (uprcase( *cur
) == 'Y') {
375 *writemask
|= TGSI_WRITEMASK_Y
;
377 if (uprcase( *cur
) == 'Z') {
379 *writemask
|= TGSI_WRITEMASK_Z
;
381 if (uprcase( *cur
) == 'W') {
383 *writemask
|= TGSI_WRITEMASK_W
;
386 if (*writemask
== TGSI_WRITEMASK_NONE
) {
387 report_error( ctx
, "Writemask expected" );
394 *writemask
= TGSI_WRITEMASK_XYZW
;
400 /* <register_file_bracket> ::= <file> `['
403 parse_register_file_bracket(
404 struct translate_ctx
*ctx
,
407 if (!parse_file( &ctx
->cur
, file
)) {
408 report_error( ctx
, "Unknown register file" );
411 eat_opt_white( &ctx
->cur
);
412 if (*ctx
->cur
!= '[') {
413 report_error( ctx
, "Expected `['" );
420 /* <register_file_bracket_index> ::= <register_file_bracket> <uint>
423 parse_register_file_bracket_index(
424 struct translate_ctx
*ctx
,
430 if (!parse_register_file_bracket( ctx
, file
))
432 eat_opt_white( &ctx
->cur
);
433 if (!parse_uint( &ctx
->cur
, &uindex
)) {
434 report_error( ctx
, "Expected literal unsigned integer" );
437 *index
= (int) uindex
;
441 /* Parse simple 1d register operand.
442 * <register_dst> ::= <register_file_bracket_index> `]'
445 parse_register_1d(struct translate_ctx
*ctx
,
449 if (!parse_register_file_bracket_index( ctx
, file
, index
))
451 eat_opt_white( &ctx
->cur
);
452 if (*ctx
->cur
!= ']') {
453 report_error( ctx
, "Expected `]'" );
460 struct parsed_bracket
{
471 parse_register_bracket(
472 struct translate_ctx
*ctx
,
473 struct parsed_bracket
*brackets
)
478 memset(brackets
, 0, sizeof(struct parsed_bracket
));
480 eat_opt_white( &ctx
->cur
);
483 if (parse_file( &cur
, &brackets
->ind_file
)) {
484 if (!parse_register_1d( ctx
, &brackets
->ind_file
,
485 &brackets
->ind_index
))
487 eat_opt_white( &ctx
->cur
);
489 if (*ctx
->cur
== '.') {
491 eat_opt_white(&ctx
->cur
);
493 switch (uprcase(*ctx
->cur
)) {
495 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
498 brackets
->ind_comp
= TGSI_SWIZZLE_Y
;
501 brackets
->ind_comp
= TGSI_SWIZZLE_Z
;
504 brackets
->ind_comp
= TGSI_SWIZZLE_W
;
507 report_error(ctx
, "Expected indirect register swizzle component `x', `y', `z' or `w'");
511 eat_opt_white(&ctx
->cur
);
514 if (*ctx
->cur
== '+' || *ctx
->cur
== '-')
515 parse_int( &ctx
->cur
, &brackets
->index
);
520 if (!parse_uint( &ctx
->cur
, &uindex
)) {
521 report_error( ctx
, "Expected literal unsigned integer" );
524 brackets
->index
= (int) uindex
;
525 brackets
->ind_file
= TGSI_FILE_NULL
;
526 brackets
->ind_index
= 0;
528 eat_opt_white( &ctx
->cur
);
529 if (*ctx
->cur
!= ']') {
530 report_error( ctx
, "Expected `]'" );
534 if (*ctx
->cur
== '(') {
536 eat_opt_white( &ctx
->cur
);
537 if (!parse_uint( &ctx
->cur
, &brackets
->ind_array
)) {
538 report_error( ctx
, "Expected literal unsigned integer" );
541 eat_opt_white( &ctx
->cur
);
542 if (*ctx
->cur
!= ')') {
543 report_error( ctx
, "Expected `)'" );
552 parse_opt_register_src_bracket(
553 struct translate_ctx
*ctx
,
554 struct parsed_bracket
*brackets
,
555 int *parsed_brackets
)
557 const char *cur
= ctx
->cur
;
559 *parsed_brackets
= 0;
561 eat_opt_white( &cur
);
566 if (!parse_register_bracket(ctx
, brackets
))
569 *parsed_brackets
= 1;
576 /* Parse source register operand.
577 * <register_src> ::= <register_file_bracket_index> `]' |
578 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `]' |
579 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `+' <uint> `]' |
580 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `-' <uint> `]'
584 struct translate_ctx
*ctx
,
586 struct parsed_bracket
*brackets
)
588 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
589 if (!parse_register_file_bracket( ctx
, file
))
591 if (!parse_register_bracket( ctx
, brackets
))
597 struct parsed_dcl_bracket
{
603 parse_register_dcl_bracket(
604 struct translate_ctx
*ctx
,
605 struct parsed_dcl_bracket
*bracket
)
608 memset(bracket
, 0, sizeof(struct parsed_dcl_bracket
));
610 eat_opt_white( &ctx
->cur
);
612 if (!parse_uint( &ctx
->cur
, &uindex
)) {
613 /* it can be an empty bracket [] which means its range
614 * is from 0 to some implied size */
615 if (ctx
->cur
[0] == ']' && ctx
->implied_array_size
!= 0) {
617 bracket
->last
= ctx
->implied_array_size
- 1;
620 report_error( ctx
, "Expected literal unsigned integer" );
623 bracket
->first
= uindex
;
625 eat_opt_white( &ctx
->cur
);
627 if (ctx
->cur
[0] == '.' && ctx
->cur
[1] == '.') {
631 eat_opt_white( &ctx
->cur
);
632 if (!parse_uint( &ctx
->cur
, &uindex
)) {
633 report_error( ctx
, "Expected literal integer" );
636 bracket
->last
= (int) uindex
;
637 eat_opt_white( &ctx
->cur
);
640 bracket
->last
= bracket
->first
;
644 if (*ctx
->cur
!= ']') {
645 report_error( ctx
, "Expected `]' or `..'" );
652 /* Parse register declaration.
653 * <register_dcl> ::= <register_file_bracket_index> `]' |
654 * <register_file_bracket_index> `..' <index> `]'
658 struct translate_ctx
*ctx
,
660 struct parsed_dcl_bracket
*brackets
,
667 if (!parse_register_file_bracket( ctx
, file
))
669 if (!parse_register_dcl_bracket( ctx
, &brackets
[0] ))
675 eat_opt_white( &cur
);
680 if (!parse_register_dcl_bracket( ctx
, &brackets
[1] ))
682 /* for geometry shader we don't really care about
683 * the first brackets it's always the size of the
684 * input primitive. so we want to declare just
685 * the index relevant to the semantics which is in
686 * the second bracket */
687 if (ctx
->processor
== TGSI_PROCESSOR_GEOMETRY
&& *file
== TGSI_FILE_INPUT
) {
688 brackets
[0] = brackets
[1];
699 /* Parse destination register operand.*/
702 struct translate_ctx
*ctx
,
704 struct parsed_bracket
*brackets
)
706 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
707 if (!parse_register_file_bracket( ctx
, file
))
709 if (!parse_register_bracket( ctx
, brackets
))
717 struct translate_ctx
*ctx
,
718 struct tgsi_full_dst_register
*dst
)
723 struct parsed_bracket bracket
[2];
724 int parsed_opt_brackets
;
726 if (!parse_register_dst( ctx
, &file
, &bracket
[0] ))
728 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
732 eat_opt_white( &cur
);
734 if (!parse_opt_writemask( ctx
, &writemask
))
737 dst
->Register
.File
= file
;
738 if (parsed_opt_brackets
) {
739 dst
->Register
.Dimension
= 1;
740 dst
->Dimension
.Indirect
= 0;
741 dst
->Dimension
.Dimension
= 0;
742 dst
->Dimension
.Index
= bracket
[0].index
;
743 bracket
[0] = bracket
[1];
745 dst
->Register
.Index
= bracket
[0].index
;
746 dst
->Register
.WriteMask
= writemask
;
747 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
748 dst
->Register
.Indirect
= 1;
749 dst
->Indirect
.File
= bracket
[0].ind_file
;
750 dst
->Indirect
.Index
= bracket
[0].ind_index
;
751 dst
->Indirect
.Swizzle
= bracket
[0].ind_comp
;
752 dst
->Indirect
.ArrayID
= bracket
[0].ind_array
;
758 parse_optional_swizzle(
759 struct translate_ctx
*ctx
,
761 boolean
*parsed_swizzle
,
764 const char *cur
= ctx
->cur
;
766 *parsed_swizzle
= FALSE
;
768 eat_opt_white( &cur
);
773 eat_opt_white( &cur
);
774 for (i
= 0; i
< components
; i
++) {
775 if (uprcase( *cur
) == 'X')
776 swizzle
[i
] = TGSI_SWIZZLE_X
;
777 else if (uprcase( *cur
) == 'Y')
778 swizzle
[i
] = TGSI_SWIZZLE_Y
;
779 else if (uprcase( *cur
) == 'Z')
780 swizzle
[i
] = TGSI_SWIZZLE_Z
;
781 else if (uprcase( *cur
) == 'W')
782 swizzle
[i
] = TGSI_SWIZZLE_W
;
784 report_error( ctx
, "Expected register swizzle component `x', `y', `z' or `w'" );
789 *parsed_swizzle
= TRUE
;
797 struct translate_ctx
*ctx
,
798 struct tgsi_full_src_register
*src
)
802 boolean parsed_swizzle
;
803 struct parsed_bracket bracket
[2];
804 int parsed_opt_brackets
;
806 if (*ctx
->cur
== '-') {
808 eat_opt_white( &ctx
->cur
);
809 src
->Register
.Negate
= 1;
812 if (*ctx
->cur
== '|') {
814 eat_opt_white( &ctx
->cur
);
815 src
->Register
.Absolute
= 1;
818 if (!parse_register_src(ctx
, &file
, &bracket
[0]))
820 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
823 src
->Register
.File
= file
;
824 if (parsed_opt_brackets
) {
825 src
->Register
.Dimension
= 1;
826 src
->Dimension
.Indirect
= 0;
827 src
->Dimension
.Dimension
= 0;
828 src
->Dimension
.Index
= bracket
[0].index
;
829 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
830 src
->Dimension
.Indirect
= 1;
831 src
->DimIndirect
.File
= bracket
[0].ind_file
;
832 src
->DimIndirect
.Index
= bracket
[0].ind_index
;
833 src
->DimIndirect
.Swizzle
= bracket
[0].ind_comp
;
834 src
->DimIndirect
.ArrayID
= bracket
[0].ind_array
;
836 bracket
[0] = bracket
[1];
838 src
->Register
.Index
= bracket
[0].index
;
839 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
840 src
->Register
.Indirect
= 1;
841 src
->Indirect
.File
= bracket
[0].ind_file
;
842 src
->Indirect
.Index
= bracket
[0].ind_index
;
843 src
->Indirect
.Swizzle
= bracket
[0].ind_comp
;
844 src
->Indirect
.ArrayID
= bracket
[0].ind_array
;
847 /* Parse optional swizzle.
849 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
, 4 )) {
850 if (parsed_swizzle
) {
851 src
->Register
.SwizzleX
= swizzle
[0];
852 src
->Register
.SwizzleY
= swizzle
[1];
853 src
->Register
.SwizzleZ
= swizzle
[2];
854 src
->Register
.SwizzleW
= swizzle
[3];
858 if (src
->Register
.Absolute
) {
859 eat_opt_white( &ctx
->cur
);
860 if (*ctx
->cur
!= '|') {
861 report_error( ctx
, "Expected `|'" );
872 parse_texoffset_operand(
873 struct translate_ctx
*ctx
,
874 struct tgsi_texture_offset
*src
)
878 boolean parsed_swizzle
;
879 struct parsed_bracket bracket
;
881 if (!parse_register_src(ctx
, &file
, &bracket
))
885 src
->Index
= bracket
.index
;
887 /* Parse optional swizzle.
889 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
, 3 )) {
890 if (parsed_swizzle
) {
891 src
->SwizzleX
= swizzle
[0];
892 src
->SwizzleY
= swizzle
[1];
893 src
->SwizzleZ
= swizzle
[2];
901 match_inst(const char **pcur
,
903 const struct tgsi_opcode_info
*info
)
905 const char *cur
= *pcur
;
907 /* simple case: the whole string matches the instruction name */
908 if (str_match_nocase_whole(&cur
, info
->mnemonic
)) {
914 if (str_match_no_case(&cur
, info
->mnemonic
)) {
915 /* the instruction has a suffix, figure it out */
916 if (str_match_nocase_whole(&cur
, "_SAT")) {
928 struct translate_ctx
*ctx
,
933 const struct tgsi_opcode_info
*info
;
934 struct tgsi_full_instruction inst
;
938 inst
= tgsi_default_full_instruction();
942 eat_opt_white( &ctx
->cur
);
943 if (*ctx
->cur
== '(') {
947 boolean parsed_swizzle
;
949 inst
.Instruction
.Predicate
= 1;
952 if (*ctx
->cur
== '!') {
954 inst
.Predicate
.Negate
= 1;
957 if (!parse_register_1d( ctx
, &file
, &index
))
960 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
, 4 )) {
961 if (parsed_swizzle
) {
962 inst
.Predicate
.SwizzleX
= swizzle
[0];
963 inst
.Predicate
.SwizzleY
= swizzle
[1];
964 inst
.Predicate
.SwizzleZ
= swizzle
[2];
965 inst
.Predicate
.SwizzleW
= swizzle
[3];
969 if (*ctx
->cur
!= ')') {
970 report_error( ctx
, "Expected `)'" );
977 /* Parse instruction name.
979 eat_opt_white( &ctx
->cur
);
980 for (i
= 0; i
< TGSI_OPCODE_LAST
; i
++) {
983 info
= tgsi_get_opcode_info( i
);
984 if (match_inst(&cur
, &saturate
, info
)) {
985 if (info
->num_dst
+ info
->num_src
+ info
->is_tex
== 0) {
989 else if (*cur
== '\0' || eat_white( &cur
)) {
995 if (i
== TGSI_OPCODE_LAST
) {
997 report_error( ctx
, "Unknown opcode" );
999 report_error( ctx
, "Expected `DCL', `IMM' or a label" );
1003 inst
.Instruction
.Opcode
= i
;
1004 inst
.Instruction
.Saturate
= saturate
;
1005 inst
.Instruction
.NumDstRegs
= info
->num_dst
;
1006 inst
.Instruction
.NumSrcRegs
= info
->num_src
;
1008 if (i
>= TGSI_OPCODE_SAMPLE
&& i
<= TGSI_OPCODE_GATHER4
) {
1010 * These are not considered tex opcodes here (no additional
1011 * target argument) however we're required to set the Texture
1012 * bit so we can set the number of tex offsets.
1014 inst
.Instruction
.Texture
= 1;
1015 inst
.Texture
.Texture
= TGSI_TEXTURE_UNKNOWN
;
1018 /* Parse instruction operands.
1020 for (i
= 0; i
< info
->num_dst
+ info
->num_src
+ info
->is_tex
; i
++) {
1022 eat_opt_white( &ctx
->cur
);
1023 if (*ctx
->cur
!= ',') {
1024 report_error( ctx
, "Expected `,'" );
1028 eat_opt_white( &ctx
->cur
);
1031 if (i
< info
->num_dst
) {
1032 if (!parse_dst_operand( ctx
, &inst
.Dst
[i
] ))
1035 else if (i
< info
->num_dst
+ info
->num_src
) {
1036 if (!parse_src_operand( ctx
, &inst
.Src
[i
- info
->num_dst
] ))
1042 for (j
= 0; j
< TGSI_TEXTURE_COUNT
; j
++) {
1043 if (str_match_nocase_whole( &ctx
->cur
, tgsi_texture_names
[j
] )) {
1044 inst
.Instruction
.Texture
= 1;
1045 inst
.Texture
.Texture
= j
;
1049 if (j
== TGSI_TEXTURE_COUNT
) {
1050 report_error( ctx
, "Expected texture target" );
1057 eat_opt_white( &cur
);
1058 for (i
= 0; inst
.Instruction
.Texture
&& *cur
== ','; i
++) {
1060 eat_opt_white( &cur
);
1062 if (!parse_texoffset_operand( ctx
, &inst
.TexOffsets
[i
] ))
1065 eat_opt_white( &cur
);
1067 inst
.Texture
.NumOffsets
= i
;
1070 eat_opt_white( &cur
);
1071 if (info
->is_branch
&& *cur
== ':') {
1075 eat_opt_white( &cur
);
1076 if (!parse_uint( &cur
, &target
)) {
1077 report_error( ctx
, "Expected a label" );
1080 inst
.Instruction
.Label
= 1;
1081 inst
.Label
.Label
= target
;
1085 advance
= tgsi_build_full_instruction(
1089 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1092 ctx
->tokens_cur
+= advance
;
1097 /* parses a 4-touple of the form {x, y, z, w}
1098 * where x, y, z, w are numbers */
1099 static boolean
parse_immediate_data(struct translate_ctx
*ctx
, unsigned type
,
1100 union tgsi_immediate_data
*values
)
1105 eat_opt_white( &ctx
->cur
);
1106 if (*ctx
->cur
!= '{') {
1107 report_error( ctx
, "Expected `{'" );
1111 for (i
= 0; i
< 4; i
++) {
1112 eat_opt_white( &ctx
->cur
);
1114 if (*ctx
->cur
!= ',') {
1115 report_error( ctx
, "Expected `,'" );
1119 eat_opt_white( &ctx
->cur
);
1123 case TGSI_IMM_FLOAT64
:
1124 ret
= parse_double(&ctx
->cur
, &values
[i
].Uint
, &values
[i
+1].Uint
);
1127 case TGSI_IMM_FLOAT32
:
1128 ret
= parse_float(&ctx
->cur
, &values
[i
].Float
);
1130 case TGSI_IMM_UINT32
:
1131 ret
= parse_uint(&ctx
->cur
, &values
[i
].Uint
);
1133 case TGSI_IMM_INT32
:
1134 ret
= parse_int(&ctx
->cur
, &values
[i
].Int
);
1143 report_error( ctx
, "Expected immediate constant" );
1147 eat_opt_white( &ctx
->cur
);
1148 if (*ctx
->cur
!= '}') {
1149 report_error( ctx
, "Expected `}'" );
1157 static boolean
parse_declaration( struct translate_ctx
*ctx
)
1159 struct tgsi_full_declaration decl
;
1161 struct parsed_dcl_bracket brackets
[2];
1164 const char *cur
, *cur2
;
1166 boolean is_vs_input
;
1168 if (!eat_white( &ctx
->cur
)) {
1169 report_error( ctx
, "Syntax error" );
1172 if (!parse_register_dcl( ctx
, &file
, brackets
, &num_brackets
))
1174 if (!parse_opt_writemask( ctx
, &writemask
))
1177 decl
= tgsi_default_full_declaration();
1178 decl
.Declaration
.File
= file
;
1179 decl
.Declaration
.UsageMask
= writemask
;
1181 if (num_brackets
== 1) {
1182 decl
.Range
.First
= brackets
[0].first
;
1183 decl
.Range
.Last
= brackets
[0].last
;
1185 decl
.Range
.First
= brackets
[1].first
;
1186 decl
.Range
.Last
= brackets
[1].last
;
1188 decl
.Declaration
.Dimension
= 1;
1189 decl
.Dim
.Index2D
= brackets
[0].first
;
1192 is_vs_input
= (file
== TGSI_FILE_INPUT
&&
1193 ctx
->processor
== TGSI_PROCESSOR_VERTEX
);
1196 eat_opt_white( &cur
);
1200 eat_opt_white( &cur2
);
1201 if (str_match_nocase_whole( &cur2
, "ARRAY" )) {
1204 report_error( ctx
, "Expected `('" );
1208 eat_opt_white( &cur2
);
1209 if (!parse_int( &cur2
, &arrayid
)) {
1210 report_error( ctx
, "Expected `,'" );
1213 eat_opt_white( &cur2
);
1215 report_error( ctx
, "Expected `)'" );
1219 decl
.Declaration
.Array
= 1;
1220 decl
.Array
.ArrayID
= arrayid
;
1221 ctx
->cur
= cur
= cur2
;
1225 if (*cur
== ',' && !is_vs_input
) {
1229 eat_opt_white( &cur
);
1230 if (file
== TGSI_FILE_RESOURCE
) {
1231 for (i
= 0; i
< TGSI_TEXTURE_COUNT
; i
++) {
1232 if (str_match_nocase_whole(&cur
, tgsi_texture_names
[i
])) {
1233 decl
.Resource
.Resource
= i
;
1237 if (i
== TGSI_TEXTURE_COUNT
) {
1238 report_error(ctx
, "Expected texture target");
1243 eat_opt_white(&cur2
);
1244 while (*cur2
== ',') {
1246 eat_opt_white(&cur2
);
1247 if (str_match_nocase_whole(&cur2
, "RAW")) {
1248 decl
.Resource
.Raw
= 1;
1250 } else if (str_match_nocase_whole(&cur2
, "WR")) {
1251 decl
.Resource
.Writable
= 1;
1257 eat_opt_white(&cur2
);
1262 } else if (file
== TGSI_FILE_SAMPLER_VIEW
) {
1263 for (i
= 0; i
< TGSI_TEXTURE_COUNT
; i
++) {
1264 if (str_match_nocase_whole(&cur
, tgsi_texture_names
[i
])) {
1265 decl
.SamplerView
.Resource
= i
;
1269 if (i
== TGSI_TEXTURE_COUNT
) {
1270 report_error(ctx
, "Expected texture target");
1273 eat_opt_white( &cur
);
1275 report_error( ctx
, "Expected `,'" );
1279 eat_opt_white( &cur
);
1280 for (j
= 0; j
< 4; ++j
) {
1281 for (i
= 0; i
< TGSI_RETURN_TYPE_COUNT
; ++i
) {
1282 if (str_match_nocase_whole(&cur
, tgsi_return_type_names
[i
])) {
1285 decl
.SamplerView
.ReturnTypeX
= i
;
1288 decl
.SamplerView
.ReturnTypeY
= i
;
1291 decl
.SamplerView
.ReturnTypeZ
= i
;
1294 decl
.SamplerView
.ReturnTypeW
= i
;
1302 if (i
== TGSI_RETURN_TYPE_COUNT
) {
1303 if (j
== 0 || j
> 2) {
1304 report_error(ctx
, "Expected type name");
1310 eat_opt_white( &cur2
);
1313 eat_opt_white( &cur2
);
1321 decl
.SamplerView
.ReturnTypeY
=
1322 decl
.SamplerView
.ReturnTypeZ
=
1323 decl
.SamplerView
.ReturnTypeW
=
1324 decl
.SamplerView
.ReturnTypeX
;
1328 if (str_match_nocase_whole(&cur
, "LOCAL")) {
1329 decl
.Declaration
.Local
= 1;
1334 eat_opt_white( &cur
);
1337 eat_opt_white( &cur
);
1339 for (i
= 0; i
< TGSI_SEMANTIC_COUNT
; i
++) {
1340 if (str_match_nocase_whole(&cur
, tgsi_semantic_names
[i
])) {
1344 eat_opt_white( &cur2
);
1347 eat_opt_white( &cur2
);
1348 if (!parse_uint( &cur2
, &index
)) {
1349 report_error( ctx
, "Expected literal integer" );
1352 eat_opt_white( &cur2
);
1354 report_error( ctx
, "Expected `]'" );
1359 decl
.Semantic
.Index
= index
;
1364 decl
.Declaration
.Semantic
= 1;
1365 decl
.Semantic
.Name
= i
;
1376 eat_opt_white( &cur
);
1377 if (*cur
== ',' && !is_vs_input
) {
1381 eat_opt_white( &cur
);
1382 for (i
= 0; i
< TGSI_INTERPOLATE_COUNT
; i
++) {
1383 if (str_match_nocase_whole( &cur
, tgsi_interpolate_names
[i
] )) {
1384 decl
.Declaration
.Interpolate
= 1;
1385 decl
.Interp
.Interpolate
= i
;
1391 if (i
== TGSI_INTERPOLATE_COUNT
) {
1392 report_error( ctx
, "Expected semantic or interpolate attribute" );
1398 eat_opt_white( &cur
);
1399 if (*cur
== ',' && !is_vs_input
) {
1403 eat_opt_white( &cur
);
1404 for (i
= 0; i
< TGSI_INTERPOLATE_LOC_COUNT
; i
++) {
1405 if (str_match_nocase_whole( &cur
, tgsi_interpolate_locations
[i
] )) {
1406 decl
.Interp
.Location
= i
;
1414 advance
= tgsi_build_full_declaration(
1418 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1422 ctx
->tokens_cur
+= advance
;
1427 static boolean
parse_immediate( struct translate_ctx
*ctx
)
1429 struct tgsi_full_immediate imm
;
1433 if (*ctx
->cur
== '[') {
1438 eat_opt_white( &ctx
->cur
);
1439 if (!parse_uint( &ctx
->cur
, &uindex
)) {
1440 report_error( ctx
, "Expected literal unsigned integer" );
1444 if (uindex
!= ctx
->num_immediates
) {
1445 report_error( ctx
, "Immediates must be sorted" );
1449 eat_opt_white( &ctx
->cur
);
1450 if (*ctx
->cur
!= ']') {
1451 report_error( ctx
, "Expected `]'" );
1458 if (!eat_white( &ctx
->cur
)) {
1459 report_error( ctx
, "Syntax error" );
1462 for (type
= 0; type
< Elements(tgsi_immediate_type_names
); ++type
) {
1463 if (str_match_nocase_whole(&ctx
->cur
, tgsi_immediate_type_names
[type
]))
1466 if (type
== Elements(tgsi_immediate_type_names
)) {
1467 report_error( ctx
, "Expected immediate type" );
1471 imm
= tgsi_default_full_immediate();
1472 imm
.Immediate
.NrTokens
+= 4;
1473 imm
.Immediate
.DataType
= type
;
1474 parse_immediate_data(ctx
, type
, imm
.u
);
1476 advance
= tgsi_build_full_immediate(
1480 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1483 ctx
->tokens_cur
+= advance
;
1485 ctx
->num_immediates
++;
1491 parse_primitive( const char **pcur
, uint
*primitive
)
1495 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++) {
1496 const char *cur
= *pcur
;
1498 if (str_match_nocase_whole( &cur
, tgsi_primitive_names
[i
])) {
1508 parse_fs_coord_origin( const char **pcur
, uint
*fs_coord_origin
)
1512 for (i
= 0; i
< Elements(tgsi_fs_coord_origin_names
); i
++) {
1513 const char *cur
= *pcur
;
1515 if (str_match_nocase_whole( &cur
, tgsi_fs_coord_origin_names
[i
])) {
1516 *fs_coord_origin
= i
;
1525 parse_fs_coord_pixel_center( const char **pcur
, uint
*fs_coord_pixel_center
)
1529 for (i
= 0; i
< Elements(tgsi_fs_coord_pixel_center_names
); i
++) {
1530 const char *cur
= *pcur
;
1532 if (str_match_nocase_whole( &cur
, tgsi_fs_coord_pixel_center_names
[i
])) {
1533 *fs_coord_pixel_center
= i
;
1542 static boolean
parse_property( struct translate_ctx
*ctx
)
1544 struct tgsi_full_property prop
;
1550 if (!eat_white( &ctx
->cur
)) {
1551 report_error( ctx
, "Syntax error" );
1554 if (!parse_identifier( &ctx
->cur
, id
)) {
1555 report_error( ctx
, "Syntax error" );
1558 for (property_name
= 0; property_name
< TGSI_PROPERTY_COUNT
;
1560 if (streq_nocase_uprcase(tgsi_property_names
[property_name
], id
)) {
1564 if (property_name
>= TGSI_PROPERTY_COUNT
) {
1565 debug_printf( "\nError: Unknown property : '%s'", id
);
1569 eat_opt_white( &ctx
->cur
);
1570 switch(property_name
) {
1571 case TGSI_PROPERTY_GS_INPUT_PRIM
:
1572 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
1573 if (!parse_primitive(&ctx
->cur
, &values
[0] )) {
1574 report_error( ctx
, "Unknown primitive name as property!" );
1577 if (property_name
== TGSI_PROPERTY_GS_INPUT_PRIM
&&
1578 ctx
->processor
== TGSI_PROCESSOR_GEOMETRY
) {
1579 ctx
->implied_array_size
= u_vertices_per_prim(values
[0]);
1582 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
1583 if (!parse_fs_coord_origin(&ctx
->cur
, &values
[0] )) {
1584 report_error( ctx
, "Unknown coord origin as property: must be UPPER_LEFT or LOWER_LEFT!" );
1588 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
1589 if (!parse_fs_coord_pixel_center(&ctx
->cur
, &values
[0] )) {
1590 report_error( ctx
, "Unknown coord pixel center as property: must be HALF_INTEGER or INTEGER!" );
1594 case TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
:
1596 if (!parse_uint(&ctx
->cur
, &values
[0] )) {
1597 report_error( ctx
, "Expected unsigned integer as property!" );
1602 prop
= tgsi_default_full_property();
1603 prop
.Property
.PropertyName
= property_name
;
1604 prop
.Property
.NrTokens
+= 1;
1605 prop
.u
[0].Data
= values
[0];
1607 advance
= tgsi_build_full_property(
1611 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1614 ctx
->tokens_cur
+= advance
;
1620 static boolean
translate( struct translate_ctx
*ctx
)
1622 eat_opt_white( &ctx
->cur
);
1623 if (!parse_header( ctx
))
1626 if (ctx
->processor
== TGSI_PROCESSOR_TESS_CTRL
||
1627 ctx
->processor
== TGSI_PROCESSOR_TESS_EVAL
)
1628 ctx
->implied_array_size
= 32;
1630 while (*ctx
->cur
!= '\0') {
1632 if (!eat_white( &ctx
->cur
)) {
1633 report_error( ctx
, "Syntax error" );
1637 if (*ctx
->cur
== '\0')
1639 if (parse_label( ctx
, &label_val
)) {
1640 if (!parse_instruction( ctx
, TRUE
))
1643 else if (str_match_nocase_whole( &ctx
->cur
, "DCL" )) {
1644 if (!parse_declaration( ctx
))
1647 else if (str_match_nocase_whole( &ctx
->cur
, "IMM" )) {
1648 if (!parse_immediate( ctx
))
1651 else if (str_match_nocase_whole( &ctx
->cur
, "PROPERTY" )) {
1652 if (!parse_property( ctx
))
1655 else if (!parse_instruction( ctx
, FALSE
)) {
1664 tgsi_text_translate(
1666 struct tgsi_token
*tokens
,
1669 struct translate_ctx ctx
= {0};
1673 ctx
.tokens
= tokens
;
1674 ctx
.tokens_cur
= tokens
;
1675 ctx
.tokens_end
= tokens
+ num_tokens
;
1677 if (!translate( &ctx
))
1680 return tgsi_sanity_check( tokens
);