ir_macros.rs 48.1 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) => {
qinsoon's avatar
qinsoon committed
105 106
        let $name = $vm.declare_const(MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
                                      $ty.clone(), $val);
107
        $vm.set_name($name.as_entity());
108 109 110
    }
}

111 112
macro_rules! globaldef {
    (($vm: expr) <$ty: ident> $name: ident) => {
qinsoon's avatar
qinsoon committed
113 114
        let $name = $vm.declare_global(MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
                                       $ty.clone());
115
        $vm.set_name($name.as_entity());
116 117 118
    }
}

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

macro_rules! funcdecl {
    (($vm: expr) <$sig: ident> $name: ident) => {
qinsoon's avatar
qinsoon committed
130 131
        let func = MuFunction::new(MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
                                   $sig.clone());
132
        $vm.set_name(func.as_entity());
133 134 135 136 137 138 139
        let $name = func.id();
        $vm.declare_func(func);
    }
}

macro_rules! funcdef {
    (($vm: expr) <$sig: ident> $func: ident VERSION $version: ident) => {
qinsoon's avatar
qinsoon committed
140 141 142
        let mut $version = MuFunctionVersion::new(MuEntityHeader::named($vm.next_id(),
                                                                        Mu(stringify!($version))),
                                                  $func, $sig.clone());
143
        $vm.set_name($version.as_entity());
144 145 146 147 148
    }
}

macro_rules! define_func_ver {
    (($vm: expr) $fv: ident (entry: $entry: ident){$($blk: ident), *}) => {
149 150 151 152 153
        $fv.define(FunctionContent::new($entry.id(), {
            let mut ret = LinkedHashMap::new();
            $ (ret.insert($blk.id(), $blk); )*
            ret
        }));
154 155 156 157 158 159 160

        $vm.define_func_version($fv);
    }
}

macro_rules! block {
    (($vm: expr, $fv: ident) $name: ident) => {
161 162
        let mut $name = Block::new(MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))));
        $vm.set_name($name.as_entity());
163 164 165 166 167 168 169 170 171 172 173
    }
}

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
        });
174 175
    };

qinsoon's avatar
qinsoon committed
176 177
    (($vm: expr, $fv: ident) $name: ident ($($arg: ident), *)
     [$exn_arg: ident] {$($inst: ident), *}) => {
178 179 180 181 182 183
        $name.content = Some(BlockContent{
            args: vec![$($arg.clone_value()), *],
            exn_arg: Some($exn_arg.clone_value()),
            body: vec![$($inst), *],
            keepalives: None
        });
184 185 186 187 188
    }
}

macro_rules! ssa {
    (($vm: expr, $fv: ident) <$ty: ident> $name: ident) => {
qinsoon's avatar
qinsoon committed
189 190
        let $name = $fv.new_ssa(MuEntityHeader::named($vm.next_id(), Mu(stringify!($name))),
                                                      $ty.clone());
191
        $vm.set_name($name.as_entity());
192 193 194
    }
}

195 196 197 198 199 200
macro_rules! machine_reg {
    (($vm: expr, $fv: ident) $name: ident = $mreg: expr) => {
        let $name = $fv.new_machine_reg($mreg.clone());
    }
}

201 202 203 204 205 206
macro_rules! consta {
    (($vm: expr, $fv: ident) $name: ident = $c: ident) => {
        let $name = $fv.new_constant($c.clone());
    }
}

207 208 209 210 211 212
macro_rules! global {
    (($vm: expr, $fv: ident) $name: ident = $g: ident) => {
        let $name = $fv.new_global($g.clone());
    }
}

213 214 215 216 217 218
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()]),
219
            ops:    vec![],
220 221 222 223
            v:      Instruction_::New($ty.clone())
        });
    };

224 225 226 227 228
    // 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()]),
229
            ops:    vec![$len.clone()],
230 231 232 233
            v:      Instruction_::NewHybrid($ty.clone(), 0)
        });
    };

234 235 236 237 238
    // 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()]),
239
            ops:    vec![$op.clone()],
