test_regalloc.rs 70.4 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
extern crate mu;
16
extern crate libloading;
17

18 19
use mu::linkutils;
use mu::linkutils::aot;
20
use mu::utils::LinkedHashMap;
21
use mu::compiler::backend::x86_64;
22
use test_compiler::test_call::gen_ccall_exit;
23
use self::mu::compiler::*;
24
use self::mu::ast::ir::*;
qinsoon's avatar
qinsoon committed
25 26 27 28
use self::mu::ast::types::*;
use self::mu::ast::inst::*;
use self::mu::ast::op::*;
use self::mu::vm::VM;
29 30 31

use std::sync::Arc;

qinsoon's avatar
qinsoon committed
32 33
// NOTE: aarch64 has 28 usable GPRs (wheras x86-64 has 14)
// so there are slightly different tests for spilling on aarch64
34

qinsoon's avatar
[wip]  
qinsoon committed
35 36
fn get_number_of_moves(fv_id: MuID, vm: &VM) -> usize {
    let cfs = vm.compiled_funcs().read().unwrap();
qinsoon's avatar
qinsoon committed
37
    let cf = cfs.get(&fv_id).unwrap().read().unwrap();
qinsoon's avatar
[wip]  
qinsoon committed
38 39 40 41 42 43 44 45 46 47 48 49 50

    let mut n_mov_insts = 0;

    let mc = cf.mc();
    for i in 0..mc.number_of_insts() {
        if mc.is_move(i) {
            n_mov_insts += 1;
        }
    }

    n_mov_insts
}

qinsoon's avatar
qinsoon committed
51
#[test]
qinsoon's avatar
qinsoon committed
52
#[allow(unused_variables)]
qinsoon's avatar
qinsoon committed
53
fn test_spill1() {
qinsoon's avatar
qinsoon committed
54
    VM::start_logging_trace();
qinsoon's avatar
qinsoon committed
55

qinsoon's avatar
qinsoon committed
56
    let vm = Arc::new(create_spill1());
qinsoon's avatar
qinsoon committed
57

qinsoon's avatar
qinsoon committed
58
    let compiler = Compiler::new(CompilerPolicy::default(), &vm);
qinsoon's avatar
qinsoon committed
59

qinsoon's avatar
qinsoon committed
60 61 62 63 64
    let func_id = vm.id_of("spill1");
    {
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
qinsoon's avatar
qinsoon committed
65 66 67 68 69 70
        let mut func_ver = func_vers
            .get(&func.cur_ver.unwrap())
            .unwrap()
            .write()
            .unwrap();

qinsoon's avatar
qinsoon committed
71 72
        compiler.compile(&mut func_ver);
    }
qinsoon's avatar
qinsoon committed
73 74 75

    backend::emit_context(&vm);

qinsoon's avatar
qinsoon committed
76 77 78
    let dylib = aot::link_dylib(
        vec![Mu("spill1")],
        &linkutils::get_dylib_name("spill1"),
79
        &vm
qinsoon's avatar
qinsoon committed
80
    );
qinsoon's avatar
qinsoon committed
81 82 83

    let lib = libloading::Library::new(dylib.as_os_str()).unwrap();
    unsafe {
qinsoon's avatar
qinsoon committed
84
        let spill1: libloading::Symbol<unsafe extern "C" fn() -> u64> = match lib.get(b"spill1") {
qinsoon's avatar
qinsoon committed
85
            Ok(symbol) => symbol,
86
            Err(e) => panic!("cannot find symbol spill1 in dylib: {:?}", e)
qinsoon's avatar
qinsoon committed
87
        };
88 89

        // we cannot call this (it doesnt return)
qinsoon's avatar
qinsoon committed
90
    }
qinsoon's avatar
qinsoon committed
91 92
}

93
#[cfg(target_arch = "x86_64")]
qinsoon's avatar
qinsoon committed
94 95
fn create_spill1() -> VM {
    let vm = VM::new();
96 97 98 99 100 101 102 103 104

    typedef!        ((vm) int64 = mu_int(64));

    funcsig!        ((vm) spill1_sig = (int64, int64, int64, int64, int64,
                                        int64, int64, int64, int64, int64) -> (int64));
    funcdecl!       ((vm) <spill1_sig> spill1);
    funcdef!        ((vm) <spill1_sig> spill1 VERSION spill1_v1);

    typedef!        ((vm) funcref_spill1 = mu_funcref(spill1_sig));
qinsoon's avatar
qinsoon committed
105 106 107
    constdef!       ((vm) <funcref_spill1> const_funcref_spill1 =
        Constant::FuncRef(vm.id_of("spill1")));

qinsoon's avatar
qinsoon committed
108
    // %entry(<@int_64> %t1, t2, ... t10):
109 110 111 112 113 114 115 116 117 118 119
    block!          ((vm, spill1_v1) blk_entry);
    ssa!            ((vm, spill1_v1) <int64> t1);
    ssa!            ((vm, spill1_v1) <int64> t2);
    ssa!            ((vm, spill1_v1) <int64> t3);
    ssa!            ((vm, spill1_v1) <int64> t4);
    ssa!            ((vm, spill1_v1) <int64> t5);
    ssa!            ((vm, spill1_v1) <int64> t6);
    ssa!            ((vm, spill1_v1) <int64> t7);
    ssa!            ((vm, spill1_v1) <int64> t8);
    ssa!            ((vm, spill1_v1) <int64> t9);
    ssa!            ((vm, spill1_v1) <int64> t10);
qinsoon's avatar
qinsoon committed
120

121
    // %x = CALL spill1(%t1, %t2, ... t10)
122 123 124 125 126 127
    ssa!            ((vm, spill1_v1) <int64> x);
    consta!         ((vm, spill1_v1) const_funcref_spill1_local = const_funcref_spill1);
    inst!           ((vm, spill1_v1) blk_entry_call:
        x = EXPRCALL (CallConvention::Mu, is_abort: false)
                     const_funcref_spill1_local(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10)
    );
qinsoon's avatar
qinsoon committed
128

qinsoon's avatar
qinsoon committed
129
    // %res0 = ADD %t1 %t2
130 131 132 133
    ssa!            ((vm, spill1_v1) <int64> res0);
    inst!           ((vm, spill1_v1) blk_entry_add0:
        res0 = BINOP (BinOp::Add) t1 t2
    );
qinsoon's avatar
qinsoon committed
134

qinsoon's avatar
qinsoon committed
135
    // %res1 = ADD %res0 %t3
136 137 138 139
    ssa!            ((vm, spill1_v1) <int64> res1);
    inst!           ((vm, spill1_v1) blk_entry_add1:
        res1 = BINOP (BinOp::Add) res0 t3
    );
qinsoon's avatar
qinsoon committed
140

qinsoon's avatar
qinsoon committed
141
    // %res2 = ADD %res1 %t4
142 143 144 145
    ssa!            ((vm, spill1_v1) <int64> res2);
    inst!           ((vm, spill1_v1) blk_entry_add2:
        res2 = BINOP (BinOp::Add) res1 t4
    );
qinsoon's avatar
qinsoon committed
146

qinsoon's avatar
qinsoon committed
147
    // %res3 = ADD %res2 %t5
148 149 150 151
    ssa!            ((vm, spill1_v1) <int64> res3);
    inst!           ((vm, spill1_v1) blk_entry_add3:
        res3 = BINOP (BinOp::Add) res2 t5
    );
qinsoon's avatar
qinsoon committed
152

qinsoon's avatar
qinsoon committed
153
    // RET %res3
154 155 156 157 158 159
    inst!           ((vm, spill1_v1) blk_entry_ret:
        RET (res3)
    );

    define_block!   ((vm, spill1_v1) blk_entry
        (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) {
qinsoon's avatar
qinsoon committed
160 161 162 163 164 165 166
            blk_entry_call,
            blk_entry_add0,
            blk_entry_add1,
            blk_entry_add2,
            blk_entry_add3,
            blk_entry_ret
        }
167 168 169 170 171
    );

    define_func_ver!((vm) spill1_v1(entry: blk_entry) {
        blk_entry
    });
qinsoon's avatar
qinsoon committed
172

qinsoon's avatar
qinsoon committed
173 174 175
    vm
}

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
#[cfg(target_arch = "aarch64")]
fn create_spill1() -> VM {
    let vm = VM::new();

    typedef!        ((vm) int64 = mu_int(64));

    funcsig!        ((vm) spill1_sig = (int64, int64, int64, int64, int64,
                                        int64, int64, int64, int64, int64,
                                        int64, int64, int64, int64, int64,
                                        int64, int64, int64, int64, int64,
                                        int64, int64, int64, int64) -> (int64));
    funcdecl!       ((vm) <spill1_sig> spill1);
    funcdef!        ((vm) <spill1_sig> spill1 VERSION spill1_v1);

    typedef!        ((vm) funcref_spill1 = mu_funcref(spill1_sig));
qinsoon's avatar
qinsoon committed
191 192
    constdef!       ((vm) <funcref_spill1> const_funcref_spill1 =
        Constant::FuncRef(vm.id_of("spill1")));
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

    // %entry(<@int_64> %t1, t2, ... t10):
    block!          ((vm, spill1_v1) blk_entry);
    ssa!            ((vm, spill1_v1) <int64> t1);
    ssa!            ((vm, spill1_v1) <int64> t2);
    ssa!            ((vm, spill1_v1) <int64> t3);
    ssa!            ((vm, spill1_v1) <int64> t4);
    ssa!            ((vm, spill1_v1) <int64> t5);
    ssa!            ((vm, spill1_v1) <int64> t6);
    ssa!            ((vm, spill1_v1) <int64> t7);
    ssa!            ((vm, spill1_v1) <int64> t8);
    ssa!            ((vm, spill1_v1) <int64> t9);
    ssa!            ((vm, spill1_v1) <int64> t10);
    ssa!            ((vm, spill1_v1) <int64> t11);
    ssa!            ((vm, spill1_v1) <int64> t12);
    ssa!            ((vm, spill1_v1) <int64> t13);
    ssa!            ((vm, spill1_v1) <int64> t14);
    ssa!            ((vm, spill1_v1) <int64> t15);
    ssa!            ((vm, spill1_v1) <int64> t16);
    ssa!            ((vm, spill1_v1) <int64> t17);
    ssa!            ((vm, spill1_v1) <int64> t18);
    ssa!            ((vm, spill1_v1) <int64> t19);
    ssa!            ((vm, spill1_v1) <int64> t20);
    ssa!            ((vm, spill1_v1) <int64> t21);
    ssa!            ((vm, spill1_v1) <int64> t22);
    ssa!            ((vm, spill1_v1) <int64> t23);
    ssa!            ((vm, spill1_v1) <int64> t24);

    // %x = CALL spill1(%t1, %t2, ... t10)
    ssa!            ((vm, spill1_v1) <int64> x);
    consta!         ((vm, spill1_v1) const_funcref_spill1_local = const_funcref_spill1);
    inst!           ((vm, spill1_v1) blk_entry_call:
        x = EXPRCALL (CallConvention::Mu, is_abort: false)
                     const_funcref_spill1_local(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10,
                      t11, t12, t13, t14, t15, t16, t17, t18, t19, t20, t21, t22, t23, t24)
    );
    // %res0 = ADD %t1 %t2
    ssa!            ((vm, spill1_v1) <int64> res0);
    inst!           ((vm, spill1_v1) blk_entry_add0:
        res0 = BINOP (BinOp::Add) t1 t2
    );

    // %res1 = ADD %res0 %t3
    ssa!            ((vm, spill1_v1) <int64> res1);
    inst!           ((vm, spill1_v1) blk_entry_add1:
        res1 = BINOP (BinOp::Add) res0 t3
    );

    // %res2 = ADD %res1 %t4
    ssa!            ((vm, spill1_v1) <int64> res2);
    inst!           ((vm, spill1_v1) blk_entry_add2:
        res2 = BINOP (BinOp::Add) res1 t4
    );

    // %res3 = ADD %res2 %t5
    ssa!            ((vm, spill1_v1) <int64> res3);
    inst!           ((vm, spill1_v1) blk_entry_add3:
        res3 = BINOP (BinOp::Add) res2 t5
    );

    // RET %res3
    inst!           ((vm, spill1_v1) blk_entry_ret:
        RET (res3)
    );

    define_block!   ((vm, spill1_v1) blk_entry
qinsoon's avatar
qinsoon committed
259 260
        (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12,
         t13, t14, t15, t16, t17, t18, t19, t20, t21, t22, t23, t24) {
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
            blk_entry_call,
            blk_entry_add0,
            blk_entry_add1,
            blk_entry_add2,
            blk_entry_add3,
            blk_entry_ret
        }
    );

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

    vm
}

