blob: 518c1a1d6eca66f0478cc2e6171cd7a987186a81 [file] [log] [blame]
Chris Lattnerfe11a972002-12-23 23:59:41 +00001//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +00009//
Chris Lattner7efea1d2003-12-26 05:07:35 +000010// 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.
Chris Lattner92101ac2001-08-23 17:05:04 +000013//
14//===----------------------------------------------------------------------===//
15
Chris Lattnerfd131292003-09-05 20:08:15 +000016#include "llvm/Module.h"
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000017#include "llvm/ModuleProvider.h"
Chris Lattner269a4282003-12-26 06:49:53 +000018#include "llvm/Type.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000019#include "llvm/Bytecode/Reader.h"
Brian Gaeked1cab3e2003-09-05 19:42:34 +000020#include "llvm/ExecutionEngine/ExecutionEngine.h"
21#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000022#include "Support/CommandLine.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000023
Brian Gaeked0fde302003-11-11 22:41:34 +000024using namespace llvm;
25
Chris Lattnerfe11a972002-12-23 23:59:41 +000026namespace {
27 cl::opt<std::string>
28 InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000029
Chris Lattnerfe11a972002-12-23 23:59:41 +000030 cl::list<std::string>
31 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
Chris Lattner5ff62e92002-07-22 02:10:13 +000032
Chris Lattnerfe11a972002-12-23 23:59:41 +000033 cl::opt<bool> ForceInterpreter("force-interpreter",
Misha Brukman3d8a54d2003-09-25 18:10:34 +000034 cl::desc("Force interpretation: disable JIT"),
35 cl::init(false));
Chris Lattnere69671d2003-10-28 22:51:44 +000036
37 cl::opt<std::string>
38 FakeArgv0("fake-argv0",
39 cl::desc("Override the 'argv[0]' value passed into the executing"
40 " program"), cl::value_desc("executable"));
Chris Lattnerfe11a972002-12-23 23:59:41 +000041}
Chris Lattner43e3f7c2001-10-27 08:43:52 +000042
Chris Lattner92101ac2001-08-23 17:05:04 +000043//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +000044// main Driver function
45//
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000046int main(int argc, char **argv, char * const *envp) {
Chris Lattnerfe11a972002-12-23 23:59:41 +000047 cl::ParseCommandLineOptions(argc, argv,
Misha Brukman3d8a54d2003-09-25 18:10:34 +000048 " llvm interpreter & dynamic compiler\n");
Chris Lattner92101ac2001-08-23 17:05:04 +000049
Chris Lattnerfe11a972002-12-23 23:59:41 +000050 // Load the bytecode...
Chris Lattnerd6840ac2002-12-24 00:39:16 +000051 std::string ErrorMsg;
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000052 ModuleProvider *MP = 0;
53 try {
54 MP = getBytecodeModuleProvider(InputFile);
55 } catch (std::string &err) {
Brian Gaeke3f6e7982003-12-12 00:47:19 +000056 std::cerr << "Error loading program '" << InputFile << "': " << err << "\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 Gaeke2f828c32003-10-24 20:00:17 +000061 ExecutionEngine::create(MP, ForceInterpreter);
Misha Brukman3d8a54d2003-09-25 18:10:34 +000062 assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
Chris Lattnerfe11a972002-12-23 23:59:41 +000063
Chris Lattnere69671d2003-10-28 22:51:44 +000064 // If the user specifically requested an argv[0] to pass into the program, do
65 // it now.
66 if (!FakeArgv0.empty()) {
67 InputFile = FakeArgv0;
68 } else {
69 // Otherwise, if there is a .bc suffix on the executable strip it off, it
70 // might confuse the program.
71 if (InputFile.rfind(".bc") == InputFile.length() - 3)
72 InputFile.erase(InputFile.length() - 3);
Brian Gaeke6ae73dc2003-05-23 20:28:07 +000073 }
Chris Lattnere69671d2003-10-28 22:51:44 +000074
75 // Add the module's name to the start of the vector of arguments to main().
Chris Lattnerfe11a972002-12-23 23:59:41 +000076 InputArgv.insert(InputArgv.begin(), InputFile);
77
Chris Lattnerefec9662003-12-26 06:14:47 +000078 // Call the main function from M as if its signature were:
79 // int main (int argc, char **argv, const char **envp)
80 // using the contents of Args to determine argc & argv, and the contents of
81 // EnvVars to determine envp.
82 //
83 Function *Fn = MP->getModule()->getMainFunction();
84 if (!Fn) {
85 std::cerr << "'main' function not found in module.\n";
86 return -1;
87 }
Chris Lattnerfe11a972002-12-23 23:59:41 +000088
Chris Lattner269a4282003-12-26 06:49:53 +000089 // Run main...
90 int Result = EE->runFunctionAsMain(Fn, InputArgv, envp);
Chris Lattnerefec9662003-12-26 06:14:47 +000091
92 // If the program didn't explicitly call exit, call exit now, for the program.
93 // This ensures that any atexit handlers get called correctly.
94 Function *Exit = MP->getModule()->getOrInsertFunction("exit", Type::VoidTy,
95 Type::IntTy, 0);
Chris Lattnerefec9662003-12-26 06:14:47 +000096
Chris Lattner269a4282003-12-26 06:49:53 +000097 std::vector<GenericValue> Args;
98 GenericValue ResultGV;
99 ResultGV.IntVal = Result;
100 Args.push_back(ResultGV);
101 EE->runFunction(Exit, Args);
102
103 std::cerr << "ERROR: exit(" << Result << ") returned!\n";
Chris Lattnerefec9662003-12-26 06:14:47 +0000104 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000105}