Skip to content

Commit

Permalink
add wasm2wat subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
zapashcanon committed Apr 24, 2024
1 parent 1052d00 commit e55b60f
Show file tree
Hide file tree
Showing 22 changed files with 316 additions and 3 deletions.
3 changes: 1 addition & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
## unreleased

- use a subcommand system for the `owi` binary
- add `owi c`, `owi fmt`, `owi opt`, `owi sym` and `owi validate` subcommands
- add `owi c`, `owi fmt`, `owi opt`, `owi sym`, `owi validate` and `owi wasm2wat` subcommands
- add a fuzzer
- add a profiling mode
- make `spectec` print stuff for real
- add a binary parser

## 0.1 - 2023-01-14

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [`owi script`]: an interpreter for [Wasm scripts];
- [`owi sym`]: a symbolic Wasm interpreter;
- [`owi validate`]: a validator for Wasm modules.
- [`owi wasm2wat`]: a Wasm binary to text format translater.

It also provides an [OCaml library] which allows for instance to [import OCaml functions in a Wasm module] in a type-safe way!

Expand Down Expand Up @@ -181,6 +182,7 @@ This project was funded through the [NGI0 Core] Fund, a fund established by [NLn
[`owi script`]: example/script
[`owi sym`]: example/sym
[`owi validate`]: example/validate
[`owi wasm2wat`]: example/wasm2wat
[import OCaml functions in a Wasm module]: example/define_host_function
[OCaml library]: example/lib
[a fuzzer]: test/fuzz
Expand Down
6 changes: 6 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [`owi script`]
- [`owi sym`]
- [`owi validate`]
- [`owi wasm2wat`]

## Owi library

Expand Down Expand Up @@ -47,6 +48,10 @@ COMMANDS
validate [--debug] [OPTION]… [ARG]…
Validate a module

wasm2wat [OPTION]… [ARG]…
Generate a text format file (.wat) file from a binary format file
(.wasm)

COMMON OPTIONS
--help[=FMT] (default=auto)
Show this help in format FMT. The value FMT must be one of auto,
Expand Down Expand Up @@ -79,5 +84,6 @@ BUGS
[`owi script`]: ./script
[`owi sym`]: ./sym
[`owi validate`]: ./validate
[`owi wasm2wat`]: ./wasm2wat
[import OCaml functions in a Wasm module]: ./define_host_function
[OCaml library]: ./lib
Binary file added example/run/42.wasm
Binary file not shown.
Binary file added example/wasm2wat/42.wasm
Binary file not shown.
57 changes: 57 additions & 0 deletions example/wasm2wat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Wasm2wat

## Basic example

Given a file `42.wasm`:

```sh
$ owi wasm2wat ./42.wasm
(module
(func
i32.const 20
i32.const 22
i32.add
drop
)
(start 0)
)
```

## Man page

```sh
$ owi wasm2wat --help=plain
NAME
owi-wasm2wat - Generate a text format file (.wat) file from a binary
format file (.wasm)

SYNOPSIS
owi wasm2wat [OPTION]… [ARG]…

COMMON OPTIONS
--help[=FMT] (default=auto)
Show this help in format FMT. The value FMT must be one of auto,
pager, groff or plain. With auto, the format is pager or plain
whenever the TERM env var is dumb or undefined.

--version
Show version information.

EXIT STATUS
owi wasm2wat exits with:

0 on success.

123 on indiscriminate errors reported on standard error.

124 on command line parsing errors.

125 on unexpected internal errors (bugs).

BUGS
Email them to <[email protected]>.

SEE ALSO
owi(1)

```
4 changes: 4 additions & 0 deletions example/wasm2wat/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(mdx
(libraries owi)
(deps %{bin:owi} 42.wasm)
(files README.md))
22 changes: 21 additions & 1 deletion src/bin/owi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,18 @@ let sym_cmd =
$ no_stop_at_failure $ no_values $ deterministic_result_order $ workspace
$ files )

let wasm2wat_cmd =
let open Cmdliner in
let info =
let doc =
"Generate a text format file (.wat) file from a binary format file \
(.wasm)"
in
let man = [] @ shared_man in
Cmd.info "wasm2wat" ~version ~doc ~sdocs ~man
in
Cmd.v info Term.(const Cmd_wasm2wat.cmd $ files)

let cli =
let open Cmdliner in
let info =
Expand All @@ -199,7 +211,15 @@ let cli =
Term.(ret (const (fun (_ : _ list) -> `Help (`Plain, None)) $ copts_t))
in
Cmd.group info ~default
[ c_cmd; fmt_cmd; opt_cmd; run_cmd; script_cmd; sym_cmd; validate_cmd ]
[ c_cmd
; fmt_cmd
; opt_cmd
; run_cmd
; script_cmd
; sym_cmd
; validate_cmd
; wasm2wat_cmd
]

