Java Code Examples for org.objectweb.asm.Opcodes#POP

The following examples show how to use org.objectweb.asm.Opcodes#POP . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LayerCustomHeadVisitor.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
    if (checkTargetInsn(opcode, owner, name, desc)) {
    	markPatchedSuccessfully();
        Label endLabel = new Label();
        Label skipLabel = new Label();
        super.visitVarInsn(Opcodes.ALOAD, 1); //load entity
        super.visitMethodInsn(Opcodes.INVOKESTATIC, ARMOR_HOOKS_OWNER, ARMOR_HOOKS_METHOD_NAME, ARMOR_HOOKS_SIGNATURE, false);
        super.visitJumpInsn(Opcodes.IFEQ, skipLabel);
        for (int i = 0; i < 4; i++) super.visitInsn(Opcodes.POP); //pop this, entity, stack, transformType
        super.visitJumpInsn(Opcodes.GOTO, endLabel);
        super.visitLabel(skipLabel);
        super.visitMethodInsn(opcode, owner, name, desc, itf);
        super.visitLabel(endLabel);
        return;
    }
    super.visitMethodInsn(opcode, owner, name, desc, itf);
}
 
Example 2
Source File: RuntimeInstrument.java    From dacapobench with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unused")
protected void onMethodEnter() {
	if (done) return;

	overridden = true;
	Label start  = new Label();
	Label normal = new Label();
	super.visitLabel(start);
	super.visitFieldInsn(Opcodes.GETSTATIC, CONFIGURATION, CONFIGURATION_FIELD_NAME, Type.INT_TYPE.getDescriptor());
	super.visitInsn(Opcodes.DUP);
	super.visitJumpInsn(Opcodes.IFEQ, normal);
	super.visitInsn(Opcodes.IRETURN);
	super.visitLabel(normal);
	super.visitInsn(Opcodes.POP);
	Label end = new Label();
	super.visitJumpInsn(Opcodes.GOTO, end);
	super.visitLabel(end);
	super.visitTryCatchBlock(start, normal, end, Type.getType(Throwable.class).getDescriptor());
}
 
Example 3
Source File: MemberVariableMutator.java    From pitest with Apache License 2.0 6 votes vote down vote up
@Override
public void visitFieldInsn(final int opcode, final String owner,
    final String name, final String desc) {
  if ((Opcodes.PUTFIELD == opcode) && shouldMutate(name)) {
    // removed setting field

    // pop the values which PUTFIELD would have used
    if (Type.getType(desc).getSize() == 2) {
      super.visitInsn(Opcodes.POP2);
      super.visitInsn(Opcodes.POP);
    } else {
      super.visitInsn(Opcodes.POP);
      super.visitInsn(Opcodes.POP);
    }
  } else {
    super.visitFieldInsn(opcode, owner, name, desc);
  }
}
 
Example 4
Source File: ReturnValuesMutator.java    From pitest with Apache License 2.0 6 votes vote down vote up
/**
 * Mutates a primitive float return (<code>Opcode.FRETURN</code>). The
 * strategy used was translated from jumble BCEL code. The following is
 * complicated by the problem of <tt>NaN</tt>s. By default the new value is
 * <code>-(x + 1)</code>, but this doesn't work for <tt>NaN</tt>s. But for a
 * <tt>NaN</tt> <code>x != x</code> is true, and we use this to detect them.
 *
 * @see #mutatePrimitiveDoubleReturn()
 */
private void mutatePrimitiveFloatReturn() {
  if (shouldMutate("primitive float", "(x != NaN)? -(x + 1) : -1 ")) {
    final Label label = new Label();

    super.visitInsn(Opcodes.DUP);
    super.visitInsn(Opcodes.DUP);
    super.visitInsn(Opcodes.FCMPG);
    super.visitJumpInsn(Opcodes.IFEQ, label);

    super.visitInsn(Opcodes.POP);
    super.visitInsn(Opcodes.FCONST_0);

    // the following code is executed in NaN case, too
    super.visitLabel(label);
    super.visitInsn(Opcodes.FCONST_1);
    super.visitInsn(Opcodes.FADD);
    super.visitInsn(Opcodes.FNEG);
    super.visitInsn(Opcodes.FRETURN);
  }
}
 
