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
51 changes: 28 additions & 23 deletions crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub(crate) struct LoweredSvModule {
#[derive(Clone)]
struct AnalyzedSvModule {
name: String,
interface_ports: HashSet<String>,
interface_member_paths: HashMap<String, Vec<String>>,
source: std::rc::Rc<sv::ParsedSource>,
implicit_nets_allowed: bool,
/// The positional interface of every module in all sources, used to bind
Expand Down Expand Up @@ -390,7 +390,8 @@ fn analyze_sources_with_requests(
)
})
.collect::<Vec<_>>();
let elaboration = sv::plan_interfaces(sources, &bindings)?;
let (elaboration, interface_member_paths) =
sv::plan_interfaces_with_member_paths(sources, &bindings)?;
let root_names = elaboration.root_names.clone();
let mut modules = HashMap::default();
let sources: Vec<_> = sources
Expand Down Expand Up @@ -487,10 +488,9 @@ fn analyze_sources_with_requests(
modules.insert(
name,
AnalyzedSvModule {
interface_ports: elaboration
.modules
interface_member_paths: interface_member_paths
.get(&module_name)
.map(|plan| plan.ports.iter().map(|port| port.name.clone()).collect())
.cloned()
.unwrap_or_default(),
implicit_nets_allowed: implicit_net_permissions
.get(
Expand Down Expand Up @@ -1656,25 +1656,9 @@ fn prepare_external_hierarchy_collecting(
}
}
}
// Structured paths carry the original interface port identity to foreign
// consumers. Ordinary identifiers containing '$' remain a single segment.
for (key, id) in &module_ids {
let Some(base) = analyzed.get(&key.name) else {
continue;
};
for port in &lowered_modules[id].port_order {
let variable = modules
.get_mut(id)
.unwrap()
.sim_module
.variables
.get_mut(port)
.unwrap();
if let Some((root, member)) = variable.path[0].split_once('$')
&& base.interface_ports.contains(root)
{
variable.path = vec![root.to_string(), member.to_string()];
}
if let Some(base) = analyzed.get(&key.name) {
restore_interface_member_paths(&mut modules.get_mut(id).unwrap().sim_module, base);
}
}
Ok(ExternalHierarchy {
Expand Down Expand Up @@ -1903,6 +1887,9 @@ fn schedule_sources_collecting(
.map(|(&module_id, lowered)| (module_id, lowered)),
&package_states,
)?;
for (key, id) in &module_ids {
restore_interface_member_paths(modules.get_mut(id).unwrap(), &analyzed[&key.name]);
}
let symbolic = SymbolicRtl {
modules,
module_names,
Expand All @@ -1922,6 +1909,24 @@ fn schedule_sources_collecting(
.map_err(FrontendError::from)
}

// Use elaboration provenance rather than treating every '$' as a separator:
// legal authored identifiers may themselves contain '$'.
fn restore_interface_member_paths(module: &mut SimModule, source: &AnalyzedSvModule) {
for variable in module.variables.values_mut() {
variable.path = variable
.path
.iter()
.flat_map(|segment| {
source
.interface_member_paths
.get(segment)
.cloned()
.unwrap_or_else(|| vec![segment.clone()])
})
.collect();
}
}

fn sv_specialization_limit_error(name: String) -> ParserError {
ParserError::unsupported(
sv::SV_FRONTEND_TRACKING_ISSUE,
Expand Down
28 changes: 26 additions & 2 deletions crates/celox-sv-analyzer/src/ast/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3069,14 +3069,26 @@ pub fn plan_interfaces(
sources: &[(&str, &Path)],
bindings: &[(String, String, Vec<(String, String)>)],
) -> Converted<NativeInterfacePlan> {
plan_interfaces_with_member_paths(sources, bindings).map(|(plan, _)| plan)
}

/// Original paths of flattened interface fields, indexed by bound module name.
pub type InterfaceMemberPaths = HashMap<String, HashMap<String, Vec<String>>>;

/// Build the interface plan and retain source member paths for reflection.
pub fn plan_interfaces_with_member_paths(
sources: &[(&str, &Path)],
bindings: &[(String, String, Vec<(String, String)>)],
) -> Converted<(NativeInterfacePlan, InterfaceMemberPaths)> {
let files = sources
.iter()
.map(|(code, path)| File::new(code, path))
.collect::<Converted<Vec<_>>>()?;
let design = Design::collect(&files)?;
let mut result = NativeInterfacePlan::default();
let mut reflected_paths = HashMap::default();
if design.interfaces.is_empty() {
return Ok(result);
return Ok((result, reflected_paths));
}
let mut queue = design
.modules
Expand Down Expand Up @@ -3402,6 +3414,18 @@ pub fn plan_interfaces(
queue.push((child_name.clone(), generic));
}
}
let mut member_paths = HashMap::default();
for (handle_name, declarations) in &handles.by_name {
for (_, handle) in declarations {
for member in &design.interfaces[&handle.interface].members {
member_paths.insert(
format!("{handle_name}{SEPARATOR}{}", member.name),
vec![handle_name.clone(), member.name.clone()],
);
}
}
}
reflected_paths.insert(bound_name.clone(), member_paths);
result.modules.insert(
bound_name,
NativeModulePlan {
Expand All @@ -3411,5 +3435,5 @@ pub fn plan_interfaces(
},
);
}
Ok(result)
Ok((result, reflected_paths))
}
4 changes: 3 additions & 1 deletion crates/celox-sv-analyzer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ pub mod syntax;
pub mod system_functions;
pub mod typecheck;

pub use ast::interfaces::{
InterfaceMemberPaths, NativeInterfacePlan, plan_interfaces, plan_interfaces_with_member_paths,
};
pub use ast::interfaces::{
InterfacePrototype, interface_prototypes, interface_prototypes_for,
interface_prototypes_with_options,
};
pub use ast::interfaces::{NativeInterfacePlan, plan_interfaces};
pub use ast::packages::Packages;
pub use ast::{ModuleInterface, ModuleInterfaces};
pub use ir::Ir;
Expand Down
115 changes: 115 additions & 0 deletions crates/celox-vpi/tests/basic_vpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2804,3 +2804,118 @@ fn callback_teardown_keeps_its_host_selected_until_dispatch_returns() {
assert!(info.argv.is_null());
}
}

#[cfg(feature = "systemverilog")]
#[test]
fn interface_members_are_navigable_after_native_image_round_trip() {
let _serial = lock_vpi();
let source = r#"
interface Axis;
logic [7:0] data, cost$usd;
logic valid, ready;
modport sink(input data, valid, output ready);
endinterface
module Child(Axis.sink s_axis, output logic [7:0] y);
assign s_axis.ready = 1;
assign y = s_axis.valid ? s_axis.data + 1 : 0;
endmodule
module Top(output logic [7:0] y);
Axis s_axis(), s$axis();
logic [7:0] cash$flow;
assign cash$flow = 3;
Child uut(s_axis, y);
endmodule
"#;
let simulator =
Simulator::from_sv_sources(vec![(source, std::path::Path::new("axis.sv"))], "Top")
.native_force_support(true)
.four_state(true)
.build_native()
.unwrap();
let container = simulator
.shared_code()
.program_image()
.to_container_bytes()
.unwrap();
let runtime = unsafe { NativeProgramInstance::from_attached_bytes(&container) }.unwrap();
drop(simulator);
let _session = install_runtime(runtime).unwrap();
unsafe {
let top = vpi_handle_by_name(c"Top".as_ptr(), ptr::null_mut());
let bus = vpi_handle_by_name(c"s_axis".as_ptr(), top);
assert!(!bus.is_null(), "the declared interface must have a scope");
let data = vpi_handle_by_name(c"data".as_ptr(), bus);
let valid = vpi_handle_by_name(c"valid".as_ptr(), bus);
let ready = vpi_handle_by_name(c"ready".as_ptr(), bus);
assert!(!data.is_null() && !valid.is_null() && !ready.is_null());
assert_eq!(CStr::from_ptr(vpi_get_str(VPI_NAME, data)), c"data");
assert_eq!(
CStr::from_ptr(vpi_get_str(VPI_FULL_NAME, data)),
c"Top.s_axis.data"
);
assert_eq!(vpi_get(VPI_SIZE, data), 8);
let parent = vpi_handle(VPI_SCOPE, data);
assert_eq!(
CStr::from_ptr(vpi_get_str(VPI_FULL_NAME, parent)),
c"Top.s_axis"
);
let child_data = vpi_handle_by_name(c"Top.uut.s_axis.data".as_ptr(), ptr::null_mut());
assert!(
!child_data.is_null(),
"interface ports must retain their paths too"
);
put_int(data, 41);
put_int(valid, 1);
let y = vpi_handle_by_name(c"y".as_ptr(), top);
let mut value = VpiValue {
format: VPI_INT_VAL,
value: VpiValueData { integer: 0 },
};
vpi_get_value(y, &mut value);
assert_eq!(value.value.integer, 42);
vpi_get_value(child_data, &mut value);
assert_eq!(
value.value.integer, 41,
"child member alias must see the deposited value"
);
let members = vpi_iterate(VPI_REG, bus);
let mut names = Vec::new();
loop {
let member = vpi_scan(members);
if member.is_null() {
break;
}
names.push(
CStr::from_ptr(vpi_get_str(VPI_NAME, member))
.to_str()
.unwrap()
.to_owned(),
);
vpi_free_object(member);
}
names.sort();
assert_eq!(names, ["cost$usd", "data", "ready", "valid"]);
let dollar_member = vpi_handle_by_name(c"cost$usd".as_ptr(), bus);
let dollar_interface = vpi_handle_by_name(c"Top.s$axis.data".as_ptr(), ptr::null_mut());
let dollar_signal = vpi_handle_by_name(c"Top.cash$flow".as_ptr(), ptr::null_mut());
assert!(
!dollar_member.is_null() && !dollar_interface.is_null() && !dollar_signal.is_null()
);
assert!(vpi_handle_by_name(c"Top.cash".as_ptr(), ptr::null_mut()).is_null());
for handle in [
top,
bus,
data,
valid,
ready,
parent,
child_data,
y,
dollar_member,
dollar_interface,
dollar_signal,
] {
vpi_free_object(handle);
}
}
}
Loading
Loading