To protect your data, the CISO officer has suggested users to enable GitLab 2FA as soon as possible.

test_alloc.rs 5.22 KB
Newer Older
qinsoon's avatar
qinsoon committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
extern crate log;
extern crate simple_logger;
extern crate libloading;
extern crate mu;

use self::mu::ast::types::*;
use self::mu::ast::ir::*;
use self::mu::ast::inst::*;
use self::mu::vm::*;
use self::mu::compiler::*;

use std::sync::Arc;
use std::sync::RwLock;
use std::collections::HashMap;
use aot;

#[test]
fn test_instruction_new() {
    simple_logger::init_with_level(log::LogLevel::Trace).ok();
    
    let vm = Arc::new(alloc_new());
    
    let compiler = Compiler::new(CompilerPolicy::default(), vm.clone());
    
    let func_id = vm.id_of("alloc_new");
    {
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
        let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();
        
        compiler.compile(&mut func_ver);
    }
    
35
    vm.make_primordial_thread(func_id, vec![]);
qinsoon's avatar
qinsoon committed
36
37
    backend::emit_context(&vm);
    
38
39
    let executable = aot::link_primordial(vec!["alloc_new".to_string()], "alloc_new_test");
    aot::execute(executable);
qinsoon's avatar
qinsoon committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
}

#[allow(unused_variables)]
pub fn alloc_new() -> VM {
    let vm = VM::new();
    
    // .typedef @int64 = int<64>
    // .typedef @iref_int64 = iref<int<64>>
    let type_def_int64 = vm.declare_type(vm.next_id(), MuType_::int(64));
    vm.set_name(type_def_int64.as_entity(), "int64".to_string());
    let type_def_iref_int64 = vm.declare_type(vm.next_id(), MuType_::iref(type_def_int64.clone()));
    vm.set_name(type_def_iref_int64.as_entity(), "iref_int64".to_string());
    let type_def_ref_int64 = vm.declare_type(vm.next_id(), MuType_::muref(type_def_int64.clone()));
    vm.set_name(type_def_ref_int64.as_entity(), "ref_int64".to_string());
    
    // .const @int_64_0 <@int_64> = 0
    // .const @int_64_1 <@int_64> = 1
    let const_def_int64_0 = vm.declare_const(vm.next_id(), type_def_int64.clone(), Constant::Int(0));
    vm.set_name(const_def_int64_0.as_entity(), "int64_0".to_string());
    let const_def_int64_1 = vm.declare_const(vm.next_id(), type_def_int64.clone(), Constant::Int(1));
    vm.set_name(const_def_int64_1.as_entity(), "int64_1".to_string());
    
    // .funcsig @alloc_new_sig = () -> (@int64)
63
    let func_sig = vm.declare_func_sig(vm.next_id(), vec![type_def_int64.clone()], vec![]);
qinsoon's avatar
qinsoon committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    vm.set_name(func_sig.as_entity(), "alloc_new_sig".to_string());

    // .funcdecl @alloc_new <@alloc_new_sig>
    let func = MuFunction::new(vm.next_id(), func_sig.clone());
    vm.set_name(func.as_entity(), "alloc_new".to_string());
    let func_id = func.id();
    vm.declare_func(func);
    
    // .funcdef @alloc VERSION @v1 <@alloc_new_sig>
    let mut func_ver = MuFunctionVersion::new(vm.next_id(), func_id, func_sig.clone());
    
    // %blk_0():
    let mut blk_0 = Block::new(vm.next_id());
    vm.set_name(blk_0.as_entity(), "blk_0".to_string());
    
    // %a = NEW <@int64_t>
    let blk_0_a = func_ver.new_ssa(vm.next_id(), type_def_ref_int64.clone());
    vm.set_name(blk_0_a.as_entity(), "blk_0_a".to_string());
    let blk_0_inst0 = func_ver.new_inst(vm.next_id(), Instruction{
        value: Some(vec![blk_0_a.clone_value()]),
        ops: RwLock::new(vec![]),
        v: Instruction_::New(type_def_int64.clone())
    });
    
    // %a_iref = GETIREF <@int_64> @a
    let blk_0_a_iref = func_ver.new_ssa(vm.next_id(), type_def_iref_int64.clone());
    vm.set_name(blk_0_a.as_entity(), "blk_0_a_iref".to_string());
    let blk_0_inst1 = func_ver.new_inst(vm.next_id(), Instruction{
        value: Some(vec![blk_0_a_iref.clone_value()]),
        ops: RwLock::new(vec![blk_0_a.clone()]),
        v: Instruction_::GetIRef(0)
    });
    
97
    // STORE <@int_64> @a_iref @int_64_1
qinsoon's avatar
qinsoon committed
98
99
100
    let blk_0_const_int64_1 = func_ver.new_constant(vm.next_id(), const_def_int64_1.clone());
    let blk_0_inst2 = func_ver.new_inst(vm.next_id(), Instruction{
        value: None,
101
        ops: RwLock::new(vec![blk_0_a_iref.clone(), blk_0_const_int64_1.clone()]),
qinsoon's avatar
qinsoon committed
102
103
104
105
106
107
108
109
        v: Instruction_::Store{
            is_ptr: false,
            order: MemoryOrder::Relaxed,
            mem_loc: 0,
            value: 1
        }
    });    
        
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//    // %x = LOAD <@int_64> @a_iref
//    let blk_0_x = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
//    vm.set_name(blk_0_x.as_entity(), "blk_0_x".to_string());
//    let blk_0_inst3 = func_ver.new_inst(vm.next_id(), Instruction{
//        value: Some(vec![blk_0_x.clone_value()]),
//        ops: RwLock::new(vec![blk_0_a_iref.clone()]),
//        v: Instruction_::Load{
//            is_ptr: false,
//            order: MemoryOrder::Relaxed,
//            mem_loc: 0
//        }
//    });
    
    let blk_0_term = func_ver.new_inst(vm.next_id(), Instruction {
qinsoon's avatar
qinsoon committed
124
        value: None,
125
126
        ops: RwLock::new(vec![]),
        v: Instruction_::ThreadExit
qinsoon's avatar
qinsoon committed
127
128
129
130
    });
    
    let blk_0_content = BlockContent {
        args: vec![],
131
        exn_arg: None,
qinsoon's avatar
qinsoon committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
        body: vec![blk_0_inst0, blk_0_inst1, blk_0_inst2, blk_0_term],
        keepalives: None
    };
    blk_0.content = Some(blk_0_content);
    
    func_ver.define(FunctionContent{
        entry: blk_0.id(),
        blocks: {
            let mut ret = HashMap::new();
            ret.insert(blk_0.id(), blk_0);
            ret
        }
    });
    
    vm.define_func_version(func_ver);
    
    vm
}