Example 5
Source File: RemoveConditionalMutator.java    From pitest with Apache License 2.0 6 votes vote down vote up
private void emptyStack(final int opcode) {
  switch (opcode) {
  // EQUAL
  case Opcodes.IF_ICMPNE:
  case Opcodes.IF_ICMPEQ:
  case Opcodes.IF_ACMPEQ:
  case Opcodes.IF_ACMPNE:
    // ORDER
  case Opcodes.IF_ICMPGE:
  case Opcodes.IF_ICMPGT:
  case Opcodes.IF_ICMPLE:
  case Opcodes.IF_ICMPLT:
    super.visitInsn(Opcodes.POP2);
    break;
  default:
    super.visitInsn(Opcodes.POP);
  }

}
 
Example 6
Source File: LangModelHelper.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the operation code for pop operations with a single instruction support by their type
 * sizes on stack top
 */
public static int getTypeSizeAlignedPopOpcode(ImmutableList<Type> elementsToPop) {
  int totalWordsToPop = getTotalWords(elementsToPop);
  switch (totalWordsToPop) {
    case 1:
      return Opcodes.POP;
    case 2:
      return Opcodes.POP2;
    default:
      throw new IllegalStateException(
          String.format(
              "Expected either 1 or 2 words to be popped, but actually requested to pop (%d)"
                  + " words from <top/>%s...<bottom/>",
              totalWordsToPop, elementsToPop));
  }
}
 
Example 7
Source File: Globalizer.java    From Concurnas with MIT License 5 votes vote down vote up
private void genericSswap(String desc) {
	//caters for double, int on stack - to swap
	if(desc.equals("J") || desc.equals("D")){//long and double take up two slots insteead of 1
		super.visitInsn(Opcodes.DUP_X2);
		super.visitInsn(Opcodes.POP);
	}
	else{//1 slot each
		super.visitInsn(Opcodes.SWAP);
	}
}
 
Example 8
Source File: TypeUtils.java    From maple-ir with GNU General Public License v3.0 5 votes vote down vote up
public static int getPopOpcode(Type type) {
	if (type.getSize() == 1) {
		return Opcodes.POP;
	} else if (type.getSize() == 2) {
		return Opcodes.POP2;
	} else {
		throw new IllegalArgumentException(type.toString());
	}
}
 
Example 9
Source File: LazyLoadingMethodVisitor.java    From AVM with MIT License 4 votes vote down vote up
/**
 * NOTE:  All calls to instruction visitation routines are made against super, directly, since we do frame offset accounting within our overrides
 * and that offset only applies to incoming bytecodes, not outgoing ones.
 * 
 * @param opcode The opcode.
 * @param descriptor The type descriptor of the field to which the opcode is applied.
 */
