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