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