Files
tinygo/src/runtime/panic.go
T
Ayke van Laethem 86f1e6aec4 compiler: properly implement div and rem operations
The division and remainder operations were lowered directly to LLVM IR.
This is wrong however because the Go specification defines exactly what
happens on a divide by zero or signed integer overflow and LLVM IR
itself treats those cases as undefined behavior. Therefore, this commit
implements divide by zero and signed integer overflow according to the
Go specification.

This does have an impact on the generated code, but it is surprisingly
small. I've used the drivers repo to test the code before and after, and
to my surprise most driver smoke tests are not changed at all. Those
that are, have only a small increase in code size. At the same time,
this change makes TinyGo more compliant to the Go specification.
2021-10-28 15:55:02 +02:00

75 lines
1.9 KiB
Go

package runtime
// trap is a compiler hint that this function cannot be executed. It is
// translated into either a trap instruction or a call to abort().
//export llvm.trap
func trap()
// Builtin function panic(msg), used as a compiler intrinsic.
func _panic(message interface{}) {
printstring("panic: ")
printitf(message)
printnl()
abort()
}
// Cause a runtime panic, which is (currently) always a string.
func runtimePanic(msg string) {
printstring("panic: runtime error: ")
println(msg)
abort()
}
// Try to recover a panicking goroutine.
func _recover() interface{} {
// Deferred functions are currently not executed during panic, so there is
// no way this can return anything besides nil.
return nil
}
// Panic when trying to dereference a nil pointer.
func nilPanic() {
runtimePanic("nil pointer dereference")
}
// Panic when trying to acces an array or slice out of bounds.
func lookupPanic() {
runtimePanic("index out of range")
}
// Panic when trying to slice a slice out of bounds.
func slicePanic() {
runtimePanic("slice out of range")
}
// Panic when trying to convert a slice to an array pointer (Go 1.17+) and the
// slice is shorter than the array.
func sliceToArrayPointerPanic() {
runtimePanic("slice smaller than array")
}
// Panic when calling unsafe.Slice() (Go 1.17+) with a len that's too large
// (which includes if the ptr is nil and len is nonzero).
func unsafeSlicePanic() {
runtimePanic("unsafe.Slice: len out of range")
}
// Panic when trying to create a new channel that is too big.
func chanMakePanic() {
runtimePanic("new channel is too big")
}
// Panic when a shift value is negative.
func negativeShiftPanic() {
runtimePanic("negative shift")
}
// Panic when there is a divide by zero.
func divideByZeroPanic() {
runtimePanic("divide by zero")
}
func blockingPanic() {
runtimePanic("trying to do blocking operation in exported function")
}