blob: db2bab9bf61875c6560831a93840bc0b1d0bcbd1 [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 Blaikied6471f72011-09-25 23:23:43 +000036 DiagnosticsEngine &Diags;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000037 BackendAction Action;
Daniel Dunbar3636e1d2009-11-30 08:39:32 +000038 const CodeGenOptions &CodeGenOpts;
Daniel Dunbar3636e1d2009-11-30 08:39:32 +000039 const TargetOptions &TargetOpts;
Dan Gohmanb18b8ad2011-07-05 22:02:36 +000040 const LangOptions &LangOpts;
Chris Lattner5f9e2722011-07-23 10:55:15 +000041 raw_ostream *AsmOutStream;
Chris Lattner49f28ca2009-03-05 08:00:35 +000042 ASTContext *Context;
Daniel Dunbar90f41302008-10-29 08:50:02 +000043
Chris Lattner6f114eb2009-02-18 01:37:30 +000044 Timer LLVMIRGeneration;
Mike Stump1eb44332009-09-09 15:08:12 +000045
Daniel Dunbard69bacc2008-10-21 23:49:24 +000046 llvm::OwningPtr<CodeGenerator> Gen;
Mike Stump1eb44332009-09-09 15:08:12 +000047
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +000048 llvm::OwningPtr<llvm::Module> TheModule, LinkModule;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000049
Mike Stump1eb44332009-09-09 15:08:12 +000050 public:
David Blaikied6471f72011-09-25 23:23:43 +000051 BackendConsumer(BackendAction action, DiagnosticsEngine &_Diags,
Daniel Dunbar6b0cf672010-06-07 23:19:17 +000052 const CodeGenOptions &compopts,
Dan Gohmanb18b8ad2011-07-05 22:02:36 +000053 const TargetOptions &targetopts,
54 const LangOptions &langopts,
55 bool TimePasses,
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +000056 const std::string &infile,
57 llvm::Module *LinkModule,
58 raw_ostream *OS,
Chris Lattnercabae682010-04-06 17:52:14 +000059 LLVMContext &C) :
Daniel Dunbar3be0d192009-12-03 09:12:54 +000060 Diags(_Diags),
Mike Stump1eb44332009-09-09 15:08:12 +000061 Action(action),
Chandler Carruth2811ccf2009-11-12 17:24:48 +000062 CodeGenOpts(compopts),
Daniel Dunbard58c03f2009-11-15 06:48:46 +000063 TargetOpts(targetopts),
Dan Gohmanb18b8ad2011-07-05 22:02:36 +000064 LangOpts(langopts),
Chris Lattner03eacc72009-07-14 20:39:15 +000065 AsmOutStream(OS),
Chris Lattner6f114eb2009-02-18 01:37:30 +000066 LLVMIRGeneration("LLVM IR Generation Time"),
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +000067 Gen(CreateLLVMCodeGen(Diags, infile, compopts, C)),
68 LinkModule(LinkModule) {
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +000069 llvm::TimePassesIsEnabled = TimePasses;
Chris Lattner44502662009-02-18 01:23:44 +000070 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +000071
Daniel Dunbarb954e982010-02-25 04:37:50 +000072 llvm::Module *takeModule() { return TheModule.take(); }
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +000073 llvm::Module *takeLinkModule() { return LinkModule.take(); }
Daniel Dunbarb954e982010-02-25 04:37:50 +000074
Chris Lattner7bb0da02009-03-28 02:18:25 +000075 virtual void Initialize(ASTContext &Ctx) {
76 Context = &Ctx;
Mike Stump1eb44332009-09-09 15:08:12 +000077
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +000078 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +000079 LLVMIRGeneration.startTimer();
Mike Stump1eb44332009-09-09 15:08:12 +000080
Chris Lattner7bb0da02009-03-28 02:18:25 +000081 Gen->Initialize(Ctx);
Daniel Dunbard69bacc2008-10-21 23:49:24 +000082
Daniel Dunbarb954e982010-02-25 04:37:50 +000083 TheModule.reset(Gen->GetModule());
Mike Stump1eb44332009-09-09 15:08:12 +000084
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +000085 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +000086 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +000087 }
Mike Stump1eb44332009-09-09 15:08:12 +000088
Chris Lattner682bf922009-03-29 16:50:03 +000089 virtual void HandleTopLevelDecl(DeclGroupRef D) {
90 PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
Chris Lattner49f28ca2009-03-05 08:00:35 +000091 Context->getSourceManager(),
92 "LLVM IR generation of declaration");
Mike Stump1eb44332009-09-09 15:08:12 +000093
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +000094 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +000095 LLVMIRGeneration.startTimer();
Chris Lattner682bf922009-03-29 16:50:03 +000096
Daniel Dunbard69bacc2008-10-21 23:49:24 +000097 Gen->HandleTopLevelDecl(D);
Chris Lattner6f114eb2009-02-18 01:37:30 +000098
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +000099 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +0000100 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000101 }
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000103 virtual void HandleTranslationUnit(ASTContext &C) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000104 {
Chris Lattner14f234e2009-03-06 06:46:31 +0000105 PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000106 if (llvm::TimePassesIsEnabled)
Chris Lattner49f28ca2009-03-05 08:00:35 +0000107 LLVMIRGeneration.startTimer();
Chris Lattner6f114eb2009-02-18 01:37:30 +0000108
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000109 Gen->HandleTranslationUnit(C);
Daniel Dunbard68ba0e2008-11-11 06:35:39 +0000110
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000111 if (llvm::TimePassesIsEnabled)
Chris Lattner49f28ca2009-03-05 08:00:35 +0000112 LLVMIRGeneration.stopTimer();
113 }
Chris Lattner6f114eb2009-02-18 01:37:30 +0000114
Daniel Dunbar897c6762010-06-07 23:20:08 +0000115 // Silently ignore if we weren't initialized for some reason.
Daniel Dunbar05a7f3d2010-06-07 23:21:04 +0000116 if (!TheModule)
Daniel Dunbar897c6762010-06-07 23:20:08 +0000117 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Daniel Dunbar897c6762010-06-07 23:20:08 +0000119 // Make sure IR generation is happy with the module. This is released by
120 // the module provider.
121 Module *M = Gen->ReleaseModule();
122 if (!M) {
123 // The module has been released by IR gen on failures, do not double
124 // free.
125 TheModule.take();
126 return;
127 }
128
129 assert(TheModule.get() == M &&
130 "Unexpected module change during IR generation");
131
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +0000132 // Link LinkModule into this module if present, preserving its validity.
133 if (LinkModule) {
134 std::string ErrorMsg;
135 if (Linker::LinkModules(M, LinkModule.get(), Linker::PreserveSource,
136 &ErrorMsg)) {
137 Diags.Report(diag::err_fe_cannot_link_module)
138 << LinkModule->getModuleIdentifier() << ErrorMsg;
139 return;
140 }
141 }
142
Daniel Dunbar897c6762010-06-07 23:20:08 +0000143 // Install an inline asm handler so that diagnostics get printed through
144 // our diagnostics hooks.
145 LLVMContext &Ctx = TheModule->getContext();
Chris Lattner063e4762010-11-17 08:13:04 +0000146 LLVMContext::InlineAsmDiagHandlerTy OldHandler =
147 Ctx.getInlineAsmDiagnosticHandler();
Daniel Dunbar897c6762010-06-07 23:20:08 +0000148 void *OldContext = Ctx.getInlineAsmDiagnosticContext();
Chris Lattner063e4762010-11-17 08:13:04 +0000149 Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this);
Daniel Dunbar897c6762010-06-07 23:20:08 +0000150
Dan Gohmanb18b8ad2011-07-05 22:02:36 +0000151 EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
Daniel Dunbar05a7f3d2010-06-07 23:21:04 +0000152 TheModule.get(), Action, AsmOutStream);
Daniel Dunbar897c6762010-06-07 23:20:08 +0000153
154 Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000155 }
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000157 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000158 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
159 Context->getSourceManager(),
160 "LLVM IR generation of declaration");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000161 Gen->HandleTagDeclDefinition(D);
162 }
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000163
164 virtual void CompleteTentativeDefinition(VarDecl *D) {
165 Gen->CompleteTentativeDefinition(D);
166 }
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000167
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000168 virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
169 Gen->HandleVTable(RD, DefinitionRequired);
170 }
171
Chris Lattner6da9eb62010-04-06 18:38:50 +0000172 static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context,
173 unsigned LocCookie) {
174 SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie);
175 ((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc);
176 }
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000177
Chris Lattner6da9eb62010-04-06 18:38:50 +0000178 void InlineAsmDiagHandler2(const llvm::SMDiagnostic &,
179 SourceLocation LocCookie);
Mike Stump1eb44332009-09-09 15:08:12 +0000180 };
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000181}
182
Chris Lattnerd6f19062010-04-08 00:23:06 +0000183/// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr
184/// buffer to be a valid FullSourceLoc.
185static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D,
186 SourceManager &CSM) {
187 // Get both the clang and llvm source managers. The location is relative to
188 // a memory buffer that the LLVM Source Manager is handling, we need to add
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000189 // a copy to the Clang source manager.
Chris Lattnerd6f19062010-04-08 00:23:06 +0000190 const llvm::SourceMgr &LSM = *D.getSourceMgr();
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000191
Chris Lattnerd6f19062010-04-08 00:23:06 +0000192 // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr
193 // already owns its one and clang::SourceManager wants to own its one.
194 const MemoryBuffer *LBuf =
195 LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc()));
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000196
Chris Lattnerd6f19062010-04-08 00:23:06 +0000197 // Create the copy and transfer ownership to clang::SourceManager.
198 llvm::MemoryBuffer *CBuf =
199 llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(),
200 LBuf->getBufferIdentifier());
201 FileID FID = CSM.createFileIDForMemBuffer(CBuf);
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000202
Chris Lattnerd6f19062010-04-08 00:23:06 +0000203 // Translate the offset into the file.
204 unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart();
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000205 SourceLocation NewLoc =
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000206 CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset);
Chris Lattnerd6f19062010-04-08 00:23:06 +0000207 return FullSourceLoc(NewLoc, CSM);
208}
209
Chris Lattnercabae682010-04-06 17:52:14 +0000210
Chris Lattner6da9eb62010-04-06 18:38:50 +0000211/// InlineAsmDiagHandler2 - This function is invoked when the backend hits an
212/// error parsing inline asm. The SMDiagnostic indicates the error relative to
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000213/// the temporary memory buffer that the inline asm parser has set up.
Chris Lattner6da9eb62010-04-06 18:38:50 +0000214void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D,
215 SourceLocation LocCookie) {
216 // There are a couple of different kinds of errors we could get here. First,
217 // we re-format the SMDiagnostic in terms of a clang diagnostic.
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000218
Chris Lattner6da9eb62010-04-06 18:38:50 +0000219 // Strip "error: " off the start of the message string.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000220 StringRef Message = D.getMessage();
Chris Lattner6da9eb62010-04-06 18:38:50 +0000221 if (Message.startswith("error: "))
222 Message = Message.substr(7);
223
Chris Lattner99e14a02010-06-15 00:03:12 +0000224 // If the SMDiagnostic has an inline asm source location, translate it.
Chris Lattner6da9eb62010-04-06 18:38:50 +0000225 FullSourceLoc Loc;
Chris Lattnerd6f19062010-04-08 00:23:06 +0000226 if (D.getLoc() != SMLoc())
227 Loc = ConvertBackendLocation(D, Context->getSourceManager());
Chris Lattner99e14a02010-06-15 00:03:12 +0000228
Daniel Dunbarf80cb752010-04-29 16:29:09 +0000229
Chris Lattner99e14a02010-06-15 00:03:12 +0000230 // If this problem has clang-level source location information, report the
231 // issue as being an error in the source with a note showing the instantiated
232 // code.
233 if (LocCookie.isValid()) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000234 Diags.Report(LocCookie, diag::err_fe_inline_asm).AddString(Message);
Chris Lattner99e14a02010-06-15 00:03:12 +0000235
Benjamin Kramer96fda0c2011-10-16 10:48:28 +0000236 if (D.getLoc().isValid()) {
237 DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here);
238 // Convert the SMDiagnostic ranges into SourceRange and attach them
239 // to the diagnostic.
240 for (unsigned i = 0, e = D.getRanges().size(); i != e; ++i) {
241 std::pair<unsigned, unsigned> Range = D.getRanges()[i];
242 unsigned Column = D.getColumnNo();
243 B << SourceRange(Loc.getLocWithOffset(Range.first - Column),
244 Loc.getLocWithOffset(Range.second - Column));
245 }
246 }
Chris Lattner99e14a02010-06-15 00:03:12 +0000247 return;
248 }
249
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000250 // Otherwise, report the backend error as occurring in the generated .s file.
Chris Lattner99e14a02010-06-15 00:03:12 +0000251 // If Loc is invalid, we still need to report the error, it just gets no
252 // location info.
253 Diags.Report(Loc, diag::err_fe_inline_asm).AddString(Message);
Chris Lattner6da9eb62010-04-06 18:38:50 +0000254}
255
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000256//
257
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000258CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext)
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +0000259 : Act(_Act), LinkModule(0),
260 VMContext(_VMContext ? _VMContext : new LLVMContext),
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000261 OwnsVMContext(!_VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000262
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000263CodeGenAction::~CodeGenAction() {
264 TheModule.reset();
265 if (OwnsVMContext)
266 delete VMContext;
267}
Daniel Dunbar9ad1c022010-02-25 20:37:44 +0000268
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000269bool CodeGenAction::hasIRSupport() const { return true; }
270
Daniel Dunbarb954e982010-02-25 04:37:50 +0000271void CodeGenAction::EndSourceFileAction() {
272 // If the consumer creation failed, do nothing.
273 if (!getCompilerInstance().hasASTConsumer())
274 return;
275
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +0000276 // If we were given a link module, release consumer's ownership of it.
277 if (LinkModule)
278 BEConsumer->takeLinkModule();
279
Daniel Dunbarb954e982010-02-25 04:37:50 +0000280 // Steal the module from the consumer.
Nico Weber5aa74af2011-01-25 20:34:14 +0000281 TheModule.reset(BEConsumer->takeModule());
Daniel Dunbarb954e982010-02-25 04:37:50 +0000282}
283
284llvm::Module *CodeGenAction::takeModule() {
285 return TheModule.take();
286}
287
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000288llvm::LLVMContext *CodeGenAction::takeLLVMContext() {
289 OwnsVMContext = false;
290 return VMContext;
291}
292
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000293static raw_ostream *GetOutputStream(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000294 StringRef InFile,
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000295 BackendAction Action) {
296 switch (Action) {
297 case Backend_EmitAssembly:
298 return CI.createDefaultOutputFile(false, InFile, "s");
299 case Backend_EmitLL:
300 return CI.createDefaultOutputFile(false, InFile, "ll");
301 case Backend_EmitBC:
302 return CI.createDefaultOutputFile(true, InFile, "bc");
303 case Backend_EmitNothing:
304 return 0;
305 case Backend_EmitMCNull:
306 case Backend_EmitObj:
307 return CI.createDefaultOutputFile(true, InFile, "o");
308 }
309
David Blaikieb219cfc2011-09-23 05:06:16 +0000310 llvm_unreachable("Invalid action!");
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000311}
312
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000313ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000314 StringRef InFile) {
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000315 BackendAction BA = static_cast<BackendAction>(Act);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000316 llvm::OwningPtr<raw_ostream> OS(GetOutputStream(CI, InFile, BA));
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000317 if (BA != Backend_EmitNothing && !OS)
318 return 0;
319
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +0000320 llvm::Module *LinkModuleToUse = LinkModule;
321
322 // If we were not given a link module, and the user requested that one be
323 // loaded from bitcode, do so now.
324 const std::string &LinkBCFile = CI.getCodeGenOpts().LinkBitcodeFile;
325 if (!LinkModuleToUse && !LinkBCFile.empty()) {
326 std::string ErrorStr;
327
328 llvm::MemoryBuffer *BCBuf =
329 CI.getFileManager().getBufferForFile(LinkBCFile, &ErrorStr);
330 if (!BCBuf) {
331 CI.getDiagnostics().Report(diag::err_cannot_open_file)
332 << LinkBCFile << ErrorStr;
333 return 0;
334 }
335
336 LinkModuleToUse = getLazyBitcodeModule(BCBuf, *VMContext, &ErrorStr);
337 if (!LinkModuleToUse) {
338 CI.getDiagnostics().Report(diag::err_cannot_open_file)
339 << LinkBCFile << ErrorStr;
340 return 0;
341 }
342 }
343
Nico Weber5aa74af2011-01-25 20:34:14 +0000344 BEConsumer =
345 new BackendConsumer(BA, CI.getDiagnostics(),
346 CI.getCodeGenOpts(), CI.getTargetOpts(),
Dan Gohmanb18b8ad2011-07-05 22:02:36 +0000347 CI.getLangOpts(),
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +0000348 CI.getFrontendOpts().ShowTimers, InFile,
349 LinkModuleToUse, OS.take(), *VMContext);
Nico Weber5aa74af2011-01-25 20:34:14 +0000350 return BEConsumer;
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000351}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000352
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000353void CodeGenAction::ExecuteAction() {
354 // If this is an IR file, we have to treat it specially.
355 if (getCurrentFileKind() == IK_LLVM_IR) {
356 BackendAction BA = static_cast<BackendAction>(Act);
357 CompilerInstance &CI = getCompilerInstance();
358 raw_ostream *OS = GetOutputStream(CI, getCurrentFile(), BA);
359 if (BA != Backend_EmitNothing && !OS)
360 return;
361
362 bool Invalid;
363 SourceManager &SM = CI.getSourceManager();
364 const llvm::MemoryBuffer *MainFile = SM.getBuffer(SM.getMainFileID(),
365 &Invalid);
366 if (Invalid)
367 return;
368
369 // FIXME: This is stupid, IRReader shouldn't take ownership.
370 llvm::MemoryBuffer *MainFileCopy =
371 llvm::MemoryBuffer::getMemBufferCopy(MainFile->getBuffer(),
372 getCurrentFile().c_str());
373
374 llvm::SMDiagnostic Err;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000375 TheModule.reset(ParseIR(MainFileCopy, Err, *VMContext));
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000376 if (!TheModule) {
377 // Translate from the diagnostic info to the SourceManager location.
Argyrios Kyrtzidis5a9ee202011-09-19 20:40:38 +0000378 SourceLocation Loc = SM.translateFileLineCol(
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000379 SM.getFileEntryForID(SM.getMainFileID()), Err.getLineNo(),
380 Err.getColumnNo() + 1);
381
382 // Get a custom diagnostic for the error. We strip off a leading
383 // diagnostic code if there is one.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000384 StringRef Msg = Err.getMessage();
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000385 if (Msg.startswith("error: "))
386 Msg = Msg.substr(7);
David Blaikied6471f72011-09-25 23:23:43 +0000387 unsigned DiagID = CI.getDiagnostics().getCustomDiagID(
388 DiagnosticsEngine::Error, Msg);
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000389
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000390 CI.getDiagnostics().Report(Loc, DiagID);
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000391 return;
392 }
393
394 EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(),
Dan Gohmanb18b8ad2011-07-05 22:02:36 +0000395 CI.getTargetOpts(), CI.getLangOpts(),
396 TheModule.get(),
Daniel Dunbar4cbbd942010-06-07 23:27:59 +0000397 BA, OS);
398 return;
399 }
400
401 // Otherwise follow the normal AST path.
402 this->ASTFrontendAction::ExecuteAction();
403}
404
405//
406
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000407EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext)
408 : CodeGenAction(Backend_EmitAssembly, _VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000409
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000410EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext)
411 : CodeGenAction(Backend_EmitBC, _VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000412
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000413EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext)
414 : CodeGenAction(Backend_EmitLL, _VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000415
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000416EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext)
417 : CodeGenAction(Backend_EmitNothing, _VMContext) {}
Daniel Dunbar4ee34612010-02-25 04:37:45 +0000418
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000419EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext)
420 : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
Daniel Dunbar32148ce2010-05-25 18:41:01 +0000421
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000422EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext)
423 : CodeGenAction(Backend_EmitObj, _VMContext) {}