Search in sources :

Example 1 with ArrayType

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

the class ValueMapAnalysis method transfer.

public void transfer(Instruction instruction) {
    switch(instruction.getOpcode()) {
        case Constants.NOP:
            break;
        case Constants.ACONST_NULL:
            values.push(new ValueInfo(Type.NULL));
            break;
        case Constants.ICONST_M1:
        case Constants.ICONST_0:
        case Constants.ICONST_1:
        case Constants.ICONST_2:
        case Constants.ICONST_3:
        case Constants.ICONST_4:
        case Constants.ICONST_5:
        case Constants.BIPUSH:
        case Constants.SIPUSH:
            {
                ConstantPushInstruction instr = (ConstantPushInstruction) instruction;
                int value = instr.getValue().intValue();
                values.push(new ValueInfo(instr.getType(cpg), new ConstantIntegerInfo(value)));
                break;
            }
        case Constants.LCONST_0:
        case Constants.LCONST_1:
            {
                ConstantPushInstruction instr = (ConstantPushInstruction) instruction;
                long value = instr.getValue().longValue();
                values.push(new ValueInfo(instr.getType(cpg), new ConstantLongInfo(value)));
                break;
            }
        case Constants.FCONST_0:
        case Constants.FCONST_1:
        case Constants.FCONST_2:
            {
                ConstantPushInstruction instr = (ConstantPushInstruction) instruction;
                float value = instr.getValue().floatValue();
                values.push(new ValueInfo(instr.getType(cpg), new ConstantFloatInfo(value)));
                break;
            }
        case Constants.DCONST_0:
        case Constants.DCONST_1:
            {
                ConstantPushInstruction instr = (ConstantPushInstruction) instruction;
                double value = instr.getValue().doubleValue();
                values.push(new ValueInfo(instr.getType(cpg), new ConstantDoubleInfo(value)));
                break;
            }
        case Constants.LDC:
        case Constants.LDC_W:
        case Constants.LDC2_W:
            {
                CPInstruction instr = (CPInstruction) instruction;
                values.push(new ValueInfo(methodInfo.getClassInfo().getConstantInfo(instr.getIndex())));
                break;
            }
        case Constants.ISTORE_0:
        case Constants.ISTORE_1:
        case Constants.ISTORE_2:
        case Constants.ISTORE_3:
        case Constants.ISTORE:
        case Constants.ASTORE_0:
        case Constants.ASTORE_1:
        case Constants.ASTORE_2:
        case Constants.ASTORE_3:
        case Constants.ASTORE:
        case Constants.LSTORE_0:
        case Constants.LSTORE_1:
        case Constants.LSTORE_2:
        case Constants.LSTORE_3:
        case Constants.LSTORE:
        case Constants.DSTORE_0:
        case Constants.DSTORE_1:
        case Constants.DSTORE_2:
        case Constants.DSTORE_3:
        case Constants.DSTORE:
        case Constants.FSTORE_0:
        case Constants.FSTORE_1:
        case Constants.FSTORE_2:
        case Constants.FSTORE_3:
        case Constants.FSTORE:
            {
                StoreInstruction instr = (StoreInstruction) instruction;
                int index = instr.getIndex();
                values.setLocalValue(index, values.popValue());
                break;
            }
        case Constants.ILOAD_0:
        case Constants.ILOAD_1:
        case Constants.ILOAD_2:
        case Constants.ILOAD_3:
        case Constants.ILOAD:
        case Constants.LLOAD_0:
        case Constants.LLOAD_1:
        case Constants.LLOAD_2:
        case Constants.LLOAD_3:
        case Constants.LLOAD:
        case Constants.FLOAD_0:
        case Constants.FLOAD_1:
        case Constants.FLOAD_2:
        case Constants.FLOAD_3:
        case Constants.FLOAD:
        case Constants.DLOAD_0:
        case Constants.DLOAD_1:
        case Constants.DLOAD_2:
        case Constants.DLOAD_3:
        case Constants.DLOAD:
        case Constants.ALOAD_0:
        case Constants.ALOAD_1:
        case Constants.ALOAD_2:
        case Constants.ALOAD_3:
        case Constants.ALOAD:
            {
                LoadInstruction instr = (LoadInstruction) instruction;
                int index = instr.getIndex();
                values.push(values.getLocalValue(index));
                break;
            }
        case Constants.DUP:
            values.push(values.top());
            break;
        case Constants.DUP_X1:
            values.insert(2, values.top());
            break;
        case Constants.DUP_X2:
            values.insert(3, values.top());
        case Constants.DUP2:
            values.push(values.top(1), false);
            values.push(values.top(1), false);
            break;
        case Constants.DUP2_X1:
            values.insert(3, values.top(1));
            values.insert(3, values.top(0));
            break;
        case Constants.DUP2_X2:
            values.insert(4, values.top(1));
            values.insert(4, values.top(0));
            break;
        case Constants.POP:
            values.pop();
            break;
        case Constants.POP2:
            values.pop();
            values.pop();
            break;
        case Constants.SWAP:
            values.insert(1, values.pop());
            break;
        case Constants.IASTORE:
        case Constants.LASTORE:
        case Constants.FASTORE:
        case Constants.DASTORE:
        case Constants.CASTORE:
        case Constants.SASTORE:
        case Constants.BASTORE:
        case Constants.AASTORE:
            values.popValue();
            values.pop();
            values.pop();
            break;
        case Constants.IALOAD:
        case Constants.LALOAD:
        case Constants.FALOAD:
        case Constants.DALOAD:
        case Constants.CALOAD:
        case Constants.SALOAD:
        case Constants.BALOAD:
        case Constants.AALOAD:
            {
                values.pop();
                values.pop();
                Type t = ((ArrayInstruction) instruction).getType(cpg);
                values.push(new ValueInfo(t));
                break;
            }
        case Constants.IINC:
            {
                int i = ((IINC) instruction).getIndex();
                ValueInfo old = values.getLocalValue(i);
                if (old.isConstantValue() && old.getConstantValue() instanceof ConstantIntegerInfo) {
                    ConstantIntegerInfo value = (ConstantIntegerInfo) old.getConstantValue();
                    int newval = value.getValue() + ((IINC) instruction).getIncrement();
                    values.setLocalValue(i, new ValueInfo(new ConstantIntegerInfo(newval)));
                } else {
                    values.setLocalValue(i, new ValueInfo(Type.INT));
                }
                break;
            }
        case Constants.IADD:
        case Constants.ISUB:
        case Constants.IMUL:
        case Constants.IDIV:
        case Constants.IREM:
        case Constants.IAND:
        case Constants.IOR:
        case Constants.IXOR:
        case Constants.ISHL:
        case Constants.ISHR:
        case Constants.IUSHR:
            values.pop();
        case Constants.INEG:
            values.pop();
            values.push(new ValueInfo(Type.INT));
            break;
        case Constants.FADD:
        case Constants.FSUB:
        case Constants.FMUL:
        case Constants.FDIV:
        case Constants.FREM:
            values.pop();
        case Constants.FNEG:
            values.pop();
            values.push(new ValueInfo(Type.FLOAT));
            break;
        case Constants.LADD:
        case Constants.LSUB:
        case Constants.LMUL:
        case Constants.LDIV:
        case Constants.LREM:
        case Constants.LAND:
        case Constants.LOR:
        case Constants.LXOR:
            values.pop();
            values.pop();
        case Constants.LNEG:
            values.pop();
            values.pop();
            values.push(new ValueInfo(Type.LONG));
            break;
        case Constants.DADD:
        case Constants.DSUB:
        case Constants.DMUL:
        case Constants.DDIV:
        case Constants.DREM:
            values.pop();
            values.pop();
        case Constants.DNEG:
            values.pop();
            values.pop();
            values.push(new ValueInfo(Type.DOUBLE));
            break;
        case Constants.LSHL:
        case Constants.LSHR:
        case Constants.LUSHR:
            values.pop();
            values.pop();
            values.pop();
            values.push(new ValueInfo(Type.LONG));
            break;
        case Constants.I2B:
        case Constants.I2C:
        case Constants.I2S:
        case Constants.I2L:
        case Constants.I2F:
        case Constants.I2D:
        case Constants.L2I:
        case Constants.L2F:
        case Constants.L2D:
        case Constants.F2I:
        case Constants.F2L:
        case Constants.F2D:
        case Constants.D2I:
        case Constants.D2L:
        case Constants.D2F:
            {
                values.popValue();
                values.push(new ValueInfo(((ConversionInstruction) instruction).getType(cpg)));
                break;
            }
        case Constants.LCMP:
        case Constants.DCMPL:
        case Constants.DCMPG:
            values.pop();
            values.pop();
        case Constants.FCMPL:
        case Constants.FCMPG:
            values.pop();
            values.pop();
            values.push(new ValueInfo(Type.INT));
            break;
        case Constants.IF_ICMPEQ:
        case Constants.IF_ICMPNE:
        case Constants.IF_ICMPLT:
        case Constants.IF_ICMPGE:
        case Constants.IF_ICMPGT:
        case Constants.IF_ICMPLE:
        case Constants.IF_ACMPEQ:
        case Constants.IF_ACMPNE:
            values.pop();
        case Constants.IFEQ:
        case Constants.IFNE:
        case Constants.IFLT:
        case Constants.IFGE:
        case Constants.IFLE:
        case Constants.IFGT:
        case Constants.IFNULL:
        case Constants.IFNONNULL:
            values.pop();
            break;
        case Constants.GOTO:
        case Constants.RET:
            break;
        case Constants.JSR:
            // This is of type 'returnAddress'
            values.push(new ValueInfo(Type.INT));
            break;
        case Constants.ARETURN:
        case Constants.IRETURN:
        case Constants.LRETURN:
        case Constants.FRETURN:
        case Constants.DRETURN:
        case Constants.RETURN:
            values.clearStack();
            break;
        case Constants.LOOKUPSWITCH:
        case Constants.TABLESWITCH:
            values.pop();
            break;
        case Constants.GETFIELD:
            values.pop();
        case Constants.GETSTATIC:
            {
                FieldInstruction f = (FieldInstruction) instruction;
                ConstantFieldInfo field = (ConstantFieldInfo) methodInfo.getClassInfo().getConstantInfo(f.getIndex());
                values.push(new ValueInfo(f.getFieldType(cpg), field.getValue()));
                break;
            }
        case Constants.PUTFIELD:
            values.pop();
        case Constants.PUTSTATIC:
            {
                FieldInstruction f = (FieldInstruction) instruction;
                values.pop(f.getFieldType(cpg).getSize());
                break;
            }
        case Constants.INVOKEVIRTUAL:
        case Constants.INVOKEINTERFACE:
        case Constants.INVOKESPECIAL:
            values.pop();
        case Constants.INVOKESTATIC:
            {
                InvokeInstruction invoke = (InvokeInstruction) instruction;
                values.pop(TypeHelper.getNumSlots(invoke.getArgumentTypes(cpg)));
                values.push(new ValueInfo(invoke.getReturnType(cpg)));
                break;
            }
        case Constants.MONITORENTER:
        case Constants.MONITOREXIT:
            values.pop();
            break;
        case Constants.ATHROW:
            ValueInfo ref = values.pop();
            values.clearStack();
            values.push(ref);
            break;
        case Constants.CHECKCAST:
            break;
        case Constants.INSTANCEOF:
            values.pop();
            values.push(new ValueInfo(Type.INT));
            break;
        case Constants.NEW:
            values.push(new ValueInfo(((NEW) instruction).getType(cpg)));
            break;
        case Constants.NEWARRAY:
            {
                Type t = ((NEWARRAY) instruction).getType();
                values.push(new ValueInfo(new ArrayType(t, 1)));
                break;
            }
        case Constants.ANEWARRAY:
            {
                Type t = ((ANEWARRAY) instruction).getType(cpg);
                values.push(new ValueInfo(new ArrayType(t, 1)));
                break;
            }
        case Constants.MULTIANEWARRAY:
            {
                MULTIANEWARRAY instr = (MULTIANEWARRAY) instruction;
                values.pop(instr.getDimensions());
                values.push(new ValueInfo(new ArrayType(instr.getType(cpg), instr.getDimensions())));
                break;
            }
        case Constants.ARRAYLENGTH:
            values.pop();
            values.push(new ValueInfo(Type.INT));
            break;
        default:
            throw new AppInfoError("Instruction not supported: " + instruction);
    }
}
Also used : NEW(org.apache.bcel.generic.NEW) ConstantDoubleInfo(com.jopdesign.common.type.ConstantDoubleInfo) MULTIANEWARRAY(org.apache.bcel.generic.MULTIANEWARRAY) ConstantIntegerInfo(com.jopdesign.common.type.ConstantIntegerInfo) ConstantFloatInfo(com.jopdesign.common.type.ConstantFloatInfo) ConstantFieldInfo(com.jopdesign.common.type.ConstantFieldInfo) ConstantLongInfo(com.jopdesign.common.type.ConstantLongInfo) AppInfoError(com.jopdesign.common.misc.AppInfoError) ArrayType(org.apache.bcel.generic.ArrayType) CPInstruction(org.apache.bcel.generic.CPInstruction) InvokeInstruction(org.apache.bcel.generic.InvokeInstruction) LoadInstruction(org.apache.bcel.generic.LoadInstruction) Type(org.apache.bcel.generic.Type) ArrayType(org.apache.bcel.generic.ArrayType) ValueInfo(com.jopdesign.common.type.ValueInfo) IINC(org.apache.bcel.generic.IINC) FieldInstruction(org.apache.bcel.generic.FieldInstruction) ConstantPushInstruction(org.apache.bcel.generic.ConstantPushInstruction) StoreInstruction(org.apache.bcel.generic.StoreInstruction)

