blob: 73052c0709071d7338b0372dc70575ba6770f6f7 [file] [log] [blame]
Chris Lattnerfe11a972002-12-23 23:59:41 +00001//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +00002//
Chris Lattnerfe11a972002-12-23 23:59:41 +00003// This utility provides a way to execute LLVM bytecode without static
4// compilation. This consists of a very simple and slow (but portable)
5// interpreter, along with capability for system specific dynamic compilers. At
6// runtime, the fastest (stable) execution engine is selected to run the
7// program. This means the JIT compiler for the current platform if it's
8// available.
Chris Lattner92101ac2001-08-23 17:05:04 +00009//
10//===----------------------------------------------------------------------===//
11
Chris Lattnerfe11a972002-12-23 23:59:41 +000012#include "ExecutionEngine.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000013#include "Support/CommandLine.h"
Chris Lattnerfe11a972002-12-23 23:59:41 +000014#include "llvm/Bytecode/Reader.h"
15#include "llvm/Module.h"
16#include "llvm/Target/TargetMachineImpls.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000017
Chris Lattnerfe11a972002-12-23 23:59:41 +000018namespace {
19 cl::opt<std::string>
20 InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000021
Chris Lattnerfe11a972002-12-23 23:59:41 +000022 cl::list<std::string>
23 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
Chris Lattner5ff62e92002-07-22 02:10:13 +000024
Chris Lattnerfe11a972002-12-23 23:59:41 +000025 cl::opt<std::string>
26 MainFunction ("f", cl::desc("Function to execute"), cl::init("main"),
27 cl::value_desc("function name"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000028
Chris Lattnerfe11a972002-12-23 23:59:41 +000029 cl::opt<bool> DebugMode("d", cl::desc("Start program in debugger"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000030
Chris Lattnerfe11a972002-12-23 23:59:41 +000031 cl::opt<bool> TraceMode("trace", cl::desc("Enable Tracing"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000032
Chris Lattnerfe11a972002-12-23 23:59:41 +000033 cl::opt<bool> ForceInterpreter("force-interpreter",
34 cl::desc("Force interpretation: disable JIT"),
35 cl::init(true));
36}
Chris Lattner43e3f7c2001-10-27 08:43:52 +000037
Chris Lattner92101ac2001-08-23 17:05:04 +000038//===----------------------------------------------------------------------===//
Chris Lattnerfe11a972002-12-23 23:59:41 +000039// ExecutionEngine Class Implementation
Chris Lattner92101ac2001-08-23 17:05:04 +000040//
Chris Lattner92101ac2001-08-23 17:05:04 +000041
Chris Lattnerfe11a972002-12-23 23:59:41 +000042ExecutionEngine::~ExecutionEngine() {
43 delete &CurMod;
Chris Lattner92101ac2001-08-23 17:05:04 +000044}
45
46//===----------------------------------------------------------------------===//
47// main Driver function
48//
49int main(int argc, char** argv) {
Chris Lattnerfe11a972002-12-23 23:59:41 +000050 cl::ParseCommandLineOptions(argc, argv,
51 " llvm interpreter & dynamic compiler\n");
Chris Lattner92101ac2001-08-23 17:05:04 +000052
Chris Lattnerfe11a972002-12-23 23:59:41 +000053 // Load the bytecode...
Chris Lattnerd6840ac2002-12-24 00:39:16 +000054 std::string ErrorMsg;
Chris Lattnerfe11a972002-12-23 23:59:41 +000055 Module *M = ParseBytecodeFile(InputFile, &ErrorMsg);
56 if (M == 0) {
Chris Lattnerd6840ac2002-12-24 00:39:16 +000057 std::cout << "Error parsing '" << InputFile << "': "
58 << ErrorMsg << "\n";
Chris Lattnerfe11a972002-12-23 23:59:41 +000059 exit(1);
Chris Lattner92101ac2001-08-23 17:05:04 +000060 }
61
Chris Lattnerfe11a972002-12-23 23:59:41 +000062#if 0
63 // Link in the runtime library for LLI...
Chris Lattnerd6840ac2002-12-24 00:39:16 +000064 std::string RuntimeLib = getCurrentExecutablePath();
Chris Lattnerfe11a972002-12-23 23:59:41 +000065 if (!RuntimeLib.empty()) RuntimeLib += "/";
66 RuntimeLib += "RuntimeLib.bc";
Chris Lattner92101ac2001-08-23 17:05:04 +000067
Chris Lattnerfe11a972002-12-23 23:59:41 +000068 if (Module *SupportLib = ParseBytecodeFile(RuntimeLib, &ErrorMsg)) {
69 if (LinkModules(M, SupportLib, &ErrorMsg))
70 std::cerr << "Error Linking runtime library into current module: "
71 << ErrorMsg << "\n";
72 } else {
73 std::cerr << "Error loading runtime library '"+RuntimeLib+"': "
74 << ErrorMsg << "\n";
75 }
76#endif
77
78 // FIXME: This should look at the PointerSize and endianness of the bytecode
79 // file to determine the endianness and pointer size of target machine to use.
80 unsigned Config = TM::PtrSize64 | TM::BigEndian;
81
82 ExecutionEngine *EE = 0;
83
84 // If there is nothing that is forcing us to use the interpreter, make a JIT.
85 if (!ForceInterpreter && !DebugMode && !TraceMode)
86 EE = ExecutionEngine::createJIT(M, Config);
87
88 // If we can't make a JIT, make an interpreter instead.
89 if (EE == 0)
90 EE = ExecutionEngine::createInterpreter(M, Config, DebugMode, TraceMode);
91
92 // Add the module name to the start of the argv vector...
93 InputArgv.insert(InputArgv.begin(), InputFile);
94
95 // Run the main function!
96 int ExitCode = EE->run(MainFunction, InputArgv);
97
98 // Now that we are done executing the program, shut down the execution engine
99 delete EE;
100 return ExitCode;
Chris Lattner92101ac2001-08-23 17:05:04 +0000101}