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
34 changes: 27 additions & 7 deletions crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3279,17 +3279,19 @@ fn evaluated_parameter_overrides(
continue;
};
if !matches!(value, sv::ir::ConstExpr::Literal(value) if value == "$") {
sv::typecheck::eval_const_integral_literal_with_types(
let integral = sv::typecheck::eval_const_integral_literal_with_types(
value,
&constants,
&HashMap::default(),
)
.ok_or_else(|| {
sv::AnalyzerError::Unsupported(format!(
"non-integer module parameter override `{}`",
);
if integral.is_none()
&& sv::typecheck::eval_const_real_expr(value, &constants).is_none()
{
return Err(sv::AnalyzerError::Unsupported(format!(
"nonconstant module parameter override `{}`",
parameter.name
))
})?;
)));
}
}
evaluated.insert(parameter.name.clone(), value.clone());
}
Expand Down Expand Up @@ -9140,6 +9142,24 @@ pub(crate) fn unsupported_sv_inout(path: String) -> ParserError {
mod tests {
use super::*;

#[test]
fn parameter_override_validation_rejects_unresolved_values() {
let overrides = [LoweredSvParameterOverride {
name: "P".into(),
value: Some(sv::ir::ConstExpr::Ident("unresolved".into())),
bound_type: None,
bound_pattern: None,
}];
let error = evaluated_parameter_overrides(&overrides).unwrap_err();
assert_eq!(error.tracking_issue(), 461);
assert!(
error
.to_string()
.contains("nonconstant module parameter override `P`"),
"{error}"
);
}

/// The process kernels of `source`, other than declaration initializers.
fn procedure_kernels(source: &str) -> usize {
let scheduled = schedule_sources(
Expand Down
1 change: 1 addition & 0 deletions crates/celox-sv-analyzer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ const UNSUPPORTED_CONSTRUCT_ISSUES: &[(&str, u32)] = &[
("gate primitive instantiation", 457),
("undriven net declaration", 460),
("non-integer module parameter override", 461),
("nonconstant module parameter override", 461),
("always_ff event expression", 464),
("package net", 1146),
];
Expand Down
9 changes: 9 additions & 0 deletions crates/celox-sv-analyzer/src/typecheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,15 @@ pub fn eval_const_expr_with_types(
eval_const_expr(&expr, constants)
}

/// Evaluate a numeric constant expression in a real context.
///
/// Integral subexpressions retain their integer arithmetic before conversion.
/// Callers that only validate a parameter binding should preserve the original
/// expression so the receiving declaration still supplies its type context.
pub fn eval_const_real_expr(expr: &ConstExpr, constants: &HashMap<String, i128>) -> Option<f64> {
real::evaluate(expr, constants)
}

/// Evaluate a literal constant expression without discarding four-state masks.
pub fn eval_const_integral_literal_with_types(
expr: &ConstExpr,
Expand Down
94 changes: 94 additions & 0 deletions crates/celox-test-suite/src/sv/cases/floating_point.vtest
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,97 @@ endmodule
"#)
(top Top) (run_until 0) (assert_eq done 1)
(expect_output "file=[ 1.50] expr=[3.0]\nwrite=[1.5e+00]\n"))

; IEEE 1800-2023 6.20.2: untyped parameters take the type of their final value.
(case real_parameter_overrides_preserve_fractional_values
(source "real_parameter_overrides.sv" #"
module Leaf #(parameter P=1)(output wire [63:0] bits,
output int truncated, rounded);
localparam COUNT_W=$clog2($rtoi(P)+1);
localparam logic [COUNT_W-1:0] COUNT=COUNT_W'($rtoi(P));
assign bits=$realtobits(P);
assign truncated=COUNT;
assign rounded=P;
endmodule
module Negative #(parameter P=-1)(output wire [63:0] bits,
output int truncated, rounded);
assign bits=$realtobits(P);
assign truncated=$rtoi(P);
assign rounded=P;
endmodule
module Relay #(parameter P=2)(output wire [63:0] bits,
output int truncated, rounded);
Leaf #(.P(P+0.25)) leaf(.bits(bits),.truncated(truncated),.rounded(rounded));
endmodule
module Top(output wire [63:0] direct_bits, forwarded_bits, negative_bits,
output int direct_truncated, direct_rounded,
forwarded_truncated, forwarded_rounded,
negative_truncated, negative_rounded);
localparam PERIOD=6.4;
Leaf #(.P(125000/PERIOD)) direct(.bits(direct_bits),.truncated(direct_truncated),
.rounded(direct_rounded));
Relay #(.P(125000/PERIOD)) forwarded(.bits(forwarded_bits),.truncated(forwarded_truncated),
.rounded(forwarded_rounded));
Negative #(.P(-2.5)) negative(.bits(negative_bits),.truncated(negative_truncated),
.rounded(negative_rounded));
endmodule
"#)
(top Top)
(eval)
(assert_eq direct_bits 0x40d312d000000000)
(assert_eq forwarded_bits 0x40d312e000000000)
(assert_eq negative_bits 0xc004000000000000)
(assert_eq direct_truncated 19531) (assert_eq direct_rounded 19531)
(assert_eq forwarded_truncated 19531) (assert_eq forwarded_rounded 19532)
(assert_eq negative_truncated 0xfffffffe) (assert_eq negative_rounded 0xfffffffd))

