Skip to content

Latest commit

 

History

History
2684 lines (2332 loc) · 83.8 KB

mojoshader_compiler.c

File metadata and controls

2684 lines (2332 loc) · 83.8 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; \
} \
}
Feb 9, 2010
Feb 9, 2010
37
typedef union TokenData
Aug 23, 2009
Aug 23, 2009
38
{
Feb 9, 2010
Feb 9, 2010
39
40
41
int64 i64;
double dbl;
const char *string;
Aug 23, 2009
Aug 23, 2009
42
43
} TokenData;
Feb 28, 2009
Feb 28, 2009
44
Feb 19, 2010
Feb 19, 2010
45
// Structures that make up the parse tree...
Feb 9, 2010
Feb 9, 2010
46
Feb 22, 2010
Feb 22, 2010
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
typedef enum ASTNodeType
{
AST_OP_START_RANGE,
AST_OP_START_RANGE_UNARY,
AST_OP_POSTINCREMENT,
AST_OP_POSTDECREMENT,
AST_OP_PREINCREMENT,
AST_OP_PREDECREMENT,
AST_OP_NEGATE,
AST_OP_COMPLEMENT,
AST_OP_NOT,
AST_OP_END_RANGE_UNARY,
AST_OP_START_RANGE_BINARY,
AST_OP_DEREF_ARRAY,
AST_OP_CALLFUNC,
AST_OP_DEREF_STRUCT,
AST_OP_COMMA,
AST_OP_MULTIPLY,
AST_OP_DIVIDE,
AST_OP_MODULO,
AST_OP_ADD,
AST_OP_SUBTRACT,
AST_OP_LSHIFT,
AST_OP_RSHIFT,
AST_OP_LESSTHAN,
AST_OP_GREATERTHAN,
AST_OP_LESSTHANOREQUAL,
AST_OP_GREATERTHANOREQUAL,
AST_OP_EQUAL,
AST_OP_NOTEQUAL,
AST_OP_BINARYAND,
AST_OP_BINARYXOR,
AST_OP_BINARYOR,
AST_OP_LOGICALAND,
AST_OP_LOGICALOR,
AST_OP_ASSIGN,
AST_OP_MULASSIGN,
AST_OP_DIVASSIGN,
AST_OP_MODASSIGN,
AST_OP_ADDASSIGN,
AST_OP_SUBASSIGN,
AST_OP_LSHIFTASSIGN,
AST_OP_RSHIFTASSIGN,
AST_OP_ANDASSIGN,
AST_OP_XORASSIGN,
AST_OP_ORASSIGN,
AST_OP_END_RANGE_BINARY,
AST_OP_START_RANGE_TERNARY,
AST_OP_CONDITIONAL,
AST_OP_END_RANGE_TERNARY,
AST_OP_START_RANGE_DATA,
AST_OP_IDENTIFIER,
AST_OP_INT_LITERAL,
AST_OP_FLOAT_LITERAL,
AST_OP_STRING_LITERAL,
Oct 20, 2010
Oct 20, 2010
105
AST_OP_BOOLEAN_LITERAL,
Feb 22, 2010
Feb 22, 2010
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
AST_OP_END_RANGE_DATA,
AST_OP_START_RANGE_MISC,
AST_OP_CONSTRUCTOR,
AST_OP_CAST,
AST_OP_END_RANGE_MISC,
AST_OP_END_RANGE,
AST_COMPUNIT_START_RANGE,
AST_COMPUNIT_FUNCTION, // function declaration or definition
AST_COMPUNIT_TYPEDEF, // typedef or struct
AST_COMPUNIT_STRUCT, // global struct
AST_COMPUNIT_VARIABLE, // global variable
AST_COMPUNIT_END_RANGE,
AST_STATEMENT_START_RANGE,
Oct 13, 2010
Oct 13, 2010
122
AST_STATEMENT_BLOCK,
Feb 22, 2010
Feb 22, 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
151
152
153
154
155
156
157
158
159
160
161
AST_STATEMENT_EMPTY,
AST_STATEMENT_EXPRESSION,
AST_STATEMENT_IF,
AST_STATEMENT_SWITCH,
AST_STATEMENT_FOR,
AST_STATEMENT_DO,
AST_STATEMENT_WHILE,
AST_STATEMENT_RETURN,
AST_STATEMENT_BREAK,
AST_STATEMENT_CONTINUE,
AST_STATEMENT_DISCARD,
AST_STATEMENT_TYPEDEF,
AST_STATEMENT_STRUCT,
AST_STATEMENT_VARDECL,
AST_STATEMENT_END_RANGE,
AST_MISC_START_RANGE,
AST_FUNCTION_ARGS,
AST_FUNCTION_SIGNATURE,
AST_SCALAR_OR_ARRAY,
AST_TYPEDEF,
AST_PACK_OFFSET,
AST_VARIABLE_LOWLEVEL,
AST_ANNOTATION,
AST_VARIABLE_DECLARATION,
AST_STRUCT_DECLARATION,
AST_STRUCT_MEMBER,
AST_SWITCH_CASE,
AST_MISC_END_RANGE,
AST_END_RANGE
} ASTNodeType;
typedef struct ASTNode
{
ASTNodeType type;
const char *filename;
uint32 line;
} ASTNode;
Feb 9, 2010
Feb 9, 2010
162
Feb 19, 2010
Feb 19, 2010
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
typedef enum VariableAttributes
{
VARATTR_EXTERN = (1 << 0),
VARATTR_NOINTERPOLATION = (1 << 1),
VARATTR_SHARED = (1 << 2),
VARATTR_STATIC = (1 << 3),
VARATTR_UNIFORM = (1 << 4),
VARATTR_VOLATILE = (1 << 5),
VARATTR_CONST = (1 << 6),
VARATTR_ROWMAJOR = (1 << 7),
VARATTR_COLUMNMAJOR = (1 << 8)
} VariableAttributes;
typedef enum IfAttributes
{
IFATTR_NONE,
IFATTR_BRANCH,
IFATTR_FLATTEN,
IFATTR_IFALL,
IFATTR_IFANY,
IFATTR_PREDICATE,
IFATTR_PREDICATEBLOCK,
} IfAttributes;
typedef enum SwitchAttributes
{
SWITCHATTR_NONE,
SWITCHATTR_FLATTEN,
SWITCHATTR_BRANCH,
SWITCHATTR_FORCECASE,
SWITCHATTR_CALL
} SwitchAttributes;
Feb 22, 2010
Feb 22, 2010
196
static inline int operator_is_unary(const ASTNodeType op)
Feb 9, 2010
Feb 9, 2010
197
{
Feb 22, 2010
Feb 22, 2010
198
return ((op > AST_OP_START_RANGE_UNARY) && (op < AST_OP_END_RANGE_UNARY));
Feb 9, 2010
Feb 9, 2010
199
200
} // operator_is_unary
Feb 22, 2010
Feb 22, 2010
201
static inline int operator_is_binary(const ASTNodeType op)
Feb 9, 2010
Feb 9, 2010
202
{
Feb 22, 2010
Feb 22, 2010
203
return ((op > AST_OP_START_RANGE_BINARY) && (op < AST_OP_END_RANGE_BINARY));
Feb 9, 2010
Feb 9, 2010
204
205
} // operator_is_binary
Feb 22, 2010
Feb 22, 2010
206
static inline int operator_is_ternary(const ASTNodeType op)
Feb 9, 2010
Feb 9, 2010
207
{
Feb 22, 2010
Feb 22, 2010
208
return ((op > AST_OP_START_RANGE_TERNARY) && (op < AST_OP_END_RANGE_TERNARY));
Feb 9, 2010
Feb 9, 2010
209
210
} // operator_is_ternary
Feb 22, 2010
Feb 22, 2010
211
typedef struct ASTGeneric
Feb 9, 2010
Feb 9, 2010
212
{
Feb 22, 2010
Feb 22, 2010
213
214
215
216
ASTNode ast;
} ASTGeneric;
typedef ASTGeneric Expression;
Feb 9, 2010
Feb 9, 2010
217
218
219
typedef struct ExpressionUnary
{
Feb 22, 2010
Feb 22, 2010
220
ASTNode ast;
Feb 9, 2010
Feb 9, 2010
221
222
223
224
225
Expression *operand;
} ExpressionUnary;
typedef struct ExpressionBinary
{
Feb 22, 2010
Feb 22, 2010
226
ASTNode ast;
Feb 9, 2010
Feb 9, 2010
227
228
229
230
231
232
Expression *left;
Expression *right;
} ExpressionBinary;
typedef struct ExpressionTernary
{
Feb 22, 2010
Feb 22, 2010
233
ASTNode ast;
Feb 9, 2010
Feb 9, 2010
234
235
236
237
238
239
240
Expression *left;
Expression *center;
Expression *right;
} ExpressionTernary;
typedef struct ExpressionIdentifier
{
Feb 22, 2010
Feb 22, 2010
241
ASTNode ast; // Always AST_OP_IDENTIFIER
Feb 9, 2010
Feb 9, 2010
242
243
244
245
246
const char *identifier;
} ExpressionIdentifier;
typedef struct ExpressionIntLiteral
{
Feb 22, 2010
Feb 22, 2010
247
ASTNode ast; // Always AST_OP_INT_LITERAL
Feb 9, 2010
Feb 9, 2010
248
249
250
251
252
int64 value;
} ExpressionIntLiteral;
typedef struct ExpressionFloatLiteral
{
Feb 22, 2010
Feb 22, 2010
253
ASTNode ast; // Always AST_OP_FLOAT_LITERAL
Feb 9, 2010
Feb 9, 2010
254
255
256
257
258
double value;
} ExpressionFloatLiteral;
typedef struct ExpressionStringLiteral
{
Feb 22, 2010
Feb 22, 2010
259
ASTNode ast; // Always AST_OP_STRING_LITERAL
Feb 9, 2010
Feb 9, 2010
260
261
262
const char *string;
} ExpressionStringLiteral;
Oct 20, 2010
Oct 20, 2010
263
264
265
266
267
268
typedef struct ExpressionBooleanLiteral
{
ASTNode ast; // Always AST_OP_BOOLEAN_LITERAL
int value; // Always 1 or 0.
} ExpressionBooleanLiteral;
Feb 20, 2010
Feb 20, 2010
269
270
typedef struct ExpressionConstructor
{
Feb 22, 2010
Feb 22, 2010
271
ASTNode ast; // Always AST_OP_CONSTRUCTOR
Feb 20, 2010
Feb 20, 2010
272
273
274
275
276
277
const char *datatype;
Expression *args;
} ExpressionConstructor;
typedef struct ExpressionCast
{
Feb 22, 2010
Feb 22, 2010
278
ASTNode ast; // Always AST_OP_CAST
Feb 20, 2010
Feb 20, 2010
279
280
281
282
const char *datatype;
Expression *operand;
} ExpressionCast;
Feb 19, 2010
Feb 19, 2010
283
284
typedef struct CompilationUnit
{
Feb 22, 2010
Feb 22, 2010
285
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
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
struct CompilationUnit *next;
} CompilationUnit;
typedef enum FunctionStorageClass
{
FNSTORECLS_NONE,
FNSTORECLS_INLINE
} FunctionStorageClass;
typedef enum InputModifier
{
INPUTMOD_NONE,
INPUTMOD_IN,
INPUTMOD_OUT,
INPUTMOD_INOUT,
INPUTMOD_UNIFORM
} InputModifier;
typedef enum InterpolationModifier
{
INTERPMOD_NONE,
INTERPMOD_LINEAR,
INTERPMOD_CENTROID,
INTERPMOD_NOINTERPOLATION,
INTERPMOD_NOPERSPECTIVE,
INTERPMOD_SAMPLE
} InterpolationModifier;
typedef struct FunctionArguments
{
Feb 22, 2010
Feb 22, 2010
316
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
317
318
319
320
321
322
323
324
325
326
327
InputModifier input_modifier;
const char *datatype;
const char *identifier;
const char *semantic;
InterpolationModifier interpolation_modifier;
Expression *initializer;
struct FunctionArguments *next;
} FunctionArguments;
typedef struct FunctionSignature
{
Feb 22, 2010
Feb 22, 2010
328
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
329
330
331
332
333
334
335
336
337
const char *datatype;
const char *identifier;
FunctionArguments *args;
FunctionStorageClass storage_class;
const char *semantic;
} FunctionSignature;
typedef struct ScalarOrArray
{
Feb 22, 2010
Feb 22, 2010
338
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
339
340
341
342
343
344
345
const char *identifier;
int isarray;
Expression *dimension;
} ScalarOrArray;
typedef struct Annotations
{
Feb 22, 2010
Feb 22, 2010
346
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
347
348
349
350
351
352
353
const char *datatype;
Expression *initializer;
struct Annotations *next;
} Annotations;
typedef struct PackOffset
{
Feb 22, 2010
Feb 22, 2010
354
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
355
356
357
358
359
360
const char *ident1; // !!! FIXME: rename this.
const char *ident2;
} PackOffset;
typedef struct VariableLowLevel
{
Feb 22, 2010
Feb 22, 2010
361
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
362
363
364
365
366
367
PackOffset *packoffset;
const char *register_name;
} VariableLowLevel;
typedef struct StructMembers
{
Feb 22, 2010
Feb 22, 2010
368
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
369
370
371
372
373
374
375
376
377
const char *datatype;
const char *semantic;
ScalarOrArray *details;
InterpolationModifier interpolation_mod;
struct StructMembers *next;
} StructMembers;
typedef struct StructDeclaration
{
Feb 22, 2010
Feb 22, 2010
378
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
379
380
381
382
383
384
const char *name;
StructMembers *members;
} StructDeclaration;
typedef struct VariableDeclaration
{
Feb 22, 2010
Feb 22, 2010
385
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
386
387
388
389
390
391
392
393
394
395
396
397
398
int attributes;
const char *datatype;
StructDeclaration *anonymous_datatype;
ScalarOrArray *details;
const char *semantic;
Annotations *annotations;
Expression *initializer;
VariableLowLevel *lowlevel;
struct VariableDeclaration *next;
} VariableDeclaration;
typedef struct Statement
{
Feb 22, 2010
Feb 22, 2010
399
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
400
401
402
403
404
405
406
407
struct Statement *next;
} Statement;
typedef Statement EmptyStatement;
typedef Statement BreakStatement;
typedef Statement ContinueStatement;
typedef Statement DiscardStatement;
Oct 13, 2010
Oct 13, 2010
408
409
410
411
412
413
414
typedef struct BlockStatement // something enclosed in "{}" braces.
{
ASTNode ast;
struct Statement *next;
Statement *statements; // list of statements enclosed by this block.
} BlockStatement;
Feb 19, 2010
Feb 19, 2010
415
416
typedef struct ReturnStatement
{
Feb 22, 2010
Feb 22, 2010
417
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
418
419
420
421
422
423
struct Statement *next;
Expression *expr;
} ReturnStatement;
typedef struct ExpressionStatement
{
Feb 22, 2010
Feb 22, 2010
424
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
425
426
427
428
429
430
struct Statement *next;
Expression *expr;
} ExpressionStatement;
typedef struct IfStatement
{
Feb 22, 2010
Feb 22, 2010
431
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
432
433
434
435
436
437
438
439
440
struct Statement *next;
int attributes;
Expression *expr;
Statement *statement;
Statement *else_statement;
} IfStatement;
typedef struct SwitchCases
{
Feb 22, 2010
Feb 22, 2010
441
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
442
443
444
445
446
447
448
Expression *expr;
Statement *statement;
struct SwitchCases *next;
} SwitchCases;
typedef struct SwitchStatement
{
Feb 22, 2010
Feb 22, 2010
449
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
450
451
452
453
454
455
456
457
struct Statement *next;
int attributes;
Expression *expr;
SwitchCases *cases;
} SwitchStatement;
typedef struct WhileStatement
{
Feb 22, 2010
Feb 22, 2010
458
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
459
struct Statement *next;
Feb 23, 2010
Feb 23, 2010
460
int64 unroll; // # times to unroll, 0 to loop, negative for compiler's choice.
Feb 19, 2010
Feb 19, 2010
461
462
463
464
465
466
467
468
Expression *expr;
Statement *statement;
} WhileStatement;
typedef WhileStatement DoStatement;
typedef struct ForStatement
{
Feb 22, 2010
Feb 22, 2010
469
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
470
struct Statement *next;
Feb 23, 2010
Feb 23, 2010
471
int64 unroll; // # times to unroll, 0 to loop, negative for compiler's choice.
Feb 19, 2010
Feb 19, 2010
472
473
474
475
476
477
478
479
480
VariableDeclaration *var_decl;
Expression *initializer;
Expression *looptest;
Expression *counter;
Statement *statement;
} ForStatement;
typedef struct Typedef
{
Feb 22, 2010
Feb 22, 2010
481
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
482
483
484
485
486
487
488
int isconst;
const char *datatype;
ScalarOrArray *details;
} Typedef;
typedef struct TypedefStatement
{
Feb 22, 2010
Feb 22, 2010
489
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
490
491
492
493
494
495
struct Statement *next;
Typedef *type_info;
} TypedefStatement;
typedef struct VarDeclStatement
{
Feb 22, 2010
Feb 22, 2010
496
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
497
struct Statement *next;
Feb 22, 2010
Feb 22, 2010
498
VariableDeclaration *declaration;
Feb 19, 2010
Feb 19, 2010
499
500
501
502
} VarDeclStatement;
typedef struct StructStatement
{
Feb 22, 2010
Feb 22, 2010
503
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
504
505
506
507
508
509
struct Statement *next;
StructDeclaration *struct_info;
} StructStatement;
typedef struct CompilationUnitFunction
{
Feb 22, 2010
Feb 22, 2010
510
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
511
512
513
514
515
516
517
struct CompilationUnit *next;
FunctionSignature *declaration;
Statement *definition;
} CompilationUnitFunction;
typedef struct CompilationUnitTypedef
{
Feb 22, 2010
Feb 22, 2010
518
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
519
520
521
522
523
524
struct CompilationUnit *next;
Typedef *type_info;
} CompilationUnitTypedef;
typedef struct CompilationUnitStruct
{
Feb 22, 2010
Feb 22, 2010
525
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
526
527
528
529
530
531
struct CompilationUnit *next;
StructDeclaration *struct_info;
} CompilationUnitStruct;
typedef struct CompilationUnitVariable
{
Feb 22, 2010
Feb 22, 2010
532
ASTNode ast;
Feb 19, 2010
Feb 19, 2010
533
534
535
536
537
struct CompilationUnit *next;
VariableDeclaration *declaration;
} CompilationUnitVariable;
Oct 19, 2010
Oct 19, 2010
538
// This tracks data types and variables, and notes when they enter/leave scope.
Feb 22, 2010
Feb 22, 2010
539
Oct 19, 2010
Oct 19, 2010
540
typedef struct SymbolScope
Feb 22, 2010
Feb 22, 2010
541
542
543
{
const char *symbol;
const char *datatype;
Oct 19, 2010
Oct 19, 2010
544
545
struct SymbolScope *next;
} SymbolScope;
Feb 22, 2010
Feb 22, 2010
546
Oct 19, 2010
Oct 19, 2010
547
typedef struct SymbolMap
Feb 22, 2010
Feb 22, 2010
548
{
Oct 19, 2010
Oct 19, 2010
549
550
551
HashTable *hash;
SymbolScope *scope;
} SymbolMap;
Feb 22, 2010
Feb 22, 2010
552
553
Feb 19, 2010
Feb 19, 2010
554
555
556
557
558
559
560
561
562
563
564
// Compile state, passed around all over the place.
typedef struct Context
{
int isfail;
int out_of_memory;
MOJOSHADER_malloc malloc;
MOJOSHADER_free free;
void *malloc_data;
int error_count;
ErrorList *errors;
Feb 24, 2010
Feb 24, 2010
565
StringCache *strcache;
Feb 22, 2010
Feb 22, 2010
566
567
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
568
569
SymbolMap usertypes;
SymbolMap variables;
Feb 19, 2010
Feb 19, 2010
570
CompilationUnit *ast; // Abstract Syntax Tree
Oct 19, 2010
Oct 19, 2010
571
572
573
574
575
576
577
578
579
// 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.
const char *str_b; // "b"
const char *str_f; // "f"
const char *str_i; // "i"
Oct 20, 2010
Oct 20, 2010
580
581
582
583
const char *str_u; // "u"
const char *str_h; // "h"
const char *str_d; // "d"
const char *str_s; // "s"
Feb 19, 2010
Feb 19, 2010
584
585
586
587
588
589
590
} Context;
// Convenience functions for allocators...
static inline void out_of_memory(Context *ctx)
{
Feb 21, 2010
Feb 21, 2010
591
if (!ctx->out_of_memory) printf("out of memory\n"); // !!! FIXME: placeholder.
Feb 19, 2010
Feb 19, 2010
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
ctx->isfail = ctx->out_of_memory = 1;
} // out_of_memory
static inline void *Malloc(Context *ctx, const size_t len)
{
void *retval = ctx->malloc((int) len, ctx->malloc_data);
if (retval == NULL)
out_of_memory(ctx);
return retval;
} // Malloc
static inline char *StrDup(Context *ctx, const char *str)
{
char *retval = (char *) Malloc(ctx, strlen(str) + 1);
if (retval != NULL)
strcpy(retval, str);
return retval;
} // StrDup
static inline void Free(Context *ctx, void *ptr)
{
if (ptr != NULL) // check for NULL in case of dumb free() impl.
ctx->free(ptr, ctx->malloc_data);
} // Free
Feb 21, 2010
Feb 21, 2010
617
618
619
620
static void fail(Context *ctx, const char *str)
{
// !!! FIXME: placeholder.
(void) ctx;
Feb 21, 2010
Feb 21, 2010
621
printf("%s:%u: %s\n", ctx->sourcefile, ctx->sourceline, str);
Feb 21, 2010
Feb 21, 2010
622
} // fail
Feb 19, 2010
Feb 19, 2010
623
Feb 22, 2010
Feb 22, 2010
624
Oct 19, 2010
Oct 19, 2010
625
static void symbolmap_nuke(const void *k, const void *v, void *d) {/*no-op*/}
Feb 22, 2010
Feb 22, 2010
626
Oct 19, 2010
Oct 19, 2010
627
static int create_symbolmap(Context *ctx, SymbolMap *map)
Feb 22, 2010
Feb 22, 2010
628
{
Oct 19, 2010
Oct 19, 2010
629
// !!! FIXME: should compare string pointer, with string in cache.
Feb 22, 2010
Feb 22, 2010
630
map->scope = NULL;
Oct 19, 2010
Oct 19, 2010
631
632
633
634
map->hash = hash_create(ctx, hash_hash_string, hash_keymatch_string,
symbolmap_nuke, 1, ctx->malloc, ctx->free,
ctx->malloc_data);
if (!map->hash)
Feb 22, 2010
Feb 22, 2010
635
636
637
638
639
640
{
out_of_memory(ctx);
return 0;
} // if
return 1;
Oct 19, 2010
Oct 19, 2010
641
} // create_symbolmap
Feb 22, 2010
Feb 22, 2010
642
643
Oct 19, 2010
Oct 19, 2010
644
645
646
647
static void push_symbol(Context *ctx, SymbolMap *map,
const char *sym, const char *datatype)
{
SymbolScope *item = (SymbolScope *) Malloc(ctx, sizeof (SymbolScope));
Feb 22, 2010
Feb 22, 2010
648
649
650
651
652
if (item == NULL)
return;
if (sym != NULL)
{
Oct 19, 2010
Oct 19, 2010
653
if (hash_insert(map->hash, sym, datatype) == -1)
Feb 22, 2010
Feb 22, 2010
654
655
656
657
658
659
660
661
662
663
{
Free(ctx, item);
return;
}
} // 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
664
665
666
667
668
} // 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
669
670
} // push_usertype
Oct 19, 2010
Oct 19, 2010
671
672
673
674
675
676
677
678
679
680
681
682
683
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
684
{
Oct 19, 2010
Oct 19, 2010
685
SymbolScope *item = map->scope;
Feb 22, 2010
Feb 22, 2010
686
687
688
if (!item)
return;
if (item->symbol)
Oct 19, 2010
Oct 19, 2010
689
hash_remove(map->hash, item->symbol);
Feb 22, 2010
Feb 22, 2010
690
691
map->scope = item->next;
Free(ctx, item);
Oct 19, 2010
Oct 19, 2010
692
} // pop_symbol
Feb 22, 2010
Feb 22, 2010
693
Oct 19, 2010
Oct 19, 2010
694
static void pop_symbol_scope(Context *ctx, SymbolMap *map)
Feb 22, 2010
Feb 22, 2010
695
696
{
while ((map->scope) && (map->scope->symbol))
Oct 19, 2010
Oct 19, 2010
697
pop_symbol(ctx, map);
Feb 22, 2010
Feb 22, 2010
698
699
700
assert(map->scope != NULL);
assert(map->scope->symbol == NULL);
Oct 19, 2010
Oct 19, 2010
701
702
703
704
705
706
707
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
708
709
} // push_scope
Oct 19, 2010
Oct 19, 2010
710
static void destroy_symbolmap(Context *ctx, SymbolMap *map)
Feb 22, 2010
Feb 22, 2010
711
712
{
while (map->scope)
Oct 19, 2010
Oct 19, 2010
713
714
715
pop_symbol_scope(ctx, map);
hash_destroy(map->hash);
} // destroy_symbolmap
Feb 22, 2010
Feb 22, 2010
716
717
Feb 19, 2010
Feb 19, 2010
718
719
720
721
722
// 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
723
#define NEW_AST_NODE(retval, cls, typ) \
Mar 3, 2010
Mar 3, 2010
724
cls *retval = (cls *) Malloc(ctx, sizeof (cls)); \
Feb 22, 2010
Feb 22, 2010
725
726
727
728
729
730
731
732
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
733
734
735
736
737
738
if (!cls) return; \
} while (0)
static void delete_compilation_unit(Context *ctx, CompilationUnit *unit);
static void delete_statement(Context *ctx, Statement *stmt);
Feb 20, 2010
Feb 20, 2010
739
740
741
static Expression *new_constructor_expr(Context *ctx, const char *datatype,
Expression *args)
{
Feb 22, 2010
Feb 22, 2010
742
NEW_AST_NODE(retval, ExpressionConstructor, AST_OP_CONSTRUCTOR);
Feb 20, 2010
Feb 20, 2010
743
744
745
746
747
748
749
750
retval->datatype = datatype;
retval->args = args;
return (Expression *) retval;
} // new_constructor_expr
static Expression *new_cast_expr(Context *ctx, const char *datatype,
Expression *operand)
{
Feb 22, 2010
Feb 22, 2010
751
NEW_AST_NODE(retval, ExpressionCast, AST_OP_CAST);
Feb 20, 2010
Feb 20, 2010
752
753
754
755
756
retval->datatype = datatype;
retval->operand = operand;
return (Expression *) retval;
} // new_cast_expr
Feb 22, 2010
Feb 22, 2010
757
static Expression *new_unary_expr(Context *ctx, const ASTNodeType op,
Feb 9, 2010
Feb 9, 2010
758
759
Expression *operand)
{
Feb 22, 2010
Feb 22, 2010
760
NEW_AST_NODE(retval, ExpressionUnary, op);
Feb 9, 2010
Feb 9, 2010
761
762
763
764
765
assert(operator_is_unary(op));
retval->operand = operand;
return (Expression *) retval;
} // new_unary_expr
Feb 22, 2010
Feb 22, 2010
766
static Expression *new_binary_expr(Context *ctx, const ASTNodeType op,
Feb 9, 2010
Feb 9, 2010
767
768
Expression *left, Expression *right)
{
Feb 22, 2010
Feb 22, 2010
769
NEW_AST_NODE(retval, ExpressionBinary, op);
Feb 9, 2010
Feb 9, 2010
770
771
772
773
774
775
assert(operator_is_binary(op));
retval->left = left;
retval->right = right;
return (Expression *) retval;
} // new_binary_expr
Feb 22, 2010
Feb 22, 2010
776
static Expression *new_ternary_expr(Context *ctx, const ASTNodeType op,
Feb 9, 2010
Feb 9, 2010
777
778
779
Expression *left, Expression *center,
Expression *right)
{
Feb 22, 2010
Feb 22, 2010
780
NEW_AST_NODE(retval, ExpressionTernary, op);
Feb 9, 2010
Feb 9, 2010
781
782
783
784
785
786
787
788
789
assert(operator_is_ternary(op));
retval->left = left;
retval->center = center;
retval->right = right;
return (Expression *) retval;
} // new_ternary_expr
static Expression *new_identifier_expr(Context *ctx, const char *string)
{
Feb 22, 2010
Feb 22, 2010
790
NEW_AST_NODE(retval, ExpressionIdentifier, AST_OP_IDENTIFIER);
Feb 9, 2010
Feb 9, 2010
791
792
793
794
795
796
retval->identifier = string; // cached; don't copy string.
return (Expression *) retval;
} // new_identifier_expr
static Expression *new_literal_int_expr(Context *ctx, const int64 value)
{
Feb 22, 2010
Feb 22, 2010
797
NEW_AST_NODE(retval, ExpressionIntLiteral, AST_OP_INT_LITERAL);
Feb 9, 2010
Feb 9, 2010
798
799
800
801
802
803
retval->value = value;
return (Expression *) retval;
} // new_literal_int_expr
static Expression *new_literal_float_expr(Context *ctx, const double dbl)
{
Feb 22, 2010
Feb 22, 2010
804
NEW_AST_NODE(retval, ExpressionFloatLiteral, AST_OP_FLOAT_LITERAL);
Feb 9, 2010
Feb 9, 2010
805
806
807
808
809
810
retval->value = dbl;
return (Expression *) retval;
} // new_literal_float_expr
static Expression *new_literal_string_expr(Context *ctx, const char *string)
{
Feb 22, 2010
Feb 22, 2010
811
NEW_AST_NODE(retval, ExpressionStringLiteral, AST_OP_STRING_LITERAL);
Feb 9, 2010
Feb 9, 2010
812
813
retval->string = string; // cached; don't copy string.
return (Expression *) retval;
Oct 20, 2010
Oct 20, 2010
814
815
816
817
818
819
820
821
} // new_literal_string_expr
static Expression *new_literal_boolean_expr(Context *ctx, const int value)
{
NEW_AST_NODE(retval, ExpressionBooleanLiteral, AST_OP_BOOLEAN_LITERAL);
retval->value = value;
return (Expression *) retval;
} // new_literal_boolean_expr
Feb 9, 2010
Feb 9, 2010
822
Feb 19, 2010
Feb 19, 2010
823
824
825
static void delete_expr(Context *ctx, Expression *expr)
{
DELETE_AST_NODE(expr);
Feb 22, 2010
Feb 22, 2010
826
if (operator_is_unary(expr->ast.type))
Feb 19, 2010
Feb 19, 2010
827
828
829
830
{
const ExpressionUnary *unary = (const ExpressionUnary *) expr;
delete_expr(ctx, unary->operand);
} // if
Feb 22, 2010
Feb 22, 2010
831
else if (operator_is_binary(expr->ast.type))
Feb 19, 2010
Feb 19, 2010
832
833
834
835
836
{
const ExpressionBinary *binary = (const ExpressionBinary *) expr;
delete_expr(ctx, binary->left);
delete_expr(ctx, binary->right);
} // else if
Feb 22, 2010
Feb 22, 2010
837
else if (operator_is_ternary(expr->ast.type))
Feb 19, 2010
Feb 19, 2010
838
839
840
841
842
843
{
const ExpressionTernary *ternary = (const ExpressionTernary *) expr;
delete_expr(ctx, ternary->left);
delete_expr(ctx, ternary->center);
delete_expr(ctx, ternary->right);
} // else if
Feb 22, 2010
Feb 22, 2010
844
else if (expr->ast.type == AST_OP_CAST)
Feb 20, 2010
Feb 20, 2010
845
846
847
{
delete_expr(ctx, ((ExpressionCast *) expr)->operand);
} // else if
Feb 22, 2010
Feb 22, 2010
848
else if (expr->ast.type == AST_OP_CONSTRUCTOR)
Feb 20, 2010
Feb 20, 2010
849
850
851
{
delete_expr(ctx, ((ExpressionConstructor *) expr)->args);
} // else if
Feb 22, 2010
Feb 22, 2010
852
853
854
// rest of operators don't have extra data to free.
Feb 19, 2010
Feb 19, 2010
855
856
857
858
859
860
861
862
863
864
865
Free(ctx, expr);
} // delete_expr
static FunctionArguments *new_function_arg(Context *ctx,
const InputModifier inputmod,
const char *datatype,
const char *identifier,
const char *semantic,
const InterpolationModifier interpmod,
Expression *initializer)
{
Feb 22, 2010
Feb 22, 2010
866
NEW_AST_NODE(retval, FunctionArguments, AST_FUNCTION_ARGS);
Feb 19, 2010
Feb 19, 2010
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
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;
} // new_function_arg
static void delete_function_args(Context *ctx, FunctionArguments *args)
{
DELETE_AST_NODE(args);
delete_function_args(ctx, args->next);
delete_expr(ctx, args->initializer);
Free(ctx, args);
} // delete_function_args
static FunctionSignature *new_function_signature(Context *ctx,
const char *datatype,
const char *identifier,
FunctionArguments *args)
{
Feb 22, 2010
Feb 22, 2010
890
NEW_AST_NODE(retval, FunctionSignature, AST_FUNCTION_SIGNATURE);
Feb 19, 2010
Feb 19, 2010
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
retval->datatype = datatype;
retval->identifier = identifier;
retval->args = args;
retval->storage_class = FNSTORECLS_NONE;
retval->semantic = NULL;
return retval;
} // new_function_signature
static void delete_function_signature(Context *ctx, FunctionSignature *sig)
{
DELETE_AST_NODE(sig);
delete_function_args(ctx, sig->args);
Free(ctx, sig);
} // delete_function_signature
static CompilationUnit *new_function(Context *ctx,
FunctionSignature *declaration,
Statement *definition)
{
Feb 22, 2010
Feb 22, 2010
910
NEW_AST_NODE(retval, CompilationUnitFunction, AST_COMPUNIT_FUNCTION);
Feb 19, 2010
Feb 19, 2010
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
retval->next = NULL;
retval->declaration = declaration;
retval->definition = definition;
return (CompilationUnit *) retval;
} // new_function
static void delete_function(Context *ctx, CompilationUnitFunction *unitfn)
{
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
static ScalarOrArray *new_scalar_or_array(Context *ctx, const char *ident,
const int isvec, Expression *dim)
{
Feb 22, 2010
Feb 22, 2010
929
NEW_AST_NODE(retval, ScalarOrArray, AST_SCALAR_OR_ARRAY);
Feb 19, 2010
Feb 19, 2010
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
retval->identifier = ident;
retval->isarray = isvec;
retval->dimension = dim;
return retval;
} // new_scalar_or_array
static void delete_scalar_or_array(Context *ctx, ScalarOrArray *soa)
{
DELETE_AST_NODE(soa);
delete_expr(ctx, soa->dimension);
Free(ctx, soa);
} // delete_scalar_or_array
static Typedef *new_typedef(Context *ctx, int isconst, const char *datatype,
ScalarOrArray *soa)
{
Feb 22, 2010
Feb 22, 2010
946
NEW_AST_NODE(retval, Typedef, AST_TYPEDEF);
Feb 19, 2010
Feb 19, 2010
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
retval->isconst = isconst;
retval->datatype = datatype;
retval->details = soa;
return retval;
} // new_typedef
static void delete_typedef(Context *ctx, Typedef *td)
{
DELETE_AST_NODE(td);
delete_scalar_or_array(ctx, td->details);
Free(ctx, td);
} // delete_typedef
static PackOffset *new_pack_offset(Context *ctx, const char *a, const char *b)
{
Feb 22, 2010
Feb 22, 2010
962
NEW_AST_NODE(retval, PackOffset, AST_PACK_OFFSET);
Feb 19, 2010
Feb 19, 2010
963
964
965
966
967
968
969
970
971
972
973
974
975
976
retval->ident1 = a;
retval->ident2 = b;
return retval;
} // new_pack_offset
static void delete_pack_offset(Context *ctx, PackOffset *o)
{
DELETE_AST_NODE(o);
Free(ctx, o);
} // delete_pack_offset
static VariableLowLevel *new_variable_lowlevel(Context *ctx, PackOffset *po,
const char *reg)
{
Feb 22, 2010
Feb 22, 2010
977
NEW_AST_NODE(retval, VariableLowLevel, AST_VARIABLE_LOWLEVEL);
Feb 19, 2010
Feb 19, 2010
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
retval->packoffset = po;
retval->register_name = reg;
return retval;
} // new_variable_lowlevel
static void delete_variable_lowlevel(Context *ctx, VariableLowLevel *vll)
{
DELETE_AST_NODE(vll);
delete_pack_offset(ctx, vll->packoffset);
Free(ctx, vll);
} // delete_variable_lowlevel
static Annotations *new_annotation(Context *ctx, const char *datatype,
Expression *initializer)
{
Feb 22, 2010
Feb 22, 2010
993
NEW_AST_NODE(retval, Annotations, AST_ANNOTATION);
Feb 19, 2010
Feb 19, 2010
994
995
996
997
998
999
1000
retval->datatype = datatype;
retval->initializer = initializer;
retval->next = NULL;
return retval;
} // new_annotation
static void delete_annotation(Context *ctx, Annotations *annotations)