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