This commit is contained in:
Richard Patel 2022-04-06 14:07:57 +02:00
parent dff1075737
commit 171666e6fc
3 changed files with 205 additions and 133 deletions

View File

@ -1,6 +1,4 @@
use std::fmt::{Display, Formatter, LowerHex, UpperHex};
use num_traits::PrimInt;
use std::fmt::{Display, Formatter};
use crate::prelude::*;
@ -8,22 +6,18 @@ pub struct FormattedIns(pub Ins);
impl Display for FormattedIns {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.fmt_ins(f)
}
}
impl FormattedIns {
fn fmt_ins(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{} ", self.0.op.mnemonic(), self.0.modifiers())?;
let fields = self.0.fields();
let mut writing_offset = false;
for (i, field) in fields.iter().enumerate() {
if let offset(o) = field {
if let Some(argument) = field.argument() {
write!(f, "{}", argument)?;
}
if let offset(_) = field {
write!(f, "(")?;
writing_offset = true;
write!(f, "{:#x}(", ReallySigned(o.0))?;
continue;
}
Self::fmt_field(field, f)?;
if writing_offset {
write!(f, ")")?;
writing_offset = false;
@ -34,109 +28,4 @@ impl FormattedIns {
}
Ok(())
}
fn fmt_field(field: &Field, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match field {
simm(s) => Self::fmt_simm(*s, f),
uimm(u) => Self::fmt_uimm(*u, f),
BO(o) => Self::fmt_opaque_u(*o, f),
BI(o) => Self::fmt_opaque_u(*o, f),
BD(bd) => Self::fmt_branch_dest(*bd, f),
LI(bd) => Self::fmt_branch_dest(*bd, f),
SH(o) => Self::fmt_opaque_u(*o, f),
MB(o) => Self::fmt_opaque_u(*o, f),
ME(o) => Self::fmt_opaque_u(*o, f),
rS(gpr) => Self::fmt_gpr(*gpr, f),
rD(gpr) => Self::fmt_gpr(*gpr, f),
rA(gpr) => Self::fmt_gpr(*gpr, f),
rB(gpr) => Self::fmt_gpr(*gpr, f),
rC(gpr) => Self::fmt_gpr(*gpr, f),
sr(s) => Self::fmt_sr(*s, f),
spr(s) => Self::fmt_spr(*s, f),
frS(fpr) => Self::fmt_fpr(*fpr, f),
frD(fpr) => Self::fmt_fpr(*fpr, f),
frA(fpr) => Self::fmt_fpr(*fpr, f),
frB(fpr) => Self::fmt_fpr(*fpr, f),
frC(fpr) => Self::fmt_fpr(*fpr, f),
crbD(crb) => Self::fmt_crb(*crb, f),
crbA(crb) => Self::fmt_crb(*crb, f),
crbB(crb) => Self::fmt_crb(*crb, f),
crfD(crf) => Self::fmt_crf(*crf, f),
crfS(crf) => Self::fmt_crf(*crf, f),
crm(o) => Self::fmt_opaque_u(*o, f),
ps_l(gqr) => Self::fmt_gqr(*gqr, f),
ps_W(o) => Self::fmt_opaque_u(*o, f),
NB(o) => Self::fmt_opaque_u(*o, f),
tbr(o) => Self::fmt_opaque_u(*o, f),
mtfsf_FM(o) => Self::fmt_opaque_u(*o, f),
mtfsf_IMM(o) => Self::fmt_opaque_u(*o, f),
tw_TO(o) => Self::fmt_opaque_u(*o, f),
_ => Ok(()),
}
}
fn fmt_gpr(gpr: GPR, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "r{}", gpr.0)
}
fn fmt_fpr(gpr: FPR, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "fr{}", gpr.0)
}
fn fmt_opaque_u(u: OpaqueU, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", u.0)
}
fn fmt_gqr(gqr: GQR, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", gqr.0)
}
fn fmt_spr(s: SPR, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", s.0)
}
fn fmt_sr(s: SR, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", s.0)
}
fn fmt_crb(crb: CRField, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", crb.0)
}
fn fmt_crf(crf: CRBit, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", crf.0)
}
fn fmt_branch_dest(bd: BranchDest, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:#x}", ReallySigned(bd.0))
}
fn fmt_uimm(u: Uimm, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:#x}", u.0)
}
fn fmt_simm(s: Simm, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:#x}", ReallySigned(s.0))
}
}
// https://stackoverflow.com/questions/44711012/how-do-i-format-a-signed-integer-to-a-sign-aware-hexadecimal-representation
struct ReallySigned<N: PrimInt>(N);
impl<N: PrimInt> LowerHex for ReallySigned<N> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let num = self.0.to_i32().unwrap();
let prefix = if f.alternate() { "0x" } else { "" };
let bare_hex = format!("{:x}", num.abs());
f.pad_integral(num >= 0, prefix, &bare_hex)
}
}
impl<N: PrimInt> UpperHex for ReallySigned<N> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let num = self.0.to_i32().unwrap();
let prefix = if f.alternate() { "0x" } else { "" };
let bare_hex = format!("{:X}", num.abs());
f.pad_integral(num >= 0, prefix, &bare_hex)
}
}

