blob: 3072204c9b66d1ddaf153b1a53ca28dd64a5ff9f [file] [log] [blame]
Daniel Dunbar4ee34612010-02-25 04:37:45 +00001//===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===//
Daniel Dunbard69bacc2008-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 Dunbar9b414d32010-06-15 17:48:49 +000010#include "clang/CodeGen/CodeGenAction.h"
Chris Lattner682bf922009-03-29 16:50:03 +000011#include "clang/AST/ASTConsumer.h"
Daniel Dunbard58c03f2009-11-15 06:48:46 +000012#include "clang/AST/ASTContext.h"
Chris Lattner682bf922009-03-29 16:50:03 +000013#include "clang/AST/DeclGroup.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000014#include "clang/Basic/FileManager.h"
15#include "clang/Basic/SourceManager.h"
16#include "clang/Basic/TargetInfo.h"
Daniel Dunbar9b414d32010-06-15 17:48:49 +000017#include "clang/CodeGen/BackendUtil.h"
Daniel Dunbard58c03f2009-11-15 06:48:46 +000018#include "clang/CodeGen/ModuleBuilder.h"
Daniel Dunbar4ee34612010-02-25 04:37:45 +000019#include "clang/Frontend/CompilerInstance.h"
Daniel Dunbar3be0d192009-12-03 09:12:54 +000020#include "clang/Frontend/FrontendDiagnostic.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "llvm/ADT/OwningPtr.h"
22#include "llvm/ADT/SmallString.h"
23#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000024#include "llvm/IR/LLVMContext.h"
25#include "llvm/IR/Module.h"
Chandler Carruth9cc935b2013-03-26 02:25:54 +000026#include "llvm/IRReader/IRReader.h"
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +000027#include "llvm/Linker.h"
Daniel Dunbar05a7f3d2010-06-07 23:21:04 +000028#include "llvm/Pass.h"
Chris Lattner6da9eb62010-04-06 18:38:50 +000029#include "llvm/Support/MemoryBuffer.h"
30#include "llvm/Support/SourceMgr.h"
Chris Lattner6f114eb2009-02-18 01:37:30 +000031#include "llvm/Support/Timer.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000032using namespace clang;
33using namespace llvm;
34
Nico Weber5aa74af2011-01-25 20:34:14 +000035namespace clang {
Benjamin Kramerbd218282009-11-28 10:07:24 +000036 class BackendConsumer : public ASTConsumer {
David Blaikie99ba9e32011-12-20 02:48:34 +000037 virtual void anchor();
David Blaikied6471f72011-09-25 23:23:43 +000038 DiagnosticsEngine &Diags;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000039 BackendAction Action;
Daniel Dunbar3636e1d2009-11-30 08:39:32 +000040 const CodeGenOptions &CodeGenOpts;
Daniel Dunbar3636e1d2009-11-30 08:39:32 +000041 const TargetOptions &TargetOpts;
Dan Gohmanb18b8ad2011-07-05 22:02:36 +000042 const LangOptions &LangOpts;
Chris Lattner5f9e2722011-07-23 10:55:15 +000043 raw_ostream *AsmOutStream;
Chris Lattner49f28ca2009-03-05 08:00:35 +000044 ASTContext *Context;
Daniel Dunbar90f41302008-10-29 08:50:02 +000045
Chris Lattner6f114eb2009-02-18 01:37:30 +000046 Timer LLVMIRGeneration;
Mike Stump1eb44332009-09-09 15:08:12 +000047
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000048 OwningPtr<CodeGenerator> Gen;
Mike Stump1eb44332009-09-09 15:08:12 +000049
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000050 OwningPtr<llvm::Module> TheModule, LinkModule;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000051
Mike Stump1eb44332009-09-09 15:08:12 +000052 public:
David Blaikied6471f72011-09-25 23:23:43 +000053 BackendConsumer(BackendAction action, DiagnosticsEngine &_Diags,
Daniel Dunbar6b0cf672010-06-07 23:19:17 +000054 const CodeGenOptions &compopts,
Dan Gohmanb18b8ad2011-07-05 22:02:36 +000055 const TargetOptions &targetopts,
56 const LangOptions &langopts,
57 bool TimePasses,
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +000058 const std::string &infile,
59 llvm::Module *LinkModule,
60 raw_ostream *OS,
Chris Lattnercabae682010-04-06 17:52:14 +000061 LLVMContext &C) :
Daniel Dunbar3be0d192009-12-03 09:12:54 +000062 Diags(_Diags),
Mike Stump1eb44332009-09-09 15:08:12 +000063 Action(action),
Chandler Carruth2811ccf2009-11-12 17:24:48 +000064 CodeGenOpts(compopts),
Daniel Dunbard58c03f2009-11-15 06:48:46 +000065 TargetOpts(targetopts),
Dan Gohmanb18b8ad2011-07-05 22:02:36 +000066 LangOpts(langopts),
Chris Lattner03eacc72009-07-14 20:39:15 +000067 AsmOutStream(OS),
Douglas Gregorc7be1022012-11-05 23:58:27 +000068 Context(),
Chris Lattner6f114eb2009-02-18 01:37:30 +000069 LLVMIRGeneration("LLVM IR Generation Time"),
Bill Wendlinge1092df2013-02-14 08:09:20 +000070 Gen(CreateLLVMCodeGen(Diags, infile, compopts, targetopts, C)),
Douglas Gregorc7be1022012-11-05 23:58:27 +000071 LinkModule(LinkModule)
72 {
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +000073 llvm::TimePassesIsEnabled = TimePasses;
Chris Lattner44502662009-02-18 01:23:44 +000074 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +000075
Daniel Dunbarb954e982010-02-25 04:37:50 +000076 llvm::Module *takeModule() { return TheModule.take(); }
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +000077 llvm::Module *takeLinkModule() { return LinkModule.take(); }
Daniel Dunbarb954e982010-02-25 04:37:50 +000078
Rafael Espindola02503932012-03-08 15:51:03 +000079 virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
80 Gen->HandleCXXStaticMemberVarInstantiation(VD);
Rafael Espindola234fe652012-03-05 10:54:55 +000081 }
82
Chris Lattner7bb0da02009-03-28 02:18:25 +000083 virtual void Initialize(ASTContext &Ctx) {
84 Context = &Ctx;
Mike Stump1eb44332009-09-09 15:08:12 +000085
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +000086 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +000087 LLVMIRGeneration.startTimer();
Mike Stump1eb44332009-09-09 15:08:12 +000088
Chris Lattner7bb0da02009-03-28 02:18:25 +000089 Gen->Initialize(Ctx);
Daniel Dunbard69bacc2008-10-21 23:49:24 +000090
Daniel Dunbarb954e982010-02-25 04:37:50 +000091 TheModule.reset(Gen->GetModule());
Mike Stump1eb44332009-09-09 15:08:12 +000092
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +000093 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +000094 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +000095 }
Mike Stump1eb44332009-09-09 15:08:12 +000096
Argyrios Kyrtzidis88c25962011-11-18 00:26:59 +000097 virtual bool HandleTopLevelDecl(DeclGroupRef D) {
Chris Lattner682bf922009-03-29 16:50:03 +000098 PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
Chris Lattner49f28ca2009-03-05 08:00:35 +000099 Context->getSourceManager(),
100 "LLVM IR generation of declaration");
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000102 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +0000103 LLVMIRGeneration.startTimer();
Chris Lattner682bf922009-03-29 16:50:03 +0000104
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000105 Gen->HandleTopLevelDecl(D);
Chris Lattner6f114eb2009-02-18 01:37:30 +0000106
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000107 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +0000108 LLVMIRGeneration.stopTimer();
Argyrios Kyrtzidis88c25962011-11-18 00:26:59 +0000109
110 return true;
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000111 }
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000113 virtual void HandleTranslationUnit(ASTContext &C) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000114 {
Chris Lattner14f234e2009-03-06 06:46:31 +0000115 PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000116 if (llvm::TimePassesIsEnabled)
Chris Lattner49f28ca2009-03-05 08:00:35 +0000117 LLVMIRGeneration.startTimer();
Chris Lattner6f114eb2009-02-18 01:37:30 +0000118
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000119 Gen->HandleTranslationUnit(C);
Daniel Dunbard68ba0e2008-11-11 06:35:39 +0000120
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000121 if (llvm::TimePassesIsEnabled)
Chris Lattner49f28ca2009-03-05 08:00:35 +0000122 LLVMIRGeneration.stopTimer();
123 }
Chris Lattner6f114eb2009-02-18 01:37:30 +0000124
Daniel Dunbar897c6762010-06-07 23:20:08 +0000125 // Silently ignore if we weren't initialized for some reason.
Daniel Dunbar05a7f3d2010-06-07 23:21:04 +0000126 if (!TheModule)
Daniel Dunbar897c6762010-06-07 23:20:08 +0000127 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Daniel Dunbar897c6762010-06-07 23:20:08 +0000129 // Make sure IR generation is happy with the module. This is released by
130 // the module provider.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000131 llvm::Module *M = Gen->ReleaseModule();
Daniel Dunbar897c6762010-06-07 23:20:08 +0000132 if (!M) {
133 // The module has been released by IR gen on failures, do not double
134 // free.
135 TheModule.take();
136 return;
137 }
138
139 assert(TheModule.get() == M &&
140 "Unexpected module change during IR generation");
141
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +0000142 // Link LinkModule into this module if present, preserving its validity.
143 if (LinkModule) {
144 std::string ErrorMsg;
145 if (Linker::LinkModules(M, LinkModule.get(), Linker::PreserveSource,
146 &ErrorMsg)) {
147 Diags.Report(diag::err_fe_cannot_link_module)
148 << LinkModule->getModuleIdentifier() << ErrorMsg;
149 return;
150 }
151 }
152
Daniel Dunbar897c6762010-06-07 23:20:08 +0000153 // Install an inline asm handler so that diagnostics get printed through
154 // our diagnostics hooks.
155 LLVMContext &Ctx = TheModule->getContext();
Chris Lattner063e4762010-11-17 08:13:04 +0000156 LLVMContext::InlineAsmDiagHandlerTy OldHandler =
157 Ctx.getInlineAsmDiagnosticHandler();
Daniel Dunbar897c6762010-06-07 23:20:08 +0000158 void *OldContext = Ctx.getInlineAsmDiagnosticContext();
Chris Lattner063e4762010-11-17 08:13:04 +0000159 Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this);
Daniel Dunbar897c6762010-06-07 23:20:08 +0000160
Dan Gohmanb18b8ad2011-07-05 22:02:36 +0000161 EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
Daniel Dunbar05a7f3d2010-06-07 23:21:04 +0000162 TheModule.get(), Action, AsmOutStream);
Daniel Dunbar897c6762010-06-07 23:20:08 +0000163
164 Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000165 }
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000167 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000168 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
169 Context->getSourceManager(),
170 "LLVM IR generation of declaration");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000171 Gen->HandleTagDeclDefinition(D);
172 }
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000173
David Blaikie658cd2c2013-07-13 21:08:14 +0000174 virtual void HandleTagDeclRequiredDefinition(const TagDecl *D) {
175 Gen->HandleTagDeclRequiredDefinition(D);
176 }
177
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000178 virtual void CompleteTentativeDefinition(VarDecl *D) {
179 Gen->CompleteTentativeDefinition(D);
180 }
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000181
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000182 virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
183 Gen->HandleVTable(RD, DefinitionRequired);
184 }
185
Reid Kleckner3190ca92013-05-08 13:44:39 +0000186 virtual void HandleLinkerOptionPragma(llvm::StringRef Opts) {
187 Gen->HandleLinkerOptionPragma(Opts);
188 }
189
Aaron Ballmana7ff62f2013-06-04 02:07:14 +0000190 virtual void HandleDetectMismatch(llvm::StringRef Name,
191 llvm::StringRef Value) {
192 Gen->HandleDetectMismatch(Name, Value);
193 }
194
Reid Kleckner3190ca92013-05-08 13:44:39 +0000195 virtual void HandleDependentLibrary(llvm::StringRef Opts) {
196 Gen->HandleDependentLibrary(Opts);
197 }
198
Chris Lattner6da9eb62010-04-06 18:38:50 +0000199 static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context,
200 unsigned LocCookie) {
201 SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie);
202 ((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc);
203 }
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000204
Chris Lattner6da9eb62010-04-06 18:38:50 +0000205 void InlineAsmDiagHandler2(const llvm::SMDiagnostic &,
206 SourceLocation LocCookie);
Mike Stump1eb44332009-09-09 15:08:12 +0000207 };
David Blaikie99ba9e32011-12-20 02:48:34 +0000208
209 void BackendConsumer::anchor() {}
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000210}
211
Chris Lattnerd6f19062010-04-08 00:23:06 +0000212/// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr
213/// buffer to be a valid FullSourceLoc.
214static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D,
215 SourceManager &CSM) {
216 // Get both the clang and llvm source managers. The location is relative to
217 // a memory buffer that the LLVM Source Manager is handling, we need to add
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000218 // a copy to the Clang source manager.
Chris Lattnerd6f19062010-04-08 00:23:06 +0000219 const llvm::SourceMgr &LSM = *D.getSourceMgr();
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000220
Chris Lattnerd6f19062010-04-08 00:23:06 +0000221 // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr
222 // already owns its one and clang::SourceManager wants to own its one.
223 const MemoryBuffer *LBuf =
224 LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc()));
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000225
Chris Lattnerd6f19062010-04-08 00:23:06 +0000226 // Create the copy and transfer ownership to clang::SourceManager.
227 llvm::MemoryBuffer *CBuf =
228 llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(),
229 LBuf->getBufferIdentifier());
230 FileID FID = CSM.createFileIDForMemBuffer(CBuf);
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000231
Chris Lattnerd6f19062010-04-08 00:23:06 +0000232 // Translate the offset into the file.
233 unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart();
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000234 SourceLocation NewLoc =
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000235 CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset);
Chris Lattnerd6f19062010-04-08 00:23:06 +0000236 return FullSourceLoc(NewLoc, CSM);
237}
238
Chris Lattnercabae682010-04-06 17:52:14 +0000239
Chris Lattner6da9eb62010-04-06 18:38:50 +0000240/// InlineAsmDiagHandler2 - This function is invoked when the backend hits an
241/// error parsing inline asm. The SMDiagnostic indicates the error relative to
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000242/// the temporary memory buffer that the inline asm parser has set up.
Chris Lattner6da9eb62010-04-06 18:38:50 +0000243void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D,
244 SourceLocation LocCookie) {
245 // There are a couple of different kinds of errors we could get here. First,
246 // we re-format the SMDiagnostic in terms of a clang diagnostic.
Argyrios Kyrtzidis5d3a4bb2012-02-01 06:36:49 +0000247
248 // Strip "error: " off the start of the message string.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000249 StringRef Message = D.getMessage();
Argyrios Kyrtzidis5d3a4bb2012-02-01 06:36:49 +0000250 if (Message.startswith("error: "))
251 Message = Message.substr(7);
Chris Lattner6da9eb62010-04-06 18:38:50 +0000252
Chris Lattner99e14a02010-06-15 00:03:12 +0000253 // If the SMDiagnostic has an inline asm source location, translate it.
Chris Lattner6da9eb62010-04-06 18:38:50 +0000254 FullSourceLoc Loc;
Chris Lattnerd6f19062010-04-08 00:23:06 +0000255 if (D.getLoc() != SMLoc())
256 Loc = ConvertBackendLocation(D, Context->getSourceManager());
Chris Lattnerff8f9ec2012-01-31 06:13:55 +0000257
Argyrios Kyrtzidis5d3a4bb2012-02-01 06:36:49 +0000258
Chris Lattner99e14a02010-06-15 00:03:12 +0000259 // If this problem has clang-level source location information, report the
260 // issue as being an error in the source with a note showing the instantiated
261 // code.
262 if (LocCookie.isValid()) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000263 Diags.Report(LocCookie, diag::err_fe_inline_asm).AddString(Message);
Chris Lattner99e14a02010-06-15 00:03:12 +0000264
Benjamin Kramer96fda0c2011-10-16 10:48:28 +0000265 if (D.getLoc().isValid()) {
266 DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here);
267 // Convert the SMDiagnostic ranges into SourceRange and attach them
268 // to the diagnostic.
269 for (unsigned i = 0, e = D.getRanges().size(); i != e; ++i) {
270 std::pair<unsigned, unsigned> Range = D.getRanges()[i];
271 unsigned Column = D.getColumnNo();
272 B << SourceRange(Loc.getLocWithOffset(Range.first - Column),
273 Loc.getLocWithOffset(Range.second - Column));
274 }
275 }
Chris Lattner99e14a02010-06-15 00:03:12 +0000276 return;
277 }
278
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000279 // Otherwise, report the backend error as occurring in the generated .s file.
Chris Lattner99e14a02010-06-15 00:03:12 +0000280 // If Loc is invalid, we still need to report the error, it just gets no
281 // location info.
282 Diags.Report(Loc, diag::err_fe_inline_asm).AddString(Message);
Chris Lattner6da9eb62010-04-06 18:38:50 +0000283}
284
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000285//
286
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000287CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext)
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +0000288 : Act(_Act), LinkModule(0),
289 VMContext(_VMContext ? _VMContext : new LLVMContext),
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000290 OwnsVMContext(!_VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000291
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000292CodeGenAction::~CodeGenAction() {
293 TheModule.reset();
294 if (OwnsVMContext)
295 delete VMContext;
296}
Daniel Dunbar9ad1c022010-02-25 20:37:44 +0000297
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000298bool CodeGenAction::hasIRSupport() const { return true; }
299
Daniel Dunbarb954e982010-02-25 04:37:50 +0000300void CodeGenAction::EndSourceFileAction() {
301 // If the consumer creation failed, do nothing.
302 if (!getCompilerInstance().hasASTConsumer())
303 return;
304
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +0000305 // If we were given a link module, release consumer's ownership of it.
306 if (LinkModule)
307 BEConsumer->takeLinkModule();
308
Daniel Dunbarb954e982010-02-25 04:37:50 +0000309 // Steal the module from the consumer.
Nico Weber5aa74af2011-01-25 20:34:14 +0000310 TheModule.reset(BEConsumer->takeModule());
Daniel Dunbarb954e982010-02-25 04:37:50 +0000311}
312
313llvm::Module *CodeGenAction::takeModule() {
314 return TheModule.take();
315}
316
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000317llvm::LLVMContext *CodeGenAction::takeLLVMContext() {
318 OwnsVMContext = false;
319 return VMContext;
320}
321
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000322static raw_ostream *GetOutputStream(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000323 StringRef InFile,
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000324 BackendAction Action) {
325 switch (Action) {
326 case Backend_EmitAssembly:
327 return CI.createDefaultOutputFile(false, InFile, "s");
328 case Backend_EmitLL:
329 return CI.createDefaultOutputFile(false, InFile, "ll");
330 case Backend_EmitBC:
331 return CI.createDefaultOutputFile(true, InFile, "bc");
332 case Backend_EmitNothing:
333 return 0;
334 case Backend_EmitMCNull:
335 case Backend_EmitObj:
336 return CI.createDefaultOutputFile(true, InFile, "o");
337 }
338
David Blaikieb219cfc2011-09-23 05:06:16 +0000339 llvm_unreachable("Invalid action!");
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000340}
341
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000342ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000343 StringRef InFile) {
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000344 BackendAction BA = static_cast<BackendAction>(Act);
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000345 OwningPtr<raw_ostream> OS(GetOutputStream(CI, InFile, BA));
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000346 if (BA != Backend_EmitNothing && !OS)
347 return 0;
348
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +0000349 llvm::Module *LinkModuleToUse = LinkModule;
350
351 // If we were not given a link module, and the user requested that one be
352 // loaded from bitcode, do so now.
353 const std::string &LinkBCFile = CI.getCodeGenOpts().LinkBitcodeFile;
354 if (!LinkModuleToUse && !LinkBCFile.empty()) {
355 std::string ErrorStr;
356
357 llvm::MemoryBuffer *BCBuf =
358 CI.getFileManager().getBufferForFile(LinkBCFile, &ErrorStr);
359 if (!BCBuf) {
360 CI.getDiagnostics().Report(diag::err_cannot_open_file)
361 << LinkBCFile << ErrorStr;
362 return 0;
363 }
364
365 LinkModuleToUse = getLazyBitcodeModule(BCBuf, *VMContext, &ErrorStr);
366 if (!LinkModuleToUse) {
367 CI.getDiagnostics().Report(diag::err_cannot_open_file)
368 << LinkBCFile << ErrorStr;
369 return 0;
370 }
371 }
372
Nico Weber5aa74af2011-01-25 20:34:14 +0000373 BEConsumer =
374 new BackendConsumer(BA, CI.getDiagnostics(),
375 CI.getCodeGenOpts(), CI.getTargetOpts(),
Dan Gohmanb18b8ad2011-07-05 22:02:36 +0000376 CI.getLangOpts(),
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +0000377 CI.getFrontendOpts().ShowTimers, InFile,
378 LinkModuleToUse, OS.take(), *VMContext);
Nico Weber5aa74af2011-01-25 20:34:14 +0000379 return BEConsumer;
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000380}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000381
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000382void CodeGenAction::ExecuteAction() {
383 // If this is an IR file, we have to treat it specially.
384 if (getCurrentFileKind() == IK_LLVM_IR) {
385 BackendAction BA = static_cast<BackendAction>(Act);
386 CompilerInstance &CI = getCompilerInstance();
387 raw_ostream *OS = GetOutputStream(CI, getCurrentFile(), BA);
388 if (BA != Backend_EmitNothing && !OS)
389 return;
390
391 bool Invalid;
392 SourceManager &SM = CI.getSourceManager();
393 const llvm::MemoryBuffer *MainFile = SM.getBuffer(SM.getMainFileID(),
394 &Invalid);
395 if (Invalid)
396 return;
397
398 // FIXME: This is stupid, IRReader shouldn't take ownership.
399 llvm::MemoryBuffer *MainFileCopy =
400 llvm::MemoryBuffer::getMemBufferCopy(MainFile->getBuffer(),
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000401 getCurrentFile());
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000402
403 llvm::SMDiagnostic Err;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000404 TheModule.reset(ParseIR(MainFileCopy, Err, *VMContext));
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000405 if (!TheModule) {
406 // Translate from the diagnostic info to the SourceManager location.
Argyrios Kyrtzidis5a9ee202011-09-19 20:40:38 +0000407 SourceLocation Loc = SM.translateFileLineCol(
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000408 SM.getFileEntryForID(SM.getMainFileID()), Err.getLineNo(),
409 Err.getColumnNo() + 1);
410
411 // Get a custom diagnostic for the error. We strip off a leading
412 // diagnostic code if there is one.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000413 StringRef Msg = Err.getMessage();
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000414 if (Msg.startswith("error: "))
415 Msg = Msg.substr(7);
Benjamin Kramerc9b47f92012-03-16 22:31:42 +0000416
417 // Escape '%', which is interpreted as a format character.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000418 SmallString<128> EscapedMessage;
Benjamin Kramerc9b47f92012-03-16 22:31:42 +0000419 for (unsigned i = 0, e = Msg.size(); i != e; ++i) {
420 if (Msg[i] == '%')
421 EscapedMessage += '%';
422 EscapedMessage += Msg[i];
423 }
424
David Blaikied6471f72011-09-25 23:23:43 +0000425 unsigned DiagID = CI.getDiagnostics().getCustomDiagID(
Benjamin Kramerc9b47f92012-03-16 22:31:42 +0000426 DiagnosticsEngine::Error, EscapedMessage);
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000427
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000428 CI.getDiagnostics().Report(Loc, DiagID);
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000429 return;
430 }
431
432 EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(),
Dan Gohmanb18b8ad2011-07-05 22:02:36 +0000433 CI.getTargetOpts(), CI.getLangOpts(),
434 TheModule.get(),
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000435 BA, OS);
436 return;
437 }
438
439 // Otherwise follow the normal AST path.
440 this->ASTFrontendAction::ExecuteAction();
441}
442
443//
444
David Blaikie99ba9e32011-12-20 02:48:34 +0000445void EmitAssemblyAction::anchor() { }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000446EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext)
447 : CodeGenAction(Backend_EmitAssembly, _VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000448
David Blaikie99ba9e32011-12-20 02:48:34 +0000449void EmitBCAction::anchor() { }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000450EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext)
451 : CodeGenAction(Backend_EmitBC, _VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000452
David Blaikie99ba9e32011-12-20 02:48:34 +0000453void EmitLLVMAction::anchor() { }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000454EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext)
455 : CodeGenAction(Backend_EmitLL, _VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000456
David Blaikie99ba9e32011-12-20 02:48:34 +0000457void EmitLLVMOnlyAction::anchor() { }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000458EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext)
459 : CodeGenAction(Backend_EmitNothing, _VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000460
David Blaikie99ba9e32011-12-20 02:48:34 +0000461void EmitCodeGenOnlyAction::anchor() { }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000462EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext)
463 : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
Daniel Dunbar32148ce2010-05-25 18:41:01 +0000464
David Blaikie99ba9e32011-12-20 02:48:34 +0000465void EmitObjAction::anchor() { }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000466EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext)
467 : CodeGenAction(Backend_EmitObj, _VMContext) {}