2017-12-08 19:08:03 +01:00
|
|
|
/// <reference path="../lib/binaryen.d.ts" />
|
2017-12-02 01:14:15 +01:00
|
|
|
|
2017-09-28 13:08:25 +02:00
|
|
|
import "../src/glue/js";
|
2017-12-02 01:14:15 +01:00
|
|
|
import { NativeType, Module, MemorySegment, BinaryOp } from "../src/module";
|
2017-09-28 13:08:25 +02:00
|
|
|
import { Target } from "../src/compiler";
|
|
|
|
import { U64 } from "../src/util";
|
|
|
|
|
|
|
|
const mod = Module.create();
|
|
|
|
|
|
|
|
mod.setMemory(1, Module.MAX_MEMORY_WASM32, [
|
|
|
|
MemorySegment.create(new Uint8Array(4), U64.fromI32(8)),
|
|
|
|
MemorySegment.create(new Uint8Array(4), U64.fromI32(12))
|
|
|
|
], Target.WASM32, "memory");
|
|
|
|
|
2017-12-02 01:14:15 +01:00
|
|
|
const add = mod.addFunctionType("iii", NativeType.I32, [ NativeType.I32, NativeType.I32 ]);
|
2017-09-28 13:08:25 +02:00
|
|
|
mod.addFunction("add", add, [], mod.createReturn(
|
|
|
|
mod.createBinary(BinaryOp.AddI32,
|
2017-12-02 01:14:15 +01:00
|
|
|
mod.createGetLocal(0, NativeType.I32),
|
|
|
|
mod.createGetLocal(1, NativeType.I32)
|
2017-09-28 13:08:25 +02:00
|
|
|
)
|
|
|
|
));
|
2017-12-08 19:08:03 +01:00
|
|
|
mod.addFunctionExport("add", "add");
|
2017-09-28 13:08:25 +02:00
|
|
|
|
2017-12-02 01:14:15 +01:00
|
|
|
const lit = mod.addFunctionType("I", NativeType.I64, []);
|
2017-09-28 13:08:25 +02:00
|
|
|
mod.addFunction("lit", lit, [], mod.createReturn(
|
|
|
|
mod.createI64(0, 0x80000000) // I64_MIN
|
|
|
|
));
|
2017-12-08 19:08:03 +01:00
|
|
|
mod.addFunctionExport("lit", "lit");
|
2017-09-28 13:08:25 +02:00
|
|
|
|
2017-12-02 01:14:15 +01:00
|
|
|
mod.addGlobal("42", NativeType.I32, false, mod.createI32(42));
|
2017-09-28 13:08:25 +02:00
|
|
|
|
2017-12-02 01:14:15 +01:00
|
|
|
const aSwitch = mod.addFunctionType("ii", NativeType.I32, [ NativeType.I32 ]);
|
2017-10-11 17:03:22 +02:00
|
|
|
const rl = mod.createRelooper();
|
2017-12-02 01:14:15 +01:00
|
|
|
const b0 = rl.addBlockWithSwitch(mod.createNop(), mod.createGetLocal(0, NativeType.I32));
|
2017-10-11 17:03:22 +02:00
|
|
|
let b1, b2, b3;
|
|
|
|
rl.addBranchForSwitch(b0, b2 = rl.addBlock(mod.createReturn(mod.createI32(1))), [1]); // indexed branch
|
|
|
|
rl.addBranchForSwitch(b0, b3 = rl.addBlock(mod.createReturn(mod.createI32(2))), [2]); // indexed branch
|
|
|
|
rl.addBranch(b0, b1 = rl.addBlock(mod.createDrop(mod.createI32(0)))); // default branch
|
|
|
|
rl.addBranch(b1, b2);
|
|
|
|
|
2017-12-02 01:14:15 +01:00
|
|
|
mod.addFunction("aSwitch", aSwitch, [ NativeType.I32 ], mod.createBlock(null, [
|
2017-10-11 17:03:22 +02:00
|
|
|
rl.renderAndDispose(b0, 1),
|
|
|
|
mod.createUnreachable()
|
2017-09-28 13:08:25 +02:00
|
|
|
]));
|
2017-12-08 19:08:03 +01:00
|
|
|
mod.addFunctionExport("aSwitch", "aSwitch");
|
2017-09-28 13:08:25 +02:00
|
|
|
|
|
|
|
// mod.optimize();
|
|
|
|
if (mod.validate())
|
|
|
|
_BinaryenModulePrint(mod.ref);
|
|
|
|
_BinaryenModuleDispose(mod.ref);
|