Commit 4a235309 authored by Isaac Oscar Gariano's avatar Isaac Oscar Gariano

Partially working swapstack

parent da783a85
...@@ -1433,7 +1433,6 @@ impl<'a> InstructionSelection { ...@@ -1433,7 +1433,6 @@ impl<'a> InstructionSelection {
vm vm
); );
} }
Instruction_::NewStack(func) => { Instruction_::NewStack(func) => {
trace!("instsel on NEWSTACK"); trace!("instsel on NEWSTACK");
...@@ -1452,7 +1451,7 @@ impl<'a> InstructionSelection { ...@@ -1452,7 +1451,7 @@ impl<'a> InstructionSelection {
self.emit_runtime_entry( self.emit_runtime_entry(
&entrypoints::MUENTRY_NEW_STACK, &entrypoints::MUENTRY_NEW_STACK,
vec![tmp_func, make_value_int_const(stack_arg_size as u64, vm)], vec![tmp_func, make_value_int_const(stack_arg_size as u64, vm)],
None, Some(vec![tmp_res]),
Some(node), Some(node),
f_context, f_context,
vm vm
......
...@@ -910,7 +910,7 @@ pub fn estimate_insts_for_ir(inst: &Instruction) -> usize { ...@@ -910,7 +910,7 @@ pub fn estimate_insts_for_ir(inst: &Instruction) -> usize {
// runtime // runtime
New(_) | NewHybrid(_, _) => 10, New(_) | NewHybrid(_, _) => 10,
NewStack(_) | NewThread(_, _) | NewThreadExn(_, _) | NewFrameCursor(_) => 10, NewStack(_) | NewThread(_, _) | NewThreadExn(_, _) | NewFrameCursor(_) => 10,
ThreadExit => 10, CurrentStack => 10, KillStack(_) => 10, NewStack(_) => 10, ThreadExit => 10, CurrentStack => 10, KillStack(_) => 10,
Throw(_) => 10, Throw(_) => 10,
SwapStackExpr { .. } | SwapStackExc { .. } | SwapStackKill { .. } => 10, SwapStackExpr { .. } | SwapStackExc { .. } | SwapStackKill { .. } => 10,
CommonInst_GetThreadLocal | CommonInst_SetThreadLocal(_) => 10, CommonInst_GetThreadLocal | CommonInst_SetThreadLocal(_) => 10,
......
...@@ -234,8 +234,42 @@ impl CompilerPass for GenMovPhi { ...@@ -234,8 +234,42 @@ impl CompilerPass for GenMovPhi {
} }
Instruction_::Watchpoint { .. } => unimplemented!(), Instruction_::Watchpoint { .. } => unimplemented!(),
Instruction_::WPBranch { .. } => unimplemented!(), Instruction_::WPBranch { .. } => unimplemented!(),
Instruction_::SwapStackExc { .. } | Instruction_::SwapStackExc { stack, is_exception, args, resume } => {
Instruction_::SwapStackKill { .. } => unimplemented!(), let norm_dest = process_dest(
resume.normal_dest,
&mut new_blocks_to_insert,
&ops,
vm,
&inst_name,
"norm"
);
let exn_dest = process_dest(
resume.exn_dest,
&mut new_blocks_to_insert,
&ops,
vm,
&inst_name,
"exc"
);
let new_inst = func.new_inst(Instruction {
hdr: inst.hdr.clone(),
value: inst.value.clone(),
ops: ops.to_vec(),
v: Instruction_::SwapStackExc {
stack: stack,
is_exception: is_exception,
args: args,
resume: ResumptionData {
normal_dest: norm_dest,
exn_dest: exn_dest
}
}
});
trace!("rewrite to {}", new_inst);
new_body.push(new_inst);
},
Instruction_::ExnInstruction { .. } => unimplemented!(), Instruction_::ExnInstruction { .. } => unimplemented!(),
_ => { _ => {
trace!("no rewrite"); trace!("no rewrite");
......
...@@ -22,8 +22,6 @@ use compiler::backend; ...@@ -22,8 +22,6 @@ use compiler::backend;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::Command; use std::process::Command;
use std::process::Stdio;
use std::process::Output;
/// links generated code for the given functions, static library of Zebu, /// links generated code for the given functions, static library of Zebu,
/// and a main function to produce an executable of the given name /// and a main function to produce an executable of the given name
......
...@@ -75,8 +75,10 @@ end_func exception_restore ...@@ -75,8 +75,10 @@ end_func exception_restore
.macro stack_kill old_sp .macro stack_kill old_sp
mov_args_to_callee_saved mov_args_to_callee_saved
push_pair X9, X10
MOV X0, \old_sp MOV X0, \old_sp
B muentry_kill_stack BL muentry_kill_stack
pop_pair X10, X9
mov_callee_saved_to_args mov_callee_saved_to_args
.endm .endm
......
...@@ -16,7 +16,6 @@ use ast::ir::*; ...@@ -16,7 +16,6 @@ use ast::ir::*;
use ast::ptr::*; use ast::ptr::*;
use ast::types::*; use ast::types::*;
use vm::VM; use vm::VM;
use runtime;
use runtime::ValueLocation; use runtime::ValueLocation;
use runtime::mm; use runtime::mm;
use compiler::backend::CALLEE_SAVED_COUNT; use compiler::backend::CALLEE_SAVED_COUNT;
...@@ -108,6 +107,26 @@ pub struct MuStack { ...@@ -108,6 +107,26 @@ pub struct MuStack {
mmap: Option<memmap::Mmap> mmap: Option<memmap::Mmap>
} }
impl Drop for MuStack
{
fn drop(&mut self) {
// Reverse memory protection so that dropping will work...
//trace!("dropping MuStack {}", Address::from_ref(self));
unsafe {
memsec::mprotect(
self.overflow_guard.to_ptr_mut::<u8>(),
PAGE_SIZE,
memsec::Prot::ReadWriteExec
);
memsec::mprotect(
self.underflow_guard.to_ptr_mut::<u8>(),
PAGE_SIZE,
memsec::Prot::ReadWriteExec
);
}
}
}
impl MuStack { impl MuStack {
/// creates a new MuStack for given entry function and function address /// creates a new MuStack for given entry function and function address
pub fn new(id: MuID, func_addr: Address, stack_arg_size: usize) -> MuStack { pub fn new(id: MuID, func_addr: Address, stack_arg_size: usize) -> MuStack {
...@@ -666,19 +685,24 @@ pub unsafe extern "C" fn muentry_prepare_swapstack_kill(new_stack: *mut MuStack) ...@@ -666,19 +685,24 @@ pub unsafe extern "C" fn muentry_prepare_swapstack_kill(new_stack: *mut MuStack)
// (i.e. when were are not on the current stack) // (i.e. when were are not on the current stack)
let cur_stack = Box::into_raw(cur_thread.stack.take().unwrap()); let cur_stack = Box::into_raw(cur_thread.stack.take().unwrap());
cur_thread.stack = Some(Box::from_raw(new_stack)); cur_thread.stack = Some(Box::from_raw(new_stack));
((*new_stack).sp, cur_stack) let new_sp = (*new_stack).sp;
trace!("ISAAC: muentry_prepare_swapstack_kill({}) -> ({}, {})", Address::from_ptr(new_stack), new_sp, Address::from_ptr(cur_stack));
(new_sp, cur_stack)
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn muentry_new_stack(entry: Address, stack_size: usize) -> *mut MuStack { pub unsafe extern "C" fn muentry_new_stack(entry: Address, stack_size: usize) -> *mut MuStack {
let ref vm = MuThread::current_mut().vm; let ref vm = MuThread::current_mut().vm;
let stack = Box::new(MuStack::new(vm.next_id(), entry, stack_size)); let stack = Box::new(MuStack::new(vm.next_id(), entry, stack_size));
return Box::into_raw(stack); let a = Box::into_raw(stack);
trace!("ISAAC: muentry_new_stack({}, {}) -> {}", entry, stack_size, Address::from_mut_ptr(a));
a
} }
// Kills the given stack. WARNING! do not call this whilst on the given stack // Kills the given stack. WARNING! do not call this whilst on the given stack
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn muentry_kill_stack(stack: *mut MuStack) { pub unsafe extern "C" fn muentry_kill_stack(stack: *mut MuStack) {
// This new box will be destroyed upon returning // This new box will be destroyed upon returning
Box::from_raw(stack); trace!("ISAAC: muentry_kill_stack({})", Address::from_mut_ptr(stack));
//Box::from_raw(stack);
} }
...@@ -87,6 +87,11 @@ impl Address { ...@@ -87,6 +87,11 @@ impl Address {
unsafe { mem::transmute(ptr) } unsafe { mem::transmute(ptr) }
} }
#[inline(always)]
pub fn from_ref<T>(r: &T) -> Address {
unsafe { mem::transmute(r) }
}
/// creates Address from a mutable pointer /// creates Address from a mutable pointer
#[inline(always)] #[inline(always)]
pub fn from_mut_ptr<T>(ptr: *mut T) -> Address { pub fn from_mut_ptr<T>(ptr: *mut T) -> Address {
......
...@@ -3414,7 +3414,7 @@ impl<'lb, 'lvm> BundleLoader<'lb, 'lvm> { ...@@ -3414,7 +3414,7 @@ impl<'lb, 'lvm> BundleLoader<'lb, 'lvm> {
let args_begin_index = ops.len(); let args_begin_index = ops.len();
let args = self.add_opnds(fcb, &mut ops, vars); let args = self.add_opnds(fcb, &mut ops, vars);
assert_ir!( assert_ir!(
ops.len() == tys.len() && args.len() == tys.len() &&
args.iter() args.iter()
.zip(tys) .zip(tys)
.all(|(arg, tid)| arg.ty() == self.get_built_type(*tid)) .all(|(arg, tid)| arg.ty() == self.get_built_type(*tid))
......
...@@ -1201,11 +1201,15 @@ impl<'a> VM { ...@@ -1201,11 +1201,15 @@ impl<'a> VM {
let func_addr = resolve_symbol(self.name_of(func_id)); let func_addr = resolve_symbol(self.name_of(func_id));
let stack_arg_size = backend::call_stack_size(func.sig.clone(), self); let stack_arg_size = backend::call_stack_size(func.sig.clone(), self);
Box::new(MuStack::new( let b = Box::new(MuStack::new(
self.next_id(), self.next_id(),
func_addr, func_addr,
stack_arg_size stack_arg_size
)) ));
let a: Address = unsafe{*(std::mem::transmute::<&Box<MuStack>, &Address>(&b))};
trace!("ISAAC: vm.new_stack({}) -> {}", func_addr, a);
b
} }
/// creates a handle that we can return to the client /// creates a handle that we can return to the client
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment