LLVM  5.0.0
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 <cstdint>
19 
20 namespace llvm {
21 
22 class MCAsmLayout;
23 class MCAssembler;
24 class MCCFIInstruction;
25 struct MCFixupKindInfo;
26 class MCFragment;
27 class MCInst;
28 class MCObjectWriter;
29 class MCRelaxableFragment;
30 class MCSubtargetInfo;
31 class MCValue;
32 class raw_pwrite_stream;
33 
34 /// Generic interface to target specific assembler backends.
35 class MCAsmBackend {
36 protected: // Can only create subclasses.
37  MCAsmBackend();
38 
39 public:
40  MCAsmBackend(const MCAsmBackend &) = delete;
41  MCAsmBackend &operator=(const MCAsmBackend &) = delete;
42  virtual ~MCAsmBackend();
43 
44  /// lifetime management
45  virtual void reset() {}
46 
47  /// Create a new MCObjectWriter instance for use by the assembler backend to
48  /// emit the final object file.
49  virtual MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const = 0;
50 
51  /// \name Target Fixup Interfaces
52  /// @{
53 
54  /// Get the number of target specific fixup kinds.
55  virtual unsigned getNumFixupKinds() const = 0;
56 
57  /// Map a relocation name used in .reloc to a fixup kind.
59 
60  /// Get information on a fixup kind.
61  virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const;
62 
63  /// Hook to check if a relocation is needed for some target specific reason.
64  virtual bool shouldForceRelocation(const MCAssembler &Asm,
65  const MCFixup &Fixup,
66  const MCValue &Target) {
67  return false;
68  }
69 
70  /// Apply the \p Value for given \p Fixup into the provided data fragment, at
71  /// the offset specified by the fixup and following the fixup kind as
72  /// appropriate. Errors (such as an out of range fixup value) should be
73  /// reported via \p Ctx.
74  virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
76  uint64_t Value, bool IsResolved) const = 0;
77 
78  /// @}
79 
80  /// \name Target Relaxation Interfaces
81  /// @{
82 
83  /// Check whether the given instruction may need relaxation.
84  ///
85  /// \param Inst - The instruction to test.
86  virtual bool mayNeedRelaxation(const MCInst &Inst) const = 0;
87 
88  /// Target specific predicate for whether a given fixup requires the
89  /// associated instruction to be relaxed.
90  virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
91  uint64_t Value,
92  const MCRelaxableFragment *DF,
93  const MCAsmLayout &Layout) const;
94 
95  /// Simple predicate for targets where !Resolved implies requiring relaxation
96  virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
97  const MCRelaxableFragment *DF,
98  const MCAsmLayout &Layout) const = 0;
99 
100  /// Relax the instruction in the given fragment to the next wider instruction.
101  ///
102  /// \param Inst The instruction to relax, which may be the same as the
103  /// output.
104  /// \param STI the subtarget information for the associated instruction.
105  /// \param [out] Res On return, the relaxed instruction.
106  virtual void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
107  MCInst &Res) const = 0;
108 
109  /// @}
110 
111  /// Returns the minimum size of a nop in bytes on this target. The assembler
112  /// will use this to emit excess padding in situations where the padding
113  /// required for simple alignment would be less than the minimum nop size.
114  ///
115  virtual unsigned getMinimumNopSize() const { return 1; }
116 
117  /// Write an (optimal) nop sequence of Count bytes to the given output. If the
118  /// target cannot generate such a sequence, it should return an error.
119  ///
120  /// \return - True on success.
121  virtual bool writeNopData(uint64_t Count, MCObjectWriter *OW) const = 0;
122 
123  /// Give backend an opportunity to finish layout after relaxation
124  virtual void finishLayout(MCAssembler const &Asm,
125  MCAsmLayout &Layout) const {}
126 
127  /// Handle any target-specific assembler flags. By default, do nothing.
129 
130  /// \brief Generate the compact unwind encoding for the CFI instructions.
131  virtual uint32_t
133  return 0;
134  }
135 };
136 
137 } // end namespace llvm
138 
139 #endif // LLVM_MC_MCASMBACKEND_H
virtual 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...
virtual void finishLayout(MCAssembler const &Asm, MCAsmLayout &Layout) const
Give backend an opportunity to finish layout after relaxation.
Definition: MCAsmBackend.h:124
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 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 ~MCAsmBackend()
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 unsigned getMinimumNopSize() const
Returns the minimum size of a nop in bytes on this target.
Definition: MCAsmBackend.h:115
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 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:64
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
const char * Name
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:255
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.
virtual Optional< MCFixupKind > getFixupKind(StringRef Name) const
Map a relocation name used in .reloc to a fixup kind.
virtual unsigned getNumFixupKinds() const =0
Get the number of target specific fixup kinds.
Target - Wrapper for Target specific information.
virtual void handleAssemblerFlag(MCAssemblerFlag Flag)
Handle any target-specific assembler flags. By default, do nothing.
Definition: MCAsmBackend.h:128
MCAssemblerFlag
Definition: MCDirectives.h:48
MCAsmBackend & operator=(const MCAsmBackend &)=delete
MCSubtargetInfo - Generic base class for all target subtargets.
virtual uint32_t generateCompactUnwindEncoding(ArrayRef< MCCFIInstruction >) const
Generate the compact unwind encoding for the CFI instructions.
Definition: MCAsmBackend.h:132
Target independent information on a fixup kind.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:333
const unsigned Kind
LLVM Value Representation.
Definition: Value.h:73
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:35
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
virtual void reset()
lifetime management
Definition: MCAsmBackend.h:45