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

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

Introduction

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

Prototype

int GT

To view the source code for org.objectweb.asm.commons GeneratorAdapter GT.

Click Source Link

Document

Constant for the #ifCmp method.

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);/*from  ww  w.j  a v  a  2 s.  c  om*/
            // 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.xruby.compiler.codegen.RubyCompilerImpl.java

License:BSD License

public Object visitMethodDefinitionDefaultParameterBegin(int index) {
    Label next_label = new Label();

    MethodGenerator mg = cg_.getMethodGenerator();
    mg.loadMethodPrameterLength();//from   w ww. ja  v a2  s . c om
    mg.push(index);
    mg.ifICmp(GeneratorAdapter.GT, next_label);

    mg.loadArg(1);

    return next_label;
}

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

License:Apache License

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

    Type leftFieldType = left.type(ctx);
    Preconditions.check(leftFieldType.equals(right.type(ctx)));
    left.load(ctx);/*w ww  .j a  va  2  s  .c o  m*/
    right.load(ctx);

    if (isPrimitiveType(leftFieldType)) {
        g.ifCmp(leftFieldType, operation.opCode, labelTrue);
    } else {
        if (operation == EQ || operation == NE) {
            g.invokeVirtual(leftFieldType,
                    new Method("equals", BOOLEAN_TYPE, new Type[] { Type.getType(Object.class) }));
            g.push(operation == EQ);
            g.ifCmp(BOOLEAN_TYPE, GeneratorAdapter.EQ, labelTrue);
        } else {
            g.invokeVirtual(leftFieldType,
                    new Method("compareTo", INT_TYPE, new Type[] { Type.getType(Object.class) }));
            if (operation == LT) {
                g.ifZCmp(GeneratorAdapter.LT, labelTrue);
            } else if (operation == GT) {
                g.ifZCmp(GeneratorAdapter.GT, labelTrue);
            } else if (operation == LE) {
                g.ifZCmp(GeneratorAdapter.LE, labelTrue);
            } else if (operation == GE) {
                g.ifZCmp(GeneratorAdapter.GE, labelTrue);
            }
        }
    }

    g.push(false);
    g.goTo(labelExit);

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

    g.mark(labelExit);

    return BOOLEAN_TYPE;
}

From source file:org.apache.lucene.expressions.js.JavascriptCompiler.java

License:Apache License

/**
 * Sends the bytecode of class file to {@link ClassWriter}.
 *//*from   ww w .j ava2 s. co  m*/
