Skip to content
Merged
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
197 changes: 190 additions & 7 deletions crates/celox-sv-analyzer/src/ast/interface_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ pub(super) fn analyze_module(
true,
Some(&header_ports),
)?;
let mut caller_ports = caller.ports.clone();
let mut caller_symbols = caller.symbols.as_ref().unwrap().clone();
let _caller_scope = scope::install(&tree.state, Arc::new(caller_symbols.clone()));
let _functions = const_functions::install(&tree.state, caller.constant_functions.clone());
Expand Down Expand Up @@ -295,6 +296,7 @@ pub(super) fn analyze_module(
true,
Some(&header_ports),
)?;
caller_ports = caller.ports.clone();
caller_symbols = caller.symbols.unwrap();
generated = true;
let mut current = caller_symbols.clone();
Expand Down Expand Up @@ -324,17 +326,17 @@ pub(super) fn analyze_module(
binding_declaration_view(&view_ports, &instantiations, tree, &imported)?;
let mut header_imports = imported.clone();
header_imports.declarations_only = true;
caller_symbols = Module::from_selected_scope(
let caller = Module::from_selected_scope(
&tree.child(&header_tree, &overrides),
&name,
&overrides,
interfaces,
Arc::new(header_imports),
true,
Some(&header_ports),
)?
.symbols
.unwrap();
)?;
caller_ports = caller.ports.clone();
caller_symbols = caller.symbols.unwrap();
}
let mut current = caller_symbols.clone();
current.extend(&imported);
Expand All @@ -345,8 +347,12 @@ pub(super) fn analyze_module(
}
let _scope = scope::install(&tree.state, Arc::new(current.clone()));
let _functions = const_functions::install(&tree.state, current.constant_functions.clone());
// Exported scope symbols omit ordinary module ports. Keep the
// selected caller header available for connection width discovery.
let mut connection_ports = caller_ports.clone();
connection_ports.extend(current.ports.iter().cloned());
let mut dimensions = packed_dimensions_from_ports_and_signals(
&current.ports,
&connection_ports,
&current.signals,
&current.const_env,
&current.type_aliases,
Expand Down Expand Up @@ -416,10 +422,10 @@ pub(super) fn analyze_module(
.insert(handle.clone(), elements.clone());
}
}
for local_handle in elements.elements {
for (ordinal, local_handle) in elements.elements.iter().enumerate() {
let handle = generated_item.as_ref().map_or_else(
|| local_handle.clone(),
|item| item.instance_name(&local_handle),
|item| item.instance_name(local_handle),
);
imported
.interface_types
Expand All @@ -443,6 +449,14 @@ pub(super) fn analyze_module(
imported.declarations_only,
)
})?;
let port_state = module.constant_function_state();
let port_symbols = module.symbols.as_ref().unwrap();
let port_dimensions = packed_dimensions_from_ports_and_signals(
&module.ports,
&module.signals,
&port_symbols.const_env,
&port_symbols.type_aliases,
);
for port in &module.ports {
let signal = Signal {
name: port.name.clone(),
Expand All @@ -460,6 +474,70 @@ pub(super) fn analyze_module(
let Some(actual) = connection.actual_expr.clone() else {
continue;
};
let actual = if elements.shape.is_empty() {
actual
} else {
let port_shape = port_dimensions.get(&port.name).unwrap();
let port_width = lvalue_expr_type(
&port_state,
&LValue::Ident(port.name.clone()),
&port_dimensions,
)
.ok_or_else(|| unsupported("interface array port width"))?
.width;
let actual_width = expr_static_width(&tree.state, &actual, &dimensions)
.ok_or_else(|| unsupported("interface array connection width"))?;
let actual_shape = if let Some(root) =
connection_expression(authored, &port.name, Some(&interface))?
{
dimensions::operand_shape(root, tree, &dimensions)?
} else if let Expr::Ident(name) = &actual {
dimensions.get(name).cloned()
} else {
None
};
let unpacked_sizes =
|shape: &VariableDimensions,
state: &AnalysisState,
env: &HashMap<String, i128>| {
shape
.unpacked
.iter()
.map(|dimension| {
usize::try_from(eval_ast_const_expr(
state,
&dimension.width,
env,
)?)
.ok()
})
.collect::<Option<Vec<_>>>()
};
let actual_shape = actual_shape
.as_ref()
.map(|shape| unpacked_sizes(shape, &tree.state, &dimensions.const_env))
.unwrap_or_else(|| Some(Vec::new()))
.ok_or_else(|| unsupported("interface array actual shape"))?;
let port_shape =
unpacked_sizes(port_shape, &port_state, &port_dimensions.const_env)
.ok_or_else(|| unsupported("interface array port shape"))?;
let (origin, ascending) =
connection_slice_origin(&actual, &tree.state, &dimensions)?;
let shape = ConnectionShape {
unpacked: actual_shape,
width: actual_width,
origin,
ascending,
};
distribute_port_connection(
actual,
&shape,
&port_shape,
port_width,
&elements,
ordinal,
)?
};
let bound = format!("{handle}${}", port.name);
let assignment = match port.direction {
PortDirection::Input => Assignment::new(LValue::Ident(bound), actual),
Expand Down Expand Up @@ -849,6 +927,111 @@ fn formal_dimensions(
.ok_or_else(|| unsupported(format!("missing interface port `{port_name}`")))
}

struct ConnectionShape {
unpacked: Vec<usize>,
width: usize,
origin: ConstExpr,
ascending: bool,
}

/// Single packed dimensions retain authored indices in analyzer expressions;
/// unpacked and multi-packed selections already use flat storage coordinates.
fn connection_slice_origin(
actual: &Expr,
state: &AnalysisState,
dimensions: &PackedDimensions,
) -> Converted<(ConstExpr, bool)> {
let (name, selected_origin) = match actual {
Expr::Ident(name) => (name, None),
Expr::Select { expr, lsb, .. } => {
let Expr::Ident(name) = expr.as_ref() else {
return Ok((ConstExpr::Literal("0".into()), false));
};
(name, Some(lsb.clone()))
}
_ => return Ok((ConstExpr::Literal("0".into()), false)),
};
if let Some(shape) = dimensions.get(name)
&& shape.unpacked.is_empty()
&& let [dimension] = shape.packed.as_slice()
&& !dimension.normalize_single
{
let left = eval_ast_const_expr(state, &dimension.left, &dimensions.const_env)
.ok_or_else(|| unsupported("interface connection packed bound"))?;
let right = eval_ast_const_expr(state, &dimension.right, &dimensions.const_env)
.ok_or_else(|| unsupported("interface connection packed bound"))?;
return Ok((
selected_origin.unwrap_or_else(|| dimension.right.clone()),
left < right,
));
}
Ok((
selected_origin.unwrap_or_else(|| ConstExpr::Literal("0".into())),
false,
))
}

/// IEEE 1800-2023 23.3.3.5. Packed actuals distribute MSB-first;
/// unpacked actuals use the compiler's declaration-order element layout.
fn distribute_port_connection(
actual: Expr,
shape: &ConnectionShape,
port_shape: &[usize],
port_width: usize,
elements: &ArrayBinding,
ordinal: usize,
) -> Converted<Expr> {
let actual_shape = shape.unpacked.as_slice();
let actual_width = shape.width;
if actual_shape == port_shape && actual_width == port_width {
return Ok(actual);
}
let packed = actual_shape.is_empty() && port_shape.is_empty();
let distributed_shape = elements
.shape
.iter()
.chain(port_shape)
.copied()
.collect::<Vec<_>>();
if (!packed && actual_shape != distributed_shape)
|| port_width.checked_mul(elements.elements.len()) != Some(actual_width)
{
return Err(unsupported(format!(
"interface array connection shape/width: {actual_width} bits {actual_shape:?} for {} ports of {port_width} bits {port_shape:?}",
elements.elements.len(),
)));
}
let offset = if packed {
elements.elements.len() - ordinal - 1
} else {
ordinal
} * port_width;
let coordinate = |offset: usize| {
let offset = const_expr_from_i128(offset as i128);
if shape.ascending {
ConstExpr::Binary {
left: Box::new(shape.origin.clone()),
op: BinaryOp::Sub,
right: Box::new(offset),
}
} else {
add_expr(shape.origin.clone(), offset)
}
};
let msb = coordinate(offset + port_width - 1);
let lsb = coordinate(offset);
let expr = match actual {
Expr::Select { expr, .. } if matches!(expr.as_ref(), Expr::Ident(_)) => expr,
value => Box::new(value),
};
Ok(Expr::Select {
expr,
msb,
lsb,
signed: false,
})
}

fn connection_lvalue(expr: Expr) -> Converted<LValue> {
match expr {
Expr::Ident(name) => Ok(LValue::Ident(name)),
Expand Down
58 changes: 57 additions & 1 deletion crates/celox-sv-analyzer/src/ast/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3118,7 +3118,63 @@ pub fn plan_interfaces_with_member_paths(
.iter()
.map(|(code, path)| File::new(code, path))
.collect::<Converted<Vec<_>>>()?;
let design = Design::collect(&files)?;
// Local interface instances are bound from their original declarations by
// interface_binding. A structural plan is needed only for module interface
// ports; collecting full contracts here rejects ordinary interface ports.
let mut interface_names = HashSet::default();
let mut contract_names = HashSet::default();
let mut module_names = HashSet::default();
for file in &files {
let native = file.syntax_tree.declarations();
for (_, unit) in roots(&file.syntax_tree)? {
let name = native_name(&unit.name);
match unit.kind {
native::UnitKind::Interface => {
if unit.ports.is_empty() {
contract_names.insert(name.clone());
}
if !interface_names.insert(name.clone()) || module_names.contains(&name) {
return Err(AnalyzerError::DuplicateModule { name });
}
}
native::UnitKind::Module => {
if interface_names.contains(&name) {
return Err(AnalyzerError::DuplicateModule { name });
}
module_names.insert(name);
for port in &unit.ports {
if port.direction.is_some() || port.net_type.is_some() || port.variable {
continue;
}
if let Some(id) = port.data_type {
match &native.data_type(id).kind {
native::DataTypeKind::Interface {
name: Some(name), ..
} => {
contract_names.insert(native_name(name));
}
native::DataTypeKind::Named(path) if path.len() == 1 => {
contract_names.insert(native_name(&path[0].name));
}
_ => {}
}
}
}
}
_ => {}
}
}
}
for (_, _, supplied) in bindings {
contract_names.extend(supplied.iter().map(|(_, interface)| interface.clone()));
}
let design = if !module_names.is_empty() {
// Ordinary module contracts still need planning when local interfaces
// have ports. Collect those contracts independently of native locals.
Design::collect_selected(&files, Some(&contract_names))?
} else {
Design::collect(&files)?
Comment on lines +3175 to +3176

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Exclude port-bearing interfaces when contracts are also needed

When any module has an interface port, needs_plan is true and this falls back to Design::collect(&files), which calls InterfaceDecl::collect for every interface and rejects every interface with ordinary ports. Consequently, a design combining a traditional zero-port interface contract (for example, module Child(Bus b)) with the newly supported local interface Wire(input logic x) array still fails in plan_interfaces before connection distribution runs. Keep contract planning enabled for the module interface ports without collecting unrelated port-bearing local interfaces.

Useful? React with 👍 / 👎.

};
let mut result = NativeInterfacePlan::default();
let mut reflected_paths = HashMap::default();
if design.interfaces.is_empty() {
Expand Down
32 changes: 32 additions & 0 deletions crates/celox-sv-analyzer/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5265,6 +5265,38 @@ fn native_interface_array_ports_reject_incompatible_shapes() {
);
}

#[test]
fn rejects_mismatched_interface_array_connection_widths_and_shapes() {
for declaration in [
"logic [2:0] value; I lanes[1:0](.value(value));",
"logic [0:0] value [3]; I lanes[1:0](.value(value));",
"logic value [1][2]; I lanes[1:0](.value(value));",
] {
let text = format!(
"interface I(input logic value); endinterface module Top; {declaration} endmodule"
);
let parsed =
ParsedSource::parse(&text, Path::new("invalid-interface-array-connection.sv")).unwrap();
let packages = ParsedSource::analyze_packages(&[&parsed]).unwrap();
let error = parsed
.analyze_module(
"Top",
&HashMap::default(),
&[],
&[],
&ModuleInterfaces::default(),
&packages,
)
.unwrap_err();
assert!(
error
.to_string()
.contains("interface array connection shape/width"),
"{declaration}: {error}"
);
}
}

#[test]
fn native_interface_port_views_do_not_repeat_instance_initializers() {
let module = native_interface_module(
Expand Down
Loading
Loading