Skip to content

Commit

Permalink
compiler: implement recover() built-in function
Browse files Browse the repository at this point in the history
  • Loading branch information
aykevl committed Dec 9, 2021
1 parent e71eed9 commit cf5d5f8
Show file tree
Hide file tree
Showing 30 changed files with 591 additions and 49 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ TEST_PACKAGES = \
crypto/des \
crypto/dsa \
crypto/elliptic/internal/fiat \
crypto/hmac \
crypto/internal/subtle \
crypto/md5 \
crypto/rc4 \
Expand Down Expand Up @@ -234,9 +235,11 @@ TEST_PACKAGES = \
os \
path \
reflect \
strconv \
testing \
testing/iotest \
text/scanner \
text/template/parse \
unicode \
unicode/utf16 \
unicode/utf8 \
Expand Down
2 changes: 1 addition & 1 deletion compileopts/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) {
// systems so we need separate assembly files.
suffix = "_windows"
}
spec.ExtraFiles = append(spec.ExtraFiles, "src/runtime/gc_"+goarch+suffix+".S")
spec.ExtraFiles = append(spec.ExtraFiles, "src/runtime/asm_"+goarch+suffix+".S")
spec.ExtraFiles = append(spec.ExtraFiles, "src/internal/task/task_stack_"+goarch+suffix+".S")
}
if goarch != runtime.GOARCH {
Expand Down
32 changes: 30 additions & 2 deletions compiler/calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,37 @@ const (
paramIsDeferenceableOrNull = 1 << iota
)

// createCall creates a new call to runtime.<fnName> with the given arguments.
func (b *builder) createRuntimeCall(fnName string, args []llvm.Value, name string) llvm.Value {
// createRuntimeCallCommon creates a runtime call. Use createRuntimeCall or
// createRuntimeInvoke instead.
func (b *builder) createRuntimeCallCommon(fnName string, args []llvm.Value, name string, isInvoke bool) llvm.Value {
fn := b.program.ImportedPackage("runtime").Members[fnName].(*ssa.Function)
llvmFn := b.getFunction(fn)
if llvmFn.IsNil() {
panic("trying to call non-existent function: " + fn.RelString(nil))
}
args = append(args, llvm.Undef(b.i8ptrType)) // unused context parameter
args = append(args, llvm.ConstPointerNull(b.i8ptrType)) // coroutine handle
if isInvoke {
return b.createInvoke(llvmFn, args, name)
}
return b.createCall(llvmFn, args, name)
}

// createRuntimeCall creates a new call to runtime.<fnName> with the given
// arguments.
func (b *builder) createRuntimeCall(fnName string, args []llvm.Value, name string) llvm.Value {
return b.createRuntimeCallCommon(fnName, args, name, false)
}

// createRuntimeInvoke creates a new call to runtime.<fnName> with the given
// arguments. If the runtime call panics, control flow is diverted to the
// landing pad block.
// Note that "invoke" here is meant in the LLVM sense (a call that can
// panic/throw), not in the Go sense (an interface method call).
func (b *builder) createRuntimeInvoke(fnName string, args []llvm.Value, name string) llvm.Value {
return b.createRuntimeCallCommon(fnName, args, name, true)
}

// createCall creates a call to the given function with the arguments possibly
// expanded.
func (b *builder) createCall(fn llvm.Value, args []llvm.Value, name string) llvm.Value {
Expand All @@ -56,6 +75,15 @@ func (b *builder) createCall(fn llvm.Value, args []llvm.Value, name string) llvm
return b.CreateCall(fn, expanded, name)
}

// createInvoke is like createCall but continues execution at the landing pad if
// the call resulted in a panic.
func (b *builder) createInvoke(fn llvm.Value, args []llvm.Value, name string) llvm.Value {
if b.hasDeferFrame() {
b.createInvokeCheckpoint()
}
return b.createCall(fn, args, name)
}

// Expand an argument type to a list that can be used in a function call
// parameter list.
func (c *compilerContext) expandFormalParamType(t llvm.Type, name string, goType types.Type) []paramInfo {
Expand Down
31 changes: 27 additions & 4 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
// Version of the compiler pacakge. Must be incremented each time the compiler
// package changes in a way that affects the generated LLVM module.
// This version is independent of the TinyGo version number.
const Version = 25 // last change: add "target-cpu" and "target-features" attributes
const Version = 26 // last change: implement recover

func init() {
llvm.InitializeAllTargets()
Expand Down Expand Up @@ -142,6 +142,8 @@ type builder struct {
currentBlock *ssa.BasicBlock
phis []phiNode
deferPtr llvm.Value
deferFrame llvm.Value
landingpad llvm.BasicBlock
difunc llvm.Metadata
dilocals map[*types.Var]llvm.Metadata
allDeferFuncs []interface{}
Expand Down Expand Up @@ -985,6 +987,12 @@ func (b *builder) createFunction() {
}
}

if b.hasDeferFrame() {
// Create the landing pad block, where execution continues after a
// panic.
b.createLandingPad()
}

// Resolve phi nodes
for _, phi := range b.phis {
block := phi.ssa.Block()
Expand Down Expand Up @@ -1113,9 +1121,12 @@ func (b *builder) createInstruction(instr ssa.Instruction) {
b.createMapUpdate(mapType.Key(), m, key, value, instr.Pos())
case *ssa.Panic:
value := b.getValue(instr.X)
b.createRuntimeCall("_panic", []llvm.Value{value}, "")
b.createRuntimeInvoke("_panic", []llvm.Value{value}, "")
b.CreateUnreachable()
case *ssa.Return:
if b.hasDeferFrame() {
b.createRuntimeCall("destroyDeferFrame", []llvm.Value{b.deferFrame}, "")
}
if len(instr.Results) == 0 {
b.CreateRetVoid()
} else if len(instr.Results) == 1 {
Expand Down Expand Up @@ -1304,7 +1315,13 @@ func (b *builder) createBuiltin(argTypes []types.Type, argValues []llvm.Value, c
cplx := argValues[0]
return b.CreateExtractValue(cplx, 0, "real"), nil
case "recover":
return b.createRuntimeCall("_recover", nil, ""), nil
useParentFrame := uint64(0)
if b.hasDeferFrame() {
// recover() should return the panic value of the parent function,
// not of the current function.
useParentFrame = 1
}
return b.createRuntimeCall("_recover", []llvm.Value{llvm.ConstInt(b.ctx.Int1Type(), useParentFrame, false)}, ""), nil
case "ssa:wrapnilchk":
// TODO: do an actual nil check?
return argValues[0], nil
Expand Down Expand Up @@ -1391,6 +1408,12 @@ func (b *builder) createFunctionCall(instr *ssa.CallCommon) (llvm.Value, error)
return b.createVolatileLoad(instr)
case strings.HasPrefix(name, "runtime/volatile.Store"):
return b.createVolatileStore(instr)
case name == "runtime.supportsRecover":
supportsRecover := uint64(0)
if b.supportsRecover() {
supportsRecover = 1
}
return llvm.ConstInt(b.ctx.Int1Type(), supportsRecover, false), nil
case strings.HasPrefix(name, "sync/atomic."):
val, ok := b.createAtomicOp(instr)
if ok {
Expand Down Expand Up @@ -1456,7 +1479,7 @@ func (b *builder) createFunctionCall(instr *ssa.CallCommon) (llvm.Value, error)
params = append(params, llvm.Undef(b.i8ptrType))
}

return b.createCall(callee, params, ""), nil
return b.createInvoke(callee, params, ""), nil
}

// getValue returns the LLVM value of a constant, function value, global, or
Expand Down
Loading

0 comments on commit cf5d5f8

Please sign in to comment.