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 | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 22 | static inline bool Error(std::string *E, const 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 | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 27 | // ResolveTypes - Attempt to link the two specified types together. Return true |
| 28 | // if there is an error and they cannot yet be linked. |
| 29 | // |
| 30 | static bool ResolveTypes(Type *DestTy, Type *SrcTy, SymbolTable *DestST, |
| 31 | const std::string &Name) { |
| 32 | // Does the type already exist in the module? |
| 33 | if (DestTy && !isa<OpaqueType>(DestTy)) { // Yup, the type already exists... |
| 34 | if (DestTy == SrcTy) return false; // If already equal, noop |
| 35 | if (OpaqueType *OT = dyn_cast<OpaqueType>(SrcTy)) { |
| 36 | OT->refineAbstractTypeTo(DestTy); |
| 37 | } else { |
| 38 | return true; // Cannot link types... neither is opaque and not-equal |
| 39 | } |
| 40 | } else { // Type not in dest module. Add it now. |
| 41 | if (DestTy) // Type _is_ in module, just opaque... |
| 42 | cast<OpaqueType>(DestTy)->refineAbstractTypeTo(SrcTy); |
| 43 | else |
| 44 | DestST->insert(Name, SrcTy); |
| 45 | } |
| 46 | return false; |
| 47 | } |
| 48 | |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 49 | // LinkTypes - Go through the symbol table of the Src module and see if any |
| 50 | // types are named in the src module that are not named in the Dst module. |
| 51 | // Make sure there are no type name conflicts. |
| 52 | // |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 53 | static bool LinkTypes(Module *Dest, const Module *Src, std::string *Err) { |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 54 | SymbolTable *DestST = &Dest->getSymbolTable(); |
| 55 | const SymbolTable *SrcST = &Src->getSymbolTable(); |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 56 | |
| 57 | // Look for a type plane for Type's... |
| 58 | SymbolTable::const_iterator PI = SrcST->find(Type::TypeTy); |
| 59 | if (PI == SrcST->end()) return false; // No named types, do nothing. |
| 60 | |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 61 | // Some types cannot be resolved immediately becuse they depend on other types |
| 62 | // being resolved to each other first. This contains a list of types we are |
| 63 | // waiting to recheck. |
| 64 | std::vector<std::string> DelayedTypesToResolve; |
| 65 | |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 66 | const SymbolTable::VarMap &VM = PI->second; |
| 67 | for (SymbolTable::type_const_iterator I = VM.begin(), E = VM.end(); |
| 68 | I != E; ++I) { |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 69 | const std::string &Name = I->first; |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 70 | Type *RHS = cast<Type>(I->second); |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 71 | |
| 72 | // Check to see if this type name is already in the dest module... |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 73 | Type *Entry = cast_or_null<Type>(DestST->lookup(Type::TypeTy, Name)); |
Chris Lattner | 2f6bb2b | 2003-01-30 20:53:43 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 75 | if (ResolveTypes(Entry, RHS, DestST, Name)) { |
| 76 | // They look different, save the types 'till later to resolve. |
| 77 | DelayedTypesToResolve.push_back(Name); |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 80 | |
| 81 | // Iteratively resolve types while we can... |
| 82 | while (!DelayedTypesToResolve.empty()) { |
| 83 | // Loop over all of the types, attempting to resolve them if possible... |
| 84 | unsigned OldSize = DelayedTypesToResolve.size(); |
| 85 | |
| 86 | for (unsigned i = 0; i != DelayedTypesToResolve.size(); ++i) { |
| 87 | const std::string &Name = DelayedTypesToResolve[i]; |
| 88 | Type *T1 = cast<Type>(VM.find(Name)->second); |
| 89 | Type *T2 = cast<Type>(DestST->lookup(Type::TypeTy, Name)); |
| 90 | if (!ResolveTypes(T2, T1, DestST, Name)) { |
| 91 | // We are making progress! |
| 92 | DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i); |
| 93 | --i; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Did we not eliminate any types? |
| 98 | if (DelayedTypesToResolve.size() == OldSize) { |
| 99 | // Build up an error message of all of the mismatched types. |
| 100 | std::string ErrorMessage; |
| 101 | for (unsigned i = 0, e = DelayedTypesToResolve.size(); i != e; ++i) { |
| 102 | const std::string &Name = DelayedTypesToResolve[i]; |
| 103 | const Type *T1 = cast<Type>(VM.find(Name)->second); |
| 104 | const Type *T2 = cast<Type>(DestST->lookup(Type::TypeTy, Name)); |
| 105 | ErrorMessage += " Type named '" + Name + |
| 106 | "' conflicts.\n Src='" + T1->getDescription() + |
| 107 | "'.\n Dest='" + T2->getDescription() + "'\n"; |
| 108 | } |
| 109 | return Error(Err, "Type conflict between types in modules:\n" + |
| 110 | ErrorMessage); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 115 | return false; |
| 116 | } |
| 117 | |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 118 | static void PrintMap(const std::map<const Value*, Value*> &M) { |
| 119 | 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] | 120 | I != E; ++I) { |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 121 | std::cerr << " Fr: " << (void*)I->first << " "; |
Chris Lattner | 87182ae | 2002-04-07 22:31:23 +0000 | [diff] [blame] | 122 | I->first->dump(); |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 123 | std::cerr << " To: " << (void*)I->second << " "; |
Chris Lattner | 87182ae | 2002-04-07 22:31:23 +0000 | [diff] [blame] | 124 | I->second->dump(); |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 125 | std::cerr << "\n"; |
Chris Lattner | 2d3e8bb | 2001-11-03 03:27:29 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 130 | // RemapOperand - Use LocalMap and GlobalMap to convert references from one |
| 131 | // module to another. This is somewhat sophisticated in that it can |
| 132 | // automatically handle constant references correctly as well... |
| 133 | // |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 134 | static Value *RemapOperand(const Value *In, |
| 135 | std::map<const Value*, Value*> &LocalMap, |
| 136 | std::map<const Value*, Value*> *GlobalMap) { |
| 137 | std::map<const Value*,Value*>::const_iterator I = LocalMap.find(In); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 138 | if (I != LocalMap.end()) return I->second; |
| 139 | |
| 140 | if (GlobalMap) { |
| 141 | I = GlobalMap->find(In); |
| 142 | if (I != GlobalMap->end()) return I->second; |
| 143 | } |
| 144 | |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 145 | // 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] | 146 | if (const Constant *CPV = dyn_cast<Constant>(In)) { |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 147 | if (!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV)) |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 148 | return const_cast<Constant*>(CPV); // Simple constants stay identical... |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 149 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 150 | Constant *Result = 0; |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 151 | |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 152 | if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 153 | const std::vector<Use> &Ops = CPA->getValues(); |
| 154 | std::vector<Constant*> Operands(Ops.size()); |
Chris Lattner | e306d94 | 2002-07-18 02:31:03 +0000 | [diff] [blame] | 155 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 156 | Operands[i] = |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 157 | cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap)); |
| 158 | Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands); |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 159 | } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 160 | const std::vector<Use> &Ops = CPS->getValues(); |
| 161 | std::vector<Constant*> Operands(Ops.size()); |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 162 | for (unsigned i = 0; i < Ops.size(); ++i) |
| 163 | Operands[i] = |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 164 | cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap)); |
| 165 | Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands); |
| 166 | } else if (isa<ConstantPointerNull>(CPV)) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 167 | Result = const_cast<Constant*>(CPV); |
| 168 | } else if (const ConstantPointerRef *CPR = |
| 169 | dyn_cast<ConstantPointerRef>(CPV)) { |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 170 | Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 171 | Result = ConstantPointerRef::get(cast<GlobalValue>(V)); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 172 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) { |
Chris Lattner | b319faf | 2002-08-20 19:35:11 +0000 | [diff] [blame] | 173 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 174 | Value *Ptr = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap); |
| 175 | std::vector<Constant*> Indices; |
| 176 | Indices.reserve(CE->getNumOperands()-1); |
| 177 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) |
| 178 | Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i), |
| 179 | LocalMap, GlobalMap))); |
| 180 | |
| 181 | Result = ConstantExpr::getGetElementPtr(cast<Constant>(Ptr), Indices); |
| 182 | } else if (CE->getNumOperands() == 1) { |
Chris Lattner | ad33348 | 2002-08-14 18:24:09 +0000 | [diff] [blame] | 183 | // Cast instruction |
| 184 | assert(CE->getOpcode() == Instruction::Cast); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 185 | Value *V = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap); |
Chris Lattner | ad33348 | 2002-08-14 18:24:09 +0000 | [diff] [blame] | 186 | Result = ConstantExpr::getCast(cast<Constant>(V), CE->getType()); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 187 | } else if (CE->getNumOperands() == 2) { |
| 188 | // Binary operator... |
| 189 | Value *V1 = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap); |
| 190 | Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap); |
| 191 | |
| 192 | Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V1), |
Chris Lattner | e8e4605 | 2002-07-30 18:54:22 +0000 | [diff] [blame] | 193 | cast<Constant>(V2)); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 194 | } else { |
Chris Lattner | b319faf | 2002-08-20 19:35:11 +0000 | [diff] [blame] | 195 | assert(0 && "Unknown constant expr type!"); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 198 | } else { |
| 199 | assert(0 && "Unknown type of derived type constant value!"); |
| 200 | } |
| 201 | |
| 202 | // Cache the mapping in our local map structure... |
Chris Lattner | d149c05 | 2002-09-23 18:14:15 +0000 | [diff] [blame] | 203 | if (GlobalMap) |
| 204 | GlobalMap->insert(std::make_pair(In, Result)); |
| 205 | else |
| 206 | LocalMap.insert(std::make_pair(In, Result)); |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 207 | return Result; |
| 208 | } |
Chris Lattner | 2d3e8bb | 2001-11-03 03:27:29 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 210 | std::cerr << "XXX LocalMap: \n"; |
Chris Lattner | 2d3e8bb | 2001-11-03 03:27:29 +0000 | [diff] [blame] | 211 | PrintMap(LocalMap); |
| 212 | |
| 213 | if (GlobalMap) { |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 214 | std::cerr << "XXX GlobalMap: \n"; |
Chris Lattner | 2d3e8bb | 2001-11-03 03:27:29 +0000 | [diff] [blame] | 215 | PrintMap(*GlobalMap); |
| 216 | } |
| 217 | |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 218 | std::cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n"; |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 219 | assert(0 && "Couldn't remap value!"); |
| 220 | return 0; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | |
| 224 | // LinkGlobals - Loop through the global variables in the src module and merge |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 225 | // them into the dest module. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 226 | // |
| 227 | static bool LinkGlobals(Module *Dest, const Module *Src, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 228 | std::map<const Value*, Value*> &ValueMap, |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 229 | std::multimap<std::string, GlobalVariable *> &AppendingVars, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 230 | std::string *Err) { |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 231 | // We will need a module level symbol table if the src module has a module |
| 232 | // level symbol table... |
Chris Lattner | b91b657 | 2002-12-03 18:32:30 +0000 | [diff] [blame] | 233 | SymbolTable *ST = (SymbolTable*)&Dest->getSymbolTable(); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 234 | |
| 235 | // Loop over all of the globals in the src module, mapping them over as we go |
| 236 | // |
| 237 | 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] | 238 | const GlobalVariable *SGV = I; |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 239 | GlobalVariable *DGV = 0; |
| 240 | if (SGV->hasName()) { |
| 241 | // A same named thing is a global variable, because the only two things |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 242 | // that may be in a module level symbol table are Global Vars and |
| 243 | // Functions, and they both have distinct, nonoverlapping, possible types. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 244 | // |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 245 | DGV = cast_or_null<GlobalVariable>(ST->lookup(SGV->getType(), |
| 246 | SGV->getName())); |
| 247 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 248 | |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 249 | assert(SGV->hasInitializer() || SGV->hasExternalLinkage() && |
| 250 | "Global must either be external or have an initializer!"); |
| 251 | |
Chris Lattner | 0fec08e | 2003-04-21 21:07:05 +0000 | [diff] [blame] | 252 | bool SGExtern = SGV->isExternal(); |
| 253 | bool DGExtern = DGV ? DGV->isExternal() : false; |
| 254 | |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 255 | if (!DGV || DGV->hasInternalLinkage() || SGV->hasInternalLinkage()) { |
| 256 | // No linking to be performed, simply create an identical version of the |
| 257 | // symbol over in the dest module... the initializer will be filled in |
| 258 | // later by LinkGlobalInits... |
| 259 | // |
Chris Lattner | 2719bac | 2003-04-21 21:15:04 +0000 | [diff] [blame] | 260 | GlobalVariable *NewDGV = |
| 261 | new GlobalVariable(SGV->getType()->getElementType(), |
| 262 | SGV->isConstant(), SGV->getLinkage(), /*init*/0, |
| 263 | SGV->getName(), Dest); |
| 264 | |
| 265 | // If the LLVM runtime renamed the global, but it is an externally visible |
| 266 | // symbol, DGV must be an existing global with internal linkage. Rename |
| 267 | // it. |
| 268 | if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage()){ |
| 269 | assert(DGV && DGV->getName() == SGV->getName() && |
| 270 | DGV->hasInternalLinkage()); |
| 271 | DGV->setName(""); |
| 272 | NewDGV->setName(SGV->getName()); // Force the name back |
| 273 | DGV->setName(SGV->getName()); // This will cause a renaming |
| 274 | assert(NewDGV->getName() == SGV->getName() && |
| 275 | DGV->getName() != SGV->getName()); |
| 276 | } |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 277 | |
| 278 | // Make sure to remember this mapping... |
Chris Lattner | 2719bac | 2003-04-21 21:15:04 +0000 | [diff] [blame] | 279 | ValueMap.insert(std::make_pair(SGV, NewDGV)); |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 280 | if (SGV->hasAppendingLinkage()) |
| 281 | // Keep track that this is an appending variable... |
| 282 | AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV)); |
| 283 | |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 284 | } else if (SGV->isExternal()) { |
| 285 | // If SGV is external or if both SGV & DGV are external.. Just link the |
| 286 | // external globals, we aren't adding anything. |
| 287 | ValueMap.insert(std::make_pair(SGV, DGV)); |
| 288 | |
| 289 | } else if (DGV->isExternal()) { // If DGV is external but SGV is not... |
| 290 | ValueMap.insert(std::make_pair(SGV, DGV)); |
| 291 | DGV->setLinkage(SGV->getLinkage()); // Inherit linkage! |
| 292 | } else if (SGV->getLinkage() != DGV->getLinkage()) { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 293 | return Error(Err, "Global variables named '" + SGV->getName() + |
| 294 | "' have different linkage specifiers!"); |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 295 | } else if (SGV->hasExternalLinkage()) { |
| 296 | // Allow linking two exactly identical external global variables... |
| 297 | if (SGV->isConstant() != DGV->isConstant() || |
| 298 | SGV->getInitializer() != DGV->getInitializer()) |
| 299 | return Error(Err, "Global Variable Collision on '" + |
| 300 | SGV->getType()->getDescription() + " %" + SGV->getName() + |
| 301 | "' - Global variables differ in const'ness"); |
| 302 | ValueMap.insert(std::make_pair(SGV, DGV)); |
| 303 | } else if (SGV->hasLinkOnceLinkage()) { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 304 | // If the global variable has a name, and that name is already in use in |
| 305 | // the Dest module, make sure that the name is a compatible global |
| 306 | // variable... |
| 307 | // |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 308 | // 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] | 309 | if (SGV->isConstant() != DGV->isConstant()) |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 310 | return Error(Err, "Global Variable Collision on '" + |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 311 | SGV->getType()->getDescription() + " %" + SGV->getName() + |
| 312 | "' - Global variables differ in const'ness"); |
Chris Lattner | 0fec08e | 2003-04-21 21:07:05 +0000 | [diff] [blame] | 313 | |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 314 | // Okay, everything is cool, remember the mapping... |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 315 | ValueMap.insert(std::make_pair(SGV, DGV)); |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 316 | } else if (SGV->hasAppendingLinkage()) { |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 317 | // No linking is performed yet. Just insert a new copy of the global, and |
| 318 | // keep track of the fact that it is an appending variable in the |
| 319 | // AppendingVars map. The name is cleared out so that no linkage is |
| 320 | // performed. |
| 321 | GlobalVariable *NewDGV = |
| 322 | new GlobalVariable(SGV->getType()->getElementType(), |
| 323 | SGV->isConstant(), SGV->getLinkage(), /*init*/0, |
| 324 | "", Dest); |
| 325 | |
| 326 | // Make sure to remember this mapping... |
| 327 | ValueMap.insert(std::make_pair(SGV, NewDGV)); |
| 328 | |
| 329 | // Keep track that this is an appending variable... |
| 330 | AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV)); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 331 | } else { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 332 | assert(0 && "Unknown linkage!"); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 339 | // LinkGlobalInits - Update the initializers in the Dest module now that all |
| 340 | // globals that may be referenced are in Dest. |
| 341 | // |
| 342 | static bool LinkGlobalInits(Module *Dest, const Module *Src, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 343 | std::map<const Value*, Value*> &ValueMap, |
| 344 | std::string *Err) { |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 345 | |
| 346 | // Loop over all of the globals in the src module, mapping them over as we go |
| 347 | // |
| 348 | 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] | 349 | const GlobalVariable *SGV = I; |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 350 | |
| 351 | if (SGV->hasInitializer()) { // Only process initialized GV's |
| 352 | // Figure out what the initializer looks like in the dest module... |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 353 | Constant *SInit = |
Chris Lattner | 2f6bb2b | 2003-01-30 20:53:43 +0000 | [diff] [blame] | 354 | cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap, 0)); |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 355 | |
| 356 | GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]); |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 357 | if (DGV->hasInitializer()) { |
| 358 | assert(SGV->getLinkage() == DGV->getLinkage()); |
| 359 | if (SGV->hasExternalLinkage()) { |
| 360 | if (DGV->getInitializer() != SInit) |
| 361 | return Error(Err, "Global Variable Collision on '" + |
| 362 | SGV->getType()->getDescription() +"':%"+SGV->getName()+ |
| 363 | " - Global variables have different initializers"); |
| 364 | } else if (DGV->hasLinkOnceLinkage()) { |
| 365 | // Nothing is required, mapped values will take the new global |
| 366 | // automatically. |
| 367 | } else if (DGV->hasAppendingLinkage()) { |
| 368 | assert(0 && "Appending linkage unimplemented!"); |
| 369 | } else { |
| 370 | assert(0 && "Unknown linkage!"); |
| 371 | } |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 372 | } else { |
| 373 | // Copy the initializer over now... |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 374 | DGV->setInitializer(SInit); |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | } |
| 378 | return false; |
| 379 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 380 | |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 381 | // LinkFunctionProtos - Link the functions together between the two modules, |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 382 | // without doing function bodies... this just adds external function prototypes |
| 383 | // to the Dest function... |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 384 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 385 | static bool LinkFunctionProtos(Module *Dest, const Module *Src, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 386 | std::map<const Value*, Value*> &ValueMap, |
| 387 | std::string *Err) { |
Chris Lattner | b91b657 | 2002-12-03 18:32:30 +0000 | [diff] [blame] | 388 | SymbolTable *ST = (SymbolTable*)&Dest->getSymbolTable(); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 389 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 390 | // Loop over all of the functions in the src module, mapping them over as we |
| 391 | // go |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 392 | // |
| 393 | 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] | 394 | const Function *SF = I; // SrcFunction |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 395 | Function *DF = 0; |
| 396 | if (SF->hasName()) |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 397 | // The same named thing is a Function, because the only two things |
| 398 | // that may be in a module level symbol table are Global Vars and |
| 399 | // Functions, and they both have distinct, nonoverlapping, possible types. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 400 | // |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 401 | DF = cast_or_null<Function>(ST->lookup(SF->getType(), SF->getName())); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 402 | |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 403 | if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) { |
Chris Lattner | 0fec08e | 2003-04-21 21:07:05 +0000 | [diff] [blame] | 404 | // Function does not already exist, simply insert an function signature |
| 405 | // identical to SF into the dest module... |
Chris Lattner | 2719bac | 2003-04-21 21:15:04 +0000 | [diff] [blame] | 406 | Function *NewDF = new Function(SF->getFunctionType(), SF->getLinkage(), |
| 407 | SF->getName(), Dest); |
| 408 | |
| 409 | // If the LLVM runtime renamed the function, but it is an externally |
| 410 | // visible symbol, DF must be an existing function with internal linkage. |
| 411 | // Rename it. |
| 412 | if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage()) { |
| 413 | assert(DF && DF->getName() == SF->getName() &&DF->hasInternalLinkage()); |
| 414 | DF->setName(""); |
| 415 | NewDF->setName(SF->getName()); // Force the name back |
| 416 | DF->setName(SF->getName()); // This will cause a renaming |
| 417 | assert(NewDF->getName() == SF->getName() && |
| 418 | DF->getName() != SF->getName()); |
| 419 | } |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 420 | |
| 421 | // ... and remember this mapping... |
Chris Lattner | 2719bac | 2003-04-21 21:15:04 +0000 | [diff] [blame] | 422 | ValueMap.insert(std::make_pair(SF, NewDF)); |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 423 | } else if (SF->isExternal()) { |
| 424 | // If SF is external or if both SF & DF are external.. Just link the |
| 425 | // external functions, we aren't adding anything. |
| 426 | ValueMap.insert(std::make_pair(SF, DF)); |
| 427 | } else if (DF->isExternal()) { // If DF is external but SF is not... |
| 428 | // Link the external functions, update linkage qualifiers |
| 429 | ValueMap.insert(std::make_pair(SF, DF)); |
| 430 | DF->setLinkage(SF->getLinkage()); |
| 431 | |
| 432 | } else if (SF->getLinkage() != DF->getLinkage()) { |
Chris Lattner | 0fec08e | 2003-04-21 21:07:05 +0000 | [diff] [blame] | 433 | return Error(Err, "Functions named '" + SF->getName() + |
| 434 | "' have different linkage specifiers!"); |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 435 | } else if (SF->hasExternalLinkage()) { |
| 436 | // The function is defined in both modules!! |
| 437 | return Error(Err, "Function '" + |
| 438 | SF->getFunctionType()->getDescription() + "':\"" + |
| 439 | SF->getName() + "\" - Function is already defined!"); |
| 440 | } else if (SF->hasLinkOnceLinkage()) { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 441 | // Completely ignore the source function. |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 442 | ValueMap.insert(std::make_pair(SF, DF)); |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 443 | } else { |
| 444 | assert(0 && "Unknown linkage configuration found!"); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | return false; |
| 448 | } |
| 449 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 450 | // LinkFunctionBody - Copy the source function over into the dest function and |
| 451 | // fix up references to values. At this point we know that Dest is an external |
| 452 | // function, and that Src is not. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 453 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 454 | static bool LinkFunctionBody(Function *Dest, const Function *Src, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 455 | std::map<const Value*, Value*> &GlobalMap, |
| 456 | std::string *Err) { |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 457 | assert(Src && Dest && Dest->isExternal() && !Src->isExternal()); |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 458 | std::map<const Value*, Value*> LocalMap; // Map for function local values |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 459 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 460 | // Go through and convert function arguments over... |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 461 | Function::aiterator DI = Dest->abegin(); |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 462 | for (Function::const_aiterator I = Src->abegin(), E = Src->aend(); |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 463 | I != E; ++I, ++DI) { |
| 464 | DI->setName(I->getName()); // Copy the name information over... |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 465 | |
| 466 | // Add a mapping to our local map |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 467 | LocalMap.insert(std::make_pair(I, DI)); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | // Loop over all of the basic blocks, copying the instructions over... |
| 471 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 472 | 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] | 473 | // Create new basic block and add to mapping and the Dest function... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 474 | BasicBlock *DBB = new BasicBlock(I->getName(), Dest); |
| 475 | LocalMap.insert(std::make_pair(I, DBB)); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 476 | |
| 477 | // Loop over all of the instructions in the src basic block, copying them |
| 478 | // over. Note that this is broken in a strict sense because the cloned |
| 479 | // instructions will still be referencing values in the Src module, not |
| 480 | // the remapped values. In our case, however, we will not get caught and |
| 481 | // so we can delay patching the values up until later... |
| 482 | // |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 483 | for (BasicBlock::const_iterator II = I->begin(), IE = I->end(); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 484 | II != IE; ++II) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 485 | Instruction *DI = II->clone(); |
| 486 | DI->setName(II->getName()); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 487 | DBB->getInstList().push_back(DI); |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 488 | LocalMap.insert(std::make_pair(II, DI)); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 492 | // At this point, all of the instructions and values of the function are now |
| 493 | // copied over. The only problem is that they are still referencing values in |
| 494 | // the Source function as operands. Loop through all of the operands of the |
| 495 | // functions and patch them up to point to the local versions... |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 496 | // |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 497 | for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB) |
| 498 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 499 | for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end(); |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 500 | OI != OE; ++OI) |
| 501 | *OI = RemapOperand(*OI, LocalMap, &GlobalMap); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 502 | |
| 503 | return false; |
| 504 | } |
| 505 | |
| 506 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 507 | // LinkFunctionBodies - Link in the function bodies that are defined in the |
| 508 | // source module into the DestModule. This consists basically of copying the |
| 509 | // function over and fixing up references to values. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 510 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 511 | static bool LinkFunctionBodies(Module *Dest, const Module *Src, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 512 | std::map<const Value*, Value*> &ValueMap, |
| 513 | std::string *Err) { |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 514 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 515 | // Loop over all of the functions in the src module, mapping them over as we |
| 516 | // go |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 517 | // |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 518 | for (Module::const_iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF){ |
| 519 | if (!SF->isExternal()) { // No body if function is external |
| 520 | Function *DF = cast<Function>(ValueMap[SF]); // Destination function |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 521 | |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 522 | // DF not external SF external? |
| 523 | if (!DF->isExternal()) { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 524 | if (DF->hasLinkOnceLinkage()) continue; // No relinkage for link-once! |
Chris Lattner | c2d774b | 2001-10-23 20:43:42 +0000 | [diff] [blame] | 525 | if (Err) |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 526 | *Err = "Function '" + (SF->hasName() ? SF->getName() :std::string("")) |
| 527 | + "' body multiply defined!"; |
Chris Lattner | c2d774b | 2001-10-23 20:43:42 +0000 | [diff] [blame] | 528 | return true; |
| 529 | } |
| 530 | |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 531 | if (LinkFunctionBody(DF, SF, ValueMap, Err)) return true; |
Chris Lattner | c2d774b | 2001-10-23 20:43:42 +0000 | [diff] [blame] | 532 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 533 | } |
| 534 | return false; |
| 535 | } |
| 536 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 537 | // LinkAppendingVars - If there were any appending global variables, link them |
| 538 | // together now. Return true on error. |
| 539 | // |
| 540 | static bool LinkAppendingVars(Module *M, |
| 541 | std::multimap<std::string, GlobalVariable *> &AppendingVars, |
| 542 | std::string *ErrorMsg) { |
| 543 | if (AppendingVars.empty()) return false; // Nothing to do. |
| 544 | |
| 545 | // Loop over the multimap of appending vars, processing any variables with the |
| 546 | // same name, forming a new appending global variable with both of the |
| 547 | // initializers merged together, then rewrite references to the old variables |
| 548 | // and delete them. |
| 549 | // |
| 550 | std::vector<Constant*> Inits; |
| 551 | while (AppendingVars.size() > 1) { |
| 552 | // Get the first two elements in the map... |
| 553 | std::multimap<std::string, |
| 554 | GlobalVariable*>::iterator Second = AppendingVars.begin(), First=Second++; |
| 555 | |
| 556 | // If the first two elements are for different names, there is no pair... |
| 557 | // Otherwise there is a pair, so link them together... |
| 558 | if (First->first == Second->first) { |
| 559 | GlobalVariable *G1 = First->second, *G2 = Second->second; |
| 560 | const ArrayType *T1 = cast<ArrayType>(G1->getType()->getElementType()); |
| 561 | const ArrayType *T2 = cast<ArrayType>(G2->getType()->getElementType()); |
| 562 | |
| 563 | // Check to see that they two arrays agree on type... |
| 564 | if (T1->getElementType() != T2->getElementType()) |
| 565 | return Error(ErrorMsg, |
| 566 | "Appending variables with different element types need to be linked!"); |
| 567 | if (G1->isConstant() != G2->isConstant()) |
| 568 | return Error(ErrorMsg, |
| 569 | "Appending variables linked with different const'ness!"); |
| 570 | |
| 571 | unsigned NewSize = T1->getNumElements() + T2->getNumElements(); |
| 572 | ArrayType *NewType = ArrayType::get(T1->getElementType(), NewSize); |
| 573 | |
| 574 | // Create the new global variable... |
| 575 | GlobalVariable *NG = |
| 576 | new GlobalVariable(NewType, G1->isConstant(), G1->getLinkage(), |
| 577 | /*init*/0, First->first, M); |
| 578 | |
| 579 | // Merge the initializer... |
| 580 | Inits.reserve(NewSize); |
| 581 | ConstantArray *I = cast<ConstantArray>(G1->getInitializer()); |
| 582 | for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i) |
| 583 | Inits.push_back(cast<Constant>(I->getValues()[i])); |
| 584 | I = cast<ConstantArray>(G2->getInitializer()); |
| 585 | for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i) |
| 586 | Inits.push_back(cast<Constant>(I->getValues()[i])); |
| 587 | NG->setInitializer(ConstantArray::get(NewType, Inits)); |
| 588 | Inits.clear(); |
| 589 | |
| 590 | // Replace any uses of the two global variables with uses of the new |
| 591 | // global... |
| 592 | |
| 593 | // FIXME: This should rewrite simple/straight-forward uses such as |
| 594 | // getelementptr instructions to not use the Cast! |
| 595 | ConstantPointerRef *NGCP = ConstantPointerRef::get(NG); |
| 596 | G1->replaceAllUsesWith(ConstantExpr::getCast(NGCP, G1->getType())); |
| 597 | G2->replaceAllUsesWith(ConstantExpr::getCast(NGCP, G2->getType())); |
| 598 | |
| 599 | // Remove the two globals from the module now... |
| 600 | M->getGlobalList().erase(G1); |
| 601 | M->getGlobalList().erase(G2); |
| 602 | |
| 603 | // Put the new global into the AppendingVars map so that we can handle |
| 604 | // linking of more than two vars... |
| 605 | Second->second = NG; |
| 606 | } |
| 607 | AppendingVars.erase(First); |
| 608 | } |
| 609 | |
| 610 | return false; |
| 611 | } |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 612 | |
| 613 | |
| 614 | // LinkModules - This function links two modules together, with the resulting |
| 615 | // left module modified to be the composite of the two input modules. If an |
| 616 | // 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] | 617 | // the problem. Upon failure, the Dest module could be in a modified state, and |
| 618 | // shouldn't be relied on to be consistent. |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 619 | // |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 620 | bool LinkModules(Module *Dest, const Module *Src, std::string *ErrorMsg) { |
Chris Lattner | 43a9994 | 2003-04-22 19:13:20 +0000 | [diff] [blame] | 621 | if (Dest->getEndianness() != Src->getEndianness()) |
| 622 | std::cerr << "WARNING: Linking two modules of different endianness!\n"; |
| 623 | if (Dest->getPointerSize() != Src->getPointerSize()) |
| 624 | std::cerr << "WARNING: Linking two modules of different pointer size!\n"; |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 625 | |
| 626 | // LinkTypes - Go through the symbol table of the Src module and see if any |
| 627 | // types are named in the src module that are not named in the Dst module. |
| 628 | // Make sure there are no type name conflicts. |
| 629 | // |
| 630 | if (LinkTypes(Dest, Src, ErrorMsg)) return true; |
| 631 | |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 632 | // ValueMap - Mapping of values from what they used to be in Src, to what they |
| 633 | // are now in Dest. |
| 634 | // |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 635 | std::map<const Value*, Value*> ValueMap; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 636 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 637 | // AppendingVars - Keep track of global variables in the destination module |
| 638 | // with appending linkage. After the module is linked together, they are |
| 639 | // appended and the module is rewritten. |
| 640 | // |
| 641 | std::multimap<std::string, GlobalVariable *> AppendingVars; |
| 642 | |
| 643 | // Add all of the appending globals already in the Dest module to |
| 644 | // AppendingVars. |
| 645 | for (Module::giterator I = Dest->gbegin(), E = Dest->gend(); I != E; ++I) |
Chris Lattner | f414646 | 2003-05-14 12:11:51 +0000 | [diff] [blame] | 646 | if (I->hasAppendingLinkage()) |
| 647 | AppendingVars.insert(std::make_pair(I->getName(), I)); |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 648 | |
| 649 | // Insert all of the globals in src into the Dest module... without linking |
| 650 | // initializers (which could refer to functions not yet mapped over). |
| 651 | // |
| 652 | if (LinkGlobals(Dest, Src, ValueMap, AppendingVars, ErrorMsg)) return true; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 653 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 654 | // Link the functions together between the two modules, without doing function |
| 655 | // bodies... this just adds external function prototypes to the Dest |
| 656 | // function... We do this so that when we begin processing function bodies, |
| 657 | // all of the global values that may be referenced are available in our |
| 658 | // ValueMap. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 659 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 660 | if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg)) return true; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 661 | |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 662 | // Update the initializers in the Dest module now that all globals that may |
| 663 | // be referenced are in Dest. |
| 664 | // |
| 665 | if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true; |
| 666 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 667 | // Link in the function bodies that are defined in the source module into the |
| 668 | // DestModule. This consists basically of copying the function over and |
| 669 | // fixing up references to values. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 670 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 671 | if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true; |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 672 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 673 | // If there were any appending global variables, link them together now. |
| 674 | // |
| 675 | if (LinkAppendingVars(Dest, AppendingVars, ErrorMsg)) return true; |
| 676 | |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 677 | return false; |
| 678 | } |
Vikram S. Adve | 9466f51 | 2001-10-28 21:38:02 +0000 | [diff] [blame] | 679 | |