Commit 2acd3fa8 authored by Martin Möhrmann's avatar Martin Möhrmann

cmd/compile: reduce switch cases in evconst

Reduces the number of cases that need to be tested and
reduces size of the evconst function by 101 bytes.

Change-Id: Ie56055a89d0dadd311fb940b51c488fc003694b9
Reviewed-on: https://go-review.googlesource.com/39950
Run-TryBot: Martin Möhrmann <moehrmann@google.com>
Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
parent 3959e079
...@@ -705,6 +705,7 @@ func evconst(n *Node) { ...@@ -705,6 +705,7 @@ func evconst(n *Node) {
var rv Val var rv Val
var lno src.XPos var lno src.XPos
var wr types.EType var wr types.EType
var ctype uint32
var v Val var v Val
var norig *Node var norig *Node
var nn *Node var nn *Node
...@@ -717,7 +718,13 @@ func evconst(n *Node) { ...@@ -717,7 +718,13 @@ func evconst(n *Node) {
v = copyval(v) v = copyval(v)
} }
switch uint32(n.Op)<<16 | uint32(v.Ctype()) { // rune values are int values for the purpose of constant folding.
ctype = uint32(v.Ctype())
if ctype == CTRUNE_ {
ctype = CTINT_
}
switch uint32(n.Op)<<16 | ctype {
default: default:
if !n.Diag() { if !n.Diag() {
yyerror("illegal constant expression %v %v", n.Op, nl.Type) yyerror("illegal constant expression %v %v", n.Op, nl.Type)
...@@ -734,7 +741,6 @@ func evconst(n *Node) { ...@@ -734,7 +741,6 @@ func evconst(n *Node) {
} }
fallthrough fallthrough
case OCONV_ | CTINT_, case OCONV_ | CTINT_,
OCONV_ | CTRUNE_,
OCONV_ | CTFLT_, OCONV_ | CTFLT_,
OCONV_ | CTCPLX_, OCONV_ | CTCPLX_,
OCONV_ | CTSTR_, OCONV_ | CTSTR_,
...@@ -742,16 +748,13 @@ func evconst(n *Node) { ...@@ -742,16 +748,13 @@ func evconst(n *Node) {
nl = convlit1(nl, n.Type, true, false) nl = convlit1(nl, n.Type, true, false)
v = nl.Val() v = nl.Val()
case OPLUS_ | CTINT_, case OPLUS_ | CTINT_:
OPLUS_ | CTRUNE_:
break break
case OMINUS_ | CTINT_, case OMINUS_ | CTINT_:
OMINUS_ | CTRUNE_:
v.U.(*Mpint).Neg() v.U.(*Mpint).Neg()
case OCOM_ | CTINT_, case OCOM_ | CTINT_:
OCOM_ | CTRUNE_:
var et types.EType = Txxx var et types.EType = Txxx
if nl.Type != nil { if nl.Type != nil {
et = nl.Type.Etype et = nl.Type.Etype
...@@ -899,25 +902,27 @@ func evconst(n *Node) { ...@@ -899,25 +902,27 @@ func evconst(n *Node) {
Fatalf("constant type mismatch %v(%d) %v(%d)", nl.Type, v.Ctype(), nr.Type, rv.Ctype()) Fatalf("constant type mismatch %v(%d) %v(%d)", nl.Type, v.Ctype(), nr.Type, rv.Ctype())
} }
// rune values are int values for the purpose of constant folding.
ctype = uint32(v.Ctype())
if ctype == CTRUNE_ {
ctype = CTINT_
}
// run op // run op
switch uint32(n.Op)<<16 | uint32(v.Ctype()) { switch uint32(n.Op)<<16 | ctype {
default: default:
goto illegal goto illegal
case OADD_ | CTINT_, case OADD_ | CTINT_:
OADD_ | CTRUNE_:
v.U.(*Mpint).Add(rv.U.(*Mpint)) v.U.(*Mpint).Add(rv.U.(*Mpint))
case OSUB_ | CTINT_, case OSUB_ | CTINT_:
OSUB_ | CTRUNE_:
v.U.(*Mpint).Sub(rv.U.(*Mpint)) v.U.(*Mpint).Sub(rv.U.(*Mpint))
case OMUL_ | CTINT_, case OMUL_ | CTINT_:
OMUL_ | CTRUNE_:
v.U.(*Mpint).Mul(rv.U.(*Mpint)) v.U.(*Mpint).Mul(rv.U.(*Mpint))
case ODIV_ | CTINT_, case ODIV_ | CTINT_:
ODIV_ | CTRUNE_:
if rv.U.(*Mpint).CmpInt64(0) == 0 { if rv.U.(*Mpint).CmpInt64(0) == 0 {
yyerror("division by zero") yyerror("division by zero")
v.U.(*Mpint).SetOverflow() v.U.(*Mpint).SetOverflow()
...@@ -926,8 +931,7 @@ func evconst(n *Node) { ...@@ -926,8 +931,7 @@ func evconst(n *Node) {
v.U.(*Mpint).Quo(rv.U.(*Mpint)) v.U.(*Mpint).Quo(rv.U.(*Mpint))
case OMOD_ | CTINT_, case OMOD_ | CTINT_:
OMOD_ | CTRUNE_:
if rv.U.(*Mpint).CmpInt64(0) == 0 { if rv.U.(*Mpint).CmpInt64(0) == 0 {
yyerror("division by zero") yyerror("division by zero")
v.U.(*Mpint).SetOverflow() v.U.(*Mpint).SetOverflow()
...@@ -936,28 +940,22 @@ func evconst(n *Node) { ...@@ -936,28 +940,22 @@ func evconst(n *Node) {
v.U.(*Mpint).Rem(rv.U.(*Mpint)) v.U.(*Mpint).Rem(rv.U.(*Mpint))
case OLSH_ | CTINT_, case OLSH_ | CTINT_:
OLSH_ | CTRUNE_:
v.U.(*Mpint).Lsh(rv.U.(*Mpint)) v.U.(*Mpint).Lsh(rv.U.(*Mpint))
case ORSH_ | CTINT_, case ORSH_ | CTINT_:
ORSH_ | CTRUNE_:
v.U.(*Mpint).Rsh(rv.U.(*Mpint)) v.U.(*Mpint).Rsh(rv.U.(*Mpint))
case OOR_ | CTINT_, case OOR_ | CTINT_:
OOR_ | CTRUNE_:
v.U.(*Mpint).Or(rv.U.(*Mpint)) v.U.(*Mpint).Or(rv.U.(*Mpint))
case OAND_ | CTINT_, case OAND_ | CTINT_:
OAND_ | CTRUNE_:
v.U.(*Mpint).And(rv.U.(*Mpint)) v.U.(*Mpint).And(rv.U.(*Mpint))
case OANDNOT_ | CTINT_, case OANDNOT_ | CTINT_:
OANDNOT_ | CTRUNE_:
v.U.(*Mpint).AndNot(rv.U.(*Mpint)) v.U.(*Mpint).AndNot(rv.U.(*Mpint))
case OXOR_ | CTINT_, case OXOR_ | CTINT_:
OXOR_ | CTRUNE_:
v.U.(*Mpint).Xor(rv.U.(*Mpint)) v.U.(*Mpint).Xor(rv.U.(*Mpint))
case OADD_ | CTFLT_: case OADD_ | CTFLT_:
...@@ -1015,43 +1013,37 @@ func evconst(n *Node) { ...@@ -1015,43 +1013,37 @@ func evconst(n *Node) {
case ONE_ | CTNIL_: case ONE_ | CTNIL_:
goto setfalse goto setfalse
case OEQ_ | CTINT_, case OEQ_ | CTINT_:
OEQ_ | CTRUNE_:
if v.U.(*Mpint).Cmp(rv.U.(*Mpint)) == 0 { if v.U.(*Mpint).Cmp(rv.U.(*Mpint)) == 0 {
goto settrue goto settrue
} }
goto setfalse goto setfalse
case ONE_ | CTINT_, case ONE_ | CTINT_:
ONE_ | CTRUNE_:
if v.U.(*Mpint).Cmp(rv.U.(*Mpint)) != 0 { if v.U.(*Mpint).Cmp(rv.U.(*Mpint)) != 0 {
goto settrue goto settrue
} }
goto setfalse goto setfalse
case OLT_ | CTINT_, case OLT_ | CTINT_:
OLT_ | CTRUNE_:
if v.U.(*Mpint).Cmp(rv.U.(*Mpint)) < 0 { if v.U.(*Mpint).Cmp(rv.U.(*Mpint)) < 0 {
goto settrue goto settrue
} }
goto setfalse goto setfalse
case OLE_ | CTINT_, case OLE_ | CTINT_:
OLE_ | CTRUNE_:
if v.U.(*Mpint).Cmp(rv.U.(*Mpint)) <= 0 { if v.U.(*Mpint).Cmp(rv.U.(*Mpint)) <= 0 {
goto settrue goto settrue
} }
goto setfalse goto setfalse
case OGE_ | CTINT_, case OGE_ | CTINT_:
OGE_ | CTRUNE_:
if v.U.(*Mpint).Cmp(rv.U.(*Mpint)) >= 0 { if v.U.(*Mpint).Cmp(rv.U.(*Mpint)) >= 0 {
goto settrue goto settrue
} }
goto setfalse goto setfalse
case OGT_ | CTINT_, case OGT_ | CTINT_:
OGT_ | CTRUNE_:
if v.U.(*Mpint).Cmp(rv.U.(*Mpint)) > 0 { if v.U.(*Mpint).Cmp(rv.U.(*Mpint)) > 0 {
goto settrue goto settrue
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment