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
67 changes: 56 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 @@ -230,21 +231,66 @@ let parse_encdec i mc format =
end
end
| _ -> assert false

let extract_operands k =
let components = Hashtbl.find assembly k in
let regex = Str.regexp ".+(\\(.*\\))" in
let rec pre_filter acc = function
| [] -> List.rev acc
| hd :: tl ->
if String.trim hd = "spc" then
tl
else
pre_filter ( hd :: acc ) tl
ThinkOpenly marked this conversation as resolved.
Show resolved Hide resolved
in
let filtered_components = pre_filter [] components in
let rec extract 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
let operand = Str.matched_group 1 hd in
let trimmed_operand =
try
let comma_index = String.index operand ',' in
let trimmed = String.sub operand 0 comma_index in
debug_print ("Trimmed operand using comma: " ^ trimmed);
trimmed
with Not_found ->
debug_print ("No comma found in operand: " ^ operand);
operand
ThinkOpenly marked this conversation as resolved.
Show resolved Hide resolved
in
debug_print ("Final trimmed operand: " ^ trimmed_operand);
extract (trimmed_operand :: acc) tl
else
ThinkOpenly marked this conversation as resolved.
Show resolved Hide resolved
extract acc tl
ThinkOpenly marked this conversation as resolved.
Show resolved Hide resolved
in
let operandl = extract [] 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_operands (string_of_id app_id)
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 +439,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 +578,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 +597,9 @@ 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 +637,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 +788,7 @@ 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