LLVM  4.0.0
MCObjectStreamer.h
Go to the documentation of this file.
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:
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 
63 
64  void insert(MCFragment *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.
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) override;
93  void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
94  void EmitValueImpl(const MCExpr *Value, unsigned Size,
95  SMLoc Loc = SMLoc()) override;
96  void EmitULEB128Value(const MCExpr *Value) override;
97  void EmitSLEB128Value(const MCExpr *Value) override;
98  void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
99  void ChangeSection(MCSection *Section, const MCExpr *Subsection) override;
100  void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo& STI) override;
101 
102  /// \brief Emit an instruction to a special fragment, because this instruction
103  /// can change its size during relaxation.
104  virtual void EmitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &);
105 
106  void EmitBundleAlignMode(unsigned AlignPow2) override;
107  void EmitBundleLock(bool AlignToEnd) override;
108  void EmitBundleUnlock() override;
109  void EmitBytes(StringRef Data) override;
110  void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
111  unsigned ValueSize = 1,
112  unsigned MaxBytesToEmit = 0) override;
113  void EmitCodeAlignment(unsigned ByteAlignment,
114  unsigned MaxBytesToEmit = 0) override;
115  void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
116  SMLoc Loc) override;
117  void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
118  unsigned Column, unsigned Flags,
119  unsigned Isa, unsigned Discriminator,
120  StringRef FileName) override;
121  void EmitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel,
122  const MCSymbol *Label,
123  unsigned PointerSize);
124  void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
125  const MCSymbol *Label);
126  void EmitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line,
127  unsigned Column, bool PrologueEnd, bool IsStmt,
128  StringRef FileName, SMLoc Loc) override;
129  void EmitCVLinetableDirective(unsigned FunctionId, const MCSymbol *Begin,
130  const MCSymbol *End) override;
131  void EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
132  unsigned SourceFileId,
133  unsigned SourceLineNum,
134  const MCSymbol *FnStartSym,
135  const MCSymbol *FnEndSym) override;
137  ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
138  StringRef FixedSizePortion) override;
139  void EmitCVStringTableDirective() override;
140  void EmitCVFileChecksumsDirective() override;
141  void EmitDTPRel32Value(const MCExpr *Value) override;
142  void EmitDTPRel64Value(const MCExpr *Value) override;
143  void EmitTPRel32Value(const MCExpr *Value) override;
144  void EmitTPRel64Value(const MCExpr *Value) override;
145  void EmitGPRel32Value(const MCExpr *Value) override;
146  void EmitGPRel64Value(const MCExpr *Value) override;
148  const MCExpr *Expr, SMLoc Loc) override;
149  using MCStreamer::emitFill;
150  void emitFill(uint64_t NumBytes, uint8_t FillValue) override;
151  void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
152  SMLoc Loc = SMLoc()) override;
153  void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
154  SMLoc Loc = SMLoc()) override;
155 
156  void FinishImpl() override;
157 
158  /// Emit the absolute difference between two symbols if possible.
159  ///
160  /// Emit the absolute difference between \c Hi and \c Lo, as long as we can
161  /// compute it. Currently, that requires that both symbols are in the same
162  /// data fragment. Otherwise, do nothing and return \c false.
163  ///
164  /// \pre Offset of \c Hi is greater than the offset \c Lo.
165  void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
166  unsigned Size) override;
167 
168  bool mayHaveInstructions(MCSection &Sec) const override;
169 };
170 
171 } // end namespace llvm
172 
173 #endif
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:40
void EmitBytes(StringRef Data) override
Emit the bytes in Data into the output.
bool changeSectionImpl(MCSection *Section, const MCExpr *Subsection)
LLVMContext & Context
void EmitCVDefRangeDirective(ArrayRef< std::pair< const MCSymbol *, const MCSymbol * >> Ranges, StringRef FixedSizePortion) override
This implements the CodeView '.cv_def_range' assembler directive.
void EmitLabel(MCSymbol *Symbol) override
Emit a label for Symbol into the current section.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
virtual void EmitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &)
Emit an instruction to a special fragment, because this instruction can change its size during relaxa...
void EmitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel, const MCSymbol *Label, unsigned PointerSize)
void EmitCVStringTableDirective() override
This implements the CodeView '.cv_stringtable' assembler directive.
MCFragment * getCurrentFragment() const
struct fuzzer::@269 Flags
void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override
Emit an weak reference from Alias to Symbol.
MCSection * getCurrentSectionOnly() const
Definition: MCStreamer.h:302
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
bool EmitRelocDirective(const MCExpr &Offset, StringRef Name, const MCExpr *Expr, SMLoc Loc) override
Emit a .reloc directive.
bool mayHaveInstructions(MCSection &Sec) const override
void EmitBundleLock(bool AlignToEnd) override
The following instructions are a bundle-locked group.
Context object for machine code objects.
Definition: MCContext.h:51
void EmitDTPRel64Value(const MCExpr *Value) override
Emit the expression Value into the output as a dtprel (64-bit DTP relative) value.
#define F(x, y, z)
Definition: MD5.cpp:51
Streaming object file generation interface.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
void EmitDTPRel32Value(const MCExpr *Value) override
Emit the expression Value into the output as a dtprel (32-bit DTP relative) value.
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Size) override
Emit the absolute difference between two symbols if possible.
void EmitULEB128Value(const MCExpr *Value) override
Streaming machine code generation interface.
Definition: MCStreamer.h:161
void visitUsedSymbol(const MCSymbol &Sym) override
void EmitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line, unsigned Column, bool PrologueEnd, bool IsStmt, StringRef FileName, SMLoc Loc) override
This implements the CodeView '.cv_loc' assembler directive.
void EmitSLEB128Value(const MCExpr *Value) override
void insert(MCFragment *F)
void EmitCodeAlignment(unsigned ByteAlignment, unsigned MaxBytesToEmit=0) override
Emit nops until the byte alignment ByteAlignment is reached.
MCAssembler & getAssembler()
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:23
uint32_t Offset
static const unsigned End
void EmitFrames(MCAsmBackend *MAB)
void emitValueToOffset(const MCExpr *Offset, unsigned char Value, SMLoc Loc) override
Emit some number of copies of Value until the byte offset Offset is reached.
void emitFill(uint64_t NumBytes, uint8_t FillValue) override
Emit NumBytes bytes worth of the value specified by FillValue.
void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value=0, unsigned ValueSize=1, unsigned MaxBytesToEmit=0) override
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
Iterator for intrusive lists based on ilist_node.
void EmitCFISections(bool EH, bool Debug) override
void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel, const MCSymbol *Label)
void ChangeSection(MCSection *Section, const MCExpr *Subsection) override
Update streamer for a new active section.
void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
void EmitCVLinetableDirective(unsigned FunctionId, const MCSymbol *Begin, const MCSymbol *End) override
This implements the CodeView '.cv_linetable' assembler directive.
void flushPendingLabels(MCFragment *F, uint64_t FOffset=0)
If any labels have been emitted but not assigned fragments, ensure that they get assigned, either to F if possible or to a new data fragment.
void EmitBundleAlignMode(unsigned AlignPow2) override
Set the bundle alignment mode from now on in the section.
void EmitTPRel32Value(const MCExpr *Value) override
Emit the expression Value into the output as a tprel (32-bit TP relative) value.
iterator insert(iterator where, pointer New)
Definition: ilist.h:241
void EmitBundleUnlock() override
Ends a bundle-locked group.
void FinishImpl() override
Streamer specific finalization.
void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override
Emit an assignment of Value to Symbol.
bool isIntegratedAssemblerRequired() const override
Object streamers require the integrated assembler.
void reset() override
state management
MCSubtargetInfo - Generic base class for all target subtargets.
void EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc()) override
Emit the expression Value into the output as a native integer of the given Size bytes.
virtual void emitFill(uint64_t NumBytes, uint8_t FillValue)
Emit NumBytes bytes worth of the value specified by FillValue.
Definition: MCStreamer.cpp:157
void EmitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column, unsigned Flags, unsigned Isa, unsigned Discriminator, StringRef FileName) override
This implements the DWARF2 '.loc fileno lineno ...' assembler directive.
void EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum, const MCSymbol *FnStartSym, const MCSymbol *FnEndSym) override
This implements the CodeView '.cv_inline_linetable' assembler directive.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:333
Fragment for data and encoded instructions.
Definition: MCFragment.h:218
LLVM Value Representation.
Definition: Value.h:71
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:36
static cl::opt< bool, true > Debug("debug", cl::desc("Enable debug output"), cl::Hidden, cl::location(DebugFlag))
void EmitGPRel32Value(const MCExpr *Value) override
Emit the expression Value into the output as a gprel32 (32-bit GP relative) value.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
void EmitCVFileChecksumsDirective() override
This implements the CodeView '.cv_filechecksums' assembler directive.
Represents a location in source code.
Definition: SMLoc.h:24
void setParent(MCSection *Value)
Definition: MCFragment.h:98
MCSection::FragmentListType & getFragmentList()
Definition: MCSection.h:143
void EmitGPRel64Value(const MCExpr *Value) override
Emit the expression Value into the output as a gprel64 (64-bit GP relative) value.
MCDataFragment * getOrCreateDataFragment()
Get a data fragment to write into, creating a new one if the current fragment is not a data fragment...
MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB, raw_pwrite_stream &OS, MCCodeEmitter *Emitter)
void EmitTPRel64Value(const MCExpr *Value) override
Emit the expression Value into the output as a tprel (64-bit TP relative) value.