-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.c
367 lines (348 loc) · 7.75 KB
/
type.c
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
#include "ast_node.h"
#include "cimple.h"
#include "type.h"
Type *enum_type(void)
{
Type *type = calloc(1, sizeof(Type));
type->kind = TYPE_ENUM;
type->size = 4;
type->align = 4;
return type;
}
Type *void_type(void)
{
Type *type = calloc(1, sizeof(Type));
type->kind = TYPE_VOID;
type->size = 1;
type->align = 1;
return type;
}
Type *bool_type(void)
{
Type *type = calloc(1, sizeof(Type));
type->kind = TYPE_BOOL;
type->size = 1;
type->align = 1;
return type;
}
Type *char_type(void)
{
Type *type = calloc(1, sizeof(Type));
type->kind = TYPE_CHAR;
type->size = 1;
type->align = 1;
return type;
}
Type *short_type(void)
{
Type *type = calloc(1, sizeof(Type));
type->kind = TYPE_SHORT;
type->size = 2;
type->align = 2;
return type;
}
Type *int_type(void)
{
Type *type = calloc(1, sizeof(Type));
type->kind = TYPE_INT;
type->size = 4;
type->align = 4;
return type;
}
Type *long_type(void)
{
Type *type = calloc(1, sizeof(Type));
type->kind = TYPE_LONG;
type->size = 8;
type->align = 8;
return type;
}
Member *new_struct_union_member(Type *type, const Token *name)
{
Member *member = calloc(1, sizeof(Member));
member->type = type;
member->name = type->name;
return member;
}
Type *new_struct_type(Member *members, size_t size, size_t align)
{
Type *type = calloc(1, sizeof(Type));
type->kind = TYPE_STRUCT;
type->members = members;
type->size = size;
type->align = align;
return type;
}
Type *new_union_type(Member *members, size_t size, size_t align)
{
Type *type = calloc(1, sizeof(Type));
type->kind = TYPE_UNION;
type->members = members;
type->size = size;
type->align = align;
return type;
}
bool is_integer(Type *type)
{
switch (type->kind)
{
case TYPE_BOOL:
case TYPE_CHAR:
case TYPE_SHORT:
case TYPE_INT:
case TYPE_LONG:
case TYPE_ENUM:
return true;
default:
return false;
}
}
Type *copy_type(Type *type)
{
Type *ret = malloc(sizeof(Type));
*ret = *type;
return ret;
}
Type *pointer_to(Type *base)
{
Type *type = calloc(1, sizeof(Type));
type->kind = TYPE_PTR;
type->size = 8;
type->align = 8;
type->base = base;
return type;
}
Type *func_type(Type *return_type)
{
Type *type = calloc(1, sizeof(Type));
type->kind = TYPE_FUNC;
type->return_type = return_type;
return type;
}
Type *array_of(Type *base, size_t len)
{
Type *ty = calloc(1, sizeof(Type));
ty->kind = TYPE_ARRAY;
ty->size = base->size * len;
ty->align = base->align;
ty->base = base;
ty->array_length = len;
return ty;
}
static Type *get_common_type(Type *ty1, Type *ty2)
{
if (ty1->base)
return pointer_to(ty1->base);
if (ty1->size == 8 || ty2->size == 8)
return long_type();
return int_type();
}
// For many binary operators, we implicitly promote operands so that
// both operands have the same type. Any integral type smaller than
// int is always promoted to int. If the type of one operand is larger
// than the other's (e.g. "long" vs. "int"), the smaller operand will
// be promoted to match with the other.
//
// This operation is called the "usual arithmetic conversion".
static void usual_arith_conv(Node **lhs, Node **rhs)
{
Type *ty = get_common_type((*lhs)->type, (*rhs)->type);
*lhs = new_cast_node(*lhs, ty);
*rhs = new_cast_node(*rhs, ty);
}
void add_type(struct Node *node)
{
if (!node || node->type)
return;
switch (node->tag)
{
case NODE_TAG_UNARY:
{
struct UnaryNode *unary = (struct UnaryNode *)node;
add_type(unary->expr);
switch (unary->kind)
{
case NODE_POST_INC:
case NODE_POST_DEC:
{
node->type = unary->expr->type;
return;
}
case NODE_NEG:
{
Type *type = get_common_type(int_type(), unary->expr->type);
unary->expr = new_cast_node(unary->expr, type);
node->type = type;
return;
}
case NODE_ADDR:
if (unary->expr->type->kind == TYPE_ARRAY)
{
/*
* int arr[2];
* int* p = &arr;
*/
node->type = pointer_to(unary->expr->type->base);
}
else
{
node->type = pointer_to(unary->expr->type);
}
return;
case NODE_DEREF:
// only pointer and array types have a base
if (unary->expr->type->base == NULL)
{
error_tok(node->tok, "invalid pointer dereference");
}
node->type = unary->expr->type->base;
return;
case NODE_STMT_EXPR:
{
if (unary->expr)
{
Node *stmt = unary->expr;
while (stmt->next)
stmt = stmt->next;
if (stmt->tag == NODE_TAG_UNARY)
{
struct UnaryNode *last_node = (struct UnaryNode *)stmt;
if (last_node->kind == NODE_EXPR_STMT)
{
node->type = last_node->expr->type;
return;
}
}
}
error_tok(node->tok,
"statement expression returning void is not supported");
return;
}
default:
break;
}
break;
}
case NODE_TAG_BINARY:
{
struct BinaryNode *binary = (struct BinaryNode *)node;
add_type(binary->lhs);
add_type(binary->rhs);
switch (binary->kind)
{
case NODE_ADD:
case NODE_SUB:
case NODE_MUL:
case NODE_DIV:
usual_arith_conv(&binary->lhs, &binary->rhs);
node->type = binary->lhs->type;
return;
case NODE_EQ:
case NODE_NE:
case NODE_LT:
case NODE_LE:
usual_arith_conv(&binary->lhs, &binary->rhs);
node->type = int_type();
return;
case NODE_ASSIGN:
if (binary->lhs->type->kind == TYPE_ARRAY)
{
error_tok(binary->lhs->tok, "not an lvalue");
}
if (binary->lhs->type->kind != TYPE_STRUCT)
{
if (binary->lhs->type->kind != TYPE_BOOL && binary->rhs->tag == NODE_TAG_NUM)
{
// compile time casting
NumNode *num_node = (NumNode *)binary->rhs;
switch (binary->lhs->type->size)
{
case 1:
num_node->val = (char)num_node->val;
break;
case 2:
num_node->val = (short)num_node->val;
break;
case 4:
num_node->val = (int)num_node->val;
break;
default:
break;
}
}
else
{
binary->rhs = new_cast_node(binary->rhs, binary->lhs->type);
}
}
node->type = binary->lhs->type;
return;
case NODE_COMMA:
node->type = binary->rhs->type;
return;
}
break;
}
case NODE_TAG_MEMBER:
{
struct MemberNode *member_node = (struct MemberNode *)node;
node->type = member_node->member->type;
break;
}
case NODE_TAG_IF:
{
struct IfNode *if_node = (struct IfNode *)node;
add_type(if_node->cond_expr);
add_type(if_node->then_stmt);
add_type(if_node->else_stmt);
break;
}
case NODE_TAG_FOR:
{
struct ForNode *for_node = (struct ForNode *)node;
add_type(for_node->init_expr);
add_type(for_node->cond_expr);
add_type(for_node->inc_expr);
add_type(for_node->body_stmt);
break;
}
case NODE_TAG_BLOCK:
{
struct BlockNode *block_node = (struct BlockNode *)node;
for (Node *n = block_node->body; n; n = n->next)
{
add_type(n);
}
break;
}
case NODE_TAG_FUNCALL:
{
struct FunCallNode *fun_call = (struct FunCallNode *)node;
for (Node *n = fun_call->args; n; n = n->next)
{
add_type(n);
}
node->type = long_type();
return;
}
case NODE_TAG_VAR:
{
struct VarNode *var_node = (struct VarNode *)node;
node->type = var_node->var->type;
return;
}
case NODE_TAG_NUM:
{
struct NumNode *num_node = (struct NumNode *)node;
if (num_node->val == (int)num_node->val)
{
node->type = int_type();
}
else
{
node->type = long_type();
}
return;
}
}
}