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