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.

425 lines
14 KiB

  1. //===- MCDwarf.h - Machine Code Dwarf support -------------------*- 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 the declaration of the MCDwarfFile to support the dwarf
  11. // .file directive and the .loc directive.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_MC_MCDWARF_H
  15. #define LLVM_MC_MCDWARF_H
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/Support/Compiler.h"
  18. #include "llvm/Support/Dwarf.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. #include <map>
  21. #include <vector>
  22. namespace llvm {
  23. class MCContext;
  24. class MCObjectWriter;
  25. class MCSection;
  26. class MCStreamer;
  27. class MCSymbol;
  28. class SourceMgr;
  29. class SMLoc;
  30. /// MCDwarfFile - Instances of this class represent the name of the dwarf
  31. /// .file directive and its associated dwarf file number in the MC file,
  32. /// and MCDwarfFile's are created and unique'd by the MCContext class where
  33. /// the file number for each is its index into the vector of DwarfFiles (note
  34. /// index 0 is not used and not a valid dwarf file number).
  35. class MCDwarfFile {
  36. // Name - the base name of the file without its directory path.
  37. // The StringRef references memory allocated in the MCContext.
  38. StringRef Name;
  39. // DirIndex - the index into the list of directory names for this file name.
  40. unsigned DirIndex;
  41. private: // MCContext creates and uniques these.
  42. friend class MCContext;
  43. MCDwarfFile(StringRef name, unsigned dirIndex)
  44. : Name(name), DirIndex(dirIndex) {}
  45. MCDwarfFile(const MCDwarfFile&) LLVM_DELETED_FUNCTION;
  46. void operator=(const MCDwarfFile&) LLVM_DELETED_FUNCTION;
  47. public:
  48. /// getName - Get the base name of this MCDwarfFile.
  49. StringRef getName() const { return Name; }
  50. /// getDirIndex - Get the dirIndex of this MCDwarfFile.
  51. unsigned getDirIndex() const { return DirIndex; }
  52. /// print - Print the value to the stream \p OS.
  53. void print(raw_ostream &OS) const;
  54. /// dump - Print the value to stderr.
  55. void dump() const;
  56. };
  57. inline raw_ostream &operator<<(raw_ostream &OS, const MCDwarfFile &DwarfFile){
  58. DwarfFile.print(OS);
  59. return OS;
  60. }
  61. /// MCDwarfLoc - Instances of this class represent the information from a
  62. /// dwarf .loc directive.
  63. class MCDwarfLoc {
  64. // FileNum - the file number.
  65. unsigned FileNum;
  66. // Line - the line number.
  67. unsigned Line;
  68. // Column - the column position.
  69. unsigned Column;
  70. // Flags (see #define's below)
  71. unsigned Flags;
  72. // Isa
  73. unsigned Isa;
  74. // Discriminator
  75. unsigned Discriminator;
  76. // Flag that indicates the initial value of the is_stmt_start flag.
  77. #define DWARF2_LINE_DEFAULT_IS_STMT 1
  78. #define DWARF2_FLAG_IS_STMT (1 << 0)
  79. #define DWARF2_FLAG_BASIC_BLOCK (1 << 1)
  80. #define DWARF2_FLAG_PROLOGUE_END (1 << 2)
  81. #define DWARF2_FLAG_EPILOGUE_BEGIN (1 << 3)
  82. private: // MCContext manages these
  83. friend class MCContext;
  84. friend class MCLineEntry;
  85. MCDwarfLoc(unsigned fileNum, unsigned line, unsigned column, unsigned flags,
  86. unsigned isa, unsigned discriminator)
  87. : FileNum(fileNum), Line(line), Column(column), Flags(flags), Isa(isa),
  88. Discriminator(discriminator) {}
  89. // Allow the default copy constructor and assignment operator to be used
  90. // for an MCDwarfLoc object.
  91. public:
  92. /// getFileNum - Get the FileNum of this MCDwarfLoc.
  93. unsigned getFileNum() const { return FileNum; }
  94. /// getLine - Get the Line of this MCDwarfLoc.
  95. unsigned getLine() const { return Line; }
  96. /// getColumn - Get the Column of this MCDwarfLoc.
  97. unsigned getColumn() const { return Column; }
  98. /// getFlags - Get the Flags of this MCDwarfLoc.
  99. unsigned getFlags() const { return Flags; }
  100. /// getIsa - Get the Isa of this MCDwarfLoc.
  101. unsigned getIsa() const { return Isa; }
  102. /// getDiscriminator - Get the Discriminator of this MCDwarfLoc.
  103. unsigned getDiscriminator() const { return Discriminator; }
  104. /// setFileNum - Set the FileNum of this MCDwarfLoc.
  105. void setFileNum(unsigned fileNum) { FileNum = fileNum; }
  106. /// setLine - Set the Line of this MCDwarfLoc.
  107. void setLine(unsigned line) { Line = line; }
  108. /// setColumn - Set the Column of this MCDwarfLoc.
  109. void setColumn(unsigned column) { Column = column; }
  110. /// setFlags - Set the Flags of this MCDwarfLoc.
  111. void setFlags(unsigned flags) { Flags = flags; }
  112. /// setIsa - Set the Isa of this MCDwarfLoc.
  113. void setIsa(unsigned isa) { Isa = isa; }
  114. /// setDiscriminator - Set the Discriminator of this MCDwarfLoc.
  115. void setDiscriminator(unsigned discriminator) {
  116. Discriminator = discriminator;
  117. }
  118. };
  119. /// MCLineEntry - Instances of this class represent the line information for
  120. /// the dwarf line table entries. Which is created after a machine
  121. /// instruction is assembled and uses an address from a temporary label
  122. /// created at the current address in the current section and the info from
  123. /// the last .loc directive seen as stored in the context.
  124. class MCLineEntry : public MCDwarfLoc {
  125. MCSymbol *Label;
  126. private:
  127. // Allow the default copy constructor and assignment operator to be used
  128. // for an MCLineEntry object.
  129. public:
  130. // Constructor to create an MCLineEntry given a symbol and the dwarf loc.
  131. MCLineEntry(MCSymbol *label, const MCDwarfLoc loc) : MCDwarfLoc(loc),
  132. Label(label) {}
  133. MCSymbol *getLabel() const { return Label; }
  134. // This is called when an instruction is assembled into the specified
  135. // section and if there is information from the last .loc directive that
  136. // has yet to have a line entry made for it is made.
  137. static void Make(MCStreamer *MCOS, const MCSection *Section);
  138. };
  139. /// MCLineSection - Instances of this class represent the line information
  140. /// for a section where machine instructions have been assembled after seeing
  141. /// .loc directives. This is the information used to build the dwarf line
  142. /// table for a section.
  143. class MCLineSection {
  144. private:
  145. MCLineSection(const MCLineSection&) LLVM_DELETED_FUNCTION;
  146. void operator=(const MCLineSection&) LLVM_DELETED_FUNCTION;
  147. public:
  148. // Constructor to create an MCLineSection with an empty MCLineEntries
  149. // vector.
  150. MCLineSection() {}
  151. // addLineEntry - adds an entry to this MCLineSection's line entries
  152. void addLineEntry(const MCLineEntry &LineEntry, unsigned CUID) {
  153. MCLineDivisions[CUID].push_back(LineEntry);
  154. }
  155. typedef std::vector<MCLineEntry> MCLineEntryCollection;
  156. typedef MCLineEntryCollection::iterator iterator;
  157. typedef MCLineEntryCollection::const_iterator const_iterator;
  158. typedef std::map<unsigned, MCLineEntryCollection> MCLineDivisionMap;
  159. private:
  160. // A collection of MCLineEntry for each Compile Unit ID.
  161. MCLineDivisionMap MCLineDivisions;
  162. public:
  163. // Returns whether MCLineSection contains entries for a given Compile
  164. // Unit ID.
  165. bool containEntriesForID(unsigned CUID) const {
  166. return MCLineDivisions.count(CUID);
  167. }
  168. // Returns the collection of MCLineEntry for a given Compile Unit ID.
  169. const MCLineEntryCollection &getMCLineEntries(unsigned CUID) const {
  170. MCLineDivisionMap::const_iterator CIter = MCLineDivisions.find(CUID);
  171. assert(CIter != MCLineDivisions.end());
  172. return CIter->second;
  173. }
  174. };
  175. class MCDwarfFileTable {
  176. public:
  177. //
  178. // This emits the Dwarf file and the line tables for all Compile Units.
  179. //
  180. static const MCSymbol *Emit(MCStreamer *MCOS);
  181. //
  182. // This emits the Dwarf file and the line tables for a given Compile Unit.
  183. //
  184. static const MCSymbol *EmitCU(MCStreamer *MCOS, unsigned ID);
  185. };
  186. class MCDwarfLineAddr {
  187. public:
  188. /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
  189. static void Encode(int64_t LineDelta, uint64_t AddrDelta, raw_ostream &OS);
  190. /// Utility function to emit the encoding to a streamer.
  191. static void Emit(MCStreamer *MCOS,
  192. int64_t LineDelta,uint64_t AddrDelta);
  193. /// Utility function to write the encoding to an object writer.
  194. static void Write(MCObjectWriter *OW,
  195. int64_t LineDelta, uint64_t AddrDelta);
  196. };
  197. class MCGenDwarfInfo {
  198. public:
  199. //
  200. // When generating dwarf for assembly source files this emits the Dwarf
  201. // sections.
  202. //
  203. static void Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol);
  204. };
  205. // When generating dwarf for assembly source files this is the info that is
  206. // needed to be gathered for each symbol that will have a dwarf label.
  207. class MCGenDwarfLabelEntry {
  208. private:
  209. // Name of the symbol without a leading underbar, if any.
  210. StringRef Name;
  211. // The dwarf file number this symbol is in.
  212. unsigned FileNumber;
  213. // The line number this symbol is at.
  214. unsigned LineNumber;
  215. // The low_pc for the dwarf label is taken from this symbol.
  216. MCSymbol *Label;
  217. public:
  218. MCGenDwarfLabelEntry(StringRef name, unsigned fileNumber,
  219. unsigned lineNumber, MCSymbol *label) :
  220. Name(name), FileNumber(fileNumber), LineNumber(lineNumber), Label(label){}
  221. StringRef getName() const { return Name; }
  222. unsigned getFileNumber() const { return FileNumber; }
  223. unsigned getLineNumber() const { return LineNumber; }
  224. MCSymbol *getLabel() const { return Label; }
  225. // This is called when label is created when we are generating dwarf for
  226. // assembly source files.
  227. static void Make(MCSymbol *Symbol, MCStreamer *MCOS, SourceMgr &SrcMgr,
  228. SMLoc &Loc);
  229. };
  230. class MCCFIInstruction {
  231. public:
  232. enum OpType { OpSameValue, OpRememberState, OpRestoreState, OpOffset,
  233. OpDefCfaRegister, OpDefCfaOffset, OpDefCfa, OpRelOffset,
  234. OpAdjustCfaOffset, OpEscape, OpRestore, OpUndefined,
  235. OpRegister };
  236. private:
  237. OpType Operation;
  238. MCSymbol *Label;
  239. unsigned Register;
  240. union {
  241. int Offset;
  242. unsigned Register2;
  243. };
  244. std::vector<char> Values;
  245. MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R, int O, StringRef V) :
  246. Operation(Op), Label(L), Register(R), Offset(O),
  247. Values(V.begin(), V.end()) {
  248. assert(Op != OpRegister);
  249. }
  250. MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R1, unsigned R2) :
  251. Operation(Op), Label(L), Register(R1), Register2(R2) {
  252. assert(Op == OpRegister);
  253. }
  254. public:
  255. static MCCFIInstruction
  256. createOffset(MCSymbol *L, unsigned Register, int Offset) {
  257. return MCCFIInstruction(OpOffset, L, Register, Offset, "");
  258. }
  259. static MCCFIInstruction
  260. createDefCfaRegister(MCSymbol *L, unsigned Register) {
  261. return MCCFIInstruction(OpDefCfaRegister, L, Register, 0, "");
  262. }
  263. static MCCFIInstruction createDefCfaOffset(MCSymbol *L, int Offset) {
  264. return MCCFIInstruction(OpDefCfaOffset, L, 0, -Offset, "");
  265. }
  266. static MCCFIInstruction
  267. createDefCfa(MCSymbol *L, unsigned Register, int Offset) {
  268. return MCCFIInstruction(OpDefCfa, L, Register, -Offset, "");
  269. }
  270. static MCCFIInstruction createUndefined(MCSymbol *L, unsigned Register) {
  271. return MCCFIInstruction(OpUndefined, L, Register, 0, "");
  272. }
  273. static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register) {
  274. return MCCFIInstruction(OpRestore, L, Register, 0, "");
  275. }
  276. static MCCFIInstruction createSameValue(MCSymbol *L, unsigned Register) {
  277. return MCCFIInstruction(OpSameValue, L, Register, 0, "");
  278. }
  279. static MCCFIInstruction createRestoreState(MCSymbol *L) {
  280. return MCCFIInstruction(OpRestoreState, L, 0, 0, "");
  281. }
  282. static MCCFIInstruction createRememberState(MCSymbol *L) {
  283. return MCCFIInstruction(OpRememberState, L, 0, 0, "");
  284. }
  285. static MCCFIInstruction
  286. createRelOffset(MCSymbol *L, unsigned Register, int Offset) {
  287. return MCCFIInstruction(OpRelOffset, L, Register, Offset, "");
  288. }
  289. static MCCFIInstruction
  290. createAdjustCfaOffset(MCSymbol *L, int Adjustment) {
  291. return MCCFIInstruction(OpAdjustCfaOffset, L, 0, Adjustment, "");
  292. }
  293. static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals) {
  294. return MCCFIInstruction(OpEscape, L, 0, 0, Vals);
  295. }
  296. static MCCFIInstruction
  297. createRegister(MCSymbol *L, unsigned Register1, unsigned Register2) {
  298. return MCCFIInstruction(OpRegister, L, Register1, Register2);
  299. }
  300. OpType getOperation() const { return Operation; }
  301. MCSymbol *getLabel() const { return Label; }
  302. unsigned getRegister() const {
  303. assert(Operation == OpDefCfa || Operation == OpOffset ||
  304. Operation == OpRestore || Operation == OpUndefined ||
  305. Operation == OpSameValue || Operation == OpDefCfaRegister ||
  306. Operation == OpRelOffset || Operation == OpRegister);
  307. return Register;
  308. }
  309. unsigned getRegister2() const {
  310. assert(Operation == OpRegister);
  311. return Register2;
  312. }
  313. int getOffset() const {
  314. assert(Operation == OpDefCfa || Operation == OpOffset ||
  315. Operation == OpRelOffset || Operation == OpDefCfaOffset ||
  316. Operation == OpAdjustCfaOffset);
  317. return Offset;
  318. }
  319. const StringRef getValues() const {
  320. assert(Operation == OpEscape);
  321. return StringRef(&Values[0], Values.size());
  322. }
  323. };
  324. struct MCDwarfFrameInfo {
  325. MCDwarfFrameInfo() : Begin(0), End(0), Personality(0), Lsda(0),
  326. Function(0), Instructions(), PersonalityEncoding(),
  327. LsdaEncoding(0), CompactUnwindEncoding(0),
  328. IsSignalFrame(false) {}
  329. MCSymbol *Begin;
  330. MCSymbol *End;
  331. const MCSymbol *Personality;
  332. const MCSymbol *Lsda;
  333. const MCSymbol *Function;
  334. std::vector<MCCFIInstruction> Instructions;
  335. unsigned PersonalityEncoding;
  336. unsigned LsdaEncoding;
  337. uint32_t CompactUnwindEncoding;
  338. bool IsSignalFrame;
  339. };
  340. class MCDwarfFrameEmitter {
  341. public:
  342. //
  343. // This emits the frame info section.
  344. //
  345. static void Emit(MCStreamer &streamer, bool usingCFI,
  346. bool isEH);
  347. static void EmitAdvanceLoc(MCStreamer &Streamer, uint64_t AddrDelta);
  348. static void EncodeAdvanceLoc(uint64_t AddrDelta, raw_ostream &OS);
  349. };
  350. } // end namespace llvm
  351. #endif