Skip to content

Commit

Permalink
add output to owi opt cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
vasucp1207 committed Aug 26, 2024
1 parent d725698 commit 24de7fd
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 28 deletions.
2 changes: 1 addition & 1 deletion example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ COMMANDS
fmt [--inplace] [OPTION]… [ARG]…
Format a .wat or .wast file

opt [--debug] [--unsafe] [OPTION]… [ARG]…
opt [--debug] [--output=FILE] [--unsafe] [OPTION]… ARG
Optimize a module

run [OPTION]… [ARG]…
Expand Down
5 changes: 4 additions & 1 deletion example/opt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,15 @@ NAME
owi-opt - Optimize a module

SYNOPSIS
owi opt [--debug] [--unsafe] [OPTION]… [ARG]…
owi opt [--debug] [--output=FILE] [--unsafe] [OPTION]… ARG

OPTIONS
-d, --debug
debug mode

-o FILE, --output=FILE
wat

-u, --unsafe
skip typechecking pass

Expand Down
15 changes: 8 additions & 7 deletions src/ast/binary_encoder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -762,14 +762,15 @@ let encode
let write_file outfile filename content =
let _dir, filename = Fpath.split_base filename in
let filename = Fpath.set_ext "wasm" filename in
let filename = match outfile with Some name -> Fpath.v name | None -> filename in
let filename = Fpath.to_string filename in
let oc = Out_channel.open_bin filename in
Out_channel.output_string oc content;
Out_channel.close oc
match outfile with
| Some name -> Bos.OS.File.write name content
| None -> Bos.OS.File.write filename content

let convert (outfile: string option) (filename : Fpath.t) ~unsafe ~optimize m =
let convert (outfile : Fpath.t option) (filename : Fpath.t) ~unsafe ~optimize m
=
Log.debug0 "bin encoding ...@\n";
let+ m = Compile.Text.until_optimize ~unsafe ~optimize m in
let content = encode m in
write_file outfile filename content
match write_file outfile filename content with
| Error _ -> raise Exit
| Ok _ -> ()
2 changes: 1 addition & 1 deletion src/ast/binary_encoder.mli
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(* Written by the Owi programmers *)

val convert :
string option
Fpath.t option
-> Fpath.t
-> unsafe:bool
-> optimize:bool
Expand Down
13 changes: 10 additions & 3 deletions src/bin/owi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ let sourcefile ty =

let outfile ty =
let doc = Fmt.str "%s" ty in
let string_to_path =
Cmdliner.Arg.conv ~docv:"FILE" (Fpath.of_string, Fpath.pp)
in
Cmdliner.Arg.(
value & opt (some string) None & info [ "o"; "output" ] ~docv:"FILE" ~doc )
value
& opt (some string_to_path) None
& info [ "o"; "output" ] ~docv:"FILE" ~doc )

let emit_files =
let doc = "emit (.wat) files from corresponding (.wasm) files" in
Expand Down Expand Up @@ -191,7 +196,8 @@ let opt_cmd =
let man = [] @ shared_man in
Cmd.info "opt" ~version ~doc ~sdocs ~man
in
Cmd.v info Term.(const Cmd_opt.cmd $ debug $ unsafe $ files)
Cmd.v info
Term.(const Cmd_opt.cmd $ debug $ unsafe $ sourcefile "wat" $ outfile "wat")

let run_cmd =
let open Cmdliner in
Expand Down Expand Up @@ -260,7 +266,8 @@ let wasm2wat_cmd =
Cmd.info "wasm2wat" ~version ~doc ~sdocs ~man
in
Cmd.v info
Term.(const Cmd_wasm2wat.cmd $ sourcefile "wasm" $ emit_files $ outfile "wasm")
Term.(
const Cmd_wasm2wat.cmd $ sourcefile "wasm" $ emit_files $ outfile "wasm" )

let wat2wasm_cmd =
let open Cmdliner in
Expand Down
18 changes: 11 additions & 7 deletions src/cmd/cmd_opt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ open Syntax
let optimize_file ~unsafe filename =
Compile.File.until_optimize ~unsafe ~optimize:true filename

let cmd debug unsafe files =
let print_or_emit ~unsafe file outfile =
let+ m = optimize_file ~unsafe file in
let m = Binary_to_text.modul m in
match outfile with
| Some name -> Bos.OS.File.writef name "%a@\n" Text.pp_modul m
| None -> Ok (Fmt.pr "%a@\n" Text.pp_modul m)

let cmd debug unsafe file outfile =
if debug then Log.debug_on := true;
list_iter
(fun file ->
let+ m = optimize_file ~unsafe file in
let m = Binary_to_text.modul m in
Fmt.pr "%a@\n" Text.pp_modul m )
files
match print_or_emit ~unsafe file outfile with
| Error _ -> raise Exit
| Ok _ -> Ok ()
2 changes: 1 addition & 1 deletion src/cmd/cmd_opt.mli
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
(* Copyright © 2021-2024 OCamlPro *)
(* Written by the Owi programmers *)

val cmd : bool -> bool -> Fpath.t list -> unit Result.t
val cmd : bool -> bool -> Fpath.t -> Fpath.t option -> unit Result.t
7 changes: 4 additions & 3 deletions src/cmd/cmd_wasm2wat.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ let cmd_one emitfile outfile file =
let outname, output =
begin
match outfile with
| Some name -> Fpath.v name, true
| None -> wat_file, false
| Some name -> (name, true)
| None -> (wat_file, false)
end
in
if emitfile || output then Bos.OS.File.writef outname "%a@\n" Text.pp_modul m
if emitfile || output then
Bos.OS.File.writef outname "%a@\n" Text.pp_modul m
else Ok (Fmt.pr "%a@\n" Text.pp_modul m)
| ext -> Error (`Unsupported_file_extension ext)

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/cmd_wasm2wat.mli
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
(* Copyright © 2021-2024 OCamlPro *)
(* Written by the Owi programmers *)

val cmd : Fpath.t -> bool -> string option -> unit Result.t
val cmd : Fpath.t -> bool -> Fpath.t option -> unit Result.t
2 changes: 1 addition & 1 deletion src/cmd/cmd_wat2wasm.mli
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
(* Copyright © 2021-2024 OCamlPro *)
(* Written by the Owi programmers *)

val cmd : bool -> bool -> bool -> bool -> string option -> Fpath.t -> unit Result.t
val cmd : bool -> bool -> bool -> bool -> Fpath.t option -> Fpath.t -> unit Result.t
2 changes: 1 addition & 1 deletion test/help/help.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ no subcommand should print help
fmt [--inplace] [OPTION]… [ARG]…
Format a .wat or .wast file

opt [--debug] [--unsafe] [OPTION]… [ARG]…
opt [--debug] [--output=FILE] [--unsafe] [OPTION]… ARG
Optimize a module

run [OPTION]… [ARG]…
Expand Down
2 changes: 1 addition & 1 deletion test/opt/not_exists.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
file doesn't exist:
$ owi opt not_exists.wat --debug
owi: no file 'not_exists.wat'
Usage: owi opt [--debug] [--unsafe] [OPTION]… [ARG]…
Usage: owi opt [--debug] [--output=FILE] [--unsafe] [OPTION]… ARG
Try 'owi opt --help' or 'owi --help' for more information.
[124]

0 comments on commit 24de7fd

Please sign in to comment.