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"
28 class AsmPrinterHandler;
36 class GCMetadataPrinter;
37 class GlobalIndirectSymbol;
40 class MachineBasicBlock;
41 class MachineFunction;
43 class MachineLocation;
44 class MachineLoopInfo;
46 class MachineConstantPoolValue;
47 class MachineJumpTableInfo;
48 class MachineModuleInfo;
50 class MCCFIInstruction;
56 class MCSubtargetInfo;
58 class MCTargetOptions;
62 class TargetLoweringObjectFile;
66 /// This class is intended to be used as a driving class for all asm writers.
67 class AsmPrinter : public MachineFunctionPass {
69 /// Target machine description.
73 /// Target Asm Printer information.
77 /// This is the context for the output file that we are streaming. This owns
78 /// all of the global MC-related objects for the generated translation unit.
79 MCContext &OutContext;
81 /// This is the MCStreamer object for the file we are generating. This
82 /// contains the transient state for the current translation unit that we are
83 /// generating (such as the current section etc).
84 std::unique_ptr<MCStreamer> OutStreamer;
86 /// The current machine function.
87 const MachineFunction *MF;
89 /// This is a pointer to the current MachineModuleInfo.
90 MachineModuleInfo *MMI;
92 /// The symbol for the current function. This is recalculated at the beginning
93 /// of each call to runOnMachineFunction().
95 MCSymbol *CurrentFnSym;
97 /// The symbol used to represent the start of the current function for the
98 /// purpose of calculating its size (e.g. using the .size directive). By
99 /// default, this is equal to CurrentFnSym.
100 MCSymbol *CurrentFnSymForSize;
102 /// Map global GOT equivalent MCSymbols to GlobalVariables and keep track of
103 /// its number of uses by other globals.
104 typedef std::pair<const GlobalVariable *, unsigned> GOTEquivUsePair;
105 MapVector<const MCSymbol *, GOTEquivUsePair> GlobalGOTEquivs;
108 MCSymbol *CurrentFnBegin;
109 MCSymbol *CurrentFnEnd;
110 MCSymbol *CurExceptionSym;
112 // The garbage collection metadata printer table.
113 void *GCMetadataPrinters; // Really a DenseMap.
115 /// Emit comments in assembly output if this is true.
120 /// If VerboseAsm is set, a pointer to the loop info for this function.
124 AsmPrinterHandler *Handler;
125 const char *TimerName;
126 const char *TimerDescription;
127 const char *TimerGroupName;
128 const char *TimerGroupDescription;
129 HandlerInfo(AsmPrinterHandler *Handler, const char *TimerName,
130 const char *TimerDescription, const char *TimerGroupName,
131 const char *TimerGroupDescription)
132 : Handler(Handler), TimerName(TimerName),
133 TimerDescription(TimerDescription), TimerGroupName(TimerGroupName),
134 TimerGroupDescription(TimerGroupDescription) {}
136 /// A vector of all debug/EH info emitters we should use. This vector
137 /// maintains ownership of the emitters.
138 SmallVector<HandlerInfo, 1> Handlers;
140 /// If the target supports dwarf debug info, this pointer is non-null.
144 explicit AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
147 ~AsmPrinter() override;
149 DwarfDebug *getDwarfDebug() { return DD; }
150 DwarfDebug *getDwarfDebug() const { return DD; }
152 uint16_t getDwarfVersion() const;
153 void setDwarfVersion(uint16_t Version);
155 bool isPositionIndependent() const;
157 /// Return true if assembly output should contain comments.
159 bool isVerbose() const { return VerboseAsm; }
161 /// Return a unique ID for the current function.
163 unsigned getFunctionNumber() const;
165 MCSymbol *getFunctionBegin() const { return CurrentFnBegin; }
166 MCSymbol *getFunctionEnd() const { return CurrentFnEnd; }
167 MCSymbol *getCurExceptionSym();
169 /// Return information about object file lowering.
170 const TargetLoweringObjectFile &getObjFileLowering() const;
172 /// Return information about data layout.
173 const DataLayout &getDataLayout() const;
175 /// Return the pointer size from the TargetMachine
176 unsigned getPointerSize() const;
178 /// Return information about subtarget.
179 const MCSubtargetInfo &getSubtargetInfo() const;
181 void EmitToStreamer(MCStreamer &S, const MCInst &Inst);
183 /// Return the current section we are emitting to.
184 const MCSection *getCurrentSection() const;
186 void getNameWithPrefix(SmallVectorImpl<char> &Name,
187 const GlobalValue *GV) const;
189 MCSymbol *getSymbol(const GlobalValue *GV) const;
191 //===------------------------------------------------------------------===//
192 // XRay instrumentation implementation.
193 //===------------------------------------------------------------------===//
195 // This describes the kind of sled we're storing in the XRay table.
196 enum class SledKind : uint8_t {
202 // The table will contain these structs that point to the sled, the function
203 // containing the sled, and what kind of sled (and whether they should always
205 struct XRayFunctionEntry {
206 const MCSymbol *Sled;
207 const MCSymbol *Function;
209 bool AlwaysInstrument;
210 const class Function *Fn;
212 void emit(int, MCStreamer *, const MCSymbol *) const;
215 // All the sleds to be emitted.
216 std::vector<XRayFunctionEntry> Sleds;
218 // Helper function to record a given XRay sled.
219 void recordSled(MCSymbol *Sled, const MachineInstr &MI, SledKind Kind);
221 /// Emit a table with all XRay instrumentation points.
222 void emitXRayTable();
224 //===------------------------------------------------------------------===//
225 // MachineFunctionPass Implementation.
226 //===------------------------------------------------------------------===//
228 /// Record analysis usage.
230 void getAnalysisUsage(AnalysisUsage &AU) const override;
232 /// Set up the AsmPrinter when we are working on a new module. If your pass
233 /// overrides this, it must make sure to explicitly call this implementation.
234 bool doInitialization(Module &M) override;
236 /// Shut down the asmprinter. If you override this in your pass, you must make
237 /// sure to call it explicitly.
238 bool doFinalization(Module &M) override;
240 /// Emit the specified function out to the OutStreamer.
241 bool runOnMachineFunction(MachineFunction &MF) override {
242 SetupMachineFunction(MF);
247 //===------------------------------------------------------------------===//
248 // Coarse grained IR lowering routines.
249 //===------------------------------------------------------------------===//
251 /// This should be called when a new MachineFunction is being processed from
252 /// runOnMachineFunction.
253 void SetupMachineFunction(MachineFunction &MF);
255 /// This method emits the body and trailer for a function.
256 void EmitFunctionBody();
258 void emitCFIInstruction(const MachineInstr &MI);
260 void emitFrameAlloc(const MachineInstr &MI);
262 enum CFIMoveType { CFI_M_None, CFI_M_EH, CFI_M_Debug };
263 CFIMoveType needsCFIMoves();
265 bool needsSEHMoves();
267 /// Print to the current output stream assembly representations of the
268 /// constants in the constant pool MCP. This is used to print out constants
269 /// which have been "spilled to memory" by the code generator.
271 virtual void EmitConstantPool();
273 /// Print assembly representations of the jump tables used by the current
274 /// function to the current output stream.
276 virtual void EmitJumpTableInfo();
278 /// Emit the specified global variable to the .s file.
279 virtual void EmitGlobalVariable(const GlobalVariable *GV);
281 /// Check to see if the specified global is a special global used by LLVM. If
282 /// so, emit it and return true, otherwise do nothing and return false.
283 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
285 /// Emit an alignment directive to the specified power of two boundary. For
286 /// example, if you pass in 3 here, you will get an 8 byte alignment. If a
287 /// global value is specified, and if that global has an explicit alignment
288 /// requested, it will override the alignment request if required for
291 void EmitAlignment(unsigned NumBits, const GlobalObject *GO = nullptr) const;
293 /// Lower the specified LLVM Constant to an MCExpr.
294 virtual const MCExpr *lowerConstant(const Constant *CV);
296 /// \brief Print a general LLVM constant to the .s file.
297 void EmitGlobalConstant(const DataLayout &DL, const Constant *CV);
299 /// \brief Unnamed constant global variables solely contaning a pointer to
300 /// another globals variable act like a global variable "proxy", or GOT
301 /// equivalents, i.e., it's only used to hold the address of the latter. One
302 /// optimization is to replace accesses to these proxies by using the GOT
303 /// entry for the final global instead. Hence, we select GOT equivalent
304 /// candidates among all the module global variables, avoid emitting them
305 /// unnecessarily and finally replace references to them by pc relative
306 /// accesses to GOT entries.
307 void computeGlobalGOTEquivs(Module &M);
309 /// \brief Constant expressions using GOT equivalent globals may not be
310 /// eligible for PC relative GOT entry conversion, in such cases we need to
311 /// emit the proxies we previously omitted in EmitGlobalVariable.
312 void emitGlobalGOTEquivs();
314 //===------------------------------------------------------------------===//
316 //===------------------------------------------------------------------===//
318 // Targets can, or in the case of EmitInstruction, must implement these to
321 /// This virtual method can be overridden by targets that want to emit
322 /// something at the start of their file.
323 virtual void EmitStartOfAsmFile(Module &) {}
325 /// This virtual method can be overridden by targets that want to emit
326 /// something at the end of their file.
327 virtual void EmitEndOfAsmFile(Module &) {}
329 /// Targets can override this to emit stuff before the first basic block in
331 virtual void EmitFunctionBodyStart() {}
333 /// Targets can override this to emit stuff after the last basic block in the
335 virtual void EmitFunctionBodyEnd() {}
337 /// Targets can override this to emit stuff at the start of a basic block.
338 /// By default, this method prints the label for the specified
339 /// MachineBasicBlock, an alignment (if present) and a comment describing it
341 virtual void EmitBasicBlockStart(const MachineBasicBlock &MBB) const;
343 /// Targets can override this to emit stuff at the end of a basic block.
344 virtual void EmitBasicBlockEnd(const MachineBasicBlock &MBB) {}
346 /// Targets should implement this to emit instructions.
347 virtual void EmitInstruction(const MachineInstr *) {
348 llvm_unreachable("EmitInstruction not implemented");
351 /// Return the symbol for the specified constant pool entry.
352 virtual MCSymbol *GetCPISymbol(unsigned CPID) const;
354 virtual void EmitFunctionEntryLabel();
356 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
358 /// Targets can override this to change how global constants that are part of
359 /// a C++ static/global constructor list are emitted.
360 virtual void EmitXXStructor(const DataLayout &DL, const Constant *CV) {
361 EmitGlobalConstant(DL, CV);
364 /// Return true if the basic block has exactly one predecessor and the control
365 /// transfer mechanism between the predecessor and this block is a
368 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
370 /// Targets can override this to customize the output of IMPLICIT_DEF
371 /// instructions in verbose mode.
372 virtual void emitImplicitDef(const MachineInstr *MI) const;
374 //===------------------------------------------------------------------===//
375 // Symbol Lowering Routines.
376 //===------------------------------------------------------------------===//
378 MCSymbol *createTempSymbol(const Twine &Name) const;
380 /// Return the MCSymbol for a private symbol with global value name as its
381 /// base, with the specified suffix.
382 MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
383 StringRef Suffix) const;
385 /// Return the MCSymbol for the specified ExternalSymbol.
386 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
388 /// Return the symbol for the specified jump table entry.
389 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
391 /// Return the symbol for the specified jump table .set
392 /// FIXME: privatize to AsmPrinter.
393 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
395 /// Return the MCSymbol used to satisfy BlockAddress uses of the specified
397 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
398 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
400 //===------------------------------------------------------------------===//
401 // Emission Helper Routines.
402 //===------------------------------------------------------------------===//
404 /// This is just convenient handler for printing offsets.
405 void printOffset(int64_t Offset, raw_ostream &OS) const;
407 /// Emit a byte directive and value.
409 void EmitInt8(int Value) const;
411 /// Emit a short directive and value.
413 void EmitInt16(int Value) const;
415 /// Emit a long directive and value.
417 void EmitInt32(int Value) const;
419 /// Emit something like ".long Hi-Lo" where the size in bytes of the directive
420 /// is specified by Size and Hi/Lo specify the labels. This implicitly uses
421 /// .set if it is available.
422 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
423 unsigned Size) const;
425 /// Emit something like ".long Label+Offset" where the size in bytes of the
426 /// directive is specified by Size and Label specifies the label. This
427 /// implicitly uses .set if it is available.
428 void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
429 unsigned Size, bool IsSectionRelative = false) const;
431 /// Emit something like ".long Label" where the size in bytes of the directive
432 /// is specified by Size and Label specifies the label.
433 void EmitLabelReference(const MCSymbol *Label, unsigned Size,
434 bool IsSectionRelative = false) const {
435 EmitLabelPlusOffset(Label, 0, Size, IsSectionRelative);
438 //===------------------------------------------------------------------===//
439 // Dwarf Emission Helper Routines
440 //===------------------------------------------------------------------===//
442 /// Emit the specified signed leb128 value.
443 void EmitSLEB128(int64_t Value, const char *Desc = nullptr) const;
445 /// Emit the specified unsigned leb128 value.
446 void EmitULEB128(uint64_t Value, const char *Desc = nullptr,
447 unsigned PadTo = 0) const;
449 /// Emit a .byte 42 directive that corresponds to an encoding. If verbose
450 /// assembly output is enabled, we output comments describing the encoding.
451 /// Desc is a string saying what the encoding is specifying (e.g. "LSDA").
452 void EmitEncodingByte(unsigned Val, const char *Desc = nullptr) const;
454 /// Return the size of the encoding in bytes.
455 unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
457 /// Emit reference to a ttype global with a specified encoding.
458 void EmitTTypeReference(const GlobalValue *GV, unsigned Encoding) const;
460 /// Emit a reference to a symbol for use in dwarf. Different object formats
461 /// represent this in different ways. Some use a relocation others encode
462 /// the label offset in its section.
463 void emitDwarfSymbolReference(const MCSymbol *Label,
464 bool ForceOffset = false) const;
466 /// Emit the 4-byte offset of a string from the start of its section.
468 /// When possible, emit a DwarfStringPool section offset without any
469 /// relocations, and without using the symbol. Otherwise, defers to \a
470 /// emitDwarfSymbolReference().
471 void emitDwarfStringOffset(DwarfStringPoolEntryRef S) const;
473 /// Get the value for DW_AT_APPLE_isa. Zero if no isa encoding specified.
474 virtual unsigned getISAEncoding() { return 0; }
476 //===------------------------------------------------------------------===//
477 // Dwarf Lowering Routines
478 //===------------------------------------------------------------------===//
480 /// \brief Emit frame instruction to describe the layout of the frame.
481 void emitCFIInstruction(const MCCFIInstruction &Inst) const;
483 /// \brief Emit Dwarf abbreviation table.
484 template <typename T> void emitDwarfAbbrevs(const T &Abbrevs) const {
485 // For each abbreviation.
486 for (const auto &Abbrev : Abbrevs)
487 emitDwarfAbbrev(*Abbrev);
489 // Mark end of abbreviations.
490 EmitULEB128(0, "EOM(3)");
493 void emitDwarfAbbrev(const DIEAbbrev &Abbrev) const;
495 /// \brief Recursively emit Dwarf DIE tree.
496 void emitDwarfDIE(const DIE &Die) const;
498 //===------------------------------------------------------------------===//
499 // Inline Asm Support
500 //===------------------------------------------------------------------===//
502 // These are hooks that targets can override to implement inline asm
503 // support. These should probably be moved out of AsmPrinter someday.
505 /// Print information related to the specified machine instr that is
506 /// independent of the operand, and may be independent of the instr itself.
507 /// This can be useful for portably encoding the comment character or other
508 /// bits of target-specific knowledge into the asmstrings. The syntax used is
509 /// ${:comment}. Targets can override this to add support for their own
511 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
512 const char *Code) const;
514 /// Print the specified operand of MI, an INLINEASM instruction, using the
515 /// specified assembler variant. Targets should override this to format as
516 /// appropriate. This method can return true if the operand is erroneous.
517 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
518 unsigned AsmVariant, const char *ExtraCode,
521 /// Print the specified operand of MI, an INLINEASM instruction, using the
522 /// specified assembler variant as an address. Targets should override this to
523 /// format as appropriate. This method can return true if the operand is
525 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
526 unsigned AsmVariant, const char *ExtraCode,
529 /// Let the target do anything it needs to do before emitting inlineasm.
530 /// \p StartInfo - the subtarget info before parsing inline asm
531 virtual void emitInlineAsmStart() const;
533 /// Let the target do anything it needs to do after emitting inlineasm.
534 /// This callback can be used restore the original mode in case the
535 /// inlineasm contains directives to switch modes.
536 /// \p StartInfo - the original subtarget info before inline asm
537 /// \p EndInfo - the final subtarget info after parsing the inline asm,
538 /// or NULL if the value is unknown.
539 virtual void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
540 const MCSubtargetInfo *EndInfo) const;
543 /// Private state for PrintSpecial()
544 // Assign a unique ID to this machine instruction.
545 mutable const MachineInstr *LastMI;
546 mutable unsigned LastFn;
547 mutable unsigned Counter;
549 /// This method emits the header for the current function.
550 virtual void EmitFunctionHeader();
552 /// Emit a blob of inline asm to the output streamer.
554 EmitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
555 const MCTargetOptions &MCOptions,
556 const MDNode *LocMDNode = nullptr,
557 InlineAsm::AsmDialect AsmDialect = InlineAsm::AD_ATT) const;
559 /// This method formats and emits the specified machine instruction that is an
561 void EmitInlineAsm(const MachineInstr *MI) const;
563 //===------------------------------------------------------------------===//
564 // Internal Implementation Details
565 //===------------------------------------------------------------------===//
567 /// This emits visibility information about symbol, if this is suported by the
569 void EmitVisibility(MCSymbol *Sym, unsigned Visibility,
570 bool IsDefinition = true) const;
572 void EmitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const;
574 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
575 const MachineBasicBlock *MBB, unsigned uid) const;
576 void EmitLLVMUsedList(const ConstantArray *InitList);
577 /// Emit llvm.ident metadata in an '.ident' directive.
578 void EmitModuleIdents(Module &M);
579 void EmitXXStructorList(const DataLayout &DL, const Constant *List,
581 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy &C);
582 /// Emit GlobalAlias or GlobalIFunc.
583 void emitGlobalIndirectSymbol(Module &M,
584 const GlobalIndirectSymbol& GIS);