aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/internal
diff options
context:
space:
mode:
authorWANG Xuerui <git@xen0n.name>2023-03-28 19:30:04 +0800
committerGopher Robot <gobot@golang.org>2023-04-07 20:20:25 +0000
commit47b22b6548822d7b51ba3dca3c8c5fe669bfdd59 (patch)
tree9b6da1b8d7f5adc895f441720889e7acc60ccc2f /src/cmd/internal
parent10c079a0ad2283be3761a47eda6e41bde38fd16b (diff)
downloadgo-47b22b6548822d7b51ba3dca3c8c5fe669bfdd59.tar.xz
cmd/link, cmd/internal/obj/loong64: support the PCALIGN directive
Allow writing `PCALIGN $imm` where imm is a power-of-2 between 8 and 2048 (inclusive), for ensuring that the following instruction is placed at an imm-byte boundary relative to the beginning of the function. If the PC is not sufficiently aligned, NOOPs will be inserted to make it so, otherwise the directive will do nothing. This could be useful for both asm performance hand-tuning, and future scenarios where a certain bigger alignment might be required. Change-Id: Iad6244669a3d5adea88eceb0dc7be1af4f0d4fc9 Reviewed-on: https://go-review.googlesource.com/c/go/+/479815 Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Keith Randall <khr@google.com> Run-TryBot: WANG Xuerui <git@xen0n.name> Auto-Submit: Ian Lance Taylor <iant@golang.org> Reviewed-by: abner chenc <chenguoqi@loongson.cn> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'src/cmd/internal')
-rw-r--r--src/cmd/internal/obj/loong64/asm.go45
1 files changed, 41 insertions, 4 deletions
diff --git a/src/cmd/internal/obj/loong64/asm.go b/src/cmd/internal/obj/loong64/asm.go
index 32b13d91c0..e23165a874 100644
--- a/src/cmd/internal/obj/loong64/asm.go
+++ b/src/cmd/internal/obj/loong64/asm.go
@@ -345,6 +345,7 @@ var optab = []Optab{
{ARDTIMED, C_NONE, C_NONE, C_NONE, C_REG, C_REG, 62, 4, 0, 0},
{obj.AUNDEF, C_NONE, C_NONE, C_NONE, C_NONE, C_NONE, 49, 4, 0, 0},
+ {obj.APCALIGN, C_SCON, C_NONE, C_NONE, C_NONE, C_NONE, 0, 0, 0, 0},
{obj.APCDATA, C_LCON, C_NONE, C_NONE, C_LCON, C_NONE, 0, 0, 0, 0},
{obj.APCDATA, C_DCON, C_NONE, C_NONE, C_DCON, C_NONE, 0, 0, 0, 0},
{obj.AFUNCDATA, C_SCON, C_NONE, C_NONE, C_ADDR, C_NONE, 0, 0, 0, 0},
@@ -359,6 +360,15 @@ var optab = []Optab{
{obj.AXXX, C_NONE, C_NONE, C_NONE, C_NONE, C_NONE, 0, 4, 0, 0},
}
+// pcAlignPadLength returns the number of bytes required to align pc to alignedValue,
+// reporting an error if alignedValue is not a power of two or is out of range.
+func pcAlignPadLength(ctxt *obj.Link, pc int64, alignedValue int64) int {
+ if !((alignedValue&(alignedValue-1) == 0) && 8 <= alignedValue && alignedValue <= 2048) {
+ ctxt.Diag("alignment value of an instruction must be a power of two and in the range [8, 2048], got %d\n", alignedValue)
+ }
+ return int(-pc & (alignedValue - 1))
+}
+
var oprange [ALAST & obj.AMask][]Optab
var xcmp [C_NCLASS][C_NCLASS]bool
@@ -390,10 +400,20 @@ func span0(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) {
o = c.oplook(p)
m = int(o.size)
if m == 0 {
- if p.As != obj.ANOP && p.As != obj.AFUNCDATA && p.As != obj.APCDATA {
+ switch p.As {
+ case obj.APCALIGN:
+ alignedValue := p.From.Offset
+ m = pcAlignPadLength(ctxt, pc, alignedValue)
+ // Update the current text symbol alignment value.
+ if int32(alignedValue) > cursym.Func().Align {
+ cursym.Func().Align = int32(alignedValue)
+ }
+ break
+ case obj.ANOP, obj.AFUNCDATA, obj.APCDATA:
+ continue
+ default:
c.ctxt.Diag("zero-width instruction\n%v", p)
}
- continue
}
pc += int64(m)
@@ -443,10 +463,16 @@ func span0(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) {
m = int(o.size)
if m == 0 {
- if p.As != obj.ANOP && p.As != obj.AFUNCDATA && p.As != obj.APCDATA {
+ switch p.As {
+ case obj.APCALIGN:
+ alignedValue := p.From.Offset
+ m = pcAlignPadLength(ctxt, pc, alignedValue)
+ break
+ case obj.ANOP, obj.AFUNCDATA, obj.APCDATA:
+ continue
+ default:
c.ctxt.Diag("zero-width instruction\n%v", p)
}
- continue
}
pc += int64(m)
@@ -470,6 +496,16 @@ func span0(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) {
if int(o.size) > 4*len(out) {
log.Fatalf("out array in span0 is too small, need at least %d for %v", o.size/4, p)
}
+ if p.As == obj.APCALIGN {
+ alignedValue := p.From.Offset
+ v := pcAlignPadLength(c.ctxt, p.Pc, alignedValue)
+ for i = 0; i < int32(v/4); i++ {
+ // emit ANOOP instruction by the padding size
+ c.ctxt.Arch.ByteOrder.PutUint32(bp, c.oprrr(ANOOP))
+ bp = bp[4:]
+ }
+ continue
+ }
c.asmout(p, o, out[:])
for i = 0; i < int32(o.size/4); i++ {
c.ctxt.Arch.ByteOrder.PutUint32(bp, out[i])
@@ -1062,6 +1098,7 @@ func buildop(ctxt *obj.Link) {
obj.ATEXT,
obj.AUNDEF,
obj.AFUNCDATA,
+ obj.APCALIGN,
obj.APCDATA,
obj.ADUFFZERO,
obj.ADUFFCOPY: