File indexing completed on 2026-05-10 08:44:12
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_MC_MCASSEMBLER_H
0010 #define LLVM_MC_MCASSEMBLER_H
0011
0012 #include "llvm/ADT/ArrayRef.h"
0013 #include "llvm/ADT/SmallPtrSet.h"
0014 #include "llvm/ADT/SmallVector.h"
0015 #include "llvm/ADT/StringRef.h"
0016 #include "llvm/ADT/iterator.h"
0017 #include "llvm/ADT/iterator_range.h"
0018 #include "llvm/MC/MCDwarf.h"
0019 #include "llvm/MC/MCSymbol.h"
0020 #include "llvm/Support/SMLoc.h"
0021 #include <algorithm>
0022 #include <cassert>
0023 #include <cstddef>
0024 #include <cstdint>
0025 #include <memory>
0026 #include <string>
0027 #include <tuple>
0028 #include <utility>
0029 #include <vector>
0030
0031 namespace llvm {
0032
0033 class MCBoundaryAlignFragment;
0034 class MCCVDefRangeFragment;
0035 class MCCVInlineLineTableFragment;
0036 class MCDwarfCallFrameFragment;
0037 class MCDwarfLineAddrFragment;
0038 class MCEncodedFragment;
0039 class MCFixup;
0040 class MCLEBFragment;
0041 class MCPseudoProbeAddrFragment;
0042 class MCRelaxableFragment;
0043 class MCSymbolRefExpr;
0044 class raw_ostream;
0045 class MCAsmBackend;
0046 class MCContext;
0047 class MCCodeEmitter;
0048 class MCFragment;
0049 class MCObjectWriter;
0050 class MCSection;
0051 class MCValue;
0052
0053 class MCAssembler {
0054 public:
0055 friend class MCObjectWriter;
0056 using SectionListType = SmallVector<MCSection *, 0>;
0057 using const_iterator = pointee_iterator<SectionListType::const_iterator>;
0058
0059 private:
0060 MCContext &Context;
0061
0062 std::unique_ptr<MCAsmBackend> Backend;
0063 std::unique_ptr<MCCodeEmitter> Emitter;
0064 std::unique_ptr<MCObjectWriter> Writer;
0065
0066 bool HasLayout = false;
0067 bool RelaxAll = false;
0068
0069 SectionListType Sections;
0070
0071 SmallVector<const MCSymbol *, 0> Symbols;
0072
0073 MCDwarfLineTableParams LTParams;
0074
0075
0076
0077
0078
0079
0080
0081
0082 mutable SmallPtrSet<const MCSymbol *, 32> ThumbFuncs;
0083
0084
0085
0086
0087 unsigned BundleAlignSize = 0;
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103 bool evaluateFixup(const MCFixup &Fixup, const MCFragment *DF,
0104 MCValue &Target, const MCSubtargetInfo *STI,
0105 uint64_t &Value, bool &WasForced) const;
0106
0107
0108
0109 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF) const;
0110
0111
0112 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF) const;
0113
0114
0115
0116 bool layoutOnce();
0117
0118
0119
0120 bool relaxFragment(MCFragment &F);
0121 bool relaxInstruction(MCRelaxableFragment &IF);
0122 bool relaxLEB(MCLEBFragment &IF);
0123 bool relaxBoundaryAlign(MCBoundaryAlignFragment &BF);
0124 bool relaxDwarfLineAddr(MCDwarfLineAddrFragment &DF);
0125 bool relaxDwarfCallFrameFragment(MCDwarfCallFrameFragment &DF);
0126 bool relaxCVInlineLineTable(MCCVInlineLineTableFragment &DF);
0127 bool relaxCVDefRange(MCCVDefRangeFragment &DF);
0128 bool relaxPseudoProbeAddr(MCPseudoProbeAddrFragment &DF);
0129
0130 std::tuple<MCValue, uint64_t, bool>
0131 handleFixup(MCFragment &F, const MCFixup &Fixup, const MCSubtargetInfo *STI);
0132
0133 public:
0134
0135
0136
0137
0138
0139
0140 MCAssembler(MCContext &Context, std::unique_ptr<MCAsmBackend> Backend,
0141 std::unique_ptr<MCCodeEmitter> Emitter,
0142 std::unique_ptr<MCObjectWriter> Writer);
0143 MCAssembler(const MCAssembler &) = delete;
0144 MCAssembler &operator=(const MCAssembler &) = delete;
0145
0146
0147 uint64_t computeFragmentSize(const MCFragment &F) const;
0148
0149 void layoutBundle(MCFragment *Prev, MCFragment *F) const;
0150 void ensureValid(MCSection &Sec) const;
0151
0152
0153 uint64_t getFragmentOffset(const MCFragment &F) const;
0154
0155 uint64_t getSectionAddressSize(const MCSection &Sec) const;
0156 uint64_t getSectionFileSize(const MCSection &Sec) const;
0157
0158
0159
0160
0161 bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const;
0162
0163
0164 uint64_t getSymbolOffset(const MCSymbol &S) const;
0165
0166
0167 const MCSymbol *getBaseSymbol(const MCSymbol &Symbol) const;
0168
0169
0170 void writeSectionData(raw_ostream &OS, const MCSection *Section) const;
0171
0172
0173 bool isThumbFunc(const MCSymbol *Func) const;
0174
0175
0176 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
0177
0178
0179
0180 void reset();
0181
0182 MCContext &getContext() const { return Context; }
0183
0184 MCAsmBackend *getBackendPtr() const { return Backend.get(); }
0185
0186 MCCodeEmitter *getEmitterPtr() const { return Emitter.get(); }
0187
0188 MCAsmBackend &getBackend() const { return *Backend; }
0189
0190 MCCodeEmitter &getEmitter() const { return *Emitter; }
0191
0192 MCObjectWriter &getWriter() const { return *Writer; }
0193
0194 MCDwarfLineTableParams getDWARFLinetableParams() const { return LTParams; }
0195
0196
0197
0198
0199 void Finish();
0200
0201
0202 void layout();
0203
0204 bool hasLayout() const { return HasLayout; }
0205 bool getRelaxAll() const { return RelaxAll; }
0206 void setRelaxAll(bool Value) { RelaxAll = Value; }
0207
0208 bool isBundlingEnabled() const { return BundleAlignSize != 0; }
0209
0210 unsigned getBundleAlignSize() const { return BundleAlignSize; }
0211
0212 void setBundleAlignSize(unsigned Size) {
0213 assert((Size == 0 || !(Size & (Size - 1))) &&
0214 "Expect a power-of-two bundle align size");
0215 BundleAlignSize = Size;
0216 }
0217
0218 const_iterator begin() const { return Sections.begin(); }
0219 const_iterator end() const { return Sections.end(); }
0220
0221 SmallVectorImpl<const MCSymbol *> &getSymbols() { return Symbols; }
0222 iterator_range<pointee_iterator<
0223 typename SmallVector<const MCSymbol *, 0>::const_iterator>>
0224 symbols() const {
0225 return make_pointee_range(Symbols);
0226 }
0227
0228 bool registerSection(MCSection &Section);
0229 bool registerSymbol(const MCSymbol &Symbol);
0230
0231
0232
0233 void writeFragmentPadding(raw_ostream &OS, const MCEncodedFragment &F,
0234 uint64_t FSize) const;
0235
0236 void dump() const;
0237 };
0238
0239 }
0240
0241 #endif