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.

125 lines
3.4 KiB

  1. import { R8 } from "../sm83/cpu"
  2. import { Loc, LocType } from "../ir/loc"
  3. import type { Operand, SSA, SSABinary, SSACopy, SSAUnary } from "../ir/ssa"
  4. import type { RegAlloc } from "../regalloc"
  5. import { BinaryOp, UnaryOp } from "../ast"
  6. export const generateBlock = (alloc: RegAlloc, block: Array<SSA>): Array<string> => {
  7. const output: Array<string> = []
  8. block.forEach(ssa => {
  9. output.push(...generateSSA(alloc, ssa))
  10. })
  11. return output
  12. }
  13. export const generateSSA = (alloc: RegAlloc, ssa: SSA): Array<string> => {
  14. let output: Array<string> = []
  15. if ("op" in ssa) {
  16. output = generateSSA_Unary(alloc, ssa)
  17. } else {
  18. output = generateSSA_Copy(alloc, ssa)
  19. }
  20. return output
  21. }
  22. export const generateSSA_Copy = (alloc: RegAlloc, ssa: SSACopy): Array<string> => {
  23. const output: Array<string> = []
  24. const source = typeof ssa.source === 'number'
  25. ? ssa.source.toString()
  26. : getAlloc(alloc, ssa.source).name
  27. switch (ssa.dest.type) {
  28. case LocType.TEMPORARY:
  29. const dest = getAlloc(alloc, ssa.dest)
  30. // TODO: Remove later with peephole optimizer
  31. if (dest.name !== source) {
  32. output.push(`LD ${dest.name}, ${source}`)
  33. }
  34. break
  35. case LocType.REGISTER:
  36. if (isA(ssa.dest) && typeof ssa.source !== 'number' && ssa.source.type === LocType.VARIABLE) {
  37. output.push(`LD A, (${ssa.source.name})`)
  38. // TODO: Remove check with peephole optimizer
  39. } else if (source !== ssa.dest.name) {
  40. output.push(`LD ${ssa.dest.name}, ${source}`)
  41. }
  42. break
  43. case LocType.VARIABLE:
  44. if (source !== R8.A) {
  45. console.log(source, ssa)
  46. throw new Error("unsupported")
  47. }
  48. output.push(`LD (${ssa.dest.name}), A`)
  49. break
  50. default:
  51. throw new Error(`unsupported destination type \`${ssa.dest.type}'`)
  52. }
  53. return output
  54. }
  55. const getAlloc = (alloc: RegAlloc, loc: Loc): Loc => {
  56. const destAlloc = alloc[loc.toString()]
  57. if (typeof destAlloc === "object") {
  58. throw new Error("stack variables not yet supported")
  59. }
  60. return Loc.reg(destAlloc)
  61. }
  62. export const generateSSA_Unary = (alloc: RegAlloc, ssa: SSAUnary): Array<string> => {
  63. if (!isA(ssa.dest)) {
  64. throw new Error("Unexpected form for unary operation")
  65. }
  66. const source = typeof ssa.source === 'number'
  67. ? ssa.source.toString()
  68. : getAlloc(alloc, ssa.source).name
  69. switch (ssa.op) {
  70. case "add":
  71. return [`ADD ${source}`]
  72. case "arith_negate":
  73. return ["CPL", "INC A"]
  74. case "bit_negate":
  75. return ["CPL"]
  76. default:
  77. throw new Error(`unsupported unary op \`${ssa.op}'`)
  78. }
  79. }
  80. /*
  81. export const generateSSA_Binary = (alloc: RegAlloc, ssa: SSABinary): Array<string> => {
  82. if (!isA(ssa.dest) || !isA(ssa.source)) {
  83. throw new Error("Unexpected form for binary operation")
  84. }
  85. const output: Array<string> = []
  86. switch (ssa.op) {
  87. case "add":
  88. output.push(`ADD ${source}`)
  89. break
  90. default:
  91. throw new Error(`unsupported binary op \`${ssa.op}'`)
  92. }
  93. return output
  94. } */
  95. const isA = (loc: Operand): boolean =>
  96. typeof loc === "object" && loc.type === LocType.REGISTER && loc.name === R8.A