Search in sources :

Example 6 with MethodGen

use of org.apache.bcel.generic.MethodGen in project candle-decompiler by bradsdavis.

the class ClassIntermediateVisitor method visitMethod.

@Override
public void visitMethod(Method obj) {
    MethodGen methodGenerator = new MethodGen(obj, this.javaClass.getClassName(), this.constantPool);
    LOG.debug("Processing MethodInvocation: " + methodGenerator.toString());
    IntermediateContext intermediateContext = new IntermediateContext(this.javaClass, methodGenerator);
    InstructionList instructions = methodGenerator.getInstructionList();
    instructions.setPositions(true);
    InstructionGraphFactory igf = new InstructionGraphFactory(instructions, methodGenerator.getExceptionHandlers());
    InstructionGraphContext igc = igf.process();
    List<InstructionGraphEnhancer> iges = new ArrayList<InstructionGraphEnhancer>();
    iges.add(new InstructionGraphWriter(igc, "before.dot"));
    iges.add(new SplitInstructionEnhancer(igc));
    iges.add(new ConditionEdgeEnhancer(igc));
    iges.add(new ExceptionEdgeEnhancer(igc, methodGenerator.getExceptionHandlers()));
    iges.add(new InstructionToIntermediateEnhancer(igc, intermediateContext));
    iges.add(new BackEdgeEnhancer(igc));
    iges.add(new LoopHeader(igc));
    iges.add(new ContinuousLoop(igc));
    iges.add(new NonIntermediateEliminator(igc));
    iges.add(new InstructionGraphWriter(igc, "after.dot"));
    for (InstructionGraphEnhancer ige : iges) {
        ige.process();
    }
    IntermediateGraphTransformer igt = new IntermediateGraphTransformer(igc);
    IntermediateGraphContext interGraphContext = igt.getIntermediateGraphContext();
    processIntermediate(interGraphContext);
    MethodBlock method = extractMethodSignature(methodGenerator);
    BlockVisitor iv = new BlockVisitor(interGraphContext, method);
    iv.process();
    classBlock.addChild(method);
    method.setParent(classBlock);
}
Also used : InstructionGraphEnhancer(org.candle.decompiler.instruction.graph.enhancer.InstructionGraphEnhancer) IntermediateGraphTransformer(org.candle.decompiler.intermediate.graph.IntermediateGraphTransformer) MethodBlock(org.candle.decompiler.ast.MethodBlock) BackEdgeEnhancer(org.candle.decompiler.instruction.graph.enhancer.BackEdgeEnhancer) InstructionGraphWriter(org.candle.decompiler.instruction.graph.enhancer.InstructionGraphWriter) InstructionList(org.apache.bcel.generic.InstructionList) ContinuousLoop(org.candle.decompiler.instruction.graph.enhancer.ContinuousLoop) ArrayList(java.util.ArrayList) MethodGen(org.apache.bcel.generic.MethodGen) SplitInstructionEnhancer(org.candle.decompiler.instruction.graph.enhancer.SplitInstructionEnhancer) InstructionToIntermediateEnhancer(org.candle.decompiler.instruction.graph.enhancer.InstructionToIntermediateEnhancer) InstructionGraphContext(org.candle.decompiler.instruction.graph.InstructionGraphContext) IntermediateGraphContext(org.candle.decompiler.intermediate.graph.context.IntermediateGraphContext) ConditionEdgeEnhancer(org.candle.decompiler.instruction.graph.enhancer.ConditionEdgeEnhancer) InstructionGraphFactory(org.candle.decompiler.instruction.graph.InstructionGraphFactory) NonIntermediateEliminator(org.candle.decompiler.instruction.graph.enhancer.NonIntermediateEliminator) ExceptionEdgeEnhancer(org.candle.decompiler.instruction.graph.enhancer.ExceptionEdgeEnhancer) LoopHeader(org.candle.decompiler.instruction.graph.enhancer.LoopHeader) BlockVisitor(org.candle.decompiler.ast.BlockVisitor)

Example 7 with MethodGen

use of org.apache.bcel.generic.MethodGen in project jop by jop-devel.

the class FindUsedConstants method find.

