Skip to content

Commit

Permalink
remove impl_opcode macro since repeation in metavariable expr is unst…
Browse files Browse the repository at this point in the history
…able

rust_issue: rust-lang/rust#83527
  • Loading branch information
junaadh committed Aug 4, 2024
1 parent 41cf5a4 commit 22d79e0
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 18 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
members = [ "asm","jnd", "vm"]
members = [ "asm","jnd", "utils/jop", "vm"]
resolver = "2"
41 changes: 41 additions & 0 deletions jnd/src/errors/asme.rs
Original file line number Diff line number Diff line change
@@ -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)*)))
}};
}
1 change: 1 addition & 0 deletions jnd/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
1 change: 1 addition & 0 deletions jnd/src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

26 changes: 19 additions & 7 deletions jnd/src/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -66,3 +66,15 @@ impl Op {
})
}
}

// impl FromStr for Op {
// type Err = errors::Jerror;

// fn from_str(s: &str) -> Result<Self, Self::Err> {
// let byte = (match s.to_lowercase().as_str() {
// "nop" => Op::Nop,
// ""
// _ => return Err(vme!(UnknownInstruction, "instruction: {s}")),
// })
// }
// }
32 changes: 22 additions & 10 deletions jnd/src/reg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{errors, vme};
use crate::{errors, op::Op, vme};

#[derive(Debug, PartialEq, PartialOrd)]
pub enum Register {
Expand All @@ -17,14 +17,14 @@ impl TryFrom<u8> for Register {

fn try_from(value: u8) -> Result<Self, Self::Error> {
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)),
})
}
Expand All @@ -34,6 +34,18 @@ impl TryFrom<u16> for Register {
type Error = errors::Jerror;

fn try_from(value: u16) -> Result<Self, Self::Error> {
(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;
}
9 changes: 9 additions & 0 deletions utils/jop/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "jop"
version = "0.1.0"
edition = "2021"

[lib]
proc-macro = true

[dependencies]
7 changes: 7 additions & 0 deletions utils/jop/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 22d79e0

Please sign in to comment.