Initial target-independent CodeGen support for BlockAddresses.
[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 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/ADT/DenseMap.h"
23
24 namespace llvm {
25   class BlockAddress;
26   class GCStrategy;
27   class Constant;
28   class ConstantArray;
29   class ConstantFP;
30   class ConstantInt;
31   class ConstantStruct;
32   class ConstantVector;
33   class GCMetadataPrinter;
34   class GlobalValue;
35   class GlobalVariable;
36   class MachineBasicBlock;
37   class MachineFunction;
38   class MachineInstr;
39   class MachineLoopInfo;
40   class MachineLoop;
41   class MachineConstantPool;
42   class MachineConstantPoolEntry;
43   class MachineConstantPoolValue;
44   class MachineJumpTableInfo;
45   class MachineModuleInfo;
46   class MCInst;
47   class MCContext;
48   class MCSection;
49   class MCStreamer;
50   class MCSymbol;
51   class DwarfWriter;
52   class Mangler;
53   class MCAsmInfo;
54   class TargetLoweringObjectFile;
55   class Type;
56   class formatted_raw_ostream;
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     static char ID;
62
63     /// FunctionNumber - This provides a unique ID for each function emitted in
64     /// this translation unit.  It is autoincremented by SetupMachineFunction,
65     /// and can be accessed with getFunctionNumber() and 
66     /// IncrementFunctionNumber().
67     ///
68     unsigned FunctionNumber;
69
70     // GCMetadataPrinters - The garbage collection metadata printer table.
71     typedef DenseMap<GCStrategy*,GCMetadataPrinter*> gcp_map_type;
72     typedef gcp_map_type::iterator gcp_iterator;
73     gcp_map_type GCMetadataPrinters;
74
75     /// If VerboseAsm is set, a pointer to the loop info for this
76     /// function.
77     ///
78     MachineLoopInfo *LI;
79
80   public:
81     /// MMI - If available, this is a pointer to the current MachineModuleInfo.
82     MachineModuleInfo *MMI;
83     
84   protected:
85     /// DW - If available, this is a pointer to the current dwarf writer.
86     DwarfWriter *DW;
87
88   public:
89     /// Output stream on which we're printing assembly code.
90     ///
91     formatted_raw_ostream &O;
92
93     /// Target machine description.
94     ///
95     TargetMachine &TM;
96     
97     /// getObjFileLowering - Return information about object file lowering.
98     TargetLoweringObjectFile &getObjFileLowering() const;
99     
100     /// Target Asm Printer information.
101     ///
102     const MCAsmInfo *MAI;
103
104     /// Target Register Information.
105     ///
106     const TargetRegisterInfo *TRI;
107
108     /// OutContext - This is the context for the output file that we are
109     /// streaming.  This owns all of the global MC-related objects for the
110     /// generated translation unit.
111     MCContext &OutContext;
112     
113     /// OutStreamer - This is the MCStreamer object for the file we are
114     /// generating.  This contains the transient state for the current
115     /// translation unit that we are generating (such as the current section
116     /// etc).
117     MCStreamer &OutStreamer;
118     
119     /// The current machine function.
120     const MachineFunction *MF;
121
122     /// Name-mangler for global names.
123     ///
124     Mangler *Mang;
125
126     /// Cache of mangled name for current function. This is recalculated at the
127     /// beginning of each call to runOnMachineFunction().
128     ///
129     std::string CurrentFnName;
130     
131     /// getCurrentSection() - Return the current section we are emitting to.
132     const MCSection *getCurrentSection() const;
133     
134
135     /// VerboseAsm - Emit comments in assembly output if this is true.
136     ///
137     bool VerboseAsm;
138
139     /// Private state for PrintSpecial()
140     // Assign a unique ID to this machine instruction.
141     mutable const MachineInstr *LastMI;
142     mutable const Function *LastFn;
143     mutable unsigned Counter;
144     
145     // Private state for processDebugLoc()
146     mutable DebugLocTuple PrevDLT;
147
148   protected:
149     explicit AsmPrinter(formatted_raw_ostream &o, TargetMachine &TM,
150                         const MCAsmInfo *T, bool V);
151     
152   public:
153     virtual ~AsmPrinter();
154
155     /// isVerbose - Return true if assembly output should contain comments.
156     ///
157     bool isVerbose() const { return VerboseAsm; }
158
159     /// getFunctionNumber - Return a unique ID for the current function.
160     ///
161     unsigned getFunctionNumber() const { return FunctionNumber; }
162     
163   protected:
164     /// getAnalysisUsage - Record analysis usage.
165     /// 
166     void getAnalysisUsage(AnalysisUsage &AU) const;
167     
168     /// doInitialization - Set up the AsmPrinter when we are working on a new
169     /// module.  If your pass overrides this, it must make sure to explicitly
170     /// call this implementation.
171     bool doInitialization(Module &M);
172
173     /// EmitStartOfAsmFile - This virtual method can be overridden by targets
174     /// that want to emit something at the start of their file.
175     virtual void EmitStartOfAsmFile(Module &M) {}
176     
177     /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
178     /// want to emit something at the end of their file.
179     virtual void EmitEndOfAsmFile(Module &M) {}
180     
181     /// doFinalization - Shut down the asmprinter.  If you override this in your
182     /// pass, you must make sure to call it explicitly.
183     bool doFinalization(Module &M);
184     
185     /// PrintSpecial - Print information related to the specified machine instr
186     /// that is independent of the operand, and may be independent of the instr
187     /// itself.  This can be useful for portably encoding the comment character
188     /// or other bits of target-specific knowledge into the asmstrings.  The
189     /// syntax used is ${:comment}.  Targets can override this to add support
190     /// for their own strange codes.
191     virtual void PrintSpecial(const MachineInstr *MI, const char *Code) const;
192
193     /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
194     /// instruction, using the specified assembler variant.  Targets should
195     /// override this to format as appropriate.  This method can return true if
196     /// the operand is erroneous.
197     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
198                                  unsigned AsmVariant, const char *ExtraCode);
199     
200     /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
201     /// instruction, using the specified assembler variant as an address.
202     /// Targets should override this to format as appropriate.  This method can
203     /// return true if the operand is erroneous.
204     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
205                                        unsigned AsmVariant, 
206                                        const char *ExtraCode);
207     
208     /// PrintGlobalVariable - Emit the specified global variable and its
209     /// initializer to the output stream.
210     virtual void PrintGlobalVariable(const GlobalVariable *GV) = 0;
211
212     /// SetupMachineFunction - This should be called when a new MachineFunction
213     /// is being processed from runOnMachineFunction.
214     void SetupMachineFunction(MachineFunction &MF);
215     
216     /// IncrementFunctionNumber - Increase Function Number.  AsmPrinters should
217     /// not normally call this, as the counter is automatically bumped by
218     /// SetupMachineFunction.
219     void IncrementFunctionNumber() { FunctionNumber++; }
220     
221     /// EmitConstantPool - Print to the current output stream assembly
222     /// representations of the constants in the constant pool MCP. This is
223     /// used to print out constants which have been "spilled to memory" by
224     /// the code generator.
225     ///
226     void EmitConstantPool(MachineConstantPool *MCP);
227
228     /// EmitJumpTableInfo - Print assembly representations of the jump tables 
229     /// used by the current function to the current output stream.  
230     ///
231     void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
232     
233     /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
234     /// special global used by LLVM.  If so, emit it and return true, otherwise
235     /// do nothing and return false.
236     bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
237
238   public:
239     //===------------------------------------------------------------------===//
240     /// LEB 128 number encoding.
241
242     /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
243     /// representing an unsigned leb128 value.
244     void PrintULEB128(unsigned Value) const;
245
246     /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
247     /// representing a signed leb128 value.
248     void PrintSLEB128(int Value) const;
249
250     //===------------------------------------------------------------------===//
251     // Emission and print routines
252     //
253
254     /// PrintHex - Print a value as a hexidecimal value.
255     ///
256     void PrintHex(int Value) const;
257
258     /// EOL - Print a newline character to asm stream.  If a comment is present
259     /// then it will be printed first.  Comments should not contain '\n'.
260     void EOL() const;
261     void EOL(const std::string &Comment) const;
262     void EOL(const char* Comment) const;
263     void EOL(const char *Comment, unsigned Encoding) const;
264
265     /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
266     /// unsigned leb128 value.
267     void EmitULEB128Bytes(unsigned Value) const;
268     
269     /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
270     /// signed leb128 value.
271     void EmitSLEB128Bytes(int Value) const;
272     
273     /// EmitInt8 - Emit a byte directive and value.
274     ///
275     void EmitInt8(int Value) const;
276
277     /// EmitInt16 - Emit a short directive and value.
278     ///
279     void EmitInt16(int Value) const;
280
281     /// EmitInt32 - Emit a long directive and value.
282     ///
283     void EmitInt32(int Value) const;
284
285     /// EmitInt64 - Emit a long long directive and value.
286     ///
287     void EmitInt64(uint64_t Value) const;
288
289     /// EmitString - Emit a string with quotes and a null terminator.
290     /// Special characters are emitted properly.
291     /// @verbatim (Eg. '\t') @endverbatim
292     void EmitString(const std::string &String) const;
293     void EmitString(const char *String, unsigned Size) const;
294
295     /// EmitFile - Emit a .file directive.
296     void EmitFile(unsigned Number, const std::string &Name) const;
297
298     //===------------------------------------------------------------------===//
299
300     /// EmitAlignment - Emit an alignment directive to the specified power of
301     /// two boundary.  For example, if you pass in 3 here, you will get an 8
302     /// byte alignment.  If a global value is specified, and if that global has
303     /// an explicit alignment requested, it will unconditionally override the
304     /// alignment request.  However, if ForcedAlignBits is specified, this value
305     /// has final say: the ultimate alignment will be the max of ForcedAlignBits
306     /// and the alignment computed with NumBits and the global.  If UseFillExpr
307     /// is true, it also emits an optional second value FillValue which the
308     /// assembler uses to fill gaps to match alignment for text sections if the
309     /// has specified a non-zero fill value.
310     ///
311     /// The algorithm is:
312     ///     Align = NumBits;
313     ///     if (GV && GV->hasalignment) Align = GV->getalignment();
314     ///     Align = std::max(Align, ForcedAlignBits);
315     ///
316     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
317                        unsigned ForcedAlignBits = 0,
318                        bool UseFillExpr = true) const;
319
320     /// printLabel - This method prints a local label used by debug and
321     /// exception handling tables.
322     void printLabel(const MachineInstr *MI) const;
323     void printLabel(unsigned Id) const;
324
325     /// printDeclare - This method prints a local variable declaration used by
326     /// debug tables.
327     void printDeclare(const MachineInstr *MI) const;
328
329     /// EmitComments - Pretty-print comments for instructions
330     void EmitComments(const MachineInstr &MI) const;
331     /// EmitComments - Pretty-print comments for basic blocks
332     void EmitComments(const MachineBasicBlock &MBB) const;
333
334     /// GetMBBSymbol - Return the MCSymbol corresponding to the specified basic
335     /// block label.
336     MCSymbol *GetMBBSymbol(unsigned MBBID) const;
337     
338     /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
339     /// uses of the specified basic block.
340     MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
341     MCSymbol *GetBlockAddressSymbol(const Function *F,
342                                     const BasicBlock *BB) const;
343
344     /// EmitBasicBlockStart - This method prints the label for the specified
345     /// MachineBasicBlock, an alignment (if present) and a comment describing
346     /// it if appropriate.
347     void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
348   protected:
349     /// EmitZeros - Emit a block of zeros.
350     ///
351     void EmitZeros(uint64_t NumZeros, unsigned AddrSpace = 0) const;
352
353     /// EmitString - Emit a zero-byte-terminated string constant.
354     ///
355     virtual void EmitString(const ConstantArray *CVA) const;
356
357     /// EmitConstantValueOnly - Print out the specified constant, without a
358     /// storage class.  Only constants of first-class type are allowed here.
359     void EmitConstantValueOnly(const Constant *CV);
360
361     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
362     void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
363
364     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
365
366     /// processDebugLoc - Processes the debug information of each machine
367     /// instruction's DebugLoc. 
368     void processDebugLoc(const MachineInstr *MI, bool BeforePrintingInsn);
369     
370     /// printInlineAsm - This method formats and prints the specified machine
371     /// instruction that is an inline asm.
372     void printInlineAsm(const MachineInstr *MI) const;
373
374     /// printImplicitDef - This method prints the specified machine instruction
375     /// that is an implicit def.
376     virtual void printImplicitDef(const MachineInstr *MI) const;
377     
378     
379     /// printPICJumpTableSetLabel - This method prints a set label for the
380     /// specified MachineBasicBlock for a jumptable entry.
381     virtual void printPICJumpTableSetLabel(unsigned uid,
382                                            const MachineBasicBlock *MBB) const;
383     virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
384                                            const MachineBasicBlock *MBB) const;
385     virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
386                                         const MachineBasicBlock *MBB,
387                                         unsigned uid) const;
388     
389     /// printDataDirective - This method prints the asm directive for the
390     /// specified type.
391     void printDataDirective(const Type *type, unsigned AddrSpace = 0);
392
393     /// printVisibility - This prints visibility information about symbol, if
394     /// this is suported by the target.
395     void printVisibility(const std::string& Name, unsigned Visibility) const;
396
397     /// printOffset - This is just convenient handler for printing offsets.
398     void printOffset(int64_t Offset) const;
399  
400   private:
401     void EmitLLVMUsedList(Constant *List);
402     void EmitXXStructorList(Constant *List);
403     void EmitGlobalConstantStruct(const ConstantStruct* CVS,
404                                   unsigned AddrSpace);
405     void EmitGlobalConstantArray(const ConstantArray* CVA, unsigned AddrSpace);
406     void EmitGlobalConstantVector(const ConstantVector* CP);
407     void EmitGlobalConstantFP(const ConstantFP* CFP, unsigned AddrSpace);
408     void EmitGlobalConstantLargeInt(const ConstantInt* CI, unsigned AddrSpace);
409     GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);
410   };
411 }
412
413 #endif