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