diff --git a/src/arch/common.rs b/src/arch/common.rs new file mode 100644 index 0000000..8710a3e --- /dev/null +++ b/src/arch/common.rs @@ -0,0 +1,31 @@ +// Copyright (c) 2015, edef +// See the LICENSE file included in this distribution. +use core::prelude::*; +use core::mem::{size_of, align_of}; +use core::cmp::max; +use core::ptr; + +use super::STACK_ALIGN; + +pub unsafe extern "C" fn rust_trampoline(f: *const F) { + ptr::read(f)() +} + +pub unsafe fn push(spp: &mut *mut usize, value: T) -> *mut T { + let mut sp = *spp as *mut T; + sp = offset_mut(sp, -1); + sp = align_down_mut(sp, max(align_of::(), STACK_ALIGN)); + *sp = value; + *spp = sp as *mut usize; + sp +} + +pub fn align_down_mut(sp: *mut T, n: usize) -> *mut T { + let sp = (sp as usize) & !(n - 1); + sp as *mut T +} + +// ptr::offset_mut is positive ints only +pub fn offset_mut(ptr: *mut T, count: isize) -> *mut T { + (ptr as isize + count * (size_of::() as isize)) as *mut T +} diff --git a/src/arch/mod.rs b/src/arch/mod.rs index 1e5b3c6..128c7a3 100644 --- a/src/arch/mod.rs +++ b/src/arch/mod.rs @@ -1,6 +1,8 @@ // Copyright (c) 2015, edef // See the LICENSE file included in this distribution. -pub use self::imp::Registers; +pub use self::imp::{Registers, STACK_ALIGN}; + +mod common; #[cfg(target_arch = "x86_64")] #[path = "x86_64/mod.rs"] diff --git a/src/arch/x86_64/mod.rs b/src/arch/x86_64/mod.rs index 301e050..3f1b7a7 100644 --- a/src/arch/x86_64/mod.rs +++ b/src/arch/x86_64/mod.rs @@ -1,11 +1,11 @@ // Copyright (c) 2015, edef // See the LICENSE file included in this distribution. use core::prelude::*; -use core::mem::{size_of, align_of}; -use core::cmp::max; -use core::ptr; use stack::Stack; +use super::common::{push, rust_trampoline}; + +pub const STACK_ALIGN: usize = 16; #[allow(raw_pointer_derive)] #[derive(Copy, Clone)] @@ -47,26 +47,3 @@ impl Registers { : "volatile"); } } - -unsafe extern "C" fn rust_trampoline(f: *const F) { - ptr::read(f)() -} - -unsafe fn push(spp: &mut *mut usize, value: T) -> *mut T { - let mut sp = *spp as *mut T; - sp = offset_mut(sp, -1); - sp = align_down_mut(sp, max(align_of::(), 16)); - *sp = value; - *spp = sp as *mut usize; - sp -} - -fn align_down_mut(sp: *mut T, n: usize) -> *mut T { - let sp = (sp as usize) & !(n - 1); - sp as *mut T -} - -// ptr::offset_mut is positive ints only -pub fn offset_mut(ptr: *mut T, count: isize) -> *mut T { - (ptr as isize + count * (size_of::() as isize)) as *mut T -}