blob: 47aa5484e76ad7e9f691b681ccf8ebed304bbb3b [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 Lattner5c377c52001-10-14 23:29:15 +000018
19// Error - Simple wrapper function to conditionally assign to E and return true.
20// This just makes error return conditions a little bit simpler...
21//
Chris Lattner5c2d3352003-01-30 19:53:34 +000022static inline bool Error(std::string *E, std::string Message) {
Chris Lattner5c377c52001-10-14 23:29:15 +000023 if (E) *E = Message;
24 return true;
25}
26
Chris Lattner2c236f32001-11-03 05:18:24 +000027// LinkTypes - Go through the symbol table of the Src module and see if any
28// types are named in the src module that are not named in the Dst module.
29// Make sure there are no type name conflicts.
30//
Chris Lattner5c2d3352003-01-30 19:53:34 +000031static bool LinkTypes(Module *Dest, const Module *Src, std::string *Err) {
Chris Lattner6e6026b2002-11-20 18:36:02 +000032 SymbolTable *DestST = &Dest->getSymbolTable();
33 const SymbolTable *SrcST = &Src->getSymbolTable();
Chris Lattner2c236f32001-11-03 05:18:24 +000034
35 // Look for a type plane for Type's...
36 SymbolTable::const_iterator PI = SrcST->find(Type::TypeTy);
37 if (PI == SrcST->end()) return false; // No named types, do nothing.
38
39 const SymbolTable::VarMap &VM = PI->second;
40 for (SymbolTable::type_const_iterator I = VM.begin(), E = VM.end();
41 I != E; ++I) {
Chris Lattner5c2d3352003-01-30 19:53:34 +000042 const std::string &Name = I->first;
Chris Lattner2c236f32001-11-03 05:18:24 +000043 const Type *RHS = cast<Type>(I->second);
44
45 // Check to see if this type name is already in the dest module...
46 const Type *Entry = cast_or_null<Type>(DestST->lookup(Type::TypeTy, Name));
Chris Lattner5c2d3352003-01-30 19:53:34 +000047 if (Entry && !isa<OpaqueType>(Entry)) { // Yup, the value already exists...
Chris Lattner2f6bb2b2003-01-30 20:53:43 +000048 if (Entry != RHS) {
49 if (OpaqueType *OT = dyn_cast<OpaqueType>(const_cast<Type*>(RHS))) {
50 OT->refineAbstractTypeTo(Entry);
51 } else {
52 // If it's the same, noop. Otherwise, error.
53 return Error(Err, "Type named '" + Name +
54 "' of different shape in modules.\n Src='" +
55 Entry->getDescription() + "'.\n Dst='" +
56 RHS->getDescription() + "'");
57 }
58 }
Chris Lattner2c236f32001-11-03 05:18:24 +000059 } else { // Type not in dest module. Add it now.
Chris Lattner2f6bb2b2003-01-30 20:53:43 +000060 if (Entry) {
61 OpaqueType *OT = cast<OpaqueType>(const_cast<Type*>(Entry));
62 OT->refineAbstractTypeTo(RHS);
63 }
64
Chris Lattner2c236f32001-11-03 05:18:24 +000065 // TODO: FIXME WHEN TYPES AREN'T CONST
66 DestST->insert(Name, const_cast<Type*>(RHS));
67 }
68 }
69 return false;
70}
71
Chris Lattner5c2d3352003-01-30 19:53:34 +000072static void PrintMap(const std::map<const Value*, Value*> &M) {
73 for (std::map<const Value*, Value*>::const_iterator I = M.begin(), E =M.end();
Chris Lattner2d3e8bb2001-11-03 03:27:29 +000074 I != E; ++I) {
Chris Lattner5c2d3352003-01-30 19:53:34 +000075 std::cerr << " Fr: " << (void*)I->first << " ";
Chris Lattner87182ae2002-04-07 22:31:23 +000076 I->first->dump();
Chris Lattner5c2d3352003-01-30 19:53:34 +000077 std::cerr << " To: " << (void*)I->second << " ";
Chris Lattner87182ae2002-04-07 22:31:23 +000078 I->second->dump();
Chris Lattner5c2d3352003-01-30 19:53:34 +000079 std::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 Lattner5c2d3352003-01-30 19:53:34 +000088static Value *RemapOperand(const Value *In,
89 std::map<const Value*, Value*> &LocalMap,
90 std::map<const Value*, Value*> *GlobalMap) {
91 std::map<const Value*,Value*>::const_iterator I = LocalMap.find(In);
Chris Lattner5c377c52001-10-14 23:29:15 +000092 if (I != LocalMap.end()) return I->second;
93
94 if (GlobalMap) {
95 I = GlobalMap->find(In);
96 if (I != GlobalMap->end()) return I->second;
97 }
98
Chris Lattner8d2de8a2001-10-15 03:12:52 +000099 // Check to see if it's a constant that we are interesting in transforming...
Chris Lattner18961502002-06-25 16:12:52 +0000100 if (const Constant *CPV = dyn_cast<Constant>(In)) {
Chris Lattner6cdf1972002-07-18 00:13:08 +0000101 if (!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV))
Chris Lattner18961502002-06-25 16:12:52 +0000102 return const_cast<Constant*>(CPV); // Simple constants stay identical...
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000103
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000104 Constant *Result = 0;
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000105
Chris Lattner18961502002-06-25 16:12:52 +0000106 if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000107 const std::vector<Use> &Ops = CPA->getValues();
108 std::vector<Constant*> Operands(Ops.size());
Chris Lattnere306d942002-07-18 02:31:03 +0000109 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000110 Operands[i] =
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000111 cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
112 Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands);
Chris Lattner18961502002-06-25 16:12:52 +0000113 } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000114 const std::vector<Use> &Ops = CPS->getValues();
115 std::vector<Constant*> Operands(Ops.size());
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000116 for (unsigned i = 0; i < Ops.size(); ++i)
117 Operands[i] =
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000118 cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
119 Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands);
120 } else if (isa<ConstantPointerNull>(CPV)) {
Chris Lattner18961502002-06-25 16:12:52 +0000121 Result = const_cast<Constant*>(CPV);
122 } else if (const ConstantPointerRef *CPR =
123 dyn_cast<ConstantPointerRef>(CPV)) {
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000124 Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000125 Result = ConstantPointerRef::get(cast<GlobalValue>(V));
Chris Lattner6cdf1972002-07-18 00:13:08 +0000126 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
Chris Lattnerb319faf2002-08-20 19:35:11 +0000127 if (CE->getOpcode() == Instruction::GetElementPtr) {
128 Value *Ptr = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
129 std::vector<Constant*> Indices;
130 Indices.reserve(CE->getNumOperands()-1);
131 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
132 Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),
133 LocalMap, GlobalMap)));
134
135 Result = ConstantExpr::getGetElementPtr(cast<Constant>(Ptr), Indices);
136 } else if (CE->getNumOperands() == 1) {
Chris Lattnerad333482002-08-14 18:24:09 +0000137 // Cast instruction
138 assert(CE->getOpcode() == Instruction::Cast);
Chris Lattner6cdf1972002-07-18 00:13:08 +0000139 Value *V = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
Chris Lattnerad333482002-08-14 18:24:09 +0000140 Result = ConstantExpr::getCast(cast<Constant>(V), CE->getType());
Chris Lattner6cdf1972002-07-18 00:13:08 +0000141 } else if (CE->getNumOperands() == 2) {
142 // Binary operator...
143 Value *V1 = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
144 Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap);
145
146 Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V1),
Chris Lattnere8e46052002-07-30 18:54:22 +0000147 cast<Constant>(V2));
Chris Lattner6cdf1972002-07-18 00:13:08 +0000148 } else {
Chris Lattnerb319faf2002-08-20 19:35:11 +0000149 assert(0 && "Unknown constant expr type!");
Chris Lattner6cdf1972002-07-18 00:13:08 +0000150 }
151
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000152 } else {
153 assert(0 && "Unknown type of derived type constant value!");
154 }
155
156 // Cache the mapping in our local map structure...
Chris Lattnerd149c052002-09-23 18:14:15 +0000157 if (GlobalMap)
158 GlobalMap->insert(std::make_pair(In, Result));
159 else
160 LocalMap.insert(std::make_pair(In, Result));
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000161 return Result;
162 }
Chris Lattner2d3e8bb2001-11-03 03:27:29 +0000163
Chris Lattner5c2d3352003-01-30 19:53:34 +0000164 std::cerr << "XXX LocalMap: \n";
Chris Lattner2d3e8bb2001-11-03 03:27:29 +0000165 PrintMap(LocalMap);
166
167 if (GlobalMap) {
Chris Lattner5c2d3352003-01-30 19:53:34 +0000168 std::cerr << "XXX GlobalMap: \n";
Chris Lattner2d3e8bb2001-11-03 03:27:29 +0000169 PrintMap(*GlobalMap);
170 }
171
Chris Lattner5c2d3352003-01-30 19:53:34 +0000172 std::cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n";
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000173 assert(0 && "Couldn't remap value!");
174 return 0;
Chris Lattner5c377c52001-10-14 23:29:15 +0000175}
176
177
178// LinkGlobals - Loop through the global variables in the src module and merge
179// them into the dest module...
180//
181static bool LinkGlobals(Module *Dest, const Module *Src,
Chris Lattner5c2d3352003-01-30 19:53:34 +0000182 std::map<const Value*, Value*> &ValueMap,
183 std::string *Err) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000184 // We will need a module level symbol table if the src module has a module
185 // level symbol table...
Chris Lattnerb91b6572002-12-03 18:32:30 +0000186 SymbolTable *ST = (SymbolTable*)&Dest->getSymbolTable();
Chris Lattner5c377c52001-10-14 23:29:15 +0000187
188 // Loop over all of the globals in the src module, mapping them over as we go
189 //
190 for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
Chris Lattner18961502002-06-25 16:12:52 +0000191 const GlobalVariable *SGV = I;
Chris Lattner4ad02e72003-04-16 20:28:45 +0000192 GlobalVariable *DGV = 0;
193 if (SGV->hasName()) {
194 // A same named thing is a global variable, because the only two things
Chris Lattner79df7c02002-03-26 18:01:55 +0000195 // that may be in a module level symbol table are Global Vars and
196 // Functions, and they both have distinct, nonoverlapping, possible types.
Chris Lattner5c377c52001-10-14 23:29:15 +0000197 //
Chris Lattner4ad02e72003-04-16 20:28:45 +0000198 DGV = cast_or_null<GlobalVariable>(ST->lookup(SGV->getType(),
199 SGV->getName()));
200 }
Chris Lattner5c377c52001-10-14 23:29:15 +0000201
Chris Lattner4ad02e72003-04-16 20:28:45 +0000202 assert(SGV->hasInitializer() || SGV->hasExternalLinkage() &&
203 "Global must either be external or have an initializer!");
204
Chris Lattner0fec08e2003-04-21 21:07:05 +0000205 bool SGExtern = SGV->isExternal();
206 bool DGExtern = DGV ? DGV->isExternal() : false;
207
Chris Lattner4ad02e72003-04-16 20:28:45 +0000208 if (!DGV || DGV->hasInternalLinkage() || SGV->hasInternalLinkage()) {
209 // No linking to be performed, simply create an identical version of the
210 // symbol over in the dest module... the initializer will be filled in
211 // later by LinkGlobalInits...
212 //
Chris Lattner2719bac2003-04-21 21:15:04 +0000213 GlobalVariable *NewDGV =
214 new GlobalVariable(SGV->getType()->getElementType(),
215 SGV->isConstant(), SGV->getLinkage(), /*init*/0,
216 SGV->getName(), Dest);
217
218 // If the LLVM runtime renamed the global, but it is an externally visible
219 // symbol, DGV must be an existing global with internal linkage. Rename
220 // it.
221 if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage()){
222 assert(DGV && DGV->getName() == SGV->getName() &&
223 DGV->hasInternalLinkage());
224 DGV->setName("");
225 NewDGV->setName(SGV->getName()); // Force the name back
226 DGV->setName(SGV->getName()); // This will cause a renaming
227 assert(NewDGV->getName() == SGV->getName() &&
228 DGV->getName() != SGV->getName());
229 }
Chris Lattner4ad02e72003-04-16 20:28:45 +0000230
231 // Make sure to remember this mapping...
Chris Lattner2719bac2003-04-21 21:15:04 +0000232 ValueMap.insert(std::make_pair(SGV, NewDGV));
Chris Lattnerc2b97d42003-04-23 18:38:39 +0000233 } else if (SGV->isExternal()) {
234 // If SGV is external or if both SGV & DGV are external.. Just link the
235 // external globals, we aren't adding anything.
236 ValueMap.insert(std::make_pair(SGV, DGV));
237
238 } else if (DGV->isExternal()) { // If DGV is external but SGV is not...
239 ValueMap.insert(std::make_pair(SGV, DGV));
240 DGV->setLinkage(SGV->getLinkage()); // Inherit linkage!
241 } else if (SGV->getLinkage() != DGV->getLinkage()) {
Chris Lattner4ad02e72003-04-16 20:28:45 +0000242 return Error(Err, "Global variables named '" + SGV->getName() +
243 "' have different linkage specifiers!");
Chris Lattnerc2b97d42003-04-23 18:38:39 +0000244 } else if (SGV->hasExternalLinkage()) {
245 // Allow linking two exactly identical external global variables...
246 if (SGV->isConstant() != DGV->isConstant() ||
247 SGV->getInitializer() != DGV->getInitializer())
248 return Error(Err, "Global Variable Collision on '" +
249 SGV->getType()->getDescription() + " %" + SGV->getName() +
250 "' - Global variables differ in const'ness");
251 ValueMap.insert(std::make_pair(SGV, DGV));
252 } else if (SGV->hasLinkOnceLinkage()) {
Chris Lattner4ad02e72003-04-16 20:28:45 +0000253 // If the global variable has a name, and that name is already in use in
254 // the Dest module, make sure that the name is a compatible global
255 // variable...
256 //
Chris Lattner5c377c52001-10-14 23:29:15 +0000257 // Check to see if the two GV's have the same Const'ness...
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000258 if (SGV->isConstant() != DGV->isConstant())
Chris Lattner5c377c52001-10-14 23:29:15 +0000259 return Error(Err, "Global Variable Collision on '" +
Chris Lattnerc2b97d42003-04-23 18:38:39 +0000260 SGV->getType()->getDescription() + " %" + SGV->getName() +
261 "' - Global variables differ in const'ness");
Chris Lattner0fec08e2003-04-21 21:07:05 +0000262
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000263 // Okay, everything is cool, remember the mapping...
Chris Lattner697954c2002-01-20 22:54:45 +0000264 ValueMap.insert(std::make_pair(SGV, DGV));
Chris Lattnerc2b97d42003-04-23 18:38:39 +0000265 } else if (SGV->hasAppendingLinkage()) {
266 assert(0 && "FIXME: Appending linkage unimplemented!");
Chris Lattner5c377c52001-10-14 23:29:15 +0000267 } else {
Chris Lattner4ad02e72003-04-16 20:28:45 +0000268 assert(0 && "Unknown linkage!");
Chris Lattner5c377c52001-10-14 23:29:15 +0000269 }
270 }
271 return false;
272}
273
274
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000275// LinkGlobalInits - Update the initializers in the Dest module now that all
276// globals that may be referenced are in Dest.
277//
278static bool LinkGlobalInits(Module *Dest, const Module *Src,
Chris Lattner5c2d3352003-01-30 19:53:34 +0000279 std::map<const Value*, Value*> &ValueMap,
280 std::string *Err) {
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000281
282 // Loop over all of the globals in the src module, mapping them over as we go
283 //
284 for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
Chris Lattner18961502002-06-25 16:12:52 +0000285 const GlobalVariable *SGV = I;
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000286
287 if (SGV->hasInitializer()) { // Only process initialized GV's
288 // Figure out what the initializer looks like in the dest module...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000289 Constant *SInit =
Chris Lattner2f6bb2b2003-01-30 20:53:43 +0000290 cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap, 0));
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000291
292 GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000293 if (DGV->hasInitializer()) {
294 assert(SGV->getLinkage() == DGV->getLinkage());
295 if (SGV->hasExternalLinkage()) {
296 if (DGV->getInitializer() != SInit)
297 return Error(Err, "Global Variable Collision on '" +
298 SGV->getType()->getDescription() +"':%"+SGV->getName()+
299 " - Global variables have different initializers");
300 } else if (DGV->hasLinkOnceLinkage()) {
301 // Nothing is required, mapped values will take the new global
302 // automatically.
303 } else if (DGV->hasAppendingLinkage()) {
304 assert(0 && "Appending linkage unimplemented!");
305 } else {
306 assert(0 && "Unknown linkage!");
307 }
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000308 } else {
309 // Copy the initializer over now...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000310 DGV->setInitializer(SInit);
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000311 }
312 }
313 }
314 return false;
315}
Chris Lattner5c377c52001-10-14 23:29:15 +0000316
Chris Lattner79df7c02002-03-26 18:01:55 +0000317// LinkFunctionProtos - Link the functions together between the two modules,
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000318// without doing function bodies... this just adds external function prototypes
319// to the Dest function...
Chris Lattner5c377c52001-10-14 23:29:15 +0000320//
Chris Lattner79df7c02002-03-26 18:01:55 +0000321static bool LinkFunctionProtos(Module *Dest, const Module *Src,
Chris Lattner5c2d3352003-01-30 19:53:34 +0000322 std::map<const Value*, Value*> &ValueMap,
323 std::string *Err) {
Chris Lattnerb91b6572002-12-03 18:32:30 +0000324 SymbolTable *ST = (SymbolTable*)&Dest->getSymbolTable();
Chris Lattner5c377c52001-10-14 23:29:15 +0000325
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000326 // Loop over all of the functions in the src module, mapping them over as we
327 // go
Chris Lattner5c377c52001-10-14 23:29:15 +0000328 //
329 for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
Chris Lattner18961502002-06-25 16:12:52 +0000330 const Function *SF = I; // SrcFunction
Chris Lattner4ad02e72003-04-16 20:28:45 +0000331 Function *DF = 0;
332 if (SF->hasName())
Chris Lattner79df7c02002-03-26 18:01:55 +0000333 // The same named thing is a Function, because the only two things
334 // that may be in a module level symbol table are Global Vars and
335 // Functions, and they both have distinct, nonoverlapping, possible types.
Chris Lattner5c377c52001-10-14 23:29:15 +0000336 //
Chris Lattner4ad02e72003-04-16 20:28:45 +0000337 DF = cast_or_null<Function>(ST->lookup(SF->getType(), SF->getName()));
Chris Lattner5c377c52001-10-14 23:29:15 +0000338
Chris Lattner4ad02e72003-04-16 20:28:45 +0000339 if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) {
Chris Lattner0fec08e2003-04-21 21:07:05 +0000340 // Function does not already exist, simply insert an function signature
341 // identical to SF into the dest module...
Chris Lattner2719bac2003-04-21 21:15:04 +0000342 Function *NewDF = new Function(SF->getFunctionType(), SF->getLinkage(),
343 SF->getName(), Dest);
344
345 // If the LLVM runtime renamed the function, but it is an externally
346 // visible symbol, DF must be an existing function with internal linkage.
347 // Rename it.
348 if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage()) {
349 assert(DF && DF->getName() == SF->getName() &&DF->hasInternalLinkage());
350 DF->setName("");
351 NewDF->setName(SF->getName()); // Force the name back
352 DF->setName(SF->getName()); // This will cause a renaming
353 assert(NewDF->getName() == SF->getName() &&
354 DF->getName() != SF->getName());
355 }
Chris Lattner4ad02e72003-04-16 20:28:45 +0000356
357 // ... and remember this mapping...
Chris Lattner2719bac2003-04-21 21:15:04 +0000358 ValueMap.insert(std::make_pair(SF, NewDF));
Chris Lattnerc2b97d42003-04-23 18:38:39 +0000359 } else if (SF->isExternal()) {
360 // If SF is external or if both SF & DF are external.. Just link the
361 // external functions, we aren't adding anything.
362 ValueMap.insert(std::make_pair(SF, DF));
363 } else if (DF->isExternal()) { // If DF is external but SF is not...
364 // Link the external functions, update linkage qualifiers
365 ValueMap.insert(std::make_pair(SF, DF));
366 DF->setLinkage(SF->getLinkage());
367
368 } else if (SF->getLinkage() != DF->getLinkage()) {
Chris Lattner0fec08e2003-04-21 21:07:05 +0000369 return Error(Err, "Functions named '" + SF->getName() +
370 "' have different linkage specifiers!");
Chris Lattnerc2b97d42003-04-23 18:38:39 +0000371 } else if (SF->hasExternalLinkage()) {
372 // The function is defined in both modules!!
373 return Error(Err, "Function '" +
374 SF->getFunctionType()->getDescription() + "':\"" +
375 SF->getName() + "\" - Function is already defined!");
376 } else if (SF->hasLinkOnceLinkage()) {
Chris Lattner4ad02e72003-04-16 20:28:45 +0000377 // Completely ignore the source function.
Chris Lattner18961502002-06-25 16:12:52 +0000378 ValueMap.insert(std::make_pair(SF, DF));
Chris Lattnerc2b97d42003-04-23 18:38:39 +0000379 } else {
380 assert(0 && "Unknown linkage configuration found!");
Chris Lattner5c377c52001-10-14 23:29:15 +0000381 }
382 }
383 return false;
384}
385
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000386// LinkFunctionBody - Copy the source function over into the dest function and
387// fix up references to values. At this point we know that Dest is an external
388// function, and that Src is not.
Chris Lattner5c377c52001-10-14 23:29:15 +0000389//
Chris Lattner79df7c02002-03-26 18:01:55 +0000390static bool LinkFunctionBody(Function *Dest, const Function *Src,
Chris Lattner5c2d3352003-01-30 19:53:34 +0000391 std::map<const Value*, Value*> &GlobalMap,
392 std::string *Err) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000393 assert(Src && Dest && Dest->isExternal() && !Src->isExternal());
Chris Lattner5c2d3352003-01-30 19:53:34 +0000394 std::map<const Value*, Value*> LocalMap; // Map for function local values
Chris Lattner5c377c52001-10-14 23:29:15 +0000395
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000396 // Go through and convert function arguments over...
Chris Lattner69da5cf2002-10-13 20:57:00 +0000397 Function::aiterator DI = Dest->abegin();
Chris Lattner18961502002-06-25 16:12:52 +0000398 for (Function::const_aiterator I = Src->abegin(), E = Src->aend();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000399 I != E; ++I, ++DI) {
400 DI->setName(I->getName()); // Copy the name information over...
Chris Lattner5c377c52001-10-14 23:29:15 +0000401
402 // Add a mapping to our local map
Chris Lattner69da5cf2002-10-13 20:57:00 +0000403 LocalMap.insert(std::make_pair(I, DI));
Chris Lattner5c377c52001-10-14 23:29:15 +0000404 }
405
406 // Loop over all of the basic blocks, copying the instructions over...
407 //
Chris Lattner79df7c02002-03-26 18:01:55 +0000408 for (Function::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000409 // Create new basic block and add to mapping and the Dest function...
Chris Lattner18961502002-06-25 16:12:52 +0000410 BasicBlock *DBB = new BasicBlock(I->getName(), Dest);
411 LocalMap.insert(std::make_pair(I, DBB));
Chris Lattner5c377c52001-10-14 23:29:15 +0000412
413 // Loop over all of the instructions in the src basic block, copying them
414 // over. Note that this is broken in a strict sense because the cloned
415 // instructions will still be referencing values in the Src module, not
416 // the remapped values. In our case, however, we will not get caught and
417 // so we can delay patching the values up until later...
418 //
Chris Lattner18961502002-06-25 16:12:52 +0000419 for (BasicBlock::const_iterator II = I->begin(), IE = I->end();
Chris Lattner5c377c52001-10-14 23:29:15 +0000420 II != IE; ++II) {
Chris Lattner18961502002-06-25 16:12:52 +0000421 Instruction *DI = II->clone();
422 DI->setName(II->getName());
Chris Lattner5c377c52001-10-14 23:29:15 +0000423 DBB->getInstList().push_back(DI);
Chris Lattner18961502002-06-25 16:12:52 +0000424 LocalMap.insert(std::make_pair(II, DI));
Chris Lattner5c377c52001-10-14 23:29:15 +0000425 }
426 }
427
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000428 // At this point, all of the instructions and values of the function are now
429 // copied over. The only problem is that they are still referencing values in
430 // the Source function as operands. Loop through all of the operands of the
431 // functions and patch them up to point to the local versions...
Chris Lattner5c377c52001-10-14 23:29:15 +0000432 //
Chris Lattner18961502002-06-25 16:12:52 +0000433 for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB)
434 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
435 for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
Chris Lattner221d6882002-02-12 21:07:25 +0000436 OI != OE; ++OI)
437 *OI = RemapOperand(*OI, LocalMap, &GlobalMap);
Chris Lattner5c377c52001-10-14 23:29:15 +0000438
439 return false;
440}
441
442
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000443// LinkFunctionBodies - Link in the function bodies that are defined in the
444// source module into the DestModule. This consists basically of copying the
445// function over and fixing up references to values.
Chris Lattner5c377c52001-10-14 23:29:15 +0000446//
Chris Lattner79df7c02002-03-26 18:01:55 +0000447static bool LinkFunctionBodies(Module *Dest, const Module *Src,
Chris Lattner5c2d3352003-01-30 19:53:34 +0000448 std::map<const Value*, Value*> &ValueMap,
449 std::string *Err) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000450
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000451 // Loop over all of the functions in the src module, mapping them over as we
452 // go
Chris Lattner5c377c52001-10-14 23:29:15 +0000453 //
Chris Lattner18961502002-06-25 16:12:52 +0000454 for (Module::const_iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF){
455 if (!SF->isExternal()) { // No body if function is external
456 Function *DF = cast<Function>(ValueMap[SF]); // Destination function
Chris Lattner5c377c52001-10-14 23:29:15 +0000457
Chris Lattner18961502002-06-25 16:12:52 +0000458 // DF not external SF external?
459 if (!DF->isExternal()) {
Chris Lattner4ad02e72003-04-16 20:28:45 +0000460 if (DF->hasLinkOnceLinkage()) continue; // No relinkage for link-once!
Chris Lattnerc2d774b2001-10-23 20:43:42 +0000461 if (Err)
Chris Lattner5c2d3352003-01-30 19:53:34 +0000462 *Err = "Function '" + (SF->hasName() ? SF->getName() :std::string(""))
463 + "' body multiply defined!";
Chris Lattnerc2d774b2001-10-23 20:43:42 +0000464 return true;
465 }
466
Chris Lattner18961502002-06-25 16:12:52 +0000467 if (LinkFunctionBody(DF, SF, ValueMap, Err)) return true;
Chris Lattnerc2d774b2001-10-23 20:43:42 +0000468 }
Chris Lattner5c377c52001-10-14 23:29:15 +0000469 }
470 return false;
471}
472
Chris Lattner52f7e902001-10-13 07:03:50 +0000473
474
475// LinkModules - This function links two modules together, with the resulting
476// left module modified to be the composite of the two input modules. If an
477// error occurs, true is returned and ErrorMsg (if not null) is set to indicate
Chris Lattner5c377c52001-10-14 23:29:15 +0000478// the problem. Upon failure, the Dest module could be in a modified state, and
479// shouldn't be relied on to be consistent.
Chris Lattner52f7e902001-10-13 07:03:50 +0000480//
Chris Lattner5c2d3352003-01-30 19:53:34 +0000481bool LinkModules(Module *Dest, const Module *Src, std::string *ErrorMsg) {
Chris Lattner43a99942003-04-22 19:13:20 +0000482 if (Dest->getEndianness() != Src->getEndianness())
483 std::cerr << "WARNING: Linking two modules of different endianness!\n";
484 if (Dest->getPointerSize() != Src->getPointerSize())
485 std::cerr << "WARNING: Linking two modules of different pointer size!\n";
Chris Lattner2c236f32001-11-03 05:18:24 +0000486
487 // LinkTypes - Go through the symbol table of the Src module and see if any
488 // types are named in the src module that are not named in the Dst module.
489 // Make sure there are no type name conflicts.
490 //
491 if (LinkTypes(Dest, Src, ErrorMsg)) return true;
492
Chris Lattner5c377c52001-10-14 23:29:15 +0000493 // ValueMap - Mapping of values from what they used to be in Src, to what they
494 // are now in Dest.
495 //
Chris Lattner5c2d3352003-01-30 19:53:34 +0000496 std::map<const Value*, Value*> ValueMap;
Chris Lattner5c377c52001-10-14 23:29:15 +0000497
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000498 // Insert all of the globals in src into the Dest module... without
499 // initializers
Chris Lattner5c377c52001-10-14 23:29:15 +0000500 if (LinkGlobals(Dest, Src, ValueMap, ErrorMsg)) return true;
501
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000502 // Link the functions together between the two modules, without doing function
503 // bodies... this just adds external function prototypes to the Dest
504 // function... We do this so that when we begin processing function bodies,
505 // all of the global values that may be referenced are available in our
506 // ValueMap.
Chris Lattner5c377c52001-10-14 23:29:15 +0000507 //
Chris Lattner79df7c02002-03-26 18:01:55 +0000508 if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg)) return true;
Chris Lattner5c377c52001-10-14 23:29:15 +0000509
Chris Lattner6cdf1972002-07-18 00:13:08 +0000510 // Update the initializers in the Dest module now that all globals that may
511 // be referenced are in Dest.
512 //
513 if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
514
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000515 // Link in the function bodies that are defined in the source module into the
516 // DestModule. This consists basically of copying the function over and
517 // fixing up references to values.
Chris Lattner5c377c52001-10-14 23:29:15 +0000518 //
Chris Lattner79df7c02002-03-26 18:01:55 +0000519 if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true;
Chris Lattner52f7e902001-10-13 07:03:50 +0000520
521 return false;
522}
Vikram S. Adve9466f512001-10-28 21:38:02 +0000523