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