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

15 16
#![allow(unused_macros)]

17
macro_rules! typedef {
qinsoon's avatar
qinsoon committed
18
    // int, floating point
19
    (($vm: expr) $name: ident = mu_int($len: expr)) => {
qinsoon's avatar
qinsoon committed
20 21
        let $name = $vm.declare_type(MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
                                     MuType_::int($len));
22
        $vm.set_name($name.as_entity());
23
    };
qinsoon's avatar
qinsoon committed
24
    (($vm: expr) $name: ident = mu_double) => {
qinsoon's avatar
qinsoon committed
25 26
        let $name = $vm.declare_type(MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
                                     MuType_::double());
27
        $vm.set_name($name.as_entity());
qinsoon's avatar
qinsoon committed
28
    };
29
    (($vm: expr) $name: ident = mu_float) => {
qinsoon's avatar
qinsoon committed
30 31
        let $name = $vm.declare_type(MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
                                     MuType_::float());
32 33
        $vm.set_name($name.as_entity());
    };
qinsoon's avatar
qinsoon committed
34

qinsoon's avatar
qinsoon committed
35
    // ref, iref, ptr
36
    (($vm: expr) $name: ident = mu_ref($ty: ident)) => {
qinsoon's avatar
qinsoon committed
37 38
        let $name = $vm.declare_type(MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
                                     MuType_::muref($ty.clone()));
39
        $vm.set_name($name.as_entity());
40 41
    };
    (($vm: expr) $name: ident = mu_iref($ty: ident)) => {
qinsoon's avatar
qinsoon committed
42 43
        let $name = $vm.declare_type(MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
                                     MuType_::iref($ty.clone()));
44
        $vm.set_name($name.as_entity());
45
    };
qinsoon's avatar
qinsoon committed
46
    (($vm: expr) $name: ident = mu_uptr($ty: ident)) => {
qinsoon's avatar
qinsoon committed
47 48
        let $name = $vm.declare_type(MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
                                     MuType_::uptr($ty.clone()));
49
        $vm.set_name($name.as_entity());
qinsoon's avatar
qinsoon committed
50 51
    };

qinsoon's avatar
qinsoon committed
52
    // struct
53
    (($vm: expr) $name: ident = mu_struct($($ty: ident), *)) => {
qinsoon's avatar
qinsoon committed
54 55 56
        let $name = $vm.declare_type(MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
                                     MuType_::mustruct(Mu(stringify!($name)),
                                                       vec![$($ty.clone()),*]));
57
        $vm.set_name($name.as_entity());
58
    };
qinsoon's avatar
qinsoon committed
59
    (($vm: expr) $name: ident = mu_struct()) => {
qinsoon's avatar
qinsoon committed
60 61
        let $name = $vm.declare_type(MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
                                     MuType_::mustruct(Mu(stringify!($name)), vec![]));
62
        $vm.set_name($name.as_entity());
qinsoon's avatar
qinsoon committed
63
    };
qinsoon's avatar
qinsoon committed
64
    (($vm: expr) $name: ident = mu_struct_placeholder()) => {
qinsoon's avatar
qinsoon committed
65 66
        let $name = $vm.declare_type(MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
                                     MuType_::mustruct_empty(Mu(stringify!($name))));
67
        $vm.set_name($name.as_entity());
qinsoon's avatar
qinsoon committed
68 69 70 71
    };
    (($vm: expr) mu_struct_put($name: ident, $($ty: ident), *)) => {
        MuType_::mustruct_put(&Mu(stringify!($name)), vec![$($ty.clone()), *])
    };
qinsoon's avatar
qinsoon committed
72

qinsoon's avatar
qinsoon committed
73
    // hybrid
74
    (($vm: expr) $name: ident = mu_hybrid($($ty: ident), *)($var_ty: ident)) => {
qinsoon's avatar
qinsoon committed
75 76 77
        let $name = $vm.declare_type(MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
                                     MuType_::hybrid(Mu(stringify!($name)),
                                                     vec![$($ty.clone()), *], $var_ty.clone()));
78
        $vm.set_name($name.as_entity());
qinsoon's avatar
qinsoon committed
79
    };
qinsoon's avatar
qinsoon committed
80

qinsoon's avatar
fix  
qinsoon committed
81
    // array
82
    (($vm: expr) $name: ident = mu_array($ty: ident, $len: expr)) => {
qinsoon's avatar
qinsoon committed
83 84
        let $name = $vm.declare_type(MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
                                     MuType_::array($ty.clone(), $len));
85
        $vm.set_name($name.as_entity());
86
    };
qinsoon's avatar
fix  
qinsoon committed
87

qinsoon's avatar
qinsoon committed
88
    // funcref
qinsoon's avatar
qinsoon committed
89
    (($vm: expr) $name: ident = mu_funcref($sig: ident)) => {
qinsoon's avatar
qinsoon committed
90 91
        let $name = $vm.declare_type(MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
                                     MuType_::funcref($sig.clone()));
92 93 94 95 96
        $vm.set_name($name.as_entity());
    };

    // ufuncptr
    (($vm: expr) $name: ident = mu_ufuncptr($sig: ident)) => {
qinsoon's avatar
qinsoon committed
97 98
        let $name = $vm.declare_type(MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
                                     MuType_::ufuncptr($sig.clone()));
99 100
        $vm.set_name($name.as_entity());
    };
101 102 103 104
}

macro_rules! constdef {
    (($vm: expr) <$ty: ident> $name: ident = $val: expr) => {
105 106 107 108 109
        let $name = $vm.declare_const(
            MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
            $ty.clone(),
            $val,
        );
110
        $vm.set_name($name.as_entity());
111
    };
112 113
}

114 115
macro_rules! globaldef {
    (($vm: expr) <$ty: ident> $name: ident) => {
116 117 118 119
        let $name = $vm.declare_global(
            MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
            $ty.clone(),
        );
120
        $vm.set_name($name.as_entity());
121
    };
122 123
}

124 125
macro_rules! funcsig {
    (($vm: expr) $name: ident = ($($arg_ty: ident),*) -> ($($ret_ty: ident),*)) => {
qinsoon's avatar
qinsoon committed
126 127 128
        let $name = $vm.declare_func_sig(MuEntityHeader::named($vm.next_id(),
                                                               Mu(stringify!($name))),
                                         vec![$($ret_ty.clone()),*], vec![$($arg_ty.clone()),*]);
129
        $vm.set_name($name.as_entity());
130 131 132 133 134
    }
}

macro_rules! funcdecl {
    (($vm: expr) <$sig: ident> $name: ident) => {
135 136 137 138
        let func = MuFunction::new(
            MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
            $sig.clone(),
        );
139
        $vm.set_name(func.as_entity());
140
        let $name = func.hdr.clone();
141
        $vm.declare_func(func);
142
    };
143 144 145 146
}

