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