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