JIT.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. // HACK:: removed because not present in the full code listing and because of
  21. // errors in linking phase (libLLVMTransformUtils)
  22. // Promote allocas to registers.
  23. JITObjects::TheFPM->add(createPromoteMemoryToRegisterPass());
  24. // Do simple "peephole" optimizations and bit-twiddling optzns.
  25. JITObjects::TheFPM->add(createInstructionCombiningPass());
  26. // Reassociate expressions.
  27. JITObjects::TheFPM->add(createReassociatePass());
  28. // Eliminate Common SubExpressions.
  29. JITObjects::TheFPM->add(createGVNPass());
  30. // Simplify the control flow graph (deleting unreachable blocks, etc).
  31. JITObjects::TheFPM->add(createCFGSimplificationPass());
  32. JITObjects::TheFPM->doInitialization();
  33. }
  34. Function *getFunction(std::string Name) {
  35. // First, see if the function has already been added to the current module.
  36. if (auto *F = AstObjects::TheModule->getFunction(Name))
  37. return F;
  38. // If not, check whether we can codegen the declaration from some existing
  39. // prototype.
  40. auto FI = JITObjects::FunctionProtos.find(Name);
  41. if (FI != JITObjects::FunctionProtos.end())
  42. return FI->second->codegen();
  43. // If no existing prototype exists, return null.
  44. return nullptr;
  45. }
  46. }