qinsoon's avatar
qinsoon committed
277 278
#[test]
fn test_simple_spill() {
qinsoon's avatar
qinsoon committed
279
    VM::start_logging_trace();
qinsoon's avatar
qinsoon committed
280 281 282

    let vm = Arc::new(create_simple_spill());

qinsoon's avatar
qinsoon committed
283
    let compiler = Compiler::new(CompilerPolicy::default(), &vm);
qinsoon's avatar
qinsoon committed
284 285 286 287 288 289

    let func_id = vm.id_of("simple_spill");
    {
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
qinsoon's avatar
qinsoon committed
290 291 292 293 294
        let mut func_ver = func_vers
            .get(&func.cur_ver.unwrap())
            .unwrap()
            .write()
            .unwrap();
qinsoon's avatar
qinsoon committed
295 296 297 298 299 300

        compiler.compile(&mut func_ver);
    }

    backend::emit_context(&vm);

qinsoon's avatar
qinsoon committed
301 302 303
    let dylib = aot::link_dylib(
        vec![Mu("simple_spill")],
        &linkutils::get_dylib_name("simple_spill"),
304
        &vm
qinsoon's avatar
qinsoon committed
305
    );
qinsoon's avatar
qinsoon committed
306 307 308

    let lib = libloading::Library::new(dylib.as_os_str()).unwrap();
    unsafe {
qinsoon's avatar
qinsoon committed
309 310 311
        let simple_spill: libloading::Symbol<unsafe extern "C" fn() -> u64> =
            match lib.get(b"simple_spill") {
                Ok(symbol) => symbol,
312
                Err(e) => panic!("cannot find symbol simple_spill in dylib: {:?}", e)
qinsoon's avatar
qinsoon committed
313
            };
qinsoon's avatar
qinsoon committed
314 315 316 317 318 319 320

        let res = simple_spill();
        println!("simple_spill() = {}", res);
        assert!(res == 2);
    }
}

321
#[cfg(target_arch = "x86_64")]
qinsoon's avatar
qinsoon committed
322 323 324
fn create_simple_spill() -> VM {
    let vm = VM::new();

325 326
    typedef!        ((vm) int64 = mu_int(64));
    constdef!       ((vm) <int64> int64_1 = Constant::Int(1));
qinsoon's avatar
qinsoon committed
327

328 329 330
    funcsig!        ((vm) simple_spill_sig = () -> (int64));
    funcdecl!       ((vm) <simple_spill_sig> simple_spill);
    funcdef!        ((vm) <simple_spill_sig> simple_spill VERSION simple_spill_v1);
qinsoon's avatar
qinsoon committed
331 332

    // %entry():
333
    block!          ((vm, simple_spill_v1) blk_entry);
qinsoon's avatar
qinsoon committed
334 335

    // BRANCH %start(1, 1, 1, 1, ..., 1) // 14 constant ONE
336 337 338 339 340 341 342 343 344 345 346 347 348
    consta!         ((vm, simple_spill_v1) int64_1_local = int64_1);
    block!          ((vm, simple_spill_v1) blk_start);
    inst!           ((vm, simple_spill_v1) blk_entry_branch:
        BRANCH blk_start (int64_1_local, int64_1_local, int64_1_local,
                          int64_1_local, int64_1_local, int64_1_local,
                          int64_1_local, int64_1_local, int64_1_local,
                          int64_1_local, int64_1_local, int64_1_local,
                          int64_1_local, int64_1_local
        )
    );

    define_block!   ((vm, simple_spill_v1) blk_entry() {
        blk_entry_branch
qinsoon's avatar
qinsoon committed
349 350 351
    });

    // %start(%t1, %t2, ..., %t14):
352 353 354 355 356 357 358 359 360 361 362 363 364 365
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t1);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t2);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t3);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t4);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t5);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t6);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t7);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t8);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t9);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t10);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t11);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t12);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t13);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t14);
qinsoon's avatar
qinsoon committed
366 367

    // %res = ADD %t1 %t2
368 369 370 371
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_res);
    inst!           ((vm, simple_spill_v1) blk_start_add:
        blk_start_res = BINOP (BinOp::Add) blk_start_t1 blk_start_t2
    );
