1 //===-- llvm/CodeGen/AsmPrinter.h - AsmPrinter Framework --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
12 // all asm writers.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CODEGEN_ASMPRINTER_H
17 #define LLVM_CODEGEN_ASMPRINTER_H
18 
19 #include "llvm/ADT/MapVector.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/IR/InlineAsm.h"
23 #include "llvm/Support/DataTypes.h"
24 #include "llvm/Support/ErrorHandling.h"
25 
26 namespace llvm {
27 class AsmPrinterHandler;
28 class BlockAddress;
29 class ByteStreamer;
30 class GCStrategy;
31 class Constant;
32 class ConstantArray;
33 class DIE;
34 class DIEAbbrev;
35 class GCMetadataPrinter;
36 class GlobalValue;
37 class GlobalVariable;
38 class MachineBasicBlock;
39 class MachineFunction;
40 class MachineInstr;
41 class MachineLocation;
42 class MachineLoopInfo;
43 class MachineLoop;
44 class MachineConstantPoolValue;
45 class MachineJumpTableInfo;
46 class MachineModuleInfo;
47 class MCAsmInfo;
48 class MCCFIInstruction;
49 class MCContext;
50 class MCExpr;
51 class MCInst;
52 class MCSection;
53 class MCStreamer;
54 class MCSubtargetInfo;
55 class MCSymbol;
56 class MDNode;
57 class DwarfDebug;
58 class Mangler;
59 class TargetLoweringObjectFile;
60 class DataLayout;
61 class TargetMachine;
62 
63 /// This class is intended to be used as a driving class for all asm writers.
64 class AsmPrinter : public MachineFunctionPass {
65 public:
66   /// Target machine description.
67   ///
68   TargetMachine &TM;
69 
70   /// Target Asm Printer information.
71   ///
72   const MCAsmInfo *MAI;
73 
74   /// This is the context for the output file that we are streaming. This owns
75   /// all of the global MC-related objects for the generated translation unit.
76   MCContext &OutContext;
77 
78   /// This is the MCStreamer object for the file we are generating. This
79   /// contains the transient state for the current translation unit that we are
80   /// generating (such as the current section etc).
81   MCStreamer &OutStreamer;
82 
83   /// The current machine function.
84   const MachineFunction *MF;
85 
86   /// This is a pointer to the current MachineModuleInfo.
87   MachineModuleInfo *MMI;
88 
89   /// Name-mangler for global names.
90   ///
91   Mangler *Mang;
92 
93   /// The symbol for the current function. This is recalculated at the beginning
94   /// of each call to runOnMachineFunction().
95   ///
96   MCSymbol *CurrentFnSym;
97 
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;
102 
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;
107 
108 private:
109   MCSymbol *CurrentFnBegin;
110   MCSymbol *CurrentFnEnd;
111   MCSymbol *CurExceptionSym;
112 
113   // The garbage collection metadata printer table.
114   void *GCMetadataPrinters; // Really a DenseMap.
115 
116   /// Emit comments in assembly output if this is true.
117   ///
118   bool VerboseAsm;
119   static char ID;
120 
121   /// If VerboseAsm is set, a pointer to the loop info for this function.
122   MachineLoopInfo *LI;
123 
124   struct HandlerInfo {
125     AsmPrinterHandler *Handler;
126     const char *TimerName, *TimerGroupName;
HandlerInfoHandlerInfo127     HandlerInfo(AsmPrinterHandler *Handler, const char *TimerName,
128                 const char *TimerGroupName)
129         : Handler(Handler), TimerName(TimerName),
130           TimerGroupName(TimerGroupName) {}
131   };
132   /// A vector of all debug/EH info emitters we should use. This vector
133   /// maintains ownership of the emitters.
134   SmallVector<HandlerInfo, 1> Handlers;
135 
136   /// If the target supports dwarf debug info, this pointer is non-null.
137   DwarfDebug *DD;
138 
139 protected:
140   explicit AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
141 
142 public:
143   ~AsmPrinter() override;
144 
getDwarfDebug()145   DwarfDebug *getDwarfDebug() { return DD; }
getDwarfDebug()146   DwarfDebug *getDwarfDebug() const { return DD; }
147 
148   /// Return true if assembly output should contain comments.
149   ///
isVerbose()150   bool isVerbose() const { return VerboseAsm; }
151 
152   /// Return a unique ID for the current function.
153   ///
154   unsigned getFunctionNumber() const;
155 
getFunctionBegin()156   MCSymbol *getFunctionBegin() const { return CurrentFnBegin; }
getFunctionEnd()157   MCSymbol *getFunctionEnd() const { return CurrentFnEnd; }
158   MCSymbol *getCurExceptionSym();
159 
160   /// Return information about object file lowering.
161   const TargetLoweringObjectFile &getObjFileLowering() const;
162 
163   /// Return information about data layout.
164   const DataLayout &getDataLayout() const;
165 
166   /// Return information about subtarget.
167   const MCSubtargetInfo &getSubtargetInfo() const;
168 
169   void EmitToStreamer(MCStreamer &S, const MCInst &Inst);
170 
171   /// Return the target triple string.
172   StringRef getTargetTriple() const;
173 
174   /// Return the current section we are emitting to.
175   const MCSection *getCurrentSection() const;
176 
177   void getNameWithPrefix(SmallVectorImpl<char> &Name,
178                          const GlobalValue *GV) const;
179 
180   MCSymbol *getSymbol(const GlobalValue *GV) const;
181 
182   //===------------------------------------------------------------------===//
183   // MachineFunctionPass Implementation.
184   //===------------------------------------------------------------------===//
185 
186   /// Record analysis usage.
187   ///
188   void getAnalysisUsage(AnalysisUsage &AU) const override;
189 
190   /// Set up the AsmPrinter when we are working on a new module. If your pass
191   /// overrides this, it must make sure to explicitly call this implementation.
192   bool doInitialization(Module &M) override;
193 
194   /// Shut down the asmprinter. If you override this in your pass, you must make
195   /// sure to call it explicitly.
196   bool doFinalization(Module &M) override;
197 
198   /// Emit the specified function out to the OutStreamer.
runOnMachineFunction(MachineFunction & MF)199   bool runOnMachineFunction(MachineFunction &MF) override {
200     SetupMachineFunction(MF);
201     EmitFunctionBody();
202     return false;
203   }
204 
205   //===------------------------------------------------------------------===//
206   // Coarse grained IR lowering routines.
207   //===------------------------------------------------------------------===//
208 
209   /// This should be called when a new MachineFunction is being processed from
210   /// runOnMachineFunction.
211   void SetupMachineFunction(MachineFunction &MF);
212 
213   /// This method emits the body and trailer for a function.
214   void EmitFunctionBody();
215 
216   void emitCFIInstruction(const MachineInstr &MI);
217 
218   void emitFrameAlloc(const MachineInstr &MI);
219 
220   enum CFIMoveType { CFI_M_None, CFI_M_EH, CFI_M_Debug };
221   CFIMoveType needsCFIMoves();
222 
223   bool needsSEHMoves();
224 
225   /// Print to the current output stream assembly representations of the
226   /// constants in the constant pool MCP. This is used to print out constants
227   /// which have been "spilled to memory" by the code generator.
228   ///
229   virtual void EmitConstantPool();
230 
231   /// Print assembly representations of the jump tables used by the current
232   /// function to the current output stream.
233   ///
234   void EmitJumpTableInfo();
235 
236   /// Emit the specified global variable to the .s file.
237   virtual void EmitGlobalVariable(const GlobalVariable *GV);
238 
239   /// Check to see if the specified global is a special global used by LLVM. If
240   /// so, emit it and return true, otherwise do nothing and return false.
241   bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
242 
243   /// Emit an alignment directive to the specified power of two boundary. For
244   /// example, if you pass in 3 here, you will get an 8 byte alignment. If a
245   /// global value is specified, and if that global has an explicit alignment
246   /// requested, it will override the alignment request if required for
247   /// correctness.
248   ///
249   void EmitAlignment(unsigned NumBits, const GlobalObject *GO = nullptr) const;
250 
251   /// Lower the specified LLVM Constant to an MCExpr.
252   const MCExpr *lowerConstant(const Constant *CV);
253 
254   /// \brief Print a general LLVM constant to the .s file.
255   void EmitGlobalConstant(const Constant *CV);
256 
257   /// \brief Unnamed constant global variables solely contaning a pointer to
258   /// another globals variable act like a global variable "proxy", or GOT
259   /// equivalents, i.e., it's only used to hold the address of the latter. One
260   /// optimization is to replace accesses to these proxies by using the GOT
261   /// entry for the final global instead. Hence, we select GOT equivalent
262   /// candidates among all the module global variables, avoid emitting them
263   /// unnecessarily and finally replace references to them by pc relative
264   /// accesses to GOT entries.
265   void computeGlobalGOTEquivs(Module &M);
266 
267   /// \brief Constant expressions using GOT equivalent globals may not be
268   /// eligible for PC relative GOT entry conversion, in such cases we need to
269   /// emit the proxies we previously omitted in EmitGlobalVariable.
270   void emitGlobalGOTEquivs();
271 
272   //===------------------------------------------------------------------===//
273   // Overridable Hooks
274   //===------------------------------------------------------------------===//
275 
276   // Targets can, or in the case of EmitInstruction, must implement these to
277   // customize output.
278 
279   /// This virtual method can be overridden by targets that want to emit
280   /// something at the start of their file.
EmitStartOfAsmFile(Module &)281   virtual void EmitStartOfAsmFile(Module &) {}
282 
283   /// This virtual method can be overridden by targets that want to emit
284   /// something at the end of their file.
EmitEndOfAsmFile(Module &)285   virtual void EmitEndOfAsmFile(Module &) {}
286 
287   /// Targets can override this to emit stuff before the first basic block in
288   /// the function.
EmitFunctionBodyStart()289   virtual void EmitFunctionBodyStart() {}
290 
291   /// Targets can override this to emit stuff after the last basic block in the
292   /// function.
EmitFunctionBodyEnd()293   virtual void EmitFunctionBodyEnd() {}
294 
295   /// Targets can override this to emit stuff at the start of a basic block.
296   /// By default, this method prints the label for the specified
297   /// MachineBasicBlock, an alignment (if present) and a comment describing it
298   /// if appropriate.
299   virtual void EmitBasicBlockStart(const MachineBasicBlock &MBB) const;
300 
301   /// Targets can override this to emit stuff at the end of a basic block.
EmitBasicBlockEnd(const MachineBasicBlock & MBB)302   virtual void EmitBasicBlockEnd(const MachineBasicBlock &MBB) {}
303 
304   /// Targets should implement this to emit instructions.
EmitInstruction(const MachineInstr *)305   virtual void EmitInstruction(const MachineInstr *) {
306     llvm_unreachable("EmitInstruction not implemented");
307   }
308 
309   /// Return the symbol for the specified constant pool entry.
310   virtual MCSymbol *GetCPISymbol(unsigned CPID) const;
311 
312   virtual void EmitFunctionEntryLabel();
313 
314   virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
315 
316   /// Targets can override this to change how global constants that are part of
317   /// a C++ static/global constructor list are emitted.
EmitXXStructor(const Constant * CV)318   virtual void EmitXXStructor(const Constant *CV) { EmitGlobalConstant(CV); }
319 
320   /// Return true if the basic block has exactly one predecessor and the control
321   /// transfer mechanism between the predecessor and this block is a
322   /// fall-through.
323   virtual bool
324   isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
325 
326   /// Targets can override this to customize the output of IMPLICIT_DEF
327   /// instructions in verbose mode.
328   virtual void emitImplicitDef(const MachineInstr *MI) const;
329 
330   //===------------------------------------------------------------------===//
331   // Symbol Lowering Routines.
332   //===------------------------------------------------------------------===//
333 public:
334   MCSymbol *createTempSymbol(const Twine &Name) const;
335 
336   /// Return the MCSymbol for a private symbol with global value name as its
337   /// base, with the specified suffix.
338   MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
339                                          StringRef Suffix) const;
340 
341   /// Return the MCSymbol for the specified ExternalSymbol.
342   MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
343 
344   /// Return the symbol for the specified jump table entry.
345   MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
346 
347   /// Return the symbol for the specified jump table .set
348   /// FIXME: privatize to AsmPrinter.
349   MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
350 
351   /// Return the MCSymbol used to satisfy BlockAddress uses of the specified
352   /// basic block.
353   MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
354   MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
355 
356   //===------------------------------------------------------------------===//
357   // Emission Helper Routines.
358   //===------------------------------------------------------------------===//
359 public:
360   /// This is just convenient handler for printing offsets.
361   void printOffset(int64_t Offset, raw_ostream &OS) const;
362 
363   /// Emit a byte directive and value.
364   ///
365   void EmitInt8(int Value) const;
366 
367   /// Emit a short directive and value.
368   ///
369   void EmitInt16(int Value) const;
370 
371   /// Emit a long directive and value.
372   ///
373   void EmitInt32(int Value) const;
374 
375   /// Emit something like ".long Hi-Lo" where the size in bytes of the directive
376   /// is specified by Size and Hi/Lo specify the labels.  This implicitly uses
377   /// .set if it is available.
378   void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
379                            unsigned Size) const;
380 
381   /// Emit something like ".long Label+Offset" where the size in bytes of the
382   /// directive is specified by Size and Label specifies the label.  This
383   /// implicitly uses .set if it is available.
384   void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
385                            unsigned Size, bool IsSectionRelative = false) const;
386 
387   /// Emit something like ".long Label" where the size in bytes of the directive
388   /// is specified by Size and Label specifies the label.
389   void EmitLabelReference(const MCSymbol *Label, unsigned Size,
390                           bool IsSectionRelative = false) const {
391     EmitLabelPlusOffset(Label, 0, Size, IsSectionRelative);
392   }
393 
394   //===------------------------------------------------------------------===//
395   // Dwarf Emission Helper Routines
396   //===------------------------------------------------------------------===//
397 
398   /// Emit the specified signed leb128 value.
399   void EmitSLEB128(int64_t Value, const char *Desc = nullptr) const;
400 
401   /// Emit the specified unsigned leb128 value.
402   void EmitULEB128(uint64_t Value, const char *Desc = nullptr,
403                    unsigned PadTo = 0) const;
404 
405   /// Emit a .byte 42 directive for a DW_CFA_xxx value.
406   void EmitCFAByte(unsigned Val) const;
407 
408   /// Emit a .byte 42 directive that corresponds to an encoding.  If verbose
409   /// assembly output is enabled, we output comments describing the encoding.
410   /// Desc is a string saying what the encoding is specifying (e.g. "LSDA").
411   void EmitEncodingByte(unsigned Val, const char *Desc = nullptr) const;
412 
413   /// Return the size of the encoding in bytes.
414   unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
415 
416   /// Emit reference to a ttype global with a specified encoding.
417   void EmitTTypeReference(const GlobalValue *GV, unsigned Encoding) const;
418 
419   /// Emit the 4-byte offset of Label from the start of its section.  This can
420   /// be done with a special directive if the target supports it (e.g. cygwin)
421   /// or by emitting it as an offset from a label at the start of the section.
422   void emitSectionOffset(const MCSymbol *Label) const;
423 
424   /// Get the value for DW_AT_APPLE_isa. Zero if no isa encoding specified.
getISAEncoding()425   virtual unsigned getISAEncoding() { return 0; }
426 
427   /// EmitDwarfRegOp - Emit a dwarf register operation.
428   virtual void EmitDwarfRegOp(ByteStreamer &BS,
429                               const MachineLocation &MLoc) const;
430 
431   //===------------------------------------------------------------------===//
432   // Dwarf Lowering Routines
433   //===------------------------------------------------------------------===//
434 
435   /// \brief Emit frame instruction to describe the layout of the frame.
436   void emitCFIInstruction(const MCCFIInstruction &Inst) const;
437 
438   /// \brief Emit Dwarf abbreviation table.
439   void emitDwarfAbbrevs(const std::vector<DIEAbbrev *>& Abbrevs) const;
440 
441   /// \brief Recursively emit Dwarf DIE tree.
442   void emitDwarfDIE(const DIE &Die) const;
443 
444   //===------------------------------------------------------------------===//
445   // Inline Asm Support
446   //===------------------------------------------------------------------===//
447 public:
448   // These are hooks that targets can override to implement inline asm
449   // support.  These should probably be moved out of AsmPrinter someday.
450 
451   /// Print information related to the specified machine instr that is
452   /// independent of the operand, and may be independent of the instr itself.
453   /// This can be useful for portably encoding the comment character or other
454   /// bits of target-specific knowledge into the asmstrings.  The syntax used is
455   /// ${:comment}.  Targets can override this to add support for their own
456   /// strange codes.
457   virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
458                             const char *Code) const;
459 
460   /// Print the specified operand of MI, an INLINEASM instruction, using the
461   /// specified assembler variant.  Targets should override this to format as
462   /// appropriate.  This method can return true if the operand is erroneous.
463   virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
464                                unsigned AsmVariant, const char *ExtraCode,
465                                raw_ostream &OS);
466 
467   /// Print the specified operand of MI, an INLINEASM instruction, using the
468   /// specified assembler variant as an address. Targets should override this to
469   /// format as appropriate.  This method can return true if the operand is
470   /// erroneous.
471   virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
472                                      unsigned AsmVariant, const char *ExtraCode,
473                                      raw_ostream &OS);
474 
475   /// Let the target do anything it needs to do before emitting inlineasm.
476   /// \p StartInfo - the subtarget info before parsing inline asm
477   virtual void emitInlineAsmStart() const;
478 
479   /// Let the target do anything it needs to do after emitting inlineasm.
480   /// This callback can be used restore the original mode in case the
481   /// inlineasm contains directives to switch modes.
482   /// \p StartInfo - the original subtarget info before inline asm
483   /// \p EndInfo   - the final subtarget info after parsing the inline asm,
484   ///                or NULL if the value is unknown.
485   virtual void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
486                                 const MCSubtargetInfo *EndInfo) const;
487 
488 private:
489   /// Private state for PrintSpecial()
490   // Assign a unique ID to this machine instruction.
491   mutable const MachineInstr *LastMI;
492   mutable unsigned LastFn;
493   mutable unsigned Counter;
494 
495   /// This method emits the header for the current function.
496   void EmitFunctionHeader();
497 
498   /// Emit a blob of inline asm to the output streamer.
499   void
500   EmitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
501                 const MDNode *LocMDNode = nullptr,
502                 InlineAsm::AsmDialect AsmDialect = InlineAsm::AD_ATT) const;
503 
504   /// This method formats and emits the specified machine instruction that is an
505   /// inline asm.
506   void EmitInlineAsm(const MachineInstr *MI) const;
507 
508   //===------------------------------------------------------------------===//
509   // Internal Implementation Details
510   //===------------------------------------------------------------------===//
511 
512   /// This emits visibility information about symbol, if this is suported by the
513   /// target.
514   void EmitVisibility(MCSymbol *Sym, unsigned Visibility,
515                       bool IsDefinition = true) const;
516 
517   void EmitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const;
518 
519   void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
520                           const MachineBasicBlock *MBB, unsigned uid) const;
521   void EmitLLVMUsedList(const ConstantArray *InitList);
522   /// Emit llvm.ident metadata in an '.ident' directive.
523   void EmitModuleIdents(Module &M);
524   void EmitXXStructorList(const Constant *List, bool isCtor);
525   GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy &C);
526 };
527 }
528 
529 #endif
530