Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 1 | //===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 7efea1d | 2003-12-26 05:07:35 +0000 | [diff] [blame] | 10 | // 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 |
Torok Edwin | db9c028 | 2009-07-03 12:11:32 +0000 | [diff] [blame] | 12 | // compiler, or through an interpreter if no JIT is available for this platform. |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "lli" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame^] | 17 | #include "llvm/IR/LLVMContext.h" |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 18 | #include "RecordingMemoryManager.h" |
| 19 | #include "RemoteTarget.h" |
Duncan Sands | 75ebbce | 2010-08-28 01:30:02 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Triple.h" |
Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 21 | #include "llvm/Bitcode/ReaderWriter.h" |
Chris Lattner | d3a680a | 2006-08-01 22:34:35 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/LinkAllCodegenComponents.h" |
Brian Gaeke | d1cab3e | 2003-09-05 19:42:34 +0000 | [diff] [blame] | 23 | #include "llvm/ExecutionEngine/GenericValue.h" |
Jeffrey Yasskin | df5a7da | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 24 | #include "llvm/ExecutionEngine/Interpreter.h" |
| 25 | #include "llvm/ExecutionEngine/JIT.h" |
| 26 | #include "llvm/ExecutionEngine/JITEventListener.h" |
Jim Grosbach | bf9ab93 | 2012-01-11 21:12:51 +0000 | [diff] [blame] | 27 | #include "llvm/ExecutionEngine/JITMemoryManager.h" |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 28 | #include "llvm/ExecutionEngine/MCJIT.h" |
Andrew Kaylor | 257a009 | 2012-11-27 19:49:00 +0000 | [diff] [blame] | 29 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame^] | 30 | #include "llvm/IR/Module.h" |
| 31 | #include "llvm/IR/Type.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Debug.h" |
| 34 | #include "llvm/Support/DynamicLibrary.h" |
| 35 | #include "llvm/Support/Format.h" |
Daniel Dunbar | 46a2716 | 2010-11-13 00:28:01 +0000 | [diff] [blame] | 36 | #include "llvm/Support/IRReader.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 37 | #include "llvm/Support/ManagedStatic.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 38 | #include "llvm/Support/MathExtras.h" |
| 39 | #include "llvm/Support/Memory.h" |
Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 40 | #include "llvm/Support/MemoryBuffer.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 41 | #include "llvm/Support/PluginLoader.h" |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 42 | #include "llvm/Support/PrettyStackTrace.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Process.h" |
| 44 | #include "llvm/Support/Signals.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 45 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 46 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 0204032 | 2007-04-27 17:02:33 +0000 | [diff] [blame] | 47 | #include <cerrno> |
NAKAMURA Takumi | a13d14a | 2010-10-22 14:53:59 +0000 | [diff] [blame] | 48 | |
| 49 | #ifdef __CYGWIN__ |
| 50 | #include <cygwin/version.h> |
| 51 | #if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007 |
| 52 | #define DO_NOTHING_ATEXIT 1 |
| 53 | #endif |
| 54 | #endif |
| 55 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 56 | using namespace llvm; |
| 57 | |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 58 | namespace { |
| 59 | cl::opt<std::string> |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 60 | InputFile(cl::desc("<input bitcode>"), cl::Positional, cl::init("-")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 61 | |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 62 | cl::list<std::string> |
| 63 | InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>...")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 64 | |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 65 | cl::opt<bool> ForceInterpreter("force-interpreter", |
Misha Brukman | 3d8a54d | 2003-09-25 18:10:34 +0000 | [diff] [blame] | 66 | cl::desc("Force interpretation: disable JIT"), |
| 67 | cl::init(false)); |
Evan Cheng | e1a4eda | 2008-08-08 08:12:06 +0000 | [diff] [blame] | 68 | |
Daniel Dunbar | 6d13597 | 2010-11-17 16:06:37 +0000 | [diff] [blame] | 69 | cl::opt<bool> UseMCJIT( |
| 70 | "use-mcjit", cl::desc("Enable use of the MC-based JIT (if available)"), |
| 71 | cl::init(false)); |
| 72 | |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 73 | // The MCJIT supports building for a target address space separate from |
| 74 | // the JIT compilation process. Use a forked process and a copying |
| 75 | // memory manager with IPC to execute using this functionality. |
| 76 | cl::opt<bool> RemoteMCJIT("remote-mcjit", |
| 77 | cl::desc("Execute MCJIT'ed code in a separate process."), |
| 78 | cl::init(false)); |
| 79 | |
Evan Cheng | 712e80e | 2009-05-04 23:05:19 +0000 | [diff] [blame] | 80 | // Determine optimization level. |
| 81 | cl::opt<char> |
| 82 | OptLevel("O", |
| 83 | cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
| 84 | "(default = '-O2')"), |
| 85 | cl::Prefix, |
| 86 | cl::ZeroOrMore, |
| 87 | cl::init(' ')); |
Evan Cheng | e1a4eda | 2008-08-08 08:12:06 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 3015e60 | 2005-12-16 05:00:21 +0000 | [diff] [blame] | 89 | cl::opt<std::string> |
Chris Lattner | 60844d4 | 2005-12-16 05:19:18 +0000 | [diff] [blame] | 90 | TargetTriple("mtriple", cl::desc("Override target triple for module")); |
Evan Cheng | ec740e3 | 2008-11-05 23:21:52 +0000 | [diff] [blame] | 91 | |
| 92 | cl::opt<std::string> |
Jeffrey Yasskin | 4688261 | 2010-02-05 16:19:36 +0000 | [diff] [blame] | 93 | MArch("march", |
| 94 | cl::desc("Architecture to generate assembly for (see --version)")); |
| 95 | |
| 96 | cl::opt<std::string> |
| 97 | MCPU("mcpu", |
| 98 | cl::desc("Target a specific cpu type (-mcpu=help for details)"), |
| 99 | cl::value_desc("cpu-name"), |
| 100 | cl::init("")); |
| 101 | |
| 102 | cl::list<std::string> |
| 103 | MAttrs("mattr", |
| 104 | cl::CommaSeparated, |
| 105 | cl::desc("Target specific attributes (-mattr=help for details)"), |
| 106 | cl::value_desc("a1,+a2,-a3,...")); |
| 107 | |
| 108 | cl::opt<std::string> |
Evan Cheng | ec740e3 | 2008-11-05 23:21:52 +0000 | [diff] [blame] | 109 | EntryFunc("entry-function", |
| 110 | cl::desc("Specify the entry function (default = 'main') " |
| 111 | "of the executable"), |
| 112 | cl::value_desc("function"), |
| 113 | cl::init("main")); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 114 | |
Chris Lattner | e69671d | 2003-10-28 22:51:44 +0000 | [diff] [blame] | 115 | cl::opt<std::string> |
| 116 | FakeArgv0("fake-argv0", |
| 117 | cl::desc("Override the 'argv[0]' value passed into the executing" |
| 118 | " program"), cl::value_desc("executable")); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 43f249a | 2006-09-14 06:17:09 +0000 | [diff] [blame] | 120 | cl::opt<bool> |
| 121 | DisableCoreFiles("disable-core-files", cl::Hidden, |
| 122 | cl::desc("Disable emission of core files if possible")); |
Evan Cheng | c290a5b | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 123 | |
| 124 | cl::opt<bool> |
Evan Cheng | 03dace8 | 2008-05-21 18:20:21 +0000 | [diff] [blame] | 125 | NoLazyCompilation("disable-lazy-compilation", |
Evan Cheng | c290a5b | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 126 | cl::desc("Disable JIT lazy compilation"), |
| 127 | cl::init(false)); |
Evan Cheng | 4396613 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 128 | |
| 129 | cl::opt<Reloc::Model> |
| 130 | RelocModel("relocation-model", |
| 131 | cl::desc("Choose relocation model"), |
| 132 | cl::init(Reloc::Default), |
| 133 | cl::values( |
| 134 | clEnumValN(Reloc::Default, "default", |
| 135 | "Target default relocation model"), |
| 136 | clEnumValN(Reloc::Static, "static", |
| 137 | "Non-relocatable code"), |
| 138 | clEnumValN(Reloc::PIC_, "pic", |
| 139 | "Fully relocatable, position independent code"), |
| 140 | clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic", |
| 141 | "Relocatable external references, non-relocatable code"), |
| 142 | clEnumValEnd)); |
Evan Cheng | 34ad6db | 2011-07-20 07:51:56 +0000 | [diff] [blame] | 143 | |
| 144 | cl::opt<llvm::CodeModel::Model> |
| 145 | CMModel("code-model", |
| 146 | cl::desc("Choose code model"), |
| 147 | cl::init(CodeModel::JITDefault), |
| 148 | cl::values(clEnumValN(CodeModel::JITDefault, "default", |
| 149 | "Target default JIT code model"), |
| 150 | clEnumValN(CodeModel::Small, "small", |
| 151 | "Small code model"), |
| 152 | clEnumValN(CodeModel::Kernel, "kernel", |
| 153 | "Kernel code model"), |
| 154 | clEnumValN(CodeModel::Medium, "medium", |
| 155 | "Medium code model"), |
| 156 | clEnumValN(CodeModel::Large, "large", |
| 157 | "Large code model"), |
| 158 | clEnumValEnd)); |
| 159 | |
Nick Lewycky | 9a14841 | 2012-04-18 08:34:12 +0000 | [diff] [blame] | 160 | cl::opt<bool> |
| 161 | EnableJITExceptionHandling("jit-enable-eh", |
| 162 | cl::desc("Emit exception handling information"), |
| 163 | cl::init(false)); |
| 164 | |
| 165 | cl::opt<bool> |
Tim Northover | 77b4c69 | 2012-10-12 09:55:13 +0000 | [diff] [blame] | 166 | GenerateSoftFloatCalls("soft-float", |
| 167 | cl::desc("Generate software floating point library calls"), |
| 168 | cl::init(false)); |
| 169 | |
| 170 | cl::opt<llvm::FloatABI::ABIType> |
| 171 | FloatABIForCalls("float-abi", |
| 172 | cl::desc("Choose float ABI type"), |
| 173 | cl::init(FloatABI::Default), |
| 174 | cl::values( |
| 175 | clEnumValN(FloatABI::Default, "default", |
| 176 | "Target default float ABI type"), |
| 177 | clEnumValN(FloatABI::Soft, "soft", |
| 178 | "Soft float ABI (implied by -soft-float)"), |
| 179 | clEnumValN(FloatABI::Hard, "hard", |
| 180 | "Hard float ABI (uses FP registers)"), |
| 181 | clEnumValEnd)); |
| 182 | cl::opt<bool> |
Nick Lewycky | 9a14841 | 2012-04-18 08:34:12 +0000 | [diff] [blame] | 183 | // In debug builds, make this default to true. |
| 184 | #ifdef NDEBUG |
| 185 | #define EMIT_DEBUG false |
| 186 | #else |
| 187 | #define EMIT_DEBUG true |
| 188 | #endif |
| 189 | EmitJitDebugInfo("jit-emit-debug", |
| 190 | cl::desc("Emit debug information to debugger"), |
| 191 | cl::init(EMIT_DEBUG)); |
| 192 | #undef EMIT_DEBUG |
| 193 | |
| 194 | static cl::opt<bool> |
| 195 | EmitJitDebugInfoToDisk("jit-emit-debug-to-disk", |
| 196 | cl::Hidden, |
| 197 | cl::desc("Emit debug info objfiles to disk"), |
| 198 | cl::init(false)); |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 199 | } |
Chris Lattner | 43e3f7c | 2001-10-27 08:43:52 +0000 | [diff] [blame] | 200 | |
Reid Spencer | f70d677 | 2007-03-03 18:21:44 +0000 | [diff] [blame] | 201 | static ExecutionEngine *EE = 0; |
| 202 | |
| 203 | static void do_shutdown() { |
NAKAMURA Takumi | a13d14a | 2010-10-22 14:53:59 +0000 | [diff] [blame] | 204 | // Cygwin-1.5 invokes DLL's dtors before atexit handler. |
| 205 | #ifndef DO_NOTHING_ATEXIT |
Reid Spencer | f70d677 | 2007-03-03 18:21:44 +0000 | [diff] [blame] | 206 | delete EE; |
| 207 | llvm_shutdown(); |
NAKAMURA Takumi | a13d14a | 2010-10-22 14:53:59 +0000 | [diff] [blame] | 208 | #endif |
Reid Spencer | f70d677 | 2007-03-03 18:21:44 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 211 | void layoutRemoteTargetMemory(RemoteTarget *T, RecordingMemoryManager *JMM) { |
| 212 | // Lay out our sections in order, with all the code sections first, then |
| 213 | // all the data sections. |
| 214 | uint64_t CurOffset = 0; |
| 215 | unsigned MaxAlign = T->getPageAlignment(); |
| 216 | SmallVector<std::pair<const void*, uint64_t>, 16> Offsets; |
| 217 | SmallVector<unsigned, 16> Sizes; |
| 218 | for (RecordingMemoryManager::const_code_iterator I = JMM->code_begin(), |
| 219 | E = JMM->code_end(); |
| 220 | I != E; ++I) { |
| 221 | DEBUG(dbgs() << "code region: size " << I->first.size() |
| 222 | << ", alignment " << I->second << "\n"); |
| 223 | // Align the current offset up to whatever is needed for the next |
| 224 | // section. |
| 225 | unsigned Align = I->second; |
| 226 | CurOffset = (CurOffset + Align - 1) / Align * Align; |
| 227 | // Save off the address of the new section and allocate its space. |
| 228 | Offsets.push_back(std::pair<const void*,uint64_t>(I->first.base(), CurOffset)); |
| 229 | Sizes.push_back(I->first.size()); |
| 230 | CurOffset += I->first.size(); |
| 231 | } |
| 232 | // Adjust to keep code and data aligned on seperate pages. |
| 233 | CurOffset = (CurOffset + MaxAlign - 1) / MaxAlign * MaxAlign; |
| 234 | unsigned FirstDataIndex = Offsets.size(); |
| 235 | for (RecordingMemoryManager::const_data_iterator I = JMM->data_begin(), |
| 236 | E = JMM->data_end(); |
| 237 | I != E; ++I) { |
| 238 | DEBUG(dbgs() << "data region: size " << I->first.size() |
| 239 | << ", alignment " << I->second << "\n"); |
| 240 | // Align the current offset up to whatever is needed for the next |
| 241 | // section. |
| 242 | unsigned Align = I->second; |
| 243 | CurOffset = (CurOffset + Align - 1) / Align * Align; |
| 244 | // Save off the address of the new section and allocate its space. |
| 245 | Offsets.push_back(std::pair<const void*,uint64_t>(I->first.base(), CurOffset)); |
| 246 | Sizes.push_back(I->first.size()); |
| 247 | CurOffset += I->first.size(); |
| 248 | } |
| 249 | |
| 250 | // Allocate space in the remote target. |
| 251 | uint64_t RemoteAddr; |
| 252 | if (T->allocateSpace(CurOffset, MaxAlign, RemoteAddr)) |
| 253 | report_fatal_error(T->getErrorMsg()); |
| 254 | // Map the section addresses so relocations will get updated in the local |
| 255 | // copies of the sections. |
| 256 | for (unsigned i = 0, e = Offsets.size(); i != e; ++i) { |
| 257 | uint64_t Addr = RemoteAddr + Offsets[i].second; |
| 258 | EE->mapSectionAddress(const_cast<void*>(Offsets[i].first), Addr); |
| 259 | |
| 260 | DEBUG(dbgs() << " Mapping local: " << Offsets[i].first |
Andrew Kaylor | 3037a58 | 2012-11-01 00:17:11 +0000 | [diff] [blame] | 261 | << " to remote: " << format("%p", Addr) << "\n"); |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 262 | |
| 263 | } |
Andrew Kaylor | 2898988 | 2012-11-05 20:57:16 +0000 | [diff] [blame] | 264 | |
| 265 | // Trigger application of relocations |
| 266 | EE->finalizeObject(); |
| 267 | |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 268 | // Now load it all to the target. |
| 269 | for (unsigned i = 0, e = Offsets.size(); i != e; ++i) { |
| 270 | uint64_t Addr = RemoteAddr + Offsets[i].second; |
| 271 | |
| 272 | if (i < FirstDataIndex) { |
| 273 | T->loadCode(Addr, Offsets[i].first, Sizes[i]); |
| 274 | |
| 275 | DEBUG(dbgs() << " loading code: " << Offsets[i].first |
Andrew Kaylor | 3037a58 | 2012-11-01 00:17:11 +0000 | [diff] [blame] | 276 | << " to remote: " << format("%p", Addr) << "\n"); |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 277 | } else { |
| 278 | T->loadData(Addr, Offsets[i].first, Sizes[i]); |
| 279 | |
| 280 | DEBUG(dbgs() << " loading data: " << Offsets[i].first |
Andrew Kaylor | 3037a58 | 2012-11-01 00:17:11 +0000 | [diff] [blame] | 281 | << " to remote: " << format("%p", Addr) << "\n"); |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | } |
| 285 | } |
| 286 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 287 | //===----------------------------------------------------------------------===// |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 288 | // main Driver function |
| 289 | // |
Misha Brukman | c4fb6fd | 2003-10-14 21:39:53 +0000 | [diff] [blame] | 290 | int main(int argc, char **argv, char * const *envp) { |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 291 | sys::PrintStackTraceOnErrorSignal(); |
| 292 | PrettyStackTraceProgram X(argc, argv); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 293 | |
Owen Anderson | 0d7c695 | 2009-07-15 22:16:10 +0000 | [diff] [blame] | 294 | LLVMContext &Context = getGlobalContext(); |
Reid Spencer | f70d677 | 2007-03-03 18:21:44 +0000 | [diff] [blame] | 295 | atexit(do_shutdown); // Call llvm_shutdown() on exit. |
Daniel Dunbar | 494d663 | 2009-07-16 02:04:54 +0000 | [diff] [blame] | 296 | |
| 297 | // If we have a native target, initialize it to ensure it is linked in and |
| 298 | // usable by the JIT. |
| 299 | InitializeNativeTarget(); |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 300 | InitializeNativeTargetAsmPrinter(); |
Jim Grosbach | 6837232 | 2012-11-05 19:06:05 +0000 | [diff] [blame] | 301 | InitializeNativeTargetAsmParser(); |
Daniel Dunbar | 494d663 | 2009-07-16 02:04:54 +0000 | [diff] [blame] | 302 | |
Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 303 | cl::ParseCommandLineOptions(argc, argv, |
Dan Gohman | 82a13c9 | 2007-10-08 15:45:12 +0000 | [diff] [blame] | 304 | "llvm interpreter & dynamic compiler\n"); |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 305 | |
Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 306 | // If the user doesn't want core files, disable them. |
| 307 | if (DisableCoreFiles) |
| 308 | sys::Process::PreventCoreFiles(); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 309 | |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 310 | // Load the bitcode... |
Daniel Dunbar | 46a2716 | 2010-11-13 00:28:01 +0000 | [diff] [blame] | 311 | SMDiagnostic Err; |
| 312 | Module *Mod = ParseIRFile(InputFile, Err, Context); |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 313 | if (!Mod) { |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 314 | Err.print(argv[0], errs()); |
Daniel Dunbar | 46a2716 | 2010-11-13 00:28:01 +0000 | [diff] [blame] | 315 | return 1; |
Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 318 | // If not jitting lazily, load the whole bitcode file eagerly too. |
Daniel Dunbar | 46a2716 | 2010-11-13 00:28:01 +0000 | [diff] [blame] | 319 | std::string ErrorMsg; |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 320 | if (NoLazyCompilation) { |
| 321 | if (Mod->MaterializeAllPermanently(&ErrorMsg)) { |
| 322 | errs() << argv[0] << ": bitcode didn't read correctly.\n"; |
| 323 | errs() << "Reason: " << ErrorMsg << "\n"; |
| 324 | exit(1); |
| 325 | } |
Evan Cheng | c290a5b | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 326 | } |
Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 327 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 328 | EngineBuilder builder(Mod); |
Jeffrey Yasskin | 4688261 | 2010-02-05 16:19:36 +0000 | [diff] [blame] | 329 | builder.setMArch(MArch); |
| 330 | builder.setMCPU(MCPU); |
| 331 | builder.setMAttrs(MAttrs); |
Evan Cheng | 4396613 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 332 | builder.setRelocationModel(RelocModel); |
Evan Cheng | 34ad6db | 2011-07-20 07:51:56 +0000 | [diff] [blame] | 333 | builder.setCodeModel(CMModel); |
Daniel Dunbar | 4dc3136 | 2009-07-18 08:07:13 +0000 | [diff] [blame] | 334 | builder.setErrorStr(&ErrorMsg); |
| 335 | builder.setEngineKind(ForceInterpreter |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 336 | ? EngineKind::Interpreter |
| 337 | : EngineKind::JIT); |
| 338 | |
Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 339 | // If we are supposed to override the target triple, do so now. |
| 340 | if (!TargetTriple.empty()) |
Duncan Sands | 75ebbce | 2010-08-28 01:30:02 +0000 | [diff] [blame] | 341 | Mod->setTargetTriple(Triple::normalize(TargetTriple)); |
Evan Cheng | c290a5b | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 342 | |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 343 | // Enable MCJIT if desired. |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 344 | JITMemoryManager *JMM = 0; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 345 | if (UseMCJIT && !ForceInterpreter) { |
Daniel Dunbar | 6d13597 | 2010-11-17 16:06:37 +0000 | [diff] [blame] | 346 | builder.setUseMCJIT(true); |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 347 | if (RemoteMCJIT) |
| 348 | JMM = new RecordingMemoryManager(); |
| 349 | else |
Andrew Kaylor | 257a009 | 2012-11-27 19:49:00 +0000 | [diff] [blame] | 350 | JMM = new SectionMemoryManager(); |
Danil Malyshev | 068c65b | 2012-05-16 18:50:11 +0000 | [diff] [blame] | 351 | builder.setJITMemoryManager(JMM); |
Benjamin Kramer | 6514551 | 2012-05-20 17:24:08 +0000 | [diff] [blame] | 352 | } else { |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 353 | if (RemoteMCJIT) { |
| 354 | errs() << "error: Remote process execution requires -use-mcjit\n"; |
| 355 | exit(1); |
| 356 | } |
Benjamin Kramer | 6514551 | 2012-05-20 17:24:08 +0000 | [diff] [blame] | 357 | builder.setJITMemoryManager(ForceInterpreter ? 0 : |
| 358 | JITMemoryManager::CreateDefaultMemManager()); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 359 | } |
Daniel Dunbar | 6d13597 | 2010-11-17 16:06:37 +0000 | [diff] [blame] | 360 | |
Evan Cheng | 712e80e | 2009-05-04 23:05:19 +0000 | [diff] [blame] | 361 | CodeGenOpt::Level OLvl = CodeGenOpt::Default; |
| 362 | switch (OptLevel) { |
| 363 | default: |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 364 | errs() << argv[0] << ": invalid optimization level.\n"; |
Evan Cheng | 712e80e | 2009-05-04 23:05:19 +0000 | [diff] [blame] | 365 | return 1; |
| 366 | case ' ': break; |
| 367 | case '0': OLvl = CodeGenOpt::None; break; |
Evan Cheng | bf57b52 | 2009-10-16 21:02:20 +0000 | [diff] [blame] | 368 | case '1': OLvl = CodeGenOpt::Less; break; |
Evan Cheng | 712e80e | 2009-05-04 23:05:19 +0000 | [diff] [blame] | 369 | case '2': OLvl = CodeGenOpt::Default; break; |
| 370 | case '3': OLvl = CodeGenOpt::Aggressive; break; |
| 371 | } |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 372 | builder.setOptLevel(OLvl); |
| 373 | |
Tim Northover | 77b4c69 | 2012-10-12 09:55:13 +0000 | [diff] [blame] | 374 | TargetOptions Options; |
| 375 | Options.UseSoftFloat = GenerateSoftFloatCalls; |
| 376 | if (FloatABIForCalls != FloatABI::Default) |
| 377 | Options.FloatABIType = FloatABIForCalls; |
| 378 | if (GenerateSoftFloatCalls) |
| 379 | FloatABIForCalls = FloatABI::Soft; |
| 380 | |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 381 | // Remote target execution doesn't handle EH or debug registration. |
| 382 | if (!RemoteMCJIT) { |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 383 | Options.JITExceptionHandling = EnableJITExceptionHandling; |
| 384 | Options.JITEmitDebugInfo = EmitJitDebugInfo; |
| 385 | Options.JITEmitDebugInfoToDisk = EmitJitDebugInfoToDisk; |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 386 | } |
Nick Lewycky | 9a14841 | 2012-04-18 08:34:12 +0000 | [diff] [blame] | 387 | |
Tim Northover | 77b4c69 | 2012-10-12 09:55:13 +0000 | [diff] [blame] | 388 | builder.setTargetOptions(Options); |
| 389 | |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 390 | EE = builder.create(); |
Chris Lattner | fd15bee | 2009-07-07 18:31:09 +0000 | [diff] [blame] | 391 | if (!EE) { |
| 392 | if (!ErrorMsg.empty()) |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 393 | errs() << argv[0] << ": error creating EE: " << ErrorMsg << "\n"; |
Chris Lattner | fd15bee | 2009-07-07 18:31:09 +0000 | [diff] [blame] | 394 | else |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 395 | errs() << argv[0] << ": unknown error creating EE!\n"; |
Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 396 | exit(1); |
| 397 | } |
| 398 | |
Eli Bendersky | 61b1851 | 2012-03-13 08:33:15 +0000 | [diff] [blame] | 399 | // The following functions have no effect if their respective profiling |
| 400 | // support wasn't enabled in the build configuration. |
| 401 | EE->RegisterJITEventListener( |
| 402 | JITEventListener::createOProfileJITEventListener()); |
| 403 | EE->RegisterJITEventListener( |
| 404 | JITEventListener::createIntelJITEventListener()); |
Jeffrey Yasskin | df5a7da | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 405 | |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 406 | if (!NoLazyCompilation && RemoteMCJIT) { |
| 407 | errs() << "warning: remote mcjit does not support lazy compilation\n"; |
| 408 | NoLazyCompilation = true; |
| 409 | } |
Jeffrey Yasskin | 18fec73 | 2009-10-27 22:39:42 +0000 | [diff] [blame] | 410 | EE->DisableLazyCompilation(NoLazyCompilation); |
Evan Cheng | c290a5b | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 411 | |
Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 412 | // If the user specifically requested an argv[0] to pass into the program, |
| 413 | // do it now. |
| 414 | if (!FakeArgv0.empty()) { |
| 415 | InputFile = FakeArgv0; |
| 416 | } else { |
| 417 | // Otherwise, if there is a .bc suffix on the executable strip it off, it |
| 418 | // might confuse the program. |
Benjamin Kramer | 3bb37e9 | 2010-04-15 11:33:14 +0000 | [diff] [blame] | 419 | if (StringRef(InputFile).endswith(".bc")) |
Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 420 | InputFile.erase(InputFile.length() - 3); |
| 421 | } |
| 422 | |
| 423 | // Add the module's name to the start of the vector of arguments to main(). |
| 424 | InputArgv.insert(InputArgv.begin(), InputFile); |
| 425 | |
| 426 | // Call the main function from M as if its signature were: |
| 427 | // int main (int argc, char **argv, const char **envp) |
| 428 | // using the contents of Args to determine argc & argv, and the contents of |
| 429 | // EnvVars to determine envp. |
| 430 | // |
Evan Cheng | ec740e3 | 2008-11-05 23:21:52 +0000 | [diff] [blame] | 431 | Function *EntryFn = Mod->getFunction(EntryFunc); |
| 432 | if (!EntryFn) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 433 | errs() << '\'' << EntryFunc << "\' function not found in module.\n"; |
Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 434 | return -1; |
| 435 | } |
| 436 | |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 437 | // If the program doesn't explicitly call exit, we will need the Exit |
| 438 | // function later on to make an explicit call, so get the function now. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 439 | Constant *Exit = Mod->getOrInsertFunction("exit", Type::getVoidTy(Context), |
| 440 | Type::getInt32Ty(Context), |
| 441 | NULL); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 442 | |
Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 443 | // Reset errno to zero on entry to main. |
| 444 | errno = 0; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 445 | |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 446 | // Remote target MCJIT doesn't (yet) support static constructors. No reason |
| 447 | // it couldn't. This is a limitation of the LLI implemantation, not the |
| 448 | // MCJIT itself. FIXME. |
| 449 | // |
Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 450 | // Run static constructors. |
Andrew Kaylor | 257a009 | 2012-11-27 19:49:00 +0000 | [diff] [blame] | 451 | if (!RemoteMCJIT) { |
| 452 | if (UseMCJIT && !ForceInterpreter) { |
| 453 | // Give MCJIT a chance to apply relocations and set page permissions. |
| 454 | EE->finalizeObject(); |
| 455 | } |
| 456 | EE->runStaticConstructorsDestructors(false); |
| 457 | } |
Evan Cheng | c290a5b | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 458 | |
| 459 | if (NoLazyCompilation) { |
| 460 | for (Module::iterator I = Mod->begin(), E = Mod->end(); I != E; ++I) { |
| 461 | Function *Fn = &*I; |
Evan Cheng | ec740e3 | 2008-11-05 23:21:52 +0000 | [diff] [blame] | 462 | if (Fn != EntryFn && !Fn->isDeclaration()) |
Evan Cheng | c290a5b | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 463 | EE->getPointerToFunction(Fn); |
| 464 | } |
| 465 | } |
| 466 | |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 467 | int Result; |
| 468 | if (RemoteMCJIT) { |
| 469 | RecordingMemoryManager *MM = static_cast<RecordingMemoryManager*>(JMM); |
| 470 | // Everything is prepared now, so lay out our program for the target |
| 471 | // address space, assign the section addresses to resolve any relocations, |
| 472 | // and send it to the target. |
| 473 | RemoteTarget Target; |
| 474 | Target.create(); |
Jim Grosbach | 5da959d | 2012-08-28 23:22:30 +0000 | [diff] [blame] | 475 | |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 476 | // Ask for a pointer to the entry function. This triggers the actual |
| 477 | // compilation. |
| 478 | (void)EE->getPointerToFunction(EntryFn); |
Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 479 | |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 480 | // Enough has been compiled to execute the entry function now, so |
| 481 | // layout the target memory. |
| 482 | layoutRemoteTargetMemory(&Target, MM); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 483 | |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 484 | // Since we're executing in a (at least simulated) remote address space, |
| 485 | // we can't use the ExecutionEngine::runFunctionAsMain(). We have to |
| 486 | // grab the function address directly here and tell the remote target |
| 487 | // to execute the function. |
| 488 | // FIXME: argv and envp handling. |
| 489 | uint64_t Entry = (uint64_t)EE->getPointerToFunction(EntryFn); |
| 490 | |
| 491 | DEBUG(dbgs() << "Executing '" << EntryFn->getName() << "' at " |
Andrew Kaylor | 3037a58 | 2012-11-01 00:17:11 +0000 | [diff] [blame] | 492 | << format("%p", Entry) << "\n"); |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 493 | |
| 494 | if (Target.executeCode(Entry, Result)) |
| 495 | errs() << "ERROR: " << Target.getErrorMsg() << "\n"; |
| 496 | |
| 497 | Target.stop(); |
Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 498 | } else { |
Tim Northover | a668b46 | 2012-09-20 08:46:30 +0000 | [diff] [blame] | 499 | // Trigger compilation separately so code regions that need to be |
| 500 | // invalidated will be known. |
| 501 | (void)EE->getPointerToFunction(EntryFn); |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 502 | // Clear instruction cache before code will be executed. |
| 503 | if (JMM) |
Andrew Kaylor | 257a009 | 2012-11-27 19:49:00 +0000 | [diff] [blame] | 504 | static_cast<SectionMemoryManager*>(JMM)->invalidateInstructionCache(); |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 505 | |
| 506 | // Run main. |
| 507 | Result = EE->runFunctionAsMain(EntryFn, InputArgv, envp); |
Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 508 | } |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 509 | |
| 510 | // Like static constructors, the remote target MCJIT support doesn't handle |
| 511 | // this yet. It could. FIXME. |
| 512 | if (!RemoteMCJIT) { |
| 513 | // Run static destructors. |
| 514 | EE->runStaticConstructorsDestructors(true); |
| 515 | |
| 516 | // If the program didn't call exit explicitly, we should call it now. |
| 517 | // This ensures that any atexit handlers get called correctly. |
| 518 | if (Function *ExitF = dyn_cast<Function>(Exit)) { |
| 519 | std::vector<GenericValue> Args; |
| 520 | GenericValue ResultGV; |
| 521 | ResultGV.IntVal = APInt(32, Result); |
| 522 | Args.push_back(ResultGV); |
| 523 | EE->runFunction(ExitF, Args); |
| 524 | errs() << "ERROR: exit(" << Result << ") returned!\n"; |
| 525 | abort(); |
| 526 | } else { |
| 527 | errs() << "ERROR: exit defined with wrong prototype!\n"; |
| 528 | abort(); |
| 529 | } |
| 530 | } |
| 531 | return Result; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 532 | } |