// // Muncher.java: applies maximal munch to generate code for Intel processors // import Tree.*; import java.util.*; public class Muncher { public void munchMove(MEM dst, Exp src) { // 1. MOVE(MEM(BINOP(PLUS, e1, CONST(i))), e2) if (dst.exp instanceof BINOP && ((BINOP) dst.exp).oper == BINOP.PLUS && ((BINOP) dst.exp).right instanceof CONST) { munchExp(((BINOP) dst.exp).left); munchExp(src); emit("STORE"); } // 2. MOVE(MEM(BINOP(PLUS, CONST(i), e1)), e2) else if (dst.exp instanceof BINOP && ((BINOP) dst.exp).oper == BINOP.PLUS && ((BINOP) dst.exp).left instanceof CONST) { munchExp(((BINOP) dst.exp).right); munchExp(src); emit("STORE"); } // 3. MOVE(MEM(e1), MEM(e2)) else if (src instanceof MEM) { munchExp(dst.exp); munchExp(((MEM) src).exp); emit("MOVEM"); } // 4. MOVE(MEM(e1, e2) else { munchExp(dst.exp); munchExp(src); emit("STORE"); } } public void munchMove(TEMP dst, Exp src) { // 5. MOVE(TEMP(t1), e) munchExp(src); emit("ADD"); } public void munchMove(Exp dst, Exp src) { // MOVE(d, e) if (dst instanceof MEM) munchMove((MEM) dst, src); else if (dst instanceof TEMP) munchMove((TEMP) dst, src); } public void munchStm(Stm s) { if (s instanceof MOVE) munchMove(((MOVE) s).dst, ((MOVE) s).src); // CALL, JUMP, CJUMP unimplemented here } public Temp.Temp munchExp(Exp e) { } }