From f07a62d1363f408283aa81a89ded60d7f6e8122c Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sat, 4 May 2024 07:47:18 +0800 Subject: [PATCH] toLLVMFunc: closure --- cl/_testrt/{_intgen => intgen}/in.go | 0 cl/_testrt/{_intgen => intgen}/out.ll | 0 internal/runtime/z_closure.go | 2 ++ ssa/cl_test.go | 2 +- ssa/expr.go | 2 +- ssa/package.go | 16 ++++------------ ssa/type.go | 9 +++------ 7 files changed, 11 insertions(+), 20 deletions(-) rename cl/_testrt/{_intgen => intgen}/in.go (100%) rename cl/_testrt/{_intgen => intgen}/out.ll (100%) diff --git a/cl/_testrt/_intgen/in.go b/cl/_testrt/intgen/in.go similarity index 100% rename from cl/_testrt/_intgen/in.go rename to cl/_testrt/intgen/in.go diff --git a/cl/_testrt/_intgen/out.ll b/cl/_testrt/intgen/out.ll similarity index 100% rename from cl/_testrt/_intgen/out.ll rename to cl/_testrt/intgen/out.ll diff --git a/internal/runtime/z_closure.go b/internal/runtime/z_closure.go index 15c70a7c..d47818b3 100644 --- a/internal/runtime/z_closure.go +++ b/internal/runtime/z_closure.go @@ -16,6 +16,7 @@ package runtime +/* import ( "unsafe" ) @@ -40,3 +41,4 @@ func ClosureF(c Closure) unsafe.Pointer { func ClosureData(c Closure) unsafe.Pointer { return c.data } +*/ diff --git a/ssa/cl_test.go b/ssa/cl_test.go index 6fa9eeb0..500bf51b 100644 --- a/ssa/cl_test.go +++ b/ssa/cl_test.go @@ -26,7 +26,7 @@ import ( ) func TestFromTestrt(t *testing.T) { - cltest.FromDir(t, "", "../cl/_testrt", true) + cltest.FromDir(t, "intgen", "../cl/_testrt", true) } func TestFromTestdata(t *testing.T) { diff --git a/ssa/expr.go b/ssa/expr.go index 30217c72..5ee75ef9 100644 --- a/ssa/expr.go +++ b/ssa/expr.go @@ -1029,7 +1029,7 @@ func (b Builder) Call(fn Expr, args ...Expr) (ret Expr) { sig := t.(*types.Signature) ret.Type = prog.retType(sig) default: - panic("todo") + panic("unreachable") } ret.impl = llvm.CreateCall(b.impl, fn.ll, fn.impl, llvmValues(args)) return diff --git a/ssa/package.go b/ssa/package.go index ac464669..406c52c5 100644 --- a/ssa/package.go +++ b/ssa/package.go @@ -114,11 +114,10 @@ type aProgram struct { voidType llvm.Type voidPtrTy llvm.Type - rtClosureTy llvm.Type - rtStringTy llvm.Type - rtIfaceTy llvm.Type - rtSliceTy llvm.Type - rtMapTy llvm.Type + rtStringTy llvm.Type + rtIfaceTy llvm.Type + rtSliceTy llvm.Type + rtMapTy llvm.Type anyTy Type voidTy Type @@ -188,13 +187,6 @@ func (p Program) rtType(name string) Type { return p.Type(p.rtNamed(name)) } -func (p Program) rtClosure() llvm.Type { - if p.rtClosureTy.IsNil() { - p.rtClosureTy = p.rtType("Closure").ll - } - return p.rtClosureTy -} - func (p Program) rtIface() llvm.Type { if p.rtIfaceTy.IsNil() { p.rtIfaceTy = p.rtType("Interface").ll diff --git a/ssa/type.go b/ssa/type.go index 00ef495d..0f31b058 100644 --- a/ssa/type.go +++ b/ssa/type.go @@ -126,11 +126,6 @@ func (p Program) Field(typ Type, i int) Type { } func (p Program) Type(typ types.Type) Type { - /* TODO(xsw): no need? - if sig, ok := typ.(*types.Signature); ok { // should methodToFunc - return p.llvmSignature(sig, true) - } - */ if v := p.typs.At(typ); v != nil { return v.(Type) } @@ -339,7 +334,9 @@ func (p Program) toLLVMFunc(sig *types.Signature, inC, isDecl bool) Type { types.NewField(token.NoPos, nil, "f", (*CFuncPtr)(sig), false), types.NewField(token.NoPos, nil, "data", types.Typ[types.UnsafePointer], false), } - return &aType{p.rtClosure(), types.NewStruct(flds, nil), vkClosure} + t := types.NewStruct(flds, nil) + ll := p.ctx.StructType(p.toLLVMFields(t), false) + return &aType{ll, t, vkClosure} } func (p Program) retType(sig *types.Signature) Type {