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 boolean
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
))
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
] =
287 parse_file( const char **pcur
, uint
*file
)
291 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++) {
292 const char *cur
= *pcur
;
294 if (str_match_no_case( &cur
, file_names
[i
] )) {
295 if (!is_digit_alpha_underscore( cur
)) {
307 struct translate_ctx
*ctx
,
313 eat_opt_white( &cur
);
316 *writemask
= TGSI_WRITEMASK_NONE
;
317 eat_opt_white( &cur
);
318 if (uprcase( *cur
) == 'X') {
320 *writemask
|= TGSI_WRITEMASK_X
;
322 if (uprcase( *cur
) == 'Y') {
324 *writemask
|= TGSI_WRITEMASK_Y
;
326 if (uprcase( *cur
) == 'Z') {
328 *writemask
|= TGSI_WRITEMASK_Z
;
330 if (uprcase( *cur
) == 'W') {
332 *writemask
|= TGSI_WRITEMASK_W
;
335 if (*writemask
== TGSI_WRITEMASK_NONE
) {
336 report_error( ctx
, "Writemask expected" );
343 *writemask
= TGSI_WRITEMASK_XYZW
;
349 parse_register_dst( struct translate_ctx
*ctx
,
353 struct parsed_src_bracket
{
363 parse_register_src_bracket(
364 struct translate_ctx
*ctx
,
365 struct parsed_src_bracket
*brackets
)
370 memset(brackets
, 0, sizeof(struct parsed_src_bracket
));
372 eat_opt_white( &ctx
->cur
);
375 if (parse_file( &cur
, &brackets
->ind_file
)) {
376 if (!parse_register_dst( ctx
, &brackets
->ind_file
,
377 &brackets
->ind_index
))
379 eat_opt_white( &ctx
->cur
);
381 if (*ctx
->cur
== '.') {
383 eat_opt_white(&ctx
->cur
);
385 switch (uprcase(*ctx
->cur
)) {
387 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
390 brackets
->ind_comp
= TGSI_SWIZZLE_Y
;
393 brackets
->ind_comp
= TGSI_SWIZZLE_Z
;
396 brackets
->ind_comp
= TGSI_SWIZZLE_W
;
399 report_error(ctx
, "Expected indirect register swizzle component `x', `y', `z' or `w'");
403 eat_opt_white(&ctx
->cur
);
406 if (*ctx
->cur
== '+' || *ctx
->cur
== '-') {
409 negate
= *ctx
->cur
== '-';
411 eat_opt_white( &ctx
->cur
);
412 if (!parse_uint( &ctx
->cur
, &uindex
)) {
413 report_error( ctx
, "Expected literal unsigned integer" );
417 brackets
->index
= -(int) uindex
;
419 brackets
->index
= (int) uindex
;
426 if (!parse_uint( &ctx
->cur
, &uindex
)) {
427 report_error( ctx
, "Expected literal unsigned integer" );
430 brackets
->index
= (int) uindex
;
431 brackets
->ind_file
= TGSI_FILE_NULL
;
432 brackets
->ind_index
= 0;
434 eat_opt_white( &ctx
->cur
);
435 if (*ctx
->cur
!= ']') {
436 report_error( ctx
, "Expected `]'" );
444 parse_opt_register_src_bracket(
445 struct translate_ctx
*ctx
,
446 struct parsed_src_bracket
*brackets
,
447 int *parsed_brackets
)
449 const char *cur
= ctx
->cur
;
451 *parsed_brackets
= 0;
453 eat_opt_white( &cur
);
458 if (!parse_register_src_bracket(ctx
, brackets
))
461 *parsed_brackets
= 1;
467 /* <register_file_bracket> ::= <file> `['
470 parse_register_file_bracket(
471 struct translate_ctx
*ctx
,
474 if (!parse_file( &ctx
->cur
, file
)) {
475 report_error( ctx
, "Unknown register file" );
478 eat_opt_white( &ctx
->cur
);
479 if (*ctx
->cur
!= '[') {
480 report_error( ctx
, "Expected `['" );
487 /* <register_file_bracket_index> ::= <register_file_bracket> <uint>
490 parse_register_file_bracket_index(
491 struct translate_ctx
*ctx
,
497 if (!parse_register_file_bracket( ctx
, file
))
499 eat_opt_white( &ctx
->cur
);
500 if (!parse_uint( &ctx
->cur
, &uindex
)) {
501 report_error( ctx
, "Expected literal unsigned integer" );
504 *index
= (int) uindex
;
508 /* Parse source register operand.
509 * <register_src> ::= <register_file_bracket_index> `]' |
510 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `]' |
511 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `+' <uint> `]' |
512 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `-' <uint> `]'
516 struct translate_ctx
*ctx
,
518 struct parsed_src_bracket
*brackets
)
521 brackets
->ind_comp
= TGSI_SWIZZLE_X
;
522 if (!parse_register_file_bracket( ctx
, file
))
524 if (!parse_register_src_bracket( ctx
, brackets
))
530 struct parsed_dcl_bracket
{
536 parse_register_dcl_bracket(
537 struct translate_ctx
*ctx
,
538 struct parsed_dcl_bracket
*bracket
)
541 memset(bracket
, 0, sizeof(struct parsed_dcl_bracket
));
543 eat_opt_white( &ctx
->cur
);
545 if (!parse_uint( &ctx
->cur
, &uindex
)) {
546 /* it can be an empty bracket [] which means its range
547 * is from 0 to some implied size */
548 if (ctx
->cur
[0] == ']' && ctx
->implied_array_size
!= 0) {
550 bracket
->last
= ctx
->implied_array_size
- 1;
553 report_error( ctx
, "Expected literal unsigned integer" );
556 bracket
->first
= uindex
;
558 eat_opt_white( &ctx
->cur
);
560 if (ctx
->cur
[0] == '.' && ctx
->cur
[1] == '.') {
564 eat_opt_white( &ctx
->cur
);
565 if (!parse_uint( &ctx
->cur
, &uindex
)) {
566 report_error( ctx
, "Expected literal integer" );
569 bracket
->last
= (int) uindex
;
570 eat_opt_white( &ctx
->cur
);
573 bracket
->last
= bracket
->first
;
577 if (*ctx
->cur
!= ']') {
578 report_error( ctx
, "Expected `]' or `..'" );
585 /* Parse register declaration.
586 * <register_dcl> ::= <register_file_bracket_index> `]' |
587 * <register_file_bracket_index> `..' <index> `]'
591 struct translate_ctx
*ctx
,
593 struct parsed_dcl_bracket
*brackets
,
600 if (!parse_register_file_bracket( ctx
, file
))
602 if (!parse_register_dcl_bracket( ctx
, &brackets
[0] ))
608 eat_opt_white( &cur
);
613 if (!parse_register_dcl_bracket( ctx
, &brackets
[1] ))
615 /* for geometry shader we don't really care about
616 * the first brackets it's always the size of the
617 * input primitive. so we want to declare just
618 * the index relevant to the semantics which is in
619 * the second bracket */
620 if (ctx
->processor
== TGSI_PROCESSOR_GEOMETRY
&& *file
== TGSI_FILE_INPUT
) {
621 brackets
[0] = brackets
[1];
632 /* Parse destination register operand.
633 * <register_dst> ::= <register_file_bracket_index> `]'
637 struct translate_ctx
*ctx
,
641 if (!parse_register_file_bracket_index( ctx
, file
, index
))
643 eat_opt_white( &ctx
->cur
);
644 if (*ctx
->cur
!= ']') {
645 report_error( ctx
, "Expected `]'" );
654 struct translate_ctx
*ctx
,
655 struct tgsi_full_dst_register
*dst
)
662 if (!parse_register_dst( ctx
, &file
, &index
))
666 eat_opt_white( &cur
);
668 if (!parse_opt_writemask( ctx
, &writemask
))
671 dst
->Register
.File
= file
;
672 dst
->Register
.Index
= index
;
673 dst
->Register
.WriteMask
= writemask
;
678 parse_optional_swizzle(
679 struct translate_ctx
*ctx
,
681 boolean
*parsed_swizzle
)
683 const char *cur
= ctx
->cur
;
685 *parsed_swizzle
= FALSE
;
687 eat_opt_white( &cur
);
692 eat_opt_white( &cur
);
693 for (i
= 0; i
< 4; i
++) {
694 if (uprcase( *cur
) == 'X')
695 swizzle
[i
] = TGSI_SWIZZLE_X
;
696 else if (uprcase( *cur
) == 'Y')
697 swizzle
[i
] = TGSI_SWIZZLE_Y
;
698 else if (uprcase( *cur
) == 'Z')
699 swizzle
[i
] = TGSI_SWIZZLE_Z
;
700 else if (uprcase( *cur
) == 'W')
701 swizzle
[i
] = TGSI_SWIZZLE_W
;
703 report_error( ctx
, "Expected register swizzle component `x', `y', `z', `w', `0' or `1'" );
708 *parsed_swizzle
= TRUE
;
716 struct translate_ctx
*ctx
,
717 struct tgsi_full_src_register
*src
)
721 boolean parsed_swizzle
;
722 struct parsed_src_bracket bracket
[2];
723 int parsed_opt_brackets
;
725 if (*ctx
->cur
== '-') {
727 eat_opt_white( &ctx
->cur
);
728 src
->Register
.Negate
= 1;
731 if (*ctx
->cur
== '|') {
733 eat_opt_white( &ctx
->cur
);
734 src
->Register
.Absolute
= 1;
737 if (!parse_register_src(ctx
, &file
, &bracket
[0]))
739 if (!parse_opt_register_src_bracket(ctx
, &bracket
[1], &parsed_opt_brackets
))
742 src
->Register
.File
= file
;
743 if (parsed_opt_brackets
) {
744 src
->Register
.Dimension
= 1;
745 src
->Dimension
.Indirect
= 0;
746 src
->Dimension
.Dimension
= 0;
747 src
->Dimension
.Index
= bracket
[0].index
;
748 bracket
[0] = bracket
[1];
750 src
->Register
.Index
= bracket
[0].index
;
751 if (bracket
[0].ind_file
!= TGSI_FILE_NULL
) {
752 src
->Register
.Indirect
= 1;
753 src
->Indirect
.File
= bracket
[0].ind_file
;
754 src
->Indirect
.Index
= bracket
[0].ind_index
;
755 src
->Indirect
.SwizzleX
= bracket
[0].ind_comp
;
756 src
->Indirect
.SwizzleY
= bracket
[0].ind_comp
;
757 src
->Indirect
.SwizzleZ
= bracket
[0].ind_comp
;
758 src
->Indirect
.SwizzleW
= bracket
[0].ind_comp
;
761 /* Parse optional swizzle.
763 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
)) {
764 if (parsed_swizzle
) {
765 src
->Register
.SwizzleX
= swizzle
[0];
766 src
->Register
.SwizzleY
= swizzle
[1];
767 src
->Register
.SwizzleZ
= swizzle
[2];
768 src
->Register
.SwizzleW
= swizzle
[3];
772 if (src
->Register
.Absolute
) {
773 eat_opt_white( &ctx
->cur
);
774 if (*ctx
->cur
!= '|') {
775 report_error( ctx
, "Expected `|'" );
785 static const char *texture_names
[TGSI_TEXTURE_COUNT
] =
799 match_inst_mnemonic(const char **pcur
,
800 const struct tgsi_opcode_info
*info
)
802 if (str_match_no_case(pcur
, info
->mnemonic
)) {
810 struct translate_ctx
*ctx
,
814 uint saturate
= TGSI_SAT_NONE
;
815 const struct tgsi_opcode_info
*info
;
816 struct tgsi_full_instruction inst
;
819 /* Parse instruction name.
821 eat_opt_white( &ctx
->cur
);
822 for (i
= 0; i
< TGSI_OPCODE_LAST
; i
++) {
823 const char *cur
= ctx
->cur
;
825 info
= tgsi_get_opcode_info( i
);
826 if (match_inst_mnemonic(&cur
, info
)) {
827 if (str_match_no_case( &cur
, "_SATNV" ))
828 saturate
= TGSI_SAT_MINUS_PLUS_ONE
;
829 else if (str_match_no_case( &cur
, "_SAT" ))
830 saturate
= TGSI_SAT_ZERO_ONE
;
832 if (info
->num_dst
+ info
->num_src
+ info
->is_tex
== 0) {
833 if (!is_digit_alpha_underscore( cur
)) {
838 else if (*cur
== '\0' || eat_white( &cur
)) {
844 if (i
== TGSI_OPCODE_LAST
) {
846 report_error( ctx
, "Unknown opcode" );
848 report_error( ctx
, "Expected `DCL', `IMM' or a label" );
852 inst
= tgsi_default_full_instruction();
853 inst
.Instruction
.Opcode
= i
;
854 inst
.Instruction
.Saturate
= saturate
;
855 inst
.Instruction
.NumDstRegs
= info
->num_dst
;
856 inst
.Instruction
.NumSrcRegs
= info
->num_src
;
858 /* Parse instruction operands.
860 for (i
= 0; i
< info
->num_dst
+ info
->num_src
+ info
->is_tex
; i
++) {
862 eat_opt_white( &ctx
->cur
);
863 if (*ctx
->cur
!= ',') {
864 report_error( ctx
, "Expected `,'" );
868 eat_opt_white( &ctx
->cur
);
871 if (i
< info
->num_dst
) {
872 if (!parse_dst_operand( ctx
, &inst
.Dst
[i
] ))
875 else if (i
< info
->num_dst
+ info
->num_src
) {
876 if (!parse_src_operand( ctx
, &inst
.Src
[i
- info
->num_dst
] ))
882 for (j
= 0; j
< TGSI_TEXTURE_COUNT
; j
++) {
883 if (str_match_no_case( &ctx
->cur
, texture_names
[j
] )) {
884 if (!is_digit_alpha_underscore( ctx
->cur
)) {
885 inst
.Instruction
.Texture
= 1;
886 inst
.Texture
.Texture
= j
;
891 if (j
== TGSI_TEXTURE_COUNT
) {
892 report_error( ctx
, "Expected texture target" );
898 if (info
->is_branch
) {
901 eat_opt_white( &ctx
->cur
);
902 if (*ctx
->cur
!= ':') {
903 report_error( ctx
, "Expected `:'" );
907 eat_opt_white( &ctx
->cur
);
908 if (!parse_uint( &ctx
->cur
, &target
)) {
909 report_error( ctx
, "Expected a label" );
912 inst
.Instruction
.Label
= 1;
913 inst
.Label
.Label
= target
;
916 advance
= tgsi_build_full_instruction(
920 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
923 ctx
->tokens_cur
+= advance
;
928 static const char *semantic_names
[TGSI_SEMANTIC_COUNT
] =
943 static const char *interpolate_names
[TGSI_INTERPOLATE_COUNT
] =
950 static boolean
parse_declaration( struct translate_ctx
*ctx
)
952 struct tgsi_full_declaration decl
;
954 struct parsed_dcl_bracket brackets
[2];
960 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
961 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
963 if (!eat_white( &ctx
->cur
)) {
964 report_error( ctx
, "Syntax error" );
967 if (!parse_register_dcl( ctx
, &file
, brackets
, &num_brackets
))
969 if (!parse_opt_writemask( ctx
, &writemask
))
972 decl
= tgsi_default_full_declaration();
973 decl
.Declaration
.File
= file
;
974 decl
.Declaration
.UsageMask
= writemask
;
976 if (num_brackets
== 1) {
977 decl
.Range
.First
= brackets
[0].first
;
978 decl
.Range
.Last
= brackets
[0].last
;
980 decl
.Range
.First
= brackets
[1].first
;
981 decl
.Range
.Last
= brackets
[1].last
;
983 decl
.Declaration
.Dimension
= 1;
984 decl
.Dim
.Index2D
= brackets
[0].first
;
988 eat_opt_white( &cur
);
993 eat_opt_white( &cur
);
994 for (i
= 0; i
< TGSI_SEMANTIC_COUNT
; i
++) {
995 if (str_match_no_case( &cur
, semantic_names
[i
] )) {
996 const char *cur2
= cur
;
999 if (is_digit_alpha_underscore( cur
))
1001 eat_opt_white( &cur2
);
1004 eat_opt_white( &cur2
);
1005 if (!parse_uint( &cur2
, &index
)) {
1006 report_error( ctx
, "Expected literal integer" );
1009 eat_opt_white( &cur2
);
1011 report_error( ctx
, "Expected `]'" );
1016 decl
.Semantic
.Index
= index
;
1021 decl
.Declaration
.Semantic
= 1;
1022 decl
.Semantic
.Name
= i
;
1031 eat_opt_white( &cur
);
1036 eat_opt_white( &cur
);
1037 for (i
= 0; i
< TGSI_INTERPOLATE_COUNT
; i
++) {
1038 if (str_match_no_case( &cur
, interpolate_names
[i
] )) {
1039 if (is_digit_alpha_underscore( cur
))
1041 decl
.Declaration
.Interpolate
= i
;
1047 if (i
== TGSI_INTERPOLATE_COUNT
) {
1048 report_error( ctx
, "Expected semantic or interpolate attribute" );
1053 advance
= tgsi_build_full_declaration(
1057 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1060 ctx
->tokens_cur
+= advance
;
1065 static boolean
parse_immediate( struct translate_ctx
*ctx
)
1067 struct tgsi_full_immediate imm
;
1072 if (!eat_white( &ctx
->cur
)) {
1073 report_error( ctx
, "Syntax error" );
1076 if (!str_match_no_case( &ctx
->cur
, "FLT32" ) || is_digit_alpha_underscore( ctx
->cur
)) {
1077 report_error( ctx
, "Expected `FLT32'" );
1080 eat_opt_white( &ctx
->cur
);
1081 if (*ctx
->cur
!= '{') {
1082 report_error( ctx
, "Expected `{'" );
1086 for (i
= 0; i
< 4; i
++) {
1087 eat_opt_white( &ctx
->cur
);
1089 if (*ctx
->cur
!= ',') {
1090 report_error( ctx
, "Expected `,'" );
1094 eat_opt_white( &ctx
->cur
);
1096 if (!parse_float( &ctx
->cur
, &values
[i
] )) {
1097 report_error( ctx
, "Expected literal floating point" );
1101 eat_opt_white( &ctx
->cur
);
1102 if (*ctx
->cur
!= '}') {
1103 report_error( ctx
, "Expected `}'" );
1108 imm
= tgsi_default_full_immediate();
1109 imm
.Immediate
.NrTokens
+= 4;
1110 imm
.Immediate
.DataType
= TGSI_IMM_FLOAT32
;
1111 imm
.u
[0].Float
= values
[0];
1112 imm
.u
[1].Float
= values
[1];
1113 imm
.u
[2].Float
= values
[2];
1114 imm
.u
[3].Float
= values
[3];
1116 advance
= tgsi_build_full_immediate(
1120 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1123 ctx
->tokens_cur
+= advance
;
1128 static const char *property_names
[] =
1130 "GS_INPUT_PRIMITIVE",
1131 "GS_OUTPUT_PRIMITIVE",
1132 "GS_MAX_OUTPUT_VERTICES",
1134 "FS_COORD_PIXEL_CENTER"
1137 static const char *primitive_names
[] =
1151 static const char *fs_coord_origin_names
[] =
1157 static const char *fs_coord_pixel_center_names
[] =
1165 parse_primitive( const char **pcur
, uint
*primitive
)
1169 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++) {
1170 const char *cur
= *pcur
;
1172 if (str_match_no_case( &cur
, primitive_names
[i
])) {
1182 parse_fs_coord_origin( const char **pcur
, uint
*fs_coord_origin
)
1186 for (i
= 0; i
< sizeof(fs_coord_origin_names
) / sizeof(fs_coord_origin_names
[0]); i
++) {
1187 const char *cur
= *pcur
;
1189 if (str_match_no_case( &cur
, fs_coord_origin_names
[i
])) {
1190 *fs_coord_origin
= i
;
1199 parse_fs_coord_pixel_center( const char **pcur
, uint
*fs_coord_pixel_center
)
1203 for (i
= 0; i
< sizeof(fs_coord_pixel_center_names
) / sizeof(fs_coord_pixel_center_names
[0]); i
++) {
1204 const char *cur
= *pcur
;
1206 if (str_match_no_case( &cur
, fs_coord_pixel_center_names
[i
])) {
1207 *fs_coord_pixel_center
= i
;
1216 static boolean
parse_property( struct translate_ctx
*ctx
)
1218 struct tgsi_full_property prop
;
1224 if (!eat_white( &ctx
->cur
)) {
1225 report_error( ctx
, "Syntax error" );
1228 if (!parse_identifier( &ctx
->cur
, id
)) {
1229 report_error( ctx
, "Syntax error" );
1232 for (property_name
= 0; property_name
< TGSI_PROPERTY_COUNT
;
1234 if (streq_nocase_uprcase(property_names
[property_name
], id
)) {
1238 if (property_name
>= TGSI_PROPERTY_COUNT
) {
1239 debug_printf( "\nError: Unknown property : '%s'", id
);
1243 eat_opt_white( &ctx
->cur
);
1244 switch(property_name
) {
1245 case TGSI_PROPERTY_GS_INPUT_PRIM
:
1246 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
1247 if (!parse_primitive(&ctx
->cur
, &values
[0] )) {
1248 report_error( ctx
, "Unknown primitive name as property!" );
1251 if (property_name
== TGSI_PROPERTY_GS_INPUT_PRIM
&&
1252 ctx
->processor
== TGSI_PROCESSOR_GEOMETRY
) {
1253 ctx
->implied_array_size
= u_vertices_per_prim(values
[0]);
1256 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
1257 if (!parse_fs_coord_origin(&ctx
->cur
, &values
[0] )) {
1258 report_error( ctx
, "Unknown coord origin as property: must be UPPER_LEFT or LOWER_LEFT!" );
1262 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
1263 if (!parse_fs_coord_pixel_center(&ctx
->cur
, &values
[0] )) {
1264 report_error( ctx
, "Unknown coord pixel center as property: must be HALF_INTEGER or INTEGER!" );
1269 if (!parse_uint(&ctx
->cur
, &values
[0] )) {
1270 report_error( ctx
, "Expected unsigned integer as property!" );
1275 prop
= tgsi_default_full_property();
1276 prop
.Property
.PropertyName
= property_name
;
1277 prop
.Property
.NrTokens
+= 1;
1278 prop
.u
[0].Data
= values
[0];
1280 advance
= tgsi_build_full_property(
1284 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
1287 ctx
->tokens_cur
+= advance
;
1293 static boolean
translate( struct translate_ctx
*ctx
)
1295 eat_opt_white( &ctx
->cur
);
1296 if (!parse_header( ctx
))
1299 while (*ctx
->cur
!= '\0') {
1301 if (!eat_white( &ctx
->cur
)) {
1302 report_error( ctx
, "Syntax error" );
1306 if (*ctx
->cur
== '\0')
1308 if (parse_label( ctx
, &label_val
)) {
1309 if (!parse_instruction( ctx
, TRUE
))
1312 else if (str_match_no_case( &ctx
->cur
, "DCL" )) {
1313 if (!parse_declaration( ctx
))
1316 else if (str_match_no_case( &ctx
->cur
, "IMM" )) {
1317 if (!parse_immediate( ctx
))
1320 else if (str_match_no_case( &ctx
->cur
, "PROPERTY" )) {
1321 if (!parse_property( ctx
))
1324 else if (!parse_instruction( ctx
, FALSE
)) {
1333 tgsi_text_translate(
1335 struct tgsi_token
*tokens
,
1338 struct translate_ctx ctx
;
1342 ctx
.tokens
= tokens
;
1343 ctx
.tokens_cur
= tokens
;
1344 ctx
.tokens_end
= tokens
+ num_tokens
;
1346 if (!translate( &ctx
))
1349 return tgsi_sanity_check( tokens
);