1
0
Fork 0

Factor out common code between x86 and x86_64

Fix #17
This commit is contained in:
John Ericson 2016-04-10 22:15:01 -07:00 committed by edef
parent c51290f8e7
commit 35fb046fb5
3 changed files with 91 additions and 85 deletions

View File

@ -1,48 +1,33 @@
// This file is part of libfringe, a low-level green threading library.
// Copyright (c) edef <edef@edef.eu>
// See the LICENSE file included in this distribution.
use void::Void;
pub use self::common::*;
use stack::Stack;
use super::common::{push, rust_trampoline};
pub const STACK_ALIGN: usize = 16;
#[derive(Debug)]
pub struct Registers {
esp: *mut usize
macro_rules! init {
($sp:expr, $f_ptr:expr, $tramp:expr) => {
asm!(include_str!("x86/init.s")
: "={eax}"($sp)
: "{eax}" ($sp),
"{ebx}" ($tramp),
"{ecx}" ($f_ptr)
:
: "volatile")
};
}
impl Registers {
#[inline]
pub unsafe fn new<S, F>(stack: &mut S, f: F) -> Registers
where S: Stack, F: FnOnce() -> Void {
let mut sp = stack.top() as *mut usize;
let f_ptr = push(&mut sp, f);
asm!(include_str!("init.s")
: "={eax}"(sp)
: "{eax}" (sp),
"{ebx}" (rust_trampoline::<F> as unsafe extern "C" fn(*const F) -> !),
"{ecx}" (f_ptr)
macro_rules! swap {
($out_spp:expr, $in_spp:expr) => {
asm!(include_str!("x86/swap.s")
:
: "volatile");
Registers { esp: sp }
}
#[inline(always)]
pub unsafe fn swap(out_regs: *mut Registers, in_regs: *const Registers) {
let out_espp = &mut (*out_regs).esp;
let in_espp = &(*in_regs).esp;
asm!(include_str!("swap.s")
:
: "{eax}" (out_espp),
"{ebx}" (in_espp)
: "{eax}" ($out_spp),
"{ebx}" ($in_spp)
: "eax", "ebx", "ecx", "edx", "esi", "edi", //"ebp", "esp",
"mmx0", "mmx1", "mmx2", "mmx3", "mmx4", "mmx5", "mmx6", "mmx7",
"xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
"cc"
: "volatile");
}
: "volatile")
};
}
#[path = "../x86_common.rs"]
mod common;

View File

@ -1,43 +1,26 @@
// This file is part of libfringe, a low-level green threading library.
// Copyright (c) edef <edef@edef.eu>
// See the LICENSE file included in this distribution.
use stack::Stack;
use void::Void;
use super::common::{push, rust_trampoline};
pub use self::common::*;
pub const STACK_ALIGN: usize = 16;
#[derive(Debug)]
pub struct Registers {
rsp: *mut usize
}
impl Registers {
#[inline]
pub unsafe fn new<S, F>(stack: &mut S, f: F) -> Registers
where S: Stack, F: FnOnce() -> Void {
let mut sp = stack.top() as *mut usize;
let f_ptr = push(&mut sp, f);
asm!(include_str!("init.s")
: "={rdi}"(sp)
: "{rdi}" (sp),
"{rsi}" (rust_trampoline::<F> as unsafe extern "C" fn(*const F) -> !),
"{rdx}" (f_ptr)
macro_rules! init {
($sp:expr, $f_ptr:expr, $tramp:expr) => {
asm!(include_str!("x86_64/init.s")
: "={rdi}"($sp)
: "{rdi}" ($sp),
"{rsi}" ($tramp),
"{rdx}" ($f_ptr)
:
: "volatile");
Registers { rsp: sp }
}
}
#[inline(always)]
pub unsafe fn swap(out_regs: *mut Registers, in_regs: *const Registers) {
let out_rspp = &mut (*out_regs).rsp;
let in_rspp = &(*in_regs).rsp;
asm!(include_str!("swap.s")
macro_rules! swap {
($out_spp:expr, $in_spp:expr) => {
asm!(include_str!("x86_64/swap.s")
:
: "{rdi}" (out_rspp),
"{rsi}" (in_rspp)
: "{rdi}" ($out_spp)
"{rsi}" ($in_spp)
: "rax", "rbx", "rcx", "rdx", "rsi", "rdi", //"rbp", "rsp",
"r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
"xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
@ -48,3 +31,6 @@ impl Registers {
: "volatile");
}
}
#[path = "../x86_common.rs"]
mod common;

35
src/arch/x86_common.rs Normal file
View File

@ -0,0 +1,35 @@
// This file is part of libfringe, a low-level green threading library.
// Copyright (c) 2015, edef <edef@edef.eu>
// See the LICENSE file included in this distribution.
use void::Void;
use stack::Stack;
use arch::common::{push, rust_trampoline};
pub const STACK_ALIGN: usize = 16;
#[derive(Debug)]
pub struct Registers {
stack_pointer: *mut usize
}
impl Registers {
#[inline]
pub unsafe fn new<S, F>(stack: &mut S, f: F) -> Registers
where S: Stack, F: FnOnce() -> Void
{
let mut sp = stack.top() as *mut usize;
let f_ptr = push(&mut sp, f);
init!(sp, f_ptr, rust_trampoline::<F> as unsafe extern "C" fn(*const F) -> !);
Registers {
stack_pointer: sp,
}
}
#[inline(always)]
pub unsafe fn swap(out_regs: *mut Registers, in_regs: *const Registers) {
swap!(&mut (*out_regs).stack_pointer, &(*in_regs).stack_pointer);
}
}