LinkModules.cpp revision b319faff77a5bb12e753c45662f6c532b77f0b91
152f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//===- Linker.cpp - Module Linker Implementation --------------------------===// 252f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// 352f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// This file implements the LLVM module linker. 452f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// 552f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// Specifically, this: 68d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner// * Merges global variables between the two modules 78d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner// * Uninit + Uninit = Init, Init + Uninit = Init, Init + Init = Error if != 8c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// * Merges functions between two modules 952f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// 1052f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//===----------------------------------------------------------------------===// 1152f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner 12c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner#include "llvm/Transforms/Utils/Linker.h" 135c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner#include "llvm/Module.h" 145c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner#include "llvm/SymbolTable.h" 155c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner#include "llvm/DerivedTypes.h" 165c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner#include "llvm/iOther.h" 1731bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner#include "llvm/Constants.h" 18697954c15da58bd8b186dbafdedd8b06db770201Chris Lattnerusing std::cerr; 19697954c15da58bd8b186dbafdedd8b06db770201Chris Lattnerusing std::string; 20697954c15da58bd8b186dbafdedd8b06db770201Chris Lattnerusing std::map; 215c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 225c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// Error - Simple wrapper function to conditionally assign to E and return true. 235c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// This just makes error return conditions a little bit simpler... 245c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// 255c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattnerstatic inline bool Error(string *E, string Message) { 265c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner if (E) *E = Message; 275c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner return true; 285c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner} 295c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 302c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner// LinkTypes - Go through the symbol table of the Src module and see if any 312c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner// types are named in the src module that are not named in the Dst module. 322c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner// Make sure there are no type name conflicts. 332c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner// 342c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattnerstatic bool LinkTypes(Module *Dest, const Module *Src, string *Err = 0) { 352c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner // No symbol table? Can't have named types. 362c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner if (!Src->hasSymbolTable()) return false; 372c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner 382c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner SymbolTable *DestST = Dest->getSymbolTableSure(); 392c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner const SymbolTable *SrcST = Src->getSymbolTable(); 402c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner 412c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner // Look for a type plane for Type's... 422c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner SymbolTable::const_iterator PI = SrcST->find(Type::TypeTy); 432c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner if (PI == SrcST->end()) return false; // No named types, do nothing. 442c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner 452c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner const SymbolTable::VarMap &VM = PI->second; 462c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner for (SymbolTable::type_const_iterator I = VM.begin(), E = VM.end(); 472c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner I != E; ++I) { 482c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner const string &Name = I->first; 492c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner const Type *RHS = cast<Type>(I->second); 502c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner 512c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner // Check to see if this type name is already in the dest module... 522c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner const Type *Entry = cast_or_null<Type>(DestST->lookup(Type::TypeTy, Name)); 532c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner if (Entry) { // Yup, the value already exists... 542c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner if (Entry != RHS) // If it's the same, noop. Otherwise, error. 552c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner return Error(Err, "Type named '" + Name + 562c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner "' of different shape in modules.\n Src='" + 579b638019ca8870f418e3b42b39dd34dc7deebb2dChris Lattner Entry->getDescription() + "'.\n Dst='" + 582c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner RHS->getDescription() + "'"); 592c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner } else { // Type not in dest module. Add it now. 602c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner // TODO: FIXME WHEN TYPES AREN'T CONST 612c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner DestST->insert(Name, const_cast<Type*>(RHS)); 622c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner } 632c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner } 642c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner return false; 652c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner} 662c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner 672d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattnerstatic void PrintMap(const map<const Value*, Value*> &M) { 682d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner for (map<const Value*, Value*>::const_iterator I = M.begin(), E = M.end(); 692d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner I != E; ++I) { 7087182ae6baf28ca6da6ee4bc00cf1857f293acfeChris Lattner cerr << " Fr: " << (void*)I->first << " "; 7187182ae6baf28ca6da6ee4bc00cf1857f293acfeChris Lattner I->first->dump(); 7287182ae6baf28ca6da6ee4bc00cf1857f293acfeChris Lattner cerr << " To: " << (void*)I->second << " "; 7387182ae6baf28ca6da6ee4bc00cf1857f293acfeChris Lattner I->second->dump(); 7487182ae6baf28ca6da6ee4bc00cf1857f293acfeChris Lattner cerr << "\n"; 752d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner } 762d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner} 772d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner 782d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner 795c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// RemapOperand - Use LocalMap and GlobalMap to convert references from one 805c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// module to another. This is somewhat sophisticated in that it can 815c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// automatically handle constant references correctly as well... 825c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// 838d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattnerstatic Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap, 845c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner const map<const Value*, Value*> *GlobalMap = 0) { 855c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner map<const Value*,Value*>::const_iterator I = LocalMap.find(In); 865c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner if (I != LocalMap.end()) return I->second; 875c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 885c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner if (GlobalMap) { 895c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner I = GlobalMap->find(In); 905c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner if (I != GlobalMap->end()) return I->second; 915c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner } 925c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 938d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner // Check to see if it's a constant that we are interesting in transforming... 9418961504fc2b299578dba817900a0696cf3ccc4dChris Lattner if (const Constant *CPV = dyn_cast<Constant>(In)) { 956cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner if (!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV)) 9618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner return const_cast<Constant*>(CPV); // Simple constants stay identical... 978d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner 98e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner Constant *Result = 0; 998d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner 10018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) { 101697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner const std::vector<Use> &Ops = CPA->getValues(); 102697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner std::vector<Constant*> Operands(Ops.size()); 103e306d94782b1c608857738279852dcc050c66004Chris Lattner for (unsigned i = 0, e = Ops.size(); i != e; ++i) 1048d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner Operands[i] = 105e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap)); 106e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands); 10718961504fc2b299578dba817900a0696cf3ccc4dChris Lattner } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) { 108697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner const std::vector<Use> &Ops = CPS->getValues(); 109697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner std::vector<Constant*> Operands(Ops.size()); 1108d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner for (unsigned i = 0; i < Ops.size(); ++i) 1118d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner Operands[i] = 112e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap)); 113e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands); 114e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner } else if (isa<ConstantPointerNull>(CPV)) { 11518961504fc2b299578dba817900a0696cf3ccc4dChris Lattner Result = const_cast<Constant*>(CPV); 11618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner } else if (const ConstantPointerRef *CPR = 11718961504fc2b299578dba817900a0696cf3ccc4dChris Lattner dyn_cast<ConstantPointerRef>(CPV)) { 1188d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap); 119e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner Result = ConstantPointerRef::get(cast<GlobalValue>(V)); 1206cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) { 121b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner if (CE->getOpcode() == Instruction::GetElementPtr) { 122b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner Value *Ptr = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap); 123b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner std::vector<Constant*> Indices; 124b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner Indices.reserve(CE->getNumOperands()-1); 125b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) 126b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i), 127b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner LocalMap, GlobalMap))); 128b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner 129b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner Result = ConstantExpr::getGetElementPtr(cast<Constant>(Ptr), Indices); 130b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner } else if (CE->getNumOperands() == 1) { 131ad333484ea5ae976b83e35ccd3f6cfa6e71290e2Chris Lattner // Cast instruction 132ad333484ea5ae976b83e35ccd3f6cfa6e71290e2Chris Lattner assert(CE->getOpcode() == Instruction::Cast); 1336cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner Value *V = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap); 134ad333484ea5ae976b83e35ccd3f6cfa6e71290e2Chris Lattner Result = ConstantExpr::getCast(cast<Constant>(V), CE->getType()); 1356cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner } else if (CE->getNumOperands() == 2) { 1366cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner // Binary operator... 1376cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner Value *V1 = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap); 1386cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap); 1396cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner 1406cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V1), 141e8e4605021141d689493132a9c7c6fce6294937fChris Lattner cast<Constant>(V2)); 1426cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner } else { 143b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner assert(0 && "Unknown constant expr type!"); 1446cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner } 1456cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner 1468d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner } else { 1478d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner assert(0 && "Unknown type of derived type constant value!"); 1488d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner } 1498d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner 1508d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner // Cache the mapping in our local map structure... 151e306d94782b1c608857738279852dcc050c66004Chris Lattner LocalMap.insert(std::make_pair(In, Result)); 1528d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner return Result; 1538d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner } 1542d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner 1552d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner cerr << "XXX LocalMap: \n"; 1562d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner PrintMap(LocalMap); 1572d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner 1582d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner if (GlobalMap) { 1592d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner cerr << "XXX GlobalMap: \n"; 1602d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner PrintMap(*GlobalMap); 1612d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner } 1622d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner 1636cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n"; 1648d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner assert(0 && "Couldn't remap value!"); 1658d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner return 0; 1665c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner} 1675c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 1685c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 1695c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// LinkGlobals - Loop through the global variables in the src module and merge 1705c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// them into the dest module... 1715c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// 1725c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattnerstatic bool LinkGlobals(Module *Dest, const Module *Src, 1735c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner map<const Value*, Value*> &ValueMap, string *Err = 0) { 1745c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // We will need a module level symbol table if the src module has a module 1755c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // level symbol table... 1765c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner SymbolTable *ST = Src->getSymbolTable() ? Dest->getSymbolTableSure() : 0; 1775c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 1785c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // Loop over all of the globals in the src module, mapping them over as we go 1795c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // 1805c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){ 18118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner const GlobalVariable *SGV = I; 1825c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner Value *V; 1835c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 1845c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // If the global variable has a name, and that name is already in use in the 1855c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // Dest module, make sure that the name is a compatible global variable... 1865c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // 187e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner if (SGV->hasExternalLinkage() && SGV->hasName() && 188e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner (V = ST->lookup(SGV->getType(), SGV->getName())) && 189e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner cast<GlobalVariable>(V)->hasExternalLinkage()) { 1905c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // The same named thing is a global variable, because the only two things 19179df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner // that may be in a module level symbol table are Global Vars and 19279df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner // Functions, and they both have distinct, nonoverlapping, possible types. 1935c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // 1945c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner GlobalVariable *DGV = cast<GlobalVariable>(V); 1955c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 1965c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // Check to see if the two GV's have the same Const'ness... 1978d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner if (SGV->isConstant() != DGV->isConstant()) 1985c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner return Error(Err, "Global Variable Collision on '" + 1998d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner SGV->getType()->getDescription() + "':%" + SGV->getName() + 2005c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner " - Global variables differ in const'ness"); 2015c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 2028d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner // Okay, everything is cool, remember the mapping... 203697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner ValueMap.insert(std::make_pair(SGV, DGV)); 2045c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner } else { 2055c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // No linking to be performed, simply create an identical version of the 2068d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner // symbol over in the dest module... the initializer will be filled in 2078d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner // later by LinkGlobalInits... 2088d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner // 2098d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner GlobalVariable *DGV = 2107a1767520611d9ff6face702068de858e1cadf2cChris Lattner new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(), 211e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner SGV->hasInternalLinkage(), 0, SGV->getName()); 2125c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 2135c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // Add the new global to the dest module 2148d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner Dest->getGlobalList().push_back(DGV); 2155c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 2165c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // Make sure to remember this mapping... 217697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner ValueMap.insert(std::make_pair(SGV, DGV)); 2185c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner } 2195c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner } 2205c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner return false; 2215c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner} 2225c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 2235c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 2248d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner// LinkGlobalInits - Update the initializers in the Dest module now that all 2258d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner// globals that may be referenced are in Dest. 2268d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner// 2278d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattnerstatic bool LinkGlobalInits(Module *Dest, const Module *Src, 2288d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner map<const Value*, Value*> &ValueMap, 2298d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner string *Err = 0) { 2308d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner 2318d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner // Loop over all of the globals in the src module, mapping them over as we go 2328d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner // 2338d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){ 23418961504fc2b299578dba817900a0696cf3ccc4dChris Lattner const GlobalVariable *SGV = I; 2358d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner 2368d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner if (SGV->hasInitializer()) { // Only process initialized GV's 2378d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner // Figure out what the initializer looks like in the dest module... 238e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner Constant *DInit = 239e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap)); 2408d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner 2418d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]); 242e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner if (DGV->hasInitializer() && SGV->hasExternalLinkage() && 243e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner DGV->hasExternalLinkage()) { 2448d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner if (DGV->getInitializer() != DInit) 2458d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner return Error(Err, "Global Variable Collision on '" + 2468d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner SGV->getType()->getDescription() + "':%" +SGV->getName()+ 2478d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner " - Global variables have different initializers"); 2488d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner } else { 2498d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner // Copy the initializer over now... 2508d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner DGV->setInitializer(DInit); 2518d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner } 2528d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner } 2538d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner } 2548d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner return false; 2558d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner} 2565c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 25779df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner// LinkFunctionProtos - Link the functions together between the two modules, 258c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// without doing function bodies... this just adds external function prototypes 259c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// to the Dest function... 2605c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// 26179df7c0aaa18129e55968c8783ef8346807bd4afChris Lattnerstatic bool LinkFunctionProtos(Module *Dest, const Module *Src, 26279df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner map<const Value*, Value*> &ValueMap, 26379df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner string *Err = 0) { 2645c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // We will need a module level symbol table if the src module has a module 2655c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // level symbol table... 2665c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner SymbolTable *ST = Src->getSymbolTable() ? Dest->getSymbolTableSure() : 0; 2675c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 268c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // Loop over all of the functions in the src module, mapping them over as we 269c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // go 2705c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // 2715c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) { 27218961504fc2b299578dba817900a0696cf3ccc4dChris Lattner const Function *SF = I; // SrcFunction 2735c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner Value *V; 2745c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 275c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // If the function has a name, and that name is already in use in the Dest 276c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // module, make sure that the name is a compatible function... 2775c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // 27818961504fc2b299578dba817900a0696cf3ccc4dChris Lattner if (SF->hasExternalLinkage() && SF->hasName() && 27918961504fc2b299578dba817900a0696cf3ccc4dChris Lattner (V = ST->lookup(SF->getType(), SF->getName())) && 28079df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner cast<Function>(V)->hasExternalLinkage()) { 28179df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner // The same named thing is a Function, because the only two things 28279df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner // that may be in a module level symbol table are Global Vars and 28379df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner // Functions, and they both have distinct, nonoverlapping, possible types. 2845c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // 28518961504fc2b299578dba817900a0696cf3ccc4dChris Lattner Function *DF = cast<Function>(V); // DestFunction 2865c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 287c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // Check to make sure the function is not defined in both modules... 28818961504fc2b299578dba817900a0696cf3ccc4dChris Lattner if (!SF->isExternal() && !DF->isExternal()) 28979df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner return Error(Err, "Function '" + 29018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner SF->getFunctionType()->getDescription() + "':\"" + 29118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner SF->getName() + "\" - Function is already defined!"); 2925c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 2935c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // Otherwise, just remember this mapping... 29418961504fc2b299578dba817900a0696cf3ccc4dChris Lattner ValueMap.insert(std::make_pair(SF, DF)); 2955c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner } else { 296c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // Function does not already exist, simply insert an external function 29718961504fc2b299578dba817900a0696cf3ccc4dChris Lattner // signature identical to SF into the dest module... 29818961504fc2b299578dba817900a0696cf3ccc4dChris Lattner Function *DF = new Function(SF->getFunctionType(), 29918961504fc2b299578dba817900a0696cf3ccc4dChris Lattner SF->hasInternalLinkage(), 30018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner SF->getName()); 3015c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 302c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // Add the function signature to the dest module... 30318961504fc2b299578dba817900a0696cf3ccc4dChris Lattner Dest->getFunctionList().push_back(DF); 3045c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 3055c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // ... and remember this mapping... 30618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner ValueMap.insert(std::make_pair(SF, DF)); 3075c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner } 3085c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner } 3095c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner return false; 3105c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner} 3115c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 312c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// LinkFunctionBody - Copy the source function over into the dest function and 313c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// fix up references to values. At this point we know that Dest is an external 314c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// function, and that Src is not. 3155c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// 31679df7c0aaa18129e55968c8783ef8346807bd4afChris Lattnerstatic bool LinkFunctionBody(Function *Dest, const Function *Src, 31779df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner const map<const Value*, Value*> &GlobalMap, 31879df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner string *Err = 0) { 3195c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner assert(Src && Dest && Dest->isExternal() && !Src->isExternal()); 320c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner map<const Value*, Value*> LocalMap; // Map for function local values 3215c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 322c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // Go through and convert function arguments over... 32318961504fc2b299578dba817900a0696cf3ccc4dChris Lattner for (Function::const_aiterator I = Src->abegin(), E = Src->aend(); 32418961504fc2b299578dba817900a0696cf3ccc4dChris Lattner I != E; ++I) { 325c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // Create the new function argument and add to the dest function... 32618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner Argument *DFA = new Argument(I->getType(), I->getName()); 32718961504fc2b299578dba817900a0696cf3ccc4dChris Lattner Dest->getArgumentList().push_back(DFA); 3285c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 3295c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // Add a mapping to our local map 33018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner LocalMap.insert(std::make_pair(I, DFA)); 3315c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner } 3325c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 3335c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // Loop over all of the basic blocks, copying the instructions over... 3345c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // 33579df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner for (Function::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) { 336c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // Create new basic block and add to mapping and the Dest function... 33718961504fc2b299578dba817900a0696cf3ccc4dChris Lattner BasicBlock *DBB = new BasicBlock(I->getName(), Dest); 33818961504fc2b299578dba817900a0696cf3ccc4dChris Lattner LocalMap.insert(std::make_pair(I, DBB)); 3395c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 3405c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // Loop over all of the instructions in the src basic block, copying them 3415c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // over. Note that this is broken in a strict sense because the cloned 3425c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // instructions will still be referencing values in the Src module, not 3435c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // the remapped values. In our case, however, we will not get caught and 3445c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // so we can delay patching the values up until later... 3455c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // 34618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner for (BasicBlock::const_iterator II = I->begin(), IE = I->end(); 3475c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner II != IE; ++II) { 34818961504fc2b299578dba817900a0696cf3ccc4dChris Lattner Instruction *DI = II->clone(); 34918961504fc2b299578dba817900a0696cf3ccc4dChris Lattner DI->setName(II->getName()); 3505c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner DBB->getInstList().push_back(DI); 35118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner LocalMap.insert(std::make_pair(II, DI)); 3525c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner } 3535c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner } 3545c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 355c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // At this point, all of the instructions and values of the function are now 356c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // copied over. The only problem is that they are still referencing values in 357c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // the Source function as operands. Loop through all of the operands of the 358c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // functions and patch them up to point to the local versions... 3595c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // 36018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB) 36118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) 36218961504fc2b299578dba817900a0696cf3ccc4dChris Lattner for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end(); 363221d688a5ef21a22c2368c9fff0e92d7966c95e5Chris Lattner OI != OE; ++OI) 364221d688a5ef21a22c2368c9fff0e92d7966c95e5Chris Lattner *OI = RemapOperand(*OI, LocalMap, &GlobalMap); 3655c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 3665c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner return false; 3675c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner} 3685c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 3695c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 370c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// LinkFunctionBodies - Link in the function bodies that are defined in the 371c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// source module into the DestModule. This consists basically of copying the 372c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// function over and fixing up references to values. 3735c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// 37479df7c0aaa18129e55968c8783ef8346807bd4afChris Lattnerstatic bool LinkFunctionBodies(Module *Dest, const Module *Src, 37579df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner map<const Value*, Value*> &ValueMap, 37679df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner string *Err = 0) { 3775c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 378c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // Loop over all of the functions in the src module, mapping them over as we 379c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // go 3805c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // 38118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner for (Module::const_iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF){ 38218961504fc2b299578dba817900a0696cf3ccc4dChris Lattner if (!SF->isExternal()) { // No body if function is external 38318961504fc2b299578dba817900a0696cf3ccc4dChris Lattner Function *DF = cast<Function>(ValueMap[SF]); // Destination function 384c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner 38518961504fc2b299578dba817900a0696cf3ccc4dChris Lattner // DF not external SF external? 38618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner if (!DF->isExternal()) { 387c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner if (Err) 38818961504fc2b299578dba817900a0696cf3ccc4dChris Lattner *Err = "Function '" + (SF->hasName() ? SF->getName() : string("")) + 389c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner "' body multiply defined!"; 390c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner return true; 391c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner } 3925c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 39318961504fc2b299578dba817900a0696cf3ccc4dChris Lattner if (LinkFunctionBody(DF, SF, ValueMap, Err)) return true; 394c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner } 3955c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner } 3965c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner return false; 3975c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner} 3985c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 39952f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner 40052f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner 40152f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// LinkModules - This function links two modules together, with the resulting 40252f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// left module modified to be the composite of the two input modules. If an 40352f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// error occurs, true is returned and ErrorMsg (if not null) is set to indicate 4045c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// the problem. Upon failure, the Dest module could be in a modified state, and 4055c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// shouldn't be relied on to be consistent. 40652f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// 407b1443b17491163db6a207959d10e6d6d83cd28fdChris Lattnerbool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg) { 4082c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner 4092c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner // LinkTypes - Go through the symbol table of the Src module and see if any 4102c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner // types are named in the src module that are not named in the Dst module. 4112c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner // Make sure there are no type name conflicts. 4122c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner // 4132c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner if (LinkTypes(Dest, Src, ErrorMsg)) return true; 4142c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner 4155c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // ValueMap - Mapping of values from what they used to be in Src, to what they 4165c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // are now in Dest. 4175c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // 4185c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner map<const Value*, Value*> ValueMap; 4195c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 4208d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner // Insert all of the globals in src into the Dest module... without 4218d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner // initializers 4225c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner if (LinkGlobals(Dest, Src, ValueMap, ErrorMsg)) return true; 4235c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 424c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // Link the functions together between the two modules, without doing function 425c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // bodies... this just adds external function prototypes to the Dest 426c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // function... We do this so that when we begin processing function bodies, 427c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // all of the global values that may be referenced are available in our 428c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // ValueMap. 4295c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // 43079df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg)) return true; 4315c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner 4326cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner // Update the initializers in the Dest module now that all globals that may 4336cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner // be referenced are in Dest. 4346cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner // 4356cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true; 4366cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner 437c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // Link in the function bodies that are defined in the source module into the 438c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // DestModule. This consists basically of copying the function over and 439c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner // fixing up references to values. 4405c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner // 44179df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true; 44252f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner 44352f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner return false; 44452f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner} 4459466f5113ba859677a28887c3c7143065e702019Vikram S. Adve 446