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
);
678 bool is_in
= *file
== TGSI_FILE_INPUT
;
679 bool is_out
= *file
== TGSI_FILE_OUTPUT
;
683 if (!parse_register_dcl_bracket( ctx
, &brackets
[1] ))
685 /* for geometry shader we don't really care about
686 * the first brackets it's always the size of the
687 * input primitive. so we want to declare just
688 * the index relevant to the semantics which is in
689 * the second bracket */
691 /* tessellation has similar constraints to geometry shader */
692 if ((ctx
->processor
== TGSI_PROCESSOR_GEOMETRY
&& is_in
) ||
693 (ctx
->processor
== TGSI_PROCESSOR_TESS_EVAL
&& is_in
) ||
694 (ctx
->processor
== TGSI_PROCESSOR_TESS_CTRL
&& (is_in
|| is_out
))) {
695 brackets
[0] = brackets
[1];
706 /* Parse destination register operand.*/
709 struct translate_ctx
*ctx
,
711 struct parsed_bracket
*brackets
)
713 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
714 if (!parse_register_file_bracket( ctx
, file
))
716 if (!parse_register_bracket( ctx
, brackets
))
724 struct translate_ctx
*ctx
,
725 struct tgsi_full_dst_register
*dst
)
730 struct parsed_bracket bracket
[2];
731 int parsed_opt_brackets
;
733 if (!parse_register_dst( ctx
, &file
, &bracket
[0] ))
735 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
739 eat_opt_white( &cur
);
741 if (!parse_opt_writemask( ctx
, &writemask
))
744 dst
->Register
.File
= file
;
745 if (parsed_opt_brackets
) {
746 dst
->Register
.Dimension
= 1;
747 dst
->Dimension
.Indirect
= 0;
748 dst
->Dimension
.Dimension
= 0;
749 dst
->Dimension
.Index
= bracket
[0].index
;
751 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
752 dst
->Dimension
.Indirect
= 1;
753 dst
->DimIndirect
.File
= bracket
[0].ind_file
;
754 dst
->DimIndirect
.Index
= bracket
[0].ind_index
;
755 dst
->DimIndirect
.Swizzle
= bracket
[0].ind_comp
;
756 dst
->DimIndirect
.ArrayID
= bracket
[0].ind_array
;
758 bracket
[0] = bracket
[1];
760 dst
->Register
.Index
= bracket
[0].index
;
761 dst
->Register
.WriteMask
= writemask
;
762 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
763 dst
->Register
.Indirect
= 1;
764 dst
->Indirect
.File
= bracket
[0].ind_file
;
765 dst
->Indirect
.Index
= bracket
[0].ind_index
;
766 dst
->Indirect
.Swizzle
= bracket
[0].ind_comp
;
767 dst
->Indirect
.ArrayID
= bracket
[0].ind_array
;
773 parse_optional_swizzle(
774 struct translate_ctx
*ctx
,
776 boolean
*parsed_swizzle
,
779 const char *cur
= ctx
->cur
;
781 *parsed_swizzle
= FALSE
;
783 eat_opt_white( &cur
);
788 eat_opt_white( &cur
);
789 for (i
= 0; i
< components
; i
++) {
790 if (uprcase( *cur
) == 'X')
791 swizzle
[i
] = TGSI_SWIZZLE_X
;
792 else if (uprcase( *cur
) == 'Y')
793 swizzle
[i
] = TGSI_SWIZZLE_Y
;
794 else if (uprcase( *cur
) == 'Z')
795 swizzle
[i
] = TGSI_SWIZZLE_Z
;
796 else if (uprcase( *cur
) == 'W')
797 swizzle
[i
] = TGSI_SWIZZLE_W
;
799 report_error( ctx
, "Expected register swizzle component `x', `y', `z' or `w'" );
804 *parsed_swizzle
= TRUE
;
812 struct translate_ctx
*ctx
,
813 struct tgsi_full_src_register
*src
)
817 boolean parsed_swizzle
;
818 struct parsed_bracket bracket
[2];
819 int parsed_opt_brackets
;
821 if (*ctx
->cur
== '-') {
823 eat_opt_white( &ctx
->cur
);
824 src
->Register
.Negate
= 1;
827 if (*ctx
->cur
== '|') {
829 eat_opt_white( &ctx
->cur
);
830 src
->Register
.Absolute
= 1;
833 if (!parse_register_src(ctx
, &file
, &bracket
[0]))
835 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
838 src
->Register
.File
= file
;
839 if (parsed_opt_brackets
) {
840 src
->Register
.Dimension
= 1;
841 src
->Dimension
.Indirect
= 0;
842 src
->Dimension
.Dimension
= 0;
843 src
->Dimension
.Index
= bracket
[0].index
;
844 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
845 src
->Dimension
.Indirect
= 1;
846 src
->DimIndirect
.File
= bracket
[0].ind_file
;
847 src
->DimIndirect
.Index
= bracket
[0].ind_index
;
848 src
->DimIndirect
.Swizzle
= bracket
[0].ind_comp
;
849 src
->DimIndirect
.ArrayID
= bracket
[0].ind_array
;
851 bracket
[0] = bracket
[1];
853 src
->Register
.Index
= bracket
[0].index
;
854 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
855 src
->Register
.Indirect
= 1;
856 src
->Indirect
.File
= bracket
[0].ind_file
;
857 src
->Indirect
.Index
= bracket
[0].ind_index
;
858 src
->Indirect
.Swizzle
= bracket
[0].ind_comp
;
859 src
->Indirect
.ArrayID
= bracket
[0].ind_array
;
862 /* Parse optional swizzle.
864 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
, 4 )) {
865 if (parsed_swizzle
) {
866 src
->Register
.SwizzleX
= swizzle
[0];
867 src
->Register
.SwizzleY
= swizzle
[1];
868 src
->Register
.SwizzleZ
= swizzle
[2];
869 src
->Register
.SwizzleW
= swizzle
[3];
873 if (src
->Register
.Absolute
) {
874 eat_opt_white( &ctx
->cur
);
875 if (*ctx
->cur
!= '|') {
876 report_error( ctx
, "Expected `|'" );
887 parse_texoffset_operand(
888 struct translate_ctx
*ctx
,
889 struct tgsi_texture_offset
*src
)
893 boolean parsed_swizzle
;
894 struct parsed_bracket bracket
;
896 if (!parse_register_src(ctx
, &file
, &bracket
))
900 src
->Index
= bracket
.index
;
902 /* Parse optional swizzle.
904 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
, 3 )) {
905 if (parsed_swizzle
) {
906 src
->SwizzleX
= swizzle
[0];
907 src
->SwizzleY
= swizzle
[1];
908 src
->SwizzleZ
= swizzle
[2];
916 match_inst(const char **pcur
,
918 const struct tgsi_opcode_info
*info
)
920 const char *cur
= *pcur
;
922 /* simple case: the whole string matches the instruction name */
923 if (str_match_nocase_whole(&cur
, info
->mnemonic
)) {
929 if (str_match_no_case(&cur
, info
->mnemonic
)) {
930 /* the instruction has a suffix, figure it out */
931 if (str_match_nocase_whole(&cur
, "_SAT")) {
943 struct translate_ctx
*ctx
,
948 const struct tgsi_opcode_info
*info
;
949 struct tgsi_full_instruction inst
;
953 inst
= tgsi_default_full_instruction();
957 eat_opt_white( &ctx
->cur
);
958 if (*ctx
->cur
== '(') {
962 boolean parsed_swizzle
;
964 inst
.Instruction
.Predicate
= 1;
967 if (*ctx
->cur
== '!') {
969 inst
.Predicate
.Negate
= 1;
972 if (!parse_register_1d( ctx
, &file
, &index
))
975 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
, 4 )) {
976 if (parsed_swizzle
) {
977 inst
.Predicate
.SwizzleX
= swizzle
[0];
978 inst
.Predicate
.SwizzleY
= swizzle
[1];
979 inst
.Predicate
.SwizzleZ
= swizzle
[2];
980 inst
.Predicate
.SwizzleW
= swizzle
[3];
984 if (*ctx
->cur
!= ')') {
985 report_error( ctx
, "Expected `)'" );
992 /* Parse instruction name.
994 eat_opt_white( &ctx
->cur
);
995 for (i
= 0; i
< TGSI_OPCODE_LAST
; i
++) {
998 info
= tgsi_get_opcode_info( i
);
999 if (match_inst(&cur
, &saturate
, info
)) {
1000 if (info
->num_dst
+ info
->num_src
+ info
->is_tex
== 0) {
1004 else if (*cur
== '\0' || eat_white( &cur
)) {
1010 if (i
== TGSI_OPCODE_LAST
) {
1012 report_error( ctx
, "Unknown opcode" );
1014 report_error( ctx
, "Expected `DCL', `IMM' or a label" );
1018 inst
.Instruction
.Opcode
= i
;
1019 inst
.Instruction
.Saturate
= saturate
;
1020 inst
.Instruction
.NumDstRegs
= info
->num_dst
;
1021 inst
.Instruction
.NumSrcRegs
= info
->num_src
;
1023 if (i
>= TGSI_OPCODE_SAMPLE
&& i
<= TGSI_OPCODE_GATHER4
) {
1025 * These are not considered tex opcodes here (no additional
1026 * target argument) however we're required to set the Texture
1027 * bit so we can set the number of tex offsets.
1029 inst
.Instruction
.Texture
= 1;
1030 inst
.Texture
.Texture
= TGSI_TEXTURE_UNKNOWN
;
1033 /* Parse instruction operands.
1035 for (i
= 0; i
< info
->num_dst
+ info
->num_src
+ info
->is_tex
; i
++) {
1037 eat_opt_white( &ctx
->cur
);
1038 if (*ctx
->cur
!= ',') {
1039 report_error( ctx
, "Expected `,'" );
1043 eat_opt_white( &ctx
->cur
);
1046 if (i
< info
->num_dst
) {
1047 if (!parse_dst_operand( ctx
, &inst
.Dst
[i
] ))
1050 else if (i
< info
->num_dst
+ info
->num_src
) {
1051 if (!parse_src_operand( ctx
, &inst
.Src
[i
- info
->num_dst
] ))
1057 for (j
= 0; j
< TGSI_TEXTURE_COUNT
; j
++) {
1058 if (str_match_nocase_whole( &ctx
->cur
, tgsi_texture_names
[j
] )) {
1059 inst
.Instruction
.Texture
= 1;
1060 inst
.Texture
.Texture
= j
;
1064 if (j
== TGSI_TEXTURE_COUNT
) {
1065 report_error( ctx
, "Expected texture target" );
1072 eat_opt_white( &cur
);
1073 for (i
= 0; inst
.Instruction
.Texture
&& *cur
== ','; i
++) {
1075 eat_opt_white( &cur
);
1077 if (!parse_texoffset_operand( ctx
, &inst
.TexOffsets
[i
] ))
1080 eat_opt_white( &cur
);
1082 inst
.Texture
.NumOffsets
= i
;
1085 eat_opt_white( &cur
);
1086 if (info
->is_branch
&& *cur
== ':') {
1090 eat_opt_white( &cur
);
1091 if (!parse_uint( &cur
, &target
)) {
1092 report_error( ctx
, "Expected a label" );
1095 inst
.Instruction
.Label
= 1;
1096 inst
.Label
.Label
= target
;
1100 advance
= tgsi_build_full_instruction(
1104 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1107 ctx
->tokens_cur
+= advance
;
1112 /* parses a 4-touple of the form {x, y, z, w}
1113 * where x, y, z, w are numbers */
1114 static boolean
parse_immediate_data(struct translate_ctx
*ctx
, unsigned type
,
1115 union tgsi_immediate_data
*values
)
1120 eat_opt_white( &ctx
->cur
);
1121 if (*ctx
->cur
!= '{') {
1122 report_error( ctx
, "Expected `{'" );
1126 for (i
= 0; i
< 4; i
++) {
1127 eat_opt_white( &ctx
->cur
);
1129 if (*ctx
->cur
!= ',') {
1130 report_error( ctx
, "Expected `,'" );
1134 eat_opt_white( &ctx
->cur
);
1138 case TGSI_IMM_FLOAT64
:
1139 ret
= parse_double(&ctx
->cur
, &values
[i
].Uint
, &values
[i
+1].Uint
);
1142 case TGSI_IMM_FLOAT32
:
1143 ret
= parse_float(&ctx
->cur
, &values
[i
].Float
);
1145 case TGSI_IMM_UINT32
:
1146 ret
= parse_uint(&ctx
->cur
, &values
[i
].Uint
);
1148 case TGSI_IMM_INT32
:
1149 ret
= parse_int(&ctx
->cur
, &values
[i
].Int
);
1158 report_error( ctx
, "Expected immediate constant" );
1162 eat_opt_white( &ctx
->cur
);
1163 if (*ctx
->cur
!= '}') {
1164 report_error( ctx
, "Expected `}'" );
1172 static boolean
parse_declaration( struct translate_ctx
*ctx
)
1174 struct tgsi_full_declaration decl
;
1176 struct parsed_dcl_bracket brackets
[2];
1179 const char *cur
, *cur2
;
1181 boolean is_vs_input
;
1183 if (!eat_white( &ctx
->cur
)) {
1184 report_error( ctx
, "Syntax error" );
1187 if (!parse_register_dcl( ctx
, &file
, brackets
, &num_brackets
))
1189 if (!parse_opt_writemask( ctx
, &writemask
))
1192 decl
= tgsi_default_full_declaration();
1193 decl
.Declaration
.File
= file
;
1194 decl
.Declaration
.UsageMask
= writemask
;
1196 if (num_brackets
== 1) {
1197 decl
.Range
.First
= brackets
[0].first
;
1198 decl
.Range
.Last
= brackets
[0].last
;
1200 decl
.Range
.First
= brackets
[1].first
;
1201 decl
.Range
.Last
= brackets
[1].last
;
1203 decl
.Declaration
.Dimension
= 1;
1204 decl
.Dim
.Index2D
= brackets
[0].first
;
1207 is_vs_input
= (file
== TGSI_FILE_INPUT
&&
1208 ctx
->processor
== TGSI_PROCESSOR_VERTEX
);
1211 eat_opt_white( &cur
);
1215 eat_opt_white( &cur2
);
1216 if (str_match_nocase_whole( &cur2
, "ARRAY" )) {
1219 report_error( ctx
, "Expected `('" );
1223 eat_opt_white( &cur2
);
1224 if (!parse_int( &cur2
, &arrayid
)) {
1225 report_error( ctx
, "Expected `,'" );
1228 eat_opt_white( &cur2
);
1230 report_error( ctx
, "Expected `)'" );
1234 decl
.Declaration
.Array
= 1;
1235 decl
.Array
.ArrayID
= arrayid
;
1236 ctx
->cur
= cur
= cur2
;
1240 if (*cur
== ',' && !is_vs_input
) {
1244 eat_opt_white( &cur
);
1245 if (file
== TGSI_FILE_RESOURCE
) {
1246 for (i
= 0; i
< TGSI_TEXTURE_COUNT
; i
++) {
1247 if (str_match_nocase_whole(&cur
, tgsi_texture_names
[i
])) {
1248 decl
.Resource
.Resource
= i
;
1252 if (i
== TGSI_TEXTURE_COUNT
) {
1253 report_error(ctx
, "Expected texture target");
1258 eat_opt_white(&cur2
);
1259 while (*cur2
== ',') {
1261 eat_opt_white(&cur2
);
1262 if (str_match_nocase_whole(&cur2
, "RAW")) {
1263 decl
.Resource
.Raw
= 1;
1265 } else if (str_match_nocase_whole(&cur2
, "WR")) {
1266 decl
.Resource
.Writable
= 1;
1272 eat_opt_white(&cur2
);
1277 } else if (file
== TGSI_FILE_SAMPLER_VIEW
) {
1278 for (i
= 0; i
< TGSI_TEXTURE_COUNT
; i
++) {
1279 if (str_match_nocase_whole(&cur
, tgsi_texture_names
[i
])) {
1280 decl
.SamplerView
.Resource
= i
;
1284 if (i
== TGSI_TEXTURE_COUNT
) {
1285 report_error(ctx
, "Expected texture target");
1288 eat_opt_white( &cur
);
1290 report_error( ctx
, "Expected `,'" );
1294 eat_opt_white( &cur
);
1295 for (j
= 0; j
< 4; ++j
) {
1296 for (i
= 0; i
< TGSI_RETURN_TYPE_COUNT
; ++i
) {
1297 if (str_match_nocase_whole(&cur
, tgsi_return_type_names
[i
])) {
1300 decl
.SamplerView
.ReturnTypeX
= i
;
1303 decl
.SamplerView
.ReturnTypeY
= i
;
1306 decl
.SamplerView
.ReturnTypeZ
= i
;
1309 decl
.SamplerView
.ReturnTypeW
= i
;
1317 if (i
== TGSI_RETURN_TYPE_COUNT
) {
1318 if (j
== 0 || j
> 2) {
1319 report_error(ctx
, "Expected type name");
1325 eat_opt_white( &cur2
);
1328 eat_opt_white( &cur2
);
1336 decl
.SamplerView
.ReturnTypeY
=
1337 decl
.SamplerView
.ReturnTypeZ
=
1338 decl
.SamplerView
.ReturnTypeW
=
1339 decl
.SamplerView
.ReturnTypeX
;
1343 if (str_match_nocase_whole(&cur
, "LOCAL")) {
1344 decl
.Declaration
.Local
= 1;
1349 eat_opt_white( &cur
);
1352 eat_opt_white( &cur
);
1354 for (i
= 0; i
< TGSI_SEMANTIC_COUNT
; i
++) {
1355 if (str_match_nocase_whole(&cur
, tgsi_semantic_names
[i
])) {
1359 eat_opt_white( &cur2
);
1362 eat_opt_white( &cur2
);
1363 if (!parse_uint( &cur2
, &index
)) {
1364 report_error( ctx
, "Expected literal integer" );
1367 eat_opt_white( &cur2
);
1369 report_error( ctx
, "Expected `]'" );
1374 decl
.Semantic
.Index
= index
;
1379 decl
.Declaration
.Semantic
= 1;
1380 decl
.Semantic
.Name
= i
;
1391 eat_opt_white( &cur
);
1392 if (*cur
== ',' && !is_vs_input
) {
1396 eat_opt_white( &cur
);
1397 for (i
= 0; i
< TGSI_INTERPOLATE_COUNT
; i
++) {
1398 if (str_match_nocase_whole( &cur
, tgsi_interpolate_names
[i
] )) {
1399 decl
.Declaration
.Interpolate
= 1;
1400 decl
.Interp
.Interpolate
= i
;
1406 if (i
== TGSI_INTERPOLATE_COUNT
) {
1407 report_error( ctx
, "Expected semantic or interpolate attribute" );
1413 eat_opt_white( &cur
);
1414 if (*cur
== ',' && !is_vs_input
) {
1418 eat_opt_white( &cur
);
1419 for (i
= 0; i
< TGSI_INTERPOLATE_LOC_COUNT
; i
++) {
1420 if (str_match_nocase_whole( &cur
, tgsi_interpolate_locations
[i
] )) {
1421 decl
.Interp
.Location
= i
;
1429 advance
= tgsi_build_full_declaration(
1433 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1437 ctx
->tokens_cur
+= advance
;
1442 static boolean
parse_immediate( struct translate_ctx
*ctx
)
1444 struct tgsi_full_immediate imm
;
1448 if (*ctx
->cur
== '[') {
1453 eat_opt_white( &ctx
->cur
);
1454 if (!parse_uint( &ctx
->cur
, &uindex
)) {
1455 report_error( ctx
, "Expected literal unsigned integer" );
1459 if (uindex
!= ctx
->num_immediates
) {
1460 report_error( ctx
, "Immediates must be sorted" );
1464 eat_opt_white( &ctx
->cur
);
1465 if (*ctx
->cur
!= ']') {
1466 report_error( ctx
, "Expected `]'" );
1473 if (!eat_white( &ctx
->cur
)) {
1474 report_error( ctx
, "Syntax error" );
1477 for (type
= 0; type
< Elements(tgsi_immediate_type_names
); ++type
) {
1478 if (str_match_nocase_whole(&ctx
->cur
, tgsi_immediate_type_names
[type
]))
1481 if (type
== Elements(tgsi_immediate_type_names
)) {
1482 report_error( ctx
, "Expected immediate type" );
1486 imm
= tgsi_default_full_immediate();
1487 imm
.Immediate
.NrTokens
+= 4;
1488 imm
.Immediate
.DataType
= type
;
1489 parse_immediate_data(ctx
, type
, imm
.u
);
1491 advance
= tgsi_build_full_immediate(
1495 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1498 ctx
->tokens_cur
+= advance
;
1500 ctx
->num_immediates
++;
1506 parse_primitive( const char **pcur
, uint
*primitive
)
1510 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++) {
1511 const char *cur
= *pcur
;
1513 if (str_match_nocase_whole( &cur
, tgsi_primitive_names
[i
])) {
1523 parse_fs_coord_origin( const char **pcur
, uint
*fs_coord_origin
)
1527 for (i
= 0; i
< Elements(tgsi_fs_coord_origin_names
); i
++) {
1528 const char *cur
= *pcur
;
1530 if (str_match_nocase_whole( &cur
, tgsi_fs_coord_origin_names
[i
])) {
1531 *fs_coord_origin
= i
;
1540 parse_fs_coord_pixel_center( const char **pcur
, uint
*fs_coord_pixel_center
)
1544 for (i
= 0; i
< Elements(tgsi_fs_coord_pixel_center_names
); i
++) {
1545 const char *cur
= *pcur
;
1547 if (str_match_nocase_whole( &cur
, tgsi_fs_coord_pixel_center_names
[i
])) {
1548 *fs_coord_pixel_center
= i
;
1557 static boolean
parse_property( struct translate_ctx
*ctx
)
1559 struct tgsi_full_property prop
;
1565 if (!eat_white( &ctx
->cur
)) {
1566 report_error( ctx
, "Syntax error" );
1569 if (!parse_identifier( &ctx
->cur
, id
)) {
1570 report_error( ctx
, "Syntax error" );
1573 for (property_name
= 0; property_name
< TGSI_PROPERTY_COUNT
;
1575 if (streq_nocase_uprcase(tgsi_property_names
[property_name
], id
)) {
1579 if (property_name
>= TGSI_PROPERTY_COUNT
) {
1580 debug_printf( "\nError: Unknown property : '%s'", id
);
1584 eat_opt_white( &ctx
->cur
);
1585 switch(property_name
) {
1586 case TGSI_PROPERTY_GS_INPUT_PRIM
:
1587 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
1588 if (!parse_primitive(&ctx
->cur
, &values
[0] )) {
1589 report_error( ctx
, "Unknown primitive name as property!" );
1592 if (property_name
== TGSI_PROPERTY_GS_INPUT_PRIM
&&
1593 ctx
->processor
== TGSI_PROCESSOR_GEOMETRY
) {
1594 ctx
->implied_array_size
= u_vertices_per_prim(values
[0]);
1597 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
1598 if (!parse_fs_coord_origin(&ctx
->cur
, &values
[0] )) {
1599 report_error( ctx
, "Unknown coord origin as property: must be UPPER_LEFT or LOWER_LEFT!" );
1603 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
1604 if (!parse_fs_coord_pixel_center(&ctx
->cur
, &values
[0] )) {
1605 report_error( ctx
, "Unknown coord pixel center as property: must be HALF_INTEGER or INTEGER!" );
1609 case TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
:
1611 if (!parse_uint(&ctx
->cur
, &values
[0] )) {
1612 report_error( ctx
, "Expected unsigned integer as property!" );
1617 prop
= tgsi_default_full_property();
1618 prop
.Property
.PropertyName
= property_name
;
1619 prop
.Property
.NrTokens
+= 1;
1620 prop
.u
[0].Data
= values
[0];
1622 advance
= tgsi_build_full_property(
1626 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1629 ctx
->tokens_cur
+= advance
;
1635 static boolean
translate( struct translate_ctx
*ctx
)
1637 eat_opt_white( &ctx
->cur
);
1638 if (!parse_header( ctx
))
1641 if (ctx
->processor
== TGSI_PROCESSOR_TESS_CTRL
||
1642 ctx
->processor
== TGSI_PROCESSOR_TESS_EVAL
)
1643 ctx
->implied_array_size
= 32;
1645 while (*ctx
->cur
!= '\0') {
1647 if (!eat_white( &ctx
->cur
)) {
1648 report_error( ctx
, "Syntax error" );
1652 if (*ctx
->cur
== '\0')
1654 if (parse_label( ctx
, &label_val
)) {
1655 if (!parse_instruction( ctx
, TRUE
))
1658 else if (str_match_nocase_whole( &ctx
->cur
, "DCL" )) {
1659 if (!parse_declaration( ctx
))
1662 else if (str_match_nocase_whole( &ctx
->cur
, "IMM" )) {
1663 if (!parse_immediate( ctx
))
1666 else if (str_match_nocase_whole( &ctx
->cur
, "PROPERTY" )) {
1667 if (!parse_property( ctx
))
1670 else if (!parse_instruction( ctx
, FALSE
)) {
1679 tgsi_text_translate(
1681 struct tgsi_token
*tokens
,
1684 struct translate_ctx ctx
= {0};
1688 ctx
.tokens
= tokens
;
1689 ctx
.tokens_cur
= tokens
;
1690 ctx
.tokens_end
= tokens
+ num_tokens
;
1692 if (!translate( &ctx
))
1695 return tgsi_sanity_check( tokens
);