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