/
mojoshader_compiler.c
4805 lines (4201 loc) · 179 KB
1
2
3
4
5
6
7
8
9
/**
* MojoShader; generate shader programs from bytecode of compiled
* Direct3D shaders.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
10
11
12
13
// !!! FIXME: this needs to be split into separate source files:
// !!! FIXME: parse, AST, IR, etc. The problem is we need to deal with the
// !!! FIXME: "Context" struct being passed around everywhere.
14
15
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"
16
17
18
19
20
#if DEBUG_COMPILER_PARSER
#define LEMON_SUPPORT_TRACING 1
#endif
21
22
// !!! FIXME: I'd like to lose this. It's really inefficient. Just keep a
// !!! FIXME: (tail) on these list structures instead?
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#define REVERSE_LINKED_LIST(typ, head) { \
if ((head) && (head->next)) { \
typ *tmp = NULL; \
typ *tmp1 = NULL; \
while (head != NULL) { \
tmp = head; \
head = head->next; \
tmp->next = tmp1; \
tmp1 = tmp; \
} \
head = tmp; \
} \
}
37
static inline int operator_is_unary(const MOJOSHADER_astNodeType op)
38
{
39
40
return ( (op > MOJOSHADER_AST_OP_START_RANGE_UNARY) &&
(op < MOJOSHADER_AST_OP_END_RANGE_UNARY) );
41
42
} // operator_is_unary
43
static inline int operator_is_binary(const MOJOSHADER_astNodeType op)
44
{
45
46
return ( (op > MOJOSHADER_AST_OP_START_RANGE_BINARY) &&
(op < MOJOSHADER_AST_OP_END_RANGE_BINARY) );
47
48
} // operator_is_binary
49
static inline int operator_is_ternary(const MOJOSHADER_astNodeType op)
50
{
51
52
return ( (op > MOJOSHADER_AST_OP_START_RANGE_TERNARY) &&
(op < MOJOSHADER_AST_OP_END_RANGE_TERNARY) );
53
54
55
} // operator_is_ternary
56
typedef union TokenData
57
{
58
59
int64 i64;
double dbl;
60
const char *string;
61
const MOJOSHADER_astDataType *datatype;
62
} TokenData;
63
64
65
// This tracks data types and variables, and notes when they enter/leave scope.
66
67
typedef struct SymbolScope
68
69
{
const char *symbol;
70
const MOJOSHADER_astDataType *datatype;
71
int index; // unique positive value within a function, negative if global.
72
int referenced; // non-zero if something looked for this symbol (so we know it's used).
73
74
struct SymbolScope *next;
} SymbolScope;
75
76
typedef struct SymbolMap
77
{
78
79
80
HashTable *hash;
SymbolScope *scope;
} SymbolMap;
81
82
83
84
85
86
87
88
89
90
91
92
// Compile state, passed around all over the place.
typedef struct Context
{
int isfail;
int out_of_memory;
MOJOSHADER_malloc malloc;
MOJOSHADER_free free;
void *malloc_data;
ErrorList *errors;
93
ErrorList *warnings;
94
StringCache *strcache;
95
96
const char *sourcefile; // current source file that we're parsing.
unsigned int sourceline; // current line in sourcefile that we're parsing.
97
98
SymbolMap usertypes;
SymbolMap variables;
99
MOJOSHADER_astNode *ast; // Abstract Syntax Tree
100
const char *source_profile;
101
int is_func_scope; // non-zero if semantic analysis is in function scope.
102
103
int loop_count;
int switch_count;
104
105
int var_index; // next variable index for current function.
int global_var_index; // next variable index for global scope.
106
107
int user_func_index; // next function index for user-defined functions.
int intrinsic_func_index; // next function index for intrinsic functions.
108
109
// Cache intrinsic types for fast lookup and consistent pointer values.
110
MOJOSHADER_astDataType dt_none;
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
MOJOSHADER_astDataType dt_bool;
MOJOSHADER_astDataType dt_int;
MOJOSHADER_astDataType dt_uint;
MOJOSHADER_astDataType dt_float;
MOJOSHADER_astDataType dt_float_snorm;
MOJOSHADER_astDataType dt_float_unorm;
MOJOSHADER_astDataType dt_half;
MOJOSHADER_astDataType dt_double;
MOJOSHADER_astDataType dt_string;
MOJOSHADER_astDataType dt_sampler1d;
MOJOSHADER_astDataType dt_sampler2d;
MOJOSHADER_astDataType dt_sampler3d;
MOJOSHADER_astDataType dt_samplercube;
MOJOSHADER_astDataType dt_samplerstate;
MOJOSHADER_astDataType dt_samplercompstate;
MOJOSHADER_astDataType dt_buf_bool;
MOJOSHADER_astDataType dt_buf_int;
MOJOSHADER_astDataType dt_buf_uint;
MOJOSHADER_astDataType dt_buf_half;
MOJOSHADER_astDataType dt_buf_float;
MOJOSHADER_astDataType dt_buf_double;
MOJOSHADER_astDataType dt_buf_float_snorm;
MOJOSHADER_astDataType dt_buf_float_unorm;
Buffer *garbage; // this is sort of hacky.
136
137
138
} Context;
139
140
141
142
// !!! FIXME: cut and paste between every damned source file follows...
// !!! FIXME: We need to make some sort of ContextBase that applies to all
// !!! FIXME: files and move this stuff to mojoshader_common.c ...
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
// 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)
{
168
ctx->free(ptr, ctx->malloc_data);
169
170
} // Free
171
172
173
174
175
176
177
178
179
180
static void *MallocBridge(int bytes, void *data)
{
return Malloc((Context *) data, (size_t) bytes);
} // MallocBridge
static void FreeBridge(void *ptr, void *data)
{
Free((Context *) data, ptr);
} // FreeBridge
181
182
183
184
static void failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
static void failf(Context *ctx, const char *fmt, ...)
{
ctx->isfail = 1;
185
if (ctx->out_of_memory)
186
187
188
189
return;
va_list ap;
va_start(ap, fmt);
190
errorlist_add_va(ctx->errors, ctx->sourcefile, ctx->sourceline, fmt, ap);
191
192
193
194
va_end(ap);
} // failf
static inline void fail(Context *ctx, const char *reason)
195
{
196
failf(ctx, "%s", reason);
197
} // fail
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
static void warnf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
static void warnf(Context *ctx, const char *fmt, ...)
{
if (ctx->out_of_memory)
return;
va_list ap;
va_start(ap, fmt);
errorlist_add_va(ctx->warnings, ctx->sourcefile, ctx->sourceline, fmt, ap);
va_end(ap);
} // warnf
static inline void warn(Context *ctx, const char *reason)
{
warnf(ctx, "%s", reason);
} // warn
216
217
218
219
220
static inline int isfail(const Context *ctx)
{
return ctx->isfail;
} // isfail
221
222
static void symbolmap_nuke(const void *k, const void *v, void *d) {/*no-op*/}
223
224
static int create_symbolmap(Context *ctx, SymbolMap *map)
225
{
226
// !!! FIXME: should compare string pointer, with string in cache.
227
map->scope = NULL;
228
map->hash = hash_create(ctx, hash_hash_string, hash_keymatch_string,
229
230
symbolmap_nuke, 1, MallocBridge, FreeBridge, ctx);
return (map->hash != NULL);
231
} // create_symbolmap
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
static int datatypes_match(const MOJOSHADER_astDataType *a,
const MOJOSHADER_astDataType *b)
{
int i;
if (a == b)
return 1;
else if (a->type != b->type)
return 0;
switch (a->type)
{
case MOJOSHADER_AST_DATATYPE_STRUCT:
if (a->structure.member_count != b->structure.member_count)
return 0;
for (i = 0; i < a->structure.member_count; i++)
{
if (!datatypes_match(a->structure.members[i].datatype,
b->structure.members[i].datatype))
return 0;
// stringcache'd, pointer compare is safe.
else if (a->structure.members[i].identifier !=
b->structure.members[i].identifier)
return 0;
} // for
return 1;
case MOJOSHADER_AST_DATATYPE_ARRAY:
if (a->array.elements != b->array.elements)
return 0;
else if (!datatypes_match(a->array.base, b->array.base))
return 0;
return 1;
case MOJOSHADER_AST_DATATYPE_VECTOR:
if (a->vector.elements != b->vector.elements)
return 0;
else if (!datatypes_match(a->vector.base, b->vector.base))
return 0;
return 1;
case MOJOSHADER_AST_DATATYPE_MATRIX:
if (a->matrix.rows != b->matrix.rows)
return 0;
else if (a->matrix.columns != b->matrix.columns)
return 0;
else if (!datatypes_match(a->matrix.base, b->matrix.base))
return 0;
return 1;
case MOJOSHADER_AST_DATATYPE_BUFFER:
return datatypes_match(a->buffer.base, b->buffer.base);
case MOJOSHADER_AST_DATATYPE_FUNCTION:
if (a->function.num_params != b->function.num_params)
return 0;
else if (a->function.intrinsic != b->function.intrinsic)
return 0;
else if (!datatypes_match(a->function.retval, b->function.retval))
return 0;
for (i = 0; i < a->function.num_params; i++)
{
if (!datatypes_match(a->function.params[i], b->function.params[i]))
return 0;
} // for
return 1;
case MOJOSHADER_AST_DATATYPE_USER:
return 0; // pointers must match, this clearly didn't.
default:
assert(0 && "unexpected case");
return 0;
} // switch
return 0;
} // datatypes_match
310
311
static void push_symbol(Context *ctx, SymbolMap *map, const char *sym,
312
313
const MOJOSHADER_astDataType *dt, const int index,
const int check_dupes)
314
{
315
316
317
if (ctx->out_of_memory)
return;
318
319
320
// Decide if this symbol is defined, and if it's in the current scope.
SymbolScope *item = NULL;
const void *value = NULL;
321
if ((check_dupes) && (sym != NULL) && (hash_find(map->hash, sym, &value)))
322
{
323
324
325
326
327
// check the current scope for a dupe.
// !!! FIXME: note current scope's starting index, see if found
// !!! FIXME: item is < index (and thus, a previous scope).
item = map->scope;
while ((item) && (item->symbol))
328
{
329
if ( ((const void *) item) == value )
330
{
331
332
333
334
335
failf(ctx, "Symbol '%s' already defined", sym);
return;
} // if
item = item->next;
} // while
336
} // if
337
338
339
// Add the symbol to our map and scope stack.
item = (SymbolScope *) Malloc(ctx, sizeof (SymbolScope));
340
341
342
if (item == NULL)
return;
343
if (sym != NULL) // sym can be NULL if we're pushing a new scope.
344
{
345
if (hash_insert(map->hash, sym, item) == -1)
346
347
348
{
Free(ctx, item);
return;
349
} // if
350
351
352
} // if
item->symbol = sym; // cached strings, don't copy.
353
item->index = index;
354
item->datatype = dt;
355
item->referenced = 0;
356
357
item->next = map->scope;
map->scope = item;
358
359
} // push_symbol
360
static void push_usertype(Context *ctx, const char *sym, const MOJOSHADER_astDataType *dt)
361
{
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
if (sym != NULL)
{
MOJOSHADER_astDataType *userdt;
userdt = (MOJOSHADER_astDataType *) Malloc(ctx, sizeof (*userdt));
if (userdt != NULL)
{
// !!! FIXME: this is hacky.
if (!buffer_append(ctx->garbage, &userdt, sizeof (userdt)))
{
Free(ctx, userdt);
userdt = NULL;
} // if
userdt->type = MOJOSHADER_AST_DATATYPE_USER;
userdt->user.details = dt;
userdt->user.name = sym;
dt = userdt;
} // if
} // if
383
push_symbol(ctx, &ctx->usertypes, sym, dt, 0, 1);
384
385
} // push_usertype
386
static inline void push_variable(Context *ctx, const char *sym, const MOJOSHADER_astDataType *dt)
387
{
388
389
390
391
392
393
394
395
396
int idx = 0;
if (sym != NULL)
{
if (ctx->is_func_scope)
idx = ++ctx->var_index; // these are positive.
else
idx = --ctx->global_var_index; // these are negative.
} // if
397
push_symbol(ctx, &ctx->variables, sym, dt, idx, 1);
398
399
} // push_variable
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
static void push_function(Context *ctx, const char *sym,
const MOJOSHADER_astDataType *dt,
const int just_declare)
{
// we don't have any reason to support nested functions at the moment,
// so this would be a bug.
assert(!ctx->is_func_scope);
assert(dt->type == MOJOSHADER_AST_DATATYPE_FUNCTION);
int idx = 0;
if ((sym != NULL) && (dt != NULL))
{
if (!dt->function.intrinsic)
idx = ++ctx->user_func_index; // these are positive.
else
idx = --ctx->intrinsic_func_index; // these are negative.
} // if
// Functions are always global, so no need to search scopes.
// Functions overload, though, so we have to continue iterating to
// see if it matches anything.
const void *value = NULL;
void *iter = NULL;
while (hash_iter(ctx->variables.hash, sym, &value, &iter))
{
// there's already something called this.
if (datatypes_match(dt, ((SymbolScope *) value)->datatype))
{
if (!just_declare)
failf(ctx, "Function '%s' already defined.", sym);
return;
} // if
} // while
// push_symbol() doesn't check dupes, because we just did.
push_symbol(ctx, &ctx->variables, sym, dt, idx, 0);
} // push_function
438
439
440
441
442
443
444
static inline void push_scope(Context *ctx)
{
push_usertype(ctx, NULL, NULL);
push_variable(ctx, NULL, NULL);
} // push_scope
static void pop_symbol(Context *ctx, SymbolMap *map)
445
{
446
SymbolScope *item = map->scope;
447
448
449
if (!item)
return;
if (item->symbol)
450
hash_remove(map->hash, item->symbol);
451
452
map->scope = item->next;
Free(ctx, item);
453
} // pop_symbol
454
455
static void pop_symbol_scope(Context *ctx, SymbolMap *map)
456
457
{
while ((map->scope) && (map->scope->symbol))
458
pop_symbol(ctx, map);
459
460
461
assert(map->scope != NULL);
assert(map->scope->symbol == NULL);
462
463
464
465
466
467
468
pop_symbol(ctx, map);
} // pop_symbol_scope
static inline void pop_scope(Context *ctx)
{
pop_symbol_scope(ctx, &ctx->usertypes);
pop_symbol_scope(ctx, &ctx->variables);
469
470
} // push_scope
471
static const MOJOSHADER_astDataType *find_symbol(Context *ctx, SymbolMap *map, const char *sym, int *_index)
472
{
473
474
const void *_item = NULL;
hash_find(map->hash, sym, &_item);
475
SymbolScope *item = (SymbolScope *) _item;
476
477
478
479
480
481
if (item != NULL)
{
item->referenced++;
if (_index != NULL)
*_index = item->index;
} // if
482
return item ? item->datatype : NULL;
483
484
} // find_symbol
485
static inline const MOJOSHADER_astDataType *find_usertype(Context *ctx, const char *sym)
486
{
487
return find_symbol(ctx, &ctx->usertypes, sym, NULL);
488
489
} // find_usertype
490
static inline const MOJOSHADER_astDataType *find_variable(Context *ctx, const char *sym, int *_index)
491
{
492
return find_symbol(ctx, &ctx->variables, sym, _index);
493
494
} // find_variable
495
static void destroy_symbolmap(Context *ctx, SymbolMap *map)
496
497
{
while (map->scope)
498
pop_symbol(ctx, map);
499
500
hash_destroy(map->hash);
} // destroy_symbolmap
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
static const MOJOSHADER_astDataType *new_datatype_vector(Context *ctx,
const MOJOSHADER_astDataType *dt,
const int columns)
{
MOJOSHADER_astDataType *retval;
retval = (MOJOSHADER_astDataType *) Malloc(ctx, sizeof (*retval));
if (retval == NULL)
return NULL;
// !!! FIXME: this is hacky.
// !!! FIXME: I'd like to cache these anyhow and reuse types.
if (!buffer_append(ctx->garbage, &retval, sizeof (retval)))
{
Free(ctx, retval);
return NULL;
} // if
if ((columns < 1) || (columns > 4))
fail(ctx, "Vector must have between 1 and 4 elements");
retval->type = MOJOSHADER_AST_DATATYPE_VECTOR;
retval->vector.base = dt;
retval->vector.elements = columns;
return retval;
} // new_datatype_vector
static const MOJOSHADER_astDataType *new_datatype_matrix(Context *ctx,
const MOJOSHADER_astDataType *dt,
const int rows, const int columns)
{
MOJOSHADER_astDataType *retval;
// !!! FIXME: allocate enough for a matrix, but we need to cleanup things that copy without checking for subsize.
retval = (MOJOSHADER_astDataType *) Malloc(ctx, sizeof (*retval));
if (retval == NULL)
return NULL;
// !!! FIXME: this is hacky.
// !!! FIXME: I'd like to cache these anyhow and reuse types.
if (!buffer_append(ctx->garbage, &retval, sizeof (retval)))
{
Free(ctx, retval);
return NULL;
} // if
if ((rows < 1) || (rows > 4))
fail(ctx, "Matrix must have between 1 and 4 rows");
if ((columns < 1) || (columns > 4))
fail(ctx, "Matrix must have between 1 and 4 columns");
retval->type = MOJOSHADER_AST_DATATYPE_MATRIX;
retval->matrix.base = dt;
retval->matrix.rows = rows;
retval->matrix.columns = columns;
return retval;
} // new_datatype_matrix
// !!! FIXME: move this to mojoshader_ast.c
561
562
// !!! FIXME: new_* and delete_* should take an allocator, not a context.
563
564
565
566
567
// These functions are mostly for construction and cleanup of nodes in the
// parse tree. Mostly this is simple allocation and initialization, so we
// can do as little in the lemon code as possible, and then sort it all out
// afterwards.
568
#define NEW_AST_NODE(retval, cls, typ) \
569
cls *retval = (cls *) Malloc(ctx, sizeof (cls)); \
570
571
572
573
574
575
576
577
do { \
if (retval == NULL) { return NULL; } \
retval->ast.type = typ; \
retval->ast.filename = ctx->sourcefile; \
retval->ast.line = ctx->sourceline; \
} while (0)
#define DELETE_AST_NODE(cls) do { \
578
579
580
if (!cls) return; \
} while (0)
581
582
583
static void delete_compilation_unit(Context*, MOJOSHADER_astCompilationUnit*);
static void delete_statement(Context *ctx, MOJOSHADER_astStatement *stmt);
584
585
586
587
588
589
static MOJOSHADER_astExpression *new_identifier_expr(Context *ctx,
const char *string)
{
NEW_AST_NODE(retval, MOJOSHADER_astExpressionIdentifier,
MOJOSHADER_AST_OP_IDENTIFIER);
590
retval->datatype = NULL;
591
retval->identifier = string; // cached; don't copy string.
592
retval->index = 0;
593
594
595
return (MOJOSHADER_astExpression *) retval;
} // new_identifier_expr
596
static MOJOSHADER_astExpression *new_callfunc_expr(Context *ctx,
597
const char *identifier,
598
MOJOSHADER_astArguments *args)
599
{
600
601
NEW_AST_NODE(retval, MOJOSHADER_astExpressionCallFunction,
MOJOSHADER_AST_OP_CALLFUNC);
602
MOJOSHADER_astExpression *expr = new_identifier_expr(ctx, identifier);
603
retval->datatype = NULL;
604
retval->identifier = (MOJOSHADER_astExpressionIdentifier *) expr;
605
retval->args = args;
606
return (MOJOSHADER_astExpression *) retval;
607
608
} // new_callfunc_expr
609
static MOJOSHADER_astExpression *new_constructor_expr(Context *ctx,
610
611
const MOJOSHADER_astDataType *dt,
MOJOSHADER_astArguments *args)
612
{
613
614
NEW_AST_NODE(retval, MOJOSHADER_astExpressionConstructor,
MOJOSHADER_AST_OP_CONSTRUCTOR);
615
retval->datatype = dt;
616
retval->args = args;
617
return (MOJOSHADER_astExpression *) retval;
618
619
} // new_constructor_expr
620
static MOJOSHADER_astExpression *new_cast_expr(Context *ctx,
621
const MOJOSHADER_astDataType *dt,
622
MOJOSHADER_astExpression *operand)
623
{
624
NEW_AST_NODE(retval, MOJOSHADER_astExpressionCast, MOJOSHADER_AST_OP_CAST);
625
retval->datatype = dt;
626
retval->operand = operand;
627
return (MOJOSHADER_astExpression *) retval;
628
629
} // new_cast_expr
630
631
632
static MOJOSHADER_astExpression *new_unary_expr(Context *ctx,
const MOJOSHADER_astNodeType op,
MOJOSHADER_astExpression *operand)
633
{
634
NEW_AST_NODE(retval, MOJOSHADER_astExpressionUnary, op);
635
assert(operator_is_unary(op));
636
retval->datatype = NULL;
637
retval->operand = operand;
638
return (MOJOSHADER_astExpression *) retval;
639
640
} // new_unary_expr
641
642
643
644
static MOJOSHADER_astExpression *new_binary_expr(Context *ctx,
const MOJOSHADER_astNodeType op,
MOJOSHADER_astExpression *left,
MOJOSHADER_astExpression *right)
645
{
646
NEW_AST_NODE(retval, MOJOSHADER_astExpressionBinary, op);
647
assert(operator_is_binary(op));
648
retval->datatype = NULL;
649
650
retval->left = left;
retval->right = right;
651
return (MOJOSHADER_astExpression *) retval;
652
653
} // new_binary_expr
654
655
656
657
658
static MOJOSHADER_astExpression *new_ternary_expr(Context *ctx,
const MOJOSHADER_astNodeType op,
MOJOSHADER_astExpression *left,
MOJOSHADER_astExpression *center,
MOJOSHADER_astExpression *right)
659
{
660
NEW_AST_NODE(retval, MOJOSHADER_astExpressionTernary, op);
661
assert(operator_is_ternary(op));
662
663
assert(op == MOJOSHADER_AST_OP_CONDITIONAL);
retval->datatype = &ctx->dt_bool;
664
665
666
retval->left = left;
retval->center = center;
retval->right = right;
667
return (MOJOSHADER_astExpression *) retval;
668
669
} // new_ternary_expr
670
671
672
static MOJOSHADER_astExpression *new_deref_struct_expr(Context *ctx,
MOJOSHADER_astExpression *identifier,
const char *member)
673
{
674
675
NEW_AST_NODE(retval, MOJOSHADER_astExpressionDerefStruct,
MOJOSHADER_AST_OP_DEREF_STRUCT);
676
retval->datatype = NULL;
677
678
retval->identifier = identifier;
retval->member = member; // cached; don't copy string.
679
retval->isswizzle = 0; // may change during semantic analysis.
680
return (MOJOSHADER_astExpression *) retval;
681
682
} // new_deref_struct_expr
683
684
static MOJOSHADER_astExpression *new_literal_int_expr(Context *ctx,
const int value)
685
{
686
687
NEW_AST_NODE(retval, MOJOSHADER_astExpressionIntLiteral,
MOJOSHADER_AST_OP_INT_LITERAL);
688
retval->datatype = &ctx->dt_int;
689
retval->value = value;
690
return (MOJOSHADER_astExpression *) retval;
691
692
} // new_literal_int_expr
693
694
static MOJOSHADER_astExpression *new_literal_float_expr(Context *ctx,
const double dbl)
695
{
696
697
NEW_AST_NODE(retval, MOJOSHADER_astExpressionFloatLiteral,
MOJOSHADER_AST_OP_FLOAT_LITERAL);
698
retval->datatype = &ctx->dt_float;
699
retval->value = dbl;
700
return (MOJOSHADER_astExpression *) retval;
701
702
} // new_literal_float_expr
703
704
static MOJOSHADER_astExpression *new_literal_string_expr(Context *ctx,
const char *string)
705
{
706
707
NEW_AST_NODE(retval, MOJOSHADER_astExpressionStringLiteral,
MOJOSHADER_AST_OP_STRING_LITERAL);
708
retval->datatype = &ctx->dt_string;
709
retval->string = string; // cached; don't copy string.
710
return (MOJOSHADER_astExpression *) retval;
711
712
} // new_literal_string_expr
713
714
static MOJOSHADER_astExpression *new_literal_boolean_expr(Context *ctx,
const int value)
715
{
716
717
NEW_AST_NODE(retval, MOJOSHADER_astExpressionBooleanLiteral,
MOJOSHADER_AST_OP_BOOLEAN_LITERAL);
718
retval->datatype = &ctx->dt_bool;
719
retval->value = value;
720
return (MOJOSHADER_astExpression *) retval;
721
} // new_literal_boolean_expr
722
723
static void delete_arguments(Context *ctx, MOJOSHADER_astArguments *args);
724
725
static void delete_expr(Context *ctx, MOJOSHADER_astExpression *_expr)
726
{
727
728
MOJOSHADER_astNode *expr = (MOJOSHADER_astNode *) _expr;
729
DELETE_AST_NODE(expr);
730
731
732
733
734
735
736
737
738
739
740
741
742
if (expr->ast.type == MOJOSHADER_AST_OP_CAST)
delete_expr(ctx, expr->cast.operand);
else if (expr->ast.type == MOJOSHADER_AST_OP_CONSTRUCTOR)
delete_arguments(ctx, expr->constructor.args);
else if (expr->ast.type == MOJOSHADER_AST_OP_DEREF_STRUCT)
delete_expr(ctx, expr->derefstruct.identifier);
else if (operator_is_unary(expr->ast.type))
delete_expr(ctx, expr->unary.operand);
743
else if (operator_is_binary(expr->ast.type))
744
{
745
746
delete_expr(ctx, expr->binary.left);
delete_expr(ctx, expr->binary.right);
747
} // else if
748
749
else if (operator_is_ternary(expr->ast.type))
750
{
751
752
753
delete_expr(ctx, expr->ternary.left);
delete_expr(ctx, expr->ternary.center);
delete_expr(ctx, expr->ternary.right);
754
} // else if
755
756
else if (expr->ast.type == MOJOSHADER_AST_OP_CALLFUNC)
757
{
758
delete_expr(ctx, (MOJOSHADER_astExpression*)expr->callfunc.identifier);
759
delete_arguments(ctx, expr->callfunc.args);
760
} // else if
761
762
763
// rest of operators don't have extra data to free.
764
765
766
Free(ctx, expr);
} // delete_expr
767
768
static MOJOSHADER_astArguments *new_argument(Context *ctx,
MOJOSHADER_astExpression *arg)
769
{
770
771
NEW_AST_NODE(retval, MOJOSHADER_astArguments, MOJOSHADER_AST_ARGUMENTS);
retval->argument = arg;
772
773
774
775
retval->next = NULL;
return retval;
} // new_argument
776
static void delete_arguments(Context *ctx, MOJOSHADER_astArguments *args)
777
778
779
780
781
782
783
{
DELETE_AST_NODE(args);
delete_arguments(ctx, args->next);
delete_expr(ctx, args->argument);
Free(ctx, args);
} // delete_arguments
784
785
static MOJOSHADER_astFunctionParameters *new_function_param(Context *ctx,
const MOJOSHADER_astInputModifier inputmod,
786
787
const MOJOSHADER_astDataType *dt,
const char *identifier, const char *semantic,
788
789
const MOJOSHADER_astInterpolationModifier interpmod,
MOJOSHADER_astExpression *initializer)
790
{
791
792
NEW_AST_NODE(retval, MOJOSHADER_astFunctionParameters,
MOJOSHADER_AST_FUNCTION_PARAMS);
793
retval->datatype = dt;
794
795
796
797
798
799
800
retval->input_modifier = inputmod;
retval->identifier = identifier;
retval->semantic = semantic;
retval->interpolation_modifier = interpmod;
retval->initializer = initializer;
retval->next = NULL;
return retval;
801
} // new_function_param
802
803
804
static void delete_function_params(Context *ctx,
MOJOSHADER_astFunctionParameters *params)
805
{
806
807
808
809
810
DELETE_AST_NODE(params);
delete_function_params(ctx, params->next);
delete_expr(ctx, params->initializer);
Free(ctx, params);
} // delete_function_params
811
812
static MOJOSHADER_astFunctionSignature *new_function_signature(Context *ctx,
813
const MOJOSHADER_astDataType *dt,
814
815
const char *identifier,
MOJOSHADER_astFunctionParameters *params)
816
{
817
818
NEW_AST_NODE(retval, MOJOSHADER_astFunctionSignature,
MOJOSHADER_AST_FUNCTION_SIGNATURE);
819
retval->datatype = dt;
820
retval->identifier = identifier;
821
retval->params = params;
822
retval->storage_class = MOJOSHADER_AST_FNSTORECLS_NONE;
823
824
825
826
retval->semantic = NULL;
return retval;
} // new_function_signature
827
828
static void delete_function_signature(Context *ctx,
MOJOSHADER_astFunctionSignature *sig)
829
830
{
DELETE_AST_NODE(sig);
831
delete_function_params(ctx, sig->params);
832
833
834
Free(ctx, sig);
} // delete_function_signature
835
836
837
static MOJOSHADER_astCompilationUnit *new_function(Context *ctx,
MOJOSHADER_astFunctionSignature *declaration,
MOJOSHADER_astStatement *definition)
838
{
839
840
NEW_AST_NODE(retval, MOJOSHADER_astCompilationUnitFunction,
MOJOSHADER_AST_COMPUNIT_FUNCTION);
841
842
843
retval->next = NULL;
retval->declaration = declaration;
retval->definition = definition;
844
return (MOJOSHADER_astCompilationUnit *) retval;
845
846
} // new_function
847
848
static void delete_function(Context *ctx,
MOJOSHADER_astCompilationUnitFunction *unitfn)
849
850
851
852
853
854
855
856
{
DELETE_AST_NODE(unitfn);
delete_compilation_unit(ctx, unitfn->next);
delete_function_signature(ctx, unitfn->declaration);
delete_statement(ctx, unitfn->definition);
Free(ctx, unitfn);
} // delete_function
857
858
859
static MOJOSHADER_astScalarOrArray *new_scalar_or_array(Context *ctx,
const char *ident, const int isvec,
MOJOSHADER_astExpression *dim)
860
{
861
862
NEW_AST_NODE(retval, MOJOSHADER_astScalarOrArray,
MOJOSHADER_AST_SCALAR_OR_ARRAY);
863
864
865
866
867
868
retval->identifier = ident;
retval->isarray = isvec;
retval->dimension = dim;
return retval;
} // new_scalar_or_array
869
static void delete_scalar_or_array(Context *ctx,MOJOSHADER_astScalarOrArray *s)
870
{
871
872
873
DELETE_AST_NODE(s);
delete_expr(ctx, s->dimension);
Free(ctx, s);
874
875
} // delete_scalar_or_array
876
static MOJOSHADER_astTypedef *new_typedef(Context *ctx, const int isconst,
877
const MOJOSHADER_astDataType *dt,
878
MOJOSHADER_astScalarOrArray *soa)
879
{
880
// we correct this datatype to the final version during semantic analysis.
881
NEW_AST_NODE(retval, MOJOSHADER_astTypedef, MOJOSHADER_AST_TYPEDEF);
882
retval->datatype = dt;
883
884
885
886
887
retval->isconst = isconst;
retval->details = soa;
return retval;
} // new_typedef
888
static void delete_typedef(Context *ctx, MOJOSHADER_astTypedef *td)
889
890
891
892
893
894
{
DELETE_AST_NODE(td);
delete_scalar_or_array(ctx, td->details);
Free(ctx, td);
} // delete_typedef
895
896
static MOJOSHADER_astPackOffset *new_pack_offset(Context *ctx,
const char *a, const char *b)
897
{
898
NEW_AST_NODE(retval, MOJOSHADER_astPackOffset, MOJOSHADER_AST_PACK_OFFSET);
899
900
901
902
903
retval->ident1 = a;
retval->ident2 = b;
return retval;
} // new_pack_offset
904
static void delete_pack_offset(Context *ctx, MOJOSHADER_astPackOffset *o)
905
906
907
908
909
{
DELETE_AST_NODE(o);
Free(ctx, o);
} // delete_pack_offset
910
911
static MOJOSHADER_astVariableLowLevel *new_variable_lowlevel(Context *ctx,
MOJOSHADER_astPackOffset *po,
912
913
const char *reg)
{
914
915
NEW_AST_NODE(retval, MOJOSHADER_astVariableLowLevel,
MOJOSHADER_AST_VARIABLE_LOWLEVEL);
916
917
918
919
920
retval->packoffset = po;
retval->register_name = reg;
return retval;
} // new_variable_lowlevel
921
922
static void delete_variable_lowlevel(Context *ctx,
MOJOSHADER_astVariableLowLevel *vll)
923
924
925
926
927
928
{
DELETE_AST_NODE(vll);
delete_pack_offset(ctx, vll->packoffset);
Free(ctx, vll);
} // delete_variable_lowlevel
929
static MOJOSHADER_astAnnotations *new_annotation(Context *ctx,
930
const MOJOSHADER_astDataType *dt,
931
MOJOSHADER_astExpression *initializer)
932
{
933
NEW_AST_NODE(retval, MOJOSHADER_astAnnotations, MOJOSHADER_AST_ANNOTATION);
934
retval->datatype = dt;
935
936
937
938
939
retval->initializer = initializer;
retval->next = NULL;
return retval;
} // new_annotation
940
static void delete_annotation(Context *ctx, MOJOSHADER_astAnnotations *annos)
941
{
942
943
944
945
DELETE_AST_NODE(annos);
delete_annotation(ctx, annos->next);
delete_expr(ctx, annos->initializer);
Free(ctx, annos);
946
947
} // delete_annotation
948
949
950
951
952
953
static MOJOSHADER_astVariableDeclaration *new_variable_declaration(
Context *ctx, MOJOSHADER_astScalarOrArray *soa,
const char *semantic,
MOJOSHADER_astAnnotations *annotations,
MOJOSHADER_astExpression *init,
MOJOSHADER_astVariableLowLevel *vll)
954
{
955
956
NEW_AST_NODE(retval, MOJOSHADER_astVariableDeclaration,
MOJOSHADER_AST_VARIABLE_DECLARATION);
957
retval->datatype = NULL;
958
959
960
961
962
963
964
965
966
967
968
retval->attributes = 0;
retval->anonymous_datatype = NULL;
retval->details = soa;
retval->semantic = semantic;
retval->annotations = annotations;
retval->initializer = init;
retval->lowlevel = vll;
retval->next = NULL;
return retval;
} // new_variable_declaration
969
970
static void delete_variable_declaration(Context *ctx,
MOJOSHADER_astVariableDeclaration *dcl)
971
972
973
974
975
976
977
978
979
980
{
DELETE_AST_NODE(dcl);
delete_variable_declaration(ctx, dcl->next);
delete_scalar_or_array(ctx, dcl->details);
delete_annotation(ctx, dcl->annotations);
delete_expr(ctx, dcl->initializer);
delete_variable_lowlevel(ctx, dcl->lowlevel);
Free(ctx, dcl);
} // delete_variable_declaration
981
982
static MOJOSHADER_astCompilationUnit *new_global_variable(Context *ctx,
MOJOSHADER_astVariableDeclaration *decl)
983
{
984
985
NEW_AST_NODE(retval, MOJOSHADER_astCompilationUnitVariable,
MOJOSHADER_AST_COMPUNIT_VARIABLE);
986
987
retval->next = NULL;
retval->declaration = decl;
988
return (MOJOSHADER_astCompilationUnit *) retval;
989
990
} // new_global_variable
991
992
static void delete_global_variable(Context *ctx,
MOJOSHADER_astCompilationUnitVariable *var)
993
994
995
996
997
998
999
{
DELETE_AST_NODE(var);
delete_compilation_unit(ctx, var->next);
delete_variable_declaration(ctx, var->declaration);
Free(ctx, var);
} // delete_global_variable
1000
static MOJOSHADER_astCompilationUnit *new_global_typedef(Context *ctx,