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