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   std::unique_ptr<MCObjectWriter> ObjectWriter;
     38   std::unique_ptr<MCAsmBackend> TAB;
     39   std::unique_ptr<MCCodeEmitter> Emitter;
     40   std::unique_ptr<MCAssembler> Assembler;
     41   MCSection::iterator CurInsertionPoint;
     42   bool EmitEHFrame;
     43   bool EmitDebugFrame;
     44   SmallVector<MCSymbol *, 2> PendingLabels;
     45 
     46   virtual void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo&) = 0;
     47   void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
     48   void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
     49   MCSymbol *EmitCFILabel() override;
     50 
     51 protected:
     52   MCObjectStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
     53                    raw_pwrite_stream &OS,
     54                    std::unique_ptr<MCCodeEmitter> Emitter);
     55   ~MCObjectStreamer();
     56 
     57 public:
     58   /// state management
     59   void reset() override;
     60 
     61   /// Object streamers require the integrated assembler.
     62   bool isIntegratedAssemblerRequired() const override { return true; }
     63 
     64   void EmitFrames(MCAsmBackend *MAB);
     65   void EmitCFISections(bool EH, bool Debug) override;
     66 
     67   MCFragment *getCurrentFragment() const;
     68 
     69   void insert(MCFragment *F) {
     70     flushPendingLabels(F);
     71     MCSection *CurSection = getCurrentSectionOnly();
     72     CurSection->getFragmentList().insert(CurInsertionPoint, F);
     73     F->setParent(CurSection);
     74   }
     75 
     76   /// Get a data fragment to write into, creating a new one if the current
     77   /// fragment is not a data fragment.
     78   MCDataFragment *getOrCreateDataFragment();
     79 
     80 protected:
     81   bool changeSectionImpl(MCSection *Section, const MCExpr *Subsection);
     82 
     83   /// If any labels have been emitted but not assigned fragments, ensure that
     84   /// they get assigned, either to F if possible or to a new data fragment.
     85   /// Optionally, it is also possible to provide an offset \p FOffset, which
     86   /// will be used as a symbol offset within the fragment.
     87   void flushPendingLabels(MCFragment *F, uint64_t FOffset = 0);
     88 
     89 public:
     90   void visitUsedSymbol(const MCSymbol &Sym) override;
     91 
     92   MCAssembler &getAssembler() { return *Assembler; }
     93 
     94   /// \name MCStreamer Interface
     95   /// @{
     96 
     97   void EmitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
     98   virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc, MCFragment *F);
     99   void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
    100   void EmitValueImpl(const MCExpr *Value, unsigned Size,
    101                      SMLoc Loc = SMLoc()) override;
    102   void EmitULEB128Value(const MCExpr *Value) override;
    103   void EmitSLEB128Value(const MCExpr *Value) override;
    104   void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
    105   void ChangeSection(MCSection *Section, const MCExpr *Subsection) override;
    106   void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
    107                        bool = false) override;
    108 
    109   /// \brief Emit an instruction to a special fragment, because this instruction
    110   /// can change its size during relaxation.
    111   virtual void EmitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &);
    112 
    113   void EmitBundleAlignMode(unsigned AlignPow2) override;
    114   void EmitBundleLock(bool AlignToEnd) override;
    115   void EmitBundleUnlock() override;
    116   void EmitBytes(StringRef Data) override;
    117   void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
    118                             unsigned ValueSize = 1,
    119                             unsigned MaxBytesToEmit = 0) override;
    120   void EmitCodeAlignment(unsigned ByteAlignment,
    121                          unsigned MaxBytesToEmit = 0) override;
    122   void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
    123                          SMLoc Loc) override;
    124   void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
    125                              unsigned Column, unsigned Flags,
    126                              unsigned Isa, unsigned Discriminator,
    127                              StringRef FileName) override;
    128   void EmitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel,
    129                                 const MCSymbol *Label,
    130                                 unsigned PointerSize);
    131   void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
    132                                  const MCSymbol *Label);
    133   void EmitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line,
    134                           unsigned Column, bool PrologueEnd, bool IsStmt,
    135                           StringRef FileName, SMLoc Loc) override;
    136   void EmitCVLinetableDirective(unsigned FunctionId, const MCSymbol *Begin,
    137                                 const MCSymbol *End) override;
    138   void EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
    139                                       unsigned SourceFileId,
    140                                       unsigned SourceLineNum,
    141                                       const MCSymbol *FnStartSym,
    142                                       const MCSymbol *FnEndSym) override;
    143   void EmitCVDefRangeDirective(
    144       ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
    145       StringRef FixedSizePortion) override;
    146   void EmitCVStringTableDirective() override;
    147   void EmitCVFileChecksumsDirective() override;
    148   void EmitCVFileChecksumOffsetDirective(unsigned FileNo) override;
    149   void EmitDTPRel32Value(const MCExpr *Value) override;
    150   void EmitDTPRel64Value(const MCExpr *Value) override;
    151   void EmitTPRel32Value(const MCExpr *Value) override;
    152   void EmitTPRel64Value(const MCExpr *Value) override;
    153   void EmitGPRel32Value(const MCExpr *Value) override;
    154   void EmitGPRel64Value(const MCExpr *Value) override;
    155   bool EmitRelocDirective(const MCExpr &Offset, StringRef Name,
    156                           const MCExpr *Expr, SMLoc Loc) override;
    157   using MCStreamer::emitFill;
    158   void emitFill(uint64_t NumBytes, uint8_t FillValue) override;
    159   void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
    160                 SMLoc Loc = SMLoc()) override;
    161   void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
    162                 SMLoc Loc = SMLoc()) override;
    163   void EmitFileDirective(StringRef Filename) override;
    164 
    165   void FinishImpl() override;
    166 
    167   /// Emit the absolute difference between two symbols if possible.
    168   ///
    169   /// Emit the absolute difference between \c Hi and \c Lo, as long as we can
    170   /// compute it.  Currently, that requires that both symbols are in the same
    171   /// data fragment.  Otherwise, do nothing and return \c false.
    172   ///
    173   /// \pre Offset of \c Hi is greater than the offset \c Lo.
    174   void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
    175                               unsigned Size) override;
    176 
    177   bool mayHaveInstructions(MCSection &Sec) const override;
    178 };
    179 
    180 } // end namespace llvm
    181 
    182 #endif
    183