use of com.oracle.truffle.llvm.runtime.LLVMContext.ExternalLibrary in project sulong by graalvm.
the class Runner method parse.
private LLVMParserResult parse(LLVMLanguage language, LLVMContext context, Source source, ExternalLibrary library, ByteBuffer bytes) throws IOException {
assert library != null;
BitcodeParserResult bitcodeParserResult = BitcodeParserResult.getFromSource(source, bytes);
context.addLibraryPaths(bitcodeParserResult.getLibraryPaths());
List<String> libraries = bitcodeParserResult.getLibraries();
if (!libraries.isEmpty()) {
ExternalLibrary[] libs = context.addExternalLibraries(libraries);
LLVMParserResult[] parserResults = parse(language, context, libs);
handleParserResult(context, parserResults);
}
return LLVMParserRuntime.parse(source, library, bitcodeParserResult, language, context, nodeFactory);
}
use of com.oracle.truffle.llvm.runtime.LLVMContext.ExternalLibrary in project sulong by graalvm.
the class Runner method parse.
private LLVMParserResult[] parse(LLVMLanguage language, LLVMContext context, ExternalLibrary[] libs) {
LLVMParserResult[] parserResults = new LLVMParserResult[libs.length];
for (int i = 0; i < libs.length; i++) {
ExternalLibrary lib = libs[i];
if (!lib.isParsed()) {
try {
Path path = lib.getPath();
byte[] bytes = Files.readAllBytes(path);
// at the moment, we don't need the bitcode as the content of the source
Source source = Source.newBuilder(path.toString()).mimeType(LLVMLanguage.LLVM_BITCODE_MIME_TYPE).name(path.getFileName().toString()).build();
parserResults[i] = parse(language, context, source, lib, ByteBuffer.wrap(bytes));
lib.setParsed();
} catch (Throwable t) {
throw new RuntimeException("Error while trying to parse " + lib.getName(), t);
}
}
}
return parserResults;
}
use of com.oracle.truffle.llvm.runtime.LLVMContext.ExternalLibrary in project sulong by graalvm.
the class LLVMParserRuntime method checkReplaceExistingFunction.
private boolean checkReplaceExistingFunction(LLVMFunctionDescriptor functionDescriptor) {
if (library.getLibrariesToReplace() != null) {
for (ExternalLibrary lib : library.getLibrariesToReplace()) {
if (functionDescriptor.getLibrary().equals(lib)) {
// We already have a symbol defined in another library but now we are
// overwriting it. We rename the already existing symbol by prefixing it with
// "__libName_", e.g., "@__clock_gettime" would be renamed to
// "@__libc___clock_gettime".
String functionName = functionDescriptor.getName();
assert functionName.charAt(0) == '@';
String renamedFunctionName = "@__" + functionDescriptor.getLibrary().getName() + "_" + functionName.substring(1);
LLVMFunctionDescriptor renamedFunctionDescriptor = scope.lookupOrCreateFunction(functionDescriptor.getContext(), renamedFunctionName, true, index -> LLVMFunctionDescriptor.createDescriptor(functionDescriptor.getContext(), functionDescriptor.getLibrary(), renamedFunctionName, functionDescriptor.getType(), index));
renamedFunctionDescriptor.declareInSulong(functionDescriptor.getFunction(), functionDescriptor.isWeak(), false);
functionDescriptor.setLibrary(library);
return true;
}
}
}
return false;
}
use of com.oracle.truffle.llvm.runtime.LLVMContext.ExternalLibrary in project sulong by graalvm.
the class LLVMParserRuntime method parse.
public static LLVMParserResult parse(Source source, ExternalLibrary library, BitcodeParserResult parserResult, LLVMLanguage language, LLVMContext context, NodeFactory nodeFactory) {
ModelModule model = parserResult.getModel();
TargetDataLayout layout = model.getTargetDataLayout();
assert layout != null;
LLVMModelVisitor module = new LLVMModelVisitor();
model.accept(module);
DataLayoutConverter.DataSpecConverterImpl targetDataLayout = DataLayoutConverter.getConverter(layout.getDataLayout());
context.setDataLayoutConverter(targetDataLayout);
LLVMParserRuntime runtime = new LLVMParserRuntime(source, library, language, context, nodeFactory, module.getAliases());
runtime.registerFunctions(model);
LLVMSymbolReadResolver symbolResolver = new LLVMSymbolReadResolver(runtime, runtime.getGlobalFrameDescriptor());
LLVMExpressionNode[] globals = runtime.createGlobalVariableInitializationNodes(symbolResolver, module.getGlobals());
RootNode globalVarInits = nodeFactory.createStaticInitsRootNode(runtime, globals);
RootCallTarget globalVarInitsTarget = Truffle.getRuntime().createCallTarget(globalVarInits);
LLVMExpressionNode[] deallocs = runtime.getDeallocations();
RootNode globalVarDeallocs = nodeFactory.createStaticInitsRootNode(runtime, deallocs);
RootCallTarget globalVarDeallocsTarget = Truffle.getRuntime().createCallTarget(globalVarDeallocs);
RootCallTarget constructorFunctions = runtime.getConstructors(module.getGlobals());
RootCallTarget destructorFunctions = runtime.getDestructors(module.getGlobals());
if (context.getEnv().getOptions().get(SulongEngineOption.ENABLE_LVI)) {
final LLVMSourceContext sourceContext = context.getSourceContext();
model.getSourceGlobals().forEach((symbol, irValue) -> {
final LLVMExpressionNode node = symbolResolver.resolve(irValue);
final LLVMDebugValue value = nodeFactory.createDebugStaticValue(node);
sourceContext.registerStatic(symbol, value);
});
model.getSourceStaticMembers().forEach(((type, symbol) -> {
final LLVMExpressionNode node = symbolResolver.resolve(symbol);
final LLVMDebugValue value = nodeFactory.createDebugStaticValue(node);
type.setValue(value);
}));
}
RootCallTarget mainFunctionCallTarget = null;
if (runtime.getScope().functionExists("@main")) {
LLVMFunctionDescriptor mainDescriptor = runtime.getScope().getFunctionDescriptor("@main");
LLVMFunctionDescriptor startDescriptor = runtime.getScope().getFunctionDescriptor("@_start");
RootCallTarget startCallTarget = startDescriptor.getLLVMIRFunction();
String applicationPath = source.getPath() == null ? "" : source.getPath().toString();
RootNode globalFunction = nodeFactory.createGlobalRootNode(runtime, startCallTarget, mainDescriptor, applicationPath);
RootCallTarget globalFunctionRoot = Truffle.getRuntime().createCallTarget(globalFunction);
RootNode globalRootNode = nodeFactory.createGlobalRootNodeWrapping(runtime, globalFunctionRoot, startDescriptor.getType().getReturnType());
mainFunctionCallTarget = Truffle.getRuntime().createCallTarget(globalRootNode);
}
return new LLVMParserResult(runtime.getScope(), mainFunctionCallTarget, globalVarInitsTarget, globalVarDeallocsTarget, constructorFunctions, destructorFunctions);
}
use of com.oracle.truffle.llvm.runtime.LLVMContext.ExternalLibrary in project sulong by graalvm.
the class NFIContextExtension method addLibraries.
private void addLibraries(LLVMContext context) {
CompilerAsserts.neverPartOfCompilation();
context.addExternalLibrary("libsulong." + getNativeLibrarySuffix());
// dummy library for C++, see {@link #handleSpecialLibraries}
context.addExternalLibrary("libsulong++." + getNativeLibrarySuffix());
List<ExternalLibrary> libraries = context.getExternalLibraries(lib -> lib.getPath().toString().contains("." + getNativeLibrarySuffix()));
for (ExternalLibrary l : libraries) {
addLibrary(l);
}
}
Aggregations