rlm@10: /*** rlm@10: * ASM: a very small and fast Java bytecode manipulation framework rlm@10: * Copyright (c) 2000-2005 INRIA, France Telecom rlm@10: * All rights reserved. rlm@10: * rlm@10: * Redistribution and use in source and binary forms, with or without rlm@10: * modification, are permitted provided that the following conditions rlm@10: * are met: rlm@10: * 1. Redistributions of source code must retain the above copyright rlm@10: * notice, this list of conditions and the following disclaimer. rlm@10: * 2. Redistributions in binary form must reproduce the above copyright rlm@10: * notice, this list of conditions and the following disclaimer in the rlm@10: * documentation and/or other materials provided with the distribution. rlm@10: * 3. Neither the name of the copyright holders nor the names of its rlm@10: * contributors may be used to endorse or promote products derived from rlm@10: * this software without specific prior written permission. rlm@10: * rlm@10: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" rlm@10: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE rlm@10: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE rlm@10: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE rlm@10: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR rlm@10: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF rlm@10: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS rlm@10: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN rlm@10: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) rlm@10: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF rlm@10: * THE POSSIBILITY OF SUCH DAMAGE. rlm@10: */ rlm@10: package clojure.asm; rlm@10: rlm@10: /** rlm@10: * A {@link MethodVisitor} that generates methods in bytecode form. Each visit rlm@10: * method of this class appends the bytecode corresponding to the visited rlm@10: * instruction to a byte vector, in the order these methods are called. rlm@10: * rlm@10: * @author Eric Bruneton rlm@10: * @author Eugene Kuleshov rlm@10: */ rlm@10: class MethodWriter implements MethodVisitor{ rlm@10: rlm@10: /** rlm@10: * Pseudo access flag used to denote constructors. rlm@10: */ rlm@10: final static int ACC_CONSTRUCTOR = 262144; rlm@10: rlm@10: /** rlm@10: * Frame has exactly the same locals as the previous stack map frame and rlm@10: * number of stack items is zero. rlm@10: */ rlm@10: final static int SAME_FRAME = 0; // to 63 (0-3f) rlm@10: rlm@10: /** rlm@10: * Frame has exactly the same locals as the previous stack map frame and rlm@10: * number of stack items is 1 rlm@10: */ rlm@10: final static int SAME_LOCALS_1_STACK_ITEM_FRAME = 64; // to 127 (40-7f) rlm@10: rlm@10: /** rlm@10: * Reserved for future use rlm@10: */ rlm@10: final static int RESERVED = 128; rlm@10: rlm@10: /** rlm@10: * Frame has exactly the same locals as the previous stack map frame and rlm@10: * number of stack items is 1. Offset is bigger then 63; rlm@10: */ rlm@10: final static int SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED = 247; // f7 rlm@10: rlm@10: /** rlm@10: * Frame where current locals are the same as the locals in the previous rlm@10: * frame, except that the k last locals are absent. The value of k is given rlm@10: * by the formula 251-frame_type. rlm@10: */ rlm@10: final static int CHOP_FRAME = 248; // to 250 (f8-fA) rlm@10: rlm@10: /** rlm@10: * Frame has exactly the same locals as the previous stack map frame and rlm@10: * number of stack items is zero. Offset is bigger then 63; rlm@10: */ rlm@10: final static int SAME_FRAME_EXTENDED = 251; // fb rlm@10: rlm@10: /** rlm@10: * Frame where current locals are the same as the locals in the previous rlm@10: * frame, except that k additional locals are defined. The value of k is rlm@10: * given by the formula frame_type-251. rlm@10: */ rlm@10: final static int APPEND_FRAME = 252; // to 254 // fc-fe rlm@10: rlm@10: /** rlm@10: * Full frame rlm@10: */ rlm@10: final static int FULL_FRAME = 255; // ff rlm@10: rlm@10: /** rlm@10: * Indicates that the stack map frames must be recomputed from scratch. In rlm@10: * this case the maximum stack size and number of local variables is also rlm@10: * recomputed from scratch. rlm@10: * rlm@10: * @see #compute rlm@10: */ rlm@10: private final static int FRAMES = 0; rlm@10: rlm@10: /** rlm@10: * Indicates that the maximum stack size and number of local variables must rlm@10: * be automatically computed. rlm@10: * rlm@10: * @see #compute rlm@10: */ rlm@10: private final static int MAXS = 1; rlm@10: rlm@10: /** rlm@10: * Indicates that nothing must be automatically computed. rlm@10: * rlm@10: * @see #compute rlm@10: */ rlm@10: private final static int NOTHING = 2; rlm@10: rlm@10: /** rlm@10: * Next method writer (see {@link ClassWriter#firstMethod firstMethod}). rlm@10: */ rlm@10: MethodWriter next; rlm@10: rlm@10: /** rlm@10: * The class writer to which this method must be added. rlm@10: */ rlm@10: ClassWriter cw; rlm@10: rlm@10: /** rlm@10: * Access flags of this method. rlm@10: */ rlm@10: private int access; rlm@10: rlm@10: /** rlm@10: * The index of the constant pool item that contains the name of this rlm@10: * method. rlm@10: */ rlm@10: private int name; rlm@10: rlm@10: /** rlm@10: * The index of the constant pool item that contains the descriptor of this rlm@10: * method. rlm@10: */ rlm@10: private int desc; rlm@10: rlm@10: /** rlm@10: * The descriptor of this method. rlm@10: */ rlm@10: private String descriptor; rlm@10: rlm@10: /** rlm@10: * The signature of this method. rlm@10: */ rlm@10: String signature; rlm@10: rlm@10: /** rlm@10: * If not zero, indicates that the code of this method must be copied from rlm@10: * the ClassReader associated to this writer in cw.cr. More rlm@10: * precisely, this field gives the index of the first byte to copied from rlm@10: * cw.cr.b. rlm@10: */ rlm@10: int classReaderOffset; rlm@10: rlm@10: /** rlm@10: * If not zero, indicates that the code of this method must be copied from rlm@10: * the ClassReader associated to this writer in cw.cr. More rlm@10: * precisely, this field gives the number of bytes to copied from rlm@10: * cw.cr.b. rlm@10: */ rlm@10: int classReaderLength; rlm@10: rlm@10: /** rlm@10: * Number of exceptions that can be thrown by this method. rlm@10: */ rlm@10: int exceptionCount; rlm@10: rlm@10: /** rlm@10: * The exceptions that can be thrown by this method. More precisely, this rlm@10: * array contains the indexes of the constant pool items that contain the rlm@10: * internal names of these exception classes. rlm@10: */ rlm@10: int[] exceptions; rlm@10: rlm@10: /** rlm@10: * The annotation default attribute of this method. May be null. rlm@10: */ rlm@10: private ByteVector annd; rlm@10: rlm@10: /** rlm@10: * The runtime visible annotations of this method. May be null. rlm@10: */ rlm@10: private AnnotationWriter anns; rlm@10: rlm@10: /** rlm@10: * The runtime invisible annotations of this method. May be null. rlm@10: */ rlm@10: private AnnotationWriter ianns; rlm@10: rlm@10: /** rlm@10: * The runtime visible parameter annotations of this method. May be rlm@10: * null. rlm@10: */ rlm@10: private AnnotationWriter[] panns; rlm@10: rlm@10: /** rlm@10: * The runtime invisible parameter annotations of this method. May be rlm@10: * null. rlm@10: */ rlm@10: private AnnotationWriter[] ipanns; rlm@10: rlm@10: /** rlm@10: * The non standard attributes of the method. rlm@10: */ rlm@10: private Attribute attrs; rlm@10: rlm@10: /** rlm@10: * The bytecode of this method. rlm@10: */ rlm@10: private ByteVector code = new ByteVector(); rlm@10: rlm@10: /** rlm@10: * Maximum stack size of this method. rlm@10: */ rlm@10: private int maxStack; rlm@10: rlm@10: /** rlm@10: * Maximum number of local variables for this method. rlm@10: */ rlm@10: private int maxLocals; rlm@10: rlm@10: /** rlm@10: * Number of stack map frames in the StackMapTable attribute. rlm@10: */ rlm@10: private int frameCount; rlm@10: rlm@10: /** rlm@10: * The StackMapTable attribute. rlm@10: */ rlm@10: private ByteVector stackMap; rlm@10: rlm@10: /** rlm@10: * The offset of the last frame that was written in the StackMapTable rlm@10: * attribute. rlm@10: */ rlm@10: private int previousFrameOffset; rlm@10: rlm@10: /** rlm@10: * The last frame that was written in the StackMapTable attribute. rlm@10: * rlm@10: * @see #frame rlm@10: */ rlm@10: private int[] previousFrame; rlm@10: rlm@10: /** rlm@10: * Index of the next element to be added in {@link #frame}. rlm@10: */ rlm@10: private int frameIndex; rlm@10: rlm@10: /** rlm@10: * The current stack map frame. The first element contains the offset of the rlm@10: * instruction to which the frame corresponds, the second element is the rlm@10: * number of locals and the third one is the number of stack elements. The rlm@10: * local variables start at index 3 and are followed by the operand stack rlm@10: * values. In summary frame[0] = offset, frame[1] = nLocal, frame[2] = rlm@10: * nStack, frame[3] = nLocal. All types are encoded as integers, with the rlm@10: * same format as the one used in {@link Label}, but limited to BASE types. rlm@10: */ rlm@10: private int[] frame; rlm@10: rlm@10: /** rlm@10: * Number of elements in the exception handler list. rlm@10: */ rlm@10: private int handlerCount; rlm@10: rlm@10: /** rlm@10: * The first element in the exception handler list. rlm@10: */ rlm@10: private Handler firstHandler; rlm@10: rlm@10: /** rlm@10: * The last element in the exception handler list. rlm@10: */ rlm@10: private Handler lastHandler; rlm@10: rlm@10: /** rlm@10: * Number of entries in the LocalVariableTable attribute. rlm@10: */ rlm@10: private int localVarCount; rlm@10: rlm@10: /** rlm@10: * The LocalVariableTable attribute. rlm@10: */ rlm@10: private ByteVector localVar; rlm@10: rlm@10: /** rlm@10: * Number of entries in the LocalVariableTypeTable attribute. rlm@10: */ rlm@10: private int localVarTypeCount; rlm@10: rlm@10: /** rlm@10: * The LocalVariableTypeTable attribute. rlm@10: */ rlm@10: private ByteVector localVarType; rlm@10: rlm@10: /** rlm@10: * Number of entries in the LineNumberTable attribute. rlm@10: */ rlm@10: private int lineNumberCount; rlm@10: rlm@10: /** rlm@10: * The LineNumberTable attribute. rlm@10: */ rlm@10: private ByteVector lineNumber; rlm@10: rlm@10: /** rlm@10: * The non standard attributes of the method's code. rlm@10: */ rlm@10: private Attribute cattrs; rlm@10: rlm@10: /** rlm@10: * Indicates if some jump instructions are too small and need to be resized. rlm@10: */ rlm@10: private boolean resize; rlm@10: rlm@10: /** rlm@10: * Indicates if the instructions contain at least one JSR instruction. rlm@10: */ rlm@10: private boolean jsr; rlm@10: rlm@10: // ------------------------------------------------------------------------ rlm@10: rlm@10: /* rlm@10: * Fields for the control flow graph analysis algorithm (used to compute the rlm@10: * maximum stack size). A control flow graph contains one node per "basic rlm@10: * block", and one edge per "jump" from one basic block to another. Each rlm@10: * node (i.e., each basic block) is represented by the Label object that rlm@10: * corresponds to the first instruction of this basic block. Each node also rlm@10: * stores the list of its successors in the graph, as a linked list of Edge rlm@10: * objects. rlm@10: */ rlm@10: rlm@10: /** rlm@10: * Indicates what must be automatically computed. rlm@10: * rlm@10: * @see FRAMES rlm@10: * @see MAXS rlm@10: * @see NOTHING rlm@10: */ rlm@10: private int compute; rlm@10: rlm@10: /** rlm@10: * A list of labels. This list is the list of basic blocks in the method, rlm@10: * i.e. a list of Label objects linked to each other by their rlm@10: * {@link Label#successor} field, in the order they are visited by rlm@10: * {@link visitLabel}, and starting with the first basic block. rlm@10: */ rlm@10: private Label labels; rlm@10: rlm@10: /** rlm@10: * The previous basic block. rlm@10: */ rlm@10: private Label previousBlock; rlm@10: rlm@10: /** rlm@10: * The current basic block. rlm@10: */ rlm@10: private Label currentBlock; rlm@10: rlm@10: /** rlm@10: * The (relative) stack size after the last visited instruction. This size rlm@10: * is relative to the beginning of the current basic block, i.e., the true rlm@10: * stack size after the last visited instruction is equal to the rlm@10: * {@link Label#inputStackTop beginStackSize} of the current basic block rlm@10: * plus stackSize. rlm@10: */ rlm@10: private int stackSize; rlm@10: rlm@10: /** rlm@10: * The (relative) maximum stack size after the last visited instruction. rlm@10: * This size is relative to the beginning of the current basic block, i.e., rlm@10: * the true maximum stack size after the last visited instruction is equal rlm@10: * to the {@link Label#inputStackTop beginStackSize} of the current basic rlm@10: * block plus stackSize. rlm@10: */ rlm@10: private int maxStackSize; rlm@10: rlm@10: // ------------------------------------------------------------------------ rlm@10: // Constructor rlm@10: // ------------------------------------------------------------------------ rlm@10: rlm@10: /** rlm@10: * Constructs a new {@link MethodWriter}. rlm@10: * rlm@10: * @param cw the class writer in which the method must be added. rlm@10: * @param access the method's access flags (see {@link Opcodes}). rlm@10: * @param name the method's name. rlm@10: * @param desc the method's descriptor (see {@link Type}). rlm@10: * @param signature the method's signature. May be null. rlm@10: * @param exceptions the internal names of the method's exceptions. May be rlm@10: * null. rlm@10: * @param computeMaxs true if the maximum stack size and number rlm@10: * of local variables must be automatically computed. rlm@10: * @param computeFrames true if the stack map tables must be rlm@10: * recomputed from scratch. rlm@10: */ rlm@10: MethodWriter( rlm@10: final ClassWriter cw, rlm@10: final int access, rlm@10: final String name, rlm@10: final String desc, rlm@10: final String signature, rlm@10: final String[] exceptions, rlm@10: final boolean computeMaxs, rlm@10: final boolean computeFrames){ rlm@10: if(cw.firstMethod == null) rlm@10: { rlm@10: cw.firstMethod = this; rlm@10: } rlm@10: else rlm@10: { rlm@10: cw.lastMethod.next = this; rlm@10: } rlm@10: cw.lastMethod = this; rlm@10: this.cw = cw; rlm@10: this.access = access; rlm@10: this.name = cw.newUTF8(name); rlm@10: this.desc = cw.newUTF8(desc); rlm@10: this.descriptor = desc; rlm@10: this.signature = signature; rlm@10: if(exceptions != null && exceptions.length > 0) rlm@10: { rlm@10: exceptionCount = exceptions.length; rlm@10: this.exceptions = new int[exceptionCount]; rlm@10: for(int i = 0; i < exceptionCount; ++i) rlm@10: { rlm@10: this.exceptions[i] = cw.newClass(exceptions[i]); rlm@10: } rlm@10: } rlm@10: this.compute = computeFrames ? FRAMES : (computeMaxs ? MAXS : NOTHING); rlm@10: if(computeMaxs || computeFrames) rlm@10: { rlm@10: if(computeFrames && name.equals("")) rlm@10: { rlm@10: this.access |= ACC_CONSTRUCTOR; rlm@10: } rlm@10: // updates maxLocals rlm@10: int size = getArgumentsAndReturnSizes(descriptor) >> 2; rlm@10: if((access & Opcodes.ACC_STATIC) != 0) rlm@10: { rlm@10: --size; rlm@10: } rlm@10: maxLocals = size; rlm@10: // creates and visits the label for the first basic block rlm@10: labels = new Label(); rlm@10: labels.status |= Label.PUSHED; rlm@10: visitLabel(labels); rlm@10: } rlm@10: } rlm@10: rlm@10: // ------------------------------------------------------------------------ rlm@10: // Implementation of the MethodVisitor interface rlm@10: // ------------------------------------------------------------------------ rlm@10: rlm@10: public AnnotationVisitor visitAnnotationDefault(){ rlm@10: annd = new ByteVector(); rlm@10: return new AnnotationWriter(cw, false, annd, null, 0); rlm@10: } rlm@10: rlm@10: public AnnotationVisitor visitAnnotation( rlm@10: final String desc, rlm@10: final boolean visible){ rlm@10: ByteVector bv = new ByteVector(); rlm@10: // write type, and reserve space for values count rlm@10: bv.putShort(cw.newUTF8(desc)).putShort(0); rlm@10: AnnotationWriter aw = new AnnotationWriter(cw, true, bv, bv, 2); rlm@10: if(visible) rlm@10: { rlm@10: aw.next = anns; rlm@10: anns = aw; rlm@10: } rlm@10: else rlm@10: { rlm@10: aw.next = ianns; rlm@10: ianns = aw; rlm@10: } rlm@10: return aw; rlm@10: } rlm@10: rlm@10: public AnnotationVisitor visitParameterAnnotation( rlm@10: final int parameter, rlm@10: final String desc, rlm@10: final boolean visible){ rlm@10: ByteVector bv = new ByteVector(); rlm@10: // write type, and reserve space for values count rlm@10: bv.putShort(cw.newUTF8(desc)).putShort(0); rlm@10: AnnotationWriter aw = new AnnotationWriter(cw, true, bv, bv, 2); rlm@10: if(visible) rlm@10: { rlm@10: if(panns == null) rlm@10: { rlm@10: panns = new AnnotationWriter[Type.getArgumentTypes(descriptor).length]; rlm@10: } rlm@10: aw.next = panns[parameter]; rlm@10: panns[parameter] = aw; rlm@10: } rlm@10: else rlm@10: { rlm@10: if(ipanns == null) rlm@10: { rlm@10: ipanns = new AnnotationWriter[Type.getArgumentTypes(descriptor).length]; rlm@10: } rlm@10: aw.next = ipanns[parameter]; rlm@10: ipanns[parameter] = aw; rlm@10: } rlm@10: return aw; rlm@10: } rlm@10: rlm@10: public void visitAttribute(final Attribute attr){ rlm@10: if(attr.isCodeAttribute()) rlm@10: { rlm@10: attr.next = cattrs; rlm@10: cattrs = attr; rlm@10: } rlm@10: else rlm@10: { rlm@10: attr.next = attrs; rlm@10: attrs = attr; rlm@10: } rlm@10: } rlm@10: rlm@10: public void visitCode(){ rlm@10: } rlm@10: rlm@10: public void visitFrame( rlm@10: final int type, rlm@10: final int nLocal, rlm@10: final Object[] local, rlm@10: final int nStack, rlm@10: final Object[] stack){ rlm@10: if(compute == FRAMES) rlm@10: { rlm@10: return; rlm@10: } rlm@10: rlm@10: if(type == Opcodes.F_NEW) rlm@10: { rlm@10: startFrame(code.length, nLocal, nStack); rlm@10: for(int i = 0; i < nLocal; ++i) rlm@10: { rlm@10: if(local[i] instanceof String) rlm@10: { rlm@10: frame[frameIndex++] = Frame.OBJECT rlm@10: | cw.addType((String) local[i]); rlm@10: } rlm@10: else if(local[i] instanceof Integer) rlm@10: { rlm@10: frame[frameIndex++] = ((Integer) local[i]).intValue(); rlm@10: } rlm@10: else rlm@10: { rlm@10: frame[frameIndex++] = Frame.UNINITIALIZED rlm@10: | cw.addUninitializedType("", rlm@10: ((Label) local[i]).position); rlm@10: } rlm@10: } rlm@10: for(int i = 0; i < nStack; ++i) rlm@10: { rlm@10: if(stack[i] instanceof String) rlm@10: { rlm@10: frame[frameIndex++] = Frame.OBJECT rlm@10: | cw.addType((String) stack[i]); rlm@10: } rlm@10: else if(stack[i] instanceof Integer) rlm@10: { rlm@10: frame[frameIndex++] = ((Integer) stack[i]).intValue(); rlm@10: } rlm@10: else rlm@10: { rlm@10: frame[frameIndex++] = Frame.UNINITIALIZED rlm@10: | cw.addUninitializedType("", rlm@10: ((Label) stack[i]).position); rlm@10: } rlm@10: } rlm@10: endFrame(); rlm@10: } rlm@10: else rlm@10: { rlm@10: int delta; rlm@10: if(stackMap == null) rlm@10: { rlm@10: stackMap = new ByteVector(); rlm@10: delta = code.length; rlm@10: } rlm@10: else rlm@10: { rlm@10: delta = code.length - previousFrameOffset - 1; rlm@10: } rlm@10: rlm@10: switch(type) rlm@10: { rlm@10: case Opcodes.F_FULL: rlm@10: stackMap.putByte(FULL_FRAME) rlm@10: .putShort(delta) rlm@10: .putShort(nLocal); rlm@10: for(int i = 0; i < nLocal; ++i) rlm@10: { rlm@10: writeFrameType(local[i]); rlm@10: } rlm@10: stackMap.putShort(nStack); rlm@10: for(int i = 0; i < nStack; ++i) rlm@10: { rlm@10: writeFrameType(stack[i]); rlm@10: } rlm@10: break; rlm@10: case Opcodes.F_APPEND: rlm@10: stackMap.putByte(SAME_FRAME_EXTENDED + nLocal) rlm@10: .putShort(delta); rlm@10: for(int i = 0; i < nLocal; ++i) rlm@10: { rlm@10: writeFrameType(local[i]); rlm@10: } rlm@10: break; rlm@10: case Opcodes.F_CHOP: rlm@10: stackMap.putByte(SAME_FRAME_EXTENDED - nLocal) rlm@10: .putShort(delta); rlm@10: break; rlm@10: case Opcodes.F_SAME: rlm@10: if(delta < 64) rlm@10: { rlm@10: stackMap.putByte(delta); rlm@10: } rlm@10: else rlm@10: { rlm@10: stackMap.putByte(SAME_FRAME_EXTENDED).putShort(delta); rlm@10: } rlm@10: break; rlm@10: case Opcodes.F_SAME1: rlm@10: if(delta < 64) rlm@10: { rlm@10: stackMap.putByte(SAME_LOCALS_1_STACK_ITEM_FRAME + delta); rlm@10: } rlm@10: else rlm@10: { rlm@10: stackMap.putByte(SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) rlm@10: .putShort(delta); rlm@10: } rlm@10: writeFrameType(stack[0]); rlm@10: break; rlm@10: } rlm@10: rlm@10: previousFrameOffset = code.length; rlm@10: ++frameCount; rlm@10: } rlm@10: } rlm@10: rlm@10: public void visitInsn(final int opcode){ rlm@10: // adds the instruction to the bytecode of the method rlm@10: code.putByte(opcode); rlm@10: // update currentBlock rlm@10: // Label currentBlock = this.currentBlock; rlm@10: if(currentBlock != null) rlm@10: { rlm@10: if(compute == FRAMES) rlm@10: { rlm@10: currentBlock.frame.execute(opcode, 0, null, null); rlm@10: } rlm@10: else rlm@10: { rlm@10: // updates current and max stack sizes rlm@10: int size = stackSize + Frame.SIZE[opcode]; rlm@10: if(size > maxStackSize) rlm@10: { rlm@10: maxStackSize = size; rlm@10: } rlm@10: stackSize = size; rlm@10: } rlm@10: // if opcode == ATHROW or xRETURN, ends current block (no successor) rlm@10: if((opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN) rlm@10: || opcode == Opcodes.ATHROW) rlm@10: { rlm@10: noSuccessor(); rlm@10: } rlm@10: } rlm@10: } rlm@10: rlm@10: public void visitIntInsn(final int opcode, final int operand){ rlm@10: // Label currentBlock = this.currentBlock; rlm@10: if(currentBlock != null) rlm@10: { rlm@10: if(compute == FRAMES) rlm@10: { rlm@10: currentBlock.frame.execute(opcode, operand, null, null); rlm@10: } rlm@10: else if(opcode != Opcodes.NEWARRAY) rlm@10: { rlm@10: // updates current and max stack sizes only for NEWARRAY rlm@10: // (stack size variation = 0 for BIPUSH or SIPUSH) rlm@10: int size = stackSize + 1; rlm@10: if(size > maxStackSize) rlm@10: { rlm@10: maxStackSize = size; rlm@10: } rlm@10: stackSize = size; rlm@10: } rlm@10: } rlm@10: // adds the instruction to the bytecode of the method rlm@10: if(opcode == Opcodes.SIPUSH) rlm@10: { rlm@10: code.put12(opcode, operand); rlm@10: } rlm@10: else rlm@10: { // BIPUSH or NEWARRAY rlm@10: code.put11(opcode, operand); rlm@10: } rlm@10: } rlm@10: rlm@10: public void visitVarInsn(final int opcode, final int var){ rlm@10: // Label currentBlock = this.currentBlock; rlm@10: if(currentBlock != null) rlm@10: { rlm@10: if(compute == FRAMES) rlm@10: { rlm@10: currentBlock.frame.execute(opcode, var, null, null); rlm@10: } rlm@10: else rlm@10: { rlm@10: // updates current and max stack sizes rlm@10: if(opcode == Opcodes.RET) rlm@10: { rlm@10: // no stack change, but end of current block (no successor) rlm@10: currentBlock.status |= Label.RET; rlm@10: // save 'stackSize' here for future use rlm@10: // (see {@link #findSubroutineSuccessors}) rlm@10: currentBlock.inputStackTop = stackSize; rlm@10: noSuccessor(); rlm@10: } rlm@10: else rlm@10: { // xLOAD or xSTORE rlm@10: int size = stackSize + Frame.SIZE[opcode]; rlm@10: if(size > maxStackSize) rlm@10: { rlm@10: maxStackSize = size; rlm@10: } rlm@10: stackSize = size; rlm@10: } rlm@10: } rlm@10: } rlm@10: if(compute != NOTHING) rlm@10: { rlm@10: // updates max locals rlm@10: int n; rlm@10: if(opcode == Opcodes.LLOAD || opcode == Opcodes.DLOAD rlm@10: || opcode == Opcodes.LSTORE || opcode == Opcodes.DSTORE) rlm@10: { rlm@10: n = var + 2; rlm@10: } rlm@10: else rlm@10: { rlm@10: n = var + 1; rlm@10: } rlm@10: if(n > maxLocals) rlm@10: { rlm@10: maxLocals = n; rlm@10: } rlm@10: } rlm@10: // adds the instruction to the bytecode of the method rlm@10: if(var < 4 && opcode != Opcodes.RET) rlm@10: { rlm@10: int opt; rlm@10: if(opcode < Opcodes.ISTORE) rlm@10: { rlm@10: /* ILOAD_0 */ rlm@10: opt = 26 + ((opcode - Opcodes.ILOAD) << 2) + var; rlm@10: } rlm@10: else rlm@10: { rlm@10: /* ISTORE_0 */ rlm@10: opt = 59 + ((opcode - Opcodes.ISTORE) << 2) + var; rlm@10: } rlm@10: code.putByte(opt); rlm@10: } rlm@10: else if(var >= 256) rlm@10: { rlm@10: code.putByte(196 /* WIDE */).put12(opcode, var); rlm@10: } rlm@10: else rlm@10: { rlm@10: code.put11(opcode, var); rlm@10: } rlm@10: if(opcode >= Opcodes.ISTORE && compute == FRAMES && handlerCount > 0) rlm@10: { rlm@10: visitLabel(new Label()); rlm@10: } rlm@10: } rlm@10: rlm@10: public void visitTypeInsn(final int opcode, final String desc){ rlm@10: Item i = cw.newClassItem(desc); rlm@10: // Label currentBlock = this.currentBlock; rlm@10: if(currentBlock != null) rlm@10: { rlm@10: if(compute == FRAMES) rlm@10: { rlm@10: currentBlock.frame.execute(opcode, code.length, cw, i); rlm@10: } rlm@10: else if(opcode == Opcodes.NEW) rlm@10: { rlm@10: // updates current and max stack sizes only if opcode == NEW rlm@10: // (no stack change for ANEWARRAY, CHECKCAST, INSTANCEOF) rlm@10: int size = stackSize + 1; rlm@10: if(size > maxStackSize) rlm@10: { rlm@10: maxStackSize = size; rlm@10: } rlm@10: stackSize = size; rlm@10: } rlm@10: } rlm@10: // adds the instruction to the bytecode of the method rlm@10: code.put12(opcode, i.index); rlm@10: } rlm@10: rlm@10: public void visitFieldInsn( rlm@10: final int opcode, rlm@10: final String owner, rlm@10: final String name, rlm@10: final String desc){ rlm@10: Item i = cw.newFieldItem(owner, name, desc); rlm@10: // Label currentBlock = this.currentBlock; rlm@10: if(currentBlock != null) rlm@10: { rlm@10: if(compute == FRAMES) rlm@10: { rlm@10: currentBlock.frame.execute(opcode, 0, cw, i); rlm@10: } rlm@10: else rlm@10: { rlm@10: int size; rlm@10: // computes the stack size variation rlm@10: char c = desc.charAt(0); rlm@10: switch(opcode) rlm@10: { rlm@10: case Opcodes.GETSTATIC: rlm@10: size = stackSize + (c == 'D' || c == 'J' ? 2 : 1); rlm@10: break; rlm@10: case Opcodes.PUTSTATIC: rlm@10: size = stackSize + (c == 'D' || c == 'J' ? -2 : -1); rlm@10: break; rlm@10: case Opcodes.GETFIELD: rlm@10: size = stackSize + (c == 'D' || c == 'J' ? 1 : 0); rlm@10: break; rlm@10: // case Constants.PUTFIELD: rlm@10: default: rlm@10: size = stackSize + (c == 'D' || c == 'J' ? -3 : -2); rlm@10: break; rlm@10: } rlm@10: // updates current and max stack sizes rlm@10: if(size > maxStackSize) rlm@10: { rlm@10: maxStackSize = size; rlm@10: } rlm@10: stackSize = size; rlm@10: } rlm@10: } rlm@10: // adds the instruction to the bytecode of the method rlm@10: code.put12(opcode, i.index); rlm@10: } rlm@10: rlm@10: public void visitMethodInsn( rlm@10: final int opcode, rlm@10: final String owner, rlm@10: final String name, rlm@10: final String desc){ rlm@10: boolean itf = opcode == Opcodes.INVOKEINTERFACE; rlm@10: Item i = cw.newMethodItem(owner, name, desc, itf); rlm@10: int argSize = i.intVal; rlm@10: // Label currentBlock = this.currentBlock; rlm@10: if(currentBlock != null) rlm@10: { rlm@10: if(compute == FRAMES) rlm@10: { rlm@10: currentBlock.frame.execute(opcode, 0, cw, i); rlm@10: } rlm@10: else rlm@10: { rlm@10: /* rlm@10: * computes the stack size variation. In order not to recompute rlm@10: * several times this variation for the same Item, we use the rlm@10: * intVal field of this item to store this variation, once it rlm@10: * has been computed. More precisely this intVal field stores rlm@10: * the sizes of the arguments and of the return value rlm@10: * corresponding to desc. rlm@10: */ rlm@10: if(argSize == 0) rlm@10: { rlm@10: // the above sizes have not been computed yet, rlm@10: // so we compute them... rlm@10: argSize = getArgumentsAndReturnSizes(desc); rlm@10: // ... and we save them in order rlm@10: // not to recompute them in the future rlm@10: i.intVal = argSize; rlm@10: } rlm@10: int size; rlm@10: if(opcode == Opcodes.INVOKESTATIC) rlm@10: { rlm@10: size = stackSize - (argSize >> 2) + (argSize & 0x03) + 1; rlm@10: } rlm@10: else rlm@10: { rlm@10: size = stackSize - (argSize >> 2) + (argSize & 0x03); rlm@10: } rlm@10: // updates current and max stack sizes rlm@10: if(size > maxStackSize) rlm@10: { rlm@10: maxStackSize = size; rlm@10: } rlm@10: stackSize = size; rlm@10: } rlm@10: } rlm@10: // adds the instruction to the bytecode of the method rlm@10: if(itf) rlm@10: { rlm@10: if(argSize == 0) rlm@10: { rlm@10: argSize = getArgumentsAndReturnSizes(desc); rlm@10: i.intVal = argSize; rlm@10: } rlm@10: code.put12(Opcodes.INVOKEINTERFACE, i.index).put11(argSize >> 2, 0); rlm@10: } rlm@10: else rlm@10: { rlm@10: code.put12(opcode, i.index); rlm@10: } rlm@10: } rlm@10: rlm@10: public void visitJumpInsn(final int opcode, final Label label){ rlm@10: Label nextInsn = null; rlm@10: // Label currentBlock = this.currentBlock; rlm@10: if(currentBlock != null) rlm@10: { rlm@10: if(compute == FRAMES) rlm@10: { rlm@10: currentBlock.frame.execute(opcode, 0, null, null); rlm@10: // 'label' is the target of a jump instruction rlm@10: label.getFirst().status |= Label.TARGET; rlm@10: // adds 'label' as a successor of this basic block rlm@10: addSuccessor(Edge.NORMAL, label); rlm@10: if(opcode != Opcodes.GOTO) rlm@10: { rlm@10: // creates a Label for the next basic block rlm@10: nextInsn = new Label(); rlm@10: } rlm@10: } rlm@10: else rlm@10: { rlm@10: if(opcode == Opcodes.JSR) rlm@10: { rlm@10: jsr = true; rlm@10: currentBlock.status |= Label.JSR; rlm@10: addSuccessor(stackSize + 1, label); rlm@10: // creates a Label for the next basic block rlm@10: nextInsn = new Label(); rlm@10: /* rlm@10: * note that, by construction in this method, a JSR block rlm@10: * has at least two successors in the control flow graph: rlm@10: * the first one leads the next instruction after the JSR, rlm@10: * while the second one leads to the JSR target. rlm@10: */ rlm@10: } rlm@10: else rlm@10: { rlm@10: // updates current stack size (max stack size unchanged rlm@10: // because stack size variation always negative in this rlm@10: // case) rlm@10: stackSize += Frame.SIZE[opcode]; rlm@10: addSuccessor(stackSize, label); rlm@10: } rlm@10: } rlm@10: } rlm@10: // adds the instruction to the bytecode of the method rlm@10: if((label.status & Label.RESOLVED) != 0 rlm@10: && label.position - code.length < Short.MIN_VALUE) rlm@10: { rlm@10: /* rlm@10: * case of a backward jump with an offset < -32768. In this case we rlm@10: * automatically replace GOTO with GOTO_W, JSR with JSR_W and IFxxx rlm@10: * with IFNOTxxx GOTO_W , where IFNOTxxx is the rlm@10: * "opposite" opcode of IFxxx (i.e., IFNE for IFEQ) and where rlm@10: * designates the instruction just after the GOTO_W. rlm@10: */ rlm@10: if(opcode == Opcodes.GOTO) rlm@10: { rlm@10: code.putByte(200); // GOTO_W rlm@10: } rlm@10: else if(opcode == Opcodes.JSR) rlm@10: { rlm@10: code.putByte(201); // JSR_W rlm@10: } rlm@10: else rlm@10: { rlm@10: // if the IF instruction is transformed into IFNOT GOTO_W the rlm@10: // next instruction becomes the target of the IFNOT instruction rlm@10: if(nextInsn != null) rlm@10: { rlm@10: nextInsn.status |= Label.TARGET; rlm@10: } rlm@10: code.putByte(opcode <= 166 rlm@10: ? ((opcode + 1) ^ 1) - 1 rlm@10: : opcode ^ 1); rlm@10: code.putShort(8); // jump offset rlm@10: code.putByte(200); // GOTO_W rlm@10: } rlm@10: label.put(this, code, code.length - 1, true); rlm@10: } rlm@10: else rlm@10: { rlm@10: /* rlm@10: * case of a backward jump with an offset >= -32768, or of a forward rlm@10: * jump with, of course, an unknown offset. In these cases we store rlm@10: * the offset in 2 bytes (which will be increased in rlm@10: * resizeInstructions, if needed). rlm@10: */ rlm@10: code.putByte(opcode); rlm@10: label.put(this, code, code.length - 1, false); rlm@10: } rlm@10: if(currentBlock != null) rlm@10: { rlm@10: if(nextInsn != null) rlm@10: { rlm@10: // if the jump instruction is not a GOTO, the next instruction rlm@10: // is also a successor of this instruction. Calling visitLabel rlm@10: // adds the label of this next instruction as a successor of the rlm@10: // current block, and starts a new basic block rlm@10: visitLabel(nextInsn); rlm@10: } rlm@10: if(opcode == Opcodes.GOTO) rlm@10: { rlm@10: noSuccessor(); rlm@10: } rlm@10: } rlm@10: } rlm@10: rlm@10: public void visitLabel(final Label label){ rlm@10: // resolves previous forward references to label, if any rlm@10: resize |= label.resolve(this, code.length, code.data); rlm@10: // updates currentBlock rlm@10: if((label.status & Label.DEBUG) != 0) rlm@10: { rlm@10: return; rlm@10: } rlm@10: if(compute == FRAMES) rlm@10: { rlm@10: if(currentBlock != null) rlm@10: { rlm@10: if(label.position == currentBlock.position) rlm@10: { rlm@10: // successive labels, do not start a new basic block rlm@10: currentBlock.status |= (label.status & Label.TARGET); rlm@10: label.frame = currentBlock.frame; rlm@10: return; rlm@10: } rlm@10: // ends current block (with one new successor) rlm@10: addSuccessor(Edge.NORMAL, label); rlm@10: } rlm@10: // begins a new current block rlm@10: currentBlock = label; rlm@10: if(label.frame == null) rlm@10: { rlm@10: label.frame = new Frame(); rlm@10: label.frame.owner = label; rlm@10: } rlm@10: // updates the basic block list rlm@10: if(previousBlock != null) rlm@10: { rlm@10: if(label.position == previousBlock.position) rlm@10: { rlm@10: previousBlock.status |= (label.status & Label.TARGET); rlm@10: label.frame = previousBlock.frame; rlm@10: currentBlock = previousBlock; rlm@10: return; rlm@10: } rlm@10: previousBlock.successor = label; rlm@10: } rlm@10: previousBlock = label; rlm@10: } rlm@10: else if(compute == MAXS) rlm@10: { rlm@10: if(currentBlock != null) rlm@10: { rlm@10: // ends current block (with one new successor) rlm@10: currentBlock.outputStackMax = maxStackSize; rlm@10: addSuccessor(stackSize, label); rlm@10: } rlm@10: // begins a new current block rlm@10: currentBlock = label; rlm@10: // resets the relative current and max stack sizes rlm@10: stackSize = 0; rlm@10: maxStackSize = 0; rlm@10: // updates the basic block list rlm@10: if(previousBlock != null) rlm@10: { rlm@10: previousBlock.successor = label; rlm@10: } rlm@10: previousBlock = label; rlm@10: } rlm@10: } rlm@10: rlm@10: public void visitLdcInsn(final Object cst){ rlm@10: Item i = cw.newConstItem(cst); rlm@10: // Label currentBlock = this.currentBlock; rlm@10: if(currentBlock != null) rlm@10: { rlm@10: if(compute == FRAMES) rlm@10: { rlm@10: currentBlock.frame.execute(Opcodes.LDC, 0, cw, i); rlm@10: } rlm@10: else rlm@10: { rlm@10: int size; rlm@10: // computes the stack size variation rlm@10: if(i.type == ClassWriter.LONG || i.type == ClassWriter.DOUBLE) rlm@10: { rlm@10: size = stackSize + 2; rlm@10: } rlm@10: else rlm@10: { rlm@10: size = stackSize + 1; rlm@10: } rlm@10: // updates current and max stack sizes rlm@10: if(size > maxStackSize) rlm@10: { rlm@10: maxStackSize = size; rlm@10: } rlm@10: stackSize = size; rlm@10: } rlm@10: } rlm@10: // adds the instruction to the bytecode of the method rlm@10: int index = i.index; rlm@10: if(i.type == ClassWriter.LONG || i.type == ClassWriter.DOUBLE) rlm@10: { rlm@10: code.put12(20 /* LDC2_W */, index); rlm@10: } rlm@10: else if(index >= 256) rlm@10: { rlm@10: code.put12(19 /* LDC_W */, index); rlm@10: } rlm@10: else rlm@10: { rlm@10: code.put11(Opcodes.LDC, index); rlm@10: } rlm@10: } rlm@10: rlm@10: public void visitIincInsn(final int var, final int increment){ rlm@10: if(currentBlock != null) rlm@10: { rlm@10: if(compute == FRAMES) rlm@10: { rlm@10: currentBlock.frame.execute(Opcodes.IINC, var, null, null); rlm@10: } rlm@10: } rlm@10: if(compute != NOTHING) rlm@10: { rlm@10: // updates max locals rlm@10: int n = var + 1; rlm@10: if(n > maxLocals) rlm@10: { rlm@10: maxLocals = n; rlm@10: } rlm@10: } rlm@10: // adds the instruction to the bytecode of the method rlm@10: if((var > 255) || (increment > 127) || (increment < -128)) rlm@10: { rlm@10: code.putByte(196 /* WIDE */) rlm@10: .put12(Opcodes.IINC, var) rlm@10: .putShort(increment); rlm@10: } rlm@10: else rlm@10: { rlm@10: code.putByte(Opcodes.IINC).put11(var, increment); rlm@10: } rlm@10: } rlm@10: rlm@10: public void visitTableSwitchInsn( rlm@10: final int min, rlm@10: final int max, rlm@10: final Label dflt, rlm@10: final Label labels[]){ rlm@10: // adds the instruction to the bytecode of the method rlm@10: int source = code.length; rlm@10: code.putByte(Opcodes.TABLESWITCH); rlm@10: code.length += (4 - code.length % 4) % 4; rlm@10: dflt.put(this, code, source, true); rlm@10: code.putInt(min).putInt(max); rlm@10: for(int i = 0; i < labels.length; ++i) rlm@10: { rlm@10: labels[i].put(this, code, source, true); rlm@10: } rlm@10: // updates currentBlock rlm@10: visitSwitchInsn(dflt, labels); rlm@10: } rlm@10: rlm@10: public void visitLookupSwitchInsn( rlm@10: final Label dflt, rlm@10: final int keys[], rlm@10: final Label labels[]){ rlm@10: // adds the instruction to the bytecode of the method rlm@10: int source = code.length; rlm@10: code.putByte(Opcodes.LOOKUPSWITCH); rlm@10: code.length += (4 - code.length % 4) % 4; rlm@10: dflt.put(this, code, source, true); rlm@10: code.putInt(labels.length); rlm@10: for(int i = 0; i < labels.length; ++i) rlm@10: { rlm@10: code.putInt(keys[i]); rlm@10: labels[i].put(this, code, source, true); rlm@10: } rlm@10: // updates currentBlock rlm@10: visitSwitchInsn(dflt, labels); rlm@10: } rlm@10: rlm@10: private void visitSwitchInsn(final Label dflt, final Label[] labels){ rlm@10: // Label currentBlock = this.currentBlock; rlm@10: if(currentBlock != null) rlm@10: { rlm@10: if(compute == FRAMES) rlm@10: { rlm@10: currentBlock.frame.execute(Opcodes.LOOKUPSWITCH, 0, null, null); rlm@10: // adds current block successors rlm@10: addSuccessor(Edge.NORMAL, dflt); rlm@10: dflt.getFirst().status |= Label.TARGET; rlm@10: for(int i = 0; i < labels.length; ++i) rlm@10: { rlm@10: addSuccessor(Edge.NORMAL, labels[i]); rlm@10: labels[i].getFirst().status |= Label.TARGET; rlm@10: } rlm@10: } rlm@10: else rlm@10: { rlm@10: // updates current stack size (max stack size unchanged) rlm@10: --stackSize; rlm@10: // adds current block successors rlm@10: addSuccessor(stackSize, dflt); rlm@10: for(int i = 0; i < labels.length; ++i) rlm@10: { rlm@10: addSuccessor(stackSize, labels[i]); rlm@10: } rlm@10: } rlm@10: // ends current block rlm@10: noSuccessor(); rlm@10: } rlm@10: } rlm@10: rlm@10: public void visitMultiANewArrayInsn(final String desc, final int dims){ rlm@10: Item i = cw.newClassItem(desc); rlm@10: // Label currentBlock = this.currentBlock; rlm@10: if(currentBlock != null) rlm@10: { rlm@10: if(compute == FRAMES) rlm@10: { rlm@10: currentBlock.frame.execute(Opcodes.MULTIANEWARRAY, dims, cw, i); rlm@10: } rlm@10: else rlm@10: { rlm@10: // updates current stack size (max stack size unchanged because rlm@10: // stack size variation always negative or null) rlm@10: stackSize += 1 - dims; rlm@10: } rlm@10: } rlm@10: // adds the instruction to the bytecode of the method rlm@10: code.put12(Opcodes.MULTIANEWARRAY, i.index).putByte(dims); rlm@10: } rlm@10: rlm@10: public void visitTryCatchBlock( rlm@10: final Label start, rlm@10: final Label end, rlm@10: final Label handler, rlm@10: final String type){ rlm@10: ++handlerCount; rlm@10: Handler h = new Handler(); rlm@10: h.start = start; rlm@10: h.end = end; rlm@10: h.handler = handler; rlm@10: h.desc = type; rlm@10: h.type = type != null ? cw.newClass(type) : 0; rlm@10: if(lastHandler == null) rlm@10: { rlm@10: firstHandler = h; rlm@10: } rlm@10: else rlm@10: { rlm@10: lastHandler.next = h; rlm@10: } rlm@10: lastHandler = h; rlm@10: } rlm@10: rlm@10: public void visitLocalVariable( rlm@10: final String name, rlm@10: final String desc, rlm@10: final String signature, rlm@10: final Label start, rlm@10: final Label end, rlm@10: final int index){ rlm@10: if(signature != null) rlm@10: { rlm@10: if(localVarType == null) rlm@10: { rlm@10: localVarType = new ByteVector(); rlm@10: } rlm@10: ++localVarTypeCount; rlm@10: localVarType.putShort(start.position) rlm@10: .putShort(end.position - start.position) rlm@10: .putShort(cw.newUTF8(name)) rlm@10: .putShort(cw.newUTF8(signature)) rlm@10: .putShort(index); rlm@10: } rlm@10: if(localVar == null) rlm@10: { rlm@10: localVar = new ByteVector(); rlm@10: } rlm@10: ++localVarCount; rlm@10: localVar.putShort(start.position) rlm@10: .putShort(end.position - start.position) rlm@10: .putShort(cw.newUTF8(name)) rlm@10: .putShort(cw.newUTF8(desc)) rlm@10: .putShort(index); rlm@10: if(compute != NOTHING) rlm@10: { rlm@10: // updates max locals rlm@10: char c = desc.charAt(0); rlm@10: int n = index + (c == 'J' || c == 'D' ? 2 : 1); rlm@10: if(n > maxLocals) rlm@10: { rlm@10: maxLocals = n; rlm@10: } rlm@10: } rlm@10: } rlm@10: rlm@10: public void visitLineNumber(final int line, final Label start){ rlm@10: if(lineNumber == null) rlm@10: { rlm@10: lineNumber = new ByteVector(); rlm@10: } rlm@10: ++lineNumberCount; rlm@10: lineNumber.putShort(start.position); rlm@10: lineNumber.putShort(line); rlm@10: } rlm@10: rlm@10: public void visitMaxs(final int maxStack, final int maxLocals){ rlm@10: if(compute == FRAMES) rlm@10: { rlm@10: // completes the control flow graph with exception handler blocks rlm@10: Handler handler = firstHandler; rlm@10: while(handler != null) rlm@10: { rlm@10: Label l = handler.start.getFirst(); rlm@10: Label h = handler.handler.getFirst(); rlm@10: Label e = handler.end.getFirst(); rlm@10: // computes the kind of the edges to 'h' rlm@10: String t = handler.desc == null rlm@10: ? "java/lang/Throwable" rlm@10: : handler.desc; rlm@10: int kind = Frame.OBJECT | cw.addType(t); rlm@10: // h is an exception handler rlm@10: h.status |= Label.TARGET; rlm@10: // adds 'h' as a successor of labels between 'start' and 'end' rlm@10: while(l != e) rlm@10: { rlm@10: // creates an edge to 'h' rlm@10: Edge b = new Edge(); rlm@10: b.info = kind; rlm@10: b.successor = h; rlm@10: // adds it to the successors of 'l' rlm@10: b.next = l.successors; rlm@10: l.successors = b; rlm@10: // goes to the next label rlm@10: l = l.successor; rlm@10: } rlm@10: handler = handler.next; rlm@10: } rlm@10: rlm@10: // creates and visits the first (implicit) frame rlm@10: Frame f = labels.frame; rlm@10: Type[] args = Type.getArgumentTypes(descriptor); rlm@10: f.initInputFrame(cw, access, args, this.maxLocals); rlm@10: visitFrame(f); rlm@10: rlm@10: /* rlm@10: * fix point algorithm: mark the first basic block as 'changed' rlm@10: * (i.e. put it in the 'changed' list) and, while there are changed rlm@10: * basic blocks, choose one, mark it as unchanged, and update its rlm@10: * successors (which can be changed in the process). rlm@10: */ rlm@10: int max = 0; rlm@10: Label changed = labels; rlm@10: while(changed != null) rlm@10: { rlm@10: // removes a basic block from the list of changed basic blocks rlm@10: Label l = changed; rlm@10: changed = changed.next; rlm@10: l.next = null; rlm@10: f = l.frame; rlm@10: // a reacheable jump target must be stored in the stack map rlm@10: if((l.status & Label.TARGET) != 0) rlm@10: { rlm@10: l.status |= Label.STORE; rlm@10: } rlm@10: // all visited labels are reacheable, by definition rlm@10: l.status |= Label.REACHABLE; rlm@10: // updates the (absolute) maximum stack size rlm@10: int blockMax = f.inputStack.length + l.outputStackMax; rlm@10: if(blockMax > max) rlm@10: { rlm@10: max = blockMax; rlm@10: } rlm@10: // updates the successors of the current basic block rlm@10: Edge e = l.successors; rlm@10: while(e != null) rlm@10: { rlm@10: Label n = e.successor.getFirst(); rlm@10: boolean change = f.merge(cw, n.frame, e.info); rlm@10: if(change && n.next == null) rlm@10: { rlm@10: // if n has changed and is not already in the 'changed' rlm@10: // list, adds it to this list rlm@10: n.next = changed; rlm@10: changed = n; rlm@10: } rlm@10: e = e.next; rlm@10: } rlm@10: } rlm@10: this.maxStack = max; rlm@10: rlm@10: // visits all the frames that must be stored in the stack map rlm@10: Label l = labels; rlm@10: while(l != null) rlm@10: { rlm@10: f = l.frame; rlm@10: if((l.status & Label.STORE) != 0) rlm@10: { rlm@10: visitFrame(f); rlm@10: } rlm@10: if((l.status & Label.REACHABLE) == 0) rlm@10: { rlm@10: // finds start and end of dead basic block rlm@10: Label k = l.successor; rlm@10: int start = l.position; rlm@10: int end = (k == null ? code.length : k.position) - 1; rlm@10: // if non empty basic block rlm@10: if(end >= start) rlm@10: { rlm@10: // replaces instructions with NOP ... NOP ATHROW rlm@10: for(int i = start; i < end; ++i) rlm@10: { rlm@10: code.data[i] = Opcodes.NOP; rlm@10: } rlm@10: code.data[end] = (byte) Opcodes.ATHROW; rlm@10: // emits a frame for this unreachable block rlm@10: startFrame(start, 0, 1); rlm@10: frame[frameIndex++] = Frame.OBJECT rlm@10: | cw.addType("java/lang/Throwable"); rlm@10: endFrame(); rlm@10: } rlm@10: } rlm@10: l = l.successor; rlm@10: } rlm@10: } rlm@10: else if(compute == MAXS) rlm@10: { rlm@10: // completes the control flow graph with exception handler blocks rlm@10: Handler handler = firstHandler; rlm@10: while(handler != null) rlm@10: { rlm@10: Label l = handler.start; rlm@10: Label h = handler.handler; rlm@10: Label e = handler.end; rlm@10: // adds 'h' as a successor of labels between 'start' and 'end' rlm@10: while(l != e) rlm@10: { rlm@10: // creates an edge to 'h' rlm@10: Edge b = new Edge(); rlm@10: b.info = Edge.EXCEPTION; rlm@10: b.successor = h; rlm@10: // adds it to the successors of 'l' rlm@10: if((l.status & Label.JSR) != 0) rlm@10: { rlm@10: // if l is a JSR block, adds b after the first two edges rlm@10: // to preserve the hypothesis about JSR block successors rlm@10: // order (see {@link #visitJumpInsn}) rlm@10: b.next = l.successors.next.next; rlm@10: l.successors.next.next = b; rlm@10: } rlm@10: else rlm@10: { rlm@10: b.next = l.successors; rlm@10: l.successors = b; rlm@10: } rlm@10: // goes to the next label rlm@10: l = l.successor; rlm@10: } rlm@10: handler = handler.next; rlm@10: } rlm@10: rlm@10: if(jsr) rlm@10: { rlm@10: // completes the control flow graph with the RET successors rlm@10: /* rlm@10: * first step: finds the subroutines. This step determines, for rlm@10: * each basic block, to which subroutine(s) it belongs, and rlm@10: * stores this set as a bit set in the {@link Label#status} rlm@10: * field. Subroutines are numbered with powers of two, from rlm@10: * 0x1000 to 0x80000000 (so there must be at most 20 subroutines rlm@10: * in a method). rlm@10: */ rlm@10: // finds the basic blocks that belong to the "main" subroutine rlm@10: int id = 0x1000; rlm@10: findSubroutine(labels, id); rlm@10: // finds the basic blocks that belong to the real subroutines rlm@10: Label l = labels; rlm@10: while(l != null) rlm@10: { rlm@10: if((l.status & Label.JSR) != 0) rlm@10: { rlm@10: // the subroutine is defined by l's TARGET, not by l rlm@10: Label subroutine = l.successors.next.successor; rlm@10: // if this subroutine does not have an id yet... rlm@10: if((subroutine.status & ~0xFFF) == 0) rlm@10: { rlm@10: // ...assigns it a new id and finds its basic blocks rlm@10: id = id << 1; rlm@10: findSubroutine(subroutine, id); rlm@10: } rlm@10: } rlm@10: l = l.successor; rlm@10: } rlm@10: // second step: finds the successors of RET blocks rlm@10: findSubroutineSuccessors(0x1000, new Label[10], 0); rlm@10: } rlm@10: rlm@10: /* rlm@10: * control flow analysis algorithm: while the block stack is not rlm@10: * empty, pop a block from this stack, update the max stack size, rlm@10: * compute the true (non relative) begin stack size of the rlm@10: * successors of this block, and push these successors onto the rlm@10: * stack (unless they have already been pushed onto the stack). rlm@10: * Note: by hypothesis, the {@link Label#inputStackTop} of the rlm@10: * blocks in the block stack are the true (non relative) beginning rlm@10: * stack sizes of these blocks. rlm@10: */ rlm@10: int max = 0; rlm@10: Label stack = labels; rlm@10: while(stack != null) rlm@10: { rlm@10: // pops a block from the stack rlm@10: Label l = stack; rlm@10: stack = stack.next; rlm@10: // computes the true (non relative) max stack size of this block rlm@10: int start = l.inputStackTop; rlm@10: int blockMax = start + l.outputStackMax; rlm@10: // updates the global max stack size rlm@10: if(blockMax > max) rlm@10: { rlm@10: max = blockMax; rlm@10: } rlm@10: // analyses the successors of the block rlm@10: Edge b = l.successors; rlm@10: if((l.status & Label.JSR) != 0) rlm@10: { rlm@10: // ignores the first edge of JSR blocks (virtual successor) rlm@10: b = b.next; rlm@10: } rlm@10: while(b != null) rlm@10: { rlm@10: l = b.successor; rlm@10: // if this successor has not already been pushed... rlm@10: if((l.status & Label.PUSHED) == 0) rlm@10: { rlm@10: // computes its true beginning stack size... rlm@10: l.inputStackTop = b.info == Edge.EXCEPTION ? 1 : start rlm@10: + b.info; rlm@10: // ...and pushes it onto the stack rlm@10: l.status |= Label.PUSHED; rlm@10: l.next = stack; rlm@10: stack = l; rlm@10: } rlm@10: b = b.next; rlm@10: } rlm@10: } rlm@10: this.maxStack = max; rlm@10: } rlm@10: else rlm@10: { rlm@10: this.maxStack = maxStack; rlm@10: this.maxLocals = maxLocals; rlm@10: } rlm@10: } rlm@10: rlm@10: public void visitEnd(){ rlm@10: } rlm@10: rlm@10: // ------------------------------------------------------------------------ rlm@10: // Utility methods: control flow analysis algorithm rlm@10: // ------------------------------------------------------------------------ rlm@10: rlm@10: /** rlm@10: * Computes the size of the arguments and of the return value of a method. rlm@10: * rlm@10: * @param desc the descriptor of a method. rlm@10: * @return the size of the arguments of the method (plus one for the rlm@10: * implicit this argument), argSize, and the size of its return rlm@10: * value, retSize, packed into a single int i = rlm@10: * (argSize << 2) | retSize (argSize is therefore equal rlm@10: * to i >> 2, and retSize to i & 0x03). rlm@10: */ rlm@10: static int getArgumentsAndReturnSizes(final String desc){ rlm@10: int n = 1; rlm@10: int c = 1; rlm@10: while(true) rlm@10: { rlm@10: char car = desc.charAt(c++); rlm@10: if(car == ')') rlm@10: { rlm@10: car = desc.charAt(c); rlm@10: return n << 2 rlm@10: | (car == 'V' ? 0 : (car == 'D' || car == 'J' ? 2 : 1)); rlm@10: } rlm@10: else if(car == 'L') rlm@10: { rlm@10: while(desc.charAt(c++) != ';') rlm@10: { rlm@10: } rlm@10: n += 1; rlm@10: } rlm@10: else if(car == '[') rlm@10: { rlm@10: while((car = desc.charAt(c)) == '[') rlm@10: { rlm@10: ++c; rlm@10: } rlm@10: if(car == 'D' || car == 'J') rlm@10: { rlm@10: n -= 1; rlm@10: } rlm@10: } rlm@10: else if(car == 'D' || car == 'J') rlm@10: { rlm@10: n += 2; rlm@10: } rlm@10: else rlm@10: { rlm@10: n += 1; rlm@10: } rlm@10: } rlm@10: } rlm@10: rlm@10: /** rlm@10: * Adds a successor to the {@link #currentBlock currentBlock} block. rlm@10: * rlm@10: * @param info information about the control flow edge to be added. rlm@10: * @param successor the successor block to be added to the current block. rlm@10: */ rlm@10: private void addSuccessor(final int info, final Label successor){ rlm@10: // creates and initializes an Edge object... rlm@10: Edge b = new Edge(); rlm@10: b.info = info; rlm@10: b.successor = successor; rlm@10: // ...and adds it to the successor list of the currentBlock block rlm@10: b.next = currentBlock.successors; rlm@10: currentBlock.successors = b; rlm@10: } rlm@10: rlm@10: /** rlm@10: * Ends the current basic block. This method must be used in the case where rlm@10: * the current basic block does not have any successor. rlm@10: */ rlm@10: private void noSuccessor(){ rlm@10: if(compute == FRAMES) rlm@10: { rlm@10: Label l = new Label(); rlm@10: l.frame = new Frame(); rlm@10: l.frame.owner = l; rlm@10: l.resolve(this, code.length, code.data); rlm@10: previousBlock.successor = l; rlm@10: previousBlock = l; rlm@10: } rlm@10: else rlm@10: { rlm@10: currentBlock.outputStackMax = maxStackSize; rlm@10: } rlm@10: currentBlock = null; rlm@10: } rlm@10: rlm@10: /** rlm@10: * Finds the basic blocks that belong to a given subroutine, and marks these rlm@10: * blocks as belonging to this subroutine (by using {@link Label#status} as rlm@10: * a bit set (see {@link #visitMaxs}). This recursive method follows the rlm@10: * control flow graph to find all the blocks that are reachable from the rlm@10: * given block WITHOUT following any JSR target. rlm@10: * rlm@10: * @param block a block that belongs to the subroutine rlm@10: * @param id the id of this subroutine rlm@10: */ rlm@10: private void findSubroutine(final Label block, final int id){ rlm@10: // if 'block' is already marked as belonging to subroutine 'id', returns rlm@10: if((block.status & id) != 0) rlm@10: { rlm@10: return; rlm@10: } rlm@10: // marks 'block' as belonging to subroutine 'id' rlm@10: block.status |= id; rlm@10: // calls this method recursively on each successor, except JSR targets rlm@10: Edge e = block.successors; rlm@10: while(e != null) rlm@10: { rlm@10: // if 'block' is a JSR block, then 'block.successors.next' leads rlm@10: // to the JSR target (see {@link #visitJumpInsn}) and must therefore rlm@10: // not be followed rlm@10: if((block.status & Label.JSR) == 0 || e != block.successors.next) rlm@10: { rlm@10: findSubroutine(e.successor, id); rlm@10: } rlm@10: e = e.next; rlm@10: } rlm@10: } rlm@10: rlm@10: /** rlm@10: * Finds the successors of the RET blocks of the specified subroutine, and rlm@10: * of any nested subroutine it calls. rlm@10: * rlm@10: * @param id id of the subroutine whose RET block successors must be found. rlm@10: * @param JSRs the JSR blocks that were followed to reach this subroutine. rlm@10: * @param nJSRs number of JSR blocks in the JSRs array. rlm@10: */ rlm@10: private void findSubroutineSuccessors( rlm@10: final int id, rlm@10: final Label[] JSRs, rlm@10: final int nJSRs){ rlm@10: // iterates over all the basic blocks... rlm@10: Label l = labels; rlm@10: while(l != null) rlm@10: { rlm@10: // for those that belong to subroutine 'id'... rlm@10: if((l.status & id) != 0) rlm@10: { rlm@10: if((l.status & Label.JSR) != 0) rlm@10: { rlm@10: // finds the subroutine to which 'l' leads by following the rlm@10: // second edge of l.successors (see {@link #visitJumpInsn}) rlm@10: int nId = l.successors.next.successor.status & ~0xFFF; rlm@10: if(nId != id) rlm@10: { rlm@10: // calls this method recursively with l pushed onto the rlm@10: // JSRs stack to find the successors of the RET blocks rlm@10: // of this nested subroutine 'nId' rlm@10: JSRs[nJSRs] = l; rlm@10: findSubroutineSuccessors(nId, JSRs, nJSRs + 1); rlm@10: } rlm@10: } rlm@10: else if((l.status & Label.RET) != 0) rlm@10: { rlm@10: /* rlm@10: * finds the JSR block in the JSRs stack that corresponds to rlm@10: * this RET block, and updates the successors of this RET rlm@10: * block accordingly. This corresponding JSR is the one that rlm@10: * leads to the subroutine to which the RET block belongs. rlm@10: * But the RET block can belong to several subroutines (if a rlm@10: * nested subroutine returns to its parent subroutine rlm@10: * implicitely, without a RET). So, in fact, the JSR that rlm@10: * corresponds to this RET is the first block in the JSRs rlm@10: * stack, starting from the bottom of the stack, that leads rlm@10: * to a subroutine to which the RET block belongs. rlm@10: */ rlm@10: for(int i = 0; i < nJSRs; ++i) rlm@10: { rlm@10: int JSRstatus = JSRs[i].successors.next.successor.status; rlm@10: if(((JSRstatus & ~0xFFF) & (l.status & ~0xFFF)) != 0) rlm@10: { rlm@10: Edge e = new Edge(); rlm@10: e.info = l.inputStackTop; rlm@10: e.successor = JSRs[i].successors.successor; rlm@10: e.next = l.successors; rlm@10: l.successors = e; rlm@10: break; rlm@10: } rlm@10: } rlm@10: } rlm@10: } rlm@10: l = l.successor; rlm@10: } rlm@10: } rlm@10: rlm@10: // ------------------------------------------------------------------------ rlm@10: // Utility methods: stack map frames rlm@10: // ------------------------------------------------------------------------ rlm@10: rlm@10: /** rlm@10: * Visits a frame that has been computed from scratch. rlm@10: * rlm@10: * @param f the frame that must be visited. rlm@10: */ rlm@10: private void visitFrame(final Frame f){ rlm@10: int i, t; rlm@10: int nTop = 0; rlm@10: int nLocal = 0; rlm@10: int nStack = 0; rlm@10: int[] locals = f.inputLocals; rlm@10: int[] stacks = f.inputStack; rlm@10: // computes the number of locals (ignores TOP types that are just after rlm@10: // a LONG or a DOUBLE, and all trailing TOP types) rlm@10: for(i = 0; i < locals.length; ++i) rlm@10: { rlm@10: t = locals[i]; rlm@10: if(t == Frame.TOP) rlm@10: { rlm@10: ++nTop; rlm@10: } rlm@10: else rlm@10: { rlm@10: nLocal += nTop + 1; rlm@10: nTop = 0; rlm@10: } rlm@10: if(t == Frame.LONG || t == Frame.DOUBLE) rlm@10: { rlm@10: ++i; rlm@10: } rlm@10: } rlm@10: // computes the stack size (ignores TOP types that are just after rlm@10: // a LONG or a DOUBLE) rlm@10: for(i = 0; i < stacks.length; ++i) rlm@10: { rlm@10: t = stacks[i]; rlm@10: ++nStack; rlm@10: if(t == Frame.LONG || t == Frame.DOUBLE) rlm@10: { rlm@10: ++i; rlm@10: } rlm@10: } rlm@10: // visits the frame and its content rlm@10: startFrame(f.owner.position, nLocal, nStack); rlm@10: for(i = 0; nLocal > 0; ++i, --nLocal) rlm@10: { rlm@10: t = locals[i]; rlm@10: frame[frameIndex++] = t; rlm@10: if(t == Frame.LONG || t == Frame.DOUBLE) rlm@10: { rlm@10: ++i; rlm@10: } rlm@10: } rlm@10: for(i = 0; i < stacks.length; ++i) rlm@10: { rlm@10: t = stacks[i]; rlm@10: frame[frameIndex++] = t; rlm@10: if(t == Frame.LONG || t == Frame.DOUBLE) rlm@10: { rlm@10: ++i; rlm@10: } rlm@10: } rlm@10: endFrame(); rlm@10: } rlm@10: rlm@10: /** rlm@10: * Starts the visit of a stack map frame. rlm@10: * rlm@10: * @param offset the offset of the instruction to which the frame rlm@10: * corresponds. rlm@10: * @param nLocal the number of local variables in the frame. rlm@10: * @param nStack the number of stack elements in the frame. rlm@10: */ rlm@10: private void startFrame(final int offset, final int nLocal, final int nStack){ rlm@10: int n = 3 + nLocal + nStack; rlm@10: if(frame == null || frame.length < n) rlm@10: { rlm@10: frame = new int[n]; rlm@10: } rlm@10: frame[0] = offset; rlm@10: frame[1] = nLocal; rlm@10: frame[2] = nStack; rlm@10: frameIndex = 3; rlm@10: } rlm@10: rlm@10: /** rlm@10: * Checks if the visit of the current frame {@link #frame} is finished, and rlm@10: * if yes, write it in the StackMapTable attribute. rlm@10: */ rlm@10: private void endFrame(){ rlm@10: if(previousFrame != null) rlm@10: { // do not write the first frame rlm@10: if(stackMap == null) rlm@10: { rlm@10: stackMap = new ByteVector(); rlm@10: } rlm@10: writeFrame(); rlm@10: ++frameCount; rlm@10: } rlm@10: previousFrame = frame; rlm@10: frame = null; rlm@10: } rlm@10: rlm@10: /** rlm@10: * Compress and writes the current frame {@link #frame} in the StackMapTable rlm@10: * attribute. rlm@10: */ rlm@10: private void writeFrame(){ rlm@10: int clocalsSize = frame[1]; rlm@10: int cstackSize = frame[2]; rlm@10: if((cw.version & 0xFFFF) < Opcodes.V1_6) rlm@10: { rlm@10: stackMap.putShort(frame[0]).putShort(clocalsSize); rlm@10: writeFrameTypes(3, 3 + clocalsSize); rlm@10: stackMap.putShort(cstackSize); rlm@10: writeFrameTypes(3 + clocalsSize, 3 + clocalsSize + cstackSize); rlm@10: return; rlm@10: } rlm@10: int localsSize = previousFrame[1]; rlm@10: int type = FULL_FRAME; rlm@10: int k = 0; rlm@10: int delta; rlm@10: if(frameCount == 0) rlm@10: { rlm@10: delta = frame[0]; rlm@10: } rlm@10: else rlm@10: { rlm@10: delta = frame[0] - previousFrame[0] - 1; rlm@10: } rlm@10: if(cstackSize == 0) rlm@10: { rlm@10: k = clocalsSize - localsSize; rlm@10: switch(k) rlm@10: { rlm@10: case-3: rlm@10: case-2: rlm@10: case-1: rlm@10: type = CHOP_FRAME; rlm@10: localsSize = clocalsSize; rlm@10: break; rlm@10: case 0: rlm@10: type = delta < 64 ? SAME_FRAME : SAME_FRAME_EXTENDED; rlm@10: break; rlm@10: case 1: rlm@10: case 2: rlm@10: case 3: rlm@10: type = APPEND_FRAME; rlm@10: break; rlm@10: } rlm@10: } rlm@10: else if(clocalsSize == localsSize && cstackSize == 1) rlm@10: { rlm@10: type = delta < 63 rlm@10: ? SAME_LOCALS_1_STACK_ITEM_FRAME rlm@10: : SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED; rlm@10: } rlm@10: if(type != FULL_FRAME) rlm@10: { rlm@10: // verify if locals are the same rlm@10: int l = 3; rlm@10: for(int j = 0; j < localsSize; j++) rlm@10: { rlm@10: if(frame[l] != previousFrame[l]) rlm@10: { rlm@10: type = FULL_FRAME; rlm@10: break; rlm@10: } rlm@10: l++; rlm@10: } rlm@10: } rlm@10: switch(type) rlm@10: { rlm@10: case SAME_FRAME: rlm@10: stackMap.putByte(delta); rlm@10: break; rlm@10: case SAME_LOCALS_1_STACK_ITEM_FRAME: rlm@10: stackMap.putByte(SAME_LOCALS_1_STACK_ITEM_FRAME + delta); rlm@10: writeFrameTypes(3 + clocalsSize, 4 + clocalsSize); rlm@10: break; rlm@10: case SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED: rlm@10: stackMap.putByte(SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) rlm@10: .putShort(delta); rlm@10: writeFrameTypes(3 + clocalsSize, 4 + clocalsSize); rlm@10: break; rlm@10: case SAME_FRAME_EXTENDED: rlm@10: stackMap.putByte(SAME_FRAME_EXTENDED).putShort(delta); rlm@10: break; rlm@10: case CHOP_FRAME: rlm@10: stackMap.putByte(SAME_FRAME_EXTENDED + k).putShort(delta); rlm@10: break; rlm@10: case APPEND_FRAME: rlm@10: stackMap.putByte(SAME_FRAME_EXTENDED + k).putShort(delta); rlm@10: writeFrameTypes(3 + localsSize, 3 + clocalsSize); rlm@10: break; rlm@10: // case FULL_FRAME: rlm@10: default: rlm@10: stackMap.putByte(FULL_FRAME) rlm@10: .putShort(delta) rlm@10: .putShort(clocalsSize); rlm@10: writeFrameTypes(3, 3 + clocalsSize); rlm@10: stackMap.putShort(cstackSize); rlm@10: writeFrameTypes(3 + clocalsSize, 3 + clocalsSize + cstackSize); rlm@10: } rlm@10: } rlm@10: rlm@10: /** rlm@10: * Writes some types of the current frame {@link #frame} into the rlm@10: * StackMapTableAttribute. This method converts types from the format used rlm@10: * in {@link Label} to the format used in StackMapTable attributes. In rlm@10: * particular, it converts type table indexes to constant pool indexes. rlm@10: * rlm@10: * @param start index of the first type in {@link #frame} to write. rlm@10: * @param end index of last type in {@link #frame} to write (exclusive). rlm@10: */ rlm@10: private void writeFrameTypes(final int start, final int end){ rlm@10: for(int i = start; i < end; ++i) rlm@10: { rlm@10: int t = frame[i]; rlm@10: int d = t & Frame.DIM; rlm@10: if(d == 0) rlm@10: { rlm@10: int v = t & Frame.BASE_VALUE; rlm@10: switch(t & Frame.BASE_KIND) rlm@10: { rlm@10: case Frame.OBJECT: rlm@10: stackMap.putByte(7) rlm@10: .putShort(cw.newClass(cw.typeTable[v].strVal1)); rlm@10: break; rlm@10: case Frame.UNINITIALIZED: rlm@10: stackMap.putByte(8).putShort(cw.typeTable[v].intVal); rlm@10: break; rlm@10: default: rlm@10: stackMap.putByte(v); rlm@10: } rlm@10: } rlm@10: else rlm@10: { rlm@10: StringBuffer buf = new StringBuffer(); rlm@10: d >>= 28; rlm@10: while(d-- > 0) rlm@10: { rlm@10: buf.append('['); rlm@10: } rlm@10: if((t & Frame.BASE_KIND) == Frame.OBJECT) rlm@10: { rlm@10: buf.append('L'); rlm@10: buf.append(cw.typeTable[t & Frame.BASE_VALUE].strVal1); rlm@10: buf.append(';'); rlm@10: } rlm@10: else rlm@10: { rlm@10: switch(t & 0xF) rlm@10: { rlm@10: case 1: rlm@10: buf.append('I'); rlm@10: break; rlm@10: case 2: rlm@10: buf.append('F'); rlm@10: break; rlm@10: case 3: rlm@10: buf.append('D'); rlm@10: break; rlm@10: case 9: rlm@10: buf.append('Z'); rlm@10: break; rlm@10: case 10: rlm@10: buf.append('B'); rlm@10: break; rlm@10: case 11: rlm@10: buf.append('C'); rlm@10: break; rlm@10: case 12: rlm@10: buf.append('S'); rlm@10: break; rlm@10: default: rlm@10: buf.append('J'); rlm@10: } rlm@10: } rlm@10: stackMap.putByte(7).putShort(cw.newClass(buf.toString())); rlm@10: } rlm@10: } rlm@10: } rlm@10: rlm@10: private void writeFrameType(final Object type){ rlm@10: if(type instanceof String) rlm@10: { rlm@10: stackMap.putByte(7).putShort(cw.newClass((String) type)); rlm@10: } rlm@10: else if(type instanceof Integer) rlm@10: { rlm@10: stackMap.putByte(((Integer) type).intValue()); rlm@10: } rlm@10: else rlm@10: { rlm@10: stackMap.putByte(8).putShort(((Label) type).position); rlm@10: } rlm@10: } rlm@10: rlm@10: // ------------------------------------------------------------------------ rlm@10: // Utility methods: dump bytecode array rlm@10: // ------------------------------------------------------------------------ rlm@10: rlm@10: /** rlm@10: * Returns the size of the bytecode of this method. rlm@10: * rlm@10: * @return the size of the bytecode of this method. rlm@10: */ rlm@10: final int getSize(){ rlm@10: if(classReaderOffset != 0) rlm@10: { rlm@10: return 6 + classReaderLength; rlm@10: } rlm@10: if(resize) rlm@10: { rlm@10: // replaces the temporary jump opcodes introduced by Label.resolve. rlm@10: resizeInstructions(); rlm@10: } rlm@10: int size = 8; rlm@10: if(code.length > 0) rlm@10: { rlm@10: cw.newUTF8("Code"); rlm@10: size += 18 + code.length + 8 * handlerCount; rlm@10: if(localVar != null) rlm@10: { rlm@10: cw.newUTF8("LocalVariableTable"); rlm@10: size += 8 + localVar.length; rlm@10: } rlm@10: if(localVarType != null) rlm@10: { rlm@10: cw.newUTF8("LocalVariableTypeTable"); rlm@10: size += 8 + localVarType.length; rlm@10: } rlm@10: if(lineNumber != null) rlm@10: { rlm@10: cw.newUTF8("LineNumberTable"); rlm@10: size += 8 + lineNumber.length; rlm@10: } rlm@10: if(stackMap != null) rlm@10: { rlm@10: boolean zip = (cw.version & 0xFFFF) >= Opcodes.V1_6; rlm@10: cw.newUTF8(zip ? "StackMapTable" : "StackMap"); rlm@10: size += 8 + stackMap.length; rlm@10: } rlm@10: if(cattrs != null) rlm@10: { rlm@10: size += cattrs.getSize(cw, rlm@10: code.data, rlm@10: code.length, rlm@10: maxStack, rlm@10: maxLocals); rlm@10: } rlm@10: } rlm@10: if(exceptionCount > 0) rlm@10: { rlm@10: cw.newUTF8("Exceptions"); rlm@10: size += 8 + 2 * exceptionCount; rlm@10: } rlm@10: if((access & Opcodes.ACC_SYNTHETIC) != 0 rlm@10: && (cw.version & 0xffff) < Opcodes.V1_5) rlm@10: { rlm@10: cw.newUTF8("Synthetic"); rlm@10: size += 6; rlm@10: } rlm@10: if((access & Opcodes.ACC_DEPRECATED) != 0) rlm@10: { rlm@10: cw.newUTF8("Deprecated"); rlm@10: size += 6; rlm@10: } rlm@10: if(signature != null) rlm@10: { rlm@10: cw.newUTF8("Signature"); rlm@10: cw.newUTF8(signature); rlm@10: size += 8; rlm@10: } rlm@10: if(annd != null) rlm@10: { rlm@10: cw.newUTF8("AnnotationDefault"); rlm@10: size += 6 + annd.length; rlm@10: } rlm@10: if(anns != null) rlm@10: { rlm@10: cw.newUTF8("RuntimeVisibleAnnotations"); rlm@10: size += 8 + anns.getSize(); rlm@10: } rlm@10: if(ianns != null) rlm@10: { rlm@10: cw.newUTF8("RuntimeInvisibleAnnotations"); rlm@10: size += 8 + ianns.getSize(); rlm@10: } rlm@10: if(panns != null) rlm@10: { rlm@10: cw.newUTF8("RuntimeVisibleParameterAnnotations"); rlm@10: size += 7 + 2 * panns.length; rlm@10: for(int i = panns.length - 1; i >= 0; --i) rlm@10: { rlm@10: size += panns[i] == null ? 0 : panns[i].getSize(); rlm@10: } rlm@10: } rlm@10: if(ipanns != null) rlm@10: { rlm@10: cw.newUTF8("RuntimeInvisibleParameterAnnotations"); rlm@10: size += 7 + 2 * ipanns.length; rlm@10: for(int i = ipanns.length - 1; i >= 0; --i) rlm@10: { rlm@10: size += ipanns[i] == null ? 0 : ipanns[i].getSize(); rlm@10: } rlm@10: } rlm@10: if(attrs != null) rlm@10: { rlm@10: size += attrs.getSize(cw, null, 0, -1, -1); rlm@10: } rlm@10: return size; rlm@10: } rlm@10: rlm@10: /** rlm@10: * Puts the bytecode of this method in the given byte vector. rlm@10: * rlm@10: * @param out the byte vector into which the bytecode of this method must be rlm@10: * copied. rlm@10: */ rlm@10: final void put(final ByteVector out){ rlm@10: out.putShort(access).putShort(name).putShort(desc); rlm@10: if(classReaderOffset != 0) rlm@10: { rlm@10: out.putByteArray(cw.cr.b, classReaderOffset, classReaderLength); rlm@10: return; rlm@10: } rlm@10: int attributeCount = 0; rlm@10: if(code.length > 0) rlm@10: { rlm@10: ++attributeCount; rlm@10: } rlm@10: if(exceptionCount > 0) rlm@10: { rlm@10: ++attributeCount; rlm@10: } rlm@10: if((access & Opcodes.ACC_SYNTHETIC) != 0 rlm@10: && (cw.version & 0xffff) < Opcodes.V1_5) rlm@10: { rlm@10: ++attributeCount; rlm@10: } rlm@10: if((access & Opcodes.ACC_DEPRECATED) != 0) rlm@10: { rlm@10: ++attributeCount; rlm@10: } rlm@10: if(signature != null) rlm@10: { rlm@10: ++attributeCount; rlm@10: } rlm@10: if(annd != null) rlm@10: { rlm@10: ++attributeCount; rlm@10: } rlm@10: if(anns != null) rlm@10: { rlm@10: ++attributeCount; rlm@10: } rlm@10: if(ianns != null) rlm@10: { rlm@10: ++attributeCount; rlm@10: } rlm@10: if(panns != null) rlm@10: { rlm@10: ++attributeCount; rlm@10: } rlm@10: if(ipanns != null) rlm@10: { rlm@10: ++attributeCount; rlm@10: } rlm@10: if(attrs != null) rlm@10: { rlm@10: attributeCount += attrs.getCount(); rlm@10: } rlm@10: out.putShort(attributeCount); rlm@10: if(code.length > 0) rlm@10: { rlm@10: int size = 12 + code.length + 8 * handlerCount; rlm@10: if(localVar != null) rlm@10: { rlm@10: size += 8 + localVar.length; rlm@10: } rlm@10: if(localVarType != null) rlm@10: { rlm@10: size += 8 + localVarType.length; rlm@10: } rlm@10: if(lineNumber != null) rlm@10: { rlm@10: size += 8 + lineNumber.length; rlm@10: } rlm@10: if(stackMap != null) rlm@10: { rlm@10: size += 8 + stackMap.length; rlm@10: } rlm@10: if(cattrs != null) rlm@10: { rlm@10: size += cattrs.getSize(cw, rlm@10: code.data, rlm@10: code.length, rlm@10: maxStack, rlm@10: maxLocals); rlm@10: } rlm@10: out.putShort(cw.newUTF8("Code")).putInt(size); rlm@10: out.putShort(maxStack).putShort(maxLocals); rlm@10: out.putInt(code.length).putByteArray(code.data, 0, code.length); rlm@10: out.putShort(handlerCount); rlm@10: if(handlerCount > 0) rlm@10: { rlm@10: Handler h = firstHandler; rlm@10: while(h != null) rlm@10: { rlm@10: out.putShort(h.start.position) rlm@10: .putShort(h.end.position) rlm@10: .putShort(h.handler.position) rlm@10: .putShort(h.type); rlm@10: h = h.next; rlm@10: } rlm@10: } rlm@10: attributeCount = 0; rlm@10: if(localVar != null) rlm@10: { rlm@10: ++attributeCount; rlm@10: } rlm@10: if(localVarType != null) rlm@10: { rlm@10: ++attributeCount; rlm@10: } rlm@10: if(lineNumber != null) rlm@10: { rlm@10: ++attributeCount; rlm@10: } rlm@10: if(stackMap != null) rlm@10: { rlm@10: ++attributeCount; rlm@10: } rlm@10: if(cattrs != null) rlm@10: { rlm@10: attributeCount += cattrs.getCount(); rlm@10: } rlm@10: out.putShort(attributeCount); rlm@10: if(localVar != null) rlm@10: { rlm@10: out.putShort(cw.newUTF8("LocalVariableTable")); rlm@10: out.putInt(localVar.length + 2).putShort(localVarCount); rlm@10: out.putByteArray(localVar.data, 0, localVar.length); rlm@10: } rlm@10: if(localVarType != null) rlm@10: { rlm@10: out.putShort(cw.newUTF8("LocalVariableTypeTable")); rlm@10: out.putInt(localVarType.length + 2).putShort(localVarTypeCount); rlm@10: out.putByteArray(localVarType.data, 0, localVarType.length); rlm@10: } rlm@10: if(lineNumber != null) rlm@10: { rlm@10: out.putShort(cw.newUTF8("LineNumberTable")); rlm@10: out.putInt(lineNumber.length + 2).putShort(lineNumberCount); rlm@10: out.putByteArray(lineNumber.data, 0, lineNumber.length); rlm@10: } rlm@10: if(stackMap != null) rlm@10: { rlm@10: boolean zip = (cw.version & 0xFFFF) >= Opcodes.V1_6; rlm@10: out.putShort(cw.newUTF8(zip ? "StackMapTable" : "StackMap")); rlm@10: out.putInt(stackMap.length + 2).putShort(frameCount); rlm@10: out.putByteArray(stackMap.data, 0, stackMap.length); rlm@10: } rlm@10: if(cattrs != null) rlm@10: { rlm@10: cattrs.put(cw, code.data, code.length, maxLocals, maxStack, out); rlm@10: } rlm@10: } rlm@10: if(exceptionCount > 0) rlm@10: { rlm@10: out.putShort(cw.newUTF8("Exceptions")) rlm@10: .putInt(2 * exceptionCount + 2); rlm@10: out.putShort(exceptionCount); rlm@10: for(int i = 0; i < exceptionCount; ++i) rlm@10: { rlm@10: out.putShort(exceptions[i]); rlm@10: } rlm@10: } rlm@10: if((access & Opcodes.ACC_SYNTHETIC) != 0 rlm@10: && (cw.version & 0xffff) < Opcodes.V1_5) rlm@10: { rlm@10: out.putShort(cw.newUTF8("Synthetic")).putInt(0); rlm@10: } rlm@10: if((access & Opcodes.ACC_DEPRECATED) != 0) rlm@10: { rlm@10: out.putShort(cw.newUTF8("Deprecated")).putInt(0); rlm@10: } rlm@10: if(signature != null) rlm@10: { rlm@10: out.putShort(cw.newUTF8("Signature")) rlm@10: .putInt(2) rlm@10: .putShort(cw.newUTF8(signature)); rlm@10: } rlm@10: if(annd != null) rlm@10: { rlm@10: out.putShort(cw.newUTF8("AnnotationDefault")); rlm@10: out.putInt(annd.length); rlm@10: out.putByteArray(annd.data, 0, annd.length); rlm@10: } rlm@10: if(anns != null) rlm@10: { rlm@10: out.putShort(cw.newUTF8("RuntimeVisibleAnnotations")); rlm@10: anns.put(out); rlm@10: } rlm@10: if(ianns != null) rlm@10: { rlm@10: out.putShort(cw.newUTF8("RuntimeInvisibleAnnotations")); rlm@10: ianns.put(out); rlm@10: } rlm@10: if(panns != null) rlm@10: { rlm@10: out.putShort(cw.newUTF8("RuntimeVisibleParameterAnnotations")); rlm@10: AnnotationWriter.put(panns, out); rlm@10: } rlm@10: if(ipanns != null) rlm@10: { rlm@10: out.putShort(cw.newUTF8("RuntimeInvisibleParameterAnnotations")); rlm@10: AnnotationWriter.put(ipanns, out); rlm@10: } rlm@10: if(attrs != null) rlm@10: { rlm@10: attrs.put(cw, null, 0, -1, -1, out); rlm@10: } rlm@10: } rlm@10: rlm@10: // ------------------------------------------------------------------------ rlm@10: // Utility methods: instruction resizing (used to handle GOTO_W and JSR_W) rlm@10: // ------------------------------------------------------------------------ rlm@10: rlm@10: /** rlm@10: * Resizes and replaces the temporary instructions inserted by rlm@10: * {@link Label#resolve} for wide forward jumps, while keeping jump offsets rlm@10: * and instruction addresses consistent. This may require to resize other rlm@10: * existing instructions, or even to introduce new instructions: for rlm@10: * example, increasing the size of an instruction by 2 at the middle of a rlm@10: * method can increases the offset of an IFEQ instruction from 32766 to rlm@10: * 32768, in which case IFEQ 32766 must be replaced with IFNEQ 8 GOTO_W rlm@10: * 32765. This, in turn, may require to increase the size of another jump rlm@10: * instruction, and so on... All these operations are handled automatically rlm@10: * by this method.

This method must be called after all the method rlm@10: * that is being built has been visited. In particular, the rlm@10: * {@link Label Label} objects used to construct the method are no longer rlm@10: * valid after this method has been called. rlm@10: */ rlm@10: private void resizeInstructions(){ rlm@10: byte[] b = code.data; // bytecode of the method rlm@10: int u, v, label; // indexes in b rlm@10: int i, j; // loop indexes rlm@10: /* rlm@10: * 1st step: As explained above, resizing an instruction may require to rlm@10: * resize another one, which may require to resize yet another one, and rlm@10: * so on. The first step of the algorithm consists in finding all the rlm@10: * instructions that need to be resized, without modifying the code. rlm@10: * This is done by the following "fix point" algorithm: rlm@10: * rlm@10: * Parse the code to find the jump instructions whose offset will need rlm@10: * more than 2 bytes to be stored (the future offset is computed from rlm@10: * the current offset and from the number of bytes that will be inserted rlm@10: * or removed between the source and target instructions). For each such rlm@10: * instruction, adds an entry in (a copy of) the indexes and sizes rlm@10: * arrays (if this has not already been done in a previous iteration!). rlm@10: * rlm@10: * If at least one entry has been added during the previous step, go rlm@10: * back to the beginning, otherwise stop. rlm@10: * rlm@10: * In fact the real algorithm is complicated by the fact that the size rlm@10: * of TABLESWITCH and LOOKUPSWITCH instructions depends on their rlm@10: * position in the bytecode (because of padding). In order to ensure the rlm@10: * convergence of the algorithm, the number of bytes to be added or rlm@10: * removed from these instructions is over estimated during the previous rlm@10: * loop, and computed exactly only after the loop is finished (this rlm@10: * requires another pass to parse the bytecode of the method). rlm@10: */ rlm@10: int[] allIndexes = new int[0]; // copy of indexes rlm@10: int[] allSizes = new int[0]; // copy of sizes rlm@10: boolean[] resize; // instructions to be resized rlm@10: int newOffset; // future offset of a jump instruction rlm@10: rlm@10: resize = new boolean[code.length]; rlm@10: rlm@10: // 3 = loop again, 2 = loop ended, 1 = last pass, 0 = done rlm@10: int state = 3; rlm@10: do rlm@10: { rlm@10: if(state == 3) rlm@10: { rlm@10: state = 2; rlm@10: } rlm@10: u = 0; rlm@10: while(u < b.length) rlm@10: { rlm@10: int opcode = b[u] & 0xFF; // opcode of current instruction rlm@10: int insert = 0; // bytes to be added after this instruction rlm@10: rlm@10: switch(ClassWriter.TYPE[opcode]) rlm@10: { rlm@10: case ClassWriter.NOARG_INSN: rlm@10: case ClassWriter.IMPLVAR_INSN: rlm@10: u += 1; rlm@10: break; rlm@10: case ClassWriter.LABEL_INSN: rlm@10: if(opcode > 201) rlm@10: { rlm@10: // converts temporary opcodes 202 to 217, 218 and rlm@10: // 219 to IFEQ ... JSR (inclusive), IFNULL and rlm@10: // IFNONNULL rlm@10: opcode = opcode < 218 ? opcode - 49 : opcode - 20; rlm@10: label = u + readUnsignedShort(b, u + 1); rlm@10: } rlm@10: else rlm@10: { rlm@10: label = u + readShort(b, u + 1); rlm@10: } rlm@10: newOffset = getNewOffset(allIndexes, allSizes, u, label); rlm@10: if(newOffset < Short.MIN_VALUE rlm@10: || newOffset > Short.MAX_VALUE) rlm@10: { rlm@10: if(!resize[u]) rlm@10: { rlm@10: if(opcode == Opcodes.GOTO rlm@10: || opcode == Opcodes.JSR) rlm@10: { rlm@10: // two additional bytes will be required to rlm@10: // replace this GOTO or JSR instruction with rlm@10: // a GOTO_W or a JSR_W rlm@10: insert = 2; rlm@10: } rlm@10: else rlm@10: { rlm@10: // five additional bytes will be required to rlm@10: // replace this IFxxx instruction with rlm@10: // IFNOTxxx GOTO_W , where IFNOTxxx rlm@10: // is the "opposite" opcode of IFxxx (i.e., rlm@10: // IFNE for IFEQ) and where designates rlm@10: // the instruction just after the GOTO_W. rlm@10: insert = 5; rlm@10: } rlm@10: resize[u] = true; rlm@10: } rlm@10: } rlm@10: u += 3; rlm@10: break; rlm@10: case ClassWriter.LABELW_INSN: rlm@10: u += 5; rlm@10: break; rlm@10: case ClassWriter.TABL_INSN: rlm@10: if(state == 1) rlm@10: { rlm@10: // true number of bytes to be added (or removed) rlm@10: // from this instruction = (future number of padding rlm@10: // bytes - current number of padding byte) - rlm@10: // previously over estimated variation = rlm@10: // = ((3 - newOffset%4) - (3 - u%4)) - u%4 rlm@10: // = (-newOffset%4 + u%4) - u%4 rlm@10: // = -(newOffset & 3) rlm@10: newOffset = getNewOffset(allIndexes, allSizes, 0, u); rlm@10: insert = -(newOffset & 3); rlm@10: } rlm@10: else if(!resize[u]) rlm@10: { rlm@10: // over estimation of the number of bytes to be rlm@10: // added to this instruction = 3 - current number rlm@10: // of padding bytes = 3 - (3 - u%4) = u%4 = u & 3 rlm@10: insert = u & 3; rlm@10: resize[u] = true; rlm@10: } rlm@10: // skips instruction rlm@10: u = u + 4 - (u & 3); rlm@10: u += 4 * (readInt(b, u + 8) - readInt(b, u + 4) + 1) + 12; rlm@10: break; rlm@10: case ClassWriter.LOOK_INSN: rlm@10: if(state == 1) rlm@10: { rlm@10: // like TABL_INSN rlm@10: newOffset = getNewOffset(allIndexes, allSizes, 0, u); rlm@10: insert = -(newOffset & 3); rlm@10: } rlm@10: else if(!resize[u]) rlm@10: { rlm@10: // like TABL_INSN rlm@10: insert = u & 3; rlm@10: resize[u] = true; rlm@10: } rlm@10: // skips instruction rlm@10: u = u + 4 - (u & 3); rlm@10: u += 8 * readInt(b, u + 4) + 8; rlm@10: break; rlm@10: case ClassWriter.WIDE_INSN: rlm@10: opcode = b[u + 1] & 0xFF; rlm@10: if(opcode == Opcodes.IINC) rlm@10: { rlm@10: u += 6; rlm@10: } rlm@10: else rlm@10: { rlm@10: u += 4; rlm@10: } rlm@10: break; rlm@10: case ClassWriter.VAR_INSN: rlm@10: case ClassWriter.SBYTE_INSN: rlm@10: case ClassWriter.LDC_INSN: rlm@10: u += 2; rlm@10: break; rlm@10: case ClassWriter.SHORT_INSN: rlm@10: case ClassWriter.LDCW_INSN: rlm@10: case ClassWriter.FIELDORMETH_INSN: rlm@10: case ClassWriter.TYPE_INSN: rlm@10: case ClassWriter.IINC_INSN: rlm@10: u += 3; rlm@10: break; rlm@10: case ClassWriter.ITFMETH_INSN: rlm@10: u += 5; rlm@10: break; rlm@10: // case ClassWriter.MANA_INSN: rlm@10: default: rlm@10: u += 4; rlm@10: break; rlm@10: } rlm@10: if(insert != 0) rlm@10: { rlm@10: // adds a new (u, insert) entry in the allIndexes and rlm@10: // allSizes arrays rlm@10: int[] newIndexes = new int[allIndexes.length + 1]; rlm@10: int[] newSizes = new int[allSizes.length + 1]; rlm@10: System.arraycopy(allIndexes, rlm@10: 0, rlm@10: newIndexes, rlm@10: 0, rlm@10: allIndexes.length); rlm@10: System.arraycopy(allSizes, 0, newSizes, 0, allSizes.length); rlm@10: newIndexes[allIndexes.length] = u; rlm@10: newSizes[allSizes.length] = insert; rlm@10: allIndexes = newIndexes; rlm@10: allSizes = newSizes; rlm@10: if(insert > 0) rlm@10: { rlm@10: state = 3; rlm@10: } rlm@10: } rlm@10: } rlm@10: if(state < 3) rlm@10: { rlm@10: --state; rlm@10: } rlm@10: } while(state != 0); rlm@10: rlm@10: // 2nd step: rlm@10: // copies the bytecode of the method into a new bytevector, updates the rlm@10: // offsets, and inserts (or removes) bytes as requested. rlm@10: rlm@10: ByteVector newCode = new ByteVector(code.length); rlm@10: rlm@10: u = 0; rlm@10: while(u < code.length) rlm@10: { rlm@10: int opcode = b[u] & 0xFF; rlm@10: switch(ClassWriter.TYPE[opcode]) rlm@10: { rlm@10: case ClassWriter.NOARG_INSN: rlm@10: case ClassWriter.IMPLVAR_INSN: rlm@10: newCode.putByte(opcode); rlm@10: u += 1; rlm@10: break; rlm@10: case ClassWriter.LABEL_INSN: rlm@10: if(opcode > 201) rlm@10: { rlm@10: // changes temporary opcodes 202 to 217 (inclusive), 218 rlm@10: // and 219 to IFEQ ... JSR (inclusive), IFNULL and rlm@10: // IFNONNULL rlm@10: opcode = opcode < 218 ? opcode - 49 : opcode - 20; rlm@10: label = u + readUnsignedShort(b, u + 1); rlm@10: } rlm@10: else rlm@10: { rlm@10: label = u + readShort(b, u + 1); rlm@10: } rlm@10: newOffset = getNewOffset(allIndexes, allSizes, u, label); rlm@10: if(resize[u]) rlm@10: { rlm@10: // replaces GOTO with GOTO_W, JSR with JSR_W and IFxxx rlm@10: // with IFNOTxxx GOTO_W , where IFNOTxxx is rlm@10: // the "opposite" opcode of IFxxx (i.e., IFNE for IFEQ) rlm@10: // and where designates the instruction just after rlm@10: // the GOTO_W. rlm@10: if(opcode == Opcodes.GOTO) rlm@10: { rlm@10: newCode.putByte(200); // GOTO_W rlm@10: } rlm@10: else if(opcode == Opcodes.JSR) rlm@10: { rlm@10: newCode.putByte(201); // JSR_W rlm@10: } rlm@10: else rlm@10: { rlm@10: newCode.putByte(opcode <= 166 rlm@10: ? ((opcode + 1) ^ 1) - 1 rlm@10: : opcode ^ 1); rlm@10: newCode.putShort(8); // jump offset rlm@10: newCode.putByte(200); // GOTO_W rlm@10: // newOffset now computed from start of GOTO_W rlm@10: newOffset -= 3; rlm@10: } rlm@10: newCode.putInt(newOffset); rlm@10: } rlm@10: else rlm@10: { rlm@10: newCode.putByte(opcode); rlm@10: newCode.putShort(newOffset); rlm@10: } rlm@10: u += 3; rlm@10: break; rlm@10: case ClassWriter.LABELW_INSN: rlm@10: label = u + readInt(b, u + 1); rlm@10: newOffset = getNewOffset(allIndexes, allSizes, u, label); rlm@10: newCode.putByte(opcode); rlm@10: newCode.putInt(newOffset); rlm@10: u += 5; rlm@10: break; rlm@10: case ClassWriter.TABL_INSN: rlm@10: // skips 0 to 3 padding bytes rlm@10: v = u; rlm@10: u = u + 4 - (v & 3); rlm@10: // reads and copies instruction rlm@10: newCode.putByte(Opcodes.TABLESWITCH); rlm@10: newCode.length += (4 - newCode.length % 4) % 4; rlm@10: label = v + readInt(b, u); rlm@10: u += 4; rlm@10: newOffset = getNewOffset(allIndexes, allSizes, v, label); rlm@10: newCode.putInt(newOffset); rlm@10: j = readInt(b, u); rlm@10: u += 4; rlm@10: newCode.putInt(j); rlm@10: j = readInt(b, u) - j + 1; rlm@10: u += 4; rlm@10: newCode.putInt(readInt(b, u - 4)); rlm@10: for(; j > 0; --j) rlm@10: { rlm@10: label = v + readInt(b, u); rlm@10: u += 4; rlm@10: newOffset = getNewOffset(allIndexes, allSizes, v, label); rlm@10: newCode.putInt(newOffset); rlm@10: } rlm@10: break; rlm@10: case ClassWriter.LOOK_INSN: rlm@10: // skips 0 to 3 padding bytes rlm@10: v = u; rlm@10: u = u + 4 - (v & 3); rlm@10: // reads and copies instruction rlm@10: newCode.putByte(Opcodes.LOOKUPSWITCH); rlm@10: newCode.length += (4 - newCode.length % 4) % 4; rlm@10: label = v + readInt(b, u); rlm@10: u += 4; rlm@10: newOffset = getNewOffset(allIndexes, allSizes, v, label); rlm@10: newCode.putInt(newOffset); rlm@10: j = readInt(b, u); rlm@10: u += 4; rlm@10: newCode.putInt(j); rlm@10: for(; j > 0; --j) rlm@10: { rlm@10: newCode.putInt(readInt(b, u)); rlm@10: u += 4; rlm@10: label = v + readInt(b, u); rlm@10: u += 4; rlm@10: newOffset = getNewOffset(allIndexes, allSizes, v, label); rlm@10: newCode.putInt(newOffset); rlm@10: } rlm@10: break; rlm@10: case ClassWriter.WIDE_INSN: rlm@10: opcode = b[u + 1] & 0xFF; rlm@10: if(opcode == Opcodes.IINC) rlm@10: { rlm@10: newCode.putByteArray(b, u, 6); rlm@10: u += 6; rlm@10: } rlm@10: else rlm@10: { rlm@10: newCode.putByteArray(b, u, 4); rlm@10: u += 4; rlm@10: } rlm@10: break; rlm@10: case ClassWriter.VAR_INSN: rlm@10: case ClassWriter.SBYTE_INSN: rlm@10: case ClassWriter.LDC_INSN: rlm@10: newCode.putByteArray(b, u, 2); rlm@10: u += 2; rlm@10: break; rlm@10: case ClassWriter.SHORT_INSN: rlm@10: case ClassWriter.LDCW_INSN: rlm@10: case ClassWriter.FIELDORMETH_INSN: rlm@10: case ClassWriter.TYPE_INSN: rlm@10: case ClassWriter.IINC_INSN: rlm@10: newCode.putByteArray(b, u, 3); rlm@10: u += 3; rlm@10: break; rlm@10: case ClassWriter.ITFMETH_INSN: rlm@10: newCode.putByteArray(b, u, 5); rlm@10: u += 5; rlm@10: break; rlm@10: // case MANA_INSN: rlm@10: default: rlm@10: newCode.putByteArray(b, u, 4); rlm@10: u += 4; rlm@10: break; rlm@10: } rlm@10: } rlm@10: rlm@10: // recomputes the stack map frames rlm@10: if(frameCount > 0) rlm@10: { rlm@10: if(compute == FRAMES) rlm@10: { rlm@10: frameCount = 0; rlm@10: stackMap = null; rlm@10: previousFrame = null; rlm@10: frame = null; rlm@10: Frame f = new Frame(); rlm@10: f.owner = labels; rlm@10: Type[] args = Type.getArgumentTypes(descriptor); rlm@10: f.initInputFrame(cw, access, args, maxLocals); rlm@10: visitFrame(f); rlm@10: Label l = labels; rlm@10: while(l != null) rlm@10: { rlm@10: /* rlm@10: * here we need the original label position. getNewOffset rlm@10: * must therefore never have been called for this label. rlm@10: */ rlm@10: u = l.position - 3; rlm@10: if((l.status & Label.STORE) != 0 || (u >= 0 && resize[u])) rlm@10: { rlm@10: getNewOffset(allIndexes, allSizes, l); rlm@10: // TODO update offsets in UNINITIALIZED values rlm@10: visitFrame(l.frame); rlm@10: } rlm@10: l = l.successor; rlm@10: } rlm@10: } rlm@10: else rlm@10: { rlm@10: /* rlm@10: * Resizing an existing stack map frame table is really hard. rlm@10: * Not only the table must be parsed to update the offets, but rlm@10: * new frames may be needed for jump instructions that were rlm@10: * inserted by this method. And updating the offsets or rlm@10: * inserting frames can change the format of the following rlm@10: * frames, in case of packed frames. In practice the whole table rlm@10: * must be recomputed. For this the frames are marked as rlm@10: * potentially invalid. This will cause the whole class to be rlm@10: * reread and rewritten with the COMPUTE_FRAMES option (see the rlm@10: * ClassWriter.toByteArray method). This is not very efficient rlm@10: * but is much easier and requires much less code than any other rlm@10: * method I can think of. rlm@10: */ rlm@10: cw.invalidFrames = true; rlm@10: } rlm@10: } rlm@10: // updates the exception handler block labels rlm@10: Handler h = firstHandler; rlm@10: while(h != null) rlm@10: { rlm@10: getNewOffset(allIndexes, allSizes, h.start); rlm@10: getNewOffset(allIndexes, allSizes, h.end); rlm@10: getNewOffset(allIndexes, allSizes, h.handler); rlm@10: h = h.next; rlm@10: } rlm@10: // updates the instructions addresses in the rlm@10: // local var and line number tables rlm@10: for(i = 0; i < 2; ++i) rlm@10: { rlm@10: ByteVector bv = i == 0 ? localVar : localVarType; rlm@10: if(bv != null) rlm@10: { rlm@10: b = bv.data; rlm@10: u = 0; rlm@10: while(u < bv.length) rlm@10: { rlm@10: label = readUnsignedShort(b, u); rlm@10: newOffset = getNewOffset(allIndexes, allSizes, 0, label); rlm@10: writeShort(b, u, newOffset); rlm@10: label += readUnsignedShort(b, u + 2); rlm@10: newOffset = getNewOffset(allIndexes, allSizes, 0, label) rlm@10: - newOffset; rlm@10: writeShort(b, u + 2, newOffset); rlm@10: u += 10; rlm@10: } rlm@10: } rlm@10: } rlm@10: if(lineNumber != null) rlm@10: { rlm@10: b = lineNumber.data; rlm@10: u = 0; rlm@10: while(u < lineNumber.length) rlm@10: { rlm@10: writeShort(b, u, getNewOffset(allIndexes, rlm@10: allSizes, rlm@10: 0, rlm@10: readUnsignedShort(b, u))); rlm@10: u += 4; rlm@10: } rlm@10: } rlm@10: // updates the labels of the other attributes rlm@10: Attribute attr = cattrs; rlm@10: while(attr != null) rlm@10: { rlm@10: Label[] labels = attr.getLabels(); rlm@10: if(labels != null) rlm@10: { rlm@10: for(i = labels.length - 1; i >= 0; --i) rlm@10: { rlm@10: getNewOffset(allIndexes, allSizes, labels[i]); rlm@10: } rlm@10: } rlm@10: attr = attr.next; rlm@10: } rlm@10: rlm@10: // replaces old bytecodes with new ones rlm@10: code = newCode; rlm@10: } rlm@10: rlm@10: /** rlm@10: * Reads an unsigned short value in the given byte array. rlm@10: * rlm@10: * @param b a byte array. rlm@10: * @param index the start index of the value to be read. rlm@10: * @return the read value. rlm@10: */ rlm@10: static int readUnsignedShort(final byte[] b, final int index){ rlm@10: return ((b[index] & 0xFF) << 8) | (b[index + 1] & 0xFF); rlm@10: } rlm@10: rlm@10: /** rlm@10: * Reads a signed short value in the given byte array. rlm@10: * rlm@10: * @param b a byte array. rlm@10: * @param index the start index of the value to be read. rlm@10: * @return the read value. rlm@10: */ rlm@10: static short readShort(final byte[] b, final int index){ rlm@10: return (short) (((b[index] & 0xFF) << 8) | (b[index + 1] & 0xFF)); rlm@10: } rlm@10: rlm@10: /** rlm@10: * Reads a signed int value in the given byte array. rlm@10: * rlm@10: * @param b a byte array. rlm@10: * @param index the start index of the value to be read. rlm@10: * @return the read value. rlm@10: */ rlm@10: static int readInt(final byte[] b, final int index){ rlm@10: return ((b[index] & 0xFF) << 24) | ((b[index + 1] & 0xFF) << 16) rlm@10: | ((b[index + 2] & 0xFF) << 8) | (b[index + 3] & 0xFF); rlm@10: } rlm@10: rlm@10: /** rlm@10: * Writes a short value in the given byte array. rlm@10: * rlm@10: * @param b a byte array. rlm@10: * @param index where the first byte of the short value must be written. rlm@10: * @param s the value to be written in the given byte array. rlm@10: */ rlm@10: static void writeShort(final byte[] b, final int index, final int s){ rlm@10: b[index] = (byte) (s >>> 8); rlm@10: b[index + 1] = (byte) s; rlm@10: } rlm@10: rlm@10: /** rlm@10: * Computes the future value of a bytecode offset.

Note: it is possible rlm@10: * to have several entries for the same instruction in the indexes rlm@10: * and sizes: two entries (index=a,size=b) and (index=a,size=b') rlm@10: * are equivalent to a single entry (index=a,size=b+b'). rlm@10: * rlm@10: * @param indexes current positions of the instructions to be resized. Each rlm@10: * instruction must be designated by the index of its last rlm@10: * byte, plus one (or, in other words, by the index of the first rlm@10: * byte of the next instruction). rlm@10: * @param sizes the number of bytes to be added to the above rlm@10: * instructions. More precisely, for each i < len, rlm@10: * sizes[i] bytes will be added at the end of the rlm@10: * instruction designated by indexes[i] or, if rlm@10: * sizes[i] is negative, the last |sizes[i]| rlm@10: * bytes of the instruction will be removed (the instruction size rlm@10: * must not become negative or null). rlm@10: * @param begin index of the first byte of the source instruction. rlm@10: * @param end index of the first byte of the target instruction. rlm@10: * @return the future value of the given bytecode offset. rlm@10: */ rlm@10: static int getNewOffset( rlm@10: final int[] indexes, rlm@10: final int[] sizes, rlm@10: final int begin, rlm@10: final int end){ rlm@10: int offset = end - begin; rlm@10: for(int i = 0; i < indexes.length; ++i) rlm@10: { rlm@10: if(begin < indexes[i] && indexes[i] <= end) rlm@10: { rlm@10: // forward jump rlm@10: offset += sizes[i]; rlm@10: } rlm@10: else if(end < indexes[i] && indexes[i] <= begin) rlm@10: { rlm@10: // backward jump rlm@10: offset -= sizes[i]; rlm@10: } rlm@10: } rlm@10: return offset; rlm@10: } rlm@10: rlm@10: /** rlm@10: * Updates the offset of the given label. rlm@10: * rlm@10: * @param indexes current positions of the instructions to be resized. Each rlm@10: * instruction must be designated by the index of its last rlm@10: * byte, plus one (or, in other words, by the index of the first rlm@10: * byte of the next instruction). rlm@10: * @param sizes the number of bytes to be added to the above rlm@10: * instructions. More precisely, for each i < len, rlm@10: * sizes[i] bytes will be added at the end of the rlm@10: * instruction designated by indexes[i] or, if rlm@10: * sizes[i] is negative, the last |sizes[i]| rlm@10: * bytes of the instruction will be removed (the instruction size rlm@10: * must not become negative or null). rlm@10: * @param label the label whose offset must be updated. rlm@10: */ rlm@10: static void getNewOffset( rlm@10: final int[] indexes, rlm@10: final int[] sizes, rlm@10: final Label label){ rlm@10: if((label.status & Label.RESIZED) == 0) rlm@10: { rlm@10: label.position = getNewOffset(indexes, sizes, 0, label.position); rlm@10: label.status |= Label.RESIZED; rlm@10: } rlm@10: } rlm@10: }