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 /// EmitConstantPool - Print to the current output stream assembly
213 /// representations of the constants in the constant pool MCP. This is
214 /// used to print out constants which have been "spilled to memory" by
215 /// the code generator.
217 virtual void EmitConstantPool();
219 /// EmitJumpTableInfo - Print assembly representations of the jump tables
220 /// used by the current function to the current output stream.
222 void EmitJumpTableInfo();
224 /// EmitGlobalVariable - Emit the specified global variable to the .s file.
225 virtual void EmitGlobalVariable(const GlobalVariable *GV);
227 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
228 /// special global used by LLVM. If so, emit it and return true, otherwise
229 /// do nothing and return false.
230 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
232 /// EmitAlignment - Emit an alignment directive to the specified power of
233 /// two boundary. For example, if you pass in 3 here, you will get an 8
234 /// byte alignment. If a global value is specified, and if that global has
235 /// an explicit alignment requested, it will override the alignment request
236 /// if required for correctness.
238 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
240 /// EmitBasicBlockStart - This method prints the label for the specified
241 /// MachineBasicBlock, an alignment (if present) and a comment describing
242 /// it if appropriate.
243 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
245 /// \brief Print a general LLVM constant to the .s file.
246 void EmitGlobalConstant(const Constant *CV);
249 //===------------------------------------------------------------------===//
251 //===------------------------------------------------------------------===//
253 // Targets can, or in the case of EmitInstruction, must implement these to
256 /// EmitStartOfAsmFile - This virtual method can be overridden by targets
257 /// that want to emit something at the start of their file.
258 virtual void EmitStartOfAsmFile(Module &) {}
260 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
261 /// want to emit something at the end of their file.
262 virtual void EmitEndOfAsmFile(Module &) {}
264 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
265 /// the first basic block in the function.
266 virtual void EmitFunctionBodyStart() {}
268 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
269 /// the last basic block in the function.
270 virtual void EmitFunctionBodyEnd() {}
272 /// EmitInstruction - Targets should implement this to emit instructions.
273 virtual void EmitInstruction(const MachineInstr *) {
274 llvm_unreachable("EmitInstruction not implemented");
277 virtual void EmitFunctionEntryLabel();
279 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
281 /// EmitXXStructor - Targets can override this to change how global
282 /// constants that are part of a C++ static/global constructor list are
284 virtual void EmitXXStructor(const Constant *CV) {
285 EmitGlobalConstant(CV);
288 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
289 /// exactly one predecessor and the control transfer mechanism between
290 /// the predecessor and this block is a fall-through.
292 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
294 /// emitImplicitDef - Targets can override this to customize the output of
295 /// IMPLICIT_DEF instructions in verbose mode.
296 virtual void emitImplicitDef(const MachineInstr *MI) const;
298 //===------------------------------------------------------------------===//
299 // Symbol Lowering Routines.
300 //===------------------------------------------------------------------===//
303 /// GetTempSymbol - Return the MCSymbol corresponding to the assembler
304 /// temporary label with the specified stem and unique ID.
305 MCSymbol *GetTempSymbol(StringRef Name, unsigned ID) const;
307 /// GetTempSymbol - Return an assembler temporary label with the specified
309 MCSymbol *GetTempSymbol(StringRef Name) const;
311 /// Return the MCSymbol for a private symbol with global value name as its
312 /// base, with the specified suffix.
313 MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
314 StringRef Suffix) const;
316 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
318 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
320 /// GetCPISymbol - Return the symbol for the specified constant pool entry.
321 MCSymbol *GetCPISymbol(unsigned CPID) const;
323 /// GetJTISymbol - Return the symbol for the specified jump table entry.
324 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
326 /// GetJTSetSymbol - Return the symbol for the specified jump table .set
327 /// FIXME: privatize to AsmPrinter.
328 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
330 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
331 /// uses of the specified basic block.
332 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
333 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
335 //===------------------------------------------------------------------===//
336 // Emission Helper Routines.
337 //===------------------------------------------------------------------===//
339 /// printOffset - This is just convenient handler for printing offsets.
340 void printOffset(int64_t Offset, raw_ostream &OS) const;
342 /// EmitInt8 - Emit a byte directive and value.
344 void EmitInt8(int Value) const;
346 /// EmitInt16 - Emit a short directive and value.
348 void EmitInt16(int Value) const;
350 /// EmitInt32 - Emit a long directive and value.
352 void EmitInt32(int Value) const;
354 /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
355 /// in bytes of the directive is specified by Size and Hi/Lo specify the
356 /// labels. This implicitly uses .set if it is available.
357 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
358 unsigned Size) const;
360 /// EmitLabelOffsetDifference - Emit something like ".long Hi+Offset-Lo"
361 /// where the size in bytes of the directive is specified by Size and Hi/Lo
362 /// specify the labels. This implicitly uses .set if it is available.
363 void EmitLabelOffsetDifference(const MCSymbol *Hi, uint64_t Offset,
364 const MCSymbol *Lo, unsigned Size) const;
366 /// EmitLabelPlusOffset - Emit something like ".long Label+Offset"
367 /// where the size in bytes of the directive is specified by Size and Label
368 /// specifies the label. This implicitly uses .set if it is available.
369 void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
371 bool IsSectionRelative = false) const;
373 /// EmitLabelReference - Emit something like ".long Label"
374 /// where the size in bytes of the directive is specified by Size and Label
375 /// specifies the label.
376 void EmitLabelReference(const MCSymbol *Label, unsigned Size,
377 bool IsSectionRelative = false) const {
378 EmitLabelPlusOffset(Label, 0, Size, IsSectionRelative);
381 //===------------------------------------------------------------------===//
382 // Dwarf Emission Helper Routines
383 //===------------------------------------------------------------------===//
385 /// EmitSLEB128 - emit the specified signed leb128 value.
386 void EmitSLEB128(int64_t Value, const char *Desc = 0) const;
388 /// EmitULEB128 - emit the specified unsigned leb128 value.
389 void EmitULEB128(uint64_t Value, const char *Desc = 0,
390 unsigned PadTo = 0) const;
392 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
393 void EmitCFAByte(unsigned Val) const;
395 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
396 /// encoding. If verbose assembly output is enabled, we output comments
397 /// describing the encoding. Desc is a string saying what the encoding is
398 /// specifying (e.g. "LSDA").
399 void EmitEncodingByte(unsigned Val, const char *Desc = 0) const;
401 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
402 unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
404 /// EmitReference - Emit reference to a ttype global with a specified encoding.
405 void EmitTTypeReference(const GlobalValue *GV, unsigned Encoding) const;
407 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of
408 /// its section. This can be done with a special directive if the target
409 /// supports it (e.g. cygwin) or by emitting it as an offset from a label at
410 /// the start of the section.
412 /// SectionLabel is a temporary label emitted at the start of the section
413 /// that Label lives in.
414 void EmitSectionOffset(const MCSymbol *Label,
415 const MCSymbol *SectionLabel) const;
417 /// getISAEncoding - Get the value for DW_AT_APPLE_isa. Zero if no isa
418 /// encoding specified.
419 virtual unsigned getISAEncoding() { return 0; }
421 /// EmitDwarfRegOp - Emit dwarf register operation.
422 virtual void EmitDwarfRegOp(const MachineLocation &MLoc,
423 bool Indirect) const;
425 //===------------------------------------------------------------------===//
426 // Dwarf Lowering Routines
427 //===------------------------------------------------------------------===//
429 /// \brief Emit frame instruction to describe the layout of the frame.
430 void emitCFIInstruction(const MCCFIInstruction &Inst) const;
432 //===------------------------------------------------------------------===//
433 // Inline Asm Support
434 //===------------------------------------------------------------------===//
436 // These are hooks that targets can override to implement inline asm
437 // support. These should probably be moved out of AsmPrinter someday.
439 /// PrintSpecial - Print information related to the specified machine instr
440 /// that is independent of the operand, and may be independent of the instr
441 /// itself. This can be useful for portably encoding the comment character
442 /// or other bits of target-specific knowledge into the asmstrings. The
443 /// syntax used is ${:comment}. Targets can override this to add support
444 /// for their own strange codes.
445 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
446 const char *Code) const;
448 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
449 /// instruction, using the specified assembler variant. Targets should
450 /// override this to format as appropriate. This method can return true if
451 /// the operand is erroneous.
452 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
453 unsigned AsmVariant, const char *ExtraCode,
456 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
457 /// instruction, using the specified assembler variant as an address.
458 /// Targets should override this to format as appropriate. This method can
459 /// return true if the operand is erroneous.
460 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
462 const char *ExtraCode, raw_ostream &OS);
465 /// Private state for PrintSpecial()
466 // Assign a unique ID to this machine instruction.
467 mutable const MachineInstr *LastMI;
468 mutable unsigned LastFn;
469 mutable unsigned Counter;
470 mutable unsigned SetCounter;
472 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
473 void EmitInlineAsm(StringRef Str, const MDNode *LocMDNode = 0,
474 InlineAsm::AsmDialect AsmDialect =
475 InlineAsm::AD_ATT) const;
477 /// EmitInlineAsm - This method formats and emits the specified machine
478 /// instruction that is an inline asm.
479 void EmitInlineAsm(const MachineInstr *MI) const;
481 //===------------------------------------------------------------------===//
482 // Internal Implementation Details
483 //===------------------------------------------------------------------===//
485 /// EmitVisibility - This emits visibility information about symbol, if
486 /// this is suported by the target.
487 void EmitVisibility(MCSymbol *Sym, unsigned Visibility,
488 bool IsDefinition = true) const;
490 void EmitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const;
492 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
493 const MachineBasicBlock *MBB, unsigned uid) const;
494 void EmitLLVMUsedList(const ConstantArray *InitList);
495 /// Emit llvm.ident metadata in an '.ident' directive.
496 void EmitModuleIdents(Module &M);
497 void EmitXXStructorList(const Constant *List, bool isCtor);
498 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);