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