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.

133 lines
3.7 KiB

  1. import { expect } from "chai"
  2. import { convertAssignStmt } from "../../lib/ir/convert"
  3. import { Loc } from "../../lib/ir/loc"
  4. describe("ir", () => {
  5. describe("convertExpr", () => {
  6. it("load with immediate", () => {
  7. const state = { nextID: 0, ssa_stmts: [] }
  8. const ir = convertAssignStmt(state, {
  9. type: "stmt",
  10. stmt_type: "assign",
  11. args: {
  12. name: "x",
  13. expr: 42,
  14. },
  15. })
  16. expect(ir).to.deep.equal([
  17. {
  18. dest: Loc.vari("x"),
  19. source: 42,
  20. },
  21. ])
  22. })
  23. it("load with variable", () => {
  24. const state = { nextID: 0, ssa_stmts: [] }
  25. const ir = convertAssignStmt(state, {
  26. type: "stmt",
  27. stmt_type: "assign",
  28. args: {
  29. name: "x",
  30. expr: "y",
  31. },
  32. })
  33. expect(ir).to.deep.equal([
  34. {
  35. dest: Loc.vari("x"),
  36. source: Loc.vari("y"),
  37. },
  38. ])
  39. })
  40. it("unary op", () => {
  41. const state = { nextID: 0, ssa_stmts: [] }
  42. const ir = convertAssignStmt(state, {
  43. type: "stmt",
  44. stmt_type: "assign",
  45. args: {
  46. name: "x",
  47. expr: {
  48. type: "unary",
  49. op: "arith_negate",
  50. arg: "y"
  51. },
  52. },
  53. })
  54. expect(ir).to.deep.equal([
  55. {
  56. dest: Loc.vari("x"),
  57. source: Loc.vari("y"),
  58. op: "arith_negate",
  59. },
  60. ])
  61. })
  62. it("binary op", () => {
  63. const state = { nextID: 0, ssa_stmts: [] }
  64. const ir = convertAssignStmt(state, {
  65. type: "stmt",
  66. stmt_type: "assign",
  67. args: {
  68. name: "x",
  69. expr: {
  70. type: "binary",
  71. op: "add",
  72. left: "y",
  73. right: 42,
  74. },
  75. },
  76. })
  77. expect(ir).to.deep.equal([
  78. {
  79. dest: Loc.vari("x"),
  80. source: Loc.vari("y"),
  81. source1: 42,
  82. op: "add",
  83. },
  84. ])
  85. })
  86. it("nested binary op", () => {
  87. const state = { nextID: 0, ssa_stmts: [] }
  88. const ir = convertAssignStmt(state, {
  89. type: "stmt",
  90. stmt_type: "assign",
  91. args: {
  92. name: "x",
  93. expr: {
  94. type: "binary",
  95. op: "add",
  96. left: "y",
  97. right: {
  98. type: "binary",
  99. op: "subtract",
  100. left: "z",
  101. right: 42,
  102. },
  103. },
  104. },
  105. })
  106. expect(ir).to.deep.equal([
  107. {
  108. dest: Loc.temp("t0"),
  109. source: Loc.vari("z"),
  110. op: "subtract",
  111. source1: 42,
  112. },
  113. {
  114. dest: Loc.vari("x"),
  115. source: Loc.vari("y"),
  116. op: "add",
  117. source1: Loc.temp("t0"),
  118. },
  119. ])
  120. })
  121. })
  122. })