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