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.

40 lines
1002 B

  1. import type { AbsInsn2 } from "./insn"
  2. import type { Loc } from "./loc"
  3. export class BasicBlock {
  4. insns: Array<AbsInsn2>
  5. labels: { [label: string]: number }
  6. constructor(insns: Array<AbsInsn2>) {
  7. this.insns = insns
  8. this.labels = this.insns
  9. .reduce((labels, insn, i) => {
  10. if (insn.type !== "label") {
  11. return labels
  12. }
  13. return {
  14. ...labels,
  15. [insn.dest]: i,
  16. }
  17. }, {})
  18. }
  19. locs(): Set<Loc> {
  20. return new Set(
  21. this.insns.flatMap(insn => {
  22. const insnLocs = []
  23. if (insn.type === "copy" || insn.type === "unary") {
  24. insnLocs.push(insn.dest)
  25. if (typeof insn.source !== "number") {
  26. insnLocs.push(insn.source)
  27. }
  28. }
  29. return insnLocs
  30. })
  31. )
  32. }
  33. }