private void generateClass(final ParseTree parseTree, final ClassWriter classWriter,
        final Map<String, Integer> externalsMap) throws ParseException {
    classWriter.visit(CLASSFILE_VERSION, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL,
            COMPILED_EXPRESSION_INTERNAL, null, EXPRESSION_TYPE.getInternalName(), null);
    final String clippedSourceText = (sourceText.length() <= MAX_SOURCE_LENGTH) ? sourceText
            : (sourceText.substring(0, MAX_SOURCE_LENGTH - 3) + "...");
    classWriter.visitSource(clippedSourceText, null);

    final GeneratorAdapter constructor = new GeneratorAdapter(Opcodes.ACC_PUBLIC, EXPRESSION_CTOR, null, null,
            classWriter);
    constructor.loadThis();
    constructor.loadArgs();
    constructor.invokeConstructor(EXPRESSION_TYPE, EXPRESSION_CTOR);
    constructor.returnValue();
    constructor.endMethod();

    final GeneratorAdapter gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC, EVALUATE_METHOD, null, null,
            classWriter);

    // to completely hide the ANTLR visitor we use an anonymous impl:
    new JavascriptBaseVisitor<Void>() {
        private final Deque<Type> typeStack = new ArrayDeque<>();

        @Override
        public Void visitCompile(JavascriptParser.CompileContext ctx) {
            typeStack.push(Type.DOUBLE_TYPE);
            visit(ctx.expression());
            typeStack.pop();

            return null;
        }

        @Override
        public Void visitPrecedence(JavascriptParser.PrecedenceContext ctx) {
            visit(ctx.expression());

            return null;
        }

        @Override
        public Void visitNumeric(JavascriptParser.NumericContext ctx) {
            if (ctx.HEX() != null) {
                pushLong(Long.parseLong(ctx.HEX().getText().substring(2), 16));
            } else if (ctx.OCTAL() != null) {
                pushLong(Long.parseLong(ctx.OCTAL().getText().substring(1), 8));
            } else if (ctx.DECIMAL() != null) {
                gen.push(Double.parseDouble(ctx.DECIMAL().getText()));
                gen.cast(Type.DOUBLE_TYPE, typeStack.peek());
            } else {
                throw new IllegalStateException("Unknown operation specified: " + ctx.getText());
            }

            return null;
        }

        @Override
        public Void visitExternal(JavascriptParser.ExternalContext ctx) {
            String text = ctx.VARIABLE().getText();
            int arguments = ctx.expression().size();
            boolean parens = ctx.LP() != null && ctx.RP() != null;
            Method method = parens ? functions.get(text) : null;

            try {
                if (method != null) {
                    int arity = method.getParameterTypes().length;

                    if (arguments != arity) {
                        throw new ParseException("Invalid expression '" + sourceText + "': Expected (" + arity
                                + ") arguments for function call (" + text + "), but found (" + arguments
                                + ").", ctx.start.getStartIndex());
                    }

                    typeStack.push(Type.DOUBLE_TYPE);

                    for (int argument = 0; argument < arguments; ++argument) {
                        visit(ctx.expression(argument));
                    }

                    typeStack.pop();

                    gen.invokeStatic(Type.getType(method.getDeclaringClass()),
                            org.objectweb.asm.commons.Method.getMethod(method));

                    gen.cast(Type.DOUBLE_TYPE, typeStack.peek());
                } else if (!parens || arguments == 0 && text.contains(".")) {
                    int index;

                    text = normalizeQuotes(ctx.getText());

                    if (externalsMap.containsKey(text)) {
                        index = externalsMap.get(text);
                    } else {
                        index = externalsMap.size();
                        externalsMap.put(text, index);
                    }

                    gen.loadArg(0);
                    gen.push(index);
                    gen.arrayLoad(FUNCTION_VALUES_TYPE);
                    gen.invokeVirtual(FUNCTION_VALUES_TYPE, DOUBLE_VAL_METHOD);
                    gen.cast(Type.DOUBLE_TYPE, typeStack.peek());
                } else {
                    throw new ParseException("Invalid expression '" + sourceText
                            + "': Unrecognized function call (" + text + ").", ctx.start.getStartIndex());
                }
                return null;
            } catch (ParseException e) {
                // The API doesn't allow checked exceptions here, so propagate up the stack. This is unwrapped
                // in getAntlrParseTree. 
                throw new RuntimeException(e);
            }
        }

        @Override
        public Void visitUnary(JavascriptParser.UnaryContext ctx) {
            if (ctx.BOOLNOT() != null) {
                Label labelNotTrue = new Label();
                Label labelNotReturn = new Label();

                typeStack.push(Type.INT_TYPE);
                visit(ctx.expression());
                typeStack.pop();
                gen.visitJumpInsn(Opcodes.IFEQ, labelNotTrue);
                pushBoolean(false);
                gen.goTo(labelNotReturn);
                gen.visitLabel(labelNotTrue);
                pushBoolean(true);
                gen.visitLabel(labelNotReturn);

            } else if (ctx.BWNOT() != null) {
                typeStack.push(Type.LONG_TYPE);
                visit(ctx.expression());
                typeStack.pop();
                gen.push(-1L);
                gen.visitInsn(Opcodes.LXOR);
                gen.cast(Type.LONG_TYPE, typeStack.peek());

            } else if (ctx.ADD() != null) {
                visit(ctx.expression());

            } else if (ctx.SUB() != null) {
                typeStack.push(Type.DOUBLE_TYPE);
                visit(ctx.expression());
                typeStack.pop();
                gen.visitInsn(Opcodes.DNEG);
                gen.cast(Type.DOUBLE_TYPE, typeStack.peek());

            } else {
                throw new IllegalStateException("Unknown operation specified: " + ctx.getText());
            }

            return null;
        }

        @Override
        public Void visitMuldiv(JavascriptParser.MuldivContext ctx) {
            int opcode;

            if (ctx.MUL() != null) {
                opcode = Opcodes.DMUL;
            } else if (ctx.DIV() != null) {
                opcode = Opcodes.DDIV;
            } else if (ctx.REM() != null) {
                opcode = Opcodes.DREM;
            } else {
                throw new IllegalStateException("Unknown operation specified: " + ctx.getText());
            }

            pushArith(opcode, ctx.expression(0), ctx.expression(1));

            return null;
        }

        @Override
        public Void visitAddsub(JavascriptParser.AddsubContext ctx) {
            int opcode;

            if (ctx.ADD() != null) {
                opcode = Opcodes.DADD;
            } else if (ctx.SUB() != null) {
                opcode = Opcodes.DSUB;
            } else {
                throw new IllegalStateException("Unknown operation specified: " + ctx.getText());
            }

            pushArith(opcode, ctx.expression(0), ctx.expression(1));

            return null;
        }

        @Override
        public Void visitBwshift(JavascriptParser.BwshiftContext ctx) {
            int opcode;

            if (ctx.LSH() != null) {
                opcode = Opcodes.LSHL;
            } else if (ctx.RSH() != null) {
                opcode = Opcodes.LSHR;
            } else if (ctx.USH() != null) {
                opcode = Opcodes.LUSHR;
            } else {
                throw new IllegalStateException("Unknown operation specified: " + ctx.getText());
            }

            pushShift(opcode, ctx.expression(0), ctx.expression(1));

            return null;
        }

        @Override
        public Void visitBoolcomp(JavascriptParser.BoolcompContext ctx) {
            int opcode;

            if (ctx.LT() != null) {
                opcode = GeneratorAdapter.LT;
            } else if (ctx.LTE() != null) {
                opcode = GeneratorAdapter.LE;
            } else if (ctx.GT() != null) {
                opcode = GeneratorAdapter.GT;
            } else if (ctx.GTE() != null) {
                opcode = GeneratorAdapter.GE;
            } else {
                throw new IllegalStateException("Unknown operation specified: " + ctx.getText());
            }

            pushCond(opcode, ctx.expression(0), ctx.expression(1));

            return null;
        }

        @Override
        public Void visitBooleqne(JavascriptParser.BooleqneContext ctx) {
            int opcode;

            if (ctx.EQ() != null) {
                opcode = GeneratorAdapter.EQ;
            } else if (ctx.NE() != null) {
                opcode = GeneratorAdapter.NE;
            } else {
                throw new IllegalStateException("Unknown operation specified: " + ctx.getText());
            }

            pushCond(opcode, ctx.expression(0), ctx.expression(1));

            return null;
        }

        @Override
        public Void visitBwand(JavascriptParser.BwandContext ctx) {
            pushBitwise(Opcodes.LAND, ctx.expression(0), ctx.expression(1));

            return null;
        }

        @Override
        public Void visitBwxor(JavascriptParser.BwxorContext ctx) {
            pushBitwise(Opcodes.LXOR, ctx.expression(0), ctx.expression(1));

            return null;
        }

        @Override
        public Void visitBwor(JavascriptParser.BworContext ctx) {
            pushBitwise(Opcodes.LOR, ctx.expression(0), ctx.expression(1));

            return null;
        }

        @Override
        public Void visitBooland(JavascriptParser.BoolandContext ctx) {
            Label andFalse = new Label();
            Label andEnd = new Label();

            typeStack.push(Type.INT_TYPE);
            visit(ctx.expression(0));
            gen.visitJumpInsn(Opcodes.IFEQ, andFalse);
            visit(ctx.expression(1));
            gen.visitJumpInsn(Opcodes.IFEQ, andFalse);
            typeStack.pop();
            pushBoolean(true);
            gen.goTo(andEnd);
            gen.visitLabel(andFalse);
            pushBoolean(false);
            gen.visitLabel(andEnd);

            return null;
        }

        @Override
        public Void visitBoolor(JavascriptParser.BoolorContext ctx) {
            Label orTrue = new Label();
            Label orEnd = new Label();

            typeStack.push(Type.INT_TYPE);
            visit(ctx.expression(0));
            gen.visitJumpInsn(Opcodes.IFNE, orTrue);
            visit(ctx.expression(1));
            gen.visitJumpInsn(Opcodes.IFNE, orTrue);
            typeStack.pop();
            pushBoolean(false);
            gen.goTo(orEnd);
            gen.visitLabel(orTrue);
            pushBoolean(true);
            gen.visitLabel(orEnd);

            return null;
        }

        @Override
        public Void visitConditional(JavascriptParser.ConditionalContext ctx) {
            Label condFalse = new Label();
            Label condEnd = new Label();

            typeStack.push(Type.INT_TYPE);
            visit(ctx.expression(0));
            typeStack.pop();
            gen.visitJumpInsn(Opcodes.IFEQ, condFalse);
            visit(ctx.expression(1));
            gen.goTo(condEnd);
            gen.visitLabel(condFalse);
            visit(ctx.expression(2));
            gen.visitLabel(condEnd);

            return null;
        }

        private void pushArith(int operator, ExpressionContext left, ExpressionContext right) {
            pushBinaryOp(operator, left, right, Type.DOUBLE_TYPE, Type.DOUBLE_TYPE, Type.DOUBLE_TYPE);
        }

        private void pushShift(int operator, ExpressionContext left, ExpressionContext right) {
            pushBinaryOp(operator, left, right, Type.LONG_TYPE, Type.INT_TYPE, Type.LONG_TYPE);
        }

        private void pushBitwise(int operator, ExpressionContext left, ExpressionContext right) {
            pushBinaryOp(operator, left, right, Type.LONG_TYPE, Type.LONG_TYPE, Type.LONG_TYPE);
        }

        private void pushBinaryOp(int operator, ExpressionContext left, ExpressionContext right, Type leftType,
                Type rightType, Type returnType) {
            typeStack.push(leftType);
            visit(left);
            typeStack.pop();
            typeStack.push(rightType);
            visit(right);
            typeStack.pop();
            gen.visitInsn(operator);
            gen.cast(returnType, typeStack.peek());
        }

        private void pushCond(int operator, ExpressionContext left, ExpressionContext right) {
            Label labelTrue = new Label();
            Label labelReturn = new Label();

            typeStack.push(Type.DOUBLE_TYPE);
            visit(left);
            visit(right);
            typeStack.pop();

            gen.ifCmp(Type.DOUBLE_TYPE, operator, labelTrue);
            pushBoolean(false);
            gen.goTo(labelReturn);
            gen.visitLabel(labelTrue);
            pushBoolean(true);
            gen.visitLabel(labelReturn);
        }

        private void pushBoolean(boolean truth) {
            switch (typeStack.peek().getSort()) {
            case Type.INT:
                gen.push(truth);
                break;
            case Type.LONG:
                gen.push(truth ? 1L : 0L);
                break;
            case Type.DOUBLE:
                gen.push(truth ? 1. : 0.);
                break;
            default:
                throw new IllegalStateException("Invalid expected type: " + typeStack.peek());
            }
        }

        private void pushLong(long i) {
            switch (typeStack.peek().getSort()) {
            case Type.INT:
                gen.push((int) i);
                break;
            case Type.LONG:
                gen.push(i);
                break;
            case Type.DOUBLE:
                gen.push((double) i);
                break;
            default:
                throw new IllegalStateException("Invalid expected type: " + typeStack.peek());
            }
        }
    }.visit(parseTree);

    gen.returnValue();
    gen.endMethod();

    classWriter.visitEnd();
}

