-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdcc.h
227 lines (196 loc) · 3.49 KB
/
mdcc.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
#ifndef __MDCC_H__
#define __MDCC_H__
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
typedef struct {
void **data;
int capacity;
int len;
} Vector;
typedef struct {
Vector *keys;
Vector *vals;
} Map;
enum {
TK_NUM = 256,
TK_LONG,
TK_INT,
TK_CHAR,
TK_IDENT,
TK_RETURN,
TK_IF,
TK_ELSE,
TK_EQ,
TK_NEQ,
TK_FOR,
TK_WHILE,
TK_SHL,
TK_SHR,
TK_ADD_EQ, // +=
TK_INC, // ++
TK_SUB_EQ, // -=
TK_DEC, // --
TK_MUL_EQ, // *=
TK_DIV_EQ, // /=
TK_SHL_EQ, // <<=
TK_SHR_EQ, // >>=
TK_GEQ, // >=
TK_LEQ, // <=
TK_AND, // &&
TK_BAND_EQ, // &=
TK_OR, // ||
TK_BOR_EQ, // |=
TK_XOR_EQ, // ^=
TK_EOF,
};
enum {
ND_NUM = 256,
ND_COMP_STMT,
ND_IDENT,
ND_CALL,
ND_ADDR,
ND_DEREF,
ND_ROOT,
ND_FUNC,
ND_RETURN,
ND_IF,
ND_EQ,
ND_NEQ,
ND_NULL,
ND_FOR,
ND_SHL,
ND_SHR,
ND_AND,
ND_OR,
ND_INC, // postfix increment
ND_DEC, // postfix decrement
ND_WHILE,
ND_INITS,
};
typedef struct Position {
int offset; // oofset, starting at 0
int line; // line number, starting at 1
} Position;
typedef struct {
int ty; // Token type
char *name; // Operator
int val; // Number value
Position *pos;
} Token;
enum {
TY_INT = 1,
TY_CHAR,
TY_LONG,
TY_PTR,
TY_ARR,
};
typedef struct Type {
int ty;
struct Type *ptr_to;
struct Type *arr_of;
int len; // for array
// Byte size of types
// char: 1, int: 4, ptr: 8
int size;
int align;
} Type;
typedef struct Var {
Type *ty;
char *name;
// Offset from rbp
int offset;
// has_address is true if the variable is passed as
// an pointer-like argument of a function.
bool has_address;
} Var;
struct Function;
/**
* Function definition
* int "name" ("params") {"body"}
*
* Function call
* "name"("args")
*
* If statement
* if ("cond") "then" else "els"
*
* Return statement
* return "expr"
*
* For statement
* for ("init"; "cond"; "after") "body"
*
* While statement
* while ("cond") "body"
*/
typedef struct Node {
int ty; // Node type
struct Node *lhs;
struct Node *rhs;
int val;
Var *var;
Type *cty; // C type.
char *name;
Vector *params;
struct Node *body;
Vector *func_vars;
Vector *args;
struct Node *expr;
struct Node *cond;
struct Node *then;
struct Node *els;
struct Node *init;
struct Node *after;
// Vector of statements
Vector *stmts;
// For the root node
Vector *funcs;
// For array reference
Vector *indice;
// For initializer
Vector *inits;
} Node;
// Basic block
typedef struct BB {
} BB;
// main.c
// util.c
__attribute__((noreturn)) void error(char *fmt, ...);
Vector *new_vec(void);
void vec_push(Vector *v, void *elm);
void *vec_pop(Vector *v);
Map *new_map(void);
void map_set(Map *map, char *key, void *val);
void *map_get(Map *map, char *key);
void *map_get_def(Map *map, char *key, void *defv);
bool isnondigit(char c);
char *format(char *fmt, ...);
int roundup(int x, int align);
Type *new_type(int ty, int size);
Type *ptr(Type *ty);
Node *new_node(int ty, Node *lhs, Node *rhs);
Node *new_node_one(int ty, Node *expr);
Type *new_long_ty();
Type *new_int_ty();
Type *new_char_ty();
Node *new_node_num(int val);
// token.c
extern char *buf;
extern int pos;
Vector *tokenize();
// parse.c
Node *parse();
// conv.c
Node *conv(Node *node);
// x64.c
void gen_x64(Node *node);
// test_util.c
void test();
#endif