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;
53 class TargetLoweringObjectFile;
57 /// AsmPrinter - This class is intended to be used as a driving class for all
59 class AsmPrinter : public MachineFunctionPass {
61 /// DW - If available, this is a pointer to the current dwarf writer.
64 /// Target machine description.
68 /// Target Asm Printer information.
72 /// OutContext - This is the context for the output file that we are
73 /// streaming. This owns all of the global MC-related objects for the
74 /// generated translation unit.
75 MCContext &OutContext;
77 /// OutStreamer - This is the MCStreamer object for the file we are
78 /// generating. This contains the transient state for the current
79 /// translation unit that we are generating (such as the current section
81 MCStreamer &OutStreamer;
83 /// The current machine function.
84 const MachineFunction *MF;
86 /// MMI - This is a pointer to the current MachineModuleInfo.
87 MachineModuleInfo *MMI;
89 /// Name-mangler for global names.
93 /// The symbol for the current function. This is recalculated at the
94 /// beginning of each call to runOnMachineFunction().
96 MCSymbol *CurrentFnSym;
98 /// getObjFileLowering - Return information about object file lowering.
99 TargetLoweringObjectFile &getObjFileLowering() const;
101 /// getCurrentSection() - Return the current section we are emitting to.
102 const MCSection *getCurrentSection() const;
105 // GCMetadataPrinters - The garbage collection metadata printer table.
106 void *GCMetadataPrinters; // Really a DenseMap.
108 /// VerboseAsm - Emit comments in assembly output if this is true.
113 /// If VerboseAsm is set, a pointer to the loop info for this
119 explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer);
122 virtual ~AsmPrinter();
124 /// isVerbose - Return true if assembly output should contain comments.
126 bool isVerbose() const { return VerboseAsm; }
128 /// getFunctionNumber - Return a unique ID for the current function.
130 unsigned getFunctionNumber() const;
132 //===------------------------------------------------------------------===//
133 // MachineFunctionPass Implementation.
134 //===------------------------------------------------------------------===//
136 /// getAnalysisUsage - Record analysis usage.
138 void getAnalysisUsage(AnalysisUsage &AU) const;
140 /// doInitialization - Set up the AsmPrinter when we are working on a new
141 /// module. If your pass overrides this, it must make sure to explicitly
142 /// call this implementation.
143 bool doInitialization(Module &M);
145 /// doFinalization - Shut down the asmprinter. If you override this in your
146 /// pass, you must make sure to call it explicitly.
147 bool doFinalization(Module &M);
149 /// runOnMachineFunction - Emit the specified function out to the
151 virtual bool runOnMachineFunction(MachineFunction &MF) {
152 SetupMachineFunction(MF);
153 EmitFunctionHeader();
158 //===------------------------------------------------------------------===//
159 // Coarse grained IR lowering routines.
160 //===------------------------------------------------------------------===//
162 /// SetupMachineFunction - This should be called when a new MachineFunction
163 /// is being processed from runOnMachineFunction.
164 void SetupMachineFunction(MachineFunction &MF);
166 /// EmitFunctionHeader - This method emits the header for the current
168 void EmitFunctionHeader();
170 /// EmitFunctionBody - This method emits the body and trailer for a
172 void EmitFunctionBody();
174 /// EmitConstantPool - Print to the current output stream assembly
175 /// representations of the constants in the constant pool MCP. This is
176 /// used to print out constants which have been "spilled to memory" by
177 /// the code generator.
179 virtual void EmitConstantPool();
181 /// EmitJumpTableInfo - Print assembly representations of the jump tables
182 /// used by the current function to the current output stream.
184 void EmitJumpTableInfo();
186 /// EmitGlobalVariable - Emit the specified global variable to the .s file.
187 virtual void EmitGlobalVariable(const GlobalVariable *GV);
189 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
190 /// special global used by LLVM. If so, emit it and return true, otherwise
191 /// do nothing and return false.
192 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
194 /// EmitAlignment - Emit an alignment directive to the specified power of
195 /// two boundary. For example, if you pass in 3 here, you will get an 8
196 /// byte alignment. If a global value is specified, and if that global has
197 /// an explicit alignment requested, it will unconditionally override the
198 /// alignment request. However, if ForcedAlignBits is specified, this value
199 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
200 /// and the alignment computed with NumBits and the global. If UseFillExpr
201 /// is true, it also emits an optional second value FillValue which the
202 /// assembler uses to fill gaps to match alignment for text sections if the
203 /// has specified a non-zero fill value.
205 /// The algorithm is:
207 /// if (GV && GV->hasalignment) Align = GV->getalignment();
208 /// Align = std::max(Align, ForcedAlignBits);
210 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
211 unsigned ForcedAlignBits = 0,
212 bool UseFillExpr = true) const;
214 /// EmitBasicBlockStart - This method prints the label for the specified
215 /// MachineBasicBlock, an alignment (if present) and a comment describing
216 /// it if appropriate.
217 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
220 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
221 void EmitGlobalConstant(const Constant *CV, unsigned AddrSpace = 0);
224 //===------------------------------------------------------------------===//
226 //===------------------------------------------------------------------===//
228 // Targets can, or in the case of EmitInstruction, must implement these to
231 /// EmitStartOfAsmFile - This virtual method can be overridden by targets
232 /// that want to emit something at the start of their file.
233 virtual void EmitStartOfAsmFile(Module &) {}
235 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
236 /// want to emit something at the end of their file.
237 virtual void EmitEndOfAsmFile(Module &) {}
239 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
240 /// the first basic block in the function.
241 virtual void EmitFunctionBodyStart() {}
243 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
244 /// the last basic block in the function.
245 virtual void EmitFunctionBodyEnd() {}
247 /// EmitInstruction - Targets should implement this to emit instructions.
248 virtual void EmitInstruction(const MachineInstr *) {
249 assert(0 && "EmitInstruction not implemented");
252 virtual void EmitFunctionEntryLabel();
254 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
256 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
257 /// exactly one predecessor and the control transfer mechanism between
258 /// the predecessor and this block is a fall-through.
260 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
262 //===------------------------------------------------------------------===//
263 // Symbol Lowering Routines.
264 //===------------------------------------------------------------------===//
267 /// GetTempSymbol - Return the MCSymbol corresponding to the assembler
268 /// temporary label with the specified stem and unique ID.
269 MCSymbol *GetTempSymbol(StringRef Name, unsigned ID) const;
271 /// GetTempSymbol - Return an assembler temporary label with the specified
273 MCSymbol *GetTempSymbol(StringRef Name) const;
276 /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
277 /// global value name as its base, with the specified suffix, and where the
278 /// symbol is forced to have private linkage if ForcePrivate is true.
279 MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
281 bool ForcePrivate = true) const;
283 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
285 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
287 /// GetCPISymbol - Return the symbol for the specified constant pool entry.
288 MCSymbol *GetCPISymbol(unsigned CPID) const;
290 /// GetJTISymbol - Return the symbol for the specified jump table entry.
291 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
293 /// GetJTSetSymbol - Return the symbol for the specified jump table .set
294 /// FIXME: privatize to AsmPrinter.
295 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
297 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
298 /// uses of the specified basic block.
299 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
300 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
302 //===------------------------------------------------------------------===//
303 // Emission Helper Routines.
304 //===------------------------------------------------------------------===//
306 /// printOffset - This is just convenient handler for printing offsets.
307 void printOffset(int64_t Offset, raw_ostream &OS) const;
309 /// EmitInt8 - Emit a byte directive and value.
311 void EmitInt8(int Value) const;
313 /// EmitInt16 - Emit a short directive and value.
315 void EmitInt16(int Value) const;
317 /// EmitInt32 - Emit a long directive and value.
319 void EmitInt32(int Value) const;
321 /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
322 /// in bytes of the directive is specified by Size and Hi/Lo specify the
323 /// labels. This implicitly uses .set if it is available.
324 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
325 unsigned Size) const;
327 //===------------------------------------------------------------------===//
328 // Dwarf Emission Helper Routines
329 //===------------------------------------------------------------------===//
331 /// EmitSLEB128 - emit the specified signed leb128 value.
332 void EmitSLEB128(int Value, const char *Desc = 0) const;
334 /// EmitULEB128 - emit the specified unsigned leb128 value.
335 void EmitULEB128(unsigned Value, const char *Desc = 0,
336 unsigned PadTo = 0) const;
338 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
339 void EmitCFAByte(unsigned Val) const;
341 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
342 /// encoding. If verbose assembly output is enabled, we output comments
343 /// describing the encoding. Desc is a string saying what the encoding is
344 /// specifying (e.g. "LSDA").
345 void EmitEncodingByte(unsigned Val, const char *Desc = 0) const;
347 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
348 unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
350 /// EmitReference - Emit a reference to a label with a specified encoding.
352 void EmitReference(const MCSymbol *Sym, unsigned Encoding) const;
353 void EmitReference(const GlobalValue *GV, unsigned Encoding) const;
355 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of
356 /// its section. This can be done with a special directive if the target
357 /// supports it (e.g. cygwin) or by emitting it as an offset from a label at
358 /// the start of the section.
360 /// SectionLabel is a temporary label emitted at the start of the section
361 /// that Label lives in.
362 void EmitSectionOffset(const MCSymbol *Label,
363 const MCSymbol *SectionLabel) const;
365 //===------------------------------------------------------------------===//
366 // Dwarf Lowering Routines
367 //===------------------------------------------------------------------===//
369 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
371 void EmitFrameMoves(const std::vector<MachineMove> &Moves,
372 MCSymbol *BaseLabel, bool isEH) const;
375 //===------------------------------------------------------------------===//
376 // Inline Asm Support
377 //===------------------------------------------------------------------===//
379 // These are hooks that targets can override to implement inline asm
380 // support. These should probably be moved out of AsmPrinter someday.
382 /// PrintSpecial - Print information related to the specified machine instr
383 /// that is independent of the operand, and may be independent of the instr
384 /// itself. This can be useful for portably encoding the comment character
385 /// or other bits of target-specific knowledge into the asmstrings. The
386 /// syntax used is ${:comment}. Targets can override this to add support
387 /// for their own strange codes.
388 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
389 const char *Code) const;
391 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
392 /// instruction, using the specified assembler variant. Targets should
393 /// override this to format as appropriate. This method can return true if
394 /// the operand is erroneous.
395 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
396 unsigned AsmVariant, const char *ExtraCode,
399 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
400 /// instruction, using the specified assembler variant as an address.
401 /// Targets should override this to format as appropriate. This method can
402 /// return true if the operand is erroneous.
403 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
405 const char *ExtraCode,
409 /// Private state for PrintSpecial()
410 // Assign a unique ID to this machine instruction.
411 mutable const MachineInstr *LastMI;
412 mutable unsigned LastFn;
413 mutable unsigned Counter;
414 mutable unsigned SetCounter;
416 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
417 void EmitInlineAsm(StringRef Str) const;
419 /// EmitInlineAsm - This method formats and emits the specified machine
420 /// instruction that is an inline asm.
421 void EmitInlineAsm(const MachineInstr *MI) const;
423 //===------------------------------------------------------------------===//
424 // Internal Implementation Details
425 //===------------------------------------------------------------------===//
427 /// EmitVisibility - This emits visibility information about symbol, if
428 /// this is suported by the target.
429 void EmitVisibility(MCSymbol *Sym, unsigned Visibility) const;
431 void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const;
433 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
434 const MachineBasicBlock *MBB,
436 void EmitLLVMUsedList(Constant *List);
437 void EmitXXStructorList(Constant *List);
438 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);