Search in sources :

Example 6 with InsnNode

use of org.objectweb.asm.tree.InsnNode in project pinpoint by naver.

the class ASMClassNodeAdapter method addSetterMethod.

public void addSetterMethod(final String methodName, final ASMFieldNodeAdapter fieldNode) {
    if (methodName == null || fieldNode == null) {
        throw new IllegalArgumentException("method name or fieldNode annotation must not be null.");
    }
    // void is V.
    final String desc = "(" + fieldNode.getDesc() + ")V";
    final MethodNode methodNode = new MethodNode(Opcodes.ACC_PUBLIC, methodName, desc, null, null);
    if (methodNode.instructions == null) {
        methodNode.instructions = new InsnList();
    }
    final InsnList instructions = methodNode.instructions;
    // load this.
    instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    final Type type = Type.getType(fieldNode.getDesc());
    // put field.
    instructions.add(new VarInsnNode(type.getOpcode(Opcodes.ILOAD), 1));
    instructions.add(new FieldInsnNode(Opcodes.PUTFIELD, classNode.name, fieldNode.getName(), fieldNode.getDesc()));
    // return.
    instructions.add(new InsnNode(Opcodes.RETURN));
    if (this.classNode.methods == null) {
        this.classNode.methods = new ArrayList<MethodNode>();
    }
    this.classNode.methods.add(methodNode);
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) Type(org.objectweb.asm.Type) MethodNode(org.objectweb.asm.tree.MethodNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) InsnList(org.objectweb.asm.tree.InsnList) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 7 with InsnNode

use of org.objectweb.asm.tree.InsnNode in project jphp by jphp-compiler.

the class TryCatchCompiler method write.

@Override
public void write(TryStmtToken token) {
    if (token.getBody() == null || token.getBody().getInstructions().isEmpty()) {
        if (token.getFinally() != null)
            expr.write(BodyStmtToken.class, token.getFinally());
        return;
    }
    expr.writeDefineVariables(token.getLocal());
    LabelNode tryStart = expr.writeLabel(node, token.getMeta().getStartLine());
    LabelNode tryEnd = new LabelNode();
    LabelNode catchStart = new LabelNode();
    LabelNode catchEnd = new LabelNode();
    LabelNode returnLabel = new LabelNode();
    method.node.tryCatchBlocks.add(0, new TryCatchBlockNode(tryStart, tryEnd, catchStart, Type.getInternalName(BaseBaseException.class)));
    if (token.getFinally() != null) {
        method.getTryStack().push(new MethodStmtCompiler.TryCatchItem(token, returnLabel));
    }
    expr.write(BodyStmtToken.class, token.getBody());
    if (token.getFinally() != null) {
        method.getTryStack().pop();
    }
    add(tryEnd);
    add(new JumpInsnNode(GOTO, catchEnd));
    add(catchStart);
    LocalVariable exception = method.addLocalVariable("~catch~" + method.nextStatementIndex(BaseException.class), catchStart, BaseBaseException.class);
    exception.setEndLabel(catchEnd);
    expr.makeVarStore(exception);
    LabelNode nextCatch = null;
    int i = 0, size = token.getCatches().size();
    LocalVariable local = null;
    LabelNode catchFail = new LabelNode();
    for (CatchStmtToken _catch : token.getCatches()) {
        if (nextCatch != null) {
            add(nextCatch);
        }
        if (i == size - 1) {
            nextCatch = catchFail;
        } else {
            nextCatch = new LabelNode();
        }
        local = method.getLocalVariable(_catch.getVariable().getName());
        expr.writePushEnv();
        expr.writeVarLoad(exception);
        expr.writePushConstString(_catch.getException().toName());
        expr.writePushConstString(_catch.getException().toName().toLowerCase());
        expr.writeSysDynamicCall(Environment.class, "__throwCatch", Memory.class, BaseBaseException.class, String.class, String.class);
        expr.writeVarAssign(local, _catch.getVariable(), true, false);
        expr.writePopBoolean();
        add(new JumpInsnNode(IFEQ, nextCatch));
        expr.stackPop();
        expr.write(BodyStmtToken.class, _catch.getBody());
        add(new JumpInsnNode(GOTO, catchEnd));
        i++;
    }
    add(catchFail);
    if (token.getFinally() != null) {
        expr.write(BodyStmtToken.class, token.getFinally());
    }
    /*if (method.getTryStack().empty()) {
            expr.writePushEnv();
            expr.writeVarLoad(exception);
            expr.writeSysDynamicCall(Environment.class, "__throwFailedCatch", void.class, BaseException.class);
        } else {*/
    expr.makeVarLoad(exception);
    add(new InsnNode(ATHROW));
    //}
    add(catchEnd);
    if (token.getFinally() != null) {
        LabelNode skip = new LabelNode();
        add(new JumpInsnNode(GOTO, skip));
        // finally for return
        add(returnLabel);
        expr.write(BodyStmtToken.class, token.getFinally());
        if (method.getTryStack().empty()) {
            // all finally blocks are done
            LocalVariable retVar = method.getOrAddLocalVariable("~result~", null, Memory.class);
            expr.writeVarLoad(retVar);
            add(new InsnNode(ARETURN));
            expr.stackPop();
        } else {
            // goto next finally block
            add(new JumpInsnNode(GOTO, method.getTryStack().peek().getReturnLabel()));
        }
        add(skip);
        // other finally
        expr.write(BodyStmtToken.class, token.getFinally());
    }
    expr.writeUndefineVariables(token.getLocal());
    method.prevStatementIndex(BaseBaseException.class);
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode) MethodStmtCompiler(org.develnext.jphp.core.compiler.jvm.statement.MethodStmtCompiler) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) CatchStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.CatchStmtToken) BodyStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken)

