[llvm-commits] CVS: llvm/tools/lli/JIT/Intercept.cpp JIT.cpp VM.h

Chris Lattner lattner at cs.uiuc.edu
Wed May 14 08:54:01 PDT 2003


Changes in directory llvm/tools/lli/JIT:

Intercept.cpp updated: 1.2 -> 1.3
JIT.cpp updated: 1.2 -> 1.3
VM.h updated: 1.4 -> 1.5

---
Log message:

Add support for atexit handlers to the JIT, fixing 2003-05-14-AtExit.c


---
Diffs of the changes:

Index: llvm/tools/lli/JIT/Intercept.cpp
diff -u llvm/tools/lli/JIT/Intercept.cpp:1.2 llvm/tools/lli/JIT/Intercept.cpp:1.3
--- llvm/tools/lli/JIT/Intercept.cpp:1.2	Wed May 14 08:27:36 2003
+++ llvm/tools/lli/JIT/Intercept.cpp	Wed May 14 08:53:40 2003
@@ -12,6 +12,17 @@
 #include <dlfcn.h>    // dlsym access
 #include <iostream>
 
+// AtExitList - List of functions registered with the at_exit function
+static std::vector<void (*)()> AtExitList;
+
+void VM::runAtExitHandlers() {
+  while (!AtExitList.empty()) {
+    void (*Fn)() = AtExitList.back();
+    AtExitList.pop_back();
+    Fn();
+  }
+}
+
 //===----------------------------------------------------------------------===//
 // Function stubs that are invoked instead of raw system calls
 //===----------------------------------------------------------------------===//
@@ -21,12 +32,14 @@
 
 // jit_exit - Used to intercept the "exit" system call.
 static void jit_exit(int Status) {
-  exit(Status);  // Do nothing for now.
+  VM::runAtExitHandlers();   // Run at_exit handlers...
+  exit(Status);
 }
 
 // jit_atexit - Used to intercept the "at_exit" system call.
 static int jit_atexit(void (*Fn)(void)) {
-  return atexit(Fn);    // Do nothing for now.
+  AtExitList.push_back(Fn);    // Take note of at_exit handler...
+  return 0;  // Always successful
 }
 
 //===----------------------------------------------------------------------===//
@@ -38,7 +51,7 @@
 void *VM::getPointerToNamedFunction(const std::string &Name) {
   // Check to see if this is one of the functions we want to intercept...
   if (Name == "exit") return (void*)&jit_exit;
-  if (Name == "at_exit") return (void*)&jit_atexit;
+  if (Name == "atexit") return (void*)&jit_atexit;
 
   // If it's an external function, look it up in the process image...
   void *Ptr = dlsym(0, Name.c_str());


Index: llvm/tools/lli/JIT/JIT.cpp
diff -u llvm/tools/lli/JIT/JIT.cpp:1.2 llvm/tools/lli/JIT/JIT.cpp:1.3
--- llvm/tools/lli/JIT/JIT.cpp:1.2	Sun May 11 21:14:34 2003
+++ llvm/tools/lli/JIT/JIT.cpp	Wed May 14 08:53:40 2003
@@ -50,5 +50,9 @@
   char **Argv = (char**)CreateArgv(Args);
 
   // Call the main function...
-  return PF(Args.size(), Argv);
+  int Result = PF(Args.size(), Argv);
+
+  // Run any atexit handlers now!
+  runAtExitHandlers();
+  return Result;
 }


Index: llvm/tools/lli/JIT/VM.h
diff -u llvm/tools/lli/JIT/VM.h:1.4 llvm/tools/lli/JIT/VM.h:1.5
--- llvm/tools/lli/JIT/VM.h:1.4	Thu May  8 22:30:07 2003
+++ llvm/tools/lli/JIT/VM.h	Wed May 14 08:53:40 2003
@@ -27,6 +27,7 @@
   // handler to lazily patch up references...
   //
   std::map<void*, Function*> FunctionRefs;
+
 public:
   VM(Module *M, TargetMachine *tm);
   ~VM();
@@ -54,6 +55,12 @@
   // which causes lazy compilation of the target function.
   // 
   static void CompilationCallback();
+
+  /// runAtExitHandlers - Before exiting the program, at_exit functions must be
+  /// called.  This method calls them.
+  ///
+  static void runAtExitHandlers();
+
 private:
   static MachineCodeEmitter *createEmitter(VM &V);
   void setupPassManager();





More information about the llvm-commits mailing list