From 6f435a74d7f954484a8a18d9712ba4dc22fcd2ae Mon Sep 17 00:00:00 2001
From: Josh Berdine <jjb@fb.com>
Date: Fri, 4 Sep 2020 13:37:59 -0700
Subject: [PATCH] [sledge] Add sledge llvm disassemble subcommand

Summary: To translate LLVM to textual LLAIR in one step.

Reviewed By: ngorogiannis

Differential Revision: D23459507

fbshipit-source-id: 4992002a9
---
 sledge/bin/sledge_cli.ml | 64 ++++++++++++++++++++++++----------------
 sledge/sledge-help.txt   | 57 +++++++++++++++++++++++++----------
 2 files changed, 80 insertions(+), 41 deletions(-)

diff --git a/sledge/bin/sledge_cli.ml b/sledge/bin/sledge_cli.ml
index 3e5228c04..c68a71aec 100644
--- a/sledge/bin/sledge_cli.ml
+++ b/sledge/bin/sledge_cli.ml
@@ -146,6 +146,33 @@ let analyze_cmd =
   in
   command ~summary ~readme param
 
+let disassemble =
+  let%map_open llair_txt_output =
+    flag "llair-txt-output" (optional string)
+      ~doc:
+        "<file> write generated textual LLAIR to <file>, or to standard \
+         output if omitted"
+  in
+  fun program () ->
+    let pgm = program () in
+    match llair_txt_output with
+    | None -> Format.printf "%a@." Llair.Program.pp pgm
+    | Some file ->
+        Out_channel.with_file file ~f:(fun oc ->
+            let fs = Format.formatter_of_out_channel oc in
+            Format.fprintf fs "%a@." Llair.Program.pp pgm )
+
+let disassemble_cmd =
+  let summary = "print LLAIR code in textual form" in
+  let readme () =
+    "The <input> file must be LLAIR code, as produced by `sledge llvm \
+     translate`."
+  in
+  let param =
+    Command.Param.(anon ("<input>" %: string) >>| unmarshal |*> disassemble)
+  in
+  command ~summary ~readme param
+
 let translate =
   let%map_open llair_output =
     flag "llair-output" (optional string)
@@ -195,6 +222,14 @@ let llvm_grp =
     let param = translate_inputs in
     command ~summary ~readme param
   in
+  let disassemble_cmd =
+    let summary =
+      "translate LLVM bitcode to LLAIR and print in textual form"
+    in
+    let readme () = "The <input> file must be LLVM bitcode." in
+    let param = translate_inputs |*> disassemble in
+    command ~summary ~readme param
+  in
   let analyze_cmd =
     let summary = "analyze LLVM bitcode" in
     let readme () =
@@ -210,32 +245,9 @@ let llvm_grp =
     "Code can be provided by one or more LLVM bitcode files."
   in
   Command.group ~summary ~readme ~preserve_subcommand_order:()
-    [("analyze", analyze_cmd); ("translate", translate_cmd)]
-
-let disassemble_cmd =
-  let summary = "print LLAIR code in textual form" in
-  let readme () =
-    "The <input> file must be LLAIR code, as produced by `sledge llvm \
-     translate`."
-  in
-  let param =
-    let%map_open input = anon ("<input>" %: string)
-    and llair_txt_output =
-      flag "llair-txt-output" (optional string)
-        ~doc:
-          "<file> write generated textual LLAIR to <file>, or to standard \
-           output if omitted"
-    in
-    fun () ->
-      let program = unmarshal input () in
-      match llair_txt_output with
-      | None -> Format.printf "%a@." Llair.Program.pp program
-      | Some file ->
-          Out_channel.with_file file ~f:(fun oc ->
-              let fs = Format.formatter_of_out_channel oc in
-              Format.fprintf fs "%a@." Llair.Program.pp program )
-  in
-  command ~summary ~readme param
+    [ ("analyze", analyze_cmd)
+    ; ("translate", translate_cmd)
+    ; ("disassemble", disassemble_cmd) ]
 
 let smt_cmd =
   let summary = "process SMT-LIB benchmarks" in
diff --git a/sledge/sledge-help.txt b/sledge/sledge-help.txt
index 7f1cc7511..55186c09f 100644
--- a/sledge/sledge-help.txt
+++ b/sledge/sledge-help.txt
@@ -6,18 +6,19 @@ The [-trace <spec>] argument of each subcommand enables debug tracing according
 
 === subcommands ===
 
-  buck         integration with Buck
-  . analyze    analyze buck target
-  . bitcode    report bitcode files in buck target
-  . link       link buck target to LLVM bitcode
-  llvm         integration with LLVM
-  . analyze    analyze LLVM bitcode
-  . translate  translate LLVM bitcode to LLAIR
-  analyze      analyze LLAIR code
-  disassemble  print LLAIR code in textual form
-  smt          process SMT-LIB benchmarks
-  version      print version information
-  help         explain a given subcommand (perhaps recursively)
+  buck           integration with Buck
+  . analyze      analyze buck target
+  . bitcode      report bitcode files in buck target
+  . link         link buck target to LLVM bitcode
+  llvm           integration with LLVM
+  . analyze      analyze LLVM bitcode
+  . translate    translate LLVM bitcode to LLAIR
+  . disassemble  translate LLVM bitcode to LLAIR and print in textual form
+  analyze        analyze LLAIR code
+  disassemble    print LLAIR code in textual form
+  smt            process SMT-LIB benchmarks
+  version        print version information
+  help           explain a given subcommand (perhaps recursively)
 
 
 ====== sledge buck ======
@@ -120,9 +121,10 @@ Code can be provided by one or more LLVM bitcode files.
 
 === subcommands ===
 
-  analyze    analyze LLVM bitcode
-  translate  translate LLVM bitcode to LLAIR
-  help       explain a given subcommand (perhaps recursively)
+  analyze      analyze LLVM bitcode
+  translate    translate LLVM bitcode to LLAIR
+  disassemble  translate LLVM bitcode to LLAIR and print in textual form
+  help         explain a given subcommand (perhaps recursively)
 
 
 ====== sledge llvm analyze ======
@@ -180,6 +182,31 @@ Translate one or more LLVM bitcode files to LLAIR. Each <input> filename may be
                           (alias: -?)
 
 
+====== sledge llvm disassemble ======
+
+translate LLVM bitcode to LLAIR and print in textual form
+
+  sledge llvm disassemble <input> [<input> ...]
+
+The <input> file must be LLVM bitcode.
+
+=== flags ===
+
+  [-colors]                   enable printing in colors
+  [-fuzzer]                   add a harness for libFuzzer targets
+  [-llair-output <file>]      write generated LLAIR to <file>
+  [-llair-txt-output <file>]  write generated textual LLAIR to <file>, or to
+                              standard output if omitted
+  [-margin <cols>]            wrap debug tracing at <cols> columns
+  [-no-internalize]           do not internalize all functions except the entry
+                              points specified in the config file
+  [-no-models]                do not add models for C/C++ runtime and standard
+                              libraries
+  [-trace <spec>]             enable debug tracing
+  [-help]                     print this help text and exit
+                              (alias: -?)
+
+
 ====== sledge analyze ======
 
 analyze LLAIR code