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/DebugLoc.h"
31 class GCMetadataPrinter;
34 class MachineBasicBlock;
35 class MachineFunction;
37 class MachineLoopInfo;
39 class MachineConstantPool;
40 class MachineConstantPoolEntry;
41 class MachineConstantPoolValue;
42 class MachineJumpTableInfo;
43 class MachineModuleInfo;
54 class TargetLoweringObjectFile;
59 /// AsmPrinter - This class is intended to be used as a driving class for all
61 class AsmPrinter : public MachineFunctionPass {
63 /// Target machine description.
67 /// Target Asm Printer information.
71 /// OutContext - This is the context for the output file that we are
72 /// streaming. This owns all of the global MC-related objects for the
73 /// generated translation unit.
74 MCContext &OutContext;
76 /// OutStreamer - This is the MCStreamer object for the file we are
77 /// generating. This contains the transient state for the current
78 /// translation unit that we are generating (such as the current section
80 MCStreamer &OutStreamer;
82 /// The current machine function.
83 const MachineFunction *MF;
85 /// MMI - This is a pointer to the current MachineModuleInfo.
86 MachineModuleInfo *MMI;
88 /// Name-mangler for global names.
92 /// The symbol for the current function. This is recalculated at the
93 /// beginning of each call to runOnMachineFunction().
95 MCSymbol *CurrentFnSym;
98 // GCMetadataPrinters - The garbage collection metadata printer table.
99 void *GCMetadataPrinters; // Really a DenseMap.
101 /// VerboseAsm - Emit comments in assembly output if this is true.
106 /// If VerboseAsm is set, a pointer to the loop info for this
110 /// DD - If the target supports dwarf debug info, this pointer is non-null.
113 /// DE - If the target supports dwarf exception info, this pointer is
118 explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer);
121 virtual ~AsmPrinter();
123 /// isVerbose - Return true if assembly output should contain comments.
125 bool isVerbose() const { return VerboseAsm; }
127 /// getFunctionNumber - Return a unique ID for the current function.
129 unsigned getFunctionNumber() const;
131 /// getObjFileLowering - Return information about object file lowering.
132 const TargetLoweringObjectFile &getObjFileLowering() const;
134 /// getTargetData - Return information about data layout.
135 const TargetData &getTargetData() const;
137 /// getCurrentSection() - Return the current section we are emitting to.
138 const MCSection *getCurrentSection() const;
141 //===------------------------------------------------------------------===//
142 // MachineFunctionPass Implementation.
143 //===------------------------------------------------------------------===//
145 /// getAnalysisUsage - Record analysis usage.
147 void getAnalysisUsage(AnalysisUsage &AU) const;
149 /// doInitialization - Set up the AsmPrinter when we are working on a new
150 /// module. If your pass overrides this, it must make sure to explicitly
151 /// call this implementation.
152 bool doInitialization(Module &M);
154 /// doFinalization - Shut down the asmprinter. If you override this in your
155 /// pass, you must make sure to call it explicitly.
156 bool doFinalization(Module &M);
158 /// runOnMachineFunction - Emit the specified function out to the
160 virtual bool runOnMachineFunction(MachineFunction &MF) {
161 SetupMachineFunction(MF);
162 EmitFunctionHeader();
167 //===------------------------------------------------------------------===//
168 // Coarse grained IR lowering routines.
169 //===------------------------------------------------------------------===//
171 /// SetupMachineFunction - This should be called when a new MachineFunction
172 /// is being processed from runOnMachineFunction.
173 void SetupMachineFunction(MachineFunction &MF);
175 /// EmitFunctionHeader - This method emits the header for the current
177 void EmitFunctionHeader();
179 /// EmitFunctionBody - This method emits the body and trailer for a
181 void EmitFunctionBody();
183 /// EmitConstantPool - Print to the current output stream assembly
184 /// representations of the constants in the constant pool MCP. This is
185 /// used to print out constants which have been "spilled to memory" by
186 /// the code generator.
188 virtual void EmitConstantPool();
190 /// EmitJumpTableInfo - Print assembly representations of the jump tables
191 /// used by the current function to the current output stream.
193 void EmitJumpTableInfo();
195 /// EmitGlobalVariable - Emit the specified global variable to the .s file.
196 virtual void EmitGlobalVariable(const GlobalVariable *GV);
198 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
199 /// special global used by LLVM. If so, emit it and return true, otherwise
200 /// do nothing and return false.
201 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
203 /// EmitAlignment - Emit an alignment directive to the specified power of
204 /// two boundary. For example, if you pass in 3 here, you will get an 8
205 /// byte alignment. If a global value is specified, and if that global has
206 /// an explicit alignment requested, it will unconditionally override the
207 /// alignment request.
209 /// The algorithm is:
211 /// if (GV && GV->hasalignment) Align = GV->getAlignment();
213 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
215 /// EmitBasicBlockStart - This method prints the label for the specified
216 /// MachineBasicBlock, an alignment (if present) and a comment describing
217 /// it if appropriate.
218 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
221 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
222 void EmitGlobalConstant(const Constant *CV, unsigned AddrSpace = 0);
225 //===------------------------------------------------------------------===//
227 //===------------------------------------------------------------------===//
229 // Targets can, or in the case of EmitInstruction, must implement these to
232 /// EmitStartOfAsmFile - This virtual method can be overridden by targets
233 /// that want to emit something at the start of their file.
234 virtual void EmitStartOfAsmFile(Module &) {}
236 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
237 /// want to emit something at the end of their file.
238 virtual void EmitEndOfAsmFile(Module &) {}
240 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
241 /// the first basic block in the function.
242 virtual void EmitFunctionBodyStart() {}
244 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
245 /// the last basic block in the function.
246 virtual void EmitFunctionBodyEnd() {}
248 /// EmitInstruction - Targets should implement this to emit instructions.
249 virtual void EmitInstruction(const MachineInstr *) {
250 assert(0 && "EmitInstruction not implemented");
253 virtual void EmitFunctionEntryLabel();
255 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
257 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
258 /// exactly one predecessor and the control transfer mechanism between
259 /// the predecessor and this block is a fall-through.
261 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
263 //===------------------------------------------------------------------===//
264 // Symbol Lowering Routines.
265 //===------------------------------------------------------------------===//
268 /// GetTempSymbol - Return the MCSymbol corresponding to the assembler
269 /// temporary label with the specified stem and unique ID.
270 MCSymbol *GetTempSymbol(StringRef Name, unsigned ID) const;
272 /// GetTempSymbol - Return an assembler temporary label with the specified
274 MCSymbol *GetTempSymbol(StringRef Name) const;
277 /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
278 /// global value name as its base, with the specified suffix, and where the
279 /// symbol is forced to have private linkage if ForcePrivate is true.
280 MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
282 bool ForcePrivate = true) const;
284 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
286 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
288 /// GetCPISymbol - Return the symbol for the specified constant pool entry.
289 MCSymbol *GetCPISymbol(unsigned CPID) const;
291 /// GetJTISymbol - Return the symbol for the specified jump table entry.
292 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
294 /// GetJTSetSymbol - Return the symbol for the specified jump table .set
295 /// FIXME: privatize to AsmPrinter.
296 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
298 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
299 /// uses of the specified basic block.
300 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
301 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
303 //===------------------------------------------------------------------===//
304 // Emission Helper Routines.
305 //===------------------------------------------------------------------===//
307 /// printOffset - This is just convenient handler for printing offsets.
308 void printOffset(int64_t Offset, raw_ostream &OS) const;
310 /// EmitInt8 - Emit a byte directive and value.
312 void EmitInt8(int Value) const;
314 /// EmitInt16 - Emit a short directive and value.
316 void EmitInt16(int Value) const;
318 /// EmitInt32 - Emit a long directive and value.
320 void EmitInt32(int Value) const;
322 /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
323 /// in bytes of the directive is specified by Size and Hi/Lo specify the
324 /// labels. This implicitly uses .set if it is available.
325 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
326 unsigned Size) const;
328 /// EmitLabelOffsetDifference - Emit something like ".long Hi+Offset-Lo"
329 /// where the size in bytes of the directive is specified by Size and Hi/Lo
330 /// specify the labels. This implicitly uses .set if it is available.
331 void EmitLabelOffsetDifference(const MCSymbol *Hi, uint64_t Offset,
332 const MCSymbol *Lo, unsigned Size) const;
334 //===------------------------------------------------------------------===//
335 // Dwarf Emission Helper Routines
336 //===------------------------------------------------------------------===//
338 /// EmitSLEB128 - emit the specified signed leb128 value.
339 void EmitSLEB128(int Value, const char *Desc = 0) const;
341 /// EmitULEB128 - emit the specified unsigned leb128 value.
342 void EmitULEB128(unsigned Value, const char *Desc = 0,
343 unsigned PadTo = 0) const;
345 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
346 void EmitCFAByte(unsigned Val) const;
348 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
349 /// encoding. If verbose assembly output is enabled, we output comments
350 /// describing the encoding. Desc is a string saying what the encoding is
351 /// specifying (e.g. "LSDA").
352 void EmitEncodingByte(unsigned Val, const char *Desc = 0) const;
354 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
355 unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
357 /// EmitReference - Emit a reference to a label with a specified encoding.
359 void EmitReference(const MCSymbol *Sym, unsigned Encoding) const;
360 void EmitReference(const GlobalValue *GV, unsigned Encoding) const;
362 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of
363 /// its section. This can be done with a special directive if the target
364 /// supports it (e.g. cygwin) or by emitting it as an offset from a label at
365 /// the start of the section.
367 /// SectionLabel is a temporary label emitted at the start of the section
368 /// that Label lives in.
369 void EmitSectionOffset(const MCSymbol *Label,
370 const MCSymbol *SectionLabel) const;
372 //===------------------------------------------------------------------===//
373 // Dwarf Lowering Routines
374 //===------------------------------------------------------------------===//
376 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
378 void EmitFrameMoves(const std::vector<MachineMove> &Moves,
379 MCSymbol *BaseLabel, bool isEH) const;
382 //===------------------------------------------------------------------===//
383 // Inline Asm Support
384 //===------------------------------------------------------------------===//
386 // These are hooks that targets can override to implement inline asm
387 // support. These should probably be moved out of AsmPrinter someday.
389 /// PrintSpecial - Print information related to the specified machine instr
390 /// that is independent of the operand, and may be independent of the instr
391 /// itself. This can be useful for portably encoding the comment character
392 /// or other bits of target-specific knowledge into the asmstrings. The
393 /// syntax used is ${:comment}. Targets can override this to add support
394 /// for their own strange codes.
395 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
396 const char *Code) const;
398 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
399 /// instruction, using the specified assembler variant. Targets should
400 /// override this to format as appropriate. This method can return true if
401 /// the operand is erroneous.
402 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
403 unsigned AsmVariant, const char *ExtraCode,
406 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
407 /// instruction, using the specified assembler variant as an address.
408 /// Targets should override this to format as appropriate. This method can
409 /// return true if the operand is erroneous.
410 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
412 const char *ExtraCode,
416 /// Private state for PrintSpecial()
417 // Assign a unique ID to this machine instruction.
418 mutable const MachineInstr *LastMI;
419 mutable unsigned LastFn;
420 mutable unsigned Counter;
421 mutable unsigned SetCounter;
423 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
424 void EmitInlineAsm(StringRef Str, unsigned LocCookie) const;
426 /// EmitInlineAsm - This method formats and emits the specified machine
427 /// instruction that is an inline asm.
428 void EmitInlineAsm(const MachineInstr *MI) const;
430 //===------------------------------------------------------------------===//
431 // Internal Implementation Details
432 //===------------------------------------------------------------------===//
434 /// EmitVisibility - This emits visibility information about symbol, if
435 /// this is suported by the target.
436 void EmitVisibility(MCSymbol *Sym, unsigned Visibility) const;
438 void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const;
440 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
441 const MachineBasicBlock *MBB,
443 void EmitLLVMUsedList(Constant *List);
444 void EmitXXStructorList(Constant *List);
445 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);