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