-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
compilium.h
311 lines (279 loc) · 9.37 KB
/
compilium.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
#include "include/stdarg.h"
#include "include/stdbool.h"
#include "include/stdio.h"
#include "include/stdlib.h"
#include "include/string.h"
char *strndup(const char *s, size_t n);
char *strdup(const char *s);
#define assert(expr) \
((void)((expr) || (__assert(#expr, __FILE__, __LINE__), 0)))
enum NodeType {
kNodeNone,
kNodeToken,
kNodeStructMember,
kNodeMacroReplacement,
//
kASTExpr,
kASTExprFuncCall,
kASTList,
kASTExprStmt,
kASTJumpStmt,
kASTSelectionStmt,
kASTIdent,
kASTDirectDecltor,
kASTDecltor,
kASTDecl,
kASTForStmt,
kASTWhileStmt,
kASTFuncDef,
kASTKeyValue,
kASTLocalVar,
kASTStructSpec,
//
kTypeBase,
kTypeLValue,
kTypePointer,
kTypeFunction,
kTypeAttrIdent,
kTypeStruct,
kTypeArray,
};
enum TokenType {
kTokenUnknownChar,
kTokenDelimiter,
kTokenZeroWidthNoBreakSpace,
kTokenIntegerConstant,
kTokenIdent,
kTokenKwBreak,
kTokenKwChar,
kTokenKwConst,
kTokenKwContinue,
kTokenKwElse,
kTokenKwExtern,
kTokenKwFor,
kTokenKwIf,
kTokenKwInt,
kTokenKwLong,
kTokenKwReturn,
kTokenKwSizeof,
kTokenKwStatic,
kTokenKwStruct,
kTokenKwTypedef,
kTokenKwUnsigned,
kTokenKwVoid,
kTokenKwWhile,
kTokenCharLiteral,
kTokenStringLiteral,
kTokenPunctuator,
kTokenLineComment,
kTokenBlockCommentBegin,
kTokenBlockCommentEnd,
};
/*
Node if-stmt:
stmt->cond = cond-expr
stmt->left = true-stmt
stmt->right = false-stmt or null
Node func-call-expr:
expr->func_expr
expr->arg_expr_list
Node expr-stmt:
stmt->op = token(;)
stmt->left = node
*/
struct Node {
enum NodeType type;
int reg;
struct Node *expr_type;
struct Node *op;
struct Node *left;
struct Node *right;
struct Node *init;
struct Node *cond;
struct Node *updt;
struct Node *body;
struct Node *if_true_stmt;
struct Node *if_else_stmt;
struct Node *decltor_init_expr;
struct Node *decltor_init_stmt;
struct Node *struct_member_dict;
struct Node *struct_member_ent_type;
struct Node *struct_member_decl;
int struct_member_ent_ofs;
// for list
int capacity;
int size;
struct Node **nodes;
// for key value
const char *key;
struct Node *value;
// for local var
int byte_offset;
// for string literal
int label_number;
// kASTExprFuncCall
struct Node *func_expr;
struct Node *arg_expr_list;
struct Node *arg_var_list;
int stack_size_needed;
// kASTFuncDef
struct Node *func_body;
struct Node *func_type;
struct Node *func_name_token;
struct Node *tag;
struct Node *type_struct_spec;
struct Node *type_array_type_of;
struct Node *type_array_index_decl;
// kNodeToken
enum TokenType token_type;
struct Node *next_token;
const char *begin;
int length;
const char *src_str;
int line;
};
_Noreturn void Error(const char *fmt, ...);
_Noreturn void __assert(const char *expr_str, const char *file, int line);
void PrintTokenLine(struct Node *t);
_Noreturn void ErrorWithToken(struct Node *t, const char *fmt, ...);
void PushToList(struct Node *list, struct Node *node);
void PushKeyValueToList(struct Node *list, const char *key, struct Node *value);
struct Node *AllocList();
int GetSizeOfList(struct Node *list);
struct Node *GetNodeAt(struct Node *list, int index);
struct Node *GetNodeByTokenKey(struct Node *list, struct Node *key);
extern const char *symbol_prefix;
extern const char *include_path;
#define NUM_OF_SCRATCH_REGS 10
extern const char *reg_names_64[NUM_OF_SCRATCH_REGS + 1];
extern const char *reg_names_32[NUM_OF_SCRATCH_REGS + 1];
extern const char *reg_names_8[NUM_OF_SCRATCH_REGS + 1];
#define NUM_OF_PARAM_REGISTERS 6
extern const char *param_reg_names_64[NUM_OF_PARAM_REGISTERS];
extern const char *param_reg_names_32[NUM_OF_PARAM_REGISTERS];
extern const char *param_reg_names_8[NUM_OF_PARAM_REGISTERS];
// @analyzer.c
struct SymbolEntry *Analyze(struct Node *node);
// @ast.c
bool IsToken(struct Node *n);
bool IsTokenWithType(struct Node *n, enum TokenType type);
bool IsASTList(struct Node *);
bool IsASTDeclOfTypedef(struct Node *n);
bool IsASTDeclOfExtern(struct Node *n);
struct Node *AllocNode(enum NodeType type);
struct Node *CreateASTBinOp(struct Node *t, struct Node *left,
struct Node *right);
struct Node *CreateASTUnaryPrefixOp(struct Node *t, struct Node *right);
struct Node *CreateASTUnaryPostfixOp(struct Node *left, struct Node *t);
struct Node *CreateASTExprStmt(struct Node *t, struct Node *left);
struct Node *CreateASTFuncDef(struct Node *func_decl, struct Node *func_body);
struct Node *CreateASTKeyValue(const char *key, struct Node *value);
struct Node *CreateASTLocalVar(int byte_offset, struct Node *var_type);
struct Node *CreateTypeBase(struct Node *t);
struct Node *CreateTypeLValue(struct Node *type);
struct Node *CreateTypePointer(struct Node *type);
struct Node *CreateTypeFunction(struct Node *return_type,
struct Node *arg_type_list);
struct Node *GetReturnTypeOfFunction(struct Node *);
struct Node *GetArgTypeList(struct Node *func_type);
struct Node *CreateTypeStruct(struct Node *tag_token, struct Node *struct_spec);
struct Node *CreateTypeAttrIdent(struct Node *ident_token, struct Node *type);
struct Node *CreateASTIdent(struct Node *ident);
struct Node *CreateTypeArray(struct Node *type_of, struct Node *index_decl);
struct Node *CreateMacroReplacement(struct Node *args_tokens,
struct Node *to_tokens);
void PrintASTNode(struct Node *n);
// @compilium.c
const char *ReadFile(FILE *fp);
// @generate.c
void Generate(struct Node *ast, struct SymbolEntry *);
// @parser.c
extern struct Node *toplevel_names;
void InitParser(struct Node **);
struct Node *Parse(struct Node **passed_tokens);
// @preprocessor.c
void Preprocess(struct Node **head_holder, struct Node *replacement_list);
// @struct.c
struct SymbolEntry;
int CalcStructSize(struct Node *spec);
int CalcStructAlign(struct Node *spec);
void AddMemberOfStructFromDecl(struct Node *struct_spec, struct Node *decl);
struct Node *FindStructMember(struct Node *struct_type, struct Node *key_token);
void ResolveTypesOfMembersOfStruct(struct SymbolEntry *ctx, struct Node *spec);
// @symbol.c
enum SymbolType {
kSymbolLocalVar,
kSymbolGlobalVar,
kSymbolExternVar,
kSymbolFuncDef,
kSymbolFuncDeclType,
kSymbolStructType,
};
struct SymbolEntry {
enum SymbolType type;
struct SymbolEntry *prev;
const char *key;
struct Node *value;
};
int GetLastLocalVarOffset(struct SymbolEntry *);
struct Node *AddLocalVar(struct SymbolEntry **ctx, const char *key,
struct Node *var_type);
void AddExternVar(struct SymbolEntry **ctx, const char *key,
struct Node *var_type);
void AddGlobalVar(struct SymbolEntry **ctx, const char *key,
struct Node *var_type);
struct Node *FindExternVar(struct SymbolEntry *e, struct Node *key_token);
struct Node *FindGlobalVar(struct SymbolEntry *e, struct Node *key_token);
struct Node *FindLocalVar(struct SymbolEntry *e, struct Node *key_token);
void AddFuncDef(struct SymbolEntry **ctx, const char *key,
struct Node *func_def);
struct Node *FindFuncDef(struct SymbolEntry *e, struct Node *key_token);
void AddFuncDeclType(struct SymbolEntry **ctx, const char *key,
struct Node *func_decl);
struct Node *FindFuncDeclType(struct SymbolEntry *e, struct Node *key_token);
void AddStructType(struct SymbolEntry **, const char *, struct Node *);
struct Node *FindStructType(struct SymbolEntry *, struct Node *);
// @token.c
bool IsToken(struct Node *n);
struct Node *AllocToken(const char *src_str, int line, const char *begin,
int length, enum TokenType type);
struct Node *DuplicateToken(struct Node *base_token);
struct Node *DuplicateTokenSequence(struct Node *base_head);
char *CreateTokenStr(struct Node *t);
int IsEqualTokenWithCStr(struct Node *t, const char *s);
void PrintTokenSequence(struct Node *t);
void OutputTokenSequenceAsCSource(struct Node *t);
void PrintToken(struct Node *t);
void PrintTokenBrief(struct Node *t);
void PrintTokenStrToFile(struct Node *t, FILE *fp);
void InitTokenStream(struct Node **head_token);
struct Node *PeekToken(void);
struct Node *ReadToken(enum TokenType type);
struct Node *ConsumeToken(enum TokenType type);
struct Node *ConsumeTokenStr(const char *s);
struct Node *ExpectTokenStr(const char *s);
struct Node *ConsumePunctuator(const char *s);
struct Node *ExpectPunctuator(const char *s);
struct Node *NextToken(void);
void RemoveCurrentToken(void);
void RemoveTokensTo(struct Node *end);
void InsertTokens(struct Node *);
void InsertTokensWithIdentReplace(struct Node *seq, struct Node *rep_list);
struct Node **RemoveDelimiterTokens(struct Node **);
// @tokenizer.c
struct Node *CreateToken(const char *input);
struct Node *Tokenize(const char *input);
// @type.c
int IsSameTypeExceptAttr(struct Node *a, struct Node *b);
int IsLValueType(struct Node *t);
struct Node *GetTypeWithoutAttr(struct Node *t);
struct Node *GetIdentifierTokenFromTypeAttr(struct Node *t);
struct Node *GetRValueType(struct Node *t);
int GetSizeOfType(struct Node *t);
int GetAlignOfType(struct Node *t);
struct Node *CreateTypeInContext(struct SymbolEntry *ctx,
struct Node *decl_spec, struct Node *decltor);
struct Node *CreateType(struct Node *decl_spec, struct Node *decltor);
struct Node *CreateTypeFromDecl(struct Node *decl);
struct Node *CreateTypeFromDeclInContext(struct SymbolEntry *ctx,
struct Node *decl);