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/IR/InlineAsm.h"
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Support/ErrorHandling.h"
29 class GCMetadataPrinter;
32 class MachineBasicBlock;
33 class MachineFunction;
35 class MachineLocation;
36 class MachineLoopInfo;
38 class MachineConstantPoolValue;
39 class MachineJumpTableInfo;
40 class MachineModuleInfo;
42 class MCCFIInstruction;
52 class TargetLoweringObjectFile;
56 /// AsmPrinter - This class is intended to be used as a driving class for all
58 class AsmPrinter : public MachineFunctionPass {
60 /// Target machine description.
64 /// Target Asm Printer information.
68 const MCInstrInfo *MII;
69 /// OutContext - This is the context for the output file that we are
70 /// streaming. This owns all of the global MC-related objects for the
71 /// generated translation unit.
72 MCContext &OutContext;
74 /// OutStreamer - This is the MCStreamer object for the file we are
75 /// generating. This contains the transient state for the current
76 /// translation unit that we are generating (such as the current section
78 MCStreamer &OutStreamer;
80 /// The current machine function.
81 const MachineFunction *MF;
83 /// MMI - This is a pointer to the current MachineModuleInfo.
84 MachineModuleInfo *MMI;
86 /// Name-mangler for global names.
90 /// The symbol for the current function. This is recalculated at the
91 /// beginning of each call to runOnMachineFunction().
93 MCSymbol *CurrentFnSym;
95 /// The symbol used to represent the start of the current function for the
96 /// purpose of calculating its size (e.g. using the .size directive). By
97 /// default, this is equal to CurrentFnSym.
98 MCSymbol *CurrentFnSymForSize;
101 // GCMetadataPrinters - The garbage collection metadata printer table.
102 void *GCMetadataPrinters; // Really a DenseMap.
104 /// VerboseAsm - Emit comments in assembly output if this is true.
109 /// If VerboseAsm is set, a pointer to the loop info for this
113 /// DD - If the target supports dwarf debug info, this pointer is non-null.
116 /// DE - If the target supports dwarf exception info, this pointer is
121 explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer);
124 virtual ~AsmPrinter();
126 const DwarfDebug *getDwarfDebug() const { return DD; }
128 /// isVerbose - Return true if assembly output should contain comments.
130 bool isVerbose() const { return VerboseAsm; }
132 /// getFunctionNumber - Return a unique ID for the current function.
134 unsigned getFunctionNumber() const;
136 /// getObjFileLowering - Return information about object file lowering.
137 const TargetLoweringObjectFile &getObjFileLowering() const;
139 /// getDataLayout - Return information about data layout.
140 const DataLayout &getDataLayout() const;
142 /// getTargetTriple - Return the target triple string.
143 StringRef getTargetTriple() const;
145 /// getCurrentSection() - Return the current section we are emitting to.
146 const MCSection *getCurrentSection() const;
149 //===------------------------------------------------------------------===//
150 // MachineFunctionPass Implementation.
151 //===------------------------------------------------------------------===//
153 /// getAnalysisUsage - Record analysis usage.
155 void getAnalysisUsage(AnalysisUsage &AU) const;
157 /// doInitialization - Set up the AsmPrinter when we are working on a new
158 /// module. If your pass overrides this, it must make sure to explicitly
159 /// call this implementation.
160 bool doInitialization(Module &M);
162 /// doFinalization - Shut down the asmprinter. If you override this in your
163 /// pass, you must make sure to call it explicitly.
164 bool doFinalization(Module &M);
166 /// runOnMachineFunction - Emit the specified function out to the
168 virtual bool runOnMachineFunction(MachineFunction &MF) {
169 SetupMachineFunction(MF);
170 EmitFunctionHeader();
175 //===------------------------------------------------------------------===//
176 // Coarse grained IR lowering routines.
177 //===------------------------------------------------------------------===//
179 /// SetupMachineFunction - This should be called when a new MachineFunction
180 /// is being processed from runOnMachineFunction.
181 void SetupMachineFunction(MachineFunction &MF);
183 /// EmitFunctionHeader - This method emits the header for the current
185 void EmitFunctionHeader();
187 /// EmitFunctionBody - This method emits the body and trailer for a
189 void EmitFunctionBody();
191 void emitPrologLabel(const MachineInstr &MI);
198 CFIMoveType needsCFIMoves();
200 bool needsSEHMoves();
202 /// needsRelocationsForDwarfStringPool - Specifies whether the object format
203 /// expects to use relocations to refer to debug entries. Alternatively we
204 /// emit section offsets in bytes from the start of the string pool.
205 bool needsRelocationsForDwarfStringPool() const;
207 /// EmitConstantPool - Print to the current output stream assembly
208 /// representations of the constants in the constant pool MCP. This is
209 /// used to print out constants which have been "spilled to memory" by
210 /// the code generator.
212 virtual void EmitConstantPool();
214 /// EmitJumpTableInfo - Print assembly representations of the jump tables
215 /// used by the current function to the current output stream.
217 void EmitJumpTableInfo();
219 /// EmitGlobalVariable - Emit the specified global variable to the .s file.
220 virtual void EmitGlobalVariable(const GlobalVariable *GV);
222 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
223 /// special global used by LLVM. If so, emit it and return true, otherwise
224 /// do nothing and return false.
225 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
227 /// EmitAlignment - Emit an alignment directive to the specified power of
228 /// two boundary. For example, if you pass in 3 here, you will get an 8
229 /// byte alignment. If a global value is specified, and if that global has
230 /// an explicit alignment requested, it will override the alignment request
231 /// if required for correctness.
233 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
235 /// EmitBasicBlockStart - This method prints the label for the specified
236 /// MachineBasicBlock, an alignment (if present) and a comment describing
237 /// it if appropriate.
238 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
240 /// \brief Print a general LLVM constant to the .s file.
241 void EmitGlobalConstant(const Constant *CV);
244 //===------------------------------------------------------------------===//
246 //===------------------------------------------------------------------===//
248 // Targets can, or in the case of EmitInstruction, must implement these to
251 /// EmitStartOfAsmFile - This virtual method can be overridden by targets
252 /// that want to emit something at the start of their file.
253 virtual void EmitStartOfAsmFile(Module &) {}
255 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
256 /// want to emit something at the end of their file.
257 virtual void EmitEndOfAsmFile(Module &) {}
259 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
260 /// the first basic block in the function.
261 virtual void EmitFunctionBodyStart() {}
263 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
264 /// the last basic block in the function.
265 virtual void EmitFunctionBodyEnd() {}
267 /// EmitInstruction - Targets should implement this to emit instructions.
268 virtual void EmitInstruction(const MachineInstr *) {
269 llvm_unreachable("EmitInstruction not implemented");
272 virtual void EmitFunctionEntryLabel();
274 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
276 /// EmitXXStructor - Targets can override this to change how global
277 /// constants that are part of a C++ static/global constructor list are
279 virtual void EmitXXStructor(const Constant *CV) {
280 EmitGlobalConstant(CV);
283 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
284 /// exactly one predecessor and the control transfer mechanism between
285 /// the predecessor and this block is a fall-through.
287 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
289 /// emitImplicitDef - Targets can override this to customize the output of
290 /// IMPLICIT_DEF instructions in verbose mode.
291 virtual void emitImplicitDef(const MachineInstr *MI) const;
293 //===------------------------------------------------------------------===//
294 // Symbol Lowering Routines.
295 //===------------------------------------------------------------------===//
298 /// GetTempSymbol - Return the MCSymbol corresponding to the assembler
299 /// temporary label with the specified stem and unique ID.
300 MCSymbol *GetTempSymbol(StringRef Name, unsigned ID) const;
302 /// GetTempSymbol - Return an assembler temporary label with the specified
304 MCSymbol *GetTempSymbol(StringRef Name) const;
307 /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
308 /// global value name as its base, with the specified suffix, and where the
309 /// symbol is forced to have private linkage if ForcePrivate is true.
310 MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
312 bool ForcePrivate = true) const;
314 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
316 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
318 /// GetCPISymbol - Return the symbol for the specified constant pool entry.
319 MCSymbol *GetCPISymbol(unsigned CPID) const;
321 /// GetJTISymbol - Return the symbol for the specified jump table entry.
322 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
324 /// GetJTSetSymbol - Return the symbol for the specified jump table .set
325 /// FIXME: privatize to AsmPrinter.
326 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
328 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
329 /// uses of the specified basic block.
330 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
331 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
333 //===------------------------------------------------------------------===//
334 // Emission Helper Routines.
335 //===------------------------------------------------------------------===//
337 /// printOffset - This is just convenient handler for printing offsets.
338 void printOffset(int64_t Offset, raw_ostream &OS) const;
340 /// EmitInt8 - Emit a byte directive and value.
342 void EmitInt8(int Value) const;
344 /// EmitInt16 - Emit a short directive and value.
346 void EmitInt16(int Value) const;
348 /// EmitInt32 - Emit a long directive and value.
350 void EmitInt32(int Value) const;
352 /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
353 /// in bytes of the directive is specified by Size and Hi/Lo specify the
354 /// labels. This implicitly uses .set if it is available.
355 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
356 unsigned Size) const;
358 /// EmitLabelOffsetDifference - Emit something like ".long Hi+Offset-Lo"
359 /// where the size in bytes of the directive is specified by Size and Hi/Lo
360 /// specify the labels. This implicitly uses .set if it is available.
361 void EmitLabelOffsetDifference(const MCSymbol *Hi, uint64_t Offset,
362 const MCSymbol *Lo, unsigned Size) const;
364 /// EmitLabelPlusOffset - Emit something like ".long Label+Offset"
365 /// where the size in bytes of the directive is specified by Size and Label
366 /// specifies the label. This implicitly uses .set if it is available.
367 void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
369 bool IsSectionRelative = false) const;
371 /// EmitLabelReference - Emit something like ".long Label"
372 /// where the size in bytes of the directive is specified by Size and Label
373 /// specifies the label.
374 void EmitLabelReference(const MCSymbol *Label, unsigned Size,
375 bool IsSectionRelative = false) const {
376 EmitLabelPlusOffset(Label, 0, Size, IsSectionRelative);
379 //===------------------------------------------------------------------===//
380 // Dwarf Emission Helper Routines
381 //===------------------------------------------------------------------===//
383 /// EmitSLEB128 - emit the specified signed leb128 value.
384 void EmitSLEB128(int64_t Value, const char *Desc = 0) const;
386 /// EmitULEB128 - emit the specified unsigned leb128 value.
387 void EmitULEB128(uint64_t Value, const char *Desc = 0,
388 unsigned PadTo = 0) const;
390 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
391 void EmitCFAByte(unsigned Val) const;
393 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
394 /// encoding. If verbose assembly output is enabled, we output comments
395 /// describing the encoding. Desc is a string saying what the encoding is
396 /// specifying (e.g. "LSDA").
397 void EmitEncodingByte(unsigned Val, const char *Desc = 0) const;
399 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
400 unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
402 /// EmitReference - Emit reference to a ttype global with a specified encoding.
403 void EmitTTypeReference(const GlobalValue *GV, unsigned Encoding) const;
405 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of
406 /// its section. This can be done with a special directive if the target
407 /// supports it (e.g. cygwin) or by emitting it as an offset from a label at
408 /// the start of the section.
410 /// SectionLabel is a temporary label emitted at the start of the section
411 /// that Label lives in.
412 void EmitSectionOffset(const MCSymbol *Label,
413 const MCSymbol *SectionLabel) const;
415 /// getISAEncoding - Get the value for DW_AT_APPLE_isa. Zero if no isa
416 /// encoding specified.
417 virtual unsigned getISAEncoding() { return 0; }
419 /// EmitDwarfRegOp - Emit dwarf register operation.
420 virtual void EmitDwarfRegOp(const MachineLocation &MLoc,
421 bool Indirect) const;
423 //===------------------------------------------------------------------===//
424 // Dwarf Lowering Routines
425 //===------------------------------------------------------------------===//
427 /// \brief Emit frame instruction to describe the layout of the frame.
428 void emitCFIInstruction(const MCCFIInstruction &Inst) const;
430 //===------------------------------------------------------------------===//
431 // Inline Asm Support
432 //===------------------------------------------------------------------===//
434 // These are hooks that targets can override to implement inline asm
435 // support. These should probably be moved out of AsmPrinter someday.
437 /// PrintSpecial - Print information related to the specified machine instr
438 /// that is independent of the operand, and may be independent of the instr
439 /// itself. This can be useful for portably encoding the comment character
440 /// or other bits of target-specific knowledge into the asmstrings. The
441 /// syntax used is ${:comment}. Targets can override this to add support
442 /// for their own strange codes.
443 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
444 const char *Code) const;
446 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
447 /// instruction, using the specified assembler variant. Targets should
448 /// override this to format as appropriate. This method can return true if
449 /// the operand is erroneous.
450 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
451 unsigned AsmVariant, const char *ExtraCode,
454 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
455 /// instruction, using the specified assembler variant as an address.
456 /// Targets should override this to format as appropriate. This method can
457 /// return true if the operand is erroneous.
458 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
460 const char *ExtraCode,
464 /// Private state for PrintSpecial()
465 // Assign a unique ID to this machine instruction.
466 mutable const MachineInstr *LastMI;
467 mutable unsigned LastFn;
468 mutable unsigned Counter;
469 mutable unsigned SetCounter;
471 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
472 void EmitInlineAsm(StringRef Str, const MDNode *LocMDNode = 0,
473 InlineAsm::AsmDialect AsmDialect = InlineAsm::AD_ATT) const;
475 /// EmitInlineAsm - This method formats and emits the specified machine
476 /// instruction that is an inline asm.
477 void EmitInlineAsm(const MachineInstr *MI) const;
479 //===------------------------------------------------------------------===//
480 // Internal Implementation Details
481 //===------------------------------------------------------------------===//
483 /// EmitVisibility - This emits visibility information about symbol, if
484 /// this is suported by the target.
485 void EmitVisibility(MCSymbol *Sym, unsigned Visibility,
486 bool IsDefinition = true) const;
488 void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const;
490 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
491 const MachineBasicBlock *MBB,
493 void EmitLLVMUsedList(const ConstantArray *InitList);
494 /// Emit llvm.ident metadata in an '.ident' directive.
495 void EmitModuleIdents(Module &M);
496 void EmitXXStructorList(const Constant *List, bool isCtor);
497 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);