MakeClosure, FreeVar; FuncAddCtx; aggregateAlloc

This commit is contained in:
xushiwei
2024-05-05 17:39:17 +08:00
parent 3c33a1d05e
commit d7df46d578
9 changed files with 175 additions and 46 deletions

View File

@@ -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