Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 1 | //===- Linker.cpp - Module Linker Implementation --------------------------===// |
| 2 | // |
| 3 | // This file implements the LLVM module linker. |
| 4 | // |
| 5 | // Specifically, this: |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 6 | // * Merges global variables between the two modules |
| 7 | // * Uninit + Uninit = Init, Init + Uninit = Init, Init + Init = Error if != |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 8 | // * Merges functions between two modules |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 9 | // |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 12 | #include "llvm/Transforms/Utils/Linker.h" |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 13 | #include "llvm/Module.h" |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 14 | #include "llvm/SymbolTable.h" |
| 15 | #include "llvm/DerivedTypes.h" |
| 16 | #include "llvm/iOther.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 18 | using std::cerr; |
| 19 | using std::string; |
| 20 | using std::map; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 21 | |
| 22 | // Error - Simple wrapper function to conditionally assign to E and return true. |
| 23 | // This just makes error return conditions a little bit simpler... |
| 24 | // |
| 25 | static inline bool Error(string *E, string Message) { |
| 26 | if (E) *E = Message; |
| 27 | return true; |
| 28 | } |
| 29 | |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 30 | // LinkTypes - Go through the symbol table of the Src module and see if any |
| 31 | // types are named in the src module that are not named in the Dst module. |
| 32 | // Make sure there are no type name conflicts. |
| 33 | // |
| 34 | static bool LinkTypes(Module *Dest, const Module *Src, string *Err = 0) { |
| 35 | // No symbol table? Can't have named types. |
| 36 | if (!Src->hasSymbolTable()) return false; |
| 37 | |
| 38 | SymbolTable *DestST = Dest->getSymbolTableSure(); |
| 39 | const SymbolTable *SrcST = Src->getSymbolTable(); |
| 40 | |
| 41 | // Look for a type plane for Type's... |
| 42 | SymbolTable::const_iterator PI = SrcST->find(Type::TypeTy); |
| 43 | if (PI == SrcST->end()) return false; // No named types, do nothing. |
| 44 | |
| 45 | const SymbolTable::VarMap &VM = PI->second; |
| 46 | for (SymbolTable::type_const_iterator I = VM.begin(), E = VM.end(); |
| 47 | I != E; ++I) { |
| 48 | const string &Name = I->first; |
| 49 | const Type *RHS = cast<Type>(I->second); |
| 50 | |
| 51 | // Check to see if this type name is already in the dest module... |
| 52 | const Type *Entry = cast_or_null<Type>(DestST->lookup(Type::TypeTy, Name)); |
| 53 | if (Entry) { // Yup, the value already exists... |
| 54 | if (Entry != RHS) // If it's the same, noop. Otherwise, error. |
| 55 | return Error(Err, "Type named '" + Name + |
| 56 | "' of different shape in modules.\n Src='" + |
Chris Lattner | 9b63801 | 2002-03-15 20:21:29 +0000 | [diff] [blame] | 57 | Entry->getDescription() + "'.\n Dst='" + |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 58 | RHS->getDescription() + "'"); |
| 59 | } else { // Type not in dest module. Add it now. |
| 60 | // TODO: FIXME WHEN TYPES AREN'T CONST |
| 61 | DestST->insert(Name, const_cast<Type*>(RHS)); |
| 62 | } |
| 63 | } |
| 64 | return false; |
| 65 | } |
| 66 | |
Chris Lattner | 2d3e8bb | 2001-11-03 03:27:29 +0000 | [diff] [blame] | 67 | static void PrintMap(const map<const Value*, Value*> &M) { |
| 68 | for (map<const Value*, Value*>::const_iterator I = M.begin(), E = M.end(); |
| 69 | I != E; ++I) { |
Chris Lattner | 87182ae | 2002-04-07 22:31:23 +0000 | [diff] [blame] | 70 | cerr << " Fr: " << (void*)I->first << " "; |
| 71 | I->first->dump(); |
| 72 | cerr << " To: " << (void*)I->second << " "; |
| 73 | I->second->dump(); |
| 74 | cerr << "\n"; |
Chris Lattner | 2d3e8bb | 2001-11-03 03:27:29 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 79 | // RemapOperand - Use LocalMap and GlobalMap to convert references from one |
| 80 | // module to another. This is somewhat sophisticated in that it can |
| 81 | // automatically handle constant references correctly as well... |
| 82 | // |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 83 | static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap, |
Chris Lattner | d149c05 | 2002-09-23 18:14:15 +0000 | [diff] [blame^] | 84 | map<const Value*, Value*> *GlobalMap = 0) { |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 85 | map<const Value*,Value*>::const_iterator I = LocalMap.find(In); |
| 86 | if (I != LocalMap.end()) return I->second; |
| 87 | |
| 88 | if (GlobalMap) { |
| 89 | I = GlobalMap->find(In); |
| 90 | if (I != GlobalMap->end()) return I->second; |
| 91 | } |
| 92 | |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 93 | // Check to see if it's a constant that we are interesting in transforming... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 94 | if (const Constant *CPV = dyn_cast<Constant>(In)) { |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 95 | if (!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV)) |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 96 | return const_cast<Constant*>(CPV); // Simple constants stay identical... |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 97 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 98 | Constant *Result = 0; |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 100 | if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 101 | const std::vector<Use> &Ops = CPA->getValues(); |
| 102 | std::vector<Constant*> Operands(Ops.size()); |
Chris Lattner | e306d94 | 2002-07-18 02:31:03 +0000 | [diff] [blame] | 103 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 104 | Operands[i] = |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 105 | cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap)); |
| 106 | Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands); |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 107 | } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 108 | const std::vector<Use> &Ops = CPS->getValues(); |
| 109 | std::vector<Constant*> Operands(Ops.size()); |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 110 | for (unsigned i = 0; i < Ops.size(); ++i) |
| 111 | Operands[i] = |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 112 | cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap)); |
| 113 | Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands); |
| 114 | } else if (isa<ConstantPointerNull>(CPV)) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 115 | Result = const_cast<Constant*>(CPV); |
| 116 | } else if (const ConstantPointerRef *CPR = |
| 117 | dyn_cast<ConstantPointerRef>(CPV)) { |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 118 | Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 119 | Result = ConstantPointerRef::get(cast<GlobalValue>(V)); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 120 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) { |
Chris Lattner | b319faf | 2002-08-20 19:35:11 +0000 | [diff] [blame] | 121 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 122 | Value *Ptr = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap); |
| 123 | std::vector<Constant*> Indices; |
| 124 | Indices.reserve(CE->getNumOperands()-1); |
| 125 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) |
| 126 | Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i), |
| 127 | LocalMap, GlobalMap))); |
| 128 | |
| 129 | Result = ConstantExpr::getGetElementPtr(cast<Constant>(Ptr), Indices); |
| 130 | } else if (CE->getNumOperands() == 1) { |
Chris Lattner | ad33348 | 2002-08-14 18:24:09 +0000 | [diff] [blame] | 131 | // Cast instruction |
| 132 | assert(CE->getOpcode() == Instruction::Cast); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 133 | Value *V = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap); |
Chris Lattner | ad33348 | 2002-08-14 18:24:09 +0000 | [diff] [blame] | 134 | Result = ConstantExpr::getCast(cast<Constant>(V), CE->getType()); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 135 | } else if (CE->getNumOperands() == 2) { |
| 136 | // Binary operator... |
| 137 | Value *V1 = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap); |
| 138 | Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap); |
| 139 | |
| 140 | Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V1), |
Chris Lattner | e8e4605 | 2002-07-30 18:54:22 +0000 | [diff] [blame] | 141 | cast<Constant>(V2)); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 142 | } else { |
Chris Lattner | b319faf | 2002-08-20 19:35:11 +0000 | [diff] [blame] | 143 | assert(0 && "Unknown constant expr type!"); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 146 | } else { |
| 147 | assert(0 && "Unknown type of derived type constant value!"); |
| 148 | } |
| 149 | |
| 150 | // Cache the mapping in our local map structure... |
Chris Lattner | d149c05 | 2002-09-23 18:14:15 +0000 | [diff] [blame^] | 151 | if (GlobalMap) |
| 152 | GlobalMap->insert(std::make_pair(In, Result)); |
| 153 | else |
| 154 | LocalMap.insert(std::make_pair(In, Result)); |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 155 | return Result; |
| 156 | } |
Chris Lattner | 2d3e8bb | 2001-11-03 03:27:29 +0000 | [diff] [blame] | 157 | |
| 158 | cerr << "XXX LocalMap: \n"; |
| 159 | PrintMap(LocalMap); |
| 160 | |
| 161 | if (GlobalMap) { |
| 162 | cerr << "XXX GlobalMap: \n"; |
| 163 | PrintMap(*GlobalMap); |
| 164 | } |
| 165 | |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 166 | cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n"; |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 167 | assert(0 && "Couldn't remap value!"); |
| 168 | return 0; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | |
| 172 | // LinkGlobals - Loop through the global variables in the src module and merge |
| 173 | // them into the dest module... |
| 174 | // |
| 175 | static bool LinkGlobals(Module *Dest, const Module *Src, |
| 176 | map<const Value*, Value*> &ValueMap, string *Err = 0) { |
| 177 | // We will need a module level symbol table if the src module has a module |
| 178 | // level symbol table... |
| 179 | SymbolTable *ST = Src->getSymbolTable() ? Dest->getSymbolTableSure() : 0; |
| 180 | |
| 181 | // Loop over all of the globals in the src module, mapping them over as we go |
| 182 | // |
| 183 | for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){ |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 184 | const GlobalVariable *SGV = I; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 185 | Value *V; |
| 186 | |
| 187 | // If the global variable has a name, and that name is already in use in the |
| 188 | // Dest module, make sure that the name is a compatible global variable... |
| 189 | // |
Chris Lattner | e4d25aa | 2001-11-26 18:59:18 +0000 | [diff] [blame] | 190 | if (SGV->hasExternalLinkage() && SGV->hasName() && |
| 191 | (V = ST->lookup(SGV->getType(), SGV->getName())) && |
| 192 | cast<GlobalVariable>(V)->hasExternalLinkage()) { |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 193 | // The same named thing is a global variable, because the only two things |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 194 | // that may be in a module level symbol table are Global Vars and |
| 195 | // Functions, and they both have distinct, nonoverlapping, possible types. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 196 | // |
| 197 | GlobalVariable *DGV = cast<GlobalVariable>(V); |
| 198 | |
| 199 | // Check to see if the two GV's have the same Const'ness... |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 200 | if (SGV->isConstant() != DGV->isConstant()) |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 201 | return Error(Err, "Global Variable Collision on '" + |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 202 | SGV->getType()->getDescription() + "':%" + SGV->getName() + |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 203 | " - Global variables differ in const'ness"); |
| 204 | |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 205 | // Okay, everything is cool, remember the mapping... |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 206 | ValueMap.insert(std::make_pair(SGV, DGV)); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 207 | } else { |
| 208 | // No linking to be performed, simply create an identical version of the |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 209 | // symbol over in the dest module... the initializer will be filled in |
| 210 | // later by LinkGlobalInits... |
| 211 | // |
| 212 | GlobalVariable *DGV = |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 213 | new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(), |
Chris Lattner | e4d25aa | 2001-11-26 18:59:18 +0000 | [diff] [blame] | 214 | SGV->hasInternalLinkage(), 0, SGV->getName()); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 215 | |
| 216 | // Add the new global to the dest module |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 217 | Dest->getGlobalList().push_back(DGV); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 218 | |
| 219 | // Make sure to remember this mapping... |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 220 | ValueMap.insert(std::make_pair(SGV, DGV)); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 227 | // LinkGlobalInits - Update the initializers in the Dest module now that all |
| 228 | // globals that may be referenced are in Dest. |
| 229 | // |
| 230 | static bool LinkGlobalInits(Module *Dest, const Module *Src, |
| 231 | map<const Value*, Value*> &ValueMap, |
| 232 | string *Err = 0) { |
| 233 | |
| 234 | // Loop over all of the globals in the src module, mapping them over as we go |
| 235 | // |
| 236 | for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){ |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 237 | const GlobalVariable *SGV = I; |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 238 | |
| 239 | if (SGV->hasInitializer()) { // Only process initialized GV's |
| 240 | // Figure out what the initializer looks like in the dest module... |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 241 | Constant *DInit = |
| 242 | cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap)); |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 243 | |
| 244 | GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]); |
Chris Lattner | e4d25aa | 2001-11-26 18:59:18 +0000 | [diff] [blame] | 245 | if (DGV->hasInitializer() && SGV->hasExternalLinkage() && |
| 246 | DGV->hasExternalLinkage()) { |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 247 | if (DGV->getInitializer() != DInit) |
| 248 | return Error(Err, "Global Variable Collision on '" + |
| 249 | SGV->getType()->getDescription() + "':%" +SGV->getName()+ |
| 250 | " - Global variables have different initializers"); |
| 251 | } else { |
| 252 | // Copy the initializer over now... |
| 253 | DGV->setInitializer(DInit); |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | return false; |
| 258 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 259 | |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 260 | // LinkFunctionProtos - Link the functions together between the two modules, |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 261 | // without doing function bodies... this just adds external function prototypes |
| 262 | // to the Dest function... |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 263 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 264 | static bool LinkFunctionProtos(Module *Dest, const Module *Src, |
| 265 | map<const Value*, Value*> &ValueMap, |
| 266 | string *Err = 0) { |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 267 | // We will need a module level symbol table if the src module has a module |
| 268 | // level symbol table... |
| 269 | SymbolTable *ST = Src->getSymbolTable() ? Dest->getSymbolTableSure() : 0; |
| 270 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 271 | // Loop over all of the functions in the src module, mapping them over as we |
| 272 | // go |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 273 | // |
| 274 | for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 275 | const Function *SF = I; // SrcFunction |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 276 | Value *V; |
| 277 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 278 | // If the function has a name, and that name is already in use in the Dest |
| 279 | // module, make sure that the name is a compatible function... |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 280 | // |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 281 | if (SF->hasExternalLinkage() && SF->hasName() && |
| 282 | (V = ST->lookup(SF->getType(), SF->getName())) && |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 283 | cast<Function>(V)->hasExternalLinkage()) { |
| 284 | // The same named thing is a Function, because the only two things |
| 285 | // that may be in a module level symbol table are Global Vars and |
| 286 | // Functions, and they both have distinct, nonoverlapping, possible types. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 287 | // |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 288 | Function *DF = cast<Function>(V); // DestFunction |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 289 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 290 | // Check to make sure the function is not defined in both modules... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 291 | if (!SF->isExternal() && !DF->isExternal()) |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 292 | return Error(Err, "Function '" + |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 293 | SF->getFunctionType()->getDescription() + "':\"" + |
| 294 | SF->getName() + "\" - Function is already defined!"); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 295 | |
| 296 | // Otherwise, just remember this mapping... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 297 | ValueMap.insert(std::make_pair(SF, DF)); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 298 | } else { |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 299 | // Function does not already exist, simply insert an external function |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 300 | // signature identical to SF into the dest module... |
| 301 | Function *DF = new Function(SF->getFunctionType(), |
| 302 | SF->hasInternalLinkage(), |
| 303 | SF->getName()); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 304 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 305 | // Add the function signature to the dest module... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 306 | Dest->getFunctionList().push_back(DF); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 307 | |
| 308 | // ... and remember this mapping... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 309 | ValueMap.insert(std::make_pair(SF, DF)); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | return false; |
| 313 | } |
| 314 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 315 | // LinkFunctionBody - Copy the source function over into the dest function and |
| 316 | // fix up references to values. At this point we know that Dest is an external |
| 317 | // function, and that Src is not. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 318 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 319 | static bool LinkFunctionBody(Function *Dest, const Function *Src, |
Chris Lattner | d149c05 | 2002-09-23 18:14:15 +0000 | [diff] [blame^] | 320 | map<const Value*, Value*> &GlobalMap, |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 321 | string *Err = 0) { |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 322 | assert(Src && Dest && Dest->isExternal() && !Src->isExternal()); |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 323 | map<const Value*, Value*> LocalMap; // Map for function local values |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 324 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 325 | // Go through and convert function arguments over... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 326 | for (Function::const_aiterator I = Src->abegin(), E = Src->aend(); |
| 327 | I != E; ++I) { |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 328 | // Create the new function argument and add to the dest function... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 329 | Argument *DFA = new Argument(I->getType(), I->getName()); |
| 330 | Dest->getArgumentList().push_back(DFA); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 331 | |
| 332 | // Add a mapping to our local map |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 333 | LocalMap.insert(std::make_pair(I, DFA)); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | // Loop over all of the basic blocks, copying the instructions over... |
| 337 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 338 | for (Function::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) { |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 339 | // Create new basic block and add to mapping and the Dest function... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 340 | BasicBlock *DBB = new BasicBlock(I->getName(), Dest); |
| 341 | LocalMap.insert(std::make_pair(I, DBB)); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 342 | |
| 343 | // Loop over all of the instructions in the src basic block, copying them |
| 344 | // over. Note that this is broken in a strict sense because the cloned |
| 345 | // instructions will still be referencing values in the Src module, not |
| 346 | // the remapped values. In our case, however, we will not get caught and |
| 347 | // so we can delay patching the values up until later... |
| 348 | // |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 349 | for (BasicBlock::const_iterator II = I->begin(), IE = I->end(); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 350 | II != IE; ++II) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 351 | Instruction *DI = II->clone(); |
| 352 | DI->setName(II->getName()); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 353 | DBB->getInstList().push_back(DI); |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 354 | LocalMap.insert(std::make_pair(II, DI)); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 358 | // At this point, all of the instructions and values of the function are now |
| 359 | // copied over. The only problem is that they are still referencing values in |
| 360 | // the Source function as operands. Loop through all of the operands of the |
| 361 | // functions and patch them up to point to the local versions... |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 362 | // |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 363 | for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB) |
| 364 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 365 | for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end(); |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 366 | OI != OE; ++OI) |
| 367 | *OI = RemapOperand(*OI, LocalMap, &GlobalMap); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 368 | |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 373 | // LinkFunctionBodies - Link in the function bodies that are defined in the |
| 374 | // source module into the DestModule. This consists basically of copying the |
| 375 | // function over and fixing up references to values. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 376 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 377 | static bool LinkFunctionBodies(Module *Dest, const Module *Src, |
| 378 | map<const Value*, Value*> &ValueMap, |
| 379 | string *Err = 0) { |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 380 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 381 | // Loop over all of the functions in the src module, mapping them over as we |
| 382 | // go |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 383 | // |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 384 | for (Module::const_iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF){ |
| 385 | if (!SF->isExternal()) { // No body if function is external |
| 386 | Function *DF = cast<Function>(ValueMap[SF]); // Destination function |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 387 | |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 388 | // DF not external SF external? |
| 389 | if (!DF->isExternal()) { |
Chris Lattner | c2d774b | 2001-10-23 20:43:42 +0000 | [diff] [blame] | 390 | if (Err) |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 391 | *Err = "Function '" + (SF->hasName() ? SF->getName() : string("")) + |
Chris Lattner | c2d774b | 2001-10-23 20:43:42 +0000 | [diff] [blame] | 392 | "' body multiply defined!"; |
| 393 | return true; |
| 394 | } |
| 395 | |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 396 | if (LinkFunctionBody(DF, SF, ValueMap, Err)) return true; |
Chris Lattner | c2d774b | 2001-10-23 20:43:42 +0000 | [diff] [blame] | 397 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 398 | } |
| 399 | return false; |
| 400 | } |
| 401 | |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 402 | |
| 403 | |
| 404 | // LinkModules - This function links two modules together, with the resulting |
| 405 | // left module modified to be the composite of the two input modules. If an |
| 406 | // error occurs, true is returned and ErrorMsg (if not null) is set to indicate |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 407 | // the problem. Upon failure, the Dest module could be in a modified state, and |
| 408 | // shouldn't be relied on to be consistent. |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 409 | // |
Chris Lattner | b1443b1 | 2002-07-24 22:40:39 +0000 | [diff] [blame] | 410 | bool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg) { |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 411 | |
| 412 | // LinkTypes - Go through the symbol table of the Src module and see if any |
| 413 | // types are named in the src module that are not named in the Dst module. |
| 414 | // Make sure there are no type name conflicts. |
| 415 | // |
| 416 | if (LinkTypes(Dest, Src, ErrorMsg)) return true; |
| 417 | |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 418 | // ValueMap - Mapping of values from what they used to be in Src, to what they |
| 419 | // are now in Dest. |
| 420 | // |
| 421 | map<const Value*, Value*> ValueMap; |
| 422 | |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 423 | // Insert all of the globals in src into the Dest module... without |
| 424 | // initializers |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 425 | if (LinkGlobals(Dest, Src, ValueMap, ErrorMsg)) return true; |
| 426 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 427 | // Link the functions together between the two modules, without doing function |
| 428 | // bodies... this just adds external function prototypes to the Dest |
| 429 | // function... We do this so that when we begin processing function bodies, |
| 430 | // all of the global values that may be referenced are available in our |
| 431 | // ValueMap. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 432 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 433 | if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg)) return true; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 434 | |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 435 | // Update the initializers in the Dest module now that all globals that may |
| 436 | // be referenced are in Dest. |
| 437 | // |
| 438 | if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true; |
| 439 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 440 | // Link in the function bodies that are defined in the source module into the |
| 441 | // DestModule. This consists basically of copying the function over and |
| 442 | // fixing up references to values. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 443 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 444 | if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true; |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 445 | |
| 446 | return false; |
| 447 | } |
Vikram S. Adve | 9466f51 | 2001-10-28 21:38:02 +0000 | [diff] [blame] | 448 | |