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_util.h"
40 static boolean
is_alpha_underscore( const char *cur
)
43 (*cur
>= 'a' && *cur
<= 'z') ||
44 (*cur
>= 'A' && *cur
<= 'Z') ||
48 static boolean
is_digit( const char *cur
)
50 return *cur
>= '0' && *cur
<= '9';
53 static boolean
is_digit_alpha_underscore( const char *cur
)
55 return is_digit( cur
) || is_alpha_underscore( cur
);
58 static char uprcase( char c
)
60 if (c
>= 'a' && c
<= 'z')
61 return c
+= 'A' - 'a';
66 * Ignore case of str1 and assume str1 is already uppercase.
67 * Return TRUE iff str1 and str2 are equal.
70 streq_nocase_uprcase(const char *str1
,
73 while (*str1
&& *str2
) {
74 if (*str1
!= uprcase(*str2
))
79 return *str1
== 0 && *str2
== 0;
82 static boolean
str_match_no_case( const char **pcur
, const char *str
)
84 const char *cur
= *pcur
;
86 while (*str
!= '\0' && *str
== uprcase( *cur
)) {
97 /* Eat zero or more whitespaces.
99 static void eat_opt_white( const char **pcur
)
101 while (**pcur
== ' ' || **pcur
== '\t' || **pcur
== '\n')
105 /* Eat one or more whitespaces.
106 * Return TRUE if at least one whitespace eaten.
108 static boolean
eat_white( const char **pcur
)
110 const char *cur
= *pcur
;
112 eat_opt_white( pcur
);
116 /* Parse unsigned integer.
117 * No checks for overflow.
119 static boolean
parse_uint( const char **pcur
, uint
*val
)
121 const char *cur
= *pcur
;
123 if (is_digit( cur
)) {
125 while (is_digit( cur
))
126 *val
= *val
* 10 + *cur
++ - '0';
133 static boolean
parse_identifier( const char **pcur
, char *ret
)
135 const char *cur
= *pcur
;
137 if (is_alpha_underscore( cur
)) {
139 while (is_alpha_underscore( cur
))
148 /* Parse floating point.
150 static boolean
parse_float( const char **pcur
, float *val
)
152 const char *cur
= *pcur
;
153 boolean integral_part
= FALSE
;
154 boolean fractional_part
= FALSE
;
156 *val
= (float) atof( cur
);
158 if (*cur
== '-' || *cur
== '+')
160 if (is_digit( cur
)) {
162 integral_part
= TRUE
;
163 while (is_digit( cur
))
168 if (is_digit( cur
)) {
170 fractional_part
= TRUE
;
171 while (is_digit( cur
))
175 if (!integral_part
&& !fractional_part
)
177 if (uprcase( *cur
) == 'E') {
179 if (*cur
== '-' || *cur
== '+')
181 if (is_digit( cur
)) {
183 while (is_digit( cur
))
197 struct tgsi_token
*tokens
;
198 struct tgsi_token
*tokens_cur
;
199 struct tgsi_token
*tokens_end
;
200 struct tgsi_header
*header
;
201 unsigned processor
: 4;
202 int implied_array_size
: 5;
205 static void report_error( struct translate_ctx
*ctx
, const char *msg
)
209 const char *itr
= ctx
->text
;
211 while (itr
!= ctx
->cur
) {
220 debug_printf( "\nTGSI asm error: %s [%d : %d] \n", msg
, line
, column
);
223 /* Parse shader header.
224 * Return TRUE for one of the following headers.
229 static boolean
parse_header( struct translate_ctx
*ctx
)
233 if (str_match_no_case( &ctx
->cur
, "FRAG" ))
234 processor
= TGSI_PROCESSOR_FRAGMENT
;
235 else if (str_match_no_case( &ctx
->cur
, "VERT" ))
236 processor
= TGSI_PROCESSOR_VERTEX
;
237 else if (str_match_no_case( &ctx
->cur
, "GEOM" ))
238 processor
= TGSI_PROCESSOR_GEOMETRY
;
240 report_error( ctx
, "Unknown header" );
244 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
246 ctx
->header
= (struct tgsi_header
*) ctx
->tokens_cur
++;
247 *ctx
->header
= tgsi_build_header();
249 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
251 *(struct tgsi_processor
*) ctx
->tokens_cur
++ = tgsi_build_processor( processor
, ctx
->header
);
252 ctx
->processor
= processor
;
257 static boolean
parse_label( struct translate_ctx
*ctx
, uint
*val
)
259 const char *cur
= ctx
->cur
;
261 if (parse_uint( &cur
, val
)) {
262 eat_opt_white( &cur
);
272 static const char *file_names
[TGSI_FILE_COUNT
] =
288 parse_file( const char **pcur
, uint
*file
)
292 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++) {
293 const char *cur
= *pcur
;
295 if (str_match_no_case( &cur
, file_names
[i
] )) {
296 if (!is_digit_alpha_underscore( cur
)) {
308 struct translate_ctx
*ctx
,
314 eat_opt_white( &cur
);
317 *writemask
= TGSI_WRITEMASK_NONE
;
318 eat_opt_white( &cur
);
319 if (uprcase( *cur
) == 'X') {
321 *writemask
|= TGSI_WRITEMASK_X
;
323 if (uprcase( *cur
) == 'Y') {
325 *writemask
|= TGSI_WRITEMASK_Y
;
327 if (uprcase( *cur
) == 'Z') {
329 *writemask
|= TGSI_WRITEMASK_Z
;
331 if (uprcase( *cur
) == 'W') {
333 *writemask
|= TGSI_WRITEMASK_W
;
336 if (*writemask
== TGSI_WRITEMASK_NONE
) {
337 report_error( ctx
, "Writemask expected" );
344 *writemask
= TGSI_WRITEMASK_XYZW
;
350 parse_register_dst( struct translate_ctx
*ctx
,
354 struct parsed_src_bracket
{
364 parse_register_src_bracket(
365 struct translate_ctx
*ctx
,
366 struct parsed_src_bracket
*brackets
)
371 memset(brackets
, 0, sizeof(struct parsed_src_bracket
));
373 eat_opt_white( &ctx
->cur
);
376 if (parse_file( &cur
, &brackets
->ind_file
)) {
377 if (!parse_register_dst( ctx
, &brackets
->ind_file
,
378 &brackets
->ind_index
))
380 eat_opt_white( &ctx
->cur
);
382 if (*ctx
->cur
== '.') {
384 eat_opt_white(&ctx
->cur
);
386 switch (uprcase(*ctx
->cur
)) {
388 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
391 brackets
->ind_comp
= TGSI_SWIZZLE_Y
;
394 brackets
->ind_comp
= TGSI_SWIZZLE_Z
;
397 brackets
->ind_comp
= TGSI_SWIZZLE_W
;
400 report_error(ctx
, "Expected indirect register swizzle component `x', `y', `z' or `w'");
404 eat_opt_white(&ctx
->cur
);
407 if (*ctx
->cur
== '+' || *ctx
->cur
== '-') {
410 negate
= *ctx
->cur
== '-';
412 eat_opt_white( &ctx
->cur
);
413 if (!parse_uint( &ctx
->cur
, &uindex
)) {
414 report_error( ctx
, "Expected literal unsigned integer" );
418 brackets
->index
= -(int) uindex
;
420 brackets
->index
= (int) uindex
;
427 if (!parse_uint( &ctx
->cur
, &uindex
)) {
428 report_error( ctx
, "Expected literal unsigned integer" );
431 brackets
->index
= (int) uindex
;
432 brackets
->ind_file
= TGSI_FILE_NULL
;
433 brackets
->ind_index
= 0;
435 eat_opt_white( &ctx
->cur
);
436 if (*ctx
->cur
!= ']') {
437 report_error( ctx
, "Expected `]'" );
445 parse_opt_register_src_bracket(
446 struct translate_ctx
*ctx
,
447 struct parsed_src_bracket
*brackets
,
448 int *parsed_brackets
)
450 const char *cur
= ctx
->cur
;
452 *parsed_brackets
= 0;
454 eat_opt_white( &cur
);
459 if (!parse_register_src_bracket(ctx
, brackets
))
462 *parsed_brackets
= 1;
468 /* <register_file_bracket> ::= <file> `['
471 parse_register_file_bracket(
472 struct translate_ctx
*ctx
,
475 if (!parse_file( &ctx
->cur
, file
)) {
476 report_error( ctx
, "Unknown register file" );
479 eat_opt_white( &ctx
->cur
);
480 if (*ctx
->cur
!= '[') {
481 report_error( ctx
, "Expected `['" );
488 /* <register_file_bracket_index> ::= <register_file_bracket> <uint>
491 parse_register_file_bracket_index(
492 struct translate_ctx
*ctx
,
498 if (!parse_register_file_bracket( ctx
, file
))
500 eat_opt_white( &ctx
->cur
);
501 if (!parse_uint( &ctx
->cur
, &uindex
)) {
502 report_error( ctx
, "Expected literal unsigned integer" );
505 *index
= (int) uindex
;
509 /* Parse source register operand.
510 * <register_src> ::= <register_file_bracket_index> `]' |
511 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `]' |
512 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `+' <uint> `]' |
513 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `-' <uint> `]'
517 struct translate_ctx
*ctx
,
519 struct parsed_src_bracket
*brackets
)
522 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
523 if (!parse_register_file_bracket( ctx
, file
))
525 if (!parse_register_src_bracket( ctx
, brackets
))
531 struct parsed_dcl_bracket
{
537 parse_register_dcl_bracket(
538 struct translate_ctx
*ctx
,
539 struct parsed_dcl_bracket
*bracket
)
542 memset(bracket
, 0, sizeof(struct parsed_dcl_bracket
));
544 eat_opt_white( &ctx
->cur
);
546 if (!parse_uint( &ctx
->cur
, &uindex
)) {
547 /* it can be an empty bracket [] which means its range
548 * is from 0 to some implied size */
549 if (ctx
->cur
[0] == ']' && ctx
->implied_array_size
!= 0) {
551 bracket
->last
= ctx
->implied_array_size
- 1;
554 report_error( ctx
, "Expected literal unsigned integer" );
557 bracket
->first
= uindex
;
559 eat_opt_white( &ctx
->cur
);
561 if (ctx
->cur
[0] == '.' && ctx
->cur
[1] == '.') {
565 eat_opt_white( &ctx
->cur
);
566 if (!parse_uint( &ctx
->cur
, &uindex
)) {
567 report_error( ctx
, "Expected literal integer" );
570 bracket
->last
= (int) uindex
;
571 eat_opt_white( &ctx
->cur
);
574 bracket
->last
= bracket
->first
;
578 if (*ctx
->cur
!= ']') {
579 report_error( ctx
, "Expected `]' or `..'" );
586 /* Parse register declaration.
587 * <register_dcl> ::= <register_file_bracket_index> `]' |
588 * <register_file_bracket_index> `..' <index> `]'
592 struct translate_ctx
*ctx
,
594 struct parsed_dcl_bracket
*brackets
,
601 if (!parse_register_file_bracket( ctx
, file
))
603 if (!parse_register_dcl_bracket( ctx
, &brackets
[0] ))
609 eat_opt_white( &cur
);
614 if (!parse_register_dcl_bracket( ctx
, &brackets
[1] ))
616 /* for geometry shader we don't really care about
617 * the first brackets it's always the size of the
618 * input primitive. so we want to declare just
619 * the index relevant to the semantics which is in
620 * the second bracket */
621 if (ctx
->processor
== TGSI_PROCESSOR_GEOMETRY
&& *file
== TGSI_FILE_INPUT
) {
622 brackets
[0] = brackets
[1];
633 /* Parse destination register operand.
634 * <register_dst> ::= <register_file_bracket_index> `]'
638 struct translate_ctx
*ctx
,
642 if (!parse_register_file_bracket_index( ctx
, file
, index
))
644 eat_opt_white( &ctx
->cur
);
645 if (*ctx
->cur
!= ']') {
646 report_error( ctx
, "Expected `]'" );
655 struct translate_ctx
*ctx
,
656 struct tgsi_full_dst_register
*dst
)
663 if (!parse_register_dst( ctx
, &file
, &index
))
667 eat_opt_white( &cur
);
669 if (!parse_opt_writemask( ctx
, &writemask
))
672 dst
->Register
.File
= file
;
673 dst
->Register
.Index
= index
;
674 dst
->Register
.WriteMask
= writemask
;
679 parse_optional_swizzle(
680 struct translate_ctx
*ctx
,
682 boolean
*parsed_swizzle
)
684 const char *cur
= ctx
->cur
;
686 *parsed_swizzle
= FALSE
;
688 eat_opt_white( &cur
);
693 eat_opt_white( &cur
);
694 for (i
= 0; i
< 4; i
++) {
695 if (uprcase( *cur
) == 'X')
696 swizzle
[i
] = TGSI_SWIZZLE_X
;
697 else if (uprcase( *cur
) == 'Y')
698 swizzle
[i
] = TGSI_SWIZZLE_Y
;
699 else if (uprcase( *cur
) == 'Z')
700 swizzle
[i
] = TGSI_SWIZZLE_Z
;
701 else if (uprcase( *cur
) == 'W')
702 swizzle
[i
] = TGSI_SWIZZLE_W
;
704 report_error( ctx
, "Expected register swizzle component `x', `y', `z', `w', `0' or `1'" );
709 *parsed_swizzle
= TRUE
;
717 struct translate_ctx
*ctx
,
718 struct tgsi_full_src_register
*src
)
722 boolean parsed_swizzle
;
723 struct parsed_src_bracket bracket
[2];
724 int parsed_opt_brackets
;
726 if (*ctx
->cur
== '-') {
728 eat_opt_white( &ctx
->cur
);
729 src
->Register
.Negate
= 1;
732 if (*ctx
->cur
== '|') {
734 eat_opt_white( &ctx
->cur
);
735 src
->Register
.Absolute
= 1;
738 if (!parse_register_src(ctx
, &file
, &bracket
[0]))
740 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
743 src
->Register
.File
= file
;
744 if (parsed_opt_brackets
) {
745 src
->Register
.Dimension
= 1;
746 src
->Dimension
.Indirect
= 0;
747 src
->Dimension
.Dimension
= 0;
748 src
->Dimension
.Index
= bracket
[0].index
;
749 bracket
[0] = bracket
[1];
751 src
->Register
.Index
= bracket
[0].index
;
752 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
753 src
->Register
.Indirect
= 1;
754 src
->Indirect
.File
= bracket
[0].ind_file
;
755 src
->Indirect
.Index
= bracket
[0].ind_index
;
756 src
->Indirect
.SwizzleX
= bracket
[0].ind_comp
;
757 src
->Indirect
.SwizzleY
= bracket
[0].ind_comp
;
758 src
->Indirect
.SwizzleZ
= bracket
[0].ind_comp
;
759 src
->Indirect
.SwizzleW
= bracket
[0].ind_comp
;
762 /* Parse optional swizzle.
764 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
)) {
765 if (parsed_swizzle
) {
766 src
->Register
.SwizzleX
= swizzle
[0];
767 src
->Register
.SwizzleY
= swizzle
[1];
768 src
->Register
.SwizzleZ
= swizzle
[2];
769 src
->Register
.SwizzleW
= swizzle
[3];
773 if (src
->Register
.Absolute
) {
774 eat_opt_white( &ctx
->cur
);
775 if (*ctx
->cur
!= '|') {
776 report_error( ctx
, "Expected `|'" );
786 static const char *texture_names
[TGSI_TEXTURE_COUNT
] =
800 match_inst_mnemonic(const char **pcur
,
801 const struct tgsi_opcode_info
*info
)
803 if (str_match_no_case(pcur
, info
->mnemonic
)) {
811 struct translate_ctx
*ctx
,
815 uint saturate
= TGSI_SAT_NONE
;
816 const struct tgsi_opcode_info
*info
;
817 struct tgsi_full_instruction inst
;
820 inst
= tgsi_default_full_instruction();
824 eat_opt_white( &ctx
->cur
);
825 if (*ctx
->cur
== '(') {
829 boolean parsed_swizzle
;
831 inst
.Instruction
.Predicate
= 1;
834 if (*ctx
->cur
== '!') {
836 inst
.Predicate
.Negate
= 1;
839 if (!parse_register_dst( ctx
, &file
, &index
))
842 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
)) {
843 if (parsed_swizzle
) {
844 inst
.Predicate
.SwizzleX
= swizzle
[0];
845 inst
.Predicate
.SwizzleY
= swizzle
[1];
846 inst
.Predicate
.SwizzleZ
= swizzle
[2];
847 inst
.Predicate
.SwizzleW
= swizzle
[3];
851 if (*ctx
->cur
!= ')') {
852 report_error( ctx
, "Expected `)'" );
859 /* Parse instruction name.
861 eat_opt_white( &ctx
->cur
);
862 for (i
= 0; i
< TGSI_OPCODE_LAST
; i
++) {
863 const char *cur
= ctx
->cur
;
865 info
= tgsi_get_opcode_info( i
);
866 if (match_inst_mnemonic(&cur
, info
)) {
867 if (str_match_no_case( &cur
, "_SATNV" ))
868 saturate
= TGSI_SAT_MINUS_PLUS_ONE
;
869 else if (str_match_no_case( &cur
, "_SAT" ))
870 saturate
= TGSI_SAT_ZERO_ONE
;
872 if (info
->num_dst
+ info
->num_src
+ info
->is_tex
== 0) {
873 if (!is_digit_alpha_underscore( cur
)) {
878 else if (*cur
== '\0' || eat_white( &cur
)) {
884 if (i
== TGSI_OPCODE_LAST
) {
886 report_error( ctx
, "Unknown opcode" );
888 report_error( ctx
, "Expected `DCL', `IMM' or a label" );
892 inst
.Instruction
.Opcode
= i
;
893 inst
.Instruction
.Saturate
= saturate
;
894 inst
.Instruction
.NumDstRegs
= info
->num_dst
;
895 inst
.Instruction
.NumSrcRegs
= info
->num_src
;
897 /* Parse instruction operands.
899 for (i
= 0; i
< info
->num_dst
+ info
->num_src
+ info
->is_tex
; i
++) {
901 eat_opt_white( &ctx
->cur
);
902 if (*ctx
->cur
!= ',') {
903 report_error( ctx
, "Expected `,'" );
907 eat_opt_white( &ctx
->cur
);
910 if (i
< info
->num_dst
) {
911 if (!parse_dst_operand( ctx
, &inst
.Dst
[i
] ))
914 else if (i
< info
->num_dst
+ info
->num_src
) {
915 if (!parse_src_operand( ctx
, &inst
.Src
[i
- info
->num_dst
] ))
921 for (j
= 0; j
< TGSI_TEXTURE_COUNT
; j
++) {
922 if (str_match_no_case( &ctx
->cur
, texture_names
[j
] )) {
923 if (!is_digit_alpha_underscore( ctx
->cur
)) {
924 inst
.Instruction
.Texture
= 1;
925 inst
.Texture
.Texture
= j
;
930 if (j
== TGSI_TEXTURE_COUNT
) {
931 report_error( ctx
, "Expected texture target" );
937 if (info
->is_branch
) {
940 eat_opt_white( &ctx
->cur
);
941 if (*ctx
->cur
!= ':') {
942 report_error( ctx
, "Expected `:'" );
946 eat_opt_white( &ctx
->cur
);
947 if (!parse_uint( &ctx
->cur
, &target
)) {
948 report_error( ctx
, "Expected a label" );
951 inst
.Instruction
.Label
= 1;
952 inst
.Label
.Label
= target
;
955 advance
= tgsi_build_full_instruction(
959 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
962 ctx
->tokens_cur
+= advance
;
967 static const char *semantic_names
[TGSI_SEMANTIC_COUNT
] =
982 static const char *interpolate_names
[TGSI_INTERPOLATE_COUNT
] =
990 /* parses a 4-touple of the form {x, y, z, w}
991 * where x, y, z, w are numbers */
992 static boolean
parse_immediate_data(struct translate_ctx
*ctx
,
997 eat_opt_white( &ctx
->cur
);
998 if (*ctx
->cur
!= '{') {
999 report_error( ctx
, "Expected `{'" );
1003 for (i
= 0; i
< 4; i
++) {
1004 eat_opt_white( &ctx
->cur
);
1006 if (*ctx
->cur
!= ',') {
1007 report_error( ctx
, "Expected `,'" );
1011 eat_opt_white( &ctx
->cur
);
1013 if (!parse_float( &ctx
->cur
, &values
[i
] )) {
1014 report_error( ctx
, "Expected literal floating point" );
1018 eat_opt_white( &ctx
->cur
);
1019 if (*ctx
->cur
!= '}') {
1020 report_error( ctx
, "Expected `}'" );
1028 static boolean
parse_declaration( struct translate_ctx
*ctx
)
1030 struct tgsi_full_declaration decl
;
1032 struct parsed_dcl_bracket brackets
[2];
1037 boolean is_vs_input
;
1038 boolean is_imm_array
;
1040 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
1041 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
1043 if (!eat_white( &ctx
->cur
)) {
1044 report_error( ctx
, "Syntax error" );
1047 if (!parse_register_dcl( ctx
, &file
, brackets
, &num_brackets
))
1049 if (!parse_opt_writemask( ctx
, &writemask
))
1052 decl
= tgsi_default_full_declaration();
1053 decl
.Declaration
.File
= file
;
1054 decl
.Declaration
.UsageMask
= writemask
;
1056 if (num_brackets
== 1) {
1057 decl
.Range
.First
= brackets
[0].first
;
1058 decl
.Range
.Last
= brackets
[0].last
;
1060 decl
.Range
.First
= brackets
[1].first
;
1061 decl
.Range
.Last
= brackets
[1].last
;
1063 decl
.Declaration
.Dimension
= 1;
1064 decl
.Dim
.Index2D
= brackets
[0].first
;
1067 is_vs_input
= (file
== TGSI_FILE_INPUT
&&
1068 ctx
->processor
== TGSI_PROCESSOR_VERTEX
);
1069 is_imm_array
= (file
== TGSI_FILE_IMMEDIATE_ARRAY
);
1072 eat_opt_white( &cur
);
1073 if (*cur
== ',' && !is_vs_input
) {
1077 eat_opt_white( &cur
);
1078 for (i
= 0; i
< TGSI_SEMANTIC_COUNT
; i
++) {
1079 if (str_match_no_case( &cur
, semantic_names
[i
] )) {
1080 const char *cur2
= cur
;
1083 if (is_digit_alpha_underscore( cur
))
1085 eat_opt_white( &cur2
);
1088 eat_opt_white( &cur2
);
1089 if (!parse_uint( &cur2
, &index
)) {
1090 report_error( ctx
, "Expected literal integer" );
1093 eat_opt_white( &cur2
);
1095 report_error( ctx
, "Expected `]'" );
1100 decl
.Semantic
.Index
= index
;
1105 decl
.Declaration
.Semantic
= 1;
1106 decl
.Semantic
.Name
= i
;
1112 } else if (is_imm_array
) {
1115 /* we have our immediate data */
1117 report_error( ctx
, "Immediate array without data" );
1123 decl
.ImmediateData
.u
=
1124 MALLOC(sizeof(union tgsi_immediate_data
) * 4 *
1125 (decl
.Range
.Last
+ 1));
1126 vals_itr
= (float*)decl
.ImmediateData
.u
;
1127 for (i
= 0; i
<= decl
.Range
.Last
; ++i
) {
1128 if (!parse_immediate_data(ctx
, vals_itr
)) {
1129 FREE(decl
.ImmediateData
.u
);
1133 eat_opt_white( &ctx
->cur
);
1134 if (*ctx
->cur
!= ',') {
1135 if (i
!= decl
.Range
.Last
) {
1136 report_error( ctx
, "Not enough data in immediate array!" );
1137 FREE(decl
.ImmediateData
.u
);
1143 eat_opt_white( &ctx
->cur
);
1144 if (*ctx
->cur
!= '}') {
1145 FREE(decl
.ImmediateData
.u
);
1146 report_error( ctx
, "Immediate array data missing closing '}'" );
1153 eat_opt_white( &cur
);
1154 if (*cur
== ',' && !is_vs_input
) {
1158 eat_opt_white( &cur
);
1159 for (i
= 0; i
< TGSI_INTERPOLATE_COUNT
; i
++) {
1160 if (str_match_no_case( &cur
, interpolate_names
[i
] )) {
1161 if (is_digit_alpha_underscore( cur
))
1163 decl
.Declaration
.Interpolate
= i
;
1169 if (i
== TGSI_INTERPOLATE_COUNT
) {
1170 report_error( ctx
, "Expected semantic or interpolate attribute" );
1175 advance
= tgsi_build_full_declaration(
1179 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1182 FREE(decl
.ImmediateData
.u
);
1186 ctx
->tokens_cur
+= advance
;
1191 static boolean
parse_immediate( struct translate_ctx
*ctx
)
1193 struct tgsi_full_immediate imm
;
1197 if (!eat_white( &ctx
->cur
)) {
1198 report_error( ctx
, "Syntax error" );
1201 if (!str_match_no_case( &ctx
->cur
, "FLT32" ) ||
1202 is_digit_alpha_underscore( ctx
->cur
)) {
1203 report_error( ctx
, "Expected `FLT32'" );
1207 parse_immediate_data(ctx
, values
);
1209 imm
= tgsi_default_full_immediate();
1210 imm
.Immediate
.NrTokens
+= 4;
1211 imm
.Immediate
.DataType
= TGSI_IMM_FLOAT32
;
1212 imm
.u
[0].Float
= values
[0];
1213 imm
.u
[1].Float
= values
[1];
1214 imm
.u
[2].Float
= values
[2];
1215 imm
.u
[3].Float
= values
[3];
1217 advance
= tgsi_build_full_immediate(
1221 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1224 ctx
->tokens_cur
+= advance
;
1229 static const char *property_names
[] =
1231 "GS_INPUT_PRIMITIVE",
1232 "GS_OUTPUT_PRIMITIVE",
1233 "GS_MAX_OUTPUT_VERTICES",
1235 "FS_COORD_PIXEL_CENTER"
1238 static const char *primitive_names
[] =
1252 static const char *fs_coord_origin_names
[] =
1258 static const char *fs_coord_pixel_center_names
[] =
1266 parse_primitive( const char **pcur
, uint
*primitive
)
1270 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++) {
1271 const char *cur
= *pcur
;
1273 if (str_match_no_case( &cur
, primitive_names
[i
])) {
1283 parse_fs_coord_origin( const char **pcur
, uint
*fs_coord_origin
)
1287 for (i
= 0; i
< sizeof(fs_coord_origin_names
) / sizeof(fs_coord_origin_names
[0]); i
++) {
1288 const char *cur
= *pcur
;
1290 if (str_match_no_case( &cur
, fs_coord_origin_names
[i
])) {
1291 *fs_coord_origin
= i
;
1300 parse_fs_coord_pixel_center( const char **pcur
, uint
*fs_coord_pixel_center
)
1304 for (i
= 0; i
< sizeof(fs_coord_pixel_center_names
) / sizeof(fs_coord_pixel_center_names
[0]); i
++) {
1305 const char *cur
= *pcur
;
1307 if (str_match_no_case( &cur
, fs_coord_pixel_center_names
[i
])) {
1308 *fs_coord_pixel_center
= i
;
1317 static boolean
parse_property( struct translate_ctx
*ctx
)
1319 struct tgsi_full_property prop
;
1325 if (!eat_white( &ctx
->cur
)) {
1326 report_error( ctx
, "Syntax error" );
1329 if (!parse_identifier( &ctx
->cur
, id
)) {
1330 report_error( ctx
, "Syntax error" );
1333 for (property_name
= 0; property_name
< TGSI_PROPERTY_COUNT
;
1335 if (streq_nocase_uprcase(property_names
[property_name
], id
)) {
1339 if (property_name
>= TGSI_PROPERTY_COUNT
) {
1340 debug_printf( "\nError: Unknown property : '%s'", id
);
1344 eat_opt_white( &ctx
->cur
);
1345 switch(property_name
) {
1346 case TGSI_PROPERTY_GS_INPUT_PRIM
:
1347 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
1348 if (!parse_primitive(&ctx
->cur
, &values
[0] )) {
1349 report_error( ctx
, "Unknown primitive name as property!" );
1352 if (property_name
== TGSI_PROPERTY_GS_INPUT_PRIM
&&
1353 ctx
->processor
== TGSI_PROCESSOR_GEOMETRY
) {
1354 ctx
->implied_array_size
= u_vertices_per_prim(values
[0]);
1357 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
1358 if (!parse_fs_coord_origin(&ctx
->cur
, &values
[0] )) {
1359 report_error( ctx
, "Unknown coord origin as property: must be UPPER_LEFT or LOWER_LEFT!" );
1363 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
1364 if (!parse_fs_coord_pixel_center(&ctx
->cur
, &values
[0] )) {
1365 report_error( ctx
, "Unknown coord pixel center as property: must be HALF_INTEGER or INTEGER!" );
1370 if (!parse_uint(&ctx
->cur
, &values
[0] )) {
1371 report_error( ctx
, "Expected unsigned integer as property!" );
1376 prop
= tgsi_default_full_property();
1377 prop
.Property
.PropertyName
= property_name
;
1378 prop
.Property
.NrTokens
+= 1;
1379 prop
.u
[0].Data
= values
[0];
1381 advance
= tgsi_build_full_property(
1385 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1388 ctx
->tokens_cur
+= advance
;
1394 static boolean
translate( struct translate_ctx
*ctx
)
1396 eat_opt_white( &ctx
->cur
);
1397 if (!parse_header( ctx
))
1400 while (*ctx
->cur
!= '\0') {
1402 if (!eat_white( &ctx
->cur
)) {
1403 report_error( ctx
, "Syntax error" );
1407 if (*ctx
->cur
== '\0')
1409 if (parse_label( ctx
, &label_val
)) {
1410 if (!parse_instruction( ctx
, TRUE
))
1413 else if (str_match_no_case( &ctx
->cur
, "DCL" )) {
1414 if (!parse_declaration( ctx
))
1417 else if (str_match_no_case( &ctx
->cur
, "IMM" )) {
1418 if (!parse_immediate( ctx
))
1421 else if (str_match_no_case( &ctx
->cur
, "PROPERTY" )) {
1422 if (!parse_property( ctx
))
1425 else if (!parse_instruction( ctx
, FALSE
)) {
1434 tgsi_text_translate(
1436 struct tgsi_token
*tokens
,
1439 struct translate_ctx ctx
;
1443 ctx
.tokens
= tokens
;
1444 ctx
.tokens_cur
= tokens
;
1445 ctx
.tokens_end
= tokens
+ num_tokens
;
1447 if (!translate( &ctx
))
1450 return tgsi_sanity_check( tokens
);