|
|
|
@ -330,11 +330,44 @@ fn match_number(s: &str) -> Number {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn compile_biop(
|
|
|
|
|
op: &str,
|
|
|
|
|
lst: &mut Vec<Ast>,
|
|
|
|
|
vars: &mut Vec<(String, String)>,
|
|
|
|
|
constants: &mut Vec<(String, String)>,
|
|
|
|
|
types: &mut Vec<String>,
|
|
|
|
|
counter: &mut i32,
|
|
|
|
|
stack: &mut Vec<String>,
|
|
|
|
|
out: &mut Vec<(Option<String>, String)>,
|
|
|
|
|
) {
|
|
|
|
|
assert!(lst.len() == 2);
|
|
|
|
|
let rhs = lst.pop().unwrap();
|
|
|
|
|
let lhs = lst.pop().unwrap();
|
|
|
|
|
compile_ast_ssa(lhs, vars, constants, types, counter, stack, out);
|
|
|
|
|
compile_ast_ssa(rhs, vars, constants, types, counter, stack, out);
|
|
|
|
|
let rhs_id = stack.pop().unwrap();
|
|
|
|
|
let lhs_id = stack.pop().unwrap();
|
|
|
|
|
let id = String::from(counter.to_string());
|
|
|
|
|
*counter += 1;
|
|
|
|
|
out.push((
|
|
|
|
|
Some(id.clone()),
|
|
|
|
|
format!(
|
|
|
|
|
"{} {} {} {}",
|
|
|
|
|
op,
|
|
|
|
|
fix_name(&String::from("f32")),
|
|
|
|
|
fix_name(&lhs_id),
|
|
|
|
|
fix_name(&rhs_id),
|
|
|
|
|
),
|
|
|
|
|
));
|
|
|
|
|
stack.push(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn compile_ast_ssa(
|
|
|
|
|
ast: Ast,
|
|
|
|
|
vars: &mut Vec<(String, String)>,
|
|
|
|
|
constants: &mut Vec<(String, String)>,
|
|
|
|
|
counter: &mut Box<i32>,
|
|
|
|
|
types: &mut Vec<String>,
|
|
|
|
|
counter: &mut i32,
|
|
|
|
|
stack: &mut Vec<String>,
|
|
|
|
|
out: &mut Vec<(Option<String>, String)>,
|
|
|
|
|
) {
|
|
|
|
@ -352,8 +385,8 @@ pub fn compile_ast_ssa(
|
|
|
|
|
assert!(lst.len() == 2);
|
|
|
|
|
let ptr = lst.pop().unwrap();
|
|
|
|
|
let val = lst.pop().unwrap();
|
|
|
|
|
compile_ast_ssa(ptr, vars, constants, counter, stack, out);
|
|
|
|
|
compile_ast_ssa(val, vars, constants, counter, stack, out);
|
|
|
|
|
compile_ast_ssa(ptr, vars, constants, types, counter, stack, out);
|
|
|
|
|
compile_ast_ssa(val, vars, constants, types, counter, stack, out);
|
|
|
|
|
let val_id = stack.pop().unwrap();
|
|
|
|
|
let ptr_id = stack.pop().unwrap();
|
|
|
|
|
out.push((
|
|
|
|
@ -361,6 +394,18 @@ pub fn compile_ast_ssa(
|
|
|
|
|
format!("OpStore {} {}", fix_name(&val_id), fix_name(&ptr_id)),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
"/" => {
|
|
|
|
|
compile_biop("OpFDiv", &mut lst, vars, constants, types, counter, stack, out);
|
|
|
|
|
}
|
|
|
|
|
"*" => {
|
|
|
|
|
compile_biop("OpFMul", &mut lst, vars, constants, types, counter, stack, out);
|
|
|
|
|
}
|
|
|
|
|
"+" => {
|
|
|
|
|
compile_biop("OpFAdd", &mut lst, vars, constants, types, counter, stack, out);
|
|
|
|
|
}
|
|
|
|
|
"-" => {
|
|
|
|
|
compile_biop("OpFSub", &mut lst, vars, constants, types, counter, stack, out);
|
|
|
|
|
}
|
|
|
|
|
s => {
|
|
|
|
|
panic!(
|
|
|
|
|
"Unknown function: {} with params {:#?} in context:\n{:#?}",
|
|
|
|
@ -382,7 +427,7 @@ pub fn compile_ast_ssa(
|
|
|
|
|
contains = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if contains {
|
|
|
|
|
if !contains {
|
|
|
|
|
constants.push((key.clone(), format!("OpConstant %i32 {}", i.to_string())));
|
|
|
|
|
}
|
|
|
|
|
stack.push(key);
|
|
|
|
@ -395,9 +440,17 @@ pub fn compile_ast_ssa(
|
|
|
|
|
contains = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if contains {
|
|
|
|
|
if !contains {
|
|
|
|
|
constants.push((key.clone(), format!("OpConstant %f32 {}", f.to_string())));
|
|
|
|
|
}
|
|
|
|
|
for t in types.iter() {
|
|
|
|
|
if t == "f32" {
|
|
|
|
|
contains = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !contains {
|
|
|
|
|
types.push("f32".to_string());
|
|
|
|
|
}
|
|
|
|
|
stack.push(key);
|
|
|
|
|
}
|
|
|
|
|
Number::NotANumber => {
|
|
|
|
|