GitLab will be upgraded to the 12.10.14-ce.0 on 28 Sept 2020 at 2.00pm (AEDT) to 2.30pm (AEDT). During the update, GitLab and Mattermost services will not be available. If you have any concerns with this, please talk to us at N110 (b) CSIT building.

vm.rs 49.7 KB
Newer Older
qinsoon's avatar
qinsoon committed
1 2
use std::collections::HashMap;

qinsoon's avatar
qinsoon committed
3
use ast::ptr::*;
qinsoon's avatar
qinsoon committed
4
use ast::ir::*;
5
use ast::inst::*;
qinsoon's avatar
qinsoon committed
6
use ast::types;
qinsoon's avatar
qinsoon committed
7
use ast::types::*;
qinsoon's avatar
qinsoon committed
8
use compiler::{Compiler, CompilerPolicy};
qinsoon's avatar
qinsoon committed
9 10
use compiler::backend;
use compiler::backend::BackendTypeInfo;
qinsoon's avatar
qinsoon committed
11
use compiler::machine_code::CompiledFunction;
12 13
use runtime::thread::*;
use runtime::ValueLocation;
qinsoon's avatar
qinsoon committed
14
use utils::ByteSize;
15
use utils::BitSize;
16
use utils::Address;
17
use runtime::mm as gc;
18
use vm::handle::*;
qinsoon's avatar
qinsoon committed
19 20
use vm::vm_options::VMOptions;
use vm::vm_options::MuLogLevel;
21

qinsoon's avatar
qinsoon committed
22 23
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
use log::LogLevel;
24
use std::sync::Arc;
qinsoon's avatar
qinsoon committed
25
use std::sync::RwLock;
26
use std::sync::RwLockWriteGuard;
27
use std::sync::atomic::{AtomicUsize, AtomicBool, ATOMIC_BOOL_INIT, ATOMIC_USIZE_INIT, Ordering};
28

qinsoon's avatar
qinsoon committed
29 30 31 32 33
// FIXME:
// besides fields in VM, there are some 'globals' we need to persist
// such as STRUCT_TAG_MAP
// possibly INTERNAL_ID in ir.rs, internal types, etc

qinsoon's avatar
qinsoon committed
34
pub struct VM {
35
    // serialize
qinsoon's avatar
qinsoon committed
36
    // 0
37
    next_id: AtomicUsize,
qinsoon's avatar
qinsoon committed
38
    // 1
qinsoon's avatar
qinsoon committed
39
    id_name_map: RwLock<HashMap<MuID, MuName>>,
qinsoon's avatar
qinsoon committed
40
    // 2
qinsoon's avatar
qinsoon committed
41
    name_id_map: RwLock<HashMap<MuName, MuID>>,
qinsoon's avatar
qinsoon committed
42
    // 3
qinsoon's avatar
qinsoon committed
43
    types: RwLock<HashMap<MuID, P<MuType>>>,
qinsoon's avatar
qinsoon committed
44 45
    // 4
    backend_type_info: RwLock<HashMap<MuID, Box<BackendTypeInfo>>>,
46
    // 5
qinsoon's avatar
qinsoon committed
47
    constants: RwLock<HashMap<MuID, P<Value>>>,
qinsoon's avatar
qinsoon committed
48
    // 6
qinsoon's avatar
qinsoon committed
49
    globals: RwLock<HashMap<MuID, P<Value>>>,
50
    pub global_locations: RwLock<HashMap<MuID, ValueLocation>>,
qinsoon's avatar
qinsoon committed
51
    // 7
qinsoon's avatar
qinsoon committed
52
    func_sigs: RwLock<HashMap<MuID, P<MuFuncSig>>>,
qinsoon's avatar
qinsoon committed
53
    // 8
qinsoon's avatar
qinsoon committed
54
    funcs: RwLock<HashMap<MuID, RwLock<MuFunction>>>,
qinsoon's avatar
qinsoon committed
55 56
    // 9
    func_vers: RwLock<HashMap<MuID, RwLock<MuFunctionVersion>>>,
57 58
    // 10
    pub primordial: RwLock<Option<MuPrimordialThread>>,
qinsoon's avatar
qinsoon committed
59 60
    // 11
    is_running: AtomicBool,
qinsoon's avatar
qinsoon committed
61 62
    // 12
    pub vm_options: VMOptions,
qinsoon's avatar
qinsoon committed
63
    
64
    // partially serialize
qinsoon's avatar
qinsoon committed
65
    // 13
66
    compiled_funcs: RwLock<HashMap<MuID, RwLock<CompiledFunction>>>,
67 68
}

qinsoon's avatar
qinsoon committed
69
const VM_SERIALIZE_FIELDS : usize = 14;
qinsoon's avatar
qinsoon committed
70

