/* -- Time-stamp: -- $Id: PPIntm.java,v 1.2 2005/12/06 12:37:52 hwloidl Exp $ -- -- A Compiler for MiniJava: -- Pretty printer for intermediate code -- Practical on compiler design, LMU 2005/6 -- Based on Appels book on "Modern Compiler Implementation" -- --------------------------------------------------------------------------- */ class PPIntm implements IntmVisitor { // ----------------------------------------------------------------------- // visitor for pretty printing the intermediate code public String visit(CONST c) { return c.value.toString(); } public String visit (NAME c) { return c.label.toString(); } public String visit (TEMP c) { return c.temp.toString(); } public String visit (BINOP c) { String lhs = c.left.accept(this); String rhs = c.right.accept(this); String op; switch (c.op) { case BINOP.PLUS: op = "+"; break; case BINOP.MINUS: op = "-"; break; case BINOP.MUL: op = "*"; break; case BINOP.DIV: op = "/"; break; case BINOP.AND: op = "&&"; break; case BINOP.OR: op = "||"; break; case BINOP.LSHIFT: op = "<<"; break; case BINOP.RSHIFT: op = ">>"; break; case BINOP.ARSHIFT: op = ">|"; break; case BINOP.XOR: op = "^^"; break; default: op = "??"; } return "("+lhs+op+rhs+")"; } public String visit(MEM c){ return "MEM("+c.addr.accept(this)+")"; } public String visit(CALL c){ return ""+c.func.accept(this)+"("+c.args.accept(new PPIntmExpList())+")"; } public String visit(ESEQ c){ String s = "{ "+c.stm.accept(this)+","+c.exp.accept(this)+" }"; return s; } public String visit(MOVE c) { return c.dest.accept(this)+" <- "+c.src.accept(this); } public String visit(EXP c){ return c.exp.accept(this); } public String visit(JUMP c){ return "JUMP "+c.dest.accept(this); } public String visit(CJUMP c){ String lhs = c.left.accept(this); String rhs = c.right.accept(this); String op; switch (c.rel) { case CJUMP.NE: op = "!="; break; case CJUMP.LT: op = "<"; break; default: op= "??"; } return "CJUMP (" + lhs + op + rhs +") ? "+c.trueLab.toString()+" : "+c.falseLab.toString(); } public String visit(SEQ c){ return c.first.accept(this)+"\n"+c.second.accept(this); } public String visit(LABEL c){ return c.label.toString()+":"; } public String visit(Exp c) { if (c instanceof CONST) { return visit((CONST)c); } else if (c instanceof NAME) { return visit((NAME)c); } else if (c instanceof TEMP) { return visit((TEMP)c); } else if (c instanceof BINOP) { return visit((BINOP)c); } else if (c instanceof MEM) { return visit((MEM)c); } else if (c instanceof CALL) { return visit((CALL)c); } else if (c instanceof ESEQ) { return visit((ESEQ)c); } else { System.out.println("PANIC: illegal instance for "+c.toString()); } return "ERROR: illegal instance for"+c.toString(); } public String visit(Stm c) { if (c instanceof MOVE) { return visit((MOVE)c); } if (c instanceof EXP) { return visit((EXP)c); } else if (c instanceof JUMP) { return visit((JUMP)c); } else if (c instanceof CJUMP) { return visit((CJUMP)c); } else if (c instanceof SEQ) { return visit ((SEQ)c); } else if (c instanceof LABEL) { return visit ((LABEL)c); } else { System.out.println("PANIC: illegal instance for"+c.toString()); } return "ERROR: illegal instance for "+c.toString(); } } class PPIntmExpList implements ListVisitor { public String visit(NIL x) { return ""; } public String visit(CONS x) { return x.hd.accept(new PPIntm())+", "+x.tl.accept(this); } }