qinsoon's avatar
qinsoon committed
372 373

    // BRANCH %ret (%res, %t1, %t2, ..., %t14)
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
    block!          ((vm, simple_spill_v1) blk_ret);
    inst!           ((vm, simple_spill_v1) blk_start_branch:
        BRANCH blk_ret (
            blk_start_res,
            blk_start_t1,
            blk_start_t2,
            blk_start_t3,
            blk_start_t4,
            blk_start_t5,
            blk_start_t6,
            blk_start_t7,
            blk_start_t8,
            blk_start_t9,
            blk_start_t10,
            blk_start_t11,
            blk_start_t12,
            blk_start_t13,
            blk_start_t14
        )
    );

    define_block!   ((vm, simple_spill_v1) blk_start(
        blk_start_t1,
        blk_start_t2,
        blk_start_t3,
        blk_start_t4,
        blk_start_t5,
        blk_start_t6,
        blk_start_t7,
        blk_start_t8,
        blk_start_t9,
        blk_start_t10,
        blk_start_t11,
        blk_start_t12,
        blk_start_t13,
        blk_start_t14) {
            blk_start_add,
            blk_start_branch
        }
     );
qinsoon's avatar
qinsoon committed
414 415

    // %ret(%res, %t1, %t2, ... %t14):
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_res);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t1);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t2);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t3);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t4);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t5);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t6);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t7);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t8);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t9);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t10);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t11);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t12);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t13);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t14);
qinsoon's avatar
qinsoon committed
431 432

    // RET %res
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
    inst!           ((vm, simple_spill_v1) blk_ret_ret:
        RET (blk_ret_res)
    );

    define_block!   ((vm, simple_spill_v1) blk_ret
        (blk_ret_res,
         blk_ret_t1,
         blk_ret_t2,
         blk_ret_t3,
         blk_ret_t4,
         blk_ret_t5,
         blk_ret_t6,
         blk_ret_t7,
         blk_ret_t8,
         blk_ret_t9,
         blk_ret_t10,
         blk_ret_t11,
         blk_ret_t12,
         blk_ret_t13,
         blk_ret_t14
        ) {
            blk_ret_ret
qinsoon's avatar
qinsoon committed
455
        }
456
     );
qinsoon's avatar
qinsoon committed
457

458 459 460 461 462
    define_func_ver!((vm) simple_spill_v1 (entry: blk_entry) {
        blk_entry,
        blk_start,
        blk_ret
    });
qinsoon's avatar
qinsoon committed
463

464 465 466
    vm
}

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 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 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690

#[cfg(target_arch = "aarch64")]
fn create_simple_spill() -> VM {
    let vm = VM::new();

    typedef!        ((vm) int64 = mu_int(64));
    constdef!       ((vm) <int64> int64_1 = Constant::Int(1));

    funcsig!        ((vm) simple_spill_sig = () -> (int64));
    funcdecl!       ((vm) <simple_spill_sig> simple_spill);
    funcdef!        ((vm) <simple_spill_sig> simple_spill VERSION simple_spill_v1);

    // %entry():
    block!          ((vm, simple_spill_v1) blk_entry);

    // BRANCH %start(1, 1, 1, 1, ..., 1) // 28 constant ONE
    consta!         ((vm, simple_spill_v1) int64_1_local = int64_1);
    block!          ((vm, simple_spill_v1) blk_start);
    inst!           ((vm, simple_spill_v1) blk_entry_branch:
        BRANCH blk_start (int64_1_local, int64_1_local, int64_1_local,
                          int64_1_local, int64_1_local, int64_1_local,
                          int64_1_local, int64_1_local, int64_1_local,
                          int64_1_local, int64_1_local, int64_1_local,
                          int64_1_local, int64_1_local, int64_1_local,
                          int64_1_local, int64_1_local, int64_1_local,
                          int64_1_local, int64_1_local, int64_1_local,
                          int64_1_local, int64_1_local, int64_1_local,
                          int64_1_local, int64_1_local, int64_1_local,
                          int64_1_local
        )
    );

    define_block!   ((vm, simple_spill_v1) blk_entry() {
        blk_entry_branch
    });

    // %start(%t1, %t2, ..., %t14):
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t1);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t2);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t3);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t4);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t5);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t6);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t7);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t8);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t9);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t10);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t11);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t12);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t13);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t14);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t15);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t16);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t17);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t18);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t19);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t20);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t21);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t22);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t23);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t24);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t25);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t26);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t27);
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_t28);


    // %res = ADD %t1 %t2
    ssa!            ((vm, simple_spill_v1) <int64> blk_start_res);
    inst!           ((vm, simple_spill_v1) blk_start_add:
        blk_start_res = BINOP (BinOp::Add) blk_start_t1 blk_start_t2
    );

    // BRANCH %ret (%res, %t1, %t2, ..., %t14)
    block!          ((vm, simple_spill_v1) blk_ret);
    inst!           ((vm, simple_spill_v1) blk_start_branch:
        BRANCH blk_ret (
            blk_start_res,
            blk_start_t1,
            blk_start_t2,
            blk_start_t3,
            blk_start_t4,
            blk_start_t5,
            blk_start_t6,
            blk_start_t7,
            blk_start_t8,
            blk_start_t9,
            blk_start_t10,
            blk_start_t11,
            blk_start_t12,
            blk_start_t13,
            blk_start_t14,
            blk_start_t15,
            blk_start_t16,
            blk_start_t17,
            blk_start_t18,
            blk_start_t19,
            blk_start_t20,
            blk_start_t21,
            blk_start_t22,
            blk_start_t23,
            blk_start_t24,
            blk_start_t25,
            blk_start_t26,
            blk_start_t27,
            blk_start_t28
        )
    );

    define_block!   ((vm, simple_spill_v1) blk_start(
        blk_start_t1,
        blk_start_t2,
        blk_start_t3,
        blk_start_t4,
        blk_start_t5,
        blk_start_t6,
        blk_start_t7,
        blk_start_t8,
        blk_start_t9,
        blk_start_t10,
        blk_start_t11,
        blk_start_t12,
        blk_start_t13,
        blk_start_t14,
        blk_start_t15,
        blk_start_t16,
        blk_start_t17,
        blk_start_t18,
        blk_start_t19,
        blk_start_t20,
        blk_start_t21,
        blk_start_t22,
        blk_start_t23,
        blk_start_t24,
        blk_start_t25,
        blk_start_t26,
        blk_start_t27,
        blk_start_t28) {
            blk_start_add,
            blk_start_branch
        }
     );

    // %ret(%res, %t1, %t2, ... %t14):
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_res);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t1);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t2);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t3);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t4);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t5);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t6);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t7);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t8);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t9);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t10);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t11);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t12);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t13);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t14);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t15);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t16);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t17);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t18);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t19);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t20);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t21);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t22);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t23);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t24);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t25);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t26);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t27);
    ssa!            ((vm, simple_spill_v1) <int64> blk_ret_t28);

    // RET %res
    inst!           ((vm, simple_spill_v1) blk_ret_ret:
        RET (blk_ret_res)
    );

    define_block!   ((vm, simple_spill_v1) blk_ret
        (blk_ret_res,
         blk_ret_t1,
         blk_ret_t2,
         blk_ret_t3,
         blk_ret_t4,
         blk_ret_t5,
         blk_ret_t6,
         blk_ret_t7,
         blk_ret_t8,
         blk_ret_t9,
         blk_ret_t10,
         blk_ret_t11,
         blk_ret_t12,
         blk_ret_t13,
         blk_ret_t14,
         blk_ret_t15,
         blk_ret_t16,
         blk_ret_t17,
         blk_ret_t18,
         blk_ret_t19,
         blk_ret_t20,
         blk_ret_t21,
         blk_ret_t22,
         blk_ret_t23,
         blk_ret_t24,
         blk_ret_t25,
         blk_ret_t26,
         blk_ret_t27,
         blk_ret_t28
        ) {
            blk_ret_ret
        }
     );

    define_func_ver!((vm) simple_spill_v1 (entry: blk_entry) {
        blk_entry,
        blk_start,
        blk_ret
    });

    vm
}

#[test] // was x86-64 only
691 692 693 694 695
fn test_coalesce_branch_moves() {
    VM::start_logging_trace();

    let vm = Arc::new(coalesce_branch_moves());

qinsoon's avatar
qinsoon committed
696
    let compiler = Compiler::new(CompilerPolicy::default(), &vm);
697 698 699 700 701 702

    let func_id = vm.id_of("coalesce_branch_moves");
    {
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
qinsoon's avatar
qinsoon committed
703 704 705 706 707
        let mut func_ver = func_vers
            .get(&func.cur_ver.unwrap())
            .unwrap()
            .write()
            .unwrap();
708 709 710 711 712 713

        compiler.compile(&mut func_ver);

        // check
        let fv_id = func_ver.id();

qinsoon's avatar
qinsoon committed
714 715 716
        assert!(get_number_of_moves(fv_id, &vm) == 2,
            "The function should not yield any mov instructions other than \
            mov %rsp->%rbp and mov %rbp->%rsp (some possible coalescing failed)");
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
    }
}

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

    typedef! ((vm) int64 = mu_int(64));

    funcsig! ((vm) sig = (int64, int64, int64, int64) -> ());
    funcdecl!((vm) <sig> coalesce_branch_moves);
    funcdef! ((vm) <sig> coalesce_branch_moves VERSION coalesce_branch_moves_v1);

    // blk entry
    block!   ((vm, coalesce_branch_moves_v1) blk_entry);
    ssa!     ((vm, coalesce_branch_moves_v1) <int64> arg0);
    ssa!     ((vm, coalesce_branch_moves_v1) <int64> arg1);
    ssa!     ((vm, coalesce_branch_moves_v1) <int64> arg2);
    ssa!     ((vm, coalesce_branch_moves_v1) <int64> arg3);

    block!   ((vm, coalesce_branch_moves_v1) blk1);
    inst!    ((vm, coalesce_branch_moves_v1) blk_entry_branch:
        BRANCH blk1 (arg0, arg1, arg2, arg3)
    );