View File

@ -1,7 +1,7 @@
use std::fmt::Formatter;
use std::fmt::{Display, Formatter, LowerHex, UpperHex, Write};
use std::ops::Range;
use num_traits::AsPrimitive;
use num_traits::{AsPrimitive, PrimInt};
use ppc750cl_macros::{fields, ins_impl, opcodes};
@ -21,10 +21,42 @@ pub mod prelude {
};
}
macro_rules! field_arg {
macro_rules! field_arg_no_display {
($name:ident, $typ:ident) => {
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct $name(pub $typ);
impl std::convert::From<$name> for Argument {
fn from(x: $name) -> Argument {
Argument::$name(x)
}
}
};
}
macro_rules! field_arg {
($name:ident, $typ:ident) => {
field_arg_no_display!($name, $typ);
impl Display for $name {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
};
($name:ident, $typ:ident, $format:literal) => {
field_arg_no_display!($name, $typ);
impl Display for $name {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, $format, self.0)
}
}
};
($name:ident, $typ:ident, $format:literal, $format_arg:expr) => {
field_arg_no_display!($name, $typ);
impl Display for $name {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, $format, $format_arg(self.0))
}
}
};
}
@ -43,36 +75,142 @@ where
masked.as_()
}
// https://stackoverflow.com/questions/44711012/how-do-i-format-a-signed-integer-to-a-sign-aware-hexadecimal-representation
struct ReallySigned<N: PrimInt>(N);
impl<N: PrimInt> LowerHex for ReallySigned<N> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let num = self.0.to_i32().unwrap();
let prefix = if f.alternate() { "0x" } else { "" };
let bare_hex = format!("{:x}", num.abs());
f.pad_integral(num >= 0, prefix, &bare_hex)
}
}
impl<N: PrimInt> UpperHex for ReallySigned<N> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let num = self.0.to_i32().unwrap();
let prefix = if f.alternate() { "0x" } else { "" };
let bare_hex = format!("{:X}", num.abs());
f.pad_integral(num >= 0, prefix, &bare_hex)
}
}
// General-purpose register.
field_arg!(GPR, u8);
field_arg!(GPR, u8, "r{}");
// Floating-point register (direct or paired-singles mode).
field_arg!(FPR, u8);
field_arg!(FPR, u8, "fr{}");
// Segment register.
field_arg!(SR, u8);
// Special-purpose register.
field_arg!(SPR, u16);
// Condition register field.
field_arg!(CRField, u8);
field_arg!(CRField, u8, "crb{}");
// Condition register bit (index + condition case).
field_arg!(CRBit, u8);
field_arg!(CRBit, u8, "crf{}");
// Paired-single graphics quantization register
field_arg!(GQR, u8);
// Unsigned immediate.
field_arg!(Uimm, u16);
field_arg!(Uimm, u16, "{:#x}");
// Signed immediate.
field_arg!(Simm, i16);
field_arg!(Simm, i16, "{:#x}", ReallySigned);
// Offset for indirect memory reference.
field_arg!(Offset, u32);
field_arg!(Offset, i32, "{:#x}", ReallySigned);
// Branch destination.
field_arg!(BranchDest, u32);
field_arg!(BranchDest, i32, "{:#x}", ReallySigned);
// Opaque zero or one argument.
field_arg!(Bit, bool);
field_arg_no_display!(Bit, bool);
impl Display for Bit {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_char(if self.0 { '1' } else { '0' })
}
}
// Unsigned opaque argument.
field_arg!(OpaqueU, u32);
#[derive(Debug, Clone)]
pub enum Argument {
GPR(GPR),
FPR(FPR),
SR(SR),
SPR(SPR),
CRField(CRField),
CRBit(CRBit),
GQR(GQR),
Uimm(Uimm),
Simm(Simm),
Offset(Offset),
BranchDest(BranchDest),
Bit(Bit),
OpaqueU(OpaqueU),
}
impl Display for Argument {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Argument::GPR(x) => x.fmt(f),
Argument::FPR(x) => x.fmt(f),
Argument::SR(x) => x.fmt(f),
Argument::SPR(x) => x.fmt(f),
Argument::CRField(x) => x.fmt(f),
Argument::CRBit(x) => x.fmt(f),
Argument::GQR(x) => x.fmt(f),
Argument::Uimm(x) => x.fmt(f),
Argument::Simm(x) => x.fmt(f),
Argument::Offset(x) => x.fmt(f),
Argument::BranchDest(x) => x.fmt(f),
Argument::Bit(x) => x.fmt(f),
Argument::OpaqueU(x) => x.fmt(f),
}
}
}
// Generate the Field enum and impls.
fields!();
impl Field {
pub fn argument(&self) -> Option<Argument> {
match self {
Field::simm(x) => Some(Argument::Simm(*x)),
Field::uimm(x) => Some(Argument::Uimm(*x)),
Field::offset(x) => Some(Argument::Offset(*x)),
Field::BO(x) => Some(Argument::OpaqueU(*x)),
Field::BI(x) => Some(Argument::OpaqueU(*x)),
Field::BD(x) => Some(Argument::BranchDest(*x)),
Field::LI(x) => Some(Argument::BranchDest(*x)),
Field::SH(x) => Some(Argument::OpaqueU(*x)),
Field::MB(x) => Some(Argument::OpaqueU(*x)),
Field::ME(x) => Some(Argument::OpaqueU(*x)),
Field::rS(x) => Some(Argument::GPR(*x)),
Field::rD(x) => Some(Argument::GPR(*x)),
Field::rA(x) => Some(Argument::GPR(*x)),
Field::rB(x) => Some(Argument::GPR(*x)),
Field::rC(x) => Some(Argument::GPR(*x)),
Field::sr(x) => Some(Argument::SR(*x)),
Field::spr(x) => Some(Argument::SPR(*x)),
Field::frS(x) => Some(Argument::FPR(*x)),
Field::frD(x) => Some(Argument::FPR(*x)),
Field::frA(x) => Some(Argument::FPR(*x)),
Field::frB(x) => Some(Argument::FPR(*x)),
Field::frC(x) => Some(Argument::FPR(*x)),
Field::crbD(x) => Some(Argument::CRField(*x)),
Field::crbA(x) => Some(Argument::CRField(*x)),
Field::crbB(x) => Some(Argument::CRField(*x)),
Field::crfD(x) => Some(Argument::CRBit(*x)),
Field::crfS(x) => Some(Argument::CRBit(*x)),
Field::crm(x) => Some(Argument::OpaqueU(*x)),
Field::ps_l(x) => Some(Argument::GQR(*x)),
Field::ps_W(x) => Some(Argument::OpaqueU(*x)),
Field::NB(x) => Some(Argument::OpaqueU(*x)),
Field::tbr(x) => Some(Argument::OpaqueU(*x)),
Field::mtfsf_FM(x) => Some(Argument::OpaqueU(*x)),
Field::mtfsf_IMM(x) => Some(Argument::OpaqueU(*x)),
Field::tw_TO(x) => Some(Argument::OpaqueU(*x)),
_ => None,
}
}
}
#[derive(Debug, Default)]
pub struct Modifiers {
pub oe: bool,
@ -146,6 +284,11 @@ impl Ins {
}
}
/// Returns the simplified representation of an instruction.
pub fn simplified(self) -> SimplifiedIns {
self._simplified() // auto-generated
}
/// Gets the fields of an instruction.
pub fn fields(&self) -> Vec<Field> {
self._fields() // auto-generated
@ -178,3 +321,34 @@ impl Ins {
}
ins_impl!();
/// A simplified PowerPC 750CL instruction.
pub struct SimplifiedIns {
pub ins: Ins,
pub mnemonic: &'static str,
pub modifiers: Modifiers,
pub args: Vec<Argument>,
}
impl Display for SimplifiedIns {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{} ", self.mnemonic, self.modifiers)?;
let mut writing_offset = false;
for (i, argument) in self.args.iter().enumerate() {
write!(f, "{}", argument)?;
if let Argument::Offset(_) = argument {
write!(f, "(")?;
writing_offset = true;
continue;
}
if writing_offset {
write!(f, ")")?;
writing_offset = false;
}
if i != self.args.len() - 1 {
write!(f, ", ")?;
}
}
Ok(())
}
}

