| Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 1 | //===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===// | 
| Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // | 
| John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | //                     The LLVM Compiler Infrastructure | 
 | 4 | // | 
| Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source | 
 | 6 | // License. See LICENSE.TXT for details. | 
| Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // | 
| John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 9 | // | 
| Chris Lattner | 7efea1d | 2003-12-26 05:07:35 +0000 | [diff] [blame] | 10 | // This utility provides a simple wrapper around the LLVM Execution Engines, | 
 | 11 | // which allow the direct execution of LLVM programs through a Just-In-Time | 
| Torok Edwin | db9c028 | 2009-07-03 12:11:32 +0000 | [diff] [blame] | 12 | // compiler, or through an interpreter if no JIT is available for this platform. | 
| Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 13 | // | 
 | 14 | //===----------------------------------------------------------------------===// | 
 | 15 |  | 
| Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "lli" | 
| Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/LLVMContext.h" | 
| Andrew Kaylor | b868e91 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 18 | #include "RemoteMemoryManager.h" | 
| Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 19 | #include "RemoteTarget.h" | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 20 | #include "RemoteTargetExternal.h" | 
| Duncan Sands | 75ebbce | 2010-08-28 01:30:02 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Triple.h" | 
| Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 22 | #include "llvm/Bitcode/ReaderWriter.h" | 
| Chris Lattner | d3a680a | 2006-08-01 22:34:35 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/LinkAllCodegenComponents.h" | 
| Brian Gaeke | d1cab3e | 2003-09-05 19:42:34 +0000 | [diff] [blame] | 24 | #include "llvm/ExecutionEngine/GenericValue.h" | 
| Jeffrey Yasskin | df5a7da | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 25 | #include "llvm/ExecutionEngine/Interpreter.h" | 
 | 26 | #include "llvm/ExecutionEngine/JIT.h" | 
 | 27 | #include "llvm/ExecutionEngine/JITEventListener.h" | 
| Jim Grosbach | bf9ab93 | 2012-01-11 21:12:51 +0000 | [diff] [blame] | 28 | #include "llvm/ExecutionEngine/JITMemoryManager.h" | 
| Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 29 | #include "llvm/ExecutionEngine/MCJIT.h" | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 30 | #include "llvm/ExecutionEngine/ObjectCache.h" | 
| Andrew Kaylor | 257a009 | 2012-11-27 19:49:00 +0000 | [diff] [blame] | 31 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" | 
| Andrew Kaylor | 47b7fd5 | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 32 | #include "llvm/IR/IRBuilder.h" | 
| Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Module.h" | 
 | 34 | #include "llvm/IR/Type.h" | 
| Andrew Kaylor | 47b7fd5 | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 35 | #include "llvm/IR/TypeBuilder.h" | 
| Chandler Carruth | 7fc162f | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 36 | #include "llvm/IRReader/IRReader.h" | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 37 | #include "llvm/Object/Archive.h" | 
 | 38 | #include "llvm/Object/ObjectFile.h" | 
| Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 39 | #include "llvm/Support/CommandLine.h" | 
| Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Debug.h" | 
 | 41 | #include "llvm/Support/DynamicLibrary.h" | 
 | 42 | #include "llvm/Support/Format.h" | 
| Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 43 | #include "llvm/Support/ManagedStatic.h" | 
| Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 44 | #include "llvm/Support/MathExtras.h" | 
 | 45 | #include "llvm/Support/Memory.h" | 
| Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 46 | #include "llvm/Support/MemoryBuffer.h" | 
| Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 47 | #include "llvm/Support/PluginLoader.h" | 
| Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 48 | #include "llvm/Support/PrettyStackTrace.h" | 
| Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Process.h" | 
| Andrew Kaylor | 0ab5c6c | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 50 | #include "llvm/Support/Program.h" | 
| Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 51 | #include "llvm/Support/Signals.h" | 
| Chandler Carruth | 7fc162f | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 52 | #include "llvm/Support/SourceMgr.h" | 
| Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 53 | #include "llvm/Support/TargetSelect.h" | 
| Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 54 | #include "llvm/Support/raw_ostream.h" | 
| Daniel Malea | e16e687 | 2013-06-28 19:11:40 +0000 | [diff] [blame] | 55 | #include "llvm/Transforms/Instrumentation.h" | 
| Chris Lattner | 0204032 | 2007-04-27 17:02:33 +0000 | [diff] [blame] | 56 | #include <cerrno> | 
| NAKAMURA Takumi | a13d14a | 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 | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 65 | using namespace llvm; | 
 | 66 |  | 
| Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 67 | namespace { | 
 | 68 |   cl::opt<std::string> | 
| Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 69 |   InputFile(cl::desc("<input bitcode>"), cl::Positional, cl::init("-")); | 
| Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 70 |  | 
| Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 71 |   cl::list<std::string> | 
 | 72 |   InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>...")); | 
| Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 73 |  | 
| Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 74 |   cl::opt<bool> ForceInterpreter("force-interpreter", | 
| Misha Brukman | 3d8a54d | 2003-09-25 18:10:34 +0000 | [diff] [blame] | 75 |                                  cl::desc("Force interpretation: disable JIT"), | 
 | 76 |                                  cl::init(false)); | 
| Evan Cheng | e1a4eda | 2008-08-08 08:12:06 +0000 | [diff] [blame] | 77 |  | 
| Daniel Dunbar | 6d13597 | 2010-11-17 16:06:37 +0000 | [diff] [blame] | 78 |   cl::opt<bool> UseMCJIT( | 
 | 79 |     "use-mcjit", cl::desc("Enable use of the MC-based JIT (if available)"), | 
 | 80 |     cl::init(false)); | 
 | 81 |  | 
| Daniel Malea | e16e687 | 2013-06-28 19:11:40 +0000 | [diff] [blame] | 82 |   cl::opt<bool> DebugIR( | 
 | 83 |     "debug-ir", cl::desc("Generate debug information to allow debugging IR."), | 
 | 84 |     cl::init(false)); | 
 | 85 |  | 
| Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 86 |   // The MCJIT supports building for a target address space separate from | 
 | 87 |   // the JIT compilation process. Use a forked process and a copying | 
 | 88 |   // memory manager with IPC to execute using this functionality. | 
 | 89 |   cl::opt<bool> RemoteMCJIT("remote-mcjit", | 
 | 90 |     cl::desc("Execute MCJIT'ed code in a separate process."), | 
 | 91 |     cl::init(false)); | 
 | 92 |  | 
| Andrew Kaylor | 0ab5c6c | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 93 |   // Manually specify the child process for remote execution. This overrides | 
 | 94 |   // the simulated remote execution that allocates address space for child | 
| Andrew Kaylor | 5f7a577 | 2013-10-29 01:33:14 +0000 | [diff] [blame] | 95 |   // execution. The child process will be executed and will communicate with | 
 | 96 |   // lli via stdin/stdout pipes. | 
| Andrew Kaylor | 0ab5c6c | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 97 |   cl::opt<std::string> | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 98 |   ChildExecPath("mcjit-remote-process", | 
 | 99 |                 cl::desc("Specify the filename of the process to launch " | 
 | 100 |                          "for remote MCJIT execution.  If none is specified," | 
 | 101 |                          "\n\tremote execution will be simulated in-process."), | 
 | 102 |                 cl::value_desc("filename"), cl::init("")); | 
| Andrew Kaylor | 0ab5c6c | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 103 |  | 
| Evan Cheng | 712e80e | 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 | e1a4eda | 2008-08-08 08:12:06 +0000 | [diff] [blame] | 112 |  | 
| Chris Lattner | 3015e60 | 2005-12-16 05:00:21 +0000 | [diff] [blame] | 113 |   cl::opt<std::string> | 
| Chris Lattner | 60844d4 | 2005-12-16 05:19:18 +0000 | [diff] [blame] | 114 |   TargetTriple("mtriple", cl::desc("Override target triple for module")); | 
| Evan Cheng | ec740e3 | 2008-11-05 23:21:52 +0000 | [diff] [blame] | 115 |  | 
 | 116 |   cl::opt<std::string> | 
| Jeffrey Yasskin | 4688261 | 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 | ec740e3 | 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 | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 138 |  | 
| Andrew Kaylor | b868e91 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 139 |   cl::list<std::string> | 
| Andrew Kaylor | 61abf15 | 2013-10-28 21:58:15 +0000 | [diff] [blame] | 140 |   ExtraModules("extra-module", | 
| Andrew Kaylor | b868e91 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 141 |          cl::desc("Extra modules to be loaded"), | 
| Alp Toker | 730e3c6 | 2013-10-28 22:51:25 +0000 | [diff] [blame] | 142 |          cl::value_desc("input bitcode")); | 
| Andrew Kaylor | b868e91 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 143 |  | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [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", | 
 | 156 |         cl::desc("Use cache manager to save/load mdoules"), | 
 | 157 |         cl::init(false)); | 
 | 158 |  | 
 | 159 |   cl::opt<std::string> | 
 | 160 |   ObjectCacheDir("object-cache-dir", | 
 | 161 |                   cl::desc("Directory to store cached object files " | 
 | 162 |                            "(must be user writable)"), | 
 | 163 |                   cl::init("")); | 
 | 164 |  | 
| Chris Lattner | e69671d | 2003-10-28 22:51:44 +0000 | [diff] [blame] | 165 |   cl::opt<std::string> | 
 | 166 |   FakeArgv0("fake-argv0", | 
 | 167 |             cl::desc("Override the 'argv[0]' value passed into the executing" | 
 | 168 |                      " program"), cl::value_desc("executable")); | 
| Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 169 |  | 
| Chris Lattner | 43f249a | 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 | c290a5b | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 173 |  | 
 | 174 |   cl::opt<bool> | 
| Evan Cheng | 03dace8 | 2008-05-21 18:20:21 +0000 | [diff] [blame] | 175 |   NoLazyCompilation("disable-lazy-compilation", | 
| Evan Cheng | c290a5b | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 176 |                   cl::desc("Disable JIT lazy compilation"), | 
 | 177 |                   cl::init(false)); | 
| Evan Cheng | 4396613 | 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 | 34ad6db | 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 | 9a14841 | 2012-04-18 08:34:12 +0000 | [diff] [blame] | 210 |   cl::opt<bool> | 
| Tim Northover | 77b4c69 | 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 | 9a14841 | 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 | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 244 | } | 
| Chris Lattner | 43e3f7c | 2001-10-27 08:43:52 +0000 | [diff] [blame] | 245 |  | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 246 | //===----------------------------------------------------------------------===// | 
 | 247 | // Object cache | 
 | 248 | // | 
 | 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 | 
 | 253 | // alongside their originating bitcodes. | 
 | 254 | // | 
 | 255 | class LLIObjectCache : public ObjectCache { | 
 | 256 | public: | 
 | 257 |   LLIObjectCache(const std::string& CacheDir) : CacheDir(CacheDir) { | 
 | 258 |     // Add trailing '/' to cache dir if necessary. | 
 | 259 |     if (!this->CacheDir.empty() && | 
 | 260 |         this->CacheDir[this->CacheDir.size() - 1] != '/') | 
 | 261 |       this->CacheDir += '/'; | 
 | 262 |   } | 
 | 263 |   virtual ~LLIObjectCache() {} | 
 | 264 |  | 
 | 265 |   void notifyObjectCompiled(const Module *M, const MemoryBuffer *Obj) override { | 
 | 266 |     const std::string ModuleID = M->getModuleIdentifier(); | 
 | 267 |     std::string CacheName; | 
 | 268 |     if (!getCacheFilename(ModuleID, CacheName)) | 
 | 269 |       return; | 
 | 270 |     std::string errStr; | 
 | 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 |     } | 
 | 276 |     raw_fd_ostream outfile(CacheName.c_str(), errStr, sys::fs::F_None); | 
 | 277 |     outfile.write(Obj->getBufferStart(), Obj->getBufferSize()); | 
 | 278 |     outfile.close(); | 
 | 279 |   } | 
 | 280 |  | 
 | 281 |   MemoryBuffer* getObject(const Module* M) override { | 
 | 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 |     std::unique_ptr<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: | 
 | 300 |   std::string CacheDir; | 
 | 301 |  | 
 | 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; | 
 | 307 |         std::string CacheSubdir = ModID.substr(PrefixLength); | 
 | 308 | #if defined(_WIN32) | 
 | 309 |         // Transform "X:\foo" => "/X\foo" for convenience. | 
 | 310 |         if (isalpha(CacheSubdir[0]) && CacheSubdir[1] == ':') { | 
 | 311 |           CacheSubdir[1] = CacheSubdir[0]; | 
 | 312 |           CacheSubdir[0] = '/'; | 
 | 313 |         } | 
 | 314 | #endif | 
 | 315 |     CacheName = CacheDir + CacheSubdir; | 
 | 316 |     size_t pos = CacheName.rfind('.'); | 
 | 317 |     CacheName.replace(pos, CacheName.length() - pos, ".o"); | 
 | 318 |     return true; | 
 | 319 |   } | 
 | 320 | }; | 
 | 321 |  | 
