package jdk.tools.jaotc;
import org.graalvm.compiler.bytecode.Bytecodes;
import jdk.vm.ci.code.BytecodePosition;
import jdk.vm.ci.code.site.Call;
import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaMethod;
final class CallInfo {
static boolean isStaticTarget(Call call) {
return !((HotSpotResolvedJavaMethod)call.target).hasReceiver();
}
private static boolean isStaticOpcode(Call call) {
int opcode = getByteCode(call) & 0xFF;
return opcode == Bytecodes.INVOKESTATIC || opcode == Bytecodes.INVOKEDYNAMIC || opcode == Bytecodes.INVOKEVIRTUAL ;
}
static boolean isStaticCall(Call call) {
if (isJavaCall(call) && isStaticTarget(call)) {
assert isStaticOpcode(call);
return true;
}
return false;
}
static boolean isSpecialCall(Call call) {
if (isJavaCall(call)) {
return ((getByteCode(call) & 0xFF) == Bytecodes.INVOKESPECIAL);
}
return false;
}
private static boolean isInvokeVirtual(Call call) {
if (isJavaCall(call)) {
return ((getByteCode(call) & 0xFF) == Bytecodes.INVOKEVIRTUAL) || ((getByteCode(call) & 0xFF) == Bytecodes.INVOKEINTERFACE);
}
return false;
}
static boolean isVirtualCall(CompiledMethodInfo methodInfo, Call call) {
return isInvokeVirtual(call) && !methodInfo.hasMark(call, MarkId.INVOKESPECIAL) && !isStaticTarget(call);
}
static boolean isOptVirtualCall(CompiledMethodInfo methodInfo, Call call) {
return isInvokeVirtual(call) && methodInfo.hasMark(call, MarkId.INVOKESPECIAL);
}
private static boolean isJavaCall(Call call) {
if (call.debugInfo == null) {
return false;
}
BytecodePosition bcpos = call.debugInfo.getBytecodePosition();
ResolvedJavaMethod method = bcpos.getMethod();
if (bcpos.getBCI() < 0) {
return false;
}
if (method == null) {
return false;
}
assert (method instanceof HotSpotResolvedJavaMethod) : "Not a resolved Java call";
return true;
}
private static byte getByteCode(Call call) {
ResolvedJavaMethod m = call.debugInfo.getBytecodePosition().getMethod();
int callPosition = call.debugInfo.getBytecodePosition().getBCI();
byte[] code = m.getCode();
return code[callPosition];
}
}