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