240 241 242 243 244
            v:      Instruction_::GetIRef(0)
        });
    };

    // GETFIELDIREF
qinsoon's avatar
qinsoon committed
245 246
    (($vm: expr, $fv: ident) $name: ident: $value: ident = GETFIELDIREF $op: ident
     (is_ptr: $is_ptr: expr, index: $index: expr)) => {
247 248 249
        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 256 257 258
            v:      Instruction_::GetFieldIRef {
                        is_ptr: $is_ptr,
                        base: 0,
                        index: $index
            }
        });
    };

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

qinsoon's avatar
qinsoon committed
274
    // GETVARPARTIREF
qinsoon's avatar
qinsoon committed
275 276
    (($vm: expr, $fv: ident) $name: ident: $value: ident = GETVARPARTIREF $op: ident
     (is_ptr: $is_ptr: expr)) => {
qinsoon's avatar
qinsoon committed
277 278 279
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  Some(vec![$value.clone_value()]),
280
            ops:    vec![$op.clone()],
qinsoon's avatar
qinsoon committed
281 282 283 284 285 286 287 288
            v:      Instruction_::GetVarPartIRef {
                        is_ptr: $is_ptr,
                        base: 0
            }
        });
    };

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

303
    // STORE
qinsoon's avatar
qinsoon committed
304 305
    (($vm: expr, $fv: ident) $name: ident: STORE $loc: ident $val: ident
     (is_ptr: $is_ptr: expr, order: $order: expr)) => {
306 307 308
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  None,
309
            ops:    vec![$loc.clone(), $val.clone()],
310 311 312 313 314 315 316 317 318 319
            v:      Instruction_::Store {
                        is_ptr: $is_ptr,
                        order: $order,
                        mem_loc: 0,
                        value: 1
            }
        });
    };

    // LOAD
qinsoon's avatar
qinsoon committed
320 321
    (($vm: expr, $fv: ident) $name: ident: $value: ident = LOAD $loc: ident
     (is_ptr: $is_ptr: expr, order: $order: expr)) => {
322 323 324
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  Some(vec![$value.clone_value()]),
325
            ops:    vec![$loc.clone()],
326 327 328 329 330 331 332 333 334
            v:      Instruction_::Load {
                        is_ptr: $is_ptr,
                        order: $order,
                        mem_loc: 0
            }
        });
    };

    // BINOP
qinsoon's avatar
qinsoon committed
335 336
    (($vm: expr, $fv: ident) $name: ident: $value: ident =
     BINOP ($op: expr) $op1: ident $op2: ident) => {
337 338 339
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  Some(vec![$value.clone_value()]),
340
            ops:    vec![$op1.clone(), $op2.clone()],
341 342 343 344
            v:      Instruction_::BinOp($op, 0, 1)
        });
    };

qinsoon's avatar
qinsoon committed
345
    // BINOP with status
qinsoon's avatar
qinsoon committed
346 347
    (($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
348 349 350
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  Some(vec![$value.clone_value(), $($flag.clone_value()), *]),
351
            ops:    vec![$op1.clone(), $op2.clone()],
qinsoon's avatar
qinsoon committed
352 353 354 355
            v:      Instruction_::BinOpWithStatus($op, $flags, 0, 1)
        });
    };

qinsoon's avatar
qinsoon committed
356
    // CMPOP
qinsoon's avatar
qinsoon committed
357 358
    (($vm: expr, $fv: ident) $name: ident: $value: ident =
     CMPOP ($op: 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()]),
362
            ops: vec![$op1.clone(), $op2.clone()],
qinsoon's avatar
qinsoon committed
363 364 365 366
            v: Instruction_::CmpOp($op, 0, 1)
        });
    };

367
    // CONVOP
qinsoon's avatar
qinsoon committed
368 369
    (($vm: expr, $fv: ident) $name: ident: $value: ident =
     CONVOP ($operation: expr) <$ty1: ident $ty2: ident> $operand: ident) => {
370 371 372
        let $name = $fv.new_inst(Instruction{
            hdr: MuEntityHeader::unnamed($vm.next_id()),
            value: Some(vec![$value.clone_value()]),
373
            ops: vec![$operand.clone()],
374 375 376 377 378 379 380 381 382
            v: Instruction_::ConvOp{
                operation: $operation,
                from_ty: $ty1.clone(),
                to_ty: $ty2.clone(),
                operand: 0
            }
        });
    };

