6eec2647d2b7f54c79c61f3d0d91e14ce717dafb
[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 "llvm/Support/CommandLine.h"
23 #include "llvm/Support/PluginLoader.h"
24 #include "llvm/System/Signals.h"
25 #include <iostream>
26
27 using namespace llvm;
28
29 namespace {
30   cl::opt<std::string>
31   InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
32
33   cl::list<std::string>
34   InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
35
36   cl::opt<bool> ForceInterpreter("force-interpreter",
37                                  cl::desc("Force interpretation: disable JIT"),
38                                  cl::init(false));
39   cl::opt<std::string>
40   TargetTriple("mtriple", cl::desc("Override target triple for module"));
41   
42   cl::opt<std::string>
43   FakeArgv0("fake-argv0",
44             cl::desc("Override the 'argv[0]' value passed into the executing"
45                      " program"), cl::value_desc("executable"));
46 }
47
48 //===----------------------------------------------------------------------===//
49 // main Driver function
50 //
51 int main(int argc, char **argv, char * const *envp) {
52   try {
53     cl::ParseCommandLineOptions(argc, argv,
54                                 " llvm interpreter & dynamic compiler\n");
55     sys::PrintStackTraceOnErrorSignal();
56
57     // Load the bytecode...
58     std::string ErrorMsg;
59     ModuleProvider *MP = 0;
60     try {
61       MP = getBytecodeModuleProvider(InputFile);
62     } catch (std::string &err) {
63       std::cerr << "Error loading program '" << InputFile << "': "
64                 << err << "\n";
65       exit(1);
66     }
67
68     // If we are supposed to override the target triple, do so now.
69     if (!TargetTriple.empty())
70       MP->getModule()->setTargetTriple(TargetTriple);
71     
72     ExecutionEngine *EE = ExecutionEngine::create(MP, ForceInterpreter);
73     assert(EE &&"Couldn't create an ExecutionEngine, not even an interpreter?");
74
75     // If the user specifically requested an argv[0] to pass into the program,
76     // do it now.
77     if (!FakeArgv0.empty()) {
78       InputFile = FakeArgv0;
79     } else {
80       // Otherwise, if there is a .bc suffix on the executable strip it off, it
81       // might confuse the program.
82       if (InputFile.rfind(".bc") == InputFile.length() - 3)
83         InputFile.erase(InputFile.length() - 3);
84     }
85
86     // Add the module's name to the start of the vector of arguments to main().
87     InputArgv.insert(InputArgv.begin(), InputFile);
88
89     // Call the main function from M as if its signature were:
90     //   int main (int argc, char **argv, const char **envp)
91     // using the contents of Args to determine argc & argv, and the contents of
92     // EnvVars to determine envp.
93     //
94     Function *Fn = MP->getModule()->getMainFunction();
95     if (!Fn) {
96       std::cerr << "'main' function not found in module.\n";
97       return -1;
98     }
99
100     // Run static constructors.
101     EE->runStaticConstructorsDestructors(false);
102     
103     // Run main.
104     int Result = EE->runFunctionAsMain(Fn, InputArgv, envp);
105
106     // Run static destructors.
107     EE->runStaticConstructorsDestructors(true);
108     
109     // If the program didn't explicitly call exit, call exit now, for the
110     // program. This ensures that any atexit handlers get called correctly.
111     Function *Exit = MP->getModule()->getOrInsertFunction("exit", Type::VoidTy,
112                                                           Type::IntTy,
113                                                           (Type *)0);
114
115     std::vector<GenericValue> Args;
116     GenericValue ResultGV;
117     ResultGV.IntVal = Result;
118     Args.push_back(ResultGV);
119     EE->runFunction(Exit, Args);
120
121     std::cerr << "ERROR: exit(" << Result << ") returned!\n";
122     abort();
123   } catch (const std::string& msg) {
124     std::cerr << argv[0] << ": " << msg << "\n";
125   } catch (...) {
126     std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
127   }
128   abort();
129 }