Example usage for org.objectweb.asm Opcodes ISTORE

List of usage examples for org.objectweb.asm Opcodes ISTORE

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ISTORE.

Prototype

int ISTORE

To view the source code for org.objectweb.asm Opcodes ISTORE.

Click Source Link

Usage

From source file:org.evosuite.instrumentation.testability.transformer.ImplicitElseTransformer.java

License:Open Source License

private void handleDependency(ControlDependency dependency, ControlDependenceGraph cdg, MethodNode mn,
        VarInsnNode varNode, BytecodeInstruction parentLevel) {

    if (addedNodes.contains(dependency))
        return;//from  w ww .  j  a va2 s. c  o  m

    // Get the basic blocks reachable if the dependency would evaluate different
    Set<BasicBlock> blocks = cdg.getAlternativeBlocks(dependency);
    addedNodes.add(dependency);

    Set<ControlDependency> dependencies = dependency.getBranch().getInstruction().getControlDependencies();
    //if (dependencies.size() == 1) {
    //   ControlDependency dep = dependencies.iterator().next();
    for (ControlDependency dep : dependencies) {
        if (!addedNodes.contains(dep) && dep != dependency)
            handleDependency(dep, cdg, mn, varNode, dependency.getBranch().getInstruction());
    }

    // TODO: Need to check that there is an assignment in every alternative path through CDG

    boolean hasAssignment = false;
    for (BasicBlock block : blocks) {
        // If this block also assigns a value to the same variable
        for (BytecodeInstruction instruction : block) {
            if (instruction.getASMNode().getOpcode() == Opcodes.ISTORE) {
                VarInsnNode otherVarNode = (VarInsnNode) instruction.getASMNode();
                VarInsnNode thisVarNode = varNode;
                if (otherVarNode.var == thisVarNode.var) {
                    hasAssignment = true;
                    break;
                }
            }
        }
        if (hasAssignment) {
            break;
        }
    }

    // The Flag assignment is is the dependency evaluates to the given value
    // We thus need to insert the tautoligical assignment either directly after the IF (if the value is true)
    // or before the jump target (if the value is false)

    if (!hasAssignment) {
        TransformationStatistics.transformedImplicitElse();
        if (dependency.getBranch().getInstruction().isSwitch()) {
            BooleanTestabilityTransformation.logger.warn("Don't know how to handle Switches yet");
            return;
        }
        JumpInsnNode jumpNode = (JumpInsnNode) dependency.getBranch().getInstruction().getASMNode();
        VarInsnNode newStore = new VarInsnNode(Opcodes.ISTORE, varNode.var);
        VarInsnNode newLoad = new VarInsnNode(Opcodes.ILOAD, varNode.var);
        if (dependency.getBranchExpressionValue()) {
            BooleanTestabilityTransformation.logger.info("Inserting else branch directly after if");
            // Insert directly after if
            if (isDefinedBefore(mn, varNode, jumpNode)) {
                mn.instructions.insert(jumpNode, newStore);
                mn.instructions.insert(jumpNode, newLoad);
                registerInstruction(mn, varNode, newStore);
                registerInstruction(mn, varNode, newLoad);
            }

        } else {
            BooleanTestabilityTransformation.logger.info("Inserting else branch as jump target");
            // Insert as jump target
            if (isDefinedBefore(mn, varNode, jumpNode)) {

                LabelNode target = jumpNode.label;
                LabelNode newTarget = new LabelNode(new Label());

                // jumpNode or target?
                registerInstruction(mn, jumpNode.getNext(), newStore);
                registerInstruction(mn, jumpNode.getNext(), newLoad);

                InsnList assignment = new InsnList();
                assignment.add(new JumpInsnNode(Opcodes.GOTO, target));
                assignment.add(newTarget);
                assignment.add(newLoad);
                assignment.add(newStore);
                jumpNode.label = newTarget;

                mn.instructions.insertBefore(target, assignment);
            }
        }
    }

}

From source file:org.evosuite.instrumentation.testability.transformer.ImplicitElseTransformer.java

License:Open Source License