macro_rules! funcdef {
    (($vm: expr) <$sig: ident> $func: ident VERSION $version: ident) => {
147 148 149 150 151
        let mut $version = MuFunctionVersion::new(
            MuEntityHeader::named($vm.next_id(), Mu(stringify!($version))),
            $func.id(),
            $sig.clone(),
        );
152
        $vm.set_name($version.as_entity());
153
    };
154 155 156 157
}

macro_rules! define_func_ver {
    (($vm: expr) $fv: ident (entry: $entry: ident){$($blk: ident), *}) => {
158 159 160 161 162
        $fv.define(FunctionContent::new($entry.id(), {
            let mut ret = LinkedHashMap::new();
            $ (ret.insert($blk.id(), $blk); )*
            ret
        }));
163 164 165 166 167 168 169

        $vm.define_func_version($fv);
    }
}

macro_rules! block {
    (($vm: expr, $fv: ident) $name: ident) => {
170 171
        let mut $name = Block::new(MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))));
        $vm.set_name($name.as_entity());
172
    };
173 174 175 176 177 178 179 180 181 182
}

macro_rules! define_block {
    (($vm: expr, $fv: ident) $name: ident ($($arg: ident), *) {$($inst: ident), *}) => {
        $name.content = Some(BlockContent{
            args: vec![$($arg.clone_value()), *],
            exn_arg: None,
            body: vec![$($inst), *],
            keepalives: None
        });
183 184
    };

qinsoon's avatar
qinsoon committed
185 186
    (($vm: expr, $fv: ident) $name: ident ($($arg: ident), *)
     [$exn_arg: ident] {$($inst: ident), *}) => {
187 188 189 190 191 192
        $name.content = Some(BlockContent{
            args: vec![$($arg.clone_value()), *],
            exn_arg: Some($exn_arg.clone_value()),
            body: vec![$($inst), *],
            keepalives: None
        });
193 194 195 196 197
    }
}

macro_rules! ssa {
    (($vm: expr, $fv: ident) <$ty: ident> $name: ident) => {
198 199 200 201
        let $name = $fv.new_ssa(
            MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
            $ty.clone(),
        );
202
        $vm.set_name($name.as_entity());
203
    };
204 205
}

206 207 208
macro_rules! machine_reg {
    (($vm: expr, $fv: ident) $name: ident = $mreg: expr) => {
        let $name = $fv.new_machine_reg($mreg.clone());
209
    };
210 211
}

212 213 214
macro_rules! consta {
    (($vm: expr, $fv: ident) $name: ident = $c: ident) => {
        let $name = $fv.new_constant($c.clone());
215
    };
216 217
}

218 219 220
macro_rules! global {
    (($vm: expr, $fv: ident) $name: ident = $g: ident) => {
        let $name = $fv.new_global($g.clone());
221
    };
222 223
}

224 225 226 227 228 229
macro_rules! inst {
    // NEW
    (($vm: expr, $fv: ident) $name: ident: $value: ident = NEW <$ty: ident>) => {
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  Some(vec![$value.clone_value()]),
230
            ops:    vec![],
231 232 233 234
            v:      Instruction_::New($ty.clone())
        });
    };

235 236 237 238 239
    // NEWHYBRID
    (($vm: expr, $fv: ident) $name: ident: $value: ident = NEWHYBRID <$ty: ident> $len: ident) => {
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  Some(vec![$value.clone_value()]),
240
            ops:    vec![$len.clone()],
241 242 243 244
            v:      Instruction_::NewHybrid($ty.clone(), 0)
        });
    };

245 246 247 248 249
    // GETIREF
    (($vm: expr, $fv: ident) $name: ident: $value: ident = GETIREF $op: ident) => {
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  Some(vec![$value.clone_value()]),
250
            ops:    vec![$op.clone()],
251 252 253 254 255
            v:      Instruction_::GetIRef(0)
        });
    };

    // GETFIELDIREF
qinsoon's avatar
qinsoon committed
256 257
    (($vm: expr, $fv: ident) $name: ident: $value: ident = GETFIELDIREF $op: ident
     (is_ptr: $is_ptr: expr, index: $index: expr)) => {
258 259 260
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  Some(vec![$value.clone_value()]),
261
            ops:    vec![$op.clone()],
262 263 264 265 266 267 268 269
            v:      Instruction_::GetFieldIRef {
                        is_ptr: $is_ptr,
                        base: 0,
                        index: $index
            }
        });
    };

270
    // GETELEMIREF
qinsoon's avatar
qinsoon committed
271 272
    (($vm: expr, $fv: ident) $name: ident: $value: ident = GETELEMIREF $op: ident $index: ident
     (is_ptr: $is_ptr: expr)) => {
273 274 275
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  Some(vec![$value.clone_value()]),
276
            ops:    vec![$op.clone(), $index.clone()],
277 278 279 280 281 282 283 284
            v:      Instruction_::GetElementIRef {
                        is_ptr: $is_ptr,
                        base: 0,
                        index: 1
            }
        });
    };

qinsoon's avatar
qinsoon committed
285
    // GETVARPARTIREF
qinsoon's avatar
qinsoon committed
286 287
    (($vm: expr, $fv: ident) $name: ident: $value: ident = GETVARPARTIREF $op: ident
     (is_ptr: $is_ptr: expr)) => {
qinsoon's avatar
qinsoon committed
288 289 290
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  Some(vec![$value.clone_value()]),
291
            ops:    vec![$op.clone()],
qinsoon's avatar
qinsoon committed
292 293 294 295 296 297 298 299
            v:      Instruction_::GetVarPartIRef {
                        is_ptr: $is_ptr,
                        base: 0
            }
        });
    };

    // SHIFTIREF
qinsoon's avatar
qinsoon committed
300 301
    (($vm: expr, $fv: ident) $name: ident: $value: ident = SHIFTIREF $op: ident $offset: ident
     (is_ptr: $is_ptr: expr)) => {
qinsoon's avatar
qinsoon committed
302 303 304
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  Some(vec![$value.clone_value()]),
305
            ops:    vec![$op.clone(), $offset.clone()],
qinsoon's avatar
qinsoon committed
306 307 308 309 310 311 312 313
            v:      Instruction_::ShiftIRef {
                        is_ptr: $is_ptr,
                        base: 0,
                        offset: 1
            }
        });
    };

314
    // STORE
qinsoon's avatar
qinsoon committed
315 316
    (($vm: expr, $fv: ident) $name: ident: STORE $loc: ident $val: ident
     (is_ptr: $is_ptr: expr, order: $order: expr)) => {
317 318 319
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  None,
320
            ops:    vec![$loc.clone(), $val.clone()],
321 322 323 324 325 326 327 328 329 330
            v:      Instruction_::Store {
                        is_ptr: $is_ptr,
                        order: $order,
                        mem_loc: 0,
                        value: 1
            }
        });
    };

    // LOAD
qinsoon's avatar
qinsoon committed
331 332
    (($vm: expr, $fv: ident) $name: ident: $value: ident = LOAD $loc: ident
     (is_ptr: $is_ptr: expr, order: $order: expr)) => {
333 334 335
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  Some(vec![$value.clone_value()]),
336
            ops:    vec![$loc.clone()],
337 338 339 340 341 342 343 344 345
            v:      Instruction_::Load {
                        is_ptr: $is_ptr,
                        order: $order,
                        mem_loc: 0
            }
        });
    };

    // BINOP
