AsmPrinter.h revision af0e2726835e096e32c30c1b88cc7a6232a6ef69
1//===-- llvm/CodeGen/AsmPrinter.h - AsmPrinter Framework --------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 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 12// all asm writers. 13// 14//===----------------------------------------------------------------------===// 15 16#ifndef LLVM_CODEGEN_ASMPRINTER_H 17#define LLVM_CODEGEN_ASMPRINTER_H 18 19#include "llvm/CodeGen/MachineFunctionPass.h" 20#include "llvm/Support/DebugLoc.h" 21#include "llvm/Target/TargetMachine.h" 22#include "llvm/ADT/DenseMap.h" 23 24namespace llvm { 25 class GCStrategy; 26 class Constant; 27 class ConstantArray; 28 class ConstantFP; 29 class ConstantInt; 30 class ConstantStruct; 31 class ConstantVector; 32 class GCMetadataPrinter; 33 class GlobalValue; 34 class GlobalVariable; 35 class MachineBasicBlock; 36 class MachineFunction; 37 class MachineInstr; 38 class MachineLoopInfo; 39 class MachineLoop; 40 class MachineConstantPool; 41 class MachineConstantPoolEntry; 42 class MachineConstantPoolValue; 43 class MachineJumpTableInfo; 44 class MachineModuleInfo; 45 class MCInst; 46 class MCContext; 47 class MCSection; 48 class MCStreamer; 49 class MCSymbol; 50 class DwarfWriter; 51 class Mangler; 52 class MCAsmInfo; 53 class TargetLoweringObjectFile; 54 class Type; 55 class formatted_raw_ostream; 56 57 /// AsmPrinter - This class is intended to be used as a driving class for all 58 /// asm writers. 59 class AsmPrinter : public MachineFunctionPass { 60 static char ID; 61 62 /// FunctionNumber - This provides a unique ID for each function emitted in 63 /// this translation unit. It is autoincremented by SetupMachineFunction, 64 /// and can be accessed with getFunctionNumber() and 65 /// IncrementFunctionNumber(). 66 /// 67 unsigned FunctionNumber; 68 69 // GCMetadataPrinters - The garbage collection metadata printer table. 70 typedef DenseMap<GCStrategy*,GCMetadataPrinter*> gcp_map_type; 71 typedef gcp_map_type::iterator gcp_iterator; 72 gcp_map_type GCMetadataPrinters; 73 74 /// If VerboseAsm is set, a pointer to the loop info for this 75 /// function. 76 /// 77 MachineLoopInfo *LI; 78 79 public: 80 /// MMI - If available, this is a pointer to the current MachineModuleInfo. 81 MachineModuleInfo *MMI; 82 83 protected: 84 /// DW - If available, this is a pointer to the current dwarf writer. 85 DwarfWriter *DW; 86 87 public: 88 /// Output stream on which we're printing assembly code. 89 /// 90 formatted_raw_ostream &O; 91 92 /// Target machine description. 93 /// 94 TargetMachine &TM; 95 96 /// getObjFileLowering - Return information about object file lowering. 97 TargetLoweringObjectFile &getObjFileLowering() const; 98 99 /// Target Asm Printer information. 100 /// 101 const MCAsmInfo *MAI; 102 103 /// Target Register Information. 104 /// 105 const TargetRegisterInfo *TRI; 106 107 /// OutContext - This is the context for the output file that we are 108 /// streaming. This owns all of the global MC-related objects for the 109 /// generated translation unit. 110 MCContext &OutContext; 111 112 /// OutStreamer - This is the MCStreamer object for the file we are 113 /// generating. This contains the transient state for the current 114 /// translation unit that we are generating (such as the current section 115 /// etc). 116 MCStreamer &OutStreamer; 117 118 /// The current machine function. 119 const MachineFunction *MF; 120 121 /// Name-mangler for global names. 122 /// 123 Mangler *Mang; 124 125 /// Cache of mangled name for current function. This is recalculated at the 126 /// beginning of each call to runOnMachineFunction(). 127 /// 128 std::string CurrentFnName; 129 130 /// getCurrentSection() - Return the current section we are emitting to. 131 const MCSection *getCurrentSection() const; 132 133 134 /// VerboseAsm - Emit comments in assembly output if this is true. 135 /// 136 bool VerboseAsm; 137 138 /// Private state for PrintSpecial() 139 // Assign a unique ID to this machine instruction. 140 mutable const MachineInstr *LastMI; 141 mutable const Function *LastFn; 142 mutable unsigned Counter; 143 144 // Private state for processDebugLoc() 145 mutable DebugLocTuple PrevDLT; 146 147 protected: 148 explicit AsmPrinter(formatted_raw_ostream &o, TargetMachine &TM, 149 const MCAsmInfo *T, bool V); 150 151 public: 152 virtual ~AsmPrinter(); 153 154 /// isVerbose - Return true if assembly output should contain comments. 155 /// 156 bool isVerbose() const { return VerboseAsm; } 157 158 /// getFunctionNumber - Return a unique ID for the current function. 159 /// 160 unsigned getFunctionNumber() const { return FunctionNumber; } 161 162 protected: 163 /// getAnalysisUsage - Record analysis usage. 164 /// 165 void getAnalysisUsage(AnalysisUsage &AU) const; 166 167 /// doInitialization - Set up the AsmPrinter when we are working on a new 168 /// module. If your pass overrides this, it must make sure to explicitly 169 /// call this implementation. 170 bool doInitialization(Module &M); 171 172 /// EmitStartOfAsmFile - This virtual method can be overridden by targets 173 /// that want to emit something at the start of their file. 174 virtual void EmitStartOfAsmFile(Module &M) {} 175 176 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that 177 /// want to emit something at the end of their file. 178 virtual void EmitEndOfAsmFile(Module &M) {} 179 180 /// doFinalization - Shut down the asmprinter. If you override this in your 181 /// pass, you must make sure to call it explicitly. 182 bool doFinalization(Module &M); 183 184 /// PrintSpecial - Print information related to the specified machine instr 185 /// that is independent of the operand, and may be independent of the instr 186 /// itself. This can be useful for portably encoding the comment character 187 /// or other bits of target-specific knowledge into the asmstrings. The 188 /// syntax used is ${:comment}. Targets can override this to add support 189 /// for their own strange codes. 190 virtual void PrintSpecial(const MachineInstr *MI, const char *Code) const; 191 192 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM 193 /// instruction, using the specified assembler variant. Targets should 194 /// override this to format as appropriate. This method can return true if 195 /// the operand is erroneous. 196 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 197 unsigned AsmVariant, const char *ExtraCode); 198 199 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM 200 /// instruction, using the specified assembler variant as an address. 201 /// Targets should override this to format as appropriate. This method can 202 /// return true if the operand is erroneous. 203 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 204 unsigned AsmVariant, 205 const char *ExtraCode); 206 207 /// PrintGlobalVariable - Emit the specified global variable and its 208 /// initializer to the output stream. 209 virtual void PrintGlobalVariable(const GlobalVariable *GV) = 0; 210 211 /// SetupMachineFunction - This should be called when a new MachineFunction 212 /// is being processed from runOnMachineFunction. 213 void SetupMachineFunction(MachineFunction &MF); 214 215 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should 216 /// not normally call this, as the counter is automatically bumped by 217 /// SetupMachineFunction. 218 void IncrementFunctionNumber() { FunctionNumber++; } 219 220 /// EmitConstantPool - Print to the current output stream assembly 221 /// representations of the constants in the constant pool MCP. This is 222 /// used to print out constants which have been "spilled to memory" by 223 /// the code generator. 224 /// 225 void EmitConstantPool(MachineConstantPool *MCP); 226 227 /// EmitJumpTableInfo - Print assembly representations of the jump tables 228 /// used by the current function to the current output stream. 229 /// 230 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF); 231 232 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a 233 /// special global used by LLVM. If so, emit it and return true, otherwise 234 /// do nothing and return false. 235 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV); 236 237 public: 238 //===------------------------------------------------------------------===// 239 /// LEB 128 number encoding. 240 241 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas) 242 /// representing an unsigned leb128 value. 243 void PrintULEB128(unsigned Value) const; 244 245 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas) 246 /// representing a signed leb128 value. 247 void PrintSLEB128(int Value) const; 248 249 //===------------------------------------------------------------------===// 250 // Emission and print routines 251 // 252 253 /// PrintHex - Print a value as a hexidecimal value. 254 /// 255 void PrintHex(int Value) const; 256 257 /// EOL - Print a newline character to asm stream. If a comment is present 258 /// then it will be printed first. Comments should not contain '\n'. 259 void EOL() const; 260 void EOL(const std::string &Comment) const; 261 void EOL(const char* Comment) const; 262 void EOL(const char *Comment, unsigned Encoding) const; 263 264 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an 265 /// unsigned leb128 value. 266 void EmitULEB128Bytes(unsigned Value) const; 267 268 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a 269 /// signed leb128 value. 270 void EmitSLEB128Bytes(int Value) const; 271 272 /// EmitInt8 - Emit a byte directive and value. 273 /// 274 void EmitInt8(int Value) const; 275 276 /// EmitInt16 - Emit a short directive and value. 277 /// 278 void EmitInt16(int Value) const; 279 280 /// EmitInt32 - Emit a long directive and value. 281 /// 282 void EmitInt32(int Value) const; 283 284 /// EmitInt64 - Emit a long long directive and value. 285 /// 286 void EmitInt64(uint64_t Value) const; 287 288 /// EmitString - Emit a string with quotes and a null terminator. 289 /// Special characters are emitted properly. 290 /// @verbatim (Eg. '\t') @endverbatim 291 void EmitString(const std::string &String) const; 292 void EmitString(const char *String, unsigned Size) const; 293 294 /// EmitFile - Emit a .file directive. 295 void EmitFile(unsigned Number, const std::string &Name) const; 296 297 //===------------------------------------------------------------------===// 298 299 /// EmitAlignment - Emit an alignment directive to the specified power of 300 /// two boundary. For example, if you pass in 3 here, you will get an 8 301 /// byte alignment. If a global value is specified, and if that global has 302 /// an explicit alignment requested, it will unconditionally override the 303 /// alignment request. However, if ForcedAlignBits is specified, this value 304 /// has final say: the ultimate alignment will be the max of ForcedAlignBits 305 /// and the alignment computed with NumBits and the global. If UseFillExpr 306 /// is true, it also emits an optional second value FillValue which the 307 /// assembler uses to fill gaps to match alignment for text sections if the 308 /// has specified a non-zero fill value. 309 /// 310 /// The algorithm is: 311 /// Align = NumBits; 312 /// if (GV && GV->hasalignment) Align = GV->getalignment(); 313 /// Align = std::max(Align, ForcedAlignBits); 314 /// 315 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0, 316 unsigned ForcedAlignBits = 0, 317 bool UseFillExpr = true) const; 318 319 /// printLabel - This method prints a local label used by debug and 320 /// exception handling tables. 321 void printLabel(const MachineInstr *MI) const; 322 void printLabel(unsigned Id) const; 323 324 /// printDeclare - This method prints a local variable declaration used by 325 /// debug tables. 326 void printDeclare(const MachineInstr *MI) const; 327 328 /// EmitComments - Pretty-print comments for instructions 329 void EmitComments(const MachineInstr &MI) const; 330 /// EmitComments - Pretty-print comments for basic blocks 331 void EmitComments(const MachineBasicBlock &MBB) const; 332 333 /// GetMBBSymbol - Return the MCSymbol corresponding to the specified basic 334 /// block label. 335 MCSymbol *GetMBBSymbol(unsigned MBBID) const; 336 337 /// EmitBasicBlockStart - This method prints the label for the specified 338 /// MachineBasicBlock, an alignment (if present) and a comment describing 339 /// it if appropriate. 340 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const; 341 protected: 342 /// EmitZeros - Emit a block of zeros. 343 /// 344 void EmitZeros(uint64_t NumZeros, unsigned AddrSpace = 0) const; 345 346 /// EmitString - Emit a zero-byte-terminated string constant. 347 /// 348 virtual void EmitString(const ConstantArray *CVA) const; 349 350 /// EmitConstantValueOnly - Print out the specified constant, without a 351 /// storage class. Only constants of first-class type are allowed here. 352 void EmitConstantValueOnly(const Constant *CV); 353 354 /// EmitGlobalConstant - Print a general LLVM constant to the .s file. 355 void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0); 356 357 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV); 358 359 /// processDebugLoc - Processes the debug information of each machine 360 /// instruction's DebugLoc. 361 void processDebugLoc(const MachineInstr *MI, bool BeforePrintingInsn); 362 363 /// printInlineAsm - This method formats and prints the specified machine 364 /// instruction that is an inline asm. 365 void printInlineAsm(const MachineInstr *MI) const; 366 367 /// printImplicitDef - This method prints the specified machine instruction 368 /// that is an implicit def. 369 virtual void printImplicitDef(const MachineInstr *MI) const; 370 371 372 /// printPICJumpTableSetLabel - This method prints a set label for the 373 /// specified MachineBasicBlock for a jumptable entry. 374 virtual void printPICJumpTableSetLabel(unsigned uid, 375 const MachineBasicBlock *MBB) const; 376 virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2, 377 const MachineBasicBlock *MBB) const; 378 virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI, 379 const MachineBasicBlock *MBB, 380 unsigned uid) const; 381 382 /// printDataDirective - This method prints the asm directive for the 383 /// specified type. 384 void printDataDirective(const Type *type, unsigned AddrSpace = 0); 385 386 /// printVisibility - This prints visibility information about symbol, if 387 /// this is suported by the target. 388 void printVisibility(const std::string& Name, unsigned Visibility) const; 389 390 /// printOffset - This is just convenient handler for printing offsets. 391 void printOffset(int64_t Offset) const; 392 393 private: 394 void EmitLLVMUsedList(Constant *List); 395 void EmitXXStructorList(Constant *List); 396 void EmitGlobalConstantStruct(const ConstantStruct* CVS, 397 unsigned AddrSpace); 398 void EmitGlobalConstantArray(const ConstantArray* CVA, unsigned AddrSpace); 399 void EmitGlobalConstantVector(const ConstantVector* CP); 400 void EmitGlobalConstantFP(const ConstantFP* CFP, unsigned AddrSpace); 401 void EmitGlobalConstantLargeInt(const ConstantInt* CI, unsigned AddrSpace); 402 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C); 403 }; 404} 405 406#endif 407