MCDwarf.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MCDwarf.h - Machine Code Dwarf support -------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file contains the declaration of the MCDwarfFile to support the dwarf
  15. // .file directive and the .loc directive.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_MC_MCDWARF_H
  19. #define LLVM_MC_MCDWARF_H
  20. #include "llvm/ADT/MapVector.h"
  21. #include "llvm/ADT/Optional.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/ADT/StringMap.h"
  24. #include "llvm/ADT/StringRef.h"
  25. #include "llvm/MC/MCSection.h"
  26. #include "llvm/MC/StringTableBuilder.h"
  27. #include "llvm/Support/Error.h"
  28. #include "llvm/Support/MD5.h"
  29. #include <cassert>
  30. #include <cstdint>
  31. #include <string>
  32. #include <tuple>
  33. #include <utility>
  34. #include <vector>
  35. namespace llvm {
  36. template <typename T> class ArrayRef;
  37. class MCAsmBackend;
  38. class MCContext;
  39. class MCObjectStreamer;
  40. class MCStreamer;
  41. class MCSymbol;
  42. class raw_ostream;
  43. class SMLoc;
  44. class SourceMgr;
  45. namespace mcdwarf {
  46. // Emit the common part of the DWARF 5 range/locations list tables header.
  47. MCSymbol *emitListsTableHeaderStart(MCStreamer &S);
  48. } // namespace mcdwarf
  49. /// Manage the .debug_line_str section contents, if we use it.
  50. class MCDwarfLineStr {
  51. MCSymbol *LineStrLabel = nullptr;
  52. StringTableBuilder LineStrings{StringTableBuilder::DWARF};
  53. bool UseRelocs = false;
  54. public:
  55. /// Construct an instance that can emit .debug_line_str (for use in a normal
  56. /// v5 line table).
  57. explicit MCDwarfLineStr(MCContext &Ctx);
  58. /// Emit a reference to the string.
  59. void emitRef(MCStreamer *MCOS, StringRef Path);
  60. /// Emit the .debug_line_str section if appropriate.
  61. void emitSection(MCStreamer *MCOS);
  62. };
  63. /// Instances of this class represent the name of the dwarf .file directive and
  64. /// its associated dwarf file number in the MC file. MCDwarfFile's are created
  65. /// and uniqued by the MCContext class. In Dwarf 4 file numbers start from 1;
  66. /// i.e. the entry with file number 1 is the first element in the vector of
  67. /// DwarfFiles and there is no MCDwarfFile with file number 0. In Dwarf 5 file
  68. /// numbers start from 0, with the MCDwarfFile with file number 0 being the
  69. /// primary source file, and file numbers correspond to their index in the
  70. /// vector.
  71. struct MCDwarfFile {
  72. // The base name of the file without its directory path.
  73. std::string Name;
  74. // The index into the list of directory names for this file name.
  75. unsigned DirIndex = 0;
  76. /// The MD5 checksum, if there is one. Non-owning pointer to data allocated
  77. /// in MCContext.
  78. Optional<MD5::MD5Result> Checksum;
  79. /// The source code of the file. Non-owning reference to data allocated in
  80. /// MCContext.
  81. Optional<StringRef> Source;
  82. };
  83. /// Instances of this class represent the information from a
  84. /// dwarf .loc directive.
  85. class MCDwarfLoc {
  86. uint32_t FileNum;
  87. uint32_t Line;
  88. uint16_t Column;
  89. // Flags (see #define's below)
  90. uint8_t Flags;
  91. uint8_t Isa;
  92. uint32_t Discriminator;
  93. // Flag that indicates the initial value of the is_stmt_start flag.
  94. #define DWARF2_LINE_DEFAULT_IS_STMT 1
  95. #define DWARF2_FLAG_IS_STMT (1 << 0)
  96. #define DWARF2_FLAG_BASIC_BLOCK (1 << 1)
  97. #define DWARF2_FLAG_PROLOGUE_END (1 << 2)
  98. #define DWARF2_FLAG_EPILOGUE_BEGIN (1 << 3)
  99. private: // MCContext manages these
  100. friend class MCContext;
  101. friend class MCDwarfLineEntry;
  102. MCDwarfLoc(unsigned fileNum, unsigned line, unsigned column, unsigned flags,
  103. unsigned isa, unsigned discriminator)
  104. : FileNum(fileNum), Line(line), Column(column), Flags(flags), Isa(isa),
  105. Discriminator(discriminator) {}
  106. // Allow the default copy constructor and assignment operator to be used
  107. // for an MCDwarfLoc object.
  108. public:
  109. /// Get the FileNum of this MCDwarfLoc.
  110. unsigned getFileNum() const { return FileNum; }
  111. /// Get the Line of this MCDwarfLoc.
  112. unsigned getLine() const { return Line; }
  113. /// Get the Column of this MCDwarfLoc.
  114. unsigned getColumn() const { return Column; }
  115. /// Get the Flags of this MCDwarfLoc.
  116. unsigned getFlags() const { return Flags; }
  117. /// Get the Isa of this MCDwarfLoc.
  118. unsigned getIsa() const { return Isa; }
  119. /// Get the Discriminator of this MCDwarfLoc.
  120. unsigned getDiscriminator() const { return Discriminator; }
  121. /// Set the FileNum of this MCDwarfLoc.
  122. void setFileNum(unsigned fileNum) { FileNum = fileNum; }
  123. /// Set the Line of this MCDwarfLoc.
  124. void setLine(unsigned line) { Line = line; }
  125. /// Set the Column of this MCDwarfLoc.
  126. void setColumn(unsigned column) {
  127. assert(column <= UINT16_MAX);
  128. Column = column;
  129. }
  130. /// Set the Flags of this MCDwarfLoc.
  131. void setFlags(unsigned flags) {
  132. assert(flags <= UINT8_MAX);
  133. Flags = flags;
  134. }
  135. /// Set the Isa of this MCDwarfLoc.
  136. void setIsa(unsigned isa) {
  137. assert(isa <= UINT8_MAX);
  138. Isa = isa;
  139. }
  140. /// Set the Discriminator of this MCDwarfLoc.
  141. void setDiscriminator(unsigned discriminator) {
  142. Discriminator = discriminator;
  143. }
  144. };
  145. /// Instances of this class represent the line information for
  146. /// the dwarf line table entries. Which is created after a machine
  147. /// instruction is assembled and uses an address from a temporary label
  148. /// created at the current address in the current section and the info from
  149. /// the last .loc directive seen as stored in the context.
  150. class MCDwarfLineEntry : public MCDwarfLoc {
  151. MCSymbol *Label;
  152. private:
  153. // Allow the default copy constructor and assignment operator to be used
  154. // for an MCDwarfLineEntry object.
  155. public:
  156. // Constructor to create an MCDwarfLineEntry given a symbol and the dwarf loc.
  157. MCDwarfLineEntry(MCSymbol *label, const MCDwarfLoc loc)
  158. : MCDwarfLoc(loc), Label(label) {}
  159. MCSymbol *getLabel() const { return Label; }
  160. // This indicates the line entry is synthesized for an end entry.
  161. bool IsEndEntry = false;
  162. // Override the label with the given EndLabel.
  163. void setEndLabel(MCSymbol *EndLabel) {
  164. Label = EndLabel;
  165. IsEndEntry = true;
  166. }
  167. // This is called when an instruction is assembled into the specified
  168. // section and if there is information from the last .loc directive that
  169. // has yet to have a line entry made for it is made.
  170. static void make(MCStreamer *MCOS, MCSection *Section);
  171. };
  172. /// Instances of this class represent the line information for a compile
  173. /// unit where machine instructions have been assembled after seeing .loc
  174. /// directives. This is the information used to build the dwarf line
  175. /// table for a section.
  176. class MCLineSection {
  177. public:
  178. // Add an entry to this MCLineSection's line entries.
  179. void addLineEntry(const MCDwarfLineEntry &LineEntry, MCSection *Sec) {
  180. MCLineDivisions[Sec].push_back(LineEntry);
  181. }
  182. // Add an end entry by cloning the last entry, if exists, for the section
  183. // the given EndLabel belongs to. The label is replaced by the given EndLabel.
  184. void addEndEntry(MCSymbol *EndLabel);
  185. using MCDwarfLineEntryCollection = std::vector<MCDwarfLineEntry>;
  186. using iterator = MCDwarfLineEntryCollection::iterator;
  187. using const_iterator = MCDwarfLineEntryCollection::const_iterator;
  188. using MCLineDivisionMap = MapVector<MCSection *, MCDwarfLineEntryCollection>;
  189. private:
  190. // A collection of MCDwarfLineEntry for each section.
  191. MCLineDivisionMap MCLineDivisions;
  192. public:
  193. // Returns the collection of MCDwarfLineEntry for a given Compile Unit ID.
  194. const MCLineDivisionMap &getMCLineEntries() const {
  195. return MCLineDivisions;
  196. }
  197. };
  198. struct MCDwarfLineTableParams {
  199. /// First special line opcode - leave room for the standard opcodes.
  200. /// Note: If you want to change this, you'll have to update the
  201. /// "StandardOpcodeLengths" table that is emitted in
  202. /// \c Emit().
  203. uint8_t DWARF2LineOpcodeBase = 13;
  204. /// Minimum line offset in a special line info. opcode. The value
  205. /// -5 was chosen to give a reasonable range of values.
  206. int8_t DWARF2LineBase = -5;
  207. /// Range of line offsets in a special line info. opcode.
  208. uint8_t DWARF2LineRange = 14;
  209. };
  210. struct MCDwarfLineTableHeader {
  211. MCSymbol *Label = nullptr;
  212. SmallVector<std::string, 3> MCDwarfDirs;
  213. SmallVector<MCDwarfFile, 3> MCDwarfFiles;
  214. StringMap<unsigned> SourceIdMap;
  215. std::string CompilationDir;
  216. MCDwarfFile RootFile;
  217. bool HasSource = false;
  218. private:
  219. bool HasAllMD5 = true;
  220. bool HasAnyMD5 = false;
  221. public:
  222. MCDwarfLineTableHeader() = default;
  223. Expected<unsigned> tryGetFile(StringRef &Directory, StringRef &FileName,
  224. Optional<MD5::MD5Result> Checksum,
  225. Optional<StringRef> Source,
  226. uint16_t DwarfVersion,
  227. unsigned FileNumber = 0);
  228. std::pair<MCSymbol *, MCSymbol *>
  229. Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
  230. Optional<MCDwarfLineStr> &LineStr) const;
  231. std::pair<MCSymbol *, MCSymbol *>
  232. Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
  233. ArrayRef<char> SpecialOpcodeLengths,
  234. Optional<MCDwarfLineStr> &LineStr) const;
  235. void resetMD5Usage() {
  236. HasAllMD5 = true;
  237. HasAnyMD5 = false;
  238. }
  239. void trackMD5Usage(bool MD5Used) {
  240. HasAllMD5 &= MD5Used;
  241. HasAnyMD5 |= MD5Used;
  242. }
  243. bool isMD5UsageConsistent() const {
  244. return MCDwarfFiles.empty() || (HasAllMD5 == HasAnyMD5);
  245. }
  246. void setRootFile(StringRef Directory, StringRef FileName,
  247. Optional<MD5::MD5Result> Checksum,
  248. Optional<StringRef> Source) {
  249. CompilationDir = std::string(Directory);
  250. RootFile.Name = std::string(FileName);
  251. RootFile.DirIndex = 0;
  252. RootFile.Checksum = Checksum;
  253. RootFile.Source = Source;
  254. trackMD5Usage(Checksum.hasValue());
  255. HasSource = Source.hasValue();
  256. }
  257. void resetFileTable() {
  258. MCDwarfDirs.clear();
  259. MCDwarfFiles.clear();
  260. RootFile.Name.clear();
  261. resetMD5Usage();
  262. HasSource = false;
  263. }
  264. private:
  265. void emitV2FileDirTables(MCStreamer *MCOS) const;
  266. void emitV5FileDirTables(MCStreamer *MCOS, Optional<MCDwarfLineStr> &LineStr) const;
  267. };
  268. class MCDwarfDwoLineTable {
  269. MCDwarfLineTableHeader Header;
  270. bool HasSplitLineTable = false;
  271. public:
  272. void maybeSetRootFile(StringRef Directory, StringRef FileName,
  273. Optional<MD5::MD5Result> Checksum,
  274. Optional<StringRef> Source) {
  275. if (!Header.RootFile.Name.empty())
  276. return;
  277. Header.setRootFile(Directory, FileName, Checksum, Source);
  278. }
  279. unsigned getFile(StringRef Directory, StringRef FileName,
  280. Optional<MD5::MD5Result> Checksum, uint16_t DwarfVersion,
  281. Optional<StringRef> Source) {
  282. HasSplitLineTable = true;
  283. return cantFail(Header.tryGetFile(Directory, FileName, Checksum, Source,
  284. DwarfVersion));
  285. }
  286. void Emit(MCStreamer &MCOS, MCDwarfLineTableParams Params,
  287. MCSection *Section) const;
  288. };
  289. class MCDwarfLineTable {
  290. MCDwarfLineTableHeader Header;
  291. MCLineSection MCLineSections;
  292. public:
  293. // This emits the Dwarf file and the line tables for all Compile Units.
  294. static void emit(MCStreamer *MCOS, MCDwarfLineTableParams Params);
  295. // This emits the Dwarf file and the line tables for a given Compile Unit.
  296. void emitCU(MCStreamer *MCOS, MCDwarfLineTableParams Params,
  297. Optional<MCDwarfLineStr> &LineStr) const;
  298. // This emits a single line table associated with a given Section.
  299. static void
  300. emitOne(MCStreamer *MCOS, MCSection *Section,
  301. const MCLineSection::MCDwarfLineEntryCollection &LineEntries);
  302. Expected<unsigned> tryGetFile(StringRef &Directory, StringRef &FileName,
  303. Optional<MD5::MD5Result> Checksum,
  304. Optional<StringRef> Source,
  305. uint16_t DwarfVersion,
  306. unsigned FileNumber = 0);
  307. unsigned getFile(StringRef &Directory, StringRef &FileName,
  308. Optional<MD5::MD5Result> Checksum, Optional<StringRef> Source,
  309. uint16_t DwarfVersion, unsigned FileNumber = 0) {
  310. return cantFail(tryGetFile(Directory, FileName, Checksum, Source,
  311. DwarfVersion, FileNumber));
  312. }
  313. void setRootFile(StringRef Directory, StringRef FileName,
  314. Optional<MD5::MD5Result> Checksum, Optional<StringRef> Source) {
  315. Header.CompilationDir = std::string(Directory);
  316. Header.RootFile.Name = std::string(FileName);
  317. Header.RootFile.DirIndex = 0;
  318. Header.RootFile.Checksum = Checksum;
  319. Header.RootFile.Source = Source;
  320. Header.trackMD5Usage(Checksum.hasValue());
  321. Header.HasSource = Source.hasValue();
  322. }
  323. void resetFileTable() { Header.resetFileTable(); }
  324. bool hasRootFile() const { return !Header.RootFile.Name.empty(); }
  325. const MCDwarfFile &getRootFile() const { return Header.RootFile; }
  326. // Report whether MD5 usage has been consistent (all-or-none).
  327. bool isMD5UsageConsistent() const { return Header.isMD5UsageConsistent(); }
  328. MCSymbol *getLabel() const {
  329. return Header.Label;
  330. }
  331. void setLabel(MCSymbol *Label) {
  332. Header.Label = Label;
  333. }
  334. const SmallVectorImpl<std::string> &getMCDwarfDirs() const {
  335. return Header.MCDwarfDirs;
  336. }
  337. SmallVectorImpl<std::string> &getMCDwarfDirs() {
  338. return Header.MCDwarfDirs;
  339. }
  340. const SmallVectorImpl<MCDwarfFile> &getMCDwarfFiles() const {
  341. return Header.MCDwarfFiles;
  342. }
  343. SmallVectorImpl<MCDwarfFile> &getMCDwarfFiles() {
  344. return Header.MCDwarfFiles;
  345. }
  346. const MCLineSection &getMCLineSections() const {
  347. return MCLineSections;
  348. }
  349. MCLineSection &getMCLineSections() {
  350. return MCLineSections;
  351. }
  352. };
  353. class MCDwarfLineAddr {
  354. public:
  355. /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
  356. static void Encode(MCContext &Context, MCDwarfLineTableParams Params,
  357. int64_t LineDelta, uint64_t AddrDelta, raw_ostream &OS);
  358. /// Utility function to emit the encoding to a streamer.
  359. static void Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
  360. int64_t LineDelta, uint64_t AddrDelta);
  361. };
  362. class MCGenDwarfInfo {
  363. public:
  364. //
  365. // When generating dwarf for assembly source files this emits the Dwarf
  366. // sections.
  367. //
  368. static void Emit(MCStreamer *MCOS);
  369. };
  370. // When generating dwarf for assembly source files this is the info that is
  371. // needed to be gathered for each symbol that will have a dwarf label.
  372. class MCGenDwarfLabelEntry {
  373. private:
  374. // Name of the symbol without a leading underbar, if any.
  375. StringRef Name;
  376. // The dwarf file number this symbol is in.
  377. unsigned FileNumber;
  378. // The line number this symbol is at.
  379. unsigned LineNumber;
  380. // The low_pc for the dwarf label is taken from this symbol.
  381. MCSymbol *Label;
  382. public:
  383. MCGenDwarfLabelEntry(StringRef name, unsigned fileNumber, unsigned lineNumber,
  384. MCSymbol *label)
  385. : Name(name), FileNumber(fileNumber), LineNumber(lineNumber),
  386. Label(label) {}
  387. StringRef getName() const { return Name; }
  388. unsigned getFileNumber() const { return FileNumber; }
  389. unsigned getLineNumber() const { return LineNumber; }
  390. MCSymbol *getLabel() const { return Label; }
  391. // This is called when label is created when we are generating dwarf for
  392. // assembly source files.
  393. static void Make(MCSymbol *Symbol, MCStreamer *MCOS, SourceMgr &SrcMgr,
  394. SMLoc &Loc);
  395. };
  396. class MCCFIInstruction {
  397. public:
  398. enum OpType {
  399. OpSameValue,
  400. OpRememberState,
  401. OpRestoreState,
  402. OpOffset,
  403. OpLLVMDefAspaceCfa,
  404. OpDefCfaRegister,
  405. OpDefCfaOffset,
  406. OpDefCfa,
  407. OpRelOffset,
  408. OpAdjustCfaOffset,
  409. OpEscape,
  410. OpRestore,
  411. OpUndefined,
  412. OpRegister,
  413. OpWindowSave,
  414. OpNegateRAState,
  415. OpGnuArgsSize
  416. };
  417. private:
  418. OpType Operation;
  419. MCSymbol *Label;
  420. unsigned Register;
  421. union {
  422. int Offset;
  423. unsigned Register2;
  424. };
  425. unsigned AddressSpace;
  426. std::vector<char> Values;
  427. std::string Comment;
  428. MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R, int O, StringRef V,
  429. StringRef Comment = "")
  430. : Operation(Op), Label(L), Register(R), Offset(O),
  431. Values(V.begin(), V.end()), Comment(Comment) {
  432. assert(Op != OpRegister && Op != OpLLVMDefAspaceCfa);
  433. }
  434. MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R1, unsigned R2)
  435. : Operation(Op), Label(L), Register(R1), Register2(R2) {
  436. assert(Op == OpRegister);
  437. }
  438. MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R, int O, unsigned AS)
  439. : Operation(Op), Label(L), Register(R), Offset(O), AddressSpace(AS) {
  440. assert(Op == OpLLVMDefAspaceCfa);
  441. }
  442. public:
  443. /// .cfi_def_cfa defines a rule for computing CFA as: take address from
  444. /// Register and add Offset to it.
  445. static MCCFIInstruction cfiDefCfa(MCSymbol *L, unsigned Register,
  446. int Offset) {
  447. return MCCFIInstruction(OpDefCfa, L, Register, Offset, "");
  448. }
  449. /// .cfi_def_cfa_register modifies a rule for computing CFA. From now
  450. /// on Register will be used instead of the old one. Offset remains the same.
  451. static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register) {
  452. return MCCFIInstruction(OpDefCfaRegister, L, Register, 0, "");
  453. }
  454. /// .cfi_def_cfa_offset modifies a rule for computing CFA. Register
  455. /// remains the same, but offset is new. Note that it is the absolute offset
  456. /// that will be added to a defined register to the compute CFA address.
  457. static MCCFIInstruction cfiDefCfaOffset(MCSymbol *L, int Offset) {
  458. return MCCFIInstruction(OpDefCfaOffset, L, 0, Offset, "");
  459. }
  460. /// .cfi_adjust_cfa_offset Same as .cfi_def_cfa_offset, but
  461. /// Offset is a relative value that is added/subtracted from the previous
  462. /// offset.
  463. static MCCFIInstruction createAdjustCfaOffset(MCSymbol *L, int Adjustment) {
  464. return MCCFIInstruction(OpAdjustCfaOffset, L, 0, Adjustment, "");
  465. }
  466. // FIXME: Update the remaining docs to use the new proposal wording.
  467. /// .cfi_llvm_def_aspace_cfa defines the rule for computing the CFA to
  468. /// be the result of evaluating the DWARF operation expression
  469. /// `DW_OP_constu AS; DW_OP_aspace_bregx R, B` as a location description.
  470. static MCCFIInstruction createLLVMDefAspaceCfa(MCSymbol *L, unsigned Register,
  471. int Offset,
  472. unsigned AddressSpace) {
  473. return MCCFIInstruction(OpLLVMDefAspaceCfa, L, Register, Offset,
  474. AddressSpace);
  475. }
  476. /// .cfi_offset Previous value of Register is saved at offset Offset
  477. /// from CFA.
  478. static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register,
  479. int Offset) {
  480. return MCCFIInstruction(OpOffset, L, Register, Offset, "");
  481. }
  482. /// .cfi_rel_offset Previous value of Register is saved at offset
  483. /// Offset from the current CFA register. This is transformed to .cfi_offset
  484. /// using the known displacement of the CFA register from the CFA.
  485. static MCCFIInstruction createRelOffset(MCSymbol *L, unsigned Register,
  486. int Offset) {
  487. return MCCFIInstruction(OpRelOffset, L, Register, Offset, "");
  488. }
  489. /// .cfi_register Previous value of Register1 is saved in
  490. /// register Register2.
  491. static MCCFIInstruction createRegister(MCSymbol *L, unsigned Register1,
  492. unsigned Register2) {
  493. return MCCFIInstruction(OpRegister, L, Register1, Register2);
  494. }
  495. /// .cfi_window_save SPARC register window is saved.
  496. static MCCFIInstruction createWindowSave(MCSymbol *L) {
  497. return MCCFIInstruction(OpWindowSave, L, 0, 0, "");
  498. }
  499. /// .cfi_negate_ra_state AArch64 negate RA state.
  500. static MCCFIInstruction createNegateRAState(MCSymbol *L) {
  501. return MCCFIInstruction(OpNegateRAState, L, 0, 0, "");
  502. }
  503. /// .cfi_restore says that the rule for Register is now the same as it
  504. /// was at the beginning of the function, after all initial instructions added
  505. /// by .cfi_startproc were executed.
  506. static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register) {
  507. return MCCFIInstruction(OpRestore, L, Register, 0, "");
  508. }
  509. /// .cfi_undefined From now on the previous value of Register can't be
  510. /// restored anymore.
  511. static MCCFIInstruction createUndefined(MCSymbol *L, unsigned Register) {
  512. return MCCFIInstruction(OpUndefined, L, Register, 0, "");
  513. }
  514. /// .cfi_same_value Current value of Register is the same as in the
  515. /// previous frame. I.e., no restoration is needed.
  516. static MCCFIInstruction createSameValue(MCSymbol *L, unsigned Register) {
  517. return MCCFIInstruction(OpSameValue, L, Register, 0, "");
  518. }
  519. /// .cfi_remember_state Save all current rules for all registers.
  520. static MCCFIInstruction createRememberState(MCSymbol *L) {
  521. return MCCFIInstruction(OpRememberState, L, 0, 0, "");
  522. }
  523. /// .cfi_restore_state Restore the previously saved state.
  524. static MCCFIInstruction createRestoreState(MCSymbol *L) {
  525. return MCCFIInstruction(OpRestoreState, L, 0, 0, "");
  526. }
  527. /// .cfi_escape Allows the user to add arbitrary bytes to the unwind
  528. /// info.
  529. static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals,
  530. StringRef Comment = "") {
  531. return MCCFIInstruction(OpEscape, L, 0, 0, Vals, Comment);
  532. }
  533. /// A special wrapper for .cfi_escape that indicates GNU_ARGS_SIZE
  534. static MCCFIInstruction createGnuArgsSize(MCSymbol *L, int Size) {
  535. return MCCFIInstruction(OpGnuArgsSize, L, 0, Size, "");
  536. }
  537. OpType getOperation() const { return Operation; }
  538. MCSymbol *getLabel() const { return Label; }
  539. unsigned getRegister() const {
  540. assert(Operation == OpDefCfa || Operation == OpOffset ||
  541. Operation == OpRestore || Operation == OpUndefined ||
  542. Operation == OpSameValue || Operation == OpDefCfaRegister ||
  543. Operation == OpRelOffset || Operation == OpRegister ||
  544. Operation == OpLLVMDefAspaceCfa);
  545. return Register;
  546. }
  547. unsigned getRegister2() const {
  548. assert(Operation == OpRegister);
  549. return Register2;
  550. }
  551. unsigned getAddressSpace() const {
  552. assert(Operation == OpLLVMDefAspaceCfa);
  553. return AddressSpace;
  554. }
  555. int getOffset() const {
  556. assert(Operation == OpDefCfa || Operation == OpOffset ||
  557. Operation == OpRelOffset || Operation == OpDefCfaOffset ||
  558. Operation == OpAdjustCfaOffset || Operation == OpGnuArgsSize ||
  559. Operation == OpLLVMDefAspaceCfa);
  560. return Offset;
  561. }
  562. StringRef getValues() const {
  563. assert(Operation == OpEscape);
  564. return StringRef(&Values[0], Values.size());
  565. }
  566. StringRef getComment() const {
  567. return Comment;
  568. }
  569. };
  570. struct MCDwarfFrameInfo {
  571. MCDwarfFrameInfo() = default;
  572. MCSymbol *Begin = nullptr;
  573. MCSymbol *End = nullptr;
  574. const MCSymbol *Personality = nullptr;
  575. const MCSymbol *Lsda = nullptr;
  576. std::vector<MCCFIInstruction> Instructions;
  577. unsigned CurrentCfaRegister = 0;
  578. unsigned PersonalityEncoding = 0;
  579. unsigned LsdaEncoding = 0;
  580. uint32_t CompactUnwindEncoding = 0;
  581. bool IsSignalFrame = false;
  582. bool IsSimple = false;
  583. unsigned RAReg = static_cast<unsigned>(INT_MAX);
  584. bool IsBKeyFrame = false;
  585. };
  586. class MCDwarfFrameEmitter {
  587. public:
  588. //
  589. // This emits the frame info section.
  590. //
  591. static void Emit(MCObjectStreamer &streamer, MCAsmBackend *MAB, bool isEH);
  592. static void EmitAdvanceLoc(MCObjectStreamer &Streamer, uint64_t AddrDelta);
  593. static void EncodeAdvanceLoc(MCContext &Context, uint64_t AddrDelta,
  594. raw_ostream &OS);
  595. };
  596. } // end namespace llvm
  597. #endif // LLVM_MC_MCDWARF_H
  598. #ifdef __GNUC__
  599. #pragma GCC diagnostic pop
  600. #endif