compileBlock refactor

This commit is contained in:
xushiwei
2024-05-12 18:42:45 +08:00
parent 9ac0450255
commit 45babef689
2 changed files with 23 additions and 26 deletions

View File

@@ -250,7 +250,6 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function, call bool)
} }
if nblk := len(f.Blocks); nblk > 0 { if nblk := len(f.Blocks); nblk > 0 {
fn.MakeBlocks(nblk) // to set fn.HasBody() = true fn.MakeBlocks(nblk) // to set fn.HasBody() = true
isPyMod := p.pyMod != ""
p.inits = append(p.inits, func() { p.inits = append(p.inits, func() {
p.fn = fn p.fn = fn
defer func() { defer func() {
@@ -270,9 +269,9 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function, call bool)
off[i] = p.compilePhis(b, block) off[i] = p.compilePhis(b, block)
} }
for i, block := range f.Blocks { for i, block := range f.Blocks {
doInit := (i == 0 && name == "main") doMainInit := (i == 0 && name == "main")
doPyModInit := (isPyMod && i == 1 && f.Name() == "init" && sig.Recv() == nil) doModInit := (i == 1 && f.Name() == "init" && sig.Recv() == nil)
p.compileBlock(b, block, off[i], doInit, doPyModInit) p.compileBlock(b, block, off[i], doMainInit, doModInit)
} }
for _, phi := range p.phis { for _, phi := range p.phis {
phi() phi()
@@ -329,17 +328,18 @@ func (p *context) funcOf(fn *ssa.Function) (aFn llssa.Function, pyFn llssa.PyObj
return return
} }
func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, n int, doInit, pyModInit bool) llssa.BasicBlock { func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, n int, doMainInit, doModInit bool) llssa.BasicBlock {
var last int var last int
var instrs []ssa.Instruction var pyModInit bool
var instrs = block.Instrs[n:]
var ret = p.fn.Block(block.Index) var ret = p.fn.Block(block.Index)
b.SetBlock(ret) b.SetBlock(ret)
if pyModInit { if doModInit {
last = len(block.Instrs) - 1 if pyModInit = p.pyMod != ""; pyModInit {
instrs = block.Instrs[n:last] last = len(instrs) - 1
} else { instrs = instrs[:last]
instrs = block.Instrs[n:] }
if doInit { } else if doMainInit {
prog := p.prog prog := p.prog
pkg := p.pkg pkg := p.pkg
fn := p.fn fn := p.fn
@@ -352,12 +352,11 @@ 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 _, instr := range instrs {
p.compileInstr(b, instr) p.compileInstr(b, instr)
} }
if pyModInit { if pyModInit {
jump := block.Instrs[last].(*ssa.Jump) jump := block.Instrs[n+last].(*ssa.Jump)
jumpTo := p.jumpTo(jump) jumpTo := p.jumpTo(jump)
modPath := p.pyMod modPath := p.pyMod
modName := pysymPrefix + modPath modName := pysymPrefix + modPath

View File

@@ -1,7 +1,5 @@
; ModuleID = '_pyg/module.c' ; ModuleID = '_pyg/module.c'
source_filename = "_pyg/module.c" source_filename = "_pyg/module.c"
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
target triple = "arm64-apple-macosx13.0.0"
%struct.PyObject = type opaque %struct.PyObject = type opaque