/// function is automatically inserted into the end of the function list for
/// the module.
///
- Function(const FunctionType *Ty, bool isInternal, const std::string &N = "",
- Module *M = 0);
+ Function(const FunctionType *Ty, LinkageTypes Linkage,
+ const std::string &N = "", Module *M = 0);
~Function();
// Specialize setName to handle symbol table majik...
class GlobalValue : public User {
GlobalValue(const GlobalValue &); // do not implement
+public:
+ enum LinkageTypes {
+ ExternalLinkage, // Externally visible function
+ LinkOnceLinkage, // Keep one copy of named function when linking (inline)
+ AppendingLinkage, // Special purpose, only applies to global arrays
+ InternalLinkage // Rename collisions when linking (static functions)
+ };
protected:
- GlobalValue(const Type *Ty, ValueTy vty, bool hasInternalLinkage,
+ GlobalValue(const Type *Ty, ValueTy vty, LinkageTypes linkage,
const std::string &name = "")
- : User(Ty, vty, name), HasInternalLinkage(hasInternalLinkage), Parent(0) {}
+ : User(Ty, vty, name), Linkage(linkage), Parent(0) {}
- bool HasInternalLinkage; // Is this value accessable externally?
+ LinkageTypes Linkage; // The linkage of this global
Module *Parent;
public:
~GlobalValue() {}
return (const PointerType*)User::getType();
}
- /// Internal Linkage - True if the global value is inaccessible to
- bool hasInternalLinkage() const { return HasInternalLinkage; }
- bool hasExternalLinkage() const { return !HasInternalLinkage; }
- void setInternalLinkage(bool HIL) { HasInternalLinkage = HIL; }
+ bool hasExternalLinkage() const { return Linkage == ExternalLinkage; }
+ bool hasLinkOnceLinkage() const { return Linkage == LinkOnceLinkage; }
+ bool hasAppendingLinkage() const { return Linkage == AppendingLinkage; }
+ bool hasInternalLinkage() const { return Linkage == InternalLinkage; }
+ void setLinkage(LinkageTypes LT) { Linkage = LT; }
+ LinkageTypes getLinkage() const { return Linkage; }
/// isExternal - Return true if the primary definition of this global value is
/// outside of the current translation unit...
/// GlobalVariable ctor - If a parent module is specified, the global is
/// automatically inserted into the end of the specified modules global list.
///
- GlobalVariable(const Type *Ty, bool isConstant, bool isInternal,
+ GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
Constant *Initializer = 0, const std::string &Name = "",
Module *Parent = 0);
DummySyncFunc = new Function(FunctionType::get( Type::VoidTy,
std::vector<const Type*>(),
/*isVararg*/ false),
- /*isInternal*/ false, DummySyncFuncName, &M);
+ GlobalValue::ExternalLinkage, DummySyncFuncName,
+ &M);
}
void Cilkifier::TransformFunc(Function* F,
constant { return CONSTANT; }
const { return CONST; }
internal { return INTERNAL; }
+linkonce { return LINKONCE; }
+appending { return APPENDING; }
uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
external { return EXTERNAL; }
implementation { return IMPLEMENTATION; }
struct MethPlaceHolderHelper : public Function {
MethPlaceHolderHelper(const Type *Ty)
- : Function(cast<FunctionType>(Ty), true) {}
+ : Function(cast<FunctionType>(Ty), InternalLinkage) {}
};
typedef PlaceholderValue<InstPlaceHolderHelper> ValuePlaceHolder;
EGV->setInitializer(GV->getInitializer());
if (GV->isConstant())
EGV->setConstant(true);
- if (GV->hasInternalLinkage())
- EGV->setInternalLinkage(true);
+ EGV->setLinkage(GV->getLinkage());
delete GV; // Destroy the duplicate!
return true; // They are equivalent!
std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
std::vector<Constant*> *ConstVector;
+ GlobalValue::LinkageTypes Linkage;
int64_t SInt64Val;
uint64_t UInt64Val;
int SIntVal;
%type <ValueList> IndexList // For GEP derived indices
%type <TypeList> TypeListI ArgTypeListI
%type <JumpTable> JumpTable
-%type <BoolVal> GlobalType OptInternal // GLOBAL or CONSTANT? Intern?
+%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
+%type <Linkage> OptLinkage
// ValueRef - Unresolved reference to a definition or BB
%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
%token IMPLEMENTATION TRUE FALSE BEGINTOK ENDTOK DECLARE GLOBAL CONSTANT
-%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL OPAQUE NOT EXTERNAL
+%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE APPENDING
+%token OPAQUE NOT EXTERNAL
// Basic Block Terminating Operators
%token <TermOpVal> RET BR SWITCH
$$ = 0;
};
-OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; };
+OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
+ LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
+ APPENDING { $$ = GlobalValue::AppendingLinkage; } |
+ /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
//===----------------------------------------------------------------------===//
// Types includes all predefined types... except void, because it can only be
// Create a placeholder for the global variable reference...
GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
- false, true);
+ false,
+ GlobalValue::ExternalLinkage);
// Keep track of the fact that we have a forward ref to recycle it
CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
}
| ConstPool FunctionProto { // Function prototypes can be in const pool
}
- | ConstPool OptAssign OptInternal GlobalType ConstVal {
+ | ConstPool OptAssign OptLinkage GlobalType ConstVal {
const Type *Ty = $5->getType();
// Global declarations appear in Constant Pool
Constant *Initializer = $5;
| ConstPool OptAssign EXTERNAL GlobalType Types {
const Type *Ty = *$5;
// Global declarations appear in Constant Pool
- GlobalVariable *GV = new GlobalVariable(Ty, $4, false);
+ GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
if (!setValueName(GV, $2)) { // If not redefining...
CurModule.CurrentModule->getGlobalList().push_back(GV);
int Slot = InsertValue(GV, CurModule.Values);
AI->setName("");
} else { // Not already defined?
- Fn = new Function(FT, false, FunctionName);
+ Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName);
InsertValue(Fn, CurModule.Values);
CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
}
BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
-FunctionHeader : OptInternal FunctionHeaderH BEGIN {
+FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
$$ = CurMeth.CurrentFunction;
- // Make sure that we keep track of the internal marker, even if there was
- // a previous "declare".
- if ($1)
- $$->setInternalLinkage(true);
+ // Make sure that we keep track of the linkage type even if there was a
+ // previous "declare".
+ $$->setLinkage($1);
// Resolve circular types before we parse the body of the function.
ResolveTypes(CurMeth.LateResolveTypes);
// Create a placeholder for the global variable reference...
GlobalVariable *GVar =
- new GlobalVariable(PT->getElementType(), false, true);
+ new GlobalVariable(PT->getElementType(), false,
+ GlobalValue::InternalLinkage);
// Keep track of the fact that we have a forward ref to recycle it
GlobalRefs.insert(std::make_pair(std::make_pair(PT, Slot), GVar));
Function *F = FunctionSignatureList.back().first;
unsigned FunctionSlot = FunctionSignatureList.back().second;
FunctionSignatureList.pop_back();
- F->setInternalLinkage(isInternal != 0);
+ F->setLinkage(isInternal ? GlobalValue::InternalLinkage :
+ GlobalValue::ExternalLinkage);
const FunctionType::ParamTypes &Params =F->getFunctionType()->getParamTypes();
Function::aiterator AI = F->abegin();
const Type *ElTy = cast<PointerType>(Ty)->getElementType();
+
+ GlobalValue::LinkageTypes Linkage =
+ (VarType & 4) ? GlobalValue::InternalLinkage :
+ GlobalValue::ExternalLinkage;
+
// Create the global variable...
- GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
+ GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Linkage,
0, "", TheModule);
int DestSlot = insertValue(GV, ModuleValues);
if (DestSlot == -1) return true;
// this placeholder is replaced.
// Insert the placeholder...
- Function *Func = new Function(cast<FunctionType>(Ty), false, "", TheModule);
+ Function *Func = new Function(cast<FunctionType>(Ty),
+ GlobalValue::InternalLinkage, "", TheModule);
int DestSlot = insertValue(Func, ModuleValues);
if (DestSlot == -1) return true;
ResolveReferencesToValue(Func, (unsigned)DestSlot);
//
for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
const GlobalVariable *SGV = I;
- Value *V;
-
- // If the global variable has a name, and that name is already in use in the
- // Dest module, make sure that the name is a compatible global variable...
- //
- if (SGV->hasExternalLinkage() && SGV->hasName() &&
- (V = ST->lookup(SGV->getType(), SGV->getName())) &&
- cast<GlobalVariable>(V)->hasExternalLinkage()) {
- // The same named thing is a global variable, because the only two things
+ GlobalVariable *DGV = 0;
+ if (SGV->hasName()) {
+ // A same named thing is a global variable, because the only two things
// that may be in a module level symbol table are Global Vars and
// Functions, and they both have distinct, nonoverlapping, possible types.
//
- GlobalVariable *DGV = cast<GlobalVariable>(V);
+ DGV = cast_or_null<GlobalVariable>(ST->lookup(SGV->getType(),
+ SGV->getName()));
+ }
- // Check to see if the two GV's have the same Const'ness...
- if (SGV->isConstant() != DGV->isConstant())
- return Error(Err, "Global Variable Collision on '" +
- SGV->getType()->getDescription() + "':%" + SGV->getName() +
- " - Global variables differ in const'ness");
+ assert(SGV->hasInitializer() || SGV->hasExternalLinkage() &&
+ "Global must either be external or have an initializer!");
- // Okay, everything is cool, remember the mapping...
- ValueMap.insert(std::make_pair(SGV, DGV));
- } else {
+ if (!DGV || DGV->hasInternalLinkage() || SGV->hasInternalLinkage()) {
// No linking to be performed, simply create an identical version of the
// symbol over in the dest module... the initializer will be filled in
// later by LinkGlobalInits...
//
- GlobalVariable *DGV =
- new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(),
- SGV->hasInternalLinkage(), 0, SGV->getName());
-
- // Add the new global to the dest module
- Dest->getGlobalList().push_back(DGV);
+ DGV = new GlobalVariable(SGV->getType()->getElementType(),
+ SGV->isConstant(), SGV->getLinkage(), /*init*/0,
+ SGV->getName(), Dest);
// Make sure to remember this mapping...
ValueMap.insert(std::make_pair(SGV, DGV));
+ } else if (SGV->getLinkage() != DGV->getLinkage()) {
+ return Error(Err, "Global variables named '" + SGV->getName() +
+ "' have different linkage specifiers!");
+ } else if (SGV->hasExternalLinkage() || SGV->hasLinkOnceLinkage() ||
+ SGV->hasAppendingLinkage()) {
+ // If the global variable has a name, and that name is already in use in
+ // the Dest module, make sure that the name is a compatible global
+ // variable...
+ //
+ // Check to see if the two GV's have the same Const'ness...
+ if (SGV->isConstant() != DGV->isConstant())
+ return Error(Err, "Global Variable Collision on '" +
+ SGV->getType()->getDescription() + "':%" + SGV->getName() +
+ " - Global variables differ in const'ness");
+ // Okay, everything is cool, remember the mapping...
+ ValueMap.insert(std::make_pair(SGV, DGV));
+ } else {
+ assert(0 && "Unknown linkage!");
}
}
return false;
if (SGV->hasInitializer()) { // Only process initialized GV's
// Figure out what the initializer looks like in the dest module...
- Constant *DInit =
+ Constant *SInit =
cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap, 0));
GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);
- if (DGV->hasInitializer() && SGV->hasExternalLinkage() &&
- DGV->hasExternalLinkage()) {
- if (DGV->getInitializer() != DInit)
- return Error(Err, "Global Variable Collision on '" +
- SGV->getType()->getDescription() + "':%" +SGV->getName()+
- " - Global variables have different initializers");
+ if (DGV->hasInitializer()) {
+ assert(SGV->getLinkage() == DGV->getLinkage());
+ if (SGV->hasExternalLinkage()) {
+ if (DGV->getInitializer() != SInit)
+ return Error(Err, "Global Variable Collision on '" +
+ SGV->getType()->getDescription() +"':%"+SGV->getName()+
+ " - Global variables have different initializers");
+ } else if (DGV->hasLinkOnceLinkage()) {
+ // Nothing is required, mapped values will take the new global
+ // automatically.
+ } else if (DGV->hasAppendingLinkage()) {
+ assert(0 && "Appending linkage unimplemented!");
+ } else {
+ assert(0 && "Unknown linkage!");
+ }
} else {
// Copy the initializer over now...
- DGV->setInitializer(DInit);
+ DGV->setInitializer(SInit);
}
}
}
//
for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
const Function *SF = I; // SrcFunction
- Value *V;
-
- // If the function has a name, and that name is already in use in the Dest
- // module, make sure that the name is a compatible function...
- //
- if (SF->hasExternalLinkage() && SF->hasName() &&
- (V = ST->lookup(SF->getType(), SF->getName())) &&
- cast<Function>(V)->hasExternalLinkage()) {
+ Function *DF = 0;
+ if (SF->hasName())
// The same named thing is a Function, because the only two things
// that may be in a module level symbol table are Global Vars and
// Functions, and they both have distinct, nonoverlapping, possible types.
//
- Function *DF = cast<Function>(V); // DestFunction
+ DF = cast_or_null<Function>(ST->lookup(SF->getType(), SF->getName()));
+ if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) {
+ // Function does not already exist, simply insert an external function
+ // signature identical to SF into the dest module...
+ Function *DF = new Function(SF->getFunctionType(), SF->getLinkage(),
+ SF->getName(), Dest);
+
+ // ... and remember this mapping...
+ ValueMap.insert(std::make_pair(SF, DF));
+ } else if (SF->getLinkage() != DF->getLinkage()) {
+ return Error(Err, "Functions named '" + SF->getName() +
+ "' have different linkage specifiers!");
+ } else if (SF->getLinkage() == GlobalValue::AppendingLinkage) {
+ return Error(Err, "Functions named '" + SF->getName() +
+ "' have appending linkage!");
+ } else if (SF->getLinkage() == GlobalValue::ExternalLinkage) {
+ // If the function has a name, and that name is already in use in the Dest
+ // module, make sure that the name is a compatible function...
+ //
// Check to make sure the function is not defined in both modules...
if (!SF->isExternal() && !DF->isExternal())
return Error(Err, "Function '" +
SF->getFunctionType()->getDescription() + "':\"" +
SF->getName() + "\" - Function is already defined!");
-
+
// Otherwise, just remember this mapping...
ValueMap.insert(std::make_pair(SF, DF));
- } else {
- // Function does not already exist, simply insert an external function
- // signature identical to SF into the dest module...
- Function *DF = new Function(SF->getFunctionType(),
- SF->hasInternalLinkage(),
- SF->getName());
-
- // Add the function signature to the dest module...
- Dest->getFunctionList().push_back(DF);
-
- // ... and remember this mapping...
+ } else if (SF->getLinkage() == GlobalValue::LinkOnceLinkage) {
+ // Completely ignore the source function.
ValueMap.insert(std::make_pair(SF, DF));
}
}
// DF not external SF external?
if (!DF->isExternal()) {
+ if (DF->hasLinkOnceLinkage()) continue; // No relinkage for link-once!
if (Err)
*Err = "Function '" + (SF->hasName() ? SF->getName() :std::string(""))
+ "' body multiply defined!";
GV = PI->second; // put in map
else
{
- GV = new GlobalVariable(CV->getType(), true,true,CV); //put in map
+ GV = new GlobalVariable(CV->getType(), true, //put in map
+ GlobalValue::InternalLinkage, CV);
myModule->getGlobalList().push_back(GV); // GV owned by module now
}
}
}
// Make sure our result is globally accessable...
- Named->setInternalLinkage(false);
+ Named->setLinkage(GlobalValue::ExternalLinkage);
// Mark all global variables internal
for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
if (!I->isExternal()) {
I->setInitializer(0); // Make all variables external
- I->setInternalLinkage(false); // Make sure it's not internal
+ I->setLinkage(GlobalValue::ExternalLinkage);
}
// All of the functions may be used by global variables or the named
for (Module::iterator I = M.begin(); ; ++I) {
if (&*I != Named) {
- Function *New = new Function(I->getFunctionType(),false,I->getName());
+ Function *New = new Function(I->getFunctionType(),
+ GlobalValue::ExternalLinkage,
+ I->getName());
I->setName(""); // Remove Old name
// If it's not the named function, delete the body of the function
if (&*I != MainFunc && // Leave the main function external
!I->isExternal() && // Function must be defined here
!I->hasInternalLinkage()) { // Can't already have internal linkage
- I->setInternalLinkage(true);
+ I->setLinkage(GlobalValue::InternalLinkage);
Changed = true;
++NumFunctions;
DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
// Mark all global variables with initializers as internal as well...
for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
if (!I->isExternal() && I->hasExternalLinkage()) {
- I->setInternalLinkage(true);
+ I->setLinkage(GlobalValue::InternalLinkage);
Changed = true;
++NumGlobals;
DEBUG(std::cerr << "Internalizing gvar " << I->getName() << "\n");
cast<FunctionType>(ConvertType(I->getFunctionType()));
// Create a new function to put stuff into...
- Function *NewMeth = new Function(NewMTy, I->hasInternalLinkage(),
- I->getName());
+ Function *NewMeth = new Function(NewMTy, I->getLinkage(), I->getName());
if (I->hasName())
I->setName("OLD."+I->getName());
DummySyncFunc = new Function(FunctionType::get( Type::VoidTy,
std::vector<const Type*>(),
/*isVararg*/ false),
- /*isInternal*/ false, DummySyncFuncName, &M);
+ GlobalValue::ExternalLinkage, DummySyncFuncName,
+ &M);
}
void Cilkifier::TransformFunc(Function* F,
FunctionType *FuncTy = FunctionType::get(OldFuncTy->getReturnType(), ArgTys,
OldFuncTy->isVarArg());
// Create the new function...
- Function *New = new Function(FuncTy, true, F.getName(), F.getParent());
+ Function *New = new Function(FuncTy, GlobalValue::InternalLinkage,
+ F.getName(), F.getParent());
// Set the rest of the new arguments names to be PDa<n> and add entries to the
// pool descriptors map
StructType *sttype = StructType::get(vType);
ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
- GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true, false,
+ GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
+ GlobalValue::ExternalLinkage,
cstruct, "llvmFunctionTable");
M.getGlobalList().push_back(gb);
return true; // Always modifies program
for(int xi=0; xi<numPaths; xi++)
arrayInitialize.push_back(ConstantSInt::get(Type::IntTy, 0));
- Constant *initializer = ConstantArray::get(ArrayType::get(Type::IntTy, numPaths), arrayInitialize);
- GlobalVariable *countVar = new GlobalVariable(ArrayType::get(Type::IntTy, numPaths), false, true, initializer, "Count", F.getParent());
+ const ArrayType *ATy = ArrayType::get(Type::IntTy, numPaths);
+ Constant *initializer = ConstantArray::get(ATy, arrayInitialize);
+ GlobalVariable *countVar = new GlobalVariable(ATy, false,
+ GlobalValue::InternalLinkage,
+ initializer, "Count",
+ F.getParent());
static GlobalVariable *threshold = NULL;
static bool insertedThreshold = false;
if(!insertedThreshold){
- threshold = new GlobalVariable(Type::IntTy, false, false, 0,
- "reopt_threshold");
+ threshold = new GlobalVariable(Type::IntTy, false,
+ GlobalValue::ExternalLinkage, 0,
+ "reopt_threshold");
F.getParent()->getGlobalList().push_back(threshold);
insertedThreshold = true;
// Create the global variable and record it in the module
// The GV will be renamed to a unique name if needed.
- GlobalVariable *GV = new GlobalVariable(Init->getType(), true, true, Init,
+ GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
+ GlobalValue::InternalLinkage, Init,
"trstr");
M->getGlobalList().push_back(GV);
return GV;
ArgTypes, F->getFunctionType()->isVarArg());
// Create the new function...
- Function *NewF = new Function(FTy, F->hasInternalLinkage(), F->getName());
+ Function *NewF = new Function(FTy, F->getLinkage(), F->getName());
// Loop over the arguments, copying the names of the mapped arguments over...
Function::aiterator DestI = NewF->abegin();
// don't worry about attributes or initializers, they will come later.
//
for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
- ValueMap[I] = new GlobalVariable(I->getType()->getElementType(),
- false, false, 0, I->getName(), New);
+ ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false,
+ GlobalValue::ExternalLinkage, 0,
+ I->getName(), New);
// Loop over the functions in the module, making external functions as before
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
ValueMap[I]=new Function(cast<FunctionType>(I->getType()->getElementType()),
- false, I->getName(), New);
+ GlobalValue::ExternalLinkage, I->getName(), New);
// Now that all of the things that global variable initializer can refer to
// have been created, loop through and copy the global variable referrers
if (I->hasInitializer())
GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
ValueMap)));
- if (I->hasInternalLinkage())
- GV->setInternalLinkage(true);
+ GV->setLinkage(I->getLinkage());
}
// Similarly, copy over function bodies now...
CloneFunctionInto(F, I, ValueMap, Returns);
}
- if (I->hasInternalLinkage())
- F->setInternalLinkage(true);
+ F->setLinkage(I->getLinkage());
}
return New;
//
for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
const GlobalVariable *SGV = I;
- Value *V;
-
- // If the global variable has a name, and that name is already in use in the
- // Dest module, make sure that the name is a compatible global variable...
- //
- if (SGV->hasExternalLinkage() && SGV->hasName() &&
- (V = ST->lookup(SGV->getType(), SGV->getName())) &&
- cast<GlobalVariable>(V)->hasExternalLinkage()) {
- // The same named thing is a global variable, because the only two things
+ GlobalVariable *DGV = 0;
+ if (SGV->hasName()) {
+ // A same named thing is a global variable, because the only two things
// that may be in a module level symbol table are Global Vars and
// Functions, and they both have distinct, nonoverlapping, possible types.
//
- GlobalVariable *DGV = cast<GlobalVariable>(V);
+ DGV = cast_or_null<GlobalVariable>(ST->lookup(SGV->getType(),
+ SGV->getName()));
+ }
- // Check to see if the two GV's have the same Const'ness...
- if (SGV->isConstant() != DGV->isConstant())
- return Error(Err, "Global Variable Collision on '" +
- SGV->getType()->getDescription() + "':%" + SGV->getName() +
- " - Global variables differ in const'ness");
+ assert(SGV->hasInitializer() || SGV->hasExternalLinkage() &&
+ "Global must either be external or have an initializer!");
- // Okay, everything is cool, remember the mapping...
- ValueMap.insert(std::make_pair(SGV, DGV));
- } else {
+ if (!DGV || DGV->hasInternalLinkage() || SGV->hasInternalLinkage()) {
// No linking to be performed, simply create an identical version of the
// symbol over in the dest module... the initializer will be filled in
// later by LinkGlobalInits...
//
- GlobalVariable *DGV =
- new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(),
- SGV->hasInternalLinkage(), 0, SGV->getName());
-
- // Add the new global to the dest module
- Dest->getGlobalList().push_back(DGV);
+ DGV = new GlobalVariable(SGV->getType()->getElementType(),
+ SGV->isConstant(), SGV->getLinkage(), /*init*/0,
+ SGV->getName(), Dest);
// Make sure to remember this mapping...
ValueMap.insert(std::make_pair(SGV, DGV));
+ } else if (SGV->getLinkage() != DGV->getLinkage()) {
+ return Error(Err, "Global variables named '" + SGV->getName() +
+ "' have different linkage specifiers!");
+ } else if (SGV->hasExternalLinkage() || SGV->hasLinkOnceLinkage() ||
+ SGV->hasAppendingLinkage()) {
+ // If the global variable has a name, and that name is already in use in
+ // the Dest module, make sure that the name is a compatible global
+ // variable...
+ //
+ // Check to see if the two GV's have the same Const'ness...
+ if (SGV->isConstant() != DGV->isConstant())
+ return Error(Err, "Global Variable Collision on '" +
+ SGV->getType()->getDescription() + "':%" + SGV->getName() +
+ " - Global variables differ in const'ness");
+ // Okay, everything is cool, remember the mapping...
+ ValueMap.insert(std::make_pair(SGV, DGV));
+ } else {
+ assert(0 && "Unknown linkage!");
}
}
return false;
if (SGV->hasInitializer()) { // Only process initialized GV's
// Figure out what the initializer looks like in the dest module...
- Constant *DInit =
+ Constant *SInit =
cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap, 0));
GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);
- if (DGV->hasInitializer() && SGV->hasExternalLinkage() &&
- DGV->hasExternalLinkage()) {
- if (DGV->getInitializer() != DInit)
- return Error(Err, "Global Variable Collision on '" +
- SGV->getType()->getDescription() + "':%" +SGV->getName()+
- " - Global variables have different initializers");
+ if (DGV->hasInitializer()) {
+ assert(SGV->getLinkage() == DGV->getLinkage());
+ if (SGV->hasExternalLinkage()) {
+ if (DGV->getInitializer() != SInit)
+ return Error(Err, "Global Variable Collision on '" +
+ SGV->getType()->getDescription() +"':%"+SGV->getName()+
+ " - Global variables have different initializers");
+ } else if (DGV->hasLinkOnceLinkage()) {
+ // Nothing is required, mapped values will take the new global
+ // automatically.
+ } else if (DGV->hasAppendingLinkage()) {
+ assert(0 && "Appending linkage unimplemented!");
+ } else {
+ assert(0 && "Unknown linkage!");
+ }
} else {
// Copy the initializer over now...
- DGV->setInitializer(DInit);
+ DGV->setInitializer(SInit);
}
}
}
//
for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
const Function *SF = I; // SrcFunction
- Value *V;
-
- // If the function has a name, and that name is already in use in the Dest
- // module, make sure that the name is a compatible function...
- //
- if (SF->hasExternalLinkage() && SF->hasName() &&
- (V = ST->lookup(SF->getType(), SF->getName())) &&
- cast<Function>(V)->hasExternalLinkage()) {
+ Function *DF = 0;
+ if (SF->hasName())
// The same named thing is a Function, because the only two things
// that may be in a module level symbol table are Global Vars and
// Functions, and they both have distinct, nonoverlapping, possible types.
//
- Function *DF = cast<Function>(V); // DestFunction
+ DF = cast_or_null<Function>(ST->lookup(SF->getType(), SF->getName()));
+ if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) {
+ // Function does not already exist, simply insert an external function
+ // signature identical to SF into the dest module...
+ Function *DF = new Function(SF->getFunctionType(), SF->getLinkage(),
+ SF->getName(), Dest);
+
+ // ... and remember this mapping...
+ ValueMap.insert(std::make_pair(SF, DF));
+ } else if (SF->getLinkage() != DF->getLinkage()) {
+ return Error(Err, "Functions named '" + SF->getName() +
+ "' have different linkage specifiers!");
+ } else if (SF->getLinkage() == GlobalValue::AppendingLinkage) {
+ return Error(Err, "Functions named '" + SF->getName() +
+ "' have appending linkage!");
+ } else if (SF->getLinkage() == GlobalValue::ExternalLinkage) {
+ // If the function has a name, and that name is already in use in the Dest
+ // module, make sure that the name is a compatible function...
+ //
// Check to make sure the function is not defined in both modules...
if (!SF->isExternal() && !DF->isExternal())
return Error(Err, "Function '" +
SF->getFunctionType()->getDescription() + "':\"" +
SF->getName() + "\" - Function is already defined!");
-
+
// Otherwise, just remember this mapping...
ValueMap.insert(std::make_pair(SF, DF));
- } else {
- // Function does not already exist, simply insert an external function
- // signature identical to SF into the dest module...
- Function *DF = new Function(SF->getFunctionType(),
- SF->hasInternalLinkage(),
- SF->getName());
-
- // Add the function signature to the dest module...
- Dest->getFunctionList().push_back(DF);
-
- // ... and remember this mapping...
+ } else if (SF->getLinkage() == GlobalValue::LinkOnceLinkage) {
+ // Completely ignore the source function.
ValueMap.insert(std::make_pair(SF, DF));
}
}
// DF not external SF external?
if (!DF->isExternal()) {
+ if (DF->hasLinkOnceLinkage()) continue; // No relinkage for link-once!
if (Err)
*Err = "Function '" + (SF->hasName() ? SF->getName() :std::string(""))
+ "' body multiply defined!";
void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
if (GV->hasName()) Out << "%" << GV->getName() << " = ";
- if (GV->hasInternalLinkage()) Out << "internal ";
- if (!GV->hasInitializer()) Out << "external ";
+ if (!GV->hasInitializer())
+ Out << "external ";
+ else
+ switch (GV->getLinkage()) {
+ case GlobalValue::InternalLinkage: Out << "internal "; break;
+ case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
+ case GlobalValue::AppendingLinkage: Out << "appending "; break;
+ case GlobalValue::ExternalLinkage: break;
+ }
Out << (GV->isConstant() ? "constant " : "global ");
printType(GV->getType()->getElementType());
//
void AssemblyWriter::printFunction(const Function *F) {
// Print out the return type and name...
- Out << "\n" << (F->isExternal() ? "declare " : "")
- << (F->hasInternalLinkage() ? "internal " : "");
+ Out << "\n";
+
+ if (F->isExternal())
+ Out << "declare ";
+ else
+ switch (F->getLinkage()) {
+ case GlobalValue::InternalLinkage: Out << "internal "; break;
+ case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
+ case GlobalValue::AppendingLinkage: Out << "appending "; break;
+ case GlobalValue::ExternalLinkage: break;
+ }
+
printType(F->getReturnType()) << " %" << F->getName() << "(";
Table.incorporateFunction(F);
// Function Implementation
//===----------------------------------------------------------------------===//
-Function::Function(const FunctionType *Ty, bool isInternal,
+Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
const std::string &name, Module *ParentModule)
- : GlobalValue(PointerType::get(Ty), Value::FunctionVal, isInternal, name) {
+ : GlobalValue(PointerType::get(Ty), Value::FunctionVal, Linkage, name) {
BasicBlocks.setItemParent(this);
BasicBlocks.setParent(this);
ArgumentList.setItemParent(this);
// GlobalVariable Implementation
//===----------------------------------------------------------------------===//
-GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
+GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
Constant *Initializer,
const std::string &Name, Module *ParentModule)
- : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
+ : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
isConstantGlobal(constant) {
if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
//
for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
const GlobalVariable *SGV = I;
- Value *V;
-
- // If the global variable has a name, and that name is already in use in the
- // Dest module, make sure that the name is a compatible global variable...
- //
- if (SGV->hasExternalLinkage() && SGV->hasName() &&
- (V = ST->lookup(SGV->getType(), SGV->getName())) &&
- cast<GlobalVariable>(V)->hasExternalLinkage()) {
- // The same named thing is a global variable, because the only two things
+ GlobalVariable *DGV = 0;
+ if (SGV->hasName()) {
+ // A same named thing is a global variable, because the only two things
// that may be in a module level symbol table are Global Vars and
// Functions, and they both have distinct, nonoverlapping, possible types.
//
- GlobalVariable *DGV = cast<GlobalVariable>(V);
+ DGV = cast_or_null<GlobalVariable>(ST->lookup(SGV->getType(),
+ SGV->getName()));
+ }
- // Check to see if the two GV's have the same Const'ness...
- if (SGV->isConstant() != DGV->isConstant())
- return Error(Err, "Global Variable Collision on '" +
- SGV->getType()->getDescription() + "':%" + SGV->getName() +
- " - Global variables differ in const'ness");
+ assert(SGV->hasInitializer() || SGV->hasExternalLinkage() &&
+ "Global must either be external or have an initializer!");
- // Okay, everything is cool, remember the mapping...
- ValueMap.insert(std::make_pair(SGV, DGV));
- } else {
+ if (!DGV || DGV->hasInternalLinkage() || SGV->hasInternalLinkage()) {
// No linking to be performed, simply create an identical version of the
// symbol over in the dest module... the initializer will be filled in
// later by LinkGlobalInits...
//
- GlobalVariable *DGV =
- new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(),
- SGV->hasInternalLinkage(), 0, SGV->getName());
-
- // Add the new global to the dest module
- Dest->getGlobalList().push_back(DGV);
+ DGV = new GlobalVariable(SGV->getType()->getElementType(),
+ SGV->isConstant(), SGV->getLinkage(), /*init*/0,
+ SGV->getName(), Dest);
// Make sure to remember this mapping...
ValueMap.insert(std::make_pair(SGV, DGV));
+ } else if (SGV->getLinkage() != DGV->getLinkage()) {
+ return Error(Err, "Global variables named '" + SGV->getName() +
+ "' have different linkage specifiers!");
+ } else if (SGV->hasExternalLinkage() || SGV->hasLinkOnceLinkage() ||
+ SGV->hasAppendingLinkage()) {
+ // If the global variable has a name, and that name is already in use in
+ // the Dest module, make sure that the name is a compatible global
+ // variable...
+ //
+ // Check to see if the two GV's have the same Const'ness...
+ if (SGV->isConstant() != DGV->isConstant())
+ return Error(Err, "Global Variable Collision on '" +
+ SGV->getType()->getDescription() + "':%" + SGV->getName() +
+ " - Global variables differ in const'ness");
+ // Okay, everything is cool, remember the mapping...
+ ValueMap.insert(std::make_pair(SGV, DGV));
+ } else {
+ assert(0 && "Unknown linkage!");
}
}
return false;
if (SGV->hasInitializer()) { // Only process initialized GV's
// Figure out what the initializer looks like in the dest module...
- Constant *DInit =
+ Constant *SInit =
cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap, 0));
GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);
- if (DGV->hasInitializer() && SGV->hasExternalLinkage() &&
- DGV->hasExternalLinkage()) {
- if (DGV->getInitializer() != DInit)
- return Error(Err, "Global Variable Collision on '" +
- SGV->getType()->getDescription() + "':%" +SGV->getName()+
- " - Global variables have different initializers");
+ if (DGV->hasInitializer()) {
+ assert(SGV->getLinkage() == DGV->getLinkage());
+ if (SGV->hasExternalLinkage()) {
+ if (DGV->getInitializer() != SInit)
+ return Error(Err, "Global Variable Collision on '" +
+ SGV->getType()->getDescription() +"':%"+SGV->getName()+
+ " - Global variables have different initializers");
+ } else if (DGV->hasLinkOnceLinkage()) {
+ // Nothing is required, mapped values will take the new global
+ // automatically.
+ } else if (DGV->hasAppendingLinkage()) {
+ assert(0 && "Appending linkage unimplemented!");
+ } else {
+ assert(0 && "Unknown linkage!");
+ }
} else {
// Copy the initializer over now...
- DGV->setInitializer(DInit);
+ DGV->setInitializer(SInit);
}
}
}
//
for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
const Function *SF = I; // SrcFunction
- Value *V;
-
- // If the function has a name, and that name is already in use in the Dest
- // module, make sure that the name is a compatible function...
- //
- if (SF->hasExternalLinkage() && SF->hasName() &&
- (V = ST->lookup(SF->getType(), SF->getName())) &&
- cast<Function>(V)->hasExternalLinkage()) {
+ Function *DF = 0;
+ if (SF->hasName())
// The same named thing is a Function, because the only two things
// that may be in a module level symbol table are Global Vars and
// Functions, and they both have distinct, nonoverlapping, possible types.
//
- Function *DF = cast<Function>(V); // DestFunction
+ DF = cast_or_null<Function>(ST->lookup(SF->getType(), SF->getName()));
+ if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) {
+ // Function does not already exist, simply insert an external function
+ // signature identical to SF into the dest module...
+ Function *DF = new Function(SF->getFunctionType(), SF->getLinkage(),
+ SF->getName(), Dest);
+
+ // ... and remember this mapping...
+ ValueMap.insert(std::make_pair(SF, DF));
+ } else if (SF->getLinkage() != DF->getLinkage()) {
+ return Error(Err, "Functions named '" + SF->getName() +
+ "' have different linkage specifiers!");
+ } else if (SF->getLinkage() == GlobalValue::AppendingLinkage) {
+ return Error(Err, "Functions named '" + SF->getName() +
+ "' have appending linkage!");
+ } else if (SF->getLinkage() == GlobalValue::ExternalLinkage) {
+ // If the function has a name, and that name is already in use in the Dest
+ // module, make sure that the name is a compatible function...
+ //
// Check to make sure the function is not defined in both modules...
if (!SF->isExternal() && !DF->isExternal())
return Error(Err, "Function '" +
SF->getFunctionType()->getDescription() + "':\"" +
SF->getName() + "\" - Function is already defined!");
-
+
// Otherwise, just remember this mapping...
ValueMap.insert(std::make_pair(SF, DF));
- } else {
- // Function does not already exist, simply insert an external function
- // signature identical to SF into the dest module...
- Function *DF = new Function(SF->getFunctionType(),
- SF->hasInternalLinkage(),
- SF->getName());
-
- // Add the function signature to the dest module...
- Dest->getFunctionList().push_back(DF);
-
- // ... and remember this mapping...
+ } else if (SF->getLinkage() == GlobalValue::LinkOnceLinkage) {
+ // Completely ignore the source function.
ValueMap.insert(std::make_pair(SF, DF));
}
}
// DF not external SF external?
if (!DF->isExternal()) {
+ if (DF->hasLinkOnceLinkage()) continue; // No relinkage for link-once!
if (Err)
*Err = "Function '" + (SF->hasName() ? SF->getName() :std::string(""))
+ "' body multiply defined!";
Function *ilist_traits<Function>::createNode() {
FunctionType *FTy =
FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
- Function *Ret = new Function(FTy, false);
+ Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage);
// This should not be garbage monitored.
LeakDetector::removeGarbageObject(Ret);
return Ret;
}
GlobalVariable *ilist_traits<GlobalVariable>::createNode() {
- GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false, false);
+ GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false,
+ GlobalValue::ExternalLinkage);
// This should not be garbage monitored.
LeakDetector::removeGarbageObject(Ret);
return Ret;
if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) {
return cast<Function>(V); // Yup, got it
} else { // Nope, add one
- Function *New = new Function(Ty, false, Name);
+ Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name);
FunctionList.push_back(New);
return New; // Return the new prototype...
}