1 /*
   2  * Copyright (c) 2016, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 
  26 package jdk.tools.jaotc;
  27 
  28 import org.graalvm.compiler.bytecode.Bytecodes;
  29 
  30 import jdk.vm.ci.code.BytecodePosition;
  31 import jdk.vm.ci.code.site.Call;
  32 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  33 import jdk.vm.ci.meta.ResolvedJavaMethod;
  34 
  35 final class CallInfo {
  36 
  37     static boolean isStaticTarget(Call call) {
  38         return !((HotSpotResolvedJavaMethod) call.target).hasReceiver();
  39     }
  40 
  41     private static boolean isStaticOpcode(Call call) {
  42         int opcode = getByteCode(call) & 0xFF;
  43         return opcode == Bytecodes.INVOKESTATIC || opcode == Bytecodes.INVOKEDYNAMIC || opcode == Bytecodes.INVOKEVIRTUAL /* invokehandle */;
  44     }
  45 
  46     static boolean isStaticCall(Call call) {
  47         if (isJavaCall(call) && isStaticTarget(call)) {
  48             assert isStaticOpcode(call);
  49             return true;
  50         }
  51         return false;
  52     }
  53 
  54     static boolean isSpecialCall(Call call) {
  55         if (isJavaCall(call)) {
  56             return ((getByteCode(call) & 0xFF) == Bytecodes.INVOKESPECIAL);
  57         }
  58         return false;
  59     }
  60 
  61     private static boolean isInvokeVirtual(Call call) {
  62         if (isJavaCall(call)) {
  63             return ((getByteCode(call) & 0xFF) == Bytecodes.INVOKEVIRTUAL) || ((getByteCode(call) & 0xFF) == Bytecodes.INVOKEINTERFACE);
  64         }
  65         return false;
  66     }
  67 
  68     static boolean isVirtualCall(CompiledMethodInfo methodInfo, Call call) {
  69         return isInvokeVirtual(call) && !methodInfo.hasMark(call, MarkId.INVOKESPECIAL) && !isStaticTarget(call);
  70     }
  71 
  72     static boolean isOptVirtualCall(CompiledMethodInfo methodInfo, Call call) {
  73         return isInvokeVirtual(call) && methodInfo.hasMark(call, MarkId.INVOKESPECIAL);
  74     }
  75 
  76     private static boolean isJavaCall(Call call) {
  77         // If there is no associated debug info return false
  78         if (call.debugInfo == null) {
  79             return false;
  80         }
  81         BytecodePosition bcpos = call.debugInfo.getBytecodePosition();
  82         ResolvedJavaMethod method = bcpos.getMethod();
  83         // If bytecode position indicates a special value (negative value) it is
  84         // not a normal java call
  85         if (bcpos.getBCI() < 0) {
  86             return false;
  87         }
  88         // If there is no method associated with the debuginfo, return false
  89         if (method == null) {
  90             return false;
  91         }
  92         assert (method instanceof HotSpotResolvedJavaMethod) : "Not a resolved Java call";
  93         return true;
  94     }
  95 
  96     private static byte getByteCode(Call call) {
  97         ResolvedJavaMethod m = call.debugInfo.getBytecodePosition().getMethod();
  98         int callPosition = call.debugInfo.getBytecodePosition().getBCI();
  99         byte[] code = m.getCode();
 100         return code[callPosition];
 101     }
 102 
 103 }