Example usage for org.objectweb.asm.tree FieldInsnNode getOpcode

List of usage examples for org.objectweb.asm.tree FieldInsnNode getOpcode

Introduction

In this page you can find the example usage for org.objectweb.asm.tree FieldInsnNode getOpcode.

Prototype

public int getOpcode() 

Source Link

Document

Returns the opcode of this instruction.

Usage

From source file:org.rascalmpl.library.lang.jvm.transform.Rascalify.java

License:Open Source License

@SuppressWarnings("unchecked")
private static void writeInstructions(OutputStreamWriter writer, MethodNode mn) throws IOException {
    writer.write("[");
    boolean first = true;
    for (AbstractInsnNode ai : mn.instructions.toArray()) {
        if (first) {
            first = false;//  ww w  . ja  va  2s  .com
        } else {
            writer.write(",");
        }
        if (ai instanceof FieldInsnNode) {
            FieldInsnNode n = ((FieldInsnNode) ai);
            writer.write("\n\t\t\t\tfieldRef(" + n.getOpcode() + ", \"" + escape(n.owner) + "\", \""
                    + escape(n.name) + "\", \"" + escape(n.desc) + "\")");
        } else if (ai instanceof IincInsnNode) {
            IincInsnNode n = ((IincInsnNode) ai);
            writer.write("\n\t\t\t\tincrement(" + n.var + ", " + n.incr + ")");
        } else if (ai instanceof InsnNode) {
            InsnNode n = ((InsnNode) ai);
            writer.write("\n\t\t\t\tinstruction(" + n.getOpcode() + ")");
        } else if (ai instanceof IntInsnNode) {
            IntInsnNode n = ((IntInsnNode) ai);
            writer.write("\n\t\t\t\tinteger(" + n.getOpcode() + ", " + n.operand + ")");
        } else if (ai instanceof JumpInsnNode) {
            JumpInsnNode n = ((JumpInsnNode) ai);
            writer.write("\n\t\t\t\tjump(" + n.getOpcode() + ", " + getLabelIndex(n.label) + ")");
        } else if (ai instanceof LabelNode) {
            LabelNode n = ((LabelNode) ai);
            writer.write("\n\t\t\t\tlabel(" + getLabelIndex(n) + ")");
        } else if (ai instanceof LineNumberNode) {
            LineNumberNode n = ((LineNumberNode) ai);
            writer.write("\n\t\t\t\tlineNumber(" + n.line + ", " + getLabelIndex(n.start) + ")");
        } else if (ai instanceof VarInsnNode) {
            VarInsnNode n = ((VarInsnNode) ai);
            writer.write("\n\t\t\t\tlocalVariable(" + n.getOpcode() + ", " + n.var + ")");
        } else if (ai instanceof LdcInsnNode) {
            LdcInsnNode n = ((LdcInsnNode) ai);
            if (n.cst instanceof String) {
                writer.write("\n\t\t\t\tloadConstantString(\"" + escape((String) n.cst) + "\")");
            } else if (n.cst instanceof Integer) {
                writer.write("\n\t\t\t\tloadConstantInteger(" + n.cst + ")");
            } else if (n.cst instanceof Long) {
                writer.write("\n\t\t\t\tloadConstantLong(" + n.cst + ")");
            } else if (n.cst instanceof Float) {
                writer.write("\n\t\t\t\tloadConstantFloat(" + n.cst + ")");
            } else if (n.cst instanceof Double) {
                writer.write("\n\t\t\t\tloadConstantDouble(" + n.cst + ")");
            }
        } else if (ai instanceof LookupSwitchInsnNode) {
            LookupSwitchInsnNode n = ((LookupSwitchInsnNode) ai);
            writer.write("\n\t\t\t\tlookupSwitch(" + getLabelIndex(n.dflt) + ", [");
            boolean firstKey = true;
            for (Integer k : (List<Integer>) n.keys) {
                if (firstKey) {
                    firstKey = false;
                } else {
                    writer.write(", ");
                }
                writer.write(k);
            }
            writer.write("], [");
            boolean firstCase = true;
            for (LabelNode l : (List<LabelNode>) n.labels) {
                if (firstCase) {
                    firstCase = false;
                } else {
                    writer.write(", ");
                }
                writer.write("" + getLabelIndex(l));
            }
            writer.write("])");
        } else if (ai instanceof MethodInsnNode) {
            MethodInsnNode n = ((MethodInsnNode) ai);
            writer.write("\n\t\t\t\tmethod(" + n.getOpcode() + ", \"" + escape(n.owner) + "\", \""
                    + escape(n.name) + "\", \"" + escape(n.desc) + "\")");
        } else if (ai instanceof MultiANewArrayInsnNode) {
            MultiANewArrayInsnNode n = ((MultiANewArrayInsnNode) ai);
            writer.write("\n\t\t\t\tmultiANewArray(\"" + escape(n.desc) + "\", " + n.dims + ")");
        } else if (ai instanceof TableSwitchInsnNode) {
            TableSwitchInsnNode n = ((TableSwitchInsnNode) ai);
            writer.write(
                    "\n\t\t\t\ttableSwitch(" + n.min + ", " + n.max + ", " + getLabelIndex(n.dflt) + ", [");
            boolean firstCase = true;
            for (LabelNode l : (List<LabelNode>) n.labels) {
                if (firstCase) {
                    firstCase = false;
                } else {
                    writer.write(", ");
                }
                writer.write("" + getLabelIndex(l));
            }
            writer.write("])");
        } else if (ai instanceof TypeInsnNode) {
            TypeInsnNode n = ((TypeInsnNode) ai);
            writer.write("\n\t\t\t\t\\type(" + n.getOpcode() + ", \"" + escape(n.desc) + "\")");
        } else {
            if (!(ai instanceof FrameNode)) {
                throw new RuntimeException(
                        "Error: Unsupported instruction encountered (" + ai.getClass() + ").");
            }
            first = true;
        }
    }
    writer.write("\n\t\t\t]");
}

