ir_macros.rs 48 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! consta {
    (($vm: expr, $fv: ident) $name: ident = $c: ident) => {
        let $name = $fv.new_constant($c.clone());
    }
}

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

207 208 209 210 211 212
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()]),
213
            ops:    vec![],
214 215 216 217
            v:      Instruction_::New($ty.clone())
        });
    };

218 219 220 221 222
    // 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()]),
223
            ops:    vec![$len.clone()],
224 225 226 227
            v:      Instruction_::NewHybrid($ty.clone(), 0)
        });
    };

228 229 230 231 232
    // 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()]),
233
            ops:    vec![$op.clone()],
234 235 236 237 238
            v:      Instruction_::GetIRef(0)
        });
    };

    // GETFIELDIREF
qinsoon's avatar
qinsoon committed
239 240
    (($vm: expr, $fv: ident) $name: ident: $value: ident = GETFIELDIREF $op: ident
     (is_ptr: $is_ptr: expr, index: $index: expr)) => {
241 242 243
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  Some(vec![$value.clone_value()]),
244
            ops:    vec![$op.clone()],
245 246 247 248 249 250 251 252
            v:      Instruction_::GetFieldIRef {
                        is_ptr: $is_ptr,
                        base: 0,
                        index: $index
            }
        });
    };

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

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

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

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

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

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

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

qinsoon's avatar
qinsoon committed
350
    // CMPOP
qinsoon's avatar
qinsoon committed
351 352
    (($vm: expr, $fv: ident) $name: ident: $value: ident =
     CMPOP ($op: expr) $op1: ident $op2: ident) => {
qinsoon's avatar
qinsoon committed
353 354 355
        let $name = $fv.new_inst(Instruction{
            hdr: MuEntityHeader::unnamed($vm.next_id()),
            value: Some(vec![$value.clone_value()]),
356
            ops: vec![$op1.clone(), $op2.clone()],
qinsoon's avatar
qinsoon committed
357 358 359 360
            v: Instruction_::CmpOp($op, 0, 1)
        });
    };

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

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

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

qinsoon's avatar
qinsoon committed
410 411 412 413 414 415 416 417 418 419 420 421 422
    // 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,
423
            ops:    vec![$($op.clone()),*],
qinsoon's avatar
qinsoon committed
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
            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
                }
            }
        });
    };

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

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

525
    // CALL (1 return result)
qinsoon's avatar
qinsoon committed
526 527
    (($vm: expr, $fv: ident) $name: ident: $res: ident =
     CALL ($($op: ident), *) FUNC($func: expr) ($args: expr) $cc: expr,
528 529 530 531 532
                      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()]),
533
            ops  : vec![$($op.clone()),*],
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
            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
                    }
                }
            }
        });
    };
553 554 555 556 557 558 559 560
    // 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,
561
            ops  : vec![$($op.clone()),*],
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
            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
582

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

    // THREADEXIT
    (($vm: expr, $fv: ident) $name: ident: THREADEXIT) => {
        let $name = $fv.new_inst(Instruction{
            hdr: MuEntityHeader::unnamed($vm.next_id()),
            value: None,
610
            ops: vec![],
611 612 613 614
            v: Instruction_::ThreadExit
        });
    };

615 616 617 618 619
    // THROW
    (($vm: expr, $fv: ident) $name: ident: THROW $op: ident) => {
        let $name = $fv.new_inst(Instruction{
            hdr: MuEntityHeader::unnamed($vm.next_id()),
            value: None,
620
            ops: vec![$op.clone()],
621 622 623 624
            v: Instruction_::Throw(0)
        });
    };

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

    // 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)
        });
    };
644 645 646 647 648 649 650 651 652 653

    // 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)
        });
    };
654
}
655 656 657 658 659 660


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

        // 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:
719 720
            result = EXPRCALL (CallConvention::Mu, is_abort: false)
                const_funcref_local (f64_0_local, f64_1_local)
721 722 723 724 725 726 727 728 729 730
        );

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

731
        ssa!    (($vm, $tester_name) <int32t> blk_entry_ret);
732 733 734 735 736 737 738 739 740 741 742
        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
        );

743
        define_block!   (($vm, $tester_name) blk_entry() {
744 745 746 747 748 749 750 751 752 753 754 755 756
             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
        });

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

788 789 790 791 792
        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:
793 794
            result = EXPRCALL (CallConvention::Mu, is_abort: false)
                const_funcref_local (f64_0_local)
795 796 797 798 799 800 801 802 803 804
        );

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

805
        ssa!    (($vm, $tester_name) <int32t> blk_entry_ret);
806 807 808 809 810 811 812 813 814 815
        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
        );
816
        define_block!   (($vm, $tester_name) blk_entry() {
817 818 819 820 821 822 823 824 825 826 827 828 829
             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
        });

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

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

856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
        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
        );

871
        ssa!    (($vm, $tester_name) <int32t> blk_entry_ret);
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
        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
        });

    };
896

897
    /*
898 899
    emit_test!      ((vm) (catch_exception catch_exception_test1 catch_exception_test1_v1
        (catch_exception_sig)));
900
    */
901 902 903 904
    (($vm: expr)
        $name: ident, $test_name: ident, $tester_name: ident,
        $test_sig: ident,
    ) => {
905
        typedef!    (($vm) int1  = mu_int(1));
906 907 908
        typedef!    (($vm) int32t  = mu_int(32));
        constdef!   (($vm) <int32t> int64_pass = Constant::Int(0));
        constdef!   (($vm) <int32t> int64_fail = Constant::Int(1));
909

910 911 912 913 914
        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));
915 916
        constdef!   (($vm) <type_funcref> const_funcref =
            Constant::FuncRef($vm.id_of(stringify!($name))));
917 918 919 920 921 922 923 924 925 926 927

        // 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);
        consta!     (($vm, $tester_name) int64_fail_local = int64_fail);
928

929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
        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
        });

    };
947

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

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

1003 1004 1005 1006 1007
        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:
1008 1009 1010
            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)
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
        );

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

1021
        ssa!    (($vm, $tester_name) <int32t> blk_entry_ret);
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
        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
        );

1033
        define_block!   (($vm, $tester_name) blk_entry() {
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
             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
        });

    };
1046

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

        // 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:
1091 1092
            result = EXPRCALL (CallConvention::Mu, is_abort: false)
                const_funcref_local (arg_0_local, arg_1_local, arg_2_local)
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
        );

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

1103
        ssa!    (($vm, $tester_name) <int32t> blk_entry_ret);
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
        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
        );

1115
        define_block!   (($vm, $tester_name) blk_entry() {
1116 1117 1118 1119 1120 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
             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);
1243 1244 1245 1246
        aot::run_test_2f(&vm, stringify!($test_name),
                              stringify!($dep_name),
                              stringify!($tester_name)
        );
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
    };
    ($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);
        }
1276

1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
        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);
1288 1289 1290 1291
        aot::run_test_2f(&vm, stringify!($test_name),
                              stringify!($dep_name),
                              stringify!($tester_name)
        );
1292
    };
1293
}