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