public IrStmt visit(IrStmtList t) { IrStmtList stmts = new IrStmtList(); for (int i = 0; i < t.size(); i++) { IrStmt s = ((IrStmt)t.elementAt(i)).accept(this); stmts.add(s); } return stmts; }
private int GetInitStmts(ClassRec c, IrStmtList stmts, IrTemp tmp) { ClassRec saveCurrClass; VarRec v; int varCnt = 0; // Get init STMTs from inherited class if (null != c.Parent()) { varCnt = GetInitStmts(c.Parent(), stmts, tmp); } // Add init STMTs from this class for (int i = 0; i < c.VarCnt(); i++) { v = c.GetClassVarAt(i); if (null == v.Init()) { stmts.add(ObjVarInitStmt(tmp, varCnt + i, v.Idx - 1, cZero)); } else { saveCurrClass = currClass; currClass = c; IrExp e = v.Init().accept(this); currClass = saveCurrClass; stmts.add(ObjVarInitStmt(tmp, varCnt + i, v.Idx - 1, e)); } } return varCnt + c.VarCnt(); }
private IrStmtList flattern(IrStmtList list) { IrStmtList sl = new IrStmtList(); for (int i = 0; i < list.size(); i++) { IrStmt s = (IrStmt)list.elementAt(i); if (null != (s as IrStmtList)) { sl.add(flattern((IrStmtList)s)); } else { sl.add(s); } } return sl; }
// *** partial implementation, will be fixed in proj2 **** // NewObj --- // Id cid; public IrExp visit(AstNewObj n) { // A NewObj node should be translated into a CALL to "malloc" with a single argument representing the size // of the object, followed by a sequence of statements for initializing the object’s class variables. // A special note: the initialization expressions saved in the the symbol table entry for this purpose are // AST expressions, and need to be translated into IR expressions. Furthermore, this translation should be // conducted in the proper scope environment — the IrgenVisitor variable currClass needs to point to the // object’s class. IrStmtList stmts = new IrStmtList(); // Calculate the size of the object ClassRec classRec = symTable.GetClass(n.cid); // <get the object's ClassRec from symbol table>; int numVars = GetNumObjVars(classRec); // Construct malloc call node IrExp size = new IrBinop(IrBinop.OP.MUL, new IrConst(numVars > 0 ? numVars : 1), cWordSize); IrCall call = new IrCall(new IrName("malloc"), new IrExpList(size)); IrTemp tmp = new IrTemp(); // This TEMP will contain the location of the object variables stmts.add(new IrMove(tmp, call)); // Move the results of the malloc call into the temp GetInitStmts(classRec, stmts, tmp); // Recursively get object's variable initialization statements return new IrEseq(stmts, tmp); // Construct resulting statement }
// 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); }
// 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; }
// VarDeclList --- public IrStmtList visit(AstVarDeclList n) { IrStmtList sl = new IrStmtList(); IrStmt s; for (int i = 0; i < n.Count(); i++) { s = n[i].accept(this); if (s != null) sl.add(s); } if (sl.size() > 0) return sl; else return null; }
// STATEMENTS // StmtList --- public IrStmtList visit(AstStmtList n) { IrStmtList sl = new IrStmtList(); for (int i = 0; i < n.Count(); i++) sl.add(n[i].accept(this)); return sl; }
public IrFunc(String l, IrStmtList sl) { label=l; varCnt=0; tmpCnt=0; argCnt=0; stmts=sl; }
public IrFunc(String l, int vc, int tc, int ac, IrStmtList sl) { label=l; varCnt=vc; tmpCnt=tc; argCnt=ac; stmts=sl; }
// Statements public int visit(IrStmtList sl) { int ret = STATUS_DEFAULT; int i = 0; while (i < sl.size()) { int next = ((IrStmt) sl.elementAt(i)).accept(this); if (next == STATUS_RETURN) { ret = STATUS_RETURN; break; } i = (next >= 0) ? next : i+1; } return ret; }
public void visit(IrFunc f) { stmtLists.Push(stmts); stmts = f.stmts; sp = sp - f.varCnt - f.argCnt - 1; f.stmts.accept(this); stmts = stmtLists.Pop(); sp = sp + f.varCnt + f.argCnt + 1; }
/* throws ParseException */ // ************************************************************************* // * Static // ************************************************************************* public static IrFunc FUNC() { IrToken t; int vc, tc, ac; IrStmtList sl=new IrStmtList(); IrStmt s; t = jj_consume_token(RegExpId.ID); jj_consume_token(RegExpId.kw32); jj_consume_token(RegExpId.kwVARCNT); jj_consume_token(RegExpId.kw33); vc = INT(); jj_consume_token(RegExpId.kw34); jj_consume_token(RegExpId.kwTMPCNT); jj_consume_token(RegExpId.kw33); tc = INT(); jj_consume_token(RegExpId.kw34); jj_consume_token(RegExpId.kwARGCNT); jj_consume_token(RegExpId.kw33); ac = INT(); jj_consume_token(RegExpId.kw35); jj_consume_token(RegExpId.kw36); // label_2: while (true) { switch ((jj_ntk == RegExpId.UNDEFINED) ? jj_ntk_fn() : jj_ntk) { case RegExpId.kw44: break; default: jj_la1[1] = jj_gen; goto label_2a; // break label_2; } s = STMT(); sl.add(s); } label_2a: jj_consume_token(RegExpId.kw37); {if (true) return new IrFunc(t.image,vc,tc,ac,sl);} throw new Error("Missing return statement in function"); }
private IrStmt mergeSTMTs(IrStmt s1, IrStmt s2) { if (nullSTMT(s1)) return s2; if (nullSTMT(s2)) return s1; IrStmtList sl = new IrStmtList(); sl.add(s1); sl.add(s2); 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; }
private IrStmt mergeSTMTs(IrStmt s1, IrStmt s2, IrStmt s3) { if (nullSTMT(s1)) return mergeSTMTs(s2, s3); if (nullSTMT(s2)) return mergeSTMTs(s1, s3); IrStmtList sl = new IrStmtList(); sl.add(s1); sl.add(mergeSTMTs(s2, s3)); return sl; }
private IrStmt mergeStmts(IrStmt s1, IrStmt s2) { if (s1 == null) return s2; if (s2 == null) return s1; IrStmtList sl = new IrStmtList(); sl.add(s1); sl.add(s2); return sl; }