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

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

Introduction

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

Prototype

public void iinc(final int local, final int amount) 

Source Link

Document

Generates the instruction to increment the given local variable.

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 w w w .ja  v a 2s  .  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:com.changingbits.Builder.java

License:Apache License

private void buildAsm(GeneratorAdapter gen, Node node, int uptoLocal) {

    if (node.outputs != null) {
        //System.out.println("gen outputs=" + node.outputs);
        // Increment any range outputs at the current node:
        for (int range : node.outputs) {
            // Load arg 1 (the int[] answers):
            gen.loadArg(1);//  www . j  a v a  2s. c  o  m
            // Load the index we will store to
            gen.loadLocal(uptoLocal, Type.INT_TYPE);
            // The range value we will store:
            gen.push(range);
            // Store it
            gen.arrayStore(Type.INT_TYPE);
            // Increment our upto:
            gen.iinc(uptoLocal, 1);
        }
    }

    if (node.left != null && (node.left.hasOutputs || node.right.hasOutputs)) {
        assert node.left.end + 1 == node.right.start;
        if (node.left.hasOutputs && node.right.hasOutputs) {
            // Recurse on either left or right
            Label labelLeft = new Label();
            Label labelEnd = new Label();
            gen.loadArg(0);
            gen.push(node.left.end);

            gen.ifCmp(Type.LONG_TYPE, GeneratorAdapter.LE, labelLeft);
            buildAsm(gen, node.right, uptoLocal);
            gen.goTo(labelEnd);
            gen.visitLabel(labelLeft);
            buildAsm(gen, node.left, uptoLocal);
            gen.visitLabel(labelEnd);
        } else if (node.left.hasOutputs) {
            // Recurse only on left
            Label labelEnd = new Label();
            gen.loadArg(0);
            gen.push(node.left.end);

            gen.ifCmp(Type.LONG_TYPE, GeneratorAdapter.GT, labelEnd);
            buildAsm(gen, node.left, uptoLocal);
            gen.visitLabel(labelEnd);
        } else {
            // Recurse only on right
            Label labelEnd = new Label();
            gen.loadArg(0);
            gen.push(node.left.end);

            gen.ifCmp(Type.LONG_TYPE, GeneratorAdapter.LE, labelEnd);
            buildAsm(gen, node.right, uptoLocal);
            gen.visitLabel(labelEnd);
        }
    }
}

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  ww. ja  v a2 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:lucee.transformer.bytecode.statement.tag.TagLoop.java

License:Open Source License

/**
 * write out file loop//  w w  w .  j a v  a  2  s  .  c om
 * @param adapter
 * @throws TemplateException
 */
