Example usage for org.objectweb.asm.tree InsnList InsnList

List of usage examples for org.objectweb.asm.tree InsnList InsnList

Introduction

In this page you can find the example usage for org.objectweb.asm.tree InsnList InsnList.

Prototype

InsnList

Source Link

Usage

From source file:net.sandius.rembulan.compiler.gen.asm.RunMethod.java

License:Apache License

private InsnList dispatchTable(List<LabelNode> extLabels, List<LabelNode> resumptionLabels,
        LabelNode errorStateLabel) {/*from www  .  ja  v a  2 s. c om*/
    InsnList il = new InsnList();

    assert (!extLabels.isEmpty());

    ArrayList<LabelNode> labels = new ArrayList<>();
    labels.addAll(extLabels);
    labels.addAll(resumptionLabels);
    LabelNode[] labelArray = labels.toArray(new LabelNode[labels.size()]);

    int min = 1 - extLabels.size();
    int max = resumptionLabels.size();

    il.add(new VarInsnNode(ILOAD, LV_RESUME));
    il.add(new TableSwitchInsnNode(min, max, errorStateLabel, labelArray));
    return il;
}

From source file:net.sandius.rembulan.compiler.gen.asm.RunMethod.java

License:Apache License

InsnList createSnapshot() {
    InsnList il = new InsnList();

    il.add(new VarInsnNode(ALOAD, 0)); // this
    il.add(new VarInsnNode(ALOAD, 0));
    il.add(new VarInsnNode(ILOAD, LV_RESUME));
    if (context.isVararg()) {
        il.add(new VarInsnNode(ALOAD, LV_VARARGS));
    }// w w  w . ja v a 2s.c  om
    for (int i = 0; i < numOfRegisters(); i++) {
        il.add(new VarInsnNode(ALOAD, slotOffset() + i));
    }
    il.add(snapshotMethodInvokeInsn());

    return il;
}

From source file:net.sandius.rembulan.compiler.gen.asm.RunMethod.java

License:Apache License

protected InsnList resumptionHandler(LabelNode label) {
    InsnList il = new InsnList();

    il.add(label);/*ww  w  . j  av a 2s  .  c  om*/
    il.add(ASMUtils.frameSame1(UnresolvedControlThrowable.class));

    il.add(createSnapshot());

    // register snapshot with the control exception
    il.add(new MethodInsnNode(INVOKEVIRTUAL, Type.getInternalName(UnresolvedControlThrowable.class), "resolve",
            Type.getMethodType(Type.getType(ResolvedControlThrowable.class), Type.getType(Resumable.class),
                    Type.getType(Object.class)).getDescriptor(),
            false));

    // rethrow
    il.add(new InsnNode(ATHROW));

    return il;
}

From source file:net.sandius.rembulan.compiler.gen.asm.RunMethod.java

License:Apache License

private MethodNode emitRunMethod(String methodName, Type returnType, BytecodeEmitVisitor visitor,
        List<BasicBlock> blocks, boolean sub) {
    MethodNode node = new MethodNode(ACC_PRIVATE, methodName, methodType(returnType).getDescriptor(), null,
            throwsExceptions());/* w  ww .  j  ava  2s . c om*/

    InsnList insns = node.instructions;

    LabelNode l_begin = new LabelNode();
    LabelNode l_end = new LabelNode();

    visitor.visitBlocks(blocks);

    InsnList prefix = new InsnList();
    InsnList suffix = new InsnList();

    final LabelNode l_head;
    final List<LabelNode> els = new ArrayList<>();
    if (sub) {
        assert (!blocks.isEmpty());
        for (int i = blocks.size() - 1; i >= 0; i--) {
            BasicBlock blk = blocks.get(i);
            LabelNode l = visitor.labels.get(blk.label());
            assert (l != null);
            els.add(l);
        }
        l_head = visitor.labels.get(blocks.get(0).label());
    } else {
        l_head = new LabelNode();
        els.add(l_head);
    }

    assert (l_head != null);

    if (visitor.isResumable()) {
        LabelNode l_error_state = new LabelNode();
        LabelNode l_handler_begin = new LabelNode();

        List<LabelNode> rls = visitor.resumptionLabels();

        assert (!rls.isEmpty() || !els.isEmpty());

        prefix.add(dispatchTable(els, rls, l_error_state));

        final LabelNode l_entry = l_head;

        if (!sub) {
            prefix.add(l_entry);
            prefix.add(ASMUtils.frameSame());
        }

        suffix.add(errorState(l_error_state));
        suffix.add(resumptionHandler(l_handler_begin));

        node.tryCatchBlocks.add(new TryCatchBlockNode(l_entry, l_error_state, l_handler_begin,
                Type.getInternalName(UnresolvedControlThrowable.class)));
    }

    insns.add(l_begin);
    insns.add(prefix);
    insns.add(visitor.instructions());
    insns.add(suffix);
    insns.add(l_end);

    addLocals(node, l_begin, l_end, visitor);

    return node;
}

