package jdk.tools.jaotc;
import java.util.ArrayList;
import org.graalvm.compiler.code.CompilationResult;
import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage;
import org.graalvm.compiler.options.OptionValues;
import jdk.tools.jaotc.binformat.BinaryContainer;
import jdk.tools.jaotc.binformat.CodeContainer;
import jdk.tools.jaotc.binformat.Symbol;
import jdk.vm.ci.amd64.AMD64;
import jdk.vm.ci.code.TargetDescription;
import jdk.vm.ci.code.site.Call;
import jdk.vm.ci.code.site.Infopoint;
import jdk.vm.ci.code.site.InfopointReason;
import jdk.vm.ci.meta.ResolvedJavaMethod;
final class CodeSectionProcessor {
private final OptionValues optionValues;
private final TargetDescription target;
private final BinaryContainer binaryContainer;
CodeSectionProcessor(DataBuilder dataBuilder) {
this.target = dataBuilder.getBackend().getTarget();
this.binaryContainer = dataBuilder.getBinaryContainer();
this.optionValues = dataBuilder.getBackend().getRuntime().getOptions();
}
void process(AOTCompiledClass compClass) {
ArrayList<CompiledMethodInfo> compiledMethods = compClass.getCompiledMethods();
for (CompiledMethodInfo methodInfo : compiledMethods) {
CompilationResult compResult = methodInfo.getCompilationResult();
byte[] targetCode = compResult.getTargetCode();
int targetCodeSize = compResult.getTargetCodeSize();
JavaMethodInfo compMethod = methodInfo.getMethodInfo();
for (Infopoint infopoint : compResult.getInfopoints()) {
if (infopoint.reason == InfopointReason.CALL) {
final Call callInfopoint = (Call) infopoint;
if (callInfopoint.target instanceof HotSpotForeignCallLinkage &&
target.arch instanceof AMD64) {
int destOffset = infopoint.pcOffset + callInfopoint.size - 4;
targetCode[destOffset + 0] = 0;
targetCode[destOffset + 1] = 0;
targetCode[destOffset + 2] = 0;
targetCode[destOffset + 3] = 0;
}
}
}
String entry = compMethod.getSymbolName();
assert entry != null : "missing name for compiled method";
CodeContainer codeSection = binaryContainer.getCodeContainer();
int codeIdOffset = BinaryContainer.alignUp(codeSection, binaryContainer.getCodeSegmentSize());
methodInfo.setCodeId();
binaryContainer.appendIntToCode(methodInfo.getCodeId());
int textBaseOffset = BinaryContainer.alignUp(codeSection, binaryContainer.getCodeEntryAlignment());
codeSection.createSymbol(textBaseOffset, Symbol.Kind.JAVA_FUNCTION, Symbol.Binding.LOCAL, targetCodeSize, entry);
methodInfo.setTextSectionOffset(textBaseOffset);
binaryContainer.appendCodeBytes(targetCode, 0, targetCodeSize);
int currentStubOffset = BinaryContainer.alignUp(codeSection, 8);
methodInfo.setStubsOffset(currentStubOffset - textBaseOffset);
for (Infopoint infopoint : compResult.getInfopoints()) {
if (infopoint.reason == InfopointReason.CALL) {
final Call callInfopoint = (Call) infopoint;
if (callInfopoint.target instanceof ResolvedJavaMethod) {
ResolvedJavaMethod call = (ResolvedJavaMethod) callInfopoint.target;
StubInformation stub = addCallStub(CallInfo.isVirtualCall(methodInfo, callInfopoint));
String targetSymbol = JavaMethodInfo.uniqueMethodName(call) + ".at." + infopoint.pcOffset;
methodInfo.addStubCode(targetSymbol, stub);
currentStubOffset += stub.getSize();
}
}
}
assert currentStubOffset == codeSection.getByteStreamSize() : "wrong offset";
binaryContainer.addCodeSegments(codeIdOffset, currentStubOffset);
}
}
private StubInformation addCallStub(boolean isVirtualCall) {
final int startOffset = binaryContainer.getCodeContainer().getByteStreamSize();
StubInformation stub = new StubInformation(startOffset, isVirtualCall);
ELFMacroAssembler masm = ELFMacroAssembler.getELFMacroAssembler(target, optionValues);
byte[] code;
if (isVirtualCall) {
code = masm.getPLTVirtualEntryCode(stub);
} else {
code = masm.getPLTStaticEntryCode(stub);
}
binaryContainer.appendCodeBytes(code, 0, code.length);
return stub;
}
}