Skip to content

Commit

Permalink
Update origen generator processors to be compatible with origen_metal…
Browse files Browse the repository at this point in the history
… ast updates
  • Loading branch information
rlaj committed Jun 13, 2024
1 parent 5e5c29f commit a5b1686
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 21 deletions.
96 changes: 82 additions & 14 deletions rust/origen/src/generator/processors/cycle_combiner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,22 @@ impl Processor<PAT> for CycleCombiner {
}
}
// For all other nodes except for cycles
_ => {
if self.cycle_count == 0 {
Ok(Return::ProcessChildren)
} else {
let cyc = self.consume_cycles();
let new_node = node.process_and_update_children(self)?;
Ok(Return::Inline(vec![cyc, new_node]))
}
}
_ => Ok(Return::ProcessChildren)
}
}

// Don't let it leave an open block with cycles pending
fn on_end_of_block(&mut self, _node: &Node<PAT>) -> origen_metal::Result<Return<PAT>> {

fn on_processed_node(&mut self, node: &Node<PAT>) -> origen_metal::Result<Return<PAT>> {
if self.cycle_count > 0 {
Ok(Return::Replace(self.consume_cycles()))
let cyc = self.consume_cycles();
if node.children.len() > 0 {
let mut new_node = node.clone();
new_node.add_child(cyc);
Ok(Return::Replace(new_node))
} else {
Ok(Return::Inline(vec![cyc, node.clone()]))
}
} else {
Ok(Return::None)
Ok(Return::Unmodified)
}
}
}
Expand Down Expand Up @@ -284,3 +282,73 @@ impl Processor<PAT> for UnpackCaptures {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::generator::nodes::PAT;
use origen_metal::ast::AST;

fn reg_write_node() -> Node<PAT> {
let mut trans = crate::Transaction::new_write(0x12345678_u32.into(), 32).unwrap();
trans.reg_id = Some(10);
node!(PAT::RegWrite, trans)
}

#[test]
fn it_works() {
let mut ast = AST::new();
ast.push(node!(PAT::Test, "cycle_combiner".to_string()));
ast.push(node!(PAT::Comment, 1, "HELLO".to_string()));
let id = ast.push_and_open(reg_write_node());
ast.push(node!(PAT::Comment, 1, "SHOULD BE INSIDE REG TRANSACTION".to_string()));
ast.push(node!(PAT::Cycle, 1, false));
ast.push(node!(PAT::Cycle, 1, true));
ast.push(node!(PAT::Cycle, 1, true));
ast.push(node!(PAT::Cycle, 1, true));
ast.push(node!(PAT::Cycle, 1, true));
ast.push(node!(PAT::Cycle, 1, true));
let _ = ast.close(id);
ast.push(node!(PAT::Comment, 1, "SHOULD BE OUTSIDE REG TRANSACTION".to_string()));

let combined = CycleCombiner::run(&ast.to_node()).expect("Cycles combined");

let mut expect = AST::new();
expect.push(node!(PAT::Test, "cycle_combiner".to_string()));
expect.push(node!(PAT::Comment, 1, "HELLO".to_string()));
let id = expect.push_and_open(reg_write_node());
expect.push(node!(PAT::Comment, 1, "SHOULD BE INSIDE REG TRANSACTION".to_string()));
expect.push(node!(PAT::Cycle, 1, false));
expect.push(node!(PAT::Cycle, 5, true));
let _ = expect.close(id);
expect.push(node!(PAT::Comment, 1, "SHOULD BE OUTSIDE REG TRANSACTION".to_string()));

assert_eq!(combined, expect.to_node());
}

#[test]
fn it_leaves_something_behind() {
let mut ast = AST::new();
ast.push(node!(PAT::Test, "all_compressable".to_string()));
ast.push(node!(PAT::SetTimeset, 0));
ast.push(node!(PAT::SetPinHeader, 10));
ast.push(node!(PAT::Cycle, 100, true));
ast.push(node!(PAT::Comment, 1, "Producing Pattern".to_string()));
ast.push(node!(PAT::Cycle, 10, true));
ast.push(node!(PAT::Cycle, 10, true));
ast.push(node!(PAT::PatternEnd));

let combined = CycleCombiner::run(&ast.to_node()).expect("Cycles combined");

let mut expect = AST::new();
expect.push(node!(PAT::Test, "all_compressable".to_string()));
expect.push(node!(PAT::SetTimeset, 0));
expect.push(node!(PAT::SetPinHeader, 10));
expect.push(node!(PAT::Cycle, 100, true));
expect.push(node!(PAT::Comment, 1, "Producing Pattern".to_string()));
expect.push(node!(PAT::Cycle, 20, true));
expect.push(node!(PAT::PatternEnd));

assert_eq!(combined, expect.to_node());
}
}
69 changes: 66 additions & 3 deletions rust/origen/src/generator/processors/flatten_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl Processor<PAT> for FlattenText {
}
}

