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.

100 lines
3.1 KiB

  1. import type { AssignStmt, Expr, Stmt } from "../ast"
  2. import { R8 } from "../sm83/cpu"
  3. import { Loc, LocType } from "./loc"
  4. import { Operand, AbsInsn2, AbsInsn3, CopyInsn, UnaryInsn, BinaryInsn } from "./insn"
  5. export class IRState {
  6. nextID: number
  7. insns: Array<AbsInsn2>
  8. constructor() {
  9. this.nextID = 0
  10. this.insns = []
  11. }
  12. temp(): Loc {
  13. return Loc.temp(`t${this.nextID++}`)
  14. }
  15. }
  16. export const convertIR = (stmts: Array<Stmt>): Array<AbsInsn2> => {
  17. const state: IRState = new IRState()
  18. stmts.forEach(stmt => {
  19. convertAssignStmt(state, stmt)
  20. })
  21. return state.insns
  22. }
  23. export const convertAssignStmt = (state: IRState, stmt: AssignStmt): void => {
  24. convertExpr(state, Loc.vari(stmt.args.name), stmt.args.expr)
  25. }
  26. export const convertExpr = (state: IRState, dest: Loc, expr: Expr): void => {
  27. const threeAddress = convertExpr3(state, dest, expr)
  28. threeAddress.forEach(ssa => {
  29. switch (ssa.type) {
  30. case 'copy':
  31. state.insns.push(ssa)
  32. break
  33. case 'unary':
  34. state.insns.push(
  35. CopyInsn(Loc.reg(R8.A), ssa.source),
  36. UnaryInsn(Loc.reg(R8.A), ssa.op, Loc.reg(R8.A)),
  37. CopyInsn(ssa.dest, Loc.reg(R8.A)),
  38. )
  39. break
  40. case 'binary':
  41. let source1 = ssa.source1
  42. if (typeof source1 !== 'number' && source1.type === LocType.VARIABLE) {
  43. source1 = state.temp()
  44. state.insns.push(
  45. CopyInsn(Loc.reg(R8.A), ssa.source1),
  46. CopyInsn(source1, Loc.reg(R8.A)),
  47. )
  48. }
  49. state.insns.push(
  50. CopyInsn(Loc.reg(R8.A), ssa.source),
  51. UnaryInsn(Loc.reg(R8.A), ssa.op, source1),
  52. CopyInsn(ssa.dest, Loc.reg(R8.A)),
  53. )
  54. break
  55. }
  56. })
  57. }
  58. export const convertExpr3 = (state: IRState, dest: Loc, expr: Expr): Array<AbsInsn3> => {
  59. let expr_stmts: Array<AbsInsn3> = []
  60. const getSource = (expr: Expr): [Operand, Array<AbsInsn3>] => {
  61. if (typeof expr === "number") {
  62. return [expr, []]
  63. } else if (typeof expr === "string") {
  64. return [Loc.vari(expr), []]
  65. }
  66. const source = state.temp()
  67. const stmts = convertExpr3(state, source, expr)
  68. return [source, stmts]
  69. }
  70. if (typeof expr === "number") {
  71. expr_stmts.push(CopyInsn(dest, expr))
  72. } else if (typeof expr === "string") {
  73. expr_stmts.push(CopyInsn(dest, Loc.vari(expr)))
  74. } else if (expr.type === "unary") {
  75. const [source, stmts] = getSource(expr.arg)
  76. stmts.push(UnaryInsn(dest, expr.op, source))
  77. expr_stmts = stmts
  78. } else {
  79. const [left_source, left_stmts] = getSource(expr.left)
  80. const [right_source, right_stmts] = getSource(expr.right)
  81. expr_stmts = [...left_stmts, ...right_stmts, BinaryInsn(dest, left_source, expr.op, right_source)]
  82. }
  83. return expr_stmts
  84. }