Package org.objectweb.asm.tree

Examples of org.objectweb.asm.tree.InsnList


      Type[] args= Type.getArgumentTypes(mnode.desc);

      // optimizations for some common cases
      if (args.length == 0)
      {
        final InsnList doNew= new InsnList();
        doNew.add(node1); // NEW
        if (requireDup)
          doNew.add(new InsnNode(DUP));
        instructions.insertBefore(nm, doNew);
        nm= doNew.getLast();
        continue;
      }

      if (args.length == 1 && args[0].getSize() == 1)
      {
        final InsnList doNew= new InsnList();
        doNew.add(node1); // NEW
        if (requireDup)
        {
          doNew.add(new InsnNode(DUP));
          doNew.add(new InsnNode(DUP2_X1));
          doNew.add(new InsnNode(POP2));
          updateMaxStack= updateMaxStack < 2 ? 2 : updateMaxStack; // a two extra slots for temp values
        }
        else
          doNew.add(new InsnNode(SWAP));
        instructions.insertBefore(nm, doNew);
        nm= doNew.getLast();
        continue;
      }

      // TODO this one untested!
      if ((args.length == 1 && args[0].getSize() == 2) || (args.length == 2 && args[0].getSize() == 1 && args[1].getSize() == 1))
      {
        final InsnList doNew= new InsnList();
        doNew.add(node1); // NEW
        if (requireDup)
        {
          doNew.add(new InsnNode(DUP));
          doNew.add(new InsnNode(DUP2_X2));
          doNew.add(new InsnNode(POP2));
          updateMaxStack= updateMaxStack < 2 ? 2 : updateMaxStack; // a two extra slots for temp values
        }
        else
        {
          doNew.add(new InsnNode(DUP_X2));
          doNew.add(new InsnNode(POP));
          updateMaxStack= updateMaxStack < 1 ? 1 : updateMaxStack; // an extra slot for temp value
        }
        instructions.insertBefore(nm, doNew);
        nm= doNew.getLast();
        continue;
      }

      final InsnList doNew= new InsnList();
      // generic code using temporary locals
      // save stack
      for (int j= args.length - 1; j >= 0; j--)
      {
        Type type= args[j];

        doNew.add(new VarInsnNode(type.getOpcode(ISTORE), varOffset));
        varOffset+= type.getSize();
      }
      if (varOffset > maxLocals)
      {
        maxLocals= varOffset;
      }

      doNew.add(node1); // NEW

      if (requireDup)
        doNew.add(new InsnNode(DUP));

      // restore stack
      for (int j= 0; j < args.length; j++)
      {
        Type type= args[j];
        varOffset-= type.getSize();

        doNew.add(new VarInsnNode(type.getOpcode(ILOAD), varOffset));

        // clean up store to avoid memory leak?
        if (type.getSort() == Type.OBJECT || type.getSort() == Type.ARRAY)
        {
          updateMaxStack= updateMaxStack < 1 ? 1 : updateMaxStack; // an extra slot for ACONST_NULL

          doNew.add(new InsnNode(ACONST_NULL));

          doNew.add(new VarInsnNode(type.getOpcode(ISTORE), varOffset));
        }
      }
      instructions.insertBefore(nm, doNew);
      nm= doNew.getLast();
    }

    maxStack+= updateMaxStack;
  }
View Full Code Here


   * @return A list of instruction nodes created from the <code>scriptLines</code>.
   * @throws InvalidBytecodeException If the script was invalid.
   */
  public static InsnList generateInsnListFromScript(String[] scriptLines)
      throws InvalidBytecodeException {
    InsnList ret = new InsnList();
    for (String line : scriptLines) {
      String[] args = MiscUtils.translateCommandline(line);

      /*
       * for (int i = 0; i < args.length; i++) { // FIXME if (args[i].contains(" ")) { args[i] = '"'
       * + args[i]; } }
       */

      ret.add(getNodeFromArgs(args));
    }

    // Generate actual labels.
    Map<Integer, LabelNode> labelz = new HashMap<Integer, LabelNode>();
    ListIterator<AbstractInsnNode> li = ret.iterator();
    AbstractInsnNode ain = li.hasNext() ? null : li.next();
    for (int i = 0; li.hasNext(); ain = li.next(), i++) {
      if (ain instanceof LineNumberNode) {
        LineNumberNode lnn = (LineNumberNode) ain;
        LabelNode cl = labelz.get(lnn.start.getLabel().getOffset());
        if (cl == null) {
          labelz.put(lnn.start.getLabel().getOffset(), lnn.start);
        } else {
          lnn.start = cl;
        }
      } else if (ain instanceof JumpInsnNode) {
        JumpInsnNode jin = (JumpInsnNode) ain;
        LabelNode cl = labelz.get(jin.label.getLabel().getOffset());
        if (cl == null) {
          labelz.put(jin.label.getLabel().getOffset(), jin.label);
        } else {
          jin.label = cl;
        }
      } else if (ain instanceof TableSwitchInsnNode) {
        TableSwitchInsnNode tsin = (TableSwitchInsnNode) ain;
        LabelNode cl = labelz.get(tsin.dflt.getLabel().getOffset());
        if (cl == null) {
          labelz.put(tsin.dflt.getLabel().getOffset(), tsin.dflt);
        } else {
          tsin.dflt = cl;
        }
      }
    }
    Map<AbstractInsnNode, LabelNode> nodeMap = new HashMap<AbstractInsnNode, LabelNode>();
    for (Entry<Integer, LabelNode> e : labelz.entrySet()) {
      nodeMap.put(ret.get(e.getKey()), e.getValue());
    }


    System.out.println("Adding labels!");
    for (Entry<AbstractInsnNode, LabelNode> e : nodeMap.entrySet()) {
      System.out.println(String.format("%s -> %s", e.getKey(), e.getValue()));
      ret.insertBefore(e.getKey(), e.getValue());
    }
    System.out.println("Added labels.");


    return ret;
View Full Code Here

    @Nullable
    private static MethodInsnNode findConstructorInvocation(
            @NonNull MethodNode method,
            @NonNull String className) {
        InsnList nodes = method.instructions;
        for (int i = 0, n = nodes.size(); i < n; i++) {
            AbstractInsnNode instruction = nodes.get(i);
            if (instruction.getOpcode() == Opcodes.INVOKESPECIAL) {
                MethodInsnNode call = (MethodInsnNode) instruction;
                if (className.equals(call.owner)) {
                    return call;
                }
View Full Code Here

      Type[] args= Type.getArgumentTypes(mnode.desc);

      // optimizations for some common cases
      if (args.length == 0)
      {
        final InsnList doNew= new InsnList();
        doNew.add(node1); // NEW
        if (requireDup)
          doNew.add(new InsnNode(DUP));
        instructions.insertBefore(nm, doNew);
        nm= doNew.getLast();
        continue;
      }

      if (args.length == 1 && args[0].getSize() == 1)
      {
        final InsnList doNew= new InsnList();
        doNew.add(node1); // NEW
        if (requireDup)
        {
          doNew.add(new InsnNode(DUP));
          doNew.add(new InsnNode(DUP2_X1));
          doNew.add(new InsnNode(POP2));
          updateMaxStack= updateMaxStack < 2 ? 2 : updateMaxStack; // a two extra slots for temp values
        }
        else
          doNew.add(new InsnNode(SWAP));
        instructions.insertBefore(nm, doNew);
        nm= doNew.getLast();
        continue;
      }

      // TODO this one untested!
      if ((args.length == 1 && args[0].getSize() == 2) || (args.length == 2 && args[0].getSize() == 1 && args[1].getSize() == 1))
      {
        final InsnList doNew= new InsnList();
        doNew.add(node1); // NEW
        if (requireDup)
        {
          doNew.add(new InsnNode(DUP));
          doNew.add(new InsnNode(DUP2_X2));
          doNew.add(new InsnNode(POP2));
          updateMaxStack= updateMaxStack < 2 ? 2 : updateMaxStack; // a two extra slots for temp values
        }
        else
        {
          doNew.add(new InsnNode(DUP_X2));
          doNew.add(new InsnNode(POP));
          updateMaxStack= updateMaxStack < 1 ? 1 : updateMaxStack; // an extra slot for temp value
        }
        instructions.insertBefore(nm, doNew);
        nm= doNew.getLast();
        continue;
      }

      final InsnList doNew= new InsnList();
      // generic code using temporary locals
      // save stack
      for (int j= args.length - 1; j >= 0; j--)
      {
        Type type= args[j];

        doNew.add(new VarInsnNode(type.getOpcode(ISTORE), varOffset));
        varOffset+= type.getSize();
      }
      if (varOffset > maxLocals)
      {
        maxLocals= varOffset;
      }

      doNew.add(node1); // NEW

      if (requireDup)
        doNew.add(new InsnNode(DUP));

      // restore stack
      for (int j= 0; j < args.length; j++)
      {
        Type type= args[j];
        varOffset-= type.getSize();

        doNew.add(new VarInsnNode(type.getOpcode(ILOAD), varOffset));

        // clean up store to avoid memory leak?
        if (type.getSort() == Type.OBJECT || type.getSort() == Type.ARRAY)
        {
          updateMaxStack= updateMaxStack < 1 ? 1 : updateMaxStack; // an extra slot for ACONST_NULL

          doNew.add(new InsnNode(ACONST_NULL));

          doNew.add(new VarInsnNode(type.getOpcode(ISTORE), varOffset));
        }
      }
      instructions.insertBefore(nm, doNew);
      nm= doNew.getLast();
    }

    maxStack+= updateMaxStack;
  }
View Full Code Here

                       
                        // make a new label node for the end of our code
                        LabelNode lmm1Node = new LabelNode(new Label());
                       
                        // make new instruction list
                        InsnList toInject = new InsnList();
                       
                        // construct instruction nodes for list
                        toInject.add(new VarInsnNode(ALOAD, 0));
                        toInject.add(new FieldInsnNode(GETFIELD, (String) hm.get("javaClassName"), (String) hm.get("worldFieldName"), "L" + (String) hm.get("worldJavaClassName") + ";"));
                        toInject.add(new VarInsnNode(ILOAD, 1));
                        toInject.add(new VarInsnNode(ILOAD, 2));
                        toInject.add(new VarInsnNode(ILOAD, 3));
                        toInject.add(new VarInsnNode(ALOAD, blockIndex));
                        toInject.add(new VarInsnNode(ILOAD, mdIndex));
                        toInject.add(new VarInsnNode(ALOAD, 0));
                        toInject.add(new FieldInsnNode(GETFIELD, (String) hm.get("javaClassName"), (String) hm.get("entityPlayerFieldName"), "L" + (String) hm.get("entityPlayerMPJavaClassName") + ";"));
                        toInject.add(new MethodInsnNode(INVOKESTATIC, "keepcalm/mods/events/EventFactory", "onBlockHarvested", "(L" + (String) hm.get("worldJavaClassName") + ";IIIL" + (String) hm.get("blockJavaClassName") + ";IL" + (String) hm.get("entityPlayerJavaClassName") + ";)Z"));
                        LabelNode endIf = new LabelNode(new Label());
                        toInject.add(new JumpInsnNode(Opcodes.IFEQ, endIf));
                        toInject.add(new InsnNode(Opcodes.ICONST_0));
                        toInject.add(new InsnNode(Opcodes.IRETURN));
                        toInject.add(endIf);
                        toInject.add(lmm1Node);
                       
                        m.instructions.insertBefore(m.instructions.get(index + offset), toInject);
                       
                        System.out.println("Method " + (String) hm.get("javaClassName") + "/" + m.name + m.desc + " at index " + (index + offset - 1));
                        System.out.println("Patching Complete!");
View Full Code Here

     
      if (m.name.equals(names.get("blockFlowing_updateFlow_func")) &&m.desc.equals(names.get("blockFlowing_updateFlow_func"))) {
        System.out.println("Found target method: " + m.name + m.desc + "!");
       
       
        InsnList toAdd = new InsnList();
       
        toAdd.add(new VarInsnNode(Opcodes.ALOAD, 0));
        toAdd.add(new VarInsnNode(Opcodes.ALOAD, 1));
        toAdd.add(new VarInsnNode(Opcodes.ILOAD, 2));
        toAdd.add(new VarInsnNode(Opcodes.ILOAD, 3));
        toAdd.add(new VarInsnNode(Opcodes.ILOAD, 4));///
        toAdd.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
            "keepcalm/mods/events/ForgeEventHelper", "onBlockFlow",
            "(L" + names.get("block_javaName") + ";L" + names.get("world_javaName") + ";III)Z"));
        LabelNode endIf = new LabelNode(new Label());
        toAdd.add(new JumpInsnNode(Opcodes.IFEQ, endIf));
        toAdd.add(new InsnNode(Opcodes.RETURN));
        toAdd.add(endIf);
        toAdd.add(new LabelNode(new Label()));
       
        m.instructions.add(toAdd);
    }
     
    }