Example 8 with InsnNode

use of org.objectweb.asm.tree.InsnNode in project jphp by jphp-compiler.

the class ClosureValueCompiler method writePushUses.

protected void writePushUses(Collection<ArgumentStmtToken> parameters) {
    if (parameters.isEmpty()) {
        add(new InsnNode(ACONST_NULL));
        expr.stackPush(Memory.Type.REFERENCE);
        return;
    }
    expr.writePushSmallInt(parameters.size());
    add(new TypeInsnNode(ANEWARRAY, Type.getInternalName(Memory.class)));
    expr.stackPop();
    expr.stackPush(Memory.Type.REFERENCE);
    int i = 0;
    for (ArgumentStmtToken param : parameters) {
        expr.writePushDup();
        expr.writePushSmallInt(i);
        LocalVariable local = method.getLocalVariable(param.getName().getName());
        if (local == null)
            expr.writePushNull();
        else
            expr.writeVarLoad(local);
        if (!param.isReference())
            expr.writePopBoxing(true);
        add(new InsnNode(AASTORE));
        expr.stackPop();
        expr.stackPop();
        expr.stackPop();
        i++;
    }
}
Also used : MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) ArgumentStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ArgumentStmtToken) LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode)

Example 9 with InsnNode

use of org.objectweb.asm.tree.InsnNode in project presto by prestodb.

the class MethodDefinition method visit.

public void visit(ClassVisitor visitor, boolean addReturn) {
    String[] exceptions = new String[this.exceptions.size()];
    for (int i = 0; i < exceptions.length; i++) {
        exceptions[i] = this.exceptions.get(i).getClassName();
    }
    MethodVisitor methodVisitor = visitor.visitMethod(toAccessModifier(access), name, getMethodDescriptor(), genericMethodSignature(returnType, parameterTypes), exceptions);
    if (methodVisitor == null) {
        return;
    }
    // visit method annotations
    for (AnnotationDefinition annotation : annotations) {
        annotation.visitMethodAnnotation(methodVisitor);
    }
    // visit parameter annotations
    for (int parameterIndex = 0; parameterIndex < parameterAnnotations.size(); parameterIndex++) {
        List<AnnotationDefinition> parameterAnnotations1 = this.parameterAnnotations.get(parameterIndex);
        for (AnnotationDefinition parameterAnnotation : parameterAnnotations1) {
            parameterAnnotation.visitParameterAnnotation(parameterIndex, methodVisitor);
        }
    }
    // visit code
    methodVisitor.visitCode();
    // visit instructions
    MethodGenerationContext generationContext = new MethodGenerationContext(methodVisitor);
    generationContext.enterScope(scope);
    body.accept(methodVisitor, generationContext);
    if (addReturn) {
        new InsnNode(RETURN).accept(methodVisitor);
    }
    generationContext.exitScope(scope);
    // done
    methodVisitor.visitMaxs(-1, -1);
    methodVisitor.visitEnd();
}
Also used : InsnNode(org.objectweb.asm.tree.InsnNode) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 10 with InsnNode

use of org.objectweb.asm.tree.InsnNode in project pinpoint by naver.

the class ASMClassNodeAdapter method addGetterMethod.

public void addGetterMethod(final String methodName, final ASMFieldNodeAdapter fieldNode) {
    if (methodName == null || fieldNode == null) {
        throw new IllegalArgumentException("method name or fieldNode annotation must not be null.");
    }
    // no argument is ().
    final String desc = "()" + fieldNode.getDesc();
    final MethodNode methodNode = new MethodNode(Opcodes.ACC_PUBLIC, methodName, desc, null, null);
    if (methodNode.instructions == null) {
        methodNode.instructions = new InsnList();
    }
    final InsnList instructions = methodNode.instructions;
    // load this.
    instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    // get fieldNode.
    instructions.add(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, fieldNode.getName(), fieldNode.getDesc()));
    // return of type.
    final Type type = Type.getType(fieldNode.getDesc());
    instructions.add(new InsnNode(type.getOpcode(Opcodes.IRETURN)));
    if (this.classNode.methods == null) {
        this.classNode.methods = new ArrayList<MethodNode>();
    }
    this.classNode.methods.add(methodNode);
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) Type(org.objectweb.asm.Type) MethodNode(org.objectweb.asm.tree.MethodNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) InsnList(org.objectweb.asm.tree.InsnList) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Aggregations

InsnNode (org.objectweb.asm.tree.InsnNode)12 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)8 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)7 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)6 Type (org.objectweb.asm.Type)5 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)5 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)5 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)4 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)4 MethodNode (org.objectweb.asm.tree.MethodNode)4 LocalVariable (org.develnext.jphp.core.compiler.jvm.misc.LocalVariable)3 InsnList (org.objectweb.asm.tree.InsnList)3 IntInsnNode (org.objectweb.asm.tree.IntInsnNode)3 LabelNode (org.objectweb.asm.tree.LabelNode)3 InvokeDynamicInsnNode (org.objectweb.asm.tree.InvokeDynamicInsnNode)2 InterceptorType (com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorType)1 MethodType (java.lang.invoke.MethodType)1 MethodType.methodType (java.lang.invoke.MethodType.methodType)1 Event (net.minecraftforge.fml.common.eventhandler.Event)1 MethodStmtCompiler (org.develnext.jphp.core.compiler.jvm.statement.MethodStmtCompiler)1