Example usage for org.objectweb.asm Opcodes DUP

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

Introduction

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

Prototype

int DUP

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

Click Source Link

Usage

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

License:Open Source License

@Override
protected void compileBody() throws CompilerException {
    final GeneratorAdapter mv = mv();
    final DataType eltDataType = this.arrayNode.getDataType();
    final ExpressionCompiler eltCompiler = expressionCompiler(eltDataType);
    final Type eltType = eltCompiler.type();
    final String initName = methodName() + "$init";
    final String initDesc = "Z";

    // private boolean xy$init;
    final FieldVisitor fv = cw().visitField(Opcodes.ACC_PRIVATE, initName, initDesc, null, null);
    fv.visitEnd();// w  w w  . java2s  . co  m

    // if (!this.xy$init) {
    final Label skipInit = mv.newLabel();
    mv.loadThis();
    mv.visitFieldInsn(Opcodes.GETFIELD, section().classInternalName(), initName, initDesc);
    mv.visitJumpInsn(Opcodes.IFNE, skipInit);

    // this.xy$init = true;
    mv.loadThis();
    mv.push(true);
    mv.visitFieldInsn(Opcodes.PUTFIELD, section().classInternalName(), initName, initDesc);

    // this.xy = { ?, c1, c2, ... }
    mv.loadThis();
    section().getArrayAccessorForConstDataOnly(this.arrayNode).compileCall(mv);
    int i = 0;
    for (ExpressionNode elt : this.arrayNode.arguments()) {
        if (!(elt instanceof ExpressionNodeForConstantValue)) {
            mv.visitInsn(Opcodes.DUP);
            mv.visitIntInsn(Opcodes.BIPUSH, i);
            eltCompiler.compile(elt);
            mv.arrayStore(eltType);
        }
        i++;
    }
    // return this.xy;
    mv.visitInsn(Opcodes.ARETURN);

    // } else
    // return this.xy;
    mv.mark(skipInit);
    mv.loadThis();
    section().getArrayAccessorForConstDataOnly(this.arrayNode).compileCall(mv);
    mv.visitInsn(Opcodes.ARETURN);

    if (section().hasReset()) {
        final GeneratorAdapter reset = section().resetter();
        // this.xy$init = false;
        reset.loadThis();
        reset.push(false);
        reset.visitFieldInsn(Opcodes.PUTFIELD, section().classInternalName(), initName, initDesc);
    }

}

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

License:Open Source License

private void compileDup(boolean _isArray) {
    if (_isArray) {
        mv().visitInsn(Opcodes.DUP);
    } else {
        compileDup();
    }
}

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

License:Open Source License

protected void compileDup() {
    mv().visitInsn(Opcodes.DUP);
}

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

License:Open Source License

protected final void compileArray(ExpressionNode _arrayNode) throws CompilerException {
    final GeneratorAdapter mv = mv();
    final ExpressionNodeForArrayReference arr = (ExpressionNodeForArrayReference) _arrayNode;
    mv.push(arr.arrayDescriptor().numberOfElements());
    mv.newArray(type());/*from   w  ww.j  a v a 2 s  . c  o m*/
    int i = 0;
    for (ExpressionNode arg : arr.arguments()) {
        mv.visitInsn(Opcodes.DUP);
        mv.push(i++);
        compile(arg);
        mv.arrayStore(type());
    }
}

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

License:Open Source License

final void buildStaticContext() {
    if (this.staticContextBuilt)
        return;/*from   w w  w.  j  a  va 2  s.c  om*/
    this.staticContextBuilt = true;

    final SectionCompiler root = engineCompiler().rootCompiler();
    final ClassWriter cw = root.cw();
    final FieldVisitor fv = cw.visitField(Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, RUNTIME_CONTEXT_NAME,
            RUNTIME_CONTEXT_TYPE.getDescriptor(), null, null);
    fv.visitEnd();

    final GeneratorAdapter mv = root.initializer();
    final MathContext mc = numericType().mathContext();
    if (mc == MathContext.DECIMAL32) {
        compilePredefinedMathContext(mv, "DECIMAL32");
    } else if (mc == MathContext.DECIMAL64) {
        compilePredefinedMathContext(mv, "DECIMAL64");
    } else if (mc == MathContext.DECIMAL128) {
        compilePredefinedMathContext(mv, "DECIMAL128");
    } else if (mc == MathContext.UNLIMITED) {
        compilePredefinedMathContext(mv, "UNLIMITED");
    } else {
        mv.visitTypeInsn(Opcodes.NEW, RUNTIME_CONTEXT_TYPE.getInternalName());
        mv.visitInsn(Opcodes.DUP);
        mv.push(mc.getPrecision());
        final Type modeType = Type.getType(RoundingMode.class);
        mv.getStatic(modeType, mc.getRoundingMode().name(), modeType);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, RUNTIME_CONTEXT_TYPE.getInternalName(), "<init>",
                "(ILjava/math/RoundingMode;)V");
    }
    mv.visitFieldInsn(Opcodes.PUTSTATIC, root.classInternalName(), RUNTIME_CONTEXT_NAME,
            RUNTIME_CONTEXT_DESCRIPTOR);
}

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