private void find(Method method) {
    MethodGen mg = new MethodGen(method, clazz.getClassName(), cpool);
    InstructionList il = mg.getInstructionList();
    InstructionFinder f = new InstructionFinder(il);
    // find instructions that access the constant pool
    // collect all indices to constants in ClassInfo
    String cpInstr = "CPInstruction";
    for (Iterator it = f.search(cpInstr); it.hasNext(); ) {
        InstructionHandle[] match = (InstructionHandle[]) it.next();
        InstructionHandle first = match[0];
        CPInstruction ii = (CPInstruction) first.getInstruction();
        int idx = ii.getIndex();
        Constant co = cpool.getConstant(idx);
        int len = 1;
        switch(co.getTag()) {
            case Constants.CONSTANT_Long:
            case Constants.CONSTANT_Double:
                len = 2;
                break;
        }
        // we don't need the field references in the cpool anymore
        if (co.getTag() != Constants.CONSTANT_Fieldref) {
            getCli().addUsedConst(idx, len);
        }
    // also modify the index!
    //			Constant cnst = cpool.getConstant(ii.getIndex());
    //			int newIndex = addConstant(cnst);
    //System.out.println(ii+" -> "+newIndex);
    //			ii.setIndex(newIndex);			
    }
    il.dispose();
    CodeExceptionGen[] et = mg.getExceptionHandlers();
    for (int i = 0; i < et.length; i++) {
        ObjectType ctype = et[i].getCatchType();
        if (ctype != null) {
            getCli().addUsedConst(cpool.lookupClass(ctype.getClassName()), 1);
        }
    }
}
Also used : InstructionList(org.apache.bcel.generic.InstructionList) Constant(org.apache.bcel.classfile.Constant) InstructionFinder(org.apache.bcel.util.InstructionFinder) MethodGen(org.apache.bcel.generic.MethodGen) InstructionHandle(org.apache.bcel.generic.InstructionHandle) CPInstruction(org.apache.bcel.generic.CPInstruction) ObjectType(org.apache.bcel.generic.ObjectType) Iterator(java.util.Iterator) CodeExceptionGen(org.apache.bcel.generic.CodeExceptionGen)

Example 8 with MethodGen

use of org.apache.bcel.generic.MethodGen in project jop by jop-devel.

the class ReplaceNativeAndCPIdx method replace.

