Files
Odin/core/rexcode/isa/arm32/instructions.odin
Brendan Punsky 201dfdb454 rexcode/arm32: every VFP and NEON register list disassembled wrong
`vpush {d8, d9}` came out as `vpush {r2}`. VFP list operands kept only
the 8-bit count from the encoding and stored it in a REG_LIST operand,
whose immediate the printer reads as a GPR bitmask -- so the register
bank was wrong, the start register was gone entirely, and the count was
read as a bitmask. `vldm r0, {d1, d2, d3}` printed `{r1-r2}`.

A list is now a register plus a shape: how many, and what the run steps
by. VLDM/VSTM/VPUSH/VPOP recover their start register from Vd and their
count from imm8. A GPR list stays a bitmask, since `{r4, lr}` is not a
run at all.

The NEON structure loads were wrong in a second way. VLD1-4/ST1-4 encode
their register count in the type field at bits 11:8, not in imm8, so
writing a count into the low byte overwrote size, alignment and Rm. Rm
was left at 0, which is a register post-increment, where the plain form
needs 0b1111; every one of those 23 forms encoded a writeback nobody
asked for. The count now comes from the form, the encoding writes only
Vd, and Rm is fixed in the pattern.

Deriving those counts from llvm-mc rather than by hand turned up a
dimension that was not modelled at all: the spaced forms step two
registers at a time (`vld2.8 {d2, d4}`), and VLD1 has a to-all-lanes
form written `{d2[]}`.

All 31 A32 list forms are now byte-exact against llvm-mc, from 8. The
arm32 sweep holds at 1656/1656.

Still open here: those forms only exist for .8 data, because the size
field at bits 7:6 is unencoded, and the writeback variants
(`[r0]!`, `[r0], r1`) are not modelled.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018UmHLRF11EoWwNWCJ7JGaA
2026-08-28 00:08:07 -04:00

162 lines
7.3 KiB
Odin

