Add new instructions for handling data passed into eh landing pad.
[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 #include <set>
22
23 namespace llvm {
24   class Constant;
25   class ConstantArray;
26   class GlobalVariable;
27   class MachineConstantPoolEntry;
28   class MachineConstantPoolValue;
29   class Mangler;
30   class TargetAsmInfo;
31   
32
33   /// AsmPrinter - This class is intended to be used as a driving class for all
34   /// asm writers.
35   class AsmPrinter : public MachineFunctionPass {
36     /// FunctionNumber - This provides a unique ID for each function emitted in
37     /// this translation unit.  It is autoincremented by SetupMachineFunction,
38     /// and can be accessed with getFunctionNumber() and 
39     /// IncrementFunctionNumber().
40     ///
41     unsigned FunctionNumber;
42
43   protected:
44     // Necessary for external weak linkage support
45     std::set<const GlobalValue*> ExtWeakSymbols;
46
47   public:
48     /// Output stream on which we're printing assembly code.
49     ///
50     std::ostream &O;
51
52     /// Target machine description.
53     ///
54     TargetMachine &TM;
55     
56     /// Target Asm Printer information.
57     ///
58     const TargetAsmInfo *TAI;
59
60     /// Name-mangler for global names.
61     ///
62     Mangler *Mang;
63
64     /// Cache of mangled name for current function. This is recalculated at the
65     /// beginning of each call to runOnMachineFunction().
66     ///
67     std::string CurrentFnName;
68     
69     /// CurrentSection - The current section we are emitting to.  This is
70     /// controlled and used by the SwitchSection method.
71     std::string CurrentSection;
72   
73   protected:
74     AsmPrinter(std::ostream &o, TargetMachine &TM, const TargetAsmInfo *T);
75     
76   public:
77     /// SwitchToTextSection - Switch to the specified section of the executable
78     /// if we are not already in it!  If GV is non-null and if the global has an
79     /// explicitly requested section, we switch to the section indicated for the
80     /// global instead of NewSection.
81     ///
82     /// If the new section is an empty string, this method forgets what the
83     /// current section is, but does not emit a .section directive.
84     ///
85     /// This method is used when about to emit executable code.
86     ///
87     void SwitchToTextSection(const char *NewSection, const GlobalValue *GV = NULL);
88
89     /// SwitchToDataSection - Switch to the specified section of the executable
90     /// if we are not already in it!  If GV is non-null and if the global has an
91     /// explicitly requested section, we switch to the section indicated for the
92     /// global instead of NewSection.
93     ///
94     /// If the new section is an empty string, this method forgets what the
95     /// current section is, but does not emit a .section directive.
96     ///
97     /// This method is used when about to emit data.  For most assemblers, this
98     /// is the same as the SwitchToTextSection method, but not all assemblers
99     /// are the same.
100     ///
101     void SwitchToDataSection(const char *NewSection, const GlobalValue *GV = NULL);
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   public:
178     //===------------------------------------------------------------------===//
179     /// LEB 128 number encoding.
180
181     /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
182     /// representing an unsigned leb128 value.
183     void PrintULEB128(unsigned Value) const;
184
185     /// SizeULEB128 - Compute the number of bytes required for an unsigned
186     /// leb128 value.
187     static unsigned SizeULEB128(unsigned Value);
188
189     /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
190     /// representing a signed leb128 value.
191     void PrintSLEB128(int Value) const;
192
193     /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
194     /// value.
195     static unsigned SizeSLEB128(int Value);
196     
197     //===------------------------------------------------------------------===//
198     // Emission and print routines
199     //
200
201     /// PrintHex - Print a value as a hexidecimal value.
202     ///
203     void PrintHex(int Value) const;
204
205     /// EOL - Print a newline character to asm stream.  If a comment is present
206     /// then it will be printed first.  Comments should not contain '\n'.
207     void EOL(const std::string &Comment) const;
208     
209     /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
210     /// unsigned leb128 value.
211     void EmitULEB128Bytes(unsigned Value) const;
212     
213     /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
214     /// signed leb128 value.
215     void EmitSLEB128Bytes(int Value) const;
216     
217     /// EmitInt8 - Emit a byte directive and value.
218     ///
219     void EmitInt8(int Value) const;
220
221     /// EmitInt16 - Emit a short directive and value.
222     ///
223     void EmitInt16(int Value) const;
224
225     /// EmitInt32 - Emit a long directive and value.
226     ///
227     void EmitInt32(int Value) const;
228
229     /// EmitInt64 - Emit a long long directive and value.
230     ///
231     void EmitInt64(uint64_t Value) const;
232
233     /// EmitString - Emit a string with quotes and a null terminator.
234     /// Special characters are emitted properly.
235     /// \literal (Eg. '\t') \endliteral
236     void EmitString(const std::string &String) const;
237     
238     //===------------------------------------------------------------------===//
239
240     /// EmitAlignment - Emit an alignment directive to the specified power of
241     /// two boundary.  For example, if you pass in 3 here, you will get an 8
242     /// byte alignment.  If a global value is specified, and if that global has
243     /// an explicit alignment requested, it will override the alignment request.
244     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
245
246   protected:
247     /// EmitZeros - Emit a block of zeros.
248     ///
249     void EmitZeros(uint64_t NumZeros) const;
250
251     /// EmitString - Emit a zero-byte-terminated string constant.
252     ///
253     virtual void EmitString(const ConstantArray *CVA) const;
254
255     /// EmitConstantValueOnly - Print out the specified constant, without a
256     /// storage class.  Only constants of first-class type are allowed here.
257     void EmitConstantValueOnly(const Constant *CV);
258
259     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
260     ///
261     void EmitGlobalConstant(const Constant* CV);
262
263     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
264     
265     /// printInlineAsm - This method formats and prints the specified machine
266     /// instruction that is an inline asm.
267     void printInlineAsm(const MachineInstr *MI) const;
268     
269     /// printLabel - This method prints a local label used by debug and
270     /// exception handling tables.
271     void printLabel(const MachineInstr *MI) const;
272
273     /// printBasicBlockLabel - This method prints the label for the specified
274     /// MachineBasicBlock
275     virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
276                                       bool printColon = false,
277                                       bool printComment = true) const;
278                                       
279     /// printSetLabel - This method prints a set label for the specified
280     /// MachineBasicBlock
281     void printSetLabel(unsigned uid, const MachineBasicBlock *MBB) const;
282     void printSetLabel(unsigned uid, unsigned uid2,
283                        const MachineBasicBlock *MBB) const;
284
285     /// printDataDirective - This method prints the asm directive for the
286     /// specified type.
287     void printDataDirective(const Type *type);
288
289   private:
290     void EmitLLVMUsedList(Constant *List);
291     void EmitXXStructorList(Constant *List);
292     void EmitConstantPool(unsigned Alignment, const char *Section,
293                 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);
294
295   };
296 }
297
298 #endif