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