/
mojoshader_assembler.c
1714 lines (1450 loc) · 49.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
/**
* MojoShader; generate shader programs from bytecode of compiled
* Direct3D shaders.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"
13
#define DEBUG_TOKENIZER 0
14
15
// !!! FIXME: no #define support yet.
16
17
typedef struct TokenizerContext
18
19
20
21
22
23
24
{
const char *source;
int on_endline;
unsigned int linenum;
char prevchar;
char token[64];
char pushedback;
25
} TokenizerContext;
26
27
28
29
30
31
32
33
34
35
36
typedef struct Context Context;
// Context...this is state that changes as we assemble a shader...
struct Context
{
MOJOSHADER_malloc malloc;
MOJOSHADER_free free;
void *malloc_data;
const char *failstr;
37
TokenizerContext tctx;
38
int started_parsing;
39
40
41
42
43
44
MOJOSHADER_shaderType shader_type;
uint8 major_ver;
uint8 minor_ver;
uint32 tokenbuf[16];
int tokenbufpos;
DestArgInfo dest_arg;
45
uint32 *output;
46
uint32 *token_to_line;
47
48
size_t output_len;
size_t output_allocation;
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
};
// Convenience functions for allocators...
static inline int out_of_memory(Context *ctx)
{
if (ctx->failstr == NULL)
ctx->failstr = out_of_mem_str; // fail() would call malloc().
return FAIL;
} // out_of_memory
static inline void *Malloc(Context *ctx, const size_t len)
{
void *retval = ctx->malloc((int) len, ctx->malloc_data);
if (retval == NULL)
out_of_memory(ctx);
return retval;
} // Malloc
static inline void Free(Context *ctx, void *ptr)
{
if (ptr != NULL) // check for NULL in case of dumb free() impl.
ctx->free(ptr, ctx->malloc_data);
} // Free
static int failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
static int failf(Context *ctx, const char *fmt, ...)
{
if (ctx->failstr == NULL) // don't change existing error.
{
char scratch = 0;
va_list ap;
va_start(ap, fmt);
const int len = vsnprintf(&scratch, sizeof (scratch), fmt, ap);
va_end(ap);
char *failstr = (char *) Malloc(ctx, len + 1);
if (failstr != NULL)
{
va_start(ap, fmt);
vsnprintf(failstr, len + 1, fmt, ap); // rebuild it.
va_end(ap);
ctx->failstr = failstr;
} // if
} // if
return FAIL;
} // failf
static inline int fail(Context *ctx, const char *reason)
{
return failf(ctx, "%s", reason);
} // fail
static inline int isfail(const Context *ctx)
{
return (ctx->failstr != NULL);
} // isfail
110
111
112
113
114
115
static inline int tokeq(const TokenizerContext *tctx, const char *token)
{
return (strcasecmp(tctx->token, token) == 0);
} // tokeq
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Shader model version magic...
static inline uint32 ver_ui32(const uint8 major, const uint8 minor)
{
return ( (((uint32) major) << 16) | (((minor) == 0xFF) ? 0 : (minor)) );
} // version_ui32
static inline int shader_version_atleast(const Context *ctx, const uint8 maj,
const uint8 min)
{
return (ver_ui32(ctx->major_ver, ctx->minor_ver) >= ver_ui32(maj, min));
} // shader_version_atleast
static inline int shader_is_pixel(const Context *ctx)
{
return (ctx->shader_type == MOJOSHADER_TYPE_PIXEL);
} // shader_is_pixel
static inline int shader_is_vertex(const Context *ctx)
{
return (ctx->shader_type == MOJOSHADER_TYPE_VERTEX);
} // shader_is_vertex
140
141
142
143
144
145
146
147
148
static int ui32fromstr(const char *str, uint32 *ui32)
{
//*ui32 = (uint32) atoi(minstr);
char *endptr = NULL;
const long val = strtol(str, &endptr, 10);
*ui32 = (uint32) val;
return ((val >= 0) && (*str != '\0') && (*endptr == '\0'));
} // ui32fromstr
149
150
151
152
153
154
static void output_token_noswap(Context *ctx, const uint32 token)
{
if (isfail(ctx))
return;
155
156
157
158
if (ctx->output_len >= ctx->output_allocation)
{
const size_t output_alloc_bump = 1024; // that's tokens, not bytes.
const size_t newsize = ctx->output_allocation + output_alloc_bump;
159
160
161
void *ptr;
ptr = Malloc(ctx, newsize * sizeof (uint32));
162
163
164
165
166
167
if (ptr == NULL)
return;
if (ctx->output_len > 0)
memcpy(ptr, ctx->output, ctx->output_len * sizeof (uint32));
Free(ctx, ctx->output);
ctx->output = (uint32 *) ptr;
168
169
170
171
172
173
174
175
176
177
ptr = Malloc(ctx, newsize * sizeof (uint32));
if (ptr == NULL)
return;
if (ctx->output_len > 0)
memcpy(ptr, ctx->token_to_line, ctx->output_len * sizeof (uint32));
Free(ctx, ctx->token_to_line);
ctx->token_to_line = (uint32 *) ptr;
ctx->output_allocation = newsize;
178
179
} // if
180
ctx->output[ctx->output_len] = token;
181
ctx->token_to_line[ctx->output_len] = ctx->tctx.linenum;
182
ctx->output_len++;
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
} // output_token_noswap
static inline void output_token(Context *ctx, const uint32 token)
{
output_token_noswap(ctx, SWAP32(token));
} // output_token
static void output_comment_bytes(Context *ctx, const uint8 *buf, size_t len)
{
if (len > (0xFFFF * 4)) // length is stored as token count, in 16 bits.
fail(ctx, "Comment field is too big");
else if (!isfail(ctx))
{
const uint32 tokencount = (len / 4) + ((len % 4) ? 1 : 0);
output_token(ctx, 0xFFFE | (tokencount << 16));
while (len >= 4)
{
output_token_noswap(ctx, *((const uint32 *) buf));
len -= 4;
buf += 4;
} // while
if (len > 0) // handle spillover...
{
union { uint8 ui8[4]; uint32 ui32; } overflow;
overflow.ui32 = 0;
memcpy(overflow.ui8, buf, len);
output_token_noswap(ctx, overflow.ui32);
} // if
} // else if
} // output_comment_bytes
static inline void output_comment_string(Context *ctx, const char *str)
{
output_comment_bytes(ctx, (const uint8 *) str, strlen(str));
} // output_comment_string
224
static int tokenize_ctx(Context *ctx, TokenizerContext *tctx)
225
226
227
228
229
230
{
int idx = 0;
if (isfail(ctx))
return FAIL;
231
if (tctx->pushedback)
232
{
233
tctx->pushedback = 0;
234
235
236
return NOFAIL;
} // if
237
if (tctx->on_endline)
238
{
239
240
tctx->on_endline = 0;
tctx->linenum++; // passed a newline, update.
241
242
} // if
243
244
while (1)
{
245
// !!! FIXME: carefully crafted (but legal) comments can trigger this.
246
if (idx >= sizeof (tctx->token))
247
248
return fail(ctx, "buffer overflow");
249
char ch = *tctx->source;
250
251
252
253
if (ch == '\t')
ch = ' '; // collapse tabs into single spaces.
else if (ch == '\r')
{
254
if (tctx->source[1] == '\n')
255
256
257
258
continue; // ignore '\r' if this is "\r\n" ...
ch = '\n';
} // else if
259
if ((ch >= '0') && (ch <= '9'))
260
261
{
// starting a number, but rest of current token was not number.
262
if ((idx > 0) && ((tctx->prevchar < '0') || (tctx->prevchar > '9')))
263
{
264
tctx->token[idx++] = '\0';
265
266
267
268
269
270
return NOFAIL;
} // if
} // if
else
{
// starting a non-number, but rest of current token was numbers.
271
if ((idx > 0) && ((tctx->prevchar >= '0') && (tctx->prevchar <= '9')))
272
{
273
tctx->token[idx++] = '\0';
274
275
276
277
278
279
280
281
282
return NOFAIL;
} // if
} // else
switch (ch)
{
case '/':
case ';': // !!! FIXME: comment, right?
if (idx != 0) // finish off existing token.
283
tctx->token[idx] = '\0';
284
285
else
{
286
287
288
tctx->token[idx++] = ch;
tctx->source++;
if ((ch == '/') && (*tctx->source == '/'))
289
{
290
291
tctx->token[idx++] = '/';
tctx->source++;
292
} // if
293
tctx->token[idx++] = '\0';
294
295
296
297
} // else
return NOFAIL;
case ' ':
298
if (tctx->prevchar == ' ')
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
break; // multiple whitespace collapses into one.
// intentional fall-through...
case '_':
case '[':
case ']':
case '(':
case ')':
case '!':
case '+':
case '-':
case ',':
case '.':
case '\n':
if (idx != 0) // finish off existing token.
314
tctx->token[idx] = '\0';
315
316
317
else // this is a token in itself.
{
if (ch == '\n')
318
319
320
321
tctx->on_endline = 1;
tctx->source++;
tctx->token[idx++] = ch;
tctx->token[idx++] = '\0';
322
323
324
325
} // else
return NOFAIL;
case '\0':
326
tctx->token[idx] = '\0';
327
328
329
330
331
if (idx != 0) // had any chars? It's a token.
return NOFAIL;
return END_OF_STREAM;
default:
332
333
tctx->source++;
tctx->token[idx++] = ch;
334
335
336
break;
} // switch
337
tctx->prevchar = ch;
338
339
340
} // while
return fail(ctx, "???"); // shouldn't hit this.
341
} // tokenize_ctx
342
343
344
345
static inline int tokenize(Context *ctx)
{
346
347
const int rc = tokenize_ctx(ctx, &ctx->tctx);
348
349
350
351
352
#if DEBUG_TOKENIZER
printf("TOKENIZE: %s '%s'\n",
(rc == END_OF_STREAM) ? "END_OF_STREAM" :
(rc == FAIL) ? "FAIL" :
(rc == NOFAIL) ? "NOFAIL" : "???",
353
(ctx->tctx.token[0] == '\n') ? "\\n" : ctx->tctx.token);
354
#endif
355
356
357
358
359
return rc;
} // tokenize
360
static int pushback_ctx(Context *ctx, TokenizerContext *tctx)
361
{
362
if (tctx->pushedback)
363
return fail(ctx, "BUG: Double pushback in parser");
364
else
365
tctx->pushedback = 1;
366
367
return NOFAIL;
368
369
370
371
372
373
374
375
376
377
378
379
380
381
}
static inline int pushback(Context *ctx)
{
const int rc = pushback_ctx(ctx, &ctx->tctx);
#if DEBUG_TOKENIZER
printf("PUSHBACK: %s\n",
(rc == END_OF_STREAM) ? "END_OF_STREAM" :
(rc == FAIL) ? "FAIL" :
(rc == NOFAIL) ? "NOFAIL" : "???");
#endif
return rc;
382
383
384
} // pushback
385
386
387
static int nexttoken_ctx(Context *ctx, TokenizerContext *tctx,
const int ignoreeol, const int ignorewhitespace,
const int eolok, const int eosok)
388
389
390
{
int rc = NOFAIL;
391
while ((rc = tokenize_ctx(ctx, tctx)) == NOFAIL)
392
{
393
if (tokeq(tctx, "\n"))
394
395
396
397
398
399
400
{
if (ignoreeol)
continue;
else if (!eolok)
return fail(ctx, "Unexpected EOL");
} // if
401
else if (tokeq(tctx, " "))
402
403
404
405
406
407
{
if (ignorewhitespace)
continue;
} // else if
// skip comments...
408
else if (tokeq(tctx, "//") || tokeq(tctx, ";"))
409
{
410
while ((rc = tokenize_ctx(ctx, tctx)) == NOFAIL)
411
{
412
if (tokeq(tctx, "\n"))
413
{
414
pushback_ctx(ctx, tctx);
415
break;
416
} // if
417
} // while
418
continue; // pick up from newline, go again.
419
420
421
422
423
} // if
break;
} // while
424
425
426
427
428
429
430
431
432
433
434
435
436
437
if ((rc == END_OF_STREAM) && (!eosok))
return fail(ctx, "Unexpected EOF");
return rc;
} // nexttoken_ctx
static inline int nexttoken(Context *ctx, const int ignoreeol,
const int ignorewhitespace, const int eolok,
const int eosok)
{
const int rc = nexttoken_ctx(ctx, &ctx->tctx, ignoreeol,
ignorewhitespace, eolok, eosok);
438
439
440
441
442
#if DEBUG_TOKENIZER
printf("NEXTTOKEN: %s '%s'\n",
(rc == END_OF_STREAM) ? "END_OF_STREAM" :
(rc == FAIL) ? "FAIL" :
(rc == NOFAIL) ? "NOFAIL" : "???",
443
(ctx->tctx.token[0] == '\n') ? "\\n" : ctx->tctx.token);
444
445
446
447
448
449
450
451
#endif
return rc;
} // nexttoken
static int require_endline(Context *ctx)
{
452
TokenizerContext *tctx = &ctx->tctx;
453
454
455
456
457
const int rc = nexttoken(ctx, 0, 1, 1, 1);
if (rc == FAIL)
return FAIL;
else if (rc == END_OF_STREAM)
return NOFAIL; // we'll call this an EOL.
458
else if (!tokeq(tctx, "\n"))
459
460
461
462
463
return fail(ctx, "Endline expected");
return NOFAIL;
} // require_endline
464
static int require_comma(Context *ctx)
465
{
466
TokenizerContext *tctx = &ctx->tctx;
467
468
469
const int rc = nexttoken(ctx, 0, 1, 0, 0);
if (rc == FAIL)
return FAIL;
470
else if (!tokeq(tctx, ","))
471
return fail(ctx, "Comma expected");
472
return NOFAIL;
473
} // require_comma
474
475
476
477
static int parse_register_name(Context *ctx, RegisterType *rtype, int *rnum)
{
478
TokenizerContext *tctx = &ctx->tctx;
479
480
481
482
483
484
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
int neednum = 1;
int regnum = 0;
RegisterType regtype = REG_TYPE_TEMP;
485
if (tokeq(tctx, "r"))
486
regtype = REG_TYPE_TEMP;
487
else if (tokeq(tctx, "v"))
488
regtype = REG_TYPE_INPUT;
489
else if (tokeq(tctx, "c"))
490
regtype = REG_TYPE_CONST;
491
else if (tokeq(tctx, "i"))
492
regtype = REG_TYPE_CONSTINT;
493
else if (tokeq(tctx, "b"))
494
regtype = REG_TYPE_CONSTBOOL;
495
else if (tokeq(tctx, "oC"))
496
regtype = REG_TYPE_COLOROUT;
497
else if (tokeq(tctx, "s"))
498
regtype = REG_TYPE_SAMPLER;
499
else if (tokeq(tctx, "oD"))
500
regtype = REG_TYPE_ATTROUT;
501
else if (tokeq(tctx, "l"))
502
regtype = REG_TYPE_LABEL;
503
else if (tokeq(tctx, "p"))
504
regtype = REG_TYPE_PREDICATE;
505
else if (tokeq(tctx, "oDepth"))
506
507
508
509
{
regtype = REG_TYPE_DEPTHOUT;
neednum = 0;
} // else if
510
else if (tokeq(tctx, "aL"))
511
512
513
514
{
regtype = REG_TYPE_LOOP;
neednum = 0;
} // else if
515
else if (tokeq(tctx, "o"))
516
517
518
519
520
{
if (!shader_is_vertex(ctx) || !shader_version_atleast(ctx, 3, 0))
return fail(ctx, "Output register not valid in this shader type");
regtype = REG_TYPE_OUTPUT;
} // else if
521
else if (tokeq(tctx, "oT"))
522
{
523
if (shader_is_vertex(ctx) && shader_version_atleast(ctx, 3, 0))
524
525
526
return fail(ctx, "Output register not valid in this shader type");
regtype = REG_TYPE_OUTPUT;
} // else if
527
else if (tokeq(tctx, "a"))
528
529
530
531
532
{
if (!shader_is_vertex(ctx))
return fail(ctx, "Address register only valid in vertex shaders.");
regtype = REG_TYPE_ADDRESS;
} // else if
533
else if (tokeq(tctx, "t"))
534
535
536
537
538
{
if (!shader_is_pixel(ctx))
return fail(ctx, "Address register only valid in pixel shaders.");
regtype = REG_TYPE_ADDRESS;
} // else if
539
else if (tokeq(tctx, "vPos"))
540
541
542
543
544
{
regtype = REG_TYPE_MISCTYPE;
regnum = (int) MISCTYPE_TYPE_POSITION;
neednum = 0;
} // else if
545
else if (tokeq(tctx, "vFace"))
546
547
548
549
550
{
regtype = REG_TYPE_MISCTYPE;
regnum = (int) MISCTYPE_TYPE_FACE;
neednum = 0;
} // else if
551
else if (tokeq(tctx, "oPos"))
552
553
554
555
556
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_POSITION;
neednum = 0;
} // else if
557
else if (tokeq(tctx, "oFog"))
558
559
560
561
562
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_FOG;
neednum = 0;
} // else if
563
else if (tokeq(tctx, "oPts"))
564
565
566
567
568
569
570
571
572
573
574
575
576
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_POINT_SIZE;
neednum = 0;
} // else if
//case REG_TYPE_TEMPFLOAT16: // !!! FIXME: don't know this asm string
else
{
return fail(ctx, "expected register type");
} // else
577
578
if (neednum)
{
579
580
581
582
583
584
// Make a temp TokenizerContext, since we need to skip whitespace here,
// but if the next non-whitespace token isn't '[', we'll want to get
// that whitespace back.
TokenizerContext tmptctx;
memcpy(&tmptctx, tctx, sizeof (TokenizerContext));
if (nexttoken_ctx(ctx, &tmptctx, 0, 1, 1, 1) == FAIL)
585
return FAIL;
586
else if (tokeq(&ctx->tctx, "["))
587
588
589
neednum = 0;
} // if
590
591
592
593
594
if (neednum)
{
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
595
uint32 ui32 = 0;
596
if (!ui32fromstr(tctx->token, &ui32))
597
598
return fail(ctx, "Invalid register index");
regnum = (int) ui32;
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
} // if
// split up REG_TYPE_CONST
if (regtype == REG_TYPE_CONST)
{
if (regnum < 2048)
{
regtype = REG_TYPE_CONST;
regnum -= 0;
} // if
else if (regnum < 4096)
{
regtype = REG_TYPE_CONST2;
regnum -= 2048;
} // if
else if (regnum < 6144)
{
regtype = REG_TYPE_CONST3;
regnum -= 4096;
} // if
else if (regnum < 8192)
{
regtype = REG_TYPE_CONST4;
regnum -= 6144;
} // if
else
{
return fail(ctx, "Invalid const register index");
} // else
} // if
*rtype = regtype;
*rnum = regnum;
return NOFAIL;
} // parse_register_name
637
static int set_result_shift(Context *ctx, DestArgInfo *info, const int val)
638
{
639
640
641
642
643
if (info->result_shift != 0)
return fail(ctx, "Multiple result shift modifiers");
info->result_shift = val;
return NOFAIL;
} // set_result_shift
644
645
646
static int parse_destination_token(Context *ctx)
647
{
648
649
650
TokenizerContext *tctx = &ctx->tctx;
DestArgInfo *info = &ctx->dest_arg;
651
memset(info, '\0', sizeof (DestArgInfo));
652
653
654
655
656
657
// See if there are destination modifiers on the instruction itself...
while (1)
{
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
658
else if (tokeq(tctx, " "))
659
break; // done with modifiers.
660
else if (!tokeq(tctx, "_"))
661
662
663
return fail(ctx, "Expected modifier or whitespace");
else if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
664
// !!! FIXME: this can be cleaned up when tokenizer is fixed.
665
else if (tokeq(tctx, "x"))
666
667
668
{
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
669
else if (tokeq(tctx, "2"))
670
set_result_shift(ctx, info, 0x1);
671
else if (tokeq(tctx, "4"))
672
set_result_shift(ctx, info, 0x2);
673
else if (tokeq(tctx, "8"))
674
675
676
677
678
set_result_shift(ctx, info, 0x3);
else
return fail(ctx, "Expected modifier");
} // else if
// !!! FIXME: this can be cleaned up when tokenizer is fixed.
679
else if (tokeq(tctx, "d"))
680
681
682
{
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
683
else if (tokeq(tctx, "8"))
684
set_result_shift(ctx, info, 0xD);
685
else if (tokeq(tctx, "4"))
686
set_result_shift(ctx, info, 0xE);
687
else if (tokeq(tctx, "2"))
688
689
690
691
set_result_shift(ctx, info, 0xF);
else
return fail(ctx, "Expected modifier");
} // else if
692
else if (tokeq(tctx, "sat"))
693
info->result_mod |= MOD_SATURATE;
694
else if (tokeq(tctx, "pp"))
695
info->result_mod |= MOD_PP;
696
else if (tokeq(tctx, "centroid"))
697
698
699
700
701
info->result_mod |= MOD_CENTROID;
else
return fail(ctx, "Expected modifier");
} // while
702
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
703
704
705
return FAIL;
// !!! FIXME: predicates.
706
if (tokeq(tctx, "("))
707
708
709
710
711
712
return fail(ctx, "Predicates unsupported at this time");
pushback(ctx); // parse_register_name calls nexttoken().
if (parse_register_name(ctx, &info->regtype, &info->regnum) == FAIL)
return FAIL;
713
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
714
715
716
717
return FAIL;
// !!! FIXME: can dest registers do relative addressing?
718
if (!tokeq(tctx, "."))
719
720
721
722
723
{
info->writemask = 0xF;
info->writemask0 = info->writemask1 = info->writemask2 = info->writemask3 = 1;
pushback(ctx); // no explicit writemask; do full mask.
} // if
724
else if (scalar_register(ctx->shader_type, info->regtype, info->regnum))
725
return fail(ctx, "Writemask specified for scalar register");
726
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
727
return FAIL;
728
else if (tokeq(tctx, ""))
729
730
731
return fail(ctx, "Invalid writemask");
else
{
732
char *ptr = tctx->token;
733
info->writemask0 = info->writemask1 = info->writemask2 = info->writemask3 = 0;
734
735
736
737
if (*ptr == 'x') { info->writemask0 = 1; ptr++; }
if (*ptr == 'y') { info->writemask1 = 1; ptr++; }
if (*ptr == 'z') { info->writemask2 = 1; ptr++; }
if (*ptr == 'w') { info->writemask3 = 1; ptr++; }
738
if ((ptr == tctx->token) && (shader_is_pixel(ctx)))
739
{
740
741
742
743
if (*ptr == 'r') { info->writemask0 = 1; ptr++; }
if (*ptr == 'g') { info->writemask1 = 1; ptr++; }
if (*ptr == 'b') { info->writemask2 = 1; ptr++; }
if (*ptr == 'a') { info->writemask3 = 1; ptr++; }
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
} // if
if (*ptr != '\0')
return fail(ctx, "Invalid writemask");
info->writemask = ( ((info->writemask0 & 0x1) << 0) |
((info->writemask1 & 0x1) << 1) |
((info->writemask2 & 0x1) << 2) |
((info->writemask3 & 0x1) << 3) );
} // else
info->orig_writemask = info->writemask;
if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
return fail(ctx, "Too many tokens");
ctx->tokenbuf[ctx->tokenbufpos++] =
761
( ((((uint32) 1)) << 31) |
762
763
764
765
((((uint32) info->regnum) & 0x7ff) << 0) |
((((uint32) info->relative) & 0x1) << 13) |
((((uint32) info->result_mod) & 0xF) << 20) |
((((uint32) info->result_shift) & 0xF) << 24) |
766
((((uint32) info->writemask) & 0xF) << 16) |
767
768
769
770
771
772
773
((((uint32) info->regtype) & 0x7) << 28) |
((((uint32) info->regtype) & 0x18) << 8) );
return 1;
} // parse_destination_token
774
775
776
static void set_source_mod(Context *ctx, const int negate,
const SourceMod norm, const SourceMod negated,
SourceMod *srcmod)
777
{
778
779
780
781
782
if ( (*srcmod != SRCMOD_NONE) || (negate && (negated == SRCMOD_NONE)) )
fail(ctx, "Incompatible source modifiers");
else
*srcmod = ((negate) ? negated : norm);
} // set_source_mod
783
784
785
static int parse_source_token_maybe_relative(Context *ctx, const int relok)
786
{
787
TokenizerContext *tctx = &ctx->tctx;
788
789
790
791
792
793
794
795
796
797
798
int retval = 1;
if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
return fail(ctx, "Too many tokens");
// mark this now, so optional relative addressing token is placed second.
uint32 *token = &ctx->tokenbuf[ctx->tokenbufpos++];
SourceMod srcmod = SRCMOD_NONE;
int negate = 0;
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
799
return FAIL;
800
else if (tokeq(tctx, "1"))
801
802
803
{
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
804
else if (!tokeq(tctx, "-"))
805
806
807
808
return fail(ctx, "Unexpected value");
else
srcmod = SRCMOD_COMPLEMENT;
} // else
809
else if (tokeq(tctx, "!"))
810
srcmod = SRCMOD_NOT;
811
else if (tokeq(tctx, "-"))
812
813
814
negate = 1;
else
pushback(ctx);
815
816
817
818
819
820
821
RegisterType regtype;
int regnum;
if (parse_register_name(ctx, ®type, ®num) == FAIL)
return FAIL;
else if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
return FAIL;
822
else if (!tokeq(tctx, "_"))
823
824
825
pushback(ctx);
else if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
826
else if (tokeq(tctx, "bias"))
827
set_source_mod(ctx, negate, SRCMOD_BIAS, SRCMOD_BIASNEGATE, &srcmod);
828
else if (tokeq(tctx, "bx2"))
829
set_source_mod(ctx, negate, SRCMOD_SIGN, SRCMOD_SIGNNEGATE, &srcmod);
830
else if (tokeq(tctx, "x2"))
831
set_source_mod(ctx, negate, SRCMOD_X2, SRCMOD_X2NEGATE, &srcmod);
832
else if (tokeq(tctx, "dz"))
833
set_source_mod(ctx, negate, SRCMOD_DZ, SRCMOD_NONE, &srcmod);
834
else if (tokeq(tctx, "dw"))
835
set_source_mod(ctx, negate, SRCMOD_DW, SRCMOD_NONE, &srcmod);
836
else if (tokeq(tctx, "abs"))
837
set_source_mod(ctx, negate, SRCMOD_ABS, SRCMOD_ABSNEGATE, &srcmod);
838
839
else
return fail(ctx, "Invalid source modifier");
840
841
842
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
return FAIL;
843
844
uint32 relative = 0;
845
if (!tokeq(tctx, "["))
846
847
848
849
850
851
852
853
854
855
pushback(ctx); // not relative addressing?
else if (!relok)
return fail(ctx, "Relative addressing not permitted here.");
else
{
const int rc = parse_source_token_maybe_relative(ctx, 0);
if (rc == FAIL)
return FAIL;
retval += rc;
relative = 1;
856
857
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
858
else if (!tokeq(tctx, "+"))
859
860
861
862
863
864
865
866
pushback(ctx);
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
else
{
if (regnum != 0) // !!! FIXME: maybe c3[a0.x + 5] is legal and becomes c[a0.x + 8] ?
fail(ctx, "Relative addressing with explicit register number.");
uint32 ui32 = 0;
867
if (!ui32fromstr(tctx->token, &ui32))
868
869
870
871
return fail(ctx, "Invalid relative addressing offset");
regnum += (int) ui32;
} // else
872
873
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
874
else if (!tokeq(tctx, "]"))
875
876
return fail(ctx, "Expected ']'");
} // else
877
878
879
880
881
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
return FAIL;
uint32 swizzle = 0;
882
if (!tokeq(tctx, "."))
883
884
885
886
{
swizzle = 0xE4; // 0xE4 == 11100100 ... 0 1 2 3. No swizzle.
pushback(ctx); // no explicit writemask; do full mask.
} // if
887
else if (scalar_register(ctx->shader_type, regtype, regnum))
888
889
return fail(ctx, "Swizzle specified for scalar register");
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
890
return FAIL;
891
else if (tokeq(tctx, ""))
892
893
894
895
return fail(ctx, "Invalid swizzle");
else
{
// deal with shortened form (.x = .xxxx, etc).
896
897
898
899
900
901
902
if (tctx->token[1] == '\0')
tctx->token[1] = tctx->token[2] = tctx->token[3] = tctx->token[0];
else if (tctx->token[2] == '\0')
tctx->token[2] = tctx->token[3] = tctx->token[1];
else if (tctx->token[3] == '\0')
tctx->token[3] = tctx->token[2];
else if (tctx->token[4] != '\0')
903
return fail(ctx, "Invalid swizzle");
904
tctx->token[4] = '\0';
905
906
907
908
uint32 val;
int saw_xyzw = 0;
int saw_rgba = 0;
909
int i;
910
911
for (i = 0; i < 4; i++)
{
912
const int component = (int) tctx->token[i];
913
914
915
916
917
918
919
920
921
922
923
924
925
926
switch (component)
{
case 'x': val = 0; saw_xyzw = 1; break;
case 'y': val = 1; saw_xyzw = 1; break;
case 'z': val = 2; saw_xyzw = 1; break;
case 'w': val = 3; saw_xyzw = 1; break;
case 'r': val = 0; saw_rgba = 1; break;
case 'g': val = 1; saw_rgba = 1; break;
case 'b': val = 2; saw_rgba = 1; break;
case 'a': val = 3; saw_rgba = 1; break;
default: return fail(ctx, "Invalid swizzle");
} // switch
swizzle |= (val << (i * 2));
} // for
927
928
929
930
if (saw_xyzw && saw_rgba)
return fail(ctx, "Invalid swizzle");
} // else
931
932
933
934
935
936
937
938
*token = ( ((((uint32) 1)) << 31) |
((((uint32) regnum) & 0x7ff) << 0) |
((((uint32) relative) & 0x1) << 13) |
((((uint32) swizzle) & 0xFF) << 16) |
((((uint32) srcmod) & 0xF) << 24) |
((((uint32) regtype) & 0x7) << 28) |
((((uint32) regtype) & 0x18) << 8) );
939
940
941
return retval;
} // parse_source_token_maybe_relative
942
943
944
static inline int parse_source_token(Context *ctx)
945
{
946
947
return parse_source_token_maybe_relative(ctx, 1);
} // parse_source_token
948
949
950
951
952
953
static int parse_args_NULL(Context *ctx)
{
return (isfail(ctx) ? FAIL : 1);
} // parse_args_NULL
954
955
956
957
static int parse_num(Context *ctx, const int floatok, uint32 *token)
{
958
TokenizerContext *tctx = &ctx->tctx;
959
960
961
int32 negative = 1;
union { float f; int32 si32; uint32 ui32; } cvt;
cvt.si32 = 0;
962
963
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
964
return FAIL;
965
else if (tokeq(tctx, "-"))
966
967
negative = -1;
else
968
pushback(ctx);
969
970
971
972
uint32 val = 0;
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
973
else if (!ui32fromstr(tctx->token, &val))
974
975
976
return fail(ctx, "Expected number");
uint32 fraction = 0;
977
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
978
return FAIL;
979
else if (!tokeq(tctx, "."))
980
981
982
983
pushback(ctx); // whole number
else if (!floatok)
return fail(ctx, "Expected whole number");
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
984
return FAIL;
985
else if (!ui32fromstr(tctx->token, &fraction))
986
987
return fail(ctx, "Expected number");
988
989
990
991
uint32 exponent = 0;
int negexp = 0;
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
return FAIL;
992
else if (!tokeq(tctx, "e"))
993
994
995
996
997
998
999
pushback(ctx);
else if (!floatok)
return fail(ctx, "Exponent on whole number"); // !!! FIXME: illegal?
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
else
{
1000
if (!tokeq(tctx, "-"))