context.rs 2.55 KB
Newer Older
qinsoon's avatar
qinsoon committed
1 2 3 4 5
use std::collections::HashMap;

use ast::ptr::P;
use ast::ir::*;
use ast::types::*;
6
use vm::machine_code::CompiledFunction;
qinsoon's avatar
qinsoon committed
7

qinsoon's avatar
qinsoon committed
8
use std::sync::RwLock;
9 10
use std::cell::RefCell;

qinsoon's avatar
qinsoon committed
11
pub struct VMContext {
qinsoon's avatar
qinsoon committed
12 13 14
    constants: RwLock<HashMap<MuTag, P<Value>>>,
    types: RwLock<HashMap<MuTag, P<MuType>>>,
    func_sigs: RwLock<HashMap<MuTag, P<MuFuncSig>>>,
15
    funcs: RwLock<HashMap<MuTag, RefCell<MuFunctionVersion>>>,
qinsoon's avatar
qinsoon committed
16
    
qinsoon's avatar
qinsoon committed
17
    compiled_funcs: RwLock<HashMap<MuTag, RefCell<CompiledFunction>>>
qinsoon's avatar
qinsoon committed
18 19
}

qinsoon's avatar
qinsoon committed
20
impl <'a> VMContext {
qinsoon's avatar
qinsoon committed
21 22
    pub fn new() -> VMContext {
        VMContext {
qinsoon's avatar
qinsoon committed
23 24 25 26 27
            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())
qinsoon's avatar
qinsoon committed
28 29 30
        }
    }
    
31
    pub fn declare_const(&self, const_name: MuTag, ty: P<MuType>, val: Constant) -> P<Value> {
qinsoon's avatar
qinsoon committed
32 33
        let mut constants = self.constants.write().unwrap();
        debug_assert!(!constants.contains_key(const_name));
qinsoon's avatar
qinsoon committed
34
        
35
        let ret = P(Value{tag: const_name, ty: ty, v: Value_::Constant(val)});
qinsoon's avatar
qinsoon committed
36
        constants.insert(const_name, ret.clone());
qinsoon's avatar
qinsoon committed
37 38 39 40
        
        ret
    }
    
41
    pub fn declare_type(&self, type_name: MuTag, ty: P<MuType>) -> P<MuType> {
qinsoon's avatar
qinsoon committed
42 43
        let mut types = self.types.write().unwrap();
        debug_assert!(!types.contains_key(type_name));
qinsoon's avatar
qinsoon committed
44
        
qinsoon's avatar
qinsoon committed
45
        types.insert(type_name, ty.clone());
qinsoon's avatar
qinsoon committed
46 47 48 49
        
        ty
    }
    
50
    pub fn declare_func_sig(&self, sig_name: MuTag, ret_tys: Vec<P<MuType>>, arg_tys: Vec<P<MuType>>) -> P<MuFuncSig> {
qinsoon's avatar
qinsoon committed
51 52
        let mut func_sigs = self.func_sigs.write().unwrap();
        debug_assert!(!func_sigs.contains_key(sig_name));
qinsoon's avatar
qinsoon committed
53 54
        
        let ret = P(MuFuncSig{ret_tys: ret_tys, arg_tys: arg_tys});
qinsoon's avatar
qinsoon committed
55
        func_sigs.insert(sig_name, ret.clone());
qinsoon's avatar
qinsoon committed
56 57 58 59
        
        ret
    }
    
60
    pub fn declare_func (&self, func: MuFunctionVersion) {
qinsoon's avatar
qinsoon committed
61 62
        let mut funcs = self.funcs.write().unwrap();
        debug_assert!(!funcs.contains_key(func.fn_name));
qinsoon's avatar
qinsoon committed
63
        
qinsoon's avatar
qinsoon committed
64
        funcs.insert(func.fn_name, RefCell::new(func));
65 66
    }
    
67
    pub fn add_compiled_func (&self, func: CompiledFunction) {
qinsoon's avatar
qinsoon committed
68
        debug_assert!(self.funcs.read().unwrap().contains_key(func.fn_name));
qinsoon's avatar
qinsoon committed
69

qinsoon's avatar
qinsoon committed
70
        self.compiled_funcs.write().unwrap().insert(func.fn_name, RefCell::new(func));
qinsoon's avatar
qinsoon committed
71 72
    }
    
73
    pub fn funcs(&self) -> &RwLock<HashMap<MuTag, RefCell<MuFunctionVersion>>> {
qinsoon's avatar
qinsoon committed
74
        &self.funcs
75
    }
76 77 78 79
    
    pub fn compiled_funcs(&self) -> &RwLock<HashMap<MuTag, RefCell<CompiledFunction>>> {
        &self.compiled_funcs
    }
80
}