Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 1 | //===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===// |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 2 | // |
| 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 Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 10 | #include "clang/CodeGen/CodeGenAction.h" |
Peter Collingbourne | 22a7dfe | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 11 | #include "clang/Basic/FileManager.h" |
Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 12 | #include "clang/Basic/SourceManager.h" |
| 13 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTConsumer.h" |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclGroup.h" |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 17 | #include "clang/CodeGen/BackendUtil.h" |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 18 | #include "clang/CodeGen/ModuleBuilder.h" |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/FrontendDiagnostic.h" |
Chris Lattner | cabae68 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 21 | #include "llvm/LLVMContext.h" |
Peter Collingbourne | 22a7dfe | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 22 | #include "llvm/Linker.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
Daniel Dunbar | 05a7f3d | 2010-06-07 23:21:04 +0000 | [diff] [blame] | 24 | #include "llvm/Pass.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/OwningPtr.h" |
Peter Collingbourne | 22a7dfe | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 26 | #include "llvm/Bitcode/ReaderWriter.h" |
Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 27 | #include "llvm/Support/IRReader.h" |
Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MemoryBuffer.h" |
| 29 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Timer.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | using namespace llvm; |
| 33 | |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 34 | namespace clang { |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 35 | class BackendConsumer : public ASTConsumer { |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 36 | DiagnosticsEngine &Diags; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 37 | BackendAction Action; |
Daniel Dunbar | 3636e1d | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 38 | const CodeGenOptions &CodeGenOpts; |
Daniel Dunbar | 3636e1d | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 39 | const TargetOptions &TargetOpts; |
Dan Gohman | b18b8ad | 2011-07-05 22:02:36 +0000 | [diff] [blame] | 40 | const LangOptions &LangOpts; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 41 | raw_ostream *AsmOutStream; |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 42 | ASTContext *Context; |
Daniel Dunbar | 90f4130 | 2008-10-29 08:50:02 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 44 | Timer LLVMIRGeneration; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 46 | llvm::OwningPtr<CodeGenerator> Gen; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
Peter Collingbourne | 22a7dfe | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 48 | llvm::OwningPtr<llvm::Module> TheModule, LinkModule; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 49 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | public: |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 51 | BackendConsumer(BackendAction action, DiagnosticsEngine &_Diags, |
Daniel Dunbar | 6b0cf67 | 2010-06-07 23:19:17 +0000 | [diff] [blame] | 52 | const CodeGenOptions &compopts, |
Dan Gohman | b18b8ad | 2011-07-05 22:02:36 +0000 | [diff] [blame] | 53 | const TargetOptions &targetopts, |
| 54 | const LangOptions &langopts, |
| 55 | bool TimePasses, |
Peter Collingbourne | 22a7dfe | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 56 | const std::string &infile, |
| 57 | llvm::Module *LinkModule, |
| 58 | raw_ostream *OS, |
Chris Lattner | cabae68 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 59 | LLVMContext &C) : |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 60 | Diags(_Diags), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | Action(action), |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 62 | CodeGenOpts(compopts), |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 63 | TargetOpts(targetopts), |
Dan Gohman | b18b8ad | 2011-07-05 22:02:36 +0000 | [diff] [blame] | 64 | LangOpts(langopts), |
Chris Lattner | 03eacc7 | 2009-07-14 20:39:15 +0000 | [diff] [blame] | 65 | AsmOutStream(OS), |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 66 | LLVMIRGeneration("LLVM IR Generation Time"), |
Peter Collingbourne | 22a7dfe | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 67 | Gen(CreateLLVMCodeGen(Diags, infile, compopts, C)), |
| 68 | LinkModule(LinkModule) { |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 69 | llvm::TimePassesIsEnabled = TimePasses; |
Chris Lattner | 4450266 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 70 | } |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 71 | |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 72 | llvm::Module *takeModule() { return TheModule.take(); } |
Peter Collingbourne | 22a7dfe | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 73 | llvm::Module *takeLinkModule() { return LinkModule.take(); } |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 7bb0da0 | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 75 | virtual void Initialize(ASTContext &Ctx) { |
| 76 | Context = &Ctx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 78 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 79 | LLVMIRGeneration.startTimer(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 7bb0da0 | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 81 | Gen->Initialize(Ctx); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 82 | |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 83 | TheModule.reset(Gen->GetModule()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 85 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 86 | LLVMIRGeneration.stopTimer(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 87 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame^] | 89 | virtual bool HandleTopLevelDecl(DeclGroupRef D) { |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 90 | PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(), |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 91 | Context->getSourceManager(), |
| 92 | "LLVM IR generation of declaration"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 94 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 95 | LLVMIRGeneration.startTimer(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 96 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 97 | Gen->HandleTopLevelDecl(D); |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 98 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 99 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 100 | LLVMIRGeneration.stopTimer(); |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame^] | 101 | |
| 102 | return true; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 103 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 105 | virtual void HandleTranslationUnit(ASTContext &C) { |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 106 | { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 107 | PrettyStackTraceString CrashInfo("Per-file LLVM IR generation"); |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 108 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 109 | LLVMIRGeneration.startTimer(); |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 110 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 111 | Gen->HandleTranslationUnit(C); |
Daniel Dunbar | d68ba0e | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 112 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 113 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 114 | LLVMIRGeneration.stopTimer(); |
| 115 | } |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 116 | |
Daniel Dunbar | 897c676 | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 117 | // Silently ignore if we weren't initialized for some reason. |
Daniel Dunbar | 05a7f3d | 2010-06-07 23:21:04 +0000 | [diff] [blame] | 118 | if (!TheModule) |
Daniel Dunbar | 897c676 | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 119 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Daniel Dunbar | 897c676 | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 121 | // Make sure IR generation is happy with the module. This is released by |
| 122 | // the module provider. |
| 123 | Module *M = Gen->ReleaseModule(); |
| 124 | if (!M) { |
| 125 | // The module has been released by IR gen on failures, do not double |
| 126 | // free. |
| 127 | TheModule.take(); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | assert(TheModule.get() == M && |
| 132 | "Unexpected module change during IR generation"); |
| 133 | |
Peter Collingbourne | 22a7dfe | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 134 | // Link LinkModule into this module if present, preserving its validity. |
| 135 | if (LinkModule) { |
| 136 | std::string ErrorMsg; |
| 137 | if (Linker::LinkModules(M, LinkModule.get(), Linker::PreserveSource, |
| 138 | &ErrorMsg)) { |
| 139 | Diags.Report(diag::err_fe_cannot_link_module) |
| 140 | << LinkModule->getModuleIdentifier() << ErrorMsg; |
| 141 | return; |
| 142 | } |
| 143 | } |
| 144 | |
Daniel Dunbar | 897c676 | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 145 | // Install an inline asm handler so that diagnostics get printed through |
| 146 | // our diagnostics hooks. |
| 147 | LLVMContext &Ctx = TheModule->getContext(); |
Chris Lattner | 063e476 | 2010-11-17 08:13:04 +0000 | [diff] [blame] | 148 | LLVMContext::InlineAsmDiagHandlerTy OldHandler = |
| 149 | Ctx.getInlineAsmDiagnosticHandler(); |
Daniel Dunbar | 897c676 | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 150 | void *OldContext = Ctx.getInlineAsmDiagnosticContext(); |
Chris Lattner | 063e476 | 2010-11-17 08:13:04 +0000 | [diff] [blame] | 151 | Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this); |
Daniel Dunbar | 897c676 | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 152 | |
Dan Gohman | b18b8ad | 2011-07-05 22:02:36 +0000 | [diff] [blame] | 153 | EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts, |
Daniel Dunbar | 05a7f3d | 2010-06-07 23:21:04 +0000 | [diff] [blame] | 154 | TheModule.get(), Action, AsmOutStream); |
Daniel Dunbar | 897c676 | 2010-06-07 23:20:08 +0000 | [diff] [blame] | 155 | |
| 156 | Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 157 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 158 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 159 | virtual void HandleTagDeclDefinition(TagDecl *D) { |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 160 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 161 | Context->getSourceManager(), |
| 162 | "LLVM IR generation of declaration"); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 163 | Gen->HandleTagDeclDefinition(D); |
| 164 | } |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 165 | |
| 166 | virtual void CompleteTentativeDefinition(VarDecl *D) { |
| 167 | Gen->CompleteTentativeDefinition(D); |
| 168 | } |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 169 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 170 | virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) { |
| 171 | Gen->HandleVTable(RD, DefinitionRequired); |
| 172 | } |
| 173 | |
Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 174 | static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context, |
| 175 | unsigned LocCookie) { |
| 176 | SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie); |
| 177 | ((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc); |
| 178 | } |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 180 | void InlineAsmDiagHandler2(const llvm::SMDiagnostic &, |
| 181 | SourceLocation LocCookie); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | }; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 185 | /// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr |
| 186 | /// buffer to be a valid FullSourceLoc. |
| 187 | static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D, |
| 188 | SourceManager &CSM) { |
| 189 | // Get both the clang and llvm source managers. The location is relative to |
| 190 | // a memory buffer that the LLVM Source Manager is handling, we need to add |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 191 | // a copy to the Clang source manager. |
Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 192 | const llvm::SourceMgr &LSM = *D.getSourceMgr(); |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 193 | |
Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 194 | // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr |
| 195 | // already owns its one and clang::SourceManager wants to own its one. |
| 196 | const MemoryBuffer *LBuf = |
| 197 | LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc())); |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 198 | |
Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 199 | // Create the copy and transfer ownership to clang::SourceManager. |
| 200 | llvm::MemoryBuffer *CBuf = |
| 201 | llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(), |
| 202 | LBuf->getBufferIdentifier()); |
| 203 | FileID FID = CSM.createFileIDForMemBuffer(CBuf); |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 204 | |
Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 205 | // Translate the offset into the file. |
| 206 | unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart(); |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 207 | SourceLocation NewLoc = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 208 | CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset); |
Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 209 | return FullSourceLoc(NewLoc, CSM); |
| 210 | } |
| 211 | |
Chris Lattner | cabae68 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 213 | /// InlineAsmDiagHandler2 - This function is invoked when the backend hits an |
| 214 | /// error parsing inline asm. The SMDiagnostic indicates the error relative to |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 215 | /// the temporary memory buffer that the inline asm parser has set up. |
Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 216 | void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D, |
| 217 | SourceLocation LocCookie) { |
| 218 | // There are a couple of different kinds of errors we could get here. First, |
| 219 | // we re-format the SMDiagnostic in terms of a clang diagnostic. |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 221 | // Strip "error: " off the start of the message string. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 222 | StringRef Message = D.getMessage(); |
Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 223 | if (Message.startswith("error: ")) |
| 224 | Message = Message.substr(7); |
| 225 | |
Chris Lattner | 99e14a0 | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 226 | // If the SMDiagnostic has an inline asm source location, translate it. |
Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 227 | FullSourceLoc Loc; |
Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 228 | if (D.getLoc() != SMLoc()) |
| 229 | Loc = ConvertBackendLocation(D, Context->getSourceManager()); |
Chris Lattner | 99e14a0 | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 230 | |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 99e14a0 | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 232 | // If this problem has clang-level source location information, report the |
| 233 | // issue as being an error in the source with a note showing the instantiated |
| 234 | // code. |
| 235 | if (LocCookie.isValid()) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 236 | Diags.Report(LocCookie, diag::err_fe_inline_asm).AddString(Message); |
Chris Lattner | 99e14a0 | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 237 | |
Benjamin Kramer | 96fda0c | 2011-10-16 10:48:28 +0000 | [diff] [blame] | 238 | if (D.getLoc().isValid()) { |
| 239 | DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here); |
| 240 | // Convert the SMDiagnostic ranges into SourceRange and attach them |
| 241 | // to the diagnostic. |
| 242 | for (unsigned i = 0, e = D.getRanges().size(); i != e; ++i) { |
| 243 | std::pair<unsigned, unsigned> Range = D.getRanges()[i]; |
| 244 | unsigned Column = D.getColumnNo(); |
| 245 | B << SourceRange(Loc.getLocWithOffset(Range.first - Column), |
| 246 | Loc.getLocWithOffset(Range.second - Column)); |
| 247 | } |
| 248 | } |
Chris Lattner | 99e14a0 | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 249 | return; |
| 250 | } |
| 251 | |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 252 | // Otherwise, report the backend error as occurring in the generated .s file. |
Chris Lattner | 99e14a0 | 2010-06-15 00:03:12 +0000 | [diff] [blame] | 253 | // If Loc is invalid, we still need to report the error, it just gets no |
| 254 | // location info. |
| 255 | Diags.Report(Loc, diag::err_fe_inline_asm).AddString(Message); |
Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 258 | // |
| 259 | |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 260 | CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext) |
Peter Collingbourne | 22a7dfe | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 261 | : Act(_Act), LinkModule(0), |
| 262 | VMContext(_VMContext ? _VMContext : new LLVMContext), |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 263 | OwnsVMContext(!_VMContext) {} |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 264 | |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 265 | CodeGenAction::~CodeGenAction() { |
| 266 | TheModule.reset(); |
| 267 | if (OwnsVMContext) |
| 268 | delete VMContext; |
| 269 | } |
Daniel Dunbar | 9ad1c02 | 2010-02-25 20:37:44 +0000 | [diff] [blame] | 270 | |
Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 271 | bool CodeGenAction::hasIRSupport() const { return true; } |
| 272 | |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 273 | void CodeGenAction::EndSourceFileAction() { |
| 274 | // If the consumer creation failed, do nothing. |
| 275 | if (!getCompilerInstance().hasASTConsumer()) |
| 276 | return; |
| 277 | |
Peter Collingbourne | 22a7dfe | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 278 | // If we were given a link module, release consumer's ownership of it. |
| 279 | if (LinkModule) |
| 280 | BEConsumer->takeLinkModule(); |
| 281 | |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 282 | // Steal the module from the consumer. |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 283 | TheModule.reset(BEConsumer->takeModule()); |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | llvm::Module *CodeGenAction::takeModule() { |
| 287 | return TheModule.take(); |
| 288 | } |
| 289 | |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 290 | llvm::LLVMContext *CodeGenAction::takeLLVMContext() { |
| 291 | OwnsVMContext = false; |
| 292 | return VMContext; |
| 293 | } |
| 294 | |
Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 295 | static raw_ostream *GetOutputStream(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 296 | StringRef InFile, |
Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 297 | BackendAction Action) { |
| 298 | switch (Action) { |
| 299 | case Backend_EmitAssembly: |
| 300 | return CI.createDefaultOutputFile(false, InFile, "s"); |
| 301 | case Backend_EmitLL: |
| 302 | return CI.createDefaultOutputFile(false, InFile, "ll"); |
| 303 | case Backend_EmitBC: |
| 304 | return CI.createDefaultOutputFile(true, InFile, "bc"); |
| 305 | case Backend_EmitNothing: |
| 306 | return 0; |
| 307 | case Backend_EmitMCNull: |
| 308 | case Backend_EmitObj: |
| 309 | return CI.createDefaultOutputFile(true, InFile, "o"); |
| 310 | } |
| 311 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 312 | llvm_unreachable("Invalid action!"); |
Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 315 | ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 316 | StringRef InFile) { |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 317 | BackendAction BA = static_cast<BackendAction>(Act); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 318 | llvm::OwningPtr<raw_ostream> OS(GetOutputStream(CI, InFile, BA)); |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 319 | if (BA != Backend_EmitNothing && !OS) |
| 320 | return 0; |
| 321 | |
Peter Collingbourne | 22a7dfe | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 322 | llvm::Module *LinkModuleToUse = LinkModule; |
| 323 | |
| 324 | // If we were not given a link module, and the user requested that one be |
| 325 | // loaded from bitcode, do so now. |
| 326 | const std::string &LinkBCFile = CI.getCodeGenOpts().LinkBitcodeFile; |
| 327 | if (!LinkModuleToUse && !LinkBCFile.empty()) { |
| 328 | std::string ErrorStr; |
| 329 | |
| 330 | llvm::MemoryBuffer *BCBuf = |
| 331 | CI.getFileManager().getBufferForFile(LinkBCFile, &ErrorStr); |
| 332 | if (!BCBuf) { |
| 333 | CI.getDiagnostics().Report(diag::err_cannot_open_file) |
| 334 | << LinkBCFile << ErrorStr; |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | LinkModuleToUse = getLazyBitcodeModule(BCBuf, *VMContext, &ErrorStr); |
| 339 | if (!LinkModuleToUse) { |
| 340 | CI.getDiagnostics().Report(diag::err_cannot_open_file) |
| 341 | << LinkBCFile << ErrorStr; |
| 342 | return 0; |
| 343 | } |
| 344 | } |
| 345 | |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 346 | BEConsumer = |
| 347 | new BackendConsumer(BA, CI.getDiagnostics(), |
| 348 | CI.getCodeGenOpts(), CI.getTargetOpts(), |
Dan Gohman | b18b8ad | 2011-07-05 22:02:36 +0000 | [diff] [blame] | 349 | CI.getLangOpts(), |
Peter Collingbourne | 22a7dfe | 2011-10-30 17:30:44 +0000 | [diff] [blame] | 350 | CI.getFrontendOpts().ShowTimers, InFile, |
| 351 | LinkModuleToUse, OS.take(), *VMContext); |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 352 | return BEConsumer; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 353 | } |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 354 | |
Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 355 | void CodeGenAction::ExecuteAction() { |
| 356 | // If this is an IR file, we have to treat it specially. |
| 357 | if (getCurrentFileKind() == IK_LLVM_IR) { |
| 358 | BackendAction BA = static_cast<BackendAction>(Act); |
| 359 | CompilerInstance &CI = getCompilerInstance(); |
| 360 | raw_ostream *OS = GetOutputStream(CI, getCurrentFile(), BA); |
| 361 | if (BA != Backend_EmitNothing && !OS) |
| 362 | return; |
| 363 | |
| 364 | bool Invalid; |
| 365 | SourceManager &SM = CI.getSourceManager(); |
| 366 | const llvm::MemoryBuffer *MainFile = SM.getBuffer(SM.getMainFileID(), |
| 367 | &Invalid); |
| 368 | if (Invalid) |
| 369 | return; |
| 370 | |
| 371 | // FIXME: This is stupid, IRReader shouldn't take ownership. |
| 372 | llvm::MemoryBuffer *MainFileCopy = |
| 373 | llvm::MemoryBuffer::getMemBufferCopy(MainFile->getBuffer(), |
| 374 | getCurrentFile().c_str()); |
| 375 | |
| 376 | llvm::SMDiagnostic Err; |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 377 | TheModule.reset(ParseIR(MainFileCopy, Err, *VMContext)); |
Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 378 | if (!TheModule) { |
| 379 | // Translate from the diagnostic info to the SourceManager location. |
Argyrios Kyrtzidis | 5a9ee20 | 2011-09-19 20:40:38 +0000 | [diff] [blame] | 380 | SourceLocation Loc = SM.translateFileLineCol( |
Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 381 | SM.getFileEntryForID(SM.getMainFileID()), Err.getLineNo(), |
| 382 | Err.getColumnNo() + 1); |
| 383 | |
| 384 | // Get a custom diagnostic for the error. We strip off a leading |
| 385 | // diagnostic code if there is one. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 386 | StringRef Msg = Err.getMessage(); |
Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 387 | if (Msg.startswith("error: ")) |
| 388 | Msg = Msg.substr(7); |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 389 | unsigned DiagID = CI.getDiagnostics().getCustomDiagID( |
| 390 | DiagnosticsEngine::Error, Msg); |
Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 391 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 392 | CI.getDiagnostics().Report(Loc, DiagID); |
Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 393 | return; |
| 394 | } |
| 395 | |
| 396 | EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(), |
Dan Gohman | b18b8ad | 2011-07-05 22:02:36 +0000 | [diff] [blame] | 397 | CI.getTargetOpts(), CI.getLangOpts(), |
| 398 | TheModule.get(), |
Daniel Dunbar | 4cbbd94 | 2010-06-07 23:27:59 +0000 | [diff] [blame] | 399 | BA, OS); |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | // Otherwise follow the normal AST path. |
| 404 | this->ASTFrontendAction::ExecuteAction(); |
| 405 | } |
| 406 | |
| 407 | // |
| 408 | |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 409 | EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext) |
| 410 | : CodeGenAction(Backend_EmitAssembly, _VMContext) {} |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 411 | |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 412 | EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext) |
| 413 | : CodeGenAction(Backend_EmitBC, _VMContext) {} |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 414 | |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 415 | EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext) |
| 416 | : CodeGenAction(Backend_EmitLL, _VMContext) {} |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 417 | |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 418 | EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext) |
| 419 | : CodeGenAction(Backend_EmitNothing, _VMContext) {} |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 420 | |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 421 | EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext) |
| 422 | : CodeGenAction(Backend_EmitMCNull, _VMContext) {} |
Daniel Dunbar | 32148ce | 2010-05-25 18:41:01 +0000 | [diff] [blame] | 423 | |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 424 | EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext) |
| 425 | : CodeGenAction(Backend_EmitObj, _VMContext) {} |