-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc.h
304 lines (272 loc) · 5.07 KB
/
cc.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
#define EOF (-1)
#define NULL ((void *) 0)
#define bool _Bool
#define false 0
#define true 1
#define noreturn _Noreturn
#define va_start __builtin_va_start
#define va_end __builtin_va_end
typedef struct {
int gp_offset;
int fp_offset;
void *overflow_arg_area;
void *reg_save_area;
} va_list[1];
typedef int size_t;
typedef struct _IO_FILE FILE;
extern struct _IO_FILE *stdin;
extern struct _IO_FILE *stdout;
extern struct _IO_FILE *stderr;
int fgetc(FILE *stream);
int ungetc(int c, FILE *stream);
int printf (char *format, ...);
int fprintf(FILE *stream, char *format, ...);
int vfprintf(FILE *s, char *format, va_list arg);
FILE *fopen(char *filename, char *modes);
void *calloc(size_t nmemb, size_t size);
void *realloc(void *ptr, size_t size);
void exit(int status);
double strtod(char *s, char *endpoint);
int strcmp(char *s1, char *s2);
int isdigit(int c);
int isalpha(int c);
int isalnum(int c);
typedef struct string {
int size, length;
char *buffer;
} String;
extern String *string_new();
extern void string_push(String *string, char c);
typedef struct vector {
int size, length;
void **array;
} Vector;
extern Vector *vector_new();
extern void vector_push(Vector *vector, void *value);
extern void *vector_pop(Vector *vector);
typedef struct map {
int count;
char *keys[1024];
void *values[1024];
} Map;
extern Map *map_new();
extern int map_count(Map *map);
extern bool map_put(Map *map, char *key, void *value);
extern void *map_lookup(Map *map, char *key);
extern void map_clear();
extern noreturn void error(char *format, ...);
extern char *read_source(char *file);
typedef enum token_type {
tVOID,
tBOOL,
tCHAR,
tINT,
tDOUBLE,
tSTRUCT,
tENUM,
tTYPEDEF,
tEXTERN,
tNORETURN,
tSIZEOF,
tALIGNOF,
tIF,
tELSE,
tWHILE,
tDO,
tFOR,
tCONTINUE,
tBREAK,
tRETURN,
tIDENTIFIER,
tINT_CONST,
tFLOAT_CONST,
tSTRING_LITERAL,
tLBRACKET,
tRBRACKET,
tLPAREN,
tRPAREN,
tRBRACE,
tLBRACE,
tDOT,
tARROW,
tINC,
tDEC,
tNOT,
tLNOT,
tMUL,
tDIV,
tMOD,
tADD,
tSUB,
tLSHIFT,
tRSHIFT,
tLT,
tGT,
tLTE,
tGTE,
tEQ,
tNEQ,
tAND,
tXOR,
tOR,
tLAND,
tLOR,
tQUESTION,
tCOLON,
tSEMICOLON,
tELLIPSIS,
tASSIGN,
tADD_ASSIGN,
tSUB_ASSIGN,
tMUL_ASSIGN,
tCOMMA,
tHASH,
tNEWLINE,
tEND
} TokenType;
typedef struct token {
TokenType type;
int int_value;
double double_value;
String *string_value;
char *identifier;
} Token;
extern Vector *lexical_analyze(char *source_buffer);
extern Vector *preprocess(Vector *token_vector);
typedef enum type_type {
VOID,
BOOL,
CHAR,
INT,
DOUBLE,
POINTER,
ARRAY,
STRUCT,
FUNCTION
} TypeType;
typedef struct type {
TypeType type;
int size, align;
struct type *pointer_to;
struct type *array_of;
int array_size;
Map *members, *offsets;
struct type *function_returning;
Vector *params;
bool ellipsis;
int original_size;
bool array_pointer;
bool definition;
bool external;
bool incomplete;
} Type;
extern Type *type_new();
extern Type *type_void();
extern Type *type_bool();
extern Type *type_char();
extern Type *type_int();
extern Type *type_double();
extern Type *type_pointer_to(Type *type);
extern Type *type_array_of(Type *type, int array_size);
extern Type *type_struct(Vector *identifiers, Map *members);
extern Type *type_function_returning(Type *returning, Vector *params, bool ellipsis);
extern Type *type_convert(Type *type);
extern void type_copy(Type *dest, Type *src);
extern bool type_integer(Type *type);
extern bool type_pointer(Type *type);
extern bool type_scalar(Type *type);
extern bool type_same(Type *type1, Type *type2);
typedef enum symbol_type {
GLOBAL,
LOCAL
} SymbolType;
typedef struct symbol {
SymbolType type;
char *identifier;
Type *value_type;
int offset;
bool declaration;
} Symbol;
typedef enum node_type {
INT_CONST,
FLOAT_CONST,
STRING_LITERAL,
IDENTIFIER,
FUNC_CALL,
DOT,
POST_INC,
POST_DEC,
PRE_INC,
PRE_DEC,
ADDRESS,
INDIRECT,
UPLUS,
UMINUS,
NOT,
LNOT,
SIZEOF,
CAST,
MUL,
DIV,
MOD,
ADD,
SUB,
LSHIFT,
RSHIFT,
LT,
GT,
LTE,
GTE,
EQ,
NEQ,
AND,
OR,
XOR,
LAND,
LOR,
CONDITION,
ASSIGN,
ADD_ASSIGN,
SUB_ASSIGN,
MUL_ASSIGN,
VAR_INIT,
VAR_ARRAY_INIT,
VAR_INIT_DECL,
VAR_DECL,
COMP_STMT,
EXPR_STMT,
IF_STMT,
IF_ELSE_STMT,
WHILE_STMT,
DO_WHILE_STMT,
FOR_STMT,
CONTINUE_STMT,
BREAK_STMT,
RETURN_STMT,
FUNC_DEF,
TLANS_UNIT
} NodeType;
typedef struct node {
enum node_type type;
Type *value_type;
int int_value;
double double_value;
String *string_value;
int string_label, float_label;
char *identifier;
Symbol *symbol;
Vector *args;
int member_offset;
struct node *left, *right, *init, *control, *afterthrough, *expr;
struct node *initializer;
Vector *array_elements;
Vector *declarations;
Vector *statements;
struct node *if_body, *else_body, *loop_body, *function_body;
int local_vars_size;
Vector *string_literals, *definitions;
} Node;
extern Node *node_new();
extern Node *parse(Vector *token_vector);
extern void analyze(Node *node);
extern void gen(Node *node);