use of com.oracle.truffle.llvm.parser.model.ModelModule in project sulong by graalvm.
the class LLVMScanner method parse.
public static ModelModule parse(Source source, ByteBuffer bytes) {
final ModelModule model = new ModelModule();
ByteBuffer b = bytes.duplicate();
b.order(ByteOrder.LITTLE_ENDIAN);
ByteBuffer bitcode;
long magicWord = Integer.toUnsignedLong(b.getInt());
if (Long.compareUnsigned(magicWord, BC_MAGIC_WORD) == 0) {
bitcode = b;
} else if (magicWord == WRAPPER_MAGIC_WORD) {
// Version
b.getInt();
// Offset32
long offset = b.getInt();
// Size32
long size = b.getInt();
b.position((int) offset);
b.limit((int) (offset + size));
bitcode = b.slice();
} else if (magicWord == ELF_MAGIC_WORD) {
ElfFile elfFile = ElfFile.create(b);
Entry llvmbc = elfFile.getSectionHeaderTable().getEntry(".llvmbc");
if (llvmbc == null) {
throw new RuntimeException("ELF File does not contain an .llvmbc section.");
}
ElfDynamicSection dynamicSection = elfFile.getDynamicSection();
if (dynamicSection != null) {
List<String> libraries = dynamicSection.getDTNeeded();
List<String> paths = dynamicSection.getDTRPath();
model.addLibraries(libraries);
model.addLibraryPaths(paths);
}
long offset = llvmbc.getOffset();
long size = llvmbc.getSize();
b.position((int) offset);
b.limit((int) (offset + size));
bitcode = b.slice();
} else {
throw new RuntimeException("Not a valid input file!");
}
parseBitcodeBlock(source, bitcode, model);
return model;
}
use of com.oracle.truffle.llvm.parser.model.ModelModule 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);
}
Aggregations