From source file:org.spongepowered.asm.mixin.transformer.MixinTargetContext.java

License:MIT License

/**
 * Handle "imaginary super" invokations, these are invokations in
 * non-derived mixins for accessing methods known to exist in a supermixin
 * which is not directly inherited by this mixix. The method can only call
 * its <b>own</b> super-implmentation and the methd must also be tagged with
 * {@link SoftOverride} to indicate that the method must exist in a super
 * class./*from www  .  ja v  a 2 s. c o m*/
 * 
 * @param method Method being processed
 * @param fieldInsn the GETFIELD insn which access the pseudo-field which is
 *      used as a handle to the superclass
 */
private void processImaginarySuper(MethodNode method, FieldInsnNode fieldInsn) {
    if (fieldInsn.getOpcode() != Opcodes.GETFIELD) {
        if (MixinTargetContext.INIT.equals(method.name)) {
            throw new InvalidMixinException(this, "Illegal imaginary super declaration: field " + fieldInsn.name
                    + " must not specify an initialiser");
        }

        throw new InvalidMixinException(this, "Illegal imaginary super access: found "
                + ASMHelper.getOpcodeName(fieldInsn.getOpcode()) + " opcode in " + method.name + method.desc);
    }

    if ((method.access & Opcodes.ACC_PRIVATE) != 0 || (method.access & Opcodes.ACC_STATIC) != 0) {
        throw new InvalidMixinException(this, "Illegal imaginary super access: method " + method.name
                + method.desc + " is private or static");
    }

    if (ASMHelper.getInvisibleAnnotation(method, SoftOverride.class) == null) {
        throw new InvalidMixinException(this, "Illegal imaginary super access: method " + method.name
                + method.desc + " is not decorated with @SoftOverride");
    }

    for (Iterator<AbstractInsnNode> methodIter = method.instructions
            .iterator(method.instructions.indexOf(fieldInsn)); methodIter.hasNext();) {
        AbstractInsnNode insn = methodIter.next();
        if (insn instanceof MethodInsnNode) {
            MethodInsnNode methodNode = (MethodInsnNode) insn;
            if (methodNode.owner.equals(this.getClassRef()) && methodNode.name.equals(method.name)
                    && methodNode.desc.equals(method.desc)) {
                methodNode.setOpcode(Opcodes.INVOKESPECIAL);
                this.updateStaticBinding(method, methodNode);
                return;
            }
        }
    }

    throw new InvalidMixinException(this,
            "Illegal imaginary super access: could not find INVOKE for " + method.name + method.desc);
}

From source file:pku.sei.checkedcoverage.tracer.instrumentation.TracingMethodInstrumenter.java

License:Creative Commons License

private void transformFieldInsn(final FieldInsnNode insn) {
    int objectTraceSeqIndex = -1;

    switch (insn.getOpcode()) {
    case PUTSTATIC:
    case GETSTATIC:
        // nothing is traced
        break;/*  w w w . j a  va2s  . c om*/

    case GETFIELD:
        // do not trace assignments or usages of "this$..." and "val$..." fields
        if (!insn.name.contains("$")) { // TODO can we lift this?
            // top item on stack is the object reference: duplicate it
            // (add instruction *before* the current one
            this.instructionIterator.previous();
            this.instructionIterator.add(new InsnNode(DUP));
            objectTraceSeqIndex = this.tracer.newLongTraceSequence();
            ++TracingMethodInstrumenter.statsGetField;
            //System.out.println("seq " + index + ": getField " + name + " in method " + readMethod.getReadClass().getClassName() + "." + readMethod.getName());
        }
        break;

    case PUTFIELD:
        // do not trace assignments or usages of "this$..." and "val$..." fields
        if (!insn.name.contains("$")) { // TODO can we lift this?
            // the second item on the stack is the object reference
            // (add instruction *before* the current one
            this.instructionIterator.previous();
            final int size = Type.getType(insn.desc).getSize(); // either 1 or 2
            if (size == 1) {
                this.instructionIterator.add(new InsnNode(DUP2));
                this.instructionIterator.add(new InsnNode(POP));
            } else {
                this.instructionIterator.add(new InsnNode(DUP2_X1));
                this.instructionIterator.add(new InsnNode(POP2));
                this.instructionIterator.add(new InsnNode(DUP_X2));
            }
            objectTraceSeqIndex = this.tracer.newLongTraceSequence();
            ++TracingMethodInstrumenter.statsPutField;
            //System.out.println("seq " + index + ": putField " + name + " in method " + readMethod.getReadClass().getClassName() + "." + readMethod.getName());
        }
        break;

    default:
        break;
    }

    if (objectTraceSeqIndex != -1) {
        this.instructionIterator.add(new VarInsnNode(ALOAD, this.tracerLocalVarIndex));
        this.instructionIterator.add(new InsnNode(SWAP));
        this.instructionIterator.add(getIntConstInsn(objectTraceSeqIndex));
        this.instructionIterator.add(new MethodInsnNode(INVOKEINTERFACE,
                Type.getInternalName(ThreadTracer.class), "traceObject", "(Ljava/lang/Object;I)V"));
        // and move to the position where it was before entering this method
        this.instructionIterator.next();
    }

    registerInstruction(new FieldInstruction(this.readMethod, insn.getOpcode(), this.currentLine, insn.owner,
            insn.name, insn.desc, objectTraceSeqIndex), InstructionType.UNSAFE);
}

