Skip to content

Latest commit

 

History

History
6299 lines (5453 loc) · 236 KB

mojoshader_compiler.c

File metadata and controls

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