71 72
impl Encodable for VM {
    fn encode<S: Encoder> (&self, s: &mut S) -> Result<(), S::Error> {
73 74
        let mut field_i = 0;

qinsoon's avatar
qinsoon committed
75 76
        // serialize VM_SERIALIZE_FIELDS fields
        // PLUS ONE extra global STRUCT_TAG_MAP
77
        s.emit_struct("VM", VM_SERIALIZE_FIELDS + 2, |s| {
78
            // next_id
qinsoon's avatar
qinsoon committed
79
            trace!("...serializing next_id");
80
            try!(s.emit_struct_field("next_id", field_i, |s| {
81 82
                s.emit_usize(self.next_id.load(Ordering::SeqCst))
            }));
83
            field_i += 1;
84 85
                
            // id_name_map
qinsoon's avatar
qinsoon committed
86
            trace!("...serializing id_name_map");
87 88
            {
                let map : &HashMap<MuID, MuName> = &self.id_name_map.read().unwrap();            
89
                try!(s.emit_struct_field("id_name_map", field_i, |s| map.encode(s)));
90
            }
91
            field_i += 1;
92 93
            
            // name_id_map
qinsoon's avatar
qinsoon committed
94
            trace!("...serializing name_id_map");
95 96
            {
                let map : &HashMap<MuName, MuID> = &self.name_id_map.read().unwrap(); 
97
                try!(s.emit_struct_field("name_id_map", field_i, |s| map.encode(s)));
98
            }
99
            field_i += 1;
100 101
            
            // types
qinsoon's avatar
qinsoon committed
102
            trace!("...serializing types");
103 104
            {
                let types = &self.types.read().unwrap();
105
                try!(s.emit_struct_field("types", field_i, |s| types.encode(s)));
qinsoon's avatar
qinsoon committed
106
            }
107 108
            field_i += 1;

qinsoon's avatar
qinsoon committed
109
            // STRUCT_TAG_MAP
qinsoon's avatar
qinsoon committed
110
            trace!("...serializing struct_tag_map");
qinsoon's avatar
qinsoon committed
111 112
            {
                let struct_tag_map = types::STRUCT_TAG_MAP.read().unwrap();
113 114 115 116 117 118 119 120 121
                try!(s.emit_struct_field("struct_tag_map", field_i, |s| struct_tag_map.encode(s)));
            }
            field_i += 1;

            // HYBRID_TAG_MAP
            trace!("...serializing hybrid_tag_map");
            {
                let hybrid_tag_map = types::HYBRID_TAG_MAP.read().unwrap();
                try!(s.emit_struct_field("hybrid_tag_map", field_i, |s| hybrid_tag_map.encode(s)));
qinsoon's avatar
qinsoon committed
122
            }
123
            field_i += 1;
qinsoon's avatar
qinsoon committed
124 125
            
            // backend_type_info
qinsoon's avatar
qinsoon committed
126
            trace!("...serializing backend_type_info");
qinsoon's avatar
qinsoon committed
127 128
            {
                let backend_type_info : &HashMap<_, _> = &self.backend_type_info.read().unwrap();
129
                try!(s.emit_struct_field("backend_type_info", field_i, |s| backend_type_info.encode(s)));
qinsoon's avatar
qinsoon committed
130
            }
131
            field_i += 1;
qinsoon's avatar
qinsoon committed
132 133
            
            // constants
qinsoon's avatar
qinsoon committed
134
            trace!("...serializing constants");
qinsoon's avatar
qinsoon committed
135 136
            {
                let constants : &HashMap<_, _> = &self.constants.read().unwrap();
137
                try!(s.emit_struct_field("constants", field_i, |s| constants.encode(s)));
qinsoon's avatar
qinsoon committed
138
            }
139
            field_i += 1;
qinsoon's avatar
qinsoon committed
140 141
            
            // globals
qinsoon's avatar
qinsoon committed
142
            trace!("...serializing globals");
qinsoon's avatar
qinsoon committed
143 144
            {
                let globals: &HashMap<_, _> = &self.globals.read().unwrap();
145
                try!(s.emit_struct_field("globals", field_i, |s| globals.encode(s)));
qinsoon's avatar
qinsoon committed
146
            }
147
            field_i += 1;
qinsoon's avatar
qinsoon committed
148 149
            
            // func sigs
qinsoon's avatar
qinsoon committed
150
            trace!("...serializing func_sigs");
qinsoon's avatar
qinsoon committed
151 152
            {
                let func_sigs: &HashMap<_, _> = &self.func_sigs.read().unwrap();
153
                try!(s.emit_struct_field("func_sigs", field_i, |s| func_sigs.encode(s)));
qinsoon's avatar
qinsoon committed
154
            }
155
            field_i += 1;
qinsoon's avatar
qinsoon committed
156 157
            
            // funcs
qinsoon's avatar
qinsoon committed
158
            trace!("...serializing funcs");
qinsoon's avatar
qinsoon committed
159 160
            {
                let funcs : &HashMap<_, _> = &self.funcs.read().unwrap();
161
                try!(s.emit_struct_field("funcs", field_i, |s| {
qinsoon's avatar
qinsoon committed
162 163 164 165 166 167 168 169 170 171 172 173
                    s.emit_map(funcs.len(), |s| {
                        let mut i = 0;
                        for (k,v) in funcs.iter() {
                            s.emit_map_elt_key(i, |s| k.encode(s)).ok();
                            let func : &MuFunction = &v.read().unwrap();
                            s.emit_map_elt_val(i, |s| func.encode(s)).ok();
                            i += 1;
                        }
                        Ok(())
                    })
                }));
            }
174
            field_i += 1;
qinsoon's avatar
qinsoon committed
175 176
            
            // func_vers
qinsoon's avatar
qinsoon committed
177
            trace!("...serializing func_vers");
qinsoon's avatar
qinsoon committed
178 179
            {
                let func_vers : &HashMap<_, _> = &self.func_vers.read().unwrap();
180
                try!(s.emit_struct_field("func_vers", field_i, |s| {
qinsoon's avatar
qinsoon committed
181 182 183 184 185 186 187 188 189 190 191 192
                    s.emit_map(func_vers.len(), |s| {
                        let mut i = 0;
                        for (k, v) in func_vers.iter() {
                            try!(s.emit_map_elt_key(i, |s| k.encode(s)));
                            let func_ver : &MuFunctionVersion = &v.read().unwrap();
                            try!(s.emit_map_elt_val(i, |s| func_ver.encode(s)));
                            i += 1;
                        }
                        Ok(())
                    })
                }));
            }
193 194
            field_i += 1;

qinsoon's avatar
qinsoon committed
195
            // primordial
qinsoon's avatar
qinsoon committed
196
            trace!("...serializing primordial");
qinsoon's avatar
qinsoon committed
197 198
            {
                let primordial = &self.primordial.read().unwrap();
199
                try!(s.emit_struct_field("primordial", field_i, |s| primordial.encode(s)));
qinsoon's avatar
qinsoon committed
200
            }
201
            field_i += 1;
qinsoon's avatar
qinsoon committed
202 203
            
            // is_running
qinsoon's avatar
qinsoon committed
204
            trace!("...serializing is_running");
qinsoon's avatar
qinsoon committed
205
            {
206
                try!(s.emit_struct_field("is_running", field_i, |s| self.is_running.load(Ordering::SeqCst).encode(s)));
207
            }
208
            field_i += 1;
qinsoon's avatar
qinsoon committed
209 210 211 212 213 214 215

            // options
            trace!("...serializing vm_options");
            {
                try!(s.emit_struct_field("vm_options", field_i, |s| self.vm_options.encode(s)));
            }
            field_i += 1;
216
            
qinsoon's avatar
qinsoon committed
217
            // compiled_funcs
qinsoon's avatar
qinsoon committed
218
            trace!("...serializing compiled_funcs");
qinsoon's avatar
qinsoon committed
219 220
            {
                let compiled_funcs : &HashMap<_, _> = &self.compiled_funcs.read().unwrap();
221
                try!(s.emit_struct_field("compiled_funcs", field_i, |s| {
qinsoon's avatar
qinsoon committed
222 223 224 225 226 227 228 229 230 231 232 233
                    s.emit_map(compiled_funcs.len(), |s| {
                        let mut i = 0;
                        for (k, v) in compiled_funcs.iter() {
                            try!(s.emit_map_elt_key(i, |s| k.encode(s)));
                            let compiled_func : &CompiledFunction = &v.read().unwrap();
                            try!(s.emit_map_elt_val(i, |s| compiled_func.encode(s)));
                            i += 1;
                        }
                        Ok(())
                    })
                }));
            }
234
            field_i += 1;
qinsoon's avatar
qinsoon committed
235
            
qinsoon's avatar
qinsoon committed
236
            trace!("serializing finished");
237 238 239
            Ok(())
        })
    }
qinsoon's avatar
qinsoon committed
240 241
}