private void checkInjectLazyLoad(int opcode, String descriptor) {
    // If this is a PUTFIELD or GETFIELD, we want to call "lazyLoad()":
    // -PUTIFELD:  DUP2, POP, INVOKEVIRTUAL
    // -GETIFELD:  DUP, INVOKEVIRTUAL
    if ((Opcodes.PUTFIELD == opcode) && ((null == this.tracker) || !this.tracker.isThisTargetOfPut(this.frameOffset))) {
        // We need to see how big this type is since double and long need a far more complex dance.
        if ((1 == descriptor.length()) && ((DescriptorParser.LONG == descriptor.charAt(0)) || (DescriptorParser.DOUBLE == descriptor.charAt(0)))) {
            // Here, the stack looks like: ... OBJECT, VAR1, VAR2 (top)
            // Where we need:  ... OBJECT, VAR1, VAR2, OBJECT (top)
            // This is multiple stages:
            // DUP2_X1: ... VAR1, VAR2, OBJECT, VAR1, VAR2 (top)
            super.visitInsn(Opcodes.DUP2_X1);
            // POP2: ... VAR1, VAR2, OBJECT (top)
            super.visitInsn(Opcodes.POP2);
            // DUP: ... VAR1, VAR2, OBJECT, OBJECT (top)
            super.visitInsn(Opcodes.DUP);
            // INOKE: ... VAR1, VAR2, OBJECT (top)
            super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, SHADOW_OBJECT_NAME, LAZY_LOAD_NAME, LAZY_LOAD_DESCRIPTOR, false);
            // DUP_X2: ... OBJECT, VAR1, VAR2, OBJECT (top)
            super.visitInsn(Opcodes.DUP_X2);
            // POP: ... OBJECT, VAR1, VAR2 (top)
            super.visitInsn(Opcodes.POP);
        } else {
            // Here, the stack looks like: ... OBJECT, VAR, (top)
            // Where we need:  ... OBJECT, VAR, OBJECT (top)
            // Stages:
            // DUP2: ... OBJECT, VAR, OBJECT, VAR (top)
            super.visitInsn(Opcodes.DUP2);
            // POP: ... OBJECT, VAR, OBJECT (top)
            super.visitInsn(Opcodes.POP);
            // INOKE: ... OBJECT, VAR (top)
            super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, SHADOW_OBJECT_NAME, LAZY_LOAD_NAME, LAZY_LOAD_DESCRIPTOR, false);
        }
    } else if ((Opcodes.GETFIELD == opcode) && ((null == this.tracker) || !this.tracker.isThisTargetOfGet(this.frameOffset))) {
        // Here, the stack looks like: ... OBJECT, (top)
        // Where we need:  ... OBJECT, OBJECT (top)
        super.visitInsn(Opcodes.DUP);
        super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, SHADOW_OBJECT_NAME, LAZY_LOAD_NAME, LAZY_LOAD_DESCRIPTOR, false);
    }
}
 
Example 10
Source File: AllocationMethodAdapter.java    From allocation-instrumenter with Apache License 2.0 4 votes vote down vote up
private void pushProductOfIntArrayOnStack() {
  Label beginScopeLabel = new Label();
  Label endScopeLabel = new Label();

  int dimsArrayIndex = newLocal("[I", beginScopeLabel, endScopeLabel);
  int counterIndex = newLocal("I", beginScopeLabel, endScopeLabel);
  int productIndex = newLocal("I", beginScopeLabel, endScopeLabel);
  Label loopLabel = new Label();
  Label endLabel = new Label();

  super.visitLabel(beginScopeLabel);

  // stack: ... intArray
  super.visitVarInsn(Opcodes.ASTORE, dimsArrayIndex);
  // -> stack: ...

  // counter = 0
  super.visitInsn(Opcodes.ICONST_0);
  super.visitVarInsn(Opcodes.ISTORE, counterIndex);
  // product = 1
  super.visitInsn(Opcodes.ICONST_1);
  super.visitVarInsn(Opcodes.ISTORE, productIndex);
  // loop:
  super.visitLabel(loopLabel);
  // if index >= arraylength goto end:
  super.visitVarInsn(Opcodes.ILOAD, counterIndex);
  super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
  super.visitInsn(Opcodes.ARRAYLENGTH);
  super.visitJumpInsn(Opcodes.IF_ICMPGE, endLabel);
  // product = product * max(array[counter],1)
  super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
  super.visitVarInsn(Opcodes.ILOAD, counterIndex);
  super.visitInsn(Opcodes.IALOAD);
  super.visitInsn(Opcodes.DUP);
  Label nonZeroDimension = new Label();
  super.visitJumpInsn(Opcodes.IFNE, nonZeroDimension);
  super.visitInsn(Opcodes.POP);
  super.visitInsn(Opcodes.ICONST_1);
  super.visitLabel(nonZeroDimension);
  super.visitVarInsn(Opcodes.ILOAD, productIndex);
  super.visitInsn(Opcodes.IMUL); // if overflow happens it happens.
  super.visitVarInsn(Opcodes.ISTORE, productIndex);
  // iinc counter 1
  super.visitIincInsn(counterIndex, 1);
  // goto loop
  super.visitJumpInsn(Opcodes.GOTO, loopLabel);
  // end:
  super.visitLabel(endLabel);
  // re-push dimensions array
  super.visitVarInsn(Opcodes.ALOAD, dimsArrayIndex);
  // push product
  super.visitVarInsn(Opcodes.ILOAD, productIndex);

  super.visitLabel(endScopeLabel);
}
 
