diff options
| author | Stephen Hines <srhines@google.com> | 2013-01-21 13:15:17 -0800 |
|---|---|---|
| committer | Stephen Hines <srhines@google.com> | 2013-01-21 13:15:17 -0800 |
| commit | 059800f9e3fee2852672f846d91a2da14da7783a (patch) | |
| tree | a6ef16b7263252ae1b8069295ea9cbbae0d9467d /lib/MC | |
| parent | cbefa15de4821975bb99fc6d74b3bdb42b2df45c (diff) | |
| parent | b6714227eda5d499f7667fc865f931126a8dc488 (diff) | |
| download | external_llvm-059800f9e3fee2852672f846d91a2da14da7783a.zip external_llvm-059800f9e3fee2852672f846d91a2da14da7783a.tar.gz external_llvm-059800f9e3fee2852672f846d91a2da14da7783a.tar.bz2 | |
Merge remote-tracking branch 'upstream/master' into merge-llvm
Conflicts:
lib/CodeGen/AsmPrinter/AsmPrinter.cpp
lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
lib/MC/MCAssembler.cpp
lib/Support/Atomic.cpp
lib/Support/Memory.cpp
lib/Target/ARM/ARMJITInfo.cpp
Change-Id: Ib339baf88df5b04870c8df1bedcfe1f877ccab8d
Diffstat (limited to 'lib/MC')
53 files changed, 1884 insertions, 3096 deletions
diff --git a/lib/MC/CMakeLists.txt b/lib/MC/CMakeLists.txt index 99bff96..db882c0 100644 --- a/lib/MC/CMakeLists.txt +++ b/lib/MC/CMakeLists.txt @@ -36,7 +36,6 @@ add_llvm_library(LLVMMC MCStreamer.cpp MCSubtargetInfo.cpp MCSymbol.cpp - MCTargetAsmLexer.cpp MCValue.cpp MCWin64EH.cpp MachObjectWriter.cpp diff --git a/lib/MC/ELFObjectWriter.cpp b/lib/MC/ELFObjectWriter.cpp index 6e37b5c..bfe1709 100644 --- a/lib/MC/ELFObjectWriter.cpp +++ b/lib/MC/ELFObjectWriter.cpp @@ -11,17 +11,17 @@ // //===----------------------------------------------------------------------===// -#include "MCELF.h" +#include "llvm/MC/MCELFObjectWriter.h" #include "llvm/ADT/OwningPtr.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallString.h" -#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringMap.h" #include "llvm/MC/MCAsmBackend.h" #include "llvm/MC/MCAsmLayout.h" #include "llvm/MC/MCAssembler.h" #include "llvm/MC/MCContext.h" -#include "llvm/MC/MCELFObjectWriter.h" +#include "llvm/MC/MCELF.h" #include "llvm/MC/MCELFSymbolFlags.h" #include "llvm/MC/MCExpr.h" #include "llvm/MC/MCFixupKindInfo.h" @@ -29,9 +29,8 @@ #include "llvm/MC/MCSectionELF.h" #include "llvm/MC/MCValue.h" #include "llvm/Support/Debug.h" -#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ELF.h" - +#include "llvm/Support/ErrorHandling.h" #include <vector> using namespace llvm; @@ -133,6 +132,11 @@ class ELFObjectWriter : public MCObjectWriter { bool IsPCRel) const { return TargetObjectWriter->ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel); } + const MCSymbol *undefinedExplicitRelSym(const MCValue &Target, + const MCFixup &Fixup, + bool IsPCRel) const { + return TargetObjectWriter->undefinedExplicitRelSym(Target, Fixup, IsPCRel); + } bool is64Bit() const { return TargetObjectWriter->is64Bit(); } bool hasRelocationAddend() const { @@ -199,7 +203,7 @@ class ELFObjectWriter : public MCObjectWriter { void String8(MCDataFragment &F, uint8_t Value) { char buf[1]; buf[0] = Value; - F.getContents() += StringRef(buf, 1); + F.getContents().append(&buf[0], &buf[1]); } void String16(MCDataFragment &F, uint16_t Value) { @@ -208,7 +212,7 @@ class ELFObjectWriter : public MCObjectWriter { StringLE16(buf, Value); else StringBE16(buf, Value); - F.getContents() += StringRef(buf, 2); + F.getContents().append(&buf[0], &buf[2]); } void String32(MCDataFragment &F, uint32_t Value) { @@ -217,7 +221,7 @@ class ELFObjectWriter : public MCObjectWriter { StringLE32(buf, Value); else StringBE32(buf, Value); - F.getContents() += StringRef(buf, 4); + F.getContents().append(&buf[0], &buf[4]); } void String64(MCDataFragment &F, uint64_t Value) { @@ -226,7 +230,7 @@ class ELFObjectWriter : public MCObjectWriter { StringLE64(buf, Value); else StringBE64(buf, Value); - F.getContents() += StringRef(buf, 8); + F.getContents().append(&buf[0], &buf[8]); } void WriteHeader(uint64_t SectionDataSize, @@ -639,7 +643,7 @@ const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm, if (ASymbol.isUndefined()) { if (Renamed) return Renamed; - return &ASymbol; + return undefinedExplicitRelSym(Target, Fixup, IsPCRel); } if (SD.isExternal()) { @@ -721,10 +725,13 @@ void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm, MCSymbolData &SD = Asm.getSymbolData(ASymbol); MCFragment *F = SD.getFragment(); - Index = F->getParent()->getOrdinal() + 1; - - // Offset of the symbol in the section - Value += Layout.getSymbolOffset(&SD); + if (F) { + Index = F->getParent()->getOrdinal() + 1; + // Offset of the symbol in the section + Value += Layout.getSymbolOffset(&SD); + } else { + Index = 0; + } } else { if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref) WeakrefUsedInReloc.insert(RelocSymbol); @@ -733,8 +740,7 @@ void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm, Index = -1; } Addend = Value; - // Compensate for the addend on i386. - if (is64Bit()) + if (hasRelocationAddend()) Value = 0; } @@ -1180,7 +1186,7 @@ void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm, // The first entry of a string table holds a null character so skip // section 0. uint64_t Index = 1; - F->getContents() += '\x00'; + F->getContents().push_back('\x00'); for (unsigned int I = 0, E = Sections.size(); I != E; ++I) { const MCSectionELF &Section = *Sections[I]; @@ -1198,8 +1204,8 @@ void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm, SectionStringTableIndex[&Section] = Index; Index += Name.size() + 1; - F->getContents() += Name; - F->getContents() += '\x00'; + F->getContents().append(Name.begin(), Name.end()); + F->getContents().push_back('\x00'); } } @@ -1374,7 +1380,7 @@ void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm, ++i) { const MCFragment &F = *i; assert(F.getKind() == MCFragment::FT_Data); - WriteBytes(cast<MCDataFragment>(F).getContents().str()); + WriteBytes(cast<MCDataFragment>(F).getContents()); } } else { Asm.writeSectionData(&SD, Layout); diff --git a/lib/MC/MCAsmBackend.cpp b/lib/MC/MCAsmBackend.cpp index 2e447b0..53960e7 100644 --- a/lib/MC/MCAsmBackend.cpp +++ b/lib/MC/MCAsmBackend.cpp @@ -12,12 +12,9 @@ using namespace llvm; MCAsmBackend::MCAsmBackend() - : HasReliableSymbolDifference(false) -{ -} + : HasReliableSymbolDifference(false), HasDataInCodeSupport(false) {} -MCAsmBackend::~MCAsmBackend() { -} +MCAsmBackend::~MCAsmBackend() {} const MCFixupKindInfo & MCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const { diff --git a/lib/MC/MCAsmInfo.cpp b/lib/MC/MCAsmInfo.cpp index 7ea0f3b..a6fa658 100644 --- a/lib/MC/MCAsmInfo.cpp +++ b/lib/MC/MCAsmInfo.cpp @@ -37,6 +37,7 @@ MCAsmInfo::MCAsmInfo() { CommentColumn = 40; CommentString = "#"; LabelSuffix = ":"; + DebugLabelSuffix = ":"; GlobalPrefix = ""; PrivateGlobalPrefix = "."; LinkerPrivateGlobalPrefix = ""; diff --git a/lib/MC/MCAsmStreamer.cpp b/lib/MC/MCAsmStreamer.cpp index b0bc290..e234dfe 100644 --- a/lib/MC/MCAsmStreamer.cpp +++ b/lib/MC/MCAsmStreamer.cpp @@ -8,6 +8,11 @@ //===----------------------------------------------------------------------===// #include "llvm/MC/MCStreamer.h" +#include "llvm/ADT/OwningPtr.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/ADT/Twine.h" +#include "llvm/MC/MCAsmBackend.h" #include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCCodeEmitter.h" #include "llvm/MC/MCContext.h" @@ -20,15 +25,10 @@ #include "llvm/MC/MCSectionCOFF.h" #include "llvm/MC/MCSectionMachO.h" #include "llvm/MC/MCSymbol.h" -#include "llvm/MC/MCAsmBackend.h" -#include "llvm/ADT/OwningPtr.h" -#include "llvm/ADT/SmallString.h" -#include "llvm/ADT/StringExtras.h" -#include "llvm/ADT/Twine.h" #include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/MathExtras.h" #include "llvm/Support/Format.h" #include "llvm/Support/FormattedStream.h" +#include "llvm/Support/MathExtras.h" #include "llvm/Support/PathV2.h" #include <cctype> using namespace llvm; @@ -135,6 +135,8 @@ public: } virtual void EmitLabel(MCSymbol *Symbol); + virtual void EmitDebugLabel(MCSymbol *Symbol); + virtual void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol); virtual void EmitAssemblerFlag(MCAssemblerFlag Flag); @@ -226,6 +228,8 @@ public: virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset); virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment); virtual void EmitCFISignalFrame(); + virtual void EmitCFIUndefined(int64_t Register); + virtual void EmitCFIRegister(int64_t Register1, int64_t Register2); virtual void EmitWin64EHStartProc(const MCSymbol *Symbol); virtual void EmitWin64EHEndProc(); @@ -251,9 +255,14 @@ public: virtual void EmitPad(int64_t Offset); virtual void EmitRegSave(const SmallVectorImpl<unsigned> &RegList, bool); + virtual void EmitTCEntry(const MCSymbol &S); virtual void EmitInstruction(const MCInst &Inst); + virtual void EmitBundleAlignMode(unsigned AlignPow2); + virtual void EmitBundleLock(bool AlignToEnd); + virtual void EmitBundleUnlock(); + /// EmitRawText - If this file is backed by an assembly streamer, this dumps /// the specified string in the output .s file. This capability is /// indicated by the hasRawTextSupport() predicate. @@ -342,6 +351,14 @@ void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) { EmitEOL(); } +void MCAsmStreamer::EmitDebugLabel(MCSymbol *Symbol) { + assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); + MCStreamer::EmitDebugLabel(Symbol); + + OS << *Symbol << MAI.getDebugLabelSuffix(); + EmitEOL(); +} + void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { switch (Flag) { case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break; @@ -1035,6 +1052,26 @@ void MCAsmStreamer::EmitCFISignalFrame() { EmitEOL(); } +void MCAsmStreamer::EmitCFIUndefined(int64_t Register) { + MCStreamer::EmitCFIUndefined(Register); + + if (!UseCFI) + return; + + OS << "\t.cfi_undefined " << Register; + EmitEOL(); +} + +void MCAsmStreamer::EmitCFIRegister(int64_t Register1, int64_t Register2) { + MCStreamer::EmitCFIRegister(Register1, Register2); + + if (!UseCFI) + return; + + OS << "\t.cfi_register " << Register1 << ", " << Register2; + EmitEOL(); +} + void MCAsmStreamer::EmitWin64EHStartProc(const MCSymbol *Symbol) { MCStreamer::EmitWin64EHStartProc(Symbol); @@ -1299,6 +1336,14 @@ void MCAsmStreamer::EmitRegSave(const SmallVectorImpl<unsigned> &RegList, EmitEOL(); } +void MCAsmStreamer::EmitTCEntry(const MCSymbol &S) { + OS << "\t.tc "; + OS << S.getName(); + OS << "[TC],"; + OS << S.getName(); + EmitEOL(); +} + void MCAsmStreamer::EmitInstruction(const MCInst &Inst) { assert(getCurrentSection() && "Cannot emit contents before setting section!"); @@ -1320,6 +1365,23 @@ void MCAsmStreamer::EmitInstruction(const MCInst &Inst) { EmitEOL(); } +void MCAsmStreamer::EmitBundleAlignMode(unsigned AlignPow2) { + OS << "\t.bundle_align_mode " << AlignPow2; + EmitEOL(); +} + +void MCAsmStreamer::EmitBundleLock(bool AlignToEnd) { + OS << "\t.bundle_lock"; + if (AlignToEnd) + OS << " align_to_end"; + EmitEOL(); +} + +void MCAsmStreamer::EmitBundleUnlock() { + OS << "\t.bundle_unlock"; + EmitEOL(); +} + /// EmitRawText - If this file is backed by an assembly streamer, this dumps /// the specified string in the output .s file. This capability is /// indicated by the hasRawTextSupport() predicate. diff --git a/lib/MC/MCAssembler.cpp b/lib/MC/MCAssembler.cpp index 926d39b..abf095e 100644 --- a/lib/MC/MCAssembler.cpp +++ b/lib/MC/MCAssembler.cpp @@ -9,31 +9,41 @@ #define DEBUG_TYPE "assembler" #include "llvm/MC/MCAssembler.h" +#include "llvm/ADT/Statistic.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/ADT/Twine.h" +#include "llvm/MC/MCAsmBackend.h" #include "llvm/MC/MCAsmLayout.h" #include "llvm/MC/MCCodeEmitter.h" #include "llvm/MC/MCContext.h" +#include "llvm/MC/MCDwarf.h" #include "llvm/MC/MCExpr.h" #include "llvm/MC/MCFixupKindInfo.h" #include "llvm/MC/MCObjectWriter.h" #include "llvm/MC/MCSection.h" #include "llvm/MC/MCSymbol.h" #include "llvm/MC/MCValue.h" -#include "llvm/MC/MCDwarf.h" -#include "llvm/MC/MCAsmBackend.h" -#include "llvm/ADT/Statistic.h" -#include "llvm/ADT/StringExtras.h" -#include "llvm/ADT/Twine.h" #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/raw_ostream.h" -#include "llvm/Support/TargetRegistry.h" #include "llvm/Support/LEB128.h" +#include "llvm/Support/TargetRegistry.h" +#include "llvm/Support/raw_ostream.h" using namespace llvm; namespace { namespace stats { -STATISTIC(EmittedFragments, "Number of emitted assembler fragments"); +STATISTIC(EmittedFragments, "Number of emitted assembler fragments - total"); +STATISTIC(EmittedRelaxableFragments, + "Number of emitted assembler fragments - relaxable"); +STATISTIC(EmittedDataFragments, + "Number of emitted assembler fragments - data"); +STATISTIC(EmittedAlignFragments, + "Number of emitted assembler fragments - align"); +STATISTIC(EmittedFillFragments, + "Number of emitted assembler fragments - fill"); +STATISTIC(EmittedOrgFragments, + "Number of emitted assembler fragments - org"); STATISTIC(evaluateFixup, "Number of evaluated fixups"); STATISTIC(FragmentLayouts, "Number of fragment layouts"); STATISTIC(ObjectBytes, "Number of emitted object file bytes"); @@ -61,7 +71,7 @@ MCAsmLayout::MCAsmLayout(MCAssembler &Asm) SectionOrder.push_back(&*it); } -bool MCAsmLayout::isFragmentUpToDate(const MCFragment *F) const { +bool MCAsmLayout::isFragmentValid(const MCFragment *F) const { const MCSectionData &SD = *F->getParent(); const MCFragment *LastValid = LastValidFragment.lookup(&SD); if (!LastValid) @@ -70,9 +80,9 @@ bool MCAsmLayout::isFragmentUpToDate(const MCFragment *F) const { return F->getLayoutOrder() <= LastValid->getLayoutOrder(); } -void MCAsmLayout::Invalidate(MCFragment *F) { - // If this fragment wasn't already up-to-date, we don't need to do anything. - if (!isFragmentUpToDate(F)) +void MCAsmLayout::invalidateFragmentsAfter(MCFragment *F) { + // If this fragment wasn't already valid, we don't need to do anything. + if (!isFragmentValid(F)) return; // Otherwise, reset the last valid fragment to this fragment. @@ -80,7 +90,7 @@ void MCAsmLayout::Invalidate(MCFragment *F) { LastValidFragment[&SD] = F; } -void MCAsmLayout::EnsureValid(const MCFragment *F) const { +void MCAsmLayout::ensureValid(const MCFragment *F) const { MCSectionData &SD = *F->getParent(); MCFragment *Cur = LastValidFragment[&SD]; @@ -89,15 +99,16 @@ void MCAsmLayout::EnsureValid(const MCFragment *F) const { else Cur = Cur->getNextNode(); - // Advance the layout position until the fragment is up-to-date. - while (!isFragmentUpToDate(F)) { - const_cast<MCAsmLayout*>(this)->LayoutFragment(Cur); + // Advance the layout position until the fragment is valid. + while (!isFragmentValid(F)) { + assert(Cur && "Layout bookkeeping error"); + const_cast<MCAsmLayout*>(this)->layoutFragment(Cur); Cur = Cur->getNextNode(); } } uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const { - EnsureValid(F); + ensureValid(F); assert(F->Offset != ~UINT64_C(0) && "Address not set!"); return F->Offset; } @@ -149,6 +160,46 @@ uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const { return getSectionAddressSize(SD); } +uint64_t MCAsmLayout::computeBundlePadding(const MCFragment *F, + uint64_t FOffset, uint64_t FSize) { + uint64_t BundleSize = Assembler.getBundleAlignSize(); + assert(BundleSize > 0 && + "computeBundlePadding should only be called if bundling is enabled"); + uint64_t BundleMask = BundleSize - 1; + uint64_t OffsetInBundle = FOffset & BundleMask; + uint64_t EndOfFragment = OffsetInBundle + FSize; + + // There are two kinds of bundling restrictions: + // + // 1) For alignToBundleEnd(), add padding to ensure that the fragment will + // *end* on a bundle boundary. + // 2) Otherwise, check if the fragment would cross a bundle boundary. If it + // would, add padding until the end of the bundle so that the fragment + // will start in a new one. + if (F->alignToBundleEnd()) { + // Three possibilities here: + // + // A) The fragment just happens to end at a bundle boundary, so we're good. + // B) The fragment ends before the current bundle boundary: pad it just + // enough to reach the boundary. + // C) The fragment ends after the current bundle boundary: pad it until it + // reaches the end of the next bundle boundary. + // + // Note: this code could be made shorter with some modulo trickery, but it's + // intentionally kept in its more explicit form for simplicity. + if (EndOfFragment == BundleSize) + return 0; + else if (EndOfFragment < BundleSize) + return BundleSize - EndOfFragment; + else { // EndOfFragment > BundleSize + return 2 * BundleSize - EndOfFragment; + } + } else if (EndOfFragment > BundleSize) + return BundleSize - OffsetInBundle; + else + return 0; +} + /* *** */ MCFragment::MCFragment() : Kind(FragmentType(~0)) { @@ -167,12 +218,18 @@ MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent) /* *** */ +MCEncodedFragment::~MCEncodedFragment() { +} + +/* *** */ + MCSectionData::MCSectionData() : Section(0) {} MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A) : Section(&_Section), Ordinal(~UINT32_C(0)), Alignment(1), + BundleLockState(NotBundleLocked), BundleGroupBeforeFirstInst(false), HasInstructions(false) { if (A) @@ -199,9 +256,9 @@ MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, MCAssembler::MCAssembler(MCContext &Context_, MCAsmBackend &Backend_, MCCodeEmitter &Emitter_, MCObjectWriter &Writer_, raw_ostream &OS_) - : Context(Context_), Backend(Backend_), Emitter(Emitter_), Writer(&Writer_), - OS(OS_), RelaxAll(false), NoExecStack(false), SubsectionsViaSymbols(false) -{ + : Context(Context_), Backend(Backend_), Emitter(Emitter_), Writer(Writer_), + OS(OS_), BundleAlignSize(0), RelaxAll(false), NoExecStack(false), + SubsectionsViaSymbols(false) { } MCAssembler::~MCAssembler() { @@ -212,6 +269,24 @@ void MCAssembler::setWriter(MCObjectWriter &ObjectWriter) { Writer = &ObjectWriter; } +void MCAssembler::reset() { + Sections.clear(); + Symbols.clear(); + SectionMap.clear(); + SymbolMap.clear(); + IndirectSymbols.clear(); + DataRegions.clear(); + ThumbFuncs.clear(); + RelaxAll = false; + NoExecStack = false; + SubsectionsViaSymbols = false; + + // reset objects owned by us + getBackend().reset(); + getEmitter().reset(); + getWriter().reset(); +} + bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const { // Non-temporary labels should always be visible to the linker. if (!Symbol.isTemporary()) @@ -318,11 +393,10 @@ uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout, const MCFragment &F) const { switch (F.getKind()) { case MCFragment::FT_Data: - return cast<MCDataFragment>(F).getContents().size(); + case MCFragment::FT_Relaxable: + return cast<MCEncodedFragment>(F).getContents().size(); case MCFragment::FT_Fill: return cast<MCFillFragment>(F).getSize(); - case MCFragment::FT_Inst: - return cast<MCInstFragment>(F).getInstSize(); case MCFragment::FT_LEB: return cast<MCLEBFragment>(F).getContents().size(); @@ -366,31 +440,84 @@ uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout, llvm_unreachable("invalid fragment kind"); } -void MCAsmLayout::LayoutFragment(MCFragment *F) { +void MCAsmLayout::layoutFragment(MCFragment *F) { MCFragment *Prev = F->getPrevNode(); - // We should never try to recompute something which is up-to-date. - assert(!isFragmentUpToDate(F) && "Attempt to recompute up-to-date fragment!"); - // We should never try to compute the fragment layout if it's predecessor - // isn't up-to-date. - assert((!Prev || isFragmentUpToDate(Prev)) && - "Attempt to compute fragment before it's predecessor!"); + // We should never try to recompute something which is valid. + assert(!isFragmentValid(F) && "Attempt to recompute a valid fragment!"); + // We should never try to compute the fragment layout if its predecessor + // isn't valid. + assert((!Prev || isFragmentValid(Prev)) && + "Attempt to compute fragment before its predecessor!"); ++stats::FragmentLayouts; // Compute fragment offset and size. - uint64_t Offset = 0; if (Prev) - Offset += Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev); - - F->Offset = Offset; + F->Offset = Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev); + else + F->Offset = 0; LastValidFragment[F->getParent()] = F; + + // If bundling is enabled and this fragment has instructions in it, it has to + // obey the bundling restrictions. With padding, we'll have: + // + // + // BundlePadding + // ||| + // ------------------------------------- + // Prev |##########| F | + // ------------------------------------- + // ^ + // | + // F->Offset + // + // The fragment's offset will point to after the padding, and its computed + // size won't include the padding. + // + if (Assembler.isBundlingEnabled() && F->hasInstructions()) { + assert(isa<MCEncodedFragment>(F) && + "Only MCEncodedFragment implementations have instructions"); + uint64_t FSize = Assembler.computeFragmentSize(*this, *F); + + if (FSize > Assembler.getBundleAlignSize()) + report_fatal_error("Fragment can't be larger than a bundle size"); + + uint64_t RequiredBundlePadding = computeBundlePadding(F, F->Offset, FSize); + if (RequiredBundlePadding > UINT8_MAX) + report_fatal_error("Padding cannot exceed 255 bytes"); + F->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding)); + F->Offset += RequiredBundlePadding; + } +} + +/// \brief Write the contents of a fragment to the given object writer. Expects +/// a MCEncodedFragment. +static void writeFragmentContents(const MCFragment &F, MCObjectWriter *OW) { + MCEncodedFragment &EF = cast<MCEncodedFragment>(F); + OW->WriteBytes(EF.getContents()); } -/// WriteFragmentData - Write the \arg F data to the output file. -static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout, - const MCFragment &F) { +/// \brief Write the fragment \p F to the output file. +static void writeFragment(const MCAssembler &Asm, const MCAsmLayout &Layout, + const MCFragment &F) { MCObjectWriter *OW = &Asm.getWriter(); + + // Should NOP padding be written out before this fragment? + unsigned BundlePadding = F.getBundlePadding(); + if (BundlePadding > 0) { + assert(Asm.isBundlingEnabled() && + "Writing bundle padding with disabled bundling"); + assert(F.hasInstructions() && + "Writing bundle padding for a fragment without instructions"); + + if (!Asm.getBackend().writeNopData(BundlePadding, OW)) + report_fatal_error("unable to write NOP sequence of " + + Twine(BundlePadding) + " bytes"); + } + + // This variable (and its dummy usage) is to participate in the assert at + // the end of the function. uint64_t Start = OW->getStream().tell(); (void) Start; @@ -400,6 +527,7 @@ static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout, uint64_t FragmentSize = Asm.computeFragmentSize(Layout, F); switch (F.getKind()) { case MCFragment::FT_Align: { + ++stats::EmittedAlignFragments; MCAlignFragment &AF = cast<MCAlignFragment>(F); uint64_t Count = FragmentSize / AF.getValueSize(); @@ -438,14 +566,18 @@ static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout, break; } - case MCFragment::FT_Data: { - MCDataFragment &DF = cast<MCDataFragment>(F); - assert(FragmentSize == DF.getContents().size() && "Invalid size!"); - OW->WriteBytes(DF.getContents().str()); + case MCFragment::FT_Data: + ++stats::EmittedDataFragments; + writeFragmentContents(F, OW); + break; + + case MCFragment::FT_Relaxable: + ++stats::EmittedRelaxableFragments; + writeFragmentContents(F, OW); break; - } case MCFragment::FT_Fill: { + ++stats::EmittedFillFragments; MCFillFragment &FF = cast<MCFillFragment>(F); assert(FF.getValueSize() && "Invalid virtual align in concrete fragment!"); @@ -462,12 +594,6 @@ static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout, break; } - case MCFragment::FT_Inst: { - MCInstFragment &IF = cast<MCInstFragment>(F); - OW->WriteBytes(StringRef(IF.getCode().begin(), IF.getCode().size())); - break; - } - case MCFragment::FT_LEB: { MCLEBFragment &LF = cast<MCLEBFragment>(F); OW->WriteBytes(LF.getContents().str()); @@ -475,6 +601,7 @@ static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout, } case MCFragment::FT_Org: { + ++stats::EmittedOrgFragments; MCOrgFragment &OF = cast<MCOrgFragment>(F); for (uint64_t i = 0, e = FragmentSize; i != e; ++i) @@ -495,7 +622,8 @@ static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout, } } - assert(OW->getStream().tell() - Start == FragmentSize); + assert(OW->getStream().tell() - Start == FragmentSize && + "The stream should advance by fragment size"); } void MCAssembler::writeSectionData(const MCSectionData *SD, @@ -539,11 +667,11 @@ void MCAssembler::writeSectionData(const MCSectionData *SD, } uint64_t Start = getWriter().getStream().tell(); - (void) Start; + (void)Start; - for (MCSectionData::const_iterator it = SD->begin(), - ie = SD->end(); it != ie; ++it) - WriteFragmentData(*this, Layout, *it); + for (MCSectionData::const_iterator it = SD->begin(), ie = SD->end(); + it != ie; ++it) + writeFragment(*this, Layout, *it); assert(getWriter().getStream().tell() - Start == Layout.getSectionAddressSize(SD)); @@ -590,9 +718,9 @@ void MCAssembler::Finish() { SD->setLayoutOrder(i); unsigned FragmentIndex = 0; - for (MCSectionData::iterator it2 = SD->begin(), - ie2 = SD->end(); it2 != ie2; ++it2) - it2->setLayoutOrder(FragmentIndex++); + for (MCSectionData::iterator iFrag = SD->begin(), iFragEnd = SD->end(); + iFrag != iFragEnd; ++iFrag) + iFrag->setLayoutOrder(FragmentIndex++); } // Layout until everything fits. @@ -620,24 +748,14 @@ void MCAssembler::Finish() { for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) { for (MCSectionData::iterator it2 = it->begin(), ie2 = it->end(); it2 != ie2; ++it2) { - MCDataFragment *DF = dyn_cast<MCDataFragment>(it2); - if (DF) { - for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(), - ie3 = DF->fixup_end(); it3 != ie3; ++it3) { + MCEncodedFragment *F = dyn_cast<MCEncodedFragment>(it2); + if (F) { + for (MCEncodedFragment::fixup_iterator it3 = F->fixup_begin(), + ie3 = F->fixup_end(); it3 != ie3; ++it3) { MCFixup &Fixup = *it3; - uint64_t FixedValue = handleFixup(Layout, *DF, Fixup); - getBackend().applyFixup(Fixup, DF->getContents().data(), - DF->getContents().size(), FixedValue); - } - } - MCInstFragment *IF = dyn_cast<MCInstFragment>(it2); - if (IF) { - for (MCInstFragment::fixup_iterator it3 = IF->fixup_begin(), - ie3 = IF->fixup_end(); it3 != ie3; ++it3) { - MCFixup &Fixup = *it3; - uint64_t FixedValue = handleFixup(Layout, *IF, Fixup); - getBackend().applyFixup(Fixup, IF->getCode().data(), - IF->getCode().size(), FixedValue); + uint64_t FixedValue = handleFixup(Layout, *F, Fixup); + getBackend().applyFixup(Fixup, F->getContents().data(), + F->getContents().size(), FixedValue); } } } @@ -650,11 +768,8 @@ void MCAssembler::Finish() { } bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup, - const MCInstFragment *DF, + const MCRelaxableFragment *DF, const MCAsmLayout &Layout) const { - if (getRelaxAll()) - return true; - // If we cannot resolve the fixup value, it requires relaxation. MCValue Target; uint64_t Value; @@ -664,25 +779,25 @@ bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup, return getBackend().fixupNeedsRelaxation(Fixup, Value, DF, Layout); } -bool MCAssembler::fragmentNeedsRelaxation(const MCInstFragment *IF, +bool MCAssembler::fragmentNeedsRelaxation(const MCRelaxableFragment *F, const MCAsmLayout &Layout) const { // If this inst doesn't ever need relaxation, ignore it. This occurs when we // are intentionally pushing out inst fragments, or because we relaxed a // previous instruction to one that doesn't need relaxation. - if (!getBackend().mayNeedRelaxation(IF->getInst())) + if (!getBackend().mayNeedRelaxation(F->getInst())) return false; - for (MCInstFragment::const_fixup_iterator it = IF->fixup_begin(), - ie = IF->fixup_end(); it != ie; ++it) - if (fixupNeedsRelaxation(*it, IF, Layout)) + for (MCRelaxableFragment::const_fixup_iterator it = F->fixup_begin(), + ie = F->fixup_end(); it != ie; ++it) + if (fixupNeedsRelaxation(*it, F, Layout)) return true; return false; } bool MCAssembler::relaxInstruction(MCAsmLayout &Layout, - MCInstFragment &IF) { - if (!fragmentNeedsRelaxation(&IF, Layout)) + MCRelaxableFragment &F) { + if (!fragmentNeedsRelaxation(&F, Layout)) return false; ++stats::RelaxedInstructions; @@ -693,7 +808,7 @@ bool MCAssembler::relaxInstruction(MCAsmLayout &Layout, // Relax the fragment. MCInst Relaxed; - getBackend().relaxInstruction(IF.getInst(), Relaxed); + getBackend().relaxInstruction(F.getInst(), Relaxed); // Encode the new instruction. // @@ -705,13 +820,10 @@ bool MCAssembler::relaxInstruction(MCAsmLayout &Layout, getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups); VecOS.flush(); - // Update the instruction fragment. - IF.setInst(Relaxed); - IF.getCode() = Code; - IF.getFixups().clear(); - // FIXME: Eliminate copy. - for (unsigned i = 0, e = Fixups.size(); i != e; ++i) - IF.getFixups().push_back(Fixups[i]); + // Update the fragment. + F.setInst(Relaxed); + F.getContents() = Code; + F.getFixups() = Fixups; return true; } @@ -765,39 +877,43 @@ bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout, return OldSize != Data.size(); } -bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout, - MCSectionData &SD) { - MCFragment *FirstInvalidFragment = NULL; - // Scan for fragments that need relaxation. - for (MCSectionData::iterator it2 = SD.begin(), - ie2 = SD.end(); it2 != ie2; ++it2) { - // Check if this is an fragment that needs relaxation. - bool relaxedFrag = false; - switch(it2->getKind()) { +bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD) { + // Holds the first fragment which needed relaxing during this layout. It will + // remain NULL if none were relaxed. + // When a fragment is relaxed, all the fragments following it should get + // invalidated because their offset is going to change. + MCFragment *FirstRelaxedFragment = NULL; + + // Attempt to relax all the fragments in the section. + for (MCSectionData::iterator I = SD.begin(), IE = SD.end(); I != IE; ++I) { + // Check if this is a fragment that needs relaxation. + bool RelaxedFrag = false; + switch(I->getKind()) { default: - break; - case MCFragment::FT_Inst: - relaxedFrag = relaxInstruction(Layout, *cast<MCInstFragment>(it2)); + break; + case MCFragment::FT_Relaxable: + assert(!getRelaxAll() && + "Did not expect a MCRelaxableFragment in RelaxAll mode"); + RelaxedFrag = relaxInstruction(Layout, *cast<MCRelaxableFragment>(I)); break; case MCFragment::FT_Dwarf: - relaxedFrag = relaxDwarfLineAddr(Layout, - *cast<MCDwarfLineAddrFragment>(it2)); + RelaxedFrag = relaxDwarfLineAddr(Layout, + *cast<MCDwarfLineAddrFragment>(I)); break; case MCFragment::FT_DwarfFrame: - relaxedFrag = + RelaxedFrag = relaxDwarfCallFrameFragment(Layout, - *cast<MCDwarfCallFrameFragment>(it2)); + *cast<MCDwarfCallFrameFragment>(I)); break; case MCFragment::FT_LEB: - relaxedFrag = relaxLEB(Layout, *cast<MCLEBFragment>(it2)); + RelaxedFrag = relaxLEB(Layout, *cast<MCLEBFragment>(I)); break; } - // Update the layout, and remember that we relaxed. - if (relaxedFrag && !FirstInvalidFragment) - FirstInvalidFragment = it2; + if (RelaxedFrag && !FirstRelaxedFragment) + FirstRelaxedFragment = I; } - if (FirstInvalidFragment) { - Layout.Invalidate(FirstInvalidFragment); + if (FirstRelaxedFragment) { + Layout.invalidateFragmentsAfter(FirstRelaxedFragment); return true; } return false; @@ -809,7 +925,7 @@ bool MCAssembler::layoutOnce(MCAsmLayout &Layout) { bool WasRelaxed = false; for (iterator it = begin(), ie = end(); it != ie; ++it) { MCSectionData &SD = *it; - while(layoutSectionOnce(Layout, SD)) + while (layoutSectionOnce(Layout, SD)) WasRelaxed = true; } @@ -836,7 +952,7 @@ raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) { } -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) void MCFragment::dump() { raw_ostream &OS = llvm::errs(); @@ -845,7 +961,7 @@ void MCFragment::dump() { case MCFragment::FT_Align: OS << "MCAlignFragment"; break; case MCFragment::FT_Data: OS << "MCDataFragment"; break; case MCFragment::FT_Fill: OS << "MCFillFragment"; break; - case MCFragment::FT_Inst: OS << "MCInstFragment"; break; + case MCFragment::FT_Relaxable: OS << "MCRelaxableFragment"; break; case MCFragment::FT_Org: OS << "MCOrgFragment"; break; case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break; case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break; @@ -853,7 +969,9 @@ void MCFragment::dump() { } OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder - << " Offset:" << Offset << ">"; + << " Offset:" << Offset + << " HasInstructions:" << hasInstructions() + << " BundlePadding:" << getBundlePadding() << ">"; switch (getKind()) { case MCFragment::FT_Align: { @@ -877,7 +995,7 @@ void MCFragment::dump() { } OS << "] (" << Contents.size() << " bytes)"; - if (!DF->getFixups().empty()) { + if (DF->fixup_begin() != DF->fixup_end()) { OS << ",\n "; OS << " Fixups:["; for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(), @@ -895,11 +1013,11 @@ void MCFragment::dump() { << " Size:" << FF->getSize(); break; } - case MCFragment::FT_Inst: { - const MCInstFragment *IF = cast<MCInstFragment>(this); + case MCFragment::FT_Relaxable: { + const MCRelaxableFragment *F = cast<MCRelaxableFragment>(this); OS << "\n "; OS << " Inst:"; - IF->getInst().dump_pretty(OS); + F->getInst().dump_pretty(OS); break; } case MCFragment::FT_Org: { @@ -935,7 +1053,8 @@ void MCSectionData::dump() { raw_ostream &OS = llvm::errs(); OS << "<MCSectionData"; - OS << " Alignment:" << getAlignment() << " Fragments:[\n "; + OS << " Alignment:" << getAlignment() + << " Fragments:[\n "; for (iterator it = begin(), ie = end(); it != ie; ++it) { if (it != begin()) OS << ",\n "; it->dump(); @@ -980,8 +1099,9 @@ void MCAssembler::dump() { #endif // anchors for MC*Fragment vtables +void MCEncodedFragment::anchor() { } void MCDataFragment::anchor() { } -void MCInstFragment::anchor() { } +void MCRelaxableFragment::anchor() { } void MCAlignFragment::anchor() { } void MCFillFragment::anchor() { } void MCOrgFragment::anchor() { } diff --git a/lib/MC/MCContext.cpp b/lib/MC/MCContext.cpp index b5b14b9..aa52b49 100644 --- a/lib/MC/MCContext.cpp +++ b/lib/MC/MCContext.cpp @@ -8,21 +8,22 @@ //===----------------------------------------------------------------------===// #include "llvm/MC/MCContext.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/ADT/Twine.h" #include "llvm/MC/MCAsmInfo.h" +#include "llvm/MC/MCDwarf.h" +#include "llvm/MC/MCLabel.h" #include "llvm/MC/MCObjectFileInfo.h" #include "llvm/MC/MCRegisterInfo.h" -#include "llvm/MC/MCSectionMachO.h" -#include "llvm/MC/MCSectionELF.h" #include "llvm/MC/MCSectionCOFF.h" +#include "llvm/MC/MCSectionELF.h" +#include "llvm/MC/MCSectionMachO.h" #include "llvm/MC/MCSymbol.h" -#include "llvm/MC/MCLabel.h" -#include "llvm/MC/MCDwarf.h" -#include "llvm/ADT/SmallString.h" -#include "llvm/ADT/Twine.h" #include "llvm/Support/ELF.h" #include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Signals.h" +#include "llvm/Support/SourceMgr.h" using namespace llvm; typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy; @@ -31,12 +32,16 @@ typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy; MCContext::MCContext(const MCAsmInfo &mai, const MCRegisterInfo &mri, - const MCObjectFileInfo *mofi, const SourceMgr *mgr) : + const MCObjectFileInfo *mofi, const SourceMgr *mgr, + bool DoAutoReset) : SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi), Allocator(), Symbols(Allocator), UsedNames(Allocator), NextUniqueID(0), - CurrentDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0), - AllowTemporaryLabels(true) { + CompilationDir(llvm::sys::Path::GetCurrentDirectory().str()), + CurrentDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0), + DwarfLocSeen(false), GenDwarfForAssembly(false), GenDwarfFileNumber(0), + AllowTemporaryLabels(true), AutoReset(DoAutoReset) { + MachOUniquingMap = 0; ELFUniquingMap = 0; COFFUniquingMap = 0; @@ -45,22 +50,54 @@ MCContext::MCContext(const MCAsmInfo &mai, const MCRegisterInfo &mri, SecureLog = 0; SecureLogUsed = false; - DwarfLocSeen = false; - GenDwarfForAssembly = false; - GenDwarfFileNumber = 0; + if (SrcMgr && SrcMgr->getNumBuffers() > 0) + MainFileName = SrcMgr->getMemoryBuffer(0)->getBufferIdentifier(); + else + MainFileName = ""; } MCContext::~MCContext() { + + if (AutoReset) + reset(); + // NOTE: The symbols are all allocated out of a bump pointer allocator, // we don't need to free them here. + + // If the stream for the .secure_log_unique directive was created free it. + delete (raw_ostream*)SecureLog; +} + +//===----------------------------------------------------------------------===// +// Module Lifetime Management +//===----------------------------------------------------------------------===// + +void MCContext::reset() { + UsedNames.clear(); + Symbols.clear(); + Allocator.Reset(); + Instances.clear(); + MCDwarfFiles.clear(); + MCDwarfDirs.clear(); + MCGenDwarfLabelEntries.clear(); + DwarfDebugFlags = StringRef(); + MCLineSections.clear(); + MCLineSectionOrder.clear(); + CurrentDwarfLoc = MCDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0); // If we have the MachO uniquing map, free it. delete (MachOUniqueMapTy*)MachOUniquingMap; delete (ELFUniqueMapTy*)ELFUniquingMap; delete (COFFUniqueMapTy*)COFFUniquingMap; + MachOUniquingMap = 0; + ELFUniquingMap = 0; + COFFUniquingMap = 0; - // If the stream for the .secure_log_unique directive was created free it. - delete (raw_ostream*)SecureLog; + NextUniqueID = 0; + AllowTemporaryLabels = true; + DwarfLocSeen = false; + GenDwarfForAssembly = false; + GenDwarfFileNumber = 0; } //===----------------------------------------------------------------------===// @@ -153,6 +190,12 @@ MCSymbol *MCContext::LookupSymbol(StringRef Name) const { return Symbols.lookup(Name); } +MCSymbol *MCContext::LookupSymbol(const Twine &Name) const { + SmallString<128> NameSV; + Name.toVector(NameSV); + return LookupSymbol(NameSV.str()); +} + //===----------------------------------------------------------------------===// // Section Management //===----------------------------------------------------------------------===// diff --git a/lib/MC/MCDisassembler/CMakeLists.txt b/lib/MC/MCDisassembler/CMakeLists.txt index 5e2cd83..5195b9e 100644 --- a/lib/MC/MCDisassembler/CMakeLists.txt +++ b/lib/MC/MCDisassembler/CMakeLists.txt @@ -1,8 +1,3 @@ add_llvm_library(LLVMMCDisassembler Disassembler.cpp - EDDisassembler.cpp - EDInst.cpp - EDMain.cpp - EDOperand.cpp - EDToken.cpp ) diff --git a/lib/MC/MCDisassembler/Disassembler.cpp b/lib/MC/MCDisassembler/Disassembler.cpp index 35f675d..d3fa906 100644 --- a/lib/MC/MCDisassembler/Disassembler.cpp +++ b/lib/MC/MCDisassembler/Disassembler.cpp @@ -9,7 +9,6 @@ #include "Disassembler.h" #include "llvm-c/Disassembler.h" - #include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCContext.h" #include "llvm/MC/MCDisassembler.h" @@ -18,10 +17,9 @@ #include "llvm/MC/MCInstrInfo.h" #include "llvm/MC/MCRegisterInfo.h" #include "llvm/MC/MCSubtargetInfo.h" +#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/MemoryObject.h" #include "llvm/Support/TargetRegistry.h" -#include "llvm/Support/TargetSelect.h" -#include "llvm/Support/ErrorHandling.h" namespace llvm { class Target; @@ -35,41 +33,29 @@ using namespace llvm; // functions can all be passed as NULL. If successful, this returns a // disassembler context. If not, it returns NULL. // -LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo, - int TagType, LLVMOpInfoCallback GetOpInfo, - LLVMSymbolLookupCallback SymbolLookUp) { - // Initialize targets and assembly printers/parsers. - // FIXME: Clients are responsible for initializing the targets. And this - // would be done by calling routines in "llvm-c/Target.h" which are static - // line functions. But the current use of LLVMCreateDisasm() is to dynamically - // load libLTO with dlopen() and then lookup the symbols using dlsym(). - // And since these initialize routines are static that does not work which - // is why the call to them in this 'C' library API was added back. - llvm::InitializeAllTargetInfos(); - llvm::InitializeAllTargetMCs(); - llvm::InitializeAllAsmParsers(); - llvm::InitializeAllDisassemblers(); - +LLVMDisasmContextRef LLVMCreateDisasmCPU(const char *Triple, const char *CPU, + void *DisInfo, int TagType, + LLVMOpInfoCallback GetOpInfo, + LLVMSymbolLookupCallback SymbolLookUp){ // Get the target. std::string Error; - const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); + const Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); assert(TheTarget && "Unable to create target!"); // Get the assembler info needed to setup the MCContext. - const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName); + const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(Triple); assert(MAI && "Unable to create target asm info!"); const MCInstrInfo *MII = TheTarget->createMCInstrInfo(); assert(MII && "Unable to create target instruction info!"); - const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName); + const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(Triple); assert(MRI && "Unable to create target register info!"); // Package up features to be passed to target/subtarget std::string FeaturesStr; - std::string CPU; - const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(TripleName, CPU, + const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(Triple, CPU, FeaturesStr); assert(STI && "Unable to create subtarget info!"); @@ -88,7 +74,7 @@ LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo, *MAI, *MII, *MRI, *STI); assert(IP && "Unable to create instruction printer!"); - LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType, + LLVMDisasmContext *DC = new LLVMDisasmContext(Triple, DisInfo, TagType, GetOpInfo, SymbolLookUp, TheTarget, MAI, MRI, STI, MII, Ctx, DisAsm, IP); @@ -97,6 +83,13 @@ LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo, return DC; } +LLVMDisasmContextRef LLVMCreateDisasm(const char *Triple, void *DisInfo, + int TagType, LLVMOpInfoCallback GetOpInfo, + LLVMSymbolLookupCallback SymbolLookUp) { + return LLVMCreateDisasmCPU(Triple, "", DisInfo, TagType, GetOpInfo, + SymbolLookUp); +} + // // LLVMDisasmDispose() disposes of the disassembler specified by the context. // @@ -184,3 +177,39 @@ size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes, } llvm_unreachable("Invalid DecodeStatus!"); } + +// +// LLVMSetDisasmOptions() sets the disassembler's options. It returns 1 if it +// can set all the Options and 0 otherwise. +// +int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){ + if (Options & LLVMDisassembler_Option_UseMarkup){ + LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; + MCInstPrinter *IP = DC->getIP(); + IP->setUseMarkup(1); + Options &= ~LLVMDisassembler_Option_UseMarkup; + } + if (Options & LLVMDisassembler_Option_PrintImmHex){ + LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; + MCInstPrinter *IP = DC->getIP(); + IP->setPrintImmHex(1); + Options &= ~LLVMDisassembler_Option_PrintImmHex; + } + if (Options & LLVMDisassembler_Option_AsmPrinterVariant){ + LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; + // Try to set up the new instruction printer. + const MCAsmInfo *MAI = DC->getAsmInfo(); + const MCInstrInfo *MII = DC->getInstrInfo(); + const MCRegisterInfo *MRI = DC->getRegisterInfo(); + const MCSubtargetInfo *STI = DC->getSubtargetInfo(); + int AsmPrinterVariant = MAI->getAssemblerDialect(); + AsmPrinterVariant = AsmPrinterVariant == 0 ? 1 : 0; + MCInstPrinter *IP = DC->getTarget()->createMCInstPrinter( + AsmPrinterVariant, *MAI, *MII, *MRI, *STI); + if (IP) { + DC->setIP(IP); + Options &= ~LLVMDisassembler_Option_AsmPrinterVariant; + } + } + return (Options == 0); +} diff --git a/lib/MC/MCDisassembler/Disassembler.h b/lib/MC/MCDisassembler/Disassembler.h index 322abd5..6eb59d0 100644 --- a/lib/MC/MCDisassembler/Disassembler.h +++ b/lib/MC/MCDisassembler/Disassembler.h @@ -18,10 +18,10 @@ #define LLVM_MC_DISASSEMBLER_H #include "llvm-c/Disassembler.h" -#include <string> #include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/SmallString.h" #include "llvm/Support/raw_ostream.h" +#include <string> namespace llvm { class MCContext; @@ -109,7 +109,11 @@ public: const Target *getTarget() const { return TheTarget; } const MCDisassembler *getDisAsm() const { return DisAsm.get(); } const MCAsmInfo *getAsmInfo() const { return MAI.get(); } + const MCInstrInfo *getInstrInfo() const { return MII.get(); } + const MCRegisterInfo *getRegisterInfo() const { return MRI.get(); } + const MCSubtargetInfo *getSubtargetInfo() const { return MSI.get(); } MCInstPrinter *getIP() { return IP.get(); } + void setIP(MCInstPrinter *NewIP) { IP.reset(NewIP); } }; } // namespace llvm diff --git a/lib/MC/MCDisassembler/EDDisassembler.cpp b/lib/MC/MCDisassembler/EDDisassembler.cpp deleted file mode 100644 index 1226f1a..0000000 --- a/lib/MC/MCDisassembler/EDDisassembler.cpp +++ /dev/null @@ -1,399 +0,0 @@ -//===-EDDisassembler.cpp - LLVM Enhanced Disassembler ---------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file implements the Enhanced Disassembly library's disassembler class. -// The disassembler is responsible for vending individual instructions according -// to a given architecture and disassembly syntax. -// -//===----------------------------------------------------------------------===// - -#include "EDDisassembler.h" -#include "EDInst.h" -#include "llvm/MC/EDInstInfo.h" -#include "llvm/MC/MCAsmInfo.h" -#include "llvm/MC/MCContext.h" -#include "llvm/MC/MCDisassembler.h" -#include "llvm/MC/MCExpr.h" -#include "llvm/MC/MCInst.h" -#include "llvm/MC/MCInstPrinter.h" -#include "llvm/MC/MCInstrInfo.h" -#include "llvm/MC/MCRegisterInfo.h" -#include "llvm/MC/MCStreamer.h" -#include "llvm/MC/MCSubtargetInfo.h" -#include "llvm/MC/MCParser/AsmLexer.h" -#include "llvm/MC/MCParser/MCAsmParser.h" -#include "llvm/MC/MCParser/MCParsedAsmOperand.h" -#include "llvm/MC/MCTargetAsmLexer.h" -#include "llvm/MC/MCTargetAsmParser.h" -#include "llvm/Support/MemoryBuffer.h" -#include "llvm/Support/MemoryObject.h" -#include "llvm/Support/SourceMgr.h" -#include "llvm/Support/TargetRegistry.h" -using namespace llvm; - -EDDisassembler::DisassemblerMap_t EDDisassembler::sDisassemblers; - -struct TripleMap { - Triple::ArchType Arch; - const char *String; -}; - -static const struct TripleMap triplemap[] = { - { Triple::x86, "i386-unknown-unknown" }, - { Triple::x86_64, "x86_64-unknown-unknown" }, - { Triple::arm, "arm-unknown-unknown" }, - { Triple::thumb, "thumb-unknown-unknown" } -}; - -/// infoFromArch - Returns the TripleMap corresponding to a given architecture, -/// or NULL if there is an error -/// -/// @arg arch - The Triple::ArchType for the desired architecture -static const char *tripleFromArch(Triple::ArchType arch) { - unsigned int infoIndex; - - for (infoIndex = 0; triplemap[infoIndex].String != NULL; ++infoIndex) { - if (arch == triplemap[infoIndex].Arch) - return triplemap[infoIndex].String; - } - - return NULL; -} - -/// getLLVMSyntaxVariant - gets the constant to use to get an assembly printer -/// for the desired assembly syntax, suitable for passing to -/// Target::createMCInstPrinter() -/// -/// @arg arch - The target architecture -/// @arg syntax - The assembly syntax in sd form -static int getLLVMSyntaxVariant(Triple::ArchType arch, - EDDisassembler::AssemblySyntax syntax) { - switch (syntax) { - // Mappings below from X86AsmPrinter.cpp - case EDDisassembler::kEDAssemblySyntaxX86ATT: - if (arch == Triple::x86 || arch == Triple::x86_64) - return 0; - break; - case EDDisassembler::kEDAssemblySyntaxX86Intel: - if (arch == Triple::x86 || arch == Triple::x86_64) - return 1; - break; - case EDDisassembler::kEDAssemblySyntaxARMUAL: - if (arch == Triple::arm || arch == Triple::thumb) - return 0; - break; - } - - return -1; -} - -EDDisassembler *EDDisassembler::getDisassembler(Triple::ArchType arch, - AssemblySyntax syntax) { - const char *triple = tripleFromArch(arch); - return getDisassembler(StringRef(triple), syntax); -} - -EDDisassembler *EDDisassembler::getDisassembler(StringRef str, - AssemblySyntax syntax) { - CPUKey key; - key.Triple = str.str(); - key.Syntax = syntax; - - EDDisassembler::DisassemblerMap_t::iterator i = sDisassemblers.find(key); - - if (i != sDisassemblers.end()) { - return i->second; - } - - EDDisassembler *sdd = new EDDisassembler(key); - if (!sdd->valid()) { - delete sdd; - return NULL; - } - - sDisassemblers[key] = sdd; - - return sdd; -} - -EDDisassembler::EDDisassembler(CPUKey &key) : - Valid(false), - HasSemantics(false), - ErrorStream(nulls()), - Key(key), - TgtTriple(key.Triple.c_str()) { - - LLVMSyntaxVariant = getLLVMSyntaxVariant(TgtTriple.getArch(), key.Syntax); - - if (LLVMSyntaxVariant < 0) - return; - - std::string tripleString(key.Triple); - std::string errorString; - - Tgt = TargetRegistry::lookupTarget(key.Triple, - errorString); - - if (!Tgt) - return; - - MRI.reset(Tgt->createMCRegInfo(tripleString)); - - if (!MRI) - return; - - initMaps(*MRI); - - AsmInfo.reset(Tgt->createMCAsmInfo(tripleString)); - - if (!AsmInfo) - return; - - STI.reset(Tgt->createMCSubtargetInfo(tripleString, "", "")); - - if (!STI) - return; - - Disassembler.reset(Tgt->createMCDisassembler(*STI)); - - if (!Disassembler) - return; - - InstInfos = Disassembler->getEDInfo(); - - MII.reset(Tgt->createMCInstrInfo()); - - if (!MII) - return; - - InstString.reset(new std::string); - InstStream.reset(new raw_string_ostream(*InstString)); - InstPrinter.reset(Tgt->createMCInstPrinter(LLVMSyntaxVariant, *AsmInfo, - *MII, *MRI, *STI)); - - if (!InstPrinter) - return; - - GenericAsmLexer.reset(new AsmLexer(*AsmInfo)); - SpecificAsmLexer.reset(Tgt->createMCAsmLexer(*MRI, *AsmInfo)); - SpecificAsmLexer->InstallLexer(*GenericAsmLexer); - - initMaps(*MRI); - - Valid = true; -} - -EDDisassembler::~EDDisassembler() { - if (!valid()) - return; -} - -namespace { - /// EDMemoryObject - a subclass of MemoryObject that allows use of a callback - /// as provided by the sd interface. See MemoryObject. - class EDMemoryObject : public llvm::MemoryObject { - private: - EDByteReaderCallback Callback; - void *Arg; - public: - EDMemoryObject(EDByteReaderCallback callback, - void *arg) : Callback(callback), Arg(arg) { } - ~EDMemoryObject() { } - uint64_t getBase() const { return 0x0; } - uint64_t getExtent() const { return (uint64_t)-1; } - int readByte(uint64_t address, uint8_t *ptr) const { - if (!Callback) - return -1; - - if (Callback(ptr, address, Arg)) - return -1; - - return 0; - } - }; -} - -EDInst *EDDisassembler::createInst(EDByteReaderCallback byteReader, - uint64_t address, - void *arg) { - EDMemoryObject memoryObject(byteReader, arg); - - MCInst* inst = new MCInst; - uint64_t byteSize; - - MCDisassembler::DecodeStatus S; - S = Disassembler->getInstruction(*inst, byteSize, memoryObject, address, - ErrorStream, nulls()); - switch (S) { - case MCDisassembler::Fail: - case MCDisassembler::SoftFail: - // FIXME: Do something different on soft failure mode? - delete inst; - return NULL; - - case MCDisassembler::Success: { - const llvm::EDInstInfo *thisInstInfo = NULL; - - if (InstInfos) { - thisInstInfo = &InstInfos[inst->getOpcode()]; - } - - EDInst* sdInst = new EDInst(inst, byteSize, *this, thisInstInfo); - return sdInst; - } - } - return NULL; -} - -void EDDisassembler::initMaps(const MCRegisterInfo ®isterInfo) { - unsigned numRegisters = registerInfo.getNumRegs(); - unsigned registerIndex; - - for (registerIndex = 0; registerIndex < numRegisters; ++registerIndex) { - const char* registerName = registerInfo.getName(registerIndex); - - RegVec.push_back(registerName); - RegRMap[registerName] = registerIndex; - } - - switch (TgtTriple.getArch()) { - default: - break; - case Triple::x86: - case Triple::x86_64: - stackPointers.insert(registerIDWithName("SP")); - stackPointers.insert(registerIDWithName("ESP")); - stackPointers.insert(registerIDWithName("RSP")); - - programCounters.insert(registerIDWithName("IP")); - programCounters.insert(registerIDWithName("EIP")); - programCounters.insert(registerIDWithName("RIP")); - break; - case Triple::arm: - case Triple::thumb: - stackPointers.insert(registerIDWithName("SP")); - - programCounters.insert(registerIDWithName("PC")); - break; - } -} - -const char *EDDisassembler::nameWithRegisterID(unsigned registerID) const { - if (registerID >= RegVec.size()) - return NULL; - else - return RegVec[registerID].c_str(); -} - -unsigned EDDisassembler::registerIDWithName(const char *name) const { - regrmap_t::const_iterator iter = RegRMap.find(std::string(name)); - if (iter == RegRMap.end()) - return 0; - else - return (*iter).second; -} - -bool EDDisassembler::registerIsStackPointer(unsigned registerID) { - return (stackPointers.find(registerID) != stackPointers.end()); -} - -bool EDDisassembler::registerIsProgramCounter(unsigned registerID) { - return (programCounters.find(registerID) != programCounters.end()); -} - -int EDDisassembler::printInst(std::string &str, MCInst &inst) { - PrinterMutex.acquire(); - - InstPrinter->printInst(&inst, *InstStream, ""); - InstStream->flush(); - str = *InstString; - InstString->clear(); - - PrinterMutex.release(); - - return 0; -} - -static void diag_handler(const SMDiagnostic &diag, void *context) { - if (context) - diag.print("", static_cast<EDDisassembler*>(context)->ErrorStream); -} - -int EDDisassembler::parseInst(SmallVectorImpl<MCParsedAsmOperand*> &operands, - SmallVectorImpl<AsmToken> &tokens, - const std::string &str) { - int ret = 0; - - switch (TgtTriple.getArch()) { - default: - return -1; - case Triple::x86: - case Triple::x86_64: - case Triple::arm: - case Triple::thumb: - break; - } - - const char *cStr = str.c_str(); - MemoryBuffer *buf = MemoryBuffer::getMemBuffer(cStr, cStr + strlen(cStr)); - - StringRef instName; - SMLoc instLoc; - - SourceMgr sourceMgr; - sourceMgr.setDiagHandler(diag_handler, static_cast<void*>(this)); - sourceMgr.AddNewSourceBuffer(buf, SMLoc()); // ownership of buf handed over - MCContext context(*AsmInfo, *MRI, NULL); - OwningPtr<MCStreamer> streamer(createNullStreamer(context)); - OwningPtr<MCAsmParser> genericParser(createMCAsmParser(sourceMgr, - context, *streamer, - *AsmInfo)); - - OwningPtr<MCSubtargetInfo> STI(Tgt->createMCSubtargetInfo(Key.Triple.c_str(), "", "")); - OwningPtr<MCTargetAsmParser> - TargetParser(Tgt->createMCAsmParser(*STI, *genericParser)); - - AsmToken OpcodeToken = genericParser->Lex(); - AsmToken NextToken = genericParser->Lex(); // consume next token, because specificParser expects us to - - if (OpcodeToken.is(AsmToken::Identifier)) { - instName = OpcodeToken.getString(); - instLoc = OpcodeToken.getLoc(); - - if (NextToken.isNot(AsmToken::Eof) && - TargetParser->ParseInstruction(instName, instLoc, operands)) - ret = -1; - } else { - ret = -1; - } - - ParserMutex.acquire(); - - if (!ret) { - GenericAsmLexer->setBuffer(buf); - - while (SpecificAsmLexer->Lex(), - SpecificAsmLexer->isNot(AsmToken::Eof) && - SpecificAsmLexer->isNot(AsmToken::EndOfStatement)) { - if (SpecificAsmLexer->is(AsmToken::Error)) { - ret = -1; - break; - } - tokens.push_back(SpecificAsmLexer->getTok()); - } - } - - ParserMutex.release(); - - return ret; -} - -int EDDisassembler::llvmSyntaxVariant() const { - return LLVMSyntaxVariant; -} diff --git a/lib/MC/MCDisassembler/EDDisassembler.h b/lib/MC/MCDisassembler/EDDisassembler.h deleted file mode 100644 index 6f71908..0000000 --- a/lib/MC/MCDisassembler/EDDisassembler.h +++ /dev/null @@ -1,271 +0,0 @@ -//===-- EDDisassembler.h - LLVM Enhanced Disassembler -----------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file defines the interface for the Enhanced Disassembly library's -// disassembler class. The disassembler is responsible for vending individual -// instructions according to a given architecture and disassembly syntax. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_EDDISASSEMBLER_H -#define LLVM_EDDISASSEMBLER_H - -#include "EDInfo.h" - -#include "llvm/ADT/OwningPtr.h" -#include "llvm/ADT/Triple.h" -#include "llvm/Support/raw_ostream.h" -#include "llvm/Support/Mutex.h" - -#include <map> -#include <set> -#include <string> -#include <vector> - -namespace llvm { -class AsmLexer; -class AsmParser; -class AsmToken; -class MCContext; -class MCAsmInfo; -class MCAsmLexer; -class MCDisassembler; -class MCInst; -class MCInstPrinter; -class MCInstrInfo; -class MCParsedAsmOperand; -class MCRegisterInfo; -class MCStreamer; -class MCSubtargetInfo; -class MCTargetAsmLexer; -class MCTargetAsmParser; -template <typename T> class SmallVectorImpl; -class SourceMgr; -class Target; - -struct EDInstInfo; -struct EDInst; -struct EDOperand; -struct EDToken; - -typedef int (*EDByteReaderCallback)(uint8_t *byte, uint64_t address, void *arg); - -/// EDDisassembler - Encapsulates a disassembler for a single architecture and -/// disassembly syntax. Also manages the static disassembler registry. -struct EDDisassembler { - typedef enum { - /*! @constant kEDAssemblySyntaxX86Intel Intel syntax for i386 and x86_64. */ - kEDAssemblySyntaxX86Intel = 0, - /*! @constant kEDAssemblySyntaxX86ATT AT&T syntax for i386 and x86_64. */ - kEDAssemblySyntaxX86ATT = 1, - kEDAssemblySyntaxARMUAL = 2 - } AssemblySyntax; - - - //////////////////// - // Static members // - //////////////////// - - /// CPUKey - Encapsulates the descriptor of an architecture/disassembly-syntax - /// pair - struct CPUKey { - /// The architecture type - std::string Triple; - - /// The assembly syntax - AssemblySyntax Syntax; - - /// operator== - Equality operator - bool operator==(const CPUKey &key) const { - return (Triple == key.Triple && - Syntax == key.Syntax); - } - - /// operator< - Less-than operator - bool operator<(const CPUKey &key) const { - return ((Triple < key.Triple) || - ((Triple == key.Triple) && Syntax < (key.Syntax))); - } - }; - - typedef std::map<CPUKey, EDDisassembler*> DisassemblerMap_t; - - /// A map from disassembler specifications to disassemblers. Populated - /// lazily. - static DisassemblerMap_t sDisassemblers; - - /// getDisassembler - Returns the specified disassemble, or NULL on failure - /// - /// @arg arch - The desired architecture - /// @arg syntax - The desired disassembly syntax - static EDDisassembler *getDisassembler(llvm::Triple::ArchType arch, - AssemblySyntax syntax); - - /// getDisassembler - Returns the disassembler for a given combination of - /// CPU type, CPU subtype, and assembly syntax, or NULL on failure - /// - /// @arg str - The string representation of the architecture triple, e.g., - /// "x86_64-apple-darwin" - /// @arg syntax - The disassembly syntax for the required disassembler - static EDDisassembler *getDisassembler(llvm::StringRef str, - AssemblySyntax syntax); - - //////////////////////// - // Per-object members // - //////////////////////// - - /// True only if the object has been successfully initialized - bool Valid; - /// True if the disassembler can provide semantic information - bool HasSemantics; - - /// The stream to write errors to - llvm::raw_ostream &ErrorStream; - - /// The triple/syntax pair for the current architecture - CPUKey Key; - /// The Triple fur the current architecture - Triple TgtTriple; - /// The LLVM target corresponding to the disassembler - const llvm::Target *Tgt; - /// The assembly information for the target architecture - llvm::OwningPtr<const llvm::MCAsmInfo> AsmInfo; - /// The subtarget information for the target architecture - llvm::OwningPtr<const llvm::MCSubtargetInfo> STI; - // The instruction information for the target architecture. - llvm::OwningPtr<const llvm::MCInstrInfo> MII; - // The register information for the target architecture. - llvm::OwningPtr<const llvm::MCRegisterInfo> MRI; - /// The disassembler for the target architecture - llvm::OwningPtr<const llvm::MCDisassembler> Disassembler; - /// The output string for the instruction printer; must be guarded with - /// PrinterMutex - llvm::OwningPtr<std::string> InstString; - /// The output stream for the disassembler; must be guarded with - /// PrinterMutex - llvm::OwningPtr<llvm::raw_string_ostream> InstStream; - /// The instruction printer for the target architecture; must be guarded with - /// PrinterMutex when printing - llvm::OwningPtr<llvm::MCInstPrinter> InstPrinter; - /// The mutex that guards the instruction printer's printing functions, which - /// use a shared stream - llvm::sys::Mutex PrinterMutex; - /// The array of instruction information provided by the TableGen backend for - /// the target architecture - const llvm::EDInstInfo *InstInfos; - /// The target-specific lexer for use in tokenizing strings, in - /// target-independent and target-specific portions - llvm::OwningPtr<llvm::AsmLexer> GenericAsmLexer; - llvm::OwningPtr<llvm::MCTargetAsmLexer> SpecificAsmLexer; - /// The guard for the above - llvm::sys::Mutex ParserMutex; - /// The LLVM number used for the target disassembly syntax variant - int LLVMSyntaxVariant; - - typedef std::vector<std::string> regvec_t; - typedef std::map<std::string, unsigned> regrmap_t; - - /// A vector of registers for quick mapping from LLVM register IDs to names - regvec_t RegVec; - /// A map of registers for quick mapping from register names to LLVM IDs - regrmap_t RegRMap; - - /// A set of register IDs for aliases of the stack pointer for the current - /// architecture - std::set<unsigned> stackPointers; - /// A set of register IDs for aliases of the program counter for the current - /// architecture - std::set<unsigned> programCounters; - - /// Constructor - initializes a disassembler with all the necessary objects, - /// which come pre-allocated from the registry accessor function - /// - /// @arg key - the architecture and disassembly syntax for the - /// disassembler - EDDisassembler(CPUKey& key); - - /// valid - reports whether there was a failure in the constructor. - bool valid() { - return Valid; - } - - /// hasSemantics - reports whether the disassembler can provide operands and - /// tokens. - bool hasSemantics() { - return HasSemantics; - } - - ~EDDisassembler(); - - /// createInst - creates and returns an instruction given a callback and - /// memory address, or NULL on failure - /// - /// @arg byteReader - A callback function that provides machine code bytes - /// @arg address - The address of the first byte of the instruction, - /// suitable for passing to byteReader - /// @arg arg - An opaque argument for byteReader - EDInst *createInst(EDByteReaderCallback byteReader, - uint64_t address, - void *arg); - - /// initMaps - initializes regVec and regRMap using the provided register - /// info - /// - /// @arg registerInfo - the register information to use as a source - void initMaps(const llvm::MCRegisterInfo ®isterInfo); - /// nameWithRegisterID - Returns the name (owned by the EDDisassembler) of a - /// register for a given register ID, or NULL on failure - /// - /// @arg registerID - the ID of the register to be queried - const char *nameWithRegisterID(unsigned registerID) const; - /// registerIDWithName - Returns the ID of a register for a given register - /// name, or (unsigned)-1 on failure - /// - /// @arg name - The name of the register - unsigned registerIDWithName(const char *name) const; - - /// registerIsStackPointer - reports whether a register ID is an alias for the - /// stack pointer register - /// - /// @arg registerID - The LLVM register ID - bool registerIsStackPointer(unsigned registerID); - /// registerIsStackPointer - reports whether a register ID is an alias for the - /// stack pointer register - /// - /// @arg registerID - The LLVM register ID - bool registerIsProgramCounter(unsigned registerID); - - /// printInst - prints an MCInst to a string, returning 0 on success, or -1 - /// otherwise - /// - /// @arg str - A reference to a string which is filled in with the string - /// representation of the instruction - /// @arg inst - A reference to the MCInst to be printed - int printInst(std::string& str, - llvm::MCInst& inst); - - /// parseInst - extracts operands and tokens from a string for use in - /// tokenizing the string. Returns 0 on success, or -1 otherwise. - /// - /// @arg operands - A reference to a vector that will be filled in with the - /// parsed operands - /// @arg tokens - A reference to a vector that will be filled in with the - /// tokens - /// @arg str - The string representation of the instruction - int parseInst(llvm::SmallVectorImpl<llvm::MCParsedAsmOperand*> &operands, - llvm::SmallVectorImpl<llvm::AsmToken> &tokens, - const std::string &str); - - /// llvmSyntaxVariant - returns the LLVM syntax variant for this disassembler - int llvmSyntaxVariant() const; -}; - -} // end namespace llvm - -#endif diff --git a/lib/MC/MCDisassembler/EDInfo.h b/lib/MC/MCDisassembler/EDInfo.h deleted file mode 100644 index e43ad16..0000000 --- a/lib/MC/MCDisassembler/EDInfo.h +++ /dev/null @@ -1,84 +0,0 @@ -//===-- EDInfo.h - LLVM Enhanced Disassembler -------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_EDINFO_H -#define LLVM_EDINFO_H - -enum { - EDIS_MAX_OPERANDS = 13, - EDIS_MAX_SYNTAXES = 2 -}; - -enum OperandTypes { - kOperandTypeNone, - kOperandTypeImmediate, - kOperandTypeRegister, - kOperandTypeX86Memory, - kOperandTypeX86EffectiveAddress, - kOperandTypeX86PCRelative, - kOperandTypeARMBranchTarget, - kOperandTypeARMSoReg, - kOperandTypeARMSoImm, - kOperandTypeARMRotImm, - kOperandTypeARMSoImm2Part, - kOperandTypeARMPredicate, - kOperandTypeAddrModeImm12, - kOperandTypeLdStSOReg, - kOperandTypeARMAddrMode2, - kOperandTypeARMAddrMode2Offset, - kOperandTypeARMAddrMode3, - kOperandTypeARMAddrMode3Offset, - kOperandTypeARMAddrMode4, - kOperandTypeARMAddrMode5, - kOperandTypeARMAddrMode6, - kOperandTypeARMAddrMode6Offset, - kOperandTypeARMAddrMode7, - kOperandTypeARMAddrModePC, - kOperandTypeARMRegisterList, - kOperandTypeARMDPRRegisterList, - kOperandTypeARMSPRRegisterList, - kOperandTypeARMTBAddrMode, - kOperandTypeThumbITMask, - kOperandTypeThumbAddrModeRegS1, - kOperandTypeThumbAddrModeRegS2, - kOperandTypeThumbAddrModeRegS4, - kOperandTypeThumbAddrModeImmS1, - kOperandTypeThumbAddrModeImmS2, - kOperandTypeThumbAddrModeImmS4, - kOperandTypeThumbAddrModeRR, - kOperandTypeThumbAddrModeSP, - kOperandTypeThumbAddrModePC, - kOperandTypeThumb2AddrModeReg, - kOperandTypeThumb2SoReg, - kOperandTypeThumb2SoImm, - kOperandTypeThumb2AddrModeImm8, - kOperandTypeThumb2AddrModeImm8Offset, - kOperandTypeThumb2AddrModeImm12, - kOperandTypeThumb2AddrModeSoReg, - kOperandTypeThumb2AddrModeImm8s4, - kOperandTypeThumb2AddrModeImm8s4Offset -}; - -enum OperandFlags { - kOperandFlagSource = 0x1, - kOperandFlagTarget = 0x2 -}; - -enum InstructionTypes { - kInstructionTypeNone, - kInstructionTypeMove, - kInstructionTypeBranch, - kInstructionTypePush, - kInstructionTypePop, - kInstructionTypeCall, - kInstructionTypeReturn -}; - - -#endif diff --git a/lib/MC/MCDisassembler/EDInst.cpp b/lib/MC/MCDisassembler/EDInst.cpp deleted file mode 100644 index 6057e16..0000000 --- a/lib/MC/MCDisassembler/EDInst.cpp +++ /dev/null @@ -1,212 +0,0 @@ -//===-EDInst.cpp - LLVM Enhanced Disassembler -----------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file implements the Enhanced Disassembly library's instruction class. -// The instruction is responsible for vending the string representation, -// individual tokens, and operands for a single instruction. -// -//===----------------------------------------------------------------------===// - -#include "EDInst.h" -#include "EDDisassembler.h" -#include "EDOperand.h" -#include "EDToken.h" - -#include "llvm/MC/EDInstInfo.h" -#include "llvm/MC/MCInst.h" - -using namespace llvm; - -EDInst::EDInst(llvm::MCInst *inst, - uint64_t byteSize, - EDDisassembler &disassembler, - const llvm::EDInstInfo *info) : - Disassembler(disassembler), - Inst(inst), - ThisInstInfo(info), - ByteSize(byteSize), - BranchTarget(-1), - MoveSource(-1), - MoveTarget(-1) { - OperandOrder = ThisInstInfo->operandOrders[Disassembler.llvmSyntaxVariant()]; -} - -EDInst::~EDInst() { - unsigned int index; - unsigned int numOperands = Operands.size(); - - for (index = 0; index < numOperands; ++index) - delete Operands[index]; - - unsigned int numTokens = Tokens.size(); - - for (index = 0; index < numTokens; ++index) - delete Tokens[index]; - - delete Inst; -} - -uint64_t EDInst::byteSize() { - return ByteSize; -} - -int EDInst::stringify() { - if (StringifyResult.valid()) - return StringifyResult.result(); - - if (Disassembler.printInst(String, *Inst)) - return StringifyResult.setResult(-1); - - String.push_back('\n'); - - return StringifyResult.setResult(0); -} - -int EDInst::getString(const char*& str) { - if (stringify()) - return -1; - - str = String.c_str(); - - return 0; -} - -unsigned EDInst::instID() { - return Inst->getOpcode(); -} - -bool EDInst::isBranch() { - if (ThisInstInfo) - return - ThisInstInfo->instructionType == kInstructionTypeBranch || - ThisInstInfo->instructionType == kInstructionTypeCall; - else - return false; -} - -bool EDInst::isMove() { - if (ThisInstInfo) - return ThisInstInfo->instructionType == kInstructionTypeMove; - else - return false; -} - -int EDInst::parseOperands() { - if (ParseResult.valid()) - return ParseResult.result(); - - if (!ThisInstInfo) - return ParseResult.setResult(-1); - - unsigned int opIndex; - unsigned int mcOpIndex = 0; - - for (opIndex = 0; opIndex < ThisInstInfo->numOperands; ++opIndex) { - if (isBranch() && - (ThisInstInfo->operandFlags[opIndex] & kOperandFlagTarget)) { - BranchTarget = opIndex; - } - else if (isMove()) { - if (ThisInstInfo->operandFlags[opIndex] & kOperandFlagSource) - MoveSource = opIndex; - else if (ThisInstInfo->operandFlags[opIndex] & kOperandFlagTarget) - MoveTarget = opIndex; - } - - EDOperand *operand = new EDOperand(Disassembler, *this, opIndex, mcOpIndex); - - Operands.push_back(operand); - } - - return ParseResult.setResult(0); -} - -int EDInst::branchTargetID() { - if (parseOperands()) - return -1; - return BranchTarget; -} - -int EDInst::moveSourceID() { - if (parseOperands()) - return -1; - return MoveSource; -} - -int EDInst::moveTargetID() { - if (parseOperands()) - return -1; - return MoveTarget; -} - -int EDInst::numOperands() { - if (parseOperands()) - return -1; - return Operands.size(); -} - -int EDInst::getOperand(EDOperand *&operand, unsigned int index) { - if (parseOperands()) - return -1; - - if (index >= Operands.size()) - return -1; - - operand = Operands[index]; - return 0; -} - -int EDInst::tokenize() { - if (TokenizeResult.valid()) - return TokenizeResult.result(); - - if (ThisInstInfo == NULL) - return TokenizeResult.setResult(-1); - - if (stringify()) - return TokenizeResult.setResult(-1); - - return TokenizeResult.setResult(EDToken::tokenize(Tokens, - String, - OperandOrder, - Disassembler)); - -} - -int EDInst::numTokens() { - if (tokenize()) - return -1; - return Tokens.size(); -} - -int EDInst::getToken(EDToken *&token, unsigned int index) { - if (tokenize()) - return -1; - token = Tokens[index]; - return 0; -} - -#ifdef __BLOCKS__ -int EDInst::visitTokens(EDTokenVisitor_t visitor) { - if (tokenize()) - return -1; - - tokvec_t::iterator iter; - - for (iter = Tokens.begin(); iter != Tokens.end(); ++iter) { - int ret = visitor(*iter); - if (ret == 1) - return 0; - if (ret != 0) - return -1; - } - - return 0; -} -#endif diff --git a/lib/MC/MCDisassembler/EDInst.h b/lib/MC/MCDisassembler/EDInst.h deleted file mode 100644 index 6b78dc8..0000000 --- a/lib/MC/MCDisassembler/EDInst.h +++ /dev/null @@ -1,182 +0,0 @@ -//===-- EDInst.h - LLVM Enhanced Disassembler -------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file defines the interface for the Enhanced Disassembly library's -// instruction class. The instruction is responsible for vending the string -// representation, individual tokens and operands for a single instruction. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_EDINST_H -#define LLVM_EDINST_H - -#include "llvm/Support/DataTypes.h" -#include "llvm/ADT/SmallVector.h" -#include <string> -#include <vector> - -namespace llvm { - class MCInst; - struct EDInstInfo; - struct EDToken; - struct EDDisassembler; - struct EDOperand; - -#ifdef __BLOCKS__ - typedef int (^EDTokenVisitor_t)(EDToken *token); -#endif - -/// CachedResult - Encapsulates the result of a function along with the validity -/// of that result, so that slow functions don't need to run twice -struct CachedResult { - /// True if the result has been obtained by executing the function - bool Valid; - /// The result last obtained from the function - int Result; - - /// Constructor - Initializes an invalid result - CachedResult() : Valid(false) { } - /// valid - Returns true if the result has been obtained by executing the - /// function and false otherwise - bool valid() { return Valid; } - /// result - Returns the result of the function or an undefined value if - /// valid() is false - int result() { return Result; } - /// setResult - Sets the result of the function and declares it valid - /// returning the result (so that setResult() can be called from inside a - /// return statement) - /// @arg result - The result of the function - int setResult(int result) { Result = result; Valid = true; return result; } -}; - -/// EDInst - Encapsulates a single instruction, which can be queried for its -/// string representation, as well as its operands and tokens -struct EDInst { - /// The parent disassembler - EDDisassembler &Disassembler; - /// The containing MCInst - llvm::MCInst *Inst; - /// The instruction information provided by TableGen for this instruction - const llvm::EDInstInfo *ThisInstInfo; - /// The number of bytes for the machine code representation of the instruction - uint64_t ByteSize; - - /// The result of the stringify() function - CachedResult StringifyResult; - /// The string representation of the instruction - std::string String; - /// The order in which operands from the InstInfo's operand information appear - /// in String - const signed char* OperandOrder; - - /// The result of the parseOperands() function - CachedResult ParseResult; - typedef llvm::SmallVector<EDOperand*, 5> opvec_t; - /// The instruction's operands - opvec_t Operands; - /// The operand corresponding to the target, if the instruction is a branch - int BranchTarget; - /// The operand corresponding to the source, if the instruction is a move - int MoveSource; - /// The operand corresponding to the target, if the instruction is a move - int MoveTarget; - - /// The result of the tokenize() function - CachedResult TokenizeResult; - typedef std::vector<EDToken*> tokvec_t; - /// The instruction's tokens - tokvec_t Tokens; - - /// Constructor - initializes an instruction given the output of the LLVM - /// C++ disassembler - /// - /// @arg inst - The MCInst, which will now be owned by this object - /// @arg byteSize - The size of the consumed instruction, in bytes - /// @arg disassembler - The parent disassembler - /// @arg instInfo - The instruction information produced by the table - /// generator for this instruction - EDInst(llvm::MCInst *inst, - uint64_t byteSize, - EDDisassembler &disassembler, - const llvm::EDInstInfo *instInfo); - ~EDInst(); - - /// byteSize - returns the number of bytes consumed by the machine code - /// representation of the instruction - uint64_t byteSize(); - /// instID - returns the LLVM instruction ID of the instruction - unsigned instID(); - - /// stringify - populates the String and AsmString members of the instruction, - /// returning 0 on success or -1 otherwise - int stringify(); - /// getString - retrieves a pointer to the string representation of the - /// instructinon, returning 0 on success or -1 otherwise - /// - /// @arg str - A reference to a pointer that, on success, is set to point to - /// the string representation of the instruction; this string is still owned - /// by the instruction and will be deleted when it is - int getString(const char *&str); - - /// isBranch - Returns true if the instruction is a branch - bool isBranch(); - /// isMove - Returns true if the instruction is a move - bool isMove(); - - /// parseOperands - populates the Operands member of the instruction, - /// returning 0 on success or -1 otherwise - int parseOperands(); - /// branchTargetID - returns the ID (suitable for use with getOperand()) of - /// the target operand if the instruction is a branch, or -1 otherwise - int branchTargetID(); - /// moveSourceID - returns the ID of the source operand if the instruction - /// is a move, or -1 otherwise - int moveSourceID(); - /// moveTargetID - returns the ID of the target operand if the instruction - /// is a move, or -1 otherwise - int moveTargetID(); - - /// numOperands - returns the number of operands available to retrieve, or -1 - /// on error - int numOperands(); - /// getOperand - retrieves an operand from the instruction's operand list by - /// index, returning 0 on success or -1 on error - /// - /// @arg operand - A reference whose target is pointed at the operand on - /// success, although the operand is still owned by the EDInst - /// @arg index - The index of the operand in the instruction - int getOperand(EDOperand *&operand, unsigned int index); - - /// tokenize - populates the Tokens member of the instruction, returning 0 on - /// success or -1 otherwise - int tokenize(); - /// numTokens - returns the number of tokens in the instruction, or -1 on - /// error - int numTokens(); - /// getToken - retrieves a token from the instruction's token list by index, - /// returning 0 on success or -1 on error - /// - /// @arg token - A reference whose target is pointed at the token on success, - /// although the token is still owned by the EDInst - /// @arg index - The index of the token in the instrcutino - int getToken(EDToken *&token, unsigned int index); - -#ifdef __BLOCKS__ - /// visitTokens - Visits each token in turn and applies a block to it, - /// returning 0 if all blocks are visited and/or the block signals - /// termination by returning 1; returns -1 on error - /// - /// @arg visitor - The visitor block to apply to all tokens. - int visitTokens(EDTokenVisitor_t visitor); -#endif -}; - -} // end namespace llvm - -#endif diff --git a/lib/MC/MCDisassembler/EDMain.cpp b/lib/MC/MCDisassembler/EDMain.cpp deleted file mode 100644 index 5c065db..0000000 --- a/lib/MC/MCDisassembler/EDMain.cpp +++ /dev/null @@ -1,276 +0,0 @@ -//===-- EDMain.cpp - LLVM Enhanced Disassembly C API ----------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file implements the enhanced disassembler's public C API. -// -//===----------------------------------------------------------------------===// - -#include "EDDisassembler.h" -#include "EDInst.h" -#include "EDOperand.h" -#include "EDToken.h" -#include "llvm-c/EnhancedDisassembly.h" -using namespace llvm; - -int EDGetDisassembler(EDDisassemblerRef *disassembler, - const char *triple, - EDAssemblySyntax_t syntax) { - EDDisassembler::AssemblySyntax Syntax; - switch (syntax) { - default: llvm_unreachable("Unknown assembly syntax!"); - case kEDAssemblySyntaxX86Intel: - Syntax = EDDisassembler::kEDAssemblySyntaxX86Intel; - break; - case kEDAssemblySyntaxX86ATT: - Syntax = EDDisassembler::kEDAssemblySyntaxX86ATT; - break; - case kEDAssemblySyntaxARMUAL: - Syntax = EDDisassembler::kEDAssemblySyntaxARMUAL; - break; - } - - EDDisassemblerRef ret = EDDisassembler::getDisassembler(triple, Syntax); - - if (!ret) - return -1; - *disassembler = ret; - return 0; -} - -int EDGetRegisterName(const char** regName, - EDDisassemblerRef disassembler, - unsigned regID) { - const char *name = ((EDDisassembler*)disassembler)->nameWithRegisterID(regID); - if (!name) - return -1; - *regName = name; - return 0; -} - -int EDRegisterIsStackPointer(EDDisassemblerRef disassembler, - unsigned regID) { - return ((EDDisassembler*)disassembler)->registerIsStackPointer(regID) ? 1 : 0; -} - -int EDRegisterIsProgramCounter(EDDisassemblerRef disassembler, - unsigned regID) { - return ((EDDisassembler*)disassembler)->registerIsProgramCounter(regID) ? 1:0; -} - -unsigned int EDCreateInsts(EDInstRef *insts, - unsigned int count, - EDDisassemblerRef disassembler, - ::EDByteReaderCallback byteReader, - uint64_t address, - void *arg) { - unsigned int index; - - for (index = 0; index < count; ++index) { - EDInst *inst = ((EDDisassembler*)disassembler)->createInst(byteReader, - address, arg); - - if (!inst) - return index; - - insts[index] = inst; - address += inst->byteSize(); - } - - return count; -} - -void EDReleaseInst(EDInstRef inst) { - delete ((EDInst*)inst); -} - -int EDInstByteSize(EDInstRef inst) { - return ((EDInst*)inst)->byteSize(); -} - -int EDGetInstString(const char **buf, - EDInstRef inst) { - return ((EDInst*)inst)->getString(*buf); -} - -int EDInstID(unsigned *instID, EDInstRef inst) { - *instID = ((EDInst*)inst)->instID(); - return 0; -} - -int EDInstIsBranch(EDInstRef inst) { - return ((EDInst*)inst)->isBranch(); -} - -int EDInstIsMove(EDInstRef inst) { - return ((EDInst*)inst)->isMove(); -} - -int EDBranchTargetID(EDInstRef inst) { - return ((EDInst*)inst)->branchTargetID(); -} - -int EDMoveSourceID(EDInstRef inst) { - return ((EDInst*)inst)->moveSourceID(); -} - -int EDMoveTargetID(EDInstRef inst) { - return ((EDInst*)inst)->moveTargetID(); -} - -int EDNumTokens(EDInstRef inst) { - return ((EDInst*)inst)->numTokens(); -} - -int EDGetToken(EDTokenRef *token, - EDInstRef inst, - int index) { - return ((EDInst*)inst)->getToken(*(EDToken**)token, index); -} - -int EDGetTokenString(const char **buf, - EDTokenRef token) { - return ((EDToken*)token)->getString(*buf); -} - -int EDOperandIndexForToken(EDTokenRef token) { - return ((EDToken*)token)->operandID(); -} - -int EDTokenIsWhitespace(EDTokenRef token) { - return ((EDToken*)token)->type() == EDToken::kTokenWhitespace; -} - -int EDTokenIsPunctuation(EDTokenRef token) { - return ((EDToken*)token)->type() == EDToken::kTokenPunctuation; -} - -int EDTokenIsOpcode(EDTokenRef token) { - return ((EDToken*)token)->type() == EDToken::kTokenOpcode; -} - -int EDTokenIsLiteral(EDTokenRef token) { - return ((EDToken*)token)->type() == EDToken::kTokenLiteral; -} - -int EDTokenIsRegister(EDTokenRef token) { - return ((EDToken*)token)->type() == EDToken::kTokenRegister; -} - -int EDTokenIsNegativeLiteral(EDTokenRef token) { - if (((EDToken*)token)->type() != EDToken::kTokenLiteral) - return -1; - - return ((EDToken*)token)->literalSign(); -} - -int EDLiteralTokenAbsoluteValue(uint64_t *value, EDTokenRef token) { - if (((EDToken*)token)->type() != EDToken::kTokenLiteral) - return -1; - - return ((EDToken*)token)->literalAbsoluteValue(*value); -} - -int EDRegisterTokenValue(unsigned *registerID, - EDTokenRef token) { - if (((EDToken*)token)->type() != EDToken::kTokenRegister) - return -1; - - return ((EDToken*)token)->registerID(*registerID); -} - -int EDNumOperands(EDInstRef inst) { - return ((EDInst*)inst)->numOperands(); -} - -int EDGetOperand(EDOperandRef *operand, - EDInstRef inst, - int index) { - return ((EDInst*)inst)->getOperand(*(EDOperand**)operand, index); -} - -int EDOperandIsRegister(EDOperandRef operand) { - return ((EDOperand*)operand)->isRegister(); -} - -int EDOperandIsImmediate(EDOperandRef operand) { - return ((EDOperand*)operand)->isImmediate(); -} - -int EDOperandIsMemory(EDOperandRef operand) { - return ((EDOperand*)operand)->isMemory(); -} - -int EDRegisterOperandValue(unsigned *value, EDOperandRef operand) { - if (!((EDOperand*)operand)->isRegister()) - return -1; - *value = ((EDOperand*)operand)->regVal(); - return 0; -} - -int EDImmediateOperandValue(uint64_t *value, EDOperandRef operand) { - if (!((EDOperand*)operand)->isImmediate()) - return -1; - *value = ((EDOperand*)operand)->immediateVal(); - return 0; -} - -int EDEvaluateOperand(uint64_t *result, EDOperandRef operand, - ::EDRegisterReaderCallback regReader, void *arg) { - return ((EDOperand*)operand)->evaluate(*result, regReader, arg); -} - -#ifdef __BLOCKS__ - -struct ByteReaderWrapper { - EDByteBlock_t byteBlock; -}; - -static int readerWrapperCallback(uint8_t *byte, - uint64_t address, - void *arg) { - struct ByteReaderWrapper *wrapper = (struct ByteReaderWrapper *)arg; - return wrapper->byteBlock(byte, address); -} - -unsigned int EDBlockCreateInsts(EDInstRef *insts, - int count, - EDDisassemblerRef disassembler, - EDByteBlock_t byteBlock, - uint64_t address) { - struct ByteReaderWrapper wrapper; - wrapper.byteBlock = byteBlock; - - return EDCreateInsts(insts, count, disassembler, readerWrapperCallback, - address, (void*)&wrapper); -} - -int EDBlockEvaluateOperand(uint64_t *result, EDOperandRef operand, - EDRegisterBlock_t regBlock) { - return ((EDOperand*)operand)->evaluate(*result, regBlock); -} - -int EDBlockVisitTokens(EDInstRef inst, ::EDTokenVisitor_t visitor) { - return ((EDInst*)inst)->visitTokens((llvm::EDTokenVisitor_t)visitor); -} - -#else - -extern "C" unsigned int EDBlockCreateInsts() { - return 0; -} - -extern "C" int EDBlockEvaluateOperand() { - return -1; -} - -extern "C" int EDBlockVisitTokens() { - return -1; -} - -#endif diff --git a/lib/MC/MCDisassembler/EDOperand.cpp b/lib/MC/MCDisassembler/EDOperand.cpp deleted file mode 100644 index 48b3746..0000000 --- a/lib/MC/MCDisassembler/EDOperand.cpp +++ /dev/null @@ -1,315 +0,0 @@ -//===-- EDOperand.cpp - LLVM Enhanced Disassembler ------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file implements the Enhanced Disassembly library's operand class. The -// operand is responsible for allowing evaluation given a particular register -// context. -// -//===----------------------------------------------------------------------===// - -#include "EDOperand.h" -#include "EDDisassembler.h" -#include "EDInst.h" -#include "llvm/MC/EDInstInfo.h" -#include "llvm/MC/MCInst.h" -using namespace llvm; - -EDOperand::EDOperand(const EDDisassembler &disassembler, - const EDInst &inst, - unsigned int opIndex, - unsigned int &mcOpIndex) : - Disassembler(disassembler), - Inst(inst), - OpIndex(opIndex), - MCOpIndex(mcOpIndex) { - unsigned int numMCOperands = 0; - - Triple::ArchType arch = Disassembler.TgtTriple.getArch(); - - if (arch == Triple::x86 || - arch == Triple::x86_64) { - uint8_t operandType = inst.ThisInstInfo->operandTypes[opIndex]; - - switch (operandType) { - default: - break; - case kOperandTypeImmediate: - numMCOperands = 1; - break; - case kOperandTypeRegister: - numMCOperands = 1; - break; - case kOperandTypeX86Memory: - numMCOperands = 5; - break; - case kOperandTypeX86EffectiveAddress: - numMCOperands = 4; - break; - case kOperandTypeX86PCRelative: - numMCOperands = 1; - break; - } - } - else if (arch == Triple::arm || - arch == Triple::thumb) { - uint8_t operandType = inst.ThisInstInfo->operandTypes[opIndex]; - - switch (operandType) { - default: - case kOperandTypeARMRegisterList: - case kOperandTypeARMDPRRegisterList: - case kOperandTypeARMSPRRegisterList: - break; - case kOperandTypeImmediate: - case kOperandTypeRegister: - case kOperandTypeARMBranchTarget: - case kOperandTypeARMSoImm: - case kOperandTypeARMRotImm: - case kOperandTypeThumb2SoImm: - case kOperandTypeARMSoImm2Part: - case kOperandTypeARMPredicate: - case kOperandTypeThumbITMask: - case kOperandTypeThumb2AddrModeImm8Offset: - case kOperandTypeARMTBAddrMode: - case kOperandTypeThumb2AddrModeImm8s4Offset: - case kOperandTypeARMAddrMode7: - case kOperandTypeThumb2AddrModeReg: - numMCOperands = 1; - break; - case kOperandTypeThumb2SoReg: - case kOperandTypeAddrModeImm12: - case kOperandTypeARMAddrMode2Offset: - case kOperandTypeARMAddrMode3Offset: - case kOperandTypeARMAddrMode4: - case kOperandTypeARMAddrMode5: - case kOperandTypeARMAddrModePC: - case kOperandTypeThumb2AddrModeImm8: - case kOperandTypeThumb2AddrModeImm12: - case kOperandTypeThumb2AddrModeImm8s4: - case kOperandTypeThumbAddrModeImmS1: - case kOperandTypeThumbAddrModeImmS2: - case kOperandTypeThumbAddrModeImmS4: - case kOperandTypeThumbAddrModeRR: - case kOperandTypeThumbAddrModeSP: - case kOperandTypeThumbAddrModePC: - numMCOperands = 2; - break; - case kOperandTypeARMSoReg: - case kOperandTypeLdStSOReg: - case kOperandTypeARMAddrMode2: - case kOperandTypeARMAddrMode3: - case kOperandTypeThumb2AddrModeSoReg: - case kOperandTypeThumbAddrModeRegS1: - case kOperandTypeThumbAddrModeRegS2: - case kOperandTypeThumbAddrModeRegS4: - case kOperandTypeARMAddrMode6Offset: - numMCOperands = 3; - break; - case kOperandTypeARMAddrMode6: - numMCOperands = 4; - break; - } - } - - mcOpIndex += numMCOperands; -} - -EDOperand::~EDOperand() { -} - -int EDOperand::evaluate(uint64_t &result, - EDRegisterReaderCallback callback, - void *arg) { - uint8_t operandType = Inst.ThisInstInfo->operandTypes[OpIndex]; - - Triple::ArchType arch = Disassembler.TgtTriple.getArch(); - - switch (arch) { - default: - return -1; - case Triple::x86: - case Triple::x86_64: - switch (operandType) { - default: - return -1; - case kOperandTypeImmediate: - result = Inst.Inst->getOperand(MCOpIndex).getImm(); - return 0; - case kOperandTypeRegister: - { - unsigned reg = Inst.Inst->getOperand(MCOpIndex).getReg(); - return callback(&result, reg, arg); - } - case kOperandTypeX86PCRelative: - { - int64_t displacement = Inst.Inst->getOperand(MCOpIndex).getImm(); - - uint64_t ripVal; - - // TODO fix how we do this - - if (callback(&ripVal, Disassembler.registerIDWithName("RIP"), arg)) - return -1; - - result = ripVal + displacement; - return 0; - } - case kOperandTypeX86Memory: - case kOperandTypeX86EffectiveAddress: - { - unsigned baseReg = Inst.Inst->getOperand(MCOpIndex).getReg(); - uint64_t scaleAmount = Inst.Inst->getOperand(MCOpIndex+1).getImm(); - unsigned indexReg = Inst.Inst->getOperand(MCOpIndex+2).getReg(); - int64_t displacement = Inst.Inst->getOperand(MCOpIndex+3).getImm(); - - uint64_t addr = 0; - - unsigned segmentReg = Inst.Inst->getOperand(MCOpIndex+4).getReg(); - - if (segmentReg != 0 && arch == Triple::x86_64) { - unsigned fsID = Disassembler.registerIDWithName("FS"); - unsigned gsID = Disassembler.registerIDWithName("GS"); - - if (segmentReg == fsID || - segmentReg == gsID) { - uint64_t segmentBase; - if (!callback(&segmentBase, segmentReg, arg)) - addr += segmentBase; - } - } - - if (baseReg) { - uint64_t baseVal; - if (callback(&baseVal, baseReg, arg)) - return -1; - addr += baseVal; - } - - if (indexReg) { - uint64_t indexVal; - if (callback(&indexVal, indexReg, arg)) - return -1; - addr += (scaleAmount * indexVal); - } - - addr += displacement; - - result = addr; - return 0; - } - } // switch (operandType) - case Triple::arm: - case Triple::thumb: - switch (operandType) { - default: - return -1; - case kOperandTypeImmediate: - if (!Inst.Inst->getOperand(MCOpIndex).isImm()) - return -1; - - result = Inst.Inst->getOperand(MCOpIndex).getImm(); - return 0; - case kOperandTypeRegister: - { - if (!Inst.Inst->getOperand(MCOpIndex).isReg()) - return -1; - - unsigned reg = Inst.Inst->getOperand(MCOpIndex).getReg(); - return callback(&result, reg, arg); - } - case kOperandTypeARMBranchTarget: - { - if (!Inst.Inst->getOperand(MCOpIndex).isImm()) - return -1; - - int64_t displacement = Inst.Inst->getOperand(MCOpIndex).getImm(); - - uint64_t pcVal; - - if (callback(&pcVal, Disassembler.registerIDWithName("PC"), arg)) - return -1; - - result = pcVal + displacement; - return 0; - } - } - } -} - -int EDOperand::isRegister() { - return(Inst.ThisInstInfo->operandFlags[OpIndex] == kOperandTypeRegister); -} - -unsigned EDOperand::regVal() { - return Inst.Inst->getOperand(MCOpIndex).getReg(); -} - -int EDOperand::isImmediate() { - return(Inst.ThisInstInfo->operandFlags[OpIndex] == kOperandTypeImmediate); -} - -uint64_t EDOperand::immediateVal() { - return Inst.Inst->getOperand(MCOpIndex).getImm(); -} - -int EDOperand::isMemory() { - uint8_t operandType = Inst.ThisInstInfo->operandTypes[OpIndex]; - - switch (operandType) { - default: - return 0; - case kOperandTypeX86Memory: - case kOperandTypeX86PCRelative: - case kOperandTypeX86EffectiveAddress: - case kOperandTypeARMSoReg: - case kOperandTypeARMSoImm: - case kOperandTypeARMAddrMode2: - case kOperandTypeARMAddrMode2Offset: - case kOperandTypeARMAddrMode3: - case kOperandTypeARMAddrMode3Offset: - case kOperandTypeARMAddrMode4: - case kOperandTypeARMAddrMode5: - case kOperandTypeARMAddrMode6: - case kOperandTypeARMAddrMode7: - case kOperandTypeARMAddrModePC: - case kOperandTypeARMBranchTarget: - case kOperandTypeThumbAddrModeRegS1: - case kOperandTypeThumbAddrModeRegS2: - case kOperandTypeThumbAddrModeRegS4: - case kOperandTypeThumbAddrModeRR: - case kOperandTypeThumbAddrModeSP: - case kOperandTypeThumb2SoImm: - case kOperandTypeThumb2AddrModeImm8: - case kOperandTypeThumb2AddrModeImm8Offset: - case kOperandTypeThumb2AddrModeImm12: - case kOperandTypeThumb2AddrModeSoReg: - case kOperandTypeThumb2AddrModeImm8s4: - case kOperandTypeThumb2AddrModeReg: - return 1; - } -} - -#ifdef __BLOCKS__ -namespace { - struct RegisterReaderWrapper { - EDOperand::EDRegisterBlock_t regBlock; - }; -} - -static int readerWrapperCallback(uint64_t *value, unsigned regID, void *arg) { - RegisterReaderWrapper *wrapper = (RegisterReaderWrapper *)arg; - return wrapper->regBlock(value, regID); -} - -int EDOperand::evaluate(uint64_t &result, EDRegisterBlock_t regBlock) { - RegisterReaderWrapper wrapper; - wrapper.regBlock = regBlock; - return evaluate(result, readerWrapperCallback, (void*)&wrapper); -} -#endif diff --git a/lib/MC/MCDisassembler/EDOperand.h b/lib/MC/MCDisassembler/EDOperand.h deleted file mode 100644 index 50260ec..0000000 --- a/lib/MC/MCDisassembler/EDOperand.h +++ /dev/null @@ -1,91 +0,0 @@ -//===-EDOperand.h - LLVM Enhanced Disassembler ------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file defines the interface for the Enhanced Disassembly library's -// operand class. The operand is responsible for allowing evaluation given a -// particular register context. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_EDOPERAND_H -#define LLVM_EDOPERAND_H - -#include "llvm/Support/DataTypes.h" - -namespace llvm { - -struct EDDisassembler; -struct EDInst; - -typedef int (*EDRegisterReaderCallback)(uint64_t *value, unsigned regID, - void* arg); - - -/// EDOperand - Encapsulates a single operand, which can be evaluated by the -/// client -struct EDOperand { - /// The parent disassembler - const EDDisassembler &Disassembler; - /// The parent instruction - const EDInst &Inst; - - /// The index of the operand in the EDInst - unsigned int OpIndex; - /// The index of the first component of the operand in the MCInst - unsigned int MCOpIndex; - - /// Constructor - Initializes an EDOperand - /// - /// @arg disassembler - The disassembler responsible for the operand - /// @arg inst - The instruction containing this operand - /// @arg opIndex - The index of the operand in inst - /// @arg mcOpIndex - The index of the operand in the original MCInst - EDOperand(const EDDisassembler &disassembler, - const EDInst &inst, - unsigned int opIndex, - unsigned int &mcOpIndex); - ~EDOperand(); - - /// evaluate - Returns the numeric value of an operand to the extent possible, - /// returning 0 on success or -1 if there was some problem (such as a - /// register not being readable) - /// - /// @arg result - A reference whose target is filled in with the value of - /// the operand (the address if it is a memory operand) - /// @arg callback - A function to call to obtain register values - /// @arg arg - An opaque argument to pass to callback - int evaluate(uint64_t &result, - EDRegisterReaderCallback callback, - void *arg); - - /// isRegister - Returns 1 if the operand is a register or 0 otherwise - int isRegister(); - /// regVal - Returns the register value. - unsigned regVal(); - - /// isImmediate - Returns 1 if the operand is an immediate or 0 otherwise - int isImmediate(); - /// immediateVal - Returns the immediate value. - uint64_t immediateVal(); - - /// isMemory - Returns 1 if the operand is a memory location or 0 otherwise - int isMemory(); - -#ifdef __BLOCKS__ - typedef int (^EDRegisterBlock_t)(uint64_t *value, unsigned regID); - - /// evaluate - Like evaluate for a callback, but uses a block instead - int evaluate(uint64_t &result, - EDRegisterBlock_t regBlock); -#endif -}; - -} // end namespace llvm - -#endif diff --git a/lib/MC/MCDisassembler/EDToken.cpp b/lib/MC/MCDisassembler/EDToken.cpp deleted file mode 100644 index 5f6c9df..0000000 --- a/lib/MC/MCDisassembler/EDToken.cpp +++ /dev/null @@ -1,214 +0,0 @@ -//===-- EDToken.cpp - LLVM Enhanced Disassembler --------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file implements the Enhanced Disassembler library's token class. The -// token is responsible for vending information about the token, such as its -// type and logical value. -// -//===----------------------------------------------------------------------===// - -#include "EDToken.h" -#include "EDDisassembler.h" -#include "llvm/MC/MCParser/MCAsmLexer.h" -#include "llvm/MC/MCParser/MCParsedAsmOperand.h" -#include "llvm/ADT/SmallVector.h" -using namespace llvm; - -EDToken::EDToken(StringRef str, - enum tokenType type, - uint64_t localType, - EDDisassembler &disassembler) : - Disassembler(disassembler), - Str(str), - Type(type), - LocalType(localType), - OperandID(-1) { -} - -EDToken::~EDToken() { -} - -void EDToken::makeLiteral(bool sign, uint64_t absoluteValue) { - Type = kTokenLiteral; - LiteralSign = sign; - LiteralAbsoluteValue = absoluteValue; -} - -void EDToken::makeRegister(unsigned registerID) { - Type = kTokenRegister; - RegisterID = registerID; -} - -void EDToken::setOperandID(int operandID) { - OperandID = operandID; -} - -enum EDToken::tokenType EDToken::type() const { - return Type; -} - -uint64_t EDToken::localType() const { - return LocalType; -} - -StringRef EDToken::string() const { - return Str; -} - -int EDToken::operandID() const { - return OperandID; -} - -int EDToken::literalSign() const { - if (Type != kTokenLiteral) - return -1; - return (LiteralSign ? 1 : 0); -} - -int EDToken::literalAbsoluteValue(uint64_t &value) const { - if (Type != kTokenLiteral) - return -1; - value = LiteralAbsoluteValue; - return 0; -} - -int EDToken::registerID(unsigned ®isterID) const { - if (Type != kTokenRegister) - return -1; - registerID = RegisterID; - return 0; -} - -int EDToken::tokenize(std::vector<EDToken*> &tokens, - std::string &str, - const signed char *operandOrder, - EDDisassembler &disassembler) { - SmallVector<MCParsedAsmOperand*, 5> parsedOperands; - SmallVector<AsmToken, 10> asmTokens; - - if (disassembler.parseInst(parsedOperands, asmTokens, str)) - { - for (unsigned i = 0, e = parsedOperands.size(); i != e; ++i) - delete parsedOperands[i]; - return -1; - } - - SmallVectorImpl<MCParsedAsmOperand*>::iterator operandIterator; - unsigned int operandIndex; - SmallVectorImpl<AsmToken>::iterator tokenIterator; - - operandIterator = parsedOperands.begin(); - operandIndex = 0; - - bool readOpcode = false; - - const char *wsPointer = asmTokens.begin()->getLoc().getPointer(); - - for (tokenIterator = asmTokens.begin(); - tokenIterator != asmTokens.end(); - ++tokenIterator) { - SMLoc tokenLoc = tokenIterator->getLoc(); - - const char *tokenPointer = tokenLoc.getPointer(); - - if (tokenPointer > wsPointer) { - unsigned long wsLength = tokenPointer - wsPointer; - - EDToken *whitespaceToken = new EDToken(StringRef(wsPointer, wsLength), - EDToken::kTokenWhitespace, - 0, - disassembler); - - tokens.push_back(whitespaceToken); - } - - wsPointer = tokenPointer + tokenIterator->getString().size(); - - while (operandIterator != parsedOperands.end() && - tokenLoc.getPointer() > - (*operandIterator)->getEndLoc().getPointer()) { - ++operandIterator; - ++operandIndex; - } - - EDToken *token; - - switch (tokenIterator->getKind()) { - case AsmToken::Identifier: - if (!readOpcode) { - token = new EDToken(tokenIterator->getString(), - EDToken::kTokenOpcode, - (uint64_t)tokenIterator->getKind(), - disassembler); - readOpcode = true; - break; - } - // any identifier that isn't an opcode is mere punctuation; so we fall - // through - default: - token = new EDToken(tokenIterator->getString(), - EDToken::kTokenPunctuation, - (uint64_t)tokenIterator->getKind(), - disassembler); - break; - case AsmToken::Integer: - { - token = new EDToken(tokenIterator->getString(), - EDToken::kTokenLiteral, - (uint64_t)tokenIterator->getKind(), - disassembler); - - int64_t intVal = tokenIterator->getIntVal(); - - if (intVal < 0) - token->makeLiteral(true, -intVal); - else - token->makeLiteral(false, intVal); - break; - } - case AsmToken::Register: - { - token = new EDToken(tokenIterator->getString(), - EDToken::kTokenLiteral, - (uint64_t)tokenIterator->getKind(), - disassembler); - - token->makeRegister((unsigned)tokenIterator->getRegVal()); - break; - } - } - - if (operandIterator != parsedOperands.end() && - tokenLoc.getPointer() >= - (*operandIterator)->getStartLoc().getPointer()) { - /// operandIndex == 0 means the operand is the instruction (which the - /// AsmParser treats as an operand but edis does not). We therefore skip - /// operandIndex == 0 and subtract 1 from all other operand indices. - - if (operandIndex > 0) - token->setOperandID(operandOrder[operandIndex - 1]); - } - - tokens.push_back(token); - } - - // Free any parsed operands. - for (unsigned i = 0, e = parsedOperands.size(); i != e; ++i) - delete parsedOperands[i]; - - return 0; -} - -int EDToken::getString(const char*& buf) { - if (PermStr.length() == 0) { - PermStr = Str.str(); - } - buf = PermStr.c_str(); - return 0; -} diff --git a/lib/MC/MCDisassembler/EDToken.h b/lib/MC/MCDisassembler/EDToken.h deleted file mode 100644 index 384079b..0000000 --- a/lib/MC/MCDisassembler/EDToken.h +++ /dev/null @@ -1,139 +0,0 @@ -//===-EDToken.h - LLVM Enhanced Disassembler --------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file defines the interface for the Enhanced Disassembly library's token -// class. The token is responsible for vending information about the token, -// such as its type and logical value. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_EDTOKEN_H -#define LLVM_EDTOKEN_H - -#include "llvm/ADT/StringRef.h" -#include "llvm/Support/DataTypes.h" -#include <string> -#include <vector> - -namespace llvm { - -struct EDDisassembler; - -/// EDToken - Encapsulates a single token, which can provide a string -/// representation of itself or interpret itself in various ways, depending -/// on the token type. -struct EDToken { - enum tokenType { - kTokenWhitespace, - kTokenOpcode, - kTokenLiteral, - kTokenRegister, - kTokenPunctuation - }; - - /// The parent disassembler - EDDisassembler &Disassembler; - - /// The token's string representation - llvm::StringRef Str; - /// The token's string representation, but in a form suitable for export - std::string PermStr; - /// The type of the token, as exposed through the external API - enum tokenType Type; - /// The type of the token, as recorded by the syntax-specific tokenizer - uint64_t LocalType; - /// The operand corresponding to the token, or (unsigned int)-1 if not - /// part of an operand. - int OperandID; - - /// The sign if the token is a literal (1 if negative, 0 otherwise) - bool LiteralSign; - /// The absolute value if the token is a literal - uint64_t LiteralAbsoluteValue; - /// The LLVM register ID if the token is a register name - unsigned RegisterID; - - /// Constructor - Initializes an EDToken with the information common to all - /// tokens - /// - /// @arg str - The string corresponding to the token - /// @arg type - The token's type as exposed through the public API - /// @arg localType - The token's type as recorded by the tokenizer - /// @arg disassembler - The disassembler responsible for the token - EDToken(llvm::StringRef str, - enum tokenType type, - uint64_t localType, - EDDisassembler &disassembler); - - /// makeLiteral - Adds the information specific to a literal - /// @arg sign - The sign of the literal (1 if negative, 0 - /// otherwise) - /// - /// @arg absoluteValue - The absolute value of the literal - void makeLiteral(bool sign, uint64_t absoluteValue); - /// makeRegister - Adds the information specific to a register - /// - /// @arg registerID - The LLVM register ID - void makeRegister(unsigned registerID); - - /// setOperandID - Links the token to a numbered operand - /// - /// @arg operandID - The operand ID to link to - void setOperandID(int operandID); - - ~EDToken(); - - /// type - Returns the public type of the token - enum tokenType type() const; - /// localType - Returns the tokenizer-specific type of the token - uint64_t localType() const; - /// string - Returns the string representation of the token - llvm::StringRef string() const; - /// operandID - Returns the operand ID of the token - int operandID() const; - - /// literalSign - Returns the sign of the token - /// (1 if negative, 0 if positive or unsigned, -1 if it is not a literal) - int literalSign() const; - /// literalAbsoluteValue - Retrieves the absolute value of the token, and - /// returns -1 if the token is not a literal - /// @arg value - A reference to a value that is filled in with the absolute - /// value, if it is valid - int literalAbsoluteValue(uint64_t &value) const; - /// registerID - Retrieves the register ID of the token, and returns -1 if the - /// token is not a register - /// - /// @arg registerID - A reference to a value that is filled in with the - /// register ID, if it is valid - int registerID(unsigned ®isterID) const; - - /// tokenize - Tokenizes a string using the platform- and syntax-specific - /// tokenizer, and returns 0 on success (-1 on failure) - /// - /// @arg tokens - A vector that will be filled in with pointers to - /// allocated tokens - /// @arg str - The string, as outputted by the AsmPrinter - /// @arg operandOrder - The order of the operands from the operandFlags array - /// as they appear in str - /// @arg disassembler - The disassembler for the desired target and - // assembly syntax - static int tokenize(std::vector<EDToken*> &tokens, - std::string &str, - const signed char *operandOrder, - EDDisassembler &disassembler); - - /// getString - Directs a character pointer to the string, returning 0 on - /// success (-1 on failure) - /// @arg buf - A reference to a pointer that is set to point to the string. - /// The string is still owned by the token. - int getString(const char*& buf); -}; - -} // end namespace llvm -#endif diff --git a/lib/MC/MCDwarf.cpp b/lib/MC/MCDwarf.cpp index 96938f7..d53d2fc 100644 --- a/lib/MC/MCDwarf.cpp +++ b/lib/MC/MCDwarf.cpp @@ -8,24 +8,24 @@ //===----------------------------------------------------------------------===// #include "llvm/MC/MCDwarf.h" +#include "llvm/ADT/Hashing.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/ADT/Twine.h" +#include "llvm/Config/config.h" #include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCContext.h" +#include "llvm/MC/MCExpr.h" #include "llvm/MC/MCObjectFileInfo.h" #include "llvm/MC/MCObjectWriter.h" #include "llvm/MC/MCRegisterInfo.h" #include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCSymbol.h" -#include "llvm/MC/MCExpr.h" #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/raw_ostream.h" #include "llvm/Support/LEB128.h" #include "llvm/Support/Path.h" #include "llvm/Support/SourceMgr.h" -#include "llvm/ADT/Hashing.h" -#include "llvm/ADT/SmallString.h" -#include "llvm/ADT/Twine.h" -#include "llvm/Config/config.h" +#include "llvm/Support/raw_ostream.h" using namespace llvm; // Given a special op, return the address skip amount (in units of @@ -425,7 +425,7 @@ void MCDwarfFile::print(raw_ostream &OS) const { OS << '"' << getName() << '"'; } -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) void MCDwarfFile::dump() const { print(dbgs()); } @@ -484,7 +484,8 @@ static void EmitGenDwarfAbbrev(MCStreamer *MCOS) { // .debug_aranges section. Which contains a header and a table of pairs of // PointerSize'ed values for the address and size of section(s) with line table // entries (just the default .text in our case) and a terminating pair of zeros. -static void EmitGenDwarfAranges(MCStreamer *MCOS) { +static void EmitGenDwarfAranges(MCStreamer *MCOS, + const MCSymbol *InfoSectionSymbol) { MCContext &context = MCOS->getContext(); // Create a symbol at the end of the section that we are creating the dwarf @@ -523,8 +524,11 @@ static void EmitGenDwarfAranges(MCStreamer *MCOS) { // The 2 byte version, which is 2. MCOS->EmitIntValue(2, 2); // The 4 byte offset to the compile unit in the .debug_info from the start - // of the .debug_info, it is at the start of that section so this is zero. - MCOS->EmitIntValue(0, 4); + // of the .debug_info. + if (InfoSectionSymbol) + MCOS->EmitSymbolValue(InfoSectionSymbol, 4); + else + MCOS->EmitIntValue(0, 4); // The 1 byte size of an address. MCOS->EmitIntValue(AddrSize, 1); // The 1 byte size of a segment descriptor, we use a value of zero. @@ -623,8 +627,7 @@ static void EmitGenDwarfInfo(MCStreamer *MCOS, MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string. // AT_comp_dir, the working directory the assembly was done in. - llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory(); - MCOS->EmitBytes(StringRef(CWD.c_str()), 0); + MCOS->EmitBytes(context.getCompilationDir(), 0); MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string. // AT_APPLE_flags, the command line arguments of the assembler tool. @@ -705,15 +708,21 @@ void MCGenDwarfInfo::Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol) { // Create the dwarf sections in this order (.debug_line already created). MCContext &context = MCOS->getContext(); const MCAsmInfo &AsmInfo = context.getAsmInfo(); + bool CreateDwarfSectionSymbols = + AsmInfo.doesDwarfUseRelocationsAcrossSections(); + if (!CreateDwarfSectionSymbols) + LineSectionSymbol = NULL; + MCSymbol *AbbrevSectionSymbol = NULL; + MCSymbol *InfoSectionSymbol = NULL; MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection()); + if (CreateDwarfSectionSymbols) { + InfoSectionSymbol = context.CreateTempSymbol(); + MCOS->EmitLabel(InfoSectionSymbol); + } MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection()); - MCSymbol *AbbrevSectionSymbol; - if (AsmInfo.doesDwarfUseRelocationsAcrossSections()) { + if (CreateDwarfSectionSymbols) { AbbrevSectionSymbol = context.CreateTempSymbol(); MCOS->EmitLabel(AbbrevSectionSymbol); - } else { - AbbrevSectionSymbol = NULL; - LineSectionSymbol = NULL; } MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection()); @@ -722,7 +731,7 @@ void MCGenDwarfInfo::Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol) { return; // Output the data for .debug_aranges section. - EmitGenDwarfAranges(MCOS); + EmitGenDwarfAranges(MCOS, InfoSectionSymbol); // Output the data for .debug_abbrev section. EmitGenDwarfAbbrev(MCOS); @@ -928,46 +937,86 @@ void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer, bool VerboseAsm = Streamer.isVerboseAsm(); switch (Instr.getOperation()) { - case MCCFIInstruction::Move: - case MCCFIInstruction::RelMove: { - const MachineLocation &Dst = Instr.getDestination(); - const MachineLocation &Src = Instr.getSource(); - const bool IsRelative = Instr.getOperation() == MCCFIInstruction::RelMove; - - // If advancing cfa. - if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) { - if (Src.getReg() == MachineLocation::VirtualFP) { - if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_offset"); - Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1); - } else { - if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa"); - Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1); - if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + - Twine(Src.getReg())); - Streamer.EmitULEB128IntValue(Src.getReg()); - } + case MCCFIInstruction::OpRegister: { + unsigned Reg1 = Instr.getRegister(); + unsigned Reg2 = Instr.getRegister2(); + if (VerboseAsm) { + Streamer.AddComment("DW_CFA_register"); + Streamer.AddComment(Twine("Reg1 ") + Twine(Reg1)); + Streamer.AddComment(Twine("Reg2 ") + Twine(Reg2)); + } + Streamer.EmitIntValue(dwarf::DW_CFA_register, 1); + Streamer.EmitULEB128IntValue(Reg1); + Streamer.EmitULEB128IntValue(Reg2); + return; + } + case MCCFIInstruction::OpUndefined: { + unsigned Reg = Instr.getRegister(); + if (VerboseAsm) { + Streamer.AddComment("DW_CFA_undefined"); + Streamer.AddComment(Twine("Reg ") + Twine(Reg)); + } + Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1); + Streamer.EmitULEB128IntValue(Reg); + return; + } + case MCCFIInstruction::OpAdjustCfaOffset: + case MCCFIInstruction::OpDefCfaOffset: { + const bool IsRelative = + Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset; - if (IsRelative) - CFAOffset += Src.getOffset(); - else - CFAOffset = -Src.getOffset(); + if (VerboseAsm) + Streamer.AddComment("DW_CFA_def_cfa_offset"); + Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1); - if (VerboseAsm) Streamer.AddComment(Twine("Offset " + Twine(CFAOffset))); - Streamer.EmitULEB128IntValue(CFAOffset); - return; - } + if (IsRelative) + CFAOffset += Instr.getOffset(); + else + CFAOffset = -Instr.getOffset(); - if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) { - assert(Dst.isReg() && "Machine move not supported yet."); - if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_register"); - Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1); - if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Dst.getReg())); - Streamer.EmitULEB128IntValue(Dst.getReg()); - return; - } + if (VerboseAsm) + Streamer.AddComment(Twine("Offset " + Twine(CFAOffset))); + Streamer.EmitULEB128IntValue(CFAOffset); + + return; + } + case MCCFIInstruction::OpDefCfa: { + if (VerboseAsm) + Streamer.AddComment("DW_CFA_def_cfa"); + Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1); + + if (VerboseAsm) + Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister())); + Streamer.EmitULEB128IntValue(Instr.getRegister()); + + CFAOffset = -Instr.getOffset(); - unsigned Reg = Src.getReg(); - int Offset = Dst.getOffset(); + if (VerboseAsm) + Streamer.AddComment(Twine("Offset " + Twine(CFAOffset))); + Streamer.EmitULEB128IntValue(CFAOffset); + + return; + } + + case MCCFIInstruction::OpDefCfaRegister: { + if (VerboseAsm) + Streamer.AddComment("DW_CFA_def_cfa_register"); + Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1); + + if (VerboseAsm) + Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister())); + Streamer.EmitULEB128IntValue(Instr.getRegister()); + + return; + } + + case MCCFIInstruction::OpOffset: + case MCCFIInstruction::OpRelOffset: { + const bool IsRelative = + Instr.getOperation() == MCCFIInstruction::OpRelOffset; + + unsigned Reg = Instr.getRegister(); + int Offset = Instr.getOffset(); if (IsRelative) Offset -= CFAOffset; Offset = Offset / dataAlignmentFactor; @@ -995,24 +1044,24 @@ void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer, } return; } - case MCCFIInstruction::RememberState: + case MCCFIInstruction::OpRememberState: if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state"); Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1); return; - case MCCFIInstruction::RestoreState: + case MCCFIInstruction::OpRestoreState: if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state"); Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1); return; - case MCCFIInstruction::SameValue: { - unsigned Reg = Instr.getDestination().getReg(); + case MCCFIInstruction::OpSameValue: { + unsigned Reg = Instr.getRegister(); if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value"); Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1); if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg)); Streamer.EmitULEB128IntValue(Reg); return; } - case MCCFIInstruction::Restore: { - unsigned Reg = Instr.getDestination().getReg(); + case MCCFIInstruction::OpRestore: { + unsigned Reg = Instr.getRegister(); if (VerboseAsm) { Streamer.AddComment("DW_CFA_restore"); Streamer.AddComment(Twine("Reg ") + Twine(Reg)); @@ -1020,7 +1069,7 @@ void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer, Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1); return; } - case MCCFIInstruction::Escape: + case MCCFIInstruction::OpEscape: if (VerboseAsm) Streamer.AddComment("Escape bytes"); Streamer.EmitBytes(Instr.getValues(), 0); return; @@ -1244,8 +1293,21 @@ const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer, TranslateMachineLocation(MRI, Moves[i].getDestination()); const MachineLocation &Src = TranslateMachineLocation(MRI, Moves[i].getSource()); - MCCFIInstruction Inst(Label, Dst, Src); - Instructions.push_back(Inst); + + if (Dst.isReg()) { + assert(Dst.getReg() == MachineLocation::VirtualFP); + assert(!Src.isReg()); + MCCFIInstruction Inst = + MCCFIInstruction::createDefCfa(Label, Src.getReg(), -Src.getOffset()); + Instructions.push_back(Inst); + } else { + assert(Src.isReg()); + unsigned Reg = Src.getReg(); + int Offset = Dst.getOffset(); + MCCFIInstruction Inst = + MCCFIInstruction::createOffset(Label, Reg, Offset); + Instructions.push_back(Inst); + } } EmitCFIInstructions(streamer, Instructions, NULL); diff --git a/lib/MC/MCELF.cpp b/lib/MC/MCELF.cpp index f9f98e0..4db2846 100644 --- a/lib/MC/MCELF.cpp +++ b/lib/MC/MCELF.cpp @@ -11,7 +11,7 @@ // //===----------------------------------------------------------------------===// -#include "MCELF.h" +#include "llvm/MC/MCELF.h" #include "llvm/MC/MCAssembler.h" #include "llvm/MC/MCELFSymbolFlags.h" #include "llvm/MC/MCFixupKindInfo.h" diff --git a/lib/MC/MCELF.h b/lib/MC/MCELF.h deleted file mode 100644 index e08f1e6..0000000 --- a/lib/MC/MCELF.h +++ /dev/null @@ -1,35 +0,0 @@ -//===- lib/MC/MCELF.h - ELF MC --------------------------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file contains some support functions used by the ELF Streamer and -// ObjectWriter. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_MC_MCELF_H -#define LLVM_MC_MCELF_H - -#include "llvm/MC/MCExpr.h" - -namespace llvm { -class MCSymbolData; - -class MCELF { - public: - static void SetBinding(MCSymbolData &SD, unsigned Binding); - static unsigned GetBinding(const MCSymbolData &SD); - static void SetType(MCSymbolData &SD, unsigned Type); - static unsigned GetType(const MCSymbolData &SD); - static void SetVisibility(MCSymbolData &SD, unsigned Visibility); - static unsigned GetVisibility(MCSymbolData &SD); -}; - -} - -#endif diff --git a/lib/MC/MCELFObjectTargetWriter.cpp b/lib/MC/MCELFObjectTargetWriter.cpp index 6eb6914..74cd042 100644 --- a/lib/MC/MCELFObjectTargetWriter.cpp +++ b/lib/MC/MCELFObjectTargetWriter.cpp @@ -9,6 +9,8 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/MC/MCELFObjectWriter.h" +#include "llvm/MC/MCExpr.h" +#include "llvm/MC/MCValue.h" using namespace llvm; @@ -35,6 +37,12 @@ const MCSymbol *MCELFObjectTargetWriter::ExplicitRelSym(const MCAssembler &Asm, return NULL; } +const MCSymbol *MCELFObjectTargetWriter::undefinedExplicitRelSym(const MCValue &Target, + const MCFixup &Fixup, + bool IsPCRel) const { + const MCSymbol &Symbol = Target.getSymA()->getSymbol(); + return &Symbol.AliasedSymbol(); +} void MCELFObjectTargetWriter::adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset) { diff --git a/lib/MC/MCELFStreamer.cpp b/lib/MC/MCELFStreamer.cpp index 2d342dc..b08fa41 100644 --- a/lib/MC/MCELFStreamer.cpp +++ b/lib/MC/MCELFStreamer.cpp @@ -11,23 +11,20 @@ // //===----------------------------------------------------------------------===// -#include "MCELF.h" +#include "llvm/MC/MCELFStreamer.h" #include "llvm/ADT/SmallPtrSet.h" -#include "llvm/ADT/StringExtras.h" -#include "llvm/ADT/Twine.h" #include "llvm/MC/MCAssembler.h" #include "llvm/MC/MCCodeEmitter.h" #include "llvm/MC/MCContext.h" -#include "llvm/MC/MCSectionELF.h" -#include "llvm/MC/MCStreamer.h" +#include "llvm/MC/MCELF.h" #include "llvm/MC/MCELFSymbolFlags.h" #include "llvm/MC/MCExpr.h" #include "llvm/MC/MCInst.h" #include "llvm/MC/MCObjectStreamer.h" #include "llvm/MC/MCSection.h" +#include "llvm/MC/MCSectionELF.h" #include "llvm/MC/MCSymbol.h" #include "llvm/MC/MCValue.h" -#include "llvm/MC/MCAsmBackend.h" #include "llvm/Support/Debug.h" #include "llvm/Support/ELF.h" #include "llvm/Support/ErrorHandling.h" @@ -35,121 +32,37 @@ using namespace llvm; -namespace { -class MCELFStreamer : public MCObjectStreamer { -public: - MCELFStreamer(MCContext &Context, MCAsmBackend &TAB, - raw_ostream &OS, MCCodeEmitter *Emitter) - : MCObjectStreamer(Context, TAB, OS, Emitter) {} - - MCELFStreamer(MCContext &Context, MCAsmBackend &TAB, - raw_ostream &OS, MCCodeEmitter *Emitter, - MCAssembler *Assembler) - : MCObjectStreamer(Context, TAB, OS, Emitter, Assembler) {} - - - ~MCELFStreamer() {} - - /// @name MCStreamer Interface - /// @{ - - virtual void InitSections(); - virtual void ChangeSection(const MCSection *Section); - virtual void EmitLabel(MCSymbol *Symbol); - virtual void EmitAssemblerFlag(MCAssemblerFlag Flag); - virtual void EmitThumbFunc(MCSymbol *Func); - virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); - virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol); - virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute); - virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { - llvm_unreachable("ELF doesn't support this directive"); - } - virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, - unsigned ByteAlignment); - virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) { - llvm_unreachable("ELF doesn't support this directive"); - } - - virtual void EmitCOFFSymbolStorageClass(int StorageClass) { - llvm_unreachable("ELF doesn't support this directive"); - } - - virtual void EmitCOFFSymbolType(int Type) { - llvm_unreachable("ELF doesn't support this directive"); - } - virtual void EndCOFFSymbolDef() { - llvm_unreachable("ELF doesn't support this directive"); - } +inline void MCELFStreamer::SetSection(StringRef Section, unsigned Type, + unsigned Flags, SectionKind Kind) { + SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind)); +} - virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { - MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); - SD.setSize(Value); - } +inline void MCELFStreamer::SetSectionData() { + SetSection(".data", + ELF::SHT_PROGBITS, + ELF::SHF_WRITE | ELF::SHF_ALLOC, + SectionKind::getDataRel()); + EmitCodeAlignment(4, 0); +} - virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, - unsigned ByteAlignment); +inline void MCELFStreamer::SetSectionText() { + SetSection(".text", + ELF::SHT_PROGBITS, + ELF::SHF_EXECINSTR | ELF::SHF_ALLOC, + SectionKind::getText()); + EmitCodeAlignment(4, 0); +} - virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, - uint64_t Size = 0, unsigned ByteAlignment = 0) { - llvm_unreachable("ELF doesn't support this directive"); - } - virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, - uint64_t Size, unsigned ByteAlignment = 0) { - llvm_unreachable("ELF doesn't support this directive"); - } - virtual void EmitBytes(StringRef Data, unsigned AddrSpace); - virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, - unsigned ValueSize = 1, - unsigned MaxBytesToEmit = 0); - virtual void EmitCodeAlignment(unsigned ByteAlignment, - unsigned MaxBytesToEmit = 0); - virtual void EmitValueImpl(const MCExpr *Value, unsigned Size, - unsigned AddrSpace); - - virtual void EmitFileDirective(StringRef Filename); - - virtual void FinishImpl(); - -private: - virtual void EmitInstToFragment(const MCInst &Inst); - virtual void EmitInstToData(const MCInst &Inst); - - void fixSymbolsInTLSFixups(const MCExpr *expr); - - struct LocalCommon { - MCSymbolData *SD; - uint64_t Size; - unsigned ByteAlignment; - }; - std::vector<LocalCommon> LocalCommons; - - SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet; - /// @} - void SetSection(StringRef Section, unsigned Type, unsigned Flags, - SectionKind Kind) { - SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind)); - } +inline void MCELFStreamer::SetSectionBss() { + SetSection(".bss", + ELF::SHT_NOBITS, + ELF::SHF_WRITE | ELF::SHF_ALLOC, + SectionKind::getBSS()); + EmitCodeAlignment(4, 0); +} - void SetSectionData() { - SetSection(".data", ELF::SHT_PROGBITS, - ELF::SHF_WRITE |ELF::SHF_ALLOC, - SectionKind::getDataRel()); - EmitCodeAlignment(4, 0); - } - void SetSectionText() { - SetSection(".text", ELF::SHT_PROGBITS, - ELF::SHF_EXECINSTR | - ELF::SHF_ALLOC, SectionKind::getText()); - EmitCodeAlignment(4, 0); - } - void SetSectionBss() { - SetSection(".bss", ELF::SHT_NOBITS, - ELF::SHF_WRITE | - ELF::SHF_ALLOC, SectionKind::getBSS()); - EmitCodeAlignment(4, 0); - } -}; +MCELFStreamer::~MCELFStreamer() { } void MCELFStreamer::InitSections() { @@ -173,6 +86,10 @@ void MCELFStreamer::EmitLabel(MCSymbol *Symbol) { MCELF::SetType(SD, ELF::STT_TLS); } +void MCELFStreamer::EmitDebugLabel(MCSymbol *Symbol) { + EmitLabel(Symbol); +} + void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { switch (Flag) { case MCAF_SyntaxUnified: return; // no-op here. @@ -187,24 +104,10 @@ void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { llvm_unreachable("invalid assembler flag!"); } -void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) { - // FIXME: Anything needed here to flag the function as thumb? - - getAssembler().setIsThumbFunc(Func); - - MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func); - SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc); -} - -void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { - // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into - // MCObjectStreamer. - // FIXME: Lift context changes into super class. - getAssembler().getOrCreateSymbolData(*Symbol); - Symbol->setVariableValue(AddValueSymbols(Value)); -} - void MCELFStreamer::ChangeSection(const MCSection *Section) { + MCSectionData *CurSection = getCurrentSectionData(); + if (CurSection && CurSection->isBundleLocked()) + report_fatal_error("Unterminated .bundle_lock when changing a section"); const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup(); if (Grp) getAssembler().getOrCreateSymbolData(*Grp); @@ -247,7 +150,6 @@ void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol, switch (Attribute) { case MCSA_LazyReference: case MCSA_Reference: - case MCSA_NoDeadStrip: case MCSA_SymbolResolver: case MCSA_PrivateExtern: case MCSA_WeakDefinition: @@ -256,6 +158,7 @@ void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol, case MCSA_IndirectSymbol: llvm_unreachable("Invalid symbol attribute for ELF!"); + case MCSA_NoDeadStrip: case MCSA_ELF_TypeGnuUniqueObject: // Ignore for now. break; @@ -345,6 +248,11 @@ void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, SD.setSize(MCConstantExpr::Create(Size, getContext())); } +void MCELFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { + MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); + SD.setSize(Value); +} + void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment) { // FIXME: Should this be caught and done earlier? @@ -355,48 +263,24 @@ void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, EmitCommonSymbol(Symbol, Size, ByteAlignment); } -void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { - // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into - // MCObjectStreamer. - getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end()); -} - -void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment, - int64_t Value, unsigned ValueSize, - unsigned MaxBytesToEmit) { - // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into - // MCObjectStreamer. - if (MaxBytesToEmit == 0) - MaxBytesToEmit = ByteAlignment; - new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, - getCurrentSectionData()); - - // Update the maximum alignment on the current section if necessary. - if (ByteAlignment > getCurrentSectionData()->getAlignment()) - getCurrentSectionData()->setAlignment(ByteAlignment); -} - -void MCELFStreamer::EmitCodeAlignment(unsigned ByteAlignment, - unsigned MaxBytesToEmit) { - // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into - // MCObjectStreamer. - if (MaxBytesToEmit == 0) - MaxBytesToEmit = ByteAlignment; - MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit, - getCurrentSectionData()); - F->setEmitNops(true); - - // Update the maximum alignment on the current section if necessary. - if (ByteAlignment > getCurrentSectionData()->getAlignment()) - getCurrentSectionData()->setAlignment(ByteAlignment); -} - void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size, unsigned AddrSpace) { + if (getCurrentSectionData()->isBundleLocked()) + report_fatal_error("Emitting values inside a locked bundle is forbidden"); fixSymbolsInTLSFixups(Value); MCObjectStreamer::EmitValueImpl(Value, Size, AddrSpace); } +void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment, + int64_t Value, + unsigned ValueSize, + unsigned MaxBytesToEmit) { + if (getCurrentSectionData()->isBundleLocked()) + report_fatal_error("Emitting values inside a locked bundle is forbidden"); + MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value, + ValueSize, MaxBytesToEmit); +} + // Add a symbol for the file name of this module. This is the second // entry in the module's symbol table (the first being the null symbol). @@ -459,32 +343,105 @@ void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) { void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) { this->MCObjectStreamer::EmitInstToFragment(Inst); - MCInstFragment &F = *cast<MCInstFragment>(getCurrentFragment()); + MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment()); for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i) fixSymbolsInTLSFixups(F.getFixups()[i].getValue()); } void MCELFStreamer::EmitInstToData(const MCInst &Inst) { - MCDataFragment *DF = getOrCreateDataFragment(); - + MCAssembler &Assembler = getAssembler(); SmallVector<MCFixup, 4> Fixups; SmallString<256> Code; raw_svector_ostream VecOS(Code); - getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); + Assembler.getEmitter().EncodeInstruction(Inst, VecOS, Fixups); VecOS.flush(); for (unsigned i = 0, e = Fixups.size(); i != e; ++i) fixSymbolsInTLSFixups(Fixups[i].getValue()); + // There are several possibilities here: + // + // If bundling is disabled, append the encoded instruction to the current data + // fragment (or create a new such fragment if the current fragment is not a + // data fragment). + // + // If bundling is enabled: + // - If we're not in a bundle-locked group, emit the instruction into a data + // fragment of its own. + // - If we're in a bundle-locked group, append the instruction to the current + // data fragment because we want all the instructions in a group to get into + // the same fragment. Be careful not to do that for the first instruction in + // the group, though. + MCDataFragment *DF; + + if (Assembler.isBundlingEnabled()) { + MCSectionData *SD = getCurrentSectionData(); + if (SD->isBundleLocked() && !SD->isBundleGroupBeforeFirstInst()) + DF = getOrCreateDataFragment(); + else { + DF = new MCDataFragment(SD); + if (SD->getBundleLockState() == MCSectionData::BundleLockedAlignToEnd) { + // If this is a new fragment created for a bundle-locked group, and the + // group was marked as "align_to_end", set a flag in the fragment. + DF->setAlignToBundleEnd(true); + } + } + + // We're now emitting an instruction in a bundle group, so this flag has + // to be turned off. + SD->setBundleGroupBeforeFirstInst(false); + } else { + DF = getOrCreateDataFragment(); + } + // Add the fixups and data. for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); - DF->addFixup(Fixups[i]); + DF->getFixups().push_back(Fixups[i]); } + DF->setHasInstructions(true); DF->getContents().append(Code.begin(), Code.end()); } +void MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) { + assert(AlignPow2 <= 30 && "Invalid bundle alignment"); + MCAssembler &Assembler = getAssembler(); + if (Assembler.getBundleAlignSize() == 0 && AlignPow2 > 0) + Assembler.setBundleAlignSize(1 << AlignPow2); + else + report_fatal_error(".bundle_align_mode should be only set once per file"); +} + +void MCELFStreamer::EmitBundleLock(bool AlignToEnd) { + MCSectionData *SD = getCurrentSectionData(); + + // Sanity checks + // + if (!getAssembler().isBundlingEnabled()) + report_fatal_error(".bundle_lock forbidden when bundling is disabled"); + else if (SD->isBundleLocked()) + report_fatal_error("Nesting of .bundle_lock is forbidden"); + + SD->setBundleLockState(AlignToEnd ? MCSectionData::BundleLockedAlignToEnd : + MCSectionData::BundleLocked); + SD->setBundleGroupBeforeFirstInst(true); +} + +void MCELFStreamer::EmitBundleUnlock() { + MCSectionData *SD = getCurrentSectionData(); + + // Sanity checks + if (!getAssembler().isBundlingEnabled()) + report_fatal_error(".bundle_unlock forbidden when bundling is disabled"); + else if (!SD->isBundleLocked()) + report_fatal_error(".bundle_unlock without matching lock"); + else if (SD->isBundleGroupBeforeFirstInst()) + report_fatal_error("Empty bundle-locked group is forbidden"); + + SD->setBundleLockState(MCSectionData::NotBundleLocked); +} + void MCELFStreamer::FinishImpl() { EmitFrames(true); @@ -510,6 +467,10 @@ void MCELFStreamer::FinishImpl() { this->MCObjectStreamer::FinishImpl(); } +void MCELFStreamer::EmitTCEntry(const MCSymbol &S) { + // Creates a R_PPC64_TOC relocation + MCObjectStreamer::EmitSymbolValue(&S, 8, 0); +} MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB, raw_ostream &OS, MCCodeEmitter *CE, @@ -521,3 +482,37 @@ MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB, S->getAssembler().setNoExecStack(true); return S; } + +void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) { + llvm_unreachable("Generic ELF doesn't support this directive"); +} + +void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { + llvm_unreachable("ELF doesn't support this directive"); +} + +void MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) { + llvm_unreachable("ELF doesn't support this directive"); +} + +void MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) { + llvm_unreachable("ELF doesn't support this directive"); +} + +void MCELFStreamer::EmitCOFFSymbolType(int Type) { + llvm_unreachable("ELF doesn't support this directive"); +} + +void MCELFStreamer::EndCOFFSymbolDef() { + llvm_unreachable("ELF doesn't support this directive"); +} + +void MCELFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, + uint64_t Size, unsigned ByteAlignment) { + llvm_unreachable("ELF doesn't support this directive"); +} + +void MCELFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, + uint64_t Size, unsigned ByteAlignment) { + llvm_unreachable("ELF doesn't support this directive"); +} diff --git a/lib/MC/MCExpr.cpp b/lib/MC/MCExpr.cpp index b196659..1a53934 100644 --- a/lib/MC/MCExpr.cpp +++ b/lib/MC/MCExpr.cpp @@ -54,13 +54,16 @@ void MCExpr::print(raw_ostream &OS) const { else OS << Sym; - if (SRE.getKind() == MCSymbolRefExpr::VK_ARM_PLT || + if (SRE.getKind() == MCSymbolRefExpr::VK_ARM_NONE || + SRE.getKind() == MCSymbolRefExpr::VK_ARM_PLT || SRE.getKind() == MCSymbolRefExpr::VK_ARM_TLSGD || SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOT || SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOTOFF || SRE.getKind() == MCSymbolRefExpr::VK_ARM_TPOFF || SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOTTPOFF || - SRE.getKind() == MCSymbolRefExpr::VK_ARM_TARGET1) + SRE.getKind() == MCSymbolRefExpr::VK_ARM_TARGET1 || + SRE.getKind() == MCSymbolRefExpr::VK_ARM_TARGET2 || + SRE.getKind() == MCSymbolRefExpr::VK_ARM_PREL31) OS << MCSymbolRefExpr::getVariantKindName(SRE.getKind()); else if (SRE.getKind() != MCSymbolRefExpr::VK_None && SRE.getKind() != MCSymbolRefExpr::VK_PPC_DARWIN_HA16 && @@ -136,7 +139,7 @@ void MCExpr::print(raw_ostream &OS) const { llvm_unreachable("Invalid expression kind!"); } -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) void MCExpr::dump() const { print(dbgs()); dbgs() << '\n'; @@ -192,6 +195,7 @@ StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) { case VK_DTPOFF: return "DTPOFF"; case VK_TLVP: return "TLVP"; case VK_SECREL: return "SECREL"; + case VK_ARM_NONE: return "(NONE)"; case VK_ARM_PLT: return "(PLT)"; case VK_ARM_GOT: return "(GOT)"; case VK_ARM_GOTOFF: return "(GOTOFF)"; @@ -199,6 +203,8 @@ StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) { case VK_ARM_GOTTPOFF: return "(gottpoff)"; case VK_ARM_TLSGD: return "(tlsgd)"; case VK_ARM_TARGET1: return "(target1)"; + case VK_ARM_TARGET2: return "(target2)"; + case VK_ARM_PREL31: return "(prel31)"; case VK_PPC_TOC: return "tocbase"; case VK_PPC_TOC_ENTRY: return "toc"; case VK_PPC_DARWIN_HA16: return "ha16"; @@ -207,6 +213,19 @@ StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) { case VK_PPC_GAS_LO16: return "l"; case VK_PPC_TPREL16_HA: return "tprel@ha"; case VK_PPC_TPREL16_LO: return "tprel@l"; + case VK_PPC_DTPREL16_HA: return "dtprel@ha"; + case VK_PPC_DTPREL16_LO: return "dtprel@l"; + case VK_PPC_TOC16_HA: return "toc@ha"; + case VK_PPC_TOC16_LO: return "toc@l"; + case VK_PPC_GOT_TPREL16_HA: return "got@tprel@ha"; + case VK_PPC_GOT_TPREL16_LO: return "got@tprel@l"; + case VK_PPC_TLS: return "tls"; + case VK_PPC_GOT_TLSGD16_HA: return "got@tlsgd@ha"; + case VK_PPC_GOT_TLSGD16_LO: return "got@tlsgd@l"; + case VK_PPC_GOT_TLSLD16_HA: return "got@tlsld@ha"; + case VK_PPC_GOT_TLSLD16_LO: return "got@tlsld@l"; + case VK_PPC_TLSGD: return "tlsgd"; + case VK_PPC_TLSLD: return "tlsld"; case VK_Mips_GPREL: return "GPREL"; case VK_Mips_GOT_CALL: return "GOT_CALL"; case VK_Mips_GOT16: return "GOT16"; @@ -227,6 +246,10 @@ StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) { case VK_Mips_GOT_OFST: return "GOT_OFST"; case VK_Mips_HIGHER: return "HIGHER"; case VK_Mips_HIGHEST: return "HIGHEST"; + case VK_Mips_GOT_HI16: return "GOT_HI16"; + case VK_Mips_GOT_LO16: return "GOT_LO16"; + case VK_Mips_CALL_HI16: return "CALL_HI16"; + case VK_Mips_CALL_LO16: return "CALL_LO16"; } llvm_unreachable("Invalid variant kind"); } @@ -267,7 +290,7 @@ MCSymbolRefExpr::getVariantKindForName(StringRef Name) { /* *** */ -void MCTargetExpr::Anchor() {} +void MCTargetExpr::anchor() {} /* *** */ diff --git a/lib/MC/MCInst.cpp b/lib/MC/MCInst.cpp index e96010b..124cc14 100644 --- a/lib/MC/MCInst.cpp +++ b/lib/MC/MCInst.cpp @@ -32,7 +32,7 @@ void MCOperand::print(raw_ostream &OS, const MCAsmInfo *MAI) const { OS << ">"; } -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) void MCOperand::dump() const { print(dbgs(), 0); dbgs() << "\n"; @@ -64,7 +64,7 @@ void MCInst::dump_pretty(raw_ostream &OS, const MCAsmInfo *MAI, OS << ">"; } -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) void MCInst::dump() const { print(dbgs(), 0); dbgs() << "\n"; diff --git a/lib/MC/MCInstPrinter.cpp b/lib/MC/MCInstPrinter.cpp index 847bcc0..73f30ff 100644 --- a/lib/MC/MCInstPrinter.cpp +++ b/lib/MC/MCInstPrinter.cpp @@ -8,10 +8,11 @@ //===----------------------------------------------------------------------===// #include "llvm/MC/MCInstPrinter.h" -#include "llvm/MC/MCInstrInfo.h" -#include "llvm/MC/MCAsmInfo.h" #include "llvm/ADT/StringRef.h" +#include "llvm/MC/MCAsmInfo.h" +#include "llvm/MC/MCInstrInfo.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/Format.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; @@ -36,3 +37,25 @@ void MCInstPrinter::printAnnotation(raw_ostream &OS, StringRef Annot) { OS << " " << MAI.getCommentString() << " " << Annot; } } + +/// Utility functions to make adding mark ups simpler. +StringRef MCInstPrinter::markup(StringRef s) const { + if (getUseMarkup()) + return s; + else + return ""; +} +StringRef MCInstPrinter::markup(StringRef a, StringRef b) const { + if (getUseMarkup()) + return a; + else + return b; +} + +/// Utility function to print immediates in decimal or hex. +format_object1<int64_t> MCInstPrinter::formatImm(const int64_t Value) const { + if (getPrintImmHex()) + return format("0x%" PRIx64, Value); + else + return format("%" PRId64, Value); +} diff --git a/lib/MC/MCLabel.cpp b/lib/MC/MCLabel.cpp index 95d7d16..1d3022a 100644 --- a/lib/MC/MCLabel.cpp +++ b/lib/MC/MCLabel.cpp @@ -16,7 +16,7 @@ void MCLabel::print(raw_ostream &OS) const { OS << '"' << getInstance() << '"'; } -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) void MCLabel::dump() const { print(dbgs()); } diff --git a/lib/MC/MCMachOStreamer.cpp b/lib/MC/MCMachOStreamer.cpp index 74f6dc6..82ccdd4 100644 --- a/lib/MC/MCMachOStreamer.cpp +++ b/lib/MC/MCMachOStreamer.cpp @@ -7,19 +7,18 @@ //===----------------------------------------------------------------------===// #include "llvm/MC/MCStreamer.h" - +#include "llvm/MC/MCAsmBackend.h" #include "llvm/MC/MCAssembler.h" -#include "llvm/MC/MCContext.h" #include "llvm/MC/MCCodeEmitter.h" +#include "llvm/MC/MCContext.h" +#include "llvm/MC/MCDwarf.h" #include "llvm/MC/MCExpr.h" #include "llvm/MC/MCInst.h" +#include "llvm/MC/MCMachOSymbolFlags.h" #include "llvm/MC/MCObjectStreamer.h" #include "llvm/MC/MCSection.h" -#include "llvm/MC/MCSymbol.h" -#include "llvm/MC/MCMachOSymbolFlags.h" #include "llvm/MC/MCSectionMachO.h" -#include "llvm/MC/MCDwarf.h" -#include "llvm/MC/MCAsmBackend.h" +#include "llvm/MC/MCSymbol.h" #include "llvm/Support/Dwarf.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/raw_ostream.h" @@ -44,12 +43,12 @@ public: virtual void InitSections(); virtual void EmitLabel(MCSymbol *Symbol); + virtual void EmitDebugLabel(MCSymbol *Symbol); virtual void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol); virtual void EmitAssemblerFlag(MCAssemblerFlag Flag); virtual void EmitDataRegion(MCDataRegionType Kind); virtual void EmitThumbFunc(MCSymbol *Func); - virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute); virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue); virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, @@ -75,12 +74,6 @@ public: uint64_t Size = 0, unsigned ByteAlignment = 0); virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment = 0); - virtual void EmitBytes(StringRef Data, unsigned AddrSpace); - virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, - unsigned ValueSize = 1, - unsigned MaxBytesToEmit = 0); - virtual void EmitCodeAlignment(unsigned ByteAlignment, - unsigned MaxBytesToEmit = 0); virtual void EmitFileDirective(StringRef Filename) { // FIXME: Just ignore the .file; it isn't important enough to fail the @@ -138,7 +131,12 @@ void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) { SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask); } +void MCMachOStreamer::EmitDebugLabel(MCSymbol *Symbol) { + EmitLabel(Symbol); +} void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) { + if (!getAssembler().getBackend().hasDataInCodeSupport()) + return; // Create a temporary label to mark the start of the data region. MCSymbol *Start = getContext().CreateTempSymbol(); EmitLabel(Start); @@ -149,6 +147,8 @@ void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) { } void MCMachOStreamer::EmitDataRegionEnd() { + if (!getAssembler().getBackend().hasDataInCodeSupport()) + return; std::vector<DataRegionData> &Regions = getAssembler().getDataRegions(); assert(Regions.size() && "Mismatched .end_data_region!"); DataRegionData &Data = Regions.back(); @@ -203,14 +203,6 @@ void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) { SD.setFlags(SD.getFlags() | SF_ThumbFunc); } -void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { - // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into - // MCObjectStreamer. - // FIXME: Lift context changes into super class. - getAssembler().getOrCreateSymbolData(*Symbol); - Symbol->setVariableValue(AddValueSymbols(Value)); -} - void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) { // Indirect symbols are handled differently, to match how 'as' handles @@ -368,42 +360,6 @@ void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, return; } -void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { - // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into - // MCObjectStreamer. - getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end()); -} - -void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment, - int64_t Value, unsigned ValueSize, - unsigned MaxBytesToEmit) { - // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into - // MCObjectStreamer. - if (MaxBytesToEmit == 0) - MaxBytesToEmit = ByteAlignment; - new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, - getCurrentSectionData()); - - // Update the maximum alignment on the current section if necessary. - if (ByteAlignment > getCurrentSectionData()->getAlignment()) - getCurrentSectionData()->setAlignment(ByteAlignment); -} - -void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment, - unsigned MaxBytesToEmit) { - // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into - // MCObjectStreamer. - if (MaxBytesToEmit == 0) - MaxBytesToEmit = ByteAlignment; - MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit, - getCurrentSectionData()); - F->setEmitNops(true); - - // Update the maximum alignment on the current section if necessary. - if (ByteAlignment > getCurrentSectionData()->getAlignment()) - getCurrentSectionData()->setAlignment(ByteAlignment); -} - void MCMachOStreamer::EmitInstToData(const MCInst &Inst) { MCDataFragment *DF = getOrCreateDataFragment(); @@ -416,7 +372,7 @@ void MCMachOStreamer::EmitInstToData(const MCInst &Inst) { // Add the fixups and data. for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); - DF->addFixup(Fixups[i]); + DF->getFixups().push_back(Fixups[i]); } DF->getContents().append(Code.begin(), Code.end()); } diff --git a/lib/MC/MCNullStreamer.cpp b/lib/MC/MCNullStreamer.cpp index 4c17d91..364c324 100644 --- a/lib/MC/MCNullStreamer.cpp +++ b/lib/MC/MCNullStreamer.cpp @@ -8,7 +8,6 @@ //===----------------------------------------------------------------------===// #include "llvm/MC/MCStreamer.h" - #include "llvm/MC/MCContext.h" #include "llvm/MC/MCInst.h" #include "llvm/MC/MCSectionMachO.h" @@ -36,7 +35,9 @@ namespace { assert(getCurrentSection() && "Cannot emit before setting section!"); Symbol->setSection(*getCurrentSection()); } - + virtual void EmitDebugLabel(MCSymbol *Symbol) { + EmitLabel(Symbol); + } virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {} virtual void EmitThumbFunc(MCSymbol *Func) {} @@ -94,6 +95,10 @@ namespace { StringRef FileName) {} virtual void EmitInstruction(const MCInst &Inst) {} + virtual void EmitBundleAlignMode(unsigned AlignPow2) {} + virtual void EmitBundleLock(bool AlignToEnd) {} + virtual void EmitBundleUnlock() {} + virtual void FinishImpl() {} virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) { diff --git a/lib/MC/MCObjectFileInfo.cpp b/lib/MC/MCObjectFileInfo.cpp index 29b4a94..a46f7be 100644 --- a/lib/MC/MCObjectFileInfo.cpp +++ b/lib/MC/MCObjectFileInfo.cpp @@ -8,12 +8,12 @@ //===----------------------------------------------------------------------===// #include "llvm/MC/MCObjectFileInfo.h" +#include "llvm/ADT/Triple.h" #include "llvm/MC/MCContext.h" #include "llvm/MC/MCSection.h" #include "llvm/MC/MCSectionCOFF.h" #include "llvm/MC/MCSectionELF.h" #include "llvm/MC/MCSectionMachO.h" -#include "llvm/ADT/Triple.h" using namespace llvm; void MCObjectFileInfo::InitMachOMCObjectFileInfo(Triple T) { @@ -256,6 +256,17 @@ void MCObjectFileInfo::InitELFMCObjectFileInfo(Triple T) { TTypeEncoding = (CMModel == CodeModel::Small) ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr; } + } else if (T.getArch() == Triple::ppc64) { + PersonalityEncoding = dwarf::DW_EH_PE_udata8; + PersonalityEncoding |= (RelocM == Reloc::PIC_) + ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel + : dwarf::DW_EH_PE_absptr; + unsigned PICFlag = (RelocM == Reloc::PIC_) ? dwarf::DW_EH_PE_pcrel + : dwarf::DW_EH_PE_absptr; + FDECFIEncoding = PICFlag | dwarf::DW_EH_PE_sdata4; + LSDAEncoding = PICFlag | dwarf::DW_EH_PE_udata8; + FDEEncoding = PICFlag | dwarf::DW_EH_PE_sdata4; + TTypeEncoding = PICFlag | dwarf::DW_EH_PE_sdata4; } // Solaris requires different flags for .eh_frame to seemingly every other @@ -392,6 +403,43 @@ void MCObjectFileInfo::InitELFMCObjectFileInfo(Triple T) { DwarfMacroInfoSection = Ctx->getELFSection(".debug_macinfo", ELF::SHT_PROGBITS, 0, SectionKind::getMetadata()); + + // DWARF5 Experimental Debug Info + + // Accelerator Tables + DwarfAccelNamesSection = + Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0, + SectionKind::getMetadata()); + DwarfAccelObjCSection = + Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0, + SectionKind::getMetadata()); + DwarfAccelNamespaceSection = + Ctx->getELFSection(".apple_namespaces", ELF::SHT_PROGBITS, 0, + SectionKind::getMetadata()); + DwarfAccelTypesSection = + Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0, + SectionKind::getMetadata()); + + // Fission Sections + DwarfInfoDWOSection = + Ctx->getELFSection(".debug_info.dwo", ELF::SHT_PROGBITS, 0, + SectionKind::getMetadata()); + DwarfAbbrevDWOSection = + Ctx->getELFSection(".debug_abbrev.dwo", ELF::SHT_PROGBITS, 0, + SectionKind::getMetadata()); + DwarfStrDWOSection = + Ctx->getELFSection(".debug_str.dwo", ELF::SHT_PROGBITS, + ELF::SHF_MERGE | ELF::SHF_STRINGS, + SectionKind::getMergeable1ByteCString()); + DwarfLineDWOSection = + Ctx->getELFSection(".debug_line.dwo", ELF::SHT_PROGBITS, 0, + SectionKind::getMetadata()); + DwarfLocDWOSection = + Ctx->getELFSection(".debug_loc.dwo", ELF::SHT_PROGBITS, 0, + SectionKind::getMetadata()); + DwarfStrOffDWOSection = + Ctx->getELFSection(".debug_str_offsets.dwo", ELF::SHT_PROGBITS, 0, + SectionKind::getMetadata()); } @@ -430,12 +478,20 @@ void MCObjectFileInfo::InitCOFFMCObjectFileInfo(Triple T) { } - StaticDtorSection = - Ctx->getCOFFSection(".dtors", - COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | - COFF::IMAGE_SCN_MEM_READ | - COFF::IMAGE_SCN_MEM_WRITE, - SectionKind::getDataRel()); + if (T.getOS() == Triple::Win32) { + StaticDtorSection = + Ctx->getCOFFSection(".CRT$XTX", + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | + COFF::IMAGE_SCN_MEM_READ, + SectionKind::getReadOnly()); + } else { + StaticDtorSection = + Ctx->getCOFFSection(".dtors", + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | + COFF::IMAGE_SCN_MEM_READ | + COFF::IMAGE_SCN_MEM_WRITE, + SectionKind::getDataRel()); + } // FIXME: We're emitting LSDA info into a readonly section on COFF, even // though it contains relocatable pointers. In PIC mode, this is probably a @@ -557,6 +613,7 @@ void MCObjectFileInfo::InitMCObjectFileInfo(StringRef TT, Reloc::Model relocm, Env = IsMachO; InitMachOMCObjectFileInfo(T); } else if ((Arch == Triple::x86 || Arch == Triple::x86_64) && + (T.getEnvironment() != Triple::ELF) && (T.getOS() == Triple::MinGW32 || T.getOS() == Triple::Cygwin || T.getOS() == Triple::Win32)) { Env = IsCOFF; diff --git a/lib/MC/MCObjectStreamer.cpp b/lib/MC/MCObjectStreamer.cpp index 21756cd..d205a8c 100644 --- a/lib/MC/MCObjectStreamer.cpp +++ b/lib/MC/MCObjectStreamer.cpp @@ -44,6 +44,13 @@ MCObjectStreamer::~MCObjectStreamer() { delete Assembler; } +void MCObjectStreamer::reset() { + if (Assembler) + Assembler->reset(); + CurSectionData = 0; + MCStreamer::reset(); +} + MCFragment *MCObjectStreamer::getCurrentFragment() const { assert(getCurrentSectionData() && "No current section!"); @@ -99,9 +106,9 @@ void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size, EmitIntValue(AbsValue, Size, AddrSpace); return; } - DF->addFixup(MCFixup::Create(DF->getContents().size(), - Value, - MCFixup::getKindForSize(Size, false))); + DF->getFixups().push_back( + MCFixup::Create(DF->getContents().size(), Value, + MCFixup::getKindForSize(Size, false))); DF->getContents().resize(DF->getContents().size() + Size, 0); } @@ -128,6 +135,10 @@ void MCObjectStreamer::EmitLabel(MCSymbol *Symbol) { SD.setOffset(F->getContents().size()); } +void MCObjectStreamer::EmitDebugLabel(MCSymbol *Symbol) { + EmitLabel(Symbol); +} + void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value) { int64_t IntValue; if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) { @@ -159,27 +170,38 @@ void MCObjectStreamer::ChangeSection(const MCSection *Section) { CurSectionData = &getAssembler().getOrCreateSectionData(*Section); } +void MCObjectStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { + getAssembler().getOrCreateSymbolData(*Symbol); + Symbol->setVariableValue(AddValueSymbols(Value)); +} + void MCObjectStreamer::EmitInstruction(const MCInst &Inst) { // Scan for values. for (unsigned i = Inst.getNumOperands(); i--; ) if (Inst.getOperand(i).isExpr()) AddValueSymbols(Inst.getOperand(i).getExpr()); - getCurrentSectionData()->setHasInstructions(true); + MCSectionData *SD = getCurrentSectionData(); + SD->setHasInstructions(true); // Now that a machine instruction has been assembled into this section, make // a line entry for any .loc directive that has been seen. MCLineEntry::Make(this, getCurrentSection()); // If this instruction doesn't need relaxation, just emit it as data. - if (!getAssembler().getBackend().mayNeedRelaxation(Inst)) { + MCAssembler &Assembler = getAssembler(); + if (!Assembler.getBackend().mayNeedRelaxation(Inst)) { EmitInstToData(Inst); return; } - // Otherwise, if we are relaxing everything, relax the instruction as much as - // possible and emit it as data. - if (getAssembler().getRelaxAll()) { + // Otherwise, relax and emit it as data if either: + // - The RelaxAll flag was passed + // - Bundling is enabled and this instruction is inside a bundle-locked + // group. We want to emit all such instructions into the same data + // fragment. + if (Assembler.getRelaxAll() || + (Assembler.isBundlingEnabled() && SD->isBundleLocked())) { MCInst Relaxed; getAssembler().getBackend().relaxInstruction(Inst, Relaxed); while (getAssembler().getBackend().mayNeedRelaxation(Relaxed)) @@ -193,13 +215,31 @@ void MCObjectStreamer::EmitInstruction(const MCInst &Inst) { } void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst) { - MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData()); + // Always create a new, separate fragment here, because its size can change + // during relaxation. + MCRelaxableFragment *IF = + new MCRelaxableFragment(Inst, getCurrentSectionData()); SmallString<128> Code; raw_svector_ostream VecOS(Code); getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, IF->getFixups()); VecOS.flush(); - IF->getCode().append(Code.begin(), Code.end()); + IF->getContents().append(Code.begin(), Code.end()); +} + +const char *BundlingNotImplementedMsg = + "Aligned bundling is not implemented for this object format"; + +void MCObjectStreamer::EmitBundleAlignMode(unsigned AlignPow2) { + llvm_unreachable(BundlingNotImplementedMsg); +} + +void MCObjectStreamer::EmitBundleLock(bool AlignToEnd) { + llvm_unreachable(BundlingNotImplementedMsg); +} + +void MCObjectStreamer::EmitBundleUnlock() { + llvm_unreachable(BundlingNotImplementedMsg); } void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta, @@ -232,6 +272,31 @@ void MCObjectStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel, new MCDwarfCallFrameFragment(*AddrDelta, getCurrentSectionData()); } +void MCObjectStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { + assert(AddrSpace == 0 && "Address space must be 0!"); + getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end()); +} + +void MCObjectStreamer::EmitValueToAlignment(unsigned ByteAlignment, + int64_t Value, + unsigned ValueSize, + unsigned MaxBytesToEmit) { + if (MaxBytesToEmit == 0) + MaxBytesToEmit = ByteAlignment; + new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, + getCurrentSectionData()); + + // Update the maximum alignment on the current section if necessary. + if (ByteAlignment > getCurrentSectionData()->getAlignment()) + getCurrentSectionData()->setAlignment(ByteAlignment); +} + +void MCObjectStreamer::EmitCodeAlignment(unsigned ByteAlignment, + unsigned MaxBytesToEmit) { + EmitValueToAlignment(ByteAlignment, 0, 1, MaxBytesToEmit); + cast<MCAlignFragment>(getCurrentFragment())->setEmitNops(true); +} + bool MCObjectStreamer::EmitValueToOffset(const MCExpr *Offset, unsigned char Value) { int64_t Res; @@ -258,7 +323,8 @@ bool MCObjectStreamer::EmitValueToOffset(const MCExpr *Offset, void MCObjectStreamer::EmitGPRel32Value(const MCExpr *Value) { MCDataFragment *DF = getOrCreateDataFragment(); - DF->addFixup(MCFixup::Create(DF->getContents().size(), Value, FK_GPRel_4)); + DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(), + Value, FK_GPRel_4)); DF->getContents().resize(DF->getContents().size() + 4, 0); } @@ -266,10 +332,19 @@ void MCObjectStreamer::EmitGPRel32Value(const MCExpr *Value) { void MCObjectStreamer::EmitGPRel64Value(const MCExpr *Value) { MCDataFragment *DF = getOrCreateDataFragment(); - DF->addFixup(MCFixup::Create(DF->getContents().size(), Value, FK_GPRel_4)); + DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(), + Value, FK_GPRel_4)); DF->getContents().resize(DF->getContents().size() + 8, 0); } +void MCObjectStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue, + unsigned AddrSpace) { + assert(AddrSpace == 0 && "Address space must be 0!"); + // FIXME: A MCFillFragment would be more memory efficient but MCExpr has + // problems evaluating expressions across multiple fragments. + getOrCreateDataFragment()->getContents().append(NumBytes, FillValue); +} + void MCObjectStreamer::FinishImpl() { // Dump out the dwarf file & directory tables and line tables. const MCSymbol *LineSectionSymbol = NULL; diff --git a/lib/MC/MCParser/AsmLexer.cpp b/lib/MC/MCParser/AsmLexer.cpp index c76052d..d0492fd 100644 --- a/lib/MC/MCParser/AsmLexer.cpp +++ b/lib/MC/MCParser/AsmLexer.cpp @@ -12,9 +12,9 @@ //===----------------------------------------------------------------------===// #include "llvm/MC/MCParser/AsmLexer.h" -#include "llvm/Support/SMLoc.h" -#include "llvm/Support/MemoryBuffer.h" #include "llvm/MC/MCAsmInfo.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/SMLoc.h" #include <cctype> #include <cerrno> #include <cstdio> @@ -396,8 +396,17 @@ AsmToken AsmLexer::LexToken() { case 0: case ' ': case '\t': - // Ignore whitespace. - return LexToken(); + if (SkipSpace) { + // Ignore whitespace. + return LexToken(); + } else { + int len = 1; + while (*CurPtr==' ' || *CurPtr=='\t') { + CurPtr++; + len++; + } + return AsmToken(AsmToken::Space, StringRef(TokStart, len)); + } case '\n': // FALL THROUGH. case '\r': isAtStartOfLine = true; diff --git a/lib/MC/MCParser/AsmParser.cpp b/lib/MC/MCParser/AsmParser.cpp index 55ef01c..7c3fea5 100644 --- a/lib/MC/MCParser/AsmParser.cpp +++ b/lib/MC/MCParser/AsmParser.cpp @@ -19,6 +19,8 @@ #include "llvm/MC/MCContext.h" #include "llvm/MC/MCDwarf.h" #include "llvm/MC/MCExpr.h" +#include "llvm/MC/MCInstPrinter.h" +#include "llvm/MC/MCInstrInfo.h" #include "llvm/MC/MCParser/AsmCond.h" #include "llvm/MC/MCParser/AsmLexer.h" #include "llvm/MC/MCParser/MCAsmParser.h" @@ -35,6 +37,8 @@ #include "llvm/Support/SourceMgr.h" #include "llvm/Support/raw_ostream.h" #include <cctype> +#include <set> +#include <string> #include <vector> using namespace llvm; @@ -42,12 +46,14 @@ static cl::opt<bool> FatalAssemblerWarnings("fatal-assembler-warnings", cl::desc("Consider warnings as error")); +MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {} + namespace { /// \brief Helper class for tracking macro definitions. typedef std::vector<AsmToken> MacroArgument; typedef std::vector<MacroArgument> MacroArguments; -typedef StringRef MacroParameter; +typedef std::pair<StringRef, MacroArgument> MacroParameter; typedef std::vector<MacroParameter> MacroParameters; struct Macro { @@ -72,20 +78,48 @@ struct MacroInstantiation { /// The location of the instantiation. SMLoc InstantiationLoc; + /// The buffer where parsing should resume upon instantiation completion. + int ExitBuffer; + /// The location where parsing should resume upon instantiation completion. SMLoc ExitLoc; public: - MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL, + MacroInstantiation(const Macro *M, SMLoc IL, int EB, SMLoc EL, MemoryBuffer *I); }; +//struct AsmRewrite; +struct ParseStatementInfo { + /// ParsedOperands - The parsed operands from the last parsed statement. + SmallVector<MCParsedAsmOperand*, 8> ParsedOperands; + + /// Opcode - The opcode from the last parsed instruction. + unsigned Opcode; + + /// Error - Was there an error parsing the inline assembly? + bool ParseError; + + SmallVectorImpl<AsmRewrite> *AsmRewrites; + + ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {} + ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites) + : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {} + + ~ParseStatementInfo() { + // Free any parsed operands. + for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i) + delete ParsedOperands[i]; + ParsedOperands.clear(); + } +}; + /// \brief The concrete assembly parser instance. class AsmParser : public MCAsmParser { friend class GenericAsmParser; - AsmParser(const AsmParser &); // DO NOT IMPLEMENT - void operator=(const AsmParser &); // DO NOT IMPLEMENT + AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION; + void operator=(const AsmParser &) LLVM_DELETED_FUNCTION; private: AsmLexer Lexer; MCContext &Ctx; @@ -126,10 +160,17 @@ private: StringRef CppHashFilename; int64_t CppHashLineNumber; SMLoc CppHashLoc; + int CppHashBuf; /// AssemblerDialect. ~OU means unset value and use value provided by MAI. unsigned AssemblerDialect; + /// IsDarwin - is Darwin compatibility enabled? + bool IsDarwin; + + /// ParsingInlineAsm - Are we parsing ms-style inline assembly? + bool ParsingInlineAsm; + public: AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, const MCAsmInfo &MAI); @@ -151,9 +192,9 @@ public: virtual MCAsmLexer &getLexer() { return Lexer; } virtual MCContext &getContext() { return Ctx; } virtual MCStreamer &getStreamer() { return Out; } - virtual unsigned getAssemblerDialect() { + virtual unsigned getAssemblerDialect() { if (AssemblerDialect == ~0U) - return MAI.getAssemblerDialect(); + return MAI.getAssemblerDialect(); else return AssemblerDialect; } @@ -168,6 +209,18 @@ public: virtual const AsmToken &Lex(); + void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; } + bool isParsingInlineAsm() { return ParsingInlineAsm; } + + bool ParseMSInlineAsm(void *AsmLoc, std::string &AsmString, + unsigned &NumOutputs, unsigned &NumInputs, + SmallVectorImpl<std::pair<void *,bool> > &OpDecls, + SmallVectorImpl<std::string> &Constraints, + SmallVectorImpl<std::string> &Clobbers, + const MCInstrInfo *MII, + const MCInstPrinter *IP, + MCAsmParserSemaCallback &SI); + bool ParseExpression(const MCExpr *&Res); virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc); virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc); @@ -178,7 +231,7 @@ public: private: void CheckForValidSection(); - bool ParseStatement(); + bool ParseStatement(ParseStatementInfo &Info); void EatToEndOfLine(); bool ParseCppHashLineFilenameComment(const SMLoc &L); @@ -202,14 +255,18 @@ private: /// This returns true on failure. bool ProcessIncbinFile(const std::string &Filename); - /// \brief Reset the current lexer position to that given by \arg Loc. The + /// \brief Reset the current lexer position to that given by \p Loc. The /// current token is not set; clients should ensure Lex() is called /// subsequently. - void JumpToLoc(SMLoc Loc); + /// + /// \param InBuffer If not -1, should be the known buffer id that contains the + /// location. + void JumpToLoc(SMLoc Loc, int InBuffer=-1); virtual void EatToEndOfStatement(); - bool ParseMacroArgument(MacroArgument &MA); + bool ParseMacroArgument(MacroArgument &MA, + AsmToken::TokenKind &ArgumentDelimiter); bool ParseMacroArguments(const Macro *M, MacroArguments &A); /// \brief Parse up to the end of statement and a return the contents from the @@ -221,7 +278,8 @@ private: /// return the contents from the current token up to the end or comma. StringRef ParseStringToComma(); - bool ParseAssignment(StringRef Name, bool allow_redef); + bool ParseAssignment(StringRef Name, bool allow_redef, + bool NoDeadStrip = false); bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc); bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc); @@ -229,23 +287,31 @@ private: bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc); /// ParseIdentifier - Parse an identifier or string (as a quoted identifier) - /// and set \arg Res to the identifier contents. + /// and set \p Res to the identifier contents. virtual bool ParseIdentifier(StringRef &Res); // Directive Parsing. - // ".ascii", ".asciiz", ".string" + // ".ascii", ".asciiz", ".string" bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated); bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ... bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ... bool ParseDirectiveFill(); // ".fill" bool ParseDirectiveSpace(); // ".space" bool ParseDirectiveZero(); // ".zero" - bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv" + // ".set", ".equ", ".equiv" + bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); bool ParseDirectiveOrg(); // ".org" // ".align{,32}", ".p2align{,w,l}" bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize); + // ".bundle_align_mode" + bool ParseDirectiveBundleAlignMode(); + // ".bundle_lock" + bool ParseDirectiveBundleLock(); + // ".bundle_unlock" + bool ParseDirectiveBundleUnlock(); + /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which /// accepts a single symbol (which should be a label or an external). bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr); @@ -282,6 +348,9 @@ private: bool ParseDirectiveIrp(SMLoc DirectiveLoc); // ".irp" bool ParseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc" bool ParseDirectiveEndr(SMLoc DirectiveLoc); // ".endr" + + // "_emit" + bool ParseDirectiveEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info); }; /// \brief Generic implementations of directive handling, etc. which is shared @@ -344,6 +413,10 @@ public: &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape"); AddDirectiveHandler< &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame"); + AddDirectiveHandler< + &GenericAsmParser::ParseDirectiveCFIUndefined>(".cfi_undefined"); + AddDirectiveHandler< + &GenericAsmParser::ParseDirectiveCFIRegister>(".cfi_register"); // Macro directives. AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>( @@ -381,6 +454,8 @@ public: bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc); bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc); bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc); + bool ParseDirectiveCFIUndefined(StringRef, SMLoc DirectiveLoc); + bool ParseDirectiveCFIRegister(StringRef, SMLoc DirectiveLoc); bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc); bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc); @@ -406,8 +481,8 @@ AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out, const MCAsmInfo &_MAI) : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM), GenericParser(new GenericAsmParser), PlatformParser(0), - CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0), - AssemblerDialect(~0U) { + CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0), + AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) { // Save the old handler. SavedDiagHandler = SrcMgr.getDiagHandler(); SavedDiagContext = SrcMgr.getDiagContext(); @@ -428,6 +503,7 @@ AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, } else if (_MAI.hasSubsectionsViaSymbols()) { PlatformParser = createDarwinAsmParser(); PlatformParser->Initialize(*this); + IsDarwin = true; } else { PlatformParser = createELFAsmParser(); PlatformParser->Initialize(*this); @@ -497,8 +573,12 @@ bool AsmParser::ProcessIncbinFile(const std::string &Filename) { return false; } -void AsmParser::JumpToLoc(SMLoc Loc) { - CurBuffer = SrcMgr.FindBufferContainingLoc(Loc); +void AsmParser::JumpToLoc(SMLoc Loc, int InBuffer) { + if (InBuffer != -1) { + CurBuffer = InBuffer; + } else { + CurBuffer = SrcMgr.FindBufferContainingLoc(Loc); + } Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer()); } @@ -540,12 +620,14 @@ bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) { getStreamer().EmitLabel(SectionStartSym); getContext().setGenDwarfSectionStartSym(SectionStartSym); getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(), - StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier()); + StringRef(), + getContext().getMainFileName()); } // While we have input, parse each statement. while (Lexer.isNot(AsmToken::Eof)) { - if (!ParseStatement()) continue; + ParseStatementInfo Info; + if (!ParseStatement(Info)) continue; // We had an error, validate that one was emitted and recover by skipping to // the next line. @@ -598,7 +680,7 @@ bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) { } void AsmParser::CheckForValidSection() { - if (!getStreamer().getCurrentSection()) { + if (!ParsingInlineAsm && !getStreamer().getCurrentSection()) { TokError("expected section directive before assembly directive"); Out.SwitchSection(Ctx.getMachOSection( "__TEXT", "__text", @@ -650,7 +732,7 @@ bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) { if (ParseExpression(Res)) return true; if (Lexer.isNot(AsmToken::RParen)) return TokError("expected ')' in parentheses expression"); - EndLoc = Lexer.getLoc(); + EndLoc = Lexer.getTok().getEndLoc(); Lex(); return false; } @@ -664,7 +746,7 @@ bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) { if (ParseExpression(Res)) return true; if (Lexer.isNot(AsmToken::RBrac)) return TokError("expected ']' in brackets expression"); - EndLoc = Lexer.getLoc(); + EndLoc = Lexer.getTok().getEndLoc(); Lex(); return false; } @@ -691,12 +773,12 @@ bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { case AsmToken::Dollar: case AsmToken::String: case AsmToken::Identifier: { - EndLoc = Lexer.getLoc(); - StringRef Identifier; if (ParseIdentifier(Identifier)) return true; + EndLoc = SMLoc::getFromPointer(Identifier.end()); + // This is a symbol reference. std::pair<StringRef, StringRef> Split = Identifier.split('@'); MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first); @@ -729,7 +811,7 @@ bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { SMLoc Loc = getTok().getLoc(); int64_t IntVal = getTok().getIntVal(); Res = MCConstantExpr::Create(IntVal, getContext()); - EndLoc = Lexer.getLoc(); + EndLoc = Lexer.getTok().getEndLoc(); Lex(); // Eat token. // Look for 'b' or 'f' following an Integer as a directional label if (Lexer.getKind() == AsmToken::Identifier) { @@ -741,7 +823,7 @@ bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { getContext()); if (IDVal == "b" && Sym->isUndefined()) return Error(Loc, "invalid reference to undefined symbol"); - EndLoc = Lexer.getLoc(); + EndLoc = Lexer.getTok().getEndLoc(); Lex(); // Eat identifier. } } @@ -751,6 +833,7 @@ bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { APFloat RealVal(APFloat::IEEEdouble, getTok().getString()); uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue(); Res = MCConstantExpr::Create(IntVal, getContext()); + EndLoc = Lexer.getTok().getEndLoc(); Lex(); // Eat token. return false; } @@ -760,7 +843,7 @@ bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { MCSymbol *Sym = Ctx.CreateTempSymbol(); Out.EmitLabel(Sym); Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext()); - EndLoc = Lexer.getLoc(); + EndLoc = Lexer.getTok().getEndLoc(); Lex(); // Eat identifier. return false; } @@ -1024,14 +1107,11 @@ bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, } } - - - /// ParseStatement: /// ::= EndOfStatement /// ::= Label* Directive ...Operands... EndOfStatement /// ::= Label* Identifier OperandList* EndOfStatement -bool AsmParser::ParseStatement() { +bool AsmParser::ParseStatement(ParseStatementInfo &Info) { if (Lexer.is(AsmToken::EndOfStatement)) { Out.AddBlankLine(); Lex(); @@ -1134,7 +1214,8 @@ bool AsmParser::ParseStatement() { return Error(IDLoc, "invalid symbol redefinition"); // Emit the label. - Out.EmitLabel(Sym); + if (!ParsingInlineAsm) + Out.EmitLabel(Sym); // If we are generating dwarf for assembly source files then gather the // info to make a dwarf label entry for this label if needed. @@ -1150,7 +1231,7 @@ bool AsmParser::ParseStatement() { return false; } - return ParseStatement(); + return false; } case AsmToken::Equal: @@ -1232,6 +1313,13 @@ bool AsmParser::ParseStatement() { if (IDVal == ".p2alignl") return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); + if (IDVal == ".bundle_align_mode") + return ParseDirectiveBundleAlignMode(); + if (IDVal == ".bundle_lock") + return ParseDirectiveBundleLock(); + if (IDVal == ".bundle_unlock") + return ParseDirectiveBundleUnlock(); + if (IDVal == ".org") return ParseDirectiveOrg(); @@ -1304,26 +1392,31 @@ bool AsmParser::ParseStatement() { return Error(IDLoc, "unknown directive"); } + // _emit + if (ParsingInlineAsm && IDVal == "_emit") + return ParseDirectiveEmit(IDLoc, Info); + CheckForValidSection(); // Canonicalize the opcode to lower case. - SmallString<128> Opcode; + SmallString<128> OpcodeStr; for (unsigned i = 0, e = IDVal.size(); i != e; ++i) - Opcode.push_back(tolower(IDVal[i])); + OpcodeStr.push_back(tolower(IDVal[i])); - SmallVector<MCParsedAsmOperand*, 8> ParsedOperands; - bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc, - ParsedOperands); + ParseInstructionInfo IInfo(Info.AsmRewrites); + bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr.str(), + IDLoc,Info.ParsedOperands); + Info.ParseError = HadError; // Dump the parsed representation, if requested. if (getShowParsedOperands()) { SmallString<256> Str; raw_svector_ostream OS(Str); OS << "parsed instruction: ["; - for (unsigned i = 0; i != ParsedOperands.size(); ++i) { + for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) { if (i != 0) OS << ", "; - ParsedOperands[i]->print(OS); + Info.ParsedOperands[i]->print(OS); } OS << "]"; @@ -1334,22 +1427,39 @@ bool AsmParser::ParseStatement() { // section is the initial text section then generate a .loc directive for // the instruction. if (!HadError && getContext().getGenDwarfForAssembly() && - getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) { + getContext().getGenDwarfSection() == getStreamer().getCurrentSection()) { + + unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer); + + // If we previously parsed a cpp hash file line comment then make sure the + // current Dwarf File is for the CppHashFilename if not then emit the + // Dwarf File table for it and adjust the line number for the .loc. + const std::vector<MCDwarfFile *> &MCDwarfFiles = + getContext().getMCDwarfFiles(); + if (CppHashFilename.size() != 0) { + if(MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() != + CppHashFilename) + getStreamer().EmitDwarfFileDirective( + getContext().nextGenDwarfFileNumber(), StringRef(), CppHashFilename); + + unsigned CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc,CppHashBuf); + Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo); + } + getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(), - SrcMgr.FindLineNumber(IDLoc, CurBuffer), - 0, DWARF2_LINE_DEFAULT_IS_STMT ? + Line, 0, DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0, StringRef()); } // If parsing succeeded, match the instruction. - if (!HadError) - HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands, - Out); - - // Free any parsed operands. - for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i) - delete ParsedOperands[i]; + if (!HadError) { + unsigned ErrorInfo; + HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode, + Info.ParsedOperands, + Out, ErrorInfo, + ParsingInlineAsm); + } // Don't skip the rest of the line, the instruction parser is responsible for // that. @@ -1394,6 +1504,7 @@ bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) { CppHashLoc = L; CppHashFilename = Filename; CppHashLineNumber = LineNumber; + CppHashBuf = CurBuffer; // Ignore any trailing characters, they're just comment. EatToEndOfLine(); @@ -1419,7 +1530,7 @@ void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) { DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS); } - // If we have not parsed a cpp hash line filename comment or the source + // If we have not parsed a cpp hash line filename comment or the source // manager changed or buffer changed (like in a nested include) then just // print the normal diagnostic using its Filename and LineNo. if (!Parser->CppHashLineNumber || @@ -1432,7 +1543,7 @@ void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) { return; } - // Use the CppHashFilename and calculate a line number based on the + // Use the CppHashFilename and calculate a line number based on the // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for // the diagnostic. const std::string Filename = Parser->CppHashFilename; @@ -1470,6 +1581,8 @@ bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, if (NParameters != 0 && NParameters != A.size()) return Error(L, "Wrong number of arguments"); + // A macro without parameters is handled differently on Darwin: + // gas accepts no arguments and does no substitutions while (!Body.empty()) { // Scan for the next substitution. std::size_t End = Body.size(), Pos = 0; @@ -1533,18 +1646,26 @@ bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, StringRef Argument(Begin, I - (Pos +1)); unsigned Index = 0; for (; Index < NParameters; ++Index) - if (Parameters[Index] == Argument) + if (Parameters[Index].first == Argument) break; - // FIXME: We should error at the macro definition. - if (Index == NParameters) - return Error(L, "Parameter not found"); - - for (MacroArgument::const_iterator it = A[Index].begin(), - ie = A[Index].end(); it != ie; ++it) - OS << it->getString(); + if (Index == NParameters) { + if (Body[Pos+1] == '(' && Body[Pos+2] == ')') + Pos += 3; + else { + OS << '\\' << Argument; + Pos = I; + } + } else { + for (MacroArgument::const_iterator it = A[Index].begin(), + ie = A[Index].end(); it != ie; ++it) + if (it->getKind() == AsmToken::String) + OS << it->getStringContents(); + else + OS << it->getString(); - Pos += 1 + Argument.size(); + Pos += 1 + Argument.size(); + } } // Update the scan point. Body = Body.substr(Pos); @@ -1553,28 +1674,105 @@ bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, return false; } -MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL, +MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, + int EB, SMLoc EL, MemoryBuffer *I) - : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL) + : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB), + ExitLoc(EL) { } +static bool IsOperator(AsmToken::TokenKind kind) +{ + switch (kind) + { + default: + return false; + case AsmToken::Plus: + case AsmToken::Minus: + case AsmToken::Tilde: + case AsmToken::Slash: + case AsmToken::Star: + case AsmToken::Dot: + case AsmToken::Equal: + case AsmToken::EqualEqual: + case AsmToken::Pipe: + case AsmToken::PipePipe: + case AsmToken::Caret: + case AsmToken::Amp: + case AsmToken::AmpAmp: + case AsmToken::Exclaim: + case AsmToken::ExclaimEqual: + case AsmToken::Percent: + case AsmToken::Less: + case AsmToken::LessEqual: + case AsmToken::LessLess: + case AsmToken::LessGreater: + case AsmToken::Greater: + case AsmToken::GreaterEqual: + case AsmToken::GreaterGreater: + return true; + } +} + /// ParseMacroArgument - Extract AsmTokens for a macro argument. /// This is used for both default macro parameter values and the /// arguments in macro invocations -bool AsmParser::ParseMacroArgument(MacroArgument &MA) { +bool AsmParser::ParseMacroArgument(MacroArgument &MA, + AsmToken::TokenKind &ArgumentDelimiter) { unsigned ParenLevel = 0; + unsigned AddTokens = 0; + + // gas accepts arguments separated by whitespace, except on Darwin + if (!IsDarwin) + Lexer.setSkipSpace(false); for (;;) { - if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) + if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) { + Lexer.setSkipSpace(true); return TokError("unexpected token in macro instantiation"); + } + + if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) { + // Spaces and commas cannot be mixed to delimit parameters + if (ArgumentDelimiter == AsmToken::Eof) + ArgumentDelimiter = AsmToken::Comma; + else if (ArgumentDelimiter != AsmToken::Comma) { + Lexer.setSkipSpace(true); + return TokError("expected ' ' for macro argument separator"); + } + break; + } + + if (Lexer.is(AsmToken::Space)) { + Lex(); // Eat spaces + + // Spaces can delimit parameters, but could also be part an expression. + // If the token after a space is an operator, add the token and the next + // one into this argument + if (ArgumentDelimiter == AsmToken::Space || + ArgumentDelimiter == AsmToken::Eof) { + if (IsOperator(Lexer.getKind())) { + // Check to see whether the token is used as an operator, + // or part of an identifier + const char *NextChar = getTok().getEndLoc().getPointer(); + if (*NextChar == ' ') + AddTokens = 2; + } + + if (!AddTokens && ParenLevel == 0) { + if (ArgumentDelimiter == AsmToken::Eof && + !IsOperator(Lexer.getKind())) + ArgumentDelimiter = AsmToken::Space; + break; + } + } + } // HandleMacroEntry relies on not advancing the lexer here // to be able to fill in the remaining default parameter values if (Lexer.is(AsmToken::EndOfStatement)) break; - if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) - break; // Adjust the current parentheses level. if (Lexer.is(AsmToken::LParen)) @@ -1584,8 +1782,12 @@ bool AsmParser::ParseMacroArgument(MacroArgument &MA) { // Append the token to the current argument list. MA.push_back(getTok()); + if (AddTokens) + AddTokens--; Lex(); } + + Lexer.setSkipSpace(true); if (ParenLevel != 0) return TokError("unbalanced parentheses in macro argument"); return false; @@ -1594,6 +1796,9 @@ bool AsmParser::ParseMacroArgument(MacroArgument &MA) { // Parse the macro instantiation arguments. bool AsmParser::ParseMacroArguments(const Macro *M, MacroArguments &A) { const unsigned NParameters = M ? M->Parameters.size() : 0; + // Argument delimiter is initially unknown. It will be set by + // ParseMacroArgument() + AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof; // Parse two kinds of macro invocations: // - macros defined without any parameters accept an arbitrary number of them @@ -1602,13 +1807,30 @@ bool AsmParser::ParseMacroArguments(const Macro *M, MacroArguments &A) { ++Parameter) { MacroArgument MA; - if (ParseMacroArgument(MA)) + if (ParseMacroArgument(MA, ArgumentDelimiter)) return true; - A.push_back(MA); + if (!MA.empty() || !NParameters) + A.push_back(MA); + else if (NParameters) { + if (!M->Parameters[Parameter].second.empty()) + A.push_back(M->Parameters[Parameter].second); + } - if (Lexer.is(AsmToken::EndOfStatement)) + // At the end of the statement, fill in remaining arguments that have + // default values. If there aren't any, then the next argument is + // required but missing + if (Lexer.is(AsmToken::EndOfStatement)) { + if (NParameters && Parameter < NParameters - 1) { + if (M->Parameters[Parameter + 1].second.empty()) + return TokError("macro argument '" + + Twine(M->Parameters[Parameter + 1].first) + + "' is missing"); + else + continue; + } return false; + } if (Lexer.is(AsmToken::Comma)) Lex(); @@ -1652,6 +1874,7 @@ bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc, // Create the macro instantiation object and add to the current macro // instantiation stack. MacroInstantiation *MI = new MacroInstantiation(M, NameLoc, + CurBuffer, getTok().getLoc(), Instantiation); ActiveMacros.push_back(MI); @@ -1666,7 +1889,7 @@ bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc, void AsmParser::HandleMacroExit() { // Jump to the EndOfStatement we should return to, and consume it. - JumpToLoc(ActiveMacros.back()->ExitLoc); + JumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer); Lex(); // Pop the instantiation entry. @@ -1697,7 +1920,8 @@ static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) { llvm_unreachable("Unknown expr kind!"); } -bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) { +bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef, + bool NoDeadStrip) { // FIXME: Use better location, we should use proper tokens. SMLoc EqualLoc = Lexer.getLoc(); @@ -1752,6 +1976,9 @@ bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) { // Do the assignment. Out.EmitAssignment(Sym, Value); + if (NoDeadStrip) + Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip); + return false; } @@ -1809,7 +2036,7 @@ bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) { return TokError("unexpected token in '" + Twine(IDVal) + "'"); Lex(); - return ParseAssignment(Name, allow_redef); + return ParseAssignment(Name, allow_redef, true); } bool AsmParser::ParseEscapedString(std::string &Data) { @@ -2218,6 +2445,75 @@ bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) { return false; } + +/// ParseDirectiveBundleAlignMode +/// ::= {.bundle_align_mode} expression +bool AsmParser::ParseDirectiveBundleAlignMode() { + CheckForValidSection(); + + // Expect a single argument: an expression that evaluates to a constant + // in the inclusive range 0-30. + SMLoc ExprLoc = getLexer().getLoc(); + int64_t AlignSizePow2; + if (ParseAbsoluteExpression(AlignSizePow2)) + return true; + else if (getLexer().isNot(AsmToken::EndOfStatement)) + return TokError("unexpected token after expression in" + " '.bundle_align_mode' directive"); + else if (AlignSizePow2 < 0 || AlignSizePow2 > 30) + return Error(ExprLoc, + "invalid bundle alignment size (expected between 0 and 30)"); + + Lex(); + + // Because of AlignSizePow2's verified range we can safely truncate it to + // unsigned. + getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2)); + return false; +} + +/// ParseDirectiveBundleLock +/// ::= {.bundle_lock} [align_to_end] +bool AsmParser::ParseDirectiveBundleLock() { + CheckForValidSection(); + bool AlignToEnd = false; + + if (getLexer().isNot(AsmToken::EndOfStatement)) { + StringRef Option; + SMLoc Loc = getTok().getLoc(); + const char *kInvalidOptionError = + "invalid option for '.bundle_lock' directive"; + + if (ParseIdentifier(Option)) + return Error(Loc, kInvalidOptionError); + + if (Option != "align_to_end") + return Error(Loc, kInvalidOptionError); + else if (getLexer().isNot(AsmToken::EndOfStatement)) + return Error(Loc, + "unexpected token after '.bundle_lock' directive option"); + AlignToEnd = true; + } + + Lex(); + + getStreamer().EmitBundleLock(AlignToEnd); + return false; +} + +/// ParseDirectiveBundleLock +/// ::= {.bundle_lock} +bool AsmParser::ParseDirectiveBundleUnlock() { + CheckForValidSection(); + + if (getLexer().isNot(AsmToken::EndOfStatement)) + return TokError("unexpected token in '.bundle_unlock' directive"); + Lex(); + + getStreamer().EmitBundleUnlock(); + return false; +} + /// ParseDirectiveSymbolAttribute /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ] bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) { @@ -2704,7 +3000,7 @@ bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) { else if (Name == "epilogue_begin") Flags |= DWARF2_FLAG_EPILOGUE_BEGIN; else if (Name == "is_stmt") { - SMLoc Loc = getTok().getLoc(); + Loc = getTok().getLoc(); const MCExpr *Value; if (getParser().ParseExpression(Value)) return true; @@ -2723,7 +3019,7 @@ bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) { } } else if (Name == "isa") { - SMLoc Loc = getTok().getLoc(); + Loc = getTok().getLoc(); const MCExpr *Value; if (getParser().ParseExpression(Value)) return true; @@ -3060,6 +3356,43 @@ bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive, return false; } +/// ParseDirectiveCFIUndefined +/// ::= .cfi_undefined register +bool GenericAsmParser::ParseDirectiveCFIUndefined(StringRef Directive, + SMLoc DirectiveLoc) { + int64_t Register = 0; + + if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) + return true; + + getStreamer().EmitCFIUndefined(Register); + + return false; +} + +/// ParseDirectiveCFIRegister +/// ::= .cfi_register register, register +bool GenericAsmParser::ParseDirectiveCFIRegister(StringRef Directive, + SMLoc DirectiveLoc) { + int64_t Register1 = 0; + + if (ParseRegisterOrRegisterNumber(Register1, DirectiveLoc)) + return true; + + if (getLexer().isNot(AsmToken::Comma)) + return TokError("unexpected token in directive"); + Lex(); + + int64_t Register2 = 0; + + if (ParseRegisterOrRegisterNumber(Register2, DirectiveLoc)) + return true; + + getStreamer().EmitCFIRegister(Register1, Register2); + + return false; +} + /// ParseDirectiveMacrosOnOff /// ::= .macros_on /// ::= .macros_off @@ -3083,22 +3416,30 @@ bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive, return TokError("expected identifier in '.macro' directive"); MacroParameters Parameters; + // Argument delimiter is initially unknown. It will be set by + // ParseMacroArgument() + AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof; if (getLexer().isNot(AsmToken::EndOfStatement)) { for (;;) { MacroParameter Parameter; - if (getParser().ParseIdentifier(Parameter)) + if (getParser().ParseIdentifier(Parameter.first)) return TokError("expected identifier in '.macro' directive"); + + if (getLexer().is(AsmToken::Equal)) { + Lex(); + if (getParser().ParseMacroArgument(Parameter.second, ArgumentDelimiter)) + return true; + } + Parameters.push_back(Parameter); - if (getLexer().isNot(AsmToken::Comma)) + if (getLexer().is(AsmToken::Comma)) + Lex(); + else if (getLexer().is(AsmToken::EndOfStatement)) break; - Lex(); } } - if (getLexer().isNot(AsmToken::EndOfStatement)) - return TokError("unexpected token in '.macro' directive"); - // Eat the end of statement. Lex(); @@ -3253,6 +3594,7 @@ void AsmParser::InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc, // Create the macro instantiation object and add to the current macro // instantiation stack. MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc, + CurBuffer, getTok().getLoc(), Instantiation); ActiveMacros.push_back(MI); @@ -3303,7 +3645,7 @@ bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) { MacroParameters Parameters; MacroParameter Parameter; - if (ParseIdentifier(Parameter)) + if (ParseIdentifier(Parameter.first)) return TokError("expected identifier in '.irp' directive"); Parameters.push_back(Parameter); @@ -3349,7 +3691,7 @@ bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) { MacroParameters Parameters; MacroParameter Parameter; - if (ParseIdentifier(Parameter)) + if (ParseIdentifier(Parameter.first)) return TokError("expected identifier in '.irpc' directive"); Parameters.push_back(Parameter); @@ -3399,7 +3741,7 @@ bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) { bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) { if (ActiveMacros.empty()) - return TokError("unexpected '.endr' directive, no current .rept"); + return TokError("unmatched '.endr' directive"); // The only .repl that should get here are the ones created by // InstantiateMacroLikeBody. @@ -3409,6 +3751,217 @@ bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) { return false; } +bool AsmParser::ParseDirectiveEmit(SMLoc IDLoc, ParseStatementInfo &Info) { + const MCExpr *Value; + SMLoc ExprLoc = getLexer().getLoc(); + if (ParseExpression(Value)) + return true; + const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); + if (!MCE) + return Error(ExprLoc, "unexpected expression in _emit"); + uint64_t IntValue = MCE->getValue(); + if (!isUIntN(8, IntValue) && !isIntN(8, IntValue)) + return Error(ExprLoc, "literal value out of range for directive"); + + Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, 5)); + return false; +} + +bool AsmParser::ParseMSInlineAsm(void *AsmLoc, std::string &AsmString, + unsigned &NumOutputs, unsigned &NumInputs, + SmallVectorImpl<std::pair<void *, bool> > &OpDecls, + SmallVectorImpl<std::string> &Constraints, + SmallVectorImpl<std::string> &Clobbers, + const MCInstrInfo *MII, + const MCInstPrinter *IP, + MCAsmParserSemaCallback &SI) { + SmallVector<void *, 4> InputDecls; + SmallVector<void *, 4> OutputDecls; + SmallVector<bool, 4> InputDeclsOffsetOf; + SmallVector<bool, 4> OutputDeclsOffsetOf; + SmallVector<std::string, 4> InputConstraints; + SmallVector<std::string, 4> OutputConstraints; + std::set<std::string> ClobberRegs; + + SmallVector<struct AsmRewrite, 4> AsmStrRewrites; + + // Prime the lexer. + Lex(); + + // While we have input, parse each statement. + unsigned InputIdx = 0; + unsigned OutputIdx = 0; + while (getLexer().isNot(AsmToken::Eof)) { + ParseStatementInfo Info(&AsmStrRewrites); + if (ParseStatement(Info)) + return true; + + if (Info.ParseError) + return true; + + if (Info.Opcode != ~0U) { + const MCInstrDesc &Desc = MII->get(Info.Opcode); + + // Build the list of clobbers, outputs and inputs. + for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) { + MCParsedAsmOperand *Operand = Info.ParsedOperands[i]; + + // Immediate. + if (Operand->isImm()) { + if (Operand->needAsmRewrite()) + AsmStrRewrites.push_back(AsmRewrite(AOK_ImmPrefix, + Operand->getStartLoc())); + continue; + } + + // Register operand. + if (Operand->isReg() && !Operand->isOffsetOf()) { + unsigned NumDefs = Desc.getNumDefs(); + // Clobber. + if (NumDefs && Operand->getMCOperandNum() < NumDefs) { + std::string Reg; + raw_string_ostream OS(Reg); + IP->printRegName(OS, Operand->getReg()); + ClobberRegs.insert(StringRef(OS.str())); + } + continue; + } + + // Expr/Input or Output. + unsigned Size; + void *OpDecl = SI.LookupInlineAsmIdentifier(Operand->getName(), AsmLoc, + Size); + if (OpDecl) { + bool isOutput = (i == 1) && Desc.mayStore(); + if (!Operand->isOffsetOf() && Operand->needSizeDirective()) + AsmStrRewrites.push_back(AsmRewrite(AOK_SizeDirective, + Operand->getStartLoc(), + /*Len*/0, + Operand->getMemSize())); + if (isOutput) { + std::string Constraint = "="; + ++InputIdx; + OutputDecls.push_back(OpDecl); + OutputDeclsOffsetOf.push_back(Operand->isOffsetOf()); + Constraint += Operand->getConstraint().str(); + OutputConstraints.push_back(Constraint); + AsmStrRewrites.push_back(AsmRewrite(AOK_Output, + Operand->getStartLoc(), + Operand->getNameLen())); + } else { + InputDecls.push_back(OpDecl); + InputDeclsOffsetOf.push_back(Operand->isOffsetOf()); + InputConstraints.push_back(Operand->getConstraint().str()); + AsmStrRewrites.push_back(AsmRewrite(AOK_Input, + Operand->getStartLoc(), + Operand->getNameLen())); + } + } + } + } + } + + // Set the number of Outputs and Inputs. + NumOutputs = OutputDecls.size(); + NumInputs = InputDecls.size(); + + // Set the unique clobbers. + for (std::set<std::string>::iterator I = ClobberRegs.begin(), + E = ClobberRegs.end(); I != E; ++I) + Clobbers.push_back(*I); + + // Merge the various outputs and inputs. Output are expected first. + if (NumOutputs || NumInputs) { + unsigned NumExprs = NumOutputs + NumInputs; + OpDecls.resize(NumExprs); + Constraints.resize(NumExprs); + // FIXME: Constraints are hard coded to 'm', but we need an 'r' + // constraint for offsetof. This needs to be cleaned up! + for (unsigned i = 0; i < NumOutputs; ++i) { + OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsOffsetOf[i]); + Constraints[i] = OutputDeclsOffsetOf[i] ? "=r" : OutputConstraints[i]; + } + for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) { + OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsOffsetOf[i]); + Constraints[j] = InputDeclsOffsetOf[i] ? "r" : InputConstraints[i]; + } + } + + // Build the IR assembly string. + std::string AsmStringIR; + AsmRewriteKind PrevKind = AOK_Imm; + raw_string_ostream OS(AsmStringIR); + const char *Start = SrcMgr.getMemoryBuffer(0)->getBufferStart(); + for (SmallVectorImpl<struct AsmRewrite>::iterator + I = AsmStrRewrites.begin(), E = AsmStrRewrites.end(); I != E; ++I) { + const char *Loc = (*I).Loc.getPointer(); + + AsmRewriteKind Kind = (*I).Kind; + + // Emit everything up to the immediate/expression. If the previous rewrite + // was a size directive, then this has already been done. + if (PrevKind != AOK_SizeDirective) + OS << StringRef(Start, Loc - Start); + PrevKind = Kind; + + // Skip the original expression. + if (Kind == AOK_Skip) { + Start = Loc + (*I).Len; + continue; + } + + // Rewrite expressions in $N notation. + switch (Kind) { + default: break; + case AOK_Imm: + OS << Twine("$$"); + OS << (*I).Val; + break; + case AOK_ImmPrefix: + OS << Twine("$$"); + break; + case AOK_Input: + OS << '$'; + OS << InputIdx++; + break; + case AOK_Output: + OS << '$'; + OS << OutputIdx++; + break; + case AOK_SizeDirective: + switch((*I).Val) { + default: break; + case 8: OS << "byte ptr "; break; + case 16: OS << "word ptr "; break; + case 32: OS << "dword ptr "; break; + case 64: OS << "qword ptr "; break; + case 80: OS << "xword ptr "; break; + case 128: OS << "xmmword ptr "; break; + case 256: OS << "ymmword ptr "; break; + } + break; + case AOK_Emit: + OS << ".byte"; + break; + case AOK_DotOperator: + OS << (*I).Val; + break; + } + + // Skip the original expression. + if (Kind != AOK_SizeDirective) + Start = Loc + (*I).Len; + } + + // Emit the remainder of the asm string. + const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd(); + if (Start != AsmEnd) + OS << StringRef(Start, AsmEnd - Start); + + AsmString = OS.str(); + return false; +} + /// \brief Create an MCAsmParser instance. MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C, MCStreamer &Out, diff --git a/lib/MC/MCParser/COFFAsmParser.cpp b/lib/MC/MCParser/COFFAsmParser.cpp index c4cdc3c..e7c564a 100644 --- a/lib/MC/MCParser/COFFAsmParser.cpp +++ b/lib/MC/MCParser/COFFAsmParser.cpp @@ -12,11 +12,11 @@ #include "llvm/ADT/Twine.h" #include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCContext.h" +#include "llvm/MC/MCExpr.h" #include "llvm/MC/MCParser/MCAsmLexer.h" #include "llvm/MC/MCRegisterInfo.h" #include "llvm/MC/MCSectionCOFF.h" #include "llvm/MC/MCStreamer.h" -#include "llvm/MC/MCExpr.h" #include "llvm/MC/MCTargetAsmParser.h" #include "llvm/Support/COFF.h" using namespace llvm; diff --git a/lib/MC/MCParser/DarwinAsmParser.cpp b/lib/MC/MCParser/DarwinAsmParser.cpp index 18033d0..7b042df 100644 --- a/lib/MC/MCParser/DarwinAsmParser.cpp +++ b/lib/MC/MCParser/DarwinAsmParser.cpp @@ -8,15 +8,15 @@ //===----------------------------------------------------------------------===// #include "llvm/MC/MCParser/MCAsmParserExtension.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSwitch.h" +#include "llvm/ADT/Twine.h" #include "llvm/MC/MCContext.h" +#include "llvm/MC/MCParser/MCAsmLexer.h" +#include "llvm/MC/MCParser/MCAsmParser.h" #include "llvm/MC/MCSectionMachO.h" #include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCSymbol.h" -#include "llvm/MC/MCParser/MCAsmLexer.h" -#include "llvm/MC/MCParser/MCAsmParser.h" -#include "llvm/ADT/StringSwitch.h" -#include "llvm/ADT/StringRef.h" -#include "llvm/ADT/Twine.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/SourceMgr.h" using namespace llvm; @@ -314,7 +314,7 @@ bool DarwinAsmParser::ParseSectionSwitch(const char *Segment, Lex(); // FIXME: Arch specific. - bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack. + bool isText = TAA & MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS; getStreamer().SwitchSection(getContext().getMachOSection( Segment, Section, TAA, StubSize, isText ? SectionKind::getText() diff --git a/lib/MC/MCParser/MCAsmLexer.cpp b/lib/MC/MCParser/MCAsmLexer.cpp index 3a3ff14..3867691 100644 --- a/lib/MC/MCParser/MCAsmLexer.cpp +++ b/lib/MC/MCParser/MCAsmLexer.cpp @@ -12,7 +12,8 @@ using namespace llvm; -MCAsmLexer::MCAsmLexer() : CurTok(AsmToken::Error, StringRef()), TokStart(0) { +MCAsmLexer::MCAsmLexer() : CurTok(AsmToken::Error, StringRef()), + TokStart(0), SkipSpace(true) { } MCAsmLexer::~MCAsmLexer() { @@ -27,5 +28,5 @@ SMLoc AsmToken::getLoc() const { } SMLoc AsmToken::getEndLoc() const { - return SMLoc::getFromPointer(Str.data() + Str.size() - 1); + return SMLoc::getFromPointer(Str.data() + Str.size()); } diff --git a/lib/MC/MCParser/MCAsmParser.cpp b/lib/MC/MCParser/MCAsmParser.cpp index 93ee2dd..a8b00cd 100644 --- a/lib/MC/MCParser/MCAsmParser.cpp +++ b/lib/MC/MCParser/MCAsmParser.cpp @@ -8,13 +8,13 @@ //===----------------------------------------------------------------------===// #include "llvm/MC/MCParser/MCAsmParser.h" +#include "llvm/ADT/Twine.h" #include "llvm/MC/MCParser/MCAsmLexer.h" #include "llvm/MC/MCParser/MCParsedAsmOperand.h" #include "llvm/MC/MCTargetAsmParser.h" +#include "llvm/Support/Debug.h" #include "llvm/Support/SourceMgr.h" #include "llvm/Support/raw_ostream.h" -#include "llvm/Support/Debug.h" -#include "llvm/ADT/Twine.h" using namespace llvm; MCAsmParser::MCAsmParser() : TargetParser(0), ShowParsedOperands(0) { @@ -44,7 +44,7 @@ bool MCAsmParser::ParseExpression(const MCExpr *&Res) { } void MCParsedAsmOperand::dump() const { -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) dbgs() << " " << *this; #endif } diff --git a/lib/MC/MCParser/MCTargetAsmParser.cpp b/lib/MC/MCParser/MCTargetAsmParser.cpp index 6fb1ba4..60a3a3b 100644 --- a/lib/MC/MCParser/MCTargetAsmParser.cpp +++ b/lib/MC/MCParser/MCTargetAsmParser.cpp @@ -11,7 +11,7 @@ using namespace llvm; MCTargetAsmParser::MCTargetAsmParser() - : AvailableFeatures(0) + : AvailableFeatures(0), ParsingInlineAsm(false) { } diff --git a/lib/MC/MCPureStreamer.cpp b/lib/MC/MCPureStreamer.cpp index 9ccab93..97e5a69 100644 --- a/lib/MC/MCPureStreamer.cpp +++ b/lib/MC/MCPureStreamer.cpp @@ -37,7 +37,7 @@ public: virtual void InitSections(); virtual void EmitLabel(MCSymbol *Symbol); - virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); + virtual void EmitDebugLabel(MCSymbol *Symbol); virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, uint64_t Size = 0, unsigned ByteAlignment = 0); virtual void EmitBytes(StringRef Data, unsigned AddrSpace); @@ -135,12 +135,9 @@ void MCPureStreamer::EmitLabel(MCSymbol *Symbol) { SD.setOffset(F->getContents().size()); } -void MCPureStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { - // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into - // MCObjectStreamer. - // FIXME: Lift context changes into super class. - getAssembler().getOrCreateSymbolData(*Symbol); - Symbol->setVariableValue(AddValueSymbols(Value)); + +void MCPureStreamer::EmitDebugLabel(MCSymbol *Symbol) { + EmitLabel(Symbol); } void MCPureStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, @@ -191,7 +188,8 @@ bool MCPureStreamer::EmitValueToOffset(const MCExpr *Offset, } void MCPureStreamer::EmitInstToFragment(const MCInst &Inst) { - MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData()); + MCRelaxableFragment *IF = + new MCRelaxableFragment(Inst, getCurrentSectionData()); // Add the fixups and data. // @@ -203,7 +201,7 @@ void MCPureStreamer::EmitInstToFragment(const MCInst &Inst) { getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); VecOS.flush(); - IF->getCode() = Code; + IF->getContents() = Code; IF->getFixups() = Fixups; } @@ -219,7 +217,7 @@ void MCPureStreamer::EmitInstToData(const MCInst &Inst) { // Add the fixups and data. for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); - DF->addFixup(Fixups[i]); + DF->getFixups().push_back(Fixups[i]); } DF->getContents().append(Code.begin(), Code.end()); } diff --git a/lib/MC/MCRegisterInfo.cpp b/lib/MC/MCRegisterInfo.cpp index 4d1aff3..5c71106 100644 --- a/lib/MC/MCRegisterInfo.cpp +++ b/lib/MC/MCRegisterInfo.cpp @@ -24,6 +24,8 @@ unsigned MCRegisterInfo::getMatchingSuperReg(unsigned Reg, unsigned SubIdx, } unsigned MCRegisterInfo::getSubReg(unsigned Reg, unsigned Idx) const { + assert(Idx && Idx < getNumSubRegIndices() && + "This is not a subregister index"); // Get a pointer to the corresponding SubRegIndices list. This list has the // name of each sub-register in the same order as MCSubRegIterator. const uint16_t *SRI = SubRegIndices + get(Reg).SubRegIndices; @@ -34,6 +36,7 @@ unsigned MCRegisterInfo::getSubReg(unsigned Reg, unsigned Idx) const { } unsigned MCRegisterInfo::getSubRegIndex(unsigned Reg, unsigned SubReg) const { + assert(SubReg && SubReg < getNumRegs() && "This is not a register"); // Get a pointer to the corresponding SubRegIndices list. This list has the // name of each sub-register in the same order as MCSubRegIterator. const uint16_t *SRI = SubRegIndices + get(Reg).SubRegIndices; diff --git a/lib/MC/MCSection.cpp b/lib/MC/MCSection.cpp index a792d56..ccf4a7d 100644 --- a/lib/MC/MCSection.cpp +++ b/lib/MC/MCSection.cpp @@ -8,8 +8,8 @@ //===----------------------------------------------------------------------===// #include "llvm/MC/MCSection.h" -#include "llvm/MC/MCContext.h" #include "llvm/MC/MCAsmInfo.h" +#include "llvm/MC/MCContext.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; diff --git a/lib/MC/MCStreamer.cpp b/lib/MC/MCStreamer.cpp index 0bac24d..7dffc3e 100644 --- a/lib/MC/MCStreamer.cpp +++ b/lib/MC/MCStreamer.cpp @@ -7,23 +7,24 @@ // //===----------------------------------------------------------------------===// +#include "llvm/MC/MCStreamer.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/ADT/Twine.h" #include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCContext.h" -#include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCExpr.h" #include "llvm/MC/MCObjectWriter.h" #include "llvm/MC/MCSymbol.h" #include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/raw_ostream.h" #include "llvm/Support/LEB128.h" -#include "llvm/ADT/SmallString.h" -#include "llvm/ADT/Twine.h" +#include "llvm/Support/raw_ostream.h" #include <cstdlib> using namespace llvm; MCStreamer::MCStreamer(MCContext &Ctx) : Context(Ctx), EmitEHFrame(true), EmitDebugFrame(false), - CurrentW64UnwindInfo(0), LastSymbol(0) { + CurrentW64UnwindInfo(0), LastSymbol(0), + AutoInitSections(false) { const MCSection *section = NULL; SectionStack.push_back(std::make_pair(section, section)); } @@ -33,6 +34,18 @@ MCStreamer::~MCStreamer() { delete W64UnwindInfos[i]; } +void MCStreamer::reset() { + for (unsigned i = 0; i < getNumW64UnwindInfos(); ++i) + delete W64UnwindInfos[i]; + EmitEHFrame = true; + EmitDebugFrame = false; + CurrentW64UnwindInfo = 0; + LastSymbol = 0; + const MCSection *section = NULL; + SectionStack.clear(); + SectionStack.push_back(std::make_pair(section, section)); +} + const MCExpr *MCStreamer::BuildSymbolDiff(MCContext &Context, const MCSymbol *A, const MCSymbol *B) { @@ -181,6 +194,13 @@ void MCStreamer::EmitLabel(MCSymbol *Symbol) { LastSymbol = Symbol; } +void MCStreamer::EmitDebugLabel(MCSymbol *Symbol) { + assert(!Symbol->isVariable() && "Cannot emit a variable symbol!"); + assert(getCurrentSection() && "Cannot emit before setting section!"); + Symbol->setSection(*getCurrentSection()); + LastSymbol = Symbol; +} + void MCStreamer::EmitCompactUnwindEncoding(uint32_t CompactUnwindEncoding) { EnsureValidFrame(); MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); @@ -234,69 +254,58 @@ void MCStreamer::RecordProcEnd(MCDwarfFrameInfo &Frame) { EmitLabel(Frame.End); } -void MCStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) { +MCSymbol *MCStreamer::EmitCFICommon() { EnsureValidFrame(); - MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); MCSymbol *Label = getContext().CreateTempSymbol(); EmitLabel(Label); - MachineLocation Dest(MachineLocation::VirtualFP); - MachineLocation Source(Register, -Offset); - MCCFIInstruction Instruction(Label, Dest, Source); + return Label; +} + +void MCStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) { + MCSymbol *Label = EmitCFICommon(); + MCCFIInstruction Instruction = + MCCFIInstruction::createDefCfa(Label, Register, Offset); + MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); CurFrame->Instructions.push_back(Instruction); } void MCStreamer::EmitCFIDefCfaOffset(int64_t Offset) { - EnsureValidFrame(); + MCSymbol *Label = EmitCFICommon(); + MCCFIInstruction Instruction = + MCCFIInstruction::createDefCfaOffset(Label, Offset); MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); - MCSymbol *Label = getContext().CreateTempSymbol(); - EmitLabel(Label); - MachineLocation Dest(MachineLocation::VirtualFP); - MachineLocation Source(MachineLocation::VirtualFP, -Offset); - MCCFIInstruction Instruction(Label, Dest, Source); CurFrame->Instructions.push_back(Instruction); } void MCStreamer::EmitCFIAdjustCfaOffset(int64_t Adjustment) { - EnsureValidFrame(); + MCSymbol *Label = EmitCFICommon(); + MCCFIInstruction Instruction = + MCCFIInstruction::createAdjustCfaOffset(Label, Adjustment); MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); - MCSymbol *Label = getContext().CreateTempSymbol(); - EmitLabel(Label); - MachineLocation Dest(MachineLocation::VirtualFP); - MachineLocation Source(MachineLocation::VirtualFP, Adjustment); - MCCFIInstruction Instruction(MCCFIInstruction::RelMove, Label, Dest, Source); CurFrame->Instructions.push_back(Instruction); } void MCStreamer::EmitCFIDefCfaRegister(int64_t Register) { - EnsureValidFrame(); + MCSymbol *Label = EmitCFICommon(); + MCCFIInstruction Instruction = + MCCFIInstruction::createDefCfaRegister(Label, Register); MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); - MCSymbol *Label = getContext().CreateTempSymbol(); - EmitLabel(Label); - MachineLocation Dest(Register); - MachineLocation Source(MachineLocation::VirtualFP); - MCCFIInstruction Instruction(Label, Dest, Source); CurFrame->Instructions.push_back(Instruction); } void MCStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) { - EnsureValidFrame(); + MCSymbol *Label = EmitCFICommon(); + MCCFIInstruction Instruction = + MCCFIInstruction::createOffset(Label, Register, Offset); MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); - MCSymbol *Label = getContext().CreateTempSymbol(); - EmitLabel(Label); - MachineLocation Dest(Register, Offset); - MachineLocation Source(Register, Offset); - MCCFIInstruction Instruction(Label, Dest, Source); CurFrame->Instructions.push_back(Instruction); } void MCStreamer::EmitCFIRelOffset(int64_t Register, int64_t Offset) { - EnsureValidFrame(); + MCSymbol *Label = EmitCFICommon(); + MCCFIInstruction Instruction = + MCCFIInstruction::createRelOffset(Label, Register, Offset); MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); - MCSymbol *Label = getContext().CreateTempSymbol(); - EmitLabel(Label); - MachineLocation Dest(Register, Offset); - MachineLocation Source(Register, Offset); - MCCFIInstruction Instruction(MCCFIInstruction::RelMove, Label, Dest, Source); CurFrame->Instructions.push_back(Instruction); } @@ -316,48 +325,40 @@ void MCStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) { } void MCStreamer::EmitCFIRememberState() { - EnsureValidFrame(); + MCSymbol *Label = EmitCFICommon(); + MCCFIInstruction Instruction = MCCFIInstruction::createRememberState(Label); MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); - MCSymbol *Label = getContext().CreateTempSymbol(); - EmitLabel(Label); - MCCFIInstruction Instruction(MCCFIInstruction::RememberState, Label); CurFrame->Instructions.push_back(Instruction); } void MCStreamer::EmitCFIRestoreState() { // FIXME: Error if there is no matching cfi_remember_state. - EnsureValidFrame(); + MCSymbol *Label = EmitCFICommon(); + MCCFIInstruction Instruction = MCCFIInstruction::createRestoreState(Label); MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); - MCSymbol *Label = getContext().CreateTempSymbol(); - EmitLabel(Label); - MCCFIInstruction Instruction(MCCFIInstruction::RestoreState, Label); CurFrame->Instructions.push_back(Instruction); } void MCStreamer::EmitCFISameValue(int64_t Register) { - EnsureValidFrame(); + MCSymbol *Label = EmitCFICommon(); + MCCFIInstruction Instruction = + MCCFIInstruction::createSameValue(Label, Register); MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); - MCSymbol *Label = getContext().CreateTempSymbol(); - EmitLabel(Label); - MCCFIInstruction Instruction(MCCFIInstruction::SameValue, Label, Register); CurFrame->Instructions.push_back(Instruction); } void MCStreamer::EmitCFIRestore(int64_t Register) { - EnsureValidFrame(); + MCSymbol *Label = EmitCFICommon(); + MCCFIInstruction Instruction = + MCCFIInstruction::createRestore(Label, Register); MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); - MCSymbol *Label = getContext().CreateTempSymbol(); - EmitLabel(Label); - MCCFIInstruction Instruction(MCCFIInstruction::Restore, Label, Register); CurFrame->Instructions.push_back(Instruction); } void MCStreamer::EmitCFIEscape(StringRef Values) { - EnsureValidFrame(); + MCSymbol *Label = EmitCFICommon(); + MCCFIInstruction Instruction = MCCFIInstruction::createEscape(Label, Values); MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); - MCSymbol *Label = getContext().CreateTempSymbol(); - EmitLabel(Label); - MCCFIInstruction Instruction(MCCFIInstruction::Escape, Label, Values); CurFrame->Instructions.push_back(Instruction); } @@ -367,6 +368,22 @@ void MCStreamer::EmitCFISignalFrame() { CurFrame->IsSignalFrame = true; } +void MCStreamer::EmitCFIUndefined(int64_t Register) { + MCSymbol *Label = EmitCFICommon(); + MCCFIInstruction Instruction = + MCCFIInstruction::createUndefined(Label, Register); + MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); + CurFrame->Instructions.push_back(Instruction); +} + +void MCStreamer::EmitCFIRegister(int64_t Register1, int64_t Register2) { + MCSymbol *Label = EmitCFICommon(); + MCCFIInstruction Instruction = + MCCFIInstruction::createRegister(Label, Register1, Register2); + MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo(); + CurFrame->Instructions.push_back(Instruction); +} + void MCStreamer::setCurrentW64UnwindInfo(MCWin64EHUnwindInfo *Frame) { W64UnwindInfos.push_back(Frame); CurrentW64UnwindInfo = W64UnwindInfos.back(); @@ -561,6 +578,10 @@ void MCStreamer::EmitRegSave(const SmallVectorImpl<unsigned> &RegList, bool) { abort(); } +void MCStreamer::EmitTCEntry(const MCSymbol &S) { + llvm_unreachable("Unsupported method"); +} + /// EmitRawText - If this file is backed by an assembly streamer, this dumps /// the specified string in the output .s file. This capability is /// indicated by the hasRawTextSupport() predicate. diff --git a/lib/MC/MCSubtargetInfo.cpp b/lib/MC/MCSubtargetInfo.cpp index cbf853c..f18828d 100644 --- a/lib/MC/MCSubtargetInfo.cpp +++ b/lib/MC/MCSubtargetInfo.cpp @@ -8,10 +8,10 @@ //===----------------------------------------------------------------------===// #include "llvm/MC/MCSubtargetInfo.h" -#include "llvm/MC/MCInstrItineraries.h" -#include "llvm/MC/SubtargetFeature.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Triple.h" +#include "llvm/MC/MCInstrItineraries.h" +#include "llvm/MC/SubtargetFeature.h" #include "llvm/Support/raw_ostream.h" #include <algorithm> @@ -19,11 +19,28 @@ using namespace llvm; MCSchedModel MCSchedModel::DefaultSchedModel; // For unknown processors. +/// InitMCProcessorInfo - Set or change the CPU (optionally supplemented +/// with feature string). Recompute feature bits and scheduling model. +void +MCSubtargetInfo::InitMCProcessorInfo(StringRef CPU, StringRef FS) { + SubtargetFeatures Features(FS); + FeatureBits = Features.getFeatureBits(CPU, ProcDesc, NumProcs, + ProcFeatures, NumFeatures); + + if (!CPU.empty()) + CPUSchedModel = getSchedModelForCPU(CPU); + else + CPUSchedModel = &MCSchedModel::DefaultSchedModel; +} + void MCSubtargetInfo::InitMCSubtargetInfo(StringRef TT, StringRef CPU, StringRef FS, const SubtargetFeatureKV *PF, const SubtargetFeatureKV *PD, const SubtargetInfoKV *ProcSched, + const MCWriteProcResEntry *WPR, + const MCWriteLatencyEntry *WL, + const MCReadAdvanceEntry *RA, const InstrStage *IS, const unsigned *OC, const unsigned *FP, @@ -31,26 +48,18 @@ MCSubtargetInfo::InitMCSubtargetInfo(StringRef TT, StringRef CPU, StringRef FS, TargetTriple = TT; ProcFeatures = PF; ProcDesc = PD; - ProcSchedModel = ProcSched; + ProcSchedModels = ProcSched; + WriteProcResTable = WPR; + WriteLatencyTable = WL; + ReadAdvanceTable = RA; + Stages = IS; OperandCycles = OC; ForwardingPaths = FP; NumFeatures = NF; NumProcs = NP; - SubtargetFeatures Features(FS); - FeatureBits = Features.getFeatureBits(CPU, ProcDesc, NumProcs, - ProcFeatures, NumFeatures); -} - - -/// ReInitMCSubtargetInfo - Change CPU (and optionally supplemented with -/// feature string) and recompute feature bits. -uint64_t MCSubtargetInfo::ReInitMCSubtargetInfo(StringRef CPU, StringRef FS) { - SubtargetFeatures Features(FS); - FeatureBits = Features.getFeatureBits(CPU, ProcDesc, NumProcs, - ProcFeatures, NumFeatures); - return FeatureBits; + InitMCProcessorInfo(CPU, FS); } /// ToggleFeature - Toggle a feature and returns the re-computed feature @@ -72,11 +81,11 @@ uint64_t MCSubtargetInfo::ToggleFeature(StringRef FS) { const MCSchedModel * MCSubtargetInfo::getSchedModelForCPU(StringRef CPU) const { - assert(ProcSchedModel && "Processor machine model not available!"); + assert(ProcSchedModels && "Processor machine model not available!"); #ifndef NDEBUG for (size_t i = 1; i < NumProcs; i++) { - assert(strcmp(ProcSchedModel[i - 1].Key, ProcSchedModel[i].Key) < 0 && + assert(strcmp(ProcSchedModels[i - 1].Key, ProcSchedModels[i].Key) < 0 && "Processor machine model table is not sorted"); } #endif @@ -85,8 +94,8 @@ MCSubtargetInfo::getSchedModelForCPU(StringRef CPU) const { SubtargetInfoKV KV; KV.Key = CPU.data(); const SubtargetInfoKV *Found = - std::lower_bound(ProcSchedModel, ProcSchedModel+NumProcs, KV); - if (Found == ProcSchedModel+NumProcs || StringRef(Found->Key) != CPU) { + std::lower_bound(ProcSchedModels, ProcSchedModels+NumProcs, KV); + if (Found == ProcSchedModels+NumProcs || StringRef(Found->Key) != CPU) { errs() << "'" << CPU << "' is not a recognized processor for this target" << " (ignoring processor)\n"; @@ -101,3 +110,9 @@ MCSubtargetInfo::getInstrItineraryForCPU(StringRef CPU) const { const MCSchedModel *SchedModel = getSchedModelForCPU(CPU); return InstrItineraryData(SchedModel, Stages, OperandCycles, ForwardingPaths); } + +/// Initialize an InstrItineraryData instance. +void MCSubtargetInfo::initInstrItins(InstrItineraryData &InstrItins) const { + InstrItins = + InstrItineraryData(CPUSchedModel, Stages, OperandCycles, ForwardingPaths); +} diff --git a/lib/MC/MCSymbol.cpp b/lib/MC/MCSymbol.cpp index f60126b..b973c57 100644 --- a/lib/MC/MCSymbol.cpp +++ b/lib/MC/MCSymbol.cpp @@ -26,7 +26,7 @@ static bool isAcceptableChar(char C) { return true; } -/// NameNeedsQuoting - Return true if the identifier \arg Str needs quotes to be +/// NameNeedsQuoting - Return true if the identifier \p Str needs quotes to be /// syntactically correct. static bool NameNeedsQuoting(StringRef Str) { assert(!Str.empty() && "Cannot create an empty MCSymbol"); @@ -76,7 +76,7 @@ void MCSymbol::print(raw_ostream &OS) const { OS << '"' << getName() << '"'; } -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) void MCSymbol::dump() const { print(dbgs()); } diff --git a/lib/MC/MCTargetAsmLexer.cpp b/lib/MC/MCTargetAsmLexer.cpp deleted file mode 100644 index c01c914..0000000 --- a/lib/MC/MCTargetAsmLexer.cpp +++ /dev/null @@ -1,16 +0,0 @@ -//===-- llvm/MC/MCTargetAsmLexer.cpp - Target Assembly Lexer --------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "llvm/MC/MCTargetAsmLexer.h" -using namespace llvm; - -MCTargetAsmLexer::MCTargetAsmLexer(const Target &T) - : TheTarget(T), Lexer(NULL) { -} -MCTargetAsmLexer::~MCTargetAsmLexer() {} diff --git a/lib/MC/MCValue.cpp b/lib/MC/MCValue.cpp index a37149d..4393777 100644 --- a/lib/MC/MCValue.cpp +++ b/lib/MC/MCValue.cpp @@ -31,7 +31,7 @@ void MCValue::print(raw_ostream &OS, const MCAsmInfo *MAI) const { OS << " + " << getConstant(); } -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) void MCValue::dump() const { print(dbgs(), 0); } diff --git a/lib/MC/MCWin64EH.cpp b/lib/MC/MCWin64EH.cpp index c05b4b1..c5b637c 100644 --- a/lib/MC/MCWin64EH.cpp +++ b/lib/MC/MCWin64EH.cpp @@ -8,13 +8,13 @@ //===----------------------------------------------------------------------===// #include "llvm/MC/MCWin64EH.h" -#include "llvm/MC/MCStreamer.h" +#include "llvm/ADT/Twine.h" #include "llvm/MC/MCContext.h" +#include "llvm/MC/MCExpr.h" #include "llvm/MC/MCObjectFileInfo.h" -#include "llvm/MC/MCSymbol.h" #include "llvm/MC/MCSectionCOFF.h" -#include "llvm/MC/MCExpr.h" -#include "llvm/ADT/Twine.h" +#include "llvm/MC/MCStreamer.h" +#include "llvm/MC/MCSymbol.h" namespace llvm { diff --git a/lib/MC/MachObjectWriter.cpp b/lib/MC/MachObjectWriter.cpp index c57b0d6..0098bea 100644 --- a/lib/MC/MachObjectWriter.cpp +++ b/lib/MC/MachObjectWriter.cpp @@ -10,24 +10,33 @@ #include "llvm/MC/MCMachObjectWriter.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/Twine.h" -#include "llvm/MC/MCAssembler.h" #include "llvm/MC/MCAsmBackend.h" #include "llvm/MC/MCAsmLayout.h" +#include "llvm/MC/MCAssembler.h" #include "llvm/MC/MCExpr.h" #include "llvm/MC/MCFixupKindInfo.h" +#include "llvm/MC/MCMachOSymbolFlags.h" #include "llvm/MC/MCObjectWriter.h" #include "llvm/MC/MCSectionMachO.h" #include "llvm/MC/MCSymbol.h" -#include "llvm/MC/MCMachOSymbolFlags.h" #include "llvm/MC/MCValue.h" #include "llvm/Object/MachOFormat.h" #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorHandling.h" - #include <vector> using namespace llvm; using namespace llvm::object; +void MachObjectWriter::reset() { + Relocations.clear(); + IndirectSymBase.clear(); + StringTable.clear(); + LocalSymbolData.clear(); + ExternalSymbolData.clear(); + UndefinedSymbolData.clear(); + MCObjectWriter::reset(); +} + bool MachObjectWriter:: doesSymbolRequireExternRelocation(const MCSymbolData *SD) { // Undefined symbols are always extern. @@ -68,6 +77,11 @@ uint64_t MachObjectWriter::getSymbolAddress(const MCSymbolData* SD, // If this is a variable, then recursively evaluate now. if (S.isVariable()) { + if (const MCConstantExpr *C = + dyn_cast<const MCConstantExpr>(S.getVariableValue())) + return C->getValue(); + + MCValue Target; if (!S.getVariableValue()->EvaluateAsRelocatable(Target, Layout)) report_fatal_error("unable to evaluate offset for variable '" + @@ -140,8 +154,8 @@ void MachObjectWriter::WriteHeader(unsigned NumLoadCommands, /// WriteSegmentLoadCommand - Write a segment load command. /// -/// \arg NumSections - The number of sections in this segment. -/// \arg SectionDataSize - The total size of the sections. +/// \param NumSections The number of sections in this segment. +/// \param SectionDataSize The total size of the sections. void MachObjectWriter::WriteSegmentLoadCommand(unsigned NumSections, uint64_t VMSize, uint64_t SectionDataStartOffset, @@ -315,11 +329,7 @@ void MachObjectWriter::WriteNlist(MachSymbolData &MSD, // Compute the symbol address. if (Symbol.isDefined()) { - if (Symbol.isAbsolute()) { - Address = cast<MCConstantExpr>(Symbol.getVariableValue())->getValue(); - } else { - Address = getSymbolAddress(&Data, Layout); - } + Address = getSymbolAddress(&Data, Layout); } else if (Data.isCommon()) { // Common symbols are encoded with the size in the address // field, and their alignment in the flags. @@ -557,6 +567,26 @@ void MachObjectWriter::computeSectionAddresses(const MCAssembler &Asm, } } +void MachObjectWriter::markAbsoluteVariableSymbols(MCAssembler &Asm, + const MCAsmLayout &Layout) { + for (MCAssembler::symbol_iterator i = Asm.symbol_begin(), + e = Asm.symbol_end(); + i != e; ++i) { + MCSymbolData &SD = *i; + if (!SD.getSymbol().isVariable()) + continue; + + // Is the variable is a symbol difference (SA - SB + C) expression, + // and neither symbol is external, mark the variable as absolute. + const MCExpr *Expr = SD.getSymbol().getVariableValue(); + MCValue Value; + if (Expr->EvaluateAsRelocatable(Value, Layout)) { + if (Value.getSymA() && Value.getSymB()) + const_cast<MCSymbol*>(&SD.getSymbol())->setAbsolute(); + } + } +} + void MachObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm, const MCAsmLayout &Layout) { computeSectionAddresses(Asm, Layout); @@ -564,6 +594,10 @@ void MachObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm, // Create symbol data for any indirect symbols. BindIndirectSymbols(Asm); + // Mark symbol difference expressions in variables (from .set or = directives) + // as absolute. + markAbsoluteVariableSymbols(Asm, Layout); + // Compute symbol table information and bind symbol indices. ComputeSymbolTable(Asm, StringTable, LocalSymbolData, ExternalSymbolData, UndefinedSymbolData); @@ -795,8 +829,12 @@ void MachObjectWriter::WriteObject(MCAssembler &Asm, it = Asm.data_region_begin(), ie = Asm.data_region_end(); it != ie; ++it) { const DataRegionData *Data = &(*it); - uint64_t Start = getSymbolAddress(&Layout.getAssembler().getSymbolData(*Data->Start), Layout); - uint64_t End = getSymbolAddress(&Layout.getAssembler().getSymbolData(*Data->End), Layout); + uint64_t Start = + getSymbolAddress(&Layout.getAssembler().getSymbolData(*Data->Start), + Layout); + uint64_t End = + getSymbolAddress(&Layout.getAssembler().getSymbolData(*Data->End), + Layout); DEBUG(dbgs() << "data in code region-- kind: " << Data->Kind << " start: " << Start << "(" << Data->Start->getName() << ")" << " end: " << End << "(" << Data->End->getName() << ")" diff --git a/lib/MC/SubtargetFeature.cpp b/lib/MC/SubtargetFeature.cpp index 317a48e..7625abd 100644 --- a/lib/MC/SubtargetFeature.cpp +++ b/lib/MC/SubtargetFeature.cpp @@ -119,14 +119,15 @@ void SubtargetFeatures::AddFeature(const StringRef String, } /// Find KV in array using binary search. -template<typename T> const T *Find(const StringRef S, const T *A, size_t L) { +static const SubtargetFeatureKV *Find(StringRef S, const SubtargetFeatureKV *A, + size_t L) { // Make the lower bound element we're looking for - T KV; + SubtargetFeatureKV KV; KV.Key = S.data(); // Determine the end of the array - const T *Hi = A + L; + const SubtargetFeatureKV *Hi = A + L; // Binary search the array - const T *F = std::lower_bound(A, Hi, KV); + const SubtargetFeatureKV *F = std::lower_bound(A, Hi, KV); // If not found then return NULL if (F == Hi || StringRef(F->Key) != S) return NULL; // Return the found array item @@ -336,30 +337,6 @@ uint64_t SubtargetFeatures::getFeatureBits(const StringRef CPU, return Bits; } -/// Get scheduling itinerary of a CPU. -const void *SubtargetFeatures::getItinerary(const StringRef CPU, - const SubtargetInfoKV *Table, - size_t TableSize) { - assert(Table && "missing table"); -#ifndef NDEBUG - for (size_t i = 1; i < TableSize; i++) { - assert(strcmp(Table[i - 1].Key, Table[i].Key) < 0 && "Table is not sorted"); - } -#endif - - // Find entry - const SubtargetInfoKV *Entry = Find(CPU, Table, TableSize); - - if (Entry) { - return Entry->Value; - } else { - errs() << "'" << CPU - << "' is not a recognized processor for this target" - << " (ignoring processor)\n"; - return NULL; - } -} - /// print - Print feature string. /// void SubtargetFeatures::print(raw_ostream &OS) const { @@ -368,7 +345,7 @@ void SubtargetFeatures::print(raw_ostream &OS) const { OS << "\n"; } -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) /// dump - Dump feature info. /// void SubtargetFeatures::dump() const { diff --git a/lib/MC/WinCOFFObjectWriter.cpp b/lib/MC/WinCOFFObjectWriter.cpp index f706cac..01860c5 100644 --- a/lib/MC/WinCOFFObjectWriter.cpp +++ b/lib/MC/WinCOFFObjectWriter.cpp @@ -13,28 +13,24 @@ #define DEBUG_TYPE "WinCOFFObjectWriter" -#include "llvm/MC/MCObjectWriter.h" -#include "llvm/MC/MCSection.h" -#include "llvm/MC/MCContext.h" -#include "llvm/MC/MCSymbol.h" -#include "llvm/MC/MCExpr.h" -#include "llvm/MC/MCValue.h" -#include "llvm/MC/MCAssembler.h" -#include "llvm/MC/MCAsmLayout.h" -#include "llvm/MC/MCSectionCOFF.h" #include "llvm/MC/MCWinCOFFObjectWriter.h" - #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" - +#include "llvm/MC/MCAsmLayout.h" +#include "llvm/MC/MCAssembler.h" +#include "llvm/MC/MCContext.h" +#include "llvm/MC/MCExpr.h" +#include "llvm/MC/MCObjectWriter.h" +#include "llvm/MC/MCSection.h" +#include "llvm/MC/MCSectionCOFF.h" +#include "llvm/MC/MCSymbol.h" +#include "llvm/MC/MCValue.h" #include "llvm/Support/COFF.h" #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorHandling.h" - #include "llvm/Support/TimeValue.h" - #include <cstdio> using namespace llvm; diff --git a/lib/MC/WinCOFFStreamer.cpp b/lib/MC/WinCOFFStreamer.cpp index b026277..5489ef8 100644 --- a/lib/MC/WinCOFFStreamer.cpp +++ b/lib/MC/WinCOFFStreamer.cpp @@ -13,19 +13,19 @@ #define DEBUG_TYPE "WinCOFFStreamer" -#include "llvm/MC/MCObjectStreamer.h" +#include "llvm/MC/MCStreamer.h" +#include "llvm/MC/MCAsmBackend.h" +#include "llvm/MC/MCAsmLayout.h" +#include "llvm/MC/MCAssembler.h" +#include "llvm/MC/MCCodeEmitter.h" #include "llvm/MC/MCContext.h" +#include "llvm/MC/MCExpr.h" +#include "llvm/MC/MCObjectStreamer.h" #include "llvm/MC/MCSection.h" +#include "llvm/MC/MCSectionCOFF.h" #include "llvm/MC/MCSymbol.h" -#include "llvm/MC/MCExpr.h" #include "llvm/MC/MCValue.h" -#include "llvm/MC/MCAssembler.h" -#include "llvm/MC/MCAsmLayout.h" -#include "llvm/MC/MCCodeEmitter.h" -#include "llvm/MC/MCSectionCOFF.h" #include "llvm/MC/MCWin64EH.h" -#include "llvm/MC/MCAsmBackend.h" - #include "llvm/Support/COFF.h" #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorHandling.h" @@ -51,6 +51,7 @@ public: virtual void InitSections(); virtual void EmitLabel(MCSymbol *Symbol); + virtual void EmitDebugLabel(MCSymbol *Symbol); virtual void EmitAssemblerFlag(MCAssemblerFlag Flag); virtual void EmitThumbFunc(MCSymbol *Func); virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); @@ -70,22 +71,26 @@ public: uint64_t Size,unsigned ByteAlignment); virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment); - virtual void EmitBytes(StringRef Data, unsigned AddrSpace); - virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value, - unsigned ValueSize, unsigned MaxBytesToEmit); - virtual void EmitCodeAlignment(unsigned ByteAlignment, - unsigned MaxBytesToEmit); virtual void EmitFileDirective(StringRef Filename); - virtual void EmitInstruction(const MCInst &Instruction); virtual void EmitWin64EHHandlerData(); virtual void FinishImpl(); private: - virtual void EmitInstToFragment(const MCInst &Inst) { - llvm_unreachable("Not used by WinCOFF."); - } virtual void EmitInstToData(const MCInst &Inst) { - llvm_unreachable("Not used by WinCOFF."); + MCDataFragment *DF = getOrCreateDataFragment(); + + SmallVector<MCFixup, 4> Fixups; + SmallString<256> Code; + raw_svector_ostream VecOS(Code); + getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); + VecOS.flush(); + + // Add the fixups and data. + for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { + Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); + DF->getFixups().push_back(Fixups[i]); + } + DF->getContents().append(Code.begin(), Code.end()); } void SetSection(StringRef Section, @@ -120,7 +125,6 @@ private: SectionKind::getBSS()); EmitCodeAlignment(4, 0); } - }; } // end anonymous namespace. @@ -181,6 +185,9 @@ void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) { MCObjectStreamer::EmitLabel(Symbol); } +void WinCOFFStreamer::EmitDebugLabel(MCSymbol *Symbol) { + EmitLabel(Symbol); +} void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { llvm_unreachable("not implemented"); } @@ -198,11 +205,7 @@ void WinCOFFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { // don't really even do. if (Value->getKind() != MCExpr::SymbolRef) { - // TODO: This is exactly the same as MachOStreamer. Consider merging into - // MCObjectStreamer. - getAssembler().getOrCreateSymbolData(*Symbol); - AddValueSymbols(Value); - Symbol->setVariableValue(Value); + MCObjectStreamer::EmitAssignment(Symbol, Value); } else { // FIXME: This is a horrible way to do this :(. This should really be // handled after we are done with the MC* objects and immediately before @@ -297,9 +300,10 @@ void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) { MCDataFragment *DF = getOrCreateDataFragment(); - DF->addFixup(MCFixup::Create(DF->getContents().size(), - MCSymbolRefExpr::Create (Symbol, getContext ()), - FK_SecRel_4)); + DF->getFixups().push_back( + MCFixup::Create(DF->getContents().size(), + MCSymbolRefExpr::Create (Symbol, getContext ()), + FK_SecRel_4)); DF->getContents().resize(DF->getContents().size() + 4, 0); } @@ -333,64 +337,11 @@ void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, llvm_unreachable("not implemented"); } -void WinCOFFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { - // TODO: This is copied exactly from the MachOStreamer. Consider merging into - // MCObjectStreamer? - getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end()); -} - -void WinCOFFStreamer::EmitValueToAlignment(unsigned ByteAlignment, - int64_t Value, - unsigned ValueSize, - unsigned MaxBytesToEmit) { - // TODO: This is copied exactly from the MachOStreamer. Consider merging into - // MCObjectStreamer? - if (MaxBytesToEmit == 0) - MaxBytesToEmit = ByteAlignment; - new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, - getCurrentSectionData()); - - // Update the maximum alignment on the current section if necessary. - if (ByteAlignment > getCurrentSectionData()->getAlignment()) - getCurrentSectionData()->setAlignment(ByteAlignment); -} - -void WinCOFFStreamer::EmitCodeAlignment(unsigned ByteAlignment, - unsigned MaxBytesToEmit) { - // TODO: This is copied exactly from the MachOStreamer. Consider merging into - // MCObjectStreamer? - if (MaxBytesToEmit == 0) - MaxBytesToEmit = ByteAlignment; - MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit, - getCurrentSectionData()); - F->setEmitNops(true); - - // Update the maximum alignment on the current section if necessary. - if (ByteAlignment > getCurrentSectionData()->getAlignment()) - getCurrentSectionData()->setAlignment(ByteAlignment); -} - void WinCOFFStreamer::EmitFileDirective(StringRef Filename) { // Ignore for now, linkers don't care, and proper debug // info will be a much large effort. } -void WinCOFFStreamer::EmitInstruction(const MCInst &Instruction) { - for (unsigned i = 0, e = Instruction.getNumOperands(); i != e; ++i) - if (Instruction.getOperand(i).isExpr()) - AddValueSymbols(Instruction.getOperand(i).getExpr()); - - getCurrentSectionData()->setHasInstructions(true); - - MCInstFragment *Fragment = - new MCInstFragment(Instruction, getCurrentSectionData()); - - raw_svector_ostream VecOS(Fragment->getCode()); - - getAssembler().getEmitter().EncodeInstruction(Instruction, VecOS, - Fragment->getFixups()); -} - void WinCOFFStreamer::EmitWin64EHHandlerData() { MCStreamer::EmitWin64EHHandlerData(); |
