Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

490 lines
19 KiB

  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. #ifndef LLVM_CODEGEN_ASMPRINTER_H
  16. #define LLVM_CODEGEN_ASMPRINTER_H
  17. #include "llvm/CodeGen/MachineFunctionPass.h"
  18. #include "llvm/IR/InlineAsm.h"
  19. #include "llvm/Support/DataTypes.h"
  20. #include "llvm/Support/ErrorHandling.h"
  21. namespace llvm {
  22. class BlockAddress;
  23. class GCStrategy;
  24. class Constant;
  25. class ConstantArray;
  26. class GCMetadataPrinter;
  27. class GlobalValue;
  28. class GlobalVariable;
  29. class MachineBasicBlock;
  30. class MachineFunction;
  31. class MachineInstr;
  32. class MachineLocation;
  33. class MachineLoopInfo;
  34. class MachineLoop;
  35. class MachineConstantPoolValue;
  36. class MachineJumpTableInfo;
  37. class MachineModuleInfo;
  38. class MachineMove;
  39. class MCAsmInfo;
  40. class MCContext;
  41. class MCSection;
  42. class MCStreamer;
  43. class MCSymbol;
  44. class MDNode;
  45. class DwarfDebug;
  46. class DwarfException;
  47. class Mangler;
  48. class TargetLoweringObjectFile;
  49. class DataLayout;
  50. class TargetMachine;
  51. /// AsmPrinter - This class is intended to be used as a driving class for all
  52. /// asm writers.
  53. class AsmPrinter : public MachineFunctionPass {
  54. public:
  55. /// Target machine description.
  56. ///
  57. TargetMachine &TM;
  58. /// Target Asm Printer information.
  59. ///
  60. const MCAsmInfo *MAI;
  61. /// OutContext - This is the context for the output file that we are
  62. /// streaming. This owns all of the global MC-related objects for the
  63. /// generated translation unit.
  64. MCContext &OutContext;
  65. /// OutStreamer - This is the MCStreamer object for the file we are
  66. /// generating. This contains the transient state for the current
  67. /// translation unit that we are generating (such as the current section
  68. /// etc).
  69. MCStreamer &OutStreamer;
  70. /// The current machine function.
  71. const MachineFunction *MF;
  72. /// MMI - This is a pointer to the current MachineModuleInfo.
  73. MachineModuleInfo *MMI;
  74. /// Name-mangler for global names.
  75. ///
  76. Mangler *Mang;
  77. /// The symbol for the current function. This is recalculated at the
  78. /// beginning of each call to runOnMachineFunction().
  79. ///
  80. MCSymbol *CurrentFnSym;
  81. /// The symbol used to represent the start of the current function for the
  82. /// purpose of calculating its size (e.g. using the .size directive). By
  83. /// default, this is equal to CurrentFnSym.
  84. MCSymbol *CurrentFnSymForSize;
  85. private:
  86. // GCMetadataPrinters - The garbage collection metadata printer table.
  87. void *GCMetadataPrinters; // Really a DenseMap.
  88. /// VerboseAsm - Emit comments in assembly output if this is true.
  89. ///
  90. bool VerboseAsm;
  91. static char ID;
  92. /// If VerboseAsm is set, a pointer to the loop info for this
  93. /// function.
  94. MachineLoopInfo *LI;
  95. /// DD - If the target supports dwarf debug info, this pointer is non-null.
  96. DwarfDebug *DD;
  97. /// DE - If the target supports dwarf exception info, this pointer is
  98. /// non-null.
  99. DwarfException *DE;
  100. protected:
  101. explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer);
  102. public:
  103. virtual ~AsmPrinter();
  104. /// isVerbose - Return true if assembly output should contain comments.
  105. ///
  106. bool isVerbose() const { return VerboseAsm; }
  107. /// getFunctionNumber - Return a unique ID for the current function.
  108. ///
  109. unsigned getFunctionNumber() const;
  110. /// getObjFileLowering - Return information about object file lowering.
  111. const TargetLoweringObjectFile &getObjFileLowering() const;
  112. /// getDataLayout - Return information about data layout.
  113. const DataLayout &getDataLayout() const;
  114. /// getCurrentSection() - Return the current section we are emitting to.
  115. const MCSection *getCurrentSection() const;
  116. //===------------------------------------------------------------------===//
  117. // MachineFunctionPass Implementation.
  118. //===------------------------------------------------------------------===//
  119. /// getAnalysisUsage - Record analysis usage.
  120. ///
  121. void getAnalysisUsage(AnalysisUsage &AU) const;
  122. /// doInitialization - Set up the AsmPrinter when we are working on a new
  123. /// module. If your pass overrides this, it must make sure to explicitly
  124. /// call this implementation.
  125. bool doInitialization(Module &M);
  126. /// doFinalization - Shut down the asmprinter. If you override this in your
  127. /// pass, you must make sure to call it explicitly.
  128. bool doFinalization(Module &M);
  129. /// runOnMachineFunction - Emit the specified function out to the
  130. /// OutStreamer.
  131. virtual bool runOnMachineFunction(MachineFunction &MF) {
  132. SetupMachineFunction(MF);
  133. EmitFunctionHeader();
  134. EmitFunctionBody();
  135. return false;
  136. }
  137. //===------------------------------------------------------------------===//
  138. // Coarse grained IR lowering routines.
  139. //===------------------------------------------------------------------===//
  140. /// SetupMachineFunction - This should be called when a new MachineFunction
  141. /// is being processed from runOnMachineFunction.
  142. void SetupMachineFunction(MachineFunction &MF);
  143. /// EmitFunctionHeader - This method emits the header for the current
  144. /// function.
  145. void EmitFunctionHeader();
  146. /// EmitFunctionBody - This method emits the body and trailer for a
  147. /// function.
  148. void EmitFunctionBody();
  149. void emitPrologLabel(const MachineInstr &MI);
  150. enum CFIMoveType {
  151. CFI_M_None,
  152. CFI_M_EH,
  153. CFI_M_Debug
  154. };
  155. CFIMoveType needsCFIMoves();
  156. bool needsSEHMoves();
  157. /// needsRelocationsForDwarfStringPool - Specifies whether the object format
  158. /// expects to use relocations to refer to debug entries. Alternatively we
  159. /// emit section offsets in bytes from the start of the string pool.
  160. bool needsRelocationsForDwarfStringPool() const;
  161. /// EmitConstantPool - Print to the current output stream assembly
  162. /// representations of the constants in the constant pool MCP. This is
  163. /// used to print out constants which have been "spilled to memory" by
  164. /// the code generator.
  165. ///
  166. virtual void EmitConstantPool();
  167. /// EmitJumpTableInfo - Print assembly representations of the jump tables
  168. /// used by the current function to the current output stream.
  169. ///
  170. void EmitJumpTableInfo();
  171. /// EmitGlobalVariable - Emit the specified global variable to the .s file.
  172. virtual void EmitGlobalVariable(const GlobalVariable *GV);
  173. /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
  174. /// special global used by LLVM. If so, emit it and return true, otherwise
  175. /// do nothing and return false.
  176. bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
  177. /// EmitAlignment - Emit an alignment directive to the specified power of
  178. /// two boundary. For example, if you pass in 3 here, you will get an 8
  179. /// byte alignment. If a global value is specified, and if that global has
  180. /// an explicit alignment requested, it will override the alignment request
  181. /// if required for correctness.
  182. ///
  183. void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
  184. /// EmitBasicBlockStart - This method prints the label for the specified
  185. /// MachineBasicBlock, an alignment (if present) and a comment describing
  186. /// it if appropriate.
  187. void EmitBasicBlockStart(const MachineBasicBlock *MBB) const;
  188. /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
  189. void EmitGlobalConstant(const Constant *CV, unsigned AddrSpace = 0);
  190. //===------------------------------------------------------------------===//
  191. // Overridable Hooks
  192. //===------------------------------------------------------------------===//
  193. // Targets can, or in the case of EmitInstruction, must implement these to
  194. // customize output.
  195. /// EmitStartOfAsmFile - This virtual method can be overridden by targets
  196. /// that want to emit something at the start of their file.
  197. virtual void EmitStartOfAsmFile(Module &) {}
  198. /// EmitEndOfAsmFile - This virtual method can be overridden by targets that
  199. /// want to emit something at the end of their file.
  200. virtual void EmitEndOfAsmFile(Module &) {}
  201. /// EmitFunctionBodyStart - Targets can override this to emit stuff before
  202. /// the first basic block in the function.
  203. virtual void EmitFunctionBodyStart() {}
  204. /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
  205. /// the last basic block in the function.
  206. virtual void EmitFunctionBodyEnd() {}
  207. /// EmitInstruction - Targets should implement this to emit instructions.
  208. virtual void EmitInstruction(const MachineInstr *) {
  209. llvm_unreachable("EmitInstruction not implemented");
  210. }
  211. virtual void EmitFunctionEntryLabel();
  212. virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
  213. /// EmitXXStructor - Targets can override this to change how global
  214. /// constants that are part of a C++ static/global constructor list are
  215. /// emitted.
  216. virtual void EmitXXStructor(const Constant *CV) {
  217. EmitGlobalConstant(CV);
  218. }
  219. /// isBlockOnlyReachableByFallthough - Return true if the basic block has
  220. /// exactly one predecessor and the control transfer mechanism between
  221. /// the predecessor and this block is a fall-through.
  222. virtual bool
  223. isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
  224. //===------------------------------------------------------------------===//
  225. // Symbol Lowering Routines.
  226. //===------------------------------------------------------------------===//
  227. public:
  228. /// GetTempSymbol - Return the MCSymbol corresponding to the assembler
  229. /// temporary label with the specified stem and unique ID.
  230. MCSymbol *GetTempSymbol(StringRef Name, unsigned ID) const;
  231. /// GetTempSymbol - Return an assembler temporary label with the specified
  232. /// stem.
  233. MCSymbol *GetTempSymbol(StringRef Name) const;
  234. /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
  235. /// global value name as its base, with the specified suffix, and where the
  236. /// symbol is forced to have private linkage if ForcePrivate is true.
  237. MCSymbol *GetSymbolWithGlobalValueBase(const GlobalValue *GV,
  238. StringRef Suffix,
  239. bool ForcePrivate = true) const;
  240. /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
  241. /// ExternalSymbol.
  242. MCSymbol *GetExternalSymbolSymbol(StringRef Sym) const;
  243. /// GetCPISymbol - Return the symbol for the specified constant pool entry.
  244. MCSymbol *GetCPISymbol(unsigned CPID) const;
  245. /// GetJTISymbol - Return the symbol for the specified jump table entry.
  246. MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
  247. /// GetJTSetSymbol - Return the symbol for the specified jump table .set
  248. /// FIXME: privatize to AsmPrinter.
  249. MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const;
  250. /// GetBlockAddressSymbol - Return the MCSymbol used to satisfy BlockAddress
  251. /// uses of the specified basic block.
  252. MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const;
  253. MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const;
  254. //===------------------------------------------------------------------===//
  255. // Emission Helper Routines.
  256. //===------------------------------------------------------------------===//
  257. public:
  258. /// printOffset - This is just convenient handler for printing offsets.
  259. void printOffset(int64_t Offset, raw_ostream &OS) const;
  260. /// EmitInt8 - Emit a byte directive and value.
  261. ///
  262. void EmitInt8(int Value) const;
  263. /// EmitInt16 - Emit a short directive and value.
  264. ///
  265. void EmitInt16(int Value) const;
  266. /// EmitInt32 - Emit a long directive and value.
  267. ///
  268. void EmitInt32(int Value) const;
  269. /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
  270. /// in bytes of the directive is specified by Size and Hi/Lo specify the
  271. /// labels. This implicitly uses .set if it is available.
  272. void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
  273. unsigned Size) const;
  274. /// EmitLabelOffsetDifference - Emit something like ".long Hi+Offset-Lo"
  275. /// where the size in bytes of the directive is specified by Size and Hi/Lo
  276. /// specify the labels. This implicitly uses .set if it is available.
  277. void EmitLabelOffsetDifference(const MCSymbol *Hi, uint64_t Offset,
  278. const MCSymbol *Lo, unsigned Size) const;
  279. /// EmitLabelPlusOffset - Emit something like ".long Label+Offset"
  280. /// where the size in bytes of the directive is specified by Size and Label
  281. /// specifies the label. This implicitly uses .set if it is available.
  282. void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
  283. unsigned Size) const;
  284. /// EmitLabelReference - Emit something like ".long Label"
  285. /// where the size in bytes of the directive is specified by Size and Label
  286. /// specifies the label.
  287. void EmitLabelReference(const MCSymbol *Label, unsigned Size) const {
  288. EmitLabelPlusOffset(Label, 0, Size);
  289. }
  290. //===------------------------------------------------------------------===//
  291. // Dwarf Emission Helper Routines
  292. //===------------------------------------------------------------------===//
  293. /// EmitSLEB128 - emit the specified signed leb128 value.
  294. void EmitSLEB128(int Value, const char *Desc = 0) const;
  295. /// EmitULEB128 - emit the specified unsigned leb128 value.
  296. void EmitULEB128(unsigned Value, const char *Desc = 0,
  297. unsigned PadTo = 0) const;
  298. /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
  299. void EmitCFAByte(unsigned Val) const;
  300. /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
  301. /// encoding. If verbose assembly output is enabled, we output comments
  302. /// describing the encoding. Desc is a string saying what the encoding is
  303. /// specifying (e.g. "LSDA").
  304. void EmitEncodingByte(unsigned Val, const char *Desc = 0) const;
  305. /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
  306. unsigned GetSizeOfEncodedValue(unsigned Encoding) const;
  307. /// EmitReference - Emit reference to a ttype global with a specified encoding.
  308. void EmitTTypeReference(const GlobalValue *GV, unsigned Encoding) const;
  309. /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of
  310. /// its section. This can be done with a special directive if the target
  311. /// supports it (e.g. cygwin) or by emitting it as an offset from a label at
  312. /// the start of the section.
  313. ///
  314. /// SectionLabel is a temporary label emitted at the start of the section
  315. /// that Label lives in.
  316. void EmitSectionOffset(const MCSymbol *Label,
  317. const MCSymbol *SectionLabel) const;
  318. /// getDebugValueLocation - Get location information encoded by DBG_VALUE
  319. /// operands.
  320. virtual MachineLocation getDebugValueLocation(const MachineInstr *MI) const;
  321. /// getISAEncoding - Get the value for DW_AT_APPLE_isa. Zero if no isa
  322. /// encoding specified.
  323. virtual unsigned getISAEncoding() { return 0; }
  324. /// EmitDwarfRegOp - Emit dwarf register operation.
  325. virtual void EmitDwarfRegOp(const MachineLocation &MLoc) const;
  326. //===------------------------------------------------------------------===//
  327. // Dwarf Lowering Routines
  328. //===------------------------------------------------------------------===//
  329. /// EmitCFIFrameMove - Emit frame instruction to describe the layout of the
  330. /// frame.
  331. void EmitCFIFrameMove(const MachineMove &Move) const;
  332. //===------------------------------------------------------------------===//
  333. // Inline Asm Support
  334. //===------------------------------------------------------------------===//
  335. public:
  336. // These are hooks that targets can override to implement inline asm
  337. // support. These should probably be moved out of AsmPrinter someday.
  338. /// PrintSpecial - Print information related to the specified machine instr
  339. /// that is independent of the operand, and may be independent of the instr
  340. /// itself. This can be useful for portably encoding the comment character
  341. /// or other bits of target-specific knowledge into the asmstrings. The
  342. /// syntax used is ${:comment}. Targets can override this to add support
  343. /// for their own strange codes.
  344. virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
  345. const char *Code) const;
  346. /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
  347. /// instruction, using the specified assembler variant. Targets should
  348. /// override this to format as appropriate. This method can return true if
  349. /// the operand is erroneous.
  350. virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
  351. unsigned AsmVariant, const char *ExtraCode,
  352. raw_ostream &OS);
  353. /// PrintAsmMemoryOperand - Print the specified operand of MI, an INLINEASM
  354. /// instruction, using the specified assembler variant as an address.
  355. /// Targets should override this to format as appropriate. This method can
  356. /// return true if the operand is erroneous.
  357. virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
  358. unsigned AsmVariant,
  359. const char *ExtraCode,
  360. raw_ostream &OS);
  361. private:
  362. /// Private state for PrintSpecial()
  363. // Assign a unique ID to this machine instruction.
  364. mutable const MachineInstr *LastMI;
  365. mutable unsigned LastFn;
  366. mutable unsigned Counter;
  367. mutable unsigned SetCounter;
  368. /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
  369. void EmitInlineAsm(StringRef Str, const MDNode *LocMDNode = 0,
  370. InlineAsm::AsmDialect AsmDialect = InlineAsm::AD_ATT) const;
  371. /// EmitInlineAsm - This method formats and emits the specified machine
  372. /// instruction that is an inline asm.
  373. void EmitInlineAsm(const MachineInstr *MI) const;
  374. //===------------------------------------------------------------------===//
  375. // Internal Implementation Details
  376. //===------------------------------------------------------------------===//
  377. /// EmitVisibility - This emits visibility information about symbol, if
  378. /// this is suported by the target.
  379. void EmitVisibility(MCSymbol *Sym, unsigned Visibility,
  380. bool IsDefinition = true) const;
  381. void EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const;
  382. void EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
  383. const MachineBasicBlock *MBB,
  384. unsigned uid) const;
  385. void EmitLLVMUsedList(const ConstantArray *InitList);
  386. void EmitXXStructorList(const Constant *List, bool isCtor);
  387. GCMetadataPrinter *GetOrCreateGCPrinter(GCStrategy *C);
  388. };
  389. }
  390. #endif