Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / auxiliary / tgsi / tgsi_text.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 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.
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 "pipe/p_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"
39
40 static boolean is_alpha_underscore( const char *cur )
41 {
42 return
43 (*cur >= 'a' && *cur <= 'z') ||
44 (*cur >= 'A' && *cur <= 'Z') ||
45 *cur == '_';
46 }
47
48 static boolean is_digit( const char *cur )
49 {
50 return *cur >= '0' && *cur <= '9';
51 }
52
53 static boolean is_digit_alpha_underscore( const char *cur )
54 {
55 return is_digit( cur ) || is_alpha_underscore( cur );
56 }
57
58 static boolean uprcase( char c )
59 {
60 if (c >= 'a' && c <= 'z')
61 return c += 'A' - 'a';
62 return c;
63 }
64
65 /*
66 * Ignore case of str1 and assume str1 is already uppercase.
67 * Return TRUE iff str1 and str2 are equal.
68 */
69 static int
70 streq_nocase_uprcase(const char *str1,
71 const char *str2)
72 {
73 while (*str1 && *str2) {
74 if (*str1 != uprcase(*str2))
75 return FALSE;
76 str1++;
77 str2++;
78 }
79 return TRUE;
80 }
81
82 static boolean str_match_no_case( const char **pcur, const char *str )
83 {
84 const char *cur = *pcur;
85
86 while (*str != '\0' && *str == uprcase( *cur )) {
87 str++;
88 cur++;
89 }
90 if (*str == '\0') {
91 *pcur = cur;
92 return TRUE;
93 }
94 return FALSE;
95 }
96
97 /* Eat zero or more whitespaces.
98 */
99 static void eat_opt_white( const char **pcur )
100 {
101 while (**pcur == ' ' || **pcur == '\t' || **pcur == '\n')
102 (*pcur)++;
103 }
104
105 /* Eat one or more whitespaces.
106 * Return TRUE if at least one whitespace eaten.
107 */
108 static boolean eat_white( const char **pcur )
109 {
110 const char *cur = *pcur;
111
112 eat_opt_white( pcur );
113 return *pcur > cur;
114 }
115
116 /* Parse unsigned integer.
117 * No checks for overflow.
118 */
119 static boolean parse_uint( const char **pcur, uint *val )
120 {
121 const char *cur = *pcur;
122
123 if (is_digit( cur )) {
124 *val = *cur++ - '0';
125 while (is_digit( cur ))
126 *val = *val * 10 + *cur++ - '0';
127 *pcur = cur;
128 return TRUE;
129 }
130 return FALSE;
131 }
132
133 static boolean parse_identifier( const char **pcur, char *ret )
134 {
135 const char *cur = *pcur;
136 int i = 0;
137 if (is_alpha_underscore( cur )) {
138 ret[i++] = *cur++;
139 while (is_alpha_underscore( cur ))
140 ret[i++] = *cur++;
141 *pcur = cur;
142 return TRUE;
143 }
144 return FALSE;
145 }
146
147 /* Parse floating point.
148 */
149 static boolean parse_float( const char **pcur, float *val )
150 {
151 const char *cur = *pcur;
152 boolean integral_part = FALSE;
153 boolean fractional_part = FALSE;
154
155 *val = (float) atof( cur );
156
157 if (*cur == '-' || *cur == '+')
158 cur++;
159 if (is_digit( cur )) {
160 cur++;
161 integral_part = TRUE;
162 while (is_digit( cur ))
163 cur++;
164 }
165 if (*cur == '.') {
166 cur++;
167 if (is_digit( cur )) {
168 cur++;
169 fractional_part = TRUE;
170 while (is_digit( cur ))
171 cur++;
172 }
173 }
174 if (!integral_part && !fractional_part)
175 return FALSE;
176 if (uprcase( *cur ) == 'E') {
177 cur++;
178 if (*cur == '-' || *cur == '+')
179 cur++;
180 if (is_digit( cur )) {
181 cur++;
182 while (is_digit( cur ))
183 cur++;
184 }
185 else
186 return FALSE;
187 }
188 *pcur = cur;
189 return TRUE;
190 }
191
192 struct translate_ctx
193 {
194 const char *text;
195 const char *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;
202 };
203
204 static void report_error( struct translate_ctx *ctx, const char *msg )
205 {
206 int line = 1;
207 int column = 1;
208 const char *itr = ctx->text;
209
210 while (itr != ctx->cur) {
211 if (*itr == '\n') {
212 column = 1;
213 ++line;
214 }
215 ++column;
216 ++itr;
217 }
218
219 debug_printf( "\nTGSI asm error: %s [%d : %d] \n", msg, line, column );
220 }
221
222 /* Parse shader header.
223 * Return TRUE for one of the following headers.
224 * FRAG
225 * GEOM
226 * VERT
227 */
228 static boolean parse_header( struct translate_ctx *ctx )
229 {
230 uint processor;
231
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;
238 else {
239 report_error( ctx, "Unknown header" );
240 return FALSE;
241 }
242
243 if (ctx->tokens_cur >= ctx->tokens_end)
244 return FALSE;
245 ctx->header = (struct tgsi_header *) ctx->tokens_cur++;
246 *ctx->header = tgsi_build_header();
247
248 if (ctx->tokens_cur >= ctx->tokens_end)
249 return FALSE;
250 *(struct tgsi_processor *) ctx->tokens_cur++ = tgsi_build_processor( processor, ctx->header );
251 ctx->processor = processor;
252
253 return TRUE;
254 }
255
256 static boolean parse_label( struct translate_ctx *ctx, uint *val )
257 {
258 const char *cur = ctx->cur;
259
260 if (parse_uint( &cur, val )) {
261 eat_opt_white( &cur );
262 if (*cur == ':') {
263 cur++;
264 ctx->cur = cur;
265 return TRUE;
266 }
267 }
268 return FALSE;
269 }
270
271 static const char *file_names[TGSI_FILE_COUNT] =
272 {
273 "NULL",
274 "CONST",
275 "IN",
276 "OUT",
277 "TEMP",
278 "SAMP",
279 "ADDR",
280 "IMM",
281 "LOOP",
282 "PRED",
283 "SV"
284 };
285
286 static boolean
287 parse_file( const char **pcur, uint *file )
288 {
289 uint i;
290
291 for (i = 0; i < TGSI_FILE_COUNT; i++) {
292 const char *cur = *pcur;
293
294 if (str_match_no_case( &cur, file_names[i] )) {
295 if (!is_digit_alpha_underscore( cur )) {
296 *pcur = cur;
297 *file = i;
298 return TRUE;
299 }
300 }
301 }
302 return FALSE;
303 }
304
305 static boolean
306 parse_opt_writemask(
307 struct translate_ctx *ctx,
308 uint *writemask )
309 {
310 const char *cur;
311
312 cur = ctx->cur;
313 eat_opt_white( &cur );
314 if (*cur == '.') {
315 cur++;
316 *writemask = TGSI_WRITEMASK_NONE;
317 eat_opt_white( &cur );
318 if (uprcase( *cur ) == 'X') {
319 cur++;
320 *writemask |= TGSI_WRITEMASK_X;
321 }
322 if (uprcase( *cur ) == 'Y') {
323 cur++;
324 *writemask |= TGSI_WRITEMASK_Y;
325 }
326 if (uprcase( *cur ) == 'Z') {
327 cur++;
328 *writemask |= TGSI_WRITEMASK_Z;
329 }
330 if (uprcase( *cur ) == 'W') {
331 cur++;
332 *writemask |= TGSI_WRITEMASK_W;
333 }
334
335 if (*writemask == TGSI_WRITEMASK_NONE) {
336 report_error( ctx, "Writemask expected" );
337 return FALSE;
338 }
339
340 ctx->cur = cur;
341 }
342 else {
343 *writemask = TGSI_WRITEMASK_XYZW;
344 }
345 return TRUE;
346 }
347
348 static boolean
349 parse_register_dst( struct translate_ctx *ctx,
350 uint *file,
351 int *index );
352
353 struct parsed_src_bracket {
354 int index;
355
356 uint ind_file;
357 int ind_index;
358 uint ind_comp;
359 };
360
361
362 static boolean
363 parse_register_src_bracket(
364 struct translate_ctx *ctx,
365 struct parsed_src_bracket *brackets)
366 {
367 const char *cur;
368 uint uindex;
369
370 memset(brackets, 0, sizeof(struct parsed_src_bracket));
371
372 eat_opt_white( &ctx->cur );
373
374 cur = ctx->cur;
375 if (parse_file( &cur, &brackets->ind_file )) {
376 if (!parse_register_dst( ctx, &brackets->ind_file,
377 &brackets->ind_index ))
378 return FALSE;
379 eat_opt_white( &ctx->cur );
380
381 if (*ctx->cur == '.') {
382 ctx->cur++;
383 eat_opt_white(&ctx->cur);
384
385 switch (uprcase(*ctx->cur)) {
386 case 'X':
387 brackets->ind_comp = TGSI_SWIZZLE_X;
388 break;
389 case 'Y':
390 brackets->ind_comp = TGSI_SWIZZLE_Y;
391 break;
392 case 'Z':
393 brackets->ind_comp = TGSI_SWIZZLE_Z;
394 break;
395 case 'W':
396 brackets->ind_comp = TGSI_SWIZZLE_W;
397 break;
398 default:
399 report_error(ctx, "Expected indirect register swizzle component `x', `y', `z' or `w'");
400 return FALSE;
401 }
402 ctx->cur++;
403 eat_opt_white(&ctx->cur);
404 }
405
406 if (*ctx->cur == '+' || *ctx->cur == '-') {
407 boolean negate;
408
409 negate = *ctx->cur == '-';
410 ctx->cur++;
411 eat_opt_white( &ctx->cur );
412 if (!parse_uint( &ctx->cur, &uindex )) {
413 report_error( ctx, "Expected literal unsigned integer" );
414 return FALSE;
415 }
416 if (negate)
417 brackets->index = -(int) uindex;
418 else
419 brackets->index = (int) uindex;
420 }
421 else {
422 brackets->index = 0;
423 }
424 }
425 else {
426 if (!parse_uint( &ctx->cur, &uindex )) {
427 report_error( ctx, "Expected literal unsigned integer" );
428 return FALSE;
429 }
430 brackets->index = (int) uindex;
431 brackets->ind_file = TGSI_FILE_NULL;
432 brackets->ind_index = 0;
433 }
434 eat_opt_white( &ctx->cur );
435 if (*ctx->cur != ']') {
436 report_error( ctx, "Expected `]'" );
437 return FALSE;
438 }
439 ctx->cur++;
440 return TRUE;
441 }
442
443 static boolean
444 parse_opt_register_src_bracket(
445 struct translate_ctx *ctx,
446 struct parsed_src_bracket *brackets,
447 int *parsed_brackets)
448 {
449 const char *cur = ctx->cur;
450
451 *parsed_brackets = 0;
452
453 eat_opt_white( &cur );
454 if (cur[0] == '[') {
455 ++cur;
456 ctx->cur = cur;
457
458 if (!parse_register_src_bracket(ctx, brackets))
459 return FALSE;
460
461 *parsed_brackets = 1;
462 }
463
464 return TRUE;
465 }
466
467 /* <register_file_bracket> ::= <file> `['
468 */
469 static boolean
470 parse_register_file_bracket(
471 struct translate_ctx *ctx,
472 uint *file )
473 {
474 if (!parse_file( &ctx->cur, file )) {
475 report_error( ctx, "Unknown register file" );
476 return FALSE;
477 }
478 eat_opt_white( &ctx->cur );
479 if (*ctx->cur != '[') {
480 report_error( ctx, "Expected `['" );
481 return FALSE;
482 }
483 ctx->cur++;
484 return TRUE;
485 }
486
487 /* <register_file_bracket_index> ::= <register_file_bracket> <uint>
488 */
489 static boolean
490 parse_register_file_bracket_index(
491 struct translate_ctx *ctx,
492 uint *file,
493 int *index )
494 {
495 uint uindex;
496
497 if (!parse_register_file_bracket( ctx, file ))
498 return FALSE;
499 eat_opt_white( &ctx->cur );
500 if (!parse_uint( &ctx->cur, &uindex )) {
501 report_error( ctx, "Expected literal unsigned integer" );
502 return FALSE;
503 }
504 *index = (int) uindex;
505 return TRUE;
506 }
507
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> `]'
513 */
514 static boolean
515 parse_register_src(
516 struct translate_ctx *ctx,
517 uint *file,
518 struct parsed_src_bracket *brackets)
519 {
520
521 brackets->ind_comp = TGSI_SWIZZLE_X;
522 if (!parse_register_file_bracket( ctx, file ))
523 return FALSE;
524 if (!parse_register_src_bracket( ctx, brackets ))
525 return FALSE;
526
527 return TRUE;
528 }
529
530 struct parsed_dcl_bracket {
531 uint first;
532 uint last;
533 };
534
535 static boolean
536 parse_register_dcl_bracket(
537 struct translate_ctx *ctx,
538 struct parsed_dcl_bracket *bracket)
539 {
540 uint uindex;
541 memset(bracket, 0, sizeof(struct parsed_dcl_bracket));
542
543 eat_opt_white( &ctx->cur );
544
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) {
549 bracket->first = 0;
550 bracket->last = ctx->implied_array_size - 1;
551 goto cleanup;
552 }
553 report_error( ctx, "Expected literal unsigned integer" );
554 return FALSE;
555 }
556 bracket->first = (int) uindex;
557
558 eat_opt_white( &ctx->cur );
559
560 if (ctx->cur[0] == '.' && ctx->cur[1] == '.') {
561 uint uindex;
562
563 ctx->cur += 2;
564 eat_opt_white( &ctx->cur );
565 if (!parse_uint( &ctx->cur, &uindex )) {
566 report_error( ctx, "Expected literal integer" );
567 return FALSE;
568 }
569 bracket->last = (int) uindex;
570 eat_opt_white( &ctx->cur );
571 }
572 else {
573 bracket->last = bracket->first;
574 }
575
576 cleanup:
577 if (*ctx->cur != ']') {
578 report_error( ctx, "Expected `]' or `..'" );
579 return FALSE;
580 }
581 ctx->cur++;
582 return TRUE;
583 }
584
585 /* Parse register declaration.
586 * <register_dcl> ::= <register_file_bracket_index> `]' |
587 * <register_file_bracket_index> `..' <index> `]'
588 */
589 static boolean
590 parse_register_dcl(
591 struct translate_ctx *ctx,
592 uint *file,
593 struct parsed_dcl_bracket *brackets,
594 int *num_brackets)
595 {
596 const char *cur;
597
598 *num_brackets = 0;
599
600 if (!parse_register_file_bracket( ctx, file ))
601 return FALSE;
602 if (!parse_register_dcl_bracket( ctx, &brackets[0] ))
603 return FALSE;
604
605 *num_brackets = 1;
606
607 cur = ctx->cur;
608 eat_opt_white( &cur );
609
610 if (cur[0] == '[') {
611 ++cur;
612 ctx->cur = cur;
613 if (!parse_register_dcl_bracket( ctx, &brackets[1] ))
614 return FALSE;
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) {
621 brackets[0] = brackets[1];
622 }
623 *num_brackets = 2;
624 }
625
626 return TRUE;
627 }
628
629
630 /* Parse destination register operand.
631 * <register_dst> ::= <register_file_bracket_index> `]'
632 */
633 static boolean
634 parse_register_dst(
635 struct translate_ctx *ctx,
636 uint *file,
637 int *index )
638 {
639 if (!parse_register_file_bracket_index( ctx, file, index ))
640 return FALSE;
641 eat_opt_white( &ctx->cur );
642 if (*ctx->cur != ']') {
643 report_error( ctx, "Expected `]'" );
644 return FALSE;
645 }
646 ctx->cur++;
647 return TRUE;
648 }
649
650 static boolean
651 parse_dst_operand(
652 struct translate_ctx *ctx,
653 struct tgsi_full_dst_register *dst )
654 {
655 uint file;
656 int index;
657 uint writemask;
658 const char *cur;
659
660 if (!parse_register_dst( ctx, &file, &index ))
661 return FALSE;
662
663 cur = ctx->cur;
664 eat_opt_white( &cur );
665
666 if (!parse_opt_writemask( ctx, &writemask ))
667 return FALSE;
668
669 dst->Register.File = file;
670 dst->Register.Index = index;
671 dst->Register.WriteMask = writemask;
672 return TRUE;
673 }
674
675 static boolean
676 parse_optional_swizzle(
677 struct translate_ctx *ctx,
678 uint swizzle[4],
679 boolean *parsed_swizzle )
680 {
681 const char *cur = ctx->cur;
682
683 *parsed_swizzle = FALSE;
684
685 eat_opt_white( &cur );
686 if (*cur == '.') {
687 uint i;
688
689 cur++;
690 eat_opt_white( &cur );
691 for (i = 0; i < 4; i++) {
692 if (uprcase( *cur ) == 'X')
693 swizzle[i] = TGSI_SWIZZLE_X;
694 else if (uprcase( *cur ) == 'Y')
695 swizzle[i] = TGSI_SWIZZLE_Y;
696 else if (uprcase( *cur ) == 'Z')
697 swizzle[i] = TGSI_SWIZZLE_Z;
698 else if (uprcase( *cur ) == 'W')
699 swizzle[i] = TGSI_SWIZZLE_W;
700 else {
701 report_error( ctx, "Expected register swizzle component `x', `y', `z', `w', `0' or `1'" );
702 return FALSE;
703 }
704 cur++;
705 }
706 *parsed_swizzle = TRUE;
707 ctx->cur = cur;
708 }
709 return TRUE;
710 }
711
712 static boolean
713 parse_src_operand(
714 struct translate_ctx *ctx,
715 struct tgsi_full_src_register *src )
716 {
717 uint file;
718 uint swizzle[4];
719 boolean parsed_swizzle;
720 struct parsed_src_bracket bracket[2];
721 int parsed_opt_brackets;
722
723 if (*ctx->cur == '-') {
724 ctx->cur++;
725 eat_opt_white( &ctx->cur );
726 src->Register.Negate = 1;
727 }
728
729 if (*ctx->cur == '|') {
730 ctx->cur++;
731 eat_opt_white( &ctx->cur );
732 src->Register.Absolute = 1;
733 }
734
735 if (!parse_register_src(ctx, &file, &bracket[0]))
736 return FALSE;
737 if (!parse_opt_register_src_bracket(ctx, &bracket[1], &parsed_opt_brackets))
738 return FALSE;
739
740 src->Register.File = file;
741 src->Register.Index = bracket[0].index;
742 if (bracket[0].ind_file != TGSI_FILE_NULL) {
743 src->Register.Indirect = 1;
744 src->Indirect.File = bracket[0].ind_file;
745 src->Indirect.Index = bracket[0].ind_index;
746 src->Indirect.SwizzleX = bracket[0].ind_comp;
747 src->Indirect.SwizzleY = bracket[0].ind_comp;
748 src->Indirect.SwizzleZ = bracket[0].ind_comp;
749 src->Indirect.SwizzleW = bracket[0].ind_comp;
750 }
751 if (parsed_opt_brackets) {
752 src->Register.Dimension = 1;
753 src->Dimension.Indirect = 0;
754 src->Dimension.Dimension = 0;
755 src->Dimension.Index = bracket[1].index;
756 }
757
758 /* Parse optional swizzle.
759 */
760 if (parse_optional_swizzle( ctx, swizzle, &parsed_swizzle )) {
761 if (parsed_swizzle) {
762 src->Register.SwizzleX = swizzle[0];
763 src->Register.SwizzleY = swizzle[1];
764 src->Register.SwizzleZ = swizzle[2];
765 src->Register.SwizzleW = swizzle[3];
766 }
767 }
768
769 if (src->Register.Absolute) {
770 eat_opt_white( &ctx->cur );
771 if (*ctx->cur != '|') {
772 report_error( ctx, "Expected `|'" );
773 return FALSE;
774 }
775 ctx->cur++;
776 }
777
778
779 return TRUE;
780 }
781
782 static const char *texture_names[TGSI_TEXTURE_COUNT] =
783 {
784 "UNKNOWN",
785 "1D",
786 "2D",
787 "3D",
788 "CUBE",
789 "RECT",
790 "SHADOW1D",
791 "SHADOW2D",
792 "SHADOWRECT"
793 };
794
795 static boolean
796 match_inst_mnemonic(const char **pcur,
797 const struct tgsi_opcode_info *info)
798 {
799 if (str_match_no_case(pcur, info->mnemonic)) {
800 return TRUE;
801 }
802 return FALSE;
803 }
804
805 static boolean
806 parse_instruction(
807 struct translate_ctx *ctx,
808 boolean has_label )
809 {
810 uint i;
811 uint saturate = TGSI_SAT_NONE;
812 const struct tgsi_opcode_info *info;
813 struct tgsi_full_instruction inst;
814 uint advance;
815
816 /* Parse instruction name.
817 */
818 eat_opt_white( &ctx->cur );
819 for (i = 0; i < TGSI_OPCODE_LAST; i++) {
820 const char *cur = ctx->cur;
821
822 info = tgsi_get_opcode_info( i );
823 if (match_inst_mnemonic(&cur, info)) {
824 if (str_match_no_case( &cur, "_SATNV" ))
825 saturate = TGSI_SAT_MINUS_PLUS_ONE;
826 else if (str_match_no_case( &cur, "_SAT" ))
827 saturate = TGSI_SAT_ZERO_ONE;
828
829 if (info->num_dst + info->num_src + info->is_tex == 0) {
830 if (!is_digit_alpha_underscore( cur )) {
831 ctx->cur = cur;
832 break;
833 }
834 }
835 else if (*cur == '\0' || eat_white( &cur )) {
836 ctx->cur = cur;
837 break;
838 }
839 }
840 }
841 if (i == TGSI_OPCODE_LAST) {
842 if (has_label)
843 report_error( ctx, "Unknown opcode" );
844 else
845 report_error( ctx, "Expected `DCL', `IMM' or a label" );
846 return FALSE;
847 }
848
849 inst = tgsi_default_full_instruction();
850 inst.Instruction.Opcode = i;
851 inst.Instruction.Saturate = saturate;
852 inst.Instruction.NumDstRegs = info->num_dst;
853 inst.Instruction.NumSrcRegs = info->num_src;
854
855 /* Parse instruction operands.
856 */
857 for (i = 0; i < info->num_dst + info->num_src + info->is_tex; i++) {
858 if (i > 0) {
859 eat_opt_white( &ctx->cur );
860 if (*ctx->cur != ',') {
861 report_error( ctx, "Expected `,'" );
862 return FALSE;
863 }
864 ctx->cur++;
865 eat_opt_white( &ctx->cur );
866 }
867
868 if (i < info->num_dst) {
869 if (!parse_dst_operand( ctx, &inst.Dst[i] ))
870 return FALSE;
871 }
872 else if (i < info->num_dst + info->num_src) {
873 if (!parse_src_operand( ctx, &inst.Src[i - info->num_dst] ))
874 return FALSE;
875 }
876 else {
877 uint j;
878
879 for (j = 0; j < TGSI_TEXTURE_COUNT; j++) {
880 if (str_match_no_case( &ctx->cur, texture_names[j] )) {
881 if (!is_digit_alpha_underscore( ctx->cur )) {
882 inst.Instruction.Texture = 1;
883 inst.Texture.Texture = j;
884 break;
885 }
886 }
887 }
888 if (j == TGSI_TEXTURE_COUNT) {
889 report_error( ctx, "Expected texture target" );
890 return FALSE;
891 }
892 }
893 }
894
895 if (info->is_branch) {
896 uint target;
897
898 eat_opt_white( &ctx->cur );
899 if (*ctx->cur != ':') {
900 report_error( ctx, "Expected `:'" );
901 return FALSE;
902 }
903 ctx->cur++;
904 eat_opt_white( &ctx->cur );
905 if (!parse_uint( &ctx->cur, &target )) {
906 report_error( ctx, "Expected a label" );
907 return FALSE;
908 }
909 inst.Instruction.Label = 1;
910 inst.Label.Label = target;
911 }
912
913 advance = tgsi_build_full_instruction(
914 &inst,
915 ctx->tokens_cur,
916 ctx->header,
917 (uint) (ctx->tokens_end - ctx->tokens_cur) );
918 if (advance == 0)
919 return FALSE;
920 ctx->tokens_cur += advance;
921
922 return TRUE;
923 }
924
925 static const char *semantic_names[TGSI_SEMANTIC_COUNT] =
926 {
927 "POSITION",
928 "COLOR",
929 "BCOLOR",
930 "FOG",
931 "PSIZE",
932 "GENERIC",
933 "NORMAL",
934 "FACE",
935 "EDGEFLAG",
936 "PRIM_ID",
937 "INSTANCEID"
938 };
939
940 static const char *interpolate_names[TGSI_INTERPOLATE_COUNT] =
941 {
942 "CONSTANT",
943 "LINEAR",
944 "PERSPECTIVE"
945 };
946
947 static boolean parse_declaration( struct translate_ctx *ctx )
948 {
949 struct tgsi_full_declaration decl;
950 uint file;
951 struct parsed_dcl_bracket brackets[2];
952 int num_brackets;
953 uint writemask;
954 const char *cur;
955 uint advance;
956
957 assert(Elements(semantic_names) == TGSI_SEMANTIC_COUNT);
958 assert(Elements(interpolate_names) == TGSI_INTERPOLATE_COUNT);
959
960 if (!eat_white( &ctx->cur )) {
961 report_error( ctx, "Syntax error" );
962 return FALSE;
963 }
964 if (!parse_register_dcl( ctx, &file, brackets, &num_brackets))
965 return FALSE;
966 if (!parse_opt_writemask( ctx, &writemask ))
967 return FALSE;
968
969 decl = tgsi_default_full_declaration();
970 decl.Declaration.File = file;
971 decl.Declaration.UsageMask = writemask;
972 decl.Range.First = brackets[0].first;
973 decl.Range.Last = brackets[0].last;
974
975 cur = ctx->cur;
976 eat_opt_white( &cur );
977 if (*cur == ',') {
978 uint i;
979
980 cur++;
981 eat_opt_white( &cur );
982 for (i = 0; i < TGSI_SEMANTIC_COUNT; i++) {
983 if (str_match_no_case( &cur, semantic_names[i] )) {
984 const char *cur2 = cur;
985 uint index;
986
987 if (is_digit_alpha_underscore( cur ))
988 continue;
989 eat_opt_white( &cur2 );
990 if (*cur2 == '[') {
991 cur2++;
992 eat_opt_white( &cur2 );
993 if (!parse_uint( &cur2, &index )) {
994 report_error( ctx, "Expected literal integer" );
995 return FALSE;
996 }
997 eat_opt_white( &cur2 );
998 if (*cur2 != ']') {
999 report_error( ctx, "Expected `]'" );
1000 return FALSE;
1001 }
1002 cur2++;
1003
1004 decl.Semantic.Index = index;
1005
1006 cur = cur2;
1007 }
1008
1009 decl.Declaration.Semantic = 1;
1010 decl.Semantic.Name = i;
1011
1012 ctx->cur = cur;
1013 break;
1014 }
1015 }
1016 }
1017
1018 cur = ctx->cur;
1019 eat_opt_white( &cur );
1020 if (*cur == ',') {
1021 uint i;
1022
1023 cur++;
1024 eat_opt_white( &cur );
1025 for (i = 0; i < TGSI_INTERPOLATE_COUNT; i++) {
1026 if (str_match_no_case( &cur, interpolate_names[i] )) {
1027 if (is_digit_alpha_underscore( cur ))
1028 continue;
1029 decl.Declaration.Interpolate = i;
1030
1031 ctx->cur = cur;
1032 break;
1033 }
1034 }
1035 if (i == TGSI_INTERPOLATE_COUNT) {
1036 report_error( ctx, "Expected semantic or interpolate attribute" );
1037 return FALSE;
1038 }
1039 }
1040
1041 advance = tgsi_build_full_declaration(
1042 &decl,
1043 ctx->tokens_cur,
1044 ctx->header,
1045 (uint) (ctx->tokens_end - ctx->tokens_cur) );
1046 if (advance == 0)
1047 return FALSE;
1048 ctx->tokens_cur += advance;
1049
1050 return TRUE;
1051 }
1052
1053 static boolean parse_immediate( struct translate_ctx *ctx )
1054 {
1055 struct tgsi_full_immediate imm;
1056 uint i;
1057 float values[4];
1058 uint advance;
1059
1060 if (!eat_white( &ctx->cur )) {
1061 report_error( ctx, "Syntax error" );
1062 return FALSE;
1063 }
1064 if (!str_match_no_case( &ctx->cur, "FLT32" ) || is_digit_alpha_underscore( ctx->cur )) {
1065 report_error( ctx, "Expected `FLT32'" );
1066 return FALSE;
1067 }
1068 eat_opt_white( &ctx->cur );
1069 if (*ctx->cur != '{') {
1070 report_error( ctx, "Expected `{'" );
1071 return FALSE;
1072 }
1073 ctx->cur++;
1074 for (i = 0; i < 4; i++) {
1075 eat_opt_white( &ctx->cur );
1076 if (i > 0) {
1077 if (*ctx->cur != ',') {
1078 report_error( ctx, "Expected `,'" );
1079 return FALSE;
1080 }
1081 ctx->cur++;
1082 eat_opt_white( &ctx->cur );
1083 }
1084 if (!parse_float( &ctx->cur, &values[i] )) {
1085 report_error( ctx, "Expected literal floating point" );
1086 return FALSE;
1087 }
1088 }
1089 eat_opt_white( &ctx->cur );
1090 if (*ctx->cur != '}') {
1091 report_error( ctx, "Expected `}'" );
1092 return FALSE;
1093 }
1094 ctx->cur++;
1095
1096 imm = tgsi_default_full_immediate();
1097 imm.Immediate.NrTokens += 4;
1098 imm.Immediate.DataType = TGSI_IMM_FLOAT32;
1099 imm.u[0].Float = values[0];
1100 imm.u[1].Float = values[1];
1101 imm.u[2].Float = values[2];
1102 imm.u[3].Float = values[3];
1103
1104 advance = tgsi_build_full_immediate(
1105 &imm,
1106 ctx->tokens_cur,
1107 ctx->header,
1108 (uint) (ctx->tokens_end - ctx->tokens_cur) );
1109 if (advance == 0)
1110 return FALSE;
1111 ctx->tokens_cur += advance;
1112
1113 return TRUE;
1114 }
1115
1116 static const char *property_names[] =
1117 {
1118 "GS_INPUT_PRIMITIVE",
1119 "GS_OUTPUT_PRIMITIVE",
1120 "GS_MAX_OUTPUT_VERTICES"
1121 };
1122
1123 static const char *primitive_names[] =
1124 {
1125 "POINTS",
1126 "LINES",
1127 "LINE_LOOP",
1128 "LINE_STRIP",
1129 "TRIANGLES",
1130 "TRIANGLE_STRIP",
1131 "TRIANGLE_FAN",
1132 "QUADS",
1133 "QUAD_STRIP",
1134 "POLYGON"
1135 };
1136
1137 static boolean
1138 parse_primitive( const char **pcur, uint *primitive )
1139 {
1140 uint i;
1141
1142 for (i = 0; i < PIPE_PRIM_MAX; i++) {
1143 const char *cur = *pcur;
1144
1145 if (str_match_no_case( &cur, primitive_names[i])) {
1146 *primitive = i;
1147 *pcur = cur;
1148 return TRUE;
1149 }
1150 }
1151 return FALSE;
1152 }
1153
1154
1155 static boolean parse_property( struct translate_ctx *ctx )
1156 {
1157 struct tgsi_full_property prop;
1158 uint property_name;
1159 uint values[8];
1160 uint advance;
1161 char id[64];
1162
1163 if (!eat_white( &ctx->cur )) {
1164 report_error( ctx, "Syntax error" );
1165 return FALSE;
1166 }
1167 if (!parse_identifier( &ctx->cur, id )) {
1168 report_error( ctx, "Syntax error" );
1169 return FALSE;
1170 }
1171 for (property_name = 0; property_name < TGSI_PROPERTY_COUNT;
1172 ++property_name) {
1173 if (streq_nocase_uprcase(property_names[property_name], id)) {
1174 break;
1175 }
1176 }
1177 if (property_name >= TGSI_PROPERTY_COUNT) {
1178 debug_printf( "\nError: Unknown property : '%s'", id );
1179 return FALSE;
1180 }
1181
1182 eat_opt_white( &ctx->cur );
1183 switch(property_name) {
1184 case TGSI_PROPERTY_GS_INPUT_PRIM:
1185 case TGSI_PROPERTY_GS_OUTPUT_PRIM:
1186 if (!parse_primitive(&ctx->cur, &values[0] )) {
1187 report_error( ctx, "Unknown primitive name as property!" );
1188 return FALSE;
1189 }
1190 if (property_name == TGSI_PROPERTY_GS_INPUT_PRIM &&
1191 ctx->processor == TGSI_PROCESSOR_GEOMETRY) {
1192 ctx->implied_array_size = u_vertices_per_prim(values[0]);
1193 }
1194 break;
1195 default:
1196 if (!parse_uint(&ctx->cur, &values[0] )) {
1197 report_error( ctx, "Expected unsigned integer as property!" );
1198 return FALSE;
1199 }
1200 }
1201
1202 prop = tgsi_default_full_property();
1203 prop.Property.PropertyName = property_name;
1204 prop.Property.NrTokens += 1;
1205 prop.u[0].Data = values[0];
1206
1207 advance = tgsi_build_full_property(
1208 &prop,
1209 ctx->tokens_cur,
1210 ctx->header,
1211 (uint) (ctx->tokens_end - ctx->tokens_cur) );
1212 if (advance == 0)
1213 return FALSE;
1214 ctx->tokens_cur += advance;
1215
1216 return TRUE;
1217 }
1218
1219
1220 static boolean translate( struct translate_ctx *ctx )
1221 {
1222 eat_opt_white( &ctx->cur );
1223 if (!parse_header( ctx ))
1224 return FALSE;
1225
1226 while (*ctx->cur != '\0') {
1227 uint label_val = 0;
1228 if (!eat_white( &ctx->cur )) {
1229 report_error( ctx, "Syntax error" );
1230 return FALSE;
1231 }
1232
1233 if (*ctx->cur == '\0')
1234 break;
1235 if (parse_label( ctx, &label_val )) {
1236 if (!parse_instruction( ctx, TRUE ))
1237 return FALSE;
1238 }
1239 else if (str_match_no_case( &ctx->cur, "DCL" )) {
1240 if (!parse_declaration( ctx ))
1241 return FALSE;
1242 }
1243 else if (str_match_no_case( &ctx->cur, "IMM" )) {
1244 if (!parse_immediate( ctx ))
1245 return FALSE;
1246 }
1247 else if (str_match_no_case( &ctx->cur, "PROPERTY" )) {
1248 if (!parse_property( ctx ))
1249 return FALSE;
1250 }
1251 else if (!parse_instruction( ctx, FALSE )) {
1252 return FALSE;
1253 }
1254 }
1255
1256 return TRUE;
1257 }
1258
1259 boolean
1260 tgsi_text_translate(
1261 const char *text,
1262 struct tgsi_token *tokens,
1263 uint num_tokens )
1264 {
1265 struct translate_ctx ctx;
1266
1267 ctx.text = text;
1268 ctx.cur = text;
1269 ctx.tokens = tokens;
1270 ctx.tokens_cur = tokens;
1271 ctx.tokens_end = tokens + num_tokens;
1272
1273 if (!translate( &ctx ))
1274 return FALSE;
1275
1276 return tgsi_sanity_check( tokens );
1277 }