import { expect } from "chai"
|
|
|
|
import { edgeConnects } from "../lib/data/graph"
|
|
import { Loc } from "../lib/ir/loc"
|
|
import { AbsInsn2, CopyInsn, UnaryInsn } from "../lib/ir/insn"
|
|
import { allocateRegisters, interference, liveness } from "../lib/regalloc"
|
|
import { R8 } from "../lib/sm83/cpu"
|
|
import { BasicBlock } from "../lib/ir/block"
|
|
|
|
describe("liveness", () => {
|
|
it("computes liveness", () => {
|
|
const x = [0, 1, 2].map(i => Loc.vari("x" + i))
|
|
const y = [0, 1].map(i => Loc.vari("y" + i))
|
|
const block = new BasicBlock([
|
|
CopyInsn(x[0], 1),
|
|
UnaryInsn(x[1], "add", x[0]),
|
|
UnaryInsn(x[2], "add", x[1]),
|
|
UnaryInsn(y[0], "add", x[0]),
|
|
UnaryInsn(y[1], "add", y[0]),
|
|
])
|
|
|
|
const info = liveness(block)
|
|
/* expect(info[0]).to.deep.equal(new Set())
|
|
expect(info[1]).to.deep.equal(new Set([x[0]]))
|
|
expect(info[2]).to.deep.equal(new Set([x[0], x[1]]))
|
|
expect(info[3]).to.deep.equal(new Set([x[0], x[1], x[2]]))
|
|
expect(info[4]).to.deep.equal(new Set([y[0], x[2]])) */
|
|
})
|
|
})
|
|
|
|
describe("interference", () => {
|
|
it("computes interference", () => {
|
|
const block: BasicBlock = new BasicBlock([
|
|
CopyInsn(Loc.vari("a"), 7),
|
|
CopyInsn(Loc.vari("b"), 3),
|
|
CopyInsn(Loc.vari("x"), Loc.vari("a")),
|
|
])
|
|
|
|
const info = liveness(block)
|
|
const g = interference(block, info)
|
|
|
|
expect(edgeConnects(g, "a", "b")).to.be.true
|
|
})
|
|
})
|
|
|
|
describe("allocateRegisters", () => {
|
|
it("allocates registers", () => {
|
|
const block: BasicBlock = new BasicBlock([
|
|
CopyInsn(Loc.vari("a"), 7),
|
|
CopyInsn(Loc.vari("b"), 3),
|
|
CopyInsn(Loc.vari("x"), Loc.vari("a")),
|
|
])
|
|
|
|
const alloc = allocateRegisters(block, Object.values(R8))
|
|
|
|
expect(alloc.a).to.equal("A")
|
|
expect(alloc.b).to.equal("B")
|
|
expect(alloc.x).to.equal("A")
|
|
})
|
|
})
|