-
Notifications
You must be signed in to change notification settings - Fork 0
/
alloycc.h
324 lines (272 loc) · 6.19 KB
/
alloycc.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
#define _POSIX_C_SOURCE 200809L
#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
typedef struct Type Type;
typedef struct Hideset Hideset;
typedef struct Member Member;
typedef struct Relocation Relocation;
//
// tokenize.c
//
typedef enum {
TK_RESERVED,
TK_IDENT,
TK_STR,
TK_NUM,
TK_EOF,
} TokenKind;
typedef struct Token Token;
struct Token {
TokenKind kind; // token kind
Token *next; // next token
long val; // its integer value (used for TK_NUM)
double fval; // its floating point value (used for TK_NUM)
Type *ty; // used if TK_NUM
char *str; // start of token in original input
int len; // token length (in original input)
char *contents; // string literal contents, including '\0' terminator
int cont_len; // string literal length
char *filename; // input filename
char *input; // entire input string
int line_no; // line number: for debugging
int file_no; // file number: for .loc directivbe
bool at_bol; // true if this token is at beginning of line
bool has_space; // true if this token follows a space character
Hideset *hideset; // for macro expansion
};
void error(char *fmt, ...);
void error_at(char *loc, char *fmt, ...);
void error_tok(Token *tok, char *fmt, ...);
void warn_tok(Token *tok, char *fmt, ...);
bool equal(Token *tok, char *op);
bool consume(Token **rest, Token *tok, char *op);
Token *skip(Token *tok, char *op);
char *expect_ident(Token **rest, Token *tok);
char *get_identifier(Token *tok);
char *expect_string(Token **rest, Token *tok);
void convert_keywords(Token *tok);
Token *tokenize(char *filename, int file_no, char *p);
Token *tokenize_file(char *path);
extern char *current_filename;
//
// preprocess.c
//
Token *preprocess(Token *tok);
//
// parser.c
//
typedef enum {
ND_ADD, // +
ND_SUB, // -
ND_MUL, // *
ND_DIV, // /
ND_MOD, // %
ND_ADDR, // unary &
ND_DEREF, // unary *
ND_NOT, // !
ND_BITNOT, // ~
ND_LOGAND, // &&
ND_LOGOR, // ||
ND_BITAND, // &
ND_BITOR, // |
ND_BITXOR, // ^
ND_SHL, // <<
ND_SHR, // >>
ND_ASSIGN, // =
ND_COND, // ? :
ND_COMMA, // ,
ND_MEMBER, // . (struct member)
ND_EQ, // ==
ND_NE, // !=
ND_LT, // <
ND_LE, // <=
ND_IF, // "if"
ND_FOR, // "for" or "while"
ND_DO, // "do"
ND_SWITCH, // "switch"
ND_CASE, // "case"
ND_BREAK, // "break"
ND_CONTINUE, // "continue"
ND_GOTO, // "goto"
ND_LABEL, // labeled statement
ND_RETURN, // "return"
ND_BLOCK, // compound statement
ND_EXPR_STMT, // statement with expression (w/o return)
ND_STMT_EXPR, // statement expression (GNU extension)
ND_FUNCALL, // function call
ND_NULL_EXPR, // do nothing
ND_VAR, // local variables
ND_NUM, // Integer
ND_CAST, // type cast
} NodeKind;
typedef struct Var Var;
struct Var {
Var *next;
char *name;
Type *ty;
Token *tok; // representative token
bool is_local;
int align;
// for local variables
int offset;
// for global variables
bool is_static;
char *init_data;
Relocation *rel;
};
// used for gloval vars initialization using a pointer to another global vars
struct Relocation {
Relocation *next;
int offset;
char *label;
long addend;
};
typedef struct Node Node;
struct Node {
NodeKind kind;
Node *next;
Type *ty; // type for the node (not evaluated for stmt, expr only)
Token *token;
Node *lhs;
Node *rhs;
// if-statement, while-statement, for-statement
Node *cond;
Node *then;
Node *els;
Node *init;
Node *inc;
// assignment
bool is_init;
// block or statement espression
Node *body;
// struct member access
Member *member;
// Function call
char *funcname;
Type *func_ty;
Var **args;
int nargs;
// Goto or labeled statement
char *label_name;
// switch-cases
Node *case_next;
Node *default_case;
int case_label;
int case_end_label;
// variable
Var *var;
// numeric literal
long val;
double fval;
};
typedef struct Function Function;
struct Function {
Function *next;
char *name;
Var *params;
bool is_static;
bool is_variadic;
Node *node;
Var *locals;
int stack_size;
};
typedef struct Program Program;
struct Program {
Function *fns;
Var *globals;
};
Node *new_node_cast(Node *expr, Type *ty);
long const_expr(Token **rest, Token *tok);
Program *parse(Token *tok);
//
// codegen.c
//
void codegen(Program *prog);
//
// main.c
//
extern bool opt_E;
extern char **include_paths;
//
// type.c
//
typedef enum {
TY_VOID,
TY_BOOL,
TY_ENUM,
TY_CHAR,
TY_SHORT,
TY_INT,
TY_LONG,
TY_FLOAT,
TY_DOUBLE,
TY_PTR,
TY_FUNC,
TY_ARRAY,
TY_STRUCT,
} TypeKind;
struct Type {
TypeKind kind;
int size; // used as sizeof() value
int align; // alignment
bool is_unsigned; // unsigned or signed
bool is_incomplete; // incomplete type
bool is_const; // const
Type *base;
// used for declaration
Token *ident;
Token *name_pos;
// function
Type *return_ty;
Type *params;
bool is_variadic;
Type *next;
// array
int array_len;
// struct
Member *members;
};
struct Member {
Member *next;
Type *ty;
Token *tok; // for error message
char *name;
int align;
int offset;
};
extern Type *ty_void;
extern Type *ty_bool;
extern Type *ty_char;
extern Type *ty_short;
extern Type *ty_int;
extern Type *ty_long;
extern Type *ty_uchar;
extern Type *ty_ushort;
extern Type *ty_uint;
extern Type *ty_ulong;
extern Type *ty_float;
extern Type *ty_double;
Type *new_type(TypeKind kind, int size, int align);
int align_to(int n, int align);
Type *pointer_to(Type *base);
Type *copy_ty(Type *ty);
Type *func_returning(Type *ty);
Type *array_of(Type *ty, int len);
Type *enum_type(void);
Type *struct_type(void);
int size_of(Type *ty);
bool is_integer(Type *ty);
bool is_flonum(Type *ty);
bool is_numeric(Type *ty);
bool is_pointer_like(Type *ty);
void generate_type(Node *node);