From source file:nova.core.wrapper.mc.forge.v17.asm.lib.ASMHelper.java

License:Open Source License

public static InsnList cloneInsnList(Map<LabelNode, LabelNode> labelMap, InsnList insns) {
    InsnList clone = new InsnList();
    for (AbstractInsnNode insn = insns.getFirst(); insn != null; insn = insn.getNext()) {
        clone.add(insn.clone(labelMap));
    }//from  ww  w  .  jav  a  2 s .  c o m

    return clone;
}

From source file:nova.core.wrapper.mc.forge.v17.asm.lib.InstructionComparator.java

License:Open Source License

public static InsnList getImportantList(InsnList list) {
    if (list.size() == 0) {
        return list;
    }/*w w  w  .  j av a 2 s.  c o  m*/

    HashMap<LabelNode, LabelNode> labels = new HashMap<LabelNode, LabelNode>();
    for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
        if (insn instanceof LabelNode) {
            labels.put((LabelNode) insn, (LabelNode) insn);
        }
    }

    InsnList importantNodeList = new InsnList();
    for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
        if (insn instanceof LabelNode || insn instanceof LineNumberNode) {
            continue;
        }

        importantNodeList.add(insn.clone(labels));
    }
    return importantNodeList;
}

From source file:nova.core.wrapper.mc.forge.v17.asm.transformers.ChunkTransformer.java

License:Open Source License

@Override
public void transform(ClassNode cnode) {
    System.out.println("[NOVA] Transforming Chunk class for chunkModified event.");

    ObfMapping obfMap = new ObfMapping("apx", "a", "(IIILaji;I)Z");
    ObfMapping srgMap = new ObfMapping("net/minecraft/world/chunk/Chunk", "func_150807_a",
            "(IIILnet/minecraft/block/Block;I)Z");

    MethodNode method = ASMHelper.findMethod(obfMap, cnode);

    if (method == null) {
        System.out.println("[NOVA] Lookup " + obfMap + " failed. You are probably in a deobf environment.");
        method = ASMHelper.findMethod(srgMap, cnode);

        if (method == null) {
            System.out.println("[NOVA] Lookup " + srgMap + " failed!");
        }//  ww  w. ja  va 2  s.c o m
    }

    System.out.println("[NOVA] Found method " + method.name);

    InsnList list = new InsnList();
    list.add(new VarInsnNode(ALOAD, 0));
    list.add(new VarInsnNode(ILOAD, 1));
    list.add(new VarInsnNode(ILOAD, 2));
    list.add(new VarInsnNode(ILOAD, 3));
    list.add(new VarInsnNode(ALOAD, 8));
    list.add(new VarInsnNode(ILOAD, 9));
    list.add(new VarInsnNode(ALOAD, 4));
    list.add(new VarInsnNode(ILOAD, 5));
    list.add(new MethodInsnNode(INVOKESTATIC, "nova/core/wrapper/mc/forge/v17/asm/StaticForwarder",
            "chunkSetBlockEvent",
            "(Lnet/minecraft/world/chunk/Chunk;IIILnet/minecraft/block/Block;ILnet/minecraft/block/Block;I)V",
            false));

    AbstractInsnNode lastInsn = method.instructions.getLast();
    while (lastInsn instanceof LabelNode || lastInsn instanceof LineNumberNode) {
        lastInsn = lastInsn.getPrevious();
    }

    if (ASMHelper.isReturn(lastInsn)) {
        method.instructions.insertBefore(lastInsn, list);
    } else {
        method.instructions.insert(list);
    }

    System.out.println("[NOVA] Injected instruction to method: " + method.name);
}

From source file:nova.core.wrapper.mc.forge.v17.asm.transformers.TileEntityTransformer.java

License:Open Source License

