cl: compileFunction if not exists

This commit is contained in:
xushiwei
2024-05-06 18:30:53 +08:00
parent cffb5e9539
commit da20aea408
3 changed files with 32 additions and 17 deletions

View File

@@ -5,6 +5,9 @@ import (
)
func main() {
func(n1, n2 int) {
c.Printf(c.Str("%d %d\n"), n1, n2)
}(100, 200)
fn1 := func(n1, n2 int) {
c.Printf(c.Str("%d %d\n"), n1, n2)
}

View File

@@ -3,6 +3,7 @@ source_filename = "main"
@"main.init$guard" = global ptr null
@0 = private unnamed_addr constant [7 x i8] c"%d %d\0A\00", align 1
@1 = private unnamed_addr constant [7 x i8] c"%d %d\0A\00", align 1
define void @main.init() {
_llgo_0:
@@ -21,10 +22,11 @@ define void @main() {
_llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
call void @"main.main$1"(i64 100, i64 200)
%0 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 16)
%1 = alloca { ptr, ptr }, align 8
%2 = getelementptr inbounds { ptr, ptr }, ptr %1, i32 0, i32 0
store ptr @"__llgo_stub.main.main$1", ptr %2, align 8
store ptr @"__llgo_stub.main.main$2", ptr %2, align 8
%3 = getelementptr inbounds { ptr, ptr }, ptr %1, i32 0, i32 1
store ptr null, ptr %3, align 8
%4 = load { ptr, ptr }, ptr %1, align 8
@@ -34,7 +36,7 @@ _llgo_0:
store ptr %0, ptr %6, align 8
%7 = alloca { ptr, ptr }, align 8
%8 = getelementptr inbounds { ptr, ptr }, ptr %7, i32 0, i32 0
store ptr @"main.main$2", ptr %8, align 8
store ptr @"main.main$3", ptr %8, align 8
%9 = getelementptr inbounds { ptr, ptr }, ptr %7, i32 0, i32 1
store ptr %5, ptr %9, align 8
%10 = load { ptr, ptr }, ptr %7, align 8
@@ -46,21 +48,27 @@ _llgo_0:
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
define void @"main.main$1"(i64 %0, i64 %1) {
_llgo_0:
%2 = call i32 (ptr, ...) @printf(ptr @0, i64 %0, i64 %1)
ret void
}
define linkonce void @"__llgo_stub.main.main$1"(ptr %0, i64 %1, i64 %2) {
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
define void @"main.main$2"(i64 %0, i64 %1) {
_llgo_0:
tail call void @"main.main$1"(i64 %1, i64 %2)
%2 = call i32 (ptr, ...) @printf(ptr @1, i64 %0, i64 %1)
ret void
}
define void @"main.main$2"(ptr %0) {
define linkonce void @"__llgo_stub.main.main$2"(ptr %0, i64 %1, i64 %2) {
_llgo_0:
tail call void @"main.main$2"(i64 %1, i64 %2)
ret void
}
define void @"main.main$3"(ptr %0) {
_llgo_0:
%1 = load { ptr }, ptr %0, align 8
%2 = extractvalue { ptr } %1, 0

View File

@@ -169,7 +169,7 @@ func (p *context) compileMethods(pkg llssa.Package, typ types.Type) {
for i, n := 0, mthds.Len(); i < n; i++ {
mthd := mthds.At(i)
if ssaMthd := prog.MethodValue(mthd); ssaMthd != nil {
p.compileFunc(pkg, mthd.Obj().Pkg(), ssaMthd)
p.compileFuncDecl(pkg, mthd.Obj().Pkg(), ssaMthd)
}
}
}
@@ -200,7 +200,7 @@ func makeClosureCtx(pkg *types.Package, vars []*ssa.FreeVar) *types.Var {
return types.NewParam(token.NoPos, pkg, "__llgo_ctx", t)
}
func (p *context) compileFunc(pkg llssa.Package, pkgTypes *types.Package, f *ssa.Function) llssa.Function {
func (p *context) compileFuncDecl(pkg llssa.Package, pkgTypes *types.Package, f *ssa.Function) llssa.Function {
name, ftype := p.funcName(pkgTypes, f, true)
if ftype != goFunc {
return nil
@@ -455,7 +455,7 @@ func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue
ret = b.BuiltinCall(fn, args...)
}
case *ssa.Function:
fn, ftype := p.funcOf(cv)
fn, ftype := p.compileFunction(cv)
switch ftype {
case goFunc, cFunc:
args := p.compileValues(b, call.Args, kind)
@@ -636,6 +636,15 @@ func (p *context) compileInstr(b llssa.Builder, instr ssa.Instruction) {
}
}
func (p *context) compileFunction(v *ssa.Function) (llssa.Function, int) {
// v.Pkg == nil: means auto generated function?
if v.Pkg == p.goPkg || v.Pkg == nil {
// function in this package
return p.compileFuncDecl(p.pkg, p.goTyps, v), goFunc
}
return p.funcOf(v)
}
func (p *context) compileValue(b llssa.Builder, v ssa.Value) llssa.Expr {
if iv, ok := v.(instrOrValue); ok {
return p.compileInstrOrValue(b, iv, true)
@@ -649,12 +658,7 @@ func (p *context) compileValue(b llssa.Builder, v ssa.Value) llssa.Expr {
}
}
case *ssa.Function:
// v.Pkg == nil: means auto generated function?
if v.Pkg == p.goPkg || v.Pkg == nil { // function in this package
fn := p.compileFunc(p.pkg, p.goTyps, v)
return fn.Expr
}
fn, _ := p.funcOf(v)
fn, _ := p.compileFunction(v)
return fn.Expr
case *ssa.Global:
g := p.varOf(v)
@@ -747,7 +751,7 @@ func NewPackage(prog llssa.Program, pkg *ssa.Package, files []*ast.File) (ret ll
// Do not try to build generic (non-instantiated) functions.
continue
}
ctx.compileFunc(ret, member.Pkg.Pkg, member)
ctx.compileFuncDecl(ret, member.Pkg.Pkg, member)
case *ssa.Type:
ctx.compileType(ret, member)
case *ssa.Global: