blob: 7d43047c0af51df248ef2cd1fd38d04809fe3a15 [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 Lattnere9bb2df2001-12-03 22:26:30 +000099 if (Constant *CPV = dyn_cast<Constant>(In)) {
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000100 if (!isa<DerivedType>(CPV->getType()))
101 return CPV; // Simple constants stay identical...
102
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000103 Constant *Result = 0;
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000104
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000105 if (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);
112 } else if (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 Lattner8d2de8a2001-10-15 03:12:52 +0000120 Result = CPV;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000121 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000122 Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000123 Result = ConstantPointerRef::get(cast<GlobalValue>(V));
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000124 } else {
125 assert(0 && "Unknown type of derived type constant value!");
126 }
127
128 // Cache the mapping in our local map structure...
Chris Lattner697954c2002-01-20 22:54:45 +0000129 LocalMap.insert(std::make_pair(In, CPV));
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000130 return Result;
131 }
Chris Lattner2d3e8bb2001-11-03 03:27:29 +0000132
133 cerr << "XXX LocalMap: \n";
134 PrintMap(LocalMap);
135
136 if (GlobalMap) {
137 cerr << "XXX GlobalMap: \n";
138 PrintMap(*GlobalMap);
139 }
140
Chris Lattner87182ae2002-04-07 22:31:23 +0000141 cerr << "Couldn't remap value: " << (void*)In << " ";
142 In->dump();
143 cerr << "\n";
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000144 assert(0 && "Couldn't remap value!");
145 return 0;
Chris Lattner5c377c52001-10-14 23:29:15 +0000146}
147
148
149// LinkGlobals - Loop through the global variables in the src module and merge
150// them into the dest module...
151//
152static bool LinkGlobals(Module *Dest, const Module *Src,
153 map<const Value*, Value*> &ValueMap, string *Err = 0) {
154 // We will need a module level symbol table if the src module has a module
155 // level symbol table...
156 SymbolTable *ST = Src->getSymbolTable() ? Dest->getSymbolTableSure() : 0;
157
158 // Loop over all of the globals in the src module, mapping them over as we go
159 //
160 for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000161 const GlobalVariable *SGV = *I;
Chris Lattner5c377c52001-10-14 23:29:15 +0000162 Value *V;
163
164 // If the global variable has a name, and that name is already in use in the
165 // Dest module, make sure that the name is a compatible global variable...
166 //
Chris Lattnere4d25aa2001-11-26 18:59:18 +0000167 if (SGV->hasExternalLinkage() && SGV->hasName() &&
168 (V = ST->lookup(SGV->getType(), SGV->getName())) &&
169 cast<GlobalVariable>(V)->hasExternalLinkage()) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000170 // The same named thing is a global variable, because the only two things
Chris Lattner79df7c02002-03-26 18:01:55 +0000171 // that may be in a module level symbol table are Global Vars and
172 // Functions, and they both have distinct, nonoverlapping, possible types.
Chris Lattner5c377c52001-10-14 23:29:15 +0000173 //
174 GlobalVariable *DGV = cast<GlobalVariable>(V);
175
176 // Check to see if the two GV's have the same Const'ness...
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000177 if (SGV->isConstant() != DGV->isConstant())
Chris Lattner5c377c52001-10-14 23:29:15 +0000178 return Error(Err, "Global Variable Collision on '" +
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000179 SGV->getType()->getDescription() + "':%" + SGV->getName() +
Chris Lattner5c377c52001-10-14 23:29:15 +0000180 " - Global variables differ in const'ness");
181
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000182 // Okay, everything is cool, remember the mapping...
Chris Lattner697954c2002-01-20 22:54:45 +0000183 ValueMap.insert(std::make_pair(SGV, DGV));
Chris Lattner5c377c52001-10-14 23:29:15 +0000184 } else {
185 // No linking to be performed, simply create an identical version of the
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000186 // symbol over in the dest module... the initializer will be filled in
187 // later by LinkGlobalInits...
188 //
189 GlobalVariable *DGV =
Chris Lattner7a176752001-12-04 00:03:30 +0000190 new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(),
Chris Lattnere4d25aa2001-11-26 18:59:18 +0000191 SGV->hasInternalLinkage(), 0, SGV->getName());
Chris Lattner5c377c52001-10-14 23:29:15 +0000192
193 // Add the new global to the dest module
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000194 Dest->getGlobalList().push_back(DGV);
Chris Lattner5c377c52001-10-14 23:29:15 +0000195
196 // Make sure to remember this mapping...
Chris Lattner697954c2002-01-20 22:54:45 +0000197 ValueMap.insert(std::make_pair(SGV, DGV));
Chris Lattner5c377c52001-10-14 23:29:15 +0000198 }
199 }
200 return false;
201}
202
203
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000204// LinkGlobalInits - Update the initializers in the Dest module now that all
205// globals that may be referenced are in Dest.
206//
207static bool LinkGlobalInits(Module *Dest, const Module *Src,
208 map<const Value*, Value*> &ValueMap,
209 string *Err = 0) {
210
211 // Loop over all of the globals in the src module, mapping them over as we go
212 //
213 for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
214 const GlobalVariable *SGV = *I;
215
216 if (SGV->hasInitializer()) { // Only process initialized GV's
217 // Figure out what the initializer looks like in the dest module...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000218 Constant *DInit =
219 cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap));
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000220
221 GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);
Chris Lattnere4d25aa2001-11-26 18:59:18 +0000222 if (DGV->hasInitializer() && SGV->hasExternalLinkage() &&
223 DGV->hasExternalLinkage()) {
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000224 if (DGV->getInitializer() != DInit)
225 return Error(Err, "Global Variable Collision on '" +
226 SGV->getType()->getDescription() + "':%" +SGV->getName()+
227 " - Global variables have different initializers");
228 } else {
229 // Copy the initializer over now...
230 DGV->setInitializer(DInit);
231 }
232 }
233 }
234 return false;
235}
Chris Lattner5c377c52001-10-14 23:29:15 +0000236
Chris Lattner79df7c02002-03-26 18:01:55 +0000237// LinkFunctionProtos - Link the functions together between the two modules,
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000238// without doing function bodies... this just adds external function prototypes
239// to the Dest function...
Chris Lattner5c377c52001-10-14 23:29:15 +0000240//
Chris Lattner79df7c02002-03-26 18:01:55 +0000241static bool LinkFunctionProtos(Module *Dest, const Module *Src,
242 map<const Value*, Value*> &ValueMap,
243 string *Err = 0) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000244 // We will need a module level symbol table if the src module has a module
245 // level symbol table...
246 SymbolTable *ST = Src->getSymbolTable() ? Dest->getSymbolTableSure() : 0;
247
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000248 // Loop over all of the functions in the src module, mapping them over as we
249 // go
Chris Lattner5c377c52001-10-14 23:29:15 +0000250 //
251 for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000252 const Function *SM = *I; // SrcFunction
Chris Lattner5c377c52001-10-14 23:29:15 +0000253 Value *V;
254
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000255 // If the function has a name, and that name is already in use in the Dest
256 // module, make sure that the name is a compatible function...
Chris Lattner5c377c52001-10-14 23:29:15 +0000257 //
Chris Lattnere4d25aa2001-11-26 18:59:18 +0000258 if (SM->hasExternalLinkage() && SM->hasName() &&
259 (V = ST->lookup(SM->getType(), SM->getName())) &&
Chris Lattner79df7c02002-03-26 18:01:55 +0000260 cast<Function>(V)->hasExternalLinkage()) {
261 // The same named thing is a Function, because the only two things
262 // that may be in a module level symbol table are Global Vars and
263 // Functions, and they both have distinct, nonoverlapping, possible types.
Chris Lattner5c377c52001-10-14 23:29:15 +0000264 //
Chris Lattner79df7c02002-03-26 18:01:55 +0000265 Function *DM = cast<Function>(V); // DestFunction
Chris Lattner5c377c52001-10-14 23:29:15 +0000266
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000267 // Check to make sure the function is not defined in both modules...
Chris Lattner5c377c52001-10-14 23:29:15 +0000268 if (!SM->isExternal() && !DM->isExternal())
Chris Lattner79df7c02002-03-26 18:01:55 +0000269 return Error(Err, "Function '" +
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000270 SM->getFunctionType()->getDescription() + "':\"" +
Chris Lattner79df7c02002-03-26 18:01:55 +0000271 SM->getName() + "\" - Function is already defined!");
Chris Lattner5c377c52001-10-14 23:29:15 +0000272
273 // Otherwise, just remember this mapping...
Chris Lattner697954c2002-01-20 22:54:45 +0000274 ValueMap.insert(std::make_pair(SM, DM));
Chris Lattner5c377c52001-10-14 23:29:15 +0000275 } else {
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000276 // Function does not already exist, simply insert an external function
Chris Lattner5c377c52001-10-14 23:29:15 +0000277 // signature identical to SM into the dest module...
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000278 Function *DM = new Function(SM->getFunctionType(),
279 SM->hasInternalLinkage(),
Chris Lattner79df7c02002-03-26 18:01:55 +0000280 SM->getName());
Chris Lattner5c377c52001-10-14 23:29:15 +0000281
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000282 // Add the function signature to the dest module...
Chris Lattner79df7c02002-03-26 18:01:55 +0000283 Dest->getFunctionList().push_back(DM);
Chris Lattner5c377c52001-10-14 23:29:15 +0000284
285 // ... and remember this mapping...
Chris Lattner697954c2002-01-20 22:54:45 +0000286 ValueMap.insert(std::make_pair(SM, DM));
Chris Lattner5c377c52001-10-14 23:29:15 +0000287 }
288 }
289 return false;
290}
291
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000292// LinkFunctionBody - Copy the source function over into the dest function and
293// fix up references to values. At this point we know that Dest is an external
294// function, and that Src is not.
Chris Lattner5c377c52001-10-14 23:29:15 +0000295//
Chris Lattner79df7c02002-03-26 18:01:55 +0000296static bool LinkFunctionBody(Function *Dest, const Function *Src,
297 const map<const Value*, Value*> &GlobalMap,
298 string *Err = 0) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000299 assert(Src && Dest && Dest->isExternal() && !Src->isExternal());
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000300 map<const Value*, Value*> LocalMap; // Map for function local values
Chris Lattner5c377c52001-10-14 23:29:15 +0000301
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000302 // Go through and convert function arguments over...
Chris Lattner79df7c02002-03-26 18:01:55 +0000303 for (Function::ArgumentListType::const_iterator
Chris Lattner5c377c52001-10-14 23:29:15 +0000304 I = Src->getArgumentList().begin(),
305 E = Src->getArgumentList().end(); I != E; ++I) {
Chris Lattner73e21422002-04-09 19:48:49 +0000306 const Argument *SMA = *I;
Chris Lattner5c377c52001-10-14 23:29:15 +0000307
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000308 // Create the new function argument and add to the dest function...
Chris Lattner73e21422002-04-09 19:48:49 +0000309 Argument *DMA = new Argument(SMA->getType(), SMA->getName());
Chris Lattner5c377c52001-10-14 23:29:15 +0000310 Dest->getArgumentList().push_back(DMA);
311
312 // Add a mapping to our local map
Chris Lattner697954c2002-01-20 22:54:45 +0000313 LocalMap.insert(std::make_pair(SMA, DMA));
Chris Lattner5c377c52001-10-14 23:29:15 +0000314 }
315
316 // Loop over all of the basic blocks, copying the instructions over...
317 //
Chris Lattner79df7c02002-03-26 18:01:55 +0000318 for (Function::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000319 const BasicBlock *SBB = *I;
320
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000321 // Create new basic block and add to mapping and the Dest function...
Chris Lattner5c377c52001-10-14 23:29:15 +0000322 BasicBlock *DBB = new BasicBlock(SBB->getName(), Dest);
Chris Lattner697954c2002-01-20 22:54:45 +0000323 LocalMap.insert(std::make_pair(SBB, DBB));
Chris Lattner5c377c52001-10-14 23:29:15 +0000324
325 // Loop over all of the instructions in the src basic block, copying them
326 // over. Note that this is broken in a strict sense because the cloned
327 // instructions will still be referencing values in the Src module, not
328 // the remapped values. In our case, however, we will not get caught and
329 // so we can delay patching the values up until later...
330 //
331 for (BasicBlock::const_iterator II = SBB->begin(), IE = SBB->end();
332 II != IE; ++II) {
333 const Instruction *SI = *II;
334 Instruction *DI = SI->clone();
Chris Lattner43a6f2e2001-10-29 16:55:41 +0000335 DI->setName(SI->getName());
Chris Lattner5c377c52001-10-14 23:29:15 +0000336 DBB->getInstList().push_back(DI);
Chris Lattner697954c2002-01-20 22:54:45 +0000337 LocalMap.insert(std::make_pair(SI, DI));
Chris Lattner5c377c52001-10-14 23:29:15 +0000338 }
339 }
340
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000341 // At this point, all of the instructions and values of the function are now
342 // copied over. The only problem is that they are still referencing values in
343 // the Source function as operands. Loop through all of the operands of the
344 // functions and patch them up to point to the local versions...
Chris Lattner5c377c52001-10-14 23:29:15 +0000345 //
Chris Lattner79df7c02002-03-26 18:01:55 +0000346 for (Function::iterator BI = Dest->begin(), BE = Dest->end();
Chris Lattner221d6882002-02-12 21:07:25 +0000347 BI != BE; ++BI) {
348 BasicBlock *BB = *BI;
349 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
350 Instruction *Inst = *I;
351
352 for (Instruction::op_iterator OI = Inst->op_begin(), OE = Inst->op_end();
353 OI != OE; ++OI)
354 *OI = RemapOperand(*OI, LocalMap, &GlobalMap);
355 }
Chris Lattner5c377c52001-10-14 23:29:15 +0000356 }
357
358 return false;
359}
360
361
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000362// LinkFunctionBodies - Link in the function bodies that are defined in the
363// source module into the DestModule. This consists basically of copying the
364// function over and fixing up references to values.
Chris Lattner5c377c52001-10-14 23:29:15 +0000365//
Chris Lattner79df7c02002-03-26 18:01:55 +0000366static bool LinkFunctionBodies(Module *Dest, const Module *Src,
367 map<const Value*, Value*> &ValueMap,
368 string *Err = 0) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000369
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000370 // Loop over all of the functions in the src module, mapping them over as we
371 // go
Chris Lattner5c377c52001-10-14 23:29:15 +0000372 //
373 for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000374 const Function *SM = *I; // Source Function
375 if (!SM->isExternal()) { // No body if function is external
376 Function *DM = cast<Function>(ValueMap[SM]); // Destination function
Chris Lattner5c377c52001-10-14 23:29:15 +0000377
Chris Lattnerc2d774b2001-10-23 20:43:42 +0000378 // DM not external SM external?
379 if (!DM->isExternal()) {
380 if (Err)
Chris Lattner79df7c02002-03-26 18:01:55 +0000381 *Err = "Function '" + (SM->hasName() ? SM->getName() : string("")) +
Chris Lattnerc2d774b2001-10-23 20:43:42 +0000382 "' body multiply defined!";
383 return true;
384 }
385
Chris Lattner79df7c02002-03-26 18:01:55 +0000386 if (LinkFunctionBody(DM, SM, ValueMap, Err)) return true;
Chris Lattnerc2d774b2001-10-23 20:43:42 +0000387 }
Chris Lattner5c377c52001-10-14 23:29:15 +0000388 }
389 return false;
390}
391
Chris Lattner52f7e902001-10-13 07:03:50 +0000392
393
394// LinkModules - This function links two modules together, with the resulting
395// left module modified to be the composite of the two input modules. If an
396// error occurs, true is returned and ErrorMsg (if not null) is set to indicate
Chris Lattner5c377c52001-10-14 23:29:15 +0000397// the problem. Upon failure, the Dest module could be in a modified state, and
398// shouldn't be relied on to be consistent.
Chris Lattner52f7e902001-10-13 07:03:50 +0000399//
400bool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg = 0) {
Chris Lattner2c236f32001-11-03 05:18:24 +0000401
402 // LinkTypes - Go through the symbol table of the Src module and see if any
403 // types are named in the src module that are not named in the Dst module.
404 // Make sure there are no type name conflicts.
405 //
406 if (LinkTypes(Dest, Src, ErrorMsg)) return true;
407
Chris Lattner5c377c52001-10-14 23:29:15 +0000408 // ValueMap - Mapping of values from what they used to be in Src, to what they
409 // are now in Dest.
410 //
411 map<const Value*, Value*> ValueMap;
412
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000413 // Insert all of the globals in src into the Dest module... without
414 // initializers
Chris Lattner5c377c52001-10-14 23:29:15 +0000415 if (LinkGlobals(Dest, Src, ValueMap, ErrorMsg)) return true;
416
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000417 // Update the initializers in the Dest module now that all globals that may
418 // be referenced are in Dest.
419 //
420 if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
421
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000422 // Link the functions together between the two modules, without doing function
423 // bodies... this just adds external function prototypes to the Dest
424 // function... We do this so that when we begin processing function bodies,
425 // all of the global values that may be referenced are available in our
426 // ValueMap.
Chris Lattner5c377c52001-10-14 23:29:15 +0000427 //
Chris Lattner79df7c02002-03-26 18:01:55 +0000428 if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg)) return true;
Chris Lattner5c377c52001-10-14 23:29:15 +0000429
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000430 // Link in the function bodies that are defined in the source module into the
431 // DestModule. This consists basically of copying the function over and
432 // fixing up references to values.
Chris Lattner5c377c52001-10-14 23:29:15 +0000433 //
Chris Lattner79df7c02002-03-26 18:01:55 +0000434 if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true;
Chris Lattner52f7e902001-10-13 07:03:50 +0000435
436 return false;
437}
Vikram S. Adve9466f512001-10-28 21:38:02 +0000438