Skip to content

Commit 73ff6d1

Browse files
limeidanabner-chenc
authored andcommitted
cmd/internal/obj/loong64: change the immediate range of ALSL{W/WU/V}
When executing the alsl.w/wu/d family of instructions, the actual shift amount is the immediate value in the instruction encoding plus one. Therefore, this change is made to align the immediate value in the assembly code with the programmer's intended shift amount, and to include the result of the immediate value minus one in the final encoding. Change-Id: Ic82249251878eabde8372e183d841a03f963f9f9 Reviewed-on: https://go-review.googlesource.com/c/go/+/693475 Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Mark Freeman <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: abner chenc <[email protected]> Reviewed-by: sophie zhao <[email protected]>
1 parent f3606b0 commit 73ff6d1

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

src/cmd/asm/internal/asm/testdata/loong64enc1.s

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,6 @@ lable2:
11011101
XVBITREVV $63, X2, X1 // 41fc1977
11021102

11031103
// ALSL{W/WU/D}
1104-
ALSLW $3, R4, R5, R6 // 86940500
1105-
ALSLWU $3, R4, R5, R6 // 86940700
1106-
ALSLV $3, R4, R5, R6 // 86942d00
1104+
ALSLW $4, R4, R5, R6 // 86940500
1105+
ALSLWU $4, R4, R5, R6 // 86940700
1106+
ALSLV $4, R4, R5, R6 // 86942d00

src/cmd/internal/obj/loong64/asm.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ var optab = []Optab{
425425
{APRELD, C_SOREG, C_U5CON, C_NONE, C_NONE, C_NONE, 47, 4, 0, 0},
426426
{APRELDX, C_SOREG, C_DCON, C_U5CON, C_NONE, C_NONE, 48, 20, 0, 0},
427427

428-
{AALSLV, C_U2CON, C_REG, C_REG, C_REG, C_NONE, 64, 4, 0, 0},
428+
{AALSLV, C_U3CON, C_REG, C_REG, C_REG, C_NONE, 64, 4, 0, 0},
429429

430430
{obj.APCALIGN, C_U12CON, C_NONE, C_NONE, C_NONE, C_NONE, 0, 0, 0, 0},
431431
{obj.APCDATA, C_32CON, C_NONE, C_NONE, C_32CON, C_NONE, 0, 0, 0, 0},
@@ -2742,8 +2742,12 @@ func (c *ctxt0) asmout(p *obj.Prog, o *Optab, out []uint32) {
27422742
o1 = OP_RR(c.oprr(p.As), uint32(p.To.Reg), uint32(p.RegTo2))
27432743

27442744
case 64: // alsl rd, rj, rk, sa2
2745+
sa := p.From.Offset - 1
2746+
if sa > 3 {
2747+
c.ctxt.Diag("The shift amount is too large.")
2748+
}
27452749
r := p.GetFrom3().Reg
2746-
o1 = OP_2IRRR(c.opirrr(p.As), uint32(p.From.Offset), uint32(r), uint32(p.Reg), uint32(p.To.Reg))
2750+
o1 = OP_2IRRR(c.opirrr(p.As), uint32(sa), uint32(r), uint32(p.Reg), uint32(p.To.Reg))
27472751

27482752
case 65: // mov sym@GOT, r ==> pcalau12i + ld.d
27492753
o1 = OP_IR(c.opir(APCALAU12I), uint32(0), uint32(p.To.Reg))

src/cmd/internal/obj/loong64/doc.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,27 @@ Note: In the following sections 3.1 to 3.6, "ui4" (4-bit unsigned int immediate)
268268
bits[11:1]: block size, the value range is [16, 1024], and it must be an integer multiple of 16
269269
bits[20:12]: block num, the value range is [1, 256]
270270
bits[36:21]: stride, the value range is [0, 0xffff]
271+
272+
4. ShiftAdd instructions
273+
Mapping between Go and platform assembly:
274+
Go assembly | platform assembly
275+
ALSL.W/WU/V $Imm, Rj, Rk, Rd | alsl.w/wu/d rd, rj, rk, $imm
276+
277+
Instruction encoding format is as follows:
278+
279+
| 31 ~ 17 | 16 ~ 15 | 14 ~ 10 | 9 ~ 5 | 4 ~ 0 |
280+
| opcode | sa2 | rk | rj | rd |
281+
282+
The alsl.w/wu/v series of instructions shift the data in rj left by sa+1, add the value
283+
in rk, and write the result to rd.
284+
285+
To allow programmers to directly write the desired shift amount in assembly code, we actually write
286+
the value of sa2+1 in the assembly code and then include the value of sa2 in the instruction encoding.
287+
288+
For example:
289+
290+
Go assembly | instruction Encoding
291+
ALSLV $4, r4, r5, R6 | 002d9486
271292
*/
272293

273294
package loong64

0 commit comments

Comments
 (0)