Skip to content

Latest commit

 

History

History
3443 lines (2976 loc) · 123 KB

mojoshader_compiler.c

File metadata and controls

3443 lines (2976 loc) · 123 KB
 
Feb 19, 2010
Feb 19, 2010
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.
*/
Oct 19, 2010
Oct 19, 2010
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.
Feb 28, 2009
Feb 28, 2009
14
15
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"
Aug 23, 2009
Aug 23, 2009
17
18
19
20
#if DEBUG_COMPILER_PARSER
#define LEMON_SUPPORT_TRACING 1
#endif
Oct 13, 2010
Oct 13, 2010
21
22
// !!! FIXME: I'd like to lose this. It's really inefficient. Just keep a
// !!! FIXME: (tail) on these list structures instead?
Feb 23, 2010
Feb 23, 2010
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; \
} \
}
Oct 28, 2010
Oct 28, 2010
37
static inline int operator_is_unary(const MOJOSHADER_astNodeType op)
Aug 23, 2009
Aug 23, 2009
38
{
Oct 28, 2010
Oct 28, 2010
39
40
return ( (op > MOJOSHADER_AST_OP_START_RANGE_UNARY) &&
(op < MOJOSHADER_AST_OP_END_RANGE_UNARY) );
Feb 9, 2010
Feb 9, 2010
41
42
} // operator_is_unary
Oct 28, 2010
Oct 28, 2010
43
static inline int operator_is_binary(const MOJOSHADER_astNodeType op)
Feb 9, 2010
Feb 9, 2010
44
{
Oct 28, 2010
Oct 28, 2010
45
46
return ( (op > MOJOSHADER_AST_OP_START_RANGE_BINARY) &&
(op < MOJOSHADER_AST_OP_END_RANGE_BINARY) );
Feb 9, 2010
Feb 9, 2010
47
48
} // operator_is_binary
Oct 28, 2010
Oct 28, 2010
49
static inline int operator_is_ternary(const MOJOSHADER_astNodeType op)
Feb 9, 2010
Feb 9, 2010
50
{
Oct 28, 2010
Oct 28, 2010
51
52
return ( (op > MOJOSHADER_AST_OP_START_RANGE_TERNARY) &&
(op < MOJOSHADER_AST_OP_END_RANGE_TERNARY) );
Feb 9, 2010
Feb 9, 2010
53
54
55
} // operator_is_ternary
Oct 28, 2010
Oct 28, 2010
56
typedef union TokenData
Feb 9, 2010
Feb 9, 2010
57
{
Oct 28, 2010
Oct 28, 2010
58
59
int64 i64;
double dbl;
Feb 9, 2010
Feb 9, 2010
60
const char *string;
Dec 6, 2010
Dec 6, 2010
61
const MOJOSHADER_astDataType *datatype;
Oct 28, 2010
Oct 28, 2010
62
} TokenData;
Feb 19, 2010
Feb 19, 2010
63
64
Oct 19, 2010
Oct 19, 2010
65
// This tracks data types and variables, and notes when they enter/leave scope.
Feb 22, 2010
Feb 22, 2010
66
Oct 19, 2010
Oct 19, 2010
67
typedef struct SymbolScope
Feb 22, 2010
Feb 22, 2010
68
69
{
const char *symbol;
Dec 6, 2010
Dec 6, 2010
70
const MOJOSHADER_astDataType *datatype;
Oct 19, 2010
Oct 19, 2010
71
72
struct SymbolScope *next;
} SymbolScope;
Feb 22, 2010
Feb 22, 2010
73
Oct 19, 2010
Oct 19, 2010
74
typedef struct SymbolMap
Feb 22, 2010
Feb 22, 2010
75
{
Oct 19, 2010
Oct 19, 2010
76
77
78
HashTable *hash;
SymbolScope *scope;
} SymbolMap;
Feb 22, 2010
Feb 22, 2010
79
80
Feb 19, 2010
Feb 19, 2010
81
82
83
84
85
86
87
88
89
90
// 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;
Nov 2, 2010
Nov 2, 2010
91
ErrorList *warnings;
Feb 24, 2010
Feb 24, 2010
92
StringCache *strcache;
Feb 22, 2010
Feb 22, 2010
93
94
const char *sourcefile; // current source file that we're parsing.
unsigned int sourceline; // current line in sourcefile that we're parsing.
Oct 19, 2010
Oct 19, 2010
95
96
SymbolMap usertypes;
SymbolMap variables;
Oct 28, 2010
Oct 28, 2010
97
MOJOSHADER_astNode *ast; // Abstract Syntax Tree
Nov 1, 2010
Nov 1, 2010
98
const char *source_profile;
Oct 19, 2010
Oct 19, 2010
99
Dec 6, 2010
Dec 6, 2010
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
// Cache intrinsic types for fast lookup and consistent pointer values.
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.
Feb 19, 2010
Feb 19, 2010
126
127
128
} Context;
Nov 4, 2010
Nov 4, 2010
129
130
131
132
// !!! 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 ...
Feb 19, 2010
Feb 19, 2010
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
// 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)
{
Nov 4, 2010
Nov 4, 2010
158
ctx->free(ptr, ctx->malloc_data);
Feb 19, 2010
Feb 19, 2010
159
160
} // Free
Nov 4, 2010
Nov 4, 2010
161
162
163
164
165
166
167
168
169
170
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
Nov 1, 2010
Nov 1, 2010
171
172
173
174
static void failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
static void failf(Context *ctx, const char *fmt, ...)
{
ctx->isfail = 1;
Nov 4, 2010
Nov 4, 2010
175
if (ctx->out_of_memory)
Nov 1, 2010
Nov 1, 2010
176
177
178
179
return;
va_list ap;
va_start(ap, fmt);
Nov 4, 2010
Nov 4, 2010
180
errorlist_add_va(ctx->errors, ctx->sourcefile, ctx->sourceline, fmt, ap);
Nov 1, 2010
Nov 1, 2010
181
182
183
184
va_end(ap);
} // failf
static inline void fail(Context *ctx, const char *reason)
Feb 21, 2010
Feb 21, 2010
185
{
Nov 1, 2010
Nov 1, 2010
186
failf(ctx, "%s", reason);
Feb 21, 2010
Feb 21, 2010
187
} // fail
Feb 19, 2010
Feb 19, 2010
188
Nov 4, 2010
Nov 4, 2010
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
Nov 1, 2010
Nov 1, 2010
206
207
208
209
210
static inline int isfail(const Context *ctx)
{
return ctx->isfail;
} // isfail
Feb 22, 2010
Feb 22, 2010
211
Oct 19, 2010
Oct 19, 2010
212
static void symbolmap_nuke(const void *k, const void *v, void *d) {/*no-op*/}
Feb 22, 2010
Feb 22, 2010
213
Oct 19, 2010
Oct 19, 2010
214
static int create_symbolmap(Context *ctx, SymbolMap *map)
Feb 22, 2010
Feb 22, 2010
215
{
Oct 19, 2010
Oct 19, 2010
216
// !!! FIXME: should compare string pointer, with string in cache.
Feb 22, 2010
Feb 22, 2010
217
map->scope = NULL;
Oct 19, 2010
Oct 19, 2010
218
map->hash = hash_create(ctx, hash_hash_string, hash_keymatch_string,
Nov 4, 2010
Nov 4, 2010
219
220
symbolmap_nuke, 1, MallocBridge, FreeBridge, ctx);
return (map->hash != NULL);
Oct 19, 2010
Oct 19, 2010
221
} // create_symbolmap
Feb 22, 2010
Feb 22, 2010
222
223
Oct 19, 2010
Oct 19, 2010
224
static void push_symbol(Context *ctx, SymbolMap *map,
Dec 6, 2010
Dec 6, 2010
225
const char *sym, const MOJOSHADER_astDataType *dt)
Oct 19, 2010
Oct 19, 2010
226
{
Oct 26, 2010
Oct 26, 2010
227
228
229
// !!! FIXME: decide if this symbol is defined, and if so, if it's in
// !!! FIXME: the current scope.
Oct 19, 2010
Oct 19, 2010
230
SymbolScope *item = (SymbolScope *) Malloc(ctx, sizeof (SymbolScope));
Feb 22, 2010
Feb 22, 2010
231
232
233
234
235
if (item == NULL)
return;
if (sym != NULL)
{
Dec 6, 2010
Dec 6, 2010
236
if (hash_insert(map->hash, sym, dt) == -1)
Feb 22, 2010
Feb 22, 2010
237
238
239
{
Free(ctx, item);
return;
Oct 26, 2010
Oct 26, 2010
240
} // if
Feb 22, 2010
Feb 22, 2010
241
242
243
} // if
item->symbol = sym; // cached strings, don't copy.
Dec 6, 2010
Dec 6, 2010
244
item->datatype = dt;
Feb 22, 2010
Feb 22, 2010
245
246
item->next = map->scope;
map->scope = item;
Oct 19, 2010
Oct 19, 2010
247
248
} // push_symbol
Dec 6, 2010
Dec 6, 2010
249
static void push_usertype(Context *ctx, const char *sym, const MOJOSHADER_astDataType *dt)
Oct 19, 2010
Oct 19, 2010
250
{
Dec 6, 2010
Dec 6, 2010
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
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
Oct 19, 2010
Oct 19, 2010
272
push_symbol(ctx, &ctx->usertypes, sym, dt);
Feb 22, 2010
Feb 22, 2010
273
274
} // push_usertype
Dec 6, 2010
Dec 6, 2010
275
static inline void push_variable(Context *ctx, const char *sym, const MOJOSHADER_astDataType *dt)
Oct 19, 2010
Oct 19, 2010
276
277
278
279
280
281
282
283
284
285
286
287
{
push_symbol(ctx, &ctx->variables, sym, dt);
} // push_variable
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)
Feb 22, 2010
Feb 22, 2010
288
{
Oct 19, 2010
Oct 19, 2010
289
SymbolScope *item = map->scope;
Feb 22, 2010
Feb 22, 2010
290
291
292
if (!item)
return;
if (item->symbol)
Oct 19, 2010
Oct 19, 2010
293
hash_remove(map->hash, item->symbol);
Feb 22, 2010
Feb 22, 2010
294
295
map->scope = item->next;
Free(ctx, item);
Oct 19, 2010
Oct 19, 2010
296
} // pop_symbol
Feb 22, 2010
Feb 22, 2010
297
Oct 19, 2010
Oct 19, 2010
298
static void pop_symbol_scope(Context *ctx, SymbolMap *map)
Feb 22, 2010
Feb 22, 2010
299
300
{
while ((map->scope) && (map->scope->symbol))
Oct 19, 2010
Oct 19, 2010
301
pop_symbol(ctx, map);
Feb 22, 2010
Feb 22, 2010
302
303
304
assert(map->scope != NULL);
assert(map->scope->symbol == NULL);
Oct 19, 2010
Oct 19, 2010
305
306
307
308
309
310
311
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);
Feb 22, 2010
Feb 22, 2010
312
313
} // push_scope
Dec 6, 2010
Dec 6, 2010
314
static const MOJOSHADER_astDataType *find_symbol(Context *ctx, SymbolMap *map, const char *sym)
Oct 26, 2010
Oct 26, 2010
315
316
317
{
const void *value = NULL;
hash_find(map->hash, sym, &value);
Dec 6, 2010
Dec 6, 2010
318
return (const MOJOSHADER_astDataType *) value;
Oct 26, 2010
Oct 26, 2010
319
320
} // find_symbol
Dec 6, 2010
Dec 6, 2010
321
static inline const MOJOSHADER_astDataType *find_usertype(Context *ctx, const char *sym)
Oct 26, 2010
Oct 26, 2010
322
323
324
325
{
return find_symbol(ctx, &ctx->usertypes, sym);
} // find_usertype
Dec 6, 2010
Dec 6, 2010
326
static inline const MOJOSHADER_astDataType *find_variable(Context *ctx, const char *sym)
Oct 26, 2010
Oct 26, 2010
327
328
329
330
{
return find_symbol(ctx, &ctx->variables, sym);
} // find_variable
Oct 19, 2010
Oct 19, 2010
331
static void destroy_symbolmap(Context *ctx, SymbolMap *map)
Feb 22, 2010
Feb 22, 2010
332
333
{
while (map->scope)
Nov 2, 2010
Nov 2, 2010
334
pop_symbol(ctx, map);
Oct 19, 2010
Oct 19, 2010
335
336
hash_destroy(map->hash);
} // destroy_symbolmap
Feb 22, 2010
Feb 22, 2010
337
338
Dec 6, 2010
Dec 6, 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
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
Nov 4, 2010
Nov 4, 2010
397
398
// !!! FIXME: new_* and delete_* should take an allocator, not a context.
Feb 19, 2010
Feb 19, 2010
399
400
401
402
403
// 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.
Feb 22, 2010
Feb 22, 2010
404
#define NEW_AST_NODE(retval, cls, typ) \
Mar 3, 2010
Mar 3, 2010
405
cls *retval = (cls *) Malloc(ctx, sizeof (cls)); \
Feb 22, 2010
Feb 22, 2010
406
407
408
409
410
411
412
413
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 { \
Feb 19, 2010
Feb 19, 2010
414
415
416
if (!cls) return; \
} while (0)
Dec 6, 2010
Dec 6, 2010
417
Oct 28, 2010
Oct 28, 2010
418
419
static void delete_compilation_unit(Context*, MOJOSHADER_astCompilationUnit*);
static void delete_statement(Context *ctx, MOJOSHADER_astStatement *stmt);
Feb 19, 2010
Feb 19, 2010
420
Oct 28, 2010
Oct 28, 2010
421
422
423
static MOJOSHADER_astExpression *new_callfunc_expr(Context *ctx,
MOJOSHADER_astExpression *identifier,
MOJOSHADER_astArguments *args)
Oct 25, 2010
Oct 25, 2010
424
{
Oct 28, 2010
Oct 28, 2010
425
426
NEW_AST_NODE(retval, MOJOSHADER_astExpressionCallFunction,
MOJOSHADER_AST_OP_CALLFUNC);
Oct 25, 2010
Oct 25, 2010
427
428
retval->identifier = identifier;
retval->args = args;
Oct 28, 2010
Oct 28, 2010
429
return (MOJOSHADER_astExpression *) retval;
Oct 25, 2010
Oct 25, 2010
430
431
} // new_callfunc_expr
Oct 28, 2010
Oct 28, 2010
432
static MOJOSHADER_astExpression *new_constructor_expr(Context *ctx,
Dec 6, 2010
Dec 6, 2010
433
434
const MOJOSHADER_astDataType *dt,
MOJOSHADER_astArguments *args)
Feb 20, 2010
Feb 20, 2010
435
{
Oct 28, 2010
Oct 28, 2010
436
437
NEW_AST_NODE(retval, MOJOSHADER_astExpressionConstructor,
MOJOSHADER_AST_OP_CONSTRUCTOR);
Dec 6, 2010
Dec 6, 2010
438
retval->datatype = dt;
Feb 20, 2010
Feb 20, 2010
439
retval->args = args;
Oct 28, 2010
Oct 28, 2010
440
return (MOJOSHADER_astExpression *) retval;
Feb 20, 2010
Feb 20, 2010
441
442
} // new_constructor_expr
Oct 28, 2010
Oct 28, 2010
443
static MOJOSHADER_astExpression *new_cast_expr(Context *ctx,
Dec 6, 2010
Dec 6, 2010
444
const MOJOSHADER_astDataType *dt,
Oct 28, 2010
Oct 28, 2010
445
MOJOSHADER_astExpression *operand)
Feb 20, 2010
Feb 20, 2010
446
{
Oct 28, 2010
Oct 28, 2010
447
NEW_AST_NODE(retval, MOJOSHADER_astExpressionCast, MOJOSHADER_AST_OP_CAST);
Dec 6, 2010
Dec 6, 2010
448
retval->datatype = dt;
Feb 20, 2010
Feb 20, 2010
449
retval->operand = operand;
Oct 28, 2010
Oct 28, 2010
450
return (MOJOSHADER_astExpression *) retval;
Feb 20, 2010
Feb 20, 2010
451
452
} // new_cast_expr
Oct 28, 2010
Oct 28, 2010
453
454
455
static MOJOSHADER_astExpression *new_unary_expr(Context *ctx,
const MOJOSHADER_astNodeType op,
MOJOSHADER_astExpression *operand)
Feb 9, 2010
Feb 9, 2010
456
{
Oct 28, 2010
Oct 28, 2010
457
NEW_AST_NODE(retval, MOJOSHADER_astExpressionUnary, op);
Feb 9, 2010
Feb 9, 2010
458
459
assert(operator_is_unary(op));
retval->operand = operand;
Oct 28, 2010
Oct 28, 2010
460
return (MOJOSHADER_astExpression *) retval;
Feb 9, 2010
Feb 9, 2010
461
462
} // new_unary_expr
Oct 28, 2010
Oct 28, 2010
463
464
465
466
static MOJOSHADER_astExpression *new_binary_expr(Context *ctx,
const MOJOSHADER_astNodeType op,
MOJOSHADER_astExpression *left,
MOJOSHADER_astExpression *right)
Feb 9, 2010
Feb 9, 2010
467
{
Oct 28, 2010
Oct 28, 2010
468
NEW_AST_NODE(retval, MOJOSHADER_astExpressionBinary, op);
Feb 9, 2010
Feb 9, 2010
469
470
471
assert(operator_is_binary(op));
retval->left = left;
retval->right = right;
Oct 28, 2010
Oct 28, 2010
472
return (MOJOSHADER_astExpression *) retval;
Feb 9, 2010
Feb 9, 2010
473
474
} // new_binary_expr
Oct 28, 2010
Oct 28, 2010
475
476
477
478
479
static MOJOSHADER_astExpression *new_ternary_expr(Context *ctx,
const MOJOSHADER_astNodeType op,
MOJOSHADER_astExpression *left,
MOJOSHADER_astExpression *center,
MOJOSHADER_astExpression *right)
Feb 9, 2010
Feb 9, 2010
480
{
Oct 28, 2010
Oct 28, 2010
481
NEW_AST_NODE(retval, MOJOSHADER_astExpressionTernary, op);
Feb 9, 2010
Feb 9, 2010
482
assert(operator_is_ternary(op));
Dec 6, 2010
Dec 6, 2010
483
484
assert(op == MOJOSHADER_AST_OP_CONDITIONAL);
retval->datatype = &ctx->dt_bool;
Feb 9, 2010
Feb 9, 2010
485
486
487
retval->left = left;
retval->center = center;
retval->right = right;
Oct 28, 2010
Oct 28, 2010
488
return (MOJOSHADER_astExpression *) retval;
Feb 9, 2010
Feb 9, 2010
489
490
} // new_ternary_expr
Oct 28, 2010
Oct 28, 2010
491
492
493
static MOJOSHADER_astExpression *new_deref_struct_expr(Context *ctx,
MOJOSHADER_astExpression *identifier,
const char *member)
Oct 26, 2010
Oct 26, 2010
494
{
Oct 28, 2010
Oct 28, 2010
495
496
NEW_AST_NODE(retval, MOJOSHADER_astExpressionDerefStruct,
MOJOSHADER_AST_OP_DEREF_STRUCT);
Oct 26, 2010
Oct 26, 2010
497
498
retval->identifier = identifier;
retval->member = member; // cached; don't copy string.
Dec 6, 2010
Dec 6, 2010
499
retval->isswizzle = 0; // may change during semantic analysis.
Oct 28, 2010
Oct 28, 2010
500
return (MOJOSHADER_astExpression *) retval;
Oct 26, 2010
Oct 26, 2010
501
502
} // new_deref_struct_expr
Oct 28, 2010
Oct 28, 2010
503
504
static MOJOSHADER_astExpression *new_identifier_expr(Context *ctx,
const char *string)
Feb 9, 2010
Feb 9, 2010
505
{
Oct 28, 2010
Oct 28, 2010
506
507
NEW_AST_NODE(retval, MOJOSHADER_astExpressionIdentifier,
MOJOSHADER_AST_OP_IDENTIFIER);
Feb 9, 2010
Feb 9, 2010
508
retval->identifier = string; // cached; don't copy string.
Oct 28, 2010
Oct 28, 2010
509
return (MOJOSHADER_astExpression *) retval;
Feb 9, 2010
Feb 9, 2010
510
511
} // new_identifier_expr
Oct 28, 2010
Oct 28, 2010
512
513
static MOJOSHADER_astExpression *new_literal_int_expr(Context *ctx,
const int value)
Feb 9, 2010
Feb 9, 2010
514
{
Oct 28, 2010
Oct 28, 2010
515
516
NEW_AST_NODE(retval, MOJOSHADER_astExpressionIntLiteral,
MOJOSHADER_AST_OP_INT_LITERAL);
Dec 6, 2010
Dec 6, 2010
517
retval->datatype = &ctx->dt_int;
Feb 9, 2010
Feb 9, 2010
518
retval->value = value;
Oct 28, 2010
Oct 28, 2010
519
return (MOJOSHADER_astExpression *) retval;
Feb 9, 2010
Feb 9, 2010
520
521
} // new_literal_int_expr
Oct 28, 2010
Oct 28, 2010
522
523
static MOJOSHADER_astExpression *new_literal_float_expr(Context *ctx,
const double dbl)
Feb 9, 2010
Feb 9, 2010
524
{
Oct 28, 2010
Oct 28, 2010
525
526
NEW_AST_NODE(retval, MOJOSHADER_astExpressionFloatLiteral,
MOJOSHADER_AST_OP_FLOAT_LITERAL);
Dec 6, 2010
Dec 6, 2010
527
retval->datatype = &ctx->dt_float;
Feb 9, 2010
Feb 9, 2010
528
retval->value = dbl;
Oct 28, 2010
Oct 28, 2010
529
return (MOJOSHADER_astExpression *) retval;
Feb 9, 2010
Feb 9, 2010
530
531
} // new_literal_float_expr
Oct 28, 2010
Oct 28, 2010
532
533
static MOJOSHADER_astExpression *new_literal_string_expr(Context *ctx,
const char *string)
Feb 9, 2010
Feb 9, 2010
534
{
Oct 28, 2010
Oct 28, 2010
535
536
NEW_AST_NODE(retval, MOJOSHADER_astExpressionStringLiteral,
MOJOSHADER_AST_OP_STRING_LITERAL);
Dec 6, 2010
Dec 6, 2010
537
retval->datatype = &ctx->dt_string;
Feb 9, 2010
Feb 9, 2010
538
retval->string = string; // cached; don't copy string.
Oct 28, 2010
Oct 28, 2010
539
return (MOJOSHADER_astExpression *) retval;
Oct 20, 2010
Oct 20, 2010
540
541
} // new_literal_string_expr
Oct 28, 2010
Oct 28, 2010
542
543
static MOJOSHADER_astExpression *new_literal_boolean_expr(Context *ctx,
const int value)
Oct 20, 2010
Oct 20, 2010
544
{
Oct 28, 2010
Oct 28, 2010
545
546
NEW_AST_NODE(retval, MOJOSHADER_astExpressionBooleanLiteral,
MOJOSHADER_AST_OP_BOOLEAN_LITERAL);
Dec 6, 2010
Dec 6, 2010
547
retval->datatype = &ctx->dt_bool;
Oct 20, 2010
Oct 20, 2010
548
retval->value = value;
Oct 28, 2010
Oct 28, 2010
549
return (MOJOSHADER_astExpression *) retval;
Oct 20, 2010
Oct 20, 2010
550
} // new_literal_boolean_expr
Feb 9, 2010
Feb 9, 2010
551
Oct 28, 2010
Oct 28, 2010
552
static void delete_arguments(Context *ctx, MOJOSHADER_astArguments *args);
Oct 25, 2010
Oct 25, 2010
553
Oct 28, 2010
Oct 28, 2010
554
static void delete_expr(Context *ctx, MOJOSHADER_astExpression *_expr)
Feb 19, 2010
Feb 19, 2010
555
{
Oct 28, 2010
Oct 28, 2010
556
557
MOJOSHADER_astNode *expr = (MOJOSHADER_astNode *) _expr;
Feb 19, 2010
Feb 19, 2010
558
DELETE_AST_NODE(expr);
Oct 28, 2010
Oct 28, 2010
559
560
561
562
563
564
565
566
567
568
569
570
571
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);
Feb 22, 2010
Feb 22, 2010
572
else if (operator_is_binary(expr->ast.type))
Feb 19, 2010
Feb 19, 2010
573
{
Oct 28, 2010
Oct 28, 2010
574
575
delete_expr(ctx, expr->binary.left);
delete_expr(ctx, expr->binary.right);
Feb 19, 2010
Feb 19, 2010
576
} // else if
Oct 28, 2010
Oct 28, 2010
577
Feb 22, 2010
Feb 22, 2010
578
else if (operator_is_ternary(expr->ast.type))
Feb 19, 2010
Feb 19, 2010
579
{
Oct 28, 2010
Oct 28, 2010
580
581
582
delete_expr(ctx, expr->ternary.left);
delete_expr(ctx, expr->ternary.center);
delete_expr(ctx, expr->ternary.right);
Oct 26, 2010
Oct 26, 2010
583
} // else if
Oct 28, 2010
Oct 28, 2010
584
585
else if (expr->ast.type == MOJOSHADER_AST_OP_CALLFUNC)
Oct 25, 2010
Oct 25, 2010
586
{
Oct 28, 2010
Oct 28, 2010
587
588
delete_expr(ctx, expr->callfunc.identifier);
delete_arguments(ctx, expr->callfunc.args);
Feb 20, 2010
Feb 20, 2010
589
} // else if
Feb 22, 2010
Feb 22, 2010
590
591
592
// rest of operators don't have extra data to free.
Feb 19, 2010
Feb 19, 2010
593
594
595
Free(ctx, expr);
} // delete_expr
Oct 28, 2010
Oct 28, 2010
596
597
static MOJOSHADER_astArguments *new_argument(Context *ctx,
MOJOSHADER_astExpression *arg)
Oct 25, 2010
Oct 25, 2010
598
{
Oct 28, 2010
Oct 28, 2010
599
600
NEW_AST_NODE(retval, MOJOSHADER_astArguments, MOJOSHADER_AST_ARGUMENTS);
retval->argument = arg;
Oct 25, 2010
Oct 25, 2010
601
602
603
604
retval->next = NULL;
return retval;
} // new_argument
Oct 28, 2010
Oct 28, 2010
605
static void delete_arguments(Context *ctx, MOJOSHADER_astArguments *args)
Oct 25, 2010
Oct 25, 2010
606
607
608
609
610
611
612
{
DELETE_AST_NODE(args);
delete_arguments(ctx, args->next);
delete_expr(ctx, args->argument);
Free(ctx, args);
} // delete_arguments
Oct 28, 2010
Oct 28, 2010
613
614
static MOJOSHADER_astFunctionParameters *new_function_param(Context *ctx,
const MOJOSHADER_astInputModifier inputmod,
Dec 6, 2010
Dec 6, 2010
615
616
const MOJOSHADER_astDataType *dt,
const char *identifier, const char *semantic,
Oct 28, 2010
Oct 28, 2010
617
618
const MOJOSHADER_astInterpolationModifier interpmod,
MOJOSHADER_astExpression *initializer)
Feb 19, 2010
Feb 19, 2010
619
{
Oct 28, 2010
Oct 28, 2010
620
621
NEW_AST_NODE(retval, MOJOSHADER_astFunctionParameters,
MOJOSHADER_AST_FUNCTION_PARAMS);
Dec 6, 2010
Dec 6, 2010
622
retval->datatype = dt;
Feb 19, 2010
Feb 19, 2010
623
624
625
626
627
628
629
retval->input_modifier = inputmod;
retval->identifier = identifier;
retval->semantic = semantic;
retval->interpolation_modifier = interpmod;
retval->initializer = initializer;
retval->next = NULL;
return retval;
Oct 25, 2010
Oct 25, 2010
630
} // new_function_param
Feb 19, 2010
Feb 19, 2010
631
Oct 28, 2010
Oct 28, 2010
632
633
static void delete_function_params(Context *ctx,
MOJOSHADER_astFunctionParameters *params)
Feb 19, 2010
Feb 19, 2010
634
{
Oct 25, 2010
Oct 25, 2010
635
636
637
638
639
DELETE_AST_NODE(params);
delete_function_params(ctx, params->next);
delete_expr(ctx, params->initializer);
Free(ctx, params);
} // delete_function_params
Feb 19, 2010
Feb 19, 2010
640
Oct 28, 2010
Oct 28, 2010
641
static MOJOSHADER_astFunctionSignature *new_function_signature(Context *ctx,
Dec 6, 2010
Dec 6, 2010
642
const MOJOSHADER_astDataType *dt,
Oct 28, 2010
Oct 28, 2010
643
644
const char *identifier,
MOJOSHADER_astFunctionParameters *params)
Feb 19, 2010
Feb 19, 2010
645
{
Oct 28, 2010
Oct 28, 2010
646
647
NEW_AST_NODE(retval, MOJOSHADER_astFunctionSignature,
MOJOSHADER_AST_FUNCTION_SIGNATURE);
Dec 6, 2010
Dec 6, 2010
648
retval->datatype = dt;
Feb 19, 2010
Feb 19, 2010
649
retval->identifier = identifier;
Oct 25, 2010
Oct 25, 2010
650
retval->params = params;
Oct 28, 2010
Oct 28, 2010
651
retval->storage_class = MOJOSHADER_AST_FNSTORECLS_NONE;
Feb 19, 2010
Feb 19, 2010
652
653
654
655
retval->semantic = NULL;
return retval;
} // new_function_signature
Oct 28, 2010
Oct 28, 2010
656
657
static void delete_function_signature(Context *ctx,
MOJOSHADER_astFunctionSignature *sig)
Feb 19, 2010
Feb 19, 2010
658
659
{
DELETE_AST_NODE(sig);
Oct 25, 2010
Oct 25, 2010
660
delete_function_params(ctx, sig->params);
Feb 19, 2010
Feb 19, 2010
661
662
663
Free(ctx, sig);
} // delete_function_signature
Oct 28, 2010
Oct 28, 2010
664
665
666
static MOJOSHADER_astCompilationUnit *new_function(Context *ctx,
MOJOSHADER_astFunctionSignature *declaration,
MOJOSHADER_astStatement *definition)
Feb 19, 2010
Feb 19, 2010
667
{
Oct 28, 2010
Oct 28, 2010
668
669
NEW_AST_NODE(retval, MOJOSHADER_astCompilationUnitFunction,
MOJOSHADER_AST_COMPUNIT_FUNCTION);
Feb 19, 2010
Feb 19, 2010
670
671
672
retval->next = NULL;
retval->declaration = declaration;
retval->definition = definition;
Oct 28, 2010
Oct 28, 2010
673
return (MOJOSHADER_astCompilationUnit *) retval;
Feb 19, 2010
Feb 19, 2010
674
675
} // new_function
Oct 28, 2010
Oct 28, 2010
676
677
static void delete_function(Context *ctx,
MOJOSHADER_astCompilationUnitFunction *unitfn)
Feb 19, 2010
Feb 19, 2010
678
679
680
681
682
683
684
685
{
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
Oct 28, 2010
Oct 28, 2010
686
687
688
static MOJOSHADER_astScalarOrArray *new_scalar_or_array(Context *ctx,
const char *ident, const int isvec,
MOJOSHADER_astExpression *dim)
Feb 19, 2010
Feb 19, 2010
689
{
Oct 28, 2010
Oct 28, 2010
690
691
NEW_AST_NODE(retval, MOJOSHADER_astScalarOrArray,
MOJOSHADER_AST_SCALAR_OR_ARRAY);
Feb 19, 2010
Feb 19, 2010
692
693
694
695
696
697
retval->identifier = ident;
retval->isarray = isvec;
retval->dimension = dim;
return retval;
} // new_scalar_or_array
Oct 28, 2010
Oct 28, 2010
698
static void delete_scalar_or_array(Context *ctx,MOJOSHADER_astScalarOrArray *s)
Feb 19, 2010
Feb 19, 2010
699
{
Oct 28, 2010
Oct 28, 2010
700
701
702
DELETE_AST_NODE(s);
delete_expr(ctx, s->dimension);
Free(ctx, s);
Feb 19, 2010
Feb 19, 2010
703
704
} // delete_scalar_or_array
Oct 28, 2010
Oct 28, 2010
705
static MOJOSHADER_astTypedef *new_typedef(Context *ctx, const int isconst,
Dec 6, 2010
Dec 6, 2010
706
const MOJOSHADER_astDataType *dt,
Oct 28, 2010
Oct 28, 2010
707
MOJOSHADER_astScalarOrArray *soa)
Feb 19, 2010
Feb 19, 2010
708
{
Dec 6, 2010
Dec 6, 2010
709
// we correct this datatype to the final version during semantic analysis.
Oct 28, 2010
Oct 28, 2010
710
NEW_AST_NODE(retval, MOJOSHADER_astTypedef, MOJOSHADER_AST_TYPEDEF);
Dec 6, 2010
Dec 6, 2010
711
retval->datatype = dt;
Feb 19, 2010
Feb 19, 2010
712
713
714
715
716
retval->isconst = isconst;
retval->details = soa;
return retval;
} // new_typedef
Oct 28, 2010
Oct 28, 2010
717
static void delete_typedef(Context *ctx, MOJOSHADER_astTypedef *td)
Feb 19, 2010
Feb 19, 2010
718
719
720
721
722
723
{
DELETE_AST_NODE(td);
delete_scalar_or_array(ctx, td->details);
Free(ctx, td);
} // delete_typedef
Oct 28, 2010
Oct 28, 2010
724
725
static MOJOSHADER_astPackOffset *new_pack_offset(Context *ctx,
const char *a, const char *b)
Feb 19, 2010
Feb 19, 2010
726
{
Oct 28, 2010
Oct 28, 2010
727
NEW_AST_NODE(retval, MOJOSHADER_astPackOffset, MOJOSHADER_AST_PACK_OFFSET);
Feb 19, 2010
Feb 19, 2010
728
729
730
731
732
retval->ident1 = a;
retval->ident2 = b;
return retval;
} // new_pack_offset
Oct 28, 2010
Oct 28, 2010
733
static void delete_pack_offset(Context *ctx, MOJOSHADER_astPackOffset *o)
Feb 19, 2010
Feb 19, 2010
734
735
736
737
738
{
DELETE_AST_NODE(o);
Free(ctx, o);
} // delete_pack_offset
Oct 28, 2010
Oct 28, 2010
739
740
static MOJOSHADER_astVariableLowLevel *new_variable_lowlevel(Context *ctx,
MOJOSHADER_astPackOffset *po,
Feb 19, 2010
Feb 19, 2010
741
742
const char *reg)
{
Oct 28, 2010
Oct 28, 2010
743
744
NEW_AST_NODE(retval, MOJOSHADER_astVariableLowLevel,
MOJOSHADER_AST_VARIABLE_LOWLEVEL);
Feb 19, 2010
Feb 19, 2010
745
746
747
748
749
retval->packoffset = po;
retval->register_name = reg;
return retval;
} // new_variable_lowlevel
Oct 28, 2010
Oct 28, 2010
750
751
static void delete_variable_lowlevel(Context *ctx,
MOJOSHADER_astVariableLowLevel *vll)
Feb 19, 2010
Feb 19, 2010
752
753
754
755
756
757
{
DELETE_AST_NODE(vll);
delete_pack_offset(ctx, vll->packoffset);
Free(ctx, vll);
} // delete_variable_lowlevel
Oct 28, 2010
Oct 28, 2010
758
static MOJOSHADER_astAnnotations *new_annotation(Context *ctx,
Dec 6, 2010
Dec 6, 2010
759
const MOJOSHADER_astDataType *dt,
Oct 28, 2010
Oct 28, 2010
760
MOJOSHADER_astExpression *initializer)
Feb 19, 2010
Feb 19, 2010
761
{
Oct 28, 2010
Oct 28, 2010
762
NEW_AST_NODE(retval, MOJOSHADER_astAnnotations, MOJOSHADER_AST_ANNOTATION);
Dec 6, 2010
Dec 6, 2010
763
retval->datatype = dt;
Feb 19, 2010
Feb 19, 2010
764
765
766
767
768
retval->initializer = initializer;
retval->next = NULL;
return retval;
} // new_annotation
Oct 28, 2010
Oct 28, 2010
769
static void delete_annotation(Context *ctx, MOJOSHADER_astAnnotations *annos)
Feb 19, 2010
Feb 19, 2010
770
{
Oct 28, 2010
Oct 28, 2010
771
772
773
774
DELETE_AST_NODE(annos);
delete_annotation(ctx, annos->next);
delete_expr(ctx, annos->initializer);
Free(ctx, annos);
Feb 19, 2010
Feb 19, 2010
775
776
} // delete_annotation
Oct 28, 2010
Oct 28, 2010
777
778
779
780
781
782
static MOJOSHADER_astVariableDeclaration *new_variable_declaration(
Context *ctx, MOJOSHADER_astScalarOrArray *soa,
const char *semantic,
MOJOSHADER_astAnnotations *annotations,
MOJOSHADER_astExpression *init,
MOJOSHADER_astVariableLowLevel *vll)
Feb 19, 2010
Feb 19, 2010
783
{
Oct 28, 2010
Oct 28, 2010
784
785
NEW_AST_NODE(retval, MOJOSHADER_astVariableDeclaration,
MOJOSHADER_AST_VARIABLE_DECLARATION);
Feb 19, 2010
Feb 19, 2010
786
787
788
789
790
791
792
793
794
795
796
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
Oct 28, 2010
Oct 28, 2010
797
798
static void delete_variable_declaration(Context *ctx,
MOJOSHADER_astVariableDeclaration *dcl)
Feb 19, 2010
Feb 19, 2010
799
800
801
802
803
804
805
806
807
808
{
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
Oct 28, 2010
Oct 28, 2010
809
810
static MOJOSHADER_astCompilationUnit *new_global_variable(Context *ctx,
MOJOSHADER_astVariableDeclaration *decl)
Feb 19, 2010
Feb 19, 2010
811
{
Oct 28, 2010
Oct 28, 2010
812
813
NEW_AST_NODE(retval, MOJOSHADER_astCompilationUnitVariable,
MOJOSHADER_AST_COMPUNIT_VARIABLE);
Feb 19, 2010
Feb 19, 2010
814
815
retval->next = NULL;
retval->declaration = decl;
Oct 28, 2010
Oct 28, 2010
816
return (MOJOSHADER_astCompilationUnit *) retval;
Feb 19, 2010
Feb 19, 2010
817
818
} // new_global_variable
Oct 28, 2010
Oct 28, 2010
819
820
static void delete_global_variable(Context *ctx,
MOJOSHADER_astCompilationUnitVariable *var)
Feb 19, 2010
Feb 19, 2010
821
822
823
824
825
826
827
{
DELETE_AST_NODE(var);
delete_compilation_unit(ctx, var->next);
delete_variable_declaration(ctx, var->declaration);
Free(ctx, var);
} // delete_global_variable
Oct 28, 2010
Oct 28, 2010
828
829
static MOJOSHADER_astCompilationUnit *new_global_typedef(Context *ctx,
MOJOSHADER_astTypedef *td)
Feb 19, 2010
Feb 19, 2010
830
{
Oct 28, 2010
Oct 28, 2010
831
832
NEW_AST_NODE(retval, MOJOSHADER_astCompilationUnitTypedef,
MOJOSHADER_AST_COMPUNIT_TYPEDEF);
Feb 19, 2010
Feb 19, 2010
833
834
retval->next = NULL;
retval->type_info = td;
Oct 28, 2010
Oct 28, 2010
835
return (MOJOSHADER_astCompilationUnit *) retval;
Feb 19, 2010
Feb 19, 2010
836
837
} // new_global_typedef
Oct 28, 2010
Oct 28, 2010
838
839
static void delete_global_typedef(Context *ctx,
MOJOSHADER_astCompilationUnitTypedef *unit)
Feb 19, 2010
Feb 19, 2010
840
841
842
843
844
845
846
{
DELETE_AST_NODE(unit);
delete_compilation_unit(ctx, unit->next);
delete_typedef(ctx, unit->type_info);
Free(ctx, unit);
} // delete_global_typedef
Oct 28, 2010
Oct 28, 2010
847
848
849
static MOJOSHADER_astStructMembers *new_struct_member(Context *ctx,
MOJOSHADER_astScalarOrArray *soa,
const char *semantic)
Feb 19, 2010
Feb 19, 2010
850
{
Oct 28, 2010
Oct 28, 2010
851
852
NEW_AST_NODE(retval, MOJOSHADER_astStructMembers,
MOJOSHADER_AST_STRUCT_MEMBER);
Feb 19, 2010
Feb 19, 2010
853
854
retval->semantic = semantic;
retval->details = soa;
Oct 28, 2010
Oct 28, 2010
855
retval->interpolation_mod = MOJOSHADER_AST_INTERPMOD_NONE;
Feb 19, 2010
Feb 19, 2010
856
857
858
859
retval->next = NULL;
return retval;
} // new_struct_member
Oct 28, 2010
Oct 28, 2010
860
861
static void delete_struct_member(Context *ctx,
MOJOSHADER_astStructMembers *member)
Feb 19, 2010
Feb 19, 2010
862
863
864
865
866
867
868
{
DELETE_AST_NODE(member);
delete_struct_member(ctx, member->next);
delete_scalar_or_array(ctx, member->details);
Free(ctx, member);
} // delete_struct_member
Oct 28, 2010
Oct 28, 2010
869
870
871
static MOJOSHADER_astStructDeclaration *new_struct_declaration(Context *ctx,
const char *name,
MOJOSHADER_astStructMembers *members)
Feb 19, 2010
Feb 19, 2010
872
{
Oct 28, 2010
Oct 28, 2010
873
874
NEW_AST_NODE(retval, MOJOSHADER_astStructDeclaration,
MOJOSHADER_AST_STRUCT_DECLARATION);
Dec 6, 2010
Dec 6, 2010
875
retval->datatype = NULL;
Feb 19, 2010
Feb 19, 2010
876
877
878
879
880
retval->name = name;
retval->members = members;
return retval;
} // new_struct_declaration
Oct 28, 2010
Oct 28, 2010
881
882
static void delete_struct_declaration(Context *ctx,
MOJOSHADER_astStructDeclaration *decl)
Feb 19, 2010
Feb 19, 2010
883
884
885
886
887
888
{
DELETE_AST_NODE(decl);
delete_struct_member(ctx, decl->members);
Free(ctx, decl);
} // delete_struct_declaration
Oct 28, 2010
Oct 28, 2010
889
890
static MOJOSHADER_astCompilationUnit *new_global_struct(Context *ctx,
MOJOSHADER_astStructDeclaration *sd)
Feb 19, 2010
Feb 19, 2010
891
{
Oct 28, 2010
Oct 28, 2010
892
893
NEW_AST_NODE(retval, MOJOSHADER_astCompilationUnitStruct,
MOJOSHADER_AST_COMPUNIT_STRUCT);
Feb 19, 2010
Feb 19, 2010
894
895
retval->next = NULL;
retval->struct_info = sd;
Oct 28, 2010
Oct 28, 2010
896
return (MOJOSHADER_astCompilationUnit *) retval;
Feb 19, 2010
Feb 19, 2010
897
898
} // new_global_struct
Oct 28, 2010
Oct 28, 2010
899
900
static void delete_global_struct(Context *ctx,
MOJOSHADER_astCompilationUnitStruct *unit)
Feb 19, 2010
Feb 19, 2010
901
902
903
904
905
906
907
{
DELETE_AST_NODE(unit);
delete_compilation_unit(ctx, unit->next);
delete_struct_declaration(ctx, unit->struct_info);
Free(ctx, unit);
} // delete_global_struct
Oct 28, 2010
Oct 28, 2010
908
909
static void delete_compilation_unit(Context *ctx,
MOJOSHADER_astCompilationUnit *unit)
Feb 19, 2010
Feb 19, 2010
910
911
912
913
914
915
916
917
918
919
920
{
if (!unit) return;
// it's important to not recurse too deeply here, since you may have
// thousands of items in this linked list (each line of a massive
// function, for example). To avoid this, we iterate the list here,
// deleting all children and making them think they have no reason
// to recurse in their own delete methods.
// Please note that everyone should _try_ to delete their "next" member,
// just in case, but hopefully this cleaned it out.
Oct 28, 2010
Oct 28, 2010
921
MOJOSHADER_astCompilationUnit *i = unit->next;
Feb 19, 2010
Feb 19, 2010
922
923
924
unit->next = NULL;
while (i)
{
Oct 28, 2010
Oct 28, 2010
925
MOJOSHADER_astCompilationUnit *next = i->next;
Feb 19, 2010
Feb 19, 2010
926
927
928
929
930
i->next = NULL;
delete_compilation_unit(ctx, i);
i = next;
} // while
Feb 22, 2010
Feb 22, 2010
931
switch (unit->ast.type)
Feb 19, 2010
Feb 19, 2010
932
933
{
#define DELETE_UNIT(typ, cls, fn) \
Oct 28, 2010
Oct 28, 2010
934
935
936
937
938
case MOJOSHADER_AST_COMPUNIT_##typ: delete_##fn(ctx, (cls *) unit); break;
DELETE_UNIT(FUNCTION, MOJOSHADER_astCompilationUnitFunction, function);
DELETE_UNIT(TYPEDEF, MOJOSHADER_astCompilationUnitTypedef, global_typedef);
DELETE_UNIT(VARIABLE, MOJOSHADER_astCompilationUnitVariable, global_variable);
DELETE_UNIT(STRUCT, MOJOSHADER_astCompilationUnitStruct, global_struct);
Feb 19, 2010
Feb 19, 2010
939
940
941
942
943
944
945
#undef DELETE_UNIT
default: assert(0 && "missing cleanup code"); break;
} // switch
// don't free (unit) here, the class-specific functions do it.
} // delete_compilation_unit
Oct 28, 2010
Oct 28, 2010
946
947
static MOJOSHADER_astStatement *new_typedef_statement(Context *ctx,
MOJOSHADER_astTypedef *td)
Feb 19, 2010
Feb 19, 2010
948
{
Oct 28, 2010
Oct 28, 2010
949
950
NEW_AST_NODE(retval, MOJOSHADER_astTypedefStatement,
MOJOSHADER_AST_STATEMENT_TYPEDEF);
Feb 19, 2010
Feb 19, 2010
951
952
retval->next = NULL;
retval->type_info = td;
Oct 28, 2010
Oct 28, 2010
953
return (MOJOSHADER_astStatement *) retval;
Feb 19, 2010
Feb 19, 2010
954
955
} // new_typedef_statement
Oct 28, 2010
Oct 28, 2010
956
957
static void delete_typedef_statement(Context *ctx,
MOJOSHADER_astTypedefStatement *stmt)
Feb 19, 2010
Feb 19, 2010
958
959
960
961
962
963
964
{
DELETE_AST_NODE(stmt);
delete_statement(ctx, stmt->next);
delete_typedef(ctx, stmt->type_info);
Free(ctx, stmt);
} // delete_typedef_statement
Oct 28, 2010
Oct 28, 2010
965
966
static MOJOSHADER_astStatement *new_return_statement(Context *ctx,
MOJOSHADER_astExpression *expr)
Feb 19, 2010
Feb 19, 2010
967
{
Oct 28, 2010
Oct 28, 2010
968
969
NEW_AST_NODE(retval, MOJOSHADER_astReturnStatement,
MOJOSHADER_AST_STATEMENT_RETURN);
Feb 19, 2010
Feb 19, 2010
970
971
retval->next = NULL;
retval->expr = expr;
Oct 28, 2010
Oct 28, 2010
972
return (MOJOSHADER_astStatement *) retval;
Feb 19, 2010
Feb 19, 2010
973
974
} // new_return_statement
Oct 28, 2010
Oct 28, 2010
975
976
static void delete_return_statement(Context *ctx,
MOJOSHADER_astReturnStatement *stmt)
Feb 19, 2010
Feb 19, 2010
977
978
979
980
981
982
983
{
DELETE_AST_NODE(stmt);
delete_statement(ctx, stmt->next);
delete_expr(ctx, stmt->expr);
Free(ctx, stmt);
} // delete_return_statement
Oct 28, 2010
Oct 28, 2010
984
985
static MOJOSHADER_astStatement *new_block_statement(Context *ctx,
MOJOSHADER_astStatement *stmts)
Oct 13, 2010
Oct 13, 2010
986
{
Oct 28, 2010
Oct 28, 2010
987
988
NEW_AST_NODE(retval, MOJOSHADER_astBlockStatement,
MOJOSHADER_AST_STATEMENT_BLOCK);
Oct 13, 2010
Oct 13, 2010
989
retval->next = NULL;
Oct 28, 2010
Oct 28, 2010
990
991
retval->statements = stmts;
return (MOJOSHADER_astStatement *) retval;
Oct 13, 2010
Oct 13, 2010
992
993
} // new_block_statement
Oct 28, 2010
Oct 28, 2010
994
995
static void delete_block_statement(Context *ctx,
MOJOSHADER_astBlockStatement *stmt)
Oct 13, 2010
Oct 13, 2010
996
997
998
999
1000
{
DELETE_AST_NODE(stmt);
delete_statement(ctx, stmt->statements);
delete_statement(ctx, stmt->next);
Free(ctx, stmt);