Add a TargetIsa::display_enc() method.
The interpretation of an encoding depends on the target ISA, and so does the proper printing of the encoding.
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
//! The `Encoding` struct.
|
//! The `Encoding` struct.
|
||||||
|
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
/// Bits needed to encode an instruction as binary machine code.
|
/// Bits needed to encode an instruction as binary machine code.
|
||||||
///
|
///
|
||||||
/// The encoding consists of two parts, both specific to the target ISA: An encoding *recipe*, and
|
/// The encoding consists of two parts, both specific to the target ISA: An encoding *recipe*, and
|
||||||
@@ -30,4 +32,47 @@ impl Encoding {
|
|||||||
pub fn bits(self) -> u16 {
|
pub fn bits(self) -> u16 {
|
||||||
self.bits
|
self.bits
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Is this a legal encoding, or the default placeholder?
|
||||||
|
pub fn is_legal(self) -> bool {
|
||||||
|
self != Self::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The default encoding is the illegal one.
|
||||||
|
impl Default for Encoding {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new(0xffff, 0xffff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// ISA-independent display of an encoding.
|
||||||
|
impl fmt::Display for Encoding {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
if self.is_legal() {
|
||||||
|
write!(f, "#{}/{:02x}", self.recipe, self.bits)
|
||||||
|
} else {
|
||||||
|
write!(f, "-")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Temporary object that holds enough context to properly display an encoding.
|
||||||
|
/// This is meant to be created by `TargetIsa::display_enc()`.
|
||||||
|
pub struct DisplayEncoding {
|
||||||
|
pub encoding: Encoding,
|
||||||
|
pub recipe_names: &'static [&'static str],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for DisplayEncoding {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
if self.encoding.is_legal() {
|
||||||
|
write!(f,
|
||||||
|
"{}/{:02x}",
|
||||||
|
self.recipe_names[self.encoding.recipe()],
|
||||||
|
self.encoding.bits)
|
||||||
|
} else {
|
||||||
|
write!(f, "-")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -101,4 +101,12 @@ pub trait TargetIsa {
|
|||||||
///
|
///
|
||||||
/// This is just used for printing and parsing encodings in the textual IL format.
|
/// This is just used for printing and parsing encodings in the textual IL format.
|
||||||
fn recipe_names(&self) -> &'static [&'static str];
|
fn recipe_names(&self) -> &'static [&'static str];
|
||||||
|
|
||||||
|
/// Create an object that can display an ISA-dependent encoding properly.
|
||||||
|
fn display_enc(&self, enc: Encoding) -> encoding::DisplayEncoding {
|
||||||
|
encoding::DisplayEncoding {
|
||||||
|
encoding: enc,
|
||||||
|
recipe_names: self.recipe_names(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ mod tests {
|
|||||||
use ir::{types, immediates};
|
use ir::{types, immediates};
|
||||||
|
|
||||||
fn encstr(isa: &isa::TargetIsa, enc: isa::Encoding) -> String {
|
fn encstr(isa: &isa::TargetIsa, enc: isa::Encoding) -> String {
|
||||||
format!("{}/{:02x}", isa.recipe_names()[enc.recipe()], enc.bits())
|
isa.display_enc(enc).to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Reference in New Issue
Block a user