blob: 9d6d183d97d91b391c5e1a005e56fd2d11e8cf2a [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"
Peter Collingbournef1d76db2011-10-30 17:30:44 +000011#include "clang/Basic/FileManager.h"
Chris Lattner5ec32e72010-04-06 18:38:50 +000012#include "clang/Basic/SourceManager.h"
13#include "clang/Basic/TargetInfo.h"
Chris Lattner5bbb3c82009-03-29 16:50:03 +000014#include "clang/AST/ASTConsumer.h"
Daniel Dunbarb9bbd542009-11-15 06:48:46 +000015#include "clang/AST/ASTContext.h"
Chris Lattner5bbb3c82009-03-29 16:50:03 +000016#include "clang/AST/DeclGroup.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"
Chris Lattner6d672132010-04-06 17:52:14 +000021#include "llvm/LLVMContext.h"
Peter Collingbournef1d76db2011-10-30 17:30:44 +000022#include "llvm/Linker.h"
Daniel Dunbarc13935e2008-10-21 23:49:24 +000023#include "llvm/Module.h"
Daniel Dunbar3e111522010-06-07 23:21:04 +000024#include "llvm/Pass.h"
Daniel Dunbarc13935e2008-10-21 23:49:24 +000025#include "llvm/ADT/OwningPtr.h"
Benjamin Kramer778b8b82012-03-16 22:31:42 +000026#include "llvm/ADT/SmallString.h"
Peter Collingbournef1d76db2011-10-30 17:30:44 +000027#include "llvm/Bitcode/ReaderWriter.h"
Daniel Dunbar6f8362c2010-06-07 23:27:59 +000028#include "llvm/Support/IRReader.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,
56 const LangOptions &langopts,
57 bool TimePasses,
Peter Collingbournef1d76db2011-10-30 17:30:44 +000058 const std::string &infile,
59 llvm::Module *LinkModule,
60 raw_ostream *OS,
Chris Lattner6d672132010-04-06 17:52:14 +000061 LLVMContext &C) :
Daniel Dunbaracadc552009-12-03 09:12:54 +000062 Diags(_Diags),
Mike Stump11289f42009-09-09 15:08:12 +000063 Action(action),
Chandler Carruthbc55fe22009-11-12 17:24:48 +000064 CodeGenOpts(compopts),
Daniel Dunbarb9bbd542009-11-15 06:48:46 +000065 TargetOpts(targetopts),
Dan Gohmanfec0ff82011-07-05 22:02:36 +000066 LangOpts(langopts),
Chris Lattner15681772009-07-14 20:39:15 +000067 AsmOutStream(OS),
Douglas Gregord6125662012-11-05 23:58:27 +000068 Context(),
Chris Lattner263d64c2009-02-18 01:37:30 +000069 LLVMIRGeneration("LLVM IR Generation Time"),
Peter Collingbournef1d76db2011-10-30 17:30:44 +000070 Gen(CreateLLVMCodeGen(Diags, infile, compopts, C)),
Douglas Gregord6125662012-11-05 23:58:27 +000071 LinkModule(LinkModule)
72 {
Daniel Dunbar8e705052009-11-30 08:39:52 +000073 llvm::TimePassesIsEnabled = TimePasses;
Chris Lattnerdeffa132009-02-18 01:23:44 +000074 }
Daniel Dunbarc13935e2008-10-21 23:49:24 +000075
Daniel Dunbar400a6932010-02-25 04:37:50 +000076 llvm::Module *takeModule() { return TheModule.take(); }
Peter Collingbournef1d76db2011-10-30 17:30:44 +000077 llvm::Module *takeLinkModule() { return LinkModule.take(); }
Daniel Dunbar400a6932010-02-25 04:37:50 +000078
Rafael Espindoladf88f6f2012-03-08 15:51:03 +000079 virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
80 Gen->HandleCXXStaticMemberVarInstantiation(VD);
Rafael Espindola189fa742012-03-05 10:54:55 +000081 }
82
Chris Lattner5cf49fe2009-03-28 02:18:25 +000083 virtual void Initialize(ASTContext &Ctx) {
84 Context = &Ctx;
Mike Stump11289f42009-09-09 15:08:12 +000085
Daniel Dunbar8e705052009-11-30 08:39:52 +000086 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +000087 LLVMIRGeneration.startTimer();
Mike Stump11289f42009-09-09 15:08:12 +000088
Chris Lattner5cf49fe2009-03-28 02:18:25 +000089 Gen->Initialize(Ctx);
Daniel Dunbarc13935e2008-10-21 23:49:24 +000090
Daniel Dunbar400a6932010-02-25 04:37:50 +000091 TheModule.reset(Gen->GetModule());
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.stopTimer();
Daniel Dunbarc13935e2008-10-21 23:49:24 +000095 }
Mike Stump11289f42009-09-09 15:08:12 +000096
Argyrios Kyrtzidis841dd882011-11-18 00:26:59 +000097 virtual bool HandleTopLevelDecl(DeclGroupRef D) {
Chris Lattner5bbb3c82009-03-29 16:50:03 +000098 PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
Chris Lattnereae6cb62009-03-05 08:00:35 +000099 Context->getSourceManager(),
100 "LLVM IR generation of declaration");
Mike Stump11289f42009-09-09 15:08:12 +0000101
Daniel Dunbar8e705052009-11-30 08:39:52 +0000102 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +0000103 LLVMIRGeneration.startTimer();
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000104
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000105 Gen->HandleTopLevelDecl(D);
Chris Lattner263d64c2009-02-18 01:37:30 +0000106
Daniel Dunbar8e705052009-11-30 08:39:52 +0000107 if (llvm::TimePassesIsEnabled)
Chris Lattner263d64c2009-02-18 01:37:30 +0000108 LLVMIRGeneration.stopTimer();
Argyrios Kyrtzidis841dd882011-11-18 00:26:59 +0000109
110 return true;
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000111 }
Mike Stump11289f42009-09-09 15:08:12 +0000112
Chris Lattnercf169832009-03-28 04:11:33 +0000113 virtual void HandleTranslationUnit(ASTContext &C) {
Chris Lattnereae6cb62009-03-05 08:00:35 +0000114 {
Chris Lattnere46de752009-03-06 06:46:31 +0000115 PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
Daniel Dunbar8e705052009-11-30 08:39:52 +0000116 if (llvm::TimePassesIsEnabled)
Chris Lattnereae6cb62009-03-05 08:00:35 +0000117 LLVMIRGeneration.startTimer();
Chris Lattner263d64c2009-02-18 01:37:30 +0000118
Chris Lattnercf169832009-03-28 04:11:33 +0000119 Gen->HandleTranslationUnit(C);
Daniel Dunbara94d8732008-11-11 06:35:39 +0000120
Daniel Dunbar8e705052009-11-30 08:39:52 +0000121 if (llvm::TimePassesIsEnabled)
Chris Lattnereae6cb62009-03-05 08:00:35 +0000122 LLVMIRGeneration.stopTimer();
123 }
Chris Lattner263d64c2009-02-18 01:37:30 +0000124
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000125 // Silently ignore if we weren't initialized for some reason.
Daniel Dunbar3e111522010-06-07 23:21:04 +0000126 if (!TheModule)
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000127 return;
Mike Stump11289f42009-09-09 15:08:12 +0000128
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000129 // Make sure IR generation is happy with the module. This is released by
130 // the module provider.
Douglas Gregorde3ef502011-11-30 23:21:26 +0000131 llvm::Module *M = Gen->ReleaseModule();
Daniel Dunbarf976d1b2010-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 Collingbournef1d76db2011-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 Dunbarf976d1b2010-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 Lattner068f2ab2010-11-17 08:13:04 +0000156 LLVMContext::InlineAsmDiagHandlerTy OldHandler =
157 Ctx.getInlineAsmDiagnosticHandler();
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000158 void *OldContext = Ctx.getInlineAsmDiagnosticContext();
Chris Lattner068f2ab2010-11-17 08:13:04 +0000159 Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this);
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000160
Dan Gohmanfec0ff82011-07-05 22:02:36 +0000161 EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
Daniel Dunbar3e111522010-06-07 23:21:04 +0000162 TheModule.get(), Action, AsmOutStream);
Daniel Dunbarf976d1b2010-06-07 23:20:08 +0000163
164 Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext);
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000165 }
Mike Stump11289f42009-09-09 15:08:12 +0000166
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000167 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattnereae6cb62009-03-05 08:00:35 +0000168 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
169 Context->getSourceManager(),
170 "LLVM IR generation of declaration");
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000171 Gen->HandleTagDeclDefinition(D);
172 }
Douglas Gregorbeecd582009-04-21 17:11:58 +0000173
174 virtual void CompleteTentativeDefinition(VarDecl *D) {
175 Gen->CompleteTentativeDefinition(D);
176 }
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000177
Douglas Gregor88d292c2010-05-13 16:44:06 +0000178 virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
179 Gen->HandleVTable(RD, DefinitionRequired);
180 }
181
Chris Lattner5ec32e72010-04-06 18:38:50 +0000182 static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context,
183 unsigned LocCookie) {
184 SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie);
185 ((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc);
186 }
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000187
Chris Lattner5ec32e72010-04-06 18:38:50 +0000188 void InlineAsmDiagHandler2(const llvm::SMDiagnostic &,
189 SourceLocation LocCookie);
Mike Stump11289f42009-09-09 15:08:12 +0000190 };
David Blaikie68e081d2011-12-20 02:48:34 +0000191
192 void BackendConsumer::anchor() {}
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000193}
194
Chris Lattner79f67a72010-04-08 00:23:06 +0000195/// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr
196/// buffer to be a valid FullSourceLoc.
197static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D,
198 SourceManager &CSM) {
199 // Get both the clang and llvm source managers. The location is relative to
200 // a memory buffer that the LLVM Source Manager is handling, we need to add
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000201 // a copy to the Clang source manager.
Chris Lattner79f67a72010-04-08 00:23:06 +0000202 const llvm::SourceMgr &LSM = *D.getSourceMgr();
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000203
Chris Lattner79f67a72010-04-08 00:23:06 +0000204 // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr
205 // already owns its one and clang::SourceManager wants to own its one.
206 const MemoryBuffer *LBuf =
207 LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc()));
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000208
Chris Lattner79f67a72010-04-08 00:23:06 +0000209 // Create the copy and transfer ownership to clang::SourceManager.
210 llvm::MemoryBuffer *CBuf =
211 llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(),
212 LBuf->getBufferIdentifier());
213 FileID FID = CSM.createFileIDForMemBuffer(CBuf);
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000214
Chris Lattner79f67a72010-04-08 00:23:06 +0000215 // Translate the offset into the file.
216 unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart();
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000217 SourceLocation NewLoc =
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000218 CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset);
Chris Lattner79f67a72010-04-08 00:23:06 +0000219 return FullSourceLoc(NewLoc, CSM);
220}
221
Chris Lattner6d672132010-04-06 17:52:14 +0000222
Chris Lattner5ec32e72010-04-06 18:38:50 +0000223/// InlineAsmDiagHandler2 - This function is invoked when the backend hits an
224/// error parsing inline asm. The SMDiagnostic indicates the error relative to
Daniel Dunbar50aa0a52010-04-29 16:29:09 +0000225/// the temporary memory buffer that the inline asm parser has set up.
Chris Lattner5ec32e72010-04-06 18:38:50 +0000226void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D,
227 SourceLocation LocCookie) {
228 // There are a couple of different kinds of errors we could get here. First,
229 // we re-format the SMDiagnostic in terms of a clang diagnostic.
Argyrios Kyrtzidisa11b35a2012-02-01 06:36:49 +0000230
231 // Strip "error: " off the start of the message string.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000232 StringRef Message = D.getMessage();
Argyrios Kyrtzidisa11b35a2012-02-01 06:36:49 +0000233 if (Message.startswith("error: "))
234 Message = Message.substr(7);
Chris Lattner5ec32e72010-04-06 18:38:50 +0000235
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000236 // If the SMDiagnostic has an inline asm source location, translate it.
Chris Lattner5ec32e72010-04-06 18:38:50 +0000237 FullSourceLoc Loc;
Chris Lattner79f67a72010-04-08 00:23:06 +0000238 if (D.getLoc() != SMLoc())
239 Loc = ConvertBackendLocation(D, Context->getSourceManager());
Chris Lattnerf4a4bec2012-01-31 06:13:55 +0000240
Argyrios Kyrtzidisa11b35a2012-02-01 06:36:49 +0000241
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000242 // If this problem has clang-level source location information, report the
243 // issue as being an error in the source with a note showing the instantiated
244 // code.
245 if (LocCookie.isValid()) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000246 Diags.Report(LocCookie, diag::err_fe_inline_asm).AddString(Message);
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000247
Benjamin Kramere06b2b72011-10-16 10:48:28 +0000248 if (D.getLoc().isValid()) {
249 DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here);
250 // Convert the SMDiagnostic ranges into SourceRange and attach them
251 // to the diagnostic.
252 for (unsigned i = 0, e = D.getRanges().size(); i != e; ++i) {
253 std::pair<unsigned, unsigned> Range = D.getRanges()[i];
254 unsigned Column = D.getColumnNo();
255 B << SourceRange(Loc.getLocWithOffset(Range.first - Column),
256 Loc.getLocWithOffset(Range.second - Column));
257 }
258 }
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000259 return;
260 }
261
Chris Lattner57540c52011-04-15 05:22:18 +0000262 // Otherwise, report the backend error as occurring in the generated .s file.
Chris Lattnerc7ed7ea2010-06-15 00:03:12 +0000263 // If Loc is invalid, we still need to report the error, it just gets no
264 // location info.
265 Diags.Report(Loc, diag::err_fe_inline_asm).AddString(Message);
Chris Lattner5ec32e72010-04-06 18:38:50 +0000266}
267
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000268//
269
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000270CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext)
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000271 : Act(_Act), LinkModule(0),
272 VMContext(_VMContext ? _VMContext : new LLVMContext),
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000273 OwnsVMContext(!_VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000274
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000275CodeGenAction::~CodeGenAction() {
276 TheModule.reset();
277 if (OwnsVMContext)
278 delete VMContext;
279}
Daniel Dunbare8ecf9a2010-02-25 20:37:44 +0000280
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000281bool CodeGenAction::hasIRSupport() const { return true; }
282
Daniel Dunbar400a6932010-02-25 04:37:50 +0000283void CodeGenAction::EndSourceFileAction() {
284 // If the consumer creation failed, do nothing.
285 if (!getCompilerInstance().hasASTConsumer())
286 return;
287
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000288 // If we were given a link module, release consumer's ownership of it.
289 if (LinkModule)
290 BEConsumer->takeLinkModule();
291
Daniel Dunbar400a6932010-02-25 04:37:50 +0000292 // Steal the module from the consumer.
Nico Weber2992efa2011-01-25 20:34:14 +0000293 TheModule.reset(BEConsumer->takeModule());
Daniel Dunbar400a6932010-02-25 04:37:50 +0000294}
295
296llvm::Module *CodeGenAction::takeModule() {
297 return TheModule.take();
298}
299
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000300llvm::LLVMContext *CodeGenAction::takeLLVMContext() {
301 OwnsVMContext = false;
302 return VMContext;
303}
304
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000305static raw_ostream *GetOutputStream(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000306 StringRef InFile,
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000307 BackendAction Action) {
308 switch (Action) {
309 case Backend_EmitAssembly:
310 return CI.createDefaultOutputFile(false, InFile, "s");
311 case Backend_EmitLL:
312 return CI.createDefaultOutputFile(false, InFile, "ll");
313 case Backend_EmitBC:
314 return CI.createDefaultOutputFile(true, InFile, "bc");
315 case Backend_EmitNothing:
316 return 0;
317 case Backend_EmitMCNull:
318 case Backend_EmitObj:
319 return CI.createDefaultOutputFile(true, InFile, "o");
320 }
321
David Blaikie83d382b2011-09-23 05:06:16 +0000322 llvm_unreachable("Invalid action!");
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000323}
324
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000325ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000326 StringRef InFile) {
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000327 BackendAction BA = static_cast<BackendAction>(Act);
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000328 OwningPtr<raw_ostream> OS(GetOutputStream(CI, InFile, BA));
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000329 if (BA != Backend_EmitNothing && !OS)
330 return 0;
331
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000332 llvm::Module *LinkModuleToUse = LinkModule;
333
334 // If we were not given a link module, and the user requested that one be
335 // loaded from bitcode, do so now.
336 const std::string &LinkBCFile = CI.getCodeGenOpts().LinkBitcodeFile;
337 if (!LinkModuleToUse && !LinkBCFile.empty()) {
338 std::string ErrorStr;
339
340 llvm::MemoryBuffer *BCBuf =
341 CI.getFileManager().getBufferForFile(LinkBCFile, &ErrorStr);
342 if (!BCBuf) {
343 CI.getDiagnostics().Report(diag::err_cannot_open_file)
344 << LinkBCFile << ErrorStr;
345 return 0;
346 }
347
348 LinkModuleToUse = getLazyBitcodeModule(BCBuf, *VMContext, &ErrorStr);
349 if (!LinkModuleToUse) {
350 CI.getDiagnostics().Report(diag::err_cannot_open_file)
351 << LinkBCFile << ErrorStr;
352 return 0;
353 }
354 }
355
Nico Weber2992efa2011-01-25 20:34:14 +0000356 BEConsumer =
357 new BackendConsumer(BA, CI.getDiagnostics(),
358 CI.getCodeGenOpts(), CI.getTargetOpts(),
Dan Gohmanfec0ff82011-07-05 22:02:36 +0000359 CI.getLangOpts(),
Peter Collingbournef1d76db2011-10-30 17:30:44 +0000360 CI.getFrontendOpts().ShowTimers, InFile,
361 LinkModuleToUse, OS.take(), *VMContext);
Nico Weber2992efa2011-01-25 20:34:14 +0000362 return BEConsumer;
Daniel Dunbarc13935e2008-10-21 23:49:24 +0000363}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000364
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000365void CodeGenAction::ExecuteAction() {
366 // If this is an IR file, we have to treat it specially.
367 if (getCurrentFileKind() == IK_LLVM_IR) {
368 BackendAction BA = static_cast<BackendAction>(Act);
369 CompilerInstance &CI = getCompilerInstance();
370 raw_ostream *OS = GetOutputStream(CI, getCurrentFile(), BA);
371 if (BA != Backend_EmitNothing && !OS)
372 return;
373
374 bool Invalid;
375 SourceManager &SM = CI.getSourceManager();
376 const llvm::MemoryBuffer *MainFile = SM.getBuffer(SM.getMainFileID(),
377 &Invalid);
378 if (Invalid)
379 return;
380
381 // FIXME: This is stupid, IRReader shouldn't take ownership.
382 llvm::MemoryBuffer *MainFileCopy =
383 llvm::MemoryBuffer::getMemBufferCopy(MainFile->getBuffer(),
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000384 getCurrentFile());
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000385
386 llvm::SMDiagnostic Err;
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000387 TheModule.reset(ParseIR(MainFileCopy, Err, *VMContext));
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000388 if (!TheModule) {
389 // Translate from the diagnostic info to the SourceManager location.
Argyrios Kyrtzidis27bf76d2011-09-19 20:40:38 +0000390 SourceLocation Loc = SM.translateFileLineCol(
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000391 SM.getFileEntryForID(SM.getMainFileID()), Err.getLineNo(),
392 Err.getColumnNo() + 1);
393
394 // Get a custom diagnostic for the error. We strip off a leading
395 // diagnostic code if there is one.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000396 StringRef Msg = Err.getMessage();
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000397 if (Msg.startswith("error: "))
398 Msg = Msg.substr(7);
Benjamin Kramer778b8b82012-03-16 22:31:42 +0000399
400 // Escape '%', which is interpreted as a format character.
401 llvm::SmallString<128> EscapedMessage;
402 for (unsigned i = 0, e = Msg.size(); i != e; ++i) {
403 if (Msg[i] == '%')
404 EscapedMessage += '%';
405 EscapedMessage += Msg[i];
406 }
407
David Blaikie9c902b52011-09-25 23:23:43 +0000408 unsigned DiagID = CI.getDiagnostics().getCustomDiagID(
Benjamin Kramer778b8b82012-03-16 22:31:42 +0000409 DiagnosticsEngine::Error, EscapedMessage);
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000410
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000411 CI.getDiagnostics().Report(Loc, DiagID);
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000412 return;
413 }
414
415 EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(),
Dan Gohmanfec0ff82011-07-05 22:02:36 +0000416 CI.getTargetOpts(), CI.getLangOpts(),
417 TheModule.get(),
Daniel Dunbar6f8362c2010-06-07 23:27:59 +0000418 BA, OS);
419 return;
420 }
421
422 // Otherwise follow the normal AST path.
423 this->ASTFrontendAction::ExecuteAction();
424}
425
426//
427
David Blaikie68e081d2011-12-20 02:48:34 +0000428void EmitAssemblyAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000429EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext)
430 : CodeGenAction(Backend_EmitAssembly, _VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000431
David Blaikie68e081d2011-12-20 02:48:34 +0000432void EmitBCAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000433EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext)
434 : CodeGenAction(Backend_EmitBC, _VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000435
David Blaikie68e081d2011-12-20 02:48:34 +0000436void EmitLLVMAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000437EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext)
438 : CodeGenAction(Backend_EmitLL, _VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000439
David Blaikie68e081d2011-12-20 02:48:34 +0000440void EmitLLVMOnlyAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000441EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext)
442 : CodeGenAction(Backend_EmitNothing, _VMContext) {}
Daniel Dunbarcea0c702010-02-25 04:37:45 +0000443
David Blaikie68e081d2011-12-20 02:48:34 +0000444void EmitCodeGenOnlyAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000445EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext)
446 : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
Daniel Dunbar4c77a642010-05-25 18:41:01 +0000447
David Blaikie68e081d2011-12-20 02:48:34 +0000448void EmitObjAction::anchor() { }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000449EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext)
450 : CodeGenAction(Backend_EmitObj, _VMContext) {}