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;
83 /// DW - If available, this is a pointer to the current dwarf writer.
87 /// Output stream on which we're printing assembly code.
89 formatted_raw_ostream &O;
91 /// Target machine description.
95 /// getObjFileLowering - Return information about object file lowering.
96 TargetLoweringObjectFile &getObjFileLowering() const;
98 /// Target Asm Printer information.
100 const MCAsmInfo *MAI;
102 /// Target Register Information.
104 const TargetRegisterInfo *TRI;
106 /// OutContext - This is the context for the output file that we are
107 /// streaming. This owns all of the global MC-related objects for the
108 /// generated translation unit.
109 MCContext &OutContext;
111 /// OutStreamer - This is the MCStreamer object for the file we are
112 /// generating. This contains the transient state for the current
113 /// translation unit that we are generating (such as the current section
115 MCStreamer &OutStreamer;
117 /// The current machine function.
118 const MachineFunction *MF;
120 /// Name-mangler for global names.
124 /// Cache of mangled name for current function. This is recalculated at the
125 /// beginning of each call to runOnMachineFunction().
127 std::string CurrentFnName;
129 /// getCurrentSection() - Return the current section we are emitting to.
130 const MCSection *getCurrentSection() const;
133 /// VerboseAsm - Emit comments in assembly output if this is true.
137 /// Private state for PrintSpecial()
138 // Assign a unique ID to this machine instruction.
139 mutable const MachineInstr *LastMI;
140 mutable const Function *LastFn;
141 mutable unsigned Counter;
143 // Private state for processDebugLock()
144 mutable DebugLocTuple PrevDLT;
147 explicit AsmPrinter(formatted_raw_ostream &o, TargetMachine &TM,
148 const MCAsmInfo *T, bool V);
151 virtual ~AsmPrinter();
153 /// isVerbose - Return true if assembly output should contain comments.
155 bool isVerbose() const { return VerboseAsm; }
157 /// getCurrentFunctionEHName - Called to return the CurrentFnEHName.
159 std::string getCurrentFunctionEHName(const MachineFunction *MF) const;
161 /// getFunctionNumber - Return a unique ID for the current function.
163 unsigned getFunctionNumber() const { return FunctionNumber; }
166 /// getAnalysisUsage - Record analysis usage.
168 void getAnalysisUsage(AnalysisUsage &AU) const;
170 /// doInitialization - Set up the AsmPrinter when we are working on a new
171 /// module. If your pass overrides this, it must make sure to explicitly
172 /// call this implementation.
173 bool doInitialization(Module &M);
175 /// doFinalization - Shut down the asmprinter. If you override this in your
176 /// pass, you must make sure to call it explicitly.
177 bool doFinalization(Module &M);
179 /// PrintSpecial - Print information related to the specified machine instr
180 /// that is independent of the operand, and may be independent of the instr
181 /// itself. This can be useful for portably encoding the comment character
182 /// or other bits of target-specific knowledge into the asmstrings. The
183 /// syntax used is ${:comment}. Targets can override this to add support
184 /// for their own strange codes.
185 virtual void PrintSpecial(const MachineInstr *MI, const char *Code) const;
187 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
188 /// instruction, using the specified assembler variant. Targets should
189 /// override this to format as appropriate. This method can return true if
190 /// the operand is erroneous.
191 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
192 unsigned AsmVariant, const char *ExtraCode);
194 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
195 /// instruction, using the specified assembler variant as an address.
196 /// Targets should override this to format as appropriate. This method can
197 /// return true if the operand is erroneous.
198 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
200 const char *ExtraCode);
202 /// PrintGlobalVariable - Emit the specified global variable and its
203 /// initializer to the output stream.
204 virtual void PrintGlobalVariable(const GlobalVariable *GV) = 0;
206 /// SetupMachineFunction - This should be called when a new MachineFunction
207 /// is being processed from runOnMachineFunction.
208 void SetupMachineFunction(MachineFunction &MF);
210 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
211 /// not normally call this, as the counter is automatically bumped by
212 /// SetupMachineFunction.
213 void IncrementFunctionNumber() { FunctionNumber++; }
215 /// EmitConstantPool - Print to the current output stream assembly
216 /// representations of the constants in the constant pool MCP. This is
217 /// used to print out constants which have been "spilled to memory" by
218 /// the code generator.
220 void EmitConstantPool(MachineConstantPool *MCP);
222 /// EmitJumpTableInfo - Print assembly representations of the jump tables
223 /// used by the current function to the current output stream.
225 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
227 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
228 /// special global used by LLVM. If so, emit it and return true, otherwise
229 /// do nothing and return false.
230 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
233 //===------------------------------------------------------------------===//
234 /// LEB 128 number encoding.
236 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
237 /// representing an unsigned leb128 value.
238 void PrintULEB128(unsigned Value) const;
240 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
241 /// representing a signed leb128 value.
242 void PrintSLEB128(int Value) const;
244 //===------------------------------------------------------------------===//
245 // Emission and print routines
248 /// PrintHex - Print a value as a hexidecimal value.
250 void PrintHex(int Value) const;
252 /// EOL - Print a newline character to asm stream. If a comment is present
253 /// then it will be printed first. Comments should not contain '\n'.
255 void EOL(const std::string &Comment) const;
256 void EOL(const char* Comment) const;
257 void EOL(const char *Comment, unsigned Encoding) const;
259 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
260 /// unsigned leb128 value.
261 void EmitULEB128Bytes(unsigned Value) const;
263 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
264 /// signed leb128 value.
265 void EmitSLEB128Bytes(int Value) const;
267 /// EmitInt8 - Emit a byte directive and value.
269 void EmitInt8(int Value) const;
271 /// EmitInt16 - Emit a short directive and value.
273 void EmitInt16(int Value) const;
275 /// EmitInt32 - Emit a long directive and value.
277 void EmitInt32(int Value) const;
279 /// EmitInt64 - Emit a long long directive and value.
281 void EmitInt64(uint64_t Value) const;
283 /// EmitString - Emit a string with quotes and a null terminator.
284 /// Special characters are emitted properly.
285 /// @verbatim (Eg. '\t') @endverbatim
286 void EmitString(const std::string &String) const;
287 void EmitString(const char *String, unsigned Size) const;
289 /// EmitFile - Emit a .file directive.
290 void EmitFile(unsigned Number, const std::string &Name) const;
292 //===------------------------------------------------------------------===//
294 /// EmitAlignment - Emit an alignment directive to the specified power of
295 /// two boundary. For example, if you pass in 3 here, you will get an 8
296 /// byte alignment. If a global value is specified, and if that global has
297 /// an explicit alignment requested, it will unconditionally override the
298 /// alignment request. However, if ForcedAlignBits is specified, this value
299 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
300 /// and the alignment computed with NumBits and the global. If UseFillExpr
301 /// is true, it also emits an optional second value FillValue which the
302 /// assembler uses to fill gaps to match alignment for text sections if the
303 /// has specified a non-zero fill value.
305 /// The algorithm is:
307 /// if (GV && GV->hasalignment) Align = GV->getalignment();
308 /// Align = std::max(Align, ForcedAlignBits);
310 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
311 unsigned ForcedAlignBits = 0,
312 bool UseFillExpr = true) const;
314 /// printLabel - This method prints a local label used by debug and
315 /// exception handling tables.
316 void printLabel(const MachineInstr *MI) const;
317 void printLabel(unsigned Id) const;
319 /// printDeclare - This method prints a local variable declaration used by
321 void printDeclare(const MachineInstr *MI) const;
323 /// EmitComments - Pretty-print comments for instructions
324 void EmitComments(const MachineInstr &MI) const;
325 /// EmitComments - Pretty-print comments for basic blocks
326 void EmitComments(const MachineBasicBlock &MBB) const;
328 /// printMCInst - Print an MCInst for this target.
330 /// Note, this is only a temporary hack to allow the MCStreamer to print
331 /// instructions, do not use this function outside of llvm-mc.
332 virtual void printMCInst(const MCInst *MI);
334 /// GetMBBSymbol - Return the MCSymbol corresponding to the specified basic
336 MCSymbol *GetMBBSymbol(unsigned MBBID) const;
338 /// EmitBasicBlockStart - This method prints the label for the specified
339 /// MachineBasicBlock, an alignment (if present) and a comment describing
340 /// it if appropriate.
341 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
343 /// EmitZeros - Emit a block of zeros.
345 void EmitZeros(uint64_t NumZeros, unsigned AddrSpace = 0) const;
347 /// EmitString - Emit a zero-byte-terminated string constant.
349 virtual void EmitString(const ConstantArray *CVA) const;
351 /// EmitConstantValueOnly - Print out the specified constant, without a
352 /// storage class. Only constants of first-class type are allowed here.
353 void EmitConstantValueOnly(const Constant *CV);
355 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
356 void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
358 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
360 /// processDebugLoc - Processes the debug information of each machine
361 /// instruction's DebugLoc.
362 void processDebugLoc(DebugLoc DL);
364 /// printInlineAsm - This method formats and prints the specified machine
365 /// instruction that is an inline asm.
366 void printInlineAsm(const MachineInstr *MI) const;
368 /// printImplicitDef - This method prints the specified machine instruction
369 /// that is an implicit def.
370 virtual void printImplicitDef(const MachineInstr *MI) const;
373 /// printPICJumpTableSetLabel - This method prints a set label for the
374 /// specified MachineBasicBlock for a jumptable entry.
375 virtual void printPICJumpTableSetLabel(unsigned uid,
376 const MachineBasicBlock *MBB) const;
377 virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
378 const MachineBasicBlock *MBB) const;
379 virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
380 const MachineBasicBlock *MBB,
383 /// printDataDirective - This method prints the asm directive for the
385 void printDataDirective(const Type *type, unsigned AddrSpace = 0);
387 /// printVisibility - This prints visibility information about symbol, if
388 /// this is suported by the target.
389 void printVisibility(const std::string& Name, unsigned Visibility) const;
391 /// printOffset - This is just convenient handler for printing offsets.
392 void printOffset(int64_t Offset) const;
395 void EmitLLVMUsedList(Constant *List);
396 void EmitXXStructorList(Constant *List);
397 void EmitGlobalConstantStruct(const ConstantStruct* CVS,
399 void EmitGlobalConstantArray(const ConstantArray* CVA, unsigned AddrSpace);
400 void EmitGlobalConstantVector(const ConstantVector* CP);
401 void EmitGlobalConstantFP(const ConstantFP* CFP, unsigned AddrSpace);
402 void EmitGlobalConstantLargeInt(const ConstantInt* CI, unsigned AddrSpace);
403 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);