WARNING! Access to this system is limited to authorised users only.
Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(1) Obtain access to data without authority. -Penalty 2 years imprisonment.
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.2% of users enabled 2FA.

ir_macros.rs 24 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)]

qinsoon's avatar
qinsoon committed
17
macro_rules! typedef {
qinsoon's avatar
qinsoon committed
18
    // int, floating point
qinsoon's avatar
qinsoon committed
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());
qinsoon's avatar
qinsoon committed
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
qinsoon's avatar
qinsoon committed
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());
qinsoon's avatar
qinsoon committed
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());
qinsoon's avatar
qinsoon committed
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
qinsoon's avatar
qinsoon committed
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());
qinsoon's avatar
qinsoon committed
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());
    };
qinsoon's avatar
qinsoon committed
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());
qinsoon's avatar
qinsoon committed
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
    }
}

qinsoon's avatar
qinsoon committed
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());
qinsoon's avatar
qinsoon committed
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());
qinsoon's avatar
qinsoon committed
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());
qinsoon's avatar
qinsoon committed
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
        }));
qinsoon's avatar
qinsoon committed
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());
qinsoon's avatar
qinsoon committed
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
        });
qinsoon's avatar
qinsoon committed
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());
qinsoon's avatar
qinsoon committed
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());
    }
}

qinsoon's avatar
qinsoon committed
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![],
qinsoon's avatar
qinsoon committed
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)
        });
    };

qinsoon's avatar
qinsoon committed
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()],
qinsoon's avatar
qinsoon committed
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)) => {
qinsoon's avatar
qinsoon committed
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()],
qinsoon's avatar
qinsoon committed
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
            }
        });
    };

qinsoon's avatar
qinsoon committed
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)) => {
qinsoon's avatar
qinsoon committed
300 301 302
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  None,
303
            ops:    vec![$loc.clone(), $val.clone()],
qinsoon's avatar
qinsoon committed
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)) => {
qinsoon's avatar
qinsoon committed
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()],
qinsoon's avatar
qinsoon committed
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) => {
qinsoon's avatar
qinsoon committed
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()],
qinsoon's avatar
qinsoon committed
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
            }
        });
    };

qinsoon's avatar
qinsoon committed
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()),*],
qinsoon's avatar
qinsoon committed
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()
qinsoon's avatar
qinsoon committed
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), *)) => {
qinsoon's avatar
qinsoon committed
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

qinsoon's avatar
qinsoon committed
583
    // RET
584
    (($vm: expr, $fv: ident) $name: ident: RET ($($val: ident), +)) => {
qinsoon's avatar
qinsoon committed
585 586 587
        let $name = $fv.new_inst(Instruction{
            hdr:    MuEntityHeader::unnamed($vm.next_id()),
            value:  None,
588
            ops:    vec![$($val.clone()), *],
qinsoon's avatar
qinsoon committed
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()
qinsoon's avatar
qinsoon committed
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
}