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