// rexcode · Brendan Punsky (dotbmp@github), original author
package rexcode_arm32
// =============================================================================
// AArch32 INSTRUCTION
// =============================================================================
//
// Variable-length: A32 is always 4 bytes, T16 is 2 bytes, T32 is 4 bytes (two
// halfwords). The `length` field is filled in by the encoder from the matched
// Encoding entry's `bits` field via `inst_size_from_bits`.
//
// The `mode` field tells the encoder whether to dispatch to A32 or T32
// encoding entries; for VFP/NEON entries the encoder applies bit-28 swap as
// documented in encoding_types.odin.
Instruction_Flags :: bit_field u8 {
sets_flags: bool | 1, // S bit (writes APSR.NZCV)
wide: bool | 1, // force T32 wide form when both T16 + T32 exist
_: u8 | 6,
}
// Sized and aligned to a cache line -- see the note in arm64/instructions.odin.
// The payload is 48 bytes; padding out to 64 and aligning is worth ~21% on
// decode, because decode writes whole Instructions and unaligned stores are
// expensive enough to outweigh writing more bytes.
Instruction :: struct #align(64) {
ops: [4]Operand `fmt:"v,operand_count"`, // 4 * 11 = 44
mnemonic: Mnemonic, // 2
// cond, operand_count, mode, length and the two flag bits share one
// 16-bit word -- together they need 13 bits, and spending six bytes on
// them was what pushed Instruction over 48. `using` keeps inst.cond,
// inst.operand_count, inst.mode, inst.length, inst.sets_flags and
// inst.wide reading and writing exactly as they did as plain fields.
using _: bit_field u16 {
cond: u8 | 4, // 0..15 (AL = 14)
operand_count: u8 | 3, // 0..4
mode: Mode | 1, // A32 / T32
length: u8 | 3, // 2 or 4 bytes on the wire
sets_flags: bool | 1, // S bit (writes APSR.NZCV)
wide: bool | 1, // force the T32 wide form when both exist
// 3 bits spare
},
// Form-id hint: when non-zero, this is (1 + the index into
// ENCODING_TABLE[mnemonic]) of the form the decoder produced. The encoder
// uses it as a tie-breaker for the shape-ambiguous entries the data type
// does not separate on its own -- register lists, LDM/STM addressing
// modes. User-constructed instructions leave it at 0 and take the
// first shape match.
form_id: u16,
// The `.i32` / `.s32.f32` suffix. Zero (.NONE) means "unspecified": the
// encoder then takes the first form of the matching shape, which is what
// every instruction did before this field existed. Set it and the encoder
// picks the encoding for that type.
dt: Data_Types,
// Spare, and free: a 48-byte struct straddles a cache line, so these
// bytes cost nothing. New fields land here without changing the layout.
_: [12]u8,
}
#assert(size_of(Instruction) == 64)
#assert(align_of(Instruction) == 64)
// =============================================================================
// Builders
// =============================================================================
@(require_results)
inst_none :: #force_inline proc "contextless" (m: Mnemonic, mode: Mode = .A32) -> Instruction {
return Instruction{mnemonic = m, operand_count = 0, length = mode == .A32 ? 4 : 2, mode = mode, cond = 14}
}
// 1-operand
@(require_results)
inst_r :: #force_inline proc "contextless" (m: Mnemonic, r: Register, mode: Mode = .A32) -> Instruction {
return Instruction{mnemonic = m, operand_count = 1, length = mode == .A32 ? 4 : 4, mode = mode, cond = 14,
ops = {op_reg(r), {}, {}, {}}}
}
@(require_results)
inst_i :: #force_inline proc "contextless" (m: Mnemonic, v: i64, mode: Mode = .A32) -> Instruction {
return Instruction{mnemonic = m, operand_count = 1, length = mode == .A32 ? 4 : 4, mode = mode, cond = 14,
ops = {op_imm(v), {}, {}, {}}}
}
// 2-operand
@(require_results)
inst_r_r :: #force_inline proc "contextless" (m: Mnemonic, rd, rm: Register, mode: Mode = .A32) -> Instruction {
return Instruction{mnemonic = m, operand_count = 2, length = mode == .A32 ? 4 : 4, mode = mode, cond = 14,
ops = {op_reg(rd), op_reg(rm), {}, {}}}
}
@(require_results)
inst_r_i :: #force_inline proc "contextless" (m: Mnemonic, rd: Register, v: i64, mode: Mode = .A32) -> Instruction {
return Instruction{mnemonic = m, operand_count = 2, length = mode == .A32 ? 4 : 4, mode = mode, cond = 14,
ops = {op_reg(rd), op_imm(v), {}, {}}}
}
// 3-operand data-proc (ADD/SUB/AND/etc.)
@(require_results)
inst_r_r_r :: #force_inline proc "contextless" (m: Mnemonic, rd, rn, rm: Register, mode: Mode = .A32) -> Instruction {
return Instruction{mnemonic = m, operand_count = 3, length = mode == .A32 ? 4 : 4, mode = mode, cond = 14,
ops = {op_reg(rd), op_reg(rn), op_reg(rm), {}}}
}
@(require_results)
inst_r_r_i :: #force_inline proc "contextless" (m: Mnemonic, rd, rn: Register, v: i64, mode: Mode = .A32) -> Instruction {
return Instruction{mnemonic = m, operand_count = 3, length = mode == .A32 ? 4 : 4, mode = mode, cond = 14,
ops = {op_reg(rd), op_reg(rn), op_imm(v), {}}}
}
@(require_results)
inst_r_r_r_shifted :: #force_inline proc "contextless" (
m: Mnemonic, rd, rn, rm: Register, st: Shift_Type, amt: u8, mode: Mode = .A32,
) -> Instruction {
return Instruction{mnemonic = m, operand_count = 3, length = mode == .A32 ? 4 : 4, mode = mode, cond = 14,
ops = {op_reg(rd), op_reg(rn), op_reg_shifted(rm, st, amt), {}}}
}
// 4-operand MLA / MLS / SMLAL etc.
@(require_results)
inst_r_r_r_r :: #force_inline proc "contextless" (m: Mnemonic, rd, rn, rm, ra: Register, mode: Mode = .A32) -> Instruction {
return Instruction{mnemonic = m, operand_count = 4, length = mode == .A32 ? 4 : 4, mode = mode, cond = 14,
ops = {op_reg(rd), op_reg(rn), op_reg(rm), op_reg(ra)}}
}
// Memory load/store
@(require_results)
inst_load :: #force_inline proc "contextless" (m: Mnemonic, rd: Register, mm: Memory, mode: Mode = .A32) -> Instruction {
return Instruction{mnemonic = m, operand_count = 2, length = mode == .A32 ? 4 : 4, mode = mode, cond = 14,
ops = {op_reg(rd), op_mem(mm), {}, {}}}
}
@(require_results)
inst_store :: #force_inline proc "contextless" (m: Mnemonic, rd: Register, mm: Memory, mode: Mode = .A32) -> Instruction {
return inst_load(m, rd, mm, mode)
}
// LDM/STM/PUSH/POP block move
@(require_results)
inst_block :: #force_inline proc "contextless" (m: Mnemonic, base: Register, mask: u16, mode: Mode = .A32) -> Instruction {
return Instruction{mnemonic = m, operand_count = 2, length = mode == .A32 ? 4 : 4, mode = mode, cond = 14,
ops = {op_reg(base), op_reg_list(mask), {}, {}}}
}
// Branches with label
@(require_results)
inst_branch :: #force_inline proc "contextless" (m: Mnemonic, label_id: u32, mode: Mode = .A32) -> Instruction {
return Instruction{mnemonic = m, operand_count = 1, length = mode == .A32 ? 4 : 4, mode = mode, cond = 14,
ops = {op_label(label_id), {}, {}, {}}}
}
// Set condition code on any builder
@(require_results)
inst_set_cond :: #force_inline proc "contextless" (inst: Instruction, cond: u8) -> Instruction {
out := inst
out.cond = cond
return out
}
// Set S flag (sets APSR.NZCV)
@(require_results)
inst_set_flags :: #force_inline proc "contextless" (inst: Instruction) -> Instruction {
out := inst
out.sets_flags = true
return out
}