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.

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