diff --git a/include/serene/jit/halley.h b/include/serene/jit/halley.h index 828106b..326a9d7 100644 --- a/include/serene/jit/halley.h +++ b/include/serene/jit/halley.h @@ -27,6 +27,7 @@ #include "serene/errors.h" #include "serene/errors/error.h" #include "serene/export.h" +#include "serene/namespace.h" #include "serene/utils.h" #include @@ -181,6 +182,8 @@ public: llvm::Optional addNS(Namespace &ns, reader::LocationRange &loc); + llvm::Optional addAST(exprs::Ast &ast); + Namespace &getActiveNS(); }; diff --git a/include/serene/namespace.h b/include/serene/namespace.h index 9355826..146f85b 100644 --- a/include/serene/namespace.h +++ b/include/serene/namespace.h @@ -147,7 +147,6 @@ public: SereneContext &getContext(); - // TODO: Fix the return type and use a `llvm::Optional` instead /// Generate and return a MLIR ModuleOp tha contains the IR of the namespace /// with respect to the compilation phase MaybeModuleOp generate(unsigned offset = 0); diff --git a/include/serene/slir/CMakeLists.txt b/include/serene/slir/CMakeLists.txt index 2d751f2..08e7cf2 100644 --- a/include/serene/slir/CMakeLists.txt +++ b/include/serene/slir/CMakeLists.txt @@ -1,6 +1,8 @@ set(LLVM_TARGET_DEFINITIONS dialect.td) mlir_tablegen(ops.h.inc -gen-op-decls) mlir_tablegen(ops.cpp.inc -gen-op-defs) +mlir_tablegen(types.h.inc -gen-typedef-decls) +mlir_tablegen(types.cpp.inc -gen-typedef-defs) mlir_tablegen(dialect.h.inc -gen-dialect-decls) mlir_tablegen(dialect.cpp.inc -gen-dialect-defs) add_public_tablegen_target(SereneDialectGen) diff --git a/src/libserene/jit/halley.cpp b/src/libserene/jit/halley.cpp index 4db14dc..b2c3bc6 100644 --- a/src/libserene/jit/halley.cpp +++ b/src/libserene/jit/halley.cpp @@ -462,6 +462,26 @@ MaybeJIT Halley::make(SereneContext &serene_ctx, return MaybeJIT(std::move(jitEngine)); }; +llvm::Optional Halley::addAST(exprs::Ast &ast) { + auto offset = activeNS->getTree().size(); + auto errs = activeNS->addTree(ast); + + if (errs) { + return errs.getValue(); + } + + auto maybeModule = activeNS->compileToLLVMFromOffset(offset); + + auto tsm = std::move(maybeModule.getValue()); + tsm.withModuleDo([](llvm::Module &m) { packFunctionArguments(&m); }); + + auto *dylib = ctx.getLatestJITDylib(*activeNS); + // TODO: Make sure that the data layout of the module is the same as the + // engine + cantFail(engine->addIRModule(*dylib, std::move(tsm))); + return llvm::None; +}; + Namespace &Halley::getActiveNS() { return *activeNS; }; llvm::Expected> makeHalleyJIT(SereneContext &ctx) { diff --git a/src/libserene/serene.cpp b/src/libserene/serene.cpp index 0e8ac26..be977c2 100644 --- a/src/libserene/serene.cpp +++ b/src/libserene/serene.cpp @@ -109,34 +109,37 @@ SERENE_EXPORT exprs::MaybeAst read(SereneContext &ctx, std::string &input) { SERENE_EXPORT exprs::MaybeNode eval(SereneContext &ctx, exprs::Ast &input) { - // TODO: Fix the eval function - UNUSED(input); - auto loc = reader::LocationRange::UnknownLocation("nsname"); - auto ns = ctx.importNamespace("docs.examples.hello_world", loc); + // auto ns = ctx.importNamespace("docs.examples.hello_world", loc); - if (!ns) { - auto es = ns.getError(); - auto nsloadErr = errors::makeError(loc, errors::NSLoadError); - es.push_back(nsloadErr); - return exprs::MaybeNode::error(es); + // if (!ns) { + // auto es = ns.getError(); + // auto nsloadErr = errors::makeError(loc, errors::NSLoadError); + // es.push_back(nsloadErr); + // return exprs::MaybeNode::error(es); + // } + + auto errs = ctx.jit->addAST(input); + if (errs) { + return exprs::MaybeNode::error(errs.getValue()); } - auto e = input[0]; - auto *sym = llvm::dyn_cast(e.get()); + // auto e = input[0]; + // auto *sym = llvm::dyn_cast(e.get()); - if (sym == nullptr) { - return exprs::makeErrorNode(e->location, errors::UnknownError, "only sym"); - } + // if (sym == nullptr) { + // return exprs::makeErrorNode(e->location, errors::UnknownError, "only + // sym"); + // } - llvm::outs() << "Read: " << sym->toString() << "\n"; + // llvm::outs() << "Read: " << sym->toString() << "\n"; - // Get the anonymous expression's JITSymbol. - auto symptr = ctx.jit->lookup(*sym); - if (!symptr) { - return exprs::MaybeNode::error(symptr.getError()); - } + // // Get the anonymous expression's JITSymbol. + // auto symptr = ctx.jit->lookup(*sym); + // if (!symptr) { + // return exprs::MaybeNode::error(symptr.getError()); + // } llvm::outs() << "eval here\n";