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.

61 lines
2.1 KiB

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