blob: 0d3cc9bfa9f5cb64762f91f9791b9b8e3b4e98fd [file] [log] [blame]
Chris Lattner52f7e902001-10-13 07:03:50 +00001//===- Linker.cpp - Module Linker Implementation --------------------------===//
2//
3// This file implements the LLVM module linker.
4//
5// Specifically, this:
Chris Lattner8d2de8a2001-10-15 03:12:52 +00006// * Merges global variables between the two modules
7// * Uninit + Uninit = Init, Init + Uninit = Init, Init + Init = Error if !=
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +00008// * Merges functions between two modules
Chris Lattner52f7e902001-10-13 07:03:50 +00009//
10//===----------------------------------------------------------------------===//
11
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +000012#include "llvm/Transforms/Utils/Linker.h"
Chris Lattner5c377c52001-10-14 23:29:15 +000013#include "llvm/Module.h"
Chris Lattner5c377c52001-10-14 23:29:15 +000014#include "llvm/SymbolTable.h"
15#include "llvm/DerivedTypes.h"
16#include "llvm/iOther.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000017#include "llvm/Constants.h"
Chris Lattner697954c2002-01-20 22:54:45 +000018using std::cerr;
19using std::string;
20using std::map;
Chris Lattner5c377c52001-10-14 23:29:15 +000021
22// Error - Simple wrapper function to conditionally assign to E and return true.
23// This just makes error return conditions a little bit simpler...
24//
25static inline bool Error(string *E, string Message) {
26 if (E) *E = Message;
27 return true;
28}
29
Chris Lattner2c236f32001-11-03 05:18:24 +000030// LinkTypes - Go through the symbol table of the Src module and see if any
31// types are named in the src module that are not named in the Dst module.
32// Make sure there are no type name conflicts.
33//
34static bool LinkTypes(Module *Dest, const Module *Src, string *Err = 0) {
Chris Lattner6e6026b2002-11-20 18:36:02 +000035 SymbolTable *DestST = &Dest->getSymbolTable();
36 const SymbolTable *SrcST = &Src->getSymbolTable();
Chris Lattner2c236f32001-11-03 05:18:24 +000037
38 // Look for a type plane for Type's...
39 SymbolTable::const_iterator PI = SrcST->find(Type::TypeTy);
40 if (PI == SrcST->end()) return false; // No named types, do nothing.
41
42 const SymbolTable::VarMap &VM = PI->second;
43 for (SymbolTable::type_const_iterator I = VM.begin(), E = VM.end();
44 I != E; ++I) {
45 const string &Name = I->first;
46 const Type *RHS = cast<Type>(I->second);
47
48 // Check to see if this type name is already in the dest module...
49 const Type *Entry = cast_or_null<Type>(DestST->lookup(Type::TypeTy, Name));
50 if (Entry) { // Yup, the value already exists...
51 if (Entry != RHS) // If it's the same, noop. Otherwise, error.
52 return Error(Err, "Type named '" + Name +
53 "' of different shape in modules.\n Src='" +
Chris Lattner9b638012002-03-15 20:21:29 +000054 Entry->getDescription() + "'.\n Dst='" +
Chris Lattner2c236f32001-11-03 05:18:24 +000055 RHS->getDescription() + "'");
56 } else { // Type not in dest module. Add it now.
57 // TODO: FIXME WHEN TYPES AREN'T CONST
58 DestST->insert(Name, const_cast<Type*>(RHS));
59 }
60 }
61 return false;
62}
63
Chris Lattner2d3e8bb2001-11-03 03:27:29 +000064static void PrintMap(const map<const Value*, Value*> &M) {
65 for (map<const Value*, Value*>::const_iterator I = M.begin(), E = M.end();
66 I != E; ++I) {
Chris Lattner87182ae2002-04-07 22:31:23 +000067 cerr << " Fr: " << (void*)I->first << " ";
68 I->first->dump();
69 cerr << " To: " << (void*)I->second << " ";
70 I->second->dump();
71 cerr << "\n";
Chris Lattner2d3e8bb2001-11-03 03:27:29 +000072 }
73}
74
75
Chris Lattner5c377c52001-10-14 23:29:15 +000076// RemapOperand - Use LocalMap and GlobalMap to convert references from one
77// module to another. This is somewhat sophisticated in that it can
78// automatically handle constant references correctly as well...
79//
Chris Lattner8d2de8a2001-10-15 03:12:52 +000080static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
Chris Lattnerd149c052002-09-23 18:14:15 +000081 map<const Value*, Value*> *GlobalMap = 0) {
Chris Lattner5c377c52001-10-14 23:29:15 +000082 map<const Value*,Value*>::const_iterator I = LocalMap.find(In);
83 if (I != LocalMap.end()) return I->second;
84
85 if (GlobalMap) {
86 I = GlobalMap->find(In);
87 if (I != GlobalMap->end()) return I->second;
88 }
89
Chris Lattner8d2de8a2001-10-15 03:12:52 +000090 // Check to see if it's a constant that we are interesting in transforming...
Chris Lattner18961502002-06-25 16:12:52 +000091 if (const Constant *CPV = dyn_cast<Constant>(In)) {
Chris Lattner6cdf1972002-07-18 00:13:08 +000092 if (!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV))
Chris Lattner18961502002-06-25 16:12:52 +000093 return const_cast<Constant*>(CPV); // Simple constants stay identical...
Chris Lattner8d2de8a2001-10-15 03:12:52 +000094
Chris Lattnere9bb2df2001-12-03 22:26:30 +000095 Constant *Result = 0;
Chris Lattner8d2de8a2001-10-15 03:12:52 +000096
Chris Lattner18961502002-06-25 16:12:52 +000097 if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
Chris Lattner697954c2002-01-20 22:54:45 +000098 const std::vector<Use> &Ops = CPA->getValues();
99 std::vector<Constant*> Operands(Ops.size());
Chris Lattnere306d942002-07-18 02:31:03 +0000100 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000101 Operands[i] =
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000102 cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
103 Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands);
Chris Lattner18961502002-06-25 16:12:52 +0000104 } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000105 const std::vector<Use> &Ops = CPS->getValues();
106 std::vector<Constant*> Operands(Ops.size());
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000107 for (unsigned i = 0; i < Ops.size(); ++i)
108 Operands[i] =
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000109 cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
110 Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands);
111 } else if (isa<ConstantPointerNull>(CPV)) {
Chris Lattner18961502002-06-25 16:12:52 +0000112 Result = const_cast<Constant*>(CPV);
113 } else if (const ConstantPointerRef *CPR =
114 dyn_cast<ConstantPointerRef>(CPV)) {
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000115 Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000116 Result = ConstantPointerRef::get(cast<GlobalValue>(V));
Chris Lattner6cdf1972002-07-18 00:13:08 +0000117 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
Chris Lattnerb319faf2002-08-20 19:35:11 +0000118 if (CE->getOpcode() == Instruction::GetElementPtr) {
119 Value *Ptr = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
120 std::vector<Constant*> Indices;
121 Indices.reserve(CE->getNumOperands()-1);
122 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
123 Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),
124 LocalMap, GlobalMap)));
125
126 Result = ConstantExpr::getGetElementPtr(cast<Constant>(Ptr), Indices);
127 } else if (CE->getNumOperands() == 1) {
Chris Lattnerad333482002-08-14 18:24:09 +0000128 // Cast instruction
129 assert(CE->getOpcode() == Instruction::Cast);
Chris Lattner6cdf1972002-07-18 00:13:08 +0000130 Value *V = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
Chris Lattnerad333482002-08-14 18:24:09 +0000131 Result = ConstantExpr::getCast(cast<Constant>(V), CE->getType());
Chris Lattner6cdf1972002-07-18 00:13:08 +0000132 } else if (CE->getNumOperands() == 2) {
133 // Binary operator...
134 Value *V1 = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
135 Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap);
136
137 Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V1),
Chris Lattnere8e46052002-07-30 18:54:22 +0000138 cast<Constant>(V2));
Chris Lattner6cdf1972002-07-18 00:13:08 +0000139 } else {
Chris Lattnerb319faf2002-08-20 19:35:11 +0000140 assert(0 && "Unknown constant expr type!");
Chris Lattner6cdf1972002-07-18 00:13:08 +0000141 }
142
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000143 } else {
144 assert(0 && "Unknown type of derived type constant value!");
145 }
146
147 // Cache the mapping in our local map structure...
Chris Lattnerd149c052002-09-23 18:14:15 +0000148 if (GlobalMap)
149 GlobalMap->insert(std::make_pair(In, Result));
150 else
151 LocalMap.insert(std::make_pair(In, Result));
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000152 return Result;
153 }
Chris Lattner2d3e8bb2001-11-03 03:27:29 +0000154
155 cerr << "XXX LocalMap: \n";
156 PrintMap(LocalMap);
157
158 if (GlobalMap) {
159 cerr << "XXX GlobalMap: \n";
160 PrintMap(*GlobalMap);
161 }
162
Chris Lattner6cdf1972002-07-18 00:13:08 +0000163 cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n";
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000164 assert(0 && "Couldn't remap value!");
165 return 0;
Chris Lattner5c377c52001-10-14 23:29:15 +0000166}
167
168
169// LinkGlobals - Loop through the global variables in the src module and merge
170// them into the dest module...
171//
172static bool LinkGlobals(Module *Dest, const Module *Src,
173 map<const Value*, Value*> &ValueMap, string *Err = 0) {
174 // We will need a module level symbol table if the src module has a module
175 // level symbol table...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000176 SymbolTable *ST = (SymbolTable*)&Src->getSymbolTable();
Chris Lattner5c377c52001-10-14 23:29:15 +0000177
178 // Loop over all of the globals in the src module, mapping them over as we go
179 //
180 for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
Chris Lattner18961502002-06-25 16:12:52 +0000181 const GlobalVariable *SGV = I;
Chris Lattner5c377c52001-10-14 23:29:15 +0000182 Value *V;
183
184 // If the global variable has a name, and that name is already in use in the
185 // Dest module, make sure that the name is a compatible global variable...
186 //
Chris Lattnere4d25aa2001-11-26 18:59:18 +0000187 if (SGV->hasExternalLinkage() && SGV->hasName() &&
188 (V = ST->lookup(SGV->getType(), SGV->getName())) &&
189 cast<GlobalVariable>(V)->hasExternalLinkage()) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000190 // The same named thing is a global variable, because the only two things
Chris Lattner79df7c02002-03-26 18:01:55 +0000191 // that may be in a module level symbol table are Global Vars and
192 // Functions, and they both have distinct, nonoverlapping, possible types.
Chris Lattner5c377c52001-10-14 23:29:15 +0000193 //
194 GlobalVariable *DGV = cast<GlobalVariable>(V);
195
196 // Check to see if the two GV's have the same Const'ness...
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000197 if (SGV->isConstant() != DGV->isConstant())
Chris Lattner5c377c52001-10-14 23:29:15 +0000198 return Error(Err, "Global Variable Collision on '" +
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000199 SGV->getType()->getDescription() + "':%" + SGV->getName() +
Chris Lattner5c377c52001-10-14 23:29:15 +0000200 " - Global variables differ in const'ness");
201
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000202 // Okay, everything is cool, remember the mapping...
Chris Lattner697954c2002-01-20 22:54:45 +0000203 ValueMap.insert(std::make_pair(SGV, DGV));
Chris Lattner5c377c52001-10-14 23:29:15 +0000204 } else {
205 // No linking to be performed, simply create an identical version of the
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000206 // symbol over in the dest module... the initializer will be filled in
207 // later by LinkGlobalInits...
208 //
209 GlobalVariable *DGV =
Chris Lattner7a176752001-12-04 00:03:30 +0000210 new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(),
Chris Lattnere4d25aa2001-11-26 18:59:18 +0000211 SGV->hasInternalLinkage(), 0, SGV->getName());
Chris Lattner5c377c52001-10-14 23:29:15 +0000212
213 // Add the new global to the dest module
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000214 Dest->getGlobalList().push_back(DGV);
Chris Lattner5c377c52001-10-14 23:29:15 +0000215
216 // Make sure to remember this mapping...
Chris Lattner697954c2002-01-20 22:54:45 +0000217 ValueMap.insert(std::make_pair(SGV, DGV));
Chris Lattner5c377c52001-10-14 23:29:15 +0000218 }
219 }
220 return false;
221}
222
223
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000224// LinkGlobalInits - Update the initializers in the Dest module now that all
225// globals that may be referenced are in Dest.
226//
227static bool LinkGlobalInits(Module *Dest, const Module *Src,
228 map<const Value*, Value*> &ValueMap,
229 string *Err = 0) {
230
231 // Loop over all of the globals in the src module, mapping them over as we go
232 //
233 for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
Chris Lattner18961502002-06-25 16:12:52 +0000234 const GlobalVariable *SGV = I;
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000235
236 if (SGV->hasInitializer()) { // Only process initialized GV's
237 // Figure out what the initializer looks like in the dest module...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000238 Constant *DInit =
239 cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap));
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000240
241 GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);
Chris Lattnere4d25aa2001-11-26 18:59:18 +0000242 if (DGV->hasInitializer() && SGV->hasExternalLinkage() &&
243 DGV->hasExternalLinkage()) {
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000244 if (DGV->getInitializer() != DInit)
245 return Error(Err, "Global Variable Collision on '" +
246 SGV->getType()->getDescription() + "':%" +SGV->getName()+
247 " - Global variables have different initializers");
248 } else {
249 // Copy the initializer over now...
250 DGV->setInitializer(DInit);
251 }
252 }
253 }
254 return false;
255}
Chris Lattner5c377c52001-10-14 23:29:15 +0000256
Chris Lattner79df7c02002-03-26 18:01:55 +0000257// LinkFunctionProtos - Link the functions together between the two modules,
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000258// without doing function bodies... this just adds external function prototypes
259// to the Dest function...
Chris Lattner5c377c52001-10-14 23:29:15 +0000260//
Chris Lattner79df7c02002-03-26 18:01:55 +0000261static bool LinkFunctionProtos(Module *Dest, const Module *Src,
262 map<const Value*, Value*> &ValueMap,
263 string *Err = 0) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000264 // We will need a module level symbol table if the src module has a module
265 // level symbol table...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000266 SymbolTable *ST = (SymbolTable*)&Src->getSymbolTable();
Chris Lattner5c377c52001-10-14 23:29:15 +0000267
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000268 // Loop over all of the functions in the src module, mapping them over as we
269 // go
Chris Lattner5c377c52001-10-14 23:29:15 +0000270 //
271 for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
Chris Lattner18961502002-06-25 16:12:52 +0000272 const Function *SF = I; // SrcFunction
Chris Lattner5c377c52001-10-14 23:29:15 +0000273 Value *V;
274
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000275 // If the function has a name, and that name is already in use in the Dest
276 // module, make sure that the name is a compatible function...
Chris Lattner5c377c52001-10-14 23:29:15 +0000277 //
Chris Lattner18961502002-06-25 16:12:52 +0000278 if (SF->hasExternalLinkage() && SF->hasName() &&
279 (V = ST->lookup(SF->getType(), SF->getName())) &&
Chris Lattner79df7c02002-03-26 18:01:55 +0000280 cast<Function>(V)->hasExternalLinkage()) {
281 // The same named thing is a Function, because the only two things
282 // that may be in a module level symbol table are Global Vars and
283 // Functions, and they both have distinct, nonoverlapping, possible types.
Chris Lattner5c377c52001-10-14 23:29:15 +0000284 //
Chris Lattner18961502002-06-25 16:12:52 +0000285 Function *DF = cast<Function>(V); // DestFunction
Chris Lattner5c377c52001-10-14 23:29:15 +0000286
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000287 // Check to make sure the function is not defined in both modules...
Chris Lattner18961502002-06-25 16:12:52 +0000288 if (!SF->isExternal() && !DF->isExternal())
Chris Lattner79df7c02002-03-26 18:01:55 +0000289 return Error(Err, "Function '" +
Chris Lattner18961502002-06-25 16:12:52 +0000290 SF->getFunctionType()->getDescription() + "':\"" +
291 SF->getName() + "\" - Function is already defined!");
Chris Lattner5c377c52001-10-14 23:29:15 +0000292
293 // Otherwise, just remember this mapping...
Chris Lattner18961502002-06-25 16:12:52 +0000294 ValueMap.insert(std::make_pair(SF, DF));
Chris Lattner5c377c52001-10-14 23:29:15 +0000295 } else {
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000296 // Function does not already exist, simply insert an external function
Chris Lattner18961502002-06-25 16:12:52 +0000297 // signature identical to SF into the dest module...
298 Function *DF = new Function(SF->getFunctionType(),
299 SF->hasInternalLinkage(),
300 SF->getName());
Chris Lattner5c377c52001-10-14 23:29:15 +0000301
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000302 // Add the function signature to the dest module...
Chris Lattner18961502002-06-25 16:12:52 +0000303 Dest->getFunctionList().push_back(DF);
Chris Lattner5c377c52001-10-14 23:29:15 +0000304
305 // ... and remember this mapping...
Chris Lattner18961502002-06-25 16:12:52 +0000306 ValueMap.insert(std::make_pair(SF, DF));
Chris Lattner5c377c52001-10-14 23:29:15 +0000307 }
308 }
309 return false;
310}
311
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000312// LinkFunctionBody - Copy the source function over into the dest function and
313// fix up references to values. At this point we know that Dest is an external
314// function, and that Src is not.
Chris Lattner5c377c52001-10-14 23:29:15 +0000315//
Chris Lattner79df7c02002-03-26 18:01:55 +0000316static bool LinkFunctionBody(Function *Dest, const Function *Src,
Chris Lattnerd149c052002-09-23 18:14:15 +0000317 map<const Value*, Value*> &GlobalMap,
Chris Lattner79df7c02002-03-26 18:01:55 +0000318 string *Err = 0) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000319 assert(Src && Dest && Dest->isExternal() && !Src->isExternal());
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000320 map<const Value*, Value*> LocalMap; // Map for function local values
Chris Lattner5c377c52001-10-14 23:29:15 +0000321
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000322 // Go through and convert function arguments over...
Chris Lattner69da5cf2002-10-13 20:57:00 +0000323 Function::aiterator DI = Dest->abegin();
Chris Lattner18961502002-06-25 16:12:52 +0000324 for (Function::const_aiterator I = Src->abegin(), E = Src->aend();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000325 I != E; ++I, ++DI) {
326 DI->setName(I->getName()); // Copy the name information over...
Chris Lattner5c377c52001-10-14 23:29:15 +0000327
328 // Add a mapping to our local map
Chris Lattner69da5cf2002-10-13 20:57:00 +0000329 LocalMap.insert(std::make_pair(I, DI));
Chris Lattner5c377c52001-10-14 23:29:15 +0000330 }
331
332 // Loop over all of the basic blocks, copying the instructions over...
333 //
Chris Lattner79df7c02002-03-26 18:01:55 +0000334 for (Function::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000335 // Create new basic block and add to mapping and the Dest function...
Chris Lattner18961502002-06-25 16:12:52 +0000336 BasicBlock *DBB = new BasicBlock(I->getName(), Dest);
337 LocalMap.insert(std::make_pair(I, DBB));
Chris Lattner5c377c52001-10-14 23:29:15 +0000338
339 // Loop over all of the instructions in the src basic block, copying them
340 // over. Note that this is broken in a strict sense because the cloned
341 // instructions will still be referencing values in the Src module, not
342 // the remapped values. In our case, however, we will not get caught and
343 // so we can delay patching the values up until later...
344 //
Chris Lattner18961502002-06-25 16:12:52 +0000345 for (BasicBlock::const_iterator II = I->begin(), IE = I->end();
Chris Lattner5c377c52001-10-14 23:29:15 +0000346 II != IE; ++II) {
Chris Lattner18961502002-06-25 16:12:52 +0000347 Instruction *DI = II->clone();
348 DI->setName(II->getName());
Chris Lattner5c377c52001-10-14 23:29:15 +0000349 DBB->getInstList().push_back(DI);
Chris Lattner18961502002-06-25 16:12:52 +0000350 LocalMap.insert(std::make_pair(II, DI));
Chris Lattner5c377c52001-10-14 23:29:15 +0000351 }
352 }
353
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000354 // At this point, all of the instructions and values of the function are now
355 // copied over. The only problem is that they are still referencing values in
356 // the Source function as operands. Loop through all of the operands of the
357 // functions and patch them up to point to the local versions...
Chris Lattner5c377c52001-10-14 23:29:15 +0000358 //
Chris Lattner18961502002-06-25 16:12:52 +0000359 for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB)
360 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
361 for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
Chris Lattner221d6882002-02-12 21:07:25 +0000362 OI != OE; ++OI)
363 *OI = RemapOperand(*OI, LocalMap, &GlobalMap);
Chris Lattner5c377c52001-10-14 23:29:15 +0000364
365 return false;
366}
367
368
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000369// LinkFunctionBodies - Link in the function bodies that are defined in the
370// source module into the DestModule. This consists basically of copying the
371// function over and fixing up references to values.
Chris Lattner5c377c52001-10-14 23:29:15 +0000372//
Chris Lattner79df7c02002-03-26 18:01:55 +0000373static bool LinkFunctionBodies(Module *Dest, const Module *Src,
374 map<const Value*, Value*> &ValueMap,
375 string *Err = 0) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000376
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000377 // Loop over all of the functions in the src module, mapping them over as we
378 // go
Chris Lattner5c377c52001-10-14 23:29:15 +0000379 //
Chris Lattner18961502002-06-25 16:12:52 +0000380 for (Module::const_iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF){
381 if (!SF->isExternal()) { // No body if function is external
382 Function *DF = cast<Function>(ValueMap[SF]); // Destination function
Chris Lattner5c377c52001-10-14 23:29:15 +0000383
Chris Lattner18961502002-06-25 16:12:52 +0000384 // DF not external SF external?
385 if (!DF->isExternal()) {
Chris Lattnerc2d774b2001-10-23 20:43:42 +0000386 if (Err)
Chris Lattner18961502002-06-25 16:12:52 +0000387 *Err = "Function '" + (SF->hasName() ? SF->getName() : string("")) +
Chris Lattnerc2d774b2001-10-23 20:43:42 +0000388 "' body multiply defined!";
389 return true;
390 }
391
Chris Lattner18961502002-06-25 16:12:52 +0000392 if (LinkFunctionBody(DF, SF, ValueMap, Err)) return true;
Chris Lattnerc2d774b2001-10-23 20:43:42 +0000393 }
Chris Lattner5c377c52001-10-14 23:29:15 +0000394 }
395 return false;
396}
397
Chris Lattner52f7e902001-10-13 07:03:50 +0000398
399
400// LinkModules - This function links two modules together, with the resulting
401// left module modified to be the composite of the two input modules. If an
402// error occurs, true is returned and ErrorMsg (if not null) is set to indicate
Chris Lattner5c377c52001-10-14 23:29:15 +0000403// the problem. Upon failure, the Dest module could be in a modified state, and
404// shouldn't be relied on to be consistent.
Chris Lattner52f7e902001-10-13 07:03:50 +0000405//
Chris Lattnerb1443b12002-07-24 22:40:39 +0000406bool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg) {
Chris Lattner2c236f32001-11-03 05:18:24 +0000407
408 // LinkTypes - Go through the symbol table of the Src module and see if any
409 // types are named in the src module that are not named in the Dst module.
410 // Make sure there are no type name conflicts.
411 //
412 if (LinkTypes(Dest, Src, ErrorMsg)) return true;
413
Chris Lattner5c377c52001-10-14 23:29:15 +0000414 // ValueMap - Mapping of values from what they used to be in Src, to what they
415 // are now in Dest.
416 //
417 map<const Value*, Value*> ValueMap;
418
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000419 // Insert all of the globals in src into the Dest module... without
420 // initializers
Chris Lattner5c377c52001-10-14 23:29:15 +0000421 if (LinkGlobals(Dest, Src, ValueMap, ErrorMsg)) return true;
422
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000423 // Link the functions together between the two modules, without doing function
424 // bodies... this just adds external function prototypes to the Dest
425 // function... We do this so that when we begin processing function bodies,
426 // all of the global values that may be referenced are available in our
427 // ValueMap.
Chris Lattner5c377c52001-10-14 23:29:15 +0000428 //
Chris Lattner79df7c02002-03-26 18:01:55 +0000429 if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg)) return true;
Chris Lattner5c377c52001-10-14 23:29:15 +0000430
Chris Lattner6cdf1972002-07-18 00:13:08 +0000431 // Update the initializers in the Dest module now that all globals that may
432 // be referenced are in Dest.
433 //
434 if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
435
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000436 // Link in the function bodies that are defined in the source module into the
437 // DestModule. This consists basically of copying the function over and
438 // fixing up references to values.
Chris Lattner5c377c52001-10-14 23:29:15 +0000439 //
Chris Lattner79df7c02002-03-26 18:01:55 +0000440 if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true;
Chris Lattner52f7e902001-10-13 07:03:50 +0000441
442 return false;
443}
Vikram S. Adve9466f512001-10-28 21:38:02 +0000444