1 //===------ Interpreter.cpp - Incremental Compilation and Execution -------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the component which performs incremental code
10 // compilation and execution.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Interpreter/Interpreter.h"
15 
16 #include "IncrementalExecutor.h"
17 #include "IncrementalParser.h"
18 
19 #include "clang/AST/ASTContext.h"
20 #include "clang/Basic/TargetInfo.h"
21 #include "clang/CodeGen/ModuleBuilder.h"
22 #include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
23 #include "clang/Driver/Compilation.h"
24 #include "clang/Driver/Driver.h"
25 #include "clang/Driver/Job.h"
26 #include "clang/Driver/Options.h"
27 #include "clang/Driver/Tool.h"
28 #include "clang/Frontend/CompilerInstance.h"
29 #include "clang/Frontend/TextDiagnosticBuffer.h"
30 #include "clang/Lex/PreprocessorOptions.h"
31 
32 #include "llvm/IR/Module.h"
33 #include "llvm/Support/Errc.h"
34 #include "llvm/Support/Host.h"
35 
36 using namespace clang;
37 
38 // FIXME: Figure out how to unify with namespace init_convenience from
39 //        tools/clang-import-test/clang-import-test.cpp
40 namespace {
41 /// Retrieves the clang CC1 specific flags out of the compilation's jobs.
42 /// \returns NULL on error.
43 static llvm::Expected<const llvm::opt::ArgStringList *>
44 GetCC1Arguments(DiagnosticsEngine *Diagnostics,
45                 driver::Compilation *Compilation) {
46   // We expect to get back exactly one Command job, if we didn't something
47   // failed. Extract that job from the Compilation.
48   const driver::JobList &Jobs = Compilation->getJobs();
49   if (!Jobs.size() || !isa<driver::Command>(*Jobs.begin()))
50     return llvm::createStringError(llvm::errc::not_supported,
51                                    "Driver initialization failed. "
52                                    "Unable to create a driver job");
53 
54   // The one job we find should be to invoke clang again.
55   const driver::Command *Cmd = cast<driver::Command>(&(*Jobs.begin()));
56   if (llvm::StringRef(Cmd->getCreator().getName()) != "clang")
57     return llvm::createStringError(llvm::errc::not_supported,
58                                    "Driver initialization failed");
59 
60   return &Cmd->getArguments();
61 }
62 
63 static llvm::Expected<std::unique_ptr<CompilerInstance>>
64 CreateCI(const llvm::opt::ArgStringList &Argv) {
65   std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
66   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
67 
68   // Register the support for object-file-wrapped Clang modules.
69   // FIXME: Clang should register these container operations automatically.
70   auto PCHOps = Clang->getPCHContainerOperations();
71   PCHOps->registerWriter(std::make_unique<ObjectFilePCHContainerWriter>());
72   PCHOps->registerReader(std::make_unique<ObjectFilePCHContainerReader>());
73 
74   // Buffer diagnostics from argument parsing so that we can output them using
75   // a well formed diagnostic object.
76   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
77   TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
78   DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
79   bool Success = CompilerInvocation::CreateFromArgs(
80       Clang->getInvocation(), llvm::makeArrayRef(Argv.begin(), Argv.size()),
81       Diags);
82 
83   // Infer the builtin include path if unspecified.
84   if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
85       Clang->getHeaderSearchOpts().ResourceDir.empty())
86     Clang->getHeaderSearchOpts().ResourceDir =
87         CompilerInvocation::GetResourcesPath(Argv[0], nullptr);
88 
89   // Create the actual diagnostics engine.
90   Clang->createDiagnostics();
91   if (!Clang->hasDiagnostics())
92     return llvm::createStringError(llvm::errc::not_supported,
93                                    "Initialization failed. "
94                                    "Unable to create diagnostics engine");
95 
96   DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
97   if (!Success)
98     return llvm::createStringError(llvm::errc::not_supported,
99                                    "Initialization failed. "
100                                    "Unable to flush diagnostics");
101 
102   // FIXME: Merge with CompilerInstance::ExecuteAction.
103   llvm::MemoryBuffer *MB = llvm::MemoryBuffer::getMemBuffer("").release();
104   Clang->getPreprocessorOpts().addRemappedFile("<<< inputs >>>", MB);
105 
106   Clang->setTarget(TargetInfo::CreateTargetInfo(
107       Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
108   if (!Clang->hasTarget())
109     return llvm::createStringError(llvm::errc::not_supported,
110                                    "Initialization failed. "
111                                    "Target is missing");
112 
113   Clang->getTarget().adjust(Clang->getDiagnostics(), Clang->getLangOpts());
114 
115   // Don't clear the AST before backend codegen since we do codegen multiple
116   // times, reusing the same AST.
117   Clang->getCodeGenOpts().ClearASTBeforeBackend = false;
118 
119   Clang->getFrontendOpts().DisableFree = false;
120   Clang->getCodeGenOpts().DisableFree = false;
121 
122   return std::move(Clang);
123 }
124 
125 } // anonymous namespace
126 
127 llvm::Expected<std::unique_ptr<CompilerInstance>>
128 IncrementalCompilerBuilder::create(std::vector<const char *> &ClangArgv) {
129 
130   // If we don't know ClangArgv0 or the address of main() at this point, try
131   // to guess it anyway (it's possible on some platforms).
132   std::string MainExecutableName =
133       llvm::sys::fs::getMainExecutable(nullptr, nullptr);
134 
135   ClangArgv.insert(ClangArgv.begin(), MainExecutableName.c_str());
136 
137   // Prepending -c to force the driver to do something if no action was
138   // specified. By prepending we allow users to override the default
139   // action and use other actions in incremental mode.
140   // FIXME: Print proper driver diagnostics if the driver flags are wrong.
141   ClangArgv.insert(ClangArgv.begin() + 1, "-c");
142 
143   if (!llvm::is_contained(ClangArgv, " -x")) {
144     // We do C++ by default; append right after argv[0] if no "-x" given
145     ClangArgv.push_back("-x");
146     ClangArgv.push_back("c++");
147   }
148 
149   // Put a dummy C++ file on to ensure there's at least one compile job for the
150   // driver to construct.
151   ClangArgv.push_back("<<< inputs >>>");
152 
153   // Buffer diagnostics from argument parsing so that we can output them using a
154   // well formed diagnostic object.
155   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
156   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts =
157       CreateAndPopulateDiagOpts(ClangArgv);
158   TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
159   DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
160 
161   driver::Driver Driver(/*MainBinaryName=*/ClangArgv[0],
162                         llvm::sys::getProcessTriple(), Diags);
163   Driver.setCheckInputsExist(false); // the input comes from mem buffers
164   llvm::ArrayRef<const char *> RF = llvm::makeArrayRef(ClangArgv);
165   std::unique_ptr<driver::Compilation> Compilation(Driver.BuildCompilation(RF));
166 
167   if (Compilation->getArgs().hasArg(driver::options::OPT_v))
168     Compilation->getJobs().Print(llvm::errs(), "\n", /*Quote=*/false);
169 
170   auto ErrOrCC1Args = GetCC1Arguments(&Diags, Compilation.get());
171   if (auto Err = ErrOrCC1Args.takeError())
172     return std::move(Err);
173 
174   return CreateCI(**ErrOrCC1Args);
175 }
176 
177 Interpreter::Interpreter(std::unique_ptr<CompilerInstance> CI,
178                          llvm::Error &Err) {
179   llvm::ErrorAsOutParameter EAO(&Err);
180   auto LLVMCtx = std::make_unique<llvm::LLVMContext>();
181   TSCtx = std::make_unique<llvm::orc::ThreadSafeContext>(std::move(LLVMCtx));
182   IncrParser = std::make_unique<IncrementalParser>(std::move(CI),
183                                                    *TSCtx->getContext(), Err);
184 }
185 
186 Interpreter::~Interpreter() {}
187 
188 llvm::Expected<std::unique_ptr<Interpreter>>
189 Interpreter::create(std::unique_ptr<CompilerInstance> CI) {
190   llvm::Error Err = llvm::Error::success();
191   auto Interp =
192       std::unique_ptr<Interpreter>(new Interpreter(std::move(CI), Err));
193   if (Err)
194     return std::move(Err);
195   return std::move(Interp);
196 }
197 
198 const CompilerInstance *Interpreter::getCompilerInstance() const {
199   return IncrParser->getCI();
200 }
201 
202 const llvm::orc::LLJIT *Interpreter::getExecutionEngine() const {
203   if (IncrExecutor)
204     return IncrExecutor->getExecutionEngine();
205   return nullptr;
206 }
207 
208 llvm::Expected<PartialTranslationUnit &>
209 Interpreter::Parse(llvm::StringRef Code) {
210   return IncrParser->Parse(Code);
211 }
212 
213 llvm::Error Interpreter::Execute(PartialTranslationUnit &T) {
214   assert(T.TheModule);
215   if (!IncrExecutor) {
216     const clang::TargetInfo &TI =
217         getCompilerInstance()->getASTContext().getTargetInfo();
218     llvm::Error Err = llvm::Error::success();
219     IncrExecutor = std::make_unique<IncrementalExecutor>(*TSCtx, Err, TI);
220 
221     if (Err)
222       return Err;
223   }
224   // FIXME: Add a callback to retain the llvm::Module once the JIT is done.
225   if (auto Err = IncrExecutor->addModule(T))
226     return Err;
227 
228   if (auto Err = IncrExecutor->runCtors())
229     return Err;
230 
231   return llvm::Error::success();
232 }
233 
234 llvm::Expected<llvm::JITTargetAddress>
235 Interpreter::getSymbolAddress(GlobalDecl GD) const {
236   if (!IncrExecutor)
237     return llvm::make_error<llvm::StringError>("Operation failed. "
238                                                "No execution engine",
239                                                std::error_code());
240   llvm::StringRef MangledName = IncrParser->GetMangledName(GD);
241   return getSymbolAddress(MangledName);
242 }
243 
244 llvm::Expected<llvm::JITTargetAddress>
245 Interpreter::getSymbolAddress(llvm::StringRef IRName) const {
246   if (!IncrExecutor)
247     return llvm::make_error<llvm::StringError>("Operation failed. "
248                                                "No execution engine",
249                                                std::error_code());
250 
251   return IncrExecutor->getSymbolAddress(IRName, IncrementalExecutor::IRName);
252 }
253 
254 llvm::Expected<llvm::JITTargetAddress>
255 Interpreter::getSymbolAddressFromLinkerName(llvm::StringRef Name) const {
256   if (!IncrExecutor)
257     return llvm::make_error<llvm::StringError>("Operation failed. "
258                                                "No execution engine",
259                                                std::error_code());
260 
261   return IncrExecutor->getSymbolAddress(Name, IncrementalExecutor::LinkerName);
262 }
263 
264 llvm::Error Interpreter::Undo(unsigned N) {
265 
266   std::list<PartialTranslationUnit> &PTUs = IncrParser->getPTUs();
267   if (N > PTUs.size())
268     return llvm::make_error<llvm::StringError>("Operation failed. "
269                                                "Too many undos",
270                                                std::error_code());
271   for (unsigned I = 0; I < N; I++) {
272     if (IncrExecutor) {
273       if (llvm::Error Err = IncrExecutor->removeModule(PTUs.back()))
274         return Err;
275     }
276 
277     IncrParser->CleanUpPTU(PTUs.back());
278     PTUs.pop_back();
279   }
280   return llvm::Error::success();
281 }
282