Example usage for org.objectweb.asm.commons GeneratorAdapter ifICmp

List of usage examples for org.objectweb.asm.commons GeneratorAdapter ifICmp

Introduction

In this page you can find the example usage for org.objectweb.asm.commons GeneratorAdapter ifICmp.

Prototype

public void ifICmp(final int mode, final Label label) 

Source Link

Document

Generates the instructions to jump to a label based on the comparison of the top two integer stack values.

Usage

From source file:co.cask.cdap.internal.io.DatumWriterGenerator.java

License:Apache License

/**
 * Generates method body for encoding array value. The logic is similar to the one in
 * {@link #encodeCollection}, with collection size replaced with array length.
 *
 * @param mg/*from ww w .  j  av a 2 s.c  o m*/
 * @param componentType
 * @param componentSchema
 * @param value
 * @param encoder
 * @param schemaLocal
 * @param seenRefs
 */
private void encodeArray(GeneratorAdapter mg, TypeToken<?> componentType, Schema componentSchema, int value,
        int encoder, int schemaLocal, int seenRefs) {
    // Encode and store the array length locally
    mg.loadArg(value);
    mg.arrayLength();
    int length = mg.newLocal(Type.INT_TYPE);
    mg.storeLocal(length);

    mg.loadArg(encoder);
    mg.loadLocal(length);
    mg.invokeInterface(Type.getType(Encoder.class), getMethod(Encoder.class, "writeInt", int.class));
    mg.pop();

    // Store the component schema
    mg.loadArg(schemaLocal);
    mg.invokeVirtual(Type.getType(Schema.class), getMethod(Schema.class, "getComponentSchema"));
    int componentSchemaLocal = mg.newLocal(Type.getType(Schema.class));
    mg.storeLocal(componentSchemaLocal);

    // for (int idx = 0; idx < array.length; idx++)
    mg.push(0);
    int idx = mg.newLocal(Type.INT_TYPE);
    mg.storeLocal(idx);
    Label beginFor = mg.mark();
    Label endFor = mg.newLabel();
    mg.loadLocal(idx);
    mg.loadLocal(length);
    mg.ifICmp(GeneratorAdapter.GE, endFor);

    // Call encode method to encode array[idx]
    mg.loadThis();
    mg.loadArg(value);
    mg.loadLocal(idx);
    TypeToken<?> callTypeToken = getCallTypeToken(componentType, componentSchema);
    mg.arrayLoad(Type.getType(callTypeToken.getRawType()));
    mg.loadArg(encoder);
    mg.loadLocal(componentSchemaLocal);
    mg.loadArg(seenRefs);
    mg.invokeVirtual(classType, getEncodeMethod(componentType, componentSchema));

    mg.iinc(idx, 1);
    mg.goTo(beginFor);
    mg.mark(endFor);

    // if length > 0, write out 0 at the end of array.
    Label zeroLength = mg.newLabel();
    mg.loadLocal(length);
    mg.ifZCmp(GeneratorAdapter.LE, zeroLength);
    encodeInt(mg, 0, encoder);
    mg.mark(zeroLength);
}

From source file:de.enough.polish.postcompile.java5.Java5ClassVisitor.java

License:Open Source License

public void visitEnd() {
    if (this.isEnumClass) {
        if (this.name_values == null) {
            throw new BuildException("This is not an enum class: " + this.classDesc);
        }/* w w w.j a  v a  2 s  .  co  m*/

        // Generate new <clinit> method.
        int numValues = EnumManager.getInstance().getNumEnumValues(this.classDesc);
        Method m = Method.getMethod("void <clinit> ()");
        MethodVisitor mv = super.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
        GeneratorAdapter mg = new GeneratorAdapter(ACC_STATIC, m, mv);
        mg.push(numValues);
        mg.newArray(Type.INT_TYPE);

        if (numValues <= 3) {
            for (int i = 1; i < numValues; i++) {
                mg.dup();
                mg.push(i);
                mg.push(i);
                mg.arrayStore(Type.INT_TYPE);
            }
        } else {
            Label labelInitializeField = new Label();
            Label labelCheck = new Label();
            Label labelDone = new Label();

            mg.push(1);
            mg.storeLocal(0, Type.INT_TYPE);
            mg.goTo(labelCheck);

            mg.mark(labelInitializeField);
            mg.dup();
            mg.loadLocal(0, Type.INT_TYPE);
            mg.dup();
            mg.arrayStore(Type.INT_TYPE);
            mg.iinc(0, 1);

            mg.mark(labelCheck);
            mg.loadLocal(0, Type.INT_TYPE);
            mg.push(numValues);
            mg.ifICmp(GeneratorAdapter.LT, labelInitializeField);

            mg.mark(labelDone);
        }

        mg.putStatic(Type.getType(this.classDesc), this.name_values, Type.getType(int[].class));
        mg.returnValue();
        mg.endMethod();
    }

    // Called super implementation of this method to really close this class.
    super.visitEnd();
}

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  v a 2 s. c  om*/

    // 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.LinearizerCompiler.java

License:Open Source License

@Override
protected void compileBody() throws CompilerException {
    final GeneratorAdapter mv = mv();
    final Label outOfRange = mv.newLabel();

    // range check row
    mv.visitVarInsn(Opcodes.ILOAD, 1); // row
    mv.push(1);//from  ww  w  .  j  a va  2  s  . co m
    mv.ifICmp(mv.LT, outOfRange);
    mv.visitVarInsn(Opcodes.ILOAD, 1); // row
    mv.push(this.rows);
    mv.ifICmp(mv.GT, outOfRange);

    // range check col
    mv.visitVarInsn(Opcodes.ILOAD, 2); // col
    mv.push(1);
    mv.ifICmp(mv.LT, outOfRange);
    mv.visitVarInsn(Opcodes.ILOAD, 2); // col
    mv.push(this.cols);
    mv.ifICmp(mv.GT, outOfRange);

    // (<row> - 1) * <num_cols>) + (<col> - 1);
    mv.visitVarInsn(Opcodes.ILOAD, 1); // row
    mv.push(1);
    mv.visitInsn(Opcodes.ISUB);
    mv.push(this.cols);
    mv.visitInsn(Opcodes.IMUL);
    mv.visitVarInsn(Opcodes.ILOAD, 2); // col
    mv.push(1);
    mv.visitInsn(Opcodes.ISUB);
    mv.visitInsn(Opcodes.IADD);
    mv.visitInsn(Opcodes.IRETURN);

    mv.visitLabel(outOfRange);
    mv.throwException(ExpressionCompiler.FORMULA_ERROR_TYPE,
            "#VALUE/REF! because index is out of range in INDEX");
}