1 //===-- llvm/CodeGen/AsmPrinter.h - AsmPrinter Framework --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a class to be used as the base class for target specific
11 // asm writers. This class primarily handles common functionality used by
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CODEGEN_ASMPRINTER_H
17 #define LLVM_CODEGEN_ASMPRINTER_H
19 #include "llvm/ADT/MapVector.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/DwarfStringPoolEntry.h"
23 #include "llvm/IR/InlineAsm.h"
24 #include "llvm/Support/DataTypes.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/SourceMgr.h"
29 class AsmPrinterHandler;
37 class GCMetadataPrinter;
38 class GlobalIndirectSymbol;
41 class MachineBasicBlock;
42 class MachineFunction;
44 class MachineLocation;
45 class MachineLoopInfo;
47 class MachineConstantPoolValue;
48 class MachineJumpTableInfo;
49 class MachineModuleInfo;
51 class MCCFIInstruction;
57 class MCSubtargetInfo;
59 class MCTargetOptions;
63 class TargetLoweringObjectFile;
67 /// This class is intended to be used as a driving class for all asm writers.
68 class AsmPrinter : public MachineFunctionPass {
70 /// Target machine description.
74 /// Target Asm Printer information.
78 /// This is the context for the output file that we are streaming. This owns
79 /// all of the global MC-related objects for the generated translation unit.
80 MCContext &OutContext;
82 /// This is the MCStreamer object for the file we are generating. This
83 /// contains the transient state for the current translation unit that we are
84 /// generating (such as the current section etc).
85 std::unique_ptr<MCStreamer> OutStreamer;
87 /// The current machine function.
88 const MachineFunction *MF;
90 /// This is a pointer to the current MachineModuleInfo.
91 MachineModuleInfo *MMI;
93 /// The symbol for the current function. This is recalculated at the beginning
94 /// of each call to runOnMachineFunction().
96 MCSymbol *CurrentFnSym;
98 /// The symbol used to represent the start of the current function for the
99 /// purpose of calculating its size (e.g. using the .size directive). By
100 /// default, this is equal to CurrentFnSym.
101 MCSymbol *CurrentFnSymForSize;
103 /// Map global GOT equivalent MCSymbols to GlobalVariables and keep track of
104 /// its number of uses by other globals.
105 typedef std::pair<const GlobalVariable *, unsigned> GOTEquivUsePair;
106 MapVector<const MCSymbol *, GOTEquivUsePair> GlobalGOTEquivs;
109 MCSymbol *CurrentFnBegin;
110 MCSymbol *CurrentFnEnd;
111 MCSymbol *CurExceptionSym;
113 // The garbage collection metadata printer table.
114 void *GCMetadataPrinters; // Really a DenseMap.
116 /// Emit comments in assembly output if this is true.
121 /// If VerboseAsm is set, a pointer to the loop info for this function.
125 AsmPrinterHandler *Handler;
126 const char *TimerName;
127 const char *TimerDescription;
128 const char *TimerGroupName;
129 const char *TimerGroupDescription;
130 HandlerInfo(AsmPrinterHandler *Handler, const char *TimerName,
131 const char *TimerDescription, const char *TimerGroupName,
132 const char *TimerGroupDescription)
133 : Handler(Handler), TimerName(TimerName),
134 TimerDescription(TimerDescription), TimerGroupName(TimerGroupName),
135 TimerGroupDescription(TimerGroupDescription) {}
137 /// A vector of all debug/EH info emitters we should use. This vector
138 /// maintains ownership of the emitters.
139 SmallVector<HandlerInfo, 1> Handlers;
142 struct SrcMgrDiagInfo {
144 const MDNode *LocInfo;
145 LLVMContext::InlineAsmDiagHandlerTy DiagHandler;
150 /// Structure for generating diagnostics for inline assembly. Only initialised
152 mutable std::unique_ptr<SrcMgrDiagInfo> DiagInfo;
154 /// If the target supports dwarf debug info, this pointer is non-null.
157 /// If the current module uses dwarf CFI annotations strictly for debugging.
158 bool isCFIMoveForDebugging;
161 explicit AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
164 ~AsmPrinter() override;
166 DwarfDebug *getDwarfDebug() { return DD; }
167 DwarfDebug *getDwarfDebug() const { return DD; }
169 uint16_t getDwarfVersion() const;
170 void setDwarfVersion(uint16_t Version);
172 bool isPositionIndependent() const;
174 /// Return true if assembly output should contain comments.
176 bool isVerbose() const { return VerboseAsm; }
178 /// Return a unique ID for the current function.
180 unsigned getFunctionNumber() const;
182 MCSymbol *getFunctionBegin() const { return CurrentFnBegin; }
183 MCSymbol *getFunctionEnd() const { return CurrentFnEnd; }
184 MCSymbol *getCurExceptionSym();
186 /// Return information about object file lowering.
187 const TargetLoweringObjectFile &getObjFileLowering() const;
189 /// Return information about data layout.
190 const DataLayout &getDataLayout() const;
192 /// Return the pointer size from the TargetMachine
193 unsigned getPointerSize() const;
195 /// Return information about subtarget.
196 const MCSubtargetInfo &getSubtargetInfo() const;
198 void EmitToStreamer(MCStreamer &S, const MCInst &Inst);
200 /// Return the current section we are emitting to.
201 const MCSection *getCurrentSection() const;
203 void getNameWithPrefix(SmallVectorImpl<char> &Name,
204 const GlobalValue *GV) const;
206 MCSymbol *getSymbol(const GlobalValue *GV) const;
208 //===------------------------------------------------------------------===//
209 // XRay instrumentation implementation.
210 //===------------------------------------------------------------------===//
212 // This describes the kind of sled we're storing in the XRay table.
213 enum class SledKind : uint8_t {
219 // The table will contain these structs that point to the sled, the function
220 // containing the sled, and what kind of sled (and whether they should always
222 struct XRayFunctionEntry {
223 const MCSymbol *Sled;
224 const MCSymbol *Function;
226 bool AlwaysInstrument;
227 const class Function *Fn;
229 void emit(int, MCStreamer *, const MCSymbol *) const;
232 // All the sleds to be emitted.
233 std::vector<XRayFunctionEntry> Sleds;
235 // Helper function to record a given XRay sled.
236 void recordSled(MCSymbol *Sled, const MachineInstr &MI, SledKind Kind);
238 /// Emit a table with all XRay instrumentation points.
239 void emitXRayTable();
241 //===------------------------------------------------------------------===//
242 // MachineFunctionPass Implementation.
243 //===------------------------------------------------------------------===//
245 /// Record analysis usage.
247 void getAnalysisUsage(AnalysisUsage &AU) const override;
249 /// Set up the AsmPrinter when we are working on a new module. If your pass
250 /// overrides this, it must make sure to explicitly call this implementation.
251 bool doInitialization(Module &M) override;
253 /// Shut down the asmprinter. If you override this in your pass, you must make
254 /// sure to call it explicitly.
255 bool doFinalization(Module &M) override;
257 /// Emit the specified function out to the OutStreamer.
258 bool runOnMachineFunction(MachineFunction &MF) override {
259 SetupMachineFunction(MF);
264 //===------------------------------------------------------------------===//
265 // Coarse grained IR lowering routines.
266 //===------------------------------------------------------------------===//
268 /// This should be called when a new MachineFunction is being processed from
269 /// runOnMachineFunction.
270 void SetupMachineFunction(MachineFunction &MF);
272 /// This method emits the body and trailer for a function.
273 void EmitFunctionBody();
275 void emitCFIInstruction(const MachineInstr &MI);
277 void emitFrameAlloc(const MachineInstr &MI);
279 enum CFIMoveType { CFI_M_None, CFI_M_EH, CFI_M_Debug };
280 CFIMoveType needsCFIMoves();
282 /// Returns false if needsCFIMoves() == CFI_M_EH for any function
284 bool needsOnlyDebugCFIMoves() const { return isCFIMoveForDebugging; }
286 bool needsSEHMoves();
288 /// Print to the current output stream assembly representations of the
289 /// constants in the constant pool MCP. This is used to print out constants
290 /// which have been "spilled to memory" by the code generator.
292 virtual void EmitConstantPool();
294 /// Print assembly representations of the jump tables used by the current
295 /// function to the current output stream.
297 virtual void EmitJumpTableInfo();
299 /// Emit the specified global variable to the .s file.
300 virtual void EmitGlobalVariable(const GlobalVariable *GV);
302 /// Check to see if the specified global is a special global used by LLVM. If
303 /// so, emit it and return true, otherwise do nothing and return false.
304 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
306 /// Emit an alignment directive to the specified power of two boundary. For
307 /// example, if you pass in 3 here, you will get an 8 byte alignment. If a
308 /// global value is specified, and if that global has an explicit alignment
309 /// requested, it will override the alignment request if required for
312 void EmitAlignment(unsigned NumBits, const GlobalObject *GO = nullptr) const;
314 /// Lower the specified LLVM Constant to an MCExpr.
315 virtual const MCExpr *lowerConstant(const Constant *CV);
317 /// \brief Print a general LLVM constant to the .s file.
318 void EmitGlobalConstant(const DataLayout &DL, const Constant *CV);
320 /// \brief Unnamed constant global variables solely contaning a pointer to
321 /// another globals variable act like a global variable "proxy", or GOT
322 /// equivalents, i.e., it's only used to hold the address of the latter. One
323 /// optimization is to replace accesses to these proxies by using the GOT
324 /// entry for the final global instead. Hence, we select GOT equivalent
325 /// candidates among all the module global variables, avoid emitting them
326 /// unnecessarily and finally replace references to them by pc relative
327 /// accesses to GOT entries.
328 void computeGlobalGOTEquivs(Module &M);
330 /// \brief Constant expressions using GOT equivalent globals may not be
331 /// eligible for PC relative GOT entry conversion, in such cases we need to
332 /// emit the proxies we previously omitted in EmitGlobalVariable.
333 void emitGlobalGOTEquivs();
335 //===------------------------------------------------------------------===//
337 //===------------------------------------------------------------------===//
339 // Targets can, or in the case of EmitInstruction, must implement these to
342 /// This virtual method can be overridden by targets that want to emit
343 /// something at the start of their file.
344 virtual void EmitStartOfAsmFile(Module &) {}
346 /// This virtual method can be overridden by targets that want to emit
347 /// something at the end of their file.
348 virtual void EmitEndOfAsmFile(Module &) {}
350 /// Targets can override this to emit stuff before the first basic block in
352 virtual void EmitFunctionBodyStart() {}
354 /// Targets can override this to emit stuff after the last basic block in the
356 virtual void EmitFunctionBodyEnd() {}
358 /// Targets can override this to emit stuff at the start of a basic block.
359 /// By default, this method prints the label for the specified
360 /// MachineBasicBlock, an alignment (if present) and a comment describing it
362 virtual void EmitBasicBlockStart(const MachineBasicBlock &MBB) const;
364 /// Targets can override this to emit stuff at the end of a basic block.
365 virtual void EmitBasicBlockEnd(const MachineBasicBlock &MBB) {}
367 /// Targets should implement this to emit instructions.
368 virtual void EmitInstruction(const MachineInstr *) {
369 llvm_unreachable("EmitInstruction not implemented");
372 /// Return the symbol for the specified constant pool entry.
373 virtual MCSymbol *GetCPISymbol(unsigned CPID) const;
375 virtual void EmitFunctionEntryLabel();
377 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
379 /// Targets can override this to change how global constants that are part of
380 /// a C++ static/global constructor list are emitted.
381 virtual void EmitXXStructor(const DataLayout &DL, const Constant *CV) {
382 EmitGlobalConstant(DL, CV);
385 /// Return true if the basic block has exactly one predecessor and the control
386 /// transfer mechanism between the predecessor and this block is a
389 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
391 /// Targets can override this to customize the output of IMPLICIT_DEF
392 /// instructions in verbose mode.
393 virtual void emitImplicitDef(const MachineInstr *MI) const;
395 //===------------------------------------------------------------------===//
396 // Symbol Lowering Routines.
397 //===------------------------------------------------------------------===//
399 MCSymbol *createTempSymbol(const Twine &Name) const;
401 /// Return the MCSymbol for a private symbol with global value name as its
402 /// base, with the specified suffix.
403 MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
404 StringRef Suffix) const;
406 /// Return the MCSymbol for the specified ExternalSymbol.
407 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
409 /// Return the symbol for the specified jump table entry.
410 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
412 /// Return the symbol for the specified jump table .set
413 /// FIXME: privatize to AsmPrinter.
414 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
416 /// Return the MCSymbol used to satisfy BlockAddress uses of the specified
418 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
419 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
421 //===------------------------------------------------------------------===//
422 // Emission Helper Routines.
423 //===------------------------------------------------------------------===//
425 /// This is just convenient handler for printing offsets.
426 void printOffset(int64_t Offset, raw_ostream &OS) const;
428 /// Emit a byte directive and value.
430 void EmitInt8(int Value) const;
432 /// Emit a short directive and value.
434 void EmitInt16(int Value) const;
436 /// Emit a long directive and value.
438 void EmitInt32(int Value) const;
440 /// Emit something like ".long Hi-Lo" where the size in bytes of the directive
441 /// is specified by Size and Hi/Lo specify the labels. This implicitly uses
442 /// .set if it is available.
443 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
444 unsigned Size) const;
446 /// Emit something like ".long Label+Offset" where the size in bytes of the
447 /// directive is specified by Size and Label specifies the label. This
448 /// implicitly uses .set if it is available.
449 void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
450 unsigned Size, bool IsSectionRelative = false) const;
452 /// Emit something like ".long Label" where the size in bytes of the directive
453 /// is specified by Size and Label specifies the label.
454 void EmitLabelReference(const MCSymbol *Label, unsigned Size,
455 bool IsSectionRelative = false) const {
456 EmitLabelPlusOffset(Label, 0, Size, IsSectionRelative);
459 //===------------------------------------------------------------------===//
460 // Dwarf Emission Helper Routines
461 //===------------------------------------------------------------------===//
463 /// Emit the specified signed leb128 value.
464 void EmitSLEB128(int64_t Value, const char *Desc = nullptr) const;
466 /// Emit the specified unsigned leb128 value.
467 void EmitULEB128(uint64_t Value, const char *Desc = nullptr,
468 unsigned PadTo = 0) const;
470 /// Emit a .byte 42 directive that corresponds to an encoding. If verbose
471 /// assembly output is enabled, we output comments describing the encoding.
472 /// Desc is a string saying what the encoding is specifying (e.g. "LSDA").
473 void EmitEncodingByte(unsigned Val, const char *Desc = nullptr) const;
475 /// Return the size of the encoding in bytes.
476 unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
478 /// Emit reference to a ttype global with a specified encoding.
479 void EmitTTypeReference(const GlobalValue *GV, unsigned Encoding) const;
481 /// Emit a reference to a symbol for use in dwarf. Different object formats
482 /// represent this in different ways. Some use a relocation others encode
483 /// the label offset in its section.
484 void emitDwarfSymbolReference(const MCSymbol *Label,
485 bool ForceOffset = false) const;
487 /// Emit the 4-byte offset of a string from the start of its section.
489 /// When possible, emit a DwarfStringPool section offset without any
490 /// relocations, and without using the symbol. Otherwise, defers to \a
491 /// emitDwarfSymbolReference().
492 void emitDwarfStringOffset(DwarfStringPoolEntryRef S) const;
494 /// Get the value for DW_AT_APPLE_isa. Zero if no isa encoding specified.
495 virtual unsigned getISAEncoding() { return 0; }
497 /// Emit the directive and value for debug thread local expression
499 /// \p Value - The value to emit.
500 /// \p Size - The size of the integer (in bytes) to emit.
501 virtual void EmitDebugValue(const MCExpr *Value, unsigned Size) const;
503 //===------------------------------------------------------------------===//
504 // Dwarf Lowering Routines
505 //===------------------------------------------------------------------===//
507 /// \brief Emit frame instruction to describe the layout of the frame.
508 void emitCFIInstruction(const MCCFIInstruction &Inst) const;
510 /// \brief Emit Dwarf abbreviation table.
511 template <typename T> void emitDwarfAbbrevs(const T &Abbrevs) const {
512 // For each abbreviation.
513 for (const auto &Abbrev : Abbrevs)
514 emitDwarfAbbrev(*Abbrev);
516 // Mark end of abbreviations.
517 EmitULEB128(0, "EOM(3)");
520 void emitDwarfAbbrev(const DIEAbbrev &Abbrev) const;
522 /// \brief Recursively emit Dwarf DIE tree.
523 void emitDwarfDIE(const DIE &Die) const;
525 //===------------------------------------------------------------------===//
526 // Inline Asm Support
527 //===------------------------------------------------------------------===//
529 // These are hooks that targets can override to implement inline asm
530 // support. These should probably be moved out of AsmPrinter someday.
532 /// Print information related to the specified machine instr that is
533 /// independent of the operand, and may be independent of the instr itself.
534 /// This can be useful for portably encoding the comment character or other
535 /// bits of target-specific knowledge into the asmstrings. The syntax used is
536 /// ${:comment}. Targets can override this to add support for their own
538 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
539 const char *Code) const;
541 /// Print the specified operand of MI, an INLINEASM instruction, using the
542 /// specified assembler variant. Targets should override this to format as
543 /// appropriate. This method can return true if the operand is erroneous.
544 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
545 unsigned AsmVariant, const char *ExtraCode,
548 /// Print the specified operand of MI, an INLINEASM instruction, using the
549 /// specified assembler variant as an address. Targets should override this to
550 /// format as appropriate. This method can return true if the operand is
552 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
553 unsigned AsmVariant, const char *ExtraCode,
556 /// Let the target do anything it needs to do before emitting inlineasm.
557 /// \p StartInfo - the subtarget info before parsing inline asm
558 virtual void emitInlineAsmStart() const;
560 /// Let the target do anything it needs to do after emitting inlineasm.
561 /// This callback can be used restore the original mode in case the
562 /// inlineasm contains directives to switch modes.
563 /// \p StartInfo - the original subtarget info before inline asm
564 /// \p EndInfo - the final subtarget info after parsing the inline asm,
565 /// or NULL if the value is unknown.
566 virtual void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
567 const MCSubtargetInfo *EndInfo) const;
570 /// Private state for PrintSpecial()
571 // Assign a unique ID to this machine instruction.
572 mutable const MachineInstr *LastMI;
573 mutable unsigned LastFn;
574 mutable unsigned Counter;
576 /// This method emits the header for the current function.
577 virtual void EmitFunctionHeader();
579 /// Emit a blob of inline asm to the output streamer.
581 EmitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
582 const MCTargetOptions &MCOptions,
583 const MDNode *LocMDNode = nullptr,
584 InlineAsm::AsmDialect AsmDialect = InlineAsm::AD_ATT) const;
586 /// This method formats and emits the specified machine instruction that is an
588 void EmitInlineAsm(const MachineInstr *MI) const;
590 //===------------------------------------------------------------------===//
591 // Internal Implementation Details
592 //===------------------------------------------------------------------===//
594 /// This emits visibility information about symbol, if this is suported by the
596 void EmitVisibility(MCSymbol *Sym, unsigned Visibility,
597 bool IsDefinition = true) const;
599 void EmitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const;
601 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
602 const MachineBasicBlock *MBB, unsigned uid) const;
603 void EmitLLVMUsedList(const ConstantArray *InitList);
604 /// Emit llvm.ident metadata in an '.ident' directive.
605 void EmitModuleIdents(Module &M);
606 void EmitXXStructorList(const DataLayout &DL, const Constant *List,
608 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy &C);
609 /// Emit GlobalAlias or GlobalIFunc.
610 void emitGlobalIndirectSymbol(Module &M,
611 const GlobalIndirectSymbol& GIS);