Skip to content

Commit

Permalink
Init bulk memory
Browse files Browse the repository at this point in the history
  • Loading branch information
dy committed Sep 14, 2023
1 parent a422dbb commit b1496b8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
10 changes: 8 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,15 @@ parse(`(func (export "double") (param f64) (result f64) (f64.mul (local.get 0) (
// ]
```

## Limitations
## Status

* [x] wasm core
* [ ] floating HEX support, eg. `(f32.const 0x1.fffffep+127)`.
* [x] multiple values
* [x] bulk memory ops (0 index)
* [ ] func/ref types
* [ ] multiple memories

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

<!--
## Projects using watr
Expand Down
18 changes: 15 additions & 3 deletions src/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const build = {
// NOTE: numeric comparison is faster than generic hash lookup

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

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

// ignore (then) and other unknown instructions
if (opCode >= 0) out.push(opCode)
// ignore (then) and other unknown (-1) instructions
if (opCode >= 0) {
// bulk memory: (memory.init) (memory.copy) etc
// https://github.com/WebAssembly/bulk-memory-operations/blob/master/proposals/bulk-memory-operations/Overview.md#instruction-encoding
if (opCode >= 252) {
opCode %= 252
out.push(0xfc)
immed = [0]
// even opCodes (memory.init, memory.copy, table.init, table.copy) have 2 immediates
if (!(opCode % 2)) immed.push(0)
}

out.push(opCode)
}
if (immed) out.push(...immed)
}

Expand Down
4 changes: 2 additions & 2 deletions src/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const OP = [
'i64.trunc_f32_s', 'i64.trunc_f32_u', 'i64.trunc_f64_s', 'i64.trunc_f64_u',
'f32.convert_i32_s', 'f32.convert_i32_u', 'f32.convert_i64_s', 'f32.convert_i64_u', 'f32.demote_f64',
'f64.convert_i32_s', 'f64.convert_i32_u', 'f64.convert_i64_s', 'f64.convert_i64_u', 'f64.promote_f32',
'i32.reinterpret_f32', 'i64.reinterpret_f64', 'f32.reinterpret_i32', 'f64.reinterpret_i64',
'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'
],
SECTION = { type: 1, import: 2, func: 3, table: 4, memory: 5, global: 6, export: 7, start: 8, elem: 9, code: 10, data: 11 },
TYPE = { i32: 0x7f, i64: 0x7e, f32: 0x7d, f64: 0x7c, void: 0x40, func: 0x60, funcref: 0x70 },
Expand All @@ -39,4 +39,4 @@ export const OP = [
},
BLOCK = {
loop: 1, block: 1, if: 1, end: -1, return: -1
}
}
15 changes: 13 additions & 2 deletions test/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,15 @@ t('feature: multiple results', () => {
is(compile(parse(src5)), wat2wasm(src5).buffer)
})

t('feature: bulk memory', () => {
let src = `(func $x (result f64)
(memory.copy (local.get 0)(i32.const 0)(i32.const 16))
(memory.fill (local.get 0)(i32.const 0)(i32.const 16))
(memory.init 0 (local.get 0)(i32.const 0)(i32.const 16))
)`
is(compile(parse(src)), wat2wasm(src).buffer)
})


// examples
t('example: wat-compiler', async () => {
Expand Down Expand Up @@ -1255,13 +1264,15 @@ const hex = (str, ...fields) =>
// convert wast code to binary via Wabt
export function wat2wasm(code, config) {
let metrics = config ? config.metrics : true
const parsed = wabt.parseWat('inline', code, {})
const parsed = wabt.parseWat('inline', code, {
bulk_memory: true
})
// metrics && console.time('wabt build')
const binary = parsed.toBinary({
log: true,
canonicalize_lebs: true,
relocatable: false,
write_debug_names: false,
write_debug_names: false
})
parsed.destroy()
// metrics && console.timeEnd('wabt build')
Expand Down

0 comments on commit b1496b8

Please sign in to comment.