risc_v_disassembler/
lib.rs1mod decoder;
61mod instructions;
62mod macros;
63mod parser;
64mod registers;
65
66pub use instructions::{parsed_instructions, ParsedInstruction32};
67use instructions::{DecodeInstruction32, Instruction32, ParseInstruction32};
68pub use registers::Register;
69use thiserror::Error;
70
71pub fn parse(
72 bytes: &[u8],
73 is_big_endian: bool,
74 use_abi_register_names: bool,
75) -> Result<ParsedInstruction32, DisassemblerError> {
76 if bytes.len() != 4 {
77 return Err(DisassemblerError::UnsupportedInstructionLength(bytes.len()));
78 }
79
80 let instruction = if is_big_endian {
81 Instruction32::from_be_bytes(bytes.try_into().unwrap())
82 } else {
83 Instruction32::from_le_bytes(bytes.try_into().unwrap())
84 };
85
86 let decoded_instruction = instruction.decode_instruction32()?;
87
88 let parsed_instruction = if use_abi_register_names {
89 decoded_instruction.parse_instruction32::<registers::ABIRegister>()?
90 } else {
91 decoded_instruction.parse_instruction32::<registers::NumberedRegister>()?
92 };
93
94 Ok(parsed_instruction)
95}
96
97#[derive(Debug, Error, PartialEq)]
98pub enum DisassemblerError {
99 #[error(
100 "Unsupported instruction length: {0}. The length of the instruction is not supported."
101 )]
102 UnsupportedInstructionLength(usize),
103
104 #[error(
105 "Invalid funct3 field with value {0:b}. The value is not valid for the given instruction."
106 )]
107 InvalidFunct3(u8),
108
109 #[error(
110 "Invalid funct7 field with value {0:b}. The value is not valid for the given instruction."
111 )]
112 InvalidFunct7(u8),
113
114 #[error(
115 "Invalid opcode field with value {0:b}. The value is not valid for the given instruction."
116 )]
117 InvalidOpcode(u8),
118
119 #[error(
120 "Invalid immediate: {0:b}. The immediate value is not valid for the given instruction."
121 )]
122 InvalidImmediate(i32),
123
124 #[error("Invalid register: {0:?}. The register index is out of bounds.")]
125 InvalidRegister(u8),
126
127 #[error("Bit extraction error: {0}.")]
128 BitExtractionError(&'static str),
129
130 #[error("Bit extension error: {0}.")]
131 BitExtensionError(&'static str),
132}