import type { Loc } from "./loc"
|
|
import type { Operand, SSA } from "./ssa"
|
|
|
|
export const prettyPrintBlock = (block: Array<SSA>): string => {
|
|
return block.map(prettyPrintSSA).join("\n")
|
|
}
|
|
|
|
export const prettyPrintSSA = (ssa: SSA): 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
|
|
}
|