private Method replace(Method method) {
    MethodGen mg = new MethodGen(method, clazz.getClassName(), cpoolgen);
    InstructionList il = mg.getInstructionList();
    InstructionFinder f = new InstructionFinder(il);
    String methodId = method.getName() + method.getSignature();
    OldMethodInfo mi = getCli().getMethodInfo(methodId);
    // find invokes first and replace call to Native by
    // JOP native instructions.
    String invokeStr = "InvokeInstruction";
    for (Iterator i = f.search(invokeStr); i.hasNext(); ) {
        InstructionHandle[] match = (InstructionHandle[]) i.next();
        InstructionHandle first = match[0];
        InvokeInstruction ii = (InvokeInstruction) first.getInstruction();
        if (ii.getClassName(cpoolgen).equals(JOPizer.nativeClass)) {
            short opid = (short) JopInstr.getNative(ii.getMethodName(cpoolgen));
            if (opid == -1) {
                System.err.println(method.getName() + ": cannot locate " + ii.getMethodName(cpoolgen) + ". Replacing with NOP.");
                first.setInstruction(new NOP());
            } else {
                first.setInstruction(new NativeInstruction(opid, (short) 1));
                ((JOPizer) ai).outTxt.println("\t" + first.getPosition());
                // then we remove pc+2 and pc+1 from the MGCI info
                if (JOPizer.dumpMgci) {
                    il.setPositions();
                    int pc = first.getPosition();
                    // important: take the high one first
                    GCRTMethodInfo.removePC(pc + 2, mi);
                    GCRTMethodInfo.removePC(pc + 1, mi);
                }
            }
        }
        if (ii instanceof INVOKESPECIAL) {
            // not an initializer
            if (!ii.getMethodName(cpoolgen).equals("<init>")) {
                // check if this is a super invoke
                // TODO this is just a hack, use InvokeSite.isInvokeSuper() when this is ported to the new framework!
                boolean isSuper = false;
                String declaredType = ii.getClassName(cpoolgen);
                JopClassInfo cls = getCli();
                OldClassInfo superClass = cls.superClass;
                while (superClass != null) {
                    if (superClass.clazz.getClassName().equals(declaredType)) {
                        isSuper = true;
                        break;
                    }
                    if ("java.lang.Object".equals(superClass.clazz.getClassName())) {
                        break;
                    }
                    superClass = superClass.superClass;
                }
                if (isSuper) {
                    Integer idx = ii.getIndex();
                    int new_index = getCli().cpoolUsed.indexOf(idx) + 1;
                    first.setInstruction(new JOPSYS_INVOKESUPER((short) new_index));
                // System.err.println("invokesuper "+ii.getClassName(cpoolgen)+"."+ii.getMethodName(cpoolgen));
                }
            }
        }
    }
    if (JOPizer.CACHE_INVAL) {
        f = new InstructionFinder(il);
        // find volatile reads and insert cache invalidation bytecode
        String fieldInstr = "GETFIELD|GETSTATIC|PUTFIELD|PUTSTATIC";
        for (Iterator i = f.search(fieldInstr); i.hasNext(); ) {
            InstructionHandle[] match = (InstructionHandle[]) i.next();
            InstructionHandle ih = match[0];
            FieldInstruction fi = (FieldInstruction) ih.getInstruction();
            JavaClass jc = JOPizer.jz.cliMap.get(fi.getClassName(cpoolgen)).clazz;
            Field field = null;
            while (field == null) {
                Field[] fields = jc.getFields();
                for (int k = 0; k < fields.length; k++) {
                    if (fields[k].getName().equals(fi.getFieldName(cpoolgen))) {
                        field = fields[k];
                        break;
                    }
                }
                if (field == null) {
                    try {
                        jc = jc.getSuperClass();
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                        throw new Error();
                    }
                }
            }
            if (field.isVolatile()) {
                if (field.getType().getSize() < 2) {
                    if (fi instanceof GETFIELD || fi instanceof GETSTATIC) {
                        ih.setInstruction(new InvalidateInstruction());
                        ih = il.append(ih, fi);
                    }
                } else {
                    // this only works because we do not throw a
                    // NullPointerException for monitorenter/-exit!
                    ih.setInstruction(new ACONST_NULL());
                    ih = il.append(ih, new MONITORENTER());
                    ih = il.append(ih, fi);
                    ih = il.append(ih, new ACONST_NULL());
                    ih = il.append(ih, new MONITOREXIT());
                }
            }
        }
    }
    f = new InstructionFinder(il);
    // find instructions that access the constant pool
    // and replace the index by the new value from ClassInfo
    String cpInstr = "CPInstruction";
    for (Iterator it = f.search(cpInstr); it.hasNext(); ) {
        InstructionHandle[] match = (InstructionHandle[]) it.next();
        InstructionHandle ih = match[0];
        CPInstruction cpii = (CPInstruction) ih.getInstruction();
        int index = cpii.getIndex();
        // we have to grab the information before we change
        // the CP index.
        FieldInstruction fi = null;
        Type ft = null;
        if (cpii instanceof FieldInstruction) {
            fi = (FieldInstruction) ih.getInstruction();
            ft = fi.getFieldType(cpoolgen);
        }
        Integer idx = new Integer(index);
        // pos is the new position in the reduced constant pool
        // idx is the position in the 'original' unresolved cpool
        int pos = getCli().cpoolUsed.indexOf(idx);
        int new_index = pos + 1;
        // and putfield and by address for getstatic and putstatic
        if (cpii instanceof GETFIELD || cpii instanceof PUTFIELD || cpii instanceof GETSTATIC || cpii instanceof PUTSTATIC) {
            // we use the offset instead of the CP index
            new_index = getFieldOffset(cp, index);
        } else {
            if (pos == -1) {
                System.out.println("Error: constant " + index + " " + cpoolgen.getConstant(index) + " not found");
                System.out.println("new cpool: " + getCli().cpoolUsed);
                System.out.println("original cpool: " + cpoolgen);
                System.exit(-1);
            }
        }
        // set new index, position starts at
        // 1 as cp points to the length of the pool
        cpii.setIndex(new_index);
        if (cpii instanceof FieldInstruction) {
            boolean isRef = ft instanceof ReferenceType;
            boolean isLong = ft == BasicType.LONG || ft == BasicType.DOUBLE;
            if (fi instanceof GETSTATIC) {
                if (isRef) {
                    ih.setInstruction(new GETSTATIC_REF((short) new_index));
                } else if (isLong) {
                    ih.setInstruction(new GETSTATIC_LONG((short) new_index));
                }
            } else if (fi instanceof PUTSTATIC) {
                if (isRef) {
                    if (!com.jopdesign.build.JOPizer.USE_RTTM) {
                        ih.setInstruction(new PUTSTATIC_REF((short) new_index));
                    }
                } else if (isLong) {
                    ih.setInstruction(new PUTSTATIC_LONG((short) new_index));
                }
            } else if (fi instanceof GETFIELD) {
                if (isRef) {
                    ih.setInstruction(new GETFIELD_REF((short) new_index));
                } else if (isLong) {
                    ih.setInstruction(new GETFIELD_LONG((short) new_index));
                }
            } else if (fi instanceof PUTFIELD) {
                if (isRef) {
                    if (!com.jopdesign.build.JOPizer.USE_RTTM) {
                        ih.setInstruction(new PUTFIELD_REF((short) new_index));
                    }
                } else if (isLong) {
                    ih.setInstruction(new PUTFIELD_LONG((short) new_index));
                }
            }
        }
    }
    Method m = mg.getMethod();
    il.dispose();
    return m;
}
Also used : InstructionList(org.apache.bcel.generic.InstructionList) InstructionFinder(org.apache.bcel.util.InstructionFinder) MONITORENTER(org.apache.bcel.generic.MONITORENTER) MethodGen(org.apache.bcel.generic.MethodGen) InstructionHandle(org.apache.bcel.generic.InstructionHandle) ReferenceType(org.apache.bcel.generic.ReferenceType) PUTSTATIC(org.apache.bcel.generic.PUTSTATIC) Field(org.apache.bcel.classfile.Field) CPInstruction(org.apache.bcel.generic.CPInstruction) Iterator(java.util.Iterator) MONITOREXIT(org.apache.bcel.generic.MONITOREXIT) ACONST_NULL(org.apache.bcel.generic.ACONST_NULL) PUTFIELD(org.apache.bcel.generic.PUTFIELD) Method(org.apache.bcel.classfile.Method) INVOKESPECIAL(org.apache.bcel.generic.INVOKESPECIAL) NOP(org.apache.bcel.generic.NOP) InvokeInstruction(org.apache.bcel.generic.InvokeInstruction) GETFIELD(org.apache.bcel.generic.GETFIELD) ReferenceType(org.apache.bcel.generic.ReferenceType) Type(org.apache.bcel.generic.Type) BasicType(org.apache.bcel.generic.BasicType) ConstantNameAndType(org.apache.bcel.classfile.ConstantNameAndType) JavaClass(org.apache.bcel.classfile.JavaClass) FieldInstruction(org.apache.bcel.generic.FieldInstruction) GETSTATIC(org.apache.bcel.generic.GETSTATIC)

