Add return value to llvm's ir main function definition

This commit is contained in:
tsingbx
2024-05-16 15:27:14 +08:00
parent 369495c8d3
commit de1281bb66

View File

@@ -152,6 +152,10 @@ type context struct {
phis []func() phis []func()
} }
func (p *context) inMain(instr ssa.Instruction) bool {
return instr.Parent().Name() == "main"
}
func (p *context) compileType(pkg llssa.Package, t *ssa.Type) { func (p *context) compileType(pkg llssa.Package, t *ssa.Type) {
tn := t.Object().(*types.TypeName) tn := t.Object().(*types.TypeName)
if tn.IsAlias() { // don't need to compile alias type if tn.IsAlias() { // don't need to compile alias type
@@ -235,7 +239,7 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function, call bool)
ctx := makeClosureCtx(pkgTypes, f.FreeVars) ctx := makeClosureCtx(pkgTypes, f.FreeVars)
sig = llssa.FuncAddCtx(ctx, sig) sig = llssa.FuncAddCtx(ctx, sig)
} else { } else {
if debugInstr { if debugInstr && sig != nil && sig.Recv() != nil {
log.Println("==> NewFunc", name, "type:", sig.Recv(), sig) log.Println("==> NewFunc", name, "type:", sig.Recv(), sig)
} }
} }
@@ -244,10 +248,13 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function, call bool)
argc := types.NewParam(token.NoPos, pkgTypes, "", types.Typ[types.Int32]) argc := types.NewParam(token.NoPos, pkgTypes, "", types.Typ[types.Int32])
argv := types.NewParam(token.NoPos, pkgTypes, "", argvTy) argv := types.NewParam(token.NoPos, pkgTypes, "", argvTy)
params := types.NewTuple(argc, argv) params := types.NewTuple(argc, argv)
sig = types.NewSignatureType(nil, nil, nil, params, nil, false) ret := types.NewParam(token.NoPos, pkgTypes, "", types.Typ[types.Int])
results := types.NewTuple(ret)
sig = types.NewSignatureType(nil, nil, nil, params, results, false)
} }
fn = pkg.NewFuncEx(name, sig, llssa.Background(ftype), hasCtx) fn = pkg.NewFuncEx(name, sig, llssa.Background(ftype), hasCtx)
} }
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
p.inits = append(p.inits, func() { p.inits = append(p.inits, func() {
@@ -772,6 +779,10 @@ func (p *context) compileInstr(b llssa.Builder, instr ssa.Instruction) {
results[i] = p.compileValue(b, r) results[i] = p.compileValue(b, r)
} }
} }
if p.inMain(instr) {
results = make([]llssa.Expr, 1)
results[0] = p.prog.IntVal(0, p.prog.Int())
}
b.Return(results...) b.Return(results...)
case *ssa.If: case *ssa.If:
fn := p.fn fn := p.fn