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