From source file:org.apache.lucene.expressions.js.XJavascriptCompiler.java

License:Apache License

private void recursiveCompile(Tree current, Type expected) {
    int type = current.getType();
    String text = current.getText();

    switch (type) {
    case XJavascriptParser.AT_CALL:
        Tree identifier = current.getChild(0);
        String call = identifier.getText();
        int arguments = current.getChildCount() - 1;

        Method method = functions.get(call);
        if (method == null) {
            throw new IllegalArgumentException("Unrecognized method call (" + call + ").");
        }/*from   w w  w . j a v  a 2 s  .  c  om*/

        int arity = method.getParameterTypes().length;
        if (arguments != arity) {
            throw new IllegalArgumentException("Expected (" + arity + ") arguments for method call (" + call
                    + "), but found (" + arguments + ").");
        }

        for (int argument = 1; argument <= arguments; ++argument) {
            recursiveCompile(current.getChild(argument), Type.DOUBLE_TYPE);
        }

        gen.invokeStatic(Type.getType(method.getDeclaringClass()),
                org.objectweb.asm.commons.Method.getMethod(method));

        gen.cast(Type.DOUBLE_TYPE, expected);
        break;
    case XJavascriptParser.VARIABLE:
        int index;

        // normalize quotes
        text = normalizeQuotes(text);

        if (externalsMap.containsKey(text)) {
            index = externalsMap.get(text);
        } else {
            index = externalsMap.size();
            externalsMap.put(text, index);
        }

        gen.loadArg(1);
        gen.push(index);
        gen.arrayLoad(FUNCTION_VALUES_TYPE);
        gen.loadArg(0);
        gen.invokeVirtual(FUNCTION_VALUES_TYPE, DOUBLE_VAL_METHOD);
        gen.cast(Type.DOUBLE_TYPE, expected);
        break;
    case XJavascriptParser.HEX:
        pushLong(expected, Long.parseLong(text.substring(2), 16));
        break;
    case XJavascriptParser.OCTAL:
        pushLong(expected, Long.parseLong(text.substring(1), 8));
        break;
    case XJavascriptParser.DECIMAL:
        gen.push(Double.parseDouble(text));
        gen.cast(Type.DOUBLE_TYPE, expected);
        break;
    case XJavascriptParser.AT_NEGATE:
        recursiveCompile(current.getChild(0), Type.DOUBLE_TYPE);
        gen.visitInsn(Opcodes.DNEG);
        gen.cast(Type.DOUBLE_TYPE, expected);
        break;
    case XJavascriptParser.AT_ADD:
        pushArith(Opcodes.DADD, current, expected);
        break;
    case XJavascriptParser.AT_SUBTRACT:
        pushArith(Opcodes.DSUB, current, expected);
        break;
    case XJavascriptParser.AT_MULTIPLY:
        pushArith(Opcodes.DMUL, current, expected);
        break;
    case XJavascriptParser.AT_DIVIDE:
        pushArith(Opcodes.DDIV, current, expected);
        break;
    case XJavascriptParser.AT_MODULO:
        pushArith(Opcodes.DREM, current, expected);
        break;
    case XJavascriptParser.AT_BIT_SHL:
        pushShift(Opcodes.LSHL, current, expected);
        break;
    case XJavascriptParser.AT_BIT_SHR:
        pushShift(Opcodes.LSHR, current, expected);
        break;
    case XJavascriptParser.AT_BIT_SHU:
        pushShift(Opcodes.LUSHR, current, expected);
        break;
    case XJavascriptParser.AT_BIT_AND:
        pushBitwise(Opcodes.LAND, current, expected);
        break;
    case XJavascriptParser.AT_BIT_OR:
        pushBitwise(Opcodes.LOR, current, expected);
        break;
    case XJavascriptParser.AT_BIT_XOR:
        pushBitwise(Opcodes.LXOR, current, expected);
        break;
    case XJavascriptParser.AT_BIT_NOT:
        recursiveCompile(current.getChild(0), Type.LONG_TYPE);
        gen.push(-1L);
        gen.visitInsn(Opcodes.LXOR);
        gen.cast(Type.LONG_TYPE, expected);
        break;
    case XJavascriptParser.AT_COMP_EQ:
        pushCond(GeneratorAdapter.EQ, current, expected);
        break;
    case XJavascriptParser.AT_COMP_NEQ:
        pushCond(GeneratorAdapter.NE, current, expected);
        break;
    case XJavascriptParser.AT_COMP_LT:
        pushCond(GeneratorAdapter.LT, current, expected);
        break;
    case XJavascriptParser.AT_COMP_GT:
        pushCond(GeneratorAdapter.GT, current, expected);
        break;
    case XJavascriptParser.AT_COMP_LTE:
        pushCond(GeneratorAdapter.LE, current, expected);
        break;
    case XJavascriptParser.AT_COMP_GTE:
        pushCond(GeneratorAdapter.GE, current, expected);
        break;
    case XJavascriptParser.AT_BOOL_NOT:
        Label labelNotTrue = new Label();
        Label labelNotReturn = new Label();

        recursiveCompile(current.getChild(0), Type.INT_TYPE);
        gen.visitJumpInsn(Opcodes.IFEQ, labelNotTrue);
        pushBoolean(expected, false);
        gen.goTo(labelNotReturn);
        gen.visitLabel(labelNotTrue);
        pushBoolean(expected, true);
        gen.visitLabel(labelNotReturn);
        break;
    case XJavascriptParser.AT_BOOL_AND:
        Label andFalse = new Label();
        Label andEnd = new Label();

        recursiveCompile(current.getChild(0), Type.INT_TYPE);
        gen.visitJumpInsn(Opcodes.IFEQ, andFalse);
        recursiveCompile(current.getChild(1), Type.INT_TYPE);
        gen.visitJumpInsn(Opcodes.IFEQ, andFalse);
        pushBoolean(expected, true);
        gen.goTo(andEnd);
        gen.visitLabel(andFalse);
        pushBoolean(expected, false);
        gen.visitLabel(andEnd);
        break;
    case XJavascriptParser.AT_BOOL_OR:
        Label orTrue = new Label();
        Label orEnd = new Label();

        recursiveCompile(current.getChild(0), Type.INT_TYPE);
        gen.visitJumpInsn(Opcodes.IFNE, orTrue);
        recursiveCompile(current.getChild(1), Type.INT_TYPE);
        gen.visitJumpInsn(Opcodes.IFNE, orTrue);
        pushBoolean(expected, false);
        gen.goTo(orEnd);
        gen.visitLabel(orTrue);
        pushBoolean(expected, true);
        gen.visitLabel(orEnd);
        break;
    case XJavascriptParser.AT_COND_QUE:
        Label condFalse = new Label();
        Label condEnd = new Label();

        recursiveCompile(current.getChild(0), Type.INT_TYPE);
        gen.visitJumpInsn(Opcodes.IFEQ, condFalse);
        recursiveCompile(current.getChild(1), expected);
        gen.goTo(condEnd);
        gen.visitLabel(condFalse);
        recursiveCompile(current.getChild(2), expected);
        gen.visitLabel(condEnd);
        break;
    default:
        throw new IllegalStateException("Unknown operation specified: (" + current.getText() + ").");
    }
}