| Reid Spencer | f70d677 | 2007-03-03 18:21:44 +0000 | [diff] [blame] | 322 | static ExecutionEngine *EE = 0; | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 323 | static LLIObjectCache *CacheManager = 0; | 
| Reid Spencer | f70d677 | 2007-03-03 18:21:44 +0000 | [diff] [blame] | 324 |  | 
 | 325 | static void do_shutdown() { | 
| NAKAMURA Takumi | a13d14a | 2010-10-22 14:53:59 +0000 | [diff] [blame] | 326 |   // Cygwin-1.5 invokes DLL's dtors before atexit handler. | 
 | 327 | #ifndef DO_NOTHING_ATEXIT | 
| Reid Spencer | f70d677 | 2007-03-03 18:21:44 +0000 | [diff] [blame] | 328 |   delete EE; | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 329 |   if (CacheManager) | 
 | 330 |     delete CacheManager; | 
| Reid Spencer | f70d677 | 2007-03-03 18:21:44 +0000 | [diff] [blame] | 331 |   llvm_shutdown(); | 
| NAKAMURA Takumi | a13d14a | 2010-10-22 14:53:59 +0000 | [diff] [blame] | 332 | #endif | 
| Reid Spencer | f70d677 | 2007-03-03 18:21:44 +0000 | [diff] [blame] | 333 | } | 
 | 334 |  | 
| Andrew Kaylor | 47b7fd5 | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 335 | // On Mingw and Cygwin, an external symbol named '__main' is called from the | 
 | 336 | // generated 'main' function to allow static intialization.  To avoid linking | 
 | 337 | // problems with remote targets (because lli's remote target support does not | 
 | 338 | // currently handle external linking) we add a secondary module which defines | 
 | 339 | // an empty '__main' function. | 
 | 340 | static void addCygMingExtraModule(ExecutionEngine *EE, | 
 | 341 |                                   LLVMContext &Context, | 
 | 342 |                                   StringRef TargetTripleStr) { | 
 | 343 |   IRBuilder<> Builder(Context); | 
 | 344 |   Triple TargetTriple(TargetTripleStr); | 
 | 345 |  | 
 | 346 |   // Create a new module. | 
 | 347 |   Module *M = new Module("CygMingHelper", Context); | 
 | 348 |   M->setTargetTriple(TargetTripleStr); | 
 | 349 |  | 
 | 350 |   // Create an empty function named "__main". | 
 | 351 |   Function *Result; | 
 | 352 |   if (TargetTriple.isArch64Bit()) { | 
 | 353 |     Result = Function::Create( | 
 | 354 |       TypeBuilder<int64_t(void), false>::get(Context), | 
 | 355 |       GlobalValue::ExternalLinkage, "__main", M); | 
 | 356 |   } else { | 
 | 357 |     Result = Function::Create( | 
 | 358 |       TypeBuilder<int32_t(void), false>::get(Context), | 
 | 359 |       GlobalValue::ExternalLinkage, "__main", M); | 
 | 360 |   } | 
 | 361 |   BasicBlock *BB = BasicBlock::Create(Context, "__main", Result); | 
 | 362 |   Builder.SetInsertPoint(BB); | 
 | 363 |   Value *ReturnVal; | 
 | 364 |   if (TargetTriple.isArch64Bit()) | 
 | 365 |     ReturnVal = ConstantInt::get(Context, APInt(64, 0)); | 
 | 366 |   else | 
 | 367 |     ReturnVal = ConstantInt::get(Context, APInt(32, 0)); | 
 | 368 |   Builder.CreateRet(ReturnVal); | 
 | 369 |  | 
 | 370 |   // Add this new module to the ExecutionEngine. | 
 | 371 |   EE->addModule(M); | 
 | 372 | } | 
 | 373 |  | 
 | 374 |  | 
| Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 375 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 376 | // main Driver function | 
 | 377 | // | 
| Misha Brukman | c4fb6fd | 2003-10-14 21:39:53 +0000 | [diff] [blame] | 378 | int main(int argc, char **argv, char * const *envp) { | 
| Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 379 |   sys::PrintStackTraceOnErrorSignal(); | 
 | 380 |   PrettyStackTraceProgram X(argc, argv); | 
| Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 381 |  | 
| Owen Anderson | 0d7c695 | 2009-07-15 22:16:10 +0000 | [diff] [blame] | 382 |   LLVMContext &Context = getGlobalContext(); | 
| Reid Spencer | f70d677 | 2007-03-03 18:21:44 +0000 | [diff] [blame] | 383 |   atexit(do_shutdown);  // Call llvm_shutdown() on exit. | 
| Daniel Dunbar | 494d663 | 2009-07-16 02:04:54 +0000 | [diff] [blame] | 384 |  | 
 | 385 |   // If we have a native target, initialize it to ensure it is linked in and | 
 | 386 |   // usable by the JIT. | 
 | 387 |   InitializeNativeTarget(); | 
| Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 388 |   InitializeNativeTargetAsmPrinter(); | 
| Jim Grosbach | 6837232 | 2012-11-05 19:06:05 +0000 | [diff] [blame] | 389 |   InitializeNativeTargetAsmParser(); | 
| Daniel Dunbar | 494d663 | 2009-07-16 02:04:54 +0000 | [diff] [blame] | 390 |  | 
| Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 391 |   cl::ParseCommandLineOptions(argc, argv, | 
| Dan Gohman | 82a13c9 | 2007-10-08 15:45:12 +0000 | [diff] [blame] | 392 |                               "llvm interpreter & dynamic compiler\n"); | 
| Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 393 |  | 
| Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 394 |   // If the user doesn't want core files, disable them. | 
 | 395 |   if (DisableCoreFiles) | 
 | 396 |     sys::Process::PreventCoreFiles(); | 
| Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 397 |  | 
| Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 398 |   // Load the bitcode... | 
| Daniel Dunbar | 46a2716 | 2010-11-13 00:28:01 +0000 | [diff] [blame] | 399 |   SMDiagnostic Err; | 
 | 400 |   Module *Mod = ParseIRFile(InputFile, Err, Context); | 
| Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 401 |   if (!Mod) { | 
| Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 402 |     Err.print(argv[0], errs()); | 
| Daniel Dunbar | 46a2716 | 2010-11-13 00:28:01 +0000 | [diff] [blame] | 403 |     return 1; | 
| Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 404 |   } | 
 | 405 |  | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 406 |   if (EnableCacheManager) { | 
 | 407 |     if (UseMCJIT) { | 
 | 408 |       std::string CacheName("file:"); | 
 | 409 |       CacheName.append(InputFile); | 
 | 410 |       Mod->setModuleIdentifier(CacheName); | 
 | 411 |     } else | 
 | 412 |       errs() << "warning: -enable-cache-manager can only be used with MCJIT."; | 
 | 413 |   } | 
 | 414 |  | 
| Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 415 |   // If not jitting lazily, load the whole bitcode file eagerly too. | 
 | 416 |   if (NoLazyCompilation) { | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 417 |     if (error_code EC = Mod->materializeAllPermanently()) { | 
| Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 418 |       errs() << argv[0] << ": bitcode didn't read correctly.\n"; | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 419 |       errs() << "Reason: " << EC.message() << "\n"; | 
| Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 420 |       exit(1); | 
 | 421 |     } | 
| Evan Cheng | c290a5b | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 422 |   } | 
| Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 423 |  | 
| Daniel Malea | e16e687 | 2013-06-28 19:11:40 +0000 | [diff] [blame] | 424 |   if (DebugIR) { | 
 | 425 |     if (!UseMCJIT) { | 
 | 426 |       errs() << "warning: -debug-ir used without -use-mcjit. Only partial debug" | 
 | 427 |         << " information will be emitted by the non-MC JIT engine. To see full" | 
 | 428 |         << " source debug information, enable the flag '-use-mcjit'.\n"; | 
 | 429 |  | 
 | 430 |     } | 
 | 431 |     ModulePass *DebugIRPass = createDebugIRPass(); | 
 | 432 |     DebugIRPass->runOnModule(*Mod); | 
 | 433 |   } | 
 | 434 |  | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 435 |   std::string ErrorMsg; | 
| Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 436 |   EngineBuilder builder(Mod); | 
| Jeffrey Yasskin | 4688261 | 2010-02-05 16:19:36 +0000 | [diff] [blame] | 437 |   builder.setMArch(MArch); | 
 | 438 |   builder.setMCPU(MCPU); | 
 | 439 |   builder.setMAttrs(MAttrs); | 
| Evan Cheng | 4396613 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 440 |   builder.setRelocationModel(RelocModel); | 
| Evan Cheng | 34ad6db | 2011-07-20 07:51:56 +0000 | [diff] [blame] | 441 |   builder.setCodeModel(CMModel); | 
| Daniel Dunbar | 4dc3136 | 2009-07-18 08:07:13 +0000 | [diff] [blame] | 442 |   builder.setErrorStr(&ErrorMsg); | 
 | 443 |   builder.setEngineKind(ForceInterpreter | 
| Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 444 |                         ? EngineKind::Interpreter | 
 | 445 |                         : EngineKind::JIT); | 
 | 446 |  | 
| Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 447 |   // If we are supposed to override the target triple, do so now. | 
 | 448 |   if (!TargetTriple.empty()) | 
| Duncan Sands | 75ebbce | 2010-08-28 01:30:02 +0000 | [diff] [blame] | 449 |     Mod->setTargetTriple(Triple::normalize(TargetTriple)); | 
| Evan Cheng | c290a5b | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 450 |  | 
| Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 451 |   // Enable MCJIT if desired. | 
| Filip Pizlo | 13a3cf1 | 2013-05-14 19:29:00 +0000 | [diff] [blame] | 452 |   RTDyldMemoryManager *RTDyldMM = 0; | 
| Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 453 |   if (UseMCJIT && !ForceInterpreter) { | 
| Daniel Dunbar | 6d13597 | 2010-11-17 16:06:37 +0000 | [diff] [blame] | 454 |     builder.setUseMCJIT(true); | 
| Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 455 |     if (RemoteMCJIT) | 
| Andrew Kaylor | b868e91 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 456 |       RTDyldMM = new RemoteMemoryManager(); | 
| Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 457 |     else | 
| Filip Pizlo | 13a3cf1 | 2013-05-14 19:29:00 +0000 | [diff] [blame] | 458 |       RTDyldMM = new SectionMemoryManager(); | 
 | 459 |     builder.setMCJITMemoryManager(RTDyldMM); | 
| Benjamin Kramer | 6514551 | 2012-05-20 17:24:08 +0000 | [diff] [blame] | 460 |   } else { | 
| Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 461 |     if (RemoteMCJIT) { | 
 | 462 |       errs() << "error: Remote process execution requires -use-mcjit\n"; | 
 | 463 |       exit(1); | 
 | 464 |     } | 
| Benjamin Kramer | 6514551 | 2012-05-20 17:24:08 +0000 | [diff] [blame] | 465 |     builder.setJITMemoryManager(ForceInterpreter ? 0 : | 
 | 466 |                                 JITMemoryManager::CreateDefaultMemManager()); | 
| Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 467 |   } | 
| Daniel Dunbar | 6d13597 | 2010-11-17 16:06:37 +0000 | [diff] [blame] | 468 |  | 
| Evan Cheng | 712e80e | 2009-05-04 23:05:19 +0000 | [diff] [blame] | 469 |   CodeGenOpt::Level OLvl = CodeGenOpt::Default; | 
 | 470 |   switch (OptLevel) { | 
 | 471 |   default: | 
| Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 472 |     errs() << argv[0] << ": invalid optimization level.\n"; | 
| Evan Cheng | 712e80e | 2009-05-04 23:05:19 +0000 | [diff] [blame] | 473 |     return 1; | 
 | 474 |   case ' ': break; | 
 | 475 |   case '0': OLvl = CodeGenOpt::None; break; | 
| Evan Cheng | bf57b52 | 2009-10-16 21:02:20 +0000 | [diff] [blame] | 476 |   case '1': OLvl = CodeGenOpt::Less; break; | 
| Evan Cheng | 712e80e | 2009-05-04 23:05:19 +0000 | [diff] [blame] | 477 |   case '2': OLvl = CodeGenOpt::Default; break; | 
 | 478 |   case '3': OLvl = CodeGenOpt::Aggressive; break; | 
 | 479 |   } | 
| Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 480 |   builder.setOptLevel(OLvl); | 
 | 481 |  | 
| Tim Northover | 77b4c69 | 2012-10-12 09:55:13 +0000 | [diff] [blame] | 482 |   TargetOptions Options; | 
 | 483 |   Options.UseSoftFloat = GenerateSoftFloatCalls; | 
 | 484 |   if (FloatABIForCalls != FloatABI::Default) | 
 | 485 |     Options.FloatABIType = FloatABIForCalls; | 
 | 486 |   if (GenerateSoftFloatCalls) | 
 | 487 |     FloatABIForCalls = FloatABI::Soft; | 
 | 488 |  | 
| Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 489 |   // Remote target execution doesn't handle EH or debug registration. | 
 | 490 |   if (!RemoteMCJIT) { | 
| Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 491 |     Options.JITEmitDebugInfo = EmitJitDebugInfo; | 
 | 492 |     Options.JITEmitDebugInfoToDisk = EmitJitDebugInfoToDisk; | 
| Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 493 |   } | 
| Nick Lewycky | 9a14841 | 2012-04-18 08:34:12 +0000 | [diff] [blame] | 494 |  | 
| Tim Northover | 77b4c69 | 2012-10-12 09:55:13 +0000 | [diff] [blame] | 495 |   builder.setTargetOptions(Options); | 
 | 496 |  | 
| Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 497 |   EE = builder.create(); | 
| Chris Lattner | fd15bee | 2009-07-07 18:31:09 +0000 | [diff] [blame] | 498 |   if (!EE) { | 
 | 499 |     if (!ErrorMsg.empty()) | 
| Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 500 |       errs() << argv[0] << ": error creating EE: " << ErrorMsg << "\n"; | 
| Chris Lattner | fd15bee | 2009-07-07 18:31:09 +0000 | [diff] [blame] | 501 |     else | 
| Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 502 |       errs() << argv[0] << ": unknown error creating EE!\n"; | 
| Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 503 |     exit(1); | 
 | 504 |   } | 
 | 505 |  | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 506 |   if (EnableCacheManager) { | 
 | 507 |     CacheManager = new LLIObjectCache(ObjectCacheDir); | 
 | 508 |     EE->setObjectCache(CacheManager); | 
 | 509 |   } | 
 | 510 |  | 
| Andrew Kaylor | b868e91 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 511 |   // Load any additional modules specified on the command line. | 
 | 512 |   for (unsigned i = 0, e = ExtraModules.size(); i != e; ++i) { | 
 | 513 |     Module *XMod = ParseIRFile(ExtraModules[i], Err, Context); | 
 | 514 |     if (!XMod) { | 
 | 515 |       Err.print(argv[0], errs()); | 
 | 516 |       return 1; | 
 | 517 |     } | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 518 |     if (EnableCacheManager) { | 
 | 519 |       if (UseMCJIT) { | 
 | 520 |         std::string CacheName("file:"); | 
 | 521 |         CacheName.append(ExtraModules[i]); | 
 | 522 |         XMod->setModuleIdentifier(CacheName); | 
 | 523 |       } | 
 | 524 |       // else, we already printed a warning above. | 
 | 525 |     } | 
| Andrew Kaylor | b868e91 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 526 |     EE->addModule(XMod); | 
 | 527 |   } | 
 | 528 |  | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 529 |   for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) { | 
 | 530 |     ErrorOr<object::ObjectFile *> Obj = | 
 | 531 |         object::ObjectFile::createObjectFile(ExtraObjects[i]); | 
 | 532 |     if (!Obj) { | 
 | 533 |       Err.print(argv[0], errs()); | 
 | 534 |       return 1; | 
 | 535 |     } | 
 | 536 |     EE->addObjectFile(Obj.get()); | 
 | 537 |   } | 
 | 538 |  | 
 | 539 |   for (unsigned i = 0, e = ExtraArchives.size(); i != e; ++i) { | 
 | 540 |     std::unique_ptr<MemoryBuffer> ArBuf; | 
 | 541 |     error_code ec; | 
 | 542 |     ec = MemoryBuffer::getFileOrSTDIN(ExtraArchives[i], ArBuf); | 
 | 543 |     if (ec) { | 
 | 544 |       Err.print(argv[0], errs()); | 
 | 545 |       return 1; | 
 | 546 |     } | 
 | 547 |     object::Archive *Ar = new object::Archive(ArBuf.release(), ec); | 
 | 548 |     if (ec || !Ar) { | 
 | 549 |       Err.print(argv[0], errs()); | 
 | 550 |       return 1; | 
 | 551 |     } | 
 | 552 |     EE->addArchive(Ar); | 
 | 553 |   } | 
 | 554 |  | 