License:Open Source License

final void buildStaticContext() {
    if (this.staticContextBuilt)
        return;/*from  w ww  . j  av  a 2 s .  c o m*/
    this.staticContextBuilt = true;

    final SectionCompiler root = engineCompiler().rootCompiler();
    final ClassWriter cw = root.cw();
    final FieldVisitor fv = cw.visitField(Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, RUNTIME_CONTEXT_NAME,
            RUNTIME_CONTEXT_TYPE.getDescriptor(), null, null);
    fv.visitEnd();

    final GeneratorAdapter mv = root.initializer();
    mv.visitTypeInsn(Opcodes.NEW, RUNTIME_CONTEXT_TYPE.getInternalName());
    mv.visitInsn(Opcodes.DUP);
    mv.push(numericType().scale());
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, RUNTIME_CONTEXT_TYPE.getInternalName(), "<init>", "(I)V");
    mv.visitFieldInsn(Opcodes.PUTSTATIC, root.classInternalName(), RUNTIME_CONTEXT_NAME,
            RUNTIME_CONTEXT_DESCRIPTOR);
}

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

License:Open Source License

public void newWithSimpleConstructor(MethodVisitor mv, Class<?> cls) {
    info(2, "generating new for class " + cls);
    Type type = Type.getType(cls);
    mv.visitTypeInsn(Opcodes.NEW, type.getInternalName());
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, type.getInternalName(), "<init>", "()V");
}

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

License:Open Source License

public void wrapArgs(MethodVisitor mv, int access, String desc) {
    info(2, "Wrapping args for descriptor " + desc);

    Type[] atypes = Type.getArgumentTypes(desc);
    emitIntConstant(mv, atypes.length);/* w  w w .  j  a  va 2s  .  c o m*/
    mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");

    int argIndex;
    if ((access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC) {
        argIndex = 0;
    } else {
        argIndex = 1;
    }

    for (int ctr = 0; ctr < atypes.length; ctr++) {
        mv.visitInsn(Opcodes.DUP);
        emitIntConstant(mv, ctr);
        argIndex = wrapArg(mv, argIndex, atypes[ctr]);
        mv.visitInsn(Opcodes.AASTORE);
    }
}

From source file:org.graalvm.compiler.replacements.test.DeoptimizeOnExceptionTest.java

License:Open Source License

private static byte[] makeClazz() {
    // Code generated the class below using asm.
    String clazzName = DeoptimizeOnExceptionTest.class.getName().replace('.', '/');
    final ClassWriter w = new ClassWriter(0);
    w.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "t/TestJSR", null, "java/lang/Object",
            new String[] { "java/lang/Runnable" });
    MethodVisitor mv = w.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[] {});
    mv.visitCode();/*  ww  w  .  j  a v a2 s.  c  o  m*/
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(10, 10);
    mv.visitEnd();

    mv = w.visitMethod(Opcodes.ACC_PUBLIC, "run", "()V", null, null);
    mv.visitCode();
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "getM", "()Ljava/lang/Object;", false);
    Label l1 = new Label();
    mv.visitJumpInsn(Opcodes.JSR, l1);
    mv.visitInsn(Opcodes.RETURN);

    mv.visitLabel(l1);
    mv.visitVarInsn(Opcodes.ASTORE, 1);

    Label lElse = new Label();
    Label lEnd = new Label();
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J", false);
    mv.visitInsn(Opcodes.POP2);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "getM", "()Ljava/lang/Object;", false);
    mv.visitInsn(Opcodes.DUP);
    mv.visitJumpInsn(Opcodes.IFNULL, lElse);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "methodA", "()V", false);
    mv.visitJumpInsn(Opcodes.GOTO, lEnd);
    mv.visitLabel(lElse);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "methodB", "()V", false);
    mv.visitLabel(lEnd);

    mv.visitVarInsn(Opcodes.RET, 1);
    mv.visitMaxs(10, 10);
    mv.visitEnd();
    return w.toByteArray();
}

