test_builder_api.rs 26.5 KB
Newer Older
1
// Copyright 2017 The Australian National University
2
//
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
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
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.

qinsoon's avatar
qinsoon committed
15 16 17 18 19 20 21 22 23 24 25
#![allow(unused_imports)]
#![allow(dead_code)]
extern crate mu;

use self::mu::ast::types::*;
use self::mu::ast::ir::*;
use self::mu::ast::inst::*;
use self::mu::ast::ptr::*;
use self::mu::ast::op::*;
use self::mu::vm::*;
use self::mu::vm::api::*;
26
use self::mu::vm::api::api_c::*;
qinsoon's avatar
qinsoon committed
27 28

use std::mem;
Kunshan Wang's avatar
Kunshan Wang committed
29 30
use std::ptr;
use std::ffi::CString;
31
use std::os::raw::c_char;
qinsoon's avatar
qinsoon committed
32 33 34 35 36 37 38 39

#[test]
#[allow(unused_variables)]
fn test_builder_factorial() {
    builder_factorial()
}

fn builder_factorial() {
40 41 42 43
    //    let mvm = MuVM::new();
    //    let mvm_ref = unsafe {mvm.as_mut()}.unwrap();
    //    let ctx = (mvm_ref.new_context)(mvm);
    //    let ctx_ref = unsafe {ctx.as_mut()}.unwrap();
qinsoon's avatar
qinsoon committed
44
}
45 46 47 48 49

#[test]
#[allow(unused_variables)]
fn test_startup_shutdown() {
    unsafe {
qinsoon's avatar
qinsoon committed
50
        VM::start_logging_trace();
51

52 53 54 55 56 57 58 59
        info!("Starting micro VM...");

        let mvm = mu_fastimpl_new();

        let ctx = ((*mvm).new_context)(mvm);

        let b = ((*ctx).new_ir_builder)(ctx);

Kunshan Wang's avatar
Kunshan Wang committed
60 61 62 63
        let id1 = ((*b).gen_sym)(b, ptr::null_mut());
        let id2 = ((*b).gen_sym)(b, CString::new("@id2").unwrap().as_ptr());
        let id3 = ((*b).gen_sym)(b, ptr::null_mut());

64 65 66 67 68 69 70
        ((*b).abort)(b);
        ((*ctx).close_context)(ctx);

        info!("Finished.");
    }
}

71 72
#[derive(Default)]
struct CStringPool {
73
    strings: Vec<CString>
74 75 76 77 78 79 80 81 82 83
}

impl CStringPool {
    fn get(&mut self, s: &str) -> *const c_char {
        self.strings.push(CString::new(s).unwrap());
        self.strings.last().unwrap().as_ptr()
    }
}


84 85 86
#[test]
#[allow(unused_variables)]
fn test_types_sigs_loading() {
87 88
    let mut csp: CStringPool = Default::default();

89
    unsafe {
qinsoon's avatar
qinsoon committed
90
        VM::start_logging_trace();
91

92 93 94 95 96 97 98 99
        info!("Starting micro VM...");

        let mvm = mu_fastimpl_new();

        let ctx = ((*mvm).new_context)(mvm);

        let b = ((*ctx).new_ir_builder)(ctx);

100 101 102
        let id1 = ((*b).gen_sym)(b, csp.get("@i8"));
        let id2 = ((*b).gen_sym)(b, csp.get("@i32"));
        let id3 = ((*b).gen_sym)(b, csp.get("@pi32"));
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
103 104
        //let id4 = ((*b).gen_sym)(b, csp.get("@str1"));
        //let id5 = ((*b).gen_sym)(b, ptr::null_mut());
105 106
        let id6 = ((*b).gen_sym)(b, csp.get("@str2"));
        let id7 = ((*b).gen_sym)(b, csp.get("@pstr2"));
107

108
        ((*b).new_type_int)(b, id1, 8);
109 110
        ((*b).new_type_int)(b, id2, 32);
        ((*b).new_type_uptr)(b, id3, id2);
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
111 112
        //((*b).new_type_struct)(b, id4, ptr::null_mut(), 0);
        //((*b).new_type_struct)(b, id5, ptr::null_mut(), 0);
113 114 115 116

        let mut fields = vec![id3, id7];
        ((*b).new_type_struct)(b, id6, fields.as_mut_ptr(), fields.len());
        ((*b).new_type_uptr)(b, id7, id6);
117

Kunshan Wang's avatar
Kunshan Wang committed
118 119 120 121 122
        let id8 = ((*b).gen_sym)(b, csp.get("@sig1"));
        let id9 = ((*b).gen_sym)(b, csp.get("@funcptr1"));

        let mut ptys = vec![id1, id2];
        let mut rtys = vec![id3, id7];
123 124 125 126 127 128
        ((*b).new_funcsig)(
            b,
            id8,
            ptys.as_mut_ptr(),
            ptys.len(),
            rtys.as_mut_ptr(),
129
            rtys.len()
130
        );
Kunshan Wang's avatar
Kunshan Wang committed
131
        ((*b).new_type_ufuncptr)(b, id9, id8);
132

133 134 135 136 137 138
        let id10 = ((*b).gen_sym)(b, csp.get("@hyb1"));
        let id11 = ((*b).gen_sym)(b, csp.get("@rhyb1"));

        let mut fixeds = vec![id2, id2];
        ((*b).new_type_hybrid)(b, id10, fixeds.as_mut_ptr(), fixeds.len(), id1);
        ((*b).new_type_ref)(b, id11, id10);
139 140 141 142 143 144 145 146

        ((*b).load)(b);
        ((*ctx).close_context)(ctx);

        info!("Finished.");
    }
}