qinsoon's avatar
qinsoon committed
383
    // SELECT
qinsoon's avatar
qinsoon committed
384 385
    (($vm: expr, $fv: ident) $name: ident: $value: ident =
     SELECT $cond: ident $op_true: ident $op_false:ident) => {
qinsoon's avatar
qinsoon committed
386 387 388
        let $name = $fv.new_inst(Instruction{
            hdr: MuEntityHeader::unnamed($vm.next_id()),
            value: Some(vec![$value.clone_value()]),
389
            ops: vec![$cond.clone(), $op_true.clone(), $op_false.clone()],
qinsoon's avatar
qinsoon committed
390 391 392 393 394 395 396 397
            v: Instruction_::Select{
                cond: 0,
                true_val: 1,
                false_val: 2
            }
        });
    };

398 399 400 401 402
    // 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,
403
            ops:    vec![$($arg.clone()),*],
404 405 406 407
            v:      Instruction_::Branch1(Destination{
                        target: $dest.id(),
                        args: {
                            let mut i =0;
408
                            vec![$($arg.clone()),*].iter().map(|_: &Arc<TreeNode>| {
qinsoon's avatar
qinsoon committed
409 410
                                let ret = DestArg::Normal(i); i+=1; ret
                             }).collect()
411 412 413 414 415
                        }
            })
        });
    };

qinsoon's avatar
qinsoon committed
416 417 418 419 420 421 422 423 424 425 426 427 428
    // 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,
429
            ops:    vec![$($op.clone()),*],
qinsoon's avatar
qinsoon committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
            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 {
                        target: $true_dest.id(),
                        args: true_args
                    },
                    false_dest: Destination {
                        target: $false_dest.id(),
                        args: false_args
                    },
                    true_prob: $prob
                }
            }
        });
    };

455
    // EXPRCALL
qinsoon's avatar
qinsoon committed
456 457
    (($vm: expr, $fv: ident) $name: ident: $res: ident =
     EXPRCALL ($cc: expr, is_abort: $is_abort: expr) $func: ident ($($val: ident), *)) => {
458
        let ops = vec![$func.clone(), $($val.clone()), *];
qinsoon's avatar
qinsoon committed
459 460 461 462
        let ops_len = ops.len();
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  Some(vec![$res.clone_value()]),
463
            ops:    ops,
qinsoon's avatar
qinsoon committed
464 465 466 467 468 469 470 471 472 473
            v:      Instruction_::ExprCall {
                        data: CallData {
                            func: 0,
                            args: (1..ops_len).collect(),
                            convention: $cc
                        },
                        is_abort: $is_abort
                    }
        });
    };
qinsoon's avatar
qinsoon committed
474 475
    (($vm: expr, $fv: ident) $name: ident:
     EXPRCALL ($cc: expr, is_abort: $is_abort: expr) $func: ident ($($val: ident), *)) => {
qinsoon's avatar
[wip]  
qinsoon committed
476 477 478 479 480
        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![]),
481
            ops:    ops,
qinsoon's avatar
[wip]  
qinsoon committed
482 483 484 485 486 487 488 489 490 491
            v:      Instruction_::ExprCall {
                        data: CallData {
                            func: 0,
                            args: (1..ops_len).collect(),
                            convention: $cc
                        },
                        is_abort: $is_abort
                    }
        });
    };
492 493

    // EXPRCCALL
qinsoon's avatar
qinsoon committed
494 495
    (($vm: expr, $fv: ident) $name: ident: $res: ident =
     EXPRCCALL ($cc: expr, is_abort: $is_abort: expr) $func: ident ($($val: ident), *)) => {
496 497 498 499 500
        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()]),
501
            ops:    ops,
