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
14 changes: 14 additions & 0 deletions crates/celox-sv-analyzer/src/ast/declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
34 changes: 34 additions & 0 deletions crates/celox-sv-analyzer/src/ast/type_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, i128>,
aliases: &HashMap<String, Type>,
) -> Converted<bool> {
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,
Expand Down
5 changes: 5 additions & 0 deletions crates/celox-sv-syntax/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ pub struct Port {
pub data_type: Option<TypeId>,
pub dimensions: Vec<Dimension>,
pub default: Option<ExprId>,
/// 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<TypeId>,
pub tokens: TokenRange,
}

Expand Down
1 change: 1 addition & 0 deletions crates/celox-sv-syntax/src/parser/declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,7 @@ impl Parser<'_> {
data_type,
dimensions,
default,
declared_type: None,
tokens: self.range(start),
};
inherited = Some(port.clone());
Expand Down
47 changes: 29 additions & 18 deletions crates/celox-sv-syntax/src/parser/non_ansi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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<BodyItemId>,
Expand All @@ -201,7 +205,7 @@ impl Parser<'_> {
),
name: &str,
port_type: TypeId,
) -> Result<Option<(Option<String>, bool, TypeId)>> {
) -> Result<Option<(Option<String>, bool, TypeId, Option<TypeId>)>> {
let qualified = items
.iter()
.any(|&item| match self.body_items[item.0].kind {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<TypeId> {
/// `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<TypeId>)> {
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()
Expand All @@ -379,25 +387,28 @@ impl Parser<'_> {
})
.collect::<Vec<_>>()
};
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))
}
}
26 changes: 26 additions & 0 deletions crates/celox-test-suite/src/sv/cases/hierarchy.vtest
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
10 changes: 9 additions & 1 deletion crates/celox/tests/frontends/systemverilog/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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";
}
}
Loading
Loading