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

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

Introduction

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

Prototype

public void ifCmp(final Type type, 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 stack values.

Usage

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);//  w  w  w.j  a  va2  s .co  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:com.changingbits.Builder.java

License:Apache License

/** Increments counts as field members (count0, count1,
 *  ...) instead of a this.intArray[0], ... */
private void buildCounterAsm2(GeneratorAdapter gen, Node node, boolean sawOutputs) {

    sawOutputs |= node.outputs != null;//from w  w  w  .j a  va 2 s.  c  o m

    if (node.left != null) {
        assert node.left.end + 1 == node.right.start;
        // 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);
        buildCounterAsm2(gen, node.right, sawOutputs);
        gen.goTo(labelEnd);
        gen.visitLabel(labelLeft);
        buildCounterAsm2(gen, node.left, sawOutputs);
        gen.visitLabel(labelEnd);
    } else if (sawOutputs) {
        // leaf: this.countN++
        gen.loadThis();
        gen.loadThis();
        gen.getField(COMPILED_COUNTER_CLASS2_TYPE, "count" + node.leafIndex, Type.INT_TYPE);
        gen.push(1);
        gen.visitInsn(Opcodes.IADD);
        gen.putField(COMPILED_COUNTER_CLASS2_TYPE, "count" + node.leafIndex, Type.INT_TYPE);
    }
}

From source file:com.changingbits.Builder.java

License:Apache License

private void buildCounterAsm(GeneratorAdapter gen, Node node, boolean sawOutputs) {

    sawOutputs |= node.outputs != null;/*  ww w  .  j a  v a2s.  c  om*/

    if (node.left != null) {
        assert node.left.end + 1 == node.right.start;
        // 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);
        buildCounterAsm(gen, node.right, sawOutputs);
        gen.goTo(labelEnd);
        gen.visitLabel(labelLeft);
        buildCounterAsm(gen, node.left, sawOutputs);
        gen.visitLabel(labelEnd);
    } else if (sawOutputs) {
        // leaf: elementaryCounts[node.leafIndex]++
        gen.loadThis();
        gen.getField(BASE_LONG_RANGE_COUNTER_TYPE, "elementaryCounts", INT_ARRAY_TYPE);
        gen.push(node.leafIndex);
        gen.dup2();
        gen.arrayLoad(Type.INT_TYPE);
        gen.push(1);
        gen.visitInsn(Opcodes.IADD);
        gen.arrayStore(Type.INT_TYPE);
    }
}

From source file:io.datakernel.codegen.ExpressionFor.java

License:Apache License

@Override
public Type load(Context ctx) {
    GeneratorAdapter g = ctx.getGeneratorAdapter();
    Label labelLoop = new Label();
    Label labelExit = new Label();

    VarLocal len = newLocal(ctx, Type.INT_TYPE);
    add(length, start).load(ctx);//from  w  ww.  j av  a  2  s. c  om
    len.store(ctx);

    start.load(ctx);
    VarLocal varPosition = newLocal(ctx, Type.INT_TYPE);
    varPosition.store(ctx);

    g.mark(labelLoop);

    varPosition.load(ctx);
    len.load(ctx);

    g.ifCmp(Type.INT_TYPE, GeneratorAdapter.GE, labelExit);

    this.forVar.forVar(varPosition).load(ctx);

    varPosition.load(ctx);
    g.push(1);
    g.math(GeneratorAdapter.ADD, Type.INT_TYPE);
    varPosition.store(ctx);

    g.goTo(labelLoop);
    g.mark(labelExit);

    return Type.VOID_TYPE;
}

From source file:io.datakernel.codegen.ExpressionIf.java

License:Apache License

@Override
public Type load(Context ctx) {
    Label labelTrue = new Label();
    Label labelExit = new Label();

    GeneratorAdapter g = ctx.getGeneratorAdapter();
    condition.load(ctx);/*from   w  w w.j a v a 2s .c o m*/
    g.push(true);

    g.ifCmp(condition.type(ctx), GeneratorAdapter.EQ, labelTrue);

    if (right != null) {
        right.load(ctx);
    }

    g.goTo(labelExit);

    g.mark(labelTrue);
    left.load(ctx);

    g.mark(labelExit);
    return left.type(ctx);
}

