From 8ab4b0ba47ff5281636d46e3cd680df1d7c550f3 Mon Sep 17 00:00:00 2001 From: Lennart Van Hirtum Date: Tue, 25 Jun 2024 22:52:19 +0200 Subject: [PATCH] Whoo, recursive modules :tada: --- README.md | 4 ++-- src/instantiation/mod.rs | 47 ++++++++++++++++++++-------------------- tinyTestFile.sus | 32 +++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index d842b33..55a94b9 100644 --- a/README.md +++ b/README.md @@ -121,7 +121,7 @@ In this example, we create a memory block with a read port and a write port. Thi - [x] Arbitrary single-clock full flow - [ ] Arbitrary multi-clock full flow - [x] Generative Code -- [ ] Generative Parameters +- [x] Generative Parameters - [ ] Type Templates ### Language Features @@ -147,7 +147,7 @@ In this example, we create a memory block with a read port and a write port. Thi - [x] Generative Conditions - [x] Generative For Loops - [ ] Generative While Loops -- [ ] Generative Parameters +- [x] Generative Parameters - [x] Multi-Interface Syntax - [ ] Native Module integration syntax - [ ] Intrinsic Modules diff --git a/src/instantiation/mod.rs b/src/instantiation/mod.rs index 317f421..c32f893 100644 --- a/src/instantiation/mod.rs +++ b/src/instantiation/mod.rs @@ -166,34 +166,35 @@ impl InstantiationList { } pub fn instantiate(&self, md : &Module, linker : &Linker, template_args : FlatAlloc) -> Option> { - let mut cache_borrow = self.cache.borrow_mut(); + let cache_borrow = self.cache.borrow(); // Temporary, no template arguments yet - match cache_borrow.entry(template_args) { - std::collections::hash_map::Entry::Occupied(occ) => { - let instance_rc = occ.get(); - if !instance_rc.errors.did_error { - Some(instance_rc.clone()) - } else { - None - } - } - std::collections::hash_map::Entry::Vacant(vac) => { - let t_args = vac.key(); - let result = perform_instantiation(md, linker, t_args); + let instance = if let Some(found) = cache_borrow.get(&template_args) { + found.clone() + } else { + std::mem::drop(cache_borrow); + + let result = perform_instantiation(md, linker, &template_args); - if config().debug_print_module_contents { - println!("[[Instantiated {}]]", result.name); - for (id, w) in &result.wires { - println!("{id:?} -> {w:?}"); - } - for (id, sm) in &result.submodules { - println!("SubModule {id:?}: {sm:?}"); - } + if config().debug_print_module_contents { + println!("[[Instantiated {}]]", result.name); + for (id, w) in &result.wires { + println!("{id:?} -> {w:?}"); + } + for (id, sm) in &result.submodules { + println!("SubModule {id:?}: {sm:?}"); } - - Some(vac.insert(Rc::new(result)).clone()) } + + let result_ref = Rc::new(result); + assert!(self.cache.borrow_mut().insert(template_args, result_ref.clone()).is_none()); + result_ref + }; + + if !instance.errors.did_error { + Some(instance.clone()) + } else { + None } } diff --git a/tinyTestFile.sus b/tinyTestFile.sus index be206dc..ea4f231 100644 --- a/tinyTestFile.sus +++ b/tinyTestFile.sus @@ -78,3 +78,35 @@ module testTinyTestMod { tinyTestMod:: b tinyTestMod:: c } + + + +module tree_add { + input gen int WIDTH + + input int[WIDTH] values + output int sum + + if WIDTH == 1 { + sum = values[0] + } else { + gen int HALF_WIDTH = WIDTH / 2 + tree_add:: left + tree_add:: right + + for int i in 0..HALF_WIDTH { + left.values[i] = values[i] + right.values[i] = values[i+HALF_WIDTH] + } + + if WIDTH % 2 == 0 { + sum = left.sum + right.sum + } else { + sum = left.sum + right.sum + values[WIDTH - 1] + } + } +} + +module make_tree_add { + tree_add::<139;> tr +} \ No newline at end of file