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