AsmPrinter.h revision 9d1c1ada213c80135fbdda704175aae689daa6f9
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 22namespace llvm { 23 class BlockAddress; 24 class GCStrategy; 25 class Constant; 26 class ConstantArray; 27 class ConstantFP; 28 class ConstantInt; 29 class ConstantStruct; 30 class ConstantVector; 31 class GCMetadataPrinter; 32 class GlobalValue; 33 class GlobalVariable; 34 class MachineBasicBlock; 35 class MachineFunction; 36 class MachineInstr; 37 class MachineLoopInfo; 38 class MachineLoop; 39 class MachineConstantPool; 40 class MachineConstantPoolEntry; 41 class MachineConstantPoolValue; 42 class MachineJumpTableInfo; 43 class MachineModuleInfo; 44 class MCInst; 45 class MCContext; 46 class MCSection; 47 class MCStreamer; 48 class MCSymbol; 49 class DwarfWriter; 50 class Mangler; 51 class MCAsmInfo; 52 class TargetLoweringObjectFile; 53 class Twine; 54 class Type; 55 56 /// AsmPrinter - This class is intended to be used as a driving class for all 57 /// asm writers. 58 class AsmPrinter : public MachineFunctionPass { 59 static char ID; 60 61 /// If VerboseAsm is set, a pointer to the loop info for this 62 /// function. 63 /// 64 MachineLoopInfo *LI; 65 public: 66 /// MMI - This is a pointer to the current MachineModuleInfo. 67 MachineModuleInfo *MMI; 68 69 protected: 70 /// DW - If available, this is a pointer to the current dwarf writer. 71 DwarfWriter *DW; 72 73 public: 74 75 /// Target machine description. 76 /// 77 TargetMachine &TM; 78 79 /// getObjFileLowering - Return information about object file lowering. 80 TargetLoweringObjectFile &getObjFileLowering() const; 81 82 /// Target Asm Printer information. 83 /// 84 const MCAsmInfo *MAI; 85 86 /// OutContext - This is the context for the output file that we are 87 /// streaming. This owns all of the global MC-related objects for the 88 /// generated translation unit. 89 MCContext &OutContext; 90 91 /// OutStreamer - This is the MCStreamer object for the file we are 92 /// generating. This contains the transient state for the current 93 /// translation unit that we are generating (such as the current section 94 /// etc). 95 MCStreamer &OutStreamer; 96 97 /// The current machine function. 98 const MachineFunction *MF; 99 100 /// Name-mangler for global names. 101 /// 102 Mangler *Mang; 103 104 /// The symbol for the current function. This is recalculated at the 105 /// beginning of each call to runOnMachineFunction(). 106 /// 107 MCSymbol *CurrentFnSym; 108 109 /// getCurrentSection() - Return the current section we are emitting to. 110 const MCSection *getCurrentSection() const; 111 112 113 /// VerboseAsm - Emit comments in assembly output if this is true. 114 /// 115 bool VerboseAsm; 116 117 118 private: 119 // GCMetadataPrinters - The garbage collection metadata printer table. 120 void *GCMetadataPrinters; // Really a DenseMap. 121 122 /// Private state for PrintSpecial() 123 // Assign a unique ID to this machine instruction. 124 mutable const MachineInstr *LastMI; 125 mutable const Function *LastFn; 126 mutable unsigned Counter; 127 mutable unsigned SetCounter; 128 129 protected: 130 explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer); 131 132 public: 133 virtual ~AsmPrinter(); 134 135 /// isVerbose - Return true if assembly output should contain comments. 136 /// 137 bool isVerbose() const { return VerboseAsm; } 138 139 /// getFunctionNumber - Return a unique ID for the current function. 140 /// 141 unsigned getFunctionNumber() const; 142 143 protected: 144 /// getAnalysisUsage - Record analysis usage. 145 /// 146 void getAnalysisUsage(AnalysisUsage &AU) const; 147 148 /// doInitialization - Set up the AsmPrinter when we are working on a new 149 /// module. If your pass overrides this, it must make sure to explicitly 150 /// call this implementation. 151 bool doInitialization(Module &M); 152 153 /// EmitStartOfAsmFile - This virtual method can be overridden by targets 154 /// that want to emit something at the start of their file. 155 virtual void EmitStartOfAsmFile(Module &) {} 156 157 /// EmitEndOfAsmFile - This virtual method can be overridden by targets that 158 /// want to emit something at the end of their file. 159 virtual void EmitEndOfAsmFile(Module &) {} 160 161 /// doFinalization - Shut down the asmprinter. If you override this in your 162 /// pass, you must make sure to call it explicitly. 163 bool doFinalization(Module &M); 164 165 /// PrintSpecial - Print information related to the specified machine instr 166 /// that is independent of the operand, and may be independent of the instr 167 /// itself. This can be useful for portably encoding the comment character 168 /// or other bits of target-specific knowledge into the asmstrings. The 169 /// syntax used is ${:comment}. Targets can override this to add support 170 /// for their own strange codes. 171 virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS, 172 const char *Code) const; 173 174 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM 175 /// instruction, using the specified assembler variant. Targets should 176 /// override this to format as appropriate. This method can return true if 177 /// the operand is erroneous. 178 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 179 unsigned AsmVariant, const char *ExtraCode, 180 raw_ostream &OS); 181 182 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM 183 /// instruction, using the specified assembler variant as an address. 184 /// Targets should override this to format as appropriate. This method can 185 /// return true if the operand is erroneous. 186 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 187 unsigned AsmVariant, 188 const char *ExtraCode, 189 raw_ostream &OS); 190 191 /// runOnMachineFunction - Emit the specified function out to the 192 /// OutStreamer. 193 virtual bool runOnMachineFunction(MachineFunction &MF) { 194 SetupMachineFunction(MF); 195 EmitFunctionHeader(); 196 EmitFunctionBody(); 197 return false; 198 } 199 200 /// SetupMachineFunction - This should be called when a new MachineFunction 201 /// is being processed from runOnMachineFunction. 202 void SetupMachineFunction(MachineFunction &MF); 203 204 /// EmitFunctionHeader - This method emits the header for the current 205 /// function. 206 void EmitFunctionHeader(); 207 208 /// EmitFunctionBody - This method emits the body and trailer for a 209 /// function. 210 void EmitFunctionBody(); 211 212 /// EmitInstruction - Targets should implement this to emit instructions. 213 virtual void EmitInstruction(const MachineInstr *) { 214 assert(0 && "EmitInstruction not implemented"); 215 } 216 217 /// EmitFunctionBodyStart - Targets can override this to emit stuff before 218 /// the first basic block in the function. 219 virtual void EmitFunctionBodyStart() {} 220 221 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after 222 /// the last basic block in the function. 223 virtual void EmitFunctionBodyEnd() {} 224 225 /// EmitConstantPool - Print to the current output stream assembly 226 /// representations of the constants in the constant pool MCP. This is 227 /// used to print out constants which have been "spilled to memory" by 228 /// the code generator. 229 /// 230 virtual void EmitConstantPool(); 231 232 /// EmitJumpTableInfo - Print assembly representations of the jump tables 233 /// used by the current function to the current output stream. 234 /// 235 void EmitJumpTableInfo(); 236 237 /// EmitGlobalVariable - Emit the specified global variable to the .s file. 238 virtual void EmitGlobalVariable(const GlobalVariable *GV); 239 240 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a 241 /// special global used by LLVM. If so, emit it and return true, otherwise 242 /// do nothing and return false. 243 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV); 244 245 public: 246 //===------------------------------------------------------------------===// 247 // Emission and print routines 248 // 249 250 /// EmitInt8 - Emit a byte directive and value. 251 /// 252 void EmitInt8(int Value) const; 253 254 /// EmitInt16 - Emit a short directive and value. 255 /// 256 void EmitInt16(int Value) const; 257 258 /// EmitInt32 - Emit a long directive and value. 259 /// 260 void EmitInt32(int Value) const; 261 262 /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size 263 /// in bytes of the directive is specified by Size and Hi/Lo specify the 264 /// labels. This implicitly uses .set if it is available. 265 void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo, 266 unsigned Size) const; 267 268 //===------------------------------------------------------------------===// 269 270 /// EmitAlignment - Emit an alignment directive to the specified power of 271 /// two boundary. For example, if you pass in 3 here, you will get an 8 272 /// byte alignment. If a global value is specified, and if that global has 273 /// an explicit alignment requested, it will unconditionally override the 274 /// alignment request. However, if ForcedAlignBits is specified, this value 275 /// has final say: the ultimate alignment will be the max of ForcedAlignBits 276 /// and the alignment computed with NumBits and the global. If UseFillExpr 277 /// is true, it also emits an optional second value FillValue which the 278 /// assembler uses to fill gaps to match alignment for text sections if the 279 /// has specified a non-zero fill value. 280 /// 281 /// The algorithm is: 282 /// Align = NumBits; 283 /// if (GV && GV->hasalignment) Align = GV->getalignment(); 284 /// Align = std::max(Align, ForcedAlignBits); 285 /// 286 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0, 287 unsigned ForcedAlignBits = 0, 288 bool UseFillExpr = true) const; 289 290 /// printDeclare - This method prints a local variable declaration used by 291 /// debug tables. 292 void printDeclare(const MachineInstr *MI) const; 293 294 /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with 295 /// global value name as its base, with the specified suffix, and where the 296 /// symbol is forced to have private linkage if ForcePrivate is true. 297 MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV, 298 StringRef Suffix, 299 bool ForcePrivate = true) const; 300 301 /// GetExternalSymbolSymbol - Return the MCSymbol for the specified 302 /// ExternalSymbol. 303 MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const; 304 305 /// GetCPISymbol - Return the symbol for the specified constant pool entry. 306 MCSymbol *GetCPISymbol(unsigned CPID) const; 307 308 /// GetJTISymbol - Return the symbol for the specified jump table entry. 309 MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const; 310 311 /// GetJTSetSymbol - Return the symbol for the specified jump table .set 312 /// FIXME: privatize to AsmPrinter. 313 MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const; 314 315 /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress 316 /// uses of the specified basic block. 317 MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const; 318 MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const; 319 320 /// EmitBasicBlockStart - This method prints the label for the specified 321 /// MachineBasicBlock, an alignment (if present) and a comment describing 322 /// it if appropriate. 323 void EmitBasicBlockStart(const MachineBasicBlock *MBB) const; 324 325 326 // Data emission. 327 328 /// EmitGlobalConstant - Print a general LLVM constant to the .s file. 329 void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0); 330 331 protected: 332 virtual void EmitFunctionEntryLabel(); 333 334 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV); 335 336 /// printOffset - This is just convenient handler for printing offsets. 337 void printOffset(int64_t Offset, raw_ostream &OS) const; 338 339 /// isBlockOnlyReachableByFallthough - Return true if the basic block has 340 /// exactly one predecessor and the control transfer mechanism between 341 /// the predecessor and this block is a fall-through. 342 virtual bool 343 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const; 344 345 private: 346 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer. 347 void EmitInlineAsm(StringRef Str) const; 348 349 /// processDebugLoc - Processes the debug information of each machine 350 /// instruction's DebugLoc. 351 void processDebugLoc(const MachineInstr *MI, bool BeforePrintingInsn); 352 353 void printLabelInst(const MachineInstr *MI) const; 354 355 /// printInlineAsm - This method formats and prints the specified machine 356 /// instruction that is an inline asm. 357 void printInlineAsm(const MachineInstr *MI) const; 358 359 /// printImplicitDef - This method prints the specified machine instruction 360 /// that is an implicit def. 361 void printImplicitDef(const MachineInstr *MI) const; 362 363 /// printKill - This method prints the specified kill machine instruction. 364 void printKill(const MachineInstr *MI) const; 365 366 /// EmitVisibility - This emits visibility information about symbol, if 367 /// this is suported by the target. 368 void EmitVisibility(MCSymbol *Sym, unsigned Visibility) const; 369 370 void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const; 371 372 void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI, 373 const MachineBasicBlock *MBB, 374 unsigned uid) const; 375 void EmitLLVMUsedList(Constant *List); 376 void EmitXXStructorList(Constant *List); 377 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C); 378 }; 379} 380 381#endif 382