mirror of
https://github.com/odin-lang/Odin.git
synced 2026-11-05 02:03:16 -05:00
The A32 Advanced SIMD rows were written out by hand, one mnemonic at a
time, and it showed: most mnemonics had one element size where the
architecture has three or four, masks left the size and the Q bit free,
so one word decoded as whichever row sorted first, the shift-by-immediate
family read the element size as part of the shift, and the element and
structure loads knew neither alignment nor writeback. Against llvm, 1243
random A32 words decoded where llvm decodes nothing and 139 decoded to
the wrong instruction.
Those rows are gone. tablegen/simd_table.odin describes the space the
way the Architecture Reference Manual lays it out -- three registers of
the same length, of different lengths, by scalar, by shift, two-register
miscellaneous, modified immediate, the loads and stores, the v8.x dot
product, complex, FHM, BFloat16 and matrix additions -- and expands each
class into a row per element size, data type and register shape. The
size and type are fixed bits of every row, so a word decodes to one
`.<dt>` and the encoder picks the row by it. The Thumb forms follow
from these as they did before.
The fields whose meaning depends on the rest of the word -- a structure
load's register count, spacing, lane and alignment; L:imm6, whose
leading one is the element size and the rest the shift -- are read off
the whole word (simd.odin), by the decoder from the word and by the
encoder from the form. That replaces thirty-eight per-size encodings
with twelve. Memory gains an alignment (`[r0:64]`) and a post-increment
mode (`[r0]!`), the Advanced SIMD loads' writeback; disp gives up three
of its nineteen bits for it.
Along the way:
* The decoder reused one Instruction across candidate rows, so a row
turned away after unpacking left its operands for the next one: an
LDC2 picked up a fourth operand from a VCMLA it was not.
* VMRS and VMSR reach different system registers per profile; a
register run that is empty or passes D31 is not an instruction;
VDUP from a core register had one size of three and left its
should-be-zero bits free; FLDMX and FSTMX start in D0-D15; SWP's
address is an address; MSR names its PSR fields instead of a
number; PSB CSYNC is AArch64's, and in AArch32 is HINT #17.
* `vmov.i8` is the byte, not four copies of it.
The harness had one blind spot of its own: a literal load's address in
llvm's trailing comment stopped it from asking whether llvm's text
reassembles. And llvm prints a D list that runs past D31 with the names
of the registers after D31 in its own table -- `{d31, fpinst2}` -- and
takes that back; the harness counts that word as having no text, which
is what it is.
The random A32 suite now passes outright: of 42,408 words none decodes
wrong, extra, missing or unassemblable, and every one re-encodes. The
Cortex-A15 and A53 objects, which had 150 Advanced SIMD words rexcode
could not decode, are exact.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BbUtG4UwNiMydtMW2qGhw4
147 lines
4.4 KiB
Odin
147 lines
4.4 KiB
Odin
// rexcode · Brendan Punsky (dotbmp@github), original author
|
|
|
|
package rexcode_arm32
|
|
|
|
// =============================================================================
|
|
// ADVANCED SIMD FIELDS READ FROM THE WHOLE WORD
|
|
// =============================================================================
|
|
//
|
|
// Some Advanced SIMD fields mean different things depending on the rest of the
|
|
// word -- which class the instruction is in, how many elements it moves, its
|
|
// element size. Each form pins all of that, so the meaning follows from the
|
|
// form's bits; the decoder reads it off the word and the encoder off the form.
|
|
// These are shared by both.
|
|
|
|
// The element and structure loads and stores, by bit 23 and the size field:
|
|
// several structures to or from whole registers, one to or from a lane, or one
|
|
// loaded into every lane.
|
|
Structure_Class :: enum u8 { MULTIPLE, ONE_LANE, ALL_LANES }
|
|
|
|
@(require_results)
|
|
structure_class :: #force_inline proc "contextless" (word: u32) -> Structure_Class {
|
|
if word & (1 << 23) == 0 {
|
|
return .MULTIPLE
|
|
}
|
|
if (word >> 10) & 0x3 == 0x3 {
|
|
return .ALL_LANES
|
|
}
|
|
return .ONE_LANE
|
|
}
|
|
|
|
// The register list of an element or structure load or store.
|
|
Structure_List :: struct {
|
|
count: u8, // registers in the list
|
|
stride: u8, // 1 for {d0, d1}, 2 for {d0, d2}
|
|
lane: u8, // for ONE_LANE
|
|
}
|
|
|
|
@(require_results)
|
|
structure_list :: proc "contextless" (word: u32) -> (list: Structure_List) {
|
|
list.stride = 1
|
|
switch structure_class(word) {
|
|
case .MULTIPLE:
|
|
// The type field, bits 11-8, is the count and the spacing at once.
|
|
switch (word >> 8) & 0xF {
|
|
case 0x7: list.count = 1
|
|
case 0xA, 0x8: list.count = 2
|
|
case 0x6, 0x4: list.count = 3
|
|
case 0x2, 0x3: list.count = 4
|
|
case 0x0: list.count = 4
|
|
case 0x9: list.count, list.stride = 2, 2
|
|
case 0x5: list.count, list.stride = 3, 2
|
|
case 0x1: list.count, list.stride = 4, 2
|
|
}
|
|
case .ONE_LANE:
|
|
list.count = u8((word >> 8) & 0x3) + 1
|
|
index_align := (word >> 4) & 0xF
|
|
switch (word >> 10) & 0x3 {
|
|
case 0:
|
|
list.lane = u8(index_align >> 1)
|
|
case 1:
|
|
list.lane = u8(index_align >> 2)
|
|
if list.count > 1 { list.stride = u8((index_align >> 1) & 1) + 1 }
|
|
case 2:
|
|
list.lane = u8(index_align >> 3)
|
|
if list.count > 1 { list.stride = u8((index_align >> 2) & 1) + 1 }
|
|
}
|
|
case .ALL_LANES:
|
|
// T is a second register for VLD1, and the spacing for the rest.
|
|
elements := u8((word >> 8) & 0x3) + 1
|
|
t := u8((word >> 5) & 1)
|
|
if elements == 1 {
|
|
list.count = t + 1
|
|
} else {
|
|
list.count = elements
|
|
list.stride = t + 1
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// The alignment an element or structure address names, as Memory.align holds
|
|
// it: 0 for none, else log2 of the bits less 3.
|
|
@(require_results)
|
|
structure_alignment :: proc "contextless" (word: u32) -> u8 {
|
|
elements := (word >> 8) & 0x3 + 1
|
|
switch structure_class(word) {
|
|
case .MULTIPLE:
|
|
// aa at bits 5-4: none, 64, 128, 256.
|
|
aa := u8((word >> 4) & 0x3)
|
|
return aa == 0 ? 0 : aa + 2
|
|
case .ONE_LANE:
|
|
size := u8((word >> 10) & 0x3)
|
|
index_align := (word >> 4) & 0xF
|
|
switch elements {
|
|
case 1:
|
|
// .16 aligns to 16 by bit 0; .32 to 32 by bits 1-0 = 11.
|
|
if size == 1 && index_align & 1 != 0 { return 1 }
|
|
if size == 2 && index_align & 3 == 3 { return 2 }
|
|
return 0
|
|
case 2:
|
|
// Twice the element.
|
|
return index_align & 1 != 0 ? size + 1 : 0
|
|
case 3:
|
|
return 0
|
|
case 4:
|
|
// .32 to 64 or 128 by bits 1-0; the rest to four elements by bit 0.
|
|
if size == 2 {
|
|
aa := u8(index_align & 3)
|
|
return aa == 0 ? 0 : aa + 2
|
|
}
|
|
return index_align & 1 != 0 ? size + 2 : 0
|
|
}
|
|
case .ALL_LANES:
|
|
if word & 0x10 == 0 {
|
|
return 0
|
|
}
|
|
size := u8((word >> 6) & 0x3)
|
|
switch elements {
|
|
case 1: return size
|
|
case 2: return size + 1
|
|
case 3: return 0
|
|
case 4:
|
|
// Four elements' worth, except that .32 stops at 64, and size 11 is
|
|
// a .32 aligned to 128.
|
|
if size == 3 { return 4 }
|
|
if size == 2 { return 3 }
|
|
return size + 2
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// The element size a shift-by-immediate word's L:imm6 names: its leading one.
|
|
@(require_results)
|
|
shift_element_bits :: #force_inline proc "contextless" (word: u32) -> u32 {
|
|
if word & (1 << 7) != 0 { return 64 }
|
|
if word & (1 << 21) != 0 { return 32 }
|
|
if word & (1 << 20) != 0 { return 16 }
|
|
return 8
|
|
}
|
|
|
|
// L:imm6 as one seven-bit number.
|
|
@(require_results)
|
|
shift_field :: #force_inline proc "contextless" (word: u32) -> u32 {
|
|
return ((word >> 7) & 1) << 6 | (word >> 16) & 0x3F
|
|
}
|