qinsoon's avatar
qinsoon committed
741 742 743
    define_block!((vm, coalesce_branch_moves_v1) blk_entry (arg0, arg1, arg2, arg3) {
        blk_entry_branch
    });
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761

    ssa!     ((vm, coalesce_branch_moves_v1) <int64> blk1_arg0);
    ssa!     ((vm, coalesce_branch_moves_v1) <int64> blk1_arg1);
    ssa!     ((vm, coalesce_branch_moves_v1) <int64> blk1_arg2);
    ssa!     ((vm, coalesce_branch_moves_v1) <int64> blk1_arg3);

    inst!    ((vm, coalesce_branch_moves_v1) blk1_ret:
        RET
    );

    define_block!((vm, coalesce_branch_moves_v1) blk1 (blk1_arg0, blk1_arg1, blk1_arg2, blk1_arg3) {
        blk1_ret
    });

    define_func_ver!((vm) coalesce_branch_moves_v1 (entry: blk_entry){
        blk_entry, blk1
    });

qinsoon's avatar
[wip]  
qinsoon committed
762 763 764
    vm
}

765
#[test] // was x86-64 only
qinsoon's avatar
[wip]  
qinsoon committed
766 767 768 769 770
fn test_coalesce_args() {
    VM::start_logging_trace();

    let vm = Arc::new(coalesce_args());

qinsoon's avatar
qinsoon committed
771
    let compiler = Compiler::new(CompilerPolicy::default(), &vm);
qinsoon's avatar
[wip]  
qinsoon committed
772 773 774 775 776 777

    let func_id = vm.id_of("coalesce_args");
    {
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
qinsoon's avatar
qinsoon committed
778 779 780 781 782
        let mut func_ver = func_vers
            .get(&func.cur_ver.unwrap())
            .unwrap()
            .write()
            .unwrap();
qinsoon's avatar
[wip]  
qinsoon committed
783 784 785 786 787 788

        compiler.compile(&mut func_ver);

        // check
        let fv_id = func_ver.id();

qinsoon's avatar
qinsoon committed
789 790 791 792
        assert!(get_number_of_moves(fv_id, &vm) == 2,
            "The function should not yield any mov instructions other than \
            mov %rsp->%rbp and mov %rbp->%rsp (or MOV SP -> FP on aarch64) \
            (some possible coalescing failed)");
qinsoon's avatar
[wip]  
qinsoon committed
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
    }
}

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

    typedef!    ((vm) int64 = mu_int(64));

    funcsig!    ((vm) sig = (int64, int64, int64, int64) -> ());
    funcdecl!   ((vm) <sig> coalesce_args);
    funcdef!    ((vm) <sig> coalesce_args VERSION coalesce_args_v1);

    typedef!    ((vm) funcref_to_sig = mu_funcref(sig));
    constdef!   ((vm) <funcref_to_sig> funcref = Constant::FuncRef(coalesce_args));

    // blk entry
    block!      ((vm, coalesce_args_v1) blk_entry);
    ssa!        ((vm, coalesce_args_v1) <int64> arg0);
    ssa!        ((vm, coalesce_args_v1) <int64> arg1);
    ssa!        ((vm, coalesce_args_v1) <int64> arg2);
    ssa!        ((vm, coalesce_args_v1) <int64> arg3);

    consta!     ((vm, coalesce_args_v1) funcref_local = funcref);
    inst!       ((vm, coalesce_args_v1) blk_entry_call:
        EXPRCALL (CallConvention::Mu, is_abort: false) funcref_local (arg0, arg1, arg2, arg3)
    );

    inst!       ((vm, coalesce_args_v1) blk_entry_ret:
        RET
    );

qinsoon's avatar
qinsoon committed
824 825 826
    define_block!   ((vm, coalesce_args_v1) blk_entry(arg0, arg1, arg2, arg3) {
        blk_entry_call, blk_entry_ret
    });
qinsoon's avatar
[wip]  
qinsoon committed
827 828 829 830 831 832

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

    vm
}

833
#[test] // was x86_64 only
qinsoon's avatar
[wip]  
qinsoon committed
834 835 836 837 838
fn test_coalesce_branch2_moves() {
    VM::start_logging_trace();

    let vm = Arc::new(coalesce_branch2_moves());

qinsoon's avatar
qinsoon committed
839
    let compiler = Compiler::new(CompilerPolicy::default(), &vm);
qinsoon's avatar
[wip]  
qinsoon committed
840 841 842 843 844 845

    let func_id = vm.id_of("coalesce_branch2_moves");
    {
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
qinsoon's avatar
qinsoon committed
846 847 848 849 850
        let mut func_ver = func_vers
            .get(&func.cur_ver.unwrap())
            .unwrap()
            .write()
            .unwrap();
qinsoon's avatar
[wip]  
qinsoon committed
851 852 853 854 855 856

        compiler.compile(&mut func_ver);

        // check
        let fv_id = func_ver.id();

qinsoon's avatar
qinsoon committed
857 858
        assert!(get_number_of_moves(fv_id, &vm) <= 4,
            "too many moves (some possible coalescing failed)");
qinsoon's avatar
[wip]  
qinsoon committed
859 860 861 862
    }

    backend::emit_context(&vm);

qinsoon's avatar
qinsoon committed
863 864 865
    let dylib = aot::link_dylib(
        vec![Mu("coalesce_branch2_moves")],
        &linkutils::get_dylib_name("coalesce_branch2_moves"),
866
        &vm
qinsoon's avatar
qinsoon committed
867
    );
qinsoon's avatar
[wip]  
qinsoon committed
868 869 870

    let lib = libloading::Library::new(dylib.as_os_str()).unwrap();
    unsafe {
qinsoon's avatar
qinsoon committed
871 872 873
        let coalesce_branch2_moves: libloading::Symbol<
            unsafe extern "C" fn(u64, u64, u64, u64, u64, u64) -> u64,
        > = match lib.get(b"coalesce_branch2_moves") {
qinsoon's avatar
[wip]  
qinsoon committed
874
            Ok(symbol) => symbol,
qinsoon's avatar
qinsoon committed
875
            Err(e) => panic!("cannot find symbol coalesce_branch2_moves in dylib: {:?}", e),
qinsoon's avatar
[wip]  
qinsoon committed
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
        };

        let res = coalesce_branch2_moves(1, 1, 10, 10, 0, 0);
        println!("if 0 == 0 then return 1 + 1 else return 10 + 10");
        println!("coalesce_branch2_moves(1, 1, 10, 10, 0, 0) = {}", res);
        assert!(res == 2);

        let res = coalesce_branch2_moves(1, 1, 10, 10, 1, 0);
        println!("if 1 == 0 then return 1 + 1 else return 10 + 10");
        println!("coalesce_branch2_moves(1, 1, 10, 10, 1, 0) = {}", res);
        assert!(res == 20);
    }
}

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

    typedef! ((vm) int64 = mu_int(64));
    typedef! ((vm) int1  = mu_int(1));

896
    funcsig! ((vm) sig = (int64, int64, int64, int64, int64, int64) -> (int64));
