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