147 148 149 150 151 152 153

#[test]
#[allow(unused_variables)]
fn test_consts_loading() {
    let mut csp: CStringPool = Default::default();

    unsafe {
qinsoon's avatar
qinsoon committed
154
        VM::start_logging_trace();
155

156 157 158 159 160 161 162 163 164 165 166 167 168 169
        info!("Starting micro VM...");

        let mvm = mu_fastimpl_new();

        let ctx = ((*mvm).new_context)(mvm);

        let b = ((*ctx).new_ir_builder)(ctx);

        let id1 = ((*b).gen_sym)(b, csp.get("@i32"));
        let id2 = ((*b).gen_sym)(b, csp.get("@CONST_I32_42"));

        ((*b).new_type_int)(b, id1, 32);
        ((*b).new_const_int)(b, id2, id1, 42);

170 171 172 173 174
        let id_refi32 = ((*b).gen_sym)(b, csp.get("@refi32"));
        let id_nullrefi32 = ((*b).gen_sym)(b, csp.get("@CONST_REFI32_NULL"));
        ((*b).new_type_ref)(b, id_refi32, id1);
        ((*b).new_const_null)(b, id_nullrefi32, id_refi32);

175 176 177 178 179 180 181
        ((*b).load)(b);
        ((*ctx).close_context)(ctx);

        info!("Finished.");
    }
}

182 183 184
#[test]
#[allow(unused_variables)]
fn test_globals_loading() {
185
    let mut csp: CStringPool = Default::default();
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

    unsafe {
        VM::start_logging_trace();

        info!("Starting micro VM...");

        let mvm = mu_fastimpl_new();

        let ctx = ((*mvm).new_context)(mvm);

        let b = ((*ctx).new_ir_builder)(ctx);

        let id_int32 = ((*b).gen_sym)(b, csp.get("@i32"));
        ((*b).new_type_int)(b, id_int32, 32);

        let id_global = ((*b).gen_sym)(b, csp.get("@my_global"));
        ((*b).new_global_cell)(b, id_global, id_int32);

        ((*b).load)(b);
        ((*ctx).close_context)(ctx);

        info!("Finished. ");
    }
}

211 212 213 214 215 216 217

