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