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;
51 class TargetLoweringObjectFile;
55 /// AsmPrinter - This class is intended to be used as a driving class for all
57 class AsmPrinter : public MachineFunctionPass {
59 /// Target machine description.
63 /// Target Asm Printer information.
67 /// OutContext - This is the context for the output file that we are
68 /// streaming. This owns all of the global MC-related objects for the
69 /// generated translation unit.
70 MCContext &OutContext;
72 /// OutStreamer - This is the MCStreamer object for the file we are
73 /// generating. This contains the transient state for the current
74 /// translation unit that we are generating (such as the current section
76 MCStreamer &OutStreamer;
78 /// The current machine function.
79 const MachineFunction *MF;
81 /// MMI - This is a pointer to the current MachineModuleInfo.
82 MachineModuleInfo *MMI;
84 /// Name-mangler for global names.
88 /// The symbol for the current function. This is recalculated at the
89 /// beginning of each call to runOnMachineFunction().
91 MCSymbol *CurrentFnSym;
93 /// The symbol used to represent the start of the current function for the
94 /// purpose of calculating its size (e.g. using the .size directive). By
95 /// default, this is equal to CurrentFnSym.
96 MCSymbol *CurrentFnSymForSize;
99 // GCMetadataPrinters - The garbage collection metadata printer table.
100 void *GCMetadataPrinters; // Really a DenseMap.
102 /// VerboseAsm - Emit comments in assembly output if this is true.
107 /// If VerboseAsm is set, a pointer to the loop info for this
111 /// DD - If the target supports dwarf debug info, this pointer is non-null.
114 /// DE - If the target supports dwarf exception info, this pointer is
119 explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer);
122 virtual ~AsmPrinter();
124 const DwarfDebug *getDwarfDebug() const { return DD; }
126 /// isVerbose - Return true if assembly output should contain comments.
128 bool isVerbose() const { return VerboseAsm; }
130 /// getFunctionNumber - Return a unique ID for the current function.
132 unsigned getFunctionNumber() const;
134 /// getObjFileLowering - Return information about object file lowering.
135 const TargetLoweringObjectFile &getObjFileLowering() const;
137 /// getDataLayout - Return information about data layout.
138 const DataLayout &getDataLayout() const;
140 /// getTargetTriple - Return the target triple string.
141 StringRef getTargetTriple() const;
143 /// getCurrentSection() - Return the current section we are emitting to.
144 const MCSection *getCurrentSection() const;
147 //===------------------------------------------------------------------===//
148 // MachineFunctionPass Implementation.
149 //===------------------------------------------------------------------===//
151 /// getAnalysisUsage - Record analysis usage.
153 void getAnalysisUsage(AnalysisUsage &AU) const;
155 /// doInitialization - Set up the AsmPrinter when we are working on a new
156 /// module. If your pass overrides this, it must make sure to explicitly
157 /// call this implementation.
158 bool doInitialization(Module &M);
160 /// doFinalization - Shut down the asmprinter. If you override this in your
161 /// pass, you must make sure to call it explicitly.
162 bool doFinalization(Module &M);
164 /// runOnMachineFunction - Emit the specified function out to the
166 virtual bool runOnMachineFunction(MachineFunction &MF) {
167 SetupMachineFunction(MF);
168 EmitFunctionHeader();
173 //===------------------------------------------------------------------===//
174 // Coarse grained IR lowering routines.
175 //===------------------------------------------------------------------===//
177 /// SetupMachineFunction - This should be called when a new MachineFunction
178 /// is being processed from runOnMachineFunction.
179 void SetupMachineFunction(MachineFunction &MF);
181 /// EmitFunctionHeader - This method emits the header for the current
183 void EmitFunctionHeader();
185 /// EmitFunctionBody - This method emits the body and trailer for a
187 void EmitFunctionBody();
189 void emitPrologLabel(const MachineInstr &MI);
196 CFIMoveType needsCFIMoves();
198 bool needsSEHMoves();
200 /// needsRelocationsForDwarfStringPool - Specifies whether the object format
201 /// expects to use relocations to refer to debug entries. Alternatively we
202 /// emit section offsets in bytes from the start of the string pool.
203 bool needsRelocationsForDwarfStringPool() const;
205 /// EmitConstantPool - Print to the current output stream assembly
206 /// representations of the constants in the constant pool MCP. This is
207 /// used to print out constants which have been "spilled to memory" by
208 /// the code generator.
210 virtual void EmitConstantPool();
212 /// EmitJumpTableInfo - Print assembly representations of the jump tables
213 /// used by the current function to the current output stream.
215 void EmitJumpTableInfo();
217 /// EmitGlobalVariable - Emit the specified global variable to the .s file.
218 virtual void EmitGlobalVariable(const GlobalVariable *GV);
220 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
221 /// special global used by LLVM. If so, emit it and return true, otherwise
222 /// do nothing and return false.
223 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
225 /// EmitAlignment - Emit an alignment directive to the specified power of
226 /// two boundary. For example, if you pass in 3 here, you will get an 8
227 /// byte alignment. If a global value is specified, and if that global has
228 /// an explicit alignment requested, it will override the alignment request
229 /// if required for correctness.
231 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
233 /// EmitBasicBlockStart - This method prints the label for the specified
234 /// MachineBasicBlock, an alignment (if present) and a comment describing
235 /// it if appropriate.
236 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
238 /// \brief Print a general LLVM constant to the .s file.
239 void EmitGlobalConstant(const Constant *CV);
242 //===------------------------------------------------------------------===//
244 //===------------------------------------------------------------------===//
246 // Targets can, or in the case of EmitInstruction, must implement these to
249 /// EmitStartOfAsmFile - This virtual method can be overridden by targets
250 /// that want to emit something at the start of their file.
251 virtual void EmitStartOfAsmFile(Module &) {}
253 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
254 /// want to emit something at the end of their file.
255 virtual void EmitEndOfAsmFile(Module &) {}
257 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
258 /// the first basic block in the function.
259 virtual void EmitFunctionBodyStart() {}
261 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
262 /// the last basic block in the function.
263 virtual void EmitFunctionBodyEnd() {}
265 /// EmitInstruction - Targets should implement this to emit instructions.
266 virtual void EmitInstruction(const MachineInstr *) {
267 llvm_unreachable("EmitInstruction not implemented");
270 virtual void EmitFunctionEntryLabel();
272 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
274 /// EmitXXStructor - Targets can override this to change how global
275 /// constants that are part of a C++ static/global constructor list are
277 virtual void EmitXXStructor(const Constant *CV) {
278 EmitGlobalConstant(CV);
281 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
282 /// exactly one predecessor and the control transfer mechanism between
283 /// the predecessor and this block is a fall-through.
285 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
287 //===------------------------------------------------------------------===//
288 // Symbol Lowering Routines.
289 //===------------------------------------------------------------------===//
292 /// GetTempSymbol - Return the MCSymbol corresponding to the assembler
293 /// temporary label with the specified stem and unique ID.
294 MCSymbol *GetTempSymbol(StringRef Name, unsigned ID) const;
296 /// GetTempSymbol - Return an assembler temporary label with the specified
298 MCSymbol *GetTempSymbol(StringRef Name) const;
301 /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
302 /// global value name as its base, with the specified suffix, and where the
303 /// symbol is forced to have private linkage if ForcePrivate is true.
304 MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
306 bool ForcePrivate = true) const;
308 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
310 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
312 /// GetCPISymbol - Return the symbol for the specified constant pool entry.
313 MCSymbol *GetCPISymbol(unsigned CPID) const;
315 /// GetJTISymbol - Return the symbol for the specified jump table entry.
316 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
318 /// GetJTSetSymbol - Return the symbol for the specified jump table .set
319 /// FIXME: privatize to AsmPrinter.
320 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
322 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
323 /// uses of the specified basic block.
324 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
325 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
327 //===------------------------------------------------------------------===//
328 // Emission Helper Routines.
329 //===------------------------------------------------------------------===//
331 /// printOffset - This is just convenient handler for printing offsets.
332 void printOffset(int64_t Offset, raw_ostream &OS) const;
334 /// EmitInt8 - Emit a byte directive and value.
336 void EmitInt8(int Value) const;
338 /// EmitInt16 - Emit a short directive and value.
340 void EmitInt16(int Value) const;
342 /// EmitInt32 - Emit a long directive and value.
344 void EmitInt32(int Value) const;
346 /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
347 /// in bytes of the directive is specified by Size and Hi/Lo specify the
348 /// labels. This implicitly uses .set if it is available.
349 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
350 unsigned Size) const;
352 /// EmitLabelOffsetDifference - Emit something like ".long Hi+Offset-Lo"
353 /// where the size in bytes of the directive is specified by Size and Hi/Lo
354 /// specify the labels. This implicitly uses .set if it is available.
355 void EmitLabelOffsetDifference(const MCSymbol *Hi, uint64_t Offset,
356 const MCSymbol *Lo, unsigned Size) const;
358 /// EmitLabelPlusOffset - Emit something like ".long Label+Offset"
359 /// where the size in bytes of the directive is specified by Size and Label
360 /// specifies the label. This implicitly uses .set if it is available.
361 void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
363 bool IsSectionRelative = false) const;
365 /// EmitLabelReference - Emit something like ".long Label"
366 /// where the size in bytes of the directive is specified by Size and Label
367 /// specifies the label.
368 void EmitLabelReference(const MCSymbol *Label, unsigned Size,
369 bool IsSectionRelative = false) const {
370 EmitLabelPlusOffset(Label, 0, Size, IsSectionRelative);
373 //===------------------------------------------------------------------===//
374 // Dwarf Emission Helper Routines
375 //===------------------------------------------------------------------===//
377 /// EmitSLEB128 - emit the specified signed leb128 value.
378 void EmitSLEB128(int64_t Value, const char *Desc = 0) const;
380 /// EmitULEB128 - emit the specified unsigned leb128 value.
381 void EmitULEB128(uint64_t Value, const char *Desc = 0,
382 unsigned PadTo = 0) const;
384 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
385 void EmitCFAByte(unsigned Val) const;
387 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
388 /// encoding. If verbose assembly output is enabled, we output comments
389 /// describing the encoding. Desc is a string saying what the encoding is
390 /// specifying (e.g. "LSDA").
391 void EmitEncodingByte(unsigned Val, const char *Desc = 0) const;
393 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
394 unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
396 /// EmitReference - Emit reference to a ttype global with a specified encoding.
397 void EmitTTypeReference(const GlobalValue *GV, unsigned Encoding) const;
399 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of
400 /// its section. This can be done with a special directive if the target
401 /// supports it (e.g. cygwin) or by emitting it as an offset from a label at
402 /// the start of the section.
404 /// SectionLabel is a temporary label emitted at the start of the section
405 /// that Label lives in.
406 void EmitSectionOffset(const MCSymbol *Label,
407 const MCSymbol *SectionLabel) const;
409 /// getISAEncoding - Get the value for DW_AT_APPLE_isa. Zero if no isa
410 /// encoding specified.
411 virtual unsigned getISAEncoding() { return 0; }
413 /// EmitDwarfRegOp - Emit dwarf register operation.
414 virtual void EmitDwarfRegOp(const MachineLocation &MLoc,
415 bool Indirect) const;
417 //===------------------------------------------------------------------===//
418 // Dwarf Lowering Routines
419 //===------------------------------------------------------------------===//
421 /// \brief Emit frame instruction to describe the layout of the frame.
422 void emitCFIInstruction(const MCCFIInstruction &Inst) const;
424 //===------------------------------------------------------------------===//
425 // Inline Asm Support
426 //===------------------------------------------------------------------===//
428 // These are hooks that targets can override to implement inline asm
429 // support. These should probably be moved out of AsmPrinter someday.
431 /// PrintSpecial - Print information related to the specified machine instr
432 /// that is independent of the operand, and may be independent of the instr
433 /// itself. This can be useful for portably encoding the comment character
434 /// or other bits of target-specific knowledge into the asmstrings. The
435 /// syntax used is ${:comment}. Targets can override this to add support
436 /// for their own strange codes.
437 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
438 const char *Code) const;
440 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
441 /// instruction, using the specified assembler variant. Targets should
442 /// override this to format as appropriate. This method can return true if
443 /// the operand is erroneous.
444 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
445 unsigned AsmVariant, const char *ExtraCode,
448 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
449 /// instruction, using the specified assembler variant as an address.
450 /// Targets should override this to format as appropriate. This method can
451 /// return true if the operand is erroneous.
452 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
454 const char *ExtraCode,
458 /// Private state for PrintSpecial()
459 // Assign a unique ID to this machine instruction.
460 mutable const MachineInstr *LastMI;
461 mutable unsigned LastFn;
462 mutable unsigned Counter;
463 mutable unsigned SetCounter;
465 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
466 void EmitInlineAsm(StringRef Str, const MDNode *LocMDNode = 0,
467 InlineAsm::AsmDialect AsmDialect = InlineAsm::AD_ATT) const;
469 /// EmitInlineAsm - This method formats and emits the specified machine
470 /// instruction that is an inline asm.
471 void EmitInlineAsm(const MachineInstr *MI) const;
473 //===------------------------------------------------------------------===//
474 // Internal Implementation Details
475 //===------------------------------------------------------------------===//
477 /// EmitVisibility - This emits visibility information about symbol, if
478 /// this is suported by the target.
479 void EmitVisibility(MCSymbol *Sym, unsigned Visibility,
480 bool IsDefinition = true) const;
482 void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const;
484 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
485 const MachineBasicBlock *MBB,
487 void EmitLLVMUsedList(const ConstantArray *InitList);
488 void EmitXXStructorList(const Constant *List, bool isCtor);
489 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);