qinsoon's avatar
qinsoon committed
242 243
impl Decodable for VM {
    fn decode<D: Decoder>(d: &mut D) -> Result<VM, D::Error> {
244 245 246
        let mut field_i = 0;

        d.read_struct("VM", VM_SERIALIZE_FIELDS + 2, |d| {
qinsoon's avatar
qinsoon committed
247
            // next_id
248
            let next_id = try!(d.read_struct_field("next_id", field_i, |d| {
qinsoon's avatar
qinsoon committed
249 250
                d.read_usize()
            }));
251
            field_i += 1;
qinsoon's avatar
qinsoon committed
252 253
            
            // id_name_map
254 255 256
            let id_name_map = try!(d.read_struct_field("id_name_map", field_i, |d| Decodable::decode(d)));
            field_i += 1;

qinsoon's avatar
qinsoon committed
257
            // name_id_map
258 259
            let name_id_map = try!(d.read_struct_field("name_id_map", field_i, |d| Decodable::decode(d)));
            field_i += 1;
qinsoon's avatar
qinsoon committed
260 261
            
            // types
262 263 264 265
            let types = try!(d.read_struct_field("types", field_i, |d| Decodable::decode(d)));
            field_i += 1;

            // struct tag map
qinsoon's avatar
qinsoon committed
266
            {
267
                let mut struct_tag_map : HashMap<MuName, StructType_> = try!(d.read_struct_field("struct_tag_map", field_i, |d| Decodable::decode(d)));
qinsoon's avatar
qinsoon committed
268 269 270 271 272 273
                
                let mut map_guard = types::STRUCT_TAG_MAP.write().unwrap();
                map_guard.clear();
                for (k, v) in struct_tag_map.drain() {
                    map_guard.insert(k, v);
                }
274 275 276 277 278 279 280 281 282 283 284 285 286
                field_i += 1;
            }

            // hybrid tag map
            {
                let mut hybrid_tag_map : HashMap<MuName, HybridType_> = try!(d.read_struct_field("hybrid_tag_map", field_i, |d| Decodable::decode(d)));

                let mut map_guard = types::HYBRID_TAG_MAP.write().unwrap();
                map_guard.clear();
                for (k, v) in hybrid_tag_map.drain() {
                    map_guard.insert(k, v);
                }
                field_i += 1;
qinsoon's avatar
qinsoon committed
287
            }
qinsoon's avatar
qinsoon committed
288 289
            
            // backend_type_info
290 291
            let backend_type_info = try!(d.read_struct_field("backend_type_info", field_i, |d| Decodable::decode(d)));
            field_i += 1;
qinsoon's avatar
qinsoon committed
292 293
            
            // constants
294 295
            let constants = try!(d.read_struct_field("constants", field_i, |d| Decodable::decode(d)));
            field_i += 1;
qinsoon's avatar
qinsoon committed
296 297
            
            // globals
298 299
            let globals = try!(d.read_struct_field("globals", field_i, |d| Decodable::decode(d)));
            field_i += 1;
qinsoon's avatar
qinsoon committed
300 301
            
            // func sigs
302 303
            let func_sigs = try!(d.read_struct_field("func_sigs", field_i, |d| Decodable::decode(d)));
            field_i += 1;
qinsoon's avatar
qinsoon committed
304 305
            
            // funcs
306
            let funcs = try!(d.read_struct_field("funcs", field_i, |d| {
qinsoon's avatar
qinsoon committed
307 308 309 310 311 312 313 314 315 316
                d.read_map(|d, len| {
                    let mut map = HashMap::new();
                    for i in 0..len {
                        let key = try!(d.read_map_elt_key(i, |d| Decodable::decode(d)));
                        let val = RwLock::new(try!(d.read_map_elt_val(i, |d| Decodable::decode(d))));
                        map.insert(key, val);
                    }
                    Ok(map)
                })
            }));
317
            field_i += 1;
qinsoon's avatar
qinsoon committed
318 319
            
            // func_vers
320
            let func_vers = try!(d.read_struct_field("func_vers", field_i, |d| {
qinsoon's avatar
qinsoon committed
321 322 323 324 325 326 327 328 329 330
                d.read_map(|d, len| {
                    let mut map = HashMap::new();
                    for i in 0..len {
                        let key = try!(d.read_map_elt_key(i, |d| Decodable::decode(d)));
                        let val = RwLock::new(try!(d.read_map_elt_val(i, |d| Decodable::decode(d))));
                        map.insert(key, val);
                    }
                    Ok(map)
                })
            }));
331
            field_i += 1;
qinsoon's avatar
qinsoon committed
332 333
            
            // primordial
334 335
            let primordial = try!(d.read_struct_field("primordial", field_i, |d| Decodable::decode(d)));
            field_i += 1;
qinsoon's avatar
qinsoon committed
336 337

            // is_running
338 339
            let is_running = try!(d.read_struct_field("is_running", field_i, |d| Decodable::decode(d)));
            field_i += 1;
qinsoon's avatar
qinsoon committed
340 341 342 343

            // vm_options
            let vm_options = try!(d.read_struct_field("vm_options", field_i, |d| Decodable::decode(d)));
            field_i += 1;
qinsoon's avatar
qinsoon committed
344
            
qinsoon's avatar
qinsoon committed
345
            // compiled funcs
346
            let compiled_funcs = try!(d.read_struct_field("compiled_funcs", field_i, |d| {
qinsoon's avatar
qinsoon committed
347 348 349 350 351 352 353 354 355 356
                d.read_map(|d, len| {
                    let mut map = HashMap::new();
                    for i in 0..len {
                        let key = try!(d.read_map_elt_key(i, |d| Decodable::decode(d)));
                        let val = RwLock::new(try!(d.read_map_elt_val(i, |d| Decodable::decode(d))));
                        map.insert(key, val);
                    }
                    Ok(map)
                })
            }));
357
            field_i += 1;
qinsoon's avatar
qinsoon committed
358
            
qinsoon's avatar
qinsoon committed
359 360 361 362 363 364 365 366
            let vm = VM{
                next_id: ATOMIC_USIZE_INIT,
                id_name_map: RwLock::new(id_name_map),
                name_id_map: RwLock::new(name_id_map),
                types: RwLock::new(types),
                backend_type_info: RwLock::new(backend_type_info),
                constants: RwLock::new(constants),
                globals: RwLock::new(globals),
367
                global_locations: RwLock::new(hashmap!{}),
qinsoon's avatar
qinsoon committed
368 369 370 371 372
                func_sigs: RwLock::new(func_sigs),
                funcs: RwLock::new(funcs),
                func_vers: RwLock::new(func_vers),
                primordial: RwLock::new(primordial),
                is_running: ATOMIC_BOOL_INIT,
qinsoon's avatar
qinsoon committed
373
                vm_options: vm_options,
374
                compiled_funcs: RwLock::new(compiled_funcs)
qinsoon's avatar
qinsoon committed
375 376 377 378 379 380 381 382 383 384
            };
            
            vm.next_id.store(next_id, Ordering::SeqCst);
            vm.is_running.store(is_running, Ordering::SeqCst);
            
            Ok(vm)
        })
    }
}

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
macro_rules! gen_handle_int {
    ($fn_from: ident, $fn_to: ident, $int_ty: ty) => {
        pub fn $fn_from (&self, num: $int_ty, len: BitSize) -> APIHandleResult {
            let handle_id = self.next_id();
            self.new_handle (APIHandle {
                id: handle_id,
                v: APIHandleValue::Int(num as u64, len)
            })
        }

        pub fn $fn_to (&self, handle: APIHandleArg) -> $int_ty {
            handle.v.as_int() as $int_ty
        }
    }
}

