MakeClosure, FreeVar; FuncAddCtx; aggregateAlloc
This commit is contained in:
16
ssa/decl.go
16
ssa/decl.go
@@ -130,15 +130,20 @@ type aFunction struct {
|
||||
blks []BasicBlock
|
||||
|
||||
params []Type
|
||||
base int // base = 1 if hasFreeVars; base = 0 otherwise
|
||||
hasVArg bool
|
||||
}
|
||||
|
||||
// Function represents a function or method.
|
||||
type Function = *aFunction
|
||||
|
||||
func newFunction(fn llvm.Value, t Type, pkg Package, prog Program) Function {
|
||||
func newFunction(fn llvm.Value, t Type, pkg Package, prog Program, hasFreeVars bool) Function {
|
||||
params, hasVArg := newParams(t, prog)
|
||||
return &aFunction{Expr{fn, t}, pkg, prog, nil, params, hasVArg}
|
||||
base := 0
|
||||
if hasFreeVars {
|
||||
base = 1
|
||||
}
|
||||
return &aFunction{Expr{fn, t}, pkg, prog, nil, params, base, hasVArg}
|
||||
}
|
||||
|
||||
func newParams(fn Type, prog Program) (params []Type, hasVArg bool) {
|
||||
@@ -158,9 +163,16 @@ func newParams(fn Type, prog Program) (params []Type, hasVArg bool) {
|
||||
|
||||
// Params returns the function's ith parameter.
|
||||
func (p Function) Param(i int) Expr {
|
||||
i += p.base // skip if hasFreeVars
|
||||
return Expr{p.impl.Param(i), p.params[i]}
|
||||
}
|
||||
|
||||
// FreeVar returns the function's ith free variable.
|
||||
func (p Function) FreeVar(b Builder, i int) Expr {
|
||||
ctx := Expr{p.impl.Param(0), p.params[0]}
|
||||
return b.Field(ctx, i)
|
||||
}
|
||||
|
||||
// NewBuilder creates a new Builder for the function.
|
||||
func (p Function) NewBuilder() Builder {
|
||||
prog := p.Prog
|
||||
|
||||
46
ssa/expr.go
46
ssa/expr.go
@@ -366,7 +366,7 @@ func checkExpr(v Expr, t types.Type, b Builder) Expr {
|
||||
return v
|
||||
}
|
||||
|
||||
func llvmValues(vals []Expr, params *types.Tuple, b Builder) (ret []llvm.Value) {
|
||||
func llvmParams(vals []Expr, params *types.Tuple, b Builder) (ret []llvm.Value) {
|
||||
n := params.Len()
|
||||
if n > 0 {
|
||||
ret = make([]llvm.Value, len(vals))
|
||||
@@ -380,6 +380,20 @@ func llvmValues(vals []Expr, params *types.Tuple, b Builder) (ret []llvm.Value)
|
||||
return
|
||||
}
|
||||
|
||||
func llvmFields(vals []Expr, t *types.Struct, b Builder) (ret []llvm.Value) {
|
||||
n := t.NumFields()
|
||||
if n > 0 {
|
||||
ret = make([]llvm.Value, len(vals))
|
||||
for i, v := range vals {
|
||||
if i < n {
|
||||
v = checkExpr(v, t.Field(i).Type(), b)
|
||||
}
|
||||
ret[i] = v.impl
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func llvmDelayValues(f func(i int) Expr, n int) []llvm.Value {
|
||||
ret := make([]llvm.Value, n)
|
||||
for i := 0; i < n; i++ {
|
||||
@@ -479,13 +493,23 @@ func (b Builder) Store(ptr, val Expr) Builder {
|
||||
return b
|
||||
}
|
||||
|
||||
func (b Builder) aggregateAlloc(t Type, flds ...llvm.Value) llvm.Value {
|
||||
prog := b.Prog
|
||||
pkg := b.Func.Pkg
|
||||
size := prog.SizeOf(t)
|
||||
ptr := b.InlineCall(pkg.rtFunc("AllocU"), prog.IntVal(size, prog.Uintptr())).impl
|
||||
tll := t.ll
|
||||
impl := b.impl
|
||||
for i, fld := range flds {
|
||||
impl.CreateStore(fld, llvm.CreateStructGEP(impl, tll, ptr, i))
|
||||
}
|
||||
return ptr
|
||||
}
|
||||
|
||||
// aggregateValue yields the value of the aggregate X with the fields
|
||||
func (b Builder) aggregateValue(t Type, flds ...llvm.Value) Expr {
|
||||
if debugInstr {
|
||||
log.Printf("AggregateValue %v, %v\n", t.RawType(), flds)
|
||||
}
|
||||
impl := b.impl
|
||||
tll := t.ll
|
||||
impl := b.impl
|
||||
ptr := llvm.CreateAlloca(impl, tll)
|
||||
for i, fld := range flds {
|
||||
impl.CreateStore(fld, llvm.CreateStructGEP(impl, tll, ptr, i))
|
||||
@@ -493,7 +517,6 @@ func (b Builder) aggregateValue(t Type, flds ...llvm.Value) Expr {
|
||||
return Expr{llvm.CreateLoad(b.impl, tll, ptr), t}
|
||||
}
|
||||
|
||||
/*
|
||||
// The MakeClosure instruction yields a closure value whose code is
|
||||
// Fn and whose free variables' values are supplied by Bindings.
|
||||
//
|
||||
@@ -507,9 +530,14 @@ func (b Builder) MakeClosure(fn Expr, bindings []Expr) Expr {
|
||||
if debugInstr {
|
||||
log.Printf("MakeClosure %v, %v\n", fn, bindings)
|
||||
}
|
||||
panic("todo")
|
||||
prog := b.Prog
|
||||
tfn := fn.Type
|
||||
sig := tfn.raw.Type.(*types.Signature)
|
||||
tctx := sig.Params().At(0).Type().Underlying().(*types.Struct)
|
||||
flds := llvmFields(bindings, tctx, b)
|
||||
data := b.aggregateAlloc(prog.rawType(tctx), flds...)
|
||||
return b.aggregateValue(prog.Closure(tfn), fn.impl, data)
|
||||
}
|
||||
*/
|
||||
|
||||
// The FieldAddr instruction yields the address of Field of *struct X.
|
||||
//
|
||||
@@ -1069,7 +1097,7 @@ func (b Builder) Call(fn Expr, args ...Expr) (ret Expr) {
|
||||
panic("unreachable")
|
||||
}
|
||||
ret.Type = prog.retType(sig)
|
||||
ret.impl = llvm.CreateCall(b.impl, ll, fn.impl, llvmValues(args, sig.Params(), b))
|
||||
ret.impl = llvm.CreateCall(b.impl, ll, fn.impl, llvmParams(args, sig.Params(), b))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -340,6 +340,11 @@ func (p Package) VarOf(name string) Global {
|
||||
|
||||
// NewFunc creates a new function.
|
||||
func (p Package) NewFunc(name string, sig *types.Signature, bg Background) Function {
|
||||
return p.NewFuncEx(name, sig, bg, false)
|
||||
}
|
||||
|
||||
// NewFuncEx creates a new function.
|
||||
func (p Package) NewFuncEx(name string, sig *types.Signature, bg Background, hasCtx bool) Function {
|
||||
if v, ok := p.fns[name]; ok {
|
||||
return v
|
||||
}
|
||||
@@ -348,7 +353,7 @@ func (p Package) NewFunc(name string, sig *types.Signature, bg Background) Funct
|
||||
log.Println("NewFunc", name, t.raw.Type)
|
||||
}
|
||||
fn := llvm.AddFunction(p.mod, name, t.ll)
|
||||
ret := newFunction(fn, t, p, p.Prog)
|
||||
ret := newFunction(fn, t, p, p.Prog, hasCtx)
|
||||
p.fns[name] = ret
|
||||
return ret
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ func (b Builder) Return(results ...Expr) {
|
||||
b.impl.CreateRet(results[0].impl)
|
||||
default:
|
||||
tret := b.Func.raw.Type.(*types.Signature).Results()
|
||||
b.impl.CreateAggregateRet(llvmValues(results, tret, b))
|
||||
b.impl.CreateAggregateRet(llvmParams(results, tret, b))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -59,6 +59,13 @@ func (p Program) FuncDecl(sig *types.Signature, bg Background) Type {
|
||||
return &aType{p.toLLVMFunc(sig), rawType{sig}, vkFuncDecl}
|
||||
}
|
||||
|
||||
// Closure creates a closture type for a function.
|
||||
func (p Program) Closure(fn Type) Type {
|
||||
sig := fn.raw.Type.(*types.Signature)
|
||||
closure := p.gocvt.cvtClosure(sig)
|
||||
return p.rawType(closure)
|
||||
}
|
||||
|
||||
func (p goTypes) cvtType(typ types.Type) (raw types.Type, cvt bool) {
|
||||
switch t := typ.(type) {
|
||||
case *types.Basic:
|
||||
@@ -239,17 +246,22 @@ func (p goTypes) cvtStruct(typ *types.Struct) (raw *types.Struct, cvt bool) {
|
||||
// convert method to func
|
||||
func methodToFunc(sig *types.Signature) *types.Signature {
|
||||
if recv := sig.Recv(); recv != nil {
|
||||
tParams := sig.Params()
|
||||
nParams := tParams.Len()
|
||||
params := make([]*types.Var, nParams+1)
|
||||
params[0] = recv
|
||||
for i := 0; i < nParams; i++ {
|
||||
params[i+1] = tParams.At(i)
|
||||
}
|
||||
return types.NewSignatureType(
|
||||
nil, nil, nil, types.NewTuple(params...), sig.Results(), sig.Variadic())
|
||||
return FuncAddCtx(recv, sig)
|
||||
}
|
||||
return sig
|
||||
}
|
||||
|
||||
// FuncAddCtx adds a ctx to a function signature.
|
||||
func FuncAddCtx(ctx *types.Var, sig *types.Signature) *types.Signature {
|
||||
tParams := sig.Params()
|
||||
nParams := tParams.Len()
|
||||
params := make([]*types.Var, nParams+1)
|
||||
params[0] = ctx
|
||||
for i := 0; i < nParams; i++ {
|
||||
params[i+1] = tParams.At(i)
|
||||
}
|
||||
return types.NewSignatureType(
|
||||
nil, nil, nil, types.NewTuple(params...), sig.Results(), sig.Variadic())
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user