superh
superh is a no_std decoder, formatter, and analysis library for the 16-bit
SH-1, SH-2, SH-3, and SH-4 instruction sets. Decode behavior is generated from
generator/assets/isa.yaml; unknown words and raw data are deliberately not
represented as valid instructions.
Decode and display
Decoding is location-independent. Attach an address only when resolving or formatting PC-relative operands.
use ;
let result = decode;
let Instruction = result else ;
assert_eq!;
assert_eq!;
assert_eq!;
Typed instruction values can be encoded directly without a textual assembler.
For every valid encoding, decoding and encoding preserves the exact 16-bit word.
Encoding returns None if an operand in a manually constructed instruction does
not fit that variant's bit field.
Unknown encodings retain the original word:
use ;
let result = decode;
assert_eq!;
assert_eq!;
Every valid instruction exposes a stable, non-reused OpcodeId. The ID can be
stored by downstream tools and checked with Opcode::from_id. If a consumer
already has that opcode, Opcode::decode reconstructs the typed operands without
repeating the full opcode search. It still validates the word and selected
architecture, returning None if either does not match.
use ;
let instruction = MovRmRn
.decode
.expect;
assert_eq!;
Streaming parser
Parser yields source offset, mapped address, byte size, the original word,
and the decode result. Data mode yields a separate Data type.
use ;
let bytes = ;
let mut parser = new;
parser.set_address;
for item in parser
Seeking changes only the buffer offset; the mapped address remains
base_address + offset, with 32-bit wrapping semantics.
Effects and control flow
Effects distinguish resources that must be accessed from resources that may be accessed under an unknown SH-4 FPSCR mode. They also report memory accesses and control flow without allocating.
use ;
let Instruction = decode else ;
let effects = ins.effects;
assert!;
assert!;
ins.at(address).branch_target() resolves direct branches, while
pc_relative_address() identifies literal-pool references for mov.w,
mov.l, and mova.
Structured formatting
FormatIns separates mnemonic and operand rendering and provides typed hooks
for registers, immediates, displacements, PC-relative addresses, and branches.
use Write as _;
use ;
let Instruction = decode else ;
let mut formatter = Formatter ;
formatter.write_ins.expect;
assert_eq!;
Architecture selection
Cargo features are additive and control code size. DecodeOptions::architecture
selects one of the architectures compiled into the build.
| Feature | Compiled instruction sets |
|---|---|
sh1 |
SH-1 |
sh2 |
SH-1, SH-2 |
sh3 |
SH-1, SH-2, SH-3 |
sh4 |
SH-1, SH-2, SH-3, SH-4 |
The default enables all four. A build with no architecture feature is rejected with a targeted compiler diagnostic.
Development gates
The independent ISA audit is sourced from the Renesas SH-1/SH-2/SH-DSP Software Manual (REJ09B0171), SH-3/SH-3E/SH3-DSP Software Manual Rev. 4.00, and SH-4 Software Manual (REJ09B0318). SH-4A-only encodings are not accepted as SH-4.