#[test]
#[allow(unused_variables)]
fn test_function_loading() {
    let mut csp: CStringPool = Default::default();

    unsafe {
qinsoon's avatar
qinsoon committed
218
        VM::start_logging_trace();
219

220 221 222 223 224 225 226 227
        info!("Starting micro VM...");

        let mvm = mu_fastimpl_new();

        let ctx = ((*mvm).new_context)(mvm);

        let b = ((*ctx).new_ir_builder)(ctx);

228 229 230 231
        let id_i32 = ((*b).gen_sym)(b, csp.get("@i32"));
        let id_i64 = ((*b).gen_sym)(b, csp.get("@i64"));
        let id_sig = ((*b).gen_sym)(b, csp.get("@sig"));
        let id_func = ((*b).gen_sym)(b, csp.get("@func"));
232

233 234
        ((*b).new_type_int)(b, id_i32, 32);
        ((*b).new_type_int)(b, id_i64, 64);
235

236
        let mut ptys = vec![id_i32];
237
        let mut rtys = vec![id_i32];
238 239 240 241 242 243
        ((*b).new_funcsig)(
            b,
            id_sig,
            ptys.as_mut_ptr(),
            ptys.len(),
            rtys.as_mut_ptr(),
244
            rtys.len()
245
        );
246

247
        ((*b).new_func)(b, id_func, id_sig);
248

249 250 251
        let id_const1 = ((*b).gen_sym)(b, csp.get("@const_i32_1"));
        ((*b).new_const_int)(b, id_const1, id_i32, 1);

252
        let id_const99 = ((*b).gen_sym)(b, csp.get("@const_i32_99"));
253 254 255
        ((*b).new_const_int)(b, id_const99, id_i32, 99);

        let id_funcver = ((*b).gen_sym)(b, csp.get("@func.v1"));
256

257
        let id_entry = ((*b).gen_sym)(b, csp.get("@func.v1.entry"));
258 259 260 261 262
        let id_bb1 = ((*b).gen_sym)(b, csp.get("@func.v1.bb1"));
        let id_bb2 = ((*b).gen_sym)(b, csp.get("@func.v1.bb2"));
        let id_bb3 = ((*b).gen_sym)(b, csp.get("@func.v1.bb3"));
        let id_bb4 = ((*b).gen_sym)(b, csp.get("@func.v1.bb4"));
        let id_bb5 = ((*b).gen_sym)(b, csp.get("@func.v1.bb5"));
263 264
        let id_bbxxx = ((*b).gen_sym)(b, csp.get("@func.v1.bbxxx"));

265
        let mut bbs = vec![id_entry, id_bb1, id_bb2, id_bb3, id_bb4, id_bb5, id_bbxxx];
266
        ((*b).new_func_ver)(b, id_funcver, id_func, bbs.as_mut_ptr(), bbs.len());
267 268

        {
269 270 271
            let id_x = ((*b).gen_sym)(b, csp.get("@func.v1.entry.x"));
            let mut args = vec![id_x];
            let mut argtys = vec![id_i32];
272 273 274

            let id_add = ((*b).gen_sym)(b, csp.get("@func.v1.entry.add"));
            let id_sub = ((*b).gen_sym)(b, csp.get("@func.v1.entry.sub"));
275 276
            let id_branch = ((*b).gen_sym)(b, csp.get("@func.v1.entry.branch"));
            let mut insts = vec![id_add, id_sub, id_branch];
277

278 279 280 281 282 283 284 285
            ((*b).new_bb)(
                b,
                id_entry,
                args.as_mut_ptr(),
                argtys.as_mut_ptr(),
                args.len(),
                0,
                insts.as_mut_ptr(),
286
                insts.len()
287
            );
288 289

            let id_y = ((*b).gen_sym)(b, csp.get("@func.v1.entry.y"));
290
            ((*b).new_binop)(b, id_add, id_y, CMU_BINOP_ADD, id_i32, id_x, id_x, 0);
291 292

            let id_z = ((*b).gen_sym)(b, csp.get("@func.v1.entry.z"));
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
            ((*b).new_binop)(b, id_sub, id_z, CMU_BINOP_SUB, id_i32, id_y, id_const99, 0);

            let id_dest = ((*b).gen_sym)(b, csp.get("@func.v1.entry.dest"));
            let mut dest_args = vec![id_z];
            ((*b).new_dest_clause)(b, id_dest, id_bb1, dest_args.as_mut_ptr(), dest_args.len());
            ((*b).new_branch)(b, id_branch, id_dest);
        }

        {
            let id_a = ((*b).gen_sym)(b, csp.get("@func.v1.bb1.a"));
            let id_ret = ((*b).gen_sym)(b, csp.get("@func.v1.bb1.ret"));

            let mut args = vec![id_a];
            let mut argtys = vec![id_i32];
            let mut insts = vec![id_ret];
308 309 310 311 312 313 314 315
            ((*b).new_bb)(
                b,
                id_bb1,
                args.as_mut_ptr(),
                argtys.as_mut_ptr(),
                args.len(),
                0,
                insts.as_mut_ptr(),
316
                insts.len()
317
            );
318 319 320

            let mut rvs = vec![id_a];
            ((*b).new_ret)(b, id_ret, rvs.as_mut_ptr(), rvs.len())
321 322
        }

323 324 325 326 327 328 329 330
        {
            let id_x = ((*b).gen_sym)(b, csp.get("@func.v1.bb2.x"));
            let id_add = ((*b).gen_sym)(b, csp.get("@func.v1.bb2.add"));
            let id_eq = ((*b).gen_sym)(b, csp.get("@func.v1.bb2.eq"));
            let id_br2 = ((*b).gen_sym)(b, csp.get("@func.v1.bb2.br2"));
            let mut args = vec![id_x];
            let mut argtys = vec![id_i32];
            let mut insts = vec![id_add, id_eq, id_br2];
331 332 333 334 335 336 337 338
            ((*b).new_bb)(
                b,
                id_bb2,
                args.as_mut_ptr(),
                argtys.as_mut_ptr(),
                args.len(),
                0,
                insts.as_mut_ptr(),
339
                insts.len()
340
            );
341 342 343 344 345 346 347 348 349 350 351 352

            let id_y = ((*b).gen_sym)(b, csp.get("@func.v1.bb2.y"));
            ((*b).new_binop)(b, id_add, id_y, CMU_BINOP_ADD, id_i32, id_x, id_const1, 0);

            let id_e = ((*b).gen_sym)(b, csp.get("@func.v1.bb2.x"));
            ((*b).new_cmp)(b, id_eq, id_e, CMU_CMP_EQ, id_i32, id_x, id_const99);

            let id_dest_t = ((*b).gen_sym)(b, csp.get("@func.v1.bb2.dest_t"));
            let id_dest_f = ((*b).gen_sym)(b, csp.get("@func.v1.bb2.dest_f"));

            {
                let mut dest_args = vec![id_x];
353 354 355 356 357
                ((*b).new_dest_clause)(
                    b,
                    id_dest_t,
                    id_bb3,
                    dest_args.as_mut_ptr(),
358
                    dest_args.len()
359
                );
360 361 362
            }
            {
                let mut dest_args = vec![id_y, id_y, id_x];
363 364 365 366 367
                ((*b).new_dest_clause)(
                    b,
                    id_dest_f,
                    id_bb4,
                    dest_args.as_mut_ptr(),
368
                    dest_args.len()
369
                );
370 371 372 373 374 375 376 377 378 379 380
            }

            ((*b).new_branch2)(b, id_br2, id_e, id_dest_t, id_dest_f);
        }

        {
            let id_x = ((*b).gen_sym)(b, csp.get("@func.v1.bb3.x"));
            let id_ret = ((*b).gen_sym)(b, csp.get("@func.v1.bb3.ret"));
            let mut args = vec![id_x];
            let mut argtys = vec![id_i32];
            let mut insts = vec![id_ret];
381 382 383 384 385 386 387 388
            ((*b).new_bb)(
                b,
                id_bb3,
                args.as_mut_ptr(),
                argtys.as_mut_ptr(),
                args.len(),
                0,
                insts.as_mut_ptr(),
389
                insts.len()
390
            );
391 392 393 394 395 396 397 398 399 400 401 402 403

            let mut rvs = vec![id_const99];
            ((*b).new_ret)(b, id_ret, rvs.as_mut_ptr(), rvs.len())
        }

        {
            let id_x = ((*b).gen_sym)(b, csp.get("@func.v1.bb4.x"));
            let id_y = ((*b).gen_sym)(b, csp.get("@func.v1.bb4.y"));
            let id_z = ((*b).gen_sym)(b, csp.get("@func.v1.bb4.z"));
            let id_ret = ((*b).gen_sym)(b, csp.get("@func.v1.bb4.ret"));
            let mut args = vec![id_x, id_y, id_z];
            let mut argtys = vec![id_i32, id_i32, id_i32];
            let mut insts = vec![id_ret];
404 405 406 407 408 409 410 411
            ((*b).new_bb)(
                b,
                id_bb4,
                args.as_mut_ptr(),
                argtys.as_mut_ptr(),
                args.len(),
                0,
                insts.as_mut_ptr(),
412
                insts.len()
413
            );
414 415 416 417 418

            let mut rvs = vec![id_const99];
            ((*b).new_ret)(b, id_ret, rvs.as_mut_ptr(), rvs.len())
        }

419
        {
420
            let id_exc = ((*b).gen_sym)(b, csp.get("@func.v1.bbxxx.exc"));
421
            let id_ret = ((*b).gen_sym)(b, csp.get("@func.v1.bbxxx.ret"));
422 423
            let mut args = vec![];
            let mut argtys = vec![];
424
            let mut insts = vec![id_ret];
425 426 427 428 429 430 431 432
            ((*b).new_bb)(
                b,
                id_bbxxx,
                args.as_mut_ptr(),
                argtys.as_mut_ptr(),
                args.len(),
                id_exc,
                insts.as_mut_ptr(),
433
                insts.len()
434
            );
435 436 437 438 439 440 441 442 443 444 445 446

            let mut rvs = vec![id_const99];
            ((*b).new_ret)(b, id_ret, rvs.as_mut_ptr(), rvs.len())
        }

        {
            let id_x = ((*b).gen_sym)(b, csp.get("@func.v1.bb5.x"));
            let id_add = ((*b).gen_sym)(b, csp.get("@func.v1.bb5.add"));
            let id_switch = ((*b).gen_sym)(b, csp.get("@func.v1.bb5.switch"));
            let mut args = vec![id_x];
            let mut argtys = vec![id_i32];
            let mut insts = vec![id_add, id_switch];
447 448 449 450 451 452 453 454
            ((*b).new_bb)(
                b,
                id_bb5,
                args.as_mut_ptr(),
                argtys.as_mut_ptr(),
                args.len(),
                0,
                insts.as_mut_ptr(),
455
                insts.len()
456
            );
457 458 459 460 461

            let id_y = ((*b).gen_sym)(b, csp.get("@func.v1.bb5.y"));
            ((*b).new_binop)(b, id_add, id_y, CMU_BINOP_ADD, id_i32, id_x, id_const1, 0);

            let id_dest_def = ((*b).gen_sym)(b, csp.get("@func.v1.bb5.dest_def"));
462
            let id_dest_99 = ((*b).gen_sym)(b, csp.get("@func.v1.bb5.dest_99"));
463 464 465

            {
                let mut dest_args = vec![id_x];
466 467 468 469 470
                ((*b).new_dest_clause)(
                    b,
                    id_dest_def,
                    id_bb3,
                    dest_args.as_mut_ptr(),
471
                    dest_args.len()
472
                );
473 474 475
            }
            {
                let mut dest_args = vec![id_y, id_y, id_x];
476 477 478 479 480
                ((*b).new_dest_clause)(
                    b,
                    id_dest_99,
                    id_bb4,
                    dest_args.as_mut_ptr(),
481
                    dest_args.len()
482
                );
483 484 485 486 487
            }

            let mut cases = vec![id_const99];
            let mut dests = vec![id_dest_99];

488 489 490 491 492 493 494 495
            ((*b).new_switch)(
                b,
                id_switch,
                id_i32,
                id_y,
                id_dest_def,
                cases.as_mut_ptr(),
                dests.as_mut_ptr(),
496
                cases.len()
497
            )
498 499
        }

500 501 502 503 504 505 506
        ((*b).load)(b);
        ((*ctx).close_context)(ctx);

        info!("Finished.");
    }
}

