1 //===-- llvm/CodeGen/AsmPrinter.h - AsmPrinter Framework --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CODEGEN_ASMPRINTER_H
17 #define LLVM_CODEGEN_ASMPRINTER_H
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/Support/DebugLoc.h"
21 #include "llvm/Target/TargetMachine.h"
32 class GCMetadataPrinter;
35 class MachineBasicBlock;
36 class MachineFunction;
38 class MachineLoopInfo;
40 class MachineConstantPool;
41 class MachineConstantPoolEntry;
42 class MachineConstantPoolValue;
43 class MachineJumpTableInfo;
44 class MachineModuleInfo;
54 class TargetLoweringObjectFile;
57 class formatted_raw_ostream;
59 /// AsmPrinter - This class is intended to be used as a driving class for all
61 class AsmPrinter : public MachineFunctionPass {
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;
69 /// If VerboseAsm is set, a pointer to the loop info for this
75 /// MMI - If available, this is a pointer to the current MachineModuleInfo.
76 MachineModuleInfo *MMI;
79 /// DW - If available, this is a pointer to the current dwarf writer.
83 /// Flags to specify different kinds of comments to output in
84 /// assembly code. These flags carry semantic information not
85 /// otherwise easily derivable from the IR text.
91 /// Output stream on which we're printing assembly code.
93 formatted_raw_ostream &O;
95 /// Target machine description.
99 /// getObjFileLowering - Return information about object file lowering.
100 TargetLoweringObjectFile &getObjFileLowering() const;
102 /// Target Asm Printer information.
104 const MCAsmInfo *MAI;
106 /// Target Register Information.
108 const TargetRegisterInfo *TRI;
110 /// OutContext - This is the context for the output file that we are
111 /// streaming. This owns all of the global MC-related objects for the
112 /// generated translation unit.
113 MCContext &OutContext;
115 /// OutStreamer - This is the MCStreamer object for the file we are
116 /// generating. This contains the transient state for the current
117 /// translation unit that we are generating (such as the current section
119 MCStreamer &OutStreamer;
121 /// The current machine function.
122 const MachineFunction *MF;
124 /// Name-mangler for global names.
128 /// The symbol for the current function. This is recalculated at the
129 /// beginning of each call to runOnMachineFunction().
131 MCSymbol *CurrentFnSym;
133 /// getCurrentSection() - Return the current section we are emitting to.
134 const MCSection *getCurrentSection() const;
137 /// VerboseAsm - Emit comments in assembly output if this is true.
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;
147 // Private state for processDebugLoc()
148 mutable const MDNode *PrevDLT;
151 explicit AsmPrinter(formatted_raw_ostream &o, TargetMachine &TM,
152 const MCAsmInfo *T, bool V);
155 virtual ~AsmPrinter();
157 /// isVerbose - Return true if assembly output should contain comments.
159 bool isVerbose() const { return VerboseAsm; }
161 /// getFunctionNumber - Return a unique ID for the current function.
163 unsigned getFunctionNumber() const;
166 /// getAnalysisUsage - Record analysis usage.
168 void getAnalysisUsage(AnalysisUsage &AU) const;
170 /// doInitialization - Set up the AsmPrinter when we are working on a new
171 /// module. If your pass overrides this, it must make sure to explicitly
172 /// call this implementation.
173 bool doInitialization(Module &M);
175 /// EmitStartOfAsmFile - This virtual method can be overridden by targets
176 /// that want to emit something at the start of their file.
177 virtual void EmitStartOfAsmFile(Module &) {}
179 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
180 /// want to emit something at the end of their file.
181 virtual void EmitEndOfAsmFile(Module &) {}
183 /// doFinalization - Shut down the asmprinter. If you override this in your
184 /// pass, you must make sure to call it explicitly.
185 bool doFinalization(Module &M);
187 /// PrintSpecial - Print information related to the specified machine instr
188 /// that is independent of the operand, and may be independent of the instr
189 /// itself. This can be useful for portably encoding the comment character
190 /// or other bits of target-specific knowledge into the asmstrings. The
191 /// syntax used is ${:comment}. Targets can override this to add support
192 /// for their own strange codes.
193 virtual void PrintSpecial(const MachineInstr *MI, const char *Code) const;
195 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
196 /// instruction, using the specified assembler variant. Targets should
197 /// override this to format as appropriate. This method can return true if
198 /// the operand is erroneous.
199 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
200 unsigned AsmVariant, const char *ExtraCode);
202 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
203 /// instruction, using the specified assembler variant as an address.
204 /// Targets should override this to format as appropriate. This method can
205 /// return true if the operand is erroneous.
206 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
208 const char *ExtraCode);
210 /// runOnMachineFunction - Emit the specified function out to the
212 virtual bool runOnMachineFunction(MachineFunction &MF) {
213 SetupMachineFunction(MF);
214 EmitFunctionHeader();
219 /// SetupMachineFunction - This should be called when a new MachineFunction
220 /// is being processed from runOnMachineFunction.
221 void SetupMachineFunction(MachineFunction &MF);
223 /// EmitFunctionHeader - This method emits the header for the current
225 void EmitFunctionHeader();
227 /// EmitFunctionBody - This method emits the body and trailer for a
229 void EmitFunctionBody();
231 /// EmitInstruction - Targets should implement this to emit instructions.
232 virtual void EmitInstruction(const MachineInstr *MI) {
233 assert(0 && "EmitInstruction not implemented");
236 /// EmitConstantPool - Print to the current output stream assembly
237 /// representations of the constants in the constant pool MCP. This is
238 /// used to print out constants which have been "spilled to memory" by
239 /// the code generator.
241 virtual void EmitConstantPool();
243 /// EmitJumpTableInfo - Print assembly representations of the jump tables
244 /// used by the current function to the current output stream.
246 void EmitJumpTableInfo();
248 /// EmitGlobalVariable - Emit the specified global variable to the .s file.
249 virtual void EmitGlobalVariable(const GlobalVariable *GV);
251 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
252 /// special global used by LLVM. If so, emit it and return true, otherwise
253 /// do nothing and return false.
254 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
257 //===------------------------------------------------------------------===//
258 // Emission and print routines
261 /// EmitInt8 - Emit a byte directive and value.
263 void EmitInt8(int Value) const;
265 /// EmitInt16 - Emit a short directive and value.
267 void EmitInt16(int Value) const;
269 /// EmitInt32 - Emit a long directive and value.
271 void EmitInt32(int Value) const;
273 /// EmitInt64 - Emit a long long directive and value.
275 void EmitInt64(uint64_t Value) const;
277 //===------------------------------------------------------------------===//
279 /// EmitAlignment - Emit an alignment directive to the specified power of
280 /// two boundary. For example, if you pass in 3 here, you will get an 8
281 /// byte alignment. If a global value is specified, and if that global has
282 /// an explicit alignment requested, it will unconditionally override the
283 /// alignment request. However, if ForcedAlignBits is specified, this value
284 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
285 /// and the alignment computed with NumBits and the global. If UseFillExpr
286 /// is true, it also emits an optional second value FillValue which the
287 /// assembler uses to fill gaps to match alignment for text sections if the
288 /// has specified a non-zero fill value.
290 /// The algorithm is:
292 /// if (GV && GV->hasalignment) Align = GV->getalignment();
293 /// Align = std::max(Align, ForcedAlignBits);
295 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
296 unsigned ForcedAlignBits = 0,
297 bool UseFillExpr = true) const;
299 /// printLabel - This method prints a local label used by debug and
300 /// exception handling tables.
301 void printLabel(const MachineInstr *MI) const;
302 void printLabel(unsigned Id) const;
304 /// printDeclare - This method prints a local variable declaration used by
306 void printDeclare(const MachineInstr *MI) const;
308 /// EmitComments - Pretty-print comments for instructions
309 void EmitComments(const MachineInstr &MI) const;
311 /// GetGlobalValueSymbol - Return the MCSymbol for the specified global
313 MCSymbol *GetGlobalValueSymbol(const GlobalValue *GV) const;
315 /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
316 /// global value name as its base, with the specified suffix, and where the
317 /// symbol is forced to have private linkage if ForcePrivate is true.
318 MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
320 bool ForcePrivate = true) const;
322 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
324 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
326 /// GetCPISymbol - Return the symbol for the specified constant pool entry.
327 MCSymbol *GetCPISymbol(unsigned CPID) const;
329 /// GetJTISymbol - Return the symbol for the specified jump table entry.
330 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
332 /// GetJTSetSymbol - Return the symbol for the specified jump table .set
333 /// FIXME: privatize to AsmPrinter.
334 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
336 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
337 /// uses of the specified basic block.
338 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA,
339 const char *Suffix = "") const;
340 MCSymbol *GetBlockAddressSymbol(const Function *F,
341 const BasicBlock *BB,
342 const char *Suffix = "") const;
344 /// EmitBasicBlockStart - This method prints the label for the specified
345 /// MachineBasicBlock, an alignment (if present) and a comment describing
346 /// it if appropriate.
347 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
352 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
353 void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
356 virtual void EmitFunctionEntryLabel();
358 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
360 /// processDebugLoc - Processes the debug information of each machine
361 /// instruction's DebugLoc.
362 void processDebugLoc(const MachineInstr *MI, bool BeforePrintingInsn);
364 /// printInlineAsm - This method formats and prints the specified machine
365 /// instruction that is an inline asm.
366 void printInlineAsm(const MachineInstr *MI) const;
368 /// printImplicitDef - This method prints the specified machine instruction
369 /// that is an implicit def.
370 void printImplicitDef(const MachineInstr *MI) const;
372 /// printKill - This method prints the specified kill machine instruction.
373 void printKill(const MachineInstr *MI) const;
375 /// printOffset - This is just convenient handler for printing offsets.
376 void printOffset(int64_t Offset) const;
379 /// EmitVisibility - This emits visibility information about symbol, if
380 /// this is suported by the target.
381 void EmitVisibility(MCSymbol *Sym, unsigned Visibility) const;
383 void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const;
385 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
386 const MachineBasicBlock *MBB,
388 void EmitLLVMUsedList(Constant *List);
389 void EmitXXStructorList(Constant *List);
390 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);