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