LLVM API Documentation
00001 //===-- llvm/CodeGen/AsmPrinter.h - AsmPrinter Framework --------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains a class to be used as the base class for target specific 00011 // asm writers. This class primarily handles common functionality used by 00012 // all asm writers. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #ifndef LLVM_CODEGEN_ASMPRINTER_H 00017 #define LLVM_CODEGEN_ASMPRINTER_H 00018 00019 #include "llvm/CodeGen/MachineFunctionPass.h" 00020 #include "llvm/Support/DebugLoc.h" 00021 #include "llvm/Target/TargetMachine.h" 00022 #include "llvm/ADT/DenseMap.h" 00023 00024 namespace llvm { 00025 class GCStrategy; 00026 class Constant; 00027 class ConstantArray; 00028 class ConstantFP; 00029 class ConstantInt; 00030 class ConstantStruct; 00031 class ConstantVector; 00032 class GCMetadataPrinter; 00033 class GlobalValue; 00034 class GlobalVariable; 00035 class MachineBasicBlock; 00036 class MachineFunction; 00037 class MachineInstr; 00038 class MachineLoopInfo; 00039 class MachineLoop; 00040 class MachineConstantPool; 00041 class MachineConstantPoolEntry; 00042 class MachineConstantPoolValue; 00043 class MachineJumpTableInfo; 00044 class MachineModuleInfo; 00045 class MCInst; 00046 class MCContext; 00047 class MCSection; 00048 class MCStreamer; 00049 class DwarfWriter; 00050 class Mangler; 00051 class TargetAsmInfo; 00052 class TargetLoweringObjectFile; 00053 class Type; 00054 class formatted_raw_ostream; 00055 00056 /// AsmPrinter - This class is intended to be used as a driving class for all 00057 /// asm writers. 00058 class AsmPrinter : public MachineFunctionPass { 00059 static char ID; 00060 00061 /// FunctionNumber - This provides a unique ID for each function emitted in 00062 /// this translation unit. It is autoincremented by SetupMachineFunction, 00063 /// and can be accessed with getFunctionNumber() and 00064 /// IncrementFunctionNumber(). 00065 /// 00066 unsigned FunctionNumber; 00067 00068 // GCMetadataPrinters - The garbage collection metadata printer table. 00069 typedef DenseMap<GCStrategy*,GCMetadataPrinter*> gcp_map_type; 00070 typedef gcp_map_type::iterator gcp_iterator; 00071 gcp_map_type GCMetadataPrinters; 00072 00073 /// If VerboseAsm is set, a pointer to the loop info for this 00074 /// function. 00075 /// 00076 MachineLoopInfo *LI; 00077 00078 protected: 00079 /// MMI - If available, this is a pointer to the current MachineModuleInfo. 00080 MachineModuleInfo *MMI; 00081 00082 /// DW - If available, this is a pointer to the current dwarf writer. 00083 DwarfWriter *DW; 00084 00085 public: 00086 /// Output stream on which we're printing assembly code. 00087 /// 00088 formatted_raw_ostream &O; 00089 00090 /// Target machine description. 00091 /// 00092 TargetMachine &TM; 00093 00094 /// getObjFileLowering - Return information about object file lowering. 00095 TargetLoweringObjectFile &getObjFileLowering() const; 00096 00097 /// Target Asm Printer information. 00098 /// 00099 const TargetAsmInfo *TAI; 00100 00101 /// Target Register Information. 00102 /// 00103 const TargetRegisterInfo *TRI; 00104 00105 /// OutContext - This is the context for the output file that we are 00106 /// streaming. This owns all of the global MC-related objects for the 00107 /// generated translation unit. 00108 MCContext &OutContext; 00109 00110 /// OutStreamer - This is the MCStreamer object for the file we are 00111 /// generating. This contains the transient state for the current 00112 /// translation unit that we are generating (such as the current section 00113 /// etc). 00114 MCStreamer &OutStreamer; 00115 00116 /// The current machine function. 00117 const MachineFunction *MF; 00118 00119 /// Name-mangler for global names. 00120 /// 00121 Mangler *Mang; 00122 00123 /// Cache of mangled name for current function. This is recalculated at the 00124 /// beginning of each call to runOnMachineFunction(). 00125 /// 00126 std::string CurrentFnName; 00127 00128 /// getCurrentSection() - Return the current section we are emitting to. 00129 const MCSection *getCurrentSection() const; 00130 00131 00132 /// VerboseAsm - Emit comments in assembly output if this is true. 00133 /// 00134 bool VerboseAsm; 00135 00136 /// ExuberantAsm - Emit many more comments in assembly output if 00137 /// this is true. 00138 /// 00139 bool ExuberantAsm; 00140 00141 /// Private state for PrintSpecial() 00142 // Assign a unique ID to this machine instruction. 00143 mutable const MachineInstr *LastMI; 00144 mutable const Function *LastFn; 00145 mutable unsigned Counter; 00146 00147 // Private state for processDebugLock() 00148 mutable DebugLocTuple PrevDLT; 00149 00150 protected: 00151 explicit AsmPrinter(formatted_raw_ostream &o, TargetMachine &TM, 00152 const TargetAsmInfo *T, bool V); 00153 00154 public: 00155 virtual ~AsmPrinter(); 00156 00157 /// isVerbose - Return true if assembly output should contain comments. 00158 /// 00159 bool isVerbose() const { return VerboseAsm; } 00160 00161 /// getGlobalLinkName - Returns the asm/link name of of the specified 00162 /// global variable. Should be overridden by each target asm printer to 00163 /// generate the appropriate value. 00164 virtual const std::string &getGlobalLinkName(const GlobalVariable *GV, 00165 std::string &LinkName) const; 00166 00167 /// EmitExternalGlobal - Emit the external reference to a global variable. 00168 /// Should be overridden if an indirect reference should be used. 00169 virtual void EmitExternalGlobal(const GlobalVariable *GV); 00170 00171 /// getCurrentFunctionEHName - Called to return the CurrentFnEHName. 00172 /// 00173 std::string getCurrentFunctionEHName(const MachineFunction *MF) const; 00174 00175 protected: 00176 /// getAnalysisUsage - Record analysis usage. 00177 /// 00178 void getAnalysisUsage(AnalysisUsage &AU) const; 00179 00180 /// doInitialization - Set up the AsmPrinter when we are working on a new 00181 /// module. If your pass overrides this, it must make sure to explicitly 00182 /// call this implementation. 00183 bool doInitialization(Module &M); 00184 00185 /// doFinalization - Shut down the asmprinter. If you override this in your 00186 /// pass, you must make sure to call it explicitly. 00187 bool doFinalization(Module &M); 00188 00189 /// PrintSpecial - Print information related to the specified machine instr 00190 /// that is independent of the operand, and may be independent of the instr 00191 /// itself. This can be useful for portably encoding the comment character 00192 /// or other bits of target-specific knowledge into the asmstrings. The 00193 /// syntax used is ${:comment}. Targets can override this to add support 00194 /// for their own strange codes. 00195 virtual void PrintSpecial(const MachineInstr *MI, const char *Code) const; 00196 00197 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM 00198 /// instruction, using the specified assembler variant. Targets should 00199 /// override this to format as appropriate. This method can return true if 00200 /// the operand is erroneous. 00201 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 00202 unsigned AsmVariant, const char *ExtraCode); 00203 00204 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM 00205 /// instruction, using the specified assembler variant as an address. 00206 /// Targets should override this to format as appropriate. This method can 00207 /// return true if the operand is erroneous. 00208 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 00209 unsigned AsmVariant, 00210 const char *ExtraCode); 00211 00212 /// PrintGlobalVariable - Emit the specified global variable and its 00213 /// initializer to the output stream. 00214 virtual void PrintGlobalVariable(const GlobalVariable *GV) = 0; 00215 00216 /// SetupMachineFunction - This should be called when a new MachineFunction 00217 /// is being processed from runOnMachineFunction. 00218 void SetupMachineFunction(MachineFunction &MF); 00219 00220 /// getFunctionNumber - Return a unique ID for the current function. 00221 /// 00222 unsigned getFunctionNumber() const { return FunctionNumber; } 00223 00224 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should 00225 /// not normally call this, as the counter is automatically bumped by 00226 /// SetupMachineFunction. 00227 void IncrementFunctionNumber() { FunctionNumber++; } 00228 00229 /// EmitConstantPool - Print to the current output stream assembly 00230 /// representations of the constants in the constant pool MCP. This is 00231 /// used to print out constants which have been "spilled to memory" by 00232 /// the code generator. 00233 /// 00234 void EmitConstantPool(MachineConstantPool *MCP); 00235 00236 /// EmitJumpTableInfo - Print assembly representations of the jump tables 00237 /// used by the current function to the current output stream. 00238 /// 00239 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF); 00240 00241 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a 00242 /// special global used by LLVM. If so, emit it and return true, otherwise 00243 /// do nothing and return false. 00244 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV); 00245 00246 public: 00247 //===------------------------------------------------------------------===// 00248 /// LEB 128 number encoding. 00249 00250 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas) 00251 /// representing an unsigned leb128 value. 00252 void PrintULEB128(unsigned Value) const; 00253 00254 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas) 00255 /// representing a signed leb128 value. 00256 void PrintSLEB128(int Value) const; 00257 00258 //===------------------------------------------------------------------===// 00259 // Emission and print routines 00260 // 00261 00262 /// PrintHex - Print a value as a hexidecimal value. 00263 /// 00264 void PrintHex(int Value) const; 00265 00266 /// EOL - Print a newline character to asm stream. If a comment is present 00267 /// then it will be printed first. Comments should not contain '\n'. 00268 void EOL() const; 00269 void EOL(const std::string &Comment) const; 00270 void EOL(const char* Comment) const; 00271 00272 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an 00273 /// unsigned leb128 value. 00274 void EmitULEB128Bytes(unsigned Value) const; 00275 00276 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a 00277 /// signed leb128 value. 00278 void EmitSLEB128Bytes(int Value) const; 00279 00280 /// EmitInt8 - Emit a byte directive and value. 00281 /// 00282 void EmitInt8(int Value) const; 00283 00284 /// EmitInt16 - Emit a short directive and value. 00285 /// 00286 void EmitInt16(int Value) const; 00287 00288 /// EmitInt32 - Emit a long directive and value. 00289 /// 00290 void EmitInt32(int Value) const; 00291 00292 /// EmitInt64 - Emit a long long directive and value. 00293 /// 00294 void EmitInt64(uint64_t Value) const; 00295 00296 /// EmitString - Emit a string with quotes and a null terminator. 00297 /// Special characters are emitted properly. 00298 /// @verbatim (Eg. '\t') @endverbatim 00299 void EmitString(const std::string &String) const; 00300 void EmitString(const char *String, unsigned Size) const; 00301 00302 /// EmitFile - Emit a .file directive. 00303 void EmitFile(unsigned Number, const std::string &Name) const; 00304 00305 //===------------------------------------------------------------------===// 00306 00307 /// EmitAlignment - Emit an alignment directive to the specified power of 00308 /// two boundary. For example, if you pass in 3 here, you will get an 8 00309 /// byte alignment. If a global value is specified, and if that global has 00310 /// an explicit alignment requested, it will unconditionally override the 00311 /// alignment request. However, if ForcedAlignBits is specified, this value 00312 /// has final say: the ultimate alignment will be the max of ForcedAlignBits 00313 /// and the alignment computed with NumBits and the global. If UseFillExpr 00314 /// is true, it also emits an optional second value FillValue which the 00315 /// assembler uses to fill gaps to match alignment for text sections if the 00316 /// has specified a non-zero fill value. 00317 /// 00318 /// The algorithm is: 00319 /// Align = NumBits; 00320 /// if (GV && GV->hasalignment) Align = GV->getalignment(); 00321 /// Align = std::max(Align, ForcedAlignBits); 00322 /// 00323 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0, 00324 unsigned ForcedAlignBits = 0, 00325 bool UseFillExpr = true) const; 00326 00327 /// printLabel - This method prints a local label used by debug and 00328 /// exception handling tables. 00329 void printLabel(const MachineInstr *MI) const; 00330 void printLabel(unsigned Id) const; 00331 00332 /// printDeclare - This method prints a local variable declaration used by 00333 /// debug tables. 00334 void printDeclare(const MachineInstr *MI) const; 00335 00336 /// EmitComments - Pretty-print comments for instructions 00337 void EmitComments(const MachineInstr &MI) const; 00338 /// EmitComments - Pretty-print comments for instructions 00339 void EmitComments(const MCInst &MI) const; 00340 /// EmitComments - Pretty-print comments for basic blocks 00341 void EmitComments(const MachineBasicBlock &MBB) const; 00342 00343 /// printMCInst - Print an MCInst for this target. 00344 /// 00345 /// Note, this is only a temporary hack to allow the MCStreamer to print 00346 /// instructions, do not use this function outside of llvm-mc. 00347 virtual void printMCInst(const MCInst *MI); 00348 00349 protected: 00350 /// EmitZeros - Emit a block of zeros. 00351 /// 00352 void EmitZeros(uint64_t NumZeros, unsigned AddrSpace = 0) const; 00353 00354 /// EmitString - Emit a zero-byte-terminated string constant. 00355 /// 00356 virtual void EmitString(const ConstantArray *CVA) const; 00357 00358 /// EmitConstantValueOnly - Print out the specified constant, without a 00359 /// storage class. Only constants of first-class type are allowed here. 00360 void EmitConstantValueOnly(const Constant *CV); 00361 00362 /// EmitGlobalConstant - Print a general LLVM constant to the .s file. 00363 void EmitGlobalConstant(const Constant* CV, unsigned AddrSpace = 0); 00364 00365 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV); 00366 00367 /// processDebugLoc - Processes the debug information of each machine 00368 /// instruction's DebugLoc. 00369 void processDebugLoc(DebugLoc DL); 00370 00371 /// printInlineAsm - This method formats and prints the specified machine 00372 /// instruction that is an inline asm. 00373 void printInlineAsm(const MachineInstr *MI) const; 00374 00375 /// printImplicitDef - This method prints the specified machine instruction 00376 /// that is an implicit def. 00377 virtual void printImplicitDef(const MachineInstr *MI) const; 00378 00379 /// printBasicBlockLabel - This method prints the label for the specified 00380 /// MachineBasicBlock 00381 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB, 00382 bool printAlign = false, 00383 bool printColon = false, 00384 bool printComment = true) const; 00385 00386 /// printPICJumpTableSetLabel - This method prints a set label for the 00387 /// specified MachineBasicBlock for a jumptable entry. 00388 virtual void printPICJumpTableSetLabel(unsigned uid, 00389 const MachineBasicBlock *MBB) const; 00390 virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2, 00391 const MachineBasicBlock *MBB) const; 00392 virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI, 00393 const MachineBasicBlock *MBB, 00394 unsigned uid) const; 00395 00396 /// printDataDirective - This method prints the asm directive for the 00397 /// specified type. 00398 void printDataDirective(const Type *type, unsigned AddrSpace = 0); 00399 00400 /// printVisibility - This prints visibility information about symbol, if 00401 /// this is suported by the target. 00402 void printVisibility(const std::string& Name, unsigned Visibility) const; 00403 00404 /// printOffset - This is just convenient handler for printing offsets. 00405 void printOffset(int64_t Offset) const; 00406 00407 private: 00408 void EmitLLVMUsedList(Constant *List); 00409 void EmitXXStructorList(Constant *List); 00410 void EmitGlobalConstantStruct(const ConstantStruct* CVS, 00411 unsigned AddrSpace); 00412 void EmitGlobalConstantArray(const ConstantArray* CVA, unsigned AddrSpace); 00413 void EmitGlobalConstantVector(const ConstantVector* CP); 00414 void EmitGlobalConstantFP(const ConstantFP* CFP, unsigned AddrSpace); 00415 void EmitGlobalConstantLargeInt(const ConstantInt* CI, unsigned AddrSpace); 00416 GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C); 00417 }; 00418 } 00419 00420 #endif