qinsoon's avatar
[wip]  
qinsoon committed
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
    funcdecl!((vm) <sig> coalesce_branch2_moves);
    funcdef! ((vm) <sig> coalesce_branch2_moves VERSION coalesce_branch2_moves_v1);

    // blk entry
    block!   ((vm, coalesce_branch2_moves_v1) blk_entry);
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> arg0);
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> arg1);
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> arg2);
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> arg3);
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> arg4);
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> arg5);

    ssa!     ((vm, coalesce_branch2_moves_v1) <int1> cond);
    inst!    ((vm, coalesce_branch2_moves_v1) blk_entry_cmp:
        cond = CMPOP (CmpOp::EQ) arg4 arg5
    );

    block!   ((vm, coalesce_branch2_moves_v1) blk_add01);
    block!   ((vm, coalesce_branch2_moves_v1) blk_add23);
    block!   ((vm, coalesce_branch2_moves_v1) blk_ret);
    inst!    ((vm, coalesce_branch2_moves_v1) blk_entry_branch2:
        BRANCH2 (cond, arg0, arg1, arg2, arg3)
            IF (OP 0)
            THEN blk_add01 (vec![1, 2]) WITH 0.6f32,
            ELSE blk_add23 (vec![3, 4])
    );

    define_block!((vm, coalesce_branch2_moves_v1) blk_entry (arg0, arg1, arg2, arg3, arg4, arg5) {
        blk_entry_cmp, blk_entry_branch2
    });

    // blk_add01
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> blk_add01_arg0);
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> blk_add01_arg1);

    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> res01);
    inst!    ((vm, coalesce_branch2_moves_v1) blk_add01_add:
        res01 = BINOP (BinOp::Add) blk_add01_arg0 blk_add01_arg1
    );

    inst!    ((vm, coalesce_branch2_moves_v1) blk_add01_branch:
        BRANCH blk_ret (res01)
    );

    define_block!((vm, coalesce_branch2_moves_v1) blk_add01 (blk_add01_arg0, blk_add01_arg1) {
        blk_add01_add, blk_add01_branch
    });

    // blk_add23
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> blk_add23_arg2);
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> blk_add23_arg3);

    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> res23);
    inst!    ((vm, coalesce_branch2_moves_v1) blk_add23_add:
        res23 = BINOP (BinOp::Add) blk_add23_arg2 blk_add23_arg3
    );

    inst!    ((vm, coalesce_branch2_moves_v1) blk_add23_branch:
        BRANCH blk_ret (res23)
    );

    define_block!((vm, coalesce_branch2_moves_v1) blk_add23 (blk_add23_arg2, blk_add23_arg3) {
        blk_add23_add, blk_add23_branch
    });

    // blk_ret
    ssa!     ((vm, coalesce_branch2_moves_v1) <int64> res);
    inst!    ((vm, coalesce_branch2_moves_v1) blk_ret_ret:
        RET (res)
    );

    define_block!((vm, coalesce_branch2_moves_v1) blk_ret (res) {
        blk_ret_ret
    });

    define_func_ver!((vm) coalesce_branch2_moves_v1 (entry: blk_entry){
        blk_entry, blk_add01, blk_add23, blk_ret
    });

qinsoon's avatar
qinsoon committed
976
    vm
977
}
978 979

#[test]
980
fn test_preserve_caller_saved_simple() {
981
    VM::start_logging_trace();
982
    let vm = Arc::new(preserve_caller_saved_simple());
983 984 985 986

    let compiler = Compiler::new(CompilerPolicy::default(), &vm);

    let func_foo = vm.id_of("foo");
987
    let func_preserve_caller_saved_simple = vm.id_of("preserve_caller_saved_simple");
988 989 990 991 992 993
    {
        let funcs = vm.funcs().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();

        {
            let func = funcs.get(&func_foo).unwrap().read().unwrap();
qinsoon's avatar
qinsoon committed
994 995 996 997 998
            let mut func_ver = func_vers
                .get(&func.cur_ver.unwrap())
                .unwrap()
                .write()
                .unwrap();
999 1000 1001 1002

            compiler.compile(&mut func_ver);
        }
        {
qinsoon's avatar
qinsoon committed
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
            let func = funcs
                .get(&func_preserve_caller_saved_simple)
                .unwrap()
                .read()
                .unwrap();
            let mut func_ver = func_vers
                .get(&func.cur_ver.unwrap())
                .unwrap()
                .write()
                .unwrap();
1013 1014 1015 1016 1017

            compiler.compile(&mut func_ver);
        }
    }

qinsoon's avatar
qinsoon committed
1018
    vm.set_primordial_thread(func_preserve_caller_saved_simple, true, vec![]);
1019 1020
    backend::emit_context(&vm);

qinsoon's avatar
qinsoon committed
1021 1022 1023
    let executable = aot::link_primordial(
        vec![Mu("foo"), Mu("preserve_caller_saved_simple")],
        "test_preserve_caller_saved_simple",
1024
        &vm
qinsoon's avatar
qinsoon committed
1025
    );
1026
    let output = linkutils::exec_path_nocheck(executable);
1027 1028 1029 1030 1031 1032

    // add from 0 to 9
    assert!(output.status.code().is_some());
    assert_eq!(output.status.code().unwrap(), 45);
}

1033
fn preserve_caller_saved_simple() -> VM {
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
    let vm = VM::new_with_opts("init_mu --disable-inline");

    create_empty_func_foo(&vm);

    typedef!    ((vm) int64 = mu_int(64));

    constdef!   ((vm) <int64> int64_0 = Constant::Int(0));
    constdef!   ((vm) <int64> int64_1 = Constant::Int(1));
    constdef!   ((vm) <int64> int64_2 = Constant::Int(2));
    constdef!   ((vm) <int64> int64_3 = Constant::Int(3));
    constdef!   ((vm) <int64> int64_4 = Constant::Int(4));
    constdef!   ((vm) <int64> int64_5 = Constant::Int(5));
    constdef!   ((vm) <int64> int64_6 = Constant::Int(6));
    constdef!   ((vm) <int64> int64_7 = Constant::Int(7));
    constdef!   ((vm) <int64> int64_8 = Constant::Int(8));
    constdef!   ((vm) <int64> int64_9 = Constant::Int(9));

    funcsig!    ((vm) sig = () -> ());
1052 1053
    funcdecl!   ((vm) <sig> preserve_caller_saved_simple);
    funcdef!    ((vm) <sig> preserve_caller_saved_simple VERSION preserve_caller_saved_simple_v1);
1054 1055

    // blk entry
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
    block!      ((vm, preserve_caller_saved_simple_v1) blk_entry);
    block!      ((vm, preserve_caller_saved_simple_v1) blk_main);

    consta!   ((vm, preserve_caller_saved_simple_v1)  int64_0_local = int64_0);
    consta!   ((vm, preserve_caller_saved_simple_v1)  int64_1_local = int64_1);
    consta!   ((vm, preserve_caller_saved_simple_v1)  int64_2_local = int64_2);
    consta!   ((vm, preserve_caller_saved_simple_v1)  int64_3_local = int64_3);
    consta!   ((vm, preserve_caller_saved_simple_v1)  int64_4_local = int64_4);
    consta!   ((vm, preserve_caller_saved_simple_v1)  int64_5_local = int64_5);
    consta!   ((vm, preserve_caller_saved_simple_v1)  int64_6_local = int64_6);
    consta!   ((vm, preserve_caller_saved_simple_v1)  int64_7_local = int64_7);
    consta!   ((vm, preserve_caller_saved_simple_v1)  int64_8_local = int64_8);
    consta!   ((vm, preserve_caller_saved_simple_v1)  int64_9_local = int64_9);
    inst!       ((vm, preserve_caller_saved_simple_v1) blk_entry_branch:
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
        BRANCH blk_main (
             int64_0_local,
             int64_1_local,
             int64_2_local,
             int64_3_local,
             int64_4_local,
             int64_5_local,
             int64_6_local,
             int64_7_local,
             int64_8_local,
             int64_9_local
        )
    );

1084
    define_block!   ((vm, preserve_caller_saved_simple_v1) blk_entry() {
1085 1086 1087 1088
        blk_entry_branch
    });

    // blk main
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
    ssa!    ((vm, preserve_caller_saved_simple_v1) <int64> v0);
    ssa!    ((vm, preserve_caller_saved_simple_v1) <int64> v1);
    ssa!    ((vm, preserve_caller_saved_simple_v1) <int64> v2);
    ssa!    ((vm, preserve_caller_saved_simple_v1) <int64> v3);
    ssa!    ((vm, preserve_caller_saved_simple_v1) <int64> v4);
    ssa!    ((vm, preserve_caller_saved_simple_v1) <int64> v5);
    ssa!    ((vm, preserve_caller_saved_simple_v1) <int64> v6);
    ssa!    ((vm, preserve_caller_saved_simple_v1) <int64> v7);
    ssa!    ((vm, preserve_caller_saved_simple_v1) <int64> v8);
    ssa!    ((vm, preserve_caller_saved_simple_v1) <int64> v9);
1099 1100

    let foo_sig = vm.get_func_sig(vm.id_of("foo_sig"));
qinsoon's avatar
qinsoon committed
1101
    let foo_id = vm.id_of("foo");
1102 1103 1104
    typedef!    ((vm) type_funcref_foo = mu_funcref(foo_sig));
    constdef!   ((vm) <type_funcref_foo> const_funcref_foo = Constant::FuncRef(foo_id));

1105 1106
    consta!     ((vm, preserve_caller_saved_simple_v1) const_funcref_foo_local = const_funcref_foo);
    inst!       ((vm, preserve_caller_saved_simple_v1) blk_main_call:
1107 1108 1109
        EXPRCALL (CallConvention::Mu, is_abort: false) const_funcref_foo_local ()
    );

1110 1111
    ssa!        ((vm, preserve_caller_saved_simple_v1) <int64> res1);
    inst!       ((vm, preserve_caller_saved_simple_v1) blk_main_add1:
1112 1113 1114
        res1 = BINOP (BinOp::Add) v0 v1
    );

1115 1116
    ssa!        ((vm, preserve_caller_saved_simple_v1) <int64> res2);
    inst!       ((vm, preserve_caller_saved_simple_v1) blk_main_add2:
1117 1118 1119
        res2 = BINOP (BinOp::Add) res1 v2
    );

1120 1121
    ssa!        ((vm, preserve_caller_saved_simple_v1) <int64> res3);
    inst!       ((vm, preserve_caller_saved_simple_v1) blk_main_add3:
1122 1123 1124
        res3 = BINOP (BinOp::Add) res2 v3
    );

1125 1126
    ssa!        ((vm, preserve_caller_saved_simple_v1) <int64> res4);
    inst!       ((vm, preserve_caller_saved_simple_v1) blk_main_add4:
1127 1128 1129
        res4 = BINOP (BinOp::Add) res3 v4
    );

1130 1131
    ssa!        ((vm, preserve_caller_saved_simple_v1) <int64> res5);
    inst!       ((vm, preserve_caller_saved_simple_v1) blk_main_add5:
1132 1133 1134
        res5 = BINOP (BinOp::Add) res4 v5
    );

1135 1136
    ssa!        ((vm, preserve_caller_saved_simple_v1) <int64> res6);
    inst!       ((vm, preserve_caller_saved_simple_v1) blk_main_add6:
1137 1138 1139
        res6 = BINOP (BinOp::Add) res5 v6
    );

1140 1141
    ssa!        ((vm, preserve_caller_saved_simple_v1) <int64> res7);
    inst!       ((vm, preserve_caller_saved_simple_v1) blk_main_add7:
1142 1143 1144
        res7 = BINOP (BinOp::Add) res6 v7
    );

1145 1146
    ssa!        ((vm, preserve_caller_saved_simple_v1) <int64> res8);
    inst!       ((vm, preserve_caller_saved_simple_v1) blk_main_add8:
1147 1148 1149
        res8 = BINOP (BinOp::Add) res7 v8
    );

1150 1151
    ssa!        ((vm, preserve_caller_saved_simple_v1) <int64> res9);
    inst!       ((vm, preserve_caller_saved_simple_v1) blk_main_add9:
1152 1153 1154
        res9 = BINOP (BinOp::Add) res8 v9
    );

1155
    let blk_main_exit = gen_ccall_exit(res9.clone(), &mut preserve_caller_saved_simple_v1, &vm);
1156

1157
    inst!       ((vm, preserve_caller_saved_simple_v1) blk_main_ret:
1158 1159 1160
        RET
    );

qinsoon's avatar
qinsoon committed
1161 1162
    define_block!   ((vm, preserve_caller_saved_simple_v1)
        blk_main(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) {
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
        blk_main_call,

        blk_main_add1,
        blk_main_add2,
        blk_main_add3,
        blk_main_add4,
        blk_main_add5,
        blk_main_add6,
        blk_main_add7,
        blk_main_add8,
        blk_main_add9,

        blk_main_exit,
        blk_main_ret
    });

1179
    define_func_ver!((vm) preserve_caller_saved_simple_v1 (entry: blk_entry) {
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
        blk_entry,
        blk_main
    });

    vm
}

