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