507 508 509 510 511 512
#[test]
#[allow(unused_variables)]
fn test_insts_call() {
    let mut csp: CStringPool = Default::default();

    unsafe {
qinsoon's avatar
qinsoon committed
513
        VM::start_logging_trace();
514

515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
        info!("Starting micro VM...");

        let mvm = mu_fastimpl_new();

        let ctx = ((*mvm).new_context)(mvm);

        let b = ((*ctx).new_ir_builder)(ctx);

        let id_i32 = ((*b).gen_sym)(b, csp.get("@i32"));
        let id_i64 = ((*b).gen_sym)(b, csp.get("@i64"));
        let id_sig = ((*b).gen_sym)(b, csp.get("@sig"));
        let id_func = ((*b).gen_sym)(b, csp.get("@func"));

        ((*b).new_type_int)(b, id_i32, 32);
        ((*b).new_type_int)(b, id_i64, 64);

        let mut ptys = vec![id_i32];
        let mut rtys = vec![id_i32];
533 534 535 536 537 538
        ((*b).new_funcsig)(
            b,
            id_sig,
            ptys.as_mut_ptr(),
            ptys.len(),
            rtys.as_mut_ptr(),
539
            rtys.len()
540
        );
541 542 543 544 545 546 547 548 549 550 551 552

        ((*b).new_func)(b, id_func, id_sig);

        let id_const1 = ((*b).gen_sym)(b, csp.get("@const_i32_1"));
        ((*b).new_const_int)(b, id_const1, id_i32, 1);

        let id_const99 = ((*b).gen_sym)(b, csp.get("@const_i32_99"));
        ((*b).new_const_int)(b, id_const99, id_i32, 99);

        let id_funcver = ((*b).gen_sym)(b, csp.get("@func.v1"));

        let id_entry = ((*b).gen_sym)(b, csp.get("@func.v1.entry"));
553 554
        let id_bb1 = ((*b).gen_sym)(b, csp.get("@func.v1.bb1"));
        let id_bb2 = ((*b).gen_sym)(b, csp.get("@func.v1.bb2"));
555

556
        let mut bbs = vec![id_entry, id_bb1, id_bb2];
557 558 559 560 561 562 563 564 565
        ((*b).new_func_ver)(b, id_funcver, id_func, bbs.as_mut_ptr(), bbs.len());

        {
            let id_x = ((*b).gen_sym)(b, csp.get("@func.v1.entry.x"));
            let mut args = vec![id_x];
            let mut argtys = vec![id_i32];

            let id_call1 = ((*b).gen_sym)(b, csp.get("@func.v1.entry.call1"));
            let id_call2 = ((*b).gen_sym)(b, csp.get("@func.v1.entry.call2"));
566
            let mut insts = vec![id_call1, id_call2];
567

568 569 570 571 572 573 574 575
            ((*b).new_bb)(
                b,
                id_entry,
                args.as_mut_ptr(),
                argtys.as_mut_ptr(),
                args.len(),
                0,
                insts.as_mut_ptr(),
576
                insts.len()
577
            );
578 579

            let id_y = ((*b).gen_sym)(b, csp.get("@func.v1.entry.y"));
580 581 582

            {
                let mut args = vec![id_x];
583 584 585 586 587 588 589 590 591 592 593
                let mut rvs = vec![id_y];
                ((*b).new_call)(
                    b,
                    id_call1,
                    rvs.as_mut_ptr(),
                    rvs.len(),
                    id_sig,
                    id_func,
                    args.as_mut_ptr(),
                    args.len(),
                    0,
594
                    0
595
                );
596
            }
597

598 599 600 601
            let id_z = ((*b).gen_sym)(b, csp.get("@func.v1.entry.z"));

            {
                let mut args = vec![id_y];
602
                let mut rvs = vec![id_z];
603 604 605 606 607 608 609

                let id_dest1 = ((*b).gen_sym)(b, csp.get("@func.v1.entry.call2.dest1"));
                let id_dest2 = ((*b).gen_sym)(b, csp.get("@func.v1.entry.call2.dest2"));
                let id_exc = ((*b).gen_sym)(b, csp.get("@func.v1.entry.call2.exc"));
                ((*b).new_exc_clause)(b, id_exc, id_dest1, id_dest2);
                {
                    let mut dest_args = vec![id_y, id_z, id_x];
610 611 612 613 614
                    ((*b).new_dest_clause)(
                        b,
                        id_dest1,
                        id_bb1,
                        dest_args.as_mut_ptr(),
615
                        dest_args.len()
616
                    );
617 618 619
                }
                {
                    let mut dest_args = vec![];
620 621 622 623 624
                    ((*b).new_dest_clause)(
                        b,
                        id_dest2,
                        id_bb2,
                        dest_args.as_mut_ptr(),
625
                        dest_args.len()
626
                    );
627 628
                }

629 630 631 632 633 634 635 636 637 638
                ((*b).new_call)(
                    b,
                    id_call2,
                    rvs.as_mut_ptr(),
                    rvs.len(),
                    id_sig,
                    id_func,
                    args.as_mut_ptr(),
                    args.len(),
                    id_exc,
639
                    0
640
                );
641 642 643 644 645 646 647 648 649 650 651 652 653
            }
        }

        {
            let id_y = ((*b).gen_sym)(b, csp.get("@func.v1.bb1.y"));
            let id_z = ((*b).gen_sym)(b, csp.get("@func.v1.bb1.z"));
            let id_x = ((*b).gen_sym)(b, csp.get("@func.v1.bb1.x"));
            let mut args = vec![id_y, id_z, id_x];
            let mut argtys = vec![id_i32, id_i32, id_i32];

            let id_ret = ((*b).gen_sym)(b, csp.get("@func.v1.bb1.ret"));
            let mut insts = vec![id_ret];

654 655 656 657 658 659 660 661
            ((*b).new_bb)(
                b,
                id_bb1,
                args.as_mut_ptr(),
                argtys.as_mut_ptr(),
                args.len(),
                0,
                insts.as_mut_ptr(),
662
                insts.len()
663
            );
664 665 666 667 668 669 670 671 672 673 674 675

            let mut retvars = vec![id_z];
            ((*b).new_ret)(b, id_ret, retvars.as_mut_ptr(), retvars.len());
        }

        {
            let mut args = vec![];
            let mut argtys = vec![];

            let id_ret = ((*b).gen_sym)(b, csp.get("@func.v1.bb2.ret"));
            let mut insts = vec![id_ret];

676 677 678 679 680 681 682 683
            ((*b).new_bb)(
                b,
                id_bb2,
                args.as_mut_ptr(),
                argtys.as_mut_ptr(),
                args.len(),
                0,
                insts.as_mut_ptr(),
684
                insts.len()
685
            );
686 687 688

            let mut retvars = vec![id_const1];
            ((*b).new_ret)(b, id_ret, retvars.as_mut_ptr(), retvars.len());
689
        }
690

691

692 693 694 695 696 697 698 699 700 701 702 703 704
        ((*b).load)(b);
        ((*ctx).close_context)(ctx);

        info!("Finished.");
    }
}

