types.rs 9.8 KB
Newer Older
qinsoon's avatar
qinsoon committed
1 2 3 4
use ptr::P;
use ir::*;

use utils::vec_utils;
5 6

use std::fmt;
qinsoon's avatar
qinsoon committed
7 8
use std::collections::HashMap;
use std::sync::RwLock;
qinsoon's avatar
qinsoon committed
9

qinsoon's avatar
qinsoon committed
10
#[derive(PartialEq, Debug, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
11
pub struct MuType {
12
    pub hdr: MuEntityHeader,
qinsoon's avatar
qinsoon committed
13 14 15 16 17 18
    pub v: MuType_
}

impl MuType {
    pub fn new(id: MuID, v: MuType_) -> MuType {
        MuType {
19
            hdr: MuEntityHeader::unnamed(id),
qinsoon's avatar
qinsoon committed
20 21 22 23
            v: v
        }
    }
}
qinsoon's avatar
qinsoon committed
24

qinsoon's avatar
qinsoon committed
25
#[derive(PartialEq, Debug, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
26
pub enum MuType_ {
qinsoon's avatar
qinsoon committed
27
    /// int <length>
qinsoon's avatar
qinsoon committed
28
    Int          (usize),
qinsoon's avatar
qinsoon committed
29
    /// float
qinsoon's avatar
qinsoon committed
30
    Float,
qinsoon's avatar
qinsoon committed
31
    /// double
qinsoon's avatar
qinsoon committed
32
    Double,
33

qinsoon's avatar
qinsoon committed
34
    /// ref<T>
qinsoon's avatar
qinsoon committed
35
    Ref          (P<MuType>),    // Box is needed for non-recursive enum
qinsoon's avatar
qinsoon committed
36
    /// iref<T>: internal reference
qinsoon's avatar
qinsoon committed
37
    IRef         (P<MuType>),
qinsoon's avatar
qinsoon committed
38
    /// weakref<T>
qinsoon's avatar
qinsoon committed
39
    WeakRef      (P<MuType>),
40

qinsoon's avatar
qinsoon committed
41
    /// uptr<T>: unsafe pointer
qinsoon's avatar
qinsoon committed
42
    UPtr         (P<MuType>),
43

qinsoon's avatar
qinsoon committed
44
    /// struct<T1 T2 ...>
qinsoon's avatar
qinsoon committed
45
    Struct       (MuName),
46

qinsoon's avatar
qinsoon committed
47
    /// array<T length>
qinsoon's avatar
qinsoon committed
48
    Array        (P<MuType>, usize),
49

qinsoon's avatar
qinsoon committed
50
    /// hybrid<F1 F2 ... V>: a hybrid of fixed length parts and a variable length part
qinsoon's avatar
qinsoon committed
51
    Hybrid       (Vec<P<MuType>>, P<MuType>),
52

qinsoon's avatar
qinsoon committed
53
    /// void
qinsoon's avatar
qinsoon committed
54
    Void,
55

qinsoon's avatar
qinsoon committed
56
    /// threadref
qinsoon's avatar
qinsoon committed
57
    ThreadRef,
qinsoon's avatar
qinsoon committed
58
    /// stackref
qinsoon's avatar
qinsoon committed
59
    StackRef,
60

qinsoon's avatar
qinsoon committed
61
    /// tagref64: hold a double or an int or an ref<void>
qinsoon's avatar
qinsoon committed
62
    Tagref64,
63

qinsoon's avatar
qinsoon committed
64
    /// vector<T length>
qinsoon's avatar
qinsoon committed
65
    Vector       (P<MuType>, usize),
66

qinsoon's avatar
qinsoon committed
67
    /// funcref<@sig>
68
    FuncRef      (P<MuFuncSig>),
69

qinsoon's avatar
qinsoon committed
70
    /// ufuncptr<@sig>
71
    UFuncPtr     (P<MuFuncSig>),
qinsoon's avatar
qinsoon committed
72 73
}

