1 //===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Module.h"
17 #include "llvm/ModuleProvider.h"
18 #include "llvm/Type.h"
19 #include "llvm/Bitcode/ReaderWriter.h"
20 #include "llvm/CodeGen/LinkAllCodegenComponents.h"
21 #include "llvm/ExecutionEngine/JIT.h"
22 #include "llvm/ExecutionEngine/Interpreter.h"
23 #include "llvm/ExecutionEngine/GenericValue.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/ManagedStatic.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/PluginLoader.h"
28 #include "llvm/Support/PrettyStackTrace.h"
29 #include "llvm/System/Process.h"
30 #include "llvm/System/Signals.h"
37 InputFile(cl::desc("<input bitcode>"), cl::Positional
, cl::init("-"));
40 InputArgv(cl::ConsumeAfter
, cl::desc("<program arguments>..."));
42 cl::opt
<bool> ForceInterpreter("force-interpreter",
43 cl::desc("Force interpretation: disable JIT"),
46 // Determine optimization level.
49 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
56 TargetTriple("mtriple", cl::desc("Override target triple for module"));
59 EntryFunc("entry-function",
60 cl::desc("Specify the entry function (default = 'main') "
62 cl::value_desc("function"),
66 FakeArgv0("fake-argv0",
67 cl::desc("Override the 'argv[0]' value passed into the executing"
68 " program"), cl::value_desc("executable"));
71 DisableCoreFiles("disable-core-files", cl::Hidden
,
72 cl::desc("Disable emission of core files if possible"));
75 NoLazyCompilation("disable-lazy-compilation",
76 cl::desc("Disable JIT lazy compilation"),
80 static ExecutionEngine
*EE
= 0;
82 static void do_shutdown() {
87 //===----------------------------------------------------------------------===//
88 // main Driver function
90 int main(int argc
, char **argv
, char * const *envp
) {
91 sys::PrintStackTraceOnErrorSignal();
92 PrettyStackTraceProgram
X(argc
, argv
);
94 atexit(do_shutdown
); // Call llvm_shutdown() on exit.
95 cl::ParseCommandLineOptions(argc
, argv
,
96 "llvm interpreter & dynamic compiler\n");
98 // If the user doesn't want core files, disable them.
100 sys::Process::PreventCoreFiles();
102 // Load the bitcode...
103 std::string ErrorMsg
;
104 ModuleProvider
*MP
= NULL
;
105 if (MemoryBuffer
*Buffer
= MemoryBuffer::getFileOrSTDIN(InputFile
,&ErrorMsg
)) {
106 MP
= getBitcodeModuleProvider(Buffer
, &ErrorMsg
);
107 if (!MP
) delete Buffer
;
111 std::cerr
<< argv
[0] << ": error loading program '" << InputFile
<< "': "
116 // Get the module as the MP could go away once EE takes over.
117 Module
*Mod
= NoLazyCompilation
118 ? MP
->materializeModule(&ErrorMsg
) : MP
->getModule();
120 std::cerr
<< argv
[0] << ": bitcode didn't read correctly.\n";
121 std::cerr
<< "Reason: " << ErrorMsg
<< "\n";
125 // If we are supposed to override the target triple, do so now.
126 if (!TargetTriple
.empty())
127 Mod
->setTargetTriple(TargetTriple
);
129 CodeGenOpt::Level OLvl
= CodeGenOpt::Default
;
132 std::cerr
<< argv
[0] << ": invalid optimization level.\n";
135 case '0': OLvl
= CodeGenOpt::None
; break;
137 case '2': OLvl
= CodeGenOpt::Default
; break;
138 case '3': OLvl
= CodeGenOpt::Aggressive
; break;
141 EE
= ExecutionEngine::create(MP
, ForceInterpreter
, &ErrorMsg
, OLvl
);
142 if (!EE
&& !ErrorMsg
.empty()) {
143 std::cerr
<< argv
[0] << ":error creating EE: " << ErrorMsg
<< "\n";
147 if (NoLazyCompilation
)
148 EE
->DisableLazyCompilation();
150 // If the user specifically requested an argv[0] to pass into the program,
152 if (!FakeArgv0
.empty()) {
153 InputFile
= FakeArgv0
;
155 // Otherwise, if there is a .bc suffix on the executable strip it off, it
156 // might confuse the program.
157 if (InputFile
.rfind(".bc") == InputFile
.length() - 3)
158 InputFile
.erase(InputFile
.length() - 3);
161 // Add the module's name to the start of the vector of arguments to main().
162 InputArgv
.insert(InputArgv
.begin(), InputFile
);
164 // Call the main function from M as if its signature were:
165 // int main (int argc, char **argv, const char **envp)
166 // using the contents of Args to determine argc & argv, and the contents of
167 // EnvVars to determine envp.
169 Function
*EntryFn
= Mod
->getFunction(EntryFunc
);
171 std::cerr
<< '\'' << EntryFunc
<< "\' function not found in module.\n";
175 // If the program doesn't explicitly call exit, we will need the Exit
176 // function later on to make an explicit call, so get the function now.
177 Constant
*Exit
= Mod
->getOrInsertFunction("exit", Type::VoidTy
,
178 Type::Int32Ty
, NULL
);
180 // Reset errno to zero on entry to main.
183 // Run static constructors.
184 EE
->runStaticConstructorsDestructors(false);
186 if (NoLazyCompilation
) {
187 for (Module::iterator I
= Mod
->begin(), E
= Mod
->end(); I
!= E
; ++I
) {
189 if (Fn
!= EntryFn
&& !Fn
->isDeclaration())
190 EE
->getPointerToFunction(Fn
);
195 int Result
= EE
->runFunctionAsMain(EntryFn
, InputArgv
, envp
);
197 // Run static destructors.
198 EE
->runStaticConstructorsDestructors(true);
200 // If the program didn't call exit explicitly, we should call it now.
201 // This ensures that any atexit handlers get called correctly.
202 if (Function
*ExitF
= dyn_cast
<Function
>(Exit
)) {
203 std::vector
<GenericValue
> Args
;
204 GenericValue ResultGV
;
205 ResultGV
.IntVal
= APInt(32, Result
);
206 Args
.push_back(ResultGV
);
207 EE
->runFunction(ExitF
, Args
);
208 std::cerr
<< "ERROR: exit(" << Result
<< ") returned!\n";
211 std::cerr
<< "ERROR: exit defined with wrong prototype!\n";