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