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.

44 lines
1.3 KiB

  1. import type { BasicBlock } from "./block"
  2. import type { AbsInsn2, AbsInsn3, Operand } from "./insn"
  3. import type { Loc } from "./loc"
  4. export const prettyPrintBlock = (block: BasicBlock): string => prettyPrintInsns(block.insns)
  5. export const prettyPrintInsns = <A extends AbsInsn2 | AbsInsn3>(block: Array<A>): string => {
  6. return block.map(prettyPrintInsn).join("\n")
  7. }
  8. export const prettyPrintInsn = (ssa: AbsInsn2 | AbsInsn3): string => {
  9. let output = ""
  10. switch (ssa.type) {
  11. case 'copy':
  12. output = `${ppLoc(ssa.dest)} = ${ppOp(ssa.source)}`
  13. break
  14. case 'unary':
  15. output = `${ppLoc(ssa.dest)} = ${ssa.op}(${ppOp(ssa.source)})`
  16. break
  17. case 'binary':
  18. output = `${ppLoc(ssa.dest)} = ${ssa.op}(${ppOp(ssa.source)}, ${ppOp(ssa.source1)})`
  19. break
  20. case 'goto':
  21. output = `goto(${ssa.dest})`
  22. break
  23. case 'label':
  24. output = `${ssa.dest}:`
  25. break
  26. }
  27. return output
  28. }
  29. const ppOp = (op: Operand): string =>
  30. typeof op === "number"
  31. ? op.toString()
  32. : ppLoc(op)
  33. const ppLoc = (loc: Loc): string => ({
  34. 'register': `%${loc.name}`,
  35. 'temporary': `#${loc.name}`,
  36. 'variable': loc.name,
  37. }[loc.type])