1/*
2 * Copyright (c) 2016, 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
33public class MiscUtils {
34
35    /**
36     * Name a java method with class and signature to make it unique.
37     *
38     * @param method to generate unique identifier for
39     * @return Unique name for this method including class and signature
40     **/
41    public static String uniqueMethodName(ResolvedJavaMethod method) {
42        String className = method.getDeclaringClass().toClassName();
43        String name = className + "." + method.getName() + method.getSignature().toMethodDescriptor();
44        return name;
45    }
46
47    public static boolean isStaticCall(Call call) {
48        if (isJavaCall(call)) {
49            return ((getByteCode(call) & 0xFF) == Bytecodes.INVOKESTATIC);
50        }
51        return false;
52    }
53
54    public 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    public static boolean isVirtualCall(CompiledMethodInfo methodInfo, Call call) {
69        return isInvokeVirtual(call) && !methodInfo.hasMark(call, MarkId.INVOKESPECIAL);
70    }
71
72    public 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}
104