import { expect } from "chai" import { edgeConnects } from "../lib/data/graph" import { Loc } from "../lib/ir/loc" import type { SSA } from "../lib/ir/ssa" import { allocateRegisters, interference, liveness } from "../lib/regalloc" 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: Array = [ { dest: x[0], source: 1 }, { dest: x[1], source: x[0], op: "add", source1: x[0] }, { dest: x[2], source: x[1], op: "add", source1: x[0] }, { dest: y[0], source: x[0], op: "add", source1: x[1] }, { dest: y[1], source: y[0], op: "add", source1: x[2] }, ] 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: Array = [ { dest: Loc.vari("a"), source: 7 }, { dest: Loc.vari("b"), source: 3 }, { dest: Loc.vari("x"), source: 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: Array = [ { dest: Loc.vari("a"), source: 7 }, { dest: Loc.vari("b"), source: 3 }, { dest: Loc.vari("x"), source: Loc.vari("a") } ] const alloc = allocateRegisters(block) expect(alloc.a).to.equal("A") expect(alloc.b).to.equal("B") expect(alloc.x).to.equal("A") }) })