[llvm-commits] CVS: llvm/tools/lli/lli.cpp

Chris Lattner lattner at cs.uiuc.edu
Fri Dec 26 00:15:01 PST 2003


Changes in directory llvm/tools/lli:

lli.cpp updated: 1.37 -> 1.38

---
Log message:

* eliminate the -f argument to lli, as it was silly and never useful anyway
* Inline callMain function
* Remove hack from the ExecutionEngines where the 'run' method would automatically
  run atExit functions.  Fixing this requires explicitly calling exit if main returns


---
Diffs of the changes:  (+33 -37)

Index: llvm/tools/lli/lli.cpp
diff -u llvm/tools/lli/lli.cpp:1.37 llvm/tools/lli/lli.cpp:1.38
--- llvm/tools/lli/lli.cpp:1.37	Thu Dec 25 23:07:35 2003
+++ llvm/tools/lli/lli.cpp	Fri Dec 26 00:14:47 2003
@@ -34,10 +34,6 @@
   cl::list<std::string>
   InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
 
-  cl::opt<std::string>
-  MainFunction("f", cl::desc("Function to execute"), cl::init("main"),
-               cl::value_desc("function name"));
-
   cl::opt<bool> ForceInterpreter("force-interpreter",
                                  cl::desc("Force interpretation: disable JIT"),
                                  cl::init(false));
@@ -96,32 +92,6 @@
   }
 }
 
-/// callAsMain - Call the function named FnName from M as if its
-/// signature were int main (int argc, char **argv, const char
-/// **envp), using the contents of Args to determine argc & argv, and
-/// the contents of EnvVars to determine envp.  Returns the result
-/// from calling FnName, or -1 and prints an error msg. if the named
-/// function cannot be found.
-///
-int callAsMain(ExecutionEngine *EE, ModuleProvider *MP,
-               const std::string &FnName,
-               const std::vector<std::string> &Args,
-               const std::vector<std::string> &EnvVars) {
-  Function *Fn = MP->getModule()->getNamedFunction(FnName);
-  if (!Fn) {
-    std::cerr << "Function '" << FnName << "' not found in module.\n";
-    return -1;
-  }
-  std::vector<GenericValue> GVArgs;
-  GenericValue GVArgc;
-  GVArgc.IntVal = Args.size();
-  GVArgs.push_back(GVArgc); // Arg #0 = argc.
-  GVArgs.push_back(PTOGV(CreateArgv(EE, Args))); // Arg #1 = argv.
-  assert(((char **)GVTOP(GVArgs[1]))[0] && "argv[0] was null after CreateArgv");
-  GVArgs.push_back(PTOGV(CreateArgv(EE, EnvVars))); // Arg #2 = envp.
-  return EE->run(Fn, GVArgs).IntVal;
-}
-
 //===----------------------------------------------------------------------===//
 // main Driver function
 //
@@ -157,11 +127,37 @@
   // Add the module's name to the start of the vector of arguments to main().
   InputArgv.insert(InputArgv.begin(), InputFile);
 
-  // Run the main function!
-  int ExitCode = callAsMain(EE, MP, MainFunction, InputArgv,
-                            makeStringVector(envp)); 
-
-  // Now that we are done executing the program, shut down the execution engine
-  delete EE;
-  return ExitCode;
+  // Call the main function from M as if its signature were:
+  //   int main (int argc, char **argv, const char **envp)
+  // using the contents of Args to determine argc & argv, and the contents of
+  // EnvVars to determine envp.
+  //
+  Function *Fn = MP->getModule()->getMainFunction();
+  if (!Fn) {
+    std::cerr << "'main' function not found in module.\n";
+    return -1;
+  }
+
+  std::vector<GenericValue> GVArgs;
+  GenericValue GVArgc;
+  GVArgc.IntVal = InputArgv.size();
+  GVArgs.push_back(GVArgc); // Arg #0 = argc.
+  GVArgs.push_back(PTOGV(CreateArgv(EE, InputArgv))); // Arg #1 = argv.
+  assert(((char **)GVTOP(GVArgs[1]))[0] && "argv[0] was null after CreateArgv");
+
+  std::vector<std::string> EnvVars = makeStringVector(envp);
+  GVArgs.push_back(PTOGV(CreateArgv(EE, EnvVars))); // Arg #2 = envp.
+  GenericValue Result = EE->runFunction(Fn, GVArgs);
+
+  // If the program didn't explicitly call exit, call exit now, for the program.
+  // This ensures that any atexit handlers get called correctly.
+  Function *Exit = MP->getModule()->getOrInsertFunction("exit", Type::VoidTy,
+                                                        Type::IntTy, 0);
+  
+  GVArgs.clear();
+  GVArgs.push_back(Result);
+  EE->runFunction(Exit, GVArgs);
+
+  std::cerr << "ERROR: exit(" << Result.IntVal << ") returned!\n";
+  abort();
 }





More information about the llvm-commits mailing list