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