Chris Lattner | a0d7b08 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 1 | //===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===// |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 345353d | 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 | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | d7ff578 | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 141ea3a | 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 | f011f28 | 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 | d7ff578 | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/LLVMContext.h" |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 17 | #include "RemoteMemoryManager.h" |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 18 | #include "RemoteTarget.h" |
Alp Toker | ce4ab59 | 2014-01-23 19:57:16 +0000 | [diff] [blame] | 19 | #include "RemoteTargetExternal.h" |
Duncan Sands | 3bd97fe | 2010-08-28 01:30:02 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Triple.h" |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 21 | #include "llvm/Bitcode/ReaderWriter.h" |
Chris Lattner | 5af6a3f | 2006-08-01 22:34:35 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/LinkAllCodegenComponents.h" |
Brian Gaeke | e99ca44 | 2003-09-05 19:42:34 +0000 | [diff] [blame] | 23 | #include "llvm/ExecutionEngine/GenericValue.h" |
Jeffrey Yasskin | 0b08f3d | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 24 | #include "llvm/ExecutionEngine/Interpreter.h" |
Eric Christopher | b9fd9ed | 2014-08-07 22:02:54 +0000 | [diff] [blame] | 25 | #include "llvm/ExecutionEngine/JIT.h" |
Jeffrey Yasskin | 0b08f3d | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 26 | #include "llvm/ExecutionEngine/JITEventListener.h" |
Jim Grosbach | bf570b9 | 2012-01-11 21:12:51 +0000 | [diff] [blame] | 27 | #include "llvm/ExecutionEngine/JITMemoryManager.h" |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 28 | #include "llvm/ExecutionEngine/MCJIT.h" |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 29 | #include "llvm/ExecutionEngine/ObjectCache.h" |
Andrew Kaylor | 58365b9 | 2012-11-27 19:49:00 +0000 | [diff] [blame] | 30 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
Andrew Kaylor | 1ca510e | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 31 | #include "llvm/IR/IRBuilder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Module.h" |
| 33 | #include "llvm/IR/Type.h" |
Andrew Kaylor | 1ca510e | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 34 | #include "llvm/IR/TypeBuilder.h" |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 35 | #include "llvm/IRReader/IRReader.h" |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 36 | #include "llvm/Object/Archive.h" |
| 37 | #include "llvm/Object/ObjectFile.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 38 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Debug.h" |
| 40 | #include "llvm/Support/DynamicLibrary.h" |
| 41 | #include "llvm/Support/Format.h" |
Chris Lattner | 76d4632 | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 42 | #include "llvm/Support/ManagedStatic.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 43 | #include "llvm/Support/MathExtras.h" |
| 44 | #include "llvm/Support/Memory.h" |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 45 | #include "llvm/Support/MemoryBuffer.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 46 | #include "llvm/Support/PluginLoader.h" |
Chris Lattner | e3fc2d1 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 47 | #include "llvm/Support/PrettyStackTrace.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Process.h" |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Program.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 50 | #include "llvm/Support/Signals.h" |
Chandler Carruth | e60e57b | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 51 | #include "llvm/Support/SourceMgr.h" |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 52 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 53 | #include "llvm/Support/raw_ostream.h" |
Daniel Malea | a960c54 | 2013-06-28 19:11:40 +0000 | [diff] [blame] | 54 | #include "llvm/Transforms/Instrumentation.h" |
Chris Lattner | 4c522b9 | 2007-04-27 17:02:33 +0000 | [diff] [blame] | 55 | #include <cerrno> |
NAKAMURA Takumi | 1530487 | 2010-10-22 14:53:59 +0000 | [diff] [blame] | 56 | |
| 57 | #ifdef __CYGWIN__ |
| 58 | #include <cygwin/version.h> |
| 59 | #if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007 |
| 60 | #define DO_NOTHING_ATEXIT 1 |
| 61 | #endif |
| 62 | #endif |
| 63 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 64 | using namespace llvm; |
| 65 | |
Chandler Carruth | f98597a | 2014-04-22 03:10:36 +0000 | [diff] [blame] | 66 | #define DEBUG_TYPE "lli" |
| 67 | |
Chris Lattner | a0d7b08 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 68 | namespace { |
| 69 | cl::opt<std::string> |
Gabor Greif | e16561c | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 70 | InputFile(cl::desc("<input bitcode>"), cl::Positional, cl::init("-")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 71 | |
Chris Lattner | a0d7b08 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 72 | cl::list<std::string> |
| 73 | InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>...")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 74 | |
Chris Lattner | a0d7b08 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 75 | cl::opt<bool> ForceInterpreter("force-interpreter", |
Misha Brukman | c2186e3 | 2003-09-25 18:10:34 +0000 | [diff] [blame] | 76 | cl::desc("Force interpretation: disable JIT"), |
| 77 | cl::init(false)); |
Evan Cheng | 06d988e | 2008-08-08 08:12:06 +0000 | [diff] [blame] | 78 | |
Eric Christopher | b9fd9ed | 2014-08-07 22:02:54 +0000 | [diff] [blame] | 79 | cl::opt<bool> UseMCJIT( |
| 80 | "use-mcjit", cl::desc("Enable use of the MC-based JIT (if available)"), |
| 81 | cl::init(false)); |
| 82 | |
Daniel Malea | a960c54 | 2013-06-28 19:11:40 +0000 | [diff] [blame] | 83 | cl::opt<bool> DebugIR( |
| 84 | "debug-ir", cl::desc("Generate debug information to allow debugging IR."), |
| 85 | cl::init(false)); |
| 86 | |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 87 | // The MCJIT supports building for a target address space separate from |
| 88 | // the JIT compilation process. Use a forked process and a copying |
| 89 | // memory manager with IPC to execute using this functionality. |
| 90 | cl::opt<bool> RemoteMCJIT("remote-mcjit", |
| 91 | cl::desc("Execute MCJIT'ed code in a separate process."), |
| 92 | cl::init(false)); |
| 93 | |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 94 | // Manually specify the child process for remote execution. This overrides |
| 95 | // the simulated remote execution that allocates address space for child |
Andrew Kaylor | 8935258 | 2013-10-29 01:33:14 +0000 | [diff] [blame] | 96 | // execution. The child process will be executed and will communicate with |
| 97 | // lli via stdin/stdout pipes. |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 98 | cl::opt<std::string> |
Alp Toker | a118638 | 2014-01-22 21:52:35 +0000 | [diff] [blame] | 99 | ChildExecPath("mcjit-remote-process", |
| 100 | cl::desc("Specify the filename of the process to launch " |
| 101 | "for remote MCJIT execution. If none is specified," |
| 102 | "\n\tremote execution will be simulated in-process."), |
| 103 | cl::value_desc("filename"), cl::init("")); |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 104 | |
Evan Cheng | 09bd0b1 | 2009-05-04 23:05:19 +0000 | [diff] [blame] | 105 | // Determine optimization level. |
| 106 | cl::opt<char> |
| 107 | OptLevel("O", |
| 108 | cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
| 109 | "(default = '-O2')"), |
| 110 | cl::Prefix, |
| 111 | cl::ZeroOrMore, |
| 112 | cl::init(' ')); |
Evan Cheng | 06d988e | 2008-08-08 08:12:06 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 76766cb | 2005-12-16 05:00:21 +0000 | [diff] [blame] | 114 | cl::opt<std::string> |
Chris Lattner | 78e9e10 | 2005-12-16 05:19:18 +0000 | [diff] [blame] | 115 | TargetTriple("mtriple", cl::desc("Override target triple for module")); |
Evan Cheng | 6158254 | 2008-11-05 23:21:52 +0000 | [diff] [blame] | 116 | |
| 117 | cl::opt<std::string> |
Jeffrey Yasskin | 31faeff | 2010-02-05 16:19:36 +0000 | [diff] [blame] | 118 | MArch("march", |
| 119 | cl::desc("Architecture to generate assembly for (see --version)")); |
| 120 | |
| 121 | cl::opt<std::string> |
| 122 | MCPU("mcpu", |
| 123 | cl::desc("Target a specific cpu type (-mcpu=help for details)"), |
| 124 | cl::value_desc("cpu-name"), |
| 125 | cl::init("")); |
| 126 | |
| 127 | cl::list<std::string> |
| 128 | MAttrs("mattr", |
| 129 | cl::CommaSeparated, |
| 130 | cl::desc("Target specific attributes (-mattr=help for details)"), |
| 131 | cl::value_desc("a1,+a2,-a3,...")); |
| 132 | |
| 133 | cl::opt<std::string> |
Evan Cheng | 6158254 | 2008-11-05 23:21:52 +0000 | [diff] [blame] | 134 | EntryFunc("entry-function", |
| 135 | cl::desc("Specify the entry function (default = 'main') " |
| 136 | "of the executable"), |
| 137 | cl::value_desc("function"), |
| 138 | cl::init("main")); |
Eli Bendersky | 4c64758 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 139 | |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 140 | cl::list<std::string> |
Andrew Kaylor | 4404eb4 | 2013-10-28 21:58:15 +0000 | [diff] [blame] | 141 | ExtraModules("extra-module", |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 142 | cl::desc("Extra modules to be loaded"), |
Alp Toker | 0a09ebf | 2013-10-28 22:51:25 +0000 | [diff] [blame] | 143 | cl::value_desc("input bitcode")); |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 144 | |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 145 | cl::list<std::string> |
| 146 | ExtraObjects("extra-object", |
| 147 | cl::desc("Extra object files to be loaded"), |
| 148 | cl::value_desc("input object")); |
| 149 | |
| 150 | cl::list<std::string> |
| 151 | ExtraArchives("extra-archive", |
| 152 | cl::desc("Extra archive files to be loaded"), |
| 153 | cl::value_desc("input archive")); |
| 154 | |
| 155 | cl::opt<bool> |
| 156 | EnableCacheManager("enable-cache-manager", |
Lang Hames | 1ddecc0 | 2014-01-09 05:24:05 +0000 | [diff] [blame] | 157 | cl::desc("Use cache manager to save/load mdoules"), |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 158 | cl::init(false)); |
| 159 | |
Chris Lattner | 5564406 | 2003-10-28 22:51:44 +0000 | [diff] [blame] | 160 | cl::opt<std::string> |
Lang Hames | 1ddecc0 | 2014-01-09 05:24:05 +0000 | [diff] [blame] | 161 | ObjectCacheDir("object-cache-dir", |
| 162 | cl::desc("Directory to store cached object files " |
| 163 | "(must be user writable)"), |
| 164 | cl::init("")); |
| 165 | |
| 166 | cl::opt<std::string> |
Chris Lattner | 5564406 | 2003-10-28 22:51:44 +0000 | [diff] [blame] | 167 | FakeArgv0("fake-argv0", |
| 168 | cl::desc("Override the 'argv[0]' value passed into the executing" |
| 169 | " program"), cl::value_desc("executable")); |
Eli Bendersky | 4c64758 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 170 | |
Chris Lattner | 3f0ffd3 | 2006-09-14 06:17:09 +0000 | [diff] [blame] | 171 | cl::opt<bool> |
| 172 | DisableCoreFiles("disable-core-files", cl::Hidden, |
| 173 | cl::desc("Disable emission of core files if possible")); |
Evan Cheng | 0422bef | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 174 | |
| 175 | cl::opt<bool> |
Evan Cheng | 16f036c | 2008-05-21 18:20:21 +0000 | [diff] [blame] | 176 | NoLazyCompilation("disable-lazy-compilation", |
Evan Cheng | 0422bef | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 177 | cl::desc("Disable JIT lazy compilation"), |
| 178 | cl::init(false)); |
Evan Cheng | 2129f59 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 179 | |
| 180 | cl::opt<Reloc::Model> |
| 181 | RelocModel("relocation-model", |
| 182 | cl::desc("Choose relocation model"), |
| 183 | cl::init(Reloc::Default), |
| 184 | cl::values( |
| 185 | clEnumValN(Reloc::Default, "default", |
| 186 | "Target default relocation model"), |
| 187 | clEnumValN(Reloc::Static, "static", |
| 188 | "Non-relocatable code"), |
| 189 | clEnumValN(Reloc::PIC_, "pic", |
| 190 | "Fully relocatable, position independent code"), |
| 191 | clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic", |
| 192 | "Relocatable external references, non-relocatable code"), |
| 193 | clEnumValEnd)); |
Evan Cheng | efd9b42 | 2011-07-20 07:51:56 +0000 | [diff] [blame] | 194 | |
| 195 | cl::opt<llvm::CodeModel::Model> |
| 196 | CMModel("code-model", |
| 197 | cl::desc("Choose code model"), |
| 198 | cl::init(CodeModel::JITDefault), |
| 199 | cl::values(clEnumValN(CodeModel::JITDefault, "default", |
| 200 | "Target default JIT code model"), |
| 201 | clEnumValN(CodeModel::Small, "small", |
| 202 | "Small code model"), |
| 203 | clEnumValN(CodeModel::Kernel, "kernel", |
| 204 | "Kernel code model"), |
| 205 | clEnumValN(CodeModel::Medium, "medium", |
| 206 | "Medium code model"), |
| 207 | clEnumValN(CodeModel::Large, "large", |
| 208 | "Large code model"), |
| 209 | clEnumValEnd)); |
| 210 | |
Nick Lewycky | b0e9789 | 2012-04-18 08:34:12 +0000 | [diff] [blame] | 211 | cl::opt<bool> |
Tim Northover | d4a2f5b | 2012-10-12 09:55:13 +0000 | [diff] [blame] | 212 | GenerateSoftFloatCalls("soft-float", |
| 213 | cl::desc("Generate software floating point library calls"), |
| 214 | cl::init(false)); |
| 215 | |
| 216 | cl::opt<llvm::FloatABI::ABIType> |
| 217 | FloatABIForCalls("float-abi", |
| 218 | cl::desc("Choose float ABI type"), |
| 219 | cl::init(FloatABI::Default), |
| 220 | cl::values( |
| 221 | clEnumValN(FloatABI::Default, "default", |
| 222 | "Target default float ABI type"), |
| 223 | clEnumValN(FloatABI::Soft, "soft", |
| 224 | "Soft float ABI (implied by -soft-float)"), |
| 225 | clEnumValN(FloatABI::Hard, "hard", |
| 226 | "Hard float ABI (uses FP registers)"), |
| 227 | clEnumValEnd)); |
| 228 | cl::opt<bool> |
Nick Lewycky | b0e9789 | 2012-04-18 08:34:12 +0000 | [diff] [blame] | 229 | // In debug builds, make this default to true. |
| 230 | #ifdef NDEBUG |
| 231 | #define EMIT_DEBUG false |
| 232 | #else |
| 233 | #define EMIT_DEBUG true |
| 234 | #endif |
| 235 | EmitJitDebugInfo("jit-emit-debug", |
| 236 | cl::desc("Emit debug information to debugger"), |
| 237 | cl::init(EMIT_DEBUG)); |
| 238 | #undef EMIT_DEBUG |
| 239 | |
| 240 | static cl::opt<bool> |
| 241 | EmitJitDebugInfoToDisk("jit-emit-debug-to-disk", |
| 242 | cl::Hidden, |
| 243 | cl::desc("Emit debug info objfiles to disk"), |
| 244 | cl::init(false)); |
Chris Lattner | a0d7b08 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 245 | } |
Chris Lattner | 009f810 | 2001-10-27 08:43:52 +0000 | [diff] [blame] | 246 | |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 247 | //===----------------------------------------------------------------------===// |
| 248 | // Object cache |
| 249 | // |
Lang Hames | 1ddecc0 | 2014-01-09 05:24:05 +0000 | [diff] [blame] | 250 | // This object cache implementation writes cached objects to disk to the |
| 251 | // directory specified by CacheDir, using a filename provided in the module |
| 252 | // descriptor. The cache tries to load a saved object using that path if the |
| 253 | // file exists. CacheDir defaults to "", in which case objects are cached |
NAKAMURA Takumi | f462f9c | 2014-01-10 10:38:28 +0000 | [diff] [blame] | 254 | // alongside their originating bitcodes. |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 255 | // |
| 256 | class LLIObjectCache : public ObjectCache { |
| 257 | public: |
Lang Hames | 1ddecc0 | 2014-01-09 05:24:05 +0000 | [diff] [blame] | 258 | LLIObjectCache(const std::string& CacheDir) : CacheDir(CacheDir) { |
| 259 | // Add trailing '/' to cache dir if necessary. |
Lang Hames | f9dd8fd | 2014-01-09 05:29:59 +0000 | [diff] [blame] | 260 | if (!this->CacheDir.empty() && |
| 261 | this->CacheDir[this->CacheDir.size() - 1] != '/') |
NAKAMURA Takumi | f462f9c | 2014-01-10 10:38:28 +0000 | [diff] [blame] | 262 | this->CacheDir += '/'; |
Lang Hames | 1ddecc0 | 2014-01-09 05:24:05 +0000 | [diff] [blame] | 263 | } |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 264 | virtual ~LLIObjectCache() {} |
| 265 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 266 | void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) override { |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 267 | const std::string ModuleID = M->getModuleIdentifier(); |
| 268 | std::string CacheName; |
| 269 | if (!getCacheFilename(ModuleID, CacheName)) |
| 270 | return; |
NAKAMURA Takumi | 390e060 | 2014-01-10 10:38:34 +0000 | [diff] [blame] | 271 | if (!CacheDir.empty()) { // Create user-defined cache dir. |
| 272 | SmallString<128> dir(CacheName); |
| 273 | sys::path::remove_filename(dir); |
| 274 | sys::fs::create_directories(Twine(dir)); |
| 275 | } |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame^] | 276 | std::error_code EC; |
| 277 | raw_fd_ostream outfile(CacheName, EC, sys::fs::F_None); |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 278 | outfile.write(Obj.getBufferStart(), Obj.getBufferSize()); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 279 | outfile.close(); |
| 280 | } |
| 281 | |
Rafael Espindola | 5f2bb7d | 2014-08-13 18:49:01 +0000 | [diff] [blame] | 282 | std::unique_ptr<MemoryBuffer> getObject(const Module* M) override { |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 283 | const std::string ModuleID = M->getModuleIdentifier(); |
| 284 | std::string CacheName; |
| 285 | if (!getCacheFilename(ModuleID, CacheName)) |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 286 | return nullptr; |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 287 | // Load the object from the cache filename |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 288 | ErrorOr<std::unique_ptr<MemoryBuffer>> IRObjectBuffer = |
| 289 | MemoryBuffer::getFile(CacheName.c_str(), -1, false); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 290 | // If the file isn't there, that's OK. |
| 291 | if (!IRObjectBuffer) |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 292 | return nullptr; |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 293 | // MCJIT will want to write into this buffer, and we don't want that |
| 294 | // because the file has probably just been mmapped. Instead we make |
| 295 | // a copy. The filed-based buffer will be released when it goes |
| 296 | // out of scope. |
Rafael Espindola | 5f2bb7d | 2014-08-13 18:49:01 +0000 | [diff] [blame] | 297 | return std::unique_ptr<MemoryBuffer>( |
| 298 | MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer())); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | private: |
Lang Hames | 1ddecc0 | 2014-01-09 05:24:05 +0000 | [diff] [blame] | 302 | std::string CacheDir; |
| 303 | |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 304 | bool getCacheFilename(const std::string &ModID, std::string &CacheName) { |
| 305 | std::string Prefix("file:"); |
| 306 | size_t PrefixLength = Prefix.length(); |
| 307 | if (ModID.substr(0, PrefixLength) != Prefix) |
| 308 | return false; |
NAKAMURA Takumi | d7fd6d9 | 2014-01-10 10:38:40 +0000 | [diff] [blame] | 309 | std::string CacheSubdir = ModID.substr(PrefixLength); |
| 310 | #if defined(_WIN32) |
| 311 | // Transform "X:\foo" => "/X\foo" for convenience. |
| 312 | if (isalpha(CacheSubdir[0]) && CacheSubdir[1] == ':') { |
| 313 | CacheSubdir[1] = CacheSubdir[0]; |
| 314 | CacheSubdir[0] = '/'; |
| 315 | } |
| 316 | #endif |
| 317 | CacheName = CacheDir + CacheSubdir; |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 318 | size_t pos = CacheName.rfind('.'); |
| 319 | CacheName.replace(pos, CacheName.length() - pos, ".o"); |
| 320 | return true; |
| 321 | } |
| 322 | }; |
| 323 | |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 324 | static ExecutionEngine *EE = nullptr; |
| 325 | static LLIObjectCache *CacheManager = nullptr; |
Reid Spencer | e586f2e | 2007-03-03 18:21:44 +0000 | [diff] [blame] | 326 | |
| 327 | static void do_shutdown() { |
NAKAMURA Takumi | 1530487 | 2010-10-22 14:53:59 +0000 | [diff] [blame] | 328 | // Cygwin-1.5 invokes DLL's dtors before atexit handler. |
| 329 | #ifndef DO_NOTHING_ATEXIT |
Reid Spencer | e586f2e | 2007-03-03 18:21:44 +0000 | [diff] [blame] | 330 | delete EE; |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 331 | if (CacheManager) |
| 332 | delete CacheManager; |
Reid Spencer | e586f2e | 2007-03-03 18:21:44 +0000 | [diff] [blame] | 333 | llvm_shutdown(); |
NAKAMURA Takumi | 1530487 | 2010-10-22 14:53:59 +0000 | [diff] [blame] | 334 | #endif |
Reid Spencer | e586f2e | 2007-03-03 18:21:44 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Andrew Kaylor | 1ca510e | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 337 | // On Mingw and Cygwin, an external symbol named '__main' is called from the |
| 338 | // generated 'main' function to allow static intialization. To avoid linking |
| 339 | // problems with remote targets (because lli's remote target support does not |
| 340 | // currently handle external linking) we add a secondary module which defines |
| 341 | // an empty '__main' function. |
| 342 | static void addCygMingExtraModule(ExecutionEngine *EE, |
| 343 | LLVMContext &Context, |
| 344 | StringRef TargetTripleStr) { |
| 345 | IRBuilder<> Builder(Context); |
| 346 | Triple TargetTriple(TargetTripleStr); |
| 347 | |
| 348 | // Create a new module. |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 349 | std::unique_ptr<Module> M = make_unique<Module>("CygMingHelper", Context); |
Andrew Kaylor | 1ca510e | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 350 | M->setTargetTriple(TargetTripleStr); |
| 351 | |
| 352 | // Create an empty function named "__main". |
| 353 | Function *Result; |
| 354 | if (TargetTriple.isArch64Bit()) { |
| 355 | Result = Function::Create( |
| 356 | TypeBuilder<int64_t(void), false>::get(Context), |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 357 | GlobalValue::ExternalLinkage, "__main", M.get()); |
Andrew Kaylor | 1ca510e | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 358 | } else { |
| 359 | Result = Function::Create( |
| 360 | TypeBuilder<int32_t(void), false>::get(Context), |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 361 | GlobalValue::ExternalLinkage, "__main", M.get()); |
Andrew Kaylor | 1ca510e | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 362 | } |
| 363 | BasicBlock *BB = BasicBlock::Create(Context, "__main", Result); |
| 364 | Builder.SetInsertPoint(BB); |
| 365 | Value *ReturnVal; |
| 366 | if (TargetTriple.isArch64Bit()) |
| 367 | ReturnVal = ConstantInt::get(Context, APInt(64, 0)); |
| 368 | else |
| 369 | ReturnVal = ConstantInt::get(Context, APInt(32, 0)); |
| 370 | Builder.CreateRet(ReturnVal); |
| 371 | |
| 372 | // Add this new module to the ExecutionEngine. |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 373 | EE->addModule(std::move(M)); |
Andrew Kaylor | 1ca510e | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | |
Chris Lattner | d7ff578 | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 377 | //===----------------------------------------------------------------------===// |
Chris Lattner | d7ff578 | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 378 | // main Driver function |
| 379 | // |
Misha Brukman | 5b255e5 | 2003-10-14 21:39:53 +0000 | [diff] [blame] | 380 | int main(int argc, char **argv, char * const *envp) { |
Chris Lattner | e3fc2d1 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 381 | sys::PrintStackTraceOnErrorSignal(); |
| 382 | PrettyStackTraceProgram X(argc, argv); |
Eli Bendersky | 4c64758 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 383 | |
Owen Anderson | 19251ec | 2009-07-15 22:16:10 +0000 | [diff] [blame] | 384 | LLVMContext &Context = getGlobalContext(); |
Reid Spencer | e586f2e | 2007-03-03 18:21:44 +0000 | [diff] [blame] | 385 | atexit(do_shutdown); // Call llvm_shutdown() on exit. |
Daniel Dunbar | 3d92d93 | 2009-07-16 02:04:54 +0000 | [diff] [blame] | 386 | |
| 387 | // If we have a native target, initialize it to ensure it is linked in and |
| 388 | // usable by the JIT. |
| 389 | InitializeNativeTarget(); |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 390 | InitializeNativeTargetAsmPrinter(); |
Jim Grosbach | 2cce3f9 | 2012-11-05 19:06:05 +0000 | [diff] [blame] | 391 | InitializeNativeTargetAsmParser(); |
Daniel Dunbar | 3d92d93 | 2009-07-16 02:04:54 +0000 | [diff] [blame] | 392 | |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 393 | cl::ParseCommandLineOptions(argc, argv, |
Dan Gohman | 2c6a821 | 2007-10-08 15:45:12 +0000 | [diff] [blame] | 394 | "llvm interpreter & dynamic compiler\n"); |
Reid Spencer | 996ec72 | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 395 | |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 396 | // If the user doesn't want core files, disable them. |
| 397 | if (DisableCoreFiles) |
| 398 | sys::Process::PreventCoreFiles(); |
Eli Bendersky | 4c64758 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 399 | |
Gabor Greif | e16561c | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 400 | // Load the bitcode... |
Daniel Dunbar | 9589bf8 | 2010-11-13 00:28:01 +0000 | [diff] [blame] | 401 | SMDiagnostic Err; |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 402 | std::unique_ptr<Module> Owner(ParseIRFile(InputFile, Err, Context)); |
| 403 | Module *Mod = Owner.get(); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 404 | if (!Mod) { |
Chris Lattner | a3a0681 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 405 | Err.print(argv[0], errs()); |
Daniel Dunbar | 9589bf8 | 2010-11-13 00:28:01 +0000 | [diff] [blame] | 406 | return 1; |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 409 | if (EnableCacheManager) { |
Eric Christopher | b9fd9ed | 2014-08-07 22:02:54 +0000 | [diff] [blame] | 410 | if (UseMCJIT) { |
| 411 | std::string CacheName("file:"); |
| 412 | CacheName.append(InputFile); |
| 413 | Mod->setModuleIdentifier(CacheName); |
| 414 | } else |
| 415 | errs() << "warning: -enable-cache-manager can only be used with MCJIT."; |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 418 | // If not jitting lazily, load the whole bitcode file eagerly too. |
| 419 | if (NoLazyCompilation) { |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 420 | if (std::error_code EC = Mod->materializeAllPermanently()) { |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 421 | errs() << argv[0] << ": bitcode didn't read correctly.\n"; |
Rafael Espindola | e9fab9b | 2014-01-14 23:51:27 +0000 | [diff] [blame] | 422 | errs() << "Reason: " << EC.message() << "\n"; |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 423 | exit(1); |
| 424 | } |
Evan Cheng | 0422bef | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 425 | } |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 426 | |
Daniel Malea | a960c54 | 2013-06-28 19:11:40 +0000 | [diff] [blame] | 427 | if (DebugIR) { |
Eric Christopher | b9fd9ed | 2014-08-07 22:02:54 +0000 | [diff] [blame] | 428 | if (!UseMCJIT) { |
| 429 | errs() << "warning: -debug-ir used without -use-mcjit. Only partial debug" |
| 430 | << " information will be emitted by the non-MC JIT engine. To see full" |
| 431 | << " source debug information, enable the flag '-use-mcjit'.\n"; |
| 432 | |
| 433 | } |
Daniel Malea | a960c54 | 2013-06-28 19:11:40 +0000 | [diff] [blame] | 434 | ModulePass *DebugIRPass = createDebugIRPass(); |
| 435 | DebugIRPass->runOnModule(*Mod); |
| 436 | } |
| 437 | |
Rafael Espindola | e9fab9b | 2014-01-14 23:51:27 +0000 | [diff] [blame] | 438 | std::string ErrorMsg; |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 439 | EngineBuilder builder(std::move(Owner)); |
Jeffrey Yasskin | 31faeff | 2010-02-05 16:19:36 +0000 | [diff] [blame] | 440 | builder.setMArch(MArch); |
| 441 | builder.setMCPU(MCPU); |
| 442 | builder.setMAttrs(MAttrs); |
Evan Cheng | 2129f59 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 443 | builder.setRelocationModel(RelocModel); |
Evan Cheng | efd9b42 | 2011-07-20 07:51:56 +0000 | [diff] [blame] | 444 | builder.setCodeModel(CMModel); |
Daniel Dunbar | a78d809 | 2009-07-18 08:07:13 +0000 | [diff] [blame] | 445 | builder.setErrorStr(&ErrorMsg); |
| 446 | builder.setEngineKind(ForceInterpreter |
Reid Kleckner | fc8a2d5 | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 447 | ? EngineKind::Interpreter |
| 448 | : EngineKind::JIT); |
| 449 | |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 450 | // If we are supposed to override the target triple, do so now. |
| 451 | if (!TargetTriple.empty()) |
Duncan Sands | 3bd97fe | 2010-08-28 01:30:02 +0000 | [diff] [blame] | 452 | Mod->setTargetTriple(Triple::normalize(TargetTriple)); |
Evan Cheng | 0422bef | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 453 | |
Eli Bendersky | 4c64758 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 454 | // Enable MCJIT if desired. |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 455 | RTDyldMemoryManager *RTDyldMM = nullptr; |
Eric Christopher | b9fd9ed | 2014-08-07 22:02:54 +0000 | [diff] [blame] | 456 | if (UseMCJIT && !ForceInterpreter) { |
| 457 | builder.setUseMCJIT(true); |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 458 | if (RemoteMCJIT) |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 459 | RTDyldMM = new RemoteMemoryManager(); |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 460 | else |
Filip Pizlo | 9bc53e8 | 2013-05-14 19:29:00 +0000 | [diff] [blame] | 461 | RTDyldMM = new SectionMemoryManager(); |
| 462 | builder.setMCJITMemoryManager(RTDyldMM); |
Benjamin Kramer | 76004e6 | 2012-05-20 17:24:08 +0000 | [diff] [blame] | 463 | } else { |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 464 | if (RemoteMCJIT) { |
| 465 | errs() << "error: Remote process execution requires -use-mcjit\n"; |
| 466 | exit(1); |
| 467 | } |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 468 | builder.setJITMemoryManager(ForceInterpreter ? nullptr : |
Benjamin Kramer | 76004e6 | 2012-05-20 17:24:08 +0000 | [diff] [blame] | 469 | JITMemoryManager::CreateDefaultMemManager()); |
Eli Bendersky | 4c64758 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 470 | } |
Daniel Dunbar | 70ff8b0 | 2010-11-17 16:06:37 +0000 | [diff] [blame] | 471 | |
Evan Cheng | 09bd0b1 | 2009-05-04 23:05:19 +0000 | [diff] [blame] | 472 | CodeGenOpt::Level OLvl = CodeGenOpt::Default; |
| 473 | switch (OptLevel) { |
| 474 | default: |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 475 | errs() << argv[0] << ": invalid optimization level.\n"; |
Evan Cheng | 09bd0b1 | 2009-05-04 23:05:19 +0000 | [diff] [blame] | 476 | return 1; |
| 477 | case ' ': break; |
| 478 | case '0': OLvl = CodeGenOpt::None; break; |
Evan Cheng | 5a28638 | 2009-10-16 21:02:20 +0000 | [diff] [blame] | 479 | case '1': OLvl = CodeGenOpt::Less; break; |
Evan Cheng | 09bd0b1 | 2009-05-04 23:05:19 +0000 | [diff] [blame] | 480 | case '2': OLvl = CodeGenOpt::Default; break; |
| 481 | case '3': OLvl = CodeGenOpt::Aggressive; break; |
| 482 | } |
Reid Kleckner | fc8a2d5 | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 483 | builder.setOptLevel(OLvl); |
| 484 | |
Tim Northover | d4a2f5b | 2012-10-12 09:55:13 +0000 | [diff] [blame] | 485 | TargetOptions Options; |
| 486 | Options.UseSoftFloat = GenerateSoftFloatCalls; |
| 487 | if (FloatABIForCalls != FloatABI::Default) |
| 488 | Options.FloatABIType = FloatABIForCalls; |
| 489 | if (GenerateSoftFloatCalls) |
| 490 | FloatABIForCalls = FloatABI::Soft; |
| 491 | |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 492 | // Remote target execution doesn't handle EH or debug registration. |
| 493 | if (!RemoteMCJIT) { |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 494 | Options.JITEmitDebugInfo = EmitJitDebugInfo; |
| 495 | Options.JITEmitDebugInfoToDisk = EmitJitDebugInfoToDisk; |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 496 | } |
Nick Lewycky | b0e9789 | 2012-04-18 08:34:12 +0000 | [diff] [blame] | 497 | |
Tim Northover | d4a2f5b | 2012-10-12 09:55:13 +0000 | [diff] [blame] | 498 | builder.setTargetOptions(Options); |
| 499 | |
Reid Kleckner | fc8a2d5 | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 500 | EE = builder.create(); |
Chris Lattner | f840ed7 | 2009-07-07 18:31:09 +0000 | [diff] [blame] | 501 | if (!EE) { |
| 502 | if (!ErrorMsg.empty()) |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 503 | errs() << argv[0] << ": error creating EE: " << ErrorMsg << "\n"; |
Chris Lattner | f840ed7 | 2009-07-07 18:31:09 +0000 | [diff] [blame] | 504 | else |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 505 | errs() << argv[0] << ": unknown error creating EE!\n"; |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 506 | exit(1); |
| 507 | } |
| 508 | |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 509 | if (EnableCacheManager) { |
Lang Hames | 1ddecc0 | 2014-01-09 05:24:05 +0000 | [diff] [blame] | 510 | CacheManager = new LLIObjectCache(ObjectCacheDir); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 511 | EE->setObjectCache(CacheManager); |
| 512 | } |
| 513 | |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 514 | // Load any additional modules specified on the command line. |
| 515 | for (unsigned i = 0, e = ExtraModules.size(); i != e; ++i) { |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 516 | std::unique_ptr<Module> XMod(ParseIRFile(ExtraModules[i], Err, Context)); |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 517 | if (!XMod) { |
| 518 | Err.print(argv[0], errs()); |
| 519 | return 1; |
| 520 | } |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 521 | if (EnableCacheManager) { |
Eric Christopher | b9fd9ed | 2014-08-07 22:02:54 +0000 | [diff] [blame] | 522 | if (UseMCJIT) { |
| 523 | std::string CacheName("file:"); |
| 524 | CacheName.append(ExtraModules[i]); |
| 525 | XMod->setModuleIdentifier(CacheName); |
| 526 | } |
| 527 | // else, we already printed a warning above. |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 528 | } |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 529 | EE->addModule(std::move(XMod)); |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Rafael Espindola | 061beab | 2014-08-20 15:19:37 +0000 | [diff] [blame] | 532 | std::vector<std::unique_ptr<MemoryBuffer>> Buffers; |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 533 | for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) { |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 534 | ErrorOr<object::OwningBinary<object::ObjectFile>> Obj = |
Rafael Espindola | 51cc360 | 2014-01-22 00:14:49 +0000 | [diff] [blame] | 535 | object::ObjectFile::createObjectFile(ExtraObjects[i]); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 536 | if (!Obj) { |
| 537 | Err.print(argv[0], errs()); |
| 538 | return 1; |
| 539 | } |
Rafael Espindola | 061beab | 2014-08-20 15:19:37 +0000 | [diff] [blame] | 540 | object::OwningBinary<object::ObjectFile> &O = Obj.get(); |
| 541 | EE->addObjectFile(std::move(O.getBinary())); |
| 542 | Buffers.push_back(std::move(O.getBuffer())); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | for (unsigned i = 0, e = ExtraArchives.size(); i != e; ++i) { |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 546 | ErrorOr<std::unique_ptr<MemoryBuffer>> ArBufOrErr = |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 547 | MemoryBuffer::getFileOrSTDIN(ExtraArchives[i]); |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 548 | if (!ArBufOrErr) { |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 549 | Err.print(argv[0], errs()); |
| 550 | return 1; |
| 551 | } |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 552 | std::unique_ptr<MemoryBuffer> &ArBuf = ArBufOrErr.get(); |
Rafael Espindola | e192341 | 2014-08-01 18:31:17 +0000 | [diff] [blame] | 553 | |
| 554 | ErrorOr<std::unique_ptr<object::Archive>> ArOrErr = |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 555 | object::Archive::create(ArBuf->getMemBufferRef()); |
Rafael Espindola | e192341 | 2014-08-01 18:31:17 +0000 | [diff] [blame] | 556 | if (std::error_code EC = ArOrErr.getError()) { |
| 557 | errs() << EC.message(); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 558 | return 1; |
| 559 | } |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 560 | std::unique_ptr<object::Archive> &Ar = ArOrErr.get(); |
| 561 | |
| 562 | object::OwningBinary<object::Archive> OB(std::move(Ar), std::move(ArBuf)); |
| 563 | |
| 564 | EE->addArchive(std::move(OB)); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Andrew Kaylor | 1ca510e | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 567 | // If the target is Cygwin/MingW and we are generating remote code, we |
| 568 | // need an extra module to help out with linking. |
| 569 | if (RemoteMCJIT && Triple(Mod->getTargetTriple()).isOSCygMing()) { |
| 570 | addCygMingExtraModule(EE, Context, Mod->getTargetTriple()); |
| 571 | } |
| 572 | |
Eli Bendersky | 5262ad2 | 2012-03-13 08:33:15 +0000 | [diff] [blame] | 573 | // The following functions have no effect if their respective profiling |
| 574 | // support wasn't enabled in the build configuration. |
| 575 | EE->RegisterJITEventListener( |
| 576 | JITEventListener::createOProfileJITEventListener()); |
| 577 | EE->RegisterJITEventListener( |
| 578 | JITEventListener::createIntelJITEventListener()); |
Jeffrey Yasskin | 0b08f3d | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 579 | |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 580 | if (!NoLazyCompilation && RemoteMCJIT) { |
| 581 | errs() << "warning: remote mcjit does not support lazy compilation\n"; |
| 582 | NoLazyCompilation = true; |
| 583 | } |
Jeffrey Yasskin | aa8814a | 2009-10-27 22:39:42 +0000 | [diff] [blame] | 584 | EE->DisableLazyCompilation(NoLazyCompilation); |
Evan Cheng | 0422bef | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 585 | |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 586 | // If the user specifically requested an argv[0] to pass into the program, |
| 587 | // do it now. |
| 588 | if (!FakeArgv0.empty()) { |
| 589 | InputFile = FakeArgv0; |
| 590 | } else { |
| 591 | // Otherwise, if there is a .bc suffix on the executable strip it off, it |
| 592 | // might confuse the program. |
Benjamin Kramer | a944a9a | 2010-04-15 11:33:14 +0000 | [diff] [blame] | 593 | if (StringRef(InputFile).endswith(".bc")) |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 594 | InputFile.erase(InputFile.length() - 3); |
| 595 | } |
| 596 | |
| 597 | // Add the module's name to the start of the vector of arguments to main(). |
| 598 | InputArgv.insert(InputArgv.begin(), InputFile); |
| 599 | |
| 600 | // Call the main function from M as if its signature were: |
| 601 | // int main (int argc, char **argv, const char **envp) |
| 602 | // using the contents of Args to determine argc & argv, and the contents of |
| 603 | // EnvVars to determine envp. |
| 604 | // |
Evan Cheng | 6158254 | 2008-11-05 23:21:52 +0000 | [diff] [blame] | 605 | Function *EntryFn = Mod->getFunction(EntryFunc); |
| 606 | if (!EntryFn) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 607 | errs() << '\'' << EntryFunc << "\' function not found in module.\n"; |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 608 | return -1; |
| 609 | } |
| 610 | |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 611 | // Reset errno to zero on entry to main. |
| 612 | errno = 0; |
Eli Bendersky | 4c64758 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 613 | |
Andrew Kaylor | c87c347 | 2013-10-02 18:04:40 +0000 | [diff] [blame] | 614 | int Result; |
| 615 | |
Andrew Kaylor | 58365b9 | 2012-11-27 19:49:00 +0000 | [diff] [blame] | 616 | if (!RemoteMCJIT) { |
Andrew Kaylor | 6587bcf | 2013-10-11 22:47:10 +0000 | [diff] [blame] | 617 | // If the program doesn't explicitly call exit, we will need the Exit |
| 618 | // function later on to make an explicit call, so get the function now. |
| 619 | Constant *Exit = Mod->getOrInsertFunction("exit", Type::getVoidTy(Context), |
| 620 | Type::getInt32Ty(Context), |
| 621 | NULL); |
| 622 | |
Andrew Kaylor | c87c347 | 2013-10-02 18:04:40 +0000 | [diff] [blame] | 623 | // Run static constructors. |
Eric Christopher | b9fd9ed | 2014-08-07 22:02:54 +0000 | [diff] [blame] | 624 | if (UseMCJIT && !ForceInterpreter) { |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 625 | // Give MCJIT a chance to apply relocations and set page permissions. |
| 626 | EE->finalizeObject(); |
| 627 | } |
| 628 | EE->runStaticConstructorsDestructors(false); |
Evan Cheng | 0422bef | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 629 | |
Eric Christopher | b9fd9ed | 2014-08-07 22:02:54 +0000 | [diff] [blame] | 630 | if (!UseMCJIT && NoLazyCompilation) { |
| 631 | for (Module::iterator I = Mod->begin(), E = Mod->end(); I != E; ++I) { |
| 632 | Function *Fn = &*I; |
| 633 | if (Fn != EntryFn && !Fn->isDeclaration()) |
| 634 | EE->getPointerToFunction(Fn); |
| 635 | } |
| 636 | } |
| 637 | |
NAKAMURA Takumi | 87e0880 | 2013-12-07 11:21:42 +0000 | [diff] [blame] | 638 | // Trigger compilation separately so code regions that need to be |
Andrew Kaylor | c87c347 | 2013-10-02 18:04:40 +0000 | [diff] [blame] | 639 | // invalidated will be known. |
| 640 | (void)EE->getPointerToFunction(EntryFn); |
| 641 | // Clear instruction cache before code will be executed. |
| 642 | if (RTDyldMM) |
| 643 | static_cast<SectionMemoryManager*>(RTDyldMM)->invalidateInstructionCache(); |
| 644 | |
| 645 | // Run main. |
| 646 | Result = EE->runFunctionAsMain(EntryFn, InputArgv, envp); |
| 647 | |
| 648 | // Run static destructors. |
| 649 | EE->runStaticConstructorsDestructors(true); |
| 650 | |
| 651 | // If the program didn't call exit explicitly, we should call it now. |
| 652 | // This ensures that any atexit handlers get called correctly. |
| 653 | if (Function *ExitF = dyn_cast<Function>(Exit)) { |
| 654 | std::vector<GenericValue> Args; |
| 655 | GenericValue ResultGV; |
| 656 | ResultGV.IntVal = APInt(32, Result); |
| 657 | Args.push_back(ResultGV); |
| 658 | EE->runFunction(ExitF, Args); |
| 659 | errs() << "ERROR: exit(" << Result << ") returned!\n"; |
| 660 | abort(); |
| 661 | } else { |
| 662 | errs() << "ERROR: exit defined with wrong prototype!\n"; |
| 663 | abort(); |
| 664 | } |
| 665 | } else { |
| 666 | // else == "if (RemoteMCJIT)" |
| 667 | |
| 668 | // Remote target MCJIT doesn't (yet) support static constructors. No reason |
| 669 | // it couldn't. This is a limitation of the LLI implemantation, not the |
| 670 | // MCJIT itself. FIXME. |
| 671 | // |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 672 | RemoteMemoryManager *MM = static_cast<RemoteMemoryManager*>(RTDyldMM); |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 673 | // Everything is prepared now, so lay out our program for the target |
| 674 | // address space, assign the section addresses to resolve any relocations, |
| 675 | // and send it to the target. |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 676 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 677 | std::unique_ptr<RemoteTarget> Target; |
Alp Toker | a118638 | 2014-01-22 21:52:35 +0000 | [diff] [blame] | 678 | if (!ChildExecPath.empty()) { // Remote execution on a child process |
Alp Toker | ce4ab59 | 2014-01-23 19:57:16 +0000 | [diff] [blame] | 679 | #ifndef LLVM_ON_UNIX |
| 680 | // FIXME: Remove this pointless fallback mode which causes tests to "pass" |
| 681 | // on platforms where they should XFAIL. |
| 682 | errs() << "Warning: host does not support external remote targets.\n" |
| 683 | << " Defaulting to simulated remote execution\n"; |
| 684 | Target.reset(new RemoteTarget); |
| 685 | #else |
| 686 | if (!sys::fs::can_execute(ChildExecPath)) { |
| 687 | errs() << "Unable to find usable child executable: '" << ChildExecPath |
| 688 | << "'\n"; |
| 689 | return -1; |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 690 | } |
Alp Toker | ce4ab59 | 2014-01-23 19:57:16 +0000 | [diff] [blame] | 691 | Target.reset(new RemoteTargetExternal(ChildExecPath)); |
| 692 | #endif |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 693 | } else { |
| 694 | // No child process name provided, use simulated remote execution. |
Alp Toker | ce4ab59 | 2014-01-23 19:57:16 +0000 | [diff] [blame] | 695 | Target.reset(new RemoteTarget); |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 698 | // Give the memory manager a pointer to our remote target interface object. |
| 699 | MM->setRemoteTarget(Target.get()); |
| 700 | |
| 701 | // Create the remote target. |
Renato Golin | 695895c | 2014-01-14 22:43:43 +0000 | [diff] [blame] | 702 | if (!Target->create()) { |
| 703 | errs() << "ERROR: " << Target->getErrorMsg() << "\n"; |
| 704 | return EXIT_FAILURE; |
| 705 | } |
Jim Grosbach | 748b947 | 2012-08-28 23:22:30 +0000 | [diff] [blame] | 706 | |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 707 | // Since we're executing in a (at least simulated) remote address space, |
| 708 | // we can't use the ExecutionEngine::runFunctionAsMain(). We have to |
| 709 | // grab the function address directly here and tell the remote target |
| 710 | // to execute the function. |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 711 | // |
| 712 | // Our memory manager will map generated code into the remote address |
| 713 | // space as it is loaded and copy the bits over during the finalizeMemory |
| 714 | // operation. |
| 715 | // |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 716 | // FIXME: argv and envp handling. |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 717 | uint64_t Entry = EE->getFunctionAddress(EntryFn->getName().str()); |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 718 | |
Tim Northover | 79ab7f7 | 2013-05-19 09:55:06 +0000 | [diff] [blame] | 719 | DEBUG(dbgs() << "Executing '" << EntryFn->getName() << "' at 0x" |
| 720 | << format("%llx", Entry) << "\n"); |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 721 | |
Renato Golin | 695895c | 2014-01-14 22:43:43 +0000 | [diff] [blame] | 722 | if (!Target->executeCode(Entry, Result)) |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 723 | errs() << "ERROR: " << Target->getErrorMsg() << "\n"; |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 724 | |
Andrew Kaylor | c87c347 | 2013-10-02 18:04:40 +0000 | [diff] [blame] | 725 | // Like static constructors, the remote target MCJIT support doesn't handle |
| 726 | // this yet. It could. FIXME. |
| 727 | |
| 728 | // Stop the remote target |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 729 | Target->stop(); |
Chris Lattner | 2785bdb | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 730 | } |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 731 | |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 732 | return Result; |
Chris Lattner | d7ff578 | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 733 | } |