Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
hhugo committed Sep 27, 2024
1 parent 5f100a9 commit 45cab17
Showing 1 changed file with 25 additions and 29 deletions.
54 changes: 25 additions & 29 deletions compiler/lib/eval.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,21 @@ let set_static_env s value = Hashtbl.add static_env s value

let get_static_env s = try Some (Hashtbl.find static_env s) with Not_found -> None

module Targetint = struct
include Targetint

let int_unop l f =
match l with
| [ Int i ] -> Some (Int (f i))
| _ -> None
let int_unop l f =
match l with
| [ Int i ] -> Some (Int (f i))
| _ -> None

let int_binop l f =
match l with
| [ Int i; Int j ] -> Some (Int (f i j))
| _ -> None
let int_binop l f =
match l with
| [ Int i; Int j ] -> Some (Int (f i j))
| _ -> None

(* For when the underlying function takes an [int] (not [t]) as its second argument *)
let shift_op l f =
match l with
| [ Int i; Int j ] -> Some (Int (f i (to_int_exn j)))
| _ -> None
end
(* For when the underlying function takes an [int] (not [t]) as its second argument *)
let shift_op l f =
match l with
| [ Int i; Int j ] -> Some (Int (f i (Targetint.to_int_exn j)))
| _ -> None

let float_binop_aux (l : constant list) (f : float -> float -> 'a) : 'a option =
let args =
Expand Down Expand Up @@ -94,19 +90,19 @@ let eval_prim x =
let name = Primitive.resolve name in
match name, l with
(* int *)
| "%int_add", _ -> Targetint.int_binop l Targetint.add
| "%int_sub", _ -> Targetint.int_binop l Targetint.sub
| "%direct_int_mul", _ -> Targetint.int_binop l Targetint.mul
| "%int_add", _ -> int_binop l Targetint.add
| "%int_sub", _ -> int_binop l Targetint.sub
| "%direct_int_mul", _ -> int_binop l Targetint.mul
| "%direct_int_div", [ _; Int x ] when Targetint.is_zero x -> None
| "%direct_int_div", _ -> Targetint.int_binop l Targetint.div
| "%direct_int_mod", _ -> Targetint.int_binop l Targetint.rem
| "%int_and", _ -> Targetint.int_binop l Targetint.logand
| "%int_or", _ -> Targetint.int_binop l Targetint.logor
| "%int_xor", _ -> Targetint.int_binop l Targetint.logxor
| "%int_lsl", _ -> Targetint.shift_op l Targetint.shift_left
| "%int_lsr", _ -> Targetint.shift_op l Targetint.shift_right_logical
| "%int_asr", _ -> Targetint.shift_op l Targetint.shift_right
| "%int_neg", _ -> Targetint.int_unop l Targetint.neg
| "%direct_int_div", _ -> int_binop l Targetint.div
| "%direct_int_mod", _ -> int_binop l Targetint.rem
| "%int_and", _ -> int_binop l Targetint.logand
| "%int_or", _ -> int_binop l Targetint.logor
| "%int_xor", _ -> int_binop l Targetint.logxor
| "%int_lsl", _ -> shift_op l Targetint.shift_left
| "%int_lsr", _ -> shift_op l Targetint.shift_right_logical
| "%int_asr", _ -> shift_op l Targetint.shift_right
| "%int_neg", _ -> int_unop l Targetint.neg
(* float *)
| "caml_eq_float", _ -> float_binop_bool l Float.( = )
| "caml_neq_float", _ -> float_binop_bool l Float.( <> )
Expand Down

0 comments on commit 45cab17

Please sign in to comment.