Example 9 with MethodGen

use of org.apache.bcel.generic.MethodGen in project jop by jop-devel.

the class ClassInfo method createMethod.

/**
     * Create a new non-static, package-visible method with the given name and descriptor.
     *
     * @param memberID the membername and descriptor of the method (classname is ignored).
     * @param argNames the names of the parameters
     * @param code an InstructionList to set to the method as code, or if null, create an abstract method.
     * @return the new method or an existing method with that memberID.
     */
public MethodInfo createMethod(MemberID memberID, String[] argNames, InstructionList code) {
    MethodInfo method = methods.get(memberID.getMethodSignature());
    if (method != null) {
        method.setAbstract(code == null);
        if (code != null) {
            method.getCode().setInstructionList(code);
        }
        return method;
    }
    Descriptor desc = memberID.getDescriptor();
    int flags = (code == null) ? Constants.ACC_ABSTRACT : 0;
    method = new MethodInfo(this, new MethodGen(flags, desc.getType(), desc.getArgumentTypes(), argNames, memberID.getMemberName(), classGen.getClassName(), code, cpg));
    methods.put(memberID.getMethodSignature(), method);
    classGen.addMethod(method.getMethod(false));
    return method;
}
Also used : Descriptor(com.jopdesign.common.type.Descriptor) MethodGen(org.apache.bcel.generic.MethodGen)

Aggregations

MethodGen (org.apache.bcel.generic.MethodGen)9 InstructionList (org.apache.bcel.generic.InstructionList)7 InstructionHandle (org.apache.bcel.generic.InstructionHandle)6 Iterator (java.util.Iterator)4 ObjectType (org.apache.bcel.generic.ObjectType)4 Type (org.apache.bcel.generic.Type)4 InstructionFinder (org.apache.bcel.util.InstructionFinder)4 JavaClass (org.apache.bcel.classfile.JavaClass)3 BranchInstruction (org.apache.bcel.generic.BranchInstruction)3 CPInstruction (org.apache.bcel.generic.CPInstruction)3 ConstantPoolGen (org.apache.bcel.generic.ConstantPoolGen)3 InstructionFactory (org.apache.bcel.generic.InstructionFactory)3 ArrayList (java.util.ArrayList)2 Random (java.util.Random)2 Constant (org.apache.bcel.classfile.Constant)2 ConstantNameAndType (org.apache.bcel.classfile.ConstantNameAndType)2 Method (org.apache.bcel.classfile.Method)2 ClassGen (org.apache.bcel.generic.ClassGen)2 INSTANCEOF (org.apache.bcel.generic.INSTANCEOF)2 PUSH (org.apache.bcel.generic.PUSH)2