cmd/internal/obj/riscv: simplify huge frame prologue

CL 307010 for riscv64.

Some of the comments on the other prologue paths were wrong, so this
CL also fixes them up.

Change-Id: Icdca1ade3a47ae6e2467af832690d40689dbe1b6
Reviewed-on: https://go-review.googlesource.com/c/go/+/307150
Trust: Austin Clements <austin@google.com>
Run-TryBot: Austin Clements <austin@google.com>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
This commit is contained in:
Austin Clements 2021-04-02 17:20:15 -04:00
parent a06b08e7d1
commit 042f4cbb6f

View file

@ -984,8 +984,9 @@ func stacksplit(ctxt *obj.Link, p *obj.Prog, cursym *obj.LSym, newprog obj.ProgA
var to_done, to_more *obj.Prog var to_done, to_more *obj.Prog
if framesize <= objabi.StackSmall { if framesize <= objabi.StackSmall {
// small stack: SP < stackguard // small stack
// BLTU SP, stackguard, done // // if SP > stackguard { goto done }
// BLTU stackguard, SP, done
p = obj.Appendp(p, newprog) p = obj.Appendp(p, newprog)
p.As = ABLTU p.As = ABLTU
p.From.Type = obj.TYPE_REG p.From.Type = obj.TYPE_REG
@ -993,80 +994,48 @@ func stacksplit(ctxt *obj.Link, p *obj.Prog, cursym *obj.LSym, newprog obj.ProgA
p.Reg = REG_SP p.Reg = REG_SP
p.To.Type = obj.TYPE_BRANCH p.To.Type = obj.TYPE_BRANCH
to_done = p to_done = p
} else if framesize <= objabi.StackBig {
// large stack: SP-framesize < stackguard-StackSmall
// ADD $-(framesize-StackSmall), SP, X11
// BLTU X11, stackguard, done
p = obj.Appendp(p, newprog)
// TODO(sorear): logic inconsistent with comment, but both match all non-x86 arches
p.As = AADDI
p.From.Type = obj.TYPE_CONST
p.From.Offset = -(int64(framesize) - objabi.StackSmall)
p.Reg = REG_SP
p.To.Type = obj.TYPE_REG
p.To.Reg = REG_X11
p = obj.Appendp(p, newprog)
p.As = ABLTU
p.From.Type = obj.TYPE_REG
p.From.Reg = REG_X10
p.Reg = REG_X11
p.To.Type = obj.TYPE_BRANCH
to_done = p
} else { } else {
// Such a large stack we need to protect against wraparound. // large stack: SP-framesize < stackguard-StackSmall
// If SP is close to zero: offset := int64(framesize) - objabi.StackSmall
// SP-stackguard+StackGuard <= framesize + (StackGuard-StackSmall) if framesize > objabi.StackBig {
// The +StackGuard on both sides is required to keep the left side positive: // Such a large stack we need to protect against underflow.
// SP is allowed to be slightly below stackguard. See stack.h. // The runtime guarantees SP > objabi.StackBig, but
// // framesize is large enough that SP-framesize may
// Preemption sets stackguard to StackPreempt, a very large value. // underflow, causing a direct comparison with the
// That breaks the math above, so we have to check for that explicitly. // stack guard to incorrectly succeed. We explicitly
// // stackguard is X10 // guard against underflow.
// MOV $StackPreempt, X11 //
// BEQ X10, X11, more // MOV $(framesize-StackSmall), X11
// ADD $StackGuard, SP, X11 // BLTU SP, X11, label-of-call-to-morestack
// SUB X10, X11
// MOV $(framesize+(StackGuard-StackSmall)), X10
// BGTU X11, X10, done
p = obj.Appendp(p, newprog)
p.As = AMOV
p.From.Type = obj.TYPE_CONST
p.From.Offset = objabi.StackPreempt
p.To.Type = obj.TYPE_REG
p.To.Reg = REG_X11
p = obj.Appendp(p, newprog) p = obj.Appendp(p, newprog)
to_more = p p.As = AMOV
p.As = ABEQ p.From.Type = obj.TYPE_CONST
p.From.Type = obj.TYPE_REG p.From.Offset = offset
p.From.Reg = REG_X10 p.To.Type = obj.TYPE_REG
p.Reg = REG_X11 p.To.Reg = REG_X11
p.To.Type = obj.TYPE_BRANCH
p = obj.Appendp(p, newprog)
p.As = ABLTU
p.From.Type = obj.TYPE_REG
p.From.Reg = REG_SP
p.Reg = REG_X11
p.To.Type = obj.TYPE_BRANCH
to_more = p
}
// Check against the stack guard. We've ensured this won't underflow.
// ADD $-(framesize-StackSmall), SP, X11
// // if X11 > stackguard { goto done }
// BLTU stackguard, X11, done
p = obj.Appendp(p, newprog) p = obj.Appendp(p, newprog)
p.As = AADDI p.As = AADDI
p.From.Type = obj.TYPE_CONST p.From.Type = obj.TYPE_CONST
p.From.Offset = int64(objabi.StackGuard) p.From.Offset = -offset
p.Reg = REG_SP p.Reg = REG_SP
p.To.Type = obj.TYPE_REG p.To.Type = obj.TYPE_REG
p.To.Reg = REG_X11 p.To.Reg = REG_X11
p = obj.Appendp(p, newprog)
p.As = ASUB
p.From.Type = obj.TYPE_REG
p.From.Reg = REG_X10
p.Reg = REG_X11
p.To.Type = obj.TYPE_REG
p.To.Reg = REG_X11
p = obj.Appendp(p, newprog)
p.As = AMOV
p.From.Type = obj.TYPE_CONST
p.From.Offset = int64(framesize) + int64(objabi.StackGuard) - objabi.StackSmall
p.To.Type = obj.TYPE_REG
p.To.Reg = REG_X10
p = obj.Appendp(p, newprog) p = obj.Appendp(p, newprog)
p.As = ABLTU p.As = ABLTU
p.From.Type = obj.TYPE_REG p.From.Type = obj.TYPE_REG