Debian Codegraph @

suite/synctools/tablegen/include/llvm/CodeGen/AsmPrinter.h

raw symbols SCIP
1 //===- llvm/CodeGen/AsmPrinter.h - AsmPrinter Framework ---------*- 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 //===----------------------------------------------------------------------===//
8 //
9 // This file contains a class to be used as the base class for target specific
10 // asm writers. This class primarily handles common functionality used by
11 // all asm writers.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CODEGEN_ASMPRINTER_H
16 #define LLVM_CODEGEN_ASMPRINTER_H
17
18 #include "llvm/ADT/MapVector.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/BinaryFormat/Dwarf.h"
21 #include "llvm/CodeGen/AsmPrinterHandler.h"
22 #include "llvm/CodeGen/DwarfStringPoolEntry.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/IR/InlineAsm.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/SourceMgr.h"
28 #include <cstdint>
29 #include <memory>
30 #include <utility>
31 #include <vector>
32
33 namespace llvm {
34
35 class BasicBlock;
36 class BlockAddress;
37 class Constant;
38 class ConstantArray;
39 class DataLayout;
40 class DIE;
41 class DIEAbbrev;
42 class DwarfDebug;
43 class GCMetadataPrinter;
44 class GCStrategy;
45 class GlobalObject;
46 class GlobalValue;
47 class GlobalVariable;
48 class MachineBasicBlock;
49 class MachineConstantPoolValue;
50 class MachineDominatorTree;
51 class MachineFunction;
52 class MachineInstr;
53 class MachineJumpTableInfo;
54 class MachineLoopInfo;
55 class MachineModuleInfo;
56 class MachineOptimizationRemarkEmitter;
57 class MCAsmInfo;
58 class MCCFIInstruction;
59 class MCContext;
60 class MCExpr;
61 class MCInst;
62 class MCSection;
63 class MCStreamer;
64 class MCSubtargetInfo;
65 class MCSymbol;
66 class MCTargetOptions;
67 class MDNode;
68 class Module;
69 class PseudoProbeHandler;
70 class raw_ostream;
71 class StackMaps;
72 class StringRef;
73 class TargetLoweringObjectFile;
74 class TargetMachine;
75 class Twine;
76
77 namespace remarks {
78 class RemarkStreamer;
79 }
80
81 /// This class is intended to be used as a driving class for all asm writers.
82 class AsmPrinter : public MachineFunctionPass {
83 public:
84 /// Target machine description.
85 TargetMachine &TM;
86
87 /// Target Asm Printer information.
88 const MCAsmInfo *MAI;
89
90 /// This is the context for the output file that we are streaming. This owns
91 /// all of the global MC-related objects for the generated translation unit.
92 MCContext &OutContext;
93
94 /// This is the MCStreamer object for the file we are generating. This
95 /// contains the transient state for the current translation unit that we are
96 /// generating (such as the current section etc).
97 std::unique_ptr<MCStreamer> OutStreamer;
98
99 /// The current machine function.
100 MachineFunction *MF = nullptr;
101
102 /// This is a pointer to the current MachineModuleInfo.
103 MachineModuleInfo *MMI = nullptr;
104
105 /// This is a pointer to the current MachineDominatorTree.
106 MachineDominatorTree *MDT = nullptr;
107
108 /// This is a pointer to the current MachineLoopInfo.
109 MachineLoopInfo *MLI = nullptr;
110
111 /// Optimization remark emitter.
112 MachineOptimizationRemarkEmitter *ORE;
113
114 /// The symbol for the entry in __patchable_function_entires.
115 MCSymbol *CurrentPatchableFunctionEntrySym = nullptr;
116
117 /// The symbol for the current function. This is recalculated at the beginning
118 /// of each call to runOnMachineFunction().
119 MCSymbol *CurrentFnSym = nullptr;
120
121 /// The symbol for the current function descriptor on AIX. This is created
122 /// at the beginning of each call to SetupMachineFunction().
123 MCSymbol *CurrentFnDescSym = nullptr;
124
125 /// The symbol used to represent the start of the current function for the
126 /// purpose of calculating its size (e.g. using the .size directive). By
127 /// default, this is equal to CurrentFnSym.
128 MCSymbol *CurrentFnSymForSize = nullptr;
129
130 /// Map a basic block section ID to the begin and end symbols of that section
131 /// which determine the section's range.
132 struct MBBSectionRange {
133 MCSymbol *BeginLabel, *EndLabel;
134 };
135
136 MapVector<unsigned, MBBSectionRange> MBBSectionRanges;
137
138 /// Map global GOT equivalent MCSymbols to GlobalVariables and keep track of
139 /// its number of uses by other globals.
140 using GOTEquivUsePair = std::pair<const GlobalVariable *, unsigned>;
141 MapVector<const MCSymbol *, GOTEquivUsePair> GlobalGOTEquivs;
142
143 /// struct HandlerInfo and Handlers permit users or target extended
144 /// AsmPrinter to add their own handlers.
145 struct HandlerInfo {
146 std::unique_ptr<AsmPrinterHandler> Handler;
147 StringRef TimerName;
148 StringRef TimerDescription;
149 StringRef TimerGroupName;
150 StringRef TimerGroupDescription;
151
152 HandlerInfo(std::unique_ptr<AsmPrinterHandler> Handler, StringRef TimerName,
153 StringRef TimerDescription, StringRef TimerGroupName,
154 StringRef TimerGroupDescription)
155 : Handler(std::move(Handler)), TimerName(TimerName),
156 TimerDescription(TimerDescription), TimerGroupName(TimerGroupName),
157 TimerGroupDescription(TimerGroupDescription) {}
158 };
159
160 // Flags representing which CFI section is required for a function/module.
161 enum class CFISection : unsigned {
162 None = 0, ///< Do not emit either .eh_frame or .debug_frame
163 EH = 1, ///< Emit .eh_frame
164 Debug = 2 ///< Emit .debug_frame
165 };
166
167 private:
168 MCSymbol *CurrentFnEnd = nullptr;
169
170 /// Map a basic block section ID to the exception symbol associated with that
171 /// section. Map entries are assigned and looked up via
172 /// AsmPrinter::getMBBExceptionSym.
173 DenseMap<unsigned, MCSymbol *> MBBSectionExceptionSyms;
174
175 // The symbol used to represent the start of the current BB section of the
176 // function. This is used to calculate the size of the BB section.
177 MCSymbol *CurrentSectionBeginSym = nullptr;
178
179 // The garbage collection metadata printer table.
180 void *GCMetadataPrinters = nullptr; // Really a DenseMap.
181
182 /// Emit comments in assembly output if this is true.
183 bool VerboseAsm;
184
185 /// Output stream for the stack usage file (i.e., .su file).
186 std::unique_ptr<raw_fd_ostream> StackUsageStream;
187
188 static char ID;
189
190 protected:
191 MCSymbol *CurrentFnBegin = nullptr;
192
193 /// A vector of all debug/EH info emitters we should use. This vector
194 /// maintains ownership of the emitters.
195 std::vector<HandlerInfo> Handlers;
196 size_t NumUserHandlers = 0;
197
198 private:
199 /// If generated on the fly this own the instance.
200 std::unique_ptr<MachineDominatorTree> OwnedMDT;
201
202 /// If generated on the fly this own the instance.
203 std::unique_ptr<MachineLoopInfo> OwnedMLI;
204
205 /// If the target supports dwarf debug info, this pointer is non-null.
206 DwarfDebug *DD = nullptr;
207
208 /// A handler that supports pseudo probe emission with embedded inline
209 /// context.
210 PseudoProbeHandler *PP = nullptr;
211
212 /// CFISection type the module needs i.e. either .eh_frame or .debug_frame.
213 CFISection ModuleCFISection = CFISection::None;
214
215 protected:
216 explicit AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
217
218 public:
219 ~AsmPrinter() override;
220
221 DwarfDebug *getDwarfDebug() { return DD; }
222 DwarfDebug *getDwarfDebug() const { return DD; }
223
224 uint16_t getDwarfVersion() const;
225 void setDwarfVersion(uint16_t Version);
226
227 bool isDwarf64() const;
228
229 /// Returns 4 for DWARF32 and 8 for DWARF64.
230 unsigned int getDwarfOffsetByteSize() const;
231
232 /// Returns 4 for DWARF32 and 12 for DWARF64.
233 unsigned int getUnitLengthFieldByteSize() const;
234
235 /// Returns information about the byte size of DW_FORM values.
236 dwarf::FormParams getDwarfFormParams() const;
237
238 bool isPositionIndependent() const;
239
240 /// Return true if assembly output should contain comments.
241 bool isVerbose() const { return VerboseAsm; }
242
243 /// Return a unique ID for the current function.
244 unsigned getFunctionNumber() const;
245
246 /// Return symbol for the function pseudo stack if the stack frame is not a
247 /// register based.
248 virtual const MCSymbol *getFunctionFrameSymbol() const { return nullptr; }
249
250 MCSymbol *getFunctionBegin() const { return CurrentFnBegin; }
251 MCSymbol *getFunctionEnd() const { return CurrentFnEnd; }
252
253 // Return the exception symbol associated with the MBB section containing a
254 // given basic block.
255 MCSymbol *getMBBExceptionSym(const MachineBasicBlock &MBB);
256
257 /// Return information about object file lowering.
258 const TargetLoweringObjectFile &getObjFileLowering() const;
259
260 /// Return information about data layout.
261 const DataLayout &getDataLayout() const;
262
263 /// Return the pointer size from the TargetMachine
264 unsigned getPointerSize() const;
265
266 /// Return information about subtarget.
267 const MCSubtargetInfo &getSubtargetInfo() const;
268
269 void EmitToStreamer(MCStreamer &S, const MCInst &Inst);
270
271 /// Emits inital debug location directive.
272 void emitInitialRawDwarfLocDirective(const MachineFunction &MF);
273
274 /// Return the current section we are emitting to.
275 const MCSection *getCurrentSection() const;
276
277 void getNameWithPrefix(SmallVectorImpl<char> &Name,
278 const GlobalValue *GV) const;
279
280 MCSymbol *getSymbol(const GlobalValue *GV) const;
281
282 /// Similar to getSymbol() but preferred for references. On ELF, this uses a
283 /// local symbol if a reference to GV is guaranteed to be resolved to the
284 /// definition in the same module.
285 MCSymbol *getSymbolPreferLocal(const GlobalValue &GV) const;
286
287 //===------------------------------------------------------------------===//
288 // XRay instrumentation implementation.
289 //===------------------------------------------------------------------===//
290 public:
291 // This describes the kind of sled we're storing in the XRay table.
292 enum class SledKind : uint8_t {
293 FUNCTION_ENTER = 0,
294 FUNCTION_EXIT = 1,
295 TAIL_CALL = 2,
296 LOG_ARGS_ENTER = 3,
297 CUSTOM_EVENT = 4,
298 TYPED_EVENT = 5,
299 };
300
301 // The table will contain these structs that point to the sled, the function
302 // containing the sled, and what kind of sled (and whether they should always
303 // be instrumented). We also use a version identifier that the runtime can use
304 // to decide what to do with the sled, depending on the version of the sled.
305 struct XRayFunctionEntry {
306 const MCSymbol *Sled;
307 const MCSymbol *Function;
308 SledKind Kind;
309 bool AlwaysInstrument;
310 const class Function *Fn;
311 uint8_t Version;
312
313 void emit(int, MCStreamer *) const;
314 };
315
316 // All the sleds to be emitted.
317 SmallVector<XRayFunctionEntry, 4> Sleds;
318
319 // Helper function to record a given XRay sled.
320 void recordSled(MCSymbol *Sled, const MachineInstr &MI, SledKind Kind,
321 uint8_t Version = 0);
322
323 /// Emit a table with all XRay instrumentation points.
324 void emitXRayTable();
325
326 void emitPatchableFunctionEntries();
327
328 //===------------------------------------------------------------------===//
329 // MachineFunctionPass Implementation.
330 //===------------------------------------------------------------------===//
331
332 /// Record analysis usage.
333 void getAnalysisUsage(AnalysisUsage &AU) const override;
334
335 /// Set up the AsmPrinter when we are working on a new module. If your pass
336 /// overrides this, it must make sure to explicitly call this implementation.
337 bool doInitialization(Module &M) override;
338
339 /// Shut down the asmprinter. If you override this in your pass, you must make
340 /// sure to call it explicitly.
341 bool doFinalization(Module &M) override;
342
343 /// Emit the specified function out to the OutStreamer.
344 bool runOnMachineFunction(MachineFunction &MF) override {
345 SetupMachineFunction(MF);
346 emitFunctionBody();
347 return false;
348 }
349
350 //===------------------------------------------------------------------===//
351 // Coarse grained IR lowering routines.
352 //===------------------------------------------------------------------===//
353
354 /// This should be called when a new MachineFunction is being processed from
355 /// runOnMachineFunction.
356 virtual void SetupMachineFunction(MachineFunction &MF);
357
358 /// This method emits the body and trailer for a function.
359 void emitFunctionBody();
360
361 void emitCFIInstruction(const MachineInstr &MI);
362
363 void emitFrameAlloc(const MachineInstr &MI);
364
365 void emitStackSizeSection(const MachineFunction &MF);
366
367 void emitStackUsage(const MachineFunction &MF);
368
369 void emitBBAddrMapSection(const MachineFunction &MF);
370
371 void emitPseudoProbe(const MachineInstr &MI);
372
373 void emitRemarksSection(remarks::RemarkStreamer &RS);
374
375 /// Get the CFISection type for a function.
376 CFISection getFunctionCFISectionType(const Function &F) const;
377
378 /// Get the CFISection type for a function.
379 CFISection getFunctionCFISectionType(const MachineFunction &MF) const;
380
381 /// Get the CFISection type for the module.
382 CFISection getModuleCFISectionType() const { return ModuleCFISection; }
383
384 bool needsSEHMoves();
385
386 /// Since emitting CFI unwind information is entangled with supporting the
387 /// exceptions, this returns true for platforms which use CFI unwind
388 /// information for debugging purpose when
389 /// `MCAsmInfo::ExceptionsType == ExceptionHandling::None`.
390 bool needsCFIForDebug() const;
391
392 /// Print to the current output stream assembly representations of the
393 /// constants in the constant pool MCP. This is used to print out constants
394 /// which have been "spilled to memory" by the code generator.
395 virtual void emitConstantPool();
396
397 /// Print assembly representations of the jump tables used by the current
398 /// function to the current output stream.
399 virtual void emitJumpTableInfo();
400
401 /// Emit the specified global variable to the .s file.
402 virtual void emitGlobalVariable(const GlobalVariable *GV);
403
404 /// Check to see if the specified global is a special global used by LLVM. If
405 /// so, emit it and return true, otherwise do nothing and return false.
406 bool emitSpecialLLVMGlobal(const GlobalVariable *GV);
407
408 /// `llvm.global_ctors` and `llvm.global_dtors` are arrays of Structor
409 /// structs.
410 ///
411 /// Priority - init priority
412 /// Func - global initialization or global clean-up function
413 /// ComdatKey - associated data
414 struct Structor {
415 int Priority = 0;
416 Constant *Func = nullptr;
417 GlobalValue *ComdatKey = nullptr;
418
419 Structor() = default;
420 };
421
422 /// This method gathers an array of Structors and then sorts them out by
423 /// Priority.
424 /// @param List The initializer of `llvm.global_ctors` or `llvm.global_dtors`
425 /// array.
426 /// @param[out] Structors Sorted Structor structs by Priority.
427 void preprocessXXStructorList(const DataLayout &DL, const Constant *List,
428 SmallVector<Structor, 8> &Structors);
429
430 /// This method emits `llvm.global_ctors` or `llvm.global_dtors` list.
431 virtual void emitXXStructorList(const DataLayout &DL, const Constant *List,
432 bool IsCtor);
433
434 /// Emit an alignment directive to the specified power of two boundary. If a
435 /// global value is specified, and if that global has an explicit alignment
436 /// requested, it will override the alignment request if required for
437 /// correctness.
438 void emitAlignment(Align Alignment, const GlobalObject *GV = nullptr,
439 unsigned MaxBytesToEmit = 0) const;
440
441 /// Lower the specified LLVM Constant to an MCExpr.
442 virtual const MCExpr *lowerConstant(const Constant *CV);
443
444 /// Print a general LLVM constant to the .s file.
445 void emitGlobalConstant(const DataLayout &DL, const Constant *CV);
446
447 /// Unnamed constant global variables solely contaning a pointer to
448 /// another globals variable act like a global variable "proxy", or GOT
449 /// equivalents, i.e., it's only used to hold the address of the latter. One
450 /// optimization is to replace accesses to these proxies by using the GOT
451 /// entry for the final global instead. Hence, we select GOT equivalent
452 /// candidates among all the module global variables, avoid emitting them
453 /// unnecessarily and finally replace references to them by pc relative
454 /// accesses to GOT entries.
455 void computeGlobalGOTEquivs(Module &M);
456
457 /// Constant expressions using GOT equivalent globals may not be
458 /// eligible for PC relative GOT entry conversion, in such cases we need to
459 /// emit the proxies we previously omitted in EmitGlobalVariable.
460 void emitGlobalGOTEquivs();
461
462 /// Emit the stack maps.
463 void emitStackMaps(StackMaps &SM);
464
465 //===------------------------------------------------------------------===//
466 // Overridable Hooks
467 //===------------------------------------------------------------------===//
468
469 void addAsmPrinterHandler(HandlerInfo Handler) {
470 Handlers.insert(Handlers.begin(), std::move(Handler));
471 NumUserHandlers++;
472 }
473
474 // Targets can, or in the case of EmitInstruction, must implement these to
475 // customize output.
476
477 /// This virtual method can be overridden by targets that want to emit
478 /// something at the start of their file.
479 virtual void emitStartOfAsmFile(Module &) {}
480
481 /// This virtual method can be overridden by targets that want to emit
482 /// something at the end of their file.
483 virtual void emitEndOfAsmFile(Module &) {}
484
485 /// Targets can override this to emit stuff before the first basic block in
486 /// the function.
487 virtual void emitFunctionBodyStart() {}
488
489 /// Targets can override this to emit stuff after the last basic block in the
490 /// function.
491 virtual void emitFunctionBodyEnd() {}
492
493 /// Targets can override this to emit stuff at the start of a basic block.
494 /// By default, this method prints the label for the specified
495 /// MachineBasicBlock, an alignment (if present) and a comment describing it
496 /// if appropriate.
497 virtual void emitBasicBlockStart(const MachineBasicBlock &MBB);
498
499 /// Targets can override this to emit stuff at the end of a basic block.
500 virtual void emitBasicBlockEnd(const MachineBasicBlock &MBB);
501
502 /// Targets should implement this to emit instructions.
503 virtual void emitInstruction(const MachineInstr *) {
504 llvm_unreachable("EmitInstruction not implemented");
505 }
506
507 /// Return the symbol for the specified constant pool entry.
508 virtual MCSymbol *GetCPISymbol(unsigned CPID) const;
509
510 virtual void emitFunctionEntryLabel();
511
512 virtual void emitFunctionDescriptor() {
513 llvm_unreachable("Function descriptor is target-specific.");
514 }
515
516 virtual void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
517
518 /// Targets can override this to change how global constants that are part of
519 /// a C++ static/global constructor list are emitted.
520 virtual void emitXXStructor(const DataLayout &DL, const Constant *CV) {
521 emitGlobalConstant(DL, CV);
522 }
523
524 /// Return true if the basic block has exactly one predecessor and the control
525 /// transfer mechanism between the predecessor and this block is a
526 /// fall-through.
527 virtual bool
528 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
529
530 /// Targets can override this to customize the output of IMPLICIT_DEF
531 /// instructions in verbose mode.
532 virtual void emitImplicitDef(const MachineInstr *MI) const;
533
534 /// Emit N NOP instructions.
535 void emitNops(unsigned N);
536
537 //===------------------------------------------------------------------===//
538 // Symbol Lowering Routines.
539 //===------------------------------------------------------------------===//
540
541 MCSymbol *createTempSymbol(const Twine &Name) const;
542
543 /// Return the MCSymbol for a private symbol with global value name as its
544 /// base, with the specified suffix.
545 MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
546 StringRef Suffix) const;
547
548 /// Return the MCSymbol for the specified ExternalSymbol.
549 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
550
551 /// Return the symbol for the specified jump table entry.
552 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
553
554 /// Return the symbol for the specified jump table .set
555 /// FIXME: privatize to AsmPrinter.
556 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
557
558 /// Return the MCSymbol used to satisfy BlockAddress uses of the specified
559 /// basic block.
560 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
561 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
562
563 //===------------------------------------------------------------------===//
564 // Emission Helper Routines.
565 //===------------------------------------------------------------------===//
566
567 /// This is just convenient handler for printing offsets.
568 void printOffset(int64_t Offset, raw_ostream &OS) const;
569
570 /// Emit a byte directive and value.
571 void emitInt8(int Value) const;
572
573 /// Emit a short directive and value.
574 void emitInt16(int Value) const;
575
576 /// Emit a long directive and value.
577 void emitInt32(int Value) const;
578
579 /// Emit a long long directive and value.
580 void emitInt64(uint64_t Value) const;
581
582 /// Emit something like ".long Hi-Lo" where the size in bytes of the directive
583 /// is specified by Size and Hi/Lo specify the labels. This implicitly uses
584 /// .set if it is available.
585 void emitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
586 unsigned Size) const;
587
588 /// Emit something like ".uleb128 Hi-Lo".
589 void emitLabelDifferenceAsULEB128(const MCSymbol *Hi,
590 const MCSymbol *Lo) const;
591
592 /// Emit something like ".long Label+Offset" where the size in bytes of the
593 /// directive is specified by Size and Label specifies the label. This
594 /// implicitly uses .set if it is available.
595 void emitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
596 unsigned Size, bool IsSectionRelative = false) const;
597
598 /// Emit something like ".long Label" where the size in bytes of the directive
599 /// is specified by Size and Label specifies the label.
600 void emitLabelReference(const MCSymbol *Label, unsigned Size,
601 bool IsSectionRelative = false) const {
602 emitLabelPlusOffset(Label, 0, Size, IsSectionRelative);
603 }
604
605 //===------------------------------------------------------------------===//
606 // Dwarf Emission Helper Routines
607 //===------------------------------------------------------------------===//
608
609 /// Emit the specified signed leb128 value.
610 void emitSLEB128(int64_t Value, const char *Desc = nullptr) const;
611
612 /// Emit the specified unsigned leb128 value.
613 void emitULEB128(uint64_t Value, const char *Desc = nullptr,
614 unsigned PadTo = 0) const;
615
616 /// Emit a .byte 42 directive that corresponds to an encoding. If verbose
617 /// assembly output is enabled, we output comments describing the encoding.
618 /// Desc is a string saying what the encoding is specifying (e.g. "LSDA").
619 void emitEncodingByte(unsigned Val, const char *Desc = nullptr) const;
620
621 /// Return the size of the encoding in bytes.
622 unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
623
624 /// Emit reference to a ttype global with a specified encoding.
625 virtual void emitTTypeReference(const GlobalValue *GV, unsigned Encoding);
626
627 /// Emit a reference to a symbol for use in dwarf. Different object formats
628 /// represent this in different ways. Some use a relocation others encode
629 /// the label offset in its section.
630 void emitDwarfSymbolReference(const MCSymbol *Label,
631 bool ForceOffset = false) const;
632
633 /// Emit the 4- or 8-byte offset of a string from the start of its section.
634 ///
635 /// When possible, emit a DwarfStringPool section offset without any
636 /// relocations, and without using the symbol. Otherwise, defers to \a
637 /// emitDwarfSymbolReference().
638 ///
639 /// The length of the emitted value depends on the DWARF format.
640 void emitDwarfStringOffset(DwarfStringPoolEntry S) const;
641
642 /// Emit the 4-or 8-byte offset of a string from the start of its section.
643 void emitDwarfStringOffset(DwarfStringPoolEntryRef S) const {
644 emitDwarfStringOffset(S.getEntry());
645 }
646
647 /// Emit something like ".long Label + Offset" or ".quad Label + Offset"
648 /// depending on the DWARF format.
649 void emitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const;
650
651 /// Emit 32- or 64-bit value depending on the DWARF format.
652 void emitDwarfLengthOrOffset(uint64_t Value) const;
653
654 /// Emit a unit length field. The actual format, DWARF32 or DWARF64, is chosen
655 /// according to the settings.
656 void emitDwarfUnitLength(uint64_t Length, const Twine &Comment) const;
657
658 /// Emit a unit length field. The actual format, DWARF32 or DWARF64, is chosen
659 /// according to the settings.
660 /// Return the end symbol generated inside, the caller needs to emit it.
661 MCSymbol *emitDwarfUnitLength(const Twine &Prefix,
662 const Twine &Comment) const;
663
664 /// Emit reference to a call site with a specified encoding
665 void emitCallSiteOffset(const MCSymbol *Hi, const MCSymbol *Lo,
666 unsigned Encoding) const;
667 /// Emit an integer value corresponding to the call site encoding
668 void emitCallSiteValue(uint64_t Value, unsigned Encoding) const;
669
670 /// Get the value for DW_AT_APPLE_isa. Zero if no isa encoding specified.
671 virtual unsigned getISAEncoding() { return 0; }
672
673 /// Emit the directive and value for debug thread local expression
674 ///
675 /// \p Value - The value to emit.
676 /// \p Size - The size of the integer (in bytes) to emit.
677 virtual void emitDebugValue(const MCExpr *Value, unsigned Size) const;
678
679 //===------------------------------------------------------------------===//
680 // Dwarf Lowering Routines
681 //===------------------------------------------------------------------===//
682
683 /// Emit frame instruction to describe the layout of the frame.
684 void emitCFIInstruction(const MCCFIInstruction &Inst) const;
685
686 /// Emit Dwarf abbreviation table.
687 template <typename T> void emitDwarfAbbrevs(const T &Abbrevs) const {
688 // For each abbreviation.
689 for (const auto &Abbrev : Abbrevs)
690 emitDwarfAbbrev(*Abbrev);
691
692 // Mark end of abbreviations.
693 emitULEB128(0, "EOM(3)");
694 }
695
696 void emitDwarfAbbrev(const DIEAbbrev &Abbrev) const;
697
698 /// Recursively emit Dwarf DIE tree.
699 void emitDwarfDIE(const DIE &Die) const;
700
701 //===------------------------------------------------------------------===//
702 // Inline Asm Support
703 //===------------------------------------------------------------------===//
704
705 // These are hooks that targets can override to implement inline asm
706 // support. These should probably be moved out of AsmPrinter someday.
707
708 /// Print information related to the specified machine instr that is
709 /// independent of the operand, and may be independent of the instr itself.
710 /// This can be useful for portably encoding the comment character or other
711 /// bits of target-specific knowledge into the asmstrings. The syntax used is
712 /// ${:comment}. Targets can override this to add support for their own
713 /// strange codes.
714 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
715 StringRef Code) const;
716
717 /// Print the MachineOperand as a symbol. Targets with complex handling of
718 /// symbol references should override the base implementation.
719 virtual void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS);
720
721 /// Print the specified operand of MI, an INLINEASM instruction, using the
722 /// specified assembler variant. Targets should override this to format as
723 /// appropriate. This method can return true if the operand is erroneous.
724 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
725 const char *ExtraCode, raw_ostream &OS);
726
727 /// Print the specified operand of MI, an INLINEASM instruction, using the
728 /// specified assembler variant as an address. Targets should override this to
729 /// format as appropriate. This method can return true if the operand is
730 /// erroneous.
731 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
732 const char *ExtraCode, raw_ostream &OS);
733
734 /// Let the target do anything it needs to do before emitting inlineasm.
735 /// \p StartInfo - the subtarget info before parsing inline asm
736 virtual void emitInlineAsmStart() const;
737
738 /// Let the target do anything it needs to do after emitting inlineasm.
739 /// This callback can be used restore the original mode in case the
740 /// inlineasm contains directives to switch modes.
741 /// \p StartInfo - the original subtarget info before inline asm
742 /// \p EndInfo - the final subtarget info after parsing the inline asm,
743 /// or NULL if the value is unknown.
744 virtual void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
745 const MCSubtargetInfo *EndInfo) const;
746
747 /// This emits visibility information about symbol, if this is supported by
748 /// the target.
749 void emitVisibility(MCSymbol *Sym, unsigned Visibility,
750 bool IsDefinition = true) const;
751
752 /// This emits linkage information about \p GVSym based on \p GV, if this is
753 /// supported by the target.
754 virtual void emitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const;
755
756 /// Return the alignment for the specified \p GV.
757 static Align getGVAlignment(const GlobalObject *GV, const DataLayout &DL,
758 Align InAlign = Align(1));
759
760 private:
761 /// Private state for PrintSpecial()
762 // Assign a unique ID to this machine instruction.
763 mutable const MachineInstr *LastMI = nullptr;
764 mutable unsigned LastFn = 0;
765 mutable unsigned Counter = ~0U;
766
767 /// This method emits the header for the current function.
768 virtual void emitFunctionHeader();
769
770 /// This method emits a comment next to header for the current function.
771 virtual void emitFunctionHeaderComment();
772
773 /// Emit a blob of inline asm to the output streamer.
774 void
775 emitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
776 const MCTargetOptions &MCOptions,
777 const MDNode *LocMDNode = nullptr,
778 InlineAsm::AsmDialect AsmDialect = InlineAsm::AD_ATT) const;
779
780 /// This method formats and emits the specified machine instruction that is an
781 /// inline asm.
782 void emitInlineAsm(const MachineInstr *MI) const;
783
784 /// Add inline assembly info to the diagnostics machinery, so we can
785 /// emit file and position info. Returns SrcMgr memory buffer position.
786 unsigned addInlineAsmDiagBuffer(StringRef AsmStr,
787 const MDNode *LocMDNode) const;
788
789 //===------------------------------------------------------------------===//
790 // Internal Implementation Details
791 //===------------------------------------------------------------------===//
792
793 void emitJumpTableEntry(const MachineJumpTableInfo *MJTI,
794 const MachineBasicBlock *MBB, unsigned uid) const;
795 void emitLLVMUsedList(const ConstantArray *InitList);
796 /// Emit llvm.ident metadata in an '.ident' directive.
797 void emitModuleIdents(Module &M);
798 /// Emit bytes for llvm.commandline metadata.
799 void emitModuleCommandLines(Module &M);
800
801 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy &S);
802 void emitGlobalAlias(Module &M, const GlobalAlias &GA);
803 void emitGlobalIFunc(Module &M, const GlobalIFunc &GI);
804
805 /// This method decides whether the specified basic block requires a label.
806 bool shouldEmitLabelForBasicBlock(const MachineBasicBlock &MBB) const;
807
808 protected:
809 virtual bool shouldEmitWeakSwiftAsyncExtendedFramePointerFlags() const {
810 return false;
811 }
812 };
813
814 } // end namespace llvm
815
816 #endif // LLVM_CODEGEN_ASMPRINTER_H