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.

57 lines
1.2 KiB

  1. import type { BinaryOp, UnaryOp } from "../ast"
  2. import type { Loc } from "./loc"
  3. export type Operand = Loc | number
  4. export type AbsInsnCopy = {
  5. type: "copy",
  6. dest: Loc,
  7. source: Operand,
  8. }
  9. type UnaryInsn<Op> = {
  10. type: "unary",
  11. dest: Loc,
  12. op: Op,
  13. source: Operand,
  14. }
  15. // NOTE: We convert binary -> unary when going SSA3 -> SSA2
  16. export type AbsInsn2Unary = UnaryInsn<UnaryOp | BinaryOp>
  17. // Abstract instruction in two-address form
  18. export type AbsInsn2 = AbsInsnCopy | AbsInsn2Unary
  19. export type AbsInsn3Unary = UnaryInsn<UnaryOp>
  20. export type AbsInsnBinary = {
  21. type: "binary",
  22. dest: Loc,
  23. source: Operand,
  24. op: BinaryOp,
  25. source1: Operand
  26. }
  27. // Abstract instruction in three-address form
  28. export type AbsInsn3 = AbsInsnCopy | AbsInsn3Unary | AbsInsnBinary
  29. export const CopyInsn = (dest: Loc, source: Operand): AbsInsnCopy => ({
  30. type: 'copy',
  31. dest,
  32. source,
  33. })
  34. export const UnaryInsn = <Op>(dest: Loc, op: Op, source: Operand): UnaryInsn<Op> => ({
  35. type: 'unary',
  36. dest,
  37. op,
  38. source,
  39. })
  40. export const BinaryInsn = (dest: Loc, source: Operand, op: BinaryOp, source1: Operand): AbsInsnBinary => ({
  41. type: 'binary',
  42. dest,
  43. source,
  44. op,
  45. source1,
  46. })