blob: 283bd48e87f7972b253532375f3bf839e416a2cb [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"
Jeff Cohen2f519142006-03-24 02:53:49 +000020#include "llvm/ExecutionEngine/JIT.h"
21#include "llvm/ExecutionEngine/Interpreter.h"
Brian Gaeked1cab3e2003-09-05 19:42:34 +000022#include "llvm/ExecutionEngine/GenericValue.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/PluginLoader.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000025#include "llvm/System/Signals.h"
Reid Spencer86f42bd2004-07-04 12:20:55 +000026#include <iostream>
Chris Lattner92101ac2001-08-23 17:05:04 +000027
Brian Gaeked0fde302003-11-11 22:41:34 +000028using namespace llvm;
29
Chris Lattnerfe11a972002-12-23 23:59:41 +000030namespace {
31 cl::opt<std::string>
32 InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000033
Chris Lattnerfe11a972002-12-23 23:59:41 +000034 cl::list<std::string>
35 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
Chris Lattner5ff62e92002-07-22 02:10:13 +000036
Chris Lattnerfe11a972002-12-23 23:59:41 +000037 cl::opt<bool> ForceInterpreter("force-interpreter",
Misha Brukman3d8a54d2003-09-25 18:10:34 +000038 cl::desc("Force interpretation: disable JIT"),
39 cl::init(false));
Chris Lattner3015e602005-12-16 05:00:21 +000040 cl::opt<std::string>
Chris Lattner60844d42005-12-16 05:19:18 +000041 TargetTriple("mtriple", cl::desc("Override target triple for module"));
Chris Lattner3015e602005-12-16 05:00:21 +000042
Chris Lattnere69671d2003-10-28 22:51:44 +000043 cl::opt<std::string>
44 FakeArgv0("fake-argv0",
45 cl::desc("Override the 'argv[0]' value passed into the executing"
46 " program"), cl::value_desc("executable"));
Chris Lattnerfe11a972002-12-23 23:59:41 +000047}
Chris Lattner43e3f7c2001-10-27 08:43:52 +000048
Chris Lattner92101ac2001-08-23 17:05:04 +000049//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +000050// main Driver function
51//
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000052int main(int argc, char **argv, char * const *envp) {
Misha Brukmanc4fb6fd2003-10-14 21:39:53 +000053 try {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000054 cl::ParseCommandLineOptions(argc, argv,
55 " llvm interpreter & dynamic compiler\n");
56 sys::PrintStackTraceOnErrorSignal();
57
58 // Load the bytecode...
59 std::string ErrorMsg;
60 ModuleProvider *MP = 0;
61 try {
62 MP = getBytecodeModuleProvider(InputFile);
63 } catch (std::string &err) {
Chris Lattner42d0b452006-03-08 18:43:36 +000064 std::cerr << "Error loading program '" << InputFile << "': "
65 << err << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000066 exit(1);
67 }
68
Chris Lattner3015e602005-12-16 05:00:21 +000069 // If we are supposed to override the target triple, do so now.
70 if (!TargetTriple.empty())
71 MP->getModule()->setTargetTriple(TargetTriple);
72
Reid Spencer1ef8bda2004-12-30 05:36:08 +000073 ExecutionEngine *EE = ExecutionEngine::create(MP, ForceInterpreter);
Chris Lattner42d0b452006-03-08 18:43:36 +000074 assert(EE &&"Couldn't create an ExecutionEngine, not even an interpreter?");
Reid Spencer1ef8bda2004-12-30 05:36:08 +000075
Chris Lattner42d0b452006-03-08 18:43:36 +000076 // If the user specifically requested an argv[0] to pass into the program,
77 // do it now.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000078 if (!FakeArgv0.empty()) {
79 InputFile = FakeArgv0;
80 } else {
81 // Otherwise, if there is a .bc suffix on the executable strip it off, it
82 // might confuse the program.
83 if (InputFile.rfind(".bc") == InputFile.length() - 3)
84 InputFile.erase(InputFile.length() - 3);
85 }
86
87 // Add the module's name to the start of the vector of arguments to main().
88 InputArgv.insert(InputArgv.begin(), InputFile);
89
90 // Call the main function from M as if its signature were:
91 // int main (int argc, char **argv, const char **envp)
92 // using the contents of Args to determine argc & argv, and the contents of
93 // EnvVars to determine envp.
94 //
95 Function *Fn = MP->getModule()->getMainFunction();
Chris Lattner52098892005-12-02 19:00:22 +000096 if (!Fn) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000097 std::cerr << "'main' function not found in module.\n";
98 return -1;
99 }
100
Chris Lattner42d0b452006-03-08 18:43:36 +0000101 // Run static constructors.
102 EE->runStaticConstructorsDestructors(false);
103
104 // Run main.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000105 int Result = EE->runFunctionAsMain(Fn, InputArgv, envp);
106
Chris Lattner42d0b452006-03-08 18:43:36 +0000107 // Run static destructors.
108 EE->runStaticConstructorsDestructors(true);
109
110 // If the program didn't explicitly call exit, call exit now, for the
111 // program. This ensures that any atexit handlers get called correctly.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000112 Function *Exit = MP->getModule()->getOrInsertFunction("exit", Type::VoidTy,
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000113 Type::IntTy,
114 (Type *)0);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000115
116 std::vector<GenericValue> Args;
117 GenericValue ResultGV;
118 ResultGV.IntVal = Result;
119 Args.push_back(ResultGV);
120 EE->runFunction(Exit, Args);
121
122 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}