qinsoon's avatar
qinsoon committed
74 75 76 77 78 79
impl fmt::Display for MuType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.v)        
    }
}

80
impl fmt::Display for MuType_ {
81 82
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
qinsoon's avatar
qinsoon committed
83 84 85
            &MuType_::Int(n)                          => write!(f, "int<{}>", n),
            &MuType_::Float                           => write!(f, "float"),
            &MuType_::Double                          => write!(f, "double"),
86 87 88 89 90
            &MuType_::Ref(ref ty)                     => write!(f, "ref<{}>", ty),
            &MuType_::IRef(ref ty)                    => write!(f, "iref<{}>", ty),
            &MuType_::WeakRef(ref ty)                 => write!(f, "weakref<{}>", ty),
            &MuType_::UPtr(ref ty)                    => write!(f, "uptr<{}>", ty),
            &MuType_::Array(ref ty, size)             => write!(f, "array<{} {}>", ty, size),
qinsoon's avatar
qinsoon committed
91
            &MuType_::Hybrid(ref fix_tys, ref var_ty) => write!(f, "hybrid<[{}] {}>", vec_utils::as_str(fix_tys), var_ty),
qinsoon's avatar
qinsoon committed
92 93 94 95
            &MuType_::Void                            => write!(f, "void"),
            &MuType_::ThreadRef                       => write!(f, "threadref"),
            &MuType_::StackRef                        => write!(f, "stackref"),
            &MuType_::Tagref64                        => write!(f, "tagref64"),
96 97 98
            &MuType_::Vector(ref ty, size)            => write!(f, "vector<{} {}>", ty, size),
            &MuType_::FuncRef(ref sig)                => write!(f, "funcref<{}>", sig),
            &MuType_::UFuncPtr(ref sig)               => write!(f, "ufuncref<{}>", sig),
99
            &MuType_::Struct(ref tag)                     => write!(f, "{}(struct)", tag)
100 101 102 103
        }
    }
}

qinsoon's avatar
qinsoon committed
104
lazy_static! {
qinsoon's avatar
qinsoon committed
105 106
    /// storing a map from MuName to StructType_
    pub static ref STRUCT_TAG_MAP : RwLock<HashMap<MuName, StructType_>> = RwLock::new(HashMap::new());
qinsoon's avatar
qinsoon committed
107 108
}

qinsoon's avatar
qinsoon committed
109
#[derive(PartialEq, Debug, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
110
pub struct StructType_ {
qinsoon's avatar
qinsoon committed
111
    tys: Vec<P<MuType>>
qinsoon's avatar
qinsoon committed
112 113
}

114
impl fmt::Display for StructType_ {
115
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
qinsoon's avatar
qinsoon committed
116 117 118
        write!(f, "struct<").unwrap();
        for i in 0..self.tys.len() {
            let ty = &self.tys[i];
119
            write!(f, "{}", ty).unwrap();
qinsoon's avatar
qinsoon committed
120 121 122
            if i != self.tys.len() - 1 {
                write!(f, " ").unwrap();
            }
123
        }
qinsoon's avatar
qinsoon committed
124
        write!(f, ">")
125
    }
126 127
}

qinsoon's avatar
qinsoon committed
128
impl StructType_ {
qinsoon's avatar
qinsoon committed
129
    pub fn set_tys(&mut self, mut list: Vec<P<MuType>>) {
qinsoon's avatar
qinsoon committed
130
        self.tys.clear();
qinsoon's avatar
qinsoon committed
131
        self.tys.append(&mut list);
qinsoon's avatar
qinsoon committed
132
    }
qinsoon's avatar
qinsoon committed
133
    
qinsoon's avatar
qinsoon committed
134
    pub fn get_tys(&self) -> &Vec<P<MuType>> {
qinsoon's avatar
qinsoon committed
135 136
        &self.tys
    }
qinsoon's avatar
qinsoon committed
137 138
}