qinsoon's avatar
qinsoon committed
346 347
    (($vm: expr, $fv: ident) $name: ident: $value: ident =
     BINOP ($op: expr) $op1: ident $op2: ident) => {
348 349 350
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  Some(vec![$value.clone_value()]),
351
            ops:    vec![$op1.clone(), $op2.clone()],
352 353 354 355
            v:      Instruction_::BinOp($op, 0, 1)
        });
    };

qinsoon's avatar
qinsoon committed
356
    // BINOP with status
qinsoon's avatar
qinsoon committed
357 358
    (($vm: expr, $fv: ident) $name: ident: $value: ident, $($flag: ident), * =
     BINOP_STATUS ($op: expr) ($flags: expr) $op1: ident $op2: ident) => {
qinsoon's avatar
qinsoon committed
359 360 361
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  Some(vec![$value.clone_value(), $($flag.clone_value()), *]),
362
            ops:    vec![$op1.clone(), $op2.clone()],
qinsoon's avatar
qinsoon committed
363 364 365 366
            v:      Instruction_::BinOpWithStatus($op, $flags, 0, 1)
        });
    };

qinsoon's avatar
qinsoon committed
367
    // CMPOP
qinsoon's avatar
qinsoon committed
368 369
    (($vm: expr, $fv: ident) $name: ident: $value: ident =
     CMPOP ($op: expr) $op1: ident $op2: ident) => {
qinsoon's avatar
qinsoon committed
370 371 372
        let $name = $fv.new_inst(Instruction{
            hdr: MuEntityHeader::unnamed($vm.next_id()),
            value: Some(vec![$value.clone_value()]),
373
            ops: vec![$op1.clone(), $op2.clone()],
qinsoon's avatar
qinsoon committed
374 375 376 377
            v: Instruction_::CmpOp($op, 0, 1)
        });
    };

378
    // CONVOP
qinsoon's avatar
qinsoon committed
379 380
    (($vm: expr, $fv: ident) $name: ident: $value: ident =
     CONVOP ($operation: expr) <$ty1: ident $ty2: ident> $operand: ident) => {
381 382 383
        let $name = $fv.new_inst(Instruction{
            hdr: MuEntityHeader::unnamed($vm.next_id()),
            value: Some(vec![$value.clone_value()]),
384
            ops: vec![$operand.clone()],
385 386 387 388 389 390 391 392 393
            v: Instruction_::ConvOp{
                operation: $operation,
                from_ty: $ty1.clone(),
                to_ty: $ty2.clone(),
                operand: 0
            }
        });
    };

qinsoon's avatar
qinsoon committed
394
    // SELECT
qinsoon's avatar
qinsoon committed
395 396
    (($vm: expr, $fv: ident) $name: ident: $value: ident =
     SELECT $cond: ident $op_true: ident $op_false:ident) => {
qinsoon's avatar
qinsoon committed
397 398 399
        let $name = $fv.new_inst(Instruction{
            hdr: MuEntityHeader::unnamed($vm.next_id()),
            value: Some(vec![$value.clone_value()]),
400
            ops: vec![$cond.clone(), $op_true.clone(), $op_false.clone()],
qinsoon's avatar
qinsoon committed
401 402 403 404 405 406 407 408
            v: Instruction_::Select{
                cond: 0,
                true_val: 1,
                false_val: 2
            }
        });
    };

409 410 411 412 413
    // BRANCH
    (($vm: expr, $fv: ident) $name: ident: BRANCH $dest: ident ($($arg: ident), *)) => {
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  None,
414
            ops:    vec![$($arg.clone()),*],
415
            v:      Instruction_::Branch1(Destination{
416
                        target: $dest.hdr.clone(),
417 418
                        args: {
                            let mut i =0;
419
                            vec![$($arg.clone()),*].iter().map(|_: &Arc<TreeNode>| {
qinsoon's avatar
qinsoon committed
420 421
                                let ret = DestArg::Normal(i); i+=1; ret
                             }).collect()
422 423 424 425 426
                        }
            })
        });
    };

qinsoon's avatar
qinsoon committed
427 428 429 430 431 432 433 434 435 436 437 438 439
    // BRANCH2
    // list all operands first
    // then use vector expr to list operands for each destination
    // (we cannot have two repetition list of different lengths in a macro)
    (($vm: expr, $fv: ident) $name: ident:
        BRANCH2 ($($op: ident), *)
            IF (OP $cond: expr)
            THEN $true_dest : ident ($true_args: expr) WITH $prob: expr,
            ELSE $false_dest: ident ($false_args: expr)
    ) => {
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  None,
440
            ops:    vec![$($op.clone()),*],
qinsoon's avatar
qinsoon committed
441 442 443 444 445 446 447 448 449 450 451 452
            v:      {
                let true_args = {
                    $true_args.iter().map(|x| DestArg::Normal(*x)).collect()
                };

                let false_args = {
                    $false_args.iter().map(|x| DestArg::Normal(*x)).collect()
                };

                Instruction_::Branch2{
                    cond: $cond,
                    true_dest: Destination {
453
                        target: $true_dest.hdr.clone(),
qinsoon's avatar
qinsoon committed
454 455 456
                        args: true_args
                    },
                    false_dest: Destination {
457
                        target: $false_dest.hdr.clone(),
qinsoon's avatar
qinsoon committed
458 459 460 461 462 463 464 465
                        args: false_args
                    },
                    true_prob: $prob
                }
            }
        });
    };

466
    // EXPRCALL
qinsoon's avatar
qinsoon committed
467 468
    (($vm: expr, $fv: ident) $name: ident: $res: ident =
     EXPRCALL ($cc: expr, is_abort: $is_abort: expr) $func: ident ($($val: ident), *)) => {
469
        let ops = vec![$func.clone(), $($val.clone()), *];
qinsoon's avatar
qinsoon committed
470 471 472 473
        let ops_len = ops.len();
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  Some(vec![$res.clone_value()]),
474
            ops:    ops,
qinsoon's avatar
qinsoon committed
475 476 477 478 479 480 481 482 483 484
            v:      Instruction_::ExprCall {
                        data: CallData {
                            func: 0,
                            args: (1..ops_len).collect(),
                            convention: $cc
                        },
                        is_abort: $is_abort
                    }
        });
    };
qinsoon's avatar
qinsoon committed
485 486
    (($vm: expr, $fv: ident) $name: ident:
     EXPRCALL ($cc: expr, is_abort: $is_abort: expr) $func: ident ($($val: ident), *)) => {
qinsoon's avatar
[wip]  
qinsoon committed
487 488 489 490 491
        let ops = vec![$func.clone(), $($val.clone()), *];
        let ops_len = ops.len();
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  Some(vec![]),
492
            ops:    ops,
qinsoon's avatar
[wip]  
qinsoon committed
493 494 495 496 497 498 499 500 501 502
            v:      Instruction_::ExprCall {
                        data: CallData {
                            func: 0,
                            args: (1..ops_len).collect(),
                            convention: $cc
                        },
                        is_abort: $is_abort
                    }
        });
    };
