Add assignments

This commit is contained in:
nora 2023-07-30 20:44:59 +02:00
parent 6d2a2fe474
commit 0bf9aed35e
6 changed files with 113 additions and 32 deletions

View file

@ -272,6 +272,36 @@ function lowerExpr(fcx: FuncContext, instrs: wasm.Instr[], expr: Expr) {
break;
}
case "assign": {
lowerExpr(fcx, instrs, expr.rhs);
const { lhs } = expr;
switch (lhs.kind) {
case "ident": {
const res = lhs.value.res!;
switch (res.kind) {
case "local": {
const location =
fcx.varLocations[fcx.varLocations.length - 1 - res.index];
storeVariable(instrs, location);
break;
}
case "item": {
throw new Error("cannot store to item");
}
case "builtin": {
throw new Error("cannot store to builtin");
}
}
break;
}
default: {
throw new Error("invalid lhs side of assignment");
}
}
break;
}
case "block": {
const prevVarLocationLengths = fcx.varLocations.length;
@ -610,6 +640,15 @@ function loadVariable(instrs: wasm.Instr[], loc: VarLocation) {
});
}
function storeVariable(instrs: wasm.Instr[], loc: VarLocation) {
// Stores are just like loads, just the other way around.
const types = loc.types.map((_, i) => i);
types.reverse();
types.forEach((i) => {
instrs.push({ kind: "local.set", imm: loc.localIdx + i });
});
}
function computeAbi(ty: TyFn): FnAbi {
function argRetAbi(param: Ty): ArgRetAbi {
switch (param.kind) {