Example 2 with ArrayType

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

the class ReplaceAtomicAnnotation method transform.

public static Method transform(Method m, JavaClass clazz, ConstantPoolGen _cp) {
    MethodGen method = new MethodGen(m, clazz.getClassName(), _cp);
    InstructionList oldIl = method.getInstructionList();
    Type returnType = m.getReturnType();
    if (returnType.equals(Type.LONG) || returnType.equals(Type.DOUBLE)) {
        throw new UnsupportedOperationException();
    }
    final int transactionLocals = 2;
    /*
		 * local variable indices:
		 * isNotNestedTransaction is -2+2
		 * result is -2+3
		 * Throwable e is -2+3 
		 */
    final int maxLocals = method.getMaxLocals();
    final int transactionLocalsBaseIndex = maxLocals;
    final int copyBaseIndex = transactionLocalsBaseIndex + transactionLocals;
    SortedSet<Integer> modifiedArguments = getModifiedArguments(method);
    // maps modified arguments indices to copies
    Map<Integer, Integer> modifiedArgumentsCopies = new TreeMap<Integer, Integer>();
    {
        int copyIndex = copyBaseIndex;
        for (Integer i : modifiedArguments) {
            System.out.println("RTTM: method " + method.getClassName() + "." + method.getName() + method.getSignature() + ": saving argument " + i + " to variable " + copyIndex);
            modifiedArgumentsCopies.put(i, copyIndex++);
        }
    }
    InstructionList il = new InstructionList();
    InstructionFactory _factory = new InstructionFactory(_cp);
    method.setInstructionList(il);
    {
        //			InstructionHandle ih_0 = il.append(new PUSH(_cp, -559038737));
        //			il.append(_factory.createStore(Type.INT, transactionLocalsBaseIndex-2+1));
        InstructionHandle ih_3 = il.append(_factory.createFieldAccess("rttm.internal.Utils", "inTransaction", new ArrayType(Type.BOOLEAN, 1), Constants.GETSTATIC));
        il.append(new PUSH(_cp, -122));
        il.append(_factory.createInvoke("com.jopdesign.sys.Native", "rd", Type.INT, new Type[] { Type.INT }, Constants.INVOKESTATIC));
        il.append(InstructionConstants.BALOAD);
        BranchInstruction ifne_12 = InstructionFactory.createBranchInstruction(Constants.IFNE, null);
        il.append(ifne_12);
        il.append(new PUSH(_cp, 1));
        BranchInstruction goto_16 = InstructionFactory.createBranchInstruction(Constants.GOTO, null);
        il.append(goto_16);
        InstructionHandle ih_19 = il.append(new PUSH(_cp, 0));
        InstructionHandle ih_20 = il.append(InstructionFactory.createStore(Type.INT, transactionLocalsBaseIndex - 2 + 2));
        InstructionHandle ih_21 = il.append(InstructionFactory.createLoad(Type.INT, transactionLocalsBaseIndex - 2 + 2));
        BranchInstruction ifeq_22 = InstructionFactory.createBranchInstruction(Constants.IFEQ, null);
        il.append(ifeq_22);
        //			InstructionHandle ih_25 = il.append(_factory.createLoad(Type.INT, transactionLocalsBaseIndex-2+0));
        //			il.append(_factory.createStore(Type.INT, transactionLocalsBaseIndex-2+1));
        {
            // only save arguments which might be modified
            for (int i : modifiedArguments) {
                il.append(InstructionFactory.createLoad(Type.INT, i));
                il.append(InstructionFactory.createStore(Type.INT, modifiedArgumentsCopies.get(i)));
            }
        }
        InstructionHandle ih_27 = il.append(new PUSH(_cp, 0));
        il.append(new PUSH(_cp, -128));
        il.append(_factory.createInvoke("com.jopdesign.sys.Native", "wr", Type.VOID, new Type[] { Type.INT, Type.INT }, Constants.INVOKESTATIC));
        InstructionHandle ih_33 = il.append(_factory.createFieldAccess("rttm.internal.Utils", "inTransaction", new ArrayType(Type.BOOLEAN, 1), Constants.GETSTATIC));
        il.append(new PUSH(_cp, -122));
        il.append(_factory.createInvoke("com.jopdesign.sys.Native", "rd", Type.INT, new Type[] { Type.INT }, Constants.INVOKESTATIC));
        il.append(new PUSH(_cp, 1));
        il.append(InstructionConstants.BASTORE);
        // transaction loop
        InstructionHandle ih_43 = il.append(InstructionFactory.createLoad(Type.INT, transactionLocalsBaseIndex - 2 + 2));
        BranchInstruction ifeq_44 = InstructionFactory.createBranchInstruction(Constants.IFEQ, null);
        il.append(ifeq_44);
        InstructionHandle ih_47 = il.append(new PUSH(_cp, 1));
        il.append(new PUSH(_cp, Const.MEM_TM_MAGIC));
        il.append(_factory.createInvoke("com.jopdesign.sys.Native", "wrMem", Type.VOID, new Type[] { Type.INT, Type.INT }, Constants.INVOKESTATIC));
        //			InstructionHandle ih_53 = il.append(_factory.createLoad(Type.INT, transactionLocalsBaseIndex-2+0));
        //			il.append(_factory.createInvoke("rttm.swtest.Transaction", "atomicSection", Type.INT, new Type[] { Type.INT }, Constants.INVOKESTATIC));
        //			il.append(_factory.createStore(Type.INT, transactionLocalsBaseIndex-2+3));
        InstructionHandle ih_53 = oldIl.getStart();
        Collection<BranchInstruction> gotos_transactionCommit = new ArrayList<BranchInstruction>();
        {
            // redirect returns
            InstructionFinder f = new InstructionFinder(oldIl);
            String returnInstructionsPattern = "ARETURN|IRETURN|FRETURN|RETURN";
            for (Iterator i = f.search(returnInstructionsPattern); i.hasNext(); ) {
                InstructionHandle oldIh = ((InstructionHandle[]) i.next())[0];
                InstructionList nl = new InstructionList();
                if (!method.getReturnType().equals(Type.VOID)) {
                    nl.append(InstructionFactory.createStore(method.getReturnType(), transactionLocalsBaseIndex - 2 + 3));
                }
                BranchInstruction goto_transactionCommit = InstructionFactory.createBranchInstruction(Constants.GOTO, null);
                nl.append(goto_transactionCommit);
                gotos_transactionCommit.add(goto_transactionCommit);
                InstructionHandle newTarget = nl.getStart();
                oldIl.append(oldIh, nl);
                try {
                    oldIl.delete(oldIh);
                } catch (TargetLostException e) {
                    InstructionHandle[] targets = e.getTargets();
                    for (int k = 0; k < targets.length; k++) {
                        InstructionTargeter[] targeters = targets[k].getTargeters();
                        for (int j = 0; j < targeters.length; j++) {
                            targeters[j].updateTarget(targets[k], newTarget);
                        }
                    }
                }
            }
            il.append(oldIl);
        }
        InstructionHandle ih_58 = il.append(InstructionFactory.createLoad(Type.INT, transactionLocalsBaseIndex - 2 + 2));
        BranchInstruction ifeq_59 = InstructionFactory.createBranchInstruction(Constants.IFEQ, null);
        il.append(ifeq_59);
        InstructionHandle ih_62 = il.append(new PUSH(_cp, 0));
        il.append(new PUSH(_cp, Const.MEM_TM_MAGIC));
        il.append(_factory.createInvoke("com.jopdesign.sys.Native", "wrMem", Type.VOID, new Type[] { Type.INT, Type.INT }, Constants.INVOKESTATIC));
        InstructionHandle ih_68 = il.append(_factory.createFieldAccess("rttm.internal.Utils", "inTransaction", new ArrayType(Type.BOOLEAN, 1), Constants.GETSTATIC));
        il.append(new PUSH(_cp, -122));
        il.append(_factory.createInvoke("com.jopdesign.sys.Native", "rd", Type.INT, new Type[] { Type.INT }, Constants.INVOKESTATIC));
        il.append(new PUSH(_cp, 0));
        il.append(InstructionConstants.BASTORE);
        InstructionHandle ih_78 = il.append(new PUSH(_cp, 1));
        il.append(new PUSH(_cp, -128));
        il.append(_factory.createInvoke("com.jopdesign.sys.Native", "wr", Type.VOID, new Type[] { Type.INT, Type.INT }, Constants.INVOKESTATIC));
        //			InstructionHandle ih_84 = il.append(_factory.createLoad(Type.INT, transactionLocalsBaseIndex-2+3));
        //			il.append(_factory.createReturn(Type.INT));
        InstructionHandle ih_84;
        {
            // return
            if (!method.getReturnType().equals(Type.VOID)) {
                ih_84 = il.append(InstructionFactory.createLoad(method.getReturnType(), transactionLocalsBaseIndex - 2 + 3));
                il.append(InstructionFactory.createReturn(method.getReturnType()));
            } else {
                ih_84 = il.append(InstructionFactory.createReturn(method.getReturnType()));
            }
        }
        // catch block
        // variable e has index 3
        // } catch (Throwable e) {
        InstructionHandle nih_86 = il.append(InstructionFactory.createStore(Type.OBJECT, transactionLocalsBaseIndex - 2 + 3));
        // if (isNotNestedTransaction) {
        InstructionHandle nih_87 = il.append(InstructionFactory.createLoad(Type.INT, transactionLocalsBaseIndex - 2 + 2));
        BranchInstruction ifeq_88 = InstructionFactory.createBranchInstruction(Constants.IFEQ, null);
        il.append(ifeq_88);
        InstructionHandle nih_91 = il.append(InstructionFactory.createLoad(Type.OBJECT, transactionLocalsBaseIndex - 2 + 3));
        il.append(_factory.createFieldAccess("com.jopdesign.sys.RetryException", "instance", new ObjectType("com.jopdesign.sys.RetryException"), Constants.GETSTATIC));
        BranchInstruction if_acmpne_95 = InstructionFactory.createBranchInstruction(Constants.IF_ACMPNE, null);
        il.append(if_acmpne_95);
        // InstructionHandle nih_98 = il.append(_factory.createLoad(Type.INT,
        // 1));
        // il.append(_factory.createStore(Type.INT, 0));
        {
            for (int i : modifiedArguments) {
                il.append(InstructionFactory.createLoad(Type.INT, modifiedArgumentsCopies.get(i)));
                il.append(InstructionFactory.createStore(Type.INT, i));
            }
        }
        BranchInstruction goto_110 = InstructionFactory.createBranchInstruction(Constants.GOTO, ih_43);
        InstructionHandle ih_110 = il.append(goto_110);
        // exception was manually aborted or a bug triggered
        InstructionHandle nih_103 = il.append(_factory.createFieldAccess("rttm.internal.Utils", "inTransaction", new ArrayType(Type.BOOLEAN, 1), Constants.GETSTATIC));
        il.append(new PUSH(_cp, -122));
        il.append(_factory.createInvoke("com.jopdesign.sys.Native", "rd", Type.INT, new Type[] { Type.INT }, Constants.INVOKESTATIC));
        il.append(new PUSH(_cp, 0));
        il.append(InstructionConstants.BASTORE);
        InstructionHandle nih_113 = il.append(new PUSH(_cp, 1));
        il.append(new PUSH(_cp, -128));
        il.append(_factory.createInvoke("com.jopdesign.sys.Native", "wrMem", Type.VOID, new Type[] { Type.INT, Type.INT }, Constants.INVOKESTATIC));
        InstructionHandle nih_119 = il.append(InstructionFactory.createLoad(Type.OBJECT, transactionLocalsBaseIndex - 2 + 3));
        // il.append(_factory.createCheckCast(new
        // ObjectType("java.lang.RuntimeException")));
        il.append(InstructionConstants.ATHROW);
        // set branch targets			
        ifne_12.setTarget(ih_19);
        goto_16.setTarget(ih_20);
        ifeq_22.setTarget(ih_43);
        ifeq_44.setTarget(ih_53);
        ifeq_59.setTarget(ih_84);
        ifeq_88.setTarget(nih_119);
        if_acmpne_95.setTarget(nih_103);
        {
            for (BranchInstruction b : gotos_transactionCommit) {
                b.setTarget(ih_58);
            }
        }
        // set exception handlers 
        // TODO restrict exception handler
        method.addExceptionHandler(ih_53, ih_84, nih_86, null);
        method.setMaxStack();
        method.setMaxLocals();
    }
    m = method.getMethod();
    oldIl.dispose();
    il.dispose();
    return m;
}
Also used : BranchInstruction(org.apache.bcel.generic.BranchInstruction) InstructionList(org.apache.bcel.generic.InstructionList) InstructionFinder(org.apache.bcel.util.InstructionFinder) TreeMap(java.util.TreeMap) MethodGen(org.apache.bcel.generic.MethodGen) InstructionHandle(org.apache.bcel.generic.InstructionHandle) InstructionFactory(org.apache.bcel.generic.InstructionFactory) ArrayType(org.apache.bcel.generic.ArrayType) ObjectType(org.apache.bcel.generic.ObjectType) Type(org.apache.bcel.generic.Type) ObjectType(org.apache.bcel.generic.ObjectType) ArrayType(org.apache.bcel.generic.ArrayType) Iterator(java.util.Iterator) Collection(java.util.Collection) TargetLostException(org.apache.bcel.generic.TargetLostException) PUSH(org.apache.bcel.generic.PUSH)

