Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions crates/celox-frontend-veryl/src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,42 @@ pub fn external_signatures_named(
use veryl_analyzer::ir::Component;

// Veryl children link through the IR, but the SV modules beneath them
// still have to be prepared by the SV frontend.
fn sv_descendants(
module: &veryl_analyzer::ir::Module,
names: &mut Vec<String>,
seen: &mut crate::HashSet<StrId>,
) {
// still have to be prepared by the SV frontend. Instances of one analyzed
// body share it, so each body is visited once across all exported roots.
// Bodies are keyed by address: equal signatures can still differ in the
// aliases of their SV children.
type Memo = HashMap<*const veryl_analyzer::ir::Module, std::rc::Rc<Vec<StrId>>>;
fn sv_descendants(module: &veryl_analyzer::ir::Module, memo: &mut Memo) -> Vec<StrId> {
let mut names = Vec::new();
let mut seen = crate::HashSet::default();
for declaration in &module.declarations {
let veryl_analyzer::ir::Declaration::Inst(instance) = declaration else {
continue;
};
match instance.component.as_ref() {
Component::SystemVerilog(child) => {
if seen.insert(child.name) {
names.push(
veryl_parser::resource_table::get_str_value(child.name)
.unwrap_or_default(),
);
names.push(child.name);
}
}
Component::Module(child) => sv_descendants(child, names, seen),
Component::Module(child) => {
let key: *const _ = child;
let nested = match memo.get(&key) {
Some(nested) => nested.clone(),
None => {
let nested = std::rc::Rc::new(sv_descendants(child, memo));
memo.insert(key, nested.clone());
nested
}
};
names.extend(nested.iter().filter(|&&name| seen.insert(name)));
}
Component::Interface(_) => {}
}
}
names
}
let mut memo = Memo::default();

let mut external = ExternalHierarchy::default();
for component in &ir.components {
Expand Down Expand Up @@ -113,12 +125,10 @@ pub fn external_signatures_named(
let (mut sim_module, _) =
project_module_with_ids(&empty, module, config, ids, &HashMap::default())?;
sim_module.name = name(module);
let mut unresolved_instances = Vec::new();
sv_descendants(
module,
&mut unresolved_instances,
&mut crate::HashSet::default(),
);
let unresolved_instances = sv_descendants(module, &mut memo)
.into_iter()
.map(|name| veryl_parser::resource_table::get_str_value(name).unwrap_or_default())
.collect();
let id = celox_design::ModuleId(external.modules.len());
external.roots.insert(sim_module.name.clone(), id);
external.modules.insert(
Expand Down
64 changes: 58 additions & 6 deletions crates/celox-frontend-veryl/src/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,22 @@ fn parse_external_hierarchy<'a>(
}
module_names.insert(global_id, external_module.sim_module.name.clone());
let native_module = match native_modules {
Some(names) => names
.get(&external_module.sim_module.name)
.and_then(|&index| match &ir.components[index] {
Component::Module(module) => Some(module),
_ => None,
}),
Some(names) => match names.get(&external_module.sim_module.name) {
Some(&index) => match ir.components.get(index) {
Some(Component::Module(module)) => Some(module),
_ => {
return Err(ParserError::illegal_context(
"external module hierarchy",
format!(
"native module `{}` maps to component {index}, which is not a module",
external_module.sim_module.name
),
None,
));
}
},
None => None,
},
None => name_to_ir
.get(&resource_table::insert_str(
&external_module.sim_module.name,
Expand Down Expand Up @@ -527,3 +537,45 @@ fn reject_generic_interface_ports(module: &Module) -> Result<(), ParserError> {
Some(&module.token),
))
}

#[cfg(test)]
mod tests {
use super::*;
use veryl_analyzer::{Analyzer, Context, attribute_table};
use veryl_parser::Parser;

#[test]
fn rejects_native_module_indices_outside_the_ir() {
symbol_table::clear();
attribute_table::clear();
let code = "module Top {} module Child {}";
let metadata = veryl_metadata::Metadata::create_default("prj").unwrap();
let parser = Parser::parse(code, &"top.veryl").unwrap();
let analyzer = Analyzer::new(&metadata);
assert!(analyzer.analyze_pass1("prj", &parser.veryl).is_empty());
assert!(Analyzer::analyze_post_pass1().is_empty());
let mut ir = veryl_analyzer::ir::Ir::default();
let mut context = Context::default();
assert!(
analyzer
.analyze_pass2(&parser.veryl, &mut context, Some(&mut ir))
.is_empty()
);
let config = BuildConfig::default();
let external = crate::external_signatures(&ir, &config).unwrap();
let native = [("Child".to_string(), ir.components.len())]
.into_iter()
.collect::<HashMap<_, _>>();
let error = parse_ir_with_external_hierarchy_and_native_modules(
&ir,
&LoopProvenance::default(),
&external,
&config,
&resource_table::insert_str("Top"),
&native,
)
.err()
.expect("an out-of-range index must be rejected");
assert!(error.to_string().contains("not a module"), "{error}");
}
}
30 changes: 30 additions & 0 deletions crates/celox/tests/frontends/systemverilog/mixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,36 @@ sv_backends! {
assert_eq!(sim.get(sim.signal("y")), 0x76u8.into());
}

fn keeps_specialized_systemverilog_leaves_of_identical_veryl_children(sim) {
@setup {
let veryl = r#"
module Inner (a: input logic<8>, y: output logic<8>) {
inst leaf: $sv::SvLeaf #(K: 8'h0f) (a, y);
}
module Mid (a: input logic<8>, y0: output logic<8>, y1: output logic<8>) {
inst first: Inner (a, y: y0);
inst second: Inner (a: a + 8'd1, y: y1);
}
"#;
let sv = r#"
module Top(input logic [7:0] a, output logic [7:0] y0, y1);
prj_Mid mid(.a(a), .y0(y0), .y1(y1));
endmodule
module SvLeaf #(parameter logic [7:0] K = 0)(input logic [7:0] a, output logic [7:0] y);
assign y = a ^ K;
endmodule
"#;
}
@build Simulator::from_mixed_sources(
vec![(veryl, Path::new("mid.veryl"))],
vec![(sv, Path::new("top.sv"))], "Top",
);
let a = sim.signal("a");
sim.modify(|io| io.set(a, 0x20u8)).unwrap();
assert_eq!(sim.get(sim.signal("y0")), 0x2fu8.into());
assert_eq!(sim.get(sim.signal("y1")), 0x2eu8.into());
}

fn reaches_systemverilog_descendants_through_native_veryl_children(sim) {
@setup {
let veryl = r#"
Expand Down
Loading