tgsi: support parsing texture offsets from text tgsi shaders
[mesa.git] / src / gallium / auxiliary / tgsi / tgsi_text.c
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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 VMWARE 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.
25 *
26 **************************************************************************/
27
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_strings.h"
39 #include "tgsi_util.h"
40 #include "tgsi_dump.h"
41
42 static boolean is_alpha_underscore( const char *cur )
43 {
44 return
45 (*cur >= 'a' && *cur <= 'z') ||
46 (*cur >= 'A' && *cur <= 'Z') ||
47 *cur == '_';
48 }
49
50 static boolean is_digit( const char *cur )
51 {
52 return *cur >= '0' && *cur <= '9';
53 }
54
55 static boolean is_digit_alpha_underscore( const char *cur )
56 {
57 return is_digit( cur ) || is_alpha_underscore( cur );
58 }
59
60 static char uprcase( char c )
61 {
62 if (c >= 'a' && c <= 'z')
63 return c + 'A' - 'a';
64 return c;
65 }
66
67 /*
68 * Ignore case of str1 and assume str1 is already uppercase.
69 * Return TRUE iff str1 and str2 are equal.
70 */
71 static int
72 streq_nocase_uprcase(const char *str1,
73 const char *str2)
74 {
75 while (*str1 && *str2) {
76 if (*str1 != uprcase(*str2))
77 return FALSE;
78 str1++;
79 str2++;
80 }
81 return *str1 == 0 && *str2 == 0;
82 }
83
84 /* Return TRUE if both strings match.
85 * The second string is terminated by zero.
86 * The pointer to the first string is moved at end of the read word
87 * on success.
88 */
89 static boolean str_match_no_case( const char **pcur, const char *str )
90 {
91 const char *cur = *pcur;
92
93 while (*str != '\0' && *str == uprcase( *cur )) {
94 str++;
95 cur++;
96 }
97 if (*str == '\0') {
98 *pcur = cur;
99 return TRUE;
100 }
101 return FALSE;
102 }
103
104 /* Return TRUE if both strings match.
105 * The first string is be terminated by a non-digit non-letter non-underscore
106 * character, the second string is terminated by zero.
107 * The pointer to the first string is moved at end of the read word
108 * on success.
109 */
110 static boolean str_match_nocase_whole( const char **pcur, const char *str )
111 {
112 const char *cur = *pcur;
113
114 if (str_match_no_case(&cur, str) &&
115 !is_digit_alpha_underscore(cur)) {
116 *pcur = cur;
117 return TRUE;
118 }
119 return FALSE;
120 }
121
122 /* Eat zero or more whitespaces.
123 */
124 static void eat_opt_white( const char **pcur )
125 {
126 while (**pcur == ' ' || **pcur == '\t' || **pcur == '\n')
127 (*pcur)++;
128 }
129
130 /* Eat one or more whitespaces.
131 * Return TRUE if at least one whitespace eaten.
132 */
133 static boolean eat_white( const char **pcur )
134 {
135 const char *cur = *pcur;
136
137 eat_opt_white( pcur );
138 return *pcur > cur;
139 }
140
141 /* Parse unsigned integer.
142 * No checks for overflow.
143 */
144 static boolean parse_uint( const char **pcur, uint *val )
145 {
146 const char *cur = *pcur;
147
148 if (is_digit( cur )) {
149 *val = *cur++ - '0';
150 while (is_digit( cur ))
151 *val = *val * 10 + *cur++ - '0';
152 *pcur = cur;
153 return TRUE;
154 }
155 return FALSE;
156 }
157
158 static boolean parse_int( const char **pcur, int *val )
159 {
160 const char *cur = *pcur;
161 int sign = (*cur == '-' ? -1 : 1);
162
163 if (*cur == '+' || *cur == '-')
164 cur++;
165
166 if (parse_uint(&cur, (uint *)val)) {
167 *val *= sign;
168 *pcur = cur;
169 return TRUE;
170 }
171
172 return FALSE;
173 }
174
175 static boolean parse_identifier( const char **pcur, char *ret )
176 {
177 const char *cur = *pcur;
178 int i = 0;
179 if (is_alpha_underscore( cur )) {
180 ret[i++] = *cur++;
181 while (is_alpha_underscore( cur ) || is_digit( cur ))
182 ret[i++] = *cur++;
183 ret[i++] = '\0';
184 *pcur = cur;
185 return TRUE;
186 }
187 return FALSE;
188 }
189
190 /* Parse floating point.
191 */
192 static boolean parse_float( const char **pcur, float *val )
193 {
194 const char *cur = *pcur;
195 boolean integral_part = FALSE;
196 boolean fractional_part = FALSE;
197
198 *val = (float) atof( cur );
199
200 if (*cur == '-' || *cur == '+')
201 cur++;
202 if (is_digit( cur )) {
203 cur++;
204 integral_part = TRUE;
205 while (is_digit( cur ))
206 cur++;
207 }
208 if (*cur == '.') {
209 cur++;
210 if (is_digit( cur )) {
211 cur++;
212 fractional_part = TRUE;
213 while (is_digit( cur ))
214 cur++;
215 }
216 }
217 if (!integral_part && !fractional_part)
218 return FALSE;
219 if (uprcase( *cur ) == 'E') {
220 cur++;
221 if (*cur == '-' || *cur == '+')
222 cur++;
223 if (is_digit( cur )) {
224 cur++;
225 while (is_digit( cur ))
226 cur++;
227 }
228 else
229 return FALSE;
230 }
231 *pcur = cur;
232 return TRUE;
233 }
234
235 struct translate_ctx
236 {
237 const char *text;
238 const char *cur;
239 struct tgsi_token *tokens;
240 struct tgsi_token *tokens_cur;
241 struct tgsi_token *tokens_end;
242 struct tgsi_header *header;
243 unsigned processor : 4;
244 int implied_array_size : 5;
245 unsigned num_immediates;
246 };
247
248 static void report_error( struct translate_ctx *ctx, const char *msg )
249 {
250 int line = 1;
251 int column = 1;
252 const char *itr = ctx->text;
253
254 while (itr != ctx->cur) {
255 if (*itr == '\n') {
256 column = 1;
257 ++line;
258 }
259 ++column;
260 ++itr;
261 }
262
263 debug_printf( "\nTGSI asm error: %s [%d : %d] \n", msg, line, column );
264 }
265
266 /* Parse shader header.
267 * Return TRUE for one of the following headers.
268 * FRAG
269 * GEOM
270 * VERT
271 */
272 static boolean parse_header( struct translate_ctx *ctx )
273 {
274 uint processor;
275
276 if (str_match_nocase_whole( &ctx->cur, "FRAG" ))
277 processor = TGSI_PROCESSOR_FRAGMENT;
278 else if (str_match_nocase_whole( &ctx->cur, "VERT" ))
279 processor = TGSI_PROCESSOR_VERTEX;
280 else if (str_match_nocase_whole( &ctx->cur, "GEOM" ))
281 processor = TGSI_PROCESSOR_GEOMETRY;
282 else if (str_match_nocase_whole( &ctx->cur, "COMP" ))
283 processor = TGSI_PROCESSOR_COMPUTE;
284 else {
285 report_error( ctx, "Unknown header" );
286 return FALSE;
287 }
288
289 if (ctx->tokens_cur >= ctx->tokens_end)
290 return FALSE;
291 ctx->header = (struct tgsi_header *) ctx->tokens_cur++;
292 *ctx->header = tgsi_build_header();
293
294 if (ctx->tokens_cur >= ctx->tokens_end)
295 return FALSE;
296 *(struct tgsi_processor *) ctx->tokens_cur++ = tgsi_build_processor( processor, ctx->header );
297 ctx->processor = processor;
298
299 return TRUE;
300 }
301
302 static boolean parse_label( struct translate_ctx *ctx, uint *val )
303 {
304 const char *cur = ctx->cur;
305
306 if (parse_uint( &cur, val )) {
307 eat_opt_white( &cur );
308 if (*cur == ':') {
309 cur++;
310 ctx->cur = cur;
311 return TRUE;
312 }
313 }
314 return FALSE;
315 }
316
317 static boolean
318 parse_file( const char **pcur, uint *file )
319 {
320 uint i;
321
322 for (i = 0; i < TGSI_FILE_COUNT; i++) {
323 const char *cur = *pcur;
324
325 if (str_match_nocase_whole( &cur, tgsi_file_name(i) )) {
326 *pcur = cur;
327 *file = i;
328 return TRUE;
329 }
330 }
331 return FALSE;
332 }
333
334 static boolean
335 parse_opt_writemask(
336 struct translate_ctx *ctx,
337 uint *writemask )
338 {
339 const char *cur;
340
341 cur = ctx->cur;
342 eat_opt_white( &cur );
343 if (*cur == '.') {
344 cur++;
345 *writemask = TGSI_WRITEMASK_NONE;
346 eat_opt_white( &cur );
347 if (uprcase( *cur ) == 'X') {
348 cur++;
349 *writemask |= TGSI_WRITEMASK_X;
350 }
351 if (uprcase( *cur ) == 'Y') {
352 cur++;
353 *writemask |= TGSI_WRITEMASK_Y;
354 }
355 if (uprcase( *cur ) == 'Z') {
356 cur++;
357 *writemask |= TGSI_WRITEMASK_Z;
358 }
359 if (uprcase( *cur ) == 'W') {
360 cur++;
361 *writemask |= TGSI_WRITEMASK_W;
362 }
363
364 if (*writemask == TGSI_WRITEMASK_NONE) {
365 report_error( ctx, "Writemask expected" );
366 return FALSE;
367 }
368
369 ctx->cur = cur;
370 }
371 else {
372 *writemask = TGSI_WRITEMASK_XYZW;
373 }
374 return TRUE;
375 }
376
377
378 /* <register_file_bracket> ::= <file> `['
379 */
380 static boolean
381 parse_register_file_bracket(
382 struct translate_ctx *ctx,
383 uint *file )
384 {
385 if (!parse_file( &ctx->cur, file )) {
386 report_error( ctx, "Unknown register file" );
387 return FALSE;
388 }
389 eat_opt_white( &ctx->cur );
390 if (*ctx->cur != '[') {
391 report_error( ctx, "Expected `['" );
392 return FALSE;
393 }
394 ctx->cur++;
395 return TRUE;
396 }
397
398 /* <register_file_bracket_index> ::= <register_file_bracket> <uint>
399 */
400 static boolean
401 parse_register_file_bracket_index(
402 struct translate_ctx *ctx,
403 uint *file,
404 int *index )
405 {
406 uint uindex;
407
408 if (!parse_register_file_bracket( ctx, file ))
409 return FALSE;
410 eat_opt_white( &ctx->cur );
411 if (!parse_uint( &ctx->cur, &uindex )) {
412 report_error( ctx, "Expected literal unsigned integer" );
413 return FALSE;
414 }
415 *index = (int) uindex;
416 return TRUE;
417 }
418
419 /* Parse simple 1d register operand.
420 * <register_dst> ::= <register_file_bracket_index> `]'
421 */
422 static boolean
423 parse_register_1d(struct translate_ctx *ctx,
424 uint *file,
425 int *index )
426 {
427 if (!parse_register_file_bracket_index( ctx, file, index ))
428 return FALSE;
429 eat_opt_white( &ctx->cur );
430 if (*ctx->cur != ']') {
431 report_error( ctx, "Expected `]'" );
432 return FALSE;
433 }
434 ctx->cur++;
435 return TRUE;
436 }
437
438 struct parsed_bracket {
439 int index;
440
441 uint ind_file;
442 int ind_index;
443 uint ind_comp;
444 uint ind_array;
445 };
446
447
448 static boolean
449 parse_register_bracket(
450 struct translate_ctx *ctx,
451 struct parsed_bracket *brackets)
452 {
453 const char *cur;
454 uint uindex;
455
456 memset(brackets, 0, sizeof(struct parsed_bracket));
457
458 eat_opt_white( &ctx->cur );
459
460 cur = ctx->cur;
461 if (parse_file( &cur, &brackets->ind_file )) {
462 if (!parse_register_1d( ctx, &brackets->ind_file,
463 &brackets->ind_index ))
464 return FALSE;
465 eat_opt_white( &ctx->cur );
466
467 if (*ctx->cur == '.') {
468 ctx->cur++;
469 eat_opt_white(&ctx->cur);
470
471 switch (uprcase(*ctx->cur)) {
472 case 'X':
473 brackets->ind_comp = TGSI_SWIZZLE_X;
474 break;
475 case 'Y':
476 brackets->ind_comp = TGSI_SWIZZLE_Y;
477 break;
478 case 'Z':
479 brackets->ind_comp = TGSI_SWIZZLE_Z;
480 break;
481 case 'W':
482 brackets->ind_comp = TGSI_SWIZZLE_W;
483 break;
484 default:
485 report_error(ctx, "Expected indirect register swizzle component `x', `y', `z' or `w'");
486 return FALSE;
487 }
488 ctx->cur++;
489 eat_opt_white(&ctx->cur);
490 }
491
492 if (*ctx->cur == '+' || *ctx->cur == '-')
493 parse_int( &ctx->cur, &brackets->index );
494 else
495 brackets->index = 0;
496 }
497 else {
498 if (!parse_uint( &ctx->cur, &uindex )) {
499 report_error( ctx, "Expected literal unsigned integer" );
500 return FALSE;
501 }
502 brackets->index = (int) uindex;
503 brackets->ind_file = TGSI_FILE_NULL;
504 brackets->ind_index = 0;
505 }
506 eat_opt_white( &ctx->cur );
507 if (*ctx->cur != ']') {
508 report_error( ctx, "Expected `]'" );
509 return FALSE;
510 }
511 ctx->cur++;
512 if (*ctx->cur == '(') {
513 ctx->cur++;
514 eat_opt_white( &ctx->cur );
515 if (!parse_uint( &ctx->cur, &brackets->ind_array )) {
516 report_error( ctx, "Expected literal unsigned integer" );
517 return FALSE;
518 }
519 eat_opt_white( &ctx->cur );
520 if (*ctx->cur != ')') {
521 report_error( ctx, "Expected `)'" );
522 return FALSE;
523 }
524 ctx->cur++;
525 }
526 return TRUE;
527 }
528
529 static boolean
530 parse_opt_register_src_bracket(
531 struct translate_ctx *ctx,
532 struct parsed_bracket *brackets,
533 int *parsed_brackets)
534 {
535 const char *cur = ctx->cur;
536
537 *parsed_brackets = 0;
538
539 eat_opt_white( &cur );
540 if (cur[0] == '[') {
541 ++cur;
542 ctx->cur = cur;
543
544 if (!parse_register_bracket(ctx, brackets))
545 return FALSE;
546
547 *parsed_brackets = 1;
548 }
549
550 return TRUE;
551 }
552
553
554 /* Parse source register operand.
555 * <register_src> ::= <register_file_bracket_index> `]' |
556 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `]' |
557 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `+' <uint> `]' |
558 * <register_file_bracket> <register_dst> [`.' (`x' | `y' | `z' | `w')] `-' <uint> `]'
559 */
560 static boolean
561 parse_register_src(
562 struct translate_ctx *ctx,
563 uint *file,
564 struct parsed_bracket *brackets)
565 {
566 brackets->ind_comp = TGSI_SWIZZLE_X;
567 if (!parse_register_file_bracket( ctx, file ))
568 return FALSE;
569 if (!parse_register_bracket( ctx, brackets ))
570 return FALSE;
571
572 return TRUE;
573 }
574
575 struct parsed_dcl_bracket {
576 uint first;
577 uint last;
578 };
579
580 static boolean
581 parse_register_dcl_bracket(
582 struct translate_ctx *ctx,
583 struct parsed_dcl_bracket *bracket)
584 {
585 uint uindex;
586 memset(bracket, 0, sizeof(struct parsed_dcl_bracket));
587
588 eat_opt_white( &ctx->cur );
589
590 if (!parse_uint( &ctx->cur, &uindex )) {
591 /* it can be an empty bracket [] which means its range
592 * is from 0 to some implied size */
593 if (ctx->cur[0] == ']' && ctx->implied_array_size != 0) {
594 bracket->first = 0;
595 bracket->last = ctx->implied_array_size - 1;
596 goto cleanup;
597 }
598 report_error( ctx, "Expected literal unsigned integer" );
599 return FALSE;
600 }
601 bracket->first = uindex;
602
603 eat_opt_white( &ctx->cur );
604
605 if (ctx->cur[0] == '.' && ctx->cur[1] == '.') {
606 uint uindex;
607
608 ctx->cur += 2;
609 eat_opt_white( &ctx->cur );
610 if (!parse_uint( &ctx->cur, &uindex )) {
611 report_error( ctx, "Expected literal integer" );
612 return FALSE;
613 }
614 bracket->last = (int) uindex;
615 eat_opt_white( &ctx->cur );
616 }
617 else {
618 bracket->last = bracket->first;
619 }
620
621 cleanup:
622 if (*ctx->cur != ']') {
623 report_error( ctx, "Expected `]' or `..'" );
624 return FALSE;
625 }
626 ctx->cur++;
627 return TRUE;
628 }
629
630 /* Parse register declaration.
631 * <register_dcl> ::= <register_file_bracket_index> `]' |
632 * <register_file_bracket_index> `..' <index> `]'
633 */
634 static boolean
635 parse_register_dcl(
636 struct translate_ctx *ctx,
637 uint *file,
638 struct parsed_dcl_bracket *brackets,
639 int *num_brackets)
640 {
641 const char *cur;
642
643 *num_brackets = 0;
644
645 if (!parse_register_file_bracket( ctx, file ))
646 return FALSE;
647 if (!parse_register_dcl_bracket( ctx, &brackets[0] ))
648 return FALSE;
649
650 *num_brackets = 1;
651
652 cur = ctx->cur;
653 eat_opt_white( &cur );
654
655 if (cur[0] == '[') {
656 ++cur;
657 ctx->cur = cur;
658 if (!parse_register_dcl_bracket( ctx, &brackets[1] ))
659 return FALSE;
660 /* for geometry shader we don't really care about
661 * the first brackets it's always the size of the
662 * input primitive. so we want to declare just
663 * the index relevant to the semantics which is in
664 * the second bracket */
665 if (ctx->processor == TGSI_PROCESSOR_GEOMETRY && *file == TGSI_FILE_INPUT) {
666 brackets[0] = brackets[1];
667 *num_brackets = 1;
668 } else {
669 *num_brackets = 2;
670 }
671 }
672
673 return TRUE;
674 }
675
676
677 /* Parse destination register operand.*/
678 static boolean
679 parse_register_dst(
680 struct translate_ctx *ctx,
681 uint *file,
682 struct parsed_bracket *brackets)
683 {
684 brackets->ind_comp = TGSI_SWIZZLE_X;
685 if (!parse_register_file_bracket( ctx, file ))
686 return FALSE;
687 if (!parse_register_bracket( ctx, brackets ))
688 return FALSE;
689
690 return TRUE;
691 }
692
693 static boolean
694 parse_dst_operand(
695 struct translate_ctx *ctx,
696 struct tgsi_full_dst_register *dst )
697 {
698 uint file;
699 uint writemask;
700 const char *cur;
701 struct parsed_bracket bracket[2];
702 int parsed_opt_brackets;
703
704 if (!parse_register_dst( ctx, &file, &bracket[0] ))
705 return FALSE;
706 if (!parse_opt_register_src_bracket(ctx, &bracket[1], &parsed_opt_brackets))
707 return FALSE;
708
709 cur = ctx->cur;
710 eat_opt_white( &cur );
711
712 if (!parse_opt_writemask( ctx, &writemask ))
713 return FALSE;
714
715 dst->Register.File = file;
716 if (parsed_opt_brackets) {
717 dst->Register.Dimension = 1;
718 dst->Dimension.Indirect = 0;
719 dst->Dimension.Dimension = 0;
720 dst->Dimension.Index = bracket[0].index;
721 bracket[0] = bracket[1];
722 }
723 dst->Register.Index = bracket[0].index;
724 dst->Register.WriteMask = writemask;
725 if (bracket[0].ind_file != TGSI_FILE_NULL) {
726 dst->Register.Indirect = 1;
727 dst->Indirect.File = bracket[0].ind_file;
728 dst->Indirect.Index = bracket[0].ind_index;
729 dst->Indirect.Swizzle = bracket[0].ind_comp;
730 dst->Indirect.ArrayID = bracket[0].ind_array;
731 }
732 return TRUE;
733 }
734
735 static boolean
736 parse_optional_swizzle(
737 struct translate_ctx *ctx,
738 uint *swizzle,
739 boolean *parsed_swizzle,
740 int components)
741 {
742 const char *cur = ctx->cur;
743
744 *parsed_swizzle = FALSE;
745
746 eat_opt_white( &cur );
747 if (*cur == '.') {
748 uint i;
749
750 cur++;
751 eat_opt_white( &cur );
752 for (i = 0; i < components; i++) {
753 if (uprcase( *cur ) == 'X')
754 swizzle[i] = TGSI_SWIZZLE_X;
755 else if (uprcase( *cur ) == 'Y')
756 swizzle[i] = TGSI_SWIZZLE_Y;
757 else if (uprcase( *cur ) == 'Z')
758 swizzle[i] = TGSI_SWIZZLE_Z;
759 else if (uprcase( *cur ) == 'W')
760 swizzle[i] = TGSI_SWIZZLE_W;
761 else {
762 report_error( ctx, "Expected register swizzle component `x', `y', `z' or `w'" );
763 return FALSE;
764 }
765 cur++;
766 }
767 *parsed_swizzle = TRUE;
768 ctx->cur = cur;
769 }
770 return TRUE;
771 }
772
773 static boolean
774 parse_src_operand(
775 struct translate_ctx *ctx,
776 struct tgsi_full_src_register *src )
777 {
778 uint file;
779 uint swizzle[4];
780 boolean parsed_swizzle;
781 struct parsed_bracket bracket[2];
782 int parsed_opt_brackets;
783
784 if (*ctx->cur == '-') {
785 ctx->cur++;
786 eat_opt_white( &ctx->cur );
787 src->Register.Negate = 1;
788 }
789
790 if (*ctx->cur == '|') {
791 ctx->cur++;
792 eat_opt_white( &ctx->cur );
793 src->Register.Absolute = 1;
794 }
795
796 if (!parse_register_src(ctx, &file, &bracket[0]))
797 return FALSE;
798 if (!parse_opt_register_src_bracket(ctx, &bracket[1], &parsed_opt_brackets))
799 return FALSE;
800
801 src->Register.File = file;
802 if (parsed_opt_brackets) {
803 src->Register.Dimension = 1;
804 src->Dimension.Indirect = 0;
805 src->Dimension.Dimension = 0;
806 src->Dimension.Index = bracket[0].index;
807 bracket[0] = bracket[1];
808 }
809 src->Register.Index = bracket[0].index;
810 if (bracket[0].ind_file != TGSI_FILE_NULL) {
811 src->Register.Indirect = 1;
812 src->Indirect.File = bracket[0].ind_file;
813 src->Indirect.Index = bracket[0].ind_index;
814 src->Indirect.Swizzle = bracket[0].ind_comp;
815 src->Indirect.ArrayID = bracket[0].ind_array;
816 }
817
818 /* Parse optional swizzle.
819 */
820 if (parse_optional_swizzle( ctx, swizzle, &parsed_swizzle, 4 )) {
821 if (parsed_swizzle) {
822 src->Register.SwizzleX = swizzle[0];
823 src->Register.SwizzleY = swizzle[1];
824 src->Register.SwizzleZ = swizzle[2];
825 src->Register.SwizzleW = swizzle[3];
826 }
827 }
828
829 if (src->Register.Absolute) {
830 eat_opt_white( &ctx->cur );
831 if (*ctx->cur != '|') {
832 report_error( ctx, "Expected `|'" );
833 return FALSE;
834 }
835 ctx->cur++;
836 }
837
838
839 return TRUE;
840 }
841
842 static boolean
843 parse_texoffset_operand(
844 struct translate_ctx *ctx,
845 struct tgsi_texture_offset *src )
846 {
847 uint file;
848 uint swizzle[3];
849 boolean parsed_swizzle;
850 struct parsed_bracket bracket;
851
852 if (!parse_register_src(ctx, &file, &bracket))
853 return FALSE;
854
855 src->File = file;
856 src->Index = bracket.index;
857
858 /* Parse optional swizzle.
859 */
860 if (parse_optional_swizzle( ctx, swizzle, &parsed_swizzle, 3 )) {
861 if (parsed_swizzle) {
862 src->SwizzleX = swizzle[0];
863 src->SwizzleY = swizzle[1];
864 src->SwizzleZ = swizzle[2];
865 }
866 }
867
868 return TRUE;
869 }
870
871 static boolean
872 match_inst(const char **pcur,
873 unsigned *saturate,
874 const struct tgsi_opcode_info *info)
875 {
876 const char *cur = *pcur;
877
878 /* simple case: the whole string matches the instruction name */
879 if (str_match_nocase_whole(&cur, info->mnemonic)) {
880 *pcur = cur;
881 *saturate = TGSI_SAT_NONE;
882 return TRUE;
883 }
884
885 if (str_match_no_case(&cur, info->mnemonic)) {
886 /* the instruction has a suffix, figure it out */
887 if (str_match_nocase_whole(&cur, "_SAT")) {
888 *pcur = cur;
889 *saturate = TGSI_SAT_ZERO_ONE;
890 return TRUE;
891 }
892
893 if (str_match_nocase_whole(&cur, "_SATNV")) {
894 *pcur = cur;
895 *saturate = TGSI_SAT_MINUS_PLUS_ONE;
896 return TRUE;
897 }
898 }
899
900 return FALSE;
901 }
902
903 static boolean
904 parse_instruction(
905 struct translate_ctx *ctx,
906 boolean has_label )
907 {
908 uint i;
909 uint saturate = TGSI_SAT_NONE;
910 const struct tgsi_opcode_info *info;
911 struct tgsi_full_instruction inst;
912 const char *cur;
913 uint advance;
914
915 inst = tgsi_default_full_instruction();
916
917 /* Parse predicate.
918 */
919 eat_opt_white( &ctx->cur );
920 if (*ctx->cur == '(') {
921 uint file;
922 int index;
923 uint swizzle[4];
924 boolean parsed_swizzle;
925
926 inst.Instruction.Predicate = 1;
927
928 ctx->cur++;
929 if (*ctx->cur == '!') {
930 ctx->cur++;
931 inst.Predicate.Negate = 1;
932 }
933
934 if (!parse_register_1d( ctx, &file, &index ))
935 return FALSE;
936
937 if (parse_optional_swizzle( ctx, swizzle, &parsed_swizzle, 4 )) {
938 if (parsed_swizzle) {
939 inst.Predicate.SwizzleX = swizzle[0];
940 inst.Predicate.SwizzleY = swizzle[1];
941 inst.Predicate.SwizzleZ = swizzle[2];
942 inst.Predicate.SwizzleW = swizzle[3];
943 }
944 }
945
946 if (*ctx->cur != ')') {
947 report_error( ctx, "Expected `)'" );
948 return FALSE;
949 }
950
951 ctx->cur++;
952 }
953
954 /* Parse instruction name.
955 */
956 eat_opt_white( &ctx->cur );
957 for (i = 0; i < TGSI_OPCODE_LAST; i++) {
958 cur = ctx->cur;
959
960 info = tgsi_get_opcode_info( i );
961 if (match_inst(&cur, &saturate, info)) {
962 if (info->num_dst + info->num_src + info->is_tex == 0) {
963 ctx->cur = cur;
964 break;
965 }
966 else if (*cur == '\0' || eat_white( &cur )) {
967 ctx->cur = cur;
968 break;
969 }
970 }
971 }
972 if (i == TGSI_OPCODE_LAST) {
973 if (has_label)
974 report_error( ctx, "Unknown opcode" );
975 else
976 report_error( ctx, "Expected `DCL', `IMM' or a label" );
977 return FALSE;
978 }
979
980 inst.Instruction.Opcode = i;
981 inst.Instruction.Saturate = saturate;
982 inst.Instruction.NumDstRegs = info->num_dst;
983 inst.Instruction.NumSrcRegs = info->num_src;
984
985 if (i >= TGSI_OPCODE_SAMPLE && i <= TGSI_OPCODE_GATHER4) {
986 /*
987 * These are not considered tex opcodes here (no additional
988 * target argument) however we're required to set the Texture
989 * bit so we can set the number of tex offsets (offsets aren't
990 * actually handled here yet in any case).
991 */
992 inst.Instruction.Texture = 1;
993 inst.Texture.Texture = TGSI_TEXTURE_UNKNOWN;
994 }
995
996 /* Parse instruction operands.
997 */
998 for (i = 0; i < info->num_dst + info->num_src + info->is_tex; i++) {
999 if (i > 0) {
1000 eat_opt_white( &ctx->cur );
1001 if (*ctx->cur != ',') {
1002 report_error( ctx, "Expected `,'" );
1003 return FALSE;
1004 }
1005 ctx->cur++;
1006 eat_opt_white( &ctx->cur );
1007 }
1008
1009 if (i < info->num_dst) {
1010 if (!parse_dst_operand( ctx, &inst.Dst[i] ))
1011 return FALSE;
1012 }
1013 else if (i < info->num_dst + info->num_src) {
1014 if (!parse_src_operand( ctx, &inst.Src[i - info->num_dst] ))
1015 return FALSE;
1016 }
1017 else {
1018 uint j;
1019
1020 for (j = 0; j < TGSI_TEXTURE_COUNT; j++) {
1021 if (str_match_nocase_whole( &ctx->cur, tgsi_texture_names[j] )) {
1022 inst.Instruction.Texture = 1;
1023 inst.Texture.Texture = j;
1024 break;
1025 }
1026 }
1027 if (j == TGSI_TEXTURE_COUNT) {
1028 report_error( ctx, "Expected texture target" );
1029 return FALSE;
1030 }
1031 }
1032 }
1033
1034 cur = ctx->cur;
1035 eat_opt_white( &cur );
1036 for (i = 0; info->is_tex && *cur == ','; i++) {
1037 cur++;
1038 eat_opt_white( &cur );
1039 ctx->cur = cur;
1040 if (!parse_texoffset_operand( ctx, &inst.TexOffsets[i] ))
1041 return FALSE;
1042 cur = ctx->cur;
1043 eat_opt_white( &cur );
1044 }
1045 inst.Texture.NumOffsets = i;
1046
1047 cur = ctx->cur;
1048 eat_opt_white( &cur );
1049 if (info->is_branch && *cur == ':') {
1050 uint target;
1051
1052 cur++;
1053 eat_opt_white( &cur );
1054 if (!parse_uint( &cur, &target )) {
1055 report_error( ctx, "Expected a label" );
1056 return FALSE;
1057 }
1058 inst.Instruction.Label = 1;
1059 inst.Label.Label = target;
1060 ctx->cur = cur;
1061 }
1062
1063 advance = tgsi_build_full_instruction(
1064 &inst,
1065 ctx->tokens_cur,
1066 ctx->header,
1067 (uint) (ctx->tokens_end - ctx->tokens_cur) );
1068 if (advance == 0)
1069 return FALSE;
1070 ctx->tokens_cur += advance;
1071
1072 return TRUE;
1073 }
1074
1075 /* parses a 4-touple of the form {x, y, z, w}
1076 * where x, y, z, w are numbers */
1077 static boolean parse_immediate_data(struct translate_ctx *ctx, unsigned type,
1078 union tgsi_immediate_data *values)
1079 {
1080 unsigned i;
1081 int ret;
1082
1083 eat_opt_white( &ctx->cur );
1084 if (*ctx->cur != '{') {
1085 report_error( ctx, "Expected `{'" );
1086 return FALSE;
1087 }
1088 ctx->cur++;
1089 for (i = 0; i < 4; i++) {
1090 eat_opt_white( &ctx->cur );
1091 if (i > 0) {
1092 if (*ctx->cur != ',') {
1093 report_error( ctx, "Expected `,'" );
1094 return FALSE;
1095 }
1096 ctx->cur++;
1097 eat_opt_white( &ctx->cur );
1098 }
1099
1100 switch (type) {
1101 case TGSI_IMM_FLOAT32:
1102 ret = parse_float(&ctx->cur, &values[i].Float);
1103 break;
1104 case TGSI_IMM_UINT32:
1105 ret = parse_uint(&ctx->cur, &values[i].Uint);
1106 break;
1107 case TGSI_IMM_INT32:
1108 ret = parse_int(&ctx->cur, &values[i].Int);
1109 break;
1110 default:
1111 assert(0);
1112 ret = FALSE;
1113 break;
1114 }
1115
1116 if (!ret) {
1117 report_error( ctx, "Expected immediate constant" );
1118 return FALSE;
1119 }
1120 }
1121 eat_opt_white( &ctx->cur );
1122 if (*ctx->cur != '}') {
1123 report_error( ctx, "Expected `}'" );
1124 return FALSE;
1125 }
1126 ctx->cur++;
1127
1128 return TRUE;
1129 }
1130
1131 static boolean parse_declaration( struct translate_ctx *ctx )
1132 {
1133 struct tgsi_full_declaration decl;
1134 uint file;
1135 struct parsed_dcl_bracket brackets[2];
1136 int num_brackets;
1137 uint writemask;
1138 const char *cur, *cur2;
1139 uint advance;
1140 boolean is_vs_input;
1141
1142 if (!eat_white( &ctx->cur )) {
1143 report_error( ctx, "Syntax error" );
1144 return FALSE;
1145 }
1146 if (!parse_register_dcl( ctx, &file, brackets, &num_brackets))
1147 return FALSE;
1148 if (!parse_opt_writemask( ctx, &writemask ))
1149 return FALSE;
1150
1151 decl = tgsi_default_full_declaration();
1152 decl.Declaration.File = file;
1153 decl.Declaration.UsageMask = writemask;
1154
1155 if (num_brackets == 1) {
1156 decl.Range.First = brackets[0].first;
1157 decl.Range.Last = brackets[0].last;
1158 } else {
1159 decl.Range.First = brackets[1].first;
1160 decl.Range.Last = brackets[1].last;
1161
1162 decl.Declaration.Dimension = 1;
1163 decl.Dim.Index2D = brackets[0].first;
1164 }
1165
1166 is_vs_input = (file == TGSI_FILE_INPUT &&
1167 ctx->processor == TGSI_PROCESSOR_VERTEX);
1168
1169 cur = ctx->cur;
1170 eat_opt_white( &cur );
1171 if (*cur == ',') {
1172 cur2 = cur;
1173 cur2++;
1174 eat_opt_white( &cur2 );
1175 if (str_match_nocase_whole( &cur2, "ARRAY" )) {
1176 int arrayid;
1177 if (*cur2 != '(') {
1178 report_error( ctx, "Expected `('" );
1179 return FALSE;
1180 }
1181 cur2++;
1182 eat_opt_white( &cur2 );
1183 if (!parse_int( &cur2, &arrayid )) {
1184 report_error( ctx, "Expected `,'" );
1185 return FALSE;
1186 }
1187 eat_opt_white( &cur2 );
1188 if (*cur2 != ')') {
1189 report_error( ctx, "Expected `)'" );
1190 return FALSE;
1191 }
1192 cur2++;
1193 decl.Declaration.Array = 1;
1194 decl.Array.ArrayID = arrayid;
1195 ctx->cur = cur = cur2;
1196 }
1197 }
1198
1199 if (*cur == ',' && !is_vs_input) {
1200 uint i, j;
1201
1202 cur++;
1203 eat_opt_white( &cur );
1204 if (file == TGSI_FILE_RESOURCE) {
1205 for (i = 0; i < TGSI_TEXTURE_COUNT; i++) {
1206 if (str_match_nocase_whole(&cur, tgsi_texture_names[i])) {
1207 decl.Resource.Resource = i;
1208 break;
1209 }
1210 }
1211 if (i == TGSI_TEXTURE_COUNT) {
1212 report_error(ctx, "Expected texture target");
1213 return FALSE;
1214 }
1215
1216 cur2 = cur;
1217 eat_opt_white(&cur2);
1218 while (*cur2 == ',') {
1219 cur2++;
1220 eat_opt_white(&cur2);
1221 if (str_match_nocase_whole(&cur2, "RAW")) {
1222 decl.Resource.Raw = 1;
1223
1224 } else if (str_match_nocase_whole(&cur2, "WR")) {
1225 decl.Resource.Writable = 1;
1226
1227 } else {
1228 break;
1229 }
1230 cur = cur2;
1231 eat_opt_white(&cur2);
1232 }
1233
1234 ctx->cur = cur;
1235
1236 } else if (file == TGSI_FILE_SAMPLER_VIEW) {
1237 for (i = 0; i < TGSI_TEXTURE_COUNT; i++) {
1238 if (str_match_nocase_whole(&cur, tgsi_texture_names[i])) {
1239 decl.SamplerView.Resource = i;
1240 break;
1241 }
1242 }
1243 if (i == TGSI_TEXTURE_COUNT) {
1244 report_error(ctx, "Expected texture target");
1245 return FALSE;
1246 }
1247 eat_opt_white( &cur );
1248 if (*cur != ',') {
1249 report_error( ctx, "Expected `,'" );
1250 return FALSE;
1251 }
1252 ++cur;
1253 eat_opt_white( &cur );
1254 for (j = 0; j < 4; ++j) {
1255 for (i = 0; i < PIPE_TYPE_COUNT; ++i) {
1256 if (str_match_nocase_whole(&cur, tgsi_type_names[i])) {
1257 switch (j) {
1258 case 0:
1259 decl.SamplerView.ReturnTypeX = i;
1260 break;
1261 case 1:
1262 decl.SamplerView.ReturnTypeY = i;
1263 break;
1264 case 2:
1265 decl.SamplerView.ReturnTypeZ = i;
1266 break;
1267 case 3:
1268 decl.SamplerView.ReturnTypeW = i;
1269 break;
1270 default:
1271 assert(0);
1272 }
1273 break;
1274 }
1275 }
1276 if (i == PIPE_TYPE_COUNT) {
1277 if (j == 0 || j > 2) {
1278 report_error(ctx, "Expected type name");
1279 return FALSE;
1280 }
1281 break;
1282 } else {
1283 cur2 = cur;
1284 eat_opt_white( &cur2 );
1285 if (*cur2 == ',') {
1286 cur2++;
1287 eat_opt_white( &cur2 );
1288 cur = cur2;
1289 continue;
1290 } else
1291 break;
1292 }
1293 }
1294 if (j < 4) {
1295 decl.SamplerView.ReturnTypeY =
1296 decl.SamplerView.ReturnTypeZ =
1297 decl.SamplerView.ReturnTypeW =
1298 decl.SamplerView.ReturnTypeX;
1299 }
1300 ctx->cur = cur;
1301 } else {
1302 if (str_match_nocase_whole(&cur, "LOCAL")) {
1303 decl.Declaration.Local = 1;
1304 ctx->cur = cur;
1305 }
1306
1307 cur = ctx->cur;
1308 eat_opt_white( &cur );
1309 if (*cur == ',') {
1310 cur++;
1311 eat_opt_white( &cur );
1312
1313 for (i = 0; i < TGSI_SEMANTIC_COUNT; i++) {
1314 if (str_match_nocase_whole(&cur, tgsi_semantic_names[i])) {
1315 uint index;
1316
1317 cur2 = cur;
1318 eat_opt_white( &cur2 );
1319 if (*cur2 == '[') {
1320 cur2++;
1321 eat_opt_white( &cur2 );
1322 if (!parse_uint( &cur2, &index )) {
1323 report_error( ctx, "Expected literal integer" );
1324 return FALSE;
1325 }
1326 eat_opt_white( &cur2 );
1327 if (*cur2 != ']') {
1328 report_error( ctx, "Expected `]'" );
1329 return FALSE;
1330 }
1331 cur2++;
1332
1333 decl.Semantic.Index = index;
1334
1335 cur = cur2;
1336 }
1337
1338 decl.Declaration.Semantic = 1;
1339 decl.Semantic.Name = i;
1340
1341 ctx->cur = cur;
1342 break;
1343 }
1344 }
1345 }
1346 }
1347 }
1348
1349 cur = ctx->cur;
1350 eat_opt_white( &cur );
1351 if (*cur == ',' && !is_vs_input) {
1352 uint i;
1353
1354 cur++;
1355 eat_opt_white( &cur );
1356 for (i = 0; i < TGSI_INTERPOLATE_COUNT; i++) {
1357 if (str_match_nocase_whole( &cur, tgsi_interpolate_names[i] )) {
1358 decl.Declaration.Interpolate = 1;
1359 decl.Interp.Interpolate = i;
1360
1361 ctx->cur = cur;
1362 break;
1363 }
1364 }
1365 if (i == TGSI_INTERPOLATE_COUNT) {
1366 report_error( ctx, "Expected semantic or interpolate attribute" );
1367 return FALSE;
1368 }
1369 }
1370
1371 advance = tgsi_build_full_declaration(
1372 &decl,
1373 ctx->tokens_cur,
1374 ctx->header,
1375 (uint) (ctx->tokens_end - ctx->tokens_cur) );
1376
1377 if (advance == 0)
1378 return FALSE;
1379 ctx->tokens_cur += advance;
1380
1381 return TRUE;
1382 }
1383
1384 static boolean parse_immediate( struct translate_ctx *ctx )
1385 {
1386 struct tgsi_full_immediate imm;
1387 uint advance;
1388 int type;
1389
1390 if (*ctx->cur == '[') {
1391 uint uindex;
1392
1393 ++ctx->cur;
1394
1395 eat_opt_white( &ctx->cur );
1396 if (!parse_uint( &ctx->cur, &uindex )) {
1397 report_error( ctx, "Expected literal unsigned integer" );
1398 return FALSE;
1399 }
1400
1401 if (uindex != ctx->num_immediates) {
1402 report_error( ctx, "Immediates must be sorted" );
1403 return FALSE;
1404 }
1405
1406 eat_opt_white( &ctx->cur );
1407 if (*ctx->cur != ']') {
1408 report_error( ctx, "Expected `]'" );
1409 return FALSE;
1410 }
1411
1412 ctx->cur++;
1413 }
1414
1415 if (!eat_white( &ctx->cur )) {
1416 report_error( ctx, "Syntax error" );
1417 return FALSE;
1418 }
1419 for (type = 0; type < Elements(tgsi_immediate_type_names); ++type) {
1420 if (str_match_nocase_whole(&ctx->cur, tgsi_immediate_type_names[type]))
1421 break;
1422 }
1423 if (type == Elements(tgsi_immediate_type_names)) {
1424 report_error( ctx, "Expected immediate type" );
1425 return FALSE;
1426 }
1427
1428 imm = tgsi_default_full_immediate();
1429 imm.Immediate.NrTokens += 4;
1430 imm.Immediate.DataType = type;
1431 parse_immediate_data(ctx, type, imm.u);
1432
1433 advance = tgsi_build_full_immediate(
1434 &imm,
1435 ctx->tokens_cur,
1436 ctx->header,
1437 (uint) (ctx->tokens_end - ctx->tokens_cur) );
1438 if (advance == 0)
1439 return FALSE;
1440 ctx->tokens_cur += advance;
1441
1442 ctx->num_immediates++;
1443
1444 return TRUE;
1445 }
1446
1447 static boolean
1448 parse_primitive( const char **pcur, uint *primitive )
1449 {
1450 uint i;
1451
1452 for (i = 0; i < PIPE_PRIM_MAX; i++) {
1453 const char *cur = *pcur;
1454
1455 if (str_match_nocase_whole( &cur, tgsi_primitive_names[i])) {
1456 *primitive = i;
1457 *pcur = cur;
1458 return TRUE;
1459 }
1460 }
1461 return FALSE;
1462 }
1463
1464 static boolean
1465 parse_fs_coord_origin( const char **pcur, uint *fs_coord_origin )
1466 {
1467 uint i;
1468
1469 for (i = 0; i < Elements(tgsi_fs_coord_origin_names); i++) {
1470 const char *cur = *pcur;
1471
1472 if (str_match_nocase_whole( &cur, tgsi_fs_coord_origin_names[i])) {
1473 *fs_coord_origin = i;
1474 *pcur = cur;
1475 return TRUE;
1476 }
1477 }
1478 return FALSE;
1479 }
1480
1481 static boolean
1482 parse_fs_coord_pixel_center( const char **pcur, uint *fs_coord_pixel_center )
1483 {
1484 uint i;
1485
1486 for (i = 0; i < Elements(tgsi_fs_coord_pixel_center_names); i++) {
1487 const char *cur = *pcur;
1488
1489 if (str_match_nocase_whole( &cur, tgsi_fs_coord_pixel_center_names[i])) {
1490 *fs_coord_pixel_center = i;
1491 *pcur = cur;
1492 return TRUE;
1493 }
1494 }
1495 return FALSE;
1496 }
1497
1498
1499 static boolean parse_property( struct translate_ctx *ctx )
1500 {
1501 struct tgsi_full_property prop;
1502 uint property_name;
1503 uint values[8];
1504 uint advance;
1505 char id[64];
1506
1507 if (!eat_white( &ctx->cur )) {
1508 report_error( ctx, "Syntax error" );
1509 return FALSE;
1510 }
1511 if (!parse_identifier( &ctx->cur, id )) {
1512 report_error( ctx, "Syntax error" );
1513 return FALSE;
1514 }
1515 for (property_name = 0; property_name < TGSI_PROPERTY_COUNT;
1516 ++property_name) {
1517 if (streq_nocase_uprcase(tgsi_property_names[property_name], id)) {
1518 break;
1519 }
1520 }
1521 if (property_name >= TGSI_PROPERTY_COUNT) {
1522 debug_printf( "\nError: Unknown property : '%s'", id );
1523 return FALSE;
1524 }
1525
1526 eat_opt_white( &ctx->cur );
1527 switch(property_name) {
1528 case TGSI_PROPERTY_GS_INPUT_PRIM:
1529 case TGSI_PROPERTY_GS_OUTPUT_PRIM:
1530 if (!parse_primitive(&ctx->cur, &values[0] )) {
1531 report_error( ctx, "Unknown primitive name as property!" );
1532 return FALSE;
1533 }
1534 if (property_name == TGSI_PROPERTY_GS_INPUT_PRIM &&
1535 ctx->processor == TGSI_PROCESSOR_GEOMETRY) {
1536 ctx->implied_array_size = u_vertices_per_prim(values[0]);
1537 }
1538 break;
1539 case TGSI_PROPERTY_FS_COORD_ORIGIN:
1540 if (!parse_fs_coord_origin(&ctx->cur, &values[0] )) {
1541 report_error( ctx, "Unknown coord origin as property: must be UPPER_LEFT or LOWER_LEFT!" );
1542 return FALSE;
1543 }
1544 break;
1545 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER:
1546 if (!parse_fs_coord_pixel_center(&ctx->cur, &values[0] )) {
1547 report_error( ctx, "Unknown coord pixel center as property: must be HALF_INTEGER or INTEGER!" );
1548 return FALSE;
1549 }
1550 break;
1551 case TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS:
1552 default:
1553 if (!parse_uint(&ctx->cur, &values[0] )) {
1554 report_error( ctx, "Expected unsigned integer as property!" );
1555 return FALSE;
1556 }
1557 }
1558
1559 prop = tgsi_default_full_property();
1560 prop.Property.PropertyName = property_name;
1561 prop.Property.NrTokens += 1;
1562 prop.u[0].Data = values[0];
1563
1564 advance = tgsi_build_full_property(
1565 &prop,
1566 ctx->tokens_cur,
1567 ctx->header,
1568 (uint) (ctx->tokens_end - ctx->tokens_cur) );
1569 if (advance == 0)
1570 return FALSE;
1571 ctx->tokens_cur += advance;
1572
1573 return TRUE;
1574 }
1575
1576
1577 static boolean translate( struct translate_ctx *ctx )
1578 {
1579 eat_opt_white( &ctx->cur );
1580 if (!parse_header( ctx ))
1581 return FALSE;
1582
1583 while (*ctx->cur != '\0') {
1584 uint label_val = 0;
1585 if (!eat_white( &ctx->cur )) {
1586 report_error( ctx, "Syntax error" );
1587 return FALSE;
1588 }
1589
1590 if (*ctx->cur == '\0')
1591 break;
1592 if (parse_label( ctx, &label_val )) {
1593 if (!parse_instruction( ctx, TRUE ))
1594 return FALSE;
1595 }
1596 else if (str_match_nocase_whole( &ctx->cur, "DCL" )) {
1597 if (!parse_declaration( ctx ))
1598 return FALSE;
1599 }
1600 else if (str_match_nocase_whole( &ctx->cur, "IMM" )) {
1601 if (!parse_immediate( ctx ))
1602 return FALSE;
1603 }
1604 else if (str_match_nocase_whole( &ctx->cur, "PROPERTY" )) {
1605 if (!parse_property( ctx ))
1606 return FALSE;
1607 }
1608 else if (!parse_instruction( ctx, FALSE )) {
1609 return FALSE;
1610 }
1611 }
1612
1613 return TRUE;
1614 }
1615
1616 boolean
1617 tgsi_text_translate(
1618 const char *text,
1619 struct tgsi_token *tokens,
1620 uint num_tokens )
1621 {
1622 struct translate_ctx ctx = {0};
1623
1624 ctx.text = text;
1625 ctx.cur = text;
1626 ctx.tokens = tokens;
1627 ctx.tokens_cur = tokens;
1628 ctx.tokens_end = tokens + num_tokens;
1629
1630 if (!translate( &ctx ))
1631 return FALSE;
1632
1633 return tgsi_sanity_check( tokens );
1634 }