| Andrew Kaylor | 47b7fd5 | 2013-10-29 01:29:56 +0000 | [diff] [blame] | 555 |   // If the target is Cygwin/MingW and we are generating remote code, we | 
 | 556 |   // need an extra module to help out with linking. | 
 | 557 |   if (RemoteMCJIT && Triple(Mod->getTargetTriple()).isOSCygMing()) { | 
 | 558 |     addCygMingExtraModule(EE, Context, Mod->getTargetTriple()); | 
 | 559 |   } | 
 | 560 |  | 
| Eli Bendersky | 61b1851 | 2012-03-13 08:33:15 +0000 | [diff] [blame] | 561 |   // The following functions have no effect if their respective profiling | 
 | 562 |   // support wasn't enabled in the build configuration. | 
 | 563 |   EE->RegisterJITEventListener( | 
 | 564 |                 JITEventListener::createOProfileJITEventListener()); | 
 | 565 |   EE->RegisterJITEventListener( | 
 | 566 |                 JITEventListener::createIntelJITEventListener()); | 
| Jeffrey Yasskin | df5a7da | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 567 |  | 
| Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 568 |   if (!NoLazyCompilation && RemoteMCJIT) { | 
 | 569 |     errs() << "warning: remote mcjit does not support lazy compilation\n"; | 
 | 570 |     NoLazyCompilation = true; | 
 | 571 |   } | 
