public IrgenVisitor(SymbolTable symTable_, TypeVisitor tv_) { cOne = new IrConst(1); cZero = new IrConst(0); cWordSize = new IrName("wSZ"); symTable = symTable_; tv = tv_; currClass = null; currMethod = null; }
public IrCJump(AstRelop.OP o, IrExp l, IrExp r, IrName t) { left = l; right = r; target = t; switch (o) { case AstRelop.OP.EQ: op = OP.EQ; break; case AstRelop.OP.NE: op = OP.NE; break; case AstRelop.OP.LT: op = OP.LT; break; case AstRelop.OP.LE: op = OP.LE; break; case AstRelop.OP.GT: op = OP.GT; break; case AstRelop.OP.GE: op = OP.GE; break; default: Debug.Assert(false, "Encountered unknown binary operator: value = " + o.ToString()); break; } }
// NewArray --- // int size; public IrExp visit(AstNewArray n) { IrExp size = new IrConst(n.size); IrStmtList sl = new IrStmtList(); IrName top = new IrName(); IrTemp array = new IrTemp(); IrTemp cnt = new IrTemp(); IrExp bound = new IrBinop(IrBinop.OP.MUL, new IrConst(n.size + 1), cWordSize); sl.add(new IrMove(array, new IrCall(new IrName("malloc"), new IrExpList(bound)))); sl.add(new IrMove(new IrMem(array), size)); sl.add(new IrMove(cnt, new IrBinop(IrBinop.OP.ADD, array, new IrBinop(IrBinop.OP.MUL, size, cWordSize)))); sl.add(new IrLabel(top)); sl.add(new IrMove(new IrMem(cnt), cZero)); sl.add(new IrMove(cnt, new IrBinop(IrBinop.OP.SUB, cnt, cWordSize))); sl.add(new IrCJump(IrCJump.OP.GT, cnt, array, top)); return new IrEseq(sl, array); }
// Relop --- // int op; // Exp e1, e2; public IrExp visit(AstRelop n) { IrExp l = n.e1.accept(this); IrExp r = n.e2.accept(this); IrStmtList sl = new IrStmtList(); IrTemp tmp = new IrTemp(); IrName done = new IrName(); sl.add(new IrMove(tmp, cOne)); sl.add(new IrCJump(n.op, l, r, done)); sl.add(new IrMove(tmp, cZero)); sl.add(new IrLabel(done)); return new IrEseq(sl, tmp); }
// Print --- // Exp e; public IrStmt visit(AstPrint n) { IrName func = new IrName("print"); IrExpList args = new IrExpList(); if (n.e != null) args.add(n.e.accept(this)); return new IrCallst(func, args); }
// While --- // Exp e; // Stmt s; public IrStmt visit(AstWhile n) { IrName test = new IrName(); IrName done = new IrName(); IrStmtList sl = new IrStmtList(); sl.add(new IrLabel(test)); IrExp exp = n.e.accept(this); sl.add(new IrCJump(IrCJump.OP.EQ, exp, cZero, done)); sl.add(n.s.accept(this)); sl.add(new IrJump(test)); sl.add(new IrLabel(done)); return sl; }
// If --- // Exp e; // Stmt s1, s2; public IrStmt visit(AstIf n) { IrName alt = new IrName(); IrStmtList sl = new IrStmtList(); IrExp exp = n.e.accept(this); sl.add(new IrCJump(IrCJump.OP.EQ, exp, cZero, alt)); sl.add(n.s1.accept(this)); if (n.s2 == null) { sl.add(new IrLabel(alt)); } else { IrName done = new IrName(); sl.add(new IrJump(done)); sl.add(new IrLabel(alt)); sl.add(n.s2.accept(this)); sl.add(new IrLabel(done)); } return sl; }
public IrJump(IrName e) { target=e; }
public IrCall(IrName func, IrExpList args) { _func = func; _args = args; }
private int findStmtIdx(IrName target) { IrStmt stmt; if (target.id.CompareTo("L5") == 0) { } for(int i = 0; i < stmts.size(); ++i) { stmt = stmts.elementAt(i); if (stmt is IrLabel) { IrLabel l = (IrLabel) stmt; if (l.lab.CompareTo(target.id) == 0) { return i; } } } throw new Exception("Label does not exist: " + target.id); }
public int visit(IrName t) { if (t.id.Equals("wSZ")) return 1; return STATUS_DEFAULT; }
public IrLabel(IrName n) { lab=n.id; }
/* throws ParseException */ public static IrExp EXP() { IrToken t; int n; String str; IrStmt s; IrExp e, e2; IrExpList el = new IrExpList(); jj_consume_token(RegExpId.kw32); switch ((jj_ntk == RegExpId.UNDEFINED) ? jj_ntk_fn() : jj_ntk) { case RegExpId.kwESEQ: jj_consume_token(RegExpId.kwESEQ); s = STMT(); e = EXP(); e = new IrEseq(s,e); break; case RegExpId.kwMEM: jj_consume_token(RegExpId.kwMEM); e = EXP(); e = new IrMem(e); break; case RegExpId.kwBINOP: jj_consume_token(RegExpId.kwBINOP); IrBinop.OP op = binopCode(); e = EXP(); e2 = EXP(); e = new IrBinop(op,e,e2); break; case RegExpId.kwCALL: jj_consume_token(RegExpId.kwCALL); e = EXP(); jj_consume_token(RegExpId.kw32); // label_4: while (true) { switch ((jj_ntk == RegExpId.UNDEFINED) ? jj_ntk_fn() : jj_ntk) { case RegExpId.kw32: break; default: jj_la1[7] = jj_gen; goto label_4a; // break label_4; } e2 = EXP(); el.add(e2); } label_4a: jj_consume_token(RegExpId.kw35); e = new IrCall((IrName)e,el); break; case RegExpId.kwTEMP: jj_consume_token(RegExpId.kwTEMP); n = INT(); e = new IrTemp(n); break; case RegExpId.kwNAME: jj_consume_token(RegExpId.kwNAME); t = jj_consume_token(RegExpId.ID); e = new IrName(t.image); break; case RegExpId.kwFIELD: jj_consume_token(RegExpId.kwFIELD); e = EXP(); n = INT(); e = new IrField(e,n); break; case RegExpId.kwPARAM: jj_consume_token(RegExpId.kwPARAM); n = INT(); e = new IrParam(n); break; case RegExpId.kwVAR: jj_consume_token(RegExpId.kwVAR); n = INT(); e = new IrVar(n); break; case RegExpId.kwCONST: jj_consume_token(RegExpId.kwCONST); n = INT(); e = new IrConst(n); break; case RegExpId.kwSTRING: jj_consume_token(RegExpId.kwSTRING); str = STR(); e = new IrString(str); break; default: jj_la1[8] = jj_gen; jj_consume_token(RegExpId.UNDEFINED); throw new IrParseException(); } jj_consume_token(RegExpId.kw35); {if (true) return e;} throw new Error("Missing return statement in function"); }
public IrExp visit(IrName t) { return t; }
public IrCJump(OP o, IrExp l, IrExp r, IrName t) { op = o; left = l; right = r; target = t; }