Example 3 with ArrayType

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

the class ArrayForToEnhancedFor method visitForIntermediate.

@Override
public void visitForIntermediate(ForIntermediate line) {
    //we need to look at the previous 2 statements and the first statement child.
    AbstractIntermediate arrayLenthCandidate = getForExteriorPredecessor(line);
    //check that the array length candidate's declared length variable is used in the for's condition.
    AbstractIntermediate tempArrayCandidate = null;
    AbstractIntermediate firstChild = igc.getTrueTarget(line);
    if (arrayLenthCandidate != null) {
        tempArrayCandidate = getSinglePredecessor(arrayLenthCandidate);
    }
    //if either of these are null, then this doesn't match.
    if (arrayLenthCandidate == null || tempArrayCandidate == null) {
        return;
    }
    GeneratedVariable generatedArrayLength = extractGeneratedVariableDeclaration(arrayLenthCandidate);
    GeneratedVariable generatedArrayReference = extractGeneratedVariableDeclaration(tempArrayCandidate);
    GeneratedVariable arrayIteratorValue = extractGeneratedVariableDeclaration(line.getInit());
    if (generatedArrayLength == null || generatedArrayReference == null || arrayIteratorValue == null) {
        return;
    }
    if (generatedArrayLength.getType() != Type.INT) {
        if (!(generatedArrayReference.getType() instanceof ArrayType)) {
            return;
        }
        if (arrayIteratorValue.getType() != Type.INT) {
            return;
        }
    }
    //great; at this point we know the pattern matches.  check the next statement to see if the transformation is possible.
    //format should be: 40 : GENERATED_ARRAY_REFERENCE_TYPE i = GENERATED_ARRAY_REFERENCE[ARRAY_ITERATOR_VALUE] | 
    StatementIntermediate childDeclarationStatement = ((StatementIntermediate) firstChild);
    Declaration childDeclaration = (Declaration) childDeclarationStatement.getExpression();
    if (firstMatchesGeneratedVariables(childDeclarationStatement, generatedArrayReference, arrayIteratorValue)) {
        LOG.debug("Likely a enhanced for loop for array: " + generatedArrayLength + " , " + generatedArrayReference);
        //we are good to go here.  Now we just need to reorganize the graph.  Start by creating the new enhanced for loop.
        EnhancedForIntermediate efl = new EnhancedForIntermediate(line.getInstruction(), line.getConditionalIntermediate(), childDeclaration.getVariable(), extractExpressionFromGeneratedArrayAssignment(tempArrayCandidate));
        //efl.setTrueBranch(line.getTrueBranch());
        //efl.setFalseBranch(line.getFalseBranch());
        //add the new node...
        this.igc.getGraph().addVertex(efl);
        //now, we just need to redirect.
        igc.redirectPredecessors(tempArrayCandidate, efl);
        igc.redirectPredecessors(line, efl);
        igc.redirectSuccessors(line, efl);
        AbstractIntermediate nextChild = getSingleSuccessor(firstChild);
        igc.redirectPredecessors(firstChild, nextChild);
        //remove line.
        igc.getGraph().removeVertex(line);
        igc.getGraph().removeVertex(tempArrayCandidate);
        igc.getGraph().removeVertex(firstChild);
        igc.getGraph().removeVertex(arrayLenthCandidate);
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) ArrayType(org.apache.bcel.generic.ArrayType) GeneratedVariable(org.candle.decompiler.intermediate.expression.GeneratedVariable) EnhancedForIntermediate(org.candle.decompiler.intermediate.code.loop.EnhancedForIntermediate) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate) Declaration(org.candle.decompiler.intermediate.expression.Declaration)