503 504

    // EXPRCCALL
qinsoon's avatar
qinsoon committed
505 506
    (($vm: expr, $fv: ident) $name: ident: $res: ident =
     EXPRCCALL ($cc: expr, is_abort: $is_abort: expr) $func: ident ($($val: ident), *)) => {
507 508 509 510 511
        let ops = vec![$func.clone(), $($val.clone()), *];
        let ops_len = ops.len();
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  Some(vec![$res.clone_value()]),
512
            ops:    ops,
513 514 515 516 517 518 519 520 521 522
            v:      Instruction_::ExprCCall {
                        data: CallData {
                            func: 0,
                            args: (1..ops_len).collect(),
                            convention: $cc
                        },
                        is_abort: $is_abort
                    }
        });
    };
qinsoon's avatar
qinsoon committed
523 524
    (($vm: expr, $fv: ident) $name: ident:
    EXPRCCALL ($cc: expr, is_abort: $is_abort: expr) $func: ident ($($val: ident), *)) => {
525 526 527 528 529
        let ops = vec![$func.clone(), $($val.clone()), *];
        let ops_len = ops.len();
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  Some(vec![]),
530
            ops:    ops,
531 532 533 534 535 536 537 538 539 540 541
            v:      Instruction_::ExprCCall {
                        data: CallData {
                            func: 0,
                            args: (1..ops_len).collect(),
                            convention: $cc
                        },
                        is_abort: $is_abort
                    }
        });
    };

542
    // CALL (1 return result)
qinsoon's avatar
qinsoon committed
543 544
    (($vm: expr, $fv: ident) $name: ident: $res: ident =
     CALL ($($op: ident), *) FUNC($func: expr) ($args: expr) $cc: expr,
545 546 547 548 549
                      normal: $norm_dest: ident ($norm_args: expr),
                      exc: $exc_dest: ident ($exc_args: expr)) => {
        let $name = $fv.new_inst(Instruction {
            hdr  : MuEntityHeader::unnamed($vm.next_id()),
            value: Some(vec![$res.clone_value()]),
550
            ops  : vec![$($op.clone()),*],
551 552 553 554 555 556 557 558
            v    : Instruction_::Call {
                data: CallData {
                    func: $func,
                    args: $args,
                    convention: $cc
                },
                resume: ResumptionData {
                    normal_dest: Destination {
559
                        target: $norm_dest.hdr.clone(),
560 561 562
                        args  : $norm_args
                    },
                    exn_dest: Destination {
563
                        target: $exc_dest.hdr.clone(),
564 565 566 567 568 569
                        args  : $exc_args
                    }
                }
            }
        });
    };
570 571 572 573 574 575 576 577
    // CALL (no return value)
    (($vm: expr, $fv: ident) $name: ident:
        CALL ($($op: ident), *) FUNC($func: expr) ($args: expr) $cc: expr,
                      normal: $norm_dest: ident ($norm_args: expr),
                      exc: $exc_dest: ident ($exc_args: expr)) => {
        let $name = $fv.new_inst(Instruction {
            hdr  : MuEntityHeader::unnamed($vm.next_id()),
            value: None,
578
            ops  : vec![$($op.clone()),*],
579 580 581 582 583 584 585 586
            v    : Instruction_::Call {
                data: CallData {
                    func: $func,
                    args: $args,
                    convention: $cc
                },
                resume: ResumptionData {
                    normal_dest: Destination {
587
                        target: $norm_dest.hdr.clone(),
588 589 590
                        args  : $norm_args
                    },
                    exn_dest: Destination {
591
                        target: $exc_dest.hdr.clone(),
592 593 594 595 596 597 598
                        args  : $exc_args
                    }
                }
            }
        });
    };

qinsoon's avatar
qinsoon committed
599

600
    // RET
601
    (($vm: expr, $fv: ident) $name: ident: RET ($($val: ident), +)) => {
602 603 604
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  None,
605
            ops:    vec![$($val.clone()), *],
606 607
            v:      Instruction_::Return({
                        let mut i = 0;
qinsoon's avatar
qinsoon committed
608
                        vec![$($val.clone()), *].iter().map(|_| {let ret = i; i+= 1; ret}).collect()
609 610 611
                    })
        });
    };
612 613 614 615 616
    // RET (no value)
    (($vm: expr, $fv: ident) $name: ident: RET) => {
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  None,
617
            ops:    vec![],
618 619 620
            v:      Instruction_::Return(vec![])
        });
    };
621 622 623 624 625 626

    // THREADEXIT
    (($vm: expr, $fv: ident) $name: ident: THREADEXIT) => {
        let $name = $fv.new_inst(Instruction{
            hdr: MuEntityHeader::unnamed($vm.next_id()),
            value: None,
627
            ops: vec![],
628 629 630 631
            v: Instruction_::ThreadExit
        });
    };

632 633 634 635 636
    // THROW
    (($vm: expr, $fv: ident) $name: ident: THROW $op: ident) => {
        let $name = $fv.new_inst(Instruction{
            hdr: MuEntityHeader::unnamed($vm.next_id()),
            value: None,
637
            ops: vec![$op.clone()],
638 639 640 641
            v: Instruction_::Throw(0)
        });
    };

642 643 644 645 646
    // PRINTHEX
    (($vm: expr, $fv: ident) $name: ident: PRINTHEX $val: ident) => {
        let $name = $fv.new_inst(Instruction{
            hdr: MuEntityHeader::unnamed($vm.next_id()),
            value: None,
647
            ops: vec![$val.clone()],
648 649
            v: Instruction_::PrintHex(0)
        });
650 651 652 653 654 655 656 657 658 659 660
    };

    // SET_RETVAL
    (($vm: expr, $fv: ident) $name: ident: SET_RETVAL $val: ident) => {
        let $name = $fv.new_inst(Instruction{
            hdr: MuEntityHeader::unnamed($vm.next_id()),
            value: None,
            ops: vec![$val.clone()],
            v: Instruction_::SetRetval(0)
        });
    };
661 662 663 664 665 666 667 668 669 670

    // MOVE
    (($vm: expr, $fv: ident) $name: ident: MOVE $src: ident -> $dst: ident) => {
        let $name = $fv.new_inst(Instruction {
            hdr: MuEntityHeader::unnamed($vm.next_id()),
            value: Some(vec![$dst.clone_value()]),
            ops: vec![$src],
            v: Instruction_::Move(0)
        });
    };
671
}
672 673 674 675 676

