import type { Loc } from "./loc" import type { AbsInsn2, AbsInsn3, Operand } from "./insn" export const prettyPrintBlock = (block: Array): 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 } 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])