diff --git a/crates/celox-frontend-sv/src/lowering/ff.rs b/crates/celox-frontend-sv/src/lowering/ff.rs index 3c4c5a0d7d4..7306b081347 100644 --- a/crates/celox-frontend-sv/src/lowering/ff.rs +++ b/crates/celox-frontend-sv/src/lowering/ff.rs @@ -7244,6 +7244,11 @@ impl Ff<'_, '_> { }) { return Err(unsupported("wait condition reading a static task argument")); } + // A write of a class property does not wake a waiting process yet, so + // a condition that reads one would never be checked again. + if let Some(op) = heap_read(self.m, condition) { + return Err(unsupported(format!("{op} in a wait condition"))); + } let mut ids: Vec<_> = names .iter() .filter_map(|name| self.m.id(name)) @@ -7471,6 +7476,66 @@ fn subroutine_reads(pm: &ProcModule<'_>, expr: &sv::ir::Expr, names: &mut HashSe } } +/// The name of an operation in `expr` that reads the class object heap, +/// directly or through the functions it calls: a class property, or the +/// random mode of one. Container contents are values of their handles. The +/// default of an argument counts only where a call omits the argument. +fn heap_read(pm: &ProcModule<'_>, expr: &sv::ir::Expr) -> Option { + let mut pending = vec![expr.clone()]; + let mut calls = Vec::new(); + let mut visited = HashSet::default(); + let mut defaults = HashSet::default(); + loop { + while let Some(expr) = pending.pop() { + let found = std::cell::RefCell::new(None); + expr_calls(&expr, &|callee| { + if let sv::ir::Callee::Object(op) = callee + && matches!( + **op, + sv::ir::ObjectOp::Load(_) + | sv::ir::ObjectOp::ModeGet(..) + | sv::ir::ObjectOp::ConstraintModeGet(_) + ) + { + found.borrow_mut().get_or_insert(op.to_string()); + } + false + }); + if let Some(op) = found.into_inner() { + return Some(op); + } + collect_calls(&expr, &mut calls); + } + let (callee, args) = calls.pop()?; + let sv::ir::Callee::Subroutine(name) = callee else { + continue; + }; + let Some(subroutine) = pm.subroutine(&name) else { + continue; + }; + for (position, param) in subroutine.params.iter().enumerate() { + if matches!(args.get(position), None | Some(None)) + && let Some(default) = ¶m.default + && defaults.insert((name.clone(), position)) + { + pending.push(default.clone()); + } + } + if !visited.insert(name) { + continue; + } + for stmt in &subroutine.body { + let mut stmt = stmt.clone(); + stmt.walk(&mut |stmt| stmt_calls(stmt, &mut calls)); + stmt.visit_mut( + &mut |expr| pending.push(expr.clone()), + &mut |_| {}, + &mut |_| {}, + ); + } + } +} + /// The name of a call in `expr` that has an effect or reads the state of the /// simulation, directly or through the functions it calls: an event /// expression must be a value of the variables it reads. diff --git a/crates/celox-test-suite/LIMITATIONS.md b/crates/celox-test-suite/LIMITATIONS.md index 3774d7cd147..323381f68bb 100644 --- a/crates/celox-test-suite/LIMITATIONS.md +++ b/crates/celox-test-suite/LIMITATIONS.md @@ -619,6 +619,7 @@ four-state expectations; the assertions remain enabled on all Celox variants. | `sv_verilator_program_exit` | execute | verilator | 1 | Verilator 5.052 continues after `$exit` and keeps the program's other `initial` procedures (24.7). Icarus does not run the case; xezim 0.11.3 agrees with the expected output. | | `sv_verilator_program_items` | compile | verilator | 2 | Verilator 5.052 rejects an `always` procedure and a module instance in a program with syntax errors, which the run cannot count as language rejections. Both are illegal (IEEE 1800-2023 24.3); Icarus 13.0 rejects them with diagnostics that name the rule. | | `sv_icarus_iff_event_controls` | compile | icarus | 15 | Icarus 13.0 rejects `iff` in an event control with "syntax error" and "Malformed event control expression." IEEE 1800-2023 9.4.2.3 and A.6.5 permit `@(posedge clk iff en)`; [Standalone probe](verification/repros/sv_iff_event_controls_icarus.json); Verilator 5.052 runs the cases and gives the asserted counts. | +| `sv_icarus_static_property_wait` | execute | icarus | 1 | Icarus 13.0 compiles a `wait` on a static class property in a class task but vvp cannot load the result ("unresolved vvp_net reference", "Program not runnable"); IEEE 1800-2023 8.9 and 9.4.3 make the property one variable that the wait watches. [Standalone probe](verification/repros/sv_static_property_wait_icarus.json); Verilator 5.052 and xezim 0.11.3 pass the case. | | `sv_verilator_iff_late_conditions` | execute | verilator | 4 | Verilator 5.052 judges an `iff` condition after the process that writes the event signal suspends, not at the write: with `clk = 1; en = 0;` from en = 1 it counts no event, with nonblocking updates `clk <= 1; en <= 0;` from en = 1 it counts none either, and an always_comb that writes `en = a; clk = a; en = 0;` gives no event of its `clk` write while `en` is 1. IEEE 1800-2023 9.4.2.3 judges the condition when the event occurs, and 10.4.2 applies nonblocking updates in issue order, so all three are events. [Standalone probes](verification/repros/sv_iff_late_conditions_verilator.json) print n=0, n=1 and n=0 in Verilator where the spec gives 1, 2 and 1; Icarus 13.0 rejects `iff` in event controls. | The [system-function edge-case evidence](verification/repros/system_function_edge_cases.json) diff --git a/crates/celox-test-suite/src/sv/cases/class.vtest b/crates/celox-test-suite/src/sv/cases/class.vtest index 32147016334..88e604db58c 100644 --- a/crates/celox-test-suite/src/sv/cases/class.vtest +++ b/crates/celox-test-suite/src/sv/cases/class.vtest @@ -2806,3 +2806,28 @@ module Top(output int a, output int b); endmodule "#) (top Top) (run_until 0) (assert_eq a 8) (assert_eq b 8)) + +(case waits_on_static_properties + ; A static property is a variable of the declaring scope, so a wait in a + ; method wakes when another process writes it (IEEE 1800-2023 8.9, 9.4.3). + (source "class.sv" #" +class C; + static int s_state; + task wait_for(int s); + wait (s_state >= s); + $display("reached %0d at %0d", s, $time); + endtask +endclass +module Top; + C c; + initial begin + c = new; + fork + c.wait_for(2); + begin #1 c.s_state = 1; #1 c.s_state = 2; end + join + end +endmodule +"#) + (top Top) (run_until 5) + (expect_output "reached 2 at 2\n")) diff --git a/crates/celox-test-suite/verification/repros/sv_static_property_wait_icarus.json b/crates/celox-test-suite/verification/repros/sv_static_property_wait_icarus.json new file mode 100644 index 00000000000..a2f10b87245 --- /dev/null +++ b/crates/celox-test-suite/verification/repros/sv_static_property_wait_icarus.json @@ -0,0 +1,16 @@ +{ + "tool": "icarus", + "version": "Icarus Verilog version 13.0 (stable) (v13_0)", + "source": "sv_static_property_wait_icarus.sv", + "standard": "IEEE 1800-2023 8.9, 9.4.3", + "command": "iverilog -g2012 -s Top -o sp.vvp sv_static_property_wait_icarus.sv && vvp sp.vvp", + "expected_output": "reached 2 at 2", + "observations": [ + { + "build_exit_code": 0, + "exit_code": 2, + "output": "unresolved vvp_net reference: v0x5555558f2f70_0\nunresolved vvp_net reference: v0x5555558f3050_0\ncompile_cleanup: 2 unresolved items\nsp.vvp: Program not runnable, 2 errors.\n" + } + ], + "note": "A task of a class waits on a static property of the class, which another process writes through a handle. Icarus 13.0 compiles the design but vvp cannot load it (unresolved nets). Verilator 5.052, xezim 0.11.3 and Celox print reached 2 at 2." +} diff --git a/crates/celox-test-suite/verification/repros/sv_static_property_wait_icarus.sv b/crates/celox-test-suite/verification/repros/sv_static_property_wait_icarus.sv new file mode 100644 index 00000000000..f32aec9dad8 --- /dev/null +++ b/crates/celox-test-suite/verification/repros/sv_static_property_wait_icarus.sv @@ -0,0 +1,17 @@ +class C; + static int s_state; + task wait_for(int s); + wait (s_state >= s); + $display("reached %0d at %0d", s, $time); + endtask +endclass +module Top; + C c; + initial begin + c = new; + fork + c.wait_for(2); + begin #1 c.s_state = 1; #1 c.s_state = 2; end + join + end +endmodule diff --git a/crates/celox-test-suite/verification/sv/icarus/class.json b/crates/celox-test-suite/verification/sv/icarus/class.json index 798ee76f9f8..5b45280df28 100644 --- a/crates/celox-test-suite/verification/sv/icarus/class.json +++ b/crates/celox-test-suite/verification/sv/icarus/class.json @@ -1692,6 +1692,29 @@ "stronger_than_sv": false, "tag_reasons": {}, "tags": [] + }, + { + "category": "Types", + "detail": "Icarus 13.0 compiles a `wait` on a static class property in a class task but vvp cannot load the result (\"unresolved vvp_net reference\", \"Program not runnable\"). IEEE 1800-2023 8.9 and 9.4.3 make the property one variable that the wait watches. Verilator 5.052 and xezim 0.11.3 pass the case.", + "expectation": "Simulation", + "known_issue": { + "id": "sv_icarus_static_property_wait", + "category": "simulator_runtime_limitation", + "phase": "execute", + "observed_version": "Icarus Verilog version 13.0 (stable) (v13_0)", + "reason": "Icarus 13.0 compiles a `wait` on a static class property in a class task but vvp cannot load the result (\"unresolved vvp_net reference\", \"Program not runnable\"). IEEE 1800-2023 8.9 and 9.4.3 make the property one variable that the wait watches. Verilator 5.052 and xezim 0.11.3 pass the case.", + "upstream": [], + "evidence": [ + "verification/repros/sv_static_property_wait_icarus.json", + "LIMITATIONS.md#systemverilog-suite" + ] + }, + "name": "class::waits_on_static_properties", + "phase": "skip", + "status": "ignored", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] } ] } diff --git a/crates/celox-test-suite/verification/sv/limitations.json b/crates/celox-test-suite/verification/sv/limitations.json index 9e3d93bcf81..1ed492b94c2 100644 --- a/crates/celox-test-suite/verification/sv/limitations.json +++ b/crates/celox-test-suite/verification/sv/limitations.json @@ -2769,6 +2769,23 @@ ] } }, + { + "id": "sv_icarus_static_property_wait", + "category": "simulator_runtime_limitation", + "phase": "execute", + "observed_version": "Icarus Verilog version 13.0 (stable) (v13_0)", + "reason": "Icarus 13.0 compiles a `wait` on a static class property in a class task but vvp cannot load the result (\"unresolved vvp_net reference\", \"Program not runnable\"). IEEE 1800-2023 8.9 and 9.4.3 make the property one variable that the wait watches. Verilator 5.052 and xezim 0.11.3 pass the case.", + "upstream": [], + "evidence": [ + "verification/repros/sv_static_property_wait_icarus.json", + "LIMITATIONS.md#systemverilog-suite" + ], + "cases": { + "icarus": [ + "class::waits_on_static_properties" + ] + } + }, { "id": "sv_verilator_iff_late_conditions", "category": "simulator_disagreement", diff --git a/crates/celox-test-suite/verification/sv/verilator/class.json b/crates/celox-test-suite/verification/sv/verilator/class.json index 07fbee05dad..42f2fc7c4d9 100644 --- a/crates/celox-test-suite/verification/sv/verilator/class.json +++ b/crates/celox-test-suite/verification/sv/verilator/class.json @@ -831,6 +831,17 @@ "stronger_than_sv": false, "tag_reasons": {}, "tags": [] + }, + { + "category": "Types", + "detail": "", + "expectation": "Simulation", + "name": "class::waits_on_static_properties", + "phase": "execute", + "status": "passed", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] } ] } diff --git a/crates/celox-test-suite/verification/sv/xezim/class.json b/crates/celox-test-suite/verification/sv/xezim/class.json index 239b5e1837a..6ad38174bf9 100644 --- a/crates/celox-test-suite/verification/sv/xezim/class.json +++ b/crates/celox-test-suite/verification/sv/xezim/class.json @@ -648,6 +648,17 @@ "stronger_than_sv": false, "tag_reasons": {}, "tags": [] + }, + { + "category": "Types", + "detail": "", + "expectation": "Simulation", + "name": "class::waits_on_static_properties", + "phase": "execute", + "status": "passed", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] } ] } diff --git a/crates/celox/tests/frontends/systemverilog/class.rs b/crates/celox/tests/frontends/systemverilog/class.rs index c7d1a3e9e7c..d17b3770a43 100644 --- a/crates/celox/tests/frontends/systemverilog/class.rs +++ b/crates/celox/tests/frontends/systemverilog/class.rs @@ -6,6 +6,7 @@ sv_backends! { fn class_imported_member_type_dimensions(sim) { @case "class::imported_member_type_dimensions"; } fn class_imported_member_type_inheritance(sim) { @case "class::imported_member_type_inheritance"; } fn class_package_constant_class_defaults(sim) { @case "class::package_constant_class_defaults"; } + fn class_waits_on_static_properties(sim) { @case "class::waits_on_static_properties"; } fn class_package_constant_type_arguments(sim) { @case "class::package_constant_type_arguments"; } fn class_named_aggregate_type_arguments(sim) { @case "class::named_aggregate_type_arguments"; } fn class_generate_scope_enum_type_arguments(sim) { @case "class::generate_scope_enum_type_arguments"; } @@ -211,6 +212,103 @@ sv_backends! { } } +/// A write of a class property does not wake a process waiting on it, so a +/// wait whose condition reads one is rejected instead of never waking. The +/// condition reads it in a method through `this`, through a handle, through +/// a queue held in a property and through a function. +#[test] +fn waits_on_class_properties_are_unsupported() { + let waits = [ + "wait (m_state >= s);", + "wait (other.m_state >= s);", + "wait (q.size() != 0);", + "wait (reached(s));", + ]; + for wait in waits { + let source = format!( + "class C; + int m_state; + int q[$]; + C other; + function bit reached(int s); return m_state >= s; endfunction + task wait_for(int s); {wait} $display(\"reached %0d at %0d\", s, $time); endtask + endclass + module Top; + initial begin + automatic C c = new; + c.other = c; + fork + c.wait_for(2); + begin #1 c.m_state = 1; #1 c.m_state = 2; c.q.push_back(1); end + join + end + endmodule" + ); + for four_state in [false, true] { + let builder = || { + celox::Simulation::from_sv_sources( + vec![(source.as_str(), Path::new("class.sv"))], + "Top", + ) + .four_state(four_state) + }; + let errors = [ + builder().build_native().err(), + builder().build_cranelift().err(), + builder().build_wasm().err(), + builder().build_interpreter().err(), + ]; + for error in errors { + let error = error + .unwrap_or_else(|| panic!("{wait} must be rejected")) + .to_string(); + assert!(error.contains("in a wait condition"), "{wait}: {error}"); + } + } + } +} + +/// The default of an argument is read only where a call omits the argument, +/// so a property-reading default does not reject a wait that supplies it. +#[test] +fn waits_read_property_defaults_only_when_omitted() { + let source = |call: &str| { + format!( + "`timescale 1ns/1ns + class C; int state; endclass + module Top(output int seen); + C c; + int signal; + function automatic bit ready(int x = c.state); return x != 0; endfunction + initial begin + c = new; + fork + begin wait ({call}); seen = $time; end + begin #2 signal = 1; end + join + end + endmodule" + ) + }; + let supplied = source("ready(signal)"); + let mut sim = + celox::Simulation::from_sv_sources(vec![(supplied.as_str(), Path::new("class.sv"))], "Top") + .build_interpreter() + .unwrap(); + sim.run_until(5).unwrap(); + let seen = sim.signal("seen"); + assert_eq!(sim.get(seen), BigUint::from(2u8)); + let omitted = source("ready()"); + let Err(error) = + celox::Simulation::from_sv_sources(vec![(omitted.as_str(), Path::new("class.sv"))], "Top") + .build_interpreter() + else { + panic!("a wait reading a property through a default is rejected"); + }; + let error = error.to_string(); + assert!(error.contains("in a wait condition"), "{error}"); +} + #[test] fn randomizing_four_state_properties_is_rejected() { for (body, want) in [ diff --git a/docs/guide/systemverilog-reference.md b/docs/guide/systemverilog-reference.md index ea203f1ba21..46cb4c3a12c 100644 --- a/docs/guide/systemverilog-reference.md +++ b/docs/guide/systemverilog-reference.md @@ -335,8 +335,12 @@ constructs without a dedicated issue point to the frontend roadmap, [#88](https: - Behavioral and verification constructs: intra-assignment timing controls (`a = #1 b`), delayed continuous assignments ([#444](https://github.com/celox-sim/celox/issues/444)), named events and `->`, `wait fork` / `disable fork`, temporal concurrent assertions beyond the Boolean subset below, `force` / `release`. -- Timing controls in processes: impure function calls in event expressions, and - `wait` or event expressions reading a static task argument. +- Timing controls in processes: impure function calls in event expressions, + `wait` or event expressions reading a static task argument, and `wait` + conditions or event expressions reading a class property (directly, through + a handle or a function, or a container held in one), since a write of the + property does not wake the waiting process. Static class properties are + module variables and may be waited on. - Nonblocking assignments inside `always_latch` ([#431](https://github.com/celox-sim/celox/issues/431)); use blocking assignments for the supported latch subset. - Inferred latches inside `always_comb` are rejected by default. Set `inferred_latch = "warn"` or `"allow"` in `celox.toml` under `[diagnostics]` to permit retained blocking assignments. Ordinary `always @*` and explicit `always_latch` do not require this option. - Ports and instances: non-ANSI port expressions such as `module m({a, b}, c[3:0], .p(d))` ([#1953](https://github.com/celox-sim/celox/issues/1953)), `ref` ports diff --git a/docs/ja/guide/systemverilog-reference.md b/docs/ja/guide/systemverilog-reference.md index 12758812b52..f540ad6bb28 100644 --- a/docs/ja/guide/systemverilog-reference.md +++ b/docs/ja/guide/systemverilog-reference.md @@ -218,7 +218,9 @@ SystemVerilog に対しても、共有の Veryl 適合性スイートを実行 - 振る舞い記述・検証向けの構文:`final`、代入内のタイミング制御(`a = #1 b`)、遅延付き継続代入([#444](https://github.com/celox-sim/celox/issues/444))、 named event と `->`、`fork` / `join`、`wait fork` / `disable fork`、下記のBoolean部分集合を超える並行アサーション、`force` / `release`。 - プロセス内のタイミング制御:イベント式と `iff` 条件内の impure な関数呼び出し、 - static task 引数を読む `wait` 条件、イベント式、`iff` 条件。 + static task 引数を読む `wait` 条件、イベント式、`iff` 条件。クラスのプロパティを(直接、ハンドルや関数を通じて、 + またはプロパティが持つコンテナとして)読む `wait` 条件とイベント式。プロパティへの書き込みで待機中のプロセスが + 起きないためです。static プロパティはモジュールの変数なので待機できます。 - `always_latch` 内のノンブロッキング代入([#431](https://github.com/celox-sim/celox/issues/431))。対応するラッチではブロッキング代入を使用してください。 - `always_comb` の暗黙のラッチ推論はデフォルトで拒否します。`celox.toml` の `[diagnostics]` で `inferred_latch = "warn"` または `"allow"` を指定すると許可できます。通常の `always @*` と明示的な `always_latch` にはこの設定は不要です。 - ポートとインスタンス:non-ANSI 形式のポート式(`module m({a, b}, c[3:0], .p(d))` など、[#1953](https://github.com/celox-sim/celox/issues/1953))、`ref` ポート([#427](https://github.com/celox-sim/celox/issues/427))、