-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.c
133 lines (121 loc) · 3.04 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
#include "kmcc.h"
// char_type->kind = TY_CHAR
// char_type->size = 1
// char_type->align = 1
Type *void_type = &(Type){TY_VOID, 1, 1};
Type *bool_type = &(Type){TY_BOOL, 1, 1};
Type *char_type = &(Type){TY_CHAR, 1, 1};
Type *short_type = &(Type){TY_SHORT, 2, 2};
Type *int_type = &(Type){TY_INT, 4, 4};
Type *long_type = &(Type){TY_LONG, 8, 8};
bool is_integer(Type *ty) {
return ty->kind == TY_BOOL || ty->kind == TY_CHAR
|| ty->kind == TY_SHORT || ty->kind == TY_INT
|| ty->kind == TY_LONG;
}
// nは現在のoffset
// alignで指定したサイズに整えた値を出す
// n = 1, align = 8 return 8;
int align_to(int n, int align) {
// ~ はbit反転演算子
// & はbit演算のAND
// n = 14; align = 8のとき
// lhs = n + align - 1 = 21 = 100101
// rhs = ~(align - 1) = ~7 = ~(000111) = 111000
// return = 100101 & 111000 = 100000 = 16
return (n + align - 1) & ~(align - 1);
}
static Type *new_type(TypeKind kind, int size, int align) {
Type *ty = calloc(1, sizeof(Type));
ty->kind = kind;
ty->size = size;
ty->align = align;
return ty;
}
Type *pointer_to(Type *base) {
Type *ty = new_type(TY_PTR, 8, 8);
ty->base = base;
return ty;
}
Type *array_of(Type *base, int len) {
Type *ty
= new_type(TY_ARRAY, base->size * len, base->align);
ty->base = base;
ty->array_len = len;
return ty;
}
Type *func_type(Type *return_ty) {
Type *ty = new_type(TY_FUNC, 1, 1);
ty->return_ty = return_ty;
return ty;
}
void add_type(Node *node) {
if (!node || node->ty)
return;
add_type(node->lhs);
add_type(node->rhs);
add_type(node->cond);
add_type(node->then);
add_type(node->els);
add_type(node->init);
add_type(node->inc);
for (Node *n = node->body; n; n = n->next) {
add_type(n);
}
for (Node *n = node->args; n; n = n->next) {
add_type(n);
}
switch (node->kind) {
case ND_ADD:
case ND_SUB:
case ND_PTR_DIFF:
case ND_MUL:
case ND_DIV:
case ND_EQ:
case ND_NE:
case ND_LT:
case ND_LE:
case ND_NUM:
node->ty = long_type;
return;
case ND_PTR_ADD:
case ND_PTR_SUB:
case ND_ASSIGN:
node->ty = node->lhs->ty;
return;
case ND_VAR:
node->ty = node->var->ty;
return;
case ND_MEMBER:
node->ty = node->member->ty;
return;
case ND_ADDR:
if (node->lhs->ty->kind == TY_ARRAY) {
node->ty = pointer_to(node->lhs->ty->base);
} else {
node->ty = pointer_to(node->lhs->ty);
}
return;
case ND_DEREF:
// baseがNULLということは,arrayでもptrでもない
if (!node->lhs->ty->base) {
error_tok(node->tok, "invalid pointer deference");
}
node->ty = node->lhs->ty->base;
if (node->ty->kind == TY_VOID) {
error_tok(node->tok,
"dereferencing a void pointer");
}
return;
case ND_STMT_EXPR: {
Node *last = node->body;
while (last->next) {
last = last->next;
}
node->ty = last->ty;
return;
}
default:
break;
}
}