| Jeffrey Yasskin | 18fec73 | 2009-10-27 22:39:42 +0000 | [diff] [blame] | 572 |   EE->DisableLazyCompilation(NoLazyCompilation); | 
| Evan Cheng | c290a5b | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 573 |  | 
| Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 574 |   // If the user specifically requested an argv[0] to pass into the program, | 
 | 575 |   // do it now. | 
 | 576 |   if (!FakeArgv0.empty()) { | 
 | 577 |     InputFile = FakeArgv0; | 
 | 578 |   } else { | 
 | 579 |     // Otherwise, if there is a .bc suffix on the executable strip it off, it | 
 | 580 |     // might confuse the program. | 
| Benjamin Kramer | 3bb37e9 | 2010-04-15 11:33:14 +0000 | [diff] [blame] | 581 |     if (StringRef(InputFile).endswith(".bc")) | 
| Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 582 |       InputFile.erase(InputFile.length() - 3); | 
 | 583 |   } | 
 | 584 |  | 
 | 585 |   // Add the module's name to the start of the vector of arguments to main(). | 
 | 586 |   InputArgv.insert(InputArgv.begin(), InputFile); | 
 | 587 |  | 
 | 588 |   // Call the main function from M as if its signature were: | 
 | 589 |   //   int main (int argc, char **argv, const char **envp) | 
 | 590 |   // using the contents of Args to determine argc & argv, and the contents of | 
 | 591 |   // EnvVars to determine envp. | 
 | 592 |   // | 
