/
mojoshader_assembler.c
1847 lines (1543 loc) · 53 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
14
15
16
17
18
19
#if DEBUG_ASSEMBLY_PARSER
#define print_debug_token(token, len, val) \
MOJOSHADER_print_debug_token("ASSEMBLER", token, len, val)
#else
#define print_debug_token(token, len, val)
#endif
20
21
22
23
// Simple linked list to cache source filenames, so we don't have to copy
// the same string over and over for each opcode.
typedef struct FilenameCache
24
{
25
26
27
char *filename;
struct FilenameCache *next;
} FilenameCache;
28
29
30
31
32
33
34
35
typedef struct SourcePos
{
const char *filename;
uint32 line;
} SourcePos;
36
37
// Context...this is state that changes as we assemble a shader...
38
typedef struct Context
39
{
40
41
int isfail;
int out_of_memory;
42
43
44
MOJOSHADER_malloc malloc;
MOJOSHADER_free free;
void *malloc_data;
45
46
int error_count;
ErrorList *errors;
47
Preprocessor *preprocessor;
48
MOJOSHADER_parsePhase parse_phase;
49
50
51
MOJOSHADER_shaderType shader_type;
uint8 major_ver;
uint8 minor_ver;
52
53
54
55
56
57
58
int pushedback;
const char *token; // assembler token!
unsigned int tokenlen; // assembler token!
Token tokenval; // assembler token!
uint32 version_token; // bytecode token!
uint32 tokenbuf[16]; // bytecode tokens!
int tokenbufpos; // bytecode tokens!
59
DestArgInfo dest_arg;
60
uint32 *output;
61
SourcePos *token_to_source;
62
63
64
uint8 *ctab;
uint32 ctab_len;
uint32 ctab_allocation;
65
66
size_t output_len;
size_t output_allocation;
67
FilenameCache *filename_cache;
68
} Context;
69
70
71
72
// Convenience functions for allocators...
73
static inline void out_of_memory(Context *ctx)
74
{
75
ctx->isfail = ctx->out_of_memory = 1;
76
77
78
79
80
81
82
83
84
85
} // 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
86
87
88
static inline char *StrDup(Context *ctx, const char *str)
{
char *retval = (char *) Malloc(ctx, strlen(str) + 1);
89
if (retval != NULL)
90
91
92
93
strcpy(retval, str);
return retval;
} // StrDup
94
95
96
97
98
99
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
100
101
static void failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
static void failf(Context *ctx, const char *fmt, ...)
102
{
103
104
105
106
const char *fname = NULL;
unsigned int linenum = 0;
int error_position = 0;
107
108
ctx->isfail = 1;
109
110
111
112
113
114
switch (ctx->parse_phase)
{
case MOJOSHADER_PARSEPHASE_NOTSTARTED:
error_position = -2;
break;
case MOJOSHADER_PARSEPHASE_WORKING:
115
116
fname = preprocessor_sourcepos(ctx->preprocessor, &linenum);
error_position = (int) linenum;
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
break;
case MOJOSHADER_PARSEPHASE_DONE:
error_position = -1;
break;
default:
assert(0 && "Unexpected value");
return;
} // switch
ErrorList *error = (ErrorList *) Malloc(ctx, sizeof (ErrorList));
if (error == NULL)
return;
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)
Free(ctx, error);
else
140
141
{
va_start(ap, fmt);
142
vsnprintf(failstr, len + 1, fmt, ap); // rebuild it.
143
144
va_end(ap);
145
146
147
error->error.error = failstr;
error->error.filename = fname ? StrDup(ctx, fname) : NULL;
error->error.error_position = error_position;
148
error->next = NULL;
149
150
ErrorList *prev = NULL;
151
152
ErrorList *item = ctx->errors;
while (item != NULL)
153
{
154
prev = item;
155
item = item->next;
156
157
} // while
158
if (prev == NULL)
159
ctx->errors = error;
160
161
else
prev->next = error;
162
163
164
ctx->error_count++;
} // else
165
166
} // failf
167
static inline void fail(Context *ctx, const char *reason)
168
{
169
failf(ctx, "%s", reason);
170
171
172
173
} // fail
static inline int isfail(const Context *ctx)
{
174
return ctx->isfail;
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
} // isfail
// 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
201
202
static inline void pushback(Context *ctx)
{
203
204
205
#if DEBUG_ASSEMBLY_PARSER
printf("ASSEMBLER PUSHBACK\n");
#endif
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
assert(!ctx->pushedback);
ctx->pushedback = 1;
} // pushback
static Token _nexttoken(Context *ctx)
{
ctx->token = preprocessor_nexttoken(ctx->preprocessor, &ctx->tokenlen,
&ctx->tokenval);
if (preprocessor_outofmemory(ctx->preprocessor))
{
out_of_memory(ctx);
ctx->tokenval = TOKEN_EOI;
ctx->token = NULL;
ctx->tokenlen = 0;
} // if
else
{
const char *err = preprocessor_error(ctx->preprocessor);
if (err)
fail(ctx, err);
} // else
return ctx->tokenval;
} // _nexttoken
static Token nexttoken(Context *ctx)
{
if (ctx->pushedback)
{
237
print_debug_token(ctx->token, ctx->tokenlen, ctx->tokenval);
238
239
240
241
242
ctx->pushedback = 0;
return ctx->tokenval;
} // if
Token token = _nexttoken(ctx);
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
while (token == ((Token) '\n'))
token = _nexttoken(ctx); // skip endlines.
if (token == ((Token) ';')) // single line comment in assembler.
{
do
{
token = _nexttoken(ctx);
} while ((token != ((Token) '\n')) && (token != TOKEN_EOI));
while (token == ((Token) '\n'))
token = _nexttoken(ctx); // skip endlines.
} // if
258
print_debug_token(ctx->token, ctx->tokenlen, ctx->tokenval);
259
260
261
262
263
return token;
} // nexttoken
static const char *cache_filename(Context *ctx, const char *fname)
264
{
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
if (fname == NULL)
return NULL;
// !!! FIXME: this could be optimized into a hash table, but oh well.
FilenameCache *item = ctx->filename_cache;
while (item != NULL)
{
if (strcmp(item->filename, fname) == 0)
return item->filename;
item = item->next;
} // while
// new cache item.
item = (FilenameCache *) Malloc(ctx, sizeof (FilenameCache));
if (item == NULL)
return NULL;
item->filename = (char *) Malloc(ctx, strlen(fname) + 1);
if (item->filename == NULL)
{
Free(ctx, item);
return NULL;
} // if
strcpy(item->filename, fname);
item->next = ctx->filename_cache;
ctx->filename_cache = item;
return item->filename;
} // cache_filename
static void free_filename_cache(Context *ctx)
{
FilenameCache *item = ctx->filename_cache;
while (item != NULL)
{
FilenameCache *next = item->next;
Free(ctx, item->filename);
Free(ctx, item);
item = next;
} // while
} // free_filename_cache
308
309
310
311
static inline void add_token_sourcepos(Context *ctx, const size_t idx)
{
312
313
314
315
unsigned int pos = 0;
const char *fname = preprocessor_sourcepos(ctx->preprocessor, &pos);
ctx->token_to_source[idx].line = pos;
ctx->token_to_source[idx].filename = cache_filename(ctx, fname);
316
317
318
} // add_token_sourcepos
319
320
321
322
323
static void output_token_noswap(Context *ctx, const uint32 token)
{
if (isfail(ctx))
return;
324
325
326
327
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;
328
329
330
void *ptr;
ptr = Malloc(ctx, newsize * sizeof (uint32));
331
332
333
334
335
336
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;
337
338
ptr = Malloc(ctx, newsize * sizeof (SourcePos));
339
340
341
if (ptr == NULL)
return;
if (ctx->output_len > 0)
342
343
344
memcpy(ptr, ctx->token_to_source, ctx->output_len * sizeof (SourcePos));
Free(ctx, ctx->token_to_source);
ctx->token_to_source = (SourcePos *) ptr;
345
346
ctx->output_allocation = newsize;
347
348
} // if
349
ctx->output[ctx->output_len] = token;
350
add_token_sourcepos(ctx, ctx->output_len);
351
ctx->output_len++;
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
} // 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
393
static int require_comma(Context *ctx)
394
{
395
396
const Token token = nexttoken(ctx);
if (token != ((Token) ','))
397
{
398
399
fail(ctx, "Comma expected");
return 0;
400
} // if
401
402
return 1;
} // require_comma
403
404
405
static int check_token_segment(Context *ctx, const char *str)
406
{
407
408
409
410
411
412
413
414
// !!! FIXME: these are case-insensitive, right?
const size_t len = strlen(str);
if ( (ctx->tokenlen < len) || (strncasecmp(ctx->token, str, len) != 0) )
return 0;
ctx->token += len;
ctx->tokenlen -= len;
return 1;
} // check_token_segment
415
416
417
static int check_token(Context *ctx, const char *str)
418
{
419
420
const size_t len = strlen(str);
if ( (ctx->tokenlen != len) || (strncasecmp(ctx->token, str, len) != 0) )
421
return 0;
422
423
ctx->token += len;
ctx->tokenlen = 0;
424
return 1;
425
} // check_token
426
427
428
static int ui32fromtoken(Context *ctx, uint32 *_val)
429
{
430
431
432
433
434
435
int i;
for (i = 0; i < ctx->tokenlen; i++)
{
if ((ctx->token[i] < '0') || (ctx->token[i] > '9'))
break;
} // for
436
437
if (i == 0)
438
{
439
440
*_val = 0;
return 0;
441
442
} // if
443
444
445
446
const int len = i;
uint32 val = 0;
uint32 mult = 1;
while (i--)
447
{
448
449
450
val += ((uint32) (ctx->token[i] - '0')) * mult;
mult *= 10;
} // while
451
452
453
ctx->token += len;
ctx->tokenlen -= len;
454
455
*_val = val;
456
return 1;
457
} // ui32fromtoken
458
459
460
461
static int parse_register_name(Context *ctx, RegisterType *rtype, int *rnum)
{
462
463
464
if (nexttoken(ctx) != TOKEN_IDENTIFIER)
{
fail(ctx, "Expected register");
465
return 0;
466
} // if
467
468
469
470
int neednum = 1;
int regnum = 0;
RegisterType regtype = REG_TYPE_TEMP;
471
472
473
474
// Watch out for substrings! oDepth must be checked before oD, since
// the latter will match either case.
if (check_token_segment(ctx, "oDepth"))
475
476
477
478
{
regtype = REG_TYPE_DEPTHOUT;
neednum = 0;
} // else if
479
else if (check_token_segment(ctx, "vFace"))
480
{
481
482
regtype = REG_TYPE_MISCTYPE;
regnum = (int) MISCTYPE_TYPE_FACE;
483
484
neednum = 0;
} // else if
485
else if (check_token_segment(ctx, "vPos"))
486
487
488
489
490
{
regtype = REG_TYPE_MISCTYPE;
regnum = (int) MISCTYPE_TYPE_POSITION;
neednum = 0;
} // else if
491
else if (check_token_segment(ctx, "oPos"))
492
493
494
495
496
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_POSITION;
neednum = 0;
} // else if
497
else if (check_token_segment(ctx, "oFog"))
498
499
500
501
502
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_FOG;
neednum = 0;
} // else if
503
else if (check_token_segment(ctx, "oPts"))
504
505
506
507
508
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_POINT_SIZE;
neednum = 0;
} // else if
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
else if (check_token_segment(ctx, "aL"))
{
regtype = REG_TYPE_LOOP;
neednum = 0;
} // else if
else if (check_token_segment(ctx, "oC"))
regtype = REG_TYPE_COLOROUT;
else if (check_token_segment(ctx, "oT"))
regtype = REG_TYPE_OUTPUT;
else if (check_token_segment(ctx, "oD"))
regtype = REG_TYPE_ATTROUT;
else if (check_token_segment(ctx, "r"))
regtype = REG_TYPE_TEMP;
else if (check_token_segment(ctx, "v"))
regtype = REG_TYPE_INPUT;
else if (check_token_segment(ctx, "c"))
regtype = REG_TYPE_CONST;
else if (check_token_segment(ctx, "i"))
regtype = REG_TYPE_CONSTINT;
else if (check_token_segment(ctx, "b"))
regtype = REG_TYPE_CONSTBOOL;
else if (check_token_segment(ctx, "s"))
regtype = REG_TYPE_SAMPLER;
else if (check_token_segment(ctx, "l"))
regtype = REG_TYPE_LABEL;
else if (check_token_segment(ctx, "p"))
regtype = REG_TYPE_PREDICATE;
else if (check_token_segment(ctx, "o"))
regtype = REG_TYPE_OUTPUT;
else if (check_token_segment(ctx, "a"))
regtype = REG_TYPE_ADDRESS;
else if (check_token_segment(ctx, "t"))
regtype = REG_TYPE_ADDRESS;
542
543
544
545
546
//case REG_TYPE_TEMPFLOAT16: // !!! FIXME: don't know this asm string
else
{
547
548
549
550
fail(ctx, "expected register type");
regtype = REG_TYPE_CONST;
regnum = 0;
neednum = 0;
551
552
} // else
553
554
// "c[5]" is the same as "c5", so if the token is done, see if next is '['.
if ((neednum) && (ctx->tokenlen == 0))
555
{
556
if (nexttoken(ctx) == ((Token) '['))
557
neednum = 0; // don't need a number on register name itself.
558
pushback(ctx);
559
560
} // if
561
562
if (neednum)
{
563
uint32 ui32 = 0;
564
if (!ui32fromtoken(ctx, &ui32))
565
fail(ctx, "Invalid register index");
566
regnum = (int) ui32;
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
} // 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
{
594
fail(ctx, "Invalid const register index");
595
596
597
598
599
600
} // else
} // if
*rtype = regtype;
*rnum = regnum;
601
return 1;
602
603
604
} // parse_register_name
605
static void set_result_shift(Context *ctx, DestArgInfo *info, const int val)
606
{
607
if (info->result_shift != 0)
608
fail(ctx, "Multiple result shift modifiers");
609
610
info->result_shift = val;
} // set_result_shift
611
612
613
static int parse_destination_token(Context *ctx)
614
{
615
DestArgInfo *info = &ctx->dest_arg;
616
memset(info, '\0', sizeof (DestArgInfo));
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// parse_instruction_token() sets ctx->token to the end of the instruction
// so we can see if there are destination modifiers on the instruction
// itself...
int invalid_modifier = 0;
while ((ctx->tokenlen > 0) && (!invalid_modifier))
{
if (check_token_segment(ctx, "_x2"))
set_result_shift(ctx, info, 0x1);
else if (check_token_segment(ctx, "_x4"))
set_result_shift(ctx, info, 0x2);
else if (check_token_segment(ctx, "_x8"))
set_result_shift(ctx, info, 0x3);
else if (check_token_segment(ctx, "_d8"))
set_result_shift(ctx, info, 0xD);
else if (check_token_segment(ctx, "_d4"))
set_result_shift(ctx, info, 0xE);
else if (check_token_segment(ctx, "_d2"))
set_result_shift(ctx, info, 0xF);
else if (check_token_segment(ctx, "_sat"))
639
info->result_mod |= MOD_SATURATE;
640
else if (check_token_segment(ctx, "_pp"))
641
info->result_mod |= MOD_PP;
642
else if (check_token_segment(ctx, "_centroid"))
643
644
info->result_mod |= MOD_CENTROID;
else
645
invalid_modifier = 1;
646
647
} // while
648
649
if (invalid_modifier)
fail(ctx, "Invalid destination modifier");
650
651
// !!! FIXME: predicates.
652
if (nexttoken(ctx) == ((Token) '('))
653
654
fail(ctx, "Predicates unsupported at this time"); // !!! FIXME: ...
655
656
pushback(ctx); // parse_register_name calls nexttoken().
657
parse_register_name(ctx, &info->regtype, &info->regnum);
658
659
660
// parse_register_name() can't check this: dest regs might have modifiers.
if (ctx->tokenlen > 0)
fail(ctx, "invalid register name");
661
662
663
// !!! FIXME: can dest registers do relative addressing?
664
int invalid_writemask = 0;
665
int implicit_writemask = 0;
666
if (nexttoken(ctx) != ((Token) '.'))
667
{
668
implicit_writemask = 1;
669
670
671
672
info->writemask = 0xF;
info->writemask0 = info->writemask1 = info->writemask2 = info->writemask3 = 1;
pushback(ctx); // no explicit writemask; do full mask.
} // if
673
674
675
676
// !!! FIXME: Cg generates code with oDepth.z ... this is a bug, I think.
//else if (scalar_register(ctx->shader_type, info->regtype, info->regnum))
else if ( (scalar_register(ctx->shader_type, info->regtype, info->regnum)) && (info->regtype != REG_TYPE_DEPTHOUT) )
677
fail(ctx, "Writemask specified for scalar register");
678
else if (nexttoken(ctx) != TOKEN_IDENTIFIER)
679
invalid_writemask = 1;
680
681
else
{
682
683
684
685
686
687
// !!! FIXME: is out-of-order okay (yxzw instead of xyzw?)
char tokenbytes[5] = { '\0', '\0', '\0', '\0', '\0' };
const unsigned int tokenlen = ctx->tokenlen;
memcpy(tokenbytes, ctx->token, ((tokenlen < 4) ? tokenlen : 4));
char *ptr = tokenbytes;
688
info->writemask0 = info->writemask1 = info->writemask2 = info->writemask3 = 0;
689
690
691
692
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++; }
693
if ((ptr == ctx->token) && (shader_is_pixel(ctx)))
694
{
695
696
697
698
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++; }
699
700
701
} // if
if (*ptr != '\0')
702
invalid_writemask = 1;
703
704
705
706
707
708
709
info->writemask = ( ((info->writemask0 & 0x1) << 0) |
((info->writemask1 & 0x1) << 1) |
((info->writemask2 & 0x1) << 2) |
((info->writemask3 & 0x1) << 3) );
} // else
710
711
712
if (invalid_writemask)
fail(ctx, "Invalid writemask");
713
714
715
716
717
// !!! FIXME: Cg generates code with oDepth.z ... this is a bug, I think.
if (info->regtype == REG_TYPE_DEPTHOUT)
{
if ( (!implicit_writemask) && ((info->writemask0 + info->writemask1 +
info->writemask2 + info->writemask3) > 1) )
718
fail(ctx, "Writemask specified for scalar register");
719
720
} // if
721
722
723
info->orig_writemask = info->writemask;
if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
724
725
726
727
{
fail(ctx, "Too many tokens");
return 1;
} // if
728
729
ctx->tokenbuf[ctx->tokenbufpos++] =
730
( ((((uint32) 1)) << 31) |
731
732
733
734
((((uint32) info->regnum) & 0x7ff) << 0) |
((((uint32) info->relative) & 0x1) << 13) |
((((uint32) info->result_mod) & 0xF) << 20) |
((((uint32) info->result_shift) & 0xF) << 24) |
735
((((uint32) info->writemask) & 0xF) << 16) |
736
737
738
739
740
741
742
((((uint32) info->regtype) & 0x7) << 28) |
((((uint32) info->regtype) & 0x18) << 8) );
return 1;
} // parse_destination_token
743
744
745
static void set_source_mod(Context *ctx, const int negate,
const SourceMod norm, const SourceMod negated,
SourceMod *srcmod)
746
{
747
748
749
750
751
if ( (*srcmod != SRCMOD_NONE) || (negate && (negated == SRCMOD_NONE)) )
fail(ctx, "Incompatible source modifiers");
else
*srcmod = ((negate) ? negated : norm);
} // set_source_mod
752
753
754
static int parse_source_token_maybe_relative(Context *ctx, const int relok)
755
{
756
757
758
int retval = 1;
if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
759
760
761
762
{
fail(ctx, "Too many tokens");
return 0;
} // if
763
764
// mark this now, so optional relative addressing token is placed second.
765
766
uint32 *outtoken = &ctx->tokenbuf[ctx->tokenbufpos++];
*outtoken = 0;
767
768
769
SourceMod srcmod = SRCMOD_NONE;
int negate = 0;
770
771
772
773
774
775
776
Token token = nexttoken(ctx);
if (token == ((Token) '!'))
srcmod = SRCMOD_NOT;
else if (token == ((Token) '-'))
negate = 1;
else if ( (token == TOKEN_INT_LITERAL) && (check_token(ctx, "1")) )
777
{
778
779
if (nexttoken(ctx) != ((Token) '-'))
fail(ctx, "Unexpected token");
780
781
782
783
else
srcmod = SRCMOD_COMPLEMENT;
} // else
else
784
{
785
pushback(ctx);
786
} // else
787
788
789
RegisterType regtype;
int regnum;
790
parse_register_name(ctx, ®type, ®num);
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
if (ctx->tokenlen > 0)
{
if (check_token_segment(ctx, "_bias"))
set_source_mod(ctx, negate, SRCMOD_BIAS, SRCMOD_BIASNEGATE, &srcmod);
else if (check_token_segment(ctx, "_bx2"))
set_source_mod(ctx, negate, SRCMOD_SIGN, SRCMOD_SIGNNEGATE, &srcmod);
else if (check_token_segment(ctx, "_x2"))
set_source_mod(ctx, negate, SRCMOD_X2, SRCMOD_X2NEGATE, &srcmod);
else if (check_token_segment(ctx, "_dz"))
set_source_mod(ctx, negate, SRCMOD_DZ, SRCMOD_NONE, &srcmod);
else if (check_token_segment(ctx, "_dw"))
set_source_mod(ctx, negate, SRCMOD_DW, SRCMOD_NONE, &srcmod);
else if (check_token_segment(ctx, "_abs"))
set_source_mod(ctx, negate, SRCMOD_ABS, SRCMOD_ABSNEGATE, &srcmod);
else
fail(ctx, "Invalid source modifier");
} // if
809
810
uint32 relative = 0;
811
if (nexttoken(ctx) != ((Token) '['))
812
813
814
pushback(ctx); // not relative addressing?
else
{
815
816
817
818
819
820
if (!relok)
fail(ctx, "Relative addressing not permitted here.");
else
retval++;
parse_source_token_maybe_relative(ctx, 0);
821
relative = 1;
822
823
if (nexttoken(ctx) != ((Token) '+'))
824
825
826
pushback(ctx);
else
{
827
828
// !!! FIXME: maybe c3[a0.x + 5] is legal and becomes c[a0.x + 8] ?
if (regnum != 0)
829
fail(ctx, "Relative addressing with explicit register number.");
830
831
uint32 ui32 = 0;
832
833
834
835
if ( (nexttoken(ctx) != TOKEN_INT_LITERAL) ||
(!ui32fromtoken(ctx, &ui32)) ||
(ctx->tokenlen != 0) )
{
836
fail(ctx, "Invalid relative addressing offset");
837
} // if
838
839
840
regnum += (int) ui32;
} // else
841
if (nexttoken(ctx) != ((Token) ']'))
842
fail(ctx, "Expected ']'");
843
} // else
844
845
int invalid_swizzle = 0;
846
uint32 swizzle = 0;
847
if (nexttoken(ctx) != ((Token) '.'))
848
849
850
851
{
swizzle = 0xE4; // 0xE4 == 11100100 ... 0 1 2 3. No swizzle.
pushback(ctx); // no explicit writemask; do full mask.
} // if
852
else if (scalar_register(ctx->shader_type, regtype, regnum))
853
fail(ctx, "Swizzle specified for scalar register");
854
else if (nexttoken(ctx) != TOKEN_IDENTIFIER)
855
invalid_swizzle = 1;
856
857
else
{
858
859
860
861
char tokenbytes[5] = { '\0', '\0', '\0', '\0', '\0' };
const unsigned int tokenlen = ctx->tokenlen;
memcpy(tokenbytes, ctx->token, ((tokenlen < 4) ? tokenlen : 4));
862
// deal with shortened form (.x = .xxxx, etc).
863
864
865
866
867
868
869
if (tokenlen == 1)
tokenbytes[1] = tokenbytes[2] = tokenbytes[3] = tokenbytes[0];
else if (tokenlen == 2)
tokenbytes[2] = tokenbytes[3] = tokenbytes[1];
else if (tokenlen == 3)
tokenbytes[3] = tokenbytes[2];
else if (tokenlen != 4)
870
invalid_swizzle = 1;
871
tokenbytes[4] = '\0';
872
873
uint32 val = 0;
874
875
int saw_xyzw = 0;
int saw_rgba = 0;
876
int i;
877
878
for (i = 0; i < 4; i++)
{
879
const int component = (int) tokenbytes[i];
880
881
882
883
884
885
886
887
888
889
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;
890
default: invalid_swizzle = 1; break;
891
892
893
} // switch
swizzle |= (val << (i * 2));
} // for
894
895
if (saw_xyzw && saw_rgba)
896
invalid_swizzle = 1;
897
898
else if (saw_rgba && !shader_is_pixel(ctx))
invalid_swizzle = 1;
899
} // else
900
901
902
903
if (invalid_swizzle)
fail(ctx, "Invalid swizzle");
904
905
906
907
908
909
910
*outtoken = ( ((((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) );
911
912
913
return retval;
} // parse_source_token_maybe_relative
914
915
916
static inline int parse_source_token(Context *ctx)
917
{
918
919
return parse_source_token_maybe_relative(ctx, 1);
} // parse_source_token
920
921
922
923
static int parse_args_NULL(Context *ctx)
{
924
return 1;
925
} // parse_args_NULL
926
927
928
static int parse_num(Context *ctx, const int floatok, uint32 *value)
929
930
{
union { float f; int32 si32; uint32 ui32; } cvt;
931
932
933
934
935
936
937
938
int negative = 0;
Token token = nexttoken(ctx);
if (token == ((Token) '-'))
{
negative = 1;
token = nexttoken(ctx);
} // if
939
940
if (token == TOKEN_INT_LITERAL)
941
{
942
943
int d = 0;
sscanf(ctx->token, "%d", &d);
944
945
946
947
if (floatok)
cvt.f = (float) ((negative) ? -d : d);
else
cvt.si32 = (int32) ((negative) ? -d : d);
948
949
} // if
else if (token == TOKEN_FLOAT_LITERAL)
950
{
951
if (!floatok)
952
{
953
954
955
fail(ctx, "Expected whole number");
*value = 0;
return 0;
956
} // if
957
sscanf(ctx->token, "%f", &cvt.f);
958
959
if (negative)
cvt.f = -cvt.f;
960
961
962
963
964
965
} // if
else
{
fail(ctx, "Expected number");
*value = 0;
return 0;
966
} // else
967
968
*value = cvt.ui32;
969
return 1;
970
971
972
973
974
} // parse_num
static int parse_args_DEFx(Context *ctx, const int isflt)
{
975
976
977
978
979
980
981
982
983
parse_destination_token(ctx);
require_comma(ctx);
parse_num(ctx, isflt, &ctx->tokenbuf[ctx->tokenbufpos++]);
require_comma(ctx);
parse_num(ctx, isflt, &ctx->tokenbuf[ctx->tokenbufpos++]);
require_comma(ctx);
parse_num(ctx, isflt, &ctx->tokenbuf[ctx->tokenbufpos++]);
require_comma(ctx);
parse_num(ctx, isflt, &ctx->tokenbuf[ctx->tokenbufpos++]);
984
985
return 6;
} // parse_args_DEFx
986
987
988
989
990
991
static int parse_args_DEF(Context *ctx)
{
return parse_args_DEFx(ctx, 1);
} // parse_args_DEF
992
993
994
995
996
997
static int parse_args_DEFI(Context *ctx)
{
return parse_args_DEFx(ctx, 0);
} // parse_args_DEFI
998
999
1000
static int parse_args_DEFB(Context *ctx)