compiler: make main function normally to compatible multiple main functions in testing mode

This commit is contained in:
Li Jie
2025-01-13 09:37:26 +08:00
parent 5cfeddef73
commit 85d01d6f28
6 changed files with 64 additions and 72 deletions

View File

@@ -127,10 +127,6 @@ const (
pkgFNoOldInit = 0x80 // flag if no initFnNameOld
)
func (p *context) inMain(instr ssa.Instruction) bool {
return p.fn.Name() == "main"
}
func (p *context) compileType(pkg llssa.Package, t *ssa.Type) {
tn := t.Object().(*types.TypeName)
if tn.IsAlias() { // don't need to compile alias type
@@ -186,10 +182,6 @@ func makeClosureCtx(pkg *types.Package, vars []*ssa.FreeVar) *types.Var {
return types.NewParam(token.NoPos, pkg, "__llgo_ctx", t)
}
var (
argvTy = types.NewPointer(types.NewPointer(types.Typ[types.Int8]))
)
func isCgoExternSymbol(f *ssa.Function) bool {
name := f.Name()
return isCgoCfunc(name) || isCgoCmacro(name)
@@ -247,14 +239,6 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun
}
}
if fn == nil {
if name == "main" {
argc := types.NewParam(token.NoPos, pkgTypes, "", types.Typ[types.Int32])
argv := types.NewParam(token.NoPos, pkgTypes, "", argvTy)
params := types.NewTuple(argc, argv)
ret := types.NewParam(token.NoPos, pkgTypes, "", p.prog.CInt().RawType())
results := types.NewTuple(ret)
sig = types.NewSignatureType(nil, nil, nil, params, results, false)
}
fn = pkg.NewFuncEx(name, sig, llssa.Background(ftype), hasCtx, f.Origin() != nil)
if debugSymbols {
fn.Inline(llssa.NoInline)
@@ -309,9 +293,8 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun
i := 0
for {
block := f.Blocks[i]
doMainInit := (i == 0 && name == "main")
doModInit := (i == 1 && isInit)
p.compileBlock(b, block, off[i], doMainInit, doModInit)
p.compileBlock(b, block, off[i], doModInit)
if isCgo {
// just process first block for performance
break
@@ -385,7 +368,7 @@ func (p *context) debugParams(b llssa.Builder, f *ssa.Function) {
}
}
func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, n int, doMainInit, doModInit bool) llssa.BasicBlock {
func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, n int, doModInit bool) llssa.BasicBlock {
var last int
var pyModInit bool
var prog = p.prog
@@ -408,15 +391,6 @@ func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, n int, do
pkg.AfterInit(b, ret)
}
}
} else if doMainInit {
argc := pkg.NewVar("__llgo_argc", types.NewPointer(types.Typ[types.Int32]), llssa.InC)
argv := pkg.NewVar("__llgo_argv", types.NewPointer(argvTy), llssa.InC)
argc.InitNil()
argv.InitNil()
b.Store(argc.Expr, fn.Param(0))
b.Store(argv.Expr, fn.Param(1))
callRuntimeInit(b, pkg)
b.Call(pkg.FuncOf("main.init").Expr)
}
fnName := block.Parent().Name()
cgoReturned := false
@@ -499,11 +473,6 @@ const (
RuntimeInit = llssa.PkgRuntime + ".init"
)
func callRuntimeInit(b llssa.Builder, pkg llssa.Package) {
fn := pkg.NewFunc(RuntimeInit, llssa.NoArgsNoRet, llssa.InC) // don't need to convert runtime.init
b.Call(fn.Expr)
}
func isAny(t types.Type) bool {
if t, ok := t.Underlying().(*types.Interface); ok {
return t.Empty()
@@ -834,10 +803,6 @@ func (p *context) compileInstr(b llssa.Builder, instr ssa.Instruction) {
results[i] = p.compileValue(b, r)
}
}
if p.inMain(instr) {
results = make([]llssa.Expr, 1)
results[0] = p.prog.IntVal(0, p.prog.CInt())
}
b.Return(results...)
case *ssa.If:
fn := p.fn