lli/OrcLazyJIT.cpp: Use <cstdio> for printf(3) introduced in r234908.
[oota-llvm.git] / tools / lli / OrcLazyJIT.cpp
1 //===------ OrcLazyJIT.cpp - Basic Orc-based JIT for lazy execution -------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "OrcLazyJIT.h"
11 #include "llvm/ExecutionEngine/Orc/OrcTargetSupport.h"
12 #include "llvm/Support/Debug.h"
13 #include "llvm/Support/DynamicLibrary.h"
14 #include <cstdio>
15 #include <system_error>
16
17 using namespace llvm;
18
19 namespace {
20
21   enum class DumpKind { NoDump, DumpFuncsToStdOut, DumpModsToStdErr,
22                         DumpModsToDisk };
23
24   cl::opt<DumpKind> OrcDumpKind("orc-lazy-debug",
25                                 cl::desc("Debug dumping for the orc-lazy JIT."),
26                                 cl::init(DumpKind::NoDump),
27                                 cl::values(
28                                   clEnumValN(DumpKind::NoDump, "no-dump",
29                                              "Don't dump anything."),
30                                   clEnumValN(DumpKind::DumpFuncsToStdOut,
31                                              "funcs-to-stdout",
32                                              "Dump function names to stdout."),
33                                   clEnumValN(DumpKind::DumpModsToStdErr,
34                                              "mods-to-stderr",
35                                              "Dump modules to stderr."),
36                                   clEnumValN(DumpKind::DumpModsToDisk,
37                                              "mods-to-disk",
38                                              "Dump modules to the current "
39                                              "working directory. (WARNING: "
40                                              "will overwrite existing files)."),
41                                   clEnumValEnd));
42 }
43
44 OrcLazyJIT::CallbackManagerBuilder
45 OrcLazyJIT::createCallbackManagerBuilder(Triple T) {
46   switch (T.getArch()) {
47     default: return nullptr;
48
49     case Triple::x86_64: {
50       typedef orc::JITCompileCallbackManager<IRDumpLayerT,
51                                              orc::OrcX86_64> CCMgrT;
52       return [](IRDumpLayerT &IRDumpLayer, RuntimeDyld::MemoryManager &MemMgr,
53                 LLVMContext &Context) {
54                return llvm::make_unique<CCMgrT>(IRDumpLayer, MemMgr, Context, 0,
55                                                 64);
56              };
57     }
58   }
59 }
60
61 OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() {
62
63   switch (OrcDumpKind) {
64   case DumpKind::NoDump:
65     return [](std::unique_ptr<Module> M) { return std::move(M); };
66
67   case DumpKind::DumpFuncsToStdOut:
68     return [](std::unique_ptr<Module> M) {
69       printf("[ ");
70
71       for (const auto &F : *M) {
72         if (F.isDeclaration())
73           continue;
74
75         if (F.hasName()) {
76           std::string Name(F.getName());
77           printf("%s ", Name.c_str());
78         } else
79           printf("<anon> ");
80       }
81
82       printf("]\n");
83       return std::move(M);
84     };
85
86   case DumpKind::DumpModsToStdErr:
87     return [](std::unique_ptr<Module> M) {
88              dbgs() << "----- Module Start -----\n" << *M
89                     << "----- Module End -----\n";
90
91              return std::move(M);
92            };
93
94   case DumpKind::DumpModsToDisk:
95     return [](std::unique_ptr<Module> M) {
96              std::error_code EC;
97              raw_fd_ostream Out(M->getModuleIdentifier() + ".ll", EC,
98                                 sys::fs::F_Text);
99              if (EC) {
100                errs() << "Couldn't open " << M->getModuleIdentifier()
101                       << " for dumping.\nError:" << EC.message() << "\n";
102                exit(1);
103              }
104              Out << *M;
105              return std::move(M);
106            };
107   }
108   llvm_unreachable("Unknown DumpKind");
109 }
110
111 int llvm::runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]) {
112   // Add the program's symbols into the JIT's search space.
113   if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
114     errs() << "Error loading program symbols.\n";
115     return 1;
116   }
117
118   // Grab a target machine and try to build a factory function for the
119   // target-specific Orc callback manager.
120   auto TM = std::unique_ptr<TargetMachine>(EngineBuilder().selectTarget());
121   auto &Context = getGlobalContext();
122   auto CallbackMgrBuilder =
123     OrcLazyJIT::createCallbackManagerBuilder(Triple(TM->getTargetTriple()));
124
125   // If we couldn't build the factory function then there must not be a callback
126   // manager for this target. Bail out.
127   if (!CallbackMgrBuilder) {
128     errs() << "No callback manager available for target '"
129            << TM->getTargetTriple() << "'.\n";
130     return 1;
131   }
132
133   // Everything looks good. Build the JIT.
134   OrcLazyJIT J(std::move(TM), Context, CallbackMgrBuilder);
135
136   // Add the module, look up main and run it.
137   auto MainHandle = J.addModule(std::move(M));
138   auto MainSym = J.findSymbolIn(MainHandle, "main");
139
140   if (!MainSym) {
141     errs() << "Could not find main function.\n";
142     return 1;
143   }
144
145   typedef int (*MainFnPtr)(int, char*[]);
146   auto Main = OrcLazyJIT::fromTargetAddress<MainFnPtr>(MainSym.getAddress());
147   return Main(ArgC, ArgV);
148 }