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/Target/TargetMachine.h"
21 #include "llvm/ADT/DenseMap.h"
30 class GCMetadataPrinter;
32 class MachineConstantPoolEntry;
33 class MachineConstantPoolValue;
34 class MachineModuleInfo;
42 class TargetLoweringObjectFile;
44 class formatted_raw_ostream;
46 /// AsmPrinter - This class is intended to be used as a driving class for all
48 class AsmPrinter : public MachineFunctionPass {
51 /// FunctionNumber - This provides a unique ID for each function emitted in
52 /// this translation unit. It is autoincremented by SetupMachineFunction,
53 /// and can be accessed with getFunctionNumber() and
54 /// IncrementFunctionNumber().
56 unsigned FunctionNumber;
58 // GCMetadataPrinters - The garbage collection metadata printer table.
59 typedef DenseMap<GCStrategy*,GCMetadataPrinter*> gcp_map_type;
60 typedef gcp_map_type::iterator gcp_iterator;
61 gcp_map_type GCMetadataPrinters;
64 /// MMI - If available, this is a pointer to the current MachineModuleInfo.
65 MachineModuleInfo *MMI;
67 /// DW - If available, this is a pointer to the current dwarf writer.
71 /// Output stream on which we're printing assembly code.
73 formatted_raw_ostream &O;
75 /// Target machine description.
79 /// getObjFileLowering - Return information about object file lowering.
80 const TargetLoweringObjectFile &getObjFileLowering() const;
82 /// Target Asm Printer information.
84 const TargetAsmInfo *TAI;
86 /// Target Register Information.
88 const TargetRegisterInfo *TRI;
90 /// OutContext - This is the context for the output file that we are
91 /// streaming. This owns all of the global MC-related objects for the
92 /// generated translation unit.
93 MCContext &OutContext;
95 /// OutStreamer - This is the MCStreamer object for the file we are
96 /// generating. This contains the transient state for the current
97 /// translation unit that we are generating (such as the current section
99 MCStreamer &OutStreamer;
101 /// The current machine function.
102 const MachineFunction *MF;
104 /// Name-mangler for global names.
108 /// Cache of mangled name for current function. This is recalculated at the
109 /// beginning of each call to runOnMachineFunction().
111 std::string CurrentFnName;
113 /// CurrentSection - The current section we are emitting to. This is
114 /// controlled and used by the SwitchSection method.
115 std::string CurrentSection;
116 const MCSection *CurrentSection_;
118 /// IsInTextSection - True if the current section we are emitting to is a
120 bool IsInTextSection;
122 /// VerboseAsm - Emit comments in assembly output if this is true.
126 /// Private state for PrintSpecial()
127 // Assign a unique ID to this machine instruction.
128 mutable const MachineInstr *LastMI;
129 mutable const Function *LastFn;
130 mutable unsigned Counter;
132 // Private state for processDebugLock()
133 mutable DebugLocTuple PrevDLT;
136 explicit AsmPrinter(formatted_raw_ostream &o, TargetMachine &TM,
137 const TargetAsmInfo *T, bool V);
140 virtual ~AsmPrinter();
142 /// isVerbose - Return true if assembly output should contain comments.
144 bool isVerbose() const { return VerboseAsm; }
146 /// SwitchToTextSection - Switch to the specified section of the executable
147 /// if we are not already in it! If GV is non-null and if the global has an
148 /// explicitly requested section, we switch to the section indicated for the
149 /// global instead of NewSection.
151 /// If the new section is an empty string, this method forgets what the
152 /// current section is, but does not emit a .section directive.
154 /// This method is used when about to emit executable code.
156 void SwitchToTextSection(const char *NewSection,
157 const GlobalValue *GV = NULL);
159 /// SwitchToDataSection - Switch to the specified section of the executable
160 /// if we are not already in it! If GV is non-null and if the global has an
161 /// explicitly requested section, we switch to the section indicated for the
162 /// global instead of NewSection.
164 /// If the new section is an empty string, this method forgets what the
165 /// current section is, but does not emit a .section directive.
167 /// This method is used when about to emit data. For most assemblers, this
168 /// is the same as the SwitchToTextSection method, but not all assemblers
171 void SwitchToDataSection(const char *NewSection,
172 const GlobalValue *GV = NULL);
174 /// SwitchToSection - Switch to the specified section of the executable if
175 /// we are not already in it!
176 void SwitchToSection(const MCSection *NS);
178 /// getGlobalLinkName - Returns the asm/link name of of the specified
179 /// global variable. Should be overridden by each target asm printer to
180 /// generate the appropriate value.
181 virtual const std::string &getGlobalLinkName(const GlobalVariable *GV,
182 std::string &LinkName) const;
184 /// EmitExternalGlobal - Emit the external reference to a global variable.
185 /// Should be overridden if an indirect reference should be used.
186 virtual void EmitExternalGlobal(const GlobalVariable *GV);
188 /// getCurrentFunctionEHName - Called to return the CurrentFnEHName.
190 std::string getCurrentFunctionEHName(const MachineFunction *MF) const;
193 /// getAnalysisUsage - Record analysis usage.
195 void getAnalysisUsage(AnalysisUsage &AU) const;
197 /// doInitialization - Set up the AsmPrinter when we are working on a new
198 /// module. If your pass overrides this, it must make sure to explicitly
199 /// call this implementation.
200 bool doInitialization(Module &M);
202 /// doFinalization - Shut down the asmprinter. If you override this in your
203 /// pass, you must make sure to call it explicitly.
204 bool doFinalization(Module &M);
206 /// PrintSpecial - Print information related to the specified machine instr
207 /// that is independent of the operand, and may be independent of the instr
208 /// itself. This can be useful for portably encoding the comment character
209 /// or other bits of target-specific knowledge into the asmstrings. The
210 /// syntax used is ${:comment}. Targets can override this to add support
211 /// for their own strange codes.
212 virtual void PrintSpecial(const MachineInstr *MI, const char *Code) const;
214 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
215 /// instruction, using the specified assembler variant. Targets should
216 /// override this to format as appropriate. This method can return true if
217 /// the operand is erroneous.
218 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
219 unsigned AsmVariant, const char *ExtraCode);
221 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
222 /// instruction, using the specified assembler variant as an address.
223 /// Targets should override this to format as appropriate. This method can
224 /// return true if the operand is erroneous.
225 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
227 const char *ExtraCode);
230 /// PrintGlobalVariable - Emit the specified global variable and its
231 /// initializer to the output stream.
232 virtual void PrintGlobalVariable(const GlobalVariable *GV) = 0;
234 /// SetupMachineFunction - This should be called when a new MachineFunction
235 /// is being processed from runOnMachineFunction.
236 void SetupMachineFunction(MachineFunction &MF);
238 /// getFunctionNumber - Return a unique ID for the current function.
240 unsigned getFunctionNumber() const { return FunctionNumber; }
242 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should
243 /// not normally call this, as the counter is automatically bumped by
244 /// SetupMachineFunction.
245 void IncrementFunctionNumber() { FunctionNumber++; }
247 /// EmitConstantPool - Print to the current output stream assembly
248 /// representations of the constants in the constant pool MCP. This is
249 /// used to print out constants which have been "spilled to memory" by
250 /// the code generator.
252 void EmitConstantPool(MachineConstantPool *MCP);
254 /// EmitJumpTableInfo - Print assembly representations of the jump tables
255 /// used by the current function to the current output stream.
257 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF);
259 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
260 /// special global used by LLVM. If so, emit it and return true, otherwise
261 /// do nothing and return false.
262 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
265 //===------------------------------------------------------------------===//
266 /// LEB 128 number encoding.
268 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas)
269 /// representing an unsigned leb128 value.
270 void PrintULEB128(unsigned Value) const;
272 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas)
273 /// representing a signed leb128 value.
274 void PrintSLEB128(int Value) const;
276 //===------------------------------------------------------------------===//
277 // Emission and print routines
280 /// PrintHex - Print a value as a hexidecimal value.
282 void PrintHex(int Value) const;
284 /// EOL - Print a newline character to asm stream. If a comment is present
285 /// then it will be printed first. Comments should not contain '\n'.
287 void EOL(const std::string &Comment) const;
288 void EOL(const char* Comment) const;
290 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
291 /// unsigned leb128 value.
292 void EmitULEB128Bytes(unsigned Value) const;
294 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
295 /// signed leb128 value.
296 void EmitSLEB128Bytes(int Value) const;
298 /// EmitInt8 - Emit a byte directive and value.
300 void EmitInt8(int Value) const;
302 /// EmitInt16 - Emit a short directive and value.
304 void EmitInt16(int Value) const;
306 /// EmitInt32 - Emit a long directive and value.
308 void EmitInt32(int Value) const;
310 /// EmitInt64 - Emit a long long directive and value.
312 void EmitInt64(uint64_t Value) const;
314 /// EmitString - Emit a string with quotes and a null terminator.
315 /// Special characters are emitted properly.
316 /// @verbatim (Eg. '\t') @endverbatim
317 void EmitString(const std::string &String) const;
318 void EmitString(const char *String, unsigned Size) const;
320 /// EmitFile - Emit a .file directive.
321 void EmitFile(unsigned Number, const std::string &Name) const;
323 //===------------------------------------------------------------------===//
325 /// EmitAlignment - Emit an alignment directive to the specified power of
326 /// two boundary. For example, if you pass in 3 here, you will get an 8
327 /// byte alignment. If a global value is specified, and if that global has
328 /// an explicit alignment requested, it will unconditionally override the
329 /// alignment request. However, if ForcedAlignBits is specified, this value
330 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
331 /// and the alignment computed with NumBits and the global. If UseFillExpr
332 /// is true, it also emits an optional second value FillValue which the
333 /// assembler uses to fill gaps to match alignment for text sections if the
334 /// has specified a non-zero fill value.
336 /// The algorithm is:
338 /// if (GV && GV->hasalignment) Align = GV->getalignment();
339 /// Align = std::max(Align, ForcedAlignBits);
341 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
342 unsigned ForcedAlignBits = 0,
343 bool UseFillExpr = true) const;
345 /// printLabel - This method prints a local label used by debug and
346 /// exception handling tables.
347 void printLabel(const MachineInstr *MI) const;
348 void printLabel(unsigned Id) const;
350 /// printDeclare - This method prints a local variable declaration used by
352 void printDeclare(const MachineInstr *MI) const;
354 /// EmitComments - Pretty-print comments for instructions
355 void EmitComments(const MachineInstr &MI) const;
356 /// EmitComments - Pretty-print comments for instructions
357 void EmitComments(const MCInst &MI) const;
360 /// EmitZeros - Emit a block of zeros.
362 void EmitZeros(uint64_t NumZeros, unsigned AddrSpace = 0) const;
364 /// EmitString - Emit a zero-byte-terminated string constant.
366 virtual void EmitString(const ConstantArray *CVA) const;
368 /// EmitConstantValueOnly - Print out the specified constant, without a
369 /// storage class. Only constants of first-class type are allowed here.
370 void EmitConstantValueOnly(const Constant *CV);
372 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
373 void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0);
375 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
377 /// processDebugLoc - Processes the debug information of each machine
378 /// instruction's DebugLoc.
379 void processDebugLoc(DebugLoc DL);
381 /// printInlineAsm - This method formats and prints the specified machine
382 /// instruction that is an inline asm.
383 void printInlineAsm(const MachineInstr *MI) const;
385 /// printImplicitDef - This method prints the specified machine instruction
386 /// that is an implicit def.
387 virtual void printImplicitDef(const MachineInstr *MI) const;
389 /// printBasicBlockLabel - This method prints the label for the specified
390 /// MachineBasicBlock
391 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
392 bool printAlign = false,
393 bool printColon = false,
394 bool printComment = true) const;
396 /// printPICJumpTableSetLabel - This method prints a set label for the
397 /// specified MachineBasicBlock for a jumptable entry.
398 virtual void printPICJumpTableSetLabel(unsigned uid,
399 const MachineBasicBlock *MBB) const;
400 virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
401 const MachineBasicBlock *MBB) const;
402 virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
403 const MachineBasicBlock *MBB,
406 /// printDataDirective - This method prints the asm directive for the
408 void printDataDirective(const Type *type, unsigned AddrSpace = 0);
410 /// printVisibility - This prints visibility information about symbol, if
411 /// this is suported by the target.
412 void printVisibility(const std::string& Name, unsigned Visibility) const;
414 /// printOffset - This is just convenient handler for printing offsets.
415 void printOffset(int64_t Offset) const;
418 void EmitLLVMUsedList(Constant *List);
419 void EmitXXStructorList(Constant *List);
420 void EmitGlobalConstantStruct(const ConstantStruct* CVS,
422 void EmitGlobalConstantArray(const ConstantArray* CVA, unsigned AddrSpace);
423 void EmitGlobalConstantVector(const ConstantVector* CP);
424 void EmitGlobalConstantFP(const ConstantFP* CFP, unsigned AddrSpace);
425 void EmitGlobalConstantLargeInt(const ConstantInt* CI, unsigned AddrSpace);
426 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);