@Override
protected AbstractInsnNode transformVarInsnNode(MethodNode mn, VarInsnNode varNode) {
    if (varNode.getOpcode() == Opcodes.ISTORE
            && this.booleanTestabilityTransformation.isBooleanVariable(varNode.var, mn)) {

        // Check if ICONST_0 or ICONST_1 are on the stack
        ControlDependenceGraph cdg = GraphPool.getInstance(this.booleanTestabilityTransformation.classLoader)
                .getCDG(this.booleanTestabilityTransformation.className.replace("/", "."), mn.name + mn.desc);
        int index = mn.instructions.indexOf(varNode);
        BytecodeInstruction insn = BytecodeInstructionPool
                .getInstance(this.booleanTestabilityTransformation.classLoader)
                .getInstruction(this.booleanTestabilityTransformation.className.replace("/", "."),
                        mn.name + mn.desc, index);
        //varNode);
        if (insn == null) {
            // TODO: Debug this on org.exolab.jms.net.uri.URI
            BooleanTestabilityTransformation.logger.info("WARNING: Instruction not found!");
            return varNode;
        }/*w  w w  . j a  v  a2  s  . c  o  m*/
        if (insn.getASMNode().getOpcode() != varNode.getOpcode()) {
            BooleanTestabilityTransformation.logger.info("Found wrong bytecode instruction at this index!");
            insn = BytecodeInstructionPool.getInstance(this.booleanTestabilityTransformation.classLoader)
                    .getInstruction(this.booleanTestabilityTransformation.className, mn.name + mn.desc,
                            varNode);
            if (insn == null) {
                // TODO: Debug this on org.exolab.jms.net.uri.URI
                BooleanTestabilityTransformation.logger.info("WARNING: Instruction not found!");
                return varNode;
            }
        }
        Set<ControlDependency> dependencies = insn.getControlDependencies();
        BooleanTestabilityTransformation.logger.info("Found flag assignment: " + insn + ", checking "
                + dependencies.size() + " control dependencies");

        for (ControlDependency dep : dependencies) {
            if (!addedNodes.contains(dep))
                handleDependency(dep, cdg, mn, varNode, insn);
        }

        // Only do completion if there's only one dependency
        // Not sure how other cases would look like
        /*
             //if (dependencies.size() > 1)
             //   return varNode;
             //else
             if (dependencies.isEmpty())
                return varNode;
                
             ControlDependency dep = dependencies.iterator().next();
             if (!addedNodes.contains(dep))
                handleDependency(dep, cdg, mn, varNode, insn);
                */

    }
    return varNode;
}

From source file:org.evosuite.testcarver.instrument.Instrumenter.java

License:Open Source License

/**
 *    public int myMethod(int i)/*from   ww w  .ja  v  a  2  s . c om*/
   {
 try
 {
    return _sw_prototype_original_myMethod(i)
 }
 finally
 {
    Capturer.enable();
 }
   }
        
 * @param classNode
 * @param className
 * @param methodNode
 */