qinsoon's avatar
qinsoon committed
401 402
impl <'a> VM {
    pub fn new() -> VM {
qinsoon's avatar
qinsoon committed
403 404 405 406 407 408 409 410 411
        VM::new_internal(VMOptions::default())
    }

    pub fn new_with_opts(str: &str) -> VM {
        VM::new_internal(VMOptions::init(str))
    }

    fn new_internal(options: VMOptions) -> VM {
        VM::start_logging(options.flag_log_level);
qinsoon's avatar
qinsoon committed
412

qinsoon's avatar
qinsoon committed
413
        let ret = VM {
414
            next_id: ATOMIC_USIZE_INIT,
415
            is_running: ATOMIC_BOOL_INIT,
qinsoon's avatar
qinsoon committed
416 417
            vm_options: options,

qinsoon's avatar
qinsoon committed
418 419
            id_name_map: RwLock::new(HashMap::new()),
            name_id_map: RwLock::new(HashMap::new()),
qinsoon's avatar
qinsoon committed
420

qinsoon's avatar
qinsoon committed
421
            constants: RwLock::new(HashMap::new()),
qinsoon's avatar
qinsoon committed
422

qinsoon's avatar
qinsoon committed
423
            types: RwLock::new(HashMap::new()),
qinsoon's avatar
qinsoon committed
424
            backend_type_info: RwLock::new(HashMap::new()),
qinsoon's avatar
qinsoon committed
425

qinsoon's avatar
qinsoon committed
426
            globals: RwLock::new(HashMap::new()),
427
            global_locations: RwLock::new(hashmap!{}),
qinsoon's avatar
qinsoon committed
428

qinsoon's avatar
qinsoon committed
429
            func_sigs: RwLock::new(HashMap::new()),
430
            func_vers: RwLock::new(HashMap::new()),
qinsoon's avatar
qinsoon committed
431
            funcs: RwLock::new(HashMap::new()),
qinsoon's avatar
qinsoon committed
432
            compiled_funcs: RwLock::new(HashMap::new()),
qinsoon's avatar
qinsoon committed
433

434
            primordial: RwLock::new(None)
435
        };
qinsoon's avatar
qinsoon committed
436

437
        // insert all intenral types
qinsoon's avatar
qinsoon committed
438 439 440 441 442 443
        {
            let mut types = ret.types.write().unwrap();
            for ty in INTERNAL_TYPES.iter() {
                types.insert(ty.id(), ty.clone());
            }
        }
qinsoon's avatar
qinsoon committed
444

445
        ret.is_running.store(false, Ordering::SeqCst);
446 447 448 449 450 451 452 453 454 455

        // Does not need SeqCst.
        //
        // If VM creates Mu threads and Mu threads calls traps, the trap handler still "happens
        // after" the creation of the VM itself. Rust does not have a proper memory model, but this
        // is how C++ works.
        //
        // If the client needs to create client-level threads, however, the client should properly
        // synchronise at the time of inter-thread communication, rather than creation of the VM.
        ret.next_id.store(USER_ID_START, Ordering::Relaxed);
qinsoon's avatar
qinsoon committed
456

457 458 459 460
        // init types
        types::init_types();

        ret.init_runtime();
qinsoon's avatar
qinsoon committed
461

462 463
        ret
    }
qinsoon's avatar
qinsoon committed
464

465
    fn init_runtime(&self) {
qinsoon's avatar
qinsoon committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
        // init log
        VM::start_logging(self.vm_options.flag_log_level);

        // init gc
        {
            let ref options = self.vm_options;
            gc::gc_init(options.flag_gc_immixspace_size, options.flag_gc_lospace_size, options.flag_gc_nthreads);
        }
    }

    fn start_logging(level: MuLogLevel) {
        match level {
            MuLogLevel::None  => {},
            MuLogLevel::Error => VM::start_logging_internal(LogLevel::Error),
            MuLogLevel::Warn  => VM::start_logging_internal(LogLevel::Warn),
            MuLogLevel::Info  => VM::start_logging_internal(LogLevel::Info),
            MuLogLevel::Debug => VM::start_logging_internal(LogLevel::Debug),
            MuLogLevel::Trace => VM::start_logging_internal(LogLevel::Trace),
        }
qinsoon's avatar
qinsoon committed
485 486 487
    }

    pub fn start_logging_trace() {
qinsoon's avatar
qinsoon committed
488 489 490 491
        VM::start_logging_internal(LogLevel::Trace)
    }

    fn start_logging_internal(level: LogLevel) {
qinsoon's avatar
qinsoon committed
492 493
        use simple_logger;

qinsoon's avatar
qinsoon committed
494
        match simple_logger::init_with_level(level) {
qinsoon's avatar
qinsoon committed
495 496 497 498
            Ok(_) => {},
            Err(_) => {}
        }
    }
499
    
500 501 502
    pub fn resume_vm(serialized_vm: &str) -> VM {
        use rustc_serialize::json;
        
qinsoon's avatar
qinsoon committed
503
        let vm : VM = json::decode(serialized_vm).unwrap();
504
        
505
        vm.init_runtime();
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524

        // restore gc types
        {
            let type_info_guard = vm.backend_type_info.read().unwrap();
            let mut type_info_vec: Vec<Box<BackendTypeInfo>> = type_info_guard.values().map(|x| x.clone()).collect();
            type_info_vec.sort_by(|a, b| a.gc_type.id.cmp(&b.gc_type.id));

            let mut expect_id = 0;
            for ty_info in type_info_vec.iter() {
                use runtime::mm;

                let ref gc_type = ty_info.gc_type;

                if gc_type.id != expect_id {
                    debug_assert!(expect_id < gc_type.id);

                    while expect_id < gc_type.id {
                        use runtime::mm::common::gctype::GCType;

qinsoon's avatar
qinsoon committed
525
                        mm::add_gc_type(GCType::new_noreftype(0, 8));
526 527 528 529 530 531 532 533 534 535 536
                        expect_id += 1;
                    }
                }

                // now expect_id == gc_type.id
                debug_assert!(expect_id == gc_type.id);

                mm::add_gc_type(gc_type.as_ref().clone());
                expect_id += 1;
            }
        }
537 538 539 540
        
        vm
    }
    
