From: Joel Sing Date: Tue, 9 Sep 2025 15:09:41 +0000 (+1000) Subject: cmd/internal/obj/riscv: simplify validation and encoding of raw instructions X-Git-Tag: go1.26rc1~526 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=e5688d0bddb0196b7f057bcae8baf8521c571119;p=gostls13.git 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 Reviewed-by: David Chase Reviewed-by: Cherry Mui --- diff --git a/src/cmd/asm/internal/asm/testdata/riscv64validation.s b/src/cmd/asm/internal/asm/testdata/riscv64validation.s index 6549765916..c8ae4c9211 100644 --- a/src/cmd/asm/internal/asm/testdata/riscv64validation.s +++ b/src/cmd/asm/internal/asm/testdata/riscv64validation.s @@ -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, 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 // diff --git a/src/cmd/internal/obj/riscv/obj.go b/src/cmd/internal/obj/riscv/obj.go index 8c4140ec5c..e55c206a98 100644 --- a/src/cmd/internal/obj/riscv/obj.go +++ b/src/cmd/internal/obj/riscv/obj.go @@ -1419,9 +1419,7 @@ func validateVsetvl(ctxt *obj.Link, ins *instruction) { func validateRaw(ctxt *obj.Link, ins *instruction) { // Treat the raw value specially as a 32-bit unsigned integer. // Nobody wants to enter negative machine code. - if ins.imm < 0 || 1<<32 <= ins.imm { - ctxt.Diag("%v: immediate %d in raw position cannot be larger than 32 bits", ins.as, ins.imm) - } + wantImmU(ctxt, ins, ins.imm, 32) } // extractBitAndShift extracts the specified bit from the given immediate, @@ -1706,10 +1704,7 @@ func encodeVsetvl(ins *instruction) uint32 { func encodeRawIns(ins *instruction) uint32 { // Treat the raw value specially as a 32-bit unsigned integer. // Nobody wants to enter negative machine code. - if ins.imm < 0 || 1<<32 <= ins.imm { - panic(fmt.Sprintf("immediate %d cannot fit in 32 bits", ins.imm)) - } - return uint32(ins.imm) + return immU(ins.as, ins.imm, 32) } func EncodeBImmediate(imm int64) (int64, error) {