blob: 02e08602922ad31ab89ebf9a7131afe19f2583f6 [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"
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +000011#include "clang/Basic/FileManager.h"
Chris Lattner6da9eb62010-04-06 18:38:50 +000012#include "clang/Basic/SourceManager.h"
13#include "clang/Basic/TargetInfo.h"
Chris Lattner682bf922009-03-29 16:50:03 +000014#include "clang/AST/ASTConsumer.h"
Daniel Dunbard58c03f2009-11-15 06:48:46 +000015#include "clang/AST/ASTContext.h"
Chris Lattner682bf922009-03-29 16:50:03 +000016#include "clang/AST/DeclGroup.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"
Chris Lattnercabae682010-04-06 17:52:14 +000021#include "llvm/LLVMContext.h"
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +000022#include "llvm/Linker.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000023#include "llvm/Module.h"
Daniel Dunbar05a7f3d2010-06-07 23:21:04 +000024#include "llvm/Pass.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000025#include "llvm/ADT/OwningPtr.h"
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +000026#include "llvm/Bitcode/ReaderWriter.h"
Daniel Dunbar4cbbd942010-06-07 23:27:59 +000027#include "llvm/Support/IRReader.h"
Chris Lattner6da9eb62010-04-06 18:38:50 +000028#include "llvm/Support/MemoryBuffer.h"
29#include "llvm/Support/SourceMgr.h"
Chris Lattner6f114eb2009-02-18 01:37:30 +000030#include "llvm/Support/Timer.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000031using namespace clang;
32using namespace llvm;
33
Nico Weber5aa74af2011-01-25 20:34:14 +000034namespace clang {
Benjamin Kramerbd218282009-11-28 10:07:24 +000035 class BackendConsumer : public ASTConsumer {
David Blaikie99ba9e32011-12-20 02:48:34 +000036 virtual void anchor();
David Blaikied6471f72011-09-25 23:23:43 +000037 DiagnosticsEngine &Diags;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000038 BackendAction Action;
Daniel Dunbar3636e1d2009-11-30 08:39:32 +000039 const CodeGenOptions &CodeGenOpts;
Daniel Dunbar3636e1d2009-11-30 08:39:32 +000040 const TargetOptions &TargetOpts;
Dan Gohmanb18b8ad2011-07-05 22:02:36 +000041 const LangOptions &LangOpts;
Chris Lattner5f9e2722011-07-23 10:55:15 +000042 raw_ostream *AsmOutStream;
Chris Lattner49f28ca2009-03-05 08:00:35 +000043 ASTContext *Context;
Daniel Dunbar90f41302008-10-29 08:50:02 +000044
Chris Lattner6f114eb2009-02-18 01:37:30 +000045 Timer LLVMIRGeneration;
Mike Stump1eb44332009-09-09 15:08:12 +000046
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000047 OwningPtr<CodeGenerator> Gen;
Mike Stump1eb44332009-09-09 15:08:12 +000048
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000049 OwningPtr<llvm::Module> TheModule, LinkModule;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000050
Mike Stump1eb44332009-09-09 15:08:12 +000051 public:
David Blaikied6471f72011-09-25 23:23:43 +000052 BackendConsumer(BackendAction action, DiagnosticsEngine &_Diags,
Daniel Dunbar6b0cf672010-06-07 23:19:17 +000053 const CodeGenOptions &compopts,
Dan Gohmanb18b8ad2011-07-05 22:02:36 +000054 const TargetOptions &targetopts,
55 const LangOptions &langopts,
56 bool TimePasses,
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +000057 const std::string &infile,
58 llvm::Module *LinkModule,
59 raw_ostream *OS,
Chris Lattnercabae682010-04-06 17:52:14 +000060 LLVMContext &C) :
Daniel Dunbar3be0d192009-12-03 09:12:54 +000061 Diags(_Diags),
Mike Stump1eb44332009-09-09 15:08:12 +000062 Action(action),
Chandler Carruth2811ccf2009-11-12 17:24:48 +000063 CodeGenOpts(compopts),
Daniel Dunbard58c03f2009-11-15 06:48:46 +000064 TargetOpts(targetopts),
Dan Gohmanb18b8ad2011-07-05 22:02:36 +000065 LangOpts(langopts),
Chris Lattner03eacc72009-07-14 20:39:15 +000066 AsmOutStream(OS),
Chris Lattner6f114eb2009-02-18 01:37:30 +000067 LLVMIRGeneration("LLVM IR Generation Time"),
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +000068 Gen(CreateLLVMCodeGen(Diags, infile, compopts, C)),
69 LinkModule(LinkModule) {
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +000070 llvm::TimePassesIsEnabled = TimePasses;
Chris Lattner44502662009-02-18 01:23:44 +000071 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +000072
Daniel Dunbarb954e982010-02-25 04:37:50 +000073 llvm::Module *takeModule() { return TheModule.take(); }
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +000074 llvm::Module *takeLinkModule() { return LinkModule.take(); }
Daniel Dunbarb954e982010-02-25 04:37:50 +000075
Chris Lattner7bb0da02009-03-28 02:18:25 +000076 virtual void Initialize(ASTContext &Ctx) {
77 Context = &Ctx;
Mike Stump1eb44332009-09-09 15:08:12 +000078
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +000079 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +000080 LLVMIRGeneration.startTimer();
Mike Stump1eb44332009-09-09 15:08:12 +000081
Chris Lattner7bb0da02009-03-28 02:18:25 +000082 Gen->Initialize(Ctx);
Daniel Dunbard69bacc2008-10-21 23:49:24 +000083
Daniel Dunbarb954e982010-02-25 04:37:50 +000084 TheModule.reset(Gen->GetModule());
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.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +000088 }
Mike Stump1eb44332009-09-09 15:08:12 +000089
Argyrios Kyrtzidis88c25962011-11-18 00:26:59 +000090 virtual bool HandleTopLevelDecl(DeclGroupRef D) {
Chris Lattner682bf922009-03-29 16:50:03 +000091 PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
Chris Lattner49f28ca2009-03-05 08:00:35 +000092 Context->getSourceManager(),
93 "LLVM IR generation of declaration");
Mike Stump1eb44332009-09-09 15:08:12 +000094
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +000095 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +000096 LLVMIRGeneration.startTimer();
Chris Lattner682bf922009-03-29 16:50:03 +000097
Daniel Dunbard69bacc2008-10-21 23:49:24 +000098 Gen->HandleTopLevelDecl(D);
Chris Lattner6f114eb2009-02-18 01:37:30 +000099
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000100 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +0000101 LLVMIRGeneration.stopTimer();
Argyrios Kyrtzidis88c25962011-11-18 00:26:59 +0000102
103 return true;
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000104 }
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000106 virtual void HandleTranslationUnit(ASTContext &C) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000107 {
Chris Lattner14f234e2009-03-06 06:46:31 +0000108 PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000109 if (llvm::TimePassesIsEnabled)
Chris Lattner49f28ca2009-03-05 08:00:35 +0000110 LLVMIRGeneration.startTimer();
Chris Lattner6f114eb2009-02-18 01:37:30 +0000111
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000112 Gen->HandleTranslationUnit(C);
Daniel Dunbard68ba0e2008-11-11 06:35:39 +0000113
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000114 if (llvm::TimePassesIsEnabled)
Chris Lattner49f28ca2009-03-05 08:00:35 +0000115 LLVMIRGeneration.stopTimer();
116 }
Chris Lattner6f114eb2009-02-18 01:37:30 +0000117
Daniel Dunbar897c6762010-06-07 23:20:08 +0000118 // Silently ignore if we weren't initialized for some reason.
Daniel Dunbar05a7f3d2010-06-07 23:21:04 +0000119 if (!TheModule)
Daniel Dunbar897c6762010-06-07 23:20:08 +0000120 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Daniel Dunbar897c6762010-06-07 23:20:08 +0000122 // Make sure IR generation is happy with the module. This is released by
123 // the module provider.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000124 llvm::Module *M = Gen->ReleaseModule();
Daniel Dunbar897c6762010-06-07 23:20:08 +0000125 if (!M) {
126 // The module has been released by IR gen on failures, do not double
127 // free.
128 TheModule.take();
129 return;
130 }
131
132 assert(TheModule.get() == M &&
133 "Unexpected module change during IR generation");
134
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +0000135 // Link LinkModule into this module if present, preserving its validity.
136 if (LinkModule) {
137 std::string ErrorMsg;
138 if (Linker::LinkModules(M, LinkModule.get(), Linker::PreserveSource,
139 &ErrorMsg)) {
140 Diags.Report(diag::err_fe_cannot_link_module)
141 << LinkModule->getModuleIdentifier() << ErrorMsg;
142 return;
143 }
144 }
145
Daniel Dunbar897c6762010-06-07 23:20:08 +0000146 // Install an inline asm handler so that diagnostics get printed through
147 // our diagnostics hooks.
148 LLVMContext &Ctx = TheModule->getContext();
Chris Lattner063e4762010-11-17 08:13:04 +0000149 LLVMContext::InlineAsmDiagHandlerTy OldHandler =
150 Ctx.getInlineAsmDiagnosticHandler();
Daniel Dunbar897c6762010-06-07 23:20:08 +0000151 void *OldContext = Ctx.getInlineAsmDiagnosticContext();
Chris Lattner063e4762010-11-17 08:13:04 +0000152 Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this);
Daniel Dunbar897c6762010-06-07 23:20:08 +0000153
Dan Gohmanb18b8ad2011-07-05 22:02:36 +0000154 EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
Daniel Dunbar05a7f3d2010-06-07 23:21:04 +0000155 TheModule.get(), Action, AsmOutStream);
Daniel Dunbar897c6762010-06-07 23:20:08 +0000156
157 Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000158 }
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000160 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000161 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
162 Context->getSourceManager(),
163 "LLVM IR generation of declaration");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000164 Gen->HandleTagDeclDefinition(D);
165 }
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000166
167 virtual void CompleteTentativeDefinition(VarDecl *D) {
168 Gen->CompleteTentativeDefinition(D);
169 }
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000170
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000171 virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
172 Gen->HandleVTable(RD, DefinitionRequired);
173 }
174
Chris Lattner6da9eb62010-04-06 18:38:50 +0000175 static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context,
176 unsigned LocCookie) {
177 SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie);
178 ((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc);
179 }
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000180
Chris Lattner6da9eb62010-04-06 18:38:50 +0000181 void InlineAsmDiagHandler2(const llvm::SMDiagnostic &,
182 SourceLocation LocCookie);
Mike Stump1eb44332009-09-09 15:08:12 +0000183 };
David Blaikie99ba9e32011-12-20 02:48:34 +0000184
185 void BackendConsumer::anchor() {}
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000186}
187
Chris Lattnerd6f19062010-04-08 00:23:06 +0000188/// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr
189/// buffer to be a valid FullSourceLoc.
190static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D,
191 SourceManager &CSM) {
192 // Get both the clang and llvm source managers. The location is relative to
193 // a memory buffer that the LLVM Source Manager is handling, we need to add
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000194 // a copy to the Clang source manager.
Chris Lattnerd6f19062010-04-08 00:23:06 +0000195 const llvm::SourceMgr &LSM = *D.getSourceMgr();
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000196
Chris Lattnerd6f19062010-04-08 00:23:06 +0000197 // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr
198 // already owns its one and clang::SourceManager wants to own its one.
199 const MemoryBuffer *LBuf =
200 LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc()));
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000201
Chris Lattnerd6f19062010-04-08 00:23:06 +0000202 // Create the copy and transfer ownership to clang::SourceManager.
203 llvm::MemoryBuffer *CBuf =
204 llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(),
205 LBuf->getBufferIdentifier());
206 FileID FID = CSM.createFileIDForMemBuffer(CBuf);
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000207
Chris Lattnerd6f19062010-04-08 00:23:06 +0000208 // Translate the offset into the file.
209 unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart();
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000210 SourceLocation NewLoc =
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000211 CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset);
Chris Lattnerd6f19062010-04-08 00:23:06 +0000212 return FullSourceLoc(NewLoc, CSM);
213}
214
Chris Lattnercabae682010-04-06 17:52:14 +0000215
Chris Lattner6da9eb62010-04-06 18:38:50 +0000216/// InlineAsmDiagHandler2 - This function is invoked when the backend hits an
217/// error parsing inline asm. The SMDiagnostic indicates the error relative to
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000218/// the temporary memory buffer that the inline asm parser has set up.
Chris Lattner6da9eb62010-04-06 18:38:50 +0000219void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D,
220 SourceLocation LocCookie) {
221 // There are a couple of different kinds of errors we could get here. First,
222 // we re-format the SMDiagnostic in terms of a clang diagnostic.
Argyrios Kyrtzidis5d3a4bb2012-02-01 06:36:49 +0000223
224 // Strip "error: " off the start of the message string.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000225 StringRef Message = D.getMessage();
Argyrios Kyrtzidis5d3a4bb2012-02-01 06:36:49 +0000226 if (Message.startswith("error: "))
227 Message = Message.substr(7);
Chris Lattner6da9eb62010-04-06 18:38:50 +0000228
Chris Lattner99e14a02010-06-15 00:03:12 +0000229 // If the SMDiagnostic has an inline asm source location, translate it.
Chris Lattner6da9eb62010-04-06 18:38:50 +0000230 FullSourceLoc Loc;
Chris Lattnerd6f19062010-04-08 00:23:06 +0000231 if (D.getLoc() != SMLoc())
232 Loc = ConvertBackendLocation(D, Context->getSourceManager());
Chris Lattnerff8f9ec2012-01-31 06:13:55 +0000233
Argyrios Kyrtzidis5d3a4bb2012-02-01 06:36:49 +0000234
Chris Lattner99e14a02010-06-15 00:03:12 +0000235 // If this problem has clang-level source location information, report the
236 // issue as being an error in the source with a note showing the instantiated
237 // code.
238 if (LocCookie.isValid()) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000239 Diags.Report(LocCookie, diag::err_fe_inline_asm).AddString(Message);
Chris Lattner99e14a02010-06-15 00:03:12 +0000240
Benjamin Kramer96fda0c2011-10-16 10:48:28 +0000241 if (D.getLoc().isValid()) {
242 DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here);
243 // Convert the SMDiagnostic ranges into SourceRange and attach them
244 // to the diagnostic.
245 for (unsigned i = 0, e = D.getRanges().size(); i != e; ++i) {
246 std::pair<unsigned, unsigned> Range = D.getRanges()[i];
247 unsigned Column = D.getColumnNo();
248 B << SourceRange(Loc.getLocWithOffset(Range.first - Column),
249 Loc.getLocWithOffset(Range.second - Column));
250 }
251 }
Chris Lattner99e14a02010-06-15 00:03:12 +0000252 return;
253 }
254
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000255 // Otherwise, report the backend error as occurring in the generated .s file.
Chris Lattner99e14a02010-06-15 00:03:12 +0000256 // If Loc is invalid, we still need to report the error, it just gets no
257 // location info.
258 Diags.Report(Loc, diag::err_fe_inline_asm).AddString(Message);
Chris Lattner6da9eb62010-04-06 18:38:50 +0000259}
260
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000261//
262
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000263CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext)
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +0000264 : Act(_Act), LinkModule(0),
265 VMContext(_VMContext ? _VMContext : new LLVMContext),
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000266 OwnsVMContext(!_VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000267
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000268CodeGenAction::~CodeGenAction() {
269 TheModule.reset();
270 if (OwnsVMContext)
271 delete VMContext;
272}
Daniel Dunbar9ad1c022010-02-25 20:37:44 +0000273
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000274bool CodeGenAction::hasIRSupport() const { return true; }
275
Daniel Dunbarb954e982010-02-25 04:37:50 +0000276void CodeGenAction::EndSourceFileAction() {
277 // If the consumer creation failed, do nothing.
278 if (!getCompilerInstance().hasASTConsumer())
279 return;
280
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +0000281 // If we were given a link module, release consumer's ownership of it.
282 if (LinkModule)
283 BEConsumer->takeLinkModule();
284
Daniel Dunbarb954e982010-02-25 04:37:50 +0000285 // Steal the module from the consumer.
Nico Weber5aa74af2011-01-25 20:34:14 +0000286 TheModule.reset(BEConsumer->takeModule());
Daniel Dunbarb954e982010-02-25 04:37:50 +0000287}
288
289llvm::Module *CodeGenAction::takeModule() {
290 return TheModule.take();
291}
292
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000293llvm::LLVMContext *CodeGenAction::takeLLVMContext() {
294 OwnsVMContext = false;
295 return VMContext;
296}
297
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000298static raw_ostream *GetOutputStream(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000299 StringRef InFile,
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000300 BackendAction Action) {
301 switch (Action) {
302 case Backend_EmitAssembly:
303 return CI.createDefaultOutputFile(false, InFile, "s");
304 case Backend_EmitLL:
305 return CI.createDefaultOutputFile(false, InFile, "ll");
306 case Backend_EmitBC:
307 return CI.createDefaultOutputFile(true, InFile, "bc");
308 case Backend_EmitNothing:
309 return 0;
310 case Backend_EmitMCNull:
311 case Backend_EmitObj:
312 return CI.createDefaultOutputFile(true, InFile, "o");
313 }
314
David Blaikieb219cfc2011-09-23 05:06:16 +0000315 llvm_unreachable("Invalid action!");
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000316}
317
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000318ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000319 StringRef InFile) {
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000320 BackendAction BA = static_cast<BackendAction>(Act);
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000321 OwningPtr<raw_ostream> OS(GetOutputStream(CI, InFile, BA));
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000322 if (BA != Backend_EmitNothing && !OS)
323 return 0;
324
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +0000325 llvm::Module *LinkModuleToUse = LinkModule;
326
327 // If we were not given a link module, and the user requested that one be
328 // loaded from bitcode, do so now.
329 const std::string &LinkBCFile = CI.getCodeGenOpts().LinkBitcodeFile;
330 if (!LinkModuleToUse && !LinkBCFile.empty()) {
331 std::string ErrorStr;
332
333 llvm::MemoryBuffer *BCBuf =
334 CI.getFileManager().getBufferForFile(LinkBCFile, &ErrorStr);
335 if (!BCBuf) {
336 CI.getDiagnostics().Report(diag::err_cannot_open_file)
337 << LinkBCFile << ErrorStr;
338 return 0;
339 }
340
341 LinkModuleToUse = getLazyBitcodeModule(BCBuf, *VMContext, &ErrorStr);
342 if (!LinkModuleToUse) {
343 CI.getDiagnostics().Report(diag::err_cannot_open_file)
344 << LinkBCFile << ErrorStr;
345 return 0;
346 }
347 }
348
Nico Weber5aa74af2011-01-25 20:34:14 +0000349 BEConsumer =
350 new BackendConsumer(BA, CI.getDiagnostics(),
351 CI.getCodeGenOpts(), CI.getTargetOpts(),
Dan Gohmanb18b8ad2011-07-05 22:02:36 +0000352 CI.getLangOpts(),
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +0000353 CI.getFrontendOpts().ShowTimers, InFile,
354 LinkModuleToUse, OS.take(), *VMContext);
Nico Weber5aa74af2011-01-25 20:34:14 +0000355 return BEConsumer;
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000356}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000357
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000358void CodeGenAction::ExecuteAction() {
359 // If this is an IR file, we have to treat it specially.
360 if (getCurrentFileKind() == IK_LLVM_IR) {
361 BackendAction BA = static_cast<BackendAction>(Act);
362 CompilerInstance &CI = getCompilerInstance();
363 raw_ostream *OS = GetOutputStream(CI, getCurrentFile(), BA);
364 if (BA != Backend_EmitNothing && !OS)
365 return;
366
367 bool Invalid;
368 SourceManager &SM = CI.getSourceManager();
369 const llvm::MemoryBuffer *MainFile = SM.getBuffer(SM.getMainFileID(),
370 &Invalid);
371 if (Invalid)
372 return;
373
374 // FIXME: This is stupid, IRReader shouldn't take ownership.
375 llvm::MemoryBuffer *MainFileCopy =
376 llvm::MemoryBuffer::getMemBufferCopy(MainFile->getBuffer(),
377 getCurrentFile().c_str());
378
379 llvm::SMDiagnostic Err;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000380 TheModule.reset(ParseIR(MainFileCopy, Err, *VMContext));
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000381 if (!TheModule) {
382 // Translate from the diagnostic info to the SourceManager location.
Argyrios Kyrtzidis5a9ee202011-09-19 20:40:38 +0000383 SourceLocation Loc = SM.translateFileLineCol(
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000384 SM.getFileEntryForID(SM.getMainFileID()), Err.getLineNo(),
385 Err.getColumnNo() + 1);
386
387 // Get a custom diagnostic for the error. We strip off a leading
388 // diagnostic code if there is one.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000389 StringRef Msg = Err.getMessage();
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000390 if (Msg.startswith("error: "))
391 Msg = Msg.substr(7);
David Blaikied6471f72011-09-25 23:23:43 +0000392 unsigned DiagID = CI.getDiagnostics().getCustomDiagID(
393 DiagnosticsEngine::Error, Msg);
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000394
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000395 CI.getDiagnostics().Report(Loc, DiagID);
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000396 return;
397 }
398
399 EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(),
Dan Gohmanb18b8ad2011-07-05 22:02:36 +0000400 CI.getTargetOpts(), CI.getLangOpts(),
401 TheModule.get(),
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000402 BA, OS);
403 return;
404 }
405
406 // Otherwise follow the normal AST path.
407 this->ASTFrontendAction::ExecuteAction();
408}
409
410//
411
David Blaikie99ba9e32011-12-20 02:48:34 +0000412void EmitAssemblyAction::anchor() { }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000413EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext)
414 : CodeGenAction(Backend_EmitAssembly, _VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000415
David Blaikie99ba9e32011-12-20 02:48:34 +0000416void EmitBCAction::anchor() { }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000417EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext)
418 : CodeGenAction(Backend_EmitBC, _VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000419
David Blaikie99ba9e32011-12-20 02:48:34 +0000420void EmitLLVMAction::anchor() { }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000421EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext)
422 : CodeGenAction(Backend_EmitLL, _VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000423
David Blaikie99ba9e32011-12-20 02:48:34 +0000424void EmitLLVMOnlyAction::anchor() { }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000425EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext)
426 : CodeGenAction(Backend_EmitNothing, _VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000427
David Blaikie99ba9e32011-12-20 02:48:34 +0000428void EmitCodeGenOnlyAction::anchor() { }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000429EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext)
430 : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
Daniel Dunbar32148ce2010-05-25 18:41:01 +0000431
David Blaikie99ba9e32011-12-20 02:48:34 +0000432void EmitObjAction::anchor() { }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000433EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext)
434 : CodeGenAction(Backend_EmitObj, _VMContext) {}