This commit is contained in:
xushiwei
2024-04-17 00:01:42 +08:00
parent 70ec2350eb
commit 2596f1d5b2
4 changed files with 29 additions and 1 deletions

View File

@@ -16,6 +16,10 @@
package ssa package ssa
import (
"github.com/goplus/llvm"
)
// Function represents the parameters, results, and code of a function // Function represents the parameters, results, and code of a function
// or method. // or method.
// //
@@ -66,4 +70,5 @@ package ssa
// the generic method. TypeArgs() refers to [string,U] or [string,int], // the generic method. TypeArgs() refers to [string,U] or [string,int],
// respectively, and is nil in the generic method. // respectively, and is nil in the generic method.
type Function struct { type Function struct {
impl llvm.Value
} }

View File

@@ -81,7 +81,8 @@ func (p *Package) NewVar(name string, typ types.Type) *Global {
} }
func (p *Package) NewFunc(name string, sig *types.Signature) *Function { func (p *Package) NewFunc(name string, sig *types.Signature) *Function {
return &Function{} fn := llvm.AddFunction(p.mod, name, p.prog.llvmSignature(sig))
return &Function{fn}
} }
func (p *Package) String() string { func (p *Package) String() string {

View File

@@ -78,3 +78,16 @@ source_filename = "foo/bar"
@a = external global %Empty @a = external global %Empty
`) `)
} }
func TestFunc(t *testing.T) {
prog := NewProgram(nil)
pkg := prog.NewPackage("bar", "foo/bar")
params := types.NewTuple(types.NewVar(0, nil, "a", types.Typ[types.Int]))
sig := types.NewSignatureType(nil, nil, nil, params, nil, false)
pkg.NewFunc("fn", sig)
assertPkg(t, pkg, `; ModuleID = 'foo/bar'
source_filename = "foo/bar"
declare void @fn(i64)
`)
}

View File

@@ -38,6 +38,15 @@ func (p *Program) llvmType(typ types.Type) llvm.Type {
return ret return ret
} }
func (p *Program) llvmSignature(sig *types.Signature) llvm.Type {
if v := p.typs.At(sig); v != nil {
return v.(llvm.Type)
}
ret := p.toLLVMFunc(sig)
p.typs.Set(sig, ret)
return ret
}
func (p *Program) tyVoidPtr() llvm.Type { func (p *Program) tyVoidPtr() llvm.Type {
if p.voidPtrTy.IsNil() { if p.voidPtrTy.IsNil() {
p.voidPtrTy = llvm.PointerType(p.tyVoid(), 0) p.voidPtrTy = llvm.PointerType(p.tyVoid(), 0)