Skip to content

Commit

Permalink
Whoo, recursive modules 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Jun 25, 2024
1 parent da17120 commit 8ab4b0b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
47 changes: 24 additions & 23 deletions src/instantiation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,34 +166,35 @@ impl InstantiationList {
}

pub fn instantiate(&self, md : &Module, linker : &Linker, template_args : FlatAlloc<ConcreteTemplateArg, TemplateIDMarker>) -> Option<Rc<InstantiatedModule>> {
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
}
}

Expand Down
32 changes: 32 additions & 0 deletions tinyTestFile.sus
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,35 @@ module testTinyTestMod {
tinyTestMod::<beep = 4;> b
tinyTestMod::<beep = 3;> 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::<HALF_WIDTH;> left
tree_add::<HALF_WIDTH;> 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
}

0 comments on commit 8ab4b0b

Please sign in to comment.