Add a devonly function to the engine to load ir from files

This commit is contained in:
Sameer Rahmani 2022-07-01 22:30:49 +01:00
parent 5bec18b327
commit 3d1353dee4
3 changed files with 46 additions and 10 deletions

View File

@ -191,6 +191,7 @@ public:
}
};
llvm::Error loadModule(const char *file);
void dumpToObjectFile(llvm::StringRef filename);
};

View File

@ -21,11 +21,20 @@
namespace serene::types {
// ============================================================================
// Expression
// ============================================================================
struct Expression {
const unsigned char *data;
explicit Expression(const unsigned char *data) : data(data){};
};
// ============================================================================
// Internal String
// ============================================================================
/// Internal string represts a smaller type of string with limited set of
/// functionalities that we use only for internal usage
struct InternalString {
// We store the actual string in a "string" data section
const char *data;
@ -35,6 +44,9 @@ struct InternalString {
: data(data), len(len){};
};
// ============================================================================
// Symbol
// ============================================================================
struct Symbol {
const InternalString *ns;
const InternalString *name;
@ -43,6 +55,9 @@ struct Symbol {
: ns(ns), name(name){};
};
// ============================================================================
// Namespace
// ============================================================================
struct Namespace {
const InternalString *name;

View File

@ -42,14 +42,19 @@
#include <llvm/IR/DataLayout.h> // for DataL...
#include <llvm/IR/LLVMContext.h> // for LLVMC...
#include <llvm/IR/Module.h> // for Module
#include <llvm/Support/CodeGen.h> // for Level
#include <llvm/Support/FileSystem.h> // for OF_None
#include <llvm/Support/FormatVariadic.h> // for formatv
#include <llvm/Support/ToolOutputFile.h> // for ToolO...
#include <llvm/Support/raw_ostream.h> // for raw_o...
#include <llvm/IRReader/IRReader.h>
#include <llvm/Support/CodeGen.h> // for Level
#include <llvm/Support/Error.h>
#include <llvm/Support/FileSystem.h> // for OF_None
#include <llvm/Support/FormatVariadic.h> // for formatv
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/SourceMgr.h>
#include <llvm/Support/ToolOutputFile.h> // for ToolO...
#include <llvm/Support/raw_ostream.h> // for raw_o...
#include <algorithm> // for max
#include <assert.h> // for assert
#include <cerrno>
#include <cstring>
#include <gc.h>
#include <memory> // for uniqu...
@ -128,8 +133,6 @@ void Halley::pushJITDylib(types::Namespace &ns, llvm::orc::JITDylib *l) {
}
size_t Halley::getNumberOfJITDylibs(types::Namespace &ns) {
llvm::outs() << *ns.name->data << "hnt\n";
if (jitDylibs.count(ns.name->data) == 0) {
return 0;
}
@ -354,6 +357,7 @@ const types::InternalString &Halley::getInternalString(const char *s) {
// TODO: [serene.core] We need to provide some functions on llvm level to
// build instances from these type in a functional way. We need to avoid
// randomly build instances here and there that causes unsafe memory
assert(s && "s is nullptr: getInternalString");
auto len = std::strlen(s);
auto *str =
@ -372,6 +376,7 @@ types::Namespace &Halley::createNamespace(const char *name) {
// TODO: [serene.core] We need to provide some functions on llvm level to
// build instances from these type in a functional way. We need to avoid
// randomly build instances here and there that causes unsafe memory
assert(name && "name is nullptr: createNamespace");
const auto &nsName = getInternalString(name);
auto *ns = (types::Namespace *)GC_MALLOC(sizeof(types::Namespace));
ns->name = &nsName;
@ -382,9 +387,8 @@ types::Namespace &Halley::createNamespace(const char *name) {
};
llvm::Error Halley::createEmptyNS(const char *name) {
auto &ns = createNamespace(name);
assert(name && "name is nullptr: createEmptyNS");
auto &ns = createNamespace(name);
auto numOfDylibs = getNumberOfJITDylibs(ns) + 1;
HALLEY_LOG(
@ -402,6 +406,22 @@ llvm::Error Halley::createEmptyNS(const char *name) {
return llvm::Error::success();
};
llvm::Error Halley::loadModule(const char *file) {
assert(file && "File is nullptr: loadModule");
auto llvmContext = ctx->genLLVMContext();
llvm::SMDiagnostic error;
auto module = llvm::parseIRFile(file, error, *llvmContext);
if (module == nullptr) {
return llvm::make_error<llvm::StringError>(
std::make_error_code(std::errc::executable_format_error),
error.getMessage().str() + " File: " + file);
}
return llvm::Error::success();
};
MaybeEngine makeHalleyJIT(std::unique_ptr<SereneContext> ctx) {
llvm::orc::JITTargetMachineBuilder jtmb(ctx->triple);
auto maybeJIT = Halley::make(std::move(ctx), std::move(jtmb));