LineProgram.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Stream.h>
  28. #include <AK/String.h>
  29. #include <AK/Vector.h>
  30. class LineProgram {
  31. public:
  32. explicit LineProgram(InputMemoryStream& stream);
  33. struct LineInfo {
  34. u32 address { 0 };
  35. String file;
  36. size_t line { 0 };
  37. };
  38. const Vector<LineInfo>& lines() const { return m_lines; }
  39. private:
  40. void parse_unit_header();
  41. void parse_source_directories();
  42. void parse_source_files();
  43. void run_program();
  44. void append_to_line_info();
  45. void reset_registers();
  46. void handle_extended_opcode();
  47. void handle_standard_opcode(u8 opcode);
  48. void handle_sepcial_opcode(u8 opcode);
  49. struct [[gnu::packed]] UnitHeader32
  50. {
  51. u32 length;
  52. u16 version;
  53. u32 header_length;
  54. u8 min_instruction_length;
  55. u8 default_is_stmt;
  56. i8 line_base;
  57. u8 line_range;
  58. u8 opcode_base;
  59. u8 std_opcode_lengths[12];
  60. };
  61. enum StandardOpcodes {
  62. Copy = 1,
  63. AdvancePc,
  64. AdvanceLine,
  65. SetFile,
  66. SetColumn,
  67. NegateStatement,
  68. SetBasicBlock,
  69. ConstAddPc,
  70. FixAdvancePc,
  71. SetProlougeEnd,
  72. SetEpilogueBegin,
  73. SetIsa
  74. };
  75. enum ExtendedOpcodes {
  76. EndSequence = 1,
  77. SetAddress,
  78. DefineFile,
  79. SetDiscriminator,
  80. };
  81. struct FileEntry {
  82. String name;
  83. size_t directory_index { 0 };
  84. };
  85. static constexpr u16 DWARF_VERSION = 3;
  86. static constexpr u8 SPECIAL_OPCODES_BASE = 13;
  87. InputMemoryStream& m_stream;
  88. size_t m_unit_offset { 0 };
  89. UnitHeader32 m_unit_header {};
  90. Vector<String> m_source_directories;
  91. Vector<FileEntry> m_source_files;
  92. // The registers of the "line program" virtual machine
  93. u32 m_address { 0 };
  94. size_t m_line { 0 };
  95. size_t m_file_index { 0 };
  96. bool m_is_statement { false };
  97. Vector<LineInfo> m_lines;
  98. };