Clean up interface to getGlobalLinkName.
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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
22 namespace llvm {
23   class Constant;
24   class ConstantArray;
25   class GlobalVariable;
26   class MachineConstantPoolEntry;
27   class MachineConstantPoolValue;
28   class Mangler;
29   class TargetAsmInfo;
30   
31
32   /// AsmPrinter - This class is intended to be used as a driving class for all
33   /// asm writers.
34   class AsmPrinter : public MachineFunctionPass {
35     /// FunctionNumber - This provides a unique ID for each function emitted in
36     /// this translation unit.  It is autoincremented by SetupMachineFunction,
37     /// and can be accessed with getFunctionNumber() and 
38     /// IncrementFunctionNumber().
39     ///
40     unsigned FunctionNumber;
41
42   public:
43     /// Output stream on which we're printing assembly code.
44     ///
45     std::ostream &O;
46
47     /// Target machine description.
48     ///
49     TargetMachine &TM;
50     
51     /// Target Asm Printer information.
52     ///
53     const TargetAsmInfo *TAI;
54
55     /// Name-mangler for global names.
56     ///
57     Mangler *Mang;
58
59     /// Cache of mangled name for current function. This is recalculated at the
60     /// beginning of each call to runOnMachineFunction().
61     ///
62     std::string CurrentFnName;
63     
64     /// CurrentSection - The current section we are emitting to.  This is
65     /// controlled and used by the SwitchSection method.
66     std::string CurrentSection;
67   
68   protected:
69     AsmPrinter(std::ostream &o, TargetMachine &TM, const TargetAsmInfo *T);
70     
71   public:
72     /// SwitchToTextSection - Switch to the specified section of the executable
73     /// if we are not already in it!  If GV is non-null and if the global has an
74     /// explicitly requested section, we switch to the section indicated for the
75     /// global instead of NewSection.
76     ///
77     /// If the new section is an empty string, this method forgets what the
78     /// current section is, but does not emit a .section directive.
79     ///
80     /// This method is used when about to emit executable code.
81     ///
82     void SwitchToTextSection(const char *NewSection, const GlobalValue *GV);
83
84     /// SwitchToDataSection - Switch to the specified section of the executable
85     /// if we are not already in it!  If GV is non-null and if the global has an
86     /// explicitly requested section, we switch to the section indicated for the
87     /// global instead of NewSection.
88     ///
89     /// If the new section is an empty string, this method forgets what the
90     /// current section is, but does not emit a .section directive.
91     ///
92     /// This method is used when about to emit data.  For most assemblers, this
93     /// is the same as the SwitchToTextSection method, but not all assemblers
94     /// are the same.
95     ///
96     void SwitchToDataSection(const char *NewSection, const GlobalValue *GV);
97     
98     /// getPreferredAlignmentLog - Return the preferred alignment of the
99     /// specified global, returned in log form.  This includes an explicitly
100     /// requested alignment (if the global has one).
101     unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
102     
103     /// getGlobalLinkName - Returns the asm/link name of of the specified
104     /// global variable.  Should be overridden by each target asm printer to
105     /// generate the appropriate value.
106     virtual const std::string getGlobalLinkName(const GlobalVariable *GV) const;
107
108   protected:
109     /// doInitialization - Set up the AsmPrinter when we are working on a new
110     /// module.  If your pass overrides this, it must make sure to explicitly
111     /// call this implementation.
112     bool doInitialization(Module &M);
113
114     /// doFinalization - Shut down the asmprinter.  If you override this in your
115     /// pass, you must make sure to call it explicitly.
116     bool doFinalization(Module &M);
117     
118     /// PrintSpecial - Print information related to the specified machine instr
119     /// that is independent of the operand, and may be independent of the instr
120     /// itself.  This can be useful for portably encoding the comment character
121     /// or other bits of target-specific knowledge into the asmstrings.  The
122     /// syntax used is ${:comment}.  Targets can override this to add support
123     /// for their own strange codes.
124     virtual void PrintSpecial(const MachineInstr *MI, const char *Code);
125
126     /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
127     /// instruction, using the specified assembler variant.  Targets should
128     /// override this to format as appropriate.  This method can return true if
129     /// the operand is erroneous.
130     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
131                                  unsigned AsmVariant, const char *ExtraCode);
132     
133     /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
134     /// instruction, using the specified assembler variant as an address.
135     /// Targets should override this to format as appropriate.  This method can
136     /// return true if the operand is erroneous.
137     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
138                                        unsigned AsmVariant, 
139                                        const char *ExtraCode);
140     
141     /// getSectionForFunction - Return the section that we should emit the
142     /// specified function body into.  This defaults to 'TextSection'.  This
143     /// should most likely be overridden by the target to put linkonce/weak
144     /// functions into special sections.
145     virtual std::string getSectionForFunction(const Function &F) const;
146     
147     /// SetupMachineFunction - This should be called when a new MachineFunction
148     /// is being processed from runOnMachineFunction.
149     void SetupMachineFunction(MachineFunction &MF);
150     
151     /// getFunctionNumber - Return a unique ID for the current function.
152     ///
153     unsigned getFunctionNumber() const { return FunctionNumber; }
154     
155     /// IncrementFunctionNumber - Increase Function Number.  AsmPrinters should
156     /// not normally call this, as the counter is automatically bumped by
157     /// SetupMachineFunction.
158     void IncrementFunctionNumber() { FunctionNumber++; }
159     
160     /// EmitConstantPool - Print to the current output stream assembly
161     /// representations of the constants in the constant pool MCP. This is
162     /// used to print out constants which have been "spilled to memory" by
163     /// the code generator.
164     ///
165     void EmitConstantPool(MachineConstantPool *MCP);
166
167     /// EmitJumpTableInfo - Print assembly representations of the jump tables 
168     /// used by the current function to the current output stream.  
169     ///
170     void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
171     
172     /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
173     /// special global used by LLVM.  If so, emit it and return true, otherwise
174     /// do nothing and return false.
175     bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
176     
177     /// EmitAlignment - Emit an alignment directive to the specified power of
178     /// two boundary.  For example, if you pass in 3 here, you will get an 8
179     /// byte alignment.  If a global value is specified, and if that global has
180     /// an explicit alignment requested, it will override the alignment request.
181     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
182
183     /// EmitZeros - Emit a block of zeros.
184     ///
185     void EmitZeros(uint64_t NumZeros) const;
186
187     /// EmitString - Emit a zero-byte-terminated string constant.
188     ///
189     virtual void EmitString(const ConstantArray *CVA) const;
190
191     /// EmitConstantValueOnly - Print out the specified constant, without a
192     /// storage class.  Only constants of first-class type are allowed here.
193     void EmitConstantValueOnly(const Constant *CV);
194
195     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
196     ///
197     void EmitGlobalConstant(const Constant* CV);
198
199     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
200     
201     /// printInlineAsm - This method formats and prints the specified machine
202     /// instruction that is an inline asm.
203     void printInlineAsm(const MachineInstr *MI) const;
204     
205     /// printBasicBlockLabel - This method prints the label for the specified
206     /// MachineBasicBlock
207     virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
208                                       bool printColon = false,
209                                       bool printComment = true) const;
210                                       
211     /// printSetLabel - This method prints a set label for the specified
212     /// MachineBasicBlock
213     void printSetLabel(unsigned uid, const MachineBasicBlock *MBB) const;
214
215     /// printDataDirective - This method prints the asm directive for the
216     /// specified type.
217     void printDataDirective(const Type *type);
218
219   private:
220     void EmitLLVMUsedList(Constant *List);
221     void EmitXXStructorList(Constant *List);
222     void EmitConstantPool(unsigned Alignment, const char *Section,
223                 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);
224
225   };
226 }
227
228 #endif