Example usage for org.aspectj.apache.bcel.generic Instruction toString

List of usage examples for org.aspectj.apache.bcel.generic Instruction toString

Introduction

In this page you can find the example usage for org.aspectj.apache.bcel.generic Instruction toString.

Prototype

@Override
    public String toString() 

Source Link

Usage

From source file:br.jabuti.graph.datastructure.ig.InstructionNode.java

License:Open Source License

/** <p>
 This is the method that calculates the efect of this instruction in a given
 configuration of the stack and local variables. It also inserts the uses and
 definitions in the sets use/def of this instruction </p>
        //from   w  w  w . j  ava  2 s. c o  m
 @param st The initial stack on which the instruction will be applied; it 
 may be changed by the instruction and hold the result of executiong it;
 @param st The initial local variables on which the instruction will be applied; it 
 may be changed by the instruction and hold the result of executing it;
 @param cp The constant pool of the class
        
 @throws InvalidInstructionExceptio If this object does not correspond to
 a valid JVM instruction. Should not occour.
 @param InvalidStackArgument If the stack or local variables have in a given
 spot a type that is not compatible with the instruction.
        
 */
public void execInstruction(VMStack st, VMLocal locals, ConstantPoolGen cp)
        throws InvalidInstructionException, InvalidStackArgument {
    //        int delta = ih.getInstruction().produceStack(cp)
    //                - ih.getInstruction().consumeStack(cp);
    Instruction ins = ih.getInstruction();

    if (ins instanceof ACONST_NULL) {
        st.push(new VMStackElement(Type.NULL, DONT_CARE, this));
    } else if (ins instanceof DNEG || ins instanceof FNEG || ins instanceof INEG || ins instanceof LNEG) {
        setStackArgs(st, 1);
        Type t1 = ((ArithmeticInstruction) ins).getType(cp);

        st.pop(1);
        VMStackElement vme = new VMStackElement(t1, DONT_CARE, this);

        st.push(vme);
    } else if (ins instanceof ArithmeticInstruction) {
        setStackArgs(st, 2);
        Type t1 = ((ArithmeticInstruction) ins).getType(cp);

        st.pop(2);
        VMStackElement vme = new VMStackElement(t1, DONT_CARE, this);

        st.push(vme);
    } else if (ins instanceof ArrayInstruction) {
        //           Type t = ((ArrayInstruction) ins).getType(cp);
        int k = (ins instanceof StackProducer) ? 0 : 1;
        // t1 = tipo do indice (inteiro)
        //            Type t1 = st.top(k).type;

        // t2  o tipo do array por exemplo int[]
        Type t3, t2 = st.top(k + 1).type;

        // t3  o tipo do elemento a ser armazenado p.e. int se t2 = int[]
        if (t2 instanceof ArrayType) {
            t3 = ((ArrayType) t2).getElementType();
        } else {
            throw new InvalidStackArgument("Instruction " + ins + " requires an array reference");
        }

        // adiciona [] na indicacao da origem do array.
        // por exemplo L@1[]
        String defUse = st.top(k + 1).defuse + "[]";

        if (k == 0) // is a LOAD instruction
        {
            setStackArgs(st, 2);
            VMStackElement vme = new VMStackElement(t3, defUse, this);

            st.pop(2);
            st.push(vme);
            useArrayAdd(defUse);
        } else // is a STORE
        {
            setStackArgs(st, 2);
            st.pop(3);
            defArrayAdd(defUse);
        }
    } else if (ins instanceof ARRAYLENGTH || ins instanceof INSTANCEOF) {
        setStackArgs(st, 1);
        VMStackElement vme = new VMStackElement(Type.INT, DONT_CARE, this);

        st.pop();
        st.push(vme);
    } else if (ins instanceof ATHROW) {
        setStackArgs(st, 1);
        VMStackElement vme = st.top();

        vme.producer = this;
        st.reset();
        st.push(vme);
    } else if (ins instanceof ConstantPushInstruction) {
        ConstantPushInstruction iins = (ConstantPushInstruction) ins;
        VMStackElement vme = new VMStackElement(iins.getType(cp), DONT_CARE, this);

        st.push(vme);
    } else if (ins instanceof JsrInstruction) {
        JsrInstruction iins = (JsrInstruction) ins;
        //            InstructionHandle jr = iins.physicalSuccessor();
        VMStackElement vme = new VMStackElement(iins.getType(cp), DONT_CARE, this);

        st.push(vme);
    } else if (ins instanceof BranchInstruction) {
        setStackArgs2(st, ins.consumeStack(cp));
        st.pop2(ins.consumeStack(cp));
    } else if (ins instanceof PUTFIELD) {
        setStackArgs(st, 2);
        st.pop();
        String defUse = st.top().defuse;

        st.pop();
        defFieldAdd(defUse + "." + ((PUTFIELD) ins).getFieldName(cp));
    } else if (ins instanceof PUTSTATIC) {
        setStackArgs(st, 1);
        st.pop(1);
        defFieldAdd(STR_STATIC + ((PUTSTATIC) ins).getClassName(cp) + "." + ((PUTSTATIC) ins).getFieldName(cp));
    } else if (ins instanceof ConversionInstruction) {
        setStackArgs(st, 1);
        ConversionInstruction iins = (ConversionInstruction) ins;
        VMStackElement vme = new VMStackElement(iins.getType(cp), DONT_CARE, this);

        st.pop();
        st.push(vme);
    } else if (ins instanceof NEW) {
        TypedInstruction iins = (TypedInstruction) ins;
        VMStackElement vme = new VMStackElement(iins.getType(cp), DONT_CARE, this);

        st.push(vme);
    } else if (ins instanceof MULTIANEWARRAY) {
        setStackArgs2(st, ins.consumeStack(cp));
        MULTIANEWARRAY iins = (MULTIANEWARRAY) ins;
        VMStackElement vme = new VMStackElement(iins.getType(cp), DONT_CARE, this);

        st.pop2(ins.consumeStack(cp));
        st.push(vme);
    } else if (ins instanceof ANEWARRAY) {
        ANEWARRAY iins = (ANEWARRAY) ins;
        VMStackElement vme = new VMStackElement(new ArrayType(iins.getType(cp), 1), DONT_CARE, this);

        st.pop();
        st.push(vme);
    } else if (ins instanceof NEWARRAY) {
        setStackArgs(st, 1);
        NEWARRAY iins = (NEWARRAY) ins;
        VMStackElement vme = new VMStackElement(iins.getType(), DONT_CARE, this);

        st.pop();
        st.push(vme);
    } else if (ins instanceof LDC || ins instanceof LDC2_W) {
        TypedInstruction iins = (TypedInstruction) ins;
        VMStackElement vme = new VMStackElement(iins.getType(cp), DONT_CARE, this);

        st.push(vme);
    } else if (ins instanceof DCMPG || ins instanceof DCMPL || ins instanceof FCMPG || ins instanceof FCMPL
            || ins instanceof LCMP) {
        setStackArgs(st, 2);
        VMStackElement vme = new VMStackElement(Type.INT, DONT_CARE, this);

        st.pop(2);
        st.push(vme);
    } else if (ins instanceof MONITORENTER || ins instanceof MONITOREXIT || ins instanceof POP
            || ins instanceof POP2) {
        setStackArgs(st, 1);
        st.pop();
    } else if (ins instanceof RETURN) {
        st.reset();
    } else if (ins instanceof ReturnInstruction) {
        setStackArgs(st, 1);
        st.reset();
    } else if (ins instanceof SWAP) {
        setStackArgs(st, 2);
        VMStackElement vme = new VMStackElement(st.top());

        st.pop();
        VMStackElement vme1 = new VMStackElement(st.top());

        st.pop();
        st.push(vme);
        st.push(vme1);
    } else if (ins instanceof DUP) {
        setStackArgs(st, 1);
        VMStackElement vme = new VMStackElement(st.top());

        vme.defuse = DONT_CARE;
        st.push(vme);
    } else if (ins instanceof DUP2) {
        setStackArgs2(st, 2);
        VMStackElement[] vme = new VMStackElement[2];
        int k = 0, sum = 0;

        while (sum < 2) {
            vme[k] = st.top();
            sum += vme[k++].type.getSize();
            st.pop();
        }
        for (int i = k - 1; i >= 0; i--) {
            VMStackElement vme1 = new VMStackElement(vme[i]);

            vme1.defuse = DONT_CARE;
            st.push(vme1);
        }
        for (int i = k - 1; i >= 0; i--) {
            st.push(vme[i]);
        }
    } else if (ins instanceof DUP_X1) {
        setStackArgs(st, 2);
        VMStackElement vme = st.top();

        st.pop();
        VMStackElement vme1 = new VMStackElement(vme);

        vme1.defuse = DONT_CARE;
        VMStackElement vme2 = st.top();

        st.pop();
        st.push(vme1);
        st.push(vme2);
        st.push(vme);
    } else if (ins instanceof DUP2_X1) {
        setStackArgs2(st, 3);
        VMStackElement[] vme = new VMStackElement[2];
        int k = 0, sum = 0;

        while (sum < 2) {
            vme[k] = st.top();
            sum += vme[k++].type.getSize();
            st.pop();
        }
        VMStackElement vme1 = st.top();

        st.pop();
        for (int i = k - 1; i >= 0; i--) {
            VMStackElement vme2 = new VMStackElement(vme[i]);

            vme2.defuse = DONT_CARE;
            st.push(vme2);
        }
        st.push(vme1);
        for (int i = k - 1; i >= 0; i--) {
            st.push(vme[i]);
        }
    } else if (ins instanceof DUP_X2) {
        setStackArgs2(st, 3);
        VMStackElement[] vme = new VMStackElement[3];
        int k = 0, sum = 0;

        while (sum < 3) {
            vme[k] = st.top();

            sum += vme[k++].type.getSize();
            st.pop();
        }
        VMStackElement vme1 = new VMStackElement(vme[0]);

        vme1.defuse = DONT_CARE;
        st.push(vme1);
        for (int i = k - 1; i >= 0; i--) {
            st.push(vme[i]);
        }
    } else if (ins instanceof DUP2_X2) {
        setStackArgs2(st, 4);
        VMStackElement[] vme = new VMStackElement[2];
        int k = 0, j = 0, sum = 0;

        while (sum < 2) {
            vme[k] = st.top();
            sum += vme[k++].type.getSize();
            st.pop();
        }
        VMStackElement[] vme1 = new VMStackElement[2];

        while (sum < 4) {
            vme1[j] = st.top();
            sum += vme1[j++].type.getSize();
            st.pop();
        }
        for (int i = k - 1; i >= 0; i--) {
            VMStackElement vme2 = new VMStackElement(vme[i]);

            vme2.defuse = DONT_CARE;
            st.push(vme2);
        }
        for (int i = j - 1; i >= 0; i--) {
            st.push(vme1[i]);
        }
        for (int i = k - 1; i >= 0; i--) {
            st.push(vme[i]);
        }
    } else if (ins instanceof LoadInstruction) {
        LoadInstruction iins = (LoadInstruction) ins;
        int index = iins.getIndex();

        setLocalArg(locals, index);
        useLocalAdd(STR_LOCAL + index);
        if (locals.get(index) == null) {
            throw new InvalidStackArgument("Trying to load " + "uninitialized element");
        }
        VMStackElement vme = new VMStackElement(locals.get(index), STR_LOCAL + index, this);

        st.push(vme);
    } else if (ins instanceof StoreInstruction) {
        setStackArgs(st, 1);
        StoreInstruction iins = (StoreInstruction) ins;
        int index = iins.getIndex();

        defLocalAdd(STR_LOCAL + index);
        VMStackElement vme = st.top();

        st.pop();
        locals.add(vme.type, index);
    } else if (ins instanceof InvokeInstruction) {
        // verifica se eh uma chamada de super.
        // para isso verifica se o methodo chamado eh <init>
        // e se o objeto sendo usado eh L@0
        if (ins instanceof INVOKESPECIAL) {
            INVOKESPECIAL iesp = (INVOKESPECIAL) ins;
            if (iesp.getName(cp).equals("<init>")) {
                Type[] t = iesp.getArgumentTypes(cp);
                VMStackElement el = st.top(t.length);

                if (el.defuse.equals(STR_LOCAL + "0")) {
                    isSuper = true;
                }
            }
        }

        setStackArgs2(st, ins.consumeStack(cp));
        InvokeInstruction iins = (InvokeInstruction) ins;
        VMStackElement vme = new VMStackElement(iins.getReturnType(cp), DONT_CARE, this);

        st.pop2(ins.consumeStack(cp));
        st.push(vme);
    } else if (ins instanceof GETSTATIC) {
        FieldInstruction iins = (FieldInstruction) ins;
        String defUse = STR_STATIC + iins.getClassName(cp) + "." + iins.getFieldName(cp);
        VMStackElement vme = new VMStackElement(iins.getFieldType(cp), defUse, this);

        useFieldAdd(defUse);
        st.push(vme);
    } else if (ins instanceof GETFIELD) {
        setStackArgs(st, 1);
        FieldInstruction iins = (FieldInstruction) ins;
        String defUse = st.top().defuse + "." + iins.getFieldName(cp);
        VMStackElement vme = new VMStackElement(iins.getFieldType(cp), defUse, this);

        useFieldAdd(defUse);
        st.pop(1);
        st.push(vme);
    } else if (ins instanceof BREAKPOINT || ins instanceof IMPDEP1 || ins instanceof IMPDEP2
            || ins instanceof NOP) {
        ;
    } else if (ins instanceof RET) {
        IndexedInstruction iins = (IndexedInstruction) ins;
        int index = iins.getIndex();

        setLocalArg(locals, index);
    } else if (ins instanceof CHECKCAST) {
        setStackArgs(st, 1);
    } else if (ins instanceof IINC) {
        String defUse = STR_LOCAL + ((IINC) ins).getIndex();

        useLocalAdd(defUse);
        defLocalAdd(defUse);
    } else {
        throw new InvalidInstructionException(ins.toString());
    }
}