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;
48 class MCSubtargetInfo;
54 class TargetLoweringObjectFile;
58 /// AsmPrinter - This class is intended to be used as a driving class for all
60 class AsmPrinter : public MachineFunctionPass {
62 /// Target machine description.
66 /// Target Asm Printer information.
70 const MCInstrInfo *MII;
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 /// The symbol used to represent the start of the current function for the
98 /// purpose of calculating its size (e.g. using the .size directive). By
99 /// default, this is equal to CurrentFnSym.
100 MCSymbol *CurrentFnSymForSize;
103 // GCMetadataPrinters - The garbage collection metadata printer table.
104 void *GCMetadataPrinters; // Really a DenseMap.
106 /// VerboseAsm - Emit comments in assembly output if this is true.
111 /// If VerboseAsm is set, a pointer to the loop info for this
116 AsmPrinterHandler *Handler;
117 const char *TimerName, *TimerGroupName;
118 HandlerInfo(AsmPrinterHandler *Handler, const char *TimerName,
119 const char *TimerGroupName)
120 : Handler(Handler), TimerName(TimerName),
121 TimerGroupName(TimerGroupName) {}
123 /// Handlers - a vector of all debug/EH info emitters we should use.
124 /// This vector maintains ownership of the emitters.
125 SmallVector<HandlerInfo, 1> Handlers;
127 /// DD - If the target supports dwarf debug info, this pointer is non-null.
131 explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer);
134 virtual ~AsmPrinter();
136 const DwarfDebug *getDwarfDebug() const { return DD; }
138 /// isVerbose - Return true if assembly output should contain comments.
140 bool isVerbose() const { return VerboseAsm; }
142 /// getFunctionNumber - Return a unique ID for the current function.
144 unsigned getFunctionNumber() const;
146 /// getObjFileLowering - Return information about object file lowering.
147 const TargetLoweringObjectFile &getObjFileLowering() const;
149 /// getDataLayout - Return information about data layout.
150 const DataLayout &getDataLayout() const;
152 /// getTargetTriple - Return the target triple string.
153 StringRef getTargetTriple() const;
155 /// getCurrentSection() - Return the current section we are emitting to.
156 const MCSection *getCurrentSection() const;
158 MCSymbol *getSymbol(const GlobalValue *GV) const;
160 //===------------------------------------------------------------------===//
161 // MachineFunctionPass Implementation.
162 //===------------------------------------------------------------------===//
164 /// getAnalysisUsage - Record analysis usage.
166 void getAnalysisUsage(AnalysisUsage &AU) const;
168 /// doInitialization - Set up the AsmPrinter when we are working on a new
169 /// module. If your pass overrides this, it must make sure to explicitly
170 /// call this implementation.
171 bool doInitialization(Module &M);
173 /// doFinalization - Shut down the asmprinter. If you override this in your
174 /// pass, you must make sure to call it explicitly.
175 bool doFinalization(Module &M);
177 /// runOnMachineFunction - Emit the specified function out to the
179 virtual bool runOnMachineFunction(MachineFunction &MF) {
180 SetupMachineFunction(MF);
181 EmitFunctionHeader();
186 //===------------------------------------------------------------------===//
187 // Coarse grained IR lowering routines.
188 //===------------------------------------------------------------------===//
190 /// SetupMachineFunction - This should be called when a new MachineFunction
191 /// is being processed from runOnMachineFunction.
192 void SetupMachineFunction(MachineFunction &MF);
194 /// EmitFunctionHeader - This method emits the header for the current
196 void EmitFunctionHeader();
198 /// EmitFunctionBody - This method emits the body and trailer for a
200 void EmitFunctionBody();
202 void emitPrologLabel(const MachineInstr &MI);
209 CFIMoveType needsCFIMoves();
211 bool needsSEHMoves();
213 /// EmitConstantPool - Print to the current output stream assembly
214 /// representations of the constants in the constant pool MCP. This is
215 /// used to print out constants which have been "spilled to memory" by
216 /// the code generator.
218 virtual void EmitConstantPool();
220 /// EmitJumpTableInfo - Print assembly representations of the jump tables
221 /// used by the current function to the current output stream.
223 void EmitJumpTableInfo();
225 /// EmitGlobalVariable - Emit the specified global variable to the .s file.
226 virtual void EmitGlobalVariable(const GlobalVariable *GV);
228 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
229 /// special global used by LLVM. If so, emit it and return true, otherwise
230 /// do nothing and return false.
231 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
233 /// EmitAlignment - Emit an alignment directive to the specified power of
234 /// two boundary. For example, if you pass in 3 here, you will get an 8
235 /// byte alignment. If a global value is specified, and if that global has
236 /// an explicit alignment requested, it will override the alignment request
237 /// if required for correctness.
239 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
241 /// EmitBasicBlockStart - This method prints the label for the specified
242 /// MachineBasicBlock, an alignment (if present) and a comment describing
243 /// it if appropriate.
244 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
246 /// \brief Print a general LLVM constant to the .s file.
247 void EmitGlobalConstant(const Constant *CV);
250 //===------------------------------------------------------------------===//
252 //===------------------------------------------------------------------===//
254 // Targets can, or in the case of EmitInstruction, must implement these to
257 /// EmitStartOfAsmFile - This virtual method can be overridden by targets
258 /// that want to emit something at the start of their file.
259 virtual void EmitStartOfAsmFile(Module &) {}
261 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
262 /// want to emit something at the end of their file.
263 virtual void EmitEndOfAsmFile(Module &) {}
265 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
266 /// the first basic block in the function.
267 virtual void EmitFunctionBodyStart() {}
269 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
270 /// the last basic block in the function.
271 virtual void EmitFunctionBodyEnd() {}
273 /// EmitInstruction - Targets should implement this to emit instructions.
274 virtual void EmitInstruction(const MachineInstr *) {
275 llvm_unreachable("EmitInstruction not implemented");
278 virtual void EmitFunctionEntryLabel();
280 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
282 /// EmitXXStructor - Targets can override this to change how global
283 /// constants that are part of a C++ static/global constructor list are
285 virtual void EmitXXStructor(const Constant *CV) {
286 EmitGlobalConstant(CV);
289 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
290 /// exactly one predecessor and the control transfer mechanism between
291 /// the predecessor and this block is a fall-through.
293 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
295 /// emitImplicitDef - Targets can override this to customize the output of
296 /// IMPLICIT_DEF instructions in verbose mode.
297 virtual void emitImplicitDef(const MachineInstr *MI) const;
299 //===------------------------------------------------------------------===//
300 // Symbol Lowering Routines.
301 //===------------------------------------------------------------------===//
304 /// GetTempSymbol - Return the MCSymbol corresponding to the assembler
305 /// temporary label with the specified stem and unique ID.
306 MCSymbol *GetTempSymbol(StringRef Name, unsigned ID) const;
308 /// GetTempSymbol - Return an assembler temporary label with the specified
310 MCSymbol *GetTempSymbol(StringRef Name) const;
312 /// Return the MCSymbol for a private symbol with global value name as its
313 /// base, with the specified suffix.
314 MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
315 StringRef Suffix) const;
317 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
319 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
321 /// GetCPISymbol - Return the symbol for the specified constant pool entry.
322 MCSymbol *GetCPISymbol(unsigned CPID) const;
324 /// GetJTISymbol - Return the symbol for the specified jump table entry.
325 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
327 /// GetJTSetSymbol - Return the symbol for the specified jump table .set
328 /// FIXME: privatize to AsmPrinter.
329 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
331 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
332 /// uses of the specified basic block.
333 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
334 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
336 //===------------------------------------------------------------------===//
337 // Emission Helper Routines.
338 //===------------------------------------------------------------------===//
340 /// printOffset - This is just convenient handler for printing offsets.
341 void printOffset(int64_t Offset, raw_ostream &OS) const;
343 /// EmitInt8 - Emit a byte directive and value.
345 void EmitInt8(int Value) const;
347 /// EmitInt16 - Emit a short directive and value.
349 void EmitInt16(int Value) const;
351 /// EmitInt32 - Emit a long directive and value.
353 void EmitInt32(int Value) const;
355 /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
356 /// in bytes of the directive is specified by Size and Hi/Lo specify the
357 /// labels. This implicitly uses .set if it is available.
358 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
359 unsigned Size) const;
361 /// EmitLabelOffsetDifference - Emit something like ".long Hi+Offset-Lo"
362 /// where the size in bytes of the directive is specified by Size and Hi/Lo
363 /// specify the labels. This implicitly uses .set if it is available.
364 void EmitLabelOffsetDifference(const MCSymbol *Hi, uint64_t Offset,
365 const MCSymbol *Lo, unsigned Size) const;
367 /// EmitLabelPlusOffset - Emit something like ".long Label+Offset"
368 /// where the size in bytes of the directive is specified by Size and Label
369 /// specifies the label. This implicitly uses .set if it is available.
370 void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
372 bool IsSectionRelative = false) const;
374 /// EmitLabelReference - Emit something like ".long Label"
375 /// where the size in bytes of the directive is specified by Size and Label
376 /// specifies the label.
377 void EmitLabelReference(const MCSymbol *Label, unsigned Size,
378 bool IsSectionRelative = false) const {
379 EmitLabelPlusOffset(Label, 0, Size, IsSectionRelative);
382 //===------------------------------------------------------------------===//
383 // Dwarf Emission Helper Routines
384 //===------------------------------------------------------------------===//
386 /// EmitSLEB128 - emit the specified signed leb128 value.
387 void EmitSLEB128(int64_t Value, const char *Desc = 0) const;
389 /// EmitULEB128 - emit the specified unsigned leb128 value.
390 void EmitULEB128(uint64_t Value, const char *Desc = 0,
391 unsigned PadTo = 0) const;
393 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
394 void EmitCFAByte(unsigned Val) const;
396 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
397 /// encoding. If verbose assembly output is enabled, we output comments
398 /// describing the encoding. Desc is a string saying what the encoding is
399 /// specifying (e.g. "LSDA").
400 void EmitEncodingByte(unsigned Val, const char *Desc = 0) const;
402 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
403 unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
405 /// EmitReference - Emit reference to a ttype global with a specified encoding.
406 void EmitTTypeReference(const GlobalValue *GV, unsigned Encoding) const;
408 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of
409 /// its section. This can be done with a special directive if the target
410 /// supports it (e.g. cygwin) or by emitting it as an offset from a label at
411 /// the start of the section.
413 /// SectionLabel is a temporary label emitted at the start of the section
414 /// that Label lives in.
415 void EmitSectionOffset(const MCSymbol *Label,
416 const MCSymbol *SectionLabel) const;
418 /// getISAEncoding - Get the value for DW_AT_APPLE_isa. Zero if no isa
419 /// encoding specified.
420 virtual unsigned getISAEncoding() { return 0; }
422 /// EmitDwarfRegOp - Emit dwarf register operation.
423 virtual void EmitDwarfRegOp(const MachineLocation &MLoc,
424 bool Indirect) const;
426 //===------------------------------------------------------------------===//
427 // Dwarf Lowering Routines
428 //===------------------------------------------------------------------===//
430 /// \brief Emit frame instruction to describe the layout of the frame.
431 void emitCFIInstruction(const MCCFIInstruction &Inst) const;
433 //===------------------------------------------------------------------===//
434 // Inline Asm Support
435 //===------------------------------------------------------------------===//
437 // These are hooks that targets can override to implement inline asm
438 // support. These should probably be moved out of AsmPrinter someday.
440 /// PrintSpecial - Print information related to the specified machine instr
441 /// that is independent of the operand, and may be independent of the instr
442 /// itself. This can be useful for portably encoding the comment character
443 /// or other bits of target-specific knowledge into the asmstrings. The
444 /// syntax used is ${:comment}. Targets can override this to add support
445 /// for their own strange codes.
446 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
447 const char *Code) const;
449 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
450 /// instruction, using the specified assembler variant. Targets should
451 /// override this to format as appropriate. This method can return true if
452 /// the operand is erroneous.
453 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
454 unsigned AsmVariant, const char *ExtraCode,
457 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
458 /// instruction, using the specified assembler variant as an address.
459 /// Targets should override this to format as appropriate. This method can
460 /// return true if the operand is erroneous.
461 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
463 const char *ExtraCode, raw_ostream &OS);
465 /// Let the target do anything it needs to do after emitting inlineasm.
466 /// This callback can be used restore the original mode in case the
467 /// inlineasm contains directives to switch modes.
468 /// \p StartInfo - the original subtarget info before inline asm
469 /// \p EndInfo - the final subtarget info after parsing the inline asm,
470 /// or NULL if the value is unknown.
471 virtual void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
472 MCSubtargetInfo *EndInfo) const;
475 /// Private state for PrintSpecial()
476 // Assign a unique ID to this machine instruction.
477 mutable const MachineInstr *LastMI;
478 mutable unsigned LastFn;
479 mutable unsigned Counter;
480 mutable unsigned SetCounter;
482 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
483 void EmitInlineAsm(StringRef Str, const MDNode *LocMDNode = 0,
484 InlineAsm::AsmDialect AsmDialect =
485 InlineAsm::AD_ATT) const;
487 /// EmitInlineAsm - This method formats and emits the specified machine
488 /// instruction that is an inline asm.
489 void EmitInlineAsm(const MachineInstr *MI) const;
491 //===------------------------------------------------------------------===//
492 // Internal Implementation Details
493 //===------------------------------------------------------------------===//
495 /// EmitVisibility - This emits visibility information about symbol, if
496 /// this is suported by the target.
497 void EmitVisibility(MCSymbol *Sym, unsigned Visibility,
498 bool IsDefinition = true) const;
500 void EmitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const;
502 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
503 const MachineBasicBlock *MBB, unsigned uid) const;
504 void EmitLLVMUsedList(const ConstantArray *InitList);
505 /// Emit llvm.ident metadata in an '.ident' directive.
506 void EmitModuleIdents(Module &M);
507 void EmitXXStructorList(const Constant *List, bool isCtor);
508 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);