tinygo/transform/panic.go
Ayke van Laethem 172efc26a7 compiler: move ReplacePanicsWithTrap pass to transforms
This moves the transformation pass to the right location, and adds tests
to see that it actually works correctly.
2019-11-16 18:41:28 +01:00

33 строки
912 Б
Go

package transform
import (
"tinygo.org/x/go-llvm"
)
// ReplacePanicsWithTrap replaces each call to panic (or similar functions) with
// calls to llvm.trap, to reduce code size. This is the -panic=trap command-line
// option.
func ReplacePanicsWithTrap(mod llvm.Module) {
ctx := mod.Context()
builder := ctx.NewBuilder()
defer builder.Dispose()
trap := mod.NamedFunction("llvm.trap")
if trap.IsNil() {
trapType := llvm.FunctionType(ctx.VoidType(), nil, false)
trap = llvm.AddFunction(mod, "llvm.trap", trapType)
}
for _, name := range []string{"runtime._panic", "runtime.runtimePanic"} {
fn := mod.NamedFunction(name)
if fn.IsNil() {
continue
}
for _, use := range getUses(fn) {
if use.IsACallInst().IsNil() || use.CalledValue() != fn {
panic("expected use of a panic function to be a call")
}
builder.SetInsertPointBefore(use)
builder.CreateCall(trap, nil, "")
}
}
}