502 503 504 505 506 507 508 509 510 511
            v:      Instruction_::ExprCCall {
                        data: CallData {
                            func: 0,
                            args: (1..ops_len).collect(),
                            convention: $cc
                        },
                        is_abort: $is_abort
                    }
        });
    };
qinsoon's avatar
qinsoon committed
512 513
    (($vm: expr, $fv: ident) $name: ident:
    EXPRCCALL ($cc: expr, is_abort: $is_abort: expr) $func: ident ($($val: ident), *)) => {
514 515 516 517 518
        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![]),
519
            ops:    ops,
520 521 522 523 524 525 526 527 528 529 530
            v:      Instruction_::ExprCCall {
                        data: CallData {
                            func: 0,
                            args: (1..ops_len).collect(),
                            convention: $cc
                        },
                        is_abort: $is_abort
                    }
        });
    };

531
    // CALL (1 return result)
qinsoon's avatar
qinsoon committed
532 533
    (($vm: expr, $fv: ident) $name: ident: $res: ident =
     CALL ($($op: ident), *) FUNC($func: expr) ($args: expr) $cc: expr,
534 535 536 537 538
                      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()]),
539
            ops  : vec![$($op.clone()),*],
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
            v    : Instruction_::Call {
                data: CallData {
                    func: $func,
                    args: $args,
                    convention: $cc
                },
                resume: ResumptionData {
                    normal_dest: Destination {
                        target: $norm_dest.id(),
                        args  : $norm_args
                    },
                    exn_dest: Destination {
                        target: $exc_dest.id(),
                        args  : $exc_args
                    }
                }
            }
        });
    };
559 560 561 562 563 564 565 566
    // 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,
567
            ops  : vec![$($op.clone()),*],
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
            v    : Instruction_::Call {
                data: CallData {
                    func: $func,
                    args: $args,
                    convention: $cc
                },
                resume: ResumptionData {
                    normal_dest: Destination {
                        target: $norm_dest.id(),
                        args  : $norm_args
                    },
                    exn_dest: Destination {
                        target: $exc_dest.id(),
                        args  : $exc_args
                    }
                }
            }
        });
    };

qinsoon's avatar
qinsoon committed
588

589
    // RET
590
    (($vm: expr, $fv: ident) $name: ident: RET ($($val: ident), +)) => {
591 592 593
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  None,
594
            ops:    vec![$($val.clone()), *],
595 596
            v:      Instruction_::Return({
                        let mut i = 0;
qinsoon's avatar
qinsoon committed
597
                        vec![$($val.clone()), *].iter().map(|_| {let ret = i; i+= 1; ret}).collect()
598 599 600
                    })
        });
    };
601 602 603 604 605
    // RET (no value)
    (($vm: expr, $fv: ident) $name: ident: RET) => {
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  None,
606
            ops:    vec![],
607 608 609
            v:      Instruction_::Return(vec![])
        });
    };
610 611 612 613 614 615

    // THREADEXIT
    (($vm: expr, $fv: ident) $name: ident: THREADEXIT) => {
        let $name = $fv.new_inst(Instruction{
            hdr: MuEntityHeader::unnamed($vm.next_id()),
            value: None,
616
            ops: vec![],
617 618 619 620
            v: Instruction_::ThreadExit
        });
    };

621 622 623 624 625
    // THROW
    (($vm: expr, $fv: ident) $name: ident: THROW $op: ident) => {
        let $name = $fv.new_inst(Instruction{
            hdr: MuEntityHeader::unnamed($vm.next_id()),
            value: None,
626
            ops: vec![$op.clone()],
627 628 629 630
            v: Instruction_::Throw(0)
        });
    };

631 632 633 634 635
    // PRINTHEX
    (($vm: expr, $fv: ident) $name: ident: PRINTHEX $val: ident) => {
        let $name = $fv.new_inst(Instruction{
            hdr: MuEntityHeader::unnamed($vm.next_id()),
            value: None,
636
            ops: vec![$val.clone()],
637 638
            v: Instruction_::PrintHex(0)
        });
639 640 641 642 643 644 645 646 647 648 649
    };

    // 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)
        });
    };