Example 11
Source File: AllocationMethodAdapter.java    From allocation-instrumenter with Apache License 2.0 4 votes vote down vote up
void calculateArrayLengthAndDispatch(String typeName, int dimCount) {
  // Since the dimensions of the array are not known at instrumentation
  // time, we take the created multi-dimensional array and peel off nesting
  // levels from the left.  For each nesting layer we probe the array length
  // and accumulate a partial product which we can then feed the recording
  // function.

  // below we note the partial product of dimensions 1 to X-1 as productToX
  // (so productTo1 == 1 == no dimensions yet).  We denote by aref0 the
  // array reference at the current nesting level (the containing aref's [0]
  // element).  If we hit a level whose arraylength is 0 or whose
  // reference is null, there's no point continuing, so we shortcut
  // out.

  // This approach works pretty well when you create a new array with the
  // newarray bytecodes.  You can also create a new array by cloning an
  // existing array; an existing multidimensional array might have had some
  // of its [0] elements nulled out.  We currently deal with this by bailing
  // out, but arguably we should do something more principled (like calculate
  // the size of the multidimensional array from scratch if you are using
  // clone()).
  // TODO(java-platform-team): Do something about modified multidimensional
  // arrays and clone().
  Label zeroDimension = new Label();
  super.visitInsn(Opcodes.DUP); // -> stack: ... origaref aref0
  super.visitLdcInsn(1); // -> stack: ... origaref aref0 productTo1
  for (int i = 0; i < dimCount; ++i) {
    // pre: stack: ... origaref aref0 productToI
    super.visitInsn(Opcodes.SWAP); // -> stack: ... origaref productToI aref
    super.visitInsn(Opcodes.DUP);

    Label nonNullDimension = new Label();
    // -> stack: ... origaref productToI aref aref
    super.visitJumpInsn(Opcodes.IFNONNULL, nonNullDimension);
    // -> stack: ... origaref productToI aref
    super.visitInsn(Opcodes.SWAP);
    // -> stack: ... origaref aref productToI
    super.visitJumpInsn(Opcodes.GOTO, zeroDimension);
    super.visitLabel(nonNullDimension);

    // -> stack: ... origaref productToI aref
    super.visitInsn(Opcodes.DUP_X1);
    // -> stack: ... origaref aref0 productToI aref
    super.visitInsn(Opcodes.ARRAYLENGTH);
    // -> stack: ... origaref aref0 productToI dimI

    Label nonZeroDimension = new Label();
    super.visitInsn(Opcodes.DUP);
    // -> stack: ... origaref aref0 productToI dimI dimI
    super.visitJumpInsn(Opcodes.IFNE, nonZeroDimension);
    // -> stack: ... origaref aref0 productToI dimI
    super.visitInsn(Opcodes.POP);
    // -> stack: ... origaref aref0 productToI
    super.visitJumpInsn(Opcodes.GOTO, zeroDimension);
    super.visitLabel(nonZeroDimension);
    // -> stack: ... origaref aref0 productToI max(dimI,1)

    super.visitInsn(Opcodes.IMUL);
    // -> stack: ... origaref aref0 productTo{I+1}
    if (i < dimCount - 1) {
      super.visitInsn(Opcodes.SWAP);
      // -> stack: ... origaref productTo{I+1} aref0
      super.visitInsn(Opcodes.ICONST_0);
      // -> stack: ... origaref productTo{I+1} aref0 0
      super.visitInsn(Opcodes.AALOAD);
      // -> stack: ... origaref productTo{I+1} aref0'
      super.visitInsn(Opcodes.SWAP);
    }
    // post: stack: ... origaref aref0 productTo{I+1}
  }
  super.visitLabel(zeroDimension);

  super.visitInsn(Opcodes.SWAP); // -> stack: ... origaref product aref0
  super.visitInsn(Opcodes.POP); // -> stack: ... origaref product
  super.visitInsn(Opcodes.SWAP); // -> stack: ... product origaref
  invokeRecordAllocation(typeName);
}
 
