1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 static boolean
str_match_no_case( const char **pcur
, const char *str
)
86 const char *cur
= *pcur
;
88 while (*str
!= '\0' && *str
== uprcase( *cur
)) {
99 /* Eat zero or more whitespaces.
101 static void eat_opt_white( const char **pcur
)
103 while (**pcur
== ' ' || **pcur
== '\t' || **pcur
== '\n')
107 /* Eat one or more whitespaces.
108 * Return TRUE if at least one whitespace eaten.
110 static boolean
eat_white( const char **pcur
)
112 const char *cur
= *pcur
;
114 eat_opt_white( pcur
);
118 /* Parse unsigned integer.
119 * No checks for overflow.
121 static boolean
parse_uint( const char **pcur
, uint
*val
)
123 const char *cur
= *pcur
;
125 if (is_digit( cur
)) {
127 while (is_digit( cur
))
128 *val
= *val
* 10 + *cur
++ - '0';
135 static boolean
parse_int( const char **pcur
, int *val
)
137 const char *cur
= *pcur
;
138 int sign
= (*cur
== '-' ? -1 : 1);
140 if (*cur
== '+' || *cur
== '-')
143 if (parse_uint(&cur
, (uint
*)val
)) {
152 static boolean
parse_identifier( const char **pcur
, char *ret
)
154 const char *cur
= *pcur
;
156 if (is_alpha_underscore( cur
)) {
158 while (is_alpha_underscore( cur
) || is_digit( cur
))
167 /* Parse floating point.
169 static boolean
parse_float( const char **pcur
, float *val
)
171 const char *cur
= *pcur
;
172 boolean integral_part
= FALSE
;
173 boolean fractional_part
= FALSE
;
175 *val
= (float) atof( cur
);
177 if (*cur
== '-' || *cur
== '+')
179 if (is_digit( cur
)) {
181 integral_part
= TRUE
;
182 while (is_digit( cur
))
187 if (is_digit( cur
)) {
189 fractional_part
= TRUE
;
190 while (is_digit( cur
))
194 if (!integral_part
&& !fractional_part
)
196 if (uprcase( *cur
) == 'E') {
198 if (*cur
== '-' || *cur
== '+')
200 if (is_digit( cur
)) {
202 while (is_digit( cur
))
216 struct tgsi_token
*tokens
;
217 struct tgsi_token
*tokens_cur
;
218 struct tgsi_token
*tokens_end
;
219 struct tgsi_header
*header
;
220 unsigned processor
: 4;
221 int implied_array_size
: 5;
224 static void report_error( struct translate_ctx
*ctx
, const char *msg
)
228 const char *itr
= ctx
->text
;
230 while (itr
!= ctx
->cur
) {
239 debug_printf( "\nTGSI asm error: %s [%d : %d] \n", msg
, line
, column
);
242 /* Parse shader header.
243 * Return TRUE for one of the following headers.
248 static boolean
parse_header( struct translate_ctx
*ctx
)
252 if (str_match_no_case( &ctx
->cur
, "FRAG" ))
253 processor
= TGSI_PROCESSOR_FRAGMENT
;
254 else if (str_match_no_case( &ctx
->cur
, "VERT" ))
255 processor
= TGSI_PROCESSOR_VERTEX
;
256 else if (str_match_no_case( &ctx
->cur
, "GEOM" ))
257 processor
= TGSI_PROCESSOR_GEOMETRY
;
258 else if (str_match_no_case( &ctx
->cur
, "COMP" ))
259 processor
= TGSI_PROCESSOR_COMPUTE
;
261 report_error( ctx
, "Unknown header" );
265 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
267 ctx
->header
= (struct tgsi_header
*) ctx
->tokens_cur
++;
268 *ctx
->header
= tgsi_build_header();
270 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
272 *(struct tgsi_processor
*) ctx
->tokens_cur
++ = tgsi_build_processor( processor
, ctx
->header
);
273 ctx
->processor
= processor
;
278 static boolean
parse_label( struct translate_ctx
*ctx
, uint
*val
)
280 const char *cur
= ctx
->cur
;
282 if (parse_uint( &cur
, val
)) {
283 eat_opt_white( &cur
);
294 parse_file( const char **pcur
, uint
*file
)
298 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++) {
299 const char *cur
= *pcur
;
301 if (str_match_no_case( &cur
, tgsi_file_names
[i
] )) {
302 if (!is_digit_alpha_underscore( cur
)) {
314 struct translate_ctx
*ctx
,
320 eat_opt_white( &cur
);
323 *writemask
= TGSI_WRITEMASK_NONE
;
324 eat_opt_white( &cur
);
325 if (uprcase( *cur
) == 'X') {
327 *writemask
|= TGSI_WRITEMASK_X
;
329 if (uprcase( *cur
) == 'Y') {
331 *writemask
|= TGSI_WRITEMASK_Y
;
333 if (uprcase( *cur
) == 'Z') {
335 *writemask
|= TGSI_WRITEMASK_Z
;
337 if (uprcase( *cur
) == 'W') {
339 *writemask
|= TGSI_WRITEMASK_W
;
342 if (*writemask
== TGSI_WRITEMASK_NONE
) {
343 report_error( ctx
, "Writemask expected" );
350 *writemask
= TGSI_WRITEMASK_XYZW
;
356 /* <register_file_bracket> ::= <file> `['
359 parse_register_file_bracket(
360 struct translate_ctx
*ctx
,
363 if (!parse_file( &ctx
->cur
, file
)) {
364 report_error( ctx
, "Unknown register file" );
367 eat_opt_white( &ctx
->cur
);
368 if (*ctx
->cur
!= '[') {
369 report_error( ctx
, "Expected `['" );
376 /* <register_file_bracket_index> ::= <register_file_bracket> <uint>
379 parse_register_file_bracket_index(
380 struct translate_ctx
*ctx
,
386 if (!parse_register_file_bracket( ctx
, file
))
388 eat_opt_white( &ctx
->cur
);
389 if (!parse_uint( &ctx
->cur
, &uindex
)) {
390 report_error( ctx
, "Expected literal unsigned integer" );
393 *index
= (int) uindex
;
397 /* Parse simple 1d register operand.
398 * <register_dst> ::= <register_file_bracket_index> `]'
401 parse_register_1d(struct translate_ctx
*ctx
,
405 if (!parse_register_file_bracket_index( ctx
, file
, index
))
407 eat_opt_white( &ctx
->cur
);
408 if (*ctx
->cur
!= ']') {
409 report_error( ctx
, "Expected `]'" );
416 struct parsed_bracket
{
426 parse_register_bracket(
427 struct translate_ctx
*ctx
,
428 struct parsed_bracket
*brackets
)
433 memset(brackets
, 0, sizeof(struct parsed_bracket
));
435 eat_opt_white( &ctx
->cur
);
438 if (parse_file( &cur
, &brackets
->ind_file
)) {
439 if (!parse_register_1d( ctx
, &brackets
->ind_file
,
440 &brackets
->ind_index
))
442 eat_opt_white( &ctx
->cur
);
444 if (*ctx
->cur
== '.') {
446 eat_opt_white(&ctx
->cur
);
448 switch (uprcase(*ctx
->cur
)) {
450 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
453 brackets
->ind_comp
= TGSI_SWIZZLE_Y
;
456 brackets
->ind_comp
= TGSI_SWIZZLE_Z
;
459 brackets
->ind_comp
= TGSI_SWIZZLE_W
;
462 report_error(ctx
, "Expected indirect register swizzle component `x', `y', `z' or `w'");
466 eat_opt_white(&ctx
->cur
);
469 if (*ctx
->cur
== '+' || *ctx
->cur
== '-')
470 parse_int( &ctx
->cur
, &brackets
->index
);
475 if (!parse_uint( &ctx
->cur
, &uindex
)) {
476 report_error( ctx
, "Expected literal unsigned integer" );
479 brackets
->index
= (int) uindex
;
480 brackets
->ind_file
= TGSI_FILE_NULL
;
481 brackets
->ind_index
= 0;
483 eat_opt_white( &ctx
->cur
);
484 if (*ctx
->cur
!= ']') {
485 report_error( ctx
, "Expected `]'" );
493 parse_opt_register_src_bracket(
494 struct translate_ctx
*ctx
,
495 struct parsed_bracket
*brackets
,
496 int *parsed_brackets
)
498 const char *cur
= ctx
->cur
;
500 *parsed_brackets
= 0;
502 eat_opt_white( &cur
);
507 if (!parse_register_bracket(ctx
, brackets
))
510 *parsed_brackets
= 1;
517 /* Parse source register operand.
518 * <register_src> ::= <register_file_bracket_index> `]' |
519 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `]' |
520 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `+' <uint> `]' |
521 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `-' <uint> `]'
525 struct translate_ctx
*ctx
,
527 struct parsed_bracket
*brackets
)
529 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
530 if (!parse_register_file_bracket( ctx
, file
))
532 if (!parse_register_bracket( ctx
, brackets
))
538 struct parsed_dcl_bracket
{
544 parse_register_dcl_bracket(
545 struct translate_ctx
*ctx
,
546 struct parsed_dcl_bracket
*bracket
)
549 memset(bracket
, 0, sizeof(struct parsed_dcl_bracket
));
551 eat_opt_white( &ctx
->cur
);
553 if (!parse_uint( &ctx
->cur
, &uindex
)) {
554 /* it can be an empty bracket [] which means its range
555 * is from 0 to some implied size */
556 if (ctx
->cur
[0] == ']' && ctx
->implied_array_size
!= 0) {
558 bracket
->last
= ctx
->implied_array_size
- 1;
561 report_error( ctx
, "Expected literal unsigned integer" );
564 bracket
->first
= uindex
;
566 eat_opt_white( &ctx
->cur
);
568 if (ctx
->cur
[0] == '.' && ctx
->cur
[1] == '.') {
572 eat_opt_white( &ctx
->cur
);
573 if (!parse_uint( &ctx
->cur
, &uindex
)) {
574 report_error( ctx
, "Expected literal integer" );
577 bracket
->last
= (int) uindex
;
578 eat_opt_white( &ctx
->cur
);
581 bracket
->last
= bracket
->first
;
585 if (*ctx
->cur
!= ']') {
586 report_error( ctx
, "Expected `]' or `..'" );
593 /* Parse register declaration.
594 * <register_dcl> ::= <register_file_bracket_index> `]' |
595 * <register_file_bracket_index> `..' <index> `]'
599 struct translate_ctx
*ctx
,
601 struct parsed_dcl_bracket
*brackets
,
608 if (!parse_register_file_bracket( ctx
, file
))
610 if (!parse_register_dcl_bracket( ctx
, &brackets
[0] ))
616 eat_opt_white( &cur
);
621 if (!parse_register_dcl_bracket( ctx
, &brackets
[1] ))
623 /* for geometry shader we don't really care about
624 * the first brackets it's always the size of the
625 * input primitive. so we want to declare just
626 * the index relevant to the semantics which is in
627 * the second bracket */
628 if (ctx
->processor
== TGSI_PROCESSOR_GEOMETRY
&& *file
== TGSI_FILE_INPUT
) {
629 brackets
[0] = brackets
[1];
640 /* Parse destination register operand.*/
643 struct translate_ctx
*ctx
,
645 struct parsed_bracket
*brackets
)
647 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
648 if (!parse_register_file_bracket( ctx
, file
))
650 if (!parse_register_bracket( ctx
, brackets
))
658 struct translate_ctx
*ctx
,
659 struct tgsi_full_dst_register
*dst
)
664 struct parsed_bracket bracket
[2];
665 int parsed_opt_brackets
;
667 if (!parse_register_dst( ctx
, &file
, &bracket
[0] ))
669 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
673 eat_opt_white( &cur
);
675 if (!parse_opt_writemask( ctx
, &writemask
))
678 dst
->Register
.File
= file
;
679 if (parsed_opt_brackets
) {
680 dst
->Register
.Dimension
= 1;
681 dst
->Dimension
.Indirect
= 0;
682 dst
->Dimension
.Dimension
= 0;
683 dst
->Dimension
.Index
= bracket
[0].index
;
684 bracket
[0] = bracket
[1];
686 dst
->Register
.Index
= bracket
[0].index
;
687 dst
->Register
.WriteMask
= writemask
;
688 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
689 dst
->Register
.Indirect
= 1;
690 dst
->Indirect
.File
= bracket
[0].ind_file
;
691 dst
->Indirect
.Index
= bracket
[0].ind_index
;
692 dst
->Indirect
.SwizzleX
= bracket
[0].ind_comp
;
693 dst
->Indirect
.SwizzleY
= bracket
[0].ind_comp
;
694 dst
->Indirect
.SwizzleZ
= bracket
[0].ind_comp
;
695 dst
->Indirect
.SwizzleW
= bracket
[0].ind_comp
;
701 parse_optional_swizzle(
702 struct translate_ctx
*ctx
,
704 boolean
*parsed_swizzle
)
706 const char *cur
= ctx
->cur
;
708 *parsed_swizzle
= FALSE
;
710 eat_opt_white( &cur
);
715 eat_opt_white( &cur
);
716 for (i
= 0; i
< 4; i
++) {
717 if (uprcase( *cur
) == 'X')
718 swizzle
[i
] = TGSI_SWIZZLE_X
;
719 else if (uprcase( *cur
) == 'Y')
720 swizzle
[i
] = TGSI_SWIZZLE_Y
;
721 else if (uprcase( *cur
) == 'Z')
722 swizzle
[i
] = TGSI_SWIZZLE_Z
;
723 else if (uprcase( *cur
) == 'W')
724 swizzle
[i
] = TGSI_SWIZZLE_W
;
726 report_error( ctx
, "Expected register swizzle component `x', `y', `z' or `w'" );
731 *parsed_swizzle
= TRUE
;
739 struct translate_ctx
*ctx
,
740 struct tgsi_full_src_register
*src
)
744 boolean parsed_swizzle
;
745 struct parsed_bracket bracket
[2];
746 int parsed_opt_brackets
;
748 if (*ctx
->cur
== '-') {
750 eat_opt_white( &ctx
->cur
);
751 src
->Register
.Negate
= 1;
754 if (*ctx
->cur
== '|') {
756 eat_opt_white( &ctx
->cur
);
757 src
->Register
.Absolute
= 1;
760 if (!parse_register_src(ctx
, &file
, &bracket
[0]))
762 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
765 src
->Register
.File
= file
;
766 if (parsed_opt_brackets
) {
767 src
->Register
.Dimension
= 1;
768 src
->Dimension
.Indirect
= 0;
769 src
->Dimension
.Dimension
= 0;
770 src
->Dimension
.Index
= bracket
[0].index
;
771 bracket
[0] = bracket
[1];
773 src
->Register
.Index
= bracket
[0].index
;
774 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
775 src
->Register
.Indirect
= 1;
776 src
->Indirect
.File
= bracket
[0].ind_file
;
777 src
->Indirect
.Index
= bracket
[0].ind_index
;
778 src
->Indirect
.SwizzleX
= bracket
[0].ind_comp
;
779 src
->Indirect
.SwizzleY
= bracket
[0].ind_comp
;
780 src
->Indirect
.SwizzleZ
= bracket
[0].ind_comp
;
781 src
->Indirect
.SwizzleW
= bracket
[0].ind_comp
;
784 /* Parse optional swizzle.
786 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
)) {
787 if (parsed_swizzle
) {
788 src
->Register
.SwizzleX
= swizzle
[0];
789 src
->Register
.SwizzleY
= swizzle
[1];
790 src
->Register
.SwizzleZ
= swizzle
[2];
791 src
->Register
.SwizzleW
= swizzle
[3];
795 if (src
->Register
.Absolute
) {
796 eat_opt_white( &ctx
->cur
);
797 if (*ctx
->cur
!= '|') {
798 report_error( ctx
, "Expected `|'" );
809 match_inst_mnemonic(const char **pcur
,
810 const struct tgsi_opcode_info
*info
)
812 if (str_match_no_case(pcur
, info
->mnemonic
)) {
820 struct translate_ctx
*ctx
,
824 uint saturate
= TGSI_SAT_NONE
;
825 const struct tgsi_opcode_info
*info
;
826 struct tgsi_full_instruction inst
;
830 inst
= tgsi_default_full_instruction();
834 eat_opt_white( &ctx
->cur
);
835 if (*ctx
->cur
== '(') {
839 boolean parsed_swizzle
;
841 inst
.Instruction
.Predicate
= 1;
844 if (*ctx
->cur
== '!') {
846 inst
.Predicate
.Negate
= 1;
849 if (!parse_register_1d( ctx
, &file
, &index
))
852 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
)) {
853 if (parsed_swizzle
) {
854 inst
.Predicate
.SwizzleX
= swizzle
[0];
855 inst
.Predicate
.SwizzleY
= swizzle
[1];
856 inst
.Predicate
.SwizzleZ
= swizzle
[2];
857 inst
.Predicate
.SwizzleW
= swizzle
[3];
861 if (*ctx
->cur
!= ')') {
862 report_error( ctx
, "Expected `)'" );
869 /* Parse instruction name.
871 eat_opt_white( &ctx
->cur
);
872 for (i
= 0; i
< TGSI_OPCODE_LAST
; i
++) {
875 info
= tgsi_get_opcode_info( i
);
876 if (match_inst_mnemonic(&cur
, info
)) {
877 if (str_match_no_case( &cur
, "_SATNV" ))
878 saturate
= TGSI_SAT_MINUS_PLUS_ONE
;
879 else if (str_match_no_case( &cur
, "_SAT" ))
880 saturate
= TGSI_SAT_ZERO_ONE
;
882 if (info
->num_dst
+ info
->num_src
+ info
->is_tex
== 0) {
883 if (!is_digit_alpha_underscore( cur
)) {
888 else if (*cur
== '\0' || eat_white( &cur
)) {
894 if (i
== TGSI_OPCODE_LAST
) {
896 report_error( ctx
, "Unknown opcode" );
898 report_error( ctx
, "Expected `DCL', `IMM' or a label" );
902 inst
.Instruction
.Opcode
= i
;
903 inst
.Instruction
.Saturate
= saturate
;
904 inst
.Instruction
.NumDstRegs
= info
->num_dst
;
905 inst
.Instruction
.NumSrcRegs
= info
->num_src
;
907 /* Parse instruction operands.
909 for (i
= 0; i
< info
->num_dst
+ info
->num_src
+ info
->is_tex
; i
++) {
911 eat_opt_white( &ctx
->cur
);
912 if (*ctx
->cur
!= ',') {
913 report_error( ctx
, "Expected `,'" );
917 eat_opt_white( &ctx
->cur
);
920 if (i
< info
->num_dst
) {
921 if (!parse_dst_operand( ctx
, &inst
.Dst
[i
] ))
924 else if (i
< info
->num_dst
+ info
->num_src
) {
925 if (!parse_src_operand( ctx
, &inst
.Src
[i
- info
->num_dst
] ))
931 for (j
= 0; j
< TGSI_TEXTURE_COUNT
; j
++) {
932 if (str_match_no_case( &ctx
->cur
, tgsi_texture_names
[j
] )) {
933 if (!is_digit_alpha_underscore( ctx
->cur
)) {
934 inst
.Instruction
.Texture
= 1;
935 inst
.Texture
.Texture
= j
;
940 if (j
== TGSI_TEXTURE_COUNT
) {
941 report_error( ctx
, "Expected texture target" );
948 eat_opt_white( &cur
);
949 if (info
->is_branch
&& *cur
== ':') {
953 eat_opt_white( &cur
);
954 if (!parse_uint( &cur
, &target
)) {
955 report_error( ctx
, "Expected a label" );
958 inst
.Instruction
.Label
= 1;
959 inst
.Label
.Label
= target
;
963 advance
= tgsi_build_full_instruction(
967 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
970 ctx
->tokens_cur
+= advance
;
975 /* parses a 4-touple of the form {x, y, z, w}
976 * where x, y, z, w are numbers */
977 static boolean
parse_immediate_data(struct translate_ctx
*ctx
, unsigned type
,
978 union tgsi_immediate_data
*values
)
983 eat_opt_white( &ctx
->cur
);
984 if (*ctx
->cur
!= '{') {
985 report_error( ctx
, "Expected `{'" );
989 for (i
= 0; i
< 4; i
++) {
990 eat_opt_white( &ctx
->cur
);
992 if (*ctx
->cur
!= ',') {
993 report_error( ctx
, "Expected `,'" );
997 eat_opt_white( &ctx
->cur
);
1001 case TGSI_IMM_FLOAT32
:
1002 ret
= parse_float(&ctx
->cur
, &values
[i
].Float
);
1004 case TGSI_IMM_UINT32
:
1005 ret
= parse_uint(&ctx
->cur
, &values
[i
].Uint
);
1007 case TGSI_IMM_INT32
:
1008 ret
= parse_int(&ctx
->cur
, &values
[i
].Int
);
1013 report_error( ctx
, "Expected immediate constant" );
1017 eat_opt_white( &ctx
->cur
);
1018 if (*ctx
->cur
!= '}') {
1019 report_error( ctx
, "Expected `}'" );
1027 static boolean
parse_declaration( struct translate_ctx
*ctx
)
1029 struct tgsi_full_declaration decl
;
1031 struct parsed_dcl_bracket brackets
[2];
1034 const char *cur
, *cur2
;
1036 boolean is_vs_input
;
1037 boolean is_imm_array
;
1039 if (!eat_white( &ctx
->cur
)) {
1040 report_error( ctx
, "Syntax error" );
1043 if (!parse_register_dcl( ctx
, &file
, brackets
, &num_brackets
))
1045 if (!parse_opt_writemask( ctx
, &writemask
))
1048 decl
= tgsi_default_full_declaration();
1049 decl
.Declaration
.File
= file
;
1050 decl
.Declaration
.UsageMask
= writemask
;
1052 if (num_brackets
== 1) {
1053 decl
.Range
.First
= brackets
[0].first
;
1054 decl
.Range
.Last
= brackets
[0].last
;
1056 decl
.Range
.First
= brackets
[1].first
;
1057 decl
.Range
.Last
= brackets
[1].last
;
1059 decl
.Declaration
.Dimension
= 1;
1060 decl
.Dim
.Index2D
= brackets
[0].first
;
1063 is_vs_input
= (file
== TGSI_FILE_INPUT
&&
1064 ctx
->processor
== TGSI_PROCESSOR_VERTEX
);
1065 is_imm_array
= (file
== TGSI_FILE_IMMEDIATE_ARRAY
);
1068 eat_opt_white( &cur
);
1069 if (*cur
== ',' && !is_vs_input
) {
1073 eat_opt_white( &cur
);
1074 if (file
== TGSI_FILE_RESOURCE
) {
1075 for (i
= 0; i
< TGSI_TEXTURE_COUNT
; i
++) {
1076 if (str_match_no_case(&cur
, tgsi_texture_names
[i
])) {
1077 if (!is_digit_alpha_underscore(cur
)) {
1078 decl
.Resource
.Resource
= i
;
1083 if (i
== TGSI_TEXTURE_COUNT
) {
1084 report_error(ctx
, "Expected texture target");
1089 eat_opt_white(&cur2
);
1090 while (*cur2
== ',') {
1092 eat_opt_white(&cur2
);
1093 if (str_match_no_case(&cur2
, "RAW") &&
1094 !is_digit_alpha_underscore(cur2
)) {
1095 decl
.Resource
.Raw
= 1;
1097 } else if (str_match_no_case(&cur2
, "WR") &&
1098 !is_digit_alpha_underscore(cur2
)) {
1099 decl
.Resource
.Writable
= 1;
1105 eat_opt_white(&cur2
);
1110 } else if (file
== TGSI_FILE_SAMPLER_VIEW
) {
1111 for (i
= 0; i
< TGSI_TEXTURE_COUNT
; i
++) {
1112 if (str_match_no_case(&cur
, tgsi_texture_names
[i
])) {
1113 if (!is_digit_alpha_underscore(cur
)) {
1114 decl
.SamplerView
.Resource
= i
;
1119 if (i
== TGSI_TEXTURE_COUNT
) {
1120 report_error(ctx
, "Expected texture target");
1123 eat_opt_white( &cur
);
1125 report_error( ctx
, "Expected `,'" );
1129 eat_opt_white( &cur
);
1130 for (j
= 0; j
< 4; ++j
) {
1131 for (i
= 0; i
< PIPE_TYPE_COUNT
; ++i
) {
1132 if (str_match_no_case(&cur
, tgsi_type_names
[i
])) {
1133 if (!is_digit_alpha_underscore(cur
)) {
1136 decl
.SamplerView
.ReturnTypeX
= i
;
1139 decl
.SamplerView
.ReturnTypeY
= i
;
1142 decl
.SamplerView
.ReturnTypeZ
= i
;
1145 decl
.SamplerView
.ReturnTypeW
= i
;
1154 if (i
== PIPE_TYPE_COUNT
) {
1155 if (j
== 0 || j
> 2) {
1156 report_error(ctx
, "Expected type name");
1162 eat_opt_white( &cur2
);
1165 eat_opt_white( &cur2
);
1173 decl
.SamplerView
.ReturnTypeY
=
1174 decl
.SamplerView
.ReturnTypeZ
=
1175 decl
.SamplerView
.ReturnTypeW
=
1176 decl
.SamplerView
.ReturnTypeX
;
1180 if (str_match_no_case(&cur
, "LOCAL") &&
1181 !is_digit_alpha_underscore(cur
)) {
1182 decl
.Declaration
.Local
= 1;
1187 eat_opt_white( &cur
);
1190 eat_opt_white( &cur
);
1192 for (i
= 0; i
< TGSI_SEMANTIC_COUNT
; i
++) {
1193 if (str_match_no_case( &cur
, tgsi_semantic_names
[i
] )) {
1196 if (is_digit_alpha_underscore( cur
))
1199 eat_opt_white( &cur2
);
1202 eat_opt_white( &cur2
);
1203 if (!parse_uint( &cur2
, &index
)) {
1204 report_error( ctx
, "Expected literal integer" );
1207 eat_opt_white( &cur2
);
1209 report_error( ctx
, "Expected `]'" );
1214 decl
.Semantic
.Index
= index
;
1219 decl
.Declaration
.Semantic
= 1;
1220 decl
.Semantic
.Name
= i
;
1228 } else if (is_imm_array
) {
1230 union tgsi_immediate_data
*vals_itr
;
1231 /* we have our immediate data */
1233 report_error( ctx
, "Immediate array without data" );
1239 decl
.ImmediateData
.u
=
1240 MALLOC(sizeof(union tgsi_immediate_data
) * 4 *
1241 (decl
.Range
.Last
+ 1));
1242 vals_itr
= decl
.ImmediateData
.u
;
1243 for (i
= 0; i
<= decl
.Range
.Last
; ++i
) {
1244 if (!parse_immediate_data(ctx
, TGSI_IMM_FLOAT32
, vals_itr
)) {
1245 FREE(decl
.ImmediateData
.u
);
1249 eat_opt_white( &ctx
->cur
);
1250 if (*ctx
->cur
!= ',') {
1251 if (i
!= decl
.Range
.Last
) {
1252 report_error( ctx
, "Not enough data in immediate array!" );
1253 FREE(decl
.ImmediateData
.u
);
1259 eat_opt_white( &ctx
->cur
);
1260 if (*ctx
->cur
!= '}') {
1261 FREE(decl
.ImmediateData
.u
);
1262 report_error( ctx
, "Immediate array data missing closing '}'" );
1269 eat_opt_white( &cur
);
1270 if (*cur
== ',' && !is_vs_input
) {
1274 eat_opt_white( &cur
);
1275 for (i
= 0; i
< TGSI_INTERPOLATE_COUNT
; i
++) {
1276 if (str_match_no_case( &cur
, tgsi_interpolate_names
[i
] )) {
1277 if (is_digit_alpha_underscore( cur
))
1279 decl
.Declaration
.Interpolate
= 1;
1280 decl
.Interp
.Interpolate
= i
;
1286 if (i
== TGSI_INTERPOLATE_COUNT
) {
1287 report_error( ctx
, "Expected semantic or interpolate attribute" );
1292 advance
= tgsi_build_full_declaration(
1296 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1299 FREE(decl
.ImmediateData
.u
);
1303 ctx
->tokens_cur
+= advance
;
1308 static boolean
parse_immediate( struct translate_ctx
*ctx
)
1310 struct tgsi_full_immediate imm
;
1314 if (!eat_white( &ctx
->cur
)) {
1315 report_error( ctx
, "Syntax error" );
1318 for (type
= 0; type
< Elements(tgsi_immediate_type_names
); ++type
) {
1319 if (str_match_no_case(&ctx
->cur
, tgsi_immediate_type_names
[type
]) &&
1320 !is_digit_alpha_underscore(ctx
->cur
))
1323 if (type
== Elements(tgsi_immediate_type_names
)) {
1324 report_error( ctx
, "Expected immediate type" );
1328 imm
= tgsi_default_full_immediate();
1329 imm
.Immediate
.NrTokens
+= 4;
1330 imm
.Immediate
.DataType
= type
;
1331 parse_immediate_data(ctx
, type
, imm
.u
);
1333 advance
= tgsi_build_full_immediate(
1337 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1340 ctx
->tokens_cur
+= advance
;
1346 parse_primitive( const char **pcur
, uint
*primitive
)
1350 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++) {
1351 const char *cur
= *pcur
;
1353 if (str_match_no_case( &cur
, tgsi_primitive_names
[i
])) {
1363 parse_fs_coord_origin( const char **pcur
, uint
*fs_coord_origin
)
1367 for (i
= 0; i
< Elements(tgsi_fs_coord_origin_names
); i
++) {
1368 const char *cur
= *pcur
;
1370 if (str_match_no_case( &cur
, tgsi_fs_coord_origin_names
[i
])) {
1371 *fs_coord_origin
= i
;
1380 parse_fs_coord_pixel_center( const char **pcur
, uint
*fs_coord_pixel_center
)
1384 for (i
= 0; i
< Elements(tgsi_fs_coord_pixel_center_names
); i
++) {
1385 const char *cur
= *pcur
;
1387 if (str_match_no_case( &cur
, tgsi_fs_coord_pixel_center_names
[i
])) {
1388 *fs_coord_pixel_center
= i
;
1397 static boolean
parse_property( struct translate_ctx
*ctx
)
1399 struct tgsi_full_property prop
;
1405 if (!eat_white( &ctx
->cur
)) {
1406 report_error( ctx
, "Syntax error" );
1409 if (!parse_identifier( &ctx
->cur
, id
)) {
1410 report_error( ctx
, "Syntax error" );
1413 for (property_name
= 0; property_name
< TGSI_PROPERTY_COUNT
;
1415 if (streq_nocase_uprcase(tgsi_property_names
[property_name
], id
)) {
1419 if (property_name
>= TGSI_PROPERTY_COUNT
) {
1420 debug_printf( "\nError: Unknown property : '%s'", id
);
1424 eat_opt_white( &ctx
->cur
);
1425 switch(property_name
) {
1426 case TGSI_PROPERTY_GS_INPUT_PRIM
:
1427 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
1428 if (!parse_primitive(&ctx
->cur
, &values
[0] )) {
1429 report_error( ctx
, "Unknown primitive name as property!" );
1432 if (property_name
== TGSI_PROPERTY_GS_INPUT_PRIM
&&
1433 ctx
->processor
== TGSI_PROCESSOR_GEOMETRY
) {
1434 ctx
->implied_array_size
= u_vertices_per_prim(values
[0]);
1437 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
1438 if (!parse_fs_coord_origin(&ctx
->cur
, &values
[0] )) {
1439 report_error( ctx
, "Unknown coord origin as property: must be UPPER_LEFT or LOWER_LEFT!" );
1443 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
1444 if (!parse_fs_coord_pixel_center(&ctx
->cur
, &values
[0] )) {
1445 report_error( ctx
, "Unknown coord pixel center as property: must be HALF_INTEGER or INTEGER!" );
1449 case TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
:
1451 if (!parse_uint(&ctx
->cur
, &values
[0] )) {
1452 report_error( ctx
, "Expected unsigned integer as property!" );
1457 prop
= tgsi_default_full_property();
1458 prop
.Property
.PropertyName
= property_name
;
1459 prop
.Property
.NrTokens
+= 1;
1460 prop
.u
[0].Data
= values
[0];
1462 advance
= tgsi_build_full_property(
1466 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1469 ctx
->tokens_cur
+= advance
;
1475 static boolean
translate( struct translate_ctx
*ctx
)
1477 eat_opt_white( &ctx
->cur
);
1478 if (!parse_header( ctx
))
1481 while (*ctx
->cur
!= '\0') {
1483 if (!eat_white( &ctx
->cur
)) {
1484 report_error( ctx
, "Syntax error" );
1488 if (*ctx
->cur
== '\0')
1490 if (parse_label( ctx
, &label_val
)) {
1491 if (!parse_instruction( ctx
, TRUE
))
1494 else if (str_match_no_case( &ctx
->cur
, "DCL" )) {
1495 if (!parse_declaration( ctx
))
1498 else if (str_match_no_case( &ctx
->cur
, "IMM" )) {
1499 if (!parse_immediate( ctx
))
1502 else if (str_match_no_case( &ctx
->cur
, "PROPERTY" )) {
1503 if (!parse_property( ctx
))
1506 else if (!parse_instruction( ctx
, FALSE
)) {
1515 tgsi_text_translate(
1517 struct tgsi_token
*tokens
,
1520 struct translate_ctx ctx
;
1524 ctx
.tokens
= tokens
;
1525 ctx
.tokens_cur
= tokens
;
1526 ctx
.tokens_end
= tokens
+ num_tokens
;
1528 if (!translate( &ctx
))
1531 return tgsi_sanity_check( tokens
);