]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/obj/riscv: simplify validation and encoding of raw instructions
authorJoel Sing <joel@sing.id.au>
Tue, 9 Sep 2025 15:09:41 +0000 (01:09 +1000)
committerJoel Sing <joel@sing.id.au>
Thu, 23 Oct 2025 12:38:59 +0000 (05:38 -0700)
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>
src/cmd/asm/internal/asm/testdata/riscv64validation.s
src/cmd/internal/obj/riscv/obj.go

index 65497659167c9828d45042974ccb5f488d2c345f..c8ae4c92114bbc396964154764b7c949108390ba 100644 (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, 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
        //
index 8c4140ec5c64526cd138ead84eb949f8d410fd0d..e55c206a98e434c6667631e04b96115f5c07ab7c 100644 (file)
@@ -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) {