-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.cpp
317 lines (296 loc) · 11 KB
/
ast.cpp
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
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include "ast.hpp"
#include <cassert>
/* ============ ============ ============ ============ ============ */
/* ============ PART1 : ABSTRACT SYNTAX TREE GENERATION ============ */
/* ============ ============ ============ ============ ============ */
/*
struct _ast_node **children must not be NULL
const char *node_val must not be null
*/
struct _ast_node* ast_node_create(enum NODE_TYPE node_type, const char *node_val, struct _ast_node** children) {
assert(node_val);
assert(children);
struct _ast_node* new_node = (struct _ast_node*) malloc(sizeof(struct _ast_node));
new_node->node_type = node_type;
new_node->node_val = node_val ? strdup(node_val) : NULL; // dynamically allocated
unsigned int i;
for(i = 0;children[i]!=NULL;i++);
// children[i] == NULL
// i is size of children array
new_node->children_size = i;
new_node->children_capacity = 1;
if (i == 0) {
new_node->children = (struct _ast_node **) malloc(sizeof(struct _ast_node*));
memcpy(new_node->children, children, sizeof(struct _ast_node*));
} else {
// i > 0
while ((i >> new_node->children_capacity) != 0) {
new_node->children_capacity++;
}
new_node->children_capacity = (1 << new_node->children_capacity);
new_node->children = (struct _ast_node **) malloc(new_node->children_capacity * sizeof(struct _ast_node*));
memcpy(new_node->children, children, new_node->children_size * sizeof(struct _ast_node*));
}
return new_node;
}
/*
root must be non null
*/
void ast_destroy(struct _ast_node* root) {
assert(root);
struct _ast_node **curr_child = root->children;
while (*curr_child != NULL) {
ast_destroy(*curr_child);
curr_child++;
}
free(root->node_val);
free(root->children);
free(root);
}
/*
root must be non null
depth >= 0
*/
void dump_ast_at_depth(struct _ast_node *root, int depth) {
assert(root);
assert(depth >= 0);
int d;
for(d = 0;d != depth;d++) {
if (d == depth-1) printf("|---");
else printf(" ");
}
printf("%s\n", root->node_val);
struct _ast_node **curr_child = root->children;
while (*curr_child != NULL) {
dump_ast_at_depth(*curr_child, depth + 1);
curr_child++;
}
}
/*
root must be non null
*/
void dump_ast(struct _ast_node *root) {
dump_ast_at_depth(root, 0);
}
/*
root must be non null
*/
void add_children(struct _ast_node *root, struct _ast_node* child) {
assert(root);
assert(child);
if (root->children_size + 1 < root->children_capacity) {
root->children[root->children_size++] = child;
root->children[root->children_size] = NULL;
} else {
// root->children_size + 1 == root->children_capacity
// TODO : grow the array
root->children_capacity *= 2;
struct _ast_node **temp = (struct _ast_node **) malloc(root->children_capacity * sizeof(struct _ast_node*));
memcpy(temp, root->children, root->children_size * sizeof(struct _ast_node*));
temp[root->children_size++] = child;
temp[root->children_size] = NULL;
free(root->children);
root->children = temp;
}
}
void raise_type_error() {
fprintf(stderr, "invalid combination of type specifiers\n");
exit(1);
}
std::string get_nodestr(enum NODE_TYPE nt) {
switch (nt) {
case NODE_TYPE::TYPE_SPEC_SINT:
return "signed int";
case NODE_TYPE::TYPE_SPEC_UINT:
return "unsigned int";
case NODE_TYPE::TYPE_SPEC_UCHAR:
return "unsigned char";
case NODE_TYPE::TYPE_SPEC_SCHAR:
return "signed char";
case NODE_TYPE::TYPE_SPEC_USHORT:
return "unsigned short";
case NODE_TYPE::TYPE_SPEC_SSHORT:
return "signed short";
case NODE_TYPE::TYPE_SPEC_SHORTINT:
return "short int";
case NODE_TYPE::TYPE_SPEC_INTLONG:
return "long int";
case NODE_TYPE::TYPE_SPEC_LONGLONG:
return "long long";
case NODE_TYPE::TYPE_SPEC_ULONGLONG:
return "unsigned long long";
case NODE_TYPE::TYPE_SPEC_INTLONGLONG:
return "long long int";
case NODE_TYPE::TYPE_SPEC_ULONG:
return "unsigned long";
case NODE_TYPE::TYPE_SPEC_LONGDOUBLE:
return "long double";
case NODE_TYPE::TYPE_SPEC_VOID:
return "void";
case NODE_TYPE::TYPE_SPEC_CHAR:
return "char";
case NODE_TYPE::TYPE_SPEC_SHORT:
return "short";
case NODE_TYPE::TYPE_SPEC_INT:
return "int";
case NODE_TYPE::TYPE_SPEC_LONG:
return "long";
case NODE_TYPE::TYPE_SPEC_FLOAT:
return "float";
case NODE_TYPE::TYPE_SPEC_DOUBLE:
return "double";
case NODE_TYPE::TYPE_SPEC_SIGNED:
return "signed";
case NODE_TYPE::TYPE_SPEC_UNSIGNED:
return "unsigned";
case NODE_TYPE::TYPE_SPEC_BOOL:
return "bool";
default:
return "unknown type";
}
}
char *getc_str(std::string &&str) {
char *retstr = (char*)malloc((str.length() + 1) * sizeof(char));
memcpy(retstr, str.c_str(), (str.length()+1) * sizeof(char));
return retstr;
}
void add_typespec(struct _ast_node *old_typespec, struct _ast_node *new_typespec) {
assert(new_typespec->node_type != NODE_TYPE::TYPE_SPEC_EMPTY);
enum NODE_TYPE new_type;
switch(old_typespec->children[0]->node_type) {
case NODE_TYPE::TYPE_SPEC_EMPTY:
switch (new_typespec->node_type) {
case NODE_TYPE::TYPE_SPEC_SIGNED:
new_type = NODE_TYPE::TYPE_SPEC_SINT;break;
case NODE_TYPE::TYPE_SPEC_UNSIGNED:
new_type = NODE_TYPE::TYPE_SPEC_UINT;break;
default:
new_type = new_typespec->node_type;
}
break;
case NODE_TYPE::TYPE_SPEC_CHAR:
switch(new_typespec->node_type) {
case NODE_TYPE::TYPE_SPEC_UNSIGNED:
new_type = NODE_TYPE::TYPE_SPEC_UCHAR;break;
case NODE_TYPE::TYPE_SPEC_SIGNED:
new_type = NODE_TYPE::TYPE_SPEC_SCHAR;break;
default:
raise_type_error();
}
break;
case NODE_TYPE::TYPE_SPEC_SHORT:
switch (new_typespec->node_type) {
case NODE_TYPE::TYPE_SPEC_UNSIGNED:
new_type = NODE_TYPE::TYPE_SPEC_USHORT;break;
case NODE_TYPE::TYPE_SPEC_SIGNED:
new_type = NODE_TYPE::TYPE_SPEC_SSHORT;break;
case NODE_TYPE::TYPE_SPEC_INT:
new_type = NODE_TYPE::TYPE_SPEC_SHORTINT;break;
default:
raise_type_error();
}
break;
case NODE_TYPE::TYPE_SPEC_INT:
switch (new_typespec->node_type) {
case NODE_TYPE::TYPE_SPEC_SIGNED:
new_type = NODE_TYPE::TYPE_SPEC_SINT;break;
case NODE_TYPE::TYPE_SPEC_UNSIGNED:
new_type = NODE_TYPE::TYPE_SPEC_UINT;break;
case NODE_TYPE::TYPE_SPEC_SHORT:
new_type = NODE_TYPE::TYPE_SPEC_SHORTINT;break;
case NODE_TYPE::TYPE_SPEC_LONG:
new_type = NODE_TYPE::TYPE_SPEC_INTLONG;break;
default:
raise_type_error();
}
break;
case NODE_TYPE::TYPE_SPEC_LONG:
switch(new_typespec->node_type) {
case NODE_TYPE::TYPE_SPEC_SIGNED:
new_type = NODE_TYPE::TYPE_SPEC_SLONG;break;
case NODE_TYPE::TYPE_SPEC_UNSIGNED:
new_type = NODE_TYPE::TYPE_SPEC_ULONG;break;
case NODE_TYPE::TYPE_SPEC_INT:
new_type = NODE_TYPE::TYPE_SPEC_INTLONG;break;
case NODE_TYPE::TYPE_SPEC_LONG:
new_type = NODE_TYPE::TYPE_SPEC_LONGLONG;break;
case NODE_TYPE::TYPE_SPEC_DOUBLE:
new_type = NODE_TYPE::TYPE_SPEC_LONGDOUBLE;break;
default:
raise_type_error();
}
break;
case NODE_TYPE::TYPE_SPEC_DOUBLE:
switch (new_typespec->node_type) {
case NODE_TYPE::TYPE_SPEC_LONG:
new_type = NODE_TYPE::TYPE_SPEC_LONGDOUBLE;break;
default:
raise_type_error();
}
break;
case NODE_TYPE::TYPE_SPEC_INTLONG:
switch (new_typespec->node_type) {
case NODE_TYPE::TYPE_SPEC_LONG:
new_type=NODE_TYPE::TYPE_SPEC_INTLONGLONG;break;
default:
raise_type_error();
}
break;
default:
raise_type_error();
}
new_typespec->node_type = new_type;
free(new_typespec->node_val); // free the old node_val
new_typespec->node_val = getc_str(get_nodestr(new_type));
ast_destroy(old_typespec->children[0]);
assert(!old_typespec->children[1]);
old_typespec->children[0] = new_typespec;
}
bool check_consistency_typequal(struct _ast_node *typequal_list, struct _ast_node *typequal) {
struct _ast_node **typespec;
enum NODE_TYPE type_tocheck = typequal->node_type;
for(typespec = typequal_list->children;*typespec!=NULL;typespec++) {
if ((*typespec)->node_type == type_tocheck)
return false; // this type is already present, no need to add
}
return true; // typequal->node_type is the new type being added
}
void add_typequal(struct _ast_node *typequal_list, struct _ast_node *typequal) {
assert(typequal_list->node_type == NODE_TYPE::TYPE_QUAL_LIST);
if (check_consistency_typequal(typequal_list, typequal)) {
add_children(typequal_list, typequal);
} else {
ast_destroy(typequal);
}
// result of adding multiple type qualifier is same as having a single type qualifier (no errors)
}
bool is_signed(struct _ast_node *typespec) {
assert(typespec->node_type == TYPE_SPECIFIER);
switch (typespec->children[0]->node_type)
{
case TYPE_SPEC_UCHAR:
case TYPE_SPEC_UINT:
case TYPE_SPEC_ULONG:
case TYPE_SPEC_ULONGLONG:
case TYPE_SPEC_USHORT:
return false;
default:
return true;
}
}
/* ============ ============ ============ ========== ========== */
/* ============ PART 2 : SYMBOL TABLE IMPLEMENTATION ============ */
/* ============ ============ ============ ========== ========== */
// see scope_check.cpp
/* ============ ============ ============ ========== ========== */
/* ============ PART 3 : LLVM IR CODE GENERATION ============ */
/* ============ ============ ============ ========== ========== */
// see codegen.cpp
/* ============ ============ ============ ========== ========== */
/* ============ PART 4 : LOCAL OPTIMIZATIONS ============ */
/* ============ ============ ============ ========== ========== */
// see optimize.cpp