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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
This commit is contained in:
Joel Sing 2025-09-10 01:09:41 +10:00
parent 77fc27972a
commit e5688d0bdd
2 changed files with 5 additions and 7 deletions

View file

@ -12,6 +12,9 @@ TEXT validation(SB),$0
SRLI $1, X5, F1 // ERROR "expected integer register in rd position but got non-integer register F1" SRLI $1, X5, F1 // ERROR "expected integer register in rd position but got non-integer register F1"
SRLI $1, F1, X5 // ERROR "expected integer register in rs1 position but got non-integer register F1" SRLI $1, F1, X5 // ERROR "expected integer register in rs1 position but got non-integer register F1"
WORD $-1 // ERROR "must be in range [0x0, 0xffffffff]"
WORD $0x100000000 // ERROR "must be in range [0x0, 0xffffffff]"
// //
// "V" Standard Extension for Vector Operations, Version 1.0 // "V" Standard Extension for Vector Operations, Version 1.0
// //

View file

@ -1419,9 +1419,7 @@ func validateVsetvl(ctxt *obj.Link, ins *instruction) {
func validateRaw(ctxt *obj.Link, ins *instruction) { func validateRaw(ctxt *obj.Link, ins *instruction) {
// Treat the raw value specially as a 32-bit unsigned integer. // Treat the raw value specially as a 32-bit unsigned integer.
// Nobody wants to enter negative machine code. // Nobody wants to enter negative machine code.
if ins.imm < 0 || 1<<32 <= ins.imm { wantImmU(ctxt, ins, ins.imm, 32)
ctxt.Diag("%v: immediate %d in raw position cannot be larger than 32 bits", ins.as, ins.imm)
}
} }
// extractBitAndShift extracts the specified bit from the given immediate, // extractBitAndShift extracts the specified bit from the given immediate,
@ -1706,10 +1704,7 @@ func encodeVsetvl(ins *instruction) uint32 {
func encodeRawIns(ins *instruction) uint32 { func encodeRawIns(ins *instruction) uint32 {
// Treat the raw value specially as a 32-bit unsigned integer. // Treat the raw value specially as a 32-bit unsigned integer.
// Nobody wants to enter negative machine code. // Nobody wants to enter negative machine code.
if ins.imm < 0 || 1<<32 <= ins.imm { return immU(ins.as, ins.imm, 32)
panic(fmt.Sprintf("immediate %d cannot fit in 32 bits", ins.imm))
}
return uint32(ins.imm)
} }
func EncodeBImmediate(imm int64) (int64, error) { func EncodeBImmediate(imm int64) (int64, error) {