diff options
Diffstat (limited to 'llvm')
-rw-r--r-- | llvm/include/llvm/Support/TargetRegistry.h | 41 | ||||
-rw-r--r-- | llvm/lib/CodeGen/LLVMTargetMachine.cpp | 12 | ||||
-rw-r--r-- | llvm/lib/MC/MCAsmStreamer.cpp | 47 | ||||
-rw-r--r-- | llvm/lib/MC/MCAssembler.cpp | 4 | ||||
-rw-r--r-- | llvm/tools/llvm-mc/llvm-mc.cpp | 19 |
5 files changed, 79 insertions, 44 deletions
diff --git a/llvm/include/llvm/Support/TargetRegistry.h b/llvm/include/llvm/Support/TargetRegistry.h index 0fc8c38d670..fea611d07d7 100644 --- a/llvm/include/llvm/Support/TargetRegistry.h +++ b/llvm/include/llvm/Support/TargetRegistry.h @@ -60,13 +60,30 @@ class TargetMachine; class TargetOptions; MCStreamer *createNullStreamer(MCContext &Ctx); -MCStreamer *createAsmStreamer(MCContext &Ctx, - std::unique_ptr<formatted_raw_ostream> OS, - bool isVerboseAsm, bool useDwarfDirectory, - MCInstPrinter *InstPrint, MCCodeEmitter *CE, - MCAsmBackend *TAB, bool ShowInst); +// Takes ownership of \p TAB and \p CE. + +/// Create a machine code streamer which will print out assembly for the native +/// target, suitable for compiling with a native assembler. +/// +/// \param InstPrint - If given, the instruction printer to use. If not given +/// the MCInst representation will be printed. This method takes ownership of +/// InstPrint. +/// +/// \param CE - If given, a code emitter to use to show the instruction +/// encoding inline with the assembly. This method takes ownership of \p CE. +/// +/// \param TAB - If given, a target asm backend to use to show the fixup +/// information in conjunction with encoding information. This method takes +/// ownership of \p TAB. +/// +/// \param ShowInst - Whether to show the MCInst representation inline with +/// the assembly. +MCStreamer * +createAsmStreamer(MCContext &Ctx, std::unique_ptr<formatted_raw_ostream> OS, + bool isVerboseAsm, bool useDwarfDirectory, + MCInstPrinter *InstPrint, std::unique_ptr<MCCodeEmitter> &&CE, + std::unique_ptr<MCAsmBackend> &&TAB, bool ShowInst); -/// Takes ownership of \p TAB and \p CE. MCStreamer *createELFStreamer(MCContext &Ctx, std::unique_ptr<MCAsmBackend> &&TAB, raw_pwrite_stream &OS, @@ -493,12 +510,14 @@ public: MCStreamer *createAsmStreamer(MCContext &Ctx, std::unique_ptr<formatted_raw_ostream> OS, bool IsVerboseAsm, bool UseDwarfDirectory, - MCInstPrinter *InstPrint, MCCodeEmitter *CE, - MCAsmBackend *TAB, bool ShowInst) const { + MCInstPrinter *InstPrint, + std::unique_ptr<MCCodeEmitter> &&CE, + std::unique_ptr<MCAsmBackend> &&TAB, + bool ShowInst) const { formatted_raw_ostream &OSRef = *OS; - MCStreamer *S = llvm::createAsmStreamer(Ctx, std::move(OS), IsVerboseAsm, - UseDwarfDirectory, InstPrint, CE, - TAB, ShowInst); + MCStreamer *S = llvm::createAsmStreamer( + Ctx, std::move(OS), IsVerboseAsm, UseDwarfDirectory, InstPrint, + std::move(CE), std::move(TAB), ShowInst); createAsmTargetStreamer(*S, OSRef, InstPrint, IsVerboseAsm); return S; } diff --git a/llvm/lib/CodeGen/LLVMTargetMachine.cpp b/llvm/lib/CodeGen/LLVMTargetMachine.cpp index 6781b07cadb..ee726c6dc67 100644 --- a/llvm/lib/CodeGen/LLVMTargetMachine.cpp +++ b/llvm/lib/CodeGen/LLVMTargetMachine.cpp @@ -138,17 +138,17 @@ bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM, getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI); // Create a code emitter if asked to show the encoding. - MCCodeEmitter *MCE = nullptr; + std::unique_ptr<MCCodeEmitter> MCE; if (Options.MCOptions.ShowMCEncoding) - MCE = getTarget().createMCCodeEmitter(MII, MRI, Context); + MCE.reset(getTarget().createMCCodeEmitter(MII, MRI, Context)); - MCAsmBackend *MAB = - getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions); + std::unique_ptr<MCAsmBackend> MAB( + getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions)); auto FOut = llvm::make_unique<formatted_raw_ostream>(Out); MCStreamer *S = getTarget().createAsmStreamer( Context, std::move(FOut), Options.MCOptions.AsmVerbose, - Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB, - Options.MCOptions.ShowMCInst); + Options.MCOptions.MCUseDwarfDirectory, InstPrinter, std::move(MCE), + std::move(MAB), Options.MCOptions.ShowMCInst); AsmStreamer.reset(S); break; } diff --git a/llvm/lib/MC/MCAsmStreamer.cpp b/llvm/lib/MC/MCAsmStreamer.cpp index 47da067f493..632a0308f29 100644 --- a/llvm/lib/MC/MCAsmStreamer.cpp +++ b/llvm/lib/MC/MCAsmStreamer.cpp @@ -14,6 +14,7 @@ #include "llvm/ADT/Twine.h" #include "llvm/MC/MCAsmBackend.h" #include "llvm/MC/MCAsmInfo.h" +#include "llvm/MC/MCAssembler.h" #include "llvm/MC/MCCodeEmitter.h" #include "llvm/MC/MCCodeView.h" #include "llvm/MC/MCContext.h" @@ -22,6 +23,7 @@ #include "llvm/MC/MCInst.h" #include "llvm/MC/MCInstPrinter.h" #include "llvm/MC/MCObjectFileInfo.h" +#include "llvm/MC/MCObjectWriter.h" #include "llvm/MC/MCRegisterInfo.h" #include "llvm/MC/MCSectionMachO.h" #include "llvm/MC/MCStreamer.h" @@ -31,6 +33,7 @@ #include "llvm/Support/LEB128.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/Path.h" +#include "llvm/Support/TargetRegistry.h" #include <cctype> using namespace llvm; @@ -42,12 +45,12 @@ class MCAsmStreamer final : public MCStreamer { formatted_raw_ostream &OS; const MCAsmInfo *MAI; std::unique_ptr<MCInstPrinter> InstPrinter; - std::unique_ptr<MCCodeEmitter> Emitter; - std::unique_ptr<MCAsmBackend> AsmBackend; + std::unique_ptr<MCAssembler> Assembler; SmallString<128> ExplicitCommentToEmit; SmallString<128> CommentToEmit; raw_svector_ostream CommentStream; + raw_null_ostream NullStream; unsigned IsVerboseAsm : 1; unsigned ShowInst : 1; @@ -60,13 +63,16 @@ class MCAsmStreamer final : public MCStreamer { public: MCAsmStreamer(MCContext &Context, std::unique_ptr<formatted_raw_ostream> os, bool isVerboseAsm, bool useDwarfDirectory, - MCInstPrinter *printer, MCCodeEmitter *emitter, - MCAsmBackend *asmbackend, bool showInst) + MCInstPrinter *printer, std::unique_ptr<MCCodeEmitter> emitter, + std::unique_ptr<MCAsmBackend> asmbackend, bool showInst) : MCStreamer(Context), OSOwner(std::move(os)), OS(*OSOwner), - MAI(Context.getAsmInfo()), InstPrinter(printer), Emitter(emitter), - AsmBackend(asmbackend), CommentStream(CommentToEmit), - IsVerboseAsm(isVerboseAsm), ShowInst(showInst), - UseDwarfDirectory(useDwarfDirectory) { + MAI(Context.getAsmInfo()), InstPrinter(printer), + Assembler(llvm::make_unique<MCAssembler>( + Context, std::move(asmbackend), std::move(emitter), + (asmbackend) ? asmbackend->createObjectWriter(NullStream) + : nullptr)), + CommentStream(CommentToEmit), IsVerboseAsm(isVerboseAsm), + ShowInst(showInst), UseDwarfDirectory(useDwarfDirectory) { assert(InstPrinter); if (IsVerboseAsm) InstPrinter->setCommentStream(CommentStream); @@ -1648,7 +1654,12 @@ void MCAsmStreamer::AddEncodingComment(const MCInst &Inst, SmallString<256> Code; SmallVector<MCFixup, 4> Fixups; raw_svector_ostream VecOS(Code); - Emitter->encodeInstruction(Inst, VecOS, Fixups, STI); + + // If we have no code emitter, don't emit code. + if (!Assembler->getEmitterPtr()) + return; + + Assembler->getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI); // If we are showing fixups, create symbolic markers in the encoded // representation. We do this by making a per-bit map to the fixup item index, @@ -1660,7 +1671,8 @@ void MCAsmStreamer::AddEncodingComment(const MCInst &Inst, for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { MCFixup &F = Fixups[i]; - const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind()); + const MCFixupKindInfo &Info = + Assembler->getBackend().getFixupKindInfo(F.getKind()); for (unsigned j = 0; j != Info.TargetSize; ++j) { unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j; assert(Index < Code.size() * 8 && "Invalid offset in fixup!"); @@ -1724,7 +1736,8 @@ void MCAsmStreamer::AddEncodingComment(const MCInst &Inst, for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { MCFixup &F = Fixups[i]; - const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind()); + const MCFixupKindInfo &Info = + Assembler->getBackend().getFixupKindInfo(F.getKind()); OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset() << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n"; } @@ -1737,8 +1750,7 @@ void MCAsmStreamer::EmitInstruction(const MCInst &Inst, "Cannot emit contents before setting section!"); // Show the encoding in a comment if we have a code emitter. - if (Emitter) - AddEncodingComment(Inst, STI, PrintSchedInfo); + AddEncodingComment(Inst, STI, PrintSchedInfo); // Show the MCInst if enabled. if (ShowInst) { @@ -1827,8 +1839,11 @@ void MCAsmStreamer::FinishImpl() { MCStreamer *llvm::createAsmStreamer(MCContext &Context, std::unique_ptr<formatted_raw_ostream> OS, bool isVerboseAsm, bool useDwarfDirectory, - MCInstPrinter *IP, MCCodeEmitter *CE, - MCAsmBackend *MAB, bool ShowInst) { + MCInstPrinter *IP, + std::unique_ptr<MCCodeEmitter> &&CE, + std::unique_ptr<MCAsmBackend> &&MAB, + bool ShowInst) { return new MCAsmStreamer(Context, std::move(OS), isVerboseAsm, - useDwarfDirectory, IP, CE, MAB, ShowInst); + useDwarfDirectory, IP, std::move(CE), std::move(MAB), + ShowInst); } diff --git a/llvm/lib/MC/MCAssembler.cpp b/llvm/lib/MC/MCAssembler.cpp index 721a2a62f52..ecbd41ec3bb 100644 --- a/llvm/lib/MC/MCAssembler.cpp +++ b/llvm/lib/MC/MCAssembler.cpp @@ -236,8 +236,8 @@ bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout, const MCSymbol &SA = A->getSymbol(); if (A->getKind() != MCSymbolRefExpr::VK_None || SA.isUndefined()) { IsResolved = false; - } else { - IsResolved = getWriter().isSymbolRefDifferenceFullyResolvedImpl( + } else if (auto *Writer = getWriterPtr()) { + IsResolved = Writer->isSymbolRefDifferenceFullyResolvedImpl( *this, SA, *DF, false, true); } } diff --git a/llvm/tools/llvm-mc/llvm-mc.cpp b/llvm/tools/llvm-mc/llvm-mc.cpp index c3ec9d495fa..dd83ac30446 100644 --- a/llvm/tools/llvm-mc/llvm-mc.cpp +++ b/llvm/tools/llvm-mc/llvm-mc.cpp @@ -439,16 +439,17 @@ int main(int argc, char **argv) { IP->setPrintImmHex(PrintImmHex); // Set up the AsmStreamer. - MCCodeEmitter *CE = nullptr; - MCAsmBackend *MAB = nullptr; - if (ShowEncoding) { - CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx); - MAB = TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions); - } + std::unique_ptr<MCCodeEmitter> CE; + if (ShowEncoding) + CE.reset(TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx)); + + std::unique_ptr<MCAsmBackend> MAB( + TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions)); auto FOut = llvm::make_unique<formatted_raw_ostream>(*OS); - Str.reset(TheTarget->createAsmStreamer( - Ctx, std::move(FOut), /*asmverbose*/ true, - /*useDwarfDirectory*/ true, IP, CE, MAB, ShowInst)); + Str.reset( + TheTarget->createAsmStreamer(Ctx, std::move(FOut), /*asmverbose*/ true, + /*useDwarfDirectory*/ true, IP, + std::move(CE), std::move(MAB), ShowInst)); } else if (FileType == OFT_Null) { Str.reset(TheTarget->createNullStreamer(Ctx)); |