Skip to content

Latest commit

 

History

History
1928 lines (1634 loc) · 56.6 KB

mojoshader_assembler.c

File metadata and controls

1928 lines (1634 loc) · 56.6 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"
Dec 10, 2008
Dec 10, 2008
13
#define DEBUG_TOKENIZER 0
Dec 10, 2008
Dec 10, 2008
15
// !!! FIXME: no #define support yet.
Dec 13, 2008
Dec 13, 2008
17
typedef struct TokenizerContext
Dec 13, 2008
Dec 13, 2008
18
19
20
21
22
23
24
{
const char *source;
int on_endline;
unsigned int linenum;
char prevchar;
char token[64];
char pushedback;
Dec 13, 2008
Dec 13, 2008
25
} TokenizerContext;
Dec 13, 2008
Dec 13, 2008
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;
Dec 13, 2008
Dec 13, 2008
37
TokenizerContext tctx;
Dec 19, 2008
Dec 19, 2008
38
MOJOSHADER_parsePhase parse_phase;
39
40
41
MOJOSHADER_shaderType shader_type;
uint8 major_ver;
uint8 minor_ver;
Dec 20, 2008
Dec 20, 2008
42
uint32 version_token;
43
44
45
uint32 tokenbuf[16];
int tokenbufpos;
DestArgInfo dest_arg;
Dec 8, 2008
Dec 8, 2008
46
uint32 *output;
Dec 10, 2008
Dec 10, 2008
47
uint32 *token_to_line;
Dec 20, 2008
Dec 20, 2008
48
49
50
uint8 *ctab;
uint32 ctab_len;
uint32 ctab_allocation;
Dec 8, 2008
Dec 8, 2008
51
52
size_t output_len;
size_t output_allocation;
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
110
111
112
113
};
// 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
Dec 13, 2008
Dec 13, 2008
114
115
116
117
118
119
static inline int tokeq(const TokenizerContext *tctx, const char *token)
{
return (strcasecmp(tctx->token, token) == 0);
} // tokeq
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// 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
Dec 10, 2008
Dec 10, 2008
144
145
146
147
148
149
150
151
152
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
153
154
155
156
157
158
static void output_token_noswap(Context *ctx, const uint32 token)
{
if (isfail(ctx))
return;
Dec 8, 2008
Dec 8, 2008
159
160
161
162
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;
Dec 10, 2008
Dec 10, 2008
163
164
165
void *ptr;
ptr = Malloc(ctx, newsize * sizeof (uint32));
Dec 8, 2008
Dec 8, 2008
166
167
168
169
170
171
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;
Dec 10, 2008
Dec 10, 2008
172
173
174
175
176
177
178
179
180
181
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;
Dec 8, 2008
Dec 8, 2008
182
183
} // if
Dec 10, 2008
Dec 10, 2008
184
ctx->output[ctx->output_len] = token;
Dec 13, 2008
Dec 13, 2008
185
ctx->token_to_line[ctx->output_len] = ctx->tctx.linenum;
Dec 10, 2008
Dec 10, 2008
186
ctx->output_len++;
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
224
225
226
227
} // 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
Dec 13, 2008
Dec 13, 2008
228
static int tokenize_ctx(Context *ctx, TokenizerContext *tctx)
229
230
231
232
233
234
{
int idx = 0;
if (isfail(ctx))
return FAIL;
Dec 13, 2008
Dec 13, 2008
235
if (tctx->pushedback)
Dec 13, 2008
Dec 13, 2008
237
tctx->pushedback = 0;
238
239
240
return NOFAIL;
} // if
Dec 13, 2008
Dec 13, 2008
241
if (tctx->on_endline)
Dec 8, 2008
Dec 8, 2008
242
{
Dec 13, 2008
Dec 13, 2008
243
244
tctx->on_endline = 0;
tctx->linenum++; // passed a newline, update.
Dec 8, 2008
Dec 8, 2008
245
246
} // if
247
248
while (1)
{
Dec 10, 2008
Dec 10, 2008
249
// !!! FIXME: carefully crafted (but legal) comments can trigger this.
Dec 13, 2008
Dec 13, 2008
250
if (idx >= sizeof (tctx->token))
251
252
return fail(ctx, "buffer overflow");
Dec 13, 2008
Dec 13, 2008
253
char ch = *tctx->source;
254
255
256
257
if (ch == '\t')
ch = ' '; // collapse tabs into single spaces.
else if (ch == '\r')
{
Dec 13, 2008
Dec 13, 2008
258
if (tctx->source[1] == '\n')
259
260
261
262
continue; // ignore '\r' if this is "\r\n" ...
ch = '\n';
} // else if
Dec 10, 2008
Dec 10, 2008
263
if ((ch >= '0') && (ch <= '9'))
264
265
{
// starting a number, but rest of current token was not number.
Dec 13, 2008
Dec 13, 2008
266
if ((idx > 0) && ((tctx->prevchar < '0') || (tctx->prevchar > '9')))
Dec 13, 2008
Dec 13, 2008
268
tctx->token[idx++] = '\0';
269
270
271
272
273
274
return NOFAIL;
} // if
} // if
else
{
// starting a non-number, but rest of current token was numbers.
Dec 13, 2008
Dec 13, 2008
275
if ((idx > 0) && ((tctx->prevchar >= '0') && (tctx->prevchar <= '9')))
Dec 13, 2008
Dec 13, 2008
277
tctx->token[idx++] = '\0';
278
279
280
281
282
283
284
285
286
return NOFAIL;
} // if
} // else
switch (ch)
{
case '/':
case ';': // !!! FIXME: comment, right?
if (idx != 0) // finish off existing token.
Dec 13, 2008
Dec 13, 2008
287
tctx->token[idx] = '\0';
288
289
else
{
Dec 13, 2008
Dec 13, 2008
290
291
292
tctx->token[idx++] = ch;
tctx->source++;
if ((ch == '/') && (*tctx->source == '/'))
Dec 13, 2008
Dec 13, 2008
294
295
tctx->token[idx++] = '/';
tctx->source++;
Dec 13, 2008
Dec 13, 2008
297
tctx->token[idx++] = '\0';
298
299
300
301
} // else
return NOFAIL;
case ' ':
Dec 13, 2008
Dec 13, 2008
302
if (tctx->prevchar == ' ')
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
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.
Dec 13, 2008
Dec 13, 2008
318
tctx->token[idx] = '\0';
319
320
321
else // this is a token in itself.
{
if (ch == '\n')
Dec 13, 2008
Dec 13, 2008
322
323
324
325
tctx->on_endline = 1;
tctx->source++;
tctx->token[idx++] = ch;
tctx->token[idx++] = '\0';
326
327
328
329
} // else
return NOFAIL;
case '\0':
Dec 13, 2008
Dec 13, 2008
330
tctx->token[idx] = '\0';
331
332
333
334
335
if (idx != 0) // had any chars? It's a token.
return NOFAIL;
return END_OF_STREAM;
default:
Dec 13, 2008
Dec 13, 2008
336
337
tctx->source++;
tctx->token[idx++] = ch;
338
339
340
break;
} // switch
Dec 13, 2008
Dec 13, 2008
341
tctx->prevchar = ch;
342
343
344
} // while
return fail(ctx, "???"); // shouldn't hit this.
Dec 13, 2008
Dec 13, 2008
345
} // tokenize_ctx
346
347
348
349
static inline int tokenize(Context *ctx)
{
Dec 13, 2008
Dec 13, 2008
350
351
const int rc = tokenize_ctx(ctx, &ctx->tctx);
352
353
354
355
356
#if DEBUG_TOKENIZER
printf("TOKENIZE: %s '%s'\n",
(rc == END_OF_STREAM) ? "END_OF_STREAM" :
(rc == FAIL) ? "FAIL" :
(rc == NOFAIL) ? "NOFAIL" : "???",
Dec 13, 2008
Dec 13, 2008
357
(ctx->tctx.token[0] == '\n') ? "\\n" : ctx->tctx.token);
Dec 13, 2008
Dec 13, 2008
359
360
361
362
363
return rc;
} // tokenize
Dec 13, 2008
Dec 13, 2008
364
static int pushback_ctx(Context *ctx, TokenizerContext *tctx)
Dec 13, 2008
Dec 13, 2008
366
if (tctx->pushedback)
Dec 10, 2008
Dec 10, 2008
367
return fail(ctx, "BUG: Double pushback in parser");
Dec 13, 2008
Dec 13, 2008
369
tctx->pushedback = 1;
Dec 10, 2008
Dec 10, 2008
370
371
return NOFAIL;
Dec 13, 2008
Dec 13, 2008
372
373
374
375
376
377
378
379
380
381
382
383
384
385
}
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;
386
387
388
} // pushback
Dec 13, 2008
Dec 13, 2008
389
390
391
static int nexttoken_ctx(Context *ctx, TokenizerContext *tctx,
const int ignoreeol, const int ignorewhitespace,
const int eolok, const int eosok)
392
393
394
{
int rc = NOFAIL;
Dec 13, 2008
Dec 13, 2008
395
while ((rc = tokenize_ctx(ctx, tctx)) == NOFAIL)
Dec 13, 2008
Dec 13, 2008
397
if (tokeq(tctx, "\n"))
398
399
400
401
402
403
404
{
if (ignoreeol)
continue;
else if (!eolok)
return fail(ctx, "Unexpected EOL");
} // if
Dec 13, 2008
Dec 13, 2008
405
else if (tokeq(tctx, " "))
406
407
408
409
410
411
{
if (ignorewhitespace)
continue;
} // else if
// skip comments...
Dec 13, 2008
Dec 13, 2008
412
else if (tokeq(tctx, "//") || tokeq(tctx, ";"))
Dec 13, 2008
Dec 13, 2008
414
while ((rc = tokenize_ctx(ctx, tctx)) == NOFAIL)
Dec 13, 2008
Dec 13, 2008
416
if (tokeq(tctx, "\n"))
Dec 10, 2008
Dec 10, 2008
417
{
Dec 13, 2008
Dec 13, 2008
418
pushback_ctx(ctx, tctx);
Dec 10, 2008
Dec 10, 2008
420
} // if
421
} // while
Dec 10, 2008
Dec 10, 2008
422
continue; // pick up from newline, go again.
423
424
425
426
427
} // if
break;
} // while
Dec 13, 2008
Dec 13, 2008
428
429
430
431
432
433
434
435
436
437
438
439
440
441
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);
442
443
444
445
446
#if DEBUG_TOKENIZER
printf("NEXTTOKEN: %s '%s'\n",
(rc == END_OF_STREAM) ? "END_OF_STREAM" :
(rc == FAIL) ? "FAIL" :
(rc == NOFAIL) ? "NOFAIL" : "???",
Dec 13, 2008
Dec 13, 2008
447
(ctx->tctx.token[0] == '\n') ? "\\n" : ctx->tctx.token);
448
449
450
451
452
453
454
455
#endif
return rc;
} // nexttoken
static int require_endline(Context *ctx)
{
Dec 13, 2008
Dec 13, 2008
456
TokenizerContext *tctx = &ctx->tctx;
457
458
459
460
461
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.
Dec 13, 2008
Dec 13, 2008
462
else if (!tokeq(tctx, "\n"))
463
464
465
466
467
return fail(ctx, "Endline expected");
return NOFAIL;
} // require_endline
Dec 8, 2008
Dec 8, 2008
468
static int require_comma(Context *ctx)
Dec 13, 2008
Dec 13, 2008
470
TokenizerContext *tctx = &ctx->tctx;
Dec 8, 2008
Dec 8, 2008
471
472
473
const int rc = nexttoken(ctx, 0, 1, 0, 0);
if (rc == FAIL)
return FAIL;
Dec 13, 2008
Dec 13, 2008
474
else if (!tokeq(tctx, ","))
Dec 8, 2008
Dec 8, 2008
475
return fail(ctx, "Comma expected");
476
return NOFAIL;
Dec 8, 2008
Dec 8, 2008
477
} // require_comma
478
479
480
481
static int parse_register_name(Context *ctx, RegisterType *rtype, int *rnum)
{
Dec 13, 2008
Dec 13, 2008
482
TokenizerContext *tctx = &ctx->tctx;
483
484
485
486
487
488
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
int neednum = 1;
int regnum = 0;
RegisterType regtype = REG_TYPE_TEMP;
Dec 13, 2008
Dec 13, 2008
489
if (tokeq(tctx, "r"))
490
regtype = REG_TYPE_TEMP;
Dec 13, 2008
Dec 13, 2008
491
else if (tokeq(tctx, "v"))
492
regtype = REG_TYPE_INPUT;
Dec 13, 2008
Dec 13, 2008
493
else if (tokeq(tctx, "c"))
494
regtype = REG_TYPE_CONST;
Dec 13, 2008
Dec 13, 2008
495
else if (tokeq(tctx, "i"))
496
regtype = REG_TYPE_CONSTINT;
Dec 13, 2008
Dec 13, 2008
497
else if (tokeq(tctx, "b"))
498
regtype = REG_TYPE_CONSTBOOL;
Dec 13, 2008
Dec 13, 2008
499
else if (tokeq(tctx, "oC"))
500
regtype = REG_TYPE_COLOROUT;
Dec 13, 2008
Dec 13, 2008
501
else if (tokeq(tctx, "s"))
502
regtype = REG_TYPE_SAMPLER;
Dec 13, 2008
Dec 13, 2008
503
else if (tokeq(tctx, "oD"))
504
regtype = REG_TYPE_ATTROUT;
Dec 13, 2008
Dec 13, 2008
505
else if (tokeq(tctx, "l"))
506
regtype = REG_TYPE_LABEL;
Dec 13, 2008
Dec 13, 2008
507
else if (tokeq(tctx, "p"))
508
regtype = REG_TYPE_PREDICATE;
Dec 13, 2008
Dec 13, 2008
509
else if (tokeq(tctx, "oDepth"))
Dec 13, 2008
Dec 13, 2008
510
511
512
513
{
regtype = REG_TYPE_DEPTHOUT;
neednum = 0;
} // else if
Dec 13, 2008
Dec 13, 2008
514
else if (tokeq(tctx, "aL"))
515
516
517
518
{
regtype = REG_TYPE_LOOP;
neednum = 0;
} // else if
Dec 13, 2008
Dec 13, 2008
519
else if (tokeq(tctx, "o"))
520
521
522
523
524
{
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
Dec 13, 2008
Dec 13, 2008
525
else if (tokeq(tctx, "oT"))
Dec 12, 2008
Dec 12, 2008
527
if (shader_is_vertex(ctx) && shader_version_atleast(ctx, 3, 0))
528
529
530
return fail(ctx, "Output register not valid in this shader type");
regtype = REG_TYPE_OUTPUT;
} // else if
Dec 13, 2008
Dec 13, 2008
531
else if (tokeq(tctx, "a"))
532
533
534
535
536
{
if (!shader_is_vertex(ctx))
return fail(ctx, "Address register only valid in vertex shaders.");
regtype = REG_TYPE_ADDRESS;
} // else if
Dec 13, 2008
Dec 13, 2008
537
else if (tokeq(tctx, "t"))
538
539
540
541
542
{
if (!shader_is_pixel(ctx))
return fail(ctx, "Address register only valid in pixel shaders.");
regtype = REG_TYPE_ADDRESS;
} // else if
Dec 13, 2008
Dec 13, 2008
543
else if (tokeq(tctx, "vPos"))
544
545
546
547
548
{
regtype = REG_TYPE_MISCTYPE;
regnum = (int) MISCTYPE_TYPE_POSITION;
neednum = 0;
} // else if
Dec 13, 2008
Dec 13, 2008
549
else if (tokeq(tctx, "vFace"))
550
551
552
553
554
{
regtype = REG_TYPE_MISCTYPE;
regnum = (int) MISCTYPE_TYPE_FACE;
neednum = 0;
} // else if
Dec 13, 2008
Dec 13, 2008
555
else if (tokeq(tctx, "oPos"))
556
557
558
559
560
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_POSITION;
neednum = 0;
} // else if
Dec 13, 2008
Dec 13, 2008
561
else if (tokeq(tctx, "oFog"))
562
563
564
565
566
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_FOG;
neednum = 0;
} // else if
Dec 13, 2008
Dec 13, 2008
567
else if (tokeq(tctx, "oPts"))
568
569
570
571
572
573
574
575
576
577
578
579
580
{
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
Dec 10, 2008
Dec 10, 2008
581
582
if (neednum)
{
Dec 13, 2008
Dec 13, 2008
583
584
585
586
587
588
// 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)
Dec 10, 2008
Dec 10, 2008
589
return FAIL;
Dec 14, 2008
Dec 14, 2008
590
else if (tokeq(&tmptctx, "["))
Dec 10, 2008
Dec 10, 2008
591
592
593
neednum = 0;
} // if
594
595
596
597
598
if (neednum)
{
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
Dec 10, 2008
Dec 10, 2008
599
uint32 ui32 = 0;
Dec 13, 2008
Dec 13, 2008
600
if (!ui32fromstr(tctx->token, &ui32))
Dec 10, 2008
Dec 10, 2008
601
602
return fail(ctx, "Invalid register index");
regnum = (int) ui32;
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
637
638
639
640
} // 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
Dec 8, 2008
Dec 8, 2008
641
static int set_result_shift(Context *ctx, DestArgInfo *info, const int val)
Dec 8, 2008
Dec 8, 2008
643
644
645
646
647
if (info->result_shift != 0)
return fail(ctx, "Multiple result shift modifiers");
info->result_shift = val;
return NOFAIL;
} // set_result_shift
Dec 8, 2008
Dec 8, 2008
649
Dec 13, 2008
Dec 13, 2008
650
static int parse_destination_token(Context *ctx)
Dec 8, 2008
Dec 8, 2008
651
{
Dec 13, 2008
Dec 13, 2008
652
653
654
TokenizerContext *tctx = &ctx->tctx;
DestArgInfo *info = &ctx->dest_arg;
Dec 12, 2008
Dec 12, 2008
655
memset(info, '\0', sizeof (DestArgInfo));
656
657
658
659
660
661
// See if there are destination modifiers on the instruction itself...
while (1)
{
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
Dec 13, 2008
Dec 13, 2008
662
else if (tokeq(tctx, " "))
663
break; // done with modifiers.
Dec 13, 2008
Dec 13, 2008
664
else if (!tokeq(tctx, "_"))
665
666
667
return fail(ctx, "Expected modifier or whitespace");
else if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
Dec 12, 2008
Dec 12, 2008
668
// !!! FIXME: this can be cleaned up when tokenizer is fixed.
Dec 13, 2008
Dec 13, 2008
669
else if (tokeq(tctx, "x"))
Dec 12, 2008
Dec 12, 2008
670
671
672
{
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
Dec 13, 2008
Dec 13, 2008
673
else if (tokeq(tctx, "2"))
Dec 12, 2008
Dec 12, 2008
674
set_result_shift(ctx, info, 0x1);
Dec 13, 2008
Dec 13, 2008
675
else if (tokeq(tctx, "4"))
Dec 12, 2008
Dec 12, 2008
676
set_result_shift(ctx, info, 0x2);
Dec 13, 2008
Dec 13, 2008
677
else if (tokeq(tctx, "8"))
Dec 12, 2008
Dec 12, 2008
678
679
680
681
682
set_result_shift(ctx, info, 0x3);
else
return fail(ctx, "Expected modifier");
} // else if
// !!! FIXME: this can be cleaned up when tokenizer is fixed.
Dec 13, 2008
Dec 13, 2008
683
else if (tokeq(tctx, "d"))
Dec 12, 2008
Dec 12, 2008
684
685
686
{
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
Dec 13, 2008
Dec 13, 2008
687
else if (tokeq(tctx, "8"))
Dec 12, 2008
Dec 12, 2008
688
set_result_shift(ctx, info, 0xD);
Dec 13, 2008
Dec 13, 2008
689
else if (tokeq(tctx, "4"))
Dec 12, 2008
Dec 12, 2008
690
set_result_shift(ctx, info, 0xE);
Dec 13, 2008
Dec 13, 2008
691
else if (tokeq(tctx, "2"))
Dec 12, 2008
Dec 12, 2008
692
693
694
695
set_result_shift(ctx, info, 0xF);
else
return fail(ctx, "Expected modifier");
} // else if
Dec 13, 2008
Dec 13, 2008
696
else if (tokeq(tctx, "sat"))
697
info->result_mod |= MOD_SATURATE;
Dec 13, 2008
Dec 13, 2008
698
else if (tokeq(tctx, "pp"))
699
info->result_mod |= MOD_PP;
Dec 13, 2008
Dec 13, 2008
700
else if (tokeq(tctx, "centroid"))
701
702
703
704
705
info->result_mod |= MOD_CENTROID;
else
return fail(ctx, "Expected modifier");
} // while
Dec 8, 2008
Dec 8, 2008
706
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
707
708
709
return FAIL;
// !!! FIXME: predicates.
Dec 13, 2008
Dec 13, 2008
710
if (tokeq(tctx, "("))
711
712
713
714
715
716
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;
Dec 8, 2008
Dec 8, 2008
717
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
718
719
720
721
return FAIL;
// !!! FIXME: can dest registers do relative addressing?
Dec 14, 2008
Dec 14, 2008
722
int implicit_writemask = 0;
Dec 13, 2008
Dec 13, 2008
723
if (!tokeq(tctx, "."))
Dec 14, 2008
Dec 14, 2008
725
implicit_writemask = 1;
726
727
728
729
info->writemask = 0xF;
info->writemask0 = info->writemask1 = info->writemask2 = info->writemask3 = 1;
pushback(ctx); // no explicit writemask; do full mask.
} // if
Dec 14, 2008
Dec 14, 2008
730
731
732
// !!! 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) )
733
return fail(ctx, "Writemask specified for scalar register");
Dec 8, 2008
Dec 8, 2008
734
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
735
return FAIL;
Dec 13, 2008
Dec 13, 2008
736
else if (tokeq(tctx, ""))
737
738
739
return fail(ctx, "Invalid writemask");
else
{
Dec 13, 2008
Dec 13, 2008
740
char *ptr = tctx->token;
741
info->writemask0 = info->writemask1 = info->writemask2 = info->writemask3 = 0;
Dec 8, 2008
Dec 8, 2008
742
743
744
745
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++; }
Dec 13, 2008
Dec 13, 2008
746
if ((ptr == tctx->token) && (shader_is_pixel(ctx)))
Dec 8, 2008
Dec 8, 2008
748
749
750
751
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++; }
752
753
754
755
756
757
758
759
760
761
762
} // 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
Dec 14, 2008
Dec 14, 2008
763
764
765
766
767
768
769
770
// !!! 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) )
return fail(ctx, "Writemask specified for scalar register");
} // if
771
772
773
774
775
776
info->orig_writemask = info->writemask;
if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
return fail(ctx, "Too many tokens");
ctx->tokenbuf[ctx->tokenbufpos++] =
Dec 8, 2008
Dec 8, 2008
777
( ((((uint32) 1)) << 31) |
778
779
780
781
((((uint32) info->regnum) & 0x7ff) << 0) |
((((uint32) info->relative) & 0x1) << 13) |
((((uint32) info->result_mod) & 0xF) << 20) |
((((uint32) info->result_shift) & 0xF) << 24) |
Dec 11, 2008
Dec 11, 2008
782
((((uint32) info->writemask) & 0xF) << 16) |
783
784
785
786
787
788
789
((((uint32) info->regtype) & 0x7) << 28) |
((((uint32) info->regtype) & 0x18) << 8) );
return 1;
} // parse_destination_token
Dec 10, 2008
Dec 10, 2008
790
791
792
static void set_source_mod(Context *ctx, const int negate,
const SourceMod norm, const SourceMod negated,
SourceMod *srcmod)
Dec 8, 2008
Dec 8, 2008
794
795
796
797
798
if ( (*srcmod != SRCMOD_NONE) || (negate && (negated == SRCMOD_NONE)) )
fail(ctx, "Incompatible source modifiers");
else
*srcmod = ((negate) ? negated : norm);
} // set_source_mod
Dec 8, 2008
Dec 8, 2008
801
static int parse_source_token_maybe_relative(Context *ctx, const int relok)
Dec 13, 2008
Dec 13, 2008
803
TokenizerContext *tctx = &ctx->tctx;
Dec 8, 2008
Dec 8, 2008
804
805
806
807
808
809
810
811
812
813
814
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)
815
return FAIL;
Dec 13, 2008
Dec 13, 2008
816
else if (tokeq(tctx, "1"))
Dec 8, 2008
Dec 8, 2008
817
818
819
{
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
Dec 13, 2008
Dec 13, 2008
820
else if (!tokeq(tctx, "-"))
Dec 8, 2008
Dec 8, 2008
821
822
823
824
return fail(ctx, "Unexpected value");
else
srcmod = SRCMOD_COMPLEMENT;
} // else
Dec 13, 2008
Dec 13, 2008
825
else if (tokeq(tctx, "!"))
Dec 8, 2008
Dec 8, 2008
826
srcmod = SRCMOD_NOT;
Dec 13, 2008
Dec 13, 2008
827
else if (tokeq(tctx, "-"))
Dec 8, 2008
Dec 8, 2008
828
829
830
negate = 1;
else
pushback(ctx);
Dec 8, 2008
Dec 8, 2008
832
833
834
835
836
837
RegisterType regtype;
int regnum;
if (parse_register_name(ctx, &regtype, &regnum) == FAIL)
return FAIL;
else if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
return FAIL;
Dec 13, 2008
Dec 13, 2008
838
else if (!tokeq(tctx, "_"))
Dec 8, 2008
Dec 8, 2008
839
840
841
pushback(ctx);
else if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
Dec 13, 2008
Dec 13, 2008
842
else if (tokeq(tctx, "bias"))
Dec 10, 2008
Dec 10, 2008
843
set_source_mod(ctx, negate, SRCMOD_BIAS, SRCMOD_BIASNEGATE, &srcmod);
Dec 13, 2008
Dec 13, 2008
844
else if (tokeq(tctx, "bx2"))
Dec 10, 2008
Dec 10, 2008
845
set_source_mod(ctx, negate, SRCMOD_SIGN, SRCMOD_SIGNNEGATE, &srcmod);
Dec 13, 2008
Dec 13, 2008
846
else if (tokeq(tctx, "x2"))
Dec 10, 2008
Dec 10, 2008
847
set_source_mod(ctx, negate, SRCMOD_X2, SRCMOD_X2NEGATE, &srcmod);
Dec 13, 2008
Dec 13, 2008
848
else if (tokeq(tctx, "dz"))
Dec 10, 2008
Dec 10, 2008
849
set_source_mod(ctx, negate, SRCMOD_DZ, SRCMOD_NONE, &srcmod);
Dec 13, 2008
Dec 13, 2008
850
else if (tokeq(tctx, "dw"))
Dec 10, 2008
Dec 10, 2008
851
set_source_mod(ctx, negate, SRCMOD_DW, SRCMOD_NONE, &srcmod);
Dec 13, 2008
Dec 13, 2008
852
else if (tokeq(tctx, "abs"))
Dec 10, 2008
Dec 10, 2008
853
set_source_mod(ctx, negate, SRCMOD_ABS, SRCMOD_ABSNEGATE, &srcmod);
Dec 8, 2008
Dec 8, 2008
854
855
else
return fail(ctx, "Invalid source modifier");
Dec 8, 2008
Dec 8, 2008
857
858
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
return FAIL;
Dec 8, 2008
Dec 8, 2008
860
uint32 relative = 0;
Dec 13, 2008
Dec 13, 2008
861
if (!tokeq(tctx, "["))
Dec 8, 2008
Dec 8, 2008
862
863
864
865
866
867
868
869
870
871
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;
Dec 10, 2008
Dec 10, 2008
872
873
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
Dec 13, 2008
Dec 13, 2008
874
else if (!tokeq(tctx, "+"))
Dec 10, 2008
Dec 10, 2008
875
876
877
878
879
880
881
882
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;
Dec 13, 2008
Dec 13, 2008
883
if (!ui32fromstr(tctx->token, &ui32))
Dec 10, 2008
Dec 10, 2008
884
885
886
887
return fail(ctx, "Invalid relative addressing offset");
regnum += (int) ui32;
} // else
Dec 8, 2008
Dec 8, 2008
888
889
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
Dec 13, 2008
Dec 13, 2008
890
else if (!tokeq(tctx, "]"))
Dec 8, 2008
Dec 8, 2008
891
892
return fail(ctx, "Expected ']'");
} // else
Dec 8, 2008
Dec 8, 2008
894
895
896
897
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
return FAIL;
uint32 swizzle = 0;
Dec 13, 2008
Dec 13, 2008
898
if (!tokeq(tctx, "."))
Dec 8, 2008
Dec 8, 2008
899
900
901
902
{
swizzle = 0xE4; // 0xE4 == 11100100 ... 0 1 2 3. No swizzle.
pushback(ctx); // no explicit writemask; do full mask.
} // if
Dec 10, 2008
Dec 10, 2008
903
else if (scalar_register(ctx->shader_type, regtype, regnum))
Dec 8, 2008
Dec 8, 2008
904
905
return fail(ctx, "Swizzle specified for scalar register");
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
906
return FAIL;
Dec 13, 2008
Dec 13, 2008
907
else if (tokeq(tctx, ""))
Dec 8, 2008
Dec 8, 2008
908
909
910
911
return fail(ctx, "Invalid swizzle");
else
{
// deal with shortened form (.x = .xxxx, etc).
Dec 13, 2008
Dec 13, 2008
912
913
914
915
916
917
918
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')
Dec 8, 2008
Dec 8, 2008
919
return fail(ctx, "Invalid swizzle");
Dec 13, 2008
Dec 13, 2008
920
tctx->token[4] = '\0';
Dec 8, 2008
Dec 8, 2008
921
922
923
924
uint32 val;
int saw_xyzw = 0;
int saw_rgba = 0;
Dec 10, 2008
Dec 10, 2008
925
int i;
Dec 8, 2008
Dec 8, 2008
926
927
for (i = 0; i < 4; i++)
{
Dec 13, 2008
Dec 13, 2008
928
const int component = (int) tctx->token[i];
Dec 8, 2008
Dec 8, 2008
929
930
931
932
933
934
935
936
937
938
939
940
941
942
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
Dec 8, 2008
Dec 8, 2008
944
945
946
if (saw_xyzw && saw_rgba)
return fail(ctx, "Invalid swizzle");
} // else
Dec 8, 2008
Dec 8, 2008
948
949
950
951
952
953
954
*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) );
Dec 8, 2008
Dec 8, 2008
956
957
return retval;
} // parse_source_token_maybe_relative
Dec 8, 2008
Dec 8, 2008
960
static inline int parse_source_token(Context *ctx)
Dec 8, 2008
Dec 8, 2008
962
963
return parse_source_token_maybe_relative(ctx, 1);
} // parse_source_token
Dec 8, 2008
Dec 8, 2008
965
966
967
968
969
static int parse_args_NULL(Context *ctx)
{
return (isfail(ctx) ? FAIL : 1);
} // parse_args_NULL
Dec 8, 2008
Dec 8, 2008
972
973
static int parse_num(Context *ctx, const int floatok, uint32 *token)
{
Dec 13, 2008
Dec 13, 2008
974
TokenizerContext *tctx = &ctx->tctx;
Dec 8, 2008
Dec 8, 2008
975
976
977
int32 negative = 1;
union { float f; int32 si32; uint32 ui32; } cvt;
cvt.si32 = 0;
Dec 8, 2008
Dec 8, 2008
979
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
980
return FAIL;
Dec 13, 2008
Dec 13, 2008
981
else if (tokeq(tctx, "-"))
Dec 8, 2008
Dec 8, 2008
982
983
negative = -1;
else
984
pushback(ctx);
Dec 8, 2008
Dec 8, 2008
985
986
987
988
uint32 val = 0;
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
Dec 13, 2008
Dec 13, 2008
989
else if (!ui32fromstr(tctx->token, &val))
Dec 8, 2008
Dec 8, 2008
990
991
992
return fail(ctx, "Expected number");
uint32 fraction = 0;
Dec 10, 2008
Dec 10, 2008
993
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
Dec 8, 2008
Dec 8, 2008
994
return FAIL;
Dec 13, 2008
Dec 13, 2008
995
else if (!tokeq(tctx, "."))
Dec 8, 2008
Dec 8, 2008
996
997
998
999
pushback(ctx); // whole number
else if (!floatok)
return fail(ctx, "Expected whole number");
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
1000
return FAIL;