From source file:org.elasticsearch.painless.MethodWriter.java

License:Apache License

public void writeLoopCounter(int slot, int count, Location location) {
    assert slot != -1;
    writeDebugInfo(location);/*from  w w  w  .j  a v a  2 s. c om*/
    final Label end = new Label();

    iinc(slot, -count);
    visitVarInsn(Opcodes.ILOAD, slot);
    push(0);
    ifICmp(GeneratorAdapter.GT, end);
    throwException(PAINLESS_ERROR_TYPE,
            "The maximum number of statements that can be executed in a loop has been reached.");
    mark(end);
}

From source file:org.elasticsearch.painless.Writer.java

License:Apache License

@Override
public Void visitComp(final CompContext ctx) {
    final Metadata.ExpressionMetadata compemd = metadata.getExpressionMetadata(ctx);
    final Object postConst = compemd.postConst;
    final Object preConst = compemd.preConst;
    final Branch branch = getBranch(ctx);

    if (postConst != null) {
        if (branch == null) {
            writeConstant(ctx, postConst);
        } else {//from  w  w  w  .  j ava2  s .  c o  m
            if ((boolean) postConst && branch.tru != null) {
                execute.mark(branch.tru);
            } else if (!(boolean) postConst && branch.fals != null) {
                execute.mark(branch.fals);
            }
        }
    } else if (preConst != null) {
        if (branch == null) {
            writeConstant(ctx, preConst);
            checkWriteCast(compemd);
        } else {
            throw new IllegalStateException(Metadata.error(ctx) + "Unexpected writer state.");
        }
    } else {
        final ExpressionContext exprctx0 = ctx.expression(0);
        final Metadata.ExpressionMetadata expremd0 = metadata.getExpressionMetadata(exprctx0);

        final ExpressionContext exprctx1 = ctx.expression(1);
        final Metadata.ExpressionMetadata expremd1 = metadata.getExpressionMetadata(exprctx1);
        final org.objectweb.asm.Type type = expremd1.to.type;
        final Sort sort1 = expremd1.to.sort;

        visit(exprctx0);

        if (!expremd1.isNull) {
            visit(exprctx1);
        }

        final boolean tru = branch != null && branch.tru != null;
        final boolean fals = branch != null && branch.fals != null;
        final Label jump = tru ? branch.tru : fals ? branch.fals : new Label();
        final Label end = new Label();

        final boolean eq = (ctx.EQ() != null || ctx.EQR() != null) && (tru || !fals)
                || (ctx.NE() != null || ctx.NER() != null) && fals;
        final boolean ne = (ctx.NE() != null || ctx.NER() != null) && (tru || !fals)
                || (ctx.EQ() != null || ctx.EQR() != null) && fals;
        final boolean lt = ctx.LT() != null && (tru || !fals) || ctx.GTE() != null && fals;
        final boolean lte = ctx.LTE() != null && (tru || !fals) || ctx.GT() != null && fals;
        final boolean gt = ctx.GT() != null && (tru || !fals) || ctx.LTE() != null && fals;
        final boolean gte = ctx.GTE() != null && (tru || !fals) || ctx.LT() != null && fals;

        boolean writejump = true;

        switch (sort1) {
        case VOID:
        case BYTE:
        case SHORT:
        case CHAR:
            throw new IllegalStateException(Metadata.error(ctx) + "Unexpected writer state.");
        case BOOL:
            if (eq)
                execute.ifZCmp(GeneratorAdapter.EQ, jump);
            else if (ne)
                execute.ifZCmp(GeneratorAdapter.NE, jump);
            else {
                throw new IllegalStateException(Metadata.error(ctx) + "Unexpected writer state.");
            }

            break;
        case INT:
        case LONG:
        case FLOAT:
        case DOUBLE:
            if (eq)
                execute.ifCmp(type, GeneratorAdapter.EQ, jump);
            else if (ne)
                execute.ifCmp(type, GeneratorAdapter.NE, jump);
            else if (lt)
                execute.ifCmp(type, GeneratorAdapter.LT, jump);
            else if (lte)
                execute.ifCmp(type, GeneratorAdapter.LE, jump);
            else if (gt)
                execute.ifCmp(type, GeneratorAdapter.GT, jump);
            else if (gte)
                execute.ifCmp(type, GeneratorAdapter.GE, jump);
            else {
                throw new IllegalStateException(Metadata.error(ctx) + "Unexpected writer state.");
            }

            break;
        case DEF:
            if (eq) {
                if (expremd1.isNull) {
                    execute.ifNull(jump);
                } else if (!expremd0.isNull && ctx.EQ() != null) {
                    execute.invokeStatic(definition.defobjType.type, DEF_EQ_CALL);
                } else {
                    execute.ifCmp(type, GeneratorAdapter.EQ, jump);
                }
            } else if (ne) {
                if (expremd1.isNull) {
                    execute.ifNonNull(jump);
                } else if (!expremd0.isNull && ctx.NE() != null) {
                    execute.invokeStatic(definition.defobjType.type, DEF_EQ_CALL);
                    execute.ifZCmp(GeneratorAdapter.EQ, jump);
                } else {
                    execute.ifCmp(type, GeneratorAdapter.NE, jump);
                }
            } else if (lt) {
                execute.invokeStatic(definition.defobjType.type, DEF_LT_CALL);
            } else if (lte) {
                execute.invokeStatic(definition.defobjType.type, DEF_LTE_CALL);
            } else if (gt) {
                execute.invokeStatic(definition.defobjType.type, DEF_GT_CALL);
            } else if (gte) {
                execute.invokeStatic(definition.defobjType.type, DEF_GTE_CALL);
            } else {
                throw new IllegalStateException(Metadata.error(ctx) + "Unexpected writer state.");
            }

            writejump = expremd1.isNull || ne || ctx.EQR() != null;

            if (branch != null && !writejump) {
                execute.ifZCmp(GeneratorAdapter.NE, jump);
            }

            break;
        default:
            if (eq) {
                if (expremd1.isNull) {
                    execute.ifNull(jump);
                } else if (ctx.EQ() != null) {
                    execute.invokeStatic(definition.utilityType.type, CHECKEQUALS);

                    if (branch != null) {
                        execute.ifZCmp(GeneratorAdapter.NE, jump);
                    }

                    writejump = false;
                } else {
                    execute.ifCmp(type, GeneratorAdapter.EQ, jump);
                }
            } else if (ne) {
                if (expremd1.isNull) {
                    execute.ifNonNull(jump);
                } else if (ctx.NE() != null) {
                    execute.invokeStatic(definition.utilityType.type, CHECKEQUALS);
                    execute.ifZCmp(GeneratorAdapter.EQ, jump);
                } else {
                    execute.ifCmp(type, GeneratorAdapter.NE, jump);
                }
            } else {
                throw new IllegalStateException(Metadata.error(ctx) + "Unexpected writer state.");
            }
        }

        if (branch == null) {
            if (writejump) {
                execute.push(false);
                execute.goTo(end);
                execute.mark(jump);
                execute.push(true);
                execute.mark(end);
            }

            checkWriteCast(compemd);
        }
    }

    return null;
}

