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