@Override
public void transform(ClassNode cnode) {

    System.out.println("[NOVA] Transforming TileEntity class for dynamic instance injection.");

    ObfMapping obfMap = new ObfMapping("aor", "c", "(Ldh;)Laor;");
    ObfMapping deobfMap = new ObfMapping("net/minecraft/tileentity/TileEntity", "createAndLoadEntity",
            "(Lnet/minecraft/nbt/NBTTagCompound;)Lnet/minecraft/tileentity/TileEntity;");

    MethodNode method = ASMHelper.findMethod(obfMap, cnode);

    if (method == null) {
        System.out.println("[NOVA] Lookup " + obfMap + " failed. You are probably in a deobf environment.");
        method = ASMHelper.findMethod(deobfMap, cnode);

        if (method == null) {
            System.out.println("[NOVA] Lookup " + deobfMap + " failed!");
        }// w  w  w  .jav a  2 s .  c  om
    }

    System.out.println("[NOVA] Transforming method " + method.name);

    ASMHelper.removeBlock(method.instructions,
            new InstructionComparator.InsnListSection(method.instructions, 23, 26));

    InsnList list = new InsnList();
    list.add(new VarInsnNode(ALOAD, 0));
    list.add(new VarInsnNode(ALOAD, 2));
    list.add(new MethodInsnNode(INVOKESTATIC, "nova/core/wrapper/mc/forge/v17/asm/StaticForwarder",
            "loadTileEntityHook",
            "(Lnet/minecraft/nbt/NBTTagCompound;Ljava/lang/Class;)Lnet/minecraft/tileentity/TileEntity;",
            false));
    list.add(new VarInsnNode(ASTORE, 1));

    method.instructions.insert(method.instructions.get(22), list);
}

From source file:nova.core.wrapper.mc.forge.v18.asm.transformers.ChunkTransformer.java

License:Open Source License

@Override
public void transform(ClassNode cnode) {
    System.out.println("[NOVA] Transforming Chunk class for chunkModified event.");

    //obf name: func_177436_a
    MethodNode method = ASMHelper.findMethod(new ObfMapping("net/minecraft/world/chunk/Chunk", "setBlockState",
            "(Lnet/minecraft/util/BlockPos;Lnet/minecraft/block/state/IBlockState;)Lnet/minecraft/block/state/IBlockState;"),
            cnode);/*w w  w .  ja va 2 s  .c  om*/

    System.out.println("[NOVA] Found method " + method.name);

    InsnList list = new InsnList();
    list.add(new VarInsnNode(ALOAD, 0)); //this
    list.add(new VarInsnNode(ALOAD, 1)); //BlockPos
    list.add(new VarInsnNode(ALOAD, 8)); //oldBlock IBlockState
    list.add(new VarInsnNode(ALOAD, 2)); //newBlock IBlockState
    list.add(new MethodInsnNode(INVOKESTATIC, "nova/core/wrapper/mc/forge/v18/asm/StaticForwarder",
            "chunkSetBlockEvent",
            "(Lnet/minecraft/world/chunk/Chunk;Lnet/minecraft/util/BlockPos;Lnet/minecraft/block/state/IBlockState;Lnet/minecraft/block/state/IBlockState;)V",
            false));

    AbstractInsnNode lastInsn = method.instructions.getLast();
    while (lastInsn instanceof LabelNode || lastInsn instanceof LineNumberNode) {
        lastInsn = lastInsn.getPrevious();
    }

    if (ASMHelper.isReturn(lastInsn)) {
        method.instructions.insertBefore(lastInsn, list);
    } else {
        method.instructions.insert(list);
    }

    System.out.println("[NOVA] Injected instruction to method: " + method.name);
}

From source file:nova.core.wrapper.mc.forge.v18.asm.transformers.TileEntityTransformer.java

License:Open Source License

@Override
public void transform(ClassNode cnode) {

    System.out.println("[NOVA] Transforming TileEntity class for dynamic instance injection.");

    ObfMapping obfMap = new ObfMapping("bcm", "c", "(Lfn;)Lbcm;");
    ObfMapping deobfMap = new ObfMapping("net/minecraft/tileentity/TileEntity", "createAndLoadEntity",
            "(Lnet/minecraft/nbt/NBTTagCompound;)Lnet/minecraft/tileentity/TileEntity;");

    MethodNode method = ASMHelper.findMethod(obfMap, cnode);

    if (method == null) {
        System.out.println("[NOVA] Lookup " + obfMap + " failed. You are probably in a deobf environment.");
        method = ASMHelper.findMethod(deobfMap, cnode);

        if (method == null) {
            System.out.println("[NOVA] Lookup " + deobfMap + " failed!");
        }//from w ww . j  a  va 2 s. com
    }

    System.out.println("[NOVA] Transforming method " + method.name);

    ASMHelper.removeBlock(method.instructions,
            new InstructionComparator.InsnListSection(method.instructions, 23, 26));

    InsnList list = new InsnList();
    list.add(new VarInsnNode(ALOAD, 0));
    list.add(new VarInsnNode(ALOAD, 2));
    list.add(new MethodInsnNode(INVOKESTATIC, "nova/core/wrapper/mc/forge/v18/asm/StaticForwarder",
            "loadTileEntityHook",
            "(Lnet/minecraft/nbt/NBTTagCompound;Ljava/lang/Class;)Lnet/minecraft/tileentity/TileEntity;",
            false));
    list.add(new VarInsnNode(ASTORE, 1));

    method.instructions.insert(method.instructions.get(22), list);
}