mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-04 11:07:46 +00:00
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.
This commit is contained in:
committed by
Ron Evans
parent
f99c600ad8
commit
86f1e6aec4
Vendored
+12
@@ -75,6 +75,10 @@ func main() {
|
||||
println("constant number")
|
||||
x := uint32(5)
|
||||
println(uint32(x) / (20e0 / 1))
|
||||
|
||||
// check for signed integer overflow
|
||||
println("-2147483648 / -1:", sdiv32(-2147483648, -1))
|
||||
println("-2147483648 % -1:", srem32(-2147483648, -1))
|
||||
}
|
||||
|
||||
var x = true
|
||||
@@ -114,6 +118,14 @@ func ashr(x int, y uint) int {
|
||||
return x >> y
|
||||
}
|
||||
|
||||
func sdiv32(x, y int32) int32 {
|
||||
return x / y
|
||||
}
|
||||
|
||||
func srem32(x, y int32) int32 {
|
||||
return x % y
|
||||
}
|
||||
|
||||
var shlSimple = shl(2, 1)
|
||||
var shlOverflow = shl(2, 1000)
|
||||
var shrSimple = shr(2, 1)
|
||||
|
||||
Vendored
+2
@@ -64,3 +64,5 @@ true
|
||||
true
|
||||
constant number
|
||||
0
|
||||
-2147483648 / -1: -2147483648
|
||||
-2147483648 % -1: 0
|
||||
|
||||
Reference in New Issue
Block a user