Compare commits
2 Commits
ab48259a5a
...
cc4ba445a7
| Author | SHA1 | Date | |
|---|---|---|---|
| cc4ba445a7 | |||
| 4a8f6f32ea |
@ -96,6 +96,23 @@ 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>();
|
||||||
@ -219,18 +236,3 @@ 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 +0,0 @@
|
|||||||
export * as lir from "./lir.ts";
|
|
||||||
94
src/mir_lower.ts
Normal file
94
src/mir_lower.ts
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
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