-
Notifications
You must be signed in to change notification settings - Fork 155
/
9cc.h
432 lines (352 loc) · 7.44 KB
/
9cc.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#define _GNU_SOURCE
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include <string.h>
/// util.c
noreturn void error(char *fmt, ...) __attribute__((format(printf, 1, 2)));
char *format(char *fmt, ...) __attribute__((format(printf, 1, 2)));
typedef struct {
void **data;
int capacity;
int len;
} Vector;
Vector *new_vec(void);
void vec_push(Vector *v, void *elem);
void vec_pushi(Vector *v, int val);
void *vec_pop(Vector *v);
void *vec_last(Vector *v);
bool vec_contains(Vector *v, void *elem);
bool vec_union1(Vector *v, void *elem);
typedef struct {
Vector *keys;
Vector *vals;
} Map;
Map *new_map(void);
void map_put(Map *map, char *key, void *val);
void map_puti(Map *map, char *key, int val);
void *map_get(Map *map, char *key);
int map_geti(Map *map, char *key, int default_);
bool map_exists(Map *map, char *key);
typedef struct {
char *data;
int capacity;
int len;
} StringBuilder;
StringBuilder *new_sb(void);
void sb_add(StringBuilder *sb, char c);
void sb_append(StringBuilder *sb, char *s);
void sb_append_n(StringBuilder *sb, char *s, int len);
char *sb_get(StringBuilder *sb);
typedef struct Type Type;
typedef struct Type {
int ty;
int size; // sizeof
int align; // alignof
// Pointer
Type *ptr_to;
// Array
Type *ary_of;
int len;
// Struct
Map *members;
int offset;
// Function
Type *returning;
} Type;
Type *ptr_to(Type *base);
Type *ary_of(Type *base, int len);
Type *void_ty();
Type *bool_ty();
Type *char_ty();
Type *int_ty();
Type *func_ty(Type *returning);
bool same_type(Type *x, Type *y);
int roundup(int x, int align);
/// util_test.c
void util_test();
/// token.c
enum {
TK_NUM = 256, // Number literal
TK_STR, // String literal
TK_IDENT, // Identifier
TK_ARROW, // ->
TK_EXTERN, // "extern"
TK_TYPEDEF, // "typedef"
TK_INT, // "int"
TK_CHAR, // "char"
TK_VOID, // "void"
TK_STRUCT, // "struct"
TK_BOOL, // "_Bool"
TK_IF, // "if"
TK_ELSE, // "else"
TK_FOR, // "for"
TK_DO, // "do"
TK_WHILE, // "while"
TK_SWITCH, // "switch"
TK_CASE, // "case"
TK_BREAK, // "break"
TK_CONTINUE, // "continue"
TK_EQ, // ==
TK_NE, // !=
TK_LE, // <=
TK_GE, // >=
TK_LOGOR, // ||
TK_LOGAND, // &&
TK_SHL, // <<
TK_SHR, // >>
TK_INC, // ++
TK_DEC, // --
TK_MUL_EQ, // *=
TK_DIV_EQ, // /=
TK_MOD_EQ, // %=
TK_ADD_EQ, // +=
TK_SUB_EQ, // -=
TK_SHL_EQ, // <<=
TK_SHR_EQ, // >>=
TK_AND_EQ, // &=
TK_XOR_EQ, // ^=
TK_OR_EQ, // |=
TK_RETURN, // "return"
TK_SIZEOF, // "sizeof"
TK_ALIGNOF, // "_Alignof"
TK_TYPEOF, // "typeof"
TK_PARAM, // Function-like macro parameter
TK_EOF, // End marker
};
// Token type
typedef struct {
int ty; // Token type
int val; // Number literal
char *name; // Identifier
// String literal
char *str;
char len;
// For preprocessor
bool stringize;
// For error reporting
char *buf;
char *path;
char *start;
char *end;
} Token;
Vector *tokenize(char *path, bool add_eof);
noreturn void bad_token(Token *t, char *msg);
void warn_token(Token *t, char *msg);
int get_line_number(Token *t);
char *stringize(Vector *tokens);
/// preprocess.c
Vector *preprocess(Vector *tokens);
/// parse.c
extern int nlabel;
enum {
ND_NUM = 256, // Number literal
ND_STRUCT, // Struct
ND_DECL, // declaration
ND_VARDEF, // Variable definition
ND_VARREF, // Variable reference
ND_CAST, // Cast
ND_IF, // "if"
ND_FOR, // "for"
ND_DO_WHILE, // do ... while
ND_SWITCH, // switch
ND_CASE, // case
ND_BREAK, // break
ND_CONTINUE, // continue
ND_ADDR, // address-of operator ("&")
ND_DEREF, // pointer dereference ("*")
ND_DOT, // Struct member access
ND_EQ, // ==
ND_NE, // !=
ND_LE, // <=
ND_LOGAND, // &&
ND_LOGOR, // ||
ND_SHL, // <<
ND_SHR, // >>
ND_MOD, // %
ND_RETURN, // "return"
ND_CALL, // Function call
ND_FUNC, // Function definition
ND_COMP_STMT, // Compound statement
ND_EXPR_STMT, // Expression statement
ND_STMT_EXPR, // Statement expression (GNU extn.)
ND_NULL, // Null statement
};
enum {
VOID = 1,
BOOL,
CHAR,
INT,
PTR,
ARY,
STRUCT,
FUNC,
};
typedef struct Reg Reg;
// Represents a variable.
typedef struct {
Type *ty;
char *name;
bool is_local;
// Local variables are compiled to offsets from RBP.
int offset;
// Global variables are compiled to labels with optional
// initialized data.
char *data;
// For optimization passes.
bool address_taken;
Reg *promoted;
} Var;
typedef struct Node Node;
typedef struct BB BB;
// AST node
typedef struct Node {
int op; // Node type
Type *ty; // C type
Node *lhs; // left-hand side
Node *rhs; // right-hand side
int val; // Number literal
Node *expr; // "return" or expresson stmt
Vector *stmts; // Compound statement
char *name;
// For ND_VARREF
Var *var;
// "if" ( cond ) then "else" els
// "for" ( init; cond; inc ) body
// "while" ( cond ) body
// "do" body "while" ( cond )
// "switch" ( cond ) body
// "case" val ":" body
Node *cond;
Node *then;
Node *els;
Node *init;
Node *inc;
Node *body;
// For switch and case
Vector *cases;
BB *bb;
// For case, break and continue
Node *target;
BB *break_;
BB *continue_;
// Function definition
Vector *params;
// Function call
Vector *args;
// For error reporting
Token *token;
} Node;
typedef struct {
char *name;
Node *node;
Vector *lvars;
Vector *bbs;
} Function;
// Represents toplevel constructs.
typedef struct {
Vector *gvars;
Vector *funcs;
} Program;
Program *parse(Vector *tokens);
Node *new_int_node(int val, Token *t);
/// sema.c
Type *get_type(Node *node);
void sema(Program *prog);
/// ir_dump.c
void dump_ir(Vector *irv);
/// gen_ir.c
enum {
IR_ADD = 1,
IR_SUB,
IR_MUL,
IR_DIV,
IR_IMM,
IR_BPREL,
IR_MOV,
IR_RETURN,
IR_CALL,
IR_LABEL_ADDR,
IR_EQ,
IR_NE,
IR_LE,
IR_LT,
IR_AND,
IR_OR,
IR_XOR,
IR_SHL,
IR_SHR,
IR_MOD,
IR_JMP,
IR_BR,
IR_LOAD,
IR_LOAD_SPILL,
IR_STORE,
IR_STORE_ARG,
IR_STORE_SPILL,
IR_NOP,
};
typedef struct Reg {
int vn; // virtual register number
int rn; // real register number
// For optimizer
Reg *promoted;
// For regalloc
int def;
int last_use;
bool spill;
Var *var;
} Reg;
typedef struct BB {
int label;
Vector *ir;
Reg *param;
// For liveness analysis
Vector *succ;
Vector *pred;
Vector *def_regs;
Vector *in_regs;
Vector *out_regs;
} BB;
typedef struct {
int op;
Reg *r0;
Reg *r1;
Reg *r2;
int imm;
int label;
Var *var;
BB *bb1;
BB *bb2;
// Load/store size in bytes
int size;
// Function call
char *name;
int nargs;
Reg *args[6];
// For liveness tracking
Vector *kill;
// For SSA
Reg *bbarg;
} IR;
void gen_ir(Program *prog);
Reg *new_reg();
/// opt.c
void optimize(Program *prog);
/// liveness.c
void liveness(Program *prog);
/// liveness.c
void liveness(Program *prog);
/// regalloc.c
void alloc_regs(Program *prog);
/// gen_x86.c
extern char *regs[];
extern char *regs8[];
extern char *regs32[];
extern int num_regs;
void gen_x86(Program *prog);