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/Twine.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/IR/InlineAsm.h"
22 #include "llvm/Support/DataTypes.h"
23 #include "llvm/Support/ErrorHandling.h"
26 class AsmPrinterHandler;
32 class GCMetadataPrinter;
35 class MachineBasicBlock;
36 class MachineFunction;
38 class MachineLocation;
39 class MachineLoopInfo;
41 class MachineConstantPoolValue;
42 class MachineJumpTableInfo;
43 class MachineModuleInfo;
45 class MCCFIInstruction;
51 class MCSubtargetInfo;
57 class TargetLoweringObjectFile;
61 /// AsmPrinter - This class is intended to be used as a driving class for all
63 class AsmPrinter : public MachineFunctionPass {
65 /// Target machine description.
69 /// Target Asm Printer information.
73 const MCInstrInfo *MII;
74 /// OutContext - This is the context for the output file that we are
75 /// streaming. This owns all of the global MC-related objects for the
76 /// generated translation unit.
77 MCContext &OutContext;
79 /// OutStreamer - This is the MCStreamer object for the file we are
80 /// generating. This contains the transient state for the current
81 /// translation unit that we are generating (such as the current section
83 MCStreamer &OutStreamer;
85 /// The current machine function.
86 const MachineFunction *MF;
88 /// MMI - This is a pointer to the current MachineModuleInfo.
89 MachineModuleInfo *MMI;
91 /// Name-mangler for global names.
95 /// The symbol for the current function. This is recalculated at the
96 /// beginning of each call to runOnMachineFunction().
98 MCSymbol *CurrentFnSym;
100 /// The symbol used to represent the start of the current function for the
101 /// purpose of calculating its size (e.g. using the .size directive). By
102 /// default, this is equal to CurrentFnSym.
103 MCSymbol *CurrentFnSymForSize;
106 // GCMetadataPrinters - The garbage collection metadata printer table.
107 void *GCMetadataPrinters; // Really a DenseMap.
109 /// VerboseAsm - Emit comments in assembly output if this is true.
114 /// If VerboseAsm is set, a pointer to the loop info for this
119 AsmPrinterHandler *Handler;
120 const char *TimerName, *TimerGroupName;
121 HandlerInfo(AsmPrinterHandler *Handler, const char *TimerName,
122 const char *TimerGroupName)
123 : Handler(Handler), TimerName(TimerName),
124 TimerGroupName(TimerGroupName) {}
126 /// Handlers - a vector of all debug/EH info emitters we should use.
127 /// This vector maintains ownership of the emitters.
128 SmallVector<HandlerInfo, 1> Handlers;
130 /// DD - If the target supports dwarf debug info, this pointer is non-null.
134 explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer);
137 virtual ~AsmPrinter();
139 DwarfDebug *getDwarfDebug() { return DD; }
141 /// isVerbose - Return true if assembly output should contain comments.
143 bool isVerbose() const { return VerboseAsm; }
145 /// getFunctionNumber - Return a unique ID for the current function.
147 unsigned getFunctionNumber() const;
149 /// getObjFileLowering - Return information about object file lowering.
150 const TargetLoweringObjectFile &getObjFileLowering() const;
152 /// getDataLayout - Return information about data layout.
153 const DataLayout &getDataLayout() const;
155 /// getSubtargetInfo - Return information about subtarget.
156 const MCSubtargetInfo &getSubtargetInfo() const;
158 void EmitToStreamer(MCStreamer &S, const MCInst &Inst);
160 /// getTargetTriple - Return the target triple string.
161 StringRef getTargetTriple() const;
163 /// getCurrentSection() - Return the current section we are emitting to.
164 const MCSection *getCurrentSection() const;
166 void getNameWithPrefix(SmallVectorImpl<char> &Name,
167 const GlobalValue *GV) const;
169 MCSymbol *getSymbol(const GlobalValue *GV) const;
171 //===------------------------------------------------------------------===//
172 // MachineFunctionPass Implementation.
173 //===------------------------------------------------------------------===//
175 /// getAnalysisUsage - Record analysis usage.
177 void getAnalysisUsage(AnalysisUsage &AU) const override;
179 /// doInitialization - Set up the AsmPrinter when we are working on a new
180 /// module. If your pass overrides this, it must make sure to explicitly
181 /// call this implementation.
182 bool doInitialization(Module &M) override;
184 /// doFinalization - Shut down the asmprinter. If you override this in your
185 /// pass, you must make sure to call it explicitly.
186 bool doFinalization(Module &M) override;
188 /// runOnMachineFunction - Emit the specified function out to the
190 bool runOnMachineFunction(MachineFunction &MF) override {
191 SetupMachineFunction(MF);
192 EmitFunctionHeader();
197 //===------------------------------------------------------------------===//
198 // Coarse grained IR lowering routines.
199 //===------------------------------------------------------------------===//
201 /// SetupMachineFunction - This should be called when a new MachineFunction
202 /// is being processed from runOnMachineFunction.
203 void SetupMachineFunction(MachineFunction &MF);
205 /// EmitFunctionHeader - This method emits the header for the current
207 void EmitFunctionHeader();
209 /// EmitFunctionBody - This method emits the body and trailer for a
211 void EmitFunctionBody();
213 void emitCFIInstruction(const MachineInstr &MI);
220 CFIMoveType needsCFIMoves();
222 bool needsSEHMoves();
224 /// EmitConstantPool - Print to the current output stream assembly
225 /// representations of the constants in the constant pool MCP. This is
226 /// used to print out constants which have been "spilled to memory" by
227 /// the code generator.
229 virtual void EmitConstantPool();
231 /// EmitJumpTableInfo - Print assembly representations of the jump tables
232 /// used by the current function to the current output stream.
234 void EmitJumpTableInfo();
236 /// EmitGlobalVariable - Emit the specified global variable to the .s file.
237 virtual void EmitGlobalVariable(const GlobalVariable *GV);
239 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
240 /// special global used by LLVM. If so, emit it and return true, otherwise
241 /// do nothing and return false.
242 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
244 /// EmitAlignment - Emit an alignment directive to the specified power of
245 /// two boundary. For example, if you pass in 3 here, you will get an 8
246 /// byte alignment. If a global value is specified, and if that global has
247 /// an explicit alignment requested, it will override the alignment request
248 /// if required for correctness.
250 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = nullptr) const;
252 /// EmitBasicBlockStart - This method prints the label for the specified
253 /// MachineBasicBlock, an alignment (if present) and a comment describing
254 /// it if appropriate.
255 void EmitBasicBlockStart(const MachineBasicBlock &MBB) const;
257 /// \brief Print a general LLVM constant to the .s file.
258 void EmitGlobalConstant(const Constant *CV);
261 //===------------------------------------------------------------------===//
263 //===------------------------------------------------------------------===//
265 // Targets can, or in the case of EmitInstruction, must implement these to
268 /// EmitStartOfAsmFile - This virtual method can be overridden by targets
269 /// that want to emit something at the start of their file.
270 virtual void EmitStartOfAsmFile(Module &) {}
272 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
273 /// want to emit something at the end of their file.
274 virtual void EmitEndOfAsmFile(Module &) {}
276 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
277 /// the first basic block in the function.
278 virtual void EmitFunctionBodyStart() {}
280 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
281 /// the last basic block in the function.
282 virtual void EmitFunctionBodyEnd() {}
284 /// EmitInstruction - Targets should implement this to emit instructions.
285 virtual void EmitInstruction(const MachineInstr *) {
286 llvm_unreachable("EmitInstruction not implemented");
289 /// GetCPISymbol - Return the symbol for the specified constant pool entry.
290 virtual MCSymbol *GetCPISymbol(unsigned CPID) const;
292 virtual void EmitFunctionEntryLabel();
294 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
296 /// EmitXXStructor - Targets can override this to change how global
297 /// constants that are part of a C++ static/global constructor list are
299 virtual void EmitXXStructor(const Constant *CV) {
300 EmitGlobalConstant(CV);
303 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
304 /// exactly one predecessor and the control transfer mechanism between
305 /// the predecessor and this block is a fall-through.
307 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
309 /// emitImplicitDef - Targets can override this to customize the output of
310 /// IMPLICIT_DEF instructions in verbose mode.
311 virtual void emitImplicitDef(const MachineInstr *MI) const;
313 //===------------------------------------------------------------------===//
314 // Symbol Lowering Routines.
315 //===------------------------------------------------------------------===//
318 /// GetTempSymbol - Return the MCSymbol corresponding to the assembler
319 /// temporary label with the specified stem and unique ID.
320 MCSymbol *GetTempSymbol(Twine Name, unsigned ID) const;
322 /// GetTempSymbol - Return an assembler temporary label with the specified
324 MCSymbol *GetTempSymbol(Twine Name) const;
326 /// Return the MCSymbol for a private symbol with global value name as its
327 /// base, with the specified suffix.
328 MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
329 StringRef Suffix) const;
331 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
333 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
335 /// GetJTISymbol - Return the symbol for the specified jump table entry.
336 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
338 /// GetJTSetSymbol - Return the symbol for the specified jump table .set
339 /// FIXME: privatize to AsmPrinter.
340 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
342 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
343 /// uses of the specified basic block.
344 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
345 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
347 //===------------------------------------------------------------------===//
348 // Emission Helper Routines.
349 //===------------------------------------------------------------------===//
351 /// printOffset - This is just convenient handler for printing offsets.
352 void printOffset(int64_t Offset, raw_ostream &OS) const;
354 /// EmitInt8 - Emit a byte directive and value.
356 void EmitInt8(int Value) const;
358 /// EmitInt16 - Emit a short directive and value.
360 void EmitInt16(int Value) const;
362 /// EmitInt32 - Emit a long directive and value.
364 void EmitInt32(int Value) const;
366 /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
367 /// in bytes of the directive is specified by Size and Hi/Lo specify the
368 /// labels. This implicitly uses .set if it is available.
369 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
370 unsigned Size) const;
372 /// EmitLabelOffsetDifference - Emit something like ".long Hi+Offset-Lo"
373 /// where the size in bytes of the directive is specified by Size and Hi/Lo
374 /// specify the labels. This implicitly uses .set if it is available.
375 void EmitLabelOffsetDifference(const MCSymbol *Hi, uint64_t Offset,
376 const MCSymbol *Lo, unsigned Size) const;
378 /// EmitLabelPlusOffset - Emit something like ".long Label+Offset"
379 /// where the size in bytes of the directive is specified by Size and Label
380 /// specifies the label. This implicitly uses .set if it is available.
381 void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
383 bool IsSectionRelative = false) const;
385 /// EmitLabelReference - Emit something like ".long Label"
386 /// where the size in bytes of the directive is specified by Size and Label
387 /// specifies the label.
388 void EmitLabelReference(const MCSymbol *Label, unsigned Size,
389 bool IsSectionRelative = false) const {
390 EmitLabelPlusOffset(Label, 0, Size, IsSectionRelative);
393 //===------------------------------------------------------------------===//
394 // Dwarf Emission Helper Routines
395 //===------------------------------------------------------------------===//
397 /// EmitSLEB128 - emit the specified signed leb128 value.
398 void EmitSLEB128(int64_t Value, const char *Desc = nullptr) const;
400 /// EmitULEB128 - emit the specified unsigned leb128 value.
401 void EmitULEB128(uint64_t Value, const char *Desc = nullptr,
402 unsigned PadTo = 0) const;
404 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
405 void EmitCFAByte(unsigned Val) const;
407 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
408 /// encoding. If verbose assembly output is enabled, we output comments
409 /// describing the encoding. Desc is a string saying what the encoding is
410 /// specifying (e.g. "LSDA").
411 void EmitEncodingByte(unsigned Val, const char *Desc = nullptr) const;
413 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
414 unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
416 /// EmitReference - Emit reference to a ttype global with a specified encoding.
417 void EmitTTypeReference(const GlobalValue *GV, unsigned Encoding) const;
419 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of
420 /// its section. This can be done with a special directive if the target
421 /// supports it (e.g. cygwin) or by emitting it as an offset from a label at
422 /// the start of the section.
424 /// SectionLabel is a temporary label emitted at the start of the section
425 /// that Label lives in.
426 void EmitSectionOffset(const MCSymbol *Label,
427 const MCSymbol *SectionLabel) const;
429 /// getISAEncoding - Get the value for DW_AT_APPLE_isa. Zero if no isa
430 /// encoding specified.
431 virtual unsigned getISAEncoding() { return 0; }
433 /// \brief Emit a partial DWARF register operation.
434 /// \param MLoc the register
435 /// \param PieceSize size and
436 /// \param PieceOffset offset of the piece in bits, if this is one
437 /// piece of an aggregate value.
439 /// If size and offset is zero an operation for the entire
440 /// register is emitted: Some targets do not provide a DWARF
441 /// register number for every register. If this is the case, this
442 /// function will attempt to emit a DWARF register by emitting a
443 /// piece of a super-register or by piecing together multiple
444 /// subregisters that alias the register.
445 void EmitDwarfRegOpPiece(ByteStreamer &BS, const MachineLocation &MLoc,
446 unsigned PieceSize = 0,
447 unsigned PieceOffset = 0) const;
449 /// EmitDwarfRegOp - Emit dwarf register operation.
450 /// \param Indirect whether this is a register-indirect address
451 virtual void EmitDwarfRegOp(ByteStreamer &BS, const MachineLocation &MLoc,
452 bool Indirect) const;
454 //===------------------------------------------------------------------===//
455 // Dwarf Lowering Routines
456 //===------------------------------------------------------------------===//
458 /// \brief Emit frame instruction to describe the layout of the frame.
459 void emitCFIInstruction(const MCCFIInstruction &Inst) const;
461 //===------------------------------------------------------------------===//
462 // Inline Asm Support
463 //===------------------------------------------------------------------===//
465 // These are hooks that targets can override to implement inline asm
466 // support. These should probably be moved out of AsmPrinter someday.
468 /// PrintSpecial - Print information related to the specified machine instr
469 /// that is independent of the operand, and may be independent of the instr
470 /// itself. This can be useful for portably encoding the comment character
471 /// or other bits of target-specific knowledge into the asmstrings. The
472 /// syntax used is ${:comment}. Targets can override this to add support
473 /// for their own strange codes.
474 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
475 const char *Code) const;
477 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
478 /// instruction, using the specified assembler variant. Targets should
479 /// override this to format as appropriate. This method can return true if
480 /// the operand is erroneous.
481 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
482 unsigned AsmVariant, const char *ExtraCode,
485 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
486 /// instruction, using the specified assembler variant as an address.
487 /// Targets should override this to format as appropriate. This method can
488 /// return true if the operand is erroneous.
489 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
491 const char *ExtraCode, raw_ostream &OS);
493 /// Let the target do anything it needs to do after emitting inlineasm.
494 /// This callback can be used restore the original mode in case the
495 /// inlineasm contains directives to switch modes.
496 /// \p StartInfo - the original subtarget info before inline asm
497 /// \p EndInfo - the final subtarget info after parsing the inline asm,
498 /// or NULL if the value is unknown.
499 virtual void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
500 const MCSubtargetInfo *EndInfo) const;
503 /// Private state for PrintSpecial()
504 // Assign a unique ID to this machine instruction.
505 mutable const MachineInstr *LastMI;
506 mutable unsigned LastFn;
507 mutable unsigned Counter;
508 mutable unsigned SetCounter;
510 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
511 void EmitInlineAsm(StringRef Str, const MDNode *LocMDNode = nullptr,
512 InlineAsm::AsmDialect AsmDialect =
513 InlineAsm::AD_ATT) const;
515 /// EmitInlineAsm - This method formats and emits the specified machine
516 /// instruction that is an inline asm.
517 void EmitInlineAsm(const MachineInstr *MI) const;
519 //===------------------------------------------------------------------===//
520 // Internal Implementation Details
521 //===------------------------------------------------------------------===//
523 /// EmitVisibility - This emits visibility information about symbol, if
524 /// this is suported by the target.
525 void EmitVisibility(MCSymbol *Sym, unsigned Visibility,
526 bool IsDefinition = true) const;
528 void EmitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const;
530 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
531 const MachineBasicBlock *MBB, unsigned uid) const;
532 void EmitLLVMUsedList(const ConstantArray *InitList);
533 /// Emit llvm.ident metadata in an '.ident' directive.
534 void EmitModuleIdents(Module &M);
535 void EmitXXStructorList(const Constant *List, bool isCtor);
536 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy &C);