Author: kohsuke Date: Tue Jan 3 23:42:14 2006 New Revision: 365854 URL: http://svn.apache.org/viewcvs?rev=365854&view=rev Log: fixed indentation to conform to the Apache standard 4 SP indentation.
Modified: jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/bytecode/transformation/asm/AsmClassTransformer.java jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/bytecode/transformation/asm/ContinuationMethodAdapter.java jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/bytecode/transformation/asm/ContinuationMethodAnalyzer.java Modified: jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/bytecode/transformation/asm/AsmClassTransformer.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/bytecode/transformation/asm/AsmClassTransformer.java?rev=365854&r1=365853&r2=365854&view=diff ============================================================================== --- jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/bytecode/transformation/asm/AsmClassTransformer.java (original) +++ jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/bytecode/transformation/asm/AsmClassTransformer.java Tue Jan 3 23:42:14 2006 @@ -34,40 +34,40 @@ */ public final class AsmClassTransformer implements ResourceTransformer { private final static Log log = LogFactory.getLog(AsmClassTransformer.class); - - public byte[] transform( InputStream is) throws IOException { - return transform( new ClassReader(is)); + + public byte[] transform(InputStream is) throws IOException { + return transform(new ClassReader(is)); } - - public byte[] transform( final byte[] original ) { - return transform( new ClassReader(original)); + + public byte[] transform(final byte[] original) { + return transform(new ClassReader(original)); } - - private byte[] transform( ClassReader cr ) { + + private byte[] transform(ClassReader cr) { // final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); final ClassWriter cw = new ClassWriter(true, false); // print bytecode before transformation // cr.accept(new TraceClassVisitor(new ContinuationClassAdapter(this, cw), new PrintWriter(System.out)), false); - + // prints bytecode after transformation // cr.accept(new ContinuationClassAdapter(this, new TraceClassVisitor(cw, new PrintWriter(System.err))), 0); // cr.accept(new ContinuationClassAdapter(this, new TraceClassVisitor(cw, new PrintWriter(System.err))), false); - cr.accept(new ContinuationClassAdapter(new CheckClassAdapter( cw)), false); - + cr.accept(new ContinuationClassAdapter(new CheckClassAdapter(cw)), false); + byte[] bytecode = cw.toByteArray(); - + // CheckClassAdapter.verify(new ClassReader(bytecode), true); // new ClassReader(bytecode).accept(new ASMifierClassVisitor(new PrintWriter(System.err)), false); - + // ClassWriter cww = new ClassWriter(ClassWriter.COMPUTE_FRAMES); // new ClassReader(bytecode).accept(cww, ClassReader.SKIP_DEBUG); // new ClassReader(cww.toByteArray()).accept(new TraceClassVisitor(new PrintWriter(System.err)), 0); - + return bytecode; } - + } Modified: jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/bytecode/transformation/asm/ContinuationMethodAdapter.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/bytecode/transformation/asm/ContinuationMethodAdapter.java?rev=365854&r1=365853&r2=365854&view=diff ============================================================================== --- jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/bytecode/transformation/asm/ContinuationMethodAdapter.java (original) +++ jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/bytecode/transformation/asm/ContinuationMethodAdapter.java Tue Jan 3 23:42:14 2006 @@ -35,295 +35,295 @@ * @author Evgueni Koulechov */ public final class ContinuationMethodAdapter extends MethodAdapter implements Opcodes { - private static final String STACK_RECORDER = Type.getInternalName(StackRecorder.class); - private static final String POP_METHOD = "pop"; - private static final String PUSH_METHOD = "push"; - - private final Analyzer analyzer; - private Label startLabel = new Label(); - private final List labels; - private final List nodes; - private final int stackRecorderVar; - private final boolean isStatic; - private final String methodDesc; - - private int currentIndex = 0; - private Frame currentFrame = null; - - - public ContinuationMethodAdapter(ContinuationMethodAnalyzer a) { - super(a.mv); - this.analyzer = a.analyzer; - this.labels = a.labels; - this.nodes = a.nodes; - this.stackRecorderVar = a.stackRecorderVar; - this.isStatic = (a.access & ACC_STATIC)>0; - this.methodDesc = a.desc; - } - - public void visitCode() { - mv.visitCode(); - - int fsize = labels.size(); - Label[] restoreLabels = new Label[fsize]; - for( int i = 0; i < restoreLabels.length; i++) { - restoreLabels[i] = new Label(); + private static final String STACK_RECORDER = Type.getInternalName(StackRecorder.class); + private static final String POP_METHOD = "pop"; + private static final String PUSH_METHOD = "push"; + + private final Analyzer analyzer; + private Label startLabel = new Label(); + private final List labels; + private final List nodes; + private final int stackRecorderVar; + private final boolean isStatic; + private final String methodDesc; + + private int currentIndex = 0; + private Frame currentFrame = null; + + + public ContinuationMethodAdapter(ContinuationMethodAnalyzer a) { + super(a.mv); + this.analyzer = a.analyzer; + this.labels = a.labels; + this.nodes = a.nodes; + this.stackRecorderVar = a.stackRecorderVar; + this.isStatic = (a.access & ACC_STATIC) > 0; + this.methodDesc = a.desc; } - - // verify if restoring - Label l0 = new Label(); - - mv.visitMethodInsn(INVOKESTATIC, STACK_RECORDER, "get", "()L"+STACK_RECORDER+";"); - mv.visitInsn(DUP); - mv.visitVarInsn(ASTORE, stackRecorderVar); - mv.visitLabel(startLabel); - - mv.visitJumpInsn(IFNULL, l0); - mv.visitVarInsn(ALOAD, stackRecorderVar); - mv.visitFieldInsn(GETFIELD, STACK_RECORDER, "isRestoring", "Z"); - mv.visitJumpInsn(IFEQ, l0); - - mv.visitVarInsn(ALOAD, stackRecorderVar); - mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, POP_METHOD + "Int", "()I"); - mv.visitTableSwitchInsn(0, fsize-1, l0, restoreLabels); - - // switch cases - for(int i = 0; i<fsize; i++) { - Label frameLabel = (Label) labels.get(i); - mv.visitLabel(restoreLabels[i]); - - MethodInsnNode mnode = (MethodInsnNode) nodes.get(i); - - Frame frame = analyzer.getFrames()[analyzer.getIndex(mnode)]; - - // locals - int lsize = frame.getLocals(); - for( int j = lsize-1; j>=0; j--) { - BasicValue value = (BasicValue) frame.getLocal(j); - if(value==null) { - mv.visitInsn(ACONST_NULL); - mv.visitVarInsn(ASTORE, j); - } else if(value==BasicValue.UNINITIALIZED_VALUE) { - // TODO ?? - } else if(value==BasicValue.RETURNADDRESS_VALUE) { - // TODO ?? - } else { - mv.visitVarInsn(ALOAD, stackRecorderVar); - Type type = value.getType(); - if(value.isReference()) { - mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, POP_METHOD + "Object", "()Ljava/lang/Object;"); - Type t = value.getType(); - String desc = t.getDescriptor(); - if(desc.charAt(0)=='[') { - mv.visitTypeInsn(CHECKCAST, desc); + + public void visitCode() { + mv.visitCode(); + + int fsize = labels.size(); + Label[] restoreLabels = new Label[fsize]; + for (int i = 0; i < restoreLabels.length; i++) { + restoreLabels[i] = new Label(); + } + + // verify if restoring + Label l0 = new Label(); + + mv.visitMethodInsn(INVOKESTATIC, STACK_RECORDER, "get", "()L" + STACK_RECORDER + ";"); + mv.visitInsn(DUP); + mv.visitVarInsn(ASTORE, stackRecorderVar); + mv.visitLabel(startLabel); + + mv.visitJumpInsn(IFNULL, l0); + mv.visitVarInsn(ALOAD, stackRecorderVar); + mv.visitFieldInsn(GETFIELD, STACK_RECORDER, "isRestoring", "Z"); + mv.visitJumpInsn(IFEQ, l0); + + mv.visitVarInsn(ALOAD, stackRecorderVar); + mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, POP_METHOD + "Int", "()I"); + mv.visitTableSwitchInsn(0, fsize - 1, l0, restoreLabels); + + // switch cases + for (int i = 0; i < fsize; i++) { + Label frameLabel = (Label) labels.get(i); + mv.visitLabel(restoreLabels[i]); + + MethodInsnNode mnode = (MethodInsnNode) nodes.get(i); + + Frame frame = analyzer.getFrames()[analyzer.getIndex(mnode)]; + + // locals + int lsize = frame.getLocals(); + for (int j = lsize - 1; j >= 0; j--) { + BasicValue value = (BasicValue) frame.getLocal(j); + if (value == null) { + mv.visitInsn(ACONST_NULL); + mv.visitVarInsn(ASTORE, j); + } else if (value == BasicValue.UNINITIALIZED_VALUE) { + // TODO ?? + } else if (value == BasicValue.RETURNADDRESS_VALUE) { + // TODO ?? } else { - mv.visitTypeInsn(CHECKCAST, t.getInternalName()); + mv.visitVarInsn(ALOAD, stackRecorderVar); + Type type = value.getType(); + if (value.isReference()) { + mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, POP_METHOD + "Object", "()Ljava/lang/Object;"); + Type t = value.getType(); + String desc = t.getDescriptor(); + if (desc.charAt(0) == '[') { + mv.visitTypeInsn(CHECKCAST, desc); + } else { + mv.visitTypeInsn(CHECKCAST, t.getInternalName()); + } + mv.visitVarInsn(ASTORE, j); + + } else { + mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, getPopMethod(type), "()" + type.getDescriptor()); + mv.visitVarInsn(type.getOpcode(ISTORE), j); + } } - mv.visitVarInsn(ASTORE, j); - - } else { - mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, getPopMethod(type), "()"+type.getDescriptor()); - mv.visitVarInsn(type.getOpcode(ISTORE), j); } - } - } - // stack - int argSize = Type.getArgumentTypes(mnode.desc).length; - int ownerSize = mnode.getOpcode()==INVOKESTATIC ? 0 : 1; // TODO - int initSize = mnode.name.equals("<init>") ? 2 : 0; - int ssize = frame.getStackSize(); - for(int j = 0; j < ssize - argSize - ownerSize - initSize; j++) { - BasicValue value = (BasicValue) frame.getStack(j); - if(value==null) { - mv.visitInsn(ACONST_NULL); - } else if(value==BasicValue.UNINITIALIZED_VALUE) { - // TODO ?? - } else if(value==BasicValue.RETURNADDRESS_VALUE) { - // TODO ?? - } else if(value.isReference()) { - mv.visitVarInsn(ALOAD, stackRecorderVar); - mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, POP_METHOD + "Object", "()Ljava/lang/Object;"); - mv.visitTypeInsn(CHECKCAST, value.getType().getInternalName()); - } else { - Type type = value.getType(); - mv.visitVarInsn(ALOAD, stackRecorderVar); - mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, getPopMethod(type), "()"+type.getDescriptor()); + // stack + int argSize = Type.getArgumentTypes(mnode.desc).length; + int ownerSize = mnode.getOpcode() == INVOKESTATIC ? 0 : 1; // TODO + int initSize = mnode.name.equals("<init>") ? 2 : 0; + int ssize = frame.getStackSize(); + for (int j = 0; j < ssize - argSize - ownerSize - initSize; j++) { + BasicValue value = (BasicValue) frame.getStack(j); + if (value == null) { + mv.visitInsn(ACONST_NULL); + } else if (value == BasicValue.UNINITIALIZED_VALUE) { + // TODO ?? + } else if (value == BasicValue.RETURNADDRESS_VALUE) { + // TODO ?? + } else if (value.isReference()) { + mv.visitVarInsn(ALOAD, stackRecorderVar); + mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, POP_METHOD + "Object", "()Ljava/lang/Object;"); + mv.visitTypeInsn(CHECKCAST, value.getType().getInternalName()); + } else { + Type type = value.getType(); + mv.visitVarInsn(ALOAD, stackRecorderVar); + mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, getPopMethod(type), "()" + type.getDescriptor()); + } + } + + if (mnode.getOpcode() != INVOKESTATIC) { + mv.visitVarInsn(ALOAD, stackRecorderVar); + mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, POP_METHOD + "Reference", "()Ljava/lang/Object;"); + mv.visitTypeInsn(CHECKCAST, ((BasicValue) frame.getStack(ssize - argSize - 1)).getType().getInternalName()); + } + + // Create null types for the parameters of the method invocation + Type[] paramTypes = Type.getArgumentTypes(mnode.desc); + for (int j = 0; j < paramTypes.length; j++) { + pushDefault(paramTypes[j]); + } + + // continue to the next method + mv.visitJumpInsn(GOTO, frameLabel); } - } - - if(mnode.getOpcode()!=INVOKESTATIC) { - mv.visitVarInsn(ALOAD, stackRecorderVar); - mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, POP_METHOD + "Reference", "()Ljava/lang/Object;"); - mv.visitTypeInsn(CHECKCAST, ((BasicValue) frame.getStack(ssize-argSize-1)).getType().getInternalName()); - } - - // Create null types for the parameters of the method invocation - Type[] paramTypes = Type.getArgumentTypes(mnode.desc); - for (int j = 0; j < paramTypes.length; j++) { - pushDefault(paramTypes[j]); - } - - // continue to the next method - mv.visitJumpInsn(GOTO, frameLabel); - } - - // end of start block - mv.visitLabel(l0); - } - - public void visitLabel( Label label) { - if(currentIndex<labels.size() && label==labels.get(currentIndex)) { - int i = analyzer.getIndex(nodes.get(currentIndex)); - currentFrame = analyzer.getFrames()[i]; + + // end of start block + mv.visitLabel(l0); } - mv.visitLabel(label); - } - public void visitMethodInsn(int opcode, String owner, String name, String desc) { - mv.visitMethodInsn(opcode, owner, name, desc); - - if(currentFrame!=null) { - Label fl = new Label(); - - mv.visitVarInsn(ALOAD, stackRecorderVar); - mv.visitJumpInsn(IFNULL, fl); - mv.visitVarInsn(ALOAD, stackRecorderVar); - mv.visitFieldInsn(GETFIELD, STACK_RECORDER, "isCapturing", "Z"); - mv.visitJumpInsn(IFEQ, fl); - - // save stack - Type returnType = Type.getReturnType(desc); - boolean hasReturn = returnType!=Type.VOID_TYPE; - if(hasReturn) { - mv.visitInsn(returnType.getSize()==1 ? POP : POP2); - } - - Type[] params = Type.getArgumentTypes(desc); - int argSize = params.length; - int ownerSize = opcode==INVOKESTATIC ? 0 : 1; // TODO - int ssize = currentFrame.getStackSize() - argSize - ownerSize; - for(int i = 0; i < ssize; i++) { - BasicValue value = (BasicValue) currentFrame.getStack(i); - if(value==null) { - mv.visitInsn(POP); - } else if(value==BasicValue.UNINITIALIZED_VALUE) { - // TODO ?? - } else if(value.isReference()) { - mv.visitVarInsn(ALOAD, stackRecorderVar); - mv.visitInsn(SWAP); - mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, PUSH_METHOD + "Object", "(Ljava/lang/Object;)V"); - } else { - Type type = value.getType(); - mv.visitVarInsn(ALOAD, stackRecorderVar); - if(type.getSize()>1) { - mv.visitInsn(DUP); // dummy stack entry - mv.visitInsn(DUP2_X2); // swap2 for long/double - mv.visitInsn(POP2); - mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, getPushMethod(type), "("+type.getDescriptor()+")V"); - mv.visitInsn(POP); // remove dummy stack entry - } else { - mv.visitInsn(SWAP); - mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, getPushMethod(type), "("+type.getDescriptor()+")V"); - } + public void visitLabel(Label label) { + if (currentIndex < labels.size() && label == labels.get(currentIndex)) { + int i = analyzer.getIndex(nodes.get(currentIndex)); + currentFrame = analyzer.getFrames()[i]; } - } - - if(!isStatic) { - mv.visitVarInsn(ALOAD, stackRecorderVar); - mv.visitVarInsn(ALOAD, 0); - mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, PUSH_METHOD + "Reference", "(Ljava/lang/Object;)V"); - } - - // save locals - int fsize = currentFrame.getLocals(); - for( int j = 0; j<fsize; j++) { - BasicValue value = (BasicValue) currentFrame.getLocal(j); - if(value==null) { - // no need to save null - } else if(value==BasicValue.UNINITIALIZED_VALUE) { - // no need to save uninitialized objects - } else if(value.isReference()) { - mv.visitVarInsn(ALOAD, stackRecorderVar); - mv.visitVarInsn(ALOAD, j); - mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, PUSH_METHOD + "Object", "(Ljava/lang/Object;)V"); - } else { - mv.visitVarInsn(ALOAD, stackRecorderVar); - Type type = value.getType(); - mv.visitVarInsn(type.getOpcode(ILOAD), j); - mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, getPushMethod(type), "("+type.getDescriptor()+")V"); + mv.visitLabel(label); + } + + public void visitMethodInsn(int opcode, String owner, String name, String desc) { + mv.visitMethodInsn(opcode, owner, name, desc); + + if (currentFrame != null) { + Label fl = new Label(); + + mv.visitVarInsn(ALOAD, stackRecorderVar); + mv.visitJumpInsn(IFNULL, fl); + mv.visitVarInsn(ALOAD, stackRecorderVar); + mv.visitFieldInsn(GETFIELD, STACK_RECORDER, "isCapturing", "Z"); + mv.visitJumpInsn(IFEQ, fl); + + // save stack + Type returnType = Type.getReturnType(desc); + boolean hasReturn = returnType != Type.VOID_TYPE; + if (hasReturn) { + mv.visitInsn(returnType.getSize() == 1 ? POP : POP2); + } + + Type[] params = Type.getArgumentTypes(desc); + int argSize = params.length; + int ownerSize = opcode == INVOKESTATIC ? 0 : 1; // TODO + int ssize = currentFrame.getStackSize() - argSize - ownerSize; + for (int i = 0; i < ssize; i++) { + BasicValue value = (BasicValue) currentFrame.getStack(i); + if (value == null) { + mv.visitInsn(POP); + } else if (value == BasicValue.UNINITIALIZED_VALUE) { + // TODO ?? + } else if (value.isReference()) { + mv.visitVarInsn(ALOAD, stackRecorderVar); + mv.visitInsn(SWAP); + mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, PUSH_METHOD + "Object", "(Ljava/lang/Object;)V"); + } else { + Type type = value.getType(); + mv.visitVarInsn(ALOAD, stackRecorderVar); + if (type.getSize() > 1) { + mv.visitInsn(DUP); // dummy stack entry + mv.visitInsn(DUP2_X2); // swap2 for long/double + mv.visitInsn(POP2); + mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, getPushMethod(type), "(" + type.getDescriptor() + ")V"); + mv.visitInsn(POP); // remove dummy stack entry + } else { + mv.visitInsn(SWAP); + mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, getPushMethod(type), "(" + type.getDescriptor() + ")V"); + } + } + } + + if (!isStatic) { + mv.visitVarInsn(ALOAD, stackRecorderVar); + mv.visitVarInsn(ALOAD, 0); + mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, PUSH_METHOD + "Reference", "(Ljava/lang/Object;)V"); + } + + // save locals + int fsize = currentFrame.getLocals(); + for (int j = 0; j < fsize; j++) { + BasicValue value = (BasicValue) currentFrame.getLocal(j); + if (value == null) { + // no need to save null + } else if (value == BasicValue.UNINITIALIZED_VALUE) { + // no need to save uninitialized objects + } else if (value.isReference()) { + mv.visitVarInsn(ALOAD, stackRecorderVar); + mv.visitVarInsn(ALOAD, j); + mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, PUSH_METHOD + "Object", "(Ljava/lang/Object;)V"); + } else { + mv.visitVarInsn(ALOAD, stackRecorderVar); + Type type = value.getType(); + mv.visitVarInsn(type.getOpcode(ILOAD), j); + mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, getPushMethod(type), "(" + type.getDescriptor() + ")V"); + } + } + + mv.visitVarInsn(ALOAD, stackRecorderVar); + mv.visitIntInsn(BIPUSH, currentIndex); // TODO optimize to iconst_0... + mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, "pushInt", "(I)V"); + + Type methodReturnType = Type.getReturnType(methodDesc); + pushDefault(methodReturnType); + mv.visitInsn(methodReturnType.getOpcode(IRETURN)); + mv.visitLabel(fl); + + currentIndex++; + currentFrame = null; } - } - - mv.visitVarInsn(ALOAD, stackRecorderVar); - mv.visitIntInsn(BIPUSH, currentIndex); // TODO optimize to iconst_0... - mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, "pushInt", "(I)V"); - - Type methodReturnType = Type.getReturnType(methodDesc); - pushDefault(methodReturnType); - mv.visitInsn(methodReturnType.getOpcode(IRETURN)); - mv.visitLabel(fl); - - currentIndex++; - currentFrame = null; } - } - public void visitMaxs( int maxStack, int maxLocals) { - Label endLabel = new Label(); - mv.visitLabel(endLabel); - - mv.visitLocalVariable("__stackRecorder", "L"+STACK_RECORDER+";", null, startLabel, endLabel, stackRecorderVar); - - mv.visitMaxs(0,0); - } - - void pushDefault(Type type) { - switch(type.getSort()) { - case Type.VOID: - break; - case Type.DOUBLE: - mv.visitInsn(DCONST_0); - break; - case Type.LONG: - mv.visitInsn(LCONST_0); - break; - case Type.FLOAT: - mv.visitInsn(FCONST_0); - break; - case Type.OBJECT: - case Type.ARRAY: - mv.visitInsn(ACONST_NULL); - break; - default: - mv.visitInsn(ICONST_0); - break; + public void visitMaxs(int maxStack, int maxLocals) { + Label endLabel = new Label(); + mv.visitLabel(endLabel); + + mv.visitLocalVariable("__stackRecorder", "L" + STACK_RECORDER + ";", null, startLabel, endLabel, stackRecorderVar); + + mv.visitMaxs(0, 0); } - } - private static String[] SUFFIXES = { - "Object", // 0 void - "Int", // 1 boolean - "Int", // 2 char - "Int", // 3 byte - "Int", // 4 short - "Int", // 5 int - "Float", // 6 float - "Long", // 7 long - "Double", // 8 double - "Object", // 9 array - "Object", // 10 object + void pushDefault(Type type) { + switch (type.getSort()) { + case Type.VOID: + break; + case Type.DOUBLE: + mv.visitInsn(DCONST_0); + break; + case Type.LONG: + mv.visitInsn(LCONST_0); + break; + case Type.FLOAT: + mv.visitInsn(FCONST_0); + break; + case Type.OBJECT: + case Type.ARRAY: + mv.visitInsn(ACONST_NULL); + break; + default: + mv.visitInsn(ICONST_0); + break; + } + } + + private static String[] SUFFIXES = { + "Object", // 0 void + "Int", // 1 boolean + "Int", // 2 char + "Int", // 3 byte + "Int", // 4 short + "Int", // 5 int + "Float", // 6 float + "Long", // 7 long + "Double", // 8 double + "Object", // 9 array + "Object", // 10 object }; - String getPopMethod(Type type) { - return POP_METHOD + SUFFIXES[type.getSort()]; - } - - String getPushMethod(Type type) { - return PUSH_METHOD + SUFFIXES[type.getSort()]; - } + String getPopMethod(Type type) { + return POP_METHOD + SUFFIXES[type.getSort()]; + } + + String getPushMethod(Type type) { + return PUSH_METHOD + SUFFIXES[type.getSort()]; + } } Modified: jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/bytecode/transformation/asm/ContinuationMethodAnalyzer.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/bytecode/transformation/asm/ContinuationMethodAnalyzer.java?rev=365854&r1=365853&r2=365854&view=diff ============================================================================== --- jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/bytecode/transformation/asm/ContinuationMethodAnalyzer.java (original) +++ jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/bytecode/transformation/asm/ContinuationMethodAnalyzer.java Tue Jan 3 23:42:14 2006 @@ -61,34 +61,34 @@ public ContinuationMethodAnalyzer(String className, ClassVisitor cv, MethodVisitor mv, int access, String name, String desc, String signature, String[] exceptions) { - super( access, name, desc, signature, exceptions); + super(access, name, desc, signature, exceptions); this.className = className; this.cv = cv; this.mv = mv; } - public void visitMethodInsn( int opcode, String owner, String name, String desc) { - MethodInsnNode mnode = new MethodInsnNode(opcode, owner, name, desc); - if(opcode == INVOKESPECIAL || "<init>".equals(name)) { - methods.add(mnode); - } - if (needsFrameGuard(opcode, owner, name, desc) /* && transformer.inScope(owner, name)*/) { - Label label = new Label(); - super.visitLabel(label); - labels.add(label); - nodes.add(mnode); - } - instructions.add(mnode); + public void visitMethodInsn(int opcode, String owner, String name, String desc) { + MethodInsnNode mnode = new MethodInsnNode(opcode, owner, name, desc); + if (opcode == INVOKESPECIAL || "<init>".equals(name)) { + methods.add(mnode); + } + if (needsFrameGuard(opcode, owner, name, desc) /* && transformer.inScope(owner, name)*/) { + Label label = new Label(); + super.visitLabel(label); + labels.add(label); + nodes.add(mnode); + } + instructions.add(mnode); } public void visitEnd() { - if(instructions.size()==0 || labels.size()==0) { - accept(mv); + if (instructions.size() == 0 || labels.size() == 0) { + accept(mv); return; - } + } - this.stackRecorderVar = maxLocals; - try { + this.stackRecorderVar = maxLocals; + try { moveNew(); // TraceMethodVisitor mv = new TraceMethodVisitor(); @@ -99,85 +99,85 @@ // } // System.err.println(); - // analyzer = new Analyzer(new BasicVerifier()); - analyzer = new Analyzer(new SimpleVerifier() { - protected Class getClass( Type t) { - try { - if (t.getSort() == Type.ARRAY) { - return Class.forName(t.getDescriptor().replace('/', '.'), true, Thread.currentThread().getContextClassLoader()); - } - return Class.forName(t.getClassName(), true, Thread.currentThread().getContextClassLoader()); - } catch (ClassNotFoundException e) { - throw new RuntimeException(e.toString()); - } - } - }); - analyzer.analyze(className, this); - accept(new ContinuationMethodAdapter(this)); - - } catch(AnalyzerException ex) { - // TODO log the error or fail? - ex.printStackTrace(); - accept(mv); + // analyzer = new Analyzer(new BasicVerifier()); + analyzer = new Analyzer(new SimpleVerifier() { + protected Class getClass(Type t) { + try { + if (t.getSort() == Type.ARRAY) { + return Class.forName(t.getDescriptor().replace('/', '.'), true, Thread.currentThread().getContextClassLoader()); + } + return Class.forName(t.getClassName(), true, Thread.currentThread().getContextClassLoader()); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e.toString()); + } + } + }); + analyzer.analyze(className, this); + accept(new ContinuationMethodAdapter(this)); + + } catch (AnalyzerException ex) { + // TODO log the error or fail? + ex.printStackTrace(); + accept(mv); } } void moveNew() throws AnalyzerException { - DataflowInterpreter i = new DataflowInterpreter(); - Analyzer a = new Analyzer(i); - a.analyze(className, this); - - HashMap movable = new HashMap(); - - Frame[] frames = a.getFrames(); - for( int j = 0; j < methods.size(); j++) { - MethodInsnNode mnode = (MethodInsnNode) methods.get(j); - // require to move NEW instruction - int n = a.getIndex(mnode); - Frame f = frames[n]; - Type[] args = Type.getArgumentTypes(mnode.desc); - - DataflowValue v = (DataflowValue) f.getStack(f.getStackSize()-args.length-1); - Set insns = v.insns; - for( Iterator it = insns.iterator(); it.hasNext();) { - AbstractInsnNode ins = (AbstractInsnNode) it.next(); - if(ins.getOpcode()==NEW) { - movable.put(ins, mnode); - } else { - // other known patterns - int n1 = a.getIndex(ins); - if(ins.getOpcode()==DUP) { // <init> with params - AbstractInsnNode ins1 = (AbstractInsnNode) instructions.get(n1-1); - if(ins1.getOpcode()==NEW) { - movable.put(ins1, mnode); - } - } else if(ins.getOpcode()==SWAP) { // in exception handler - AbstractInsnNode ins1 = (AbstractInsnNode) instructions.get(n1-1); - AbstractInsnNode ins2 = (AbstractInsnNode) instructions.get(n1-2); - if(ins1.getOpcode()==DUP_X1 && ins2.getOpcode()==NEW) { - movable.put(ins2, mnode); + DataflowInterpreter i = new DataflowInterpreter(); + Analyzer a = new Analyzer(i); + a.analyze(className, this); + + HashMap movable = new HashMap(); + + Frame[] frames = a.getFrames(); + for (int j = 0; j < methods.size(); j++) { + MethodInsnNode mnode = (MethodInsnNode) methods.get(j); + // require to move NEW instruction + int n = a.getIndex(mnode); + Frame f = frames[n]; + Type[] args = Type.getArgumentTypes(mnode.desc); + + DataflowValue v = (DataflowValue) f.getStack(f.getStackSize() - args.length - 1); + Set insns = v.insns; + for (Iterator it = insns.iterator(); it.hasNext();) { + AbstractInsnNode ins = (AbstractInsnNode) it.next(); + if (ins.getOpcode() == NEW) { + movable.put(ins, mnode); + } else { + // other known patterns + int n1 = a.getIndex(ins); + if (ins.getOpcode() == DUP) { // <init> with params + AbstractInsnNode ins1 = (AbstractInsnNode) instructions.get(n1 - 1); + if (ins1.getOpcode() == NEW) { + movable.put(ins1, mnode); + } + } else if (ins.getOpcode() == SWAP) { // in exception handler + AbstractInsnNode ins1 = (AbstractInsnNode) instructions.get(n1 - 1); + AbstractInsnNode ins2 = (AbstractInsnNode) instructions.get(n1 - 2); + if (ins1.getOpcode() == DUP_X1 && ins2.getOpcode() == NEW) { + movable.put(ins2, mnode); + } } } - } - } - } + } + } - int updateMaxStack = 0; - for( Iterator it = movable.entrySet().iterator(); it.hasNext();) { + int updateMaxStack = 0; + for (Iterator it = movable.entrySet().iterator(); it.hasNext();) { Map.Entry e = (Map.Entry) it.next(); AbstractInsnNode node1 = (AbstractInsnNode) e.getKey(); int n1 = instructions.indexOf(node1); - AbstractInsnNode node2 = (AbstractInsnNode) instructions.get(n1+1); - AbstractInsnNode node3 = (AbstractInsnNode) instructions.get(n1+2); + AbstractInsnNode node2 = (AbstractInsnNode) instructions.get(n1 + 1); + AbstractInsnNode node3 = (AbstractInsnNode) instructions.get(n1 + 2); int producer = node2.getOpcode(); instructions.remove(node1); // NEW boolean requireDup = false; - if(producer==DUP) { + if (producer == DUP) { instructions.remove(node2); // DUP requireDup = true; - } else if(producer==DUP_X1) { + } else if (producer == DUP_X1) { instructions.remove(node2); // DUP_X1 instructions.remove(node3); // SWAP requireDup = true; @@ -186,25 +186,25 @@ MethodInsnNode mnode = (MethodInsnNode) e.getValue(); int nm = instructions.indexOf(mnode); - int varOffset = stackRecorderVar+1; + int varOffset = stackRecorderVar + 1; Type[] args = Type.getArgumentTypes(mnode.desc); // optimizations for some common cases - if(args.length==0) { + if (args.length == 0) { instructions.add(nm++, node1); // NEW - if(requireDup) { + if (requireDup) { instructions.add(nm++, new InsnNode(DUP)); } continue; } - if(args.length==1 && args[0].getSize()==1) { + if (args.length == 1 && args[0].getSize() == 1) { instructions.add(nm++, node1); // NEW - if(requireDup) { + if (requireDup) { instructions.add(nm++, new InsnNode(DUP)); instructions.add(nm++, new InsnNode(DUP2_X1)); instructions.add(nm++, new InsnNode(POP2)); - updateMaxStack = updateMaxStack<2 ? 2 : updateMaxStack; // a two extra slots for temp values + updateMaxStack = updateMaxStack < 2 ? 2 : updateMaxStack; // a two extra slots for temp values } else { instructions.add(nm++, new InsnNode(SWAP)); } @@ -212,18 +212,18 @@ } // TODO this one untested! - if((args.length==1 && args[0].getSize()==2) || - (args.length==2 && args[0].getSize()==1 && args[1].getSize()==1)) { + if ((args.length == 1 && args[0].getSize() == 2) || + (args.length == 2 && args[0].getSize() == 1 && args[1].getSize() == 1)) { instructions.add(nm++, node1); // NEW - if(requireDup) { + if (requireDup) { instructions.add(nm++, new InsnNode(DUP)); instructions.add(nm++, new InsnNode(DUP2_X2)); instructions.add(nm++, new InsnNode(POP2)); - updateMaxStack = updateMaxStack<2 ? 2 : updateMaxStack; // a two extra slots for temp values + updateMaxStack = updateMaxStack < 2 ? 2 : updateMaxStack; // a two extra slots for temp values } else { instructions.add(nm++, new InsnNode(DUP_X2)); instructions.add(nm++, new InsnNode(POP)); - updateMaxStack = updateMaxStack<1 ? 1 : updateMaxStack; // an extra slot for temp value + updateMaxStack = updateMaxStack < 1 ? 1 : updateMaxStack; // an extra slot for temp value } continue; } @@ -235,41 +235,41 @@ instructions.add(nm++, new VarInsnNode(type.getOpcode(ISTORE), varOffset)); varOffset += type.getSize(); } - if(varOffset>maxLocals) { + if (varOffset > maxLocals) { maxLocals = varOffset; } instructions.add(nm++, node1); // NEW - if(requireDup) { + if (requireDup) { instructions.add(nm++, new InsnNode(DUP)); } // restore stack - for( int j = 0; j < args.length; j++) { - Type type = args[j]; - varOffset -= type.getSize(); - instructions.add(nm++, new VarInsnNode(type.getOpcode(ILOAD), varOffset)); - // clean up store to avoid memory leak? - if(type.getSort()==Type.OBJECT || type.getSort()==Type.ARRAY) { - updateMaxStack = updateMaxStack<1 ? 1 : updateMaxStack; // an extra slot for ACONST_NULL - instructions.add(nm++, new InsnNode(ACONST_NULL)); - instructions.add(nm++, new VarInsnNode(type.getOpcode(ISTORE), varOffset)); - } - } - } + for (int j = 0; j < args.length; j++) { + Type type = args[j]; + varOffset -= type.getSize(); + instructions.add(nm++, new VarInsnNode(type.getOpcode(ILOAD), varOffset)); + // clean up store to avoid memory leak? + if (type.getSort() == Type.OBJECT || type.getSort() == Type.ARRAY) { + updateMaxStack = updateMaxStack < 1 ? 1 : updateMaxStack; // an extra slot for ACONST_NULL + instructions.add(nm++, new InsnNode(ACONST_NULL)); + instructions.add(nm++, new VarInsnNode(type.getOpcode(ISTORE), varOffset)); + } + } + } - maxStack += updateMaxStack; + maxStack += updateMaxStack; } // TODO - boolean needsFrameGuard( int opcode, String owner, String name, String desc ) { - if (opcode == Opcodes.INVOKEINTERFACE || - (opcode == Opcodes.INVOKESPECIAL && !"<init>".equals(name)) || - opcode == Opcodes.INVOKESTATIC || - opcode == Opcodes.INVOKEVIRTUAL ) { - return true; - } - return false; + boolean needsFrameGuard(int opcode, String owner, String name, String desc) { + if (opcode == Opcodes.INVOKEINTERFACE || + (opcode == Opcodes.INVOKESPECIAL && !"<init>".equals(name)) || + opcode == Opcodes.INVOKESTATIC || + opcode == Opcodes.INVOKEVIRTUAL) { + return true; + } + return false; } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]