Skip to content

Commit

Permalink
[JSON]: Extract register information
Browse files Browse the repository at this point in the history
Fixes #10
  • Loading branch information
AbhinavMir authored and ThinkOpenly committed Sep 16, 2024
1 parent ffcccb7 commit 5fbbd37
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/sail_json_backend/json.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ let op_functions = Hashtbl.create 997
let formats = Hashtbl.create 997
let extensions = Hashtbl.create 997
let mappings = Hashtbl.create 997
let registers = Hashtbl.create 997

let debug_print ?(printer = prerr_endline) message = if debug_enabled then printer message else ()

Expand All @@ -98,6 +99,21 @@ let dequote qs =

let string_of_arg = function E_aux (E_id id, _) -> "\"" ^ string_of_id id ^ "\"" | exp -> "exp " ^ string_of_exp exp

(* Function to parse the register type from the register name *)
let json_of_registers () =
let regs = Hashtbl.fold (fun name regtype accum -> (name, regtype) :: accum) registers [] in
let sorted_regs =
List.sort (fun (n1, t1) (n2, t2) -> match String.compare t1 t2 with 0 -> String.compare n1 n2 | c -> c) regs
in
let json_regs =
List.map
(fun (name, regtype) ->
"\n {\n" ^ " \"name\": \"" ^ name ^ "\",\n" ^ " \"type\": \"" ^ regtype ^ "\"\n" ^ " }"
)
sorted_regs
in
"[ " ^ String.concat ",\n" json_regs ^ "\n]"

let rec string_list_of_mpat x =
match x with
| MP_aux (MP_lit l, _) ->
Expand Down Expand Up @@ -675,6 +691,10 @@ let defs { defs; _ } =
| DEF_aux (DEF_mapdef (MD_aux (MD_mapping (i, _, ml), _)), _) ->
debug_print "DEF_mapdef";
List.iter (parse_mapcl i) ml
| DEF_aux (DEF_register (DEC_aux (DEC_reg (atyp, id, _), _)), _) ->
let reg_name = string_of_id id in
let reg_type = string_of_typ atyp in
Hashtbl.add registers reg_name reg_type
| _ -> debug_print ~printer:prerr_string ""
)
defs;
Expand Down Expand Up @@ -732,6 +752,9 @@ let defs { defs; _ } =
(String.concat ",\n" (List.map (fun a -> json_of_instruction (List.hd a) (List.tl a)) key_mnemonic_sorted));

print_endline " ],";
print_endline " \"registers\": ";
print_endline (json_of_registers ());
print_endline ",";

print_endline " \"formats\": [";
let format_list = Hashtbl.fold (fun k v accum -> ("\"" ^ v ^ "\"") :: accum) formats [] in
Expand Down

0 comments on commit 5fbbd37

Please sign in to comment.