qinsoon's avatar
qinsoon committed
139 140 141
impl MuType_ {
    pub fn int(len: usize) -> MuType_ {
        MuType_::Int(len)
qinsoon's avatar
qinsoon committed
142
    }
qinsoon's avatar
qinsoon committed
143 144
    pub fn float() -> MuType_ {
        MuType_::Float
qinsoon's avatar
qinsoon committed
145
    }
qinsoon's avatar
qinsoon committed
146 147
    pub fn double() -> MuType_ {
        MuType_::Double
qinsoon's avatar
qinsoon committed
148
    }
qinsoon's avatar
qinsoon committed
149
    pub fn muref(referent: P<MuType>) -> MuType_ {
qinsoon's avatar
qinsoon committed
150
        MuType_::Ref(referent)
qinsoon's avatar
qinsoon committed
151
    }
qinsoon's avatar
qinsoon committed
152
    pub fn iref(referent: P<MuType>) -> MuType_ {
qinsoon's avatar
qinsoon committed
153
        MuType_::IRef(referent)
qinsoon's avatar
qinsoon committed
154
    }
qinsoon's avatar
qinsoon committed
155
    pub fn weakref(referent: P<MuType>) -> MuType_ {
qinsoon's avatar
qinsoon committed
156
        MuType_::WeakRef(referent)
qinsoon's avatar
qinsoon committed
157
    }
qinsoon's avatar
qinsoon committed
158
    pub fn uptr(referent: P<MuType>) -> MuType_ {
qinsoon's avatar
qinsoon committed
159
        MuType_::UPtr(referent)
qinsoon's avatar
qinsoon committed
160
    }
qinsoon's avatar
qinsoon committed
161
    pub fn mustruct_empty(tag: MuName) -> MuType_ {
qinsoon's avatar
qinsoon committed
162
        let struct_ty_ = StructType_{tys: vec![]};
163
        STRUCT_TAG_MAP.write().unwrap().insert(tag.clone(), struct_ty_);
qinsoon's avatar
qinsoon committed
164

qinsoon's avatar
qinsoon committed
165
        MuType_::Struct(tag)
qinsoon's avatar
qinsoon committed
166
    }
qinsoon's avatar
qinsoon committed
167
    pub fn mustruct(tag: MuName, list: Vec<P<MuType>>) -> MuType_ {
qinsoon's avatar
qinsoon committed
168
        let struct_ty_ = StructType_{tys: list};
qinsoon's avatar
qinsoon committed
169 170 171

        // if there is an attempt to use a same tag for different struct,
        // we panic
172
        match STRUCT_TAG_MAP.read().unwrap().get(&tag) {
qinsoon's avatar
qinsoon committed
173 174 175 176 177 178 179 180 181 182
            Some(old_struct_ty_) => {
                if struct_ty_ != *old_struct_ty_ {
                    panic!(format!(
                            "trying to insert {:?} as {}, while the old struct is defined as {:?}",
                            struct_ty_, tag, old_struct_ty_))
                }
            },
            None => {}
        }
        // otherwise, store the tag
183
        STRUCT_TAG_MAP.write().unwrap().insert(tag.clone(), struct_ty_);
qinsoon's avatar
qinsoon committed
184

qinsoon's avatar
qinsoon committed
185
        MuType_::Struct(tag)
qinsoon's avatar
qinsoon committed
186
    }
qinsoon's avatar
qinsoon committed
187
    pub fn array(ty: P<MuType>, len: usize) -> MuType_ {
qinsoon's avatar
qinsoon committed
188
        MuType_::Array(ty, len)
qinsoon's avatar
qinsoon committed
189
    }
qinsoon's avatar
qinsoon committed
190
    pub fn hybrid(fix_tys: Vec<P<MuType>>, var_ty: P<MuType>) -> MuType_ {
qinsoon's avatar
qinsoon committed
191
        MuType_::Hybrid(fix_tys, var_ty)
qinsoon's avatar
qinsoon committed
192
    }
qinsoon's avatar
qinsoon committed
193 194
    pub fn void() -> MuType_ {
        MuType_::Void
qinsoon's avatar
qinsoon committed
195
    }
qinsoon's avatar
qinsoon committed
196 197
    pub fn threadref() -> MuType_ {
        MuType_::ThreadRef
qinsoon's avatar
qinsoon committed
198
    }
qinsoon's avatar
qinsoon committed
199 200
    pub fn stackref() -> MuType_ {
        MuType_::StackRef
qinsoon's avatar
qinsoon committed
201
    }
qinsoon's avatar
qinsoon committed
202 203
    pub fn tagref64() -> MuType_ {
        MuType_::Tagref64
qinsoon's avatar
qinsoon committed
204
    }
qinsoon's avatar
qinsoon committed
205
    pub fn vector(ty: P<MuType>, len: usize) -> MuType_ {
qinsoon's avatar
qinsoon committed
206
        MuType_::Vector(ty, len)
qinsoon's avatar
qinsoon committed
207
    }
208
    pub fn funcref(sig: P<MuFuncSig>) -> MuType_ {
qinsoon's avatar
qinsoon committed
209
        MuType_::FuncRef(sig)
qinsoon's avatar
qinsoon committed
210
    }
211
    pub fn ufuncptr(sig: P<MuFuncSig>) -> MuType_ {
qinsoon's avatar
qinsoon committed
212
        MuType_::UFuncPtr(sig)
qinsoon's avatar
qinsoon committed
213 214 215 216
    }
}

