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"
33 class GCMetadataPrinter;
36 class MachineBasicBlock;
37 class MachineFunction;
39 class MachineLoopInfo;
41 class MachineConstantPool;
42 class MachineConstantPoolEntry;
43 class MachineConstantPoolValue;
44 class MachineJumpTableInfo;
45 class MachineModuleInfo;
54 class TargetLoweringObjectFile;
56 class formatted_raw_ostream;
58 /// AsmPrinter - This class is intended to be used as a driving class for all
60 class AsmPrinter : public MachineFunctionPass {
63 /// FunctionNumber - This provides a unique ID for each function emitted in
64 /// this translation unit. It is autoincremented by SetupMachineFunction,
65 /// and can be accessed with getFunctionNumber() and
66 /// IncrementFunctionNumber().
68 unsigned FunctionNumber;
70 // GCMetadataPrinters - The garbage collection metadata printer table.
71 typedef DenseMap<GCStrategy*,GCMetadataPrinter*> gcp_map_type;
72 typedef gcp_map_type::iterator gcp_iterator;
73 gcp_map_type GCMetadataPrinters;
75 /// If VerboseAsm is set, a pointer to the loop info for this
81 /// MMI - If available, this is a pointer to the current MachineModuleInfo.
82 MachineModuleInfo *MMI;
85 /// DW - If available, this is a pointer to the current dwarf writer.
89 /// Output stream on which we're printing assembly code.
91 formatted_raw_ostream &O;
93 /// Target machine description.
97 /// getObjFileLowering - Return information about object file lowering.
98 TargetLoweringObjectFile &getObjFileLowering() const;
100 /// Target Asm Printer information.
102 const MCAsmInfo *MAI;
104 /// Target Register Information.
106 const TargetRegisterInfo *TRI;
108 /// OutContext - This is the context for the output file that we are
109 /// streaming. This owns all of the global MC-related objects for the
110 /// generated translation unit.
111 MCContext &OutContext;
113 /// OutStreamer - This is the MCStreamer object for the file we are
114 /// generating. This contains the transient state for the current
115 /// translation unit that we are generating (such as the current section
117 MCStreamer &OutStreamer;
119 /// The current machine function.
120 const MachineFunction *MF;
122 /// Name-mangler for global names.
126 /// Cache of mangled name for current function. This is recalculated at the
127 /// beginning of each call to runOnMachineFunction().
129 std::string CurrentFnName;
131 /// getCurrentSection() - Return the current section we are emitting to.
132 const MCSection *getCurrentSection() const;
135 /// VerboseAsm - Emit comments in assembly output if this is true.
139 /// Private state for PrintSpecial()
140 // Assign a unique ID to this machine instruction.
141 mutable const MachineInstr *LastMI;
142 mutable const Function *LastFn;
143 mutable unsigned Counter;
145 // Private state for processDebugLoc()
146 mutable DebugLocTuple PrevDLT;
149 explicit AsmPrinter(formatted_raw_ostream &o, TargetMachine &TM,
150 const MCAsmInfo *T, bool V);
153 virtual ~AsmPrinter();
155 /// isVerbose - Return true if assembly output should contain comments.
157 bool isVerbose() const { return VerboseAsm; }
159 /// getFunctionNumber - Return a unique ID for the current function.
161 unsigned getFunctionNumber() const { return FunctionNumber; }
164 /// getAnalysisUsage - Record analysis usage.
166 void getAnalysisUsage(AnalysisUsage &AU) const;
168 /// doInitialization - Set up the AsmPrinter when we are working on a new
169 /// module. If your pass overrides this, it must make sure to explicitly
170 /// call this implementation.
171 bool doInitialization(Module &M);
173 /// EmitStartOfAsmFile - This virtual method can be overridden by targets
174 /// that want to emit something at the start of their file.
175 virtual void EmitStartOfAsmFile(Module &M) {}
177 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
178 /// want to emit something at the end of their file.
179 virtual void EmitEndOfAsmFile(Module &M) {}
181 /// doFinalization - Shut down the asmprinter. If you override this in your
182 /// pass, you must make sure to call it explicitly.
183 bool doFinalization(Module &M);
185 /// PrintSpecial - Print information related to the specified machine instr
186 /// that is independent of the operand, and may be independent of the instr
187 /// itself. This can be useful for portably encoding the comment character
188 /// or other bits of target-specific knowledge into the asmstrings. The
189 /// syntax used is ${:comment}. Targets can override this to add support
190 /// for their own strange codes.
191 virtual void PrintSpecial(const MachineInstr *MI, const char *Code) const;
193 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
194 /// instruction, using the specified assembler variant. Targets should
195 /// override this to format as appropriate. This method can return true if
196 /// the operand is erroneous.
197 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
198 unsigned AsmVariant, const char *ExtraCode);
200 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
201 /// instruction, using the specified assembler variant as an address.
202 /// Targets should override this to format as appropriate. This method can
203 /// return true if the operand is erroneous.
204 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
206 const char *ExtraCode);
208 /// PrintGlobalVariable - Emit the specified global variable and its
209 /// initializer to the output stream.
210 virtual void PrintGlobalVariable(const GlobalVariable *GV) = 0;
212 /// SetupMachineFunction - This should be called when a new MachineFunction
213 /// is being processed from runOnMachineFunction.
214 void SetupMachineFunction(MachineFunction &MF);
216 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
217 /// not normally call this, as the counter is automatically bumped by
218 /// SetupMachineFunction.
219 void IncrementFunctionNumber() { FunctionNumber++; }
221 /// EmitConstantPool - Print to the current output stream assembly
222 /// representations of the constants in the constant pool MCP. This is
223 /// used to print out constants which have been "spilled to memory" by
224 /// the code generator.
226 void EmitConstantPool(MachineConstantPool *MCP);
228 /// EmitJumpTableInfo - Print assembly representations of the jump tables
229 /// used by the current function to the current output stream.
231 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
233 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
234 /// special global used by LLVM. If so, emit it and return true, otherwise
235 /// do nothing and return false.
236 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
239 //===------------------------------------------------------------------===//
240 /// LEB 128 number encoding.
242 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
243 /// representing an unsigned leb128 value.
244 void PrintULEB128(unsigned Value) const;
246 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
247 /// representing a signed leb128 value.
248 void PrintSLEB128(int Value) const;
250 //===------------------------------------------------------------------===//
251 // Emission and print routines
254 /// PrintHex - Print a value as a hexidecimal value.
256 void PrintHex(int Value) const;
258 /// EOL - Print a newline character to asm stream. If a comment is present
259 /// then it will be printed first. Comments should not contain '\n'.
261 void EOL(const std::string &Comment) const;
262 void EOL(const char* Comment) const;
263 void EOL(const char *Comment, unsigned Encoding) const;
265 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
266 /// unsigned leb128 value.
267 void EmitULEB128Bytes(unsigned Value) const;
269 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
270 /// signed leb128 value.
271 void EmitSLEB128Bytes(int Value) const;
273 /// EmitInt8 - Emit a byte directive and value.
275 void EmitInt8(int Value) const;
277 /// EmitInt16 - Emit a short directive and value.
279 void EmitInt16(int Value) const;
281 /// EmitInt32 - Emit a long directive and value.
283 void EmitInt32(int Value) const;
285 /// EmitInt64 - Emit a long long directive and value.
287 void EmitInt64(uint64_t Value) const;
289 /// EmitString - Emit a string with quotes and a null terminator.
290 /// Special characters are emitted properly.
291 /// @verbatim (Eg. '\t') @endverbatim
292 void EmitString(const std::string &String) const;
293 void EmitString(const char *String, unsigned Size) const;
295 /// EmitFile - Emit a .file directive.
296 void EmitFile(unsigned Number, const std::string &Name) const;
298 //===------------------------------------------------------------------===//
300 /// EmitAlignment - Emit an alignment directive to the specified power of
301 /// two boundary. For example, if you pass in 3 here, you will get an 8
302 /// byte alignment. If a global value is specified, and if that global has
303 /// an explicit alignment requested, it will unconditionally override the
304 /// alignment request. However, if ForcedAlignBits is specified, this value
305 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
306 /// and the alignment computed with NumBits and the global. If UseFillExpr
307 /// is true, it also emits an optional second value FillValue which the
308 /// assembler uses to fill gaps to match alignment for text sections if the
309 /// has specified a non-zero fill value.
311 /// The algorithm is:
313 /// if (GV && GV->hasalignment) Align = GV->getalignment();
314 /// Align = std::max(Align, ForcedAlignBits);
316 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
317 unsigned ForcedAlignBits = 0,
318 bool UseFillExpr = true) const;
320 /// printLabel - This method prints a local label used by debug and
321 /// exception handling tables.
322 void printLabel(const MachineInstr *MI) const;
323 void printLabel(unsigned Id) const;
325 /// printDeclare - This method prints a local variable declaration used by
327 void printDeclare(const MachineInstr *MI) const;
329 /// EmitComments - Pretty-print comments for instructions
330 void EmitComments(const MachineInstr &MI) const;
331 /// EmitComments - Pretty-print comments for basic blocks
332 void EmitComments(const MachineBasicBlock &MBB) const;
334 /// GetMBBSymbol - Return the MCSymbol corresponding to the specified basic
336 MCSymbol *GetMBBSymbol(unsigned MBBID) const;
338 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
339 /// uses of the specified basic block.
340 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
341 MCSymbol *GetBlockAddressSymbol(const Function *F,
342 const BasicBlock *BB) const;
344 /// EmitBasicBlockStart - This method prints the label for the specified
345 /// MachineBasicBlock, an alignment (if present) and a comment describing
346 /// it if appropriate.
347 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
349 /// EmitZeros - Emit a block of zeros.
351 void EmitZeros(uint64_t NumZeros, unsigned AddrSpace = 0) const;
353 /// EmitString - Emit a zero-byte-terminated string constant.
355 virtual void EmitString(const ConstantArray *CVA) const;
357 /// EmitConstantValueOnly - Print out the specified constant, without a
358 /// storage class. Only constants of first-class type are allowed here.
359 void EmitConstantValueOnly(const Constant *CV);
361 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
362 void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
364 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
366 /// processDebugLoc - Processes the debug information of each machine
367 /// instruction's DebugLoc.
368 void processDebugLoc(const MachineInstr *MI, bool BeforePrintingInsn);
370 /// printInlineAsm - This method formats and prints the specified machine
371 /// instruction that is an inline asm.
372 void printInlineAsm(const MachineInstr *MI) const;
374 /// printImplicitDef - This method prints the specified machine instruction
375 /// that is an implicit def.
376 virtual void printImplicitDef(const MachineInstr *MI) const;
379 /// printPICJumpTableSetLabel - This method prints a set label for the
380 /// specified MachineBasicBlock for a jumptable entry.
381 virtual void printPICJumpTableSetLabel(unsigned uid,
382 const MachineBasicBlock *MBB) const;
383 virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
384 const MachineBasicBlock *MBB) const;
385 virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
386 const MachineBasicBlock *MBB,
389 /// printDataDirective - This method prints the asm directive for the
391 void printDataDirective(const Type *type, unsigned AddrSpace = 0);
393 /// printVisibility - This prints visibility information about symbol, if
394 /// this is suported by the target.
395 void printVisibility(const std::string& Name, unsigned Visibility) const;
397 /// printOffset - This is just convenient handler for printing offsets.
398 void printOffset(int64_t Offset) const;
401 void EmitLLVMUsedList(Constant *List);
402 void EmitXXStructorList(Constant *List);
403 void EmitGlobalConstantStruct(const ConstantStruct* CVS,
405 void EmitGlobalConstantArray(const ConstantArray* CVA, unsigned AddrSpace);
406 void EmitGlobalConstantVector(const ConstantVector* CP);
407 void EmitGlobalConstantFP(const ConstantFP* CFP, unsigned AddrSpace);
408 void EmitGlobalConstantLargeInt(const ConstantInt* CI, unsigned AddrSpace);
409 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);