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/Support/DataTypes.h"
31 class GCMetadataPrinter;
34 class MachineBasicBlock;
35 class MachineFunction;
37 class MachineLocation;
38 class MachineLoopInfo;
40 class MachineConstantPool;
41 class MachineConstantPoolEntry;
42 class MachineConstantPoolValue;
43 class MachineJumpTableInfo;
44 class MachineModuleInfo;
56 class TargetLoweringObjectFile;
62 /// AsmPrinter - This class is intended to be used as a driving class for all
64 class AsmPrinter : public MachineFunctionPass {
66 /// Target machine description.
70 /// Target Asm Printer information.
74 /// OutContext - This is the context for the output file that we are
75 /// streaming. This owns all of the global MC-related objects for the
76 /// generated translation unit.
77 MCContext &OutContext;
79 /// OutStreamer - This is the MCStreamer object for the file we are
80 /// generating. This contains the transient state for the current
81 /// translation unit that we are generating (such as the current section
83 MCStreamer &OutStreamer;
85 /// The current machine function.
86 const MachineFunction *MF;
88 /// MMI - This is a pointer to the current MachineModuleInfo.
89 MachineModuleInfo *MMI;
91 /// Name-mangler for global names.
95 /// The symbol for the current function. This is recalculated at the
96 /// beginning of each call to runOnMachineFunction().
98 MCSymbol *CurrentFnSym;
101 // GCMetadataPrinters - The garbage collection metadata printer table.
102 void *GCMetadataPrinters; // Really a DenseMap.
104 /// VerboseAsm - Emit comments in assembly output if this is true.
109 /// If VerboseAsm is set, a pointer to the loop info for this
113 /// DD - If the target supports dwarf debug info, this pointer is non-null.
116 /// DE - If the target supports dwarf exception info, this pointer is
121 explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer);
124 virtual ~AsmPrinter();
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 /// getTargetData - Return information about data layout.
138 const TargetData &getTargetData() const;
140 /// getCurrentSection() - Return the current section we are emitting to.
141 const MCSection *getCurrentSection() const;
144 //===------------------------------------------------------------------===//
145 // MachineFunctionPass Implementation.
146 //===------------------------------------------------------------------===//
148 /// getAnalysisUsage - Record analysis usage.
150 void getAnalysisUsage(AnalysisUsage &AU) const;
152 /// doInitialization - Set up the AsmPrinter when we are working on a new
153 /// module. If your pass overrides this, it must make sure to explicitly
154 /// call this implementation.
155 bool doInitialization(Module &M);
157 /// doFinalization - Shut down the asmprinter. If you override this in your
158 /// pass, you must make sure to call it explicitly.
159 bool doFinalization(Module &M);
161 /// runOnMachineFunction - Emit the specified function out to the
163 virtual bool runOnMachineFunction(MachineFunction &MF) {
164 SetupMachineFunction(MF);
165 EmitFunctionHeader();
170 //===------------------------------------------------------------------===//
171 // Coarse grained IR lowering routines.
172 //===------------------------------------------------------------------===//
174 /// SetupMachineFunction - This should be called when a new MachineFunction
175 /// is being processed from runOnMachineFunction.
176 void SetupMachineFunction(MachineFunction &MF);
178 /// EmitFunctionHeader - This method emits the header for the current
180 void EmitFunctionHeader();
182 /// EmitFunctionBody - This method emits the body and trailer for a
184 void EmitFunctionBody();
186 void emitPrologLabel(const MachineInstr &MI);
193 CFIMoveType needsCFIMoves();
195 bool needsSEHMoves();
197 /// needsRelocationsForDwarfStringPool - Specifies whether the object format
198 /// expects to use relocations to refer to debug entries. Alternatively we
199 /// emit section offsets in bytes from the start of the string pool.
200 bool needsRelocationsForDwarfStringPool() const;
202 /// EmitConstantPool - Print to the current output stream assembly
203 /// representations of the constants in the constant pool MCP. This is
204 /// used to print out constants which have been "spilled to memory" by
205 /// the code generator.
207 virtual void EmitConstantPool();
209 /// EmitJumpTableInfo - Print assembly representations of the jump tables
210 /// used by the current function to the current output stream.
212 void EmitJumpTableInfo();
214 /// EmitGlobalVariable - Emit the specified global variable to the .s file.
215 virtual void EmitGlobalVariable(const GlobalVariable *GV);
217 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
218 /// special global used by LLVM. If so, emit it and return true, otherwise
219 /// do nothing and return false.
220 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
222 /// EmitAlignment - Emit an alignment directive to the specified power of
223 /// two boundary. For example, if you pass in 3 here, you will get an 8
224 /// byte alignment. If a global value is specified, and if that global has
225 /// an explicit alignment requested, it will override the alignment request
226 /// if required for correctness.
228 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
230 /// EmitBasicBlockStart - This method prints the label for the specified
231 /// MachineBasicBlock, an alignment (if present) and a comment describing
232 /// it if appropriate.
233 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
235 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
236 void EmitGlobalConstant(const Constant *CV, unsigned AddrSpace = 0);
239 //===------------------------------------------------------------------===//
241 //===------------------------------------------------------------------===//
243 // Targets can, or in the case of EmitInstruction, must implement these to
246 /// EmitStartOfAsmFile - This virtual method can be overridden by targets
247 /// that want to emit something at the start of their file.
248 virtual void EmitStartOfAsmFile(Module &) {}
250 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
251 /// want to emit something at the end of their file.
252 virtual void EmitEndOfAsmFile(Module &) {}
254 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
255 /// the first basic block in the function.
256 virtual void EmitFunctionBodyStart() {}
258 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
259 /// the last basic block in the function.
260 virtual void EmitFunctionBodyEnd() {}
262 /// EmitInstruction - Targets should implement this to emit instructions.
263 virtual void EmitInstruction(const MachineInstr *) {
264 assert(0 && "EmitInstruction not implemented");
267 virtual void EmitFunctionEntryLabel();
269 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
271 /// EmitXXStructor - Targets can override this to change how global constants
272 /// that are part of a C++ static/global constructor list are emitted.
273 virtual void EmitXXStructor(const Constant *CV) {
274 EmitGlobalConstant(CV);
277 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
278 /// exactly one predecessor and the control transfer mechanism between
279 /// the predecessor and this block is a fall-through.
281 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
283 //===------------------------------------------------------------------===//
284 // Symbol Lowering Routines.
285 //===------------------------------------------------------------------===//
288 /// GetTempSymbol - Return the MCSymbol corresponding to the assembler
289 /// temporary label with the specified stem and unique ID.
290 MCSymbol *GetTempSymbol(StringRef Name, unsigned ID) const;
292 /// GetTempSymbol - Return an assembler temporary label with the specified
294 MCSymbol *GetTempSymbol(StringRef Name) const;
297 /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
298 /// global value name as its base, with the specified suffix, and where the
299 /// symbol is forced to have private linkage if ForcePrivate is true.
300 MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
302 bool ForcePrivate = true) const;
304 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
306 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
308 /// GetCPISymbol - Return the symbol for the specified constant pool entry.
309 MCSymbol *GetCPISymbol(unsigned CPID) const;
311 /// GetJTISymbol - Return the symbol for the specified jump table entry.
312 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
314 /// GetJTSetSymbol - Return the symbol for the specified jump table .set
315 /// FIXME: privatize to AsmPrinter.
316 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
318 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
319 /// uses of the specified basic block.
320 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
321 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
323 //===------------------------------------------------------------------===//
324 // Emission Helper Routines.
325 //===------------------------------------------------------------------===//
327 /// printOffset - This is just convenient handler for printing offsets.
328 void printOffset(int64_t Offset, raw_ostream &OS) const;
330 /// EmitInt8 - Emit a byte directive and value.
332 void EmitInt8(int Value) const;
334 /// EmitInt16 - Emit a short directive and value.
336 void EmitInt16(int Value) const;
338 /// EmitInt32 - Emit a long directive and value.
340 void EmitInt32(int Value) const;
342 /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
343 /// in bytes of the directive is specified by Size and Hi/Lo specify the
344 /// labels. This implicitly uses .set if it is available.
345 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
346 unsigned Size) const;
348 /// EmitLabelOffsetDifference - Emit something like ".long Hi+Offset-Lo"
349 /// where the size in bytes of the directive is specified by Size and Hi/Lo
350 /// specify the labels. This implicitly uses .set if it is available.
351 void EmitLabelOffsetDifference(const MCSymbol *Hi, uint64_t Offset,
352 const MCSymbol *Lo, unsigned Size) const;
354 /// EmitLabelPlusOffset - Emit something like ".long Label+Offset"
355 /// where the size in bytes of the directive is specified by Size and Label
356 /// specifies the label. This implicitly uses .set if it is available.
357 void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
358 unsigned Size) const;
360 //===------------------------------------------------------------------===//
361 // Dwarf Emission Helper Routines
362 //===------------------------------------------------------------------===//
364 /// EmitSLEB128 - emit the specified signed leb128 value.
365 void EmitSLEB128(int Value, const char *Desc = 0) const;
367 /// EmitULEB128 - emit the specified unsigned leb128 value.
368 void EmitULEB128(unsigned Value, const char *Desc = 0,
369 unsigned PadTo = 0) const;
371 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
372 void EmitCFAByte(unsigned Val) const;
374 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
375 /// encoding. If verbose assembly output is enabled, we output comments
376 /// describing the encoding. Desc is a string saying what the encoding is
377 /// specifying (e.g. "LSDA").
378 void EmitEncodingByte(unsigned Val, const char *Desc = 0) const;
380 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
381 unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
383 /// EmitReference - Emit a reference to a label with a specified encoding.
385 void EmitReference(const MCSymbol *Sym, unsigned Encoding) const;
386 void EmitReference(const GlobalValue *GV, unsigned Encoding) const;
388 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of
389 /// its section. This can be done with a special directive if the target
390 /// supports it (e.g. cygwin) or by emitting it as an offset from a label at
391 /// the start of the section.
393 /// SectionLabel is a temporary label emitted at the start of the section
394 /// that Label lives in.
395 void EmitSectionOffset(const MCSymbol *Label,
396 const MCSymbol *SectionLabel) const;
398 /// getDebugValueLocation - Get location information encoded by DBG_VALUE
400 virtual MachineLocation getDebugValueLocation(const MachineInstr *MI) const;
402 /// getISAEncoding - Get the value for DW_AT_APPLE_isa. Zero if no isa
403 /// encoding specified.
404 virtual unsigned getISAEncoding() { return 0; }
406 /// EmitDwarfRegOp - Emit dwarf register operation.
407 virtual void EmitDwarfRegOp(const MachineLocation &MLoc) const;
409 //===------------------------------------------------------------------===//
410 // Dwarf Lowering Routines
411 //===------------------------------------------------------------------===//
413 /// EmitCFIFrameMove - Emit frame instruction to describe the layout of the
415 void EmitCFIFrameMove(const MachineMove &Move) const;
417 //===------------------------------------------------------------------===//
418 // Inline Asm Support
419 //===------------------------------------------------------------------===//
421 // These are hooks that targets can override to implement inline asm
422 // support. These should probably be moved out of AsmPrinter someday.
424 /// PrintSpecial - Print information related to the specified machine instr
425 /// that is independent of the operand, and may be independent of the instr
426 /// itself. This can be useful for portably encoding the comment character
427 /// or other bits of target-specific knowledge into the asmstrings. The
428 /// syntax used is ${:comment}. Targets can override this to add support
429 /// for their own strange codes.
430 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
431 const char *Code) const;
433 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
434 /// instruction, using the specified assembler variant. Targets should
435 /// override this to format as appropriate. This method can return true if
436 /// the operand is erroneous.
437 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
438 unsigned AsmVariant, const char *ExtraCode,
441 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
442 /// instruction, using the specified assembler variant as an address.
443 /// Targets should override this to format as appropriate. This method can
444 /// return true if the operand is erroneous.
445 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
447 const char *ExtraCode,
451 /// Private state for PrintSpecial()
452 // Assign a unique ID to this machine instruction.
453 mutable const MachineInstr *LastMI;
454 mutable unsigned LastFn;
455 mutable unsigned Counter;
456 mutable unsigned SetCounter;
458 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
459 void EmitInlineAsm(StringRef Str, const MDNode *LocMDNode = 0) const;
461 /// EmitInlineAsm - This method formats and emits the specified machine
462 /// instruction that is an inline asm.
463 void EmitInlineAsm(const MachineInstr *MI) const;
465 //===------------------------------------------------------------------===//
466 // Internal Implementation Details
467 //===------------------------------------------------------------------===//
469 /// EmitVisibility - This emits visibility information about symbol, if
470 /// this is suported by the target.
471 void EmitVisibility(MCSymbol *Sym, unsigned Visibility,
472 bool IsDefinition = true) const;
474 void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const;
476 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
477 const MachineBasicBlock *MBB,
479 void EmitLLVMUsedList(const Constant *List);
480 void EmitXXStructorList(const Constant *List, bool isCtor);
481 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);