JIT.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 InitializeModuleAndPassManager(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. // Create a new pass manager attached to it.
  19. JITObjects::TheFPM = llvm::make_unique<legacy::FunctionPassManager>(AstObjects::TheModule.get());
  20. // Do simple "peephole" optimizations and bit-twiddling optzns.
  21. JITObjects::TheFPM->add(createInstructionCombiningPass());
  22. // Reassociate expressions.
  23. JITObjects::TheFPM->add(createReassociatePass());
  24. // Eliminate Common SubExpressions.
  25. JITObjects::TheFPM->add(createGVNPass());
  26. // Simplify the control flow graph (deleting unreachable blocks, etc).
  27. JITObjects::TheFPM->add(createCFGSimplificationPass());
  28. JITObjects::TheFPM->doInitialization();
  29. }
  30. Function *getFunction(std::string Name) {
  31. // First, see if the function has already been added to the current module.
  32. if (auto *F = AstObjects::TheModule->getFunction(Name))
  33. return F;
  34. // If not, check whether we can codegen the declaration from some existing
  35. // prototype.
  36. auto FI = JITObjects::FunctionProtos.find(Name);
  37. if (FI != JITObjects::FunctionProtos.end())
  38. return FI->second->codegen();
  39. // If no existing prototype exists, return null.
  40. return nullptr;
  41. }
  42. }