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