1 //===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
3 // The LLVM Compiler Infrastructure
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.
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/DerivedTypes.h"
17 #include "llvm/Module.h"
18 #include "llvm/ModuleProvider.h"
19 #include "llvm/Bytecode/Reader.h"
20 #include "llvm/ExecutionEngine/ExecutionEngine.h"
21 #include "llvm/ExecutionEngine/GenericValue.h"
22 #include "llvm/Target/TargetMachineImpls.h"
23 #include "llvm/Target/TargetData.h"
24 #include "Support/CommandLine.h"
25 #include "Support/Debug.h"
26 #include "Support/SystemUtils.h"
32 InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
35 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
37 cl::opt<bool> ForceInterpreter("force-interpreter",
38 cl::desc("Force interpretation: disable JIT"),
42 FakeArgv0("fake-argv0",
43 cl::desc("Override the 'argv[0]' value passed into the executing"
44 " program"), cl::value_desc("executable"));
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]);
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");
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");
65 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
68 // Endian safe: Result[i] = (PointerTy)Dest;
69 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i),
72 Result[InputArgv.size()] = 0;
74 } else { // 32 bit target?
75 int *Result = new int[InputArgv.size()+1];
76 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
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");
83 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
86 // Endian safe: Result[i] = (PointerTy)Dest;
87 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i),
90 Result[InputArgv.size()] = 0; // null terminate it
95 //===----------------------------------------------------------------------===//
96 // main Driver function
98 int main(int argc, char **argv, char * const *envp) {
99 cl::ParseCommandLineOptions(argc, argv,
100 " llvm interpreter & dynamic compiler\n");
102 // Load the bytecode...
103 std::string ErrorMsg;
104 ModuleProvider *MP = 0;
106 MP = getBytecodeModuleProvider(InputFile);
107 } catch (std::string &err) {
108 std::cerr << "Error loading program '" << InputFile << "': " << err << "\n";
112 ExecutionEngine *EE =
113 ExecutionEngine::create(MP, ForceInterpreter);
114 assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
116 // If the user specifically requested an argv[0] to pass into the program, do
118 if (!FakeArgv0.empty()) {
119 InputFile = FakeArgv0;
121 // Otherwise, if there is a .bc suffix on the executable strip it off, it
122 // might confuse the program.
123 if (InputFile.rfind(".bc") == InputFile.length() - 3)
124 InputFile.erase(InputFile.length() - 3);
127 // Add the module's name to the start of the vector of arguments to main().
128 InputArgv.insert(InputArgv.begin(), InputFile);
130 // Call the main function from M as if its signature were:
131 // int main (int argc, char **argv, const char **envp)
132 // using the contents of Args to determine argc & argv, and the contents of
133 // EnvVars to determine envp.
135 Function *Fn = MP->getModule()->getMainFunction();
137 std::cerr << "'main' function not found in module.\n";
141 std::vector<GenericValue> GVArgs;
143 GVArgc.IntVal = InputArgv.size();
144 GVArgs.push_back(GVArgc); // Arg #0 = argc.
145 GVArgs.push_back(PTOGV(CreateArgv(EE, InputArgv))); // Arg #1 = argv.
146 assert(((char **)GVTOP(GVArgs[1]))[0] && "argv[0] was null after CreateArgv");
148 std::vector<std::string> EnvVars = makeStringVector(envp);
149 GVArgs.push_back(PTOGV(CreateArgv(EE, EnvVars))); // Arg #2 = envp.
150 GenericValue Result = EE->runFunction(Fn, GVArgs);
152 // If the program didn't explicitly call exit, call exit now, for the program.
153 // This ensures that any atexit handlers get called correctly.
154 Function *Exit = MP->getModule()->getOrInsertFunction("exit", Type::VoidTy,
158 GVArgs.push_back(Result);
159 EE->runFunction(Exit, GVArgs);
161 std::cerr << "ERROR: exit(" << Result.IntVal << ") returned!\n";