//LLVM includes #include "llvm/Analysis/Passes.h" // Local includes #include "JIT.h" using namespace ast; using namespace llvm; using namespace llvm::orc; namespace jit{ std::unique_ptr JITObjects::TheFPM = std::make_unique(AstObjects::TheModule.get()); std::unique_ptr JITObjects::TheJIT = std::unique_ptr(nullptr); std::map> JITObjects::FunctionProtos{}; void InitializeModuleAndPassManager(void) { // Open a new module. AstObjects::TheModule = llvm::make_unique("my cool jit", getGlobalContext()); AstObjects::TheModule->setDataLayout(JITObjects::TheJIT->getTargetMachine().createDataLayout()); // Create a new pass manager attached to it. JITObjects::TheFPM = llvm::make_unique(AstObjects::TheModule.get()); // HACK:: removed because not present in the full code listing and because of // errors in linking phase (libLLVMTransformUtils) // Promote allocas to registers. JITObjects::TheFPM->add(createPromoteMemoryToRegisterPass()); // Do simple "peephole" optimizations and bit-twiddling optzns. JITObjects::TheFPM->add(createInstructionCombiningPass()); // Reassociate expressions. JITObjects::TheFPM->add(createReassociatePass()); // Eliminate Common SubExpressions. JITObjects::TheFPM->add(createGVNPass()); // Simplify the control flow graph (deleting unreachable blocks, etc). JITObjects::TheFPM->add(createCFGSimplificationPass()); JITObjects::TheFPM->doInitialization(); } Function *getFunction(std::string Name) { // First, see if the function has already been added to the current module. if (auto *F = AstObjects::TheModule->getFunction(Name)) return F; // If not, check whether we can codegen the declaration from some existing // prototype. auto FI = JITObjects::FunctionProtos.find(Name); if (FI != JITObjects::FunctionProtos.end()) return FI->second->codegen(); // If no existing prototype exists, return null. return nullptr; } }