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

test_ir.rs 5.27 KB
Newer Older
1
2
3
4
5
extern crate mu;

use self::mu::ast::types::*;
use self::mu::ast::ir::*;
use self::mu::ast::ptr::*;
qinsoon's avatar
qinsoon committed
6
use self::mu::ast::op::*;
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use self::mu::vm::context::*;

#[test]
#[allow(unused_variables)]
fn test_factorial() {
    let vm = factorial();
}

#[allow(unused_variables)]
pub fn factorial() -> VMContext {
    let mut vm = VMContext::new();
    
    // .typedef @int_64 = int<64>
    // .typedef @int_1 = int<1>
    // .typedef @float = float
    // .typedef @double = double
    // .typedef @void = void
    // .typedef @int_8 = int<8>
    // .typedef @int_32 = int<32>
qinsoon's avatar
qinsoon committed
26
27
28
29
30
31
32
    let type_def_int64 = vm.declare_type("int_64", P(MuType::int(64)));
    let type_def_int1  = vm.declare_type("int_1", P(MuType::int(1)));
    let type_def_float = vm.declare_type("float", P(MuType::float()));
    let type_def_double = vm.declare_type("double", P(MuType::double()));
    let type_def_void  = vm.declare_type("void", P(MuType::void()));
    let type_def_int8  = vm.declare_type("int8", P(MuType::int(8)));
    let type_def_int32 = vm.declare_type("int32", P(MuType::int(32)));
33
34
    
    // .const @int_64_1 <@int_64> = 1
35
    let const_def_int64_1 = vm.declare_const("int64_1", type_def_int64.clone(), Constant::Int(1));
36
37
38
39
40
    
    // .funcsig @fac_sig = (@int_64) -> (@int_64)
    let fac_sig = vm.declare_func_sig("fac_sig", vec![type_def_int64.clone()], vec![type_def_int64.clone()]);
    
    // .funcdef @fac VERSION @fac_v1 <@fac_sig>
41
    let mut func = MuFunction::new("fac", fac_sig.clone());
42
43
44
    
    // %blk_0(<@int_64> %n_3):
    let mut blk_0 = Block::new("blk_0");
qinsoon's avatar
qinsoon committed
45
    let blk_0_n_3 = func.new_ssa(0, "blk_0_n_3", type_def_int64.clone());
46
    let const_def_int64_1_local = func.new_value(const_def_int64_1.clone());
47
48
    
    //   %v48 = EQ <@int_64> %n_3 @int_64_1
qinsoon's avatar
qinsoon committed
49
    let blk_0_v48 = func.new_ssa(1, "blk_0_v48", type_def_int64.clone());
qinsoon's avatar
qinsoon committed
50
    let blk_0_v48_expr = Expression_::CmpOp(
51
52
        CmpOp::EQ,
        blk_0_n_3.clone(),
qinsoon's avatar
qinsoon committed
53
        const_def_int64_1_local.clone()
54
    );
qinsoon's avatar
qinsoon committed
55
    let blk_0_inst0 = TreeNode::new_inst(Instruction::Assign{left: vec![blk_0_v48.clone()], right: blk_0_v48_expr});
56
57
    
    //   BRANCH2 %v48 %blk_2(@int_64_1) %blk_1(%n_3)        
qinsoon's avatar
qinsoon committed
58
    let blk_0_term = TreeNode::new_inst(Instruction::Branch2{
59
60
61
        cond: blk_0_v48.clone(), 
        true_dest: Destination {
            target: "blk_2", 
qinsoon's avatar
qinsoon committed
62
            args: vec![DestArg::Normal(const_def_int64_1_local.clone())]
63
64
65
66
67
        },
        false_dest: Destination {
            target: "blk_1", 
            args: vec![DestArg::Normal(blk_0_n_3.clone())]
        }
qinsoon's avatar
qinsoon committed
68
    });
69
70
71
    
    let blk_0_content = BlockContent {
        args: vec![blk_0_n_3.clone()], 
72
        body: vec![blk_0_inst0, blk_0_term], 
73
74
        keepalives: None
    }; 
75
    blk_0.content = Some(blk_0_content);
76
77
78

    // %blk_2(<@int_64> %v53):
    let mut blk_2 = Block::new("blk_2");
qinsoon's avatar
qinsoon committed
79
    let blk_2_v53 = func.new_ssa(2, "blk_2_v53", type_def_int64.clone());
80
81
    
    //   RET %v53
qinsoon's avatar
qinsoon committed
82
    let blk_2_term = TreeNode::new_inst(Instruction::Return(vec![blk_2_v53.clone()]));
83
84
85
    
    let blk_2_content = BlockContent {
        args: vec![blk_2_v53.clone()], 
86
        body: vec![blk_2_term], 
87
88
        keepalives: None
    };
89
    blk_2.content = Some(blk_2_content);
90
91
92
    
    // %blk_1(<@int_64> %n_3):
    let mut blk_1 = Block::new("blk_1");
qinsoon's avatar
qinsoon committed
93
    let blk_1_n_3 = func.new_ssa(3, "blk_1_n_3", type_def_int64.clone());
94
95
    
    //   %v50 = SUB <@int_64> %n_3 @int_64_1
qinsoon's avatar
qinsoon committed
96
    let blk_1_v50 = func.new_ssa(4, "blk_1_v50", type_def_int64.clone());
qinsoon's avatar
qinsoon committed
97
    let blk_1_v50_expr = Expression_::BinOp(
98
99
        BinOp::Sub,
        blk_1_n_3.clone(),
qinsoon's avatar
qinsoon committed
100
        const_def_int64_1_local.clone()
101
    );
qinsoon's avatar
qinsoon committed
102
    let blk_1_inst0 = TreeNode::new_inst(Instruction::Assign{left: vec![blk_1_v50.clone()], right: blk_1_v50_expr});
103
104
    
    //   %v51 = CALL <@fac_sig> @fac (%v50)
qinsoon's avatar
qinsoon committed
105
    let blk_1_v51 = func.new_ssa(5, "blk_1_v51", type_def_int64.clone());
qinsoon's avatar
qinsoon committed
106
    let blk_1_inst1 = TreeNode::new_inst(Instruction::Assign{
107
            left: vec![blk_1_v51.clone()],
qinsoon's avatar
qinsoon committed
108
            right: Expression_::ExprCall {
109
                data: CallData {
qinsoon's avatar
qinsoon committed
110
                    func: func.new_ssa(6, "blk_1_fac", P(MuType::funcref(fac_sig.clone()))),
111
112
113
114
115
                    args: vec![blk_1_v50.clone()],
                    convention: CallConvention::Mu
                },
                is_abort: true
            }
qinsoon's avatar
qinsoon committed
116
    });
117
118
    
    //   %v52 = MUL <@int_64> %n_3 %v51
qinsoon's avatar
qinsoon committed
119
    let blk_1_v52 = func.new_ssa(7, "blk_1_v52", type_def_int64.clone());
qinsoon's avatar
qinsoon committed
120
    let blk_1_v52_expr = Expression_::BinOp(
121
        BinOp::Mul,
122
123
        blk_1_n_3.clone(),
        blk_1_v51.clone()
124
    );
qinsoon's avatar
qinsoon committed
125
    let blk_1_inst2 = TreeNode::new_inst(Instruction::Assign{
126
127
            left: vec![blk_1_v52.clone()], 
            right: blk_1_v52_expr
qinsoon's avatar
qinsoon committed
128
    });
129
    
qinsoon's avatar
qinsoon committed
130
    let blk_1_term = TreeNode::new_inst(Instruction::Branch1 (
131
132
        Destination {
            target: "blk_2", 
133
            args: vec![DestArg::Normal(blk_1_v52.clone())]
134
        }
qinsoon's avatar
qinsoon committed
135
    ));
136
    
137
138
139
    let blk_1_content = BlockContent {
        args: vec![blk_1_n_3.clone()], 
        body: vec![blk_1_inst0, blk_1_inst1, blk_1_inst2, blk_1_term],
140
141
        keepalives: None
    };
142
    blk_1.content = Some(blk_1_content);
143
144
    
    // wrap into a function
145
146
147
148
149
150
151
    func.define(FunctionContent{
            entry: "blk_0", 
            blocks: vec![
                ("blk_0", blk_0),
                ("blk_1", blk_1),
                ("blk_2", blk_2)
            ]}
152
153
    );
    
154
155
    vm.declare_func(func);
    
156
157
    vm
}