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

qinsoon's avatar
qinsoon committed
4
use utils::POINTER_SIZE;
qinsoon's avatar
qinsoon committed
5
use utils::vec_utils;
6 7

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

qinsoon's avatar
qinsoon committed
11 12 13 14
lazy_static! {
    pub static ref ADDRESS_TYPE : P<MuType> = P(
        MuType::new(new_internal_id(), MuType_::int(POINTER_SIZE * 8))
    );
15 16 17 18 19 20 21 22

    pub static ref UINT8_TYPE : P<MuType> = P(
        MuType::new(new_internal_id(), MuType_::int(8))
    );

    pub static ref UINT16_TYPE : P<MuType> = P(
        MuType::new(new_internal_id(), MuType_::int(16))
    );
qinsoon's avatar
qinsoon committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    
    pub static ref UINT32_TYPE : P<MuType> = P(
        MuType::new(new_internal_id(), MuType_::int(32))
    );
    
    pub static ref UINT64_TYPE : P<MuType> = P(
        MuType::new(new_internal_id(), MuType_::int(64))
    );
    
    pub static ref DOUBLE_TYPE : P<MuType> = P(
        MuType::new(new_internal_id(), MuType_::double())
    );
    
    pub static ref INTERNAL_TYPES : Vec<P<MuType>> = vec![
        ADDRESS_TYPE.clone(),
        UINT32_TYPE.clone(),
        UINT64_TYPE.clone(),
        DOUBLE_TYPE.clone()
    ];    
}

qinsoon's avatar
qinsoon committed
44
#[derive(PartialEq, Debug, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
45
pub struct MuType {
46
    pub hdr: MuEntityHeader,
qinsoon's avatar
qinsoon committed
47 48 49 50 51 52
    pub v: MuType_
}

impl MuType {
    pub fn new(id: MuID, v: MuType_) -> MuType {
        MuType {
53
            hdr: MuEntityHeader::unnamed(id),
qinsoon's avatar
qinsoon committed
54 55 56 57
            v: v
        }
    }
}
qinsoon's avatar
qinsoon committed
58

qinsoon's avatar
qinsoon committed
59
#[derive(PartialEq, Debug, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
60
pub enum MuType_ {
qinsoon's avatar
qinsoon committed
61
    /// int <length>
qinsoon's avatar
qinsoon committed
62
    Int          (usize),
qinsoon's avatar
qinsoon committed
63
    /// float
qinsoon's avatar
qinsoon committed
64
    Float,
qinsoon's avatar
qinsoon committed
65
    /// double
qinsoon's avatar
qinsoon committed
66
    Double,
67

qinsoon's avatar
qinsoon committed
68
    /// ref<T>
qinsoon's avatar
qinsoon committed
69
    Ref          (P<MuType>),    // Box is needed for non-recursive enum
qinsoon's avatar
qinsoon committed
70
    /// iref<T>: internal reference
qinsoon's avatar
qinsoon committed
71
    IRef         (P<MuType>),
qinsoon's avatar
qinsoon committed
72
    /// weakref<T>
qinsoon's avatar
qinsoon committed
73
    WeakRef      (P<MuType>),
74

qinsoon's avatar
qinsoon committed
75
    /// uptr<T>: unsafe pointer
qinsoon's avatar
qinsoon committed
76
    UPtr         (P<MuType>),
77

qinsoon's avatar
qinsoon committed
78
    /// struct<T1 T2 ...>
qinsoon's avatar
qinsoon committed
79
    Struct       (MuName),
80

qinsoon's avatar
qinsoon committed
81
    /// array<T length>
qinsoon's avatar
qinsoon committed
82
    Array        (P<MuType>, usize),
83

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

qinsoon's avatar
qinsoon committed
87
    /// void
qinsoon's avatar
qinsoon committed
88
    Void,
89

qinsoon's avatar
qinsoon committed
90
    /// threadref
qinsoon's avatar
qinsoon committed
91
    ThreadRef,
qinsoon's avatar
qinsoon committed
92
    /// stackref
qinsoon's avatar
qinsoon committed
93
    StackRef,
94

qinsoon's avatar
qinsoon committed
95
    /// tagref64: hold a double or an int or an ref<void>
qinsoon's avatar
qinsoon committed
96
    Tagref64,
97

qinsoon's avatar
qinsoon committed
98
    /// vector<T length>
qinsoon's avatar
qinsoon committed
99
    Vector       (P<MuType>, usize),
100

qinsoon's avatar
qinsoon committed
101
    /// funcref<@sig>
102
    FuncRef      (P<MuFuncSig>),
103

qinsoon's avatar
qinsoon committed
104
    /// ufuncptr<@sig>
105
    UFuncPtr     (P<MuFuncSig>),
qinsoon's avatar
qinsoon committed
106 107
}

