[cfe-commits] r48362 - in /cfe/trunk/CodeGen: CodeGenModule.cpp CodeGenModule.h
Chris Lattner
sabre at nondot.org
Fri Mar 14 10:18:19 PDT 2008
Author: lattner
Date: Fri Mar 14 12:18:18 2008
New Revision: 48362
URL: http://llvm.org/viewvc/llvm-project?rev=48362&view=rev
Log:
add initial support for generating an llvm.globalctors list. Patch by David Chisnall
Modified:
cfe/trunk/CodeGen/CodeGenModule.cpp
cfe/trunk/CodeGen/CodeGenModule.h
Modified: cfe/trunk/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenModule.cpp?rev=48362&r1=48361&r2=48362&view=diff
==============================================================================
--- cfe/trunk/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/CodeGen/CodeGenModule.cpp Fri Mar 14 12:18:18 2008
@@ -38,6 +38,7 @@
}
CodeGenModule::~CodeGenModule() {
+ EmitGlobalCtors();
delete Runtime;
}
@@ -62,6 +63,59 @@
&Msg, 1);
}
+/// AddGlobalCtor - Add a function to the list that will be called before
+/// main() runs.
+void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor) {
+ // TODO: Type coercion of void()* types.
+ GlobalCtors.push_back(Ctor);
+}
+
+void CodeGenModule::EmitGlobalCtors() {
+ // Get the type of @llvm.global_ctors
+ std::vector<const llvm::Type*> CtorFields;
+ CtorFields.push_back(llvm::IntegerType::get(32));
+ // Constructor function type
+ std::vector<const llvm::Type*> VoidArgs;
+ llvm::FunctionType* CtorFuncTy = llvm::FunctionType::get(
+ llvm::Type::VoidTy,
+ VoidArgs,
+ false);
+ // i32, function type pair
+ CtorFields.push_back(llvm::PointerType::getUnqual(CtorFuncTy));
+ llvm::StructType* CtorStructTy = llvm::StructType::get(CtorFields, false);
+ // Array of fields
+ llvm::ArrayType* GlobalCtorsTy = llvm::ArrayType::get(CtorStructTy,
+ GlobalCtors.size());
+
+ const std::string GlobalCtorsVar = std::string("llvm.global_ctors");
+ // Define the global variable
+ llvm::GlobalVariable *GlobalCtorsVal = new llvm::GlobalVariable(
+ GlobalCtorsTy,
+ false,
+ llvm::GlobalValue::AppendingLinkage,
+ (llvm::Constant*)0,
+ GlobalCtorsVar,
+ &TheModule);
+
+ // Populate the array
+ std::vector<llvm::Constant*> CtorValues;
+ llvm::Constant *MagicNumber = llvm::ConstantInt::get(llvm::IntegerType::Int32Ty,
+ 65535,
+ false);
+ for (std::vector<llvm::Constant*>::iterator I = GlobalCtors.begin(),
+ E = GlobalCtors.end(); I != E; ++I) {
+ std::vector<llvm::Constant*> StructValues;
+ StructValues.push_back(MagicNumber);
+ StructValues.push_back(*I);
+
+ llvm::Constant* CtorEntry = llvm::ConstantStruct::get(CtorStructTy, StructValues);
+ CtorValues.push_back(CtorEntry);
+ }
+ llvm::Constant* CtorArray = llvm::ConstantArray::get(GlobalCtorsTy, CtorValues);
+ GlobalCtorsVal->setInitializer(CtorArray);
+
+}
+
/// ReplaceMapValuesWith - This is a really slow and bad function that
/// searches for any entries in GlobalDeclMap that point to OldVal, changing
/// them to point to NewVal. This is badbadbad, FIXME!
Modified: cfe/trunk/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenModule.h?rev=48362&r1=48361&r2=48362&view=diff
==============================================================================
--- cfe/trunk/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/CodeGen/CodeGenModule.h Fri Mar 14 12:18:18 2008
@@ -58,6 +58,7 @@
llvm::Function *MemCpyFn;
llvm::Function *MemSetFn;
llvm::DenseMap<const Decl*, llvm::Constant*> GlobalDeclMap;
+ std::vector<llvm::Constant*> GlobalCtors;
llvm::StringMap<llvm::Constant*> CFConstantStringMap;
llvm::StringMap<llvm::Constant*> ConstantStringMap;
@@ -96,6 +97,9 @@
llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0,
unsigned NumTys = 0);
+ void AddGlobalCtor(llvm::Function * Ctor);
+ void EmitGlobalCtors(void);
+
void EmitFunction(const FunctionDecl *FD);
void EmitGlobalVar(const FileVarDecl *D);
void EmitGlobalVarDeclarator(const FileVarDecl *D);
More information about the cfe-commits
mailing list