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
31 changes: 24 additions & 7 deletions crates/celox-sv-analyzer/src/ast/interface_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,13 @@ pub(super) fn analyze_module(
return Ok(None);
}
let array_scope = if interface_ports.iter().any(|port| port.array_rank != 0) {
let (header_ports, header_tree) =
binding_declaration_view(&view_ports, &instantiations, tree, &imported)?;
let (header_ports, header_tree) = binding_declaration_view(
&view_ports,
&instantiations,
interface_ports,
tree,
&imported,
)?;
let mut header_imports = imported.clone();
header_imports.declarations_only = true;
Some(Module::from_selected_scope(
Expand Down Expand Up @@ -257,8 +262,13 @@ pub(super) fn analyze_module(
}
}
// Resolve the caller's declarations before evaluating instance overrides.
let (header_ports, header_tree) =
binding_declaration_view(&view_ports, &instantiations, tree, &imported)?;
let (header_ports, header_tree) = binding_declaration_view(
&view_ports,
&instantiations,
interface_ports,
tree,
&imported,
)?;
let mut header_imports = imported.clone();
header_imports.declarations_only = true;
let caller = Module::from_selected_scope(
Expand Down Expand Up @@ -324,8 +334,13 @@ pub(super) fn analyze_module(
if generated_item.is_none() {
// Newly bound instances may unlock declarations used by the next
// instance's overrides. Keep only still-unbound dependencies out.
let (header_ports, header_tree) =
binding_declaration_view(&view_ports, &instantiations, tree, &imported)?;
let (header_ports, header_tree) = binding_declaration_view(
&view_ports,
&instantiations,
interface_ports,
tree,
&imported,
)?;
let mut header_imports = imported.clone();
header_imports.declarations_only = true;
let caller = Module::from_selected_scope(
Expand Down Expand Up @@ -817,11 +832,12 @@ fn native_body_items(tree: &SyntaxTree) -> Converted<Vec<celox_sv_syntax::BodyIt
Ok(items)
}

/// Delay declarations that reference local interface instances (and declarations
/// Delay declarations that reference unbound interface instances or ports (and declarations
/// depending on those names) until the instances have populated semantic scopes.
fn binding_declaration_view<'a>(
selected: &port_owners::Selection<'a>,
instances: &[celox_sv_syntax::BodyItemId],
ports: &[interfaces::NativePortPlan],
tree: &AnalysisContext<'_>,
imported: &scope::ScopeSymbols,
) -> Converted<(port_owners::Selection<'a>, SyntaxTree)> {
Expand All @@ -831,6 +847,7 @@ fn binding_declaration_view<'a>(
.filter_map(|id| instances::declaration_from_item(*id, tree).ok())
.flat_map(|instance| instance.instances.iter())
.map(|instance| instances::normalize_identifier(&instance.name.spelling))
.chain(ports.iter().map(|port| port.name.clone()))
.filter(|name| {
!imported.interface_types.contains_key(name)
&& !imported.interface_arrays.contains_key(name)
Expand Down
36 changes: 36 additions & 0 deletions crates/celox-test-suite/src/sv/cases/interfaces.vtest
Original file line number Diff line number Diff line change
Expand Up @@ -946,3 +946,39 @@
(<< (trunc 8 (+ value 4)) 8) (trunc 8 (+ value 5))))
(assert_eq first (| (<< (trunc 8 (+ value 1)) 8) (trunc 8 (+ value 2))))
(assert_eq second (| (<< (trunc 8 (+ value 3)) 8) (trunc 8 (+ value 4))))))

(case defers_interface_dependent_declarations_before_binding_array_ports
(source "interface_array_declarations.sv" #"
interface Bus #(parameter int W = 8);
logic [W-1:0] data;
modport sink(input data);
endinterface
module Reader #(parameter int LEFT = 3, RIGHT = 2)(
Bus.sink a[LEFT:RIGHT], Bus.sink b,
input logic [b.W-1:0] delta, output wire [31:0] y);
localparam SCALAR_W = b.W;
localparam ARRAY_W = a[LEFT].W;
localparam TOTAL_W = SCALAR_W + ARRAY_W;
typedef logic [TOTAL_W-1:0] sum_t;
sum_t sum;
assign sum = b.data + a[LEFT].data + a[RIGHT].data + delta;
assign y = sum + TOTAL_W;
endmodule
module Top(input logic [7:0] value,
output wire [31:0] descending, ascending);
Bus #(.W(8)) scalar();
Bus #(.W(12)) down[3:2](), up[5:6]();
assign scalar.data = value;
assign down[3].data = 12'h100;
assign down[2].data = 12'h200;
assign up[5].data = 12'h400;
assign up[6].data = 12'h800;
Reader d(down, scalar, value, descending);
Reader #(.LEFT(5), .RIGHT(6)) u(up, scalar, value, ascending);
endmodule
"#)
(top Top)
(for value (list 0 1 0x7f 0xff)
(modify (set value value))
(assert_eq descending (+ (* value 2) 0x300 20))
(assert_eq ascending (+ (* value 2) 0xc00 20))))
3 changes: 3 additions & 0 deletions crates/celox/tests/frontends/systemverilog/interfaces.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::*;

sv_backends! {
fn defers_interface_dependent_declarations_before_binding_array_ports(sim) {
@case "interfaces::defers_interface_dependent_declarations_before_binding_array_ports";
}
fn connects_interface_array_slices_in_declaration_order(sim) {
@case "interfaces::connects_interface_array_slices_in_declaration_order";
}
Expand Down
Loading