From source file:org.gradle.api.internal.model.DefaultObjectFactory.java

License:Apache License

private ClassGeneratingLoader loaderFor(Class<?> publicClass) {
    ////from  ww  w  .  j a v  a  2s . c o  m
    // Generate implementation class
    //

    FormattingValidationProblemCollector problemCollector = new FormattingValidationProblemCollector(
            "Named implementation class", ModelType.of(publicClass));
    visitFields(publicClass, problemCollector);
    if (problemCollector.hasProblems()) {
        throw new GradleException(problemCollector.format());
    }

    AsmClassGenerator generator = new AsmClassGenerator(publicClass, "$Impl");
    Type implementationType = generator.getGeneratedType();
    ClassWriter visitor = generator.getVisitor();
    Type publicType = Type.getType(publicClass);

    Type superClass;
    String[] interfaces;
    if (publicClass.isInterface()) {
        superClass = OBJECT;
        interfaces = new String[] { publicType.getInternalName() };
    } else {
        superClass = publicType;
        interfaces = EMPTY_STRINGS;
    }

    visitor.visit(V1_5, ACC_PUBLIC | ACC_SYNTHETIC, implementationType.getInternalName(), null,
            superClass.getInternalName(), interfaces);

    //
    // Add name field
    //

    visitor.visitField(ACC_PRIVATE, NAME_FIELD, STRING.getDescriptor(), null, null);

    //
    // Add constructor
    //

    MethodVisitor methodVisitor = visitor.visitMethod(ACC_PUBLIC, CONSTRUCTOR_NAME, RETURN_VOID_FROM_STRING,
            null, EMPTY_STRINGS);
    methodVisitor.visitCode();
    // Call this.super()
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superClass.getInternalName(), CONSTRUCTOR_NAME,
            RETURN_VOID, false);
    // Set this.name = param1
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
    methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, implementationType.getInternalName(), NAME_FIELD,
            STRING.getDescriptor());
    // Done
    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    //
    // Add `getName()`
    //

    methodVisitor = visitor.visitMethod(ACC_PUBLIC, "getName", RETURN_STRING, null, EMPTY_STRINGS);
    methodVisitor.visitCode();
    // return this.name
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitFieldInsn(Opcodes.GETFIELD, implementationType.getInternalName(), NAME_FIELD,
            STRING.getDescriptor());
    methodVisitor.visitInsn(Opcodes.ARETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    //
    // Add `toString()`
    //

    methodVisitor = visitor.visitMethod(ACC_PUBLIC, "toString", RETURN_STRING, null, EMPTY_STRINGS);
    methodVisitor.visitCode();
    // return this.name
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitFieldInsn(Opcodes.GETFIELD, implementationType.getInternalName(), NAME_FIELD,
            STRING.getDescriptor());
    methodVisitor.visitInsn(Opcodes.ARETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    visitor.visitEnd();
    generator.define();

    //
    // Generate factory class
    //

    generator = new AsmClassGenerator(publicClass, "$Factory");
    visitor = generator.getVisitor();
    visitor.visit(V1_5, ACC_PUBLIC | ACC_SYNTHETIC, generator.getGeneratedType().getInternalName(), null,
            Type.getType(ClassGeneratingLoader.class).getInternalName(), EMPTY_STRINGS);

    //
    // Add constructor
    //

    methodVisitor = visitor.visitMethod(ACC_PUBLIC, CONSTRUCTOR_NAME, RETURN_VOID, null, EMPTY_STRINGS);
    methodVisitor.visitCode();
    // Call this.super()
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL,
            Type.getType(ClassGeneratingLoader.class).getInternalName(), CONSTRUCTOR_NAME, RETURN_VOID, false);
    // Done
    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    //
    // Add factory method
    //

    methodVisitor = visitor.visitMethod(ACC_PUBLIC, "load", Type.getMethodDescriptor(OBJECT, STRING), null,
            EMPTY_STRINGS);
    methodVisitor.visitCode();
    // Call return new <implClass>(param1)
    methodVisitor.visitTypeInsn(Opcodes.NEW, implementationType.getInternalName());
    methodVisitor.visitInsn(Opcodes.DUP);
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, implementationType.getInternalName(), CONSTRUCTOR_NAME,
            RETURN_VOID_FROM_STRING, false);
    methodVisitor.visitInsn(Opcodes.ARETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    visitor.visitEnd();
    Class<Object> factoryClass = generator.define();
    try {
        return (ClassGeneratingLoader) (factoryClass.newInstance());
    } catch (Exception e) {
        throw UncheckedException.throwAsUncheckedException(e);
    }
}