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

test_alloc.rs 5.31 KB
Newer Older
qinsoon's avatar
qinsoon committed
1
2
3
4
5
6
7
8
9
10
11
12
13
extern crate log;
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;
14
use self::mu::testutil::aot;
qinsoon's avatar
qinsoon committed
15
16
17

#[test]
fn test_instruction_new() {
qinsoon's avatar
qinsoon committed
18
    VM::start_logging_trace();
qinsoon's avatar
qinsoon committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    
    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);
    }
    
34
    vm.make_primordial_thread(func_id, vec![]);
qinsoon's avatar
qinsoon committed
35
36
    backend::emit_context(&vm);
    
37
38
    let executable = aot::link_primordial(vec!["alloc_new".to_string()], "alloc_new_test");
    aot::execute(executable);
qinsoon's avatar
qinsoon committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
}

#[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)
62
    let func_sig = vm.declare_func_sig(vm.next_id(), vec![type_def_int64.clone()], vec![]);
qinsoon's avatar
qinsoon committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    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());
qinsoon's avatar
qinsoon committed
81
82
    let blk_0_inst0 = func_ver.new_inst(Instruction{
        hdr: MuEntityHeader::unnamed(vm.next_id()),
qinsoon's avatar
qinsoon committed
83
84
85
86
87
88
89
90
        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());
qinsoon's avatar
qinsoon committed
91
92
    let blk_0_inst1 = func_ver.new_inst(Instruction{
        hdr: MuEntityHeader::unnamed(vm.next_id()),
qinsoon's avatar
qinsoon committed
93
94
95
96
97
        value: Some(vec![blk_0_a_iref.clone_value()]),
        ops: RwLock::new(vec![blk_0_a.clone()]),
        v: Instruction_::GetIRef(0)
    });
    
98
    // STORE <@int_64> @a_iref @int_64_1
qinsoon's avatar
qinsoon committed
99
100
101
    let blk_0_const_int64_1 = func_ver.new_constant(const_def_int64_1.clone());
    let blk_0_inst2 = func_ver.new_inst(Instruction{
        hdr: MuEntityHeader::unnamed(vm.next_id()),
qinsoon's avatar
qinsoon committed
102
        value: None,
103
        ops: RwLock::new(vec![blk_0_a_iref.clone(), blk_0_const_int64_1.clone()]),
qinsoon's avatar
qinsoon committed
104
105
106
107
108
109
110
111
        v: Instruction_::Store{
            is_ptr: false,
            order: MemoryOrder::Relaxed,
            mem_loc: 0,
            value: 1
        }
    });    
        
112
113
114
115
116
117
118
119
120
121
122
123
124
//    // %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
//        }
//    });
    
qinsoon's avatar
qinsoon committed
125
126
    let blk_0_term = func_ver.new_inst(Instruction {
        hdr: MuEntityHeader::unnamed(vm.next_id()),
qinsoon's avatar
qinsoon committed
127
        value: None,
128
129
        ops: RwLock::new(vec![]),
        v: Instruction_::ThreadExit
qinsoon's avatar
qinsoon committed
130
131
132
133
    });
    
    let blk_0_content = BlockContent {
        args: vec![],
134
        exn_arg: None,
qinsoon's avatar
qinsoon committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
        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
}