-
Notifications
You must be signed in to change notification settings - Fork 155
/
sema.c
259 lines (236 loc) · 6.34 KB
/
sema.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
#include "9cc.h"
// Semantics analyzer. This pass plays a few important roles as shown
// below:
//
// - Add types to nodes. For example, a tree that represents "1+2" is
// typed as INT because the result type of an addition of two
// integers is integer.
//
// - Insert nodes to make array-to-pointer conversion explicit.
// Recall that, in C, "array of T" is automatically converted to
// "pointer to T" in most contexts.
//
// - Insert nodes for implicit cast so that they are explicitly
// represented in AST.
//
// - Scales operands for pointer arithmetic. E.g. ptr+1 becomes ptr+4
// for integer and becomes ptr+8 for pointer.
//
// - Reject bad assignments, such as `1=2+3`.
static Node *maybe_decay(Node *base, bool decay) {
if (!decay || base->ty->ty != ARY)
return base;
Node *node = calloc(1, sizeof(Node));
node->op = ND_ADDR;
node->ty = ptr_to(base->ty->ary_of);
node->expr = base;
node->token = base->token;
return node;
}
noreturn static void bad_node(Node *node, char *msg) {
bad_token(node->token, msg);
}
static void check_lval(Node *node) {
int op = node->op;
if (op != ND_VARREF && op != ND_DEREF && op != ND_DOT)
bad_node(node, "not an lvalue");
}
static Node *scale_ptr(int op, Node *base, Type *ty) {
Node *node = calloc(1, sizeof(Node));
node->op = op;
node->lhs = base;
node->rhs = new_int_node(ty->ptr_to->size, base->token);
node->token = base->token;
return node;
}
static Node *cast(Node *base, Type *ty) {
Node *node = calloc(1, sizeof(Node));
node->op = ND_CAST;
node->ty = ty;
node->expr = base;
node->token = base->token;
return node;
}
static void check_int(Node *node) {
int t = node->ty->ty;
if (t != INT && t != CHAR && t != BOOL)
bad_node(node, "not an integer");
}
static Node *do_walk(Node *node, bool decay);
static Node *walk(Node *node) {
return do_walk(node, true);
}
static Node *walk_nodecay(Node *node) {
return do_walk(node, false);
}
static Node *do_walk(Node *node, bool decay) {
switch (node->op) {
case ND_NUM:
case ND_NULL:
case ND_BREAK:
case ND_CONTINUE:
return node;
case ND_VARREF:
return maybe_decay(node, decay);
case ND_IF:
node->cond = walk(node->cond);
node->then = walk(node->then);
if (node->els)
node->els = walk(node->els);
return node;
case ND_FOR:
if (node->init)
node->init = walk(node->init);
if (node->cond)
node->cond = walk(node->cond);
if (node->inc)
node->inc = walk(node->inc);
node->body = walk(node->body);
return node;
case ND_DO_WHILE:
case ND_SWITCH:
node->cond = walk(node->cond);
node->body = walk(node->body);
return node;
case ND_CASE:
node->body = walk(node->body);
return node;
case '+':
node->lhs = walk(node->lhs);
node->rhs = walk(node->rhs);
if (node->rhs->ty->ty == PTR) {
Node *n = node->lhs;
node->lhs = node->rhs;
node->rhs = n;
}
check_int(node->rhs);
if (node->lhs->ty->ty == PTR) {
node->rhs = scale_ptr('*', node->rhs, node->lhs->ty);
node->ty = node->lhs->ty;
} else {
node->ty = int_ty();
}
return node;
case '-': {
node->lhs = walk(node->lhs);
node->rhs = walk(node->rhs);
Type *lty = node->lhs->ty;
Type *rty = node->rhs->ty;
if (lty->ty == PTR && rty->ty == PTR) {
if (!same_type(rty, lty))
bad_node(node, "incompatible pointer");
node = scale_ptr('/', node, lty);
node->ty = lty;
} else {
node->ty = int_ty();
}
return node;
}
case '=':
node->lhs = walk_nodecay(node->lhs);
check_lval(node->lhs);
node->rhs = walk(node->rhs);
if (node->lhs->ty->ty == BOOL)
node->rhs = cast(node->rhs, bool_ty());
node->ty = node->lhs->ty;
return node;
case ND_DOT: {
node->expr = walk(node->expr);
if (node->expr->ty->ty != STRUCT)
bad_node(node, "struct expected before '.'");
Type *ty = node->expr->ty;
if (!ty->members)
bad_node(node, "incomplete type");
node->ty = map_get(ty->members, node->name);
if (!node->ty)
bad_node(node, format("member missing: %s", node->name));
return maybe_decay(node, decay);
}
case '?':
node->cond = walk(node->cond);
node->then = walk(node->then);
node->els = walk(node->els);
node->ty = node->then->ty;
return node;
case '*':
case '/':
case '%':
case '<':
case '|':
case '^':
case '&':
case ND_EQ:
case ND_NE:
case ND_LE:
case ND_SHL:
case ND_SHR:
case ND_LOGAND:
case ND_LOGOR:
node->lhs = walk(node->lhs);
node->rhs = walk(node->rhs);
check_int(node->lhs);
check_int(node->rhs);
node->ty = int_ty();
return node;
case ',':
node->lhs = walk(node->lhs);
node->rhs = walk(node->rhs);
node->ty = node->rhs->ty;
return node;
case '!':
case '~':
node->expr = walk(node->expr);
check_int(node->expr);
node->ty = int_ty();
return node;
case ND_ADDR:
node->expr = walk(node->expr);
check_lval(node->expr);
node->ty = ptr_to(node->expr->ty);
if (node->expr->op == ND_VARREF)
node->expr->var->address_taken = true;
return node;
case ND_DEREF:
node->expr = walk(node->expr);
if (node->expr->ty->ty != PTR)
bad_node(node, "operand must be a pointer");
if (node->expr->ty->ptr_to->ty == VOID)
bad_node(node, "cannot dereference void pointer");
node->ty = node->expr->ty->ptr_to;
return maybe_decay(node, decay);
case ND_RETURN:
case ND_EXPR_STMT:
node->expr = walk(node->expr);
return node;
case ND_CALL:
for (int i = 0; i < node->args->len; i++)
node->args->data[i] = walk(node->args->data[i]);
node->ty = node->ty->returning;
return node;
case ND_COMP_STMT: {
for (int i = 0; i < node->stmts->len; i++)
node->stmts->data[i] = walk(node->stmts->data[i]);
return node;
}
case ND_STMT_EXPR: {
for (int i = 0; i < node->stmts->len; i++)
node->stmts->data[i] = walk(node->stmts->data[i]);
node->expr = walk(node->expr);
node->ty = node->expr->ty;
return node;
}
default:
assert(0 && "unknown node type");
}
}
Type *get_type(Node *node) {
return walk_nodecay(node)->ty;
}
void sema(Program *prog) {
for (int i = 0; i < prog->funcs->len; i++) {
Function *fn = prog->funcs->data[i];
Node *node = fn->node;
assert(node->op == ND_FUNC);
node->body = walk(node->body);
}
}