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.7% of users enabled 2FA.

Commit 78c4c439 authored by Eduardo Souza's avatar Eduardo Souza
Browse files

Set up explicit environment and stores.

parent 8e4268ab
......@@ -346,10 +346,19 @@ impl Exp for VarExp {
e @ Err(_) => return e,
};
let var_name = vm.generate_name(name.name.clone());
let var = fun_ver.new_ssa(
MuEntityHeader::named(vm.vm.next_id(), Arc::new(var_name)),
vm.ty.clone(),
);
vm.vm.set_name(var.as_entity());
let current_block = vm.blocks_map.get_mut(&vm.curr_block).unwrap();
current_block.env.push(var.clone_value());
old_bindings.push(current_block.scoped_vars.remove(name.name.as_str()));
current_block.scoped_vars.insert(name.name.clone(), start_exp);
old_bindings.push(current_block.store.remove(name.name.as_str()));
current_block.store.insert(name.name.clone(), start_exp);
}
let body_value = match self.body_exp.code_gen_mu(vm, fun_ver) {
......@@ -363,11 +372,11 @@ impl Exp for VarExp {
let (name, _) = var;
let current_block = vm.blocks_map.get_mut(&vm.curr_block).unwrap();
current_block.scoped_vars.remove(name.name.as_str());
current_block.store.remove(name.name.as_str());
match old_bindings_iter.next() {
Some(Some(value)) => {
current_block.scoped_vars.insert(name.name.clone(), value);
current_block.store.insert(name.name.clone(), value);
}
_ => (),
}
......@@ -514,12 +523,26 @@ impl Exp for ForExp {
// branch to preloop
let current_block = vm.blocks_map.get_mut(&vm.curr_block).unwrap();
let current_block_args = (&current_block.args).clone();
let current_block_form_args = (&current_block.form_args).clone();
let current_block_scoped_vars = (&current_block.scoped_vars).clone();
let mut args_preloop = current_block_args.clone();
let mut form_args_preloop = current_block_form_args.clone();
let mut scoped_vars_preloop = current_block_scoped_vars.clone();
let current_block_env = (&current_block.env).clone();
let current_block_form_args = (&current_block.store).clone();
let mut args_preloop = vec![];
let mut store_before_preloop = LinkedHashMap::new();
let mut args_before_preloop = vec![];
let mut preloop_store = LinkedHashMap::new();
for (arg_name, value) in current_block_form_args.iter() {
let form_arg = fun_ver.new_ssa(
MuEntityHeader::named(vm.vm.next_id(), Arc::new(arg_name.clone())),
vm.ty.clone(),
);
vm.vm.set_name(form_arg.as_entity());
preloop_store.insert(arg_name.clone(), form_arg.clone());
store_before_preloop.insert(arg_name.clone(), form_arg.clone());
args_preloop.push(form_arg.clone_value());
args_before_preloop.push(form_arg.clone_value());
}
let mut args = vec![];
for (_, arg) in &current_block_form_args {
......@@ -530,8 +553,7 @@ impl Exp for ForExp {
let branch_preloop = create_branch1_instr(vm.vm.next_id(), fun_ver, args, &preloop_block);
current_block.instr.push(branch_preloop);
form_args_preloop.insert(phi_node_name.clone(), phi_node.clone());
scoped_vars_preloop.insert(phi_node_name.clone(), phi_node.clone());
preloop_store.insert(phi_node_name.clone(), phi_node.clone());
args_preloop.push(phi_node.clone_value());
vm.blocks_map.insert(
......@@ -539,26 +561,25 @@ impl Exp for ForExp {
Mu_Block {
block: preloop_block.clone(),
instr: vec![],
args: args_preloop.clone(),
form_args: form_args_preloop.clone(),
scoped_vars : scoped_vars_preloop.clone(),
block_actual_args: args_preloop.clone(),
args_mapping: preloop_store.clone(),
store: preloop_store.clone(),
env: args_preloop.clone(),
},
);
vm.curr_block = preloop_block_id;
let current_block = vm.blocks_map.get_mut(&vm.curr_block).unwrap();
let old_value = current_block.scoped_vars.remove(self.var.name());
let old_value = current_block.store.remove(self.var.name());
current_block
.scoped_vars
.store
.insert(self.var.name().clone(), phi_node.clone());
// build end loop check
let end_exp = match self.end_exp.code_gen_mu(vm, fun_ver) {
Ok(r) => r,
Err(err) => {
return Err(err)
},
Err(err) => return Err(err),
};
let name = format!("const_{}", 0);
......@@ -604,9 +625,10 @@ impl Exp for ForExp {
Mu_Block {
block: loop_block.clone(),
instr: vec![],
args: args_preloop.clone(),
form_args: form_args_preloop.clone(),
scoped_vars : scoped_vars_preloop.clone()
block_actual_args: args_preloop.clone(),
args_mapping: preloop_store.clone(),
store: preloop_store.clone(),
env: args_preloop.clone(),
},
);
......@@ -623,14 +645,15 @@ impl Exp for ForExp {
Mu_Block {
block: after_loop_block.clone(),
instr: vec![],
args: current_block_args.clone(),
form_args: current_block_form_args.clone(),
scoped_vars: current_block_scoped_vars.clone()
block_actual_args: args_before_preloop.clone(),
args_mapping: store_before_preloop.clone(),
store: store_before_preloop.clone(),
env: args_before_preloop.clone(),
},
);
let mut ops = vec![loop_check];
for arg in current_block_form_args.values() {
for arg in preloop_store.values() {
ops.push(arg.clone())
}
ops.push(phi_node.clone());
......@@ -642,8 +665,8 @@ impl Exp for ForExp {
vm.vm.next_id(),
fun_ver,
ops,
form_args_true.form_args.clone(),
form_args_false.form_args.clone(),
form_args_true.store.clone(),
form_args_false.store.clone(),
&loop_block,
&after_loop_block,
);
......@@ -677,9 +700,11 @@ impl Exp for ForExp {
v: Instruction_::BinOp(FAdd, 0, 1),
});
let current_block = vm.blocks_map.get_mut(&loop_block_id).unwrap();
let mut ops = vec![];
for arg in current_block_form_args.values() {
ops.push(arg.clone())
for (name, arg) in store_before_preloop {
let new_arg = current_block.store.get(name.as_str()).unwrap();
ops.push(new_arg.clone())
}
ops.push(var.clone());
......@@ -693,7 +718,7 @@ impl Exp for ForExp {
let preloop_block = vm.blocks_map.get_mut(&preloop_block_id).unwrap();
match old_value {
Some(v) => {
preloop_block.scoped_vars.insert(self.var.name.clone(), v);
preloop_block.store.insert(self.var.name.clone(), v);
}
None => (),
}
......@@ -869,8 +894,8 @@ impl Exp for ConditionalExp {
current_block.instr.push(ifcond_inst.clone());
// create branch instr
let current_block_args = (&current_block.args).clone();
let current_block_form_args = (&current_block.form_args).clone();
let current_block_args = (&current_block.block_actual_args).clone();
let current_block_form_args = (&current_block.args_mapping).clone();
let mut ops = vec![var.clone()];
for arg in current_block_form_args.values() {
......@@ -894,9 +919,10 @@ impl Exp for ConditionalExp {
Mu_Block {
block: then_branch_block,
instr: vec![],
args: current_block_args.clone(),
form_args: current_block_form_args.clone(),
scoped_vars: current_block_form_args.clone()
block_actual_args: current_block_args.clone(),
args_mapping: current_block_form_args.clone(),
store: current_block_form_args.clone(),
env: current_block_args.clone(),
},
);
......@@ -924,9 +950,10 @@ impl Exp for ConditionalExp {
Mu_Block {
block: else_branch_block,
instr: vec![],
args: current_block_args.clone(),
form_args: current_block_form_args.clone(),
scoped_vars: current_block_form_args.clone()
block_actual_args: current_block_args.clone(),
args_mapping: current_block_form_args.clone(),
store: current_block_form_args.clone(),
env: current_block_args.clone(),
},
);
......@@ -951,8 +978,8 @@ impl Exp for ConditionalExp {
current_block.instr.push(branch_if_cont_inst);
// process if_cont block
let mut args_if_cont = current_block_args.clone();
let mut form_args_if_cont = current_block_form_args.clone();
let mut args_if_cont = current_block.env.clone();
let mut form_args_if_cont = current_block.store.clone();
let phi_node_name = vm.generate_name(String::from("phi_node"));
let phi_node = fun_ver.new_ssa(
......@@ -969,9 +996,10 @@ impl Exp for ConditionalExp {
Mu_Block {
block: if_cont_branch_block,
instr: vec![],
args: args_if_cont.clone(),
form_args: form_args_if_cont.clone(),
scoped_vars: form_args_if_cont.clone()
block_actual_args: args_if_cont.clone(),
args_mapping: form_args_if_cont.clone(),
store: form_args_if_cont.clone(),
env: args_if_cont.clone(),
},
);
......@@ -1146,9 +1174,7 @@ impl Exp for BinaryExp {
let rhs = match self.rhs.code_gen_mu(mu_vm, function_version) {
Ok(r) => r,
Err(err) => {
return Err(err)
},
Err(err) => return Err(err),
};
let var = function_version.new_ssa(
......@@ -1185,9 +1211,7 @@ impl Exp for BinaryExp {
'=' => {
let current_block = mu_vm.blocks_map.get_mut(&mu_vm.curr_block).unwrap();
let var_name = &*lhs.name();
current_block
.scoped_vars
.insert(var_name.clone(), rhs.clone());
current_block.store.insert(var_name.clone(), rhs.clone());
return Ok(rhs);
}
_ => unimplemented!(),
......@@ -1259,7 +1283,7 @@ impl Exp for Var {
);
Ok(var)
}
None => unimplemented!()
None => unimplemented!(),
}
}
......@@ -1270,7 +1294,7 @@ impl Exp for Var {
) -> Result<P<TreeNode>, String> {
let current_block = mu_vm.blocks_map.get_mut(&mu_vm.curr_block).unwrap();
let r = current_block.scoped_vars.get(self.name.as_str());
let r = current_block.store.get(self.name.as_str());
match r {
Some(v) => {
// let mem_loc = v.clone();
......
......@@ -14,8 +14,6 @@ pub mod mu_generator;
fn main() {
const USE_LLVM: bool = false;
const USE_AOT_MU_LLVM: bool = false;
const USE_ALLOCA: bool = false;
const USE_NEW: bool = false;
// process chars into tokens
let (filename, tokens) = lexer::read_file();
......@@ -33,8 +31,7 @@ fn main() {
}
} else {
unsafe {
values =
mu_generator::code_gen(filename, nodes, USE_AOT_MU_LLVM, false, USE_ALLOCA, USE_NEW);
values = mu_generator::code_gen(filename, nodes, USE_AOT_MU_LLVM, false);
}
}
......
......@@ -42,12 +42,10 @@ pub struct Mu_VM {
pub whitelist: Vec<MuID>,
pub last_runnable_function: Option<MuEntityHeader>,
pub name_generator: HashMap<String, i32>,
pub use_alloca: bool,
pub use_new: bool,
}
impl Mu_VM {
pub fn new(use_llvm_aot: bool, debug: bool, use_alloca: bool, use_new: bool) -> Mu_VM {
pub fn new(use_llvm_aot: bool, debug: bool) -> Mu_VM {
let debug_opt = "--log-level=debug --emit-debug-info ";
let generate_llvm_opt = "--generate-llvm";
let mut opts = String::from("init_mu");
......@@ -93,8 +91,6 @@ impl Mu_VM {
whitelist,
last_runnable_function: None,
name_generator: HashMap::new(),
use_alloca,
use_new,
}
}
......@@ -117,9 +113,10 @@ impl Mu_VM {
pub struct Mu_Block {
pub block: Block,
pub instr: Vec<P<TreeNode>>,
pub args: Vec<P<Value>>,
pub form_args: LinkedHashMap<String, P<TreeNode>>,
pub scoped_vars : LinkedHashMap<String, P<TreeNode>>,
pub block_actual_args: Vec<P<Value>>,
pub args_mapping: LinkedHashMap<String, P<TreeNode>>,
pub store: LinkedHashMap<String, P<TreeNode>>,
pub env: Vec<P<Value>>,
}
pub unsafe fn code_gen(
......@@ -127,10 +124,8 @@ pub unsafe fn code_gen(
nodes: ASTNodes,
use_llvm_aot: bool,
debug: bool,
use_alloca: bool,
use_new: bool,
) -> Vec<Result<f64, String>> {
let mut vm = Mu_VM::new(use_llvm_aot, debug, use_alloca, use_new);
let mut vm = Mu_VM::new(use_llvm_aot, debug);
let mut results = vec![];
for prototype in nodes.prototypes {
......@@ -274,20 +269,6 @@ impl Mu_code_gen for Function {
mu_vm.ty.clone(),
);
mu_vm.vm.set_name(form_arg.as_entity());
if mu_vm.use_alloca {
let (loc_var, instr) =
create_mu_alloca(mu_vm, &mut function_version, mu_vm.ty.clone(), arg.clone());
alloca_instr.push(instr);
alloca_instr.push(create_mu_store(
mu_vm,
&mut function_version,
form_arg.clone(),
loc_var.clone(),
mu_vm.ty.clone(),
));
}
form_args.insert(arg.clone(), form_arg.clone());
args.push(form_arg.clone_value());
}
......@@ -302,9 +283,10 @@ impl Mu_code_gen for Function {
Mu_Block {
block: blk_entry,
instr: alloca_instr,
args,
form_args : form_args.clone(),
scoped_vars : form_args.clone()
block_actual_args: args.clone(),
args_mapping: form_args.clone(),
store: form_args.clone(),
env: args.clone(),
},
);
......@@ -510,7 +492,7 @@ pub fn create_int_cmp_op_instruction_eq(
fn set_block_contents(blocks_map: &mut LinkedHashMap<MuID, Mu_Block>) {
for (block_id, mu_block) in blocks_map.iter_mut() {
mu_block.block.content = Some(BlockContent {
args: mu_block.args.clone(),
args: mu_block.block_actual_args.clone(),
exn_arg: None,
body: mu_block.instr.clone(),
keepalives: None,
......
......@@ -18,16 +18,12 @@ fn test_single_exp() {
nodes.clone(),
false,
false,
false,
false,
);
mu_llvm_values = mu_generator::code_gen(
String::from("test_single_exp\0"),
nodes.clone(),
true,
false,
false,
false,
);
}
......@@ -52,16 +48,12 @@ fn test_multiple_exps() {
nodes.clone(),
false,
false,
false,
false,
);
mu_llvm_values = mu_generator::code_gen(
String::from("test_multiple_exps\0"),
nodes.clone(),
true,
false,
false,
false,
);
}
......@@ -86,16 +78,12 @@ fn test_function_call() {
nodes.clone(),
false,
false,
false,
false,
);
mu_llvm_values = mu_generator::code_gen(
String::from("test_function_call\0"),
nodes.clone(),
true,
false,
false,
false,
);
}
......@@ -121,16 +109,12 @@ fn test_extern_function_call() {
nodes.clone(),
false,
false,
false,
false,
);
mu_llvm_values = mu_generator::code_gen(
String::from("test_extern_function_call\0"),
nodes.clone(),
true,
false,
false,
false,
);
}
......@@ -155,17 +139,9 @@ fn test_simple_if() {
nodes.clone(),
false,
false,
false,
false,
);
mu_llvm_values = mu_generator::code_gen(
String::from("test_simple_if\0"),
nodes.clone(),
true,
false,
false,
false,
);
mu_llvm_values =
mu_generator::code_gen(String::from("test_simple_if\0"), nodes.clone(), true, false);
}
let expected: Vec<Result<f64, String>> = vec![Ok(1.0)];
......@@ -190,16 +166,12 @@ fn test_simple_if_else_case() {
nodes.clone(),
false,
false,
false,
false,
);
mu_llvm_values = mu_generator::code_gen(
String::from("test_simple_if_else_case\0"),
nodes.clone(),
true,
false,
false,
false,
);
}
......@@ -224,17 +196,9 @@ fn test_fibonacci() {
nodes.clone(),
false,
false,
false,
false,
);
mu_llvm_values = mu_generator::code_gen(
String::from("test_fibonacci\0"),
nodes.clone(),
true,
false,
false,
false,
);
mu_llvm_values =
mu_generator::code_gen(String::from("test_fibonacci\0"), nodes.clone(), true, false);
}
let expected: Vec<Result<f64, String>> = vec![Ok(55.0)];
......@@ -259,16 +223,12 @@ fn test_for_loop_no_mutable() {
nodes.clone(),
false,
false,
false,
false,
);
mu_llvm_values = mu_generator::code_gen(
String::from("test_for_loop_no_mutable\0"),
nodes.clone(),
true,
false,
false,
false,
);
}
......@@ -296,16 +256,12 @@ fn test_mutable_assignment_binary_op() {
nodes.clone(),
false,
false,
false,
false,
);
mu_llvm_values = mu_generator::code_gen(
String::from("test_mutable_assignment_binary_op"),
nodes.clone(),
true,
false,
false,
false,
);
}
......@@ -333,16 +289,12 @@ fn test_mutable_var_plus_one_function_with_assignment_expr() {
nodes.clone(),
false,
false,
false,
false,
);
mu_llvm_values = mu_generator::code_gen(
String::from("test_mutable_var_assignment_exp"),
nodes.clone(),
true,
false,
false,
false,
);
}
......@@ -358,23 +310,23 @@ fn test_mutable_pow_function() {
let tokens = lexer::tokenize(String::from(input));
let nodes = parser::parse_program(tokens);
let llvm_values;
// let mu_values;
// let mu_llvm_values;
let mu_values;
let mu_llvm_values;
unsafe {
llvm_values =
generator::code_gen(String::from("test_mutable_pow_function\0"), nodes.clone());
// mu_values = mu_generator::code_gen(
// String::from("test_mutable_var_assignment_exp"),
// nodes.clone(),
// false,
// false,
// );
// mu_llvm_values = mu_generator::code_gen(
// String::from("test_mutable_var_assignment_exp"),
// nodes.clone(),
// true,
// false,
// );
mu_values = mu_generator::code_gen(
String::from("test_mutable_var_assignment_exp"),
nodes.clone(),
false,
false,
);
mu_llvm_values = mu_generator::code_gen(
String::from("test_mutable_var_assignment_exp"),
nodes.clone(),
true,
false,
);
}
let expected: Vec<Result<f64, String>> = vec![Ok(81.0), Ok(16.0)];
......@@ -389,27 +341,27 @@ fn test_mutable_fibonacci_iter() {
let tokens = lexer::tokenize(String::from(input));
let nodes = parser::parse_program(tokens);
let llvm_values;
// let mu_values;
// let mu_llvm_values;
let mu_values;
let mu_llvm_values;
unsafe {
llvm_values =
generator::code_gen(String::from("test_mutable_fibonacci_iter\0"), nodes.clone());
// mu_values = mu_generator::code_gen(
// String::from("test_mutable_var_assignment_exp"),
// nodes.clone(),
// false,
// false,
// );
// mu_llvm_values = mu_generator::code_gen(
// String::from("test_mutable_var_assignment_exp"),
// nodes.clone(),
// true,
// false,
// );
mu_values = mu_generator::code_gen(
String::from("test_mutable_var_assignment_exp"),
nodes.clone(),
false,
false,
);
mu_llvm_values = mu_generator::code_gen(
String::from("test_mutable_var_assignment_exp"),
nodes.clone(),
true,
false,
);
}
let expected: Vec<Result<f64, String>> = vec![Ok(55.0)];
assert_eq!(llvm_values, expected);