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 void *CreateArgv(ExecutionEngine *EE,
48 const std::vector<std::string> &InputArgv) {
49 unsigned PtrSize = EE->getTargetData().getPointerSize();
50 char *Result = new char[(InputArgv.size()+1)*PtrSize];
52 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
53 const Type *SBytePtr = PointerType::get(Type::SByteTy);
55 for (unsigned i = 0; i != InputArgv.size(); ++i) {
56 unsigned Size = InputArgv[i].size()+1;
57 char *Dest = new char[Size];
58 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
60 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
63 // Endian safe: Result[i] = (PointerTy)Dest;
64 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize),
69 EE->StoreValueToMemory(PTOGV(0),
70 (GenericValue*)(Result+InputArgv.size()*PtrSize),
75 //===----------------------------------------------------------------------===//
76 // main Driver function
78 int main(int argc, char **argv, char * const *envp) {
79 cl::ParseCommandLineOptions(argc, argv,
80 " llvm interpreter & dynamic compiler\n");
82 // Load the bytecode...
84 ModuleProvider *MP = 0;
86 MP = getBytecodeModuleProvider(InputFile);
87 } catch (std::string &err) {
88 std::cerr << "Error loading program '" << InputFile << "': " << err << "\n";
93 ExecutionEngine::create(MP, ForceInterpreter);
94 assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
96 // If the user specifically requested an argv[0] to pass into the program, do
98 if (!FakeArgv0.empty()) {
99 InputFile = FakeArgv0;
101 // Otherwise, if there is a .bc suffix on the executable strip it off, it
102 // might confuse the program.
103 if (InputFile.rfind(".bc") == InputFile.length() - 3)
104 InputFile.erase(InputFile.length() - 3);
107 // Add the module's name to the start of the vector of arguments to main().
108 InputArgv.insert(InputArgv.begin(), InputFile);
110 // Call the main function from M as if its signature were:
111 // int main (int argc, char **argv, const char **envp)
112 // using the contents of Args to determine argc & argv, and the contents of
113 // EnvVars to determine envp.
115 Function *Fn = MP->getModule()->getMainFunction();
117 std::cerr << "'main' function not found in module.\n";
121 std::vector<GenericValue> GVArgs;
123 GVArgc.IntVal = InputArgv.size();
124 GVArgs.push_back(GVArgc); // Arg #0 = argc.
125 GVArgs.push_back(PTOGV(CreateArgv(EE, InputArgv))); // Arg #1 = argv.
126 assert(((char **)GVTOP(GVArgs[1]))[0] && "argv[0] was null after CreateArgv");
128 std::vector<std::string> EnvVars;
129 for (unsigned i = 0; envp[i]; ++i)
130 EnvVars.push_back(envp[i]);
131 GVArgs.push_back(PTOGV(CreateArgv(EE, EnvVars))); // Arg #2 = envp.
132 GenericValue Result = EE->runFunction(Fn, GVArgs);
134 // If the program didn't explicitly call exit, call exit now, for the program.
135 // This ensures that any atexit handlers get called correctly.
136 Function *Exit = MP->getModule()->getOrInsertFunction("exit", Type::VoidTy,
140 GVArgs.push_back(Result);
141 EE->runFunction(Exit, GVArgs);
143 std::cerr << "ERROR: exit(" << Result.IntVal << ") returned!\n";