From: chiba Date: Thu, 28 Aug 2003 10:46:36 +0000 (+0000) Subject: corrected to be able to compile an infinite loop. X-Git-Tag: rel_3_17_1_ga~570 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=87f4d5dd1ddf61cfa8a78b806b743b43e08a6202;p=javassist.git corrected to be able to compile an infinite loop. git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@37 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- diff --git a/Readme.html b/Readme.html index 74506f3f..4aa77616 100644 --- a/Readme.html +++ b/Readme.html @@ -246,7 +246,8 @@ see javassist.Dump.
  • javassist.expr.Expr.indexOfBytecode() has been added.
  • javassist.Loader has been modified so that getPackage() returns a package object. -
  • Now, the compiler can correctly compile a try statement. +
  • Now, the compiler can correctly compile a try statement and an + infinite while-loop.

    - version 2.5.1 in May, 2003. diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java index bb8c46cb..3b02a41f 100644 --- a/src/main/javassist/CtClass.java +++ b/src/main/javassist/CtClass.java @@ -390,7 +390,8 @@ public abstract class CtClass { } /** - * Changes a super class. + * Changes a super class. The new super class must be compatible + * with the old one. */ public void setSuperclass(CtClass clazz) throws CannotCompileException { checkModify(); diff --git a/src/main/javassist/compiler/CodeGen.java b/src/main/javassist/compiler/CodeGen.java index 9d6f7690..54f14477 100644 --- a/src/main/javassist/compiler/CodeGen.java +++ b/src/main/javassist/compiler/CodeGen.java @@ -265,10 +265,13 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { hasReturned = false; s.accept(this); - if (isVoid && !hasReturned) { - bytecode.addOpcode(Opcode.RETURN); - hasReturned = true; - } + if (!hasReturned) + if (isVoid) { + bytecode.addOpcode(Opcode.RETURN); + hasReturned = true; + } + else + throw new CompileError("no return statement"); } private boolean needsSuperCall(Stmnt body) throws CompileError { @@ -401,14 +404,14 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { if (notDo) bytecode.write16bit(pc, pc3 - pc + 1); - booleanExpr(true, expr); + boolean alwaysBranch = booleanExpr(true, expr); bytecode.addIndex(pc2 - bytecode.currentPc() + 1); patchGoto(breakList, bytecode.currentPc()); patchGoto(continueList, pc3); continueList = prevContList; breakList = prevBreakList; - hasReturned = false; + hasReturned = alwaysBranch; } private void patchGoto(ArrayList list, int targetPc) { @@ -879,10 +882,9 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { /* Produces the opcode to branch if the condition is true. * The oprand is not produced. * - * If false is returned, the branch occurs if the condition is - * false. + * @return true if the compiled code is GOTO (always branch). */ - private void booleanExpr(boolean branchIf, ASTree expr) + private boolean booleanExpr(boolean branchIf, ASTree expr) throws CompileError { boolean isAndAnd; @@ -907,6 +909,10 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { bytecode.addOpcode(Opcode.GOTO); } } + else if (isAlwaysBranch(expr, branchIf)) { + bytecode.addOpcode(Opcode.GOTO); + return true; // always branch + } else { // others expr.accept(this); bytecode.addOpcode(branchIf ? IFNE : IFEQ); @@ -914,6 +920,17 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { exprType = BOOLEAN; arrayDim = 0; + return false; + } + + + private static boolean isAlwaysBranch(ASTree expr, boolean branchIf) { + if (expr instanceof Keyword) { + int t = ((Keyword)expr).get(); + return branchIf ? t == TRUE : t == FALSE; + } + + return false; } private static int getCompOperator(ASTree expr) throws CompileError {