package jdk.tools.jaotc;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import jdk.tools.jaotc.binformat.BinaryContainer;
import jdk.tools.jaotc.binformat.ByteContainer;
import jdk.tools.jaotc.binformat.HeaderContainer;
import jdk.tools.jaotc.utils.Timer;
import org.graalvm.compiler.code.CompilationResult;
import org.graalvm.compiler.debug.Debug;
import org.graalvm.compiler.debug.Debug.Scope;
import org.graalvm.compiler.hotspot.HotSpotHostBackend;
import org.graalvm.compiler.hotspot.stubs.Stub;
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
import jdk.vm.ci.hotspot.HotSpotVMConfigStore;
import jdk.vm.ci.hotspot.VMField;
class DataBuilder {
private final Main main;
private final HotSpotHostBackend backend;
private final List<AOTCompiledClass> classes;
private final BinaryContainer binaryContainer;
private final HashMap<Long, String> vmAddresses = new HashMap<>();
public DataBuilder(Main main, HotSpotHostBackend backend, List<AOTCompiledClass> classes, BinaryContainer binaryContainer) {
this.main = main;
this.backend = backend;
this.classes = classes;
this.binaryContainer = binaryContainer;
fillVMAddresses(HotSpotJVMCIRuntime.runtime().getConfigStore());
}
private void fillVMAddresses(HotSpotVMConfigStore config) {
for (VMField vmField : config.getFields().values()) {
if (vmField.value != null && vmField.value instanceof Long) {
final long address = (Long) vmField.value;
String value = vmField.name;
if (address != 0) {
vmAddresses.put(address, value);
}
}
}
for (Entry<String, Long> vmAddress : config.getAddresses().entrySet()) {
final long address = vmAddress.getValue();
String value = vmAddress.getKey();
String old = vmAddresses.put(address, value);
if (old != null) {
throw new InternalError("already in map: address: " + address + ", current: " + value + ", old: " + old);
}
}
}
public String getVMFunctionNameForAddress(long address) {
return vmAddresses.get(address);
}
public HotSpotHostBackend getBackend() {
return backend;
}
public BinaryContainer getBinaryContainer() {
return binaryContainer;
}
@SuppressWarnings("try")
public void prepareData() throws Exception {
try (Timer t = new Timer(main, "Parsing compiled code")) {
CodeSectionProcessor codeSectionProcessor = new CodeSectionProcessor(this);
for (AOTCompiledClass c : classes) {
c.addAOTKlassData(binaryContainer);
codeSectionProcessor.process(c);
}
}
AOTCompiledClass stubCompiledCode = retrieveStubCode();
try (Timer t = main.options.verbose ? new Timer(main, "Freeing memory") : null) {
main.printMemoryUsage();
System.gc();
}
MetadataBuilder metadataBuilder = null;
try (Timer t = new Timer(main, "Processing metadata")) {
metadataBuilder = new MetadataBuilder(this);
metadataBuilder.processMetadata(classes, stubCompiledCode);
}
try (Timer t = main.options.verbose ? new Timer(main, "Freeing memory") : null) {
main.printMemoryUsage();
System.gc();
}
try (Timer t = new Timer(main, "Preparing stubs binary")) {
prepareStubsBinary(stubCompiledCode);
}
try (Timer t = new Timer(main, "Preparing compiled binary")) {
prepareCompiledBinary(metadataBuilder);
}
}
@SuppressWarnings("try")
private AOTCompiledClass retrieveStubCode() {
ArrayList<CompiledMethodInfo> stubs = new ArrayList<>();
for (Stub stub : Stub.getStubs()) {
try (Scope scope = Debug.scope("CompileStubs")) {
CompilationResult result = stub.getCompilationResult(backend);
CompiledMethodInfo cm = new CompiledMethodInfo(result, new AOTStub(stub, backend));
stubs.add(cm);
} catch (Throwable e) {
throw Debug.handle(e);
}
}
AOTCompiledClass stubCompiledCode = new AOTCompiledClass(stubs);
CodeSectionProcessor codeSectionProcessor = new CodeSectionProcessor(this);
codeSectionProcessor.process(stubCompiledCode);
return stubCompiledCode;
}
private void prepareCompiledBinary(MetadataBuilder metadataBuilder) {
for (AOTCompiledClass c : classes) {
c.putMethodsData(binaryContainer);
}
AOTCompiledClass.putAOTKlassData(binaryContainer);
HeaderContainer header = binaryContainer.getHeaderContainer();
header.setClassesCount(AOTCompiledClass.getClassesCount());
header.setMethodsCount(CompiledMethodInfo.getMethodsCount());
ByteContainer bc = binaryContainer.getMetaspaceGotContainer();
header.setMetaspaceGotSize((bc.getByteStreamSize() / 8));
bc = binaryContainer.getMetadataGotContainer();
header.setMetadataGotSize((bc.getByteStreamSize() / 8));
bc = binaryContainer.getOopGotContainer();
header.setOopGotSize((bc.getByteStreamSize() / 8));
}
private void prepareStubsBinary(AOTCompiledClass compiledClass) {
ArrayList<CompiledMethodInfo> compiledStubs = compiledClass.getCompiledMethods();
int cntStubs = compiledStubs.size();
binaryContainer.addMethodsCount(cntStubs, binaryContainer.getStubsOffsetsContainer());
for (CompiledMethodInfo methodInfo : compiledStubs) {
methodInfo.addMethodOffsets(binaryContainer, binaryContainer.getStubsOffsetsContainer());
}
}
}