541
    pub fn next_id(&self) -> MuID {
542 543 544 545
        // This only needs to be atomic, and does not need to be a synchronisation operation. The
        // only requirement for IDs is that all IDs obtained from `next_id()` are different. So
        // `Ordering::Relaxed` is sufficient.
        self.next_id.fetch_add(1, Ordering::Relaxed)
546 547
    }
    
548 549 550 551 552 553
    pub fn run_vm(&self) {
        self.is_running.store(true, Ordering::SeqCst);
    }
    
    pub fn is_running(&self) -> bool {
        self.is_running.load(Ordering::Relaxed)
qinsoon's avatar
qinsoon committed
554 555
    }
    
556
    pub fn set_name(&self, entity: &MuEntity, name: MuName) {
qinsoon's avatar
qinsoon committed
557
        let id = entity.id();
558
        entity.set_name(name.clone());
qinsoon's avatar
qinsoon committed
559 560
        
        let mut map = self.id_name_map.write().unwrap();
561
        map.insert(id, name.clone());
qinsoon's avatar
qinsoon committed
562 563 564 565 566
        
        let mut map2 = self.name_id_map.write().unwrap();
        map2.insert(name, id);
    }
    
567
    pub fn id_of_by_refstring(&self, name: &String) -> MuID {
qinsoon's avatar
qinsoon committed
568
        let map = self.name_id_map.read().unwrap();
569 570 571 572 573
        *map.get(name).unwrap()
    }
    
    pub fn id_of(&self, name: &str) -> MuID {
        self.id_of_by_refstring(&name.to_string())
qinsoon's avatar
qinsoon committed
574 575 576 577
    }
    
    pub fn name_of(&self, id: MuID) -> MuName {
        let map = self.id_name_map.read().unwrap();
578
        map.get(&id).unwrap().clone()
qinsoon's avatar
qinsoon committed
579 580
    }
    
qinsoon's avatar
qinsoon committed
581
    pub fn declare_const(&self, id: MuID, ty: P<MuType>, val: Constant) -> P<Value> {
qinsoon's avatar
qinsoon committed
582
        let mut constants = self.constants.write().unwrap();
583
        let ret = P(Value{hdr: MuEntityHeader::unnamed(id), ty: ty, v: Value_::Constant(val)});
584 585

        self.declare_const_internal(&mut constants, id, ret.clone());
qinsoon's avatar
qinsoon committed
586 587 588
        
        ret
    }
589 590 591 592 593 594 595

    fn declare_const_internal(&self, map: &mut RwLockWriteGuard<HashMap<MuID, P<Value>>>, id: MuID, val: P<Value>) {
        debug_assert!(!map.contains_key(&id));

        info!("declare const #{} = {}", id, val);
        map.insert(id, val);
    }
qinsoon's avatar
qinsoon committed
596
    
597 598 599 600 601 602 603
    pub fn get_const(&self, id: MuID) -> P<Value> {
        let const_lock = self.constants.read().unwrap();
        match const_lock.get(&id) {
            Some(ret) => ret.clone(),
            None => panic!("cannot find const #{}", id)
        }
    }
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622

    pub fn get_const_nocheck(&self, id: MuID) -> Option<P<Value>> {
        let const_lock = self.constants.read().unwrap();
        match const_lock.get(&id) {
            Some(ret) => Some(ret.clone()),
            None => None
        }
    }

    #[cfg(feature = "aot")]
    pub fn allocate_const(&self, val: P<Value>) -> ValueLocation {
        let id = val.id();
        let name = match val.name() {
            Some(name) => format!("CONST_{}_{}", id, name),
            None => format!("CONST_{}", id)
        };

        ValueLocation::Relocatable(backend::RegGroup::GPR, name)
    }
623
    
qinsoon's avatar
qinsoon committed
624 625
    pub fn declare_global(&self, id: MuID, ty: P<MuType>) -> P<Value> {
        let global = P(Value{
626
            hdr: MuEntityHeader::unnamed(id),
627
            ty: self.declare_type(self.next_id(), MuType_::iref(ty.clone())),
628
            v: Value_::Global(ty)
qinsoon's avatar
qinsoon committed
629
        });
qinsoon's avatar
qinsoon committed
630 631
        
        let mut globals = self.globals.write().unwrap();
632
        let mut global_locs = self.global_locations.write().unwrap();
633 634

        self.declare_global_internal(&mut globals, &mut global_locs, id, global.clone());
qinsoon's avatar
qinsoon committed
635
        
qinsoon's avatar
qinsoon committed
636
        global
qinsoon's avatar
qinsoon committed
637
    }
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670

    fn declare_global_internal(
        &self,
        globals: &mut RwLockWriteGuard<HashMap<MuID, P<Value>>>,
        global_locs: &mut RwLockWriteGuard<HashMap<MuID, ValueLocation>>,
        id: MuID, val: P<Value>
    ) {
        self.declare_global_internal_no_alloc(globals, id, val.clone());
        self.alloc_global(global_locs, id, val);
    }

    // when bulk declaring, we hold locks for everything, we cannot resolve backend type, and do alloc
    fn declare_global_internal_no_alloc(
        &self,
        globals: &mut RwLockWriteGuard<HashMap<MuID, P<Value>>>,
        id: MuID, val: P<Value>
    ) {
        debug_assert!(!globals.contains_key(&id));

        info!("declare global #{} = {}", id, val);
        globals.insert(id, val.clone());
    }

    fn alloc_global(
        &self,
        global_locs: &mut RwLockWriteGuard<HashMap<MuID, ValueLocation>>,
        id: MuID, val: P<Value>
    ) {
        let backend_ty = self.get_backend_type_info(val.ty.get_referenced_ty().unwrap().id());
        let loc = gc::allocate_global(val, backend_ty);
        info!("allocate global #{} as {}", id, loc);
        global_locs.insert(id, loc);
    }
qinsoon's avatar
qinsoon committed
671
    
qinsoon's avatar
qinsoon committed
672
    pub fn declare_type(&self, id: MuID, ty: MuType_) -> P<MuType> {
673
        let ty = P(MuType{hdr: MuEntityHeader::unnamed(id), v: ty});
qinsoon's avatar
qinsoon committed
674
        
qinsoon's avatar
qinsoon committed
675
        let mut types = self.types.write().unwrap();
676

677
        self.declare_type_internal(&mut types, id, ty.clone());
qinsoon's avatar
qinsoon committed
678 679 680
        
        ty
    }
