diff --git a/crates/celox-sv-analyzer/src/ast/declarations.rs b/crates/celox-sv-analyzer/src/ast/declarations.rs index 56bc4a11bb5..0aa6d394bc8 100644 --- a/crates/celox-sv-analyzer/src/ast/declarations.rs +++ b/crates/celox-sv-analyzer/src/ast/declarations.rs @@ -37,6 +37,20 @@ 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, + session, + 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 6e24559762b..301805c5d12 100644 --- a/crates/celox-sv-analyzer/src/ast/type_binding.rs +++ b/crates/celox-sv-analyzer/src/ast/type_binding.rs @@ -86,6 +86,40 @@ pub(super) fn port_type( bind(id, tree, session, &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<'_>, + session: &mut Session, + 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, session, &context)?; + let bounds = |range: &PackedRange| { + constants::eval_ast_const_expr(&tree.functions, range.left(), env).zip( + constants::eval_ast_const_expr(&tree.functions, 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 b37fef66046..4ae7ec33857 100644 --- a/crates/celox-sv-syntax/src/nodes.rs +++ b/crates/celox-sv-syntax/src/nodes.rs @@ -322,6 +322,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 529706cb3ad..9e4e54aaed9 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 891856da116..ceb2ca02dff 100644 --- a/crates/celox-test-suite/src/sv/cases/hierarchy.vtest +++ b/crates/celox-test-suite/src/sv/cases/hierarchy.vtest @@ -1760,3 +1760,29 @@ (modify (set x 255)) (assert_eq first 255) (assert_eq last 191)) + +; 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 8c893ac3d94..a925b6e234d 100644 --- a/crates/celox/tests/frontends/systemverilog/hierarchy.rs +++ b/crates/celox/tests/frontends/systemverilog/hierarchy.rs @@ -746,7 +746,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", @@ -781,4 +785,8 @@ sv_backends! { fn many_sibling_parameter_specializations(sim) { @case "hierarchy::many_sibling_parameter_specializations"; } + + fn non_ansi_port_ranges_compare_bounds(sim) { + @case "hierarchy::non_ansi_port_ranges_compare_bounds"; + } }