View Full Code Here

      MethodNode m = methods.next();
     
      if (m.name.equals(names.get("block_breakBlock_func")) && m.desc.equals(names.get("block_breakBlock_desc"))) {
        System.out.println("Found global block break call: " + m.name + m.desc );
       
        InsnList toAdd = new InsnList();
       
        LabelNode lmmnode = new LabelNode(new Label());
       
        toAdd.add(new VarInsnNode(Opcodes.ALOAD, 1));
        toAdd.add(new VarInsnNode(Opcodes.ILOAD, 2));
        toAdd.add(new VarInsnNode(Opcodes.ILOAD, 3));
        toAdd.add(new VarInsnNode(Opcodes.ILOAD, 4));
        toAdd.add(new VarInsnNode(Opcodes.ILOAD, 5));
        toAdd.add(new VarInsnNode(Opcodes.ILOAD, 6));
        toAdd.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "keepcalm/mods/events/ForgeEventHelper", "onBlockBreak", "(L" + names.get("world_javaName") + ";IIIII)V"));
        toAdd.add(lmmnode);
        // insert at the beginning
        m.instructions.insert(toAdd);
        System.out.println("Inserted instructions!");
        // done!
        break;
View Full Code Here

            System.out.println("Does " + f.owner + " equal " + names.get("blockDispenser_JavaName") + "?");



            System.out.println("Found landmark! Inserting code...");
            InsnList toAdd = new InsnList();

            // mark end of our code
            LabelNode lmmnode = new LabelNode(new Label());
            // we want 1, 2, 3, 4, 8 - world, x, y, z, ItemStack
            toAdd.add(new VarInsnNode(Opcodes.ALOAD, 1));
            toAdd.add(new VarInsnNode(Opcodes.ILOAD, 2));
            toAdd.add(new VarInsnNode(Opcodes.ILOAD, 3));
            toAdd.add(new VarInsnNode(Opcodes.ILOAD, 4));
            toAdd.add(new VarInsnNode(Opcodes.ALOAD, 8));
            toAdd.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "keepcalm/mods/events/ForgeEventHelper", "onDispenseItem",
                "(L" + names.get("world_javaName") + ";IIIL" + names.get("itemStack_javaName") + ";)Z"));

            LabelNode endLabel = new LabelNode(new Label());
            toAdd.add(new JumpInsnNode(Opcodes.IFEQ, endLabel)); // if the return value of ^ is true
            // then return - it was cancelled - and so will not be run
            toAdd.add(new InsnNode(Opcodes.RETURN));
            toAdd.add(endLabel); // otherwise, continue on
            toAdd.add(lmmnode);

            System.out.println("Instructions have been compiled, adding to bytecode...");

            m.instructions.insertBefore(i.getNext(), toAdd);
