summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Kerneis2014-04-03 16:25:04 +0100
committerGabriel Kerneis2014-04-03 17:28:45 +0100
commit86143f90a53dd488f2b04009f38eb4dc6cef1e15 (patch)
treedb72eb2ac6437472ca6dc25aba17e1b885506a72 /src
parent499e9b0104aa9592a312a2cad6fa259eb87e2468 (diff)
Implement bitwise library operations
Diffstat (limited to 'src')
-rw-r--r--src/lem_interp/interp_lib.lem14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/lem_interp/interp_lib.lem b/src/lem_interp/interp_lib.lem
index f2c7ef0f..b9fdb5a1 100644
--- a/src/lem_interp/interp_lib.lem
+++ b/src/lem_interp/interp_lib.lem
@@ -5,6 +5,7 @@ import Maybe_extra
open import Num
open import List
open import Word
+open import Bool
let ignore_sail x = V_lit (L_aux L_unit Unknown) ;;
@@ -29,6 +30,15 @@ let bool_to_bit b = match b with
| true -> V_lit (L_aux L_one Unknown)
end ;;
+let bitwise_not (V_vector idx inc v) =
+ let apply x = bool_to_bit(not (bit_to_bool x)) in
+ V_vector idx inc (List.map apply v)
+
+let bitwise_binop op (V_tuple [V_vector idx inc v; V_vector idx' inc' v']) =
+ (* typechecker ensures inc = inc', idx = idx' and length v = length v' *)
+ let apply (x, y) = bool_to_bit(op (bit_to_bool x) (bit_to_bool y)) in
+ V_vector idx inc (List.map apply (List.zip v v'))
+
(* BitSeq expects LSB first.
* By convention, MSB is on the left, so increasing = Big-Endian (MSB0),
* hence MSB first.
@@ -71,6 +81,10 @@ let function_map = [
(* XXX the size of the target vector should be given by the interpreter *)
("to_vec_inc", to_vec_inc 64);
("to_vec_dec", to_vec_dec 64);
+ ("bitwise_not", bitwise_not);
+ ("bitwise_and", bitwise_binop (&&));
+ ("bitwise_or", bitwise_binop (||));
+ ("bitwise_xor", bitwise_binop xor);
] ;;
let eval_external name v = (Maybe_extra.fromJust (List.lookup name function_map)) v ;;