GitLab will be upgraded on 30 Jan 2023 from 2.00 pm (AEDT) to 3.00 pm (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.

test_convop.rs 4.99 KB
Newer Older
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
1
// Copyright 2017 The Australian National University
qinsoon's avatar
qinsoon committed
2
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
3
4
5
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
qinsoon's avatar
qinsoon committed
6
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
7
//     http://www.apache.org/licenses/LICENSE-2.0
qinsoon's avatar
qinsoon committed
8
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
9
10
11
12
13
14
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15
16
17
18
19
20
21
22
23
extern crate mu;
extern crate log;
extern crate libloading;

use self::mu::ast::types::*;
use self::mu::ast::ir::*;
use self::mu::ast::inst::*;
use self::mu::ast::op::*;
use self::mu::vm::*;
qinsoon's avatar
qinsoon committed
24
use self::mu::linkutils;
25
26
27
28
use mu::utils::LinkedHashMap;

#[test]
fn test_truncate_then_call() {
qinsoon's avatar
qinsoon committed
29
30
31
    let lib = linkutils::aot::compile_fncs(
        "truncate_then_call",
        vec!["truncate_then_call", "dummy_call"],
32
        &truncate_then_call
qinsoon's avatar
qinsoon committed
33
    );
34
35

    unsafe {
qinsoon's avatar
qinsoon committed
36
37
        let truncate_then_call: libloading::Symbol<unsafe extern "C" fn(u64) -> u32> =
            lib.get(b"truncate_then_call").unwrap();
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
97
98
99
100

        let res = truncate_then_call(1);
        println!("truncate_then_call(1) = {}", res);
        assert!(res == 1);
    }
}

fn truncate_then_call() -> VM {
    let vm = VM::new_with_opts("init_mu --disable-inline");

    typedef! ((vm) u64 = mu_int(64));
    typedef! ((vm) u32 = mu_int(32));

    funcsig! ((vm) dummy_call_sig = (u32) -> (u32));
    funcdecl!((vm) <dummy_call_sig> dummy_call);
    {
        // --- dummy call ---
        funcdef! ((vm) <dummy_call_sig> dummy_call VERSION dummy_call_v1);

        // entry
        block! ((vm, dummy_call_v1) blk_entry);
        ssa!   ((vm, dummy_call_v1) <u32> x);

        inst!  ((vm, dummy_call_v1) ret:
            RET (x)
        );

        define_block!((vm, dummy_call_v1) blk_entry(x) {
            ret
        });

        define_func_ver!((vm) dummy_call_v1 (entry: blk_entry) {
            blk_entry
        });
    }

    {
        // --- truncate_then_call ---
        typedef! ((vm) funcref_to_dummy = mu_funcref(dummy_call_sig));
        constdef!((vm) <funcref_to_dummy> funcref_dummy = Constant::FuncRef(dummy_call));

        funcsig! ((vm) sig = (u64) -> (u32));
        funcdecl!((vm) <sig> truncate_then_call);
        funcdef! ((vm) <sig> truncate_then_call VERSION truncate_then_call_v1);

        // entry
        block!((vm, truncate_then_call_v1) blk_entry);
        ssa!  ((vm, truncate_then_call_v1) <u64> arg);

        // %arg_u32 = TRUNC <u64 u32> arg
        ssa! ((vm, truncate_then_call_v1) <u32> arg_u32);
        inst!((vm, truncate_then_call_v1) blk_entry_truncate:
            arg_u32 = CONVOP (ConvOp::TRUNC) <u64 u32> arg
        );

        // %ret = CALL dummy_call (arg_u32)
        ssa!    ((vm, truncate_then_call_v1) <u32> res);
        consta! ((vm, truncate_then_call_v1) funcref_dummy_local = funcref_dummy);
        inst!   ((vm, truncate_then_call_v1) blk_entry_call:
            res = EXPRCALL (CallConvention::Mu, is_abort: false) funcref_dummy_local (arg_u32)
        );

        inst!((vm, truncate_then_call_v1) blk_entry_ret:
101
            RET (res)
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        );

        define_block!((vm, truncate_then_call_v1) blk_entry(arg) {
            blk_entry_truncate,
            blk_entry_call,
            blk_entry_ret
        });

        define_func_ver!((vm) truncate_then_call_v1 (entry: blk_entry) {
            blk_entry
        });
    }

    vm
qinsoon's avatar
qinsoon committed
116
}
qinsoon's avatar
qinsoon committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

#[test]
fn test_bitcast_f32_to_u32() {
    let lib = linkutils::aot::compile_fnc("bitcast_f32_to_u32", &bitcast_f32_to_u32);

    unsafe {
        use std::f32;

        let bitcast_f32_to_u32: libloading::Symbol<unsafe extern "C" fn(f32) -> u32> =
            lib.get(b"bitcast_f32_to_u32").unwrap();

        let res = bitcast_f32_to_u32(f32::MAX);
        println!("bitcast_f32_to_u32(f32::MAX) = {}", res);
        assert!(res == 2139095039u32);

        let res = bitcast_f32_to_u32(3.1415926f32);
        println!("bitcast_f32_to_u32(PI) = {}", res);
        assert!(res == 1078530010u32);
    }
}

fn bitcast_f32_to_u32() -> VM {
    let vm = VM::new();

    typedef!    ((vm) float = mu_float);
    typedef!    ((vm) u32 = mu_int(32));

    funcsig!    ((vm) sig = (float) -> (u32));
    funcdecl!   ((vm) <sig> bitcast_f32_to_u32);
    funcdef!    ((vm) <sig> bitcast_f32_to_u32 VERSION bitcast_f32_to_u32_v1);

    // blk entry
    block!      ((vm, bitcast_f32_to_u32_v1) blk_entry);
    ssa!        ((vm, bitcast_f32_to_u32_v1) <float> f);
    ssa!        ((vm, bitcast_f32_to_u32_v1) <u32> i);

    inst!       ((vm, bitcast_f32_to_u32_v1) blk_entry_bitcast:
        i = CONVOP (ConvOp::BITCAST) <float u32> f
    );

    inst!       ((vm, bitcast_f32_to_u32_v1) blk_entry_ret:
        RET (i)
    );

    define_block!((vm, bitcast_f32_to_u32_v1) blk_entry(f) {
        blk_entry_bitcast, blk_entry_ret
    });

    define_func_ver!((vm) bitcast_f32_to_u32_v1 (entry: blk_entry) {
        blk_entry
    });

    vm
}