650 651 652 653 654 655 656 657 658 659

    // 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)
        });
    };
660
}
661 662 663 664 665 666


/**************************************
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):
667 668
      emit_test!      ((vm) (add add_test1 add_test1_v1
        III (sig, int64(22), int64(27), int64(49))));
669 670 671 672 673 674 675 676 677 678 679 680 681
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 {
    /*
682 683 684 685 686 687 688
    emit_test! ((vm)
        udiv udiv_test1 udiv_test1_v1
        Int,Int, Int
        EQ
        udiv_sig
        int64(22), int64(4), int64(5)
    );
689
    */
690 691 692 693 694 695 696
    (($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),
    ) => {
697
        typedef!    (($vm) int1  = mu_int(1));
698 699 700
        typedef!    (($vm) int32t  = mu_int(32));
        constdef!   (($vm) <int32t> int64_pass = Constant::Int(0));
        constdef!   (($vm) <int32t> int64_fail = Constant::Int(1));
701 702 703 704 705 706 707 708 709 710 711 712
        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));
713 714
        constdef!   (($vm) <type_funcref> const_funcref =
            Constant::FuncRef($vm.id_of(stringify!($name))));
715 716 717 718 719 720 721 722 723 724

        // 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:
725 726
            result = EXPRCALL (CallConvention::Mu, is_abort: false)
                const_funcref_local (f64_0_local, f64_1_local)
727 728 729 730 731 732 733 734 735 736
        );

        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
        );

737
        ssa!    (($vm, $tester_name) <int32t> blk_entry_ret);
738 739 740 741 742 743 744 745 746 747 748
        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
        );

749
        define_block!   (($vm, $tester_name) blk_entry() {
750 751 752 753 754 755 756 757 758 759 760 761 762
             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
        });

    };
    /*
763 764 765 766 767 768
    emit_test! ((vm) (test_sitofp, test_sitofp_test1, test_sitofp_test1_v1)
        Int -> Double
        EQ
        sig
        int64(-1i64) -> double(-1f64)
    );
769
    */
770 771 772 773 774 775 776
    (($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),
    ) => {
777
        typedef!    (($vm) int1  = mu_int(1));
778 779 780
        typedef!    (($vm) int32t  = mu_int(32));
        constdef!   (($vm) <int32t> int64_pass = Constant::Int(0));
        constdef!   (($vm) <int32t> int64_fail = Constant::Int(1));
781 782 783 784 785 786 787 788
        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));
789 790
        constdef!   (($vm) <type_funcref> const_funcref =
            Constant::FuncRef($vm.id_of(stringify!($name))));
791 792
        // blk_entry
        consta!     (($vm, $tester_name) f64_0_local = f64_0);
793

794 795 796 797 798
        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:
799 800
            result = EXPRCALL (CallConvention::Mu, is_abort: false)
                const_funcref_local (f64_0_local)
801 802 803 804 805 806 807 808 809 810
        );

        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
        );

811
        ssa!    (($vm, $tester_name) <int32t> blk_entry_ret);
812 813 814 815 816 817 818 819 820 821
        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
        );
822
        define_block!   (($vm, $tester_name) blk_entry() {
823 824 825 826 827 828 829 830 831 832 833 834 835
             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
        });

    };
    /*
836 837
    emit_test!      ((vm) (pass_1arg_by_stack pass_1arg_by_stack_test1 pass_1arg_by_stack_test1_v1
        Int,EQ (sig, int64(1u64))));
838
    */
839 840 841 842 843 844 845
    (($vm: expr)
        $name: ident, $test_name: ident, $tester_name: ident,
        RET $Arg1Type: ident,
        $CMPType: ident,
        $test_sig: ident,
        RET $ty1: ident($in1: expr),
    ) => {
846
        typedef!    (($vm) int1  = mu_int(1));
847 848 849
        typedef!    (($vm) int32t  = mu_int(32));
        constdef!   (($vm) <int32t> int64_pass = Constant::Int(0));
        constdef!   (($vm) <int32t> int64_fail = Constant::Int(1));
850 851 852 853 854 855
        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));