fn create_empty_func_foo(vm: &VM) {
    funcsig!    ((vm) foo_sig = () -> ());
    funcdecl!   ((vm) <foo_sig> foo);
    funcdef!    ((vm) <foo_sig> foo VERSION foo_v1);

    block!      ((vm, foo_v1) blk_entry);
    inst!       ((vm, foo_v1) blk_entry_ret:
        RET
    );

    define_block!   ((vm, foo_v1) blk_entry() {
        blk_entry_ret
    });

    define_func_ver!((vm) foo_v1 (entry: blk_entry) {blk_entry});
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
}

#[test]
fn test_preserve_caller_saved_call_args() {
    VM::start_logging_trace();
    let vm = Arc::new(preserve_caller_saved_call_args());

    let compiler = Compiler::new(CompilerPolicy::default(), &vm);

    let func_foo = vm.id_of("foo6");
    let func_preserve_caller_saved_simple = vm.id_of("preserve_caller_saved_call_args");
    {
        let funcs = vm.funcs().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();

        {
            let func = funcs.get(&func_foo).unwrap().read().unwrap();
qinsoon's avatar
qinsoon committed
1219 1220 1221 1222 1223
            let mut func_ver = func_vers
                .get(&func.cur_ver.unwrap())
                .unwrap()
                .write()
                .unwrap();
1224 1225 1226 1227

            compiler.compile(&mut func_ver);
        }
        {
qinsoon's avatar
qinsoon committed
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
            let func = funcs
                .get(&func_preserve_caller_saved_simple)
                .unwrap()
                .read()
                .unwrap();
            let mut func_ver = func_vers
                .get(&func.cur_ver.unwrap())
                .unwrap()
                .write()
                .unwrap();
1238 1239 1240 1241 1242

            compiler.compile(&mut func_ver);
        }
    }

qinsoon's avatar
qinsoon committed
1243
    vm.set_primordial_thread(func_preserve_caller_saved_simple, true, vec![]);
1244 1245
    backend::emit_context(&vm);

qinsoon's avatar
qinsoon committed
1246 1247 1248
    let executable = aot::link_primordial(
        vec![Mu("foo6"), Mu("preserve_caller_saved_call_args")],
        "test_preserve_caller_saved_call_args",
1249
        &vm
qinsoon's avatar
qinsoon committed
1250
    );
1251
    let output = linkutils::exec_path_nocheck(executable);
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277

    // add from 0 to 9
    assert!(output.status.code().is_some());
    assert_eq!(output.status.code().unwrap(), 45);
}

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

    typedef!    ((vm) int64 = mu_int(64));

    create_empty_func_foo6(&vm);

    constdef!   ((vm) <int64> int64_0 = Constant::Int(0));
    constdef!   ((vm) <int64> int64_1 = Constant::Int(1));
    constdef!   ((vm) <int64> int64_2 = Constant::Int(2));
    constdef!   ((vm) <int64> int64_3 = Constant::Int(3));
    constdef!   ((vm) <int64> int64_4 = Constant::Int(4));
    constdef!   ((vm) <int64> int64_5 = Constant::Int(5));
    constdef!   ((vm) <int64> int64_6 = Constant::Int(6));
    constdef!   ((vm) <int64> int64_7 = Constant::Int(7));
    constdef!   ((vm) <int64> int64_8 = Constant::Int(8));
    constdef!   ((vm) <int64> int64_9 = Constant::Int(9));

    funcsig!    ((vm) sig = () -> ());
    funcdecl!   ((vm) <sig> preserve_caller_saved_call_args);
