]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/MC/MCAsmBackend.h
Merge lldb trunk r321017 to contrib/llvm/tools/lldb.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / MC / MCAsmBackend.h
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>
58   createObjectWriter(raw_pwrite_stream &OS) const = 0;
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.
67   virtual Optional<MCFixupKind> getFixupKind(StringRef Name) const;
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,
84                           const MCValue &Target, MutableArrayRef<char> Data,
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.
137   virtual void handleAssemblerFlag(MCAssemblerFlag Flag) {}
138
139   /// \brief Generate the compact unwind encoding for the CFI instructions.
140   virtual uint32_t
141       generateCompactUnwindEncoding(ArrayRef<MCCFIInstruction>) const {
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.
151   void handleCodePaddingBasicBlockStart(MCObjectStreamer *OS,
152                                         const MCCodePaddingContext &Context);
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.
158   void handleCodePaddingBasicBlockEnd(const MCCodePaddingContext &Context);
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