| Evan Cheng | ec740e3 | 2008-11-05 23:21:52 +0000 | [diff] [blame] | 593 |   Function *EntryFn = Mod->getFunction(EntryFunc); | 
 | 594 |   if (!EntryFn) { | 
| Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 595 |     errs() << '\'' << EntryFunc << "\' function not found in module.\n"; | 
| Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 596 |     return -1; | 
 | 597 |   } | 
 | 598 |  | 
| Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 599 |   // Reset errno to zero on entry to main. | 
 | 600 |   errno = 0; | 
| Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 601 |  | 
| Andrew Kaylor | f188156 | 2013-10-02 18:04:40 +0000 | [diff] [blame] | 602 |   int Result; | 
 | 603 |  | 
| Andrew Kaylor | 257a009 | 2012-11-27 19:49:00 +0000 | [diff] [blame] | 604 |   if (!RemoteMCJIT) { | 
| Andrew Kaylor | 4bad07f | 2013-10-11 22:47:10 +0000 | [diff] [blame] | 605 |     // If the program doesn't explicitly call exit, we will need the Exit | 
 | 606 |     // function later on to make an explicit call, so get the function now. | 
 | 607 |     Constant *Exit = Mod->getOrInsertFunction("exit", Type::getVoidTy(Context), | 
 | 608 |                                                       Type::getInt32Ty(Context), | 
 | 609 |                                                       NULL); | 
 | 610 |  | 
| Andrew Kaylor | f188156 | 2013-10-02 18:04:40 +0000 | [diff] [blame] | 611 |     // Run static constructors. | 
| Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 612 |     if (UseMCJIT && !ForceInterpreter) { | 
 | 613 |       // Give MCJIT a chance to apply relocations and set page permissions. | 
 | 614 |       EE->finalizeObject(); | 
 | 615 |     } | 
 | 616 |     EE->runStaticConstructorsDestructors(false); | 
| Evan Cheng | c290a5b | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 617 |  | 
| Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 618 |     if (!UseMCJIT && NoLazyCompilation) { | 
 | 619 |       for (Module::iterator I = Mod->begin(), E = Mod->end(); I != E; ++I) { | 
 | 620 |         Function *Fn = &*I; | 
 | 621 |         if (Fn != EntryFn && !Fn->isDeclaration()) | 
 | 622 |           EE->getPointerToFunction(Fn); | 
 | 623 |       } | 
| Evan Cheng | c290a5b | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 624 |     } | 
| Evan Cheng | c290a5b | 2008-04-22 06:51:41 +0000 | [diff] [blame] | 625 |  | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 626 |     // Trigger compilation separately so code regions that need to be | 
| Andrew Kaylor | f188156 | 2013-10-02 18:04:40 +0000 | [diff] [blame] | 627 |     // invalidated will be known. | 
 | 628 |     (void)EE->getPointerToFunction(EntryFn); | 
 | 629 |     // Clear instruction cache before code will be executed. | 
 | 630 |     if (RTDyldMM) | 
 | 631 |       static_cast<SectionMemoryManager*>(RTDyldMM)->invalidateInstructionCache(); | 
 | 632 |  | 
 | 633 |     // Run main. | 
 | 634 |     Result = EE->runFunctionAsMain(EntryFn, InputArgv, envp); | 
 | 635 |  | 
 | 636 |     // Run static destructors. | 
 | 637 |     EE->runStaticConstructorsDestructors(true); | 
 | 638 |  | 
 | 639 |     // If the program didn't call exit explicitly, we should call it now. | 
 | 640 |     // This ensures that any atexit handlers get called correctly. | 
 | 641 |     if (Function *ExitF = dyn_cast<Function>(Exit)) { | 
 | 642 |       std::vector<GenericValue> Args; | 
 | 643 |       GenericValue ResultGV; | 
 | 644 |       ResultGV.IntVal = APInt(32, Result); | 
 | 645 |       Args.push_back(ResultGV); | 
 | 646 |       EE->runFunction(ExitF, Args); | 
 | 647 |       errs() << "ERROR: exit(" << Result << ") returned!\n"; | 
 | 648 |       abort(); | 
 | 649 |     } else { | 
 | 650 |       errs() << "ERROR: exit defined with wrong prototype!\n"; | 
 | 651 |       abort(); | 
 | 652 |     } | 
 | 653 |   } else { | 
 | 654 |     // else == "if (RemoteMCJIT)" | 
 | 655 |  | 
 | 656 |     // Remote target MCJIT doesn't (yet) support static constructors. No reason | 
 | 657 |     // it couldn't. This is a limitation of the LLI implemantation, not the | 
 | 658 |     // MCJIT itself. FIXME. | 
 | 659 |     // | 
| Andrew Kaylor | b868e91 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 660 |     RemoteMemoryManager *MM = static_cast<RemoteMemoryManager*>(RTDyldMM); | 
| Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 661 |     // Everything is prepared now, so lay out our program for the target | 
 | 662 |     // address space, assign the section addresses to resolve any relocations, | 
 | 663 |     // and send it to the target. | 
| Andrew Kaylor | 0ab5c6c | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 664 |  | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 665 |     std::unique_ptr<RemoteTarget> Target; | 
 | 666 |     if (!ChildExecPath.empty()) { // Remote execution on a child process | 
 | 667 | #ifndef LLVM_ON_UNIX | 
 | 668 |       // FIXME: Remove this pointless fallback mode which causes tests to "pass" | 
 | 669 |       // on platforms where they should XFAIL. | 
 | 670 |       errs() << "Warning: host does not support external remote targets.\n" | 
 | 671 |              << "  Defaulting to simulated remote execution\n"; | 
 | 672 |       Target.reset(new RemoteTarget); | 
 | 673 | #else | 
 | 674 |       if (!sys::fs::can_execute(ChildExecPath)) { | 
 | 675 |         errs() << "Unable to find usable child executable: '" << ChildExecPath | 
 | 676 |                << "'\n"; | 
 | 677 |         return -1; | 
| Andrew Kaylor | 0ab5c6c | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 678 |       } | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 679 |       Target.reset(new RemoteTargetExternal(ChildExecPath)); | 
 | 680 | #endif | 
| Andrew Kaylor | 0ab5c6c | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 681 |     } else { | 
 | 682 |       // No child process name provided, use simulated remote execution. | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 683 |       Target.reset(new RemoteTarget); | 
| Andrew Kaylor | 0ab5c6c | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 684 |     } | 
 | 685 |  | 
| Andrew Kaylor | b868e91 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 686 |     // Give the memory manager a pointer to our remote target interface object. | 
 | 687 |     MM->setRemoteTarget(Target.get()); | 
 | 688 |  | 
 | 689 |     // Create the remote target. | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 690 |     if (!Target->create()) { | 
 | 691 |       errs() << "ERROR: " << Target->getErrorMsg() << "\n"; | 
 | 692 |       return EXIT_FAILURE; | 
 | 693 |     } | 