private void writeOutTypeFile(BytecodeContext bc) throws BytecodeException {
    WhileVisitor whileVisitor = new WhileVisitor();
    loopVisitor = whileVisitor;
    GeneratorAdapter adapter = bc.getAdapter();

    // charset=@charset
    int charset = adapter.newLocal(Types.STRING);
    Attribute attrCharset = getAttribute("charset");
    if (attrCharset == null)
        adapter.visitInsn(Opcodes.ACONST_NULL);
    else
        attrCharset.getValue().writeOut(bc, Expression.MODE_REF);
    adapter.storeLocal(charset);

    // startline=@startline
    int startline = adapter.newLocal(Types.INT_VALUE);
    Attribute attrStartLine = getAttribute("startline");
    if (attrStartLine == null)
        attrStartLine = getAttribute("from"); // CF8
    if (attrStartLine == null)
        adapter.push(1);
    else {
        attrStartLine.getValue().writeOut(bc, Expression.MODE_VALUE);
        adapter.visitInsn(Opcodes.D2I);
    }
    adapter.storeLocal(startline);

    // endline=@endline
    int endline = adapter.newLocal(Types.INT_VALUE);
    Attribute attrEndLine = getAttribute("endline");
    if (attrEndLine == null)
        attrEndLine = getAttribute("to");
    if (attrEndLine == null)
        adapter.push(-1);
    else {
        attrEndLine.getValue().writeOut(bc, Expression.MODE_VALUE);
        adapter.visitInsn(Opcodes.D2I);
    }
    adapter.storeLocal(endline);

    //VariableReference index=VariableInterpreter.getVariableReference(pc,@index);
    int index = -1, item = -1;

    // item
    Attribute attrItem = getAttribute("item");
    if (attrItem != null) {
        item = adapter.newLocal(Types.VARIABLE_REFERENCE);
        adapter.loadArg(0);
        attrItem.getValue().writeOut(bc, Expression.MODE_REF);
        adapter.invokeStatic(Types.VARIABLE_INTERPRETER, GET_VARIABLE_REFERENCE);
        adapter.storeLocal(item);
    }

    // index
    Attribute attrIndex = getAttribute("index");
    if (attrIndex != null) {
        index = adapter.newLocal(Types.VARIABLE_REFERENCE);
        adapter.loadArg(0);
        attrIndex.getValue().writeOut(bc, Expression.MODE_REF);
        adapter.invokeStatic(Types.VARIABLE_INTERPRETER, GET_VARIABLE_REFERENCE);
        adapter.storeLocal(index);
    }

    //java.io.File file=FileUtil.toResourceExisting(pc,@file);
    int resource = adapter.newLocal(Types.RESOURCE);
    adapter.loadArg(0);
    getAttribute("file").getValue().writeOut(bc, Expression.MODE_REF);
    adapter.invokeStatic(RESOURCE_UTIL, TO_RESOURCE_EXISTING);
    adapter.storeLocal(resource);

    // pc.getConfig().getSecurityManager().checkFileLocation(resource);
    adapter.loadArg(0);
    adapter.invokeVirtual(Types.PAGE_CONTEXT, GET_CONFIG);
    adapter.invokeInterface(Types.CONFIG_WEB, GET_SECURITY_MANAGER);
    adapter.loadLocal(resource);
    adapter.invokeInterface(Types.SECURITY_MANAGER, CHECK_FILE_LOCATION);

    // char[] carr=new char[characters];
    Attribute attr = getAttribute("characters");
    int carr = -1;
    if (attr != null) {
        carr = adapter.newLocal(Types.CHAR_ARRAY);
        attr.getValue().writeOut(bc, Expression.MODE_VALUE);
        adapter.cast(Types.DOUBLE_VALUE, Types.INT_VALUE);
        adapter.newArray(Types.CHAR);
        adapter.storeLocal(carr);
    }

    // BufferedReader reader = IOUtil.getBufferedReader(resource,charset);
    final int br = adapter.newLocal(Types.BUFFERED_READER);
    adapter.loadLocal(resource);
    adapter.loadLocal(charset);
    adapter.invokeStatic(IO_UTIL, GET_BUFFERED_READER);
    adapter.storeLocal(br);

    // String line;
    int line = adapter.newLocal(Types.STRING);

    // int count=0;  
    int count = adapter.newLocal(Types.INT_VALUE);
    adapter.push(0);
    adapter.storeLocal(count);

    TryFinallyVisitor tfv = new TryFinallyVisitor(new OnFinally() {
        public void _writeOut(BytecodeContext bc) {
            bc.getAdapter().loadLocal(br);
            bc.getAdapter().invokeStatic(IO_UTIL, CLOSE_EL);
        }
    }, null);
    //TryFinallyVisitor tcfv=new TryFinallyVisitor();

    // try
    tfv.visitTryBegin(bc);
    //tcfv.visitTryBegin(bc);
    // while((line=br.readLine())!=null) { 
    //WhileVisitor wv=new WhileVisitor();
    whileVisitor.visitBeforeExpression(bc);
    DecisionObjectVisitor dv = new DecisionObjectVisitor();
    dv.visitBegin();
    if (attr != null) {
        // IOUtil.read(bufferedreader,12)
        adapter.loadLocal(br);
        adapter.loadLocal(carr);
        adapter.arrayLength();
        adapter.invokeStatic(Types.IOUTIL, READ);
    } else {
        // br.readLine()
        adapter.loadLocal(br);
        adapter.invokeVirtual(Types.BUFFERED_READER, READ_LINE);
    }
    adapter.dup();
    adapter.storeLocal(line);

    dv.visitNEQ();
    adapter.visitInsn(Opcodes.ACONST_NULL);
    dv.visitEnd(bc);

    whileVisitor.visitAfterExpressionBeforeBody(bc);
    //if(++count < startLine) continue; 
    DecisionIntVisitor dv2 = new DecisionIntVisitor();
    dv2.visitBegin();
    adapter.iinc(count, 1);
    adapter.loadLocal(count);
    dv2.visitLT();
    adapter.loadLocal(startline);
    dv2.visitEnd(bc);
    Label end = new Label();
    adapter.ifZCmp(Opcodes.IFEQ, end);
    whileVisitor.visitContinue(bc);
    adapter.visitLabel(end);

    // if(endLine!=-1 && count > endLine) break; 
    DecisionIntVisitor div = new DecisionIntVisitor();
    div.visitBegin();
    adapter.loadLocal(endline);
    div.visitNEQ();
    adapter.push(-1);
    div.visitEnd(bc);
    Label end2 = new Label();
    adapter.ifZCmp(Opcodes.IFEQ, end2);

    DecisionIntVisitor div2 = new DecisionIntVisitor();
    div2.visitBegin();
    adapter.loadLocal(count);
    div2.visitGT();
    adapter.loadLocal(endline);
    div2.visitEnd(bc);
    Label end3 = new Label();
    adapter.ifZCmp(Opcodes.IFEQ, end3);
    whileVisitor.visitBreak(bc);
    adapter.visitLabel(end3);
    adapter.visitLabel(end2);

    // index and item
    if (index != -1 && item != -1) {
        // index.set(pc,line); 
        adapter.loadLocal(index);
        adapter.loadArg(0);
        adapter.loadLocal(count);
        adapter.cast(Types.INT_VALUE, Types.DOUBLE_VALUE);
        adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_DOUBLE_FROM_DOUBLE);

        adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
        adapter.pop();

        // item.set(pc,line); 
        adapter.loadLocal(item);
        adapter.loadArg(0);
        adapter.loadLocal(line);
        adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
        adapter.pop();

    }
    // only index
    else if (index != -1) {
        // index.set(pc,line); 
        adapter.loadLocal(index);
        adapter.loadArg(0);
        adapter.loadLocal(line);
        adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
        adapter.pop();

    }
    // only item
    else {
        // item.set(pc,line); 
        adapter.loadLocal(item);
        adapter.loadArg(0);
        adapter.loadLocal(line);
        adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
        adapter.pop();
    }

    getBody().writeOut(bc);

    whileVisitor.visitAfterBody(bc, getEnd());

    tfv.visitTryEnd(bc);

}

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

