2022-07-31 10:18:42 +00:00
|
|
|
// Copyright (c) 2022 Weird Constructor <weirdconstructor@gmail.com>
|
|
|
|
// This file is a part of HexoDSP. Released under GPL-3.0-or-later.
|
|
|
|
// See README.md and COPYING for details.
|
|
|
|
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::collections::HashMap;
|
2022-07-31 15:53:52 +00:00
|
|
|
use std::rc::Rc;
|
2022-07-31 10:18:42 +00:00
|
|
|
|
|
|
|
use crate::blocklang::*;
|
2022-07-31 15:53:52 +00:00
|
|
|
use synfx_dsp_jit::ASTNode;
|
2022-07-31 10:18:42 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct JASTNode {
|
2022-07-31 15:53:52 +00:00
|
|
|
id: usize,
|
|
|
|
typ: String,
|
|
|
|
lbl: String,
|
2022-07-31 10:18:42 +00:00
|
|
|
nodes: Vec<(String, String, ASTNodeRef)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct ASTNodeRef(Rc<RefCell<JASTNode>>);
|
|
|
|
|
|
|
|
impl BlockASTNode for ASTNodeRef {
|
|
|
|
fn from(id: usize, typ: &str, lbl: &str) -> ASTNodeRef {
|
|
|
|
ASTNodeRef(Rc::new(RefCell::new(JASTNode {
|
|
|
|
id,
|
2022-07-31 15:53:52 +00:00
|
|
|
typ: typ.to_string(),
|
|
|
|
lbl: lbl.to_string(),
|
|
|
|
nodes: vec![],
|
2022-07-31 10:18:42 +00:00
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_node(&self, in_port: String, out_port: String, node: ASTNodeRef) {
|
|
|
|
self.0.borrow_mut().nodes.push((in_port, out_port, node));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ASTNodeRef {
|
|
|
|
pub fn first_child_ref(&self) -> Option<ASTNodeRef> {
|
|
|
|
self.0.borrow().nodes.get(0).map(|n| n.2.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn first_child(&self) -> Option<(String, String, ASTNodeRef)> {
|
|
|
|
self.0.borrow().nodes.get(0).cloned()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn nth_child(&self, i: usize) -> Option<(String, String, ASTNodeRef)> {
|
|
|
|
self.0.borrow().nodes.get(i).cloned()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn walk_dump(&self, input: &str, output: &str, indent: usize) -> String {
|
|
|
|
let indent_str = " ".repeat(indent + 1);
|
|
|
|
|
2022-07-31 15:53:52 +00:00
|
|
|
let out_port = if output.len() > 0 { format!("(out: {})", output) } else { "".to_string() };
|
|
|
|
let in_port = if input.len() > 0 { format!("(in: {})", input) } else { "".to_string() };
|
2022-07-31 10:18:42 +00:00
|
|
|
|
|
|
|
let mut s = format!(
|
|
|
|
"{}{}#{}[{}] {}{}\n",
|
2022-07-31 15:53:52 +00:00
|
|
|
indent_str,
|
|
|
|
self.0.borrow().id,
|
|
|
|
self.0.borrow().typ,
|
|
|
|
self.0.borrow().lbl,
|
|
|
|
out_port,
|
|
|
|
in_port
|
|
|
|
);
|
2022-07-31 10:18:42 +00:00
|
|
|
|
|
|
|
for (inp, out, n) in &self.0.borrow().nodes {
|
|
|
|
s += &n.walk_dump(&inp, &out, indent + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 01:56:38 +00:00
|
|
|
type BlkASTRef = Rc<BlkASTNode>;
|
2022-07-31 10:18:42 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
enum BlkASTNode {
|
2022-07-31 15:53:52 +00:00
|
|
|
Area {
|
|
|
|
childs: Vec<BlkASTRef>,
|
|
|
|
},
|
|
|
|
Set {
|
|
|
|
var: String,
|
|
|
|
expr: BlkASTRef,
|
|
|
|
},
|
|
|
|
Get {
|
|
|
|
id: usize,
|
|
|
|
var: String,
|
|
|
|
},
|
|
|
|
Node {
|
|
|
|
id: usize,
|
|
|
|
out: Option<String>,
|
|
|
|
typ: String,
|
|
|
|
lbl: String,
|
|
|
|
childs: Vec<(Option<String>, BlkASTRef)>,
|
|
|
|
},
|
2022-07-31 18:55:04 +00:00
|
|
|
Literal {
|
|
|
|
value: f64,
|
|
|
|
}
|
2022-07-31 10:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BlkASTNode {
|
2022-07-31 15:53:52 +00:00
|
|
|
pub fn dump(&self, indent: usize, inp: Option<&str>) -> String {
|
|
|
|
let mut indent_str = " ".repeat(indent + 1);
|
|
|
|
|
|
|
|
if let Some(inp) = inp {
|
|
|
|
indent_str += &format!("{}<= ", inp);
|
2022-07-31 18:55:04 +00:00
|
|
|
} else {
|
|
|
|
indent_str += "<= ";
|
2022-07-31 15:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
match self {
|
|
|
|
BlkASTNode::Area { childs } => {
|
2022-07-31 18:55:04 +00:00
|
|
|
let mut s = format!("{}Area\n", indent_str);
|
2022-07-31 15:53:52 +00:00
|
|
|
for c in childs.iter() {
|
2022-08-01 01:56:38 +00:00
|
|
|
s += &c.dump(indent + 1, None);
|
2022-07-31 15:53:52 +00:00
|
|
|
}
|
|
|
|
s
|
|
|
|
}
|
|
|
|
BlkASTNode::Set { var, expr } => {
|
2022-08-01 01:56:38 +00:00
|
|
|
format!("{}set '{}'=\n", indent_str, var) + &expr.dump(indent + 1, None)
|
2022-07-31 15:53:52 +00:00
|
|
|
}
|
2022-08-01 01:56:38 +00:00
|
|
|
BlkASTNode::Get { id, var } => {
|
|
|
|
format!("{}get '{}' (id={})\n", indent_str, var, id)
|
2022-07-31 15:53:52 +00:00
|
|
|
}
|
2022-07-31 18:55:04 +00:00
|
|
|
BlkASTNode::Literal { value } => {
|
|
|
|
format!("{}{}\n", indent_str, value)
|
|
|
|
}
|
2022-08-01 01:56:38 +00:00
|
|
|
BlkASTNode::Node { id, out, typ, lbl, childs } => {
|
2022-07-31 15:53:52 +00:00
|
|
|
let lbl = if *typ == *lbl { "".to_string() } else { format!("[{}]", lbl) };
|
|
|
|
|
|
|
|
let mut s = if let Some(out) = out {
|
2022-08-01 01:56:38 +00:00
|
|
|
format!("{}{}{} (id={}/{})\n", indent_str, typ, lbl, id, out)
|
2022-07-31 15:53:52 +00:00
|
|
|
} else {
|
2022-08-01 01:56:38 +00:00
|
|
|
format!("{}{}{} (id={})\n", indent_str, typ, lbl, id)
|
2022-07-31 15:53:52 +00:00
|
|
|
};
|
|
|
|
for (inp, c) in childs.iter() {
|
2022-08-01 01:56:38 +00:00
|
|
|
s += &format!("{}", c.dump(indent + 1, inp.as_ref().map(|s| &s[..])));
|
2022-07-31 15:53:52 +00:00
|
|
|
}
|
|
|
|
s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-31 10:18:42 +00:00
|
|
|
pub fn new_area(childs: Vec<BlkASTRef>) -> BlkASTRef {
|
2022-08-01 01:56:38 +00:00
|
|
|
Rc::new(BlkASTNode::Area { childs })
|
2022-07-31 10:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_set(var: &str, expr: BlkASTRef) -> BlkASTRef {
|
2022-08-01 01:56:38 +00:00
|
|
|
Rc::new(BlkASTNode::Set { var: var.to_string(), expr })
|
2022-07-31 10:18:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-31 15:53:52 +00:00
|
|
|
pub fn new_get(id: usize, var: &str) -> BlkASTRef {
|
2022-08-01 01:56:38 +00:00
|
|
|
Rc::new(BlkASTNode::Get { id, var: var.to_string() })
|
2022-07-31 10:18:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-31 18:55:04 +00:00
|
|
|
pub fn new_literal(val: &str) -> Result<BlkASTRef, BlkJITCompileError> {
|
|
|
|
if let Ok(value) = val.parse::<f64>() {
|
2022-08-01 01:56:38 +00:00
|
|
|
Ok(Rc::new(BlkASTNode::Literal { value }))
|
2022-07-31 18:55:04 +00:00
|
|
|
} else {
|
|
|
|
Err(BlkJITCompileError::BadLiteralNumber(val.to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-31 15:53:52 +00:00
|
|
|
pub fn new_node(
|
|
|
|
id: usize,
|
|
|
|
out: Option<String>,
|
|
|
|
typ: &str,
|
|
|
|
lbl: &str,
|
|
|
|
childs: Vec<(Option<String>, BlkASTRef)>,
|
|
|
|
) -> BlkASTRef {
|
2022-08-01 01:56:38 +00:00
|
|
|
Rc::new(BlkASTNode::Node {
|
2022-07-31 15:53:52 +00:00
|
|
|
id,
|
|
|
|
out,
|
|
|
|
typ: typ.to_string(),
|
|
|
|
lbl: lbl.to_string(),
|
|
|
|
childs,
|
2022-08-01 01:56:38 +00:00
|
|
|
})
|
2022-07-31 15:53:52 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-31 10:18:42 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum BlkJITCompileError {
|
|
|
|
UnknownError,
|
|
|
|
BadTree(ASTNodeRef),
|
2022-07-31 18:55:04 +00:00
|
|
|
NoOutputAtIdx(String, usize),
|
|
|
|
ASTMissingOutputLabel(usize),
|
|
|
|
NoTmpVarForOutput(usize, String),
|
|
|
|
BadLiteralNumber(String),
|
2022-07-31 10:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Block2JITCompiler {
|
|
|
|
id_node_map: HashMap<usize, BlkASTRef>,
|
2022-07-31 18:55:04 +00:00
|
|
|
idout_var_map: HashMap<String, String>,
|
|
|
|
lang: Rc<RefCell<BlockLanguage>>,
|
|
|
|
tmpvar_counter: usize,
|
2022-07-31 10:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 1. compile the weird tree into a graph
|
|
|
|
// - make references where IDs go
|
|
|
|
// - add a use count to each node, so that we know when to make temporary variables
|
|
|
|
|
|
|
|
impl Block2JITCompiler {
|
2022-07-31 18:55:04 +00:00
|
|
|
pub fn new(lang: Rc<RefCell<BlockLanguage>>) -> Self {
|
|
|
|
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[..])
|
2022-07-31 10:18:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-31 15:53:52 +00:00
|
|
|
pub fn trans2bjit(
|
2022-07-31 18:55:04 +00:00
|
|
|
&mut self,
|
2022-07-31 15:53:52 +00:00
|
|
|
node: &ASTNodeRef,
|
|
|
|
my_out: Option<String>,
|
|
|
|
) -> Result<BlkASTRef, BlkJITCompileError> {
|
2022-07-31 18:55:04 +00:00
|
|
|
let id = node.0.borrow().id;
|
|
|
|
|
|
|
|
if let Some(out) = &my_out {
|
|
|
|
if let Some(tmpvar) = self.get_var_for_idout(id, out) {
|
|
|
|
return Ok(BlkASTNode::new_get(0, tmpvar));
|
2022-07-31 10:18:42 +00:00
|
|
|
}
|
2022-07-31 18:55:04 +00:00
|
|
|
} else {
|
|
|
|
if let Some(tmpvar) = self.get_var_for_idout(id, "") {
|
|
|
|
return Ok(BlkASTNode::new_get(0, tmpvar));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match &node.0.borrow().typ[..] {
|
2022-07-31 10:18:42 +00:00
|
|
|
"<a>" => {
|
|
|
|
let mut childs = vec![];
|
|
|
|
|
|
|
|
let mut i = 0;
|
2022-07-31 15:53:52 +00:00
|
|
|
while let Some((_in, out, child)) = node.nth_child(i) {
|
|
|
|
let out = if out.len() > 0 { Some(out) } else { None };
|
|
|
|
let child = self.trans2bjit(&child, out)?;
|
2022-07-31 10:18:42 +00:00
|
|
|
childs.push(child);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(BlkASTNode::new_area(childs))
|
|
|
|
}
|
2022-07-31 18:55:04 +00:00
|
|
|
// TODO: handle results properly, like remembering the most recent result
|
|
|
|
// and append it to the end of the statements block. so that a temporary
|
|
|
|
// variable is created.
|
|
|
|
"<r>" | "->" | "<res>" => {
|
2022-07-31 15:53:52 +00:00
|
|
|
if let Some((_in, out, first)) = node.first_child() {
|
|
|
|
let out = if out.len() > 0 { Some(out) } else { None };
|
|
|
|
self.trans2bjit(&first, out)
|
2022-07-31 10:18:42 +00:00
|
|
|
} else {
|
|
|
|
Err(BlkJITCompileError::BadTree(node.clone()))
|
|
|
|
}
|
|
|
|
}
|
2022-07-31 18:55:04 +00:00
|
|
|
"value" => {
|
|
|
|
Ok(BlkASTNode::new_literal(&node.0.borrow().lbl)?)
|
|
|
|
}
|
2022-07-31 10:18:42 +00:00
|
|
|
"set" => {
|
2022-07-31 15:53:52 +00:00
|
|
|
if let Some((_in, out, first)) = node.first_child() {
|
|
|
|
let out = if out.len() > 0 { Some(out) } else { None };
|
|
|
|
let expr = self.trans2bjit(&first, out)?;
|
2022-07-31 10:18:42 +00:00
|
|
|
Ok(BlkASTNode::new_set(&node.0.borrow().lbl, expr))
|
|
|
|
} else {
|
|
|
|
Err(BlkJITCompileError::BadTree(node.clone()))
|
|
|
|
}
|
|
|
|
}
|
2022-07-31 18:55:04 +00:00
|
|
|
"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()))
|
|
|
|
}
|
|
|
|
}
|
2022-07-31 10:18:42 +00:00
|
|
|
optype => {
|
|
|
|
let mut childs = vec![];
|
|
|
|
|
|
|
|
let mut i = 0;
|
2022-07-31 15:53:52 +00:00
|
|
|
while let Some((inp, out, child)) = node.nth_child(i) {
|
|
|
|
let out = if out.len() > 0 { Some(out) } else { None };
|
|
|
|
|
|
|
|
let child = self.trans2bjit(&child, out)?;
|
|
|
|
if inp.len() > 0 {
|
|
|
|
childs.push((Some(inp.to_string()), child));
|
|
|
|
} else {
|
|
|
|
childs.push((None, child));
|
|
|
|
}
|
2022-07-31 10:18:42 +00:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
|
2022-07-31 12:14:40 +00:00
|
|
|
// TODO: Reorder the childs/arguments according to the input
|
|
|
|
// order in the BlockLanguage
|
|
|
|
|
2022-07-31 18:55:04 +00:00
|
|
|
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(
|
|
|
|
id,
|
|
|
|
my_out,
|
|
|
|
&node.0.borrow().typ,
|
|
|
|
&node.0.borrow().lbl,
|
|
|
|
childs,
|
|
|
|
))
|
|
|
|
}
|
2022-07-31 10:18:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 01:56:38 +00:00
|
|
|
pub fn bjit2jit(&mut self, ast: &BlkASTRef) -> Result<Box<ASTNode>, BlkJITCompileError> {
|
|
|
|
use synfx_dsp_jit::build::*;
|
|
|
|
|
|
|
|
match &**ast {
|
|
|
|
BlkASTNode::Area { childs } => {
|
|
|
|
let mut stmt = vec![];
|
|
|
|
for c in childs.iter() {
|
|
|
|
stmt.push(self.bjit2jit(&c)?);
|
|
|
|
}
|
|
|
|
Ok(stmts(&stmt[..]))
|
|
|
|
},
|
|
|
|
BlkASTNode::Set { var, expr } => {
|
|
|
|
let e = self.bjit2jit(&expr)?;
|
|
|
|
Ok(assign(var, e))
|
|
|
|
}
|
|
|
|
BlkASTNode::Get { id, var: varname } => {
|
|
|
|
Ok(var(varname))
|
|
|
|
},
|
|
|
|
BlkASTNode::Node { id, out, typ, lbl, childs } => {
|
|
|
|
Err(BlkJITCompileError::UnknownError)
|
|
|
|
}
|
|
|
|
BlkASTNode::Literal { value } => {
|
|
|
|
Ok(literal(*value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn compile(&mut self, fun: &BlockFun) -> Result<Box<ASTNode>, BlkJITCompileError> {
|
2022-07-31 10:18:42 +00:00
|
|
|
let tree = fun.generate_tree::<ASTNodeRef>("zero").unwrap();
|
|
|
|
println!("{}", tree.walk_dump("", "", 0));
|
|
|
|
|
2022-08-01 01:56:38 +00:00
|
|
|
let blkast = self.trans2bjit(&tree, None)?;
|
|
|
|
println!("R: {}", blkast.dump(0, None));
|
2022-07-31 10:18:42 +00:00
|
|
|
|
2022-08-01 01:56:38 +00:00
|
|
|
self.bjit2jit(&blkast)
|
2022-07-31 10:18:42 +00:00
|
|
|
}
|
|
|
|
}
|