mirror of
https://github.com/Noratrieb/riverdelta.git
synced 2026-01-17 09:55:02 +01:00
make codegen smarter around !
This commit is contained in:
parent
1dad80f4c1
commit
0c996eb9bc
2 changed files with 15 additions and 7 deletions
|
|
@ -12,7 +12,7 @@ import { exec } from "child_process";
|
||||||
const input = `
|
const input = `
|
||||||
function main() = (
|
function main() = (
|
||||||
let i = 0;
|
let i = 0;
|
||||||
loop break;
|
|
||||||
loop (
|
loop (
|
||||||
if i > 10 then break;
|
if i > 10 then break;
|
||||||
|
|
||||||
|
|
|
||||||
12
src/lower.ts
12
src/lower.ts
|
|
@ -577,10 +577,14 @@ function lowerExpr(fcx: FuncContext, instrs: wasm.Instr[], expr: Expr) {
|
||||||
fcx.currentBlockDepth += 2;
|
fcx.currentBlockDepth += 2;
|
||||||
const bodyInstrs: wasm.Instr[] = [];
|
const bodyInstrs: wasm.Instr[] = [];
|
||||||
lowerExpr(fcx, bodyInstrs, expr.body);
|
lowerExpr(fcx, bodyInstrs, expr.body);
|
||||||
|
|
||||||
|
// For diverging bodies, we don't need to bother creating the back edge.
|
||||||
|
if (expr.body.ty!.kind !== "never") {
|
||||||
bodyInstrs.push({
|
bodyInstrs.push({
|
||||||
kind: "br",
|
kind: "br",
|
||||||
label: /*innermost control structure, the loop*/ 0,
|
label: /*innermost control structure, the loop*/ 0,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
fcx.currentBlockDepth -= 2;
|
fcx.currentBlockDepth -= 2;
|
||||||
|
|
||||||
outerBlockInstrs.push({
|
outerBlockInstrs.push({
|
||||||
|
|
@ -633,11 +637,15 @@ function lowerExprBlockBody(
|
||||||
const headExprs = expr.exprs.slice(0, -1);
|
const headExprs = expr.exprs.slice(0, -1);
|
||||||
const tailExpr = expr.exprs[expr.exprs.length - 1];
|
const tailExpr = expr.exprs[expr.exprs.length - 1];
|
||||||
|
|
||||||
headExprs.forEach((inner) => {
|
for (const inner of headExprs) {
|
||||||
lowerExpr(fcx, innerInstrs, inner);
|
lowerExpr(fcx, innerInstrs, inner);
|
||||||
|
if (inner.ty!.kind === "never") {
|
||||||
|
// The rest of the block is unreachable, so we don't bother codegening it.
|
||||||
|
break;
|
||||||
|
}
|
||||||
const types = wasmTypeForBody(inner.ty!);
|
const types = wasmTypeForBody(inner.ty!);
|
||||||
types.forEach(() => innerInstrs.push({ kind: "drop" }));
|
types.forEach(() => innerInstrs.push({ kind: "drop" }));
|
||||||
});
|
}
|
||||||
|
|
||||||
lowerExpr(fcx, innerInstrs, tailExpr);
|
lowerExpr(fcx, innerInstrs, tailExpr);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue