Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 1 | //===- Linker.cpp - Module Linker Implementation --------------------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the LLVM module linker. |
| 11 | // |
| 12 | // Specifically, this: |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 13 | // * Merges global variables between the two modules |
| 14 | // * Uninit + Uninit = Init, Init + Uninit = Init, Init + Init = Error if != |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 15 | // * Merges functions between two modules |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Utils/Linker.h" |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 21 | #include "llvm/SymbolTable.h" |
| 22 | #include "llvm/DerivedTypes.h" |
| 23 | #include "llvm/iOther.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 24 | #include "llvm/Constants.h" |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 25 | |
| 26 | // Error - Simple wrapper function to conditionally assign to E and return true. |
| 27 | // This just makes error return conditions a little bit simpler... |
| 28 | // |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 29 | static inline bool Error(std::string *E, const std::string &Message) { |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 30 | if (E) *E = Message; |
| 31 | return true; |
| 32 | } |
| 33 | |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 34 | // ResolveTypes - Attempt to link the two specified types together. Return true |
| 35 | // if there is an error and they cannot yet be linked. |
| 36 | // |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 37 | static bool ResolveTypes(const Type *DestTy, const Type *SrcTy, |
| 38 | SymbolTable *DestST, const std::string &Name) { |
| 39 | if (DestTy == SrcTy) return false; // If already equal, noop |
| 40 | |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 41 | // Does the type already exist in the module? |
| 42 | if (DestTy && !isa<OpaqueType>(DestTy)) { // Yup, the type already exists... |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 43 | if (const OpaqueType *OT = dyn_cast<OpaqueType>(SrcTy)) { |
| 44 | const_cast<OpaqueType*>(OT)->refineAbstractTypeTo(DestTy); |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 45 | } else { |
| 46 | return true; // Cannot link types... neither is opaque and not-equal |
| 47 | } |
| 48 | } else { // Type not in dest module. Add it now. |
| 49 | if (DestTy) // Type _is_ in module, just opaque... |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 50 | const_cast<OpaqueType*>(cast<OpaqueType>(DestTy)) |
| 51 | ->refineAbstractTypeTo(SrcTy); |
Chris Lattner | fcd0234 | 2003-08-23 20:31:10 +0000 | [diff] [blame] | 52 | else if (!Name.empty()) |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 53 | DestST->insert(Name, const_cast<Type*>(SrcTy)); |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 54 | } |
| 55 | return false; |
| 56 | } |
| 57 | |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 58 | static const FunctionType *getFT(const PATypeHolder &TH) { |
| 59 | return cast<FunctionType>(TH.get()); |
| 60 | } |
Chris Lattner | 9732be7 | 2003-08-22 20:16:48 +0000 | [diff] [blame] | 61 | static const StructType *getST(const PATypeHolder &TH) { |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 62 | return cast<StructType>(TH.get()); |
| 63 | } |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 64 | |
| 65 | // RecursiveResolveTypes - This is just like ResolveTypes, except that it |
| 66 | // recurses down into derived types, merging the used types if the parent types |
| 67 | // are compatible. |
| 68 | // |
Chris Lattner | e3092c9 | 2003-08-23 21:25:54 +0000 | [diff] [blame] | 69 | static bool RecursiveResolveTypesI(const PATypeHolder &DestTy, |
| 70 | const PATypeHolder &SrcTy, |
| 71 | SymbolTable *DestST, const std::string &Name, |
| 72 | std::vector<std::pair<PATypeHolder, PATypeHolder> > &Pointers) { |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 73 | const Type *SrcTyT = SrcTy.get(); |
| 74 | const Type *DestTyT = DestTy.get(); |
| 75 | if (DestTyT == SrcTyT) return false; // If already equal, noop |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 76 | |
| 77 | // If we found our opaque type, resolve it now! |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 78 | if (isa<OpaqueType>(DestTyT) || isa<OpaqueType>(SrcTyT)) |
| 79 | return ResolveTypes(DestTyT, SrcTyT, DestST, Name); |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 80 | |
| 81 | // Two types cannot be resolved together if they are of different primitive |
| 82 | // type. For example, we cannot resolve an int to a float. |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 83 | if (DestTyT->getPrimitiveID() != SrcTyT->getPrimitiveID()) return true; |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 84 | |
| 85 | // Otherwise, resolve the used type used by this derived type... |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 86 | switch (DestTyT->getPrimitiveID()) { |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 87 | case Type::FunctionTyID: { |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 88 | if (cast<FunctionType>(DestTyT)->isVarArg() != |
Chris Lattner | 841e00b | 2003-08-28 16:42:50 +0000 | [diff] [blame] | 89 | cast<FunctionType>(SrcTyT)->isVarArg() || |
| 90 | cast<FunctionType>(DestTyT)->getNumContainedTypes() != |
| 91 | cast<FunctionType>(SrcTyT)->getNumContainedTypes()) |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 92 | return true; |
| 93 | for (unsigned i = 0, e = getFT(DestTy)->getNumContainedTypes(); i != e; ++i) |
Chris Lattner | e3092c9 | 2003-08-23 21:25:54 +0000 | [diff] [blame] | 94 | if (RecursiveResolveTypesI(getFT(DestTy)->getContainedType(i), |
| 95 | getFT(SrcTy)->getContainedType(i), DestST, "", |
| 96 | Pointers)) |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 97 | return true; |
| 98 | return false; |
| 99 | } |
| 100 | case Type::StructTyID: { |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 101 | if (getST(DestTy)->getNumContainedTypes() != |
| 102 | getST(SrcTy)->getNumContainedTypes()) return 1; |
| 103 | for (unsigned i = 0, e = getST(DestTy)->getNumContainedTypes(); i != e; ++i) |
Chris Lattner | e3092c9 | 2003-08-23 21:25:54 +0000 | [diff] [blame] | 104 | if (RecursiveResolveTypesI(getST(DestTy)->getContainedType(i), |
| 105 | getST(SrcTy)->getContainedType(i), DestST, "", |
| 106 | Pointers)) |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 107 | return true; |
| 108 | return false; |
| 109 | } |
| 110 | case Type::ArrayTyID: { |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 111 | const ArrayType *DAT = cast<ArrayType>(DestTy.get()); |
| 112 | const ArrayType *SAT = cast<ArrayType>(SrcTy.get()); |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 113 | if (DAT->getNumElements() != SAT->getNumElements()) return true; |
Chris Lattner | e3092c9 | 2003-08-23 21:25:54 +0000 | [diff] [blame] | 114 | return RecursiveResolveTypesI(DAT->getElementType(), SAT->getElementType(), |
| 115 | DestST, "", Pointers); |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 116 | } |
Chris Lattner | e3092c9 | 2003-08-23 21:25:54 +0000 | [diff] [blame] | 117 | case Type::PointerTyID: { |
| 118 | // If this is a pointer type, check to see if we have already seen it. If |
| 119 | // so, we are in a recursive branch. Cut off the search now. We cannot use |
| 120 | // an associative container for this search, because the type pointers (keys |
| 121 | // in the container) change whenever types get resolved... |
| 122 | // |
| 123 | for (unsigned i = 0, e = Pointers.size(); i != e; ++i) |
| 124 | if (Pointers[i].first == DestTy) |
| 125 | return Pointers[i].second != SrcTy; |
| 126 | |
| 127 | // Otherwise, add the current pointers to the vector to stop recursion on |
| 128 | // this pair. |
| 129 | Pointers.push_back(std::make_pair(DestTyT, SrcTyT)); |
| 130 | bool Result = |
| 131 | RecursiveResolveTypesI(cast<PointerType>(DestTy.get())->getElementType(), |
| 132 | cast<PointerType>(SrcTy.get())->getElementType(), |
| 133 | DestST, "", Pointers); |
| 134 | Pointers.pop_back(); |
| 135 | return Result; |
| 136 | } |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 137 | default: assert(0 && "Unexpected type!"); return true; |
| 138 | } |
| 139 | } |
| 140 | |
Chris Lattner | e3092c9 | 2003-08-23 21:25:54 +0000 | [diff] [blame] | 141 | static bool RecursiveResolveTypes(const PATypeHolder &DestTy, |
| 142 | const PATypeHolder &SrcTy, |
| 143 | SymbolTable *DestST, const std::string &Name){ |
| 144 | std::vector<std::pair<PATypeHolder, PATypeHolder> > PointerTypes; |
| 145 | return RecursiveResolveTypesI(DestTy, SrcTy, DestST, Name, PointerTypes); |
| 146 | } |
| 147 | |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 148 | |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 149 | // LinkTypes - Go through the symbol table of the Src module and see if any |
| 150 | // types are named in the src module that are not named in the Dst module. |
| 151 | // Make sure there are no type name conflicts. |
| 152 | // |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 153 | static bool LinkTypes(Module *Dest, const Module *Src, std::string *Err) { |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 154 | SymbolTable *DestST = &Dest->getSymbolTable(); |
| 155 | const SymbolTable *SrcST = &Src->getSymbolTable(); |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 156 | |
| 157 | // Look for a type plane for Type's... |
| 158 | SymbolTable::const_iterator PI = SrcST->find(Type::TypeTy); |
| 159 | if (PI == SrcST->end()) return false; // No named types, do nothing. |
| 160 | |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 161 | // Some types cannot be resolved immediately because they depend on other |
| 162 | // types being resolved to each other first. This contains a list of types we |
| 163 | // are waiting to recheck. |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 164 | std::vector<std::string> DelayedTypesToResolve; |
| 165 | |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 166 | const SymbolTable::VarMap &VM = PI->second; |
| 167 | for (SymbolTable::type_const_iterator I = VM.begin(), E = VM.end(); |
| 168 | I != E; ++I) { |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 169 | const std::string &Name = I->first; |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 170 | Type *RHS = cast<Type>(I->second); |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 171 | |
| 172 | // 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] | 173 | Type *Entry = cast_or_null<Type>(DestST->lookup(Type::TypeTy, Name)); |
Chris Lattner | 2f6bb2b | 2003-01-30 20:53:43 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 175 | if (ResolveTypes(Entry, RHS, DestST, Name)) { |
| 176 | // They look different, save the types 'till later to resolve. |
| 177 | DelayedTypesToResolve.push_back(Name); |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 180 | |
| 181 | // Iteratively resolve types while we can... |
| 182 | while (!DelayedTypesToResolve.empty()) { |
| 183 | // Loop over all of the types, attempting to resolve them if possible... |
| 184 | unsigned OldSize = DelayedTypesToResolve.size(); |
| 185 | |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 186 | // Try direct resolution by name... |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 187 | for (unsigned i = 0; i != DelayedTypesToResolve.size(); ++i) { |
| 188 | const std::string &Name = DelayedTypesToResolve[i]; |
| 189 | Type *T1 = cast<Type>(VM.find(Name)->second); |
| 190 | Type *T2 = cast<Type>(DestST->lookup(Type::TypeTy, Name)); |
| 191 | if (!ResolveTypes(T2, T1, DestST, Name)) { |
| 192 | // We are making progress! |
| 193 | DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i); |
| 194 | --i; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Did we not eliminate any types? |
| 199 | if (DelayedTypesToResolve.size() == OldSize) { |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 200 | // Attempt to resolve subelements of types. This allows us to merge these |
| 201 | // two types: { int* } and { opaque* } |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 202 | for (unsigned i = 0, e = DelayedTypesToResolve.size(); i != e; ++i) { |
| 203 | const std::string &Name = DelayedTypesToResolve[i]; |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 204 | PATypeHolder T1(cast<Type>(VM.find(Name)->second)); |
| 205 | PATypeHolder T2(cast<Type>(DestST->lookup(Type::TypeTy, Name))); |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 206 | |
| 207 | if (!RecursiveResolveTypes(T2, T1, DestST, Name)) { |
| 208 | // We are making progress! |
| 209 | DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i); |
| 210 | |
| 211 | // Go back to the main loop, perhaps we can resolve directly by name |
| 212 | // now... |
| 213 | break; |
| 214 | } |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 215 | } |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 216 | |
| 217 | // If we STILL cannot resolve the types, then there is something wrong. |
| 218 | // Report the error. |
| 219 | if (DelayedTypesToResolve.size() == OldSize) { |
| 220 | // Build up an error message of all of the mismatched types. |
| 221 | std::string ErrorMessage; |
| 222 | for (unsigned i = 0, e = DelayedTypesToResolve.size(); i != e; ++i) { |
| 223 | const std::string &Name = DelayedTypesToResolve[i]; |
| 224 | const Type *T1 = cast<Type>(VM.find(Name)->second); |
| 225 | const Type *T2 = cast<Type>(DestST->lookup(Type::TypeTy, Name)); |
| 226 | ErrorMessage += " Type named '" + Name + |
| 227 | "' conflicts.\n Src='" + T1->getDescription() + |
| 228 | "'.\n Dest='" + T2->getDescription() + "'\n"; |
| 229 | } |
| 230 | return Error(Err, "Type conflict between types in modules:\n" + |
| 231 | ErrorMessage); |
| 232 | } |
Chris Lattner | 4c00e53 | 2003-05-15 16:30:55 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
| 236 | |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 237 | return false; |
| 238 | } |
| 239 | |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 240 | static void PrintMap(const std::map<const Value*, Value*> &M) { |
| 241 | 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] | 242 | I != E; ++I) { |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 243 | std::cerr << " Fr: " << (void*)I->first << " "; |
Chris Lattner | 87182ae | 2002-04-07 22:31:23 +0000 | [diff] [blame] | 244 | I->first->dump(); |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 245 | std::cerr << " To: " << (void*)I->second << " "; |
Chris Lattner | 87182ae | 2002-04-07 22:31:23 +0000 | [diff] [blame] | 246 | I->second->dump(); |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 247 | std::cerr << "\n"; |
Chris Lattner | 2d3e8bb | 2001-11-03 03:27:29 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 252 | // RemapOperand - Use LocalMap and GlobalMap to convert references from one |
| 253 | // module to another. This is somewhat sophisticated in that it can |
| 254 | // automatically handle constant references correctly as well... |
| 255 | // |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 256 | static Value *RemapOperand(const Value *In, |
| 257 | std::map<const Value*, Value*> &LocalMap, |
| 258 | std::map<const Value*, Value*> *GlobalMap) { |
| 259 | std::map<const Value*,Value*>::const_iterator I = LocalMap.find(In); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 260 | if (I != LocalMap.end()) return I->second; |
| 261 | |
| 262 | if (GlobalMap) { |
| 263 | I = GlobalMap->find(In); |
| 264 | if (I != GlobalMap->end()) return I->second; |
| 265 | } |
| 266 | |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 267 | // 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] | 268 | if (const Constant *CPV = dyn_cast<Constant>(In)) { |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 269 | if (!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV)) |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 270 | return const_cast<Constant*>(CPV); // Simple constants stay identical... |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 271 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 272 | Constant *Result = 0; |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 273 | |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 274 | if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 275 | const std::vector<Use> &Ops = CPA->getValues(); |
| 276 | std::vector<Constant*> Operands(Ops.size()); |
Chris Lattner | e306d94 | 2002-07-18 02:31:03 +0000 | [diff] [blame] | 277 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 278 | Operands[i] = |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 279 | cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap)); |
| 280 | Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands); |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 281 | } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 282 | const std::vector<Use> &Ops = CPS->getValues(); |
| 283 | std::vector<Constant*> Operands(Ops.size()); |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 284 | for (unsigned i = 0; i < Ops.size(); ++i) |
| 285 | Operands[i] = |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 286 | cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap)); |
| 287 | Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands); |
| 288 | } else if (isa<ConstantPointerNull>(CPV)) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 289 | Result = const_cast<Constant*>(CPV); |
| 290 | } else if (const ConstantPointerRef *CPR = |
| 291 | dyn_cast<ConstantPointerRef>(CPV)) { |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 292 | Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 293 | Result = ConstantPointerRef::get(cast<GlobalValue>(V)); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 294 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) { |
Chris Lattner | b319faf | 2002-08-20 19:35:11 +0000 | [diff] [blame] | 295 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 296 | Value *Ptr = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap); |
| 297 | std::vector<Constant*> Indices; |
| 298 | Indices.reserve(CE->getNumOperands()-1); |
| 299 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) |
| 300 | Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i), |
| 301 | LocalMap, GlobalMap))); |
| 302 | |
| 303 | Result = ConstantExpr::getGetElementPtr(cast<Constant>(Ptr), Indices); |
| 304 | } else if (CE->getNumOperands() == 1) { |
Chris Lattner | ad33348 | 2002-08-14 18:24:09 +0000 | [diff] [blame] | 305 | // Cast instruction |
| 306 | assert(CE->getOpcode() == Instruction::Cast); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 307 | Value *V = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap); |
Chris Lattner | ad33348 | 2002-08-14 18:24:09 +0000 | [diff] [blame] | 308 | Result = ConstantExpr::getCast(cast<Constant>(V), CE->getType()); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 309 | } else if (CE->getNumOperands() == 2) { |
| 310 | // Binary operator... |
| 311 | Value *V1 = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap); |
| 312 | Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap); |
| 313 | |
| 314 | Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V1), |
Chris Lattner | e8e4605 | 2002-07-30 18:54:22 +0000 | [diff] [blame] | 315 | cast<Constant>(V2)); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 316 | } else { |
Chris Lattner | b319faf | 2002-08-20 19:35:11 +0000 | [diff] [blame] | 317 | assert(0 && "Unknown constant expr type!"); |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 320 | } else { |
| 321 | assert(0 && "Unknown type of derived type constant value!"); |
| 322 | } |
| 323 | |
| 324 | // Cache the mapping in our local map structure... |
Chris Lattner | d149c05 | 2002-09-23 18:14:15 +0000 | [diff] [blame] | 325 | if (GlobalMap) |
| 326 | GlobalMap->insert(std::make_pair(In, Result)); |
| 327 | else |
| 328 | LocalMap.insert(std::make_pair(In, Result)); |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 329 | return Result; |
| 330 | } |
Chris Lattner | 2d3e8bb | 2001-11-03 03:27:29 +0000 | [diff] [blame] | 331 | |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 332 | std::cerr << "XXX LocalMap: \n"; |
Chris Lattner | 2d3e8bb | 2001-11-03 03:27:29 +0000 | [diff] [blame] | 333 | PrintMap(LocalMap); |
| 334 | |
| 335 | if (GlobalMap) { |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 336 | std::cerr << "XXX GlobalMap: \n"; |
Chris Lattner | 2d3e8bb | 2001-11-03 03:27:29 +0000 | [diff] [blame] | 337 | PrintMap(*GlobalMap); |
| 338 | } |
| 339 | |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 340 | std::cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n"; |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 341 | assert(0 && "Couldn't remap value!"); |
| 342 | return 0; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Chris Lattner | fcd0234 | 2003-08-23 20:31:10 +0000 | [diff] [blame] | 345 | /// FindGlobalNamed - Look in the specified symbol table for a global with the |
| 346 | /// specified name and type. If an exactly matching global does not exist, see |
| 347 | /// if there is a global which is "type compatible" with the specified |
| 348 | /// name/type. This allows us to resolve things like '%x = global int*' with |
| 349 | /// '%x = global opaque*'. |
| 350 | /// |
| 351 | static GlobalValue *FindGlobalNamed(const std::string &Name, const Type *Ty, |
| 352 | SymbolTable *ST) { |
| 353 | // See if an exact match exists in the symbol table... |
| 354 | if (Value *V = ST->lookup(Ty, Name)) return cast<GlobalValue>(V); |
| 355 | |
| 356 | // It doesn't exist exactly, scan through all of the type planes in the symbol |
| 357 | // table, checking each of them for a type-compatible version. |
| 358 | // |
Chris Lattner | f44c605 | 2003-08-23 21:32:24 +0000 | [diff] [blame] | 359 | for (SymbolTable::iterator I = ST->begin(), E = ST->end(); I != E; ++I) |
Chris Lattner | 77c5f73 | 2003-08-24 19:30:20 +0000 | [diff] [blame] | 360 | if (I->first != Type::TypeTy) { |
Chris Lattner | f44c605 | 2003-08-23 21:32:24 +0000 | [diff] [blame] | 361 | SymbolTable::VarMap &VM = I->second; |
| 362 | // Does this type plane contain an entry with the specified name? |
| 363 | SymbolTable::type_iterator TI = VM.find(Name); |
| 364 | if (TI != VM.end()) { |
| 365 | // Determine whether we can fold the two types together, resolving them. |
| 366 | // If so, we can use this value. |
| 367 | if (!RecursiveResolveTypes(Ty, I->first, ST, "")) |
| 368 | return cast<GlobalValue>(TI->second); |
| 369 | } |
Chris Lattner | fcd0234 | 2003-08-23 20:31:10 +0000 | [diff] [blame] | 370 | } |
Chris Lattner | fcd0234 | 2003-08-23 20:31:10 +0000 | [diff] [blame] | 371 | return 0; // Otherwise, nothing could be found. |
| 372 | } |
| 373 | |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 374 | |
| 375 | // LinkGlobals - Loop through the global variables in the src module and merge |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 376 | // them into the dest module. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 377 | // |
| 378 | static bool LinkGlobals(Module *Dest, const Module *Src, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 379 | std::map<const Value*, Value*> &ValueMap, |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 380 | std::multimap<std::string, GlobalVariable *> &AppendingVars, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 381 | std::string *Err) { |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 382 | // We will need a module level symbol table if the src module has a module |
| 383 | // level symbol table... |
Chris Lattner | b91b657 | 2002-12-03 18:32:30 +0000 | [diff] [blame] | 384 | SymbolTable *ST = (SymbolTable*)&Dest->getSymbolTable(); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 385 | |
| 386 | // Loop over all of the globals in the src module, mapping them over as we go |
| 387 | // |
| 388 | 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] | 389 | const GlobalVariable *SGV = I; |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 390 | GlobalVariable *DGV = 0; |
| 391 | if (SGV->hasName()) { |
| 392 | // 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] | 393 | // that may be in a module level symbol table are Global Vars and |
| 394 | // Functions, and they both have distinct, nonoverlapping, possible types. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 395 | // |
Chris Lattner | fcd0234 | 2003-08-23 20:31:10 +0000 | [diff] [blame] | 396 | DGV = cast_or_null<GlobalVariable>(FindGlobalNamed(SGV->getName(), |
| 397 | SGV->getType(), ST)); |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 398 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 399 | |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 400 | assert(SGV->hasInitializer() || SGV->hasExternalLinkage() && |
| 401 | "Global must either be external or have an initializer!"); |
| 402 | |
Chris Lattner | 0fec08e | 2003-04-21 21:07:05 +0000 | [diff] [blame] | 403 | bool SGExtern = SGV->isExternal(); |
| 404 | bool DGExtern = DGV ? DGV->isExternal() : false; |
| 405 | |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 406 | if (!DGV || DGV->hasInternalLinkage() || SGV->hasInternalLinkage()) { |
| 407 | // No linking to be performed, simply create an identical version of the |
| 408 | // symbol over in the dest module... the initializer will be filled in |
| 409 | // later by LinkGlobalInits... |
| 410 | // |
Chris Lattner | 2719bac | 2003-04-21 21:15:04 +0000 | [diff] [blame] | 411 | GlobalVariable *NewDGV = |
| 412 | new GlobalVariable(SGV->getType()->getElementType(), |
| 413 | SGV->isConstant(), SGV->getLinkage(), /*init*/0, |
| 414 | SGV->getName(), Dest); |
| 415 | |
| 416 | // If the LLVM runtime renamed the global, but it is an externally visible |
| 417 | // symbol, DGV must be an existing global with internal linkage. Rename |
| 418 | // it. |
| 419 | if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage()){ |
| 420 | assert(DGV && DGV->getName() == SGV->getName() && |
| 421 | DGV->hasInternalLinkage()); |
| 422 | DGV->setName(""); |
| 423 | NewDGV->setName(SGV->getName()); // Force the name back |
| 424 | DGV->setName(SGV->getName()); // This will cause a renaming |
| 425 | assert(NewDGV->getName() == SGV->getName() && |
| 426 | DGV->getName() != SGV->getName()); |
| 427 | } |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 428 | |
| 429 | // Make sure to remember this mapping... |
Chris Lattner | 2719bac | 2003-04-21 21:15:04 +0000 | [diff] [blame] | 430 | ValueMap.insert(std::make_pair(SGV, NewDGV)); |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 431 | if (SGV->hasAppendingLinkage()) |
| 432 | // Keep track that this is an appending variable... |
| 433 | AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV)); |
| 434 | |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 435 | } else if (SGV->isExternal()) { |
| 436 | // If SGV is external or if both SGV & DGV are external.. Just link the |
| 437 | // external globals, we aren't adding anything. |
| 438 | ValueMap.insert(std::make_pair(SGV, DGV)); |
| 439 | |
| 440 | } else if (DGV->isExternal()) { // If DGV is external but SGV is not... |
| 441 | ValueMap.insert(std::make_pair(SGV, DGV)); |
| 442 | DGV->setLinkage(SGV->getLinkage()); // Inherit linkage! |
Chris Lattner | 72ac148d | 2003-10-16 18:29:00 +0000 | [diff] [blame] | 443 | } else if (SGV->hasWeakLinkage()) { |
| 444 | // At this point we know that DGV has LinkOnce, Appending, Weak, or |
| 445 | // External linkage. If DGV is Appending, this is an error. |
| 446 | if (DGV->hasAppendingLinkage()) |
| 447 | return Error(Err, "Linking globals named '" + SGV->getName() + |
| 448 | " ' with 'weak' and 'appending' linkage is not allowed!"); |
| 449 | // Otherwise, just perform the link. |
| 450 | ValueMap.insert(std::make_pair(SGV, DGV)); |
| 451 | } else if (DGV->hasWeakLinkage()) { |
| 452 | // At this point we know that SGV has LinkOnce, Appending, or External |
| 453 | // linkage. If SGV is Appending, this is an error. |
| 454 | if (SGV->hasAppendingLinkage()) |
| 455 | return Error(Err, "Linking globals named '" + SGV->getName() + |
| 456 | " ' with 'weak' and 'appending' linkage is not allowed!"); |
| 457 | if (!SGV->hasLinkOnceLinkage()) |
| 458 | DGV->setLinkage(SGV->getLinkage()); // Inherit linkage! |
| 459 | ValueMap.insert(std::make_pair(SGV, DGV)); |
| 460 | |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 461 | } else if (SGV->getLinkage() != DGV->getLinkage()) { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 462 | return Error(Err, "Global variables named '" + SGV->getName() + |
| 463 | "' have different linkage specifiers!"); |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 464 | } else if (SGV->hasExternalLinkage()) { |
| 465 | // Allow linking two exactly identical external global variables... |
Chris Lattner | f85770c | 2003-10-21 21:52:20 +0000 | [diff] [blame^] | 466 | if (SGV->isConstant() != DGV->isConstant()) |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 467 | return Error(Err, "Global Variable Collision on '" + |
| 468 | SGV->getType()->getDescription() + " %" + SGV->getName() + |
| 469 | "' - Global variables differ in const'ness"); |
Chris Lattner | f85770c | 2003-10-21 21:52:20 +0000 | [diff] [blame^] | 470 | |
| 471 | if (SGV->getInitializer() != DGV->getInitializer()) |
| 472 | return Error(Err, "Global Variable Collision on '" + |
| 473 | SGV->getType()->getDescription() + " %" + SGV->getName() + |
| 474 | "' - External linkage globals have different initializers"); |
| 475 | |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 476 | ValueMap.insert(std::make_pair(SGV, DGV)); |
| 477 | } else if (SGV->hasLinkOnceLinkage()) { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 478 | // If the global variable has a name, and that name is already in use in |
| 479 | // the Dest module, make sure that the name is a compatible global |
| 480 | // variable... |
| 481 | // |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 482 | // 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] | 483 | if (SGV->isConstant() != DGV->isConstant()) |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 484 | return Error(Err, "Global Variable Collision on '" + |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 485 | SGV->getType()->getDescription() + " %" + SGV->getName() + |
| 486 | "' - Global variables differ in const'ness"); |
Chris Lattner | 0fec08e | 2003-04-21 21:07:05 +0000 | [diff] [blame] | 487 | |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 488 | // Okay, everything is cool, remember the mapping... |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 489 | ValueMap.insert(std::make_pair(SGV, DGV)); |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 490 | } else if (SGV->hasAppendingLinkage()) { |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 491 | // No linking is performed yet. Just insert a new copy of the global, and |
| 492 | // keep track of the fact that it is an appending variable in the |
| 493 | // AppendingVars map. The name is cleared out so that no linkage is |
| 494 | // performed. |
| 495 | GlobalVariable *NewDGV = |
| 496 | new GlobalVariable(SGV->getType()->getElementType(), |
| 497 | SGV->isConstant(), SGV->getLinkage(), /*init*/0, |
| 498 | "", Dest); |
| 499 | |
| 500 | // Make sure to remember this mapping... |
| 501 | ValueMap.insert(std::make_pair(SGV, NewDGV)); |
| 502 | |
| 503 | // Keep track that this is an appending variable... |
| 504 | AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV)); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 505 | } else { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 506 | assert(0 && "Unknown linkage!"); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | return false; |
| 510 | } |
| 511 | |
| 512 | |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 513 | // LinkGlobalInits - Update the initializers in the Dest module now that all |
| 514 | // globals that may be referenced are in Dest. |
| 515 | // |
| 516 | static bool LinkGlobalInits(Module *Dest, const Module *Src, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 517 | std::map<const Value*, Value*> &ValueMap, |
| 518 | std::string *Err) { |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 519 | |
| 520 | // Loop over all of the globals in the src module, mapping them over as we go |
| 521 | // |
| 522 | 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] | 523 | const GlobalVariable *SGV = I; |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 524 | |
| 525 | if (SGV->hasInitializer()) { // Only process initialized GV's |
| 526 | // Figure out what the initializer looks like in the dest module... |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 527 | Constant *SInit = |
Chris Lattner | 2f6bb2b | 2003-01-30 20:53:43 +0000 | [diff] [blame] | 528 | cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap, 0)); |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 529 | |
| 530 | GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]); |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 531 | if (DGV->hasInitializer()) { |
| 532 | assert(SGV->getLinkage() == DGV->getLinkage()); |
| 533 | if (SGV->hasExternalLinkage()) { |
| 534 | if (DGV->getInitializer() != SInit) |
| 535 | return Error(Err, "Global Variable Collision on '" + |
| 536 | SGV->getType()->getDescription() +"':%"+SGV->getName()+ |
| 537 | " - Global variables have different initializers"); |
Chris Lattner | 72ac148d | 2003-10-16 18:29:00 +0000 | [diff] [blame] | 538 | } else if (DGV->hasLinkOnceLinkage() || DGV->hasWeakLinkage()) { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 539 | // Nothing is required, mapped values will take the new global |
| 540 | // automatically. |
| 541 | } else if (DGV->hasAppendingLinkage()) { |
| 542 | assert(0 && "Appending linkage unimplemented!"); |
| 543 | } else { |
| 544 | assert(0 && "Unknown linkage!"); |
| 545 | } |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 546 | } else { |
| 547 | // Copy the initializer over now... |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 548 | DGV->setInitializer(SInit); |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | } |
| 552 | return false; |
| 553 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 554 | |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 555 | // LinkFunctionProtos - Link the functions together between the two modules, |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 556 | // without doing function bodies... this just adds external function prototypes |
| 557 | // to the Dest function... |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 558 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 559 | static bool LinkFunctionProtos(Module *Dest, const Module *Src, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 560 | std::map<const Value*, Value*> &ValueMap, |
| 561 | std::string *Err) { |
Chris Lattner | b91b657 | 2002-12-03 18:32:30 +0000 | [diff] [blame] | 562 | SymbolTable *ST = (SymbolTable*)&Dest->getSymbolTable(); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 563 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 564 | // Loop over all of the functions in the src module, mapping them over as we |
| 565 | // go |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 566 | // |
| 567 | 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] | 568 | const Function *SF = I; // SrcFunction |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 569 | Function *DF = 0; |
| 570 | if (SF->hasName()) |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 571 | // The same named thing is a Function, because the only two things |
| 572 | // that may be in a module level symbol table are Global Vars and |
| 573 | // Functions, and they both have distinct, nonoverlapping, possible types. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 574 | // |
Chris Lattner | fcd0234 | 2003-08-23 20:31:10 +0000 | [diff] [blame] | 575 | DF = cast_or_null<Function>(FindGlobalNamed(SF->getName(), SF->getType(), |
| 576 | ST)); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 577 | |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 578 | if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) { |
Chris Lattner | 0fec08e | 2003-04-21 21:07:05 +0000 | [diff] [blame] | 579 | // Function does not already exist, simply insert an function signature |
| 580 | // identical to SF into the dest module... |
Chris Lattner | 2719bac | 2003-04-21 21:15:04 +0000 | [diff] [blame] | 581 | Function *NewDF = new Function(SF->getFunctionType(), SF->getLinkage(), |
| 582 | SF->getName(), Dest); |
| 583 | |
| 584 | // If the LLVM runtime renamed the function, but it is an externally |
| 585 | // visible symbol, DF must be an existing function with internal linkage. |
| 586 | // Rename it. |
| 587 | if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage()) { |
| 588 | assert(DF && DF->getName() == SF->getName() &&DF->hasInternalLinkage()); |
| 589 | DF->setName(""); |
| 590 | NewDF->setName(SF->getName()); // Force the name back |
| 591 | DF->setName(SF->getName()); // This will cause a renaming |
| 592 | assert(NewDF->getName() == SF->getName() && |
| 593 | DF->getName() != SF->getName()); |
| 594 | } |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 595 | |
| 596 | // ... and remember this mapping... |
Chris Lattner | 2719bac | 2003-04-21 21:15:04 +0000 | [diff] [blame] | 597 | ValueMap.insert(std::make_pair(SF, NewDF)); |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 598 | } else if (SF->isExternal()) { |
| 599 | // If SF is external or if both SF & DF are external.. Just link the |
| 600 | // external functions, we aren't adding anything. |
| 601 | ValueMap.insert(std::make_pair(SF, DF)); |
| 602 | } else if (DF->isExternal()) { // If DF is external but SF is not... |
| 603 | // Link the external functions, update linkage qualifiers |
| 604 | ValueMap.insert(std::make_pair(SF, DF)); |
| 605 | DF->setLinkage(SF->getLinkage()); |
| 606 | |
Chris Lattner | 72ac148d | 2003-10-16 18:29:00 +0000 | [diff] [blame] | 607 | } else if (SF->hasWeakLinkage()) { |
| 608 | // At this point we know that DF has LinkOnce, Weak, or External linkage. |
| 609 | ValueMap.insert(std::make_pair(SF, DF)); |
| 610 | |
| 611 | } else if (DF->hasWeakLinkage()) { |
| 612 | // At this point we know that SF has LinkOnce or External linkage. |
| 613 | ValueMap.insert(std::make_pair(SF, DF)); |
| 614 | if (!SF->hasLinkOnceLinkage()) // Don't inherit linkonce linkage |
| 615 | DF->setLinkage(SF->getLinkage()); |
| 616 | |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 617 | } else if (SF->getLinkage() != DF->getLinkage()) { |
Chris Lattner | 0fec08e | 2003-04-21 21:07:05 +0000 | [diff] [blame] | 618 | return Error(Err, "Functions named '" + SF->getName() + |
| 619 | "' have different linkage specifiers!"); |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 620 | } else if (SF->hasExternalLinkage()) { |
| 621 | // The function is defined in both modules!! |
| 622 | return Error(Err, "Function '" + |
| 623 | SF->getFunctionType()->getDescription() + "':\"" + |
| 624 | SF->getName() + "\" - Function is already defined!"); |
| 625 | } else if (SF->hasLinkOnceLinkage()) { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 626 | // Completely ignore the source function. |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 627 | ValueMap.insert(std::make_pair(SF, DF)); |
Chris Lattner | c2b97d4 | 2003-04-23 18:38:39 +0000 | [diff] [blame] | 628 | } else { |
| 629 | assert(0 && "Unknown linkage configuration found!"); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | return false; |
| 633 | } |
| 634 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 635 | // LinkFunctionBody - Copy the source function over into the dest function and |
| 636 | // fix up references to values. At this point we know that Dest is an external |
| 637 | // function, and that Src is not. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 638 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 639 | static bool LinkFunctionBody(Function *Dest, const Function *Src, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 640 | std::map<const Value*, Value*> &GlobalMap, |
| 641 | std::string *Err) { |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 642 | assert(Src && Dest && Dest->isExternal() && !Src->isExternal()); |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 643 | std::map<const Value*, Value*> LocalMap; // Map for function local values |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 644 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 645 | // Go through and convert function arguments over... |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 646 | Function::aiterator DI = Dest->abegin(); |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 647 | for (Function::const_aiterator I = Src->abegin(), E = Src->aend(); |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 648 | I != E; ++I, ++DI) { |
| 649 | DI->setName(I->getName()); // Copy the name information over... |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 650 | |
| 651 | // Add a mapping to our local map |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 652 | LocalMap.insert(std::make_pair(I, DI)); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | // Loop over all of the basic blocks, copying the instructions over... |
| 656 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 657 | 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] | 658 | // Create new basic block and add to mapping and the Dest function... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 659 | BasicBlock *DBB = new BasicBlock(I->getName(), Dest); |
| 660 | LocalMap.insert(std::make_pair(I, DBB)); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 661 | |
| 662 | // Loop over all of the instructions in the src basic block, copying them |
| 663 | // over. Note that this is broken in a strict sense because the cloned |
| 664 | // instructions will still be referencing values in the Src module, not |
| 665 | // the remapped values. In our case, however, we will not get caught and |
| 666 | // so we can delay patching the values up until later... |
| 667 | // |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 668 | for (BasicBlock::const_iterator II = I->begin(), IE = I->end(); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 669 | II != IE; ++II) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 670 | Instruction *DI = II->clone(); |
| 671 | DI->setName(II->getName()); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 672 | DBB->getInstList().push_back(DI); |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 673 | LocalMap.insert(std::make_pair(II, DI)); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 677 | // At this point, all of the instructions and values of the function are now |
| 678 | // copied over. The only problem is that they are still referencing values in |
| 679 | // the Source function as operands. Loop through all of the operands of the |
| 680 | // functions and patch them up to point to the local versions... |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 681 | // |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 682 | for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB) |
| 683 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 684 | for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end(); |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 685 | OI != OE; ++OI) |
| 686 | *OI = RemapOperand(*OI, LocalMap, &GlobalMap); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 687 | |
| 688 | return false; |
| 689 | } |
| 690 | |
| 691 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 692 | // LinkFunctionBodies - Link in the function bodies that are defined in the |
| 693 | // source module into the DestModule. This consists basically of copying the |
| 694 | // function over and fixing up references to values. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 695 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 696 | static bool LinkFunctionBodies(Module *Dest, const Module *Src, |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 697 | std::map<const Value*, Value*> &ValueMap, |
| 698 | std::string *Err) { |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 699 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 700 | // Loop over all of the functions in the src module, mapping them over as we |
| 701 | // go |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 702 | // |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 703 | for (Module::const_iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF){ |
| 704 | if (!SF->isExternal()) { // No body if function is external |
| 705 | Function *DF = cast<Function>(ValueMap[SF]); // Destination function |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 706 | |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 707 | // DF not external SF external? |
| 708 | if (!DF->isExternal()) { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 709 | if (DF->hasLinkOnceLinkage()) continue; // No relinkage for link-once! |
Chris Lattner | 72ac148d | 2003-10-16 18:29:00 +0000 | [diff] [blame] | 710 | if (SF->hasWeakLinkage()) continue; |
| 711 | return Error(Err, "Function '" + SF->getName() + |
| 712 | "' body multiply defined!"); |
Chris Lattner | c2d774b | 2001-10-23 20:43:42 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 715 | if (LinkFunctionBody(DF, SF, ValueMap, Err)) return true; |
Chris Lattner | c2d774b | 2001-10-23 20:43:42 +0000 | [diff] [blame] | 716 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 717 | } |
| 718 | return false; |
| 719 | } |
| 720 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 721 | // LinkAppendingVars - If there were any appending global variables, link them |
| 722 | // together now. Return true on error. |
| 723 | // |
| 724 | static bool LinkAppendingVars(Module *M, |
| 725 | std::multimap<std::string, GlobalVariable *> &AppendingVars, |
| 726 | std::string *ErrorMsg) { |
| 727 | if (AppendingVars.empty()) return false; // Nothing to do. |
| 728 | |
| 729 | // Loop over the multimap of appending vars, processing any variables with the |
| 730 | // same name, forming a new appending global variable with both of the |
| 731 | // initializers merged together, then rewrite references to the old variables |
| 732 | // and delete them. |
| 733 | // |
| 734 | std::vector<Constant*> Inits; |
| 735 | while (AppendingVars.size() > 1) { |
| 736 | // Get the first two elements in the map... |
| 737 | std::multimap<std::string, |
| 738 | GlobalVariable*>::iterator Second = AppendingVars.begin(), First=Second++; |
| 739 | |
| 740 | // If the first two elements are for different names, there is no pair... |
| 741 | // Otherwise there is a pair, so link them together... |
| 742 | if (First->first == Second->first) { |
| 743 | GlobalVariable *G1 = First->second, *G2 = Second->second; |
| 744 | const ArrayType *T1 = cast<ArrayType>(G1->getType()->getElementType()); |
| 745 | const ArrayType *T2 = cast<ArrayType>(G2->getType()->getElementType()); |
| 746 | |
| 747 | // Check to see that they two arrays agree on type... |
| 748 | if (T1->getElementType() != T2->getElementType()) |
| 749 | return Error(ErrorMsg, |
| 750 | "Appending variables with different element types need to be linked!"); |
| 751 | if (G1->isConstant() != G2->isConstant()) |
| 752 | return Error(ErrorMsg, |
| 753 | "Appending variables linked with different const'ness!"); |
| 754 | |
| 755 | unsigned NewSize = T1->getNumElements() + T2->getNumElements(); |
| 756 | ArrayType *NewType = ArrayType::get(T1->getElementType(), NewSize); |
| 757 | |
| 758 | // Create the new global variable... |
| 759 | GlobalVariable *NG = |
| 760 | new GlobalVariable(NewType, G1->isConstant(), G1->getLinkage(), |
| 761 | /*init*/0, First->first, M); |
| 762 | |
| 763 | // Merge the initializer... |
| 764 | Inits.reserve(NewSize); |
| 765 | ConstantArray *I = cast<ConstantArray>(G1->getInitializer()); |
| 766 | for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i) |
| 767 | Inits.push_back(cast<Constant>(I->getValues()[i])); |
| 768 | I = cast<ConstantArray>(G2->getInitializer()); |
| 769 | for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i) |
| 770 | Inits.push_back(cast<Constant>(I->getValues()[i])); |
| 771 | NG->setInitializer(ConstantArray::get(NewType, Inits)); |
| 772 | Inits.clear(); |
| 773 | |
| 774 | // Replace any uses of the two global variables with uses of the new |
| 775 | // global... |
| 776 | |
| 777 | // FIXME: This should rewrite simple/straight-forward uses such as |
| 778 | // getelementptr instructions to not use the Cast! |
| 779 | ConstantPointerRef *NGCP = ConstantPointerRef::get(NG); |
| 780 | G1->replaceAllUsesWith(ConstantExpr::getCast(NGCP, G1->getType())); |
| 781 | G2->replaceAllUsesWith(ConstantExpr::getCast(NGCP, G2->getType())); |
| 782 | |
| 783 | // Remove the two globals from the module now... |
| 784 | M->getGlobalList().erase(G1); |
| 785 | M->getGlobalList().erase(G2); |
| 786 | |
| 787 | // Put the new global into the AppendingVars map so that we can handle |
| 788 | // linking of more than two vars... |
| 789 | Second->second = NG; |
| 790 | } |
| 791 | AppendingVars.erase(First); |
| 792 | } |
| 793 | |
| 794 | return false; |
| 795 | } |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 796 | |
| 797 | |
| 798 | // LinkModules - This function links two modules together, with the resulting |
| 799 | // left module modified to be the composite of the two input modules. If an |
| 800 | // 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] | 801 | // the problem. Upon failure, the Dest module could be in a modified state, and |
| 802 | // shouldn't be relied on to be consistent. |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 803 | // |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 804 | bool LinkModules(Module *Dest, const Module *Src, std::string *ErrorMsg) { |
Chris Lattner | 873c5e7 | 2003-08-24 19:26:42 +0000 | [diff] [blame] | 805 | if (Dest->getEndianness() == Module::AnyEndianness) |
| 806 | Dest->setEndianness(Src->getEndianness()); |
| 807 | if (Dest->getPointerSize() == Module::AnyPointerSize) |
| 808 | Dest->setPointerSize(Src->getPointerSize()); |
| 809 | |
| 810 | if (Src->getEndianness() != Module::AnyEndianness && |
| 811 | Dest->getEndianness() != Src->getEndianness()) |
Chris Lattner | 43a9994 | 2003-04-22 19:13:20 +0000 | [diff] [blame] | 812 | std::cerr << "WARNING: Linking two modules of different endianness!\n"; |
Chris Lattner | 873c5e7 | 2003-08-24 19:26:42 +0000 | [diff] [blame] | 813 | if (Src->getPointerSize() != Module::AnyPointerSize && |
| 814 | Dest->getPointerSize() != Src->getPointerSize()) |
Chris Lattner | 43a9994 | 2003-04-22 19:13:20 +0000 | [diff] [blame] | 815 | std::cerr << "WARNING: Linking two modules of different pointer size!\n"; |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 816 | |
| 817 | // LinkTypes - Go through the symbol table of the Src module and see if any |
| 818 | // types are named in the src module that are not named in the Dst module. |
| 819 | // Make sure there are no type name conflicts. |
| 820 | // |
| 821 | if (LinkTypes(Dest, Src, ErrorMsg)) return true; |
| 822 | |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 823 | // ValueMap - Mapping of values from what they used to be in Src, to what they |
| 824 | // are now in Dest. |
| 825 | // |
Chris Lattner | 5c2d335 | 2003-01-30 19:53:34 +0000 | [diff] [blame] | 826 | std::map<const Value*, Value*> ValueMap; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 827 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 828 | // AppendingVars - Keep track of global variables in the destination module |
| 829 | // with appending linkage. After the module is linked together, they are |
| 830 | // appended and the module is rewritten. |
| 831 | // |
| 832 | std::multimap<std::string, GlobalVariable *> AppendingVars; |
| 833 | |
| 834 | // Add all of the appending globals already in the Dest module to |
| 835 | // AppendingVars. |
| 836 | for (Module::giterator I = Dest->gbegin(), E = Dest->gend(); I != E; ++I) |
Chris Lattner | f414646 | 2003-05-14 12:11:51 +0000 | [diff] [blame] | 837 | if (I->hasAppendingLinkage()) |
| 838 | AppendingVars.insert(std::make_pair(I->getName(), I)); |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 839 | |
| 840 | // Insert all of the globals in src into the Dest module... without linking |
| 841 | // initializers (which could refer to functions not yet mapped over). |
| 842 | // |
| 843 | if (LinkGlobals(Dest, Src, ValueMap, AppendingVars, ErrorMsg)) return true; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 844 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 845 | // Link the functions together between the two modules, without doing function |
| 846 | // bodies... this just adds external function prototypes to the Dest |
| 847 | // function... We do this so that when we begin processing function bodies, |
| 848 | // all of the global values that may be referenced are available in our |
| 849 | // ValueMap. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 850 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 851 | if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg)) return true; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 852 | |
Chris Lattner | 6cdf197 | 2002-07-18 00:13:08 +0000 | [diff] [blame] | 853 | // Update the initializers in the Dest module now that all globals that may |
| 854 | // be referenced are in Dest. |
| 855 | // |
| 856 | if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true; |
| 857 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 858 | // Link in the function bodies that are defined in the source module into the |
| 859 | // DestModule. This consists basically of copying the function over and |
| 860 | // fixing up references to values. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 861 | // |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 862 | if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true; |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 863 | |
Chris Lattner | 8166e6e | 2003-05-13 21:33:43 +0000 | [diff] [blame] | 864 | // If there were any appending global variables, link them together now. |
| 865 | // |
| 866 | if (LinkAppendingVars(Dest, AppendingVars, ErrorMsg)) return true; |
| 867 | |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 868 | return false; |
| 869 | } |
Vikram S. Adve | 9466f51 | 2001-10-28 21:38:02 +0000 | [diff] [blame] | 870 | |