Example 12
Source File: NameTranslatorClassVisitor.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
public void visitMethodInsn(final int opcode, final String owner,
		final String name, final String desc) {

	// Special case method invocations for name translation.
	// Specifically to deal with methods mirrors.
	String newOwner = owner;
	int newOpcode = opcode;
	String newDesc = translator.translateMethodDescriptor(desc);
	
	String lookupOwner = owner;
	while (lookupOwner.startsWith("[")) {
		lookupOwner = lookupOwner.substring(1);
	}
	final Mirror mirror = translator.getMirror(lookupOwner);

	if (mirror.isClassMirror()) {
		newOwner = translator.translate(owner);
	} else if ("<init>".equals(name)&&(opcode == Opcodes.INVOKESPECIAL)) {
		/* Look for an equivalent constructor. For instance,
		 * INVOKESPECIAL, "java/math/BigDecimal", "<init>", "(I)V")
		 * will be transformed as
		 * INVOKESTATIC, "../BigDecimal_", "BigDecimal", "(I)Ljava/math/BigDecimal;"
		 * 
		 * the previously constructed object was on top of the stack and the mirror
		 * function has put its result on top, so a SWAP and POP are issued to
		 * discard the previously constructed object and store the new one instead.
		 */
		String constructorDesc = newDesc.substring(0, newDesc.length()-1) + 'L' + owner + ';';
		String constructorName;
		int i = owner.lastIndexOf('/');
		if (i == -1) {
			constructorName = owner;
		} else {
			constructorName = owner.substring(i+1);
		}
		if (mirror.hasMethod(owner, constructorName, constructorDesc, Opcodes.INVOKESPECIAL)) {
			newOwner = translator.translate(owner);

			super.visitMethodInsn(Opcodes.INVOKESTATIC, newOwner, constructorName,
					constructorDesc);

			super.visitInsn(Opcodes.SWAP);
			super.visitInsn(Opcodes.POP);
			super.visitInsn(Opcodes.SWAP);
			super.visitInsn(Opcodes.POP);
			return;
		}
	} else if (mirror.hasMethod(owner, name, newDesc, opcode)) {
		newOwner = translator.translate(owner);
		newOpcode = Opcodes.INVOKESTATIC;

		// We have to insert the owner into the arguments of the
		// descriptor
		if (opcode == Opcodes.INVOKEVIRTUAL || opcode == Opcodes.INVOKEINTERFACE) {
			final Type[] argTypes = Type.getArgumentTypes(newDesc);
			final Type[] newArgTypes = new Type[argTypes.length + 1];
			newArgTypes[0] = Type.getType("L" + owner + ";");
			System.arraycopy(argTypes, 0, newArgTypes, 1, argTypes.length);
			newDesc = Type.getMethodDescriptor(
					Type.getReturnType(newDesc), newArgTypes);
			newDesc = translator.translateMethodDescriptor(newDesc);
		}
	}

	super.visitMethodInsn(newOpcode, newOwner, name, newDesc);
}