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;
96 /// IsInTextSection - True if the current section we are emitting to is a
101 AsmPrinter(raw_ostream &o, TargetMachine &TM, const TargetAsmInfo *T);
104 virtual ~AsmPrinter();
106 /// SwitchToTextSection - Switch to the specified section of the executable
107 /// if we are not already in it! If GV is non-null and if the global has an
108 /// explicitly requested section, we switch to the section indicated for the
109 /// global instead of NewSection.
111 /// If the new section is an empty string, this method forgets what the
112 /// current section is, but does not emit a .section directive.
114 /// This method is used when about to emit executable code.
116 void SwitchToTextSection(const char *NewSection, const GlobalValue *GV = NULL);
118 /// SwitchToDataSection - Switch to the specified section of the executable
119 /// if we are not already in it! If GV is non-null and if the global has an
120 /// explicitly requested section, we switch to the section indicated for the
121 /// global instead of NewSection.
123 /// If the new section is an empty string, this method forgets what the
124 /// current section is, but does not emit a .section directive.
126 /// This method is used when about to emit data. For most assemblers, this
127 /// is the same as the SwitchToTextSection method, but not all assemblers
130 void SwitchToDataSection(const char *NewSection, const GlobalValue *GV = NULL);
132 /// SwitchToSection - Switch to the specified section of the executable if
133 /// we are not already in it!
134 void SwitchToSection(const Section* NS);
136 /// getGlobalLinkName - Returns the asm/link name of of the specified
137 /// global variable. Should be overridden by each target asm printer to
138 /// generate the appropriate value.
139 virtual const std::string getGlobalLinkName(const GlobalVariable *GV) const;
141 /// EmitExternalGlobal - Emit the external reference to a global variable.
142 /// Should be overridden if an indirect reference should be used.
143 virtual void EmitExternalGlobal(const GlobalVariable *GV);
145 /// getCurrentFunctionEHName - Called to return (and cache) the
148 std::string getCurrentFunctionEHName(const MachineFunction *MF);
151 /// getAnalysisUsage - Record analysis usage.
153 void getAnalysisUsage(AnalysisUsage &AU) const;
155 /// doInitialization - Set up the AsmPrinter when we are working on a new
156 /// module. If your pass overrides this, it must make sure to explicitly
157 /// call this implementation.
158 bool doInitialization(Module &M);
160 /// doFinalization - Shut down the asmprinter. If you override this in your
161 /// pass, you must make sure to call it explicitly.
162 bool doFinalization(Module &M);
164 /// PrintSpecial - Print information related to the specified machine instr
165 /// that is independent of the operand, and may be independent of the instr
166 /// itself. This can be useful for portably encoding the comment character
167 /// or other bits of target-specific knowledge into the asmstrings. The
168 /// syntax used is ${:comment}. Targets can override this to add support
169 /// for their own strange codes.
170 virtual void PrintSpecial(const MachineInstr *MI, const char *Code);
172 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
173 /// instruction, using the specified assembler variant. Targets should
174 /// override this to format as appropriate. This method can return true if
175 /// the operand is erroneous.
176 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
177 unsigned AsmVariant, const char *ExtraCode);
179 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
180 /// instruction, using the specified assembler variant as an address.
181 /// Targets should override this to format as appropriate. This method can
182 /// return true if the operand is erroneous.
183 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
185 const char *ExtraCode);
187 /// getSectionForFunction - Return the section that we should emit the
188 /// specified function body into. This defaults to 'TextSection'. This
189 /// should most likely be overridden by the target to put linkonce/weak
190 /// functions into special sections.
191 virtual std::string getSectionForFunction(const Function &F) const;
193 /// SetupMachineFunction - This should be called when a new MachineFunction
194 /// is being processed from runOnMachineFunction.
195 void SetupMachineFunction(MachineFunction &MF);
197 /// getFunctionNumber - Return a unique ID for the current function.
199 unsigned getFunctionNumber() const { return FunctionNumber; }
201 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
202 /// not normally call this, as the counter is automatically bumped by
203 /// SetupMachineFunction.
204 void IncrementFunctionNumber() { FunctionNumber++; }
206 /// EmitConstantPool - Print to the current output stream assembly
207 /// representations of the constants in the constant pool MCP. This is
208 /// used to print out constants which have been "spilled to memory" by
209 /// the code generator.
211 void EmitConstantPool(MachineConstantPool *MCP);
213 /// EmitJumpTableInfo - Print assembly representations of the jump tables
214 /// used by the current function to the current output stream.
216 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
218 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
219 /// special global used by LLVM. If so, emit it and return true, otherwise
220 /// do nothing and return false.
221 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
224 //===------------------------------------------------------------------===//
225 /// LEB 128 number encoding.
227 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
228 /// representing an unsigned leb128 value.
229 void PrintULEB128(unsigned Value) const;
231 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
232 /// representing a signed leb128 value.
233 void PrintSLEB128(int Value) const;
235 //===------------------------------------------------------------------===//
236 // Emission and print routines
239 /// PrintHex - Print a value as a hexidecimal value.
241 void PrintHex(int Value) const;
243 /// EOL - Print a newline character to asm stream. If a comment is present
244 /// then it will be printed first. Comments should not contain '\n'.
246 void EOL(const std::string &Comment) const;
247 void EOL(const char* Comment) const;
249 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
250 /// unsigned leb128 value.
251 void EmitULEB128Bytes(unsigned Value) const;
253 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
254 /// signed leb128 value.
255 void EmitSLEB128Bytes(int Value) const;
257 /// EmitInt8 - Emit a byte directive and value.
259 void EmitInt8(int Value) const;
261 /// EmitInt16 - Emit a short directive and value.
263 void EmitInt16(int Value) const;
265 /// EmitInt32 - Emit a long directive and value.
267 void EmitInt32(int Value) const;
269 /// EmitInt64 - Emit a long long directive and value.
271 void EmitInt64(uint64_t Value) const;
273 /// EmitString - Emit a string with quotes and a null terminator.
274 /// Special characters are emitted properly.
275 /// @verbatim (Eg. '\t') @endverbatim
276 void EmitString(const std::string &String) const;
278 /// EmitFile - Emit a .file directive.
279 void EmitFile(unsigned Number, const std::string &Name) const;
281 //===------------------------------------------------------------------===//
283 /// EmitAlignment - Emit an alignment directive to the specified power of
284 /// two boundary. For example, if you pass in 3 here, you will get an 8
285 /// byte alignment. If a global value is specified, and if that global has
286 /// an explicit alignment requested, it will unconditionally override the
287 /// alignment request. However, if ForcedAlignBits is specified, this value
288 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
289 /// and the alignment computed with NumBits and the global. If UseFillExpr
290 /// is true, it also emits an optional second value FillValue which the
291 /// assembler uses to fill gaps to match alignment for text sections if the
292 /// has specified a non-zero fill value.
294 /// The algorithm is:
296 /// if (GV && GV->hasalignment) Align = GV->getalignment();
297 /// Align = std::max(Align, ForcedAlignBits);
299 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
300 unsigned ForcedAlignBits = 0,
301 bool UseFillExpr = true) const;
303 /// printLabel - This method prints a local label used by debug and
304 /// exception handling tables.
305 void printLabel(const MachineInstr *MI) const;
306 void printLabel(unsigned Id) const;
308 /// printDeclare - This method prints a local variable declaration used by
310 void printDeclare(const MachineInstr *MI) const;
313 /// EmitZeros - Emit a block of zeros.
315 void EmitZeros(uint64_t NumZeros) const;
317 /// EmitString - Emit a zero-byte-terminated string constant.
319 virtual void EmitString(const ConstantArray *CVA) const;
321 /// EmitConstantValueOnly - Print out the specified constant, without a
322 /// storage class. Only constants of first-class type are allowed here.
323 void EmitConstantValueOnly(const Constant *CV);
325 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
326 void EmitGlobalConstant(const Constant* CV);
328 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
330 /// printInlineAsm - This method formats and prints the specified machine
331 /// instruction that is an inline asm.
332 void printInlineAsm(const MachineInstr *MI) const;
334 /// printImplicitDef - This method prints the specified machine instruction
335 /// that is an implicit def.
336 virtual void printImplicitDef(const MachineInstr *MI) const;
338 /// printBasicBlockLabel - This method prints the label for the specified
339 /// MachineBasicBlock
340 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
341 bool printAlign = false,
342 bool printColon = false,
343 bool printComment = true) const;
345 /// printPICJumpTableSetLabel - This method prints a set label for the
346 /// specified MachineBasicBlock for a jumptable entry.
347 virtual void printPICJumpTableSetLabel(unsigned uid,
348 const MachineBasicBlock *MBB) const;
349 virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
350 const MachineBasicBlock *MBB) const;
351 virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
352 const MachineBasicBlock *MBB,
355 /// printDataDirective - This method prints the asm directive for the
357 void printDataDirective(const Type *type);
359 /// printSuffixedName - This prints a name with preceding
360 /// getPrivateGlobalPrefix and the specified suffix, handling quoted names
362 void printSuffixedName(const char *Name, const char *Suffix,
363 const char *Prefix = 0);
364 void printSuffixedName(const std::string &Name, const char* Suffix);
366 /// printVisibility - This prints visibility information about symbol, if
367 /// this is suported by the target.
368 void printVisibility(const std::string& Name, unsigned Visibility) const;
371 const GlobalValue *findGlobalValue(const Constant* CV);
372 void EmitLLVMUsedList(Constant *List);
373 void EmitXXStructorList(Constant *List);
374 void EmitConstantPool(unsigned Alignment, const char *Section,
375 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP);
376 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);