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;
53 class TargetLoweringObjectFile;
57 /// AsmPrinter - This class is intended to be used as a driving class for all
59 class AsmPrinter : public MachineFunctionPass {
62 /// If VerboseAsm is set, a pointer to the loop info for this
67 /// MMI - This is a pointer to the current MachineModuleInfo.
68 MachineModuleInfo *MMI;
71 /// DW - If available, this is a pointer to the current dwarf writer.
76 /// Target machine description.
80 /// getObjFileLowering - Return information about object file lowering.
81 TargetLoweringObjectFile &getObjFileLowering() const;
83 /// Target Asm Printer information.
87 /// Target Register Information.
89 const TargetRegisterInfo *TRI;
91 /// OutContext - This is the context for the output file that we are
92 /// streaming. This owns all of the global MC-related objects for the
93 /// generated translation unit.
94 MCContext &OutContext;
96 /// OutStreamer - This is the MCStreamer object for the file we are
97 /// generating. This contains the transient state for the current
98 /// translation unit that we are generating (such as the current section
100 MCStreamer &OutStreamer;
102 /// The current machine function.
103 const MachineFunction *MF;
105 /// Name-mangler for global names.
109 /// The symbol for the current function. This is recalculated at the
110 /// beginning of each call to runOnMachineFunction().
112 MCSymbol *CurrentFnSym;
114 /// getCurrentSection() - Return the current section we are emitting to.
115 const MCSection *getCurrentSection() const;
118 /// VerboseAsm - Emit comments in assembly output if this is true.
124 // GCMetadataPrinters - The garbage collection metadata printer table.
125 void *GCMetadataPrinters; // Really a DenseMap.
127 /// Private state for PrintSpecial()
128 // Assign a unique ID to this machine instruction.
129 mutable const MachineInstr *LastMI;
130 mutable const Function *LastFn;
131 mutable unsigned Counter;
132 mutable unsigned SetCounter;
135 explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer);
138 virtual ~AsmPrinter();
140 /// isVerbose - Return true if assembly output should contain comments.
142 bool isVerbose() const { return VerboseAsm; }
144 /// getFunctionNumber - Return a unique ID for the current function.
146 unsigned getFunctionNumber() const;
149 /// getAnalysisUsage - Record analysis usage.
151 void getAnalysisUsage(AnalysisUsage &AU) const;
153 /// doInitialization - Set up the AsmPrinter when we are working on a new
154 /// module. If your pass overrides this, it must make sure to explicitly
155 /// call this implementation.
156 bool doInitialization(Module &M);
158 /// EmitStartOfAsmFile - This virtual method can be overridden by targets
159 /// that want to emit something at the start of their file.
160 virtual void EmitStartOfAsmFile(Module &) {}
162 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
163 /// want to emit something at the end of their file.
164 virtual void EmitEndOfAsmFile(Module &) {}
166 /// doFinalization - Shut down the asmprinter. If you override this in your
167 /// pass, you must make sure to call it explicitly.
168 bool doFinalization(Module &M);
170 /// PrintSpecial - Print information related to the specified machine instr
171 /// that is independent of the operand, and may be independent of the instr
172 /// itself. This can be useful for portably encoding the comment character
173 /// or other bits of target-specific knowledge into the asmstrings. The
174 /// syntax used is ${:comment}. Targets can override this to add support
175 /// for their own strange codes.
176 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
177 const char *Code) const;
179 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
180 /// instruction, using the specified assembler variant. Targets should
181 /// override this to format as appropriate. This method can return true if
182 /// the operand is erroneous.
183 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
184 unsigned AsmVariant, const char *ExtraCode,
187 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
188 /// instruction, using the specified assembler variant as an address.
189 /// Targets should override this to format as appropriate. This method can
190 /// return true if the operand is erroneous.
191 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
193 const char *ExtraCode,
196 /// runOnMachineFunction - Emit the specified function out to the
198 virtual bool runOnMachineFunction(MachineFunction &MF) {
199 SetupMachineFunction(MF);
200 EmitFunctionHeader();
205 /// SetupMachineFunction - This should be called when a new MachineFunction
206 /// is being processed from runOnMachineFunction.
207 void SetupMachineFunction(MachineFunction &MF);
209 /// EmitFunctionHeader - This method emits the header for the current
211 void EmitFunctionHeader();
213 /// EmitFunctionBody - This method emits the body and trailer for a
215 void EmitFunctionBody();
217 /// EmitInstruction - Targets should implement this to emit instructions.
218 virtual void EmitInstruction(const MachineInstr *) {
219 assert(0 && "EmitInstruction not implemented");
222 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
223 /// the first basic block in the function.
224 virtual void EmitFunctionBodyStart() {}
226 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
227 /// the last basic block in the function.
228 virtual void EmitFunctionBodyEnd() {}
230 /// EmitConstantPool - Print to the current output stream assembly
231 /// representations of the constants in the constant pool MCP. This is
232 /// used to print out constants which have been "spilled to memory" by
233 /// the code generator.
235 virtual void EmitConstantPool();
237 /// EmitJumpTableInfo - Print assembly representations of the jump tables
238 /// used by the current function to the current output stream.
240 void EmitJumpTableInfo();
242 /// EmitGlobalVariable - Emit the specified global variable to the .s file.
243 virtual void EmitGlobalVariable(const GlobalVariable *GV);
245 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
246 /// special global used by LLVM. If so, emit it and return true, otherwise
247 /// do nothing and return false.
248 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
251 //===------------------------------------------------------------------===//
252 // Emission and print routines
255 /// EmitInt8 - Emit a byte directive and value.
257 void EmitInt8(int Value) const;
259 /// EmitInt16 - Emit a short directive and value.
261 void EmitInt16(int Value) const;
263 /// EmitInt32 - Emit a long directive and value.
265 void EmitInt32(int Value) const;
267 /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
268 /// in bytes of the directive is specified by Size and Hi/Lo specify the
269 /// labels. This implicitly uses .set if it is available.
270 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
271 unsigned Size) const;
273 //===------------------------------------------------------------------===//
275 /// EmitAlignment - Emit an alignment directive to the specified power of
276 /// two boundary. For example, if you pass in 3 here, you will get an 8
277 /// byte alignment. If a global value is specified, and if that global has
278 /// an explicit alignment requested, it will unconditionally override the
279 /// alignment request. However, if ForcedAlignBits is specified, this value
280 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
281 /// and the alignment computed with NumBits and the global. If UseFillExpr
282 /// is true, it also emits an optional second value FillValue which the
283 /// assembler uses to fill gaps to match alignment for text sections if the
284 /// has specified a non-zero fill value.
286 /// The algorithm is:
288 /// if (GV && GV->hasalignment) Align = GV->getalignment();
289 /// Align = std::max(Align, ForcedAlignBits);
291 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
292 unsigned ForcedAlignBits = 0,
293 bool UseFillExpr = true) const;
295 /// printDeclare - This method prints a local variable declaration used by
297 void printDeclare(const MachineInstr *MI) const;
299 /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
300 /// global value name as its base, with the specified suffix, and where the
301 /// symbol is forced to have private linkage if ForcePrivate is true.
302 MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
304 bool ForcePrivate = true) const;
306 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
308 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
310 /// GetCPISymbol - Return the symbol for the specified constant pool entry.
311 MCSymbol *GetCPISymbol(unsigned CPID) const;
313 /// GetJTISymbol - Return the symbol for the specified jump table entry.
314 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
316 /// GetJTSetSymbol - Return the symbol for the specified jump table .set
317 /// FIXME: privatize to AsmPrinter.
318 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
320 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
321 /// uses of the specified basic block.
322 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
323 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
325 /// EmitBasicBlockStart - This method prints the label for the specified
326 /// MachineBasicBlock, an alignment (if present) and a comment describing
327 /// it if appropriate.
328 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
333 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
334 void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
337 virtual void EmitFunctionEntryLabel();
339 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
341 /// printOffset - This is just convenient handler for printing offsets.
342 void printOffset(int64_t Offset, raw_ostream &OS) const;
344 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
345 /// exactly one predecessor and the control transfer mechanism between
346 /// the predecessor and this block is a fall-through.
348 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
351 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
352 void EmitInlineAsm(StringRef Str) const;
354 /// processDebugLoc - Processes the debug information of each machine
355 /// instruction's DebugLoc.
356 void processDebugLoc(const MachineInstr *MI, bool BeforePrintingInsn);
358 void printLabelInst(const MachineInstr *MI) const;
360 /// printInlineAsm - This method formats and prints the specified machine
361 /// instruction that is an inline asm.
362 void printInlineAsm(const MachineInstr *MI) const;
364 /// printImplicitDef - This method prints the specified machine instruction
365 /// that is an implicit def.
366 void printImplicitDef(const MachineInstr *MI) const;
368 /// printKill - This method prints the specified kill machine instruction.
369 void printKill(const MachineInstr *MI) const;
371 /// EmitVisibility - This emits visibility information about symbol, if
372 /// this is suported by the target.
373 void EmitVisibility(MCSymbol *Sym, unsigned Visibility) const;
375 void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const;
377 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
378 const MachineBasicBlock *MBB,
380 void EmitLLVMUsedList(Constant *List);
381 void EmitXXStructorList(Constant *List);
382 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);