Skip to content

Latest commit

 

History

History
2679 lines (2297 loc) · 95.1 KB

mojoshader_compiler.c

File metadata and controls

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