• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/CodeGen/MachineFunctionPass.h"
20 #include "llvm/IR/InlineAsm.h"
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Support/ErrorHandling.h"
23 
24 namespace llvm {
25   class BlockAddress;
26   class GCStrategy;
27   class Constant;
28   class GCMetadataPrinter;
29   class GlobalValue;
30   class GlobalVariable;
31   class MachineBasicBlock;
32   class MachineFunction;
33   class MachineInstr;
34   class MachineLocation;
35   class MachineLoopInfo;
36   class MachineLoop;
37   class MachineConstantPoolValue;
38   class MachineJumpTableInfo;
39   class MachineModuleInfo;
40   class MachineMove;
41   class MCAsmInfo;
42   class MCContext;
43   class MCSection;
44   class MCStreamer;
45   class MCSymbol;
46   class MDNode;
47   class DwarfDebug;
48   class DwarfException;
49   class Mangler;
50   class TargetLoweringObjectFile;
51   class DataLayout;
52   class TargetMachine;
53 
54   /// AsmPrinter - This class is intended to be used as a driving class for all
55   /// asm writers.
56   class AsmPrinter : public MachineFunctionPass {
57   public:
58     /// Target machine description.
59     ///
60     TargetMachine &TM;
61 
62     /// Target Asm Printer information.
63     ///
64     const MCAsmInfo *MAI;
65 
66     /// OutContext - This is the context for the output file that we are
67     /// streaming.  This owns all of the global MC-related objects for the
68     /// generated translation unit.
69     MCContext &OutContext;
70 
71     /// OutStreamer - This is the MCStreamer object for the file we are
72     /// generating.  This contains the transient state for the current
73     /// translation unit that we are generating (such as the current section
74     /// etc).
75     MCStreamer &OutStreamer;
76 
77     /// The current machine function.
78     const MachineFunction *MF;
79 
80     /// MMI - This is a pointer to the current MachineModuleInfo.
81     MachineModuleInfo *MMI;
82 
83     /// Name-mangler for global names.
84     ///
85     Mangler *Mang;
86 
87     /// The symbol for the current function. This is recalculated at the
88     /// beginning of each call to runOnMachineFunction().
89     ///
90     MCSymbol *CurrentFnSym;
91 
92     /// The symbol used to represent the start of the current function for the
93     /// purpose of calculating its size (e.g. using the .size directive). By
94     /// default, this is equal to CurrentFnSym.
95     MCSymbol *CurrentFnSymForSize;
96 
97   private:
98     // GCMetadataPrinters - The garbage collection metadata printer table.
99     void *GCMetadataPrinters;  // Really a DenseMap.
100 
101     /// VerboseAsm - Emit comments in assembly output if this is true.
102     ///
103     bool VerboseAsm;
104     static char ID;
105 
106     /// If VerboseAsm is set, a pointer to the loop info for this
107     /// function.
108     MachineLoopInfo *LI;
109 
110     /// DD - If the target supports dwarf debug info, this pointer is non-null.
111     DwarfDebug *DD;
112 
113     /// DE - If the target supports dwarf exception info, this pointer is
114     /// non-null.
115     DwarfException *DE;
116 
117   protected:
118     explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer);
119 
120   public:
121     virtual ~AsmPrinter();
122 
123     /// isVerbose - Return true if assembly output should contain comments.
124     ///
isVerbose()125     bool isVerbose() const { return VerboseAsm; }
126 
127     /// getFunctionNumber - Return a unique ID for the current function.
128     ///
129     unsigned getFunctionNumber() const;
130 
131     /// getObjFileLowering - Return information about object file lowering.
132     const TargetLoweringObjectFile &getObjFileLowering() const;
133 
134     /// getDataLayout - Return information about data layout.
135     const DataLayout &getDataLayout() const;
136 
137     /// getCurrentSection() - Return the current section we are emitting to.
138     const MCSection *getCurrentSection() const;
139 
140 
141     //===------------------------------------------------------------------===//
142     // MachineFunctionPass Implementation.
143     //===------------------------------------------------------------------===//
144 
145     /// getAnalysisUsage - Record analysis usage.
146     ///
147     void getAnalysisUsage(AnalysisUsage &AU) const;
148 
149     /// doInitialization - Set up the AsmPrinter when we are working on a new
150     /// module.  If your pass overrides this, it must make sure to explicitly
151     /// call this implementation.
152     bool doInitialization(Module &M);
153 
154     /// doFinalization - Shut down the asmprinter.  If you override this in your
155     /// pass, you must make sure to call it explicitly.
156     bool doFinalization(Module &M);
157 
158     /// runOnMachineFunction - Emit the specified function out to the
159     /// OutStreamer.
runOnMachineFunction(MachineFunction & MF)160     virtual bool runOnMachineFunction(MachineFunction &MF) {
161       SetupMachineFunction(MF);
162       EmitFunctionHeader();
163       EmitFunctionBody();
164       return false;
165     }
166 
167     //===------------------------------------------------------------------===//
168     // Coarse grained IR lowering routines.
169     //===------------------------------------------------------------------===//
170 
171     /// SetupMachineFunction - This should be called when a new MachineFunction
172     /// is being processed from runOnMachineFunction.
173     void SetupMachineFunction(MachineFunction &MF);
174 
175     /// EmitFunctionHeader - This method emits the header for the current
176     /// function.
177     void EmitFunctionHeader();
178 
179     /// EmitFunctionBody - This method emits the body and trailer for a
180     /// function.
181     void EmitFunctionBody();
182 
183     void emitPrologLabel(const MachineInstr &MI);
184 
185     enum CFIMoveType {
186       CFI_M_None,
187       CFI_M_EH,
188       CFI_M_Debug
189     };
190     CFIMoveType needsCFIMoves();
191 
192     bool needsSEHMoves();
193 
194     /// needsRelocationsForDwarfStringPool - Specifies whether the object format
195     /// expects to use relocations to refer to debug entries. Alternatively we
196     /// emit section offsets in bytes from the start of the string pool.
197     bool needsRelocationsForDwarfStringPool() const;
198 
199     /// EmitConstantPool - Print to the current output stream assembly
200     /// representations of the constants in the constant pool MCP. This is
201     /// used to print out constants which have been "spilled to memory" by
202     /// the code generator.
203     ///
204     virtual void EmitConstantPool();
205 
206     /// EmitJumpTableInfo - Print assembly representations of the jump tables
207     /// used by the current function to the current output stream.
208     ///
209     void EmitJumpTableInfo();
210 
211     /// EmitGlobalVariable - Emit the specified global variable to the .s file.
212     virtual void EmitGlobalVariable(const GlobalVariable *GV);
213 
214     /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
215     /// special global used by LLVM.  If so, emit it and return true, otherwise
216     /// do nothing and return false.
217     bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
218 
219     /// EmitAlignment - Emit an alignment directive to the specified power of
220     /// two boundary.  For example, if you pass in 3 here, you will get an 8
221     /// byte alignment.  If a global value is specified, and if that global has
222     /// an explicit alignment requested, it will override the alignment request
223     /// if required for correctness.
224     ///
225     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
226 
227     /// EmitBasicBlockStart - This method prints the label for the specified
228     /// MachineBasicBlock, an alignment (if present) and a comment describing
229     /// it if appropriate.
230     void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
231 
232     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
233     void EmitGlobalConstant(const Constant *CV, unsigned AddrSpace = 0);
234 
235 
236     //===------------------------------------------------------------------===//
237     // Overridable Hooks
238     //===------------------------------------------------------------------===//
239 
240     // Targets can, or in the case of EmitInstruction, must implement these to
241     // customize output.
242 
243     /// EmitStartOfAsmFile - This virtual method can be overridden by targets
244     /// that want to emit something at the start of their file.
EmitStartOfAsmFile(Module &)245     virtual void EmitStartOfAsmFile(Module &) {}
246 
247     /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
248     /// want to emit something at the end of their file.
EmitEndOfAsmFile(Module &)249     virtual void EmitEndOfAsmFile(Module &) {}
250 
251     /// EmitFunctionBodyStart - Targets can override this to emit stuff before
252     /// the first basic block in the function.
EmitFunctionBodyStart()253     virtual void EmitFunctionBodyStart() {}
254 
255     /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
256     /// the last basic block in the function.
EmitFunctionBodyEnd()257     virtual void EmitFunctionBodyEnd() {}
258 
259     /// EmitInstruction - Targets should implement this to emit instructions.
EmitInstruction(const MachineInstr *)260     virtual void EmitInstruction(const MachineInstr *) {
261       llvm_unreachable("EmitInstruction not implemented");
262     }
263 
264     virtual void EmitFunctionEntryLabel();
265 
266     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
267 
268     /// EmitXXStructor - Targets can override this to change how global
269     /// constants that are part of a C++ static/global constructor list are
270     /// emitted.
EmitXXStructor(const Constant * CV)271     virtual void EmitXXStructor(const Constant *CV) {
272       EmitGlobalConstant(CV);
273     }
274 
275     /// isBlockOnlyReachableByFallthough - Return true if the basic block has
276     /// exactly one predecessor and the control transfer mechanism between
277     /// the predecessor and this block is a fall-through.
278     virtual bool
279     isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
280 
281     //===------------------------------------------------------------------===//
282     // Symbol Lowering Routines.
283     //===------------------------------------------------------------------===//
284   public:
285 
286     /// GetTempSymbol - Return the MCSymbol corresponding to the assembler
287     /// temporary label with the specified stem and unique ID.
288     MCSymbol *GetTempSymbol(StringRef Name, unsigned ID) const;
289 
290     /// GetTempSymbol - Return an assembler temporary label with the specified
291     /// stem.
292     MCSymbol *GetTempSymbol(StringRef Name) const;
293 
294 
295     /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
296     /// global value name as its base, with the specified suffix, and where the
297     /// symbol is forced to have private linkage if ForcePrivate is true.
298     MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
299                                            StringRef Suffix,
300                                            bool ForcePrivate = true) const;
301 
302     /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
303     /// ExternalSymbol.
304     MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
305 
306     /// GetCPISymbol - Return the symbol for the specified constant pool entry.
307     MCSymbol *GetCPISymbol(unsigned CPID) const;
308 
309     /// GetJTISymbol - Return the symbol for the specified jump table entry.
310     MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
311 
312     /// GetJTSetSymbol - Return the symbol for the specified jump table .set
313     /// FIXME: privatize to AsmPrinter.
314     MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
315 
316     /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
317     /// uses of the specified basic block.
318     MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
319     MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
320 
321     //===------------------------------------------------------------------===//
322     // Emission Helper Routines.
323     //===------------------------------------------------------------------===//
324   public:
325     /// printOffset - This is just convenient handler for printing offsets.
326     void printOffset(int64_t Offset, raw_ostream &OS) const;
327 
328     /// EmitInt8 - Emit a byte directive and value.
329     ///
330     void EmitInt8(int Value) const;
331 
332     /// EmitInt16 - Emit a short directive and value.
333     ///
334     void EmitInt16(int Value) const;
335 
336     /// EmitInt32 - Emit a long directive and value.
337     ///
338     void EmitInt32(int Value) const;
339 
340     /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
341     /// in bytes of the directive is specified by Size and Hi/Lo specify the
342     /// labels.  This implicitly uses .set if it is available.
343     void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
344                              unsigned Size) const;
345 
346     /// EmitLabelOffsetDifference - Emit something like ".long Hi+Offset-Lo"
347     /// where the size in bytes of the directive is specified by Size and Hi/Lo
348     /// specify the labels.  This implicitly uses .set if it is available.
349     void EmitLabelOffsetDifference(const MCSymbol *Hi, uint64_t Offset,
350                                    const MCSymbol *Lo, unsigned Size) const;
351 
352     /// EmitLabelPlusOffset - Emit something like ".long Label+Offset"
353     /// where the size in bytes of the directive is specified by Size and Label
354     /// specifies the label.  This implicitly uses .set if it is available.
355     void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
356                                    unsigned Size) const;
357 
358     /// EmitLabelReference - Emit something like ".long Label"
359     /// where the size in bytes of the directive is specified by Size and Label
360     /// specifies the label.
EmitLabelReference(const MCSymbol * Label,unsigned Size)361     void EmitLabelReference(const MCSymbol *Label, unsigned Size) const {
362       EmitLabelPlusOffset(Label, 0, Size);
363     }
364 
365     //===------------------------------------------------------------------===//
366     // Dwarf Emission Helper Routines
367     //===------------------------------------------------------------------===//
368 
369     /// EmitSLEB128 - emit the specified signed leb128 value.
370     void EmitSLEB128(int Value, const char *Desc = 0) const;
371 
372     /// EmitULEB128 - emit the specified unsigned leb128 value.
373     void EmitULEB128(unsigned Value, const char *Desc = 0,
374                      unsigned PadTo = 0) const;
375 
376     /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
377     void EmitCFAByte(unsigned Val) const;
378 
379     /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
380     /// encoding.  If verbose assembly output is enabled, we output comments
381     /// describing the encoding.  Desc is a string saying what the encoding is
382     /// specifying (e.g. "LSDA").
383     void EmitEncodingByte(unsigned Val, const char *Desc = 0) const;
384 
385     /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
386     unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
387 
388     /// EmitReference - Emit reference to a ttype global with a specified encoding.
389     void EmitTTypeReference(const GlobalValue *GV, unsigned Encoding) const;
390 
391     /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of
392     /// its section.  This can be done with a special directive if the target
393     /// supports it (e.g. cygwin) or by emitting it as an offset from a label at
394     /// the start of the section.
395     ///
396     /// SectionLabel is a temporary label emitted at the start of the section
397     /// that Label lives in.
398     void EmitSectionOffset(const MCSymbol *Label,
399                            const MCSymbol *SectionLabel) const;
400 
401     /// getDebugValueLocation - Get location information encoded by DBG_VALUE
402     /// operands.
403     virtual MachineLocation getDebugValueLocation(const MachineInstr *MI) const;
404 
405     /// getISAEncoding - Get the value for DW_AT_APPLE_isa. Zero if no isa
406     /// encoding specified.
getISAEncoding()407     virtual unsigned getISAEncoding() { return 0; }
408 
409     /// EmitDwarfRegOp - Emit dwarf register operation.
410     virtual void EmitDwarfRegOp(const MachineLocation &MLoc) const;
411 
412     //===------------------------------------------------------------------===//
413     // Dwarf Lowering Routines
414     //===------------------------------------------------------------------===//
415 
416     /// EmitCFIFrameMove - Emit frame instruction to describe the layout of the
417     /// frame.
418     void EmitCFIFrameMove(const MachineMove &Move) const;
419 
420     //===------------------------------------------------------------------===//
421     // Inline Asm Support
422     //===------------------------------------------------------------------===//
423   public:
424     // These are hooks that targets can override to implement inline asm
425     // support.  These should probably be moved out of AsmPrinter someday.
426 
427     /// PrintSpecial - Print information related to the specified machine instr
428     /// that is independent of the operand, and may be independent of the instr
429     /// itself.  This can be useful for portably encoding the comment character
430     /// or other bits of target-specific knowledge into the asmstrings.  The
431     /// syntax used is ${:comment}.  Targets can override this to add support
432     /// for their own strange codes.
433     virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
434                               const char *Code) const;
435 
436     /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
437     /// instruction, using the specified assembler variant.  Targets should
438     /// override this to format as appropriate.  This method can return true if
439     /// the operand is erroneous.
440     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
441                                  unsigned AsmVariant, const char *ExtraCode,
442                                  raw_ostream &OS);
443 
444     /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
445     /// instruction, using the specified assembler variant as an address.
446     /// Targets should override this to format as appropriate.  This method can
447     /// return true if the operand is erroneous.
448     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
449                                        unsigned AsmVariant,
450                                        const char *ExtraCode,
451                                        raw_ostream &OS);
452 
453   private:
454     /// Private state for PrintSpecial()
455     // Assign a unique ID to this machine instruction.
456     mutable const MachineInstr *LastMI;
457     mutable unsigned LastFn;
458     mutable unsigned Counter;
459     mutable unsigned SetCounter;
460 
461     /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
462     void EmitInlineAsm(StringRef Str, const MDNode *LocMDNode = 0,
463                     InlineAsm::AsmDialect AsmDialect = InlineAsm::AD_ATT) const;
464 
465     /// EmitInlineAsm - This method formats and emits the specified machine
466     /// instruction that is an inline asm.
467     void EmitInlineAsm(const MachineInstr *MI) const;
468 
469     //===------------------------------------------------------------------===//
470     // Internal Implementation Details
471     //===------------------------------------------------------------------===//
472 
473     /// EmitVisibility - This emits visibility information about symbol, if
474     /// this is suported by the target.
475     void EmitVisibility(MCSymbol *Sym, unsigned Visibility,
476                         bool IsDefinition = true) const;
477 
478     void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const;
479 
480     void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
481                             const MachineBasicBlock *MBB,
482                             unsigned uid) const;
483     void EmitLLVMUsedList(const Constant *List);
484     void EmitXXStructorList(const Constant *List, bool isCtor);
485     GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);
486   };
487 }
488 
489 #endif
490