Example usage for org.objectweb.asm Opcodes ARETURN

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

Introduction

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

Prototype

int ARETURN

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

Click Source Link

Usage

From source file:com.yahoo.yqlplus.engine.internal.bytecode.CodeEmitterTest.java

@Test
public void requireSimpleAdd() throws Exception {
    UnitGenerator unit = new UnitGenerator("foo", source);
    unit.addInterface(Callable.class);
    MethodGenerator gen = unit.createMethod("call");
    gen.setReturnType(AnyTypeWidget.getInstance());
    final BytecodeExpression leftExpr = source.constant(1);
    final BytecodeExpression rightExpr = source.constant(1);

    gen.add(new BytecodeSequence() {
        @Override//from  w  w w. ja  va2  s.c  o  m
        public void generate(CodeEmitter code) {
            Label hasNull = new Label();
            CodeEmitter.Unification unify = code.unifiedEmit(leftExpr, rightExpr, hasNull);
            code.getMethodVisitor().visitInsn(Opcodes.IADD);
            code.box(BaseTypeAdapter.INT32);
            code.getMethodVisitor().visitInsn(Opcodes.ARETURN);
            if (unify.nullPossible) {
                code.getMethodVisitor().visitLabel(hasNull);
                code.getMethodVisitor().visitInsn(Opcodes.ACONST_NULL);
                code.getMethodVisitor().visitInsn(Opcodes.ARETURN);
            }
        }
    });

    source.build();
    Class<? extends Callable> clazz = (Class<? extends Callable>) source.getGeneratedClass(unit);
    Callable c = clazz.newInstance();
    Assert.assertEquals(2, c.call());
}

From source file:com.yahoo.yqlplus.engine.internal.bytecode.CodeEmitterTest.java

@Test
public void requireWidenAdd() throws Exception {
    UnitGenerator unit = new UnitGenerator("foo", source);
    unit.addInterface(Callable.class);
    MethodGenerator gen = unit.createMethod("call");
    gen.setReturnType(AnyTypeWidget.getInstance());
    final BytecodeExpression leftExpr = source.constant(1);
    final BytecodeExpression rightExpr = source.constant(1L);

    gen.add(new BytecodeSequence() {
        @Override/*from   w w  w  . ja v  a  2 s .  com*/
        public void generate(CodeEmitter code) {
            Label hasNull = new Label();
            CodeEmitter.Unification unify = code.unifiedEmit(leftExpr, rightExpr, hasNull);
            code.getMethodVisitor().visitInsn(Opcodes.LADD);
            code.box(BaseTypeAdapter.INT64);
            code.getMethodVisitor().visitInsn(Opcodes.ARETURN);
            if (unify.nullPossible) {
                code.getMethodVisitor().visitLabel(hasNull);
                code.getMethodVisitor().visitInsn(Opcodes.ACONST_NULL);
                code.getMethodVisitor().visitInsn(Opcodes.ARETURN);
            }
        }
    });

    source.build();
    Class<? extends Callable> clazz = (Class<? extends Callable>) source.getGeneratedClass(unit);
    Callable c = clazz.newInstance();
    Assert.assertEquals(2L, c.call());
}

From source file:com.yahoo.yqlplus.engine.internal.bytecode.CodeEmitterTest.java

@Test
public void requireFloatAdd() throws Exception {
    UnitGenerator unit = new UnitGenerator("foo", source);
    unit.addInterface(Callable.class);
    MethodGenerator gen = unit.createMethod("call");
    gen.setReturnType(AnyTypeWidget.getInstance());
    final BytecodeExpression leftExpr = source.constant(1);
    final BytecodeExpression rightExpr = source.constant(1.0f);

    gen.add(new BytecodeSequence() {
        @Override/* ww w. j  av  a  2  s  .  c o  m*/
        public void generate(CodeEmitter code) {
            Label hasNull = new Label();
            CodeEmitter.Unification unify = code.unifiedEmit(leftExpr, rightExpr, hasNull);
            code.getMethodVisitor().visitInsn(Opcodes.DADD);
            code.box(BaseTypeAdapter.FLOAT64);
            code.getMethodVisitor().visitInsn(Opcodes.ARETURN);
            if (unify.nullPossible) {
                code.getMethodVisitor().visitLabel(hasNull);
                code.getMethodVisitor().visitInsn(Opcodes.ACONST_NULL);
                code.getMethodVisitor().visitInsn(Opcodes.ARETURN);
            }
        }
    });

    source.build();
    Class<? extends Callable> clazz = (Class<? extends Callable>) source.getGeneratedClass(unit);
    Callable c = clazz.newInstance();
    Assert.assertEquals(2.0, c.call());
}

From source file:com.yahoo.yqlplus.engine.internal.bytecode.CodeEmitterTest.java

