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