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"
31 class GCMetadataPrinter;
34 class MachineBasicBlock;
35 class MachineFunction;
37 class MachineLoopInfo;
39 class MachineConstantPool;
40 class MachineConstantPoolEntry;
41 class MachineConstantPoolValue;
42 class MachineJumpTableInfo;
43 class MachineModuleInfo;
52 class TargetLoweringObjectFile;
56 /// AsmPrinter - This class is intended to be used as a driving class for all
58 class AsmPrinter : public MachineFunctionPass {
61 /// If VerboseAsm is set, a pointer to the loop info for this
67 /// DW - If available, this is a pointer to the current dwarf writer.
72 /// Target machine description.
76 /// Target Asm Printer information.
80 /// OutContext - This is the context for the output file that we are
81 /// streaming. This owns all of the global MC-related objects for the
82 /// generated translation unit.
83 MCContext &OutContext;
85 /// OutStreamer - This is the MCStreamer object for the file we are
86 /// generating. This contains the transient state for the current
87 /// translation unit that we are generating (such as the current section
89 MCStreamer &OutStreamer;
91 /// The current machine function.
92 const MachineFunction *MF;
94 /// MMI - This is a pointer to the current MachineModuleInfo.
95 MachineModuleInfo *MMI;
97 /// Name-mangler for global names.
101 /// The symbol for the current function. This is recalculated at the
102 /// beginning of each call to runOnMachineFunction().
104 MCSymbol *CurrentFnSym;
106 /// getCurrentSection() - Return the current section we are emitting to.
107 const MCSection *getCurrentSection() const;
109 /// VerboseAsm - Emit comments in assembly output if this is true.
113 /// getObjFileLowering - Return information about object file lowering.
114 TargetLoweringObjectFile &getObjFileLowering() const;
117 // GCMetadataPrinters - The garbage collection metadata printer table.
118 void *GCMetadataPrinters; // Really a DenseMap.
120 /// Private state for PrintSpecial()
121 // Assign a unique ID to this machine instruction.
122 mutable const MachineInstr *LastMI;
123 mutable const Function *LastFn;
124 mutable unsigned Counter;
125 mutable unsigned SetCounter;
128 explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer);
131 virtual ~AsmPrinter();
133 /// isVerbose - Return true if assembly output should contain comments.
135 bool isVerbose() const { return VerboseAsm; }
137 /// getFunctionNumber - Return a unique ID for the current function.
139 unsigned getFunctionNumber() const;
141 /// getAnalysisUsage - Record analysis usage.
143 void getAnalysisUsage(AnalysisUsage &AU) const;
145 /// doInitialization - Set up the AsmPrinter when we are working on a new
146 /// module. If your pass overrides this, it must make sure to explicitly
147 /// call this implementation.
148 bool doInitialization(Module &M);
150 /// EmitStartOfAsmFile - This virtual method can be overridden by targets
151 /// that want to emit something at the start of their file.
152 virtual void EmitStartOfAsmFile(Module &) {}
154 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
155 /// want to emit something at the end of their file.
156 virtual void EmitEndOfAsmFile(Module &) {}
158 /// doFinalization - Shut down the asmprinter. If you override this in your
159 /// pass, you must make sure to call it explicitly.
160 bool doFinalization(Module &M);
162 /// PrintSpecial - Print information related to the specified machine instr
163 /// that is independent of the operand, and may be independent of the instr
164 /// itself. This can be useful for portably encoding the comment character
165 /// or other bits of target-specific knowledge into the asmstrings. The
166 /// syntax used is ${:comment}. Targets can override this to add support
167 /// for their own strange codes.
168 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
169 const char *Code) const;
171 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
172 /// instruction, using the specified assembler variant. Targets should
173 /// override this to format as appropriate. This method can return true if
174 /// the operand is erroneous.
175 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
176 unsigned AsmVariant, const char *ExtraCode,
179 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
180 /// instruction, using the specified assembler variant as an address.
181 /// Targets should override this to format as appropriate. This method can
182 /// return true if the operand is erroneous.
183 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
185 const char *ExtraCode,
188 /// runOnMachineFunction - Emit the specified function out to the
190 virtual bool runOnMachineFunction(MachineFunction &MF) {
191 SetupMachineFunction(MF);
192 EmitFunctionHeader();
197 /// SetupMachineFunction - This should be called when a new MachineFunction
198 /// is being processed from runOnMachineFunction.
199 void SetupMachineFunction(MachineFunction &MF);
201 /// EmitFunctionHeader - This method emits the header for the current
203 void EmitFunctionHeader();
205 /// EmitFunctionBody - This method emits the body and trailer for a
207 void EmitFunctionBody();
209 /// EmitInstruction - Targets should implement this to emit instructions.
210 virtual void EmitInstruction(const MachineInstr *) {
211 assert(0 && "EmitInstruction not implemented");
214 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
215 /// the first basic block in the function.
216 virtual void EmitFunctionBodyStart() {}
218 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
219 /// the last basic block in the function.
220 virtual void EmitFunctionBodyEnd() {}
222 /// EmitConstantPool - Print to the current output stream assembly
223 /// representations of the constants in the constant pool MCP. This is
224 /// used to print out constants which have been "spilled to memory" by
225 /// the code generator.
227 virtual void EmitConstantPool();
229 /// EmitJumpTableInfo - Print assembly representations of the jump tables
230 /// used by the current function to the current output stream.
232 void EmitJumpTableInfo();
234 /// EmitGlobalVariable - Emit the specified global variable to the .s file.
235 virtual void EmitGlobalVariable(const GlobalVariable *GV);
237 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
238 /// special global used by LLVM. If so, emit it and return true, otherwise
239 /// do nothing and return false.
240 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
243 //===------------------------------------------------------------------===//
244 // Emission routines.
247 /// EmitInt8 - Emit a byte directive and value.
249 void EmitInt8(int Value) const;
251 /// EmitInt16 - Emit a short directive and value.
253 void EmitInt16(int Value) const;
255 /// EmitInt32 - Emit a long directive and value.
257 void EmitInt32(int Value) const;
259 /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
260 /// in bytes of the directive is specified by Size and Hi/Lo specify the
261 /// labels. This implicitly uses .set if it is available.
262 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
263 unsigned Size) const;
265 //===------------------------------------------------------------------===//
267 /// EmitAlignment - Emit an alignment directive to the specified power of
268 /// two boundary. For example, if you pass in 3 here, you will get an 8
269 /// byte alignment. If a global value is specified, and if that global has
270 /// an explicit alignment requested, it will unconditionally override the
271 /// alignment request. However, if ForcedAlignBits is specified, this value
272 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
273 /// and the alignment computed with NumBits and the global. If UseFillExpr
274 /// is true, it also emits an optional second value FillValue which the
275 /// assembler uses to fill gaps to match alignment for text sections if the
276 /// has specified a non-zero fill value.
278 /// The algorithm is:
280 /// if (GV && GV->hasalignment) Align = GV->getalignment();
281 /// Align = std::max(Align, ForcedAlignBits);
283 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
284 unsigned ForcedAlignBits = 0,
285 bool UseFillExpr = true) const;
287 /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
288 /// global value name as its base, with the specified suffix, and where the
289 /// symbol is forced to have private linkage if ForcePrivate is true.
290 MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
292 bool ForcePrivate = true) const;
294 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
296 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
298 /// GetCPISymbol - Return the symbol for the specified constant pool entry.
299 MCSymbol *GetCPISymbol(unsigned CPID) const;
301 /// GetJTISymbol - Return the symbol for the specified jump table entry.
302 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
304 /// GetJTSetSymbol - Return the symbol for the specified jump table .set
305 /// FIXME: privatize to AsmPrinter.
306 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
308 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
309 /// uses of the specified basic block.
310 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
311 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
313 /// EmitBasicBlockStart - This method prints the label for the specified
314 /// MachineBasicBlock, an alignment (if present) and a comment describing
315 /// it if appropriate.
316 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
321 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
322 void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
325 virtual void EmitFunctionEntryLabel();
327 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
329 /// printOffset - This is just convenient handler for printing offsets.
330 void printOffset(int64_t Offset, raw_ostream &OS) const;
332 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
333 /// exactly one predecessor and the control transfer mechanism between
334 /// the predecessor and this block is a fall-through.
336 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
339 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
340 void EmitInlineAsm(StringRef Str) const;
342 /// EmitInlineAsm - This method formats and emits the specified machine
343 /// instruction that is an inline asm.
344 void EmitInlineAsm(const MachineInstr *MI) const;
346 /// EmitImplicitDef - This method emits the specified machine instruction
347 /// that is an implicit def.
348 void EmitImplicitDef(const MachineInstr *MI) const;
350 /// EmitKill - This method emits the specified kill machine instruction.
351 void EmitKill(const MachineInstr *MI) const;
353 /// EmitVisibility - This emits visibility information about symbol, if
354 /// this is suported by the target.
355 void EmitVisibility(MCSymbol *Sym, unsigned Visibility) const;
357 void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const;
359 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
360 const MachineBasicBlock *MBB,
362 void EmitLLVMUsedList(Constant *List);
363 void EmitXXStructorList(Constant *List);
364 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);