Skip to content

Latest commit

 

History

History
1767 lines (1479 loc) · 51.2 KB

mojoshader_assembler.c

File metadata and controls

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