blob: c1b7478a3769ba4372eeac91b6378c46c0ce4ab2 [file] [log] [blame]
Chris Lattnerfe11a972002-12-23 23:59:41 +00001//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// 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.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
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"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/PluginLoader.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000024#include "llvm/System/Signals.h"
Reid Spencer86f42bd2004-07-04 12:20:55 +000025#include <iostream>
Chris Lattner92101ac2001-08-23 17:05:04 +000026
Brian Gaeked0fde302003-11-11 22:41:34 +000027using namespace llvm;
28
Chris Lattnerfe11a972002-12-23 23:59:41 +000029namespace {
30 cl::opt<std::string>
31 InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000032
Chris Lattnerfe11a972002-12-23 23:59:41 +000033 cl::list<std::string>
34 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
Chris Lattner5ff62e92002-07-22 02:10:13 +000035
Chris Lattnerfe11a972002-12-23 23:59:41 +000036 cl::opt<bool> ForceInterpreter("force-interpreter",
Misha Brukman3d8a54d2003-09-25 18:10:34 +000037 cl::desc("Force interpretation: disable JIT"),
38 cl::init(false));
Chris Lattnere69671d2003-10-28 22:51:44 +000039
40 cl::opt<std::string>
41 FakeArgv0("fake-argv0",
42 cl::desc("Override the 'argv[0]' value passed into the executing"
43 " program"), cl::value_desc("executable"));
Chris Lattnerfe11a972002-12-23 23:59:41 +000044}
Chris Lattner43e3f7c2001-10-27 08:43:52 +000045
Chris Lattner92101ac2001-08-23 17:05:04 +000046//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +000047// main Driver function
48//
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000049int main(int argc, char **argv, char * const *envp) {
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000050 try {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000051 cl::ParseCommandLineOptions(argc, argv,
52 " llvm interpreter & dynamic compiler\n");
53 sys::PrintStackTraceOnErrorSignal();
54
55 // Load the bytecode...
56 std::string ErrorMsg;
57 ModuleProvider *MP = 0;
58 try {
59 MP = getBytecodeModuleProvider(InputFile);
60 } catch (std::string &err) {
61 std::cerr << "Error loading program '" << InputFile << "': " << err << "\n";
62 exit(1);
63 }
64
65 ExecutionEngine *EE = ExecutionEngine::create(MP, ForceInterpreter);
66 assert(EE && "Couldn't create an ExecutionEngine, not even an interpreter?");
67
68 // If the user specifically requested an argv[0] to pass into the program, do
69 // it now.
70 if (!FakeArgv0.empty()) {
71 InputFile = FakeArgv0;
72 } else {
73 // Otherwise, if there is a .bc suffix on the executable strip it off, it
74 // might confuse the program.
75 if (InputFile.rfind(".bc") == InputFile.length() - 3)
76 InputFile.erase(InputFile.length() - 3);
77 }
78
79 // Add the module's name to the start of the vector of arguments to main().
80 InputArgv.insert(InputArgv.begin(), InputFile);
81
82 // Call the main function from M as if its signature were:
83 // int main (int argc, char **argv, const char **envp)
84 // using the contents of Args to determine argc & argv, and the contents of
85 // EnvVars to determine envp.
86 //
87 Function *Fn = MP->getModule()->getMainFunction();
Chris Lattner52098892005-12-02 19:00:22 +000088 if (!Fn) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000089 std::cerr << "'main' function not found in module.\n";
90 return -1;
91 }
92
93 // Run main...
94 int Result = EE->runFunctionAsMain(Fn, InputArgv, envp);
95
96 // If the program didn't explicitly call exit, call exit now, for the program.
97 // This ensures that any atexit handlers get called correctly.
98 Function *Exit = MP->getModule()->getOrInsertFunction("exit", Type::VoidTy,
Jeff Cohen66c5fd62005-10-23 04:37:20 +000099 Type::IntTy,
100 (Type *)0);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000101
102 std::vector<GenericValue> Args;
103 GenericValue ResultGV;
104 ResultGV.IntVal = Result;
105 Args.push_back(ResultGV);
106 EE->runFunction(Exit, Args);
107
108 std::cerr << "ERROR: exit(" << Result << ") returned!\n";
109 abort();
110 } catch (const std::string& msg) {
111 std::cerr << argv[0] << ": " << msg << "\n";
112 } catch (...) {
113 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000114 }
Chris Lattnerefec9662003-12-26 06:14:47 +0000115 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000116}