mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-04 02:57:46 +00:00
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:
Vendored
+29
@@ -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)
|
||||
|
||||
Vendored
+8
@@ -54,3 +54,11 @@ false
|
||||
true
|
||||
true
|
||||
true
|
||||
shifts
|
||||
true
|
||||
true
|
||||
true
|
||||
true
|
||||
true
|
||||
true
|
||||
true
|
||||
|
||||
Reference in New Issue
Block a user