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.0 KiB

  1. import { expect } from "chai"
  2. import { edgeConnects } from "../lib/data/graph"
  3. import { Loc } from "../lib/ir/loc"
  4. import type { SSA } from "../lib/ir/ssa"
  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<SSA> = [
  12. { dest: x[0], source: 1 },
  13. { dest: x[1], source: x[0], op: "add", source1: x[0] },
  14. { dest: x[2], source: x[1], op: "add", source1: x[0] },
  15. { dest: y[0], source: x[0], op: "add", source1: x[1] },
  16. { dest: y[1], source: y[0], op: "add", source1: x[2] },
  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<SSA> = [
  29. { dest: Loc.vari("a"), source: 7 },
  30. { dest: Loc.vari("b"), source: 3 },
  31. { 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<SSA> = [
  41. { dest: Loc.vari("a"), source: 7 },
  42. { dest: Loc.vari("b"), source: 3 },
  43. { 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. })