blob: 998e7616813a94823a1eb637c2d705b0d02b64ea [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"
Chris Lattnerd3a680a2006-08-01 22:34:35 +000020#include "llvm/CodeGen/LinkAllCodegenComponents.h"
Jeff Cohen2f519142006-03-24 02:53:49 +000021#include "llvm/ExecutionEngine/JIT.h"
22#include "llvm/ExecutionEngine/Interpreter.h"
Brian Gaeked1cab3e2003-09-05 19:42:34 +000023#include "llvm/ExecutionEngine/GenericValue.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000025#include "llvm/Support/ManagedStatic.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/PluginLoader.h"
Chris Lattner43f249a2006-09-14 06:17:09 +000027#include "llvm/System/Process.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000028#include "llvm/System/Signals.h"
Reid Spencer86f42bd2004-07-04 12:20:55 +000029#include <iostream>
Chris Lattner92101ac2001-08-23 17:05:04 +000030
Brian Gaeked0fde302003-11-11 22:41:34 +000031using namespace llvm;
32
Chris Lattnerfe11a972002-12-23 23:59:41 +000033namespace {
34 cl::opt<std::string>
35 InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000036
Chris Lattnerfe11a972002-12-23 23:59:41 +000037 cl::list<std::string>
38 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
Chris Lattner5ff62e92002-07-22 02:10:13 +000039
Chris Lattnerfe11a972002-12-23 23:59:41 +000040 cl::opt<bool> ForceInterpreter("force-interpreter",
Misha Brukman3d8a54d2003-09-25 18:10:34 +000041 cl::desc("Force interpretation: disable JIT"),
42 cl::init(false));
Chris Lattner3015e602005-12-16 05:00:21 +000043 cl::opt<std::string>
Chris Lattner60844d42005-12-16 05:19:18 +000044 TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner3015e602005-12-16 05:00:21 +000045
Chris Lattnere69671d2003-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 Lattner43f249a2006-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 Lattnerfe11a972002-12-23 23:59:41 +000054}
Chris Lattner43e3f7c2001-10-27 08:43:52 +000055
Chris Lattner92101ac2001-08-23 17:05:04 +000056//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +000057// main Driver function
58//
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000059int main(int argc, char **argv, char * const *envp) {
Chris Lattnerca3aa262006-12-10 19:01:52 +000060 atexit(llvm_shutdown); // Call llvm_shutdown() on exit.
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000061 try {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000062 cl::ParseCommandLineOptions(argc, argv,
63 " llvm interpreter & dynamic compiler\n");
64 sys::PrintStackTraceOnErrorSignal();
65
Chris Lattner43f249a2006-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 Spencer1ef8bda2004-12-30 05:36:08 +000070 // Load the bytecode...
71 std::string ErrorMsg;
72 ModuleProvider *MP = 0;
Reid Spencer0b5a5042006-08-25 17:43:11 +000073 MP = getBytecodeModuleProvider(InputFile, &ErrorMsg);
74 if (!MP) {
Chris Lattner42d0b452006-03-08 18:43:36 +000075 std::cerr << "Error loading program '" << InputFile << "': "
Reid Spencer0b5a5042006-08-25 17:43:11 +000076 << ErrorMsg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000077 exit(1);
78 }
79
Chris Lattner3015e602005-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 Spencer1ef8bda2004-12-30 05:36:08 +000084 ExecutionEngine *EE = ExecutionEngine::create(MP, ForceInterpreter);
Chris Lattner42d0b452006-03-08 18:43:36 +000085 assert(EE &&"Couldn't create an ExecutionEngine, not even an interpreter?");
Reid Spencer1ef8bda2004-12-30 05:36:08 +000086
Chris Lattner42d0b452006-03-08 18:43:36 +000087 // If the user specifically requested an argv[0] to pass into the program,
88 // do it now.
Reid Spencer1ef8bda2004-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 Lattner52098892005-12-02 19:00:22 +0000107 if (!Fn) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000108 std::cerr << "'main' function not found in module.\n";
109 return -1;
110 }
111
Chris Lattner42d0b452006-03-08 18:43:36 +0000112 // Run static constructors.
113 EE->runStaticConstructorsDestructors(false);
114
115 // Run main.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000116 int Result = EE->runFunctionAsMain(Fn, InputArgv, envp);
117
Chris Lattner42d0b452006-03-08 18:43:36 +0000118 // Run static destructors.
119 EE->runStaticConstructorsDestructors(true);
120
Chris Lattnerf74edf22007-01-07 06:43:08 +0000121 exit(Result);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000122 std::cerr << "ERROR: exit(" << Result << ") returned!\n";
123 abort();
124 } catch (const std::string& msg) {
125 std::cerr << argv[0] << ": " << msg << "\n";
126 } catch (...) {
127 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner92101ac2001-08-23 17:05:04 +0000128 }
Chris Lattnerefec9662003-12-26 06:14:47 +0000129 abort();
Chris Lattner92101ac2001-08-23 17:05:04 +0000130}