From 4dfbe939199d2664920b667903272e98f306bc54 Mon Sep 17 00:00:00 2001 From: Erin Date: Wed, 12 Jul 2023 02:23:47 +0200 Subject: [PATCH] special-cased BRC --- hbasm/src/lib.rs | 10 +++++++++- hbasm/src/macros/asm.rs | 2 -- hbasm/src/macros/text.rs | 21 +++++++++++++++++++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/hbasm/src/lib.rs b/hbasm/src/lib.rs index 0e07fbf..75a5731 100644 --- a/hbasm/src/lib.rs +++ b/hbasm/src/lib.rs @@ -16,7 +16,7 @@ macros::impl_both!( bbbb(p0: R, p1: R, p2: R, p3: R) => [DIR, DIRF, FMAF], bbb(p0: R, p1: R, p2: R) - => [ADD, SUB, MUL, AND, OR, XOR, SL, SR, SRS, CMP, CMPU, BRC, ADDF, SUBF, MULF], + => [ADD, SUB, MUL, AND, OR, XOR, SL, SR, SRS, CMP, CMPU, /*BRC,*/ ADDF, SUBF, MULF], bbdh(p0: R, p1: R, p2: I, p3: u16) => [LD, ST], bbd(p0: R, p1: R, p2: I) @@ -30,6 +30,14 @@ macros::impl_both!( => [NOP, ECALL], ); +impl Assembler { + // Special-cased + #[inline(always)] + pub fn i_brc(&mut self, p0: u8, p1: u8, p2: u8) { + self.i_param_bbb(hbbytecode::opcode::BRC, p0, p1, p2) + } +} + pub trait Imm { fn insert(&self, asm: &mut Assembler); } diff --git a/hbasm/src/macros/asm.rs b/hbasm/src/macros/asm.rs index 2e1c904..fc8366c 100644 --- a/hbasm/src/macros/asm.rs +++ b/hbasm/src/macros/asm.rs @@ -11,7 +11,6 @@ macro_rules! impl_asm_opcodes { => [$opcode:ident, $($rest:tt)*] ) => { paste::paste! { - #[allow(dead_code)] #[inline(always)] pub fn [](&mut self, $($param_i: $param_ty),*) { self.$generic(hbbytecode::opcode::$opcode, $($param_i),*) @@ -45,7 +44,6 @@ macro_rules! impl_asm { ) => { paste::paste! { $( - #[allow(dead_code)] fn [](&mut self, opcode: u8, $($param_i: macros::asm::ident_map_ty!($param_ty)),*) { self.buf.push(opcode); $(macros::asm::impl_asm_insert!(self, $param_i, $param_ty);)* diff --git a/hbasm/src/macros/text.rs b/hbasm/src/macros/text.rs index f297567..93acfae 100644 --- a/hbasm/src/macros/text.rs +++ b/hbasm/src/macros/text.rs @@ -24,6 +24,7 @@ macro_rules! gen_text { #[logos(skip r"-- .*")] pub enum Token { $($(#[token(~([<$opcode:lower>]), |_| hbbytecode::opcode::[<$opcode:upper>])])*)* + #[token("brc", |_| hbbytecode::opcode::BRC)] // Special-cased Opcode(u8), #[regex("[0-9]+", |l| l.slice().parse().ok())] @@ -100,6 +101,17 @@ macro_rules! gen_text { self.asm.[](op, $($param_i),*); }), )* + // Special-cased + hbbytecode::opcode::BRC => { + param_extract_itm!( + self, + p0: R, + p1: R, + p2: u8 + ); + + self.asm.i_param_bbb(op, p0, p1, p2); + } _ => unreachable!(), } } @@ -123,7 +135,7 @@ macro_rules! gen_text { asm.run() .map_err(|kind| Error { kind, span: asm.lexer.span() })?; - + for &loc in &asm.asm.sub { let val = asm.symloc .get( @@ -166,7 +178,7 @@ macro_rules! gen_text { macro_rules! extract_pat { ($self:expr, $pat:pat) => { let $pat = $self.next()? - else { return Err(ErrorKind::UnexpectedToken) }; + else { return Err(ErrorKind::UnexpectedToken) }; }; } @@ -183,6 +195,11 @@ macro_rules! extract { }; }; + ($self:expr, u8, $id:ident) => { + extract_pat!($self, Token::Integer($id)); + let $id = u8::try_from($id).map_err(|_| ErrorKind::InvalidToken)?; + }; + ($self:expr, u16, $id:ident) => { extract_pat!($self, Token::Integer($id)); let $id = u16::try_from($id).map_err(|_| ErrorKind::InvalidToken)?;