patch library: call init

This commit is contained in:
xushiwei
2024-06-18 18:23:16 +08:00
parent 778a4373ae
commit 8c876c302a
2 changed files with 29 additions and 15 deletions

View File

@@ -100,13 +100,21 @@ type context struct {
inits []func() inits []func()
phis []func() phis []func()
inCFunc bool state pkgState
skipall bool inCFunc bool
hasPatch bool skipall bool
} }
type pkgState byte
const (
pkgNormal pkgState = iota
pkgHasPatch
pkgInPatch
)
func (p *context) inMain(instr ssa.Instruction) bool { func (p *context) inMain(instr ssa.Instruction) bool {
return instr.Parent().Name() == "main" return p.fn.Name() == "main"
} }
func (p *context) compileType(pkg llssa.Package, t *ssa.Type) { func (p *context) compileType(pkg llssa.Package, t *ssa.Type) {
@@ -186,6 +194,7 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun
} }
var isInit bool var isInit bool
var state = p.state
var sig = f.Signature var sig = f.Signature
var hasCtx = len(f.FreeVars) > 0 var hasCtx = len(f.FreeVars) > 0
if hasCtx { if hasCtx {
@@ -208,6 +217,8 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun
ret := types.NewParam(token.NoPos, pkgTypes, "", p.prog.CInt().RawType()) ret := types.NewParam(token.NoPos, pkgTypes, "", p.prog.CInt().RawType())
results := types.NewTuple(ret) results := types.NewTuple(ret)
sig = types.NewSignatureType(nil, nil, nil, params, results, false) sig = types.NewSignatureType(nil, nil, nil, params, results, false)
} else if isInit && state == pkgHasPatch {
name = initFnNameOfHasPatch(name)
} }
fn = pkg.NewFuncEx(name, sig, llssa.Background(ftype), hasCtx) fn = pkg.NewFuncEx(name, sig, llssa.Background(ftype), hasCtx)
} }
@@ -219,6 +230,7 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun
} }
p.inits = append(p.inits, func() { p.inits = append(p.inits, func() {
p.fn = fn p.fn = fn
p.state = state // restore pkgState when compiling funcBody
defer func() { defer func() {
p.fn = nil p.fn = nil
}() }()
@@ -263,8 +275,9 @@ func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, n int, do
var pyModInit bool var pyModInit bool
var prog = p.prog var prog = p.prog
var pkg = p.pkg var pkg = p.pkg
var fn = p.fn
var instrs = block.Instrs[n:] var instrs = block.Instrs[n:]
var ret = p.fn.Block(block.Index) var ret = fn.Block(block.Index)
b.SetBlock(ret) b.SetBlock(ret)
if doModInit { if doModInit {
if pyModInit = p.pyMod != ""; pyModInit { if pyModInit = p.pyMod != ""; pyModInit {
@@ -277,7 +290,6 @@ func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, n int, do
}) })
} }
} else if doMainInit { } else if doMainInit {
fn := p.fn
argc := pkg.NewVar("__llgo_argc", types.NewPointer(types.Typ[types.Int32]), llssa.InC) argc := pkg.NewVar("__llgo_argc", types.NewPointer(types.Typ[types.Int32]), llssa.InC)
argv := pkg.NewVar("__llgo_argv", types.NewPointer(argvTy), llssa.InC) argv := pkg.NewVar("__llgo_argv", types.NewPointer(argvTy), llssa.InC)
argc.InitNil() argc.InitNil()
@@ -287,7 +299,12 @@ func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, n int, do
callRuntimeInit(b, pkg) callRuntimeInit(b, pkg)
b.Call(pkg.FuncOf("main.init").Expr) b.Call(pkg.FuncOf("main.init").Expr)
} }
for _, instr := range instrs { for i, instr := range instrs {
if i == 1 && doModInit && p.state == pkgInPatch {
initFnNameOld := initFnNameOfHasPatch(p.fn.Name())
fnOld := pkg.NewFunc(initFnNameOld, llssa.NoArgsNoRet, llssa.InC)
b.Call(fnOld.Expr)
}
p.compileInstr(b, instr) p.compileInstr(b, instr)
} }
if pyModInit { if pyModInit {
@@ -298,7 +315,7 @@ func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, n int, do
modPtr := pkg.PyNewModVar(modName, true).Expr modPtr := pkg.PyNewModVar(modName, true).Expr
mod := b.Load(modPtr) mod := b.Load(modPtr)
cond := b.BinOp(token.NEQ, mod, prog.Nil(mod.Type)) cond := b.BinOp(token.NEQ, mod, prog.Nil(mod.Type))
newBlk := p.fn.MakeBlock() newBlk := fn.MakeBlock()
b.If(cond, jumpTo, newBlk) b.If(cond, jumpTo, newBlk)
b.SetBlockEx(newBlk, llssa.AtEnd, false) b.SetBlockEx(newBlk, llssa.AtEnd, false)
b.Store(modPtr, b.PyImportMod(modPath)) b.Store(modPtr, b.PyImportMod(modPath))
@@ -755,11 +772,12 @@ func NewPackageEx(prog llssa.Program, patches Patches, pkg *ssa.Package, files [
if hasPatch { if hasPatch {
skips := ctx.skips skips := ctx.skips
ctx.skips = nil ctx.skips = nil
ctx.state = pkgInPatch
processPkg(ctx, ret, alt) processPkg(ctx, ret, alt)
ctx.state = pkgHasPatch
ctx.skips = skips ctx.skips = skips
} }
if !ctx.skipall { if !ctx.skipall {
ctx.hasPatch = hasPatch
processPkg(ctx, ret, pkg) processPkg(ctx, ret, pkg)
} }
for len(ctx.inits) > 0 { for len(ctx.inits) > 0 {
@@ -772,11 +790,9 @@ func NewPackageEx(prog llssa.Program, patches Patches, pkg *ssa.Package, files [
return return
} }
/* TODO(xsw): func initFnNameOfHasPatch(name string) string {
func inPatch(ctx *context) bool { return name + "$hasPatch"
return ctx.skips == nil
} }
*/
func processPkg(ctx *context, ret llssa.Package, pkg *ssa.Package) { func processPkg(ctx *context, ret llssa.Package, pkg *ssa.Package) {
type namedMember struct { type namedMember struct {

View File

@@ -234,12 +234,10 @@ func newParams(fn Type, prog Program) (params []Type, hasVArg bool) {
return return
} }
/*
// Name returns the function's name. // Name returns the function's name.
func (p Function) Name() string { func (p Function) Name() string {
return p.impl.Name() return p.impl.Name()
} }
*/
// Params returns the function's ith parameter. // Params returns the function's ith parameter.
func (p Function) Param(i int) Expr { func (p Function) Param(i int) Expr {