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 "tgsi_text.h"
31 #include "tgsi_build.h"
32 #include "tgsi_info.h"
33 #include "tgsi_parse.h"
34 #include "tgsi_sanity.h"
35 #include "tgsi_util.h"
37 static boolean
is_alpha_underscore( const char *cur
)
40 (*cur
>= 'a' && *cur
<= 'z') ||
41 (*cur
>= 'A' && *cur
<= 'Z') ||
45 static boolean
is_digit( const char *cur
)
47 return *cur
>= '0' && *cur
<= '9';
50 static boolean
is_digit_alpha_underscore( const char *cur
)
52 return is_digit( cur
) || is_alpha_underscore( cur
);
55 static boolean
uprcase( char c
)
57 if (c
>= 'a' && c
<= 'z')
58 return c
+= 'A' - 'a';
62 static boolean
str_match_no_case( const char **pcur
, const char *str
)
64 const char *cur
= *pcur
;
66 while (*str
!= '\0' && *str
== uprcase( *cur
)) {
77 /* Eat zero or more whitespaces.
79 static void eat_opt_white( const char **pcur
)
81 while (**pcur
== ' ' || **pcur
== '\t' || **pcur
== '\n')
85 /* Eat one or more whitespaces.
86 * Return TRUE if at least one whitespace eaten.
88 static boolean
eat_white( const char **pcur
)
90 const char *cur
= *pcur
;
92 eat_opt_white( pcur
);
96 /* Parse unsigned integer.
97 * No checks for overflow.
99 static boolean
parse_uint( const char **pcur
, uint
*val
)
101 const char *cur
= *pcur
;
103 if (is_digit( cur
)) {
105 while (is_digit( cur
))
106 *val
= *val
* 10 + *cur
++ - '0';
113 /* Parse floating point.
115 static boolean
parse_float( const char **pcur
, float *val
)
117 const char *cur
= *pcur
;
118 boolean integral_part
= FALSE
;
119 boolean fractional_part
= FALSE
;
121 *val
= (float) atof( cur
);
123 if (*cur
== '-' || *cur
== '+')
125 if (is_digit( cur
)) {
127 integral_part
= TRUE
;
128 while (is_digit( cur
))
133 if (is_digit( cur
)) {
135 fractional_part
= TRUE
;
136 while (is_digit( cur
))
140 if (!integral_part
&& !fractional_part
)
142 if (uprcase( *cur
) == 'E') {
144 if (*cur
== '-' || *cur
== '+')
146 if (is_digit( cur
)) {
148 while (is_digit( cur
))
162 struct tgsi_token
*tokens
;
163 struct tgsi_token
*tokens_cur
;
164 struct tgsi_token
*tokens_end
;
165 struct tgsi_header
*header
;
168 static void report_error( struct translate_ctx
*ctx
, const char *msg
)
170 debug_printf( "\nError: %s", msg
);
173 /* Parse shader header.
174 * Return TRUE for one of the following headers.
179 static boolean
parse_header( struct translate_ctx
*ctx
)
183 if (str_match_no_case( &ctx
->cur
, "FRAG1.1" ))
184 processor
= TGSI_PROCESSOR_FRAGMENT
;
185 else if (str_match_no_case( &ctx
->cur
, "VERT1.1" ))
186 processor
= TGSI_PROCESSOR_VERTEX
;
187 else if (str_match_no_case( &ctx
->cur
, "GEOM1.1" ))
188 processor
= TGSI_PROCESSOR_GEOMETRY
;
190 report_error( ctx
, "Unknown header" );
194 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
196 *(struct tgsi_version
*) ctx
->tokens_cur
++ = tgsi_build_version();
198 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
200 ctx
->header
= (struct tgsi_header
*) ctx
->tokens_cur
++;
201 *ctx
->header
= tgsi_build_header();
203 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
205 *(struct tgsi_processor
*) ctx
->tokens_cur
++ = tgsi_build_processor( processor
, ctx
->header
);
210 static boolean
parse_label( struct translate_ctx
*ctx
, uint
*val
)
212 const char *cur
= ctx
->cur
;
214 if (parse_uint( &cur
, val
)) {
215 eat_opt_white( &cur
);
225 static const char *file_names
[TGSI_FILE_COUNT
] =
240 parse_file( const char **pcur
, uint
*file
)
244 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++) {
245 const char *cur
= *pcur
;
247 if (str_match_no_case( &cur
, file_names
[i
] )) {
248 if (!is_digit_alpha_underscore( cur
)) {
260 struct translate_ctx
*ctx
,
266 eat_opt_white( &cur
);
269 *writemask
= TGSI_WRITEMASK_NONE
;
270 eat_opt_white( &cur
);
271 if (uprcase( *cur
) == 'X') {
273 *writemask
|= TGSI_WRITEMASK_X
;
275 if (uprcase( *cur
) == 'Y') {
277 *writemask
|= TGSI_WRITEMASK_Y
;
279 if (uprcase( *cur
) == 'Z') {
281 *writemask
|= TGSI_WRITEMASK_Z
;
283 if (uprcase( *cur
) == 'W') {
285 *writemask
|= TGSI_WRITEMASK_W
;
288 if (*writemask
== TGSI_WRITEMASK_NONE
) {
289 report_error( ctx
, "Writemask expected" );
296 *writemask
= TGSI_WRITEMASK_XYZW
;
301 /* <register_file_bracket> ::= <file> `['
304 parse_register_file_bracket(
305 struct translate_ctx
*ctx
,
308 if (!parse_file( &ctx
->cur
, file
)) {
309 report_error( ctx
, "Unknown register file" );
312 eat_opt_white( &ctx
->cur
);
313 if (*ctx
->cur
!= '[') {
314 report_error( ctx
, "Expected `['" );
321 /* <register_file_bracket_index> ::= <register_file_bracket> <uint>
324 parse_register_file_bracket_index(
325 struct translate_ctx
*ctx
,
331 if (!parse_register_file_bracket( ctx
, file
))
333 eat_opt_white( &ctx
->cur
);
334 if (!parse_uint( &ctx
->cur
, &uindex
)) {
335 report_error( ctx
, "Expected literal unsigned integer" );
338 *index
= (int) uindex
;
342 /* Parse destination register operand.
343 * <register_dst> ::= <register_file_bracket_index> `]'
347 struct translate_ctx
*ctx
,
351 if (!parse_register_file_bracket_index( ctx
, file
, index
))
353 eat_opt_white( &ctx
->cur
);
354 if (*ctx
->cur
!= ']') {
355 report_error( ctx
, "Expected `]'" );
362 /* Parse source register operand.
363 * <register_src> ::= <register_file_bracket_index> `]' |
364 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `]' |
365 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `+' <uint> `]' |
366 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `-' <uint> `]'
370 struct translate_ctx
*ctx
,
380 *ind_comp
= TGSI_SWIZZLE_X
;
381 if (!parse_register_file_bracket( ctx
, file
))
383 eat_opt_white( &ctx
->cur
);
385 if (parse_file( &cur
, ind_file
)) {
386 if (!parse_register_dst( ctx
, ind_file
, ind_index
))
388 eat_opt_white( &ctx
->cur
);
390 if (*ctx
->cur
== '.') {
392 eat_opt_white(&ctx
->cur
);
394 switch (uprcase(*ctx
->cur
)) {
396 *ind_comp
= TGSI_SWIZZLE_X
;
399 *ind_comp
= TGSI_SWIZZLE_Y
;
402 *ind_comp
= TGSI_SWIZZLE_Z
;
405 *ind_comp
= TGSI_SWIZZLE_W
;
408 report_error(ctx
, "Expected indirect register swizzle component `x', `y', `z' or `w'");
412 eat_opt_white(&ctx
->cur
);
415 if (*ctx
->cur
== '+' || *ctx
->cur
== '-') {
418 negate
= *ctx
->cur
== '-';
420 eat_opt_white( &ctx
->cur
);
421 if (!parse_uint( &ctx
->cur
, &uindex
)) {
422 report_error( ctx
, "Expected literal unsigned integer" );
426 *index
= -(int) uindex
;
428 *index
= (int) uindex
;
435 if (!parse_uint( &ctx
->cur
, &uindex
)) {
436 report_error( ctx
, "Expected literal unsigned integer" );
439 *index
= (int) uindex
;
440 *ind_file
= TGSI_FILE_NULL
;
443 eat_opt_white( &ctx
->cur
);
444 if (*ctx
->cur
!= ']') {
445 report_error( ctx
, "Expected `]'" );
452 /* Parse register declaration.
453 * <register_dcl> ::= <register_file_bracket_index> `]' |
454 * <register_file_bracket_index> `..' <index> `]'
458 struct translate_ctx
*ctx
,
463 if (!parse_register_file_bracket_index( ctx
, file
, first
))
465 eat_opt_white( &ctx
->cur
);
466 if (ctx
->cur
[0] == '.' && ctx
->cur
[1] == '.') {
470 eat_opt_white( &ctx
->cur
);
471 if (!parse_uint( &ctx
->cur
, &uindex
)) {
472 report_error( ctx
, "Expected literal integer" );
475 *last
= (int) uindex
;
476 eat_opt_white( &ctx
->cur
);
481 if (*ctx
->cur
!= ']') {
482 report_error( ctx
, "Expected `]' or `..'" );
492 struct translate_ctx
*ctx
,
493 struct tgsi_full_dst_register
*dst
)
500 if (!parse_register_dst( ctx
, &file
, &index
))
504 eat_opt_white( &cur
);
506 if (!parse_opt_writemask( ctx
, &writemask
))
509 dst
->DstRegister
.File
= file
;
510 dst
->DstRegister
.Index
= index
;
511 dst
->DstRegister
.WriteMask
= writemask
;
516 parse_optional_swizzle(
517 struct translate_ctx
*ctx
,
519 boolean
*parsed_swizzle
)
521 const char *cur
= ctx
->cur
;
523 *parsed_swizzle
= FALSE
;
525 eat_opt_white( &cur
);
530 eat_opt_white( &cur
);
531 for (i
= 0; i
< 4; i
++) {
532 if (uprcase( *cur
) == 'X')
533 swizzle
[i
] = TGSI_SWIZZLE_X
;
534 else if (uprcase( *cur
) == 'Y')
535 swizzle
[i
] = TGSI_SWIZZLE_Y
;
536 else if (uprcase( *cur
) == 'Z')
537 swizzle
[i
] = TGSI_SWIZZLE_Z
;
538 else if (uprcase( *cur
) == 'W')
539 swizzle
[i
] = TGSI_SWIZZLE_W
;
541 report_error( ctx
, "Expected register swizzle component `x', `y', `z', `w', `0' or `1'" );
546 *parsed_swizzle
= TRUE
;
554 struct translate_ctx
*ctx
,
555 struct tgsi_full_src_register
*src
)
563 boolean parsed_swizzle
;
565 if (*ctx
->cur
== '-') {
567 eat_opt_white( &ctx
->cur
);
568 src
->SrcRegister
.Negate
= 1;
571 if (*ctx
->cur
== '|') {
573 eat_opt_white( &ctx
->cur
);
574 src
->SrcRegister
.Absolute
= 1;
577 if (!parse_register_src(ctx
, &file
, &index
, &ind_file
, &ind_index
, &ind_comp
))
579 src
->SrcRegister
.File
= file
;
580 src
->SrcRegister
.Index
= index
;
581 if (ind_file
!= TGSI_FILE_NULL
) {
582 src
->SrcRegister
.Indirect
= 1;
583 src
->SrcRegisterInd
.File
= ind_file
;
584 src
->SrcRegisterInd
.Index
= ind_index
;
585 src
->SrcRegisterInd
.SwizzleX
= ind_comp
;
586 src
->SrcRegisterInd
.SwizzleY
= ind_comp
;
587 src
->SrcRegisterInd
.SwizzleZ
= ind_comp
;
588 src
->SrcRegisterInd
.SwizzleW
= ind_comp
;
591 /* Parse optional swizzle.
593 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
)) {
594 if (parsed_swizzle
) {
595 src
->SrcRegister
.SwizzleX
= swizzle
[0];
596 src
->SrcRegister
.SwizzleY
= swizzle
[1];
597 src
->SrcRegister
.SwizzleZ
= swizzle
[2];
598 src
->SrcRegister
.SwizzleW
= swizzle
[3];
602 if (src
->SrcRegister
.Absolute
) {
603 eat_opt_white( &ctx
->cur
);
604 if (*ctx
->cur
!= '|') {
605 report_error( ctx
, "Expected `|'" );
615 static const char *texture_names
[TGSI_TEXTURE_COUNT
] =
629 match_inst_mnemonic(const char **pcur
,
630 const struct tgsi_opcode_info
*info
)
632 if (str_match_no_case(pcur
, info
->mnemonic
)) {
640 struct translate_ctx
*ctx
,
644 uint saturate
= TGSI_SAT_NONE
;
645 const struct tgsi_opcode_info
*info
;
646 struct tgsi_full_instruction inst
;
649 /* Parse instruction name.
651 eat_opt_white( &ctx
->cur
);
652 for (i
= 0; i
< TGSI_OPCODE_LAST
; i
++) {
653 const char *cur
= ctx
->cur
;
655 info
= tgsi_get_opcode_info( i
);
656 if (match_inst_mnemonic(&cur
, info
)) {
657 if (str_match_no_case( &cur
, "_SATNV" ))
658 saturate
= TGSI_SAT_MINUS_PLUS_ONE
;
659 else if (str_match_no_case( &cur
, "_SAT" ))
660 saturate
= TGSI_SAT_ZERO_ONE
;
662 if (info
->num_dst
+ info
->num_src
+ info
->is_tex
== 0) {
663 if (!is_digit_alpha_underscore( cur
)) {
668 else if (*cur
== '\0' || eat_white( &cur
)) {
674 if (i
== TGSI_OPCODE_LAST
) {
676 report_error( ctx
, "Unknown opcode" );
678 report_error( ctx
, "Expected `DCL', `IMM' or a label" );
682 inst
= tgsi_default_full_instruction();
683 inst
.Instruction
.Opcode
= i
;
684 inst
.Instruction
.Saturate
= saturate
;
685 inst
.Instruction
.NumDstRegs
= info
->num_dst
;
686 inst
.Instruction
.NumSrcRegs
= info
->num_src
;
688 /* Parse instruction operands.
690 for (i
= 0; i
< info
->num_dst
+ info
->num_src
+ info
->is_tex
; i
++) {
692 eat_opt_white( &ctx
->cur
);
693 if (*ctx
->cur
!= ',') {
694 report_error( ctx
, "Expected `,'" );
698 eat_opt_white( &ctx
->cur
);
701 if (i
< info
->num_dst
) {
702 if (!parse_dst_operand( ctx
, &inst
.FullDstRegisters
[i
] ))
705 else if (i
< info
->num_dst
+ info
->num_src
) {
706 if (!parse_src_operand( ctx
, &inst
.FullSrcRegisters
[i
- info
->num_dst
] ))
712 for (j
= 0; j
< TGSI_TEXTURE_COUNT
; j
++) {
713 if (str_match_no_case( &ctx
->cur
, texture_names
[j
] )) {
714 if (!is_digit_alpha_underscore( ctx
->cur
)) {
715 inst
.Instruction
.Texture
= 1;
716 inst
.InstructionTexture
.Texture
= j
;
721 if (j
== TGSI_TEXTURE_COUNT
) {
722 report_error( ctx
, "Expected texture target" );
728 if (info
->is_branch
) {
731 eat_opt_white( &ctx
->cur
);
732 if (*ctx
->cur
!= ':') {
733 report_error( ctx
, "Expected `:'" );
737 eat_opt_white( &ctx
->cur
);
738 if (!parse_uint( &ctx
->cur
, &target
)) {
739 report_error( ctx
, "Expected a label" );
742 inst
.Instruction
.Label
= 1;
743 inst
.InstructionLabel
.Label
= target
;
746 advance
= tgsi_build_full_instruction(
750 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
753 ctx
->tokens_cur
+= advance
;
758 static const char *semantic_names
[TGSI_SEMANTIC_COUNT
] =
770 static const char *interpolate_names
[TGSI_INTERPOLATE_COUNT
] =
777 static boolean
parse_declaration( struct translate_ctx
*ctx
)
779 struct tgsi_full_declaration decl
;
787 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
788 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
790 if (!eat_white( &ctx
->cur
)) {
791 report_error( ctx
, "Syntax error" );
794 if (!parse_register_dcl( ctx
, &file
, &first
, &last
))
796 if (!parse_opt_writemask( ctx
, &writemask
))
799 decl
= tgsi_default_full_declaration();
800 decl
.Declaration
.File
= file
;
801 decl
.Declaration
.UsageMask
= writemask
;
802 decl
.DeclarationRange
.First
= first
;
803 decl
.DeclarationRange
.Last
= last
;
806 eat_opt_white( &cur
);
811 eat_opt_white( &cur
);
812 for (i
= 0; i
< TGSI_SEMANTIC_COUNT
; i
++) {
813 if (str_match_no_case( &cur
, semantic_names
[i
] )) {
814 const char *cur2
= cur
;
817 if (is_digit_alpha_underscore( cur
))
819 eat_opt_white( &cur2
);
822 eat_opt_white( &cur2
);
823 if (!parse_uint( &cur2
, &index
)) {
824 report_error( ctx
, "Expected literal integer" );
827 eat_opt_white( &cur2
);
829 report_error( ctx
, "Expected `]'" );
834 decl
.Semantic
.Index
= index
;
839 decl
.Declaration
.Semantic
= 1;
840 decl
.Semantic
.Name
= i
;
849 eat_opt_white( &cur
);
854 eat_opt_white( &cur
);
855 for (i
= 0; i
< TGSI_INTERPOLATE_COUNT
; i
++) {
856 if (str_match_no_case( &cur
, interpolate_names
[i
] )) {
857 if (is_digit_alpha_underscore( cur
))
859 decl
.Declaration
.Interpolate
= i
;
865 if (i
== TGSI_INTERPOLATE_COUNT
) {
866 report_error( ctx
, "Expected semantic or interpolate attribute" );
871 advance
= tgsi_build_full_declaration(
875 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
878 ctx
->tokens_cur
+= advance
;
883 static boolean
parse_immediate( struct translate_ctx
*ctx
)
885 struct tgsi_full_immediate imm
;
890 if (!eat_white( &ctx
->cur
)) {
891 report_error( ctx
, "Syntax error" );
894 if (!str_match_no_case( &ctx
->cur
, "FLT32" ) || is_digit_alpha_underscore( ctx
->cur
)) {
895 report_error( ctx
, "Expected `FLT32'" );
898 eat_opt_white( &ctx
->cur
);
899 if (*ctx
->cur
!= '{') {
900 report_error( ctx
, "Expected `{'" );
904 for (i
= 0; i
< 4; i
++) {
905 eat_opt_white( &ctx
->cur
);
907 if (*ctx
->cur
!= ',') {
908 report_error( ctx
, "Expected `,'" );
912 eat_opt_white( &ctx
->cur
);
914 if (!parse_float( &ctx
->cur
, &values
[i
] )) {
915 report_error( ctx
, "Expected literal floating point" );
919 eat_opt_white( &ctx
->cur
);
920 if (*ctx
->cur
!= '}') {
921 report_error( ctx
, "Expected `}'" );
926 imm
= tgsi_default_full_immediate();
927 imm
.Immediate
.NrTokens
+= 4;
928 imm
.Immediate
.DataType
= TGSI_IMM_FLOAT32
;
929 imm
.u
[0].Float
= values
[0];
930 imm
.u
[1].Float
= values
[1];
931 imm
.u
[2].Float
= values
[2];
932 imm
.u
[3].Float
= values
[3];
934 advance
= tgsi_build_full_immediate(
938 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
941 ctx
->tokens_cur
+= advance
;
946 static boolean
translate( struct translate_ctx
*ctx
)
948 eat_opt_white( &ctx
->cur
);
949 if (!parse_header( ctx
))
952 while (*ctx
->cur
!= '\0') {
955 if (!eat_white( &ctx
->cur
)) {
956 report_error( ctx
, "Syntax error" );
960 if (*ctx
->cur
== '\0')
963 if (parse_label( ctx
, &label_val
)) {
964 if (!parse_instruction( ctx
, TRUE
))
967 else if (str_match_no_case( &ctx
->cur
, "DCL" )) {
968 if (!parse_declaration( ctx
))
971 else if (str_match_no_case( &ctx
->cur
, "IMM" )) {
972 if (!parse_immediate( ctx
))
975 else if (!parse_instruction( ctx
, FALSE
)) {
986 struct tgsi_token
*tokens
,
989 struct translate_ctx ctx
;
994 ctx
.tokens_cur
= tokens
;
995 ctx
.tokens_end
= tokens
+ num_tokens
;
997 if (!translate( &ctx
))
1000 return tgsi_sanity_check( tokens
);