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.

35 lines
991 B

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