License:Open Source License

private int compileInitFromArray(final SubSectionCompiler sub, final GeneratorAdapter mv, final int l_ds) {
    // final int dl = ds.length;
    final int l_dl = mv.newLocal(Type.INT_TYPE);
    mv.loadLocal(l_ds);//from www  .j a v a2 s.c om
    mv.arrayLength();
    mv.storeLocal(l_dl);

    // DetailImpl[] di = new DetailPrototype[ dl ];
    final int l_di = mv.newLocal(sub.arrayType());
    mv.loadLocal(l_dl);
    mv.newArray(sub.classType());
    mv.storeLocal(l_di);

    // for (int i = 0; i < dl; i++) {
    final int l_i = mv.newLocal(Type.INT_TYPE);
    final Label next = mv.newLabel();
    mv.push(0);
    mv.storeLocal(l_i);
    mv.goTo(next);
    final Label again = mv.mark();

    // ~ di[ i ] = new DetailPrototype( ds[ i ], this );
    mv.loadLocal(l_di);
    mv.loadLocal(l_i);
    mv.newInstance(sub.classType());
    mv.dup();
    mv.loadLocal(l_ds);
    mv.loadLocal(l_i);
    mv.arrayLoad(sub.inputType());
    final IndexCompiler ic = sub.isComputationListenerEnabled() ? new IndexCompiler(l_i) : null;
    compileInit(mv, sub, ic);
    mv.arrayStore(sub.classType());

    // }
    mv.iinc(l_i, 1);
    mv.mark(next);
    mv.loadLocal(l_i);
    mv.loadLocal(l_dl);
    mv.ifCmp(Type.INT_TYPE, mv.LT, again);

    return l_di;
}

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

License:Open Source License

