Skip to content

Latest commit

 

History

History
806 lines (722 loc) · 29.3 KB

mojoshader_compiler.c

File metadata and controls

806 lines (722 loc) · 29.3 KB
 
Feb 28, 2009
Feb 28, 2009
1
2
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"
Aug 23, 2009
Aug 23, 2009
4
5
6
7
#if DEBUG_COMPILER_PARSER
#define LEMON_SUPPORT_TRACING 1
#endif
Feb 9, 2010
Feb 9, 2010
8
typedef union TokenData
Aug 23, 2009
Aug 23, 2009
9
{
Feb 9, 2010
Feb 9, 2010
10
11
12
int64 i64;
double dbl;
const char *string;
Aug 23, 2009
Aug 23, 2009
13
14
} TokenData;
Feb 9, 2010
Feb 9, 2010
15
16
17
18
19
20
typedef struct StringBucket
{
char *string;
struct StringBucket *next;
} StringBucket;
Feb 28, 2009
Feb 28, 2009
21
typedef struct Context
Feb 9, 2010
Feb 9, 2010
23
24
25
26
27
28
29
int isfail;
int out_of_memory;
MOJOSHADER_malloc malloc;
MOJOSHADER_free free;
void *malloc_data;
int error_count;
ErrorList *errors;
30
Preprocessor *preprocessor;
Feb 9, 2010
Feb 9, 2010
31
32
33
StringBucket *string_hashtable[256];
const char *usertypes[512]; // !!! FIXME: dynamic allocation
int usertype_count;
Feb 28, 2009
Feb 28, 2009
34
35
} Context;
Feb 28, 2009
Feb 28, 2009
36
Feb 9, 2010
Feb 9, 2010
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
180
181
182
183
184
185
186
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Convenience functions for allocators...
static inline void out_of_memory(Context *ctx)
{
ctx->isfail = ctx->out_of_memory = 1;
} // 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 char *StrDup(Context *ctx, const char *str)
{
char *retval = (char *) Malloc(ctx, strlen(str) + 1);
if (retval != NULL)
strcpy(retval, str);
return retval;
} // StrDup
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
typedef enum Operator
{
OP_START_RANGE_UNARY,
OP_POSTINCREMENT,
OP_POSTDECREMENT,
OP_PREINCREMENT,
OP_PREDECREMENT,
OP_NEGATE,
OP_COMPLEMENT,
OP_NOT,
OP_END_RANGE_UNARY,
OP_START_RANGE_BINARY,
OP_DEREF_ARRAY,
OP_CALLFUNC,
OP_DEREF_STRUCT,
OP_COMMA,
OP_MULTIPLY,
OP_DIVIDE,
OP_MODULO,
OP_ADD,
OP_SUBTRACT,
OP_LSHIFT,
OP_RSHIFT,
OP_LESSTHAN,
OP_GREATERTHAN,
OP_LESSTHANOREQUAL,
OP_GREATERTHANOREQUAL,
OP_EQUAL,
OP_NOTEQUAL,
OP_BINARYAND,
OP_BINARYXOR,
OP_BINARYOR,
OP_LOGICALAND,
OP_LOGICALOR,
OP_ASSIGN,
OP_MULASSIGN,
OP_DIVASSIGN,
OP_MODASSIGN,
OP_ADDASSIGN,
OP_SUBASSIGN,
OP_LSHIFTASSIGN,
OP_RSHIFTASSIGN,
OP_ANDASSIGN,
OP_XORASSIGN,
OP_ORASSIGN,
OP_END_RANGE_BINARY,
OP_START_RANGE_TERNARY,
OP_CONDITIONAL,
OP_END_RANGE_TERNARY,
OP_START_RANGE_DATA,
OP_IDENTIFIER,
OP_INT_LITERAL,
OP_FLOAT_LITERAL,
OP_STRING_LITERAL,
OP_END_RANGE_DATA,
} Operator;
static inline int operator_is_unary(const Operator op)
{
return ((op > OP_START_RANGE_UNARY) && (op < OP_END_RANGE_UNARY));
} // operator_is_unary
static inline int operator_is_binary(const Operator op)
{
return ((op > OP_START_RANGE_BINARY) && (op < OP_END_RANGE_BINARY));
} // operator_is_binary
static inline int operator_is_ternary(const Operator op)
{
return ((op > OP_START_RANGE_TERNARY) && (op < OP_END_RANGE_TERNARY));
} // operator_is_ternary
typedef struct Expression
{
Operator op; // operator
} Expression;
#define NEW_EXPR(cls) \
cls *retval = Malloc(ctx, sizeof (cls)); \
if (retval == NULL) { return NULL; }
typedef struct ExpressionUnary
{
Operator op; // operator
Expression *operand;
} ExpressionUnary;
typedef struct ExpressionBinary
{
Operator op; // operator
Expression *left;
Expression *right;
} ExpressionBinary;
typedef struct ExpressionTernary
{
Operator op; // operator
Expression *left;
Expression *center;
Expression *right;
} ExpressionTernary;
typedef struct ExpressionIdentifier
{
Operator op; // Always OP_IDENTIFIER
const char *identifier;
} ExpressionIdentifier;
typedef struct ExpressionIntLiteral
{
Operator op; // Always OP_INT_LITERAL
int64 value;
} ExpressionIntLiteral;
typedef struct ExpressionFloatLiteral
{
Operator op; // Always OP_FLOAT_LITERAL
double value;
} ExpressionFloatLiteral;
typedef struct ExpressionStringLiteral
{
Operator op; // Always OP_STRING_LITERAL
const char *string;
} ExpressionStringLiteral;
static Expression *new_unary_expr(Context *ctx, const Operator op,
Expression *operand)
{
NEW_EXPR(ExpressionUnary);
assert(operator_is_unary(op));
retval->op = op;
retval->operand = operand;
return (Expression *) retval;
} // new_unary_expr
static Expression *new_binary_expr(Context *ctx, const Operator op,
Expression *left, Expression *right)
{
NEW_EXPR(ExpressionBinary);
assert(operator_is_binary(op));
retval->op = op;
retval->left = left;
retval->right = right;
return (Expression *) retval;
} // new_binary_expr
static Expression *new_ternary_expr(Context *ctx, const Operator op,
Expression *left, Expression *center,
Expression *right)
{
NEW_EXPR(ExpressionTernary);
assert(operator_is_ternary(op));
retval->op = op;
retval->left = left;
retval->center = center;
retval->right = right;
return (Expression *) retval;
} // new_ternary_expr
static Expression *new_identifier_expr(Context *ctx, const char *string)
{
NEW_EXPR(ExpressionIdentifier);
retval->op = OP_IDENTIFIER;
retval->identifier = string; // cached; don't copy string.
return (Expression *) retval;
} // new_identifier_expr
static inline int64 strtoi64(const char *str, unsigned int len)
{
int64 retval = 0;
int64 mult = 1;
int i = 0;
while ((len) && (*str == ' '))
{
str++;
len--;
} // while
if ((len) && (*str == '-'))
{
mult = -1;
str++;
len--;
} // if
while (i < len)
{
const char ch = str[i];
if ((ch < '0') || (ch > '9'))
break;
i++;
} // while
while (--i >= 0)
{
const char ch = str[i];
retval += ((int64) (ch - '0')) * mult;
mult *= 10;
} // while
return retval;
} // strtoi64
static Expression *new_literal_int_expr(Context *ctx, const int64 value)
{
NEW_EXPR(ExpressionIntLiteral);
retval->op = OP_INT_LITERAL;
retval->value = value;
return (Expression *) retval;
} // new_literal_int_expr
static inline double strtodouble(const char *_str, unsigned int len)
{
// !!! FIXME: laziness prevails.
char *str = (char *) alloca(len+1);
memcpy(str, _str, len);
str[len] = '\0';
return strtod(str, NULL);
} // strtodouble
static Expression *new_literal_float_expr(Context *ctx, const double dbl)
{
NEW_EXPR(ExpressionFloatLiteral);
retval->op = OP_FLOAT_LITERAL;
retval->value = dbl;
return (Expression *) retval;
} // new_literal_float_expr
static Expression *new_literal_string_expr(Context *ctx, const char *string)
{
NEW_EXPR(ExpressionStringLiteral);
retval->op = OP_STRING_LITERAL;
retval->string = string; // cached; don't copy string.
return (Expression *) retval;
} // new_string_literal_expr
static void add_usertype(Context *ctx, const char *sym)
Aug 23, 2009
Aug 23, 2009
310
311
312
313
{
// !!! FIXME: error if this is a reserved keyword.
// !!! FIXME: dynamic allocation
assert(ctx->usertype_count < STATICARRAYLEN(ctx->usertypes));
Feb 9, 2010
Feb 9, 2010
314
ctx->usertypes[ctx->usertype_count++] = sym;
Aug 23, 2009
Aug 23, 2009
315
316
317
ctx->usertype_count++;
} // add_usertype
Feb 9, 2010
Feb 9, 2010
318
319
static int is_usertype(const Context *ctx, const char *token,
const unsigned int tokenlen)
Aug 23, 2009
Aug 23, 2009
320
321
322
323
324
325
{
// !!! FIXME: dynamic allocation
// !!! FIXME: should probably redesign this anyhow.
int i;
for (i = 0; i < ctx->usertype_count; i++)
{
Feb 9, 2010
Feb 9, 2010
326
327
328
const char *type = ctx->usertypes[i];
if (strncmp(type, token, tokenlen) == 0)
return type[tokenlen] == '\0';
Aug 23, 2009
Aug 23, 2009
329
330
331
332
333
334
335
} // for
return 0;
} // is_usertype
// This is where the actual parsing happens. It's Lemon-generated!
Feb 28, 2009
Feb 28, 2009
336
#define __MOJOSHADER_HLSL_COMPILER__ 1
Feb 28, 2009
Feb 28, 2009
337
#include "mojoshader_parser_hlsl.h"
Feb 9, 2010
Feb 9, 2010
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
static void free_expr(Context *ctx, Expression *expr)
{
if (operator_is_unary(expr->op))
{
const ExpressionUnary *unary = (const ExpressionUnary *) expr;
free_expr(ctx, unary->operand);
} // if
else if (operator_is_binary(expr->op))
{
const ExpressionBinary *binary = (const ExpressionBinary *) expr;
free_expr(ctx, binary->left);
free_expr(ctx, binary->right);
} // else if
else if (operator_is_ternary(expr->op))
{
const ExpressionTernary *ternary = (const ExpressionTernary *) expr;
free_expr(ctx, ternary->left);
free_expr(ctx, ternary->center);
free_expr(ctx, ternary->right);
} // else if
// don't need to free extra fields in other types at the moment.
Free(ctx, expr);
} // free_expr
// !!! FIXME: sort of cut-and-paste from the preprocessor...
// this is djb's xor hashing function.
static inline uint32 hash_string_djbxor(const char *str, unsigned int len)
{
register uint32 hash = 5381;
while (len--)
hash = ((hash << 5) + hash) ^ *(str++);
return hash;
} // hash_string_djbxor
static inline uint8 hash_string(const char *str, const unsigned int len)
{
return (uint8) hash_string_djbxor(str, len);
} // hash_string
static const char *cache_string(Context *ctx, const char *str,
const unsigned int len)
{
const uint8 hash = hash_string(str, len);
StringBucket *bucket = ctx->string_hashtable[hash];
StringBucket *prev = NULL;
while (bucket)
{
const char *bstr = bucket->string;
if ((strncmp(bstr, str, len) == 0) && (bstr[len] == 0))
{
// Matched! Move this to the front of the list.
if (prev != NULL)
{
assert(prev->next == bucket);
prev->next = bucket->next;
bucket->next = ctx->string_hashtable[hash];
ctx->string_hashtable[hash] = bucket;
} // if
return bstr; // already cached
} // if
prev = bucket;
bucket = bucket->next;
} // while
// no match, add to the table.
bucket = (StringBucket *) Malloc(ctx, sizeof (StringBucket));
if (bucket == NULL)
return NULL;
bucket->string = (char *) Malloc(ctx, len + 1);
if (bucket->string == NULL)
{
Free(ctx, bucket);
return NULL;
} // if
memcpy(bucket->string, str, len);
bucket->string[len] = '\0';
bucket->next = ctx->string_hashtable[hash];
ctx->string_hashtable[hash] = bucket;
return bucket->string;
} // cache_string
static void free_string_cache(Context *ctx)
{
size_t i;
for (i = 0; i < STATICARRAYLEN(ctx->string_hashtable); i++)
{
StringBucket *bucket = ctx->string_hashtable[i];
ctx->string_hashtable[i] = NULL;
while (bucket)
{
StringBucket *next = bucket->next;
Free(ctx, bucket->string);
Free(ctx, bucket);
bucket = next;
} // while
} // for
} // free_string_cache
Apr 4, 2009
Apr 4, 2009
442
// This does not check correctness (POSITIONT993842 passes, etc).
Feb 9, 2010
Feb 9, 2010
443
444
static int is_semantic(const Context *ctx, const char *token,
const unsigned int tokenlen)
Apr 4, 2009
Apr 4, 2009
445
446
447
448
{
static const char *names[] = {
"BINORMAL", "BLENDINDICES", "BLENDWEIGHT",
"COLOR", "NORMAL", "POSITION", "POSITIONT", "PSIZE", "TANGENT",
Aug 26, 2009
Aug 26, 2009
449
450
"TEXCOORD", "FOG", "TESSFACTOR", "TEXCOORD", "VFACE", "VPOS",
"DEPTH", NULL
Apr 4, 2009
Apr 4, 2009
451
452
453
454
455
456
457
458
};
// !!! FIXME: DX10 has SV_* ("System Value Semantics").
const char **i;
for (i = names; *i; i++)
{
const char *name = *i;
const size_t namelen = strlen(name);
Feb 9, 2010
Feb 9, 2010
459
if (tokenlen < namelen)
Apr 4, 2009
Apr 4, 2009
460
continue;
Feb 9, 2010
Feb 9, 2010
461
else if (memcmp(token, name, namelen) != 0)
Apr 4, 2009
Apr 4, 2009
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
continue;
for (name += namelen; *name; name++)
{
if ((*name < '0') || (*name > '9'))
break;
} // for
if (*name == '\0')
return 1;
} // for
return 0;
} // is_semantic
Feb 9, 2010
Feb 9, 2010
478
479
static int convert_to_lemon_token(const Context *ctx, const char *token,
unsigned int tokenlen, const Token tokenval)
Feb 9, 2010
Feb 9, 2010
481
switch (tokenval)
482
483
484
485
486
487
488
489
490
491
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
519
520
521
522
{
case ((Token) ','): return TOKEN_HLSL_COMMA;
case ((Token) '='): return TOKEN_HLSL_ASSIGN;
case ((Token) TOKEN_ADDASSIGN): return TOKEN_HLSL_ADDASSIGN;
case ((Token) TOKEN_SUBASSIGN): return TOKEN_HLSL_SUBASSIGN;
case ((Token) TOKEN_MULTASSIGN): return TOKEN_HLSL_MULASSIGN;
case ((Token) TOKEN_DIVASSIGN): return TOKEN_HLSL_DIVASSIGN;
case ((Token) TOKEN_MODASSIGN): return TOKEN_HLSL_MODASSIGN;
case ((Token) TOKEN_LSHIFTASSIGN): return TOKEN_HLSL_LSHIFTASSIGN;
case ((Token) TOKEN_RSHIFTASSIGN): return TOKEN_HLSL_RSHIFTASSIGN;
case ((Token) TOKEN_ANDASSIGN): return TOKEN_HLSL_ANDASSIGN;
case ((Token) TOKEN_ORASSIGN): return TOKEN_HLSL_ORASSIGN;
case ((Token) TOKEN_XORASSIGN): return TOKEN_HLSL_XORASSIGN;
case ((Token) '?'): return TOKEN_HLSL_QUESTION;
case ((Token) TOKEN_OROR): return TOKEN_HLSL_OROR;
case ((Token) TOKEN_ANDAND): return TOKEN_HLSL_ANDAND;
case ((Token) '|'): return TOKEN_HLSL_OR;
case ((Token) '^'): return TOKEN_HLSL_XOR;
case ((Token) '&'): return TOKEN_HLSL_AND;
case ((Token) TOKEN_EQL): return TOKEN_HLSL_EQL;
case ((Token) TOKEN_NEQ): return TOKEN_HLSL_NEQ;
case ((Token) '<'): return TOKEN_HLSL_LT;
case ((Token) TOKEN_LEQ): return TOKEN_HLSL_LEQ;
case ((Token) '>'): return TOKEN_HLSL_GT;
case ((Token) TOKEN_GEQ): return TOKEN_HLSL_GEQ;
case ((Token) TOKEN_LSHIFT): return TOKEN_HLSL_LSHIFT;
case ((Token) TOKEN_RSHIFT): return TOKEN_HLSL_RSHIFT;
case ((Token) '+'): return TOKEN_HLSL_PLUS;
case ((Token) '-'): return TOKEN_HLSL_MINUS;
case ((Token) '*'): return TOKEN_HLSL_STAR;
case ((Token) '/'): return TOKEN_HLSL_SLASH;
case ((Token) '%'): return TOKEN_HLSL_PERCENT;
case ((Token) '!'): return TOKEN_HLSL_EXCLAMATION;
case ((Token) '~'): return TOKEN_HLSL_COMPLEMENT;
case ((Token) TOKEN_DECREMENT): return TOKEN_HLSL_MINUSMINUS;
case ((Token) TOKEN_INCREMENT): return TOKEN_HLSL_PLUSPLUS;
case ((Token) '.'): return TOKEN_HLSL_DOT;
case ((Token) '['): return TOKEN_HLSL_LBRACKET;
case ((Token) ']'): return TOKEN_HLSL_RBRACKET;
case ((Token) '('): return TOKEN_HLSL_LPAREN;
case ((Token) ')'): return TOKEN_HLSL_RPAREN;
Mar 7, 2009
Mar 7, 2009
523
524
case ((Token) TOKEN_INT_LITERAL): return TOKEN_HLSL_INT_CONSTANT;
case ((Token) TOKEN_FLOAT_LITERAL): return TOKEN_HLSL_FLOAT_CONSTANT;
525
526
527
528
529
530
531
case ((Token) TOKEN_STRING_LITERAL): return TOKEN_HLSL_STRING_LITERAL;
case ((Token) ':'): return TOKEN_HLSL_COLON;
case ((Token) ';'): return TOKEN_HLSL_SEMICOLON;
case ((Token) '{'): return TOKEN_HLSL_LBRACE;
case ((Token) '}'): return TOKEN_HLSL_RBRACE;
case ((Token) TOKEN_IDENTIFIER):
Feb 9, 2010
Feb 9, 2010
532
#define tokencmp(t) ((tokenlen == strlen(t)) && (memcmp(token, t, tokenlen) == 0))
533
534
535
536
//case ((Token) ''): return TOKEN_HLSL_TYPECAST
//if (tokencmp("")) return TOKEN_HLSL_TYPE_NAME
//if (tokencmp("...")) return TOKEN_HLSL_ELIPSIS
if (tokencmp("else")) return TOKEN_HLSL_ELSE;
Mar 7, 2009
Mar 7, 2009
537
538
539
540
541
542
543
544
545
546
547
548
if (tokencmp("inline")) return TOKEN_HLSL_INLINE;
if (tokencmp("void")) return TOKEN_HLSL_VOID;
if (tokencmp("in")) return TOKEN_HLSL_IN;
if (tokencmp("inout")) return TOKEN_HLSL_INOUT;
if (tokencmp("out")) return TOKEN_HLSL_OUT;
if (tokencmp("uniform")) return TOKEN_HLSL_UNIFORM;
if (tokencmp("linear")) return TOKEN_HLSL_LINEAR;
if (tokencmp("centroid")) return TOKEN_HLSL_CENTROID;
if (tokencmp("nointerpolation")) return TOKEN_HLSL_NOINTERPOLATION;
if (tokencmp("noperspective")) return TOKEN_HLSL_NOPERSPECTIVE;
if (tokencmp("sample")) return TOKEN_HLSL_SAMPLE;
if (tokencmp("struct")) return TOKEN_HLSL_STRUCT;
549
if (tokencmp("typedef")) return TOKEN_HLSL_TYPEDEF;
Mar 7, 2009
Mar 7, 2009
550
551
552
if (tokencmp("const")) return TOKEN_HLSL_CONST;
if (tokencmp("packoffset")) return TOKEN_HLSL_PACKOFFSET;
if (tokencmp("register")) return TOKEN_HLSL_REGISTER;
553
if (tokencmp("extern")) return TOKEN_HLSL_EXTERN;
Mar 7, 2009
Mar 7, 2009
554
if (tokencmp("shared")) return TOKEN_HLSL_SHARED;
555
if (tokencmp("static")) return TOKEN_HLSL_STATIC;
Mar 7, 2009
Mar 7, 2009
556
557
558
559
if (tokencmp("volatile")) return TOKEN_HLSL_VOLATILE;
if (tokencmp("row_major")) return TOKEN_HLSL_ROWMAJOR;
if (tokencmp("column_major")) return TOKEN_HLSL_COLUMNMAJOR;
if (tokencmp("bool")) return TOKEN_HLSL_BOOL;
560
if (tokencmp("int")) return TOKEN_HLSL_INT;
Mar 7, 2009
Mar 7, 2009
561
562
if (tokencmp("uint")) return TOKEN_HLSL_UINT;
if (tokencmp("half")) return TOKEN_HLSL_HALF;
563
564
if (tokencmp("float")) return TOKEN_HLSL_FLOAT;
if (tokencmp("double")) return TOKEN_HLSL_DOUBLE;
Mar 7, 2009
Mar 7, 2009
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
if (tokencmp("string")) return TOKEN_HLSL_STRING;
if (tokencmp("snorm")) return TOKEN_HLSL_SNORM;
if (tokencmp("unorm")) return TOKEN_HLSL_UNORM;
if (tokencmp("buffer")) return TOKEN_HLSL_BUFFER;
if (tokencmp("vector")) return TOKEN_HLSL_VECTOR;
if (tokencmp("bool1")) return TOKEN_HLSL_BOOL1;
if (tokencmp("bool2")) return TOKEN_HLSL_BOOL2;
if (tokencmp("bool3")) return TOKEN_HLSL_BOOL3;
if (tokencmp("bool4")) return TOKEN_HLSL_BOOL4;
if (tokencmp("int1")) return TOKEN_HLSL_INT1;
if (tokencmp("int2")) return TOKEN_HLSL_INT2;
if (tokencmp("int3")) return TOKEN_HLSL_INT3;
if (tokencmp("int4")) return TOKEN_HLSL_INT4;
if (tokencmp("uint1")) return TOKEN_HLSL_UINT1;
if (tokencmp("uint2")) return TOKEN_HLSL_UINT2;
if (tokencmp("uint3")) return TOKEN_HLSL_UINT3;
if (tokencmp("uint4")) return TOKEN_HLSL_UINT4;
if (tokencmp("half1")) return TOKEN_HLSL_HALF1;
if (tokencmp("half2")) return TOKEN_HLSL_HALF2;
if (tokencmp("half3")) return TOKEN_HLSL_HALF3;
if (tokencmp("half4")) return TOKEN_HLSL_HALF4;
if (tokencmp("float1")) return TOKEN_HLSL_FLOAT1;
if (tokencmp("float2")) return TOKEN_HLSL_FLOAT2;
if (tokencmp("float3")) return TOKEN_HLSL_FLOAT3;
if (tokencmp("float4")) return TOKEN_HLSL_FLOAT4;
if (tokencmp("double1")) return TOKEN_HLSL_DOUBLE1;
if (tokencmp("double2")) return TOKEN_HLSL_DOUBLE2;
if (tokencmp("double3")) return TOKEN_HLSL_DOUBLE3;
if (tokencmp("double4")) return TOKEN_HLSL_DOUBLE4;
if (tokencmp("matrix")) return TOKEN_HLSL_MATRIX;
if (tokencmp("bool1x1")) return TOKEN_HLSL_BOOL1X1;
if (tokencmp("bool1x2")) return TOKEN_HLSL_BOOL1X2;
if (tokencmp("bool1x3")) return TOKEN_HLSL_BOOL1X3;
if (tokencmp("bool1x4")) return TOKEN_HLSL_BOOL1X4;
if (tokencmp("bool2x1")) return TOKEN_HLSL_BOOL2X1;
if (tokencmp("bool2x2")) return TOKEN_HLSL_BOOL2X2;
if (tokencmp("bool2x3")) return TOKEN_HLSL_BOOL2X3;
if (tokencmp("bool2x4")) return TOKEN_HLSL_BOOL2X4;
if (tokencmp("bool3x1")) return TOKEN_HLSL_BOOL3X1;
if (tokencmp("bool3x2")) return TOKEN_HLSL_BOOL3X2;
if (tokencmp("bool3x3")) return TOKEN_HLSL_BOOL3X3;
if (tokencmp("bool3x4")) return TOKEN_HLSL_BOOL3X4;
if (tokencmp("bool4x1")) return TOKEN_HLSL_BOOL4X1;
if (tokencmp("bool4x2")) return TOKEN_HLSL_BOOL4X2;
if (tokencmp("bool4x3")) return TOKEN_HLSL_BOOL4X3;
if (tokencmp("bool4x4")) return TOKEN_HLSL_BOOL4X4;
if (tokencmp("int1x1")) return TOKEN_HLSL_INT1X1;
if (tokencmp("int1x2")) return TOKEN_HLSL_INT1X2;
if (tokencmp("int1x3")) return TOKEN_HLSL_INT1X3;
if (tokencmp("int1x4")) return TOKEN_HLSL_INT1X4;
if (tokencmp("int2x1")) return TOKEN_HLSL_INT2X1;
if (tokencmp("int2x2")) return TOKEN_HLSL_INT2X2;
if (tokencmp("int2x3")) return TOKEN_HLSL_INT2X3;
if (tokencmp("int2x4")) return TOKEN_HLSL_INT2X4;
if (tokencmp("int3x1")) return TOKEN_HLSL_INT3X1;
if (tokencmp("int3x2")) return TOKEN_HLSL_INT3X2;
if (tokencmp("int3x3")) return TOKEN_HLSL_INT3X3;
if (tokencmp("int3x4")) return TOKEN_HLSL_INT3X4;
if (tokencmp("int4x1")) return TOKEN_HLSL_INT4X1;
if (tokencmp("int4x2")) return TOKEN_HLSL_INT4X2;
if (tokencmp("int4x3")) return TOKEN_HLSL_INT4X3;
if (tokencmp("int4x4")) return TOKEN_HLSL_INT4X4;
if (tokencmp("uint1x1")) return TOKEN_HLSL_UINT1X1;
if (tokencmp("uint1x2")) return TOKEN_HLSL_UINT1X2;
if (tokencmp("uint1x3")) return TOKEN_HLSL_UINT1X3;
if (tokencmp("uint1x4")) return TOKEN_HLSL_UINT1X4;
if (tokencmp("uint2x1")) return TOKEN_HLSL_UINT2X1;
if (tokencmp("uint2x2")) return TOKEN_HLSL_UINT2X2;
if (tokencmp("uint2x3")) return TOKEN_HLSL_UINT2X3;
if (tokencmp("uint2x4")) return TOKEN_HLSL_UINT2X4;
if (tokencmp("uint3x1")) return TOKEN_HLSL_UINT3X1;
if (tokencmp("uint3x2")) return TOKEN_HLSL_UINT3X2;
if (tokencmp("uint3x3")) return TOKEN_HLSL_UINT3X3;
if (tokencmp("uint3x4")) return TOKEN_HLSL_UINT3X4;
if (tokencmp("uint4x1")) return TOKEN_HLSL_UINT4X1;
if (tokencmp("uint4x2")) return TOKEN_HLSL_UINT4X2;
if (tokencmp("uint4x3")) return TOKEN_HLSL_UINT4X3;
if (tokencmp("uint4x4")) return TOKEN_HLSL_UINT4X4;
if (tokencmp("half1x1")) return TOKEN_HLSL_HALF1X1;
if (tokencmp("half1x2")) return TOKEN_HLSL_HALF1X2;
if (tokencmp("half1x3")) return TOKEN_HLSL_HALF1X3;
if (tokencmp("half1x4")) return TOKEN_HLSL_HALF1X4;
if (tokencmp("half2x1")) return TOKEN_HLSL_HALF2X1;
if (tokencmp("half2x2")) return TOKEN_HLSL_HALF2X2;
if (tokencmp("half2x3")) return TOKEN_HLSL_HALF2X3;
if (tokencmp("half2x4")) return TOKEN_HLSL_HALF2X4;
if (tokencmp("half3x1")) return TOKEN_HLSL_HALF3X1;
if (tokencmp("half3x2")) return TOKEN_HLSL_HALF3X2;
if (tokencmp("half3x3")) return TOKEN_HLSL_HALF3X3;
if (tokencmp("half3x4")) return TOKEN_HLSL_HALF3X4;
if (tokencmp("half4x1")) return TOKEN_HLSL_HALF4X1;
if (tokencmp("half4x2")) return TOKEN_HLSL_HALF4X2;
if (tokencmp("half4x3")) return TOKEN_HLSL_HALF4X3;
if (tokencmp("half4x4")) return TOKEN_HLSL_HALF4X4;
if (tokencmp("float1x1")) return TOKEN_HLSL_FLOAT1X1;
if (tokencmp("float1x2")) return TOKEN_HLSL_FLOAT1X2;
if (tokencmp("float1x3")) return TOKEN_HLSL_FLOAT1X3;
if (tokencmp("float1x4")) return TOKEN_HLSL_FLOAT1X4;
if (tokencmp("float2x1")) return TOKEN_HLSL_FLOAT2X1;
if (tokencmp("float2x2")) return TOKEN_HLSL_FLOAT2X2;
if (tokencmp("float2x3")) return TOKEN_HLSL_FLOAT2X3;
if (tokencmp("float2x4")) return TOKEN_HLSL_FLOAT2X4;
if (tokencmp("float3x1")) return TOKEN_HLSL_FLOAT3X1;
if (tokencmp("float3x2")) return TOKEN_HLSL_FLOAT3X2;
if (tokencmp("float3x3")) return TOKEN_HLSL_FLOAT3X3;
if (tokencmp("float3x4")) return TOKEN_HLSL_FLOAT3X4;
if (tokencmp("float4x1")) return TOKEN_HLSL_FLOAT4X1;
if (tokencmp("float4x2")) return TOKEN_HLSL_FLOAT4X2;
if (tokencmp("float4x3")) return TOKEN_HLSL_FLOAT4X3;
if (tokencmp("float4x4")) return TOKEN_HLSL_FLOAT4X4;
if (tokencmp("double1x1")) return TOKEN_HLSL_DOUBLE1X1;
if (tokencmp("double1x2")) return TOKEN_HLSL_DOUBLE1X2;
if (tokencmp("double1x3")) return TOKEN_HLSL_DOUBLE1X3;
if (tokencmp("double1x4")) return TOKEN_HLSL_DOUBLE1X4;
if (tokencmp("double2x1")) return TOKEN_HLSL_DOUBLE2X1;
if (tokencmp("double2x2")) return TOKEN_HLSL_DOUBLE2X2;
if (tokencmp("double2x3")) return TOKEN_HLSL_DOUBLE2X3;
if (tokencmp("double2x4")) return TOKEN_HLSL_DOUBLE2X4;
if (tokencmp("double3x1")) return TOKEN_HLSL_DOUBLE3X1;
if (tokencmp("double3x2")) return TOKEN_HLSL_DOUBLE3X2;
if (tokencmp("double3x3")) return TOKEN_HLSL_DOUBLE3X3;
if (tokencmp("double3x4")) return TOKEN_HLSL_DOUBLE3X4;
if (tokencmp("double4x1")) return TOKEN_HLSL_DOUBLE4X1;
if (tokencmp("double4x2")) return TOKEN_HLSL_DOUBLE4X2;
if (tokencmp("double4x3")) return TOKEN_HLSL_DOUBLE4X3;
if (tokencmp("double4x4")) return TOKEN_HLSL_DOUBLE4X4;
if (tokencmp("break")) return TOKEN_HLSL_BREAK;
if (tokencmp("continue")) return TOKEN_HLSL_CONTINUE;
Feb 28, 2009
Feb 28, 2009
693
if (tokencmp("discard")) return TOKEN_HLSL_DISCARD;
Mar 7, 2009
Mar 7, 2009
694
if (tokencmp("return")) return TOKEN_HLSL_RETURN;
695
696
if (tokencmp("while")) return TOKEN_HLSL_WHILE;
if (tokencmp("for")) return TOKEN_HLSL_FOR;
Mar 7, 2009
Mar 7, 2009
697
698
699
700
701
702
703
704
705
706
707
if (tokencmp("unroll")) return TOKEN_HLSL_UNROLL;
if (tokencmp("loop")) return TOKEN_HLSL_LOOP;
if (tokencmp("do")) return TOKEN_HLSL_DO;
if (tokencmp("if")) return TOKEN_HLSL_IF;
if (tokencmp("branch")) return TOKEN_HLSL_BRANCH;
if (tokencmp("flatten")) return TOKEN_HLSL_FLATTEN;
if (tokencmp("switch")) return TOKEN_HLSL_SWITCH;
if (tokencmp("forcecase")) return TOKEN_HLSL_FORCECASE;
if (tokencmp("call")) return TOKEN_HLSL_CALL;
if (tokencmp("case")) return TOKEN_HLSL_CASE;
if (tokencmp("default")) return TOKEN_HLSL_DEFAULT;
Mar 7, 2009
Mar 7, 2009
708
709
710
711
712
713
714
715
if (tokencmp("sampler")) return TOKEN_HLSL_SAMPLER;
if (tokencmp("sampler1D")) return TOKEN_HLSL_SAMPLER1D;
if (tokencmp("sampler2D")) return TOKEN_HLSL_SAMPLER2D;
if (tokencmp("sampler3D")) return TOKEN_HLSL_SAMPLER3D;
if (tokencmp("samplerCUBE")) return TOKEN_HLSL_SAMPLERCUBE;
if (tokencmp("sampler_state")) return TOKEN_HLSL_SAMPLER_STATE;
if (tokencmp("SamplerState")) return TOKEN_HLSL_SAMPLERSTATE;
if (tokencmp("SamplerComparisonState")) return TOKEN_HLSL_SAMPLERCOMPARISONSTATE;
Aug 26, 2009
Aug 26, 2009
716
717
718
719
720
if (tokencmp("isolate")) return TOKEN_HLSL_ISOLATE;
if (tokencmp("maxInstructionCount")) return TOKEN_HLSL_MAXINSTRUCTIONCOUNT;
if (tokencmp("noExpressionOptimizations")) return TOKEN_HLSL_NOEXPRESSIONOPTIMIZATIONS;
if (tokencmp("unused")) return TOKEN_HLSL_UNUSED;
if (tokencmp("xps")) return TOKEN_HLSL_XPS;
Mar 7, 2009
Mar 7, 2009
721
Apr 4, 2009
Apr 4, 2009
723
Feb 9, 2010
Feb 9, 2010
724
if (is_semantic(ctx, token, tokenlen))
Apr 4, 2009
Apr 4, 2009
725
return TOKEN_HLSL_SEMANTIC;
Feb 9, 2010
Feb 9, 2010
726
else if (is_usertype(ctx, token, tokenlen))
Aug 23, 2009
Aug 23, 2009
727
return TOKEN_HLSL_USERTYPE;
728
729
730
731
732
733
734
735
736
return TOKEN_HLSL_IDENTIFIER;
case TOKEN_EOI: return 0;
case TOKEN_BAD_CHARS: printf("bad chars from lexer\n"); return 0;
case TOKEN_PREPROCESSING_ERROR: printf("error from lexer\n"); return 0;
default: assert(0 && "unexpected token from lexer\n"); return 0;
} // switch
return 0;
Apr 4, 2009
Apr 4, 2009
737
} // convert_to_lemon_token
738
739
740
void MOJOSHADER_compile(const char *filename,
Feb 9, 2010
Feb 9, 2010
741
742
743
744
745
746
const char *source, unsigned int sourcelen,
const MOJOSHADER_preprocessorDefine *defines,
unsigned int define_count,
MOJOSHADER_includeOpen include_open,
MOJOSHADER_includeClose include_close,
MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
Feb 9, 2010
Feb 9, 2010
749
750
751
752
753
754
TokenData data;
unsigned int tokenlen;
Token tokenval;
const char *token;
int lemon_token;
755
756
if (m == NULL) m = MOJOSHADER_internal_malloc;
if (f == NULL) f = MOJOSHADER_internal_free;
Aug 23, 2009
Aug 23, 2009
757
758
memset(&ctx, '\0', sizeof (Context));
Feb 9, 2010
Feb 9, 2010
759
760
761
ctx.malloc = m;
ctx.free = f;
ctx.malloc_data = d;
762
763
764
765
766
ctx.preprocessor = preprocessor_start(filename, source, sourcelen,
include_open, include_close,
defines, define_count, 0, m, f, d);
void *pParser = ParseHLSLAlloc(m, d);
Feb 28, 2009
Feb 28, 2009
767
768
769
770
#if DEBUG_COMPILER_PARSER
ParseHLSLTrace(stdout, "COMPILER: ");
#endif
Feb 9, 2010
Feb 9, 2010
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
token = preprocessor_nexttoken(ctx.preprocessor, &tokenlen, &tokenval);
lemon_token = convert_to_lemon_token(&ctx, token, tokenlen, tokenval);
switch (lemon_token)
{
case TOKEN_HLSL_INT_CONSTANT:
data.i64 = strtoi64(token, tokenlen);
break;
case TOKEN_HLSL_FLOAT_CONSTANT:
data.dbl = strtodouble(token, tokenlen);
break;
case TOKEN_HLSL_SEMANTIC:
case TOKEN_HLSL_USERTYPE:
case TOKEN_HLSL_STRING_LITERAL:
case TOKEN_HLSL_IDENTIFIER:
data.string = cache_string(&ctx, token, tokenlen);
break;
default:
data.i64 = 0;
break;
} // switch
ParseHLSL(pParser, lemon_token, data, &ctx);
} while ((!ctx.isfail) && (tokenval != TOKEN_EOI));
Aug 23, 2009
Aug 23, 2009
799
800
ParseHLSLFree(pParser, f, d);
Feb 9, 2010
Feb 9, 2010
801
802
803
// !!! FIXME: destruct (ctx) here.
free_string_cache(&ctx);
} // MOJOSHADER_compile
Aug 23, 2009
Aug 23, 2009
805
// end of mojoshader_compiler.c ...