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
))
147 /* Parse floating point.
149 static boolean
parse_float( const char **pcur
, float *val
)
151 const char *cur
= *pcur
;
152 boolean integral_part
= FALSE
;
153 boolean fractional_part
= FALSE
;
155 *val
= (float) atof( cur
);
157 if (*cur
== '-' || *cur
== '+')
159 if (is_digit( cur
)) {
161 integral_part
= TRUE
;
162 while (is_digit( cur
))
167 if (is_digit( cur
)) {
169 fractional_part
= TRUE
;
170 while (is_digit( cur
))
174 if (!integral_part
&& !fractional_part
)
176 if (uprcase( *cur
) == 'E') {
178 if (*cur
== '-' || *cur
== '+')
180 if (is_digit( cur
)) {
182 while (is_digit( cur
))
196 struct tgsi_token
*tokens
;
197 struct tgsi_token
*tokens_cur
;
198 struct tgsi_token
*tokens_end
;
199 struct tgsi_header
*header
;
200 unsigned processor
: 4;
201 int implied_array_size
: 5;
204 static void report_error( struct translate_ctx
*ctx
, const char *msg
)
208 const char *itr
= ctx
->text
;
210 while (itr
!= ctx
->cur
) {
219 debug_printf( "\nTGSI asm error: %s [%d : %d] \n", msg
, line
, column
);
222 /* Parse shader header.
223 * Return TRUE for one of the following headers.
228 static boolean
parse_header( struct translate_ctx
*ctx
)
232 if (str_match_no_case( &ctx
->cur
, "FRAG" ))
233 processor
= TGSI_PROCESSOR_FRAGMENT
;
234 else if (str_match_no_case( &ctx
->cur
, "VERT" ))
235 processor
= TGSI_PROCESSOR_VERTEX
;
236 else if (str_match_no_case( &ctx
->cur
, "GEOM" ))
237 processor
= TGSI_PROCESSOR_GEOMETRY
;
239 report_error( ctx
, "Unknown header" );
243 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
245 ctx
->header
= (struct tgsi_header
*) ctx
->tokens_cur
++;
246 *ctx
->header
= tgsi_build_header();
248 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
250 *(struct tgsi_processor
*) ctx
->tokens_cur
++ = tgsi_build_processor( processor
, ctx
->header
);
251 ctx
->processor
= processor
;
256 static boolean
parse_label( struct translate_ctx
*ctx
, uint
*val
)
258 const char *cur
= ctx
->cur
;
260 if (parse_uint( &cur
, val
)) {
261 eat_opt_white( &cur
);
271 static const char *file_names
[TGSI_FILE_COUNT
] =
286 parse_file( const char **pcur
, uint
*file
)
290 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++) {
291 const char *cur
= *pcur
;
293 if (str_match_no_case( &cur
, file_names
[i
] )) {
294 if (!is_digit_alpha_underscore( cur
)) {
306 struct translate_ctx
*ctx
,
312 eat_opt_white( &cur
);
315 *writemask
= TGSI_WRITEMASK_NONE
;
316 eat_opt_white( &cur
);
317 if (uprcase( *cur
) == 'X') {
319 *writemask
|= TGSI_WRITEMASK_X
;
321 if (uprcase( *cur
) == 'Y') {
323 *writemask
|= TGSI_WRITEMASK_Y
;
325 if (uprcase( *cur
) == 'Z') {
327 *writemask
|= TGSI_WRITEMASK_Z
;
329 if (uprcase( *cur
) == 'W') {
331 *writemask
|= TGSI_WRITEMASK_W
;
334 if (*writemask
== TGSI_WRITEMASK_NONE
) {
335 report_error( ctx
, "Writemask expected" );
342 *writemask
= TGSI_WRITEMASK_XYZW
;
348 parse_register_dst( struct translate_ctx
*ctx
,
352 struct parsed_src_bracket
{
362 parse_register_src_bracket(
363 struct translate_ctx
*ctx
,
364 struct parsed_src_bracket
*brackets
)
369 memset(brackets
, 0, sizeof(struct parsed_src_bracket
));
371 eat_opt_white( &ctx
->cur
);
374 if (parse_file( &cur
, &brackets
->ind_file
)) {
375 if (!parse_register_dst( ctx
, &brackets
->ind_file
,
376 &brackets
->ind_index
))
378 eat_opt_white( &ctx
->cur
);
380 if (*ctx
->cur
== '.') {
382 eat_opt_white(&ctx
->cur
);
384 switch (uprcase(*ctx
->cur
)) {
386 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
389 brackets
->ind_comp
= TGSI_SWIZZLE_Y
;
392 brackets
->ind_comp
= TGSI_SWIZZLE_Z
;
395 brackets
->ind_comp
= TGSI_SWIZZLE_W
;
398 report_error(ctx
, "Expected indirect register swizzle component `x', `y', `z' or `w'");
402 eat_opt_white(&ctx
->cur
);
405 if (*ctx
->cur
== '+' || *ctx
->cur
== '-') {
408 negate
= *ctx
->cur
== '-';
410 eat_opt_white( &ctx
->cur
);
411 if (!parse_uint( &ctx
->cur
, &uindex
)) {
412 report_error( ctx
, "Expected literal unsigned integer" );
416 brackets
->index
= -(int) uindex
;
418 brackets
->index
= (int) uindex
;
425 if (!parse_uint( &ctx
->cur
, &uindex
)) {
426 report_error( ctx
, "Expected literal unsigned integer" );
429 brackets
->index
= (int) uindex
;
430 brackets
->ind_file
= TGSI_FILE_NULL
;
431 brackets
->ind_index
= 0;
433 eat_opt_white( &ctx
->cur
);
434 if (*ctx
->cur
!= ']') {
435 report_error( ctx
, "Expected `]'" );
443 parse_opt_register_src_bracket(
444 struct translate_ctx
*ctx
,
445 struct parsed_src_bracket
*brackets
,
446 int *parsed_brackets
)
448 const char *cur
= ctx
->cur
;
450 *parsed_brackets
= 0;
452 eat_opt_white( &cur
);
457 if (!parse_register_src_bracket(ctx
, brackets
))
460 *parsed_brackets
= 1;
466 /* <register_file_bracket> ::= <file> `['
469 parse_register_file_bracket(
470 struct translate_ctx
*ctx
,
473 if (!parse_file( &ctx
->cur
, file
)) {
474 report_error( ctx
, "Unknown register file" );
477 eat_opt_white( &ctx
->cur
);
478 if (*ctx
->cur
!= '[') {
479 report_error( ctx
, "Expected `['" );
486 /* <register_file_bracket_index> ::= <register_file_bracket> <uint>
489 parse_register_file_bracket_index(
490 struct translate_ctx
*ctx
,
496 if (!parse_register_file_bracket( ctx
, file
))
498 eat_opt_white( &ctx
->cur
);
499 if (!parse_uint( &ctx
->cur
, &uindex
)) {
500 report_error( ctx
, "Expected literal unsigned integer" );
503 *index
= (int) uindex
;
507 /* Parse source register operand.
508 * <register_src> ::= <register_file_bracket_index> `]' |
509 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `]' |
510 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `+' <uint> `]' |
511 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `-' <uint> `]'
515 struct translate_ctx
*ctx
,
517 struct parsed_src_bracket
*brackets
)
520 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
521 if (!parse_register_file_bracket( ctx
, file
))
523 if (!parse_register_src_bracket( ctx
, brackets
))
529 struct parsed_dcl_bracket
{
535 parse_register_dcl_bracket(
536 struct translate_ctx
*ctx
,
537 struct parsed_dcl_bracket
*bracket
)
540 memset(bracket
, 0, sizeof(struct parsed_dcl_bracket
));
542 eat_opt_white( &ctx
->cur
);
544 if (!parse_uint( &ctx
->cur
, &uindex
)) {
545 /* it can be an empty bracket [] which means its range
546 * is from 0 to some implied size */
547 if (ctx
->cur
[0] == ']' && ctx
->implied_array_size
!= 0) {
549 bracket
->last
= ctx
->implied_array_size
- 1;
552 report_error( ctx
, "Expected literal unsigned integer" );
555 bracket
->first
= uindex
;
557 eat_opt_white( &ctx
->cur
);
559 if (ctx
->cur
[0] == '.' && ctx
->cur
[1] == '.') {
563 eat_opt_white( &ctx
->cur
);
564 if (!parse_uint( &ctx
->cur
, &uindex
)) {
565 report_error( ctx
, "Expected literal integer" );
568 bracket
->last
= (int) uindex
;
569 eat_opt_white( &ctx
->cur
);
572 bracket
->last
= bracket
->first
;
576 if (*ctx
->cur
!= ']') {
577 report_error( ctx
, "Expected `]' or `..'" );
584 /* Parse register declaration.
585 * <register_dcl> ::= <register_file_bracket_index> `]' |
586 * <register_file_bracket_index> `..' <index> `]'
590 struct translate_ctx
*ctx
,
592 struct parsed_dcl_bracket
*brackets
,
599 if (!parse_register_file_bracket( ctx
, file
))
601 if (!parse_register_dcl_bracket( ctx
, &brackets
[0] ))
607 eat_opt_white( &cur
);
612 if (!parse_register_dcl_bracket( ctx
, &brackets
[1] ))
614 /* for geometry shader we don't really care about
615 * the first brackets it's always the size of the
616 * input primitive. so we want to declare just
617 * the index relevant to the semantics which is in
618 * the second bracket */
619 if (ctx
->processor
== TGSI_PROCESSOR_GEOMETRY
&& *file
== TGSI_FILE_INPUT
) {
620 brackets
[0] = brackets
[1];
631 /* Parse destination register operand.
632 * <register_dst> ::= <register_file_bracket_index> `]'
636 struct translate_ctx
*ctx
,
640 if (!parse_register_file_bracket_index( ctx
, file
, index
))
642 eat_opt_white( &ctx
->cur
);
643 if (*ctx
->cur
!= ']') {
644 report_error( ctx
, "Expected `]'" );
653 struct translate_ctx
*ctx
,
654 struct tgsi_full_dst_register
*dst
)
661 if (!parse_register_dst( ctx
, &file
, &index
))
665 eat_opt_white( &cur
);
667 if (!parse_opt_writemask( ctx
, &writemask
))
670 dst
->Register
.File
= file
;
671 dst
->Register
.Index
= index
;
672 dst
->Register
.WriteMask
= writemask
;
677 parse_optional_swizzle(
678 struct translate_ctx
*ctx
,
680 boolean
*parsed_swizzle
)
682 const char *cur
= ctx
->cur
;
684 *parsed_swizzle
= FALSE
;
686 eat_opt_white( &cur
);
691 eat_opt_white( &cur
);
692 for (i
= 0; i
< 4; i
++) {
693 if (uprcase( *cur
) == 'X')
694 swizzle
[i
] = TGSI_SWIZZLE_X
;
695 else if (uprcase( *cur
) == 'Y')
696 swizzle
[i
] = TGSI_SWIZZLE_Y
;
697 else if (uprcase( *cur
) == 'Z')
698 swizzle
[i
] = TGSI_SWIZZLE_Z
;
699 else if (uprcase( *cur
) == 'W')
700 swizzle
[i
] = TGSI_SWIZZLE_W
;
702 report_error( ctx
, "Expected register swizzle component `x', `y', `z', `w', `0' or `1'" );
707 *parsed_swizzle
= TRUE
;
715 struct translate_ctx
*ctx
,
716 struct tgsi_full_src_register
*src
)
720 boolean parsed_swizzle
;
721 struct parsed_src_bracket bracket
[2];
722 int parsed_opt_brackets
;
724 if (*ctx
->cur
== '-') {
726 eat_opt_white( &ctx
->cur
);
727 src
->Register
.Negate
= 1;
730 if (*ctx
->cur
== '|') {
732 eat_opt_white( &ctx
->cur
);
733 src
->Register
.Absolute
= 1;
736 if (!parse_register_src(ctx
, &file
, &bracket
[0]))
738 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
741 src
->Register
.File
= file
;
742 if (parsed_opt_brackets
) {
743 src
->Register
.Dimension
= 1;
744 src
->Dimension
.Indirect
= 0;
745 src
->Dimension
.Dimension
= 0;
746 src
->Dimension
.Index
= bracket
[0].index
;
747 bracket
[0] = bracket
[1];
749 src
->Register
.Index
= bracket
[0].index
;
750 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
751 src
->Register
.Indirect
= 1;
752 src
->Indirect
.File
= bracket
[0].ind_file
;
753 src
->Indirect
.Index
= bracket
[0].ind_index
;
754 src
->Indirect
.SwizzleX
= bracket
[0].ind_comp
;
755 src
->Indirect
.SwizzleY
= bracket
[0].ind_comp
;
756 src
->Indirect
.SwizzleZ
= bracket
[0].ind_comp
;
757 src
->Indirect
.SwizzleW
= bracket
[0].ind_comp
;
760 /* Parse optional swizzle.
762 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
)) {
763 if (parsed_swizzle
) {
764 src
->Register
.SwizzleX
= swizzle
[0];
765 src
->Register
.SwizzleY
= swizzle
[1];
766 src
->Register
.SwizzleZ
= swizzle
[2];
767 src
->Register
.SwizzleW
= swizzle
[3];
771 if (src
->Register
.Absolute
) {
772 eat_opt_white( &ctx
->cur
);
773 if (*ctx
->cur
!= '|') {
774 report_error( ctx
, "Expected `|'" );
784 static const char *texture_names
[TGSI_TEXTURE_COUNT
] =
798 match_inst_mnemonic(const char **pcur
,
799 const struct tgsi_opcode_info
*info
)
801 if (str_match_no_case(pcur
, info
->mnemonic
)) {
809 struct translate_ctx
*ctx
,
813 uint saturate
= TGSI_SAT_NONE
;
814 const struct tgsi_opcode_info
*info
;
815 struct tgsi_full_instruction inst
;
818 inst
= tgsi_default_full_instruction();
822 eat_opt_white( &ctx
->cur
);
823 if (*ctx
->cur
== '(') {
827 boolean parsed_swizzle
;
829 inst
.Instruction
.Predicate
= 1;
832 if (*ctx
->cur
== '!') {
834 inst
.Predicate
.Negate
= 1;
837 if (!parse_register_dst( ctx
, &file
, &index
))
840 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
)) {
841 if (parsed_swizzle
) {
842 inst
.Predicate
.SwizzleX
= swizzle
[0];
843 inst
.Predicate
.SwizzleY
= swizzle
[1];
844 inst
.Predicate
.SwizzleZ
= swizzle
[2];
845 inst
.Predicate
.SwizzleW
= swizzle
[3];
849 if (*ctx
->cur
!= ')') {
850 report_error( ctx
, "Expected `)'" );
857 /* Parse instruction name.
859 eat_opt_white( &ctx
->cur
);
860 for (i
= 0; i
< TGSI_OPCODE_LAST
; i
++) {
861 const char *cur
= ctx
->cur
;
863 info
= tgsi_get_opcode_info( i
);
864 if (match_inst_mnemonic(&cur
, info
)) {
865 if (str_match_no_case( &cur
, "_SATNV" ))
866 saturate
= TGSI_SAT_MINUS_PLUS_ONE
;
867 else if (str_match_no_case( &cur
, "_SAT" ))
868 saturate
= TGSI_SAT_ZERO_ONE
;
870 if (info
->num_dst
+ info
->num_src
+ info
->is_tex
== 0) {
871 if (!is_digit_alpha_underscore( cur
)) {
876 else if (*cur
== '\0' || eat_white( &cur
)) {
882 if (i
== TGSI_OPCODE_LAST
) {
884 report_error( ctx
, "Unknown opcode" );
886 report_error( ctx
, "Expected `DCL', `IMM' or a label" );
890 inst
.Instruction
.Opcode
= i
;
891 inst
.Instruction
.Saturate
= saturate
;
892 inst
.Instruction
.NumDstRegs
= info
->num_dst
;
893 inst
.Instruction
.NumSrcRegs
= info
->num_src
;
895 /* Parse instruction operands.
897 for (i
= 0; i
< info
->num_dst
+ info
->num_src
+ info
->is_tex
; i
++) {
899 eat_opt_white( &ctx
->cur
);
900 if (*ctx
->cur
!= ',') {
901 report_error( ctx
, "Expected `,'" );
905 eat_opt_white( &ctx
->cur
);
908 if (i
< info
->num_dst
) {
909 if (!parse_dst_operand( ctx
, &inst
.Dst
[i
] ))
912 else if (i
< info
->num_dst
+ info
->num_src
) {
913 if (!parse_src_operand( ctx
, &inst
.Src
[i
- info
->num_dst
] ))
919 for (j
= 0; j
< TGSI_TEXTURE_COUNT
; j
++) {
920 if (str_match_no_case( &ctx
->cur
, texture_names
[j
] )) {
921 if (!is_digit_alpha_underscore( ctx
->cur
)) {
922 inst
.Instruction
.Texture
= 1;
923 inst
.Texture
.Texture
= j
;
928 if (j
== TGSI_TEXTURE_COUNT
) {
929 report_error( ctx
, "Expected texture target" );
935 if (info
->is_branch
) {
938 eat_opt_white( &ctx
->cur
);
939 if (*ctx
->cur
!= ':') {
940 report_error( ctx
, "Expected `:'" );
944 eat_opt_white( &ctx
->cur
);
945 if (!parse_uint( &ctx
->cur
, &target
)) {
946 report_error( ctx
, "Expected a label" );
949 inst
.Instruction
.Label
= 1;
950 inst
.Label
.Label
= target
;
953 advance
= tgsi_build_full_instruction(
957 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
960 ctx
->tokens_cur
+= advance
;
965 static const char *semantic_names
[TGSI_SEMANTIC_COUNT
] =
980 static const char *interpolate_names
[TGSI_INTERPOLATE_COUNT
] =
987 static boolean
parse_declaration( struct translate_ctx
*ctx
)
989 struct tgsi_full_declaration decl
;
991 struct parsed_dcl_bracket brackets
[2];
997 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
998 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
1000 if (!eat_white( &ctx
->cur
)) {
1001 report_error( ctx
, "Syntax error" );
1004 if (!parse_register_dcl( ctx
, &file
, brackets
, &num_brackets
))
1006 if (!parse_opt_writemask( ctx
, &writemask
))
1009 decl
= tgsi_default_full_declaration();
1010 decl
.Declaration
.File
= file
;
1011 decl
.Declaration
.UsageMask
= writemask
;
1013 if (num_brackets
== 1) {
1014 decl
.Range
.First
= brackets
[0].first
;
1015 decl
.Range
.Last
= brackets
[0].last
;
1017 decl
.Range
.First
= brackets
[1].first
;
1018 decl
.Range
.Last
= brackets
[1].last
;
1020 decl
.Declaration
.Dimension
= 1;
1021 decl
.Dim
.Index2D
= brackets
[0].first
;
1025 eat_opt_white( &cur
);
1030 eat_opt_white( &cur
);
1031 for (i
= 0; i
< TGSI_SEMANTIC_COUNT
; i
++) {
1032 if (str_match_no_case( &cur
, semantic_names
[i
] )) {
1033 const char *cur2
= cur
;
1036 if (is_digit_alpha_underscore( cur
))
1038 eat_opt_white( &cur2
);
1041 eat_opt_white( &cur2
);
1042 if (!parse_uint( &cur2
, &index
)) {
1043 report_error( ctx
, "Expected literal integer" );
1046 eat_opt_white( &cur2
);
1048 report_error( ctx
, "Expected `]'" );
1053 decl
.Semantic
.Index
= index
;
1058 decl
.Declaration
.Semantic
= 1;
1059 decl
.Semantic
.Name
= i
;
1068 eat_opt_white( &cur
);
1073 eat_opt_white( &cur
);
1074 for (i
= 0; i
< TGSI_INTERPOLATE_COUNT
; i
++) {
1075 if (str_match_no_case( &cur
, interpolate_names
[i
] )) {
1076 if (is_digit_alpha_underscore( cur
))
1078 decl
.Declaration
.Interpolate
= i
;
1084 if (i
== TGSI_INTERPOLATE_COUNT
) {
1085 report_error( ctx
, "Expected semantic or interpolate attribute" );
1090 advance
= tgsi_build_full_declaration(
1094 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1097 ctx
->tokens_cur
+= advance
;
1102 static boolean
parse_immediate( struct translate_ctx
*ctx
)
1104 struct tgsi_full_immediate imm
;
1109 if (!eat_white( &ctx
->cur
)) {
1110 report_error( ctx
, "Syntax error" );
1113 if (!str_match_no_case( &ctx
->cur
, "FLT32" ) || is_digit_alpha_underscore( ctx
->cur
)) {
1114 report_error( ctx
, "Expected `FLT32'" );
1117 eat_opt_white( &ctx
->cur
);
1118 if (*ctx
->cur
!= '{') {
1119 report_error( ctx
, "Expected `{'" );
1123 for (i
= 0; i
< 4; i
++) {
1124 eat_opt_white( &ctx
->cur
);
1126 if (*ctx
->cur
!= ',') {
1127 report_error( ctx
, "Expected `,'" );
1131 eat_opt_white( &ctx
->cur
);
1133 if (!parse_float( &ctx
->cur
, &values
[i
] )) {
1134 report_error( ctx
, "Expected literal floating point" );
1138 eat_opt_white( &ctx
->cur
);
1139 if (*ctx
->cur
!= '}') {
1140 report_error( ctx
, "Expected `}'" );
1145 imm
= tgsi_default_full_immediate();
1146 imm
.Immediate
.NrTokens
+= 4;
1147 imm
.Immediate
.DataType
= TGSI_IMM_FLOAT32
;
1148 imm
.u
[0].Float
= values
[0];
1149 imm
.u
[1].Float
= values
[1];
1150 imm
.u
[2].Float
= values
[2];
1151 imm
.u
[3].Float
= values
[3];
1153 advance
= tgsi_build_full_immediate(
1157 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1160 ctx
->tokens_cur
+= advance
;
1165 static const char *property_names
[] =
1167 "GS_INPUT_PRIMITIVE",
1168 "GS_OUTPUT_PRIMITIVE",
1169 "GS_MAX_OUTPUT_VERTICES",
1171 "FS_COORD_PIXEL_CENTER"
1174 static const char *primitive_names
[] =
1188 static const char *fs_coord_origin_names
[] =
1194 static const char *fs_coord_pixel_center_names
[] =
1202 parse_primitive( const char **pcur
, uint
*primitive
)
1206 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++) {
1207 const char *cur
= *pcur
;
1209 if (str_match_no_case( &cur
, primitive_names
[i
])) {
1219 parse_fs_coord_origin( const char **pcur
, uint
*fs_coord_origin
)
1223 for (i
= 0; i
< sizeof(fs_coord_origin_names
) / sizeof(fs_coord_origin_names
[0]); i
++) {
1224 const char *cur
= *pcur
;
1226 if (str_match_no_case( &cur
, fs_coord_origin_names
[i
])) {
1227 *fs_coord_origin
= i
;
1236 parse_fs_coord_pixel_center( const char **pcur
, uint
*fs_coord_pixel_center
)
1240 for (i
= 0; i
< sizeof(fs_coord_pixel_center_names
) / sizeof(fs_coord_pixel_center_names
[0]); i
++) {
1241 const char *cur
= *pcur
;
1243 if (str_match_no_case( &cur
, fs_coord_pixel_center_names
[i
])) {
1244 *fs_coord_pixel_center
= i
;
1253 static boolean
parse_property( struct translate_ctx
*ctx
)
1255 struct tgsi_full_property prop
;
1261 if (!eat_white( &ctx
->cur
)) {
1262 report_error( ctx
, "Syntax error" );
1265 if (!parse_identifier( &ctx
->cur
, id
)) {
1266 report_error( ctx
, "Syntax error" );
1269 for (property_name
= 0; property_name
< TGSI_PROPERTY_COUNT
;
1271 if (streq_nocase_uprcase(property_names
[property_name
], id
)) {
1275 if (property_name
>= TGSI_PROPERTY_COUNT
) {
1276 debug_printf( "\nError: Unknown property : '%s'", id
);
1280 eat_opt_white( &ctx
->cur
);
1281 switch(property_name
) {
1282 case TGSI_PROPERTY_GS_INPUT_PRIM
:
1283 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
1284 if (!parse_primitive(&ctx
->cur
, &values
[0] )) {
1285 report_error( ctx
, "Unknown primitive name as property!" );
1288 if (property_name
== TGSI_PROPERTY_GS_INPUT_PRIM
&&
1289 ctx
->processor
== TGSI_PROCESSOR_GEOMETRY
) {
1290 ctx
->implied_array_size
= u_vertices_per_prim(values
[0]);
1293 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
1294 if (!parse_fs_coord_origin(&ctx
->cur
, &values
[0] )) {
1295 report_error( ctx
, "Unknown coord origin as property: must be UPPER_LEFT or LOWER_LEFT!" );
1299 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
1300 if (!parse_fs_coord_pixel_center(&ctx
->cur
, &values
[0] )) {
1301 report_error( ctx
, "Unknown coord pixel center as property: must be HALF_INTEGER or INTEGER!" );
1306 if (!parse_uint(&ctx
->cur
, &values
[0] )) {
1307 report_error( ctx
, "Expected unsigned integer as property!" );
1312 prop
= tgsi_default_full_property();
1313 prop
.Property
.PropertyName
= property_name
;
1314 prop
.Property
.NrTokens
+= 1;
1315 prop
.u
[0].Data
= values
[0];
1317 advance
= tgsi_build_full_property(
1321 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1324 ctx
->tokens_cur
+= advance
;
1330 static boolean
translate( struct translate_ctx
*ctx
)
1332 eat_opt_white( &ctx
->cur
);
1333 if (!parse_header( ctx
))
1336 while (*ctx
->cur
!= '\0') {
1338 if (!eat_white( &ctx
->cur
)) {
1339 report_error( ctx
, "Syntax error" );
1343 if (*ctx
->cur
== '\0')
1345 if (parse_label( ctx
, &label_val
)) {
1346 if (!parse_instruction( ctx
, TRUE
))
1349 else if (str_match_no_case( &ctx
->cur
, "DCL" )) {
1350 if (!parse_declaration( ctx
))
1353 else if (str_match_no_case( &ctx
->cur
, "IMM" )) {
1354 if (!parse_immediate( ctx
))
1357 else if (str_match_no_case( &ctx
->cur
, "PROPERTY" )) {
1358 if (!parse_property( ctx
))
1361 else if (!parse_instruction( ctx
, FALSE
)) {
1370 tgsi_text_translate(
1372 struct tgsi_token
*tokens
,
1375 struct translate_ctx ctx
;
1379 ctx
.tokens
= tokens
;
1380 ctx
.tokens_cur
= tokens
;
1381 ctx
.tokens_end
= tokens
+ num_tokens
;
1383 if (!translate( &ctx
))
1386 return tgsi_sanity_check( tokens
);