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"
28 class GCMetadataPrinter;
31 class MachineConstantPoolEntry;
32 class MachineConstantPoolValue;
33 class MachineModuleInfo;
40 /// AsmPrinter - This class is intended to be used as a driving class for all
42 class AsmPrinter : public MachineFunctionPass {
45 /// FunctionNumber - This provides a unique ID for each function emitted in
46 /// this translation unit. It is autoincremented by SetupMachineFunction,
47 /// and can be accessed with getFunctionNumber() and
48 /// IncrementFunctionNumber().
50 unsigned FunctionNumber;
52 /// MachineModuleInfo - This is needed because printDeclare() has to insert
53 /// DebugVariable entries into the dwarf table. This is a short term hack
54 /// that ought be fixed soon.
55 MachineModuleInfo *MMI;
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 // Necessary for external weak linkage support
64 std::set<const GlobalValue*> ExtWeakSymbols;
67 /// Output stream on which we're printing assembly code.
71 /// Target machine description.
75 /// Target Asm Printer information.
77 const TargetAsmInfo *TAI;
79 /// Target Register Information.
81 const TargetRegisterInfo *TRI;
83 /// Name-mangler for global names.
87 /// Cache of mangled name for current function. This is recalculated at the
88 /// beginning of each call to runOnMachineFunction().
90 std::string CurrentFnName;
92 /// CurrentSection - The current section we are emitting to. This is
93 /// controlled and used by the SwitchSection method.
94 std::string CurrentSection;
95 const Section* CurrentSection_;
97 /// IsInTextSection - True if the current section we are emitting to is a
102 AsmPrinter(raw_ostream &o, TargetMachine &TM, const TargetAsmInfo *T);
105 virtual ~AsmPrinter();
107 /// SwitchToTextSection - Switch to the specified section of the executable
108 /// if we are not already in it! If GV is non-null and if the global has an
109 /// explicitly requested section, we switch to the section indicated for the
110 /// global instead of NewSection.
112 /// If the new section is an empty string, this method forgets what the
113 /// current section is, but does not emit a .section directive.
115 /// This method is used when about to emit executable code.
117 void SwitchToTextSection(const char *NewSection, const GlobalValue *GV = NULL);
119 /// SwitchToDataSection - Switch to the specified section of the executable
120 /// if we are not already in it! If GV is non-null and if the global has an
121 /// explicitly requested section, we switch to the section indicated for the
122 /// global instead of NewSection.
124 /// If the new section is an empty string, this method forgets what the
125 /// current section is, but does not emit a .section directive.
127 /// This method is used when about to emit data. For most assemblers, this
128 /// is the same as the SwitchToTextSection method, but not all assemblers
131 void SwitchToDataSection(const char *NewSection, const GlobalValue *GV = NULL);
133 /// SwitchToSection - Switch to the specified section of the executable if
134 /// we are not already in it!
135 void SwitchToSection(const Section* NS);
137 /// getGlobalLinkName - Returns the asm/link name of of the specified
138 /// global variable. Should be overridden by each target asm printer to
139 /// generate the appropriate value.
140 virtual const std::string getGlobalLinkName(const GlobalVariable *GV) const;
142 /// EmitExternalGlobal - Emit the external reference to a global variable.
143 /// Should be overridden if an indirect reference should be used.
144 virtual void EmitExternalGlobal(const GlobalVariable *GV);
146 /// getCurrentFunctionEHName - Called to return (and cache) the
149 std::string getCurrentFunctionEHName(const MachineFunction *MF);
152 /// getAnalysisUsage - Record analysis usage.
154 void getAnalysisUsage(AnalysisUsage &AU) const;
156 /// doInitialization - Set up the AsmPrinter when we are working on a new
157 /// module. If your pass overrides this, it must make sure to explicitly
158 /// call this implementation.
159 bool doInitialization(Module &M);
161 /// doFinalization - Shut down the asmprinter. If you override this in your
162 /// pass, you must make sure to call it explicitly.
163 bool doFinalization(Module &M);
165 /// PrintSpecial - Print information related to the specified machine instr
166 /// that is independent of the operand, and may be independent of the instr
167 /// itself. This can be useful for portably encoding the comment character
168 /// or other bits of target-specific knowledge into the asmstrings. The
169 /// syntax used is ${:comment}. Targets can override this to add support
170 /// for their own strange codes.
171 virtual void PrintSpecial(const MachineInstr *MI, const char *Code);
173 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
174 /// instruction, using the specified assembler variant. Targets should
175 /// override this to format as appropriate. This method can return true if
176 /// the operand is erroneous.
177 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
178 unsigned AsmVariant, const char *ExtraCode);
180 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
181 /// instruction, using the specified assembler variant as an address.
182 /// Targets should override this to format as appropriate. This method can
183 /// return true if the operand is erroneous.
184 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
186 const char *ExtraCode);
188 /// SetupMachineFunction - This should be called when a new MachineFunction
189 /// is being processed from runOnMachineFunction.
190 void SetupMachineFunction(MachineFunction &MF);
192 /// getFunctionNumber - Return a unique ID for the current function.
194 unsigned getFunctionNumber() const { return FunctionNumber; }
196 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
197 /// not normally call this, as the counter is automatically bumped by
198 /// SetupMachineFunction.
199 void IncrementFunctionNumber() { FunctionNumber++; }
201 /// EmitConstantPool - Print to the current output stream assembly
202 /// representations of the constants in the constant pool MCP. This is
203 /// used to print out constants which have been "spilled to memory" by
204 /// the code generator.
206 void EmitConstantPool(MachineConstantPool *MCP);
208 /// EmitJumpTableInfo - Print assembly representations of the jump tables
209 /// used by the current function to the current output stream.
211 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
213 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
214 /// special global used by LLVM. If so, emit it and return true, otherwise
215 /// do nothing and return false.
216 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
219 //===------------------------------------------------------------------===//
220 /// LEB 128 number encoding.
222 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
223 /// representing an unsigned leb128 value.
224 void PrintULEB128(unsigned Value) const;
226 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
227 /// representing a signed leb128 value.
228 void PrintSLEB128(int Value) const;
230 //===------------------------------------------------------------------===//
231 // Emission and print routines
234 /// PrintHex - Print a value as a hexidecimal value.
236 void PrintHex(int Value) const;
238 /// EOL - Print a newline character to asm stream. If a comment is present
239 /// then it will be printed first. Comments should not contain '\n'.
241 void EOL(const std::string &Comment) const;
242 void EOL(const char* Comment) const;
244 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
245 /// unsigned leb128 value.
246 void EmitULEB128Bytes(unsigned Value) const;
248 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
249 /// signed leb128 value.
250 void EmitSLEB128Bytes(int Value) const;
252 /// EmitInt8 - Emit a byte directive and value.
254 void EmitInt8(int Value) const;
256 /// EmitInt16 - Emit a short directive and value.
258 void EmitInt16(int Value) const;
260 /// EmitInt32 - Emit a long directive and value.
262 void EmitInt32(int Value) const;
264 /// EmitInt64 - Emit a long long directive and value.
266 void EmitInt64(uint64_t Value) const;
268 /// EmitString - Emit a string with quotes and a null terminator.
269 /// Special characters are emitted properly.
270 /// @verbatim (Eg. '\t') @endverbatim
271 void EmitString(const std::string &String) const;
273 /// EmitFile - Emit a .file directive.
274 void EmitFile(unsigned Number, const std::string &Name) const;
276 //===------------------------------------------------------------------===//
278 /// EmitAlignment - Emit an alignment directive to the specified power of
279 /// two boundary. For example, if you pass in 3 here, you will get an 8
280 /// byte alignment. If a global value is specified, and if that global has
281 /// an explicit alignment requested, it will unconditionally override the
282 /// alignment request. However, if ForcedAlignBits is specified, this value
283 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
284 /// and the alignment computed with NumBits and the global. If UseFillExpr
285 /// is true, it also emits an optional second value FillValue which the
286 /// assembler uses to fill gaps to match alignment for text sections if the
287 /// has specified a non-zero fill value.
289 /// The algorithm is:
291 /// if (GV && GV->hasalignment) Align = GV->getalignment();
292 /// Align = std::max(Align, ForcedAlignBits);
294 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
295 unsigned ForcedAlignBits = 0,
296 bool UseFillExpr = true) const;
298 /// printLabel - This method prints a local label used by debug and
299 /// exception handling tables.
300 void printLabel(const MachineInstr *MI) const;
301 void printLabel(unsigned Id) const;
303 /// printDeclare - This method prints a local variable declaration used by
305 void printDeclare(const MachineInstr *MI) const;
308 /// EmitZeros - Emit a block of zeros.
310 void EmitZeros(uint64_t NumZeros) const;
312 /// EmitString - Emit a zero-byte-terminated string constant.
314 virtual void EmitString(const ConstantArray *CVA) const;
316 /// EmitConstantValueOnly - Print out the specified constant, without a
317 /// storage class. Only constants of first-class type are allowed here.
318 void EmitConstantValueOnly(const Constant *CV);
320 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
321 void EmitGlobalConstant(const Constant* CV);
323 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
325 /// printInlineAsm - This method formats and prints the specified machine
326 /// instruction that is an inline asm.
327 void printInlineAsm(const MachineInstr *MI) const;
329 /// printImplicitDef - This method prints the specified machine instruction
330 /// that is an implicit def.
331 virtual void printImplicitDef(const MachineInstr *MI) const;
333 /// printBasicBlockLabel - This method prints the label for the specified
334 /// MachineBasicBlock
335 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
336 bool printAlign = false,
337 bool printColon = false,
338 bool printComment = true) const;
340 /// printPICJumpTableSetLabel - This method prints a set label for the
341 /// specified MachineBasicBlock for a jumptable entry.
342 virtual void printPICJumpTableSetLabel(unsigned uid,
343 const MachineBasicBlock *MBB) const;
344 virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
345 const MachineBasicBlock *MBB) const;
346 virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
347 const MachineBasicBlock *MBB,
350 /// printDataDirective - This method prints the asm directive for the
352 void printDataDirective(const Type *type);
354 /// printSuffixedName - This prints a name with preceding
355 /// getPrivateGlobalPrefix and the specified suffix, handling quoted names
357 void printSuffixedName(const char *Name, const char *Suffix,
358 const char *Prefix = 0);
359 void printSuffixedName(const std::string &Name, const char* Suffix);
361 /// printVisibility - This prints visibility information about symbol, if
362 /// this is suported by the target.
363 void printVisibility(const std::string& Name, unsigned Visibility) const;
365 /// printOffset - This is just convenient handler for printing offsets.
366 void printOffset(int64_t Offset) const;
369 const GlobalValue *findGlobalValue(const Constant* CV);
370 void EmitLLVMUsedList(Constant *List);
371 void EmitXXStructorList(Constant *List);
372 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);