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.

117 lines
2.9 KiB

  1. import { convertIR } from "./ir/convert"
  2. import { prettyPrintBlock } from "./ir/pretty"
  3. import { parse } from "./parser"
  4. import { allocateRegisters } from "./regalloc"
  5. import { realizeBlock } from "./sm83/realize"
  6. import { R8 } from "./sm83/cpu"
  7. import { allocate } from "./codegen/allocate"
  8. import type { Attr, Decl, Stmt, Type, VarDecl } from "./ast"
  9. type SymbolDefn = {
  10. attrs: Array<Attr>,
  11. name: string,
  12. type: Type
  13. }
  14. type SymbolMap = {
  15. [name: string]: SymbolDefn
  16. }
  17. // TODO: Support more than one TU
  18. export const compile = (fileName: string, source: string): string => {
  19. // 1. Parse
  20. const ast = parse(source)
  21. // 2. Partition declarations and statements
  22. const decls = ast.filter((x): x is Decl => x.type == "decl")
  23. const stmts = ast.filter((x): x is Stmt => x.type == "stmt")
  24. // 3. Create top-level symbol map
  25. const symbols = processDecls(decls)
  26. // TODO: Support declaring types other than U8/S8
  27. const asmDecls = Object.values(symbols).map(symbol => `${symbol.name}:: DB`)
  28. // TODO: Some form of type-checking
  29. // 4. Generate IR
  30. const ir = convertIR(stmts)
  31. console.log("=== IR === ")
  32. console.log(prettyPrintBlock(ir))
  33. // 5. Generate code
  34. const alloc = allocateRegisters(ir, Object.values(R8))
  35. const insns = realizeBlock(allocate(alloc, ir))
  36. let output = ''
  37. if (asmDecls.length > 0) {
  38. output += `SECTION "${fileName} Data", WRAM0\n\n`
  39. output += asmDecls.join("\n")
  40. output += "\n\n"
  41. }
  42. if (insns.length > 0) {
  43. output += `SECTION "${fileName} Code", ROM0\n\n`
  44. output += insns.join("\n")
  45. }
  46. console.log("=== ASM ===")
  47. return output
  48. }
  49. export const processDecls = (decls: Array<Decl>): SymbolMap => decls.reduce(processDecl, {})
  50. export const processDecl = (symbols: SymbolMap, decl: Decl): SymbolMap => {
  51. const symbol = processVarDecl(symbols, decl)
  52. // Don't declare extern symbols
  53. if (hasAttr(symbol, { name: 'extern', args: [] })) {
  54. return symbols
  55. }
  56. return {
  57. ...symbols,
  58. [symbol.name]: symbol,
  59. }
  60. }
  61. export const processVarDecl = (symbols: SymbolMap, decl: VarDecl): SymbolDefn => {
  62. if (typeof symbols[decl.name] !== 'undefined') {
  63. throw new Error(`a variable named \`${decl.name}' is already defined`)
  64. }
  65. return {
  66. attrs: decl.attrs,
  67. name: decl.name,
  68. type: decl.args.type,
  69. }
  70. }
  71. export const hasAttr = (symbol: SymbolDefn, attr: Attr) => {
  72. let has = false
  73. for (let i = 0; i < symbol.attrs.length; ++i) {
  74. has = attrEquals(symbol.attrs[i], attr)
  75. if (has) {
  76. break
  77. }
  78. }
  79. return has
  80. }
  81. const attrEquals = (attr1: Attr, attr2: Attr): boolean => {
  82. if (attr1.name !== attr2.name || attr1.args.length !== attr2.args.length) {
  83. return false
  84. }
  85. for (let i = 0; i < attr1.args.length; ++i) {
  86. if (attr1.args[i] !== attr2.args[i]) {
  87. return false
  88. }
  89. }
  90. return true
  91. }