AsmPrinter.h revision af76e592c7f9deff0e55c13dbb4a34f07f1c7f64
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
24namespace llvm {
25  class GCStrategy;
26  class Constant;
27  class ConstantArray;
28  class ConstantFP;
29  class ConstantInt;
30  class ConstantStruct;
31  class ConstantVector;
32  class GCMetadataPrinter;
33  class GlobalValue;
34  class GlobalVariable;
35  class MachineBasicBlock;
36  class MachineFunction;
37  class MachineInstr;
38  class MachineLoopInfo;
39  class MachineLoop;
40  class MachineConstantPool;
41  class MachineConstantPoolEntry;
42  class MachineConstantPoolValue;
43  class MachineJumpTableInfo;
44  class MachineModuleInfo;
45  class MCInst;
46  class MCContext;
47  class MCSection;
48  class MCStreamer;
49  class DwarfWriter;
50  class Mangler;
51  class MCAsmInfo;
52  class TargetLoweringObjectFile;
53  class Type;
54  class formatted_raw_ostream;
55
56  /// AsmPrinter - This class is intended to be used as a driving class for all
57  /// asm writers.
58  class AsmPrinter : public MachineFunctionPass {
59    static char ID;
60
61    /// FunctionNumber - This provides a unique ID for each function emitted in
62    /// this translation unit.  It is autoincremented by SetupMachineFunction,
63    /// and can be accessed with getFunctionNumber() and
64    /// IncrementFunctionNumber().
65    ///
66    unsigned FunctionNumber;
67
68    // GCMetadataPrinters - The garbage collection metadata printer table.
69    typedef DenseMap<GCStrategy*,GCMetadataPrinter*> gcp_map_type;
70    typedef gcp_map_type::iterator gcp_iterator;
71    gcp_map_type GCMetadataPrinters;
72
73    /// If VerboseAsm is set, a pointer to the loop info for this
74    /// function.
75    ///
76    MachineLoopInfo *LI;
77
78  protected:
79    /// MMI - If available, this is a pointer to the current MachineModuleInfo.
80    MachineModuleInfo *MMI;
81
82    /// DW - If available, this is a pointer to the current dwarf writer.
83    DwarfWriter *DW;
84
85  public:
86    /// Output stream on which we're printing assembly code.
87    ///
88    formatted_raw_ostream &O;
89
90    /// Target machine description.
91    ///
92    TargetMachine &TM;
93
94    /// getObjFileLowering - Return information about object file lowering.
95    TargetLoweringObjectFile &getObjFileLowering() const;
96
97    /// Target Asm Printer information.
98    ///
99    const MCAsmInfo *TAI;
100
101    /// Target Register Information.
102    ///
103    const TargetRegisterInfo *TRI;
104
105    /// OutContext - This is the context for the output file that we are
106    /// streaming.  This owns all of the global MC-related objects for the
107    /// generated translation unit.
108    MCContext &OutContext;
109
110    /// OutStreamer - This is the MCStreamer object for the file we are
111    /// generating.  This contains the transient state for the current
112    /// translation unit that we are generating (such as the current section
113    /// etc).
114    MCStreamer &OutStreamer;
115
116    /// The current machine function.
117    const MachineFunction *MF;
118
119    /// Name-mangler for global names.
120    ///
121    Mangler *Mang;
122
123    /// Cache of mangled name for current function. This is recalculated at the
124    /// beginning of each call to runOnMachineFunction().
125    ///
126    std::string CurrentFnName;
127
128    /// getCurrentSection() - Return the current section we are emitting to.
129    const MCSection *getCurrentSection() const;
130
131
132    /// VerboseAsm - Emit comments in assembly output if this is true.
133    ///
134    bool VerboseAsm;
135
136    /// ExuberantAsm - Emit many more comments in assembly output if
137    /// this is true.
138    ///
139    bool ExuberantAsm;
140
141    /// Private state for PrintSpecial()
142    // Assign a unique ID to this machine instruction.
143    mutable const MachineInstr *LastMI;
144    mutable const Function *LastFn;
145    mutable unsigned Counter;
146
147    // Private state for processDebugLock()
148    mutable DebugLocTuple PrevDLT;
149
150  protected:
151    explicit AsmPrinter(formatted_raw_ostream &o, TargetMachine &TM,
152                        const MCAsmInfo *T, bool V);
153
154  public:
155    virtual ~AsmPrinter();
156
157    /// isVerbose - Return true if assembly output should contain comments.
158    ///
159    bool isVerbose() const { return VerboseAsm; }
160
161    /// getGlobalLinkName - Returns the asm/link name of of the specified
162    /// global variable.  Should be overridden by each target asm printer to
163    /// generate the appropriate value.
164    virtual const std::string &getGlobalLinkName(const GlobalVariable *GV,
165                                                 std::string &LinkName) const;
166
167    /// EmitExternalGlobal - Emit the external reference to a global variable.
168    /// Should be overridden if an indirect reference should be used.
169    virtual void EmitExternalGlobal(const GlobalVariable *GV);
170
171    /// getCurrentFunctionEHName - Called to return the CurrentFnEHName.
172    ///
173    std::string getCurrentFunctionEHName(const MachineFunction *MF) const;
174
175  protected:
176    /// getAnalysisUsage - Record analysis usage.
177    ///
178    void getAnalysisUsage(AnalysisUsage &AU) const;
179
180    /// doInitialization - Set up the AsmPrinter when we are working on a new
181    /// module.  If your pass overrides this, it must make sure to explicitly
182    /// call this implementation.
183    bool doInitialization(Module &M);
184
185    /// doFinalization - Shut down the asmprinter.  If you override this in your
186    /// pass, you must make sure to call it explicitly.
187    bool doFinalization(Module &M);
188
189    /// PrintSpecial - Print information related to the specified machine instr
190    /// that is independent of the operand, and may be independent of the instr
191    /// itself.  This can be useful for portably encoding the comment character
192    /// or other bits of target-specific knowledge into the asmstrings.  The
193    /// syntax used is ${:comment}.  Targets can override this to add support
194    /// for their own strange codes.
195    virtual void PrintSpecial(const MachineInstr *MI, const char *Code) const;
196
197    /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
198    /// instruction, using the specified assembler variant.  Targets should
199    /// override this to format as appropriate.  This method can return true if
200    /// the operand is erroneous.
201    virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
202                                 unsigned AsmVariant, const char *ExtraCode);
203
204    /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
205    /// instruction, using the specified assembler variant as an address.
206    /// Targets should override this to format as appropriate.  This method can
207    /// return true if the operand is erroneous.
208    virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
209                                       unsigned AsmVariant,
210                                       const char *ExtraCode);
211
212    /// PrintGlobalVariable - Emit the specified global variable and its
213    /// initializer to the output stream.
214    virtual void PrintGlobalVariable(const GlobalVariable *GV) = 0;
215
216    /// SetupMachineFunction - This should be called when a new MachineFunction
217    /// is being processed from runOnMachineFunction.
218    void SetupMachineFunction(MachineFunction &MF);
219
220    /// getFunctionNumber - Return a unique ID for the current function.
221    ///
222    unsigned getFunctionNumber() const { return FunctionNumber; }
223
224    /// IncrementFunctionNumber - Increase Function Number.  AsmPrinters should
225    /// not normally call this, as the counter is automatically bumped by
226    /// SetupMachineFunction.
227    void IncrementFunctionNumber() { FunctionNumber++; }
228
229    /// EmitConstantPool - Print to the current output stream assembly
230    /// representations of the constants in the constant pool MCP. This is
231    /// used to print out constants which have been "spilled to memory" by
232    /// the code generator.
233    ///
234    void EmitConstantPool(MachineConstantPool *MCP);
235
236    /// EmitJumpTableInfo - Print assembly representations of the jump tables
237    /// used by the current function to the current output stream.
238    ///
239    void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
240
241    /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
242    /// special global used by LLVM.  If so, emit it and return true, otherwise
243    /// do nothing and return false.
244    bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
245
246  public:
247    //===------------------------------------------------------------------===//
248    /// LEB 128 number encoding.
249
250    /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
251    /// representing an unsigned leb128 value.
252    void PrintULEB128(unsigned Value) const;
253
254    /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
255    /// representing a signed leb128 value.
256    void PrintSLEB128(int Value) const;
257
258    //===------------------------------------------------------------------===//
259    // Emission and print routines
260    //
261
262    /// PrintHex - Print a value as a hexidecimal value.
263    ///
264    void PrintHex(int Value) const;
265
266    /// EOL - Print a newline character to asm stream.  If a comment is present
267    /// then it will be printed first.  Comments should not contain '\n'.
268    void EOL() const;
269    void EOL(const std::string &Comment) const;
270    void EOL(const char* Comment) const;
271
272    /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
273    /// unsigned leb128 value.
274    void EmitULEB128Bytes(unsigned Value) const;
275
276    /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
277    /// signed leb128 value.
278    void EmitSLEB128Bytes(int Value) const;
279
280    /// EmitInt8 - Emit a byte directive and value.
281    ///
282    void EmitInt8(int Value) const;
283
284    /// EmitInt16 - Emit a short directive and value.
285    ///
286    void EmitInt16(int Value) const;
287
288    /// EmitInt32 - Emit a long directive and value.
289    ///
290    void EmitInt32(int Value) const;
291
292    /// EmitInt64 - Emit a long long directive and value.
293    ///
294    void EmitInt64(uint64_t Value) const;
295
296    /// EmitString - Emit a string with quotes and a null terminator.
297    /// Special characters are emitted properly.
298    /// @verbatim (Eg. '\t') @endverbatim
299    void EmitString(const std::string &String) const;
300    void EmitString(const char *String, unsigned Size) const;
301
302    /// EmitFile - Emit a .file directive.
303    void EmitFile(unsigned Number, const std::string &Name) const;
304
305    //===------------------------------------------------------------------===//
306
307    /// EmitAlignment - Emit an alignment directive to the specified power of
308    /// two boundary.  For example, if you pass in 3 here, you will get an 8
309    /// byte alignment.  If a global value is specified, and if that global has
310    /// an explicit alignment requested, it will unconditionally override the
311    /// alignment request.  However, if ForcedAlignBits is specified, this value
312    /// has final say: the ultimate alignment will be the max of ForcedAlignBits
313    /// and the alignment computed with NumBits and the global.  If UseFillExpr
314    /// is true, it also emits an optional second value FillValue which the
315    /// assembler uses to fill gaps to match alignment for text sections if the
316    /// has specified a non-zero fill value.
317    ///
318    /// The algorithm is:
319    ///     Align = NumBits;
320    ///     if (GV && GV->hasalignment) Align = GV->getalignment();
321    ///     Align = std::max(Align, ForcedAlignBits);
322    ///
323    void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
324                       unsigned ForcedAlignBits = 0,
325                       bool UseFillExpr = true) const;
326
327    /// printLabel - This method prints a local label used by debug and
328    /// exception handling tables.
329    void printLabel(const MachineInstr *MI) const;
330    void printLabel(unsigned Id) const;
331
332    /// printDeclare - This method prints a local variable declaration used by
333    /// debug tables.
334    void printDeclare(const MachineInstr *MI) const;
335
336    /// EmitComments - Pretty-print comments for instructions
337    void EmitComments(const MachineInstr &MI) const;
338    /// EmitComments - Pretty-print comments for instructions
339    void EmitComments(const MCInst &MI) const;
340    /// EmitComments - Pretty-print comments for basic blocks
341    void EmitComments(const MachineBasicBlock &MBB) const;
342
343    /// printMCInst - Print an MCInst for this target.
344    ///
345    /// Note, this is only a temporary hack to allow the MCStreamer to print
346    /// instructions, do not use this function outside of llvm-mc.
347    virtual void printMCInst(const MCInst *MI);
348
349  protected:
350    /// EmitZeros - Emit a block of zeros.
351    ///
352    void EmitZeros(uint64_t NumZeros, unsigned AddrSpace = 0) const;
353
354    /// EmitString - Emit a zero-byte-terminated string constant.
355    ///
356    virtual void EmitString(const ConstantArray *CVA) const;
357
358    /// EmitConstantValueOnly - Print out the specified constant, without a
359    /// storage class.  Only constants of first-class type are allowed here.
360    void EmitConstantValueOnly(const Constant *CV);
361
362    /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
363    void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
364
365    virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
366
367    /// processDebugLoc - Processes the debug information of each machine
368    /// instruction's DebugLoc.
369    void processDebugLoc(DebugLoc DL);
370
371    /// printInlineAsm - This method formats and prints the specified machine
372    /// instruction that is an inline asm.
373    void printInlineAsm(const MachineInstr *MI) const;
374
375    /// printImplicitDef - This method prints the specified machine instruction
376    /// that is an implicit def.
377    virtual void printImplicitDef(const MachineInstr *MI) const;
378
379    /// printBasicBlockLabel - This method prints the label for the specified
380    /// MachineBasicBlock
381    virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
382                                      bool printAlign = false,
383                                      bool printColon = false,
384                                      bool printComment = true) const;
385
386    /// printPICJumpTableSetLabel - This method prints a set label for the
387    /// specified MachineBasicBlock for a jumptable entry.
388    virtual void printPICJumpTableSetLabel(unsigned uid,
389                                           const MachineBasicBlock *MBB) const;
390    virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
391                                           const MachineBasicBlock *MBB) const;
392    virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
393                                        const MachineBasicBlock *MBB,
394                                        unsigned uid) const;
395
396    /// printDataDirective - This method prints the asm directive for the
397    /// specified type.
398    void printDataDirective(const Type *type, unsigned AddrSpace = 0);
399
400    /// printVisibility - This prints visibility information about symbol, if
401    /// this is suported by the target.
402    void printVisibility(const std::string& Name, unsigned Visibility) const;
403
404    /// printOffset - This is just convenient handler for printing offsets.
405    void printOffset(int64_t Offset) const;
406
407  private:
408    void EmitLLVMUsedList(Constant *List);
409    void EmitXXStructorList(Constant *List);
410    void EmitGlobalConstantStruct(const ConstantStruct* CVS,
411                                  unsigned AddrSpace);
412    void EmitGlobalConstantArray(const ConstantArray* CVA, unsigned AddrSpace);
413    void EmitGlobalConstantVector(const ConstantVector* CP);
414    void EmitGlobalConstantFP(const ConstantFP* CFP, unsigned AddrSpace);
415    void EmitGlobalConstantLargeInt(const ConstantInt* CI, unsigned AddrSpace);
416    GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);
417  };
418}
419
420#endif
421