/// is a type floating-point type?
qinsoon's avatar
qinsoon committed
217
pub fn is_fp(ty: &MuType) -> bool {
qinsoon's avatar
qinsoon committed
218
    match ty.v {
qinsoon's avatar
qinsoon committed
219
        MuType_::Float | MuType_::Double => true,
qinsoon's avatar
qinsoon committed
220 221 222 223 224
        _ => false
    }
}

/// is a type raw pointer?
qinsoon's avatar
qinsoon committed
225
pub fn is_ptr(ty: &MuType) -> bool {
qinsoon's avatar
qinsoon committed
226
    match ty.v {
qinsoon's avatar
qinsoon committed
227
        MuType_::UPtr(_) | MuType_::UFuncPtr(_) => true,
qinsoon's avatar
qinsoon committed
228 229 230 231 232
        _ => false
    }
}

/// is a type scalar type?
qinsoon's avatar
qinsoon committed
233
pub fn is_scalar(ty: &MuType) -> bool {
qinsoon's avatar
qinsoon committed
234
    match ty.v {
qinsoon's avatar
qinsoon committed
235 236 237 238 239 240 241 242 243 244 245 246
        MuType_::Int(_)
        | MuType_::Float
        | MuType_::Double
        | MuType_::Ref(_)
        | MuType_::IRef(_)
        | MuType_::WeakRef(_)
        | MuType_::FuncRef(_)
        | MuType_::UFuncPtr(_)
        | MuType_::ThreadRef
        | MuType_::StackRef
        | MuType_::Tagref64
        | MuType_::UPtr(_) => true,
qinsoon's avatar
qinsoon committed
247 248 249 250 251
        _ => false
    }
}

