[llvm-commits] CVS: llvm/lib/ExecutionEngine/ExecutionEngine.cpp

Chris Lattner lattner at cs.uiuc.edu
Wed Mar 8 10:42:59 PST 2006



Changes in directory llvm/lib/ExecutionEngine:

ExecutionEngine.cpp updated: 1.74 -> 1.75
---
Log message:

Add a helper method for running static ctors/dtors in the module.


---
Diffs of the changes:  (+31 -2)

 ExecutionEngine.cpp |   33 +++++++++++++++++++++++++++++++--
 1 files changed, 31 insertions(+), 2 deletions(-)


Index: llvm/lib/ExecutionEngine/ExecutionEngine.cpp
diff -u llvm/lib/ExecutionEngine/ExecutionEngine.cpp:1.74 llvm/lib/ExecutionEngine/ExecutionEngine.cpp:1.75
--- llvm/lib/ExecutionEngine/ExecutionEngine.cpp:1.74	Mon Feb  6 23:11:57 2006
+++ llvm/lib/ExecutionEngine/ExecutionEngine.cpp	Wed Mar  8 12:42:46 2006
@@ -95,6 +95,37 @@
   return Result;
 }
 
+
+/// runStaticConstructorsDestructors - This method is used to execute all of
+/// the static constructors or destructors for a module, depending on the
+/// value of isDtors.
+void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
+  const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
+  GlobalVariable *GV = CurMod.getNamedGlobal(Name);
+  if (!GV || GV->isExternal() || !GV->hasInternalLinkage()) return;
+  
+  // Should be an array of '{ int, void ()* }' structs.  The first value is the
+  // init priority, which we ignore.
+  ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
+  if (!InitList) return;
+  for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
+    if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
+      if (CS->getNumOperands() != 2) return;  // Not array of 2-element structs.
+      
+      Constant *FP = CS->getOperand(1);
+      if (FP->isNullValue())
+        return;  // Found a null terminator, exit.
+      
+      if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
+        if (CE->getOpcode() == Instruction::Cast)
+          FP = CE->getOperand(0);
+      if (Function *F = dyn_cast<Function>(FP)) {
+        // Execute the ctor/dtor function!
+        runFunction(F, std::vector<GenericValue>());
+      }
+    }
+}
+
 /// runFunctionAsMain - This is a helper function which wraps runFunction to
 /// handle the common task of starting up main with the specified argc, argv,
 /// and envp parameters.
@@ -122,8 +153,6 @@
   return runFunction(Fn, GVArgs).IntVal;
 }
 
-
-
 /// If possible, create a JIT, unless the caller specifically requests an
 /// Interpreter or there's an error. If even an Interpreter cannot be created,
 /// NULL is returned.






More information about the llvm-commits mailing list