From: Keith Randall Date: Wed, 13 Dec 2023 21:20:01 +0000 (-0800) Subject: cmd/compile: use symbolic offsets of fields in internal/abi.ITab X-Git-Tag: go1.23rc1~1212 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=b138b19d2f7d99c462bf53b0dcc1ee98884630ab;p=gostls13.git 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 LUCI-TryBot-Result: Go LUCI Reviewed-by: David Chase --- diff --git a/src/cmd/compile/internal/ssagen/ssa.go b/src/cmd/compile/internal/ssagen/ssa.go index 6544c469bf..68b1547048 100644 --- a/src/cmd/compile/internal/ssagen/ssa.go +++ b/src/cmd/compile/internal/ssagen/ssa.go @@ -22,6 +22,7 @@ import ( "cmd/compile/internal/liveness" "cmd/compile/internal/objw" "cmd/compile/internal/reflectdata" + "cmd/compile/internal/rttype" "cmd/compile/internal/ssa" "cmd/compile/internal/staticdata" "cmd/compile/internal/typecheck" @@ -5537,7 +5538,7 @@ func (s *state) getClosureAndRcvr(fn *ir.SelectorExpr) (*ssa.Value, *ssa.Value) i := s.expr(fn.X) itab := s.newValue1(ssa.OpITab, types.Types[types.TUINTPTR], i) s.nilCheck(itab) - itabidx := fn.Offset() + 2*int64(types.PtrSize) + 8 // offset of fun field in runtime.itab + itabidx := fn.Offset() + rttype.ITab.OffsetOf("Fun") closure := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.UintptrPtr, itabidx, itab) rcvr := s.newValue1(ssa.OpIData, s.f.Config.Types.BytePtr, i) return closure, rcvr @@ -6522,7 +6523,7 @@ func (s *state) dynamicDottype(n *ir.DynamicTypeAssertExpr, commaok bool) (res, targetItab = s.expr(n.ITab) // TODO(mdempsky): Investigate whether compiling n.RType could be // better than loading itab.typ. - target = s.load(byteptr, s.newValue1I(ssa.OpOffPtr, byteptr, int64(types.PtrSize), targetItab)) // itab.typ + target = s.load(byteptr, s.newValue1I(ssa.OpOffPtr, byteptr, rttype.ITab.OffsetOf("Type"), targetItab)) } else { target = s.expr(n.RType) } @@ -6580,7 +6581,7 @@ func (s *state) dottype1(pos src.XPos, src, dst *types.Type, iface, source, targ return } // Load type out of itab, build interface with existing idata. - off := s.newValue1I(ssa.OpOffPtr, byteptr, int64(types.PtrSize), itab) + off := s.newValue1I(ssa.OpOffPtr, byteptr, rttype.ITab.OffsetOf("Type"), itab) typ := s.load(byteptr, off) idata := s.newValue1(ssa.OpIData, byteptr, iface) 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 s.startBlock(bOk) // nonempty -> empty // Need to load type from itab - off := s.newValue1I(ssa.OpOffPtr, byteptr, int64(types.PtrSize), itab) + off := s.newValue1I(ssa.OpOffPtr, byteptr, rttype.ITab.OffsetOf("Type"), itab) s.vars[typVar] = s.load(byteptr, off) s.endBlock() @@ -6644,7 +6645,7 @@ func (s *state) dottype1(pos src.XPos, src, dst *types.Type, iface, source, targ s.startBlock(bNonNil) typ := itab if !src.IsEmptyInterface() { - typ = s.load(byteptr, s.newValue1I(ssa.OpOffPtr, byteptr, int64(types.PtrSize), itab)) + typ = s.load(byteptr, s.newValue1I(ssa.OpOffPtr, byteptr, rttype.ITab.OffsetOf("Type"), itab)) } // Check the cache first. @@ -6685,9 +6686,9 @@ func (s *state) dottype1(pos src.XPos, src, dst *types.Type, iface, source, targ // Load hash from type or itab. var hash *ssa.Value if src.IsEmptyInterface() { - hash = s.newValue2(ssa.OpLoad, typs.UInt32, s.newValue1I(ssa.OpOffPtr, typs.UInt32Ptr, 2*s.config.PtrSize, typ), s.mem()) + hash = s.newValue2(ssa.OpLoad, typs.UInt32, s.newValue1I(ssa.OpOffPtr, typs.UInt32Ptr, rttype.Type.OffsetOf("Hash"), typ), s.mem()) } else { - hash = s.newValue2(ssa.OpLoad, typs.UInt32, s.newValue1I(ssa.OpOffPtr, typs.UInt32Ptr, 2*s.config.PtrSize, itab), s.mem()) + hash = s.newValue2(ssa.OpLoad, typs.UInt32, s.newValue1I(ssa.OpOffPtr, typs.UInt32Ptr, rttype.ITab.OffsetOf("Hash"), itab), s.mem()) } hash = s.newValue1(zext, typs.Uintptr, hash) s.vars[hashVar] = hash diff --git a/src/cmd/compile/internal/walk/switch.go b/src/cmd/compile/internal/walk/switch.go index b67d0114c7..d008cbc3ef 100644 --- a/src/cmd/compile/internal/walk/switch.go +++ b/src/cmd/compile/internal/walk/switch.go @@ -700,7 +700,7 @@ func typeHashFieldOf(pos src.XPos, itab *ir.UnaryExpr) *ir.SelectorExpr { } else { // runtime.itab's hash field if itabHashField == nil { - itabHashField = runtimeField("hash", int64(2*types.PtrSize), types.Types[types.TUINT32]) + itabHashField = runtimeField("hash", rttype.ITab.OffsetOf("Hash"), types.Types[types.TUINT32]) } hashField = itabHashField } diff --git a/src/cmd/compile/internal/walk/walk.go b/src/cmd/compile/internal/walk/walk.go index 001edcc332..6cb80270e5 100644 --- a/src/cmd/compile/internal/walk/walk.go +++ b/src/cmd/compile/internal/walk/walk.go @@ -10,6 +10,7 @@ import ( "cmd/compile/internal/base" "cmd/compile/internal/ir" "cmd/compile/internal/reflectdata" + "cmd/compile/internal/rttype" "cmd/compile/internal/ssagen" "cmd/compile/internal/typecheck" "cmd/compile/internal/types" @@ -345,8 +346,8 @@ func mayCall(n ir.Node) bool { // itabType loads the _type field from a runtime.itab struct. func itabType(itab ir.Node) ir.Node { if itabTypeField == nil { - // runtime.itab's _type field - itabTypeField = runtimeField("_type", int64(types.PtrSize), types.NewPtr(types.Types[types.TUINT8])) + // internal/abi.ITab's Type field + itabTypeField = runtimeField("Type", rttype.ITab.OffsetOf("Type"), types.NewPtr(types.Types[types.TUINT8])) } return boundedDotPtr(base.Pos, itab, itabTypeField) } diff --git a/src/internal/abi/iface.go b/src/internal/abi/iface.go index 01235f64e6..8fefcaefa0 100644 --- a/src/internal/abi/iface.go +++ b/src/internal/abi/iface.go @@ -8,10 +8,7 @@ package abi // It records the underlying concrete type (Type), the interface type it // is implementing (Inter), and some ancillary information. // -// layout of ITab known to compilers // allocated in non-garbage-collected memory -// Needs to be in sync with -// cmd/compile/internal/reflectdata/reflect.go:/^func.WritePluginTable. type ITab struct { Inter *InterfaceType Type *Type