TraceMode, as you may have heard, is history.
[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 way to execute LLVM bytecode without static
11 // compilation.  This consists of a very simple and slow (but portable)
12 // interpreter, along with capability for system specific dynamic compilers.  At
13 // runtime, the fastest (stable) execution engine is selected to run the
14 // program.  This means the JIT compiler for the current platform if it's
15 // available.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Module.h"
21 #include "llvm/ModuleProvider.h"
22 #include "llvm/Bytecode/Reader.h"
23 #include "llvm/ExecutionEngine/ExecutionEngine.h"
24 #include "llvm/ExecutionEngine/GenericValue.h"
25 #include "llvm/Target/TargetMachineImpls.h"
26 #include "llvm/Target/TargetData.h"
27 #include "Support/CommandLine.h"
28 #include "Support/Debug.h"
29 #include "Support/SystemUtils.h"
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<std::string>
39   MainFunction("f", cl::desc("Function to execute"), cl::init("main"),
40                cl::value_desc("function name"));
41
42   cl::opt<bool> ForceInterpreter("force-interpreter",
43                                  cl::desc("Force interpretation: disable JIT"),
44                                  cl::init(false));
45 }
46
47 static std::vector<std::string> makeStringVector(char * const *envp) {
48   std::vector<std::string> rv;
49   for (unsigned i = 0; envp[i]; ++i)
50     rv.push_back(envp[i]);
51   return rv;
52 }
53
54 static void *CreateArgv(ExecutionEngine *EE,
55                         const std::vector<std::string> &InputArgv) {
56   if (EE->getTargetData().getPointerSize() == 8) {   // 64 bit target?
57     PointerTy *Result = new PointerTy[InputArgv.size()+1];
58     DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
59
60     for (unsigned i = 0; i < InputArgv.size(); ++i) {
61       unsigned Size = InputArgv[i].size()+1;
62       char *Dest = new char[Size];
63       DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
64       
65       std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
66       Dest[Size-1] = 0;
67       
68       // Endian safe: Result[i] = (PointerTy)Dest;
69       EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i),
70                              Type::LongTy);
71     }
72     Result[InputArgv.size()] = 0;
73     return Result;
74   } else {                                      // 32 bit target?
75     int *Result = new int[InputArgv.size()+1];
76     DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
77
78     for (unsigned i = 0; i < InputArgv.size(); ++i) {
79       unsigned Size = InputArgv[i].size()+1;
80       char *Dest = new char[Size];
81       DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
82       
83       std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
84       Dest[Size-1] = 0;
85       
86       // Endian safe: Result[i] = (PointerTy)Dest;
87       EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i),
88                              Type::IntTy);
89     }
90     Result[InputArgv.size()] = 0;  // null terminate it
91     return Result;
92   }
93 }
94
95 /// callAsMain - Call the function named FnName from M as if its
96 /// signature were int main (int argc, char **argv, const char
97 /// **envp), using the contents of Args to determine argc & argv, and
98 /// the contents of EnvVars to determine envp.  Returns the result
99 /// from calling FnName, or -1 and prints an error msg. if the named
100 /// function cannot be found.
101 ///
102 int callAsMain(ExecutionEngine *EE, ModuleProvider *MP,
103                const std::string &FnName,
104                const std::vector<std::string> &Args,
105                const std::vector<std::string> &EnvVars) {
106   Function *Fn = MP->getModule()->getNamedFunction(FnName);
107   if (!Fn) {
108     std::cerr << "Function '" << FnName << "' not found in module.\n";
109     return -1;
110   }
111   std::vector<GenericValue> GVArgs;
112   GenericValue GVArgc;
113   GVArgc.IntVal = Args.size();
114   GVArgs.push_back(GVArgc); // Arg #0 = argc.
115   GVArgs.push_back(PTOGV(CreateArgv(EE, Args))); // Arg #1 = argv.
116   GVArgs.push_back(PTOGV(CreateArgv(EE, EnvVars))); // Arg #2 = envp.
117   return EE->run(Fn, GVArgs).IntVal;
118 }
119
120 //===----------------------------------------------------------------------===//
121 // main Driver function
122 //
123 int main(int argc, char **argv, char * const *envp) {
124   cl::ParseCommandLineOptions(argc, argv,
125                               " llvm interpreter & dynamic compiler\n");
126
127   // Load the bytecode...
128   std::string ErrorMsg;
129   ModuleProvider *MP = 0;
130   try {
131     MP = getBytecodeModuleProvider(InputFile);
132   } catch (std::string &err) {
133     std::cerr << "Error parsing '" << InputFile << "': " << err << "\n";
134     exit(1);
135   }
136
137   ExecutionEngine *EE =
138     ExecutionEngine::create(MP, ForceInterpreter);
139   assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
140
141   // Add the module's name to the start of the vector of arguments to main().
142   // But delete .bc first, since programs (and users) might not expect to
143   // see it.
144   const std::string ByteCodeFileSuffix(".bc");
145   if (InputFile.rfind(ByteCodeFileSuffix) ==
146       InputFile.length() - ByteCodeFileSuffix.length()) {
147     InputFile.erase (InputFile.length() - ByteCodeFileSuffix.length());
148   }
149   InputArgv.insert(InputArgv.begin(), InputFile);
150
151   // Run the main function!
152   int ExitCode = callAsMain(EE, MP, MainFunction, InputArgv,
153                             makeStringVector(envp)); 
154
155   // Now that we are done executing the program, shut down the execution engine
156   delete EE;
157   return ExitCode;
158 }