-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpointer_arith.c
232 lines (222 loc) · 6.38 KB
/
pointer_arith.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
#include "9cc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FunctionDeclaration *function_declarations;
Type *EvaluateType(Expr *expr)
{
if (expr->expr_kind == EK_Number)
{
Type *integer = calloc(1, sizeof(Type));
integer->kind = TYPE_INT;
return integer;
}
if (expr->expr_kind == EK_Identifier)
{
LVar *local = findLVar(expr->name);
return local->type;
// return expr->type;
}
if (expr->expr_kind == EK_CallFunction)
{
FunctionDeclaration *func = findFunction(expr->name);
if (func == NULL)
{
fprintf(stderr, "function declaration missing:%s\n", expr->name);
Type *int_type = calloc(1, sizeof(Type));
int_type->kind = TYPE_INT;
return int_type;
}
return func->return_type;
}
if (expr->expr_kind == EK_UnaryOperator)
{
if (expr->op == UO_SizeOf)
{
Type *int_type = calloc(1, sizeof(Type));
int_type->kind = TYPE_INT;
return int_type;
}
if (expr->op == UO_AddressOf)
{
Type *ptr_to = EvaluateType(expr->first_child);
Type *ptr_type = calloc(1, sizeof(Type));
ptr_type->kind = TYPE_PTR;
ptr_type->ptr_to = ptr_to;
return ptr_type;
}
else if (expr->op == UO_Deref)
{
Type *ptr_from = EvaluateType(expr->first_child);
return ptr_from->ptr_to;
}
}
if (expr->expr_kind == EK_BinaryOperator)
{
if (expr->op == BO_Add)
{
Type *first = EvaluateType(expr->first_child);
Type *second = EvaluateType(expr->second_child);
if (first->kind == TYPE_INT && second->kind == TYPE_INT)
{
return first;
}
if (first->kind == TYPE_INT && isPointerOrArray(second))
{
return second;
}
if (isPointerOrArray(first) && second->kind == TYPE_INT)
{
return first;
}
fprintf(stderr, "invalid type:expected INT+INT, INT + ptr or ptr+INT but got %d and %d", first->kind, second->kind);
exit(1);
}
else if (expr->op == BO_Sub)
{
Type *first = EvaluateType(expr->first_child);
Type *second = EvaluateType(expr->second_child);
if (first->kind == TYPE_INT && second->kind == TYPE_INT)
{
return first;
}
if (isPointerOrArray(first) && second->kind == TYPE_INT)
{
return first;
}
if (isPointerOrArray(first) && isPointerOrArray(second))
{
Type *int_type = calloc(1, sizeof(Type));
int_type->kind = TYPE_INT;
return int_type;
}
fprintf(stderr, "invalid type:expected INT-INT, PTR - INT, PTR-PTR but got %d and %d", first->kind, second->kind);
exit(1);
}
else if (expr->op == BO_Div || expr->op == BO_Mul)
{
Type *first = EvaluateType(expr->first_child);
Type *second = EvaluateType(expr->second_child);
if (first->kind != TYPE_INT || second->kind != TYPE_INT)
{
fprintf(stderr, "invalid type:expected INT*/ INT but got %d and %d", first->kind, second->kind);
exit(1);
}
else
{
return first;
}
}
else if (expr->op == BO_Assign)
{
Type *first = EvaluateType(expr->first_child);
Type *second = EvaluateType(expr->second_child);
if (!typeEqual(first, second))
{
fprintf(stderr, "BO_Assign:invalid type:left hand side and right hand side must have the same types but %d and %d", first->kind, second->kind);
exit(1);
}
return first;
}
else if (expr->op == BO_Equal || expr->op == BO_Greater || expr->op == BO_GreaterEqual || expr->op == BO_NotEqual)
{
Type *first = EvaluateType(expr->first_child);
Type *second = EvaluateType(expr->second_child);
if (!typeEqual(first, second))
{
fprintf(stderr, "invalid type:left hand side and right hand side must have the same types but %d and %d", first->kind, second->kind);
exit(1);
}
Type *int_type = calloc(1, sizeof(Type));
int_type->kind = TYPE_INT;
return int_type;
}
else
{
fprintf(stderr, "types not implemented:%d", expr->op);
exit(1);
}
}
return NULL;
}
int isPointerOrArray(Type *t)
{
return t->kind == TYPE_PTR || t->kind == TYPE_ARRAY;
}
int typeEqual(Type *t1, Type *t2)
{
if (t1->kind != TYPE_PTR || t2->kind != TYPE_PTR)
{
return t1->kind == t2->kind;
}
return typeEqual(t1->ptr_to, t2->ptr_to);
}
FunctionDeclaration *findFunction(char *name)
{
FunctionDeclaration *func = function_declarations;
if (!func)
{
return NULL;
}
while (func)
{
if (!strcmp(name, func->name))
{
return func;
}
func = func->next;
}
return NULL;
}
FunctionDeclaration *insertFunction(TypeAndIdent *typeandident)
{
char *name = typeandident->identifier;
Type *type = typeandident->type;
FunctionDeclaration *newfunc = calloc(1, sizeof(FunctionDeclaration));
FunctionDeclaration *last = lastFunction();
newfunc->name = name;
newfunc->next = NULL;
if (!last)
{
function_declarations = newfunc;
}
else
{
last->next = newfunc;
}
newfunc->return_type = type;
return newfunc;
}
FunctionDeclaration *lastFunction()
{
FunctionDeclaration *func = function_declarations;
if (!func)
{
return NULL;
}
while (1)
{
if (!func->next)
{
return func;
}
func = func->next;
}
}
int getSize(Type *type)
{
if (type->kind == TYPE_INT)
{
return 4;
}
if (type->kind == TYPE_PTR)
{
return 8;
}
if (type->kind == TYPE_ARRAY)
{
return getSize(type->ptr_to) * type->array_length;
}
fprintf(stderr, "getSize:invalid type:%d", type->kind);
exit(1);
}