summaryrefslogtreecommitdiff
path: root/src/ast_util.ml
diff options
context:
space:
mode:
authorThomas Bauereiss2019-11-29 16:17:27 +0000
committerThomas Bauereiss2019-11-29 16:17:54 +0000
commitc8dc80c02cd32ccfb3609d5a5de30e3f8bbd5bf5 (patch)
tree7b0480fba859f4b12be9b31d2ad65b9d58ff5669 /src/ast_util.ml
parenta5d0c2ed7a6d70135b78ab439fa1e48c0ca7302d (diff)
parent325ec55dea017c7b095c407454835014d31f70b8 (diff)
Merge branch 'word-numerals' into sail2
Diffstat (limited to 'src/ast_util.ml')
-rw-r--r--src/ast_util.ml35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/ast_util.ml b/src/ast_util.ml
index 6544d3b4..d00120de 100644
--- a/src/ast_util.ml
+++ b/src/ast_util.ml
@@ -1661,6 +1661,41 @@ let hex_to_bin hex =
|> List.map Sail_lib.char_of_bit
|> (fun bits -> String.init (List.length bits) (List.nth bits))
+let explode s =
+ let rec exp i l = if i < 0 then l else exp (i - 1) (s.[i] :: l) in
+ exp (String.length s - 1) []
+
+let vector_string_to_bit_list (L_aux (lit, l)) =
+
+ let hexchar_to_binlist = function
+ | '0' -> ['0';'0';'0';'0']
+ | '1' -> ['0';'0';'0';'1']
+ | '2' -> ['0';'0';'1';'0']
+ | '3' -> ['0';'0';'1';'1']
+ | '4' -> ['0';'1';'0';'0']
+ | '5' -> ['0';'1';'0';'1']
+ | '6' -> ['0';'1';'1';'0']
+ | '7' -> ['0';'1';'1';'1']
+ | '8' -> ['1';'0';'0';'0']
+ | '9' -> ['1';'0';'0';'1']
+ | 'A' -> ['1';'0';'1';'0']
+ | 'B' -> ['1';'0';'1';'1']
+ | 'C' -> ['1';'1';'0';'0']
+ | 'D' -> ['1';'1';'0';'1']
+ | 'E' -> ['1';'1';'1';'0']
+ | 'F' -> ['1';'1';'1';'1']
+ | _ -> raise (Reporting.err_unreachable l __POS__ "hexchar_to_binlist given unrecognized character") in
+
+ let s_bin = match lit with
+ | L_hex s_hex -> List.flatten (List.map hexchar_to_binlist (explode (String.uppercase_ascii s_hex)))
+ | L_bin s_bin -> explode s_bin
+ | _ -> raise (Reporting.err_unreachable l __POS__ "s_bin given non vector literal") in
+
+ List.map (function '0' -> L_aux (L_zero, gen_loc l)
+ | '1' -> L_aux (L_one, gen_loc l)
+ | _ -> raise (Reporting.err_unreachable (gen_loc l) __POS__ "binary had non-zero or one")) s_bin
+
+
(* Functions for working with locations *)
let locate_id f (Id_aux (name, l)) = Id_aux (name, f l)