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