From source file:org.elasticsearch.painless.Writer.java

License:Apache License

private void writeLoopCounter(final int count) {
    final Label end = new Label();

    execute.iinc(metadata.loopCounterSlot, -count);
    execute.visitVarInsn(Opcodes.ILOAD, metadata.loopCounterSlot);
    execute.push(0);/*from  w  w  w.  j  a va  2s.  c o  m*/
    execute.ifICmp(GeneratorAdapter.GT, end);
    execute.throwException(PAINLESS_ERROR_TYPE,
            "The maximum number of statements that can be executed in a loop has been reached.");
    execute.mark(end);
}

From source file:org.elasticsearch.painless.WriterExpression.java

License:Apache License

void processComp(final CompContext ctx) {
    final ExpressionMetadata compemd = metadata.getExpressionMetadata(ctx);
    final Object postConst = compemd.postConst;
    final Object preConst = compemd.preConst;
    final Branch branch = utility.getBranch(ctx);

    if (postConst != null) {
        if (branch == null) {
            utility.writeConstant(ctx, postConst);
        } else {/*from ww  w.  j  a  va 2  s  .co m*/
            if ((boolean) postConst && branch.tru != null) {
                execute.mark(branch.tru);
            } else if (!(boolean) postConst && branch.fals != null) {
                execute.mark(branch.fals);
            }
        }
    } else if (preConst != null) {
        if (branch == null) {
            utility.writeConstant(ctx, preConst);
            caster.checkWriteCast(compemd);
        } else {
            throw new IllegalStateException(WriterUtility.error(ctx) + "Unexpected state.");
        }
    } else {
        final ExpressionContext exprctx0 = ctx.expression(0);
        final ExpressionMetadata expremd0 = metadata.getExpressionMetadata(exprctx0);

        final ExpressionContext exprctx1 = ctx.expression(1);
        final ExpressionMetadata expremd1 = metadata.getExpressionMetadata(exprctx1);
        final org.objectweb.asm.Type type = expremd1.to.type;
        final Sort sort1 = expremd1.to.sort;

        writer.visit(exprctx0);

        if (!expremd1.isNull) {
            writer.visit(exprctx1);
        }

        final boolean tru = branch != null && branch.tru != null;
        final boolean fals = branch != null && branch.fals != null;
        final Label jump = tru ? branch.tru : fals ? branch.fals : new Label();
        final Label end = new Label();

        final boolean eq = (ctx.EQ() != null || ctx.EQR() != null) && (tru || !fals)
                || (ctx.NE() != null || ctx.NER() != null) && fals;
        final boolean ne = (ctx.NE() != null || ctx.NER() != null) && (tru || !fals)
                || (ctx.EQ() != null || ctx.EQR() != null) && fals;
        final boolean lt = ctx.LT() != null && (tru || !fals) || ctx.GTE() != null && fals;
        final boolean lte = ctx.LTE() != null && (tru || !fals) || ctx.GT() != null && fals;
        final boolean gt = ctx.GT() != null && (tru || !fals) || ctx.LTE() != null && fals;
        final boolean gte = ctx.GTE() != null && (tru || !fals) || ctx.LT() != null && fals;

        boolean writejump = true;

        switch (sort1) {
        case VOID:
        case BYTE:
        case SHORT:
        case CHAR:
            throw new IllegalStateException(WriterUtility.error(ctx) + "Unexpected state.");
        case BOOL:
            if (eq)
                execute.ifZCmp(GeneratorAdapter.EQ, jump);
            else if (ne)
                execute.ifZCmp(GeneratorAdapter.NE, jump);
            else {
                throw new IllegalStateException(WriterUtility.error(ctx) + "Unexpected state.");
            }

            break;
        case INT:
        case LONG:
        case FLOAT:
        case DOUBLE:
            if (eq)
                execute.ifCmp(type, GeneratorAdapter.EQ, jump);
            else if (ne)
                execute.ifCmp(type, GeneratorAdapter.NE, jump);
            else if (lt)
                execute.ifCmp(type, GeneratorAdapter.LT, jump);
            else if (lte)
                execute.ifCmp(type, GeneratorAdapter.LE, jump);
            else if (gt)
                execute.ifCmp(type, GeneratorAdapter.GT, jump);
            else if (gte)
                execute.ifCmp(type, GeneratorAdapter.GE, jump);
            else {
                throw new IllegalStateException(WriterUtility.error(ctx) + "Unexpected state.");
            }

            break;
        case DEF:
            if (eq) {
                if (expremd1.isNull) {
                    execute.ifNull(jump);
                } else if (!expremd0.isNull && ctx.EQ() != null) {
                    execute.invokeStatic(definition.defobjType.type, DEF_EQ_CALL);
                } else {
                    execute.ifCmp(type, GeneratorAdapter.EQ, jump);
                }
            } else if (ne) {
                if (expremd1.isNull) {
                    execute.ifNonNull(jump);
                } else if (!expremd0.isNull && ctx.NE() != null) {
                    execute.invokeStatic(definition.defobjType.type, DEF_EQ_CALL);
                    execute.ifZCmp(GeneratorAdapter.EQ, jump);
                } else {
                    execute.ifCmp(type, GeneratorAdapter.NE, jump);
                }
            } else if (lt) {
                execute.invokeStatic(definition.defobjType.type, DEF_LT_CALL);
            } else if (lte) {
                execute.invokeStatic(definition.defobjType.type, DEF_LTE_CALL);
            } else if (gt) {
                execute.invokeStatic(definition.defobjType.type, DEF_GT_CALL);
            } else if (gte) {
                execute.invokeStatic(definition.defobjType.type, DEF_GTE_CALL);
            } else {
                throw new IllegalStateException(WriterUtility.error(ctx) + "Unexpected state.");
            }

            writejump = expremd1.isNull || ne || ctx.EQR() != null;

            if (branch != null && !writejump) {
                execute.ifZCmp(GeneratorAdapter.NE, jump);
            }

            break;
        default:
            if (eq) {
                if (expremd1.isNull) {
                    execute.ifNull(jump);
                } else if (ctx.EQ() != null) {
                    execute.invokeStatic(definition.utilityType.type, CHECKEQUALS);

                    if (branch != null) {
                        execute.ifZCmp(GeneratorAdapter.NE, jump);
                    }

                    writejump = false;
                } else {
                    execute.ifCmp(type, GeneratorAdapter.EQ, jump);
                }
            } else if (ne) {
                if (expremd1.isNull) {
                    execute.ifNonNull(jump);
                } else if (ctx.NE() != null) {
                    execute.invokeStatic(definition.utilityType.type, CHECKEQUALS);
                    execute.ifZCmp(GeneratorAdapter.EQ, jump);
                } else {
                    execute.ifCmp(type, GeneratorAdapter.NE, jump);
                }
            } else {
                throw new IllegalStateException(WriterUtility.error(ctx) + "Unexpected state.");
            }
        }

        if (branch == null) {
            if (writejump) {
                execute.push(false);
                execute.goTo(end);
                execute.mark(jump);
                execute.push(true);
                execute.mark(end);
            }

            caster.checkWriteCast(compemd);
        }
    }
}

