package jdk.tools.jaotc;
import static jdk.tools.jaotc.AOTCompiledClass.getType;
import static jdk.tools.jaotc.AOTCompiledClass.metadataName;
import java.util.ArrayList;
import java.util.List;
import org.graalvm.compiler.code.CompilationResult;
import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
import org.graalvm.compiler.hotspot.HotSpotGraalServices;
import jdk.tools.jaotc.binformat.BinaryContainer;
import jdk.tools.jaotc.binformat.ByteContainer;
import jdk.tools.jaotc.binformat.GotSymbol;
import jdk.tools.jaotc.utils.NativeOrderOutputStream;
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;
final 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) {
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();
byte[] implicitExceptionBytes = HotSpotGraalServices.getImplicitExceptionBytes(metaData);
byte[] exceptionBytes = metaData.exceptionBytes();
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 implictTableOffset = null;
if (implicitExceptionBytes != null) {
implictTableOffset = 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(exceptionBytes).align(8);
if (implicitExceptionBytes != null) {
implictTableOffset.set(metadataStream.position());
metadataStream.put(implicitExceptionBytes).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) {
Object[] metaDataEntries = metaData.metadataEntries();
if (metaDataEntries.length == 0) {
return 0;
}
int metadataGotSlotsStart = binaryContainer.getMetadataGotContainer().getByteStreamSize();
for (int index = 0; index < metaDataEntries.length; index++) {
Object ref = metaDataEntries[index];
String name = metadataName(ref);
addMetadataEntry(binaryContainer, name);
assert AOTCompiledClass.getAOTKlassData(getType(ref)) != null;
assert methodInfo.getDependentKlassData(getType(ref)) != 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 (CompilationResult.CodeMark mark : compilationResult.getMarks()) {
markProcessor.process(methodInfo, mark);
}
for (DataPatch dataPatch : compilationResult.getDataPatches()) {
dataPatchProcessor.process(methodInfo, dataPatch);
}
}
}
}