Skip to content

Commit cfd200d

Browse files
authored
Add sign-extension operators (#523)
* implement sign-extension operators * add tests * update py-wasm * wasm2kast: add sign extension operators * add mx basic-features.wat binary parser test * install wabt with apt-get
1 parent 822d849 commit cfd200d

File tree

10 files changed

+22212
-162
lines changed

10 files changed

+22212
-162
lines changed

Dockerfile

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ RUN apt-get update \
88
curl \
99
pandoc \
1010
python3 \
11-
python3-pip
11+
python3-pip \
12+
wabt
1213

1314
RUN git clone 'https://github.com/z3prover/z3' --branch=z3-4.8.15 \
1415
&& cd z3 \
@@ -33,11 +34,4 @@ RUN pip3 install --user \
3334
cytoolz \
3435
numpy
3536

36-
RUN git clone 'https://github.com/WebAssembly/wabt' --branch 1.0.13 --recurse-submodules wabt \
37-
&& cd wabt \
38-
&& mkdir build \
39-
&& cd build \
40-
&& cmake .. \
41-
&& cmake --build .
42-
4337
ENV PATH=/home/user/wabt/build:/home/user/.local/bin:$PATH

numeric.md

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ module WASM-NUMERIC-SYNTAX
2424
| "nearest" [klabel(aNearest), symbol]
2525
// -----------------------------------------------------
2626
27+
syntax ExtendS ::= "extend8_s" [klabel(aExtend8_s) , symbol]
28+
| "extend16_s" [klabel(aExtend16_s), symbol]
29+
| "extend32_s" [klabel(aExtend32_s), symbol]
30+
// ---------------------------------------------------------------
31+
2732
syntax IBinOp ::= "add" [klabel(intAdd), symbol]
2833
| "sub" [klabel(intSub), symbol]
2934
| "mul" [klabel(intMul), symbol]
@@ -110,9 +115,10 @@ module WASM-NUMERIC
110115
`*UnOp` takes one oprand and returns a `Val`.
111116

112117
```k
113-
syntax Val ::= IValType "." IUnOp Int [klabel(intUnOp) , function]
114-
| FValType "." FUnOp Float [klabel(floatUnOp), function]
115-
// ---------------------------------------------------------------------
118+
syntax Val ::= IValType "." IUnOp Int [klabel(intUnOp) , function]
119+
| FValType "." FUnOp Float [klabel(floatUnOp) , function]
120+
| IValType "." ExtendS Int [klabel(extendSUnOp), function]
121+
// ---------------------------------------------------------------------------
116122
```
117123

118124
#### Unary Operators for Integers
@@ -178,6 +184,20 @@ There are 7 unary operators for floats: `abs`, `neg`, `sqrt`, `floor`, `ceil`, `
178184
rule FTYPE . nearest F => < FTYPE > -0.0 requires (notBool #isInfinityOrNaN (F)) andBool Float2Int(F) ==Int 0 andBool signFloat(F)
179185
```
180186

187+
#### Sign-extension Operators for Integers
188+
189+
There are 3 sign-extension operators for integers.
190+
191+
- `extend8_s`
192+
- `extend16_s`
193+
- `extend32_s`
194+
195+
```k
196+
rule ITYPE . extend8_s I => #extends(ITYPE, 1, #wrap(1, I))
197+
rule ITYPE . extend16_s I => #extends(ITYPE, 2, #wrap(2, I))
198+
rule ITYPE . extend32_s I => #extends(ITYPE, 4, #wrap(4, I))
199+
```
200+
181201
### Binary Operators
182202

183203
`*BinOp` takes two oprands and returns a `Val`.
@@ -425,7 +445,13 @@ There are 7 conversion operators: `wrap`, `extend`, `trunc`, `convert`, `demote`
425445

426446
```k
427447
rule i64 . extend_i32_u I:Int => < i64 > I
428-
rule i64 . extend_i32_s I:Int => < i64 > #unsigned(i64, #signed(i32, I))
448+
rule i64 . extend_i32_s I:Int => #extends(i64, 4, I)
449+
450+
syntax IVal ::= #extends(to: IValType, width: Int, val: Int) [function, klabel(extends), symbol]
451+
// ---------------------------------------------------------------------------------------------------
452+
rule #extends(ITYPE, WIDTH, VAL) => < ITYPE > #unsigned(ITYPE, #signedWidth(WIDTH, VAL))
453+
requires WIDTH <=Int #numBytes(ITYPE)
454+
429455
```
430456

431457
- `convert` takes an `int` type value and convert it to the nearest `float` type value.

pykwasm/poetry.lock

Lines changed: 107 additions & 148 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pykwasm/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ python = "^3.10"
1515
cytoolz = "^0.12.1"
1616
numpy = "^1.24.2"
1717
pyk = { git = "https://github.com/runtimeverification/pyk.git", tag="v0.1.489" }
18-
py-wasm = {url = "https://github.com/runtimeverification/py-wasm/archive/refs/tags/0.1.0-alpha.0.tar.gz"}
18+
py-wasm = {url = "https://github.com/runtimeverification/py-wasm/archive/refs/tags/0.1.1.tar.gz"}
1919

2020
[tool.poetry.group.dev.dependencies]
2121
autoflake = "*"

pykwasm/src/pykwasm/kwasm_ast.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ def CALL_INDIRECT(type_idx: int) -> KInner:
243243
I64_CLZ = KApply('aIUnOp', [i64, KApply('aClz', [])])
244244
I64_CTZ = KApply('aIUnOp', [i64, KApply('aCtz', [])])
245245
I64_POPCNT = KApply('aIUnOp', [i64, KApply('aPopcnt', [])])
246+
I32_EXTEND8_s = KApply('aExtendS', [i32, KApply('aExtend8_s', [])])
247+
I32_EXTEND16_s = KApply('aExtendS', [i32, KApply('aExtend16_s', [])])
248+
I64_EXTEND8_s = KApply('aExtendS', [i32, KApply('aExtend8_s', [])])
249+
I64_EXTEND8_s = KApply('aExtendS', [i32, KApply('aExtend16_s', [])])
250+
I64_EXTEND8_s = KApply('aExtendS', [i32, KApply('aExtend32_s', [])])
246251

247252
###############
248253
# Float BinOp #

0 commit comments

Comments
 (0)