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 {
60 /// DW - If available, this is a pointer to the current dwarf writer.
63 /// Target machine description.
67 /// Target Asm Printer information.
71 /// OutContext - This is the context for the output file that we are
72 /// streaming. This owns all of the global MC-related objects for the
73 /// generated translation unit.
74 MCContext &OutContext;
76 /// OutStreamer - This is the MCStreamer object for the file we are
77 /// generating. This contains the transient state for the current
78 /// translation unit that we are generating (such as the current section
80 MCStreamer &OutStreamer;
82 /// The current machine function.
83 const MachineFunction *MF;
85 /// MMI - This is a pointer to the current MachineModuleInfo.
86 MachineModuleInfo *MMI;
88 /// Name-mangler for global names.
92 /// The symbol for the current function. This is recalculated at the
93 /// beginning of each call to runOnMachineFunction().
95 MCSymbol *CurrentFnSym;
97 /// getObjFileLowering - Return information about object file lowering.
98 TargetLoweringObjectFile &getObjFileLowering() const;
100 /// getCurrentSection() - Return the current section we are emitting to.
101 const MCSection *getCurrentSection() const;
104 // GCMetadataPrinters - The garbage collection metadata printer table.
105 void *GCMetadataPrinters; // Really a DenseMap.
107 /// VerboseAsm - Emit comments in assembly output if this is true.
112 /// If VerboseAsm is set, a pointer to the loop info for this
118 explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer);
121 virtual ~AsmPrinter();
123 /// isVerbose - Return true if assembly output should contain comments.
125 bool isVerbose() const { return VerboseAsm; }
127 /// getFunctionNumber - Return a unique ID for the current function.
129 unsigned getFunctionNumber() const;
131 //===------------------------------------------------------------------===//
132 // MachineFunctionPass Implementation.
133 //===------------------------------------------------------------------===//
135 /// getAnalysisUsage - Record analysis usage.
137 void getAnalysisUsage(AnalysisUsage &AU) const;
139 /// doInitialization - Set up the AsmPrinter when we are working on a new
140 /// module. If your pass overrides this, it must make sure to explicitly
141 /// call this implementation.
142 bool doInitialization(Module &M);
144 /// doFinalization - Shut down the asmprinter. If you override this in your
145 /// pass, you must make sure to call it explicitly.
146 bool doFinalization(Module &M);
148 /// runOnMachineFunction - Emit the specified function out to the
150 virtual bool runOnMachineFunction(MachineFunction &MF) {
151 SetupMachineFunction(MF);
152 EmitFunctionHeader();
157 //===------------------------------------------------------------------===//
158 // Coarse grained IR lowering routines.
159 //===------------------------------------------------------------------===//
161 /// SetupMachineFunction - This should be called when a new MachineFunction
162 /// is being processed from runOnMachineFunction.
163 void SetupMachineFunction(MachineFunction &MF);
165 /// EmitFunctionHeader - This method emits the header for the current
167 void EmitFunctionHeader();
169 /// EmitFunctionBody - This method emits the body and trailer for a
171 void EmitFunctionBody();
173 /// EmitConstantPool - Print to the current output stream assembly
174 /// representations of the constants in the constant pool MCP. This is
175 /// used to print out constants which have been "spilled to memory" by
176 /// the code generator.
178 virtual void EmitConstantPool();
180 /// EmitJumpTableInfo - Print assembly representations of the jump tables
181 /// used by the current function to the current output stream.
183 void EmitJumpTableInfo();
185 /// EmitGlobalVariable - Emit the specified global variable to the .s file.
186 virtual void EmitGlobalVariable(const GlobalVariable *GV);
188 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
189 /// special global used by LLVM. If so, emit it and return true, otherwise
190 /// do nothing and return false.
191 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
193 //===------------------------------------------------------------------===//
195 //===------------------------------------------------------------------===//
197 // Targets can, or in the case of EmitInstruction, must implement these to
200 /// EmitStartOfAsmFile - This virtual method can be overridden by targets
201 /// that want to emit something at the start of their file.
202 virtual void EmitStartOfAsmFile(Module &) {}
204 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
205 /// want to emit something at the end of their file.
206 virtual void EmitEndOfAsmFile(Module &) {}
208 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
209 /// the first basic block in the function.
210 virtual void EmitFunctionBodyStart() {}
212 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
213 /// the last basic block in the function.
214 virtual void EmitFunctionBodyEnd() {}
216 /// EmitInstruction - Targets should implement this to emit instructions.
217 virtual void EmitInstruction(const MachineInstr *) {
218 assert(0 && "EmitInstruction not implemented");
221 virtual void EmitFunctionEntryLabel();
223 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
225 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
226 /// exactly one predecessor and the control transfer mechanism between
227 /// the predecessor and this block is a fall-through.
229 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
231 //===------------------------------------------------------------------===//
232 // Lowering Routines.
233 //===------------------------------------------------------------------===//
236 /// EmitAlignment - Emit an alignment directive to the specified power of
237 /// two boundary. For example, if you pass in 3 here, you will get an 8
238 /// byte alignment. If a global value is specified, and if that global has
239 /// an explicit alignment requested, it will unconditionally override the
240 /// alignment request. However, if ForcedAlignBits is specified, this value
241 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
242 /// and the alignment computed with NumBits and the global. If UseFillExpr
243 /// is true, it also emits an optional second value FillValue which the
244 /// assembler uses to fill gaps to match alignment for text sections if the
245 /// has specified a non-zero fill value.
247 /// The algorithm is:
249 /// if (GV && GV->hasalignment) Align = GV->getalignment();
250 /// Align = std::max(Align, ForcedAlignBits);
252 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
253 unsigned ForcedAlignBits = 0,
254 bool UseFillExpr = true) const;
256 /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
257 /// global value name as its base, with the specified suffix, and where the
258 /// symbol is forced to have private linkage if ForcePrivate is true.
259 MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
261 bool ForcePrivate = true) const;
263 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
265 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
267 /// GetCPISymbol - Return the symbol for the specified constant pool entry.
268 MCSymbol *GetCPISymbol(unsigned CPID) const;
270 /// GetJTISymbol - Return the symbol for the specified jump table entry.
271 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
273 /// GetJTSetSymbol - Return the symbol for the specified jump table .set
274 /// FIXME: privatize to AsmPrinter.
275 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
277 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
278 /// uses of the specified basic block.
279 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
280 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
282 /// EmitBasicBlockStart - This method prints the label for the specified
283 /// MachineBasicBlock, an alignment (if present) and a comment describing
284 /// it if appropriate.
285 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);
291 //===------------------------------------------------------------------===//
292 // Emission Helper Routines.
293 //===------------------------------------------------------------------===//
295 /// printOffset - This is just convenient handler for printing offsets.
296 void printOffset(int64_t Offset, raw_ostream &OS) const;
298 /// EmitInt8 - Emit a byte directive and value.
300 void EmitInt8(int Value) const;
302 /// EmitInt16 - Emit a short directive and value.
304 void EmitInt16(int Value) const;
306 /// EmitInt32 - Emit a long directive and value.
308 void EmitInt32(int Value) const;
310 /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
311 /// in bytes of the directive is specified by Size and Hi/Lo specify the
312 /// labels. This implicitly uses .set if it is available.
313 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
314 unsigned Size) const;
317 //===------------------------------------------------------------------===//
318 // Inline Asm Support
319 //===------------------------------------------------------------------===//
321 // These are hooks that targets can override to implement inline asm
322 // support. These should probably be moved out of AsmPrinter someday.
324 /// PrintSpecial - Print information related to the specified machine instr
325 /// that is independent of the operand, and may be independent of the instr
326 /// itself. This can be useful for portably encoding the comment character
327 /// or other bits of target-specific knowledge into the asmstrings. The
328 /// syntax used is ${:comment}. Targets can override this to add support
329 /// for their own strange codes.
330 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
331 const char *Code) const;
333 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
334 /// instruction, using the specified assembler variant. Targets should
335 /// override this to format as appropriate. This method can return true if
336 /// the operand is erroneous.
337 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
338 unsigned AsmVariant, const char *ExtraCode,
341 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
342 /// instruction, using the specified assembler variant as an address.
343 /// Targets should override this to format as appropriate. This method can
344 /// return true if the operand is erroneous.
345 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
347 const char *ExtraCode,
351 /// Private state for PrintSpecial()
352 // Assign a unique ID to this machine instruction.
353 mutable const MachineInstr *LastMI;
354 mutable unsigned LastFn;
355 mutable unsigned Counter;
356 mutable unsigned SetCounter;
358 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
359 void EmitInlineAsm(StringRef Str) const;
361 /// EmitInlineAsm - This method formats and emits the specified machine
362 /// instruction that is an inline asm.
363 void EmitInlineAsm(const MachineInstr *MI) const;
365 //===------------------------------------------------------------------===//
366 // Internal Implementation Details
367 //===------------------------------------------------------------------===//
369 /// EmitVisibility - This emits visibility information about symbol, if
370 /// this is suported by the target.
371 void EmitVisibility(MCSymbol *Sym, unsigned Visibility) const;
373 void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const;
375 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
376 const MachineBasicBlock *MBB,
378 void EmitLLVMUsedList(Constant *List);
379 void EmitXXStructorList(Constant *List);
380 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);