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 // GCMetadataPrinters - The garbage collection metadata printer table.
55 typedef DenseMap<GCStrategy*,GCMetadataPrinter*> gcp_map_type;
56 typedef gcp_map_type::iterator gcp_iterator;
57 gcp_map_type GCMetadataPrinters;
60 /// DW -This is needed because printDeclare() has to insert
61 /// DbgVariable entries into the dwarf table. This is a short term hack
62 /// that ought be fixed soon.
65 // Necessary for external weak linkage support
66 std::set<const GlobalValue*> ExtWeakSymbols;
68 /// Fast - Generating code via fast instruction selection.
71 /// Output stream on which we're printing assembly code.
75 /// Target machine description.
79 /// Target Asm Printer information.
81 const TargetAsmInfo *TAI;
83 /// Target Register Information.
85 const TargetRegisterInfo *TRI;
87 /// The current machine function.
88 const MachineFunction *MF;
90 /// Name-mangler for global names.
94 /// Cache of mangled name for current function. This is recalculated at the
95 /// beginning of each call to runOnMachineFunction().
97 std::string CurrentFnName;
99 /// CurrentSection - The current section we are emitting to. This is
100 /// controlled and used by the SwitchSection method.
101 std::string CurrentSection;
102 const Section* CurrentSection_;
104 /// IsInTextSection - True if the current section we are emitting to is a
106 bool IsInTextSection;
108 /// VerboseAsm - Emit comments in assembly output if this is true.
113 AsmPrinter(raw_ostream &o, TargetMachine &TM,
114 const TargetAsmInfo *T, bool F, bool V);
117 virtual ~AsmPrinter();
119 /// isVerbose - Return true if assembly output should contain comments.
121 bool isVerbose() const { return VerboseAsm; }
123 /// SwitchToTextSection - Switch to the specified section of the executable
124 /// if we are not already in it! If GV is non-null and if the global has an
125 /// explicitly requested section, we switch to the section indicated for the
126 /// global instead of NewSection.
128 /// If the new section is an empty string, this method forgets what the
129 /// current section is, but does not emit a .section directive.
131 /// This method is used when about to emit executable code.
133 void SwitchToTextSection(const char *NewSection, const GlobalValue *GV = NULL);
135 /// SwitchToDataSection - Switch to the specified section of the executable
136 /// if we are not already in it! If GV is non-null and if the global has an
137 /// explicitly requested section, we switch to the section indicated for the
138 /// global instead of NewSection.
140 /// If the new section is an empty string, this method forgets what the
141 /// current section is, but does not emit a .section directive.
143 /// This method is used when about to emit data. For most assemblers, this
144 /// is the same as the SwitchToTextSection method, but not all assemblers
147 void SwitchToDataSection(const char *NewSection, const GlobalValue *GV = NULL);
149 /// SwitchToSection - Switch to the specified section of the executable if
150 /// we are not already in it!
151 void SwitchToSection(const Section* NS);
153 /// getGlobalLinkName - Returns the asm/link name of of the specified
154 /// global variable. Should be overridden by each target asm printer to
155 /// generate the appropriate value.
156 virtual const std::string getGlobalLinkName(const GlobalVariable *GV) const;
158 /// EmitExternalGlobal - Emit the external reference to a global variable.
159 /// Should be overridden if an indirect reference should be used.
160 virtual void EmitExternalGlobal(const GlobalVariable *GV);
162 /// getCurrentFunctionEHName - Called to return (and cache) the
165 std::string getCurrentFunctionEHName(const MachineFunction *MF);
168 /// getAnalysisUsage - Record analysis usage.
170 void getAnalysisUsage(AnalysisUsage &AU) const;
172 /// doInitialization - Set up the AsmPrinter when we are working on a new
173 /// module. If your pass overrides this, it must make sure to explicitly
174 /// call this implementation.
175 bool doInitialization(Module &M);
177 /// doFinalization - Shut down the asmprinter. If you override this in your
178 /// pass, you must make sure to call it explicitly.
179 bool doFinalization(Module &M);
181 /// PrintSpecial - Print information related to the specified machine instr
182 /// that is independent of the operand, and may be independent of the instr
183 /// itself. This can be useful for portably encoding the comment character
184 /// or other bits of target-specific knowledge into the asmstrings. The
185 /// syntax used is ${:comment}. Targets can override this to add support
186 /// for their own strange codes.
187 virtual void PrintSpecial(const MachineInstr *MI, const char *Code) const;
189 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
190 /// instruction, using the specified assembler variant. Targets should
191 /// override this to format as appropriate. This method can return true if
192 /// the operand is erroneous.
193 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
194 unsigned AsmVariant, const char *ExtraCode);
196 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
197 /// instruction, using the specified assembler variant as an address.
198 /// Targets should override this to format as appropriate. This method can
199 /// return true if the operand is erroneous.
200 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
202 const char *ExtraCode);
204 /// SetupMachineFunction - This should be called when a new MachineFunction
205 /// is being processed from runOnMachineFunction.
206 void SetupMachineFunction(MachineFunction &MF);
208 /// getFunctionNumber - Return a unique ID for the current function.
210 unsigned getFunctionNumber() const { return FunctionNumber; }
212 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
213 /// not normally call this, as the counter is automatically bumped by
214 /// SetupMachineFunction.
215 void IncrementFunctionNumber() { FunctionNumber++; }
217 /// EmitConstantPool - Print to the current output stream assembly
218 /// representations of the constants in the constant pool MCP. This is
219 /// used to print out constants which have been "spilled to memory" by
220 /// the code generator.
222 void EmitConstantPool(MachineConstantPool *MCP);
224 /// EmitJumpTableInfo - Print assembly representations of the jump tables
225 /// used by the current function to the current output stream.
227 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
229 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
230 /// special global used by LLVM. If so, emit it and return true, otherwise
231 /// do nothing and return false.
232 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
235 //===------------------------------------------------------------------===//
236 /// LEB 128 number encoding.
238 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
239 /// representing an unsigned leb128 value.
240 void PrintULEB128(unsigned Value) const;
242 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
243 /// representing a signed leb128 value.
244 void PrintSLEB128(int Value) const;
246 //===------------------------------------------------------------------===//
247 // Emission and print routines
250 /// PrintHex - Print a value as a hexidecimal value.
252 void PrintHex(int Value) const;
254 /// EOL - Print a newline character to asm stream. If a comment is present
255 /// then it will be printed first. Comments should not contain '\n'.
257 void EOL(const std::string &Comment) const;
258 void EOL(const char* Comment) const;
260 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
261 /// unsigned leb128 value.
262 void EmitULEB128Bytes(unsigned Value) const;
264 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
265 /// signed leb128 value.
266 void EmitSLEB128Bytes(int Value) const;
268 /// EmitInt8 - Emit a byte directive and value.
270 void EmitInt8(int Value) const;
272 /// EmitInt16 - Emit a short directive and value.
274 void EmitInt16(int Value) const;
276 /// EmitInt32 - Emit a long directive and value.
278 void EmitInt32(int Value) const;
280 /// EmitInt64 - Emit a long long directive and value.
282 void EmitInt64(uint64_t Value) const;
284 /// EmitString - Emit a string with quotes and a null terminator.
285 /// Special characters are emitted properly.
286 /// @verbatim (Eg. '\t') @endverbatim
287 void EmitString(const std::string &String) 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;
324 /// EmitZeros - Emit a block of zeros.
326 void EmitZeros(uint64_t NumZeros, unsigned AddrSpace = 0) const;
328 /// EmitString - Emit a zero-byte-terminated string constant.
330 virtual void EmitString(const ConstantArray *CVA) const;
332 /// EmitConstantValueOnly - Print out the specified constant, without a
333 /// storage class. Only constants of first-class type are allowed here.
334 void EmitConstantValueOnly(const Constant *CV);
336 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
337 void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
339 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
341 /// printInlineAsm - This method formats and prints the specified machine
342 /// instruction that is an inline asm.
343 void printInlineAsm(const MachineInstr *MI) const;
345 /// printImplicitDef - This method prints the specified machine instruction
346 /// that is an implicit def.
347 virtual void printImplicitDef(const MachineInstr *MI) const;
349 /// printBasicBlockLabel - This method prints the label for the specified
350 /// MachineBasicBlock
351 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
352 bool printAlign = false,
353 bool printColon = false,
354 bool printComment = true) const;
356 /// printPICJumpTableSetLabel - This method prints a set label for the
357 /// specified MachineBasicBlock for a jumptable entry.
358 virtual void printPICJumpTableSetLabel(unsigned uid,
359 const MachineBasicBlock *MBB) const;
360 virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
361 const MachineBasicBlock *MBB) const;
362 virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
363 const MachineBasicBlock *MBB,
366 /// printDataDirective - This method prints the asm directive for the
368 void printDataDirective(const Type *type, unsigned AddrSpace = 0);
370 /// printSuffixedName - This prints a name with preceding
371 /// getPrivateGlobalPrefix and the specified suffix, handling quoted names
373 void printSuffixedName(const char *Name, const char *Suffix,
374 const char *Prefix = 0);
375 void printSuffixedName(const std::string &Name, const char* Suffix);
377 /// printVisibility - This prints visibility information about symbol, if
378 /// this is suported by the target.
379 void printVisibility(const std::string& Name, unsigned Visibility) const;
381 /// printOffset - This is just convenient handler for printing offsets.
382 void printOffset(int64_t Offset) const;
385 const GlobalValue *findGlobalValue(const Constant* CV);
386 void EmitLLVMUsedList(Constant *List);
387 void EmitXXStructorList(Constant *List);
388 void EmitGlobalConstantStruct(const ConstantStruct* CVS,
390 void EmitGlobalConstantArray(const ConstantArray* CVA);
391 void EmitGlobalConstantVector(const ConstantVector* CP);
392 void EmitGlobalConstantFP(const ConstantFP* CFP, unsigned AddrSpace);
393 void EmitGlobalConstantLargeInt(const ConstantInt* CI, unsigned AddrSpace);
394 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);