-
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
Fix order of operands. #34
Changes from 4 commits
597d481
8a4b5fb
b377126
69ad767
300079a
5908e18
ed19dba
aab5e63
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 |
---|---|---|
|
@@ -80,6 +80,7 @@ let sigs = Hashtbl.create 997 | |
let names = Hashtbl.create 997 | ||
let descriptions = Hashtbl.create 997 | ||
let operands = Hashtbl.create 997 | ||
let inputs = Hashtbl.create 997 | ||
let encodings = Hashtbl.create 997 | ||
let assembly = Hashtbl.create 997 | ||
let assembly_clean = Hashtbl.create 997 | ||
|
@@ -231,20 +232,69 @@ let parse_encdec i mc format = | |
end | ||
| _ -> assert false | ||
|
||
let filter_non_operands components = | ||
let rec aux acc = function | ||
| [] -> List.rev acc | ||
| hd :: tl -> if String.trim hd = "spc" then tl else aux (hd :: acc) tl | ||
in | ||
aux [] components | ||
|
||
let extract_operands k regex components = | ||
let rec aux acc = function | ||
| [] -> List.rev acc | ||
ThinkOpenly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| hd :: tl -> | ||
if Str.string_match regex hd 0 then ( | ||
ThinkOpenly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let operand = Str.matched_group 1 hd in | ||
try | ||
let comma_index = String.index operand ',' in | ||
debug_print ("Operand before trimming: " ^ operand); | ||
let trimmed = String.sub operand 0 comma_index in | ||
debug_print ("Final trimmed operand: " ^ trimmed); | ||
let inputl = Hashtbl.find inputs k in | ||
if List.mem trimmed inputl then aux (trimmed :: acc) tl else aux acc tl | ||
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 make this a more generic loop that looks at each element in the list of parameters of this function? For example, if it encounters a string "func(a,b,c)", it checks to see if "a", "b", and "c" are in "inputs"? 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. You removed 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. Fixed : ) |
||
with Not_found -> aux (operand :: acc) tl | ||
) | ||
else aux acc tl | ||
in | ||
aux [] components | ||
|
||
let extract_and_map_operands k = | ||
let components = Hashtbl.find assembly k in | ||
ThinkOpenly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let regex = Str.regexp ".+(\\(.*\\))" in | ||
ThinkOpenly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let filtered_components = filter_non_operands components in | ||
let operandl = extract_operands k regex filtered_components in | ||
let opmap = List.combine (Hashtbl.find inputs k) (Hashtbl.find sigs k) in | ||
let operand_with_type = | ||
List.map | ||
(fun op -> | ||
match List.find_opt (fun (name, _) -> String.equal name op) opmap with | ||
| Some (_, t) -> (op, t) | ||
| None -> (op, "") | ||
) | ||
operandl | ||
in | ||
debug_print | ||
("Adding to operands hashtable: " ^ k ^ " -> " | ||
^ String.concat ", " (List.map (fun (op, t) -> Printf.sprintf "(%s, %s)" op t) operand_with_type) | ||
); | ||
Hashtbl.add operands k operand_with_type | ||
|
||
let add_assembly app_id p = | ||
let x = string_list_of_mpat p in | ||
begin | ||
debug_print ("assembly.add " ^ string_of_id app_id ^ " : " ^ List.hd x); | ||
Hashtbl.add assembly (string_of_id app_id) x | ||
Hashtbl.add assembly (string_of_id app_id) x; | ||
extract_and_map_operands (string_of_id app_id) | ||
ThinkOpenly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
let parse_assembly_mpat mp pb = | ||
match mp with | ||
| MP_aux (MP_app (app_id, mpl), _) -> | ||
debug_print ("MP_app " ^ string_of_id app_id); | ||
let operandl = List.concat (List.map string_list_of_mpat mpl) in | ||
let inputl = List.concat (List.map string_list_of_mpat mpl) in | ||
Hashtbl.add inputs (string_of_id app_id) inputl; | ||
begin | ||
List.iter debug_print operandl; | ||
List.iter debug_print inputl; | ||
debug_print "MCL_bidir (right part)"; | ||
match pb with | ||
| MPat_aux (MPat_pat p, _) -> | ||
|
@@ -393,9 +443,8 @@ let parse_funcl fcl = | |
Hashtbl.add functions id source_code | ||
| 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 operandl = List.concat (List.map string_list_of_pat pl) in | ||
if not (String.equal (List.hd operandl) "()") then Hashtbl.add operands (string_of_id i) operandl; | ||
Hashtbl.add executes (string_of_id i) (extract_source_code (Ast_util.exp_loc e)) | ||
let source_code = extract_source_code (Ast_util.exp_loc e) in | ||
Hashtbl.add executes (string_of_id i) source_code | ||
| _ -> () | ||
end | ||
| _ -> debug_print "FCL_funcl other" | ||
|
@@ -533,7 +582,7 @@ let default_operand optional opt_operand = | |
|
||
let json_of_operand k op = | ||
debug_print ("json_of_operand " ^ k ^ ":" ^ op); | ||
let opmap = List.combine (Hashtbl.find operands k) (Hashtbl.find sigs k) in | ||
let opmap = Hashtbl.find operands k in | ||
let opplus = remove_identity_funcs op in | ||
let opname = List.hd (String.split_on_char ',' opplus) in | ||
let optional, opt_operand = optional_operand k op in | ||
|
@@ -552,9 +601,11 @@ let json_of_operands k = | |
| Some ops -> | ||
String.concat "," | ||
(List.map | ||
(fun op -> json_of_operand k op) | ||
(fun (op, _) -> json_of_operand k op) | ||
(List.filter | ||
(fun s -> not (String.equal s "(" || String.equal s ")" || String.equal s "spc" || String.equal s "sep")) | ||
(fun (s, _) -> | ||
not (String.equal s "(" || String.equal s ")" || String.equal s "spc" || String.equal s "sep") | ||
) | ||
ops | ||
) | ||
) | ||
|
@@ -592,7 +643,7 @@ let rec string_of_sizeof_field k f = | |
) | ||
else begin | ||
(* match operand names to function signature types *) | ||
let opmap = List.combine (Hashtbl.find operands k) (Hashtbl.find sigs k) in | ||
let opmap = Hashtbl.find operands k in | ||
begin | ||
(* find matching operand type *) | ||
match List.assoc_opt f opmap with | ||
|
@@ -743,7 +794,9 @@ let defs { defs; _ } = | |
debug_print "DESCRIPTIONS"; | ||
Hashtbl.iter (fun k v -> debug_print (k ^ ":" ^ v)) descriptions; | ||
debug_print "OPERANDS"; | ||
Hashtbl.iter (fun k v -> debug_print (k ^ ":" ^ Util.string_of_list ", " (fun x -> x) v)) operands; | ||
Hashtbl.iter | ||
(fun k v -> debug_print (k ^ ":" ^ Util.string_of_list ", " (fun (op, t) -> "(" ^ op ^ ", " ^ t ^ ")") v)) | ||
operands; | ||
debug_print "ENCODINGS"; | ||
Hashtbl.iter (fun k v -> debug_print (k ^ ":" ^ Util.string_of_list ", " (fun x -> x) v)) encodings; | ||
debug_print "ASSEMBLY"; | ||
|
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.
I took a stab at an alternative implementation that doesn't use
List.rev
:What do you think? It's slightly smaller, doesn't require an embedded
aux
function, and doesn't useList.rev
, so it could be slightly faster (although we're not terribly concerned with performance at this point).I think it could be adapted to be used in
extract_operands
, below, as well.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.
It's much more readable. I've adapted it for
extact_operands