[jboss-cvs] javassist/src/main/javassist ...

Shigeru Chiba chiba at is.titech.ac.jp
Fri May 25 03:45:07 EDT 2007


  User: chiba   
  Date: 07/05/25 03:45:07

  Modified:    src/main/javassist  CtBehavior.java
  Log:
  modified so that CtBehavior#insertAfter() will not use JSR/RET anymore.
  
  Revision  Changes    Path
  1.35      +35 -49    javassist/src/main/javassist/CtBehavior.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CtBehavior.java
  ===================================================================
  RCS file: /cvsroot/jboss/javassist/src/main/javassist/CtBehavior.java,v
  retrieving revision 1.34
  retrieving revision 1.35
  diff -u -b -r1.34 -r1.35
  --- CtBehavior.java	6 Feb 2007 16:40:36 -0000	1.34
  +++ CtBehavior.java	25 May 2007 07:45:07 -0000	1.35
  @@ -646,14 +646,12 @@
               int varNo = jv.recordReturnType(rtype, true);
               jv.recordLocalVariables(ca, 0);
   
  -            int handlerLen = insertAfterHandler(asFinally, b, rtype, varNo);
  +            // finally clause for exceptions
  +            int handlerLen = insertAfterHandler(asFinally, b, rtype, varNo,
  +                                                jv, src);
  +            // finally clause for normal termination
  +            insertAfterAdvice(b, jv, src, pool, rtype, varNo);
   
  -            byte[] save = makeSaveCode(pool, rtype, varNo);
  -            byte[] restore = makeRestoreCode(b, pool, rtype, varNo);
  -
  -            b.addAstore(retAddr);
  -            jv.compileStmnt(src);
  -            b.addRet(retAddr);
               ca.setMaxStack(b.getMaxStack());
               ca.setMaxLocals(b.getMaxLocals());
   
  @@ -675,7 +673,7 @@
                   if (c == Opcode.ARETURN || c == Opcode.IRETURN
                       || c == Opcode.FRETURN || c == Opcode.LRETURN
                       || c == Opcode.DRETURN || c == Opcode.RETURN) {
  -                    insertJSR(iterator, subr, pos, save, restore);
  +                    insertGoto(iterator, subr, pos);
                       subr = iterator.getCodeLength() - gapLen;
                   }
               }
  @@ -691,60 +689,52 @@
           }
       }
   
  -    private byte[] makeSaveCode(ConstPool cp, CtClass rtype, int varNo) {
  -        Bytecode b = new Bytecode(cp, 0, 0);
  -        if (rtype == CtClass.voidType) {
  -            b.addOpcode(Opcode.ACONST_NULL);
  -            b.addAstore(varNo);
  -            return b.get();
  -        }
  -        else {
  -            b.addStore(varNo, rtype);
  -            return b.get();
  -        }
  -    }
  -
  -    private byte[] makeRestoreCode(Bytecode code, ConstPool cp,
  -                                   CtClass rtype, int varNo) {
  +    private void insertAfterAdvice(Bytecode code, Javac jv, String src,
  +                                   ConstPool cp, CtClass rtype, int varNo)
  +        throws CompileError
  +    {
           if (rtype == CtClass.voidType) {
  +            code.addOpcode(Opcode.ACONST_NULL);
  +            code.addAstore(varNo);
  +            jv.compileStmnt(src);
  +            code.addOpcode(Opcode.RETURN);
               if (code.getMaxLocals() < 1)
                   code.setMaxLocals(1);
  -
  -            return new byte[0];
           }
           else {
  -            Bytecode b = new Bytecode(cp, 0, 0);
  -            b.addLoad(varNo, rtype);
  -            return b.get();
  +            code.addStore(varNo, rtype);
  +            jv.compileStmnt(src);
  +            code.addLoad(varNo, rtype);
  +            if (rtype.isPrimitive())
  +                code.addOpcode(((CtPrimitiveType)rtype).getReturnOp());
  +            else
  +                code.addOpcode(Opcode.ARETURN);
           }
       }
   
  -    private void insertJSR(CodeIterator iterator, int subr, int pos,
  -                           byte[] save, byte[] restore)
  +    private void insertGoto(CodeIterator iterator, int subr, int pos)
           throws BadBytecode
       {
  -        int gapSize = 5 + save.length + restore.length;
  -        boolean wide = subr - pos > Short.MAX_VALUE - gapSize - 4;
  -        gapSize = iterator.insertGap(pos, wide ? gapSize : gapSize - 2);
  +        // the gap length might be a multiple of 4.
  +        boolean wide = subr + 4 - pos > Short.MAX_VALUE;
  +        int gapSize = iterator.insertGap(pos, wide ? 4 : 2);
   
  -        iterator.write(save, pos);
  -        pos += save.length;
           if (wide) {
  -            iterator.writeByte(Opcode.JSR_W, pos);
  +            iterator.writeByte(Opcode.GOTO_W, pos);
               iterator.write32bit(subr - pos + gapSize, pos + 1);
  -            pos += 5;
           }
           else {
  -            iterator.writeByte(Opcode.JSR, pos);
  +            iterator.writeByte(Opcode.GOTO, pos);
               iterator.write16bit(subr - pos + gapSize, pos + 1);
  -            pos += 3;
           }
  -
  -        iterator.write(restore, pos);
       }
   
  +    /* insert a finally clause
  +     */
       private int insertAfterHandler(boolean asFinally, Bytecode b,
  -                                   CtClass rtype, int returnVarNo)
  +                                   CtClass rtype, int returnVarNo,
  +                                   Javac javac, String src)
  +        throws CompileError
       {
           if (!asFinally)
               return 0;
  @@ -752,7 +742,7 @@
           int var = b.getMaxLocals();
           b.incMaxLocals(1);
           int pc = b.currentPc();
  -        b.addAstore(var);
  +        b.addAstore(var);   // store an exception
           if (rtype.isPrimitive()) {
               char c = ((CtPrimitiveType)rtype).getDescriptor();
               if (c == 'D') {
  @@ -777,14 +767,10 @@
               b.addAstore(returnVarNo);
           }
   
  -        b.addOpcode(Opcode.JSR);
  -        int pc2 = b.currentPc();
  -        b.addIndex(0);  // correct later
  +        javac.compileStmnt(src);
           b.addAload(var);
           b.addOpcode(Opcode.ATHROW);
  -        int pc3 = b.currentPc();
  -        b.write16bit(pc2, pc3 - pc2 + 1);
  -        return pc3 - pc;
  +        return b.currentPc() - pc;
       }
   
       /* -- OLD version --
  
  
  



More information about the jboss-cvs-commits mailing list