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