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