Skip to content

Commit

Permalink
fix binary to text
Browse files Browse the repository at this point in the history
  • Loading branch information
zapashcanon committed Jun 12, 2024
1 parent e0b686c commit f86db3d
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 112 deletions.
38 changes: 22 additions & 16 deletions src/ast/binary_to_text.ml
Original file line number Diff line number Diff line change
Expand Up @@ -93,70 +93,76 @@ let convert_data (e : Binary.data) : Text.data =
let from_global (global : (Binary.global, binary global_type) Runtime.t Named.t)
: Text.module_field list =
Named.fold
(fun _i (g : (Binary.global, binary global_type) Runtime.t) acc ->
(fun i (g : (Binary.global, binary global_type) Runtime.t) acc ->
match g with
| Runtime.Local g ->
let typ = convert_global_type g.typ in
let init = convert_expr g.init in
let id = g.id in
MGlobal { typ; init; id } :: acc
(i, MGlobal { typ; init; id }) :: acc
| Imported { modul; name; assigned_name; desc } ->
let desc = Import_global (assigned_name, convert_global_type desc) in
MImport { modul; name; desc } :: acc )
(i, MImport { modul; name; desc }) :: acc )
global []
|> List.sort compare |> List.map snd

let from_table (table : (binary table, binary table_type) Runtime.t Named.t) :
Text.module_field list =
Named.fold
(fun _i (t : (binary table, binary table_type) Runtime.t) acc ->
(fun i (t : (binary table, binary table_type) Runtime.t) acc ->
match t with
| Runtime.Local t ->
let t = convert_table t in
MTable t :: acc
(i, MTable t) :: acc
| Imported { modul; name; assigned_name; desc } ->
let desc = Import_table (assigned_name, convert_table_type desc) in
MImport { modul; name; desc } :: acc )
(i, MImport { modul; name; desc }) :: acc )
table []
|> List.sort compare |> List.map snd

let from_mem (mem : (mem, limits) Runtime.t Named.t) : Text.module_field list =
Named.fold
(fun _i mem acc ->
(fun i mem acc ->
match mem with
| Runtime.Local mem -> MMem mem :: acc
| Runtime.Local mem -> (i, MMem mem) :: acc
| Imported { modul; name; assigned_name; desc } ->
let desc = Import_mem (assigned_name, desc) in
MImport { modul; name; desc } :: acc )
(i, MImport { modul; name; desc }) :: acc )
mem []
|> List.sort compare |> List.map snd

let from_func (func : (binary func, binary block_type) Runtime.t Named.t) :
Text.module_field list =
Named.fold
(fun _i (func : (binary func, binary block_type) Runtime.t) acc ->
(fun i (func : (binary func, binary block_type) Runtime.t) acc ->
match func with
| Runtime.Local func ->
let type_f = convert_block_type func.type_f in
let locals = convert_param_type func.locals in
let body = convert_expr func.body in
let id = func.id in
MFunc { type_f; locals; body; id } :: acc
(i, MFunc { type_f; locals; body; id }) :: acc
| Imported { modul; name; assigned_name; desc } ->
let desc = Import_func (assigned_name, convert_block_type desc) in
MImport { modul; name; desc } :: acc )
(i, MImport { modul; name; desc }) :: acc )
func []
|> List.sort compare |> List.map snd

let from_elem (elem : Binary.elem Named.t) : Text.module_field list =
Named.fold
(fun _i (elem : Binary.elem) acc ->
(fun i (elem : Binary.elem) acc ->
let elem = convert_elem elem in
MElem elem :: acc )
(i, MElem elem) :: acc )
elem []
|> List.sort compare |> List.map snd

let from_data (data : Binary.data Named.t) : Text.module_field list =
Named.fold
(fun _i (data : Binary.data) acc ->
(fun i (data : Binary.data) acc ->
let data = convert_data data in
MData data :: acc )
(i, MData data) :: acc )
data []
|> List.sort compare |> List.map snd

let from_exports (exports : Binary.exports) : Text.module_field list =
let global =
Expand Down
10 changes: 5 additions & 5 deletions test/wasm2wat/done.t
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
$ owi wasm2wat done.wasm
(module
(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
)
(func (param i32) (param i32) (result i32)
local.get 0
local.get 1
i32.add
)
(start 1)
)
3 changes: 1 addition & 2 deletions test/wasm2wat/from_c.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
All OK
$ owi wasm2wat a.out.wasm > a.out.wat
$ owi sym a.out.wat
type mismatch (pop)
[35]
All OK
92 changes: 46 additions & 46 deletions test/wasm2wat/locals.t
Original file line number Diff line number Diff line change
@@ -1,59 +1,19 @@
$ 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 3
local.get 4
local.get 0
i32.add
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 2
local.get 3
i32.const 0
i32.add
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 1
local.get 4
i32.const 0
i32.add
drop
Expand All @@ -65,22 +25,62 @@
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 4
local.get 1
i32.const 0
i32.add
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 3
local.get 2
i32.const 0
i32.add
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 3
local.get 4
local.get 0
i32.add
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 0
local.get 1
i32.add
drop
)
(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
)
(start 8)
)
76 changes: 38 additions & 38 deletions test/wasm2wat/locals_drop.t
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
$ owi wasm2wat locals_drop.wasm
(module
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 3
drop
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 4
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 1
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 4
drop
local.get 0
drop
)
(func (param i32) (param i32) (param i32) (local i32) (local i32)
local.get 0
drop
local.get 1
drop
)
(func
i32.const 0
i32.const 1
Expand Down Expand Up @@ -34,43 +72,5 @@
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)
)
10 changes: 5 additions & 5 deletions test/wasm2wat/print.t
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
print symbolic:
$ owi wasm2wat m.wasm
(module
(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
)
(func (param i32) (param i32) (result i32)
local.get 0
local.get 1
i32.add
)
(start 1)
)

0 comments on commit f86db3d

Please sign in to comment.