test_int128.rs 1.38 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
extern crate libloading;

use mu::ast::ir::*;
use mu::ast::inst::*;
use mu::ast::types;
use mu::ast::types::*;
use mu::ast::op::*;
use mu::vm::*;

use std::sync::Arc;
use std::sync::RwLock;
use mu::utils::LinkedHashMap;
use mu::testutil;

#[test]
fn test_add_u128() {
    let lib = testutil::compile_fnc("add_u128", &add_u128);

    unsafe {
        let add_u128 : libloading::Symbol<unsafe extern fn(u64, u64, u64, u64) -> (u64, u64)> = lib.get(b"add_u128").unwrap();

        let res = add_u128(1, 0, 1, 0);
        println!("add_u128(1, 1) = {:?}", res);
        assert!(res == (2, 0));
    }
}

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

    typedef!    ((vm) u128 = mu_int(128));

    funcsig!    ((vm) sig = (u128, u128) -> (u128));
    funcdecl!   ((vm) <sig> add_u128);
    funcdef!    ((vm) <sig> add_u128 VERSION add_u128_v1);

    block!      ((vm, add_u128_v1) blk_entry);
    ssa!        ((vm, add_u128_v1) <u128> a);
    ssa!        ((vm, add_u128_v1) <u128> b);

    // sum = Add %a %b
    ssa!        ((vm, add_u128_v1) <u128> sum);
    inst!       ((vm, add_u128_v1) blk_entry_add_u128:
        sum = BINOP (BinOp::Add) a b
    );

    inst!       ((vm, add_u128_v1) blk_entry_ret:
        RET (sum)
    );

    define_block!   ((vm, add_u128_v1) blk_entry(a, b) {
        blk_entry_add_u128, blk_entry_ret
    });

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

    vm
}