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/DataTypes.h"
21 #include "llvm/ADT/DenseMap.h"
31 class GCMetadataPrinter;
33 class MachineConstantPoolEntry;
34 class MachineConstantPoolValue;
42 /// AsmPrinter - This class is intended to be used as a driving class for all
44 class AsmPrinter : public MachineFunctionPass {
47 /// FunctionNumber - This provides a unique ID for each function emitted in
48 /// this translation unit. It is autoincremented by SetupMachineFunction,
49 /// and can be accessed with getFunctionNumber() and
50 /// IncrementFunctionNumber().
52 unsigned FunctionNumber;
54 /// DW -This is needed because printDeclare() has to insert
55 /// DbgVariable entries into the dwarf table. This is a short term hack
56 /// that ought be fixed soon.
59 // GCMetadataPrinters - The garbage collection metadata printer table.
60 typedef DenseMap<GCStrategy*,GCMetadataPrinter*> gcp_map_type;
61 typedef gcp_map_type::iterator gcp_iterator;
62 gcp_map_type GCMetadataPrinters;
65 // Necessary for external weak linkage support
66 std::set<const GlobalValue*> ExtWeakSymbols;
69 /// Output stream on which we're printing assembly code.
73 /// Target machine description.
77 /// Target Asm Printer information.
79 const TargetAsmInfo *TAI;
81 /// Target Register Information.
83 const TargetRegisterInfo *TRI;
85 /// Name-mangler for global names.
89 /// Cache of mangled name for current function. This is recalculated at the
90 /// beginning of each call to runOnMachineFunction().
92 std::string CurrentFnName;
94 /// CurrentSection - The current section we are emitting to. This is
95 /// controlled and used by the SwitchSection method.
96 std::string CurrentSection;
97 const Section* CurrentSection_;
99 /// IsInTextSection - True if the current section we are emitting to is a
101 bool IsInTextSection;
104 AsmPrinter(raw_ostream &o, TargetMachine &TM, const TargetAsmInfo *T);
107 virtual ~AsmPrinter();
109 /// SwitchToTextSection - Switch to the specified section of the executable
110 /// if we are not already in it! If GV is non-null and if the global has an
111 /// explicitly requested section, we switch to the section indicated for the
112 /// global instead of NewSection.
114 /// If the new section is an empty string, this method forgets what the
115 /// current section is, but does not emit a .section directive.
117 /// This method is used when about to emit executable code.
119 void SwitchToTextSection(const char *NewSection, const GlobalValue *GV = NULL);
121 /// SwitchToDataSection - Switch to the specified section of the executable
122 /// if we are not already in it! If GV is non-null and if the global has an
123 /// explicitly requested section, we switch to the section indicated for the
124 /// global instead of NewSection.
126 /// If the new section is an empty string, this method forgets what the
127 /// current section is, but does not emit a .section directive.
129 /// This method is used when about to emit data. For most assemblers, this
130 /// is the same as the SwitchToTextSection method, but not all assemblers
133 void SwitchToDataSection(const char *NewSection, const GlobalValue *GV = NULL);
135 /// SwitchToSection - Switch to the specified section of the executable if
136 /// we are not already in it!
137 void SwitchToSection(const Section* NS);
139 /// getGlobalLinkName - Returns the asm/link name of of the specified
140 /// global variable. Should be overridden by each target asm printer to
141 /// generate the appropriate value.
142 virtual const std::string getGlobalLinkName(const GlobalVariable *GV) const;
144 /// EmitExternalGlobal - Emit the external reference to a global variable.
145 /// Should be overridden if an indirect reference should be used.
146 virtual void EmitExternalGlobal(const GlobalVariable *GV);
148 /// getCurrentFunctionEHName - Called to return (and cache) the
151 std::string getCurrentFunctionEHName(const MachineFunction *MF);
154 /// getAnalysisUsage - Record analysis usage.
156 void getAnalysisUsage(AnalysisUsage &AU) const;
158 /// doInitialization - Set up the AsmPrinter when we are working on a new
159 /// module. If your pass overrides this, it must make sure to explicitly
160 /// call this implementation.
161 bool doInitialization(Module &M);
163 /// doFinalization - Shut down the asmprinter. If you override this in your
164 /// pass, you must make sure to call it explicitly.
165 bool doFinalization(Module &M);
167 /// PrintSpecial - Print information related to the specified machine instr
168 /// that is independent of the operand, and may be independent of the instr
169 /// itself. This can be useful for portably encoding the comment character
170 /// or other bits of target-specific knowledge into the asmstrings. The
171 /// syntax used is ${:comment}. Targets can override this to add support
172 /// for their own strange codes.
173 virtual void PrintSpecial(const MachineInstr *MI, const char *Code);
175 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
176 /// instruction, using the specified assembler variant. Targets should
177 /// override this to format as appropriate. This method can return true if
178 /// the operand is erroneous.
179 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
180 unsigned AsmVariant, const char *ExtraCode);
182 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
183 /// instruction, using the specified assembler variant as an address.
184 /// Targets should override this to format as appropriate. This method can
185 /// return true if the operand is erroneous.
186 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
188 const char *ExtraCode);
190 /// SetupMachineFunction - This should be called when a new MachineFunction
191 /// is being processed from runOnMachineFunction.
192 void SetupMachineFunction(MachineFunction &MF);
194 /// getFunctionNumber - Return a unique ID for the current function.
196 unsigned getFunctionNumber() const { return FunctionNumber; }
198 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
199 /// not normally call this, as the counter is automatically bumped by
200 /// SetupMachineFunction.
201 void IncrementFunctionNumber() { FunctionNumber++; }
203 /// EmitConstantPool - Print to the current output stream assembly
204 /// representations of the constants in the constant pool MCP. This is
205 /// used to print out constants which have been "spilled to memory" by
206 /// the code generator.
208 void EmitConstantPool(MachineConstantPool *MCP);
210 /// EmitJumpTableInfo - Print assembly representations of the jump tables
211 /// used by the current function to the current output stream.
213 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
215 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
216 /// special global used by LLVM. If so, emit it and return true, otherwise
217 /// do nothing and return false.
218 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
221 //===------------------------------------------------------------------===//
222 /// LEB 128 number encoding.
224 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
225 /// representing an unsigned leb128 value.
226 void PrintULEB128(unsigned Value) const;
228 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
229 /// representing a signed leb128 value.
230 void PrintSLEB128(int Value) const;
232 //===------------------------------------------------------------------===//
233 // Emission and print routines
236 /// PrintHex - Print a value as a hexidecimal value.
238 void PrintHex(int Value) const;
240 /// EOL - Print a newline character to asm stream. If a comment is present
241 /// then it will be printed first. Comments should not contain '\n'.
243 void EOL(const std::string &Comment) const;
244 void EOL(const char* Comment) const;
246 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
247 /// unsigned leb128 value.
248 void EmitULEB128Bytes(unsigned Value) const;
250 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
251 /// signed leb128 value.
252 void EmitSLEB128Bytes(int Value) const;
254 /// EmitInt8 - Emit a byte directive and value.
256 void EmitInt8(int Value) const;
258 /// EmitInt16 - Emit a short directive and value.
260 void EmitInt16(int Value) const;
262 /// EmitInt32 - Emit a long directive and value.
264 void EmitInt32(int Value) const;
266 /// EmitInt64 - Emit a long long directive and value.
268 void EmitInt64(uint64_t Value) const;
270 /// EmitString - Emit a string with quotes and a null terminator.
271 /// Special characters are emitted properly.
272 /// @verbatim (Eg. '\t') @endverbatim
273 void EmitString(const std::string &String) const;
275 /// EmitFile - Emit a .file directive.
276 void EmitFile(unsigned Number, const std::string &Name) const;
278 //===------------------------------------------------------------------===//
280 /// EmitAlignment - Emit an alignment directive to the specified power of
281 /// two boundary. For example, if you pass in 3 here, you will get an 8
282 /// byte alignment. If a global value is specified, and if that global has
283 /// an explicit alignment requested, it will unconditionally override the
284 /// alignment request. However, if ForcedAlignBits is specified, this value
285 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
286 /// and the alignment computed with NumBits and the global. If UseFillExpr
287 /// is true, it also emits an optional second value FillValue which the
288 /// assembler uses to fill gaps to match alignment for text sections if the
289 /// has specified a non-zero fill value.
291 /// The algorithm is:
293 /// if (GV && GV->hasalignment) Align = GV->getalignment();
294 /// Align = std::max(Align, ForcedAlignBits);
296 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
297 unsigned ForcedAlignBits = 0,
298 bool UseFillExpr = true) const;
300 /// printLabel - This method prints a local label used by debug and
301 /// exception handling tables.
302 void printLabel(const MachineInstr *MI) const;
303 void printLabel(unsigned Id) const;
305 /// printDeclare - This method prints a local variable declaration used by
307 void printDeclare(const MachineInstr *MI) const;
310 /// EmitZeros - Emit a block of zeros.
312 void EmitZeros(uint64_t NumZeros) const;
314 /// EmitString - Emit a zero-byte-terminated string constant.
316 virtual void EmitString(const ConstantArray *CVA) const;
318 /// EmitConstantValueOnly - Print out the specified constant, without a
319 /// storage class. Only constants of first-class type are allowed here.
320 void EmitConstantValueOnly(const Constant *CV);
322 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
323 void EmitGlobalConstant(const Constant* CV);
325 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
327 /// printInlineAsm - This method formats and prints the specified machine
328 /// instruction that is an inline asm.
329 void printInlineAsm(const MachineInstr *MI) const;
331 /// printImplicitDef - This method prints the specified machine instruction
332 /// that is an implicit def.
333 virtual void printImplicitDef(const MachineInstr *MI) const;
335 /// printBasicBlockLabel - This method prints the label for the specified
336 /// MachineBasicBlock
337 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
338 bool printAlign = false,
339 bool printColon = false,
340 bool printComment = true) const;
342 /// printPICJumpTableSetLabel - This method prints a set label for the
343 /// specified MachineBasicBlock for a jumptable entry.
344 virtual void printPICJumpTableSetLabel(unsigned uid,
345 const MachineBasicBlock *MBB) const;
346 virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
347 const MachineBasicBlock *MBB) const;
348 virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
349 const MachineBasicBlock *MBB,
352 /// printDataDirective - This method prints the asm directive for the
354 void printDataDirective(const Type *type);
356 /// printSuffixedName - This prints a name with preceding
357 /// getPrivateGlobalPrefix and the specified suffix, handling quoted names
359 void printSuffixedName(const char *Name, const char *Suffix,
360 const char *Prefix = 0);
361 void printSuffixedName(const std::string &Name, const char* Suffix);
363 /// printVisibility - This prints visibility information about symbol, if
364 /// this is suported by the target.
365 void printVisibility(const std::string& Name, unsigned Visibility) const;
367 /// printOffset - This is just convenient handler for printing offsets.
368 void printOffset(int64_t Offset) const;
371 const GlobalValue *findGlobalValue(const Constant* CV);
372 void EmitLLVMUsedList(Constant *List);
373 void EmitXXStructorList(Constant *List);
374 void EmitGlobalConstantStruct(const ConstantStruct* CVS);
375 void EmitGlobalConstantArray(const ConstantArray* CVA);
376 void EmitGlobalConstantVector(const ConstantVector* CP);
377 void EmitGlobalConstantFP(const ConstantFP* CFP);
378 void EmitGlobalConstantLargeInt(const ConstantInt* CI);
379 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);