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.

63 lines
1.6 KiB

  1. import { createGraph, Graph } from "./data/graph"
  2. import type { Loc } from "./ir/loc"
  3. import type { SSA } from "./ir/ssa"
  4. export type LivenessInfo = Array<Set<Loc>>
  5. export const liveness = (block: Array<SSA>): LivenessInfo => {
  6. const info: LivenessInfo = []
  7. info[block.length] = new Set()
  8. for (let i = block.length - 1; i >= 0; --i) {
  9. const insn = block[i]
  10. const last = info[i + 1]
  11. info[i] = new Set()
  12. if (typeof insn.source !== "number") {
  13. info[i].add(insn.source)
  14. }
  15. if ("source1" in insn && typeof insn.source1 !== "number") {
  16. info[i].add(insn.source1)
  17. }
  18. last.forEach(loc => {
  19. if (loc.toString() === insn.dest.toString()) {
  20. return
  21. }
  22. info[i].add(loc)
  23. })
  24. }
  25. return info
  26. }
  27. export const locations = (block: Array<SSA>): Set<Loc> => {
  28. const ls: Set<Loc> = new Set()
  29. block.forEach(ssa => {
  30. if ("source1" in ssa && typeof ssa.source1 !== "number") {
  31. ls.add(ssa.source1)
  32. } else if (typeof ssa.source !== "number") {
  33. ls.add(ssa.source)
  34. }
  35. })
  36. return ls
  37. }
  38. export const interference = (block: Array<SSA>, live: LivenessInfo): Graph<Loc> =>
  39. createGraph((v, e) => {
  40. block.forEach((ssa, i) =>
  41. live[i + 1].forEach(u => {
  42. if (ssa.dest.toString() !== u.toString()) {
  43. v(ssa.dest.toString(), ssa.dest)
  44. v(u.toString(), u)
  45. e(ssa.dest.toString(), u.toString())
  46. }
  47. })
  48. )
  49. })