681 682 683 684 685 686 687

    fn declare_type_internal(&self, types: &mut RwLockWriteGuard<HashMap<MuID, P<MuType>>>, id: MuID, ty: P<MuType>) {
        debug_assert!(!types.contains_key(&id));

        info!("declare type #{} = {}", id, ty);
        types.insert(id, ty.clone());
    }
qinsoon's avatar
qinsoon committed
688
    
689 690 691 692 693 694 695 696
    pub fn get_type(&self, id: MuID) -> P<MuType> {
        let type_lock = self.types.read().unwrap();
        match type_lock.get(&id) {
            Some(ret) => ret.clone(),
            None => panic!("cannot find type #{}", id)
        }
    }    
    
qinsoon's avatar
qinsoon committed
697
    pub fn declare_func_sig(&self, id: MuID, ret_tys: Vec<P<MuType>>, arg_tys: Vec<P<MuType>>) -> P<MuFuncSig> {
698
        let ret = P(MuFuncSig{hdr: MuEntityHeader::unnamed(id), ret_tys: ret_tys, arg_tys: arg_tys});
699 700 701

        let mut func_sigs = self.func_sigs.write().unwrap();
        self.declare_func_sig_internal(&mut func_sigs, id, ret.clone());
qinsoon's avatar
qinsoon committed
702 703 704
        
        ret
    }
705 706 707 708 709 710 711

    fn declare_func_sig_internal(&self, sigs: &mut RwLockWriteGuard<HashMap<MuID, P<MuFuncSig>>>, id: MuID, sig: P<MuFuncSig>) {
        debug_assert!(!sigs.contains_key(&id));

        info!("declare func sig #{} = {}", id, sig);
        sigs.insert(id, sig);
    }
qinsoon's avatar
qinsoon committed
712
    
713 714 715 716 717 718 719 720
    pub fn get_func_sig(&self, id: MuID) -> P<MuFuncSig> {
        let func_sig_lock = self.func_sigs.read().unwrap();
        match func_sig_lock.get(&id) {
            Some(ret) => ret.clone(),
            None => panic!("cannot find func sig #{}", id)
        }
    }
    
721
    pub fn declare_func (&self, func: MuFunction) {
qinsoon's avatar
qinsoon committed
722
        let mut funcs = self.funcs.write().unwrap();
723 724 725 726 727 728 729 730 731

        self.declare_func_internal(&mut funcs, func.id(), func);
    }

    fn declare_func_internal(&self, funcs: &mut RwLockWriteGuard<HashMap<MuID, RwLock<MuFunction>>>, id: MuID, func: MuFunction) {
        debug_assert!(!funcs.contains_key(&id));

        info!("declare func #{} = {}", id, func);
        funcs.insert(id, RwLock::new(func));
732 733
    }
    
734 735 736 737 738 739 740 741 742
    /// The IR builder needs to look-up the function signature from the existing function ID.
    pub fn get_func_sig_for_func(&self, id: MuID) -> P<MuFuncSig> {
        let funcs_lock = self.funcs.read().unwrap();
        match funcs_lock.get(&id) {
            Some(func) => func.read().unwrap().sig.clone(),
            None => panic!("cannot find Mu function #{}", id)
        }
    }    
    
743
    pub fn define_func_version (&self, func_ver: MuFunctionVersion) {
qinsoon's avatar
qinsoon committed
744
        info!("define function version {}", func_ver);
745
        // record this version
qinsoon's avatar
qinsoon committed
746
        let func_ver_id = func_ver.id();
747 748
        {
            let mut func_vers = self.func_vers.write().unwrap();
qinsoon's avatar
qinsoon committed
749
            func_vers.insert(func_ver_id, RwLock::new(func_ver));
750 751 752 753
        }
        
        // acquire a reference to the func_ver
        let func_vers = self.func_vers.read().unwrap();
qinsoon's avatar
qinsoon committed
754
        let func_ver = func_vers.get(&func_ver_id).unwrap().write().unwrap();
755 756 757
        
        // change current version to this (obsolete old versions)
        let funcs = self.funcs.read().unwrap();
qinsoon's avatar
qinsoon committed
758
        debug_assert!(funcs.contains_key(&func_ver.func_id)); // it should be declared before defining
qinsoon's avatar
qinsoon committed
759
        let mut func = funcs.get(&func_ver.func_id).unwrap().write().unwrap();
760
        
761 762 763 764
        func.new_version(func_ver.id());
        
        // redefinition happens here
        // do stuff        
765
    }
766 767 768 769 770 771 772 773 774 775 776 777 778 779

    /// Add a new bundle into VM.
    ///
    /// This function will drain the contents of all arguments.
    ///
    /// Ideally, this function should happen atomically. e.g. The client should not see a new type
    /// added without also seeing a new function added.
    pub fn declare_many(&self,
                        new_id_name_map: &mut HashMap<MuID, MuName>,
                        new_types: &mut HashMap<MuID, P<MuType>>,
                        new_func_sigs: &mut HashMap<MuID, P<MuFuncSig>>,
                        new_constants: &mut HashMap<MuID, P<Value>>,
                        new_globals: &mut HashMap<MuID, P<Value>>,
                        new_funcs: &mut HashMap<MuID, Box<MuFunction>>,
780 781
                        new_func_vers: &mut HashMap<MuID, Box<MuFunctionVersion>>,
                        arc_vm: Arc<VM>
782 783 784
                        ) {
        // Make sure other components, if ever acquiring multiple locks at the same time, acquire
        // them in this order, to prevent deadlock.
785 786 787 788 789 790 791 792 793
        {
            let mut id_name_map = self.id_name_map.write().unwrap();
            let mut name_id_map = self.name_id_map.write().unwrap();
            let mut types = self.types.write().unwrap();
            let mut constants = self.constants.write().unwrap();
            let mut globals = self.globals.write().unwrap();
            let mut func_sigs = self.func_sigs.write().unwrap();
            let mut funcs = self.funcs.write().unwrap();
            let mut func_vers = self.func_vers.write().unwrap();
794

795 796 797 798
            for (id, name) in new_id_name_map.drain() {
                id_name_map.insert(id, name.clone());
                name_id_map.insert(name, id);
            }
799

800 801 802
            for (id, obj) in new_types.drain() {
                self.declare_type_internal(&mut types, id, obj);
            }
803

804 805 806
            for (id, obj) in new_constants.drain() {
                self.declare_const_internal(&mut constants, id, obj);
            }
807

808 809 810 811
            for (id, obj) in new_globals.drain() {
                // we bulk allocate later (since we are holding all the locks, we cannot find ty info)
                self.declare_global_internal_no_alloc(&mut globals, id, obj);
            }
812

813 814 815
            for (id, obj) in new_func_sigs.drain() {
                self.declare_func_sig_internal(&mut func_sigs, id, obj);
            }
816

817 818 819
            for (id, obj) in new_funcs.drain() {
                self.declare_func_internal(&mut funcs, id, *obj);
            }
820

821 822 823
            for (id, obj) in new_func_vers.drain() {
                let func_id = obj.func_id;
                func_vers.insert(id, RwLock::new(*obj));
824

825 826 827 828 829 830
                {
                    trace!("Adding funcver {} as a version of {}...", id, func_id);
                    let func = funcs.get_mut(&func_id).unwrap();
                    func.write().unwrap().new_version(id);
                    trace!("Added funcver {} as a version of {} {:?}.", id, func_id, func);
                }
831 832 833
            }
        }
        // Locks released here
834 835 836 837 838 839 840

        // allocate all the globals defined
        {
            let globals = self.globals.read().unwrap();
            let mut global_locs = self.global_locations.write().unwrap();

            // make sure current thread has allocator
841
            let created = unsafe {MuThread::current_thread_as_mu_thread(Address::zero(), arc_vm.clone())};
842 843 844 845

            for (id, global) in globals.iter() {
                self.alloc_global(&mut global_locs, *id, global.clone());
            }
846 847 848 849

            if created {
                unsafe {MuThread::cleanup_current_mu_thread()};
            }
850
        }
851
    }
