Skip to content

Commit b1496b8

Browse files
committed
Init bulk memory
1 parent a422dbb commit b1496b8

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

readme.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,15 @@ parse(`(func (export "double") (param f64) (result f64) (f64.mul (local.get 0) (
9696
// ]
9797
```
9898

99-
## Limitations
99+
## Status
100+
101+
* [x] wasm core
102+
* [ ] floating HEX support, eg. `(f32.const 0x1.fffffep+127)`.
103+
* [x] multiple values
104+
* [x] bulk memory ops (0 index)
105+
* [ ] func/ref types
106+
* [ ] multiple memories
100107

101-
No floating HEX support, eg. `(f32.const 0x1.fffffep+127)`.
102108

103109
<!--
104110
## Projects using watr

src/compile.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ const build = {
122122
// NOTE: numeric comparison is faster than generic hash lookup
123123

124124
// binary/unary - just consume immed
125-
if (opCode >= 69) { }
125+
if (opCode >= 69 && opCode <= 252) { }
126126

127127
// (i32.store align=n offset=m at value)
128128
else if (opCode >= 40 && opCode <= 62) {
@@ -237,8 +237,20 @@ const build = {
237237
while (args.length) consume(args, out)
238238
}
239239

240-
// ignore (then) and other unknown instructions
241-
if (opCode >= 0) out.push(opCode)
240+
// ignore (then) and other unknown (-1) instructions
241+
if (opCode >= 0) {
242+
// bulk memory: (memory.init) (memory.copy) etc
243+
// https://github.com/WebAssembly/bulk-memory-operations/blob/master/proposals/bulk-memory-operations/Overview.md#instruction-encoding
244+
if (opCode >= 252) {
245+
opCode %= 252
246+
out.push(0xfc)
247+
immed = [0]
248+
// even opCodes (memory.init, memory.copy, table.init, table.copy) have 2 immediates
249+
if (!(opCode % 2)) immed.push(0)
250+
}
251+
252+
out.push(opCode)
253+
}
242254
if (immed) out.push(...immed)
243255
}
244256

src/const.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const OP = [
2525
'i64.trunc_f32_s', 'i64.trunc_f32_u', 'i64.trunc_f64_s', 'i64.trunc_f64_u',
2626
'f32.convert_i32_s', 'f32.convert_i32_u', 'f32.convert_i64_s', 'f32.convert_i64_u', 'f32.demote_f64',
2727
'f64.convert_i32_s', 'f64.convert_i32_u', 'f64.convert_i64_s', 'f64.convert_i64_u', 'f64.promote_f32',
28-
'i32.reinterpret_f32', 'i64.reinterpret_f64', 'f32.reinterpret_i32', 'f64.reinterpret_i64',
28+
'i32.reinterpret_f32', 'i64.reinterpret_f64', 'f32.reinterpret_i32', 'f64.reinterpret_i64', , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , 'memory.init', 'data.drop', 'memory.copy', 'memory.fill', 'table.init', 'elem.drop', 'table.copy'
2929
],
3030
SECTION = { type: 1, import: 2, func: 3, table: 4, memory: 5, global: 6, export: 7, start: 8, elem: 9, code: 10, data: 11 },
3131
TYPE = { i32: 0x7f, i64: 0x7e, f32: 0x7d, f64: 0x7c, void: 0x40, func: 0x60, funcref: 0x70 },
@@ -39,4 +39,4 @@ export const OP = [
3939
},
4040
BLOCK = {
4141
loop: 1, block: 1, if: 1, end: -1, return: -1
42-
}
42+
}

test/compile.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,15 @@ t('feature: multiple results', () => {
11621162
is(compile(parse(src5)), wat2wasm(src5).buffer)
11631163
})
11641164

1165+
t('feature: bulk memory', () => {
1166+
let src = `(func $x (result f64)
1167+
(memory.copy (local.get 0)(i32.const 0)(i32.const 16))
1168+
(memory.fill (local.get 0)(i32.const 0)(i32.const 16))
1169+
(memory.init 0 (local.get 0)(i32.const 0)(i32.const 16))
1170+
)`
1171+
is(compile(parse(src)), wat2wasm(src).buffer)
1172+
})
1173+
11651174

11661175
// examples
11671176
t('example: wat-compiler', async () => {
@@ -1255,13 +1264,15 @@ const hex = (str, ...fields) =>
12551264
// convert wast code to binary via Wabt
12561265
export function wat2wasm(code, config) {
12571266
let metrics = config ? config.metrics : true
1258-
const parsed = wabt.parseWat('inline', code, {})
1267+
const parsed = wabt.parseWat('inline', code, {
1268+
bulk_memory: true
1269+
})
12591270
// metrics && console.time('wabt build')
12601271
const binary = parsed.toBinary({
12611272
log: true,
12621273
canonicalize_lebs: true,
12631274
relocatable: false,
1264-
write_debug_names: false,
1275+
write_debug_names: false
12651276
})
12661277
parsed.destroy()
12671278
// metrics && console.timeEnd('wabt build')

0 commit comments

Comments
 (0)