Skip to content

Commit

Permalink
fix: provide type to select instruction (#46)
Browse files Browse the repository at this point in the history
* fix: provide type to select instruction

* Add test for select instruction
  • Loading branch information
ospencer committed Jan 9, 2021
1 parent ab737bd commit 2096a3c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/expression.ml
Expand Up @@ -65,7 +65,9 @@ external unary : Module.t -> Op.t -> t -> t = "caml_binaryen_unary"

external binary : Module.t -> Op.t -> t -> t -> t = "caml_binaryen_binary"

external select : Module.t -> t -> t -> t -> t = "caml_binaryen_select"
external select : Module.t -> t -> t -> t -> Type.t -> t = "caml_binaryen_select"
let select wasm_mod cond tru fals =
select wasm_mod cond tru fals Type.auto
(** Module, condition, true branch, false branch. *)

external drop : Module.t -> t -> t = "caml_binaryen_drop"
Expand Down
12 changes: 10 additions & 2 deletions test/test.expected
Expand Up @@ -3,7 +3,11 @@
(func $adder (param $0 i32) (param $1 i32) (result i32)
(block $add (result i32)
(i32.add
(local.get $0)
(select
(local.get $0)
(local.get $1)
(i32.const 1)
)
(local.get $1)
)
)
Expand All @@ -13,7 +17,11 @@
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(func $0 (param $0 i32) (param $1 i32) (result i32)
(i32.add
(local.get $0)
(select
(local.get $0)
(local.get $1)
(i32.const 1)
)
(local.get $1)
)
)
Expand Down
4 changes: 3 additions & 1 deletion test/test.ml
Expand Up @@ -12,7 +12,9 @@ let x = Expression.local_get wasm_mod 0 Type.int32

let y = Expression.local_get wasm_mod 1 Type.int32

let add = Expression.block wasm_mod ~return_type:Type.int32 "add" [Expression.binary wasm_mod Op.add_int32 x y]
let select = Expression.select wasm_mod (Expression.const wasm_mod (Literal.int32 1l)) x y

let add = Expression.block wasm_mod ~return_type:Type.int32 "add" [Expression.binary wasm_mod Op.add_int32 select y]

(* Create the add function *)
let adder = Function.add_function wasm_mod "adder" params results [||] add
Expand Down

0 comments on commit 2096a3c

Please sign in to comment.