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
);
302 struct tgsi_token
*tokens
;
303 struct tgsi_token
*tokens_cur
;
304 struct tgsi_token
*tokens_end
;
305 struct tgsi_header
*header
;
306 unsigned processor
: 4;
307 unsigned implied_array_size
: 6;
308 unsigned num_immediates
;
311 static void report_error( struct translate_ctx
*ctx
, const char *msg
)
315 const char *itr
= ctx
->text
;
317 while (itr
!= ctx
->cur
) {
326 debug_printf( "\nTGSI asm error: %s [%d : %d] \n", msg
, line
, column
);
329 /* Parse shader header.
330 * Return TRUE for one of the following headers.
335 static boolean
parse_header( struct translate_ctx
*ctx
)
339 if (str_match_nocase_whole( &ctx
->cur
, "FRAG" ))
340 processor
= PIPE_SHADER_FRAGMENT
;
341 else if (str_match_nocase_whole( &ctx
->cur
, "VERT" ))
342 processor
= PIPE_SHADER_VERTEX
;
343 else if (str_match_nocase_whole( &ctx
->cur
, "GEOM" ))
344 processor
= PIPE_SHADER_GEOMETRY
;
345 else if (str_match_nocase_whole( &ctx
->cur
, "TESS_CTRL" ))
346 processor
= PIPE_SHADER_TESS_CTRL
;
347 else if (str_match_nocase_whole( &ctx
->cur
, "TESS_EVAL" ))
348 processor
= PIPE_SHADER_TESS_EVAL
;
349 else if (str_match_nocase_whole( &ctx
->cur
, "COMP" ))
350 processor
= PIPE_SHADER_COMPUTE
;
352 report_error( ctx
, "Unknown header" );
356 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
358 ctx
->header
= (struct tgsi_header
*) ctx
->tokens_cur
++;
359 *ctx
->header
= tgsi_build_header();
361 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
363 *(struct tgsi_processor
*) ctx
->tokens_cur
++ = tgsi_build_processor( processor
, ctx
->header
);
364 ctx
->processor
= processor
;
369 static boolean
parse_label( struct translate_ctx
*ctx
, uint
*val
)
371 const char *cur
= ctx
->cur
;
373 if (parse_uint( &cur
, val
)) {
374 eat_opt_white( &cur
);
385 parse_file( const char **pcur
, uint
*file
)
389 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++) {
390 const char *cur
= *pcur
;
392 if (str_match_nocase_whole( &cur
, tgsi_file_name(i
) )) {
403 struct translate_ctx
*ctx
,
409 eat_opt_white( &cur
);
412 *writemask
= TGSI_WRITEMASK_NONE
;
413 eat_opt_white( &cur
);
414 if (uprcase( *cur
) == 'X') {
416 *writemask
|= TGSI_WRITEMASK_X
;
418 if (uprcase( *cur
) == 'Y') {
420 *writemask
|= TGSI_WRITEMASK_Y
;
422 if (uprcase( *cur
) == 'Z') {
424 *writemask
|= TGSI_WRITEMASK_Z
;
426 if (uprcase( *cur
) == 'W') {
428 *writemask
|= TGSI_WRITEMASK_W
;
431 if (*writemask
== TGSI_WRITEMASK_NONE
) {
432 report_error( ctx
, "Writemask expected" );
439 *writemask
= TGSI_WRITEMASK_XYZW
;
445 /* <register_file_bracket> ::= <file> `['
448 parse_register_file_bracket(
449 struct translate_ctx
*ctx
,
452 if (!parse_file( &ctx
->cur
, file
)) {
453 report_error( ctx
, "Unknown register file" );
456 eat_opt_white( &ctx
->cur
);
457 if (*ctx
->cur
!= '[') {
458 report_error( ctx
, "Expected `['" );
465 /* <register_file_bracket_index> ::= <register_file_bracket> <uint>
468 parse_register_file_bracket_index(
469 struct translate_ctx
*ctx
,
475 if (!parse_register_file_bracket( ctx
, file
))
477 eat_opt_white( &ctx
->cur
);
478 if (!parse_uint( &ctx
->cur
, &uindex
)) {
479 report_error( ctx
, "Expected literal unsigned integer" );
482 *index
= (int) uindex
;
486 /* Parse simple 1d register operand.
487 * <register_dst> ::= <register_file_bracket_index> `]'
490 parse_register_1d(struct translate_ctx
*ctx
,
494 if (!parse_register_file_bracket_index( ctx
, file
, index
))
496 eat_opt_white( &ctx
->cur
);
497 if (*ctx
->cur
!= ']') {
498 report_error( ctx
, "Expected `]'" );
505 struct parsed_bracket
{
516 parse_register_bracket(
517 struct translate_ctx
*ctx
,
518 struct parsed_bracket
*brackets
)
523 memset(brackets
, 0, sizeof(struct parsed_bracket
));
525 eat_opt_white( &ctx
->cur
);
528 if (parse_file( &cur
, &brackets
->ind_file
)) {
529 if (!parse_register_1d( ctx
, &brackets
->ind_file
,
530 &brackets
->ind_index
))
532 eat_opt_white( &ctx
->cur
);
534 if (*ctx
->cur
== '.') {
536 eat_opt_white(&ctx
->cur
);
538 switch (uprcase(*ctx
->cur
)) {
540 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
543 brackets
->ind_comp
= TGSI_SWIZZLE_Y
;
546 brackets
->ind_comp
= TGSI_SWIZZLE_Z
;
549 brackets
->ind_comp
= TGSI_SWIZZLE_W
;
552 report_error(ctx
, "Expected indirect register swizzle component `x', `y', `z' or `w'");
556 eat_opt_white(&ctx
->cur
);
559 if (*ctx
->cur
== '+' || *ctx
->cur
== '-')
560 parse_int( &ctx
->cur
, &brackets
->index
);
565 if (!parse_uint( &ctx
->cur
, &uindex
)) {
566 report_error( ctx
, "Expected literal unsigned integer" );
569 brackets
->index
= (int) uindex
;
570 brackets
->ind_file
= TGSI_FILE_NULL
;
571 brackets
->ind_index
= 0;
573 eat_opt_white( &ctx
->cur
);
574 if (*ctx
->cur
!= ']') {
575 report_error( ctx
, "Expected `]'" );
579 if (*ctx
->cur
== '(') {
581 eat_opt_white( &ctx
->cur
);
582 if (!parse_uint( &ctx
->cur
, &brackets
->ind_array
)) {
583 report_error( ctx
, "Expected literal unsigned integer" );
586 eat_opt_white( &ctx
->cur
);
587 if (*ctx
->cur
!= ')') {
588 report_error( ctx
, "Expected `)'" );
597 parse_opt_register_src_bracket(
598 struct translate_ctx
*ctx
,
599 struct parsed_bracket
*brackets
,
600 int *parsed_brackets
)
602 const char *cur
= ctx
->cur
;
604 *parsed_brackets
= 0;
606 eat_opt_white( &cur
);
611 if (!parse_register_bracket(ctx
, brackets
))
614 *parsed_brackets
= 1;
621 /* Parse source register operand.
622 * <register_src> ::= <register_file_bracket_index> `]' |
623 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `]' |
624 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `+' <uint> `]' |
625 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `-' <uint> `]'
629 struct translate_ctx
*ctx
,
631 struct parsed_bracket
*brackets
)
633 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
634 if (!parse_register_file_bracket( ctx
, file
))
636 if (!parse_register_bracket( ctx
, brackets
))
642 struct parsed_dcl_bracket
{
648 parse_register_dcl_bracket(
649 struct translate_ctx
*ctx
,
650 struct parsed_dcl_bracket
*bracket
)
653 memset(bracket
, 0, sizeof(struct parsed_dcl_bracket
));
655 eat_opt_white( &ctx
->cur
);
657 if (!parse_uint( &ctx
->cur
, &uindex
)) {
658 /* it can be an empty bracket [] which means its range
659 * is from 0 to some implied size */
660 if (ctx
->cur
[0] == ']' && ctx
->implied_array_size
!= 0) {
662 bracket
->last
= ctx
->implied_array_size
- 1;
665 report_error( ctx
, "Expected literal unsigned integer" );
668 bracket
->first
= uindex
;
670 eat_opt_white( &ctx
->cur
);
672 if (ctx
->cur
[0] == '.' && ctx
->cur
[1] == '.') {
676 eat_opt_white( &ctx
->cur
);
677 if (!parse_uint( &ctx
->cur
, &uindex
)) {
678 report_error( ctx
, "Expected literal integer" );
681 bracket
->last
= (int) uindex
;
682 eat_opt_white( &ctx
->cur
);
685 bracket
->last
= bracket
->first
;
689 if (*ctx
->cur
!= ']') {
690 report_error( ctx
, "Expected `]' or `..'" );
697 /* Parse register declaration.
698 * <register_dcl> ::= <register_file_bracket_index> `]' |
699 * <register_file_bracket_index> `..' <index> `]'
703 struct translate_ctx
*ctx
,
705 struct parsed_dcl_bracket
*brackets
,
712 if (!parse_register_file_bracket( ctx
, file
))
714 if (!parse_register_dcl_bracket( ctx
, &brackets
[0] ))
720 eat_opt_white( &cur
);
723 bool is_in
= *file
== TGSI_FILE_INPUT
;
724 bool is_out
= *file
== TGSI_FILE_OUTPUT
;
728 if (!parse_register_dcl_bracket( ctx
, &brackets
[1] ))
730 /* for geometry shader we don't really care about
731 * the first brackets it's always the size of the
732 * input primitive. so we want to declare just
733 * the index relevant to the semantics which is in
734 * the second bracket */
736 /* tessellation has similar constraints to geometry shader */
737 if ((ctx
->processor
== PIPE_SHADER_GEOMETRY
&& is_in
) ||
738 (ctx
->processor
== PIPE_SHADER_TESS_EVAL
&& is_in
) ||
739 (ctx
->processor
== PIPE_SHADER_TESS_CTRL
&& (is_in
|| is_out
))) {
740 brackets
[0] = brackets
[1];
751 /* Parse destination register operand.*/
754 struct translate_ctx
*ctx
,
756 struct parsed_bracket
*brackets
)
758 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
759 if (!parse_register_file_bracket( ctx
, file
))
761 if (!parse_register_bracket( ctx
, brackets
))
769 struct translate_ctx
*ctx
,
770 struct tgsi_full_dst_register
*dst
)
775 struct parsed_bracket bracket
[2];
776 int parsed_opt_brackets
;
778 if (!parse_register_dst( ctx
, &file
, &bracket
[0] ))
780 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
784 eat_opt_white( &cur
);
786 if (!parse_opt_writemask( ctx
, &writemask
))
789 dst
->Register
.File
= file
;
790 if (parsed_opt_brackets
) {
791 dst
->Register
.Dimension
= 1;
792 dst
->Dimension
.Indirect
= 0;
793 dst
->Dimension
.Dimension
= 0;
794 dst
->Dimension
.Index
= bracket
[0].index
;
796 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
797 dst
->Dimension
.Indirect
= 1;
798 dst
->DimIndirect
.File
= bracket
[0].ind_file
;
799 dst
->DimIndirect
.Index
= bracket
[0].ind_index
;
800 dst
->DimIndirect
.Swizzle
= bracket
[0].ind_comp
;
801 dst
->DimIndirect
.ArrayID
= bracket
[0].ind_array
;
803 bracket
[0] = bracket
[1];
805 dst
->Register
.Index
= bracket
[0].index
;
806 dst
->Register
.WriteMask
= writemask
;
807 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
808 dst
->Register
.Indirect
= 1;
809 dst
->Indirect
.File
= bracket
[0].ind_file
;
810 dst
->Indirect
.Index
= bracket
[0].ind_index
;
811 dst
->Indirect
.Swizzle
= bracket
[0].ind_comp
;
812 dst
->Indirect
.ArrayID
= bracket
[0].ind_array
;
818 parse_optional_swizzle(
819 struct translate_ctx
*ctx
,
821 boolean
*parsed_swizzle
,
824 const char *cur
= ctx
->cur
;
826 *parsed_swizzle
= FALSE
;
828 eat_opt_white( &cur
);
833 eat_opt_white( &cur
);
834 for (i
= 0; i
< components
; i
++) {
835 if (uprcase( *cur
) == 'X')
836 swizzle
[i
] = TGSI_SWIZZLE_X
;
837 else if (uprcase( *cur
) == 'Y')
838 swizzle
[i
] = TGSI_SWIZZLE_Y
;
839 else if (uprcase( *cur
) == 'Z')
840 swizzle
[i
] = TGSI_SWIZZLE_Z
;
841 else if (uprcase( *cur
) == 'W')
842 swizzle
[i
] = TGSI_SWIZZLE_W
;
844 report_error( ctx
, "Expected register swizzle component `x', `y', `z' or `w'" );
849 *parsed_swizzle
= TRUE
;
857 struct translate_ctx
*ctx
,
858 struct tgsi_full_src_register
*src
)
862 boolean parsed_swizzle
;
863 struct parsed_bracket bracket
[2];
864 int parsed_opt_brackets
;
866 if (*ctx
->cur
== '-') {
868 eat_opt_white( &ctx
->cur
);
869 src
->Register
.Negate
= 1;
872 if (*ctx
->cur
== '|') {
874 eat_opt_white( &ctx
->cur
);
875 src
->Register
.Absolute
= 1;
878 if (!parse_register_src(ctx
, &file
, &bracket
[0]))
880 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
883 src
->Register
.File
= file
;
884 if (parsed_opt_brackets
) {
885 src
->Register
.Dimension
= 1;
886 src
->Dimension
.Indirect
= 0;
887 src
->Dimension
.Dimension
= 0;
888 src
->Dimension
.Index
= bracket
[0].index
;
889 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
890 src
->Dimension
.Indirect
= 1;
891 src
->DimIndirect
.File
= bracket
[0].ind_file
;
892 src
->DimIndirect
.Index
= bracket
[0].ind_index
;
893 src
->DimIndirect
.Swizzle
= bracket
[0].ind_comp
;
894 src
->DimIndirect
.ArrayID
= bracket
[0].ind_array
;
896 bracket
[0] = bracket
[1];
898 src
->Register
.Index
= bracket
[0].index
;
899 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
900 src
->Register
.Indirect
= 1;
901 src
->Indirect
.File
= bracket
[0].ind_file
;
902 src
->Indirect
.Index
= bracket
[0].ind_index
;
903 src
->Indirect
.Swizzle
= bracket
[0].ind_comp
;
904 src
->Indirect
.ArrayID
= bracket
[0].ind_array
;
907 /* Parse optional swizzle.
909 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
, 4 )) {
910 if (parsed_swizzle
) {
911 src
->Register
.SwizzleX
= swizzle
[0];
912 src
->Register
.SwizzleY
= swizzle
[1];
913 src
->Register
.SwizzleZ
= swizzle
[2];
914 src
->Register
.SwizzleW
= swizzle
[3];
918 if (src
->Register
.Absolute
) {
919 eat_opt_white( &ctx
->cur
);
920 if (*ctx
->cur
!= '|') {
921 report_error( ctx
, "Expected `|'" );
932 parse_texoffset_operand(
933 struct translate_ctx
*ctx
,
934 struct tgsi_texture_offset
*src
)
938 boolean parsed_swizzle
;
939 struct parsed_bracket bracket
;
941 if (!parse_register_src(ctx
, &file
, &bracket
))
945 src
->Index
= bracket
.index
;
947 /* Parse optional swizzle.
949 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
, 3 )) {
950 if (parsed_swizzle
) {
951 src
->SwizzleX
= swizzle
[0];
952 src
->SwizzleY
= swizzle
[1];
953 src
->SwizzleZ
= swizzle
[2];
961 match_inst(const char **pcur
,
963 const struct tgsi_opcode_info
*info
)
965 const char *cur
= *pcur
;
967 /* simple case: the whole string matches the instruction name */
968 if (str_match_nocase_whole(&cur
, info
->mnemonic
)) {
974 if (str_match_no_case(&cur
, info
->mnemonic
)) {
975 /* the instruction has a suffix, figure it out */
976 if (str_match_nocase_whole(&cur
, "_SAT")) {
988 struct translate_ctx
*ctx
,
993 const struct tgsi_opcode_info
*info
;
994 struct tgsi_full_instruction inst
;
998 inst
= tgsi_default_full_instruction();
1002 eat_opt_white( &ctx
->cur
);
1003 if (*ctx
->cur
== '(') {
1007 boolean parsed_swizzle
;
1009 inst
.Instruction
.Predicate
= 1;
1012 if (*ctx
->cur
== '!') {
1014 inst
.Predicate
.Negate
= 1;
1017 if (!parse_register_1d( ctx
, &file
, &index
))
1020 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
, 4 )) {
1021 if (parsed_swizzle
) {
1022 inst
.Predicate
.SwizzleX
= swizzle
[0];
1023 inst
.Predicate
.SwizzleY
= swizzle
[1];
1024 inst
.Predicate
.SwizzleZ
= swizzle
[2];
1025 inst
.Predicate
.SwizzleW
= swizzle
[3];
1029 if (*ctx
->cur
!= ')') {
1030 report_error( ctx
, "Expected `)'" );
1037 /* Parse instruction name.
1039 eat_opt_white( &ctx
->cur
);
1040 for (i
= 0; i
< TGSI_OPCODE_LAST
; i
++) {
1043 info
= tgsi_get_opcode_info( i
);
1044 if (match_inst(&cur
, &saturate
, info
)) {
1045 if (info
->num_dst
+ info
->num_src
+ info
->is_tex
== 0) {
1049 else if (*cur
== '\0' || eat_white( &cur
)) {
1055 if (i
== TGSI_OPCODE_LAST
) {
1057 report_error( ctx
, "Unknown opcode" );
1059 report_error( ctx
, "Expected `DCL', `IMM' or a label" );
1063 inst
.Instruction
.Opcode
= i
;
1064 inst
.Instruction
.Saturate
= saturate
;
1065 inst
.Instruction
.NumDstRegs
= info
->num_dst
;
1066 inst
.Instruction
.NumSrcRegs
= info
->num_src
;
1068 if (i
>= TGSI_OPCODE_SAMPLE
&& i
<= TGSI_OPCODE_GATHER4
) {
1070 * These are not considered tex opcodes here (no additional
1071 * target argument) however we're required to set the Texture
1072 * bit so we can set the number of tex offsets.
1074 inst
.Instruction
.Texture
= 1;
1075 inst
.Texture
.Texture
= TGSI_TEXTURE_UNKNOWN
;
1078 if ((i
>= TGSI_OPCODE_LOAD
&& i
<= TGSI_OPCODE_ATOMIMAX
) ||
1079 i
== TGSI_OPCODE_RESQ
) {
1080 inst
.Instruction
.Memory
= 1;
1081 inst
.Memory
.Qualifier
= 0;
1084 /* Parse instruction operands.
1086 for (i
= 0; i
< info
->num_dst
+ info
->num_src
+ info
->is_tex
; i
++) {
1088 eat_opt_white( &ctx
->cur
);
1089 if (*ctx
->cur
!= ',') {
1090 report_error( ctx
, "Expected `,'" );
1094 eat_opt_white( &ctx
->cur
);
1097 if (i
< info
->num_dst
) {
1098 if (!parse_dst_operand( ctx
, &inst
.Dst
[i
] ))
1101 else if (i
< info
->num_dst
+ info
->num_src
) {
1102 if (!parse_src_operand( ctx
, &inst
.Src
[i
- info
->num_dst
] ))
1108 for (j
= 0; j
< TGSI_TEXTURE_COUNT
; j
++) {
1109 if (str_match_nocase_whole( &ctx
->cur
, tgsi_texture_names
[j
] )) {
1110 inst
.Instruction
.Texture
= 1;
1111 inst
.Texture
.Texture
= j
;
1115 if (j
== TGSI_TEXTURE_COUNT
) {
1116 report_error( ctx
, "Expected texture target" );
1123 eat_opt_white( &cur
);
1124 for (i
= 0; inst
.Instruction
.Texture
&& *cur
== ','; i
++) {
1126 eat_opt_white( &cur
);
1128 if (!parse_texoffset_operand( ctx
, &inst
.TexOffsets
[i
] ))
1131 eat_opt_white( &cur
);
1133 inst
.Texture
.NumOffsets
= i
;
1136 eat_opt_white(&cur
);
1138 for (; inst
.Instruction
.Memory
&& *cur
== ',';
1139 ctx
->cur
= cur
, eat_opt_white(&cur
)) {
1143 eat_opt_white(&cur
);
1145 j
= str_match_name_from_array(&cur
, tgsi_memory_names
,
1146 ARRAY_SIZE(tgsi_memory_names
));
1148 inst
.Memory
.Qualifier
|= 1U << j
;
1152 j
= str_match_name_from_array(&cur
, tgsi_texture_names
,
1153 ARRAY_SIZE(tgsi_texture_names
));
1155 inst
.Memory
.Texture
= j
;
1159 j
= str_match_format(&cur
);
1161 inst
.Memory
.Format
= j
;
1166 report_error(ctx
, "Expected memory qualifier, texture target, or format\n");
1171 eat_opt_white( &cur
);
1172 if (info
->is_branch
&& *cur
== ':') {
1176 eat_opt_white( &cur
);
1177 if (!parse_uint( &cur
, &target
)) {
1178 report_error( ctx
, "Expected a label" );
1181 inst
.Instruction
.Label
= 1;
1182 inst
.Label
.Label
= target
;
1186 advance
= tgsi_build_full_instruction(
1190 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1193 ctx
->tokens_cur
+= advance
;
1198 /* parses a 4-touple of the form {x, y, z, w}
1199 * where x, y, z, w are numbers */
1200 static boolean
parse_immediate_data(struct translate_ctx
*ctx
, unsigned type
,
1201 union tgsi_immediate_data
*values
)
1206 eat_opt_white( &ctx
->cur
);
1207 if (*ctx
->cur
!= '{') {
1208 report_error( ctx
, "Expected `{'" );
1212 for (i
= 0; i
< 4; i
++) {
1213 eat_opt_white( &ctx
->cur
);
1215 if (*ctx
->cur
!= ',') {
1216 report_error( ctx
, "Expected `,'" );
1220 eat_opt_white( &ctx
->cur
);
1224 case TGSI_IMM_FLOAT64
:
1225 ret
= parse_double(&ctx
->cur
, &values
[i
].Uint
, &values
[i
+1].Uint
);
1228 case TGSI_IMM_FLOAT32
:
1229 ret
= parse_float(&ctx
->cur
, &values
[i
].Float
);
1231 case TGSI_IMM_UINT32
:
1232 ret
= parse_uint(&ctx
->cur
, &values
[i
].Uint
);
1234 case TGSI_IMM_INT32
:
1235 ret
= parse_int(&ctx
->cur
, &values
[i
].Int
);
1244 report_error( ctx
, "Expected immediate constant" );
1248 eat_opt_white( &ctx
->cur
);
1249 if (*ctx
->cur
!= '}') {
1250 report_error( ctx
, "Expected `}'" );
1258 static boolean
parse_declaration( struct translate_ctx
*ctx
)
1260 struct tgsi_full_declaration decl
;
1262 struct parsed_dcl_bracket brackets
[2];
1265 const char *cur
, *cur2
;
1267 boolean is_vs_input
;
1269 if (!eat_white( &ctx
->cur
)) {
1270 report_error( ctx
, "Syntax error" );
1273 if (!parse_register_dcl( ctx
, &file
, brackets
, &num_brackets
))
1275 if (!parse_opt_writemask( ctx
, &writemask
))
1278 decl
= tgsi_default_full_declaration();
1279 decl
.Declaration
.File
= file
;
1280 decl
.Declaration
.UsageMask
= writemask
;
1282 if (num_brackets
== 1) {
1283 decl
.Range
.First
= brackets
[0].first
;
1284 decl
.Range
.Last
= brackets
[0].last
;
1286 decl
.Range
.First
= brackets
[1].first
;
1287 decl
.Range
.Last
= brackets
[1].last
;
1289 decl
.Declaration
.Dimension
= 1;
1290 decl
.Dim
.Index2D
= brackets
[0].first
;
1293 is_vs_input
= (file
== TGSI_FILE_INPUT
&&
1294 ctx
->processor
== PIPE_SHADER_VERTEX
);
1297 eat_opt_white( &cur
);
1301 eat_opt_white( &cur2
);
1302 if (str_match_nocase_whole( &cur2
, "ARRAY" )) {
1305 report_error( ctx
, "Expected `('" );
1309 eat_opt_white( &cur2
);
1310 if (!parse_int( &cur2
, &arrayid
)) {
1311 report_error( ctx
, "Expected `,'" );
1314 eat_opt_white( &cur2
);
1316 report_error( ctx
, "Expected `)'" );
1320 decl
.Declaration
.Array
= 1;
1321 decl
.Array
.ArrayID
= arrayid
;
1322 ctx
->cur
= cur
= cur2
;
1326 if (*cur
== ',' && !is_vs_input
) {
1330 eat_opt_white( &cur
);
1331 if (file
== TGSI_FILE_IMAGE
) {
1332 for (i
= 0; i
< TGSI_TEXTURE_COUNT
; i
++) {
1333 if (str_match_nocase_whole(&cur
, tgsi_texture_names
[i
])) {
1334 decl
.Image
.Resource
= i
;
1338 if (i
== TGSI_TEXTURE_COUNT
) {
1339 report_error(ctx
, "Expected texture target");
1344 eat_opt_white(&cur2
);
1345 while (*cur2
== ',') {
1347 eat_opt_white(&cur2
);
1348 if (str_match_nocase_whole(&cur2
, "RAW")) {
1351 } else if (str_match_nocase_whole(&cur2
, "WR")) {
1352 decl
.Image
.Writable
= 1;
1355 int format
= str_match_format(&cur2
);
1359 decl
.Image
.Format
= format
;
1362 eat_opt_white(&cur2
);
1367 } else if (file
== TGSI_FILE_SAMPLER_VIEW
) {
1368 for (i
= 0; i
< TGSI_TEXTURE_COUNT
; i
++) {
1369 if (str_match_nocase_whole(&cur
, tgsi_texture_names
[i
])) {
1370 decl
.SamplerView
.Resource
= i
;
1374 if (i
== TGSI_TEXTURE_COUNT
) {
1375 report_error(ctx
, "Expected texture target");
1378 eat_opt_white( &cur
);
1380 report_error( ctx
, "Expected `,'" );
1384 eat_opt_white( &cur
);
1385 for (j
= 0; j
< 4; ++j
) {
1386 for (i
= 0; i
< TGSI_RETURN_TYPE_COUNT
; ++i
) {
1387 if (str_match_nocase_whole(&cur
, tgsi_return_type_names
[i
])) {
1390 decl
.SamplerView
.ReturnTypeX
= i
;
1393 decl
.SamplerView
.ReturnTypeY
= i
;
1396 decl
.SamplerView
.ReturnTypeZ
= i
;
1399 decl
.SamplerView
.ReturnTypeW
= i
;
1407 if (i
== TGSI_RETURN_TYPE_COUNT
) {
1408 if (j
== 0 || j
> 2) {
1409 report_error(ctx
, "Expected type name");
1415 eat_opt_white( &cur2
);
1418 eat_opt_white( &cur2
);
1426 decl
.SamplerView
.ReturnTypeY
=
1427 decl
.SamplerView
.ReturnTypeZ
=
1428 decl
.SamplerView
.ReturnTypeW
=
1429 decl
.SamplerView
.ReturnTypeX
;
1432 } else if (file
== TGSI_FILE_BUFFER
) {
1433 if (str_match_nocase_whole(&cur
, "ATOMIC")) {
1434 decl
.Declaration
.Atomic
= 1;
1437 } else if (file
== TGSI_FILE_MEMORY
) {
1438 if (str_match_nocase_whole(&cur
, "GLOBAL")) {
1439 /* Note this is a no-op global is the default */
1440 decl
.Declaration
.MemType
= TGSI_MEMORY_TYPE_GLOBAL
;
1442 } else if (str_match_nocase_whole(&cur
, "SHARED")) {
1443 decl
.Declaration
.MemType
= TGSI_MEMORY_TYPE_SHARED
;
1445 } else if (str_match_nocase_whole(&cur
, "PRIVATE")) {
1446 decl
.Declaration
.MemType
= TGSI_MEMORY_TYPE_PRIVATE
;
1448 } else if (str_match_nocase_whole(&cur
, "INPUT")) {
1449 decl
.Declaration
.MemType
= TGSI_MEMORY_TYPE_INPUT
;
1453 if (str_match_nocase_whole(&cur
, "LOCAL")) {
1454 decl
.Declaration
.Local
= 1;
1459 eat_opt_white( &cur
);
1462 eat_opt_white( &cur
);
1464 for (i
= 0; i
< TGSI_SEMANTIC_COUNT
; i
++) {
1465 if (str_match_nocase_whole(&cur
, tgsi_semantic_names
[i
])) {
1469 eat_opt_white( &cur2
);
1472 eat_opt_white( &cur2
);
1473 if (!parse_uint( &cur2
, &index
)) {
1474 report_error( ctx
, "Expected literal integer" );
1477 eat_opt_white( &cur2
);
1479 report_error( ctx
, "Expected `]'" );
1484 decl
.Semantic
.Index
= index
;
1489 decl
.Declaration
.Semantic
= 1;
1490 decl
.Semantic
.Name
= i
;
1501 eat_opt_white( &cur
);
1502 if (*cur
== ',' && !is_vs_input
) {
1506 eat_opt_white( &cur
);
1507 for (i
= 0; i
< TGSI_INTERPOLATE_COUNT
; i
++) {
1508 if (str_match_nocase_whole( &cur
, tgsi_interpolate_names
[i
] )) {
1509 decl
.Declaration
.Interpolate
= 1;
1510 decl
.Interp
.Interpolate
= i
;
1516 if (i
== TGSI_INTERPOLATE_COUNT
) {
1517 report_error( ctx
, "Expected semantic or interpolate attribute" );
1523 eat_opt_white( &cur
);
1524 if (*cur
== ',' && !is_vs_input
) {
1528 eat_opt_white( &cur
);
1529 for (i
= 0; i
< TGSI_INTERPOLATE_LOC_COUNT
; i
++) {
1530 if (str_match_nocase_whole( &cur
, tgsi_interpolate_locations
[i
] )) {
1531 decl
.Interp
.Location
= i
;
1539 advance
= tgsi_build_full_declaration(
1543 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1547 ctx
->tokens_cur
+= advance
;
1552 static boolean
parse_immediate( struct translate_ctx
*ctx
)
1554 struct tgsi_full_immediate imm
;
1558 if (*ctx
->cur
== '[') {
1563 eat_opt_white( &ctx
->cur
);
1564 if (!parse_uint( &ctx
->cur
, &uindex
)) {
1565 report_error( ctx
, "Expected literal unsigned integer" );
1569 if (uindex
!= ctx
->num_immediates
) {
1570 report_error( ctx
, "Immediates must be sorted" );
1574 eat_opt_white( &ctx
->cur
);
1575 if (*ctx
->cur
!= ']') {
1576 report_error( ctx
, "Expected `]'" );
1583 if (!eat_white( &ctx
->cur
)) {
1584 report_error( ctx
, "Syntax error" );
1587 for (type
= 0; type
< ARRAY_SIZE(tgsi_immediate_type_names
); ++type
) {
1588 if (str_match_nocase_whole(&ctx
->cur
, tgsi_immediate_type_names
[type
]))
1591 if (type
== ARRAY_SIZE(tgsi_immediate_type_names
)) {
1592 report_error( ctx
, "Expected immediate type" );
1596 imm
= tgsi_default_full_immediate();
1597 imm
.Immediate
.NrTokens
+= 4;
1598 imm
.Immediate
.DataType
= type
;
1599 parse_immediate_data(ctx
, type
, imm
.u
);
1601 advance
= tgsi_build_full_immediate(
1605 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1608 ctx
->tokens_cur
+= advance
;
1610 ctx
->num_immediates
++;
1616 parse_primitive( const char **pcur
, uint
*primitive
)
1620 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++) {
1621 const char *cur
= *pcur
;
1623 if (str_match_nocase_whole( &cur
, tgsi_primitive_names
[i
])) {
1633 parse_fs_coord_origin( const char **pcur
, uint
*fs_coord_origin
)
1637 for (i
= 0; i
< ARRAY_SIZE(tgsi_fs_coord_origin_names
); i
++) {
1638 const char *cur
= *pcur
;
1640 if (str_match_nocase_whole( &cur
, tgsi_fs_coord_origin_names
[i
])) {
1641 *fs_coord_origin
= i
;
1650 parse_fs_coord_pixel_center( const char **pcur
, uint
*fs_coord_pixel_center
)
1654 for (i
= 0; i
< ARRAY_SIZE(tgsi_fs_coord_pixel_center_names
); i
++) {
1655 const char *cur
= *pcur
;
1657 if (str_match_nocase_whole( &cur
, tgsi_fs_coord_pixel_center_names
[i
])) {
1658 *fs_coord_pixel_center
= i
;
1667 static boolean
parse_property( struct translate_ctx
*ctx
)
1669 struct tgsi_full_property prop
;
1675 if (!eat_white( &ctx
->cur
)) {
1676 report_error( ctx
, "Syntax error" );
1679 if (!parse_identifier( &ctx
->cur
, id
)) {
1680 report_error( ctx
, "Syntax error" );
1683 for (property_name
= 0; property_name
< TGSI_PROPERTY_COUNT
;
1685 if (streq_nocase_uprcase(tgsi_property_names
[property_name
], id
)) {
1689 if (property_name
>= TGSI_PROPERTY_COUNT
) {
1690 debug_printf( "\nError: Unknown property : '%s'", id
);
1694 eat_opt_white( &ctx
->cur
);
1695 switch(property_name
) {
1696 case TGSI_PROPERTY_GS_INPUT_PRIM
:
1697 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
1698 if (!parse_primitive(&ctx
->cur
, &values
[0] )) {
1699 report_error( ctx
, "Unknown primitive name as property!" );
1702 if (property_name
== TGSI_PROPERTY_GS_INPUT_PRIM
&&
1703 ctx
->processor
== PIPE_SHADER_GEOMETRY
) {
1704 ctx
->implied_array_size
= u_vertices_per_prim(values
[0]);
1707 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
1708 if (!parse_fs_coord_origin(&ctx
->cur
, &values
[0] )) {
1709 report_error( ctx
, "Unknown coord origin as property: must be UPPER_LEFT or LOWER_LEFT!" );
1713 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
1714 if (!parse_fs_coord_pixel_center(&ctx
->cur
, &values
[0] )) {
1715 report_error( ctx
, "Unknown coord pixel center as property: must be HALF_INTEGER or INTEGER!" );
1719 case TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
:
1721 if (!parse_uint(&ctx
->cur
, &values
[0] )) {
1722 report_error( ctx
, "Expected unsigned integer as property!" );
1727 prop
= tgsi_default_full_property();
1728 prop
.Property
.PropertyName
= property_name
;
1729 prop
.Property
.NrTokens
+= 1;
1730 prop
.u
[0].Data
= values
[0];
1732 advance
= tgsi_build_full_property(
1736 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1739 ctx
->tokens_cur
+= advance
;
1745 static boolean
translate( struct translate_ctx
*ctx
)
1747 eat_opt_white( &ctx
->cur
);
1748 if (!parse_header( ctx
))
1751 if (ctx
->processor
== PIPE_SHADER_TESS_CTRL
||
1752 ctx
->processor
== PIPE_SHADER_TESS_EVAL
)
1753 ctx
->implied_array_size
= 32;
1755 while (*ctx
->cur
!= '\0') {
1757 if (!eat_white( &ctx
->cur
)) {
1758 report_error( ctx
, "Syntax error" );
1762 if (*ctx
->cur
== '\0')
1764 if (parse_label( ctx
, &label_val
)) {
1765 if (!parse_instruction( ctx
, TRUE
))
1768 else if (str_match_nocase_whole( &ctx
->cur
, "DCL" )) {
1769 if (!parse_declaration( ctx
))
1772 else if (str_match_nocase_whole( &ctx
->cur
, "IMM" )) {
1773 if (!parse_immediate( ctx
))
1776 else if (str_match_nocase_whole( &ctx
->cur
, "PROPERTY" )) {
1777 if (!parse_property( ctx
))
1780 else if (!parse_instruction( ctx
, FALSE
)) {
1789 tgsi_text_translate(
1791 struct tgsi_token
*tokens
,
1794 struct translate_ctx ctx
= {0};
1798 ctx
.tokens
= tokens
;
1799 ctx
.tokens_cur
= tokens
;
1800 ctx
.tokens_end
= tokens
+ num_tokens
;
1802 if (!translate( &ctx
))
1805 return tgsi_sanity_check( tokens
);