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
10 changes: 0 additions & 10 deletions crates/celox-sv-analyzer/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6587,11 +6587,6 @@ fn explicit_output_data_types_default_to_variables() {
#[test]
fn class_randomize_rejects_unsupported_semantics() {
for (members, call, expected) in [
(
"rand int x; constraint c { if (x) x == 1; }",
"c.randomize()",
"constraints",
),
(
"rand int x; constraint c { soft x == 1; }",
"c.randomize()",
Expand All @@ -6607,11 +6602,6 @@ fn class_randomize_rejects_unsupported_semantics() {
"c.randomize()",
"constraint",
),
(
"rand int x; constraint c { (x == 1) -> (x < 3); }",
"c.randomize()",
"constraints",
),
(
"rand int x; constraint c { (x == 1) <-> (x < 3); }",
"c.randomize()",
Expand Down
92 changes: 76 additions & 16 deletions crates/celox-sv-syntax/src/parser/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ enum Job {
Block(bool),
Expression,
Set,
Else,
Guard {
condition: ExprId,
first: usize,
start: usize,
negate: bool,
has_else: bool,
},
}

impl Parser<'_> {
Expand Down Expand Up @@ -43,8 +49,6 @@ impl Parser<'_> {
}) {
return Err(self.error("duplicate constraint block"));
}
let mut expressions = Vec::new();
let mut supported = !is_static;
if self.eat(";") {
if let Some(class) = self.class_stack.last_mut() {
class.constraints.push(ClassConstraint {
Expand All @@ -57,6 +61,19 @@ impl Parser<'_> {
if prototype {
return Err(self.error("extern or pure constraint requires a prototype"));
}
let expressions = self.constraint_block()?;
if let Some(class) = self.class_stack.last_mut() {
class.constraints.push(ClassConstraint {
name,
expressions: if is_static { None } else { expressions },
});
}
Ok(())
}

pub(super) fn constraint_block(&mut self) -> Result<Option<Vec<ExprId>>> {
let mut expressions = Vec::new();
let mut supported = true;
self.expect("{")?;
let mut jobs = vec![Job::Block(true)];
while let Some(job) = jobs.pop() {
Expand Down Expand Up @@ -84,18 +101,60 @@ impl Parser<'_> {
jobs.push(Job::Expression);
}
}
Job::Else => {
if self.eat("else") {
Job::Guard {
condition,
first,
start,
negate,
has_else,
} => {
let guard = if negate {
self.node(
start,
ExprKind::Unary {
operator: "!".into(),
expression: condition,
postfix: false,
},
)?
} else {
condition
};
let body: Vec<_> = expressions.drain(first..).collect();
for expression in body {
expressions.push(self.node(
start,
ExprKind::Binary {
left: guard,
operator: "||".into(),
right: expression,
},
)?);
}
if has_else && self.eat("else") {
jobs.push(Job::Guard {
condition,
first: expressions.len(),
start,
negate: false,
has_else: false,
});
jobs.push(Job::Set);
}
}
Job::Expression => {
let start = self.at;
if self.eat("if") {
supported = false;
self.expect("(")?;
self.expression(0)?;
let condition = self.expression(0)?;
self.expect(")")?;
jobs.push(Job::Else);
jobs.push(Job::Guard {
condition,
first: expressions.len(),
start,
negate: true,
has_else: true,
});
jobs.push(Job::Set);
} else if self.eat("foreach") {
supported = false;
Expand All @@ -121,7 +180,14 @@ impl Parser<'_> {
supported &= simple;
}
if !soft && self.eat("->") {
supported = false;
let condition = expressions.pop().expect("condition retained");
jobs.push(Job::Guard {
condition,
first: expressions.len(),
start,
negate: true,
has_else: false,
});
jobs.push(Job::Set);
} else {
if self.eat("dist") {
Expand All @@ -134,13 +200,7 @@ impl Parser<'_> {
}
}
}
if let Some(class) = self.class_stack.last_mut() {
class.constraints.push(ClassConstraint {
name,
expressions: supported.then_some(expressions),
});
}
Ok(())
Ok(supported.then_some(expressions))
}

fn constraint_condition(&mut self) -> Result<(ExprId, bool)> {
Expand Down
10 changes: 8 additions & 2 deletions crates/celox-sv-syntax/src/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3047,10 +3047,16 @@ fn malformed_array_method_with_is_rejected() {
#[test]
fn constraints_retain_simple_expressions_and_mark_other_forms() {
let tree = parse(
"class C; rand int x; constraint a { x > 0; x < 3; } constraint b { if (x) x == 1; } endclass",
"class C; rand int x; constraint a { x > 0; x < 3; } constraint b { if (x) x == 1; } constraint c { soft x == 1; } constraint d { if (1) {} } endclass",
);
let blocks = &tree.classes[0].constraints;
assert_eq!(blocks[0].name.spelling, "a");
assert_eq!(blocks[0].expressions.as_ref().unwrap().len(), 2);
assert!(blocks[1].expressions.is_none());
let guarded = blocks[1].expressions.as_ref().unwrap();
assert_eq!(guarded.len(), 1);
assert!(
matches!(&tree.expression(guarded[0]).kind, ExprKind::Binary { operator, .. } if operator == "||")
);
assert!(blocks[2].expressions.is_none());
assert!(blocks[3].expressions.as_ref().unwrap().is_empty());
}
53 changes: 53 additions & 0 deletions crates/celox-test-suite/src/sv/cases/class.vtest
Original file line number Diff line number Diff line change
Expand Up @@ -2075,3 +2075,56 @@ module Top(output bit done);
endmodule
"#)
(top Top) (run_until 0) (assert_eq done 1))

(case constrained_randomize_conditionals
(source "conditional_constraints.sv" #"
module Top(output bit done);
class Packet;
rand bit mode;
rand int value;
int required;
constraint c {
value == required;
if (mode) { value inside {[1:3]}; value != 2; }
else value == 8;
mode -> { value > 0; if (value == 3) value != 1; }
}
endclass
Packet p;
initial begin
p = new;
p.required = 8;
if (!p.randomize() || p.mode != 0 || p.value != 8) $fatal(1, "reverse else solve");
p.required = 3;
if (!p.randomize() || p.mode != 1 || p.value != 3) $fatal(1, "reverse then solve");
p.required = 2;
if (p.randomize() || p.value != 3) $fatal(1, "block constraint lost");
done = 1;
end
endmodule
"#)
(top Top) (run_until 0) (assert_eq done 1))

(case constrained_randomize_nested_guards
(source "nested_guards.sv" #"
module Top(output bit done);
class Packet;
rand bit a, b;
rand int x;
constraint c {
x == 7;
if (a) if (b) x == 1; else x == 7;
!a -> x == 2;
a -> b -> x == 1;
if (1'b0) x == 123;
}
endclass
Packet p;
initial begin
p = new;
if (!p.randomize() || p.a != 1 || p.b != 0 || p.x != 7) $fatal(1, "nested guards");
done = 1;
end
endmodule
"#)
(top Top) (run_until 0) (assert_eq done 1))
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"observations": [
{
"tool": "icarus",
"case": "class::constrained_randomize_conditionals",
"status": "compile_error",
"log": "<target>/sv-icarus/class/constrained_randomize_conditionals/source_0.sv:9: sorry: \"inside\" expressions not supported yet.\n<target>/sv-icarus/class/constrained_randomize_conditionals/source_0.sv:7: sorry: Constraint declarations not supported.\n"
},
{
"tool": "icarus",
"case": "class::constrained_randomize_nested_guards",
"status": "compile_error",
"log": "<target>/sv-icarus/class/constrained_randomize_nested_guards/source_0.sv:6: sorry: Constraint declarations not supported.\n"
}
]
}
65 changes: 60 additions & 5 deletions crates/celox-test-suite/verification/sv/icarus/class.json
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,33 @@
"evidence": [
"verification/repros/sv_scalar_constraints.json",
"LIMITATIONS.md#systemverilog-suite",
"verification/repros/sv_inside_constraints.json"
"verification/repros/sv_inside_constraints.json",
"verification/repros/sv_conditional_constraints.json"
],
"id": "sv_icarus_scalar_constraints",
"observed_version": "Icarus Verilog version 13.0 (devel)",
"phase": "compile",
"reason": "Icarus rejects constraint declarations before elaboration. IEEE 1800-2023 18.5 defines these blocks. Celox retains all assertions; Verilator passes the scalar and width/inheritance cases.",
"upstream": []
},
"name": "class::constrained_randomize_conditionals",
"phase": "skip",
"status": "ignored",
"stronger_than_sv": false,
"tag_reasons": {},
"tags": []
},
{
"category": "Types",
"detail": "Icarus rejects constraint declarations before elaboration. IEEE 1800-2023 18.5 defines these blocks. Celox retains all assertions; Verilator passes the scalar and width/inheritance cases.",
"expectation": "Simulation",
"known_issue": {
"category": "simulator_compile_limitation",
"evidence": [
"verification/repros/sv_scalar_constraints.json",
"LIMITATIONS.md#systemverilog-suite",
"verification/repros/sv_inside_constraints.json",
"verification/repros/sv_conditional_constraints.json"
],
"id": "sv_icarus_scalar_constraints",
"observed_version": "Icarus Verilog version 13.0 (devel)",
Expand All @@ -217,7 +243,8 @@
"evidence": [
"verification/repros/sv_scalar_constraints.json",
"LIMITATIONS.md#systemverilog-suite",
"verification/repros/sv_inside_constraints.json"
"verification/repros/sv_inside_constraints.json",
"verification/repros/sv_conditional_constraints.json"
],
"id": "sv_icarus_scalar_constraints",
"observed_version": "Icarus Verilog version 13.0 (devel)",
Expand All @@ -241,7 +268,8 @@
"evidence": [
"verification/repros/sv_scalar_constraints.json",
"LIMITATIONS.md#systemverilog-suite",
"verification/repros/sv_inside_constraints.json"
"verification/repros/sv_inside_constraints.json",
"verification/repros/sv_conditional_constraints.json"
],
"id": "sv_icarus_scalar_constraints",
"observed_version": "Icarus Verilog version 13.0 (devel)",
Expand All @@ -265,7 +293,33 @@
"evidence": [
"verification/repros/sv_scalar_constraints.json",
"LIMITATIONS.md#systemverilog-suite",
"verification/repros/sv_inside_constraints.json"
"verification/repros/sv_inside_constraints.json",
"verification/repros/sv_conditional_constraints.json"
],
"id": "sv_icarus_scalar_constraints",
"observed_version": "Icarus Verilog version 13.0 (devel)",
"phase": "compile",
"reason": "Icarus rejects constraint declarations before elaboration. IEEE 1800-2023 18.5 defines these blocks. Celox retains all assertions; Verilator passes the scalar and width/inheritance cases.",
"upstream": []
},
"name": "class::constrained_randomize_nested_guards",
"phase": "skip",
"status": "ignored",
"stronger_than_sv": false,
"tag_reasons": {},
"tags": []
},
{
"category": "Types",
"detail": "Icarus rejects constraint declarations before elaboration. IEEE 1800-2023 18.5 defines these blocks. Celox retains all assertions; Verilator passes the scalar and width/inheritance cases.",
"expectation": "Simulation",
"known_issue": {
"category": "simulator_compile_limitation",
"evidence": [
"verification/repros/sv_scalar_constraints.json",
"LIMITATIONS.md#systemverilog-suite",
"verification/repros/sv_inside_constraints.json",
"verification/repros/sv_conditional_constraints.json"
],
"id": "sv_icarus_scalar_constraints",
"observed_version": "Icarus Verilog version 13.0 (devel)",
Expand All @@ -289,7 +343,8 @@
"evidence": [
"verification/repros/sv_scalar_constraints.json",
"LIMITATIONS.md#systemverilog-suite",
"verification/repros/sv_inside_constraints.json"
"verification/repros/sv_inside_constraints.json",
"verification/repros/sv_conditional_constraints.json"
],
"id": "sv_icarus_scalar_constraints",
"observed_version": "Icarus Verilog version 13.0 (devel)",
Expand Down
7 changes: 5 additions & 2 deletions crates/celox-test-suite/verification/sv/limitations.json
Original file line number Diff line number Diff line change
Expand Up @@ -2589,15 +2589,18 @@
"evidence": [
"verification/repros/sv_scalar_constraints.json",
"LIMITATIONS.md#systemverilog-suite",
"verification/repros/sv_inside_constraints.json"
"verification/repros/sv_inside_constraints.json",
"verification/repros/sv_conditional_constraints.json"
],
"cases": {
"icarus": [
"class::constrained_randomize_scalar",
"class::constrained_randomize_width_and_inheritance",
"class::constrained_randomize_hooks",
"class::constrained_randomize_inside",
"class::constrained_randomize_inside_widths"
"class::constrained_randomize_inside_widths",
"class::constrained_randomize_conditionals",
"class::constrained_randomize_nested_guards"
]
}
},
Expand Down
5 changes: 5 additions & 0 deletions crates/celox/tests/frontends/systemverilog/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,3 +937,8 @@ sv_backends! {
fn constrained_randomize_inside(sim) { @case "class::constrained_randomize_inside"; }
fn constrained_randomize_inside_widths(sim) { @case "class::constrained_randomize_inside_widths"; }
}

sv_backends! {
fn constrained_randomize_conditionals(sim) { @case "class::constrained_randomize_conditionals"; }
fn constrained_randomize_nested_guards(sim) { @case "class::constrained_randomize_nested_guards"; }
}
Loading
Loading