qinsoon's avatar
qinsoon committed
108 109 110 111 112 113
impl fmt::Display for MuType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.v)        
    }
}

114
impl fmt::Display for MuType_ {
115 116
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
qinsoon's avatar
qinsoon committed
117 118 119
            &MuType_::Int(n)                          => write!(f, "int<{}>", n),
            &MuType_::Float                           => write!(f, "float"),
            &MuType_::Double                          => write!(f, "double"),
120 121 122 123 124
            &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
125
            &MuType_::Hybrid(ref fix_tys, ref var_ty) => write!(f, "hybrid<[{}] {}>", vec_utils::as_str(fix_tys), var_ty),
qinsoon's avatar
qinsoon committed
126 127 128 129
            &MuType_::Void                            => write!(f, "void"),
            &MuType_::ThreadRef                       => write!(f, "threadref"),
            &MuType_::StackRef                        => write!(f, "stackref"),
            &MuType_::Tagref64                        => write!(f, "tagref64"),
130 131 132
            &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),
133
            &MuType_::Struct(ref tag)                     => write!(f, "{}(struct)", tag)
134 135 136 137
        }
    }
}

qinsoon's avatar
qinsoon committed
138
lazy_static! {
qinsoon's avatar
qinsoon committed
139 140
    /// 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
141 142
}

qinsoon's avatar
qinsoon committed
143
#[derive(PartialEq, Debug, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
144
pub struct StructType_ {
qinsoon's avatar
qinsoon committed
145
    tys: Vec<P<MuType>>
qinsoon's avatar
qinsoon committed
146 147
}

148
impl fmt::Display for StructType_ {
149
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
qinsoon's avatar
qinsoon committed
150 151 152
        write!(f, "struct<").unwrap();
        for i in 0..self.tys.len() {
            let ty = &self.tys[i];
153
            write!(f, "{}", ty).unwrap();
qinsoon's avatar
qinsoon committed
154 155 156
            if i != self.tys.len() - 1 {
                write!(f, " ").unwrap();
            }
157
        }
qinsoon's avatar
qinsoon committed
158
        write!(f, ">")
159
    }
160 161
}

qinsoon's avatar
qinsoon committed
162
impl StructType_ {
163 164 165 166
    // The IR builder needs to create StructType objects, too.
    pub fn new(tys: Vec<P<MuType>>) -> StructType_ {
        StructType_ { tys: tys }
    }
qinsoon's avatar
qinsoon committed
167
    pub fn set_tys(&mut self, mut list: Vec<P<MuType>>) {
qinsoon's avatar
qinsoon committed
168
        self.tys.clear();
qinsoon's avatar
qinsoon committed
169
        self.tys.append(&mut list);
qinsoon's avatar
qinsoon committed
170
    }
qinsoon's avatar
qinsoon committed
171
    
qinsoon's avatar
qinsoon committed
172
    pub fn get_tys(&self) -> &Vec<P<MuType>> {
qinsoon's avatar
qinsoon committed
173 174
        &self.tys
    }
qinsoon's avatar
qinsoon committed
175 176
}

