Skip to content

Commit 72c08ae

Browse files
committed
Factor out swizzle
1 parent 700bcbd commit 72c08ae

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

src/compile.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,18 @@ const build = {
139139
// (v128.load_lane_zero)
140140
if (opCode <= 0x5b) immed.push(...uleb(args.shift()))
141141
}
142-
// (v128.const i32x4), (i8x16.shuffle 0 1 ... 15 a b)
143-
else if (opCode === 0x0c || opCode === 0x0d) {
144-
immed.push(...consumeConst(op, args))
142+
// (i8x16.shuffle 0 1 ... 15 a b)
143+
else if (opCode === 0x0d) {
144+
// i8, i16, i32 - bypass the encoding
145+
for (let i = 0; i < 16; i++) immed.push(encode.i32.parse(args.shift()))
146+
}
147+
// (v128.const i32x4)
148+
else if (opCode === 0x0c) {
149+
// immed.push(...consumeConst(op, args))
150+
// FIXME: find more elegant way here
151+
args.unshift(op)
152+
immed = initGlobal(args, ctx)
153+
immed.pop()
145154
}
146155
// (i8x16.extract_lane_s 0 ...)
147156
else if (opCode >= 0x15 && opCode <= 0x22) {
@@ -311,7 +320,7 @@ const build = {
311320
let name = args[0][0] === '$' && args.shift()
312321
if (name) ctx.global[name] = ctx.global.length
313322
let [type, init] = args, mut = type[0] === 'mut' ? 1 : 0
314-
ctx.global.push([TYPE[mut ? type[1] : type], mut, ...initGlobal(init)])
323+
ctx.global.push([TYPE[mut ? type[1] : type], mut, ...initGlobal([...init], ctx)])
315324
},
316325

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

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

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

385394
// (start $main)
@@ -389,25 +398,27 @@ const build = {
389398
}
390399

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

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

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

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

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

413424
n = +n

test/bench.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { wat2wasm } from './compile.js'
88

99

1010
// bench
11-
t('bench: brownian', async () => {
11+
t.skip('bench: brownian', async () => {
1212
// example.ts
1313
let src = await file('/test/example/brownian.wat')
1414
// let src = `(module

test/compile.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2195,7 +2195,6 @@ t('example: legacy', async () => {
21952195

21962196

21972197

2198-
21992198
export async function file(path) {
22002199
let res = await fetch(path)
22012200
let src = await res.text()

test/index.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,4 @@
1111
}
1212
</script>
1313

14-
<script src="./parse.js" type="module"></script>
15-
<script src="./compile.js" type="module"></script>
16-
<script src="./print.js" type="module"></script>
17-
<script src="./bench.js" type="module"></script>
14+
<script src="./index.js" type="module"></script>

0 commit comments

Comments
 (0)