llvm.org GIT mirror llvm / 2c0f0f9
[Kaleidoscope] Run clang-format over chapter 4 of kaleidoscope. I forgot to do this for r226308. Thanks to Eric Christopher for the reminder. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226327 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 5 years ago
1 changed file(s) with 41 addition(s) and 47 deletion(s). Raw diff Collapse all Expand all
384384 //===----------------------------------------------------------------------===//
385385
386386 // FIXME: Obviously we can do better than this
387 std::string GenerateUniqueName(const char *root)
388 {
387 std::string GenerateUniqueName(const char *root) {
389388 static int i = 0;
390389 char s[16];
391390 sprintf(s, "%s%d", root, i++);
393392 return S;
394393 }
395394
396 std::string MakeLegalFunctionName(std::string Name)
397 {
395 std::string MakeLegalFunctionName(std::string Name) {
398396 std::string NewName;
399397 if (!Name.length())
400 return GenerateUniqueName("anon_func_");
398 return GenerateUniqueName("anon_func_");
401399
402400 // Start with what we have
403401 NewName = Name;
408406 }
409407
410408 // Replace illegal characters with their ASCII equivalent
411 std::string legal_elements = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
409 std::string legal_elements =
410 "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
412411 size_t pos;
413 while ((pos = NewName.find_first_not_of(legal_elements)) != std::string::npos) {
412 while ((pos = NewName.find_first_not_of(legal_elements)) !=
413 std::string::npos) {
414414 char old_c = NewName.at(pos);
415415 char new_str[16];
416416 sprintf(new_str, "%d", (int)old_c);
424424 // MCJIT helper class
425425 //===----------------------------------------------------------------------===//
426426
427 class MCJITHelper
428 {
427 class MCJITHelper {
429428 public:
430 MCJITHelper(LLVMContext& C) : Context(C), OpenModule(NULL) {}
429 MCJITHelper(LLVMContext &C) : Context(C), OpenModule(NULL) {}
431430 ~MCJITHelper();
432431
433432 Function *getFunction(const std::string FnName);
434433 Module *getModuleForNewFunction();
435 void *getPointerToFunction(Function* F);
434 void *getPointerToFunction(Function *F);
436435 void *getSymbolAddress(const std::string &Name);
437436 void dump();
438437
439438 private:
440 typedef std::vector ModuleVector;
441 typedef std::vector EngineVector;
442
443 LLVMContext &Context;
444 Module *OpenModule;
445 ModuleVector Modules;
446 EngineVector Engines;
447 };
448
449 class HelpingMemoryManager : public SectionMemoryManager
450 {
451 HelpingMemoryManager(const HelpingMemoryManager&) LLVM_DELETED_FUNCTION;
452 void operator=(const HelpingMemoryManager&) LLVM_DELETED_FUNCTION;
439 typedef std::vector ModuleVector;
440 typedef std::vector EngineVector;
441
442 LLVMContext &Context;
443 Module *OpenModule;
444 ModuleVector Modules;
445 EngineVector Engines;
446 };
447
448 class HelpingMemoryManager : public SectionMemoryManager {
449 HelpingMemoryManager(const HelpingMemoryManager &) LLVM_DELETED_FUNCTION;
450 void operator=(const HelpingMemoryManager &) LLVM_DELETED_FUNCTION;
453451
454452 public:
455453 HelpingMemoryManager(MCJITHelper *Helper) : MasterHelper(Helper) {}
460458 /// modules associated with the MCJITHelper to cross link symbols
461459 /// from one generated module to another.
462460 virtual uint64_t getSymbolAddress(const std::string &Name) override;
461
463462 private:
464463 MCJITHelper *MasterHelper;
465464 };
466465
467 uint64_t HelpingMemoryManager::getSymbolAddress(const std::string &Name)
468 {
466 uint64_t HelpingMemoryManager::getSymbolAddress(const std::string &Name) {
469467 uint64_t FnAddr = SectionMemoryManager::getSymbolAddress(Name);
470468 if (FnAddr)
471469 return FnAddr;
472470
473 uint64_t HelperFun = (uint64_t) MasterHelper->getSymbolAddress(Name);
471 uint64_t HelperFun = (uint64_t)MasterHelper->getSymbolAddress(Name);
474472 if (!HelperFun)
475473 report_fatal_error("Program used extern function '" + Name +
476474 "' which could not be resolved!");
478476 return HelperFun;
479477 }
480478
481 MCJITHelper::~MCJITHelper()
482 {
479 MCJITHelper::~MCJITHelper() {
483480 if (OpenModule)
484481 delete OpenModule;
485482 EngineVector::iterator begin = Engines.begin();
497494 Function *F = (*it)->getFunction(FnName);
498495 if (F) {
499496 if (*it == OpenModule)
500 return F;
497 return F;
501498
502499 assert(OpenModule != NULL);
503500
511508
512509 // If we don't have a prototype yet, create one.
513510 if (!PF)
514 PF = Function::Create(F->getFunctionType(),
515 Function::ExternalLinkage,
516 FnName,
517 OpenModule);
511 PF = Function::Create(F->getFunctionType(), Function::ExternalLinkage,
512 FnName, OpenModule);
518513 return PF;
519514 }
520515 }
534529 return M;
535530 }
536531
537 void *MCJITHelper::getPointerToFunction(Function* F) {
532 void *MCJITHelper::getPointerToFunction(Function *F) {
538533 // See if an existing instance of MCJIT has this function.
539534 EngineVector::iterator begin = Engines.begin();
540535 EngineVector::iterator end = Engines.end();
548543 // If we didn't find the function, see if we can generate it.
549544 if (OpenModule) {
550545 std::string ErrStr;
551 ExecutionEngine *NewEngine = EngineBuilder(std::unique_ptr(OpenModule))
552 .setErrorStr(&ErrStr)
553 .setMCJITMemoryManager(std::unique_ptr(new HelpingMemoryManager(this)))
554 .create();
546 ExecutionEngine *NewEngine =
547 EngineBuilder(std::unique_ptr(OpenModule))
548 .setErrorStr(&ErrStr)
549 .setMCJITMemoryManager(std::unique_ptr(
550 new HelpingMemoryManager(this)))
551 .create();
555552 if (!NewEngine) {
556553 fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
557554 exit(1);
597594 return NULL;
598595 }
599596
600 void *MCJITHelper::getSymbolAddress(const std::string &Name)
601 {
597 void *MCJITHelper::getSymbolAddress(const std::string &Name) {
602598 // Look for the symbol in each of our execution engines.
603599 EngineVector::iterator begin = Engines.begin();
604600 EngineVector::iterator end = Engines.end();
606602 for (it = begin; it != end; ++it) {
607603 uint64_t FAddr = (*it)->getFunctionAddress(Name);
608604 if (FAddr) {
609 return (void *)FAddr;
605 return (void *)FAddr;
610606 }
611607 }
612608 return NULL;
613609 }
614610
615 void MCJITHelper::dump()
616 {
611 void MCJITHelper::dump() {
617612 ModuleVector::iterator begin = Modules.begin();
618613 ModuleVector::iterator end = Modules.end();
619614 ModuleVector::iterator it;
697692
698693 Module *M = JITHelper->getModuleForNewFunction();
699694
700 Function *F =
701 Function::Create(FT, Function::ExternalLinkage, FnName, M);
695 Function *F = Function::Create(FT, Function::ExternalLinkage, FnName, M);
702696
703697 // If F conflicted, there was already something named 'Name'. If it has a
704698 // body, don't allow redefinition or reextern.
705699 if (F->getName() != FnName) {
706700 // Delete the one we just made and get the existing one.
707701 F->eraseFromParent();
708 F = JITHelper->getFunction(Name);
702 F = JITHelper->getFunction(Name);
709703 // If F already has a body, reject this.
710704 if (!F->empty()) {
711705 ErrorF("redefinition of function");