This commit is contained in:
parent
35cb81e266
commit
ab48259a5a
@ -1,4 +1,5 @@
|
|||||||
import * as mir from "../../mir.ts";
|
import * as mir from "../../mir.ts";
|
||||||
|
import { Ty } from "../../ty.ts";
|
||||||
import { Block, Fn, Inst, Reg, Regs } from "./module.ts";
|
import { Block, Fn, Inst, Reg, Regs } from "./module.ts";
|
||||||
|
|
||||||
export function selectFnInstructions(fn: mir.Fn): Fn {
|
export function selectFnInstructions(fn: mir.Fn): Fn {
|
||||||
@ -74,30 +75,11 @@ class InstructionSelector {
|
|||||||
};
|
};
|
||||||
|
|
||||||
this.fn.visit({
|
this.fn.visit({
|
||||||
visitInst(inst) {
|
visitInst: (inst) => {
|
||||||
if (inst.kind.tag === "Alloca") {
|
if (inst.kind.tag === "Alloca") {
|
||||||
const ty = inst.ty.as("PtrMut").kind.ty;
|
const ty = inst.ty.as("PtrMut").kind.ty;
|
||||||
switch (ty.kind.tag) {
|
const [size, align] = this.tySizeAlign(ty);
|
||||||
case "Int": {
|
pushLocal(inst, size, align);
|
||||||
switch (ty.kind.intTy) {
|
|
||||||
case "i32":
|
|
||||||
pushLocal(inst, 4, 4);
|
|
||||||
break;
|
|
||||||
case "i64":
|
|
||||||
pushLocal(inst, 8, 8);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new Error(
|
|
||||||
`not implemented (${ty.kind.intTy})`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
throw new Error(
|
|
||||||
`not implemented (${ty.kind.tag})`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@ -109,6 +91,40 @@ class InstructionSelector {
|
|||||||
this.localsSize = -offset;
|
this.localsSize = -offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private tySizeAlign(ty: Ty): [number, number] {
|
||||||
|
switch (ty.kind.tag) {
|
||||||
|
case "Int": {
|
||||||
|
switch (ty.kind.intTy) {
|
||||||
|
case "i32":
|
||||||
|
return [4, 4];
|
||||||
|
case "i64":
|
||||||
|
return [8, 8];
|
||||||
|
case "u8":
|
||||||
|
return [1, 1];
|
||||||
|
default:
|
||||||
|
throw new Error(
|
||||||
|
`not implemented (${ty.kind.intTy})`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "Bool": {
|
||||||
|
return [1, 1];
|
||||||
|
}
|
||||||
|
case "Ptr":
|
||||||
|
case "PtrMut": {
|
||||||
|
return [8, 8];
|
||||||
|
}
|
||||||
|
case "Array": {
|
||||||
|
const [innerSize, align] = this.tySizeAlign(ty.kind.ty);
|
||||||
|
return [innerSize * ty.kind.length, align];
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
throw new Error(
|
||||||
|
`not implemented (${ty.kind.tag})`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private generateBasicBlock(bb: mir.BasicBlock): Block {
|
private generateBasicBlock(bb: mir.BasicBlock): Block {
|
||||||
const regs = new Map<mir.Inst, Reg>();
|
const regs = new Map<mir.Inst, Reg>();
|
||||||
|
|
||||||
|
|||||||
@ -51,10 +51,11 @@ if (Deno.args.includes("--print-mir")) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const result = selectFnInstructions(mainMiddleFn);
|
const result = selectFnInstructions(mainMiddleFn);
|
||||||
stringify.printWithConsoleColors(
|
if (Deno.args.includes("--print-isel")) {
|
||||||
stringify.x86_64FnPretty(result, stringify.consoleColors),
|
stringify.printWithConsoleColors(
|
||||||
);
|
stringify.x86_64FnPretty(result, stringify.consoleColors),
|
||||||
// console.log(JSON.stringify(result, null, 4));
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (!reporter.ok()) {
|
if (!reporter.ok()) {
|
||||||
reporter.printAll();
|
reporter.printAll();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user