1fe11a97fcde7c63109c3ad36570657807d0cd6efChris Lattner//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
23da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman//
37c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//                     The LLVM Compiler Infrastructure
47c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//
521c62da287237d39d0d95004881ea4baae3be6daChris Lattner// This file is distributed under the University of Illinois Open Source
621c62da287237d39d0d95004881ea4baae3be6daChris Lattner// License. See LICENSE.TXT for details.
73da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman//
87c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//===----------------------------------------------------------------------===//
992101acd7fd44fd467fbeb974ae6c042289c74f9Chris Lattner//
107efea1dd98cd9e20ef160c838190f5249123f494Chris Lattner// This utility provides a simple wrapper around the LLVM Execution Engines,
117efea1dd98cd9e20ef160c838190f5249123f494Chris Lattner// which allow the direct execution of LLVM programs through a Just-In-Time
12db9c028d019f60c7bbb0d8282ffd14f348c0cf52Torok Edwin// compiler, or through an interpreter if no JIT is available for this platform.
1392101acd7fd44fd467fbeb974ae6c042289c74f9Chris Lattner//
1492101acd7fd44fd467fbeb974ae6c042289c74f9Chris Lattner//===----------------------------------------------------------------------===//
1592101acd7fd44fd467fbeb974ae6c042289c74f9Chris Lattner
160b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/LLVMContext.h"
174c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar#include "OrcLazyJIT.h"
18b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor#include "RemoteMemoryManager.h"
19706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach#include "RemoteTarget.h"
2036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "RemoteTargetExternal.h"
2175ebbceeeda1e7a78efe1848bb90e034f0c6ba61Duncan Sands#include "llvm/ADT/Triple.h"
22c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner#include "llvm/Bitcode/ReaderWriter.h"
23d3a680ae2cb1ca2d96d1272754af4702862dcb30Chris Lattner#include "llvm/CodeGen/LinkAllCodegenComponents.h"
24d1cab3e6ce508e593b17fdd660ed8cc4ea486d72Brian Gaeke#include "llvm/ExecutionEngine/GenericValue.h"
25df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin#include "llvm/ExecutionEngine/Interpreter.h"
26df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin#include "llvm/ExecutionEngine/JITEventListener.h"
276aec29848676494867e26307698155bc2c5a4033Daniel Dunbar#include "llvm/ExecutionEngine/MCJIT.h"
2836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/ExecutionEngine/ObjectCache.h"
29ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include "llvm/ExecutionEngine/OrcMCJITReplacement.h"
30257a009332a62927bed91c6862730f43ef89d429Andrew Kaylor#include "llvm/ExecutionEngine/SectionMemoryManager.h"
3147b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor#include "llvm/IR/IRBuilder.h"
320b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Module.h"
330b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Type.h"
3447b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor#include "llvm/IR/TypeBuilder.h"
357fc162f893d67ffd96fdb19e2eb9a03b4621f0c0Chandler Carruth#include "llvm/IRReader/IRReader.h"
3636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/Object/Archive.h"
3736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/Object/ObjectFile.h"
38551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/CommandLine.h"
39f010c464a11444733ec67e31aace8bcebeaf2588Chandler Carruth#include "llvm/Support/Debug.h"
40f010c464a11444733ec67e31aace8bcebeaf2588Chandler Carruth#include "llvm/Support/DynamicLibrary.h"
41f010c464a11444733ec67e31aace8bcebeaf2588Chandler Carruth#include "llvm/Support/Format.h"
42c30598bc3ad792eb8cc75b188eb872a28c62ab71Chris Lattner#include "llvm/Support/ManagedStatic.h"
43f010c464a11444733ec67e31aace8bcebeaf2588Chandler Carruth#include "llvm/Support/MathExtras.h"
44f010c464a11444733ec67e31aace8bcebeaf2588Chandler Carruth#include "llvm/Support/Memory.h"
45c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner#include "llvm/Support/MemoryBuffer.h"
464c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar#include "llvm/Support/Path.h"
47551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/PluginLoader.h"
48cc14d25dd99e891c586bd56aa41796abbe4ac3d8Chris Lattner#include "llvm/Support/PrettyStackTrace.h"
491f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/Process.h"
500ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor#include "llvm/Support/Program.h"
511f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/Signals.h"
527fc162f893d67ffd96fdb19e2eb9a03b4621f0c0Chandler Carruth#include "llvm/Support/SourceMgr.h"
533e74d6fdd248e20a280f1dff3da9a6c689c2c4c3Evan Cheng#include "llvm/Support/TargetSelect.h"
54f010c464a11444733ec67e31aace8bcebeaf2588Chandler Carruth#include "llvm/Support/raw_ostream.h"
55e16e687034fb416f9e6f90d8e169cc8633c34f35Daniel Malea#include "llvm/Transforms/Instrumentation.h"
56020403209a86a3b4d210a4bc62b37ee5cf49dec8Chris Lattner#include <cerrno>
57a13d14a1290bc2c8b2e25a51ec4ae4e9bc8cd215NAKAMURA Takumi
58a13d14a1290bc2c8b2e25a51ec4ae4e9bc8cd215NAKAMURA Takumi#ifdef __CYGWIN__
59a13d14a1290bc2c8b2e25a51ec4ae4e9bc8cd215NAKAMURA Takumi#include <cygwin/version.h>
60a13d14a1290bc2c8b2e25a51ec4ae4e9bc8cd215NAKAMURA Takumi#if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007
61a13d14a1290bc2c8b2e25a51ec4ae4e9bc8cd215NAKAMURA Takumi#define DO_NOTHING_ATEXIT 1
62a13d14a1290bc2c8b2e25a51ec4ae4e9bc8cd215NAKAMURA Takumi#endif
63a13d14a1290bc2c8b2e25a51ec4ae4e9bc8cd215NAKAMURA Takumi#endif
64a13d14a1290bc2c8b2e25a51ec4ae4e9bc8cd215NAKAMURA Takumi
65d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekeusing namespace llvm;
66d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
67dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#define DEBUG_TYPE "lli"
68dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
69fe11a97fcde7c63109c3ad36570657807d0cd6efChris Lattnernamespace {
704c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar
714c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  enum class JITKind { MCJIT, OrcMCJITReplacement, OrcLazy };
724c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar
73fe11a97fcde7c63109c3ad36570657807d0cd6efChris Lattner  cl::opt<std::string>
74a99be51bf5cdac1438069d4b01766c47704961c8Gabor Greif  InputFile(cl::desc("<input bitcode>"), cl::Positional, cl::init("-"));
755ff62e90d0bc321206023897edc1e2691cb0fbb6Chris Lattner
76fe11a97fcde7c63109c3ad36570657807d0cd6efChris Lattner  cl::list<std::string>
77fe11a97fcde7c63109c3ad36570657807d0cd6efChris Lattner  InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
785ff62e90d0bc321206023897edc1e2691cb0fbb6Chris Lattner
79fe11a97fcde7c63109c3ad36570657807d0cd6efChris Lattner  cl::opt<bool> ForceInterpreter("force-interpreter",
803d8a54d00c1f3e9a1a349a9b0645c85a09d7c20bMisha Brukman                                 cl::desc("Force interpretation: disable JIT"),
813d8a54d00c1f3e9a1a349a9b0645c85a09d7c20bMisha Brukman                                 cl::init(false));
82e1a4eda990a765952434d39d7c14299d1d4614a5Evan Cheng
834c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  cl::opt<JITKind> UseJITKind("jit-kind",
844c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar                              cl::desc("Choose underlying JIT kind."),
854c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar                              cl::init(JITKind::MCJIT),
864c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar                              cl::values(
874c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar                                clEnumValN(JITKind::MCJIT, "mcjit",
884c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar                                           "MCJIT"),
894c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar                                clEnumValN(JITKind::OrcMCJITReplacement,
904c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar                                           "orc-mcjit",
914c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar                                           "Orc-based MCJIT replacement"),
924c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar                                clEnumValN(JITKind::OrcLazy,
934c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar                                           "orc-lazy",
944c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar                                           "Orc-based lazy JIT."),
954c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar                                clEnumValEnd));
96e16e687034fb416f9e6f90d8e169cc8633c34f35Daniel Malea
97706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  // The MCJIT supports building for a target address space separate from
98706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  // the JIT compilation process. Use a forked process and a copying
99706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  // memory manager with IPC to execute using this functionality.
100706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  cl::opt<bool> RemoteMCJIT("remote-mcjit",
101706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach    cl::desc("Execute MCJIT'ed code in a separate process."),
102706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach    cl::init(false));
103706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
1040ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor  // Manually specify the child process for remote execution. This overrides
1050ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor  // the simulated remote execution that allocates address space for child
1065f7a5773e679107565f9da79c26bb558d5e9c051Andrew Kaylor  // execution. The child process will be executed and will communicate with
1075f7a5773e679107565f9da79c26bb558d5e9c051Andrew Kaylor  // lli via stdin/stdout pipes.
1080ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor  cl::opt<std::string>
10936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ChildExecPath("mcjit-remote-process",
11036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                cl::desc("Specify the filename of the process to launch "
11136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                         "for remote MCJIT execution.  If none is specified,"
11236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                         "\n\tremote execution will be simulated in-process."),
11336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                cl::value_desc("filename"), cl::init(""));
1140ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor
115712e80e188c306125e32e46e9ed5db9dfc2f7cc9Evan Cheng  // Determine optimization level.
116712e80e188c306125e32e46e9ed5db9dfc2f7cc9Evan Cheng  cl::opt<char>
117712e80e188c306125e32e46e9ed5db9dfc2f7cc9Evan Cheng  OptLevel("O",
118712e80e188c306125e32e46e9ed5db9dfc2f7cc9Evan Cheng           cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
119712e80e188c306125e32e46e9ed5db9dfc2f7cc9Evan Cheng                    "(default = '-O2')"),
120712e80e188c306125e32e46e9ed5db9dfc2f7cc9Evan Cheng           cl::Prefix,
121712e80e188c306125e32e46e9ed5db9dfc2f7cc9Evan Cheng           cl::ZeroOrMore,
122712e80e188c306125e32e46e9ed5db9dfc2f7cc9Evan Cheng           cl::init(' '));
123e1a4eda990a765952434d39d7c14299d1d4614a5Evan Cheng
1243015e6061c67ef30e85df83f320f3dcf9c9f023bChris Lattner  cl::opt<std::string>
12560844d46ceaba7d020d55b521b31c787f149e7a4Chris Lattner  TargetTriple("mtriple", cl::desc("Override target triple for module"));
126ec740e325c2d5b1814bd72337d49fe5a74ba9686Evan Cheng
127ec740e325c2d5b1814bd72337d49fe5a74ba9686Evan Cheng  cl::opt<std::string>
1284688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  MArch("march",
1294688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin        cl::desc("Architecture to generate assembly for (see --version)"));
1304688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin
1314688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  cl::opt<std::string>
1324688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  MCPU("mcpu",
1334688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin       cl::desc("Target a specific cpu type (-mcpu=help for details)"),
1344688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin       cl::value_desc("cpu-name"),
1354688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin       cl::init(""));
1364688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin
1374688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  cl::list<std::string>
1384688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  MAttrs("mattr",
1394688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin         cl::CommaSeparated,
1404688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin         cl::desc("Target specific attributes (-mattr=help for details)"),
1414688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin         cl::value_desc("a1,+a2,-a3,..."));
1424688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin
1434688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  cl::opt<std::string>
144ec740e325c2d5b1814bd72337d49fe5a74ba9686Evan Cheng  EntryFunc("entry-function",
145ec740e325c2d5b1814bd72337d49fe5a74ba9686Evan Cheng            cl::desc("Specify the entry function (default = 'main') "
146ec740e325c2d5b1814bd72337d49fe5a74ba9686Evan Cheng                     "of the executable"),
147ec740e325c2d5b1814bd72337d49fe5a74ba9686Evan Cheng            cl::value_desc("function"),
148ec740e325c2d5b1814bd72337d49fe5a74ba9686Evan Cheng            cl::init("main"));
149a66a18505e07a4e72d6fa7e85663937a257577f3Eli Bendersky
150b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor  cl::list<std::string>
15161abf1550f6b12b066c959512ab10ce0720df207Andrew Kaylor  ExtraModules("extra-module",
152b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor         cl::desc("Extra modules to be loaded"),
153730e3c69952d4f26a0c51b55902ac55c88238ee8Alp Toker         cl::value_desc("input bitcode"));
154b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor
15536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  cl::list<std::string>
15636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ExtraObjects("extra-object",
15736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines         cl::desc("Extra object files to be loaded"),
15836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines         cl::value_desc("input object"));
15936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
16036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  cl::list<std::string>
16136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ExtraArchives("extra-archive",
16236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines         cl::desc("Extra archive files to be loaded"),
16336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines         cl::value_desc("input archive"));
16436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
16536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  cl::opt<bool>
16636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  EnableCacheManager("enable-cache-manager",
16736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        cl::desc("Use cache manager to save/load mdoules"),
16836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        cl::init(false));
16936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
17036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  cl::opt<std::string>
17136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ObjectCacheDir("object-cache-dir",
17236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                  cl::desc("Directory to store cached object files "
17336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                           "(must be user writable)"),
17436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                  cl::init(""));
17536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
176e69671d65b45b6a1bbbdc1b215b8c0f943171c29Chris Lattner  cl::opt<std::string>
177e69671d65b45b6a1bbbdc1b215b8c0f943171c29Chris Lattner  FakeArgv0("fake-argv0",
178e69671d65b45b6a1bbbdc1b215b8c0f943171c29Chris Lattner            cl::desc("Override the 'argv[0]' value passed into the executing"
179e69671d65b45b6a1bbbdc1b215b8c0f943171c29Chris Lattner                     " program"), cl::value_desc("executable"));
180a66a18505e07a4e72d6fa7e85663937a257577f3Eli Bendersky
18143f249adb711b7f05f16c8be1549323ccff21a50Chris Lattner  cl::opt<bool>
18243f249adb711b7f05f16c8be1549323ccff21a50Chris Lattner  DisableCoreFiles("disable-core-files", cl::Hidden,
18343f249adb711b7f05f16c8be1549323ccff21a50Chris Lattner                   cl::desc("Disable emission of core files if possible"));
184c290a5b9ffa6ade5a02e9d8c46fc521a5b71529eEvan Cheng
185c290a5b9ffa6ade5a02e9d8c46fc521a5b71529eEvan Cheng  cl::opt<bool>
18603dace82f416687f3752b99c7caef8d4519637a1Evan Cheng  NoLazyCompilation("disable-lazy-compilation",
187c290a5b9ffa6ade5a02e9d8c46fc521a5b71529eEvan Cheng                  cl::desc("Disable JIT lazy compilation"),
188c290a5b9ffa6ade5a02e9d8c46fc521a5b71529eEvan Cheng                  cl::init(false));
189439661395fd2a2a832dba01c65bc88718528313cEvan Cheng
190439661395fd2a2a832dba01c65bc88718528313cEvan Cheng  cl::opt<Reloc::Model>
191439661395fd2a2a832dba01c65bc88718528313cEvan Cheng  RelocModel("relocation-model",
192439661395fd2a2a832dba01c65bc88718528313cEvan Cheng             cl::desc("Choose relocation model"),
193439661395fd2a2a832dba01c65bc88718528313cEvan Cheng             cl::init(Reloc::Default),
194439661395fd2a2a832dba01c65bc88718528313cEvan Cheng             cl::values(
195439661395fd2a2a832dba01c65bc88718528313cEvan Cheng            clEnumValN(Reloc::Default, "default",
196439661395fd2a2a832dba01c65bc88718528313cEvan Cheng                       "Target default relocation model"),
197439661395fd2a2a832dba01c65bc88718528313cEvan Cheng            clEnumValN(Reloc::Static, "static",
198439661395fd2a2a832dba01c65bc88718528313cEvan Cheng                       "Non-relocatable code"),
199439661395fd2a2a832dba01c65bc88718528313cEvan Cheng            clEnumValN(Reloc::PIC_, "pic",
200439661395fd2a2a832dba01c65bc88718528313cEvan Cheng                       "Fully relocatable, position independent code"),
201439661395fd2a2a832dba01c65bc88718528313cEvan Cheng            clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
202439661395fd2a2a832dba01c65bc88718528313cEvan Cheng                       "Relocatable external references, non-relocatable code"),
203439661395fd2a2a832dba01c65bc88718528313cEvan Cheng            clEnumValEnd));
20434ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng
20534ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng  cl::opt<llvm::CodeModel::Model>
20634ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng  CMModel("code-model",
20734ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng          cl::desc("Choose code model"),
20834ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng          cl::init(CodeModel::JITDefault),
20934ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng          cl::values(clEnumValN(CodeModel::JITDefault, "default",
21034ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng                                "Target default JIT code model"),
21134ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng                     clEnumValN(CodeModel::Small, "small",
21234ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng                                "Small code model"),
21334ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng                     clEnumValN(CodeModel::Kernel, "kernel",
21434ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng                                "Kernel code model"),
21534ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng                     clEnumValN(CodeModel::Medium, "medium",
21634ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng                                "Medium code model"),
21734ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng                     clEnumValN(CodeModel::Large, "large",
21834ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng                                "Large code model"),
21934ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng                     clEnumValEnd));
22034ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng
2219a1484165cd7cc3b95f5f65f845257c44f55a5b3Nick Lewycky  cl::opt<bool>
22277b4c69165090dcbf60e20492e41479489f64a6cTim Northover  GenerateSoftFloatCalls("soft-float",
22377b4c69165090dcbf60e20492e41479489f64a6cTim Northover    cl::desc("Generate software floating point library calls"),
22477b4c69165090dcbf60e20492e41479489f64a6cTim Northover    cl::init(false));
22577b4c69165090dcbf60e20492e41479489f64a6cTim Northover
22677b4c69165090dcbf60e20492e41479489f64a6cTim Northover  cl::opt<llvm::FloatABI::ABIType>
22777b4c69165090dcbf60e20492e41479489f64a6cTim Northover  FloatABIForCalls("float-abi",
22877b4c69165090dcbf60e20492e41479489f64a6cTim Northover                   cl::desc("Choose float ABI type"),
22977b4c69165090dcbf60e20492e41479489f64a6cTim Northover                   cl::init(FloatABI::Default),
23077b4c69165090dcbf60e20492e41479489f64a6cTim Northover                   cl::values(
23177b4c69165090dcbf60e20492e41479489f64a6cTim Northover                     clEnumValN(FloatABI::Default, "default",
23277b4c69165090dcbf60e20492e41479489f64a6cTim Northover                                "Target default float ABI type"),
23377b4c69165090dcbf60e20492e41479489f64a6cTim Northover                     clEnumValN(FloatABI::Soft, "soft",
23477b4c69165090dcbf60e20492e41479489f64a6cTim Northover                                "Soft float ABI (implied by -soft-float)"),
23577b4c69165090dcbf60e20492e41479489f64a6cTim Northover                     clEnumValN(FloatABI::Hard, "hard",
23677b4c69165090dcbf60e20492e41479489f64a6cTim Northover                                "Hard float ABI (uses FP registers)"),
23777b4c69165090dcbf60e20492e41479489f64a6cTim Northover                     clEnumValEnd));
23877b4c69165090dcbf60e20492e41479489f64a6cTim Northover  cl::opt<bool>
2399a1484165cd7cc3b95f5f65f845257c44f55a5b3Nick Lewycky// In debug builds, make this default to true.
2409a1484165cd7cc3b95f5f65f845257c44f55a5b3Nick Lewycky#ifdef NDEBUG
2419a1484165cd7cc3b95f5f65f845257c44f55a5b3Nick Lewycky#define EMIT_DEBUG false
2429a1484165cd7cc3b95f5f65f845257c44f55a5b3Nick Lewycky#else
2439a1484165cd7cc3b95f5f65f845257c44f55a5b3Nick Lewycky#define EMIT_DEBUG true
2449a1484165cd7cc3b95f5f65f845257c44f55a5b3Nick Lewycky#endif
2459a1484165cd7cc3b95f5f65f845257c44f55a5b3Nick Lewycky  EmitJitDebugInfo("jit-emit-debug",
2469a1484165cd7cc3b95f5f65f845257c44f55a5b3Nick Lewycky    cl::desc("Emit debug information to debugger"),
2479a1484165cd7cc3b95f5f65f845257c44f55a5b3Nick Lewycky    cl::init(EMIT_DEBUG));
2489a1484165cd7cc3b95f5f65f845257c44f55a5b3Nick Lewycky#undef EMIT_DEBUG
2499a1484165cd7cc3b95f5f65f845257c44f55a5b3Nick Lewycky
2509a1484165cd7cc3b95f5f65f845257c44f55a5b3Nick Lewycky  static cl::opt<bool>
2519a1484165cd7cc3b95f5f65f845257c44f55a5b3Nick Lewycky  EmitJitDebugInfoToDisk("jit-emit-debug-to-disk",
2529a1484165cd7cc3b95f5f65f845257c44f55a5b3Nick Lewycky    cl::Hidden,
2539a1484165cd7cc3b95f5f65f845257c44f55a5b3Nick Lewycky    cl::desc("Emit debug info objfiles to disk"),
2549a1484165cd7cc3b95f5f65f845257c44f55a5b3Nick Lewycky    cl::init(false));
255fe11a97fcde7c63109c3ad36570657807d0cd6efChris Lattner}
25643e3f7c9627d0793a31a9457d3bbbe4d8d56d331Chris Lattner
25736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines//===----------------------------------------------------------------------===//
25836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// Object cache
25936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines//
26036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// This object cache implementation writes cached objects to disk to the
26136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// directory specified by CacheDir, using a filename provided in the module
26236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// descriptor. The cache tries to load a saved object using that path if the
26336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// file exists. CacheDir defaults to "", in which case objects are cached
26436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// alongside their originating bitcodes.
26536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines//
26636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesclass LLIObjectCache : public ObjectCache {
26736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinespublic:
26836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  LLIObjectCache(const std::string& CacheDir) : CacheDir(CacheDir) {
26936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // Add trailing '/' to cache dir if necessary.
27036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (!this->CacheDir.empty() &&
27136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        this->CacheDir[this->CacheDir.size() - 1] != '/')
27236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      this->CacheDir += '/';
27336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
2742c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar  ~LLIObjectCache() override {}
27536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
27637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) override {
27736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    const std::string ModuleID = M->getModuleIdentifier();
27836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    std::string CacheName;
27936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (!getCacheFilename(ModuleID, CacheName))
28036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      return;
28136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (!CacheDir.empty()) { // Create user-defined cache dir.
28236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      SmallString<128> dir(CacheName);
28336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      sys::path::remove_filename(dir);
28436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      sys::fs::create_directories(Twine(dir));
28536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    }
28637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    std::error_code EC;
28737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    raw_fd_ostream outfile(CacheName, EC, sys::fs::F_None);
28837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    outfile.write(Obj.getBufferStart(), Obj.getBufferSize());
28936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    outfile.close();
29036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
29136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
29237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  std::unique_ptr<MemoryBuffer> getObject(const Module* M) override {
29336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    const std::string ModuleID = M->getModuleIdentifier();
29436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    std::string CacheName;
29536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (!getCacheFilename(ModuleID, CacheName))
296dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      return nullptr;
29736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // Load the object from the cache filename
298c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines    ErrorOr<std::unique_ptr<MemoryBuffer>> IRObjectBuffer =
299c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines        MemoryBuffer::getFile(CacheName.c_str(), -1, false);
30036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // If the file isn't there, that's OK.
30136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (!IRObjectBuffer)
302dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      return nullptr;
30336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // MCJIT will want to write into this buffer, and we don't want that
30436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // because the file has probably just been mmapped.  Instead we make
30536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // a copy.  The filed-based buffer will be released when it goes
30636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // out of scope.
307c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines    return MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer());
30836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
30936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
31036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesprivate:
31136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  std::string CacheDir;
31236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
31336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  bool getCacheFilename(const std::string &ModID, std::string &CacheName) {
31436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    std::string Prefix("file:");
31536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    size_t PrefixLength = Prefix.length();
31636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (ModID.substr(0, PrefixLength) != Prefix)
31736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      return false;
31836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        std::string CacheSubdir = ModID.substr(PrefixLength);
31936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#if defined(_WIN32)
32036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        // Transform "X:\foo" => "/X\foo" for convenience.
32136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        if (isalpha(CacheSubdir[0]) && CacheSubdir[1] == ':') {
32236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines          CacheSubdir[1] = CacheSubdir[0];
32336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines          CacheSubdir[0] = '/';
32436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        }
32536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#endif
32636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    CacheName = CacheDir + CacheSubdir;
32736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    size_t pos = CacheName.rfind('.');
32836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    CacheName.replace(pos, CacheName.length() - pos, ".o");
32936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    return true;
33036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
33136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines};
33236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
333dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesstatic ExecutionEngine *EE = nullptr;
334dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesstatic LLIObjectCache *CacheManager = nullptr;
335f70d67750102509d0192c58ab5ddf4e450b5f114Reid Spencer
336f70d67750102509d0192c58ab5ddf4e450b5f114Reid Spencerstatic void do_shutdown() {
337a13d14a1290bc2c8b2e25a51ec4ae4e9bc8cd215NAKAMURA Takumi  // Cygwin-1.5 invokes DLL's dtors before atexit handler.
338a13d14a1290bc2c8b2e25a51ec4ae4e9bc8cd215NAKAMURA Takumi#ifndef DO_NOTHING_ATEXIT
339f70d67750102509d0192c58ab5ddf4e450b5f114Reid Spencer  delete EE;
34036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if (CacheManager)
34136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    delete CacheManager;
342f70d67750102509d0192c58ab5ddf4e450b5f114Reid Spencer  llvm_shutdown();
343a13d14a1290bc2c8b2e25a51ec4ae4e9bc8cd215NAKAMURA Takumi#endif
344f70d67750102509d0192c58ab5ddf4e450b5f114Reid Spencer}
345f70d67750102509d0192c58ab5ddf4e450b5f114Reid Spencer
34647b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor// On Mingw and Cygwin, an external symbol named '__main' is called from the
34747b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor// generated 'main' function to allow static intialization.  To avoid linking
34847b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor// problems with remote targets (because lli's remote target support does not
34947b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor// currently handle external linking) we add a secondary module which defines
35047b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor// an empty '__main' function.
35147b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylorstatic void addCygMingExtraModule(ExecutionEngine *EE,
35247b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor                                  LLVMContext &Context,
35347b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor                                  StringRef TargetTripleStr) {
35447b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor  IRBuilder<> Builder(Context);
35547b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor  Triple TargetTriple(TargetTripleStr);
35647b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor
35747b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor  // Create a new module.
35837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  std::unique_ptr<Module> M = make_unique<Module>("CygMingHelper", Context);
35947b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor  M->setTargetTriple(TargetTripleStr);
36047b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor
36147b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor  // Create an empty function named "__main".
36247b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor  Function *Result;
36347b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor  if (TargetTriple.isArch64Bit()) {
36447b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor    Result = Function::Create(
36547b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor      TypeBuilder<int64_t(void), false>::get(Context),
36637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      GlobalValue::ExternalLinkage, "__main", M.get());
36747b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor  } else {
36847b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor    Result = Function::Create(
36947b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor      TypeBuilder<int32_t(void), false>::get(Context),
37037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      GlobalValue::ExternalLinkage, "__main", M.get());
37147b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor  }
37247b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor  BasicBlock *BB = BasicBlock::Create(Context, "__main", Result);
37347b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor  Builder.SetInsertPoint(BB);
37447b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor  Value *ReturnVal;
37547b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor  if (TargetTriple.isArch64Bit())
37647b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor    ReturnVal = ConstantInt::get(Context, APInt(64, 0));
37747b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor  else
37847b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor    ReturnVal = ConstantInt::get(Context, APInt(32, 0));
37947b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor  Builder.CreateRet(ReturnVal);
38047b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor
38147b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor  // Add this new module to the ExecutionEngine.
38237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  EE->addModule(std::move(M));
38347b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor}
38447b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor
38547b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor
38692101acd7fd44fd467fbeb974ae6c042289c74f9Chris Lattner//===----------------------------------------------------------------------===//
38792101acd7fd44fd467fbeb974ae6c042289c74f9Chris Lattner// main Driver function
38892101acd7fd44fd467fbeb974ae6c042289c74f9Chris Lattner//
389c4fb6fdd8677530d9b1db34b6254edb0094b78edMisha Brukmanint main(int argc, char **argv, char * const *envp) {
390cc14d25dd99e891c586bd56aa41796abbe4ac3d8Chris Lattner  sys::PrintStackTraceOnErrorSignal();
391cc14d25dd99e891c586bd56aa41796abbe4ac3d8Chris Lattner  PrettyStackTraceProgram X(argc, argv);
392a66a18505e07a4e72d6fa7e85663937a257577f3Eli Bendersky
3930d7c695c74ae6d5f68cc07378c17491915e607d3Owen Anderson  LLVMContext &Context = getGlobalContext();
394f70d67750102509d0192c58ab5ddf4e450b5f114Reid Spencer  atexit(do_shutdown);  // Call llvm_shutdown() on exit.
395494d663175bbaa7db4743105d1efdf78be9cdb03Daniel Dunbar
396494d663175bbaa7db4743105d1efdf78be9cdb03Daniel Dunbar  // If we have a native target, initialize it to ensure it is linked in and
397494d663175bbaa7db4743105d1efdf78be9cdb03Daniel Dunbar  // usable by the JIT.
398494d663175bbaa7db4743105d1efdf78be9cdb03Daniel Dunbar  InitializeNativeTarget();
39931649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach  InitializeNativeTargetAsmPrinter();
4006837232a609fe7b13473606a1c8a18eabfe2df2aJim Grosbach  InitializeNativeTargetAsmParser();
401494d663175bbaa7db4743105d1efdf78be9cdb03Daniel Dunbar
402c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  cl::ParseCommandLineOptions(argc, argv,
40382a13c9c4811e40d0ff858c508cb54d672ee926eDan Gohman                              "llvm interpreter & dynamic compiler\n");
4041ef8bdaedbd98bee35a573b8bc87149f2182cb5eReid Spencer
405c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  // If the user doesn't want core files, disable them.
406c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  if (DisableCoreFiles)
407c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner    sys::Process::PreventCoreFiles();
408a66a18505e07a4e72d6fa7e85663937a257577f3Eli Bendersky
409a99be51bf5cdac1438069d4b01766c47704961c8Gabor Greif  // Load the bitcode...
41046a27169104ac4cded2bd91e7f872efa80e08446Daniel Dunbar  SMDiagnostic Err;
41137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  std::unique_ptr<Module> Owner = parseIRFile(InputFile, Err, Context);
41237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Module *Mod = Owner.get();
413f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  if (!Mod) {
414d8b7aa26134d2abee777f745c32005e63dea2455Chris Lattner    Err.print(argv[0], errs());
41546a27169104ac4cded2bd91e7f872efa80e08446Daniel Dunbar    return 1;
416c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  }
4171ef8bdaedbd98bee35a573b8bc87149f2182cb5eReid Spencer
4184c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  if (UseJITKind == JITKind::OrcLazy)
4194c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    return runOrcLazyJIT(std::move(Owner), argc, argv);
4204c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar
42136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if (EnableCacheManager) {
42237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    std::string CacheName("file:");
42337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    CacheName.append(InputFile);
42437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    Mod->setModuleIdentifier(CacheName);
42536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
42636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
427f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  // If not jitting lazily, load the whole bitcode file eagerly too.
428f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  if (NoLazyCompilation) {
429c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines    if (std::error_code EC = Mod->materializeAllPermanently()) {
430f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin      errs() << argv[0] << ": bitcode didn't read correctly.\n";
43136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      errs() << "Reason: " << EC.message() << "\n";
432f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin      exit(1);
433f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin    }
434c290a5b9ffa6ade5a02e9d8c46fc521a5b71529eEvan Cheng  }
4351ef8bdaedbd98bee35a573b8bc87149f2182cb5eReid Spencer
43636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  std::string ErrorMsg;
43737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  EngineBuilder builder(std::move(Owner));
4384688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  builder.setMArch(MArch);
4394688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  builder.setMCPU(MCPU);
4404688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  builder.setMAttrs(MAttrs);
441439661395fd2a2a832dba01c65bc88718528313cEvan Cheng  builder.setRelocationModel(RelocModel);
44234ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng  builder.setCodeModel(CMModel);
4434dc31369eb711344055a69f6d9663b584b2a032dDaniel Dunbar  builder.setErrorStr(&ErrorMsg);
4444dc31369eb711344055a69f6d9663b584b2a032dDaniel Dunbar  builder.setEngineKind(ForceInterpreter
4454b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner                        ? EngineKind::Interpreter
4464b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner                        : EngineKind::JIT);
4474c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  builder.setUseOrcMCJITReplacement(UseJITKind == JITKind::OrcMCJITReplacement);
4484b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
449c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  // If we are supposed to override the target triple, do so now.
450c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  if (!TargetTriple.empty())
45175ebbceeeda1e7a78efe1848bb90e034f0c6ba61Duncan Sands    Mod->setTargetTriple(Triple::normalize(TargetTriple));
452c290a5b9ffa6ade5a02e9d8c46fc521a5b71529eEvan Cheng
453a66a18505e07a4e72d6fa7e85663937a257577f3Eli Bendersky  // Enable MCJIT if desired.
454dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  RTDyldMemoryManager *RTDyldMM = nullptr;
45537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (!ForceInterpreter) {
456706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach    if (RemoteMCJIT)
457b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor      RTDyldMM = new RemoteMemoryManager();
458706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach    else
45913a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo      RTDyldMM = new SectionMemoryManager();
460ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
461ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    // Deliberately construct a temp std::unique_ptr to pass in. Do not null out
462ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    // RTDyldMM: We still use it below, even though we don't own it.
463ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    builder.setMCJITMemoryManager(
464ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      std::unique_ptr<RTDyldMemoryManager>(RTDyldMM));
46537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  } else if (RemoteMCJIT) {
46637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    errs() << "error: Remote process execution does not work with the "
46737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines              "interpreter.\n";
46837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    exit(1);
469a66a18505e07a4e72d6fa7e85663937a257577f3Eli Bendersky  }
4706d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar
471712e80e188c306125e32e46e9ed5db9dfc2f7cc9Evan Cheng  CodeGenOpt::Level OLvl = CodeGenOpt::Default;
472712e80e188c306125e32e46e9ed5db9dfc2f7cc9Evan Cheng  switch (OptLevel) {
473712e80e188c306125e32e46e9ed5db9dfc2f7cc9Evan Cheng  default:
47465f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman    errs() << argv[0] << ": invalid optimization level.\n";
475712e80e188c306125e32e46e9ed5db9dfc2f7cc9Evan Cheng    return 1;
476712e80e188c306125e32e46e9ed5db9dfc2f7cc9Evan Cheng  case ' ': break;
477712e80e188c306125e32e46e9ed5db9dfc2f7cc9Evan Cheng  case '0': OLvl = CodeGenOpt::None; break;
478bf57b52ba424bbaaa6e2d25dd4df731deb59f959Evan Cheng  case '1': OLvl = CodeGenOpt::Less; break;
479712e80e188c306125e32e46e9ed5db9dfc2f7cc9Evan Cheng  case '2': OLvl = CodeGenOpt::Default; break;
480712e80e188c306125e32e46e9ed5db9dfc2f7cc9Evan Cheng  case '3': OLvl = CodeGenOpt::Aggressive; break;
481712e80e188c306125e32e46e9ed5db9dfc2f7cc9Evan Cheng  }
4824b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  builder.setOptLevel(OLvl);
4834b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
48477b4c69165090dcbf60e20492e41479489f64a6cTim Northover  TargetOptions Options;
48577b4c69165090dcbf60e20492e41479489f64a6cTim Northover  Options.UseSoftFloat = GenerateSoftFloatCalls;
48677b4c69165090dcbf60e20492e41479489f64a6cTim Northover  if (FloatABIForCalls != FloatABI::Default)
48777b4c69165090dcbf60e20492e41479489f64a6cTim Northover    Options.FloatABIType = FloatABIForCalls;
48877b4c69165090dcbf60e20492e41479489f64a6cTim Northover  if (GenerateSoftFloatCalls)
48977b4c69165090dcbf60e20492e41479489f64a6cTim Northover    FloatABIForCalls = FloatABI::Soft;
49077b4c69165090dcbf60e20492e41479489f64a6cTim Northover
491706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  // Remote target execution doesn't handle EH or debug registration.
492706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  if (!RemoteMCJIT) {
493706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach    Options.JITEmitDebugInfo = EmitJitDebugInfo;
494706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach    Options.JITEmitDebugInfoToDisk = EmitJitDebugInfoToDisk;
495706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  }
4969a1484165cd7cc3b95f5f65f845257c44f55a5b3Nick Lewycky
49777b4c69165090dcbf60e20492e41479489f64a6cTim Northover  builder.setTargetOptions(Options);
49877b4c69165090dcbf60e20492e41479489f64a6cTim Northover
4994b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  EE = builder.create();
500fd15beefeedcb8108913e75e7c736dfcc17b433aChris Lattner  if (!EE) {
501fd15beefeedcb8108913e75e7c736dfcc17b433aChris Lattner    if (!ErrorMsg.empty())
50265f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman      errs() << argv[0] << ": error creating EE: " << ErrorMsg << "\n";
503fd15beefeedcb8108913e75e7c736dfcc17b433aChris Lattner    else
50465f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman      errs() << argv[0] << ": unknown error creating EE!\n";
505c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner    exit(1);
506c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  }
5071ef8bdaedbd98bee35a573b8bc87149f2182cb5eReid Spencer
50836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if (EnableCacheManager) {
50936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    CacheManager = new LLIObjectCache(ObjectCacheDir);
51036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    EE->setObjectCache(CacheManager);
51136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
51236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
513b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor  // Load any additional modules specified on the command line.
514b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor  for (unsigned i = 0, e = ExtraModules.size(); i != e; ++i) {
51537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    std::unique_ptr<Module> XMod = parseIRFile(ExtraModules[i], Err, Context);
516b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor    if (!XMod) {
517b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor      Err.print(argv[0], errs());
518b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor      return 1;
519b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor    }
52036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (EnableCacheManager) {
52137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      std::string CacheName("file:");
52237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      CacheName.append(ExtraModules[i]);
52337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      XMod->setModuleIdentifier(CacheName);
52436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    }
52537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    EE->addModule(std::move(XMod));
526b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor  }
527b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor
52836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) {
52937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    ErrorOr<object::OwningBinary<object::ObjectFile>> Obj =
53036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        object::ObjectFile::createObjectFile(ExtraObjects[i]);
53136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (!Obj) {
53236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      Err.print(argv[0], errs());
53336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      return 1;
53436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    }
53537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    object::OwningBinary<object::ObjectFile> &O = Obj.get();
53637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    EE->addObjectFile(std::move(O));
53736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
53836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
53936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  for (unsigned i = 0, e = ExtraArchives.size(); i != e; ++i) {
54037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    ErrorOr<std::unique_ptr<MemoryBuffer>> ArBufOrErr =
541c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines        MemoryBuffer::getFileOrSTDIN(ExtraArchives[i]);
54237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (!ArBufOrErr) {
54336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      Err.print(argv[0], errs());
54436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      return 1;
54536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    }
54637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    std::unique_ptr<MemoryBuffer> &ArBuf = ArBufOrErr.get();
54737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
54837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    ErrorOr<std::unique_ptr<object::Archive>> ArOrErr =
54937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        object::Archive::create(ArBuf->getMemBufferRef());
55037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (std::error_code EC = ArOrErr.getError()) {
55137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      errs() << EC.message();
55236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      return 1;
55336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    }
55437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    std::unique_ptr<object::Archive> &Ar = ArOrErr.get();
55537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
55637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    object::OwningBinary<object::Archive> OB(std::move(Ar), std::move(ArBuf));
55737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
55837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    EE->addArchive(std::move(OB));
55936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
56036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
56147b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor  // If the target is Cygwin/MingW and we are generating remote code, we
56247b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor  // need an extra module to help out with linking.
56347b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor  if (RemoteMCJIT && Triple(Mod->getTargetTriple()).isOSCygMing()) {
56447b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor    addCygMingExtraModule(EE, Context, Mod->getTargetTriple());
56547b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor  }
56647b7fd54481d25aff90bfe772d54435bbea0908fAndrew Kaylor
56761b1851a205cb8dd29c1d3d4231efb8f8f7da283Eli Bendersky  // The following functions have no effect if their respective profiling
56861b1851a205cb8dd29c1d3d4231efb8f8f7da283Eli Bendersky  // support wasn't enabled in the build configuration.
56961b1851a205cb8dd29c1d3d4231efb8f8f7da283Eli Bendersky  EE->RegisterJITEventListener(
57061b1851a205cb8dd29c1d3d4231efb8f8f7da283Eli Bendersky                JITEventListener::createOProfileJITEventListener());
57161b1851a205cb8dd29c1d3d4231efb8f8f7da283Eli Bendersky  EE->RegisterJITEventListener(
57261b1851a205cb8dd29c1d3d4231efb8f8f7da283Eli Bendersky                JITEventListener::createIntelJITEventListener());
573df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin
574706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  if (!NoLazyCompilation && RemoteMCJIT) {
575706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach    errs() << "warning: remote mcjit does not support lazy compilation\n";
576706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach    NoLazyCompilation = true;
577706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  }
57818fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin  EE->DisableLazyCompilation(NoLazyCompilation);
579c290a5b9ffa6ade5a02e9d8c46fc521a5b71529eEvan Cheng
580c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  // If the user specifically requested an argv[0] to pass into the program,
581c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  // do it now.
582c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  if (!FakeArgv0.empty()) {
583ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    InputFile = static_cast<std::string>(FakeArgv0);
584c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  } else {
585c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner    // Otherwise, if there is a .bc suffix on the executable strip it off, it
586c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner    // might confuse the program.
5873bb37e968bcb202abba0c359c178817fc97ab6e8Benjamin Kramer    if (StringRef(InputFile).endswith(".bc"))
588c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner      InputFile.erase(InputFile.length() - 3);
589c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  }
590c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner
591c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  // Add the module's name to the start of the vector of arguments to main().
592c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  InputArgv.insert(InputArgv.begin(), InputFile);
593c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner
594c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  // Call the main function from M as if its signature were:
595c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  //   int main (int argc, char **argv, const char **envp)
596c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  // using the contents of Args to determine argc & argv, and the contents of
597c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  // EnvVars to determine envp.
598c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  //
599ec740e325c2d5b1814bd72337d49fe5a74ba9686Evan Cheng  Function *EntryFn = Mod->getFunction(EntryFunc);
600ec740e325c2d5b1814bd72337d49fe5a74ba9686Evan Cheng  if (!EntryFn) {
60165f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman    errs() << '\'' << EntryFunc << "\' function not found in module.\n";
602c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner    return -1;
603c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  }
604c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner
605c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  // Reset errno to zero on entry to main.
606c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner  errno = 0;
607a66a18505e07a4e72d6fa7e85663937a257577f3Eli Bendersky
608f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor  int Result;
609f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor
610257a009332a62927bed91c6862730f43ef89d429Andrew Kaylor  if (!RemoteMCJIT) {
6114bad07fbec9d42769fdc5cea891f8cb7a3b284d5Andrew Kaylor    // If the program doesn't explicitly call exit, we will need the Exit
6124bad07fbec9d42769fdc5cea891f8cb7a3b284d5Andrew Kaylor    // function later on to make an explicit call, so get the function now.
6134bad07fbec9d42769fdc5cea891f8cb7a3b284d5Andrew Kaylor    Constant *Exit = Mod->getOrInsertFunction("exit", Type::getVoidTy(Context),
6144bad07fbec9d42769fdc5cea891f8cb7a3b284d5Andrew Kaylor                                                      Type::getInt32Ty(Context),
615ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                                                      nullptr);
6164bad07fbec9d42769fdc5cea891f8cb7a3b284d5Andrew Kaylor
617f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    // Run static constructors.
61837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (!ForceInterpreter) {
6198e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor      // Give MCJIT a chance to apply relocations and set page permissions.
6208e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor      EE->finalizeObject();
6218e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor    }
6228e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor    EE->runStaticConstructorsDestructors(false);
623c290a5b9ffa6ade5a02e9d8c46fc521a5b71529eEvan Cheng
62436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // Trigger compilation separately so code regions that need to be
625f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    // invalidated will be known.
626f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    (void)EE->getPointerToFunction(EntryFn);
627f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    // Clear instruction cache before code will be executed.
628f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    if (RTDyldMM)
629f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor      static_cast<SectionMemoryManager*>(RTDyldMM)->invalidateInstructionCache();
630f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor
631f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    // Run main.
632f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    Result = EE->runFunctionAsMain(EntryFn, InputArgv, envp);
633f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor
634f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    // Run static destructors.
635f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    EE->runStaticConstructorsDestructors(true);
636f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor
637f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    // If the program didn't call exit explicitly, we should call it now.
638f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    // This ensures that any atexit handlers get called correctly.
639f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    if (Function *ExitF = dyn_cast<Function>(Exit)) {
640f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor      std::vector<GenericValue> Args;
641f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor      GenericValue ResultGV;
642f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor      ResultGV.IntVal = APInt(32, Result);
643f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor      Args.push_back(ResultGV);
644f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor      EE->runFunction(ExitF, Args);
645f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor      errs() << "ERROR: exit(" << Result << ") returned!\n";
646f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor      abort();
647f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    } else {
648f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor      errs() << "ERROR: exit defined with wrong prototype!\n";
649f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor      abort();
650f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    }
651f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor  } else {
652f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    // else == "if (RemoteMCJIT)"
653f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor
654f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    // Remote target MCJIT doesn't (yet) support static constructors. No reason
655f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    // it couldn't. This is a limitation of the LLI implemantation, not the
656f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    // MCJIT itself. FIXME.
657f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    //
658b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor    RemoteMemoryManager *MM = static_cast<RemoteMemoryManager*>(RTDyldMM);
659706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach    // Everything is prepared now, so lay out our program for the target
660706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach    // address space, assign the section addresses to resolve any relocations,
661706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach    // and send it to the target.
6620ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor
66336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    std::unique_ptr<RemoteTarget> Target;
66436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (!ChildExecPath.empty()) { // Remote execution on a child process
66536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#ifndef LLVM_ON_UNIX
66636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      // FIXME: Remove this pointless fallback mode which causes tests to "pass"
66736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      // on platforms where they should XFAIL.
66836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      errs() << "Warning: host does not support external remote targets.\n"
66936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines             << "  Defaulting to simulated remote execution\n";
67036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      Target.reset(new RemoteTarget);
67136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#else
67236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      if (!sys::fs::can_execute(ChildExecPath)) {
67336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        errs() << "Unable to find usable child executable: '" << ChildExecPath
67436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines               << "'\n";
67536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        return -1;
6760ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor      }
67736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      Target.reset(new RemoteTargetExternal(ChildExecPath));
67836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#endif
6790ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor    } else {
6800ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor      // No child process name provided, use simulated remote execution.
68136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      Target.reset(new RemoteTarget);
6820ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor    }
6830ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor
684b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor    // Give the memory manager a pointer to our remote target interface object.
685b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor    MM->setRemoteTarget(Target.get());
686b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor
687b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor    // Create the remote target.
68836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (!Target->create()) {
68936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      errs() << "ERROR: " << Target->getErrorMsg() << "\n";
69036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      return EXIT_FAILURE;
69136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    }
692706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
693706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach    // Since we're executing in a (at least simulated) remote address space,
694706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach    // we can't use the ExecutionEngine::runFunctionAsMain(). We have to
695706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach    // grab the function address directly here and tell the remote target
696706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach    // to execute the function.
697b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor    //
698b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor    // Our memory manager will map generated code into the remote address
699b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor    // space as it is loaded and copy the bits over during the finalizeMemory
700b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor    // operation.
701b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor    //
702706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach    // FIXME: argv and envp handling.
7030ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor    uint64_t Entry = EE->getFunctionAddress(EntryFn->getName().str());
704706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
705a73a6141cac0091a2121fcbf55b78d232680317cTim Northover    DEBUG(dbgs() << "Executing '" << EntryFn->getName() << "' at 0x"
706a73a6141cac0091a2121fcbf55b78d232680317cTim Northover                 << format("%llx", Entry) << "\n");
707706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
70836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (!Target->executeCode(Entry, Result))
7090ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor      errs() << "ERROR: " << Target->getErrorMsg() << "\n";
710706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
711f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    // Like static constructors, the remote target MCJIT support doesn't handle
712f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    // this yet. It could. FIXME.
713c1e6d686986f511774e0f46e21d4cd90f734bed1Chris Lattner
714f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    // Stop the remote target
715f18815607a93f2b9d498aad4c8e13d1265e204efAndrew Kaylor    Target->stop();
716706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  }
717a66a18505e07a4e72d6fa7e85663937a257577f3Eli Bendersky
718706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  return Result;
71992101acd7fd44fd467fbeb974ae6c042289c74f9Chris Lattner}
720