852
    
853
    pub fn add_compiled_func (&self, func: CompiledFunction) {
qinsoon's avatar
qinsoon committed
854
        debug_assert!(self.funcs.read().unwrap().contains_key(&func.func_id));
qinsoon's avatar
qinsoon committed
855
        debug_assert!(self.func_vers.read().unwrap().contains_key(&func.func_ver_id));
qinsoon's avatar
qinsoon committed
856

qinsoon's avatar
qinsoon committed
857
        self.compiled_funcs.write().unwrap().insert(func.func_ver_id, RwLock::new(func));
qinsoon's avatar
qinsoon committed
858 859
    }
    
qinsoon's avatar
qinsoon committed
860
    pub fn get_backend_type_info(&self, tyid: MuID) -> Box<BackendTypeInfo> {        
qinsoon's avatar
qinsoon committed
861 862 863
        {
            let read_lock = self.backend_type_info.read().unwrap();
        
864
            match read_lock.get(&tyid) {
qinsoon's avatar
qinsoon committed
865 866 867 868
                Some(info) => {return info.clone();},
                None => {}
            }
        }
869 870

        let types = self.types.read().unwrap();
qinsoon's avatar
qinsoon committed
871 872 873 874
        let ty = match types.get(&tyid) {
            Some(ty) => ty,
            None => panic!("invalid type id during get_backend_type_info(): {}", tyid)
        };
qinsoon's avatar
qinsoon committed
875
        let resolved = Box::new(backend::resolve_backend_type_info(ty, self));
qinsoon's avatar
qinsoon committed
876 877
        
        let mut write_lock = self.backend_type_info.write().unwrap();
878
        write_lock.insert(tyid, resolved.clone());
qinsoon's avatar
qinsoon committed
879 880 881 882
        
        resolved        
    }
    
qinsoon's avatar
qinsoon committed
883 884 885 886
    pub fn get_type_size(&self, tyid: MuID) -> ByteSize {
        self.get_backend_type_info(tyid).size
    }
    
qinsoon's avatar
qinsoon committed
887
    pub fn globals(&self) -> &RwLock<HashMap<MuID, P<Value>>> {
qinsoon's avatar
qinsoon committed
888 889 890
        &self.globals
    }
    
qinsoon's avatar
qinsoon committed
891
    pub fn funcs(&self) -> &RwLock<HashMap<MuID, RwLock<MuFunction>>> {
qinsoon's avatar
qinsoon committed
892
        &self.funcs
893
    }
894
    
qinsoon's avatar
qinsoon committed
895
    pub fn func_vers(&self) -> &RwLock<HashMap<MuID, RwLock<MuFunctionVersion>>> {
896 897
        &self.func_vers
    }
qinsoon's avatar
qinsoon committed
898 899 900 901 902 903 904 905 906 907 908

    pub fn get_cur_version_of(&self, fid: MuID) -> Option<MuID> {
        let funcs_guard = self.funcs.read().unwrap();
        match funcs_guard.get(&fid) {
            Some(rwlock_func) => {
                let func_guard = rwlock_func.read().unwrap();
                func_guard.cur_ver
            },
            None => None
        }
    }
909
    
qinsoon's avatar
qinsoon committed
910
    pub fn compiled_funcs(&self) -> &RwLock<HashMap<MuID, RwLock<CompiledFunction>>> {
911 912
        &self.compiled_funcs
    }
913 914 915 916 917 918 919 920
    
    pub fn types(&self) -> &RwLock<HashMap<MuID, P<MuType>>> {
        &self.types
    }
    
    pub fn func_sigs(&self) -> &RwLock<HashMap<MuID, P<MuFuncSig>>> {
        &self.func_sigs
    }
qinsoon's avatar
qinsoon committed
921
    
922 923 924 925 926 927 928
    pub fn resolve_function_address(&self, func_id: MuID) -> ValueLocation {
        let funcs = self.funcs.read().unwrap();
        let func : &MuFunction = &funcs.get(&func_id).unwrap().read().unwrap();
                
        if self.is_running() {
            unimplemented!()
        } else {
929
            ValueLocation::Relocatable(backend::RegGroup::GPR, func.name().unwrap())
930 931 932
        }
    }
    
qinsoon's avatar
qinsoon committed
933
    pub fn new_stack(&self, func_id: MuID) -> Box<MuStack> {
qinsoon's avatar
qinsoon committed
934 935 936
        let funcs = self.funcs.read().unwrap();
        let func : &MuFunction = &funcs.get(&func_id).unwrap().read().unwrap();
        
937 938 939
        Box::new(MuStack::new(self.next_id(), self.resolve_function_address(func_id), func))
    }
    
qinsoon's avatar
qinsoon committed
940
    pub fn make_primordial_thread(&self, func_id: MuID, has_const_args: bool, args: Vec<Constant>) {
941
        let mut guard = self.primordial.write().unwrap();
qinsoon's avatar
qinsoon committed
942
        *guard = Some(MuPrimordialThread{func_id: func_id, has_const_args: has_const_args, args: args});
qinsoon's avatar
qinsoon committed
943 944
    }
    
qinsoon's avatar
qinsoon committed
945
    #[allow(unused_variables)]
