1 //===- Linker.cpp - Module Linker Implementation --------------------------===//
3 // This file implements the LLVM module linker.
6 // * Merges global variables between the two modules
7 // * Uninit + Uninit = Init, Init + Uninit = Init, Init + Init = Error if !=
8 // * Merges methods between two modules
10 //===----------------------------------------------------------------------===//
12 #include "llvm/Transforms/Linker.h"
13 #include "llvm/Module.h"
14 #include "llvm/Method.h"
15 #include "llvm/GlobalVariable.h"
16 #include "llvm/SymbolTable.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/iOther.h"
19 #include "llvm/ConstPoolVals.h"
21 // Error - Simple wrapper function to conditionally assign to E and return true.
22 // This just makes error return conditions a little bit simpler...
24 static inline bool Error(string *E, string Message) {
29 #include "llvm/Assembly/Writer.h" // TODO: REMOVE
32 // LinkTypes - Go through the symbol table of the Src module and see if any
33 // types are named in the src module that are not named in the Dst module.
34 // Make sure there are no type name conflicts.
36 static bool LinkTypes(Module *Dest, const Module *Src, string *Err = 0) {
37 // No symbol table? Can't have named types.
38 if (!Src->hasSymbolTable()) return false;
40 SymbolTable *DestST = Dest->getSymbolTableSure();
41 const SymbolTable *SrcST = Src->getSymbolTable();
43 // Look for a type plane for Type's...
44 SymbolTable::const_iterator PI = SrcST->find(Type::TypeTy);
45 if (PI == SrcST->end()) return false; // No named types, do nothing.
47 const SymbolTable::VarMap &VM = PI->second;
48 for (SymbolTable::type_const_iterator I = VM.begin(), E = VM.end();
50 const string &Name = I->first;
51 const Type *RHS = cast<Type>(I->second);
53 // Check to see if this type name is already in the dest module...
54 const Type *Entry = cast_or_null<Type>(DestST->lookup(Type::TypeTy, Name));
55 if (Entry) { // Yup, the value already exists...
56 if (Entry != RHS) // If it's the same, noop. Otherwise, error.
57 return Error(Err, "Type named '" + Name +
58 "' of different shape in modules.\n Src='" +
59 Entry->getDescription() + "'. Dest='" +
60 RHS->getDescription() + "'");
61 } else { // Type not in dest module. Add it now.
62 // TODO: FIXME WHEN TYPES AREN'T CONST
63 DestST->insert(Name, const_cast<Type*>(RHS));
69 static void PrintMap(const map<const Value*, Value*> &M) {
70 for (map<const Value*, Value*>::const_iterator I = M.begin(), E = M.end();
72 cerr << " Fr: " << (void*)I->first << " " << I->first
73 << " To: " << (void*)I->second << " " << I->second << endl;
78 // RemapOperand - Use LocalMap and GlobalMap to convert references from one
79 // module to another. This is somewhat sophisticated in that it can
80 // automatically handle constant references correctly as well...
82 static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
83 const map<const Value*, Value*> *GlobalMap = 0) {
84 map<const Value*,Value*>::const_iterator I = LocalMap.find(In);
85 if (I != LocalMap.end()) return I->second;
88 I = GlobalMap->find(In);
89 if (I != GlobalMap->end()) return I->second;
92 // Check to see if it's a constant that we are interesting in transforming...
93 if (ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(In)) {
94 if (!isa<DerivedType>(CPV->getType()))
95 return CPV; // Simple constants stay identical...
97 ConstPoolVal *Result = 0;
99 if (ConstPoolArray *CPA = dyn_cast<ConstPoolArray>(CPV)) {
100 const vector<Use> &Ops = CPA->getValues();
101 vector<ConstPoolVal*> Operands(Ops.size());
102 for (unsigned i = 0; i < Ops.size(); ++i)
104 cast<ConstPoolVal>(RemapOperand(Ops[i], LocalMap, GlobalMap));
105 Result = ConstPoolArray::get(cast<ArrayType>(CPA->getType()), Operands);
106 } else if (ConstPoolStruct *CPS = dyn_cast<ConstPoolStruct>(CPV)) {
107 const vector<Use> &Ops = CPS->getValues();
108 vector<ConstPoolVal*> Operands(Ops.size());
109 for (unsigned i = 0; i < Ops.size(); ++i)
111 cast<ConstPoolVal>(RemapOperand(Ops[i], LocalMap, GlobalMap));
112 Result = ConstPoolStruct::get(cast<StructType>(CPS->getType()), Operands);
113 } else if (isa<ConstPoolPointerNull>(CPV)) {
115 } else if (ConstPoolPointerRef *CPR = dyn_cast<ConstPoolPointerRef>(CPV)) {
116 Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap);
117 Result = ConstPoolPointerRef::get(cast<GlobalValue>(V));
119 assert(0 && "Unknown type of derived type constant value!");
122 // Cache the mapping in our local map structure...
123 LocalMap.insert(make_pair(In, CPV));
127 cerr << "XXX LocalMap: \n";
131 cerr << "XXX GlobalMap: \n";
132 PrintMap(*GlobalMap);
135 cerr << "Couldn't remap value: " << (void*)In << " " << In << endl;
136 assert(0 && "Couldn't remap value!");
141 // LinkGlobals - Loop through the global variables in the src module and merge
142 // them into the dest module...
144 static bool LinkGlobals(Module *Dest, const Module *Src,
145 map<const Value*, Value*> &ValueMap, string *Err = 0) {
146 // We will need a module level symbol table if the src module has a module
147 // level symbol table...
148 SymbolTable *ST = Src->getSymbolTable() ? Dest->getSymbolTableSure() : 0;
150 // Loop over all of the globals in the src module, mapping them over as we go
152 for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
153 const GlobalVariable *SGV = *I;
156 // If the global variable has a name, and that name is already in use in the
157 // Dest module, make sure that the name is a compatible global variable...
159 if (SGV->hasExternalLinkage() && SGV->hasName() &&
160 (V = ST->lookup(SGV->getType(), SGV->getName())) &&
161 cast<GlobalVariable>(V)->hasExternalLinkage()) {
162 // The same named thing is a global variable, because the only two things
163 // that may be in a module level symbol table are Global Vars and Methods,
164 // and they both have distinct, nonoverlapping, possible types.
166 GlobalVariable *DGV = cast<GlobalVariable>(V);
168 // Check to see if the two GV's have the same Const'ness...
169 if (SGV->isConstant() != DGV->isConstant())
170 return Error(Err, "Global Variable Collision on '" +
171 SGV->getType()->getDescription() + "':%" + SGV->getName() +
172 " - Global variables differ in const'ness");
174 // Okay, everything is cool, remember the mapping...
175 ValueMap.insert(make_pair(SGV, DGV));
177 // No linking to be performed, simply create an identical version of the
178 // symbol over in the dest module... the initializer will be filled in
179 // later by LinkGlobalInits...
181 GlobalVariable *DGV =
182 new GlobalVariable(SGV->getType()->getValueType(), SGV->isConstant(),
183 SGV->hasInternalLinkage(), 0, SGV->getName());
185 // Add the new global to the dest module
186 Dest->getGlobalList().push_back(DGV);
188 // Make sure to remember this mapping...
189 ValueMap.insert(make_pair(SGV, DGV));
196 // LinkGlobalInits - Update the initializers in the Dest module now that all
197 // globals that may be referenced are in Dest.
199 static bool LinkGlobalInits(Module *Dest, const Module *Src,
200 map<const Value*, Value*> &ValueMap,
203 // Loop over all of the globals in the src module, mapping them over as we go
205 for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
206 const GlobalVariable *SGV = *I;
208 if (SGV->hasInitializer()) { // Only process initialized GV's
209 // Figure out what the initializer looks like in the dest module...
210 ConstPoolVal *DInit =
211 cast<ConstPoolVal>(RemapOperand(SGV->getInitializer(), ValueMap));
213 GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);
214 if (DGV->hasInitializer() && SGV->hasExternalLinkage() &&
215 DGV->hasExternalLinkage()) {
216 if (DGV->getInitializer() != DInit)
217 return Error(Err, "Global Variable Collision on '" +
218 SGV->getType()->getDescription() + "':%" +SGV->getName()+
219 " - Global variables have different initializers");
221 // Copy the initializer over now...
222 DGV->setInitializer(DInit);
229 // LinkMethodProtos - Link the methods together between the two modules, without
230 // doing method bodies... this just adds external method prototypes to the Dest
233 static bool LinkMethodProtos(Module *Dest, const Module *Src,
234 map<const Value*, Value*> &ValueMap,
236 // We will need a module level symbol table if the src module has a module
237 // level symbol table...
238 SymbolTable *ST = Src->getSymbolTable() ? Dest->getSymbolTableSure() : 0;
240 // Loop over all of the methods in the src module, mapping them over as we go
242 for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
243 const Method *SM = *I; // SrcMethod
246 // If the method has a name, and that name is already in use in the
247 // Dest module, make sure that the name is a compatible method...
249 if (SM->hasExternalLinkage() && SM->hasName() &&
250 (V = ST->lookup(SM->getType(), SM->getName())) &&
251 cast<Method>(V)->hasExternalLinkage()) {
252 // The same named thing is a Method, because the only two things
253 // that may be in a module level symbol table are Global Vars and Methods,
254 // and they both have distinct, nonoverlapping, possible types.
256 Method *DM = cast<Method>(V); // DestMethod
258 // Check to make sure the method is not defined in both modules...
259 if (!SM->isExternal() && !DM->isExternal())
260 return Error(Err, "Method '" +
261 SM->getMethodType()->getDescription() + "':\"" +
262 SM->getName() + "\" - Method is already defined!");
264 // Otherwise, just remember this mapping...
265 ValueMap.insert(make_pair(SM, DM));
267 // Method does not already exist, simply insert an external method
268 // signature identical to SM into the dest module...
269 Method *DM = new Method(SM->getMethodType(), SM->hasInternalLinkage(),
272 // Add the method signature to the dest module...
273 Dest->getMethodList().push_back(DM);
275 // ... and remember this mapping...
276 ValueMap.insert(make_pair(SM, DM));
282 // LinkMethodBody - Copy the source method over into the dest method and fix up
283 // references to values. At this point we know that Dest is an external method,
284 // and that Src is not.
286 static bool LinkMethodBody(Method *Dest, const Method *Src,
287 const map<const Value*, Value*> &GlobalMap,
289 assert(Src && Dest && Dest->isExternal() && !Src->isExternal());
290 map<const Value*, Value*> LocalMap; // Map for method local values
292 // Go through and convert method arguments over...
293 for (Method::ArgumentListType::const_iterator
294 I = Src->getArgumentList().begin(),
295 E = Src->getArgumentList().end(); I != E; ++I) {
296 const MethodArgument *SMA = *I;
298 // Create the new method argument and add to the dest method...
299 MethodArgument *DMA = new MethodArgument(SMA->getType(), SMA->getName());
300 Dest->getArgumentList().push_back(DMA);
302 // Add a mapping to our local map
303 LocalMap.insert(make_pair(SMA, DMA));
306 // Loop over all of the basic blocks, copying the instructions over...
308 for (Method::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
309 const BasicBlock *SBB = *I;
311 // Create new basic block and add to mapping and the Dest method...
312 BasicBlock *DBB = new BasicBlock(SBB->getName(), Dest);
313 LocalMap.insert(make_pair(SBB, DBB));
315 // Loop over all of the instructions in the src basic block, copying them
316 // over. Note that this is broken in a strict sense because the cloned
317 // instructions will still be referencing values in the Src module, not
318 // the remapped values. In our case, however, we will not get caught and
319 // so we can delay patching the values up until later...
321 for (BasicBlock::const_iterator II = SBB->begin(), IE = SBB->end();
323 const Instruction *SI = *II;
324 Instruction *DI = SI->clone();
325 DI->setName(SI->getName());
326 DBB->getInstList().push_back(DI);
327 LocalMap.insert(make_pair(SI, DI));
331 // At this point, all of the instructions and values of the method are now
332 // copied over. The only problem is that they are still referencing values
333 // in the Source method as operands. Loop through all of the operands of the
334 // methods and patch them up to point to the local versions...
336 for (Method::inst_iterator I = Dest->inst_begin(), E = Dest->inst_end();
338 Instruction *Inst = *I;
340 for (Instruction::op_iterator OI = Inst->op_begin(), OE = Inst->op_end();
342 *OI = RemapOperand(*OI, LocalMap, &GlobalMap);
349 // LinkMethodBodies - Link in the method bodies that are defined in the source
350 // module into the DestModule. This consists basically of copying the method
351 // over and fixing up references to values.
353 static bool LinkMethodBodies(Module *Dest, const Module *Src,
354 map<const Value*, Value*> &ValueMap,
357 // Loop over all of the methods in the src module, mapping them over as we go
359 for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
360 const Method *SM = *I; // Source Method
361 if (!SM->isExternal()) { // No body if method is external
362 Method *DM = cast<Method>(ValueMap[SM]); // Destination method
364 // DM not external SM external?
365 if (!DM->isExternal()) {
367 *Err = "Method '" + (SM->hasName() ? SM->getName() : string("")) +
368 "' body multiply defined!";
372 if (LinkMethodBody(DM, SM, ValueMap, Err)) return true;
380 // LinkModules - This function links two modules together, with the resulting
381 // left module modified to be the composite of the two input modules. If an
382 // error occurs, true is returned and ErrorMsg (if not null) is set to indicate
383 // the problem. Upon failure, the Dest module could be in a modified state, and
384 // shouldn't be relied on to be consistent.
386 bool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg = 0) {
388 // LinkTypes - Go through the symbol table of the Src module and see if any
389 // types are named in the src module that are not named in the Dst module.
390 // Make sure there are no type name conflicts.
392 if (LinkTypes(Dest, Src, ErrorMsg)) return true;
394 // ValueMap - Mapping of values from what they used to be in Src, to what they
397 map<const Value*, Value*> ValueMap;
399 // Insert all of the globals in src into the Dest module... without
401 if (LinkGlobals(Dest, Src, ValueMap, ErrorMsg)) return true;
403 // Update the initializers in the Dest module now that all globals that may
404 // be referenced are in Dest.
406 if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
408 // Link the methods together between the two modules, without doing method
409 // bodies... this just adds external method prototypes to the Dest method...
410 // We do this so that when we begin processing method bodies, all of the
411 // global values that may be referenced are available in our ValueMap.
413 if (LinkMethodProtos(Dest, Src, ValueMap, ErrorMsg)) return true;
415 // Link in the method bodies that are defined in the source module into the
416 // DestModule. This consists basically of copying the method over and fixing
417 // up references to values.
419 if (LinkMethodBodies(Dest, Src, ValueMap, ErrorMsg)) return true;