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.
 
 

45 lines
1.1 KiB

import type { Loc } from "./loc"
import type { AbsInsn2, AbsInsn3, Operand } from "./insn"
export const prettyPrintBlock = <A extends AbsInsn2 | AbsInsn3>(block: Array<A>): string => {
return block.map(prettyPrintInsn).join("\n")
}
export const prettyPrintInsn = (ssa: AbsInsn2 | AbsInsn3): string => {
let output = ""
if ("source1" in ssa) {
output = `${ppLoc(ssa.dest)} = ${ssa.op}(${ppOp(ssa.source)}, ${ppOp(ssa.source1)})`
} else if ("op" in ssa) {
output = `${ppLoc(ssa.dest)} = ${ssa.op}(${ppOp(ssa.source)})`
} else {
output = `${ppLoc(ssa.dest)} = ${ppOp(ssa.source)}`
}
return output
}
const ppOp = (op: Operand): string =>
typeof op === "number"
? op.toString()
: ppLoc(op)
const ppLoc = (loc: Loc): string => {
let output = ''
switch (loc.type) {
case "register":
output = `%${loc.name}`
break
case "temporary":
output = `#${loc.name}`
break
case "variable":
output = loc.name
break
}
return output
}