diff --git a/conformance/sv-tests/expected.tsv b/conformance/sv-tests/expected.tsv index 1645eeadf6a..7682b9d7c17 100644 --- a/conformance/sv-tests/expected.tsv +++ b/conformance/sv-tests/expected.tsv @@ -649,8 +649,8 @@ chapter-18/18.7--in-line-constraints--randomize_5.sv 2state fail chapter-18/18.7--in-line-constraints--randomize_5.sv 4state fail chapter-18/18.7--in-line-constraints--randomize_6.sv 2state fail chapter-18/18.7--in-line-constraints--randomize_6.sv 4state fail -chapter-18/18.7.1--local-scope-resolution_0.sv 2state fail -chapter-18/18.7.1--local-scope-resolution_0.sv 4state fail +chapter-18/18.7.1--local-scope-resolution_0.sv 2state pass +chapter-18/18.7.1--local-scope-resolution_0.sv 4state pass chapter-18/18.7.1--local-scope-resolution_1.sv 2state fail chapter-18/18.7.1--local-scope-resolution_1.sv 4state fail chapter-18/18.8--disabling-random-variables-with-rand_mode_0.sv 2state fail diff --git a/crates/celox-frontend-sv/src/lowering/ff/objects.rs b/crates/celox-frontend-sv/src/lowering/ff/objects.rs index e2d499dae70..d7d455b940c 100644 --- a/crates/celox-frontend-sv/src/lowering/ff/objects.rs +++ b/crates/celox-frontend-sv/src/lowering/ff/objects.rs @@ -38,7 +38,19 @@ impl Ff<'_, '_> { buffer = self.heap_call(ops::APPEND_BYTES, &[buffer, value, count]); } let program = self.heap_call(ops::FREEZE, &[buffer]); - self.heap_call(ops::OBJECT_RANDOMIZE, &[object, program, check]) + if args.len() == 2 { + self.heap_call(ops::OBJECT_RANDOMIZE, &[object, program, check]) + } else { + let mut inputs = self.heap_call(ops::NEW, &[]); + let one = self.constant64(1); + inputs = self.heap_call(ops::APPEND_BYTES, &[inputs, check, one]); + for value in &args[2..] { + let value = self.integer_of_width(value, 64, false)?; + let eight = self.constant64(8); + inputs = self.heap_call(ops::APPEND_BYTES, &[inputs, value, eight]); + } + self.heap_call(ops::OBJECT_RANDOMIZE_INPUTS, &[object, program, inputs]) + } } ObjectOp::Container { .. } => unreachable!("handled by container_call"), ObjectOp::New { diff --git a/crates/celox-sv-analyzer/src/ast/classes/constraints.rs b/crates/celox-sv-analyzer/src/ast/classes/constraints.rs index bd5ecf18c7a..9ecf0d40229 100644 --- a/crates/celox-sv-analyzer/src/ast/classes/constraints.rs +++ b/crates/celox-sv-analyzer/src/ast/classes/constraints.rs @@ -115,12 +115,23 @@ pub(in crate::ast) fn bind( tree: &AnalysisContext<'_>, properties: &[Property], env: &HashMap, +) -> Result { + bind_inputs(id, tree, properties, env, &HashMap::default()) +} + +pub(in crate::ast) fn bind_inputs( + id: ExprId, + tree: &AnalysisContext<'_>, + properties: &[Property], + env: &HashMap, + inputs: &HashMap, ) -> Result { fn expression( id: ExprId, tree: &AnalysisContext<'_>, properties: &[Property], env: &HashMap, + inputs: &HashMap, depth: usize, budget: &std::cell::Cell, ) -> Result { @@ -128,7 +139,7 @@ pub(in crate::ast) fn bind( return Err("constraint expression nesting limit".into()); } charge(budget)?; - let recurse = |id| expression(id, tree, properties, env, depth + 1, budget); + let recurse = |id| expression(id, tree, properties, env, inputs, depth + 1, budget); let native = tree.declarations(); Ok(match &native.expression(id).kind { ExprKind::Group(id) => return recurse(*id), @@ -145,6 +156,12 @@ pub(in crate::ast) fn bind( width: property.field.width, signed: property.field.signed, } + } else if let Some(&(index, width, signed)) = inputs.get(&name) { + Typed { + kind: Kind::Leaf(ConstraintExpr::Input { index, width }), + width, + signed, + } } else if env.contains_key(&name) { // Parameter type reconstruction belongs to the general expression binder. // Do not guess its width/sign here. @@ -153,6 +170,21 @@ pub(in crate::ast) fn bind( return Err(format!("constraint variable `{name}`")); } } + ExprKind::Member { + expression, + separator, + name, + } if separator == "::" + && matches!(&native.expression(*expression).kind, ExprKind::Name(root) if root.spelling == "local") => + { + let key = format!("local::{}", instances::normalize_identifier(&name.spelling)); + let &(index, width, signed) = inputs.get(&key).ok_or("inline local variable")?; + Typed { + kind: Kind::Leaf(ConstraintExpr::Input { index, width }), + width, + signed, + } + } ExprKind::Literal(text) => { if text.starts_with("'0") || text.starts_with("'1") { return Err("unbased unsized constraint literal".into()); @@ -286,8 +318,16 @@ pub(in crate::ast) fn bind( _ => return Err("unsupported scalar constraint expression".into()), }) } - expression(id, tree, properties, env, 0, &std::cell::Cell::new(16_384)) - .map(|value| emit(value, None)) + expression( + id, + tree, + properties, + env, + inputs, + 0, + &std::cell::Cell::new(16_384), + ) + .map(|value| emit(value, None)) } impl Class { @@ -332,10 +372,15 @@ pub(in crate::ast) struct Inline { pub key: usize, pub class: std::rc::Rc, pub constraints: Vec, + pub inputs: Vec, pub dimensions: PackedDimensions, } impl Inline { + pub fn prepare_name(&self, class: &str) -> String { + format!("{}$prepare", self.name(class)) + } + pub fn name(&self, class: &str) -> String { format!("{class}::$inline_randomize@{}", self.key) } @@ -348,6 +393,7 @@ pub(in crate::ast) struct InlineRequest { pub key: usize, pub class: Class, pub constraints: Vec, + pub inputs: Vec, } impl Inline { @@ -356,6 +402,7 @@ impl Inline { key: self.key, class: (*self.class).clone(), constraints: self.constraints.clone(), + inputs: self.inputs.clone(), } } } diff --git a/crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs b/crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs index b438437f392..714fae46d06 100644 --- a/crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs +++ b/crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs @@ -955,12 +955,96 @@ pub(super) fn randomize_with( let class = class(tree, dimensions, owner)?; // Inline constraints need the solver even on otherwise unconstrained classes. class.constraint_program()?; - let constraints = constraints - .ok_or_else(|| unsupported("inline constraint form"))? + let constraint_ids = constraints.ok_or_else(|| unsupported("inline constraint form"))?; + let mut inputs = HashMap::default(); + let mut input_types = Vec::new(); + let mut input_values = Vec::new(); + let mut pending = constraint_ids.to_vec(); + while let Some(id) = pending.pop() { + let candidate = match &native.expression(id).kind { + ExprKind::Name(name) => { + let name = instances::normalize_identifier(&name.spelling); + if class.properties.iter().any(|p| p.name == name) { + None + } else if class.statics.iter().any(|p| p.name == name) { + return Err(unsupported("static properties in inline constraints")); + } else { + Some((name.clone(), name)) + } + } + ExprKind::Member { + expression, + separator, + name, + } if separator == "::" + && matches!(&native.expression(*expression).kind, ExprKind::Name(root) if root.spelling == "local") => + { + let name = instances::normalize_identifier(&name.spelling); + Some((format!("local::{name}"), name)) + } + ExprKind::Group(id) | ExprKind::Unary { expression: id, .. } => { + pending.push(*id); + None + } + ExprKind::Binary { left, right, .. } => { + pending.extend([*right, *left]); + None + } + ExprKind::Set(items) => { + for item in items { + match item.kind { + celox_sv_syntax::DimensionKind::Index(id) => pending.push(id), + celox_sv_syntax::DimensionKind::Range { left, right } => { + pending.extend([right, left]) + } + _ => {} + } + } + None + } + _ => None, + }; + let Some((key, name)) = candidate else { + continue; + }; + if inputs.contains_key(&key) { + continue; + } + let variable = dimensions + .get(&name) + .ok_or_else(|| unsupported(format!("inline local variable `{name}`")))?; + if variable.real + || variable.string + || variable.class.is_some() + || variable.container.is_some() + || !variable.unpacked.is_empty() + || !variable.is_2state + { + return Err(unsupported( + "inline inputs require two-state scalar variables", + )); + } + let value = Expr::Ident(name); + let width = expr_static_width(&tree.state, &value, dimensions) + .filter(|width| (1..=64).contains(width)) + .ok_or_else(|| unsupported("inline input width"))?; + let mut ty = classes::unsigned_type(width); + ty.is_signed = variable.signed; + inputs.insert(key, (input_types.len(), width as u32, variable.signed)); + input_types.push(ty); + input_values.push(Some(value)); + } + let constraints = constraint_ids .iter() .map(|&id| { - classes::constraints::bind(id, tree, &class.properties, &dimensions.const_env) - .map_err(|reason| unsupported(format!("inline constraints: {reason}"))) + classes::constraints::bind_inputs( + id, + tree, + &class.properties, + &dimensions.const_env, + &inputs, + ) + .map_err(|reason| unsupported(format!("inline constraints: {reason}"))) }) .collect::>>()?; let selection = random_selection(tree, dimensions, &method_name, arguments, native)?; @@ -975,13 +1059,25 @@ pub(super) fn randomize_with( key, class, constraints, + inputs: input_types, dimensions: dimensions.clone(), }; let name = inline.name(owner); + // Argument evaluation is left-to-right. Prepare the receiver and run its + // pre-hook before sampling any caller-scope input. Evaluate the receiver once. + let handle = if input_values.is_empty() { + handle + } else { + Expr::Call { + callee: Callee::Subroutine(inline.prepare_name(owner)), + args: vec![Some(handle), Some(selection.clone())], + arg_order: Vec::new(), + } + }; tree.state.inline_randomizations.borrow_mut().push(inline); Ok(Expr::Call { callee: Callee::Subroutine(name), - args: vec![Some(handle), Some(selection)], + args: [vec![Some(handle), Some(selection)], input_values].concat(), arg_order: Vec::new(), }) } diff --git a/crates/celox-sv-analyzer/src/ast/imports.rs b/crates/celox-sv-analyzer/src/ast/imports.rs index d0279b3bd7e..fc5feacd2d1 100644 --- a/crates/celox-sv-analyzer/src/ast/imports.rs +++ b/crates/celox-sv-analyzer/src/ast/imports.rs @@ -123,6 +123,11 @@ impl ScopeImports { if package == "std" && normalize_identifier(&name.spelling) == "randomize" { continue; } + // Inline constraint local:: lookup bypasses the receiver class, + // not through a package dependency (18.7.1). + if package == "local" { + continue; + } // `C::name` names a member of class `C` (IEEE 1800-2023 8.23). if visible_class(tree, &package) { continue; diff --git a/crates/celox-sv-analyzer/src/ast/procedural/randomization.rs b/crates/celox-sv-analyzer/src/ast/procedural/randomization.rs index 20c1bdd9e52..7cf4e43d484 100644 --- a/crates/celox-sv-analyzer/src/ast/procedural/randomization.rs +++ b/crates/celox-sv-analyzer/src/ast/procedural/randomization.rs @@ -13,7 +13,7 @@ pub(super) fn lower( state: &mut BodyState<'_>, item: &generate::Item, ) -> Converted> { - lower_methods(class, tree, dimensions, state, Some(item), None) + lower_methods(class, tree, dimensions, state, Some(item), None, false) } fn lower_methods( @@ -23,6 +23,7 @@ fn lower_methods( state: &mut BodyState<'_>, item: Option<&generate::Item>, inline: Option<&classes::constraints::Inline>, + prepare: bool, ) -> Converted> { let env = item.map(|item| &*item.env).unwrap_or(&dimensions.const_env); let aliases = item @@ -30,7 +31,11 @@ fn lower_methods( .unwrap_or_else(|| std::borrow::Cow::Borrowed(&dimensions.type_aliases)); let method_name = |class: &str, method: &str| { if let Some(inline) = inline { - inline.name(class) + if prepare { + inline.prepare_name(class) + } else { + inline.name(class) + } } else { let name = classes::Class::subroutine_name(class, method); item.map(|item| item.name(&name)).unwrap_or(name) @@ -66,7 +71,7 @@ fn lower_methods( _ => ("on_off", classes::unsigned_type(1)), }; let argument = builder.declare(argument, argument_type.clone()); - let params = vec![ + let mut params = vec![ SubroutineParam { name: this.clone(), source_name: classes::THIS.into(), @@ -82,6 +87,20 @@ fn lower_methods( default: None, }, ]; + let mut input_values = Vec::new(); + if let Some(inline) = inline.filter(|_| !prepare) { + for (index, ty) in inline.inputs.iter().enumerate() { + let name = builder.declare(&format!("$input{index}"), ty.clone()); + params.push(SubroutineParam { + name: name.clone(), + source_name: name.clone(), + direction: ParamDirection::Input, + r#type: convert_type(ty.clone()), + default: None, + }); + input_values.push(Expr::Ident(name)); + } + } // Faults are consumed after the kernel completes. Return immediately // on rejection, before hooks, draws or property/mode updates. let mut body = vec![Stmt::If { @@ -153,93 +172,105 @@ fn lower_methods( vec![Expr::Ident(this.clone()), selected], ))); } - body.push(Stmt::If { - condition: Expr::Ident(argument.clone()), - then_body: select, - else_body: Vec::new(), - }); - body.extend(hook("pre_randomize")); - if class.has_constraints || inline.is_some() { - let mut program = class.constraint_program()?; - if let Some(inline) = inline { - program.constraints.extend(inline.constraints.clone()); - } - let success = builder.declare("$constraint_success", int_type()); - body.push(Stmt::Assign { - lhs: LValue::Ident(success.clone()), - rhs: classes::object_call( - ObjectOp::Randomize(program), - vec![ - Expr::Ident(this.clone()), - Expr::Binary { - left: Box::new(Expr::Ident(argument.clone())), - op: BinaryOp::Eq, - right: Box::new(Expr::Literal("1".into())), - }, - ], - ), - nonblocking: false, - }); + if prepare || inline.is_none_or(|inline| inline.inputs.is_empty()) { body.push(Stmt::If { - condition: Expr::Ident(success), - then_body: Vec::new(), - else_body: vec![ - Stmt::If { - condition: Expr::Ident(argument.clone()), - then_body: vec![Stmt::Eval(classes::object_call( - ObjectOp::ModePop, - vec![Expr::Ident(this.clone())], - ))], - else_body: Vec::new(), - }, - Stmt::Return(Some(Expr::Literal("32'sd0".into()))), - ], + condition: Expr::Ident(argument.clone()), + then_body: select, + else_body: Vec::new(), }); + body.extend(hook("pre_randomize")); + } + if prepare { + body.push(Stmt::Return(Some(Expr::Ident(this.clone())))); + Some(builder.declare("$prepared", handle.clone())) } else { - for property in &class.properties { - if property.constant - || property.field.handle.is_some() - || (property.r#type.is_enum && property.r#type.enum_values.is_none()) - { - continue; + if class.has_constraints || inline.is_some() { + let mut program = class.constraint_program()?; + if let Some(inline) = inline { + program.constraints.extend(inline.constraints.clone()); } + let success = builder.declare("$constraint_success", int_type()); + body.push(Stmt::Assign { + lhs: LValue::Ident(success.clone()), + rhs: classes::object_call( + ObjectOp::Randomize(program), + [ + vec![ + Expr::Ident(this.clone()), + Expr::Binary { + left: Box::new(Expr::Ident(argument.clone())), + op: BinaryOp::Eq, + right: Box::new(Expr::Literal("1".into())), + }, + ], + input_values.clone(), + ] + .concat(), + ), + nonblocking: false, + }); body.push(Stmt::If { - condition: Expr::Binary { - left: Box::new(Expr::Binary { - left: Box::new(Expr::Ident(argument.clone())), - op: BinaryOp::Ne, - right: Box::new(Expr::Literal("1".into())), - }), - op: BinaryOp::LogicAnd, - right: Box::new(classes::object_call( - ObjectOp::ModeGet( - property.field.offset, - property.rand || property.randc, - ), - vec![Expr::Ident(this.clone())], - )), - }, - then_body: random_property(&mut builder, property, &this), - else_body: Vec::new(), + condition: Expr::Ident(success), + then_body: Vec::new(), + else_body: vec![ + Stmt::If { + condition: Expr::Ident(argument.clone()), + then_body: vec![Stmt::Eval(classes::object_call( + ObjectOp::ModePop, + vec![Expr::Ident(this.clone())], + ))], + else_body: Vec::new(), + }, + Stmt::Return(Some(Expr::Literal("32'sd0".into()))), + ], }); + } else { + for property in &class.properties { + if property.constant + || property.field.handle.is_some() + || (property.r#type.is_enum + && property.r#type.enum_values.is_none()) + { + continue; + } + body.push(Stmt::If { + condition: Expr::Binary { + left: Box::new(Expr::Binary { + left: Box::new(Expr::Ident(argument.clone())), + op: BinaryOp::Ne, + right: Box::new(Expr::Literal("1".into())), + }), + op: BinaryOp::LogicAnd, + right: Box::new(classes::object_call( + ObjectOp::ModeGet( + property.field.offset, + property.rand || property.randc, + ), + vec![Expr::Ident(this.clone())], + )), + }, + then_body: random_property(&mut builder, property, &this), + else_body: Vec::new(), + }); + } } + body.extend(hook("post_randomize")); + body.push(Stmt::If { + condition: Expr::Ident(argument.clone()), + then_body: vec![Stmt::Eval(classes::object_call( + ObjectOp::ModePop, + vec![Expr::Ident(this.clone())], + ))], + else_body: Vec::new(), + }); + let returned = builder.declare("randomize", int_type()); + body.push(Stmt::Assign { + lhs: LValue::Ident(returned.clone()), + rhs: Expr::Literal("32'sd1".into()), + nonblocking: false, + }); + Some(returned) } - body.extend(hook("post_randomize")); - body.push(Stmt::If { - condition: Expr::Ident(argument.clone()), - then_body: vec![Stmt::Eval(classes::object_call( - ObjectOp::ModePop, - vec![Expr::Ident(this.clone())], - ))], - else_body: Vec::new(), - }); - let returned = builder.declare("randomize", int_type()); - body.push(Stmt::Assign { - lhs: LValue::Ident(returned.clone()), - rhs: Expr::Literal("32'sd1".into()), - nonblocking: false, - }); - Some(returned) } }; if method != "srandom" { @@ -262,10 +293,11 @@ fn lower_methods( { continue; } - let args = vec![ + let mut args = vec![ Some(Expr::Ident(this.clone())), Some(Expr::Ident(argument.clone())), ]; + args.extend(input_values.iter().cloned().map(Some)); let name = method_name(&name, method); let call = if method == "randomize" { vec![Stmt::Return(Some(Expr::Call { @@ -317,7 +349,9 @@ fn lower_methods( .map(|local| local.name.clone()) .collect(), persistent_locals: Vec::new(), - return_type: return_var.as_ref().map(|_| convert_type(int_type())), + return_type: return_var + .as_ref() + .map(|_| convert_type(if prepare { handle.clone() } else { int_type() })), params, return_var, body, @@ -427,6 +461,7 @@ pub(in crate::ast) fn inline_subroutines( key: request.key, class: std::rc::Rc::new(request.class.clone()), constraints: request.constraints.clone(), + inputs: request.inputs.clone(), dimensions: dimensions.clone(), }); } @@ -468,7 +503,19 @@ pub(in crate::ast) fn inline_subroutines( state, None, Some(&inline), + false, )?); + if !inline.inputs.is_empty() { + result.extend(lower_methods( + &class, + tree, + dims, + state, + None, + Some(&inline), + true, + )?); + } } } Ok((result, exported)) diff --git a/crates/celox-sv-analyzer/src/tests.rs b/crates/celox-sv-analyzer/src/tests.rs index 985f96f74f9..641dbb8670d 100644 --- a/crates/celox-sv-analyzer/src/tests.rs +++ b/crates/celox-sv-analyzer/src/tests.rs @@ -7374,3 +7374,42 @@ fn class_inline_randomize_retains_call_specific_constraints() { endmodule"#; analyze_source(source, Path::new("inline_randomize.sv")).unwrap(); } + +#[test] +fn class_inline_randomize_binds_local_inputs() { + let source = r#"module Top; + class C; rand bit [7:0] x; endclass + C obj; + bit [7:0] wanted, x; + initial begin + obj = new; + wanted = 2; x = 3; + obj.randomize() with { x == wanted; }; + obj.randomize() with { x == local::x; }; + end + endmodule"#; + analyze_source(source, Path::new("inline_inputs.sv")).unwrap(); +} + +#[test] +fn class_inline_randomize_static_names_do_not_capture_caller_locals() { + for (expression, supported) in [("x == limit", false), ("x == local::limit", true)] { + let source = format!( + r#"module Top; + class C; rand bit [7:0] x; static bit [7:0] limit = 5; endclass + C obj; bit [7:0] limit; + initial begin obj = new; limit = 3; obj.randomize() with {{ {expression}; }}; end + endmodule"# + ); + let result = analyze_source(&source, Path::new("inline_static_scope.sv")); + if supported { + result.unwrap(); + } else { + let error = result.unwrap_err().to_string(); + assert!( + error.contains("static properties in inline constraints"), + "{error}" + ); + } + } +} diff --git a/crates/celox-test-suite/src/sv/cases/class.vtest b/crates/celox-test-suite/src/sv/cases/class.vtest index b4b2156e1ab..185ea735a99 100644 --- a/crates/celox-test-suite/src/sv/cases/class.vtest +++ b/crates/celox-test-suite/src/sv/cases/class.vtest @@ -2247,3 +2247,95 @@ module Top(output bit done); endmodule "#) (top Top) (run_until 0) (assert_eq done 1)) + +(case constrained_randomize_inline_inputs + (source "inline_inputs.sv" #" +module Top(output bit done); + class Packet; + rand bit signed [7:0] x; + rand bit [15:0] y; + constraint c { y == 7; } + endclass + Packet p; + bit signed [7:0] wanted; + bit signed [7:0] x; + initial begin + p = new; + wanted = -2; + x = -3; + if (!(p.randomize() with { x == wanted; })) $fatal(1, "fallback input"); + if (p.x != -2 || p.y != 7 || wanted != -2) $fatal(1, "signed readonly input"); + if (!(p.randomize() with { x == local::x; })) $fatal(1, "local qualifier"); + if (p.x != -3 || x != -3) $fatal(1, "shadowed local"); + wanted = 4; + if (!(p.randomize() with { x inside {[wanted:6]}; })) $fatal(1, "input range"); + if (p.x < 4 || p.x > 6) $fatal(1, "new call input"); + wanted = p.x; + if (!(p.randomize(null) with { x == wanted; })) $fatal(1, "input check"); + wanted = 12; + if (p.randomize(y) with { y == wanted; }) $fatal(1, "input unsat"); + if (p.x < 4 || p.x > 6 || p.y != 7) $fatal(1, "unsat changed object"); + done = 1; + end +endmodule +"#) + (top Top) (run_until 0) (assert_eq done 1)) + +(case constrained_randomize_inline_input_methods + (source "inline_input_methods.sv" #" +package pkg; + class Base; + rand bit [7:0] x; + function int pick(bit [7:0] wanted); + return randomize() with { x == wanted; }; + endfunction + endclass +endpackage +module Top(output bit done); + class Derived extends pkg::Base; + rand bit [7:0] y; + constraint c { y == 9; } + endclass + Derived d; + pkg::Base b; + initial begin + d = new; b = d; + if (!b.pick(2) || d.x != 2 || d.y != 9) $fatal(1, "imported input"); + if (!b.pick(5) || d.x != 5 || d.y != 9) $fatal(1, "repeated method input"); + done = 1; + end +endmodule +"#) + (top Top) (run_until 0) (assert_eq done 1)) + +(case constrained_randomize_inline_input_hooks + (source "inline_input_hooks.sv" #" +module Top(output bit done); + bit [7:0] wanted; + class Base; + rand bit [7:0] x; + int pre_calls, post_calls; + function void pre_randomize(); wanted = 5; pre_calls++; endfunction + function void post_randomize(); post_calls++; endfunction + endclass + class Derived extends Base; + function void pre_randomize(); wanted = 6; pre_calls++; endfunction + endclass + Base p; + Derived d; + initial begin + p = new; wanted = 2; + if (!(p.randomize(x) with { x == wanted; })) $fatal(1, "hook input solve"); + if (p.x != 5 || wanted != 5 || p.pre_calls != 1 || p.post_calls != 1) + $fatal(1, "input sampled before hook"); + wanted = 2; + if (p.randomize(x) with { x == wanted; x == 2; }) $fatal(1, "hook unsat"); + if (p.x != 5 || p.pre_calls != 2 || p.post_calls != 1) $fatal(1, "hook failure state"); + d = new; p = d; wanted = 2; + if (!(p.randomize() with { x == wanted; }) || p.x != 6 || p.pre_calls != 1 || p.post_calls != 1) + $fatal(1, "dynamic input hook"); + done = 1; + end +endmodule +"#) + (top Top) (run_until 0) (assert_eq done 1)) diff --git a/crates/celox-test-suite/verification/repros/sv_inline_input_hooks.json b/crates/celox-test-suite/verification/repros/sv_inline_input_hooks.json new file mode 100644 index 00000000000..f7c9f1714b8 --- /dev/null +++ b/crates/celox-test-suite/verification/repros/sv_inline_input_hooks.json @@ -0,0 +1,17 @@ +{ + "specification": "IEEE 1800-2023 18.6.2, 18.7", + "observations": [ + { + "tool": "icarus", + "case": "class::constrained_randomize_inline_input_hooks", + "status": "compile_error", + "log": "/sv-icarus/class/constrained_randomize_inline_input_hooks/source_0.sv:17: syntax error\n/sv-icarus/class/constrained_randomize_inline_input_hooks/source_0.sv:17: error: Operand of unary ! is not a primary expression.\n/sv-icarus/class/constrained_randomize_inline_input_hooks/source_0.sv:17: error: Malformed statement\n/sv-icarus/class/constrained_randomize_inline_input_hooks/source_0.sv:17: error: Malformed conditional expression.\n/sv-icarus/class/constrained_randomize_inline_input_hooks/source_0.sv:21: syntax error\n/sv-icarus/class/constrained_randomize_inline_input_hooks/source_0.sv:21: error: Malformed conditional expression.\n/sv-icarus/class/constrained_randomize_inline_input_hooks/source_0.sv:24: syntax error\n/sv-icarus/class/constrained_randomize_inline_input_hooks/source_0.sv:24: error: Operand of unary ! is not a primary expression.\n/sv-icarus/class/constrained_randomize_inline_input_hooks/source_0.sv:25: error: Malformed statement\n/sv-icarus/class/constrained_randomize_inline_input_hooks/source_0.sv:24: error: Malformed conditional expression.\n" + }, + { + "tool": "verilator", + "case": "class::constrained_randomize_inline_input_hooks", + "status": "runtime_error", + "log": "[0] %Fatal: source_0.sv:19: Assertion failed in TOP.celox_suite_tb.dut: input sampled before hook\n%Error: /sv-verilator/class/constrained_randomize_inline_input_hooks/source_0.sv:19: Verilog $stop\nAborting...\n" + } + ] +} diff --git a/crates/celox-test-suite/verification/repros/sv_inline_input_icarus.json b/crates/celox-test-suite/verification/repros/sv_inline_input_icarus.json new file mode 100644 index 00000000000..453dae87fb7 --- /dev/null +++ b/crates/celox-test-suite/verification/repros/sv_inline_input_icarus.json @@ -0,0 +1,19 @@ +{ + "specification": "IEEE 1800-2023 18.7, 18.7.1", + "observations": [ + { + "tool": "icarus", + "version": "Icarus 13.0", + "case": "class::constrained_randomize_inline_inputs", + "status": "compile_error", + "log": "/sv-icarus/class/constrained_randomize_inline_inputs/source_0.sv:6: sorry: Constraint declarations not supported.\n/sv-icarus/class/constrained_randomize_inline_inputs/source_0.sv:15: syntax error\n/sv-icarus/class/constrained_randomize_inline_inputs/source_0.sv:15: error: Operand of unary ! is not a primary expression.\n/sv-icarus/class/constrained_randomize_inline_inputs/source_0.sv:15: error: Malformed statement\n/sv-icarus/class/constrained_randomize_inline_inputs/source_0.sv:15: error: Malformed conditional expression.\n/sv-icarus/class/constrained_randomize_inline_inputs/source_0.sv:17: syntax error\n/sv-icarus/class/constrained_randomize_inline_inputs/source_0.sv:17: error: Operand of unary ! is not a primary expression.\n/sv-icarus/class/constrained_randomize_inline_inputs/source_0.sv:17: error: Malformed statement\n/sv-icarus/class/constrained_randomize_inline_inputs/source_0.sv:17: error: Malformed conditional expression.\n/sv-icarus/class/constrained_randomize_inline_inputs/source_0.sv:20: syntax error\n/sv-icarus/class/constrained_randomize_inline_inputs/source_0.sv:20: error: Operand of unary ! is not a primary expression.\n/sv-icarus/class/constrained_randomize_inline_inputs/source_0.sv:20: error: Malformed statement\n/sv-icarus/class/constrained_randomize_inline_inputs/source_0.sv:20: error: Malformed conditional expression.\n/sv-icarus/class/constrained_randomize_inline_inputs/source_0.sv:23: syntax error\n/sv-icarus/class/constrained_randomize_inline_inputs/source_0.sv:23: error: Operand of unary ! is not a primary expression.\n/sv-icarus/class/constrained_randomize_inline_inputs/source_0.sv:23: error: Malformed statement\n/sv-icarus/class/constrained_randomize_inline_inputs/source_0.sv:23: error: Malformed conditional expression.\n/sv-icarus/class/constrained_randomize_inline_inputs/source_0.sv:25: syntax error\n/sv-icarus/class/constrained_randomize_inline_inputs/source_0.sv:25: error: Malformed conditional expression.\n" + }, + { + "tool": "icarus", + "version": "Icarus 13.0", + "case": "class::constrained_randomize_inline_input_methods", + "status": "compile_error", + "log": "/sv-icarus/class/constrained_randomize_inline_input_methods/source_0.sv:6: syntax error\n/sv-icarus/class/constrained_randomize_inline_input_methods/source_0.sv:6: error: Malformed statement\n/sv-icarus/class/constrained_randomize_inline_input_methods/source_0.sv:6: syntax error\n/sv-icarus/class/constrained_randomize_inline_input_methods/source_0.sv:6: error: Malformed statement\n/sv-icarus/class/constrained_randomize_inline_input_methods/source_0.sv:13: sorry: Constraint declarations not supported.\n" + } + ] +} diff --git a/crates/celox-test-suite/verification/sv/icarus/class.json b/crates/celox-test-suite/verification/sv/icarus/class.json index c0cd8261b6a..05ab75a150e 100644 --- a/crates/celox-test-suite/verification/sv/icarus/class.json +++ b/crates/celox-test-suite/verification/sv/icarus/class.json @@ -195,7 +195,9 @@ "LIMITATIONS.md#systemverilog-suite", "verification/repros/sv_inside_constraints.json", "verification/repros/sv_conditional_constraints.json", - "verification/repros/sv_inline_constraints.json" + "verification/repros/sv_inline_constraints.json", + "verification/repros/sv_inline_input_icarus.json", + "verification/repros/sv_inline_input_hooks.json" ], "id": "sv_icarus_scalar_constraints", "observed_version": "Icarus Verilog version 13.0 (devel)", @@ -221,7 +223,9 @@ "LIMITATIONS.md#systemverilog-suite", "verification/repros/sv_inside_constraints.json", "verification/repros/sv_conditional_constraints.json", - "verification/repros/sv_inline_constraints.json" + "verification/repros/sv_inline_constraints.json", + "verification/repros/sv_inline_input_icarus.json", + "verification/repros/sv_inline_input_hooks.json" ], "id": "sv_icarus_scalar_constraints", "observed_version": "Icarus Verilog version 13.0 (devel)", @@ -247,7 +251,9 @@ "LIMITATIONS.md#systemverilog-suite", "verification/repros/sv_inside_constraints.json", "verification/repros/sv_conditional_constraints.json", - "verification/repros/sv_inline_constraints.json" + "verification/repros/sv_inline_constraints.json", + "verification/repros/sv_inline_input_icarus.json", + "verification/repros/sv_inline_input_hooks.json" ], "id": "sv_icarus_scalar_constraints", "observed_version": "Icarus Verilog version 13.0 (devel)", @@ -273,7 +279,9 @@ "LIMITATIONS.md#systemverilog-suite", "verification/repros/sv_inside_constraints.json", "verification/repros/sv_conditional_constraints.json", - "verification/repros/sv_inline_constraints.json" + "verification/repros/sv_inline_constraints.json", + "verification/repros/sv_inline_input_icarus.json", + "verification/repros/sv_inline_input_hooks.json" ], "id": "sv_icarus_scalar_constraints", "observed_version": "Icarus Verilog version 13.0 (devel)", @@ -299,7 +307,9 @@ "LIMITATIONS.md#systemverilog-suite", "verification/repros/sv_inside_constraints.json", "verification/repros/sv_conditional_constraints.json", - "verification/repros/sv_inline_constraints.json" + "verification/repros/sv_inline_constraints.json", + "verification/repros/sv_inline_input_icarus.json", + "verification/repros/sv_inline_input_hooks.json" ], "id": "sv_icarus_scalar_constraints", "observed_version": "Icarus Verilog version 13.0 (devel)", @@ -325,7 +335,93 @@ "LIMITATIONS.md#systemverilog-suite", "verification/repros/sv_inside_constraints.json", "verification/repros/sv_conditional_constraints.json", - "verification/repros/sv_inline_constraints.json" + "verification/repros/sv_inline_constraints.json", + "verification/repros/sv_inline_input_icarus.json", + "verification/repros/sv_inline_input_hooks.json" + ], + "id": "sv_icarus_scalar_constraints", + "observed_version": "Icarus Verilog version 13.0 (devel)", + "phase": "compile", + "reason": "Icarus rejects constraint declarations and inline randomize with blocks before elaboration. IEEE 1800-2023 18.5 and 18.7 define these constructs. Celox retains all assertions.", + "upstream": [] + }, + "name": "class::constrained_randomize_inline_input_hooks", + "phase": "skip", + "status": "ignored", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] + }, + { + "category": "Types", + "detail": "Icarus rejects constraint declarations and inline randomize with blocks before elaboration. IEEE 1800-2023 18.5 and 18.7 define these constructs. Celox retains all assertions.", + "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", + "verification/repros/sv_inline_constraints.json", + "verification/repros/sv_inline_input_icarus.json", + "verification/repros/sv_inline_input_hooks.json" + ], + "id": "sv_icarus_scalar_constraints", + "observed_version": "Icarus Verilog version 13.0 (devel)", + "phase": "compile", + "reason": "Icarus rejects constraint declarations and inline randomize with blocks before elaboration. IEEE 1800-2023 18.5 and 18.7 define these constructs. Celox retains all assertions.", + "upstream": [] + }, + "name": "class::constrained_randomize_inline_input_methods", + "phase": "skip", + "status": "ignored", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] + }, + { + "category": "Types", + "detail": "Icarus rejects constraint declarations and inline randomize with blocks before elaboration. IEEE 1800-2023 18.5 and 18.7 define these constructs. Celox retains all assertions.", + "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", + "verification/repros/sv_inline_constraints.json", + "verification/repros/sv_inline_input_icarus.json", + "verification/repros/sv_inline_input_hooks.json" + ], + "id": "sv_icarus_scalar_constraints", + "observed_version": "Icarus Verilog version 13.0 (devel)", + "phase": "compile", + "reason": "Icarus rejects constraint declarations and inline randomize with blocks before elaboration. IEEE 1800-2023 18.5 and 18.7 define these constructs. Celox retains all assertions.", + "upstream": [] + }, + "name": "class::constrained_randomize_inline_inputs", + "phase": "skip", + "status": "ignored", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] + }, + { + "category": "Types", + "detail": "Icarus rejects constraint declarations and inline randomize with blocks before elaboration. IEEE 1800-2023 18.5 and 18.7 define these constructs. Celox retains all assertions.", + "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", + "verification/repros/sv_inline_constraints.json", + "verification/repros/sv_inline_input_icarus.json", + "verification/repros/sv_inline_input_hooks.json" ], "id": "sv_icarus_scalar_constraints", "observed_version": "Icarus Verilog version 13.0 (devel)", @@ -351,7 +447,9 @@ "LIMITATIONS.md#systemverilog-suite", "verification/repros/sv_inside_constraints.json", "verification/repros/sv_conditional_constraints.json", - "verification/repros/sv_inline_constraints.json" + "verification/repros/sv_inline_constraints.json", + "verification/repros/sv_inline_input_icarus.json", + "verification/repros/sv_inline_input_hooks.json" ], "id": "sv_icarus_scalar_constraints", "observed_version": "Icarus Verilog version 13.0 (devel)", @@ -377,7 +475,9 @@ "LIMITATIONS.md#systemverilog-suite", "verification/repros/sv_inside_constraints.json", "verification/repros/sv_conditional_constraints.json", - "verification/repros/sv_inline_constraints.json" + "verification/repros/sv_inline_constraints.json", + "verification/repros/sv_inline_input_icarus.json", + "verification/repros/sv_inline_input_hooks.json" ], "id": "sv_icarus_scalar_constraints", "observed_version": "Icarus Verilog version 13.0 (devel)", @@ -403,7 +503,9 @@ "LIMITATIONS.md#systemverilog-suite", "verification/repros/sv_inside_constraints.json", "verification/repros/sv_conditional_constraints.json", - "verification/repros/sv_inline_constraints.json" + "verification/repros/sv_inline_constraints.json", + "verification/repros/sv_inline_input_icarus.json", + "verification/repros/sv_inline_input_hooks.json" ], "id": "sv_icarus_scalar_constraints", "observed_version": "Icarus Verilog version 13.0 (devel)", @@ -429,7 +531,9 @@ "LIMITATIONS.md#systemverilog-suite", "verification/repros/sv_inside_constraints.json", "verification/repros/sv_conditional_constraints.json", - "verification/repros/sv_inline_constraints.json" + "verification/repros/sv_inline_constraints.json", + "verification/repros/sv_inline_input_icarus.json", + "verification/repros/sv_inline_input_hooks.json" ], "id": "sv_icarus_scalar_constraints", "observed_version": "Icarus Verilog version 13.0 (devel)", @@ -455,7 +559,9 @@ "LIMITATIONS.md#systemverilog-suite", "verification/repros/sv_inside_constraints.json", "verification/repros/sv_conditional_constraints.json", - "verification/repros/sv_inline_constraints.json" + "verification/repros/sv_inline_constraints.json", + "verification/repros/sv_inline_input_icarus.json", + "verification/repros/sv_inline_input_hooks.json" ], "id": "sv_icarus_scalar_constraints", "observed_version": "Icarus Verilog version 13.0 (devel)", diff --git a/crates/celox-test-suite/verification/sv/limitations.json b/crates/celox-test-suite/verification/sv/limitations.json index 607baebddef..3134fa38015 100644 --- a/crates/celox-test-suite/verification/sv/limitations.json +++ b/crates/celox-test-suite/verification/sv/limitations.json @@ -2608,7 +2608,9 @@ "LIMITATIONS.md#systemverilog-suite", "verification/repros/sv_inside_constraints.json", "verification/repros/sv_conditional_constraints.json", - "verification/repros/sv_inline_constraints.json" + "verification/repros/sv_inline_constraints.json", + "verification/repros/sv_inline_input_icarus.json", + "verification/repros/sv_inline_input_hooks.json" ], "cases": { "icarus": [ @@ -2622,7 +2624,10 @@ "class::constrained_randomize_inline", "class::constrained_randomize_inline_inheritance", "class::constrained_randomize_inline_basic", - "class::constrained_randomize_inline_package_instances" + "class::constrained_randomize_inline_package_instances", + "class::constrained_randomize_inline_inputs", + "class::constrained_randomize_inline_input_methods", + "class::constrained_randomize_inline_input_hooks" ] } }, @@ -2661,5 +2666,21 @@ "class::constrained_randomize_inline_inheritance" ] } + }, + { + "id": "sv_verilator_inline_inputs_before_pre_hook", + "category": "simulator_disagreement", + "phase": "execute", + "observed_version": "Verilator 5.052 2026-09-05 rev v5.052", + "reason": "Verilator copies caller-scope inline inputs into its generated helper before pre_randomize. When the hook changes wanted from 2 to 5, the solve still assigns x = 2. IEEE 1800-2023 18.6.2 invokes the pre-hook before randomization, and 18.7 permits references to caller-scope state variables. The regression retains the post-hook value and single-invocation assertions.", + "upstream": [], + "evidence": [ + "verification/repros/sv_inline_input_hooks.json" + ], + "cases": { + "verilator": [ + "class::constrained_randomize_inline_input_hooks" + ] + } } ] diff --git a/crates/celox-test-suite/verification/sv/verilator/class.json b/crates/celox-test-suite/verification/sv/verilator/class.json index cb0538712d6..2cf1b0997f6 100644 --- a/crates/celox-test-suite/verification/sv/verilator/class.json +++ b/crates/celox-test-suite/verification/sv/verilator/class.json @@ -172,6 +172,28 @@ "tag_reasons": {}, "tags": [] }, + { + "category": "Types", + "detail": "Verilator copies caller-scope inline inputs into its generated helper before pre_randomize. When the hook changes wanted from 2 to 5, the solve still assigns x = 2. IEEE 1800-2023 18.6.2 invokes the pre-hook before randomization, and 18.7 permits references to caller-scope state variables. The regression retains the post-hook value and single-invocation assertions.", + "expectation": "Simulation", + "known_issue": { + "category": "simulator_disagreement", + "evidence": [ + "verification/repros/sv_inline_input_hooks.json" + ], + "id": "sv_verilator_inline_inputs_before_pre_hook", + "observed_version": "Verilator 5.052 2026-09-05 rev v5.052", + "phase": "execute", + "reason": "Verilator copies caller-scope inline inputs into its generated helper before pre_randomize. When the hook changes wanted from 2 to 5, the solve still assigns x = 2. IEEE 1800-2023 18.6.2 invokes the pre-hook before randomization, and 18.7 permits references to caller-scope state variables. The regression retains the post-hook value and single-invocation assertions.", + "upstream": [] + }, + "name": "class::constrained_randomize_inline_input_hooks", + "phase": "skip", + "status": "ignored", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] + }, { "category": "Types", "detail": "", diff --git a/crates/celox/tests/frontends/systemverilog/class.rs b/crates/celox/tests/frontends/systemverilog/class.rs index c622ad2a374..96ff7dad702 100644 --- a/crates/celox/tests/frontends/systemverilog/class.rs +++ b/crates/celox/tests/frontends/systemverilog/class.rs @@ -955,3 +955,12 @@ sv_backends! { sv_backends! { fn constrained_randomize_inline_package_instances(sim) { @case "class::constrained_randomize_inline_package_instances"; } } + +sv_backends! { + fn constrained_randomize_inline_inputs(sim) { @case "class::constrained_randomize_inline_inputs"; } + fn constrained_randomize_inline_input_methods(sim) { @case "class::constrained_randomize_inline_input_methods"; } +} + +sv_backends! { + fn constrained_randomize_inline_input_hooks(sim) { @case "class::constrained_randomize_inline_input_hooks"; } +} diff --git a/docs/guide/systemverilog-reference.md b/docs/guide/systemverilog-reference.md index 293906005e3..c858387fd96 100644 --- a/docs/guide/systemverilog-reference.md +++ b/docs/guide/systemverilog-reference.md @@ -246,9 +246,19 @@ object constraints and hooks still apply. A failed solve preserves properties, skips the post-hook and restores temporary selection modes. Inline constraints do not remain installed for later calls. -This initial inline subset accepts unqualified instance-property references. -Caller-local captures, `local::`, `this` / `super` qualified references, restricted -`with (names)` lookup and scope `std::randomize(...) with` remain unsupported. +If an unqualified name is not an instance property, inline lookup falls back to +scalar variables and arguments in the calling scope. `local::name` bypasses the +receiver's properties explicitly (18.7, 18.7.1). These read-only call inputs support +1–64-bit two-state integral values, including signed values. They are read after +`pre_randomize()` and are not randomized or retained on the object. Package methods preserve their inputs through dynamic +dispatch to subclasses defined by the consumer. + +Receiver static-property references are currently rejected; they do not fall back +to caller variables of the same name. + +Four-state and aggregate inputs, typed parameter inputs, qualified `this` / `super` +references, restricted `with (names)` lookup and scope `std::randomize(...) with` +remain unsupported. A call through `null` is a runtime error.