#[test]
#[allow(unused_variables)]
fn test_insts_new() {
    let mut csp: CStringPool = Default::default();

    unsafe {
qinsoon's avatar
qinsoon committed
705
        VM::start_logging_trace();
706

707 708 709 710 711 712 713 714
        info!("Starting micro VM...");

        let mvm = mu_fastimpl_new();

        let ctx = ((*mvm).new_context)(mvm);

        let b = ((*ctx).new_ir_builder)(ctx);

715 716
        let id_float = ((*b).gen_sym)(b, csp.get("@float"));
        let id_i8 = ((*b).gen_sym)(b, csp.get("@i8"));
717
        let id_i32 = ((*b).gen_sym)(b, csp.get("@i32"));
718
        let id_i64 = ((*b).gen_sym)(b, csp.get("@i64"));
719 720 721
        let id_s = ((*b).gen_sym)(b, csp.get("@s"));
        let id_a = ((*b).gen_sym)(b, csp.get("@a"));
        let id_h = ((*b).gen_sym)(b, csp.get("@h"));
722

723 724 725
        let id_sig = ((*b).gen_sym)(b, csp.get("@sig"));
        let id_func = ((*b).gen_sym)(b, csp.get("@func"));

726 727
        ((*b).new_type_float)(b, id_float);
        ((*b).new_type_int)(b, id_i8, 8);
728
        ((*b).new_type_int)(b, id_i32, 32);
729 730 731 732 733 734 735 736
        ((*b).new_type_int)(b, id_i64, 64);

        {
            let mut fields = vec![id_i32, id_a, id_i64];
            ((*b).new_type_struct)(b, id_s, fields.as_mut_ptr(), fields.len());
            ((*b).new_type_hybrid)(b, id_h, fields.as_mut_ptr(), fields.len(), id_i8);
        }
        ((*b).new_type_array)(b, id_a, id_float, 4);
737 738 739

        let mut ptys = vec![];
        let mut rtys = vec![];
740 741 742 743 744 745
        ((*b).new_funcsig)(
            b,
            id_sig,
            ptys.as_mut_ptr(),
            ptys.len(),
            rtys.as_mut_ptr(),
746
            rtys.len()
747
        );
748

749 750 751 752 753
        let id_consti64_3 = ((*b).gen_sym)(b, csp.get("@CONSTI64_3"));
        ((*b).new_const_int)(b, id_consti64_3, id_i64, 3);
        let id_consti64_4 = ((*b).gen_sym)(b, csp.get("@CONSTI64_4"));
        ((*b).new_const_int)(b, id_consti64_4, id_i64, 4);

754 755 756 757 758 759
        ((*b).new_func)(b, id_func, id_sig);

        let id_funcver = ((*b).gen_sym)(b, csp.get("@func.v1"));

        let id_entry = ((*b).gen_sym)(b, csp.get("@func.v1.entry"));

760
        let mut bbs = vec![id_entry];
761 762 763 764 765 766
        ((*b).new_func_ver)(b, id_funcver, id_func, bbs.as_mut_ptr(), bbs.len());

        {
            let mut args = vec![];
            let mut argtys = vec![];

767 768 769 770 771 772 773
            let id_new = ((*b).gen_sym)(b, csp.get("@func.v1.entry.new"));
            let id_newhybrid = ((*b).gen_sym)(b, csp.get("@func.v1.entry.newhybrid"));
            let id_getiref1 = ((*b).gen_sym)(b, csp.get("@func.v1.entry.getiref1"));
            let id_getiref2 = ((*b).gen_sym)(b, csp.get("@func.v1.entry.getiref2"));
            let id_getfieldiref1 = ((*b).gen_sym)(b, csp.get("@func.v1.entry.getfieldiref1"));
            let id_getfieldiref2 = ((*b).gen_sym)(b, csp.get("@func.v1.entry.getfieldiref2"));
            let id_getelemiref = ((*b).gen_sym)(b, csp.get("@func.v1.entry.getelemiref"));
774
            let id_getvarpartiref = ((*b).gen_sym)(b, csp.get("@func.v1.entry.getvarpartiref"));
775 776
            let id_shiftiref = ((*b).gen_sym)(b, csp.get("@func.v1.entry.shiftiref"));
            let id_ret = ((*b).gen_sym)(b, csp.get("@func.v1.entry.ret"));
777 778 779 780 781 782 783 784 785 786 787 788
            let mut insts = vec![
                id_new,
                id_newhybrid,
                id_getiref1,
                id_getiref2,
                id_getfieldiref1,
                id_getfieldiref2,
                id_getelemiref,
                id_getvarpartiref,
                id_shiftiref,
                id_ret,
            ];
789

790 791 792 793 794 795 796 797
            ((*b).new_bb)(
                b,
                id_entry,
                args.as_mut_ptr(),
                argtys.as_mut_ptr(),
                args.len(),
                0,
                insts.as_mut_ptr(),
798
                insts.len()
799
            );
800

801 802 803 804 805 806
            let id_v_r1 = ((*b).gen_sym)(b, csp.get("@func.v1.entry.r1"));
            let id_v_r2 = ((*b).gen_sym)(b, csp.get("@func.v1.entry.r2"));
            let id_v_i1 = ((*b).gen_sym)(b, csp.get("@func.v1.entry.i1"));
            let id_v_i2 = ((*b).gen_sym)(b, csp.get("@func.v1.entry.i2"));
            let id_v_f1 = ((*b).gen_sym)(b, csp.get("@func.v1.entry.f1"));
            let id_v_f2 = ((*b).gen_sym)(b, csp.get("@func.v1.entry.f2"));
807 808 809
            let id_v_e = ((*b).gen_sym)(b, csp.get("@func.v1.entry.e"));
            let id_v_v = ((*b).gen_sym)(b, csp.get("@func.v1.entry.v"));
            let id_v_s = ((*b).gen_sym)(b, csp.get("@func.v1.entry.s"));
810 811 812 813 814 815 816 817 818 819

            ((*b).new_new)(b, id_new, id_v_r1, id_s, 0);
            ((*b).new_newhybrid)(b, id_newhybrid, id_v_r2, id_h, id_i64, id_consti64_4, 0);

            ((*b).new_getiref)(b, id_getiref1, id_v_i1, id_s, id_v_r1);
            ((*b).new_getiref)(b, id_getiref2, id_v_i2, id_h, id_v_r2);

            ((*b).new_getfieldiref)(b, id_getfieldiref1, id_v_f1, 0, id_s, 1, id_v_i1);
            ((*b).new_getfieldiref)(b, id_getfieldiref2, id_v_f2, 0, id_h, 2, id_v_i2);

820 821 822 823 824 825 826 827
            ((*b).new_getelemiref)(
                b,
                id_getelemiref,
                id_v_e,
                0,
                id_a,
                id_i64,
                id_v_f1,
828
                id_consti64_3
829
            );
830

831
            ((*b).new_getvarpartiref)(b, id_getvarpartiref, id_v_v, 0, id_h, id_v_i2);
832 833 834 835 836 837 838 839
            ((*b).new_shiftiref)(
                b,
                id_shiftiref,
                id_v_s,
                0,
                id_i8,
                id_i64,
                id_v_v,
840
                id_consti64_3
841
            );
842 843 844 845 846 847

            {
                let mut args = vec![];
                ((*b).new_ret)(b, id_ret, args.as_mut_ptr(), args.len());
            }
        }
848

849

850 851 852 853 854 855
        ((*b).load)(b);
        ((*ctx).close_context)(ctx);

        info!("Finished.");
    }
}