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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
|
// Copyright 2019 The CC Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cc // import "modernc.org/cc/v3"
import (
"encoding/binary"
"fmt"
"math"
"os"
"runtime"
"lukechampine.com/uint128"
"modernc.org/mathutil"
)
var (
idAligned = String("aligned")
idGCCStruct = String("gcc_struct")
idMSStruct = String("ms_struct")
idPacked = String("packed")
complexTypedefs = map[StringID]Kind{
dict.sid("__COMPLEX_CHAR_TYPE__"): ComplexChar,
dict.sid("__COMPLEX_DOUBLE_TYPE__"): ComplexDouble,
dict.sid("__COMPLEX_FLOAT_TYPE__"): ComplexFloat,
dict.sid("__COMPLEX_INT_TYPE__"): ComplexInt,
dict.sid("__COMPLEX_LONG_TYPE__"): ComplexLong,
dict.sid("__COMPLEX_LONG_DOUBLE_TYPE__"): ComplexLongDouble,
dict.sid("__COMPLEX_LONG_LONG_TYPE__"): ComplexLongLong,
dict.sid("__COMPLEX_SHORT_TYPE__"): ComplexShort,
dict.sid("__COMPLEX_UNSIGNED_TYPE__"): ComplexUInt,
dict.sid("__COMPLEX_LONG_UNSIGNED_TYPE__"): ComplexULong,
dict.sid("__COMPLEX_LONG_LONG_UNSIGNED_TYPE__"): ComplexULongLong,
dict.sid("__COMPLEX_SHORT_UNSIGNED_TYPE__"): ComplexUShort,
}
)
// NewABI creates an ABI for a given OS and architecture. The OS and architecture values are the same as used in Go.
// The ABI type map may miss advanced types like complex numbers, etc. If the os/arch pair is not recognized, a
// *ErrUnsupportedOSArch is returned.
func NewABI(os, arch string) (ABI, error) {
order, ok := abiByteOrders[arch]
if !ok {
return ABI{}, fmt.Errorf("unsupported arch: %s", arch)
}
types, ok := abiTypes[[2]string{os, arch}]
if !ok {
return ABI{}, fmt.Errorf("unsupported os/arch pair: %s-%s", os, arch)
}
abi := ABI{
ByteOrder: order,
Types: make(map[Kind]ABIType, len(types)),
SignedChar: abiSignedChar[[2]string{os, arch}],
os: os,
arch: arch,
}
// copy the map, so it can be modified by user
for k, v := range types {
abi.Types[k] = v
}
return abi, nil
}
// NewABIFromEnv uses GOOS and GOARCH values to create a corresponding ABI.
// If those environment variables are not set, an OS/arch of a Go runtime is used.
// It returns a *ErrUnsupportedOSArch if OS/arch pair is not supported.
func NewABIFromEnv() (ABI, error) {
osv := os.Getenv("GOOS")
if osv == "" {
osv = runtime.GOOS
}
arch := os.Getenv("GOARCH")
if arch == "" {
arch = runtime.GOARCH
}
return NewABI(osv, arch)
}
// ABIType describes properties of a non-aggregate type.
type ABIType struct {
Size uintptr
Align int
FieldAlign int
}
// ABI describes selected parts of the Application Binary Interface.
type ABI struct {
ByteOrder binary.ByteOrder
Types map[Kind]ABIType
arch string
os string
types map[Kind]Type
SignedChar bool
}
func (a *ABI) sanityCheck(ctx *context, intMaxWidth int, s Scope) error {
if intMaxWidth == 0 {
intMaxWidth = 64
}
a.types = map[Kind]Type{}
for _, k := range []Kind{
Bool,
Char,
Double,
Enum,
Float,
Int,
Long,
LongDouble,
LongLong,
Ptr,
SChar,
Short,
UChar,
UInt,
ULong,
ULongLong,
UShort,
Void,
} {
v, ok := a.Types[k]
if !ok {
if ctx.err(noPos, "ABI is missing %s", k) {
return ctx.Err()
}
continue
}
if (k != Void && v.Size == 0) || v.Align == 0 || v.FieldAlign == 0 ||
v.Align > math.MaxUint8 || v.FieldAlign > math.MaxUint8 {
if ctx.err(noPos, "invalid ABI type %s: %+v", k, v) {
return ctx.Err()
}
}
if integerTypes[k] && v.Size > 8 {
if ctx.err(noPos, "invalid ABI type %s size: %v, must be <= 8", k, v.Size) {
return ctx.Err()
}
}
var f flag
if integerTypes[k] && a.isSignedInteger(k) {
f = fSigned
}
t := &typeBase{
align: byte(a.align(k)),
fieldAlign: byte(a.fieldAlign(k)),
flags: f,
kind: byte(k),
size: uintptr(a.size(k)),
}
a.types[k] = t
}
if _, ok := a.Types[Int128]; ok {
t := &typeBase{
align: byte(a.align(Int128)),
fieldAlign: byte(a.fieldAlign(Int128)),
flags: fSigned,
kind: byte(Int128),
size: uintptr(a.size(Int128)),
}
a.types[Int128] = t
}
if _, ok := a.Types[UInt128]; ok {
t := &typeBase{
align: byte(a.align(UInt128)),
fieldAlign: byte(a.fieldAlign(UInt128)),
kind: byte(UInt128),
size: uintptr(a.size(UInt128)),
}
a.types[UInt128] = t
}
return ctx.Err()
}
func (a *ABI) Type(k Kind) Type { return a.types[k] }
func (a *ABI) align(k Kind) int { return a.Types[k].Align }
func (a *ABI) fieldAlign(k Kind) int { return a.Types[k].FieldAlign }
func (a *ABI) size(k Kind) int { return int(a.Types[k].Size) }
func (a *ABI) isSignedInteger(k Kind) bool {
if !integerTypes[k] {
internalError()
}
switch k {
case Bool, UChar, UInt, ULong, ULongLong, UShort:
return false
case Char:
return a.SignedChar
default:
return true
}
}
func roundup(n, to int64) int64 {
if r := n % to; r != 0 {
return n + to - r
}
return n
}
func roundup128(n uint128.Uint128, to uint64) uint128.Uint128 {
if r := n.Mod(uint128.From64(to)); !r.IsZero() {
return n.Add64(to).Sub(r)
}
return n
}
func rounddown(n, to int64) int64 {
return n &^ (to - 1)
}
func rounddown128(n uint128.Uint128, to uint64) uint128.Uint128 {
return n.And(uint128.Uint128{Hi: ^uint64(0), Lo: ^(to - 1)})
}
func normalizeBitFieldWidth(n byte) byte {
switch {
case n <= 8:
return 8
case n <= 16:
return 16
case n <= 32:
return 32
case n <= 64:
return 64
default:
panic(todo("internal error: %v", n))
}
}
func (a *ABI) layout(ctx *context, n Node, t *structType) *structType {
if t == nil {
return nil
}
if t.typeBase.align < 1 {
t.typeBase.align = 1
}
for _, v := range t.attr {
if _, ok := v.Has(idGCCStruct); ok {
return a.gccLayout(ctx, n, t)
}
//TODO if _, ok := v.Has(idMSStruct); ok {
//TODO return a.msLayout(ctx, n, t)
//TODO }
}
switch {
case ctx.cfg.Config3.GCCStructs:
return a.gccLayout(ctx, n, t)
//TODO case ctx.cfg.Config3.MSStructs:
//TODO return a.msLayout(ctx, n, t)
}
var hasBitfields bool
defer func() {
if !hasBitfields {
return
}
m := make(map[uintptr][]*field, len(t.fields))
for _, f := range t.fields {
off := f.offset
m[off] = append(m[off], f)
}
for _, s := range m {
var first *field
var w byte
for _, f := range s {
if first == nil {
first = f
}
if f.isBitField {
n := f.bitFieldOffset + f.bitFieldWidth
if n > w {
w = n
}
}
}
w = normalizeBitFieldWidth(w)
for _, f := range s {
if f.isBitField {
f.blockStart = first
f.blockWidth = w
}
if a.ByteOrder == binary.BigEndian {
f.bitFieldOffset = w - f.bitFieldWidth - f.bitFieldOffset
f.bitFieldMask = (uint64(1)<<f.bitFieldWidth - 1) << f.bitFieldOffset
}
}
}
}()
var off int64 // bit offset
align := int(t.typeBase.align)
switch {
case t.Kind() == Union:
for _, f := range t.fields {
ft := f.Type()
sz := ft.Size()
if n := int64(8 * sz); n > off {
off = n
}
al := ft.FieldAlign()
if al == 0 {
al = 1
}
if al > align {
align = al
}
if f.isBitField {
hasBitfields = true
f.bitFieldMask = 1<<f.bitFieldWidth - 1
}
f.promote = integerPromotion(a, ft)
}
t.align = byte(align)
t.fieldAlign = byte(align)
off = roundup(off, 8*int64(align))
t.size = uintptr(off >> 3)
ctx.structs[StructInfo{Size: t.size, Align: t.Align()}] = struct{}{}
default:
var i int
var group byte
var f, lf *field
for i, f = range t.fields {
ft := f.Type()
var sz uintptr
switch {
case ft.Kind() == Array && i == len(t.fields)-1:
if ft.IsIncomplete() || ft.Len() == 0 {
t.hasFlexibleMember = true
f.isFlexible = true
break
}
fallthrough
default:
sz = ft.Size()
}
bitSize := 8 * int(sz)
al := ft.FieldAlign()
if al == 0 {
al = 1
}
if al > align {
align = al
}
switch {
case f.isBitField:
hasBitfields = true
eal := 8 * al
if eal < bitSize {
eal = bitSize
}
down := off &^ (int64(eal) - 1)
bitoff := off - down
downMax := off &^ (int64(bitSize) - 1)
skip := lf != nil && lf.isBitField && lf.bitFieldWidth == 0 ||
lf != nil && lf.bitFieldWidth == 0 && ctx.cfg.NoFieldAndBitfieldOverlap
switch {
case skip || int(off-downMax)+int(f.bitFieldWidth) > bitSize:
group = 0
off = roundup(off, 8*int64(al))
f.offset = uintptr(off >> 3)
f.bitFieldOffset = 0
f.bitFieldMask = 1<<f.bitFieldWidth - 1
off += int64(f.bitFieldWidth)
if f.bitFieldWidth == 0 {
lf = f
continue
}
default:
f.offset = uintptr(down >> 3)
f.bitFieldOffset = byte(bitoff)
f.bitFieldMask = (1<<f.bitFieldWidth - 1) << byte(bitoff)
off += int64(f.bitFieldWidth)
}
group += f.bitFieldWidth
default:
if n := group % 64; n != 0 {
if ctx.cfg.FixBitfieldPadding {
off += int64(normalizeBitFieldWidth(group-n) - group)
} else {
group -= n
off += int64(normalizeBitFieldWidth(group) - group)
}
}
off0 := off
off = roundup(off, 8*int64(al))
f.pad = byte(off-off0) >> 3
f.offset = uintptr(off) >> 3
off += 8 * int64(sz)
group = 0
}
f.promote = integerPromotion(a, ft)
lf = f
}
t.align = byte(align)
t.fieldAlign = byte(align)
off0 := off
off = roundup(off, 8*int64(align))
if f != nil && !f.IsBitField() {
f.pad = byte(off-off0) >> 3
}
t.size = uintptr(off >> 3)
ctx.structs[StructInfo{Size: t.size, Align: t.Align()}] = struct{}{}
}
return t
}
func (a *ABI) Ptr(n Node, t Type) Type {
base := t.base()
base.align = byte(a.align(Ptr))
base.fieldAlign = byte(a.fieldAlign(Ptr))
base.kind = byte(Ptr)
base.size = uintptr(a.size(Ptr))
base.flags &^= fIncomplete
return &pointerType{
elem: t,
typeBase: base,
}
}
func (a *ABI) gccLayout(ctx *context, n Node, t *structType) (r *structType) {
if t.IsPacked() {
return a.gccPackedLayout(ctx, n, t)
}
if t.Kind() == Union {
var off uint128.Uint128 // In bits.
align := int(t.typeBase.align)
for _, f := range t.fields {
switch {
case f.isBitField:
f.offset = 0
f.bitFieldOffset = 0
f.bitFieldMask = 1<<f.bitFieldWidth - 1
if uint64(f.bitFieldWidth) > off.Lo {
off.Lo = uint64(f.bitFieldWidth)
}
default:
al := f.Type().Align()
if al > align {
align = al
}
f.offset = 0
off2 := uint128.From64(uint64(f.Type().Size())).Mul64(8)
if off2.Cmp(off) > 0 {
off = off2
}
}
f.promote = integerPromotion(a, f.Type())
}
t.align = byte(align)
t.fieldAlign = byte(align)
off = roundup128(off, 8*uint64(align))
t.size = uintptr(off.Rsh(3).Lo)
ctx.structs[StructInfo{Size: t.size, Align: t.Align()}] = struct{}{}
return t
}
var off uint128.Uint128 // In bits.
align := int(t.typeBase.align)
for i, f := range t.fields {
switch {
case f.isBitField:
al := f.Type().Align()
// http://jkz.wtf/bit-field-packing-in-gcc-and-clang
// 1. Jump backwards to nearest address that would support this type. For
// example if we have an int jump to the closest address where an int could be
// stored according to the platform alignment rules.
down := rounddown128(off, 8*uint64(al))
// 2. Get sizeof(current field) bytes from that address.
alloc := int64(f.Type().Size()) * 8
need := int64(f.bitFieldWidth)
if need == 0 && i != 0 {
off = roundup128(off, 8*uint64(al))
continue
}
if al > align {
align = al
}
used := int64(off.Sub(down).Lo)
switch {
case alloc-used >= need:
// 3. If the number of bits that we need to store can be stored in these bits,
// put the bits in the lowest possible bits of this block.
off = down.Add64(uint64(used))
f.offset = uintptr(down.Rsh(3).Lo)
f.bitFieldOffset = byte(used)
f.bitFieldMask = (1<<f.bitFieldWidth - 1) << used
off = off.Add64(uint64(f.bitFieldWidth))
f.promote = integerPromotion(a, f.Type())
default:
// 4. Otherwise, pad the rest of this block with zeros, and store the bits that
// make up this bit-field in the lowest bits of the next block.
off = roundup128(off, 8*uint64(al))
f.offset = uintptr(off.Rsh(3).Lo)
f.bitFieldOffset = 0
f.bitFieldMask = 1<<f.bitFieldWidth - 1
off = off.Add64(uint64(f.bitFieldWidth))
f.promote = integerPromotion(a, f.Type())
}
default:
al := f.Type().Align()
if al > align {
align = al
}
off = roundup128(off, 8*uint64(al))
f.offset = uintptr(off.Rsh(3).Lo)
sz := uint128.From64(uint64(f.Type().Size()))
off = off.Add(sz.Mul64(8))
f.promote = integerPromotion(a, f.Type())
}
}
var lf *field
for _, f := range t.fields {
if lf != nil && !lf.isBitField && !f.isBitField {
lf.pad = byte(f.offset - lf.offset - lf.Type().Size())
}
lf = f
}
t.align = byte(align)
t.fieldAlign = byte(align)
off0 := off
off = roundup128(off, 8*uint64(align))
if lf != nil && !lf.IsBitField() {
lf.pad = byte(off.Sub(off0).Rsh(3).Lo)
}
t.size = uintptr(off.Rsh(3).Lo)
ctx.structs[StructInfo{Size: t.size, Align: t.Align()}] = struct{}{}
return t
}
func (a *ABI) gccPackedLayout(ctx *context, n Node, t *structType) (r *structType) {
switch a.arch {
case "arm", "arm64":
return a.gccPackedLayoutARM(ctx, n, t)
}
if t.typeBase.flags&fAligned == 0 {
t.align = 1
}
t.fieldAlign = t.align
if t.Kind() == Union {
var off int64 // In bits.
for _, f := range t.fields {
switch {
case f.isBitField:
panic(todo("%v: ", n.Position()))
default:
f.offset = 0
if off2 := 8 * int64(f.Type().Size()); off2 > off {
off = off2
}
f.promote = integerPromotion(a, f.Type())
}
}
off = roundup(off, 8)
t.size = uintptr(off >> 3)
ctx.structs[StructInfo{Size: t.size, Align: t.Align()}] = struct{}{}
return t
}
var off int64 // In bits.
for i, f := range t.fields {
switch {
case f.isBitField:
if f.bitFieldWidth == 0 {
if i != 0 {
off = roundup(off, 8*int64(f.Type().Align()))
}
continue
}
if b := f.Type().base(); b.flags&fAligned != 0 {
off = roundup(off, 8*int64(a.Types[f.Type().Kind()].Align))
}
f.offset = uintptr(off >> 3)
f.bitFieldOffset = byte(off & 7)
f.bitFieldMask = (1<<f.bitFieldWidth - 1) << f.bitFieldOffset
off += int64(f.bitFieldWidth)
f.promote = integerPromotion(a, f.Type())
default:
al := f.Type().Align()
off = roundup(off, 8*int64(al))
f.offset = uintptr(off) >> 3
off += 8 * int64(f.Type().Size())
f.promote = integerPromotion(a, f.Type())
}
}
var lf *field
for _, f := range t.fields {
if lf != nil && !lf.isBitField && !f.isBitField {
lf.pad = byte(f.offset - lf.offset - lf.Type().Size())
}
lf = f
}
off0 := off
off = roundup(off, 8*int64(t.Align()))
if lf != nil && !lf.IsBitField() {
lf.pad = byte(off-off0) >> 3
}
t.size = uintptr(off >> 3)
ctx.structs[StructInfo{Size: t.size, Align: t.Align()}] = struct{}{}
return t
}
func (a *ABI) gccPackedLayoutARM(ctx *context, n Node, t *structType) (r *structType) {
align := 1
if t.typeBase.flags&fAligned == 0 {
t.align = 1
}
t.fieldAlign = t.align
if t.Kind() == Union {
var off int64 // In bits.
for _, f := range t.fields {
switch {
case f.isBitField:
panic(todo("%v: ", n.Position()))
default:
f.offset = 0
if off2 := 8 * int64(f.Type().Size()); off2 > off {
off = off2
}
f.promote = integerPromotion(a, f.Type())
}
}
off = roundup(off, 8)
t.size = uintptr(off >> 3)
ctx.structs[StructInfo{Size: t.size, Align: t.Align()}] = struct{}{}
return t
}
var off int64 // In bits.
for i, f := range t.fields {
switch {
case f.isBitField:
if f.bitFieldWidth == 0 {
al := f.Type().Align()
if al > align {
align = al
}
if i != 0 {
off = roundup(off, 8*int64(f.Type().Align()))
}
continue
}
if b := f.Type().base(); b.flags&fAligned != 0 {
off = roundup(off, 8*int64(a.Types[f.Type().Kind()].Align))
}
f.offset = uintptr(off >> 3)
f.bitFieldOffset = byte(off & 7)
f.bitFieldMask = (1<<f.bitFieldWidth - 1) << f.bitFieldOffset
off += int64(f.bitFieldWidth)
f.promote = integerPromotion(a, f.Type())
default:
al := f.Type().Align()
off = roundup(off, 8*int64(al))
f.offset = uintptr(off) >> 3
off += 8 * int64(f.Type().Size())
f.promote = integerPromotion(a, f.Type())
}
}
var lf *field
for _, f := range t.fields {
if lf != nil && !lf.isBitField && !f.isBitField {
lf.pad = byte(f.offset - lf.offset - lf.Type().Size())
}
lf = f
}
if b := t.base(); b.flags&fAligned == 0 {
t.align = byte(align)
t.fieldAlign = byte(align)
}
off0 := off
off = roundup(off, 8*int64(t.Align()))
if lf != nil && !lf.IsBitField() {
lf.pad = byte(off-off0) >> 3
}
t.size = uintptr(off >> 3)
ctx.structs[StructInfo{Size: t.size, Align: t.Align()}] = struct{}{}
return t
}
// https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html#x86-Options
//
// -mno-ms-bitfields
//
// Enable/disable bit-field layout compatible with the native Microsoft Windows
// compiler.
//
// If packed is used on a structure, or if bit-fields are used, it may be that
// the Microsoft ABI lays out the structure differently than the way GCC
// normally does. Particularly when moving packed data between functions
// compiled with GCC and the native Microsoft compiler (either via function
// call or as data in a file), it may be necessary to access either format.
//
// This option is enabled by default for Microsoft Windows targets. This
// behavior can also be controlled locally by use of variable or type
// attributes. For more information, see x86 Variable Attributes and x86 Type
// Attributes.
//
// The Microsoft structure layout algorithm is fairly simple with the exception
// of the bit-field packing. The padding and alignment of members of structures
// and whether a bit-field can straddle a storage-unit boundary are determine
// by these rules:
//
// Structure members are stored sequentially in the order in which they are
// declared: the first member has the lowest memory address and the last member
// the highest. Every data object has an alignment requirement. The alignment
// requirement for all data except structures, unions, and arrays is either the
// size of the object or the current packing size (specified with either the
// aligned attribute or the pack pragma), whichever is less. For structures,
// unions, and arrays, the alignment requirement is the largest alignment
// requirement of its members. Every object is allocated an offset so that:
// offset % alignment_requirement == 0 Adjacent bit-fields are packed into the
// same 1-, 2-, or 4-byte allocation unit if the integral types are the same
// size and if the next bit-field fits into the current allocation unit without
// crossing the boundary imposed by the common alignment requirements of the
// bit-fields. MSVC interprets zero-length bit-fields in the following ways:
//
// If a zero-length bit-field is inserted between two bit-fields that are
// normally coalesced, the bit-fields are not coalesced. For example:
//
// struct
// {
// unsigned long bf_1 : 12;
// unsigned long : 0;
// unsigned long bf_2 : 12;
// } t1;
//
// The size of t1 is 8 bytes with the zero-length bit-field. If the zero-length
// bit-field were removed, t1’s size would be 4 bytes.
//
// If a zero-length bit-field is inserted after a bit-field, foo, and the
// alignment of the zero-length bit-field is greater than the member that
// follows it, bar, bar is aligned as the type of the zero-length bit-field.
// For example:
//
// struct
// {
// char foo : 4;
// short : 0;
// char bar;
// } t2;
//
// struct
// {
// char foo : 4;
// short : 0;
// double bar;
// } t3;
//
// For t2, bar is placed at offset 2, rather than offset 1. Accordingly, the
// size of t2 is 4. For t3, the zero-length bit-field does not affect the
// alignment of bar or, as a result, the size of the structure.
//
// Taking this into account, it is important to note the following:
//
// If a zero-length bit-field follows a normal bit-field, the type of the
// zero-length bit-field may affect the alignment of the structure as whole.
// For example, t2 has a size of 4 bytes, since the zero-length bit-field
// follows a normal bit-field, and is of type short. Even if a zero-length
// bit-field is not followed by a normal bit-field, it may still affect the
// alignment of the structure:
//
// struct
// {
// char foo : 6;
// long : 0;
// } t4;
//
// Here, t4 takes up 4 bytes.
//
// Zero-length bit-fields following non-bit-field members are ignored:
//
// struct
// {
// char foo;
// long : 0;
// char bar;
// } t5;
//
// Here, t5 takes up 2 bytes.
func (a *ABI) msLayout(ctx *context, n Node, t *structType) (r *structType) {
if t.IsPacked() {
return a.msPackedLayout(ctx, n, t)
}
if t.Kind() == Union {
panic(todo(""))
}
var off int64 // In bits.
align := int(t.typeBase.align)
var prev *field
for i, f := range t.fields {
switch {
case f.isBitField:
al := f.Type().Align()
if prev != nil {
switch {
case prev.isBitField && prev.Type().Size() != f.Type().Size():
off = roundup(off, 8*int64(prev.Type().Align()))
off = roundup(off, 8*int64(al))
case !prev.isBitField:
off = roundup(off, 8*int64(al))
default:
// Adjacent bit-fields are packed into the same 1-, 2-, or 4-byte allocation
// unit if the integral types are the same size and if the next bit-field fits
// into the current allocation unit without crossing the boundary imposed by
// the common alignment requirements of the bit-fields.
}
}
// http://jkz.wtf/bit-field-packing-in-gcc-and-clang
// 1. Jump backwards to nearest address that would support this type. For
// example if we have an int jump to the closest address where an int could be
// stored according to the platform alignment rules.
down := rounddown(off, 8*int64(al))
// 2. Get sizeof(current field) bytes from that address.
alloc := int64(f.Type().Size()) * 8
need := int64(f.bitFieldWidth)
if need == 0 && i != 0 {
off = roundup(off, 8*int64(al))
continue
}
if al > align {
align = al
}
used := off - down
switch {
case alloc-used >= need:
// 3. If the number of bits that we need to store can be stored in these bits,
// put the bits in the lowest possible bits of this block.
off = down + used
f.offset = uintptr(down >> 3)
f.bitFieldOffset = byte(used)
f.bitFieldMask = (1<<f.bitFieldWidth - 1) << used
off += int64(f.bitFieldWidth)
f.promote = integerPromotion(a, f.Type())
default:
// 4. Otherwise, pad the rest of this block with zeros, and store the bits that
// make up this bit-field in the lowest bits of the next block.
off = roundup(off, 8*int64(al))
f.offset = uintptr(off >> 3)
f.bitFieldOffset = 0
f.bitFieldMask = 1<<f.bitFieldWidth - 1
off += int64(f.bitFieldWidth)
f.promote = integerPromotion(a, f.Type())
}
default:
if prev != nil && prev.isBitField {
off = roundup(off, 8*int64(prev.Type().Align()))
}
al := f.Type().Align()
if al > align {
align = al
}
off = roundup(off, 8*int64(al))
f.offset = uintptr(off) >> 3
off += 8 * int64(f.Type().Size())
f.promote = integerPromotion(a, f.Type())
}
prev = f
}
var lf *field
for _, f := range t.fields {
if lf != nil && !lf.isBitField && !f.isBitField {
lf.pad = byte(f.offset - lf.offset - lf.Type().Size())
}
lf = f
}
t.align = byte(align)
t.fieldAlign = byte(align)
off0 := off
off = roundup(off, 8*int64(align))
if lf != nil && !lf.IsBitField() {
lf.pad = byte(off-off0) >> 3
}
t.size = uintptr(off >> 3)
ctx.structs[StructInfo{Size: t.size, Align: t.Align()}] = struct{}{}
return t
}
func (a *ABI) msPackedLayout(ctx *context, n Node, t *structType) (r *structType) {
if t.typeBase.flags&fAligned == 0 {
t.align = 1
}
t.fieldAlign = t.align
if t.Kind() == Union {
panic(todo(""))
var off int64 // In bits.
for _, f := range t.fields {
switch {
case f.isBitField:
panic(todo("%v: ", n.Position()))
default:
f.offset = 0
if off2 := 8 * int64(f.Type().Size()); off2 > off {
off = off2
}
f.promote = integerPromotion(a, f.Type())
}
}
off = roundup(off, 8)
t.size = uintptr(off >> 3)
ctx.structs[StructInfo{Size: t.size, Align: t.Align()}] = struct{}{}
return t
}
var off int64 // In bits.
var prev *field
align := int(t.typeBase.align)
for i, f := range t.fields {
out:
switch {
case f.isBitField:
al := f.Type().Align()
switch {
case prev != nil && prev.IsBitField() && prev.Type().Size() != f.Type().Size():
off = mathutil.MaxInt64(off, int64(prev.Offset()*8)+int64(prev.BitFieldOffset()+8*prev.Type().Align()))
off = roundup(off, 8*int64(align))
f.offset = uintptr(off >> 3)
f.bitFieldOffset = 0
f.bitFieldMask = 1<<f.bitFieldWidth - 1
off += int64(f.bitFieldWidth)
f.promote = integerPromotion(a, f.Type())
break out
}
// http://jkz.wtf/bit-field-packing-in-gcc-and-clang
// 1. Jump backwards to nearest address that would support this type. For
// example if we have an int jump to the closest address where an int could be
// stored according to the platform alignment rules.
down := rounddown(off, 8*int64(al))
// 2. Get sizeof(current field) bytes from that address.
alloc := int64(f.Type().Size()) * 8
need := int64(f.bitFieldWidth)
if need == 0 && i != 0 {
off = roundup(off, 8*int64(al))
continue
}
used := off - down
switch {
case alloc-used >= need:
// 3. If the number of bits that we need to store can be stored in these bits,
// put the bits in the lowest possible bits of this block.
off = down + used
f.offset = uintptr(down >> 3)
f.bitFieldOffset = byte(used)
f.bitFieldMask = (1<<f.bitFieldWidth - 1) << used
off += int64(f.bitFieldWidth)
f.promote = integerPromotion(a, f.Type())
default:
// 4. Otherwise, pad the rest of this block with zeros, and store the bits that
// make up this bit-field in the lowest bits of the next block.
off = roundup(off, 8*int64(al))
f.offset = uintptr(off >> 3)
f.bitFieldOffset = 0
f.bitFieldMask = 1<<f.bitFieldWidth - 1
off += int64(f.bitFieldWidth)
f.promote = integerPromotion(a, f.Type())
}
default:
off = roundup(off, 8)
f.offset = uintptr(off) >> 3
off += 8 * int64(f.Type().Size())
f.promote = integerPromotion(a, f.Type())
}
prev = f
}
var lf *field
for _, f := range t.fields {
if lf != nil && !lf.isBitField && !f.isBitField {
lf.pad = byte(f.offset - lf.offset - lf.Type().Size())
}
lf = f
}
t.align = byte(align)
t.fieldAlign = byte(align)
switch {
case lf != nil && lf.IsBitField():
off = mathutil.MaxInt64(off, int64(lf.Offset()*8)+int64(lf.BitFieldOffset()+8*lf.Type().Align()))
off = roundup(off, 8*int64(align))
default:
off0 := off
off = roundup(off, 8*int64(align))
if lf != nil && !lf.IsBitField() {
lf.pad = byte(off-off0) >> 3
}
}
t.size = uintptr(off >> 3)
ctx.structs[StructInfo{Size: t.size, Align: t.Align()}] = struct{}{}
return t
}
|