Example 4 with ArrayType

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

the class MethodCode method getReferencedClassName.

/**
     * Get the classname or array-type name referenced by an invoke- or field instruction in this code.
     *
     * @param instr the instruction to check, using this methods constantpool.
     * @return the referenced classname or array-typename, to be used for a ClassRef or AppInfo getter. 
     */
public String getReferencedClassName(FieldOrMethod instr) {
    ConstantPoolGen cpg = getConstantPoolGen();
    ReferenceType refType = instr.getReferenceType(cpg);
    String classname;
    if (refType instanceof ObjectType) {
        classname = ((ObjectType) refType).getClassName();
    } else if (refType instanceof ArrayType) {
        // need to call array.<method>, which class should we use? Let's decide later..
        String msg = "Calling a method of an array: " + refType.getSignature() + "#" + instr.getName(cpg) + " in " + methodInfo;
        logger.debug(msg);
        classname = refType.getSignature();
    } else {
        // Hu??
        throw new JavaClassFormatError("Unknown reference type " + refType);
    }
    return classname;
}
Also used : ArrayType(org.apache.bcel.generic.ArrayType) ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) ObjectType(org.apache.bcel.generic.ObjectType) JavaClassFormatError(com.jopdesign.common.misc.JavaClassFormatError) HashedString(com.jopdesign.common.misc.HashedString) CallString(com.jopdesign.common.code.CallString) ReferenceType(org.apache.bcel.generic.ReferenceType)

