Skip to content

Commit b138b19

Browse files
committed
cmd/compile: use symbolic offsets of fields in internal/abi.ITab
After this CL, we can reorder or pad internal/abi.ITab fields at will (keeping Fun last, and updating ITabTypeOff correctly) without breaking anything. Change-Id: Ib7bb5828519813e0d1aa36be5092f96fcd62b3be Reviewed-on: https://go-review.googlesource.com/c/go/+/549516 Reviewed-by: Keith Randall <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: David Chase <[email protected]>
1 parent f2db96c commit b138b19

File tree

4 files changed

+12
-13
lines changed

4 files changed

+12
-13
lines changed

src/cmd/compile/internal/ssagen/ssa.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"cmd/compile/internal/liveness"
2323
"cmd/compile/internal/objw"
2424
"cmd/compile/internal/reflectdata"
25+
"cmd/compile/internal/rttype"
2526
"cmd/compile/internal/ssa"
2627
"cmd/compile/internal/staticdata"
2728
"cmd/compile/internal/typecheck"
@@ -5537,7 +5538,7 @@ func (s *state) getClosureAndRcvr(fn *ir.SelectorExpr) (*ssa.Value, *ssa.Value)
55375538
i := s.expr(fn.X)
55385539
itab := s.newValue1(ssa.OpITab, types.Types[types.TUINTPTR], i)
55395540
s.nilCheck(itab)
5540-
itabidx := fn.Offset() + 2*int64(types.PtrSize) + 8 // offset of fun field in runtime.itab
5541+
itabidx := fn.Offset() + rttype.ITab.OffsetOf("Fun")
55415542
closure := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.UintptrPtr, itabidx, itab)
55425543
rcvr := s.newValue1(ssa.OpIData, s.f.Config.Types.BytePtr, i)
55435544
return closure, rcvr
@@ -6522,7 +6523,7 @@ func (s *state) dynamicDottype(n *ir.DynamicTypeAssertExpr, commaok bool) (res,
65226523
targetItab = s.expr(n.ITab)
65236524
// TODO(mdempsky): Investigate whether compiling n.RType could be
65246525
// better than loading itab.typ.
6525-
target = s.load(byteptr, s.newValue1I(ssa.OpOffPtr, byteptr, int64(types.PtrSize), targetItab)) // itab.typ
6526+
target = s.load(byteptr, s.newValue1I(ssa.OpOffPtr, byteptr, rttype.ITab.OffsetOf("Type"), targetItab))
65266527
} else {
65276528
target = s.expr(n.RType)
65286529
}
@@ -6580,7 +6581,7 @@ func (s *state) dottype1(pos src.XPos, src, dst *types.Type, iface, source, targ
65806581
return
65816582
}
65826583
// Load type out of itab, build interface with existing idata.
6583-
off := s.newValue1I(ssa.OpOffPtr, byteptr, int64(types.PtrSize), itab)
6584+
off := s.newValue1I(ssa.OpOffPtr, byteptr, rttype.ITab.OffsetOf("Type"), itab)
65846585
typ := s.load(byteptr, off)
65856586
idata := s.newValue1(ssa.OpIData, byteptr, iface)
65866587
res = s.newValue2(ssa.OpIMake, dst, typ, idata)
@@ -6590,7 +6591,7 @@ func (s *state) dottype1(pos src.XPos, src, dst *types.Type, iface, source, targ
65906591
s.startBlock(bOk)
65916592
// nonempty -> empty
65926593
// Need to load type from itab
6593-
off := s.newValue1I(ssa.OpOffPtr, byteptr, int64(types.PtrSize), itab)
6594+
off := s.newValue1I(ssa.OpOffPtr, byteptr, rttype.ITab.OffsetOf("Type"), itab)
65946595
s.vars[typVar] = s.load(byteptr, off)
65956596
s.endBlock()
65966597

@@ -6644,7 +6645,7 @@ func (s *state) dottype1(pos src.XPos, src, dst *types.Type, iface, source, targ
66446645
s.startBlock(bNonNil)
66456646
typ := itab
66466647
if !src.IsEmptyInterface() {
6647-
typ = s.load(byteptr, s.newValue1I(ssa.OpOffPtr, byteptr, int64(types.PtrSize), itab))
6648+
typ = s.load(byteptr, s.newValue1I(ssa.OpOffPtr, byteptr, rttype.ITab.OffsetOf("Type"), itab))
66486649
}
66496650

66506651
// Check the cache first.
@@ -6685,9 +6686,9 @@ func (s *state) dottype1(pos src.XPos, src, dst *types.Type, iface, source, targ
66856686
// Load hash from type or itab.
66866687
var hash *ssa.Value
66876688
if src.IsEmptyInterface() {
6688-
hash = s.newValue2(ssa.OpLoad, typs.UInt32, s.newValue1I(ssa.OpOffPtr, typs.UInt32Ptr, 2*s.config.PtrSize, typ), s.mem())
6689+
hash = s.newValue2(ssa.OpLoad, typs.UInt32, s.newValue1I(ssa.OpOffPtr, typs.UInt32Ptr, rttype.Type.OffsetOf("Hash"), typ), s.mem())
66896690
} else {
6690-
hash = s.newValue2(ssa.OpLoad, typs.UInt32, s.newValue1I(ssa.OpOffPtr, typs.UInt32Ptr, 2*s.config.PtrSize, itab), s.mem())
6691+
hash = s.newValue2(ssa.OpLoad, typs.UInt32, s.newValue1I(ssa.OpOffPtr, typs.UInt32Ptr, rttype.ITab.OffsetOf("Hash"), itab), s.mem())
66916692
}
66926693
hash = s.newValue1(zext, typs.Uintptr, hash)
66936694
s.vars[hashVar] = hash

src/cmd/compile/internal/walk/switch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ func typeHashFieldOf(pos src.XPos, itab *ir.UnaryExpr) *ir.SelectorExpr {
700700
} else {
701701
// runtime.itab's hash field
702702
if itabHashField == nil {
703-
itabHashField = runtimeField("hash", int64(2*types.PtrSize), types.Types[types.TUINT32])
703+
itabHashField = runtimeField("hash", rttype.ITab.OffsetOf("Hash"), types.Types[types.TUINT32])
704704
}
705705
hashField = itabHashField
706706
}

src/cmd/compile/internal/walk/walk.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"cmd/compile/internal/base"
1111
"cmd/compile/internal/ir"
1212
"cmd/compile/internal/reflectdata"
13+
"cmd/compile/internal/rttype"
1314
"cmd/compile/internal/ssagen"
1415
"cmd/compile/internal/typecheck"
1516
"cmd/compile/internal/types"
@@ -345,8 +346,8 @@ func mayCall(n ir.Node) bool {
345346
// itabType loads the _type field from a runtime.itab struct.
346347
func itabType(itab ir.Node) ir.Node {
347348
if itabTypeField == nil {
348-
// runtime.itab's _type field
349-
itabTypeField = runtimeField("_type", int64(types.PtrSize), types.NewPtr(types.Types[types.TUINT8]))
349+
// internal/abi.ITab's Type field
350+
itabTypeField = runtimeField("Type", rttype.ITab.OffsetOf("Type"), types.NewPtr(types.Types[types.TUINT8]))
350351
}
351352
return boundedDotPtr(base.Pos, itab, itabTypeField)
352353
}

src/internal/abi/iface.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ package abi
88
// It records the underlying concrete type (Type), the interface type it
99
// is implementing (Inter), and some ancillary information.
1010
//
11-
// layout of ITab known to compilers
1211
// allocated in non-garbage-collected memory
13-
// Needs to be in sync with
14-
// cmd/compile/internal/reflectdata/reflect.go:/^func.WritePluginTable.
1512
type ITab struct {
1613
Inter *InterfaceType
1714
Type *Type

0 commit comments

Comments
 (0)