blob: f3955c89b44766444999d32502e399d83be981e4 [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 !=
8// * Merges methods between two modules
Chris Lattner52f7e902001-10-13 07:03:50 +00009//
10//===----------------------------------------------------------------------===//
11
12#include "llvm/Transforms/Linker.h"
Chris Lattner5c377c52001-10-14 23:29:15 +000013#include "llvm/Module.h"
Chris Lattner79df7c02002-03-26 18:01:55 +000014#include "llvm/Function.h"
Chris Lattner221d6882002-02-12 21:07:25 +000015#include "llvm/BasicBlock.h"
Chris Lattner5c377c52001-10-14 23:29:15 +000016#include "llvm/GlobalVariable.h"
17#include "llvm/SymbolTable.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/iOther.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000020#include "llvm/ConstantVals.h"
Chris Lattner697954c2002-01-20 22:54:45 +000021#include <iostream>
22using std::cerr;
23using std::string;
24using std::map;
Chris Lattner5c377c52001-10-14 23:29:15 +000025
26// Error - Simple wrapper function to conditionally assign to E and return true.
27// This just makes error return conditions a little bit simpler...
28//
29static inline bool Error(string *E, string Message) {
30 if (E) *E = Message;
31 return true;
32}
33
34#include "llvm/Assembly/Writer.h" // TODO: REMOVE
35
Chris Lattner2c236f32001-11-03 05:18:24 +000036
37// LinkTypes - Go through the symbol table of the Src module and see if any
38// types are named in the src module that are not named in the Dst module.
39// Make sure there are no type name conflicts.
40//
41static bool LinkTypes(Module *Dest, const Module *Src, string *Err = 0) {
42 // No symbol table? Can't have named types.
43 if (!Src->hasSymbolTable()) return false;
44
45 SymbolTable *DestST = Dest->getSymbolTableSure();
46 const SymbolTable *SrcST = Src->getSymbolTable();
47
48 // Look for a type plane for Type's...
49 SymbolTable::const_iterator PI = SrcST->find(Type::TypeTy);
50 if (PI == SrcST->end()) return false; // No named types, do nothing.
51
52 const SymbolTable::VarMap &VM = PI->second;
53 for (SymbolTable::type_const_iterator I = VM.begin(), E = VM.end();
54 I != E; ++I) {
55 const string &Name = I->first;
56 const Type *RHS = cast<Type>(I->second);
57
58 // Check to see if this type name is already in the dest module...
59 const Type *Entry = cast_or_null<Type>(DestST->lookup(Type::TypeTy, Name));
60 if (Entry) { // Yup, the value already exists...
61 if (Entry != RHS) // If it's the same, noop. Otherwise, error.
62 return Error(Err, "Type named '" + Name +
63 "' of different shape in modules.\n Src='" +
Chris Lattner9b638012002-03-15 20:21:29 +000064 Entry->getDescription() + "'.\n Dst='" +
Chris Lattner2c236f32001-11-03 05:18:24 +000065 RHS->getDescription() + "'");
66 } else { // Type not in dest module. Add it now.
67 // TODO: FIXME WHEN TYPES AREN'T CONST
68 DestST->insert(Name, const_cast<Type*>(RHS));
69 }
70 }
71 return false;
72}
73
Chris Lattner2d3e8bb2001-11-03 03:27:29 +000074static void PrintMap(const map<const Value*, Value*> &M) {
75 for (map<const Value*, Value*>::const_iterator I = M.begin(), E = M.end();
76 I != E; ++I) {
77 cerr << " Fr: " << (void*)I->first << " " << I->first
Chris Lattner697954c2002-01-20 22:54:45 +000078 << " To: " << (void*)I->second << " " << I->second << "\n";
Chris Lattner2d3e8bb2001-11-03 03:27:29 +000079 }
80}
81
82
Chris Lattner5c377c52001-10-14 23:29:15 +000083// RemapOperand - Use LocalMap and GlobalMap to convert references from one
84// module to another. This is somewhat sophisticated in that it can
85// automatically handle constant references correctly as well...
86//
Chris Lattner8d2de8a2001-10-15 03:12:52 +000087static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
Chris Lattner5c377c52001-10-14 23:29:15 +000088 const map<const Value*, Value*> *GlobalMap = 0) {
89 map<const Value*,Value*>::const_iterator I = LocalMap.find(In);
90 if (I != LocalMap.end()) return I->second;
91
92 if (GlobalMap) {
93 I = GlobalMap->find(In);
94 if (I != GlobalMap->end()) return I->second;
95 }
96
Chris Lattner8d2de8a2001-10-15 03:12:52 +000097 // Check to see if it's a constant that we are interesting in transforming...
Chris Lattnere9bb2df2001-12-03 22:26:30 +000098 if (Constant *CPV = dyn_cast<Constant>(In)) {
Chris Lattner8d2de8a2001-10-15 03:12:52 +000099 if (!isa<DerivedType>(CPV->getType()))
100 return CPV; // Simple constants stay identical...
101
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000102 Constant *Result = 0;
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000103
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000104 if (ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000105 const std::vector<Use> &Ops = CPA->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 = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands);
111 } else if (ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000112 const std::vector<Use> &Ops = CPS->getValues();
113 std::vector<Constant*> Operands(Ops.size());
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000114 for (unsigned i = 0; i < Ops.size(); ++i)
115 Operands[i] =
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000116 cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
117 Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands);
118 } else if (isa<ConstantPointerNull>(CPV)) {
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000119 Result = CPV;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000120 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000121 Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000122 Result = ConstantPointerRef::get(cast<GlobalValue>(V));
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000123 } else {
124 assert(0 && "Unknown type of derived type constant value!");
125 }
126
127 // Cache the mapping in our local map structure...
Chris Lattner697954c2002-01-20 22:54:45 +0000128 LocalMap.insert(std::make_pair(In, CPV));
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000129 return Result;
130 }
Chris Lattner2d3e8bb2001-11-03 03:27:29 +0000131
132 cerr << "XXX LocalMap: \n";
133 PrintMap(LocalMap);
134
135 if (GlobalMap) {
136 cerr << "XXX GlobalMap: \n";
137 PrintMap(*GlobalMap);
138 }
139
Chris Lattner697954c2002-01-20 22:54:45 +0000140 cerr << "Couldn't remap value: " << (void*)In << " " << In << "\n";
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000141 assert(0 && "Couldn't remap value!");
142 return 0;
Chris Lattner5c377c52001-10-14 23:29:15 +0000143}
144
145
146// LinkGlobals - Loop through the global variables in the src module and merge
147// them into the dest module...
148//
149static bool LinkGlobals(Module *Dest, const Module *Src,
150 map<const Value*, Value*> &ValueMap, string *Err = 0) {
151 // We will need a module level symbol table if the src module has a module
152 // level symbol table...
153 SymbolTable *ST = Src->getSymbolTable() ? Dest->getSymbolTableSure() : 0;
154
155 // Loop over all of the globals in the src module, mapping them over as we go
156 //
157 for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000158 const GlobalVariable *SGV = *I;
Chris Lattner5c377c52001-10-14 23:29:15 +0000159 Value *V;
160
161 // If the global variable has a name, and that name is already in use in the
162 // Dest module, make sure that the name is a compatible global variable...
163 //
Chris Lattnere4d25aa2001-11-26 18:59:18 +0000164 if (SGV->hasExternalLinkage() && SGV->hasName() &&
165 (V = ST->lookup(SGV->getType(), SGV->getName())) &&
166 cast<GlobalVariable>(V)->hasExternalLinkage()) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000167 // The same named thing is a global variable, because the only two things
Chris Lattner79df7c02002-03-26 18:01:55 +0000168 // that may be in a module level symbol table are Global Vars and
169 // Functions, and they both have distinct, nonoverlapping, possible types.
Chris Lattner5c377c52001-10-14 23:29:15 +0000170 //
171 GlobalVariable *DGV = cast<GlobalVariable>(V);
172
173 // Check to see if the two GV's have the same Const'ness...
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000174 if (SGV->isConstant() != DGV->isConstant())
Chris Lattner5c377c52001-10-14 23:29:15 +0000175 return Error(Err, "Global Variable Collision on '" +
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000176 SGV->getType()->getDescription() + "':%" + SGV->getName() +
Chris Lattner5c377c52001-10-14 23:29:15 +0000177 " - Global variables differ in const'ness");
178
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000179 // Okay, everything is cool, remember the mapping...
Chris Lattner697954c2002-01-20 22:54:45 +0000180 ValueMap.insert(std::make_pair(SGV, DGV));
Chris Lattner5c377c52001-10-14 23:29:15 +0000181 } else {
182 // No linking to be performed, simply create an identical version of the
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000183 // symbol over in the dest module... the initializer will be filled in
184 // later by LinkGlobalInits...
185 //
186 GlobalVariable *DGV =
Chris Lattner7a176752001-12-04 00:03:30 +0000187 new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(),
Chris Lattnere4d25aa2001-11-26 18:59:18 +0000188 SGV->hasInternalLinkage(), 0, SGV->getName());
Chris Lattner5c377c52001-10-14 23:29:15 +0000189
190 // Add the new global to the dest module
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000191 Dest->getGlobalList().push_back(DGV);
Chris Lattner5c377c52001-10-14 23:29:15 +0000192
193 // Make sure to remember this mapping...
Chris Lattner697954c2002-01-20 22:54:45 +0000194 ValueMap.insert(std::make_pair(SGV, DGV));
Chris Lattner5c377c52001-10-14 23:29:15 +0000195 }
196 }
197 return false;
198}
199
200
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000201// LinkGlobalInits - Update the initializers in the Dest module now that all
202// globals that may be referenced are in Dest.
203//
204static bool LinkGlobalInits(Module *Dest, const Module *Src,
205 map<const Value*, Value*> &ValueMap,
206 string *Err = 0) {
207
208 // Loop over all of the globals in the src module, mapping them over as we go
209 //
210 for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
211 const GlobalVariable *SGV = *I;
212
213 if (SGV->hasInitializer()) { // Only process initialized GV's
214 // Figure out what the initializer looks like in the dest module...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000215 Constant *DInit =
216 cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap));
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000217
218 GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);
Chris Lattnere4d25aa2001-11-26 18:59:18 +0000219 if (DGV->hasInitializer() && SGV->hasExternalLinkage() &&
220 DGV->hasExternalLinkage()) {
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000221 if (DGV->getInitializer() != DInit)
222 return Error(Err, "Global Variable Collision on '" +
223 SGV->getType()->getDescription() + "':%" +SGV->getName()+
224 " - Global variables have different initializers");
225 } else {
226 // Copy the initializer over now...
227 DGV->setInitializer(DInit);
228 }
229 }
230 }
231 return false;
232}
Chris Lattner5c377c52001-10-14 23:29:15 +0000233
Chris Lattner79df7c02002-03-26 18:01:55 +0000234// LinkFunctionProtos - Link the functions together between the two modules,
235// without doing method bodies... this just adds external method prototypes to
236// the Dest function...
Chris Lattner5c377c52001-10-14 23:29:15 +0000237//
Chris Lattner79df7c02002-03-26 18:01:55 +0000238static bool LinkFunctionProtos(Module *Dest, const Module *Src,
239 map<const Value*, Value*> &ValueMap,
240 string *Err = 0) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000241 // We will need a module level symbol table if the src module has a module
242 // level symbol table...
243 SymbolTable *ST = Src->getSymbolTable() ? Dest->getSymbolTableSure() : 0;
244
245 // Loop over all of the methods in the src module, mapping them over as we go
246 //
247 for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000248 const Function *SM = *I; // SrcFunction
Chris Lattner5c377c52001-10-14 23:29:15 +0000249 Value *V;
250
251 // If the method has a name, and that name is already in use in the
252 // Dest module, make sure that the name is a compatible method...
253 //
Chris Lattnere4d25aa2001-11-26 18:59:18 +0000254 if (SM->hasExternalLinkage() && SM->hasName() &&
255 (V = ST->lookup(SM->getType(), SM->getName())) &&
Chris Lattner79df7c02002-03-26 18:01:55 +0000256 cast<Function>(V)->hasExternalLinkage()) {
257 // The same named thing is a Function, because the only two things
258 // that may be in a module level symbol table are Global Vars and
259 // Functions, and they both have distinct, nonoverlapping, possible types.
Chris Lattner5c377c52001-10-14 23:29:15 +0000260 //
Chris Lattner79df7c02002-03-26 18:01:55 +0000261 Function *DM = cast<Function>(V); // DestFunction
Chris Lattner5c377c52001-10-14 23:29:15 +0000262
263 // Check to make sure the method is not defined in both modules...
264 if (!SM->isExternal() && !DM->isExternal())
Chris Lattner79df7c02002-03-26 18:01:55 +0000265 return Error(Err, "Function '" +
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000266 SM->getFunctionType()->getDescription() + "':\"" +
Chris Lattner79df7c02002-03-26 18:01:55 +0000267 SM->getName() + "\" - Function is already defined!");
Chris Lattner5c377c52001-10-14 23:29:15 +0000268
269 // Otherwise, just remember this mapping...
Chris Lattner697954c2002-01-20 22:54:45 +0000270 ValueMap.insert(std::make_pair(SM, DM));
Chris Lattner5c377c52001-10-14 23:29:15 +0000271 } else {
Chris Lattner79df7c02002-03-26 18:01:55 +0000272 // Function does not already exist, simply insert an external method
Chris Lattner5c377c52001-10-14 23:29:15 +0000273 // signature identical to SM into the dest module...
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000274 Function *DM = new Function(SM->getFunctionType(),
275 SM->hasInternalLinkage(),
Chris Lattner79df7c02002-03-26 18:01:55 +0000276 SM->getName());
Chris Lattner5c377c52001-10-14 23:29:15 +0000277
278 // Add the method signature to the dest module...
Chris Lattner79df7c02002-03-26 18:01:55 +0000279 Dest->getFunctionList().push_back(DM);
Chris Lattner5c377c52001-10-14 23:29:15 +0000280
281 // ... and remember this mapping...
Chris Lattner697954c2002-01-20 22:54:45 +0000282 ValueMap.insert(std::make_pair(SM, DM));
Chris Lattner5c377c52001-10-14 23:29:15 +0000283 }
284 }
285 return false;
286}
287
Chris Lattner79df7c02002-03-26 18:01:55 +0000288// LinkFunctionBody - Copy the source method over into the dest method
289// and fix up references to values. At this point we know that Dest
290// is an external method, and that Src is not.
Chris Lattner5c377c52001-10-14 23:29:15 +0000291//
Chris Lattner79df7c02002-03-26 18:01:55 +0000292static bool LinkFunctionBody(Function *Dest, const Function *Src,
293 const map<const Value*, Value*> &GlobalMap,
294 string *Err = 0) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000295 assert(Src && Dest && Dest->isExternal() && !Src->isExternal());
296 map<const Value*, Value*> LocalMap; // Map for method local values
297
298 // Go through and convert method arguments over...
Chris Lattner79df7c02002-03-26 18:01:55 +0000299 for (Function::ArgumentListType::const_iterator
Chris Lattner5c377c52001-10-14 23:29:15 +0000300 I = Src->getArgumentList().begin(),
301 E = Src->getArgumentList().end(); I != E; ++I) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000302 const FunctionArgument *SMA = *I;
Chris Lattner5c377c52001-10-14 23:29:15 +0000303
304 // Create the new method argument and add to the dest method...
Chris Lattner79df7c02002-03-26 18:01:55 +0000305 FunctionArgument *DMA = new FunctionArgument(SMA->getType(),SMA->getName());
Chris Lattner5c377c52001-10-14 23:29:15 +0000306 Dest->getArgumentList().push_back(DMA);
307
308 // Add a mapping to our local map
Chris Lattner697954c2002-01-20 22:54:45 +0000309 LocalMap.insert(std::make_pair(SMA, DMA));
Chris Lattner5c377c52001-10-14 23:29:15 +0000310 }
311
312 // Loop over all of the basic blocks, copying the instructions over...
313 //
Chris Lattner79df7c02002-03-26 18:01:55 +0000314 for (Function::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000315 const BasicBlock *SBB = *I;
316
317 // Create new basic block and add to mapping and the Dest method...
318 BasicBlock *DBB = new BasicBlock(SBB->getName(), Dest);
Chris Lattner697954c2002-01-20 22:54:45 +0000319 LocalMap.insert(std::make_pair(SBB, DBB));
Chris Lattner5c377c52001-10-14 23:29:15 +0000320
321 // Loop over all of the instructions in the src basic block, copying them
322 // over. Note that this is broken in a strict sense because the cloned
323 // instructions will still be referencing values in the Src module, not
324 // the remapped values. In our case, however, we will not get caught and
325 // so we can delay patching the values up until later...
326 //
327 for (BasicBlock::const_iterator II = SBB->begin(), IE = SBB->end();
328 II != IE; ++II) {
329 const Instruction *SI = *II;
330 Instruction *DI = SI->clone();
Chris Lattner43a6f2e2001-10-29 16:55:41 +0000331 DI->setName(SI->getName());
Chris Lattner5c377c52001-10-14 23:29:15 +0000332 DBB->getInstList().push_back(DI);
Chris Lattner697954c2002-01-20 22:54:45 +0000333 LocalMap.insert(std::make_pair(SI, DI));
Chris Lattner5c377c52001-10-14 23:29:15 +0000334 }
335 }
336
337 // At this point, all of the instructions and values of the method are now
338 // copied over. The only problem is that they are still referencing values
339 // in the Source method as operands. Loop through all of the operands of the
340 // methods and patch them up to point to the local versions...
341 //
Chris Lattner79df7c02002-03-26 18:01:55 +0000342 for (Function::iterator BI = Dest->begin(), BE = Dest->end();
Chris Lattner221d6882002-02-12 21:07:25 +0000343 BI != BE; ++BI) {
344 BasicBlock *BB = *BI;
345 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
346 Instruction *Inst = *I;
347
348 for (Instruction::op_iterator OI = Inst->op_begin(), OE = Inst->op_end();
349 OI != OE; ++OI)
350 *OI = RemapOperand(*OI, LocalMap, &GlobalMap);
351 }
Chris Lattner5c377c52001-10-14 23:29:15 +0000352 }
353
354 return false;
355}
356
357
Chris Lattner79df7c02002-03-26 18:01:55 +0000358// LinkFunctionBodies - Link in the method bodies that are defined in the source
Chris Lattner5c377c52001-10-14 23:29:15 +0000359// module into the DestModule. This consists basically of copying the method
360// over and fixing up references to values.
361//
Chris Lattner79df7c02002-03-26 18:01:55 +0000362static bool LinkFunctionBodies(Module *Dest, const Module *Src,
363 map<const Value*, Value*> &ValueMap,
364 string *Err = 0) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000365
366 // Loop over all of the methods in the src module, mapping them over as we go
367 //
368 for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000369 const Function *SM = *I; // Source Function
Chris Lattnerc2d774b2001-10-23 20:43:42 +0000370 if (!SM->isExternal()) { // No body if method is external
Chris Lattner79df7c02002-03-26 18:01:55 +0000371 Function *DM = cast<Function>(ValueMap[SM]); // Destination method
Chris Lattner5c377c52001-10-14 23:29:15 +0000372
Chris Lattnerc2d774b2001-10-23 20:43:42 +0000373 // DM not external SM external?
374 if (!DM->isExternal()) {
375 if (Err)
Chris Lattner79df7c02002-03-26 18:01:55 +0000376 *Err = "Function '" + (SM->hasName() ? SM->getName() : string("")) +
Chris Lattnerc2d774b2001-10-23 20:43:42 +0000377 "' body multiply defined!";
378 return true;
379 }
380
Chris Lattner79df7c02002-03-26 18:01:55 +0000381 if (LinkFunctionBody(DM, SM, ValueMap, Err)) return true;
Chris Lattnerc2d774b2001-10-23 20:43:42 +0000382 }
Chris Lattner5c377c52001-10-14 23:29:15 +0000383 }
384 return false;
385}
386
Chris Lattner52f7e902001-10-13 07:03:50 +0000387
388
389// LinkModules - This function links two modules together, with the resulting
390// left module modified to be the composite of the two input modules. If an
391// error occurs, true is returned and ErrorMsg (if not null) is set to indicate
Chris Lattner5c377c52001-10-14 23:29:15 +0000392// the problem. Upon failure, the Dest module could be in a modified state, and
393// shouldn't be relied on to be consistent.
Chris Lattner52f7e902001-10-13 07:03:50 +0000394//
395bool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg = 0) {
Chris Lattner2c236f32001-11-03 05:18:24 +0000396
397 // LinkTypes - Go through the symbol table of the Src module and see if any
398 // types are named in the src module that are not named in the Dst module.
399 // Make sure there are no type name conflicts.
400 //
401 if (LinkTypes(Dest, Src, ErrorMsg)) return true;
402
Chris Lattner5c377c52001-10-14 23:29:15 +0000403 // ValueMap - Mapping of values from what they used to be in Src, to what they
404 // are now in Dest.
405 //
406 map<const Value*, Value*> ValueMap;
407
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000408 // Insert all of the globals in src into the Dest module... without
409 // initializers
Chris Lattner5c377c52001-10-14 23:29:15 +0000410 if (LinkGlobals(Dest, Src, ValueMap, ErrorMsg)) return true;
411
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000412 // Update the initializers in the Dest module now that all globals that may
413 // be referenced are in Dest.
414 //
415 if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
416
Chris Lattner5c377c52001-10-14 23:29:15 +0000417 // Link the methods together between the two modules, without doing method
418 // bodies... this just adds external method prototypes to the Dest method...
419 // We do this so that when we begin processing method bodies, all of the
420 // global values that may be referenced are available in our ValueMap.
421 //
Chris Lattner79df7c02002-03-26 18:01:55 +0000422 if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg)) return true;
Chris Lattner5c377c52001-10-14 23:29:15 +0000423
424 // Link in the method bodies that are defined in the source module into the
425 // DestModule. This consists basically of copying the method over and fixing
426 // up references to values.
427 //
Chris Lattner79df7c02002-03-26 18:01:55 +0000428 if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true;
Chris Lattner52f7e902001-10-13 07:03:50 +0000429
430 return false;
431}
Vikram S. Adve9466f512001-10-28 21:38:02 +0000432