private int compileInitFromCollection(final SubSectionCompiler sub, final GeneratorAdapter mv, final int l_dc) {
    final String n_iter = ITERATOR_INTF.getInternalName();
    final String n_coll = COLLECTION_INTF.getInternalName();

    // final int dl = dc.size();
    final int l_dl = mv.newLocal(Type.INT_TYPE);
    mv.loadLocal(l_dc);/*from  w  ww . ja  va 2 s .  c  o  m*/
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, n_coll, "size", "()I");
    mv.storeLocal(l_dl);

    // final Iterator<DetailInput> ds = dc.iterator();
    final int l_ds = mv.newLocal(ITERATOR_INTF);
    mv.loadLocal(l_dc);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, n_coll, "iterator", "()" + ITERATOR_INTF.getDescriptor());
    mv.storeLocal(l_ds);

    // DetailImpl[] di = new DetailPrototype[ dl ];
    final int l_di = mv.newLocal(sub.arrayType());
    mv.loadLocal(l_dl);
    mv.newArray(sub.classType());
    mv.storeLocal(l_di);

    // for (int i = 0; i < dl; i++) {
    final int l_i = mv.newLocal(Type.INT_TYPE);
    final Label next = mv.newLabel();
    mv.push(0);
    mv.storeLocal(l_i);
    mv.goTo(next);
    final Label again = mv.mark();

    // ~ di[ i ] = new DetailPrototype( ds.next(), this );
    mv.loadLocal(l_di);
    mv.loadLocal(l_i);
    mv.newInstance(sub.classType());
    mv.dup();
    mv.loadLocal(l_ds);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, n_iter, "next", "()Ljava/lang/Object;");
    mv.checkCast(sub.inputType());
    final IndexCompiler ic = sub.isComputationListenerEnabled() ? new IndexCompiler(l_i) : null;
    compileInit(mv, sub, ic);
    mv.arrayStore(sub.classType());

    // }
    mv.iinc(l_i, 1);
    mv.mark(next);
    mv.loadLocal(l_i);
    mv.loadLocal(l_dl);
    mv.ifCmp(Type.INT_TYPE, mv.LT, again);

    return l_di;
}

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

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from w  ww  . j ava 2 s . c o m
protected void compileBody() throws CompilerException {
    final SubSectionCompiler sub = this.sub;
    final GeneratorAdapter mv = mv();

    final CallFrame outputCall = this.callToImplement;
    final Class outputContainerClass = outputCall.getMethod().getReturnType();

    // get$Sect0()
    mv.loadThis();
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, section().classInternalName(), sub.getterName(),
            sub.getterDescriptor());

    if (outputContainerClass.isArray()) {
        mv.visitInsn(Opcodes.ARETURN);
    } else {
        // Detail[] arr = get$Sect0();
        final int l_arr = mv.newLocal(sub.arrayType());
        mv.storeLocal(l_arr);

        final int l_len = mv.newLocal(Type.INT_TYPE);
        mv.loadLocal(l_arr);
        mv.arrayLength();
        mv.storeLocal(l_len);

        // List lst = new ArrayList( arr.length );
        final int l_lst = mv.newLocal(ARRAYLIST_CLASS);
        mv.newInstance(ARRAYLIST_CLASS);
        mv.dup();
        mv.loadLocal(l_len);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, ARRAYLIST_CLASS.getInternalName(), "<init>", "(I)V");
        mv.storeLocal(l_lst);

        // for (int i = 0; i < len; i++) {
        final int l_i = mv.newLocal(Type.INT_TYPE);
        mv.push(0);
        mv.storeLocal(l_i);
        final Label test = mv.newLabel();
        mv.goTo(test);
        final Label again = mv.mark();

        // lst.add( arr[ i ] );
        mv.loadLocal(l_lst);
        mv.loadLocal(l_arr);
        mv.loadLocal(l_i);
        mv.arrayLoad(sub.classType());
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ARRAYLIST_CLASS.getInternalName(), "add",
                "(Ljava/lang/Object;)Z");
        mv.pop();

        // } // for
        mv.iinc(l_i, 1);
        mv.mark(test);
        mv.loadLocal(l_i);
        mv.loadLocal(l_len);
        mv.ifCmp(Type.INT_TYPE, mv.LT, again);

        mv.loadLocal(l_lst);
        if (outputContainerClass.isAssignableFrom(List.class)) {
            // return lst;
            mv.visitInsn(Opcodes.ARETURN);
        } else if (outputContainerClass.isAssignableFrom(Iterator.class)) {
            // return lst.iterator();
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ARRAYLIST_CLASS.getInternalName(), "iterator",
                    "()" + ITERATOR_INTF.getDescriptor());
            mv.visitInsn(Opcodes.ARETURN);
        } else {
            throw new CompilerException.UnsupportedDataType("The return type of '" + outputCall.getMethod()
                    + "' is not supported as input to a repeating section.");
        }
    }
}