Skip to content

Commit 3e47682

Browse files
committed
cmd/compile/internal/gc: move intLiteral to gc.Node
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 <[email protected]> Reviewed-by: Dave Cheney <[email protected]> Run-TryBot: Dave Cheney <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent c742ff6 commit 3e47682

File tree

4 files changed

+19
-42
lines changed

4 files changed

+19
-42
lines changed

src/cmd/compile/internal/arm64/gsubr.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -467,30 +467,18 @@ hard:
467467
return
468468
}
469469

470-
func intLiteral(n *gc.Node) (x int64, ok bool) {
471-
switch {
472-
case n == nil:
473-
return
474-
case gc.Isconst(n, gc.CTINT):
475-
return n.Int(), true
476-
case gc.Isconst(n, gc.CTBOOL):
477-
return int64(obj.Bool2int(n.Bool())), true
478-
}
479-
return
480-
}
481-
482470
// gins is called by the front end.
483471
// It synthesizes some multiple-instruction sequences
484472
// so the front end can stay simpler.
485473
func gins(as int, f, t *gc.Node) *obj.Prog {
486474
if as >= obj.A_ARCHSPECIFIC {
487-
if x, ok := intLiteral(f); ok {
475+
if x, ok := f.IntLiteral(); ok {
488476
ginscon(as, x, t)
489477
return nil // caller must not use
490478
}
491479
}
492480
if as == arm64.ACMP {
493-
if x, ok := intLiteral(t); ok {
481+
if x, ok := t.IntLiteral(); ok {
494482
ginscon2(as, f, x)
495483
return nil // caller must not use
496484
}

src/cmd/compile/internal/gc/const.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ import (
1010
"strings"
1111
)
1212

13+
// IntLiteral returns the Node's literal value as an interger.
14+
func (n *Node) IntLiteral() (x int64, ok bool) {
15+
switch {
16+
case n == nil:
17+
return
18+
case Isconst(n, CTINT):
19+
return n.Int(), true
20+
case Isconst(n, CTBOOL):
21+
return int64(obj.Bool2int(n.Bool())), true
22+
}
23+
return
24+
}
25+
1326
// Int returns n as an int.
1427
// n must be an integer constant.
1528
func (n *Node) Int() int64 {

src/cmd/compile/internal/mips64/gsubr.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -545,30 +545,18 @@ hard:
545545
return
546546
}
547547

548-
func intLiteral(n *gc.Node) (x int64, ok bool) {
549-
switch {
550-
case n == nil:
551-
return
552-
case gc.Isconst(n, gc.CTINT):
553-
return n.Int(), true
554-
case gc.Isconst(n, gc.CTBOOL):
555-
return int64(obj.Bool2int(n.Bool())), true
556-
}
557-
return
558-
}
559-
560548
// gins is called by the front end.
561549
// It synthesizes some multiple-instruction sequences
562550
// so the front end can stay simpler.
563551
func gins(as int, f, t *gc.Node) *obj.Prog {
564552
if as >= obj.A_ARCHSPECIFIC {
565-
if x, ok := intLiteral(f); ok {
553+
if x, ok := f.IntLiteral(); ok {
566554
ginscon(as, x, t)
567555
return nil // caller must not use
568556
}
569557
}
570558
if as == ppc64.ACMP || as == ppc64.ACMPU {
571-
if x, ok := intLiteral(t); ok {
559+
if x, ok := t.IntLiteral(); ok {
572560
ginscon2(as, f, x)
573561
return nil // caller must not use
574562
}

src/cmd/compile/internal/ppc64/gsubr.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -545,30 +545,18 @@ hard:
545545
return
546546
}
547547

548-
func intLiteral(n *gc.Node) (x int64, ok bool) {
549-
switch {
550-
case n == nil:
551-
return
552-
case gc.Isconst(n, gc.CTINT):
553-
return n.Int(), true
554-
case gc.Isconst(n, gc.CTBOOL):
555-
return int64(obj.Bool2int(n.Bool())), true
556-
}
557-
return
558-
}
559-
560548
// gins is called by the front end.
561549
// It synthesizes some multiple-instruction sequences
562550
// so the front end can stay simpler.
563551
func gins(as int, f, t *gc.Node) *obj.Prog {
564552
if as >= obj.A_ARCHSPECIFIC {
565-
if x, ok := intLiteral(f); ok {
553+
if x, ok := f.IntLiteral(); ok {
566554
ginscon(as, x, t)
567555
return nil // caller must not use
568556
}
569557
}
570558
if as == ppc64.ACMP || as == ppc64.ACMPU {
571-
if x, ok := intLiteral(t); ok {
559+
if x, ok := t.IntLiteral(); ok {
572560
ginscon2(as, f, x)
573561
return nil // caller must not use
574562
}

0 commit comments

Comments
 (0)