856 857
        constdef!   (($vm) <type_funcref> const_funcref =
            Constant::FuncRef($vm.id_of(stringify!($name))));
858 859 860

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

862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
        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
        );

877
        ssa!    (($vm, $tester_name) <int32t> blk_entry_ret);
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
        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
        });

    };
902

903
    /*
904 905
    emit_test!      ((vm) (catch_exception catch_exception_test1 catch_exception_test1_v1
        (catch_exception_sig)));
906
    */
907 908 909 910
    (($vm: expr)
        $name: ident, $test_name: ident, $tester_name: ident,
        $test_sig: ident,
    ) => {
911
        typedef!    (($vm) int1  = mu_int(1));
912 913 914
        typedef!    (($vm) int32t  = mu_int(32));
        constdef!   (($vm) <int32t> int64_pass = Constant::Int(0));
        constdef!   (($vm) <int32t> int64_fail = Constant::Int(1));
915

916 917 918 919 920
        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));
921 922
        constdef!   (($vm) <type_funcref> const_funcref =
            Constant::FuncRef($vm.id_of(stringify!($name))));
923 924 925 926 927 928 929 930 931 932

        // 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);
933

934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
        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
        });

    };
952

953
    /*
954 955 956 957 958 959 960
    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))));
961
    */
962 963 964 965 966 967 968 969 970 971 972
    (($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),
    ) => {
973
        typedef!    (($vm) int1  = mu_int(1));
974 975 976
        typedef!    (($vm) int32t  = mu_int(32));
        constdef!   (($vm) <int32t> int64_pass = Constant::Int(0));
        constdef!   (($vm) <int32t> int64_fail = Constant::Int(1));
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
        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));
997 998
        constdef!   (($vm) <type_funcref> const_funcref =
            Constant::FuncRef($vm.id_of(stringify!($name))));
999 1000 1001 1002 1003 1004 1005 1006

        // 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);
1007

1008 1009 1010 1011 1012
        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:
1013 1014 1015
            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)
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
        );

        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
        );

1026
        ssa!    (($vm, $tester_name) <int32t> blk_entry_ret);
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
        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
        );

1038
        define_block!   (($vm, $tester_name) blk_entry() {
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
             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
        });

    };
1051

1052
    /*
1053 1054
    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))));
1055
    */
1056 1057 1058 1059 1060 1061 1062 1063 1064
    (($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),
    ) => {
1065
        typedef!    (($vm) int1  = mu_int(1));
1066 1067 1068
        typedef!    (($vm) int32t  = mu_int(32));
        constdef!   (($vm) <int32t> int64_pass = Constant::Int(0));
        constdef!   (($vm) <int32t> int64_fail = Constant::Int(1));
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
        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));
1083 1084
        constdef!   (($vm) <type_funcref> const_funcref =
            Constant::FuncRef($vm.id_of(stringify!($name))));
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095

        // 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:
1096 1097
            result = EXPRCALL (CallConvention::Mu, is_abort: false)
                const_funcref_local (arg_0_local, arg_1_local, arg_2_local)
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
        );

        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
        );

1108
        ssa!    (($vm, $tester_name) <int32t> blk_entry_ret);
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
        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
        );

1120
        define_block!   (($vm, $tester_name) blk_entry() {
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 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 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
             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();
            let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

            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();
            let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

            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();
            let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

            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();
            let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

            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();
            let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

            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();
            let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

            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();
            let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

            compiler.compile(&mut func_ver);
        }

        backend::emit_context(&vm);
1248 1249 1250 1251
        aot::run_test_2f(&vm, stringify!($test_name),
                              stringify!($dep_name),
                              stringify!($tester_name)
        );
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 1278 1279 1280
    };
    ($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();
            let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

            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();
            let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

            compiler.compile(&mut func_ver);
        }
1281

1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
        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();
            let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

            compiler.compile(&mut func_ver);
        }

        backend::emit_context(&vm);
1293 1294 1295 1296
        aot::run_test_2f(&vm, stringify!($test_name),
                              stringify!($dep_name),
                              stringify!($tester_name)
        );
1297
    };
1298
}