AsmPrinter.h revision 6e19896999f9297db38e1a1a66d9bef0e2f09776
1//===-- llvm/CodeGen/AsmPrinter.h - AsmPrinter Framework --------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source 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 <set> 22 23namespace llvm { 24 class Constant; 25 class ConstantArray; 26 class GlobalVariable; 27 class GlobalAlias; 28 class MachineConstantPoolEntry; 29 class MachineConstantPoolValue; 30 class Mangler; 31 class TargetAsmInfo; 32 33 34 /// AsmPrinter - This class is intended to be used as a driving class for all 35 /// asm writers. 36 class AsmPrinter : public MachineFunctionPass { 37 static char ID; 38 39 /// FunctionNumber - This provides a unique ID for each function emitted in 40 /// this translation unit. It is autoincremented by SetupMachineFunction, 41 /// and can be accessed with getFunctionNumber() and 42 /// IncrementFunctionNumber(). 43 /// 44 unsigned FunctionNumber; 45 46 /// Cache of mangled exception handling name for current function. This is 47 /// recalculated at the beginning of each call to runOnMachineFunction(). 48 /// 49 std::string CurrentFnEHName; 50 51 protected: 52 // Necessary for external weak linkage support 53 std::set<const GlobalValue*> ExtWeakSymbols; 54 55 public: 56 /// Output stream on which we're printing assembly code. 57 /// 58 std::ostream &O; 59 60 /// Target machine description. 61 /// 62 TargetMachine &TM; 63 64 /// Target Asm Printer information. 65 /// 66 const TargetAsmInfo *TAI; 67 68 /// Name-mangler for global names. 69 /// 70 Mangler *Mang; 71 72 /// Cache of mangled name for current function. This is recalculated at the 73 /// beginning of each call to runOnMachineFunction(). 74 /// 75 std::string CurrentFnName; 76 77 /// CurrentSection - The current section we are emitting to. This is 78 /// controlled and used by the SwitchSection method. 79 std::string CurrentSection; 80 81 protected: 82 AsmPrinter(std::ostream &o, TargetMachine &TM, const TargetAsmInfo *T); 83 84 public: 85 /// SwitchToTextSection - Switch to the specified section of the executable 86 /// if we are not already in it! If GV is non-null and if the global has an 87 /// explicitly requested section, we switch to the section indicated for the 88 /// global instead of NewSection. 89 /// 90 /// If the new section is an empty string, this method forgets what the 91 /// current section is, but does not emit a .section directive. 92 /// 93 /// This method is used when about to emit executable code. 94 /// 95 void SwitchToTextSection(const char *NewSection, const GlobalValue *GV = NULL); 96 97 /// SwitchToDataSection - Switch to the specified section of the executable 98 /// if we are not already in it! If GV is non-null and if the global has an 99 /// explicitly requested section, we switch to the section indicated for the 100 /// global instead of NewSection. 101 /// 102 /// If the new section is an empty string, this method forgets what the 103 /// current section is, but does not emit a .section directive. 104 /// 105 /// This method is used when about to emit data. For most assemblers, this 106 /// is the same as the SwitchToTextSection method, but not all assemblers 107 /// are the same. 108 /// 109 void SwitchToDataSection(const char *NewSection, const GlobalValue *GV = NULL); 110 111 /// getGlobalLinkName - Returns the asm/link name of of the specified 112 /// global variable. Should be overridden by each target asm printer to 113 /// generate the appropriate value. 114 virtual const std::string getGlobalLinkName(const GlobalVariable *GV) const; 115 116 /// EmitExternalGlobal - Emit the external reference to a global variable. 117 /// Should be overridden if an indirect reference should be used. 118 virtual void EmitExternalGlobal(const GlobalVariable *GV); 119 120 /// getCurrentFunctionEHName - Called to return (and cache) the 121 /// CurrentFnEHName. 122 /// 123 const std::string &getCurrentFunctionEHName(const MachineFunction *MF); 124 125 protected: 126 /// doInitialization - Set up the AsmPrinter when we are working on a new 127 /// module. If your pass overrides this, it must make sure to explicitly 128 /// call this implementation. 129 bool doInitialization(Module &M); 130 131 /// doFinalization - Shut down the asmprinter. If you override this in your 132 /// pass, you must make sure to call it explicitly. 133 bool doFinalization(Module &M); 134 135 /// PrintSpecial - Print information related to the specified machine instr 136 /// that is independent of the operand, and may be independent of the instr 137 /// itself. This can be useful for portably encoding the comment character 138 /// or other bits of target-specific knowledge into the asmstrings. The 139 /// syntax used is ${:comment}. Targets can override this to add support 140 /// for their own strange codes. 141 virtual void PrintSpecial(const MachineInstr *MI, const char *Code); 142 143 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM 144 /// instruction, using the specified assembler variant. Targets should 145 /// override this to format as appropriate. This method can return true if 146 /// the operand is erroneous. 147 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 148 unsigned AsmVariant, const char *ExtraCode); 149 150 /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM 151 /// instruction, using the specified assembler variant as an address. 152 /// Targets should override this to format as appropriate. This method can 153 /// return true if the operand is erroneous. 154 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 155 unsigned AsmVariant, 156 const char *ExtraCode); 157 158 /// getSectionForFunction - Return the section that we should emit the 159 /// specified function body into. This defaults to 'TextSection'. This 160 /// should most likely be overridden by the target to put linkonce/weak 161 /// functions into special sections. 162 virtual std::string getSectionForFunction(const Function &F) const; 163 164 /// SetupMachineFunction - This should be called when a new MachineFunction 165 /// is being processed from runOnMachineFunction. 166 void SetupMachineFunction(MachineFunction &MF); 167 168 /// getFunctionNumber - Return a unique ID for the current function. 169 /// 170 unsigned getFunctionNumber() const { return FunctionNumber; } 171 172 /// IncrementFunctionNumber - Increase Function Number. AsmPrinters should 173 /// not normally call this, as the counter is automatically bumped by 174 /// SetupMachineFunction. 175 void IncrementFunctionNumber() { FunctionNumber++; } 176 177 /// EmitConstantPool - Print to the current output stream assembly 178 /// representations of the constants in the constant pool MCP. This is 179 /// used to print out constants which have been "spilled to memory" by 180 /// the code generator. 181 /// 182 void EmitConstantPool(MachineConstantPool *MCP); 183 184 /// EmitJumpTableInfo - Print assembly representations of the jump tables 185 /// used by the current function to the current output stream. 186 /// 187 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI, MachineFunction &MF); 188 189 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a 190 /// special global used by LLVM. If so, emit it and return true, otherwise 191 /// do nothing and return false. 192 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV); 193 194 public: 195 //===------------------------------------------------------------------===// 196 /// LEB 128 number encoding. 197 198 /// PrintULEB128 - Print a series of hexidecimal values(separated by commas) 199 /// representing an unsigned leb128 value. 200 void PrintULEB128(unsigned Value) const; 201 202 /// SizeULEB128 - Compute the number of bytes required for an unsigned 203 /// leb128 value. 204 static unsigned SizeULEB128(unsigned Value); 205 206 /// PrintSLEB128 - Print a series of hexidecimal values(separated by commas) 207 /// representing a signed leb128 value. 208 void PrintSLEB128(int Value) const; 209 210 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128 211 /// value. 212 static unsigned SizeSLEB128(int Value); 213 214 //===------------------------------------------------------------------===// 215 // Emission and print routines 216 // 217 218 /// PrintHex - Print a value as a hexidecimal value. 219 /// 220 void PrintHex(int Value) const; 221 222 /// EOL - Print a newline character to asm stream. If a comment is present 223 /// then it will be printed first. Comments should not contain '\n'. 224 void EOL() const; 225 void EOL(const std::string &Comment) const; 226 227 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an 228 /// unsigned leb128 value. 229 void EmitULEB128Bytes(unsigned Value) const; 230 231 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a 232 /// signed leb128 value. 233 void EmitSLEB128Bytes(int Value) const; 234 235 /// EmitInt8 - Emit a byte directive and value. 236 /// 237 void EmitInt8(int Value) const; 238 239 /// EmitInt16 - Emit a short directive and value. 240 /// 241 void EmitInt16(int Value) const; 242 243 /// EmitInt32 - Emit a long directive and value. 244 /// 245 void EmitInt32(int Value) const; 246 247 /// EmitInt64 - Emit a long long directive and value. 248 /// 249 void EmitInt64(uint64_t Value) const; 250 251 /// EmitString - Emit a string with quotes and a null terminator. 252 /// Special characters are emitted properly. 253 /// @verbatim (Eg. '\t') @endverbatim 254 void EmitString(const std::string &String) const; 255 256 //===------------------------------------------------------------------===// 257 258 /// EmitAlignment - Emit an alignment directive to the specified power of 259 /// two boundary. For example, if you pass in 3 here, you will get an 8 260 /// byte alignment. If a global value is specified, and if that global has 261 /// an explicit alignment requested, it will unconditionally override the 262 /// alignment request. However, if ForcedAlignBits is specified, this value 263 /// has final say: the ultimate alignment will be the max of ForcedAlignBits 264 /// and the alignment computed with NumBits and the global. If UseFillExpr 265 /// is true, it also emits an optional second value FillValue which the 266 /// assembler uses to fill gaps to match alignment. 267 /// 268 /// The algorithm is: 269 /// Align = NumBits; 270 /// if (GV && GV->hasalignment) Align = GV->getalignment(); 271 /// Align = std::max(Align, ForcedAlignBits); 272 /// 273 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0, 274 unsigned ForcedAlignBits = 0, bool UseFillExpr = false, 275 unsigned FillValue = 0) const; 276 277 protected: 278 /// EmitZeros - Emit a block of zeros. 279 /// 280 void EmitZeros(uint64_t NumZeros) const; 281 282 /// EmitString - Emit a zero-byte-terminated string constant. 283 /// 284 virtual void EmitString(const ConstantArray *CVA) const; 285 286 /// EmitConstantValueOnly - Print out the specified constant, without a 287 /// storage class. Only constants of first-class type are allowed here. 288 void EmitConstantValueOnly(const Constant *CV); 289 290 /// EmitGlobalConstant - Print a general LLVM constant to the .s file. 291 /// 292 void EmitGlobalConstant(const Constant* CV); 293 294 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV); 295 296 /// printInlineAsm - This method formats and prints the specified machine 297 /// instruction that is an inline asm. 298 void printInlineAsm(const MachineInstr *MI) 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 304 /// printBasicBlockLabel - This method prints the label for the specified 305 /// MachineBasicBlock 306 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB, 307 bool printColon = false, 308 bool printComment = true) const; 309 310 /// printSetLabel - This method prints a set label for the specified 311 /// MachineBasicBlock 312 void printSetLabel(unsigned uid, const MachineBasicBlock *MBB) const; 313 void printSetLabel(unsigned uid, unsigned uid2, 314 const MachineBasicBlock *MBB) const; 315 316 /// printDataDirective - This method prints the asm directive for the 317 /// specified type. 318 void printDataDirective(const Type *type); 319 320 private: 321 void EmitLLVMUsedList(Constant *List); 322 void EmitXXStructorList(Constant *List); 323 void EmitConstantPool(unsigned Alignment, const char *Section, 324 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP); 325 326 }; 327} 328 329#endif 330