/**************************************
This macro is used as follows:
1- for a test like add_simple(int, int) -> int,
the following syntax should be used (each I  means an int):
677 678
      emit_test!      ((vm) (add add_test1 add_test1_v1
        III (sig, int64(22), int64(27), int64(49))));
679 680 681 682 683 684 685 686 687 688 689 690 691
2- for a test like add_double(double, double) -> double,
the following syntax should be used (each I  means an int):
      emit_test! ((vm) (double_add test1 FFF (sig, f64(1f64), f64(1f64), f64(2f64))));

0- other test types may be manually added using the same approach
***************************************
Macro limitations and points to use:
1 - Macro assumes that the test function signature is named "sig" \
    as currently is.

****************************************/
macro_rules! emit_test {
    /*
692 693 694 695 696 697 698
    emit_test! ((vm)
        udiv udiv_test1 udiv_test1_v1
        Int,Int, Int
        EQ
        udiv_sig
        int64(22), int64(4), int64(5)
    );
699
    */
700 701 702 703 704 705 706
    (($vm: expr)
        $name: ident, $test_name: ident, $tester_name: ident,
        $Arg1Type: ident, $Arg2Type: ident RET $Arg3Type: ident,
        $CMPType: ident,
        $test_sig: ident,
        $ty1: ident($in1: expr), $ty2: ident($in2: expr) RET $ty3: ident($out: expr),
    ) => {
707
        typedef!    (($vm) int1  = mu_int(1));
708 709 710
        typedef!    (($vm) int32t  = mu_int(32));
        constdef!   (($vm) <int32t> int64_pass = Constant::Int(0));
        constdef!   (($vm) <int32t> int64_fail = Constant::Int(1));
711 712 713 714 715 716 717 718 719 720 721 722
        constdef!   (($vm) <$ty1> f64_0 = Constant::$Arg1Type($in1));
        constdef!   (($vm) <$ty2> f64_1 = Constant::$Arg2Type($in2));
        constdef!   (($vm) <$ty3> f64_2 = Constant::$Arg3Type($out));

        funcsig!    (($vm) tester_sig = () -> ());
        funcdecl!   (($vm) <tester_sig> $test_name);
        funcdef!    (($vm) <tester_sig> $test_name VERSION $tester_name);

        ssa!    (($vm, $tester_name) <$ty1> a);
        ssa!    (($vm, $tester_name) <$ty1> b);

        typedef!    (($vm) type_funcref = mu_funcref($test_sig));
723
        constdef!   (($vm) <type_funcref> const_funcref =
724
            Constant::FuncRef($name.clone()));
725 726 727 728 729 730 731 732 733 734

        // blk_entry
        consta!     (($vm, $tester_name) f64_0_local = f64_0);
        consta!     (($vm, $tester_name) f64_1_local = f64_1);

        block!      (($vm, $tester_name) blk_entry);

        consta!     (($vm, $tester_name) const_funcref_local = const_funcref);
        ssa!    (($vm, $tester_name) <$ty3> result);
        inst!   (($vm, $tester_name) blk_entry_call:
735 736
            result = EXPRCALL (CallConvention::Mu, is_abort: false)
                const_funcref_local (f64_0_local, f64_1_local)
737 738 739 740 741 742 743 744 745 746
        );

        consta!     (($vm, $tester_name) f64_2_local = f64_2);
        consta!     (($vm, $tester_name) int64_pass_local = int64_pass);
        consta!     (($vm, $tester_name) int64_fail_local = int64_fail);
        ssa!    (($vm, $tester_name) <int1> cmp_res);
        inst!   (($vm, $tester_name) blk_entry_cmp:
            cmp_res = CMPOP (CmpOp::$CMPType) result f64_2_local
        );

747
        ssa!    (($vm, $tester_name) <int32t> blk_entry_ret);
748 749 750 751 752 753 754 755 756 757 758
        inst!   (($vm, $tester_name) blk_entry_inst_select:
            blk_entry_ret = SELECT cmp_res int64_pass_local int64_fail_local
        );

        inst!   (($vm, $tester_name) blk_entry_inst_ret:
             SET_RETVAL blk_entry_ret
        );
        inst!   (($vm, $tester_name) blk_entry_inst_exit:
            THREADEXIT
        );

759
        define_block!   (($vm, $tester_name) blk_entry() {
760 761 762 763 764 765 766 767 768 769 770 771 772
             blk_entry_call,
             blk_entry_cmp,
             blk_entry_inst_select,
             blk_entry_inst_ret,
             blk_entry_inst_exit
        });

        define_func_ver!    (($vm) $tester_name (entry: blk_entry) {
            blk_entry
        });

    };
    /*
773 774 775 776 777 778
    emit_test! ((vm) (test_sitofp, test_sitofp_test1, test_sitofp_test1_v1)
        Int -> Double
        EQ
        sig
        int64(-1i64) -> double(-1f64)
    );
779
    */
780 781 782 783 784 785 786
    (($vm: expr)
        $name: ident, $test_name: ident, $tester_name: ident,
        $Arg1Type: ident RET $Arg3Type: ident,
        $CMPType: ident,
        $test_sig: ident,
        $ty1: ident($in1: expr) RET $ty3: ident($out: expr),
    ) => {
787
        typedef!    (($vm) int1  = mu_int(1));
788 789 790
        typedef!    (($vm) int32t  = mu_int(32));
        constdef!   (($vm) <int32t> int64_pass = Constant::Int(0));
        constdef!   (($vm) <int32t> int64_fail = Constant::Int(1));
791 792 793 794 795 796 797 798
        constdef!   (($vm) <$ty1> f64_0 = Constant::$Arg1Type($in1));
        constdef!   (($vm) <$ty3> f64_2 = Constant::$Arg3Type($out));

        funcsig!    (($vm) tester_sig = () -> ());
        funcdecl!   (($vm) <tester_sig> $test_name);
        funcdef!    (($vm) <tester_sig> $test_name VERSION $tester_name);
        ssa!    (($vm, $tester_name) <$ty1> a);
        typedef!    (($vm) type_funcref = mu_funcref($test_sig));
799
        constdef!   (($vm) <type_funcref> const_funcref =
800
            Constant::FuncRef($name.clone()));
801 802
        // blk_entry
        consta!     (($vm, $tester_name) f64_0_local = f64_0);
803

804 805 806 807 808
        block!      (($vm, $tester_name) blk_entry);

        consta!     (($vm, $tester_name) const_funcref_local = const_funcref);
        ssa!    (($vm, $tester_name) <$ty3> result);
        inst!   (($vm, $tester_name) blk_entry_call:
809 810
            result = EXPRCALL (CallConvention::Mu, is_abort: false)
                const_funcref_local (f64_0_local)
811 812 813 814 815 816 817 818 819 820
        );

        consta!     (($vm, $tester_name) f64_2_local = f64_2);
        consta!     (($vm, $tester_name) int64_pass_local = int64_pass);
        consta!     (($vm, $tester_name) int64_fail_local = int64_fail);
        ssa!    (($vm, $tester_name) <int1> cmp_res);
        inst!   (($vm, $tester_name) blk_entry_cmp:
            cmp_res = CMPOP (CmpOp::$CMPType) result f64_2_local
        );

821
        ssa!    (($vm, $tester_name) <int32t> blk_entry_ret);
822 823 824 825 826 827 828 829 830 831
        inst!   (($vm, $tester_name) blk_entry_inst_select:
            blk_entry_ret = SELECT cmp_res int64_pass_local int64_fail_local
        );

        inst!   (($vm, $tester_name) blk_entry_inst_ret:
             SET_RETVAL blk_entry_ret
        );
        inst!   (($vm, $tester_name) blk_entry_inst_exit:
            THREADEXIT
        );
832
        define_block!   (($vm, $tester_name) blk_entry() {
833 834 835 836 837 838 839 840 841 842 843 844 845
             blk_entry_call,
             blk_entry_cmp,
             blk_entry_inst_select,
             blk_entry_inst_ret,
             blk_entry_inst_exit
        });

        define_func_ver!    (($vm) $tester_name (entry: blk_entry) {
            blk_entry
        });

    };
    /*
846 847
    emit_test!      ((vm) (pass_1arg_by_stack pass_1arg_by_stack_test1 pass_1arg_by_stack_test1_v1
        Int,EQ (sig, int64(1u64))));
848
    */
849 850 851 852 853 854 855
    (($vm: expr)
        $name: ident, $test_name: ident, $tester_name: ident,
        RET $Arg1Type: ident,
        $CMPType: ident,
        $test_sig: ident,
        RET $ty1: ident($in1: expr),
    ) => {
856
        typedef!    (($vm) int1  = mu_int(1));
857 858 859
        typedef!    (($vm) int32t  = mu_int(32));
        constdef!   (($vm) <int32t> int64_pass = Constant::Int(0));
        constdef!   (($vm) <int32t> int64_fail = Constant::Int(1));
860 861 862 863 864 865
        constdef!   (($vm) <$ty1> arg_0 = Constant::$Arg1Type($in1));
        funcsig!    (($vm) tester_sig = () -> ());
        funcdecl!   (($vm) <tester_sig> $test_name);
        funcdef!    (($vm) <tester_sig> $test_name VERSION $tester_name);

        typedef!    (($vm) type_funcref = mu_funcref($test_sig));
866
        constdef!   (($vm) <type_funcref> const_funcref =
867
            Constant::FuncRef($name.clone()));
868 869 870

        // blk_entry
        consta!     (($vm, $tester_name) arg_0_local = arg_0);
871

872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
        block!      (($vm, $tester_name) blk_entry);

        consta!     (($vm, $tester_name) const_funcref_local = const_funcref);
        ssa!    (($vm, $tester_name) <$ty1> result);
        inst!   (($vm, $tester_name) blk_entry_call:
            result = EXPRCALL (CallConvention::Mu, is_abort: false) const_funcref_local ()
        );

        consta!     (($vm, $tester_name) int64_pass_local = int64_pass);
        consta!     (($vm, $tester_name) int64_fail_local = int64_fail);
        ssa!    (($vm, $tester_name) <int1> cmp_res);
        inst!   (($vm, $tester_name) blk_entry_cmp:
            cmp_res = CMPOP (CmpOp::$CMPType) result arg_0_local
        );

887
        ssa!    (($vm, $tester_name) <int32t> blk_entry_ret);
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
        inst!   (($vm, $tester_name) blk_entry_inst_select:
            blk_entry_ret = SELECT cmp_res int64_pass_local int64_fail_local
        );

        inst!   (($vm, $tester_name) blk_entry_inst_ret:
             SET_RETVAL blk_entry_ret
        );
        inst!   (($vm, $tester_name) blk_entry_inst_exit:
            THREADEXIT
        );

        define_block!   (($vm, $tester_name) blk_entry() {
             blk_entry_call,
             blk_entry_cmp,
             blk_entry_inst_select,
             blk_entry_inst_ret,
             blk_entry_inst_exit
        });

        define_func_ver!    (($vm) $tester_name (entry: blk_entry) {
            blk_entry
        });

    };
912

913
    /*
914 915
    emit_test!      ((vm) (catch_exception catch_exception_test1 catch_exception_test1_v1
        (catch_exception_sig)));
916
    */
917 918 919 920
    (($vm: expr)
        $name: ident, $test_name: ident, $tester_name: ident,
        $test_sig: ident,
    ) => {
921
        typedef!    (($vm) int1  = mu_int(1));
922 923 924
        typedef!    (($vm) int32t  = mu_int(32));
        constdef!   (($vm) <int32t> int64_pass = Constant::Int(0));
        constdef!   (($vm) <int32t> int64_fail = Constant::Int(1));
925

926 927 928 929 930
        funcsig!    (($vm) tester_sig = () -> ());
        funcdecl!   (($vm) <tester_sig> $test_name);
        funcdef!    (($vm) <tester_sig> $test_name VERSION $tester_name);

        typedef!    (($vm) type_funcref = mu_funcref($test_sig));
931
        constdef!   (($vm) <type_funcref> const_funcref =
932
            Constant::FuncRef($name.clone()));
933 934 935 936 937 938 939 940 941 942

        // blk_entry
        block!      (($vm, $tester_name) blk_entry);

        consta!     (($vm, $tester_name) const_funcref_local = const_funcref);
        inst!   (($vm, $tester_name) blk_entry_call:
            EXPRCALL (CallConvention::Mu, is_abort: false) const_funcref_local ()
        );

        consta!     (($vm, $tester_name) int64_pass_local = int64_pass);
943

944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
        inst!   (($vm, $tester_name) blk_entry_inst_ret:
             SET_RETVAL int64_pass_local
        );
        inst!   (($vm, $tester_name) blk_entry_inst_exit:
            THREADEXIT
        );

        define_block!   (($vm, $tester_name) blk_entry() {
             blk_entry_call,
             blk_entry_inst_ret,
             blk_entry_inst_exit
        });

        define_func_ver!    (($vm) $tester_name (entry: blk_entry) {
            blk_entry
        });

    };
962

963
    /*
964 965 966 967 968 969 970
    emit_test! ((vm) (coalesce_branch2_moves coalesce_branch2_moves_test1
        coalesce_branch2_moves_test1_v1
        Int,Int,Int,Int,Int,Int,Int,EQ
        sig,
        int64(1u64), int64(1u64), int64(10u64),
        int64(10u64), int64(0u64), int64(0u64),
        int64(2u64))));
971
    */
972 973 974 975 976 977 978 979 980 981 982
    (($vm: expr)
        $name: ident, $test_name: ident, $tester_name: ident,
        $Arg1Type: ident, $Arg2Type: ident, $Arg3Type: ident,
        $Arg4Type: ident, $Arg5Type: ident, $Arg6Type: ident,
        RET $Arg7Type: ident,
        $CMPType: ident,
        $test_sig: ident,
        $ty1: ident($in1: expr), $ty2: ident($in2: expr), $ty3: ident($in3: expr),
        $ty4: ident($in4: expr), $ty5: ident($in5: expr), $ty6: ident($in6: expr),
        RET $ty7: ident($out: expr),
    ) => {
983
        typedef!    (($vm) int1  = mu_int(1));
984 985 986
        typedef!    (($vm) int32t  = mu_int(32));
        constdef!   (($vm) <int32t> int64_pass = Constant::Int(0));
        constdef!   (($vm) <int32t> int64_fail = Constant::Int(1));
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
        constdef!   (($vm) <$ty1> arg_0 = Constant::$Arg1Type($in1));
        constdef!   (($vm) <$ty2> arg_1 = Constant::$Arg2Type($in2));
        constdef!   (($vm) <$ty3> arg_2 = Constant::$Arg3Type($in3));
        constdef!   (($vm) <$ty4> arg_3 = Constant::$Arg4Type($in4));
        constdef!   (($vm) <$ty5> arg_4 = Constant::$Arg5Type($in5));
        constdef!   (($vm) <$ty6> arg_5 = Constant::$Arg6Type($in6));
        constdef!   (($vm) <$ty7> arg_6 = Constant::$Arg7Type($out));

        funcsig!    (($vm) tester_sig = () -> ());
        funcdecl!   (($vm) <tester_sig> $test_name);
        funcdef!    (($vm) <tester_sig> $test_name VERSION $tester_name);

        ssa!    (($vm, $tester_name) <$ty1> a);
        ssa!    (($vm, $tester_name) <$ty2> b);
        ssa!    (($vm, $tester_name) <$ty3> c);
        ssa!    (($vm, $tester_name) <$ty4> d);
        ssa!    (($vm, $tester_name) <$ty5> e);
        ssa!    (($vm, $tester_name) <$ty6> f);

        typedef!    (($vm) type_funcref = mu_funcref($test_sig));
1007
        constdef!   (($vm) <type_funcref> const_funcref =
1008
            Constant::FuncRef($name.clone()));
1009 1010 1011 1012 1013 1014 1015 1016

        // blk_entry
        consta!     (($vm, $tester_name) arg_0_local = arg_0);
        consta!     (($vm, $tester_name) arg_1_local = arg_1);
        consta!     (($vm, $tester_name) arg_2_local = arg_2);
        consta!     (($vm, $tester_name) arg_3_local = arg_3);
        consta!     (($vm, $tester_name) arg_4_local = arg_4);
        consta!     (($vm, $tester_name) arg_5_local = arg_5);
1017

1018 1019 1020 1021 1022
        block!      (($vm, $tester_name) blk_entry);

        consta!     (($vm, $tester_name) const_funcref_local = const_funcref);
        ssa!    (($vm, $tester_name) <$ty7> result);
        inst!   (($vm, $tester_name) blk_entry_call:
1023 1024 1025
            result = EXPRCALL (CallConvention::Mu, is_abort: false)
                const_funcref_local (arg_0_local, arg_1_local, arg_2_local,
                    arg_3_local, arg_4_local, arg_5_local)
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
        );

        consta!     (($vm, $tester_name) arg_6_local = arg_6);
        consta!     (($vm, $tester_name) int64_pass_local = int64_pass);
        consta!     (($vm, $tester_name) int64_fail_local = int64_fail);
        ssa!    (($vm, $tester_name) <int1> cmp_res);
        inst!   (($vm, $tester_name) blk_entry_cmp:
            cmp_res = CMPOP (CmpOp::$CMPType) result arg_6_local
        );

1036
        ssa!    (($vm, $tester_name) <int32t> blk_entry_ret);
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
        inst!   (($vm, $tester_name) blk_entry_inst_select:
            blk_entry_ret = SELECT cmp_res int64_pass_local int64_fail_local
        );

        inst!   (($vm, $tester_name) blk_entry_inst_ret:
             SET_RETVAL blk_entry_ret
        );
        inst!   (($vm, $tester_name) blk_entry_inst_exit:
            THREADEXIT
        );

1048
        define_block!   (($vm, $tester_name) blk_entry() {
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
             blk_entry_call,
             blk_entry_cmp,
             blk_entry_inst_select,
             blk_entry_inst_ret,
             blk_entry_inst_exit
        });

        define_func_ver!    (($vm) $tester_name (entry: blk_entry) {
            blk_entry
        });

    };
1061

1062
    /*
1063 1064
    emit_test!      ((vm) (add_twice add_twice_test1 add_twice_test1_v1
        Int,Int,Int,Int,EQ (add_twice_sig, int64(1u64), int64(1u64), int64(1u64), int64(3u64))));
1065
    */
1066 1067 1068 1069 1070 1071 1072 1073 1074
    (($vm: expr)
        $name: ident, $test_name: ident, $tester_name: ident,
        $Arg1Type: ident, $Arg2Type: ident, $Arg3Type: ident,
        RET $Arg4Type: ident,
        $CMPType: ident,
        $test_sig: ident,
        $ty1: ident($in1: expr), $ty2: ident($in2: expr), $ty3: ident($in3: expr),
        RET $ty4: ident($out: expr),
    ) => {
1075
        typedef!    (($vm) int1  = mu_int(1));
1076 1077 1078
        typedef!    (($vm) int32t  = mu_int(32));
        constdef!   (($vm) <int32t> int64_pass = Constant::Int(0));
        constdef!   (($vm) <int32t> int64_fail = Constant::Int(1));
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
        constdef!   (($vm) <$ty1> arg_0 = Constant::$Arg1Type($in1));
        constdef!   (($vm) <$ty2> arg_1 = Constant::$Arg2Type($in2));
        constdef!   (($vm) <$ty2> arg_2 = Constant::$Arg3Type($in3));
        constdef!   (($vm) <$ty3> arg_3 = Constant::$Arg3Type($out));

        funcsig!    (($vm) tester_sig = () -> ());
        funcdecl!   (($vm) <tester_sig> $test_name);
        funcdef!    (($vm) <tester_sig> $test_name VERSION $tester_name);

        ssa!    (($vm, $tester_name) <$ty1> a);
        ssa!    (($vm, $tester_name) <$ty2> b);
        ssa!    (($vm, $tester_name) <$ty3> c);

        typedef!    (($vm) type_funcref = mu_funcref($test_sig));
1093
        constdef!   (($vm) <type_funcref> const_funcref =
1094
            Constant::FuncRef($name.clone()));
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105

        // blk_entry
        consta!     (($vm, $tester_name) arg_0_local = arg_0);
        consta!     (($vm, $tester_name) arg_1_local = arg_1);
        consta!     (($vm, $tester_name) arg_2_local = arg_2);

        block!      (($vm, $tester_name) blk_entry);

        consta!     (($vm, $tester_name) const_funcref_local = const_funcref);
        ssa!    (($vm, $tester_name) <$ty4> result);
        inst!   (($vm, $tester_name) blk_entry_call:
1106 1107
            result = EXPRCALL (CallConvention::Mu, is_abort: false)
                const_funcref_local (arg_0_local, arg_1_local, arg_2_local)
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
        );

        consta!     (($vm, $tester_name) arg_3_local = arg_3);
        consta!     (($vm, $tester_name) int64_pass_local = int64_pass);
        consta!     (($vm, $tester_name) int64_fail_local = int64_fail);
        ssa!    (($vm, $tester_name) <int1> cmp_res);
        inst!   (($vm, $tester_name) blk_entry_cmp:
            cmp_res = CMPOP (CmpOp::$CMPType) result arg_3_local
        );

1118
        ssa!    (($vm, $tester_name) <int32t> blk_entry_ret);
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
        inst!   (($vm, $tester_name) blk_entry_inst_select:
            blk_entry_ret = SELECT cmp_res int64_pass_local int64_fail_local
        );

        inst!   (($vm, $tester_name) blk_entry_inst_ret:
             SET_RETVAL blk_entry_ret
        );
        inst!   (($vm, $tester_name) blk_entry_inst_exit:
            THREADEXIT
        );

1130
        define_block!   (($vm, $tester_name) blk_entry() {
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
             blk_entry_call,
             blk_entry_cmp,
             blk_entry_inst_select,
             blk_entry_inst_ret,
             blk_entry_inst_exit
        });

        define_func_ver!    (($vm) $tester_name (entry: blk_entry) {
            blk_entry
        });

    };
}

