add a new EmitInlineAsm function to asmprinter to handle inline asm.
[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/DebugLoc.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/ADT/DenseMap.h"
23
24 namespace llvm {
25   class BlockAddress;
26   class GCStrategy;
27   class Constant;
28   class ConstantArray;
29   class ConstantFP;
30   class ConstantInt;
31   class ConstantStruct;
32   class ConstantVector;
33   class GCMetadataPrinter;
34   class GlobalValue;
35   class GlobalVariable;
36   class MachineBasicBlock;
37   class MachineFunction;
38   class MachineInstr;
39   class MachineLoopInfo;
40   class MachineLoop;
41   class MachineConstantPool;
42   class MachineConstantPoolEntry;
43   class MachineConstantPoolValue;
44   class MachineJumpTableInfo;
45   class MachineModuleInfo;
46   class MCInst;
47   class MCContext;
48   class MCSection;
49   class MCStreamer;
50   class MCSymbol;
51   class DwarfWriter;
52   class Mangler;
53   class MCAsmInfo;
54   class TargetLoweringObjectFile;
55   class Twine;
56   class Type;
57   class formatted_raw_ostream;
58
59   /// AsmPrinter - This class is intended to be used as a driving class for all
60   /// asm writers.
61   class AsmPrinter : public MachineFunctionPass {
62     static char ID;
63
64     // GCMetadataPrinters - The garbage collection metadata printer table.
65     typedef DenseMap<GCStrategy*,GCMetadataPrinter*> gcp_map_type;
66     typedef gcp_map_type::iterator gcp_iterator;
67     gcp_map_type GCMetadataPrinters;
68
69     /// If VerboseAsm is set, a pointer to the loop info for this
70     /// function.
71     ///
72     MachineLoopInfo *LI;
73
74   public:
75     /// MMI - If available, this is a pointer to the current MachineModuleInfo.
76     MachineModuleInfo *MMI;
77     
78   protected:
79     /// DW - If available, this is a pointer to the current dwarf writer.
80     DwarfWriter *DW;
81
82   public:
83
84     /// Output stream on which we're printing assembly code.
85     ///
86     formatted_raw_ostream &O;
87
88     /// Target machine description.
89     ///
90     TargetMachine &TM;
91     
92     /// getObjFileLowering - Return information about object file lowering.
93     TargetLoweringObjectFile &getObjFileLowering() const;
94     
95     /// Target Asm Printer information.
96     ///
97     const MCAsmInfo *MAI;
98
99     /// Target Register Information.
100     ///
101     const TargetRegisterInfo *TRI;
102
103     /// OutContext - This is the context for the output file that we are
104     /// streaming.  This owns all of the global MC-related objects for the
105     /// generated translation unit.
106     MCContext &OutContext;
107     
108     /// OutStreamer - This is the MCStreamer object for the file we are
109     /// generating.  This contains the transient state for the current
110     /// translation unit that we are generating (such as the current section
111     /// etc).
112     MCStreamer &OutStreamer;
113     
114     /// The current machine function.
115     const MachineFunction *MF;
116
117     /// Name-mangler for global names.
118     ///
119     Mangler *Mang;
120
121     /// The symbol for the current function. This is recalculated at the
122     /// beginning of each call to runOnMachineFunction().
123     ///
124     MCSymbol *CurrentFnSym;
125     
126     /// getCurrentSection() - Return the current section we are emitting to.
127     const MCSection *getCurrentSection() const;
128     
129
130     /// VerboseAsm - Emit comments in assembly output if this is true.
131     ///
132     bool VerboseAsm;
133
134     /// Private state for PrintSpecial()
135     // Assign a unique ID to this machine instruction.
136     mutable const MachineInstr *LastMI;
137     mutable const Function *LastFn;
138     mutable unsigned Counter;
139     mutable unsigned SetCounter;
140     
141   protected:
142     explicit AsmPrinter(formatted_raw_ostream &o, TargetMachine &TM,
143                         MCStreamer &Streamer);
144     
145   public:
146     virtual ~AsmPrinter();
147
148     /// isVerbose - Return true if assembly output should contain comments.
149     ///
150     bool isVerbose() const { return VerboseAsm; }
151
152     /// getFunctionNumber - Return a unique ID for the current function.
153     ///
154     unsigned getFunctionNumber() const;
155     
156   protected:
157     /// getAnalysisUsage - Record analysis usage.
158     /// 
159     void getAnalysisUsage(AnalysisUsage &AU) const;
160     
161     /// doInitialization - Set up the AsmPrinter when we are working on a new
162     /// module.  If your pass overrides this, it must make sure to explicitly
163     /// call this implementation.
164     bool doInitialization(Module &M);
165
166     /// EmitStartOfAsmFile - This virtual method can be overridden by targets
167     /// that want to emit something at the start of their file.
168     virtual void EmitStartOfAsmFile(Module &) {}
169     
170     /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
171     /// want to emit something at the end of their file.
172     virtual void EmitEndOfAsmFile(Module &) {}
173     
174     /// doFinalization - Shut down the asmprinter.  If you override this in your
175     /// pass, you must make sure to call it explicitly.
176     bool doFinalization(Module &M);
177     
178     /// PrintSpecial - Print information related to the specified machine instr
179     /// that is independent of the operand, and may be independent of the instr
180     /// itself.  This can be useful for portably encoding the comment character
181     /// or other bits of target-specific knowledge into the asmstrings.  The
182     /// syntax used is ${:comment}.  Targets can override this to add support
183     /// for their own strange codes.
184     virtual void PrintSpecial(const MachineInstr *MI, const char *Code) const;
185
186     /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
187     /// instruction, using the specified assembler variant.  Targets should
188     /// override this to format as appropriate.  This method can return true if
189     /// the operand is erroneous.
190     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
191                                  unsigned AsmVariant, const char *ExtraCode);
192     
193     /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
194     /// instruction, using the specified assembler variant as an address.
195     /// Targets should override this to format as appropriate.  This method can
196     /// return true if the operand is erroneous.
197     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
198                                        unsigned AsmVariant, 
199                                        const char *ExtraCode);
200     
201     /// runOnMachineFunction - Emit the specified function out to the
202     /// OutStreamer.
203     virtual bool runOnMachineFunction(MachineFunction &MF) {
204       SetupMachineFunction(MF);
205       EmitFunctionHeader();
206       EmitFunctionBody();
207       return false;
208     }      
209     
210     /// SetupMachineFunction - This should be called when a new MachineFunction
211     /// is being processed from runOnMachineFunction.
212     void SetupMachineFunction(MachineFunction &MF);
213     
214     /// EmitFunctionHeader - This method emits the header for the current
215     /// function.
216     void EmitFunctionHeader();
217     
218     /// EmitFunctionBody - This method emits the body and trailer for a
219     /// function.
220     void EmitFunctionBody();
221
222     /// EmitInstruction - Targets should implement this to emit instructions.
223     virtual void EmitInstruction(const MachineInstr *) {
224       assert(0 && "EmitInstruction not implemented");
225     }
226     
227     /// EmitFunctionBodyStart - Targets can override this to emit stuff before
228     /// the first basic block in the function.
229     virtual void EmitFunctionBodyStart() {}
230
231     /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
232     /// the last basic block in the function.
233     virtual void EmitFunctionBodyEnd() {}
234     
235     /// EmitConstantPool - Print to the current output stream assembly
236     /// representations of the constants in the constant pool MCP. This is
237     /// used to print out constants which have been "spilled to memory" by
238     /// the code generator.
239     ///
240     virtual void EmitConstantPool();
241     
242     /// EmitJumpTableInfo - Print assembly representations of the jump tables 
243     /// used by the current function to the current output stream.  
244     ///
245     void EmitJumpTableInfo();
246     
247     /// EmitGlobalVariable - Emit the specified global variable to the .s file.
248     virtual void EmitGlobalVariable(const GlobalVariable *GV);
249     
250     /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
251     /// special global used by LLVM.  If so, emit it and return true, otherwise
252     /// do nothing and return false.
253     bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
254
255   public:
256     //===------------------------------------------------------------------===//
257     // Emission and print routines
258     //
259
260     /// EmitInt8 - Emit a byte directive and value.
261     ///
262     void EmitInt8(int Value) const;
263
264     /// EmitInt16 - Emit a short directive and value.
265     ///
266     void EmitInt16(int Value) const;
267
268     /// EmitInt32 - Emit a long directive and value.
269     ///
270     void EmitInt32(int Value) const;
271
272     /// EmitInt64 - Emit a long long directive and value.
273     ///
274     void EmitInt64(uint64_t Value) const;
275     
276     
277     /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
278     /// in bytes of the directive is specified by Size and Hi/Lo specify the
279     /// labels.  This implicitly uses .set if it is available.
280     void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
281                              unsigned Size) const;
282
283     //===------------------------------------------------------------------===//
284
285     /// EmitAlignment - Emit an alignment directive to the specified power of
286     /// two boundary.  For example, if you pass in 3 here, you will get an 8
287     /// byte alignment.  If a global value is specified, and if that global has
288     /// an explicit alignment requested, it will unconditionally override the
289     /// alignment request.  However, if ForcedAlignBits is specified, this value
290     /// has final say: the ultimate alignment will be the max of ForcedAlignBits
291     /// and the alignment computed with NumBits and the global.  If UseFillExpr
292     /// is true, it also emits an optional second value FillValue which the
293     /// assembler uses to fill gaps to match alignment for text sections if the
294     /// has specified a non-zero fill value.
295     ///
296     /// The algorithm is:
297     ///     Align = NumBits;
298     ///     if (GV && GV->hasalignment) Align = GV->getalignment();
299     ///     Align = std::max(Align, ForcedAlignBits);
300     ///
301     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
302                        unsigned ForcedAlignBits = 0,
303                        bool UseFillExpr = true) const;
304
305     /// printDeclare - This method prints a local variable declaration used by
306     /// debug tables.
307     void printDeclare(const MachineInstr *MI) const;
308
309     /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
310     /// global value name as its base, with the specified suffix, and where the
311     /// symbol is forced to have private linkage if ForcePrivate is true.
312     MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
313                                            StringRef Suffix,
314                                            bool ForcePrivate = true) const;
315     
316     /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
317     /// ExternalSymbol.
318     MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
319     
320     /// GetCPISymbol - Return the symbol for the specified constant pool entry.
321     MCSymbol *GetCPISymbol(unsigned CPID) const;
322
323     /// GetJTISymbol - Return the symbol for the specified jump table entry.
324     MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
325
326     /// GetJTSetSymbol - Return the symbol for the specified jump table .set
327     /// FIXME: privatize to AsmPrinter.
328     MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
329
330     /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
331     /// uses of the specified basic block.
332     MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
333     MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
334
335     /// EmitBasicBlockStart - This method prints the label for the specified
336     /// MachineBasicBlock, an alignment (if present) and a comment describing
337     /// it if appropriate.
338     void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
339     
340     
341     // Data emission.
342     
343     /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
344     void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
345     
346   protected:
347     virtual void EmitFunctionEntryLabel();
348     
349     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
350
351     /// printOffset - This is just convenient handler for printing offsets.
352     void printOffset(int64_t Offset) const;
353
354     /// isBlockOnlyReachableByFallthough - Return true if the basic block has
355     /// exactly one predecessor and the control transfer mechanism between
356     /// the predecessor and this block is a fall-through.
357     virtual bool
358     isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
359
360   private:
361     /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
362     void EmitInlineAsm(StringRef Str);
363
364     /// processDebugLoc - Processes the debug information of each machine
365     /// instruction's DebugLoc. 
366     void processDebugLoc(const MachineInstr *MI, bool BeforePrintingInsn);
367     
368     void printLabelInst(const MachineInstr *MI) const;
369
370     /// printInlineAsm - This method formats and prints the specified machine
371     /// instruction that is an inline asm.
372     void printInlineAsm(const MachineInstr *MI) const;
373
374     /// printImplicitDef - This method prints the specified machine instruction
375     /// that is an implicit def.
376     void printImplicitDef(const MachineInstr *MI) const;
377
378     /// printKill - This method prints the specified kill machine instruction.
379     void printKill(const MachineInstr *MI) const;
380
381     /// EmitVisibility - This emits visibility information about symbol, if
382     /// this is suported by the target.
383     void EmitVisibility(MCSymbol *Sym, unsigned Visibility) const;
384     
385     void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const;
386     
387     void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
388                             const MachineBasicBlock *MBB,
389                             unsigned uid) const;
390     void EmitLLVMUsedList(Constant *List);
391     void EmitXXStructorList(Constant *List);
392     GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);
393   };
394 }
395
396 #endif