Cleanup; Initial switch support; Relooper interface fwiw

This commit is contained in:
dcodeIO
2017-10-11 17:03:22 +02:00
parent 50116acede
commit 6e98c52f76
12 changed files with 646 additions and 383 deletions

View File

@ -28,25 +28,18 @@ mod.addExport("lit", "lit");
mod.addGlobal("42", Type.I32, false, mod.createI32(42));
const aSwitch = mod.addFunctionType("i", Type.I32, []);
mod.addFunction("aSwitch", aSwitch, [], mod.createBlock("", [
mod.createBlock("a", [
mod.createBlock("b", [
mod.createBlock("c", [
mod.createBlock("d", [
mod.createSwitch(
["a", "b", "c"],
"d",
mod.createI32(4)
)
]),
mod.createReturn(mod.createI32(3))
]),
mod.createReturn(mod.createI32(2))
]),
mod.createReturn(mod.createI32(1))
]),
mod.createReturn(mod.createI32(0))
const aSwitch = mod.addFunctionType("ii", Type.I32, [ Type.I32 ]);
const rl = mod.createRelooper();
const b0 = rl.addBlockWithSwitch(mod.createNop(), mod.createGetLocal(0, Type.I32));
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);
mod.addFunction("aSwitch", aSwitch, [ Type.I32 ], mod.createBlock(null, [
rl.renderAndDispose(b0, 1),
mod.createUnreachable()
]));
mod.addExport("aSwitch", "aSwitch");

View File

@ -11,12 +11,37 @@ import { Parser } from "../src/parser";
]); */
const files: Map<string,string> = new Map([
["main", `
export function add(a: i32, b: i32): i32 { let c: i32 = a + b; return c; }
`]
["main",
`
function add(a: i32, b: i32): i32 { return a + b; };
export { add };
export { sub as notadd } from "../other";
2+3;
export function switchMe(n: i32): i32 {
switch (n) {
case 0:
return 0;
default:
return 2;
case 1:
return 1;
case -1:
break;
}
return -1;
}
`],
["../other",
`
export function sub(a: i32, b: i32): i32 { return a - b + c; };
let c: i32 = 42 >> 31;
1+2;
`]
]);
const parser = new Parser();
parser.parseFile(<string>files.get("main"), "main", true);
do {
let nextFile = parser.nextFile();
@ -30,15 +55,7 @@ const program = parser.finish();
const compiler = new Compiler(program);
const module = compiler.compile();
console.log("names", program.names.keys());
console.log("exports", program.exports.keys());
module.optimize();
// module.validate(); // global initializers can't use i32.add etc. yet
// module.optimize();
module.validate();
if (!module.noEmit)
_BinaryenModulePrint(module.ref);
/* console.log("--- statements ---");
compiler.statements.forEach(stmt => {
_BinaryenExpressionPrint(stmt);
}); */