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