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;
53 class TargetLoweringObjectFile;
58 /// AsmPrinter - This class is intended to be used as a driving class for all
60 class AsmPrinter : public MachineFunctionPass {
62 /// DW - If available, this is a pointer to the current dwarf writer.
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
114 explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer);
117 virtual ~AsmPrinter();
119 /// isVerbose - Return true if assembly output should contain comments.
121 bool isVerbose() const { return VerboseAsm; }
123 /// getFunctionNumber - Return a unique ID for the current function.
125 unsigned getFunctionNumber() const;
127 /// getObjFileLowering - Return information about object file lowering.
128 TargetLoweringObjectFile &getObjFileLowering() const;
130 /// getTargetData - Return information about data layout.
131 const TargetData &getTargetData() const;
133 /// getCurrentSection() - Return the current section we are emitting to.
134 const MCSection *getCurrentSection() const;
137 //===------------------------------------------------------------------===//
138 // MachineFunctionPass Implementation.
139 //===------------------------------------------------------------------===//
141 /// getAnalysisUsage - Record analysis usage.
143 void getAnalysisUsage(AnalysisUsage &AU) const;
145 /// doInitialization - Set up the AsmPrinter when we are working on a new
146 /// module. If your pass overrides this, it must make sure to explicitly
147 /// call this implementation.
148 bool doInitialization(Module &M);
150 /// doFinalization - Shut down the asmprinter. If you override this in your
151 /// pass, you must make sure to call it explicitly.
152 bool doFinalization(Module &M);
154 /// runOnMachineFunction - Emit the specified function out to the
156 virtual bool runOnMachineFunction(MachineFunction &MF) {
157 SetupMachineFunction(MF);
158 EmitFunctionHeader();
163 //===------------------------------------------------------------------===//
164 // Coarse grained IR lowering routines.
165 //===------------------------------------------------------------------===//
167 /// SetupMachineFunction - This should be called when a new MachineFunction
168 /// is being processed from runOnMachineFunction.
169 void SetupMachineFunction(MachineFunction &MF);
171 /// EmitFunctionHeader - This method emits the header for the current
173 void EmitFunctionHeader();
175 /// EmitFunctionBody - This method emits the body and trailer for a
177 void EmitFunctionBody();
179 /// EmitConstantPool - Print to the current output stream assembly
180 /// representations of the constants in the constant pool MCP. This is
181 /// used to print out constants which have been "spilled to memory" by
182 /// the code generator.
184 virtual void EmitConstantPool();
186 /// EmitJumpTableInfo - Print assembly representations of the jump tables
187 /// used by the current function to the current output stream.
189 void EmitJumpTableInfo();
191 /// EmitGlobalVariable - Emit the specified global variable to the .s file.
192 virtual void EmitGlobalVariable(const GlobalVariable *GV);
194 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
195 /// special global used by LLVM. If so, emit it and return true, otherwise
196 /// do nothing and return false.
197 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
199 /// EmitAlignment - Emit an alignment directive to the specified power of
200 /// two boundary. For example, if you pass in 3 here, you will get an 8
201 /// byte alignment. If a global value is specified, and if that global has
202 /// an explicit alignment requested, it will unconditionally override the
203 /// alignment request. However, if ForcedAlignBits is specified, this value
204 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
205 /// and the alignment computed with NumBits and the global. If UseFillExpr
206 /// is true, it also emits an optional second value FillValue which the
207 /// assembler uses to fill gaps to match alignment for text sections if the
208 /// has specified a non-zero fill value.
210 /// The algorithm is:
212 /// if (GV && GV->hasalignment) Align = GV->getalignment();
213 /// Align = std::max(Align, ForcedAlignBits);
215 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
216 unsigned ForcedAlignBits = 0,
217 bool UseFillExpr = true) const;
219 /// EmitBasicBlockStart - This method prints the label for the specified
220 /// MachineBasicBlock, an alignment (if present) and a comment describing
221 /// it if appropriate.
222 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
225 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
226 void EmitGlobalConstant(const Constant *CV, unsigned AddrSpace = 0);
229 //===------------------------------------------------------------------===//
231 //===------------------------------------------------------------------===//
233 // Targets can, or in the case of EmitInstruction, must implement these to
236 /// EmitStartOfAsmFile - This virtual method can be overridden by targets
237 /// that want to emit something at the start of their file.
238 virtual void EmitStartOfAsmFile(Module &) {}
240 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
241 /// want to emit something at the end of their file.
242 virtual void EmitEndOfAsmFile(Module &) {}
244 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
245 /// the first basic block in the function.
246 virtual void EmitFunctionBodyStart() {}
248 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
249 /// the last basic block in the function.
250 virtual void EmitFunctionBodyEnd() {}
252 /// EmitInstruction - Targets should implement this to emit instructions.
253 virtual void EmitInstruction(const MachineInstr *) {
254 assert(0 && "EmitInstruction not implemented");
257 virtual void EmitFunctionEntryLabel();
259 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
261 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
262 /// exactly one predecessor and the control transfer mechanism between
263 /// the predecessor and this block is a fall-through.
265 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
267 //===------------------------------------------------------------------===//
268 // Symbol Lowering Routines.
269 //===------------------------------------------------------------------===//
272 /// GetTempSymbol - Return the MCSymbol corresponding to the assembler
273 /// temporary label with the specified stem and unique ID.
274 MCSymbol *GetTempSymbol(StringRef Name, unsigned ID) const;
276 /// GetTempSymbol - Return an assembler temporary label with the specified
278 MCSymbol *GetTempSymbol(StringRef Name) const;
281 /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
282 /// global value name as its base, with the specified suffix, and where the
283 /// symbol is forced to have private linkage if ForcePrivate is true.
284 MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
286 bool ForcePrivate = true) const;
288 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
290 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
292 /// GetCPISymbol - Return the symbol for the specified constant pool entry.
293 MCSymbol *GetCPISymbol(unsigned CPID) const;
295 /// GetJTISymbol - Return the symbol for the specified jump table entry.
296 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
298 /// GetJTSetSymbol - Return the symbol for the specified jump table .set
299 /// FIXME: privatize to AsmPrinter.
300 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
302 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
303 /// uses of the specified basic block.
304 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
305 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
307 //===------------------------------------------------------------------===//
308 // Emission Helper Routines.
309 //===------------------------------------------------------------------===//
311 /// printOffset - This is just convenient handler for printing offsets.
312 void printOffset(int64_t Offset, raw_ostream &OS) const;
314 /// EmitInt8 - Emit a byte directive and value.
316 void EmitInt8(int Value) const;
318 /// EmitInt16 - Emit a short directive and value.
320 void EmitInt16(int Value) const;
322 /// EmitInt32 - Emit a long directive and value.
324 void EmitInt32(int Value) const;
326 /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
327 /// in bytes of the directive is specified by Size and Hi/Lo specify the
328 /// labels. This implicitly uses .set if it is available.
329 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
330 unsigned Size) const;
332 //===------------------------------------------------------------------===//
333 // Dwarf Emission Helper Routines
334 //===------------------------------------------------------------------===//
336 /// EmitSLEB128 - emit the specified signed leb128 value.
337 void EmitSLEB128(int Value, const char *Desc = 0) const;
339 /// EmitULEB128 - emit the specified unsigned leb128 value.
340 void EmitULEB128(unsigned Value, const char *Desc = 0,
341 unsigned PadTo = 0) const;
343 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
344 void EmitCFAByte(unsigned Val) const;
346 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
347 /// encoding. If verbose assembly output is enabled, we output comments
348 /// describing the encoding. Desc is a string saying what the encoding is
349 /// specifying (e.g. "LSDA").
350 void EmitEncodingByte(unsigned Val, const char *Desc = 0) const;
352 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
353 unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
355 /// EmitReference - Emit a reference to a label with a specified encoding.
357 void EmitReference(const MCSymbol *Sym, unsigned Encoding) const;
358 void EmitReference(const GlobalValue *GV, unsigned Encoding) const;
360 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of
361 /// its section. This can be done with a special directive if the target
362 /// supports it (e.g. cygwin) or by emitting it as an offset from a label at
363 /// the start of the section.
365 /// SectionLabel is a temporary label emitted at the start of the section
366 /// that Label lives in.
367 void EmitSectionOffset(const MCSymbol *Label,
368 const MCSymbol *SectionLabel) const;
370 //===------------------------------------------------------------------===//
371 // Dwarf Lowering Routines
372 //===------------------------------------------------------------------===//
374 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
376 void EmitFrameMoves(const std::vector<MachineMove> &Moves,
377 MCSymbol *BaseLabel, bool isEH) const;
380 //===------------------------------------------------------------------===//
381 // Inline Asm Support
382 //===------------------------------------------------------------------===//
384 // These are hooks that targets can override to implement inline asm
385 // support. These should probably be moved out of AsmPrinter someday.
387 /// PrintSpecial - Print information related to the specified machine instr
388 /// that is independent of the operand, and may be independent of the instr
389 /// itself. This can be useful for portably encoding the comment character
390 /// or other bits of target-specific knowledge into the asmstrings. The
391 /// syntax used is ${:comment}. Targets can override this to add support
392 /// for their own strange codes.
393 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
394 const char *Code) const;
396 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
397 /// instruction, using the specified assembler variant. Targets should
398 /// override this to format as appropriate. This method can return true if
399 /// the operand is erroneous.
400 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
401 unsigned AsmVariant, const char *ExtraCode,
404 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
405 /// instruction, using the specified assembler variant as an address.
406 /// Targets should override this to format as appropriate. This method can
407 /// return true if the operand is erroneous.
408 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
410 const char *ExtraCode,
414 /// Private state for PrintSpecial()
415 // Assign a unique ID to this machine instruction.
416 mutable const MachineInstr *LastMI;
417 mutable unsigned LastFn;
418 mutable unsigned Counter;
419 mutable unsigned SetCounter;
421 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
422 void EmitInlineAsm(StringRef Str) const;
424 /// EmitInlineAsm - This method formats and emits the specified machine
425 /// instruction that is an inline asm.
426 void EmitInlineAsm(const MachineInstr *MI) const;
428 //===------------------------------------------------------------------===//
429 // Internal Implementation Details
430 //===------------------------------------------------------------------===//
432 /// EmitVisibility - This emits visibility information about symbol, if
433 /// this is suported by the target.
434 void EmitVisibility(MCSymbol *Sym, unsigned Visibility) const;
436 void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const;
438 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
439 const MachineBasicBlock *MBB,
441 void EmitLLVMUsedList(Constant *List);
442 void EmitXXStructorList(Constant *List);
443 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);