Skip to content

Latest commit

 

History

History
2417 lines (2095 loc) · 86.7 KB

mojoshader_compiler.c

File metadata and controls

2417 lines (2095 loc) · 86.7 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;
Oct 28, 2010
Oct 28, 2010
61
} TokenData;
Feb 19, 2010
Feb 19, 2010
62
63
Oct 19, 2010
Oct 19, 2010
64
// This tracks data types and variables, and notes when they enter/leave scope.
Feb 22, 2010
Feb 22, 2010
65
Oct 19, 2010
Oct 19, 2010
66
typedef struct SymbolScope
Feb 22, 2010
Feb 22, 2010
67
68
69
{
const char *symbol;
const char *datatype;
Oct 19, 2010
Oct 19, 2010
70
71
struct SymbolScope *next;
} SymbolScope;
Feb 22, 2010
Feb 22, 2010
72
Oct 19, 2010
Oct 19, 2010
73
typedef struct SymbolMap
Feb 22, 2010
Feb 22, 2010
74
{
Oct 19, 2010
Oct 19, 2010
75
76
77
HashTable *hash;
SymbolScope *scope;
} SymbolMap;
Feb 22, 2010
Feb 22, 2010
78
79
Feb 19, 2010
Feb 19, 2010
80
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;
int error_count;
ErrorList *errors;
Feb 24, 2010
Feb 24, 2010
91
StringCache *strcache;
Feb 22, 2010
Feb 22, 2010
92
93
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
94
95
SymbolMap usertypes;
SymbolMap variables;
Oct 28, 2010
Oct 28, 2010
96
MOJOSHADER_astNode *ast; // Abstract Syntax Tree
Oct 19, 2010
Oct 19, 2010
97
98
99
100
101
102
// These are entries allocated in the strcache, so these pointers are
// valid from shortly after we create the cache until we destroy it
// with the rest of the context. This makes it so we can compare common
// strings by pointer without having to hash them every time, so long as
// we're comparing a string pointer we know came from this string cache.
Oct 26, 2010
Oct 26, 2010
103
// The first batch are simplifed datatype strings ("b" == bool, etc).
Oct 19, 2010
Oct 19, 2010
104
105
106
const char *str_b; // "b"
const char *str_f; // "f"
const char *str_i; // "i"
Oct 20, 2010
Oct 20, 2010
107
108
109
110
const char *str_u; // "u"
const char *str_h; // "h"
const char *str_d; // "d"
const char *str_s; // "s"
Oct 26, 2010
Oct 26, 2010
111
112
113
114
115
116
117
118
119
120
121
const char *str_S; // "S"
const char *str_s1; // "s1"
const char *str_s2; // "s2"
const char *str_s3; // "s3"
const char *str_sc; // "sc"
const char *str_ss; // "ss"
const char *str_sS; // "sS"
const char *str_Fs; // "Fs"
const char *str_Fu; // "Fu"
const char *str_ns; // "ns"
const char *str_nu; // "nu"
Feb 19, 2010
Feb 19, 2010
122
123
124
125
126
127
128
} Context;
// Convenience functions for allocators...
static inline void out_of_memory(Context *ctx)
{
Feb 21, 2010
Feb 21, 2010
129
if (!ctx->out_of_memory) printf("out of memory\n"); // !!! FIXME: placeholder.
Feb 19, 2010
Feb 19, 2010
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
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
Feb 21, 2010
Feb 21, 2010
155
156
157
158
static void fail(Context *ctx, const char *str)
{
// !!! FIXME: placeholder.
(void) ctx;
Feb 21, 2010
Feb 21, 2010
159
printf("%s:%u: %s\n", ctx->sourcefile, ctx->sourceline, str);
Feb 21, 2010
Feb 21, 2010
160
} // fail
Feb 19, 2010
Feb 19, 2010
161
Feb 22, 2010
Feb 22, 2010
162
Oct 19, 2010
Oct 19, 2010
163
static void symbolmap_nuke(const void *k, const void *v, void *d) {/*no-op*/}
Feb 22, 2010
Feb 22, 2010
164
Oct 19, 2010
Oct 19, 2010
165
static int create_symbolmap(Context *ctx, SymbolMap *map)
Feb 22, 2010
Feb 22, 2010
166
{
Oct 19, 2010
Oct 19, 2010
167
// !!! FIXME: should compare string pointer, with string in cache.
Feb 22, 2010
Feb 22, 2010
168
map->scope = NULL;
Oct 19, 2010
Oct 19, 2010
169
170
171
172
map->hash = hash_create(ctx, hash_hash_string, hash_keymatch_string,
symbolmap_nuke, 1, ctx->malloc, ctx->free,
ctx->malloc_data);
if (!map->hash)
Feb 22, 2010
Feb 22, 2010
173
174
175
176
177
178
{
out_of_memory(ctx);
return 0;
} // if
return 1;
Oct 19, 2010
Oct 19, 2010
179
} // create_symbolmap
Feb 22, 2010
Feb 22, 2010
180
181
Oct 19, 2010
Oct 19, 2010
182
183
184
static void push_symbol(Context *ctx, SymbolMap *map,
const char *sym, const char *datatype)
{
Oct 26, 2010
Oct 26, 2010
185
186
187
// !!! FIXME: decide if this symbol is defined, and if so, if it's in
// !!! FIXME: the current scope.
Oct 19, 2010
Oct 19, 2010
188
SymbolScope *item = (SymbolScope *) Malloc(ctx, sizeof (SymbolScope));
Feb 22, 2010
Feb 22, 2010
189
190
191
192
193
if (item == NULL)
return;
if (sym != NULL)
{
Oct 19, 2010
Oct 19, 2010
194
if (hash_insert(map->hash, sym, datatype) == -1)
Feb 22, 2010
Feb 22, 2010
195
196
197
{
Free(ctx, item);
return;
Oct 26, 2010
Oct 26, 2010
198
} // if
Feb 22, 2010
Feb 22, 2010
199
200
201
202
203
204
} // if
item->symbol = sym; // cached strings, don't copy.
item->datatype = datatype;
item->next = map->scope;
map->scope = item;
Oct 19, 2010
Oct 19, 2010
205
206
207
208
209
} // push_symbol
static inline void push_usertype(Context *ctx, const char *sym, const char *dt)
{
push_symbol(ctx, &ctx->usertypes, sym, dt);
Feb 22, 2010
Feb 22, 2010
210
211
} // push_usertype
Oct 19, 2010
Oct 19, 2010
212
213
214
215
216
217
218
219
220
221
222
223
224
static inline void push_variable(Context *ctx, const char *sym, const char *dt)
{
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
225
{
Oct 19, 2010
Oct 19, 2010
226
SymbolScope *item = map->scope;
Feb 22, 2010
Feb 22, 2010
227
228
229
if (!item)
return;
if (item->symbol)
Oct 19, 2010
Oct 19, 2010
230
hash_remove(map->hash, item->symbol);
Feb 22, 2010
Feb 22, 2010
231
232
map->scope = item->next;
Free(ctx, item);
Oct 19, 2010
Oct 19, 2010
233
} // pop_symbol
Feb 22, 2010
Feb 22, 2010
234
Oct 19, 2010
Oct 19, 2010
235
static void pop_symbol_scope(Context *ctx, SymbolMap *map)
Feb 22, 2010
Feb 22, 2010
236
237
{
while ((map->scope) && (map->scope->symbol))
Oct 19, 2010
Oct 19, 2010
238
pop_symbol(ctx, map);
Feb 22, 2010
Feb 22, 2010
239
240
241
assert(map->scope != NULL);
assert(map->scope->symbol == NULL);
Oct 19, 2010
Oct 19, 2010
242
243
244
245
246
247
248
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
249
250
} // push_scope
Oct 26, 2010
Oct 26, 2010
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
static const char *find_symbol(Context *ctx, SymbolMap *map, const char *sym)
{
const void *value = NULL;
hash_find(map->hash, sym, &value);
return (const char *) value;
} // find_symbol
static inline const char *find_usertype(Context *ctx, const char *sym)
{
return find_symbol(ctx, &ctx->usertypes, sym);
} // find_usertype
static inline const char *find_variable(Context *ctx, const char *sym)
{
return find_symbol(ctx, &ctx->variables, sym);
} // find_variable
Oct 19, 2010
Oct 19, 2010
268
static void destroy_symbolmap(Context *ctx, SymbolMap *map)
Feb 22, 2010
Feb 22, 2010
269
270
{
while (map->scope)
Oct 19, 2010
Oct 19, 2010
271
272
273
pop_symbol_scope(ctx, map);
hash_destroy(map->hash);
} // destroy_symbolmap
Feb 22, 2010
Feb 22, 2010
274
275
Feb 19, 2010
Feb 19, 2010
276
277
278
279
280
// 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
281
#define NEW_AST_NODE(retval, cls, typ) \
Mar 3, 2010
Mar 3, 2010
282
cls *retval = (cls *) Malloc(ctx, sizeof (cls)); \
Feb 22, 2010
Feb 22, 2010
283
284
285
286
287
288
289
290
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
291
292
293
if (!cls) return; \
} while (0)
Oct 28, 2010
Oct 28, 2010
294
295
static void delete_compilation_unit(Context*, MOJOSHADER_astCompilationUnit*);
static void delete_statement(Context *ctx, MOJOSHADER_astStatement *stmt);
Feb 19, 2010
Feb 19, 2010
296
Oct 28, 2010
Oct 28, 2010
297
298
299
static MOJOSHADER_astExpression *new_callfunc_expr(Context *ctx,
MOJOSHADER_astExpression *identifier,
MOJOSHADER_astArguments *args)
Oct 25, 2010
Oct 25, 2010
300
{
Oct 28, 2010
Oct 28, 2010
301
302
NEW_AST_NODE(retval, MOJOSHADER_astExpressionCallFunction,
MOJOSHADER_AST_OP_CALLFUNC);
Oct 25, 2010
Oct 25, 2010
303
304
retval->identifier = identifier;
retval->args = args;
Oct 28, 2010
Oct 28, 2010
305
return (MOJOSHADER_astExpression *) retval;
Oct 25, 2010
Oct 25, 2010
306
307
} // new_callfunc_expr
Oct 28, 2010
Oct 28, 2010
308
309
310
static MOJOSHADER_astExpression *new_constructor_expr(Context *ctx,
const char *datatype,
MOJOSHADER_astArguments *args)
Feb 20, 2010
Feb 20, 2010
311
{
Oct 28, 2010
Oct 28, 2010
312
313
NEW_AST_NODE(retval, MOJOSHADER_astExpressionConstructor,
MOJOSHADER_AST_OP_CONSTRUCTOR);
Feb 20, 2010
Feb 20, 2010
314
315
retval->datatype = datatype;
retval->args = args;
Oct 28, 2010
Oct 28, 2010
316
return (MOJOSHADER_astExpression *) retval;
Feb 20, 2010
Feb 20, 2010
317
318
} // new_constructor_expr
Oct 28, 2010
Oct 28, 2010
319
320
321
static MOJOSHADER_astExpression *new_cast_expr(Context *ctx,
const char *datatype,
MOJOSHADER_astExpression *operand)
Feb 20, 2010
Feb 20, 2010
322
{
Oct 28, 2010
Oct 28, 2010
323
NEW_AST_NODE(retval, MOJOSHADER_astExpressionCast, MOJOSHADER_AST_OP_CAST);
Feb 20, 2010
Feb 20, 2010
324
325
retval->datatype = datatype;
retval->operand = operand;
Oct 28, 2010
Oct 28, 2010
326
return (MOJOSHADER_astExpression *) retval;
Feb 20, 2010
Feb 20, 2010
327
328
} // new_cast_expr
Oct 28, 2010
Oct 28, 2010
329
330
331
static MOJOSHADER_astExpression *new_unary_expr(Context *ctx,
const MOJOSHADER_astNodeType op,
MOJOSHADER_astExpression *operand)
Feb 9, 2010
Feb 9, 2010
332
{
Oct 28, 2010
Oct 28, 2010
333
NEW_AST_NODE(retval, MOJOSHADER_astExpressionUnary, op);
Feb 9, 2010
Feb 9, 2010
334
335
assert(operator_is_unary(op));
retval->operand = operand;
Oct 28, 2010
Oct 28, 2010
336
return (MOJOSHADER_astExpression *) retval;
Feb 9, 2010
Feb 9, 2010
337
338
} // new_unary_expr
Oct 28, 2010
Oct 28, 2010
339
340
341
342
static MOJOSHADER_astExpression *new_binary_expr(Context *ctx,
const MOJOSHADER_astNodeType op,
MOJOSHADER_astExpression *left,
MOJOSHADER_astExpression *right)
Feb 9, 2010
Feb 9, 2010
343
{
Oct 28, 2010
Oct 28, 2010
344
NEW_AST_NODE(retval, MOJOSHADER_astExpressionBinary, op);
Feb 9, 2010
Feb 9, 2010
345
346
347
assert(operator_is_binary(op));
retval->left = left;
retval->right = right;
Oct 28, 2010
Oct 28, 2010
348
return (MOJOSHADER_astExpression *) retval;
Feb 9, 2010
Feb 9, 2010
349
350
} // new_binary_expr
Oct 28, 2010
Oct 28, 2010
351
352
353
354
355
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
356
{
Oct 28, 2010
Oct 28, 2010
357
NEW_AST_NODE(retval, MOJOSHADER_astExpressionTernary, op);
Feb 9, 2010
Feb 9, 2010
358
359
360
361
assert(operator_is_ternary(op));
retval->left = left;
retval->center = center;
retval->right = right;
Oct 28, 2010
Oct 28, 2010
362
return (MOJOSHADER_astExpression *) retval;
Feb 9, 2010
Feb 9, 2010
363
364
} // new_ternary_expr
Oct 28, 2010
Oct 28, 2010
365
366
367
static MOJOSHADER_astExpression *new_deref_struct_expr(Context *ctx,
MOJOSHADER_astExpression *identifier,
const char *member)
Oct 26, 2010
Oct 26, 2010
368
{
Oct 28, 2010
Oct 28, 2010
369
370
NEW_AST_NODE(retval, MOJOSHADER_astExpressionDerefStruct,
MOJOSHADER_AST_OP_DEREF_STRUCT);
Oct 26, 2010
Oct 26, 2010
371
372
retval->identifier = identifier;
retval->member = member; // cached; don't copy string.
Oct 28, 2010
Oct 28, 2010
373
return (MOJOSHADER_astExpression *) retval;
Oct 26, 2010
Oct 26, 2010
374
375
} // new_deref_struct_expr
Oct 28, 2010
Oct 28, 2010
376
377
static MOJOSHADER_astExpression *new_identifier_expr(Context *ctx,
const char *string)
Feb 9, 2010
Feb 9, 2010
378
{
Oct 28, 2010
Oct 28, 2010
379
380
NEW_AST_NODE(retval, MOJOSHADER_astExpressionIdentifier,
MOJOSHADER_AST_OP_IDENTIFIER);
Feb 9, 2010
Feb 9, 2010
381
retval->identifier = string; // cached; don't copy string.
Oct 28, 2010
Oct 28, 2010
382
return (MOJOSHADER_astExpression *) retval;
Feb 9, 2010
Feb 9, 2010
383
384
} // new_identifier_expr
Oct 28, 2010
Oct 28, 2010
385
386
static MOJOSHADER_astExpression *new_literal_int_expr(Context *ctx,
const int value)
Feb 9, 2010
Feb 9, 2010
387
{
Oct 28, 2010
Oct 28, 2010
388
389
NEW_AST_NODE(retval, MOJOSHADER_astExpressionIntLiteral,
MOJOSHADER_AST_OP_INT_LITERAL);
Feb 9, 2010
Feb 9, 2010
390
retval->value = value;
Oct 28, 2010
Oct 28, 2010
391
return (MOJOSHADER_astExpression *) retval;
Feb 9, 2010
Feb 9, 2010
392
393
} // new_literal_int_expr
Oct 28, 2010
Oct 28, 2010
394
395
static MOJOSHADER_astExpression *new_literal_float_expr(Context *ctx,
const double dbl)
Feb 9, 2010
Feb 9, 2010
396
{
Oct 28, 2010
Oct 28, 2010
397
398
NEW_AST_NODE(retval, MOJOSHADER_astExpressionFloatLiteral,
MOJOSHADER_AST_OP_FLOAT_LITERAL);
Feb 9, 2010
Feb 9, 2010
399
retval->value = dbl;
Oct 28, 2010
Oct 28, 2010
400
return (MOJOSHADER_astExpression *) retval;
Feb 9, 2010
Feb 9, 2010
401
402
} // new_literal_float_expr
Oct 28, 2010
Oct 28, 2010
403
404
static MOJOSHADER_astExpression *new_literal_string_expr(Context *ctx,
const char *string)
Feb 9, 2010
Feb 9, 2010
405
{
Oct 28, 2010
Oct 28, 2010
406
407
NEW_AST_NODE(retval, MOJOSHADER_astExpressionStringLiteral,
MOJOSHADER_AST_OP_STRING_LITERAL);
Feb 9, 2010
Feb 9, 2010
408
retval->string = string; // cached; don't copy string.
Oct 28, 2010
Oct 28, 2010
409
return (MOJOSHADER_astExpression *) retval;
Oct 20, 2010
Oct 20, 2010
410
411
} // new_literal_string_expr
Oct 28, 2010
Oct 28, 2010
412
413
static MOJOSHADER_astExpression *new_literal_boolean_expr(Context *ctx,
const int value)
Oct 20, 2010
Oct 20, 2010
414
{
Oct 28, 2010
Oct 28, 2010
415
416
NEW_AST_NODE(retval, MOJOSHADER_astExpressionBooleanLiteral,
MOJOSHADER_AST_OP_BOOLEAN_LITERAL);
Oct 20, 2010
Oct 20, 2010
417
retval->value = value;
Oct 28, 2010
Oct 28, 2010
418
return (MOJOSHADER_astExpression *) retval;
Oct 20, 2010
Oct 20, 2010
419
} // new_literal_boolean_expr
Feb 9, 2010
Feb 9, 2010
420
Oct 28, 2010
Oct 28, 2010
421
static void delete_arguments(Context *ctx, MOJOSHADER_astArguments *args);
Oct 25, 2010
Oct 25, 2010
422
Oct 28, 2010
Oct 28, 2010
423
static void delete_expr(Context *ctx, MOJOSHADER_astExpression *_expr)
Feb 19, 2010
Feb 19, 2010
424
{
Oct 28, 2010
Oct 28, 2010
425
426
MOJOSHADER_astNode *expr = (MOJOSHADER_astNode *) _expr;
Feb 19, 2010
Feb 19, 2010
427
DELETE_AST_NODE(expr);
Oct 28, 2010
Oct 28, 2010
428
429
430
431
432
433
434
435
436
437
438
439
440
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
441
else if (operator_is_binary(expr->ast.type))
Feb 19, 2010
Feb 19, 2010
442
{
Oct 28, 2010
Oct 28, 2010
443
444
delete_expr(ctx, expr->binary.left);
delete_expr(ctx, expr->binary.right);
Feb 19, 2010
Feb 19, 2010
445
} // else if
Oct 28, 2010
Oct 28, 2010
446
Feb 22, 2010
Feb 22, 2010
447
else if (operator_is_ternary(expr->ast.type))
Feb 19, 2010
Feb 19, 2010
448
{
Oct 28, 2010
Oct 28, 2010
449
450
451
delete_expr(ctx, expr->ternary.left);
delete_expr(ctx, expr->ternary.center);
delete_expr(ctx, expr->ternary.right);
Oct 26, 2010
Oct 26, 2010
452
} // else if
Oct 28, 2010
Oct 28, 2010
453
454
else if (expr->ast.type == MOJOSHADER_AST_OP_CALLFUNC)
Oct 25, 2010
Oct 25, 2010
455
{
Oct 28, 2010
Oct 28, 2010
456
457
delete_expr(ctx, expr->callfunc.identifier);
delete_arguments(ctx, expr->callfunc.args);
Feb 20, 2010
Feb 20, 2010
458
} // else if
Feb 22, 2010
Feb 22, 2010
459
460
461
// rest of operators don't have extra data to free.
Feb 19, 2010
Feb 19, 2010
462
463
464
Free(ctx, expr);
} // delete_expr
Oct 28, 2010
Oct 28, 2010
465
466
static MOJOSHADER_astArguments *new_argument(Context *ctx,
MOJOSHADER_astExpression *arg)
Oct 25, 2010
Oct 25, 2010
467
{
Oct 28, 2010
Oct 28, 2010
468
469
NEW_AST_NODE(retval, MOJOSHADER_astArguments, MOJOSHADER_AST_ARGUMENTS);
retval->argument = arg;
Oct 25, 2010
Oct 25, 2010
470
471
472
473
retval->next = NULL;
return retval;
} // new_argument
Oct 28, 2010
Oct 28, 2010
474
static void delete_arguments(Context *ctx, MOJOSHADER_astArguments *args)
Oct 25, 2010
Oct 25, 2010
475
476
477
478
479
480
481
{
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
482
483
484
485
486
487
static MOJOSHADER_astFunctionParameters *new_function_param(Context *ctx,
const MOJOSHADER_astInputModifier inputmod,
const char *datatype, const char *identifier,
const char *semantic,
const MOJOSHADER_astInterpolationModifier interpmod,
MOJOSHADER_astExpression *initializer)
Feb 19, 2010
Feb 19, 2010
488
{
Oct 28, 2010
Oct 28, 2010
489
490
NEW_AST_NODE(retval, MOJOSHADER_astFunctionParameters,
MOJOSHADER_AST_FUNCTION_PARAMS);
Feb 19, 2010
Feb 19, 2010
491
492
493
494
495
496
497
498
retval->input_modifier = inputmod;
retval->datatype = datatype;
retval->identifier = identifier;
retval->semantic = semantic;
retval->interpolation_modifier = interpmod;
retval->initializer = initializer;
retval->next = NULL;
return retval;
Oct 25, 2010
Oct 25, 2010
499
} // new_function_param
Feb 19, 2010
Feb 19, 2010
500
Oct 28, 2010
Oct 28, 2010
501
502
static void delete_function_params(Context *ctx,
MOJOSHADER_astFunctionParameters *params)
Feb 19, 2010
Feb 19, 2010
503
{
Oct 25, 2010
Oct 25, 2010
504
505
506
507
508
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
509
Oct 28, 2010
Oct 28, 2010
510
511
512
513
static MOJOSHADER_astFunctionSignature *new_function_signature(Context *ctx,
const char *datatype,
const char *identifier,
MOJOSHADER_astFunctionParameters *params)
Feb 19, 2010
Feb 19, 2010
514
{
Oct 28, 2010
Oct 28, 2010
515
516
NEW_AST_NODE(retval, MOJOSHADER_astFunctionSignature,
MOJOSHADER_AST_FUNCTION_SIGNATURE);
Feb 19, 2010
Feb 19, 2010
517
518
retval->datatype = datatype;
retval->identifier = identifier;
Oct 25, 2010
Oct 25, 2010
519
retval->params = params;
Oct 28, 2010
Oct 28, 2010
520
retval->storage_class = MOJOSHADER_AST_FNSTORECLS_NONE;
Feb 19, 2010
Feb 19, 2010
521
522
523
524
retval->semantic = NULL;
return retval;
} // new_function_signature
Oct 28, 2010
Oct 28, 2010
525
526
static void delete_function_signature(Context *ctx,
MOJOSHADER_astFunctionSignature *sig)
Feb 19, 2010
Feb 19, 2010
527
528
{
DELETE_AST_NODE(sig);
Oct 25, 2010
Oct 25, 2010
529
delete_function_params(ctx, sig->params);
Feb 19, 2010
Feb 19, 2010
530
531
532
Free(ctx, sig);
} // delete_function_signature
Oct 28, 2010
Oct 28, 2010
533
534
535
static MOJOSHADER_astCompilationUnit *new_function(Context *ctx,
MOJOSHADER_astFunctionSignature *declaration,
MOJOSHADER_astStatement *definition)
Feb 19, 2010
Feb 19, 2010
536
{
Oct 28, 2010
Oct 28, 2010
537
538
NEW_AST_NODE(retval, MOJOSHADER_astCompilationUnitFunction,
MOJOSHADER_AST_COMPUNIT_FUNCTION);
Feb 19, 2010
Feb 19, 2010
539
540
541
retval->next = NULL;
retval->declaration = declaration;
retval->definition = definition;
Oct 28, 2010
Oct 28, 2010
542
return (MOJOSHADER_astCompilationUnit *) retval;
Feb 19, 2010
Feb 19, 2010
543
544
} // new_function
Oct 28, 2010
Oct 28, 2010
545
546
static void delete_function(Context *ctx,
MOJOSHADER_astCompilationUnitFunction *unitfn)
Feb 19, 2010
Feb 19, 2010
547
548
549
550
551
552
553
554
{
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
555
556
557
static MOJOSHADER_astScalarOrArray *new_scalar_or_array(Context *ctx,
const char *ident, const int isvec,
MOJOSHADER_astExpression *dim)
Feb 19, 2010
Feb 19, 2010
558
{
Oct 28, 2010
Oct 28, 2010
559
560
NEW_AST_NODE(retval, MOJOSHADER_astScalarOrArray,
MOJOSHADER_AST_SCALAR_OR_ARRAY);
Feb 19, 2010
Feb 19, 2010
561
562
563
564
565
566
retval->identifier = ident;
retval->isarray = isvec;
retval->dimension = dim;
return retval;
} // new_scalar_or_array
Oct 28, 2010
Oct 28, 2010
567
static void delete_scalar_or_array(Context *ctx,MOJOSHADER_astScalarOrArray *s)
Feb 19, 2010
Feb 19, 2010
568
{
Oct 28, 2010
Oct 28, 2010
569
570
571
DELETE_AST_NODE(s);
delete_expr(ctx, s->dimension);
Free(ctx, s);
Feb 19, 2010
Feb 19, 2010
572
573
} // delete_scalar_or_array
Oct 28, 2010
Oct 28, 2010
574
575
576
static MOJOSHADER_astTypedef *new_typedef(Context *ctx, const int isconst,
const char *datatype,
MOJOSHADER_astScalarOrArray *soa)
Feb 19, 2010
Feb 19, 2010
577
{
Oct 26, 2010
Oct 26, 2010
578
// we correct this datatype to the final string during semantic analysis.
Oct 28, 2010
Oct 28, 2010
579
NEW_AST_NODE(retval, MOJOSHADER_astTypedef, MOJOSHADER_AST_TYPEDEF);
Feb 19, 2010
Feb 19, 2010
580
581
582
583
584
585
retval->isconst = isconst;
retval->datatype = datatype;
retval->details = soa;
return retval;
} // new_typedef
Oct 28, 2010
Oct 28, 2010
586
static void delete_typedef(Context *ctx, MOJOSHADER_astTypedef *td)
Feb 19, 2010
Feb 19, 2010
587
588
589
590
591
592
{
DELETE_AST_NODE(td);
delete_scalar_or_array(ctx, td->details);
Free(ctx, td);
} // delete_typedef
Oct 28, 2010
Oct 28, 2010
593
594
static MOJOSHADER_astPackOffset *new_pack_offset(Context *ctx,
const char *a, const char *b)
Feb 19, 2010
Feb 19, 2010
595
{
Oct 28, 2010
Oct 28, 2010
596
NEW_AST_NODE(retval, MOJOSHADER_astPackOffset, MOJOSHADER_AST_PACK_OFFSET);
Feb 19, 2010
Feb 19, 2010
597
598
599
600
601
retval->ident1 = a;
retval->ident2 = b;
return retval;
} // new_pack_offset
Oct 28, 2010
Oct 28, 2010
602
static void delete_pack_offset(Context *ctx, MOJOSHADER_astPackOffset *o)
Feb 19, 2010
Feb 19, 2010
603
604
605
606
607
{
DELETE_AST_NODE(o);
Free(ctx, o);
} // delete_pack_offset
Oct 28, 2010
Oct 28, 2010
608
609
static MOJOSHADER_astVariableLowLevel *new_variable_lowlevel(Context *ctx,
MOJOSHADER_astPackOffset *po,
Feb 19, 2010
Feb 19, 2010
610
611
const char *reg)
{
Oct 28, 2010
Oct 28, 2010
612
613
NEW_AST_NODE(retval, MOJOSHADER_astVariableLowLevel,
MOJOSHADER_AST_VARIABLE_LOWLEVEL);
Feb 19, 2010
Feb 19, 2010
614
615
616
617
618
retval->packoffset = po;
retval->register_name = reg;
return retval;
} // new_variable_lowlevel
Oct 28, 2010
Oct 28, 2010
619
620
static void delete_variable_lowlevel(Context *ctx,
MOJOSHADER_astVariableLowLevel *vll)
Feb 19, 2010
Feb 19, 2010
621
622
623
624
625
626
{
DELETE_AST_NODE(vll);
delete_pack_offset(ctx, vll->packoffset);
Free(ctx, vll);
} // delete_variable_lowlevel
Oct 28, 2010
Oct 28, 2010
627
628
629
static MOJOSHADER_astAnnotations *new_annotation(Context *ctx,
const char *datatype,
MOJOSHADER_astExpression *initializer)
Feb 19, 2010
Feb 19, 2010
630
{
Oct 28, 2010
Oct 28, 2010
631
NEW_AST_NODE(retval, MOJOSHADER_astAnnotations, MOJOSHADER_AST_ANNOTATION);
Feb 19, 2010
Feb 19, 2010
632
633
634
635
636
637
retval->datatype = datatype;
retval->initializer = initializer;
retval->next = NULL;
return retval;
} // new_annotation
Oct 28, 2010
Oct 28, 2010
638
static void delete_annotation(Context *ctx, MOJOSHADER_astAnnotations *annos)
Feb 19, 2010
Feb 19, 2010
639
{
Oct 28, 2010
Oct 28, 2010
640
641
642
643
DELETE_AST_NODE(annos);
delete_annotation(ctx, annos->next);
delete_expr(ctx, annos->initializer);
Free(ctx, annos);
Feb 19, 2010
Feb 19, 2010
644
645
} // delete_annotation
Oct 28, 2010
Oct 28, 2010
646
647
648
649
650
651
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
652
{
Oct 28, 2010
Oct 28, 2010
653
654
NEW_AST_NODE(retval, MOJOSHADER_astVariableDeclaration,
MOJOSHADER_AST_VARIABLE_DECLARATION);
Feb 19, 2010
Feb 19, 2010
655
656
657
658
659
660
661
662
663
664
665
666
retval->attributes = 0;
retval->datatype = NULL;
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
667
668
static void delete_variable_declaration(Context *ctx,
MOJOSHADER_astVariableDeclaration *dcl)
Feb 19, 2010
Feb 19, 2010
669
670
671
672
673
674
675
676
677
678
{
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
679
680
static MOJOSHADER_astCompilationUnit *new_global_variable(Context *ctx,
MOJOSHADER_astVariableDeclaration *decl)
Feb 19, 2010
Feb 19, 2010
681
{
Oct 28, 2010
Oct 28, 2010
682
683
NEW_AST_NODE(retval, MOJOSHADER_astCompilationUnitVariable,
MOJOSHADER_AST_COMPUNIT_VARIABLE);
Feb 19, 2010
Feb 19, 2010
684
685
retval->next = NULL;
retval->declaration = decl;
Oct 28, 2010
Oct 28, 2010
686
return (MOJOSHADER_astCompilationUnit *) retval;
Feb 19, 2010
Feb 19, 2010
687
688
} // new_global_variable
Oct 28, 2010
Oct 28, 2010
689
690
static void delete_global_variable(Context *ctx,
MOJOSHADER_astCompilationUnitVariable *var)
Feb 19, 2010
Feb 19, 2010
691
692
693
694
695
696
697
{
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
698
699
static MOJOSHADER_astCompilationUnit *new_global_typedef(Context *ctx,
MOJOSHADER_astTypedef *td)
Feb 19, 2010
Feb 19, 2010
700
{
Oct 28, 2010
Oct 28, 2010
701
702
NEW_AST_NODE(retval, MOJOSHADER_astCompilationUnitTypedef,
MOJOSHADER_AST_COMPUNIT_TYPEDEF);
Feb 19, 2010
Feb 19, 2010
703
704
retval->next = NULL;
retval->type_info = td;
Oct 28, 2010
Oct 28, 2010
705
return (MOJOSHADER_astCompilationUnit *) retval;
Feb 19, 2010
Feb 19, 2010
706
707
} // new_global_typedef
Oct 28, 2010
Oct 28, 2010
708
709
static void delete_global_typedef(Context *ctx,
MOJOSHADER_astCompilationUnitTypedef *unit)
Feb 19, 2010
Feb 19, 2010
710
711
712
713
714
715
716
{
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
717
718
719
static MOJOSHADER_astStructMembers *new_struct_member(Context *ctx,
MOJOSHADER_astScalarOrArray *soa,
const char *semantic)
Feb 19, 2010
Feb 19, 2010
720
{
Oct 28, 2010
Oct 28, 2010
721
722
NEW_AST_NODE(retval, MOJOSHADER_astStructMembers,
MOJOSHADER_AST_STRUCT_MEMBER);
Feb 19, 2010
Feb 19, 2010
723
724
725
retval->datatype = NULL;
retval->semantic = semantic;
retval->details = soa;
Oct 28, 2010
Oct 28, 2010
726
retval->interpolation_mod = MOJOSHADER_AST_INTERPMOD_NONE;
Feb 19, 2010
Feb 19, 2010
727
728
729
730
retval->next = NULL;
return retval;
} // new_struct_member
Oct 28, 2010
Oct 28, 2010
731
732
static void delete_struct_member(Context *ctx,
MOJOSHADER_astStructMembers *member)
Feb 19, 2010
Feb 19, 2010
733
734
735
736
737
738
739
{
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
740
741
742
static MOJOSHADER_astStructDeclaration *new_struct_declaration(Context *ctx,
const char *name,
MOJOSHADER_astStructMembers *members)
Feb 19, 2010
Feb 19, 2010
743
{
Oct 28, 2010
Oct 28, 2010
744
745
NEW_AST_NODE(retval, MOJOSHADER_astStructDeclaration,
MOJOSHADER_AST_STRUCT_DECLARATION);
Feb 19, 2010
Feb 19, 2010
746
747
748
749
750
retval->name = name;
retval->members = members;
return retval;
} // new_struct_declaration
Oct 28, 2010
Oct 28, 2010
751
752
static void delete_struct_declaration(Context *ctx,
MOJOSHADER_astStructDeclaration *decl)
Feb 19, 2010
Feb 19, 2010
753
754
755
756
757
758
{
DELETE_AST_NODE(decl);
delete_struct_member(ctx, decl->members);
Free(ctx, decl);
} // delete_struct_declaration
Oct 28, 2010
Oct 28, 2010
759
760
static MOJOSHADER_astCompilationUnit *new_global_struct(Context *ctx,
MOJOSHADER_astStructDeclaration *sd)
Feb 19, 2010
Feb 19, 2010
761
{
Oct 28, 2010
Oct 28, 2010
762
763
NEW_AST_NODE(retval, MOJOSHADER_astCompilationUnitStruct,
MOJOSHADER_AST_COMPUNIT_STRUCT);
Feb 19, 2010
Feb 19, 2010
764
765
retval->next = NULL;
retval->struct_info = sd;
Oct 28, 2010
Oct 28, 2010
766
return (MOJOSHADER_astCompilationUnit *) retval;
Feb 19, 2010
Feb 19, 2010
767
768
} // new_global_struct
Oct 28, 2010
Oct 28, 2010
769
770
static void delete_global_struct(Context *ctx,
MOJOSHADER_astCompilationUnitStruct *unit)
Feb 19, 2010
Feb 19, 2010
771
772
773
774
775
776
777
{
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
778
779
static void delete_compilation_unit(Context *ctx,
MOJOSHADER_astCompilationUnit *unit)
Feb 19, 2010
Feb 19, 2010
780
781
782
783
784
785
786
787
788
789
790
{
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
791
MOJOSHADER_astCompilationUnit *i = unit->next;
Feb 19, 2010
Feb 19, 2010
792
793
794
unit->next = NULL;
while (i)
{
Oct 28, 2010
Oct 28, 2010
795
MOJOSHADER_astCompilationUnit *next = i->next;
Feb 19, 2010
Feb 19, 2010
796
797
798
799
800
i->next = NULL;
delete_compilation_unit(ctx, i);
i = next;
} // while
Feb 22, 2010
Feb 22, 2010
801
switch (unit->ast.type)
Feb 19, 2010
Feb 19, 2010
802
803
{
#define DELETE_UNIT(typ, cls, fn) \
Oct 28, 2010
Oct 28, 2010
804
805
806
807
808
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
809
810
811
812
813
814
815
#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
816
817
static MOJOSHADER_astStatement *new_typedef_statement(Context *ctx,
MOJOSHADER_astTypedef *td)
Feb 19, 2010
Feb 19, 2010
818
{
Oct 28, 2010
Oct 28, 2010
819
820
NEW_AST_NODE(retval, MOJOSHADER_astTypedefStatement,
MOJOSHADER_AST_STATEMENT_TYPEDEF);
Feb 19, 2010
Feb 19, 2010
821
822
retval->next = NULL;
retval->type_info = td;
Oct 28, 2010
Oct 28, 2010
823
return (MOJOSHADER_astStatement *) retval;
Feb 19, 2010
Feb 19, 2010
824
825
} // new_typedef_statement
Oct 28, 2010
Oct 28, 2010
826
827
static void delete_typedef_statement(Context *ctx,
MOJOSHADER_astTypedefStatement *stmt)
Feb 19, 2010
Feb 19, 2010
828
829
830
831
832
833
834
{
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
835
836
static MOJOSHADER_astStatement *new_return_statement(Context *ctx,
MOJOSHADER_astExpression *expr)
Feb 19, 2010
Feb 19, 2010
837
{
Oct 28, 2010
Oct 28, 2010
838
839
NEW_AST_NODE(retval, MOJOSHADER_astReturnStatement,
MOJOSHADER_AST_STATEMENT_RETURN);
Feb 19, 2010
Feb 19, 2010
840
841
retval->next = NULL;
retval->expr = expr;
Oct 28, 2010
Oct 28, 2010
842
return (MOJOSHADER_astStatement *) retval;
Feb 19, 2010
Feb 19, 2010
843
844
} // new_return_statement
Oct 28, 2010
Oct 28, 2010
845
846
static void delete_return_statement(Context *ctx,
MOJOSHADER_astReturnStatement *stmt)
Feb 19, 2010
Feb 19, 2010
847
848
849
850
851
852
853
{
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
854
855
static MOJOSHADER_astStatement *new_block_statement(Context *ctx,
MOJOSHADER_astStatement *stmts)
Oct 13, 2010
Oct 13, 2010
856
{
Oct 28, 2010
Oct 28, 2010
857
858
NEW_AST_NODE(retval, MOJOSHADER_astBlockStatement,
MOJOSHADER_AST_STATEMENT_BLOCK);
Oct 13, 2010
Oct 13, 2010
859
retval->next = NULL;
Oct 28, 2010
Oct 28, 2010
860
861
retval->statements = stmts;
return (MOJOSHADER_astStatement *) retval;
Oct 13, 2010
Oct 13, 2010
862
863
} // new_block_statement
Oct 28, 2010
Oct 28, 2010
864
865
static void delete_block_statement(Context *ctx,
MOJOSHADER_astBlockStatement *stmt)
Oct 13, 2010
Oct 13, 2010
866
867
868
869
870
871
872
{
DELETE_AST_NODE(stmt);
delete_statement(ctx, stmt->statements);
delete_statement(ctx, stmt->next);
Free(ctx, stmt);
} // delete_statement_block
Oct 28, 2010
Oct 28, 2010
873
874
875
876
877
878
static MOJOSHADER_astStatement *new_for_statement(Context *ctx,
MOJOSHADER_astVariableDeclaration *decl,
MOJOSHADER_astExpression *initializer,
MOJOSHADER_astExpression *looptest,
MOJOSHADER_astExpression *counter,
MOJOSHADER_astStatement *statement)
Feb 19, 2010
Feb 19, 2010
879
{
Oct 28, 2010
Oct 28, 2010
880
881
NEW_AST_NODE(retval, MOJOSHADER_astForStatement,
MOJOSHADER_AST_STATEMENT_FOR);
Feb 19, 2010
Feb 19, 2010
882
883
884
885
886
887
888
retval->next = NULL;
retval->unroll = -1;
retval->var_decl = decl;
retval->initializer = initializer;
retval->looptest = looptest;
retval->counter = counter;
retval->statement = statement;
Oct 28, 2010
Oct 28, 2010
889
return (MOJOSHADER_astStatement *) retval;
Feb 19, 2010
Feb 19, 2010
890
891
} // new_for_statement
Oct 28, 2010
Oct 28, 2010
892
static void delete_for_statement(Context *ctx,MOJOSHADER_astForStatement *stmt)
Feb 19, 2010
Feb 19, 2010
893
894
895
896
897
898
899
900
901
902
903
{
DELETE_AST_NODE(stmt);
delete_statement(ctx, stmt->next);
delete_variable_declaration(ctx, stmt->var_decl);
delete_expr(ctx, stmt->initializer);
delete_expr(ctx, stmt->looptest);
delete_expr(ctx, stmt->counter);
delete_statement(ctx, stmt->statement);
Free(ctx, stmt);
} // delete_for_statement
Oct 28, 2010
Oct 28, 2010
904
905
906
907
static MOJOSHADER_astStatement *new_do_statement(Context *ctx,
const int unroll,
MOJOSHADER_astStatement *stmt,
MOJOSHADER_astExpression *expr)
Feb 19, 2010
Feb 19, 2010
908
{
Oct 28, 2010
Oct 28, 2010
909
NEW_AST_NODE(retval,MOJOSHADER_astDoStatement,MOJOSHADER_AST_STATEMENT_DO);
Feb 19, 2010
Feb 19, 2010
910
911
912
913
retval->next = NULL;
retval->unroll = unroll;
retval->expr = expr;
retval->statement = stmt;
Oct 28, 2010
Oct 28, 2010
914
return (MOJOSHADER_astStatement *) retval;
Feb 19, 2010
Feb 19, 2010
915
916
} // new_do_statement
Oct 28, 2010
Oct 28, 2010
917
static void delete_do_statement(Context *ctx, MOJOSHADER_astDoStatement *stmt)
Feb 19, 2010
Feb 19, 2010
918
919
920
921
922
923
924
925
{
DELETE_AST_NODE(stmt);
delete_statement(ctx, stmt->next);
delete_statement(ctx, stmt->statement);
delete_expr(ctx, stmt->expr);
Free(ctx, stmt);
} // delete_do_statement
Oct 28, 2010
Oct 28, 2010
926
927
928
929
static MOJOSHADER_astStatement *new_while_statement(Context *ctx,
const int unroll,
MOJOSHADER_astExpression *expr,
MOJOSHADER_astStatement *stmt)
Feb 19, 2010
Feb 19, 2010
930
{
Oct 28, 2010
Oct 28, 2010
931
932
NEW_AST_NODE(retval, MOJOSHADER_astWhileStatement,
MOJOSHADER_AST_STATEMENT_WHILE);
Feb 19, 2010
Feb 19, 2010
933
934
935
936
retval->next = NULL;
retval->unroll = unroll;
retval->expr = expr;
retval->statement = stmt;
Oct 28, 2010
Oct 28, 2010
937
return (MOJOSHADER_astStatement *) retval;
Feb 19, 2010
Feb 19, 2010
938
939
} // new_while_statement
Oct 28, 2010
Oct 28, 2010
940
941
static void delete_while_statement(Context *ctx,
MOJOSHADER_astWhileStatement *stmt)
Feb 19, 2010
Feb 19, 2010
942
943
944
945
946
947
948
949
{
DELETE_AST_NODE(stmt);
delete_statement(ctx, stmt->next);
delete_statement(ctx, stmt->statement);
delete_expr(ctx, stmt->expr);
Free(ctx, stmt);
} // delete_while_statement
Oct 28, 2010
Oct 28, 2010
950
951
952
953
954
static MOJOSHADER_astStatement *new_if_statement(Context *ctx,
const int attr,
MOJOSHADER_astExpression *expr,
MOJOSHADER_astStatement *stmt,
MOJOSHADER_astStatement *elsestmt)
Feb 19, 2010
Feb 19, 2010
955
{
Oct 28, 2010
Oct 28, 2010
956
NEW_AST_NODE(retval,MOJOSHADER_astIfStatement,MOJOSHADER_AST_STATEMENT_IF);
Feb 19, 2010
Feb 19, 2010
957
958
959
960
961
retval->next = NULL;
retval->attributes = attr;
retval->expr = expr;
retval->statement = stmt;
retval->else_statement = elsestmt;
Oct 28, 2010
Oct 28, 2010
962
return (MOJOSHADER_astStatement *) retval;
Feb 19, 2010
Feb 19, 2010
963
964
} // new_if_statement
Oct 28, 2010
Oct 28, 2010
965
static void delete_if_statement(Context *ctx, MOJOSHADER_astIfStatement *stmt)
Feb 19, 2010
Feb 19, 2010
966
967
968
969
970
971
972
973
974
{
DELETE_AST_NODE(stmt);
delete_statement(ctx, stmt->next);
delete_expr(ctx, stmt->expr);
delete_statement(ctx, stmt->statement);
delete_statement(ctx, stmt->else_statement);
Free(ctx, stmt);
} // delete_if_statement
Oct 28, 2010
Oct 28, 2010
975
976
977
static MOJOSHADER_astSwitchCases *new_switch_case(Context *ctx,
MOJOSHADER_astExpression *expr,
MOJOSHADER_astStatement *stmt)
Feb 19, 2010
Feb 19, 2010
978
{
Oct 28, 2010
Oct 28, 2010
979
NEW_AST_NODE(retval, MOJOSHADER_astSwitchCases, MOJOSHADER_AST_SWITCH_CASE);
Feb 19, 2010
Feb 19, 2010
980
981
982
983
984
985
retval->expr = expr;
retval->statement = stmt;
retval->next = NULL;
return retval;
} // new_switch_case
Oct 28, 2010
Oct 28, 2010
986
static void delete_switch_case(Context *ctx, MOJOSHADER_astSwitchCases *sc)
Feb 19, 2010
Feb 19, 2010
987
988
989
990
991
992
993
994
{
DELETE_AST_NODE(sc);
delete_switch_case(ctx, sc->next);
delete_expr(ctx, sc->expr);
delete_statement(ctx, sc->statement);
Free(ctx, sc);
} // delete_switch_case
Oct 28, 2010
Oct 28, 2010
995
static MOJOSHADER_astStatement *new_empty_statement(Context *ctx)
Feb 19, 2010
Feb 19, 2010
996
{
Oct 28, 2010
Oct 28, 2010
997
998
NEW_AST_NODE(retval, MOJOSHADER_astEmptyStatement,
MOJOSHADER_AST_STATEMENT_EMPTY);
Feb 19, 2010
Feb 19, 2010
999
retval->next = NULL;
Oct 28, 2010
Oct 28, 2010
1000
return (MOJOSHADER_astStatement *) retval;