-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add mnemonic mapping for pseudoinstructions #45
base: json
Are you sure you want to change the base?
Changes from 6 commits
b5c738a
783185a
9c62811
dd68065
a81da7f
49dd15f
20cd11c
eb4611c
35af3fd
b95e0f2
67fa077
e2799d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,6 +91,7 @@ let formats = Hashtbl.create 997 | |
let extensions = Hashtbl.create 997 | ||
let mappings = Hashtbl.create 997 | ||
let registers = Hashtbl.create 997 | ||
let base_instructions = Hashtbl.create 997 | ||
|
||
let debug_print ?(printer = prerr_endline) message = if debug_enabled then printer message else () | ||
|
||
|
@@ -433,14 +434,105 @@ let parse_funcl fcl = | |
debug_print ("id_of_dependent: " ^ id); | ||
let source_code = extract_source_code (Ast_util.exp_loc e) in | ||
Hashtbl.add functions id source_code | ||
| Pat_exp (P_aux (P_app (i, pl), _), e) | Pat_when (P_aux (P_app (i, pl), _), e, _) -> | ||
| Pat_exp (P_aux (P_app (i, pl), _), e) | Pat_when (P_aux (P_app (i, pl), _), e, _) -> ( | ||
debug_print ("FCL_funcl execute " ^ string_of_id i); | ||
let source_code = extract_source_code (Ast_util.exp_loc e) in | ||
Hashtbl.add executes (string_of_id i) source_code | ||
match id with | ||
| "pseudo_of" -> ( | ||
debug_print ("FCL funcl pseudoinstruction " ^ string_of_id i); | ||
match e with | ||
| E_aux (E_list exp_list, _) -> | ||
debug_print ("Exp el: " ^ String.concat ", " (List.map string_of_exp exp_list)); | ||
List.iter | ||
(fun exp -> | ||
match exp with | ||
| E_aux (E_app (id, el), _) -> | ||
List.iter | ||
(fun inner_exp -> | ||
match inner_exp with | ||
| E_aux (E_app (id_inner, el_inner), _) -> | ||
List.iteri | ||
(fun index inner_value -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need |
||
match inner_value with | ||
| E_aux (E_tuple tuple_list, _) -> | ||
let args_inner_list = List.map string_of_exp tuple_list in | ||
debug_print | ||
("Adding to hashtable with key: " ^ string_of_id i ^ ", id_inner: " | ||
^ string_of_id id_inner ^ ", args_inner_list: [" | ||
^ String.concat ", " args_inner_list ^ "]" | ||
); | ||
Hashtbl.add base_instructions (string_of_id i) | ||
(string_of_id id_inner, args_inner_list) | ||
| _ -> () | ||
) | ||
el_inner | ||
| _ -> () | ||
) | ||
el | ||
| _ -> () | ||
) | ||
exp_list | ||
| _ -> () | ||
) | ||
| "execute" | "pseudo_execute" -> | ||
debug_print ("FCL_funcl execute " ^ string_of_id i); | ||
Hashtbl.add executes (string_of_id i) source_code | ||
| _ -> () | ||
) | ||
| _ -> () | ||
end | ||
| _ -> debug_print "FCL_funcl other" | ||
|
||
let map_arg_to_mnemonic arg id = | ||
List.find_map | ||
(fun (enum, mnemonic) -> | ||
if List.hd enum = arg then ( | ||
debug_print ("Matched " ^ List.hd enum ^ " with mnemonic: " ^ List.hd mnemonic); | ||
Some (List.hd mnemonic) | ||
) | ||
else None | ||
) | ||
(Hashtbl.find_all mappings (String.lowercase_ascii (id ^ "_mnemonic"))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is relying on a convention used to identify mappings for mnemonics in the Sail code. Since this is not guaranteed, please add a comment indicating assumptions you are making here. Also, I see similar conventions which likely violate the assumptions, like:
Would it work to use all entries in the "mappings" table? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Decided to go the all entries route for this |
||
|
||
let get_index elem lst = | ||
List.find_map (fun (i, x) -> if x = elem then Some i else None) (List.mapi (fun i x -> (i, x)) lst) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use |
||
|
||
let map_param_to_arg id param args_list = | ||
match Hashtbl.find_opt inputs id with | ||
| Some inputl -> ( | ||
match get_index param inputl with Some index -> List.nth_opt args_list index | None -> None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that's |
||
) | ||
| None -> None | ||
|
||
let get_mnemonic id args_list = | ||
match Hashtbl.find_opt assembly id with | ||
| Some (str :: _) -> | ||
if Str.string_match (Str.regexp ".+(\\(.*\\))") str 0 then ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be nice to describe what this regex is looking for, or make it a variable with a meaningful name. |
||
let param = Str.matched_group 1 str in | ||
debug_print ("param: " ^ param); | ||
match map_param_to_arg id param args_list with Some arg -> map_arg_to_mnemonic arg id | None -> None | ||
) | ||
else ( | ||
match Hashtbl.find_opt assembly_clean id with | ||
| Some (mnemonic :: _) when mnemonic = str -> | ||
debug_print ("Mnemonic matched: " ^ str); | ||
Some str | ||
| Some _ -> None | ||
| None -> None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These could be combined in a catch-all using "_". |
||
) | ||
| Some [] -> None | ||
| None -> None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These could be combined in a catch-all using "_". |
||
|
||
let process_base_instruction () = | ||
let mnemonics = | ||
Hashtbl.fold | ||
(fun k (id, args_list) acc -> | ||
match get_mnemonic id args_list with Some mnemonic -> mnemonic :: acc | None -> acc | ||
) | ||
base_instructions [] | ||
in | ||
mnemonics | ||
|
||
let json_of_key_operand key op t = "\n{\n" ^ " \"name\": \"" ^ op ^ "\", \"type\": \"" ^ t ^ "\"\n" ^ "}" | ||
|
||
let json_of_mnemonic m = "\"" ^ m ^ "\"" | ||
|
@@ -791,6 +883,8 @@ let defs { defs; _ } = | |
) | ||
defs; | ||
|
||
let mnemonics = process_base_instruction () in | ||
|
||
debug_print "TYPES"; | ||
Hashtbl.iter (fun k v -> debug_print (k ^ ":" ^ v)) types; | ||
debug_print "SIGS"; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this line down to just above
Hashtbl.add executes
below, since it's not used in thepseudo_of
processing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ThinkOpenly, I intended for the line you're suggesting to move to be part of
pseudo_of
processing. My thought process is thatpat
matches similar patterns with differentid
values:pseudo_of
,execute
, andpseudo_execute
. That's why I nested theid
matches within thepat
match. Do you think this approach is flawed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to make sure we're talking about the same line, I was referring to:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aah, okay... I had the wrong line. We're on the same page now : )