qinsoon's avatar
qinsoon committed
1278 1279
    funcdef!    ((vm) <sig> preserve_caller_saved_call_args
        VERSION preserve_caller_saved_call_args_v1);
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326

    // blk entry
    block!      ((vm, preserve_caller_saved_call_args_v1) blk_entry);
    block!      ((vm, preserve_caller_saved_call_args_v1) blk_main);

    consta!   ((vm, preserve_caller_saved_call_args_v1)  int64_0_local = int64_0);
    consta!   ((vm, preserve_caller_saved_call_args_v1)  int64_1_local = int64_1);
    consta!   ((vm, preserve_caller_saved_call_args_v1)  int64_2_local = int64_2);
    consta!   ((vm, preserve_caller_saved_call_args_v1)  int64_3_local = int64_3);
    consta!   ((vm, preserve_caller_saved_call_args_v1)  int64_4_local = int64_4);
    consta!   ((vm, preserve_caller_saved_call_args_v1)  int64_5_local = int64_5);
    consta!   ((vm, preserve_caller_saved_call_args_v1)  int64_6_local = int64_6);
    consta!   ((vm, preserve_caller_saved_call_args_v1)  int64_7_local = int64_7);
    consta!   ((vm, preserve_caller_saved_call_args_v1)  int64_8_local = int64_8);
    consta!   ((vm, preserve_caller_saved_call_args_v1)  int64_9_local = int64_9);
    inst!       ((vm, preserve_caller_saved_call_args_v1) blk_entry_branch:
        BRANCH blk_main (
             int64_0_local,
             int64_1_local,
             int64_2_local,
             int64_3_local,
             int64_4_local,
             int64_5_local,
             int64_6_local,
             int64_7_local,
             int64_8_local,
             int64_9_local
        )
    );

    define_block!   ((vm, preserve_caller_saved_call_args_v1) blk_entry() {
        blk_entry_branch
    });

    // blk main
    ssa!    ((vm, preserve_caller_saved_call_args_v1) <int64> v0);
    ssa!    ((vm, preserve_caller_saved_call_args_v1) <int64> v1);
    ssa!    ((vm, preserve_caller_saved_call_args_v1) <int64> v2);
    ssa!    ((vm, preserve_caller_saved_call_args_v1) <int64> v3);
    ssa!    ((vm, preserve_caller_saved_call_args_v1) <int64> v4);
    ssa!    ((vm, preserve_caller_saved_call_args_v1) <int64> v5);
    ssa!    ((vm, preserve_caller_saved_call_args_v1) <int64> v6);
    ssa!    ((vm, preserve_caller_saved_call_args_v1) <int64> v7);
    ssa!    ((vm, preserve_caller_saved_call_args_v1) <int64> v8);
    ssa!    ((vm, preserve_caller_saved_call_args_v1) <int64> v9);

    let foo_sig = vm.get_func_sig(vm.id_of("foo6_sig"));
qinsoon's avatar
qinsoon committed
1327
    let foo_id = vm.id_of("foo6");
1328 1329 1330
    typedef!    ((vm) type_funcref_foo = mu_funcref(foo_sig));
    constdef!   ((vm) <type_funcref_foo> const_funcref_foo = Constant::FuncRef(foo_id));

qinsoon's avatar
qinsoon committed
1331 1332
    consta!     ((vm, preserve_caller_saved_call_args_v1) const_funcref_foo_local
        = const_funcref_foo);
1333
    inst!       ((vm, preserve_caller_saved_call_args_v1) blk_main_call:
qinsoon's avatar
qinsoon committed
1334 1335
        EXPRCALL (CallConvention::Mu, is_abort: false)
        const_funcref_foo_local (v0, v1, v2, v3, v4, v5)
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
    );

    ssa!        ((vm, preserve_caller_saved_call_args_v1) <int64> res1);
    inst!       ((vm, preserve_caller_saved_call_args_v1) blk_main_add1:
        res1 = BINOP (BinOp::Add) v0 v1
    );

    ssa!        ((vm, preserve_caller_saved_call_args_v1) <int64> res2);
    inst!       ((vm, preserve_caller_saved_call_args_v1) blk_main_add2:
        res2 = BINOP (BinOp::Add) res1 v2
    );

    ssa!        ((vm, preserve_caller_saved_call_args_v1) <int64> res3);
    inst!       ((vm, preserve_caller_saved_call_args_v1) blk_main_add3:
        res3 = BINOP (BinOp::Add) res2 v3
    );

    ssa!        ((vm, preserve_caller_saved_call_args_v1) <int64> res4);
    inst!       ((vm, preserve_caller_saved_call_args_v1) blk_main_add4:
        res4 = BINOP (BinOp::Add) res3 v4
    );

    ssa!        ((vm, preserve_caller_saved_call_args_v1) <int64> res5);
    inst!       ((vm, preserve_caller_saved_call_args_v1) blk_main_add5:
        res5 = BINOP (BinOp::Add) res4 v5
    );

    ssa!        ((vm, preserve_caller_saved_call_args_v1) <int64> res6);
    inst!       ((vm, preserve_caller_saved_call_args_v1) blk_main_add6:
        res6 = BINOP (BinOp::Add) res5 v6
    );

    ssa!        ((vm, preserve_caller_saved_call_args_v1) <int64> res7);
    inst!       ((vm, preserve_caller_saved_call_args_v1) blk_main_add7:
        res7 = BINOP (BinOp::Add) res6 v7
    );

    ssa!        ((vm, preserve_caller_saved_call_args_v1) <int64> res8);
    inst!       ((vm, preserve_caller_saved_call_args_v1) blk_main_add8:
        res8 = BINOP (BinOp::Add) res7 v8
    );

    ssa!        ((vm, preserve_caller_saved_call_args_v1) <int64> res9);
    inst!       ((vm, preserve_caller_saved_call_args_v1) blk_main_add9:
        res9 = BINOP (BinOp::Add) res8 v9
    );

    let blk_main_exit = gen_ccall_exit(res9.clone(), &mut preserve_caller_saved_call_args_v1, &vm);

    inst!       ((vm, preserve_caller_saved_call_args_v1) blk_main_ret:
        RET
    );

qinsoon's avatar
qinsoon committed
1389 1390
    define_block!   ((vm, preserve_caller_saved_call_args_v1)
        blk_main(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) {
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
        blk_main_call,

        blk_main_add1,
        blk_main_add2,
        blk_main_add3,
        blk_main_add4,
        blk_main_add5,
        blk_main_add6,
        blk_main_add7,
        blk_main_add8,
        blk_main_add9,

        blk_main_exit,
        blk_main_ret
    });

    define_func_ver!((vm) preserve_caller_saved_call_args_v1 (entry: blk_entry) {
        blk_entry,
        blk_main
    });

    vm
}

fn create_empty_func_foo6(vm: &VM) {
    let int64 = vm.get_type(vm.id_of("int64"));

    funcsig!    ((vm) foo6_sig = (int64, int64, int64, int64, int64, int64) -> ());
    funcdecl!   ((vm) <foo6_sig> foo6);
    funcdef!    ((vm) <foo6_sig> foo6 VERSION foo6_v1);

    block!      ((vm, foo6_v1) blk_entry);
    inst!       ((vm, foo6_v1) blk_entry_ret:
        RET
    );

    define_block!   ((vm, foo6_v1) blk_entry() {
        blk_entry_ret
    });

    define_func_ver!((vm) foo6_v1 (entry: blk_entry) {blk_entry});
1432 1433 1434 1435 1436
}


#[test]
#[allow(unused_variables)]
1437
#[allow(overflowing_literals)]
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
fn test_spill_int8() {
    VM::start_logging_trace();

    let vm = Arc::new(spill_int8());

    let compiler = Compiler::new(CompilerPolicy::default(), &vm);

    let func_id = vm.id_of("spill_int8");
    {
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
qinsoon's avatar
qinsoon committed
1450 1451 1452 1453 1454
        let mut func_ver = func_vers
            .get(&func.cur_ver.unwrap())
            .unwrap()
            .write()
            .unwrap();
1455 1456 1457 1458 1459 1460

        compiler.compile(&mut func_ver);
    }

    backend::emit_context(&vm);

qinsoon's avatar
qinsoon committed
1461 1462 1463
    let dylib = aot::link_dylib(
        vec![Mu("spill_int8")],
        &linkutils::get_dylib_name("spill_int8"),
1464
        &vm
qinsoon's avatar
qinsoon committed
1465
    );
1466 1467 1468

    let lib = libloading::Library::new(dylib.as_os_str()).unwrap();
    unsafe {
qinsoon's avatar
qinsoon committed
1469 1470 1471
        let spill_int8: libloading::Symbol<unsafe extern "C" fn() -> u8> =
            match lib.get(b"spill_int8") {
                Ok(symbol) => symbol,
1472
                Err(e) => panic!("cannot find symbol spill_int8 in dylib: {:?}", e)
qinsoon's avatar
qinsoon committed
1473
            };
1474 1475 1476

        let res = spill_int8();
        println!("spill_int8() = {}", res);
1477 1478 1479 1480 1481 1482
        if cfg!(target_arch = "x86_64") {
            assert_eq!(res, 136); // add from 0 to 16
        } else {
            //Note: 465 does not fit in a u8
            assert_eq!(res, 465u8); // add from 0 to 30
        }
1483 1484 1485
    }
}