From source file:io.datakernel.codegen.ExpressionIteratorForEach.java

License:Apache License

@Override
public Type load(Context ctx) {
    GeneratorAdapter g = ctx.getGeneratorAdapter();
    Label labelLoop = new Label();
    Label labelExit = new Label();

    if (collection.type(ctx).getSort() == Type.ARRAY) {
        expressionFor(length(collection), new ForVar() {
            @Override/*from w  ww .  j  a va 2s  .c  o m*/
            public Expression forVar(Expression it) {
                return forCollection.forVar(getArrayItem(collection, it));
            }
        }).load(ctx);
        return Type.VOID_TYPE;
    }

    VarLocal varIter = newLocal(ctx, getType(Iterator.class));

    Class<?> t = tryGetJavaType(collection.type(ctx));
    if (t.isInstance(Iterator.class) || t == Iterator.class) {
        collection.load(ctx);
        varIter.store(ctx);
    } else {
        call(collection, "iterator").load(ctx);
        varIter.store(ctx);

    }

    g.mark(labelLoop);

    call(varIter, "hasNext").load(ctx);
    g.push(false);
    g.ifCmp(Type.BOOLEAN_TYPE, GeneratorAdapter.EQ, labelExit);

    cast(call(varIter, "next"), type).load(ctx);
    VarLocal varKey = newLocal(ctx, getType(type));
    varKey.store(ctx);

    forCollection.forVar(varKey).load(ctx);

    g.goTo(labelLoop);
    g.mark(labelExit);
    return Type.VOID_TYPE;
}

From source file:io.datakernel.codegen.ExpressionMapForEach.java

License:Apache License

@Override
public Type load(Context ctx) {
    GeneratorAdapter g = ctx.getGeneratorAdapter();
    Label labelLoop = new Label();
    Label labelExit = new Label();

    call(call(field, "entrySet"), "iterator").load(ctx);
    VarLocal varIter = newLocal(ctx, Type.getType(Iterator.class));
    varIter.store(ctx);//from   w ww.  jav a 2 s.  c o m

    g.mark(labelLoop);

    call(varIter, "hasNext").load(ctx);
    g.push(false);
    g.ifCmp(Type.BOOLEAN_TYPE, GeneratorAdapter.EQ, labelExit);

    cast(call(varIter, "next"), Map.Entry.class).load(ctx);
    VarLocal varEntry = newLocal(ctx, Type.getType(Map.Entry.class));

    varEntry.store(ctx);

    forKey.forVar(call(varEntry, "getKey")).load(ctx);
    forValue.forVar(call(varEntry, "getValue")).load(ctx);

    g.goTo(labelLoop);
    g.mark(labelExit);
    return Type.VOID_TYPE;
}

From source file:io.datakernel.codegen.ExpressionNeg.java

License:Apache License

@Override
public Type load(Context ctx) {
    GeneratorAdapter g = ctx.getGeneratorAdapter();
    int sort = arg.type(ctx).getSort();
    arg.load(ctx);/*w  w w  . j a  v  a2  s . com*/

    if (sort == Type.DOUBLE || sort == Type.FLOAT || sort == Type.LONG || sort == Type.INT) {
        g.math(GeneratorAdapter.NEG, arg.type(ctx));
        return arg.type(ctx);
    }
    if (sort == Type.BYTE || sort == Type.SHORT || sort == Type.CHAR) {
        g.cast(arg.type(ctx), INT_TYPE);
        g.math(GeneratorAdapter.NEG, INT_TYPE);
        return arg.type(ctx);
    }

    if (sort == Type.BOOLEAN) {
        Label labelTrue = new Label();
        Label labelExit = new Label();
        g.push(true);
        g.ifCmp(BOOLEAN_TYPE, GeneratorAdapter.EQ, labelTrue);
        g.push(true);
        g.goTo(labelExit);

        g.mark(labelTrue);
        g.push(false);

        g.mark(labelExit);
        return INT_TYPE;
    }

    throw new RuntimeException(format("%s is not primitive. %s",
            getJavaType(ctx.getClassLoader(), arg.type(ctx)), exceptionInGeneratedClass(ctx)));
}

