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