@Test
public void requireBoxedAdd() throws Exception {
    UnitGenerator unit = new UnitGenerator("foo", source);
    unit.addInterface(Callable.class);
    MethodGenerator gen = unit.createMethod("call");
    gen.setReturnType(AnyTypeWidget.getInstance());
    final BytecodeExpression leftExpr = source.constant(BaseTypeAdapter.INT32.boxed(), 1);
    final BytecodeExpression rightExpr = source.constant(1.0f);

    gen.add(new BytecodeSequence() {
        @Override//  w  w  w .j  a  v  a2  s.co m
        public void generate(CodeEmitter code) {
            Label hasNull = new Label();
            CodeEmitter.Unification unify = code.unifiedEmit(leftExpr, rightExpr, hasNull);
            code.getMethodVisitor().visitInsn(Opcodes.DADD);
            code.box(BaseTypeAdapter.FLOAT64);
            code.getMethodVisitor().visitInsn(Opcodes.ARETURN);
            if (unify.nullPossible) {
                code.getMethodVisitor().visitLabel(hasNull);
                code.getMethodVisitor().visitInsn(Opcodes.ACONST_NULL);
                code.getMethodVisitor().visitInsn(Opcodes.ARETURN);
            }
        }
    });

    source.build();
    Class<? extends Callable> clazz = (Class<? extends Callable>) source.getGeneratedClass(unit);
    Callable c = clazz.newInstance();
    Assert.assertEquals(2.0, c.call());
}

From source file:com.yahoo.yqlplus.engine.internal.bytecode.CodeEmitterTest.java

@Test
public void requireBoxedNull() throws Exception {
    UnitGenerator unit = new UnitGenerator("foo", source);
    unit.addInterface(Callable.class);
    MethodGenerator gen = unit.createMethod("call");
    gen.setReturnType(AnyTypeWidget.getInstance());
    final BytecodeExpression rightExpr = source.constant(1.0f);

    gen.add(new BytecodeSequence() {
        @Override//from   w ww  .  j av  a  2 s  .c  o m
        public void generate(CodeEmitter code) {
            Label hasNull = new Label();
            CodeEmitter.Unification unify = code.unifiedEmit(new NullExpr(BaseTypeAdapter.INT32.boxed()),
                    rightExpr, hasNull);
            code.getMethodVisitor().visitInsn(Opcodes.DADD);
            code.box(BaseTypeAdapter.FLOAT64);
            code.getMethodVisitor().visitInsn(Opcodes.ARETURN);
            if (unify.nullPossible) {
                code.getMethodVisitor().visitLabel(hasNull);
                code.getMethodVisitor().visitInsn(Opcodes.ACONST_NULL);
                code.getMethodVisitor().visitInsn(Opcodes.ARETURN);
            }
        }
    });

    source.build();
    Class<? extends Callable> clazz = (Class<? extends Callable>) source.getGeneratedClass(unit);
    Callable c = clazz.newInstance();
    Assert.assertNull(c.call());
}

From source file:com.yahoo.yqlplus.engine.internal.bytecode.DynamicBootstrapUnit.java

public void init() {
    ProgramValueTypeAdapter types = environment.getValueTypeAdapter();
    BytecodeExpression classSource = types.constant(getEnvironment());
    TypeWidget dynamicLinker = types.adaptInternal(DynamicLinker.class);
    FieldDefinition field = createField(dynamicLinker, "bootstrap");
    field.addModifier(Opcodes.ACC_STATIC | Opcodes.ACC_FINAL);
    MethodGenerator sl = createMethod("<clinit>");
    sl.setModifier(Opcodes.ACC_STATIC);/*from   ww  w.j  a  v a 2 s.c om*/
    sl.add(classSource);
    sl.add(new BytecodeSequence() {
        @Override
        public void generate(CodeEmitter code) {
            final MethodVisitor mv = code.getMethodVisitor();
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Dynamic.class), "createDynamicLinker",
                    MethodType.methodType(DynamicLinker.class, ASMClassSource.class).toMethodDescriptorString(),
                    false);
            mv.visitFieldInsn(Opcodes.PUTSTATIC, Dynamic.DYNAMIC_INTERNAL_NAME, "bootstrap",
                    Type.getDescriptor(DynamicLinker.class));
            mv.visitInsn(Opcodes.RETURN);

        }
    });

    MethodGenerator mgen = createStaticMethod(Dynamic.DYNAMIC_BOOTSTRAP_METHOD);
    TypeWidget callSite = types.adaptInternal(CallSite.class);
    TypeWidget lookup = types.adaptInternal(MethodHandles.Lookup.class);
    TypeWidget methodType = types.adaptInternal(MethodType.class);
    mgen.setReturnType(callSite);
    AssignableValue lu = mgen.addArgument("lookup", lookup);
    AssignableValue name = mgen.addArgument("name", BaseTypeAdapter.STRING);
    AssignableValue sig = mgen.addArgument("sig", methodType);
    final String desc = MethodType.methodType(CallSite.class, // ...that will return a CallSite object, ...
            MethodHandles.Lookup.class, // ... when given a lookup object, ...
            String.class, // ... the operation name, ...
            MethodType.class, // ... and the signature at the call site.
            DynamicLinker.class).toMethodDescriptorString();

    mgen.add(lu.read());
    mgen.add(name.read());
    mgen.add(sig.read());

    mgen.add(new BytecodeSequence() {
        @Override
        public void generate(CodeEmitter code) {
            final MethodVisitor mv = code.getMethodVisitor();
            mv.visitFieldInsn(Opcodes.GETSTATIC, Dynamic.DYNAMIC_INTERNAL_NAME, "bootstrap",
                    Type.getDescriptor(DynamicLinker.class));
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Dynamic.class), "link", desc, false);
            mv.visitInsn(Opcodes.ARETURN);
        }
    });

}

