#include "llvm/Transforms/Utils/Linker.h"
#include "llvm/Module.h"
-#include "llvm/Function.h"
-#include "llvm/BasicBlock.h"
-#include "llvm/GlobalVariable.h"
#include "llvm/SymbolTable.h"
#include "llvm/DerivedTypes.h"
#include "llvm/iOther.h"
#include "llvm/Constants.h"
-#include "llvm/Argument.h"
-#include <iostream>
using std::cerr;
using std::string;
using std::map;
// automatically handle constant references correctly as well...
//
static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
- const map<const Value*, Value*> *GlobalMap = 0) {
+ map<const Value*, Value*> *GlobalMap = 0) {
map<const Value*,Value*>::const_iterator I = LocalMap.find(In);
if (I != LocalMap.end()) return I->second;
if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
const std::vector<Use> &Ops = CPA->getValues();
std::vector<Constant*> Operands(Ops.size());
- for (unsigned i = 0; i < Ops.size(); ++i)
+ for (unsigned i = 0, e = Ops.size(); i != e; ++i)
Operands[i] =
cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands);
Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap);
Result = ConstantPointerRef::get(cast<GlobalValue>(V));
} else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
- if (CE->getNumOperands() == 1) {
- // Cast instruction, unary operator
+ if (CE->getOpcode() == Instruction::GetElementPtr) {
+ Value *Ptr = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
+ std::vector<Constant*> Indices;
+ Indices.reserve(CE->getNumOperands()-1);
+ for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
+ Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),
+ LocalMap, GlobalMap)));
+
+ Result = ConstantExpr::getGetElementPtr(cast<Constant>(Ptr), Indices);
+ } else if (CE->getNumOperands() == 1) {
+ // Cast instruction
+ assert(CE->getOpcode() == Instruction::Cast);
Value *V = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
- Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V),
- CE->getType());
+ Result = ConstantExpr::getCast(cast<Constant>(V), CE->getType());
} else if (CE->getNumOperands() == 2) {
// Binary operator...
Value *V1 = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap);
Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V1),
- cast<Constant>(V2), CE->getType());
+ cast<Constant>(V2));
} else {
- // GetElementPtr Expression
- assert(CE->getOpcode() == Instruction::GetElementPtr);
- Value *Ptr = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
- std::vector<Constant*> Indices;
- Indices.reserve(CE->getNumOperands()-1);
- for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
- Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),
- LocalMap, GlobalMap)));
-
- Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(Ptr),
- Indices, CE->getType());
+ assert(0 && "Unknown constant expr type!");
}
} else {
}
// Cache the mapping in our local map structure...
- LocalMap.insert(std::make_pair(In, const_cast<Constant*>(CPV)));
+ if (GlobalMap)
+ GlobalMap->insert(std::make_pair(In, Result));
+ else
+ LocalMap.insert(std::make_pair(In, Result));
return Result;
}
// function, and that Src is not.
//
static bool LinkFunctionBody(Function *Dest, const Function *Src,
- const map<const Value*, Value*> &GlobalMap,
+ map<const Value*, Value*> &GlobalMap,
string *Err = 0) {
assert(Src && Dest && Dest->isExternal() && !Src->isExternal());
map<const Value*, Value*> LocalMap; // Map for function local values
// Go through and convert function arguments over...
+ Function::aiterator DI = Dest->abegin();
for (Function::const_aiterator I = Src->abegin(), E = Src->aend();
- I != E; ++I) {
- // Create the new function argument and add to the dest function...
- Argument *DFA = new Argument(I->getType(), I->getName());
- Dest->getArgumentList().push_back(DFA);
+ I != E; ++I, ++DI) {
+ DI->setName(I->getName()); // Copy the name information over...
// Add a mapping to our local map
- LocalMap.insert(std::make_pair(I, DFA));
+ LocalMap.insert(std::make_pair(I, DI));
}
// Loop over all of the basic blocks, copying the instructions over...
// the problem. Upon failure, the Dest module could be in a modified state, and
// shouldn't be relied on to be consistent.
//
-bool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg = 0) {
+bool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg) {
// LinkTypes - Go through the symbol table of the Src module and see if any
// types are named in the src module that are not named in the Dst module.
//
map<const Value*, Value*> ValueMap;
- // FIXME:
- // FIXME: This should be a two step process:
- // FIXME: 1. LinkGlobals & LinkFunctionProtos
- // FIXME: 2. LinkGlobalContents
- // FIXME:
- // FIXME: Global variables and functions are the same!
- // FIXME:
-
-
// Insert all of the globals in src into the Dest module... without
// initializers
if (LinkGlobals(Dest, Src, ValueMap, ErrorMsg)) return true;