blob: d0d1c4997b43d707ed137a94e85339ad6f417b8e [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> TraceMode("trace", cl::desc("Enable Tracing"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000030
Chris Lattnerfe11a972002-12-23 23:59:41 +000031 cl::opt<bool> ForceInterpreter("force-interpreter",
32 cl::desc("Force interpretation: disable JIT"),
Chris Lattner19669f32003-05-12 14:31:57 +000033 cl::init(false));
Chris Lattnerfe11a972002-12-23 23:59:41 +000034}
Chris Lattner43e3f7c2001-10-27 08:43:52 +000035
Chris Lattner92101ac2001-08-23 17:05:04 +000036//===----------------------------------------------------------------------===//
Chris Lattnerfe11a972002-12-23 23:59:41 +000037// ExecutionEngine Class Implementation
Chris Lattner92101ac2001-08-23 17:05:04 +000038//
Chris Lattner92101ac2001-08-23 17:05:04 +000039
Chris Lattnerfe11a972002-12-23 23:59:41 +000040ExecutionEngine::~ExecutionEngine() {
41 delete &CurMod;
Chris Lattner92101ac2001-08-23 17:05:04 +000042}
43
44//===----------------------------------------------------------------------===//
45// main Driver function
46//
John Criswell69582b32003-08-21 21:12:30 +000047int main(int argc, char** argv, const char ** envp) {
Chris Lattnerfe11a972002-12-23 23:59:41 +000048 cl::ParseCommandLineOptions(argc, argv,
49 " llvm interpreter & dynamic compiler\n");
Chris Lattner92101ac2001-08-23 17:05:04 +000050
Chris Lattnerfe11a972002-12-23 23:59:41 +000051 // Load the bytecode...
Chris Lattnerd6840ac2002-12-24 00:39:16 +000052 std::string ErrorMsg;
Chris Lattnerfe11a972002-12-23 23:59:41 +000053 Module *M = ParseBytecodeFile(InputFile, &ErrorMsg);
54 if (M == 0) {
Chris Lattnerd6840ac2002-12-24 00:39:16 +000055 std::cout << "Error parsing '" << InputFile << "': "
56 << ErrorMsg << "\n";
Chris Lattnerfe11a972002-12-23 23:59:41 +000057 exit(1);
Chris Lattner92101ac2001-08-23 17:05:04 +000058 }
59
Brian Gaeke82d82772003-09-03 20:34:19 +000060 ExecutionEngine *EE =
Brian Gaekef58815e2003-09-04 22:21:24 +000061 ExecutionEngine::create (M, ForceInterpreter, TraceMode);
Brian Gaeke82d82772003-09-03 20:34:19 +000062 assert (EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
Chris Lattnerfe11a972002-12-23 23:59:41 +000063
Brian Gaekef58815e2003-09-04 22:21:24 +000064 // Add the module's name to the start of the vector of arguments to main().
Brian Gaeke6ae73dc2003-05-23 20:28:07 +000065 // But delete .bc first, since programs (and users) might not expect to
66 // see it.
67 const std::string ByteCodeFileSuffix (".bc");
68 if (InputFile.rfind (ByteCodeFileSuffix) ==
69 InputFile.length () - ByteCodeFileSuffix.length ()) {
70 InputFile.erase (InputFile.length () - ByteCodeFileSuffix.length ());
71 }
Chris Lattnerfe11a972002-12-23 23:59:41 +000072 InputArgv.insert(InputArgv.begin(), InputFile);
73
74 // Run the main function!
John Criswell69582b32003-08-21 21:12:30 +000075 int ExitCode = EE->run(MainFunction, InputArgv, envp);
Chris Lattnerfe11a972002-12-23 23:59:41 +000076
77 // Now that we are done executing the program, shut down the execution engine
78 delete EE;
79 return ExitCode;
Chris Lattner92101ac2001-08-23 17:05:04 +000080}