1486
#[cfg(target_arch = "x86_64")]
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
fn spill_int8() -> VM {
    let vm = VM::new();

    typedef!    ((vm) int8 = mu_int(8));

    constdef!   ((vm) <int8> int8_0  = Constant::Int(0));
    constdef!   ((vm) <int8> int8_1  = Constant::Int(1));
    constdef!   ((vm) <int8> int8_2  = Constant::Int(2));
    constdef!   ((vm) <int8> int8_3  = Constant::Int(3));
    constdef!   ((vm) <int8> int8_4  = Constant::Int(4));
    constdef!   ((vm) <int8> int8_5  = Constant::Int(5));
    constdef!   ((vm) <int8> int8_6  = Constant::Int(6));
    constdef!   ((vm) <int8> int8_7  = Constant::Int(7));
    constdef!   ((vm) <int8> int8_8  = Constant::Int(8));
    constdef!   ((vm) <int8> int8_9  = Constant::Int(9));
    constdef!   ((vm) <int8> int8_10 = Constant::Int(10));
    constdef!   ((vm) <int8> int8_11 = Constant::Int(11));
    constdef!   ((vm) <int8> int8_12 = Constant::Int(12));
    constdef!   ((vm) <int8> int8_13 = Constant::Int(13));
    constdef!   ((vm) <int8> int8_14 = Constant::Int(14));
    constdef!   ((vm) <int8> int8_15 = Constant::Int(15));
    constdef!   ((vm) <int8> int8_16 = Constant::Int(16));
1509

1510 1511 1512
    funcsig!    ((vm) sig = () -> (int8));
    funcdecl!   ((vm) <sig> spill_int8);
    funcdef!    ((vm) <sig> spill_int8 VERSION spill_int8_v1);
1513

1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
    block!      ((vm, spill_int8_v1) blk_entry);

    consta!   ((vm, spill_int8_v1) int8_0_local = int8_0);
    consta!   ((vm, spill_int8_v1) int8_1_local = int8_1);
    consta!   ((vm, spill_int8_v1) int8_2_local = int8_2);
    consta!   ((vm, spill_int8_v1) int8_3_local = int8_3);
    consta!   ((vm, spill_int8_v1) int8_4_local = int8_4);
    consta!   ((vm, spill_int8_v1) int8_5_local = int8_5);
    consta!   ((vm, spill_int8_v1) int8_6_local = int8_6);
    consta!   ((vm, spill_int8_v1) int8_7_local = int8_7);
    consta!   ((vm, spill_int8_v1) int8_8_local = int8_8);
    consta!   ((vm, spill_int8_v1) int8_9_local = int8_9);
    consta!   ((vm, spill_int8_v1) int8_10_local= int8_10);
    consta!   ((vm, spill_int8_v1) int8_11_local= int8_11);
    consta!   ((vm, spill_int8_v1) int8_12_local= int8_12);
    consta!   ((vm, spill_int8_v1) int8_13_local= int8_13);
    consta!   ((vm, spill_int8_v1) int8_14_local= int8_14);
    consta!   ((vm, spill_int8_v1) int8_15_local= int8_15);
    consta!   ((vm, spill_int8_v1) int8_16_local= int8_16);

    block!      ((vm, spill_int8_v1) blk_ret);
    inst!       ((vm, spill_int8_v1) blk_entry_branch:
        BRANCH blk_ret (
            int8_0_local,
            int8_1_local,
            int8_2_local,
            int8_3_local,
            int8_4_local,
            int8_5_local,
            int8_6_local,
            int8_7_local,
            int8_8_local,
            int8_9_local,
            int8_10_local,
            int8_11_local,
            int8_12_local,
            int8_13_local,
            int8_14_local,
            int8_15_local,
            int8_16_local
        )
    );

    define_block!((vm, spill_int8_v1) blk_entry() {
        blk_entry_branch
    });

    ssa!    ((vm, spill_int8_v1) <int8> v0);
    ssa!    ((vm, spill_int8_v1) <int8> v1);
    ssa!    ((vm, spill_int8_v1) <int8> v2);
    ssa!    ((vm, spill_int8_v1) <int8> v3);
    ssa!    ((vm, spill_int8_v1) <int8> v4);
    ssa!    ((vm, spill_int8_v1) <int8> v5);
    ssa!    ((vm, spill_int8_v1) <int8> v6);
    ssa!    ((vm, spill_int8_v1) <int8> v7);
    ssa!    ((vm, spill_int8_v1) <int8> v8);
    ssa!    ((vm, spill_int8_v1) <int8> v9);
    ssa!    ((vm, spill_int8_v1) <int8> v10);
    ssa!    ((vm, spill_int8_v1) <int8> v11);
    ssa!    ((vm, spill_int8_v1) <int8> v12);
    ssa!    ((vm, spill_int8_v1) <int8> v13);
    ssa!    ((vm, spill_int8_v1) <int8> v14);
    ssa!    ((vm, spill_int8_v1) <int8> v15);
    ssa!    ((vm, spill_int8_v1) <int8> v16);

    ssa!    ((vm, spill_int8_v1) <int8> res1);
    inst!   ((vm, spill_int8_v1) blk_ret_add1:
        res1 = BINOP (BinOp::Add) v0 v1
    );

    ssa!    ((vm, spill_int8_v1) <int8> res2);
    inst!   ((vm, spill_int8_v1) blk_ret_add2:
        res2 = BINOP (BinOp::Add) res1 v2
    );

    ssa!    ((vm, spill_int8_v1) <int8> res3);
    inst!   ((vm, spill_int8_v1) blk_ret_add3:
        res3 = BINOP (BinOp::Add) res2 v3
    );

    ssa!    ((vm, spill_int8_v1) <int8> res4);
    inst!   ((vm, spill_int8_v1) blk_ret_add4:
        res4 = BINOP (BinOp::Add) res3 v4
    );

    ssa!    ((vm, spill_int8_v1) <int8> res5);
    inst!   ((vm, spill_int8_v1) blk_ret_add5:
        res5 = BINOP (BinOp::Add) res4 v5
    );

    ssa!    ((vm, spill_int8_v1) <int8> res6);
    inst!   ((vm, spill_int8_v1) blk_ret_add6:
        res6 = BINOP (BinOp::Add) res5 v6
    );

    ssa!    ((vm, spill_int8_v1) <int8> res7);
    inst!   ((vm, spill_int8_v1) blk_ret_add7:
        res7 = BINOP (BinOp::Add) res6 v7
    );

    ssa!    ((vm, spill_int8_v1) <int8> res8);
    inst!   ((vm, spill_int8_v1) blk_ret_add8:
        res8 = BINOP (BinOp::Add) res7 v8
    );

    ssa!    ((vm, spill_int8_v1) <int8> res9);
    inst!   ((vm, spill_int8_v1) blk_ret_add9:
        res9 = BINOP (BinOp::Add) res8 v9
    );

    ssa!    ((vm, spill_int8_v1) <int8> res10);
    inst!   ((vm, spill_int8_v1) blk_ret_add10:
        res10 = BINOP (BinOp::Add) res9 v10
    );

    ssa!    ((vm, spill_int8_v1) <int8> res11);
    inst!   ((vm, spill_int8_v1) blk_ret_add11:
        res11 = BINOP (BinOp::Add) res10 v11
    );

    ssa!    ((vm, spill_int8_v1) <int8> res12);
    inst!   ((vm, spill_int8_v1) blk_ret_add12:
        res12 = BINOP (BinOp::Add) res11 v12
    );

    ssa!    ((vm, spill_int8_v1) <int8> res13);
    inst!   ((vm, spill_int8_v1) blk_ret_add13:
        res13 = BINOP (BinOp::Add) res12 v13
    );

    ssa!    ((vm, spill_int8_v1) <int8> res14);
    inst!   ((vm, spill_int8_v1) blk_ret_add14:
        res14 = BINOP (BinOp::Add) res13 v14
    );

    ssa!    ((vm, spill_int8_v1) <int8> res15);
    inst!   ((vm, spill_int8_v1) blk_ret_add15:
        res15 = BINOP (BinOp::Add) res14 v15
    );

    ssa!    ((vm, spill_int8_v1) <int8> res16);
    inst!   ((vm, spill_int8_v1) blk_ret_add16:
        res16 = BINOP (BinOp::Add) res15 v16
    );

    inst!   ((vm, spill_int8_v1) blk_ret_ret:
        RET (res16)
    );

qinsoon's avatar
qinsoon committed
1663 1664 1665
    define_block!   ((vm, spill_int8_v1)
        blk_ret(v0, v1, v2, v3, v4, v5, v6, v7, v8,
                v9, v10, v11, v12, v13, v14, v15, v16) {
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
        blk_ret_add1,
        blk_ret_add2,
        blk_ret_add3,
        blk_ret_add4,
        blk_ret_add5,
        blk_ret_add6,
        blk_ret_add7,
        blk_ret_add8,
        blk_ret_add9,
        blk_ret_add10,
        blk_ret_add11,
        blk_ret_add12,
        blk_ret_add13,
        blk_ret_add14,
        blk_ret_add15,
        blk_ret_add16,
        blk_ret_ret
    });

    define_func_ver!((vm) spill_int8_v1 (entry: blk_entry) {
        blk_entry,
        blk_ret
    });

    vm
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871