



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Portland State University
Jingke Li (Portland State University) CS322 IR Tree Canonicalization 1 / 1
(CALL (NAME f) ((CONST 1))) => (ESEQ [MOVE (TEMP 100) (CALL (NAME f) ((CONST 1)))] (TEMP 100))
[ASSIGN (TEMP 101) (BINOP + (CONST 2) (CALL (NAME f) ((CONST 1))))] => [ASSIGN (TEMP 101) (BINOP + (CONST 2) (ESEQ [MOVE (TEMP 100) (CALL (NAME f) ((CONST 1)))] (TEMP 100)))] => [MOVE (TEMP 100) (CALL (NAME f) ((CONST 1)))] [ASSIGN (TEMP 101) (BINOP + (CONST 2) (TEMP 100))]
Jingke Li (Portland State University) CS322 IR Tree Canonicalization 3 / 1
public class Canon implements IrVI { public PROG visit(PROG t) { return new PROG(t.funcs.accept(this)); } public FUNClist visit(FUNClist t) { FUNClist funcs = new FUNClist(); for (int i=0; i<t.size(); i++) { funcs.add(((FUNC) t.elementAt(i)).accept(this)); } return funcs; } public FUNC visit(FUNC t) { return new FUNC(t.label, t.varCnt, t.argCnt, (STMTlist) t.stmts.accept(this)); } ... }
public EXP visit(EXPlist t) { STMT s = null; EXPlist args = new EXPlist(); for (int i=0; i<t.size(); i++) { EXP e = ((EXP) t.elementAt(i)).accept(this); STMT s1 = getStmt(e); if (s1 != null) s = mergeStmts(s, s1); args.add(getExp(e)); } if (s != null) return new ESEQ(s, args); return t; }
public EXP visit(ESEQ t) { STMT stmt = t.stmt.accept(this); EXP exp = t.exp.accept(this); STMT s = mergeStmts(stmt, getStmt(exp)); if (s != null) return new ESEQ(s, getExp(exp)); return t; }
Jingke Li (Portland State University) CS322 IR Tree Canonicalization 7 / 1
public STMT visit(MOVE t) { EXP dst = t.dst.accept(this); EXP src = t.src.accept(this); STMT s = mergeStmts(getStmt(dst), getStmt(src)); if (s != null) return mergeStmts(s, new MOVE(getExp(dst), getExp(src))); return t; }
public STMT visit(CJUMP t) { EXP left = t.left.accept(this); EXP right = t.right.accept(this); STMT s = mergeStmts(getStmt(left), getStmt(right)); if (s != null) return mergeStmts(s, new CJUMP(t.op, getExp(left), getExp(right), t.target)); return t; }
public STMT visit(CALLST t) { EXP args = t.args.accept(this); STMT s = getStmt(args); if (s != null) return mergeStmts(s, new CALLST(t.func, (EXPlist) getExp(args))); return t; }
public STMT visit(RETURN t) { EXP exp = t.exp.accept(this); STMT s = getStmt(exp); if (s != null) return mergeStmts(s, new RETURN(getExp(exp))); return t; }