mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
Object file writing routines are used not just at the end
of the compilation but also during static data layout in walk.
Split them into their own package.
[git-generate]
cd src/cmd/compile/internal/gc
rf '
# Move bit vector to new package bitvec
mv bvec.n bvec.N
mv bvec.b bvec.B
mv bvec BitVec
mv bvalloc New
mv bvbulkalloc NewBulk
mv bulkBvec.next bulkBvec.Next
mv bulkBvec Bulk
mv H0 h0
mv Hp hp
# Leave bvecSet and bitmap hashes behind - not needed as broadly.
mv bvecSet.extractUniqe bvecSet.extractUnique
mv h0 bvecSet bvecSet.grow bvecSet.add \
bvecSet.extractUnique hashbitmap bvset.go
mv bv.go cmd/compile/internal/bitvec
ex . ../arm ../arm64 ../mips ../mips64 ../ppc64 ../s390x ../riscv64 {
import "cmd/internal/obj"
var a *obj.Addr
var i int64
Addrconst(a, i) -> a.SetConst(i)
var p, to *obj.Prog
Patch(p, to) -> p.To.SetTarget(to)
}
rm Addrconst Patch
# Move object-writing API to new package objw
mv duint8 Objw_Uint8
mv duint16 Objw_Uint16
mv duint32 Objw_Uint32
mv duintptr Objw_Uintptr
mv duintxx Objw_UintN
mv dsymptr Objw_SymPtr
mv dsymptrOff Objw_SymPtrOff
mv dsymptrWeakOff Objw_SymPtrWeakOff
mv ggloblsym Objw_Global
mv dbvec Objw_BitVec
mv newProgs NewProgs
mv Progs.clearp Progs.Clear
mv Progs.settext Progs.SetText
mv Progs.next Progs.Next
mv Progs.pc Progs.PC
mv Progs.pos Progs.Pos
mv Progs.curfn Progs.CurFunc
mv Progs.progcache Progs.Cache
mv Progs.cacheidx Progs.CacheIndex
mv Progs.nextLive Progs.NextLive
mv Progs.prevLive Progs.PrevLive
mv Progs.Appendpp Progs.Append
mv LivenessIndex.stackMapIndex LivenessIndex.StackMapIndex
mv LivenessIndex.isUnsafePoint LivenessIndex.IsUnsafePoint
mv Objw_Uint8 Objw_Uint16 Objw_Uint32 Objw_Uintptr Objw_UintN \
Objw_SymPtr Objw_SymPtrOff Objw_SymPtrWeakOff Objw_Global \
Objw_BitVec \
objw.go
mv sharedProgArray NewProgs Progs \
LivenessIndex StackMapDontCare \
LivenessDontCare LivenessIndex.StackMapValid \
Progs.NewProg Progs.Flush Progs.Free Progs.Prog Progs.Clear Progs.Append Progs.SetText \
prog.go
mv prog.go objw.go cmd/compile/internal/objw
# Move ggloblnod to obj with the rest of the non-objw higher-level writing.
mv ggloblnod obj.go
'
cd ../objw
rf '
mv Objw_Uint8 Uint8
mv Objw_Uint16 Uint16
mv Objw_Uint32 Uint32
mv Objw_Uintptr Uintptr
mv Objw_UintN UintN
mv Objw_SymPtr SymPtr
mv Objw_SymPtrOff SymPtrOff
mv Objw_SymPtrWeakOff SymPtrWeakOff
mv Objw_Global Global
mv Objw_BitVec BitVec
'
Change-Id: I2b87085aa788564fb322e9c55bddd73347b4d5fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/279310
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
117 lines
3.6 KiB
Go
117 lines
3.6 KiB
Go
// Copyright 2009 The Go 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 gc
|
|
|
|
import (
|
|
"cmd/compile/internal/base"
|
|
"cmd/compile/internal/ir"
|
|
"cmd/compile/internal/objw"
|
|
"cmd/compile/internal/typecheck"
|
|
"cmd/compile/internal/types"
|
|
"cmd/internal/obj"
|
|
)
|
|
|
|
// A function named init is a special case.
|
|
// It is called by the initialization before main is run.
|
|
// To make it unique within a package and also uncallable,
|
|
// the name, normally "pkg.init", is altered to "pkg.init.0".
|
|
var renameinitgen int
|
|
|
|
func renameinit() *types.Sym {
|
|
s := typecheck.LookupNum("init.", renameinitgen)
|
|
renameinitgen++
|
|
return s
|
|
}
|
|
|
|
// fninit makes and returns an initialization record for the package.
|
|
// See runtime/proc.go:initTask for its layout.
|
|
// The 3 tasks for initialization are:
|
|
// 1) Initialize all of the packages the current package depends on.
|
|
// 2) Initialize all the variables that have initializers.
|
|
// 3) Run any init functions.
|
|
func fninit() *ir.Name {
|
|
nf := initOrder(typecheck.Target.Decls)
|
|
|
|
var deps []*obj.LSym // initTask records for packages the current package depends on
|
|
var fns []*obj.LSym // functions to call for package initialization
|
|
|
|
// Find imported packages with init tasks.
|
|
for _, pkg := range typecheck.Target.Imports {
|
|
n := typecheck.Resolve(ir.NewIdent(base.Pos, pkg.Lookup(".inittask")))
|
|
if n.Op() == ir.ONONAME {
|
|
continue
|
|
}
|
|
if n.Op() != ir.ONAME || n.(*ir.Name).Class_ != ir.PEXTERN {
|
|
base.Fatalf("bad inittask: %v", n)
|
|
}
|
|
deps = append(deps, n.(*ir.Name).Sym().Linksym())
|
|
}
|
|
|
|
// Make a function that contains all the initialization statements.
|
|
if len(nf) > 0 {
|
|
base.Pos = nf[0].Pos() // prolog/epilog gets line number of first init stmt
|
|
initializers := typecheck.Lookup("init")
|
|
fn := typecheck.DeclFunc(initializers, ir.NewFuncType(base.Pos, nil, nil, nil))
|
|
for _, dcl := range typecheck.InitTodoFunc.Dcl {
|
|
dcl.Curfn = fn
|
|
}
|
|
fn.Dcl = append(fn.Dcl, typecheck.InitTodoFunc.Dcl...)
|
|
typecheck.InitTodoFunc.Dcl = nil
|
|
|
|
fn.Body.Set(nf)
|
|
typecheck.FinishFuncBody()
|
|
|
|
typecheck.Func(fn)
|
|
ir.CurFunc = fn
|
|
typecheck.Stmts(nf)
|
|
ir.CurFunc = nil
|
|
typecheck.Target.Decls = append(typecheck.Target.Decls, fn)
|
|
fns = append(fns, initializers.Linksym())
|
|
}
|
|
if typecheck.InitTodoFunc.Dcl != nil {
|
|
// We only generate temps using initTodo if there
|
|
// are package-scope initialization statements, so
|
|
// something's weird if we get here.
|
|
base.Fatalf("initTodo still has declarations")
|
|
}
|
|
typecheck.InitTodoFunc = nil
|
|
|
|
// Record user init functions.
|
|
for _, fn := range typecheck.Target.Inits {
|
|
// Skip init functions with empty bodies.
|
|
if len(fn.Body) == 1 {
|
|
if stmt := fn.Body[0]; stmt.Op() == ir.OBLOCK && len(stmt.(*ir.BlockStmt).List) == 0 {
|
|
continue
|
|
}
|
|
}
|
|
fns = append(fns, fn.Nname.Sym().Linksym())
|
|
}
|
|
|
|
if len(deps) == 0 && len(fns) == 0 && types.LocalPkg.Name != "main" && types.LocalPkg.Name != "runtime" {
|
|
return nil // nothing to initialize
|
|
}
|
|
|
|
// Make an .inittask structure.
|
|
sym := typecheck.Lookup(".inittask")
|
|
task := typecheck.NewName(sym)
|
|
task.SetType(types.Types[types.TUINT8]) // fake type
|
|
task.Class_ = ir.PEXTERN
|
|
sym.Def = task
|
|
lsym := sym.Linksym()
|
|
ot := 0
|
|
ot = objw.Uintptr(lsym, ot, 0) // state: not initialized yet
|
|
ot = objw.Uintptr(lsym, ot, uint64(len(deps)))
|
|
ot = objw.Uintptr(lsym, ot, uint64(len(fns)))
|
|
for _, d := range deps {
|
|
ot = objw.SymPtr(lsym, ot, d, 0)
|
|
}
|
|
for _, f := range fns {
|
|
ot = objw.SymPtr(lsym, ot, f, 0)
|
|
}
|
|
// An initTask has pointers, but none into the Go heap.
|
|
// It's not quite read only, the state field must be modifiable.
|
|
objw.Global(lsym, int32(ot), obj.NOPTR)
|
|
return task
|
|
}
|