Skip to content

Commit

Permalink
Adds come of owi's constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
filipeom committed Feb 11, 2024
1 parent d7afe42 commit c2644a8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/expr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,46 @@ let simplify (hte : t) : t =
in
loop hte

let unop ty (op : unop) (e : t) : t =
mk
@@
match e.node with
| Val (Num n) -> Val (Num (Eval_numeric.eval_unop ty op n))
| _ -> Unop (ty, op, e)

let binop ty (op : binop) (e1 : t) (e2 : t) =
match (e1.node, e2.node) with
| Val (Num n1), Val (Num n2) ->
mk @@ Val (Num (Eval_numeric.eval_binop ty op n1 n2))
| Ptr _, _ | _, Ptr _ ->
(* Does pointer arithmetic *)
simplify @@ mk (Binop (Ty_bitv 32, op, e1, e2))
| _ -> mk @@ Binop (ty, op, e1, e2)

let relop ty (op : relop) (e1 : t) (e2 : t) =
mk
@@
match (e1.node, e2.node) with
| Val (Num n1), Val (Num n2) ->
let res = Eval_numeric.eval_relop ty op n1 n2 in
Val (if res then True else False)
| Val (Num n), Ptr (b, { node = Val (Num o); _ }) ->
let base = Eval_numeric.eval_binop (Ty_bitv 32) Add (I32 b) o in
let res = Eval_numeric.eval_relop ty op n base in
Val (if res then True else False)
| Ptr (b, { node = Val (Num o); _ }), Val (Num n) ->
let base = Eval_numeric.eval_binop (Ty_bitv 32) Add (I32 b) o in
let res = Eval_numeric.eval_relop ty op base n in
Val (if res then True else False)
| _ -> Relop (ty, op, e1, e2)

let cvtop ty (op : cvtop) (e : t) =
mk
@@
match e.node with
| Val (Num n) -> Val (Num (Eval_numeric.eval_cvtop ty op n))
| _ -> Cvtop (ty, op, e)

module Bool = struct
let v b = mk @@ match b with true -> Val True | false -> Val False

Expand Down
8 changes: 8 additions & 0 deletions lib/expr.mli
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ val to_string : t -> string

val simplify : t -> t

val unop : Ty.t -> Ty.unop -> t -> t

val binop : Ty.t -> Ty.binop -> t -> t -> t

val relop : Ty.t -> Ty.relop -> t -> t -> t

val cvtop : Ty.t -> Ty.cvtop -> t -> t

module Hc : sig
val clear : unit -> unit

Expand Down

0 comments on commit c2644a8

Please sign in to comment.