View Full Code Here

          // return integer (or boolean), load integer (or boolean)
          if (instr.getOpcode() == Opcodes.ILOAD && instr.getPrevious().getOpcode() == Opcodes.IRETURN) {
            System.out.println("Found IRETURN after ILOAD, inserting code before...");
            index++;
            InsnList toInject = new InsnList();
            /*toInject.add(new FieldInsnNode(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"));
            toInject.add(new VarInsnNode(Opcodes.ALOAD, 0));
            toInject.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/Object;)V"));
            toInject.add(new FieldInsnNode(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"));
            toInject.add(new VarInsnNode(Opcodes.ALOAD, 1));
            toInject.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/Object;)V"));*/
            LabelNode lmmnode = new LabelNode(new Label());
            // this
            toInject.add(new VarInsnNode(Opcodes.ALOAD, 0));
            // entityplayer
            toInject.add(new VarInsnNode(Opcodes.ALOAD, 1));
            // world
            toInject.add(new VarInsnNode(Opcodes.ALOAD, 2));
            // x y z direction
            toInject.add(new VarInsnNode(Opcodes.ILOAD, 3));
            toInject.add(new VarInsnNode(Opcodes.ILOAD, 4));
            toInject.add(new VarInsnNode(Opcodes.ILOAD, 5));
            toInject.add(new VarInsnNode(Opcodes.ILOAD, 6));
            toInject.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "keepcalm/mods/events/ForgeEventHelper", "onItemUse", String.format("(L%s;L%s;L%s;IIII)V", new Object[] {names.get("itemStack_javaName"), names.get("entityPlayer_javaName"), names.get("world_javaName")})));
            toInject.add(lmmnode);
            m.instructions.insertBefore(m.instructions.get(index), toInject);
            //System.out.println("Used desc: " + String.format("(L%s;L%s;L%s;IIII)V", new Object[] {names.get("itemStackJavaName"), names.get("entityPlayerJavaName"), names.get("worldJavaName")}));
            System.out.println("Finished patching ItemStack! - Inserted before " + index + ", toInject: " + toInject);
            break;
          }
