Skip to content

Commit e5688d0

Browse files
committed
cmd/internal/obj/riscv: simplify validation and encoding of raw instructions
Use wantImmU/immU rather than handrolling the same code. This also corrects the validation output - add tests to ensure this is the case. Change-Id: Id48f459c7c0de09ddde7a10506f66a3a269f325f Reviewed-on: https://go-review.googlesource.com/c/go/+/702396 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: David Chase <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent 77fc279 commit e5688d0

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ TEXT validation(SB),$0
1212
SRLI $1, X5, F1 // ERROR "expected integer register in rd position but got non-integer register F1"
1313
SRLI $1, F1, X5 // ERROR "expected integer register in rs1 position but got non-integer register F1"
1414

15+
WORD $-1 // ERROR "must be in range [0x0, 0xffffffff]"
16+
WORD $0x100000000 // ERROR "must be in range [0x0, 0xffffffff]"
17+
1518
//
1619
// "V" Standard Extension for Vector Operations, Version 1.0
1720
//

src/cmd/internal/obj/riscv/obj.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,9 +1419,7 @@ func validateVsetvl(ctxt *obj.Link, ins *instruction) {
14191419
func validateRaw(ctxt *obj.Link, ins *instruction) {
14201420
// Treat the raw value specially as a 32-bit unsigned integer.
14211421
// Nobody wants to enter negative machine code.
1422-
if ins.imm < 0 || 1<<32 <= ins.imm {
1423-
ctxt.Diag("%v: immediate %d in raw position cannot be larger than 32 bits", ins.as, ins.imm)
1424-
}
1422+
wantImmU(ctxt, ins, ins.imm, 32)
14251423
}
14261424

14271425
// extractBitAndShift extracts the specified bit from the given immediate,
@@ -1706,10 +1704,7 @@ func encodeVsetvl(ins *instruction) uint32 {
17061704
func encodeRawIns(ins *instruction) uint32 {
17071705
// Treat the raw value specially as a 32-bit unsigned integer.
17081706
// Nobody wants to enter negative machine code.
1709-
if ins.imm < 0 || 1<<32 <= ins.imm {
1710-
panic(fmt.Sprintf("immediate %d cannot fit in 32 bits", ins.imm))
1711-
}
1712-
return uint32(ins.imm)
1707+
return immU(ins.as, ins.imm, 32)
17131708
}
17141709

17151710
func EncodeBImmediate(imm int64) (int64, error) {

0 commit comments

Comments
 (0)