Commit f408e4f4 authored by qinsoon's avatar qinsoon

[wip] refactoring

parent f7ff598b
use ast::ir::*;
use ast::ptr::P;
use std::marker::Send;
use std::marker::Sync;
pub struct CompiledFunction {
pub fn_name: MuTag,
pub mc: Box<MachineCode>
pub mc: Box<MachineCode + Send + Sync>
}
pub trait MachineCode {
......
......@@ -5,67 +5,73 @@ use ast::ir::*;
use ast::types::*;
use vm::CompiledFunction;
use std::sync::Arc;
use std::sync::RwLock;
use std::cell::RefCell;
pub struct VMContext {
constants: HashMap<MuTag, P<Value>>,
types: HashMap<MuTag, P<MuType>>,
func_sigs: HashMap<MuTag, P<MuFuncSig>>,
funcs: HashMap<MuTag, RefCell<MuFunction>>,
constants: RwLock<HashMap<MuTag, P<Value>>>,
types: RwLock<HashMap<MuTag, P<MuType>>>,
func_sigs: RwLock<HashMap<MuTag, P<MuFuncSig>>>,
funcs: RwLock<HashMap<MuTag, RefCell<MuFunction>>>,
compiled_funcs: HashMap<MuTag, RefCell<CompiledFunction>>
compiled_funcs: RwLock<HashMap<MuTag, RefCell<CompiledFunction>>>
}
impl VMContext {
impl <'a> VMContext {
pub fn new() -> VMContext {
VMContext {
constants: HashMap::new(),
types: HashMap::new(),
func_sigs: HashMap::new(),
funcs: HashMap::new(),
compiled_funcs: HashMap::new()
constants: RwLock::new(HashMap::new()),
types: RwLock::new(HashMap::new()),
func_sigs: RwLock::new(HashMap::new()),
funcs: RwLock::new(HashMap::new()),
compiled_funcs: RwLock::new(HashMap::new())
}
}
pub fn declare_const(&mut self, const_name: MuTag, ty: P<MuType>, val: Constant) -> P<Value> {
debug_assert!(!self.constants.contains_key(const_name));
let mut constants = self.constants.write().unwrap();
debug_assert!(!constants.contains_key(const_name));
let ret = P(Value{tag: const_name, ty: ty, v: Value_::Constant(val)});
self.constants.insert(const_name, ret.clone());
constants.insert(const_name, ret.clone());
ret
}
pub fn declare_type(&mut self, type_name: MuTag, ty: P<MuType>) -> P<MuType> {
debug_assert!(!self.types.contains_key(type_name));
let mut types = self.types.write().unwrap();
debug_assert!(!types.contains_key(type_name));
self.types.insert(type_name, ty.clone());
types.insert(type_name, ty.clone());
ty
}
pub fn declare_func_sig(&mut self, sig_name: MuTag, ret_tys: Vec<P<MuType>>, arg_tys: Vec<P<MuType>>) -> P<MuFuncSig> {
debug_assert!(!self.func_sigs.contains_key(sig_name));
let mut func_sigs = self.func_sigs.write().unwrap();
debug_assert!(!func_sigs.contains_key(sig_name));
let ret = P(MuFuncSig{ret_tys: ret_tys, arg_tys: arg_tys});
self.func_sigs.insert(sig_name, ret.clone());
func_sigs.insert(sig_name, ret.clone());
ret
}
pub fn declare_func (&mut self, func: MuFunction) {
debug_assert!(!self.funcs.contains_key(func.fn_name));
let mut funcs = self.funcs.write().unwrap();
debug_assert!(!funcs.contains_key(func.fn_name));
self.funcs.insert(func.fn_name, RefCell::new(func));
funcs.insert(func.fn_name, RefCell::new(func));
}
pub fn add_compiled_func (&mut self, func: CompiledFunction) {
debug_assert!(self.funcs.contains_key(func.fn_name));
debug_assert!(self.funcs.read().unwrap().contains_key(func.fn_name));
self.compiled_funcs.insert(func.fn_name, RefCell::new(func));
self.compiled_funcs.write().unwrap().insert(func.fn_name, RefCell::new(func));
}
pub fn get_func(&self, fn_name: MuTag) -> Option<&RefCell<MuFunction>> {
self.funcs.get(fn_name)
pub fn funcs(&self) -> &RwLock<HashMap<MuTag, RefCell<MuFunction>>> {
&self.funcs
}
}
\ No newline at end of file
......@@ -19,9 +19,8 @@ fn test_instsel_fac() {
Box::new(backend::inst_sel::InstructionSelection::new())
]));
let mut factorial_func = {
vm_context.get_func("fac").unwrap().borrow_mut()
};
let funcs = vm_context.funcs().read().unwrap();
let mut factorial_func = funcs.get("fac").unwrap().borrow_mut();
compiler.compile(&vm_context, &mut factorial_func);
}
\ No newline at end of file
......@@ -18,9 +18,8 @@ fn test_use_count() {
vec![Box::new(passes::DefUse::new())]
));
let mut factorial_func = {
vm_context.get_func("fac").unwrap().borrow_mut()
};
let funcs = vm_context.funcs().read().unwrap();
let mut factorial_func = funcs.get("fac").unwrap().borrow_mut();
compiler.compile(&vm_context, &mut factorial_func);
......@@ -43,9 +42,8 @@ fn test_build_tree() {
Box::new(passes::TreeGen::new())]
));
let mut factorial_func = {
vm_context.get_func("fac").unwrap().borrow_mut()
};
let funcs = vm_context.funcs().read().unwrap();
let mut factorial_func = funcs.get("fac").unwrap().borrow_mut();
compiler.compile(&vm_context, &mut factorial_func);
}
......@@ -61,9 +59,8 @@ fn test_cfa_factorial() {
Box::new(passes::ControlFlowAnalysis::new())
]));
let mut factorial_func = {
vm_context.get_func("fac").unwrap().borrow_mut()
};
let funcs = vm_context.funcs().read().unwrap();
let mut factorial_func = funcs.get("fac").unwrap().borrow_mut();
compiler.compile(&vm_context, &mut factorial_func);
......@@ -97,9 +94,8 @@ fn test_cfa_sum() {
Box::new(passes::ControlFlowAnalysis::new())
]));
let mut sum_func = {
vm_context.get_func("sum").unwrap().borrow_mut()
};
let funcs = vm_context.funcs().read().unwrap();
let mut sum_func = funcs.get("sum").unwrap().borrow_mut();
compiler.compile(&vm_context, &mut sum_func);
......@@ -142,9 +138,8 @@ fn test_trace_factorial() {
Box::new(passes::TraceGen::new())
]));
let mut factorial_func = {
vm_context.get_func("fac").unwrap().borrow_mut()
};
let funcs = vm_context.funcs().read().unwrap();
let mut factorial_func = funcs.get("fac").unwrap().borrow_mut();
compiler.compile(&vm_context, &mut factorial_func);
......@@ -163,9 +158,8 @@ fn test_trace_sum() {
Box::new(passes::TraceGen::new())
]));
let mut sum_func = {
vm_context.get_func("sum").unwrap().borrow_mut()
};
let funcs = vm_context.funcs().read().unwrap();
let mut sum_func = funcs.get("sum").unwrap().borrow_mut();
compiler.compile(&vm_context, &mut sum_func);
......
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