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 | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 10 | #include "clang/Frontend/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" |
| 13 | #include "clang/Basic/TargetOptions.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 | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 17 | #include "clang/CodeGen/CodeGenOptions.h" |
| 18 | #include "clang/CodeGen/ModuleBuilder.h" |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/ASTConsumers.h" |
| 20 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/FrontendDiagnostic.h" |
Chris Lattner | cabae68 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 22 | #include "llvm/LLVMContext.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 24 | #include "llvm/PassManager.h" |
| 25 | #include "llvm/ADT/OwningPtr.h" |
| 26 | #include "llvm/Assembly/PrintModulePass.h" |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/CallGraph.h" |
| 28 | #include "llvm/Analysis/Verifier.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 29 | #include "llvm/Bitcode/ReaderWriter.h" |
| 30 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 31 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Chris Lattner | 03eacc7 | 2009-07-14 20:39:15 +0000 | [diff] [blame] | 32 | #include "llvm/Support/FormattedStream.h" |
Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 33 | #include "llvm/Support/MemoryBuffer.h" |
| 34 | #include "llvm/Support/SourceMgr.h" |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 35 | #include "llvm/Support/StandardPasses.h" |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Timer.h" |
Daniel Dunbar | a034ba8 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 37 | #include "llvm/Target/SubtargetFeature.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetData.h" |
| 39 | #include "llvm/Target/TargetMachine.h" |
Daniel Dunbar | 821e2eb | 2009-12-12 23:01:36 +0000 | [diff] [blame] | 40 | #include "llvm/Target/TargetOptions.h" |
Daniel Dunbar | f7d47c0 | 2009-07-15 20:25:38 +0000 | [diff] [blame] | 41 | #include "llvm/Target/TargetRegistry.h" |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 42 | using namespace clang; |
| 43 | using namespace llvm; |
| 44 | |
| 45 | namespace { |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 46 | enum BackendAction { |
| 47 | Backend_EmitAssembly, ///< Emit native assembly files |
| 48 | Backend_EmitBC, ///< Emit LLVM bitcode files |
| 49 | Backend_EmitLL, ///< Emit human-readable LLVM assembly |
| 50 | Backend_EmitNothing, ///< Don't emit anything (benchmarking mode) |
| 51 | Backend_EmitObj ///< Emit native object files |
| 52 | }; |
| 53 | |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 54 | class BackendConsumer : public ASTConsumer { |
Daniel Dunbar | 125bbbe | 2009-12-04 08:17:40 +0000 | [diff] [blame] | 55 | Diagnostic &Diags; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 56 | BackendAction Action; |
Daniel Dunbar | 3636e1d | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 57 | const CodeGenOptions &CodeGenOpts; |
| 58 | const LangOptions &LangOpts; |
| 59 | const TargetOptions &TargetOpts; |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 60 | llvm::raw_ostream *AsmOutStream; |
Chris Lattner | 03eacc7 | 2009-07-14 20:39:15 +0000 | [diff] [blame] | 61 | llvm::formatted_raw_ostream FormattedOutStream; |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 62 | ASTContext *Context; |
Daniel Dunbar | 90f4130 | 2008-10-29 08:50:02 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 64 | Timer LLVMIRGeneration; |
| 65 | Timer CodeGenerationTime; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 67 | llvm::OwningPtr<CodeGenerator> Gen; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 69 | llvm::OwningPtr<llvm::Module> TheModule; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 70 | llvm::TargetData *TheTargetData; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 71 | |
| 72 | mutable FunctionPassManager *CodeGenPasses; |
| 73 | mutable PassManager *PerModulePasses; |
| 74 | mutable FunctionPassManager *PerFunctionPasses; |
| 75 | |
| 76 | FunctionPassManager *getCodeGenPasses() const; |
| 77 | PassManager *getPerModulePasses() const; |
| 78 | FunctionPassManager *getPerFunctionPasses() const; |
| 79 | |
| 80 | void CreatePasses(); |
| 81 | |
Daniel Dunbar | 125bbbe | 2009-12-04 08:17:40 +0000 | [diff] [blame] | 82 | /// AddEmitPasses - Add passes necessary to emit assembly or LLVM IR. |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 83 | /// |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 84 | /// \return True on success. |
| 85 | bool AddEmitPasses(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 86 | |
| 87 | void EmitAssembly(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | |
| 89 | public: |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 90 | BackendConsumer(BackendAction action, Diagnostic &_Diags, |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 91 | const LangOptions &langopts, const CodeGenOptions &compopts, |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 92 | const TargetOptions &targetopts, bool TimePasses, |
| 93 | const std::string &infile, llvm::raw_ostream *OS, |
Chris Lattner | cabae68 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 94 | LLVMContext &C) : |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 95 | Diags(_Diags), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | Action(action), |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 97 | CodeGenOpts(compopts), |
Daniel Dunbar | 3636e1d | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 98 | LangOpts(langopts), |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 99 | TargetOpts(targetopts), |
Chris Lattner | 03eacc7 | 2009-07-14 20:39:15 +0000 | [diff] [blame] | 100 | AsmOutStream(OS), |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 101 | LLVMIRGeneration("LLVM IR Generation Time"), |
| 102 | CodeGenerationTime("Code Generation Time"), |
John McCall | 468ec6c | 2010-03-04 04:29:44 +0000 | [diff] [blame] | 103 | Gen(CreateLLVMCodeGen(Diags, infile, compopts, C)), |
| 104 | TheTargetData(0), |
Chris Lattner | 4450266 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 105 | CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 03eacc7 | 2009-07-14 20:39:15 +0000 | [diff] [blame] | 107 | if (AsmOutStream) |
| 108 | FormattedOutStream.setStream(*AsmOutStream, |
| 109 | formatted_raw_ostream::PRESERVE_STREAM); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 111 | llvm::TimePassesIsEnabled = TimePasses; |
Chris Lattner | 4450266 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 112 | } |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 113 | |
| 114 | ~BackendConsumer() { |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 115 | delete TheTargetData; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 116 | delete CodeGenPasses; |
| 117 | delete PerModulePasses; |
| 118 | delete PerFunctionPasses; |
| 119 | } |
| 120 | |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 121 | llvm::Module *takeModule() { return TheModule.take(); } |
| 122 | |
Chris Lattner | 7bb0da0 | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 123 | virtual void Initialize(ASTContext &Ctx) { |
| 124 | Context = &Ctx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 126 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 127 | LLVMIRGeneration.startTimer(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Chris Lattner | 7bb0da0 | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 129 | Gen->Initialize(Ctx); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 130 | |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 131 | TheModule.reset(Gen->GetModule()); |
Chris Lattner | 7bb0da0 | 2009-03-28 02:18:25 +0000 | [diff] [blame] | 132 | TheTargetData = new llvm::TargetData(Ctx.Target.getTargetDescription()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 134 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 135 | LLVMIRGeneration.stopTimer(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 136 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 138 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 139 | PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(), |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 140 | Context->getSourceManager(), |
| 141 | "LLVM IR generation of declaration"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 143 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 144 | LLVMIRGeneration.startTimer(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 145 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 146 | Gen->HandleTopLevelDecl(D); |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 147 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 148 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 149 | LLVMIRGeneration.stopTimer(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 150 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 152 | virtual void HandleTranslationUnit(ASTContext &C) { |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 153 | { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 154 | PrettyStackTraceString CrashInfo("Per-file LLVM IR generation"); |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 155 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 156 | LLVMIRGeneration.startTimer(); |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 157 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 158 | Gen->HandleTranslationUnit(C); |
Daniel Dunbar | d68ba0e | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 159 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 160 | if (llvm::TimePassesIsEnabled) |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 161 | LLVMIRGeneration.stopTimer(); |
| 162 | } |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 163 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 164 | // EmitAssembly times and registers crash info itself. |
Chris Lattner | 6f114eb | 2009-02-18 01:37:30 +0000 | [diff] [blame] | 165 | EmitAssembly(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Daniel Dunbar | d68ba0e | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 167 | // Force a flush here in case we never get released. |
| 168 | if (AsmOutStream) |
Chris Lattner | 03eacc7 | 2009-07-14 20:39:15 +0000 | [diff] [blame] | 169 | FormattedOutStream.flush(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 170 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 172 | virtual void HandleTagDeclDefinition(TagDecl *D) { |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 173 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 174 | Context->getSourceManager(), |
| 175 | "LLVM IR generation of declaration"); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 176 | Gen->HandleTagDeclDefinition(D); |
| 177 | } |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 178 | |
| 179 | virtual void CompleteTentativeDefinition(VarDecl *D) { |
| 180 | Gen->CompleteTentativeDefinition(D); |
| 181 | } |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 182 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame^] | 183 | virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) { |
| 184 | Gen->HandleVTable(RD, DefinitionRequired); |
| 185 | } |
| 186 | |
Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 187 | static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context, |
| 188 | unsigned LocCookie) { |
| 189 | SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie); |
| 190 | ((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc); |
| 191 | } |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 193 | void InlineAsmDiagHandler2(const llvm::SMDiagnostic &, |
| 194 | SourceLocation LocCookie); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | }; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | FunctionPassManager *BackendConsumer::getCodeGenPasses() const { |
| 199 | if (!CodeGenPasses) { |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 200 | CodeGenPasses = new FunctionPassManager(&*TheModule); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 201 | CodeGenPasses->add(new TargetData(*TheTargetData)); |
| 202 | } |
| 203 | |
| 204 | return CodeGenPasses; |
| 205 | } |
| 206 | |
| 207 | PassManager *BackendConsumer::getPerModulePasses() const { |
| 208 | if (!PerModulePasses) { |
| 209 | PerModulePasses = new PassManager(); |
| 210 | PerModulePasses->add(new TargetData(*TheTargetData)); |
| 211 | } |
| 212 | |
| 213 | return PerModulePasses; |
| 214 | } |
| 215 | |
| 216 | FunctionPassManager *BackendConsumer::getPerFunctionPasses() const { |
| 217 | if (!PerFunctionPasses) { |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 218 | PerFunctionPasses = new FunctionPassManager(&*TheModule); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 219 | PerFunctionPasses->add(new TargetData(*TheTargetData)); |
| 220 | } |
| 221 | |
| 222 | return PerFunctionPasses; |
| 223 | } |
| 224 | |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 225 | bool BackendConsumer::AddEmitPasses() { |
Daniel Dunbar | e8e2600 | 2009-02-26 22:39:37 +0000 | [diff] [blame] | 226 | if (Action == Backend_EmitNothing) |
| 227 | return true; |
| 228 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 229 | if (Action == Backend_EmitBC) { |
Dan Gohman | b8d4239 | 2009-09-26 15:06:14 +0000 | [diff] [blame] | 230 | getPerModulePasses()->add(createBitcodeWriterPass(FormattedOutStream)); |
Chris Lattner | cabae68 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 231 | return true; |
| 232 | } |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 233 | |
Chris Lattner | cabae68 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 234 | if (Action == Backend_EmitLL) { |
Dan Gohman | b8d4239 | 2009-09-26 15:06:14 +0000 | [diff] [blame] | 235 | getPerModulePasses()->add(createPrintModulePass(&FormattedOutStream)); |
Chris Lattner | cabae68 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 236 | return true; |
| 237 | } |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 238 | |
Chris Lattner | cabae68 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 239 | bool Fast = CodeGenOpts.OptimizationLevel == 0; |
| 240 | |
| 241 | // Create the TargetMachine for generating code. |
| 242 | std::string Error; |
| 243 | std::string Triple = TheModule->getTargetTriple(); |
| 244 | const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); |
| 245 | if (!TheTarget) { |
| 246 | Diags.Report(diag::err_fe_unable_to_create_target) << Error; |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | // FIXME: Expose these capabilities via actual APIs!!!! Aside from just |
| 251 | // being gross, this is also totally broken if we ever care about |
| 252 | // concurrency. |
| 253 | llvm::NoFramePointerElim = CodeGenOpts.DisableFPElim; |
| 254 | if (CodeGenOpts.FloatABI == "soft") |
| 255 | llvm::FloatABIType = llvm::FloatABI::Soft; |
| 256 | else if (CodeGenOpts.FloatABI == "hard") |
| 257 | llvm::FloatABIType = llvm::FloatABI::Hard; |
| 258 | else { |
| 259 | assert(CodeGenOpts.FloatABI.empty() && "Invalid float abi!"); |
| 260 | llvm::FloatABIType = llvm::FloatABI::Default; |
| 261 | } |
| 262 | NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS; |
| 263 | llvm::UseSoftFloat = CodeGenOpts.SoftFloat; |
| 264 | UnwindTablesMandatory = CodeGenOpts.UnwindTables; |
| 265 | |
| 266 | TargetMachine::setAsmVerbosityDefault(CodeGenOpts.AsmVerbose); |
| 267 | |
Chris Lattner | bbea716 | 2010-04-13 00:38:24 +0000 | [diff] [blame] | 268 | TargetMachine::setFunctionSections(CodeGenOpts.FunctionSections); |
| 269 | TargetMachine::setDataSections (CodeGenOpts.DataSections); |
| 270 | |
Chris Lattner | cabae68 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 271 | // FIXME: Parse this earlier. |
| 272 | if (CodeGenOpts.RelocationModel == "static") { |
| 273 | TargetMachine::setRelocationModel(llvm::Reloc::Static); |
| 274 | } else if (CodeGenOpts.RelocationModel == "pic") { |
| 275 | TargetMachine::setRelocationModel(llvm::Reloc::PIC_); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 276 | } else { |
Chris Lattner | cabae68 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 277 | assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" && |
| 278 | "Invalid PIC model!"); |
| 279 | TargetMachine::setRelocationModel(llvm::Reloc::DynamicNoPIC); |
| 280 | } |
| 281 | // FIXME: Parse this earlier. |
| 282 | if (CodeGenOpts.CodeModel == "small") { |
| 283 | TargetMachine::setCodeModel(llvm::CodeModel::Small); |
| 284 | } else if (CodeGenOpts.CodeModel == "kernel") { |
| 285 | TargetMachine::setCodeModel(llvm::CodeModel::Kernel); |
| 286 | } else if (CodeGenOpts.CodeModel == "medium") { |
| 287 | TargetMachine::setCodeModel(llvm::CodeModel::Medium); |
| 288 | } else if (CodeGenOpts.CodeModel == "large") { |
| 289 | TargetMachine::setCodeModel(llvm::CodeModel::Large); |
| 290 | } else { |
| 291 | assert(CodeGenOpts.CodeModel.empty() && "Invalid code model!"); |
| 292 | TargetMachine::setCodeModel(llvm::CodeModel::Default); |
| 293 | } |
Daniel Dunbar | 4c877cc | 2008-10-23 05:59:43 +0000 | [diff] [blame] | 294 | |
Chris Lattner | cabae68 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 295 | std::vector<const char *> BackendArgs; |
| 296 | BackendArgs.push_back("clang"); // Fake program name. |
| 297 | if (!CodeGenOpts.DebugPass.empty()) { |
| 298 | BackendArgs.push_back("-debug-pass"); |
| 299 | BackendArgs.push_back(CodeGenOpts.DebugPass.c_str()); |
| 300 | } |
| 301 | if (!CodeGenOpts.LimitFloatPrecision.empty()) { |
| 302 | BackendArgs.push_back("-limit-float-precision"); |
| 303 | BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str()); |
| 304 | } |
| 305 | if (llvm::TimePassesIsEnabled) |
| 306 | BackendArgs.push_back("-time-passes"); |
| 307 | BackendArgs.push_back(0); |
| 308 | llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1, |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 309 | const_cast<char **>(&BackendArgs[0])); |
John McCall | 468ec6c | 2010-03-04 04:29:44 +0000 | [diff] [blame] | 310 | |
Chris Lattner | cabae68 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 311 | std::string FeaturesStr; |
| 312 | if (TargetOpts.CPU.size() || TargetOpts.Features.size()) { |
| 313 | SubtargetFeatures Features; |
| 314 | Features.setCPU(TargetOpts.CPU); |
| 315 | for (std::vector<std::string>::const_iterator |
| 316 | it = TargetOpts.Features.begin(), |
| 317 | ie = TargetOpts.Features.end(); it != ie; ++it) |
| 318 | Features.AddFeature(*it); |
| 319 | FeaturesStr = Features.getString(); |
| 320 | } |
| 321 | TargetMachine *TM = TheTarget->createTargetMachine(Triple, FeaturesStr); |
Daniel Dunbar | 821e2eb | 2009-12-12 23:01:36 +0000 | [diff] [blame] | 322 | |
Chris Lattner | cabae68 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 323 | // Set register scheduler & allocation policy. |
| 324 | RegisterScheduler::setDefault(createDefaultScheduler); |
| 325 | RegisterRegAlloc::setDefault(Fast ? createLocalRegisterAllocator : |
| 326 | createLinearScanRegisterAllocator); |
John McCall | 468ec6c | 2010-03-04 04:29:44 +0000 | [diff] [blame] | 327 | |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 328 | // Create the code generator passes. |
Chris Lattner | cabae68 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 329 | FunctionPassManager *PM = getCodeGenPasses(); |
| 330 | CodeGenOpt::Level OptLevel = CodeGenOpt::Default; |
John McCall | 468ec6c | 2010-03-04 04:29:44 +0000 | [diff] [blame] | 331 | |
Chris Lattner | cabae68 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 332 | switch (CodeGenOpts.OptimizationLevel) { |
| 333 | default: break; |
| 334 | case 0: OptLevel = CodeGenOpt::None; break; |
| 335 | case 3: OptLevel = CodeGenOpt::Aggressive; break; |
| 336 | } |
Daniel Dunbar | f219e7c | 2009-11-29 07:18:39 +0000 | [diff] [blame] | 337 | |
Chris Lattner | cabae68 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 338 | // Normal mode, emit a .s or .o file by running the code generator. Note, |
| 339 | // this also adds codegenerator level optimization passes. |
| 340 | TargetMachine::CodeGenFileType CGFT = TargetMachine::CGFT_AssemblyFile; |
| 341 | if (Action == Backend_EmitObj) |
| 342 | CGFT = TargetMachine::CGFT_ObjectFile; |
| 343 | if (TM->addPassesToEmitFile(*PM, FormattedOutStream, CGFT, OptLevel, |
Daniel Dunbar | 8a5e83c | 2010-04-29 16:29:06 +0000 | [diff] [blame] | 344 | /*DisableVerify=*/!CodeGenOpts.VerifyModule)) { |
Chris Lattner | cabae68 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 345 | Diags.Report(diag::err_fe_unable_to_interface_with_target); |
| 346 | return false; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | return true; |
| 350 | } |
| 351 | |
| 352 | void BackendConsumer::CreatePasses() { |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 353 | unsigned OptLevel = CodeGenOpts.OptimizationLevel; |
| 354 | CodeGenOptions::InliningMethod Inlining = CodeGenOpts.Inlining; |
Daniel Dunbar | 8d35314 | 2009-11-10 17:50:53 +0000 | [diff] [blame] | 355 | |
| 356 | // Handle disabling of LLVM optimization, where we want to preserve the |
| 357 | // internal module before any optimization. |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 358 | if (CodeGenOpts.DisableLLVMOpts) { |
Daniel Dunbar | 8d35314 | 2009-11-10 17:50:53 +0000 | [diff] [blame] | 359 | OptLevel = 0; |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 360 | Inlining = CodeGenOpts.NoInlining; |
Daniel Dunbar | 8d35314 | 2009-11-10 17:50:53 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 363 | // In -O0 if checking is disabled, we don't even have per-function passes. |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 364 | if (CodeGenOpts.VerifyModule) |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 365 | getPerFunctionPasses()->add(createVerifierPass()); |
| 366 | |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 367 | // Assume that standard function passes aren't run for -O0. |
Daniel Dunbar | 8d35314 | 2009-11-10 17:50:53 +0000 | [diff] [blame] | 368 | if (OptLevel > 0) |
| 369 | llvm::createStandardFunctionPasses(getPerFunctionPasses(), OptLevel); |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 370 | |
| 371 | llvm::Pass *InliningPass = 0; |
Daniel Dunbar | 8d35314 | 2009-11-10 17:50:53 +0000 | [diff] [blame] | 372 | switch (Inlining) { |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 373 | case CodeGenOptions::NoInlining: break; |
| 374 | case CodeGenOptions::NormalInlining: { |
Daniel Dunbar | 90de51f | 2009-12-08 23:15:55 +0000 | [diff] [blame] | 375 | // Set the inline threshold following llvm-gcc. |
| 376 | // |
| 377 | // FIXME: Derive these constants in a principled fashion. |
Daniel Dunbar | 9da5598 | 2010-02-05 07:32:37 +0000 | [diff] [blame] | 378 | unsigned Threshold = 225; |
Daniel Dunbar | 90de51f | 2009-12-08 23:15:55 +0000 | [diff] [blame] | 379 | if (CodeGenOpts.OptimizeSize) |
Daniel Dunbar | 9da5598 | 2010-02-05 07:32:37 +0000 | [diff] [blame] | 380 | Threshold = 75; |
Daniel Dunbar | 90de51f | 2009-12-08 23:15:55 +0000 | [diff] [blame] | 381 | else if (OptLevel > 2) |
Daniel Dunbar | 9da5598 | 2010-02-05 07:32:37 +0000 | [diff] [blame] | 382 | Threshold = 275; |
Eli Friedman | b9b7dd6 | 2009-06-11 20:33:41 +0000 | [diff] [blame] | 383 | InliningPass = createFunctionInliningPass(Threshold); |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 384 | break; |
Eli Friedman | b9b7dd6 | 2009-06-11 20:33:41 +0000 | [diff] [blame] | 385 | } |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 386 | case CodeGenOptions::OnlyAlwaysInlining: |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 387 | InliningPass = createAlwaysInlinerPass(); // Respect always_inline |
| 388 | break; |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | // For now we always create per module passes. |
| 392 | PassManager *PM = getPerModulePasses(); |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 393 | llvm::createStandardModulePasses(PM, OptLevel, CodeGenOpts.OptimizeSize, |
| 394 | CodeGenOpts.UnitAtATime, |
| 395 | CodeGenOpts.UnrollLoops, |
Daniel Dunbar | 3636e1d | 2009-11-30 08:39:32 +0000 | [diff] [blame] | 396 | /*SimplifyLibCalls=*/!LangOpts.NoBuiltin, |
Daniel Dunbar | 10d861e | 2009-06-03 18:01:18 +0000 | [diff] [blame] | 397 | /*HaveExceptions=*/true, |
| 398 | InliningPass); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /// EmitAssembly - Handle interaction with LLVM backend to generate |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 402 | /// actual machine code. |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 403 | void BackendConsumer::EmitAssembly() { |
| 404 | // Silently ignore if we weren't initialized for some reason. |
| 405 | if (!TheModule || !TheTargetData) |
| 406 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 408 | TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : 0); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 409 | |
Daniel Dunbar | d611bac | 2008-10-27 20:40:41 +0000 | [diff] [blame] | 410 | // Make sure IR generation is happy with the module. This is |
| 411 | // released by the module provider. |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 412 | Module *M = Gen->ReleaseModule(); |
| 413 | if (!M) { |
Daniel Dunbar | d611bac | 2008-10-27 20:40:41 +0000 | [diff] [blame] | 414 | // The module has been released by IR gen on failures, do not |
| 415 | // double free. |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 416 | TheModule.take(); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 417 | return; |
| 418 | } |
| 419 | |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 420 | assert(TheModule.get() == M && |
| 421 | "Unexpected module change during IR generation"); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 422 | |
| 423 | CreatePasses(); |
Daniel Dunbar | 3be0d19 | 2009-12-03 09:12:54 +0000 | [diff] [blame] | 424 | if (!AddEmitPasses()) |
| 425 | return; |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 426 | |
| 427 | // Run passes. For now we do all passes at once, but eventually we |
| 428 | // would like to have the option of streaming code generation. |
| 429 | |
| 430 | if (PerFunctionPasses) { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 431 | PrettyStackTraceString CrashInfo("Per-function optimization"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 433 | PerFunctionPasses->doInitialization(); |
| 434 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 435 | if (!I->isDeclaration()) |
| 436 | PerFunctionPasses->run(*I); |
| 437 | PerFunctionPasses->doFinalization(); |
| 438 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 440 | if (PerModulePasses) { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 441 | PrettyStackTraceString CrashInfo("Per-module optimization passes"); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 442 | PerModulePasses->run(*M); |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 443 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 445 | if (CodeGenPasses) { |
Chris Lattner | 14f234e | 2009-03-06 06:46:31 +0000 | [diff] [blame] | 446 | PrettyStackTraceString CrashInfo("Code generation"); |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 447 | |
Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 448 | // Install an inline asm handler so that diagnostics get printed through our |
| 449 | // diagnostics hooks. |
| 450 | LLVMContext &Ctx = TheModule->getContext(); |
| 451 | void *OldHandler = Ctx.getInlineAsmDiagnosticHandler(); |
| 452 | void *OldContext = Ctx.getInlineAsmDiagnosticContext(); |
| 453 | Ctx.setInlineAsmDiagnosticHandler((void*)(intptr_t)InlineAsmDiagHandler, |
| 454 | this); |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 455 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 456 | CodeGenPasses->doInitialization(); |
| 457 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 458 | if (!I->isDeclaration()) |
| 459 | CodeGenPasses->run(*I); |
| 460 | CodeGenPasses->doFinalization(); |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 461 | |
Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 462 | Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 466 | /// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr |
| 467 | /// buffer to be a valid FullSourceLoc. |
| 468 | static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D, |
| 469 | SourceManager &CSM) { |
| 470 | // Get both the clang and llvm source managers. The location is relative to |
| 471 | // 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] | 472 | // a copy to the Clang source manager. |
Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 473 | const llvm::SourceMgr &LSM = *D.getSourceMgr(); |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 474 | |
Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 475 | // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr |
| 476 | // already owns its one and clang::SourceManager wants to own its one. |
| 477 | const MemoryBuffer *LBuf = |
| 478 | LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc())); |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 479 | |
Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 480 | // Create the copy and transfer ownership to clang::SourceManager. |
| 481 | llvm::MemoryBuffer *CBuf = |
| 482 | llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(), |
| 483 | LBuf->getBufferIdentifier()); |
| 484 | FileID FID = CSM.createFileIDForMemBuffer(CBuf); |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 485 | |
Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 486 | // Translate the offset into the file. |
| 487 | unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart(); |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 488 | SourceLocation NewLoc = |
Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 489 | CSM.getLocForStartOfFile(FID).getFileLocWithOffset(Offset); |
| 490 | return FullSourceLoc(NewLoc, CSM); |
| 491 | } |
| 492 | |
Chris Lattner | cabae68 | 2010-04-06 17:52:14 +0000 | [diff] [blame] | 493 | |
Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 494 | /// InlineAsmDiagHandler2 - This function is invoked when the backend hits an |
| 495 | /// error parsing inline asm. The SMDiagnostic indicates the error relative to |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 496 | /// the temporary memory buffer that the inline asm parser has set up. |
Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 497 | void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D, |
| 498 | SourceLocation LocCookie) { |
| 499 | // There are a couple of different kinds of errors we could get here. First, |
| 500 | // we re-format the SMDiagnostic in terms of a clang diagnostic. |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 501 | |
Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 502 | // Strip "error: " off the start of the message string. |
| 503 | llvm::StringRef Message = D.getMessage(); |
| 504 | if (Message.startswith("error: ")) |
| 505 | Message = Message.substr(7); |
| 506 | |
| 507 | // There are two cases: the SMDiagnostic could have a inline asm source |
| 508 | // location or it might not. If it does, translate the location. |
| 509 | FullSourceLoc Loc; |
Chris Lattner | d6f1906 | 2010-04-08 00:23:06 +0000 | [diff] [blame] | 510 | if (D.getLoc() != SMLoc()) |
| 511 | Loc = ConvertBackendLocation(D, Context->getSourceManager()); |
Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 512 | Diags.Report(Loc, diag::err_fe_inline_asm).AddString(Message); |
Daniel Dunbar | f80cb75 | 2010-04-29 16:29:09 +0000 | [diff] [blame] | 513 | |
Chris Lattner | 6da9eb6 | 2010-04-06 18:38:50 +0000 | [diff] [blame] | 514 | // This could be a problem with no clang-level source location information. |
| 515 | // In this case, LocCookie is invalid. If there is source level information, |
| 516 | // print an "generated from" note. |
| 517 | if (LocCookie.isValid()) |
| 518 | Diags.Report(FullSourceLoc(LocCookie, Context->getSourceManager()), |
| 519 | diag::note_fe_inline_asm_here); |
| 520 | } |
| 521 | |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 522 | // |
| 523 | |
| 524 | CodeGenAction::CodeGenAction(unsigned _Act) : Act(_Act) {} |
| 525 | |
Daniel Dunbar | 9ad1c02 | 2010-02-25 20:37:44 +0000 | [diff] [blame] | 526 | CodeGenAction::~CodeGenAction() {} |
| 527 | |
Daniel Dunbar | b954e98 | 2010-02-25 04:37:50 +0000 | [diff] [blame] | 528 | void CodeGenAction::EndSourceFileAction() { |
| 529 | // If the consumer creation failed, do nothing. |
| 530 | if (!getCompilerInstance().hasASTConsumer()) |
| 531 | return; |
| 532 | |
| 533 | // Steal the module from the consumer. |
| 534 | BackendConsumer *Consumer = static_cast<BackendConsumer*>( |
| 535 | &getCompilerInstance().getASTConsumer()); |
| 536 | |
| 537 | TheModule.reset(Consumer->takeModule()); |
| 538 | } |
| 539 | |
| 540 | llvm::Module *CodeGenAction::takeModule() { |
| 541 | return TheModule.take(); |
| 542 | } |
| 543 | |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 544 | ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI, |
| 545 | llvm::StringRef InFile) { |
| 546 | BackendAction BA = static_cast<BackendAction>(Act); |
| 547 | llvm::OwningPtr<llvm::raw_ostream> OS; |
| 548 | switch (BA) { |
| 549 | case Backend_EmitAssembly: |
| 550 | OS.reset(CI.createDefaultOutputFile(false, InFile, "s")); |
| 551 | break; |
| 552 | case Backend_EmitLL: |
| 553 | OS.reset(CI.createDefaultOutputFile(false, InFile, "ll")); |
| 554 | break; |
| 555 | case Backend_EmitBC: |
| 556 | OS.reset(CI.createDefaultOutputFile(true, InFile, "bc")); |
| 557 | break; |
| 558 | case Backend_EmitNothing: |
| 559 | break; |
| 560 | case Backend_EmitObj: |
| 561 | OS.reset(CI.createDefaultOutputFile(true, InFile, "o")); |
| 562 | break; |
| 563 | } |
| 564 | if (BA != Backend_EmitNothing && !OS) |
| 565 | return 0; |
| 566 | |
John McCall | 468ec6c | 2010-03-04 04:29:44 +0000 | [diff] [blame] | 567 | return new BackendConsumer(BA, CI.getDiagnostics(), CI.getLangOpts(), |
| 568 | CI.getCodeGenOpts(), CI.getTargetOpts(), |
| 569 | CI.getFrontendOpts().ShowTimers, InFile, OS.take(), |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 570 | CI.getLLVMContext()); |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 571 | } |
Daniel Dunbar | 4ee3461 | 2010-02-25 04:37:45 +0000 | [diff] [blame] | 572 | |
| 573 | EmitAssemblyAction::EmitAssemblyAction() |
| 574 | : CodeGenAction(Backend_EmitAssembly) {} |
| 575 | |
| 576 | EmitBCAction::EmitBCAction() : CodeGenAction(Backend_EmitBC) {} |
| 577 | |
| 578 | EmitLLVMAction::EmitLLVMAction() : CodeGenAction(Backend_EmitLL) {} |
| 579 | |
| 580 | EmitLLVMOnlyAction::EmitLLVMOnlyAction() : CodeGenAction(Backend_EmitNothing) {} |
| 581 | |
| 582 | EmitObjAction::EmitObjAction() : CodeGenAction(Backend_EmitObj) {} |