package jdk.tools.jaotc;
import java.util.ArrayList;
import java.util.List;
import jdk.tools.jaotc.binformat.BinaryContainer;
import jdk.tools.jaotc.binformat.ByteContainer;
import jdk.tools.jaotc.binformat.Symbol.Binding;
import jdk.tools.jaotc.binformat.Symbol.Kind;
import jdk.tools.jaotc.binformat.GotSymbol;
import jdk.tools.jaotc.AOTCompiledClass.AOTKlassData;
import jdk.tools.jaotc.utils.NativeOrderOutputStream;
import org.graalvm.compiler.code.CompilationResult;
import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
import jdk.vm.ci.code.StackSlot;
import jdk.vm.ci.code.site.DataPatch;
import jdk.vm.ci.code.site.Infopoint;
import jdk.vm.ci.code.site.Mark;
import jdk.vm.ci.hotspot.HotSpotCompiledCode;
import jdk.vm.ci.hotspot.HotSpotMetaData;
class MetadataBuilder {
private final DataBuilder dataBuilder;
private final BinaryContainer binaryContainer;
MetadataBuilder(DataBuilder dataBuilder) {
this.dataBuilder = dataBuilder;
this.binaryContainer = dataBuilder.getBinaryContainer();
}
void processMetadata(List<AOTCompiledClass> classes, AOTCompiledClass stubCompiledCode) {
binaryContainer.getMethodMetadataContainer().createSymbol(0, Kind.OBJECT, Binding.LOCAL, 0, "metaStart");
for (AOTCompiledClass c : classes) {
processMetadataClass(c);
}
processMetadataClass(stubCompiledCode);
}
private void processMetadataClass(AOTCompiledClass c) {
processInfopointsAndMarks(c);
createMethodMetadata(c);
}
private void createMethodMetadata(AOTCompiledClass compiledClass) {
HotSpotGraalRuntimeProvider runtime = dataBuilder.getBackend().getRuntime();
ByteContainer methodMetadataContainer = binaryContainer.getMethodMetadataContainer();
for (CompiledMethodInfo methodInfo : compiledClass.getCompiledMethods()) {
final int startOffset = methodMetadataContainer.getByteStreamSize();
assert startOffset % 8 == 0 : "Must be aligned on 8";
methodInfo.setMetadataOffset(startOffset);
HotSpotCompiledCode compiledMethod = methodInfo.compiledCode();
HotSpotMetaData metaData = new HotSpotMetaData(runtime.getTarget(), compiledMethod);
byte[] pcDesc = metaData.pcDescBytes();
byte[] scopeDesc = metaData.scopesDescBytes();
byte[] relocationInfo = metaData.relocBytes();
byte[] oopMapInfo = metaData.oopMaps();
NativeOrderOutputStream metadataStream = new NativeOrderOutputStream();
int codeSize = methodInfo.getCodeSize();
CodeOffsets co = CodeOffsets.buildFrom(methodInfo.getCompilationResult().getMarks());
int unverifiedEntry = co.entry();
int verifiedEntry = co.verifiedEntry();
int exceptionHandler = co.exceptionHandler();
int deoptHandler = co.deoptHandler();
int frameSize = methodInfo.getCompilationResult().getTotalFrameSize();
StackSlot deoptRescueSlot = methodInfo.getCompilationResult().getCustomStackArea();
int origPcOffset = deoptRescueSlot != null ? deoptRescueSlot.getOffset(frameSize) : -1;
int stubsOffset = methodInfo.getStubsOffset();
int offset = addMetadataEntries(binaryContainer, metaData, methodInfo);
methodInfo.setMetadataGotOffset(offset);
methodInfo.setMetadataGotSize(metaData.metadataEntries().length);
int unsafeAccess = methodInfo.getCompilationResult().hasUnsafeAccess() ? 1 : 0;
try {
NativeOrderOutputStream.PatchableInt totalSize = metadataStream.patchableInt();
metadataStream.putInt(codeSize).
putInt(unverifiedEntry).
putInt(verifiedEntry).
putInt(exceptionHandler).
putInt(deoptHandler).
putInt(stubsOffset).
putInt(frameSize).
putInt(origPcOffset).
putInt(unsafeAccess);
NativeOrderOutputStream.PatchableInt pcDescOffset = metadataStream.patchableInt();
NativeOrderOutputStream.PatchableInt scopeOffset = metadataStream.patchableInt();
NativeOrderOutputStream.PatchableInt relocationOffset = metadataStream.patchableInt();
NativeOrderOutputStream.PatchableInt exceptionOffset = metadataStream.patchableInt();
NativeOrderOutputStream.PatchableInt oopMapOffset = metadataStream.patchableInt();
metadataStream.align(8);
pcDescOffset.set(metadataStream.position());
metadataStream.put(pcDesc).align(8);
scopeOffset.set(metadataStream.position());
metadataStream.put(scopeDesc).align(8);
relocationOffset.set(metadataStream.position());
metadataStream.put(relocationInfo).align(8);
exceptionOffset.set(metadataStream.position());
metadataStream.put(metaData.exceptionBytes()).align(8);
oopMapOffset.set(metadataStream.position());
metadataStream.put(oopMapInfo).align(8);
totalSize.set(metadataStream.position());
byte[] data = metadataStream.array();
methodMetadataContainer.appendBytes(data, 0, data.length);
} catch (Exception e) {
throw new InternalError("Exception occurred during compilation of " + methodInfo.getMethodInfo().getSymbolName(), e);
}
methodInfo.clearCompileData();
}
}
private static int addMetadataEntries(BinaryContainer binaryContainer, HotSpotMetaData metaData, CompiledMethodInfo methodInfo) {
String[] metaDataEntries = metaData.metadataEntries();
if (metaDataEntries.length == 0) {
return 0;
}
int metadataGotSlotsStart = binaryContainer.getMetadataGotContainer().getByteStreamSize();
for (int index = 0; index < metaDataEntries.length; index++) {
String name = metaDataEntries[index];
addMetadataEntry(binaryContainer, name);
String klassName = name;
int len = name.length();
int parenthesesIndex = name.lastIndexOf('(', len - 1);
if (parenthesesIndex > 0) {
int dotIndex = name.lastIndexOf('.', parenthesesIndex - 1);
assert dotIndex > 0 : "method's full name should have '.' : " + name;
klassName = name.substring(0, dotIndex);
}
assert AOTCompiledClass.getAOTKlassData(klassName) != null;
assert methodInfo.getDependentKlassData(klassName) != null;
}
return metadataGotSlotsStart;
}
private static void addMetadataEntry(BinaryContainer binaryContainer, String name) {
int stringOffset = binaryContainer.addMetaspaceName(name);
binaryContainer.addMetadataGotEntry(stringOffset);
}
private void processInfopointsAndMarks(AOTCompiledClass compiledClass) {
ArrayList<CompiledMethodInfo> compiledMethods = compiledClass.getCompiledMethods();
MarkProcessor markProcessor = new MarkProcessor(dataBuilder);
DataPatchProcessor dataPatchProcessor = new DataPatchProcessor(dataBuilder);
InfopointProcessor infopointProcessor = new InfopointProcessor(dataBuilder);
for (CompiledMethodInfo methodInfo : compiledMethods) {
CompilationResult compilationResult = methodInfo.getCompilationResult();
String targetSymbol = "state.M" + methodInfo.getCodeId();
String gotName = "got." + targetSymbol;
GotSymbol symbol = binaryContainer.getMethodStateContainer().createGotSymbol(gotName);
assert (symbol.getIndex() == methodInfo.getCodeId()) : "wrong offset";
for (Infopoint infoPoint : compilationResult.getInfopoints()) {
infopointProcessor.process(methodInfo, infoPoint);
}
for (Mark mark : compilationResult.getMarks()) {
markProcessor.process(methodInfo, mark);
}
for (DataPatch dataPatch : compilationResult.getDataPatches()) {
dataPatchProcessor.process(methodInfo, dataPatch);
}
}
}
}