llvm.org GIT mirror llvm / f5261f5
Generate functions in 2 steps in the C API echo test. NFC git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@260939 91177308-0d34-0410-b5e6-96231b3b80d8 Amaury Sechet 4 years ago
1 changed file(s) with 33 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
584584 }
585585 };
586586
587 static LLVMValueRef clone_function(LLVMValueRef Src, LLVMModuleRef M) {
587 static void clone_function(LLVMValueRef Src, LLVMModuleRef M) {
588 const char *Name = LLVMGetValueName(Src);
589 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
590 if (!Fun)
591 report_fatal_error("Function must have been declared already");
592
593 FunCloner FC(Src, Fun);
594 FC.CloneBBs(Src);
595 }
596
597 static void declare_function(LLVMValueRef Src, LLVMModuleRef M) {
588598 const char *Name = LLVMGetValueName(Src);
589599 LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
590600 if (Fun != nullptr)
591 return Fun;
601 report_fatal_error("Function already cloned");
592602
593603 LLVMTypeRef FunTy = LLVMGetElementType(TypeCloner(M).Clone(Src));
594 Fun = LLVMAddFunction(M, Name, FunTy);
595 FunCloner FC(Src, Fun);
596 FC.CloneBBs(Src);
597
598 return Fun;
604 LLVMAddFunction(M, Name, FunTy);
599605 }
600606
601607 static void clone_functions(LLVMModuleRef Src, LLVMModuleRef Dst) {
602608 LLVMValueRef Begin = LLVMGetFirstFunction(Src);
603609 LLVMValueRef End = LLVMGetLastFunction(Src);
604610
611 // First pass, we declare all function
605612 LLVMValueRef Cur = Begin;
606613 LLVMValueRef Next = nullptr;
614 while (true) {
615 declare_function(Cur, Dst);
616 Next = LLVMGetNextFunction(Cur);
617 if (Next == nullptr) {
618 if (Cur != End)
619 report_fatal_error("Last function does not match End");
620 break;
621 }
622
623 LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
624 if (Prev != Cur)
625 report_fatal_error("Next.Previous function is not Current");
626
627 Cur = Next;
628 }
629
630 // Second pass, we define them
631 Cur = Begin;
632 Next = nullptr;
607633 while (true) {
608634 clone_function(Cur, Dst);
609635 Next = LLVMGetNextFunction(Cur);