Generate an opcode_name() function.

This function returning a &'static str is more primitive that the Display
implementation. It allows the opcode strings to be reused by the parser.
This commit is contained in:
Jakob Stoklund Olesen
2016-04-07 13:49:35 -07:00
parent b0c0dd1b9f
commit 85248a4b18
2 changed files with 13 additions and 6 deletions

View File

@@ -8,9 +8,16 @@
use std::fmt::{self, Display, Formatter};
use std::mem;
// The `Opcode` enum is generated from the meta instruction descriptions.
// The `Opcode` enum and the `opcode_name` function are generated from the meta instruction
// descriptions.
include!(concat!(env!("OUT_DIR"), "/opcodes.rs"));
impl Display for Opcode {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", opcode_name(*self))
}
}
/// 64-bit immediate integer operand.
///
#[derive(Copy, Clone, PartialEq, Eq, Debug)]

View File

@@ -36,9 +36,9 @@ def gen_opcodes(groups, out_dir):
# Enum variant itself.
fmt.line(i.camel_name + ',')
with fmt.indented('impl Display for Opcode {', '}'):
with fmt.indented('fn fmt(&self, f: &mut Formatter) -> fmt::Result {', '}'):
with fmt.indented('f.write_str(match *self {', '})'):
# Generate a private opcode_name function.
with fmt.indented('fn opcode_name(opc: Opcode) -> &\'static str {', '}'):
with fmt.indented('match opc {', '}'):
for i in instrs:
fmt.format('Opcode::{} => "{}",', i.camel_name, i.name)