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 /// VerboseAsm - Emit comments in assembly output if this is true.
110 /// getObjFileLowering - Return information about object file lowering.
111 TargetLoweringObjectFile &getObjFileLowering() const;
113 /// getCurrentSection() - Return the current section we are emitting to.
114 const MCSection *getCurrentSection() const;
117 // GCMetadataPrinters - The garbage collection metadata printer table.
118 void *GCMetadataPrinters; // Really a DenseMap.
121 explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer);
124 virtual ~AsmPrinter();
126 /// isVerbose - Return true if assembly output should contain comments.
128 bool isVerbose() const { return VerboseAsm; }
130 /// getFunctionNumber - Return a unique ID for the current function.
132 unsigned getFunctionNumber() const;
134 /// getAnalysisUsage - Record analysis usage.
136 void getAnalysisUsage(AnalysisUsage &AU) const;
138 /// doInitialization - Set up the AsmPrinter when we are working on a new
139 /// module. If your pass overrides this, it must make sure to explicitly
140 /// call this implementation.
141 bool doInitialization(Module &M);
143 /// EmitStartOfAsmFile - This virtual method can be overridden by targets
144 /// that want to emit something at the start of their file.
145 virtual void EmitStartOfAsmFile(Module &) {}
147 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
148 /// want to emit something at the end of their file.
149 virtual void EmitEndOfAsmFile(Module &) {}
151 /// doFinalization - Shut down the asmprinter. If you override this in your
152 /// pass, you must make sure to call it explicitly.
153 bool doFinalization(Module &M);
155 /// runOnMachineFunction - Emit the specified function out to the
157 virtual bool runOnMachineFunction(MachineFunction &MF) {
158 SetupMachineFunction(MF);
159 EmitFunctionHeader();
164 /// SetupMachineFunction - This should be called when a new MachineFunction
165 /// is being processed from runOnMachineFunction.
166 void SetupMachineFunction(MachineFunction &MF);
168 /// EmitFunctionHeader - This method emits the header for the current
170 void EmitFunctionHeader();
172 /// EmitFunctionBody - This method emits the body and trailer for a
174 void EmitFunctionBody();
176 /// EmitInstruction - Targets should implement this to emit instructions.
177 virtual void EmitInstruction(const MachineInstr *) {
178 assert(0 && "EmitInstruction not implemented");
181 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
182 /// the first basic block in the function.
183 virtual void EmitFunctionBodyStart() {}
185 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
186 /// the last basic block in the function.
187 virtual void EmitFunctionBodyEnd() {}
189 /// EmitConstantPool - Print to the current output stream assembly
190 /// representations of the constants in the constant pool MCP. This is
191 /// used to print out constants which have been "spilled to memory" by
192 /// the code generator.
194 virtual void EmitConstantPool();
196 /// EmitJumpTableInfo - Print assembly representations of the jump tables
197 /// used by the current function to the current output stream.
199 void EmitJumpTableInfo();
201 /// EmitGlobalVariable - Emit the specified global variable to the .s file.
202 virtual void EmitGlobalVariable(const GlobalVariable *GV);
204 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
205 /// special global used by LLVM. If so, emit it and return true, otherwise
206 /// do nothing and return false.
207 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
210 //===------------------------------------------------------------------===//
211 // Emission routines.
214 /// EmitInt8 - Emit a byte directive and value.
216 void EmitInt8(int Value) const;
218 /// EmitInt16 - Emit a short directive and value.
220 void EmitInt16(int Value) const;
222 /// EmitInt32 - Emit a long directive and value.
224 void EmitInt32(int Value) const;
226 /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
227 /// in bytes of the directive is specified by Size and Hi/Lo specify the
228 /// labels. This implicitly uses .set if it is available.
229 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
230 unsigned Size) const;
232 //===------------------------------------------------------------------===//
234 /// EmitAlignment - Emit an alignment directive to the specified power of
235 /// two boundary. For example, if you pass in 3 here, you will get an 8
236 /// byte alignment. If a global value is specified, and if that global has
237 /// an explicit alignment requested, it will unconditionally override the
238 /// alignment request. However, if ForcedAlignBits is specified, this value
239 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
240 /// and the alignment computed with NumBits and the global. If UseFillExpr
241 /// is true, it also emits an optional second value FillValue which the
242 /// assembler uses to fill gaps to match alignment for text sections if the
243 /// has specified a non-zero fill value.
245 /// The algorithm is:
247 /// if (GV && GV->hasalignment) Align = GV->getalignment();
248 /// Align = std::max(Align, ForcedAlignBits);
250 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
251 unsigned ForcedAlignBits = 0,
252 bool UseFillExpr = true) const;
254 /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
255 /// global value name as its base, with the specified suffix, and where the
256 /// symbol is forced to have private linkage if ForcePrivate is true.
257 MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
259 bool ForcePrivate = true) const;
261 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
263 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
265 /// GetCPISymbol - Return the symbol for the specified constant pool entry.
266 MCSymbol *GetCPISymbol(unsigned CPID) const;
268 /// GetJTISymbol - Return the symbol for the specified jump table entry.
269 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
271 /// GetJTSetSymbol - Return the symbol for the specified jump table .set
272 /// FIXME: privatize to AsmPrinter.
273 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
275 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
276 /// uses of the specified basic block.
277 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
278 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
280 /// EmitBasicBlockStart - This method prints the label for the specified
281 /// MachineBasicBlock, an alignment (if present) and a comment describing
282 /// it if appropriate.
283 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
288 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
289 void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
292 virtual void EmitFunctionEntryLabel();
294 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
296 /// printOffset - This is just convenient handler for printing offsets.
297 void printOffset(int64_t Offset, raw_ostream &OS) const;
299 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
300 /// exactly one predecessor and the control transfer mechanism between
301 /// the predecessor and this block is a fall-through.
303 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
306 /// EmitImplicitDef - This method emits the specified machine instruction
307 /// that is an implicit def.
308 void EmitImplicitDef(const MachineInstr *MI) const;
310 /// EmitKill - This method emits the specified kill machine instruction.
311 void EmitKill(const MachineInstr *MI) const;
313 /// EmitVisibility - This emits visibility information about symbol, if
314 /// this is suported by the target.
315 void EmitVisibility(MCSymbol *Sym, unsigned Visibility) const;
317 void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const;
319 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
320 const MachineBasicBlock *MBB,
322 void EmitLLVMUsedList(Constant *List);
323 void EmitXXStructorList(Constant *List);
324 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);
326 //===------------------------------------------------------------------===//
327 // Inline Asm Support
328 //===------------------------------------------------------------------===//
330 // These are hooks that targets can override to implement inline asm
331 // support. These should probably be moved out of AsmPrinter someday.
333 /// PrintSpecial - Print information related to the specified machine instr
334 /// that is independent of the operand, and may be independent of the instr
335 /// itself. This can be useful for portably encoding the comment character
336 /// or other bits of target-specific knowledge into the asmstrings. The
337 /// syntax used is ${:comment}. Targets can override this to add support
338 /// for their own strange codes.
339 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
340 const char *Code) const;
342 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
343 /// instruction, using the specified assembler variant. Targets should
344 /// override this to format as appropriate. This method can return true if
345 /// the operand is erroneous.
346 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
347 unsigned AsmVariant, const char *ExtraCode,
350 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
351 /// instruction, using the specified assembler variant as an address.
352 /// Targets should override this to format as appropriate. This method can
353 /// return true if the operand is erroneous.
354 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
356 const char *ExtraCode,
360 /// Private state for PrintSpecial()
361 // Assign a unique ID to this machine instruction.
362 mutable const MachineInstr *LastMI;
363 mutable unsigned LastFn;
364 mutable unsigned Counter;
365 mutable unsigned SetCounter;
367 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
368 void EmitInlineAsm(StringRef Str) const;
370 /// EmitInlineAsm - This method formats and emits the specified machine
371 /// instruction that is an inline asm.
372 void EmitInlineAsm(const MachineInstr *MI) const;