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.
 
 

44 lines
1.3 KiB

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