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 /* 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;
247 static void report_error( struct translate_ctx
*ctx
, const char *msg
)
251 const char *itr
= ctx
->text
;
253 while (itr
!= ctx
->cur
) {
262 debug_printf( "\nTGSI asm error: %s [%d : %d] \n", msg
, line
, column
);
265 /* Parse shader header.
266 * Return TRUE for one of the following headers.
271 static boolean
parse_header( struct translate_ctx
*ctx
)
275 if (str_match_nocase_whole( &ctx
->cur
, "FRAG" ))
276 processor
= TGSI_PROCESSOR_FRAGMENT
;
277 else if (str_match_nocase_whole( &ctx
->cur
, "VERT" ))
278 processor
= TGSI_PROCESSOR_VERTEX
;
279 else if (str_match_nocase_whole( &ctx
->cur
, "GEOM" ))
280 processor
= TGSI_PROCESSOR_GEOMETRY
;
281 else if (str_match_nocase_whole( &ctx
->cur
, "COMP" ))
282 processor
= TGSI_PROCESSOR_COMPUTE
;
284 report_error( ctx
, "Unknown header" );
288 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
290 ctx
->header
= (struct tgsi_header
*) ctx
->tokens_cur
++;
291 *ctx
->header
= tgsi_build_header();
293 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
295 *(struct tgsi_processor
*) ctx
->tokens_cur
++ = tgsi_build_processor( processor
, ctx
->header
);
296 ctx
->processor
= processor
;
301 static boolean
parse_label( struct translate_ctx
*ctx
, uint
*val
)
303 const char *cur
= ctx
->cur
;
305 if (parse_uint( &cur
, val
)) {
306 eat_opt_white( &cur
);
317 parse_file( const char **pcur
, uint
*file
)
321 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++) {
322 const char *cur
= *pcur
;
324 if (str_match_nocase_whole( &cur
, tgsi_file_names
[i
] )) {
335 struct translate_ctx
*ctx
,
341 eat_opt_white( &cur
);
344 *writemask
= TGSI_WRITEMASK_NONE
;
345 eat_opt_white( &cur
);
346 if (uprcase( *cur
) == 'X') {
348 *writemask
|= TGSI_WRITEMASK_X
;
350 if (uprcase( *cur
) == 'Y') {
352 *writemask
|= TGSI_WRITEMASK_Y
;
354 if (uprcase( *cur
) == 'Z') {
356 *writemask
|= TGSI_WRITEMASK_Z
;
358 if (uprcase( *cur
) == 'W') {
360 *writemask
|= TGSI_WRITEMASK_W
;
363 if (*writemask
== TGSI_WRITEMASK_NONE
) {
364 report_error( ctx
, "Writemask expected" );
371 *writemask
= TGSI_WRITEMASK_XYZW
;
377 /* <register_file_bracket> ::= <file> `['
380 parse_register_file_bracket(
381 struct translate_ctx
*ctx
,
384 if (!parse_file( &ctx
->cur
, file
)) {
385 report_error( ctx
, "Unknown register file" );
388 eat_opt_white( &ctx
->cur
);
389 if (*ctx
->cur
!= '[') {
390 report_error( ctx
, "Expected `['" );
397 /* <register_file_bracket_index> ::= <register_file_bracket> <uint>
400 parse_register_file_bracket_index(
401 struct translate_ctx
*ctx
,
407 if (!parse_register_file_bracket( ctx
, file
))
409 eat_opt_white( &ctx
->cur
);
410 if (!parse_uint( &ctx
->cur
, &uindex
)) {
411 report_error( ctx
, "Expected literal unsigned integer" );
414 *index
= (int) uindex
;
418 /* Parse simple 1d register operand.
419 * <register_dst> ::= <register_file_bracket_index> `]'
422 parse_register_1d(struct translate_ctx
*ctx
,
426 if (!parse_register_file_bracket_index( ctx
, file
, index
))
428 eat_opt_white( &ctx
->cur
);
429 if (*ctx
->cur
!= ']') {
430 report_error( ctx
, "Expected `]'" );
437 struct parsed_bracket
{
447 parse_register_bracket(
448 struct translate_ctx
*ctx
,
449 struct parsed_bracket
*brackets
)
454 memset(brackets
, 0, sizeof(struct parsed_bracket
));
456 eat_opt_white( &ctx
->cur
);
459 if (parse_file( &cur
, &brackets
->ind_file
)) {
460 if (!parse_register_1d( ctx
, &brackets
->ind_file
,
461 &brackets
->ind_index
))
463 eat_opt_white( &ctx
->cur
);
465 if (*ctx
->cur
== '.') {
467 eat_opt_white(&ctx
->cur
);
469 switch (uprcase(*ctx
->cur
)) {
471 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
474 brackets
->ind_comp
= TGSI_SWIZZLE_Y
;
477 brackets
->ind_comp
= TGSI_SWIZZLE_Z
;
480 brackets
->ind_comp
= TGSI_SWIZZLE_W
;
483 report_error(ctx
, "Expected indirect register swizzle component `x', `y', `z' or `w'");
487 eat_opt_white(&ctx
->cur
);
490 if (*ctx
->cur
== '+' || *ctx
->cur
== '-')
491 parse_int( &ctx
->cur
, &brackets
->index
);
496 if (!parse_uint( &ctx
->cur
, &uindex
)) {
497 report_error( ctx
, "Expected literal unsigned integer" );
500 brackets
->index
= (int) uindex
;
501 brackets
->ind_file
= TGSI_FILE_NULL
;
502 brackets
->ind_index
= 0;
504 eat_opt_white( &ctx
->cur
);
505 if (*ctx
->cur
!= ']') {
506 report_error( ctx
, "Expected `]'" );
514 parse_opt_register_src_bracket(
515 struct translate_ctx
*ctx
,
516 struct parsed_bracket
*brackets
,
517 int *parsed_brackets
)
519 const char *cur
= ctx
->cur
;
521 *parsed_brackets
= 0;
523 eat_opt_white( &cur
);
528 if (!parse_register_bracket(ctx
, brackets
))
531 *parsed_brackets
= 1;
538 /* Parse source register operand.
539 * <register_src> ::= <register_file_bracket_index> `]' |
540 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `]' |
541 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `+' <uint> `]' |
542 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `-' <uint> `]'
546 struct translate_ctx
*ctx
,
548 struct parsed_bracket
*brackets
)
550 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
551 if (!parse_register_file_bracket( ctx
, file
))
553 if (!parse_register_bracket( ctx
, brackets
))
559 struct parsed_dcl_bracket
{
565 parse_register_dcl_bracket(
566 struct translate_ctx
*ctx
,
567 struct parsed_dcl_bracket
*bracket
)
570 memset(bracket
, 0, sizeof(struct parsed_dcl_bracket
));
572 eat_opt_white( &ctx
->cur
);
574 if (!parse_uint( &ctx
->cur
, &uindex
)) {
575 /* it can be an empty bracket [] which means its range
576 * is from 0 to some implied size */
577 if (ctx
->cur
[0] == ']' && ctx
->implied_array_size
!= 0) {
579 bracket
->last
= ctx
->implied_array_size
- 1;
582 report_error( ctx
, "Expected literal unsigned integer" );
585 bracket
->first
= uindex
;
587 eat_opt_white( &ctx
->cur
);
589 if (ctx
->cur
[0] == '.' && ctx
->cur
[1] == '.') {
593 eat_opt_white( &ctx
->cur
);
594 if (!parse_uint( &ctx
->cur
, &uindex
)) {
595 report_error( ctx
, "Expected literal integer" );
598 bracket
->last
= (int) uindex
;
599 eat_opt_white( &ctx
->cur
);
602 bracket
->last
= bracket
->first
;
606 if (*ctx
->cur
!= ']') {
607 report_error( ctx
, "Expected `]' or `..'" );
614 /* Parse register declaration.
615 * <register_dcl> ::= <register_file_bracket_index> `]' |
616 * <register_file_bracket_index> `..' <index> `]'
620 struct translate_ctx
*ctx
,
622 struct parsed_dcl_bracket
*brackets
,
629 if (!parse_register_file_bracket( ctx
, file
))
631 if (!parse_register_dcl_bracket( ctx
, &brackets
[0] ))
637 eat_opt_white( &cur
);
642 if (!parse_register_dcl_bracket( ctx
, &brackets
[1] ))
644 /* for geometry shader we don't really care about
645 * the first brackets it's always the size of the
646 * input primitive. so we want to declare just
647 * the index relevant to the semantics which is in
648 * the second bracket */
649 if (ctx
->processor
== TGSI_PROCESSOR_GEOMETRY
&& *file
== TGSI_FILE_INPUT
) {
650 brackets
[0] = brackets
[1];
661 /* Parse destination register operand.*/
664 struct translate_ctx
*ctx
,
666 struct parsed_bracket
*brackets
)
668 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
669 if (!parse_register_file_bracket( ctx
, file
))
671 if (!parse_register_bracket( ctx
, brackets
))
679 struct translate_ctx
*ctx
,
680 struct tgsi_full_dst_register
*dst
)
685 struct parsed_bracket bracket
[2];
686 int parsed_opt_brackets
;
688 if (!parse_register_dst( ctx
, &file
, &bracket
[0] ))
690 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
694 eat_opt_white( &cur
);
696 if (!parse_opt_writemask( ctx
, &writemask
))
699 dst
->Register
.File
= file
;
700 if (parsed_opt_brackets
) {
701 dst
->Register
.Dimension
= 1;
702 dst
->Dimension
.Indirect
= 0;
703 dst
->Dimension
.Dimension
= 0;
704 dst
->Dimension
.Index
= bracket
[0].index
;
705 bracket
[0] = bracket
[1];
707 dst
->Register
.Index
= bracket
[0].index
;
708 dst
->Register
.WriteMask
= writemask
;
709 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
710 dst
->Register
.Indirect
= 1;
711 dst
->Indirect
.File
= bracket
[0].ind_file
;
712 dst
->Indirect
.Index
= bracket
[0].ind_index
;
713 dst
->Indirect
.SwizzleX
= bracket
[0].ind_comp
;
714 dst
->Indirect
.SwizzleY
= bracket
[0].ind_comp
;
715 dst
->Indirect
.SwizzleZ
= bracket
[0].ind_comp
;
716 dst
->Indirect
.SwizzleW
= bracket
[0].ind_comp
;
722 parse_optional_swizzle(
723 struct translate_ctx
*ctx
,
725 boolean
*parsed_swizzle
)
727 const char *cur
= ctx
->cur
;
729 *parsed_swizzle
= FALSE
;
731 eat_opt_white( &cur
);
736 eat_opt_white( &cur
);
737 for (i
= 0; i
< 4; i
++) {
738 if (uprcase( *cur
) == 'X')
739 swizzle
[i
] = TGSI_SWIZZLE_X
;
740 else if (uprcase( *cur
) == 'Y')
741 swizzle
[i
] = TGSI_SWIZZLE_Y
;
742 else if (uprcase( *cur
) == 'Z')
743 swizzle
[i
] = TGSI_SWIZZLE_Z
;
744 else if (uprcase( *cur
) == 'W')
745 swizzle
[i
] = TGSI_SWIZZLE_W
;
747 report_error( ctx
, "Expected register swizzle component `x', `y', `z' or `w'" );
752 *parsed_swizzle
= TRUE
;
760 struct translate_ctx
*ctx
,
761 struct tgsi_full_src_register
*src
)
765 boolean parsed_swizzle
;
766 struct parsed_bracket bracket
[2];
767 int parsed_opt_brackets
;
769 if (*ctx
->cur
== '-') {
771 eat_opt_white( &ctx
->cur
);
772 src
->Register
.Negate
= 1;
775 if (*ctx
->cur
== '|') {
777 eat_opt_white( &ctx
->cur
);
778 src
->Register
.Absolute
= 1;
781 if (!parse_register_src(ctx
, &file
, &bracket
[0]))
783 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
786 src
->Register
.File
= file
;
787 if (parsed_opt_brackets
) {
788 src
->Register
.Dimension
= 1;
789 src
->Dimension
.Indirect
= 0;
790 src
->Dimension
.Dimension
= 0;
791 src
->Dimension
.Index
= bracket
[0].index
;
792 bracket
[0] = bracket
[1];
794 src
->Register
.Index
= bracket
[0].index
;
795 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
796 src
->Register
.Indirect
= 1;
797 src
->Indirect
.File
= bracket
[0].ind_file
;
798 src
->Indirect
.Index
= bracket
[0].ind_index
;
799 src
->Indirect
.SwizzleX
= bracket
[0].ind_comp
;
800 src
->Indirect
.SwizzleY
= bracket
[0].ind_comp
;
801 src
->Indirect
.SwizzleZ
= bracket
[0].ind_comp
;
802 src
->Indirect
.SwizzleW
= bracket
[0].ind_comp
;
805 /* Parse optional swizzle.
807 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
)) {
808 if (parsed_swizzle
) {
809 src
->Register
.SwizzleX
= swizzle
[0];
810 src
->Register
.SwizzleY
= swizzle
[1];
811 src
->Register
.SwizzleZ
= swizzle
[2];
812 src
->Register
.SwizzleW
= swizzle
[3];
816 if (src
->Register
.Absolute
) {
817 eat_opt_white( &ctx
->cur
);
818 if (*ctx
->cur
!= '|') {
819 report_error( ctx
, "Expected `|'" );
830 match_inst(const char **pcur
,
832 const struct tgsi_opcode_info
*info
)
834 const char *cur
= *pcur
;
836 /* simple case: the whole string matches the instruction name */
837 if (str_match_nocase_whole(&cur
, info
->mnemonic
)) {
839 *saturate
= TGSI_SAT_NONE
;
843 if (str_match_no_case(&cur
, info
->mnemonic
)) {
844 /* the instruction has a suffix, figure it out */
845 if (str_match_nocase_whole(&cur
, "_SAT")) {
847 *saturate
= TGSI_SAT_ZERO_ONE
;
851 if (str_match_nocase_whole(&cur
, "_SATNV")) {
853 *saturate
= TGSI_SAT_MINUS_PLUS_ONE
;
863 struct translate_ctx
*ctx
,
867 uint saturate
= TGSI_SAT_NONE
;
868 const struct tgsi_opcode_info
*info
;
869 struct tgsi_full_instruction inst
;
873 inst
= tgsi_default_full_instruction();
877 eat_opt_white( &ctx
->cur
);
878 if (*ctx
->cur
== '(') {
882 boolean parsed_swizzle
;
884 inst
.Instruction
.Predicate
= 1;
887 if (*ctx
->cur
== '!') {
889 inst
.Predicate
.Negate
= 1;
892 if (!parse_register_1d( ctx
, &file
, &index
))
895 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
)) {
896 if (parsed_swizzle
) {
897 inst
.Predicate
.SwizzleX
= swizzle
[0];
898 inst
.Predicate
.SwizzleY
= swizzle
[1];
899 inst
.Predicate
.SwizzleZ
= swizzle
[2];
900 inst
.Predicate
.SwizzleW
= swizzle
[3];
904 if (*ctx
->cur
!= ')') {
905 report_error( ctx
, "Expected `)'" );
912 /* Parse instruction name.
914 eat_opt_white( &ctx
->cur
);
915 for (i
= 0; i
< TGSI_OPCODE_LAST
; i
++) {
918 info
= tgsi_get_opcode_info( i
);
919 if (match_inst(&cur
, &saturate
, info
)) {
920 if (info
->num_dst
+ info
->num_src
+ info
->is_tex
== 0) {
924 else if (*cur
== '\0' || eat_white( &cur
)) {
930 if (i
== TGSI_OPCODE_LAST
) {
932 report_error( ctx
, "Unknown opcode" );
934 report_error( ctx
, "Expected `DCL', `IMM' or a label" );
938 inst
.Instruction
.Opcode
= i
;
939 inst
.Instruction
.Saturate
= saturate
;
940 inst
.Instruction
.NumDstRegs
= info
->num_dst
;
941 inst
.Instruction
.NumSrcRegs
= info
->num_src
;
943 /* Parse instruction operands.
945 for (i
= 0; i
< info
->num_dst
+ info
->num_src
+ info
->is_tex
; i
++) {
947 eat_opt_white( &ctx
->cur
);
948 if (*ctx
->cur
!= ',') {
949 report_error( ctx
, "Expected `,'" );
953 eat_opt_white( &ctx
->cur
);
956 if (i
< info
->num_dst
) {
957 if (!parse_dst_operand( ctx
, &inst
.Dst
[i
] ))
960 else if (i
< info
->num_dst
+ info
->num_src
) {
961 if (!parse_src_operand( ctx
, &inst
.Src
[i
- info
->num_dst
] ))
967 for (j
= 0; j
< TGSI_TEXTURE_COUNT
; j
++) {
968 if (str_match_nocase_whole( &ctx
->cur
, tgsi_texture_names
[j
] )) {
969 inst
.Instruction
.Texture
= 1;
970 inst
.Texture
.Texture
= j
;
974 if (j
== TGSI_TEXTURE_COUNT
) {
975 report_error( ctx
, "Expected texture target" );
982 eat_opt_white( &cur
);
983 if (info
->is_branch
&& *cur
== ':') {
987 eat_opt_white( &cur
);
988 if (!parse_uint( &cur
, &target
)) {
989 report_error( ctx
, "Expected a label" );
992 inst
.Instruction
.Label
= 1;
993 inst
.Label
.Label
= target
;
997 advance
= tgsi_build_full_instruction(
1001 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1004 ctx
->tokens_cur
+= advance
;
1009 /* parses a 4-touple of the form {x, y, z, w}
1010 * where x, y, z, w are numbers */
1011 static boolean
parse_immediate_data(struct translate_ctx
*ctx
, unsigned type
,
1012 union tgsi_immediate_data
*values
)
1017 eat_opt_white( &ctx
->cur
);
1018 if (*ctx
->cur
!= '{') {
1019 report_error( ctx
, "Expected `{'" );
1023 for (i
= 0; i
< 4; i
++) {
1024 eat_opt_white( &ctx
->cur
);
1026 if (*ctx
->cur
!= ',') {
1027 report_error( ctx
, "Expected `,'" );
1031 eat_opt_white( &ctx
->cur
);
1035 case TGSI_IMM_FLOAT32
:
1036 ret
= parse_float(&ctx
->cur
, &values
[i
].Float
);
1038 case TGSI_IMM_UINT32
:
1039 ret
= parse_uint(&ctx
->cur
, &values
[i
].Uint
);
1041 case TGSI_IMM_INT32
:
1042 ret
= parse_int(&ctx
->cur
, &values
[i
].Int
);
1051 report_error( ctx
, "Expected immediate constant" );
1055 eat_opt_white( &ctx
->cur
);
1056 if (*ctx
->cur
!= '}') {
1057 report_error( ctx
, "Expected `}'" );
1065 static boolean
parse_declaration( struct translate_ctx
*ctx
)
1067 struct tgsi_full_declaration decl
;
1069 struct parsed_dcl_bracket brackets
[2];
1072 const char *cur
, *cur2
;
1074 boolean is_vs_input
;
1075 boolean is_imm_array
;
1077 if (!eat_white( &ctx
->cur
)) {
1078 report_error( ctx
, "Syntax error" );
1081 if (!parse_register_dcl( ctx
, &file
, brackets
, &num_brackets
))
1083 if (!parse_opt_writemask( ctx
, &writemask
))
1086 decl
= tgsi_default_full_declaration();
1087 decl
.Declaration
.File
= file
;
1088 decl
.Declaration
.UsageMask
= writemask
;
1090 if (num_brackets
== 1) {
1091 decl
.Range
.First
= brackets
[0].first
;
1092 decl
.Range
.Last
= brackets
[0].last
;
1094 decl
.Range
.First
= brackets
[1].first
;
1095 decl
.Range
.Last
= brackets
[1].last
;
1097 decl
.Declaration
.Dimension
= 1;
1098 decl
.Dim
.Index2D
= brackets
[0].first
;
1101 is_vs_input
= (file
== TGSI_FILE_INPUT
&&
1102 ctx
->processor
== TGSI_PROCESSOR_VERTEX
);
1103 is_imm_array
= (file
== TGSI_FILE_IMMEDIATE_ARRAY
);
1106 eat_opt_white( &cur
);
1107 if (*cur
== ',' && !is_vs_input
) {
1111 eat_opt_white( &cur
);
1112 if (file
== TGSI_FILE_RESOURCE
) {
1113 for (i
= 0; i
< TGSI_TEXTURE_COUNT
; i
++) {
1114 if (str_match_nocase_whole(&cur
, tgsi_texture_names
[i
])) {
1115 decl
.Resource
.Resource
= i
;
1119 if (i
== TGSI_TEXTURE_COUNT
) {
1120 report_error(ctx
, "Expected texture target");
1125 eat_opt_white(&cur2
);
1126 while (*cur2
== ',') {
1128 eat_opt_white(&cur2
);
1129 if (str_match_nocase_whole(&cur2
, "RAW")) {
1130 decl
.Resource
.Raw
= 1;
1132 } else if (str_match_nocase_whole(&cur2
, "WR")) {
1133 decl
.Resource
.Writable
= 1;
1139 eat_opt_white(&cur2
);
1144 } else if (file
== TGSI_FILE_SAMPLER_VIEW
) {
1145 for (i
= 0; i
< TGSI_TEXTURE_COUNT
; i
++) {
1146 if (str_match_nocase_whole(&cur
, tgsi_texture_names
[i
])) {
1147 decl
.SamplerView
.Resource
= i
;
1151 if (i
== TGSI_TEXTURE_COUNT
) {
1152 report_error(ctx
, "Expected texture target");
1155 eat_opt_white( &cur
);
1157 report_error( ctx
, "Expected `,'" );
1161 eat_opt_white( &cur
);
1162 for (j
= 0; j
< 4; ++j
) {
1163 for (i
= 0; i
< PIPE_TYPE_COUNT
; ++i
) {
1164 if (str_match_nocase_whole(&cur
, tgsi_type_names
[i
])) {
1167 decl
.SamplerView
.ReturnTypeX
= i
;
1170 decl
.SamplerView
.ReturnTypeY
= i
;
1173 decl
.SamplerView
.ReturnTypeZ
= i
;
1176 decl
.SamplerView
.ReturnTypeW
= i
;
1184 if (i
== PIPE_TYPE_COUNT
) {
1185 if (j
== 0 || j
> 2) {
1186 report_error(ctx
, "Expected type name");
1192 eat_opt_white( &cur2
);
1195 eat_opt_white( &cur2
);
1203 decl
.SamplerView
.ReturnTypeY
=
1204 decl
.SamplerView
.ReturnTypeZ
=
1205 decl
.SamplerView
.ReturnTypeW
=
1206 decl
.SamplerView
.ReturnTypeX
;
1210 if (str_match_nocase_whole(&cur
, "LOCAL")) {
1211 decl
.Declaration
.Local
= 1;
1216 eat_opt_white( &cur
);
1219 eat_opt_white( &cur
);
1221 for (i
= 0; i
< TGSI_SEMANTIC_COUNT
; i
++) {
1222 if (str_match_nocase_whole(&cur
, tgsi_semantic_names
[i
])) {
1226 eat_opt_white( &cur2
);
1229 eat_opt_white( &cur2
);
1230 if (!parse_uint( &cur2
, &index
)) {
1231 report_error( ctx
, "Expected literal integer" );
1234 eat_opt_white( &cur2
);
1236 report_error( ctx
, "Expected `]'" );
1241 decl
.Semantic
.Index
= index
;
1246 decl
.Declaration
.Semantic
= 1;
1247 decl
.Semantic
.Name
= i
;
1255 } else if (is_imm_array
) {
1257 union tgsi_immediate_data
*vals_itr
;
1258 /* we have our immediate data */
1260 report_error( ctx
, "Immediate array without data" );
1266 decl
.ImmediateData
.u
=
1267 MALLOC(sizeof(union tgsi_immediate_data
) * 4 *
1268 (decl
.Range
.Last
+ 1));
1269 vals_itr
= decl
.ImmediateData
.u
;
1270 for (i
= 0; i
<= decl
.Range
.Last
; ++i
) {
1271 if (!parse_immediate_data(ctx
, TGSI_IMM_FLOAT32
, vals_itr
)) {
1272 FREE(decl
.ImmediateData
.u
);
1276 eat_opt_white( &ctx
->cur
);
1277 if (*ctx
->cur
!= ',') {
1278 if (i
!= decl
.Range
.Last
) {
1279 report_error( ctx
, "Not enough data in immediate array!" );
1280 FREE(decl
.ImmediateData
.u
);
1286 eat_opt_white( &ctx
->cur
);
1287 if (*ctx
->cur
!= '}') {
1288 FREE(decl
.ImmediateData
.u
);
1289 report_error( ctx
, "Immediate array data missing closing '}'" );
1296 eat_opt_white( &cur
);
1297 if (*cur
== ',' && !is_vs_input
) {
1301 eat_opt_white( &cur
);
1302 for (i
= 0; i
< TGSI_INTERPOLATE_COUNT
; i
++) {
1303 if (str_match_nocase_whole( &cur
, tgsi_interpolate_names
[i
] )) {
1304 decl
.Declaration
.Interpolate
= 1;
1305 decl
.Interp
.Interpolate
= i
;
1311 if (i
== TGSI_INTERPOLATE_COUNT
) {
1312 report_error( ctx
, "Expected semantic or interpolate attribute" );
1317 advance
= tgsi_build_full_declaration(
1321 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1324 FREE(decl
.ImmediateData
.u
);
1328 ctx
->tokens_cur
+= advance
;
1333 static boolean
parse_immediate( struct translate_ctx
*ctx
)
1335 struct tgsi_full_immediate imm
;
1339 if (!eat_white( &ctx
->cur
)) {
1340 report_error( ctx
, "Syntax error" );
1343 for (type
= 0; type
< Elements(tgsi_immediate_type_names
); ++type
) {
1344 if (str_match_nocase_whole(&ctx
->cur
, tgsi_immediate_type_names
[type
]))
1347 if (type
== Elements(tgsi_immediate_type_names
)) {
1348 report_error( ctx
, "Expected immediate type" );
1352 imm
= tgsi_default_full_immediate();
1353 imm
.Immediate
.NrTokens
+= 4;
1354 imm
.Immediate
.DataType
= type
;
1355 parse_immediate_data(ctx
, type
, imm
.u
);
1357 advance
= tgsi_build_full_immediate(
1361 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1364 ctx
->tokens_cur
+= advance
;
1370 parse_primitive( const char **pcur
, uint
*primitive
)
1374 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++) {
1375 const char *cur
= *pcur
;
1377 if (str_match_nocase_whole( &cur
, tgsi_primitive_names
[i
])) {
1387 parse_fs_coord_origin( const char **pcur
, uint
*fs_coord_origin
)
1391 for (i
= 0; i
< Elements(tgsi_fs_coord_origin_names
); i
++) {
1392 const char *cur
= *pcur
;
1394 if (str_match_nocase_whole( &cur
, tgsi_fs_coord_origin_names
[i
])) {
1395 *fs_coord_origin
= i
;
1404 parse_fs_coord_pixel_center( const char **pcur
, uint
*fs_coord_pixel_center
)
1408 for (i
= 0; i
< Elements(tgsi_fs_coord_pixel_center_names
); i
++) {
1409 const char *cur
= *pcur
;
1411 if (str_match_nocase_whole( &cur
, tgsi_fs_coord_pixel_center_names
[i
])) {
1412 *fs_coord_pixel_center
= i
;
1421 static boolean
parse_property( struct translate_ctx
*ctx
)
1423 struct tgsi_full_property prop
;
1429 if (!eat_white( &ctx
->cur
)) {
1430 report_error( ctx
, "Syntax error" );
1433 if (!parse_identifier( &ctx
->cur
, id
)) {
1434 report_error( ctx
, "Syntax error" );
1437 for (property_name
= 0; property_name
< TGSI_PROPERTY_COUNT
;
1439 if (streq_nocase_uprcase(tgsi_property_names
[property_name
], id
)) {
1443 if (property_name
>= TGSI_PROPERTY_COUNT
) {
1444 debug_printf( "\nError: Unknown property : '%s'", id
);
1448 eat_opt_white( &ctx
->cur
);
1449 switch(property_name
) {
1450 case TGSI_PROPERTY_GS_INPUT_PRIM
:
1451 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
1452 if (!parse_primitive(&ctx
->cur
, &values
[0] )) {
1453 report_error( ctx
, "Unknown primitive name as property!" );
1456 if (property_name
== TGSI_PROPERTY_GS_INPUT_PRIM
&&
1457 ctx
->processor
== TGSI_PROCESSOR_GEOMETRY
) {
1458 ctx
->implied_array_size
= u_vertices_per_prim(values
[0]);
1461 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
1462 if (!parse_fs_coord_origin(&ctx
->cur
, &values
[0] )) {
1463 report_error( ctx
, "Unknown coord origin as property: must be UPPER_LEFT or LOWER_LEFT!" );
1467 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
1468 if (!parse_fs_coord_pixel_center(&ctx
->cur
, &values
[0] )) {
1469 report_error( ctx
, "Unknown coord pixel center as property: must be HALF_INTEGER or INTEGER!" );
1473 case TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
:
1475 if (!parse_uint(&ctx
->cur
, &values
[0] )) {
1476 report_error( ctx
, "Expected unsigned integer as property!" );
1481 prop
= tgsi_default_full_property();
1482 prop
.Property
.PropertyName
= property_name
;
1483 prop
.Property
.NrTokens
+= 1;
1484 prop
.u
[0].Data
= values
[0];
1486 advance
= tgsi_build_full_property(
1490 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1493 ctx
->tokens_cur
+= advance
;
1499 static boolean
translate( struct translate_ctx
*ctx
)
1501 eat_opt_white( &ctx
->cur
);
1502 if (!parse_header( ctx
))
1505 while (*ctx
->cur
!= '\0') {
1507 if (!eat_white( &ctx
->cur
)) {
1508 report_error( ctx
, "Syntax error" );
1512 if (*ctx
->cur
== '\0')
1514 if (parse_label( ctx
, &label_val
)) {
1515 if (!parse_instruction( ctx
, TRUE
))
1518 else if (str_match_nocase_whole( &ctx
->cur
, "DCL" )) {
1519 if (!parse_declaration( ctx
))
1522 else if (str_match_nocase_whole( &ctx
->cur
, "IMM" )) {
1523 if (!parse_immediate( ctx
))
1526 else if (str_match_nocase_whole( &ctx
->cur
, "PROPERTY" )) {
1527 if (!parse_property( ctx
))
1530 else if (!parse_instruction( ctx
, FALSE
)) {
1539 tgsi_text_translate(
1541 struct tgsi_token
*tokens
,
1544 struct translate_ctx ctx
;
1548 ctx
.tokens
= tokens
;
1549 ctx
.tokens_cur
= tokens
;
1550 ctx
.tokens_end
= tokens
+ num_tokens
;
1552 if (!translate( &ctx
))
1555 return tgsi_sanity_check( tokens
);