Skip to content
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

Merged
merged 8 commits into from
Aug 7, 2024
75 changes: 64 additions & 11 deletions src/sail_json_backend/json.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Owner

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:

let rec filter_non_operands components = match components with
  | [] -> []
  | hd :: tl when String.trim hd = "spc" -> tl
  | _ -> filter_non_operands (List.tl components)

What do you think? It's slightly smaller, doesn't require an embedded aux function, and doesn't use List.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.

Copy link
Author

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


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
Copy link
Owner

Choose a reason for hiding this comment

The 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"?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You removed trimmed above, so I'm not sure how you can use it here? :-)

Copy link
Author

Choose a reason for hiding this comment

The 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, _) ->
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
)
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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";
Expand Down