From 22d79e0671b344d6ded4281ee7fc526f2c0802a6 Mon Sep 17 00:00:00 2001 From: Junaadh Date: Mon, 5 Aug 2024 03:48:45 +0800 Subject: [PATCH] remove impl_opcode macro since repeation in metavariable expr is unstable rust_issue: https://github.com/rust-lang/rust/issues/83527 --- Cargo.lock | 4 ++++ Cargo.toml | 2 +- jnd/src/errors/asme.rs | 41 +++++++++++++++++++++++++++++++++++++++++ jnd/src/lib.rs | 1 + jnd/src/macros.rs | 1 + jnd/src/op.rs | 26 +++++++++++++++++++------- jnd/src/reg.rs | 32 ++++++++++++++++++++++---------- utils/jop/Cargo.toml | 9 +++++++++ utils/jop/src/lib.rs | 7 +++++++ 9 files changed, 105 insertions(+), 18 deletions(-) create mode 100644 jnd/src/macros.rs create mode 100644 utils/jop/Cargo.toml create mode 100644 utils/jop/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 0cdd7ba..1605540 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -16,3 +16,7 @@ version = "0.1.0" dependencies = [ "jnd", ] + +[[package]] +name = "jop" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 361c935..2bdb9d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,3 @@ [workspace] -members = [ "asm","jnd", "vm"] +members = [ "asm","jnd", "utils/jop", "vm"] resolver = "2" diff --git a/jnd/src/errors/asme.rs b/jnd/src/errors/asme.rs index e69de29..604e3f4 100644 --- a/jnd/src/errors/asme.rs +++ b/jnd/src/errors/asme.rs @@ -0,0 +1,41 @@ +use core::fmt; + +use super::Erroring; + +#[derive(Debug)] +pub enum AsmErr { + ReadFile, + OpenFile, + Parse, + Write, +} + +impl Erroring for AsmErr { + fn err(&self) -> &str { + match self { + Self::ReadFile => "failed to read file to buffer", + Self::OpenFile => "failed to open file", + Self::Parse => "failed to parse token", + Self::Write => "failed to write binary to stdout", + } + } +} + +impl fmt::Display for AsmErr { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.err()) + } +} + +#[macro_export] +macro_rules! asme { + ($err: expr, $kind: ident, $($args:tt)*) => {{ + use $crate::errors::Erroring; + $err.vme(format!("{}: {}", $crate::errors::vme::AsmErr::$kind.err(), format_args!($($args)*)).as_str()) + }}; + + ($kind:ident, $($args:tt)*) => {{ + use $crate::errors::Erroring; + $crate::errors::Jerror::Vme(format!("{}: {}", $crate::errors::vme::AsmErr::$kind.err(), format_args!($($args)*))) + }}; +} diff --git a/jnd/src/lib.rs b/jnd/src/lib.rs index fa50369..28ceac8 100644 --- a/jnd/src/lib.rs +++ b/jnd/src/lib.rs @@ -1,6 +1,7 @@ pub mod disassembler; pub mod errors; pub mod interrupts; +pub mod macros; pub mod mem; pub mod op; pub mod reg; diff --git a/jnd/src/macros.rs b/jnd/src/macros.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/jnd/src/macros.rs @@ -0,0 +1 @@ + diff --git a/jnd/src/op.rs b/jnd/src/op.rs index 8ca9313..bb7b1c6 100644 --- a/jnd/src/op.rs +++ b/jnd/src/op.rs @@ -2,14 +2,14 @@ use crate::{reg::Register, vme, Res}; /// return-eq macro_rules! retq { - ($name: ident, $x: expr) => { - $x == $crate::op::Op::$name.as_u8() - }; + ($name: ident, $x: expr) => { + $x == $crate::op::Op::$name.as_u8() + }; - ($name:ident($($args:tt)*), $x: expr) => { - $x == $crate::op::Op::$name($($args)*).as_u8() - }; -} + ($name:ident($($args:tt)*), $x: expr) => { + $x == $crate::op::Op::$name($($args)*).as_u8() + }; + } #[derive(Debug, PartialEq, PartialOrd)] #[repr(u8)] @@ -66,3 +66,15 @@ impl Op { }) } } + +// impl FromStr for Op { +// type Err = errors::Jerror; + +// fn from_str(s: &str) -> Result { +// let byte = (match s.to_lowercase().as_str() { +// "nop" => Op::Nop, +// "" +// _ => return Err(vme!(UnknownInstruction, "instruction: {s}")), +// }) +// } +// } diff --git a/jnd/src/reg.rs b/jnd/src/reg.rs index b5210b7..3e4d902 100644 --- a/jnd/src/reg.rs +++ b/jnd/src/reg.rs @@ -1,4 +1,4 @@ -use crate::{errors, vme}; +use crate::{errors, op::Op, vme}; #[derive(Debug, PartialEq, PartialOrd)] pub enum Register { @@ -17,14 +17,14 @@ impl TryFrom for Register { fn try_from(value: u8) -> Result { Ok(match value { - x if x == Register::A as u8 => Register::A, - x if x == Register::B as u8 => Register::B, - x if x == Register::C as u8 => Register::C, - x if x == Register::M as u8 => Register::M, - x if x == Register::SP as u8 => Register::SP, - x if x == Register::PC as u8 => Register::PC, - x if x == Register::BP as u8 => Register::BP, - x if x == Register::Flags as u8 => Register::Flags, + 0 => Register::A, + 1 => Register::B, + 2 => Register::C, + 3 => Register::M, + 4 => Register::SP, + 5 => Register::PC, + 6 => Register::BP, + 7 => Register::Flags, _ => return Err(vme!(InvalidRegister, "{}", value)), }) } @@ -34,6 +34,18 @@ impl TryFrom for Register { type Error = errors::Jerror; fn try_from(value: u16) -> Result { - (value as u8).try_into() + ((value & 0xff) as u8).try_into() } } + +#[macro_export] +macro_rules! reg { + ($kind: ident) => {{ + $crate::reg::Register::$kind as u8 + }}; +} + +pub trait Parser { + fn encode(&self) -> u16; + fn decode(&self) -> Op; +} diff --git a/utils/jop/Cargo.toml b/utils/jop/Cargo.toml new file mode 100644 index 0000000..a23b666 --- /dev/null +++ b/utils/jop/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "jop" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true + +[dependencies] diff --git a/utils/jop/src/lib.rs b/utils/jop/src/lib.rs new file mode 100644 index 0000000..761332c --- /dev/null +++ b/utils/jop/src/lib.rs @@ -0,0 +1,7 @@ +extern crate proc_macro; +use proc_macro::TokenStream; + +#[proc_macro_derive(AnswerFn)] +pub fn derive_answer_fn(_item: TokenStream) -> TokenStream { + "fn answer() -> u32 { 42 }".parse().unwrap() +}