-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmimicc.h
377 lines (345 loc) · 11.2 KB
/
mimicc.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#ifndef HEADER_MIMICC_H
#define HEADER_MIMICC_H
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#define REG_ARGS_MAX_COUNT (6)
#define ONE_WORD_BYTES (8)
#define errorUnreachable() error("%s:%d: Internal error: unreachable", __FILE__, __LINE__)
#define runtimeAssert(expr) \
do { \
if (!(expr)) \
error("%d:%d: Internal error: assert: %s", __FILE__, __LINE__, #expr); \
} while (0)
#define safeFree(p) \
do { \
if (p) { \
free(p); \
p = NULL; \
} \
} while (0)
typedef struct LiteralString LiteralString;
typedef struct Obj Obj;
typedef struct Struct Struct;
typedef struct Enum Enum;
typedef struct Function Function;
typedef struct Typedef Typedef;
typedef struct Token Token;
typedef struct FilePath FilePath;
#define TypePtrdiff_t TypeInt
#define Ptrdiff_t Int
typedef enum {
TypeNone, // No type (block, if, for, while, ...)
TypeVoid, // `void`
TypeInt, // `int`
TypeChar, // `char`
TypeNumber, // Literal number
TypeArray,
TypePointer,
TypeStruct, // Struct
TypeEnum, // Enum
TypeFunction,
} TypeKind;
typedef struct TypeInfo TypeInfo;
struct TypeInfo {
TypeInfo *next;
TypeKind type;
TypeInfo *baseType; // Valid when type is TypePointer or TypeArray.
int arraySize;
Function *funcDef; // Valid when type is TypeFunction
Struct *structDef; // Valid when type is TypeStruct
Enum *enumDef; // Valid when type is TypeEnum
};
extern struct Types {
TypeInfo None;
TypeInfo Void;
TypeInfo Int;
TypeInfo Char;
TypeInfo Number;
} Types;
typedef enum {
TokenReserved,
TokenTypeName,
TokenIdent,
TokenNumber,
TokenStatic,
TokenExtern,
TokenTypedef,
TokenIf,
TokenElseif,
TokenElse,
TokenSwitch,
TokenCase,
TokenDefault,
TokenFor,
TokenWhile,
TokenDo,
TokenBreak,
TokenContinue,
TokenReturn,
TokenSizeof,
TokenLiteralString,
TokenStruct,
TokenEnum,
TokenNewLine,
TokenSOF, // Start of file.
TokenEOF, // End of file.
} TokenType;
struct Token {
TokenType type;
Token *prev;
Token *next;
int val; // Number valid when type is TokenNumber.
TypeKind varType; // Variable type valid when type is TokenTypeName.
LiteralString *literalStr; // Reference to literal string when type is
// TokenLiteralString.
FilePath *file; // File path information
char *str; // The token string.
int len; // The token length.
int line; // Line number in file.
int column; // Column number in line.
};
typedef struct Env Env;
struct Env {
Env *outer;
Struct *structs;
Enum *enums;
Typedef *typedefs;
Obj *vars; // List of variables local to this env.
int varSize; // Total size of variables local to this env.
};
typedef enum {
NodeNop,
NodeAdd, // +
NodeSub, // -
NodeMul, // *
NodeDiv, // /
NodeDivRem, // %
NodeEq, // ==
NodeNeq, // !=
NodeLT, // <
NodeLE, // <=
NodePreIncl, // ++{var}
NodePreDecl, // --{var}
NodePostIncl, // {var}++
NodePostDecl, // {var}--
NodeMemberAccess, // Struct member accessing (struct.member)
NodeAddress, // &{var}
NodeDeref, // *{ptr}
NodeNot, // !
NodeLogicalOR, // ||
NodeLogicalAND, // &&
NodeBitwiseAND, // &
NodeBitwiseOR, // |
NodeBitwiseXOR, // ^
NodeArithShiftL, // << (arithmetic)
NodeArithShiftR, // >> (arithmetic)
NodeNum, // Integer
NodeLiteralString, // literal string
NodeLVar, // Left hand side value (local variable)
NodeAssign, // {lhs} = {rhs};
NodeAssignStruct, // Assignment, but only for structs
NodeFCall, // Function calls,
NodeConditional, // {expr} ? {expr} : {expr}
NodeIf,
NodeElseif,
NodeElse,
NodeSwitch,
NodeSwitchCase,
NodeFor,
NodeDoWhile,
NodeExprList, // Expressions concatenated by commas.
NodeInitVar, // Variable initialization on declaration.
NodeInitList, // Initializer list
NodeBlock, // { ... }
NodeBreak, // break;
NodeContinue, // continue;
NodeReturn, // return {expr};
NodeFunction,
NodeGVar, // Global variable, work as lvar.
NodeTypeCast, // Type casting.
NodeClearStack, // Clear certain range of stack with 0.
NodeVaStart, // Built-in va_args()
} NodeKind;
typedef struct SwitchCase SwitchCase;
typedef struct FCall FCall;
typedef struct Node Node;
struct Node {
NodeKind kind;
Env *env;
Node *lhs;
Node *rhs;
Node *condition; // Used by if/for/while/switch statements.
Node *body; // Used by if/for/while statements, block and functions.
Node *elseblock; // Used by if statement. Holds "else if" and "else" blocks.
Node *initializer; // Used by for statement.
Node *iterator; // Used by for statement.
Node *next; // Next statement in the same block. NULL if next
// statement doesn't exist.
TypeInfo *type; // Type of this node's result value.
Token *token; // Token which gave this node.
FCall *fcall; // Called function information used when kind is NodeFCall.
SwitchCase *cases; // "case" or "default" nodes within switch statement.
Obj *obj;
Obj *parentFunc;
int val; // Used when kind is NodeNum.
int blockID; // Unique ID for jump labels. Valid only when the node
// is control syntax, logical AND, and logical OR.
};
struct FCall {
char *name; // Function name.
int len; // Function name length.
int argsCount; // The number of arguments.
Node *args; // Function arguments in reversed order.
TypeInfo *declType; // Called function's type
};
struct SwitchCase {
SwitchCase *next;
Node *node;
};
struct Function {
int argsCount;
int haveVaArgs; // TRUE if function have variadic arguments
int haveImpl; // TRUE when implementation is given. FALSE if
// only declaration is given.
int capStackSize; // Size of stack which this function uses.
Obj *args; // Function arguments. NOT IN REVERSED ORDER.
TypeInfo *retType; // Type of return value.
};
typedef struct ObjAttr ObjAttr;
struct ObjAttr {
int isStatic;
int isExtern;
int isTypedef;
};
// Struct for objects; variables and functions.
struct Obj {
Obj *next;
Token *token;
TypeInfo *type; // Type of object.
int offset; // Offset from rbp. Variable adress is calculated
// as RBP - offset.
int isExtern; // TRUE if object is declared with "extern".
int isStatic; // TRUE if object is declared with "static".
int staticVarID; // ID for static local variables.
Function *func; // Valid when object holds function.
};
// Struct for global variables.
typedef struct GVar GVar;
typedef struct GVarInit GVarInit;
struct GVar {
GVar *next;
Obj *obj;
GVarInit *initializer;
};
typedef enum {
GVarInitZero,
GVarInitString,
GVarInitNum,
GVarInitPointer,
} GVarInitKind;
struct GVarInit {
GVarInit *next;
GVarInitKind kind;
int size;
Node *rhs;
};
struct LiteralString {
LiteralString *next;
char *string;
int len; // String length in program. (Not on text editor.)
int id;
};
struct Struct {
Struct *next;
Token *tagName;
Obj *members;
int totalSize;
int hasImpl;
};
typedef struct EnumItem EnumItem;
struct Enum {
Enum *next;
Token *tagName;
EnumItem *items;
int hasImpl;
};
struct EnumItem {
EnumItem *next;
Token *token;
int value;
};
struct Typedef {
Typedef *next;
Token *name; // Typedef name.
TypeInfo *type; // Actual type.
};
struct FilePath {
char *basename;
char *dirname; // Parent directory name with '/' at the end.
char *path;
char *display; // File path for display (error message, __FILE__ macro,
// etc.)
};
typedef struct Globals Globals;
struct Globals {
Node *code; // The root node of program.
Env globalEnv;
Obj *functions; // Declared function list.
GVar *globalVars; // Global variable list.
GVar *staticVars; // Static local variable list.
LiteralString *strings; // Literal string list.
Env *currentEnv;
Obj *currentFunction; // Function currently parsed.
int blockCount; // The number of blocks appeared in program.
int literalStringCount; // The number of literal strings appeared in
// program.
int staticVarCount; // The number of variables declared with "static."
int namelessEnumCount; // The number of nameless enums.
int namelessStructCount; // The number of nameless structs.
Token *token; // Token currently watches.
FILE *destFile; // The output file.
FilePath *ccFile; // The binary file path infomation.
char *includePath; // The include path.
};
extern Globals globals;
// main.c
void *safeAlloc(size_t size);
_Noreturn void error(const char *fmt, ...);
_Noreturn void errorAt(Token *loc, const char *fmt, ...);
int dumpc(int c);
int dumps(const char *s);
int dumpf(const char *fmt, ...);
FilePath *analyzeFilepath(const char *path, const char *display);
char *readFile(const char *path);
// codegen.c
void genCode(const Node *n);
void genCodeGlobals(void);
// tokenizer.c
int isSpace(char c);
int checkEscapeChar(char c, char *decoded);
void popTokenRange(Token *begin, Token *end);
void removeAllNewLineToken(Token *token);
void printToken(Token *token);
void printTokenList(Token *token);
Token *tokenize(char *source, FilePath *file);
// preproc.c
void preprocess(Token *token);
// parser.c
void program(void);
Obj *findFunction(const char *name, int len);
Obj *findStructMember(const Struct *s, const char *name, int len);
GVar *findGlobalVar(char *name, int len);
Obj *findLVar(char *name, int len);
int sizeOf(const TypeInfo *ti);
int matchToken(const Token *token, const char *name, const int len);
Node *evalConstantExpr(Node *n);
// verifier.c
void verifyFlow(const Node *n);
void verifyType(const Node *n);
int checkTypeEqual(const TypeInfo *t1, const TypeInfo *t2);
int checkAssignable(const TypeInfo *lhs, const TypeInfo *rhs);
int isLvalue(const Node *n);
int isWorkLikePointer(const TypeInfo *t);
const TypeInfo *getBaseType(const TypeInfo *t);
#endif // HEADER_MIMICC_H