Compare commits
No commits in common. "cc4ba445a7ccec87ff987a27008781c995afb045" and "ab48259a5a79121996e462c4b632a68de763cf15" have entirely different histories.
cc4ba445a7
...
ab48259a5a
@ -96,23 +96,6 @@ export type TyKind =
|
|||||||
| { tag: "Ptr" }
|
| { tag: "Ptr" }
|
||||||
| { tag: "Array"; ty: Ty; length: number };
|
| { tag: "Array"; ty: Ty; length: number };
|
||||||
|
|
||||||
export class Builder {
|
|
||||||
constructor(
|
|
||||||
private bb: BasicBlock,
|
|
||||||
private cx: Context,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
createInt(ty: Ty, value: number): Inst {
|
|
||||||
return this.push(ty, { tag: "Int", value });
|
|
||||||
}
|
|
||||||
|
|
||||||
private push(ty: Ty, kind: InstKind): Inst {
|
|
||||||
const inst = this.cx.createInst(this.bb, ty, kind);
|
|
||||||
this.bb.insts.push(inst);
|
|
||||||
return inst;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class Context {
|
export class Context {
|
||||||
private tys = new Map<string, Ty>();
|
private tys = new Map<string, Ty>();
|
||||||
private instsHashIds = new Map<Inst, number>();
|
private instsHashIds = new Map<Inst, number>();
|
||||||
@ -236,3 +219,18 @@ export class Context {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class Builder {
|
||||||
|
constructor(
|
||||||
|
private bb: BasicBlock,
|
||||||
|
private cx: Context,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
createInt(ty: Ty, value: number) {
|
||||||
|
this.push(ty, { tag: "Int", value });
|
||||||
|
}
|
||||||
|
|
||||||
|
private push(ty: Ty, kind: InstKind) {
|
||||||
|
this.bb.insts.push(this.cx.createInst(this.bb, ty, kind));
|
||||||
|
}
|
||||||
|
}
|
||||||
1
src/middle/mod.ts
Normal file
1
src/middle/mod.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * as lir from "./lir.ts";
|
||||||
@ -1,94 +0,0 @@
|
|||||||
import * as mir from "./mir.ts";
|
|
||||||
import * as lir from "./lir.ts";
|
|
||||||
import { Ty } from "./ty.ts";
|
|
||||||
|
|
||||||
export class MirModuleLowerer {
|
|
||||||
private cx = new lir.Context();
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
private mod: lir.Mod,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
lowerFn(mirFn: mir.Fn) {
|
|
||||||
const lirFn = new lir.Fn(this.mod);
|
|
||||||
const fnLowerer = new MirFnLowerer(mirFn, lirFn, this.cx);
|
|
||||||
fnLowerer.lower();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class MirFnLowerer {
|
|
||||||
private insts = new Map<mir.Inst, lir.Inst>();
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
private mirFn: mir.Fn,
|
|
||||||
private lirFn: lir.Fn,
|
|
||||||
private cx: lir.Context,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
lower() {
|
|
||||||
for (const mirBb of this.mirFn.bbs) {
|
|
||||||
const lirBb = new lir.BasicBlock(this.lirFn);
|
|
||||||
const builder = new lir.Builder(lirBb, this.cx);
|
|
||||||
for (const mirInst of mirBb.insts) {
|
|
||||||
const lirInst = this.lowerInst(mirInst, builder);
|
|
||||||
this.insts.set(mirInst, lirInst);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private lowerInst(inst: mir.Inst, builder: lir.Builder): lir.Inst {
|
|
||||||
switch (inst.kind.tag) {
|
|
||||||
case "Error":
|
|
||||||
throw new Error();
|
|
||||||
case "Void":
|
|
||||||
break;
|
|
||||||
case "Int": {
|
|
||||||
const ty = this.lowerTy(inst.ty);
|
|
||||||
return builder.createInt(ty, inst.kind.value);
|
|
||||||
}
|
|
||||||
case "Bool":
|
|
||||||
case "Str":
|
|
||||||
case "Array":
|
|
||||||
case "Fn":
|
|
||||||
case "Param":
|
|
||||||
case "GetElemPtr":
|
|
||||||
case "Slice":
|
|
||||||
case "Call":
|
|
||||||
case "Alloca":
|
|
||||||
case "Load":
|
|
||||||
case "Store":
|
|
||||||
case "Jump":
|
|
||||||
case "Branch":
|
|
||||||
case "Return":
|
|
||||||
case "Not":
|
|
||||||
case "Negate":
|
|
||||||
case "Eq":
|
|
||||||
case "Ne":
|
|
||||||
case "Lt":
|
|
||||||
case "Gt":
|
|
||||||
case "Lte":
|
|
||||||
case "Gte":
|
|
||||||
case "BitOr":
|
|
||||||
case "BitXor":
|
|
||||||
case "BitAnd":
|
|
||||||
case "Shl":
|
|
||||||
case "Shr":
|
|
||||||
case "Add":
|
|
||||||
case "Sub":
|
|
||||||
case "Mul":
|
|
||||||
case "Div":
|
|
||||||
case "Rem":
|
|
||||||
case "Len":
|
|
||||||
case "DebugPrint":
|
|
||||||
throw new Error(`not handled (${inst.kind.tag})`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private lowerTy(ty: Ty): lir.Ty {
|
|
||||||
switch (ty.kind.tag) {
|
|
||||||
default:
|
|
||||||
throw new Error(`not handled (${ty.kind.tag})`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user