fn on_end_of_block(&mut self, node: &Node<PAT>) -> origen_metal::Result<Return<PAT>> {
fn on_processed_node(&mut self, node: &Node<PAT>) -> origen_metal::Result<Return<PAT>> {
match node.attrs {
PAT::TextLine => {
let n = self.current_line_to_text();
Expand All @@ -178,10 +178,73 @@ impl Processor<PAT> for FlattenText {
if lvl.is_some() && lvl.unwrap() == 0 {
Ok(Return::Inline(vec![self.section_boundary()]))
} else {
Ok(Return::None)
Ok(Return::Unmodified)
}
}
_ => Ok(Return::None),
_ => Ok(Return::Unmodified)
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::generator::nodes::PAT;

#[test]
fn it_works() {
let mut header = node!(PAT::PatternHeader);
header.add_child(node!(PAT::TextBoundaryLine));
let mut section = node!(PAT::TextSection, Some("Generated".to_string()), None);
let mut text_lines = vec![];
let mut text_line = node!(PAT::TextLine);
text_line.add_child(node!(PAT::Text, "By: user".to_string()));
text_lines.push(text_line);
let mut text_line = node!(PAT::TextLine);
text_line.add_child(node!(PAT::Text, "Command: origen generate pattern".to_string()));
text_lines.push(text_line);
section.add_children(text_lines);
header.add_child(section);
header.add_child(node!(PAT::TextBoundaryLine));
let mut section = node!(PAT::TextSection, Some("Workspace".to_string()), None);
let mut sub_section = node!(PAT::TextSection, Some("Environment".to_string()), None);
let mut text_lines = vec![];
let mut text_line = node!(PAT::TextLine);
text_line.add_child(node!(PAT::Text, "Mode: development".to_string()));
text_lines.push(text_line);
sub_section.add_children(text_lines);
section.add_child(sub_section);
let mut sub_section = node!(PAT::TextSection, Some("Origen Core".to_string()), None);
let mut text_lines = vec![];
let mut text_line = node!(PAT::TextLine);
text_line.add_child(node!(PAT::Text, "Version: 2.something".to_string()));
text_lines.push(text_line);
let mut text_line = node!(PAT::TextLine);
text_line.add_child(node!(PAT::Text, "Executable Path: /path/to/python/python.exe".to_string()));
text_lines.push(text_line);
sub_section.add_children(text_lines);
section.add_child(sub_section);
header.add_child(section);
header.add_child(node!(PAT::TextBoundaryLine));

let header_flat = FlattenText::run(&header).expect("Text flattened");

let mut expect = node!(PAT::PatternHeader);
expect.add_children(vec![
node!(PAT::Text, "******************************************************************************************".to_string()),
node!(PAT::Text, "Generated".to_string()),
node!(PAT::Text, " By: user".to_string()),
node!(PAT::Text, " Command: origen generate pattern".to_string()),
node!(PAT::Text, "******************************************************************************************".to_string()),
node!(PAT::Text, "Workspace".to_string()),
node!(PAT::Text, " Environment".to_string()),
node!(PAT::Text, " Mode: development".to_string()),
node!(PAT::Text, " Origen Core".to_string()),
node!(PAT::Text, " Version: 2.something".to_string()),
node!(PAT::Text, " Executable Path: /path/to/python/python.exe".to_string()),
node!(PAT::Text, "******************************************************************************************".to_string()),
]);

assert_eq!(header_flat, expect);
}
}
8 changes: 4 additions & 4 deletions rust/origen/src/generator/processors/pin_action_combiner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,19 @@ impl Processor<PAT> for PinActionCombiner {
}
}

fn on_end_of_block(&mut self, node: &Node<PAT>) -> origen_metal::Result<Return<PAT>> {
fn on_processed_node(&mut self, node: &Node<PAT>) -> origen_metal::Result<Return<PAT>> {
match &node.attrs {
PAT::PinGroupAction(_grp_id, _actions, _metadata) => {
if self.first_pass {
if self.delete_current_index {
self.indices_to_delete.push(self.i);
}
self.delete_current_index = false;
self.i += 1;
}
self.i += 1;
Ok(Return::None)
Ok(Return::Unmodified)
}
_ => Ok(Return::None),
_ => Ok(Return::Unmodified)
}
}
}
44 changes: 44 additions & 0 deletions rust/origen_metal/src/ast/processors/to_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,47 @@ impl<T: Attrs> Processor<T> for ToString<T> {
Ok(Return::None)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[derive(Clone, PartialEq, Serialize, Debug)]
pub enum STRTEST {
Root,
Integer(i64),
Float(f64),
String(String),
}

impl std::fmt::Display for STRTEST {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self {
_ => write!(f, "{}", format!("{:?}", self)),
}
}
}

#[test]
fn test_process_children() {
let mut ast: AST<STRTEST> = AST::new();
let mut ids: Vec<usize> = vec![];
ids.push(ast.push_and_open(node!(STRTEST::Root)));
ast.push(node!(STRTEST::String, "Hello World!".to_string()));
ast.push(node!(STRTEST::Integer, 2001));
ids.push(ast.push_and_open(node!(STRTEST::Float, 97.1)));
ast.push(node!(STRTEST::String, "Test Indent".to_string()));
let _ = ast.close(ids.pop().unwrap());
ast.push(node!(STRTEST::String, "Pop back".to_string()));
let _ = ast.close(ids.pop().unwrap());

let expect = "Root
String(\"Hello World!\")
Integer(2001)
Float(97.1)
String(\"Test Indent\")
String(\"Pop back\")\n".to_string();

assert_eq!(ast.to_string(), expect);
}
}

0 comments on commit a5b1686

Please sign in to comment.