From source file:soot.asm.AsmMethodSource.java

License:Open Source License

private void convertGetFieldInsn(FieldInsnNode insn) {
    StackFrame frame = getFrame(insn);/*from  www.j a v a  2s.  c o m*/
    Operand[] out = frame.out();
    Operand opr;
    Type type;
    if (out == null) {
        SootClass declClass = Scene.v().getSootClass(AsmUtil.toQualifiedName(insn.owner));
        type = AsmUtil.toJimpleType(insn.desc);
        Value val;
        SootFieldRef ref;
        if (insn.getOpcode() == GETSTATIC) {
            ref = Scene.v().makeFieldRef(declClass, insn.name, type, true);
            val = Jimple.v().newStaticFieldRef(ref);
        } else {
            Operand base = popLocal();
            ref = Scene.v().makeFieldRef(declClass, insn.name, type, false);
            InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(base.stackOrValue(), ref);
            val = ifr;
            base.addBox(ifr.getBaseBox());
            frame.in(base);
            frame.boxes(ifr.getBaseBox());
        }
        opr = new Operand(insn, val);
        frame.out(opr);
    } else {
        opr = out[0];
        type = opr.<FieldRef>value().getFieldRef().type();
        if (insn.getOpcode() == GETFIELD)
            frame.mergeIn(pop());
    }
    push(type, opr);
}

From source file:soot.asm.AsmMethodSource.java

License:Open Source License

private void convertPutFieldInsn(FieldInsnNode insn) {
    boolean instance = insn.getOpcode() == PUTFIELD;
    StackFrame frame = getFrame(insn);//from  w  w w . j  a v a  2s.c om
    Operand[] out = frame.out();
    Operand opr, rvalue;
    Type type;
    if (out == null) {
        SootClass declClass = Scene.v().getSootClass(AsmUtil.toQualifiedName(insn.owner));
        type = AsmUtil.toJimpleType(insn.desc);
        Value val;
        SootFieldRef ref;
        rvalue = popImmediate(type);
        if (!instance) {
            ref = Scene.v().makeFieldRef(declClass, insn.name, type, true);
            val = Jimple.v().newStaticFieldRef(ref);
            frame.in(rvalue);
        } else {
            Operand base = popLocal();
            ref = Scene.v().makeFieldRef(declClass, insn.name, type, false);
            InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(base.stackOrValue(), ref);
            val = ifr;
            base.addBox(ifr.getBaseBox());
            frame.in(rvalue, base);
        }
        opr = new Operand(insn, val);
        frame.out(opr);
        AssignStmt as = Jimple.v().newAssignStmt(val, rvalue.stackOrValue());
        rvalue.addBox(as.getRightOpBox());
        if (!instance) {
            frame.boxes(as.getRightOpBox());
        } else {
            frame.boxes(as.getRightOpBox(), ((InstanceFieldRef) val).getBaseBox());
        }
        setUnit(insn, as);
    } else {
        opr = out[0];
        type = opr.<FieldRef>value().getFieldRef().type();
        rvalue = pop(type);
        if (!instance) {
            /* PUTSTATIC only needs one operand on the stack, the rvalue */
            frame.mergeIn(rvalue);
        } else {
            /* PUTFIELD has a rvalue and a base */
            frame.mergeIn(rvalue, pop());
        }
    }
    /*
     * in case any static field or array is read from, and the static constructor
     * or the field this instruction writes to, modifies that field, write out any
     * previous read from field/array
     */
    assignReadOps(null);
}

From source file:soot.asm.AsmMethodSource.java

License:Open Source License

private void convertFieldInsn(FieldInsnNode insn) {
    int op = insn.getOpcode();
    if (op == GETSTATIC || op == GETFIELD)
        convertGetFieldInsn(insn);//w  w w  .  j  a  v a2s  . com
    else
        convertPutFieldInsn(insn);
}