Skip to content

Commit

Permalink
Factor out swizzle
Browse files Browse the repository at this point in the history
  • Loading branch information
dy committed Sep 10, 2024
1 parent 700bcbd commit 72c08ae
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
37 changes: 24 additions & 13 deletions src/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,18 @@ const build = {
// (v128.load_lane_zero)
if (opCode <= 0x5b) immed.push(...uleb(args.shift()))
}
// (v128.const i32x4), (i8x16.shuffle 0 1 ... 15 a b)
else if (opCode === 0x0c || opCode === 0x0d) {
immed.push(...consumeConst(op, args))
// (i8x16.shuffle 0 1 ... 15 a b)
else if (opCode === 0x0d) {
// i8, i16, i32 - bypass the encoding
for (let i = 0; i < 16; i++) immed.push(encode.i32.parse(args.shift()))
}
// (v128.const i32x4)
else if (opCode === 0x0c) {
// immed.push(...consumeConst(op, args))
// FIXME: find more elegant way here
args.unshift(op)
immed = initGlobal(args, ctx)
immed.pop()
}
// (i8x16.extract_lane_s 0 ...)
else if (opCode >= 0x15 && opCode <= 0x22) {
Expand Down Expand Up @@ -311,7 +320,7 @@ const build = {
let name = args[0][0] === '$' && args.shift()
if (name) ctx.global[name] = ctx.global.length
let [type, init] = args, mut = type[0] === 'mut' ? 1 : 0
ctx.global.push([TYPE[mut ? type[1] : type], mut, ...initGlobal(init)])
ctx.global.push([TYPE[mut ? type[1] : type], mut, ...initGlobal([...init], ctx)])
},

// (table 1 2? funcref)
Expand All @@ -326,7 +335,7 @@ const build = {
// (elem (i32.const 0) $f1 $f2), (elem (global.get 0) $f1 $f2)
elem([, offset, ...elems], ctx) {
const tableIdx = 0 // FIXME: table index can be defined
ctx.elem.push([tableIdx, ...initGlobal(offset, ctx), ...uleb(elems.length), ...elems.flatMap(el => uleb(el[0] === '$' ? ctx.func[el] : el))])
ctx.elem.push([tableIdx, ...initGlobal([...offset], ctx), ...uleb(elems.length), ...elems.flatMap(el => uleb(el[0] === '$' ? ctx.func[el] : el))])
},

// (export "name" (kind $name|idx))
Expand Down Expand Up @@ -379,7 +388,7 @@ const build = {
if (!offset && !mem) offset = inits.shift()
if (!offset) offset = ['i32.const', 0]

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

// (start $main)
Expand All @@ -389,25 +398,27 @@ const build = {
}

// (i32.const 0), (global.get idx) - instantiation time initializer
const initGlobal = ([op, literal, ...args], ctx) => {
if (op === 'global.get') return [0x23, ...uleb(literal[0] === '$' ? ctx.global[literal] : literal), 0x0b]
const initGlobal = (args, ctx) => {
let op = args.shift()

if (op === 'global.get') return [0x23, ...uleb(args[0][0] === '$' ? ctx.global[args[0]] : args[0]), 0x0b]

// (v128.const i32x4 1 2 3 4), (i32.add a b) etc
return [
...(op === 'v128.const' ? [0xfd, 0x0c] : [0x41 + ['i32.const', 'i64.const', 'f32.const', 'f64.const'].indexOf(op)]),
...consumeConst(op, [literal, ...args]), 0x0b
...consumeConst(op, args), 0x0b
]
}

// consume cost, no op type
const consumeConst = (op, args) => {
if (op === 'global.get') return [0x23, ...uleb(literal[0] === '$' ? ctx.global[literal] : literal), 0x0b]
// if (op === 'global.get') return [0x23, ...uleb(literal[0] === '$' ? ctx.global[literal] : literal), 0x0b]

const [type] = op.split('.')

// (v128.const i32x4 1 2 3 4), (i8x16.shuffle 1 2 ... 15)
if (type === 'v128' || type === 'i8x16') {
let [t, n] = (type === 'v128' ? args.shift() : type).split('x'),
// (v128.const i32x4 1 2 3 4)
if (type === 'v128') {
let [t, n] = args.shift().split('x'),
stride = t.slice(1) >>> 3 // i16 -> 2, f32 -> 4

n = +n
Expand Down
2 changes: 1 addition & 1 deletion test/bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { wat2wasm } from './compile.js'


// bench
t('bench: brownian', async () => {
t.skip('bench: brownian', async () => {
// example.ts
let src = await file('/test/example/brownian.wat')
// let src = `(module
Expand Down
1 change: 0 additions & 1 deletion test/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2195,7 +2195,6 @@ t('example: legacy', async () => {




export async function file(path) {
let res = await fetch(path)
let src = await res.text()
Expand Down
5 changes: 1 addition & 4 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,4 @@
}
</script>

<script src="./parse.js" type="module"></script>
<script src="./compile.js" type="module"></script>
<script src="./print.js" type="module"></script>
<script src="./bench.js" type="module"></script>
<script src="./index.js" type="module"></script>

0 comments on commit 72c08ae

Please sign in to comment.