fix(asm): match RISC-V branch and jump encodings

This commit is contained in:
2026-08-13 17:57:10 +02:00
parent 31a2cee382
commit 681a449c01
6 changed files with 62 additions and 72 deletions
+8 -8
View File
@@ -428,7 +428,7 @@ TEXT ·`+tt.name+`(SB), NOSPLIT, $0
}
func TestRISCV_RVC_branch(t *testing.T) {
// BEQ rs, X0, target → C.BEQZ when rs is in prime regs and offset fits.
// Branches are never RVC-compressed (no C.BEQZ/C.BNEZ), matching go tool asm.
fn := firstTextRISCV(t, `#include "textflag.h"
TEXT ·cbeqz(SB), NOSPLIT, $0
ADDI $1, X10, X10
@@ -438,14 +438,14 @@ done:
RET
`)
code := assembleRISCVHelper(t, fn)
// C.ADDI(2) + C.BEQZ(2) + C.ADDI(2) + JALR(4) = 10 (all compress)
if len(code) != 10 {
t.Errorf("expected 10 bytes with C.BEQZ, got %d", len(code))
// C.ADDI(2) + BEQ(4) + C.ADDI(2) + JALR(4) = 12
if len(code) != 12 {
t.Errorf("expected 12 bytes with uncompressed BEQ, got %d", len(code))
}
}
func TestRISCV_RVC_CJ(t *testing.T) {
// JMP target → C.J when offset fits.
// JMP target → JAL X0 (never compressed to C.J), matching go tool asm.
fn := firstTextRISCV(t, `#include "textflag.h"
TEXT ·cj(SB), NOSPLIT, $0
JMP done
@@ -453,9 +453,9 @@ func TestRISCV_RVC_CJ(t *testing.T) {
RET
`)
code := assembleRISCVHelper(t, fn)
// C.J(2) + JALR(4) = 6
if len(code) != 6 {
t.Errorf("expected 6 bytes with C.J, got %d", len(code))
// JAL(4) + JALR(4) = 8
if len(code) != 8 {
t.Errorf("expected 8 bytes with uncompressed JMP, got %d", len(code))
}
}