View Full Code Here

            int loc = index + 1;
            System.out.println("Will insert code at: " + loc);
            // after, not at the same location
            LabelNode lmmnode = new LabelNode(new Label());

            InsnList toInject = new InsnList();

            // load 'this'
            toInject.add(new VarInsnNode(Opcodes.ALOAD, 0));
            // call the helper method
            System.out.println("Using desc: " + "(L" + names.get("itemInWorldManager_javaName") + ";)V");
            toInject.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "keepcalm/mods/events/ForgeEventHelper",
                "onBlockDamage", "(L" + names.get("itemInWorldManager_javaName") + ";)Z"));
            LabelNode endIf = new LabelNode(new Label());
            toInject.add(new JumpInsnNode(Opcodes.IFEQ, endIf));
            toInject.add(new InsnNode(Opcodes.RETURN));
            toInject.add(endIf);
            toInject.add(lmmnode);
            System.out.println("Finished compiling instruction nodes, inserting new instructions... at " + loc);

            m.instructions.insertBefore(m.instructions.get(index + 1), toInject);
            /*for (int i1 = 0; i1 < m.instructions.size(); i1++) {
              System.out.println("Location " + i1 + ": " + m.instructions.get(i1).getClass().getName());
View Full Code Here

TOP

Related Classes of org.objectweb.asm.tree.InsnList

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.