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"
25 class AsmPrinterHandler;
30 class GCMetadataPrinter;
33 class MachineBasicBlock;
34 class MachineFunction;
36 class MachineLocation;
37 class MachineLoopInfo;
39 class MachineConstantPoolValue;
40 class MachineJumpTableInfo;
41 class MachineModuleInfo;
43 class MCCFIInstruction;
53 class TargetLoweringObjectFile;
57 /// AsmPrinter - This class is intended to be used as a driving class for all
59 class AsmPrinter : public MachineFunctionPass {
61 /// Target machine description.
65 /// Target Asm Printer information.
69 const MCInstrInfo *MII;
70 /// OutContext - This is the context for the output file that we are
71 /// streaming. This owns all of the global MC-related objects for the
72 /// generated translation unit.
73 MCContext &OutContext;
75 /// OutStreamer - This is the MCStreamer object for the file we are
76 /// generating. This contains the transient state for the current
77 /// translation unit that we are generating (such as the current section
79 MCStreamer &OutStreamer;
81 /// The current machine function.
82 const MachineFunction *MF;
84 /// MMI - This is a pointer to the current MachineModuleInfo.
85 MachineModuleInfo *MMI;
87 /// Name-mangler for global names.
91 /// The symbol for the current function. This is recalculated at the
92 /// beginning of each call to runOnMachineFunction().
94 MCSymbol *CurrentFnSym;
96 /// The symbol used to represent the start of the current function for the
97 /// purpose of calculating its size (e.g. using the .size directive). By
98 /// default, this is equal to CurrentFnSym.
99 MCSymbol *CurrentFnSymForSize;
102 // GCMetadataPrinters - The garbage collection metadata printer table.
103 void *GCMetadataPrinters; // Really a DenseMap.
105 /// VerboseAsm - Emit comments in assembly output if this is true.
110 /// If VerboseAsm is set, a pointer to the loop info for this
115 AsmPrinterHandler *Handler;
116 const char *TimerName, *TimerGroupName;
117 HandlerInfo(AsmPrinterHandler *Handler, const char *TimerName,
118 const char *TimerGroupName)
119 : Handler(Handler), TimerName(TimerName),
120 TimerGroupName(TimerGroupName) {}
122 /// Handlers - a vector of all debug/EH info emitters we should use.
123 /// This vector maintains ownership of the emitters.
124 SmallVector<HandlerInfo, 1> Handlers;
126 /// DD - If the target supports dwarf debug info, this pointer is non-null.
130 explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer);
133 virtual ~AsmPrinter();
135 const DwarfDebug *getDwarfDebug() const { return DD; }
137 /// isVerbose - Return true if assembly output should contain comments.
139 bool isVerbose() const { return VerboseAsm; }
141 /// getFunctionNumber - Return a unique ID for the current function.
143 unsigned getFunctionNumber() const;
145 /// getObjFileLowering - Return information about object file lowering.
146 const TargetLoweringObjectFile &getObjFileLowering() const;
148 /// getDataLayout - Return information about data layout.
149 const DataLayout &getDataLayout() const;
151 /// getTargetTriple - Return the target triple string.
152 StringRef getTargetTriple() const;
154 /// getCurrentSection() - Return the current section we are emitting to.
155 const MCSection *getCurrentSection() const;
157 MCSymbol *getSymbol(const GlobalValue *GV) const;
159 //===------------------------------------------------------------------===//
160 // MachineFunctionPass Implementation.
161 //===------------------------------------------------------------------===//
163 /// getAnalysisUsage - Record analysis usage.
165 void getAnalysisUsage(AnalysisUsage &AU) const;
167 /// doInitialization - Set up the AsmPrinter when we are working on a new
168 /// module. If your pass overrides this, it must make sure to explicitly
169 /// call this implementation.
170 bool doInitialization(Module &M);
172 /// doFinalization - Shut down the asmprinter. If you override this in your
173 /// pass, you must make sure to call it explicitly.
174 bool doFinalization(Module &M);
176 /// runOnMachineFunction - Emit the specified function out to the
178 virtual bool runOnMachineFunction(MachineFunction &MF) {
179 SetupMachineFunction(MF);
180 EmitFunctionHeader();
185 //===------------------------------------------------------------------===//
186 // Coarse grained IR lowering routines.
187 //===------------------------------------------------------------------===//
189 /// SetupMachineFunction - This should be called when a new MachineFunction
190 /// is being processed from runOnMachineFunction.
191 void SetupMachineFunction(MachineFunction &MF);
193 /// EmitFunctionHeader - This method emits the header for the current
195 void EmitFunctionHeader();
197 /// EmitFunctionBody - This method emits the body and trailer for a
199 void EmitFunctionBody();
201 void emitPrologLabel(const MachineInstr &MI);
208 CFIMoveType needsCFIMoves();
210 bool needsSEHMoves();
212 /// needsRelocationsForDwarfStringPool - Specifies whether the object format
213 /// expects to use relocations to refer to debug entries. Alternatively we
214 /// emit section offsets in bytes from the start of the string pool.
215 bool needsRelocationsForDwarfStringPool() const;
217 /// EmitConstantPool - Print to the current output stream assembly
218 /// representations of the constants in the constant pool MCP. This is
219 /// used to print out constants which have been "spilled to memory" by
220 /// the code generator.
222 virtual void EmitConstantPool();
224 /// EmitJumpTableInfo - Print assembly representations of the jump tables
225 /// used by the current function to the current output stream.
227 void EmitJumpTableInfo();
229 /// EmitGlobalVariable - Emit the specified global variable to the .s file.
230 virtual void EmitGlobalVariable(const GlobalVariable *GV);
232 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
233 /// special global used by LLVM. If so, emit it and return true, otherwise
234 /// do nothing and return false.
235 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
237 /// EmitAlignment - Emit an alignment directive to the specified power of
238 /// two boundary. For example, if you pass in 3 here, you will get an 8
239 /// byte alignment. If a global value is specified, and if that global has
240 /// an explicit alignment requested, it will override the alignment request
241 /// if required for correctness.
243 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
245 /// EmitBasicBlockStart - This method prints the label for the specified
246 /// MachineBasicBlock, an alignment (if present) and a comment describing
247 /// it if appropriate.
248 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
250 /// \brief Print a general LLVM constant to the .s file.
251 void EmitGlobalConstant(const Constant *CV);
254 //===------------------------------------------------------------------===//
256 //===------------------------------------------------------------------===//
258 // Targets can, or in the case of EmitInstruction, must implement these to
261 /// EmitStartOfAsmFile - This virtual method can be overridden by targets
262 /// that want to emit something at the start of their file.
263 virtual void EmitStartOfAsmFile(Module &) {}
265 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
266 /// want to emit something at the end of their file.
267 virtual void EmitEndOfAsmFile(Module &) {}
269 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
270 /// the first basic block in the function.
271 virtual void EmitFunctionBodyStart() {}
273 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
274 /// the last basic block in the function.
275 virtual void EmitFunctionBodyEnd() {}
277 /// EmitInstruction - Targets should implement this to emit instructions.
278 virtual void EmitInstruction(const MachineInstr *) {
279 llvm_unreachable("EmitInstruction not implemented");
282 virtual void EmitFunctionEntryLabel();
284 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
286 /// EmitXXStructor - Targets can override this to change how global
287 /// constants that are part of a C++ static/global constructor list are
289 virtual void EmitXXStructor(const Constant *CV) {
290 EmitGlobalConstant(CV);
293 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
294 /// exactly one predecessor and the control transfer mechanism between
295 /// the predecessor and this block is a fall-through.
297 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
299 /// emitImplicitDef - Targets can override this to customize the output of
300 /// IMPLICIT_DEF instructions in verbose mode.
301 virtual void emitImplicitDef(const MachineInstr *MI) const;
303 //===------------------------------------------------------------------===//
304 // Symbol Lowering Routines.
305 //===------------------------------------------------------------------===//
308 /// GetTempSymbol - Return the MCSymbol corresponding to the assembler
309 /// temporary label with the specified stem and unique ID.
310 MCSymbol *GetTempSymbol(StringRef Name, unsigned ID) const;
312 /// GetTempSymbol - Return an assembler temporary label with the specified
314 MCSymbol *GetTempSymbol(StringRef Name) const;
316 /// Return the MCSymbol for a private symbol with global value name as its
317 /// base, with the specified suffix.
318 MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
319 StringRef Suffix) const;
321 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
323 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
325 /// GetCPISymbol - Return the symbol for the specified constant pool entry.
326 MCSymbol *GetCPISymbol(unsigned CPID) const;
328 /// GetJTISymbol - Return the symbol for the specified jump table entry.
329 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
331 /// GetJTSetSymbol - Return the symbol for the specified jump table .set
332 /// FIXME: privatize to AsmPrinter.
333 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
335 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
336 /// uses of the specified basic block.
337 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
338 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
340 //===------------------------------------------------------------------===//
341 // Emission Helper Routines.
342 //===------------------------------------------------------------------===//
344 /// printOffset - This is just convenient handler for printing offsets.
345 void printOffset(int64_t Offset, raw_ostream &OS) const;
347 /// EmitInt8 - Emit a byte directive and value.
349 void EmitInt8(int Value) const;
351 /// EmitInt16 - Emit a short directive and value.
353 void EmitInt16(int Value) const;
355 /// EmitInt32 - Emit a long directive and value.
357 void EmitInt32(int Value) const;
359 /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
360 /// in bytes of the directive is specified by Size and Hi/Lo specify the
361 /// labels. This implicitly uses .set if it is available.
362 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
363 unsigned Size) const;
365 /// EmitLabelOffsetDifference - Emit something like ".long Hi+Offset-Lo"
366 /// where the size in bytes of the directive is specified by Size and Hi/Lo
367 /// specify the labels. This implicitly uses .set if it is available.
368 void EmitLabelOffsetDifference(const MCSymbol *Hi, uint64_t Offset,
369 const MCSymbol *Lo, unsigned Size) const;
371 /// EmitLabelPlusOffset - Emit something like ".long Label+Offset"
372 /// where the size in bytes of the directive is specified by Size and Label
373 /// specifies the label. This implicitly uses .set if it is available.
374 void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
376 bool IsSectionRelative = false) const;
378 /// EmitLabelReference - Emit something like ".long Label"
379 /// where the size in bytes of the directive is specified by Size and Label
380 /// specifies the label.
381 void EmitLabelReference(const MCSymbol *Label, unsigned Size,
382 bool IsSectionRelative = false) const {
383 EmitLabelPlusOffset(Label, 0, Size, IsSectionRelative);
386 //===------------------------------------------------------------------===//
387 // Dwarf Emission Helper Routines
388 //===------------------------------------------------------------------===//
390 /// EmitSLEB128 - emit the specified signed leb128 value.
391 void EmitSLEB128(int64_t Value, const char *Desc = 0) const;
393 /// EmitULEB128 - emit the specified unsigned leb128 value.
394 void EmitULEB128(uint64_t Value, const char *Desc = 0,
395 unsigned PadTo = 0) const;
397 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
398 void EmitCFAByte(unsigned Val) const;
400 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
401 /// encoding. If verbose assembly output is enabled, we output comments
402 /// describing the encoding. Desc is a string saying what the encoding is
403 /// specifying (e.g. "LSDA").
404 void EmitEncodingByte(unsigned Val, const char *Desc = 0) const;
406 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
407 unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
409 /// EmitReference - Emit reference to a ttype global with a specified encoding.
410 void EmitTTypeReference(const GlobalValue *GV, unsigned Encoding) const;
412 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of
413 /// its section. This can be done with a special directive if the target
414 /// supports it (e.g. cygwin) or by emitting it as an offset from a label at
415 /// the start of the section.
417 /// SectionLabel is a temporary label emitted at the start of the section
418 /// that Label lives in.
419 void EmitSectionOffset(const MCSymbol *Label,
420 const MCSymbol *SectionLabel) const;
422 /// getISAEncoding - Get the value for DW_AT_APPLE_isa. Zero if no isa
423 /// encoding specified.
424 virtual unsigned getISAEncoding() { return 0; }
426 /// EmitDwarfRegOp - Emit dwarf register operation.
427 virtual void EmitDwarfRegOp(const MachineLocation &MLoc,
428 bool Indirect) const;
430 //===------------------------------------------------------------------===//
431 // Dwarf Lowering Routines
432 //===------------------------------------------------------------------===//
434 /// \brief Emit frame instruction to describe the layout of the frame.
435 void emitCFIInstruction(const MCCFIInstruction &Inst) const;
437 //===------------------------------------------------------------------===//
438 // Inline Asm Support
439 //===------------------------------------------------------------------===//
441 // These are hooks that targets can override to implement inline asm
442 // support. These should probably be moved out of AsmPrinter someday.
444 /// PrintSpecial - Print information related to the specified machine instr
445 /// that is independent of the operand, and may be independent of the instr
446 /// itself. This can be useful for portably encoding the comment character
447 /// or other bits of target-specific knowledge into the asmstrings. The
448 /// syntax used is ${:comment}. Targets can override this to add support
449 /// for their own strange codes.
450 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
451 const char *Code) const;
453 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
454 /// instruction, using the specified assembler variant. Targets should
455 /// override this to format as appropriate. This method can return true if
456 /// the operand is erroneous.
457 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
458 unsigned AsmVariant, const char *ExtraCode,
461 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
462 /// instruction, using the specified assembler variant as an address.
463 /// Targets should override this to format as appropriate. This method can
464 /// return true if the operand is erroneous.
465 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
467 const char *ExtraCode, raw_ostream &OS);
470 /// Private state for PrintSpecial()
471 // Assign a unique ID to this machine instruction.
472 mutable const MachineInstr *LastMI;
473 mutable unsigned LastFn;
474 mutable unsigned Counter;
475 mutable unsigned SetCounter;
477 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
478 void EmitInlineAsm(StringRef Str, const MDNode *LocMDNode = 0,
479 InlineAsm::AsmDialect AsmDialect =
480 InlineAsm::AD_ATT) const;
482 /// EmitInlineAsm - This method formats and emits the specified machine
483 /// instruction that is an inline asm.
484 void EmitInlineAsm(const MachineInstr *MI) const;
486 //===------------------------------------------------------------------===//
487 // Internal Implementation Details
488 //===------------------------------------------------------------------===//
490 /// EmitVisibility - This emits visibility information about symbol, if
491 /// this is suported by the target.
492 void EmitVisibility(MCSymbol *Sym, unsigned Visibility,
493 bool IsDefinition = true) const;
495 void EmitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const;
497 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
498 const MachineBasicBlock *MBB, unsigned uid) const;
499 void EmitLLVMUsedList(const ConstantArray *InitList);
500 /// Emit llvm.ident metadata in an '.ident' directive.
501 void EmitModuleIdents(Module &M);
502 void EmitXXStructorList(const Constant *List, bool isCtor);
503 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);