View File

@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::iter::FromIterator;
use std::ops::Range;
@ -5,7 +6,6 @@ use itertools::Itertools;
use proc_macro2::{Ident, Literal, Span, TokenStream, TokenTree};
use quote::quote;
use serde::{Deserialize, Deserializer};
use std::collections::HashMap;
use syn::LitInt;
macro_rules! token_stream {
@ -195,7 +195,7 @@ impl Isa {
.map(|opcode| {
let variant = opcode.variant_identifier()?;
let literal =
Literal::string(&opcode.name.strip_suffix(".").unwrap_or(&opcode.name));
Literal::string(opcode.name.strip_suffix('.').unwrap_or(&opcode.name));
Ok(quote! {
Opcode::#variant => #literal,
})
@ -325,7 +325,7 @@ impl Isa {
let set_modifiers = token_stream!(set_modifiers);
modifier_match_arms.push(quote! {
Opcode::#ident => {
let mut m: Modifiers = std::default::Default::default();
let mut m = Modifiers::default();
#set_modifiers
m
}
@ -419,11 +419,20 @@ impl Isa {
fn _modifiers(&self) -> Modifiers {
match self.op {
Opcode::Illegal => std::default::Default::default(),
Opcode::Illegal => Modifiers::default(),
#modifier_match_arms
_ => todo!()
}
}
fn _simplified(self) -> SimplifiedIns {
SimplifiedIns {
mnemonic: self.op.mnemonic(),
modifiers: self._modifiers(),
args: vec![],
ins: self,
}
}
}
};
Ok(ins_impl)