move all the common arch stuff into a module of its own

This commit is contained in:
edef 2015-04-16 06:34:03 -04:00
parent b7624aa49a
commit f7ab28de4b
3 changed files with 37 additions and 27 deletions

31
src/arch/common.rs Normal file
View File

@ -0,0 +1,31 @@
// Copyright (c) 2015, edef <edef@edef.eu>
// 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: FnOnce()>(f: *const F) {
ptr::read(f)()
}
pub unsafe fn push<T>(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::<T>(), STACK_ALIGN));
*sp = value;
*spp = sp as *mut usize;
sp
}
pub fn align_down_mut<T>(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<T>(ptr: *mut T, count: isize) -> *mut T {
(ptr as isize + count * (size_of::<T>() as isize)) as *mut T
}

View File

@ -1,6 +1,8 @@
// Copyright (c) 2015, edef <edef@edef.eu>
// 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"]

View File

@ -1,11 +1,11 @@
// Copyright (c) 2015, edef <edef@edef.eu>
// 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: FnOnce()>(f: *const F) {
ptr::read(f)()
}
unsafe fn push<T>(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::<T>(), 16));
*sp = value;
*spp = sp as *mut usize;
sp
}
fn align_down_mut<T>(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<T>(ptr: *mut T, count: isize) -> *mut T {
(ptr as isize + count * (size_of::<T>() as isize)) as *mut T
}