(case real_parameter_overrides_keep_declared_conversion_contexts
(source "typed_real_parameter_overrides.sv" #"
module Typed #(parameter real R=0.0, parameter shortreal S=0.0, parameter int I=0)
(output wire [63:0] real_bits, output wire [31:0] short_bits, output int integer_value);
assign real_bits=$realtobits(R);
assign short_bits=$shortrealtobits(S);
assign integer_value=I;
endmodule
module Top(output wire [63:0] fractional_bits, integer_bits,
output wire [31:0] narrowed_bits, negative_zero_bits,
output int negative_value, positive_value);
localparam real LARGE=16777217.0;
Typed #(.R(LARGE/2.0),.S(LARGE),.I(-2.5)) negative(.real_bits(fractional_bits),
.short_bits(narrowed_bits),.integer_value(negative_value));
Typed #(.R(5),.S(-0.0),.I(2.5)) positive(.real_bits(integer_bits),
.short_bits(negative_zero_bits),.integer_value(positive_value));
endmodule
"#)
(top Top)
(eval)
(assert_eq fractional_bits 0x4160000010000000)
(assert_eq integer_bits 0x4014000000000000)
(assert_eq narrowed_bits 0x4b800000)
(assert_eq negative_zero_bits 0x80000000)
(assert_eq negative_value 0xfffffffd) (assert_eq positive_value 3))

(case real_parameter_overrides_from_math_and_generate_scopes
(source "generated_real_parameter_overrides.sv" #"
package Values;
localparam real STEP=0.25;
endpackage
module Leaf #(parameter P=0)(output wire [63:0] bits);
assign bits=$realtobits(P);
endmodule
module Top(output wire [191:0] generated_bits,
output wire [63:0] math_bits, selected_bits);
import Values::*;
localparam BASE=$sqrt(2.25);
for(genvar i=0;i<3;i++) begin: g
Leaf #(.P(BASE+i*STEP)) leaf(.bits(generated_bits[i*64 +: 64]));
end
Leaf #(.P($sqrt(2.25))) math(.bits(math_bits));
Leaf #(.P(1 ? -0.0 : 1.5)) selected(.bits(selected_bits));
endmodule
"#)
(top Top)
(eval)
(assert_eq generated_bits 0x40000000000000003ffc0000000000003ff8000000000000)
(assert_eq math_bits 0x3ff8000000000000)
(assert_eq selected_bits 0x8000000000000000))
10 changes: 10 additions & 0 deletions crates/celox/tests/frontends/systemverilog/floating_point.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
use super::*;

sv_backends! {
fn real_parameter_overrides_preserve_fractional_values(sim) {
@case "floating_point::real_parameter_overrides_preserve_fractional_values";
}
fn real_parameter_overrides_keep_declared_conversion_contexts(sim) {
@case "floating_point::real_parameter_overrides_keep_declared_conversion_contexts";
}
fn real_parameter_overrides_from_math_and_generate_scopes(sim) {
@case "floating_point::real_parameter_overrides_from_math_and_generate_scopes";
}

fn real_math_runtime_functions(sim) { @case "real_math::runtime_functions"; }
fn real_math_constant_functions(sim) { @case "real_math::constant_functions"; }
fn real_arithmetic_and_rounding(sim) { @case "floating_point::real_arithmetic_and_rounding"; }
Expand Down
Loading