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
, "FRAG" ))
184 processor
= TGSI_PROCESSOR_FRAGMENT
;
185 else if (str_match_no_case( &ctx
->cur
, "VERT" ))
186 processor
= TGSI_PROCESSOR_VERTEX
;
187 else if (str_match_no_case( &ctx
->cur
, "GEOM" ))
188 processor
= TGSI_PROCESSOR_GEOMETRY
;
190 report_error( ctx
, "Unknown header" );
194 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
196 ctx
->header
= (struct tgsi_header
*) ctx
->tokens_cur
++;
197 *ctx
->header
= tgsi_build_header();
199 if (ctx
->tokens_cur
>= ctx
->tokens_end
)
201 *(struct tgsi_processor
*) ctx
->tokens_cur
++ = tgsi_build_processor( processor
, ctx
->header
);
206 static boolean
parse_label( struct translate_ctx
*ctx
, uint
*val
)
208 const char *cur
= ctx
->cur
;
210 if (parse_uint( &cur
, val
)) {
211 eat_opt_white( &cur
);
221 static const char *file_names
[TGSI_FILE_COUNT
] =
236 parse_file( const char **pcur
, uint
*file
)
240 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++) {
241 const char *cur
= *pcur
;
243 if (str_match_no_case( &cur
, file_names
[i
] )) {
244 if (!is_digit_alpha_underscore( cur
)) {
256 struct translate_ctx
*ctx
,
262 eat_opt_white( &cur
);
265 *writemask
= TGSI_WRITEMASK_NONE
;
266 eat_opt_white( &cur
);
267 if (uprcase( *cur
) == 'X') {
269 *writemask
|= TGSI_WRITEMASK_X
;
271 if (uprcase( *cur
) == 'Y') {
273 *writemask
|= TGSI_WRITEMASK_Y
;
275 if (uprcase( *cur
) == 'Z') {
277 *writemask
|= TGSI_WRITEMASK_Z
;
279 if (uprcase( *cur
) == 'W') {
281 *writemask
|= TGSI_WRITEMASK_W
;
284 if (*writemask
== TGSI_WRITEMASK_NONE
) {
285 report_error( ctx
, "Writemask expected" );
292 *writemask
= TGSI_WRITEMASK_XYZW
;
297 /* <register_file_bracket> ::= <file> `['
300 parse_register_file_bracket(
301 struct translate_ctx
*ctx
,
304 if (!parse_file( &ctx
->cur
, file
)) {
305 report_error( ctx
, "Unknown register file" );
308 eat_opt_white( &ctx
->cur
);
309 if (*ctx
->cur
!= '[') {
310 report_error( ctx
, "Expected `['" );
317 /* <register_file_bracket_index> ::= <register_file_bracket> <uint>
320 parse_register_file_bracket_index(
321 struct translate_ctx
*ctx
,
327 if (!parse_register_file_bracket( ctx
, file
))
329 eat_opt_white( &ctx
->cur
);
330 if (!parse_uint( &ctx
->cur
, &uindex
)) {
331 report_error( ctx
, "Expected literal unsigned integer" );
334 *index
= (int) uindex
;
338 /* Parse destination register operand.
339 * <register_dst> ::= <register_file_bracket_index> `]'
343 struct translate_ctx
*ctx
,
347 if (!parse_register_file_bracket_index( ctx
, file
, index
))
349 eat_opt_white( &ctx
->cur
);
350 if (*ctx
->cur
!= ']') {
351 report_error( ctx
, "Expected `]'" );
358 /* Parse source register operand.
359 * <register_src> ::= <register_file_bracket_index> `]' |
360 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `]' |
361 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `+' <uint> `]' |
362 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `-' <uint> `]'
366 struct translate_ctx
*ctx
,
376 *ind_comp
= TGSI_SWIZZLE_X
;
377 if (!parse_register_file_bracket( ctx
, file
))
379 eat_opt_white( &ctx
->cur
);
381 if (parse_file( &cur
, ind_file
)) {
382 if (!parse_register_dst( ctx
, ind_file
, ind_index
))
384 eat_opt_white( &ctx
->cur
);
386 if (*ctx
->cur
== '.') {
388 eat_opt_white(&ctx
->cur
);
390 switch (uprcase(*ctx
->cur
)) {
392 *ind_comp
= TGSI_SWIZZLE_X
;
395 *ind_comp
= TGSI_SWIZZLE_Y
;
398 *ind_comp
= TGSI_SWIZZLE_Z
;
401 *ind_comp
= TGSI_SWIZZLE_W
;
404 report_error(ctx
, "Expected indirect register swizzle component `x', `y', `z' or `w'");
408 eat_opt_white(&ctx
->cur
);
411 if (*ctx
->cur
== '+' || *ctx
->cur
== '-') {
414 negate
= *ctx
->cur
== '-';
416 eat_opt_white( &ctx
->cur
);
417 if (!parse_uint( &ctx
->cur
, &uindex
)) {
418 report_error( ctx
, "Expected literal unsigned integer" );
422 *index
= -(int) uindex
;
424 *index
= (int) uindex
;
431 if (!parse_uint( &ctx
->cur
, &uindex
)) {
432 report_error( ctx
, "Expected literal unsigned integer" );
435 *index
= (int) uindex
;
436 *ind_file
= TGSI_FILE_NULL
;
439 eat_opt_white( &ctx
->cur
);
440 if (*ctx
->cur
!= ']') {
441 report_error( ctx
, "Expected `]'" );
448 /* Parse register declaration.
449 * <register_dcl> ::= <register_file_bracket_index> `]' |
450 * <register_file_bracket_index> `..' <index> `]'
454 struct translate_ctx
*ctx
,
459 if (!parse_register_file_bracket_index( ctx
, file
, first
))
461 eat_opt_white( &ctx
->cur
);
462 if (ctx
->cur
[0] == '.' && ctx
->cur
[1] == '.') {
466 eat_opt_white( &ctx
->cur
);
467 if (!parse_uint( &ctx
->cur
, &uindex
)) {
468 report_error( ctx
, "Expected literal integer" );
471 *last
= (int) uindex
;
472 eat_opt_white( &ctx
->cur
);
477 if (*ctx
->cur
!= ']') {
478 report_error( ctx
, "Expected `]' or `..'" );
488 struct translate_ctx
*ctx
,
489 struct tgsi_full_dst_register
*dst
)
496 if (!parse_register_dst( ctx
, &file
, &index
))
500 eat_opt_white( &cur
);
502 if (!parse_opt_writemask( ctx
, &writemask
))
505 dst
->Register
.File
= file
;
506 dst
->Register
.Index
= index
;
507 dst
->Register
.WriteMask
= writemask
;
512 parse_optional_swizzle(
513 struct translate_ctx
*ctx
,
515 boolean
*parsed_swizzle
)
517 const char *cur
= ctx
->cur
;
519 *parsed_swizzle
= FALSE
;
521 eat_opt_white( &cur
);
526 eat_opt_white( &cur
);
527 for (i
= 0; i
< 4; i
++) {
528 if (uprcase( *cur
) == 'X')
529 swizzle
[i
] = TGSI_SWIZZLE_X
;
530 else if (uprcase( *cur
) == 'Y')
531 swizzle
[i
] = TGSI_SWIZZLE_Y
;
532 else if (uprcase( *cur
) == 'Z')
533 swizzle
[i
] = TGSI_SWIZZLE_Z
;
534 else if (uprcase( *cur
) == 'W')
535 swizzle
[i
] = TGSI_SWIZZLE_W
;
537 report_error( ctx
, "Expected register swizzle component `x', `y', `z', `w', `0' or `1'" );
542 *parsed_swizzle
= TRUE
;
550 struct translate_ctx
*ctx
,
551 struct tgsi_full_src_register
*src
)
559 boolean parsed_swizzle
;
561 if (*ctx
->cur
== '-') {
563 eat_opt_white( &ctx
->cur
);
564 src
->Register
.Negate
= 1;
567 if (*ctx
->cur
== '|') {
569 eat_opt_white( &ctx
->cur
);
570 src
->Register
.Absolute
= 1;
573 if (!parse_register_src(ctx
, &file
, &index
, &ind_file
, &ind_index
, &ind_comp
))
575 src
->Register
.File
= file
;
576 src
->Register
.Index
= index
;
577 if (ind_file
!= TGSI_FILE_NULL
) {
578 src
->Register
.Indirect
= 1;
579 src
->Indirect
.File
= ind_file
;
580 src
->Indirect
.Index
= ind_index
;
581 src
->Indirect
.SwizzleX
= ind_comp
;
582 src
->Indirect
.SwizzleY
= ind_comp
;
583 src
->Indirect
.SwizzleZ
= ind_comp
;
584 src
->Indirect
.SwizzleW
= ind_comp
;
587 /* Parse optional swizzle.
589 if (parse_optional_swizzle( ctx
, swizzle
, &parsed_swizzle
)) {
590 if (parsed_swizzle
) {
591 src
->Register
.SwizzleX
= swizzle
[0];
592 src
->Register
.SwizzleY
= swizzle
[1];
593 src
->Register
.SwizzleZ
= swizzle
[2];
594 src
->Register
.SwizzleW
= swizzle
[3];
598 if (src
->Register
.Absolute
) {
599 eat_opt_white( &ctx
->cur
);
600 if (*ctx
->cur
!= '|') {
601 report_error( ctx
, "Expected `|'" );
611 static const char *texture_names
[TGSI_TEXTURE_COUNT
] =
625 match_inst_mnemonic(const char **pcur
,
626 const struct tgsi_opcode_info
*info
)
628 if (str_match_no_case(pcur
, info
->mnemonic
)) {
636 struct translate_ctx
*ctx
,
640 uint saturate
= TGSI_SAT_NONE
;
641 const struct tgsi_opcode_info
*info
;
642 struct tgsi_full_instruction inst
;
645 /* Parse instruction name.
647 eat_opt_white( &ctx
->cur
);
648 for (i
= 0; i
< TGSI_OPCODE_LAST
; i
++) {
649 const char *cur
= ctx
->cur
;
651 info
= tgsi_get_opcode_info( i
);
652 if (match_inst_mnemonic(&cur
, info
)) {
653 if (str_match_no_case( &cur
, "_SATNV" ))
654 saturate
= TGSI_SAT_MINUS_PLUS_ONE
;
655 else if (str_match_no_case( &cur
, "_SAT" ))
656 saturate
= TGSI_SAT_ZERO_ONE
;
658 if (info
->num_dst
+ info
->num_src
+ info
->is_tex
== 0) {
659 if (!is_digit_alpha_underscore( cur
)) {
664 else if (*cur
== '\0' || eat_white( &cur
)) {
670 if (i
== TGSI_OPCODE_LAST
) {
672 report_error( ctx
, "Unknown opcode" );
674 report_error( ctx
, "Expected `DCL', `IMM' or a label" );
678 inst
= tgsi_default_full_instruction();
679 inst
.Instruction
.Opcode
= i
;
680 inst
.Instruction
.Saturate
= saturate
;
681 inst
.Instruction
.NumDstRegs
= info
->num_dst
;
682 inst
.Instruction
.NumSrcRegs
= info
->num_src
;
684 /* Parse instruction operands.
686 for (i
= 0; i
< info
->num_dst
+ info
->num_src
+ info
->is_tex
; i
++) {
688 eat_opt_white( &ctx
->cur
);
689 if (*ctx
->cur
!= ',') {
690 report_error( ctx
, "Expected `,'" );
694 eat_opt_white( &ctx
->cur
);
697 if (i
< info
->num_dst
) {
698 if (!parse_dst_operand( ctx
, &inst
.Dst
[i
] ))
701 else if (i
< info
->num_dst
+ info
->num_src
) {
702 if (!parse_src_operand( ctx
, &inst
.Src
[i
- info
->num_dst
] ))
708 for (j
= 0; j
< TGSI_TEXTURE_COUNT
; j
++) {
709 if (str_match_no_case( &ctx
->cur
, texture_names
[j
] )) {
710 if (!is_digit_alpha_underscore( ctx
->cur
)) {
711 inst
.Instruction
.Texture
= 1;
712 inst
.Texture
.Texture
= j
;
717 if (j
== TGSI_TEXTURE_COUNT
) {
718 report_error( ctx
, "Expected texture target" );
724 if (info
->is_branch
) {
727 eat_opt_white( &ctx
->cur
);
728 if (*ctx
->cur
!= ':') {
729 report_error( ctx
, "Expected `:'" );
733 eat_opt_white( &ctx
->cur
);
734 if (!parse_uint( &ctx
->cur
, &target
)) {
735 report_error( ctx
, "Expected a label" );
738 inst
.Instruction
.Label
= 1;
739 inst
.Label
.Label
= target
;
742 advance
= tgsi_build_full_instruction(
746 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
749 ctx
->tokens_cur
+= advance
;
754 static const char *semantic_names
[TGSI_SEMANTIC_COUNT
] =
766 static const char *interpolate_names
[TGSI_INTERPOLATE_COUNT
] =
773 static boolean
parse_declaration( struct translate_ctx
*ctx
)
775 struct tgsi_full_declaration decl
;
783 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
784 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
786 if (!eat_white( &ctx
->cur
)) {
787 report_error( ctx
, "Syntax error" );
790 if (!parse_register_dcl( ctx
, &file
, &first
, &last
))
792 if (!parse_opt_writemask( ctx
, &writemask
))
795 decl
= tgsi_default_full_declaration();
796 decl
.Declaration
.File
= file
;
797 decl
.Declaration
.UsageMask
= writemask
;
798 decl
.Range
.First
= first
;
799 decl
.Range
.Last
= last
;
802 eat_opt_white( &cur
);
807 eat_opt_white( &cur
);
808 for (i
= 0; i
< TGSI_SEMANTIC_COUNT
; i
++) {
809 if (str_match_no_case( &cur
, semantic_names
[i
] )) {
810 const char *cur2
= cur
;
813 if (is_digit_alpha_underscore( cur
))
815 eat_opt_white( &cur2
);
818 eat_opt_white( &cur2
);
819 if (!parse_uint( &cur2
, &index
)) {
820 report_error( ctx
, "Expected literal integer" );
823 eat_opt_white( &cur2
);
825 report_error( ctx
, "Expected `]'" );
830 decl
.Semantic
.Index
= index
;
835 decl
.Declaration
.Semantic
= 1;
836 decl
.Semantic
.Name
= i
;
845 eat_opt_white( &cur
);
850 eat_opt_white( &cur
);
851 for (i
= 0; i
< TGSI_INTERPOLATE_COUNT
; i
++) {
852 if (str_match_no_case( &cur
, interpolate_names
[i
] )) {
853 if (is_digit_alpha_underscore( cur
))
855 decl
.Declaration
.Interpolate
= i
;
861 if (i
== TGSI_INTERPOLATE_COUNT
) {
862 report_error( ctx
, "Expected semantic or interpolate attribute" );
867 advance
= tgsi_build_full_declaration(
871 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
874 ctx
->tokens_cur
+= advance
;
879 static boolean
parse_immediate( struct translate_ctx
*ctx
)
881 struct tgsi_full_immediate imm
;
886 if (!eat_white( &ctx
->cur
)) {
887 report_error( ctx
, "Syntax error" );
890 if (!str_match_no_case( &ctx
->cur
, "FLT32" ) || is_digit_alpha_underscore( ctx
->cur
)) {
891 report_error( ctx
, "Expected `FLT32'" );
894 eat_opt_white( &ctx
->cur
);
895 if (*ctx
->cur
!= '{') {
896 report_error( ctx
, "Expected `{'" );
900 for (i
= 0; i
< 4; i
++) {
901 eat_opt_white( &ctx
->cur
);
903 if (*ctx
->cur
!= ',') {
904 report_error( ctx
, "Expected `,'" );
908 eat_opt_white( &ctx
->cur
);
910 if (!parse_float( &ctx
->cur
, &values
[i
] )) {
911 report_error( ctx
, "Expected literal floating point" );
915 eat_opt_white( &ctx
->cur
);
916 if (*ctx
->cur
!= '}') {
917 report_error( ctx
, "Expected `}'" );
922 imm
= tgsi_default_full_immediate();
923 imm
.Immediate
.NrTokens
+= 4;
924 imm
.Immediate
.DataType
= TGSI_IMM_FLOAT32
;
925 imm
.u
[0].Float
= values
[0];
926 imm
.u
[1].Float
= values
[1];
927 imm
.u
[2].Float
= values
[2];
928 imm
.u
[3].Float
= values
[3];
930 advance
= tgsi_build_full_immediate(
934 (uint
) (ctx
->tokens_end
- ctx
->tokens_cur
) );
937 ctx
->tokens_cur
+= advance
;
942 static boolean
translate( struct translate_ctx
*ctx
)
944 eat_opt_white( &ctx
->cur
);
945 if (!parse_header( ctx
))
948 while (*ctx
->cur
!= '\0') {
951 if (!eat_white( &ctx
->cur
)) {
952 report_error( ctx
, "Syntax error" );
956 if (*ctx
->cur
== '\0')
959 if (parse_label( ctx
, &label_val
)) {
960 if (!parse_instruction( ctx
, TRUE
))
963 else if (str_match_no_case( &ctx
->cur
, "DCL" )) {
964 if (!parse_declaration( ctx
))
967 else if (str_match_no_case( &ctx
->cur
, "IMM" )) {
968 if (!parse_immediate( ctx
))
971 else if (!parse_instruction( ctx
, FALSE
)) {
982 struct tgsi_token
*tokens
,
985 struct translate_ctx ctx
;
990 ctx
.tokens_cur
= tokens
;
991 ctx
.tokens_end
= tokens
+ num_tokens
;
993 if (!translate( &ctx
))
996 return tgsi_sanity_check( tokens
);