From 5a06b04a6aff15fc56165d1631f1921351decea6 Mon Sep 17 00:00:00 2001 From: edef Date: Wed, 24 Dec 2014 06:22:49 +0100 Subject: [PATCH] factor macros out and generalise them --- src/arch.rs | 32 +------------------------------- src/lib.rs | 3 +++ src/macros.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 31 deletions(-) create mode 100644 src/macros.rs diff --git a/src/arch.rs b/src/arch.rs index a305e93..98943d3 100644 --- a/src/arch.rs +++ b/src/arch.rs @@ -50,36 +50,6 @@ impl Registers { } } -macro_rules! into_regs_ { - ($regs:ident { $reg:ident } <- $iter:ident) => { - match $iter.next() { - Some(value) => { - $regs.$reg = *value; - Some($iter) - } - None => None - } - }; - ($regs:ident { $reg:ident, $($regs_rest:ident),* } <- $iter:ident) => { - match $iter.next() { - Some(value) => { - $regs.$reg = *value; - into_regs_!($regs { $($regs_rest),* } <- $iter) - } - None => None - } - }; -} - -macro_rules! into_regs { - ($regs:ident { $($regs_rest:ident),* } <- $iter:expr) => { - { - let mut iter = $iter; - into_regs_!($regs { $($regs_rest),* } <- iter) - } - } -} - pub fn initialise_call_frame(stack: &mut Stack, init: uintptr_t, args: &[uintptr_t]) -> Registers { let sp = stack.top() as *mut uintptr_t; let sp = align_down_mut(sp, 16); @@ -96,7 +66,7 @@ pub fn initialise_call_frame(stack: &mut Stack, init: uintptr_t, args: &[uintptr .. Registers::new() }; - match into_regs!(regs { rdi, r13, r14, r15 } <- args.iter()) { + match into_fields!(regs { rdi, r13, r14, r15 } <- args.iter()) { Some(mut args) => if args.next().is_some() { panic!("too many arguments") }, diff --git a/src/lib.rs b/src/lib.rs index 474b5bf..038c394 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,9 @@ extern crate fn_box; pub use context::Context; +#[macro_escape] +mod macros; + mod context; mod stack; diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..403560b --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,29 @@ +macro_rules! __into_fields { + ($x:ident { $field:ident } <- $iter:ident) => { + match $iter.next() { + Some(value) => { + $x.$field = *value; + Some($iter) + } + None => None + } + }; + ($x:ident { $field:ident, $($fields_rest:ident),* } <- $iter:ident) => { + match $iter.next() { + Some(value) => { + $x.$field = *value; + __into_fields!($x { $($fields_rest),* } <- $iter) + } + None => None + } + }; +} + +macro_rules! into_fields { + ($x:ident { $($fields_rest:ident),* } <- $iter:expr) => { + { + let mut iter = $iter; + __into_fields!($x { $($fields_rest),* } <- iter) + } + } +}