From ba12d6659609d1558b964393a44766d3f234e9ea Mon Sep 17 00:00:00 2001 From: tignear Date: Sat, 24 Oct 2026 14:36:35 +0900 Subject: [PATCH] fix(sv): bind unpacked array values in module parameter arguments Taxi forwarded unpacked parameter elements to scalar child parameters, but the constant-binding path retained the array name after resolving the selection coordinates. Apply the existing immutable initializer and preserve the selected width, signedness and X/Z mask. Keep whole-array conditional arguments in the typed array binding path. Check both branch shapes, including the unselected branch, then select the array for a fully resolved integral predicate. Nested conditions are handled iteratively; ambiguous and nonintegral array predicates remain unsupported. Add six shared regressions covering generated numeric/string elements, signedness, bounds, package multidimensional arrays, 129-bit values, specializations, four-state conversion, and whole-array conditionals. Consumer tests reject runtime arguments and incompatible inactive branches. Validation after syncing master c3177672e7 (except where noted): - Full SystemVerilog suite: 4,425 passed, 20 existing ignored. Focused regressions: 32 passed across five backends and two rejection tests. - Analyzer: 796 passed, 3 existing ignored; frontend: 5 passed. Affected doctests passed. The shared suite passed all 30 tests before syncing master. - Affected analyzer/frontend libraries and tests and the SV consumer harness passed Clippy with -D warnings; formatting and diff checks passed. - All four retained original minimal reproductions now return FRONTEND_OK. - Slang 12 elaborates all six exact shared sources without semantic errors and confirms the expected child parameter values. - Verilator 5.052: three passed; one package-array compile error; one conditional-array ordering mismatch; one four-state case unsupported by the adapter. The ordering expectation follows IEEE 1800-2023 7.6 and agrees with Slang. Standalone lint reproduces the package-array rejection. - Icarus 13 rejects all six sources because unpacked array parameters are unsupported. No baseline or exclusions were changed to conceal failures. Before syncing with current master, replayed the same 25 Taxi configurations with original RTL/parameters, release frontend binaries, two workers, 120-second limits, two-state mode and the previous diagnostic settings. All 18 GT_TX_DIFFCTRL and seven STAT_PREFIX_STR blockers advance to undriven GT nets: 19 gt_rxdata and six gt_rxbyteisaligned. There are zero timeouts. These remain frontend errors; no complete FPGA-core configuration or upstream cocotb test pass is claimed. All 188 resolved upstream SV sources match the earlier hashes. Unrelated parameter arithmetic widening and wide scalar parameter array initializer limitations are retained as separate follow-up reproductions. Change-Id: Icd56e125c141961d6e79242b1fad03ccdb79b992 --- .../src/ast/constant_binding.rs | 15 +- crates/celox-sv-analyzer/src/ast/instances.rs | 86 ++++++++++- crates/celox-sv-analyzer/src/ast/selects.rs | 3 + .../src/sv/cases/hierarchy.vtest | 144 ++++++++++++++++++ .../frontends/systemverilog/hierarchy.rs | 73 +++++++++ 5 files changed, 315 insertions(+), 6 deletions(-) diff --git a/crates/celox-sv-analyzer/src/ast/constant_binding.rs b/crates/celox-sv-analyzer/src/ast/constant_binding.rs index 125bcc4a2ca..091cd7568a8 100644 --- a/crates/celox-sv-analyzer/src/ast/constant_binding.rs +++ b/crates/celox-sv-analyzer/src/ast/constant_binding.rs @@ -267,9 +267,20 @@ fn bind_in_context( }) { let value = indexed_expression(id, native, tree, query_dimensions)?; - values.push( + let value = if query_dimensions + .array_parameter_values + .contains_key(&name_key) + { + selects::indexed_expression_value( + &tree.state, + value, + query_dimensions, + ) + } else { expr_to_index_const(&tree.state, value, query_dimensions) - .ok_or_else(|| unsupported("parameter selection shape"))?, + }; + values.push( + value.ok_or_else(|| unsupported("parameter selection shape"))?, ); continue; } diff --git a/crates/celox-sv-analyzer/src/ast/instances.rs b/crates/celox-sv-analyzer/src/ast/instances.rs index 2f10cf8e479..f1442ad9380 100644 --- a/crates/celox-sv-analyzer/src/ast/instances.rs +++ b/crates/celox-sv-analyzer/src/ast/instances.rs @@ -344,6 +344,60 @@ pub(crate) fn normalize_identifier(text: &str) -> String { } } +/// Keep whole-array conditional arguments out of the scalar constant path. +/// Traverse branch types iteratively, including branches a constant condition +/// does not select. Array sizes and integral element types must agree. +fn array_argument_shape( + root: celox_sv_syntax::ExprId, + tree: &AnalysisContext<'_>, + dims: &PackedDimensions, +) -> Option { + use celox_sv_syntax::{ExprId, ExprKind}; + enum Job { + Enter(ExprId), + Conditional, + } + let native = tree.declarations(); + let mut jobs = vec![Job::Enter(root)]; + let mut values = Vec::::new(); + while let Some(job) = jobs.pop() { + match job { + Job::Enter(id) => { + let id = expression_binding::ungroup(id, native); + if let ExprKind::Conditional { + then_expression, + else_expression, + .. + } = native.expression(id).kind + { + jobs.push(Job::Conditional); + jobs.push(Job::Enter(else_expression)); + jobs.push(Job::Enter(then_expression)); + } else { + values.push(array_compatibility::native_unpacked_shape( + id, native, dims, + )?); + } + } + Job::Conditional => { + let right = values.pop()?; + let left = values.pop()?; + let left_type = array_compatibility::unpacked_array_type(&tree.state, &left, dims)?; + let right_type = + array_compatibility::unpacked_array_type(&tree.state, &right, dims)?; + if left_type != right_type + || left.packed != right.packed + || left.members != right.members + { + return None; + } + values.push(left); + } + } + } + values.pop() +} + /// One `.name(expr)` binding: a `parameter type` when `expr` is a data type or /// the name of a type, a value otherwise. fn parameter_override( @@ -383,11 +437,35 @@ fn parameter_override( } pending.extend(parameters::skipped::children(id, declarations)); } - if let Some(shape) = - array_compatibility::native_unpacked_shape(native, declarations, packed_dimensions) - { + if let Some(shape) = array_argument_shape(native, syntax_tree, packed_dimensions) { + // Aggregate conditional expressions require equivalent branch types + // (11.4.11). A known condition selects a whole array before element + // binding; treating it as a packed mux would use the wrong X merge rule. + let mut selected = root; + while let ExprKind::Conditional { + condition, + then_expression, + else_expression, + } = declarations.expression(selected).kind + { + let condition = constant_binding::bind(condition, syntax_tree, packed_dimensions)? + .and_then(|value| { + eval_ast_const_expr(&syntax_tree.state, &value, &packed_dimensions.const_env) + }) + .ok_or_else(|| { + unsupported("nonconstant or ambiguous condition in array parameter argument") + })?; + selected = expression_binding::ungroup( + if condition != 0 { + then_expression + } else { + else_expression + }, + declarations, + ); + } let pattern = - bound_patterns::from_array_parameter(native, syntax_tree, packed_dimensions, &shape)?; + bound_patterns::from_array_parameter(selected, syntax_tree, packed_dimensions, &shape)?; let mut bound = ParameterOverride::new(name, None); bound.bound_pattern = Some(pattern); return Ok(bound); diff --git a/crates/celox-sv-analyzer/src/ast/selects.rs b/crates/celox-sv-analyzer/src/ast/selects.rs index 3945c4f45eb..c0c475936f4 100644 --- a/crates/celox-sv-analyzer/src/ast/selects.rs +++ b/crates/celox-sv-analyzer/src/ast/selects.rs @@ -1058,6 +1058,9 @@ fn substitute_indexed_parameter_values( expression: Expr, dimensions: &PackedDimensions, ) -> Expr { + // Unpacked parameter values use the declared array layout, while selections + // have already been translated to bit offsets in that layout. + let expression = substitute_expr_idents(expression, &dimensions.array_parameter_values); // Known values come from the current environment, which may include a // generate index shadowing a parameter. Supply expressions only for values // absent from that numeric environment, including X/Z parameters. diff --git a/crates/celox-test-suite/src/sv/cases/hierarchy.vtest b/crates/celox-test-suite/src/sv/cases/hierarchy.vtest index 3644c826083..5fa3f849094 100644 --- a/crates/celox-test-suite/src/sv/cases/hierarchy.vtest +++ b/crates/celox-test-suite/src/sv/cases/hierarchy.vtest @@ -1246,3 +1246,147 @@ (top Top) (assert_eq default_rows 0x34123412) (assert_eq explicit_rows 0xcdabcdab)) + + +; IEEE 1800-2023 6.20.2: an aggregate parameter member can be used in an expression. +(case array_parameter_elements_forward_generated_values + (source "array_parameter_elements.sv" #" + module Number #(parameter logic [4:0] P=0)(output wire [4:0] y); + assign y=P; + endmodule + module Text #(parameter logic [63:0] P="MAC")(output wire [63:0] y); + assign y=P; + endmodule + module Relay #(parameter logic [4:0] N[6:7]='{0,0}, + parameter logic [63:0] S[-2:-1]='{"D0","D1"}) + (output wire [9:0] numbers, output wire [127:0] strings); + for(genvar i=6;i<=7;i++) begin:g + Number #(.P(N[i])) n(.y(numbers[(i-6)*5 +: 5])); + Text #(.P(S[i-8])) s(.y(strings[(i-6)*64 +: 64])); + end + endmodule + module Top(output wire [9:0] numbers, output wire [127:0] strings); + localparam logic [4:0] N[2]='{5'h12,5'h1b}; + localparam logic [63:0] S[2]='{"A0","A1"}; + Relay #(.N(N),.S(S)) relay(.numbers(numbers),.strings(strings)); + endmodule + "#) + (top Top) (eval) + (assert_eq numbers 0x372) + (assert_eq strings 0x00000000000041310000000000004130)) + +(case array_parameter_elements_keep_signed_element_types + (source "signed_array_parameter_elements.sv" #" + module Sink #(parameter P=0, parameter logic signed [15:0] Q=0) + (output wire [31:0] value, width, output wire [15:0] sum, output wire negative); + assign value=P; + assign width=$bits(P); + assign sum=Q; + assign negative=P<0; + endmodule + module Top(output wire [31:0] value, width, output wire [15:0] sum, + output wire negative); + localparam logic signed [4:0] A[3:2]='{5'sd15,-5'sd2}; + Sink #(.P(A[2]),.Q(A[3]-5'sd1)) sink(.value(value),.width(width),.sum(sum),.negative(negative)); + endmodule + "#) + (top Top) (eval) + (assert_eq value 0xfffffffe) + (assert_eq width 5) + (assert_eq negative 1) + (assert_eq sum 14)) + +(case array_parameter_elements_select_package_multidimensional_values + (source "package_array_parameter_elements.sv" #" + package Values; + localparam logic [1:0][3:0] TABLE[3:2][-1:0]= + '{'{8'h12,8'h34},'{8'h56,8'h78}}; + endpackage + module Sink #(parameter P=0)(output wire [7:0] y, output wire [31:0] width); + assign y=P; + assign width=$bits(P); + endmodule + module Top(output wire [7:0] whole, nibble, grouped, + output wire [31:0] whole_width, nibble_width, grouped_width); + import Values::*; + Sink #(.P(TABLE[2][0])) a(.y(whole),.width(whole_width)); + Sink #(.P(TABLE[3][-1][1])) b(.y(nibble),.width(nibble_width)); + Sink #(.P((Values::TABLE[3][-1]))) c(.y(grouped),.width(grouped_width)); + endmodule + "#) + (top Top) (eval) + (assert_eq whole 0x78) (assert_eq whole_width 8) + (assert_eq nibble 1) (assert_eq nibble_width 4) + (assert_eq grouped 0x12) (assert_eq grouped_width 8)) + +(case array_parameter_elements_preserve_four_state_and_conversion + (four_state) + (source "state_array_parameter_elements.sv" #" + module Sink #(parameter logic [7:0] U=0, parameter logic signed [7:0] S=0, + parameter bit [7:0] B=0)(output wire [7:0] u,s,b); + assign u=U; + assign s=S; + assign b=B; + endmodule + module Top(output wire [15:0] unsigned_value, signed_value, two_state_value); + localparam logic [3:0] A[-1:0]='{4'b10xz,4'bz01x}; + localparam logic signed [3:0] S[-1:0]='{4'b10xz,4'bz01x}; + for(genvar i=-1;i<=0;i++) begin:g + Sink #(.U(A[i]),.S(S[i]),.B(A[i])) sink( + .u(unsigned_value[(0-i)*8 +: 8]), + .s(signed_value[(0-i)*8 +: 8]), + .b(two_state_value[(0-i)*8 +: 8])); + end + endmodule + "#) + (top Top) (eval) + (assert_eq unsigned_value (fourstate 0x0a03 0x0309)) + (assert_eq signed_value (fourstate 0xfa03 0x03f9)) + (assert_eq two_state_value 0x0802)) + + +(case array_parameter_elements_preserve_wide_specializations + (source "wide_array_parameter_elements.sv" #" + module Sink #(parameter P=0)(output wire [128:0] value, output wire [31:0] width); + assign value=P; + assign width=$bits(P); + endmodule + module Parent #(parameter SELECT=0) + (output wire [128:0] value, output wire [31:0] width); + localparam logic [128:0] TABLE[-2:-1]='{129'h100000000000000000000000000000001,129'd2}; + Sink #(.P(TABLE[SELECT-2])) sink(.value(value),.width(width)); + endmodule + module Top(output wire [128:0] first, second, output wire [31:0] first_width, second_width); + Parent #(.SELECT(0)) a(.value(first),.width(first_width)); + Parent #(.SELECT(1)) b(.value(second),.width(second_width)); + endmodule + "#) + (top Top) (eval) + (assert_eq first 0x100000000000000000000000000000001) + (assert_eq second 2) + (assert_eq first_width 129) (assert_eq second_width 129)) + + +(case array_parameter_elements_select_conditional_whole_arrays + (source "conditional_array_parameter_elements.sv" #" + module Sink #(parameter logic [63:0] P=0)(output wire [63:0] y); + assign y=P; + endmodule + module Relay #(parameter logic [63:0] A[3:2]='{0,0})(output wire [127:0] y); + for(genvar i=2;i<=3;i++) begin:g + Sink #(.P(A[i])) sink(.y(y[(3-i)*64 +: 64])); + end + endmodule + module Parent #(parameter SELECT=1)(output wire [127:0] y); + localparam logic [63:0] A[2]='{"A0","A1"}; + localparam logic [63:0] B[2]='{"B0","B1"}; + Relay #(.A(SELECT ? A : (SELECT==0 ? B : A))) relay(.y(y)); + endmodule + module Top(output wire [127:0] first, second); + Parent #(.SELECT(1)) a(.y(first)); + Parent #(.SELECT(0)) b(.y(second)); + endmodule + "#) + (top Top) (eval) + (assert_eq first 0x00000000000041310000000000004130) + (assert_eq second 0x00000000000042310000000000004230)) diff --git a/crates/celox/tests/frontends/systemverilog/hierarchy.rs b/crates/celox/tests/frontends/systemverilog/hierarchy.rs index 51913fe38fb..13d628a419a 100644 --- a/crates/celox/tests/frontends/systemverilog/hierarchy.rs +++ b/crates/celox/tests/frontends/systemverilog/hierarchy.rs @@ -1,6 +1,25 @@ use super::*; sv_backends! { + fn array_parameter_elements_select_conditional_whole_arrays(sim) { + @case "hierarchy::array_parameter_elements_select_conditional_whole_arrays"; + } + fn array_parameter_elements_preserve_wide_specializations(sim) { + @case "hierarchy::array_parameter_elements_preserve_wide_specializations"; + } + fn array_parameter_elements_forward_generated_values(sim) { + @case "hierarchy::array_parameter_elements_forward_generated_values"; + } + fn array_parameter_elements_keep_signed_element_types(sim) { + @case "hierarchy::array_parameter_elements_keep_signed_element_types"; + } + fn array_parameter_elements_select_package_multidimensional_values(sim) { + @case "hierarchy::array_parameter_elements_select_package_multidimensional_values"; + } + fn array_parameter_elements_preserve_four_state_and_conversion(sim) { + @case "hierarchy::array_parameter_elements_preserve_four_state_and_conversion"; + } + fn port_pattern_constant_unpacked_defaults(sim) { @case "hierarchy::port_pattern_constant_unpacked_defaults"; } @@ -614,3 +633,57 @@ fn rejects_recursive_port_pattern_defaults_for_packed_unions() { ); } } + +#[test] +fn array_parameter_elements_reject_runtime_values() { + for (declaration, expression) in [ + ("logic [4:0] A[2]='{5'd3,5'd4};", "A[0]"), + ("localparam logic [4:0] A[2]='{5'd3,5'd4};", "A[index]"), + ] { + let source = format!( + "module Child #(parameter P=0)(output wire[4:0] y);assign y=P;endmodule\n\ + module Top(input int index,output wire[4:0] y);{declaration}\n\ + Child #(.P({expression})) child(.y(y));endmodule" + ); + let error = match Simulator::from_sv_sources( + vec![(&source, Path::new("runtime_array_parameter.sv"))], + "Top", + ) + .build_cranelift() + { + Err(error) => error, + Ok(_) => panic!("runtime parameter argument {expression} must be rejected"), + }; + assert!( + error.to_string().contains("parameter"), + "{expression}: {error}" + ); + } +} + +#[test] +fn array_parameter_elements_check_unselected_conditional_branch_types() { + for declaration in [ + "logic[4:0] B[2]='{1,2};", + "logic[3:0] B[3]='{1,2,3};", + "logic signed[3:0] B[2]='{1,2};", + "bit[3:0] B[2]='{1,2};", + ] { + let source = format!( + "module Child #(parameter logic[3:0] Q[2]='{{0,0}})(output wire[3:0] y);\n\ + assign y=Q[0];endmodule\n\ + module Top(output wire[3:0] y);localparam logic[3:0] A[2]='{{3,4}};\n\ + localparam {declaration}\n\ + Child #(.Q(1 ? A : B)) child(.y(y));endmodule" + ); + assert!( + Simulator::from_sv_sources( + vec![(&source, Path::new("conditional_array_type.sv"))], + "Top" + ) + .build_cranelift() + .is_err(), + "unselected conditional branch with {declaration} must be rejected" + ); + } +}