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/ADT/DenseMap.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Target/TargetMachine.h"
32 class GCMetadataPrinter;
34 class MachineConstantPoolEntry;
35 class MachineConstantPoolValue;
36 class MachineModuleInfo;
43 class formatted_raw_ostream;
45 /// AsmPrinter - This class is intended to be used as a driving class for all
47 class AsmPrinter : public MachineFunctionPass {
50 /// FunctionNumber - This provides a unique ID for each function emitted in
51 /// this translation unit. It is autoincremented by SetupMachineFunction,
52 /// and can be accessed with getFunctionNumber() and
53 /// IncrementFunctionNumber().
55 unsigned FunctionNumber;
57 // GCMetadataPrinters - The garbage collection metadata printer table.
58 typedef DenseMap<GCStrategy*,GCMetadataPrinter*> gcp_map_type;
59 typedef gcp_map_type::iterator gcp_iterator;
60 gcp_map_type GCMetadataPrinters;
63 /// MMI - If available, this is a pointer to the current MachineModuleInfo.
64 MachineModuleInfo *MMI;
66 /// DW - If available, this is a pointer to the current dwarf writer.
70 /// Output stream on which we're printing assembly code.
72 formatted_raw_ostream &O;
74 /// Target machine description.
78 /// Target Asm Printer information.
80 const TargetAsmInfo *TAI;
82 /// Target Register Information.
84 const TargetRegisterInfo *TRI;
86 /// The current machine function.
87 const MachineFunction *MF;
89 /// Name-mangler for global names.
93 /// Cache of mangled name for current function. This is recalculated at the
94 /// beginning of each call to runOnMachineFunction().
96 std::string CurrentFnName;
98 /// CurrentSection - The current section we are emitting to. This is
99 /// controlled and used by the SwitchSection method.
100 std::string CurrentSection;
101 const Section* CurrentSection_;
103 /// IsInTextSection - True if the current section we are emitting to is a
105 bool IsInTextSection;
107 /// VerboseAsm - Emit comments in assembly output if this is true.
111 /// Private state for PrintSpecial()
112 // Assign a unique ID to this machine instruction.
113 mutable const MachineInstr *LastMI;
114 mutable const Function *LastFn;
115 mutable unsigned Counter;
117 // Private state for processDebugLock()
118 mutable DebugLocTuple PrevDLT;
121 explicit AsmPrinter(formatted_raw_ostream &o, TargetMachine &TM,
122 const TargetAsmInfo *T, bool V);
125 virtual ~AsmPrinter();
127 /// isVerbose - Return true if assembly output should contain comments.
129 bool isVerbose() const { return VerboseAsm; }
131 /// SwitchToTextSection - Switch to the specified section of the executable
132 /// if we are not already in it! If GV is non-null and if the global has an
133 /// explicitly requested section, we switch to the section indicated for the
134 /// global instead of NewSection.
136 /// If the new section is an empty string, this method forgets what the
137 /// current section is, but does not emit a .section directive.
139 /// This method is used when about to emit executable code.
141 void SwitchToTextSection(const char *NewSection,
142 const GlobalValue *GV = NULL);
144 /// SwitchToDataSection - Switch to the specified section of the executable
145 /// if we are not already in it! If GV is non-null and if the global has an
146 /// explicitly requested section, we switch to the section indicated for the
147 /// global instead of NewSection.
149 /// If the new section is an empty string, this method forgets what the
150 /// current section is, but does not emit a .section directive.
152 /// This method is used when about to emit data. For most assemblers, this
153 /// is the same as the SwitchToTextSection method, but not all assemblers
156 void SwitchToDataSection(const char *NewSection,
157 const GlobalValue *GV = NULL);
159 /// SwitchToSection - Switch to the specified section of the executable if
160 /// we are not already in it!
161 void SwitchToSection(const Section* NS);
163 /// getGlobalLinkName - Returns the asm/link name of of the specified
164 /// global variable. Should be overridden by each target asm printer to
165 /// generate the appropriate value.
166 virtual const std::string &getGlobalLinkName(const GlobalVariable *GV,
167 std::string &LinkName) const;
169 /// EmitExternalGlobal - Emit the external reference to a global variable.
170 /// Should be overridden if an indirect reference should be used.
171 virtual void EmitExternalGlobal(const GlobalVariable *GV);
173 /// getCurrentFunctionEHName - Called to return (and cache) the
176 const std::string &getCurrentFunctionEHName(const MachineFunction *MF,
177 std::string &FuncEHName) const;
180 /// getAnalysisUsage - Record analysis usage.
182 void getAnalysisUsage(AnalysisUsage &AU) const;
184 /// doInitialization - Set up the AsmPrinter when we are working on a new
185 /// module. If your pass overrides this, it must make sure to explicitly
186 /// call this implementation.
187 bool doInitialization(Module &M);
189 /// doFinalization - Shut down the asmprinter. If you override this in your
190 /// pass, you must make sure to call it explicitly.
191 bool doFinalization(Module &M);
193 /// PrintSpecial - Print information related to the specified machine instr
194 /// that is independent of the operand, and may be independent of the instr
195 /// itself. This can be useful for portably encoding the comment character
196 /// or other bits of target-specific knowledge into the asmstrings. The
197 /// syntax used is ${:comment}. Targets can override this to add support
198 /// for their own strange codes.
199 virtual void PrintSpecial(const MachineInstr *MI, const char *Code) const;
201 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
202 /// instruction, using the specified assembler variant. Targets should
203 /// override this to format as appropriate. This method can return true if
204 /// the operand is erroneous.
205 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
206 unsigned AsmVariant, const char *ExtraCode);
208 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
209 /// instruction, using the specified assembler variant as an address.
210 /// Targets should override this to format as appropriate. This method can
211 /// return true if the operand is erroneous.
212 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
214 const char *ExtraCode);
216 /// SetupMachineFunction - This should be called when a new MachineFunction
217 /// is being processed from runOnMachineFunction.
218 void SetupMachineFunction(MachineFunction &MF);
220 /// getFunctionNumber - Return a unique ID for the current function.
222 unsigned getFunctionNumber() const { return FunctionNumber; }
224 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
225 /// not normally call this, as the counter is automatically bumped by
226 /// SetupMachineFunction.
227 void IncrementFunctionNumber() { FunctionNumber++; }
229 /// EmitConstantPool - Print to the current output stream assembly
230 /// representations of the constants in the constant pool MCP. This is
231 /// used to print out constants which have been "spilled to memory" by
232 /// the code generator.
234 void EmitConstantPool(MachineConstantPool *MCP);
236 /// EmitJumpTableInfo - Print assembly representations of the jump tables
237 /// used by the current function to the current output stream.
239 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
241 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
242 /// special global used by LLVM. If so, emit it and return true, otherwise
243 /// do nothing and return false.
244 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
247 //===------------------------------------------------------------------===//
248 /// LEB 128 number encoding.
250 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
251 /// representing an unsigned leb128 value.
252 void PrintULEB128(unsigned Value) const;
254 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
255 /// representing a signed leb128 value.
256 void PrintSLEB128(int Value) const;
258 //===------------------------------------------------------------------===//
259 // Emission and print routines
262 /// PrintHex - Print a value as a hexidecimal value.
264 void PrintHex(int Value) const;
266 /// EOL - Print a newline character to asm stream. If a comment is present
267 /// then it will be printed first. Comments should not contain '\n'.
269 void EOL(const std::string &Comment) const;
270 void EOL(const char* Comment) const;
272 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
273 /// unsigned leb128 value.
274 void EmitULEB128Bytes(unsigned Value) const;
276 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
277 /// signed leb128 value.
278 void EmitSLEB128Bytes(int Value) const;
280 /// EmitInt8 - Emit a byte directive and value.
282 void EmitInt8(int Value) const;
284 /// EmitInt16 - Emit a short directive and value.
286 void EmitInt16(int Value) const;
288 /// EmitInt32 - Emit a long directive and value.
290 void EmitInt32(int Value) const;
292 /// EmitInt64 - Emit a long long directive and value.
294 void EmitInt64(uint64_t Value) const;
296 /// EmitString - Emit a string with quotes and a null terminator.
297 /// Special characters are emitted properly.
298 /// @verbatim (Eg. '\t') @endverbatim
299 void EmitString(const std::string &String) const;
300 void EmitString(const char *String, unsigned Size) const;
302 /// EmitFile - Emit a .file directive.
303 void EmitFile(unsigned Number, const std::string &Name) const;
305 //===------------------------------------------------------------------===//
307 /// EmitAlignment - Emit an alignment directive to the specified power of
308 /// two boundary. For example, if you pass in 3 here, you will get an 8
309 /// byte alignment. If a global value is specified, and if that global has
310 /// an explicit alignment requested, it will unconditionally override the
311 /// alignment request. However, if ForcedAlignBits is specified, this value
312 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
313 /// and the alignment computed with NumBits and the global. If UseFillExpr
314 /// is true, it also emits an optional second value FillValue which the
315 /// assembler uses to fill gaps to match alignment for text sections if the
316 /// has specified a non-zero fill value.
318 /// The algorithm is:
320 /// if (GV && GV->hasalignment) Align = GV->getalignment();
321 /// Align = std::max(Align, ForcedAlignBits);
323 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
324 unsigned ForcedAlignBits = 0,
325 bool UseFillExpr = true) const;
327 /// printLabel - This method prints a local label used by debug and
328 /// exception handling tables.
329 void printLabel(const MachineInstr *MI) const;
330 void printLabel(unsigned Id) const;
332 /// printDeclare - This method prints a local variable declaration used by
334 void printDeclare(const MachineInstr *MI) const;
336 /// postInstructionAction - Handling printing of items after the
337 /// instruction iteself has been printed (e.g. comments)
338 void postInstructionAction(const MachineInstr &MI) const {
339 postInstructionActionImpl(MI);
342 void postInstructionAction(const MCInst &MI) const {
343 postInstructionActionImpl(MI);
348 /// EmitZeros - Emit a block of zeros.
350 void EmitZeros(uint64_t NumZeros, unsigned AddrSpace = 0) const;
352 /// EmitString - Emit a zero-byte-terminated string constant.
354 virtual void EmitString(const ConstantArray *CVA) const;
356 /// EmitConstantValueOnly - Print out the specified constant, without a
357 /// storage class. Only constants of first-class type are allowed here.
358 void EmitConstantValueOnly(const Constant *CV);
360 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
361 void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
363 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
365 /// processDebugLoc - Processes the debug information of each machine
366 /// instruction's DebugLoc.
367 void processDebugLoc(DebugLoc DL);
369 /// printInlineAsm - This method formats and prints the specified machine
370 /// instruction that is an inline asm.
371 void printInlineAsm(const MachineInstr *MI) const;
373 /// printImplicitDef - This method prints the specified machine instruction
374 /// that is an implicit def.
375 virtual void printImplicitDef(const MachineInstr *MI) const;
377 /// printBasicBlockLabel - This method prints the label for the specified
378 /// MachineBasicBlock
379 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
380 bool printAlign = false,
381 bool printColon = false,
382 bool printComment = true) const;
384 /// printPICJumpTableSetLabel - This method prints a set label for the
385 /// specified MachineBasicBlock for a jumptable entry.
386 virtual void printPICJumpTableSetLabel(unsigned uid,
387 const MachineBasicBlock *MBB) const;
388 virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
389 const MachineBasicBlock *MBB) const;
390 virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
391 const MachineBasicBlock *MBB,
394 /// printDataDirective - This method prints the asm directive for the
396 void printDataDirective(const Type *type, unsigned AddrSpace = 0);
398 /// printVisibility - This prints visibility information about symbol, if
399 /// this is suported by the target.
400 void printVisibility(const std::string& Name, unsigned Visibility) const;
402 /// printOffset - This is just convenient handler for printing offsets.
403 void printOffset(int64_t Offset) const;
406 const GlobalValue *findGlobalValue(const Constant* CV);
407 void EmitLLVMUsedList(Constant *List);
408 void EmitXXStructorList(Constant *List);
409 void EmitGlobalConstantStruct(const ConstantStruct* CVS,
411 void EmitGlobalConstantArray(const ConstantArray* CVA, unsigned AddrSpace);
412 void EmitGlobalConstantVector(const ConstantVector* CP);
413 void EmitGlobalConstantFP(const ConstantFP* CFP, unsigned AddrSpace);
414 void EmitGlobalConstantLargeInt(const ConstantInt* CI, unsigned AddrSpace);
415 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);
417 /// EmitComments - Pretty-print comments for instructions
418 void EmitComments(const MachineInstr &MI) const;
419 /// EmitComments - Pretty-print comments for instructions
420 void EmitComments(const MCInst &MI) const;
422 virtual void postInstructionActionImpl(const MachineInstr &MI) const {}
423 virtual void postInstructionActionImpl(const MCInst &MI) const {}