2010-02-25 12:37:45 +08:00
|
|
|
//===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===//
|
2008-10-22 07:49:24 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-06-16 01:48:49 +08:00
|
|
|
#include "clang/CodeGen/CodeGenAction.h"
|
2011-10-31 01:30:44 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2010-04-07 02:38:50 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2008-10-22 07:49:24 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2009-11-15 14:48:46 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-03-30 00:50:03 +08:00
|
|
|
#include "clang/AST/DeclGroup.h"
|
2010-06-16 01:48:49 +08:00
|
|
|
#include "clang/CodeGen/BackendUtil.h"
|
2009-11-15 14:48:46 +08:00
|
|
|
#include "clang/CodeGen/ModuleBuilder.h"
|
2010-02-25 12:37:45 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
2009-12-03 17:12:54 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2010-04-07 01:52:14 +08:00
|
|
|
#include "llvm/LLVMContext.h"
|
2011-10-31 01:30:44 +08:00
|
|
|
#include "llvm/Linker.h"
|
2008-10-22 07:49:24 +08:00
|
|
|
#include "llvm/Module.h"
|
2010-06-08 07:21:04 +08:00
|
|
|
#include "llvm/Pass.h"
|
2008-10-22 07:49:24 +08:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2011-10-31 01:30:44 +08:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2010-06-08 07:27:59 +08:00
|
|
|
#include "llvm/Support/IRReader.h"
|
2010-04-07 02:38:50 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2009-02-18 09:37:30 +08:00
|
|
|
#include "llvm/Support/Timer.h"
|
2008-10-22 07:49:24 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace llvm;
|
|
|
|
|
2011-01-26 04:34:14 +08:00
|
|
|
namespace clang {
|
2009-11-28 18:07:24 +08:00
|
|
|
class BackendConsumer : public ASTConsumer {
|
2011-12-20 10:48:34 +08:00
|
|
|
virtual void anchor();
|
2011-09-26 07:23:43 +08:00
|
|
|
DiagnosticsEngine &Diags;
|
2008-10-22 07:49:24 +08:00
|
|
|
BackendAction Action;
|
2009-11-30 16:39:32 +08:00
|
|
|
const CodeGenOptions &CodeGenOpts;
|
|
|
|
const TargetOptions &TargetOpts;
|
2011-07-06 06:02:36 +08:00
|
|
|
const LangOptions &LangOpts;
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream *AsmOutStream;
|
2009-03-05 16:00:35 +08:00
|
|
|
ASTContext *Context;
|
2008-10-29 16:50:02 +08:00
|
|
|
|
2009-02-18 09:37:30 +08:00
|
|
|
Timer LLVMIRGeneration;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<CodeGenerator> Gen;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<llvm::Module> TheModule, LinkModule;
|
2008-10-22 07:49:24 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
public:
|
2011-09-26 07:23:43 +08:00
|
|
|
BackendConsumer(BackendAction action, DiagnosticsEngine &_Diags,
|
2010-06-08 07:19:17 +08:00
|
|
|
const CodeGenOptions &compopts,
|
2011-07-06 06:02:36 +08:00
|
|
|
const TargetOptions &targetopts,
|
|
|
|
const LangOptions &langopts,
|
|
|
|
bool TimePasses,
|
2011-10-31 01:30:44 +08:00
|
|
|
const std::string &infile,
|
|
|
|
llvm::Module *LinkModule,
|
|
|
|
raw_ostream *OS,
|
2010-04-07 01:52:14 +08:00
|
|
|
LLVMContext &C) :
|
2009-12-03 17:12:54 +08:00
|
|
|
Diags(_Diags),
|
2009-09-09 23:08:12 +08:00
|
|
|
Action(action),
|
2009-11-13 01:24:48 +08:00
|
|
|
CodeGenOpts(compopts),
|
2009-11-15 14:48:46 +08:00
|
|
|
TargetOpts(targetopts),
|
2011-07-06 06:02:36 +08:00
|
|
|
LangOpts(langopts),
|
2009-07-15 04:39:15 +08:00
|
|
|
AsmOutStream(OS),
|
2009-02-18 09:37:30 +08:00
|
|
|
LLVMIRGeneration("LLVM IR Generation Time"),
|
2011-10-31 01:30:44 +08:00
|
|
|
Gen(CreateLLVMCodeGen(Diags, infile, compopts, C)),
|
|
|
|
LinkModule(LinkModule) {
|
2009-11-30 16:39:52 +08:00
|
|
|
llvm::TimePassesIsEnabled = TimePasses;
|
2009-02-18 09:23:44 +08:00
|
|
|
}
|
2008-10-22 07:49:24 +08:00
|
|
|
|
2010-02-25 12:37:50 +08:00
|
|
|
llvm::Module *takeModule() { return TheModule.take(); }
|
2011-10-31 01:30:44 +08:00
|
|
|
llvm::Module *takeLinkModule() { return LinkModule.take(); }
|
2010-02-25 12:37:50 +08:00
|
|
|
|
2009-03-28 10:18:25 +08:00
|
|
|
virtual void Initialize(ASTContext &Ctx) {
|
|
|
|
Context = &Ctx;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-30 16:39:52 +08:00
|
|
|
if (llvm::TimePassesIsEnabled)
|
2009-02-18 09:37:30 +08:00
|
|
|
LLVMIRGeneration.startTimer();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-28 10:18:25 +08:00
|
|
|
Gen->Initialize(Ctx);
|
2008-10-22 07:49:24 +08:00
|
|
|
|
2010-02-25 12:37:50 +08:00
|
|
|
TheModule.reset(Gen->GetModule());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-30 16:39:52 +08:00
|
|
|
if (llvm::TimePassesIsEnabled)
|
2009-02-18 09:37:30 +08:00
|
|
|
LLVMIRGeneration.stopTimer();
|
2008-10-22 07:49:24 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-11-18 08:26:59 +08:00
|
|
|
virtual bool HandleTopLevelDecl(DeclGroupRef D) {
|
2009-03-30 00:50:03 +08:00
|
|
|
PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
|
2009-03-05 16:00:35 +08:00
|
|
|
Context->getSourceManager(),
|
|
|
|
"LLVM IR generation of declaration");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-30 16:39:52 +08:00
|
|
|
if (llvm::TimePassesIsEnabled)
|
2009-02-18 09:37:30 +08:00
|
|
|
LLVMIRGeneration.startTimer();
|
2009-03-30 00:50:03 +08:00
|
|
|
|
2008-10-22 07:49:24 +08:00
|
|
|
Gen->HandleTopLevelDecl(D);
|
2009-02-18 09:37:30 +08:00
|
|
|
|
2009-11-30 16:39:52 +08:00
|
|
|
if (llvm::TimePassesIsEnabled)
|
2009-02-18 09:37:30 +08:00
|
|
|
LLVMIRGeneration.stopTimer();
|
2011-11-18 08:26:59 +08:00
|
|
|
|
|
|
|
return true;
|
2008-10-22 07:49:24 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-28 12:11:33 +08:00
|
|
|
virtual void HandleTranslationUnit(ASTContext &C) {
|
2009-03-05 16:00:35 +08:00
|
|
|
{
|
2009-03-06 14:46:31 +08:00
|
|
|
PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
|
2009-11-30 16:39:52 +08:00
|
|
|
if (llvm::TimePassesIsEnabled)
|
2009-03-05 16:00:35 +08:00
|
|
|
LLVMIRGeneration.startTimer();
|
2009-02-18 09:37:30 +08:00
|
|
|
|
2009-03-28 12:11:33 +08:00
|
|
|
Gen->HandleTranslationUnit(C);
|
2008-11-11 14:35:39 +08:00
|
|
|
|
2009-11-30 16:39:52 +08:00
|
|
|
if (llvm::TimePassesIsEnabled)
|
2009-03-05 16:00:35 +08:00
|
|
|
LLVMIRGeneration.stopTimer();
|
|
|
|
}
|
2009-02-18 09:37:30 +08:00
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
// Silently ignore if we weren't initialized for some reason.
|
2010-06-08 07:21:04 +08:00
|
|
|
if (!TheModule)
|
2010-06-08 07:20:08 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Make sure IR generation is happy with the module. This is released by
|
|
|
|
// the module provider.
|
2011-12-01 07:21:26 +08:00
|
|
|
llvm::Module *M = Gen->ReleaseModule();
|
2010-06-08 07:20:08 +08:00
|
|
|
if (!M) {
|
|
|
|
// The module has been released by IR gen on failures, do not double
|
|
|
|
// free.
|
|
|
|
TheModule.take();
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
assert(TheModule.get() == M &&
|
|
|
|
"Unexpected module change during IR generation");
|
|
|
|
|
2011-10-31 01:30:44 +08:00
|
|
|
// Link LinkModule into this module if present, preserving its validity.
|
|
|
|
if (LinkModule) {
|
|
|
|
std::string ErrorMsg;
|
|
|
|
if (Linker::LinkModules(M, LinkModule.get(), Linker::PreserveSource,
|
|
|
|
&ErrorMsg)) {
|
|
|
|
Diags.Report(diag::err_fe_cannot_link_module)
|
|
|
|
<< LinkModule->getModuleIdentifier() << ErrorMsg;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
// Install an inline asm handler so that diagnostics get printed through
|
|
|
|
// our diagnostics hooks.
|
|
|
|
LLVMContext &Ctx = TheModule->getContext();
|
2010-11-17 16:13:04 +08:00
|
|
|
LLVMContext::InlineAsmDiagHandlerTy OldHandler =
|
|
|
|
Ctx.getInlineAsmDiagnosticHandler();
|
2010-06-08 07:20:08 +08:00
|
|
|
void *OldContext = Ctx.getInlineAsmDiagnosticContext();
|
2010-11-17 16:13:04 +08:00
|
|
|
Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this);
|
2010-06-08 07:20:08 +08:00
|
|
|
|
2011-07-06 06:02:36 +08:00
|
|
|
EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
|
2010-06-08 07:21:04 +08:00
|
|
|
TheModule.get(), Action, AsmOutStream);
|
2010-06-08 07:20:08 +08:00
|
|
|
|
|
|
|
Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext);
|
2008-10-22 07:49:24 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-22 07:49:24 +08:00
|
|
|
virtual void HandleTagDeclDefinition(TagDecl *D) {
|
2009-03-05 16:00:35 +08:00
|
|
|
PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
|
|
|
|
Context->getSourceManager(),
|
|
|
|
"LLVM IR generation of declaration");
|
2008-10-22 07:49:24 +08:00
|
|
|
Gen->HandleTagDeclDefinition(D);
|
|
|
|
}
|
2009-04-22 01:11:58 +08:00
|
|
|
|
|
|
|
virtual void CompleteTentativeDefinition(VarDecl *D) {
|
|
|
|
Gen->CompleteTentativeDefinition(D);
|
|
|
|
}
|
2010-04-30 00:29:09 +08:00
|
|
|
|
Rework when and how vtables are emitted, by tracking where vtables are
"used" (e.g., we will refer to the vtable in the generated code) and
when they are defined (i.e., because we've seen the key function
definition). Previously, we were effectively tracking "potential
definitions" rather than uses, so we were a bit too eager about emitting
vtables for classes without key functions.
The new scheme:
- For every use of a vtable, Sema calls MarkVTableUsed() to indicate
the use. For example, this occurs when calling a virtual member
function of the class, defining a constructor of that class type,
dynamic_cast'ing from that type to a derived class, casting
to/through a virtual base class, etc.
- For every definition of a vtable, Sema calls MarkVTableUsed() to
indicate the definition. This happens at the end of the translation
unit for classes whose key function has been defined (so we can
delay computation of the key function; see PR6564), and will also
occur with explicit template instantiation definitions.
- For every vtable defined/used, we mark all of the virtual member
functions of that vtable as defined/used, unless we know that the key
function is in another translation unit. This instantiates virtual
member functions when needed.
- At the end of the translation unit, Sema tells CodeGen (via the
ASTConsumer) which vtables must be defined (CodeGen will define
them) and which may be used (for which CodeGen will define the
vtables lazily).
From a language perspective, both the old and the new schemes are
permissible: we're allowed to instantiate virtual member functions
whenever we want per the standard. However, all other C++ compilers
were more lazy than we were, and our eagerness was both a performance
issue (we instantiated too much) and a portability problem (we broke
Boost test cases, which now pass).
Notes:
(1) There's a ton of churn in the tests, because the order in which
vtables get emitted to IR has changed. I've tried to isolate some of
the larger tests from these issues.
(2) Some diagnostics related to
implicitly-instantiated/implicitly-defined virtual member functions
have moved to the point of first use/definition. It's better this
way.
(3) I could use a review of the places where we MarkVTableUsed, to
see if I missed any place where the language effectively requires a
vtable.
Fixes PR7114 and PR6564.
llvm-svn: 103718
2010-05-14 00:44:06 +08:00
|
|
|
virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
|
|
|
|
Gen->HandleVTable(RD, DefinitionRequired);
|
|
|
|
}
|
|
|
|
|
2010-04-07 02:38:50 +08:00
|
|
|
static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context,
|
|
|
|
unsigned LocCookie) {
|
|
|
|
SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie);
|
|
|
|
((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc);
|
|
|
|
}
|
2010-04-30 00:29:09 +08:00
|
|
|
|
2010-04-07 02:38:50 +08:00
|
|
|
void InlineAsmDiagHandler2(const llvm::SMDiagnostic &,
|
|
|
|
SourceLocation LocCookie);
|
2009-09-09 23:08:12 +08:00
|
|
|
};
|
2011-12-20 10:48:34 +08:00
|
|
|
|
|
|
|
void BackendConsumer::anchor() {}
|
2008-10-22 07:49:24 +08:00
|
|
|
}
|
|
|
|
|
2010-04-08 08:23:06 +08:00
|
|
|
/// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr
|
|
|
|
/// buffer to be a valid FullSourceLoc.
|
|
|
|
static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D,
|
|
|
|
SourceManager &CSM) {
|
|
|
|
// Get both the clang and llvm source managers. The location is relative to
|
|
|
|
// a memory buffer that the LLVM Source Manager is handling, we need to add
|
2010-04-30 00:29:09 +08:00
|
|
|
// a copy to the Clang source manager.
|
2010-04-08 08:23:06 +08:00
|
|
|
const llvm::SourceMgr &LSM = *D.getSourceMgr();
|
2010-04-30 00:29:09 +08:00
|
|
|
|
2010-04-08 08:23:06 +08:00
|
|
|
// We need to copy the underlying LLVM memory buffer because llvm::SourceMgr
|
|
|
|
// already owns its one and clang::SourceManager wants to own its one.
|
|
|
|
const MemoryBuffer *LBuf =
|
|
|
|
LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc()));
|
2010-04-30 00:29:09 +08:00
|
|
|
|
2010-04-08 08:23:06 +08:00
|
|
|
// Create the copy and transfer ownership to clang::SourceManager.
|
|
|
|
llvm::MemoryBuffer *CBuf =
|
|
|
|
llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(),
|
|
|
|
LBuf->getBufferIdentifier());
|
|
|
|
FileID FID = CSM.createFileIDForMemBuffer(CBuf);
|
2010-04-30 00:29:09 +08:00
|
|
|
|
2010-04-08 08:23:06 +08:00
|
|
|
// Translate the offset into the file.
|
|
|
|
unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart();
|
2010-04-30 00:29:09 +08:00
|
|
|
SourceLocation NewLoc =
|
2011-09-20 04:40:19 +08:00
|
|
|
CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset);
|
2010-04-08 08:23:06 +08:00
|
|
|
return FullSourceLoc(NewLoc, CSM);
|
|
|
|
}
|
|
|
|
|
2010-04-07 01:52:14 +08:00
|
|
|
|
2010-04-07 02:38:50 +08:00
|
|
|
/// InlineAsmDiagHandler2 - This function is invoked when the backend hits an
|
|
|
|
/// error parsing inline asm. The SMDiagnostic indicates the error relative to
|
2010-04-30 00:29:09 +08:00
|
|
|
/// the temporary memory buffer that the inline asm parser has set up.
|
2010-04-07 02:38:50 +08:00
|
|
|
void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D,
|
|
|
|
SourceLocation LocCookie) {
|
|
|
|
// There are a couple of different kinds of errors we could get here. First,
|
|
|
|
// we re-format the SMDiagnostic in terms of a clang diagnostic.
|
2012-02-01 14:36:49 +08:00
|
|
|
|
|
|
|
// Strip "error: " off the start of the message string.
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Message = D.getMessage();
|
2012-02-01 14:36:49 +08:00
|
|
|
if (Message.startswith("error: "))
|
|
|
|
Message = Message.substr(7);
|
2010-04-07 02:38:50 +08:00
|
|
|
|
2010-06-15 08:03:12 +08:00
|
|
|
// If the SMDiagnostic has an inline asm source location, translate it.
|
2010-04-07 02:38:50 +08:00
|
|
|
FullSourceLoc Loc;
|
2010-04-08 08:23:06 +08:00
|
|
|
if (D.getLoc() != SMLoc())
|
|
|
|
Loc = ConvertBackendLocation(D, Context->getSourceManager());
|
2012-01-31 14:13:55 +08:00
|
|
|
|
2012-02-01 14:36:49 +08:00
|
|
|
|
2010-06-15 08:03:12 +08:00
|
|
|
// If this problem has clang-level source location information, report the
|
|
|
|
// issue as being an error in the source with a note showing the instantiated
|
|
|
|
// code.
|
|
|
|
if (LocCookie.isValid()) {
|
2010-11-19 04:06:41 +08:00
|
|
|
Diags.Report(LocCookie, diag::err_fe_inline_asm).AddString(Message);
|
2010-06-15 08:03:12 +08:00
|
|
|
|
2011-10-16 18:48:28 +08:00
|
|
|
if (D.getLoc().isValid()) {
|
|
|
|
DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here);
|
|
|
|
// Convert the SMDiagnostic ranges into SourceRange and attach them
|
|
|
|
// to the diagnostic.
|
|
|
|
for (unsigned i = 0, e = D.getRanges().size(); i != e; ++i) {
|
|
|
|
std::pair<unsigned, unsigned> Range = D.getRanges()[i];
|
|
|
|
unsigned Column = D.getColumnNo();
|
|
|
|
B << SourceRange(Loc.getLocWithOffset(Range.first - Column),
|
|
|
|
Loc.getLocWithOffset(Range.second - Column));
|
|
|
|
}
|
|
|
|
}
|
2010-06-15 08:03:12 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-15 13:22:18 +08:00
|
|
|
// Otherwise, report the backend error as occurring in the generated .s file.
|
2010-06-15 08:03:12 +08:00
|
|
|
// If Loc is invalid, we still need to report the error, it just gets no
|
|
|
|
// location info.
|
|
|
|
Diags.Report(Loc, diag::err_fe_inline_asm).AddString(Message);
|
2010-04-07 02:38:50 +08:00
|
|
|
}
|
|
|
|
|
2010-02-25 12:37:45 +08:00
|
|
|
//
|
|
|
|
|
2011-02-20 07:03:58 +08:00
|
|
|
CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext)
|
2011-10-31 01:30:44 +08:00
|
|
|
: Act(_Act), LinkModule(0),
|
|
|
|
VMContext(_VMContext ? _VMContext : new LLVMContext),
|
2011-02-20 07:03:58 +08:00
|
|
|
OwnsVMContext(!_VMContext) {}
|
|
|
|
|
|
|
|
CodeGenAction::~CodeGenAction() {
|
|
|
|
TheModule.reset();
|
|
|
|
if (OwnsVMContext)
|
|
|
|
delete VMContext;
|
|
|
|
}
|
2010-02-26 04:37:44 +08:00
|
|
|
|
2010-06-08 07:27:59 +08:00
|
|
|
bool CodeGenAction::hasIRSupport() const { return true; }
|
|
|
|
|
2010-02-25 12:37:50 +08:00
|
|
|
void CodeGenAction::EndSourceFileAction() {
|
|
|
|
// If the consumer creation failed, do nothing.
|
|
|
|
if (!getCompilerInstance().hasASTConsumer())
|
|
|
|
return;
|
|
|
|
|
2011-10-31 01:30:44 +08:00
|
|
|
// If we were given a link module, release consumer's ownership of it.
|
|
|
|
if (LinkModule)
|
|
|
|
BEConsumer->takeLinkModule();
|
|
|
|
|
2010-02-25 12:37:50 +08:00
|
|
|
// Steal the module from the consumer.
|
2011-01-26 04:34:14 +08:00
|
|
|
TheModule.reset(BEConsumer->takeModule());
|
2010-02-25 12:37:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Module *CodeGenAction::takeModule() {
|
|
|
|
return TheModule.take();
|
|
|
|
}
|
|
|
|
|
2011-02-20 07:03:58 +08:00
|
|
|
llvm::LLVMContext *CodeGenAction::takeLLVMContext() {
|
|
|
|
OwnsVMContext = false;
|
|
|
|
return VMContext;
|
|
|
|
}
|
|
|
|
|
2010-06-08 07:27:59 +08:00
|
|
|
static raw_ostream *GetOutputStream(CompilerInstance &CI,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef InFile,
|
2010-06-08 07:27:59 +08:00
|
|
|
BackendAction Action) {
|
|
|
|
switch (Action) {
|
2010-02-25 12:37:45 +08:00
|
|
|
case Backend_EmitAssembly:
|
2010-06-08 07:27:59 +08:00
|
|
|
return CI.createDefaultOutputFile(false, InFile, "s");
|
2010-02-25 12:37:45 +08:00
|
|
|
case Backend_EmitLL:
|
2010-06-08 07:27:59 +08:00
|
|
|
return CI.createDefaultOutputFile(false, InFile, "ll");
|
2010-02-25 12:37:45 +08:00
|
|
|
case Backend_EmitBC:
|
2010-06-08 07:27:59 +08:00
|
|
|
return CI.createDefaultOutputFile(true, InFile, "bc");
|
2010-02-25 12:37:45 +08:00
|
|
|
case Backend_EmitNothing:
|
2010-06-08 07:27:59 +08:00
|
|
|
return 0;
|
2010-05-26 02:41:01 +08:00
|
|
|
case Backend_EmitMCNull:
|
2010-02-25 12:37:45 +08:00
|
|
|
case Backend_EmitObj:
|
2010-06-08 07:27:59 +08:00
|
|
|
return CI.createDefaultOutputFile(true, InFile, "o");
|
2010-02-25 12:37:45 +08:00
|
|
|
}
|
2010-06-08 07:27:59 +08:00
|
|
|
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Invalid action!");
|
2010-06-08 07:27:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef InFile) {
|
2010-06-08 07:27:59 +08:00
|
|
|
BackendAction BA = static_cast<BackendAction>(Act);
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<raw_ostream> OS(GetOutputStream(CI, InFile, BA));
|
2010-02-25 12:37:45 +08:00
|
|
|
if (BA != Backend_EmitNothing && !OS)
|
|
|
|
return 0;
|
|
|
|
|
2011-10-31 01:30:44 +08:00
|
|
|
llvm::Module *LinkModuleToUse = LinkModule;
|
|
|
|
|
|
|
|
// If we were not given a link module, and the user requested that one be
|
|
|
|
// loaded from bitcode, do so now.
|
|
|
|
const std::string &LinkBCFile = CI.getCodeGenOpts().LinkBitcodeFile;
|
|
|
|
if (!LinkModuleToUse && !LinkBCFile.empty()) {
|
|
|
|
std::string ErrorStr;
|
|
|
|
|
|
|
|
llvm::MemoryBuffer *BCBuf =
|
|
|
|
CI.getFileManager().getBufferForFile(LinkBCFile, &ErrorStr);
|
|
|
|
if (!BCBuf) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_cannot_open_file)
|
|
|
|
<< LinkBCFile << ErrorStr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
LinkModuleToUse = getLazyBitcodeModule(BCBuf, *VMContext, &ErrorStr);
|
|
|
|
if (!LinkModuleToUse) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_cannot_open_file)
|
|
|
|
<< LinkBCFile << ErrorStr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-26 04:34:14 +08:00
|
|
|
BEConsumer =
|
|
|
|
new BackendConsumer(BA, CI.getDiagnostics(),
|
|
|
|
CI.getCodeGenOpts(), CI.getTargetOpts(),
|
2011-07-06 06:02:36 +08:00
|
|
|
CI.getLangOpts(),
|
2011-10-31 01:30:44 +08:00
|
|
|
CI.getFrontendOpts().ShowTimers, InFile,
|
|
|
|
LinkModuleToUse, OS.take(), *VMContext);
|
2011-01-26 04:34:14 +08:00
|
|
|
return BEConsumer;
|
2008-10-22 07:49:24 +08:00
|
|
|
}
|
2010-02-25 12:37:45 +08:00
|
|
|
|
2010-06-08 07:27:59 +08:00
|
|
|
void CodeGenAction::ExecuteAction() {
|
|
|
|
// If this is an IR file, we have to treat it specially.
|
|
|
|
if (getCurrentFileKind() == IK_LLVM_IR) {
|
|
|
|
BackendAction BA = static_cast<BackendAction>(Act);
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
|
|
|
raw_ostream *OS = GetOutputStream(CI, getCurrentFile(), BA);
|
|
|
|
if (BA != Backend_EmitNothing && !OS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool Invalid;
|
|
|
|
SourceManager &SM = CI.getSourceManager();
|
|
|
|
const llvm::MemoryBuffer *MainFile = SM.getBuffer(SM.getMainFileID(),
|
|
|
|
&Invalid);
|
|
|
|
if (Invalid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// FIXME: This is stupid, IRReader shouldn't take ownership.
|
|
|
|
llvm::MemoryBuffer *MainFileCopy =
|
|
|
|
llvm::MemoryBuffer::getMemBufferCopy(MainFile->getBuffer(),
|
|
|
|
getCurrentFile().c_str());
|
|
|
|
|
|
|
|
llvm::SMDiagnostic Err;
|
2011-02-20 07:03:58 +08:00
|
|
|
TheModule.reset(ParseIR(MainFileCopy, Err, *VMContext));
|
2010-06-08 07:27:59 +08:00
|
|
|
if (!TheModule) {
|
|
|
|
// Translate from the diagnostic info to the SourceManager location.
|
2011-09-20 04:40:38 +08:00
|
|
|
SourceLocation Loc = SM.translateFileLineCol(
|
2010-06-08 07:27:59 +08:00
|
|
|
SM.getFileEntryForID(SM.getMainFileID()), Err.getLineNo(),
|
|
|
|
Err.getColumnNo() + 1);
|
|
|
|
|
|
|
|
// Get a custom diagnostic for the error. We strip off a leading
|
|
|
|
// diagnostic code if there is one.
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Msg = Err.getMessage();
|
2010-06-08 07:27:59 +08:00
|
|
|
if (Msg.startswith("error: "))
|
|
|
|
Msg = Msg.substr(7);
|
2011-09-26 07:23:43 +08:00
|
|
|
unsigned DiagID = CI.getDiagnostics().getCustomDiagID(
|
|
|
|
DiagnosticsEngine::Error, Msg);
|
2010-06-08 07:27:59 +08:00
|
|
|
|
2010-11-19 04:06:41 +08:00
|
|
|
CI.getDiagnostics().Report(Loc, DiagID);
|
2010-06-08 07:27:59 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EmitBackendOutput(CI.getDiagnostics(), CI.getCodeGenOpts(),
|
2011-07-06 06:02:36 +08:00
|
|
|
CI.getTargetOpts(), CI.getLangOpts(),
|
|
|
|
TheModule.get(),
|
2010-06-08 07:27:59 +08:00
|
|
|
BA, OS);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise follow the normal AST path.
|
|
|
|
this->ASTFrontendAction::ExecuteAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void EmitAssemblyAction::anchor() { }
|
2011-02-20 07:03:58 +08:00
|
|
|
EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext)
|
|
|
|
: CodeGenAction(Backend_EmitAssembly, _VMContext) {}
|
2010-02-25 12:37:45 +08:00
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void EmitBCAction::anchor() { }
|
2011-02-20 07:03:58 +08:00
|
|
|
EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext)
|
|
|
|
: CodeGenAction(Backend_EmitBC, _VMContext) {}
|
2010-02-25 12:37:45 +08:00
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void EmitLLVMAction::anchor() { }
|
2011-02-20 07:03:58 +08:00
|
|
|
EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext)
|
|
|
|
: CodeGenAction(Backend_EmitLL, _VMContext) {}
|
2010-02-25 12:37:45 +08:00
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void EmitLLVMOnlyAction::anchor() { }
|
2011-02-20 07:03:58 +08:00
|
|
|
EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext)
|
|
|
|
: CodeGenAction(Backend_EmitNothing, _VMContext) {}
|
2010-02-25 12:37:45 +08:00
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void EmitCodeGenOnlyAction::anchor() { }
|
2011-02-20 07:03:58 +08:00
|
|
|
EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext)
|
|
|
|
: CodeGenAction(Backend_EmitMCNull, _VMContext) {}
|
2010-05-26 02:41:01 +08:00
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void EmitObjAction::anchor() { }
|
2011-02-20 07:03:58 +08:00
|
|
|
EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext)
|
|
|
|
: CodeGenAction(Backend_EmitObj, _VMContext) {}
|