Skip to content

Commit 37470c0

Browse files
committed
cmd/internal/obj/ppc64: add support for pcalign 32 on ppc64x
Previous PCALIGN support on ppc64x only accepted 8 and 16 byte alignment since the default function alignment was 16. Now that the function's alignment can be set to a larger value when needed, PCALIGN can accept 32. When this happens then the function's alignment will be changed to 32. Test has been updated to recognized this new value. Change-Id: If82c3cd50d7c686fcf8a9e819708b15660cdfa63 Reviewed-on: https://go-review.googlesource.com/c/go/+/227775 Run-TryBot: Lynn Boger <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Cherry Zhang <[email protected]>
1 parent 162f1bf commit 37470c0

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

src/cmd/internal/obj/ppc64/asm9.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ var oprange [ALAST & obj.AMask][]Optab
620620
var xcmp [C_NCLASS][C_NCLASS]bool
621621

622622
// padding bytes to add to align code as requested
623-
func addpad(pc, a int64, ctxt *obj.Link) int {
623+
func addpad(pc, a int64, ctxt *obj.Link, cursym *obj.LSym) int {
624624
switch a {
625625
case 8:
626626
if pc&7 != 0 {
@@ -633,6 +633,21 @@ func addpad(pc, a int64, ctxt *obj.Link) int {
633633
case 8:
634634
return 8
635635
}
636+
case 32:
637+
switch pc & 31 {
638+
case 4, 20:
639+
return 12
640+
case 8, 24:
641+
return 8
642+
case 12, 28:
643+
return 4
644+
}
645+
// The default function alignment is 16, but
646+
// if 32 byte alignment is requested then the
647+
// function needs to be aligned to 32.
648+
if cursym.Func.Align < 32 {
649+
cursym.Func.Align = 32
650+
}
636651
default:
637652
ctxt.Diag("Unexpected alignment: %d for PCALIGN directive\n", a)
638653
}
@@ -663,7 +678,7 @@ func span9(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) {
663678
if m == 0 {
664679
if p.As == obj.APCALIGN {
665680
a := c.vregoff(&p.From)
666-
m = addpad(pc, a, ctxt)
681+
m = addpad(pc, a, ctxt, cursym)
667682
} else {
668683
if p.As != obj.ANOP && p.As != obj.AFUNCDATA && p.As != obj.APCDATA {
669684
ctxt.Diag("zero-width instruction\n%v", p)
@@ -721,7 +736,7 @@ func span9(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) {
721736
if m == 0 {
722737
if p.As == obj.APCALIGN {
723738
a := c.vregoff(&p.From)
724-
m = addpad(pc, a, ctxt)
739+
m = addpad(pc, a, ctxt, cursym)
725740
} else {
726741
if p.As != obj.ANOP && p.As != obj.AFUNCDATA && p.As != obj.APCDATA {
727742
ctxt.Diag("zero-width instruction\n%v", p)
@@ -736,10 +751,6 @@ func span9(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) {
736751
c.cursym.Size = pc
737752
}
738753

739-
if r := pc & funcAlignMask; r != 0 {
740-
pc += funcAlign - r
741-
}
742-
743754
c.cursym.Size = pc
744755

745756
/*
@@ -761,7 +772,7 @@ func span9(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) {
761772
if o.type_ == 0 && p.As == obj.APCALIGN {
762773
pad := LOP_RRR(OP_OR, REGZERO, REGZERO, REGZERO)
763774
aln := c.vregoff(&p.From)
764-
v := addpad(p.Pc, aln, c.ctxt)
775+
v := addpad(p.Pc, aln, c.ctxt, c.cursym)
765776
if v > 0 {
766777
// Same padding instruction for all
767778
for i = 0; i < int32(v/4); i++ {

src/cmd/internal/obj/ppc64/asm_test.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,39 @@ import (
1010
"os"
1111
"os/exec"
1212
"path/filepath"
13+
"regexp"
1314
"strings"
1415
"testing"
1516
)
1617

1718
var invalidPCAlignSrc = `
1819
TEXT test(SB),0,$0-0
1920
ADD $2, R3
20-
PCALIGN $32
21+
PCALIGN $64
2122
RET
2223
`
24+
2325
var validPCAlignSrc = `
2426
TEXT test(SB),0,$0-0
2527
ADD $2, R3
2628
PCALIGN $16
27-
MOVD $8, R4
28-
ADD $8, R4
29-
PCALIGN $16
29+
MOVD $8, R16
3030
ADD $8, R4
31+
PCALIGN $32
32+
ADD $8, R3
3133
PCALIGN $8
32-
ADD $4, R6
33-
PCALIGN $16
34-
ADD R2, R3, R4
34+
ADD $4, R8
3535
RET
3636
`
3737

3838
// TestPCalign generates two asm files containing the
3939
// PCALIGN directive, to verify correct values are and
4040
// accepted, and incorrect values are flagged in error.
4141
func TestPCalign(t *testing.T) {
42+
var pattern8 = `0x...8\s.*ADD\s..,\sR8`
43+
var pattern16 = `0x...[80]\s.*MOVD\s..,\sR16`
44+
var pattern32 = `0x...0\s.*ADD\s..,\sR3`
45+
4246
testenv.MustHaveGoBuild(t)
4347

4448
dir, err := ioutil.TempDir("", "testpcalign")
@@ -63,6 +67,30 @@ func TestPCalign(t *testing.T) {
6367
t.Errorf("Build failed: %v, output: %s", err, out)
6468
}
6569

70+
matched, err := regexp.MatchString(pattern8, string(out))
71+
if err != nil {
72+
t.Fatal(err)
73+
}
74+
if !matched {
75+
t.Errorf("The 8 byte alignment is not correct: %t, output:%s\n", matched, out)
76+
}
77+
78+
matched, err = regexp.MatchString(pattern16, string(out))
79+
if err != nil {
80+
t.Fatal(err)
81+
}
82+
if !matched {
83+
t.Errorf("The 16 byte alignment is not correct: %t, output:%s\n", matched, out)
84+
}
85+
86+
matched, err = regexp.MatchString(pattern32, string(out))
87+
if err != nil {
88+
t.Fatal(err)
89+
}
90+
if !matched {
91+
t.Errorf("The 32 byte alignment is not correct: %t, output:%s\n", matched, out)
92+
}
93+
6694
// generate a test with invalid use of PCALIGN
6795

6896
tmpfile = filepath.Join(dir, "xi.s")

0 commit comments

Comments
 (0)