]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: use symbolic offsets of fields in internal/abi.ITab
authorKeith Randall <khr@golang.org>
Wed, 13 Dec 2023 21:20:01 +0000 (13:20 -0800)
committerKeith Randall <khr@golang.org>
Fri, 16 Feb 2024 20:25:04 +0000 (20:25 +0000)
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 <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
src/cmd/compile/internal/ssagen/ssa.go
src/cmd/compile/internal/walk/switch.go
src/cmd/compile/internal/walk/walk.go
src/internal/abi/iface.go

index 6544c469bfec6d81282b2f08e8d3df0580244306..68b1547048f8927f3fcf5cca82d6830f82c5dab5 100644 (file)
@@ -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
index b67d0114c7b6381a226602c8e046a5cd4ee01d25..d008cbc3ef0d19268f90179a132069a13bdf564e 100644 (file)
@@ -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
        }
index 001edcc3325c223c6cb72785c03b913a1c0e0f4e..6cb80270e5242061c629d329036f6c64c0a403ab 100644 (file)
@@ -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)
 }
index 01235f64e6e17da7136e22b524462eb6ec84cb07..8fefcaefa00788eca0cc1ea3a4cc669371af29d2 100644 (file)
@@ -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