/*
This macro is used as follows:
1 - for add_simple:
    compile_and_run_test! (add, tester_mu);
*/
macro_rules! build_and_run_test {
    ($test_name: ident, $tester_name: ident) => {
        VM::start_logging_trace();

        let vm = Arc::new($test_name());

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

        let func_id = vm.id_of(stringify!($tester_name));
        {
            let funcs = vm.funcs().read().unwrap();
            let func = funcs.get(&func_id).unwrap().read().unwrap();
            let func_vers = vm.func_vers().read().unwrap();
1163 1164 1165 1166 1167
            let mut func_ver = func_vers
                .get(&func.cur_ver.unwrap())
                .unwrap()
                .write()
                .unwrap();
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178

            compiler.compile(&mut func_ver);
        }

        vm.set_primordial_thread(func_id, true, vec![]);

        let func_id = vm.id_of(stringify!($test_name));
        {
            let funcs = vm.funcs().read().unwrap();
            let func = funcs.get(&func_id).unwrap().read().unwrap();
            let func_vers = vm.func_vers().read().unwrap();
1179 1180 1181 1182 1183
            let mut func_ver = func_vers
                .get(&func.cur_ver.unwrap())
                .unwrap()
                .write()
                .unwrap();
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204

            compiler.compile(&mut func_ver);
        }

        backend::emit_context(&vm);
        aot::run_test(&vm, stringify!($test_name), stringify!($tester_name));
    };
    // When test name in mu IR is different from the name of rust function \
    // which creates the vm
    ($test_name: ident, $tester_name: ident, $fnc_name: ident) => {
        VM::start_logging_trace();

        let vm = Arc::new($fnc_name());

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

        let func_id = vm.id_of(stringify!($tester_name));
        {
            let funcs = vm.funcs().read().unwrap();
            let func = funcs.get(&func_id).unwrap().read().unwrap();
            let func_vers = vm.func_vers().read().unwrap();
1205 1206 1207 1208 1209
            let mut func_ver = func_vers
                .get(&func.cur_ver.unwrap())
                .unwrap()
                .write()
                .unwrap();
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220

            compiler.compile(&mut func_ver);
        }

        vm.set_primordial_thread(func_id, true, vec![]);

        let func_id = vm.id_of(stringify!($test_name));
        {
            let funcs = vm.funcs().read().unwrap();
            let func = funcs.get(&func_id).unwrap().read().unwrap();
            let func_vers = vm.func_vers().read().unwrap();
1221 1222 1223 1224 1225
            let mut func_ver = func_vers
                .get(&func.cur_ver.unwrap())
                .unwrap()
                .write()
                .unwrap();
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246

            compiler.compile(&mut func_ver);
        }

        backend::emit_context(&vm);
        aot::run_test(&vm, stringify!($test_name), stringify!($tester_name));
    };
    // When the testee has one dependent function
    //
    ($test_name: ident AND $dep_name: ident, $tester_name: ident) => {
        VM::start_logging_trace();

        let vm = Arc::new($test_name());

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

        let func_id = vm.id_of(stringify!($tester_name));
        {
            let funcs = vm.funcs().read().unwrap();
            let func = funcs.get(&func_id).unwrap().read().unwrap();
            let func_vers = vm.func_vers().read().unwrap();
1247 1248 1249 1250 1251
            let mut func_ver = func_vers
                .get(&func.cur_ver.unwrap())
                .unwrap()
                .write()
                .unwrap();
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262

            compiler.compile(&mut func_ver);
        }

        vm.set_primordial_thread(func_id, true, vec![]);

        let func_id = vm.id_of(stringify!($test_name));
        {
            let funcs = vm.funcs().read().unwrap();
            let func = funcs.get(&func_id).unwrap().read().unwrap();
            let func_vers = vm.func_vers().read().unwrap();
1263 1264 1265 1266 1267
            let mut func_ver = func_vers
                .get(&func.cur_ver.unwrap())
                .unwrap()
                .write()
                .unwrap();
1268 1269 1270 1271 1272 1273 1274 1275

            compiler.compile(&mut func_ver);
        }
        let func_id = vm.id_of(stringify!($dep_name));
        {
            let funcs = vm.funcs().read().unwrap();
            let func = funcs.get(&func_id).unwrap().read().unwrap();
            let func_vers = vm.func_vers().read().unwrap();
1276 1277 1278 1279 1280
            let mut func_ver = func_vers
                .get(&func.cur_ver.unwrap())
                .unwrap()
                .write()
                .unwrap();
1281 1282 1283 1284 1285

            compiler.compile(&mut func_ver);
        }

        backend::emit_context(&vm);
1286 1287 1288 1289 1290
        aot::run_test_2f(
            &vm,
            stringify!($test_name),
            stringify!($dep_name),
            stringify!($tester_name),
1291
        );
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
    };
    ($test_name: ident AND $dep_name: ident, $tester_name: ident, $fnc_name: ident) => {
        VM::start_logging_trace();

        let vm = Arc::new($fnc_name());

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

        let func_id = vm.id_of(stringify!($tester_name));
        {
            let funcs = vm.funcs().read().unwrap();
            let func = funcs.get(&func_id).unwrap().read().unwrap();
            let func_vers = vm.func_vers().read().unwrap();
1305 1306 1307 1308 1309
            let mut func_ver = func_vers
                .get(&func.cur_ver.unwrap())
                .unwrap()
                .write()
                .unwrap();
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320

            compiler.compile(&mut func_ver);
        }

        vm.set_primordial_thread(func_id, true, vec![]);

        let func_id = vm.id_of(stringify!($test_name));
        {
            let funcs = vm.funcs().read().unwrap();
            let func = funcs.get(&func_id).unwrap().read().unwrap();
            let func_vers = vm.func_vers().read().unwrap();
1321