bd22b2924bf95d84d14370f57610606b8c366d54
[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
40   cl::opt<std::string>
41   FakeArgv0("fake-argv0",
42             cl::desc("Override the 'argv[0]' value passed into the executing"
43                      " program"), cl::value_desc("executable"));
44 }
45
46 //===----------------------------------------------------------------------===//
47 // main Driver function
48 //
49 int main(int argc, char **argv, char * const *envp) {
50   try {
51     cl::ParseCommandLineOptions(argc, argv,
52                                 " llvm interpreter & dynamic compiler\n");
53     sys::PrintStackTraceOnErrorSignal();
54
55     // Load the bytecode...
56     std::string ErrorMsg;
57     ModuleProvider *MP = 0;
58     try {
59       MP = getBytecodeModuleProvider(InputFile);
60     } catch (std::string &err) {
61       std::cerr << "Error loading program '" << InputFile << "': " << err << "\n";
62       exit(1);
63     }
64
65     ExecutionEngine *EE = ExecutionEngine::create(MP, ForceInterpreter);
66     assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
67
68     // If the user specifically requested an argv[0] to pass into the program, do
69     // it now.
70     if (!FakeArgv0.empty()) {
71       InputFile = FakeArgv0;
72     } else {
73       // Otherwise, if there is a .bc suffix on the executable strip it off, it
74       // might confuse the program.
75       if (InputFile.rfind(".bc") == InputFile.length() - 3)
76         InputFile.erase(InputFile.length() - 3);
77     }
78
79     // Add the module's name to the start of the vector of arguments to main().
80     InputArgv.insert(InputArgv.begin(), InputFile);
81
82     // Call the main function from M as if its signature were:
83     //   int main (int argc, char **argv, const char **envp)
84     // using the contents of Args to determine argc & argv, and the contents of
85     // EnvVars to determine envp.
86     //
87     Function *Fn = MP->getModule()->getMainFunction();
88     if (!Fn) {
89       std::cerr << "'main' function not found in module.\n";
90       return -1;
91     }
92
93     // Run main...
94     int Result = EE->runFunctionAsMain(Fn, InputArgv, envp);
95
96     // If the program didn't explicitly call exit, call exit now, for the program.
97     // This ensures that any atexit handlers get called correctly.
98     Function *Exit = MP->getModule()->getOrInsertFunction("exit", Type::VoidTy,
99                                                           Type::IntTy, 0);
100
101     std::vector<GenericValue> Args;
102     GenericValue ResultGV;
103     ResultGV.IntVal = Result;
104     Args.push_back(ResultGV);
105     EE->runFunction(Exit, Args);
106
107     std::cerr << "ERROR: exit(" << Result << ") returned!\n";
108     abort();
109   } catch (const std::string& msg) {
110     std::cerr << argv[0] << ": " << msg << "\n";
111   } catch (...) {
112     std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
113   }
114   abort();
115 }