LinkModules.cpp revision 4ad02e726d9b634372b037d4b352d8b63bb9e849
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"
185c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
195c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// Error - Simple wrapper function to conditionally assign to E and return true.
205c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// This just makes error return conditions a little bit simpler...
215c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
225c2d335d86579941b71816196a95a64b277e8963Chris Lattnerstatic inline bool Error(std::string *E, std::string Message) {
235c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  if (E) *E = Message;
245c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return true;
255c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
265c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
272c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner// LinkTypes - Go through the symbol table of the Src module and see if any
282c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner// types are named in the src module that are not named in the Dst module.
292c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner// Make sure there are no type name conflicts.
302c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner//
315c2d335d86579941b71816196a95a64b277e8963Chris Lattnerstatic bool LinkTypes(Module *Dest, const Module *Src, std::string *Err) {
326e6026b46569b01f8f6d4dcdb6c899c3a9c76b3eChris Lattner  SymbolTable       *DestST = &Dest->getSymbolTable();
336e6026b46569b01f8f6d4dcdb6c899c3a9c76b3eChris Lattner  const SymbolTable *SrcST  = &Src->getSymbolTable();
342c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
352c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // Look for a type plane for Type's...
362c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  SymbolTable::const_iterator PI = SrcST->find(Type::TypeTy);
372c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  if (PI == SrcST->end()) return false;  // No named types, do nothing.
382c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
392c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  const SymbolTable::VarMap &VM = PI->second;
402c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  for (SymbolTable::type_const_iterator I = VM.begin(), E = VM.end();
412c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner       I != E; ++I) {
425c2d335d86579941b71816196a95a64b277e8963Chris Lattner    const std::string &Name = I->first;
432c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    const Type *RHS = cast<Type>(I->second);
442c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
452c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    // Check to see if this type name is already in the dest module...
462c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    const Type *Entry = cast_or_null<Type>(DestST->lookup(Type::TypeTy, Name));
475c2d335d86579941b71816196a95a64b277e8963Chris Lattner    if (Entry && !isa<OpaqueType>(Entry)) {  // Yup, the value already exists...
482f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner      if (Entry != RHS) {
492f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner        if (OpaqueType *OT = dyn_cast<OpaqueType>(const_cast<Type*>(RHS))) {
502f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner          OT->refineAbstractTypeTo(Entry);
512f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner        } else {
522f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner          // If it's the same, noop.  Otherwise, error.
532f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner          return Error(Err, "Type named '" + Name +
542f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner                       "' of different shape in modules.\n  Src='" +
552f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner                       Entry->getDescription() + "'.\n  Dst='" +
562f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner                       RHS->getDescription() + "'");
572f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner        }
582f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner      }
592c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    } else {                       // Type not in dest module.  Add it now.
602f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner      if (Entry) {
612f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner        OpaqueType *OT = cast<OpaqueType>(const_cast<Type*>(Entry));
622f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner        OT->refineAbstractTypeTo(RHS);
632f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner      }
642f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner
652c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner      // TODO: FIXME WHEN TYPES AREN'T CONST
662c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner      DestST->insert(Name, const_cast<Type*>(RHS));
672c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    }
682c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  }
692c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  return false;
702c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner}
712c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
725c2d335d86579941b71816196a95a64b277e8963Chris Lattnerstatic void PrintMap(const std::map<const Value*, Value*> &M) {
735c2d335d86579941b71816196a95a64b277e8963Chris Lattner  for (std::map<const Value*, Value*>::const_iterator I = M.begin(), E =M.end();
742d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner       I != E; ++I) {
755c2d335d86579941b71816196a95a64b277e8963Chris Lattner    std::cerr << " Fr: " << (void*)I->first << " ";
7687182ae6baf28ca6da6ee4bc00cf1857f293acfeChris Lattner    I->first->dump();
775c2d335d86579941b71816196a95a64b277e8963Chris Lattner    std::cerr << " To: " << (void*)I->second << " ";
7887182ae6baf28ca6da6ee4bc00cf1857f293acfeChris Lattner    I->second->dump();
795c2d335d86579941b71816196a95a64b277e8963Chris Lattner    std::cerr << "\n";
802d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  }
812d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner}
822d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
832d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
845c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// RemapOperand - Use LocalMap and GlobalMap to convert references from one
855c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// module to another.  This is somewhat sophisticated in that it can
865c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// automatically handle constant references correctly as well...
875c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
885c2d335d86579941b71816196a95a64b277e8963Chris Lattnerstatic Value *RemapOperand(const Value *In,
895c2d335d86579941b71816196a95a64b277e8963Chris Lattner                           std::map<const Value*, Value*> &LocalMap,
905c2d335d86579941b71816196a95a64b277e8963Chris Lattner                           std::map<const Value*, Value*> *GlobalMap) {
915c2d335d86579941b71816196a95a64b277e8963Chris Lattner  std::map<const Value*,Value*>::const_iterator I = LocalMap.find(In);
925c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  if (I != LocalMap.end()) return I->second;
935c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
945c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  if (GlobalMap) {
955c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    I = GlobalMap->find(In);
965c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    if (I != GlobalMap->end()) return I->second;
975c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
985c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
998d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  // Check to see if it's a constant that we are interesting in transforming...
10018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  if (const Constant *CPV = dyn_cast<Constant>(In)) {
1016cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner    if (!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV))
10218961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      return const_cast<Constant*>(CPV);   // Simple constants stay identical...
1038d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
104e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    Constant *Result = 0;
1058d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
10618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
107697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner      const std::vector<Use> &Ops = CPA->getValues();
108697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner      std::vector<Constant*> Operands(Ops.size());
109e306d94782b1c608857738279852dcc050c66004Chris Lattner      for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1108d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner        Operands[i] =
111e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner          cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
112e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner      Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands);
11318961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
114697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner      const std::vector<Use> &Ops = CPS->getValues();
115697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner      std::vector<Constant*> Operands(Ops.size());
1168d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      for (unsigned i = 0; i < Ops.size(); ++i)
1178d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner        Operands[i] =
118e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner          cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
119e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner      Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands);
120e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    } else if (isa<ConstantPointerNull>(CPV)) {
12118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      Result = const_cast<Constant*>(CPV);
12218961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    } else if (const ConstantPointerRef *CPR =
12318961504fc2b299578dba817900a0696cf3ccc4dChris Lattner                      dyn_cast<ConstantPointerRef>(CPV)) {
1248d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap);
125e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner      Result = ConstantPointerRef::get(cast<GlobalValue>(V));
1266cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner    } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
127b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner      if (CE->getOpcode() == Instruction::GetElementPtr) {
128b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        Value *Ptr = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
129b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        std::vector<Constant*> Indices;
130b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        Indices.reserve(CE->getNumOperands()-1);
131b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
132b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner          Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),
133b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner                                                        LocalMap, GlobalMap)));
134b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner
135b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        Result = ConstantExpr::getGetElementPtr(cast<Constant>(Ptr), Indices);
136b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner      } else if (CE->getNumOperands() == 1) {
137ad333484ea5ae976b83e35ccd3f6cfa6e71290e2Chris Lattner        // Cast instruction
138ad333484ea5ae976b83e35ccd3f6cfa6e71290e2Chris Lattner        assert(CE->getOpcode() == Instruction::Cast);
1396cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner        Value *V = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
140ad333484ea5ae976b83e35ccd3f6cfa6e71290e2Chris Lattner        Result = ConstantExpr::getCast(cast<Constant>(V), CE->getType());
1416cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner      } else if (CE->getNumOperands() == 2) {
1426cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner        // Binary operator...
1436cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner        Value *V1 = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
1446cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner        Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap);
1456cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner
1466cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner        Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V1),
147e8e4605021141d689493132a9c7c6fce6294937fChris Lattner                                   cast<Constant>(V2));
1486cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner      } else {
149b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        assert(0 && "Unknown constant expr type!");
1506cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner      }
1516cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner
1528d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    } else {
1538d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      assert(0 && "Unknown type of derived type constant value!");
1548d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    }
1558d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
1568d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    // Cache the mapping in our local map structure...
157d149c053cdbb406b7b5dff24127b4c509420893eChris Lattner    if (GlobalMap)
158d149c053cdbb406b7b5dff24127b4c509420893eChris Lattner      GlobalMap->insert(std::make_pair(In, Result));
159d149c053cdbb406b7b5dff24127b4c509420893eChris Lattner    else
160d149c053cdbb406b7b5dff24127b4c509420893eChris Lattner      LocalMap.insert(std::make_pair(In, Result));
1618d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    return Result;
1628d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  }
1632d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
1645c2d335d86579941b71816196a95a64b277e8963Chris Lattner  std::cerr << "XXX LocalMap: \n";
1652d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  PrintMap(LocalMap);
1662d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
1672d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  if (GlobalMap) {
1685c2d335d86579941b71816196a95a64b277e8963Chris Lattner    std::cerr << "XXX GlobalMap: \n";
1692d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner    PrintMap(*GlobalMap);
1702d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  }
1712d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
1725c2d335d86579941b71816196a95a64b277e8963Chris Lattner  std::cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n";
1738d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  assert(0 && "Couldn't remap value!");
1748d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  return 0;
1755c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
1765c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
1775c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
1785c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// LinkGlobals - Loop through the global variables in the src module and merge
1795c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// them into the dest module...
1805c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
1815c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattnerstatic bool LinkGlobals(Module *Dest, const Module *Src,
1825c2d335d86579941b71816196a95a64b277e8963Chris Lattner                        std::map<const Value*, Value*> &ValueMap,
1835c2d335d86579941b71816196a95a64b277e8963Chris Lattner                        std::string *Err) {
1845c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // We will need a module level symbol table if the src module has a module
1855c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // level symbol table...
186b91b657e026e86082b665cc875c2881ed075b68dChris Lattner  SymbolTable *ST = (SymbolTable*)&Dest->getSymbolTable();
1875c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
1885c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // Loop over all of the globals in the src module, mapping them over as we go
1895c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
1905c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
19118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    const GlobalVariable *SGV = I;
1924ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    GlobalVariable *DGV = 0;
1934ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    if (SGV->hasName()) {
1944ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      // A same named thing is a global variable, because the only two things
19579df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner      // that may be in a module level symbol table are Global Vars and
19679df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner      // Functions, and they both have distinct, nonoverlapping, possible types.
1975c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      //
1984ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      DGV = cast_or_null<GlobalVariable>(ST->lookup(SGV->getType(),
1994ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner                                                    SGV->getName()));
2004ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    }
2015c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2024ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    assert(SGV->hasInitializer() || SGV->hasExternalLinkage() &&
2034ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner           "Global must either be external or have an initializer!");
2045c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2054ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    if (!DGV || DGV->hasInternalLinkage() || SGV->hasInternalLinkage()) {
2065c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // No linking to be performed, simply create an identical version of the
2078d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      // symbol over in the dest module... the initializer will be filled in
2088d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      // later by LinkGlobalInits...
2098d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      //
2104ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      DGV = new GlobalVariable(SGV->getType()->getElementType(),
2114ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner                               SGV->isConstant(), SGV->getLinkage(), /*init*/0,
2124ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner                               SGV->getName(), Dest);
2135c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2145c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // Make sure to remember this mapping...
215697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner      ValueMap.insert(std::make_pair(SGV, DGV));
2164ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    } else if (SGV->getLinkage() != DGV->getLinkage()) {
2174ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      return Error(Err, "Global variables named '" + SGV->getName() +
2184ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner                   "' have different linkage specifiers!");
2194ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    } else if (SGV->hasExternalLinkage() || SGV->hasLinkOnceLinkage() ||
2204ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner               SGV->hasAppendingLinkage()) {
2214ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      // If the global variable has a name, and that name is already in use in
2224ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      // the Dest module, make sure that the name is a compatible global
2234ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      // variable...
2244ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      //
2254ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      // Check to see if the two GV's have the same Const'ness...
2264ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      if (SGV->isConstant() != DGV->isConstant())
2274ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner        return Error(Err, "Global Variable Collision on '" +
2284ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner                     SGV->getType()->getDescription() + "':%" + SGV->getName() +
2294ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner                     " - Global variables differ in const'ness");
2304ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      // Okay, everything is cool, remember the mapping...
2314ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      ValueMap.insert(std::make_pair(SGV, DGV));
2324ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    } else {
2334ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      assert(0 && "Unknown linkage!");
2345c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    }
2355c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
2365c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return false;
2375c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
2385c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2395c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2408d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner// LinkGlobalInits - Update the initializers in the Dest module now that all
2418d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner// globals that may be referenced are in Dest.
2428d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner//
2438d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattnerstatic bool LinkGlobalInits(Module *Dest, const Module *Src,
2445c2d335d86579941b71816196a95a64b277e8963Chris Lattner                            std::map<const Value*, Value*> &ValueMap,
2455c2d335d86579941b71816196a95a64b277e8963Chris Lattner                            std::string *Err) {
2468d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
2478d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  // Loop over all of the globals in the src module, mapping them over as we go
2488d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  //
2498d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
25018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    const GlobalVariable *SGV = I;
2518d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
2528d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    if (SGV->hasInitializer()) {      // Only process initialized GV's
2538d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      // Figure out what the initializer looks like in the dest module...
2544ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      Constant *SInit =
2552f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner        cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap, 0));
2568d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
2578d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);
2584ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      if (DGV->hasInitializer()) {
2594ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner        assert(SGV->getLinkage() == DGV->getLinkage());
2604ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner        if (SGV->hasExternalLinkage()) {
2614ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner          if (DGV->getInitializer() != SInit)
2624ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner            return Error(Err, "Global Variable Collision on '" +
2634ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner                         SGV->getType()->getDescription() +"':%"+SGV->getName()+
2644ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner                         " - Global variables have different initializers");
2654ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner        } else if (DGV->hasLinkOnceLinkage()) {
2664ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner          // Nothing is required, mapped values will take the new global
2674ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner          // automatically.
2684ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner        } else if (DGV->hasAppendingLinkage()) {
2694ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner          assert(0 && "Appending linkage unimplemented!");
2704ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner        } else {
2714ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner          assert(0 && "Unknown linkage!");
2724ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner        }
2738d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      } else {
2748d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner        // Copy the initializer over now...
2754ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner        DGV->setInitializer(SInit);
2768d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      }
2778d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    }
2788d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  }
2798d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  return false;
2808d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner}
2815c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
28279df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner// LinkFunctionProtos - Link the functions together between the two modules,
283c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// without doing function bodies... this just adds external function prototypes
284c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// to the Dest function...
2855c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
28679df7c0aaa18129e55968c8783ef8346807bd4afChris Lattnerstatic bool LinkFunctionProtos(Module *Dest, const Module *Src,
2875c2d335d86579941b71816196a95a64b277e8963Chris Lattner                               std::map<const Value*, Value*> &ValueMap,
2885c2d335d86579941b71816196a95a64b277e8963Chris Lattner                               std::string *Err) {
289b91b657e026e86082b665cc875c2881ed075b68dChris Lattner  SymbolTable *ST = (SymbolTable*)&Dest->getSymbolTable();
2905c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
291c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // Loop over all of the functions in the src module, mapping them over as we
292c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // go
2935c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
2945c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
29518961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    const Function *SF = I;   // SrcFunction
2964ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    Function *DF = 0;
2974ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    if (SF->hasName())
29879df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner      // The same named thing is a Function, because the only two things
29979df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner      // that may be in a module level symbol table are Global Vars and
30079df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner      // Functions, and they both have distinct, nonoverlapping, possible types.
3015c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      //
3024ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      DF = cast_or_null<Function>(ST->lookup(SF->getType(), SF->getName()));
3035c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3044ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) {
3054ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      // Function does not already exist, simply insert an external function
3064ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      // signature identical to SF into the dest module...
3074ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      Function *DF = new Function(SF->getFunctionType(), SF->getLinkage(),
3084ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner                                  SF->getName(), Dest);
3094ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner
3104ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      // ... and remember this mapping...
3114ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      ValueMap.insert(std::make_pair(SF, DF));
3124ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    } else if (SF->getLinkage() != DF->getLinkage()) {
3134ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      return Error(Err, "Functions named '" + SF->getName() +
3144ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner                   "' have different linkage specifiers!");
3154ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    } else if (SF->getLinkage() == GlobalValue::AppendingLinkage) {
3164ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      return Error(Err, "Functions named '" + SF->getName() +
3174ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner                   "' have appending linkage!");
3184ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    } else if (SF->getLinkage() == GlobalValue::ExternalLinkage) {
3194ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      // If the function has a name, and that name is already in use in the Dest
3204ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      // module, make sure that the name is a compatible function...
3214ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      //
322c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner      // Check to make sure the function is not defined in both modules...
32318961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      if (!SF->isExternal() && !DF->isExternal())
32479df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner        return Error(Err, "Function '" +
32518961504fc2b299578dba817900a0696cf3ccc4dChris Lattner                     SF->getFunctionType()->getDescription() + "':\"" +
32618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner                     SF->getName() + "\" - Function is already defined!");
3274ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner
3285c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // Otherwise, just remember this mapping...
32918961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      ValueMap.insert(std::make_pair(SF, DF));
3304ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    } else if (SF->getLinkage() == GlobalValue::LinkOnceLinkage) {
3314ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      // Completely ignore the source function.
33218961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      ValueMap.insert(std::make_pair(SF, DF));
3335c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    }
3345c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
3355c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return false;
3365c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
3375c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
338c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// LinkFunctionBody - Copy the source function over into the dest function and
339c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// fix up references to values.  At this point we know that Dest is an external
340c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// function, and that Src is not.
3415c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
34279df7c0aaa18129e55968c8783ef8346807bd4afChris Lattnerstatic bool LinkFunctionBody(Function *Dest, const Function *Src,
3435c2d335d86579941b71816196a95a64b277e8963Chris Lattner                             std::map<const Value*, Value*> &GlobalMap,
3445c2d335d86579941b71816196a95a64b277e8963Chris Lattner                             std::string *Err) {
3455c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  assert(Src && Dest && Dest->isExternal() && !Src->isExternal());
3465c2d335d86579941b71816196a95a64b277e8963Chris Lattner  std::map<const Value*, Value*> LocalMap;   // Map for function local values
3475c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
348c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // Go through and convert function arguments over...
34969da5cf26143e4542d4bf8c78ffac6d079efe5c9Chris Lattner  Function::aiterator DI = Dest->abegin();
35018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  for (Function::const_aiterator I = Src->abegin(), E = Src->aend();
35169da5cf26143e4542d4bf8c78ffac6d079efe5c9Chris Lattner       I != E; ++I, ++DI) {
35269da5cf26143e4542d4bf8c78ffac6d079efe5c9Chris Lattner    DI->setName(I->getName());  // Copy the name information over...
3535c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3545c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // Add a mapping to our local map
35569da5cf26143e4542d4bf8c78ffac6d079efe5c9Chris Lattner    LocalMap.insert(std::make_pair(I, DI));
3565c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
3575c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3585c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // Loop over all of the basic blocks, copying the instructions over...
3595c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
36079df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner  for (Function::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
361c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner    // Create new basic block and add to mapping and the Dest function...
36218961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    BasicBlock *DBB = new BasicBlock(I->getName(), Dest);
36318961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    LocalMap.insert(std::make_pair(I, DBB));
3645c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3655c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // Loop over all of the instructions in the src basic block, copying them
3665c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // over.  Note that this is broken in a strict sense because the cloned
3675c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // instructions will still be referencing values in the Src module, not
3685c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // the remapped values.  In our case, however, we will not get caught and
3695c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // so we can delay patching the values up until later...
3705c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    //
37118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    for (BasicBlock::const_iterator II = I->begin(), IE = I->end();
3725c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner         II != IE; ++II) {
37318961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      Instruction *DI = II->clone();
37418961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      DI->setName(II->getName());
3755c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      DBB->getInstList().push_back(DI);
37618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      LocalMap.insert(std::make_pair(II, DI));
3775c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    }
3785c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
3795c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
380c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // At this point, all of the instructions and values of the function are now
381c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // copied over.  The only problem is that they are still referencing values in
382c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // the Source function as operands.  Loop through all of the operands of the
383c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // functions and patch them up to point to the local versions...
3845c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
38518961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB)
38618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
38718961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
388221d688a5ef21a22c2368c9fff0e92d7966c95e5Chris Lattner           OI != OE; ++OI)
389221d688a5ef21a22c2368c9fff0e92d7966c95e5Chris Lattner        *OI = RemapOperand(*OI, LocalMap, &GlobalMap);
3905c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3915c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return false;
3925c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
3935c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3945c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
395c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// LinkFunctionBodies - Link in the function bodies that are defined in the
396c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// source module into the DestModule.  This consists basically of copying the
397c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// function over and fixing up references to values.
3985c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
39979df7c0aaa18129e55968c8783ef8346807bd4afChris Lattnerstatic bool LinkFunctionBodies(Module *Dest, const Module *Src,
4005c2d335d86579941b71816196a95a64b277e8963Chris Lattner                               std::map<const Value*, Value*> &ValueMap,
4015c2d335d86579941b71816196a95a64b277e8963Chris Lattner                               std::string *Err) {
4025c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
403c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // Loop over all of the functions in the src module, mapping them over as we
404c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // go
4055c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
40618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  for (Module::const_iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF){
40718961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    if (!SF->isExternal()) {                  // No body if function is external
40818961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      Function *DF = cast<Function>(ValueMap[SF]); // Destination function
409c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner
41018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      // DF not external SF external?
41118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      if (!DF->isExternal()) {
4124ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner        if (DF->hasLinkOnceLinkage()) continue; // No relinkage for link-once!
413c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner        if (Err)
4145c2d335d86579941b71816196a95a64b277e8963Chris Lattner          *Err = "Function '" + (SF->hasName() ? SF->getName() :std::string(""))
4155c2d335d86579941b71816196a95a64b277e8963Chris Lattner               + "' body multiply defined!";
416c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner        return true;
417c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner      }
4185c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
41918961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      if (LinkFunctionBody(DF, SF, ValueMap, Err)) return true;
420c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner    }
4215c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
4225c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return false;
4235c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
4245c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
42552f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner
42652f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner
42752f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// LinkModules - This function links two modules together, with the resulting
42852f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// left module modified to be the composite of the two input modules.  If an
42952f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// error occurs, true is returned and ErrorMsg (if not null) is set to indicate
4305c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// the problem.  Upon failure, the Dest module could be in a modified state, and
4315c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// shouldn't be relied on to be consistent.
43252f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//
4335c2d335d86579941b71816196a95a64b277e8963Chris Lattnerbool LinkModules(Module *Dest, const Module *Src, std::string *ErrorMsg) {
4342c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
4352c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // LinkTypes - Go through the symbol table of the Src module and see if any
4362c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // types are named in the src module that are not named in the Dst module.
4372c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // Make sure there are no type name conflicts.
4382c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  //
4392c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  if (LinkTypes(Dest, Src, ErrorMsg)) return true;
4402c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
4415c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // ValueMap - Mapping of values from what they used to be in Src, to what they
4425c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // are now in Dest.
4435c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
4445c2d335d86579941b71816196a95a64b277e8963Chris Lattner  std::map<const Value*, Value*> ValueMap;
4455c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
4468d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  // Insert all of the globals in src into the Dest module... without
4478d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  // initializers
4485c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  if (LinkGlobals(Dest, Src, ValueMap, ErrorMsg)) return true;
4495c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
450c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // Link the functions together between the two modules, without doing function
451c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // bodies... this just adds external function prototypes to the Dest
452c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // function...  We do this so that when we begin processing function bodies,
453c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // all of the global values that may be referenced are available in our
454c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // ValueMap.
4555c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
45679df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner  if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg)) return true;
4575c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
4586cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner  // Update the initializers in the Dest module now that all globals that may
4596cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner  // be referenced are in Dest.
4606cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner  //
4616cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner  if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
4626cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner
463c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // Link in the function bodies that are defined in the source module into the
464c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // DestModule.  This consists basically of copying the function over and
465c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // fixing up references to values.
4665c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
46779df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner  if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true;
46852f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner
46952f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner  return false;
47052f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner}
4719466f5113ba859677a28887c3c7143065e702019Vikram S. Adve
472