Skip to content

Latest commit

 

History

History
2664 lines (2284 loc) · 94.2 KB

mojoshader_compiler.c

File metadata and controls

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