blob: 9f1ecc2978b91dae16ced9b5b02e8fd3b8682e9f [file] [log] [blame]
Chris Lattnera0d7b082002-12-23 23:59:41 +00001//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-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 Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerd7ff5782001-08-23 17:05:04 +00009//
Chris Lattner141ea3a2003-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 Lattnerd7ff5782001-08-23 17:05:04 +000013//
14//===----------------------------------------------------------------------===//
15
Chris Lattnerad481312003-09-05 20:08:15 +000016#include "llvm/Module.h"
Misha Brukman5b255e52003-10-14 21:39:53 +000017#include "llvm/ModuleProvider.h"
Chris Lattner99958ce2003-12-26 06:49:53 +000018#include "llvm/Type.h"
Chris Lattnerad481312003-09-05 20:08:15 +000019#include "llvm/Bytecode/Reader.h"
Chris Lattner5af6a3f2006-08-01 22:34:35 +000020#include "llvm/CodeGen/LinkAllCodegenComponents.h"
Jeff Cohen0eafbc32006-03-24 02:53:49 +000021#include "llvm/ExecutionEngine/JIT.h"
22#include "llvm/ExecutionEngine/Interpreter.h"
Brian Gaekee99ca442003-09-05 19:42:34 +000023#include "llvm/ExecutionEngine/GenericValue.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
Chris Lattner76d46322006-12-06 01:18:01 +000025#include "llvm/Support/ManagedStatic.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000026#include "llvm/Support/PluginLoader.h"
Chris Lattner3f0ffd32006-09-14 06:17:09 +000027#include "llvm/System/Process.h"
Chris Lattner278f5152004-05-27 05:41:36 +000028#include "llvm/System/Signals.h"
Reid Spencerf0ebb252004-07-04 12:20:55 +000029#include <iostream>
Chris Lattnerd7ff5782001-08-23 17:05:04 +000030
Brian Gaeke960707c2003-11-11 22:41:34 +000031using namespace llvm;
32
Chris Lattnera0d7b082002-12-23 23:59:41 +000033namespace {
34 cl::opt<std::string>
35 InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000036
Chris Lattnera0d7b082002-12-23 23:59:41 +000037 cl::list<std::string>
38 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
Chris Lattnerf5cad152002-07-22 02:10:13 +000039
Chris Lattnera0d7b082002-12-23 23:59:41 +000040 cl::opt<bool> ForceInterpreter("force-interpreter",
Misha Brukmanc2186e32003-09-25 18:10:34 +000041 cl::desc("Force interpretation: disable JIT"),
42 cl::init(false));
Chris Lattner76766cb2005-12-16 05:00:21 +000043 cl::opt<std::string>
Chris Lattner78e9e102005-12-16 05:19:18 +000044 TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner76766cb2005-12-16 05:00:21 +000045
Chris Lattner55644062003-10-28 22:51:44 +000046 cl::opt<std::string>
47 FakeArgv0("fake-argv0",
48 cl::desc("Override the 'argv[0]' value passed into the executing"
49 " program"), cl::value_desc("executable"));
Chris Lattner3f0ffd32006-09-14 06:17:09 +000050
51 cl::opt<bool>
52 DisableCoreFiles("disable-core-files", cl::Hidden,
53 cl::desc("Disable emission of core files if possible"));
Chris Lattnera0d7b082002-12-23 23:59:41 +000054}
Chris Lattner009f8102001-10-27 08:43:52 +000055
Chris Lattnerd7ff5782001-08-23 17:05:04 +000056//===----------------------------------------------------------------------===//
Chris Lattnerd7ff5782001-08-23 17:05:04 +000057// main Driver function
58//
Misha Brukman5b255e52003-10-14 21:39:53 +000059int main(int argc, char **argv, char * const *envp) {
Chris Lattner61614522006-12-10 19:01:52 +000060 atexit(llvm_shutdown); // Call llvm_shutdown() on exit.
Misha Brukman5b255e52003-10-14 21:39:53 +000061 try {
Reid Spencer996ec722004-12-30 05:36:08 +000062 cl::ParseCommandLineOptions(argc, argv,
63 " llvm interpreter & dynamic compiler\n");
64 sys::PrintStackTraceOnErrorSignal();
65
Chris Lattner3f0ffd32006-09-14 06:17:09 +000066 // If the user doesn't want core files, disable them.
67 if (DisableCoreFiles)
68 sys::Process::PreventCoreFiles();
69
Reid Spencer996ec722004-12-30 05:36:08 +000070 // Load the bytecode...
71 std::string ErrorMsg;
72 ModuleProvider *MP = 0;
Reid Spencerf25aebf2006-08-25 17:43:11 +000073 MP = getBytecodeModuleProvider(InputFile, &ErrorMsg);
74 if (!MP) {
Chris Lattnerd0eb1d12006-03-08 18:43:36 +000075 std::cerr << "Error loading program '" << InputFile << "': "
Reid Spencerf25aebf2006-08-25 17:43:11 +000076 << ErrorMsg << "\n";
Reid Spencer996ec722004-12-30 05:36:08 +000077 exit(1);
78 }
79
Chris Lattner76766cb2005-12-16 05:00:21 +000080 // If we are supposed to override the target triple, do so now.
81 if (!TargetTriple.empty())
82 MP->getModule()->setTargetTriple(TargetTriple);
83
Reid Spencer996ec722004-12-30 05:36:08 +000084 ExecutionEngine *EE = ExecutionEngine::create(MP, ForceInterpreter);
Chris Lattnerd0eb1d12006-03-08 18:43:36 +000085 assert(EE &&"Couldn't create an ExecutionEngine, not even an interpreter?");
Reid Spencer996ec722004-12-30 05:36:08 +000086
Chris Lattnerd0eb1d12006-03-08 18:43:36 +000087 // If the user specifically requested an argv[0] to pass into the program,
88 // do it now.
Reid Spencer996ec722004-12-30 05:36:08 +000089 if (!FakeArgv0.empty()) {
90 InputFile = FakeArgv0;
91 } else {
92 // Otherwise, if there is a .bc suffix on the executable strip it off, it
93 // might confuse the program.
94 if (InputFile.rfind(".bc") == InputFile.length() - 3)
95 InputFile.erase(InputFile.length() - 3);
96 }
97
98 // Add the module's name to the start of the vector of arguments to main().
99 InputArgv.insert(InputArgv.begin(), InputFile);
100
101 // Call the main function from M as if its signature were:
102 // int main (int argc, char **argv, const char **envp)
103 // using the contents of Args to determine argc & argv, and the contents of
104 // EnvVars to determine envp.
105 //
106 Function *Fn = MP->getModule()->getMainFunction();
Chris Lattner352630142005-12-02 19:00:22 +0000107 if (!Fn) {
Reid Spencer996ec722004-12-30 05:36:08 +0000108 std::cerr << "'main' function not found in module.\n";
109 return -1;
110 }
111
Chris Lattnerd0eb1d12006-03-08 18:43:36 +0000112 // Run static constructors.
113 EE->runStaticConstructorsDestructors(false);
114
115 // Run main.
Reid Spencer996ec722004-12-30 05:36:08 +0000116 int Result = EE->runFunctionAsMain(Fn, InputArgv, envp);
117
Chris Lattnerd0eb1d12006-03-08 18:43:36 +0000118 // Run static destructors.
119 EE->runStaticConstructorsDestructors(true);
120
Chris Lattnere05182c2007-01-08 07:36:34 +0000121 // If the program didn't explicitly call exit, call exit now, for the
122 // program. This ensures that any atexit handlers get called correctly.
123 Constant *Exit = MP->getModule()->getOrInsertFunction("exit", Type::VoidTy,
124 Type::Int32Ty, NULL);
125 if (Function *ExitF = dyn_cast<Function>(Exit)) {
126 std::vector<GenericValue> Args;
127 GenericValue ResultGV;
128 ResultGV.Int32Val = Result;
129 Args.push_back(ResultGV);
130 EE->runFunction(ExitF, Args);
131 std::cerr << "ERROR: exit(" << Result << ") returned!\n";
132 abort();
133 } else {
134 std::cerr << "ERROR: exit defined with wrong prototype!\n";
135 abort();
136 }
Reid Spencer996ec722004-12-30 05:36:08 +0000137 } catch (const std::string& msg) {
138 std::cerr << argv[0] << ": " << msg << "\n";
139 } catch (...) {
140 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000141 }
Chris Lattner079f4d52003-12-26 06:14:47 +0000142 abort();
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000143}