1
0
mirror of https://github.com/golang/go synced 2024-10-05 14:11:22 -06:00

[dev.ssa] cmd/compile: make zero-divide panic from div/mod explicit

Added an explicit compare-zero and branch-to-panic for
integer division and mod so that other optimizations will
not be fooled by their implicit panics.

Change-Id: Ibf96f636b541c0088861907c537a6beb4b99fa4c
Reviewed-on: https://go-review.googlesource.com/16450
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
David Chase 2015-10-28 13:55:46 -04:00
parent c24681ae2e
commit 18559e2da7
3 changed files with 18 additions and 2 deletions

View File

@ -858,6 +858,8 @@ var Panicindex *Node
var panicslice *Node var panicslice *Node
var panicdivide *Node
var throwreturn *Node var throwreturn *Node
var growslice *Node var growslice *Node

View File

@ -339,6 +339,7 @@ func compile(fn *Node) {
Deferreturn = Sysfunc("deferreturn") Deferreturn = Sysfunc("deferreturn")
Panicindex = Sysfunc("panicindex") Panicindex = Sysfunc("panicindex")
panicslice = Sysfunc("panicslice") panicslice = Sysfunc("panicslice")
panicdivide = Sysfunc("panicdivide")
throwreturn = Sysfunc("throwreturn") throwreturn = Sysfunc("throwreturn")
growslice = Sysfunc("growslice") growslice = Sysfunc("growslice")
typedmemmove_nostore = Sysfunc("typedmemmove_nostore") typedmemmove_nostore = Sysfunc("typedmemmove_nostore")

View File

@ -1655,9 +1655,22 @@ func (s *state) expr(n *Node) *ssa.Value {
xreal = s.newValue1(ssa.OpCvt64Fto32F, pt, xreal) xreal = s.newValue1(ssa.OpCvt64Fto32F, pt, xreal)
ximag = s.newValue1(ssa.OpCvt64Fto32F, pt, ximag) ximag = s.newValue1(ssa.OpCvt64Fto32F, pt, ximag)
} }
return s.newValue2(ssa.OpComplexMake, n.Type, xreal, ximag) return s.newValue2(ssa.OpComplexMake, n.Type, xreal, ximag)
} }
if n.Type.IsFloat() {
return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
} else {
// do a size-appropriate check for zero
cmp := s.newValue2(s.ssaOp(ONE, n.Type), Types[TBOOL], b, s.zeroVal(n.Type))
s.check(cmp, panicdivide)
return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
}
case OMOD:
a := s.expr(n.Left)
b := s.expr(n.Right)
// do a size-appropriate check for zero
cmp := s.newValue2(s.ssaOp(ONE, n.Type), Types[TBOOL], b, s.zeroVal(n.Type))
s.check(cmp, panicdivide)
return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b) return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
case OADD, OSUB: case OADD, OSUB:
a := s.expr(n.Left) a := s.expr(n.Left)
@ -1670,7 +1683,7 @@ func (s *state) expr(n *Node) *ssa.Value {
s.newValue2(op, pt, s.newValue1(ssa.OpComplexImag, pt, a), s.newValue1(ssa.OpComplexImag, pt, b))) s.newValue2(op, pt, s.newValue1(ssa.OpComplexImag, pt, a), s.newValue1(ssa.OpComplexImag, pt, b)))
} }
return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b) return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
case OAND, OOR, OMOD, OHMUL, OXOR: case OAND, OOR, OHMUL, OXOR:
a := s.expr(n.Left) a := s.expr(n.Left)
b := s.expr(n.Right) b := s.expr(n.Right)
return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b) return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)