@SuppressWarnings("unchecked")
private MethodNode wrapMethod(final ClassNode classNode, final String className, final MethodNode methodNode) {
    methodNode.maxStack += 4;

    // create wrapper for original method
    final MethodNode wrappingMethodNode = new MethodNode(methodNode.access, methodNode.name, methodNode.desc,
            methodNode.signature,
            (String[]) methodNode.exceptions.toArray(new String[methodNode.exceptions.size()]));
    wrappingMethodNode.maxStack = methodNode.maxStack;

    // assign annotations to wrapping method
    wrappingMethodNode.visibleAnnotations = methodNode.visibleAnnotations;
    wrappingMethodNode.visibleParameterAnnotations = methodNode.visibleParameterAnnotations;

    // remove annotations from wrapped method to avoid wrong behavior controlled by annotations
    methodNode.visibleAnnotations = null;
    methodNode.visibleParameterAnnotations = null;

    // rename original method
    methodNode.access = TransformerUtil.modifyVisibility(methodNode.access, Opcodes.ACC_PRIVATE);

    final LabelNode l0 = new LabelNode();
    final LabelNode l1 = new LabelNode();
    final LabelNode l2 = new LabelNode();

    final InsnList wInstructions = wrappingMethodNode.instructions;

    if ("<init>".equals(methodNode.name)) {
        // wrap a constructor 

        methodNode.name = WRAP_NAME_PREFIX + "init" + WRAP_NAME_PREFIX;

        // move call to other constructors to new method
        AbstractInsnNode ins = null;
        ListIterator<AbstractInsnNode> iter = methodNode.instructions.iterator();

        int numInvokeSpecials = 0; // number of invokespecial calls before actual constructor call

        while (iter.hasNext()) {
            ins = iter.next();
            iter.remove();
            wInstructions.add(ins);

            if (ins instanceof MethodInsnNode) {
                MethodInsnNode mins = (MethodInsnNode) ins;
                if (ins.getOpcode() == Opcodes.INVOKESPECIAL) {
                    if (mins.name.startsWith("<init>")) {
                        if (numInvokeSpecials == 0) {
                            break;
                        } else {
                            numInvokeSpecials--;
                        }
                    }
                }
            } else if (ins instanceof TypeInsnNode) {
                TypeInsnNode typeIns = (TypeInsnNode) ins;
                if (typeIns.getOpcode() == Opcodes.NEW || typeIns.getOpcode() == Opcodes.NEWARRAY) {
                    numInvokeSpecials++;
                }
            }
        }
    } else {
        methodNode.name = WRAP_NAME_PREFIX + methodNode.name;
    }

    int varReturnValue = 0;

    final Type returnType = Type.getReturnType(methodNode.desc);

    if (returnType.equals(Type.VOID_TYPE)) {
        wrappingMethodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l1, "java/lang/Throwable"));

    } else {

        wrappingMethodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l2, "java/lang/Throwable"));

        //--- create "Object returnValue = null;"

        if (!TransformerUtil.isStatic(methodNode.access)) {
            // load "this"
            varReturnValue++;
        }

        // consider method arguments to find right variable index
        final Type[] argTypes = Type.getArgumentTypes(methodNode.desc);
        for (int i = 0; i < argTypes.length; i++) {
            varReturnValue++;

            // long/double take two registers
            if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) {
                varReturnValue++;
            }
        }

        // push NULL on the stack and initialize variable for return value for it
        wInstructions.add(new InsnNode(Opcodes.ACONST_NULL));
        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, varReturnValue));
    }

    int var = 0;

    // --- L0
    wInstructions.add(l0);

    wInstructions.add(this.addCaptureCall(TransformerUtil.isStatic(methodNode.access), className,
            wrappingMethodNode.name, wrappingMethodNode.desc, Type.getArgumentTypes(methodNode.desc)));

    // --- construct call to wrapped methode

    if (!TransformerUtil.isStatic(methodNode.access)) {
        // load "this" to call method
        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
        var++;
    }

    final Type[] argTypes = Type.getArgumentTypes(methodNode.desc);
    for (int i = 0; i < argTypes.length; i++) {
        this.addLoadInsn(wInstructions, argTypes[i], var++);

        // long/double take two registers
        if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) {
            var++;
        }
    }

    if (TransformerUtil.isStatic(methodNode.access)) {
        wInstructions.add(
                new MethodInsnNode(Opcodes.INVOKESTATIC, classNode.name, methodNode.name, methodNode.desc));
    } else {
        wInstructions.add(
                new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classNode.name, methodNode.name, methodNode.desc));
    }

    var++;

    if (returnType.equals(Type.VOID_TYPE)) {
        wInstructions.add(new JumpInsnNode(Opcodes.GOTO, l2));

        // --- L1

        wInstructions.add(l1);

        wInstructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }));

        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, --var));

        this.addCaptureEnableStatement(className, methodNode, wInstructions, -1);

        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, var));
        wInstructions.add(new InsnNode(Opcodes.ATHROW));

        // FIXME <--- DUPLICATE CODE

        // --- L2

        wInstructions.add(l2);
        wInstructions.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null));

        this.addCaptureEnableStatement(className, methodNode, wInstructions, -1);

        wInstructions.add(new InsnNode(Opcodes.RETURN));
    } else {
        // construct store of the wrapped method call's result

        this.addBoxingStmt(wInstructions, returnType);

        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, varReturnValue));
        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, varReturnValue));

        this.addUnBoxingStmt(wInstructions, returnType);

        final int storeOpcode = returnType.getOpcode(Opcodes.ISTORE);
        wInstructions.add(new VarInsnNode(storeOpcode, ++var)); // might be only var

        // --- L1

        wInstructions.add(l1);

        this.addCaptureEnableStatement(className, methodNode, wInstructions, varReturnValue);

        // construct load of the wrapped method call's result
        int loadOpcode = returnType.getOpcode(Opcodes.ILOAD);
        wInstructions.add(new VarInsnNode(loadOpcode, var));

        // construct return of the wrapped method call's result
        this.addReturnInsn(wInstructions, returnType);

        //---- L2

        wInstructions.add(l2);

        wInstructions.add(
                new FrameNode(Opcodes.F_FULL, 2, new Object[] { className, this.getInternalName(returnType) },
                        1, new Object[] { "java/lang/Throwable" }));
        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, --var));

        this.addCaptureEnableStatement(className, methodNode, wInstructions, varReturnValue);

        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, var));
        wInstructions.add(new InsnNode(Opcodes.ATHROW));
    }
    transformWrapperCalls(methodNode);
    return wrappingMethodNode;
}