Aggregations

ArrayType (org.apache.bcel.generic.ArrayType)4 ObjectType (org.apache.bcel.generic.ObjectType)2 Type (org.apache.bcel.generic.Type)2 CallString (com.jopdesign.common.code.CallString)1 AppInfoError (com.jopdesign.common.misc.AppInfoError)1 HashedString (com.jopdesign.common.misc.HashedString)1 JavaClassFormatError (com.jopdesign.common.misc.JavaClassFormatError)1 ConstantDoubleInfo (com.jopdesign.common.type.ConstantDoubleInfo)1 ConstantFieldInfo (com.jopdesign.common.type.ConstantFieldInfo)1 ConstantFloatInfo (com.jopdesign.common.type.ConstantFloatInfo)1 ConstantIntegerInfo (com.jopdesign.common.type.ConstantIntegerInfo)1 ConstantLongInfo (com.jopdesign.common.type.ConstantLongInfo)1 ValueInfo (com.jopdesign.common.type.ValueInfo)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 TreeMap (java.util.TreeMap)1 BranchInstruction (org.apache.bcel.generic.BranchInstruction)1 CPInstruction (org.apache.bcel.generic.CPInstruction)1 ConstantPoolGen (org.apache.bcel.generic.ConstantPoolGen)1 ConstantPushInstruction (org.apache.bcel.generic.ConstantPushInstruction)1