let exit_code =
let open Cmdliner.Cmd.Exit in
Expand Down
15 changes: 15 additions & 0 deletions src/cmd/cmd_wasm2wat.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(* SPDX-License-Identifier: AGPL-3.0-or-later *)
(* Copyright © 2021-2024 OCamlPro *)
(* Written by the Owi programmers *)

open Syntax

let cmd_one file =
let ext = Fpath.get_ext file in
match ext with
| ".wasm" ->
let* m = Binary_deserializer.from_file file in
Ok (Format.pp_std "%a@\n" Simplified.Pp.modul m)
| ext -> Error (`Msg (Format.sprintf "invalid extension: `%s`" ext))

let cmd files = list_iter cmd_one files
5 changes: 5 additions & 0 deletions src/cmd/cmd_wasm2wat.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(* SPDX-License-Identifier: AGPL-3.0-or-later *)
(* Copyright © 2021-2024 OCamlPro *)
(* Written by the Owi programmers *)

val cmd : Fpath.t list -> unit Result.t
1 change: 1 addition & 0 deletions src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
cmd_script
cmd_sym
cmd_validate
cmd_wasm2wat
compile
concrete
concrete_choice
Expand Down
4 changes: 4 additions & 0 deletions test/help/help.t
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ no subcommand should print help
validate [--debug] [OPTION]… [ARG]…
Validate a module

wasm2wat [OPTION]… [ARG]…
Generate a text format file (.wat) file from a binary format file
(.wasm)

COMMON OPTIONS
--help[=FMT] (default=auto)
Show this help in format FMT. The value FMT must be one of auto,
Expand Down
15 changes: 15 additions & 0 deletions test/wasm2wat/done.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
$ owi wasm2wat done.wasm
(module
(func
i32.const 22
i32.const 20
call 0
drop
)
(func (param i32) (param i32) (result i32)
local.get 0
local.get 1
i32.add
)
(start 1)
)
Binary file added test/wasm2wat/done.wasm
Binary file not shown.
2 changes: 2 additions & 0 deletions test/wasm2wat/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(cram
(deps %{bin:owi} done.wasm m.wasm locals.wasm locals_drop.wasm))
86 changes: 86 additions & 0 deletions test/wasm2wat/locals.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
$ owi wasm2wat locals.wasm
(module
(func
i32.const 0
i32.const 1
i32.const 2
call 0
i32.const 0
i32.const 1
i32.const 2
call 1
i32.const 0
i32.const 1
i32.const 2
call 2
i32.const 0
i32.const 1
i32.const 2
call 3
i32.const 0
i32.const 1
i32.const 2
call 4
i32.const 0
i32.const 1
i32.const 2
call 5
i32.const 0
i32.const 1
i32.const 2
call 6
i32.const 0
i32.const 1
i32.const 2
call 7
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 0
local.get 1
i32.add
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 4
local.get 0
i32.add
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 2
i32.const 0
i32.add
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 1
i32.const 0
i32.add
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 0
i32.const 0
i32.add
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 4
i32.const 0
i32.add
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 3
i32.const 0
i32.add
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 3
local.get 4
i32.add
drop
)
(start 8)
)
Binary file added test/wasm2wat/locals.wasm
Binary file not shown.
76 changes: 76 additions & 0 deletions test/wasm2wat/locals_drop.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
$ owi wasm2wat locals_drop.wasm
(module
(func
i32.const 0
i32.const 1
i32.const 2
call 0
i32.const 0
i32.const 1
i32.const 2
call 1
i32.const 0
i32.const 1
i32.const 2
call 2
i32.const 0
i32.const 1
i32.const 2
call 3
i32.const 0
i32.const 1
i32.const 2
call 4
i32.const 0
i32.const 1
i32.const 2
call 5
i32.const 0
i32.const 1
i32.const 2
call 6
i32.const 0
i32.const 1
i32.const 2
call 7
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 0
drop
local.get 1
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 4
drop
local.get 0
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 2
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 1
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 0
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 4
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 3
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 3
drop
local.get 4
drop
)
(start 8)
)
Binary file added test/wasm2wat/locals_drop.wasm
Binary file not shown.
Binary file added test/wasm2wat/m.wasm
Binary file not shown.
5 changes: 5 additions & 0 deletions test/wasm2wat/not_exists.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$ owi wasm2wat idontexist.wat
owi: no file 'idontexist.wat'
Usage: owi wasm2wat [OPTION]… [ARG]…
Try 'owi wasm2wat --help' or 'owi --help' for more information.
[124]
Loading

0 comments on commit e55b60f

Please sign in to comment.