From 3dcd2f8e58f7cca639b0b4cae905db18c87f40d7 Mon Sep 17 00:00:00 2001 From: Jakob Stoklund Olesen Date: Thu, 7 Apr 2016 13:49:35 -0700 Subject: [PATCH] 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. --- meta/gen_instr.py | 10 +++++----- src/libcretonne/immediates.rs | 9 ++++++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/meta/gen_instr.py b/meta/gen_instr.py index cd50b4ee8a..b0fe4686f4 100644 --- a/meta/gen_instr.py +++ b/meta/gen_instr.py @@ -36,11 +36,11 @@ 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 {', '})'): - for i in instrs: - fmt.format('Opcode::{} => "{}",', i.camel_name, i.name) + # 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) fmt.update_file('opcodes.rs', out_dir) diff --git a/src/libcretonne/immediates.rs b/src/libcretonne/immediates.rs index 0034139f50..4b9adcc43a 100644 --- a/src/libcretonne/immediates.rs +++ b/src/libcretonne/immediates.rs @@ -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)]