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
35 changes: 30 additions & 5 deletions crates/celox-sv-analyzer/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,32 @@ impl Module {
}
packed_dimensions.subroutine_param_shapes = Arc::new(subroutine_shapes.clone());
packed_dimensions.subroutine_param_names = Arc::new(subroutine_params.clone());
// Array parameters retain a constant value as well as their signal layout.
// Bind their values in declaration order before child parameter arguments.
for process in &imported.initial_processes {
for statement in &process.body {
if let Stmt::Assign {
lhs: LValue::Ident(name),
rhs,
..
} = statement
{
packed_dimensions
.array_parameter_values
.insert(name.clone(), rhs.clone());
}
}
}
let mut array_initial_processes = Vec::new();
for parameter in &array_parameters {
let process = parameter.initial_process(syntax_tree, &packed_dimensions)?;
if let Stmt::Assign { rhs, .. } = &process.body[0] {
packed_dimensions
.array_parameter_values
.insert(parameter.signal.name.clone(), rhs.clone());
}
array_initial_processes.push(process);
}
let mut instances =
instances_from_scope(syntax_tree, &const_env, &packed_dimensions, interfaces)?;
let mut instance_names = HashSet::default();
Expand Down Expand Up @@ -1108,11 +1134,7 @@ impl Module {
)?;
comb_processes.extend(imported.comb_processes.iter().cloned());
ff_processes.extend(imported.ff_processes.iter().cloned());
let mut initial_processes = Vec::new();
for parameter in &array_parameters {
initial_processes
.push(parameter.initial_process(syntax_tree, &packed_dimensions)?);
}
let mut initial_processes = array_initial_processes;
initial_processes.extend(imported.initial_processes.iter().cloned());
initial_processes.extend(procedural::initial_processes_from_scope(
syntax_tree,
Expand Down Expand Up @@ -2645,6 +2667,8 @@ struct PackedDimensions {
function_return_types: HashMap<String, FunctionReturnMetadata>,
functions: Arc<HashMap<String, Function>>,
parameter_values: SharedMap<Expr>,
/// Constant array initializers for binding child parameter values.
array_parameter_values: SharedMap<Expr>,
expression_signedness: ScopedMap<bool>,
constant_indexed_base: bool,
/// The current lexical scope has already collected its visible declarations.
Expand All @@ -2671,6 +2695,7 @@ impl PackedDimensions {
function_return_types: HashMap::default(),
functions: Arc::default(),
parameter_values: SharedMap::default(),
array_parameter_values: SharedMap::default(),
expression_signedness: ScopedMap::default(),
constant_indexed_base: false,
scope_types_complete: false,
Expand Down
2 changes: 1 addition & 1 deletion crates/celox-sv-analyzer/src/ast/array_compatibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use typecheck::UnpackedArrayType;

/// The type of an unpacked array of `shape`, or `None` when `shape` is not an
/// unpacked array or its type cannot be resolved.
fn unpacked_array_type(
pub(super) fn unpacked_array_type(
state: &AnalysisState,
shape: &VariableDimensions,
dims: &PackedDimensions,
Expand Down
36 changes: 35 additions & 1 deletion crates/celox-sv-analyzer/src/ast/bound_patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ pub struct BoundPattern {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum Items {
Value(Expr),
ArrayValue {
value: Expr,
ty: typecheck::UnpackedArrayType,
},
List(Vec<Value>),
Repeat(usize, Vec<Value>),
Keyed(Vec<(Key, Value)>),
Expand Down Expand Up @@ -44,6 +48,24 @@ pub(super) fn from_param(
}
}

pub(super) fn from_array_parameter(
root: celox_sv_syntax::ExprId,
tree: &AnalysisContext<'_>,
dims: &PackedDimensions,
shape: &VariableDimensions,
) -> Converted<BoundPattern> {
let ty = array_compatibility::unpacked_array_type(&tree.state, shape, dims)
.ok_or_else(|| unsupported("array parameter argument type"))?;
let value = substitute_expr_idents(
expression_binding::bind(root, tree, dims)?,
&dims.array_parameter_values,
);
let value = frozen(&tree.state, value, dims)?;
Ok(BoundPattern {
items: Items::ArrayValue { value, ty },
})
}

fn frozen(state: &AnalysisState, expr: Expr, dims: &PackedDimensions) -> Converted<Expr> {
let freeze = |expr| frozen(state, expr, dims).map(Box::new);
Ok(match expr {
Expand Down Expand Up @@ -174,6 +196,18 @@ impl BoundPattern {
shape: &VariableDimensions,
dims: &PackedDimensions,
) -> Converted<Expr> {
if let Items::ArrayValue { value, ty } = &self.items {
let target = array_compatibility::unpacked_array_type(state, shape, dims)
.ok_or_else(|| unsupported("unpacked array parameter supplied to a scalar"))?;
if !ty.is_assignment_compatible_with(&target) {
return Err(AnalyzerError::IncompatibleUnpackedArray {
context: "module parameter override".into(),
actual: ty.clone(),
target,
});
}
return Ok(value.clone());
}
if let Items::Value(value) = &self.items {
return Ok(value.clone());
}
Expand Down Expand Up @@ -269,7 +303,7 @@ impl BoundPattern {
keys = (0..width).rev().map(|i| Key::Index(i as i128)).collect();
}
let selected = match &self.items {
Items::Value(value) => return Ok(value.clone()),
Items::Value(value) | Items::ArrayValue { value, .. } => return Ok(value.clone()),
Items::Repeat(count, values) => {
if count.checked_mul(values.len()) != Some(shapes.len()) {
return Err(unsupported(
Expand Down
5 changes: 5 additions & 0 deletions crates/celox-sv-analyzer/src/ast/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ impl Item {
} else {
self.parameter_literals(&dimensions.parameter_values).into()
};
if !self.shadowed.is_empty() || !self.names.is_empty() {
local
.array_parameter_values
.retain(|name, _| !self.shadowed.contains(name) && !self.names.contains_key(name));
}
// Ordinary module items keep the module's function bindings. Rewriting
// every function body for every item costs O(items * function size).
if !self.shadowed.is_empty() || !self.names.is_empty() {
Expand Down
9 changes: 9 additions & 0 deletions crates/celox-sv-analyzer/src/ast/instances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,15 @@ fn parameter_override(
}
pending.extend(parameters::skipped::children(id, declarations));
}
if let Some(shape) =
array_compatibility::native_unpacked_shape(native, declarations, packed_dimensions)
{
let pattern =
bound_patterns::from_array_parameter(native, syntax_tree, packed_dimensions, &shape)?;
let mut bound = ParameterOverride::new(name, None);
bound.bound_pattern = Some(pattern);
return Ok(bound);
}
if let Some((type_name, dimensions)) = type_binding::argument_alias(native, declarations)
&& packed_dimensions.type_aliases.contains_key(&type_name)
&& !dimensions.is_empty()
Expand Down
2 changes: 1 addition & 1 deletion crates/celox-sv-analyzer/src/typecheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct IntegralLiteral {
}

/// The type of a fixed-size unpacked array of an integral element type.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct UnpackedArrayType {
/// The element count of each unpacked dimension, slowest varying first.
pub dims: Vec<usize>,
Expand Down
11 changes: 11 additions & 0 deletions crates/celox-test-suite/LIMITATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,17 @@ records the native-runner result; the case stays enabled on all Celox backends.

## SystemVerilog suite

Named unpacked array parameter forwarding is checked against IEEE 1800-2023
6.20.2 and 7.6. Verilator 5.052 passes the whole-array forwarding case, but
reverses elements across opposite declaration directions and rejects a constant
subarray argument. Slang 12.0 accepts all four cases and confirms their parameter
values. Icarus 13.0 explicitly reports unpacked array parameters as unsupported.
Only these exact tool/case combinations are excluded; Celox runs every assertion.
The focused [Verilator](verification/repros/named_array_parameters_verilator.json),
[Icarus](verification/repros/named_array_parameters_icarus.json), and
[Slang](verification/repros/named_array_parameters_slang.json) observations retain
the failures and independent elaborated values.

The new `$dist_uniform` seed-width case is excluded for Verilator 5.052 and
Icarus 13.0. Verilator generates an invalid C++ reference binding for converted
seed arguments; Icarus rejects the `shortint` seed at startup. IEEE 1800-2023
Expand Down
86 changes: 86 additions & 0 deletions crates/celox-test-suite/src/sv/cases/hierarchy.vtest
Original file line number Diff line number Diff line change
Expand Up @@ -664,3 +664,89 @@
(top Top)
(assert_eq a 0xdc)
(assert_eq b 0xedc))

; IEEE 1800-2023 6.20.2 and 7.6: array values bind in the caller and
; corresponding elements follow declaration order, independently of bounds.
(case named_array_parameters_follow_caller_specialization
(source "named_array_parameters.sv" #"
module Child #(parameter byte Q[3:2] = '{1, 2})
(input logic sel, output logic [15:0] y, output byte selected);
localparam byte TABLE[2] = '{99, 98};
assign y = {Q[3], Q[2]};
assign selected = Q[3-sel];
endmodule
module Parent #(parameter byte B = -3)
(input logic sel, output logic [15:0] y, output byte selected);
localparam byte TABLE[0:1] = '{B, B-2};
Child #(.Q((TABLE))) child(sel, y, selected);
endmodule
module Top(input logic sel, output logic [15:0] a, b,
output byte a_selected, b_selected);
Parent #(.B(-3)) first(sel, a, a_selected);
Parent #(.B(7)) second(sel, b, b_selected);
endmodule
"#)
(top Top)
(assert_eq a 0xfdfb)
(assert_eq b 0x0705)
(assert_eq a_selected 0xfd)
(assert_eq b_selected 7)
(modify (set sel 1))
(assert_eq a_selected 0xfb)
(assert_eq b_selected 5))

(case named_array_parameters_keep_four_state_values
(four_state)
(source "named_array_parameter_states.sv" #"
module Child #(parameter logic [3:0] Q[2] = '{0, 0})
(output logic [7:0] y);
assign y = {Q[0], Q[1]};
endmodule
module Top(output logic [7:0] y);
localparam logic [3:0] TABLE[2] = '{4'b10xz, 4'bz01x};
Child #(.Q(TABLE)) child(y);
endmodule
"#)
(top Top)
(assert_eq y (fourstate 0xa3 0x39)))

(case named_array_parameters_forward_package_subarrays
(source "named_array_parameter_packages.sv" #"
package Values;
localparam logic [7:0] TABLE[2][2] = '{'{8'h12, 8'h34}, '{8'h56, 8'h78}};
endpackage
module Child #(parameter logic [7:0] Q[2] = '{0, 0})
(output logic [15:0] y);
assign y = {Q[0], Q[1]};
endmodule
module Middle #(parameter logic [7:0] Q[2][2] = '{default: 0})
(output logic [15:0] y);
Child #(.Q(Q[1])) child(y);
endmodule
module Top(output logic [15:0] y);
import Values::*;
Middle #(.Q(TABLE)) middle(y);
endmodule
"#)
(top Top)
(assert_eq y 0x5678))

(case named_array_parameters_forward_whole_values
(source "named_array_parameter_whole_values.sv" #"
module Child #(parameter logic [7:0] Q[2] = '{0, 0})
(input logic sel, output logic [7:0] y);
assign y = Q[sel];
endmodule
module Middle #(parameter logic [7:0] Q[2] = '{0, 0})
(input logic sel, output logic [7:0] y);
Child #(.Q(Q)) child(sel, y);
endmodule
module Top(input logic sel, output logic [7:0] y);
localparam logic [7:0] TABLE[2] = '{8'h12, 8'ha5};
Middle #(.Q(TABLE)) middle(sel, y);
endmodule
"#)
(top Top)
(assert_eq y 0x12)
(modify (set sel 1))
(assert_eq y 0xa5))
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"cases": [
{
"category": "Hierarchy",
"detail": "Icarus build exit status: 3; see <case>/build.log\n<case>/source_0.sv:2: sorry: unpacked array parameters are not supported yet.\n<case>/source_0.sv:4: sorry: unpacked array parameters are not supported yet.\n<case>/source_0.sv:10: sorry: unpacked array parameters are not supported yet.\n",
"expectation": "Simulation",
"name": "hierarchy::named_array_parameters_follow_caller_specialization",
"phase": "compile",
"status": "compile_error",
"stronger_than_sv": false,
"tag_reasons": {},
"tags": []
},
{
"category": "Hierarchy",
"detail": "Icarus build exit status: 4; see <case>/build.log\n<case>/source_0.sv:3: sorry: unpacked array parameters are not supported yet.\n<case>/source_0.sv:5: sorry: unpacked array parameters are not supported yet.\n<case>/source_0.sv:9: syntax error\nI give up.\n",
"expectation": "Simulation",
"name": "hierarchy::named_array_parameters_forward_package_subarrays",
"phase": "compile",
"status": "compile_error",
"stronger_than_sv": false,
"tag_reasons": {},
"tags": []
},
{
"category": "Hierarchy",
"detail": "Icarus build exit status: 3; see <case>/build.log\n<case>/source_0.sv:2: sorry: unpacked array parameters are not supported yet.\n<case>/source_0.sv:6: sorry: unpacked array parameters are not supported yet.\n<case>/source_0.sv:11: sorry: unpacked array parameters are not supported yet.\n",
"expectation": "Simulation",
"name": "hierarchy::named_array_parameters_forward_whole_values",
"phase": "compile",
"status": "compile_error",
"stronger_than_sv": false,
"tag_reasons": {},
"tags": []
},
{
"category": "Hierarchy",
"detail": "Icarus build exit status: 2; see <case>/build.log\n<case>/source_0.sv:2: sorry: unpacked array parameters are not supported yet.\n<case>/source_0.sv:7: sorry: unpacked array parameters are not supported yet.\n",
"expectation": "Simulation",
"name": "hierarchy::named_array_parameters_keep_four_state_values",
"phase": "compile",
"status": "compile_error",
"stronger_than_sv": false,
"tag_reasons": {},
"tags": []
}
],
"counts": {
"compile_error": 4,
"emission_error": 0,
"ignored": 0,
"mismatch": 0,
"passed": 0,
"rejected": 0,
"runtime_error": 0,
"unexpected_accept": 0,
"unsupported": 0
},
"include_ignored": true,
"schema_version": 3,
"suite": "sv",
"suite_version": "0.11.0",
"tool": "icarus",
"version": "Icarus Verilog version 13.0 (stable) (v13_0)",
"note": "Focused observations before exact-case exclusions for named array parameter forwarding; shared assertions are preserved."
}
Loading
Loading