From e7c7f3326335649d6fcae8cba297808fc60ed388 Mon Sep 17 00:00:00 2001 From: Joel Sing Date: Mon, 9 Jan 2023 04:21:37 +1100 Subject: [PATCH] cmd/internal/obj/arm64: factor out code generation for addition of 12 bit immediates Factor out and simplify code that generates the addition of a 12 bit immediate (the addition of a negative value is still handled via subtraction). This also fixes the mishandling of the case where v is 0. Change-Id: I6040f33d2fec87b772272531b3bf02390ae7f200 Reviewed-on: https://go-review.googlesource.com/c/go/+/461141 TryBot-Result: Gopher Robot Reviewed-by: Eric Fang Reviewed-by: Than McIntosh Run-TryBot: Joel Sing Reviewed-by: Cherry Mui --- src/cmd/internal/obj/arm64/asm7.go | 56 ++++++++++++------------------ 1 file changed, 23 insertions(+), 33 deletions(-) diff --git a/src/cmd/internal/obj/arm64/asm7.go b/src/cmd/internal/obj/arm64/asm7.go index 03844cba08..7dc8729728 100644 --- a/src/cmd/internal/obj/arm64/asm7.go +++ b/src/cmd/internal/obj/arm64/asm7.go @@ -4678,27 +4678,15 @@ func (c *ctxt7) asmout(p *obj.Prog, o *Optab, out []uint32) { case 74: // add $O, R, Rtmp or sub $O, R, Rtmp // ldp (Rtmp), (R1, R2) - r := int(p.From.Reg) - if r == obj.REG_NONE { - r = int(o.param) + rf := p.From.Reg + if rf == obj.REG_NONE { + rf = o.param } - if r == obj.REG_NONE { + if rf == obj.REG_NONE { c.ctxt.Diag("invalid ldp source: %v", p) } v := int32(c.regoff(&p.From)) - - if v > 0 { - if v > 4095 { - c.ctxt.Diag("offset out of range: %v", p) - } - o1 = c.oaddi(p, int32(c.opirr(p, AADD)), v, r, REGTMP) - } - if v < 0 { - if v < -4095 { - c.ctxt.Diag("offset out of range: %v", p) - } - o1 = c.oaddi(p, int32(c.opirr(p, ASUB)), -v, r, REGTMP) - } + o1 = c.oaddi12(p, v, REGTMP, rf) o2 = c.opldpstp(p, o, 0, uint32(REGTMP), uint32(p.To.Reg), uint32(p.To.Offset), 1) case 75: @@ -4728,26 +4716,15 @@ func (c *ctxt7) asmout(p *obj.Prog, o *Optab, out []uint32) { if p.From.Reg == REGTMP || p.From.Offset == REGTMP { c.ctxt.Diag("cannot use REGTMP as source: %v", p) } - r := int(p.To.Reg) - if r == obj.REG_NONE { - r = int(o.param) + rt := p.To.Reg + if rt == obj.REG_NONE { + rt = o.param } - if r == obj.REG_NONE { + if rt == obj.REG_NONE { c.ctxt.Diag("invalid stp destination: %v", p) } v := int32(c.regoff(&p.To)) - if v > 0 { - if v > 4095 { - c.ctxt.Diag("offset out of range: %v", p) - } - o1 = c.oaddi(p, int32(c.opirr(p, AADD)), v, r, REGTMP) - } - if v < 0 { - if v < -4095 { - c.ctxt.Diag("offset out of range: %v", p) - } - o1 = c.oaddi(p, int32(c.opirr(p, ASUB)), -v, r, REGTMP) - } + o1 = c.oaddi12(p, v, REGTMP, rt) o2 = c.opldpstp(p, o, 0, uint32(REGTMP), uint32(p.From.Reg), uint32(p.From.Offset), 0) case 77: @@ -7146,6 +7123,19 @@ func (c *ctxt7) oaddi(p *obj.Prog, o1 int32, v int32, r int, rt int) uint32 { return uint32(o1) } +func (c *ctxt7) oaddi12(p *obj.Prog, v int32, rd, rn int16) uint32 { + if v < -4095 || v > 4095 { + c.ctxt.Diag("%v is not a 12 bit immediate: %v", v, p) + return 0 + } + a := AADD + if v < 0 { + a = ASUB + v = -v + } + return c.oaddi(p, int32(c.opirr(p, a)), v, int(rn), int(rd)) +} + /* * load a literal value into dr */