-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.c
314 lines (272 loc) · 6.09 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
#include "boncc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
char *type_text(TypeKind kind) {
switch (kind) {
case TYPE_VOID:
return "void";
case TYPE_INT:
return "int";
case TYPE_CHAR:
return "char";
case TYPE_SHORT:
return "short";
case TYPE_LONG:
return "long";
case TYPE_UINT:
return "unsigned int";
case TYPE_UCHAR:
return "unsigned char";
case TYPE_USHORT:
return "unsigned short";
case TYPE_ULONG:
return "unsigned long";
case TYPE_FLOAT:
return "float";
case TYPE_DOUBLE:
return "double";
case TYPE_LDOUBLE:
return "long double";
case TYPE_BOOL:
return "_Bool";
case TYPE_PTR:
return "ptr";
case TYPE_ARRAY:
return "array";
case TYPE_STRUCT:
return "struct";
case TYPE_UNION:
return "union";
case TYPE_ENUM:
return "enum";
case TYPE_FUNC:
return "function";
case TYPE_NONE:
return "none";
}
assert(false);
return NULL;
}
Type *base_type(TypeKind kind) {
Type *t = calloc(1, sizeof(Type));
t->kind = kind;
switch (kind) {
case TYPE_VOID:
case TYPE_CHAR:
case TYPE_UCHAR:
case TYPE_BOOL:
t->align = t->size = 1;
break;
case TYPE_SHORT:
case TYPE_USHORT:
t->align = t->size = 2;
break;
case TYPE_INT:
case TYPE_UINT:
case TYPE_FLOAT:
t->align = t->size = 4;
break;
case TYPE_LONG:
case TYPE_ULONG:
case TYPE_DOUBLE:
t->align = t->size = 8;
break;
case TYPE_LDOUBLE:
t->align = t->size = 16;
break;
default:
assert(false);
}
return t;
}
Type *pointer_type(Type *base) {
Type *t = calloc(1, sizeof(Type));
t->kind = TYPE_PTR;
t->base = base;
t->objdec = base->objdec;
t->align = t->size = 8;
return t;
}
Type *array_type(Type *base, int len) {
Type *t = calloc(1, sizeof(Type));
t->kind = TYPE_ARRAY;
t->base = base;
t->objdec = base->objdec;
t->array_size = len;
t->size = base->size * len;
t->align = base->align;
return t;
}
Type *struct_type(bool is_unnamed) {
Type *t = calloc(1, sizeof(Type));
t->kind = TYPE_STRUCT;
t->size = -1;
t->is_unnamed = is_unnamed;
return t;
}
Type *union_type(bool is_unnamed) {
Type *t = calloc(1, sizeof(Type));
t->kind = TYPE_UNION;
t->size = -1;
t->is_unnamed = is_unnamed;
return t;
}
Type *enum_type(bool is_unnamed) {
Type *t = calloc(1, sizeof(Type));
t->kind = TYPE_ENUM;
t->size = -1;
t->align = 4;
t->is_unnamed = is_unnamed;
return t;
}
Type *func_type(Type *return_type) {
Type *t = calloc(1, sizeof(Type));
t->kind = TYPE_FUNC;
t->align = t->size = 0;
t->params = new_vector(0, sizeof(Variable *));
t->return_type = return_type;
t->objdec = return_type->objdec;
return t;
}
bool same_type(Type *a, Type *b) {
assert(a);
assert(b);
if (a == b)
return true;
if (a->kind != b->kind)
return false;
if (a->kind == TYPE_STRUCT || a->kind == TYPE_UNION || a->kind == TYPE_ENUM)
return a == b; // for user defined types, compare as pointers
if (a->kind == TYPE_ARRAY || a->kind == TYPE_PTR)
return same_type(a->base, b->base);
if (a->kind == TYPE_FUNC) {
if (!same_type(a->return_type, b->return_type))
return false;
if (a->params->size != b->params->size || a->is_variadic != b->is_variadic)
return false;
for (int i = 0; i < a->params->size; ++i) {
Type *ap = *(Type **)vector_get(a->params, i);
Type *bp = *(Type **)vector_get(b->params, i);
if (!same_type(ap, bp))
return false;
}
}
return true;
}
bool is_float(Type *type) {
assert(type);
switch (type->kind) {
case TYPE_FLOAT:
case TYPE_DOUBLE:
case TYPE_LDOUBLE:
return true;
default:
return false;
}
}
bool is_integer(Type *type) {
assert(type);
switch (type->kind) {
case TYPE_INT:
case TYPE_CHAR:
case TYPE_SHORT:
case TYPE_LONG:
case TYPE_UINT:
case TYPE_UCHAR:
case TYPE_USHORT:
case TYPE_ULONG:
case TYPE_ENUM:
case TYPE_BOOL:
return true;
default:
return false;
}
}
bool is_scalar(Type *type) {
assert(type);
return is_integer(type) || is_float(type) || type->kind == TYPE_PTR;
}
bool is_numerical(Type *type) {
assert(type);
return is_integer(type) || is_float(type);
}
bool is_signed(Type *type) {
assert(type);
switch (type->kind) {
case TYPE_INT:
case TYPE_CHAR:
case TYPE_SHORT:
case TYPE_LONG:
case TYPE_ENUM:
return true;
default:
return false;
}
}
bool is_unsigned(Type *type) {
assert(type);
switch (type->kind) {
case TYPE_UINT:
case TYPE_UCHAR:
case TYPE_USHORT:
case TYPE_ULONG:
case TYPE_PTR:
return true;
default:
return false;
}
}
bool is_funcptr(Type *type) {
return type->kind == TYPE_PTR && type->base->kind == TYPE_FUNC;
}
bool is_struct_union(Type *type) {
return type->kind == TYPE_STRUCT || type->kind == TYPE_UNION;
}
bool pass_on_memory(Type *type) {
return is_struct_union(type) && type->size > 16;
}
Type *implicit_type_conversion(Type *l, Type *r) {
assert(!is_struct_union(l));
assert(!is_struct_union(r));
assert(l->kind != TYPE_VOID);
assert(r->kind != TYPE_VOID);
assert(!(is_float(l) && (r->kind == TYPE_ARRAY || r->kind == TYPE_PTR)));
assert(!(is_float(r) && (l->kind == TYPE_ARRAY || l->kind == TYPE_PTR)));
if (l->kind == TYPE_ARRAY)
return pointer_type(l->base);
if (r->kind == TYPE_ARRAY)
return pointer_type(r->base);
if (l->kind == TYPE_PTR)
return l;
if (r->kind == TYPE_PTR)
return r;
if (is_float(l) && is_float(r))
return l->size > r->size ? l : r;
if (is_float(l))
return l;
if (is_float(r))
return r;
if (l->size > r->size)
return l;
if (l->size < r->size)
return r;
if (is_unsigned(r))
return r;
return l;
}
bool castable(Type *from, Type *to) {
assert(from->kind != TYPE_FUNC);
assert(to->kind != TYPE_FUNC);
assert(from->kind != TYPE_NONE);
assert(to->kind != TYPE_NONE);
if (same_type(from, to))
return true;
if (from->kind == TYPE_ARRAY && is_scalar(to))
return true;
if (!is_scalar(from) && to->kind != TYPE_VOID)
return false;
if (from->kind == TYPE_VOID && to->kind != TYPE_VOID)
return false;
return true;
}