Skip to content

Commit 0ffa1ea

Browse files
committed
[dev.regabi] cmd/compile: use *obj.LSym instead of *ir.Name for staticdata functions
Those functions only use (*ir.Name).Linksym(), so just change them to get an *obj.LSym directly. This helps get rid of un-necessary validations that their callers have already done. Passes toolstash -cmp. For #43737. Change-Id: Ifd6c2525e472f8e790940bc167665f9d74dd1bc5 Reviewed-on: https://go-review.googlesource.com/c/go/+/284121 Trust: Cuong Manh Le <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 7e0fa38 commit 0ffa1ea

File tree

3 files changed

+34
-45
lines changed

3 files changed

+34
-45
lines changed

src/cmd/compile/internal/staticdata/data.go

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,46 +25,29 @@ import (
2525
"cmd/internal/src"
2626
)
2727

28-
// InitAddr writes the static address of a to n. a must be an ONAME.
29-
// Neither n nor a is modified.
30-
func InitAddr(n *ir.Name, noff int64, a *ir.Name, aoff int64) {
28+
// InitAddrOffset writes the static name symbol lsym to n, it does not modify n.
29+
// It's the caller responsibility to make sure lsym is from ONAME/PEXTERN node.
30+
func InitAddrOffset(n *ir.Name, noff int64, lsym *obj.LSym, off int64) {
3131
if n.Op() != ir.ONAME {
3232
base.Fatalf("InitAddr n op %v", n.Op())
3333
}
3434
if n.Sym() == nil {
3535
base.Fatalf("InitAddr nil n sym")
3636
}
37-
if a.Op() != ir.ONAME {
38-
base.Fatalf("InitAddr a op %v", a.Op())
39-
}
4037
s := n.Linksym()
41-
s.WriteAddr(base.Ctxt, noff, types.PtrSize, a.Linksym(), aoff)
38+
s.WriteAddr(base.Ctxt, noff, types.PtrSize, lsym, off)
4239
}
4340

44-
// InitFunc writes the static address of f to n. f must be a global function.
45-
// Neither n nor f is modified.
46-
func InitFunc(n *ir.Name, noff int64, f *ir.Name) {
47-
if n.Op() != ir.ONAME {
48-
base.Fatalf("InitFunc n op %v", n.Op())
49-
}
50-
if n.Sym() == nil {
51-
base.Fatalf("InitFunc nil n sym")
52-
}
53-
if f.Class != ir.PFUNC {
54-
base.Fatalf("InitFunc class not PFUNC %d", f.Class)
55-
}
56-
s := n.Linksym()
57-
s.WriteAddr(base.Ctxt, noff, types.PtrSize, FuncLinksym(f), 0)
41+
// InitAddr is InitAddrOffset, with offset fixed to 0.
42+
func InitAddr(n *ir.Name, noff int64, lsym *obj.LSym) {
43+
InitAddrOffset(n, noff, lsym, 0)
5844
}
5945

60-
// InitSlice writes a static slice symbol {&arr, lencap, lencap} to n+noff.
61-
// InitSlice does not modify n.
62-
func InitSlice(n *ir.Name, noff int64, arr *ir.Name, lencap int64) {
46+
// InitSlice writes a static slice symbol {lsym, lencap, lencap} to n+noff, it does not modify n.
47+
// It's the caller responsibility to make sure lsym is from ONAME node.
48+
func InitSlice(n *ir.Name, noff int64, lsym *obj.LSym, lencap int64) {
6349
s := n.Linksym()
64-
if arr.Op() != ir.ONAME {
65-
base.Fatalf("InitSlice non-name arr %v", arr)
66-
}
67-
s.WriteAddr(base.Ctxt, noff, types.PtrSize, arr.Linksym(), 0)
50+
s.WriteAddr(base.Ctxt, noff, types.PtrSize, lsym, 0)
6851
s.WriteInt(base.Ctxt, noff+types.SliceLenOffset, types.PtrSize, lencap)
6952
s.WriteInt(base.Ctxt, noff+types.SliceCapOffset, types.PtrSize, lencap)
7053
}
@@ -73,7 +56,7 @@ func InitSliceBytes(nam *ir.Name, off int64, s string) {
7356
if nam.Op() != ir.ONAME {
7457
base.Fatalf("InitSliceBytes %v", nam)
7558
}
76-
InitSlice(nam, off, slicedata(nam.Pos(), s), int64(len(s)))
59+
InitSlice(nam, off, slicedata(nam.Pos(), s).Linksym(), int64(len(s)))
7760
}
7861

7962
const (
@@ -265,6 +248,13 @@ func FuncLinksym(n *ir.Name) *obj.LSym {
265248
return FuncSym(n.Sym()).Linksym()
266249
}
267250

251+
func GlobalLinksym(n *ir.Name) *obj.LSym {
252+
if n.Op() != ir.ONAME || n.Class != ir.PEXTERN {
253+
base.Fatalf("expected global variable: %v", n)
254+
}
255+
return n.Linksym()
256+
}
257+
268258
// NeedFuncSym ensures that s·f is exported, if needed.
269259
// It is only used with -dynlink.
270260
// When not compiling for dynamic linking,

src/cmd/compile/internal/staticinit/sched.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (s *Schedule) tryStaticInit(nn ir.Node) bool {
8181
func (s *Schedule) staticcopy(l *ir.Name, loff int64, rn *ir.Name, typ *types.Type) bool {
8282
if rn.Class == ir.PFUNC {
8383
// TODO if roff != 0 { panic }
84-
staticdata.InitFunc(l, loff, rn)
84+
staticdata.InitAddr(l, loff, staticdata.FuncLinksym(rn))
8585
return true
8686
}
8787
if rn.Class != ir.PEXTERN || rn.Sym().Pkg != types.LocalPkg {
@@ -138,9 +138,8 @@ func (s *Schedule) staticcopy(l *ir.Name, loff int64, rn *ir.Name, typ *types.Ty
138138

139139
case ir.OADDR:
140140
r := r.(*ir.AddrExpr)
141-
if a := r.X; a.Op() == ir.ONAME {
142-
a := a.(*ir.Name)
143-
staticdata.InitAddr(l, loff, a, 0)
141+
if a, ok := r.X.(*ir.Name); ok && a.Op() == ir.ONAME {
142+
staticdata.InitAddr(l, loff, staticdata.GlobalLinksym(a))
144143
return true
145144
}
146145

@@ -149,14 +148,14 @@ func (s *Schedule) staticcopy(l *ir.Name, loff int64, rn *ir.Name, typ *types.Ty
149148
switch r.X.Op() {
150149
case ir.OARRAYLIT, ir.OSLICELIT, ir.OSTRUCTLIT, ir.OMAPLIT:
151150
// copy pointer
152-
staticdata.InitAddr(l, loff, s.Temps[r], 0)
151+
staticdata.InitAddr(l, loff, staticdata.GlobalLinksym(s.Temps[r]))
153152
return true
154153
}
155154

156155
case ir.OSLICELIT:
157156
r := r.(*ir.CompLitExpr)
158157
// copy slice
159-
staticdata.InitSlice(l, loff, s.Temps[r], r.Len)
158+
staticdata.InitSlice(l, loff, staticdata.GlobalLinksym(s.Temps[r]), r.Len)
160159
return true
161160

162161
case ir.OARRAYLIT, ir.OSTRUCTLIT:
@@ -235,8 +234,8 @@ func (s *Schedule) StaticAssign(l *ir.Name, loff int64, r ir.Node, typ *types.Ty
235234

236235
case ir.OADDR:
237236
r := r.(*ir.AddrExpr)
238-
if name, offset, ok := StaticLoc(r.X); ok {
239-
staticdata.InitAddr(l, loff, name, offset)
237+
if name, offset, ok := StaticLoc(r.X); ok && name.Class == ir.PEXTERN {
238+
staticdata.InitAddrOffset(l, loff, name.Linksym(), offset)
240239
return true
241240
}
242241
fallthrough
@@ -249,7 +248,7 @@ func (s *Schedule) StaticAssign(l *ir.Name, loff int64, r ir.Node, typ *types.Ty
249248
a := StaticName(r.X.Type())
250249

251250
s.Temps[r] = a
252-
staticdata.InitAddr(l, loff, a, 0)
251+
staticdata.InitAddr(l, loff, a.Linksym())
253252

254253
// Init underlying literal.
255254
assign(base.Pos, a, 0, r.X)
@@ -273,7 +272,7 @@ func (s *Schedule) StaticAssign(l *ir.Name, loff int64, r ir.Node, typ *types.Ty
273272
ta.SetNoalg(true)
274273
a := StaticName(ta)
275274
s.Temps[r] = a
276-
staticdata.InitSlice(l, loff, a, r.Len)
275+
staticdata.InitSlice(l, loff, a.Linksym(), r.Len)
277276
// Fall through to init underlying array.
278277
l = a
279278
loff = 0
@@ -308,7 +307,7 @@ func (s *Schedule) StaticAssign(l *ir.Name, loff int64, r ir.Node, typ *types.Ty
308307
// Closures with no captured variables are globals,
309308
// so the assignment can be done at link time.
310309
// TODO if roff != 0 { panic }
311-
staticdata.InitFunc(l, loff, r.Func.Nname)
310+
staticdata.InitAddr(l, loff, staticdata.FuncLinksym(r.Func.Nname))
312311
return true
313312
}
314313
ir.ClosureDebugRuntimeCheck(r)
@@ -345,7 +344,7 @@ func (s *Schedule) StaticAssign(l *ir.Name, loff int64, r ir.Node, typ *types.Ty
345344
// Create a copy of l to modify while we emit data.
346345

347346
// Emit itab, advance offset.
348-
staticdata.InitAddr(l, loff, itab.X.(*ir.Name), 0)
347+
staticdata.InitAddr(l, loff, itab.X.(*ir.Name).Linksym())
349348

350349
// Emit data.
351350
if types.IsDirectIface(val.Type()) {
@@ -361,7 +360,7 @@ func (s *Schedule) StaticAssign(l *ir.Name, loff int64, r ir.Node, typ *types.Ty
361360
a := StaticName(val.Type())
362361
s.Temps[val] = a
363362
assign(base.Pos, a, 0, val)
364-
staticdata.InitAddr(l, loff+int64(types.PtrSize), a, 0)
363+
staticdata.InitAddr(l, loff+int64(types.PtrSize), a.Linksym())
365364
}
366365

367366
return true

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func slicelit(ctxt initContext, n *ir.CompLitExpr, var_ ir.Node, init *ir.Nodes)
297297
if !ok || name.Class != ir.PEXTERN {
298298
base.Fatalf("slicelit: %v", var_)
299299
}
300-
staticdata.InitSlice(name, offset, vstat, t.NumElem())
300+
staticdata.InitSlice(name, offset, vstat.Linksym(), t.NumElem())
301301
return
302302
}
303303

@@ -647,15 +647,15 @@ func genAsStatic(as *ir.AssignStmt) {
647647
return
648648
case ir.OMETHEXPR:
649649
r := r.(*ir.SelectorExpr)
650-
staticdata.InitFunc(name, offset, r.FuncName())
650+
staticdata.InitAddr(name, offset, staticdata.FuncLinksym(r.FuncName()))
651651
return
652652
case ir.ONAME:
653653
r := r.(*ir.Name)
654654
if r.Offset_ != 0 {
655655
base.Fatalf("genAsStatic %+v", as)
656656
}
657657
if r.Class == ir.PFUNC {
658-
staticdata.InitFunc(name, offset, r)
658+
staticdata.InitAddr(name, offset, staticdata.FuncLinksym(r))
659659
return
660660
}
661661
}

0 commit comments

Comments
 (0)