From source file:org.formulacompiler.compiler.internal.bytecode.ExpressionCompiler.java

License:Open Source License

private void compileScanArray(ForEachElementCompilation _forElement) throws CompilerException {
    final GeneratorAdapter mv = mv();
    final int loc = localsOffset();
    incLocalsOffset(4);//  w  w  w.  j  a va 2  s. c  o  m

    // store array
    mv.visitVarInsn(Opcodes.ASTORE, loc);

    // store array length
    mv.visitVarInsn(Opcodes.ALOAD, loc);
    mv.arrayLength();
    mv.visitVarInsn(Opcodes.ISTORE, 1 + loc);

    // loop index
    mv.push(0);
    mv.visitVarInsn(Opcodes.ISTORE, 2 + loc);

    // loop start
    final Label l0 = mv.mark();

    // check loop condition
    mv.visitVarInsn(Opcodes.ILOAD, 2 + loc);
    mv.visitVarInsn(Opcodes.ILOAD, 1 + loc);
    final Label l1 = new Label();
    mv.ifICmp(GeneratorAdapter.GE, l1);

    // loop body
    mv.visitVarInsn(Opcodes.ALOAD, loc);
    mv.visitVarInsn(Opcodes.ILOAD, 2 + loc);
    mv.visitInsn(Opcodes.AALOAD);
    mv.visitVarInsn(Opcodes.ASTORE, 3 + loc);
    _forElement.compile(3 + loc, 2 + loc);

    // inc loop index
    mv.visitIincInsn(2 + loc, 1);

    mv.goTo(l0);
    mv.visitLabel(l1);
}

From source file:org.formulacompiler.compiler.internal.bytecode.HelperCompilerForFoldApply.java

License:Open Source License

private void compileAccumulatorStore(int _iAccu) {
    mv().visitVarInsn(accuTypes[_iAccu].getOpcode(Opcodes.ISTORE), accuVars[_iAccu]);
}

From source file:org.formulacompiler.compiler.internal.bytecode.HelperCompilerForFoldApply.java

License:Open Source License

private void compileIndexSetup() {
    staticCount = fold.getPartiallyFoldedElementCount();
    if (mustDetectEmptiness || mustCount || fold.isIndexed()) {
        this.indexVar = newLocal(Type.INT_TYPE.getSize());
        mv().push(fold.getPartiallyFoldedElementCount());
        mv().visitVarInsn(Opcodes.ISTORE, indexVar);
        letIndexVarAs(fold.indexName());
    }/* w  w w  .  j  a  v  a2s.c om*/
}

From source file:org.formulacompiler.compiler.internal.bytecode.HelperCompilerForFoldChained.java

License:Open Source License

private void compileAccumulatorStore() {
    mv().visitVarInsn(accuType.getOpcode(Opcodes.ISTORE), accuVar);
}

From source file:org.glassfish.pfl.tf.spi.Util.java

License:Open Source License

