llvm.org GIT mirror llvm / fe85403
initial changes to support JIT'ing from multiple module providers, implicitly linking the program on the fly. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29721 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 14 years ago
5 changed file(s) with 177 addition(s) and 68 deletion(s). Raw diff Collapse all Expand all
1919 #include
2020 #include
2121 #include "llvm/System/Mutex.h"
22 #include "llvm/ADT/SmallVector.h"
2223
2324 namespace llvm {
2425
5960
6061
6162 class ExecutionEngine {
62 Module &CurMod;
6363 const TargetData *TD;
64
6564 ExecutionEngineState state;
66
6765 protected:
68 ModuleProvider *MP;
69
66 /// Modules - This is a list of ModuleProvider's that we are JIT'ing from. We
67 /// use a smallvector to optimize for the case where there is only one module.
68 SmallVector Modules;
69
7070 void setTargetData(const TargetData *td) {
7171 TD = td;
7272 }
8787 ExecutionEngine(Module *M);
8888 virtual ~ExecutionEngine();
8989
90 Module &getModule() const { return CurMod; }
90 //Module &getModule() const { return CurMod; }
9191 const TargetData *getTargetData() const { return TD; }
9292
93 /// FindFunctionNamed - Search all of the active modules to find the one that
94 /// defines FnName. This is very slow operation and shouldn't be used for
95 /// general code.
96 Function *FindFunctionNamed(const char *FnName);
97
9398 /// create - This is the factory method for creating an execution engine which
9499 /// is appropriate for the current machine.
95100 static ExecutionEngine *create(ModuleProvider *MP,
3434 ExecutionEngine::EECtorFn ExecutionEngine::JITCtor = 0;
3535 ExecutionEngine::EECtorFn ExecutionEngine::InterpCtor = 0;
3636
37 ExecutionEngine::ExecutionEngine(ModuleProvider *P) :
38 CurMod(*P->getModule()), MP(P) {
37 ExecutionEngine::ExecutionEngine(ModuleProvider *P) {
38 Modules.push_back(P);
3939 assert(P && "ModuleProvider is null?");
4040 }
4141
42 ExecutionEngine::ExecutionEngine(Module *M) : CurMod(*M), MP(0) {
42 ExecutionEngine::ExecutionEngine(Module *M) {
4343 assert(M && "Module is null?");
44 Modules.push_back(new ExistingModuleProvider(M));
4445 }
4546
4647 ExecutionEngine::~ExecutionEngine() {
47 delete MP;
48 }
48 for (unsigned i = 0, e = Modules.size(); i != e; ++i)
49 delete Modules[i];
50 }
51
52 /// FindFunctionNamed - Search all of the active modules to find the one that
53 /// defines FnName. This is very slow operation and shouldn't be used for
54 /// general code.
55 Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
56 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
57 if (Function *F = Modules[i]->getModule()->getNamedFunction(FnName))
58 return F;
59 }
60 return 0;
61 }
62
4963
5064 /// addGlobalMapping - Tell the execution engine that the specified global is
5165 /// at the specified location. This is used internally as functions are JIT'd
167181
168182
169183 /// runStaticConstructorsDestructors - This method is used to execute all of
170 /// the static constructors or destructors for a module, depending on the
184 /// the static constructors or destructors for a program, depending on the
171185 /// value of isDtors.
172186 void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
173187 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
174 GlobalVariable *GV = CurMod.getNamedGlobal(Name);
175
176 // If this global has internal linkage, or if it has a use, then it must be
177 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
178 // this is the case, don't execute any of the global ctors, __main will do it.
179 if (!GV || GV->isExternal() || GV->hasInternalLinkage()) return;
180
181 // Should be an array of '{ int, void ()* }' structs. The first value is the
182 // init priority, which we ignore.
183 ConstantArray *InitList = dyn_cast(GV->getInitializer());
184 if (!InitList) return;
185 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
186 if (ConstantStruct *CS = dyn_cast(InitList->getOperand(i))){
187 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
188
189 // Execute global ctors/dtors for each module in the program.
190 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
191 GlobalVariable *GV = Modules[m]->getModule()->getNamedGlobal(Name);
192
193 // If this global has internal linkage, or if it has a use, then it must be
194 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
195 // this is the case, don't execute any of the global ctors, __main will do
196 // it.
197 if (!GV || GV->isExternal() || GV->hasInternalLinkage()) continue;
198
199 // Should be an array of '{ int, void ()* }' structs. The first value is
200 // the init priority, which we ignore.
201 ConstantArray *InitList = dyn_cast(GV->getInitializer());
202 if (!InitList) continue;
203 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
204 if (ConstantStruct *CS =
205 dyn_cast(InitList->getOperand(i))) {
206 if (CS->getNumOperands() != 2) break; // Not array of 2-element structs.
188207
189 Constant *FP = CS->getOperand(1);
190 if (FP->isNullValue())
191 return; // Found a null terminator, exit.
208 Constant *FP = CS->getOperand(1);
209 if (FP->isNullValue())
210 break; // Found a null terminator, exit.
192211
193 if (ConstantExpr *CE = dyn_cast(FP))
194 if (CE->getOpcode() == Instruction::Cast)
195 FP = CE->getOperand(0);
196 if (Function *F = dyn_cast(FP)) {
197 // Execute the ctor/dtor function!
198 runFunction(F, std::vector());
212 if (ConstantExpr *CE = dyn_cast(FP))
213 if (CE->getOpcode() == Instruction::Cast)
214 FP = CE->getOperand(0);
215 if (Function *F = dyn_cast(FP)) {
216 // Execute the ctor/dtor function!
217 runFunction(F, std::vector());
218 }
199219 }
200 }
220 }
201221 }
202222
203223 /// runFunctionAsMain - This is a helper function which wraps runFunction to
609629 const TargetData *TD = getTargetData();
610630
611631 // Loop over all of the global variables in the program, allocating the memory
612 // to hold them.
613 Module &M = getModule();
614 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
615 I != E; ++I)
616 if (!I->isExternal()) {
617 // Get the type of the global...
618 const Type *Ty = I->getType()->getElementType();
619
620 // Allocate some memory for it!
621 unsigned Size = TD->getTypeSize(Ty);
622 addGlobalMapping(I, new char[Size]);
623 } else {
624 // External variable reference. Try to use the dynamic loader to
625 // get a pointer to it.
626 if (void *SymAddr = sys::DynamicLibrary::SearchForAddressOfSymbol(
627 I->getName().c_str()))
628 addGlobalMapping(I, SymAddr);
629 else {
630 std::cerr << "Could not resolve external global address: "
631 << I->getName() << "\n";
632 abort();
632 // to hold them. If there is more than one module, do a prepass over globals
633 // to figure out how the different modules should link together.
634 //
635 std::map,
636 const GlobalValue*> LinkedGlobalsMap;
637
638 if (Modules.size() != 1) {
639 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
640 Module &M = *Modules[m]->getModule();
641 for (Module::const_global_iterator I = M.global_begin(),
642 E = M.global_end(); I != E; ++I) {
643 const GlobalValue *GV = I;
644 if (GV->hasInternalLinkage() || GV->isExternal() ||
645 GV->hasAppendingLinkage() || !GV->hasName())
646 continue;// Ignore external globals and globals with internal linkage.
647
648 const GlobalValue *&GVEntry =
649 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
650
651 // If this is the first time we've seen this global, it is the canonical
652 // version.
653 if (!GVEntry) {
654 GVEntry = GV;
655 continue;
656 }
657
658 // If the existing global is strong, never replace it.
659 if (GVEntry->hasExternalLinkage())
660 continue;
661
662 // Otherwise, we know it's linkonce/weak, replace it if this is a strong
663 // symbol.
664 if (GV->hasExternalLinkage())
665 GVEntry = GV;
633666 }
634667 }
635
636 // Now that all of the globals are set up in memory, loop through them all and
637 // initialize their contents.
638 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
639 I != E; ++I)
640 if (!I->isExternal())
641 EmitGlobalVariable(I);
668 }
669
670 std::vector NonCanonicalGlobals;
671 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
672 Module &M = *Modules[m]->getModule();
673 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
674 I != E; ++I) {
675 // In the multi-module case, see what this global maps to.
676 if (!LinkedGlobalsMap.empty()) {
677 if (const GlobalValue *GVEntry =
678 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) {
679 // If something else is the canonical global, ignore this one.
680 if (GVEntry != &*I) {
681 NonCanonicalGlobals.push_back(I);
682 continue;
683 }
684 }
685 }
686
687 if (!I->isExternal()) {
688 // Get the type of the global.
689 const Type *Ty = I->getType()->getElementType();
690
691 // Allocate some memory for it!
692 unsigned Size = TD->getTypeSize(Ty);
693 addGlobalMapping(I, new char[Size]);
694 } else {
695 // External variable reference. Try to use the dynamic loader to
696 // get a pointer to it.
697 if (void *SymAddr =
698 sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName().c_str()))
699 addGlobalMapping(I, SymAddr);
700 else {
701 std::cerr << "Could not resolve external global address: "
702 << I->getName() << "\n";
703 abort();
704 }
705 }
706 }
707
708 // If there are multiple modules, map the non-canonical globals to their
709 // canonical location.
710 if (!NonCanonicalGlobals.empty()) {
711 for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
712 const GlobalValue *GV = NonCanonicalGlobals[i];
713 const GlobalValue *CGV =
714 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
715 void *Ptr = getPointerToGlobalIfAvailable(CGV);
716 assert(Ptr && "Canonical global wasn't codegen'd!");
717 addGlobalMapping(GV, getPointerToGlobalIfAvailable(CGV));
718 }
719 }
720
721 // Now that all of the globals are set up in memory, loop through them all and
722 // initialize their contents.
723 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
724 I != E; ++I) {
725 if (!I->isExternal()) {
726 if (!LinkedGlobalsMap.empty()) {
727 if (const GlobalValue *GVEntry =
728 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())])
729 if (GVEntry != &*I) // Not the canonical variable.
730 continue;
731 }
732 EmitGlobalVariable(I);
733 }
734 }
735 }
642736 }
643737
644738 // EmitGlobalVariable - This method emits the specified global variable to the
312312 case 'x': case 'X':
313313 if (HowLong >= 1) {
314314 if (HowLong == 1 &&
315 TheInterpreter->getModule().getPointerSize()==Module::Pointer64 &&
315 TheInterpreter->getTargetData()->getPointerSizeInBits() == 64 &&
316316 sizeof(long) < sizeof(int64_t)) {
317317 // Make sure we use %lld with a 64 bit argument because we might be
318318 // compiling LLI on a 32 bit compiler.
262262 if (void *Addr = getPointerToGlobalIfAvailable(F))
263263 return Addr; // Check if function already code gen'd
264264
265 // Make sure we read in the function if it exists in this Module
265 // Make sure we read in the function if it exists in this Module.
266266 if (F->hasNotBeenReadFromBytecode()) {
267 // Determine the module provider this function is provided by.
268 Module *M = F->getParent();
269 ModuleProvider *MP = 0;
270 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
271 if (Modules[i]->getModule() == M) {
272 MP = Modules[i];
273 break;
274 }
275 }
276 assert(MP && "Function isn't in a module we know about!");
277
267278 std::string ErrorMsg;
268279 if (MP->materializeFunction(F, &ErrorMsg)) {
269280 std::cerr << "Error reading function '" << F->getName()
971971 // resolve their addresses at runtime, and this is the way to do it.
972972 extern "C" {
973973 void *getPointerToNamedFunction(const char *Name) {
974 Module &M = TheJIT->getModule();
975 if (Function *F = M.getNamedFunction(Name))
974 if (Function *F = TheJIT->FindFunctionNamed(Name))
976975 return TheJIT->getPointerToFunction(F);
977976 return TheJIT->getPointerToNamedFunction(Name);
978977 }