-
Notifications
You must be signed in to change notification settings - Fork 1
/
CirType.c
1573 lines (1435 loc) · 48.4 KB
/
CirType.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "cir_internal.h"
#include <assert.h>
#include <stdalign.h>
#include <stdlib.h>
// data1:
// bits 31 to 28: type
// bits 27 to 21: ikind/fkind/num params (7 bits) [u1]
// bits 20: isva/array len (1 bit) [u2]
// bits 19 to 15: num attrs (5 bits)
// type (4 bits)
#define CIR_TVOID 0
#define CIR_TINT 1
#define CIR_TFLOAT 2
#define CIR_TPTR 3
#define CIR_TARRAY 4
#define CIR_TFUN 5
#define CIR_TNAMED 6
#define CIR_TCOMP 7
#define CIR_TENUM 8
#define CIR_TVALIST 9
#define data1ToType(x) (((x) >> 28) & 0x0f)
#define typeToData1(x) (((x) & 0x0f) << 28)
#define data1ToU1(x) (((x) >> 21) & 0x7f)
#define u1ToData1(x) (((x) & 0x7f) << 21)
#define data1ToU2(x) (((x) >> 20) & 0x01)
#define u2ToData1(x) (((x) & 0x01) << 20)
#define data1ToNumAttrs(x) (((x) >> 15) & 0x1f)
#define numAttrsToData1(x) (((x) & 0x1f) << 15)
#define MAX_ATTRS 0x1f
#define MAX_FUN_PARAMS 0x7f
typedef struct CirType {
uint32_t data1;
const CirAttr *attrs[];
} CirType;
typedef struct CirTypePtr {
uint32_t data1;
const struct CirType *baseType;
const CirAttr *attrs[];
} CirTypePtr;
typedef struct CirTypeArray {
uint32_t data1;
uint32_t arrayLen;
const struct CirType *baseType;
const CirAttr *attrs[];
} CirTypeArray;
typedef struct CirTypeNamed {
uint32_t data1;
CirTypedefId typedefId;
const CirAttr *attrs[];
} CirTypeNamed;
typedef struct CirTypeComp {
uint32_t data1;
CirCompId compId;
const CirAttr *attrs[];
} CirTypeComp;
typedef struct CirTypeEnum {
uint32_t data1;
CirEnumId enumId;
const CirAttr *attrs[];
} CirTypeEnum;
typedef struct CirTypeFun {
uint32_t data1;
const struct CirType *baseType;
CirFunParam funParams[];
} CirTypeFun;
_Static_assert(alignof(CirFunParam) >= alignof(const CirAttr *), "alignment");
static const CirType voidType = {
.data1 = typeToData1(CIR_TVOID),
};
static const CirType shortType = {
.data1 = typeToData1(CIR_TINT) | u1ToData1(CIR_ISHORT),
};
static const CirType ushortType = {
.data1 = typeToData1(CIR_TINT) | u1ToData1(CIR_IUSHORT),
};
static const CirType intType = {
.data1 = typeToData1(CIR_TINT) | u1ToData1(CIR_IINT),
};
static const CirType uintType = {
.data1 = typeToData1(CIR_TINT) | u1ToData1(CIR_IUINT),
};
static const CirType longType = {
.data1 = typeToData1(CIR_TINT) | u1ToData1(CIR_ILONG),
};
static const CirType ulongType = {
.data1 = typeToData1(CIR_TINT) | u1ToData1(CIR_IULONG),
};
static const CirType charType = {
.data1 = typeToData1(CIR_TINT) | u1ToData1(CIR_ICHAR),
};
static const CirType scharType = {
.data1 = typeToData1(CIR_TINT) | u1ToData1(CIR_ISCHAR),
};
static const CirType ucharType = {
.data1 = typeToData1(CIR_TINT) | u1ToData1(CIR_IUCHAR),
};
static const CirType boolType = {
.data1 = typeToData1(CIR_TINT) | u1ToData1(CIR_IBOOL),
};
static const CirType longlongType = {
.data1 = typeToData1(CIR_TINT) | u1ToData1(CIR_ILONGLONG),
};
static const CirType ulonglongType = {
.data1 = typeToData1(CIR_TINT) | u1ToData1(CIR_IULONGLONG),
};
static const CirType floatType = {
.data1 = typeToData1(CIR_TFLOAT) | u1ToData1(CIR_FFLOAT),
};
static const CirType doubleType = {
.data1 = typeToData1(CIR_TFLOAT) | u1ToData1(CIR_FDOUBLE),
};
static const CirType longdoubleType = {
.data1 = typeToData1(CIR_TFLOAT) | u1ToData1(CIR_FLONGDOUBLE),
};
static const CirType f128Type = {
.data1 = typeToData1(CIR_TFLOAT) | u1ToData1(CIR_F128),
};
static const CirType valistType = {
.data1 = typeToData1(CIR_TVALIST),
};
bool
CirType_isVoid(const CirType *type)
{
assert(type);
return data1ToType(type->data1) == CIR_TVOID;
}
uint32_t
CirType_isInt(const CirType *type)
{
assert(type);
if (data1ToType(type->data1) != CIR_TINT)
return 0;
return data1ToU1(type->data1);
}
uint32_t
CirType_isFloat(const CirType *type)
{
assert(type);
if (data1ToType(type->data1) != CIR_TFLOAT)
return 0;
return data1ToU1(type->data1);
}
bool
CirType_isArithmetic(const CirType *type)
{
return CirType_isInt(type) || CirType_isFloat(type);
}
bool
CirType_isPtr(const CirType *type)
{
assert(type);
return data1ToType(type->data1) == CIR_TPTR;
}
bool
CirType_isArray(const CirType *type)
{
assert(type);
return data1ToType(type->data1) == CIR_TARRAY;
}
bool
CirType_isFun(const CirType *type)
{
assert(type);
return data1ToType(type->data1) == CIR_TFUN;
}
bool
CirType_isNamed(const CirType *type)
{
assert(type);
return data1ToType(type->data1) == CIR_TNAMED;
}
bool
CirType_isComp(const CirType *type)
{
assert(type);
return data1ToType(type->data1) == CIR_TCOMP;
}
bool
CirType_isEnum(const CirType *type)
{
assert(type);
return data1ToType(type->data1) == CIR_TENUM;
}
bool
CirType_isVaList(const CirType *type)
{
assert(type);
return data1ToType(type->data1) == CIR_TVALIST;
}
const CirType *
CirType_getBaseType(const CirType *type)
{
assert(type);
switch (data1ToType(type->data1)) {
case CIR_TPTR:
return ((const CirTypePtr *) type)->baseType;
case CIR_TARRAY:
return ((const CirTypeArray *)type)->baseType;
case CIR_TFUN:
return ((const CirTypeFun *)type)->baseType;
default:
cir_fatal("CirType_getBaseType called on leaf type");
}
}
CirTypedefId
CirType_getTypedefId(const CirType *type)
{
assert(type);
if (data1ToType(type->data1) != CIR_TNAMED)
cir_fatal("CirType_getTypedefId called on non-named type");
return ((const CirTypeNamed *)type)->typedefId;
}
CirCompId
CirType_getCompId(const CirType *type)
{
assert(type);
if (data1ToType(type->data1) != CIR_TCOMP)
cir_fatal("CirType_getCompId called on non-comp type");
return ((const CirTypeComp *)type)->compId;
}
CirEnumId
CirType_getEnumId(const CirType *type)
{
assert(type);
if (data1ToType(type->data1) != CIR_TENUM)
cir_fatal("CirType_getEnumId called on non-enum type");
return ((const CirTypeEnum *)type)->enumId;
}
static void
copyAttrs(const CirAttr ** restrict dst, const CirAttr * const * restrict src, uint32_t numAttrs)
{
for (uint32_t i = 0; i < numAttrs; i++) {
if (i > 0 && CirAttr_getName(src[i-1]) >= CirAttr_getName(src[i]))
cir_bug("copyAttrs: src attrs is not sorted and unique");
dst[i] = src[i];
}
}
const CirType *
CirType__void(const CirAttr * const *attrs, uint32_t numAttrs)
{
if (!numAttrs)
return &voidType;
if (numAttrs > MAX_ATTRS)
cir_bug("too many attrs");
CirType *out = CirMem_balloc(sizeof(*out) + sizeof(*attrs) * numAttrs, alignof(*out));
out->data1 = typeToData1(CIR_TVOID) | numAttrsToData1(numAttrs);
copyAttrs(out->attrs, attrs, numAttrs);
return out;
}
const CirType *
CirType_void(void)
{
return CirType__void(NULL, 0);
}
const CirType *
CirType__int(uint32_t ikind, const CirAttr * const *attrs, uint32_t numAttrs)
{
if (numAttrs > MAX_ATTRS)
cir_bug("too many attrs");
if (numAttrs) {
CirType *out = CirMem_balloc(sizeof(*out) + sizeof(*attrs) * numAttrs, alignof(*out));
out->data1 = typeToData1(CIR_TINT) | u1ToData1(ikind) | numAttrsToData1(numAttrs);
copyAttrs(out->attrs, attrs, numAttrs);
return out;
}
switch (ikind) {
case CIR_ICHAR:
return &charType;
case CIR_ISCHAR:
return &scharType;
case CIR_IUCHAR:
return &ucharType;
case CIR_IBOOL:
return &boolType;
case CIR_IINT:
return &intType;
case CIR_IUINT:
return &uintType;
case CIR_ISHORT:
return &shortType;
case CIR_IUSHORT:
return &ushortType;
case CIR_ILONG:
return &longType;
case CIR_IULONG:
return &ulongType;
case CIR_ILONGLONG:
return &longlongType;
case CIR_IULONGLONG:
return &ulonglongType;
default:
cir_bug("unknown ikind");
}
}
const CirType *
CirType_int(uint32_t ikind)
{
return CirType__int(ikind, NULL, 0);
}
const CirType *
CirType__float(uint32_t fkind, const CirAttr * const *attrs, uint32_t numAttrs)
{
if (numAttrs > MAX_ATTRS)
cir_bug("too many attrs");
if (numAttrs) {
CirType *out = CirMem_balloc(sizeof(*out) + sizeof(*attrs) * numAttrs, alignof(*out));
out->data1 = typeToData1(CIR_TFLOAT) | u1ToData1(fkind) | numAttrsToData1(numAttrs);
copyAttrs(out->attrs, attrs, numAttrs);
return out;
}
switch (fkind) {
case CIR_FFLOAT:
return &floatType;
case CIR_FDOUBLE:
return &doubleType;
case CIR_FLONGDOUBLE:
return &longdoubleType;
case CIR_F128:
return &f128Type;
default:
cir_bug("invalid fkind");
}
}
const CirType *
CirType_float(uint32_t fkind)
{
return CirType__float(fkind, NULL, 0);
}
const CirType *
CirType__typedef(CirTypedefId tid, const CirAttr * const *attrs, uint32_t numAttrs)
{
if (numAttrs > MAX_ATTRS)
cir_bug("too many attrs");
CirTypeNamed *out = CirMem_balloc(sizeof(*out) + sizeof(*attrs) * numAttrs, alignof(*out));
out->data1 = typeToData1(CIR_TNAMED) | numAttrsToData1(numAttrs);
out->typedefId = tid;
copyAttrs(out->attrs, attrs, numAttrs);
return (CirType *)out;
}
const CirType *
CirType_typedef(CirTypedefId tid)
{
return CirType__typedef(tid, NULL, 0);
}
const CirType *
CirType__comp(CirCompId cid, const CirAttr * const *attrs, uint32_t numAttrs)
{
if (numAttrs > MAX_ATTRS)
cir_bug("too many attrs");
CirTypeComp *out = CirMem_balloc(sizeof(*out) + sizeof(*attrs) * numAttrs, alignof(*out));
out->data1 = typeToData1(CIR_TCOMP) | numAttrsToData1(numAttrs);
out->compId = cid;
copyAttrs(out->attrs, attrs, numAttrs);
return (CirType *)out;
}
const CirType *
CirType_comp(CirCompId cid)
{
return CirType__comp(cid, NULL, 0);
}
const CirType *
CirType__enum(CirEnumId enumId, const CirAttr * const *attrs, uint32_t numAttrs)
{
if (numAttrs > MAX_ATTRS)
cir_bug("too many attrs");
CirTypeEnum *out = CirMem_balloc(sizeof(*out) + sizeof(*attrs) * numAttrs, alignof(*out));
out->data1 = typeToData1(CIR_TENUM) | numAttrsToData1(numAttrs);
out->enumId = enumId;
copyAttrs(out->attrs, attrs, numAttrs);
return (CirType *)out;
}
const CirType *
CirType_enum(CirEnumId enumId)
{
return CirType__enum(enumId, NULL, 0);
}
const CirType *
CirType__ptr(const CirType *bt, const CirAttr * const *attrs, size_t numAttrs)
{
assert(bt != NULL);
if (numAttrs > MAX_ATTRS)
cir_bug("too many attrs");
CirTypePtr *out = CirMem_balloc(sizeof(*out) + sizeof(*attrs) * numAttrs, alignof(*out));
out->data1 = typeToData1(CIR_TPTR) | numAttrsToData1(numAttrs);
out->baseType = bt;
copyAttrs(out->attrs, attrs, numAttrs);
return (CirType *)out;
}
const CirType *
CirType_ptr(const CirType *bt)
{
return CirType__ptr(bt, NULL, 0);
}
const CirType *
CirType__array(const CirType *bt, const CirAttr * const *attrs, uint32_t numAttrs)
{
assert(bt != NULL);
if (numAttrs > MAX_ATTRS)
cir_bug("too many attrs");
CirTypeArray *out = CirMem_balloc(sizeof(*out) + sizeof(*attrs) * numAttrs, alignof(*out));
out->data1 = typeToData1(CIR_TARRAY) | numAttrsToData1(numAttrs);
out->arrayLen = 0;
out->baseType = bt;
copyAttrs(out->attrs, attrs, numAttrs);
return (CirType *)out;
}
const CirType *
CirType_array(const CirType *bt)
{
return CirType__array(bt, NULL, 0);
}
const CirType *
CirType__arrayWithLen(const CirType *bt, uint32_t len, const CirAttr * const *attrs, uint32_t numAttrs)
{
assert(bt != NULL);
if (numAttrs > MAX_ATTRS)
cir_bug("too many attrs");
CirTypeArray *out = CirMem_balloc(sizeof(*out) + sizeof(*attrs) * numAttrs, alignof(*out));
out->data1 = typeToData1(CIR_TARRAY) | u2ToData1(1) | numAttrsToData1(numAttrs);
out->arrayLen = len;
out->baseType = bt;
copyAttrs(out->attrs, attrs, numAttrs);
return (CirType *)out;
}
const CirType *
CirType_arrayWithLen(const CirType *bt, uint32_t len)
{
return CirType__arrayWithLen(bt, len, NULL, 0);
}
const CirType *
CirType__fun(const CirType *bt, const CirFunParam *params, size_t numParams, bool isVa, const CirAttr * const *attrs, size_t numAttrs)
{
assert(bt != NULL);
if (numAttrs > MAX_ATTRS)
cir_bug("too many attrs");
if (numParams > MAX_FUN_PARAMS)
cir_bug("too many params");
CirTypeFun *out = CirMem_balloc(sizeof(*out) + sizeof(*params) * numParams + sizeof(*attrs) * numAttrs, alignof(*out));
out->data1 = typeToData1(CIR_TFUN) | u1ToData1(numParams) | u2ToData1(isVa) | numAttrsToData1(numAttrs);
out->baseType = bt;
// Copy params
for (size_t i = 0; i < numParams; i++)
out->funParams[i] = params[i];
const CirFunParam *lastParam = out->funParams + numParams;
const CirAttr **outAttrs = (const CirAttr **)lastParam;
copyAttrs(outAttrs, attrs, numAttrs);
return (CirType *)out;
}
const CirType *
CirType_fun(const CirType *bt, const CirFunParam *params, size_t numParams, bool isVa)
{
return CirType__fun(bt, params, numParams, isVa, NULL, 0);
}
const CirType *
CirType__valist(const CirAttr * const *attrs, size_t numAttrs)
{
if (!numAttrs)
return &valistType;
if (numAttrs > MAX_ATTRS)
cir_bug("too many attrs");
CirType *out = CirMem_balloc(sizeof(*out) + sizeof(*attrs) * numAttrs, alignof(*out));
out->data1 = typeToData1(CIR_TVALIST) | numAttrsToData1(numAttrs);
copyAttrs(out->attrs, attrs, numAttrs);
return out;
}
const CirType *
CirType_valist(void)
{
return CirType__valist(NULL, 0);
}
// Returns array decayed into pointer
const CirType *
CirType__arrayToPtr(const CirType *tt)
{
if (!CirType_isArray(tt))
return tt;
return CirType__ptr(CirType_getBaseType(tt), CirType_getAttrs(tt), CirType_getNumAttrs(tt));
}
const CirType *
CirType_unroll(const CirType *t)
{
for (;;) {
if (CirType_isNamed(t)) {
CirTypedefId tid = CirType_getTypedefId(t);
const CirType *bt = CirTypedef_getType(tid);
const CirAttr *const *attrs = CirType_getAttrs(t);
size_t numAttrs = CirType_getNumAttrs(t);
t = CirType_withAttrs(bt, attrs, numAttrs);
} else {
return t;
}
}
}
const CirType *
CirType_unrollDeep(const CirType *t)
{
const CirAttr *const *attrs = CirType_getAttrs(t);
size_t numAttrs = CirType_getNumAttrs(t);
switch (data1ToType(t->data1)) {
case CIR_TVOID:
case CIR_TINT:
case CIR_TFLOAT:
case CIR_TVALIST:
case CIR_TCOMP:
case CIR_TENUM:
// Leaf
return t;
case CIR_TNAMED: {
CirTypedefId tid = CirType_getTypedefId(t);
const CirType *bt = CirTypedef_getType(tid);
t = CirType_withAttrs(bt, attrs, numAttrs);
return CirType_unrollDeep(t);
}
case CIR_TPTR: {
const CirTypePtr *ptrType = (const CirTypePtr *)t;
const CirType *bt = ptrType->baseType;
bt = CirType_unrollDeep(bt);
return CirType__ptr(bt, attrs, numAttrs);
}
case CIR_TARRAY: {
const CirTypeArray *arrayType = (const CirTypeArray *)t;
const CirType *bt = CirType_unrollDeep(arrayType->baseType);
if (data1ToU2(t->data1))
return CirType__arrayWithLen(bt, arrayType->arrayLen, attrs, numAttrs);
else
return CirType__array(bt, attrs, numAttrs);
}
case CIR_TFUN: {
const CirTypeFun *funType = (const CirTypeFun *)t;
const CirType *bt = CirType_unrollDeep(funType->baseType);
size_t numParams = CirType_getNumParams(t);
const CirFunParam *params = CirType_getParams(t);
return CirType__fun(bt, params, numParams, data1ToU2(t->data1), attrs, numAttrs);
}
default:
cir_bug("CirType_unrollDeep: unhandled type case");
}
}
const CirType *
CirType_removeQual(const CirType *type)
{
// TODO: optimize
const CirAttr *attrs[] = {
CirAttr_name(CirName_of("const")),
CirAttr_name(CirName_of("restrict")),
CirAttr_name(CirName_of("volatile"))
};
return CirType_removeAttrs(type, attrs, 3);
}
const CirType *
CirType_lvalConv(const CirType *type)
{
const CirType *unrolledType = CirType_unroll(type);
if (CirType_isFun(unrolledType)) {
return CirType_ptr(type);
} else if (CirType_isArray(unrolledType)) {
return CirType__ptr(CirType_getBaseType(unrolledType), CirType_getAttrs(unrolledType), CirType_getNumAttrs(unrolledType));
} else {
const CirType *unqualType = CirType_removeQual(unrolledType);
return unqualType == unrolledType ? type : unqualType;
}
}
size_t
CirType_getNumAttrs(const CirType *t)
{
return data1ToNumAttrs(t->data1);
}
const CirAttr * const *
CirType_getAttrs(const CirType *t)
{
switch (data1ToType(t->data1)) {
case CIR_TVOID:
case CIR_TINT:
case CIR_TFLOAT:
case CIR_TVALIST:
return t->attrs;
case CIR_TPTR:
return ((const CirTypePtr *)t)->attrs;
case CIR_TARRAY:
return ((const CirTypeArray *)t)->attrs;
case CIR_TFUN: {
size_t numParams = CirType_getNumParams(t);
const CirFunParam *lastParam = ((const CirTypeFun *)t)->funParams + numParams;
return (const CirAttr * const *)lastParam;
}
case CIR_TNAMED:
return ((const CirTypeNamed *)t)->attrs;
case CIR_TCOMP:
return ((const CirTypeComp *)t)->attrs;
case CIR_TENUM:
return ((const CirTypeEnum *)t)->attrs;
default:
cir_bug("unknown type???");
}
}
const CirType *
CirType_withAttrs(const CirType *t, const CirAttr * const *attrs, size_t numAttrs)
{
if (!numAttrs)
return t; // No-op
CirAttrArray arr = CIRARRAY_INIT;
CirAttrArray__merge(&arr, attrs, numAttrs, CirType_getAttrs(t), CirType_getNumAttrs(t));
if (arr.len > MAX_ATTRS)
cir_bug("too many attrs after merging");
const CirType *ret = CirType_replaceAttrs(t, arr.items, arr.len);
CirArray_release(&arr);
return ret;
}
const CirType *
CirType_replaceAttrs(const CirType *t, const CirAttr *const *attrs, size_t numAttrs)
{
// Simple no-op optimization
size_t originalNumAttrs = CirType_getNumAttrs(t);
if (!originalNumAttrs && !numAttrs)
return t;
const CirType *bt;
const CirTypeNamed *tmpNamed;
const CirTypeComp *tmpComp;
const CirTypeArray *tmpArray;
const CirTypeEnum *tmpEnum;
switch (data1ToType(t->data1)) {
case CIR_TVOID:
return CirType__void(attrs, numAttrs);
case CIR_TINT:
return CirType__int(data1ToU1(t->data1), attrs, numAttrs);
case CIR_TFLOAT:
return CirType__float(data1ToU1(t->data1), attrs, numAttrs);
case CIR_TNAMED:
tmpNamed = (const CirTypeNamed *)t;
return CirType__typedef(tmpNamed->typedefId, attrs, numAttrs);
case CIR_TCOMP:
tmpComp = (const CirTypeComp *)t;
return CirType__comp(tmpComp->compId, attrs, numAttrs);
case CIR_TENUM:
tmpEnum = (const CirTypeEnum *)t;
return CirType__enum(tmpEnum->enumId, attrs, numAttrs);
case CIR_TPTR:
bt = CirType_getBaseType(t);
return CirType__ptr(bt, attrs, numAttrs);
case CIR_TARRAY:
tmpArray = (const CirTypeArray *)t;
bt = tmpArray->baseType;
if (data1ToU2(t->data1))
return CirType__arrayWithLen(bt, tmpArray->arrayLen, attrs, numAttrs);
else
return CirType__array(bt, attrs, numAttrs);
case CIR_TFUN:
cir_bug("TODO: CIR_TFUN");
case CIR_TVALIST:
return CirType__valist(attrs, numAttrs);
default:
cir_bug("unknown type???");
}
}
const CirType *
CirType_removeAttrs(const CirType *t, const CirAttr *const *attrs, size_t numAttrs)
{
if (!numAttrs)
return t; // No-op
size_t initialNumAttrs = CirType_getNumAttrs(t);
CirAttrArray arr = CIRARRAY_INIT;
CirAttrArray__remove(&arr, CirType_getAttrs(t), initialNumAttrs, attrs, numAttrs);
// Did any attrs actually get removed?
if (arr.len == initialNumAttrs) {
CirArray_release(&arr);
return t;
}
const CirType *ret = CirType_replaceAttrs(t, arr.items, arr.len);
CirArray_release(&arr);
return ret;
}
size_t
CirType_getNumParams(const CirType *tt)
{
if (!CirType_isFun(tt))
cir_bug("CirType_getNumParams: not a function type");
const CirTypeFun *t = (const CirTypeFun *)tt;
return data1ToU1(t->data1);
}
const CirFunParam *
CirType_getParams(const CirType *tt)
{
if (!CirType_isFun(tt))
cir_bug("CirType_getParams: not a function type");
return ((const CirTypeFun *)tt)->funParams;
}
bool
CirType_isParamsVa(const CirType *t)
{
if (!CirType_isFun(t))
cir_bug("CirType_isParamsVa: not a function type");
return data1ToU2(t->data1);
}
bool
CirType_hasArrayLen(const CirType *t)
{
if (!CirType_isArray(t))
cir_bug("CirType_hasArrayLen: not an array type");
return data1ToU2(t->data1);
}
uint32_t
CirType_getArrayLen(const CirType *t)
{
if (!CirType_isArray(t))
cir_bug("CirType_getArrayLen: not an array type");
if (!CirType_hasArrayLen(t))
cir_bug("CirType_getArrayLen: array type has no len");
return ((const CirTypeArray *)t)->arrayLen;
}
const CirType *
CirType__integralPromotion(const CirType *t, const CirMachine *mach)
{
const CirType *tu = CirType_unroll(t);
const CirAttr *const *attrs = CirType_getAttrs(tu);
size_t numAttrs = CirType_getNumAttrs(tu);
uint32_t ikind = CirType_isInt(tu);
if (ikind == CIR_IBOOL) {
// _Bool can only be 0 or 1, irrespective of its size
return CirType__int(CIR_IINT, attrs, numAttrs);
} else if (ikind == CIR_ISHORT || ikind == CIR_IUSHORT || ikind == CIR_ICHAR || ikind == CIR_ISCHAR || ikind == CIR_IUCHAR) {
if (CirIkind_size(ikind, mach) < CirIkind_size(CIR_IINT, mach) || CirIkind_isSigned(ikind, mach)) {
return CirType__int(CIR_IINT, attrs, numAttrs);
} else {
return CirType__int(CIR_IUINT, attrs, numAttrs);
}
} else if (ikind) {
return t;
} else {
cir_bug("CirType__integralPromotion: not expecting this type");
}
}
static unsigned
intRank(uint32_t ikind)
{
switch (ikind) {
case CIR_IBOOL:
return 0;
case CIR_ICHAR:
case CIR_ISCHAR:
case CIR_IUCHAR:
return 1;
case CIR_ISHORT:
case CIR_IUSHORT:
return 2;
case CIR_IINT:
case CIR_IUINT:
return 3;
case CIR_ILONG:
case CIR_IULONG:
return 4;
case CIR_ILONGLONG:
case CIR_IULONGLONG:
return 5;
default:
cir_bug("invalid ikind");
}
}
const CirType *
CirType__arithmeticConversion(const CirType *t1, const CirType *t2, const CirMachine *mach)
{
const CirType *t1u = CirType_unroll(t1);
const CirType *t2u = CirType_unroll(t2);
uint32_t t1_fkind = CirType_isFloat(t1u);
uint32_t t2_fkind = CirType_isFloat(t2u);
if (t1_fkind == CIR_FLONGDOUBLE) {
return t1;
} else if (t2_fkind == CIR_FLONGDOUBLE) {
return t2;
} else if (t1_fkind == CIR_FDOUBLE) {
return t1;
} else if (t2_fkind == CIR_FDOUBLE) {
return t2;
} else if (t1_fkind == CIR_FFLOAT) {
return t1;
} else if (t2_fkind == CIR_FFLOAT) {
return t2;
}
t1 = CirType__integralPromotion(t1, mach);
t2 = CirType__integralPromotion(t2, mach);
t1u = CirType_unroll(t1);
t2u = CirType_unroll(t2);
uint32_t t1_ikind = CirType_isInt(t1u);
uint32_t t2_ikind = CirType_isInt(t2u);
// CirType__integralPromotion would have ensured they are ints
assert(t1_ikind);
assert(t2_ikind);
// If both operands have the same type, then no further conversion is needed
if (t1_ikind == t2_ikind) {
return t1;
}
// Otherwise, if both operands have signed integer types
// or both have unsigned integer types,
// the operand with the type of lesser integer conversion rank is converted
// to the type of the operand with greater rank.
if (CirIkind_isSigned(t1_ikind, mach) == CirIkind_isSigned(t2_ikind, mach)) {
assert(intRank(t1_ikind) != intRank(t2_ikind));
return intRank(t1_ikind) < intRank(t2_ikind) ? t2 : t1;
}
// We need to know which one is signed for the next cases.
uint32_t signed_ikind, unsigned_ikind;
const CirType *signedt, *unsignedt;
if (CirIkind_isSigned(t1_ikind, mach)) {
signed_ikind = t1_ikind;
unsigned_ikind = t2_ikind;
signedt = t1;
unsignedt = t2;
} else {
signed_ikind = t2_ikind;
unsigned_ikind = t1_ikind;
signedt = t2;
unsignedt = t1;
}
// Otherwise, if the operand that has unsigned integer type has
// rank greater of equal to the rank of the type of the other operand,
// then the operand with signed integer type is converted to the type
// of the operand with unsigned integer type.
if (intRank(unsigned_ikind) >= intRank(signed_ikind)) {
return unsignedt;
}
// Otherwise, if the type of the operand with signed integer type
// can represent all of the values of the type of the operand
// with unsigned integer type, then the operand with unsigned integer
// type is converted to the type of the operand with signed integer type.
if (CirIkind_size(signed_ikind, mach) > CirIkind_size(unsigned_ikind, mach)) {
return signedt;
}
// Otherwise, both operands are converted to the unsigned integer type
// corresponding to the type of the operand with signed integer type.
return CirType_int(CirIkind_toUnsigned(signed_ikind));
}
// Rounds up `nrbits` to the nearest multiple of `roundto`.
// `roundto` must be a power of two.
static uint64_t
addTrailing(uint64_t nrbits, uint64_t roundto) {
return (nrbits + roundto - 1) & (~(roundto - 1));
}
// Return alignment in bytes
uint64_t
CirType_alignof(const CirType *t, const CirMachine *mach)
{
assert(t);
assert(mach);
uint32_t ikind = CirType_isInt(t);
uint32_t fkind = CirType_isFloat(t);
if (ikind == CIR_ICHAR || ikind == CIR_ISCHAR || ikind == CIR_IUCHAR) {
return 1;
} else if (ikind == CIR_IBOOL) {
return mach->alignofBool;
} else if (ikind == CIR_ISHORT || ikind == CIR_IUSHORT) {
return mach->alignofShort;
} else if (ikind == CIR_IINT || ikind == CIR_IUINT) {
return mach->alignofInt;
} else if (ikind == CIR_ILONG || ikind == CIR_IULONG) {
return mach->alignofLong;
} else if (ikind == CIR_ILONGLONG || ikind == CIR_IULONGLONG) {
return mach->alignofLongLong;
} else if (fkind == CIR_FFLOAT) {
return mach->alignofFloat;
} else if (fkind == CIR_FDOUBLE) {
return mach->alignofDouble;
} else if (fkind == CIR_FLONGDOUBLE) {
return mach->alignofLongDouble;
} else if (CirType_isNamed(t)) {
CirTypedefId tid = CirType_getTypedefId(t);
return CirType_alignof(CirTypedef_getType(tid), mach);
} else if (CirType_isArray(t)) {
return CirType_alignof(CirType_getBaseType(t), mach);
} else if (CirType_isPtr(t) || CirType_isVaList(t)) {
return mach->alignofPtr;
} else if (CirType_isComp(t)) {
CirCompId cid = CirType_getCompId(t);
return CirComp_getAlign(cid, mach);
} else if (CirType_isEnum(t)) {
CirEnumId enumId = CirType_getEnumId(t);
uint32_t ikind = CirEnum_getIkind(enumId);
return CirType_alignof(CirType_int(ikind), mach);
} else if (CirType_isFun(t)) {
if (mach->compiler == CIR_GCC) {
return mach->alignofFun;
} else {
cir_fatal("alignof called on function");
}
} else if (CirType_isVoid(t)) {
cir_fatal("alignof called on void");
} else {
cir_bug("CirType_alignof: unhandled case");
}
}
uint64_t
CirType_sizeof(const CirType *t, const CirMachine *mach)
{
assert(t);
assert(mach);
uint32_t ikind = CirType_isInt(t);