From source file:com.yahoo.yqlplus.engine.internal.generate.StructGenerator.java

private void implementRecord() {
    MethodGenerator fieldNamesGenerator = createMethod("getAllFieldNames");
    ImmutableList.Builder<String> fieldNames = ImmutableList.builder();
    fieldNames.addAll(fields.keySet());//from  w w  w.  jav  a  2 s. c  o  m
    BytecodeExpression expr = constant(fieldNames.build());
    fieldNamesGenerator.setReturnType(getValueTypeAdapter().adaptInternal(new TypeLiteral<Iterable<String>>() {
    }));
    fieldNamesGenerator.add(expr);
    fieldNamesGenerator.add(new ReturnCode(Opcodes.ARETURN));

    //   Object get(String field);
    generateGetMethod();

    //   void put(String field, Object value);
    generatePutMethod();

    // generate equals() and hashCode and hashValue(byte[] ...)
}

From source file:com.yahoo.yqlplus.engine.internal.generate.StructGenerator.java

private void generateGetMethod() {
    final MethodGenerator getGenerator = createMethod("get");
    getGenerator.setReturnType(AnyTypeWidget.getInstance());
    final BytecodeExpression propertyNameExpr = getGenerator.addArgument("propertyName", BaseTypeAdapter.STRING)
            .read();//from   w  w w .  ja v a2s  .c o  m
    getGenerator.add(new BytecodeSequence() {
        @Override
        public void generate(CodeEmitter code) {
            Map<String, Label> labelMap = Maps.newLinkedHashMap();
            Label defaultCase = new Label();
            Label done = new Label();
            for (Map.Entry<String, FieldProperty> field : fields.entrySet()) {
                labelMap.put(field.getKey(), new Label());
            }
            propertyNameExpr.generate(code);
            code.emitStringSwitch(labelMap, defaultCase, true);
            MethodVisitor mv = code.getMethodVisitor();
            for (Map.Entry<String, FieldProperty> field : fields.entrySet()) {
                mv.visitLabel(labelMap.get(field.getKey()));
                field.getValue().property(getGenerator.getLocal("this").read()).read().generate(code);
                code.box(field.getValue().property.type);
                mv.visitJumpInsn(Opcodes.GOTO, done);
            }
            mv.visitLabel(defaultCase);
            new NullExpr(AnyTypeWidget.getInstance()).generate(code);
            mv.visitLabel(done);
            mv.visitInsn(Opcodes.ARETURN);
        }
    });
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.RuntimeWidgetGenerator.java

private void generateGetFieldNames(TypeWidget targetType, RuntimeAdapter adapter) {
    MethodGenerator method = createMethod("getFieldNames");
    method.setReturnType(new IterableTypeWidget(BaseTypeAdapter.STRING));
    BytecodeExpression sourceExpr = new BytecodeCastExpression(targetType,
            method.addArgument("source", AnyTypeWidget.getInstance()).read());
    if (!targetType.hasProperties()) {
        method.add(environment.constant(method.getReturnType(), ImmutableList.<String>of()));
    } else {/* www. j  av a2s  . c  o m*/
        method.add(targetType.getPropertyAdapter().getPropertyNameIterable(sourceExpr));
    }
    method.add(new ReturnCode(Opcodes.ARETURN));
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.RuntimeWidgetGenerator.java

private void generateProperty(TypeWidget targetType, RuntimeAdapter adapter) {
    MethodGenerator method = createMethod("property");
    method.setReturnType(AnyTypeWidget.getInstance());
    BytecodeExpression sourceExpr = new BytecodeCastExpression(targetType,
            method.addArgument("source", AnyTypeWidget.getInstance()).read());
    BytecodeExpression nameExpr = method.addArgument("name", BaseTypeAdapter.STRING).read();
    final BytecodeExpression resultExpr = adapter.property(sourceExpr, nameExpr);
    method.add(resultExpr);//from w  ww. ja  v a  2s .com
    method.add(new BytecodeSequence() {
        @Override
        public void generate(CodeEmitter code) {
            code.box(resultExpr.getType());
            code.getMethodVisitor().visitInsn(Opcodes.ARETURN);
        }
    });
}