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.

45 lines
1.0 KiB

  1. import type { Loc } from "./loc"
  2. import type { Operand, SSA, SSAWithBinary } from "./ssa"
  3. export const prettyPrintBlock = (block: Array<SSA>): string => {
  4. return block.map(prettyPrintSSA).join("\n")
  5. }
  6. export const prettyPrintSSA = (ssa: SSAWithBinary): string => {
  7. let output = ""
  8. if ("source1" in ssa) {
  9. output = `${ppLoc(ssa.dest)} = ${ssa.op}(${ppOp(ssa.source)}, ${ppOp(ssa.source1)})`
  10. } else if ("op" in ssa) {
  11. output = `${ppLoc(ssa.dest)} = ${ssa.op}(${ppOp(ssa.source)})`
  12. } else {
  13. output = `${ppLoc(ssa.dest)} = ${ppOp(ssa.source)}`
  14. }
  15. return output
  16. }
  17. const ppOp = (op: Operand): string =>
  18. typeof op === "number"
  19. ? op.toString()
  20. : ppLoc(op)
  21. const ppLoc = (loc: Loc): string => {
  22. let output = ''
  23. switch (loc.type) {
  24. case "register":
  25. output = `%${loc.name}`
  26. break
  27. case "temporary":
  28. output = `#${loc.name}`
  29. break
  30. case "variable":
  31. output = loc.name
  32. break
  33. }
  34. return output
  35. }