Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial tests for globals #57

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ml-proto/src/spec/check.ml
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,14 @@ let rec check_expr c ts e =

| SetLocal (x, e1) ->
check_expr c [local c x] e1;
check_type [] ts e.at
check_type [local c x] ts e.at
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and below, it might be nice to factor out via let t1 = local c x in to avoid duplicating the expression.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will, just need to read an introduction to ocaml somewhere, any good recommendation? (I already code in clojure, erlang and scheme so it can be a high level introduction)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I actually find it more readable (since shorter) as is. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hehe, fair enough. I was on the edge anyhow for such a simple duplicated expr.


| LoadGlobal x ->
check_type [global c x] ts e.at

| StoreGlobal (x, e1) ->
check_expr c [global c x] e1;
check_type [] ts e.at
check_type [global c x] ts e.at

| Load (memop, e1) ->
check_memop memop e.at;
Expand All @@ -197,7 +197,7 @@ let rec check_expr c ts e =
check_memop memop e.at;
check_expr c [Int32Type] e1;
check_expr c [memop.ty] e2;
check_type [] ts e.at
check_type [memop.ty] ts e.at

| Const v ->
check_literal c ts v
Expand Down
6 changes: 3 additions & 3 deletions ml-proto/src/spec/eval.ml
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ let rec eval_expr c e =
| SetLocal (x, e1) ->
let v1 = unary (eval_expr c e1) e1.at in
local c x := v1;
[]
[v1]

| LoadGlobal x ->
[!(global c x)]

| StoreGlobal (x, e1) ->
let v1 = unary (eval_expr c e1) e1.at in
global c x := v1;
[]
[v1]

| Load ({mem; ty; _}, e1) ->
let v1 = unary (eval_expr c e1) e1.at in
Expand All @@ -172,7 +172,7 @@ let rec eval_expr c e =
let v2 = unary (eval_expr c e2) e2.at in
(try Memory.store c.modul.memory (Memory.address_of_value v1) mem v2
with exn -> memory_error e.at exn);
[]
[v2]

| Const v ->
[v.it]
Expand Down
44 changes: 44 additions & 0 deletions ml-proto/test/globals.wasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(module
(global $rx0 i32)
(global $rx1 i32)

(func $gx0 (load_global $rx0))
(func $sx0 (param i32) (store_global $rx0 (get_local 0)))

(func $gx1 (load_global $rx1))
(func $sx1 (param i32) (store_global $rx1 (get_local 0)))

(func $storeandload (param i32)
(call $sx0 (get_local 0))
(call $gx0))

(func $lssg (param i32) (result i32)
(local $l1 i32)
(local $l2 i32)

(set_local $l1 (set_local $l2 (get_local 0))))

(func $gssg (param i32) (result i32)
(store_global $rx0 (store_global $rx1 (get_local 0))))

(export "gx0" $gx0)
(export "sx0" $sx0)

(export "gx1" $gx1)
(export "sx1" $sx1)

(export "localsetsetget" $lssg)
(export "globalsetsetget" $gssg)

(export "storeandload" $storeandload)
)

(assert_eq (invoke "storeandload" (i32.const 25)) (i32.const 25))
(assert_eq (invoke "sx0" (i32.const 10)) (i32.const 10))
(assert_eq (invoke "gx0") (i32.const 10))

(assert_eq (invoke "sx1" (i32.const 12)) (i32.const 12))
(assert_eq (invoke "gx1") (i32.const 12))
(assert_eq (invoke "gx0") (i32.const 10))
(assert_eq (invoke "localsetsetget" (i32.const 13)) (i32.const 13))
(assert_eq (invoke "globalsetsetget" (i32.const 14)) (i32.const 14))
11 changes: 11 additions & 0 deletions ml-proto/test/memory_store_return.wasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(module
(memory 1024)

(func $store-i32 (param $v i32) (result i32)
(i32.store/i32 (i32.const 4) (get_local $v)))

(export "store-i32" $store-i32))

(assert_eq (invoke "store-i32" (i32.const 42)) (i32.const 42))