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.

Commit b8187a00 authored by qinsoon's avatar qinsoon
Browse files

add macros to construct IR in tests

currently only translate one old test into new one with macros,
the macro set is going to grow as it needs to. The macros are
located under tests. atm I do not intend to expose it for clients.
parent d69dcb0c
macro_rules! typedef {
(($vm: expr) $name: ident = mu_int($len: expr)) => {
let $name = $vm.declare_type($vm.next_id(), MuType_::int($len));
$vm.set_name($name.as_entity(), Mu(stringify!($name)));
};
(($vm: expr) $name: ident = mu_ref($ty: ident)) => {
let $name = $vm.declare_type($vm.next_id(), MuType_::muref($ty.clone()));
$vm.set_name($name.as_entity(), Mu(stringify!($name)));
};
(($vm: expr) $name: ident = mu_iref($ty: ident)) => {
let $name = $vm.declare_type($vm.next_id(), MuType_::iref($ty.clone()));
$vm.set_name($name.as_entity(), Mu(stringify!($name)));
};
(($vm: expr) $name: ident = mu_struct($($ty: ident), *)) => {
let $name = $vm.declare_type($vm.next_id(), MuType_::mustruct(Mu(stringify!($name)), vec![$($ty.clone()),*]));
$vm.set_name($name.as_entity(), Mu(stringify!($name)));
};
}
macro_rules! constdef {
(($vm: expr) <$ty: ident> $name: ident = $val: expr) => {
let $name = $vm.declare_const($vm.next_id(), $ty.clone(), $val);
$vm.set_name($name.as_entity(), Mu(stringify!($name)));
}
}
macro_rules! funcsig {
(($vm: expr) $name: ident = ($($arg_ty: ident),*) -> ($($ret_ty: ident),*)) => {
let $name = $vm.declare_func_sig($vm.next_id(), vec![$($ret_ty.clone()),*], vec![$($arg_ty.clone()),*]);
$vm.set_name($name.as_entity(), Mu(stringify!($name)));
}
}
macro_rules! funcdecl {
(($vm: expr) <$sig: ident> $name: ident) => {
let func = MuFunction::new($vm.next_id(), $sig.clone());
$vm.set_name(func.as_entity(), Mu(stringify!($name)));
let $name = func.id();
$vm.declare_func(func);
}
}
macro_rules! funcdef {
(($vm: expr) <$sig: ident> $func: ident VERSION $version: ident) => {
let mut $version = MuFunctionVersion::new($vm.next_id(), $func, $sig.clone());
$vm.set_name($version.as_entity(), Mu(stringify!($version)));
}
}
macro_rules! define_func_ver {
(($vm: expr) $fv: ident (entry: $entry: ident){$($blk: ident), *}) => {
$fv.define(FunctionContent{
entry: $entry.id(),
blocks: hashmap!{
$($blk.id() => $blk),*
}
});
$vm.define_func_version($fv);
}
}
macro_rules! block {
(($vm: expr, $fv: ident) $name: ident) => {
let mut $name = Block::new($vm.next_id());
$vm.set_name($name.as_entity(), Mu(stringify!($name)));
}
}
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
});
}
}
macro_rules! ssa {
(($vm: expr, $fv: ident) <$ty: ident> $name: ident) => {
let $name = $fv.new_ssa($vm.next_id(), $ty.clone());
$vm.set_name($name.as_entity(), Mu(stringify!($name)));
}
}
macro_rules! consta {
(($vm: expr, $fv: ident) $name: ident = $c: ident) => {
let $name = $fv.new_constant($c.clone());
}
}
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()]),
ops: RwLock::new(vec![]),
v: Instruction_::New($ty.clone())
});
};
// 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()]),
ops: RwLock::new(vec![$op.clone()]),
v: Instruction_::GetIRef(0)
});
};
// GETFIELDIREF
(($vm: expr, $fv: ident) $name: ident: $value: ident = GETFIELDIREF $op: ident (is_ptr: $is_ptr: expr, index: $index: expr)) => {
let $name = $fv.new_inst(Instruction{
hdr: MuEntityHeader::unnamed($vm.next_id()),
value: Some(vec![$value.clone_value()]),
ops: RwLock::new(vec![$op.clone()]),
v: Instruction_::GetFieldIRef {
is_ptr: $is_ptr,
base: 0,
index: $index
}
});
};
// STORE
(($vm: expr, $fv: ident) $name: ident: STORE $loc: ident $val: ident (is_ptr: $is_ptr: expr, order: $order: expr)) => {
let $name = $fv.new_inst(Instruction{
hdr: MuEntityHeader::unnamed($vm.next_id()),
value: None,
ops: RwLock::new(vec![$loc.clone(), $val.clone()]),
v: Instruction_::Store {
is_ptr: $is_ptr,
order: $order,
mem_loc: 0,
value: 1
}
});
};
// LOAD
(($vm: expr, $fv: ident) $name: ident: $value: ident = LOAD $loc: ident (is_ptr: $is_ptr: expr, order: $order: expr)) => {
let $name = $fv.new_inst(Instruction{
hdr: MuEntityHeader::unnamed($vm.next_id()),
value: Some(vec![$value.clone_value()]),
ops: RwLock::new(vec![$loc.clone()]),
v: Instruction_::Load {
is_ptr: $is_ptr,
order: $order,
mem_loc: 0
}
});
};
// BINOP
(($vm: expr, $fv: ident) $name: ident: $value: ident = BINOP ($op: expr) $op1: ident $op2: ident) => {
let $name = $fv.new_inst(Instruction{
hdr: MuEntityHeader::unnamed($vm.next_id()),
value: Some(vec![$value.clone_value()]),
ops: RwLock::new(vec![$op1.clone(), $op2.clone()]),
v: Instruction_::BinOp($op, 0, 1)
});
};
// 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,
ops: RwLock::new(vec![$($arg.clone()),*]),
v: Instruction_::Branch1(Destination{
target: $dest.id(),
args: {
let mut i =0;
vec![$($arg.clone()),*].iter().map(|x| {let ret = DestArg::Normal(i); i+=1; ret}).collect()
}
})
});
};
// RET
(($vm: expr, $fv: ident) $name: ident: RET ($($val: ident), *)) => {
let $name = $fv.new_inst(Instruction{
hdr: MuEntityHeader::unnamed($vm.next_id()),
value: None,
ops: RwLock::new(vec![$($val.clone()), *]),
v: Instruction_::Return({
let mut i = 0;
vec![$($val.clone()), *].iter().map(|x| {let ret = i; i+= 1; ret}).collect()
})
});
};
}
\ No newline at end of file
......@@ -4,6 +4,9 @@ extern crate log;
#[macro_use]
extern crate maplit;
#[macro_use]
mod ir_macros;
mod test_ir;
mod test_compiler;
mod test_runtime;
......
......@@ -18,7 +18,7 @@ use test_compiler::test_call::gen_ccall_exit;
fn test_struct() {
VM::start_logging_trace();
let vm = Arc::new(struct_insts());
let vm = Arc::new(struct_insts_macro());
let compiler = Compiler::new(CompilerPolicy::default(), vm.clone());
......@@ -45,6 +45,105 @@ fn test_struct() {
assert!(ret_code == 1);
}
// this IR construction function is a replicate of struct_insts() with macros
pub fn struct_insts_macro() -> VM {
let vm = VM::new();
typedef! ((vm) int64 = mu_int(64));
typedef! ((vm) struct_point = mu_struct(int64, int64));
typedef! ((vm) ref_point = mu_ref(struct_point));
typedef! ((vm) iref_point = mu_iref(struct_point));
typedef! ((vm) iref_int64 = mu_iref(int64));
constdef!((vm) <int64> int64_0 = Constant::Int(0));
constdef!((vm) <int64> int64_1 = Constant::Int(1));
funcsig! ((vm) noparam_noret_sig = () -> ());
funcdecl!((vm) <noparam_noret_sig> struct_insts);
funcdef! ((vm) <noparam_noret_sig> struct_insts VERSION struct_insts_v1);
// blk entry
block! ((vm, struct_insts_v1) blk_entry);
ssa! ((vm, struct_insts_v1) <ref_point> blk_entry_a);
inst! ((vm, struct_insts_v1) blk_entry_inst0:
blk_entry_a = NEW <struct_point>
);
ssa! ((vm, struct_insts_v1) <iref_point> blk_entry_iref_a);
inst! ((vm, struct_insts_v1) blk_entry_inst1:
blk_entry_iref_a = GETIREF blk_entry_a
);
ssa! ((vm, struct_insts_v1) <iref_int64> blk_entry_iref_x);
inst! ((vm, struct_insts_v1) blk_entry_inst2:
blk_entry_iref_x = GETFIELDIREF blk_entry_iref_a (is_ptr: false, index: 0)
);
consta! ((vm, struct_insts_v1) int64_1_local = int64_1);
inst! ((vm, struct_insts_v1) blk_entry_inst3:
STORE blk_entry_iref_x int64_1_local (is_ptr: false, order: MemoryOrder::Relaxed)
);
block! ((vm, struct_insts_v1) blk_check);
inst! ((vm, struct_insts_v1) blk_entry_branch:
BRANCH blk_check (blk_entry_a)
);
define_block! ((vm, struct_insts_v1) blk_entry() {
blk_entry_inst0, blk_entry_inst1, blk_entry_inst2, blk_entry_inst3, blk_entry_branch
});
// blk check
ssa! ((vm, struct_insts_v1) <ref_point> blk_check_a);
ssa! ((vm, struct_insts_v1) <iref_point> blk_check_iref_a);
inst! ((vm, struct_insts_v1) blk_check_inst0:
blk_check_iref_a = GETIREF blk_check_a
);
ssa! ((vm, struct_insts_v1) <iref_int64> blk_check_iref_x);
inst! ((vm, struct_insts_v1) blk_check_inst1:
blk_check_iref_x = GETFIELDIREF blk_check_iref_a (is_ptr: false, index: 0)
);
ssa! ((vm, struct_insts_v1) <int64> blk_check_x);
inst! ((vm, struct_insts_v1) blk_check_inst2:
blk_check_x = LOAD blk_check_iref_x (is_ptr: false, order: MemoryOrder::Relaxed)
);
ssa! ((vm, struct_insts_v1) <iref_int64> blk_check_iref_y);
inst! ((vm, struct_insts_v1) blk_check_inst3:
blk_check_iref_y = GETFIELDIREF blk_check_iref_a (is_ptr: false, index: 1)
);
ssa! ((vm, struct_insts_v1) <int64> blk_check_y);
inst! ((vm, struct_insts_v1) blk_check_inst4:
blk_check_y = LOAD blk_check_iref_y (is_ptr: false, order: MemoryOrder::Relaxed)
);
ssa! ((vm, struct_insts_v1) <int64> blk_check_res);
inst! ((vm, struct_insts_v1) blk_check_inst5:
blk_check_res = BINOP (BinOp::Add) blk_check_x blk_check_y
);
let blk_check_ccall = gen_ccall_exit(blk_check_res.clone(), &mut struct_insts_v1, &vm);
inst! ((vm, struct_insts_v1) blk_check_ret:
RET (blk_check_res)
);
define_block! ((vm, struct_insts_v1) blk_check(blk_check_a) {
blk_check_inst0, blk_check_inst1, blk_check_inst2, blk_check_inst3, blk_check_inst4, blk_check_inst5, blk_check_ccall, blk_check_ret
});
define_func_ver! ((vm) struct_insts_v1 (entry: blk_entry) {blk_entry, blk_check});
vm
}
#[allow(dead_code)]
pub fn struct_insts() -> VM {
let vm = VM::new();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment