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.
84 /// Output stream on which we're printing assembly code.
86 formatted_raw_ostream &O;
88 /// Target machine description.
92 /// getObjFileLowering - Return information about object file lowering.
93 TargetLoweringObjectFile &getObjFileLowering() const;
95 /// Target Asm Printer information.
99 /// Target Register Information.
101 const TargetRegisterInfo *TRI;
103 /// OutContext - This is the context for the output file that we are
104 /// streaming. This owns all of the global MC-related objects for the
105 /// generated translation unit.
106 MCContext &OutContext;
108 /// OutStreamer - This is the MCStreamer object for the file we are
109 /// generating. This contains the transient state for the current
110 /// translation unit that we are generating (such as the current section
112 MCStreamer &OutStreamer;
114 /// The current machine function.
115 const MachineFunction *MF;
117 /// Name-mangler for global names.
121 /// The symbol for the current function. This is recalculated at the
122 /// beginning of each call to runOnMachineFunction().
124 MCSymbol *CurrentFnSym;
126 /// getCurrentSection() - Return the current section we are emitting to.
127 const MCSection *getCurrentSection() const;
130 /// VerboseAsm - Emit comments in assembly output if this is true.
134 /// Private state for PrintSpecial()
135 // Assign a unique ID to this machine instruction.
136 mutable const MachineInstr *LastMI;
137 mutable const Function *LastFn;
138 mutable unsigned Counter;
139 mutable unsigned SetCounter;
141 // Private state for processDebugLoc()
142 mutable const MDNode *PrevDLT;
145 explicit AsmPrinter(formatted_raw_ostream &o, TargetMachine &TM,
146 MCContext &Ctx, MCStreamer &Streamer,
150 virtual ~AsmPrinter();
152 /// isVerbose - Return true if assembly output should contain comments.
154 bool isVerbose() const { return VerboseAsm; }
156 /// getFunctionNumber - Return a unique ID for the current function.
158 unsigned getFunctionNumber() const;
161 /// getAnalysisUsage - Record analysis usage.
163 void getAnalysisUsage(AnalysisUsage &AU) const;
165 /// doInitialization - Set up the AsmPrinter when we are working on a new
166 /// module. If your pass overrides this, it must make sure to explicitly
167 /// call this implementation.
168 bool doInitialization(Module &M);
170 /// EmitStartOfAsmFile - This virtual method can be overridden by targets
171 /// that want to emit something at the start of their file.
172 virtual void EmitStartOfAsmFile(Module &) {}
174 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
175 /// want to emit something at the end of their file.
176 virtual void EmitEndOfAsmFile(Module &) {}
178 /// doFinalization - Shut down the asmprinter. If you override this in your
179 /// pass, you must make sure to call it explicitly.
180 bool doFinalization(Module &M);
182 /// PrintSpecial - Print information related to the specified machine instr
183 /// that is independent of the operand, and may be independent of the instr
184 /// itself. This can be useful for portably encoding the comment character
185 /// or other bits of target-specific knowledge into the asmstrings. The
186 /// syntax used is ${:comment}. Targets can override this to add support
187 /// for their own strange codes.
188 virtual void PrintSpecial(const MachineInstr *MI, const char *Code) const;
190 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
191 /// instruction, using the specified assembler variant. Targets should
192 /// override this to format as appropriate. This method can return true if
193 /// the operand is erroneous.
194 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
195 unsigned AsmVariant, const char *ExtraCode);
197 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
198 /// instruction, using the specified assembler variant as an address.
199 /// Targets should override this to format as appropriate. This method can
200 /// return true if the operand is erroneous.
201 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
203 const char *ExtraCode);
205 /// runOnMachineFunction - Emit the specified function out to the
207 virtual bool runOnMachineFunction(MachineFunction &MF) {
208 SetupMachineFunction(MF);
209 EmitFunctionHeader();
214 /// SetupMachineFunction - This should be called when a new MachineFunction
215 /// is being processed from runOnMachineFunction.
216 void SetupMachineFunction(MachineFunction &MF);
218 /// EmitFunctionHeader - This method emits the header for the current
220 void EmitFunctionHeader();
222 /// EmitFunctionBody - This method emits the body and trailer for a
224 void EmitFunctionBody();
226 /// EmitInstruction - Targets should implement this to emit instructions.
227 virtual void EmitInstruction(const MachineInstr *) {
228 assert(0 && "EmitInstruction not implemented");
231 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
232 /// the first basic block in the function.
233 virtual void EmitFunctionBodyStart() {}
235 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
236 /// the last basic block in the function.
237 virtual void EmitFunctionBodyEnd() {}
239 /// EmitConstantPool - Print to the current output stream assembly
240 /// representations of the constants in the constant pool MCP. This is
241 /// used to print out constants which have been "spilled to memory" by
242 /// the code generator.
244 virtual void EmitConstantPool();
246 /// EmitJumpTableInfo - Print assembly representations of the jump tables
247 /// used by the current function to the current output stream.
249 void EmitJumpTableInfo();
251 /// EmitGlobalVariable - Emit the specified global variable to the .s file.
252 virtual void EmitGlobalVariable(const GlobalVariable *GV);
254 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
255 /// special global used by LLVM. If so, emit it and return true, otherwise
256 /// do nothing and return false.
257 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
260 //===------------------------------------------------------------------===//
261 // Emission and print routines
264 /// EmitInt8 - Emit a byte directive and value.
266 void EmitInt8(int Value) const;
268 /// EmitInt16 - Emit a short directive and value.
270 void EmitInt16(int Value) const;
272 /// EmitInt32 - Emit a long directive and value.
274 void EmitInt32(int Value) const;
276 /// EmitInt64 - Emit a long long directive and value.
278 void EmitInt64(uint64_t Value) const;
281 /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
282 /// in bytes of the directive is specified by Size and Hi/Lo specify the
283 /// labels. This implicitly uses .set if it is available.
284 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
285 unsigned Size) const;
287 //===------------------------------------------------------------------===//
289 /// EmitAlignment - Emit an alignment directive to the specified power of
290 /// two boundary. For example, if you pass in 3 here, you will get an 8
291 /// byte alignment. If a global value is specified, and if that global has
292 /// an explicit alignment requested, it will unconditionally override the
293 /// alignment request. However, if ForcedAlignBits is specified, this value
294 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
295 /// and the alignment computed with NumBits and the global. If UseFillExpr
296 /// is true, it also emits an optional second value FillValue which the
297 /// assembler uses to fill gaps to match alignment for text sections if the
298 /// has specified a non-zero fill value.
300 /// The algorithm is:
302 /// if (GV && GV->hasalignment) Align = GV->getalignment();
303 /// Align = std::max(Align, ForcedAlignBits);
305 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
306 unsigned ForcedAlignBits = 0,
307 bool UseFillExpr = true) const;
309 /// printLabel - This method prints a local label used by debug and
310 /// exception handling tables.
311 void printLabel(unsigned Id) const;
313 /// printDeclare - This method prints a local variable declaration used by
315 void printDeclare(const MachineInstr *MI) const;
317 /// GetGlobalValueSymbol - Return the MCSymbol for the specified global
319 MCSymbol *GetGlobalValueSymbol(const GlobalValue *GV) const;
321 /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
322 /// global value name as its base, with the specified suffix, and where the
323 /// symbol is forced to have private linkage if ForcePrivate is true.
324 MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
326 bool ForcePrivate = true) const;
328 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
330 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
332 /// GetCPISymbol - Return the symbol for the specified constant pool entry.
333 MCSymbol *GetCPISymbol(unsigned CPID) const;
335 /// GetJTISymbol - Return the symbol for the specified jump table entry.
336 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
338 /// GetJTSetSymbol - Return the symbol for the specified jump table .set
339 /// FIXME: privatize to AsmPrinter.
340 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
342 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
343 /// uses of the specified basic block.
344 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
345 MCSymbol *GetBlockAddressSymbol(const Function *F,
346 const BasicBlock *BB) const;
348 /// EmitBasicBlockStart - This method prints the label for the specified
349 /// MachineBasicBlock, an alignment (if present) and a comment describing
350 /// it if appropriate.
351 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
356 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
357 void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
360 virtual void EmitFunctionEntryLabel();
362 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
364 /// printOffset - This is just convenient handler for printing offsets.
365 void printOffset(int64_t Offset) const;
367 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
368 /// exactly one predecessor and the control transfer mechanism between
369 /// the predecessor and this block is a fall-through.
370 virtual bool isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
374 /// processDebugLoc - Processes the debug information of each machine
375 /// instruction's DebugLoc.
376 void processDebugLoc(const MachineInstr *MI, bool BeforePrintingInsn);
378 void printLabelInst(const MachineInstr *MI) const;
380 /// printInlineAsm - This method formats and prints the specified machine
381 /// instruction that is an inline asm.
382 void printInlineAsm(const MachineInstr *MI) const;
384 /// printImplicitDef - This method prints the specified machine instruction
385 /// that is an implicit def.
386 void printImplicitDef(const MachineInstr *MI) const;
388 /// printKill - This method prints the specified kill machine instruction.
389 void printKill(const MachineInstr *MI) const;
391 /// EmitVisibility - This emits visibility information about symbol, if
392 /// this is suported by the target.
393 void EmitVisibility(MCSymbol *Sym, unsigned Visibility) const;
395 void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const;
397 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
398 const MachineBasicBlock *MBB,
400 void EmitLLVMUsedList(Constant *List);
401 void EmitXXStructorList(Constant *List);
402 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);