LLVM  7.0.0svn
MCAsmBackend.h
Go to the documentation of this file.
1 //===- llvm/MC/MCAsmBackend.h - MC Asm Backend ------------------*- 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_MCASMBACKEND_H
11 #define LLVM_MC_MCASMBACKEND_H
12 
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/Optional.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/MC/MCDirectives.h"
17 #include "llvm/MC/MCFixup.h"
18 #include "llvm/MC/MCFragment.h"
19 #include "llvm/Support/Endian.h"
20 #include <cstdint>
21 #include <memory>
22 
23 namespace llvm {
24 
25 class MCAsmLayout;
26 class MCAssembler;
27 class MCCFIInstruction;
28 class MCCodePadder;
29 struct MCFixupKindInfo;
30 class MCFragment;
31 class MCInst;
32 class MCObjectStreamer;
33 class MCObjectTargetWriter;
34 class MCObjectWriter;
35 struct MCCodePaddingContext;
36 class MCRelaxableFragment;
37 class MCSubtargetInfo;
38 class MCValue;
39 class raw_pwrite_stream;
40 
41 /// Generic interface to target specific assembler backends.
42 class MCAsmBackend {
43  std::unique_ptr<MCCodePadder> CodePadder;
44 
45 protected: // Can only create subclasses.
47 
48 public:
49  MCAsmBackend(const MCAsmBackend &) = delete;
50  MCAsmBackend &operator=(const MCAsmBackend &) = delete;
51  virtual ~MCAsmBackend();
52 
54 
55  /// lifetime management
56  virtual void reset() {}
57 
58  /// Create a new MCObjectWriter instance for use by the assembler backend to
59  /// emit the final object file.
60  std::unique_ptr<MCObjectWriter>
62 
63  /// Create an MCObjectWriter that writes two object files: a .o file which is
64  /// linked into the final program and a .dwo file which is used by debuggers.
65  /// This function is only supported with ELF targets.
66  std::unique_ptr<MCObjectWriter>
68 
69  virtual std::unique_ptr<MCObjectTargetWriter>
70  createObjectTargetWriter() const = 0;
71 
72  /// \name Target Fixup Interfaces
73  /// @{
74 
75  /// Get the number of target specific fixup kinds.
76  virtual unsigned getNumFixupKinds() const = 0;
77 
78  /// Map a relocation name used in .reloc to a fixup kind.
80 
81  /// Get information on a fixup kind.
82  virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const;
83 
84  /// Hook to check if a relocation is needed for some target specific reason.
85  virtual bool shouldForceRelocation(const MCAssembler &Asm,
86  const MCFixup &Fixup,
87  const MCValue &Target) {
88  return false;
89  }
90 
91  /// Apply the \p Value for given \p Fixup into the provided data fragment, at
92  /// the offset specified by the fixup and following the fixup kind as
93  /// appropriate. Errors (such as an out of range fixup value) should be
94  /// reported via \p Ctx.
95  /// The \p STI is present only for fragments of type MCRelaxableFragment and
96  /// MCDataFragment with hasInstructions() == true.
97  virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
99  uint64_t Value, bool IsResolved,
100  const MCSubtargetInfo *STI) const = 0;
101 
102  /// Check whether the given target requires emitting differences of two
103  /// symbols as a set of relocations.
104  virtual bool requiresDiffExpressionRelocations() const { return false; }
105 
106  /// @}
107 
108  /// \name Target Relaxation Interfaces
109  /// @{
110 
111  /// Check whether the given instruction may need relaxation.
112  ///
113  /// \param Inst - The instruction to test.
114  /// \param STI - The MCSubtargetInfo in effect when the instruction was
115  /// encoded.
116  virtual bool mayNeedRelaxation(const MCInst &Inst,
117  const MCSubtargetInfo &STI) const = 0;
118 
119  /// Target specific predicate for whether a given fixup requires the
120  /// associated instruction to be relaxed.
121  virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
122  uint64_t Value,
123  const MCRelaxableFragment *DF,
124  const MCAsmLayout &Layout,
125  const bool WasForced) const;
126 
127  /// Simple predicate for targets where !Resolved implies requiring relaxation
128  virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
129  const MCRelaxableFragment *DF,
130  const MCAsmLayout &Layout) const = 0;
131 
132  /// Relax the instruction in the given fragment to the next wider instruction.
133  ///
134  /// \param Inst The instruction to relax, which may be the same as the
135  /// output.
136  /// \param STI the subtarget information for the associated instruction.
137  /// \param [out] Res On return, the relaxed instruction.
138  virtual void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
139  MCInst &Res) const = 0;
140 
141  /// @}
142 
143  /// Returns the minimum size of a nop in bytes on this target. The assembler
144  /// will use this to emit excess padding in situations where the padding
145  /// required for simple alignment would be less than the minimum nop size.
146  ///
147  virtual unsigned getMinimumNopSize() const { return 1; }
148 
149  /// Write an (optimal) nop sequence of Count bytes to the given output. If the
150  /// target cannot generate such a sequence, it should return an error.
151  ///
152  /// \return - True on success.
153  virtual bool writeNopData(raw_ostream &OS, uint64_t Count) const = 0;
154 
155  /// Give backend an opportunity to finish layout after relaxation
156  virtual void finishLayout(MCAssembler const &Asm,
157  MCAsmLayout &Layout) const {}
158 
159  /// Handle any target-specific assembler flags. By default, do nothing.
161 
162  /// Generate the compact unwind encoding for the CFI instructions.
163  virtual uint32_t
165  return 0;
166  }
167 
168  /// Handles all target related code padding when starting to write a new
169  /// basic block to an object file.
170  ///
171  /// \param OS The streamer used for writing the padding data and function.
172  /// \param Context the context of the padding, Embeds the basic block's
173  /// parameters.
176  /// Handles all target related code padding after writing a block to an object
177  /// file.
178  ///
179  /// \param Context the context of the padding, Embeds the basic block's
180  /// parameters.
182  /// Handles all target related code padding before writing a new instruction
183  /// to an object file.
184  ///
185  /// \param Inst the instruction.
186  void handleCodePaddingInstructionBegin(const MCInst &Inst);
187  /// Handles all target related code padding after writing an instruction to an
188  /// object file.
189  ///
190  /// \param Inst the instruction.
191  void handleCodePaddingInstructionEnd(const MCInst &Inst);
192 
193  /// Relaxes a fragment (changes the size of the padding) according to target
194  /// requirements. The new size computation is done w.r.t a layout.
195  ///
196  /// \param PF The fragment to relax.
197  /// \param Layout Code layout information.
198  ///
199  /// \returns true iff any relaxation occurred.
200  bool relaxFragment(MCPaddingFragment *PF, MCAsmLayout &Layout);
201 };
202 
203 } // end namespace llvm
204 
205 #endif // LLVM_MC_MCASMBACKEND_H
Fragment for adding required padding.
Definition: MCFragment.h:340
void handleCodePaddingBasicBlockEnd(const MCCodePaddingContext &Context)
Handles all target related code padding after writing a block to an object file.
LLVMContext & Context
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
virtual bool mayNeedRelaxation(const MCInst &Inst, const MCSubtargetInfo &STI) const =0
Check whether the given instruction may need relaxation.
This represents an "assembler immediate".
Definition: MCValue.h:40
const support::endianness Endian
Definition: MCAsmBackend.h:53
virtual bool requiresDiffExpressionRelocations() const
Check whether the given target requires emitting differences of two symbols as a set of relocations...
Definition: MCAsmBackend.h:104
virtual void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI, MCInst &Res) const =0
Relax the instruction in the given fragment to the next wider instruction.
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
virtual ~MCAsmBackend()
virtual std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const =0
void handleCodePaddingInstructionEnd(const MCInst &Inst)
Handles all target related code padding after writing an instruction to an object file...
virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value, const MCRelaxableFragment *DF, const MCAsmLayout &Layout) const =0
Simple predicate for targets where !Resolved implies requiring relaxation.
std::unique_ptr< MCObjectWriter > createDwoObjectWriter(raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS) const
Create an MCObjectWriter that writes two object files: a .o file which is linked into the final progr...
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:74
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
virtual uint32_t generateCompactUnwindEncoding(ArrayRef< MCCFIInstruction >) const
Generate the compact unwind encoding for the CFI instructions.
Definition: MCAsmBackend.h:164
virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved, uint64_t Value, const MCRelaxableFragment *DF, const MCAsmLayout &Layout, const bool WasForced) const
Target specific predicate for whether a given fixup requires the associated instruction to be relaxed...
virtual bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target)
Hook to check if a relocation is needed for some target specific reason.
Definition: MCAsmBackend.h:85
Streaming object file generation interface.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
virtual unsigned getMinimumNopSize() const
Returns the minimum size of a nop in bytes on this target.
Definition: MCAsmBackend.h:147
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:118
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:270
virtual bool writeNopData(raw_ostream &OS, uint64_t Count) const =0
Write an (optimal) nop sequence of Count bytes to the given output.
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:291
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:23
void handleCodePaddingInstructionBegin(const MCInst &Inst)
Handles all target related code padding before writing a new instruction to an object file...
virtual unsigned getNumFixupKinds() const =0
Get the number of target specific fixup kinds.
PowerPC TLS Dynamic Call Fixup
void handleCodePaddingBasicBlockStart(MCObjectStreamer *OS, const MCCodePaddingContext &Context)
Handles all target related code padding when starting to write a new basic block to an object file...
virtual Optional< MCFixupKind > getFixupKind(StringRef Name) const
Map a relocation name used in .reloc to a fixup kind.
std::unique_ptr< MCObjectWriter > createObjectWriter(raw_pwrite_stream &OS) const
Create a new MCObjectWriter instance for use by the assembler backend to emit the final object file...
Target - Wrapper for Target specific information.
virtual void handleAssemblerFlag(MCAssemblerFlag Flag)
Handle any target-specific assembler flags. By default, do nothing.
Definition: MCAsmBackend.h:160
MCAssemblerFlag
Definition: MCDirectives.h:48
MCAsmBackend & operator=(const MCAsmBackend &)=delete
Generic base class for all target subtargets.
Target independent information on a fixup kind.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:341
bool relaxFragment(MCPaddingFragment *PF, MCAsmLayout &Layout)
Relaxes a fragment (changes the size of the padding) according to target requirements.
const unsigned Kind
LLVM Value Representation.
Definition: Value.h:73
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:42
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
virtual void reset()
lifetime management
Definition: MCAsmBackend.h:56
virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target, MutableArrayRef< char > Data, uint64_t Value, bool IsResolved, const MCSubtargetInfo *STI) const =0
Apply the Value for given Fixup into the provided data fragment, at the offset specified by the fixup...
virtual void finishLayout(MCAssembler const &Asm, MCAsmLayout &Layout) const
Give backend an opportunity to finish layout after relaxation.
Definition: MCAsmBackend.h:156
MCAsmBackend(support::endianness Endian)