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