Generate an InstructionFormat enum.

This is a no-payload enum which will have the same variants as InstructionData.
This makes it possible to talk about the format of an instruction without
actually creating an InstructionData instance.
This commit is contained in:
Jakob Stoklund Olesen
2016-05-13 11:54:05 -07:00
parent 4109b9fe13
commit 593b7bbd51
4 changed files with 57 additions and 7 deletions

View File

@@ -23,6 +23,17 @@ impl Display for Opcode {
}
}
impl Opcode {
/// Get the instruction format for this opcode.
pub fn format(self) -> Option<InstructionFormat> {
if self == Opcode::NotAnOpcode {
None
} else {
Some(OPCODE_FORMAT[self as usize - 1])
}
}
}
// A primitive hash function for matching opcodes.
// Must match `meta/constant_hash.py`.
fn simple_hash(s: &str) -> u32 {
@@ -491,6 +502,7 @@ mod tests {
assert!(x != y);
y = Opcode::Iadd;
assert_eq!(x, y);
assert_eq!(x.format(), Some(InstructionFormat::Binary));
assert_eq!(format!("{:?}", Opcode::IaddImm), "IaddImm");
assert_eq!(Opcode::IaddImm.to_string(), "iadd_imm");