Skip to content

Commit

Permalink
Fix wrong safemath lowering
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed May 27, 2022
1 parent f810961 commit f4e82f5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 3 additions & 1 deletion crates/codegen/src/yul/isel/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,12 +494,13 @@ impl<'db, 'a> FuncLowerHelper<'db, 'a> {
}

fn lower_unary(&mut self, op: UnOp, value: ValueId) -> yul::Expression {
let value_ty = self.body.store.value_ty(value);
let value = self.value_expr(value);
match op {
UnOp::Not => expression! { iszero([value])},
UnOp::Neg => {
let zero = literal_expression! {0};
expression! { sub([zero], [value])}
self.ctx.runtime.safe_sub(self.db, zero, value, value_ty)
}
UnOp::Inv => expression! { not([value])},
}
Expand Down Expand Up @@ -535,6 +536,7 @@ impl<'db, 'a> FuncLowerHelper<'db, 'a> {
BinOp::Mod => self.ctx.runtime.safe_mod(self.db, lhs, rhs, inst_result_ty),
BinOp::Pow => self.ctx.runtime.safe_pow(self.db, lhs, rhs, inst_result_ty),
BinOp::Shl => expression! {shl([rhs], [lhs])},
BinOp::Shr if is_signed => expression! {sar([rhs], [lhs])},
BinOp::Shr => expression! {shr([rhs], [lhs])},
BinOp::BitOr | BinOp::LogicalOr => expression! {or([lhs], [rhs])},
BinOp::BitXor => expression! {xor([lhs], [rhs])},
Expand Down
2 changes: 2 additions & 0 deletions newsfragments/723.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Properly lower right shift operation to yul's `sar` if operand is signed type
* Properly lower negate operation to call `safe_sub`

0 comments on commit f4e82f5

Please sign in to comment.