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"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/ADT/DenseMap.h"
32 class GCMetadataPrinter;
35 class MachineBasicBlock;
36 class MachineFunction;
38 class MachineLoopInfo;
40 class MachineConstantPool;
41 class MachineConstantPoolEntry;
42 class MachineConstantPoolValue;
43 class MachineJumpTableInfo;
44 class MachineModuleInfo;
53 class TargetLoweringObjectFile;
55 class formatted_raw_ostream;
57 /// AsmPrinter - This class is intended to be used as a driving class for all
59 class AsmPrinter : public MachineFunctionPass {
62 /// FunctionNumber - This provides a unique ID for each function emitted in
63 /// this translation unit. It is autoincremented by SetupMachineFunction,
64 /// and can be accessed with getFunctionNumber() and
65 /// IncrementFunctionNumber().
67 unsigned FunctionNumber;
69 // GCMetadataPrinters - The garbage collection metadata printer table.
70 typedef DenseMap<GCStrategy*,GCMetadataPrinter*> gcp_map_type;
71 typedef gcp_map_type::iterator gcp_iterator;
72 gcp_map_type GCMetadataPrinters;
74 /// If VerboseAsm is set, a pointer to the loop info for this
80 /// MMI - If available, this is a pointer to the current MachineModuleInfo.
81 MachineModuleInfo *MMI;
84 /// DW - If available, this is a pointer to the current dwarf writer.
88 /// Output stream on which we're printing assembly code.
90 formatted_raw_ostream &O;
92 /// Target machine description.
96 /// getObjFileLowering - Return information about object file lowering.
97 TargetLoweringObjectFile &getObjFileLowering() const;
99 /// Target Asm Printer information.
101 const MCAsmInfo *MAI;
103 /// Target Register Information.
105 const TargetRegisterInfo *TRI;
107 /// OutContext - This is the context for the output file that we are
108 /// streaming. This owns all of the global MC-related objects for the
109 /// generated translation unit.
110 MCContext &OutContext;
112 /// OutStreamer - This is the MCStreamer object for the file we are
113 /// generating. This contains the transient state for the current
114 /// translation unit that we are generating (such as the current section
116 MCStreamer &OutStreamer;
118 /// The current machine function.
119 const MachineFunction *MF;
121 /// Name-mangler for global names.
125 /// Cache of mangled name for current function. This is recalculated at the
126 /// beginning of each call to runOnMachineFunction().
128 std::string CurrentFnName;
130 /// getCurrentSection() - Return the current section we are emitting to.
131 const MCSection *getCurrentSection() const;
134 /// VerboseAsm - Emit comments in assembly output if this is true.
138 /// Private state for PrintSpecial()
139 // Assign a unique ID to this machine instruction.
140 mutable const MachineInstr *LastMI;
141 mutable const Function *LastFn;
142 mutable unsigned Counter;
144 // Private state for processDebugLock()
145 mutable DebugLocTuple PrevDLT;
148 explicit AsmPrinter(formatted_raw_ostream &o, TargetMachine &TM,
149 const MCAsmInfo *T, bool V);
152 virtual ~AsmPrinter();
154 /// isVerbose - Return true if assembly output should contain comments.
156 bool isVerbose() const { return VerboseAsm; }
158 /// getFunctionNumber - Return a unique ID for the current function.
160 unsigned getFunctionNumber() const { return FunctionNumber; }
163 /// getAnalysisUsage - Record analysis usage.
165 void getAnalysisUsage(AnalysisUsage &AU) const;
167 /// doInitialization - Set up the AsmPrinter when we are working on a new
168 /// module. If your pass overrides this, it must make sure to explicitly
169 /// call this implementation.
170 bool doInitialization(Module &M);
172 /// doFinalization - Shut down the asmprinter. If you override this in your
173 /// pass, you must make sure to call it explicitly.
174 bool doFinalization(Module &M);
176 /// PrintSpecial - Print information related to the specified machine instr
177 /// that is independent of the operand, and may be independent of the instr
178 /// itself. This can be useful for portably encoding the comment character
179 /// or other bits of target-specific knowledge into the asmstrings. The
180 /// syntax used is ${:comment}. Targets can override this to add support
181 /// for their own strange codes.
182 virtual void PrintSpecial(const MachineInstr *MI, const char *Code) const;
184 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
185 /// instruction, using the specified assembler variant. Targets should
186 /// override this to format as appropriate. This method can return true if
187 /// the operand is erroneous.
188 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
189 unsigned AsmVariant, const char *ExtraCode);
191 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
192 /// instruction, using the specified assembler variant as an address.
193 /// Targets should override this to format as appropriate. This method can
194 /// return true if the operand is erroneous.
195 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
197 const char *ExtraCode);
199 /// PrintGlobalVariable - Emit the specified global variable and its
200 /// initializer to the output stream.
201 virtual void PrintGlobalVariable(const GlobalVariable *GV) = 0;
203 /// SetupMachineFunction - This should be called when a new MachineFunction
204 /// is being processed from runOnMachineFunction.
205 void SetupMachineFunction(MachineFunction &MF);
207 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
208 /// not normally call this, as the counter is automatically bumped by
209 /// SetupMachineFunction.
210 void IncrementFunctionNumber() { FunctionNumber++; }
212 /// EmitConstantPool - Print to the current output stream assembly
213 /// representations of the constants in the constant pool MCP. This is
214 /// used to print out constants which have been "spilled to memory" by
215 /// the code generator.
217 void EmitConstantPool(MachineConstantPool *MCP);
219 /// EmitJumpTableInfo - Print assembly representations of the jump tables
220 /// used by the current function to the current output stream.
222 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
224 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
225 /// special global used by LLVM. If so, emit it and return true, otherwise
226 /// do nothing and return false.
227 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
230 //===------------------------------------------------------------------===//
231 /// LEB 128 number encoding.
233 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
234 /// representing an unsigned leb128 value.
235 void PrintULEB128(unsigned Value) const;
237 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
238 /// representing a signed leb128 value.
239 void PrintSLEB128(int Value) const;
241 //===------------------------------------------------------------------===//
242 // Emission and print routines
245 /// PrintHex - Print a value as a hexidecimal value.
247 void PrintHex(int Value) const;
249 /// EOL - Print a newline character to asm stream. If a comment is present
250 /// then it will be printed first. Comments should not contain '\n'.
252 void EOL(const std::string &Comment) const;
253 void EOL(const char* Comment) const;
254 void EOL(const char *Comment, unsigned Encoding) const;
256 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
257 /// unsigned leb128 value.
258 void EmitULEB128Bytes(unsigned Value) const;
260 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
261 /// signed leb128 value.
262 void EmitSLEB128Bytes(int Value) const;
264 /// EmitInt8 - Emit a byte directive and value.
266 void EmitInt8(int Value) const;
268 /// EmitInt16 - Emit a short directive and value.
270 void EmitInt16(int Value) const;
272 /// EmitInt32 - Emit a long directive and value.
274 void EmitInt32(int Value) const;
276 /// EmitInt64 - Emit a long long directive and value.
278 void EmitInt64(uint64_t Value) const;
280 /// EmitString - Emit a string with quotes and a null terminator.
281 /// Special characters are emitted properly.
282 /// @verbatim (Eg. '\t') @endverbatim
283 void EmitString(const std::string &String) const;
284 void EmitString(const char *String, unsigned Size) const;
286 /// EmitFile - Emit a .file directive.
287 void EmitFile(unsigned Number, const std::string &Name) const;
289 //===------------------------------------------------------------------===//
291 /// EmitAlignment - Emit an alignment directive to the specified power of
292 /// two boundary. For example, if you pass in 3 here, you will get an 8
293 /// byte alignment. If a global value is specified, and if that global has
294 /// an explicit alignment requested, it will unconditionally override the
295 /// alignment request. However, if ForcedAlignBits is specified, this value
296 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
297 /// and the alignment computed with NumBits and the global. If UseFillExpr
298 /// is true, it also emits an optional second value FillValue which the
299 /// assembler uses to fill gaps to match alignment for text sections if the
300 /// has specified a non-zero fill value.
302 /// The algorithm is:
304 /// if (GV && GV->hasalignment) Align = GV->getalignment();
305 /// Align = std::max(Align, ForcedAlignBits);
307 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
308 unsigned ForcedAlignBits = 0,
309 bool UseFillExpr = true) const;
311 /// printLabel - This method prints a local label used by debug and
312 /// exception handling tables.
313 void printLabel(const MachineInstr *MI) const;
314 void printLabel(unsigned Id) const;
316 /// printDeclare - This method prints a local variable declaration used by
318 void printDeclare(const MachineInstr *MI) const;
320 /// EmitComments - Pretty-print comments for instructions
321 void EmitComments(const MachineInstr &MI) const;
322 /// EmitComments - Pretty-print comments for basic blocks
323 void EmitComments(const MachineBasicBlock &MBB) const;
325 /// GetMBBSymbol - Return the MCSymbol corresponding to the specified basic
327 MCSymbol *GetMBBSymbol(unsigned MBBID) const;
329 /// EmitBasicBlockStart - This method prints the label for the specified
330 /// MachineBasicBlock, an alignment (if present) and a comment describing
331 /// it if appropriate.
332 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
334 /// EmitZeros - Emit a block of zeros.
336 void EmitZeros(uint64_t NumZeros, unsigned AddrSpace = 0) const;
338 /// EmitString - Emit a zero-byte-terminated string constant.
340 virtual void EmitString(const ConstantArray *CVA) const;
342 /// EmitConstantValueOnly - Print out the specified constant, without a
343 /// storage class. Only constants of first-class type are allowed here.
344 void EmitConstantValueOnly(const Constant *CV);
346 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
347 void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
349 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
351 /// processDebugLoc - Processes the debug information of each machine
352 /// instruction's DebugLoc.
353 void processDebugLoc(DebugLoc DL);
355 /// printInlineAsm - This method formats and prints the specified machine
356 /// instruction that is an inline asm.
357 void printInlineAsm(const MachineInstr *MI) const;
359 /// printImplicitDef - This method prints the specified machine instruction
360 /// that is an implicit def.
361 virtual void printImplicitDef(const MachineInstr *MI) const;
364 /// printPICJumpTableSetLabel - This method prints a set label for the
365 /// specified MachineBasicBlock for a jumptable entry.
366 virtual void printPICJumpTableSetLabel(unsigned uid,
367 const MachineBasicBlock *MBB) const;
368 virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
369 const MachineBasicBlock *MBB) const;
370 virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
371 const MachineBasicBlock *MBB,
374 /// printDataDirective - This method prints the asm directive for the
376 void printDataDirective(const Type *type, unsigned AddrSpace = 0);
378 /// printVisibility - This prints visibility information about symbol, if
379 /// this is suported by the target.
380 void printVisibility(const std::string& Name, unsigned Visibility) const;
382 /// printOffset - This is just convenient handler for printing offsets.
383 void printOffset(int64_t Offset) const;
386 void EmitLLVMUsedList(Constant *List);
387 void EmitXXStructorList(Constant *List);
388 void EmitGlobalConstantStruct(const ConstantStruct* CVS,
390 void EmitGlobalConstantArray(const ConstantArray* CVA, unsigned AddrSpace);
391 void EmitGlobalConstantVector(const ConstantVector* CP);
392 void EmitGlobalConstantFP(const ConstantFP* CFP, unsigned AddrSpace);
393 void EmitGlobalConstantLargeInt(const ConstantInt* CI, unsigned AddrSpace);
394 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);