return LazyEmitLayer.findSymbol(Name, true);
}
- JITSymbol findSymbol(const std::string &Name) {
- return findMangledSymbol(Mangle(Name));
+ JITSymbol findMangledSymbolIn(ModuleHandleT H, const std::string &Name) {
+ return LazyEmitLayer.findSymbolIn(H, Name, true);
}
- JITSymbol findMangledSymbolIn(LazyEmitLayerT::ModuleSetHandleT H,
- const std::string &Name) {
- return LazyEmitLayer.findSymbolIn(H, Name, true);
+ JITSymbol findSymbol(const std::string &Name) {
+ return findMangledSymbol(Mangle(Name));
}
- JITSymbol findSymbolIn(LazyEmitLayerT::ModuleSetHandleT H,
- const std::string &Name) {
+ JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name) {
return findMangledSymbolIn(H, Mangle(Name));
}
// FIXME: What happens if IRGen fails?
auto H = irGenStub(std::move(DefI->second));
- // Remove the map entry now that we're done with it.
+ // Remove the function definition's AST now that we're
+ // finished with it.
FunctionDefs.erase(DefI);
// Return the address of the stub.
CompileLayerT CompileLayer;
LazyEmitLayerT LazyEmitLayer;
- JITCompileCallbackManager<LazyEmitLayerT, OrcX86_64> CompileCallbacks;
-
std::map<std::string, std::unique_ptr<FunctionAST>> FunctionDefs;
+
+ JITCompileCallbackManager<LazyEmitLayerT, OrcX86_64> CompileCallbacks;
};
static void HandleDefinition(SessionContext &S, KaleidoscopeJIT &J) {
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/IRBuilder.h"
-#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/TargetSelect.h"
LLVMContext& getLLVMContext() const { return Context; }
void addPrototypeAST(std::unique_ptr<PrototypeAST> P);
PrototypeAST* getPrototypeAST(const std::string &Name);
- std::map<std::string, std::unique_ptr<FunctionAST>> FunctionDefs;
private:
typedef std::map<std::string, std::unique_ptr<PrototypeAST>> PrototypeMap;
LLVMContext &Context;
// We need a memory manager to allocate memory and resolve symbols for this
// new module. Create one that resolves symbols by looking back into the JIT.
auto MM = createLookasideRTDyldMM<SectionMemoryManager>(
- [&](const std::string &Name) -> uint64_t {
+ [&](const std::string &Name) {
// First try to find 'Name' within the JIT.
if (auto Symbol = findMangledSymbol(Name))
return Symbol.getAddress();
- // If we don't find 'Name' in the JIT, see if we have some AST
- // for it.
- auto DefI = Session.FunctionDefs.find(Name);
- if (DefI == Session.FunctionDefs.end())
- return 0;
-
- // We have AST for 'Name'. IRGen it, add it to the JIT, and
- // return the address for it.
- // FIXME: What happens if IRGen fails?
- addModule(IRGen(Session, *DefI->second));
-
- // Remove the function definition's AST now that we've
- // finished with it.
- Session.FunctionDefs.erase(DefI);
-
- return findMangledSymbol(Name).getAddress();
+ // If we don't already have a definition of 'Name' then search
+ // the ASTs.
+ return searchUncompiledASTs(Name);
},
[](const std::string &S) { return 0; } );
void removeModule(ModuleHandleT H) { LazyEmitLayer.removeModuleSet(H); }
JITSymbol findMangledSymbol(const std::string &Name) {
- return LazyEmitLayer.findSymbol(Name, false);
+ return LazyEmitLayer.findSymbol(Name, true);
+ }
+
+ JITSymbol findMangledSymbolIn(ModuleHandleT H, const std::string &Name) {
+ return LazyEmitLayer.findSymbolIn(H, Name, true);
}
JITSymbol findSymbol(const std::string &Name) {
return findMangledSymbol(Mangle(Name));
}
+ void addFunctionDefinition(std::unique_ptr<FunctionAST> FnAST) {
+ FunctionDefs[Mangle(FnAST->Proto->Name)] = std::move(FnAST);
+ }
+
private:
+ // This method searches the FunctionDefs map for a definition of 'Name'. If it
+ // finds one it generates a stub for it and returns the address of the stub.
+ TargetAddress searchUncompiledASTs(const std::string &Name) {
+ auto DefI = FunctionDefs.find(Name);
+ if (DefI == FunctionDefs.end())
+ return 0;
+
+ // We have AST for 'Name'. IRGen it, add it to the JIT, and
+ // return the address for it.
+ // FIXME: What happens if IRGen fails?
+ auto H = addModule(IRGen(Session, *DefI->second));
+
+ // Remove the function definition's AST now that we're
+ // finished with it.
+ FunctionDefs.erase(DefI);
+
+ // Return the address of the function.
+ return findMangledSymbolIn(H, Name).getAddress();
+ }
+
std::unique_ptr<TargetMachine> TM;
Mangler Mang;
SessionContext &Session;
ObjLayerT ObjectLayer;
CompileLayerT CompileLayer;
LazyEmitLayerT LazyEmitLayer;
+
+ std::map<std::string, std::unique_ptr<FunctionAST>> FunctionDefs;
};
static void HandleDefinition(SessionContext &S, KaleidoscopeJIT &J) {
if (auto F = ParseDefinition()) {
S.addPrototypeAST(llvm::make_unique<PrototypeAST>(*F->Proto));
- S.FunctionDefs[J.Mangle(F->Proto->Name)] = std::move(F);
+ J.addFunctionDefinition(std::move(F));
} else {
// Skip token for error recovery.
getNextToken();