public void initLocal(MethodVisitor mv, LocalVariableNode var) {
    info(2, "Initializing variable " + var);
    Type type = Type.getType(var.desc);
    switch (type.getSort()) {
    case Type.BYTE:
    case Type.BOOLEAN:
    case Type.CHAR:
    case Type.SHORT:
    case Type.INT:
        mv.visitInsn(Opcodes.ICONST_0);//from   w  w w .j  a v a 2 s.co  m
        mv.visitVarInsn(Opcodes.ISTORE, var.index);
        break;

    case Type.LONG:
        mv.visitInsn(Opcodes.LCONST_0);
        mv.visitVarInsn(Opcodes.LSTORE, var.index);
        break;

    case Type.FLOAT:
        mv.visitInsn(Opcodes.FCONST_0);
        mv.visitVarInsn(Opcodes.FSTORE, var.index);
        break;

    case Type.DOUBLE:
        mv.visitInsn(Opcodes.DCONST_0);
        mv.visitVarInsn(Opcodes.DSTORE, var.index);
        break;

    default:
        mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitVarInsn(Opcodes.ASTORE, var.index);
    }
}

From source file:org.glassfish.pfl.tf.spi.Util.java

License:Open Source License

public void storeFromXReturn(MethodVisitor mv, int returnOpcode, LocalVariableNode holder) {

    switch (returnOpcode) {
    case Opcodes.RETURN:
        // NOP/*  www.j  a v  a 2 s  .c o m*/
        break;
    case Opcodes.ARETURN:
        mv.visitVarInsn(Opcodes.ASTORE, holder.index);
        break;
    case Opcodes.IRETURN:
        mv.visitVarInsn(Opcodes.ISTORE, holder.index);
        break;
    case Opcodes.LRETURN:
        mv.visitVarInsn(Opcodes.LSTORE, holder.index);
        break;
    case Opcodes.FRETURN:
        mv.visitVarInsn(Opcodes.FSTORE, holder.index);
        break;
    case Opcodes.DRETURN:
        mv.visitVarInsn(Opcodes.DSTORE, holder.index);
        break;
    }
}

From source file:org.hua.ast.visitors.BytecodeGeneratorASTVisitor.java

@Override
public void visit(BinaryExpression node) throws ASTVisitorException {
    node.getExpression1().accept(this);
    Type expr1Type = ASTUtils.getSafeType(node.getExpression1());

    node.getExpression2().accept(this);
    Type expr2Type = ASTUtils.getSafeType(node.getExpression2());

    Type maxType = TypeUtils.maxType(expr1Type, expr2Type);

    // cast top of stack to max
    if (!maxType.equals(expr2Type)) {
        widen(maxType, expr2Type);
    }/*w w w  .  ja  v  a 2 s  .c  o m*/
    System.out.println("^^^^^^^^^^^^^^^^^^^ binary op: " + node.getOperator());
    System.out.println("             type1: " + expr1Type + " type2: " + expr2Type);
    System.out.println("                max type: " + !maxType.equals(expr1Type));
    // cast second from top to max
    if (!maxType.equals(expr1Type)) {
        System.out.println("not to be dispalyed");
        LocalIndexPool lip = ASTUtils.getSafeLocalIndexPool(node);
        int localIndex = -1;
        if (expr2Type.equals(Type.DOUBLE_TYPE) || expr1Type.equals(Type.DOUBLE_TYPE)) {
            localIndex = lip.getLocalIndex(expr2Type);
            mn.instructions.add(new VarInsnNode(expr2Type.getOpcode(Opcodes.ISTORE), localIndex));
        } else {
            mn.instructions.add(new InsnNode(Opcodes.SWAP));
        }
        widen(maxType, expr1Type);
        if (expr2Type.equals(Type.DOUBLE_TYPE) || expr1Type.equals(Type.DOUBLE_TYPE)) {
            mn.instructions.add(new VarInsnNode(expr2Type.getOpcode(Opcodes.ILOAD), localIndex));
            lip.freeLocalIndex(localIndex, expr2Type);
        } else {
            mn.instructions.add(new InsnNode(Opcodes.SWAP));
        }
    }

    // 
    if (ASTUtils.isBooleanExpression(node)) {
        handleBooleanOperator(node, node.getOperator(), maxType);
    } else if (maxType.equals(TypeUtils.STRING_TYPE)) {
        mn.instructions.add(new InsnNode(Opcodes.SWAP));
        handleStringOperator(node, node.getOperator());
    } else {
        handleNumberOperator(node, node.getOperator(), maxType);
    }
}