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/System/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;
55 class TargetLoweringObjectFile;
61 /// AsmPrinter - This class is intended to be used as a driving class for all
63 class AsmPrinter : public MachineFunctionPass {
65 /// Target machine description.
69 /// Target Asm Printer information.
73 /// OutContext - This is the context for the output file that we are
74 /// streaming. This owns all of the global MC-related objects for the
75 /// generated translation unit.
76 MCContext &OutContext;
78 /// OutStreamer - This is the MCStreamer object for the file we are
79 /// generating. This contains the transient state for the current
80 /// translation unit that we are generating (such as the current section
82 MCStreamer &OutStreamer;
84 /// The current machine function.
85 const MachineFunction *MF;
87 /// MMI - This is a pointer to the current MachineModuleInfo.
88 MachineModuleInfo *MMI;
90 /// Name-mangler for global names.
94 /// The symbol for the current function. This is recalculated at the
95 /// beginning of each call to runOnMachineFunction().
97 MCSymbol *CurrentFnSym;
100 // GCMetadataPrinters - The garbage collection metadata printer table.
101 void *GCMetadataPrinters; // Really a DenseMap.
103 /// VerboseAsm - Emit comments in assembly output if this is true.
108 /// If VerboseAsm is set, a pointer to the loop info for this
112 /// DD - If the target supports dwarf debug info, this pointer is non-null.
115 /// DE - If the target supports dwarf exception info, this pointer is
120 explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer);
123 virtual ~AsmPrinter();
125 /// isVerbose - Return true if assembly output should contain comments.
127 bool isVerbose() const { return VerboseAsm; }
129 /// getFunctionNumber - Return a unique ID for the current function.
131 unsigned getFunctionNumber() const;
133 /// getObjFileLowering - Return information about object file lowering.
134 const TargetLoweringObjectFile &getObjFileLowering() const;
136 /// getTargetData - Return information about data layout.
137 const TargetData &getTargetData() const;
139 /// getCurrentSection() - Return the current section we are emitting to.
140 const MCSection *getCurrentSection() const;
143 //===------------------------------------------------------------------===//
144 // MachineFunctionPass Implementation.
145 //===------------------------------------------------------------------===//
147 /// getAnalysisUsage - Record analysis usage.
149 void getAnalysisUsage(AnalysisUsage &AU) const;
151 /// doInitialization - Set up the AsmPrinter when we are working on a new
152 /// module. If your pass overrides this, it must make sure to explicitly
153 /// call this implementation.
154 bool doInitialization(Module &M);
156 /// doFinalization - Shut down the asmprinter. If you override this in your
157 /// pass, you must make sure to call it explicitly.
158 bool doFinalization(Module &M);
160 /// runOnMachineFunction - Emit the specified function out to the
162 virtual bool runOnMachineFunction(MachineFunction &MF) {
163 SetupMachineFunction(MF);
164 EmitFunctionHeader();
169 //===------------------------------------------------------------------===//
170 // Coarse grained IR lowering routines.
171 //===------------------------------------------------------------------===//
173 /// SetupMachineFunction - This should be called when a new MachineFunction
174 /// is being processed from runOnMachineFunction.
175 void SetupMachineFunction(MachineFunction &MF);
177 /// EmitFunctionHeader - This method emits the header for the current
179 void EmitFunctionHeader();
181 /// EmitFunctionBody - This method emits the body and trailer for a
183 void EmitFunctionBody();
185 /// EmitConstantPool - Print to the current output stream assembly
186 /// representations of the constants in the constant pool MCP. This is
187 /// used to print out constants which have been "spilled to memory" by
188 /// the code generator.
190 virtual void EmitConstantPool();
192 /// EmitJumpTableInfo - Print assembly representations of the jump tables
193 /// used by the current function to the current output stream.
195 void EmitJumpTableInfo();
197 /// EmitGlobalVariable - Emit the specified global variable to the .s file.
198 virtual void EmitGlobalVariable(const GlobalVariable *GV);
200 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
201 /// special global used by LLVM. If so, emit it and return true, otherwise
202 /// do nothing and return false.
203 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
205 /// EmitAlignment - Emit an alignment directive to the specified power of
206 /// two boundary. For example, if you pass in 3 here, you will get an 8
207 /// byte alignment. If a global value is specified, and if that global has
208 /// an explicit alignment requested, it will override the alignment request
209 /// if required for correctness.
211 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
213 /// EmitBasicBlockStart - This method prints the label for the specified
214 /// MachineBasicBlock, an alignment (if present) and a comment describing
215 /// it if appropriate.
216 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
218 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
219 void EmitGlobalConstant(const Constant *CV, unsigned AddrSpace = 0);
222 //===------------------------------------------------------------------===//
224 //===------------------------------------------------------------------===//
226 // Targets can, or in the case of EmitInstruction, must implement these to
229 /// EmitStartOfAsmFile - This virtual method can be overridden by targets
230 /// that want to emit something at the start of their file.
231 virtual void EmitStartOfAsmFile(Module &) {}
233 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
234 /// want to emit something at the end of their file.
235 virtual void EmitEndOfAsmFile(Module &) {}
237 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
238 /// the first basic block in the function.
239 virtual void EmitFunctionBodyStart() {}
241 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
242 /// the last basic block in the function.
243 virtual void EmitFunctionBodyEnd() {}
245 /// EmitInstruction - Targets should implement this to emit instructions.
246 virtual void EmitInstruction(const MachineInstr *) {
247 assert(0 && "EmitInstruction not implemented");
250 virtual void EmitFunctionEntryLabel();
252 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
254 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
255 /// exactly one predecessor and the control transfer mechanism between
256 /// the predecessor and this block is a fall-through.
258 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
260 //===------------------------------------------------------------------===//
261 // Symbol Lowering Routines.
262 //===------------------------------------------------------------------===//
265 /// GetTempSymbol - Return the MCSymbol corresponding to the assembler
266 /// temporary label with the specified stem and unique ID.
267 MCSymbol *GetTempSymbol(StringRef Name, unsigned ID) const;
269 /// GetTempSymbol - Return an assembler temporary label with the specified
271 MCSymbol *GetTempSymbol(StringRef Name) const;
274 /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
275 /// global value name as its base, with the specified suffix, and where the
276 /// symbol is forced to have private linkage if ForcePrivate is true.
277 MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
279 bool ForcePrivate = true) const;
281 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
283 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
285 /// GetCPISymbol - Return the symbol for the specified constant pool entry.
286 MCSymbol *GetCPISymbol(unsigned CPID) const;
288 /// GetJTISymbol - Return the symbol for the specified jump table entry.
289 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
291 /// GetJTSetSymbol - Return the symbol for the specified jump table .set
292 /// FIXME: privatize to AsmPrinter.
293 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
295 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
296 /// uses of the specified basic block.
297 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
298 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
300 //===------------------------------------------------------------------===//
301 // Emission Helper Routines.
302 //===------------------------------------------------------------------===//
304 /// printOffset - This is just convenient handler for printing offsets.
305 void printOffset(int64_t Offset, raw_ostream &OS) const;
307 /// EmitInt8 - Emit a byte directive and value.
309 void EmitInt8(int Value) const;
311 /// EmitInt16 - Emit a short directive and value.
313 void EmitInt16(int Value) const;
315 /// EmitInt32 - Emit a long directive and value.
317 void EmitInt32(int Value) const;
319 /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
320 /// in bytes of the directive is specified by Size and Hi/Lo specify the
321 /// labels. This implicitly uses .set if it is available.
322 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
323 unsigned Size) const;
325 /// EmitLabelOffsetDifference - Emit something like ".long Hi+Offset-Lo"
326 /// where the size in bytes of the directive is specified by Size and Hi/Lo
327 /// specify the labels. This implicitly uses .set if it is available.
328 void EmitLabelOffsetDifference(const MCSymbol *Hi, uint64_t Offset,
329 const MCSymbol *Lo, unsigned Size) const;
331 /// EmitLabelPlusOffset - Emit something like ".long Label+Offset"
332 /// where the size in bytes of the directive is specified by Size and Label
333 /// specifies the label. This implicitly uses .set if it is available.
334 void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
335 unsigned Size) const;
337 //===------------------------------------------------------------------===//
338 // Dwarf Emission Helper Routines
339 //===------------------------------------------------------------------===//
341 /// EmitSLEB128 - emit the specified signed leb128 value.
342 void EmitSLEB128(int Value, const char *Desc = 0) const;
344 /// EmitULEB128 - emit the specified unsigned leb128 value.
345 void EmitULEB128(unsigned Value, const char *Desc = 0,
346 unsigned PadTo = 0) const;
348 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
349 void EmitCFAByte(unsigned Val) const;
351 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
352 /// encoding. If verbose assembly output is enabled, we output comments
353 /// describing the encoding. Desc is a string saying what the encoding is
354 /// specifying (e.g. "LSDA").
355 void EmitEncodingByte(unsigned Val, const char *Desc = 0) const;
357 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
358 unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
360 /// EmitReference - Emit a reference to a label with a specified encoding.
362 void EmitReference(const MCSymbol *Sym, unsigned Encoding) const;
363 void EmitReference(const GlobalValue *GV, unsigned Encoding) const;
365 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of
366 /// its section. This can be done with a special directive if the target
367 /// supports it (e.g. cygwin) or by emitting it as an offset from a label at
368 /// the start of the section.
370 /// SectionLabel is a temporary label emitted at the start of the section
371 /// that Label lives in.
372 void EmitSectionOffset(const MCSymbol *Label,
373 const MCSymbol *SectionLabel) const;
375 /// getDebugValueLocation - Get location information encoded by DBG_VALUE
377 virtual MachineLocation getDebugValueLocation(const MachineInstr *MI) const;
379 /// getISAEncoding - Get the value for DW_AT_APPLE_isa. Zero if no isa
380 /// encoding specified.
381 virtual unsigned getISAEncoding() { return 0; }
383 //===------------------------------------------------------------------===//
384 // Dwarf Lowering Routines
385 //===------------------------------------------------------------------===//
387 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
389 void EmitFrameMoves(const std::vector<MachineMove> &Moves,
390 MCSymbol *BaseLabel, bool isEH) const;
393 //===------------------------------------------------------------------===//
394 // Inline Asm Support
395 //===------------------------------------------------------------------===//
397 // These are hooks that targets can override to implement inline asm
398 // support. These should probably be moved out of AsmPrinter someday.
400 /// PrintSpecial - Print information related to the specified machine instr
401 /// that is independent of the operand, and may be independent of the instr
402 /// itself. This can be useful for portably encoding the comment character
403 /// or other bits of target-specific knowledge into the asmstrings. The
404 /// syntax used is ${:comment}. Targets can override this to add support
405 /// for their own strange codes.
406 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
407 const char *Code) const;
409 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
410 /// instruction, using the specified assembler variant. Targets should
411 /// override this to format as appropriate. This method can return true if
412 /// the operand is erroneous.
413 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
414 unsigned AsmVariant, const char *ExtraCode,
417 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
418 /// instruction, using the specified assembler variant as an address.
419 /// Targets should override this to format as appropriate. This method can
420 /// return true if the operand is erroneous.
421 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
423 const char *ExtraCode,
427 /// Private state for PrintSpecial()
428 // Assign a unique ID to this machine instruction.
429 mutable const MachineInstr *LastMI;
430 mutable unsigned LastFn;
431 mutable unsigned Counter;
432 mutable unsigned SetCounter;
434 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
435 void EmitInlineAsm(StringRef Str, unsigned LocCookie) const;
437 /// EmitInlineAsm - This method formats and emits the specified machine
438 /// instruction that is an inline asm.
439 void EmitInlineAsm(const MachineInstr *MI) const;
441 //===------------------------------------------------------------------===//
442 // Internal Implementation Details
443 //===------------------------------------------------------------------===//
445 /// EmitVisibility - This emits visibility information about symbol, if
446 /// this is suported by the target.
447 void EmitVisibility(MCSymbol *Sym, unsigned Visibility) const;
449 void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const;
451 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
452 const MachineBasicBlock *MBB,
454 void EmitLLVMUsedList(Constant *List);
455 void EmitXXStructorList(Constant *List);
456 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);