| Jim Grosbach | 5da959d | 2012-08-28 23:22:30 +0000 | [diff] [blame] | 694 |  | 
| Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 695 |     // Since we're executing in a (at least simulated) remote address space, | 
 | 696 |     // we can't use the ExecutionEngine::runFunctionAsMain(). We have to | 
 | 697 |     // grab the function address directly here and tell the remote target | 
 | 698 |     // to execute the function. | 
| Andrew Kaylor | b868e91 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 699 |     // | 
 | 700 |     // Our memory manager will map generated code into the remote address | 
 | 701 |     // space as it is loaded and copy the bits over during the finalizeMemory | 
 | 702 |     // operation. | 
 | 703 |     // | 
| Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 704 |     // FIXME: argv and envp handling. | 
| Andrew Kaylor | 0ab5c6c | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 705 |     uint64_t Entry = EE->getFunctionAddress(EntryFn->getName().str()); | 
| Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 706 |  | 
| Tim Northover | a73a614 | 2013-05-19 09:55:06 +0000 | [diff] [blame] | 707 |     DEBUG(dbgs() << "Executing '" << EntryFn->getName() << "' at 0x" | 
 | 708 |                  << format("%llx", Entry) << "\n"); | 
| Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 709 |  | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 710 |     if (!Target->executeCode(Entry, Result)) | 
| Andrew Kaylor | 0ab5c6c | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 711 |       errs() << "ERROR: " << Target->getErrorMsg() << "\n"; | 
| Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 712 |  | 
| Andrew Kaylor | f188156 | 2013-10-02 18:04:40 +0000 | [diff] [blame] | 713 |     // Like static constructors, the remote target MCJIT support doesn't handle | 
 | 714 |     // this yet. It could. FIXME. | 
 | 715 |  | 
 | 716 |     // Stop the remote target | 
| Andrew Kaylor | 0ab5c6c | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 717 |     Target->stop(); | 
| Chris Lattner | c1e6d68 | 2007-05-06 04:58:26 +0000 | [diff] [blame] | 718 |   } | 
| Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 719 |  | 
| Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 720 |   return Result; | 
| Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 721 | } |