qinsoon's avatar
qinsoon committed
177 178 179
impl MuType_ {
    pub fn int(len: usize) -> MuType_ {
        MuType_::Int(len)
qinsoon's avatar
qinsoon committed
180
    }
qinsoon's avatar
qinsoon committed
181 182
    pub fn float() -> MuType_ {
        MuType_::Float
qinsoon's avatar
qinsoon committed
183
    }
qinsoon's avatar
qinsoon committed
184 185
    pub fn double() -> MuType_ {
        MuType_::Double
qinsoon's avatar
qinsoon committed
186
    }
qinsoon's avatar
qinsoon committed
187
    pub fn muref(referent: P<MuType>) -> MuType_ {
qinsoon's avatar
qinsoon committed
188
        MuType_::Ref(referent)
qinsoon's avatar
qinsoon committed
189
    }
qinsoon's avatar
qinsoon committed
190
    pub fn iref(referent: P<MuType>) -> MuType_ {
qinsoon's avatar
qinsoon committed
191
        MuType_::IRef(referent)
qinsoon's avatar
qinsoon committed
192
    }
qinsoon's avatar
qinsoon committed
193
    pub fn weakref(referent: P<MuType>) -> MuType_ {
qinsoon's avatar
qinsoon committed
194
        MuType_::WeakRef(referent)
qinsoon's avatar
qinsoon committed
195
    }
qinsoon's avatar
qinsoon committed
196
    pub fn uptr(referent: P<MuType>) -> MuType_ {
qinsoon's avatar
qinsoon committed
197
        MuType_::UPtr(referent)
qinsoon's avatar
qinsoon committed
198
    }
qinsoon's avatar
qinsoon committed
199
    pub fn mustruct_empty(tag: MuName) -> MuType_ {
qinsoon's avatar
qinsoon committed
200
        let struct_ty_ = StructType_{tys: vec![]};
201
        STRUCT_TAG_MAP.write().unwrap().insert(tag.clone(), struct_ty_);
qinsoon's avatar
qinsoon committed
202

qinsoon's avatar
qinsoon committed
203
        MuType_::Struct(tag)
qinsoon's avatar
qinsoon committed
204
    }
qinsoon's avatar
qinsoon committed
205
    pub fn mustruct(tag: MuName, list: Vec<P<MuType>>) -> MuType_ {
qinsoon's avatar
qinsoon committed
206
        let struct_ty_ = StructType_{tys: list};
qinsoon's avatar
qinsoon committed
207 208 209

        // if there is an attempt to use a same tag for different struct,
        // we panic
210
        match STRUCT_TAG_MAP.read().unwrap().get(&tag) {
qinsoon's avatar
qinsoon committed
211 212 213 214 215 216 217 218 219 220
            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
221
        STRUCT_TAG_MAP.write().unwrap().insert(tag.clone(), struct_ty_);
qinsoon's avatar
qinsoon committed
222

qinsoon's avatar
qinsoon committed
223
        MuType_::Struct(tag)
qinsoon's avatar
qinsoon committed
224
    }
qinsoon's avatar
qinsoon committed
225
    pub fn array(ty: P<MuType>, len: usize) -> MuType_ {
qinsoon's avatar
qinsoon committed
226
        MuType_::Array(ty, len)
qinsoon's avatar
qinsoon committed
227
    }
qinsoon's avatar
qinsoon committed
228
    pub fn hybrid(fix_tys: Vec<P<MuType>>, var_ty: P<MuType>) -> MuType_ {
qinsoon's avatar
qinsoon committed
229
        MuType_::Hybrid(fix_tys, var_ty)
qinsoon's avatar
qinsoon committed
230
    }
qinsoon's avatar
qinsoon committed
231 232
    pub fn void() -> MuType_ {
        MuType_::Void
qinsoon's avatar
qinsoon committed
233
    }
qinsoon's avatar
qinsoon committed
234 235
    pub fn threadref() -> MuType_ {
        MuType_::ThreadRef
qinsoon's avatar
qinsoon committed
236
    }
qinsoon's avatar
qinsoon committed
237 238
    pub fn stackref() -> MuType_ {
        MuType_::StackRef
qinsoon's avatar
qinsoon committed
239
    }
qinsoon's avatar
qinsoon committed
240 241
    pub fn tagref64() -> MuType_ {
        MuType_::Tagref64
qinsoon's avatar
qinsoon committed
242
    }
qinsoon's avatar
qinsoon committed
243
    pub fn vector(ty: P<MuType>, len: usize) -> MuType_ {
qinsoon's avatar
qinsoon committed
244
        MuType_::Vector(ty, len)
qinsoon's avatar
qinsoon committed
245
    }
246
    pub fn funcref(sig: P<MuFuncSig>) -> MuType_ {
qinsoon's avatar
qinsoon committed
247
        MuType_::FuncRef(sig)
qinsoon's avatar
qinsoon committed
248
    }
249
    pub fn ufuncptr(sig: P<MuFuncSig>) -> MuType_ {
qinsoon's avatar
qinsoon committed
250
        MuType_::UFuncPtr(sig)
qinsoon's avatar
qinsoon committed
251 252 253 254
    }
}

/// is a type floating-point type?
qinsoon's avatar
qinsoon committed
255
pub fn is_fp(ty: &MuType) -> bool {
qinsoon's avatar
qinsoon committed
256
    match ty.v {
qinsoon's avatar
qinsoon committed
257
        MuType_::Float | MuType_::Double => true,
qinsoon's avatar
qinsoon committed
258 259 260 261 262
        _ => false
    }
}

/// is a type raw pointer?
qinsoon's avatar
qinsoon committed
263
pub fn is_ptr(ty: &MuType) -> bool {
qinsoon's avatar
qinsoon committed
264
    match ty.v {
qinsoon's avatar
qinsoon committed
265
        MuType_::UPtr(_) | MuType_::UFuncPtr(_) => true,
qinsoon's avatar
qinsoon committed
266 267 268 269 270
        _ => false
    }
}

/// is a type scalar type?
qinsoon's avatar
qinsoon committed
271
pub fn is_scalar(ty: &MuType) -> bool {
qinsoon's avatar
qinsoon committed
272
    match ty.v {
qinsoon's avatar
qinsoon committed
273 274 275 276 277 278 279 280 281 282 283 284
        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
285 286 287 288 289
        _ => false
    }
}

/// is a type traced by the garbage collector?
290
/// Note: An aggregated type is traced if any of its part is traced.
qinsoon's avatar
qinsoon committed
291
pub fn is_traced(ty: &MuType) -> bool {
qinsoon's avatar
qinsoon committed
292
    match ty.v {
qinsoon's avatar
qinsoon committed
293 294 295 296 297 298 299 300 301
        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
302 303
            is_traced(var_ty) ||
            fix_tys.into_iter().map(|ty| is_traced(ty))
304
                .fold(false, |ret, this| ret || this)
qinsoon's avatar
qinsoon committed
305
            },
306
        MuType_::Struct(ref tag) => {
qinsoon's avatar
qinsoon committed
307 308 309
            let map = STRUCT_TAG_MAP.read().unwrap();
            let struct_ty = map.get(tag).unwrap();
            let ref field_tys = struct_ty.tys;
310

qinsoon's avatar
qinsoon committed
311 312 313 314 315 316 317 318 319
            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
320
pub fn is_native_safe(ty: &MuType) -> bool {
qinsoon's avatar
qinsoon committed
321
    match ty.v {
qinsoon's avatar
qinsoon committed
322 323 324 325 326 327 328 329 330
        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) => {
331
            is_native_safe(var_ty) &&
qinsoon's avatar
qinsoon committed
332 333 334
            fix_tys.into_iter().map(|ty| is_native_safe(&ty))
                .fold(true, |ret, this| ret && this)
        },
335
        MuType_::Struct(ref tag) => {
qinsoon's avatar
qinsoon committed
336 337 338
            let map = STRUCT_TAG_MAP.read().unwrap();
            let struct_ty = map.get(tag).unwrap();
            let ref field_tys = struct_ty.tys;
339

qinsoon's avatar
qinsoon committed
340 341 342 343
            field_tys.into_iter().map(|ty| is_native_safe(&ty))
                .fold(true, |ret, this| ret && this)
        },
        _ => false
qinsoon's avatar
qinsoon committed
344 345 346
    }
}

347
pub fn get_referent_ty(ty: &MuType) -> Option<P<MuType>> {
qinsoon's avatar
qinsoon committed
348 349 350 351
    match ty.v {
        MuType_::Ref(ref referent)
        | MuType_::IRef(ref referent)
        | MuType_::WeakRef(ref referent) => Some(referent.clone()),
352 353 354 355
        _ => None
    }
}

qinsoon's avatar
qinsoon committed
356 357 358 359 360 361 362 363 364
macro_rules! is_type (
    ($e:expr, $p:pat) => (
        match $e {
            $p => true,
            _ => false
        }
    )
);

365 366
pub type CFuncSig = MuFuncSig;

qinsoon's avatar
qinsoon committed
367
#[derive(PartialEq, Debug, RustcEncodable, RustcDecodable)]
qinsoon's avatar
qinsoon committed
368
pub struct MuFuncSig {
369
    pub hdr: MuEntityHeader,
qinsoon's avatar
qinsoon committed
370 371
    pub ret_tys : Vec<P<MuType>>,
    pub arg_tys: Vec<P<MuType>>
372 373
}

374
impl fmt::Display for MuFuncSig {
375
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
qinsoon's avatar
qinsoon committed
376
        write!(f, "[{}] -> [{}]", vec_utils::as_str(&self.ret_tys), vec_utils::as_str(&self.arg_tys))
377 378
    }
}