/// is a type traced by the garbage collector?
252
/// Note: An aggregated type is traced if any of its part is traced.
qinsoon's avatar
qinsoon committed
253
pub fn is_traced(ty: &MuType) -> bool {
qinsoon's avatar
qinsoon committed
254
    match ty.v {
qinsoon's avatar
qinsoon committed
255 256 257 258 259 260 261 262 263
        MuType_::Ref(_) => true,
        MuType_::IRef(_) => true,
        MuType_::WeakRef(_) => true,
        MuType_::Array(ref elem_ty, _)
        | MuType_::Vector(ref elem_ty, _) => is_traced(elem_ty),
        MuType_::ThreadRef
        | MuType_::StackRef
        | MuType_::Tagref64 => true,
        MuType_::Hybrid(ref fix_tys, ref var_ty) => {
qinsoon's avatar
qinsoon committed
264 265
            is_traced(var_ty) ||
            fix_tys.into_iter().map(|ty| is_traced(ty))
266
                .fold(false, |ret, this| ret || this)
qinsoon's avatar
qinsoon committed
267
            },
268
        MuType_::Struct(ref tag) => {
qinsoon's avatar
qinsoon committed
269 270 271
            let map = STRUCT_TAG_MAP.read().unwrap();
            let struct_ty = map.get(tag).unwrap();
            let ref field_tys = struct_ty.tys;
272

qinsoon's avatar
qinsoon committed
273 274 275 276 277 278 279 280 281
            field_tys.into_iter().map(|ty| is_traced(&ty))
                .fold(false, |ret, this| ret || this)
        },
        _ => false
    }
}

/// is a type native safe?
/// Note: An aggregated type is native safe if all of its parts are native safe.
qinsoon's avatar
qinsoon committed
282
pub fn is_native_safe(ty: &MuType) -> bool {
qinsoon's avatar
qinsoon committed
283
    match ty.v {
qinsoon's avatar
qinsoon committed
284 285 286 287 288 289 290 291 292
        MuType_::Int(_) => true,
        MuType_::Float => true,
        MuType_::Double => true,
        MuType_::Void => true,
        MuType_::Array(ref elem_ty, _)
        | MuType_::Vector(ref elem_ty, _) => is_native_safe(elem_ty),
        MuType_::UPtr(_) => true,
        MuType_::UFuncPtr(_) => true,
        MuType_::Hybrid(ref fix_tys, ref var_ty) => {
293
            is_native_safe(var_ty) &&
qinsoon's avatar
qinsoon committed
294 295 296
            fix_tys.into_iter().map(|ty| is_native_safe(&ty))
                .fold(true, |ret, this| ret && this)
        },
297
        MuType_::Struct(ref tag) => {
qinsoon's avatar
qinsoon committed
298 299 300
            let map = STRUCT_TAG_MAP.read().unwrap();
            let struct_ty = map.get(tag).unwrap();
            let ref field_tys = struct_ty.tys;
301

qinsoon's avatar
qinsoon committed
302 303 304 305
            field_tys.into_iter().map(|ty| is_native_safe(&ty))
                .fold(true, |ret, this| ret && this)
        },
        _ => false
qinsoon's avatar
qinsoon committed
306 307 308
    }
}

309
pub fn get_referent_ty(ty: &MuType) -> Option<P<MuType>> {
qinsoon's avatar
qinsoon committed
310 311 312 313
    match ty.v {
        MuType_::Ref(ref referent)
        | MuType_::IRef(ref referent)
        | MuType_::WeakRef(ref referent) => Some(referent.clone()),
314 315 316 317
        _ => None
    }
}

qinsoon's avatar
qinsoon committed
318 319 320 321 322 323 324 325 326
macro_rules! is_type (
    ($e:expr, $p:pat) => (
        match $e {
            $p => true,
            _ => false
        }
    )
);

327 328
pub type CFuncSig = MuFuncSig;

qinsoon's avatar
qinsoon committed
329
#[derive(PartialEq, Debug, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
330
pub struct MuFuncSig {
331
    pub hdr: MuEntityHeader,
qinsoon's avatar
qinsoon committed
332 333
    pub ret_tys : Vec<P<MuType>>,
    pub arg_tys: Vec<P<MuType>>
334 335
}

336
impl fmt::Display for MuFuncSig {
337
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
qinsoon's avatar
qinsoon committed
338
        write!(f, "[{}] -> [{}]", vec_utils::as_str(&self.ret_tys), vec_utils::as_str(&self.arg_tys))
339 340
    }
}