96a1a34bfaadc09cc7ad60a7c49e8b6b9c773302
[oota-llvm.git] / tools / lli / lli.cpp
1 //===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This utility provides a simple wrapper around the LLVM Execution Engines,
11 // which allow the direct execution of LLVM programs through a Just-In-Time
12 // compiler, or through an intepreter if no JIT is available for this platform.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Module.h"
17 #include "llvm/ModuleProvider.h"
18 #include "llvm/Type.h"
19 #include "llvm/Bytecode/Reader.h"
20 #include "llvm/ExecutionEngine/ExecutionEngine.h"
21 #include "llvm/ExecutionEngine/GenericValue.h"
22 #include "Support/CommandLine.h"
23 #include "Support/Signals.h"
24
25 using namespace llvm;
26
27 namespace {
28   cl::opt<std::string>
29   InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
30
31   cl::list<std::string>
32   InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
33
34   cl::opt<bool> ForceInterpreter("force-interpreter",
35                                  cl::desc("Force interpretation: disable JIT"),
36                                  cl::init(false));
37
38   cl::opt<std::string>
39   FakeArgv0("fake-argv0",
40             cl::desc("Override the 'argv[0]' value passed into the executing"
41                      " program"), cl::value_desc("executable"));
42 }
43
44 //===----------------------------------------------------------------------===//
45 // main Driver function
46 //
47 int main(int argc, char **argv, char * const *envp) {
48   cl::ParseCommandLineOptions(argc, argv,
49                               " llvm interpreter & dynamic compiler\n");
50   PrintStackTraceOnErrorSignal();
51
52   // Load the bytecode...
53   std::string ErrorMsg;
54   ModuleProvider *MP = 0;
55   try {
56     MP = getBytecodeModuleProvider(InputFile);
57   } catch (std::string &err) {
58     std::cerr << "Error loading program '" << InputFile << "': " << err << "\n";
59     exit(1);
60   }
61
62   ExecutionEngine *EE = ExecutionEngine::create(MP, ForceInterpreter);
63   assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
64
65   // If the user specifically requested an argv[0] to pass into the program, do
66   // it now.
67   if (!FakeArgv0.empty()) {
68     InputFile = FakeArgv0;
69   } else {
70     // Otherwise, if there is a .bc suffix on the executable strip it off, it
71     // might confuse the program.
72     if (InputFile.rfind(".bc") == InputFile.length() - 3)
73       InputFile.erase(InputFile.length() - 3);
74   }
75
76   // Add the module's name to the start of the vector of arguments to main().
77   InputArgv.insert(InputArgv.begin(), InputFile);
78
79   // Call the main function from M as if its signature were:
80   //   int main (int argc, char **argv, const char **envp)
81   // using the contents of Args to determine argc & argv, and the contents of
82   // EnvVars to determine envp.
83   //
84   Function *Fn = MP->getModule()->getMainFunction();
85   if (!Fn) {
86     std::cerr << "'main' function not found in module.\n";
87     return -1;
88   }
89
90   // Run main...
91   int Result = EE->runFunctionAsMain(Fn, InputArgv, envp);
92
93   // If the program didn't explicitly call exit, call exit now, for the program.
94   // This ensures that any atexit handlers get called correctly.
95   Function *Exit = MP->getModule()->getOrInsertFunction("exit", Type::VoidTy,
96                                                         Type::IntTy, 0);
97
98   std::vector<GenericValue> Args;
99   GenericValue ResultGV;
100   ResultGV.IntVal = Result;
101   Args.push_back(ResultGV);
102   EE->runFunction(Exit, Args);
103
104   std::cerr << "ERROR: exit(" << Result << ") returned!\n";
105   abort();
106 }