diff --git a/interpreter/valid/valid.ml b/interpreter/valid/valid.ml index 7dd573b5..25a65f59 100644 --- a/interpreter/valid/valid.ml +++ b/interpreter/valid/valid.ml @@ -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 | GlobalGet x -> let GlobalType (_, mut) = global c x in mut = Immutable | _ -> false diff --git a/proposals/extended-const/Overview.md b/proposals/extended-const/Overview.md index b3833031..10777909 100644 --- a/proposals/extended-const/Overview.md +++ b/proposals/extended-const/Overview.md @@ -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 diff --git a/test/core/data.wast b/test/core/data.wast index e840a946..15f37e37 100644 --- a/test/core/data.wast +++ b/test/core/data.wast @@ -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 @@ -455,4 +487,4 @@ (data (global.get 0)) ) "constant expression required" -) \ No newline at end of file +) diff --git a/test/core/global.wast b/test/core/global.wast index 55d7447b..4f36df71 100644 --- a/test/core/global.wast +++ b/test/core/global.wast @@ -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 @@ -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))