[Alignment][NFC] Use Align with TargetLowering::setMinFunctionAlignment
[llvm-core.git] / include / llvm / MC / MCAssembler.h
blob4543018901a40b4d6b2d7df65e1bb5a25d889bad
1 //===- MCAssembler.h - Object File Generation -------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_MC_MCASSEMBLER_H
10 #define LLVM_MC_MCASSEMBLER_H
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/iterator.h"
17 #include "llvm/ADT/iterator_range.h"
18 #include "llvm/BinaryFormat/MachO.h"
19 #include "llvm/MC/MCDirectives.h"
20 #include "llvm/MC/MCDwarf.h"
21 #include "llvm/MC/MCFixup.h"
22 #include "llvm/MC/MCFragment.h"
23 #include "llvm/MC/MCLinkerOptimizationHint.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/Support/VersionTuple.h"
26 #include <cassert>
27 #include <cstddef>
28 #include <cstdint>
29 #include <string>
30 #include <utility>
31 #include <vector>
33 namespace llvm {
35 class MCAsmBackend;
36 class MCAsmLayout;
37 class MCContext;
38 class MCCodeEmitter;
39 class MCFragment;
40 class MCObjectWriter;
41 class MCSection;
42 class MCValue;
44 // FIXME: This really doesn't belong here. See comments below.
45 struct IndirectSymbolData {
46 MCSymbol *Symbol;
47 MCSection *Section;
50 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
51 // to one another.
52 struct DataRegionData {
53 // This enum should be kept in sync w/ the mach-o definition in
54 // llvm/Object/MachOFormat.h.
55 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
56 MCSymbol *Start;
57 MCSymbol *End;
60 class MCAssembler {
61 friend class MCAsmLayout;
63 public:
64 using SectionListType = std::vector<MCSection *>;
65 using SymbolDataListType = std::vector<const MCSymbol *>;
67 using const_iterator = pointee_iterator<SectionListType::const_iterator>;
68 using iterator = pointee_iterator<SectionListType::iterator>;
70 using const_symbol_iterator =
71 pointee_iterator<SymbolDataListType::const_iterator>;
72 using symbol_iterator = pointee_iterator<SymbolDataListType::iterator>;
74 using symbol_range = iterator_range<symbol_iterator>;
75 using const_symbol_range = iterator_range<const_symbol_iterator>;
77 using const_indirect_symbol_iterator =
78 std::vector<IndirectSymbolData>::const_iterator;
79 using indirect_symbol_iterator = std::vector<IndirectSymbolData>::iterator;
81 using const_data_region_iterator =
82 std::vector<DataRegionData>::const_iterator;
83 using data_region_iterator = std::vector<DataRegionData>::iterator;
85 /// MachO specific deployment target version info.
86 // A Major version of 0 indicates that no version information was supplied
87 // and so the corresponding load command should not be emitted.
88 using VersionInfoType = struct {
89 bool EmitBuildVersion;
90 union {
91 MCVersionMinType Type; ///< Used when EmitBuildVersion==false.
92 MachO::PlatformType Platform; ///< Used when EmitBuildVersion==true.
93 } TypeOrPlatform;
94 unsigned Major;
95 unsigned Minor;
96 unsigned Update;
97 /// An optional version of the SDK that was used to build the source.
98 VersionTuple SDKVersion;
101 private:
102 MCContext &Context;
104 std::unique_ptr<MCAsmBackend> Backend;
106 std::unique_ptr<MCCodeEmitter> Emitter;
108 std::unique_ptr<MCObjectWriter> Writer;
110 SectionListType Sections;
112 SymbolDataListType Symbols;
114 std::vector<IndirectSymbolData> IndirectSymbols;
116 std::vector<DataRegionData> DataRegions;
118 /// The list of linker options to propagate into the object file.
119 std::vector<std::vector<std::string>> LinkerOptions;
121 /// List of declared file names
122 std::vector<std::string> FileNames;
124 MCDwarfLineTableParams LTParams;
126 /// The set of function symbols for which a .thumb_func directive has
127 /// been seen.
129 // FIXME: We really would like this in target specific code rather than
130 // here. Maybe when the relocation stuff moves to target specific,
131 // this can go with it? The streamer would need some target specific
132 // refactoring too.
133 mutable SmallPtrSet<const MCSymbol *, 32> ThumbFuncs;
135 /// The bundle alignment size currently set in the assembler.
137 /// By default it's 0, which means bundling is disabled.
138 unsigned BundleAlignSize;
140 bool RelaxAll : 1;
141 bool SubsectionsViaSymbols : 1;
142 bool IncrementalLinkerCompatible : 1;
144 /// ELF specific e_header flags
145 // It would be good if there were an MCELFAssembler class to hold this.
146 // ELF header flags are used both by the integrated and standalone assemblers.
147 // Access to the flags is necessary in cases where assembler directives affect
148 // which flags to be set.
149 unsigned ELFHeaderEFlags;
151 /// Used to communicate Linker Optimization Hint information between
152 /// the Streamer and the .o writer
153 MCLOHContainer LOHContainer;
155 VersionInfoType VersionInfo;
157 /// Evaluate a fixup to a relocatable expression and the value which should be
158 /// placed into the fixup.
160 /// \param Layout The layout to use for evaluation.
161 /// \param Fixup The fixup to evaluate.
162 /// \param DF The fragment the fixup is inside.
163 /// \param Target [out] On return, the relocatable expression the fixup
164 /// evaluates to.
165 /// \param Value [out] On return, the value of the fixup as currently laid
166 /// out.
167 /// \param WasForced [out] On return, the value in the fixup is set to the
168 /// correct value if WasForced is true, even if evaluateFixup returns false.
169 /// \return Whether the fixup value was fully resolved. This is true if the
170 /// \p Value result is fixed, otherwise the value may change due to
171 /// relocation.
172 bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
173 const MCFragment *DF, MCValue &Target,
174 uint64_t &Value, bool &WasForced) const;
176 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
177 /// (increased in size, in order to hold its value correctly).
178 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
179 const MCAsmLayout &Layout) const;
181 /// Check whether the given fragment needs relaxation.
182 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
183 const MCAsmLayout &Layout) const;
185 /// Perform one layout iteration and return true if any offsets
186 /// were adjusted.
187 bool layoutOnce(MCAsmLayout &Layout);
189 /// Perform one layout iteration of the given section and return true
190 /// if any offsets were adjusted.
191 bool layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec);
193 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
195 bool relaxPaddingFragment(MCAsmLayout &Layout, MCPaddingFragment &PF);
197 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
199 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
200 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
201 MCDwarfCallFrameFragment &DF);
202 bool relaxCVInlineLineTable(MCAsmLayout &Layout,
203 MCCVInlineLineTableFragment &DF);
204 bool relaxCVDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &DF);
206 /// finishLayout - Finalize a layout, including fragment lowering.
207 void finishLayout(MCAsmLayout &Layout);
209 std::tuple<MCValue, uint64_t, bool>
210 handleFixup(const MCAsmLayout &Layout, MCFragment &F, const MCFixup &Fixup);
212 public:
213 std::vector<std::pair<StringRef, const MCSymbol *>> Symvers;
215 /// Construct a new assembler instance.
217 // FIXME: How are we going to parameterize this? Two obvious options are stay
218 // concrete and require clients to pass in a target like object. The other
219 // option is to make this abstract, and have targets provide concrete
220 // implementations as we do with AsmParser.
221 MCAssembler(MCContext &Context, std::unique_ptr<MCAsmBackend> Backend,
222 std::unique_ptr<MCCodeEmitter> Emitter,
223 std::unique_ptr<MCObjectWriter> Writer);
224 MCAssembler(const MCAssembler &) = delete;
225 MCAssembler &operator=(const MCAssembler &) = delete;
226 ~MCAssembler();
228 /// Compute the effective fragment size assuming it is laid out at the given
229 /// \p SectionAddress and \p FragmentOffset.
230 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
231 const MCFragment &F) const;
233 /// Find the symbol which defines the atom containing the given symbol, or
234 /// null if there is no such symbol.
235 const MCSymbol *getAtom(const MCSymbol &S) const;
237 /// Check whether a particular symbol is visible to the linker and is required
238 /// in the symbol table, or whether it can be discarded by the assembler. This
239 /// also effects whether the assembler treats the label as potentially
240 /// defining a separate atom.
241 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
243 /// Emit the section contents to \p OS.
244 void writeSectionData(raw_ostream &OS, const MCSection *Section,
245 const MCAsmLayout &Layout) const;
247 /// Check whether a given symbol has been flagged with .thumb_func.
248 bool isThumbFunc(const MCSymbol *Func) const;
250 /// Flag a function symbol as the target of a .thumb_func directive.
251 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
253 /// ELF e_header flags
254 unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
255 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
257 /// MachO deployment target version information.
258 const VersionInfoType &getVersionInfo() const { return VersionInfo; }
259 void setVersionMin(MCVersionMinType Type, unsigned Major, unsigned Minor,
260 unsigned Update,
261 VersionTuple SDKVersion = VersionTuple()) {
262 VersionInfo.EmitBuildVersion = false;
263 VersionInfo.TypeOrPlatform.Type = Type;
264 VersionInfo.Major = Major;
265 VersionInfo.Minor = Minor;
266 VersionInfo.Update = Update;
267 VersionInfo.SDKVersion = SDKVersion;
269 void setBuildVersion(MachO::PlatformType Platform, unsigned Major,
270 unsigned Minor, unsigned Update,
271 VersionTuple SDKVersion = VersionTuple()) {
272 VersionInfo.EmitBuildVersion = true;
273 VersionInfo.TypeOrPlatform.Platform = Platform;
274 VersionInfo.Major = Major;
275 VersionInfo.Minor = Minor;
276 VersionInfo.Update = Update;
277 VersionInfo.SDKVersion = SDKVersion;
280 /// Reuse an assembler instance
282 void reset();
284 MCContext &getContext() const { return Context; }
286 MCAsmBackend *getBackendPtr() const { return Backend.get(); }
288 MCCodeEmitter *getEmitterPtr() const { return Emitter.get(); }
290 MCObjectWriter *getWriterPtr() const { return Writer.get(); }
292 MCAsmBackend &getBackend() const { return *Backend; }
294 MCCodeEmitter &getEmitter() const { return *Emitter; }
296 MCObjectWriter &getWriter() const { return *Writer; }
298 MCDwarfLineTableParams getDWARFLinetableParams() const { return LTParams; }
299 void setDWARFLinetableParams(MCDwarfLineTableParams P) { LTParams = P; }
301 /// Finish - Do final processing and write the object to the output stream.
302 /// \p Writer is used for custom object writer (as the MCJIT does),
303 /// if not specified it is automatically created from backend.
304 void Finish();
306 // Layout all section and prepare them for emission.
307 void layout(MCAsmLayout &Layout);
309 // FIXME: This does not belong here.
310 bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
311 void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
313 bool isIncrementalLinkerCompatible() const {
314 return IncrementalLinkerCompatible;
316 void setIncrementalLinkerCompatible(bool Value) {
317 IncrementalLinkerCompatible = Value;
320 bool getRelaxAll() const { return RelaxAll; }
321 void setRelaxAll(bool Value) { RelaxAll = Value; }
323 bool isBundlingEnabled() const { return BundleAlignSize != 0; }
325 unsigned getBundleAlignSize() const { return BundleAlignSize; }
327 void setBundleAlignSize(unsigned Size) {
328 assert((Size == 0 || !(Size & (Size - 1))) &&
329 "Expect a power-of-two bundle align size");
330 BundleAlignSize = Size;
333 /// \name Section List Access
334 /// @{
336 iterator begin() { return Sections.begin(); }
337 const_iterator begin() const { return Sections.begin(); }
339 iterator end() { return Sections.end(); }
340 const_iterator end() const { return Sections.end(); }
342 size_t size() const { return Sections.size(); }
344 /// @}
345 /// \name Symbol List Access
346 /// @{
347 symbol_iterator symbol_begin() { return Symbols.begin(); }
348 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
350 symbol_iterator symbol_end() { return Symbols.end(); }
351 const_symbol_iterator symbol_end() const { return Symbols.end(); }
353 symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
354 const_symbol_range symbols() const {
355 return make_range(symbol_begin(), symbol_end());
358 size_t symbol_size() const { return Symbols.size(); }
360 /// @}
361 /// \name Indirect Symbol List Access
362 /// @{
364 // FIXME: This is a total hack, this should not be here. Once things are
365 // factored so that the streamer has direct access to the .o writer, it can
366 // disappear.
367 std::vector<IndirectSymbolData> &getIndirectSymbols() {
368 return IndirectSymbols;
371 indirect_symbol_iterator indirect_symbol_begin() {
372 return IndirectSymbols.begin();
374 const_indirect_symbol_iterator indirect_symbol_begin() const {
375 return IndirectSymbols.begin();
378 indirect_symbol_iterator indirect_symbol_end() {
379 return IndirectSymbols.end();
381 const_indirect_symbol_iterator indirect_symbol_end() const {
382 return IndirectSymbols.end();
385 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
387 /// @}
388 /// \name Linker Option List Access
389 /// @{
391 std::vector<std::vector<std::string>> &getLinkerOptions() {
392 return LinkerOptions;
395 /// @}
396 /// \name Data Region List Access
397 /// @{
399 // FIXME: This is a total hack, this should not be here. Once things are
400 // factored so that the streamer has direct access to the .o writer, it can
401 // disappear.
402 std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
404 data_region_iterator data_region_begin() { return DataRegions.begin(); }
405 const_data_region_iterator data_region_begin() const {
406 return DataRegions.begin();
409 data_region_iterator data_region_end() { return DataRegions.end(); }
410 const_data_region_iterator data_region_end() const {
411 return DataRegions.end();
414 size_t data_region_size() const { return DataRegions.size(); }
416 /// @}
417 /// \name Data Region List Access
418 /// @{
420 // FIXME: This is a total hack, this should not be here. Once things are
421 // factored so that the streamer has direct access to the .o writer, it can
422 // disappear.
423 MCLOHContainer &getLOHContainer() { return LOHContainer; }
424 const MCLOHContainer &getLOHContainer() const {
425 return const_cast<MCAssembler *>(this)->getLOHContainer();
428 struct CGProfileEntry {
429 const MCSymbolRefExpr *From;
430 const MCSymbolRefExpr *To;
431 uint64_t Count;
433 std::vector<CGProfileEntry> CGProfile;
434 /// @}
435 /// \name Backend Data Access
436 /// @{
438 bool registerSection(MCSection &Section);
440 void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr);
442 ArrayRef<std::string> getFileNames() { return FileNames; }
444 void addFileName(StringRef FileName) {
445 if (!is_contained(FileNames, FileName))
446 FileNames.push_back(FileName);
449 /// Write the necessary bundle padding to \p OS.
450 /// Expects a fragment \p F containing instructions and its size \p FSize.
451 void writeFragmentPadding(raw_ostream &OS, const MCEncodedFragment &F,
452 uint64_t FSize) const;
454 /// @}
456 void dump() const;
459 /// Compute the amount of padding required before the fragment \p F to
460 /// obey bundling restrictions, where \p FOffset is the fragment's offset in
461 /// its section and \p FSize is the fragment's size.
462 uint64_t computeBundlePadding(const MCAssembler &Assembler,
463 const MCEncodedFragment *F, uint64_t FOffset,
464 uint64_t FSize);
466 } // end namespace llvm
468 #endif // LLVM_MC_MCASSEMBLER_H