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