Skip to content

Commit

Permalink
New warning to report constant zero multiplications (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
denismerigoux authored Feb 8, 2023
2 parents 03e2fd2 + 480489c commit 0435165
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/mlang/backend_compilers/decoupledExpr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ let mult (e1 : constr) (e2 : constr) (st : local_stacks) (ctx : local_vars) : t
match (e1, e2) with
| Dlit 1., _ -> (e2, Val, lv2)
| _, Dlit 1. -> (e1, Val, lv1)
| Dlit 0., _ | _, Dlit 0. -> (Dlit 0., Val, [])
| Dlit f1, Dlit f2 -> (Dlit (f1 *. f2), Val, [])
| _ -> (Dbinop ("*", e1, e2), Val, lv2 @ lv1)

Expand Down
9 changes: 9 additions & 0 deletions src/mlang/m_frontend/mast_to_mir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,15 @@ let rec translate_expression (ctx : translating_context)
let new_e2 = translate_expression ctx e2 in
Mir.Comparison (op, new_e1, new_e2)
| Mast.Binop (op, e1, e2) ->
if
Pos.unmark op = Mast.Mul
&& (Pos.unmark e1 = Mast.Literal (Float 0.)
|| Pos.unmark e2 = Mast.Literal (Float 0.))
then
(* It is difficult to do a broader or deeper analysis because of
constant substitutions that could wrongly trigger the warning *)
Errors.print_spanned_warning
"Nullifying constant multiplication found." (Pos.get_position f);
let new_e1 = translate_expression ctx e1 in
let new_e2 = translate_expression ctx e2 in
Mir.Binop (op, new_e1, new_e2)
Expand Down

0 comments on commit 0435165

Please sign in to comment.