From 5876ec800b02d19e944703d5388c1ca653a91b38 Mon Sep 17 00:00:00 2001 From: tignear Date: Mon, 26 Oct 2026 18:34:40 +0900 Subject: [PATCH] fix(sv): compare the bounds, not the spelling, of non-ANSI port ranges A non-ANSI port can be declared again in a net or variable declaration, and if that declaration is a vector, the two ranges "shall be identical" (IEEE 1800-2023 23.2.2.1). The parser compared the token text of the two ranges, so equal ranges written differently were rejected. rtlmeter's XuanTie E902 and E906 declare `output [79:0] busif_kid_clicintattr_sel;` and `wire [CLICINTNUM-1:0] busif_kid_clicintattr_sel;` with `CLICINTNUM` = 64+16, and the compile failed. The parser cannot evaluate parameters, so the check moves to elaboration: - When the spellings differ, the parser keeps the net or variable declaration's type for the port and records the port declaration's type in the new `Port::declared_type`. - `ports_from_scope` evaluates both ranges with the module's parameter values and rejects the port when the bounds differ: "the ranges of a non-ANSI port declaration and its net or variable declaration of `x` differ". - A port declaration without a range completed by a vector declaration is still rejected by the parser. Closes #2126. - Shared case `hierarchy::non_ansi_port_ranges_compare_bounds`: the E902 shape (a macro-built parameter in the net's range), matching Icarus and Verilator. - `rejects_malformed_non_ansi_port_declarations` also rejects ranges that differ through a parameter (`input [3:0] a; wire [N-1:0] a;` with N = 8). Change-Id: I0fc04b280c929c9f50d191276e5cc31fb4949ad5 --- .../celox-sv-analyzer/src/ast/declarations.rs | 13 +++++ .../celox-sv-analyzer/src/ast/type_binding.rs | 33 +++++++++++++ crates/celox-sv-syntax/src/nodes.rs | 5 ++ .../src/parser/declarations.rs | 1 + crates/celox-sv-syntax/src/parser/non_ansi.rs | 47 ++++++++++++------- .../src/sv/cases/hierarchy.vtest | 27 +++++++++++ .../frontends/systemverilog/hierarchy.rs | 12 ++++- 7 files changed, 119 insertions(+), 19 deletions(-) diff --git a/crates/celox-sv-analyzer/src/ast/declarations.rs b/crates/celox-sv-analyzer/src/ast/declarations.rs index aa2cb256ead..e3a001b7c84 100644 --- a/crates/celox-sv-analyzer/src/ast/declarations.rs +++ b/crates/celox-sv-analyzer/src/ast/declarations.rs @@ -34,6 +34,19 @@ pub(super) fn ports_from_scope( None if native.data_type.is_none() => inherited_type.clone(), None => return Err(unsupported("unsupported port data type")), }; + if !type_binding::declared_port_range_matches( + native, + &base, + syntax_tree, + const_env, + type_aliases, + )? { + return Err(AnalyzerError::Parse(format!( + "the ranges of a non-ANSI port declaration and its net or variable \ + declaration of `{}` differ", + native_port_name(native)? + ))); + } inherited_direction = direction; inherited_type = base.clone(); let ty = type_with_unpacked_ranges( diff --git a/crates/celox-sv-analyzer/src/ast/type_binding.rs b/crates/celox-sv-analyzer/src/ast/type_binding.rs index 5d2ecd8d7ef..980793e8810 100644 --- a/crates/celox-sv-analyzer/src/ast/type_binding.rs +++ b/crates/celox-sv-analyzer/src/ast/type_binding.rs @@ -64,6 +64,39 @@ pub(super) fn port_type( bind(id, tree, &context) } +/// Whether the range of the port declaration of a non-ANSI port, which its +/// net or variable declaration repeats in other words, has the bounds of the +/// port's type (IEEE 1800-2023 23.2.2.1). +pub(super) fn declared_port_range_matches( + port: &celox_sv_syntax::Port, + ty: &Type, + tree: &AnalysisContext<'_>, + env: &HashMap, + aliases: &HashMap, +) -> Converted { + let Some(id) = port.declared_type else { + return Ok(true); + }; + let context = PackedDimensions::new(HashMap::default(), env, aliases); + let data = tree.declarations().data_type(id); + let declared = binding::packed_ranges(&data.packed_dimensions, tree, &context)?; + let bounds = |range: &PackedRange| { + constants::eval_ast_const_expr(&tree.state, range.left(), env).zip( + constants::eval_ast_const_expr(&tree.state, range.right(), env), + ) + }; + Ok(declared.len() == ty.packed_ranges.len() + && declared + .iter() + .zip(&ty.packed_ranges) + .all( + |(declared, range)| match (bounds(declared), bounds(range)) { + (Some(declared), Some(range)) => declared == range, + _ => declared == range, + }, + )) +} + /// Formal/return implicit types use scalar logic rather than an unsized local. pub(super) fn implicit_subroutine_type( id: TypeId, diff --git a/crates/celox-sv-syntax/src/nodes.rs b/crates/celox-sv-syntax/src/nodes.rs index d77bc1ad138..09359f8bcd3 100644 --- a/crates/celox-sv-syntax/src/nodes.rs +++ b/crates/celox-sv-syntax/src/nodes.rs @@ -311,6 +311,11 @@ pub struct Port { pub data_type: Option, pub dimensions: Vec, pub default: Option, + /// The type of the port declaration of a non-ANSI port whose net or + /// variable declaration repeats its range in other words. The ranges + /// must have the same bounds (IEEE 1800-2023 23.2.2.1), which takes + /// parameter values to check. + pub declared_type: Option, pub tokens: TokenRange, } diff --git a/crates/celox-sv-syntax/src/parser/declarations.rs b/crates/celox-sv-syntax/src/parser/declarations.rs index 5e7dedaa740..5db5ef2a24f 100644 --- a/crates/celox-sv-syntax/src/parser/declarations.rs +++ b/crates/celox-sv-syntax/src/parser/declarations.rs @@ -1324,6 +1324,7 @@ impl Parser<'_> { data_type, dimensions, default, + declared_type: None, tokens: self.range(start), }; inherited = Some(port.clone()); diff --git a/crates/celox-sv-syntax/src/parser/non_ansi.rs b/crates/celox-sv-syntax/src/parser/non_ansi.rs index 1a1560d916d..c20aa9a2dc3 100644 --- a/crates/celox-sv-syntax/src/parser/non_ansi.rs +++ b/crates/celox-sv-syntax/src/parser/non_ansi.rs @@ -72,7 +72,7 @@ impl Parser<'_> { return Err(self.error(format!("port `{name}` has no port declaration"))); }; let (_, declaration) = declarations.swap_remove(index); - let (net_type, variable, data_type) = if declaration.net_type.is_some() + let (net_type, variable, data_type, declared_type) = if declaration.net_type.is_some() || declaration.variable || !matches!( self.types[declaration.data_type.0].kind, @@ -89,6 +89,7 @@ impl Parser<'_> { declaration.net_type, declaration.variable, declaration.data_type, + None, ) } else { // The port is completed by a net or variable declaration of @@ -104,13 +105,14 @@ impl Parser<'_> { declaration.data_type, )? { Some(object) => object, - None => (None, false, declaration.data_type), + None => (None, false, declaration.data_type, None), } }; port.direction = Some(declaration.direction); port.net_type = net_type; port.variable = variable; port.data_type = Some(data_type); + port.declared_type = declared_type; port.dimensions = declaration.dimensions; port.default = declaration.default; port.tokens = declaration.tokens; @@ -187,10 +189,12 @@ impl Parser<'_> { } /// Remove the declarator of `name` from the module-scope net or variable - /// declarations, returning the port's net type, `var` and type. The - /// port declaration's packed range and signing carry over to a type - /// that has neither (IEEE 1800-2023 23.2.2.1). A net declaration - /// assignment stays as the continuous assignment it is (10.3.1). + /// declarations, returning the port's net type, `var` and type, and the + /// port declaration's type when its range must be checked against the + /// object's (see [`Self::merge_port_type`]). The port declaration's + /// packed range and signing carry over to a type that has neither (IEEE + /// 1800-2023 23.2.2.1). A net declaration assignment stays as the + /// continuous assignment it is (10.3.1). fn take_port_object( &mut self, items: &mut Vec, @@ -201,7 +205,7 @@ impl Parser<'_> { ), name: &str, port_type: TypeId, - ) -> Result, bool, TypeId)>> { + ) -> Result, bool, TypeId, Option)>> { let qualified = items .iter() .any(|&item| match self.body_items[item.0].kind { @@ -297,11 +301,8 @@ impl Parser<'_> { } (None, false) => {} } - Ok(Some(( - net_type, - variable, - self.merge_port_type(port_type, object_type)?, - ))) + let (data_type, declared_type) = self.merge_port_type(port_type, object_type)?; + Ok(Some((net_type, variable, data_type, declared_type))) } /// The continuous assignment `name = right` of a net declaration @@ -357,16 +358,23 @@ impl Parser<'_> { } /// The type of a port declared by a port declaration of type - /// `port_type` and a net or variable declaration of type `object_type`. - fn merge_port_type(&mut self, port_type: TypeId, object_type: TypeId) -> Result { + /// `port_type` and a net or variable declaration of type `object_type`, + /// and `port_type` when both give a range in other words: the ranges + /// must be identical (IEEE 1800-2023 23.2.2.1), so their bounds are + /// compared once parameters have values. + fn merge_port_type( + &mut self, + port_type: TypeId, + object_type: TypeId, + ) -> Result<(TypeId, Option)> { let port = self.types[port_type.0].clone(); let mut object = self.types[object_type.0].clone(); let mut changed = false; + let mut declared = None; if object.packed_dimensions.is_empty() { changed |= !port.packed_dimensions.is_empty(); object.packed_dimensions = port.packed_dimensions.clone(); } else { - // A vector net or variable repeats the port's range exactly. let text = |dimensions: &[Dimension]| { dimensions .iter() @@ -379,25 +387,28 @@ impl Parser<'_> { }) .collect::>() }; - if text(&port.packed_dimensions) != text(&object.packed_dimensions) { + if port.packed_dimensions.is_empty() { return Err(self.error( "the ranges of a non-ANSI port declaration and its net or variable \ declaration differ", )); } + if text(&port.packed_dimensions) != text(&object.packed_dimensions) { + declared = Some(port_type); + } } if port.signed == Some(true) && object.signed != Some(true) { object.signed = Some(true); changed = true; } if !changed { - return Ok(object_type); + return Ok((object_type, declared)); } if self.types.len() >= 1_000_000 { return Err(self.unsupported("native type arena exceeds one million nodes")); } let id = TypeId(self.types.len()); self.types.push(object); - Ok(id) + Ok((id, declared)) } } diff --git a/crates/celox-test-suite/src/sv/cases/hierarchy.vtest b/crates/celox-test-suite/src/sv/cases/hierarchy.vtest index 1dc7a3c7ed2..57ac6fe38f9 100644 --- a/crates/celox-test-suite/src/sv/cases/hierarchy.vtest +++ b/crates/celox-test-suite/src/sv/cases/hierarchy.vtest @@ -1684,3 +1684,30 @@ (modify (set oe 0) (set d 0)) (assert_eq from_pad (fourstate 0 1)) (assert_eq from_source 0)) + +; The range of a non-ANSI port declaration and of the net declaration that +; completes it must be identical (IEEE 1800-2023 23.2.2.1): the bounds, not +; the spelling, as in E902's `cr_clic_busif` (#2126). +(case non_ansi_port_ranges_compare_bounds + (source "non_ansi_ranges.sv" #" + `define EXT_INT_NUM 64 + `define CLIC_INTNUM `EXT_INT_NUM+16 + module child(o); + parameter N = `CLIC_INTNUM; + output [79:0] o; + wire [N-1:0] o; + assign o = {N{1'b1}}; + endmodule + module Top; + wire [79:0] o; + child c(o); + initial begin + #1 $display("%h", o); + $finish; + end + endmodule + "#) + (top Top) + (run_to_finish) + (expect_output "ffffffffffffffffffff\n")) + diff --git a/crates/celox/tests/frontends/systemverilog/hierarchy.rs b/crates/celox/tests/frontends/systemverilog/hierarchy.rs index 617a4470cd9..d245d663078 100644 --- a/crates/celox/tests/frontends/systemverilog/hierarchy.rs +++ b/crates/celox/tests/frontends/systemverilog/hierarchy.rs @@ -740,7 +740,11 @@ fn rejects_malformed_non_ansi_port_declarations() { ), ( "module Top(a); input [3:0] a; wire [7:0] a; endmodule", - "its net or variable declaration differ", + "its net or variable declaration of `a` differ", + ), + ( + "module Top(a); parameter N = 8; input [3:0] a; wire [N-1:0] a; endmodule", + "its net or variable declaration of `a` differ", ), ( "module Top(a); input a; wire [3:0] a; endmodule", @@ -770,3 +774,9 @@ fn rejects_malformed_non_ansi_port_declarations() { assert!(error.contains(message), "{source}: {error}"); } } + +sv_backends! { + fn non_ansi_port_ranges_compare_bounds(sim) { + @case "hierarchy::non_ansi_port_ranges_compare_bounds"; + } +}