Tidy some #includes and forward-declarations, and move the C binding code
[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 TargetMachine;
58   class Twine;
59   class Type;
60
61   /// AsmPrinter - This class is intended to be used as a driving class for all
62   /// asm writers.
63   class AsmPrinter : public MachineFunctionPass {
64   public:
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     MachineLoopInfo *LI;
111
112     /// DD - If the target supports dwarf debug info, this pointer is non-null.
113     DwarfDebug *DD;
114
115     /// DE - If the target supports dwarf exception info, this pointer is
116     /// non-null.
117     DwarfException *DE;
118
119   protected:
120     explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer);
121
122   public:
123     virtual ~AsmPrinter();
124
125     /// isVerbose - Return true if assembly output should contain comments.
126     ///
127     bool isVerbose() const { return VerboseAsm; }
128
129     /// getFunctionNumber - Return a unique ID for the current function.
130     ///
131     unsigned getFunctionNumber() const;
132
133     /// getObjFileLowering - Return information about object file lowering.
134     const TargetLoweringObjectFile &getObjFileLowering() const;
135
136     /// getTargetData - Return information about data layout.
137     const TargetData &getTargetData() const;
138
139     /// getCurrentSection() - Return the current section we are emitting to.
140     const MCSection *getCurrentSection() const;
141
142
143     //===------------------------------------------------------------------===//
144     // MachineFunctionPass Implementation.
145     //===------------------------------------------------------------------===//
146
147     /// getAnalysisUsage - Record analysis usage.
148     ///
149     void getAnalysisUsage(AnalysisUsage &AU) const;
150
151     /// doInitialization - Set up the AsmPrinter when we are working on a new
152     /// module.  If your pass overrides this, it must make sure to explicitly
153     /// call this implementation.
154     bool doInitialization(Module &M);
155
156     /// doFinalization - Shut down the asmprinter.  If you override this in your
157     /// pass, you must make sure to call it explicitly.
158     bool doFinalization(Module &M);
159
160     /// runOnMachineFunction - Emit the specified function out to the
161     /// OutStreamer.
162     virtual bool runOnMachineFunction(MachineFunction &MF) {
163       SetupMachineFunction(MF);
164       EmitFunctionHeader();
165       EmitFunctionBody();
166       return false;
167     }
168
169     //===------------------------------------------------------------------===//
170     // Coarse grained IR lowering routines.
171     //===------------------------------------------------------------------===//
172
173     /// SetupMachineFunction - This should be called when a new MachineFunction
174     /// is being processed from runOnMachineFunction.
175     void SetupMachineFunction(MachineFunction &MF);
176
177     /// EmitFunctionHeader - This method emits the header for the current
178     /// function.
179     void EmitFunctionHeader();
180
181     /// EmitFunctionBody - This method emits the body and trailer for a
182     /// function.
183     void EmitFunctionBody();
184
185     /// EmitConstantPool - Print to the current output stream assembly
186     /// representations of the constants in the constant pool MCP. This is
187     /// used to print out constants which have been "spilled to memory" by
188     /// the code generator.
189     ///
190     virtual void EmitConstantPool();
191
192     /// EmitJumpTableInfo - Print assembly representations of the jump tables
193     /// used by the current function to the current output stream.
194     ///
195     void EmitJumpTableInfo();
196
197     /// EmitGlobalVariable - Emit the specified global variable to the .s file.
198     virtual void EmitGlobalVariable(const GlobalVariable *GV);
199
200     /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
201     /// special global used by LLVM.  If so, emit it and return true, otherwise
202     /// do nothing and return false.
203     bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
204
205     /// EmitAlignment - Emit an alignment directive to the specified power of
206     /// two boundary.  For example, if you pass in 3 here, you will get an 8
207     /// byte alignment.  If a global value is specified, and if that global has
208     /// an explicit alignment requested, it will override the alignment request
209     /// if required for correctness.
210     ///
211     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
212
213     /// EmitBasicBlockStart - This method prints the label for the specified
214     /// MachineBasicBlock, an alignment (if present) and a comment describing
215     /// it if appropriate.
216     void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
217
218     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
219     void EmitGlobalConstant(const Constant *CV, unsigned AddrSpace = 0);
220
221
222     //===------------------------------------------------------------------===//
223     // Overridable Hooks
224     //===------------------------------------------------------------------===//
225
226     // Targets can, or in the case of EmitInstruction, must implement these to
227     // customize output.
228
229     /// EmitStartOfAsmFile - This virtual method can be overridden by targets
230     /// that want to emit something at the start of their file.
231     virtual void EmitStartOfAsmFile(Module &) {}
232
233     /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
234     /// want to emit something at the end of their file.
235     virtual void EmitEndOfAsmFile(Module &) {}
236
237     /// EmitFunctionBodyStart - Targets can override this to emit stuff before
238     /// the first basic block in the function.
239     virtual void EmitFunctionBodyStart() {}
240
241     /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
242     /// the last basic block in the function.
243     virtual void EmitFunctionBodyEnd() {}
244
245     /// EmitInstruction - Targets should implement this to emit instructions.
246     virtual void EmitInstruction(const MachineInstr *) {
247       assert(0 && "EmitInstruction not implemented");
248     }
249
250     virtual void EmitFunctionEntryLabel();
251
252     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
253
254     /// isBlockOnlyReachableByFallthough - Return true if the basic block has
255     /// exactly one predecessor and the control transfer mechanism between
256     /// the predecessor and this block is a fall-through.
257     virtual bool
258     isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
259
260     //===------------------------------------------------------------------===//
261     // Symbol Lowering Routines.
262     //===------------------------------------------------------------------===//
263   public:
264
265     /// GetTempSymbol - Return the MCSymbol corresponding to the assembler
266     /// temporary label with the specified stem and unique ID.
267     MCSymbol *GetTempSymbol(StringRef Name, unsigned ID) const;
268
269     /// GetTempSymbol - Return an assembler temporary label with the specified
270     /// stem.
271     MCSymbol *GetTempSymbol(StringRef Name) const;
272
273
274     /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
275     /// global value name as its base, with the specified suffix, and where the
276     /// symbol is forced to have private linkage if ForcePrivate is true.
277     MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
278                                            StringRef Suffix,
279                                            bool ForcePrivate = true) const;
280
281     /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
282     /// ExternalSymbol.
283     MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
284
285     /// GetCPISymbol - Return the symbol for the specified constant pool entry.
286     MCSymbol *GetCPISymbol(unsigned CPID) const;
287
288     /// GetJTISymbol - Return the symbol for the specified jump table entry.
289     MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
290
291     /// GetJTSetSymbol - Return the symbol for the specified jump table .set
292     /// FIXME: privatize to AsmPrinter.
293     MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
294
295     /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
296     /// uses of the specified basic block.
297     MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
298     MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
299
300     //===------------------------------------------------------------------===//
301     // Emission Helper Routines.
302     //===------------------------------------------------------------------===//
303   public:
304     /// printOffset - This is just convenient handler for printing offsets.
305     void printOffset(int64_t Offset, raw_ostream &OS) const;
306
307     /// EmitInt8 - Emit a byte directive and value.
308     ///
309     void EmitInt8(int Value) const;
310
311     /// EmitInt16 - Emit a short directive and value.
312     ///
313     void EmitInt16(int Value) const;
314
315     /// EmitInt32 - Emit a long directive and value.
316     ///
317     void EmitInt32(int Value) const;
318
319     /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
320     /// in bytes of the directive is specified by Size and Hi/Lo specify the
321     /// labels.  This implicitly uses .set if it is available.
322     void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
323                              unsigned Size) const;
324
325     /// EmitLabelOffsetDifference - Emit something like ".long Hi+Offset-Lo"
326     /// where the size in bytes of the directive is specified by Size and Hi/Lo
327     /// specify the labels.  This implicitly uses .set if it is available.
328     void EmitLabelOffsetDifference(const MCSymbol *Hi, uint64_t Offset,
329                                    const MCSymbol *Lo, unsigned Size) const;
330
331     //===------------------------------------------------------------------===//
332     // Dwarf Emission Helper Routines
333     //===------------------------------------------------------------------===//
334
335     /// EmitSLEB128 - emit the specified signed leb128 value.
336     void EmitSLEB128(int Value, const char *Desc = 0) const;
337
338     /// EmitULEB128 - emit the specified unsigned leb128 value.
339     void EmitULEB128(unsigned Value, const char *Desc = 0,
340                      unsigned PadTo = 0) const;
341
342     /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
343     void EmitCFAByte(unsigned Val) const;
344
345     /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
346     /// encoding.  If verbose assembly output is enabled, we output comments
347     /// describing the encoding.  Desc is a string saying what the encoding is
348     /// specifying (e.g. "LSDA").
349     void EmitEncodingByte(unsigned Val, const char *Desc = 0) const;
350
351     /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
352     unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
353
354     /// EmitReference - Emit a reference to a label with a specified encoding.
355     ///
356     void EmitReference(const MCSymbol *Sym, unsigned Encoding) const;
357     void EmitReference(const GlobalValue *GV, unsigned Encoding) const;
358
359     /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of
360     /// its section.  This can be done with a special directive if the target
361     /// supports it (e.g. cygwin) or by emitting it as an offset from a label at
362     /// the start of the section.
363     ///
364     /// SectionLabel is a temporary label emitted at the start of the section
365     /// that Label lives in.
366     void EmitSectionOffset(const MCSymbol *Label,
367                            const MCSymbol *SectionLabel) const;
368
369     /// getDebugValueLocation - Get location information encoded by DBG_VALUE
370     /// operands.
371     virtual MachineLocation getDebugValueLocation(const MachineInstr *MI) const;
372
373     /// getISAEncoding - Get the value for DW_AT_APPLE_isa. Zero if no isa
374     /// encoding specified.
375     virtual unsigned getISAEncoding() { return 0; }
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