blob: fb83e027b06ced8aac737b54b5f982ca570ec0e2 [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 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 Lattner31bcdb82002-04-28 19:55:58 +000020#include "llvm/Constants.h"
Chris Lattner73e21422002-04-09 19:48:49 +000021#include "llvm/Argument.h"
Chris Lattner697954c2002-01-20 22:54:45 +000022#include <iostream>
23using std::cerr;
24using std::string;
25using std::map;
Chris Lattner5c377c52001-10-14 23:29:15 +000026
27// Error - Simple wrapper function to conditionally assign to E and return true.
28// This just makes error return conditions a little bit simpler...
29//
30static inline bool Error(string *E, string Message) {
31 if (E) *E = Message;
32 return true;
33}
34
Chris Lattner2c236f32001-11-03 05:18:24 +000035// LinkTypes - Go through the symbol table of the Src module and see if any
36// types are named in the src module that are not named in the Dst module.
37// Make sure there are no type name conflicts.
38//
39static bool LinkTypes(Module *Dest, const Module *Src, string *Err = 0) {
40 // No symbol table? Can't have named types.
41 if (!Src->hasSymbolTable()) return false;
42
43 SymbolTable *DestST = Dest->getSymbolTableSure();
44 const SymbolTable *SrcST = Src->getSymbolTable();
45
46 // Look for a type plane for Type's...
47 SymbolTable::const_iterator PI = SrcST->find(Type::TypeTy);
48 if (PI == SrcST->end()) return false; // No named types, do nothing.
49
50 const SymbolTable::VarMap &VM = PI->second;
51 for (SymbolTable::type_const_iterator I = VM.begin(), E = VM.end();
52 I != E; ++I) {
53 const string &Name = I->first;
54 const Type *RHS = cast<Type>(I->second);
55
56 // Check to see if this type name is already in the dest module...
57 const Type *Entry = cast_or_null<Type>(DestST->lookup(Type::TypeTy, Name));
58 if (Entry) { // Yup, the value already exists...
59 if (Entry != RHS) // If it's the same, noop. Otherwise, error.
60 return Error(Err, "Type named '" + Name +
61 "' of different shape in modules.\n Src='" +
Chris Lattner9b638012002-03-15 20:21:29 +000062 Entry->getDescription() + "'.\n Dst='" +
Chris Lattner2c236f32001-11-03 05:18:24 +000063 RHS->getDescription() + "'");
64 } else { // Type not in dest module. Add it now.
65 // TODO: FIXME WHEN TYPES AREN'T CONST
66 DestST->insert(Name, const_cast<Type*>(RHS));
67 }
68 }
69 return false;
70}
71
Chris Lattner2d3e8bb2001-11-03 03:27:29 +000072static void PrintMap(const map<const Value*, Value*> &M) {
73 for (map<const Value*, Value*>::const_iterator I = M.begin(), E = M.end();
74 I != E; ++I) {
Chris Lattner87182ae2002-04-07 22:31:23 +000075 cerr << " Fr: " << (void*)I->first << " ";
76 I->first->dump();
77 cerr << " To: " << (void*)I->second << " ";
78 I->second->dump();
79 cerr << "\n";
Chris Lattner2d3e8bb2001-11-03 03:27:29 +000080 }
81}
82
83
Chris Lattner5c377c52001-10-14 23:29:15 +000084// RemapOperand - Use LocalMap and GlobalMap to convert references from one
85// module to another. This is somewhat sophisticated in that it can
86// automatically handle constant references correctly as well...
87//
Chris Lattner8d2de8a2001-10-15 03:12:52 +000088static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
Chris Lattner5c377c52001-10-14 23:29:15 +000089 const map<const Value*, Value*> *GlobalMap = 0) {
90 map<const Value*,Value*>::const_iterator I = LocalMap.find(In);
91 if (I != LocalMap.end()) return I->second;
92
93 if (GlobalMap) {
94 I = GlobalMap->find(In);
95 if (I != GlobalMap->end()) return I->second;
96 }
97
Chris Lattner8d2de8a2001-10-15 03:12:52 +000098 // Check to see if it's a constant that we are interesting in transforming...
Chris Lattner18961502002-06-25 16:12:52 +000099 if (const Constant *CPV = dyn_cast<Constant>(In)) {
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000100 if (!isa<DerivedType>(CPV->getType()))
Chris Lattner18961502002-06-25 16:12:52 +0000101 return const_cast<Constant*>(CPV); // Simple constants stay identical...
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000102
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000103 Constant *Result = 0;
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000104
Chris Lattner18961502002-06-25 16:12:52 +0000105 if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000106 const std::vector<Use> &Ops = CPA->getValues();
107 std::vector<Constant*> Operands(Ops.size());
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000108 for (unsigned i = 0; i < Ops.size(); ++i)
109 Operands[i] =
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000110 cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
111 Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands);
Chris Lattner18961502002-06-25 16:12:52 +0000112 } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000113 const std::vector<Use> &Ops = CPS->getValues();
114 std::vector<Constant*> Operands(Ops.size());
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000115 for (unsigned i = 0; i < Ops.size(); ++i)
116 Operands[i] =
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000117 cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
118 Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands);
119 } else if (isa<ConstantPointerNull>(CPV)) {
Chris Lattner18961502002-06-25 16:12:52 +0000120 Result = const_cast<Constant*>(CPV);
121 } else if (const ConstantPointerRef *CPR =
122 dyn_cast<ConstantPointerRef>(CPV)) {
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000123 Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000124 Result = ConstantPointerRef::get(cast<GlobalValue>(V));
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000125 } else {
126 assert(0 && "Unknown type of derived type constant value!");
127 }
128
129 // Cache the mapping in our local map structure...
Chris Lattner18961502002-06-25 16:12:52 +0000130 LocalMap.insert(std::make_pair(In, const_cast<Constant*>(CPV)));
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000131 return Result;
132 }
Chris Lattner2d3e8bb2001-11-03 03:27:29 +0000133
134 cerr << "XXX LocalMap: \n";
135 PrintMap(LocalMap);
136
137 if (GlobalMap) {
138 cerr << "XXX GlobalMap: \n";
139 PrintMap(*GlobalMap);
140 }
141
Chris Lattner87182ae2002-04-07 22:31:23 +0000142 cerr << "Couldn't remap value: " << (void*)In << " ";
143 In->dump();
144 cerr << "\n";
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000145 assert(0 && "Couldn't remap value!");
146 return 0;
Chris Lattner5c377c52001-10-14 23:29:15 +0000147}
148
149
150// LinkGlobals - Loop through the global variables in the src module and merge
151// them into the dest module...
152//
153static bool LinkGlobals(Module *Dest, const Module *Src,
154 map<const Value*, Value*> &ValueMap, string *Err = 0) {
155 // We will need a module level symbol table if the src module has a module
156 // level symbol table...
157 SymbolTable *ST = Src->getSymbolTable() ? Dest->getSymbolTableSure() : 0;
158
159 // Loop over all of the globals in the src module, mapping them over as we go
160 //
161 for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
Chris Lattner18961502002-06-25 16:12:52 +0000162 const GlobalVariable *SGV = I;
Chris Lattner5c377c52001-10-14 23:29:15 +0000163 Value *V;
164
165 // If the global variable has a name, and that name is already in use in the
166 // Dest module, make sure that the name is a compatible global variable...
167 //
Chris Lattnere4d25aa2001-11-26 18:59:18 +0000168 if (SGV->hasExternalLinkage() && SGV->hasName() &&
169 (V = ST->lookup(SGV->getType(), SGV->getName())) &&
170 cast<GlobalVariable>(V)->hasExternalLinkage()) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000171 // The same named thing is a global variable, because the only two things
Chris Lattner79df7c02002-03-26 18:01:55 +0000172 // that may be in a module level symbol table are Global Vars and
173 // Functions, and they both have distinct, nonoverlapping, possible types.
Chris Lattner5c377c52001-10-14 23:29:15 +0000174 //
175 GlobalVariable *DGV = cast<GlobalVariable>(V);
176
177 // Check to see if the two GV's have the same Const'ness...
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000178 if (SGV->isConstant() != DGV->isConstant())
Chris Lattner5c377c52001-10-14 23:29:15 +0000179 return Error(Err, "Global Variable Collision on '" +
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000180 SGV->getType()->getDescription() + "':%" + SGV->getName() +
Chris Lattner5c377c52001-10-14 23:29:15 +0000181 " - Global variables differ in const'ness");
182
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000183 // Okay, everything is cool, remember the mapping...
Chris Lattner697954c2002-01-20 22:54:45 +0000184 ValueMap.insert(std::make_pair(SGV, DGV));
Chris Lattner5c377c52001-10-14 23:29:15 +0000185 } else {
186 // No linking to be performed, simply create an identical version of the
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000187 // symbol over in the dest module... the initializer will be filled in
188 // later by LinkGlobalInits...
189 //
190 GlobalVariable *DGV =
Chris Lattner7a176752001-12-04 00:03:30 +0000191 new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(),
Chris Lattnere4d25aa2001-11-26 18:59:18 +0000192 SGV->hasInternalLinkage(), 0, SGV->getName());
Chris Lattner5c377c52001-10-14 23:29:15 +0000193
194 // Add the new global to the dest module
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000195 Dest->getGlobalList().push_back(DGV);
Chris Lattner5c377c52001-10-14 23:29:15 +0000196
197 // Make sure to remember this mapping...
Chris Lattner697954c2002-01-20 22:54:45 +0000198 ValueMap.insert(std::make_pair(SGV, DGV));
Chris Lattner5c377c52001-10-14 23:29:15 +0000199 }
200 }
201 return false;
202}
203
204
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000205// LinkGlobalInits - Update the initializers in the Dest module now that all
206// globals that may be referenced are in Dest.
207//
208static bool LinkGlobalInits(Module *Dest, const Module *Src,
209 map<const Value*, Value*> &ValueMap,
210 string *Err = 0) {
211
212 // Loop over all of the globals in the src module, mapping them over as we go
213 //
214 for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
Chris Lattner18961502002-06-25 16:12:52 +0000215 const GlobalVariable *SGV = I;
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000216
217 if (SGV->hasInitializer()) { // Only process initialized GV's
218 // Figure out what the initializer looks like in the dest module...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000219 Constant *DInit =
220 cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap));
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000221
222 GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);
Chris Lattnere4d25aa2001-11-26 18:59:18 +0000223 if (DGV->hasInitializer() && SGV->hasExternalLinkage() &&
224 DGV->hasExternalLinkage()) {
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000225 if (DGV->getInitializer() != DInit)
226 return Error(Err, "Global Variable Collision on '" +
227 SGV->getType()->getDescription() + "':%" +SGV->getName()+
228 " - Global variables have different initializers");
229 } else {
230 // Copy the initializer over now...
231 DGV->setInitializer(DInit);
232 }
233 }
234 }
235 return false;
236}
Chris Lattner5c377c52001-10-14 23:29:15 +0000237
Chris Lattner79df7c02002-03-26 18:01:55 +0000238// LinkFunctionProtos - Link the functions together between the two modules,
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000239// without doing function bodies... this just adds external function prototypes
240// to the Dest function...
Chris Lattner5c377c52001-10-14 23:29:15 +0000241//
Chris Lattner79df7c02002-03-26 18:01:55 +0000242static bool LinkFunctionProtos(Module *Dest, const Module *Src,
243 map<const Value*, Value*> &ValueMap,
244 string *Err = 0) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000245 // We will need a module level symbol table if the src module has a module
246 // level symbol table...
247 SymbolTable *ST = Src->getSymbolTable() ? Dest->getSymbolTableSure() : 0;
248
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000249 // Loop over all of the functions in the src module, mapping them over as we
250 // go
Chris Lattner5c377c52001-10-14 23:29:15 +0000251 //
252 for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
Chris Lattner18961502002-06-25 16:12:52 +0000253 const Function *SF = I; // SrcFunction
Chris Lattner5c377c52001-10-14 23:29:15 +0000254 Value *V;
255
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000256 // If the function has a name, and that name is already in use in the Dest
257 // module, make sure that the name is a compatible function...
Chris Lattner5c377c52001-10-14 23:29:15 +0000258 //
Chris Lattner18961502002-06-25 16:12:52 +0000259 if (SF->hasExternalLinkage() && SF->hasName() &&
260 (V = ST->lookup(SF->getType(), SF->getName())) &&
Chris Lattner79df7c02002-03-26 18:01:55 +0000261 cast<Function>(V)->hasExternalLinkage()) {
262 // The same named thing is a Function, because the only two things
263 // that may be in a module level symbol table are Global Vars and
264 // Functions, and they both have distinct, nonoverlapping, possible types.
Chris Lattner5c377c52001-10-14 23:29:15 +0000265 //
Chris Lattner18961502002-06-25 16:12:52 +0000266 Function *DF = cast<Function>(V); // DestFunction
Chris Lattner5c377c52001-10-14 23:29:15 +0000267
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000268 // Check to make sure the function is not defined in both modules...
Chris Lattner18961502002-06-25 16:12:52 +0000269 if (!SF->isExternal() && !DF->isExternal())
Chris Lattner79df7c02002-03-26 18:01:55 +0000270 return Error(Err, "Function '" +
Chris Lattner18961502002-06-25 16:12:52 +0000271 SF->getFunctionType()->getDescription() + "':\"" +
272 SF->getName() + "\" - Function is already defined!");
Chris Lattner5c377c52001-10-14 23:29:15 +0000273
274 // Otherwise, just remember this mapping...
Chris Lattner18961502002-06-25 16:12:52 +0000275 ValueMap.insert(std::make_pair(SF, DF));
Chris Lattner5c377c52001-10-14 23:29:15 +0000276 } else {
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000277 // Function does not already exist, simply insert an external function
Chris Lattner18961502002-06-25 16:12:52 +0000278 // signature identical to SF into the dest module...
279 Function *DF = new Function(SF->getFunctionType(),
280 SF->hasInternalLinkage(),
281 SF->getName());
Chris Lattner5c377c52001-10-14 23:29:15 +0000282
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000283 // Add the function signature to the dest module...
Chris Lattner18961502002-06-25 16:12:52 +0000284 Dest->getFunctionList().push_back(DF);
Chris Lattner5c377c52001-10-14 23:29:15 +0000285
286 // ... and remember this mapping...
Chris Lattner18961502002-06-25 16:12:52 +0000287 ValueMap.insert(std::make_pair(SF, DF));
Chris Lattner5c377c52001-10-14 23:29:15 +0000288 }
289 }
290 return false;
291}
292
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000293// LinkFunctionBody - Copy the source function over into the dest function and
294// fix up references to values. At this point we know that Dest is an external
295// function, and that Src is not.
Chris Lattner5c377c52001-10-14 23:29:15 +0000296//
Chris Lattner79df7c02002-03-26 18:01:55 +0000297static bool LinkFunctionBody(Function *Dest, const Function *Src,
298 const map<const Value*, Value*> &GlobalMap,
299 string *Err = 0) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000300 assert(Src && Dest && Dest->isExternal() && !Src->isExternal());
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000301 map<const Value*, Value*> LocalMap; // Map for function local values
Chris Lattner5c377c52001-10-14 23:29:15 +0000302
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000303 // Go through and convert function arguments over...
Chris Lattner18961502002-06-25 16:12:52 +0000304 for (Function::const_aiterator I = Src->abegin(), E = Src->aend();
305 I != E; ++I) {
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000306 // Create the new function argument and add to the dest function...
Chris Lattner18961502002-06-25 16:12:52 +0000307 Argument *DFA = new Argument(I->getType(), I->getName());
308 Dest->getArgumentList().push_back(DFA);
Chris Lattner5c377c52001-10-14 23:29:15 +0000309
310 // Add a mapping to our local map
Chris Lattner18961502002-06-25 16:12:52 +0000311 LocalMap.insert(std::make_pair(I, DFA));
Chris Lattner5c377c52001-10-14 23:29:15 +0000312 }
313
314 // Loop over all of the basic blocks, copying the instructions over...
315 //
Chris Lattner79df7c02002-03-26 18:01:55 +0000316 for (Function::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000317 // Create new basic block and add to mapping and the Dest function...
Chris Lattner18961502002-06-25 16:12:52 +0000318 BasicBlock *DBB = new BasicBlock(I->getName(), Dest);
319 LocalMap.insert(std::make_pair(I, 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 //
Chris Lattner18961502002-06-25 16:12:52 +0000327 for (BasicBlock::const_iterator II = I->begin(), IE = I->end();
Chris Lattner5c377c52001-10-14 23:29:15 +0000328 II != IE; ++II) {
Chris Lattner18961502002-06-25 16:12:52 +0000329 Instruction *DI = II->clone();
330 DI->setName(II->getName());
Chris Lattner5c377c52001-10-14 23:29:15 +0000331 DBB->getInstList().push_back(DI);
Chris Lattner18961502002-06-25 16:12:52 +0000332 LocalMap.insert(std::make_pair(II, DI));
Chris Lattner5c377c52001-10-14 23:29:15 +0000333 }
334 }
335
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000336 // At this point, all of the instructions and values of the function are now
337 // copied over. The only problem is that they are still referencing values in
338 // the Source function as operands. Loop through all of the operands of the
339 // functions and patch them up to point to the local versions...
Chris Lattner5c377c52001-10-14 23:29:15 +0000340 //
Chris Lattner18961502002-06-25 16:12:52 +0000341 for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB)
342 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
343 for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
Chris Lattner221d6882002-02-12 21:07:25 +0000344 OI != OE; ++OI)
345 *OI = RemapOperand(*OI, LocalMap, &GlobalMap);
Chris Lattner5c377c52001-10-14 23:29:15 +0000346
347 return false;
348}
349
350
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000351// LinkFunctionBodies - Link in the function bodies that are defined in the
352// source module into the DestModule. This consists basically of copying the
353// function over and fixing up references to values.
Chris Lattner5c377c52001-10-14 23:29:15 +0000354//
Chris Lattner79df7c02002-03-26 18:01:55 +0000355static bool LinkFunctionBodies(Module *Dest, const Module *Src,
356 map<const Value*, Value*> &ValueMap,
357 string *Err = 0) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000358
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000359 // Loop over all of the functions in the src module, mapping them over as we
360 // go
Chris Lattner5c377c52001-10-14 23:29:15 +0000361 //
Chris Lattner18961502002-06-25 16:12:52 +0000362 for (Module::const_iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF){
363 if (!SF->isExternal()) { // No body if function is external
364 Function *DF = cast<Function>(ValueMap[SF]); // Destination function
Chris Lattner5c377c52001-10-14 23:29:15 +0000365
Chris Lattner18961502002-06-25 16:12:52 +0000366 // DF not external SF external?
367 if (!DF->isExternal()) {
Chris Lattnerc2d774b2001-10-23 20:43:42 +0000368 if (Err)
Chris Lattner18961502002-06-25 16:12:52 +0000369 *Err = "Function '" + (SF->hasName() ? SF->getName() : string("")) +
Chris Lattnerc2d774b2001-10-23 20:43:42 +0000370 "' body multiply defined!";
371 return true;
372 }
373
Chris Lattner18961502002-06-25 16:12:52 +0000374 if (LinkFunctionBody(DF, SF, ValueMap, Err)) return true;
Chris Lattnerc2d774b2001-10-23 20:43:42 +0000375 }
Chris Lattner5c377c52001-10-14 23:29:15 +0000376 }
377 return false;
378}
379
Chris Lattner52f7e902001-10-13 07:03:50 +0000380
381
382// LinkModules - This function links two modules together, with the resulting
383// left module modified to be the composite of the two input modules. If an
384// error occurs, true is returned and ErrorMsg (if not null) is set to indicate
Chris Lattner5c377c52001-10-14 23:29:15 +0000385// the problem. Upon failure, the Dest module could be in a modified state, and
386// shouldn't be relied on to be consistent.
Chris Lattner52f7e902001-10-13 07:03:50 +0000387//
388bool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg = 0) {
Chris Lattner2c236f32001-11-03 05:18:24 +0000389
390 // LinkTypes - Go through the symbol table of the Src module and see if any
391 // types are named in the src module that are not named in the Dst module.
392 // Make sure there are no type name conflicts.
393 //
394 if (LinkTypes(Dest, Src, ErrorMsg)) return true;
395
Chris Lattner5c377c52001-10-14 23:29:15 +0000396 // ValueMap - Mapping of values from what they used to be in Src, to what they
397 // are now in Dest.
398 //
399 map<const Value*, Value*> ValueMap;
400
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000401 // Insert all of the globals in src into the Dest module... without
402 // initializers
Chris Lattner5c377c52001-10-14 23:29:15 +0000403 if (LinkGlobals(Dest, Src, ValueMap, ErrorMsg)) return true;
404
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000405 // Update the initializers in the Dest module now that all globals that may
406 // be referenced are in Dest.
407 //
408 if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
409
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000410 // Link the functions together between the two modules, without doing function
411 // bodies... this just adds external function prototypes to the Dest
412 // function... We do this so that when we begin processing function bodies,
413 // all of the global values that may be referenced are available in our
414 // ValueMap.
Chris Lattner5c377c52001-10-14 23:29:15 +0000415 //
Chris Lattner79df7c02002-03-26 18:01:55 +0000416 if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg)) return true;
Chris Lattner5c377c52001-10-14 23:29:15 +0000417
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000418 // Link in the function bodies that are defined in the source module into the
419 // DestModule. This consists basically of copying the function over and
420 // fixing up references to values.
Chris Lattner5c377c52001-10-14 23:29:15 +0000421 //
Chris Lattner79df7c02002-03-26 18:01:55 +0000422 if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true;
Chris Lattner52f7e902001-10-13 07:03:50 +0000423
424 return false;
425}
Vikram S. Adve9466f512001-10-28 21:38:02 +0000426