First steps of the block language compiler are done
This commit is contained in:
parent
d9b4dcd984
commit
bf79157f8a
4 changed files with 182 additions and 54 deletions
|
@ -99,6 +99,9 @@ enum BlkASTNode {
|
||||||
lbl: String,
|
lbl: String,
|
||||||
childs: Vec<(Option<String>, BlkASTRef)>,
|
childs: Vec<(Option<String>, BlkASTRef)>,
|
||||||
},
|
},
|
||||||
|
Literal {
|
||||||
|
value: f64,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BlkASTNode {
|
impl BlkASTNode {
|
||||||
|
@ -107,14 +110,16 @@ impl BlkASTNode {
|
||||||
|
|
||||||
if let Some(inp) = inp {
|
if let Some(inp) = inp {
|
||||||
indent_str += &format!("{}<= ", inp);
|
indent_str += &format!("{}<= ", inp);
|
||||||
|
} else {
|
||||||
|
indent_str += "<= ";
|
||||||
}
|
}
|
||||||
|
|
||||||
match self {
|
match self {
|
||||||
BlkASTNode::Root { child } => {
|
BlkASTNode::Root { child } => {
|
||||||
format!("{}* Root\n", indent_str) + &child.borrow().dump(indent + 1, None)
|
format!("{}Root\n", indent_str) + &child.borrow().dump(indent + 1, None)
|
||||||
}
|
}
|
||||||
BlkASTNode::Area { childs } => {
|
BlkASTNode::Area { childs } => {
|
||||||
let mut s = format!("{}* Area\n", indent_str);
|
let mut s = format!("{}Area\n", indent_str);
|
||||||
for c in childs.iter() {
|
for c in childs.iter() {
|
||||||
s += &c.borrow().dump(indent + 1, None);
|
s += &c.borrow().dump(indent + 1, None);
|
||||||
}
|
}
|
||||||
|
@ -126,14 +131,14 @@ impl BlkASTNode {
|
||||||
BlkASTNode::Get { id, use_count, var } => {
|
BlkASTNode::Get { id, use_count, var } => {
|
||||||
format!("{}get '{}' (id={}, use={})\n", indent_str, var, id, use_count)
|
format!("{}get '{}' (id={}, use={})\n", indent_str, var, id, use_count)
|
||||||
}
|
}
|
||||||
|
BlkASTNode::Literal { value } => {
|
||||||
|
format!("{}{}\n", indent_str, value)
|
||||||
|
}
|
||||||
BlkASTNode::Node { id, out, use_count, typ, lbl, childs } => {
|
BlkASTNode::Node { id, out, use_count, typ, lbl, childs } => {
|
||||||
let lbl = if *typ == *lbl { "".to_string() } else { format!("[{}]", lbl) };
|
let lbl = if *typ == *lbl { "".to_string() } else { format!("[{}]", lbl) };
|
||||||
|
|
||||||
let mut s = if let Some(out) = out {
|
let mut s = if let Some(out) = out {
|
||||||
format!(
|
format!("{}{}{} (id={}/{}, use={})\n", indent_str, typ, lbl, id, out, use_count)
|
||||||
"{}{}{} (id={}/{}, use={})\n",
|
|
||||||
indent_str, typ, lbl, id, out, use_count
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
format!("{}{}{} (id={}, use={})\n", indent_str, typ, lbl, id, use_count)
|
format!("{}{}{} (id={}, use={})\n", indent_str, typ, lbl, id, use_count)
|
||||||
};
|
};
|
||||||
|
@ -161,6 +166,14 @@ impl BlkASTNode {
|
||||||
Rc::new(RefCell::new(BlkASTNode::Get { id, var: var.to_string(), use_count: 1 }))
|
Rc::new(RefCell::new(BlkASTNode::Get { id, var: var.to_string(), use_count: 1 }))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_literal(val: &str) -> Result<BlkASTRef, BlkJITCompileError> {
|
||||||
|
if let Ok(value) = val.parse::<f64>() {
|
||||||
|
Ok(Rc::new(RefCell::new(BlkASTNode::Literal { value })))
|
||||||
|
} else {
|
||||||
|
Err(BlkJITCompileError::BadLiteralNumber(val.to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new_node(
|
pub fn new_node(
|
||||||
id: usize,
|
id: usize,
|
||||||
out: Option<String>,
|
out: Option<String>,
|
||||||
|
@ -183,10 +196,17 @@ impl BlkASTNode {
|
||||||
pub enum BlkJITCompileError {
|
pub enum BlkJITCompileError {
|
||||||
UnknownError,
|
UnknownError,
|
||||||
BadTree(ASTNodeRef),
|
BadTree(ASTNodeRef),
|
||||||
|
NoOutputAtIdx(String, usize),
|
||||||
|
ASTMissingOutputLabel(usize),
|
||||||
|
NoTmpVarForOutput(usize, String),
|
||||||
|
BadLiteralNumber(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Block2JITCompiler {
|
pub struct Block2JITCompiler {
|
||||||
id_node_map: HashMap<usize, BlkASTRef>,
|
id_node_map: HashMap<usize, BlkASTRef>,
|
||||||
|
idout_var_map: HashMap<String, String>,
|
||||||
|
lang: Rc<RefCell<BlockLanguage>>,
|
||||||
|
tmpvar_counter: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. compile the weird tree into a graph
|
// 1. compile the weird tree into a graph
|
||||||
|
@ -194,12 +214,25 @@ pub struct Block2JITCompiler {
|
||||||
// - add a use count to each node, so that we know when to make temporary variables
|
// - add a use count to each node, so that we know when to make temporary variables
|
||||||
|
|
||||||
impl Block2JITCompiler {
|
impl Block2JITCompiler {
|
||||||
pub fn new() -> Self {
|
pub fn new(lang: Rc<RefCell<BlockLanguage>>) -> Self {
|
||||||
Self { id_node_map: HashMap::new() }
|
Self { id_node_map: HashMap::new(), idout_var_map: HashMap::new(), lang, tmpvar_counter: 0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn next_tmpvar_name(&mut self, extra: &str) -> String {
|
||||||
|
self.tmpvar_counter += 1;
|
||||||
|
format!("_tmp{}_{}_", self.tmpvar_counter, extra)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn store_idout_var(&mut self, id: usize, out: &str, v: &str) {
|
||||||
|
self.idout_var_map.insert(format!("{}/{}", id, out), v.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_var_for_idout(&self, id: usize, out: &str) -> Option<&str> {
|
||||||
|
self.idout_var_map.get(&format!("{}/{}", id, out)).map(|s| &s[..])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn trans2bjit(
|
pub fn trans2bjit(
|
||||||
&self,
|
&mut self,
|
||||||
node: &ASTNodeRef,
|
node: &ASTNodeRef,
|
||||||
my_out: Option<String>,
|
my_out: Option<String>,
|
||||||
) -> Result<BlkASTRef, BlkJITCompileError> {
|
) -> Result<BlkASTRef, BlkJITCompileError> {
|
||||||
|
@ -223,16 +256,19 @@ impl Block2JITCompiler {
|
||||||
|
|
||||||
// XXX: SSA form of cranelift should take care of the rest!
|
// XXX: SSA form of cranelift should take care of the rest!
|
||||||
|
|
||||||
match &node.0.borrow().typ[..] {
|
let id = node.0.borrow().id;
|
||||||
"<r>" => {
|
|
||||||
if let Some((_in, out, first)) = node.first_child() {
|
if let Some(out) = &my_out {
|
||||||
let out = if out.len() > 0 { Some(out) } else { None };
|
if let Some(tmpvar) = self.get_var_for_idout(id, out) {
|
||||||
let child = self.trans2bjit(&first, out)?;
|
return Ok(BlkASTNode::new_get(0, tmpvar));
|
||||||
Ok(BlkASTNode::new_root(child))
|
}
|
||||||
} else {
|
} else {
|
||||||
Err(BlkJITCompileError::BadTree(node.clone()))
|
if let Some(tmpvar) = self.get_var_for_idout(id, "") {
|
||||||
|
return Ok(BlkASTNode::new_get(0, tmpvar));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
match &node.0.borrow().typ[..] {
|
||||||
"<a>" => {
|
"<a>" => {
|
||||||
let mut childs = vec![];
|
let mut childs = vec![];
|
||||||
|
|
||||||
|
@ -246,10 +282,10 @@ impl Block2JITCompiler {
|
||||||
|
|
||||||
Ok(BlkASTNode::new_area(childs))
|
Ok(BlkASTNode::new_area(childs))
|
||||||
}
|
}
|
||||||
"<res>" => {
|
|
||||||
// TODO: handle results properly, like remembering the most recent result
|
// TODO: handle results properly, like remembering the most recent result
|
||||||
// and append it to the end of the statements block. so that a temporary
|
// and append it to the end of the statements block. so that a temporary
|
||||||
// variable is created.
|
// variable is created.
|
||||||
|
"<r>" | "->" | "<res>" => {
|
||||||
if let Some((_in, out, first)) = node.first_child() {
|
if let Some((_in, out, first)) = node.first_child() {
|
||||||
let out = if out.len() > 0 { Some(out) } else { None };
|
let out = if out.len() > 0 { Some(out) } else { None };
|
||||||
self.trans2bjit(&first, out)
|
self.trans2bjit(&first, out)
|
||||||
|
@ -257,6 +293,9 @@ impl Block2JITCompiler {
|
||||||
Err(BlkJITCompileError::BadTree(node.clone()))
|
Err(BlkJITCompileError::BadTree(node.clone()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
"value" => {
|
||||||
|
Ok(BlkASTNode::new_literal(&node.0.borrow().lbl)?)
|
||||||
|
}
|
||||||
"set" => {
|
"set" => {
|
||||||
if let Some((_in, out, first)) = node.first_child() {
|
if let Some((_in, out, first)) = node.first_child() {
|
||||||
let out = if out.len() > 0 { Some(out) } else { None };
|
let out = if out.len() > 0 { Some(out) } else { None };
|
||||||
|
@ -266,7 +305,22 @@ impl Block2JITCompiler {
|
||||||
Err(BlkJITCompileError::BadTree(node.clone()))
|
Err(BlkJITCompileError::BadTree(node.clone()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"get" => Ok(BlkASTNode::new_get(node.0.borrow().id, &node.0.borrow().lbl)),
|
"get" => Ok(BlkASTNode::new_get(id, &node.0.borrow().lbl)),
|
||||||
|
"->2" | "->3" => {
|
||||||
|
if let Some((_in, out, first)) = node.first_child() {
|
||||||
|
let out = if out.len() > 0 { Some(out) } else { None };
|
||||||
|
let mut area = vec![];
|
||||||
|
let tmp_var = self.next_tmpvar_name("");
|
||||||
|
let expr = self.trans2bjit(&first, out)?;
|
||||||
|
area.push(BlkASTNode::new_set(&tmp_var, expr));
|
||||||
|
area.push(BlkASTNode::new_get(0, &tmp_var));
|
||||||
|
self.store_idout_var(id, "", &tmp_var);
|
||||||
|
Ok(BlkASTNode::new_area(area))
|
||||||
|
|
||||||
|
} else {
|
||||||
|
Err(BlkJITCompileError::BadTree(node.clone()))
|
||||||
|
}
|
||||||
|
}
|
||||||
optype => {
|
optype => {
|
||||||
let mut childs = vec![];
|
let mut childs = vec![];
|
||||||
|
|
||||||
|
@ -283,20 +337,53 @@ impl Block2JITCompiler {
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
|
|
||||||
// TODO: Check here if the optype has multiple outputs.
|
|
||||||
// when it has, make a sub-collection of statements
|
|
||||||
// and make temporary variables with ::Set
|
|
||||||
// then return the output with a final ::Get to the
|
|
||||||
// output "my_out".
|
|
||||||
// If no output is given in "my_out" it's an error!
|
|
||||||
//""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|
||||||
|
|
||||||
// TODO: Reorder the childs/arguments according to the input
|
// TODO: Reorder the childs/arguments according to the input
|
||||||
// order in the BlockLanguage
|
// order in the BlockLanguage
|
||||||
|
|
||||||
|
let cnt = self.lang.borrow().type_output_count(optype);
|
||||||
|
if cnt > 1 {
|
||||||
|
let mut area = vec![];
|
||||||
|
area.push(BlkASTNode::new_node(
|
||||||
|
id,
|
||||||
|
my_out.clone(),
|
||||||
|
&node.0.borrow().typ,
|
||||||
|
&node.0.borrow().lbl,
|
||||||
|
childs,
|
||||||
|
));
|
||||||
|
|
||||||
|
for i in 0..cnt {
|
||||||
|
let oname = self.lang.borrow().get_output_name_at_index(optype, i);
|
||||||
|
if let Some(oname) = oname {
|
||||||
|
let tmp_var = self.next_tmpvar_name(&oname);
|
||||||
|
|
||||||
|
area.push(BlkASTNode::new_set(
|
||||||
|
&tmp_var,
|
||||||
|
BlkASTNode::new_get(0, &format!("%{}", i)),
|
||||||
|
));
|
||||||
|
self.store_idout_var(
|
||||||
|
id,
|
||||||
|
&oname,
|
||||||
|
&tmp_var,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return Err(BlkJITCompileError::NoOutputAtIdx(optype.to_string(), i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(out) = &my_out {
|
||||||
|
if let Some(tmpvar) = self.get_var_for_idout(id, out) {
|
||||||
|
area.push(BlkASTNode::new_get(0, tmpvar));
|
||||||
|
} else {
|
||||||
|
return Err(BlkJITCompileError::NoTmpVarForOutput(id, out.to_string()));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err(BlkJITCompileError::ASTMissingOutputLabel(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(BlkASTNode::new_area(area))
|
||||||
|
} else {
|
||||||
Ok(BlkASTNode::new_node(
|
Ok(BlkASTNode::new_node(
|
||||||
node.0.borrow().id,
|
id,
|
||||||
my_out,
|
my_out,
|
||||||
&node.0.borrow().typ,
|
&node.0.borrow().typ,
|
||||||
&node.0.borrow().lbl,
|
&node.0.borrow().lbl,
|
||||||
|
@ -305,8 +392,9 @@ impl Block2JITCompiler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn compile(&self, fun: &BlockFun) -> Result<ASTNode, BlkJITCompileError> {
|
pub fn compile(&mut self, fun: &BlockFun) -> Result<ASTNode, BlkJITCompileError> {
|
||||||
let tree = fun.generate_tree::<ASTNodeRef>("zero").unwrap();
|
let tree = fun.generate_tree::<ASTNodeRef>("zero").unwrap();
|
||||||
println!("{}", tree.walk_dump("", "", 0));
|
println!("{}", tree.walk_dump("", "", 0));
|
||||||
|
|
||||||
|
|
|
@ -902,6 +902,40 @@ impl BlockLanguage {
|
||||||
identifiers
|
identifiers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_type_outputs(&self, typ: &str) -> Option<&[Option<String>]> {
|
||||||
|
let typ = self.types.get(typ)?;
|
||||||
|
Some(&typ.outputs)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_output_name_at_index(&self, typ: &str, idx: usize) -> Option<String> {
|
||||||
|
let outs = self.get_type_outputs(typ)?;
|
||||||
|
let mut i = 0;
|
||||||
|
for o in outs.iter() {
|
||||||
|
if let Some(outname) = o {
|
||||||
|
if i == idx {
|
||||||
|
return Some(outname.to_string());
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn type_output_count(&self, typ: &str) -> usize {
|
||||||
|
let mut cnt = 0;
|
||||||
|
|
||||||
|
if let Some(outs) = self.get_type_outputs(typ) {
|
||||||
|
for o in outs.iter() {
|
||||||
|
if o.is_some() {
|
||||||
|
cnt += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cnt
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_type_list(&self) -> Vec<(String, String, BlockUserInput)> {
|
pub fn get_type_list(&self) -> Vec<(String, String, BlockUserInput)> {
|
||||||
let mut out = vec![];
|
let mut out = vec![];
|
||||||
for (_, typ) in &self.types {
|
for (_, typ) in &self.types {
|
||||||
|
|
|
@ -700,7 +700,7 @@ impl NodeConfigurator {
|
||||||
if let Ok(block_fun) = block_fun.lock() {
|
if let Ok(block_fun) = block_fun.lock() {
|
||||||
if *generation != block_fun.generation() {
|
if *generation != block_fun.generation() {
|
||||||
*generation = block_fun.generation();
|
*generation = block_fun.generation();
|
||||||
let mut compiler = Block2JITCompiler::new();
|
let mut compiler = Block2JITCompiler::new(block_fun.block_language());
|
||||||
compiler.compile(&block_fun);
|
compiler.compile(&block_fun);
|
||||||
|
|
||||||
// let ast = block_compiler::compile(block_fun);
|
// let ast = block_compiler::compile(block_fun);
|
||||||
|
|
|
@ -26,19 +26,25 @@ fn check_blocklang_1() {
|
||||||
{
|
{
|
||||||
let mut block_fun = block_fun.lock().expect("matrix lock");
|
let mut block_fun = block_fun.lock().expect("matrix lock");
|
||||||
|
|
||||||
// block_fun.instanciate_at(0, 0, 0, "get", Some("in1".to_string()));
|
block_fun.instanciate_at(0, 0, 0, "get", Some("in1".to_string()));
|
||||||
// block_fun.instanciate_at(0, 0, 1, "value", Some("0.3".to_string()));
|
block_fun.instanciate_at(0, 0, 1, "value", Some("0.3".to_string()));
|
||||||
// block_fun.instanciate_at(0, 1, 0, "+", None);
|
block_fun.instanciate_at(0, 1, 0, "+", None);
|
||||||
// block_fun.instanciate_at(0, 2, 0, "set", Some("&sig1".to_string()));
|
block_fun.instanciate_at(0, 2, 0, "set", Some("&sig1".to_string()));
|
||||||
//
|
|
||||||
// block_fun.instanciate_at(0, 3, 0, "get", Some("in1".to_string()));
|
block_fun.instanciate_at(0, 3, 0, "get", Some("in1".to_string()));
|
||||||
// block_fun.instanciate_at(0, 3, 1, "get", Some("in2".to_string()));
|
block_fun.instanciate_at(0, 3, 1, "get", Some("in2".to_string()));
|
||||||
// block_fun.instanciate_at(0, 4, 0, "-", None);
|
block_fun.instanciate_at(0, 4, 0, "-", None);
|
||||||
// block_fun.instanciate_at(0, 5, 0, "->3", None);
|
block_fun.instanciate_at(0, 5, 0, "->3", None);
|
||||||
// block_fun.instanciate_at(0, 6, 1, "set", Some("*a".to_string()));
|
|
||||||
// block_fun.instanciate_at(0, 6, 2, "set", Some("x".to_string()));
|
block_fun.instanciate_at(0, 3, 5, "get", Some("in1".to_string()));
|
||||||
// block_fun.instanciate_at(0, 6, 0, "->", None);
|
block_fun.instanciate_at(0, 4, 5, "if", None);
|
||||||
// block_fun.instanciate_at(0, 7, 0, "->2", None);
|
block_fun.instanciate_at(1, 0, 0, "value", Some("0.5".to_string()));
|
||||||
|
block_fun.instanciate_at(2, 0, 0, "value", Some("-0.5".to_string()));
|
||||||
|
|
||||||
|
block_fun.instanciate_at(0, 6, 1, "set", Some("*a".to_string()));
|
||||||
|
block_fun.instanciate_at(0, 6, 2, "set", Some("x".to_string()));
|
||||||
|
block_fun.instanciate_at(0, 6, 0, "->", None);
|
||||||
|
block_fun.instanciate_at(0, 7, 0, "->2", None);
|
||||||
|
|
||||||
block_fun.instanciate_at(0, 0, 3, "get", Some("in1".to_string()));
|
block_fun.instanciate_at(0, 0, 3, "get", Some("in1".to_string()));
|
||||||
block_fun.instanciate_at(0, 0, 4, "get", Some("in2".to_string()));
|
block_fun.instanciate_at(0, 0, 4, "get", Some("in2".to_string()));
|
||||||
|
|
Loading…
Reference in a new issue