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