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;
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 /// isVerbose - Return true if assembly output should contain comments.
126 bool isVerbose() const { return VerboseAsm; }
128 /// getFunctionNumber - Return a unique ID for the current function.
130 unsigned getFunctionNumber() const;
132 /// getObjFileLowering - Return information about object file lowering.
133 const TargetLoweringObjectFile &getObjFileLowering() const;
135 /// getDataLayout - Return information about data layout.
136 const DataLayout &getDataLayout() const;
138 /// getCurrentSection() - Return the current section we are emitting to.
139 const MCSection *getCurrentSection() const;
142 //===------------------------------------------------------------------===//
143 // MachineFunctionPass Implementation.
144 //===------------------------------------------------------------------===//
146 /// getAnalysisUsage - Record analysis usage.
148 void getAnalysisUsage(AnalysisUsage &AU) const;
150 /// doInitialization - Set up the AsmPrinter when we are working on a new
151 /// module. If your pass overrides this, it must make sure to explicitly
152 /// call this implementation.
153 bool doInitialization(Module &M);
155 /// doFinalization - Shut down the asmprinter. If you override this in your
156 /// pass, you must make sure to call it explicitly.
157 bool doFinalization(Module &M);
159 /// runOnMachineFunction - Emit the specified function out to the
161 virtual bool runOnMachineFunction(MachineFunction &MF) {
162 SetupMachineFunction(MF);
163 EmitFunctionHeader();
168 //===------------------------------------------------------------------===//
169 // Coarse grained IR lowering routines.
170 //===------------------------------------------------------------------===//
172 /// SetupMachineFunction - This should be called when a new MachineFunction
173 /// is being processed from runOnMachineFunction.
174 void SetupMachineFunction(MachineFunction &MF);
176 /// EmitFunctionHeader - This method emits the header for the current
178 void EmitFunctionHeader();
180 /// EmitFunctionBody - This method emits the body and trailer for a
182 void EmitFunctionBody();
184 void emitPrologLabel(const MachineInstr &MI);
191 CFIMoveType needsCFIMoves();
193 bool needsSEHMoves();
195 /// needsRelocationsForDwarfStringPool - Specifies whether the object format
196 /// expects to use relocations to refer to debug entries. Alternatively we
197 /// emit section offsets in bytes from the start of the string pool.
198 bool needsRelocationsForDwarfStringPool() const;
200 /// EmitConstantPool - Print to the current output stream assembly
201 /// representations of the constants in the constant pool MCP. This is
202 /// used to print out constants which have been "spilled to memory" by
203 /// the code generator.
205 virtual void EmitConstantPool();
207 /// EmitJumpTableInfo - Print assembly representations of the jump tables
208 /// used by the current function to the current output stream.
210 void EmitJumpTableInfo();
212 /// EmitGlobalVariable - Emit the specified global variable to the .s file.
213 virtual void EmitGlobalVariable(const GlobalVariable *GV);
215 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
216 /// special global used by LLVM. If so, emit it and return true, otherwise
217 /// do nothing and return false.
218 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
220 /// EmitAlignment - Emit an alignment directive to the specified power of
221 /// two boundary. For example, if you pass in 3 here, you will get an 8
222 /// byte alignment. If a global value is specified, and if that global has
223 /// an explicit alignment requested, it will override the alignment request
224 /// if required for correctness.
226 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
228 /// EmitBasicBlockStart - This method prints the label for the specified
229 /// MachineBasicBlock, an alignment (if present) and a comment describing
230 /// it if appropriate.
231 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
233 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
234 void EmitGlobalConstant(const Constant *CV, unsigned AddrSpace = 0);
237 //===------------------------------------------------------------------===//
239 //===------------------------------------------------------------------===//
241 // Targets can, or in the case of EmitInstruction, must implement these to
244 /// EmitStartOfAsmFile - This virtual method can be overridden by targets
245 /// that want to emit something at the start of their file.
246 virtual void EmitStartOfAsmFile(Module &) {}
248 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
249 /// want to emit something at the end of their file.
250 virtual void EmitEndOfAsmFile(Module &) {}
252 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
253 /// the first basic block in the function.
254 virtual void EmitFunctionBodyStart() {}
256 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
257 /// the last basic block in the function.
258 virtual void EmitFunctionBodyEnd() {}
260 /// EmitInstruction - Targets should implement this to emit instructions.
261 virtual void EmitInstruction(const MachineInstr *) {
262 llvm_unreachable("EmitInstruction not implemented");
265 virtual void EmitFunctionEntryLabel();
267 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
269 /// EmitXXStructor - Targets can override this to change how global
270 /// constants that are part of a C++ static/global constructor list are
272 virtual void EmitXXStructor(const Constant *CV) {
273 EmitGlobalConstant(CV);
276 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
277 /// exactly one predecessor and the control transfer mechanism between
278 /// the predecessor and this block is a fall-through.
280 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
282 //===------------------------------------------------------------------===//
283 // Symbol Lowering Routines.
284 //===------------------------------------------------------------------===//
287 /// GetTempSymbol - Return the MCSymbol corresponding to the assembler
288 /// temporary label with the specified stem and unique ID.
289 MCSymbol *GetTempSymbol(StringRef Name, unsigned ID) const;
291 /// GetTempSymbol - Return an assembler temporary label with the specified
293 MCSymbol *GetTempSymbol(StringRef Name) const;
296 /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
297 /// global value name as its base, with the specified suffix, and where the
298 /// symbol is forced to have private linkage if ForcePrivate is true.
299 MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
301 bool ForcePrivate = true) const;
303 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
305 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
307 /// GetCPISymbol - Return the symbol for the specified constant pool entry.
308 MCSymbol *GetCPISymbol(unsigned CPID) const;
310 /// GetJTISymbol - Return the symbol for the specified jump table entry.
311 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
313 /// GetJTSetSymbol - Return the symbol for the specified jump table .set
314 /// FIXME: privatize to AsmPrinter.
315 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
317 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
318 /// uses of the specified basic block.
319 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
320 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
322 //===------------------------------------------------------------------===//
323 // Emission Helper Routines.
324 //===------------------------------------------------------------------===//
326 /// printOffset - This is just convenient handler for printing offsets.
327 void printOffset(int64_t Offset, raw_ostream &OS) const;
329 /// EmitInt8 - Emit a byte directive and value.
331 void EmitInt8(int Value) const;
333 /// EmitInt16 - Emit a short directive and value.
335 void EmitInt16(int Value) const;
337 /// EmitInt32 - Emit a long directive and value.
339 void EmitInt32(int Value) const;
341 /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
342 /// in bytes of the directive is specified by Size and Hi/Lo specify the
343 /// labels. This implicitly uses .set if it is available.
344 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
345 unsigned Size) const;
347 /// EmitLabelOffsetDifference - Emit something like ".long Hi+Offset-Lo"
348 /// where the size in bytes of the directive is specified by Size and Hi/Lo
349 /// specify the labels. This implicitly uses .set if it is available.
350 void EmitLabelOffsetDifference(const MCSymbol *Hi, uint64_t Offset,
351 const MCSymbol *Lo, unsigned Size) const;
353 /// EmitLabelPlusOffset - Emit something like ".long Label+Offset"
354 /// where the size in bytes of the directive is specified by Size and Label
355 /// specifies the label. This implicitly uses .set if it is available.
356 void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
357 unsigned Size) const;
359 /// EmitLabelReference - Emit something like ".long Label"
360 /// where the size in bytes of the directive is specified by Size and Label
361 /// specifies the label.
362 void EmitLabelReference(const MCSymbol *Label, unsigned Size) const {
363 EmitLabelPlusOffset(Label, 0, Size);
366 //===------------------------------------------------------------------===//
367 // Dwarf Emission Helper Routines
368 //===------------------------------------------------------------------===//
370 /// EmitSLEB128 - emit the specified signed leb128 value.
371 void EmitSLEB128(int Value, const char *Desc = 0) const;
373 /// EmitULEB128 - emit the specified unsigned leb128 value.
374 void EmitULEB128(unsigned Value, const char *Desc = 0,
375 unsigned PadTo = 0) const;
377 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
378 void EmitCFAByte(unsigned Val) const;
380 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
381 /// encoding. If verbose assembly output is enabled, we output comments
382 /// describing the encoding. Desc is a string saying what the encoding is
383 /// specifying (e.g. "LSDA").
384 void EmitEncodingByte(unsigned Val, const char *Desc = 0) const;
386 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
387 unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
389 /// EmitReference - Emit reference to a ttype global with a specified encoding.
390 void EmitTTypeReference(const GlobalValue *GV, unsigned Encoding) const;
392 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of
393 /// its section. This can be done with a special directive if the target
394 /// supports it (e.g. cygwin) or by emitting it as an offset from a label at
395 /// the start of the section.
397 /// SectionLabel is a temporary label emitted at the start of the section
398 /// that Label lives in.
399 void EmitSectionOffset(const MCSymbol *Label,
400 const MCSymbol *SectionLabel) const;
402 /// getDebugValueLocation - Get location information encoded by DBG_VALUE
404 virtual MachineLocation getDebugValueLocation(const MachineInstr *MI) const;
406 /// getISAEncoding - Get the value for DW_AT_APPLE_isa. Zero if no isa
407 /// encoding specified.
408 virtual unsigned getISAEncoding() { return 0; }
410 /// EmitDwarfRegOp - Emit dwarf register operation.
411 virtual void EmitDwarfRegOp(const MachineLocation &MLoc) const;
413 //===------------------------------------------------------------------===//
414 // Dwarf Lowering Routines
415 //===------------------------------------------------------------------===//
417 /// EmitCFIFrameMove - Emit frame instruction to describe the layout of the
419 void EmitCFIFrameMove(const MachineMove &Move) const;
421 //===------------------------------------------------------------------===//
422 // Inline Asm Support
423 //===------------------------------------------------------------------===//
425 // These are hooks that targets can override to implement inline asm
426 // support. These should probably be moved out of AsmPrinter someday.
428 /// PrintSpecial - Print information related to the specified machine instr
429 /// that is independent of the operand, and may be independent of the instr
430 /// itself. This can be useful for portably encoding the comment character
431 /// or other bits of target-specific knowledge into the asmstrings. The
432 /// syntax used is ${:comment}. Targets can override this to add support
433 /// for their own strange codes.
434 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
435 const char *Code) const;
437 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
438 /// instruction, using the specified assembler variant. Targets should
439 /// override this to format as appropriate. This method can return true if
440 /// the operand is erroneous.
441 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
442 unsigned AsmVariant, const char *ExtraCode,
445 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
446 /// instruction, using the specified assembler variant as an address.
447 /// Targets should override this to format as appropriate. This method can
448 /// return true if the operand is erroneous.
449 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
451 const char *ExtraCode,
455 /// Private state for PrintSpecial()
456 // Assign a unique ID to this machine instruction.
457 mutable const MachineInstr *LastMI;
458 mutable unsigned LastFn;
459 mutable unsigned Counter;
460 mutable unsigned SetCounter;
462 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
463 void EmitInlineAsm(StringRef Str, const MDNode *LocMDNode = 0,
464 InlineAsm::AsmDialect AsmDialect = InlineAsm::AD_ATT) const;
466 /// EmitInlineAsm - This method formats and emits the specified machine
467 /// instruction that is an inline asm.
468 void EmitInlineAsm(const MachineInstr *MI) const;
470 //===------------------------------------------------------------------===//
471 // Internal Implementation Details
472 //===------------------------------------------------------------------===//
474 /// EmitVisibility - This emits visibility information about symbol, if
475 /// this is suported by the target.
476 void EmitVisibility(MCSymbol *Sym, unsigned Visibility,
477 bool IsDefinition = true) const;
479 void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const;
481 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
482 const MachineBasicBlock *MBB,
484 void EmitLLVMUsedList(const ConstantArray *InitList);
485 void EmitXXStructorList(const Constant *List, bool isCtor);
486 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);