TODO section 20 round 1, coverage_gaps C1 for constraints. A constraint
declaration was opaque text with no name span; renaming a rand field
left constraint c { user_id < 5; } as non-compiling code. Now
ItemKind::Constraint with a structured ConstraintSet (Expr{soft},
Implication, If, Foreach, SolveBefore, Unique, DisableSoft, Directive,
Empty), ExprKind::Dist, and randomize() with bodies as
ConstraintBody::{Items, Verbatim}. The parser attempts the structured
body behind a Rewind snapshot - cursor, both pending-comment buckets,
depth, links, open_conds, diagnostics - and on any error re-runs
v0.26.0's verbatim capture character for character, so an unknown body
IS the old behavior. No keywords reserved: soft/dist/solve/before are
matched by text, so logic before; keeps working and token.rs, pairs.rs
and the editor grammar are untouched. Canonical layout, verify.rs
untouched, attack pairs re-derived and extended. Every operand binds to
the class's properties (foreach vars nest; a with block resolves against
the randomized object's class first), so hover, definition, references
and rename reach inside. FACTS_FORMAT 10 -> 11. Lint rules read
constraint operands.
QA's finding - comments after the round's own words were dropped and
the safety net refused the file - answered with an exhaustive gap fuzzer
(358 gaps, format and idempotency demanded) and fourteen named comment
fields; the forty gaps that remain are shared machinery that fails
identically outside a constraint, filed as topo 2.9-22. Opentitan: 1993
of 2009 bodies structured, sweep unchanged, 48 DV files idempotent with
comment streams byte-identical, per-file diagnostics identical.
docs/features/constraint-bodies/README.md.
Co-Authored-By: Claude Fable 5 <[email protected]>
653 lines
13 KiB
Plaintext
653 lines
13 KiB
Plaintext
# Constraint bodies (IEEE 1800 §18.5) — the canonical layout the
|
|
# constraint-bodies round introduced. See
|
|
# docs/features/constraint-bodies/README.md.
|
|
#
|
|
# A named constraint is a block and is laid out like one: the header keeps
|
|
# the `{`, one constraint item per line at one further indent, and the `}`
|
|
# closes at the header's column. A body the grammar does not recognise
|
|
# rewinds to the byte-for-byte verbatim capture constraints had before this
|
|
# round; a `randomize() with { ... }` block may stay on the statement's line
|
|
# when it holds one item and nothing inside it forces a break.
|
|
|
|
# a constraint block: one item per line, indented one step, `}` at the header's column
|
|
class item;
|
|
constraint c_len {
|
|
len > 0;
|
|
len < 64;
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c_len {
|
|
len > 0;
|
|
len < 64;
|
|
}
|
|
endclass
|
|
====
|
|
# an empty constraint body
|
|
class item;
|
|
constraint c_none { }
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c_none {}
|
|
endclass
|
|
====
|
|
# a constraint prototype, and its `extern`/`pure static` spellings
|
|
class item;
|
|
constraint c_a;
|
|
extern constraint c_b;
|
|
pure static constraint c_c;
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c_a;
|
|
extern constraint c_b;
|
|
pure static constraint c_c;
|
|
endclass
|
|
====
|
|
# a stray `;` after the closing brace is reproduced
|
|
class item;
|
|
constraint c { a == 0; };
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c {
|
|
a == 0;
|
|
};
|
|
endclass
|
|
====
|
|
# soft constraints and a `dist` set, both weight operators kept apart
|
|
class item;
|
|
constraint c_dist {
|
|
soft len dist { 0 := 5, [1:20] :/ 20, [21:100] :/ 1 };
|
|
kind dist { READ := 1, WRITE := 1 };
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c_dist {
|
|
soft len dist {0 := 5, [1:20] :/ 20, [21:100] :/ 1};
|
|
kind dist {READ := 1, WRITE := 1};
|
|
}
|
|
endclass
|
|
====
|
|
# a long dist set breaks one weight per line
|
|
class item;
|
|
constraint c_dist {
|
|
delay dist { 0 := 500, [1:16] := 200, [17:64] := 100, [65:256] := 50, [257:1024] := 10, [1025:4096] := 1 };
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c_dist {
|
|
delay dist {
|
|
0 := 500,
|
|
[1:16] := 200,
|
|
[17:64] := 100,
|
|
[65:256] := 50,
|
|
[257:1024] := 10,
|
|
[1025:4096] := 1
|
|
};
|
|
}
|
|
endclass
|
|
====
|
|
# a dist item with no weight at all
|
|
class item;
|
|
constraint c { a dist { 0, 1, [2:3] }; }
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c {
|
|
a dist {0, 1, [2:3]};
|
|
}
|
|
endclass
|
|
====
|
|
# `inside` and `!` and index expressions inside a constraint
|
|
class item;
|
|
constraint c_align {
|
|
// The branch address is always required to be half-word aligned.
|
|
!addr[0];
|
|
addr inside {[0:255], 512};
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c_align {
|
|
// The branch address is always required to be half-word aligned.
|
|
!addr[0];
|
|
addr inside {[0:255], 512};
|
|
}
|
|
endclass
|
|
====
|
|
# an implication with a braced set, and one with a bare item
|
|
class item;
|
|
constraint c_impl {
|
|
(kind != Branch) -> addr == 0;
|
|
kind == Branch -> { addr[1:0] == 0; addr < 1024; }
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c_impl {
|
|
(kind != Branch) -> addr == 0;
|
|
kind == Branch -> {
|
|
addr[1:0] == 0;
|
|
addr < 1024;
|
|
}
|
|
}
|
|
endclass
|
|
====
|
|
# `<->` is an operator, not an implication
|
|
class item;
|
|
constraint c { a <-> b; }
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c {
|
|
a <-> b;
|
|
}
|
|
endclass
|
|
====
|
|
# a constraint if/else-if chain, braced and bare
|
|
class item;
|
|
constraint c_if {
|
|
if (mode == 0) len == 1;
|
|
else if (mode == 1) { len == 2; kind == READ; }
|
|
else len == 3;
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c_if {
|
|
if (mode == 0) len == 1;
|
|
else if (mode == 1) {
|
|
len == 2;
|
|
kind == READ;
|
|
} else len == 3;
|
|
}
|
|
endclass
|
|
====
|
|
# a constraint foreach, including a skipped dimension
|
|
class item;
|
|
constraint c_each {
|
|
foreach (data[i]) data[i] < 256;
|
|
foreach (grid[, j]) { grid[j] inside {0, 1}; }
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c_each {
|
|
foreach (data[i]) data[i] < 256;
|
|
foreach (grid[, j]) {
|
|
grid[j] inside {0, 1};
|
|
}
|
|
}
|
|
endclass
|
|
====
|
|
# solve ... before, with lists on both sides
|
|
class item;
|
|
constraint c_order { solve kind, mode before len, addr; }
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c_order {
|
|
solve kind, mode before len, addr;
|
|
}
|
|
endclass
|
|
====
|
|
# unique and disable soft
|
|
class item;
|
|
constraint c_u {
|
|
unique {a, b, c};
|
|
disable soft len;
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c_u {
|
|
unique {a, b, c};
|
|
disable soft len;
|
|
}
|
|
endclass
|
|
====
|
|
# a null constraint item is its own `;`
|
|
class item;
|
|
constraint c {
|
|
a == 0;
|
|
;
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c {
|
|
a == 0;
|
|
;
|
|
}
|
|
endclass
|
|
====
|
|
# comments in every slot of a constraint body
|
|
class item;
|
|
// why this constraint exists
|
|
constraint c { // opening
|
|
// before the item
|
|
a == 0; // after the item
|
|
b == 1;
|
|
// before the closing brace
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
// why this constraint exists
|
|
constraint c {
|
|
// opening
|
|
// before the item
|
|
a == 0; // after the item
|
|
b == 1;
|
|
// before the closing brace
|
|
}
|
|
endclass
|
|
====
|
|
# a blank line between constraint items is kept
|
|
class item;
|
|
constraint c {
|
|
a == 0;
|
|
|
|
b == 1;
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c {
|
|
a == 0;
|
|
|
|
b == 1;
|
|
}
|
|
endclass
|
|
====
|
|
# an `ifdef inside a constraint body keeps both arms as ordinary items
|
|
class item;
|
|
constraint c {
|
|
`ifdef FAST
|
|
len < 4;
|
|
`else
|
|
len < 64;
|
|
`endif
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c {
|
|
`ifdef FAST
|
|
len < 4;
|
|
`else
|
|
len < 64;
|
|
`endif
|
|
}
|
|
endclass
|
|
====
|
|
# a constraint written at column 1 is re-indented into the class body
|
|
class item;
|
|
constraint c_dist {
|
|
num_insns dist { 0 :/ 5, [1:20] :/ 20, [21:100] :/ 1 };
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c_dist {
|
|
num_insns dist {0 :/ 5, [1:20] :/ 20, [21:100] :/ 1};
|
|
}
|
|
endclass
|
|
====
|
|
# a body the grammar does not know falls back to the byte-for-byte capture
|
|
class item;
|
|
constraint c {
|
|
`MY_CONSTRAINT_HALF
|
|
len < 4;
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c {
|
|
`MY_CONSTRAINT_HALF
|
|
len < 4;
|
|
}
|
|
endclass
|
|
====
|
|
# a nested implication inside a foreach inside an if
|
|
class item;
|
|
constraint c {
|
|
if (enable) {
|
|
foreach (q[i]) {
|
|
q[i] > 0 -> { q[i] < 100; }
|
|
}
|
|
}
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c {
|
|
if (enable) {
|
|
foreach (q[i]) {
|
|
q[i] > 0 -> {
|
|
q[i] < 100;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
endclass
|
|
====
|
|
# a randomize() with block stays on the statement line when it fits
|
|
module m;
|
|
initial begin
|
|
if (!req.randomize() with { addr == 0; }) $fatal(1, "no");
|
|
end
|
|
endmodule
|
|
----
|
|
module m;
|
|
initial begin
|
|
if (!req.randomize() with {addr == 0;}) $fatal(1, "no");
|
|
end
|
|
endmodule
|
|
====
|
|
# a randomize() with block of several items breaks like a constraint body
|
|
module m;
|
|
initial begin
|
|
void'(std::randomize(x, y) with { x > 3; y < x; });
|
|
end
|
|
endmodule
|
|
----
|
|
module m;
|
|
initial begin
|
|
void'(std::randomize(x, y) with {
|
|
x > 3;
|
|
y < x;
|
|
});
|
|
end
|
|
endmodule
|
|
====
|
|
# a with block the grammar does not know keeps its bytes
|
|
module m;
|
|
initial begin
|
|
void'(std::randomize(x) with { `MY_HALF x > 3; });
|
|
end
|
|
endmodule
|
|
----
|
|
module m;
|
|
initial begin
|
|
void'(std::randomize(x) with { `MY_HALF x > 3; });
|
|
end
|
|
endmodule
|
|
====
|
|
# a with block carrying a comment breaks so the `}` cannot be swallowed
|
|
module m;
|
|
initial begin
|
|
void'(std::randomize(x) with { x > 3; // at least four
|
|
});
|
|
end
|
|
endmodule
|
|
----
|
|
module m;
|
|
initial begin
|
|
void'(std::randomize(x) with {
|
|
x > 3; // at least four
|
|
});
|
|
end
|
|
endmodule
|
|
====
|
|
# a constraint carrying a multi-line string leaves the string's bytes alone
|
|
class item;
|
|
constraint c {
|
|
n == "a
|
|
b";
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c {
|
|
n == "a
|
|
b";
|
|
}
|
|
endclass
|
|
====
|
|
# an escaped identifier as a constraint name, and inside the body
|
|
class item;
|
|
constraint \c-1 {
|
|
\a-1 == 0;
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint \c-1 {
|
|
\a-1 == 0;
|
|
}
|
|
endclass
|
|
====
|
|
# a very long constraint expression wraps like any other expression
|
|
class item;
|
|
constraint c {
|
|
a_very_long_signal_name + another_long_signal_name + a_third_long_signal_name == 0;
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c {
|
|
a_very_long_signal_name + another_long_signal_name + a_third_long_signal_name == 0;
|
|
}
|
|
endclass
|
|
====
|
|
# ----------------------------------------------------------------------
|
|
# Comment slots (the 2026-09-02 rework). Every keyword and punctuation
|
|
# position this round introduced owns the comments written in front of it,
|
|
# so none of them drifts across a token and `format_source` never has to
|
|
# refuse the file. Each case is asserted for output *and* idempotency.
|
|
# ----------------------------------------------------------------------
|
|
|
|
# a comment after `soft` prints between `soft` and the expression
|
|
class item;
|
|
constraint c {
|
|
soft /* pick one */ len < 5;
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c {
|
|
soft /* pick one */ len < 5;
|
|
}
|
|
endclass
|
|
====
|
|
# comments on both sides of `dist`, and on both sides of each weight operator
|
|
class item;
|
|
constraint c {
|
|
len /* the field */ dist /* the set */ { 0 /* zero */ := /* five */ 5, [1:3] :/ /* two */ 2 };
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c {
|
|
len /* the field */ dist /* the set */ {0 /* zero */ := /* five */ 5, [1:3] :/ /* two */ 2};
|
|
}
|
|
endclass
|
|
====
|
|
# comments on both sides of the `soft` of a `disable soft`
|
|
class item;
|
|
constraint c {
|
|
disable /* off */ soft /* the soft one */ len;
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c {
|
|
disable /* off */ soft /* the soft one */ len;
|
|
}
|
|
endclass
|
|
====
|
|
# a comment before `before`, and one after it, in a solve list
|
|
class item;
|
|
constraint c {
|
|
solve kind /* first */ before /* then */ len;
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c {
|
|
solve kind /* first */ before /* then */ len;
|
|
}
|
|
endclass
|
|
====
|
|
# a comment between `unique` and its set
|
|
class item;
|
|
constraint c {
|
|
unique /* all four */ {a, b, c, d};
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c {
|
|
unique /* all four */ {a, b, c, d};
|
|
}
|
|
endclass
|
|
====
|
|
# comments in the constraint header: after the keyword, and before the `{`
|
|
class item;
|
|
constraint /* the name */ c_len /* the body */ {
|
|
len < 5;
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint /* the name */ c_len /* the body */ {
|
|
len < 5;
|
|
}
|
|
endclass
|
|
====
|
|
# the same two slots on a prototype and on an out-of-block definition
|
|
class item;
|
|
extern constraint /* named */ c_len /* nothing follows */ ;
|
|
endclass
|
|
|
|
constraint item::c_len /* out of block */ {
|
|
len < 5;
|
|
} /* and a stray semicolon */ ;
|
|
----
|
|
class item;
|
|
extern constraint /* named */ c_len /* nothing follows */;
|
|
endclass
|
|
|
|
constraint item::c_len /* out of block */ {
|
|
len < 5;
|
|
} /* and a stray semicolon */;
|
|
====
|
|
# a comment written in front of the item's own `;` stays on the item's side
|
|
class item;
|
|
constraint c {
|
|
len < 5 /* inclusive */ ;
|
|
unique {a, b} /* distinct */ ;
|
|
disable soft len /* not this one */ ;
|
|
solve a before b /* in that order */ ;
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c {
|
|
len < 5 /* inclusive */;
|
|
unique {a, b} /* distinct */;
|
|
disable soft len /* not this one */;
|
|
solve a before b /* in that order */;
|
|
}
|
|
endclass
|
|
====
|
|
# a line comment in front of the `;` forces the break the comment demands
|
|
class item;
|
|
constraint c {
|
|
len < 5 // inclusive
|
|
;
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c {
|
|
len < 5 // inclusive
|
|
;
|
|
}
|
|
endclass
|
|
====
|
|
# every slot of a constraint `if` header, and the one after `else`
|
|
class item;
|
|
constraint c {
|
|
if /* test */ ( /* the mode */ mode == 0 /* only zero */ ) /* then */ {
|
|
len == 1;
|
|
} else /* otherwise */ {
|
|
len == 2;
|
|
}
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c {
|
|
if /* test */ (
|
|
/* the mode */
|
|
mode == 0 /* only zero */
|
|
) /* then */ {
|
|
len == 1;
|
|
} else /* otherwise */ {
|
|
len == 2;
|
|
}
|
|
}
|
|
endclass
|
|
====
|
|
# the `foreach` header's slots: after the keyword, inside the `(`, before the `)`, before the body
|
|
class item;
|
|
constraint c {
|
|
foreach /* each */ ( /* the array */ data[i] /* done */ ) /* then */ {
|
|
data[i] < 4;
|
|
}
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c {
|
|
foreach /* each */ ( /* the array */ data[i] /* done */ ) /* then */ {
|
|
data[i] < 4;
|
|
}
|
|
}
|
|
endclass
|
|
====
|
|
# a comment after the `->` of an implication, and after an `else if`
|
|
class item;
|
|
constraint c {
|
|
m -> /* then */ { a == 1; }
|
|
if (m) a == 1;
|
|
else /* chained */ if (n) a == 2;
|
|
}
|
|
endclass
|
|
----
|
|
class item;
|
|
constraint c {
|
|
m -> /* then */ {
|
|
a == 1;
|
|
}
|
|
if (m) a == 1;
|
|
else /* chained */ if (n) a == 2;
|
|
}
|
|
endclass
|
|
====
|
|
# the same slots inside a randomize() with block
|
|
module m;
|
|
initial begin
|
|
void'(req.randomize() with {
|
|
soft /* pick */ addr dist /* set */ { 0 := 5 };
|
|
addr < 9 /* inclusive */ ;
|
|
});
|
|
end
|
|
endmodule
|
|
----
|
|
module m;
|
|
initial begin
|
|
void'(req.randomize() with {
|
|
soft /* pick */ addr dist /* set */ {0 := 5};
|
|
addr < 9 /* inclusive */;
|
|
});
|
|
end
|
|
endmodule
|