Skip to content

Commit

Permalink
small refactor and some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vasucp1207 authored and zapashcanon committed Aug 27, 2024
1 parent 0b300ec commit aef0bd6
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## unreleased

- `wat2wasm` emit wasm files in same dir as wat files
- add `--output` option to `wat2wasm`, `wasm2wat` and `opt` cmd
- add `--emit-file` option to `wasm2wat` to emit .wat files
- adds flags `--fail-on-trap-only` and `fail-on-assertion-only`
- parameterize the `Thread` module on the symbolic memory and the `Choice_monad` module on a Thread
Expand Down
2 changes: 1 addition & 1 deletion example/opt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ OPTIONS
debug mode

-o FILE, --output=FILE
wat
Write output to a file.

-u, --unsafe
skip typechecking pass
Expand Down
4 changes: 2 additions & 2 deletions example/wasm2wat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ SYNOPSIS

OPTIONS
--emit-file
emit (.wat) files from corresponding (.wasm) files
Emit (.wat) files from corresponding (.wasm) files.

-o FILE, --output=FILE
wasm
Write output to a file.

COMMON OPTIONS
--help[=FMT] (default=auto)
Expand Down
2 changes: 1 addition & 1 deletion example/wat2wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ OPTIONS
debug mode

-o FILE, --output=FILE
wat
Write output to a file.

--optimize
optimize mode
Expand Down
23 changes: 10 additions & 13 deletions src/bin/owi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ let files =
let f = existing_non_dir_file in
Cmdliner.Arg.(value & pos_all f [] (info [] ~doc))

let sourcefile ty =
let doc = Fmt.str "%s source file" ty in
let sourcefile =
let doc = "source file" in
let f = existing_non_dir_file in
Cmdliner.Arg.(required & pos 0 (some f) None (info [] ~doc))

let outfile ty =
let doc = Fmt.str "%s" ty in
let outfile =
let doc = "Write output to a file." in
let string_to_path =
Cmdliner.Arg.conv ~docv:"FILE" (Fpath.of_string, Fpath.pp)
in
Expand All @@ -54,8 +54,8 @@ let outfile ty =
& 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
let emit_file =
let doc = "Emit (.wat) files from corresponding (.wasm) files." in
Cmdliner.Arg.(value & flag & info [ "emit-file" ] ~doc)

let no_exhaustion =
Expand Down Expand Up @@ -196,8 +196,7 @@ 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 $ sourcefile "wat" $ outfile "wat")
Cmd.v info Term.(const Cmd_opt.cmd $ debug $ unsafe $ sourcefile $ outfile)

let run_cmd =
let open Cmdliner in
Expand Down Expand Up @@ -265,9 +264,7 @@ let wasm2wat_cmd =
let man = [] @ shared_man in
Cmd.info "wasm2wat" ~version ~doc ~sdocs ~man
in
Cmd.v info
Term.(
const Cmd_wasm2wat.cmd $ sourcefile "wasm" $ emit_files $ outfile "wasm" )
Cmd.v info Term.(const Cmd_wasm2wat.cmd $ sourcefile $ emit_file $ outfile)

let wat2wasm_cmd =
let open Cmdliner in
Expand All @@ -280,8 +277,8 @@ let wat2wasm_cmd =
in
Cmd.v info
Term.(
const Cmd_wat2wasm.cmd $ profiling $ debug $ unsafe $ optimize
$ outfile "wat" $ sourcefile "wat" )
const Cmd_wat2wasm.cmd $ profiling $ debug $ unsafe $ optimize $ outfile
$ sourcefile )

let cli =
let open Cmdliner in
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/cmd_opt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ let optimize_file ~unsafe filename =
Compile.File.until_optimize ~unsafe ~optimize:true filename

let print_or_emit ~unsafe file outfile =
let+ m = optimize_file ~unsafe file in
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;
Result.bind (print_or_emit ~unsafe file outfile) (fun _ -> Ok ())
print_or_emit ~unsafe file outfile
10 changes: 10 additions & 0 deletions test/opt/output.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
$ owi opt fbinop.wat -o bar.wat
$ owi fmt bar.wat
(module

(type (sub final (func)))
(func $start

)
(start 0)
)
20 changes: 20 additions & 0 deletions test/wasm2wat/output.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
$ owi wasm2wat done.wasm -o bar.wat
$ owi fmt bar.wat
(module

(type (sub final (func (param i32) (param i32) (result i32))))

(type (sub final (func)))
(func (param i32) (param i32) (result i32)
local.get 0
local.get 1
i32.add
)
(func
i32.const 22
i32.const 20
call 0
drop
)
(start 1)
)
26 changes: 26 additions & 0 deletions test/wat2wasm/output.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
$ owi wat2wasm func.wat -o bar.wasm
$ owi run bar.wasm --debug
typechecking ...
linking ...
interpreting ...
stack : [ ]
running instr: call 1
calling func : func anonymous
stack : [ ]
running instr: f32.const 40.400_001_525_878_906
stack : [ f32.const 40.400_001_525_878_906 ]
running instr: call 0
calling func : func anonymous
stack : [ ]
running instr: f32.const 2.019_999_980_926_513_7
stack : [ f32.const 2.019_999_980_926_513_7 ]
running instr: local.tee 1
stack : [ f32.const 2.019_999_980_926_513_7 ]
running instr: local.get 0
stack : [ f32.const 40.400_001_525_878_906 ; f32.const 2.019_999_980_926_513_7 ]
running instr: f32.add
stack : [ f32.const 42.420_001_983_642_578 ]
stack : [ f32.const 42.420_001_983_642_578 ]
running instr: drop
stack : [ ]
stack : [ ]

0 comments on commit aef0bd6

Please sign in to comment.