Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.
Merged
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
2 changes: 2 additions & 0 deletions interpreter/valid/valid.ml
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ let check_func (c : context) (f : func) =
let is_const (c : context) (e : instr) =
match e.it with
| Const _ -> true
| Binary (Values.I32 I32Op.(Add | Sub | Mul)) -> true
| Binary (Values.I64 I64Op.(Add | Sub | Mul)) -> true
Comment on lines +385 to +386
Copy link
Member

Choose a reason for hiding this comment

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

Nit: the first -> true isn't needed here. ;)

| GlobalGet x -> let GlobalType (_, mut) = global c x in mut = Immutable
| _ -> false

Expand Down
2 changes: 1 addition & 1 deletion proposals/extended-const/Overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ instruction:

- `i32.add`
- `i32.sub`
- `i32.mul`
- `i64.add`
- `i64.sub`
- `i32.mul`
- `i64.mul`

[spec]: https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
Expand Down
34 changes: 33 additions & 1 deletion test/core/data.wast
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,38 @@
(data (i32.const 1) "a")
)

;; Extended contant expressions

(module
(memory 1)
(data (i32.add (i32.const 0) (i32.const 42)))
)

(module
(memory 1)
(data (i32.sub (i32.const 42) (i32.const 0)))
)

(module
(memory 1)
(data (i32.mul (i32.const 1) (i32.const 2)))
)

;; Combining add, sub, mul and global.get

(module
(global (import "spectest" "global_i32") i32)
(memory 1)
(data (i32.mul
(i32.const 2)
(i32.add
(i32.sub (global.get 0) (i32.const 1))
(i32.const 2)
)
)
)
)

;; Invalid bounds for data

(assert_unlinkable
Expand Down Expand Up @@ -455,4 +487,4 @@
(data (global.get 0))
)
"constant expression required"
)
)
61 changes: 42 additions & 19 deletions test/core/global.wast
Original file line number Diff line number Diff line change
@@ -1,29 +1,46 @@
;; Test globals

(module
(global (import "spectest" "global_i32") i32)
(global (import "spectest" "global_i64") i64)

(global $a i32 (i32.const -2))
(global (;1;) f32 (f32.const -3))
(global (;2;) f64 (f64.const -4))
(global (;3;) f32 (f32.const -3))
(global (;4;) f64 (f64.const -4))
(global $b i64 (i64.const -5))

(global $x (mut i32) (i32.const -12))
(global (;5;) (mut f32) (f32.const -13))
(global (;6;) (mut f64) (f64.const -14))
(global (;7;) (mut f32) (f32.const -13))
(global (;8;) (mut f64) (f64.const -14))
(global $y (mut i64) (i64.const -15))

(global $z1 i32 (global.get 0))
(global $z2 i64 (global.get 1))
(global $z3 i32 (i32.add (i32.sub (i32.mul (i32.const 20) (i32.const 2)) (i32.const 2)) (i32.const 4)))
(global $z4 i64 (i64.add (i64.sub (i64.mul (i64.const 20) (i64.const 2)) (i64.const 2)) (i64.const 5)))
(global $z5 i32 (i32.add (global.get 0) (i32.const 42)))
(global $z6 i64 (i64.add (global.get 1) (i64.const 42)))


(func (export "get-a") (result i32) (global.get $a))
(func (export "get-b") (result i64) (global.get $b))
(func (export "get-x") (result i32) (global.get $x))
(func (export "get-y") (result i64) (global.get $y))
(func (export "get-z1") (result i32) (global.get $z1))
(func (export "get-z2") (result i64) (global.get $z2))
(func (export "get-z3") (result i32) (global.get $z3))
(func (export "get-z4") (result i64) (global.get $z4))
(func (export "get-z5") (result i32) (global.get $z5))
(func (export "get-z6") (result i64) (global.get $z6))
(func (export "set-x") (param i32) (global.set $x (local.get 0)))
(func (export "set-y") (param i64) (global.set $y (local.get 0)))

(func (export "get-1") (result f32) (global.get 1))
(func (export "get-2") (result f64) (global.get 2))
(func (export "get-5") (result f32) (global.get 5))
(func (export "get-6") (result f64) (global.get 6))
(func (export "set-5") (param f32) (global.set 5 (local.get 0)))
(func (export "set-6") (param f64) (global.set 6 (local.get 0)))
(func (export "get-3") (result f32) (global.get 3))
(func (export "get-4") (result f64) (global.get 4))
(func (export "get-7") (result f32) (global.get 7))
(func (export "get-8") (result f64) (global.get 8))
(func (export "set-7") (param f32) (global.set 7 (local.get 0)))
(func (export "set-8") (param f64) (global.set 8 (local.get 0)))

;; As the argument of control constructs and instructions

Expand Down Expand Up @@ -182,21 +199,27 @@
(assert_return (invoke "get-b") (i64.const -5))
(assert_return (invoke "get-x") (i32.const -12))
(assert_return (invoke "get-y") (i64.const -15))

(assert_return (invoke "get-1") (f32.const -3))
(assert_return (invoke "get-2") (f64.const -4))
(assert_return (invoke "get-5") (f32.const -13))
(assert_return (invoke "get-6") (f64.const -14))
(assert_return (invoke "get-z1") (i32.const 666))
(assert_return (invoke "get-z2") (i64.const 666))
(assert_return (invoke "get-z3") (i32.const 42))
(assert_return (invoke "get-z4") (i64.const 43))
(assert_return (invoke "get-z5") (i32.const 708))
(assert_return (invoke "get-z6") (i64.const 708))

(assert_return (invoke "get-3") (f32.const -3))
(assert_return (invoke "get-4") (f64.const -4))
(assert_return (invoke "get-7") (f32.const -13))
(assert_return (invoke "get-8") (f64.const -14))

(assert_return (invoke "set-x" (i32.const 6)))
(assert_return (invoke "set-y" (i64.const 7)))
(assert_return (invoke "set-5" (f32.const 8)))
(assert_return (invoke "set-6" (f64.const 9)))
(assert_return (invoke "set-7" (f32.const 8)))
(assert_return (invoke "set-8" (f64.const 9)))

(assert_return (invoke "get-x") (i32.const 6))
(assert_return (invoke "get-y") (i64.const 7))
(assert_return (invoke "get-5") (f32.const 8))
(assert_return (invoke "get-6") (f64.const 9))
(assert_return (invoke "get-7") (f32.const 8))
(assert_return (invoke "get-8") (f64.const 9))

(assert_return (invoke "as-select-first") (i32.const 6))
(assert_return (invoke "as-select-mid") (i32.const 2))
Expand Down