-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.c
348 lines (266 loc) · 7.49 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
#include "type.h"
#define TYPE_TABLE_SIZE (1llu<<TYPEID_INDEX_BITS)
static Type* ts_table = null;
static u32 ts_table_head;
static inline TypeKind ts_get_kind(TypeID id) { return id >> TYPEID_INDEX_BITS; }
static inline u32 ts_get_index(TypeID id) { return id & ((1llu<<TYPEID_INDEX_BITS)-1); }
static inline Type* ts_get_info(TypeID id) { return ts_table + ts_get_index(id); }
static inline u64 ts_get_size(TypeID id) { return ts_get_info(id)->size; }
static void ts_init(void) {
ts_table = alloc(sizeof(Type)*TYPE_TABLE_SIZE);
ts_table_head = LARGEST_CORE_TYPE_INDEX+1;
*ts_get_info(TYPE_BYTE) = (Type){ .size = 1 };
*ts_get_info(TYPE_BOOL) = (Type){ .size = 1 };
*ts_get_info(TYPE_TYPEID) = (Type){ .size = 8 };
*ts_get_info(TYPE_INT8) = (Type){ .size = 1 };
*ts_get_info(TYPE_INT16) = (Type){ .size = 2 };
*ts_get_info(TYPE_INT32) = (Type){ .size = 4 };
*ts_get_info(TYPE_INT64) = (Type){ .size = 8 };
*ts_get_info(TYPE_UINT8) = (Type){ .size = 1 };
*ts_get_info(TYPE_UINT16) = (Type){ .size = 2 };
*ts_get_info(TYPE_UINT32) = (Type){ .size = 4 };
*ts_get_info(TYPE_UINT64) = (Type){ .size = 8 };
*ts_get_info(TYPE_FLOAT32) = (Type){ .size = 4 };
*ts_get_info(TYPE_FLOAT64) = (Type){ .size = 8 };
*ts_get_info(ts_get_index(TYPE_EMPTY_TUPLE)) = (Type){ .size = 0, .length = 0 };
}
static inline u64 ts_get_tuple_length(TypeID type) {
assert(type);
assert(ts_get_kind(type) == TYPE_KIND_TUPLE);
return ts_get_info(type)->length;
}
static inline u64 ts_get_fixed_length(TypeID type) {
assert(type);
assert(ts_get_kind(type) == TYPE_KIND_FIXED);
return ts_get_info(type)->length;
}
static inline TypeID ts_create(TypeKind kind, Type value) {
TypeID id = MAKE_TYPEID(kind, ts_table_head++);
Type* type = ts_get_info(id);
*type = value;
return id;
}
static void xtable_push(XTable* table, TypeExtent ext) {
if (is_pow2(table->length+1)) {
table->exts = realloc(
table->exts,
sizeof(TypeExtent) * table->length,
sizeof(TypeExtent) * ((table->length+1)*2)
);
}
table->exts[table->length++] = ext;
}
static TypeID ts_get_ptr(TypeID subtype) {
assert(subtype);
Type* subinfo = ts_get_info(subtype);
if (subinfo->ptr)
return subinfo->ptr;
TypeID newid = ts_create(TYPE_KIND_PTR, (Type){
.subtype = subtype,
.size = 8,
});
subinfo->ptr = newid;
return newid;
}
static TypeID ts_get_array(TypeID subtype) {
assert(subtype);
Type* subinfo = ts_get_info(subtype);
if (subinfo->array)
return subinfo->array;
TypeID newid = ts_create(TYPE_KIND_ARRAY, (Type){
.subtype = subtype,
.size = 16,
});
subinfo->array = newid;
return newid;
}
// If get_function_type or get_tuple_type lookups are too slow we should start using binary search
static TypeID ts_get_func(TypeID input, TypeID output) {
assert(input);
assert(output);
Type* input_info = ts_get_info(input);
Type* output_info = ts_get_info(output);
XTable* xtable = &input_info->xtable;
for (u64 i = 0; i < xtable->length; i++) {
TypeExtent* ext = &xtable->exts[i];
if (ext->output != output)
continue;
if (ts_get_kind(ext->type) != TYPE_KIND_FUNCTION)
continue;
return ext->type;
}
TypeID newid = ts_create(TYPE_KIND_FUNCTION, (Type){
.input = input,
.output = output,
.size = 0,
});
xtable_push(xtable, (TypeExtent){
.output = output,
.type = newid
});
return newid;
}
static TypeID ts_get_tuple(TypeID* types, u64 count) {
if (!count)
return TYPE_EMPTY_TUPLE;
TypeID head_id = types[0];
Type* head_info = ts_get_info(head_id);
if (count == 1)
return head_id;
XTable* xtable = &head_info->xtable;
for (u64 i = 0; i < xtable->length; i++) {
TypeExtent* ext = &xtable->exts[i];
if (ext->length != count)
continue;
if (ext->output != types[1])
continue;
if (ts_get_kind(ext->type) != TYPE_KIND_TUPLE)
continue;
if (count > 2 && !compare(types+2, ts_get_info(ext->type)->elements+2, (count-2)*sizeof(TypeID)))
continue;
return ext->type;
}
TypeID* elems = alloc(count*sizeof(TypeID));
u64 size = 0;
for (u64 i = 0; i < count; i++) {
elems[i] = types[i];
size += ts_get_size(types[i]);
}
TypeID newid = ts_create(TYPE_KIND_TUPLE, (Type){
.size = size,
.length = count,
.elements = elems,
});
xtable_push(xtable, (TypeExtent){
.length = count,
.type = newid,
.output = types[1],
});
return newid;
}
static TypeID ts_get_fixed(TypeID subtype, u64 length) {
assert(subtype);
assert(length);
Type* subinfo = ts_get_info(subtype);
XTable* xtable = &subinfo->xtable;
for (u64 i = 0; i < xtable->length; i++) {
TypeExtent* ext = xtable->exts+i;
if (ext->length != length)
continue;
if (ts_get_kind(ext->type) != TYPE_KIND_FIXED)
continue;
return ext->type;
}
u64 size = length * ts_get_size(subtype);
TypeID newid = ts_create(TYPE_KIND_FIXED, (Type){
.size = size,
.length = length,
.subtype = subtype,
});
xtable_push(xtable, (TypeExtent){
.length = length,
.output = subtype,
.type = newid,
});
return newid;
}
static TypeID ts_get_subtype(TypeID type) {
TypeKind kind = ts_get_kind(type);
assert(ts_is_specifier(type));
Type* p = ts_get_info(type);
return p->subtype;
}
static TypeID ts_get_integral_type(TypeID type) {
switch (type) {
case TYPE_BYTE: return TYPE_INT8;
case TYPE_BOOL: return TYPE_INT8;
case TYPE_TYPEID: return TYPE_UINT32;
case TYPE_INT8:
case TYPE_INT16:
case TYPE_INT32:
case TYPE_INT64:
case TYPE_UINT8:
case TYPE_UINT16:
case TYPE_UINT32:
case TYPE_UINT64:
case TYPE_FLOAT32:
case TYPE_FLOAT64:
return type;
default:
return 0;
}
}
static TypeID ts_get_unsigned(TypeID type) {
assert(ts_is_int(type));
switch (type) {
case TYPE_INT8: return TYPE_UINT8;
case TYPE_INT16: return TYPE_UINT16;
case TYPE_INT32: return TYPE_UINT32;
case TYPE_INT64: return TYPE_UINT64;
case TYPE_UINT8:
case TYPE_UINT16:
case TYPE_UINT32:
case TYPE_UINT64:
return type;
}
assert_unreachable();
}
static TypeID ts_get_signed(TypeID type) {
assert(ts_is_int(type));
switch (type) {
case TYPE_INT8:
case TYPE_INT16:
case TYPE_INT32:
case TYPE_INT64:
return type;
case TYPE_UINT8: return TYPE_INT8;
case TYPE_UINT16: return TYPE_INT16;
case TYPE_UINT32: return TYPE_INT32;
case TYPE_UINT64: return TYPE_INT64;
}
assert_unreachable();
}
static TypeID ts_get_function_return_type(TypeID type) {
print("type = %\n", arg_type(type));
assert(ts_get_kind(type) == TYPE_KIND_FUNCTION);
return ts_get_info(type)->output;
}
static TypeID ts_get_function_input_type(TypeID type) {
assert(ts_get_kind(type) == TYPE_KIND_FUNCTION);
return ts_get_info(type)->input;
}
static bool ts_is_integral_type(TypeID type) {
return ts_get_integral_type(type) != 0;
}
static bool ts_is_int(TypeID type) {
return ts_is_signed(type) || ts_is_unsigned(type);
}
static bool ts_is_unsigned(TypeID type) {
return type >= TYPE_UINT8 && type <= TYPE_UINT64;
}
static bool ts_is_signed(TypeID type) {
return type >= TYPE_INT8 && type <= TYPE_INT64;
}
static bool ts_is_float(TypeID type) {
return type >= TYPE_FLOAT32 && type <= TYPE_FLOAT64;
}
static bool ts_is_ptr(TypeID type) {
return ts_get_kind(type) == TYPE_KIND_PTR;
}
static bool ts_is_specifier(TypeID type) {
switch (ts_get_kind(type)) {
case TYPE_KIND_PTR:
case TYPE_KIND_ARRAY:
case TYPE_KIND_FIXED:
return true;
case TYPE_KIND_STRUCT:
case TYPE_KIND_ENUM:
case TYPE_KIND_TUPLE:
case TYPE_KIND_FUNCTION:
case TYPE_KIND_PRIMITIVE:
return false;
default:
// print("ts_is_specifier(%)\n", arg_type(type));
assert_unreachable();
}
}