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