Home | History | Annotate | Download | only in MC
      1 //===- MCObjectStreamer.h - MCStreamer Object File Interface ----*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #ifndef LLVM_MC_MCOBJECTSTREAMER_H
     11 #define LLVM_MC_MCOBJECTSTREAMER_H
     12 
     13 #include "llvm/ADT/SmallVector.h"
     14 #include "llvm/MC/MCAssembler.h"
     15 #include "llvm/MC/MCSection.h"
     16 #include "llvm/MC/MCStreamer.h"
     17 
     18 namespace llvm {
     19 class MCAssembler;
     20 class MCCodeEmitter;
     21 class MCSubtargetInfo;
     22 class MCExpr;
     23 class MCFragment;
     24 class MCDataFragment;
     25 class MCAsmBackend;
     26 class raw_ostream;
     27 class raw_pwrite_stream;
     28 
     29 /// \brief Streaming object file generation interface.
     30 ///
     31 /// This class provides an implementation of the MCStreamer interface which is
     32 /// suitable for use with the assembler backend. Specific object file formats
     33 /// are expected to subclass this interface to implement directives specific
     34 /// to that file format or custom semantics expected by the object writer
     35 /// implementation.
     36 class MCObjectStreamer : public MCStreamer {
     37   MCAssembler *Assembler;
     38   MCSection::iterator CurInsertionPoint;
     39   bool EmitEHFrame;
     40   bool EmitDebugFrame;
     41   SmallVector<MCSymbol *, 2> PendingLabels;
     42 
     43   virtual void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo&) = 0;
     44   void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
     45   void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
     46 
     47 protected:
     48   MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB, raw_pwrite_stream &OS,
     49                    MCCodeEmitter *Emitter);
     50   ~MCObjectStreamer() override;
     51 
     52 public:
     53   /// state management
     54   void reset() override;
     55 
     56   /// Object streamers require the integrated assembler.
     57   bool isIntegratedAssemblerRequired() const override { return true; }
     58 
     59   void EmitFrames(MCAsmBackend *MAB);
     60   void EmitCFISections(bool EH, bool Debug) override;
     61 
     62   MCFragment *getCurrentFragment() const;
     63 
     64   void insert(MCFragment *F) {
     65     flushPendingLabels(F);
     66     MCSection *CurSection = getCurrentSectionOnly();
     67     CurSection->getFragmentList().insert(CurInsertionPoint, F);
     68     F->setParent(CurSection);
     69   }
     70 
     71   /// Get a data fragment to write into, creating a new one if the current
     72   /// fragment is not a data fragment.
     73   MCDataFragment *getOrCreateDataFragment();
     74 
     75 protected:
     76   bool changeSectionImpl(MCSection *Section, const MCExpr *Subsection);
     77 
     78   /// If any labels have been emitted but not assigned fragments, ensure that
     79   /// they get assigned, either to F if possible or to a new data fragment.
     80   /// Optionally, it is also possible to provide an offset \p FOffset, which
     81   /// will be used as a symbol offset within the fragment.
     82   void flushPendingLabels(MCFragment *F, uint64_t FOffset = 0);
     83 
     84 public:
     85   void visitUsedSymbol(const MCSymbol &Sym) override;
     86 
     87   MCAssembler &getAssembler() { return *Assembler; }
     88 
     89   /// \name MCStreamer Interface
     90   /// @{
     91 
     92   void EmitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
     93   virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc, MCFragment *F);
     94   void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
     95   void EmitValueImpl(const MCExpr *Value, unsigned Size,
     96                      SMLoc Loc = SMLoc()) override;
     97   void EmitULEB128Value(const MCExpr *Value) override;
     98   void EmitSLEB128Value(const MCExpr *Value) override;
     99   void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
    100   void ChangeSection(MCSection *Section, const MCExpr *Subsection) override;
    101   void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
    102                        bool = false) override;
    103 
    104   /// \brief Emit an instruction to a special fragment, because this instruction
    105   /// can change its size during relaxation.
    106   virtual void EmitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &);
    107 
    108   void EmitBundleAlignMode(unsigned AlignPow2) override;
    109   void EmitBundleLock(bool AlignToEnd) override;
    110   void EmitBundleUnlock() override;
    111   void EmitBytes(StringRef Data) override;
    112   void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
    113                             unsigned ValueSize = 1,
    114                             unsigned MaxBytesToEmit = 0) override;
    115   void EmitCodeAlignment(unsigned ByteAlignment,
    116                          unsigned MaxBytesToEmit = 0) override;
    117   void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
    118                          SMLoc Loc) override;
    119   void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
    120                              unsigned Column, unsigned Flags,
    121                              unsigned Isa, unsigned Discriminator,
    122                              StringRef FileName) override;
    123   void EmitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel,
    124                                 const MCSymbol *Label,
    125                                 unsigned PointerSize);
    126   void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
    127                                  const MCSymbol *Label);
    128   void EmitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line,
    129                           unsigned Column, bool PrologueEnd, bool IsStmt,
    130                           StringRef FileName, SMLoc Loc) override;
    131   void EmitCVLinetableDirective(unsigned FunctionId, const MCSymbol *Begin,
    132                                 const MCSymbol *End) override;
    133   void EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
    134                                       unsigned SourceFileId,
    135                                       unsigned SourceLineNum,
    136                                       const MCSymbol *FnStartSym,
    137                                       const MCSymbol *FnEndSym) override;
    138   void EmitCVDefRangeDirective(
    139       ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
    140       StringRef FixedSizePortion) override;
    141   void EmitCVStringTableDirective() override;
    142   void EmitCVFileChecksumsDirective() override;
    143   void EmitDTPRel32Value(const MCExpr *Value) override;
    144   void EmitDTPRel64Value(const MCExpr *Value) override;
    145   void EmitTPRel32Value(const MCExpr *Value) override;
    146   void EmitTPRel64Value(const MCExpr *Value) override;
    147   void EmitGPRel32Value(const MCExpr *Value) override;
    148   void EmitGPRel64Value(const MCExpr *Value) override;
    149   bool EmitRelocDirective(const MCExpr &Offset, StringRef Name,
    150                           const MCExpr *Expr, SMLoc Loc) override;
    151   using MCStreamer::emitFill;
    152   void emitFill(uint64_t NumBytes, uint8_t FillValue) override;
    153   void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
    154                 SMLoc Loc = SMLoc()) override;
    155   void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
    156                 SMLoc Loc = SMLoc()) override;
    157   void EmitFileDirective(StringRef Filename) override;
    158 
    159   void FinishImpl() override;
    160 
    161   /// Emit the absolute difference between two symbols if possible.
    162   ///
    163   /// Emit the absolute difference between \c Hi and \c Lo, as long as we can
    164   /// compute it.  Currently, that requires that both symbols are in the same
    165   /// data fragment.  Otherwise, do nothing and return \c false.
    166   ///
    167   /// \pre Offset of \c Hi is greater than the offset \c Lo.
    168   void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
    169                               unsigned Size) override;
    170 
    171   bool mayHaveInstructions(MCSection &Sec) const override;
    172 };
    173 
    174 } // end namespace llvm
    175 
    176 #endif
    177