JIT.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //LLVM includes
  2. #include "llvm/Analysis/Passes.h"
  3. // Local includes
  4. #include "JIT.h"
  5. using namespace ast;
  6. using namespace llvm;
  7. using namespace llvm::orc;
  8. namespace jit{
  9. std::unique_ptr<llvm::legacy::FunctionPassManager> JITObjects::TheFPM =
  10. std::make_unique<llvm::legacy::FunctionPassManager>(AstObjects::TheModule.get());
  11. std::unique_ptr<llvm::orc::KaleidoscopeJIT> JITObjects::TheJIT =
  12. std::unique_ptr<llvm::orc::KaleidoscopeJIT>(nullptr);
  13. std::map<std::string, std::unique_ptr<ast::PrototypeAST>> JITObjects::FunctionProtos{};
  14. void InitializeModule(void) {
  15. // Open a new module.
  16. AstObjects::TheModule = llvm::make_unique<Module>("my cool jit", getGlobalContext());
  17. AstObjects::TheModule->setDataLayout(JITObjects::TheJIT->getTargetMachine().createDataLayout());
  18. }
  19. Function *getFunction(std::string Name) {
  20. // First, see if the function has already been added to the current module.
  21. if (auto *F = AstObjects::TheModule->getFunction(Name))
  22. return F;
  23. // If not, check whether we can codegen the declaration from some existing
  24. // prototype.
  25. auto FI = JITObjects::FunctionProtos.find(Name);
  26. if (FI != JITObjects::FunctionProtos.end())
  27. return FI->second->codegen();
  28. // If no existing prototype exists, return null.
  29. return nullptr;
  30. }
  31. }