compiler: implement spec-compliant shifts

Previously, the compiler used LLVM's shift instructions directly, which have UB whenever the shifts are large or negative.
This commit adds runtime checks for negative shifts, and handles oversized shifts.
This commit is contained in:
Jaden Weiss
2020-03-28 12:35:19 -04:00
committed by GitHub
parent 91d1a23b14
commit 5cc130bb6e
6 changed files with 99 additions and 15 deletions
+29
View File
@@ -61,6 +61,15 @@ func main() {
println(c128 != 3+2i)
println(c128 != 4+2i)
println(c128 != 3+3i)
println("shifts")
println(shlSimple == 4)
println(shlOverflow == 0)
println(shrSimple == 1)
println(shrOverflow == 0)
println(ashrNeg == -1)
println(ashrOverflow == 0)
println(ashrNegOverflow == -1)
}
var x = true
@@ -87,3 +96,23 @@ type Struct2 struct {
_ float64
i int
}
func shl(x uint, y uint) uint {
return x << y
}
func shr(x uint, y uint) uint {
return x >> y
}
func ashr(x int, y uint) int {
return x >> y
}
var shlSimple = shl(2, 1)
var shlOverflow = shl(2, 1000)
var shrSimple = shr(2, 1)
var shrOverflow = shr(2, 1000000)
var ashrNeg = ashr(-1, 1)
var ashrOverflow = ashr(1, 1000000)
var ashrNegOverflow = ashr(-1, 1000000)
+8
View File
@@ -54,3 +54,11 @@ false
true
true
true
shifts
true
true
true
true
true
true
true