Skip to content

Commit

Permalink
Cleanup data init cases
Browse files Browse the repository at this point in the history
  • Loading branch information
dy committed Aug 12, 2024
1 parent 740340a commit f4fd0ac
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ parse(`(func (export "double") (param f64) (result f64) (f64.mul (local.get 0) (
* [x] wasm core
* [x] multiple values
* [x] bulk memory ops (0 index)
* [ ] func/ref types
* [ ] simd
* [ ] multiple memories
* [ ] func/ref types


<!--
Expand Down
26 changes: 18 additions & 8 deletions src/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { uleb, leb, bigleb, f64, f32 } from './util.js'
import { OP, SECTION, ALIGN, TYPE, KIND } from './const.js'
import parse from './parse.js'

const OP_END = 0xb, OP_I32_CONST = 0x41, OP_I64_CONST = 0x42, OP_F32_CONST = 0x43, OP_F64_CONST = 0x44, OP_SKIP = 0x6
const OP_END = 0xb, OP_I32_CONST = 0x41, OP_I64_CONST = 0x42, OP_F32_CONST = 0x43, OP_F64_CONST = 0x44, OP_SKIP = 0x6, OP_GLOBAL_GET = 35, OP_GLOBAL_SET = 36

/**
* Converts a WebAssembly Text Format (WAT) tree to a WebAssembly binary format (Wasm).
Expand Down Expand Up @@ -156,7 +156,7 @@ const build = {
}

// (global.get id), (global.set id)
else if (opCode == 35 || opCode == 36) {
else if (opCode == OP_GLOBAL_GET || opCode == OP_GLOBAL_SET) {
immed = uleb(args[0]?.[0] === '$' ? ctx.global[args.shift()] : args.shift())
}

Expand Down Expand Up @@ -341,8 +341,17 @@ const build = {
},

// (data (i32.const 0) "\aa" "\bb"?)
data([, offset, ...inits], ctx) {
if (offset[0] === 'offset') [, offset] = offset
// (data (offset (i32.const 0)) (memory ref) "\aa" "\bb"?)
// (data (global.get $x) "\aa" "\bb"?)
data([, ...inits], ctx) {
let offset, mem

if (inits[0]?.[0] === 'offset') [, offset] = inits.shift()
if (inits[0]?.[0] === 'memory') [, mem] = inits.shift()
if (inits[0]?.[0] === 'offset') [, offset] = inits.shift()
if (!offset && !mem) offset = inits.shift()
if (!offset) offset = ['i32.const', 0]

ctx.data.push([0, ...iinit(offset, ctx), ...str(inits.map(i => i[0] === '"' ? i.slice(1, -1) : i).join(''))])
},

Expand All @@ -352,13 +361,14 @@ const build = {
}
}

// (i32.const 0) - instantiation time initializer
// (i32.const 0), (global.get idx) - instantiation time initializer
const iinit = ([op, literal], ctx) =>
op === 'f32.const' ? [OP_F32_CONST, ...f32(literal), OP_END] :
op === 'f64.const' ? [OP_F64_CONST, ...f64(literal), OP_END] :
op === 'i32.const' ? [OP_I32_CONST, ...leb(literal[0] === '$' ? ctx.global[literal] : literal), OP_END] :
op === 'i64.const' ? [OP_I64_CONST, ...bigleb(literal[0] === '$' ? ctx.global[literal] : literal), OP_END] :
err(`Unknown init ${op} ${literal}`)
op === 'i32.const' ? [OP_I32_CONST, ...leb(literal), OP_END] :
op === 'i64.const' ? [OP_I64_CONST, ...bigleb(literal), OP_END] :
op === 'global.get' ? [OP_GLOBAL_GET, ...uleb(literal[0] === '$' ? ctx.global[literal] : literal), OP_END] :
err(`Unknown init ${op} ${literal}`)

const escape = { n: 10, r: 13, t: 9, v: 1, '\\': 92 }

Expand Down
37 changes: 37 additions & 0 deletions test/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,43 @@ t('case: data offset', () => {
is(compile(parse(src)), wat2wasm(src).buffer)
})

t('case: data full cases', () => {
// ref: https://github.com/WebAssembly/simd/blob/master/test/core/data.wast
let src = `
(global $a i32 (i32.const 1))
(global $b i32 (i32.const 1))
(data (i32.const 0))
(data (i32.const 1) "a" "" "bcd")
(data (i32.const 0x1_0000) "")
(data (offset (i32.const 0)))
(data (offset (i32.const 0)) "" "a" "bc" "")
(data (global.get $a) "a")
(data (global.get 1) "bc")
;; (data (memory 0) (i32.const 0))
;; (data (memory 0x0) (i32.const 1) "a" "" "bcd")
;; (data (memory 0x000) (offset (i32.const 0)))
;; (data (memory 0) (offset (i32.const 0)) "" "a" "bc" "")
;; (data (memory $m) (i32.const 0))
;; (data (memory $m) (i32.const 1) "a" "" "bcd")
;; (data (memory $m) (offset (i32.const 0)))
;; (data (memory $m) (offset (i32.const 0)) "" "a" "bc" "")
;; (data $d1 (i32.const 0))
;; (data $d2 (i32.const 1) "a" "" "bcd")
;; (data $d3 (offset (i32.const 0)))
;; (data $d4 (offset (i32.const 0)) "" "a" "bc" "")
;; (data $d5 (memory 0) (i32.const 0))
;; (data $d6 (memory 0x0) (i32.const 1) "a" "" "bcd")
;; (data $d7 (memory 0x000) (offset (i32.const 0)))
;; (data $d8 (memory 0) (offset (i32.const 0)) "" "a" "bc" "")
;; (data $d9 (memory $m) (i32.const 0))
;; (data $d10 (memory $m) (i32.const 1) "a" "" "bcd")
;; (data $d11 (memory $m) (offset (i32.const 0)))
;; (data $d12 (memory $m) (offset (i32.const 0)) "" "a" "bc" "")
`

is(compile(parse(src)), wat2wasm(src).buffer)
})

t('case: float hex', () => {
let src = `(func (f64.const 0x1p+0) (f64.const -0x1.7f00a2d80faabp-35))`

Expand Down

0 comments on commit f4fd0ac

Please sign in to comment.