946
    pub fn make_boot_image(&self,
qinsoon's avatar
qinsoon committed
947 948 949 950 951 952
                           whitelist: Vec<MuID>,
                           primordial_func: Option<&APIHandle>, primordial_stack: Option<&APIHandle>,
                           primordial_threadlocal: Option<&APIHandle>,
                           sym_fields: Vec<&APIHandle>, sym_strings: Vec<String>,
                           reloc_fields: Vec<&APIHandle>, reloc_strings: Vec<String>,
                           output_file: String) {
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
        trace!("Making boot image...");

        let whitelist_funcs = {
            let compiler = Compiler::new(CompilerPolicy::default(), self);
            let funcs = self.funcs().read().unwrap();
            let func_vers = self.func_vers().read().unwrap();

            // make sure all functions in whitelist are compiled
            let mut whitelist_funcs: Vec<MuID> = vec![];
            for &id in whitelist.iter() {
                if let Some(f) = funcs.get(&id) {
                    whitelist_funcs.push(id);

                    let f: &MuFunction = &f.read().unwrap();
                    match f.cur_ver {
                        Some(fv_id) => {
                            let mut func_ver = func_vers.get(&fv_id).unwrap().write().unwrap();

                            if !func_ver.is_compiled() {
                                compiler.compile(&mut func_ver);
                            }
qinsoon's avatar
qinsoon committed
974
                        }
975
                        None => panic!("whitelist function {} has no version defined", f)
qinsoon's avatar
qinsoon committed
976 977 978
                    }
                }
            }
979 980 981 982 983 984 985

            whitelist_funcs
        };

        if primordial_threadlocal.is_some() {
            // we are going to need to persist this threadlocal
            unimplemented!()
qinsoon's avatar
qinsoon committed
986 987 988
        }

        // make sure only one of primordial_func or primoridial_stack is set
989 990
        let has_primordial_func  = primordial_func.is_some();
        let has_primordial_stack = primordial_stack.is_some();
qinsoon's avatar
qinsoon committed
991
        assert!(
992 993 994 995
            // do not have promordial stack/func
            (!has_primordial_func && !has_primordial_stack)
            // have either stack or func
            || ((has_primordial_func && !has_primordial_stack) || (!has_primordial_func && has_primordial_stack))
qinsoon's avatar
qinsoon committed
996
        );
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038

        // we assume client will start with a function (instead of a stack)
        if has_primordial_stack {
            unimplemented!()
        } else {
            // extract func id
            let func_id = primordial_func.unwrap().v.as_func();

            // make primordial thread in vm
            self.make_primordial_thread(func_id, false, vec![]);    // do not pass const args, use argc/argv

            // emit context (serialized vm, etc)
            backend::emit_context(self);

            // link
            self.link_boot_image(whitelist_funcs, output_file);
        }
    }

    #[cfg(feature = "aot")]
    fn link_boot_image(&self, funcs: Vec<MuID>, output_file: String) {
        use testutil;

        trace!("Linking boot image...");

        let func_names = {
            let funcs_guard = self.funcs().read().unwrap();
            funcs.iter().map(|x| funcs_guard.get(x).unwrap().read().unwrap().name().unwrap()).collect()
        };

        trace!("functions: {:?}", func_names);
        trace!("output   : {}", output_file);

        if output_file.ends_with("dylib") || output_file.ends_with("so") {
            // compile as dynamic library
            testutil::aot::link_dylib(func_names, &output_file, self);
        } else {
            // compile as executable
            testutil::aot::link_primordial(func_names, &output_file, self);
        }

        trace!("Done!");
qinsoon's avatar
qinsoon committed
1039
    }
1040

qinsoon's avatar
qinsoon committed
1041
    // -- API ---
1042 1043
    fn new_handle(&self, handle: APIHandle) -> APIHandleResult {
        let ret = Box::new(handle);
qinsoon's avatar
qinsoon committed
1044 1045 1046 1047

        ret
    }

1048
    pub fn new_fixed(&self, tyid: MuID) -> APIHandleResult {
1049
        let ty = self.get_type(tyid);
qinsoon's avatar
qinsoon committed
1050

1051 1052
        let backend_ty = self.get_backend_type_info(tyid);
        let addr = gc::allocate_fixed(ty.clone(), backend_ty);
qinsoon's avatar
qinsoon committed
1053 1054 1055 1056 1057 1058 1059 1060
        trace!("API: allocated fixed type {} at {}", ty, addr);

        self.new_handle(APIHandle {
            id: self.next_id(),
            v : APIHandleValue::Ref(ty, addr)
        })
    }

1061
    pub fn new_hybrid(&self, tyid: MuID, length: APIHandleArg) -> APIHandleResult {
qinsoon's avatar
qinsoon committed
1062 1063 1064
        let ty  = self.get_type(tyid);
        let len = self.handle_to_uint64(length);

1065 1066
        let backend_ty = self.get_backend_type_info(tyid);
        let addr = gc::allocate_hybrid(ty.clone(), len, backend_ty);
qinsoon's avatar
qinsoon committed
1067 1068 1069 1070 1071 1072 1073 1074
        trace!("API: allocated hybrid type {} of length {} at {}", ty, len, addr);

        self.new_handle(APIHandle {
            id: self.next_id(),
            v: APIHandleValue::Ref(ty, addr)
        })
    }

qinsoon's avatar
qinsoon committed
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
    pub fn handle_refcast(&self, from_op: APIHandleArg, to_ty: MuID) -> APIHandleResult {
        let handle_id = self.next_id();
        let to_ty = self.get_type(to_ty);

        trace!("API: refcast {} into type {}", from_op, to_ty);

        match from_op.v {
            APIHandleValue::Ref(_, addr) => {
                assert!(to_ty.is_ref());
                let inner_ty = to_ty.get_referenced_ty().unwrap();

                self.new_handle(APIHandle {
                    id: handle_id,
                    v: APIHandleValue::Ref(inner_ty, addr)
                })
            },
            APIHandleValue::IRef(_, addr) => {
                assert!(to_ty.is_iref());
                let inner_ty = to_ty.get_referenced_ty().unwrap();

                self.new_handle(APIHandle {
                    id: handle_id,
                    v : APIHandleValue::IRef(inner_ty, addr)
                })
            },
            APIHandleValue::FuncRef => unimplemented!(),

            _ => panic!("unexpected operand for refcast: {:?}", from_op)
        }
    }

1106
    pub fn handle_get_iref(&self, handle_ref: APIHandleArg) -> APIHandleResult {
qinsoon's avatar
qinsoon committed
1107 1108
        let (ty, addr) = handle_ref.v.as_ref();

qinsoon's avatar
qinsoon committed
1109
        /// FIXME: iref/ref share the same address - this actually depends on GC
qinsoon's avatar
qinsoon committed
1110
        // iref has the same address as ref
qinsoon's avatar
qinsoon committed
1111 1112 1113

        trace!("API: get iref from {:?}", handle_ref);

qinsoon's avatar
qinsoon committed
1114
        let ret = self.new_handle(APIHandle {
qinsoon's avatar
qinsoon committed
1115 1116
            id: self.next_id(),
            v : APIHandleValue::IRef(ty, addr)
qinsoon's avatar
qinsoon committed
1117 1118 1119 1120 1121
        });

        trace!("API: result {:?}", ret);

        ret