1 /*
   2  * Copyright (c) 2016, 2017, 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 package jdk.tools.jaotc;
  25 
  26 import org.graalvm.compiler.bytecode.Bytecodes;
  27 
  28 import jdk.vm.ci.code.BytecodePosition;
  29 import jdk.vm.ci.code.site.Call;
  30 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  31 import jdk.vm.ci.meta.ResolvedJavaMethod;
  32 
  33 final class CallInfo {
  34 
  35     static boolean isStaticTarget(Call call) {
  36         return !((HotSpotResolvedJavaMethod)call.target).hasReceiver();
  37     }
  38 
  39     private static boolean isStaticOpcode(Call call) {
  40         int opcode = getByteCode(call) & 0xFF;
  41         return opcode == Bytecodes.INVOKESTATIC || opcode == Bytecodes.INVOKEDYNAMIC || opcode == Bytecodes.INVOKEVIRTUAL /* invokehandle */;
  42     }
  43 
  44     static boolean isStaticCall(Call call) {
  45         if (isJavaCall(call) && isStaticTarget(call)) {
  46             assert isStaticOpcode(call);
  47             return true;
  48         }
  49         return false;
  50     }
  51 
  52     static boolean isSpecialCall(Call call) {
  53         if (isJavaCall(call)) {
  54             return ((getByteCode(call) & 0xFF) == Bytecodes.INVOKESPECIAL);
  55         }
  56         return false;
  57     }
  58 
  59     private static boolean isInvokeVirtual(Call call) {
  60         if (isJavaCall(call)) {
  61             return ((getByteCode(call) & 0xFF) == Bytecodes.INVOKEVIRTUAL) || ((getByteCode(call) & 0xFF) == Bytecodes.INVOKEINTERFACE);
  62         }
  63         return false;
  64     }
  65 
  66     static boolean isVirtualCall(CompiledMethodInfo methodInfo, Call call) {
  67         return isInvokeVirtual(call) && !methodInfo.hasMark(call, MarkId.INVOKESPECIAL) && !isStaticTarget(call);
  68     }
  69 
  70     static boolean isOptVirtualCall(CompiledMethodInfo methodInfo, Call call) {
  71         return isInvokeVirtual(call) && methodInfo.hasMark(call, MarkId.INVOKESPECIAL);
  72     }
  73 
  74     private static boolean isJavaCall(Call call) {
  75         // If there is no associated debug info return false
  76         if (call.debugInfo == null) {
  77             return false;
  78         }
  79         BytecodePosition bcpos = call.debugInfo.getBytecodePosition();
  80         ResolvedJavaMethod method = bcpos.getMethod();
  81         // If bytecode position indicates a special value (negative value) it is
  82         // not a normal java call
  83         if (bcpos.getBCI() < 0) {
  84             return false;
  85         }
  86         // If there is no method associated with the debuginfo, return false
  87         if (method == null) {
  88             return false;
  89         }
  90         assert (method instanceof HotSpotResolvedJavaMethod) : "Not a resolved Java call";
  91         return true;
  92     }
  93 
  94     private static byte getByteCode(Call call) {
  95         ResolvedJavaMethod m = call.debugInfo.getBytecodePosition().getMethod();
  96         int callPosition = call.debugInfo.getBytecodePosition().getBCI();
  97         byte[] code = m.getCode();
  98         return code[callPosition];
  99     }
 100 
 101 }