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
11 changes: 10 additions & 1 deletion crates/celox-sv-analyzer/src/ast/casts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,17 @@ pub(super) fn runtime_cast_target_expr(
let width = expr_static_width(state, &expr, packed_dimensions).ok_or_else(|| {
unsupported("signedness cast of an operand without a static width")
})?;
let operand_signed =
dimensions::expression_signedness(&expr, packed_dimensions, const_env)
.ok_or_else(|| unsupported("cast of an operand whose signedness is unknown"))?;
return Ok(Expr::Resize {
expr: Box::new(expr),
// Signing changes the result type after evaluating the operand
// in its own type; it must not change a division's operator.
expr: Box::new(Expr::Resize {
expr: Box::new(expr),
width,
signed: operand_signed,
}),
width,
signed,
});
Expand Down
8 changes: 8 additions & 0 deletions crates/celox-sv-analyzer/src/ast/comb_rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ pub(super) fn expr_static_width(
}
}
Expr::Inside { .. } => Some(1),
// The synthetic unknown arm has the guarded operation's width.
Expr::Mux {
then_expr,
else_expr,
..
} if matches!(&**then_expr, Expr::Literal(text) if text == crate::DIV_ZERO_UNKNOWN_LITERAL) => {
expr_static_width(state, else_expr, packed_dimensions)
}
Expr::Mux {
then_expr,
else_expr,
Expand Down
8 changes: 8 additions & 0 deletions crates/celox-sv-analyzer/src/ast/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
use super::*;

pub(super) fn guard_zero_divisions(state: &AnalysisState, expr: Expr) -> Expr {
// Binding an enclosing expression can visit an already guarded operand.
// Its children were guarded when it was constructed. Wrapping it again
// loses the operation's type when the internal unknown arm is lowered.
if matches!(&expr, Expr::Mux { then_expr, .. }
if matches!(&**then_expr, Expr::Literal(text) if text == crate::DIV_ZERO_UNKNOWN_LITERAL))
{
return expr;
}
match expr {
Expr::Ident(_) | Expr::Literal(_) => expr,
Expr::Select {
Expand Down
14 changes: 14 additions & 0 deletions crates/celox-sv-analyzer/src/ast/inlining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ pub(super) fn expr_signedness_with_return_types(
)
}
}
// The internal zero-divisor arm represents an unknown value of the
// guarded operation's type, not a separate source-language operand.
Expr::Mux {
then_expr,
else_expr,
..
} if matches!(&**then_expr, Expr::Literal(text) if text == crate::DIV_ZERO_UNKNOWN_LITERAL) => {
expr_signedness_with_return_types(
else_expr,
identifiers,
functions,
function_return_types,
)
}
Expr::Mux {
then_expr,
else_expr,
Expand Down
69 changes: 69 additions & 0 deletions crates/celox-test-suite/src/sv/cases/operators.vtest
Original file line number Diff line number Diff line change
Expand Up @@ -1690,3 +1690,72 @@ endmodule
(assert_eq mul 0xfffffffc)
(assert_eq div 0)
(assert_eq dynamic_mul 0xfffffff0))

; IEEE 1800-2023 6.24.1: a size cast preserves operand signedness.
(case casts_preserve_guarded_division_signedness
(source "guarded_division_casts.sv" #"
module Top #(parameter DIVISOR = 2)(
input logic signed [12:0] a,
input logic signed [3:0] b,
input logic [3:0] u,
output logic [31:0] quotient, remainder, mixed, narrowed, nested,
output logic [31:0] parameter_quotient, typed, unsigned_quotient, signed_mixed);
assign quotient = 32'(a / b);
assign remainder = 32'(a % b);
assign mixed = 32'(a / u);
assign narrowed = 4'(a / b);
assign nested = 32'((a / b) / b);
assign parameter_quotient = 32'(a / DIVISOR);
assign typed = int'(a / b);
assign unsigned_quotient = unsigned'(a / b);
assign signed_mixed = signed'(a / u);
endmodule
"#)
(top Top)
(modify (set a 0x1ff3) (set b 2) (set u 2))
(assert_eq quotient 0xfffffffa)
(assert_eq remainder 0xffffffff)
(assert_eq mixed 4089)
(assert_eq narrowed 0xfffffffa)
(assert_eq nested 0xfffffffd)
(assert_eq parameter_quotient 0xfffffffa)
(assert_eq typed 0xfffffffa)
(assert_eq unsigned_quotient 8186)
(assert_eq signed_mixed 4089)
(modify (set u 1))
(assert_eq signed_mixed 0xfffffff3)
(modify (set u 2))
(modify (set a 13) (set b 0xe))
(assert_eq quotient 0xfffffffa)
(assert_eq remainder 1)
(assert_eq mixed 6)
(assert_eq narrowed 0xfffffffa)
(assert_eq nested 3)
(assert_eq parameter_quotient 6)
(assert_eq typed 0xfffffffa)
(assert_eq unsigned_quotient 8186)
(assert_eq signed_mixed 6))

(case casts_preserve_guarded_division_unknowns
(four_state)
(source "guarded_division_unknowns.sv" #"
module Top(input logic signed [12:0] a, input logic signed [3:0] b,
output logic [31:0] quotient, remainder, nested,
output int two_state);
assign quotient = 32'(a / b);
assign remainder = 32'(a % b);
assign nested = 32'((a / b) / b);
assign two_state = int'(a / b);
endmodule
"#)
(top Top)
(modify (set a 0x1ff3) (set b 2))
(assert_eq quotient 0xfffffffa)
(assert_eq remainder 0xffffffff)
(assert_eq nested 0xfffffffd)
(assert_eq two_state 0xfffffffa)
(modify (set b 0))
(assert_eq quotient (fourstate 0xffffffff 0xffffffff))
(assert_eq remainder (fourstate 0xffffffff 0xffffffff))
(assert_eq nested (fourstate 0xffffffff 0xffffffff))
(assert_eq two_state 0))
8 changes: 8 additions & 0 deletions crates/celox/tests/frontends/systemverilog/operators.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
sv_backends! {
fn casts_preserve_guarded_division_signedness(sim) {
@case "operators::casts_preserve_guarded_division_signedness";
}

fn casts_preserve_guarded_division_unknowns(sim) {
@case "operators::casts_preserve_guarded_division_unknowns";
}

fn preserves_power_signedness_in_arithmetic(sim) {
@case "operators::preserves_power_signedness_in_arithmetic";
}
Expand Down
Loading