From 3e476827a68a64168f345ae96d2fa1bb80eb6bf4 Mon Sep 17 00:00:00 2001 From: Dave Cheney Date: Fri, 18 Sep 2015 18:56:47 +1000 Subject: [PATCH] cmd/compile/internal/gc: move intLiteral to gc.Node MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit intLiteral is used by the gins wrappers in arm64, ppc64 and mips64. Refactor the function to a method on gc.Node and update the callers to use the common copy. Change-Id: I2db90d801a9cb18f8526eb921e13daa75ca1cf6f Reviewed-on: https://go-review.googlesource.com/14744 Reviewed-by: Aram Hăvărneanu Reviewed-by: Dave Cheney Run-TryBot: Dave Cheney TryBot-Result: Gobot Gobot --- src/cmd/compile/internal/arm64/gsubr.go | 16 ++-------------- src/cmd/compile/internal/gc/const.go | 13 +++++++++++++ src/cmd/compile/internal/mips64/gsubr.go | 16 ++-------------- src/cmd/compile/internal/ppc64/gsubr.go | 16 ++-------------- 4 files changed, 19 insertions(+), 42 deletions(-) diff --git a/src/cmd/compile/internal/arm64/gsubr.go b/src/cmd/compile/internal/arm64/gsubr.go index 11214789f5..50ff29bf8f 100644 --- a/src/cmd/compile/internal/arm64/gsubr.go +++ b/src/cmd/compile/internal/arm64/gsubr.go @@ -467,30 +467,18 @@ hard: return } -func intLiteral(n *gc.Node) (x int64, ok bool) { - switch { - case n == nil: - return - case gc.Isconst(n, gc.CTINT): - return n.Int(), true - case gc.Isconst(n, gc.CTBOOL): - return int64(obj.Bool2int(n.Bool())), true - } - return -} - // gins is called by the front end. // It synthesizes some multiple-instruction sequences // so the front end can stay simpler. func gins(as int, f, t *gc.Node) *obj.Prog { if as >= obj.A_ARCHSPECIFIC { - if x, ok := intLiteral(f); ok { + if x, ok := f.IntLiteral(); ok { ginscon(as, x, t) return nil // caller must not use } } if as == arm64.ACMP { - if x, ok := intLiteral(t); ok { + if x, ok := t.IntLiteral(); ok { ginscon2(as, f, x) return nil // caller must not use } diff --git a/src/cmd/compile/internal/gc/const.go b/src/cmd/compile/internal/gc/const.go index e7559c206e..71b582b739 100644 --- a/src/cmd/compile/internal/gc/const.go +++ b/src/cmd/compile/internal/gc/const.go @@ -10,6 +10,19 @@ import ( "strings" ) +// IntLiteral returns the Node's literal value as an interger. +func (n *Node) IntLiteral() (x int64, ok bool) { + switch { + case n == nil: + return + case Isconst(n, CTINT): + return n.Int(), true + case Isconst(n, CTBOOL): + return int64(obj.Bool2int(n.Bool())), true + } + return +} + // Int returns n as an int. // n must be an integer constant. func (n *Node) Int() int64 { diff --git a/src/cmd/compile/internal/mips64/gsubr.go b/src/cmd/compile/internal/mips64/gsubr.go index 4ef928c826..dde05c4a51 100644 --- a/src/cmd/compile/internal/mips64/gsubr.go +++ b/src/cmd/compile/internal/mips64/gsubr.go @@ -545,30 +545,18 @@ hard: return } -func intLiteral(n *gc.Node) (x int64, ok bool) { - switch { - case n == nil: - return - case gc.Isconst(n, gc.CTINT): - return n.Int(), true - case gc.Isconst(n, gc.CTBOOL): - return int64(obj.Bool2int(n.Bool())), true - } - return -} - // gins is called by the front end. // It synthesizes some multiple-instruction sequences // so the front end can stay simpler. func gins(as int, f, t *gc.Node) *obj.Prog { if as >= obj.A_ARCHSPECIFIC { - if x, ok := intLiteral(f); ok { + if x, ok := f.IntLiteral(); ok { ginscon(as, x, t) return nil // caller must not use } } if as == ppc64.ACMP || as == ppc64.ACMPU { - if x, ok := intLiteral(t); ok { + if x, ok := t.IntLiteral(); ok { ginscon2(as, f, x) return nil // caller must not use } diff --git a/src/cmd/compile/internal/ppc64/gsubr.go b/src/cmd/compile/internal/ppc64/gsubr.go index 4ef928c826..dde05c4a51 100644 --- a/src/cmd/compile/internal/ppc64/gsubr.go +++ b/src/cmd/compile/internal/ppc64/gsubr.go @@ -545,30 +545,18 @@ hard: return } -func intLiteral(n *gc.Node) (x int64, ok bool) { - switch { - case n == nil: - return - case gc.Isconst(n, gc.CTINT): - return n.Int(), true - case gc.Isconst(n, gc.CTBOOL): - return int64(obj.Bool2int(n.Bool())), true - } - return -} - // gins is called by the front end. // It synthesizes some multiple-instruction sequences // so the front end can stay simpler. func gins(as int, f, t *gc.Node) *obj.Prog { if as >= obj.A_ARCHSPECIFIC { - if x, ok := intLiteral(f); ok { + if x, ok := f.IntLiteral(); ok { ginscon(as, x, t) return nil // caller must not use } } if as == ppc64.ACMP || as == ppc64.ACMPU { - if x, ok := intLiteral(t); ok { + if x, ok := t.IntLiteral(); ok { ginscon2(as, f, x) return nil // caller must not use } -- 2.48.1