diff options
Diffstat (limited to 'src/main/javassist/compiler/CodeGen.java')
-rw-r--r-- | src/main/javassist/compiler/CodeGen.java | 128 |
1 files changed, 84 insertions, 44 deletions
diff --git a/src/main/javassist/compiler/CodeGen.java b/src/main/javassist/compiler/CodeGen.java index ab02d8cf..d4c748f8 100644 --- a/src/main/javassist/compiler/CodeGen.java +++ b/src/main/javassist/compiler/CodeGen.java @@ -18,10 +18,36 @@ package javassist.compiler; import java.util.ArrayList; import java.util.Arrays; -import javassist.compiler.ast.*; -import javassist.bytecode.*; - -/* The code generator is implemeted by three files: +import java.util.List; + +import javassist.bytecode.Bytecode; +import javassist.bytecode.Opcode; +import javassist.compiler.ast.ASTList; +import javassist.compiler.ast.ASTree; +import javassist.compiler.ast.ArrayInit; +import javassist.compiler.ast.AssignExpr; +import javassist.compiler.ast.BinExpr; +import javassist.compiler.ast.CallExpr; +import javassist.compiler.ast.CastExpr; +import javassist.compiler.ast.CondExpr; +import javassist.compiler.ast.Declarator; +import javassist.compiler.ast.DoubleConst; +import javassist.compiler.ast.Expr; +import javassist.compiler.ast.FieldDecl; +import javassist.compiler.ast.InstanceOfExpr; +import javassist.compiler.ast.IntConst; +import javassist.compiler.ast.Keyword; +import javassist.compiler.ast.Member; +import javassist.compiler.ast.MethodDecl; +import javassist.compiler.ast.NewExpr; +import javassist.compiler.ast.Pair; +import javassist.compiler.ast.Stmnt; +import javassist.compiler.ast.StringL; +import javassist.compiler.ast.Symbol; +import javassist.compiler.ast.Variable; +import javassist.compiler.ast.Visitor; + +/* The code generator is implemented by three files: * CodeGen.java, MemberCodeGen.java, and JvstCodeGen. * I just wanted to split a big file into three smaller ones. */ @@ -47,7 +73,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { */ public boolean inStaticMethod; - protected ArrayList breakList, continueList; + protected List<Integer> breakList, continueList; /** * doit() in ReturnHook is called from atReturn(). @@ -171,18 +197,16 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { if (dim == 0) return name; - else { - StringBuffer sbuf = new StringBuffer(); - int d = dim; - while (d-- > 0) - sbuf.append('['); + StringBuffer sbuf = new StringBuffer(); + int d = dim; + while (d-- > 0) + sbuf.append('['); - sbuf.append('L'); - sbuf.append(name); - sbuf.append(';'); + sbuf.append('L'); + sbuf.append(name); + sbuf.append(';'); - return sbuf.toString(); - } + return sbuf.toString(); } protected static String toJvmTypeName(int type, int dim) { @@ -242,16 +266,21 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { expr.accept(typeChecker); } + @Override public void atASTList(ASTList n) throws CompileError { fatal(); } - + + @Override public void atPair(Pair n) throws CompileError { fatal(); } + @Override public void atSymbol(Symbol n) throws CompileError { fatal(); } + @Override public void atFieldDecl(FieldDecl field) throws CompileError { field.getInit().accept(this); } + @Override public void atMethodDecl(MethodDecl method) throws CompileError { ASTList mods = method.getModifiers(); setMaxLocals(1); @@ -263,7 +292,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { inStaticMethod = true; } } - + ASTList params = method.getParams(); while (params != null) { atDeclarator((Declarator)params.head()); @@ -320,6 +349,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { protected abstract void insertDefaultSuperCall() throws CompileError; + @Override public void atStmnt(Stmnt st) throws CompileError { if (st == null) return; // empty @@ -417,10 +447,10 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { } private void atWhileStmnt(Stmnt st, boolean notDo) throws CompileError { - ArrayList prevBreakList = breakList; - ArrayList prevContList = continueList; - breakList = new ArrayList(); - continueList = new ArrayList(); + List<Integer> prevBreakList = breakList; + List<Integer> prevContList = continueList; + breakList = new ArrayList<Integer>(); + continueList = new ArrayList<Integer>(); ASTree expr = st.head(); Stmnt body = (Stmnt)st.tail(); @@ -454,19 +484,16 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { hasReturned = alwaysBranch; } - protected void patchGoto(ArrayList list, int targetPc) { - int n = list.size(); - for (int i = 0; i < n; ++i) { - int pc = ((Integer)list.get(i)).intValue(); + protected void patchGoto(List<Integer> list, int targetPc) { + for (int pc:list) bytecode.write16bit(pc, targetPc - pc + 1); - } } private void atForStmnt(Stmnt st) throws CompileError { - ArrayList prevBreakList = breakList; - ArrayList prevContList = continueList; - breakList = new ArrayList(); - continueList = new ArrayList(); + List<Integer> prevBreakList = breakList; + List<Integer> prevContList = continueList; + breakList = new ArrayList<Integer>(); + continueList = new ArrayList<Integer>(); Stmnt init = (Stmnt)st.head(); ASTList p = st.tail(); @@ -517,8 +544,8 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { private void atSwitchStmnt(Stmnt st) throws CompileError { compileExpr(st.head()); - ArrayList prevBreakList = breakList; - breakList = new ArrayList(); + List<Integer> prevBreakList = breakList; + breakList = new ArrayList<Integer>(); int opcodePc = bytecode.currentPc(); bytecode.addOpcode(LOOKUPSWITCH); int npads = 3 - (opcodePc & 3); @@ -583,8 +610,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { expr = TypeChecker.stripPlusExpr(expr); if (expr instanceof IntConst) return (int)((IntConst)expr).get(); - else - throw new CompileError("bad case label"); + throw new CompileError("bad case label"); } private void atBreakStmnt(Stmnt st, boolean notCont) @@ -672,6 +698,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { bc.addOpcode(MONITORENTER); ReturnHook rh = new ReturnHook(this) { + @Override protected boolean doit(Bytecode b, int opcode) { b.addAload(var); b.addOpcode(MONITOREXIT); @@ -711,7 +738,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { "sorry, cannot break/continue in synchronized block"); } - private static int getListSize(ArrayList list) { + private static int getListSize(List<Integer> list) { return list == null ? 0 : list.size(); } @@ -724,6 +751,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { return false; } + @Override public void atDeclarator(Declarator d) throws CompileError { d.setLocalVar(getMaxLocals()); d.setClassName(resolveClassName(d.getClassName())); @@ -745,10 +773,13 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { } } + @Override public abstract void atNewExpr(NewExpr n) throws CompileError; + @Override public abstract void atArrayInit(ArrayInit init) throws CompileError; + @Override public void atAssignExpr(AssignExpr expr) throws CompileError { atAssignExpr(expr, true); } @@ -931,6 +962,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { return false; } + @Override public void atCondExpr(CondExpr expr) throws CompileError { if (booleanExpr(false, expr.condExpr())) expr.elseExpr().accept(this); @@ -974,6 +1006,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { return -1; } + @Override public void atBinExpr(BinExpr expr) throws CompileError { int token = expr.getOperator(); @@ -1136,7 +1169,6 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { arrayDim = 0; return true; } - else { int pc = bytecode.currentPc(); bytecode.addIndex(0); // correct later if (booleanExpr(isAndAnd, bexpr.oprand2())) @@ -1147,7 +1179,6 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { bytecode.addIndex(6); // skip GOTO instruction bytecode.addOpcode(Opcode.GOTO); } - } } else if (isAlwaysBranch(expr, branchIf)) { // Opcode.GOTO is not added here. The caller must add it. @@ -1207,8 +1238,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { if (type1 == NULL) return exprType; - else - return type1; + return type1; } private static final int ifOp[] = { EQ, IF_ICMPEQ, IF_ICMPNE, @@ -1390,6 +1420,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { bytecode.addOpcode(op); } + @Override public void atCastExpr(CastExpr expr) throws CompileError { String cname = resolveClassName(expr.getClassName()); String toClass = checkCastExpr(expr, cname); @@ -1403,6 +1434,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { bytecode.addCheckcast(toClass); } + @Override public void atInstanceOfExpr(InstanceOfExpr expr) throws CompileError { String cname = resolveClassName(expr.getClassName()); String toClass = checkCastExpr(expr, cname); @@ -1443,7 +1475,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { { if (srcType == destType) return; - + int op, op2; int stype = typePrecedence(srcType); int dtype = typePrecedence(destType); @@ -1475,6 +1507,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { bytecode.addOpcode(op2); } + @Override public void atExpr(Expr expr) throws CompileError { // array access, member access, // (unary) +, (unary) -, ++, --, !, ~ @@ -1483,7 +1516,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { ASTree oprand = expr.oprand1(); if (token == '.') { String member = ((Symbol)expr.oprand2()).get(); - if (member.equals("class")) + if (member.equals("class")) atClassObject(expr); // .class else atFieldRead(expr); @@ -1560,6 +1593,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { throw new CompileError("invalid type for " + expr.getName()); } + @Override public abstract void atCallExpr(CallExpr expr) throws CompileError; protected abstract void atFieldRead(ASTree expr) throws CompileError; @@ -1749,7 +1783,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { if (doDup && isPost) bytecode.addOpcode(DUP2); - bytecode.addLconst((long)1); + bytecode.addLconst(1); bytecode.addOpcode(token == PLUSPLUS ? LADD : LSUB); if (doDup && !isPost) bytecode.addOpcode(DUP2); @@ -1835,7 +1869,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { exprType = INT; } else if (t == LONG) { - bytecode.addLconst((long)1); + bytecode.addLconst(1); bytecode.addOpcode(token == PLUSPLUS ? LADD : LSUB); } else if (t == FLOAT) { @@ -1856,8 +1890,10 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { protected abstract void atFieldPlusPlus(int token, boolean isPost, ASTree oprand, Expr expr, boolean doDup) throws CompileError; + @Override public abstract void atMember(Member n) throws CompileError; + @Override public void atVariable(Variable v) throws CompileError { Declarator d = v.getDeclarator(); exprType = d.getType(); @@ -1887,6 +1923,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { } } + @Override public void atKeyword(Keyword k) throws CompileError { arrayDim = 0; int token = k.get(); @@ -1914,13 +1951,14 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { if (token == THIS) className = getThisName(); else - className = getSuperName(); + className = getSuperName(); break; default : fatal(); } } + @Override public void atStringL(StringL s) throws CompileError { exprType = CLASS; arrayDim = 0; @@ -1928,6 +1966,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { bytecode.addLdc(s.get()); } + @Override public void atIntConst(IntConst i) throws CompileError { arrayDim = 0; long value = i.get(); @@ -1942,6 +1981,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { } } + @Override public void atDoubleConst(DoubleConst d) throws CompileError { arrayDim = 0; if (d.getType() == DoubleConstant) { |