-
Notifications
You must be signed in to change notification settings - Fork 0
/
boncc.h
413 lines (375 loc) · 9.84 KB
/
boncc.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
#include "vector.h"
#include <stdbool.h>
#include <stdio.h>
typedef enum {
TK_HASH, // #
TK_2HASH, // ##
TK_PLUS, // +
TK_MINUS, // -
TK_STAR, // *
TK_SLASH, // /
TK_PERCENT, // %
TK_AMP, // &
TK_TILDE, // ~
TK_HAT, // ^
TK_BAR, // |
TK_LSHIFT, // <<
TK_RSHIFT, // >>
TK_INC, // ++
TK_DEC, // --
TK_ADDEQ, // +=
TK_SUBEQ, // -=
TK_MULEQ, // *=
TK_DIVEQ, // /=
TK_MODEQ, // %=
TK_XOREQ, // ^=
TK_ANDEQ, // &=
TK_OREQ, // |=
TK_LSHIFTEQ, // <<=
TK_RSHIFTEQ, // >>=
TK_EQ, // ==
TK_NE, // !=
TK_LT, // <
TK_LE, // <=
TK_GT, // >
TK_GE, // >=
TK_LOGAND, // &&
TK_LOGOR, // ||
TK_LOGNOT, // !
TK_ASSIGN, // =
TK_LPAREN, // (
TK_RPAREN, // )
TK_LBRACE, // {
TK_RBRACE, // }
TK_LBRACKET, // [
TK_RBRACKET, // ]
TK_QUESTION, // ?
TK_COLON, // :
TK_SEMICOLON, // ;
TK_COMMA, // ,
TK_DOT, // .
TK_3DOTS, // ...
TK_ARROW, // ->
TK_RETURN, // return
TK_IF, // if
TK_ELSE, // else
TK_DO, // do
TK_WHILE, // while
TK_FOR, // for
TK_SWITCH, // switch
TK_CASE, // case
TK_DEFAULT, // default
TK_BREAK, // break
TK_CONTINUE, // continue
TK_GOTO, // goto
TK_VOID, // void
TK_INT, // int
TK_CHAR, // char
TK_SHORT, // short
TK_LONG, // long
TK_FLOAT, // float
TK_DOUBLE, // double
TK_BOOL, // _Bool
TK_SIZEOF, // sizeof
TK_STRUCT, // struct
TK_UNION, // union
TK_ENUM, // enum
TK_TYPEDEF, // typedef
TK_STATIC, // static
TK_EXTERN, // extern
TK_CONST, // const
TK_SIGNED, // signed
TK_UNSIGNED, // unsigned
TK_STR, // string literal
TK_IDENT,
TK_NUM,
TK_EOF,
TK_EMPTY, // used by preprocessor
} TokenKind;
extern const char *token_text[];
typedef struct Position Position;
struct Position {
char *file_name;
char *pos;
int line_number;
int column_number;
};
typedef struct Type Type;
typedef struct Number Number;
struct Number {
Type *type;
union {
unsigned long long ulong_value;
long long long_value;
float float_value;
double double_value;
} value;
};
typedef struct String String;
struct String {
// a string that is not null-terminated
char *str;
int len;
};
typedef struct Type Type;
typedef struct Token Token;
struct Token {
TokenKind kind;
Token *next;
Position pos;
String *str;
bool is_identifier; // true iff TK_IDENT or reserved identifiers
Number *num; // only for TK_NUM
char *string_literal; // null terminated, only for TK_STR
Type *type; // TK_NUM
bool at_bol;
bool at_eol;
bool has_right_space; // to detect function-like macros
int idx; // used by function-like macros
};
typedef struct Type Type;
typedef struct Member Member;
struct Member {
String *ident;
Type *type;
int offset;
int padding;
Member *next; // liked list
};
typedef enum {
TYPE_VOID,
TYPE_PTR,
TYPE_ARRAY,
TYPE_STRUCT,
TYPE_UNION,
TYPE_ENUM,
TYPE_INT,
TYPE_CHAR,
TYPE_SHORT,
TYPE_LONG,
TYPE_UINT,
TYPE_UCHAR,
TYPE_USHORT,
TYPE_ULONG,
TYPE_FLOAT,
TYPE_DOUBLE,
TYPE_LDOUBLE,
TYPE_BOOL,
TYPE_FUNC,
TYPE_NONE, // internally used for declarator
} TypeKind;
typedef struct Type Type;
struct Type {
TypeKind kind;
int size; // sizeof
int align;
bool is_const;
struct Type *base; // only for TYPE_PTR and TYPE_ARRAY
int array_size; // number of elements for TYPE_ARRAY
bool is_unnamed; // TYPE_STRUCT, TYPE_UNION, TYPE_ENUM
Member *member; // TYPE_STRUCT, TYPE_UNION
Token *objdec; // used for declaration
Type *return_type; // TYPE_FUNC
Vector *params; // vector of Type* for TYPE_FUNC
bool is_variadic; // TYPE_FUNC
};
typedef struct Node Node;
typedef struct VariableInit VariableInit;
struct VariableInit {
Node *expr;
Vector *vec;
bool nested; // true if vec is nested
};
typedef enum {
OBJ_GVAR,
OBJ_LVAR,
OBJ_STRLIT,
OBJ_FUNC,
} ObjectKind;
typedef struct Object Variable;
typedef struct Object Function;
typedef struct Object Object;
struct Object { // variable or function
String *ident;
Type *type;
Token *token; // for error messages
ObjectKind kind;
bool is_static;
int offset; // OBJ_LVAR, OBJ_FUNC
// variable
char *string_literal; // null terminated, only for OBJ_STRLIT
VariableInit *init; // OBJ_GVAR, OBJ_LVAR
String *internal_ident; // for static local
bool is_extern;
// function
Node *body;
Vector *params; // vector of Variable*
Variable *hidden_va_area;
Variable *return_buffer_address; // only for callee function
int return_buffer_offset; // only for caller function
};
typedef enum {
ND_ADD, // +
ND_SUB, // -
ND_MUL, // *
ND_DIV, // /
ND_MOD, // %
ND_BITXOR, // ^
ND_BITOR, // |
ND_BITAND, // &
ND_BITNOT, // ~
ND_LSHIFT, // <<
ND_RSHIFT, // >>
ND_EQ, // ==
ND_NE, // !=
ND_LT, // <
ND_LE, // <=
ND_ASSIGN, // =
ND_COND, // ?:
ND_LOGNOT, // !
ND_LOGAND, // &&
ND_LOGOR, // ||
ND_CAST, // (type)
ND_COMMA, // ,
ND_VAR, // variable
ND_NUM, // integer
ND_RETURN, // return
ND_IF, // if
ND_DO, // do-while
ND_WHILE, // while
ND_FOR, // for
ND_SWITCH, // switch
ND_CASE, // case
ND_DEFAULT, // default
ND_BREAK, // break
ND_CONTINUE, // continue
ND_LABEL, // label:
ND_GOTO, // goto
ND_BLOCK, // { }
ND_CALL, // call function
ND_ADDR, // &val
ND_DEREF, // *ptr
ND_MEMBER, // struct member access
ND_NOP, // dummy node only used in parse.c
} NodeKind;
typedef struct Node Node;
struct Node {
NodeKind kind;
Token *token; // for error messages
Node *lhs;
Node *rhs;
Variable *variable; // only for ND_VAR
Type *type;
Number *num; // ND_NUM or constant expr
bool is_constant_expr;
int label_index;
// if(condition) body else else_
// do body while(condition)
// while(condition) body
// for(init; condition; update) body
// condition ? lhs : rhs
// switch(condition) body
// case condition: body
// default: body
// label: body
Node *condition;
Node *body;
Node *else_;
Node *init;
Node *update;
Node *next_case; // linked-list for ND_SWITCH, ND_CASE
Node *default_; // ND_SWITCH
Vector *blk_stmts; // statements in ND_BLOCK
Vector *args; // ND_CALL
Function *caller; // ND_CALL
Variable *return_buffer_address; // ND_RETURN
Member *member; // ND_MEMBER
};
typedef struct Vector Map;
typedef struct Scope Scope;
struct Scope {
Scope *prev;
Map *objects; // Object*
Map *types; // Type*
Map *enum_elements; // int*
Map *typedefs; // Type*
int offset; // local variable offset
};
Map *new_map();
void *map_geti(Map *map, int idx);
void *map_get(Map *map, String *key);
void map_push(Map *map, String *key, void *val);
void map_erase(Map *map, String *key);
extern Scope *global_scope;
extern Map *strings; // Variable*
extern Vector *static_local_variables; // Variable*
extern Vector *include_path; // char*
int iceil(int x, int y);
bool is_alphabet(char c);
bool is_alphanumeric_or_underscore(char c);
bool is_hexdigit(char c);
__attribute__((format(printf, 2, 3))) void error(Position *pos, char *fmt, ...);
char *read_file(char *path);
char *path_join(char *dir, char *file);
char *replace_ext(const char *path, const char *ext);
String *new_string(char *str, int len);
bool same_string(String *a, String *b);
bool same_string_nt(String *s, char *null_terminated);
void define_macro_from_command_line(char *arg);
Token *tokenize(char *src, char *input_path);
Token *preprocess(Token *input, Vector *included);
void parse(Token *input);
void generate_code(FILE *output_stream);
char *type_text(TypeKind kind);
Type *base_type(TypeKind kind);
Type *pointer_type(Type *base);
Type *array_type(Type *base, int len);
Type *struct_type(bool is_unnamed);
Type *union_type(bool is_unnamed);
Type *enum_type(bool is_unnamed);
Type *func_type(Type *return_type);
bool same_type(Type *a, Type *b);
bool is_float(Type *type);
bool is_integer(Type *type);
bool is_scalar(Type *type);
bool is_numerical(Type *type);
bool is_unsigned(Type *type);
bool is_signed(Type *type);
bool is_funcptr(Type *type);
bool is_struct_union(Type *type);
bool pass_on_memory(Type *type);
Type *implicit_type_conversion(Type *l, Type *r);
bool castable(Type *from, Type *to);
Number *new_number_int(int val);
bool is_integer_zero(Number *num);
int number2int(Number *num);
unsigned int number2uint(Number *num);
long long number2long(Number *num);
unsigned long long number2ulong(Number *num);
short number2short(Number *num);
unsigned short number2ushort(Number *num);
char number2char(Number *num);
unsigned char number2uchar(Number *num);
bool number2bool(Number *num);
float number2float(Number *num);
double number2double(Number *num);
Number *number_add(Number *l, Number *r);
Number *number_sub(Number *l, Number *r);
Number *number_mul(Number *l, Number *r);
Number *number_div(Number *l, Number *r);
Number *number_mod(Number *l, Number *r);
Number *number_bitxor(Number *l, Number *r);
Number *number_bitor(Number *l, Number *r);
Number *number_bitand(Number *l, Number *r);
Number *number_bitnot(Number *l);
Number *number_lshift(Number *l, Number *r);
Number *number_rshift(Number *l, Number *r);
Number *number_eq(Number *l, Number *r);
Number *number_ne(Number *l, Number *r);
Number *number_lt(Number *l, Number *r);
Number *number_le(Number *l, Number *r);
Number *number_cond(Number *cond, Number *l, Number *r);
Number *number_lognot(Number *l);
Number *number_logand(Number *l, Number *r);
Number *number_logor(Number *l, Number *r);
Number *number_cast(Number *l, Type *type);