From source file:org.elasticsearch.plan.a.Writer.java

License:Apache License

@Override
public Void visitComp(final CompContext ctx) {
    final ExpressionMetadata compemd = metadata.getExpressionMetadata(ctx);
    final Object postConst = compemd.postConst;
    final Object preConst = compemd.preConst;
    final Branch branch = getBranch(ctx);

    if (postConst != null) {
        if (branch == null) {
            writeConstant(ctx, postConst);
        } else {// w w  w .  j  a v a2s. c om
            if ((boolean) postConst && branch.tru != null) {
                execute.mark(branch.tru);
            } else if (!(boolean) postConst && branch.fals != null) {
                execute.mark(branch.fals);
            }
        }
    } else if (preConst != null) {
        if (branch == null) {
            writeConstant(ctx, preConst);
            checkWriteCast(compemd);
        } else {
            throw new IllegalStateException(error(ctx) + "Unexpected writer state.");
        }
    } else {
        final ExpressionContext exprctx0 = ctx.expression(0);
        final ExpressionMetadata expremd0 = metadata.getExpressionMetadata(exprctx0);

        final ExpressionContext exprctx1 = ctx.expression(1);
        final ExpressionMetadata expremd1 = metadata.getExpressionMetadata(exprctx1);
        final org.objectweb.asm.Type type = expremd1.to.type;
        final Sort sort1 = expremd1.to.sort;

        visit(exprctx0);

        if (!expremd1.isNull) {
            visit(exprctx1);
        }

        final boolean tru = branch != null && branch.tru != null;
        final boolean fals = branch != null && branch.fals != null;
        final Label jump = tru ? branch.tru : fals ? branch.fals : new Label();
        final Label end = new Label();

        final boolean eq = (ctx.EQ() != null || ctx.EQR() != null) && (tru || !fals)
                || (ctx.NE() != null || ctx.NER() != null) && fals;
        final boolean ne = (ctx.NE() != null || ctx.NER() != null) && (tru || !fals)
                || (ctx.EQ() != null || ctx.EQR() != null) && fals;
        final boolean lt = ctx.LT() != null && (tru || !fals) || ctx.GTE() != null && fals;
        final boolean lte = ctx.LTE() != null && (tru || !fals) || ctx.GT() != null && fals;
        final boolean gt = ctx.GT() != null && (tru || !fals) || ctx.LTE() != null && fals;
        final boolean gte = ctx.GTE() != null && (tru || !fals) || ctx.LT() != null && fals;

        boolean writejump = true;

        switch (sort1) {
        case VOID:
        case BYTE:
        case SHORT:
        case CHAR:
            throw new IllegalStateException(error(ctx) + "Unexpected writer state.");
        case BOOL:
            if (eq)
                execute.ifZCmp(GeneratorAdapter.EQ, jump);
            else if (ne)
                execute.ifZCmp(GeneratorAdapter.NE, jump);
            else {
                throw new IllegalStateException(error(ctx) + "Unexpected writer state.");
            }

            break;
        case INT:
        case LONG:
        case FLOAT:
        case DOUBLE:
            if (eq)
                execute.ifCmp(type, GeneratorAdapter.EQ, jump);
            else if (ne)
                execute.ifCmp(type, GeneratorAdapter.NE, jump);
            else if (lt)
                execute.ifCmp(type, GeneratorAdapter.LT, jump);
            else if (lte)
                execute.ifCmp(type, GeneratorAdapter.LE, jump);
            else if (gt)
                execute.ifCmp(type, GeneratorAdapter.GT, jump);
            else if (gte)
                execute.ifCmp(type, GeneratorAdapter.GE, jump);
            else {
                throw new IllegalStateException(error(ctx) + "Unexpected writer state.");
            }

            break;
        case DEF:
            if (eq) {
                if (expremd1.isNull) {
                    execute.ifNull(jump);
                } else if (!expremd0.isNull && ctx.EQ() != null) {
                    execute.invokeStatic(definition.defobjType.type, DEF_EQ_CALL);
                } else {
                    execute.ifCmp(type, GeneratorAdapter.EQ, jump);
                }
            } else if (ne) {
                if (expremd1.isNull) {
                    execute.ifNonNull(jump);
                } else if (!expremd0.isNull && ctx.NE() != null) {
                    execute.invokeStatic(definition.defobjType.type, DEF_EQ_CALL);
                    execute.ifZCmp(GeneratorAdapter.EQ, jump);
                } else {
                    execute.ifCmp(type, GeneratorAdapter.NE, jump);
                }
            } else if (lt) {
                execute.invokeStatic(definition.defobjType.type, DEF_LT_CALL);
            } else if (lte) {
                execute.invokeStatic(definition.defobjType.type, DEF_LTE_CALL);
            } else if (gt) {
                execute.invokeStatic(definition.defobjType.type, DEF_GT_CALL);
            } else if (gte) {
                execute.invokeStatic(definition.defobjType.type, DEF_GTE_CALL);
            } else {
                throw new IllegalStateException(error(ctx) + "Unexpected writer state.");
            }

            writejump = expremd1.isNull || ne || ctx.EQR() != null;

            if (branch != null && !writejump) {
                execute.ifZCmp(GeneratorAdapter.NE, jump);
            }

            break;
        default:
            if (eq) {
                if (expremd1.isNull) {
                    execute.ifNull(jump);
                } else if (ctx.EQ() != null) {
                    execute.invokeStatic(definition.utilityType.type, CHECKEQUALS);

                    if (branch != null) {
                        execute.ifZCmp(GeneratorAdapter.NE, jump);
                    }

                    writejump = false;
                } else {
                    execute.ifCmp(type, GeneratorAdapter.EQ, jump);
                }
            } else if (ne) {
                if (expremd1.isNull) {
                    execute.ifNonNull(jump);
                } else if (ctx.NE() != null) {
                    execute.invokeStatic(definition.utilityType.type, CHECKEQUALS);
                    execute.ifZCmp(GeneratorAdapter.EQ, jump);
                } else {
                    execute.ifCmp(type, GeneratorAdapter.NE, jump);
                }
            } else {
                throw new IllegalStateException(error(ctx) + "Unexpected writer state.");
            }
        }

        if (branch == null) {
            if (writejump) {
                execute.push(false);
                execute.goTo(end);
                execute.mark(jump);
                execute.push(true);
                execute.mark(end);
            }

            checkWriteCast(compemd);
        }
    }

    return null;
}