LLVM  6.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 <cstdint>
20 #include <memory>
21 
22 namespace llvm {
23 
24 class MCAsmLayout;
25 class MCAssembler;
26 class MCCFIInstruction;
27 class MCCodePadder;
28 struct MCFixupKindInfo;
29 class MCFragment;
30 class MCInst;
31 class MCObjectStreamer;
32 class MCObjectWriter;
33 struct MCCodePaddingContext;
34 class MCRelaxableFragment;
35 class MCSubtargetInfo;
36 class MCValue;
37 class raw_pwrite_stream;
38 
39 /// Generic interface to target specific assembler backends.
40 class MCAsmBackend {
41  std::unique_ptr<MCCodePadder> CodePadder;
42 
43 protected: // Can only create subclasses.
44  MCAsmBackend();
45  MCAsmBackend(std::unique_ptr<MCCodePadder> TargetCodePadder);
46 
47 public:
48  MCAsmBackend(const MCAsmBackend &) = delete;
49  MCAsmBackend &operator=(const MCAsmBackend &) = delete;
50  virtual ~MCAsmBackend();
51 
52  /// lifetime management
53  virtual void reset() {}
54 
55  /// Create a new MCObjectWriter instance for use by the assembler backend to
56  /// emit the final object file.
57  virtual std::unique_ptr<MCObjectWriter>
59 
60  /// \name Target Fixup Interfaces
61  /// @{
62 
63  /// Get the number of target specific fixup kinds.
64  virtual unsigned getNumFixupKinds() const = 0;
65 
66  /// Map a relocation name used in .reloc to a fixup kind.
68 
69  /// Get information on a fixup kind.
70  virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const;
71 
72  /// Hook to check if a relocation is needed for some target specific reason.
73  virtual bool shouldForceRelocation(const MCAssembler &Asm,
74  const MCFixup &Fixup,
75  const MCValue &Target) {
76  return false;
77  }
78 
79  /// Apply the \p Value for given \p Fixup into the provided data fragment, at
80  /// the offset specified by the fixup and following the fixup kind as
81  /// appropriate. Errors (such as an out of range fixup value) should be
82  /// reported via \p Ctx.
83  virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
85  uint64_t Value, bool IsResolved) const = 0;
86 
87  /// @}
88 
89  /// \name Target Relaxation Interfaces
90  /// @{
91 
92  /// Check whether the given instruction may need relaxation.
93  ///
94  /// \param Inst - The instruction to test.
95  virtual bool mayNeedRelaxation(const MCInst &Inst) const = 0;
96 
97  /// Target specific predicate for whether a given fixup requires the
98  /// associated instruction to be relaxed.
99  virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
100  uint64_t Value,
101  const MCRelaxableFragment *DF,
102  const MCAsmLayout &Layout) const;
103 
104  /// Simple predicate for targets where !Resolved implies requiring relaxation
105  virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
106  const MCRelaxableFragment *DF,
107  const MCAsmLayout &Layout) const = 0;
108 
109  /// Relax the instruction in the given fragment to the next wider instruction.
110  ///
111  /// \param Inst The instruction to relax, which may be the same as the
112  /// output.
113  /// \param STI the subtarget information for the associated instruction.
114  /// \param [out] Res On return, the relaxed instruction.
115  virtual void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
116  MCInst &Res) const = 0;
117 
118  /// @}
119 
120  /// Returns the minimum size of a nop in bytes on this target. The assembler
121  /// will use this to emit excess padding in situations where the padding
122  /// required for simple alignment would be less than the minimum nop size.
123  ///
124  virtual unsigned getMinimumNopSize() const { return 1; }
125 
126  /// Write an (optimal) nop sequence of Count bytes to the given output. If the
127  /// target cannot generate such a sequence, it should return an error.
128  ///
129  /// \return - True on success.
130  virtual bool writeNopData(uint64_t Count, MCObjectWriter *OW) const = 0;
131 
132  /// Give backend an opportunity to finish layout after relaxation
133  virtual void finishLayout(MCAssembler const &Asm,
134  MCAsmLayout &Layout) const {}
135 
136  /// Handle any target-specific assembler flags. By default, do nothing.
138 
139  /// \brief Generate the compact unwind encoding for the CFI instructions.
140  virtual uint32_t
142  return 0;
143  }
144 
145  /// Handles all target related code padding when starting to write a new
146  /// basic block to an object file.
147  ///
148  /// \param OS The streamer used for writing the padding data and function.
149  /// \param Context the context of the padding, Embeds the basic block's
150  /// parameters.
153  /// Handles all target related code padding after writing a block to an object
154  /// file.
155  ///
156  /// \param Context the context of the padding, Embeds the basic block's
157  /// parameters.
159  /// Handles all target related code padding before writing a new instruction
160  /// to an object file.
161  ///
162  /// \param Inst the instruction.
163  void handleCodePaddingInstructionBegin(const MCInst &Inst);
164  /// Handles all target related code padding after writing an instruction to an
165  /// object file.
166  ///
167  /// \param Inst the instruction.
168  void handleCodePaddingInstructionEnd(const MCInst &Inst);
169 
170  /// Relaxes a fragment (changes the size of the padding) according to target
171  /// requirements. The new size computation is done w.r.t a layout.
172  ///
173  /// \param PF The fragment to relax.
174  /// \param Layout Code layout information.
175  ///
176  /// \returns true iff any relaxation occured.
177  bool relaxFragment(MCPaddingFragment *PF, MCAsmLayout &Layout);
178 };
179 
180 } // end namespace llvm
181 
182 #endif // LLVM_MC_MCASMBACKEND_H
Fragment for adding required padding.
Definition: MCFragment.h:333
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 std::unique_ptr< MCObjectWriter > createObjectWriter(raw_pwrite_stream &OS) const =0
Create a new MCObjectWriter instance for use by the assembler backend to emit the final object file...
This represents an "assembler immediate".
Definition: MCValue.h:40
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()
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.
Defines the object file and target independent interfaces used by the assembler backend to write nati...
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:66
virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target, MutableArrayRef< char > Data, uint64_t Value, bool IsResolved) const =0
Apply the Value for given Fixup into the provided data fragment, at the offset specified by the fixup...
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:141
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:73
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:124
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
virtual bool writeNopData(uint64_t Count, MCObjectWriter *OW) const =0
Write an (optimal) nop sequence of Count bytes to the given output.
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:121
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:257
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
virtual bool mayNeedRelaxation(const MCInst &Inst) const =0
Check whether the given instruction may need relaxation.
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 bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved, uint64_t Value, const MCRelaxableFragment *DF, const MCAsmLayout &Layout) const
Target specific predicate for whether a given fixup requires the associated instruction to be relaxed...
virtual Optional< MCFixupKind > getFixupKind(StringRef Name) const
Map a relocation name used in .reloc to a fixup kind.
Target - Wrapper for Target specific information.
virtual void handleAssemblerFlag(MCAssemblerFlag Flag)
Handle any target-specific assembler flags. By default, do nothing.
Definition: MCAsmBackend.h:137
MCAssemblerFlag
Definition: MCDirectives.h:48
MCAsmBackend & operator=(const MCAsmBackend &)=delete
MCSubtargetInfo - 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:337
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:40
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
virtual void reset()
lifetime management
Definition: MCAsmBackend.h:53
virtual void finishLayout(MCAssembler const &Asm, MCAsmLayout &Layout) const
Give backend an opportunity to finish layout after relaxation.
Definition: MCAsmBackend.h:133