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
))
239 struct tgsi_token
*tokens
;
240 struct tgsi_token
*tokens_cur
;
241 struct tgsi_token
*tokens_end
;
242 struct tgsi_header
*header
;
243 unsigned processor
: 4;
244 int implied_array_size
: 5;
245 unsigned num_immediates
;
248 static void report_error( struct translate_ctx
*ctx
, const char *msg
)
252 const char *itr
= ctx
->text
;
254 while (itr
!= ctx
->cur
) {
263 debug_printf( "\nTGSI asm error: %s [%d : %d] \n", msg
, line
, column
);
266 /* Parse shader header.
267 * Return TRUE for one of the following headers.
272 static boolean
parse_header( struct translate_ctx
*ctx
)
276 if (str_match_nocase_whole( &ctx
->cur
, "FRAG" ))
277 processor
= TGSI_PROCESSOR_FRAGMENT
;
278 else if (str_match_nocase_whole( &ctx
->cur
, "VERT" ))
279 processor
= TGSI_PROCESSOR_VERTEX
;
280 else if (str_match_nocase_whole( &ctx
->cur
, "GEOM" ))
281 processor
= TGSI_PROCESSOR_GEOMETRY
;
282 else if (str_match_nocase_whole( &ctx
->cur
, "COMP" ))
283 processor
= TGSI_PROCESSOR_COMPUTE
;
285 report_error( ctx
, "Unknown header" );
289 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
291 ctx
->header
= (struct tgsi_header
*) ctx
->tokens_cur
++;
292 *ctx
->header
= tgsi_build_header();
294 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
296 *(struct tgsi_processor
*) ctx
->tokens_cur
++ = tgsi_build_processor( processor
, ctx
->header
);
297 ctx
->processor
= processor
;
302 static boolean
parse_label( struct translate_ctx
*ctx
, uint
*val
)
304 const char *cur
= ctx
->cur
;
306 if (parse_uint( &cur
, val
)) {
307 eat_opt_white( &cur
);
318 parse_file( const char **pcur
, uint
*file
)
322 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++) {
323 const char *cur
= *pcur
;
325 if (str_match_nocase_whole( &cur
, tgsi_file_name(i
) )) {
336 struct translate_ctx
*ctx
,
342 eat_opt_white( &cur
);
345 *writemask
= TGSI_WRITEMASK_NONE
;
346 eat_opt_white( &cur
);
347 if (uprcase( *cur
) == 'X') {
349 *writemask
|= TGSI_WRITEMASK_X
;
351 if (uprcase( *cur
) == 'Y') {
353 *writemask
|= TGSI_WRITEMASK_Y
;
355 if (uprcase( *cur
) == 'Z') {
357 *writemask
|= TGSI_WRITEMASK_Z
;
359 if (uprcase( *cur
) == 'W') {
361 *writemask
|= TGSI_WRITEMASK_W
;
364 if (*writemask
== TGSI_WRITEMASK_NONE
) {
365 report_error( ctx
, "Writemask expected" );
372 *writemask
= TGSI_WRITEMASK_XYZW
;
378 /* <register_file_bracket> ::= <file> `['
381 parse_register_file_bracket(
382 struct translate_ctx
*ctx
,
385 if (!parse_file( &ctx
->cur
, file
)) {
386 report_error( ctx
, "Unknown register file" );
389 eat_opt_white( &ctx
->cur
);
390 if (*ctx
->cur
!= '[') {
391 report_error( ctx
, "Expected `['" );
398 /* <register_file_bracket_index> ::= <register_file_bracket> <uint>
401 parse_register_file_bracket_index(
402 struct translate_ctx
*ctx
,
408 if (!parse_register_file_bracket( ctx
, file
))
410 eat_opt_white( &ctx
->cur
);
411 if (!parse_uint( &ctx
->cur
, &uindex
)) {
412 report_error( ctx
, "Expected literal unsigned integer" );
415 *index
= (int) uindex
;
419 /* Parse simple 1d register operand.
420 * <register_dst> ::= <register_file_bracket_index> `]'
423 parse_register_1d(struct translate_ctx
*ctx
,
427 if (!parse_register_file_bracket_index( ctx
, file
, index
))
429 eat_opt_white( &ctx
->cur
);
430 if (*ctx
->cur
!= ']') {
431 report_error( ctx
, "Expected `]'" );
438 struct parsed_bracket
{
449 parse_register_bracket(
450 struct translate_ctx
*ctx
,
451 struct parsed_bracket
*brackets
)
456 memset(brackets
, 0, sizeof(struct parsed_bracket
));
458 eat_opt_white( &ctx
->cur
);
461 if (parse_file( &cur
, &brackets
->ind_file
)) {
462 if (!parse_register_1d( ctx
, &brackets
->ind_file
,
463 &brackets
->ind_index
))
465 eat_opt_white( &ctx
->cur
);
467 if (*ctx
->cur
== '.') {
469 eat_opt_white(&ctx
->cur
);
471 switch (uprcase(*ctx
->cur
)) {
473 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
476 brackets
->ind_comp
= TGSI_SWIZZLE_Y
;
479 brackets
->ind_comp
= TGSI_SWIZZLE_Z
;
482 brackets
->ind_comp
= TGSI_SWIZZLE_W
;
485 report_error(ctx
, "Expected indirect register swizzle component `x', `y', `z' or `w'");
489 eat_opt_white(&ctx
->cur
);
492 if (*ctx
->cur
== '+' || *ctx
->cur
== '-')
493 parse_int( &ctx
->cur
, &brackets
->index
);
498 if (!parse_uint( &ctx
->cur
, &uindex
)) {
499 report_error( ctx
, "Expected literal unsigned integer" );
502 brackets
->index
= (int) uindex
;
503 brackets
->ind_file
= TGSI_FILE_NULL
;
504 brackets
->ind_index
= 0;
506 eat_opt_white( &ctx
->cur
);
507 if (*ctx
->cur
!= ']') {
508 report_error( ctx
, "Expected `]'" );
512 if (*ctx
->cur
== '(') {
514 eat_opt_white( &ctx
->cur
);
515 if (!parse_uint( &ctx
->cur
, &brackets
->ind_array
)) {
516 report_error( ctx
, "Expected literal unsigned integer" );
519 eat_opt_white( &ctx
->cur
);
520 if (*ctx
->cur
!= ')') {
521 report_error( ctx
, "Expected `)'" );
530 parse_opt_register_src_bracket(
531 struct translate_ctx
*ctx
,
532 struct parsed_bracket
*brackets
,
533 int *parsed_brackets
)
535 const char *cur
= ctx
->cur
;
537 *parsed_brackets
= 0;
539 eat_opt_white( &cur
);
544 if (!parse_register_bracket(ctx
, brackets
))
547 *parsed_brackets
= 1;
554 /* Parse source register operand.
555 * <register_src> ::= <register_file_bracket_index> `]' |
556 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `]' |
557 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `+' <uint> `]' |
558 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `-' <uint> `]'
562 struct translate_ctx
*ctx
,
564 struct parsed_bracket
*brackets
)
566 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
567 if (!parse_register_file_bracket( ctx
, file
))
569 if (!parse_register_bracket( ctx
, brackets
))
575 struct parsed_dcl_bracket
{
581 parse_register_dcl_bracket(
582 struct translate_ctx
*ctx
,
583 struct parsed_dcl_bracket
*bracket
)
586 memset(bracket
, 0, sizeof(struct parsed_dcl_bracket
));
588 eat_opt_white( &ctx
->cur
);
590 if (!parse_uint( &ctx
->cur
, &uindex
)) {
591 /* it can be an empty bracket [] which means its range
592 * is from 0 to some implied size */
593 if (ctx
->cur
[0] == ']' && ctx
->implied_array_size
!= 0) {
595 bracket
->last
= ctx
->implied_array_size
- 1;
598 report_error( ctx
, "Expected literal unsigned integer" );
601 bracket
->first
= uindex
;
603 eat_opt_white( &ctx
->cur
);
605 if (ctx
->cur
[0] == '.' && ctx
->cur
[1] == '.') {
609 eat_opt_white( &ctx
->cur
);
610 if (!parse_uint( &ctx
->cur
, &uindex
)) {
611 report_error( ctx
, "Expected literal integer" );
614 bracket
->last
= (int) uindex
;
615 eat_opt_white( &ctx
->cur
);
618 bracket
->last
= bracket
->first
;
622 if (*ctx
->cur
!= ']') {
623 report_error( ctx
, "Expected `]' or `..'" );
630 /* Parse register declaration.
631 * <register_dcl> ::= <register_file_bracket_index> `]' |
632 * <register_file_bracket_index> `..' <index> `]'
636 struct translate_ctx
*ctx
,
638 struct parsed_dcl_bracket
*brackets
,
645 if (!parse_register_file_bracket( ctx
, file
))
647 if (!parse_register_dcl_bracket( ctx
, &brackets
[0] ))
653 eat_opt_white( &cur
);
658 if (!parse_register_dcl_bracket( ctx
, &brackets
[1] ))
660 /* for geometry shader we don't really care about
661 * the first brackets it's always the size of the
662 * input primitive. so we want to declare just
663 * the index relevant to the semantics which is in
664 * the second bracket */
665 if (ctx
->processor
== TGSI_PROCESSOR_GEOMETRY
&& *file
== TGSI_FILE_INPUT
) {
666 brackets
[0] = brackets
[1];
677 /* Parse destination register operand.*/
680 struct translate_ctx
*ctx
,
682 struct parsed_bracket
*brackets
)
684 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
685 if (!parse_register_file_bracket( ctx
, file
))
687 if (!parse_register_bracket( ctx
, brackets
))
695 struct translate_ctx
*ctx
,
696 struct tgsi_full_dst_register
*dst
)
701 struct parsed_bracket bracket
[2];
702 int parsed_opt_brackets
;
704 if (!parse_register_dst( ctx
, &file
, &bracket
[0] ))
706 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
710 eat_opt_white( &cur
);
712 if (!parse_opt_writemask( ctx
, &writemask
))
715 dst
->Register
.File
= file
;
716 if (parsed_opt_brackets
) {
717 dst
->Register
.Dimension
= 1;
718 dst
->Dimension
.Indirect
= 0;
719 dst
->Dimension
.Dimension
= 0;
720 dst
->Dimension
.Index
= bracket
[0].index
;
721 bracket
[0] = bracket
[1];
723 dst
->Register
.Index
= bracket
[0].index
;
724 dst
->Register
.WriteMask
= writemask
;
725 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
726 dst
->Register
.Indirect
= 1;
727 dst
->Indirect
.File
= bracket
[0].ind_file
;
728 dst
->Indirect
.Index
= bracket
[0].ind_index
;
729 dst
->Indirect
.Swizzle
= bracket
[0].ind_comp
;
730 dst
->Indirect
.ArrayID
= bracket
[0].ind_array
;
736 parse_optional_swizzle(
737 struct translate_ctx
*ctx
,
739 boolean
*parsed_swizzle
,
742 const char *cur
= ctx
->cur
;
744 *parsed_swizzle
= FALSE
;
746 eat_opt_white( &cur
);
751 eat_opt_white( &cur
);
752 for (i
= 0; i
< components
; i
++) {
753 if (uprcase( *cur
) == 'X')
754 swizzle
[i
] = TGSI_SWIZZLE_X
;
755 else if (uprcase( *cur
) == 'Y')
756 swizzle
[i
] = TGSI_SWIZZLE_Y
;
757 else if (uprcase( *cur
) == 'Z')
758 swizzle
[i
] = TGSI_SWIZZLE_Z
;
759 else if (uprcase( *cur
) == 'W')
760 swizzle
[i
] = TGSI_SWIZZLE_W
;
762 report_error( ctx
, "Expected register swizzle component `x', `y', `z' or `w'" );
767 *parsed_swizzle
= TRUE
;
775 struct translate_ctx
*ctx
,
776 struct tgsi_full_src_register
*src
)
780 boolean parsed_swizzle
;
781 struct parsed_bracket bracket
[2];
782 int parsed_opt_brackets
;
784 if (*ctx
->cur
== '-') {
786 eat_opt_white( &ctx
->cur
);
787 src
->Register
.Negate
= 1;
790 if (*ctx
->cur
== '|') {
792 eat_opt_white( &ctx
->cur
);
793 src
->Register
.Absolute
= 1;
796 if (!parse_register_src(ctx
, &file
, &bracket
[0]))
798 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
801 src
->Register
.File
= file
;
802 if (parsed_opt_brackets
) {
803 src
->Register
.Dimension
= 1;
804 src
->Dimension
.Indirect
= 0;
805 src
->Dimension
.Dimension
= 0;
806 src
->Dimension
.Index
= bracket
[0].index
;
807 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
808 src
->Dimension
.Indirect
= 1;
809 src
->DimIndirect
.File
= bracket
[0].ind_file
;
810 src
->DimIndirect
.Index
= bracket
[0].ind_index
;
811 src
->DimIndirect
.Swizzle
= bracket
[0].ind_comp
;
812 src
->DimIndirect
.ArrayID
= bracket
[0].ind_array
;
814 bracket
[0] = bracket
[1];
816 src
->Register
.Index
= bracket
[0].index
;
817 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
818 src
->Register
.Indirect
= 1;
819 src
->Indirect
.File
= bracket
[0].ind_file
;
820 src
->Indirect
.Index
= bracket
[0].ind_index
;
821 src
->Indirect
.Swizzle
= bracket
[0].ind_comp
;
822 src
->Indirect
.ArrayID
= bracket
[0].ind_array
;
825 /* Parse optional swizzle.
827 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
, 4 )) {
828 if (parsed_swizzle
) {
829 src
->Register
.SwizzleX
= swizzle
[0];
830 src
->Register
.SwizzleY
= swizzle
[1];
831 src
->Register
.SwizzleZ
= swizzle
[2];
832 src
->Register
.SwizzleW
= swizzle
[3];
836 if (src
->Register
.Absolute
) {
837 eat_opt_white( &ctx
->cur
);
838 if (*ctx
->cur
!= '|') {
839 report_error( ctx
, "Expected `|'" );
850 parse_texoffset_operand(
851 struct translate_ctx
*ctx
,
852 struct tgsi_texture_offset
*src
)
856 boolean parsed_swizzle
;
857 struct parsed_bracket bracket
;
859 if (!parse_register_src(ctx
, &file
, &bracket
))
863 src
->Index
= bracket
.index
;
865 /* Parse optional swizzle.
867 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
, 3 )) {
868 if (parsed_swizzle
) {
869 src
->SwizzleX
= swizzle
[0];
870 src
->SwizzleY
= swizzle
[1];
871 src
->SwizzleZ
= swizzle
[2];
879 match_inst(const char **pcur
,
881 const struct tgsi_opcode_info
*info
)
883 const char *cur
= *pcur
;
885 /* simple case: the whole string matches the instruction name */
886 if (str_match_nocase_whole(&cur
, info
->mnemonic
)) {
888 *saturate
= TGSI_SAT_NONE
;
892 if (str_match_no_case(&cur
, info
->mnemonic
)) {
893 /* the instruction has a suffix, figure it out */
894 if (str_match_nocase_whole(&cur
, "_SAT")) {
896 *saturate
= TGSI_SAT_ZERO_ONE
;
900 if (str_match_nocase_whole(&cur
, "_SATNV")) {
902 *saturate
= TGSI_SAT_MINUS_PLUS_ONE
;
912 struct translate_ctx
*ctx
,
916 uint saturate
= TGSI_SAT_NONE
;
917 const struct tgsi_opcode_info
*info
;
918 struct tgsi_full_instruction inst
;
922 inst
= tgsi_default_full_instruction();
926 eat_opt_white( &ctx
->cur
);
927 if (*ctx
->cur
== '(') {
931 boolean parsed_swizzle
;
933 inst
.Instruction
.Predicate
= 1;
936 if (*ctx
->cur
== '!') {
938 inst
.Predicate
.Negate
= 1;
941 if (!parse_register_1d( ctx
, &file
, &index
))
944 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
, 4 )) {
945 if (parsed_swizzle
) {
946 inst
.Predicate
.SwizzleX
= swizzle
[0];
947 inst
.Predicate
.SwizzleY
= swizzle
[1];
948 inst
.Predicate
.SwizzleZ
= swizzle
[2];
949 inst
.Predicate
.SwizzleW
= swizzle
[3];
953 if (*ctx
->cur
!= ')') {
954 report_error( ctx
, "Expected `)'" );
961 /* Parse instruction name.
963 eat_opt_white( &ctx
->cur
);
964 for (i
= 0; i
< TGSI_OPCODE_LAST
; i
++) {
967 info
= tgsi_get_opcode_info( i
);
968 if (match_inst(&cur
, &saturate
, info
)) {
969 if (info
->num_dst
+ info
->num_src
+ info
->is_tex
== 0) {
973 else if (*cur
== '\0' || eat_white( &cur
)) {
979 if (i
== TGSI_OPCODE_LAST
) {
981 report_error( ctx
, "Unknown opcode" );
983 report_error( ctx
, "Expected `DCL', `IMM' or a label" );
987 inst
.Instruction
.Opcode
= i
;
988 inst
.Instruction
.Saturate
= saturate
;
989 inst
.Instruction
.NumDstRegs
= info
->num_dst
;
990 inst
.Instruction
.NumSrcRegs
= info
->num_src
;
992 if (i
>= TGSI_OPCODE_SAMPLE
&& i
<= TGSI_OPCODE_GATHER4
) {
994 * These are not considered tex opcodes here (no additional
995 * target argument) however we're required to set the Texture
996 * bit so we can set the number of tex offsets (offsets aren't
997 * actually handled here yet in any case).
999 inst
.Instruction
.Texture
= 1;
1000 inst
.Texture
.Texture
= TGSI_TEXTURE_UNKNOWN
;
1003 /* Parse instruction operands.
1005 for (i
= 0; i
< info
->num_dst
+ info
->num_src
+ info
->is_tex
; i
++) {
1007 eat_opt_white( &ctx
->cur
);
1008 if (*ctx
->cur
!= ',') {
1009 report_error( ctx
, "Expected `,'" );
1013 eat_opt_white( &ctx
->cur
);
1016 if (i
< info
->num_dst
) {
1017 if (!parse_dst_operand( ctx
, &inst
.Dst
[i
] ))
1020 else if (i
< info
->num_dst
+ info
->num_src
) {
1021 if (!parse_src_operand( ctx
, &inst
.Src
[i
- info
->num_dst
] ))
1027 for (j
= 0; j
< TGSI_TEXTURE_COUNT
; j
++) {
1028 if (str_match_nocase_whole( &ctx
->cur
, tgsi_texture_names
[j
] )) {
1029 inst
.Instruction
.Texture
= 1;
1030 inst
.Texture
.Texture
= j
;
1034 if (j
== TGSI_TEXTURE_COUNT
) {
1035 report_error( ctx
, "Expected texture target" );
1042 eat_opt_white( &cur
);
1043 for (i
= 0; info
->is_tex
&& *cur
== ','; i
++) {
1045 eat_opt_white( &cur
);
1047 if (!parse_texoffset_operand( ctx
, &inst
.TexOffsets
[i
] ))
1050 eat_opt_white( &cur
);
1052 inst
.Texture
.NumOffsets
= i
;
1055 eat_opt_white( &cur
);
1056 if (info
->is_branch
&& *cur
== ':') {
1060 eat_opt_white( &cur
);
1061 if (!parse_uint( &cur
, &target
)) {
1062 report_error( ctx
, "Expected a label" );
1065 inst
.Instruction
.Label
= 1;
1066 inst
.Label
.Label
= target
;
1070 advance
= tgsi_build_full_instruction(
1074 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1077 ctx
->tokens_cur
+= advance
;
1082 /* parses a 4-touple of the form {x, y, z, w}
1083 * where x, y, z, w are numbers */
1084 static boolean
parse_immediate_data(struct translate_ctx
*ctx
, unsigned type
,
1085 union tgsi_immediate_data
*values
)
1090 eat_opt_white( &ctx
->cur
);
1091 if (*ctx
->cur
!= '{') {
1092 report_error( ctx
, "Expected `{'" );
1096 for (i
= 0; i
< 4; i
++) {
1097 eat_opt_white( &ctx
->cur
);
1099 if (*ctx
->cur
!= ',') {
1100 report_error( ctx
, "Expected `,'" );
1104 eat_opt_white( &ctx
->cur
);
1108 case TGSI_IMM_FLOAT32
:
1109 ret
= parse_float(&ctx
->cur
, &values
[i
].Float
);
1111 case TGSI_IMM_UINT32
:
1112 ret
= parse_uint(&ctx
->cur
, &values
[i
].Uint
);
1114 case TGSI_IMM_INT32
:
1115 ret
= parse_int(&ctx
->cur
, &values
[i
].Int
);
1124 report_error( ctx
, "Expected immediate constant" );
1128 eat_opt_white( &ctx
->cur
);
1129 if (*ctx
->cur
!= '}') {
1130 report_error( ctx
, "Expected `}'" );
1138 static boolean
parse_declaration( struct translate_ctx
*ctx
)
1140 struct tgsi_full_declaration decl
;
1142 struct parsed_dcl_bracket brackets
[2];
1145 const char *cur
, *cur2
;
1147 boolean is_vs_input
;
1149 if (!eat_white( &ctx
->cur
)) {
1150 report_error( ctx
, "Syntax error" );
1153 if (!parse_register_dcl( ctx
, &file
, brackets
, &num_brackets
))
1155 if (!parse_opt_writemask( ctx
, &writemask
))
1158 decl
= tgsi_default_full_declaration();
1159 decl
.Declaration
.File
= file
;
1160 decl
.Declaration
.UsageMask
= writemask
;
1162 if (num_brackets
== 1) {
1163 decl
.Range
.First
= brackets
[0].first
;
1164 decl
.Range
.Last
= brackets
[0].last
;
1166 decl
.Range
.First
= brackets
[1].first
;
1167 decl
.Range
.Last
= brackets
[1].last
;
1169 decl
.Declaration
.Dimension
= 1;
1170 decl
.Dim
.Index2D
= brackets
[0].first
;
1173 is_vs_input
= (file
== TGSI_FILE_INPUT
&&
1174 ctx
->processor
== TGSI_PROCESSOR_VERTEX
);
1177 eat_opt_white( &cur
);
1181 eat_opt_white( &cur2
);
1182 if (str_match_nocase_whole( &cur2
, "ARRAY" )) {
1185 report_error( ctx
, "Expected `('" );
1189 eat_opt_white( &cur2
);
1190 if (!parse_int( &cur2
, &arrayid
)) {
1191 report_error( ctx
, "Expected `,'" );
1194 eat_opt_white( &cur2
);
1196 report_error( ctx
, "Expected `)'" );
1200 decl
.Declaration
.Array
= 1;
1201 decl
.Array
.ArrayID
= arrayid
;
1202 ctx
->cur
= cur
= cur2
;
1206 if (*cur
== ',' && !is_vs_input
) {
1210 eat_opt_white( &cur
);
1211 if (file
== TGSI_FILE_RESOURCE
) {
1212 for (i
= 0; i
< TGSI_TEXTURE_COUNT
; i
++) {
1213 if (str_match_nocase_whole(&cur
, tgsi_texture_names
[i
])) {
1214 decl
.Resource
.Resource
= i
;
1218 if (i
== TGSI_TEXTURE_COUNT
) {
1219 report_error(ctx
, "Expected texture target");
1224 eat_opt_white(&cur2
);
1225 while (*cur2
== ',') {
1227 eat_opt_white(&cur2
);
1228 if (str_match_nocase_whole(&cur2
, "RAW")) {
1229 decl
.Resource
.Raw
= 1;
1231 } else if (str_match_nocase_whole(&cur2
, "WR")) {
1232 decl
.Resource
.Writable
= 1;
1238 eat_opt_white(&cur2
);
1243 } else if (file
== TGSI_FILE_SAMPLER_VIEW
) {
1244 for (i
= 0; i
< TGSI_TEXTURE_COUNT
; i
++) {
1245 if (str_match_nocase_whole(&cur
, tgsi_texture_names
[i
])) {
1246 decl
.SamplerView
.Resource
= i
;
1250 if (i
== TGSI_TEXTURE_COUNT
) {
1251 report_error(ctx
, "Expected texture target");
1254 eat_opt_white( &cur
);
1256 report_error( ctx
, "Expected `,'" );
1260 eat_opt_white( &cur
);
1261 for (j
= 0; j
< 4; ++j
) {
1262 for (i
= 0; i
< PIPE_TYPE_COUNT
; ++i
) {
1263 if (str_match_nocase_whole(&cur
, tgsi_type_names
[i
])) {
1266 decl
.SamplerView
.ReturnTypeX
= i
;
1269 decl
.SamplerView
.ReturnTypeY
= i
;
1272 decl
.SamplerView
.ReturnTypeZ
= i
;
1275 decl
.SamplerView
.ReturnTypeW
= i
;
1283 if (i
== PIPE_TYPE_COUNT
) {
1284 if (j
== 0 || j
> 2) {
1285 report_error(ctx
, "Expected type name");
1291 eat_opt_white( &cur2
);
1294 eat_opt_white( &cur2
);
1302 decl
.SamplerView
.ReturnTypeY
=
1303 decl
.SamplerView
.ReturnTypeZ
=
1304 decl
.SamplerView
.ReturnTypeW
=
1305 decl
.SamplerView
.ReturnTypeX
;
1309 if (str_match_nocase_whole(&cur
, "LOCAL")) {
1310 decl
.Declaration
.Local
= 1;
1315 eat_opt_white( &cur
);
1318 eat_opt_white( &cur
);
1320 for (i
= 0; i
< TGSI_SEMANTIC_COUNT
; i
++) {
1321 if (str_match_nocase_whole(&cur
, tgsi_semantic_names
[i
])) {
1325 eat_opt_white( &cur2
);
1328 eat_opt_white( &cur2
);
1329 if (!parse_uint( &cur2
, &index
)) {
1330 report_error( ctx
, "Expected literal integer" );
1333 eat_opt_white( &cur2
);
1335 report_error( ctx
, "Expected `]'" );
1340 decl
.Semantic
.Index
= index
;
1345 decl
.Declaration
.Semantic
= 1;
1346 decl
.Semantic
.Name
= i
;
1357 eat_opt_white( &cur
);
1358 if (*cur
== ',' && !is_vs_input
) {
1362 eat_opt_white( &cur
);
1363 for (i
= 0; i
< TGSI_INTERPOLATE_COUNT
; i
++) {
1364 if (str_match_nocase_whole( &cur
, tgsi_interpolate_names
[i
] )) {
1365 decl
.Declaration
.Interpolate
= 1;
1366 decl
.Interp
.Interpolate
= i
;
1372 if (i
== TGSI_INTERPOLATE_COUNT
) {
1373 report_error( ctx
, "Expected semantic or interpolate attribute" );
1378 advance
= tgsi_build_full_declaration(
1382 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1386 ctx
->tokens_cur
+= advance
;
1391 static boolean
parse_immediate( struct translate_ctx
*ctx
)
1393 struct tgsi_full_immediate imm
;
1397 if (*ctx
->cur
== '[') {
1402 eat_opt_white( &ctx
->cur
);
1403 if (!parse_uint( &ctx
->cur
, &uindex
)) {
1404 report_error( ctx
, "Expected literal unsigned integer" );
1408 if (uindex
!= ctx
->num_immediates
) {
1409 report_error( ctx
, "Immediates must be sorted" );
1413 eat_opt_white( &ctx
->cur
);
1414 if (*ctx
->cur
!= ']') {
1415 report_error( ctx
, "Expected `]'" );
1422 if (!eat_white( &ctx
->cur
)) {
1423 report_error( ctx
, "Syntax error" );
1426 for (type
= 0; type
< Elements(tgsi_immediate_type_names
); ++type
) {
1427 if (str_match_nocase_whole(&ctx
->cur
, tgsi_immediate_type_names
[type
]))
1430 if (type
== Elements(tgsi_immediate_type_names
)) {
1431 report_error( ctx
, "Expected immediate type" );
1435 imm
= tgsi_default_full_immediate();
1436 imm
.Immediate
.NrTokens
+= 4;
1437 imm
.Immediate
.DataType
= type
;
1438 parse_immediate_data(ctx
, type
, imm
.u
);
1440 advance
= tgsi_build_full_immediate(
1444 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1447 ctx
->tokens_cur
+= advance
;
1449 ctx
->num_immediates
++;
1455 parse_primitive( const char **pcur
, uint
*primitive
)
1459 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++) {
1460 const char *cur
= *pcur
;
1462 if (str_match_nocase_whole( &cur
, tgsi_primitive_names
[i
])) {
1472 parse_fs_coord_origin( const char **pcur
, uint
*fs_coord_origin
)
1476 for (i
= 0; i
< Elements(tgsi_fs_coord_origin_names
); i
++) {
1477 const char *cur
= *pcur
;
1479 if (str_match_nocase_whole( &cur
, tgsi_fs_coord_origin_names
[i
])) {
1480 *fs_coord_origin
= i
;
1489 parse_fs_coord_pixel_center( const char **pcur
, uint
*fs_coord_pixel_center
)
1493 for (i
= 0; i
< Elements(tgsi_fs_coord_pixel_center_names
); i
++) {
1494 const char *cur
= *pcur
;
1496 if (str_match_nocase_whole( &cur
, tgsi_fs_coord_pixel_center_names
[i
])) {
1497 *fs_coord_pixel_center
= i
;
1506 static boolean
parse_property( struct translate_ctx
*ctx
)
1508 struct tgsi_full_property prop
;
1514 if (!eat_white( &ctx
->cur
)) {
1515 report_error( ctx
, "Syntax error" );
1518 if (!parse_identifier( &ctx
->cur
, id
)) {
1519 report_error( ctx
, "Syntax error" );
1522 for (property_name
= 0; property_name
< TGSI_PROPERTY_COUNT
;
1524 if (streq_nocase_uprcase(tgsi_property_names
[property_name
], id
)) {
1528 if (property_name
>= TGSI_PROPERTY_COUNT
) {
1529 debug_printf( "\nError: Unknown property : '%s'", id
);
1533 eat_opt_white( &ctx
->cur
);
1534 switch(property_name
) {
1535 case TGSI_PROPERTY_GS_INPUT_PRIM
:
1536 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
1537 if (!parse_primitive(&ctx
->cur
, &values
[0] )) {
1538 report_error( ctx
, "Unknown primitive name as property!" );
1541 if (property_name
== TGSI_PROPERTY_GS_INPUT_PRIM
&&
1542 ctx
->processor
== TGSI_PROCESSOR_GEOMETRY
) {
1543 ctx
->implied_array_size
= u_vertices_per_prim(values
[0]);
1546 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
1547 if (!parse_fs_coord_origin(&ctx
->cur
, &values
[0] )) {
1548 report_error( ctx
, "Unknown coord origin as property: must be UPPER_LEFT or LOWER_LEFT!" );
1552 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
1553 if (!parse_fs_coord_pixel_center(&ctx
->cur
, &values
[0] )) {
1554 report_error( ctx
, "Unknown coord pixel center as property: must be HALF_INTEGER or INTEGER!" );
1558 case TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
:
1560 if (!parse_uint(&ctx
->cur
, &values
[0] )) {
1561 report_error( ctx
, "Expected unsigned integer as property!" );
1566 prop
= tgsi_default_full_property();
1567 prop
.Property
.PropertyName
= property_name
;
1568 prop
.Property
.NrTokens
+= 1;
1569 prop
.u
[0].Data
= values
[0];
1571 advance
= tgsi_build_full_property(
1575 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1578 ctx
->tokens_cur
+= advance
;
1584 static boolean
translate( struct translate_ctx
*ctx
)
1586 eat_opt_white( &ctx
->cur
);
1587 if (!parse_header( ctx
))
1590 while (*ctx
->cur
!= '\0') {
1592 if (!eat_white( &ctx
->cur
)) {
1593 report_error( ctx
, "Syntax error" );
1597 if (*ctx
->cur
== '\0')
1599 if (parse_label( ctx
, &label_val
)) {
1600 if (!parse_instruction( ctx
, TRUE
))
1603 else if (str_match_nocase_whole( &ctx
->cur
, "DCL" )) {
1604 if (!parse_declaration( ctx
))
1607 else if (str_match_nocase_whole( &ctx
->cur
, "IMM" )) {
1608 if (!parse_immediate( ctx
))
1611 else if (str_match_nocase_whole( &ctx
->cur
, "PROPERTY" )) {
1612 if (!parse_property( ctx
))
1615 else if (!parse_instruction( ctx
, FALSE
)) {
1624 tgsi_text_translate(
1626 struct tgsi_token
*tokens
,
1629 struct translate_ctx ctx
= {0};
1633 ctx
.tokens
= tokens
;
1634 ctx
.tokens_cur
= tokens
;
1635 ctx
.tokens_end
= tokens
+ num_tokens
;
1637 if (!translate( &ctx
))
1640 return tgsi_sanity_check( tokens
);