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 May 31, 2022
1 parent 441035e commit 6775814
Show file tree
Hide file tree
Showing 31 changed files with 827 additions and 150 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,14 @@ TEST_PACKAGES_LINUX := \
archive/zip \
compress/flate \
compress/lzw \
crypto/hmac \
debug/dwarf \
debug/plan9obj \
io/fs \
io/ioutil \
testing/fstest
strconv \
testing/fstest \
text/template/parse

TEST_PACKAGES_DARWIN := $(TEST_PACKAGES_LINUX)

Expand Down
2 changes: 1 addition & 1 deletion compileopts/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,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,17 +33,36 @@ 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
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 @@ -55,6 +74,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
29 changes: 26 additions & 3 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,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
initInlinedAt llvm.Metadata // fake inlinedAt position
Expand Down Expand Up @@ -1193,6 +1195,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 @@ -1320,9 +1328,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 @@ -1511,7 +1522,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 @@ -1598,6 +1615,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 @@ -1668,7 +1691,7 @@ func (b *builder) createFunctionCall(instr *ssa.CallCommon) (llvm.Value, error)
params = append(params, context)
}

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
1 change: 1 addition & 0 deletions compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func TestCompiler(t *testing.T) {
}
compilerConfig := &Config{
Triple: config.Triple(),
Features: config.Features(),
GOOS: config.GOOS(),
GOARCH: config.GOARCH(),
CodeModel: config.CodeModel(),
Expand Down
Loading

0 comments on commit 6775814

Please sign in to comment.