1 //===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===//
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 "clang/CodeGen/CodeGenAction.h"
11 #include "clang/Basic/SourceManager.h"
12 #include "clang/Basic/TargetInfo.h"
13 #include "clang/AST/ASTConsumer.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/DeclGroup.h"
16 #include "clang/CodeGen/BackendUtil.h"
17 #include "clang/CodeGen/ModuleBuilder.h"
18 #include "clang/Frontend/CompilerInstance.h"
19 #include "clang/Frontend/FrontendDiagnostic.h"
20 #include "llvm/LLVMContext.h"
21 #include "llvm/Module.h"
22 #include "llvm/Pass.h"
23 #include "llvm/ADT/OwningPtr.h"
24 #include "llvm/Support/IRReader.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/SourceMgr.h"
27 #include "llvm/Support/Timer.h"
28 using namespace clang;
29 using namespace llvm;
30 
31 namespace clang {
32   class BackendConsumer : public ASTConsumer {
33     DiagnosticsEngine &Diags;
34     BackendAction Action;
35     const CodeGenOptions &CodeGenOpts;
36     const TargetOptions &TargetOpts;
37     const LangOptions &LangOpts;
38     raw_ostream *AsmOutStream;
39     ASTContext *Context;
40 
41     Timer LLVMIRGeneration;
42 
43     llvm::OwningPtr<CodeGenerator> Gen;
44 
45     llvm::OwningPtr<llvm::Module> TheModule;
46 
47   public:
48     BackendConsumer(BackendAction action, DiagnosticsEngine &_Diags,
49                     const CodeGenOptions &compopts,
50                     const TargetOptions &targetopts,
51                     const LangOptions &langopts,
52                     bool TimePasses,
53                     const std::string &infile, raw_ostream *OS,
54                     LLVMContext &C) :
55       Diags(_Diags),
56       Action(action),
57       CodeGenOpts(compopts),
58       TargetOpts(targetopts),
59       LangOpts(langopts),
60       AsmOutStream(OS),
61       LLVMIRGeneration("LLVM IR Generation Time"),
62       Gen(CreateLLVMCodeGen(Diags, infile, compopts, C)) {
63       llvm::TimePassesIsEnabled = TimePasses;
64     }
65 
66     llvm::Module *takeModule() { return TheModule.take(); }
67 
68     virtual void Initialize(ASTContext &Ctx) {
69       Context = &Ctx;
70 
71       if (llvm::TimePassesIsEnabled)
72         LLVMIRGeneration.startTimer();
73 
74       Gen->Initialize(Ctx);
75 
76       TheModule.reset(Gen->GetModule());
77 
78       if (llvm::TimePassesIsEnabled)
79         LLVMIRGeneration.stopTimer();
80     }
81 
82     virtual void HandleTopLevelDecl(DeclGroupRef D) {
83       PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
84                                      Context->getSourceManager(),
85                                      "LLVM IR generation of declaration");
86 
87       if (llvm::TimePassesIsEnabled)
88         LLVMIRGeneration.startTimer();
89 
90       Gen->HandleTopLevelDecl(D);
91 
92       if (llvm::TimePassesIsEnabled)
93         LLVMIRGeneration.stopTimer();
94     }
95 
96     virtual void HandleTranslationUnit(ASTContext &C) {
97       {
98         PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
99         if (llvm::TimePassesIsEnabled)
100           LLVMIRGeneration.startTimer();
101 
102         Gen->HandleTranslationUnit(C);
103 
104         if (llvm::TimePassesIsEnabled)
105           LLVMIRGeneration.stopTimer();
106       }
107 
108       // Silently ignore if we weren't initialized for some reason.
109       if (!TheModule)
110         return;
111 
112       // Make sure IR generation is happy with the module. This is released by
113       // the module provider.
114       Module *M = Gen->ReleaseModule();
115       if (!M) {
116         // The module has been released by IR gen on failures, do not double
117         // free.
118         TheModule.take();
119         return;
120       }
121 
122       assert(TheModule.get() == M &&
123              "Unexpected module change during IR generation");
124 
125       // Install an inline asm handler so that diagnostics get printed through
126       // our diagnostics hooks.
127       LLVMContext &Ctx = TheModule->getContext();
128       LLVMContext::InlineAsmDiagHandlerTy OldHandler =
129         Ctx.getInlineAsmDiagnosticHandler();
130       void *OldContext = Ctx.getInlineAsmDiagnosticContext();
131       Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this);
132 
133       EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
134                         TheModule.get(), Action, AsmOutStream);
135 
136       Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext);
137     }
138 
139     virtual void HandleTagDeclDefinition(TagDecl *D) {
140       PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
141                                      Context->getSourceManager(),
142                                      "LLVM IR generation of declaration");
143       Gen->HandleTagDeclDefinition(D);
144     }
145 
146     virtual void CompleteTentativeDefinition(VarDecl *D) {
147       Gen->CompleteTentativeDefinition(D);
148     }
149 
150     virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
151       Gen->HandleVTable(RD, DefinitionRequired);
152     }
153 
154     static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context,
155                                      unsigned LocCookie) {
156       SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie);
157       ((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc);
158     }
159 
160     void InlineAsmDiagHandler2(const llvm::SMDiagnostic &,
161                                SourceLocation LocCookie);
162   };
163 }
164 
165 /// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr
166 /// buffer to be a valid FullSourceLoc.
167 static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D,
168                                             SourceManager &CSM) {
169   // Get both the clang and llvm source managers.  The location is relative to
170   // a memory buffer that the LLVM Source Manager is handling, we need to add
171   // a copy to the Clang source manager.
172   const llvm::SourceMgr &LSM = *D.getSourceMgr();
173 
174   // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr
175   // already owns its one and clang::SourceManager wants to own its one.
176   const MemoryBuffer *LBuf =
177   LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc()));
178 
179   // Create the copy and transfer ownership to clang::SourceManager.
180   llvm::MemoryBuffer *CBuf =
181   llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(),
182                                        LBuf->getBufferIdentifier());
183   FileID FID = CSM.createFileIDForMemBuffer(CBuf);
184 
185   // Translate the offset into the file.
186   unsigned Offset = D.getLoc().getPointer()  - LBuf->getBufferStart();
187   SourceLocation NewLoc =
188   CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset);
189   return FullSourceLoc(NewLoc, CSM);
190 }
191 
192 
193 /// InlineAsmDiagHandler2 - This function is invoked when the backend hits an
194 /// error parsing inline asm.  The SMDiagnostic indicates the error relative to
195 /// the temporary memory buffer that the inline asm parser has set up.
196 void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D,
197                                             SourceLocation LocCookie) {
198   // There are a couple of different kinds of errors we could get here.  First,
199   // we re-format the SMDiagnostic in terms of a clang diagnostic.
200 
201   // Strip "error: " off the start of the message string.
202   StringRef Message = D.getMessage();
203   if (Message.startswith("error: "))
204     Message = Message.substr(7);
205 
206   // If the SMDiagnostic has an inline asm source location, translate it.
207   FullSourceLoc Loc;
208   if (D.getLoc() != SMLoc())
209     Loc = ConvertBackendLocation(D, Context->getSourceManager());
210 
211 
212   // If this problem has clang-level source location information, report the
213   // issue as being an error in the source with a note showing the instantiated
214   // code.
215   if (LocCookie.isValid()) {
216     Diags.Report(LocCookie, diag::err_fe_inline_asm).AddString(Message);
217 
218     if (D.getLoc().isValid()) {
219       DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here);
220       // Convert the SMDiagnostic ranges into SourceRange and attach them
221       // to the diagnostic.
222       for (unsigned i = 0, e = D.getRanges().size(); i != e; ++i) {
223         std::pair<unsigned, unsigned> Range = D.getRanges()[i];
224         unsigned Column = D.getColumnNo();
225         B << SourceRange(Loc.getLocWithOffset(Range.first - Column),
226                          Loc.getLocWithOffset(Range.second - Column));
227       }
228     }
229     return;
230   }
231 
232   // Otherwise, report the backend error as occurring in the generated .s file.
233   // If Loc is invalid, we still need to report the error, it just gets no
234   // location info.
235   Diags.Report(Loc, diag::err_fe_inline_asm).AddString(Message);
236 }
237 
238 //
239 
240 CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext)
241   : Act(_Act), VMContext(_VMContext ? _VMContext : new LLVMContext),
242     OwnsVMContext(!_VMContext) {}
243 
244 CodeGenAction::~CodeGenAction() {
245   TheModule.reset();
246   if (OwnsVMContext)
247     delete VMContext;
248 }
249 
250 bool CodeGenAction::hasIRSupport() const { return true; }
251 
252 void CodeGenAction::EndSourceFileAction() {
253   // If the consumer creation failed, do nothing.
254   if (!getCompilerInstance().hasASTConsumer())
255     return;
256 
257   // Steal the module from the consumer.
258   TheModule.reset(BEConsumer->takeModule());
259 }
260 
261 llvm::Module *CodeGenAction::takeModule() {
262   return TheModule.take();
263 }
264 
265 llvm::LLVMContext *CodeGenAction::takeLLVMContext() {
266   OwnsVMContext = false;
267   return VMContext;
268 }
269 
270 static raw_ostream *GetOutputStream(CompilerInstance &CI,
271                                     StringRef InFile,
272                                     BackendAction Action) {
273   switch (Action) {
274   case Backend_EmitAssembly:
275     return CI.createDefaultOutputFile(false, InFile, "s");
276   case Backend_EmitLL:
277     return CI.createDefaultOutputFile(false, InFile, "ll");
278   case Backend_EmitBC:
279     return CI.createDefaultOutputFile(true, InFile, "bc");
280   case Backend_EmitNothing:
281     return 0;
282   case Backend_EmitMCNull:
283   case Backend_EmitObj:
284     return CI.createDefaultOutputFile(true, InFile, "o");
285   }
286 
287   llvm_unreachable("Invalid action!");
288 }
289 
290 ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI,
291                                               StringRef InFile) {
292   BackendAction BA = static_cast<BackendAction>(Act);
293   llvm::OwningPtr<raw_ostream> OS(GetOutputStream(CI, InFile, BA));
294   if (BA != Backend_EmitNothing && !OS)
295     return 0;
296 
297   BEConsumer =
298       new BackendConsumer(BA, CI.getDiagnostics(),
299                           CI.getCodeGenOpts(), CI.getTargetOpts(),
300                           CI.getLangOpts(),
301                           CI.getFrontendOpts().ShowTimers, InFile, OS.take(),
302                           *VMContext);
303   return BEConsumer;
304 }
305 
306 void CodeGenAction::ExecuteAction() {
307   // If this is an IR file, we have to treat it specially.
308   if (getCurrentFileKind() == IK_LLVM_IR) {
309     BackendAction BA = static_cast<BackendAction>(Act);
310     CompilerInstance &CI = getCompilerInstance();
311     raw_ostream *OS = GetOutputStream(CI, getCurrentFile(), BA);
312     if (BA != Backend_EmitNothing && !OS)
313       return;
314 
315     bool Invalid;
316     SourceManager &SM = CI.getSourceManager();
317     const llvm::MemoryBuffer *MainFile = SM.getBuffer(SM.getMainFileID(),
318                                                       &Invalid);
319     if (Invalid)
320       return;
321 
322     // FIXME: This is stupid, IRReader shouldn't take ownership.
323     llvm::MemoryBuffer *MainFileCopy =
324       llvm::MemoryBuffer::getMemBufferCopy(MainFile->getBuffer(),
325                                            getCurrentFile().c_str());
326 
327     llvm::SMDiagnostic Err;
328     TheModule.reset(ParseIR(MainFileCopy, Err, *VMContext));
329     if (!TheModule) {
330       // Translate from the diagnostic info to the SourceManager location.
331       SourceLocation Loc = SM.translateFileLineCol(
332         SM.getFileEntryForID(SM.getMainFileID()), Err.getLineNo(),
333         Err.getColumnNo() + 1);
334 
335       // Get a custom diagnostic for the error. We strip off a leading
336       // diagnostic code if there is one.
337       StringRef Msg = Err.getMessage();
338       if (Msg.startswith("error: "))
339         Msg = Msg.substr(7);
340       unsigned DiagID = CI.getDiagnostics().getCustomDiagID(
341           DiagnosticsEngine::Error, Msg);
342 
343       CI.getDiagnostics().Report(Loc, DiagID);
344       return;
345     }
346 
347     EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(),
348                       CI.getTargetOpts(), CI.getLangOpts(),
349                       TheModule.get(),
350                       BA, OS);
351     return;
352   }
353 
354   // Otherwise follow the normal AST path.
355   this->ASTFrontendAction::ExecuteAction();
356 }
357 
358 //
359 
360 EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext)
361   : CodeGenAction(Backend_EmitAssembly, _VMContext) {}
362 
363 EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext)
364   : CodeGenAction(Backend_EmitBC, _VMContext) {}
365 
366 EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext)
367   : CodeGenAction(Backend_EmitLL, _VMContext) {}
368 
369 EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext)
370   : CodeGenAction(Backend_EmitNothing, _VMContext) {}
371 
372 EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext)
373   : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
374 
375 EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext)
376   : CodeGenAction(Backend_EmitObj, _VMContext) {}
377