From source file:io.datakernel.codegen.ExpressionSwitch.java

License:Apache License

@Override
public Type load(Context ctx) {
    VarLocal varReadedSubClass = newLocal(ctx, nom.type(ctx));
    nom.load(ctx);//from  w  ww. j av a 2  s  .c om
    varReadedSubClass.store(ctx);

    Label labelExit = new Label();
    GeneratorAdapter g = ctx.getGeneratorAdapter();

    for (int i = 0; i < list.size(); i++) {
        Label labelNext = new Label();

        g.push(i);
        varReadedSubClass.load(ctx);
        g.ifCmp(INT_TYPE, GeneratorAdapter.NE, labelNext);

        list.get(i).load(ctx);
        g.goTo(labelExit);

        g.mark(labelNext);
    }

    if (defaultExp != null) {
        defaultExp.load(ctx);
    } else {
        final Variable sb = let(constructor(StringBuilder.class));
        call(sb, "append", value("Key '")).load(ctx);
        call(sb, "append", cast(varReadedSubClass, getType(int.class))).load(ctx);
        call(sb, "append", value(String.format("' not in range [0-%d)", list.size()))).load(ctx);
        constructor(IllegalArgumentException.class, call(sb, "toString")).load(ctx);
        g.throwException();
    }
    g.mark(labelExit);

    return type(ctx);
}

From source file:io.datakernel.codegen.ExpressionSwitchForKey.java

License:Apache License

@Override
public Type load(Context ctx) {
    VarLocal varKey = newLocal(ctx, key.type(ctx));
    key.load(ctx);//  w  w  w  .ja v a 2 s  .  c  o m
    varKey.store(ctx);

    Label labelExit = new Label();
    GeneratorAdapter g = ctx.getGeneratorAdapter();

    final boolean keyPrimitiveType = Utils.isPrimitiveType(key.type(ctx));
    for (int i = 0; i < listKey.size(); i++) {
        Label labelNext = new Label();
        if (keyPrimitiveType) {
            listKey.get(i).load(ctx);
            varKey.load(ctx);
            g.ifCmp(key.type(ctx), GeneratorAdapter.NE, labelNext);
        } else {
            call(listKey.get(i), "equals", varKey).load(ctx);
            g.push(true);
            g.ifCmp(Type.BOOLEAN_TYPE, GeneratorAdapter.NE, labelNext);
        }

        listValue.get(i).load(ctx);
        g.goTo(labelExit);

        g.mark(labelNext);
    }

    if (defaultExp != null) {
        defaultExp.load(ctx);
    } else {
        final Variable sb = let(constructor(StringBuilder.class));
        call(sb, "append", value("Key '")).load(ctx);
        call(sb, "append", keyPrimitiveType ? varKey : call(key, "toString")).load(ctx);
        call(sb, "append", value("' not in keyList: [")).load(ctx);
        final Iterator<Expression> iterator = listKey.iterator();
        while (iterator.hasNext()) {
            final Expression expression = iterator.next();
            final boolean primitiveType = Utils.isPrimitiveType(expression.type(ctx));
            call(sb, "append", primitiveType ? expression : call(expression, "toString")).load(ctx);
            if (iterator.hasNext()) {
                call(sb, "append", value(", ")).load(ctx);
            }
        }
        call(sb, "append", value("]")).load(ctx);

        constructor(IllegalArgumentException.class, call(sb, "toString")).load(ctx);
        g.throwException();
    }
    g.mark(labelExit);

    return type(ctx);
}