-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
ast.h
359 lines (287 loc) · 8.01 KB
/
ast.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
162
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#ifndef SPIN_AST_H
#define SPIN_AST_H
#include <stdint.h>
typedef struct LineInfo {
const char *fileName;
int lineno;
char *linedata;
} LineInfo;
/*
* types of data which may be contained within an AST node
*/
union ASTdata {
uint64_t ival; /* unsigned integer value */
const char *string; /* string value */
void *ptr; /* generic pointer */
};
typedef struct AST AST;
/* AST types */
enum astkind {
AST_UNKNOWN = 0,
AST_LISTHOLDER,
AST_INTEGER,
AST_STRING,
AST_IDENTIFIER = 4,
AST_OPERATOR,
AST_FLOAT,
AST_ASSIGN,
AST_ENUMSET = 8,
AST_ARRAYDECL,
AST_BYTELIST,
AST_WORDLIST,
AST_LONGLIST = 12,
AST_INTTYPE,
AST_UNSIGNEDTYPE,
AST_ARRAYTYPE,
AST_FUNCDECL = 16,
AST_FUNCDEF,
AST_FUNCVARS,
AST_STMTLIST,
AST_INSTR = 20,
AST_HWREG,
AST_RETURN,
AST_IF,
AST_THENELSE = 24,
AST_RANGE,
AST_RANGEREF,
AST_FUNCCALL,
AST_EXPRLIST = 28,
AST_INSTRHOLDER,
AST_INSTRMODIFIER,
AST_ORG,
AST_HERE = 32,
AST_POSTSET,
AST_WHILE,
AST_DOWHILE,
AST_FOR = 36,
AST_MEMREF,
AST_ARRAYREF,
AST_COUNTREPEAT,
AST_CASE = 40,
AST_CASEITEM,
AST_OTHER,
AST_RES,
AST_FROM = 44,
AST_TO,
AST_STEP,
AST_FIT,
AST_ADDROF = 48,
AST_LOOKUP,
AST_LOOKDOWN,
AST_OBJECT,
AST_METHODREF = 52,
AST_CONSTREF,
AST_OBJDECL,
AST_STRINGPTR,
AST_YIELD = 56,
AST_CONSTANT,
AST_QUITLOOP,
AST_CONTINUE,
AST_RESULT = 60,
AST_ROUND,
AST_TRUNC,
AST_TOFLOAT,
AST_FILE = 64,
AST_THROW,
AST_CATCH,
AST_LOOKEXPR,
AST_DATADDROF = 68,
AST_ANNOTATION,
AST_TEMPARRAYDECL,
AST_TEMPARRAYUSE,
AST_PUBFUNC = 72,
AST_PRIFUNC,
AST_FUNCHOLDER,
AST_ENUMSKIP,
AST_LINEBREAK = 76,
AST_COMMENT,
AST_COMMENTEDNODE,
AST_COGINIT,
AST_SPRREF = 80,
AST_ABSADDROF,
AST_FLOATTYPE,
AST_PTRTYPE,
AST_GENERICTYPE = 84,
AST_VOIDTYPE,
AST_SEQUENCE,
AST_CONDRESULT,
AST_FORATLEASTONCE = 88,
AST_ISBETWEEN, /* left is between two values on right */
AST_INLINEASM,
AST_OPERAND, /* used in ASM backend, ptr is operand */
AST_ORGH = 92,
AST_MASKMOVE,
AST_MODIFIER_CONST,
AST_MODIFIER_VOLATILE,
AST_IMMHOLDER = 96,
AST_BIGIMMHOLDER,
AST_ALIGN,
AST_ORGF,
AST_TUPLE_TYPE = 100,
AST_SRCCOMMENT,
AST_DECLARE_VAR,
AST_DECLARE_VAR_WEAK, // like AST_DECLARE_VAR, but no error if already defined
AST_LABEL = 104,
AST_GOTO,
AST_PRINT,
AST_CHAR,
AST_REGPAIR = 108,
AST_FUNCTYPE,
AST_SELF,
AST_BITVALUE, // a generic bit value, e.g. NIL is BITVALUE(0)
AST_NEW = 112,
AST_DELETE,
AST_USING,
AST_GLOBALVARS,
AST_LAMBDA = 116,
AST_SETJMP,
AST_TRYENV,
AST_CATCHRESULT,
AST_SIZEOF = 120,
AST_CAST,
AST_VARARGS,
AST_ALLOCA,
AST_SCOPE = 124,
AST_EXTERN,
AST_STATIC,
AST_TYPEDEF,
AST_SYMBOL = 128,
AST_VA_START,
AST_VA_ARG,
AST_DECLARE_ALIAS,
AST_STRUCT = 132,
AST_UNION,
AST_SIMPLEFUNCPTR,
AST_READ,
AST_GOSUB = 136,
AST_SUPER,
AST_LOCAL_IDENTIFIER,
AST_COMPRESS_INSTR,
AST_BITFIELD = 140,
AST_CASETABLE,
AST_JUMPTABLE,
AST_FUNC_TEMPLATE,
AST_CLASS_TEMPLATE = 144, // reserved for future use
AST_ENDCASE,
AST_REFTYPE, // like a ptrtype, but transparent
AST_COPYREFTYPE, // first the value is copied, then a reference passed; used for passing large structs
AST_EMPTY = 148,
AST_MODIFIER_SEND_ARGS = 149,
AST_FVAR_LIST = 150,
AST_FVARS_LIST = 151,
AST_INITMODIFIER = 152,
AST_DECLARE_BITFIELD = 153,
AST_GETLOW = 154,
AST_GETHIGH = 155,
AST_FUNC_NAME = 156,
AST_CASEEXPR = 157, // Represents the hidden variable in a bytecode CASE. Very terrible hack.
AST_BYTECODE = 158, // a raw bytecode opcode, used in sys/
AST_SAMETYPES = 159, // BASIC compile time type checking _sametypes(T, long)
AST_HASMETHOD = 160, // BASIC compile time checking, _hasmethod(T, identifier)
AST_BRKDEBUG = 161, // Opaque debug break.
AST_BYTEFITLIST = 162,
AST_WORDFITLIST = 163,
AST_REGISTER = 164,
AST_REGISTERVARS = 165,
AST_TYPEOF = 166,
AST_ASSIGN_INIT = 167,
AST_HERE_IMM = 168,
AST_FIELDADDR = 169,
AST_FIELDREF = 170,
AST_ASM_IF = 171,
AST_ASM_ELSEIF = 172,
AST_ASM_ENDIF = 173,
AST_EXPECT = 174,
AST_PRINTDEBUG = 175,
AST_ERRHOLDER = 176,
AST_SIGNED_BOOLTYPE = 177,
AST_UNS_BOOLTYPE = 178,
AST_BYTEPTR = 179,
AST_STATIC_ASSERT = 180,
};
/* forward reference */
typedef struct lexstream LexStream;
struct AST {
enum astkind kind; /* type of this node */
union ASTdata d; /* data in this node */
AST *left;
AST *right;
// debug info
LexStream *lexdata; /* points to the current lexer */
int lineidx; /* index within the lexer LineInfo struct */
};
typedef struct ASTReportInfo {
LexStream *lexdata;
int lineidx;
} ASTReportInfo;
/* function declarations */
AST *NewAST(enum astkind kind, AST *left, AST *right);
AST *AddToList(AST *list, AST *newelement);
AST *AddToLeftList(AST *list, AST *newelement);
AST *AddToListEx(AST *list, AST *newelement, AST **tail);
AST *ListInsertBefore(AST *list, AST *member, AST *newelem);
AST *AppendList(AST *list, AST *newlist);
void RemoveFromList(AST **listptr, AST *newelement);
AST *DupAST(AST *ast);
AST *DupASTTypeSafe(AST *ast);
AST *DupASTWithReplace(AST *ast, AST *orig, AST *replace);
AST *AstInteger(int64_t intval);
AST *AstFloat(float f);
AST *AstBitValue(int64_t intval);
AST *AstPlainString(const char *string);
AST *AstStringPtr(const char *string);
const char *GetStringFromAst(AST *ast);
AST *AstIdentifier(const char *name);
AST *AstSymbol(void *sym);
AST *AstTempIdentifier(const char *name);
AST *AstInstrModifier(int32_t intval);
AST *AstOperator(int32_t intval, AST *left, AST *right);
AST *AstOpAssign(int32_t intval, AST *left, AST *right);
AST *AstAssign(AST *left, AST *right);
AST *AstDeclareLocal(AST *left, AST *right);
AST *AstTempVariable(const char *prefix);
AST *AstTempLocalVariable(const char *prefix, AST *type);
int IsAstTempVariable(AST *var);
AST *AstLookup(enum astkind kind, int index, AST *expr, AST *table);
/* check to see if two trees are identical */
int AstMatch(AST *a, AST *b);
/* see if two function bodies are identical, up to identifier renames */
int AstBodyMatch(AST *a, AST *b);
/* check to see if a tree is a subtree of another */
int AstUses(AST *big, AST *sub);
/* check to see if identifier AST *id is modified inside *body */
int AstModifiesIdentifier(AST *body, AST *id);
/* length of an AST list */
int AstListLen(AST *a);
/* length of an AST stringptr list */
int AstStringLen(AST *a);
/* merge two AST string lists */
AST *AstMergeStrings(AST *lhs, AST *rhs);
/* mark new ASTs to be created to have the same line as AST old */
/* used when we're transforming ASTs */
void AstReportAs(AST *old, ASTReportInfo *save);
void AstReportDone(ASTReportInfo *save);
/* print out an AST */
void DumpAST(AST *);
/* get LineInfo for an AST */
LineInfo *GetLineInfo(AST *);
/* create a dummy AST to report an error on line lineNo of the current file */
AST *DummyLineAst(int lineNum);
/* useful utilities for language parsers */
AST *NewCommentedAST(enum astkind kind, AST *left, AST *right, AST *comment);
AST *NewStatement(AST *stmt);
AST *NewCommentedStatement(AST *stmt);
AST *AstReturn(AST *expr, AST *comment);
AST *AstAssignList(AST *dest, AST *expr, AST *comment);
AST *AstYield(void);
AST *AstAbort(AST *expr, AST *comment);
AST *AstCatch(AST *expr);
AST *AstSprRef(AST *index, int ormask);
AST *CheckYield(AST *loopbody);
void ReplaceAst(AST *body, AST *old, AST *newast);
// checks for a list with just one element, and returns that element
AST *ExpectOneListElem(AST *list);
// turn an AST into a no-op
void AstNullify(AST *ptr);
#define IsIdentifier(ast) ((ast)->kind == AST_IDENTIFIER || ((ast)->kind == AST_LOCAL_IDENTIFIER))
#endif