1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package openjdk.jcov.filter.simplemethods;
  26 
  27 import org.objectweb.asm.tree.AbstractInsnNode;
  28 import org.objectweb.asm.tree.InsnList;
  29 
  30 import java.util.Arrays;
  31 
  32 import static org.objectweb.asm.Opcodes.*;
  33 
  34 public class Utils {
  35     private final static int[] SIMPLE_INSTRUCTIONS = new int[]{DUP, LDC,
  36             BALOAD, CALOAD, AALOAD, DALOAD, FALOAD, IALOAD, SALOAD,
  37             ACONST_NULL,
  38             ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4, ICONST_5, ICONST_M1,
  39             LCONST_0, LCONST_1,
  40             FCONST_0, FCONST_1, FCONST_2,
  41             DCONST_0, DCONST_1,
  42             ALOAD, ILOAD, FLOAD, LLOAD, DLOAD,
  43             GETFIELD, GETSTATIC,
  44             BIPUSH, SIPUSH};
  45     private final static int[] INVOKE_INSTRUCTIONS = new int[]{INVOKEVIRTUAL, INVOKEINTERFACE, INVOKESTATIC,
  46             INVOKEDYNAMIC, INVOKESPECIAL};
  47     private final static int[] RETURN_INSTRUCTIONS = new int[]{RETURN, ARETURN, IRETURN, FRETURN, LRETURN, DRETURN};
  48 
  49     static {
  50         Arrays.sort(SIMPLE_INSTRUCTIONS);
  51         Arrays.sort(INVOKE_INSTRUCTIONS);
  52         Arrays.sort(RETURN_INSTRUCTIONS);
  53     }
  54 
  55     public static int countInstructions(InsnList list) {
  56         int count = 0;
  57         for (int i = 0; i < list.size(); i++) {
  58             if (list.get(i).getOpcode() > 0) count++;
  59         }
  60         return count;
  61     }
  62     public static AbstractInsnNode getInstruction(InsnList list, int index) {
  63         return list.get(getInstructionIndex(list, index));
  64     }
  65     public static int getInstructionIndex(InsnList list, int index) {
  66         int count = 0;
  67         for (int i = 0; i < list.size(); i++) {
  68             if (list.get(i).getOpcode() > 0) {
  69                 if(count == index) return i;
  70                 count++;
  71             }
  72         }
  73         throw new IllegalStateException();
  74     }
  75 
  76     /**
  77      * An instruction is called "simple" if its only effect is to bring values onto the stack from stack, variables, fields, constants, etc.
  78      * @param opCode
  79      * @return
  80      */
  81     public static boolean isSimpleInstruction(int opCode) {
  82         return Arrays.binarySearch(SIMPLE_INSTRUCTIONS, opCode) >= 0;
  83     }
  84     public static boolean isReturnInstruction(int opCode) {
  85         return Arrays.binarySearch(RETURN_INSTRUCTIONS, opCode) >= 0;
  86     }
  87     public static boolean isInvokeInstruction(int opCode) {
  88         return Arrays.binarySearch(INVOKE_INSTRUCTIONS, opCode) >= 0;
  89     }
  90 }