A "high-level" language for the Gameboy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.9 KiB

  1. import { expect } from "chai"
  2. import { edgeConnects } from "../lib/data/graph"
  3. import { Loc } from "../lib/ir/loc"
  4. import { AbsInsn2, CopyInsn, UnaryInsn } from "../lib/ir/insn"
  5. import { allocateRegisters, interference, liveness } from "../lib/regalloc"
  6. import { R8 } from "../lib/sm83/cpu"
  7. import { BasicBlock } from "../lib/ir/block"
  8. describe("liveness", () => {
  9. it("computes liveness", () => {
  10. const x = [0, 1, 2].map(i => Loc.vari("x" + i))
  11. const y = [0, 1].map(i => Loc.vari("y" + i))
  12. const block = new BasicBlock([
  13. CopyInsn(x[0], 1),
  14. UnaryInsn(x[1], "add", x[0]),
  15. UnaryInsn(x[2], "add", x[1]),
  16. UnaryInsn(y[0], "add", x[0]),
  17. UnaryInsn(y[1], "add", y[0]),
  18. ])
  19. const info = liveness(block)
  20. /* expect(info[0]).to.deep.equal(new Set())
  21. expect(info[1]).to.deep.equal(new Set([x[0]]))
  22. expect(info[2]).to.deep.equal(new Set([x[0], x[1]]))
  23. expect(info[3]).to.deep.equal(new Set([x[0], x[1], x[2]]))
  24. expect(info[4]).to.deep.equal(new Set([y[0], x[2]])) */
  25. })
  26. })
  27. describe("interference", () => {
  28. it("computes interference", () => {
  29. const block: BasicBlock = new BasicBlock([
  30. CopyInsn(Loc.vari("a"), 7),
  31. CopyInsn(Loc.vari("b"), 3),
  32. CopyInsn(Loc.vari("x"), Loc.vari("a")),
  33. ])
  34. const info = liveness(block)
  35. const g = interference(block, info)
  36. expect(edgeConnects(g, "a", "b")).to.be.true
  37. })
  38. })
  39. describe("allocateRegisters", () => {
  40. it("allocates registers", () => {
  41. const block: BasicBlock = new BasicBlock([
  42. CopyInsn(Loc.vari("a"), 7),
  43. CopyInsn(Loc.vari("b"), 3),
  44. CopyInsn(Loc.vari("x"), Loc.vari("a")),
  45. ])
  46. const alloc = allocateRegisters(block, Object.values(R8))
  47. expect(alloc.a).to.equal("A")
  48. expect(alloc.b).to.equal("B")
  49. expect(alloc.x).to.equal("A")
  50. })
  51. })