blob: ef44c96bb6bccb99c4fa5fbca4804c21c2f9d79c [file] [log] [blame]
Daniel Dunbarcea0c702010-02-25 04:37:45 +00001//===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===//
Daniel Dunbarc13935e2008-10-21 23:49:24 +00002//
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
Daniel Dunbarc1b17292010-06-15 17:48:49 +000010#include "clang/CodeGen/CodeGenAction.h"
Chris Lattner5bbb3c82009-03-29 16:50:03 +000011#include "clang/AST/ASTConsumer.h"
Daniel Dunbarb9bbd542009-11-15 06:48:46 +000012#include "clang/AST/ASTContext.h"
Chris Lattner5bbb3c82009-03-29 16:50:03 +000013#include "clang/AST/DeclGroup.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000014#include "clang/Basic/FileManager.h"
15#include "clang/Basic/SourceManager.h"
16#include "clang/Basic/TargetInfo.h"
Daniel Dunbarc1b17292010-06-15 17:48:49 +000017#include "clang/CodeGen/BackendUtil.h"
Daniel Dunbarb9bbd542009-11-15 06:48:46 +000018#include "clang/CodeGen/ModuleBuilder.h"
Daniel Dunbarcea0c702010-02-25 04:37:45 +000019#include "clang/Frontend/CompilerInstance.h"
Daniel Dunbaracadc552009-12-03 09:12:54 +000020#include "clang/Frontend/FrontendDiagnostic.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "llvm/ADT/OwningPtr.h"
22#include "llvm/ADT/SmallString.h"
23#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000024#include "llvm/IR/LLVMContext.h"
25#include "llvm/IR/Module.h"
Chandler Carruthb45836a2013-03-26 02:25:54 +000026#include "llvm/IRReader/IRReader.h"
Peter Collingbournef1d76db2011-10-30 17:30:44 +000027#include "llvm/Linker.h"
Daniel Dunbar3e111522010-06-07 23:21:04 +000028#include "llvm/Pass.h"
Chris Lattner5ec32e72010-04-06 18:38:50 +000029#include "llvm/Support/MemoryBuffer.h"
30#include "llvm/Support/SourceMgr.h"
Chris Lattner263d64c2009-02-18 01:37:30 +000031#include "llvm/Support/Timer.h"
Daniel Dunbarc13935e2008-10-21 23:49:24 +000032using namespace clang;
33using namespace llvm;
34
Nico Weber2992efa2011-01-25 20:34:14 +000035namespace clang {
Benjamin Kramer16634c22009-11-28 10:07:24 +000036 class BackendConsumer : public ASTConsumer {
David Blaikie68e081d2011-12-20 02:48:34 +000037 virtual void anchor();
David Blaikie9c902b52011-09-25 23:23:43 +000038 DiagnosticsEngine &Diags;
Daniel Dunbarc13935e2008-10-21 23:49:24 +000039 BackendAction Action;
Daniel Dunbarde182242009-11-30 08:39:32 +000040 const CodeGenOptions &CodeGenOpts;
Daniel Dunbarde182242009-11-30 08:39:32 +000041 const TargetOptions &TargetOpts;
Dan Gohmanfec0ff82011-07-05 22:02:36 +000042 const LangOptions &LangOpts;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000043 raw_ostream *AsmOutStream;
Chris Lattnereae6cb62009-03-05 08:00:35 +000044 ASTContext *Context;
Daniel Dunbarb3a36cf2008-10-29 08:50:02 +000045
Chris Lattner263d64c2009-02-18 01:37:30 +000046 Timer LLVMIRGeneration;
Mike Stump11289f42009-09-09 15:08:12 +000047
Dylan Noblesmithe2778992012-02-05 02:12:40 +000048 OwningPtr<CodeGenerator> Gen;
Mike Stump11289f42009-09-09 15:08:12 +000049
Dylan Noblesmithe2778992012-02-05 02:12:40 +000050 OwningPtr<llvm::Module> TheModule, LinkModule;
Daniel Dunbarc13935e2008-10-21 23:49:24 +000051
Mike Stump11289f42009-09-09 15:08:12 +000052 public:
David Blaikie9c902b52011-09-25 23:23:43 +000053 BackendConsumer(BackendAction action, DiagnosticsEngine &_Diags,
Daniel Dunbar6d5824f2010-06-07 23:19:17 +000054 const CodeGenOptions &compopts,
Dan Gohmanfec0ff82011-07-05 22:02:36 +000055 const TargetOptions &targetopts,
Rafael Espindola666a2ab2013-12-18 16:38:48 +000056 const LangOptions &langopts, bool TimePasses,
57 const std::string &infile, llvm::Module *LinkModule,
58 raw_ostream *OS, LLVMContext &C)
59 : Diags(_Diags), Action(action), CodeGenOpts(compopts),
60 TargetOpts(targetopts), LangOpts(langopts), AsmOutStream(OS),
61 Context(), LLVMIRGeneration("LLVM IR Generation Time"),
62 Gen(CreateLLVMCodeGen(Diags, infile, compopts, targetopts, C)),
63 LinkModule(LinkModule) {
Daniel Dunbar8e705052009-11-30 08:39:52 +000064 llvm::TimePassesIsEnabled = TimePasses;
Chris Lattnerdeffa132009-02-18 01:23:44 +000065 }
Daniel Dunbarc13935e2008-10-21 23:49:24 +000066
Daniel Dunbar400a6932010-02-25 04:37:50 +000067 llvm::Module *takeModule() { return TheModule.take(); }
Peter Collingbournef1d76db2011-10-30 17:30:44 +000068 llvm::Module *takeLinkModule() { return LinkModule.take(); }
Daniel Dunbar400a6932010-02-25 04:37:50 +000069
Rafael Espindoladf88f6f2012-03-08 15:51:03 +000070 virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
71 Gen->HandleCXXStaticMemberVarInstantiation(VD);
Rafael Espindola189fa742012-03-05 10:54:55 +000072 }
73
Chris Lattner5cf49fe2009-03-28 02:18:25 +000074 virtual void Initialize(ASTContext &Ctx) {
75 Context = &Ctx;
Mike Stump11289f42009-09-09 15:08:12 +000076
Daniel Dunbar8e705052009-11-30 08:39:52 +000077 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +000078 LLVMIRGeneration.startTimer();
Mike Stump11289f42009-09-09 15:08:12 +000079
Chris Lattner5cf49fe2009-03-28 02:18:25 +000080 Gen->Initialize(Ctx);
Daniel Dunbarc13935e2008-10-21 23:49:24 +000081
Daniel Dunbar400a6932010-02-25 04:37:50 +000082 TheModule.reset(Gen->GetModule());
Mike Stump11289f42009-09-09 15:08:12 +000083
Daniel Dunbar8e705052009-11-30 08:39:52 +000084 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +000085 LLVMIRGeneration.stopTimer();
Daniel Dunbarc13935e2008-10-21 23:49:24 +000086 }
Mike Stump11289f42009-09-09 15:08:12 +000087
Argyrios Kyrtzidis841dd882011-11-18 00:26:59 +000088 virtual bool HandleTopLevelDecl(DeclGroupRef D) {
Chris Lattner5bbb3c82009-03-29 16:50:03 +000089 PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
Chris Lattnereae6cb62009-03-05 08:00:35 +000090 Context->getSourceManager(),
91 "LLVM IR generation of declaration");
Mike Stump11289f42009-09-09 15:08:12 +000092
Daniel Dunbar8e705052009-11-30 08:39:52 +000093 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +000094 LLVMIRGeneration.startTimer();
Chris Lattner5bbb3c82009-03-29 16:50:03 +000095
Daniel Dunbarc13935e2008-10-21 23:49:24 +000096 Gen->HandleTopLevelDecl(D);
Chris Lattner263d64c2009-02-18 01:37:30 +000097
Daniel Dunbar8e705052009-11-30 08:39:52 +000098 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +000099 LLVMIRGeneration.stopTimer();
Argyrios Kyrtzidis841dd882011-11-18 00:26:59 +0000100
101 return true;
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000102 }
Mike Stump11289f42009-09-09 15:08:12 +0000103
Chris Lattnercf169832009-03-28 04:11:33 +0000104 virtual void HandleTranslationUnit(ASTContext &C) {
Chris Lattnereae6cb62009-03-05 08:00:35 +0000105 {
Chris Lattnere46de752009-03-06 06:46:31 +0000106 PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
Daniel Dunbar8e705052009-11-30 08:39:52 +0000107 if (llvm::TimePassesIsEnabled)
Chris Lattnereae6cb62009-03-05 08:00:35 +0000108 LLVMIRGeneration.startTimer();
Chris Lattner263d64c2009-02-18 01:37:30 +0000109
Chris Lattnercf169832009-03-28 04:11:33 +0000110 Gen->HandleTranslationUnit(C);
Daniel Dunbara94d8732008-11-11 06:35:39 +0000111
Daniel Dunbar8e705052009-11-30 08:39:52 +0000112 if (llvm::TimePassesIsEnabled)
Chris Lattnereae6cb62009-03-05 08:00:35 +0000113 LLVMIRGeneration.stopTimer();
114 }
Chris Lattner263d64c2009-02-18 01:37:30 +0000115
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000116 // Silently ignore if we weren't initialized for some reason.
Daniel Dunbar3e111522010-06-07 23:21:04 +0000117 if (!TheModule)
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000118 return;
Mike Stump11289f42009-09-09 15:08:12 +0000119
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000120 // Make sure IR generation is happy with the module. This is released by
121 // the module provider.
Douglas Gregorde3ef502011-11-30 23:21:26 +0000122 llvm::Module *M = Gen->ReleaseModule();
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000123 if (!M) {
124 // The module has been released by IR gen on failures, do not double
125 // free.
126 TheModule.take();
127 return;
128 }
129
130 assert(TheModule.get() == M &&
131 "Unexpected module change during IR generation");
132
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000133 // Link LinkModule into this module if present, preserving its validity.
134 if (LinkModule) {
135 std::string ErrorMsg;
136 if (Linker::LinkModules(M, LinkModule.get(), Linker::PreserveSource,
137 &ErrorMsg)) {
138 Diags.Report(diag::err_fe_cannot_link_module)
139 << LinkModule->getModuleIdentifier() << ErrorMsg;
140 return;
141 }
142 }
143
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000144 // Install an inline asm handler so that diagnostics get printed through
145 // our diagnostics hooks.
146 LLVMContext &Ctx = TheModule->getContext();
Chris Lattner068f2ab2010-11-17 08:13:04 +0000147 LLVMContext::InlineAsmDiagHandlerTy OldHandler =
148 Ctx.getInlineAsmDiagnosticHandler();
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000149 void *OldContext = Ctx.getInlineAsmDiagnosticContext();
Chris Lattner068f2ab2010-11-17 08:13:04 +0000150 Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this);
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000151
Dan Gohmanfec0ff82011-07-05 22:02:36 +0000152 EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
Alp Tokere83b9062014-01-02 15:08:04 +0000153 C.getTargetInfo().getTargetDescription(),
Daniel Dunbar3e111522010-06-07 23:21:04 +0000154 TheModule.get(), Action, AsmOutStream);
Rafael Espindola666a2ab2013-12-18 16:38:48 +0000155
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000156 Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext);
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000157 }
Mike Stump11289f42009-09-09 15:08:12 +0000158
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000159 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattnereae6cb62009-03-05 08:00:35 +0000160 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
161 Context->getSourceManager(),
162 "LLVM IR generation of declaration");
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000163 Gen->HandleTagDeclDefinition(D);
164 }
Douglas Gregorbeecd582009-04-21 17:11:58 +0000165
David Blaikie48ad6dc2013-07-13 21:08:14 +0000166 virtual void HandleTagDeclRequiredDefinition(const TagDecl *D) {
167 Gen->HandleTagDeclRequiredDefinition(D);
168 }
169
Douglas Gregorbeecd582009-04-21 17:11:58 +0000170 virtual void CompleteTentativeDefinition(VarDecl *D) {
171 Gen->CompleteTentativeDefinition(D);
172 }
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000173
Douglas Gregor88d292c2010-05-13 16:44:06 +0000174 virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
175 Gen->HandleVTable(RD, DefinitionRequired);
176 }
177
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000178 virtual void HandleLinkerOptionPragma(llvm::StringRef Opts) {
179 Gen->HandleLinkerOptionPragma(Opts);
180 }
181
Aaron Ballman5d041be2013-06-04 02:07:14 +0000182 virtual void HandleDetectMismatch(llvm::StringRef Name,
183 llvm::StringRef Value) {
184 Gen->HandleDetectMismatch(Name, Value);
185 }
186
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000187 virtual void HandleDependentLibrary(llvm::StringRef Opts) {
188 Gen->HandleDependentLibrary(Opts);
189 }
190
Chris Lattner5ec32e72010-04-06 18:38:50 +0000191 static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context,
192 unsigned LocCookie) {
193 SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie);
194 ((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc);
195 }
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000196
Chris Lattner5ec32e72010-04-06 18:38:50 +0000197 void InlineAsmDiagHandler2(const llvm::SMDiagnostic &,
198 SourceLocation LocCookie);
Mike Stump11289f42009-09-09 15:08:12 +0000199 };
David Blaikie68e081d2011-12-20 02:48:34 +0000200
201 void BackendConsumer::anchor() {}
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000202}
203
Chris Lattner79f67a72010-04-08 00:23:06 +0000204/// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr
205/// buffer to be a valid FullSourceLoc.
206static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D,
207 SourceManager &CSM) {
208 // Get both the clang and llvm source managers. The location is relative to
209 // a memory buffer that the LLVM Source Manager is handling, we need to add
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000210 // a copy to the Clang source manager.
Chris Lattner79f67a72010-04-08 00:23:06 +0000211 const llvm::SourceMgr &LSM = *D.getSourceMgr();
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000212
Chris Lattner79f67a72010-04-08 00:23:06 +0000213 // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr
214 // already owns its one and clang::SourceManager wants to own its one.
215 const MemoryBuffer *LBuf =
216 LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc()));
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000217
Chris Lattner79f67a72010-04-08 00:23:06 +0000218 // Create the copy and transfer ownership to clang::SourceManager.
219 llvm::MemoryBuffer *CBuf =
220 llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(),
221 LBuf->getBufferIdentifier());
222 FileID FID = CSM.createFileIDForMemBuffer(CBuf);
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000223
Chris Lattner79f67a72010-04-08 00:23:06 +0000224 // Translate the offset into the file.
225 unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart();
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000226 SourceLocation NewLoc =
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000227 CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset);
Chris Lattner79f67a72010-04-08 00:23:06 +0000228 return FullSourceLoc(NewLoc, CSM);
229}
230
Chris Lattner6d672132010-04-06 17:52:14 +0000231
Chris Lattner5ec32e72010-04-06 18:38:50 +0000232/// InlineAsmDiagHandler2 - This function is invoked when the backend hits an
233/// error parsing inline asm. The SMDiagnostic indicates the error relative to
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000234/// the temporary memory buffer that the inline asm parser has set up.
Chris Lattner5ec32e72010-04-06 18:38:50 +0000235void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D,
236 SourceLocation LocCookie) {
237 // There are a couple of different kinds of errors we could get here. First,
238 // we re-format the SMDiagnostic in terms of a clang diagnostic.
Argyrios Kyrtzidisa11b35a2012-02-01 06:36:49 +0000239
240 // Strip "error: " off the start of the message string.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000241 StringRef Message = D.getMessage();
Argyrios Kyrtzidisa11b35a2012-02-01 06:36:49 +0000242 if (Message.startswith("error: "))
243 Message = Message.substr(7);
Chris Lattner5ec32e72010-04-06 18:38:50 +0000244
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000245 // If the SMDiagnostic has an inline asm source location, translate it.
Chris Lattner5ec32e72010-04-06 18:38:50 +0000246 FullSourceLoc Loc;
Chris Lattner79f67a72010-04-08 00:23:06 +0000247 if (D.getLoc() != SMLoc())
248 Loc = ConvertBackendLocation(D, Context->getSourceManager());
Chris Lattnerf4a4bec2012-01-31 06:13:55 +0000249
Argyrios Kyrtzidisa11b35a2012-02-01 06:36:49 +0000250
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000251 // If this problem has clang-level source location information, report the
252 // issue as being an error in the source with a note showing the instantiated
253 // code.
254 if (LocCookie.isValid()) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000255 Diags.Report(LocCookie, diag::err_fe_inline_asm).AddString(Message);
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000256
Benjamin Kramere06b2b72011-10-16 10:48:28 +0000257 if (D.getLoc().isValid()) {
258 DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here);
259 // Convert the SMDiagnostic ranges into SourceRange and attach them
260 // to the diagnostic.
261 for (unsigned i = 0, e = D.getRanges().size(); i != e; ++i) {
262 std::pair<unsigned, unsigned> Range = D.getRanges()[i];
263 unsigned Column = D.getColumnNo();
264 B << SourceRange(Loc.getLocWithOffset(Range.first - Column),
265 Loc.getLocWithOffset(Range.second - Column));
266 }
267 }
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000268 return;
269 }
270
Chris Lattner57540c52011-04-15 05:22:18 +0000271 // Otherwise, report the backend error as occurring in the generated .s file.
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000272 // If Loc is invalid, we still need to report the error, it just gets no
273 // location info.
274 Diags.Report(Loc, diag::err_fe_inline_asm).AddString(Message);
Chris Lattner5ec32e72010-04-06 18:38:50 +0000275}
276
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000277//
278
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000279CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext)
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000280 : Act(_Act), LinkModule(0),
281 VMContext(_VMContext ? _VMContext : new LLVMContext),
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000282 OwnsVMContext(!_VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000283
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000284CodeGenAction::~CodeGenAction() {
285 TheModule.reset();
286 if (OwnsVMContext)
287 delete VMContext;
288}
Daniel Dunbare8ecf9a2010-02-25 20:37:44 +0000289
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000290bool CodeGenAction::hasIRSupport() const { return true; }
291
Daniel Dunbar400a6932010-02-25 04:37:50 +0000292void CodeGenAction::EndSourceFileAction() {
293 // If the consumer creation failed, do nothing.
294 if (!getCompilerInstance().hasASTConsumer())
295 return;
296
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000297 // If we were given a link module, release consumer's ownership of it.
298 if (LinkModule)
299 BEConsumer->takeLinkModule();
300
Daniel Dunbar400a6932010-02-25 04:37:50 +0000301 // Steal the module from the consumer.
Nico Weber2992efa2011-01-25 20:34:14 +0000302 TheModule.reset(BEConsumer->takeModule());
Daniel Dunbar400a6932010-02-25 04:37:50 +0000303}
304
305llvm::Module *CodeGenAction::takeModule() {
306 return TheModule.take();
307}
308
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000309llvm::LLVMContext *CodeGenAction::takeLLVMContext() {
310 OwnsVMContext = false;
311 return VMContext;
312}
313
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000314static raw_ostream *GetOutputStream(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000315 StringRef InFile,
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000316 BackendAction Action) {
317 switch (Action) {
318 case Backend_EmitAssembly:
319 return CI.createDefaultOutputFile(false, InFile, "s");
320 case Backend_EmitLL:
321 return CI.createDefaultOutputFile(false, InFile, "ll");
322 case Backend_EmitBC:
323 return CI.createDefaultOutputFile(true, InFile, "bc");
324 case Backend_EmitNothing:
325 return 0;
326 case Backend_EmitMCNull:
327 case Backend_EmitObj:
328 return CI.createDefaultOutputFile(true, InFile, "o");
329 }
330
David Blaikie83d382b2011-09-23 05:06:16 +0000331 llvm_unreachable("Invalid action!");
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000332}
333
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000334ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000335 StringRef InFile) {
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000336 BackendAction BA = static_cast<BackendAction>(Act);
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000337 OwningPtr<raw_ostream> OS(GetOutputStream(CI, InFile, BA));
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000338 if (BA != Backend_EmitNothing && !OS)
339 return 0;
340
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000341 llvm::Module *LinkModuleToUse = LinkModule;
342
343 // If we were not given a link module, and the user requested that one be
344 // loaded from bitcode, do so now.
345 const std::string &LinkBCFile = CI.getCodeGenOpts().LinkBitcodeFile;
346 if (!LinkModuleToUse && !LinkBCFile.empty()) {
347 std::string ErrorStr;
348
349 llvm::MemoryBuffer *BCBuf =
350 CI.getFileManager().getBufferForFile(LinkBCFile, &ErrorStr);
351 if (!BCBuf) {
352 CI.getDiagnostics().Report(diag::err_cannot_open_file)
353 << LinkBCFile << ErrorStr;
354 return 0;
355 }
356
Rafael Espindola6b6004a2014-01-13 18:31:09 +0000357 ErrorOr<llvm::Module *> ModuleOrErr =
358 getLazyBitcodeModule(BCBuf, *VMContext);
359 if (error_code EC = ModuleOrErr.getError()) {
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000360 CI.getDiagnostics().Report(diag::err_cannot_open_file)
Rafael Espindola6b6004a2014-01-13 18:31:09 +0000361 << LinkBCFile << EC.message();
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000362 return 0;
363 }
Rafael Espindola6b6004a2014-01-13 18:31:09 +0000364 LinkModuleToUse = ModuleOrErr.get();
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000365 }
366
Nico Weber2992efa2011-01-25 20:34:14 +0000367 BEConsumer =
368 new BackendConsumer(BA, CI.getDiagnostics(),
369 CI.getCodeGenOpts(), CI.getTargetOpts(),
Dan Gohmanfec0ff82011-07-05 22:02:36 +0000370 CI.getLangOpts(),
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000371 CI.getFrontendOpts().ShowTimers, InFile,
372 LinkModuleToUse, OS.take(), *VMContext);
Nico Weber2992efa2011-01-25 20:34:14 +0000373 return BEConsumer;
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000374}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000375
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000376void CodeGenAction::ExecuteAction() {
377 // If this is an IR file, we have to treat it specially.
378 if (getCurrentFileKind() == IK_LLVM_IR) {
379 BackendAction BA = static_cast<BackendAction>(Act);
380 CompilerInstance &CI = getCompilerInstance();
381 raw_ostream *OS = GetOutputStream(CI, getCurrentFile(), BA);
382 if (BA != Backend_EmitNothing && !OS)
383 return;
384
385 bool Invalid;
386 SourceManager &SM = CI.getSourceManager();
387 const llvm::MemoryBuffer *MainFile = SM.getBuffer(SM.getMainFileID(),
388 &Invalid);
389 if (Invalid)
390 return;
391
392 // FIXME: This is stupid, IRReader shouldn't take ownership.
393 llvm::MemoryBuffer *MainFileCopy =
394 llvm::MemoryBuffer::getMemBufferCopy(MainFile->getBuffer(),
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000395 getCurrentFile());
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000396
397 llvm::SMDiagnostic Err;
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000398 TheModule.reset(ParseIR(MainFileCopy, Err, *VMContext));
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000399 if (!TheModule) {
400 // Translate from the diagnostic info to the SourceManager location.
Argyrios Kyrtzidis27bf76d2011-09-19 20:40:38 +0000401 SourceLocation Loc = SM.translateFileLineCol(
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000402 SM.getFileEntryForID(SM.getMainFileID()), Err.getLineNo(),
403 Err.getColumnNo() + 1);
404
Alp Tokerbc043f22013-12-21 05:20:03 +0000405 // Strip off a leading diagnostic code if there is one.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000406 StringRef Msg = Err.getMessage();
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000407 if (Msg.startswith("error: "))
408 Msg = Msg.substr(7);
Benjamin Kramer778b8b82012-03-16 22:31:42 +0000409
Alp Tokerbc043f22013-12-21 05:20:03 +0000410 unsigned DiagID =
411 CI.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, "%0");
Benjamin Kramer778b8b82012-03-16 22:31:42 +0000412
Alp Tokerbc043f22013-12-21 05:20:03 +0000413 CI.getDiagnostics().Report(Loc, DiagID) << Msg;
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000414 return;
415 }
Rafael Espindolad5e81e52013-12-20 22:01:25 +0000416 const TargetOptions &TargetOpts = CI.getTargetOpts();
417 if (TheModule->getTargetTriple() != TargetOpts.Triple) {
418 unsigned DiagID = CI.getDiagnostics().getCustomDiagID(
419 DiagnosticsEngine::Warning,
420 "overriding the module target triple with %0");
421
422 CI.getDiagnostics().Report(SourceLocation(), DiagID) << TargetOpts.Triple;
423 TheModule->setTargetTriple(TargetOpts.Triple);
424 }
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000425
Alp Tokere83b9062014-01-02 15:08:04 +0000426 EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(), TargetOpts,
427 CI.getLangOpts(), CI.getTarget().getTargetDescription(),
428 TheModule.get(), BA, OS);
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000429 return;
430 }
431
432 // Otherwise follow the normal AST path.
433 this->ASTFrontendAction::ExecuteAction();
434}
435
436//
437
David Blaikie68e081d2011-12-20 02:48:34 +0000438void EmitAssemblyAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000439EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext)
440 : CodeGenAction(Backend_EmitAssembly, _VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000441
David Blaikie68e081d2011-12-20 02:48:34 +0000442void EmitBCAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000443EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext)
444 : CodeGenAction(Backend_EmitBC, _VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000445
David Blaikie68e081d2011-12-20 02:48:34 +0000446void EmitLLVMAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000447EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext)
448 : CodeGenAction(Backend_EmitLL, _VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000449
David Blaikie68e081d2011-12-20 02:48:34 +0000450void EmitLLVMOnlyAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000451EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext)
452 : CodeGenAction(Backend_EmitNothing, _VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000453
David Blaikie68e081d2011-12-20 02:48:34 +0000454void EmitCodeGenOnlyAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000455EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext)
456 : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
Daniel Dunbar4c77a642010-05-25 18:41:01 +0000457
David Blaikie68e081d2011-12-20 02:48:34 +0000458void EmitObjAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000459EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext)
460 : CodeGenAction(Backend_EmitObj, _VMContext) {}