-
Notifications
You must be signed in to change notification settings - Fork 77
/
vmir_type.c
570 lines (480 loc) · 11.4 KB
/
vmir_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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/*
* Copyright (c) 2016 Lonelycoder AB
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
typedef enum {
IR_TYPE_STRUCT, // Must be first (0), because of forward references
IR_TYPE_VOID,
IR_TYPE_INT1,
IR_TYPE_INT8,
IR_TYPE_INT16,
IR_TYPE_INT32,
IR_TYPE_INT64,
IR_TYPE_INTx,
IR_TYPE_FLOAT,
IR_TYPE_DOUBLE,
IR_TYPE_ARRAY,
IR_TYPE_POINTER,
IR_TYPE_FUNCTION,
IR_TYPE_METADATA,
IR_TYPE_LABEL,
IR_TYPE_OPAQUE,
} ir_type_code_t;
/**
* Type
*/
typedef struct ir_type {
ir_type_code_t it_code;
union {
int it_bits; // Number of bits in IR_TYPE_INTx
struct {
int num_elements;
int element_type;
} it_array;
struct {
struct {
int type;
int offset;
} *elements;
char *name;
int num_elements;
int size;
int alignment;
char packed;
char computed;
} it_struct;
struct {
int pointee;
} it_pointer;
struct {
int *parameters;
int return_type;
int num_parameters;
int varargs;
} it_function;
};
} ir_type_t;
static int type_print(char **dst, const ir_unit_t *iu, const ir_type_t *it);
static const char *type_str_index(ir_unit_t *iu, int id);
static void type_struct_layout(ir_unit_t *iu, ir_type_t *it);
static unsigned int type_sizeof(ir_unit_t *iu, int index);
static unsigned int type_alignment(ir_unit_t *iu, int index);
/**
*
*/
static void
type_clean(ir_type_t *it)
{
switch(it->it_code) {
default:
break;
case IR_TYPE_STRUCT:
free(it->it_struct.name);
free(it->it_struct.elements);
break;
case IR_TYPE_FUNCTION:
free(it->it_function.parameters);
break;
}
}
/**
*
*/
static ir_type_t *
type_get(ir_unit_t *iu, unsigned int id)
{
if(id >= VECTOR_LEN(&iu->iu_types))
parser_error(iu, "Bad type index %d", id);
return &VECTOR_ITEM(&iu->iu_types, id);
}
/**
*
*/
static unsigned int
type_get_pointee(ir_unit_t *iu, unsigned int id)
{
ir_type_t *it = type_get(iu, id);
if(it->it_code != IR_TYPE_POINTER)
parser_error(iu, "Type (%s) (index:%d) is not a pointer",
type_str_index(iu, id), id);
if(it->it_pointer.pointee == -1)
parser_error(iu, "Attempting to dereference void pointer");
return it->it_pointer.pointee;
}
/**
*
*/
static int
type_print_id(char **dstp, const ir_unit_t *iu, int id)
{
char tmpbuf[128];
if(id < VECTOR_LEN(&iu->iu_types))
return type_print(dstp, iu, &VECTOR_ITEM(&iu->iu_types, id));
snprintf(tmpbuf, sizeof(tmpbuf), "[TypeId-%d]", id);
return addstr(dstp, tmpbuf);
}
/**
*
*/
static int
type_print(char **dst, const ir_unit_t *iu, const ir_type_t *it)
{
char tmpbuf[128];
const char *append = "type-???";
int len = 0;
switch(it->it_code) {
case IR_TYPE_VOID: append = "void"; break;
case IR_TYPE_INT1: append = "i1"; break;
case IR_TYPE_INT8: append = "i8"; break;
case IR_TYPE_INT16: append = "i16"; break;
case IR_TYPE_INT32: append = "i32"; break;
case IR_TYPE_INT64: append = "i64"; break;
case IR_TYPE_FLOAT: append = "float"; break;
case IR_TYPE_DOUBLE: append = "double"; break;
case IR_TYPE_METADATA: append = "metadata"; break;
case IR_TYPE_LABEL: append = "label"; break;
case IR_TYPE_OPAQUE: append = "opaque"; break;
case IR_TYPE_INTx:
snprintf(tmpbuf, sizeof(tmpbuf), "i%d", it->it_bits);
return addstr(dst, tmpbuf);
case IR_TYPE_STRUCT:
if(it->it_struct.name != NULL) {
len += addstr(dst, it->it_struct.name);
return len;
}
snprintf(tmpbuf, sizeof(tmpbuf), "struct {");
len += addstr(dst, tmpbuf);
for(int i = 0; i < it->it_struct.num_elements; i++) {
if(i > 0)
len += addstr(dst, " ");
len += type_print_id(dst, iu, it->it_struct.elements[i].type);
snprintf(tmpbuf, sizeof(tmpbuf), " @ %x",
it->it_struct.elements[i].offset);
len += addstr(dst, tmpbuf);
}
len += addstr(dst, "}");
return len;
case IR_TYPE_ARRAY:
len += addstr(dst, "[ ");
len += type_print_id(dst, iu, it->it_array.element_type);
snprintf(tmpbuf, sizeof(tmpbuf), " x %d ]", it->it_array.num_elements);
len += addstr(dst, tmpbuf);
return len;
case IR_TYPE_POINTER:
if(it->it_pointer.pointee == -1) {
len += addstr(dst, "void*");
} else {
len += type_print_id(dst, iu, it->it_pointer.pointee);
len += addstr(dst, "*");
}
return len;
case IR_TYPE_FUNCTION:
len += type_print_id(dst, iu, it->it_function.return_type);
len += addstr(dst, " (*)(");
for(int i = 0; i < it->it_function.num_parameters; i++) {
if(i > 0)
len += addstr(dst, ", ");
len += type_print_id(dst, iu, it->it_function.parameters[i]);
}
len += addstr(dst, ")");
return len;
}
len += addstr(dst, append);
return len;
}
/**
*
*/
static const char *
type_str(ir_unit_t *iu, const ir_type_t *it)
{
int len = type_print(NULL, iu, it);
char *dst = tmpstr(iu, len);
const char *ret = dst;
type_print(&dst, iu, it);
return ret;
}
/**
*
*/
static const char *
type_str_index(ir_unit_t *iu, int id)
{
int l = type_print_id(NULL, iu, id);
char *dst = tmpstr(iu, l);
const char *ret = dst;
type_print_id(&dst, iu, id);
return ret;
}
/**
*
*/
static int
type_make(ir_unit_t *iu, int code)
{
for(int i = 0; i < VECTOR_LEN(&iu->iu_types); i++) {
ir_type_t *it = &VECTOR_ITEM(&iu->iu_types, i);
if(it->it_code == code)
return i;
}
iu->iu_types_created = 1;
ir_type_t it;
int r = VECTOR_LEN(&iu->iu_types);
it.it_code = code;
VECTOR_PUSH_BACK(&iu->iu_types, it);
return r;
}
/**
*
*/
static int
type_make_pointer(ir_unit_t *iu, int type, int may_create)
{
for(int i = 0; i < VECTOR_LEN(&iu->iu_types); i++) {
ir_type_t *it = &VECTOR_ITEM(&iu->iu_types, i);
if(it->it_code == IR_TYPE_POINTER && it->it_pointer.pointee == type)
return i;
}
if(!may_create)
return -1;
iu->iu_types_created = 1;
ir_type_t it;
int r = VECTOR_LEN(&iu->iu_types);
it.it_code = IR_TYPE_POINTER;
it.it_pointer.pointee = type;
VECTOR_PUSH_BACK(&iu->iu_types, it);
return r;
}
/**
*
*/
static int
type_find_by_code(ir_unit_t *iu, ir_type_code_t code)
{
for(int i = 0; i < VECTOR_LEN(&iu->iu_types); i++) {
if(VECTOR_ITEM(&iu->iu_types, i).it_code == code)
return i;
}
parser_error(iu, "Unable to find type class %d", code);
}
/**
*
*/
static unsigned int
type_sizeof_ptr(ir_unit_t *iu, const ir_type_t *it)
{
switch(it->it_code) {
case IR_TYPE_VOID:
return 0;
case IR_TYPE_INT1:
case IR_TYPE_INT8:
return 1;
case IR_TYPE_INT16:
return 2;
case IR_TYPE_INT32:
case IR_TYPE_FLOAT:
return 4;
case IR_TYPE_INT64:
case IR_TYPE_DOUBLE:
return 8;
case IR_TYPE_INTx:
if(it->it_bits <= 32)
return 4;
if(it->it_bits <= 64) {
return 8;
}
goto bad;
case IR_TYPE_POINTER:
return 4;
case IR_TYPE_STRUCT:
return it->it_struct.size;
case IR_TYPE_ARRAY:
return type_sizeof(iu, it->it_array.element_type) *
it->it_array.num_elements;
default:
bad:
parser_error(iu, "Unable to compute size of type %s\n",
type_str(iu, it));
}
}
/**
*
*/
static unsigned int
type_sizeof(ir_unit_t *iu, int index)
{
return type_sizeof_ptr(iu, type_get(iu, index));
}
/**
*
*/
static uint32_t
type_code_mask(ir_type_code_t code)
{
switch(code) {
case IR_TYPE_INT1:
return 0xff;
case IR_TYPE_INT8:
return 0xff;
case IR_TYPE_INT16:
return 0xffff;
default:
return 0xffffffff;
}
}
/**
*
*/
static unsigned int
type_bitwidth(ir_unit_t *iu, const ir_type_t *it)
{
switch(it->it_code) {
case IR_TYPE_VOID:
return 0;
case IR_TYPE_INT1:
return 1;
case IR_TYPE_INT8:
return 8;
case IR_TYPE_INT16:
return 16;
case IR_TYPE_INT32:
return 32;
case IR_TYPE_INT64:
return 64;
case IR_TYPE_INTx:
return it->it_bits;
default:
parser_error(iu, "Unable to compute bitwidth of type %s\n",
type_str(iu, it));
}
}
/**
*
*/
static unsigned int
type_alignment_ptr(ir_unit_t *iu, const ir_type_t *it)
{
switch(it->it_code) {
case IR_TYPE_VOID:
return 0;
case IR_TYPE_INT1:
case IR_TYPE_INT8:
return 1;
case IR_TYPE_INT16:
return 2;
case IR_TYPE_INT32:
case IR_TYPE_FLOAT:
return 4;
case IR_TYPE_INT64:
case IR_TYPE_DOUBLE:
return 8;
case IR_TYPE_INTx:
if(it->it_bits <= 32)
return 4;
if(it->it_bits <= 64) {
return 8;
}
goto bad;
case IR_TYPE_POINTER:
return 4;
case IR_TYPE_STRUCT:
return it->it_struct.alignment;
case IR_TYPE_ARRAY:
return type_alignment(iu, it->it_array.element_type);
default:
bad:
parser_error(iu, "Unable to compute alignment for type %s\n",
type_str(iu, it));
}
}
static unsigned int
type_alignment(ir_unit_t *iu, int index)
{
return type_alignment_ptr(iu, type_get(iu, index));
}
/**
*
*/
static void __attribute__((unused))
type_print_list(ir_unit_t *iu)
{
for(int i = 0; i < VECTOR_LEN(&iu->iu_types); i++) {
printf("Type-%-5d %s\n", i, type_str_index(iu, i));
}
}
/**
*
*/
static int
legalize_type(const ir_type_t *ty)
{
if(ty->it_code == IR_TYPE_INTx) {
if(ty->it_bits <= 8)
return IR_TYPE_INT8;
else if(ty->it_bits <= 16)
return IR_TYPE_INT16;
else if(ty->it_bits <= 32)
return IR_TYPE_INT32;
else if(ty->it_bits <= 64)
return IR_TYPE_INT64;
}
return ty->it_code;
}
/**
*
*/
static void
type_struct_layout(ir_unit_t *iu, ir_type_t *it)
{
int offset = 0;
int ba = 1; // Biggest alignment
const int packed = it->it_struct.packed;
for(int i = 0; i < it->it_struct.num_elements; i++) {
ir_type_t *ty = type_get(iu, it->it_struct.elements[i].type);
if(ty->it_code == IR_TYPE_STRUCT && !ty->it_struct.computed)
type_struct_layout(iu, ty);
int s = type_sizeof_ptr(iu, ty);
if(!packed) {
int a = type_alignment_ptr(iu, ty);
offset = VMIR_ALIGN(offset, a);
ba = VMIR_MAX(ba, a);
}
it->it_struct.elements[i].offset = offset;
offset += s;
}
it->it_struct.size = packed ? offset : VMIR_ALIGN(offset, ba);
it->it_struct.alignment = ba;
it->it_struct.computed = 1;
}
/**
*
*/
static void
types_finalize(ir_unit_t *iu)
{
for(int i = 0; i < VECTOR_LEN(&iu->iu_types); i++) {
ir_type_t *it = &VECTOR_ITEM(&iu->iu_types, i);
if(it->it_code == IR_TYPE_STRUCT && !it->it_struct.computed) {
type_struct_layout(iu, it);
}
}
}