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