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