package jdk.tools.jaotc;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.HashMap;
import java.util.HashSet;
import jdk.tools.jaotc.binformat.BinaryContainer;
import jdk.tools.jaotc.binformat.ReadOnlyDataContainer;
import jdk.tools.jaotc.AOTCompiledClass.AOTKlassData;
import org.graalvm.compiler.code.CompilationResult;
import jdk.vm.ci.code.site.Mark;
import jdk.vm.ci.code.site.Site;
import jdk.vm.ci.hotspot.HotSpotCompiledCode;
import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
final class CompiledMethodInfo {
private static final int UNINITIALIZED_OFFSET = -1;
private static class AOTMethodOffsets {
private int nameOffset;
private int textSectionOffset;
private int metadataOffset;
private int metadataGotOffset;
private int metadataGotSize;
private int codeId;
AOTMethodOffsets() {
this.nameOffset = UNINITIALIZED_OFFSET;
this.textSectionOffset = UNINITIALIZED_OFFSET;
this.metadataOffset = UNINITIALIZED_OFFSET;
this.metadataGotOffset = UNINITIALIZED_OFFSET;
this.metadataGotSize = -1;
this.codeId = -1;
}
void addMethodOffsets(ReadOnlyDataContainer container, String name) {
verify(name);
container.appendInt(nameOffset).
appendInt(textSectionOffset).
appendInt(metadataOffset).
appendInt(metadataGotOffset).
appendInt(metadataGotSize).
appendInt(codeId);
}
private void verify(String name) {
assert nameOffset >= 0 : "incorrect nameOffset: " + nameOffset + " for method: " + name;
assert textSectionOffset > 0 : "incorrect textSectionOffset: " + textSectionOffset + " for method: " + name;
assert metadataOffset >= 0 : "incorrect metadataOffset: " + metadataOffset + " for method: " + name;
assert metadataGotOffset >= 0 : "incorrect metadataGotOffset: " + metadataGotOffset + " for method: " + name;
assert metadataGotSize >= 0 : "incorrect metadataGotSize: " + metadataGotSize + " for method: " + name;
assert codeId >= 0 : "incorrect codeId: " + codeId + " for method: " + name;
}
protected void setNameOffset(int offset) {
nameOffset = offset;
}
protected void setTextSectionOffset(int textSectionOffset) {
this.textSectionOffset = textSectionOffset;
}
protected int getTextSectionOffset() {
return textSectionOffset;
}
protected void setCodeId(int codeId) {
this.codeId = codeId;
}
protected int getCodeId() {
return codeId;
}
protected void setMetadataOffset(int offset) {
metadataOffset = offset;
}
protected void setMetadataGotOffset(int metadataGotOffset) {
this.metadataGotOffset = metadataGotOffset;
}
protected void setMetadataGotSize(int length) {
this.metadataGotSize = length;
}
}
private String name;
private CompilationResult compilationResult;
private JavaMethodInfo methodInfo;
private HotSpotCompiledCode code;
private int stubsOffset;
private int totalStubSize;
private AOTMethodOffsets methodOffsets;
private HashMap<String, StubInformation> stubs = new HashMap<>();
private HashSet<AOTKlassData> dependentKlasses = new HashSet<>();
private static final AtomicInteger methodsCount = new AtomicInteger();
CompiledMethodInfo(CompilationResult compilationResult, JavaMethodInfo methodInfo) {
this.name = methodInfo.getNameAndSignature();
this.compilationResult = compilationResult;
this.methodInfo = methodInfo;
this.stubsOffset = UNINITIALIZED_OFFSET;
this.methodOffsets = new AOTMethodOffsets();
}
String name() {
return name;
}
void addMethodOffsets(BinaryContainer binaryContainer, ReadOnlyDataContainer container) {
this.methodOffsets.setNameOffset(binaryContainer.addMetaspaceName(name));
this.methodOffsets.addMethodOffsets(container, name);
for (AOTKlassData data : dependentKlasses) {
data.addDependentMethod(this);
}
}
CompilationResult getCompilationResult() {
return compilationResult;
}
JavaMethodInfo getMethodInfo() {
return methodInfo;
}
void setTextSectionOffset(int textSectionOffset) {
methodOffsets.setTextSectionOffset(textSectionOffset);
}
public int getTextSectionOffset() {
return methodOffsets.getTextSectionOffset();
}
void setCodeId() {
methodOffsets.setCodeId(CompiledMethodInfo.getNextCodeId());
}
int getCodeId() {
return this.methodOffsets.getCodeId();
}
static int getMethodsCount() {
return methodsCount.get();
}
static int getNextCodeId() {
return methodsCount.getAndIncrement();
}
int getCodeSize() {
return stubsOffset + getStubCodeSize();
}
int getStubCodeSize() {
return totalStubSize;
}
void setMetadataOffset(int offset) {
this.methodOffsets.setMetadataOffset(offset);
}
void setStubsOffset(int offset) {
stubsOffset = offset;
}
int getStubsOffset() {
return stubsOffset;
}
void setMetadataGotOffset(int metadataGotOffset) {
this.methodOffsets.setMetadataGotOffset(metadataGotOffset);
}
void setMetadataGotSize(int length) {
this.methodOffsets.setMetadataGotSize(length);
}
void addStubCode(String call, StubInformation stub) {
stubs.put(call, stub);
totalStubSize += stub.getSize();
}
StubInformation getStubFor(String call) {
StubInformation stub = stubs.get(call);
assert stub != null : "missing stub for call " + call;
stub.verify();
return stub;
}
void addDependentKlassData(BinaryContainer binaryContainer, HotSpotResolvedObjectType type) {
AOTKlassData klassData = AOTCompiledClass.addFingerprintKlassData(binaryContainer, type);
dependentKlasses.add(klassData);
}
AOTKlassData getDependentKlassData(HotSpotResolvedObjectType type) {
AOTKlassData klassData = AOTCompiledClass.getAOTKlassData(type);
if (dependentKlasses.contains(klassData)) {
return klassData;
}
return null;
}
boolean hasMark(Site call, MarkId id) {
for (Mark m : compilationResult.getMarks()) {
int adjOffset = (m.pcOffset & (-8)) + 7;
if ((call.pcOffset == adjOffset) && MarkId.getEnum((int) m.id) == id) {
return true;
}
}
return false;
}
String asTag() {
return "[" + methodInfo.getSymbolName() + "]";
}
HotSpotCompiledCode compiledCode() {
if (code == null) {
code = methodInfo.compiledCode(compilationResult);
}
return code;
}
void clear() {
this.dependentKlasses = null;
this.name = null;
}
void clearCompileData() {
this.code = null;
this.stubs = null;
this.compilationResult = null;
this.methodInfo = null;
}
}