holey-bytes/hbasm/src/macros/asm.rs

72 lines
1.8 KiB
Rust
Raw Normal View History

2023-07-10 16:18:23 -05:00
macro_rules! impl_asm_opcodes {
(
$generic:ident
($($param_i:ident: $param_ty:ty),*)
=> []
) => {};
(
$generic:ident
($($param_i:ident: $param_ty:ty),*)
=> [$opcode:ident, $($rest:tt)*]
) => {
paste::paste! {
#[inline(always)]
pub fn [<i_ $opcode:lower>](&mut self, $($param_i: $param_ty),*) {
self.$generic(hbbytecode::opcode::$opcode, $($param_i),*)
}
}
2023-07-11 19:16:23 -05:00
macros::asm::impl_asm_opcodes!(
2023-07-10 16:18:23 -05:00
$generic($($param_i: $param_ty),*)
=> [$($rest)*]
);
};
}
2023-07-11 19:16:23 -05:00
macro_rules! impl_asm_insert {
($self:expr, $id:ident, I) => {
Imm::insert(&$id, $self)
};
2023-07-10 16:18:23 -05:00
2023-07-11 19:16:23 -05:00
($self:expr, $id:ident, $_:ident) => {
$self.buf.extend($id.to_le_bytes())
2023-07-10 16:18:23 -05:00
};
}
macro_rules! impl_asm {
(
$(
$ityn:ident
2023-07-11 19:16:23 -05:00
($($param_i:ident: $param_ty:ident),* $(,)?)
2023-07-10 16:18:23 -05:00
=> [$($opcode:ident),* $(,)?],
)*
) => {
paste::paste! {
$(
2023-07-11 19:16:23 -05:00
fn [<i_param_ $ityn>](&mut self, opcode: u8, $($param_i: macros::asm::ident_map_ty!($param_ty)),*) {
2023-07-10 16:18:23 -05:00
self.buf.push(opcode);
2023-07-11 19:16:23 -05:00
$(macros::asm::impl_asm_insert!(self, $param_i, $param_ty);)*
2023-07-10 16:18:23 -05:00
}
2023-07-11 19:16:23 -05:00
macros::asm::impl_asm_opcodes!(
[<i_param_ $ityn>]($($param_i: macros::asm::ident_map_ty!($param_ty)),*)
2023-07-10 16:18:23 -05:00
=> [$($opcode,)*]
);
)*
}
};
}
2023-07-11 19:16:23 -05:00
#[rustfmt::skip]
macro_rules! ident_map_ty {
(R) => { u8 };
(I) => { impl Imm };
($id:ident) => { $id };
}
pub(crate) use {ident_map_ty, impl_asm, impl_asm_opcodes};
2023-07-10 16:18:23 -05:00
#[allow(clippy::single_component_path_imports)]
2023-07-11 19:16:23 -05:00
pub(crate) use impl_asm_insert;